Skip to content

Commit

Permalink
Use the plane code and not the distance when computing statistics.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vrabaud committed Jun 1, 2017
1 parent b903b80 commit f209a54
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
14 changes: 11 additions & 3 deletions src/enc/backward_references_cost_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_),
Expand Down Expand Up @@ -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;
}

Expand Down
16 changes: 12 additions & 4 deletions src/enc/histogram_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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)];
Expand All @@ -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];
}
}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/enc/histogram_enc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
Expand Down

0 comments on commit f209a54

Please sign in to comment.