2012-03-28 13:07:42 +02:00
|
|
|
// Copyright 2012 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This code is licensed under the same terms as WebM:
|
|
|
|
// Software License Agreement: http://www.webmproject.org/license/software/
|
|
|
|
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
2012-04-02 12:58:36 +02:00
|
|
|
// main entry for the lossless encoder.
|
2012-03-28 13:07:42 +02:00
|
|
|
//
|
|
|
|
// Author: Vikas Arora (vikaas.arora@gmail.com)
|
|
|
|
//
|
|
|
|
|
2012-04-11 11:52:13 +02:00
|
|
|
#ifdef USE_LOSSLESS_ENCODER
|
|
|
|
|
2012-03-28 13:07:42 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-04-10 09:00:36 +02:00
|
|
|
|
|
|
|
#include "./backward_references.h"
|
2012-03-28 13:07:42 +02:00
|
|
|
#include "./vp8enci.h"
|
|
|
|
#include "./vp8li.h"
|
2012-04-10 09:00:36 +02:00
|
|
|
#include "../dsp/lossless.h"
|
2012-03-28 13:07:42 +02:00
|
|
|
#include "../utils/bit_writer.h"
|
2012-04-12 13:31:17 +02:00
|
|
|
#include "../utils/huffman_encode.h"
|
2012-03-28 13:07:42 +02:00
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2012-04-25 13:14:11 +02:00
|
|
|
#define MAX_HUFF_IMAGE_SIZE (32 * 1024 * 1024)
|
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
// TODO(vikas): find a common place between enc and dec for these:
|
|
|
|
#define PREDICTOR_TRANSFORM 0
|
|
|
|
#define CROSS_COLOR_TRANSFORM 1
|
|
|
|
#define SUBTRACT_GREEN 2
|
|
|
|
#define COLOR_INDEXING_TRANSFORM 3
|
|
|
|
#define TRANSFORM_PRESENT 1
|
|
|
|
|
|
|
|
#define IMAGE_SIZE_BITS 14
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Palette
|
2012-03-28 13:07:42 +02:00
|
|
|
|
2012-04-11 06:20:16 +02:00
|
|
|
static int CompareColors(const void* p1, const void* p2) {
|
2012-04-10 09:00:36 +02:00
|
|
|
const uint32_t a = *(const uint32_t*)p1;
|
|
|
|
const uint32_t b = *(const uint32_t*)p2;
|
2012-04-25 11:40:41 +02:00
|
|
|
return (a < b) ? -1 : (a > b) ? 1 : 0;
|
2012-04-10 09:00:36 +02:00
|
|
|
}
|
|
|
|
|
2012-04-23 09:40:34 +02:00
|
|
|
// If number of colors in the image is less than or equal to MAX_PALETTE_SIZE,
|
|
|
|
// creates a palette and returns true, else returns false.
|
|
|
|
static int AnalyzeAndCreatePalette(const uint32_t* const argb, int num_pix,
|
|
|
|
uint32_t palette[MAX_PALETTE_SIZE],
|
|
|
|
int* const palette_size) {
|
2012-04-10 09:00:36 +02:00
|
|
|
int i, key;
|
2012-04-11 06:20:16 +02:00
|
|
|
int num_colors = 0;
|
|
|
|
uint8_t in_use[MAX_PALETTE_SIZE * 4] = { 0 };
|
2012-04-10 09:00:36 +02:00
|
|
|
uint32_t colors[MAX_PALETTE_SIZE * 4];
|
|
|
|
static const uint32_t kHashMul = 0x1e35a7bd;
|
|
|
|
|
|
|
|
key = (kHashMul * argb[0]) >> PALETTE_KEY_RIGHT_SHIFT;
|
|
|
|
colors[key] = argb[0];
|
|
|
|
in_use[key] = 1;
|
2012-04-11 06:20:16 +02:00
|
|
|
++num_colors;
|
2012-04-10 09:00:36 +02:00
|
|
|
|
|
|
|
for (i = 1; i < num_pix; ++i) {
|
|
|
|
if (argb[i] == argb[i - 1]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
key = (kHashMul * argb[i]) >> PALETTE_KEY_RIGHT_SHIFT;
|
|
|
|
while (1) {
|
|
|
|
if (!in_use[key]) {
|
|
|
|
colors[key] = argb[i];
|
|
|
|
in_use[key] = 1;
|
2012-04-11 06:20:16 +02:00
|
|
|
++num_colors;
|
|
|
|
if (num_colors > MAX_PALETTE_SIZE) {
|
2012-04-10 09:00:36 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else if (colors[key] == argb[i]) {
|
|
|
|
// The color is already there.
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// Some other color sits there.
|
|
|
|
// Do linear conflict resolution.
|
|
|
|
++key;
|
2012-04-11 06:20:16 +02:00
|
|
|
key &= (MAX_PALETTE_SIZE * 4 - 1); // key mask for 1K buffer.
|
2012-04-10 09:00:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-11 06:20:16 +02:00
|
|
|
num_colors = 0;
|
|
|
|
for (i = 0; i < (int)(sizeof(in_use) / sizeof(in_use[0])); ++i) {
|
2012-04-10 09:00:36 +02:00
|
|
|
if (in_use[i]) {
|
2012-04-11 06:20:16 +02:00
|
|
|
palette[num_colors] = colors[i];
|
|
|
|
++num_colors;
|
2012-04-10 09:00:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-11 06:20:16 +02:00
|
|
|
qsort(palette, num_colors, sizeof(*palette), CompareColors);
|
|
|
|
*palette_size = num_colors;
|
2012-03-28 13:07:42 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-04-10 09:00:36 +02:00
|
|
|
static int AnalyzeEntropy(const uint32_t const *argb, int xsize, int ysize,
|
2012-04-30 21:46:22 +02:00
|
|
|
double* const nonpredicted_bits,
|
|
|
|
double* const predicted_bits) {
|
2012-04-10 09:00:36 +02:00
|
|
|
int i;
|
|
|
|
VP8LHistogram* nonpredicted = NULL;
|
|
|
|
VP8LHistogram* predicted = (VP8LHistogram*)malloc(2 * sizeof(*predicted));
|
|
|
|
if (predicted == NULL) return 0;
|
2012-04-11 06:20:16 +02:00
|
|
|
nonpredicted = predicted + 1;
|
2012-04-10 09:00:36 +02:00
|
|
|
|
|
|
|
VP8LHistogramInit(predicted, 0);
|
|
|
|
VP8LHistogramInit(nonpredicted, 0);
|
|
|
|
for (i = 1; i < xsize * ysize; ++i) {
|
2012-04-26 09:19:24 +02:00
|
|
|
const uint32_t pix = argb[i];
|
|
|
|
const uint32_t pix_diff = VP8LSubPixels(pix, argb[i - 1]);
|
|
|
|
if (pix_diff == 0) continue;
|
|
|
|
if (i >= xsize && pix == argb[i - xsize]) {
|
2012-04-10 09:00:36 +02:00
|
|
|
continue;
|
|
|
|
}
|
2012-04-30 14:18:50 +02:00
|
|
|
{
|
|
|
|
const PixOrCopy pix_token = PixOrCopyCreateLiteral(pix);
|
|
|
|
const PixOrCopy pix_diff_token = PixOrCopyCreateLiteral(pix_diff);
|
|
|
|
VP8LHistogramAddSinglePixOrCopy(nonpredicted, &pix_token);
|
|
|
|
VP8LHistogramAddSinglePixOrCopy(predicted, &pix_diff_token);
|
|
|
|
}
|
2012-04-10 09:00:36 +02:00
|
|
|
}
|
2012-04-30 21:46:22 +02:00
|
|
|
*nonpredicted_bits = VP8LHistogramEstimateBitsBulk(nonpredicted);
|
|
|
|
*predicted_bits = VP8LHistogramEstimateBitsBulk(predicted);
|
2012-04-10 09:00:36 +02:00
|
|
|
free(predicted);
|
2012-03-28 13:07:42 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-04-10 09:00:36 +02:00
|
|
|
static int VP8LEncAnalyze(VP8LEncoder* const enc) {
|
|
|
|
const WebPPicture* const pic = enc->pic_;
|
2012-04-26 09:19:24 +02:00
|
|
|
assert(pic != NULL && pic->argb != NULL);
|
2012-04-10 09:00:36 +02:00
|
|
|
|
2012-04-23 20:23:18 +02:00
|
|
|
enc->use_palette_ =
|
|
|
|
AnalyzeAndCreatePalette(pic->argb, pic->width * pic->height,
|
|
|
|
enc->palette_, &enc->palette_size_);
|
|
|
|
if (!enc->use_palette_) {
|
2012-04-30 21:46:22 +02:00
|
|
|
double non_pred_entropy, pred_entropy;
|
2012-04-23 20:23:18 +02:00
|
|
|
if (!AnalyzeEntropy(pic->argb, pic->width, pic->height,
|
|
|
|
&non_pred_entropy, &pred_entropy)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-04-10 09:00:36 +02:00
|
|
|
|
2012-04-30 21:46:22 +02:00
|
|
|
if (pred_entropy < 0.95 * non_pred_entropy) {
|
2012-04-23 20:23:18 +02:00
|
|
|
enc->use_predict_ = 1;
|
|
|
|
enc->use_cross_color_ = 1;
|
|
|
|
}
|
2012-04-10 09:00:36 +02:00
|
|
|
}
|
2012-03-28 13:07:42 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
2012-03-28 13:07:42 +02:00
|
|
|
|
2012-04-12 13:31:17 +02:00
|
|
|
// Heuristics for selecting the stride ranges to collapse.
|
|
|
|
static int ValuesShouldBeCollapsedToStrideAverage(int a, int b) {
|
|
|
|
return abs(a - b) < 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the population counts in a way that the consequent
|
|
|
|
// Hufmann tree compression, especially its rle-part will be more
|
|
|
|
// likely to compress this data more efficiently.
|
|
|
|
//
|
|
|
|
// length contains the size of the histogram.
|
|
|
|
// data contains the population counts.
|
|
|
|
static int OptimizeHuffmanForRle(int length, int* counts) {
|
|
|
|
int stride;
|
|
|
|
int limit;
|
|
|
|
int sum;
|
|
|
|
uint8_t* good_for_rle;
|
|
|
|
// 1) Let's make the Huffman code more compatible with rle encoding.
|
|
|
|
int i;
|
|
|
|
for (; length >= 0; --length) {
|
|
|
|
if (length == 0) {
|
|
|
|
return 1; // All zeros.
|
|
|
|
}
|
|
|
|
if (counts[length - 1] != 0) {
|
|
|
|
// Now counts[0..length - 1] does not have trailing zeros.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 2) Let's mark all population counts that already can be encoded
|
|
|
|
// with an rle code.
|
|
|
|
good_for_rle = (uint8_t*)calloc(length, 1);
|
|
|
|
if (good_for_rle == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// Let's not spoil any of the existing good rle codes.
|
|
|
|
// Mark any seq of 0's that is longer as 5 as a good_for_rle.
|
|
|
|
// Mark any seq of non-0's that is longer as 7 as a good_for_rle.
|
|
|
|
int symbol = counts[0];
|
|
|
|
int stride = 0;
|
|
|
|
for (i = 0; i < length + 1; ++i) {
|
|
|
|
if (i == length || counts[i] != symbol) {
|
|
|
|
if ((symbol == 0 && stride >= 5) ||
|
|
|
|
(symbol != 0 && stride >= 7)) {
|
|
|
|
int k;
|
|
|
|
for (k = 0; k < stride; ++k) {
|
|
|
|
good_for_rle[i - k - 1] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stride = 1;
|
|
|
|
if (i != length) {
|
|
|
|
symbol = counts[i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
++stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 3) Let's replace those population counts that lead to more rle codes.
|
|
|
|
stride = 0;
|
|
|
|
limit = counts[0];
|
|
|
|
sum = 0;
|
|
|
|
for (i = 0; i < length + 1; ++i) {
|
|
|
|
if (i == length || good_for_rle[i] ||
|
|
|
|
(i != 0 && good_for_rle[i - 1]) ||
|
|
|
|
!ValuesShouldBeCollapsedToStrideAverage(counts[i], limit)) {
|
|
|
|
if (stride >= 4 || (stride >= 3 && sum == 0)) {
|
|
|
|
int k;
|
|
|
|
// The stride must end, collapse what we have, if we have enough (4).
|
|
|
|
int count = (sum + stride / 2) / stride;
|
|
|
|
if (count < 1) {
|
|
|
|
count = 1;
|
|
|
|
}
|
|
|
|
if (sum == 0) {
|
|
|
|
// Don't make an all zeros stride to be upgraded to ones.
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
for (k = 0; k < stride; ++k) {
|
|
|
|
// We don't want to change value at counts[i],
|
|
|
|
// that is already belonging to the next stride. Thus - 1.
|
|
|
|
counts[i - k - 1] = count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stride = 0;
|
|
|
|
sum = 0;
|
|
|
|
if (i < length - 3) {
|
|
|
|
// All interesting strides have a count of at least 4,
|
|
|
|
// at least when non-zeros.
|
|
|
|
limit = (counts[i] + counts[i + 1] +
|
|
|
|
counts[i + 2] + counts[i + 3] + 2) / 4;
|
|
|
|
} else if (i < length) {
|
|
|
|
limit = counts[i];
|
|
|
|
} else {
|
|
|
|
limit = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++stride;
|
|
|
|
if (i != length) {
|
|
|
|
sum += counts[i];
|
|
|
|
if (stride >= 4) {
|
|
|
|
limit = (sum + stride / 2) / stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(good_for_rle);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int GetHuffBitLengthsAndCodes(
|
2012-04-30 21:46:22 +02:00
|
|
|
const VP8LHistogramSet* const histogram_image,
|
2012-05-14 10:23:58 +02:00
|
|
|
HuffmanTreeCode* const huffman_codes) {
|
2012-04-12 13:31:17 +02:00
|
|
|
int i, k;
|
|
|
|
int ok = 1;
|
2012-05-14 09:30:00 +02:00
|
|
|
int total_length_size = 0;
|
|
|
|
uint8_t* mem_buf = NULL;
|
2012-04-30 21:46:22 +02:00
|
|
|
const int histogram_image_size = histogram_image->size;
|
2012-05-14 09:30:00 +02:00
|
|
|
|
|
|
|
// Iterate over all histograms and get the aggregate number of codes used.
|
2012-04-12 13:31:17 +02:00
|
|
|
for (i = 0; i < histogram_image_size; ++i) {
|
2012-05-14 10:23:58 +02:00
|
|
|
const VP8LHistogram* const histo = histogram_image->histograms[i];
|
|
|
|
HuffmanTreeCode* const codes = &huffman_codes[5 * i];
|
|
|
|
for (k = 0; k < 5; ++k) {
|
|
|
|
const int num_symbols = (k == 0) ? VP8LHistogramNumCodes(histo)
|
|
|
|
: (k == 4) ? DISTANCE_CODES_MAX
|
|
|
|
: 256;
|
|
|
|
codes[k].num_symbols = num_symbols;
|
|
|
|
total_length_size += num_symbols;
|
2012-05-14 09:30:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-14 10:19:45 +02:00
|
|
|
// Allocate and Set Huffman codes.
|
2012-05-14 09:30:00 +02:00
|
|
|
{
|
|
|
|
uint16_t* codes;
|
|
|
|
uint8_t* lengths;
|
|
|
|
const size_t total_buf_size = total_length_size * sizeof(*lengths)
|
|
|
|
+ total_length_size * sizeof(*codes);
|
|
|
|
mem_buf = (uint8_t*)calloc(total_buf_size, 1);
|
|
|
|
if (mem_buf == NULL) {
|
2012-04-12 13:31:17 +02:00
|
|
|
ok = 0;
|
2012-05-14 09:30:00 +02:00
|
|
|
goto End;
|
2012-04-12 13:31:17 +02:00
|
|
|
}
|
2012-05-14 09:30:00 +02:00
|
|
|
codes = (uint16_t*)mem_buf;
|
|
|
|
lengths = (uint8_t*)&codes[total_length_size];
|
|
|
|
for (i = 0; i < 5 * histogram_image_size; ++i) {
|
2012-05-14 10:19:45 +02:00
|
|
|
const int bit_length = huffman_codes[i].num_symbols;
|
|
|
|
huffman_codes[i].codes = codes;
|
|
|
|
huffman_codes[i].code_lengths = lengths;
|
2012-05-14 09:30:00 +02:00
|
|
|
codes += bit_length;
|
|
|
|
lengths += bit_length;
|
|
|
|
}
|
|
|
|
}
|
2012-04-12 13:31:17 +02:00
|
|
|
|
2012-05-14 09:30:00 +02:00
|
|
|
// Create Huffman trees.
|
|
|
|
for (i = 0; i < histogram_image_size; ++i) {
|
2012-05-14 10:23:58 +02:00
|
|
|
HuffmanTreeCode* const codes = &huffman_codes[5 * i];
|
2012-05-14 09:30:00 +02:00
|
|
|
VP8LHistogram* const histo = histogram_image->histograms[i];
|
2012-05-14 10:23:58 +02:00
|
|
|
const int num_literals = codes[0].num_symbols;
|
|
|
|
// For each component, optimize histogram for Huffman with RLE compression,
|
|
|
|
// and create a Huffman tree (in the form of bit lengths) for each.
|
2012-04-30 21:46:22 +02:00
|
|
|
ok = ok && OptimizeHuffmanForRle(num_literals, histo->literal_);
|
2012-05-14 09:30:00 +02:00
|
|
|
ok = ok && VP8LCreateHuffmanTree(histo->literal_, num_literals, 15,
|
2012-05-14 10:23:58 +02:00
|
|
|
codes[0].code_lengths);
|
2012-05-14 09:30:00 +02:00
|
|
|
|
2012-04-30 21:46:22 +02:00
|
|
|
ok = ok && OptimizeHuffmanForRle(256, histo->red_);
|
2012-05-14 09:30:00 +02:00
|
|
|
ok = ok && VP8LCreateHuffmanTree(histo->red_, 256, 15,
|
2012-05-14 10:23:58 +02:00
|
|
|
codes[1].code_lengths);
|
2012-05-14 09:30:00 +02:00
|
|
|
|
2012-04-30 21:46:22 +02:00
|
|
|
ok = ok && OptimizeHuffmanForRle(256, histo->blue_);
|
2012-05-14 09:30:00 +02:00
|
|
|
ok = ok && VP8LCreateHuffmanTree(histo->blue_, 256, 15,
|
2012-05-14 10:23:58 +02:00
|
|
|
codes[2].code_lengths);
|
2012-05-14 09:30:00 +02:00
|
|
|
|
2012-04-30 21:46:22 +02:00
|
|
|
ok = ok && OptimizeHuffmanForRle(256, histo->alpha_);
|
2012-05-14 09:30:00 +02:00
|
|
|
ok = ok && VP8LCreateHuffmanTree(histo->alpha_, 256, 15,
|
2012-05-14 10:23:58 +02:00
|
|
|
codes[3].code_lengths);
|
2012-05-14 09:30:00 +02:00
|
|
|
|
2012-04-30 21:46:22 +02:00
|
|
|
ok = ok && OptimizeHuffmanForRle(DISTANCE_CODES_MAX, histo->distance_);
|
2012-05-14 09:30:00 +02:00
|
|
|
ok = ok && VP8LCreateHuffmanTree(histo->distance_, DISTANCE_CODES_MAX, 15,
|
2012-05-14 10:23:58 +02:00
|
|
|
codes[4].code_lengths);
|
2012-04-12 13:31:17 +02:00
|
|
|
|
|
|
|
// Create the actual bit codes for the bit lengths.
|
2012-05-14 10:23:58 +02:00
|
|
|
// TODO(vikasa): merge with each VP8LCreateHuffmanTree() ?
|
2012-04-12 13:31:17 +02:00
|
|
|
for (k = 0; k < 5; ++k) {
|
2012-05-14 10:23:58 +02:00
|
|
|
VP8LConvertBitDepthsToSymbols(codes + k);
|
2012-04-12 13:31:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-14 09:30:00 +02:00
|
|
|
End:
|
|
|
|
if (!ok) free(mem_buf);
|
|
|
|
return ok;
|
2012-04-12 13:31:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void StoreHuffmanTreeOfHuffmanTreeToBitMask(
|
|
|
|
VP8LBitWriter* const bw, const uint8_t* code_length_bitdepth) {
|
|
|
|
// RFC 1951 will calm you down if you are worried about this funny sequence.
|
|
|
|
// This sequence is tuned from that, but more weighted for lower symbol count,
|
|
|
|
// and more spiking histograms.
|
|
|
|
static const uint8_t kStorageOrder[CODE_LENGTH_CODES] = {
|
|
|
|
17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
|
|
|
|
};
|
2012-05-11 10:16:34 +02:00
|
|
|
int i;
|
2012-04-12 13:31:17 +02:00
|
|
|
// Throw away trailing zeros:
|
2012-05-11 10:16:34 +02:00
|
|
|
int codes_to_store = CODE_LENGTH_CODES;
|
2012-04-12 13:31:17 +02:00
|
|
|
for (; codes_to_store > 4; --codes_to_store) {
|
|
|
|
if (code_length_bitdepth[kStorageOrder[codes_to_store - 1]] != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VP8LWriteBits(bw, 4, codes_to_store - 4);
|
|
|
|
for (i = 0; i < codes_to_store; ++i) {
|
|
|
|
VP8LWriteBits(bw, 3, code_length_bitdepth[kStorageOrder[i]]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-14 10:23:58 +02:00
|
|
|
static void ClearHuffmanTreeIfOnlyOneSymbol(
|
|
|
|
HuffmanTreeCode* const huffman_code) {
|
|
|
|
int k;
|
|
|
|
int count = 0;
|
|
|
|
for (k = 0; k < huffman_code->num_symbols; ++k) {
|
|
|
|
if (huffman_code->code_lengths[k] != 0) {
|
|
|
|
++count;
|
|
|
|
if (count > 1) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (k = 0; k < huffman_code->num_symbols; ++k) {
|
|
|
|
huffman_code->code_lengths[k] = 0;
|
|
|
|
huffman_code->codes[k] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-12 13:31:17 +02:00
|
|
|
static void StoreHuffmanTreeToBitMask(
|
|
|
|
VP8LBitWriter* const bw,
|
2012-05-10 18:11:47 +02:00
|
|
|
const HuffmanTreeToken* const tokens,
|
|
|
|
const int num_tokens,
|
2012-04-12 13:31:17 +02:00
|
|
|
const uint8_t* code_length_bitdepth,
|
|
|
|
const uint16_t* code_length_bitdepth_symbols) {
|
|
|
|
int i;
|
2012-05-10 18:11:47 +02:00
|
|
|
for (i = 0; i < num_tokens; ++i) {
|
|
|
|
const int ix = tokens[i].code;
|
|
|
|
const int extra_bits = tokens[i].extra_bits;
|
2012-04-12 13:31:17 +02:00
|
|
|
VP8LWriteBits(bw, code_length_bitdepth[ix],
|
|
|
|
code_length_bitdepth_symbols[ix]);
|
|
|
|
switch (ix) {
|
|
|
|
case 16:
|
2012-05-10 18:11:47 +02:00
|
|
|
VP8LWriteBits(bw, 2, extra_bits);
|
2012-04-12 13:31:17 +02:00
|
|
|
break;
|
|
|
|
case 17:
|
2012-05-10 18:11:47 +02:00
|
|
|
VP8LWriteBits(bw, 3, extra_bits);
|
2012-04-12 13:31:17 +02:00
|
|
|
break;
|
|
|
|
case 18:
|
2012-05-10 18:11:47 +02:00
|
|
|
VP8LWriteBits(bw, 7, extra_bits);
|
2012-04-12 13:31:17 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-03 19:04:32 +02:00
|
|
|
static int StoreFullHuffmanCode(VP8LBitWriter* const bw,
|
|
|
|
const uint8_t* const bit_lengths,
|
|
|
|
int bit_lengths_size) {
|
2012-04-12 13:31:17 +02:00
|
|
|
int ok = 0;
|
2012-05-03 19:04:32 +02:00
|
|
|
uint8_t code_length_bitdepth[CODE_LENGTH_CODES] = { 0 };
|
|
|
|
uint16_t code_length_bitdepth_symbols[CODE_LENGTH_CODES] = { 0 };
|
2012-05-10 18:11:47 +02:00
|
|
|
int num_tokens;
|
2012-05-14 10:19:45 +02:00
|
|
|
HuffmanTreeCode huffman_code;
|
2012-05-10 18:11:47 +02:00
|
|
|
HuffmanTreeToken* const tokens =
|
|
|
|
(HuffmanTreeToken*)malloc(bit_lengths_size * sizeof(*tokens));
|
|
|
|
if (tokens == NULL) return 0;
|
2012-04-12 13:31:17 +02:00
|
|
|
|
|
|
|
VP8LWriteBits(bw, 1, 0);
|
2012-05-10 18:11:47 +02:00
|
|
|
num_tokens = VP8LCreateCompressedHuffmanTree(bit_lengths, bit_lengths_size,
|
|
|
|
tokens, bit_lengths_size);
|
2012-05-03 19:04:32 +02:00
|
|
|
{
|
|
|
|
int histogram[CODE_LENGTH_CODES] = { 0 };
|
|
|
|
int i;
|
2012-05-10 18:11:47 +02:00
|
|
|
for (i = 0; i < num_tokens; ++i) {
|
|
|
|
++histogram[tokens[i].code];
|
2012-05-03 19:04:32 +02:00
|
|
|
}
|
2012-04-12 13:31:17 +02:00
|
|
|
|
2012-05-03 19:04:32 +02:00
|
|
|
if (!VP8LCreateHuffmanTree(histogram, CODE_LENGTH_CODES,
|
|
|
|
7, code_length_bitdepth)) {
|
|
|
|
goto End;
|
|
|
|
}
|
2012-04-12 13:31:17 +02:00
|
|
|
}
|
2012-05-14 10:19:45 +02:00
|
|
|
|
|
|
|
huffman_code.num_symbols = CODE_LENGTH_CODES;
|
|
|
|
huffman_code.code_lengths = code_length_bitdepth;
|
|
|
|
huffman_code.codes = code_length_bitdepth_symbols;
|
|
|
|
|
|
|
|
VP8LConvertBitDepthsToSymbols(&huffman_code);
|
2012-04-12 13:31:17 +02:00
|
|
|
StoreHuffmanTreeOfHuffmanTreeToBitMask(bw, code_length_bitdepth);
|
2012-05-14 10:19:45 +02:00
|
|
|
ClearHuffmanTreeIfOnlyOneSymbol(&huffman_code);
|
2012-04-12 13:31:17 +02:00
|
|
|
{
|
|
|
|
int trailing_zero_bits = 0;
|
2012-05-10 18:11:47 +02:00
|
|
|
int trimmed_length = num_tokens;
|
2012-05-03 19:04:32 +02:00
|
|
|
int write_trimmed_length;
|
2012-04-12 13:31:17 +02:00
|
|
|
int length;
|
2012-05-10 18:11:47 +02:00
|
|
|
int i = num_tokens;
|
2012-05-03 19:04:32 +02:00
|
|
|
while (i-- > 0) {
|
2012-05-10 18:11:47 +02:00
|
|
|
const int ix = tokens[i].code;
|
2012-04-12 13:31:17 +02:00
|
|
|
if (ix == 0 || ix == 17 || ix == 18) {
|
2012-05-03 19:04:32 +02:00
|
|
|
--trimmed_length; // discount trailing zeros
|
2012-04-12 13:31:17 +02:00
|
|
|
trailing_zero_bits += code_length_bitdepth[ix];
|
2012-05-03 19:04:32 +02:00
|
|
|
if (ix == 17) {
|
|
|
|
trailing_zero_bits += 3;
|
|
|
|
} else if (ix == 18) {
|
|
|
|
trailing_zero_bits += 7;
|
|
|
|
}
|
2012-04-12 13:31:17 +02:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-05-03 19:04:32 +02:00
|
|
|
write_trimmed_length = (trimmed_length > 1 && trailing_zero_bits > 12);
|
2012-05-10 18:11:47 +02:00
|
|
|
length = write_trimmed_length ? trimmed_length : num_tokens;
|
2012-05-03 19:04:32 +02:00
|
|
|
VP8LWriteBits(bw, 1, write_trimmed_length);
|
|
|
|
if (write_trimmed_length) {
|
2012-04-12 13:31:17 +02:00
|
|
|
const int nbits = VP8LBitsLog2Ceiling(trimmed_length - 1);
|
2012-05-03 19:04:32 +02:00
|
|
|
const int nbitpairs = (nbits == 0) ? 1 : (nbits + 1) / 2;
|
2012-04-12 13:31:17 +02:00
|
|
|
VP8LWriteBits(bw, 3, nbitpairs - 1);
|
|
|
|
VP8LWriteBits(bw, nbitpairs * 2, trimmed_length - 2);
|
|
|
|
}
|
2012-05-10 18:11:47 +02:00
|
|
|
StoreHuffmanTreeToBitMask(bw, tokens,
|
2012-04-12 13:31:17 +02:00
|
|
|
length, code_length_bitdepth,
|
|
|
|
code_length_bitdepth_symbols);
|
|
|
|
}
|
|
|
|
ok = 1;
|
|
|
|
End:
|
2012-05-10 18:11:47 +02:00
|
|
|
free(tokens);
|
2012-04-12 13:31:17 +02:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2012-05-03 19:04:32 +02:00
|
|
|
static int StoreHuffmanCode(VP8LBitWriter* const bw,
|
|
|
|
const uint8_t* const bit_lengths,
|
|
|
|
int bit_lengths_size) {
|
|
|
|
int i;
|
|
|
|
int count = 0;
|
|
|
|
int symbols[2] = { 0, 0 };
|
|
|
|
const int kMaxBits = 8;
|
|
|
|
const int kMaxSymbol = 1 << kMaxBits;
|
|
|
|
|
|
|
|
// Check whether it's a small tree.
|
|
|
|
for (i = 0; i < bit_lengths_size && count < 3; ++i) {
|
|
|
|
if (bit_lengths[i] != 0) {
|
|
|
|
if (count < 2) symbols[count] = i;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count == 0) { // emit minimal tree for empty cases
|
|
|
|
// bits: small tree marker: 1, count-1: 0, large 8-bit code: 0, code: 0
|
|
|
|
VP8LWriteBits(bw, 4, 0x01);
|
|
|
|
return 1;
|
|
|
|
} else if (count <= 2 && symbols[0] < kMaxSymbol && symbols[1] < kMaxSymbol) {
|
|
|
|
VP8LWriteBits(bw, 1, 1); // Small tree marker to encode 1 or 2 symbols.
|
|
|
|
VP8LWriteBits(bw, 1, count - 1);
|
|
|
|
if (symbols[0] <= 1) {
|
|
|
|
VP8LWriteBits(bw, 1, 0); // Code bit for small (1 bit) symbol value.
|
|
|
|
VP8LWriteBits(bw, 1, symbols[0]);
|
|
|
|
} else {
|
|
|
|
VP8LWriteBits(bw, 1, 1);
|
|
|
|
VP8LWriteBits(bw, 8, symbols[0]);
|
|
|
|
}
|
|
|
|
if (count == 2) {
|
|
|
|
VP8LWriteBits(bw, 8, symbols[1]);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return StoreFullHuffmanCode(bw, bit_lengths, bit_lengths_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-14 10:19:45 +02:00
|
|
|
static void WriteHuffmanCode(VP8LBitWriter* const bw,
|
|
|
|
const HuffmanTreeCode* const code, int index) {
|
|
|
|
const int depth = code->code_lengths[index];
|
|
|
|
const int symbol = code->codes[index];
|
|
|
|
VP8LWriteBits(bw, depth, symbol);
|
|
|
|
}
|
|
|
|
|
2012-04-12 13:31:17 +02:00
|
|
|
static void StoreImageToBitMask(
|
|
|
|
VP8LBitWriter* const bw, int width, int histo_bits,
|
2012-04-26 09:19:24 +02:00
|
|
|
const VP8LBackwardRefs* const refs,
|
2012-04-30 14:18:50 +02:00
|
|
|
const uint16_t* histogram_symbols,
|
2012-05-14 10:19:45 +02:00
|
|
|
HuffmanTreeCode* const huffman_codes) {
|
2012-04-12 13:31:17 +02:00
|
|
|
// x and y trace the position in the image.
|
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
|
|
|
const int histo_xsize = histo_bits ? VP8LSubSampleSize(width, histo_bits) : 1;
|
|
|
|
int i;
|
2012-04-26 09:19:24 +02:00
|
|
|
for (i = 0; i < refs->size; ++i) {
|
|
|
|
const PixOrCopy* const v = &refs->refs[i];
|
2012-04-12 13:31:17 +02:00
|
|
|
const int histogram_ix = histogram_symbols[histo_bits ?
|
|
|
|
(y >> histo_bits) * histo_xsize +
|
|
|
|
(x >> histo_bits) : 0];
|
2012-05-14 10:23:58 +02:00
|
|
|
const HuffmanTreeCode* const codes = huffman_codes + 5 * histogram_ix;
|
2012-04-26 09:19:24 +02:00
|
|
|
if (PixOrCopyIsCacheIdx(v)) {
|
|
|
|
const int code = PixOrCopyCacheIdx(v);
|
|
|
|
const int literal_ix = 256 + kLengthCodes + code;
|
2012-05-14 10:23:58 +02:00
|
|
|
WriteHuffmanCode(bw, codes, literal_ix);
|
2012-04-26 09:19:24 +02:00
|
|
|
} else if (PixOrCopyIsLiteral(v)) {
|
2012-05-11 10:16:34 +02:00
|
|
|
static const int order[] = { 1, 2, 0, 3 };
|
2012-04-12 13:31:17 +02:00
|
|
|
int k;
|
|
|
|
for (k = 0; k < 4; ++k) {
|
2012-04-26 09:19:24 +02:00
|
|
|
const int code = PixOrCopyLiteral(v, order[k]);
|
2012-05-14 10:23:58 +02:00
|
|
|
WriteHuffmanCode(bw, codes + k, code);
|
2012-04-12 13:31:17 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int bits, n_bits;
|
|
|
|
int code, distance;
|
2012-05-14 10:23:58 +02:00
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
PrefixEncode(v->len, &code, &n_bits, &bits);
|
2012-05-14 10:23:58 +02:00
|
|
|
WriteHuffmanCode(bw, codes, 256 + code);
|
2012-04-12 13:31:17 +02:00
|
|
|
VP8LWriteBits(bw, n_bits, bits);
|
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
distance = PixOrCopyDistance(v);
|
2012-04-12 13:31:17 +02:00
|
|
|
PrefixEncode(distance, &code, &n_bits, &bits);
|
2012-05-14 10:23:58 +02:00
|
|
|
WriteHuffmanCode(bw, codes + 4, code);
|
2012-04-12 13:31:17 +02:00
|
|
|
VP8LWriteBits(bw, n_bits, bits);
|
|
|
|
}
|
2012-04-26 09:19:24 +02:00
|
|
|
x += PixOrCopyLength(v);
|
2012-04-12 13:31:17 +02:00
|
|
|
while (x >= width) {
|
|
|
|
x -= width;
|
|
|
|
++y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-10 09:00:36 +02:00
|
|
|
static int EncodeImageInternal(VP8LBitWriter* const bw,
|
|
|
|
const uint32_t* const argb,
|
|
|
|
int width, int height, int quality,
|
|
|
|
int cache_bits, int histogram_bits) {
|
2012-04-12 13:31:17 +02:00
|
|
|
int i;
|
|
|
|
int ok = 0;
|
|
|
|
int write_histogram_image;
|
|
|
|
const int use_2d_locality = 1;
|
2012-04-18 10:58:01 +02:00
|
|
|
const int use_color_cache = (cache_bits > 0);
|
2012-04-30 21:46:22 +02:00
|
|
|
const int histogram_image_xysize =
|
|
|
|
VP8LSubSampleSize(width, histogram_bits) *
|
2012-04-12 13:31:17 +02:00
|
|
|
VP8LSubSampleSize(height, histogram_bits);
|
2012-04-30 21:46:22 +02:00
|
|
|
VP8LHistogramSet* histogram_image =
|
|
|
|
VP8LAllocateHistogramSet(histogram_image_xysize, 0);
|
2012-05-08 03:32:04 +02:00
|
|
|
int histogram_image_size = 0;
|
2012-05-09 02:12:00 +02:00
|
|
|
int bit_array_size = 0;
|
2012-05-14 10:19:45 +02:00
|
|
|
HuffmanTreeCode* huffman_codes = NULL;
|
2012-04-25 15:37:36 +02:00
|
|
|
VP8LBackwardRefs refs;
|
2012-04-30 14:18:50 +02:00
|
|
|
uint16_t* const histogram_symbols =
|
|
|
|
(uint16_t*)malloc(histogram_image_xysize * sizeof(*histogram_symbols));
|
2012-04-12 13:31:17 +02:00
|
|
|
|
2012-04-27 21:37:50 +02:00
|
|
|
if (histogram_image == NULL || histogram_symbols == NULL) goto Error;
|
2012-04-12 13:31:17 +02:00
|
|
|
|
|
|
|
// Calculate backward references from ARGB image.
|
2012-05-09 08:41:55 +02:00
|
|
|
if (!VP8LGetBackwardReferences(width, height, argb, quality, cache_bits,
|
|
|
|
use_2d_locality, &refs)) {
|
2012-04-12 13:31:17 +02:00
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
// Build histogram image & symbols from backward references.
|
2012-04-30 14:18:50 +02:00
|
|
|
if (!VP8LGetHistoImageSymbols(width, height, &refs,
|
|
|
|
quality, histogram_bits, cache_bits,
|
2012-04-30 21:46:22 +02:00
|
|
|
histogram_image,
|
2012-04-30 14:18:50 +02:00
|
|
|
histogram_symbols)) {
|
2012-04-12 13:31:17 +02:00
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
// Create Huffman bit lengths & codes for each histogram image.
|
2012-04-30 21:46:22 +02:00
|
|
|
histogram_image_size = histogram_image->size;
|
2012-05-09 02:12:00 +02:00
|
|
|
bit_array_size = 5 * histogram_image_size;
|
2012-05-14 10:19:45 +02:00
|
|
|
huffman_codes = (HuffmanTreeCode*)calloc(bit_array_size,
|
|
|
|
sizeof(*huffman_codes));
|
|
|
|
if (huffman_codes == NULL ||
|
2012-05-14 10:23:58 +02:00
|
|
|
!GetHuffBitLengthsAndCodes(histogram_image, huffman_codes)) {
|
2012-04-12 13:31:17 +02:00
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
|
2012-04-25 11:40:41 +02:00
|
|
|
// Color Cache parameters.
|
|
|
|
VP8LWriteBits(bw, 1, use_color_cache);
|
|
|
|
if (use_color_cache) {
|
|
|
|
VP8LWriteBits(bw, 4, cache_bits);
|
|
|
|
}
|
|
|
|
|
2012-04-12 13:31:17 +02:00
|
|
|
// Huffman image + meta huffman.
|
|
|
|
write_histogram_image = (histogram_image_size > 1);
|
|
|
|
VP8LWriteBits(bw, 1, write_histogram_image);
|
|
|
|
if (write_histogram_image) {
|
2012-04-30 14:18:50 +02:00
|
|
|
uint32_t* const histogram_argb =
|
|
|
|
(uint32_t*)malloc(histogram_image_xysize * sizeof(*histogram_argb));
|
2012-04-25 11:40:41 +02:00
|
|
|
int max_index = 0;
|
2012-04-12 13:31:17 +02:00
|
|
|
if (histogram_argb == NULL) goto Error;
|
2012-04-25 11:40:41 +02:00
|
|
|
for (i = 0; i < histogram_image_xysize; ++i) {
|
|
|
|
const int index = histogram_symbols[i] & 0xffff;
|
|
|
|
histogram_argb[i] = 0xff000000 | (index << 8);
|
|
|
|
if (index >= max_index) {
|
|
|
|
max_index = index + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
histogram_image_size = max_index;
|
2012-04-12 13:31:17 +02:00
|
|
|
|
|
|
|
VP8LWriteBits(bw, 4, histogram_bits);
|
2012-04-25 11:40:41 +02:00
|
|
|
ok = EncodeImageInternal(bw, histogram_argb,
|
2012-04-12 13:31:17 +02:00
|
|
|
VP8LSubSampleSize(width, histogram_bits),
|
|
|
|
VP8LSubSampleSize(height, histogram_bits),
|
2012-04-25 11:40:41 +02:00
|
|
|
quality, 0, 0);
|
2012-04-12 13:31:17 +02:00
|
|
|
free(histogram_argb);
|
2012-04-25 11:40:41 +02:00
|
|
|
if (!ok) goto Error;
|
2012-04-12 13:31:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store Huffman codes.
|
2012-05-14 10:23:58 +02:00
|
|
|
for (i = 0; i < 5 * histogram_image_size; ++i) {
|
|
|
|
const HuffmanTreeCode* const codes = &huffman_codes[i];
|
|
|
|
if (!StoreHuffmanCode(bw, codes->code_lengths, codes->num_symbols)) {
|
|
|
|
goto Error;
|
2012-04-12 13:31:17 +02:00
|
|
|
}
|
2012-05-14 10:23:58 +02:00
|
|
|
ClearHuffmanTreeIfOnlyOneSymbol(&huffman_codes[i]);
|
2012-04-12 13:31:17 +02:00
|
|
|
}
|
2012-04-13 09:14:16 +02:00
|
|
|
|
2012-04-12 13:31:17 +02:00
|
|
|
// Free combined histograms.
|
2012-04-30 21:46:22 +02:00
|
|
|
free(histogram_image);
|
2012-04-13 09:18:09 +02:00
|
|
|
histogram_image = NULL;
|
2012-04-12 13:31:17 +02:00
|
|
|
|
|
|
|
// Store actual literals.
|
2012-04-26 09:19:24 +02:00
|
|
|
StoreImageToBitMask(bw, width, histogram_bits, &refs,
|
2012-05-14 10:19:45 +02:00
|
|
|
histogram_symbols, huffman_codes);
|
2012-04-12 13:31:17 +02:00
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
Error:
|
2012-04-30 21:46:22 +02:00
|
|
|
if (!ok) free(histogram_image);
|
|
|
|
|
2012-04-25 15:37:36 +02:00
|
|
|
VP8LClearBackwardRefs(&refs);
|
2012-05-14 10:19:45 +02:00
|
|
|
if (huffman_codes != NULL) {
|
|
|
|
free(huffman_codes->codes);
|
|
|
|
free(huffman_codes);
|
|
|
|
}
|
2012-04-12 13:31:17 +02:00
|
|
|
free(histogram_symbols);
|
|
|
|
return ok;
|
2012-03-28 13:07:42 +02:00
|
|
|
}
|
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Transforms
|
|
|
|
|
|
|
|
// Check if it would be a good idea to subtract green from red and blue. We
|
|
|
|
// only impact entropy in red/blue components, don't bother to look at others.
|
|
|
|
static int EvalAndApplySubtractGreen(const VP8LEncoder* const enc,
|
|
|
|
int width, int height,
|
|
|
|
VP8LBitWriter* const bw) {
|
2012-04-23 20:23:18 +02:00
|
|
|
if (!enc->use_palette_) {
|
|
|
|
int i;
|
2012-04-26 09:19:24 +02:00
|
|
|
const uint32_t* const argb = enc->argb_;
|
2012-04-30 14:18:50 +02:00
|
|
|
double bit_cost_before, bit_cost_after;
|
2012-04-26 09:19:24 +02:00
|
|
|
VP8LHistogram* const histo = (VP8LHistogram*)malloc(sizeof(*histo));
|
|
|
|
if (histo == NULL) return 0;
|
|
|
|
|
|
|
|
VP8LHistogramInit(histo, 1);
|
2012-04-23 20:23:18 +02:00
|
|
|
for (i = 0; i < width * height; ++i) {
|
2012-04-26 09:19:24 +02:00
|
|
|
const uint32_t c = argb[i];
|
|
|
|
++histo->red_[(c >> 16) & 0xff];
|
|
|
|
++histo->blue_[(c >> 0) & 0xff];
|
|
|
|
}
|
|
|
|
bit_cost_before = VP8LHistogramEstimateBits(histo);
|
|
|
|
|
|
|
|
VP8LHistogramInit(histo, 1);
|
|
|
|
for (i = 0; i < width * height; ++i) {
|
|
|
|
const uint32_t c = argb[i];
|
2012-04-23 20:23:18 +02:00
|
|
|
const int green = (c >> 8) & 0xff;
|
2012-04-26 09:19:24 +02:00
|
|
|
++histo->red_[((c >> 16) - green) & 0xff];
|
|
|
|
++histo->blue_[((c >> 0) - green) & 0xff];
|
2012-04-23 20:23:18 +02:00
|
|
|
}
|
2012-04-26 09:19:24 +02:00
|
|
|
bit_cost_after = VP8LHistogramEstimateBits(histo);
|
|
|
|
free(histo);
|
|
|
|
|
2012-04-23 20:23:18 +02:00
|
|
|
// Check if subtracting green yields low entropy.
|
2012-04-26 09:19:24 +02:00
|
|
|
if (bit_cost_after < bit_cost_before) {
|
|
|
|
VP8LWriteBits(bw, 1, TRANSFORM_PRESENT);
|
|
|
|
VP8LWriteBits(bw, 2, SUBTRACT_GREEN);
|
2012-04-23 20:23:18 +02:00
|
|
|
VP8LSubtractGreenFromBlueAndRed(enc->argb_, width * height);
|
|
|
|
}
|
2012-04-10 09:00:36 +02:00
|
|
|
}
|
2012-03-28 13:07:42 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
static int ApplyPredictFilter(const VP8LEncoder* const enc,
|
|
|
|
int width, int height, int quality,
|
|
|
|
VP8LBitWriter* const bw) {
|
2012-04-10 09:00:36 +02:00
|
|
|
const int pred_bits = enc->transform_bits_;
|
|
|
|
const int transform_width = VP8LSubSampleSize(width, pred_bits);
|
|
|
|
const int transform_height = VP8LSubSampleSize(height, pred_bits);
|
|
|
|
|
2012-04-13 09:01:11 +02:00
|
|
|
VP8LResidualImage(width, height, pred_bits, enc->argb_, enc->argb_scratch_,
|
|
|
|
enc->transform_data_);
|
2012-04-26 09:19:24 +02:00
|
|
|
VP8LWriteBits(bw, 1, TRANSFORM_PRESENT);
|
|
|
|
VP8LWriteBits(bw, 2, PREDICTOR_TRANSFORM);
|
2012-04-10 09:00:36 +02:00
|
|
|
VP8LWriteBits(bw, 4, pred_bits);
|
|
|
|
if (!EncodeImageInternal(bw, enc->transform_data_,
|
|
|
|
transform_width, transform_height, quality, 0, 0)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
static int ApplyCrossColorFilter(const VP8LEncoder* const enc,
|
|
|
|
int width, int height, int quality,
|
|
|
|
VP8LBitWriter* const bw) {
|
2012-04-10 09:00:36 +02:00
|
|
|
const int ccolor_transform_bits = enc->transform_bits_;
|
|
|
|
const int transform_width = VP8LSubSampleSize(width, ccolor_transform_bits);
|
|
|
|
const int transform_height = VP8LSubSampleSize(height, ccolor_transform_bits);
|
2012-04-11 06:20:16 +02:00
|
|
|
const int step = (quality == 0) ? 32 : 8;
|
2012-04-10 09:00:36 +02:00
|
|
|
|
2012-04-11 06:20:16 +02:00
|
|
|
VP8LColorSpaceTransform(width, height, ccolor_transform_bits, step,
|
2012-04-10 09:00:36 +02:00
|
|
|
enc->argb_, enc->transform_data_);
|
2012-04-26 09:19:24 +02:00
|
|
|
VP8LWriteBits(bw, 1, TRANSFORM_PRESENT);
|
|
|
|
VP8LWriteBits(bw, 2, CROSS_COLOR_TRANSFORM);
|
2012-04-10 09:00:36 +02:00
|
|
|
VP8LWriteBits(bw, 4, ccolor_transform_bits);
|
|
|
|
if (!EncodeImageInternal(bw, enc->transform_data_,
|
|
|
|
transform_width, transform_height, quality, 0, 0)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2012-03-28 13:07:42 +02:00
|
|
|
}
|
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2012-03-28 13:07:42 +02:00
|
|
|
static void PutLE32(uint8_t* const data, uint32_t val) {
|
|
|
|
data[0] = (val >> 0) & 0xff;
|
|
|
|
data[1] = (val >> 8) & 0xff;
|
|
|
|
data[2] = (val >> 16) & 0xff;
|
|
|
|
data[3] = (val >> 24) & 0xff;
|
|
|
|
}
|
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
static WebPEncodingError WriteRiffHeader(const VP8LEncoder* const enc,
|
2012-03-28 13:07:42 +02:00
|
|
|
size_t riff_size, size_t vp8l_size) {
|
|
|
|
const WebPPicture* const pic = enc->pic_;
|
|
|
|
uint8_t riff[HEADER_SIZE + SIGNATURE_SIZE] = {
|
|
|
|
'R', 'I', 'F', 'F', 0, 0, 0, 0, 'W', 'E', 'B', 'P',
|
|
|
|
'V', 'P', '8', 'L', 0, 0, 0, 0, LOSSLESS_MAGIC_BYTE,
|
|
|
|
};
|
|
|
|
PutLE32(riff + TAG_SIZE, (uint32_t)riff_size);
|
|
|
|
PutLE32(riff + RIFF_HEADER_SIZE + TAG_SIZE, (uint32_t)vp8l_size);
|
|
|
|
if (!pic->writer(riff, sizeof(riff), pic)) {
|
|
|
|
return VP8_ENC_ERROR_BAD_WRITE;
|
|
|
|
}
|
|
|
|
return VP8_ENC_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
static void WriteImageSize(VP8LEncoder* const enc, VP8LBitWriter* const bw) {
|
|
|
|
WebPPicture* const pic = enc->pic_;
|
|
|
|
const int width = pic->width - 1;
|
|
|
|
const int height = pic->height -1;
|
|
|
|
assert(width < WEBP_MAX_DIMENSION && height < WEBP_MAX_DIMENSION);
|
|
|
|
|
|
|
|
VP8LWriteBits(bw, IMAGE_SIZE_BITS, width);
|
|
|
|
VP8LWriteBits(bw, IMAGE_SIZE_BITS, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
static WebPEncodingError WriteImage(const VP8LEncoder* const enc,
|
2012-05-09 08:57:34 +02:00
|
|
|
VP8LBitWriter* const bw,
|
|
|
|
size_t* const coded_size) {
|
2012-04-02 12:58:36 +02:00
|
|
|
const WebPPicture* const pic = enc->pic_;
|
2012-03-28 13:07:42 +02:00
|
|
|
WebPEncodingError err = VP8_ENC_OK;
|
2012-04-02 12:58:36 +02:00
|
|
|
const uint8_t* const webpll_data = VP8LBitWriterFinish(bw);
|
2012-05-14 07:49:00 +02:00
|
|
|
const size_t webpll_size = VP8LBitWriterNumBytes(bw);
|
|
|
|
const size_t vp8l_size = SIGNATURE_SIZE + webpll_size;
|
|
|
|
const size_t pad = vp8l_size & 1;
|
|
|
|
const size_t riff_size = TAG_SIZE + CHUNK_HEADER_SIZE + vp8l_size + pad;
|
2012-03-28 13:07:42 +02:00
|
|
|
|
|
|
|
err = WriteRiffHeader(enc, riff_size, vp8l_size);
|
|
|
|
if (err != VP8_ENC_OK) goto Error;
|
|
|
|
|
|
|
|
if (!pic->writer(webpll_data, webpll_size, pic)) {
|
|
|
|
err = VP8_ENC_ERROR_BAD_WRITE;
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pad) {
|
|
|
|
const uint8_t pad_byte[1] = { 0 };
|
|
|
|
if (!pic->writer(pad_byte, 1, pic)) {
|
|
|
|
err = VP8_ENC_ERROR_BAD_WRITE;
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
}
|
2012-05-09 08:57:34 +02:00
|
|
|
*coded_size = vp8l_size;
|
2012-03-28 13:07:42 +02:00
|
|
|
return VP8_ENC_OK;
|
|
|
|
|
|
|
|
Error:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
2012-03-28 13:07:42 +02:00
|
|
|
|
2012-04-13 09:01:11 +02:00
|
|
|
// Allocates the memory for argb (W x H) buffer, 2 rows of context for
|
|
|
|
// prediction and transform data.
|
2012-04-11 06:20:16 +02:00
|
|
|
static WebPEncodingError AllocateTransformBuffer(VP8LEncoder* const enc,
|
2012-04-26 09:19:24 +02:00
|
|
|
int width, int height) {
|
2012-04-10 09:00:36 +02:00
|
|
|
WebPEncodingError err = VP8_ENC_OK;
|
2012-04-17 10:29:05 +02:00
|
|
|
const size_t tile_size = 1 << enc->transform_bits_;
|
2012-04-10 09:00:36 +02:00
|
|
|
const size_t image_size = height * width;
|
2012-04-19 14:11:28 +02:00
|
|
|
const size_t argb_scratch_size = (tile_size + 1) * width;
|
2012-04-10 09:00:36 +02:00
|
|
|
const size_t transform_data_size =
|
|
|
|
VP8LSubSampleSize(height, enc->transform_bits_) *
|
|
|
|
VP8LSubSampleSize(width, enc->transform_bits_);
|
2012-04-13 09:01:11 +02:00
|
|
|
const size_t total_size =
|
|
|
|
image_size + argb_scratch_size + transform_data_size;
|
|
|
|
uint32_t* mem = (uint32_t*)malloc(total_size * sizeof(*mem));
|
|
|
|
if (mem == NULL) {
|
2012-04-10 09:00:36 +02:00
|
|
|
err = VP8_ENC_ERROR_OUT_OF_MEMORY;
|
|
|
|
goto Error;
|
|
|
|
}
|
2012-04-13 09:01:11 +02:00
|
|
|
enc->argb_ = mem;
|
|
|
|
mem += image_size;
|
|
|
|
enc->argb_scratch_ = mem;
|
|
|
|
mem += argb_scratch_size;
|
|
|
|
enc->transform_data_ = mem;
|
2012-04-11 06:20:16 +02:00
|
|
|
enc->current_width_ = width;
|
|
|
|
|
|
|
|
Error:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
// Bundles multiple (2, 4 or 8) pixels into a single pixel.
|
|
|
|
// Returns the new xsize.
|
|
|
|
static void BundleColorMap(const uint32_t* const argb,
|
|
|
|
int width, int height, int xbits,
|
|
|
|
uint32_t* bundled_argb, int xs) {
|
|
|
|
int x, y;
|
|
|
|
const int bit_depth = 1 << (3 - xbits);
|
|
|
|
uint32_t code = 0;
|
|
|
|
|
|
|
|
for (y = 0; y < height; ++y) {
|
|
|
|
for (x = 0; x < width; ++x) {
|
|
|
|
const int mask = (1 << xbits) - 1;
|
|
|
|
const int xsub = x & mask;
|
|
|
|
if (xsub == 0) {
|
|
|
|
code = 0;
|
|
|
|
}
|
|
|
|
// TODO(vikasa): simplify the bundling logic.
|
|
|
|
code |= (argb[y * width + x] & 0xff00) << (bit_depth * xsub);
|
|
|
|
bundled_argb[y * xs + (x >> xbits)] = 0xff000000 | code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-23 09:40:34 +02:00
|
|
|
// Note: Expects "enc->palette_" to be set properly.
|
|
|
|
// Also, "enc->palette_" will be modified after this call and should not be used
|
|
|
|
// later.
|
2012-04-11 06:20:16 +02:00
|
|
|
static WebPEncodingError ApplyPalette(VP8LBitWriter* const bw,
|
|
|
|
VP8LEncoder* const enc,
|
|
|
|
int width, int height, int quality) {
|
|
|
|
WebPEncodingError err = VP8_ENC_OK;
|
|
|
|
int i;
|
2012-04-26 09:19:24 +02:00
|
|
|
uint32_t* const argb = enc->pic_->argb;
|
2012-04-20 16:43:01 +02:00
|
|
|
uint32_t* const palette = enc->palette_;
|
2012-04-11 06:20:16 +02:00
|
|
|
const int palette_size = enc->palette_size_;
|
|
|
|
|
2012-04-20 16:43:01 +02:00
|
|
|
// Replace each input pixel by corresponding palette index.
|
2012-04-11 06:20:16 +02:00
|
|
|
for (i = 0; i < width * height; ++i) {
|
|
|
|
int k;
|
|
|
|
for (k = 0; k < palette_size; ++k) {
|
|
|
|
const uint32_t pix = argb[i];
|
|
|
|
if (pix == palette[k]) {
|
|
|
|
argb[i] = 0xff000000u | (k << 8);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-20 16:43:01 +02:00
|
|
|
|
|
|
|
// Save palette to bitstream.
|
2012-04-26 09:19:24 +02:00
|
|
|
VP8LWriteBits(bw, 1, TRANSFORM_PRESENT);
|
|
|
|
VP8LWriteBits(bw, 2, COLOR_INDEXING_TRANSFORM);
|
2012-04-11 06:20:16 +02:00
|
|
|
VP8LWriteBits(bw, 8, palette_size - 1);
|
|
|
|
for (i = palette_size - 1; i >= 1; --i) {
|
2012-04-20 16:43:01 +02:00
|
|
|
palette[i] = VP8LSubPixels(palette[i], palette[i - 1]);
|
2012-04-11 06:20:16 +02:00
|
|
|
}
|
2012-04-20 16:43:01 +02:00
|
|
|
if (!EncodeImageInternal(bw, palette, palette_size, 1, quality, 0, 0)) {
|
2012-04-11 06:20:16 +02:00
|
|
|
err = VP8_ENC_ERROR_INVALID_CONFIGURATION;
|
|
|
|
goto Error;
|
|
|
|
}
|
2012-04-20 16:43:01 +02:00
|
|
|
|
2012-04-11 06:20:16 +02:00
|
|
|
if (palette_size <= 16) {
|
2012-04-20 16:43:01 +02:00
|
|
|
// Image can be packed (multiple pixels per uint32_t).
|
2012-04-11 06:20:16 +02:00
|
|
|
int xbits = 1;
|
|
|
|
if (palette_size <= 2) {
|
|
|
|
xbits = 3;
|
|
|
|
} else if (palette_size <= 4) {
|
|
|
|
xbits = 2;
|
|
|
|
}
|
2012-04-26 09:19:24 +02:00
|
|
|
err = AllocateTransformBuffer(enc, VP8LSubSampleSize(width, xbits), height);
|
2012-04-11 06:20:16 +02:00
|
|
|
if (err != VP8_ENC_OK) goto Error;
|
|
|
|
BundleColorMap(argb, width, height, xbits, enc->argb_, enc->current_width_);
|
|
|
|
}
|
2012-04-10 09:00:36 +02:00
|
|
|
|
|
|
|
Error:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static int GetHistoBits(const WebPConfig* const config,
|
|
|
|
const WebPPicture* const pic) {
|
|
|
|
const int width = pic->width;
|
|
|
|
const int height = pic->height;
|
|
|
|
const size_t hist_size = sizeof(VP8LHistogram);
|
|
|
|
int histo_bits = 9 - (int)(config->quality / 16.f + .5f);
|
|
|
|
while (1) {
|
|
|
|
const size_t huff_image_size = VP8LSubSampleSize(width, histo_bits) *
|
|
|
|
VP8LSubSampleSize(height, histo_bits) *
|
|
|
|
hist_size;
|
|
|
|
if (huff_image_size <= MAX_HUFF_IMAGE_SIZE) break;
|
|
|
|
++histo_bits;
|
|
|
|
}
|
|
|
|
return (histo_bits < 3) ? 3 : (histo_bits > 10) ? 10 : histo_bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void InitEncParams(VP8LEncoder* const enc) {
|
|
|
|
const WebPConfig* const config = enc->config_;
|
|
|
|
const WebPPicture* const picture = enc->pic_;
|
|
|
|
const int method = config->method;
|
|
|
|
const float quality = config->quality;
|
|
|
|
enc->transform_bits_ = (method < 4) ? 5 : (method > 4) ? 3 : 4;
|
|
|
|
enc->histo_bits_ = GetHistoBits(config, picture);
|
|
|
|
enc->cache_bits_ = (quality <= 25.f) ? 0 : 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// VP8LEncoder
|
|
|
|
|
|
|
|
static VP8LEncoder* NewVP8LEncoder(const WebPConfig* const config,
|
|
|
|
WebPPicture* const picture) {
|
|
|
|
VP8LEncoder* const enc = (VP8LEncoder*)calloc(1, sizeof(*enc));
|
|
|
|
if (enc == NULL) {
|
|
|
|
WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
enc->config_ = config;
|
|
|
|
enc->pic_ = picture;
|
|
|
|
return enc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DeleteVP8LEncoder(VP8LEncoder* enc) {
|
|
|
|
free(enc->argb_);
|
|
|
|
free(enc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Main call
|
|
|
|
|
2012-03-28 13:07:42 +02:00
|
|
|
int VP8LEncodeImage(const WebPConfig* const config,
|
|
|
|
WebPPicture* const picture) {
|
|
|
|
int ok = 0;
|
2012-04-10 09:00:36 +02:00
|
|
|
int width, height, quality;
|
2012-05-09 08:57:34 +02:00
|
|
|
size_t coded_size;
|
2012-03-28 13:07:42 +02:00
|
|
|
VP8LEncoder* enc = NULL;
|
|
|
|
WebPEncodingError err = VP8_ENC_OK;
|
2012-04-02 12:58:36 +02:00
|
|
|
VP8LBitWriter bw;
|
2012-03-28 13:07:42 +02:00
|
|
|
|
|
|
|
if (config == NULL || picture == NULL) return 0;
|
2012-04-02 12:58:36 +02:00
|
|
|
|
|
|
|
if (picture->argb == NULL) {
|
|
|
|
err = VP8_ENC_ERROR_NULL_PARAMETER;
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
enc = NewVP8LEncoder(config, picture);
|
2012-04-02 12:58:36 +02:00
|
|
|
if (enc == NULL) {
|
2012-04-26 09:19:24 +02:00
|
|
|
err = VP8_ENC_ERROR_OUT_OF_MEMORY;
|
2012-04-02 12:58:36 +02:00
|
|
|
goto Error;
|
|
|
|
}
|
2012-04-10 09:00:36 +02:00
|
|
|
width = picture->width;
|
|
|
|
height = picture->height;
|
|
|
|
quality = config->quality;
|
2012-04-02 12:58:36 +02:00
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
InitEncParams(enc);
|
2012-03-28 13:07:42 +02:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Analyze image (entropy, num_palettes etc)
|
|
|
|
|
2012-04-10 09:00:36 +02:00
|
|
|
if (!VP8LEncAnalyze(enc)) {
|
|
|
|
err = VP8_ENC_ERROR_OUT_OF_MEMORY;
|
|
|
|
goto Error;
|
2012-03-28 13:07:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write image size.
|
2012-04-26 09:19:24 +02:00
|
|
|
VP8LBitWriterInit(&bw, (width * height) >> 1);
|
2012-04-02 12:58:36 +02:00
|
|
|
WriteImageSize(enc, &bw);
|
2012-03-28 13:07:42 +02:00
|
|
|
|
2012-04-10 09:00:36 +02:00
|
|
|
if (enc->use_palette_) {
|
2012-04-11 06:20:16 +02:00
|
|
|
err = ApplyPalette(&bw, enc, width, height, quality);
|
|
|
|
if (err != VP8_ENC_OK) goto Error;
|
2012-04-26 09:19:24 +02:00
|
|
|
enc->cache_bits_ = 0;
|
2012-04-10 09:00:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// In case image is not packed.
|
|
|
|
if (enc->argb_ == NULL) {
|
2012-04-11 06:20:16 +02:00
|
|
|
const size_t image_size = height * width;
|
2012-04-26 09:19:24 +02:00
|
|
|
err = AllocateTransformBuffer(enc, width, height);
|
2012-04-10 09:00:36 +02:00
|
|
|
if (err != VP8_ENC_OK) goto Error;
|
|
|
|
memcpy(enc->argb_, picture->argb, image_size * sizeof(*enc->argb_));
|
2012-04-11 06:20:16 +02:00
|
|
|
enc->current_width_ = width;
|
2012-04-10 09:00:36 +02:00
|
|
|
}
|
|
|
|
|
2012-03-28 13:07:42 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Apply transforms and write transform data.
|
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
if (!EvalAndApplySubtractGreen(enc, enc->current_width_, height, &bw)) {
|
2012-04-10 09:00:36 +02:00
|
|
|
err = VP8_ENC_ERROR_OUT_OF_MEMORY;
|
|
|
|
goto Error;
|
|
|
|
}
|
2012-03-28 13:07:42 +02:00
|
|
|
|
|
|
|
if (enc->use_predict_) {
|
2012-04-26 09:19:24 +02:00
|
|
|
if (!ApplyPredictFilter(enc, enc->current_width_, height, quality, &bw)) {
|
2012-04-10 09:00:36 +02:00
|
|
|
err = VP8_ENC_ERROR_INVALID_CONFIGURATION;
|
|
|
|
goto Error;
|
|
|
|
}
|
2012-03-28 13:07:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (enc->use_cross_color_) {
|
2012-04-26 09:19:24 +02:00
|
|
|
if (!ApplyCrossColorFilter(enc, enc->current_width_, height, quality,
|
|
|
|
&bw)) {
|
2012-04-10 09:00:36 +02:00
|
|
|
err = VP8_ENC_ERROR_INVALID_CONFIGURATION;
|
|
|
|
goto Error;
|
|
|
|
}
|
2012-03-28 13:07:42 +02:00
|
|
|
}
|
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
VP8LWriteBits(&bw, 1, !TRANSFORM_PRESENT); // No more transforms.
|
2012-04-23 14:15:39 +02:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Estimate the color cache size.
|
|
|
|
|
2012-04-26 09:19:24 +02:00
|
|
|
if (enc->cache_bits_ > 0) {
|
|
|
|
if (!VP8LCalculateEstimateForCacheSize(enc->argb_, enc->current_width_,
|
|
|
|
height, &enc->cache_bits_)) {
|
|
|
|
err = VP8_ENC_ERROR_INVALID_CONFIGURATION;
|
|
|
|
goto Error;
|
2012-04-10 09:00:36 +02:00
|
|
|
}
|
2012-03-28 13:07:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Encode and write the transformed image.
|
|
|
|
|
2012-04-11 06:20:16 +02:00
|
|
|
ok = EncodeImageInternal(&bw, enc->argb_, enc->current_width_, height,
|
2012-04-26 09:19:24 +02:00
|
|
|
quality, enc->cache_bits_, enc->histo_bits_);
|
2012-03-28 13:07:42 +02:00
|
|
|
if (!ok) goto Error;
|
|
|
|
|
2012-05-09 08:57:34 +02:00
|
|
|
err = WriteImage(enc, &bw, &coded_size);
|
2012-03-28 13:07:42 +02:00
|
|
|
if (err != VP8_ENC_OK) {
|
|
|
|
ok = 0;
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
|
2012-04-30 14:18:50 +02:00
|
|
|
if (picture->stats != NULL) {
|
|
|
|
WebPAuxStats* const stats = picture->stats;
|
|
|
|
memset(stats, 0, sizeof(*stats));
|
|
|
|
stats->PSNR[0] = 99.;
|
|
|
|
stats->PSNR[1] = 99.;
|
|
|
|
stats->PSNR[2] = 99.;
|
|
|
|
stats->PSNR[3] = 99.;
|
2012-05-09 08:57:34 +02:00
|
|
|
stats->coded_size = coded_size;
|
2012-04-30 14:18:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (picture->extra_info != NULL) {
|
|
|
|
const int mb_w = (width + 15) >> 4;
|
|
|
|
const int mb_h = (height + 15) >> 4;
|
|
|
|
memset(picture->extra_info, 0, mb_w * mb_h * sizeof(*picture->extra_info));
|
|
|
|
}
|
|
|
|
|
2012-03-28 13:07:42 +02:00
|
|
|
Error:
|
2012-04-02 12:58:36 +02:00
|
|
|
VP8LBitWriterDestroy(&bw);
|
2012-03-28 13:07:42 +02:00
|
|
|
DeleteVP8LEncoder(enc);
|
|
|
|
if (!ok) {
|
2012-04-18 10:58:01 +02:00
|
|
|
assert(err != VP8_ENC_OK);
|
2012-03-28 13:07:42 +02:00
|
|
|
WebPEncodingSetError(picture, err);
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
2012-04-11 11:52:13 +02:00
|
|
|
|
|
|
|
#endif
|