A quick pass of cleanup in backward reference code

const correctness, renaming, cosmetics etc.

Change-Id: I432befbb22f0eafd9a613f5f632398b6ef03c0f6
This commit is contained in:
Urvang Joshi 2012-04-25 07:33:57 +00:00 committed by James Zern
parent 83332b3c16
commit e491729905
4 changed files with 207 additions and 247 deletions

View File

@ -12,7 +12,6 @@
#include <assert.h> #include <assert.h>
#include <math.h> #include <math.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include "./backward_references.h" #include "./backward_references.h"
@ -35,8 +34,8 @@ static const uint8_t plane_to_code_lut[128] = {
static const int kMinLength = 2; static const int kMinLength = 2;
int VP8LDistanceToPlaneCode(int xsize, int dist) { int VP8LDistanceToPlaneCode(int xsize, int dist) {
int yoffset = dist / xsize; const int yoffset = dist / xsize;
int xoffset = dist - yoffset * xsize; const int xoffset = dist - yoffset * xsize;
if (xoffset <= 8 && yoffset < 8) { if (xoffset <= 8 && yoffset < 8) {
return plane_to_code_lut[yoffset * 16 + 8 - xoffset] + 1; return plane_to_code_lut[yoffset * 16 + 8 - xoffset] + 1;
} else if (xoffset > xsize - 8 && yoffset < 7) { } else if (xoffset > xsize - 8 && yoffset < 7) {
@ -45,14 +44,14 @@ int VP8LDistanceToPlaneCode(int xsize, int dist) {
return dist + 120; return dist + 120;
} }
static WEBP_INLINE int FindMatchLength(const uint32_t* array1, static WEBP_INLINE int FindMatchLength(const uint32_t* const array1,
const uint32_t* array2, const uint32_t* const array2,
const int max_limit) { const int max_limit) {
int matched = 0; int match_len = 0;
while (matched < max_limit && array1[matched] == array2[matched]) { while (match_len < max_limit && array1[match_len] == array2[match_len]) {
++matched; ++match_len;
} }
return matched; return match_len;
} }
#define HASH_BITS 18 #define HASH_BITS 18
@ -69,7 +68,7 @@ static WEBP_INLINE uint64_t GetHash64(uint64_t num) {
return num; return num;
} }
static WEBP_INLINE uint64_t GetPixPair(const uint32_t* argb) { static WEBP_INLINE uint64_t GetPixPair(const uint32_t* const argb) {
return ((uint64_t)(argb[1]) << 32) | argb[0]; return ((uint64_t)(argb[1]) << 32) | argb[0];
} }
@ -81,7 +80,7 @@ typedef struct {
int32_t* chain_; int32_t* chain_;
} VP8LHashChain; } VP8LHashChain;
static int VP8LHashChain_Init(VP8LHashChain* p, int size) { static int VP8LHashChainInit(VP8LHashChain* const p, int size) {
int i; int i;
p->chain_ = (int*)malloc(size * sizeof(*p->chain_)); p->chain_ = (int*)malloc(size * sizeof(*p->chain_));
if (!p->chain_) { if (!p->chain_) {
@ -96,27 +95,25 @@ static int VP8LHashChain_Init(VP8LHashChain* p, int size) {
return 1; return 1;
} }
static void VP8LHashChain_Delete(VP8LHashChain* p) { static void VP8LHashChainClear(VP8LHashChain* const p) {
if (p != NULL) { if (p != NULL) {
free(p->chain_); free(p->chain_);
} }
} }
static void VP8LHashChain_Insert(VP8LHashChain* p, static void VP8LHashChainInsert(VP8LHashChain* const p,
const uint32_t* argb, int32_t ix) { const uint32_t* const argb, int32_t pos) {
// Insertion of two pixels at a time. // Insertion of two pixels at a time.
const uint64_t key = GetPixPair(argb); const uint64_t key = GetPixPair(argb);
const uint64_t hash_code = GetHash64(key); const uint64_t hash_code = GetHash64(key);
p->chain_[ix] = p->hash_to_first_index_[hash_code]; p->chain_[pos] = p->hash_to_first_index_[hash_code];
p->hash_to_first_index_[hash_code] = ix; p->hash_to_first_index_[hash_code] = pos;
} }
static int VP8LHashChain_FindCopy(VP8LHashChain* p, static int VP8LHashChainFindCopy(
int quality, const VP8LHashChain* const p, int quality, int index, int xsize,
int index, int xsize, const uint32_t* const argb, int maxlen, int* const distance_ptr,
const uint32_t* argb, int* const length_ptr) {
int maxlen, int* offset_out,
int* len_out) {
const uint64_t next_two_pixels = GetPixPair(&argb[index]); const uint64_t next_two_pixels = GetPixPair(&argb[index]);
const uint64_t hash_code = GetHash64(next_two_pixels); const uint64_t hash_code = GetHash64(next_two_pixels);
int prev_length = 0; int prev_length = 0;
@ -124,33 +121,32 @@ static int VP8LHashChain_FindCopy(VP8LHashChain* p,
int give_up = 10 + (quality >> 1); int give_up = 10 + (quality >> 1);
const int min_pos = (index > kWindowSize) ? index - kWindowSize : 0; const int min_pos = (index > kWindowSize) ? index - kWindowSize : 0;
int32_t pos; int32_t pos;
int64_t length;
int64_t val; int64_t val;
int x; int best_length = 0;
int y; int best_distance = 0;
int len = 0;
int offset = 0;
for (pos = p->hash_to_first_index_[hash_code]; for (pos = p->hash_to_first_index_[hash_code];
pos >= min_pos; pos >= min_pos;
pos = p->chain_[pos]) { pos = p->chain_[pos]) {
int curr_length;
if (give_up < 0) { if (give_up < 0) {
if (give_up < -quality * 2 || best_val >= 0xff0000) { if (give_up < -quality * 2 || best_val >= 0xff0000) {
break; break;
} }
} }
--give_up; --give_up;
if (len != 0 && argb[pos + len - 1] != argb[index + len - 1]) { if (best_length != 0 &&
argb[pos + best_length - 1] != argb[index + best_length - 1]) {
continue; continue;
} }
length = FindMatchLength(argb + pos, argb + index, maxlen); curr_length = FindMatchLength(argb + pos, argb + index, maxlen);
if (length < prev_length) { if (curr_length < prev_length) {
continue; continue;
} }
val = 65536 * length; val = 65536 * curr_length;
// Favoring 2d locality here gives savings for certain images. // Favoring 2d locality here gives savings for certain images.
if (index - pos < 9 * xsize) { if (index - pos < 9 * xsize) {
y = (index - pos) / xsize; const int y = (index - pos) / xsize;
x = (index - pos) % xsize; int x = (index - pos) % xsize;
if (x > xsize / 2) { if (x > xsize / 2) {
x = xsize - x; x = xsize - x;
} }
@ -163,26 +159,26 @@ static int VP8LHashChain_FindCopy(VP8LHashChain* p,
val -= 9 * 9 + 9 * 9; val -= 9 * 9 + 9 * 9;
} }
if (best_val < val) { if (best_val < val) {
prev_length = length; prev_length = curr_length;
best_val = val; best_val = val;
len = length; best_length = curr_length;
offset = index - pos; best_distance = index - pos;
if (length >= kMaxLength) { if (curr_length >= kMaxLength) {
break; break;
} }
if ((offset == 1 || offset == xsize) && len >= 128) { if ((best_distance == 1 || best_distance == xsize) &&
best_length >= 128) {
break; break;
} }
} }
} }
*offset_out = offset; *distance_ptr = best_distance;
*len_out = len; *length_ptr = best_length;
return len >= kMinLength; return best_length >= kMinLength;
} }
static WEBP_INLINE void PushBackCopy(int length, static WEBP_INLINE void PushBackCopy(int length, PixOrCopy* const stream,
PixOrCopy* stream, int* const stream_size) {
int* stream_size) {
while (length >= kMaxLength) { while (length >= kMaxLength) {
stream[*stream_size] = PixOrCopyCreateCopy(1, kMaxLength); stream[*stream_size] = PixOrCopyCreateCopy(1, kMaxLength);
++(*stream_size); ++(*stream_size);
@ -194,38 +190,39 @@ static WEBP_INLINE void PushBackCopy(int length,
} }
} }
void VP8LBackwardReferencesRle(int xsize, int ysize, const uint32_t* argb, void VP8LBackwardReferencesRle(
PixOrCopy* stream, int* stream_size) { int xsize, int ysize, const uint32_t* const argb, PixOrCopy* const stream,
int* const stream_size) {
const int pix_count = xsize * ysize; const int pix_count = xsize * ysize;
int streak = 0; int match_len = 0;
int i; int i;
*stream_size = 0; *stream_size = 0;
for (i = 0; i < pix_count; ++i) { for (i = 0; i < pix_count; ++i) {
if (i >= 1 && argb[i] == argb[i - 1]) { if (i >= 1 && argb[i] == argb[i - 1]) {
++streak; ++match_len;
} else { } else {
PushBackCopy(streak, stream, stream_size); PushBackCopy(match_len, stream, stream_size);
streak = 0; match_len = 0;
stream[*stream_size] = PixOrCopyCreateLiteral(argb[i]); stream[*stream_size] = PixOrCopyCreateLiteral(argb[i]);
++(*stream_size); ++(*stream_size);
} }
} }
PushBackCopy(streak, stream, stream_size); PushBackCopy(match_len, stream, stream_size);
} }
// Returns 1 when successful. // Returns 1 when successful.
int VP8LBackwardReferencesHashChain(int xsize, int ysize, int use_palette, int VP8LBackwardReferencesHashChain(
const uint32_t* argb, int palette_bits, int xsize, int ysize, int use_color_cache, const uint32_t* const argb,
int quality, PixOrCopy* stream, int cache_bits, int quality, PixOrCopy* const stream,
int* stream_size) { int* const stream_size) {
int i; int i;
int ok = 0; int ok = 0;
const int pix_count = xsize * ysize; const int pix_count = xsize * ysize;
VP8LHashChain* hash_chain = (VP8LHashChain*)malloc(sizeof(*hash_chain)); VP8LHashChain* hash_chain = (VP8LHashChain*)malloc(sizeof(*hash_chain));
VP8LColorCache hashers; VP8LColorCache hashers;
if (!hash_chain || if (!hash_chain ||
!VP8LColorCacheInit(&hashers, palette_bits) || !VP8LColorCacheInit(&hashers, cache_bits) ||
!VP8LHashChain_Init(hash_chain, pix_count)) { !VP8LHashChainInit(hash_chain, pix_count)) {
goto Error; goto Error;
} }
*stream_size = 0; *stream_size = 0;
@ -238,8 +235,8 @@ int VP8LBackwardReferencesHashChain(int xsize, int ysize, int use_palette,
if (maxlen > kMaxLength) { if (maxlen > kMaxLength) {
maxlen = kMaxLength; maxlen = kMaxLength;
} }
VP8LHashChain_FindCopy(hash_chain, quality, VP8LHashChainFindCopy(hash_chain, quality, i, xsize, argb, maxlen,
i, xsize, argb, maxlen, &offset, &len); &offset, &len);
} }
if (len >= kMinLength) { if (len >= kMinLength) {
// Alternative#2: Insert the pixel at 'i' as literal, and code the // Alternative#2: Insert the pixel at 'i' as literal, and code the
@ -247,19 +244,19 @@ int VP8LBackwardReferencesHashChain(int xsize, int ysize, int use_palette,
int offset2 = 0; int offset2 = 0;
int len2 = 0; int len2 = 0;
int k; int k;
VP8LHashChain_Insert(hash_chain, &argb[i], i); VP8LHashChainInsert(hash_chain, &argb[i], i);
if (i < pix_count - 2) { // FindCopy(i+1,..) reads [i + 1] and [i + 2]. if (i < pix_count - 2) { // FindCopy(i+1,..) reads [i + 1] and [i + 2].
int maxlen = pix_count - (i + 1); int maxlen = pix_count - (i + 1);
if (maxlen > kMaxLength) { if (maxlen > kMaxLength) {
maxlen = kMaxLength; maxlen = kMaxLength;
} }
VP8LHashChain_FindCopy(hash_chain, quality, VP8LHashChainFindCopy(hash_chain, quality,
i + 1, xsize, argb, maxlen, &offset2, &len2); i + 1, xsize, argb, maxlen, &offset2, &len2);
if (len2 > len + 1) { if (len2 > len + 1) {
// Alternative#2 is a better match. So push pixel at 'i' as literal. // Alternative#2 is a better match. So push pixel at 'i' as literal.
if (use_palette && VP8LColorCacheContains(&hashers, argb[i])) { if (use_color_cache && VP8LColorCacheContains(&hashers, argb[i])) {
const int ix = VP8LColorCacheGetIndex(&hashers, argb[i]); const int ix = VP8LColorCacheGetIndex(&hashers, argb[i]);
stream[*stream_size] = PixOrCopyCreatePaletteIx(ix); stream[*stream_size] = PixOrCopyCreateCacheIdx(ix);
} else { } else {
stream[*stream_size] = PixOrCopyCreateLiteral(argb[i]); stream[*stream_size] = PixOrCopyCreateLiteral(argb[i]);
} }
@ -279,29 +276,29 @@ int VP8LBackwardReferencesHashChain(int xsize, int ysize, int use_palette,
VP8LColorCacheInsert(&hashers, argb[i + k]); VP8LColorCacheInsert(&hashers, argb[i + k]);
if (k != 0 && i + k + 1 < pix_count) { if (k != 0 && i + k + 1 < pix_count) {
// Add to the hash_chain (but cannot add the last pixel). // Add to the hash_chain (but cannot add the last pixel).
VP8LHashChain_Insert(hash_chain, &argb[i + k], i + k); VP8LHashChainInsert(hash_chain, &argb[i + k], i + k);
} }
} }
i += len; i += len;
} else { } else {
if (use_palette && VP8LColorCacheContains(&hashers, argb[i])) { if (use_color_cache && VP8LColorCacheContains(&hashers, argb[i])) {
// push pixel as a palette pixel // push pixel as a PixOrCopyCreateCacheIdx pixel
int ix = VP8LColorCacheGetIndex(&hashers, argb[i]); int ix = VP8LColorCacheGetIndex(&hashers, argb[i]);
stream[*stream_size] = PixOrCopyCreatePaletteIx(ix); stream[*stream_size] = PixOrCopyCreateCacheIdx(ix);
} else { } else {
stream[*stream_size] = PixOrCopyCreateLiteral(argb[i]); stream[*stream_size] = PixOrCopyCreateLiteral(argb[i]);
} }
++(*stream_size); ++(*stream_size);
VP8LColorCacheInsert(&hashers, argb[i]); VP8LColorCacheInsert(&hashers, argb[i]);
if (i + 1 < pix_count) { if (i + 1 < pix_count) {
VP8LHashChain_Insert(hash_chain, &argb[i], i); VP8LHashChainInsert(hash_chain, &argb[i], i);
} }
++i; ++i;
} }
} }
ok = 1; ok = 1;
Error: Error:
VP8LHashChain_Delete(hash_chain); VP8LHashChainClear(hash_chain);
free(hash_chain); free(hash_chain);
VP8LColorCacheClear(&hashers); VP8LColorCacheClear(&hashers);
return ok; return ok;
@ -313,12 +310,12 @@ typedef struct {
double literal_[PIX_OR_COPY_CODES_MAX]; double literal_[PIX_OR_COPY_CODES_MAX];
double blue_[VALUES_IN_BYTE]; double blue_[VALUES_IN_BYTE];
double distance_[DISTANCE_CODES_MAX]; double distance_[DISTANCE_CODES_MAX];
int palette_bits_; int cache_bits_;
} CostModel; } CostModel;
static int CostModel_Build(CostModel* p, int xsize, int ysize, static int CostModelBuild(CostModel* const p, int xsize, int ysize,
int recursion_level, int use_palette, int recursion_level, int use_color_cache,
const uint32_t* argb, int palette_bits) { const uint32_t* const argb, int cache_bits) {
int ok = 0; int ok = 0;
int stream_size; int stream_size;
VP8LHistogram histo; VP8LHistogram histo;
@ -327,22 +324,22 @@ static int CostModel_Build(CostModel* p, int xsize, int ysize,
if (stream == NULL) { if (stream == NULL) {
goto Error; goto Error;
} }
p->palette_bits_ = palette_bits; p->cache_bits_ = cache_bits;
if (recursion_level > 0) { if (recursion_level > 0) {
if (!VP8LBackwardReferencesTraceBackwards(xsize, ysize, recursion_level - 1, if (!VP8LBackwardReferencesTraceBackwards(xsize, ysize, recursion_level - 1,
use_palette, argb, palette_bits, use_color_cache, argb, cache_bits,
&stream[0], &stream_size)) { &stream[0], &stream_size)) {
goto Error; goto Error;
} }
} else { } else {
const int quality = 100; const int quality = 100;
if (!VP8LBackwardReferencesHashChain(xsize, ysize, use_palette, argb, if (!VP8LBackwardReferencesHashChain(xsize, ysize, use_color_cache, argb,
palette_bits, quality, cache_bits, quality,
&stream[0], &stream_size)) { &stream[0], &stream_size)) {
goto Error; goto Error;
} }
} }
VP8LHistogramInit(&histo, palette_bits); VP8LHistogramInit(&histo, cache_bits);
for (i = 0; i < stream_size; ++i) { for (i = 0; i < stream_size; ++i) {
VP8LHistogramAddSinglePixOrCopy(&histo, stream[i]); VP8LHistogramAddSinglePixOrCopy(&histo, stream[i]);
} }
@ -363,28 +360,26 @@ Error:
return ok; return ok;
} }
static WEBP_INLINE double CostModel_LiteralCost(const CostModel* p, static WEBP_INLINE double GetLiteralCost(const CostModel* const p, uint32_t v) {
uint32_t v) {
return p->alpha_[v >> 24] + return p->alpha_[v >> 24] +
p->red_[(v >> 16) & 0xff] + p->red_[(v >> 16) & 0xff] +
p->literal_[(v >> 8) & 0xff] + p->literal_[(v >> 8) & 0xff] +
p->blue_[v & 0xff]; p->blue_[v & 0xff];
} }
static WEBP_INLINE double CostModel_PaletteCost(const CostModel* p, static WEBP_INLINE double GetCacheCost(const CostModel* const p, uint32_t idx) {
uint32_t ix) { const int literal_idx = VALUES_IN_BYTE + kLengthCodes + idx;
int literal_ix = VALUES_IN_BYTE + kLengthCodes + ix; return p->literal_[literal_idx];
return p->literal_[literal_ix];
} }
static WEBP_INLINE double CostModel_LengthCost(const CostModel* p, static WEBP_INLINE double GetLengthCost(const CostModel* const p,
uint32_t len) { uint32_t length) {
int code, extra_bits_count, extra_bits_value; int code, extra_bits_count, extra_bits_value;
PrefixEncode(len, &code, &extra_bits_count, &extra_bits_value); PrefixEncode(length, &code, &extra_bits_count, &extra_bits_value);
return p->literal_[VALUES_IN_BYTE + code] + extra_bits_count; return p->literal_[VALUES_IN_BYTE + code] + extra_bits_count;
} }
static WEBP_INLINE double CostModel_DistanceCost(const CostModel* p, static WEBP_INLINE double GetDistanceCost(const CostModel* const p,
uint32_t distance) { uint32_t distance) {
int code, extra_bits_count, extra_bits_value; int code, extra_bits_count, extra_bits_value;
PrefixEncode(distance, &code, &extra_bits_count, &extra_bits_value); PrefixEncode(distance, &code, &extra_bits_count, &extra_bits_value);
@ -392,12 +387,8 @@ static WEBP_INLINE double CostModel_DistanceCost(const CostModel* p,
} }
static int BackwardReferencesHashChainDistanceOnly( static int BackwardReferencesHashChainDistanceOnly(
int xsize, int ysize, int xsize, int ysize, int recursive_cost_model, int use_color_cache,
int recursive_cost_model, const uint32_t* const argb, int cache_bits, uint32_t* const dist_array) {
int use_palette,
const uint32_t* argb,
int palette_bits,
uint32_t* dist_array) {
const int quality = 100; const int quality = 100;
const int pix_count = xsize * ysize; const int pix_count = xsize * ysize;
double* cost = (double*)malloc(pix_count * sizeof(*cost)); double* cost = (double*)malloc(pix_count * sizeof(*cost));
@ -410,12 +401,12 @@ static int BackwardReferencesHashChainDistanceOnly(
if (cost == NULL || if (cost == NULL ||
cost_model == NULL || cost_model == NULL ||
hash_chain == NULL || hash_chain == NULL ||
!VP8LColorCacheInit(&hashers, palette_bits)) { !VP8LColorCacheInit(&hashers, cache_bits)) {
goto Error; goto Error;
} }
VP8LHashChain_Init(hash_chain, pix_count); VP8LHashChainInit(hash_chain, pix_count);
CostModel_Build(cost_model, xsize, ysize, recursive_cost_model, CostModelBuild(cost_model, xsize, ysize, recursive_cost_model,
use_palette, argb, palette_bits); use_color_cache, argb, cache_bits);
for (i = 0; i < pix_count; ++i) { for (i = 0; i < pix_count; ++i) {
cost[i] = 1e100; cost[i] = 1e100;
} }
@ -436,17 +427,17 @@ static int BackwardReferencesHashChainDistanceOnly(
if (maxlen > pix_count - i) { if (maxlen > pix_count - i) {
maxlen = pix_count - i; maxlen = pix_count - i;
} }
VP8LHashChain_FindCopy(hash_chain, quality, i, xsize, argb, maxlen, VP8LHashChainFindCopy(hash_chain, quality, i, xsize, argb, maxlen,
&offset, &len); &offset, &len);
} }
if (len >= kMinLength) { if (len >= kMinLength) {
const int code = VP8LDistanceToPlaneCode(xsize, offset); const int code = VP8LDistanceToPlaneCode(xsize, offset);
const double distance_cost = const double distance_cost =
prev_cost + CostModel_DistanceCost(cost_model, code); prev_cost + GetDistanceCost(cost_model, code);
int k; int k;
for (k = 1; k < len; ++k) { for (k = 1; k < len; ++k) {
const double cost_val = const double cost_val =
distance_cost + CostModel_LengthCost(cost_model, k); distance_cost + GetLengthCost(cost_model, k);
if (cost[i + k] > cost_val) { if (cost[i + k] > cost_val) {
cost[i + k] = cost_val; cost[i + k] = cost_val;
dist_array[i + k] = k + 1; dist_array[i + k] = k + 1;
@ -462,7 +453,7 @@ static int BackwardReferencesHashChainDistanceOnly(
VP8LColorCacheInsert(&hashers, argb[i + k]); VP8LColorCacheInsert(&hashers, argb[i + k]);
if (i + k + 1 < pix_count) { if (i + k + 1 < pix_count) {
// Add to the hash_chain (but cannot add the last pixel). // Add to the hash_chain (but cannot add the last pixel).
VP8LHashChain_Insert(hash_chain, &argb[i + k], i + k); VP8LHashChainInsert(hash_chain, &argb[i + k], i + k);
} }
} }
// 2) jump. // 2) jump.
@ -472,7 +463,7 @@ static int BackwardReferencesHashChainDistanceOnly(
} }
} }
if (i < pix_count - 1) { if (i < pix_count - 1) {
VP8LHashChain_Insert(hash_chain, &argb[i], i); VP8LHashChainInsert(hash_chain, &argb[i], i);
} }
{ {
// inserting a literal pixel // inserting a literal pixel
@ -483,11 +474,11 @@ static int BackwardReferencesHashChainDistanceOnly(
mul0 = 0.68; mul0 = 0.68;
mul1 = 0.82; mul1 = 0.82;
} }
if (use_palette && VP8LColorCacheContains(&hashers, argb[i])) { if (use_color_cache && VP8LColorCacheContains(&hashers, argb[i])) {
int ix = VP8LColorCacheGetIndex(&hashers, argb[i]); int ix = VP8LColorCacheGetIndex(&hashers, argb[i]);
cost_val += CostModel_PaletteCost(cost_model, ix) * mul0; cost_val += GetCacheCost(cost_model, ix) * mul0;
} else { } else {
cost_val += CostModel_LiteralCost(cost_model, argb[i]) * mul1; cost_val += GetLiteralCost(cost_model, argb[i]) * mul1;
} }
if (cost[i] > cost_val) { if (cost[i] > cost_val) {
cost[i] = cost_val; cost[i] = cost_val;
@ -501,7 +492,7 @@ static int BackwardReferencesHashChainDistanceOnly(
// through cheaper means already. // through cheaper means already.
ok = 1; ok = 1;
Error: Error:
if (hash_chain) VP8LHashChain_Delete(hash_chain); if (hash_chain) VP8LHashChainClear(hash_chain);
free(hash_chain); free(hash_chain);
free(cost_model); free(cost_model);
free(cost); free(cost);
@ -509,8 +500,9 @@ Error:
return ok; return ok;
} }
static void TraceBackwards(const uint32_t* dist_array, int dist_array_size, static void TraceBackwards(
uint32_t** chosen_path, int* chosen_path_size) { const uint32_t* const dist_array, int dist_array_size,
uint32_t** const chosen_path, int* const chosen_path_size) {
int i; int i;
// Count how many. // Count how many.
int count = 0; int count = 0;
@ -533,15 +525,9 @@ static void TraceBackwards(const uint32_t* dist_array, int dist_array_size,
} }
static int BackwardReferencesHashChainFollowChosenPath( static int BackwardReferencesHashChainFollowChosenPath(
int xsize, int xsize, int ysize, int use_color_cache, const uint32_t* const argb,
int ysize, int cache_bits, const uint32_t* const chosen_path, int chosen_path_size,
int use_palette, PixOrCopy* const stream, int* const stream_size) {
const uint32_t* argb,
int palette_bits,
uint32_t* chosen_path,
int chosen_path_size,
PixOrCopy* stream,
int* stream_size) {
const int quality = 100; const int quality = 100;
const int pix_count = xsize * ysize; const int pix_count = xsize * ysize;
int i = 0; int i = 0;
@ -550,9 +536,9 @@ static int BackwardReferencesHashChainFollowChosenPath(
int ok = 0; int ok = 0;
VP8LColorCache hashers; VP8LColorCache hashers;
VP8LHashChain* hash_chain = (VP8LHashChain*)malloc(sizeof(*hash_chain)); VP8LHashChain* hash_chain = (VP8LHashChain*)malloc(sizeof(*hash_chain));
VP8LHashChain_Init(hash_chain, pix_count); VP8LHashChainInit(hash_chain, pix_count);
if (hash_chain == NULL || if (hash_chain == NULL ||
!VP8LColorCacheInit(&hashers, palette_bits)) { !VP8LColorCacheInit(&hashers, cache_bits)) {
goto Error; goto Error;
} }
*stream_size = 0; *stream_size = 0;
@ -561,7 +547,7 @@ static int BackwardReferencesHashChainFollowChosenPath(
int len = 0; int len = 0;
int maxlen = chosen_path[ix]; int maxlen = chosen_path[ix];
if (maxlen != 1) { if (maxlen != 1) {
VP8LHashChain_FindCopy(hash_chain, quality, VP8LHashChainFindCopy(hash_chain, quality,
i, xsize, argb, maxlen, &offset, &len); i, xsize, argb, maxlen, &offset, &len);
assert(len == maxlen); assert(len == maxlen);
stream[*stream_size] = PixOrCopyCreateCopy(offset, len); stream[*stream_size] = PixOrCopyCreateCopy(offset, len);
@ -570,29 +556,29 @@ static int BackwardReferencesHashChainFollowChosenPath(
VP8LColorCacheInsert(&hashers, argb[i + k]); VP8LColorCacheInsert(&hashers, argb[i + k]);
if (i + k + 1 < pix_count) { if (i + k + 1 < pix_count) {
// Add to the hash_chain (but cannot add the last pixel). // Add to the hash_chain (but cannot add the last pixel).
VP8LHashChain_Insert(hash_chain, &argb[i + k], i + k); VP8LHashChainInsert(hash_chain, &argb[i + k], i + k);
} }
} }
i += len; i += len;
} else { } else {
if (use_palette && VP8LColorCacheContains(&hashers, argb[i])) { if (use_color_cache && VP8LColorCacheContains(&hashers, argb[i])) {
// push pixel as a palette pixel // push pixel as a color cache index
int ix = VP8LColorCacheGetIndex(&hashers, argb[i]); int ix = VP8LColorCacheGetIndex(&hashers, argb[i]);
stream[*stream_size] = PixOrCopyCreatePaletteIx(ix); stream[*stream_size] = PixOrCopyCreateCacheIdx(ix);
} else { } else {
stream[*stream_size] = PixOrCopyCreateLiteral(argb[i]); stream[*stream_size] = PixOrCopyCreateLiteral(argb[i]);
} }
++(*stream_size); ++(*stream_size);
VP8LColorCacheInsert(&hashers, argb[i]); VP8LColorCacheInsert(&hashers, argb[i]);
if (i + 1 < pix_count) { if (i + 1 < pix_count) {
VP8LHashChain_Insert(hash_chain, &argb[i], i); VP8LHashChainInsert(hash_chain, &argb[i], i);
} }
++i; ++i;
} }
} }
ok = 1; ok = 1;
Error: Error:
VP8LHashChain_Delete(hash_chain); VP8LHashChainClear(hash_chain);
if (hash_chain) { if (hash_chain) {
free(hash_chain); free(hash_chain);
} }
@ -601,25 +587,22 @@ Error:
} }
// Returns 1 on success. // Returns 1 on success.
int VP8LBackwardReferencesTraceBackwards(int xsize, int ysize, int VP8LBackwardReferencesTraceBackwards(
int recursive_cost_model, int xsize, int ysize, int recursive_cost_model, int use_color_cache,
int use_palette, const uint32_t* const argb, int cache_bits, PixOrCopy* const stream,
const uint32_t* argb, int* const stream_size) {
int palette_bits,
PixOrCopy* stream,
int* stream_size) {
int ok = 0; int ok = 0;
const int dist_array_size = xsize * ysize; const int dist_array_size = xsize * ysize;
uint32_t* chosen_path = NULL; uint32_t* chosen_path = NULL;
int chosen_path_size = 0; int chosen_path_size = 0;
uint32_t* const dist_array = (uint32_t*) uint32_t* const dist_array =
malloc(dist_array_size * sizeof(*dist_array)); (uint32_t*)malloc(dist_array_size * sizeof(*dist_array));
if (dist_array == NULL) { if (dist_array == NULL) {
goto Error; goto Error;
} }
*stream_size = 0; *stream_size = 0;
if (!BackwardReferencesHashChainDistanceOnly( if (!BackwardReferencesHashChainDistanceOnly(
xsize, ysize, recursive_cost_model, use_palette, argb, palette_bits, xsize, ysize, recursive_cost_model, use_color_cache, argb, cache_bits,
dist_array)) { dist_array)) {
free(dist_array); free(dist_array);
goto Error; goto Error;
@ -627,7 +610,7 @@ int VP8LBackwardReferencesTraceBackwards(int xsize, int ysize,
TraceBackwards(dist_array, dist_array_size, &chosen_path, &chosen_path_size); TraceBackwards(dist_array, dist_array_size, &chosen_path, &chosen_path_size);
free(dist_array); free(dist_array);
if (!BackwardReferencesHashChainFollowChosenPath( if (!BackwardReferencesHashChainFollowChosenPath(
xsize, ysize, use_palette, argb, palette_bits, xsize, ysize, use_color_cache, argb, cache_bits,
chosen_path, chosen_path_size, chosen_path, chosen_path_size,
stream, stream_size)) { stream, stream_size)) {
goto Error; goto Error;
@ -639,25 +622,24 @@ Error:
} }
void VP8LBackwardReferences2DLocality(int xsize, int data_size, void VP8LBackwardReferences2DLocality(int xsize, int data_size,
PixOrCopy* data) { PixOrCopy* const data) {
int i; int i;
for (i = 0; i < data_size; ++i) { for (i = 0; i < data_size; ++i) {
if (PixOrCopyIsCopy(&data[i])) { if (PixOrCopyIsCopy(&data[i])) {
int dist = data[i].argb_or_offset; int dist = data[i].argb_or_distance;
int transformed_dist = VP8LDistanceToPlaneCode(xsize, dist); int transformed_dist = VP8LDistanceToPlaneCode(xsize, dist);
data[i].argb_or_offset = transformed_dist; data[i].argb_or_distance = transformed_dist;
} }
} }
} }
int VP8LVerifyBackwardReferences(const uint32_t* argb, int xsize, int ysize, int VP8LVerifyBackwardReferences(
int palette_bits, const uint32_t* const argb, int xsize, int ysize, int cache_bits,
const PixOrCopy* lit, const PixOrCopy* const lit, int lit_size) {
int lit_size) {
int num_pixels = 0; int num_pixels = 0;
int i; int i;
VP8LColorCache hashers; VP8LColorCache hashers;
VP8LColorCacheInit(&hashers, palette_bits); VP8LColorCacheInit(&hashers, cache_bits);
for (i = 0; i < lit_size; ++i) { for (i = 0; i < lit_size; ++i) {
if (PixOrCopyIsLiteral(&lit[i])) { if (PixOrCopyIsLiteral(&lit[i])) {
if (argb[num_pixels] != PixOrCopyArgb(&lit[i])) { if (argb[num_pixels] != PixOrCopyArgb(&lit[i])) {
@ -668,14 +650,14 @@ int VP8LVerifyBackwardReferences(const uint32_t* argb, int xsize, int ysize,
} }
VP8LColorCacheInsert(&hashers, argb[num_pixels]); VP8LColorCacheInsert(&hashers, argb[num_pixels]);
++num_pixels; ++num_pixels;
} else if (PixOrCopyIsPaletteIx(&lit[i])) { } else if (PixOrCopyIsCacheIdx(&lit[i])) {
uint32_t palette_entry = uint32_t cache_entry =
VP8LColorCacheLookup(&hashers, PixOrCopyPaletteIx(&lit[i])); VP8LColorCacheLookup(&hashers, PixOrCopyCacheIdx(&lit[i]));
if (argb[num_pixels] != palette_entry) { if (argb[num_pixels] != cache_entry) {
printf("i %d, pixel %d, original: 0x%08x, palette_ix: %d, " printf("i %d, pixel %d, original: 0x%08x, cache_idx: %d, "
"palette_entry: 0x%08x\n", "cache_entry: 0x%08x\n",
i, num_pixels, argb[num_pixels], PixOrCopyPaletteIx(&lit[i]), i, num_pixels, argb[num_pixels], PixOrCopyCacheIdx(&lit[i]),
palette_entry); cache_entry);
VP8LColorCacheClear(&hashers); VP8LColorCacheClear(&hashers);
return 0; return 0;
} }
@ -716,24 +698,25 @@ int VP8LVerifyBackwardReferences(const uint32_t* argb, int xsize, int ysize,
} }
// Returns 1 on success. // Returns 1 on success.
static int ComputePaletteHistogram(const uint32_t* argb, int xsize, int ysize, static int ComputeCacheHistogram(
PixOrCopy* stream, int stream_size, const uint32_t* const argb, int xsize, int ysize,
int palette_bits, VP8LHistogram* histo) { const PixOrCopy* const stream, int stream_size, int cache_bits,
VP8LHistogram* const histo) {
int pixel_index = 0; int pixel_index = 0;
int i; int i;
uint32_t k; uint32_t k;
VP8LColorCache hashers; VP8LColorCache hashers;
if (!VP8LColorCacheInit(&hashers, palette_bits)) { if (!VP8LColorCacheInit(&hashers, cache_bits)) {
return 0; return 0;
} }
for (i = 0; i < stream_size; ++i) { for (i = 0; i < stream_size; ++i) {
const PixOrCopy v = stream[i]; const PixOrCopy v = stream[i];
if (PixOrCopyIsLiteral(&v)) { if (PixOrCopyIsLiteral(&v)) {
if (palette_bits != 0 && if (cache_bits != 0 &&
VP8LColorCacheContains(&hashers, argb[pixel_index])) { VP8LColorCacheContains(&hashers, argb[pixel_index])) {
// push pixel as a palette pixel // push pixel as a cache index
const int ix = VP8LColorCacheGetIndex(&hashers, argb[pixel_index]); const int ix = VP8LColorCacheGetIndex(&hashers, argb[pixel_index]);
VP8LHistogramAddSinglePixOrCopy(histo, PixOrCopyCreatePaletteIx(ix)); VP8LHistogramAddSinglePixOrCopy(histo, PixOrCopyCreateCacheIdx(ix));
} else { } else {
VP8LHistogramAddSinglePixOrCopy(histo, v); VP8LHistogramAddSinglePixOrCopy(histo, v);
} }
@ -752,32 +735,32 @@ static int ComputePaletteHistogram(const uint32_t* argb, int xsize, int ysize,
return 1; return 1;
} }
// Returns how many bits are to be used for a palette. // Returns how many bits are to be used for a color cache.
int VP8LCalculateEstimateForPaletteSize(const uint32_t* argb, int VP8LCalculateEstimateForCacheSize(
int xsize, int ysize, const uint32_t* const argb, int xsize, int ysize,
int* best_palette_bits) { int* const best_cache_bits) {
int ok = 0; int ok = 0;
int palette_bits; int cache_bits;
double lowest_entropy = 1e99; double lowest_entropy = 1e99;
PixOrCopy* stream = (PixOrCopy*)malloc(xsize * ysize * sizeof(*stream)); PixOrCopy* stream = (PixOrCopy*)malloc(xsize * ysize * sizeof(*stream));
int stream_size; int stream_size;
static const double kSmallPenaltyForLargePalette = 4.0; static const double kSmallPenaltyForLargeCache = 4.0;
static const int quality = 30; static const int quality = 30;
if (stream == NULL || if (stream == NULL ||
!VP8LBackwardReferencesHashChain(xsize, ysize, 0, argb, 0, quality, !VP8LBackwardReferencesHashChain(xsize, ysize, 0, argb, 0, quality,
stream, &stream_size)) { stream, &stream_size)) {
goto Error; goto Error;
} }
for (palette_bits = 0; palette_bits < 12; ++palette_bits) { for (cache_bits = 0; cache_bits < 12; ++cache_bits) {
double cur_entropy; double cur_entropy;
VP8LHistogram histo; VP8LHistogram histo;
VP8LHistogramInit(&histo, palette_bits); VP8LHistogramInit(&histo, cache_bits);
ComputePaletteHistogram(argb, xsize, ysize, &stream[0], stream_size, ComputeCacheHistogram(argb, xsize, ysize, &stream[0], stream_size,
palette_bits, &histo); cache_bits, &histo);
cur_entropy = VP8LHistogramEstimateBits(&histo) + cur_entropy = VP8LHistogramEstimateBits(&histo) +
kSmallPenaltyForLargePalette * palette_bits; kSmallPenaltyForLargeCache * cache_bits;
if (palette_bits == 0 || cur_entropy < lowest_entropy) { if (cache_bits == 0 || cur_entropy < lowest_entropy) {
*best_palette_bits = palette_bits; *best_cache_bits = cache_bits;
lowest_entropy = cur_entropy; lowest_entropy = cur_entropy;
} }
} }

View File

@ -15,7 +15,6 @@
#include <assert.h> #include <assert.h>
#include <stdint.h> #include <stdint.h>
#include "../webp/types.h" #include "../webp/types.h"
#if defined(__cplusplus) || defined(c_plusplus) #if defined(__cplusplus) || defined(c_plusplus)
@ -28,7 +27,7 @@ extern "C" {
// Compression constants // Compression constants
#define CODE_LENGTH_CODES 19 #define CODE_LENGTH_CODES 19
static const int kLengthCodes = 24; static const int kLengthCodes = 24;
static const int kPaletteCodeBitsMax = 11; static const int kColorCacheBitsMax = 11;
#define PIX_OR_COPY_CODES_MAX (256 + 24 + (1 << 11)) #define PIX_OR_COPY_CODES_MAX (256 + 24 + (1 << 11))
static const int kMaxLength = 4096; static const int kMaxLength = 4096;
@ -89,7 +88,7 @@ static WEBP_INLINE void PrefixEncode(
enum Mode { enum Mode {
kLiteral, kLiteral,
kPaletteIx, kCacheIdx,
kCopy, kCopy,
kNone, kNone,
}; };
@ -98,24 +97,24 @@ typedef struct {
// mode as uint8_t to make the memory layout to be exactly 8 bytes. // mode as uint8_t to make the memory layout to be exactly 8 bytes.
uint8_t mode; uint8_t mode;
uint16_t len; uint16_t len;
uint32_t argb_or_offset; uint32_t argb_or_distance;
} PixOrCopy; } PixOrCopy;
static WEBP_INLINE PixOrCopy PixOrCopyCreateCopy(uint32_t offset, static WEBP_INLINE PixOrCopy PixOrCopyCreateCopy(uint32_t distance,
uint16_t len) { uint16_t len) {
PixOrCopy retval; PixOrCopy retval;
retval.mode = kCopy; retval.mode = kCopy;
retval.argb_or_offset = offset; retval.argb_or_distance = distance;
retval.len = len; retval.len = len;
return retval; return retval;
} }
static WEBP_INLINE PixOrCopy PixOrCopyCreatePaletteIx(int ix) { static WEBP_INLINE PixOrCopy PixOrCopyCreateCacheIdx(int idx) {
PixOrCopy retval; PixOrCopy retval;
assert(ix >= 0); assert(idx >= 0);
assert(ix < (1 << kPaletteCodeBitsMax)); assert(idx < (1 << kColorCacheBitsMax));
retval.mode = kPaletteIx; retval.mode = kCacheIdx;
retval.argb_or_offset = ix; retval.argb_or_distance = idx;
retval.len = 1; retval.len = 1;
return retval; return retval;
} }
@ -123,111 +122,89 @@ static WEBP_INLINE PixOrCopy PixOrCopyCreatePaletteIx(int ix) {
static WEBP_INLINE PixOrCopy PixOrCopyCreateLiteral(uint32_t argb) { static WEBP_INLINE PixOrCopy PixOrCopyCreateLiteral(uint32_t argb) {
PixOrCopy retval; PixOrCopy retval;
retval.mode = kLiteral; retval.mode = kLiteral;
retval.argb_or_offset = argb; retval.argb_or_distance = argb;
retval.len = 1; retval.len = 1;
return retval; return retval;
} }
static WEBP_INLINE int PixOrCopyIsLiteral(const PixOrCopy *p) { static WEBP_INLINE int PixOrCopyIsLiteral(const PixOrCopy* const p) {
return p->mode == kLiteral; return p->mode == kLiteral;
} }
static WEBP_INLINE int PixOrCopyIsPaletteIx(const PixOrCopy *p) { static WEBP_INLINE int PixOrCopyIsCacheIdx(const PixOrCopy* const p) {
return p->mode == kPaletteIx; return p->mode == kCacheIdx;
} }
static WEBP_INLINE int PixOrCopyIsCopy(const PixOrCopy *p) { static WEBP_INLINE int PixOrCopyIsCopy(const PixOrCopy* const p) {
return p->mode == kCopy; return p->mode == kCopy;
} }
static WEBP_INLINE uint32_t PixOrCopyLiteral(const PixOrCopy *p, static WEBP_INLINE uint32_t PixOrCopyLiteral(const PixOrCopy* const p,
int component) { int component) {
assert(p->mode == kLiteral); assert(p->mode == kLiteral);
return (p->argb_or_offset >> (component * 8)) & 0xff; return (p->argb_or_distance >> (component * 8)) & 0xff;
} }
static WEBP_INLINE uint32_t PixOrCopyLength(const PixOrCopy *p) { static WEBP_INLINE uint32_t PixOrCopyLength(const PixOrCopy* const p) {
return p->len; return p->len;
} }
static WEBP_INLINE uint32_t PixOrCopyArgb(const PixOrCopy *p) { static WEBP_INLINE uint32_t PixOrCopyArgb(const PixOrCopy* const p) {
assert(p->mode == kLiteral); assert(p->mode == kLiteral);
return p->argb_or_offset; return p->argb_or_distance;
} }
static WEBP_INLINE uint32_t PixOrCopyPaletteIx(const PixOrCopy *p) { static WEBP_INLINE uint32_t PixOrCopyCacheIdx(const PixOrCopy* const p) {
assert(p->mode == kPaletteIx); assert(p->mode == kCacheIdx);
assert(p->argb_or_offset < (1 << kPaletteCodeBitsMax)); assert(p->argb_or_distance < (1 << kColorCacheBitsMax));
return p->argb_or_offset; return p->argb_or_distance;
} }
static WEBP_INLINE uint32_t PixOrCopyDistance(const PixOrCopy *p) { static WEBP_INLINE uint32_t PixOrCopyDistance(const PixOrCopy* const p) {
assert(p->mode == kCopy); assert(p->mode == kCopy);
return p->argb_or_offset; return p->argb_or_distance;
} }
static WEBP_INLINE void PixOrCopyLengthCodeAndBits(
const PixOrCopy *p, int *code, int *n_bits, int *bits) {
assert(p->len >= 1 && p->len <= kMaxLength);
PrefixEncode(p->len, code, n_bits, bits);
}
// Ridiculously simple backward references for images where it is unlikely // Ridiculously simple backward references for images where it is unlikely
// that there are large backward references (photos). // that there are large backward references (photos).
void VP8LBackwardReferencesRle( void VP8LBackwardReferencesRle(
int xsize, int xsize, int ysize, const uint32_t* const argb, PixOrCopy* const stream,
int ysize, int* const stream_size);
const uint32_t *argb,
PixOrCopy *stream,
int *stream_size);
// This is a simple fast function for obtaining backward references // This is a simple fast function for obtaining backward references
// based on simple heuristics. Returns 1 on success. // based on simple heuristics. Returns 1 on success.
int VP8LBackwardReferencesHashChain( int VP8LBackwardReferencesHashChain(
int xsize, int xsize, int ysize, int use_color_cache, const uint32_t* const argb,
int ysize, int cache_bits, int quality, PixOrCopy* const stream,
int use_palette, int* const stream_size);
const uint32_t *argb,
int palette_bits,
int quality,
PixOrCopy *stream,
int *stream_size);
// This method looks for a shortest path through the backward reference // This method looks for a shortest path through the backward reference
// network based on a cost model generated by a first round of compression. // network based on a cost model generated by a first round of compression.
// Returns 1 on success. // Returns 1 on success.
int VP8LBackwardReferencesTraceBackwards( int VP8LBackwardReferencesTraceBackwards(
int xsize, int xsize, int ysize, int recursive_cost_model, int use_color_cache,
int ysize, const uint32_t* const argb, int cache_bits, PixOrCopy* const stream,
int recursive_cost_model, int* const stream_size);
int use_palette,
const uint32_t *argb,
int palette_bits,
PixOrCopy *stream,
int *stream_size);
// Convert backward references that are of linear distance along // Convert backward references that are of linear distance along
// the image scan lines to have a 2d locality indexing where // the image scan lines to have a 2d locality indexing where
// smaller values are used for backward references that are close by. // smaller values are used for backward references that are close by.
void VP8LBackwardReferences2DLocality(int xsize, int data_size, void VP8LBackwardReferences2DLocality(int xsize, int data_size,
PixOrCopy *data); PixOrCopy* const data);
// Internals of locality transform exposed for testing use. // Internals of locality transform exposed for testing use.
int VP8LDistanceToPlaneCode(int xsize, int distance); int VP8LDistanceToPlaneCode(int xsize, int distance);
// Returns true if the given backward references actually produce // Returns true if the given backward references actually produce
// the image given in tuple (argb, xsize, ysize). // the image given in tuple (argb, xsize, ysize).
int VP8LVerifyBackwardReferences(const uint32_t* argb, int VP8LVerifyBackwardReferences(
int xsize, int ysize, const uint32_t* const argb, int xsize, int ysize, int cache_bits,
int palette_bits, const PixOrCopy* const lit, int lit_size);
const PixOrCopy *lit,
int lit_size);
// Produce an estimate for a good emerging palette size for the image. // Produce an estimate for a good color cache size for the image.
int VP8LCalculateEstimateForPaletteSize(const uint32_t *argb, int VP8LCalculateEstimateForCacheSize(
int xsize, int ysize, const uint32_t* const argb, int xsize, int ysize,
int *best_palette_bits); int* const best_cache_bits);
#if defined(__cplusplus) || defined(c_plusplus) #if defined(__cplusplus) || defined(c_plusplus)
} }

View File

@ -53,8 +53,8 @@ void VP8LHistogramAddSinglePixOrCopy(VP8LHistogram* const p,
++p->red_[PixOrCopyLiteral(&v, 2)]; ++p->red_[PixOrCopyLiteral(&v, 2)];
++p->literal_[PixOrCopyLiteral(&v, 1)]; ++p->literal_[PixOrCopyLiteral(&v, 1)];
++p->blue_[PixOrCopyLiteral(&v, 0)]; ++p->blue_[PixOrCopyLiteral(&v, 0)];
} else if (PixOrCopyIsPaletteIx(&v)) { } else if (PixOrCopyIsCacheIdx(&v)) {
int literal_ix = 256 + kLengthCodes + PixOrCopyPaletteIx(&v); int literal_ix = 256 + kLengthCodes + PixOrCopyCacheIdx(&v);
++p->literal_[literal_ix]; ++p->literal_[literal_ix];
} else { } else {
int code, extra_bits_count, extra_bits_value; int code, extra_bits_count, extra_bits_value;

View File

@ -702,8 +702,8 @@ static void StoreImageToBitMask(
const int histogram_ix = histogram_symbols[histo_bits ? const int histogram_ix = histogram_symbols[histo_bits ?
(y >> histo_bits) * histo_xsize + (y >> histo_bits) * histo_xsize +
(x >> histo_bits) : 0]; (x >> histo_bits) : 0];
if (PixOrCopyIsPaletteIx(&v)) { if (PixOrCopyIsCacheIdx(&v)) {
const int code = PixOrCopyPaletteIx(&v); const int code = PixOrCopyCacheIdx(&v);
int literal_ix = 256 + kLengthCodes + code; int literal_ix = 256 + kLengthCodes + code;
VP8LWriteBits(bw, bitdepths[5 * histogram_ix][literal_ix], VP8LWriteBits(bw, bitdepths[5 * histogram_ix][literal_ix],
bit_symbols[5 * histogram_ix][literal_ix]); bit_symbols[5 * histogram_ix][literal_ix]);
@ -719,7 +719,7 @@ static void StoreImageToBitMask(
int bits, n_bits; int bits, n_bits;
int code, distance; int code, distance;
int len_ix; int len_ix;
PixOrCopyLengthCodeAndBits(&v, &code, &n_bits, &bits); PrefixEncode(v.len, &code, &n_bits, &bits);
len_ix = 256 + code; len_ix = 256 + code;
VP8LWriteBits(bw, bitdepths[5 * histogram_ix][len_ix], VP8LWriteBits(bw, bitdepths[5 * histogram_ix][len_ix],
bit_symbols[5 * histogram_ix][len_ix]); bit_symbols[5 * histogram_ix][len_ix]);
@ -1202,7 +1202,7 @@ int VP8LEncodeImage(const WebPConfig* const config,
if (cache_bits > 0) { if (cache_bits > 0) {
if (quality > 25) { if (quality > 25) {
if (!VP8LCalculateEstimateForPaletteSize(enc->argb_, enc->current_width_, if (!VP8LCalculateEstimateForCacheSize(enc->argb_, enc->current_width_,
height, &cache_bits)) { height, &cache_bits)) {
err = VP8_ENC_ERROR_INVALID_CONFIGURATION; err = VP8_ENC_ERROR_INVALID_CONFIGURATION;
goto Error; goto Error;