Unify some entropy functions.

The code and logic is unified when computing bit entropy + Huffman cost.

Speed-wise, we gain 8% for lossless encoding.
Logic-wise, the beginning/end of the distributions are handled properly
and the compression ratio does not change much.

Change-Id: Ifa91d7d3e667c9a9a421faec4e845ecb6479a633
This commit is contained in:
Vincent Rabaud
2015-12-17 16:25:34 +01:00
parent ba7f4b68c9
commit ca509a3362
4 changed files with 107 additions and 142 deletions

View File

@ -221,18 +221,19 @@ static double FinalHuffmanCost(const VP8LStreaks* const stats) {
return retval;
}
// Trampolines
static double HuffmanCost(const uint32_t* const population, int length) {
const VP8LStreaks stats = VP8LHuffmanCostCount(population, length);
return FinalHuffmanCost(&stats);
}
// Get the symbol entropy for the distribution 'population'.
// Set 'trivial_sym', if there's only one symbol present in the distribution.
static double PopulationCost(const uint32_t* const population, int length,
uint32_t* const trivial_sym) {
return VP8LBitsEntropy(population, length, trivial_sym) +
HuffmanCost(population, length);
VP8LBitEntropy bit_entropy;
VP8LStreaks stats;
VP8LGetEntropyUnrefined(population, length, &bit_entropy, &stats);
if (trivial_sym != NULL) {
*trivial_sym = (bit_entropy.nonzeros == 1) ? bit_entropy.nonzero_code
: VP8L_NON_TRIVIAL_SYM;
}
return BitsEntropyRefine(&bit_entropy) + FinalHuffmanCost(&stats);
}
static WEBP_INLINE double GetCombinedEntropy(const uint32_t* const X,
@ -363,8 +364,8 @@ static void UpdateDominantCostRange(
static void UpdateHistogramCost(VP8LHistogram* const h) {
uint32_t alpha_sym, red_sym, blue_sym;
const double alpha_cost = PopulationCost(h->alpha_, NUM_LITERAL_CODES,
&alpha_sym);
const double alpha_cost =
PopulationCost(h->alpha_, NUM_LITERAL_CODES, &alpha_sym);
const double distance_cost =
PopulationCost(h->distance_, NUM_DISTANCE_CODES, NULL) +
VP8LExtraCost(h->distance_, NUM_DISTANCE_CODES);