mirror of
https://github.com/webmproject/libwebp.git
synced 2024-11-20 12:28:26 +01:00
Merge "Fix bug in VP8LCalculateEstimateForCacheSize."
This commit is contained in:
commit
53b096c0d7
@ -927,33 +927,44 @@ int VP8LCalculateEstimateForCacheSize(const uint32_t* const argb,
|
|||||||
int eval_high = 1;
|
int eval_high = 1;
|
||||||
double entropy_low = MAX_ENTROPY;
|
double entropy_low = MAX_ENTROPY;
|
||||||
double entropy_high = MAX_ENTROPY;
|
double entropy_high = MAX_ENTROPY;
|
||||||
|
const double cost_mul = 5e-4;
|
||||||
int cache_bits_low = 0;
|
int cache_bits_low = 0;
|
||||||
int cache_bits_high = MAX_COLOR_CACHE_BITS;
|
int cache_bits_high = *best_cache_bits;
|
||||||
|
|
||||||
|
assert(cache_bits_high <= MAX_COLOR_CACHE_BITS);
|
||||||
|
|
||||||
|
if (cache_bits_high == 0) {
|
||||||
|
// Local color cache is disabled.
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (!BackwardReferencesHashChain(xsize, ysize, argb, 0, quality, hash_chain,
|
if (!BackwardReferencesHashChain(xsize, ysize, argb, 0, quality, hash_chain,
|
||||||
refs)) {
|
refs)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
// Do a binary search to find the optimal entropy for cache_bits.
|
// Do a binary search to find the optimal entropy for cache_bits.
|
||||||
while (cache_bits_high - cache_bits_low > 1) {
|
while (eval_low || eval_high) {
|
||||||
if (eval_low) {
|
if (eval_low) {
|
||||||
entropy_low =
|
entropy_low =
|
||||||
ComputeCacheEntropy(argb, xsize, ysize, refs, cache_bits_low);
|
ComputeCacheEntropy(argb, xsize, ysize, refs, cache_bits_low);
|
||||||
|
entropy_low += entropy_low * cache_bits_low * cost_mul;
|
||||||
eval_low = 0;
|
eval_low = 0;
|
||||||
}
|
}
|
||||||
if (eval_high) {
|
if (eval_high) {
|
||||||
entropy_high =
|
entropy_high =
|
||||||
ComputeCacheEntropy(argb, xsize, ysize, refs, cache_bits_high);
|
ComputeCacheEntropy(argb, xsize, ysize, refs, cache_bits_high);
|
||||||
|
entropy_high += entropy_high * cache_bits_high * cost_mul;
|
||||||
eval_high = 0;
|
eval_high = 0;
|
||||||
}
|
}
|
||||||
if (entropy_high < entropy_low) {
|
if (entropy_high < entropy_low) {
|
||||||
|
const int prev_cache_bits_low = cache_bits_low;
|
||||||
*best_cache_bits = cache_bits_high;
|
*best_cache_bits = cache_bits_high;
|
||||||
cache_bits_low = (cache_bits_low + cache_bits_high) / 2;
|
cache_bits_low = (cache_bits_low + cache_bits_high) / 2;
|
||||||
eval_low = 1;
|
if (cache_bits_low != prev_cache_bits_low) eval_low = 1;
|
||||||
} else {
|
} else {
|
||||||
*best_cache_bits = cache_bits_low;
|
*best_cache_bits = cache_bits_low;
|
||||||
cache_bits_high = (cache_bits_low + cache_bits_high) / 2;
|
cache_bits_high = (cache_bits_low + cache_bits_high) / 2;
|
||||||
eval_high = 1;
|
if (cache_bits_high != cache_bits_low) eval_high = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -284,7 +284,7 @@ static int GetTransformBits(int method, int histo_bits) {
|
|||||||
static int GetCacheBits(int use_palette, float quality) {
|
static int GetCacheBits(int use_palette, float quality) {
|
||||||
// Color cache is disabled for compression at lower quality or when a palette
|
// Color cache is disabled for compression at lower quality or when a palette
|
||||||
// is used.
|
// is used.
|
||||||
return (use_palette || (quality <= 25.f)) ? 0 : 7;
|
return (use_palette || (quality <= 25.f)) ? 0 : MAX_COLOR_CACHE_BITS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int EvalSubtractGreenForPalette(int palette_size, float quality) {
|
static int EvalSubtractGreenForPalette(int palette_size, float quality) {
|
||||||
@ -1309,14 +1309,12 @@ WebPEncodingError VP8LEncodeStream(const WebPConfig* const config,
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Estimate the color cache size.
|
// Estimate the color cache size.
|
||||||
|
|
||||||
if (enc->cache_bits_ > 0) {
|
|
||||||
if (!VP8LCalculateEstimateForCacheSize(enc->argb_, enc->current_width_,
|
if (!VP8LCalculateEstimateForCacheSize(enc->argb_, enc->current_width_,
|
||||||
height, quality, &enc->hash_chain_,
|
height, quality, &enc->hash_chain_,
|
||||||
&enc->refs_[0], &enc->cache_bits_)) {
|
&enc->refs_[0], &enc->cache_bits_)) {
|
||||||
err = VP8_ENC_ERROR_OUT_OF_MEMORY;
|
err = VP8_ENC_ERROR_OUT_OF_MEMORY;
|
||||||
goto Error;
|
goto Error;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Encode and write the transformed image.
|
// Encode and write the transformed image.
|
||||||
|
Loading…
Reference in New Issue
Block a user