more consolidation: introduce VP8LHistogramSet

VP8LHistogramSet is container for pointers to histograms that
we can shuffle around. Allocation is one big chunk of memory.
Downside is that we don't de-allocate memory on-the-go during
HistogramRefine().

+ renamed HistogramRefine() into HistogramRemap(), so we don't
confuse with "HistogramCombine"
+ made VP8LHistogramClear() static.

Change-Id: Idf1a748a871c3b942cca5c8050072ccd82c7511d
This commit is contained in:
Pascal Massimino 2012-04-30 19:46:22 +00:00 committed by James Zern
parent 1a210ef1a9
commit d0d88990d8
3 changed files with 121 additions and 126 deletions

View File

@ -17,20 +17,7 @@
#include "./histogram.h" #include "./histogram.h"
#include "../dsp/lossless.h" #include "../dsp/lossless.h"
void VP8LHistogramCreate(VP8LHistogram* const p, static void HistogramClear(VP8LHistogram* const p) {
const VP8LBackwardRefs* const refs,
int palette_code_bits) {
int i;
if (palette_code_bits >= 0) {
p->palette_code_bits_ = palette_code_bits;
}
VP8LHistogramClear(p);
for (i = 0; i < refs->size; ++i) {
VP8LHistogramAddSinglePixOrCopy(p, &refs->refs[i]);
}
}
void VP8LHistogramClear(VP8LHistogram* const p) {
memset(p->literal_, 0, sizeof(p->literal_)); memset(p->literal_, 0, sizeof(p->literal_));
memset(p->red_, 0, sizeof(p->red_)); memset(p->red_, 0, sizeof(p->red_));
memset(p->blue_, 0, sizeof(p->blue_)); memset(p->blue_, 0, sizeof(p->blue_));
@ -39,38 +26,52 @@ void VP8LHistogramClear(VP8LHistogram* const p) {
p->bit_cost_ = 0; p->bit_cost_ = 0;
} }
void VP8LHistogramCreate(VP8LHistogram* const p,
const VP8LBackwardRefs* const refs,
int palette_code_bits) {
int i;
if (palette_code_bits >= 0) {
p->palette_code_bits_ = palette_code_bits;
}
HistogramClear(p);
for (i = 0; i < refs->size; ++i) {
VP8LHistogramAddSinglePixOrCopy(p, &refs->refs[i]);
}
}
void VP8LHistogramInit(VP8LHistogram* const p, int palette_code_bits) { void VP8LHistogramInit(VP8LHistogram* const p, int palette_code_bits) {
p->palette_code_bits_ = palette_code_bits; p->palette_code_bits_ = palette_code_bits;
VP8LHistogramClear(p); HistogramClear(p);
} }
VP8LHistogram** VP8LAllocateHistograms(int size, int cache_bits) { VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits) {
int i; int i;
VP8LHistogram** const histos = VP8LHistogramSet* set;
(VP8LHistogram**)calloc(size, sizeof(*histos)); VP8LHistogram* bulk;
if (histos == NULL) return NULL; const size_t total_size = sizeof(*set)
+ size * sizeof(*set->histograms)
+ size * sizeof(**set->histograms);
uint8_t* memory = (uint8_t*)malloc(total_size);
if (memory == NULL) return NULL;
set = (VP8LHistogramSet*)memory;
memory += sizeof(*set);
set->histograms = (VP8LHistogram**)memory;
memory += size * sizeof(*set->histograms);
bulk = (VP8LHistogram*)memory;
set->max_size = size;
set->size = size;
for (i = 0; i < size; ++i) { for (i = 0; i < size; ++i) {
histos[i] = (VP8LHistogram*)malloc(sizeof(**histos)); set->histograms[i] = bulk + i;
if (histos[i] == NULL) { VP8LHistogramInit(set->histograms[i], cache_bits);
VP8LDeleteHistograms(histos, i);
return NULL;
}
VP8LHistogramInit(histos[i], cache_bits);
} }
return histos; return set;
} }
void VP8LDeleteHistograms(VP8LHistogram** const histograms, int size) { // -----------------------------------------------------------------------------
if (histograms != NULL) {
int i;
for (i = 0; i < size; ++i) free(histograms[i]);
free(histograms);
}
}
void VP8LConvertPopulationCountTableToBitEstimates( void VP8LConvertPopulationCountTableToBitEstimates(
int num_symbols, int num_symbols, const int* const population_counts,
const int* const population_counts,
double* const output) { double* const output) {
int sum = 0; int sum = 0;
int nonzeros = 0; int nonzeros = 0;
@ -241,7 +242,7 @@ double VP8LHistogramEstimateBitsHeader(const VP8LHistogram* const p) {
static void HistogramBuildImage(int xsize, int histo_bits, static void HistogramBuildImage(int xsize, int histo_bits,
const VP8LBackwardRefs* const backward_refs, const VP8LBackwardRefs* const backward_refs,
VP8LHistogram** const image) { VP8LHistogramSet* const image) {
int i; int i;
int x = 0, y = 0; int x = 0, y = 0;
const int histo_xsize = const int histo_xsize =
@ -251,7 +252,7 @@ static void HistogramBuildImage(int xsize, int histo_bits,
const int ix = const int ix =
(histo_bits > 0) ? (y >> histo_bits) * histo_xsize + (x >> histo_bits) (histo_bits > 0) ? (y >> histo_bits) * histo_xsize + (x >> histo_bits)
: 0; : 0;
VP8LHistogramAddSinglePixOrCopy(image[ix], v); VP8LHistogramAddSinglePixOrCopy(image->histograms[ix], v);
x += PixOrCopyLength(v); x += PixOrCopyLength(v);
while (x >= xsize) { while (x >= xsize) {
x -= xsize; x -= xsize;
@ -260,27 +261,25 @@ static void HistogramBuildImage(int xsize, int histo_bits,
} }
} }
static int HistogramCombine(VP8LHistogram* const * const in, int in_size, static int HistogramCombine(const VP8LHistogramSet* const in,
int num_pairs, VP8LHistogram** const out, VP8LHistogramSet* const out, int num_pairs) {
int* const final_out_size) {
int ok = 0; int ok = 0;
int i, iter; int i, iter;
unsigned int seed = 0; unsigned int seed = 0;
int tries_with_no_success = 0; int tries_with_no_success = 0;
const int min_cluster_size = 2; const int min_cluster_size = 2;
int out_size = in_size; int out_size = in->size;
const int outer_iters = in_size * 3; const int outer_iters = in->size * 3;
VP8LHistogram* const histos = (VP8LHistogram*)malloc(2 * sizeof(*histos)); VP8LHistogram* const histos = (VP8LHistogram*)malloc(2 * sizeof(*histos));
VP8LHistogram* cur_combo = histos + 0; // trial merged histogram VP8LHistogram* cur_combo = histos + 0; // trial merged histogram
VP8LHistogram* best_combo = histos + 1; // best merged histogram so far VP8LHistogram* best_combo = histos + 1; // best merged histogram so far
if (histos == NULL) goto End; if (histos == NULL) goto End;
// Copy histograms from in[] to out[]. // Copy histograms from in[] to out[].
for (i = 0; i < in_size; ++i) { assert(in->size <= out->size);
out[i] = (VP8LHistogram*)malloc(sizeof(*(out[i]))); for (i = 0; i < in->size; ++i) {
if (out[i] == NULL) goto End; in->histograms[i]->bit_cost_ = VP8LHistogramEstimateBits(in->histograms[i]);
*(out[i]) = *(in[i]); *out->histograms[i] = *in->histograms[i];
out[i]->bit_cost_ = in[i]->bit_cost_ = VP8LHistogramEstimateBits(out[i]);
} }
// Collapse similar histograms in 'out'. // Collapse similar histograms in 'out'.
@ -299,12 +298,13 @@ static int HistogramCombine(VP8LHistogram* const * const in, int in_size,
if (idx1 == idx2) { if (idx1 == idx2) {
continue; continue;
} }
*cur_combo = *out[idx1]; *cur_combo = *out->histograms[idx1];
VP8LHistogramAdd(cur_combo, out[idx2]); VP8LHistogramAdd(cur_combo, out->histograms[idx2]);
cur_combo->bit_cost_ = VP8LHistogramEstimateBits(cur_combo); cur_combo->bit_cost_ = VP8LHistogramEstimateBits(cur_combo);
// Calculate cost reduction on combining. // Calculate cost reduction on combining.
curr_cost_diff = cur_combo->bit_cost_ curr_cost_diff = cur_combo->bit_cost_
- out[idx1]->bit_cost_ - out[idx2]->bit_cost_; - out->histograms[idx1]->bit_cost_
- out->histograms[idx2]->bit_cost_;
if (best_cost_diff > curr_cost_diff) { // found a better pair? if (best_cost_diff > curr_cost_diff) { // found a better pair?
{ // swap cur/best combo histograms { // swap cur/best combo histograms
VP8LHistogram* const tmp = cur_combo; VP8LHistogram* const tmp = cur_combo;
@ -318,13 +318,12 @@ static int HistogramCombine(VP8LHistogram* const * const in, int in_size,
} }
if (best_cost_diff < 0.0) { if (best_cost_diff < 0.0) {
*out[best_idx1] = *best_combo; *out->histograms[best_idx1] = *best_combo;
// swap best_idx2 slot with last one (which is now unused) // swap best_idx2 slot with last one (which is now unused)
free(out[best_idx2]);
--out_size; --out_size;
if (best_idx2 != out_size) { if (best_idx2 != out_size) {
out[best_idx2] = out[out_size]; out->histograms[best_idx2] = out->histograms[out_size];
out[out_size] = NULL; // just for sanity check. out->histograms[out_size] = NULL; // just for sanity check.
} }
tries_with_no_success = 0; tries_with_no_success = 0;
} }
@ -332,7 +331,7 @@ static int HistogramCombine(VP8LHistogram* const * const in, int in_size,
break; break;
} }
} }
*final_out_size = out_size; out->size = out_size;
ok = 1; ok = 1;
End: End:
@ -359,18 +358,19 @@ static double HistogramDistance(const VP8LHistogram* const square_histogram,
return new_bit_cost - previous_bit_cost; return new_bit_cost - previous_bit_cost;
} }
// Find the best 'out' histogram for each of the raw histograms. // Find the best 'out' histogram for each of the 'in' histograms.
// Note: we assume that out[]->bit_cost_ is already up-to-date. // Note: we assume that out[]->bit_cost_ is already up-to-date.
static void HistogramRefine(VP8LHistogram* const * const raw, int raw_size, static void HistogramRemap(const VP8LHistogramSet* const in,
uint16_t* const symbols, const VP8LHistogramSet* const out,
VP8LHistogram** const out, int out_size) { uint16_t* const symbols) {
int i; int i;
for (i = 0; i < raw_size; ++i) { for (i = 0; i < in->size; ++i) {
int best_out = 0; int best_out = 0;
double best_bits = HistogramDistance(raw[i], out[0]); double best_bits = HistogramDistance(in->histograms[i], out->histograms[0]);
int k; int k;
for (k = 1; k < out_size; ++k) { for (k = 1; k < out->size; ++k) {
const double cur_bits = HistogramDistance(raw[i], out[k]); const double cur_bits =
HistogramDistance(in->histograms[i], out->histograms[k]);
if (cur_bits < best_bits) { if (cur_bits < best_bits) {
best_bits = cur_bits; best_bits = cur_bits;
best_out = k; best_out = k;
@ -380,47 +380,40 @@ static void HistogramRefine(VP8LHistogram* const * const raw, int raw_size,
} }
// Recompute each out based on raw and symbols. // Recompute each out based on raw and symbols.
for (i = 0; i < out_size; ++i) { for (i = 0; i < out->size; ++i) {
VP8LHistogramClear(out[i]); HistogramClear(out->histograms[i]);
} }
for (i = 0; i < raw_size; ++i) { for (i = 0; i < in->size; ++i) {
VP8LHistogramAdd(out[symbols[i]], raw[i]); VP8LHistogramAdd(out->histograms[symbols[i]], in->histograms[i]);
} }
} }
int VP8LGetHistoImageSymbols(int xsize, int ysize, int VP8LGetHistoImageSymbols(int xsize, int ysize,
const VP8LBackwardRefs* const refs, const VP8LBackwardRefs* const refs,
int quality, int histo_bits, int cache_bits, int quality, int histo_bits, int cache_bits,
VP8LHistogram** const histogram_image, VP8LHistogramSet* const image_in,
int* const histo_image_raw_size_ptr,
uint16_t* const histogram_symbols) { uint16_t* const histogram_symbols) {
int ok = 0; int ok = 0;
const int histo_xsize = histo_bits ? VP8LSubSampleSize(xsize, histo_bits) : 1; const int histo_xsize = histo_bits ? VP8LSubSampleSize(xsize, histo_bits) : 1;
const int histo_ysize = histo_bits ? VP8LSubSampleSize(ysize, histo_bits) : 1; const int histo_ysize = histo_bits ? VP8LSubSampleSize(ysize, histo_bits) : 1;
const int num_histo_pairs = 10 + quality / 2; // For HistogramCombine(). const int num_histo_pairs = 10 + quality / 2; // For HistogramCombine().
const int histo_image_raw_size = histo_xsize * histo_ysize; const int histo_image_raw_size = histo_xsize * histo_ysize;
VP8LHistogram** const histo_image_raw = VP8LHistogramSet* const image_out =
VP8LAllocateHistograms(histo_image_raw_size, cache_bits); VP8LAllocateHistogramSet(histo_image_raw_size, cache_bits);
if (histo_image_raw == NULL) return 0; if (image_out == NULL) return 0;
*histo_image_raw_size_ptr = histo_image_raw_size; // initial guess.
// Build histogram image. // Build histogram image.
HistogramBuildImage(xsize, histo_bits, refs, histo_image_raw); HistogramBuildImage(xsize, histo_bits, refs, image_out);
// Collapse similar histograms. // Collapse similar histograms.
if (!HistogramCombine(histo_image_raw, histo_image_raw_size, num_histo_pairs, if (!HistogramCombine(image_out, image_in, num_histo_pairs)) {
histogram_image, histo_image_raw_size_ptr)) {
goto Error; goto Error;
} }
// Refine histogram image. // Find the optimal map from original histograms to the final ones.
HistogramRefine(histo_image_raw, histo_image_raw_size, histogram_symbols, HistogramRemap(image_out, image_in, histogram_symbols);
histogram_image, *histo_image_raw_size_ptr);
ok = 1; ok = 1;
Error: Error:
if (!ok) { free(image_out);
VP8LDeleteHistograms(histogram_image, *histo_image_raw_size_ptr);
}
VP8LDeleteHistograms(histo_image_raw, histo_image_raw_size);
return ok; return ok;
} }

View File

@ -41,6 +41,14 @@ typedef struct {
double bit_cost_; // cached value of VP8LHistogramEstimateBits(this) double bit_cost_; // cached value of VP8LHistogramEstimateBits(this)
} VP8LHistogram; } VP8LHistogram;
// Collection of histograms with fixed capacity, allocated as one
// big memory chunk. Can be destroyed by simply calling 'free()'.
typedef struct {
int size; // number of slots currently in use
int max_size; // maximum capacity
VP8LHistogram** histograms;
} VP8LHistogramSet;
// Create the histogram. // Create the histogram.
// //
// The input data is the PixOrCopy data, which models the literals, stop // The input data is the PixOrCopy data, which models the literals, stop
@ -50,18 +58,12 @@ void VP8LHistogramCreate(VP8LHistogram* const p,
const VP8LBackwardRefs* const refs, const VP8LBackwardRefs* const refs,
int palette_code_bits); int palette_code_bits);
// Reset the histogram's stats.
void VP8LHistogramClear(VP8LHistogram* const p);
// Set the palette_code_bits and reset the stats. // Set the palette_code_bits and reset the stats.
void VP8LHistogramInit(VP8LHistogram* const p, int palette_code_bits); void VP8LHistogramInit(VP8LHistogram* const p, int palette_code_bits);
// Allocate an array of pointer to histograms, allocated and initialized // Allocate an array of pointer to histograms, allocated and initialized
// using 'cache_bits'. Return NULL in case of memory error. // using 'cache_bits'. Return NULL in case of memory error.
VP8LHistogram** VP8LAllocateHistograms(int size, int cache_bits); VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits);
// Destroy an array of histograms (and the array itself).
void VP8LDeleteHistograms(VP8LHistogram** const histograms, int size);
void VP8LHistogramAddSinglePixOrCopy(VP8LHistogram* const p, void VP8LHistogramAddSinglePixOrCopy(VP8LHistogram* const p,
const PixOrCopy* const v); const PixOrCopy* const v);
@ -125,10 +127,8 @@ void VP8LConvertPopulationCountTableToBitEstimates(
// Builds the histogram image. // Builds the histogram image.
int VP8LGetHistoImageSymbols(int xsize, int ysize, int VP8LGetHistoImageSymbols(int xsize, int ysize,
const VP8LBackwardRefs* const refs, const VP8LBackwardRefs* const refs,
int quality, int histogram_bits, int quality, int histogram_bits, int cache_bits,
int cache_bits, VP8LHistogramSet* const image_in,
VP8LHistogram** const histogram_image,
int* const histogram_image_size,
uint16_t* const histogram_symbols); uint16_t* const histogram_symbols);
#if defined(__cplusplus) || defined(c_plusplus) #if defined(__cplusplus) || defined(c_plusplus)

View File

@ -103,7 +103,8 @@ static int AnalyzeAndCreatePalette(const uint32_t* const argb, int num_pix,
} }
static int AnalyzeEntropy(const uint32_t const *argb, int xsize, int ysize, static int AnalyzeEntropy(const uint32_t const *argb, int xsize, int ysize,
int* nonpredicted_bits, int* predicted_bits) { double* const nonpredicted_bits,
double* const predicted_bits) {
int i; int i;
VP8LHistogram* nonpredicted = NULL; VP8LHistogram* nonpredicted = NULL;
VP8LHistogram* predicted = (VP8LHistogram*)malloc(2 * sizeof(*predicted)); VP8LHistogram* predicted = (VP8LHistogram*)malloc(2 * sizeof(*predicted));
@ -126,8 +127,8 @@ static int AnalyzeEntropy(const uint32_t const *argb, int xsize, int ysize,
VP8LHistogramAddSinglePixOrCopy(predicted, &pix_diff_token); VP8LHistogramAddSinglePixOrCopy(predicted, &pix_diff_token);
} }
} }
*nonpredicted_bits = (int)VP8LHistogramEstimateBitsBulk(nonpredicted); *nonpredicted_bits = VP8LHistogramEstimateBitsBulk(nonpredicted);
*predicted_bits = (int)VP8LHistogramEstimateBitsBulk(predicted); *predicted_bits = VP8LHistogramEstimateBitsBulk(predicted);
free(predicted); free(predicted);
return 1; return 1;
} }
@ -140,13 +141,13 @@ static int VP8LEncAnalyze(VP8LEncoder* const enc) {
AnalyzeAndCreatePalette(pic->argb, pic->width * pic->height, AnalyzeAndCreatePalette(pic->argb, pic->width * pic->height,
enc->palette_, &enc->palette_size_); enc->palette_, &enc->palette_size_);
if (!enc->use_palette_) { if (!enc->use_palette_) {
int non_pred_entropy, pred_entropy; double non_pred_entropy, pred_entropy;
if (!AnalyzeEntropy(pic->argb, pic->width, pic->height, if (!AnalyzeEntropy(pic->argb, pic->width, pic->height,
&non_pred_entropy, &pred_entropy)) { &non_pred_entropy, &pred_entropy)) {
return 0; return 0;
} }
if (20 * pred_entropy < 19 * non_pred_entropy) { if (pred_entropy < 0.95 * non_pred_entropy) {
enc->use_predict_ = 1; enc->use_predict_ = 1;
enc->use_cross_color_ = 1; enc->use_cross_color_ = 1;
} }
@ -157,7 +158,7 @@ static int VP8LEncAnalyze(VP8LEncoder* const enc) {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
static int GetBackwardReferences(int width, int height, static int GetBackwardReferences(int width, int height,
const uint32_t* argb, const uint32_t* const argb,
int quality, int use_color_cache, int quality, int use_color_cache,
int cache_bits, int use_2d_locality, int cache_bits, int use_2d_locality,
VP8LBackwardRefs* const best) { VP8LBackwardRefs* const best) {
@ -345,13 +346,15 @@ static int OptimizeHuffmanForRle(int length, int* counts) {
// TODO(vikasa): Wrap bit_codes and bit_lengths in a Struct. // TODO(vikasa): Wrap bit_codes and bit_lengths in a Struct.
static int GetHuffBitLengthsAndCodes( static int GetHuffBitLengthsAndCodes(
int histogram_image_size, VP8LHistogram** histogram_image, const VP8LHistogramSet* const histogram_image,
int use_color_cache, int** bit_length_sizes, int use_color_cache, int** bit_length_sizes,
uint16_t*** bit_codes, uint8_t*** bit_lengths) { uint16_t*** bit_codes, uint8_t*** bit_lengths) {
int i, k; int i, k;
int ok = 1; int ok = 1;
const int histogram_image_size = histogram_image->size;
for (i = 0; i < histogram_image_size; ++i) { for (i = 0; i < histogram_image_size; ++i) {
const int num_literals = VP8LHistogramNumCodes(histogram_image[i]); VP8LHistogram* const histo = histogram_image->histograms[i];
const int num_literals = VP8LHistogramNumCodes(histo);
k = 0; k = 0;
// TODO(vikasa): Alloc one big buffer instead of allocating in the loop. // TODO(vikasa): Alloc one big buffer instead of allocating in the loop.
(*bit_length_sizes)[5 * i] = num_literals; (*bit_length_sizes)[5 * i] = num_literals;
@ -364,23 +367,21 @@ static int GetHuffBitLengthsAndCodes(
} }
// For each component, optimize histogram for Huffman with RLE compression. // For each component, optimize histogram for Huffman with RLE compression.
ok = ok && OptimizeHuffmanForRle(num_literals, ok = ok && OptimizeHuffmanForRle(num_literals, histo->literal_);
histogram_image[i]->literal_);
if (!use_color_cache) { if (!use_color_cache) {
// Implies that palette_bits == 0, // Implies that palette_bits == 0,
// and so number of palette entries = (1 << 0) = 1. // and so number of palette entries = (1 << 0) = 1.
// Optimization might have smeared population count in this single // Optimization might have smeared population count in this single
// palette entry, so zero it out. // palette entry, so zero it out.
histogram_image[i]->literal_[256 + kLengthCodes] = 0; histo->literal_[256 + kLengthCodes] = 0;
} }
ok = ok && OptimizeHuffmanForRle(256, histogram_image[i]->red_); ok = ok && OptimizeHuffmanForRle(256, histo->red_);
ok = ok && OptimizeHuffmanForRle(256, histogram_image[i]->blue_); ok = ok && OptimizeHuffmanForRle(256, histo->blue_);
ok = ok && OptimizeHuffmanForRle(256, histogram_image[i]->alpha_); ok = ok && OptimizeHuffmanForRle(256, histo->alpha_);
ok = ok && OptimizeHuffmanForRle(DISTANCE_CODES_MAX, ok = ok && OptimizeHuffmanForRle(DISTANCE_CODES_MAX, histo->distance_);
histogram_image[i]->distance_);
// Create a Huffman tree (in the form of bit lengths) for each component. // Create a Huffman tree (in the form of bit lengths) for each component.
ok = ok && VP8LCreateHuffmanTree(histogram_image[i]->literal_, num_literals, ok = ok && VP8LCreateHuffmanTree(histo->literal_, num_literals,
15, (*bit_lengths)[5 * i]); 15, (*bit_lengths)[5 * i]);
for (k = 1; k < 5; ++k) { for (k = 1; k < 5; ++k) {
int val = 256; int val = 256;
@ -396,14 +397,14 @@ static int GetHuffBitLengthsAndCodes(
goto Error; goto Error;
} }
} }
ok = ok && VP8LCreateHuffmanTree(histogram_image[i]->red_, 256, 15, ok = ok &&
VP8LCreateHuffmanTree(histo->red_, 256, 15,
(*bit_lengths)[5 * i + 1]) && (*bit_lengths)[5 * i + 1]) &&
VP8LCreateHuffmanTree(histogram_image[i]->blue_, 256, 15, VP8LCreateHuffmanTree(histo->blue_, 256, 15,
(*bit_lengths)[5 * i + 2]) && (*bit_lengths)[5 * i + 2]) &&
VP8LCreateHuffmanTree(histogram_image[i]->alpha_, 256, 15, VP8LCreateHuffmanTree(histo->alpha_, 256, 15,
(*bit_lengths)[5 * i + 3]) && (*bit_lengths)[5 * i + 3]) &&
VP8LCreateHuffmanTree(histogram_image[i]->distance_, VP8LCreateHuffmanTree(histo->distance_, DISTANCE_CODES_MAX, 15,
DISTANCE_CODES_MAX, 15,
(*bit_lengths)[5 * i + 4]); (*bit_lengths)[5 * i + 4]);
// Create the actual bit codes for the bit lengths. // Create the actual bit codes for the bit lengths.
for (k = 0; k < 5; ++k) { for (k = 0; k < 5; ++k) {
@ -657,10 +658,11 @@ static int EncodeImageInternal(VP8LBitWriter* const bw,
const int use_2d_locality = 1; const int use_2d_locality = 1;
const int use_color_cache = (cache_bits > 0); const int use_color_cache = (cache_bits > 0);
const int color_cache_size = use_color_cache ? (1 << cache_bits) : 0; const int color_cache_size = use_color_cache ? (1 << cache_bits) : 0;
const int histogram_image_xysize = VP8LSubSampleSize(width, histogram_bits) * const int histogram_image_xysize =
VP8LSubSampleSize(width, histogram_bits) *
VP8LSubSampleSize(height, histogram_bits); VP8LSubSampleSize(height, histogram_bits);
VP8LHistogram** histogram_image = VP8LHistogramSet* histogram_image =
(VP8LHistogram**)calloc(histogram_image_xysize, sizeof(*histogram_image)); VP8LAllocateHistogramSet(histogram_image_xysize, 0);
int histogram_image_size; int histogram_image_size;
VP8LBackwardRefs refs; VP8LBackwardRefs refs;
uint16_t* const histogram_symbols = uint16_t* const histogram_symbols =
@ -677,11 +679,12 @@ static int EncodeImageInternal(VP8LBitWriter* const bw,
// Build histogram image & symbols from backward references. // Build histogram image & symbols from backward references.
if (!VP8LGetHistoImageSymbols(width, height, &refs, if (!VP8LGetHistoImageSymbols(width, height, &refs,
quality, histogram_bits, cache_bits, quality, histogram_bits, cache_bits,
histogram_image, &histogram_image_size, histogram_image,
histogram_symbols)) { histogram_symbols)) {
goto Error; goto Error;
} }
// Create Huffman bit lengths & codes for each histogram image. // Create Huffman bit lengths & codes for each histogram image.
histogram_image_size = histogram_image->size;
bit_lengths_sizes = (int*)calloc(5 * histogram_image_size, bit_lengths_sizes = (int*)calloc(5 * histogram_image_size,
sizeof(*bit_lengths_sizes)); sizeof(*bit_lengths_sizes));
bit_lengths = (uint8_t**)calloc(5 * histogram_image_size, bit_lengths = (uint8_t**)calloc(5 * histogram_image_size,
@ -689,8 +692,8 @@ static int EncodeImageInternal(VP8LBitWriter* const bw,
bit_codes = (uint16_t**)calloc(5 * histogram_image_size, bit_codes = (uint16_t**)calloc(5 * histogram_image_size,
sizeof(*bit_codes)); sizeof(*bit_codes));
if (bit_lengths_sizes == NULL || bit_lengths == NULL || bit_codes == NULL || if (bit_lengths_sizes == NULL || bit_lengths == NULL || bit_codes == NULL ||
!GetHuffBitLengthsAndCodes(histogram_image_size, histogram_image, !GetHuffBitLengthsAndCodes(histogram_image, use_color_cache,
use_color_cache, &bit_lengths_sizes, &bit_lengths_sizes,
&bit_codes, &bit_lengths)) { &bit_codes, &bit_lengths)) {
goto Error; goto Error;
} }
@ -742,7 +745,7 @@ static int EncodeImageInternal(VP8LBitWriter* const bw,
} }
// Free combined histograms. // Free combined histograms.
VP8LDeleteHistograms(histogram_image, histogram_image_size); free(histogram_image);
histogram_image = NULL; histogram_image = NULL;
// Emit no bits if there is only one symbol in the histogram. // Emit no bits if there is only one symbol in the histogram.
@ -757,9 +760,8 @@ static int EncodeImageInternal(VP8LBitWriter* const bw,
ok = 1; ok = 1;
Error: Error:
if (!ok) { if (!ok) free(histogram_image);
VP8LDeleteHistograms(histogram_image, histogram_image_size);
}
VP8LClearBackwardRefs(&refs); VP8LClearBackwardRefs(&refs);
for (i = 0; i < 5 * histogram_image_size; ++i) { for (i = 0; i < 5 * histogram_image_size; ++i) {
free(bit_lengths[i]); free(bit_lengths[i]);