Update BackwardRefsWithLocalCache.

Update BackwardRefsWithLocalCache to do in-place update of backward
references w.r.t local color cache index.

No impact on the compression density or compression speed.

Change-Id: Ie066251464c3928c044e037b43df3af28b48ca30
This commit is contained in:
Vikas Arora 2014-11-12 16:53:46 -08:00
parent d69e36ec59
commit 0f23566558

View File

@ -817,39 +817,33 @@ static int CalculateBestCacheSize(const uint32_t* const argb,
int* const lz77_computed, int* const lz77_computed,
int* const best_cache_bits); int* const best_cache_bits);
// Create the backward references for specified cache_bits from a source // Update (in-place) backward references for specified cache_bits.
// backward refs that is created without local color cache.
static int BackwardRefsWithLocalCache(const uint32_t* const argb, static int BackwardRefsWithLocalCache(const uint32_t* const argb,
int cache_bits, int cache_bits,
const VP8LBackwardRefs* const refs_src, VP8LBackwardRefs* const refs) {
VP8LBackwardRefs* const refs_dst) {
int pixel_index = 0; int pixel_index = 0;
VP8LColorCache hashers; VP8LColorCache hashers;
VP8LRefsCursor c = VP8LRefsCursorInit(refs_src); VP8LRefsCursor c = VP8LRefsCursorInit(refs);
if (!VP8LColorCacheInit(&hashers, cache_bits)) return 0; if (!VP8LColorCacheInit(&hashers, cache_bits)) return 0;
ClearBackwardRefs(refs_dst);
while (VP8LRefsCursorOk(&c)) { while (VP8LRefsCursorOk(&c)) {
const PixOrCopy* const v_src = c.cur_pos; PixOrCopy* const v = c.cur_pos;
if (PixOrCopyIsLiteral(v_src)) { if (PixOrCopyIsLiteral(v)) {
const uint32_t argb_literal = v_src->argb_or_distance; const uint32_t argb_literal = v->argb_or_distance;
if (VP8LColorCacheContains(&hashers, argb_literal)) { if (VP8LColorCacheContains(&hashers, argb_literal)) {
const int ix = VP8LColorCacheGetIndex(&hashers, argb_literal); const int ix = VP8LColorCacheGetIndex(&hashers, argb_literal);
BackwardRefsCursorAdd(refs_dst, PixOrCopyCreateCacheIdx(ix)); *v = PixOrCopyCreateCacheIdx(ix);
} else { } else {
VP8LColorCacheInsert(&hashers, argb_literal); VP8LColorCacheInsert(&hashers, argb_literal);
BackwardRefsCursorAdd(refs_dst, *v_src);
} }
++pixel_index; ++pixel_index;
} else { } else {
// refs_src was created without local cache, so it can not have cache // refs was created without local cache, so it can not have cache indexes.
// indexes.
int k; int k;
assert(PixOrCopyIsCopy(v_src)); assert(PixOrCopyIsCopy(v));
for (k = 0; k < v_src->len; ++k) { for (k = 0; k < v->len; ++k) {
VP8LColorCacheInsert(&hashers, argb[pixel_index++]); VP8LColorCacheInsert(&hashers, argb[pixel_index++]);
} }
BackwardRefsCursorAdd(refs_dst, *v_src);
} }
VP8LRefsCursorNext(&c); VP8LRefsCursorNext(&c);
} }
@ -877,13 +871,9 @@ VP8LBackwardRefs* VP8LGetBackwardReferences(
if (lz77_computed) { if (lz77_computed) {
// Transform refs_lz77 for the optimized cache_bits. // Transform refs_lz77 for the optimized cache_bits.
if (*cache_bits > 0) { if (*cache_bits > 0) {
if (!BackwardRefsWithLocalCache(argb, *cache_bits, refs_lz77, refs_rle)) { if (!BackwardRefsWithLocalCache(argb, *cache_bits, refs_lz77)) {
goto Error; goto Error;
} }
// Swap refs_lz77 and refs_rle.
best = refs_lz77;
refs_lz77 = refs_rle;
refs_rle = best;
} }
} else { } else {
if (!BackwardReferencesLz77(width, height, argb, *cache_bits, quality, if (!BackwardReferencesLz77(width, height, argb, *cache_bits, quality,