From f209a5481ef106ffb4a1cab4adc5eeb63a4a1e88 Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Thu, 1 Jun 2017 17:08:43 +0200 Subject: [PATCH] Use the plane code and not the distance when computing statistics. As backward references use the plane code when checking the cost of a distance, statistics used to compute the cost should use it too. This provides a small compression improvement at no speed cost. Change-Id: Icade150929ee39ef6dc0d8b1fc85973086ecf41d --- src/enc/backward_references_cost_enc.c | 14 +++++++++++--- src/enc/histogram_enc.c | 16 ++++++++++++---- src/enc/histogram_enc.h | 4 +++- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/enc/backward_references_cost_enc.c b/src/enc/backward_references_cost_enc.c index 226bb437..cbad3b41 100644 --- a/src/enc/backward_references_cost_enc.c +++ b/src/enc/backward_references_cost_enc.c @@ -58,13 +58,21 @@ static void ConvertPopulationCountTableToBitEstimates( } } -static int CostModelBuild(CostModel* const m, int cache_bits, +static int CostModelBuild(CostModel* const m, int xsize, int cache_bits, const VP8LBackwardRefs* const refs) { int ok = 0; + VP8LRefsCursor c = VP8LRefsCursorInit(refs); VP8LHistogram* const histo = VP8LAllocateHistogram(cache_bits); if (histo == NULL) goto Error; - VP8LHistogramCreate(histo, refs, cache_bits); + // The following code is similar to VP8LHistogramCreate but converts the + // distance to plane code. + VP8LHistogramInit(histo, cache_bits); + while (VP8LRefsCursorOk(&c)) { + VP8LHistogramAddSinglePixOrCopy(histo, c.cur_pos, VP8LDistanceToPlaneCode, + xsize); + VP8LRefsCursorNext(&c); + } ConvertPopulationCountTableToBitEstimates( VP8LHistogramNumCodes(histo->palette_code_bits_), @@ -583,7 +591,7 @@ static int BackwardReferencesHashChainDistanceOnly( if (!cc_init) goto Error; } - if (!CostModelBuild(cost_model, cache_bits, refs)) { + if (!CostModelBuild(cost_model, xsize, cache_bits, refs)) { goto Error; } diff --git a/src/enc/histogram_enc.c b/src/enc/histogram_enc.c index beb0c53a..c2806332 100644 --- a/src/enc/histogram_enc.c +++ b/src/enc/histogram_enc.c @@ -76,7 +76,7 @@ void VP8LHistogramStoreRefs(const VP8LBackwardRefs* const refs, VP8LHistogram* const histo) { VP8LRefsCursor c = VP8LRefsCursorInit(refs); while (VP8LRefsCursorOk(&c)) { - VP8LHistogramAddSinglePixOrCopy(histo, c.cur_pos); + VP8LHistogramAddSinglePixOrCopy(histo, c.cur_pos, NULL, 0); VP8LRefsCursorNext(&c); } } @@ -138,7 +138,9 @@ VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits) { // ----------------------------------------------------------------------------- void VP8LHistogramAddSinglePixOrCopy(VP8LHistogram* const histo, - const PixOrCopy* const v) { + const PixOrCopy* const v, + int (*const distance_modifier)(int, int), + int distance_modifier_arg0) { if (PixOrCopyIsLiteral(v)) { ++histo->alpha_[PixOrCopyLiteral(v, 3)]; ++histo->red_[PixOrCopyLiteral(v, 2)]; @@ -152,7 +154,13 @@ void VP8LHistogramAddSinglePixOrCopy(VP8LHistogram* const histo, int code, extra_bits; VP8LPrefixEncodeBits(PixOrCopyLength(v), &code, &extra_bits); ++histo->literal_[NUM_LITERAL_CODES + code]; - VP8LPrefixEncodeBits(PixOrCopyDistance(v), &code, &extra_bits); + if (distance_modifier == NULL) { + VP8LPrefixEncodeBits(PixOrCopyDistance(v), &code, &extra_bits); + } else { + VP8LPrefixEncodeBits( + distance_modifier(distance_modifier_arg0, PixOrCopyDistance(v)), + &code, &extra_bits); + } ++histo->distance_[code]; } } @@ -473,7 +481,7 @@ static void HistogramBuild( while (VP8LRefsCursorOk(&c)) { const PixOrCopy* const v = c.cur_pos; const int ix = (y >> histo_bits) * histo_xsize + (x >> histo_bits); - VP8LHistogramAddSinglePixOrCopy(histograms[ix], v); + VP8LHistogramAddSinglePixOrCopy(histograms[ix], v, NULL, 0); x += PixOrCopyLength(v); while (x >= xsize) { x -= xsize; diff --git a/src/enc/histogram_enc.h b/src/enc/histogram_enc.h index ce563d37..00c73f51 100644 --- a/src/enc/histogram_enc.h +++ b/src/enc/histogram_enc.h @@ -90,7 +90,9 @@ VP8LHistogram* VP8LAllocateHistogram(int cache_bits); // Accumulate a token 'v' into a histogram. void VP8LHistogramAddSinglePixOrCopy(VP8LHistogram* const histo, - const PixOrCopy* const v); + const PixOrCopy* const v, + int (*const distance_modifier)(int, int), + int distance_modifier_arg0); static WEBP_INLINE int VP8LHistogramNumCodes(int palette_code_bits) { return NUM_LITERAL_CODES + NUM_LENGTH_CODES +