2012-04-03 16:24:25 +02:00
|
|
|
|
// Copyright 2012 Google Inc. All Rights Reserved.
|
|
|
|
|
//
|
2013-06-07 08:05:58 +02:00
|
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
|
// that can be found in the COPYING file in the root of the source
|
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
2012-04-03 16:24:25 +02:00
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Author: Jyrki Alakuijala (jyrki@google.com)
|
|
|
|
|
//
|
2012-06-05 03:36:38 +02:00
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2014-06-11 02:53:44 +02:00
|
|
|
|
#include "../webp/config.h"
|
2012-06-05 03:36:38 +02:00
|
|
|
|
#endif
|
2012-04-03 16:24:25 +02:00
|
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
2017-01-19 09:21:40 +01:00
|
|
|
|
#include "./backward_references_enc.h"
|
|
|
|
|
#include "./histogram_enc.h"
|
2012-04-10 09:00:36 +02:00
|
|
|
|
#include "../dsp/lossless.h"
|
2016-09-12 17:23:15 +02:00
|
|
|
|
#include "../dsp/lossless_common.h"
|
2012-08-01 21:06:04 +02:00
|
|
|
|
#include "../utils/utils.h"
|
2012-04-03 16:24:25 +02:00
|
|
|
|
|
2014-03-13 19:34:12 +01:00
|
|
|
|
#define MAX_COST 1.e38
|
|
|
|
|
|
|
|
|
|
// Number of partitions for the three dominant (literal, red and blue) symbol
|
|
|
|
|
// costs.
|
|
|
|
|
#define NUM_PARTITIONS 4
|
|
|
|
|
// The size of the bin-hash corresponding to the three dominant costs.
|
|
|
|
|
#define BIN_SIZE (NUM_PARTITIONS * NUM_PARTITIONS * NUM_PARTITIONS)
|
2014-09-26 15:26:56 +02:00
|
|
|
|
// Maximum number of histograms allowed in greedy combining algorithm.
|
|
|
|
|
#define MAX_HISTO_GREEDY 100
|
2014-03-13 19:34:12 +01:00
|
|
|
|
|
2012-04-30 21:46:22 +02:00
|
|
|
|
static void HistogramClear(VP8LHistogram* const p) {
|
2014-04-28 11:11:46 +02:00
|
|
|
|
uint32_t* const literal = p->literal_;
|
2014-04-26 01:01:49 +02:00
|
|
|
|
const int cache_bits = p->palette_code_bits_;
|
2014-04-29 12:32:39 +02:00
|
|
|
|
const int histo_size = VP8LGetHistogramSize(cache_bits);
|
2014-04-26 01:01:49 +02:00
|
|
|
|
memset(p, 0, histo_size);
|
|
|
|
|
p->palette_code_bits_ = cache_bits;
|
|
|
|
|
p->literal_ = literal;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-17 15:31:22 +01:00
|
|
|
|
// Swap two histogram pointers.
|
|
|
|
|
static void HistogramSwap(VP8LHistogram** const A, VP8LHistogram** const B) {
|
|
|
|
|
VP8LHistogram* const tmp = *A;
|
|
|
|
|
*A = *B;
|
|
|
|
|
*B = tmp;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-26 01:01:49 +02:00
|
|
|
|
static void HistogramCopy(const VP8LHistogram* const src,
|
|
|
|
|
VP8LHistogram* const dst) {
|
2014-04-28 11:11:46 +02:00
|
|
|
|
uint32_t* const dst_literal = dst->literal_;
|
2014-04-26 01:01:49 +02:00
|
|
|
|
const int dst_cache_bits = dst->palette_code_bits_;
|
2014-04-29 12:32:39 +02:00
|
|
|
|
const int histo_size = VP8LGetHistogramSize(dst_cache_bits);
|
2014-04-26 01:01:49 +02:00
|
|
|
|
assert(src->palette_code_bits_ == dst_cache_bits);
|
|
|
|
|
memcpy(dst, src, histo_size);
|
|
|
|
|
dst->literal_ = dst_literal;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-29 12:32:39 +02:00
|
|
|
|
int VP8LGetHistogramSize(int cache_bits) {
|
|
|
|
|
const int literal_size = VP8LHistogramNumCodes(cache_bits);
|
|
|
|
|
const size_t total_size = sizeof(VP8LHistogram) + sizeof(int) * literal_size;
|
|
|
|
|
assert(total_size <= (size_t)0x7fffffff);
|
|
|
|
|
return (int)total_size;
|
2014-04-26 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VP8LFreeHistogram(VP8LHistogram* const histo) {
|
|
|
|
|
WebPSafeFree(histo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VP8LFreeHistogramSet(VP8LHistogramSet* const histo) {
|
|
|
|
|
WebPSafeFree(histo);
|
2012-04-30 21:46:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-06-08 20:52:31 +02:00
|
|
|
|
void VP8LHistogramStoreRefs(const VP8LBackwardRefs* const refs,
|
|
|
|
|
VP8LHistogram* const histo) {
|
2014-05-05 20:11:55 +02:00
|
|
|
|
VP8LRefsCursor c = VP8LRefsCursorInit(refs);
|
|
|
|
|
while (VP8LRefsCursorOk(&c)) {
|
|
|
|
|
VP8LHistogramAddSinglePixOrCopy(histo, c.cur_pos);
|
|
|
|
|
VP8LRefsCursorNext(&c);
|
2012-06-08 20:52:31 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-30 14:18:50 +02:00
|
|
|
|
void VP8LHistogramCreate(VP8LHistogram* const p,
|
|
|
|
|
const VP8LBackwardRefs* const refs,
|
|
|
|
|
int palette_code_bits) {
|
|
|
|
|
if (palette_code_bits >= 0) {
|
|
|
|
|
p->palette_code_bits_ = palette_code_bits;
|
|
|
|
|
}
|
2012-04-30 21:46:22 +02:00
|
|
|
|
HistogramClear(p);
|
2012-06-08 20:52:31 +02:00
|
|
|
|
VP8LHistogramStoreRefs(refs, p);
|
2012-04-30 14:18:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VP8LHistogramInit(VP8LHistogram* const p, int palette_code_bits) {
|
|
|
|
|
p->palette_code_bits_ = palette_code_bits;
|
2012-04-30 21:46:22 +02:00
|
|
|
|
HistogramClear(p);
|
2012-04-30 14:18:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-26 01:01:49 +02:00
|
|
|
|
VP8LHistogram* VP8LAllocateHistogram(int cache_bits) {
|
|
|
|
|
VP8LHistogram* histo = NULL;
|
2014-04-29 12:32:39 +02:00
|
|
|
|
const int total_size = VP8LGetHistogramSize(cache_bits);
|
2014-04-26 01:01:49 +02:00
|
|
|
|
uint8_t* const memory = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*memory));
|
|
|
|
|
if (memory == NULL) return NULL;
|
|
|
|
|
histo = (VP8LHistogram*)memory;
|
|
|
|
|
// literal_ won't necessary be aligned.
|
2014-04-28 11:11:46 +02:00
|
|
|
|
histo->literal_ = (uint32_t*)(memory + sizeof(VP8LHistogram));
|
2014-04-26 01:01:49 +02:00
|
|
|
|
VP8LHistogramInit(histo, cache_bits);
|
|
|
|
|
return histo;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-30 21:46:22 +02:00
|
|
|
|
VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits) {
|
2012-04-30 14:18:50 +02:00
|
|
|
|
int i;
|
2012-04-30 21:46:22 +02:00
|
|
|
|
VP8LHistogramSet* set;
|
2015-09-17 08:54:29 +02:00
|
|
|
|
const int histo_size = VP8LGetHistogramSize(cache_bits);
|
|
|
|
|
const size_t total_size =
|
2015-10-14 08:51:19 +02:00
|
|
|
|
sizeof(*set) + size * (sizeof(*set->histograms) +
|
|
|
|
|
histo_size + WEBP_ALIGN_CST);
|
2012-08-01 21:06:04 +02:00
|
|
|
|
uint8_t* memory = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*memory));
|
2012-04-30 21:46:22 +02:00
|
|
|
|
if (memory == NULL) return NULL;
|
|
|
|
|
|
|
|
|
|
set = (VP8LHistogramSet*)memory;
|
|
|
|
|
memory += sizeof(*set);
|
|
|
|
|
set->histograms = (VP8LHistogram**)memory;
|
|
|
|
|
memory += size * sizeof(*set->histograms);
|
|
|
|
|
set->max_size = size;
|
|
|
|
|
set->size = size;
|
2012-04-30 14:18:50 +02:00
|
|
|
|
for (i = 0; i < size; ++i) {
|
2015-10-14 08:51:19 +02:00
|
|
|
|
memory = (uint8_t*)WEBP_ALIGN(memory);
|
2014-04-26 01:01:49 +02:00
|
|
|
|
set->histograms[i] = (VP8LHistogram*)memory;
|
|
|
|
|
// literal_ won't necessary be aligned.
|
2014-04-28 11:11:46 +02:00
|
|
|
|
set->histograms[i]->literal_ = (uint32_t*)(memory + sizeof(VP8LHistogram));
|
2012-04-30 21:46:22 +02:00
|
|
|
|
VP8LHistogramInit(set->histograms[i], cache_bits);
|
2015-09-17 08:54:29 +02:00
|
|
|
|
memory += histo_size;
|
2012-04-30 14:18:50 +02:00
|
|
|
|
}
|
2012-04-30 21:46:22 +02:00
|
|
|
|
return set;
|
2012-04-30 14:18:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-30 21:46:22 +02:00
|
|
|
|
// -----------------------------------------------------------------------------
|
2012-04-30 14:18:50 +02:00
|
|
|
|
|
2012-06-08 20:52:31 +02:00
|
|
|
|
void VP8LHistogramAddSinglePixOrCopy(VP8LHistogram* const histo,
|
2012-04-30 14:18:50 +02:00
|
|
|
|
const PixOrCopy* const v) {
|
|
|
|
|
if (PixOrCopyIsLiteral(v)) {
|
2012-06-08 20:52:31 +02:00
|
|
|
|
++histo->alpha_[PixOrCopyLiteral(v, 3)];
|
|
|
|
|
++histo->red_[PixOrCopyLiteral(v, 2)];
|
|
|
|
|
++histo->literal_[PixOrCopyLiteral(v, 1)];
|
|
|
|
|
++histo->blue_[PixOrCopyLiteral(v, 0)];
|
2012-04-30 14:18:50 +02:00
|
|
|
|
} else if (PixOrCopyIsCacheIdx(v)) {
|
2014-04-28 11:11:46 +02:00
|
|
|
|
const int literal_ix =
|
2014-04-26 01:01:49 +02:00
|
|
|
|
NUM_LITERAL_CODES + NUM_LENGTH_CODES + PixOrCopyCacheIdx(v);
|
2012-06-08 20:52:31 +02:00
|
|
|
|
++histo->literal_[literal_ix];
|
2012-04-03 16:24:25 +02:00
|
|
|
|
} else {
|
2013-08-12 20:54:48 +02:00
|
|
|
|
int code, extra_bits;
|
|
|
|
|
VP8LPrefixEncodeBits(PixOrCopyLength(v), &code, &extra_bits);
|
2014-04-26 01:01:49 +02:00
|
|
|
|
++histo->literal_[NUM_LITERAL_CODES + code];
|
2013-08-12 20:54:48 +02:00
|
|
|
|
VP8LPrefixEncodeBits(PixOrCopyDistance(v), &code, &extra_bits);
|
2012-06-08 20:52:31 +02:00
|
|
|
|
++histo->distance_[code];
|
2012-04-03 16:24:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-16 18:52:05 +01:00
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Entropy-related functions.
|
|
|
|
|
|
|
|
|
|
static WEBP_INLINE double BitsEntropyRefine(const VP8LBitEntropy* entropy) {
|
|
|
|
|
double mix;
|
|
|
|
|
if (entropy->nonzeros < 5) {
|
|
|
|
|
if (entropy->nonzeros <= 1) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
// Two symbols, they will be 0 and 1 in a Huffman code.
|
|
|
|
|
// Let's mix in a bit of entropy to favor good clustering when
|
|
|
|
|
// distributions of these are combined.
|
|
|
|
|
if (entropy->nonzeros == 2) {
|
|
|
|
|
return 0.99 * entropy->sum + 0.01 * entropy->entropy;
|
|
|
|
|
}
|
|
|
|
|
// No matter what the entropy says, we cannot be better than min_limit
|
|
|
|
|
// with Huffman coding. I am mixing a bit of entropy into the
|
|
|
|
|
// min_limit since it produces much better (~0.5 %) compression results
|
|
|
|
|
// perhaps because of better entropy clustering.
|
|
|
|
|
if (entropy->nonzeros == 3) {
|
|
|
|
|
mix = 0.95;
|
|
|
|
|
} else {
|
|
|
|
|
mix = 0.7; // nonzeros == 4.
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
mix = 0.627;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
double min_limit = 2 * entropy->sum - entropy->max_val;
|
|
|
|
|
min_limit = mix * min_limit + (1.0 - mix) * entropy->entropy;
|
|
|
|
|
return (entropy->entropy < min_limit) ? min_limit : entropy->entropy;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double VP8LBitsEntropy(const uint32_t* const array, int n,
|
|
|
|
|
uint32_t* const trivial_symbol) {
|
|
|
|
|
VP8LBitEntropy entropy;
|
|
|
|
|
VP8LBitsEntropyUnrefined(array, n, &entropy);
|
|
|
|
|
if (trivial_symbol != NULL) {
|
|
|
|
|
*trivial_symbol =
|
|
|
|
|
(entropy.nonzeros == 1) ? entropy.nonzero_code : VP8L_NON_TRIVIAL_SYM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BitsEntropyRefine(&entropy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static double InitialHuffmanCost(void) {
|
|
|
|
|
// Small bias because Huffman code length is typically not stored in
|
|
|
|
|
// full length.
|
|
|
|
|
static const int kHuffmanCodeOfHuffmanCodeSize = CODE_LENGTH_CODES * 3;
|
|
|
|
|
static const double kSmallBias = 9.1;
|
|
|
|
|
return kHuffmanCodeOfHuffmanCodeSize - kSmallBias;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Finalize the Huffman cost based on streak numbers and length type (<3 or >=3)
|
|
|
|
|
static double FinalHuffmanCost(const VP8LStreaks* const stats) {
|
2017-01-06 13:17:14 +01:00
|
|
|
|
// The constants in this function are experimental and got rounded from
|
|
|
|
|
// their original values in 1/8 when switched to 1/1024.
|
2015-12-16 18:52:05 +01:00
|
|
|
|
double retval = InitialHuffmanCost();
|
2017-01-06 13:17:14 +01:00
|
|
|
|
// Second coefficient: Many zeros in the histogram are covered efficiently
|
|
|
|
|
// by a run-length encode. Originally 2/8.
|
2015-12-16 18:52:05 +01:00
|
|
|
|
retval += stats->counts[0] * 1.5625 + 0.234375 * stats->streaks[0][1];
|
2017-01-06 13:17:14 +01:00
|
|
|
|
// Second coefficient: Constant values are encoded less efficiently, but still
|
|
|
|
|
// RLE'ed. Originally 6/8.
|
2015-12-16 18:52:05 +01:00
|
|
|
|
retval += stats->counts[1] * 2.578125 + 0.703125 * stats->streaks[1][1];
|
2017-01-06 13:17:14 +01:00
|
|
|
|
// 0s are usually encoded more efficiently than non-0s.
|
|
|
|
|
// Originally 15/8.
|
2015-12-16 18:52:05 +01:00
|
|
|
|
retval += 1.796875 * stats->streaks[0][0];
|
2017-01-06 13:17:14 +01:00
|
|
|
|
// Originally 26/8.
|
2015-12-16 18:52:05 +01:00
|
|
|
|
retval += 3.28125 * stats->streaks[1][0];
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the symbol entropy for the distribution 'population'.
|
|
|
|
|
// Set 'trivial_sym', if there's only one symbol present in the distribution.
|
|
|
|
|
static double PopulationCost(const uint32_t* const population, int length,
|
|
|
|
|
uint32_t* const trivial_sym) {
|
2015-12-17 16:25:34 +01:00
|
|
|
|
VP8LBitEntropy bit_entropy;
|
|
|
|
|
VP8LStreaks stats;
|
|
|
|
|
VP8LGetEntropyUnrefined(population, length, &bit_entropy, &stats);
|
|
|
|
|
if (trivial_sym != NULL) {
|
|
|
|
|
*trivial_sym = (bit_entropy.nonzeros == 1) ? bit_entropy.nonzero_code
|
|
|
|
|
: VP8L_NON_TRIVIAL_SYM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BitsEntropyRefine(&bit_entropy) + FinalHuffmanCost(&stats);
|
2015-12-16 18:52:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-28 13:22:13 +02:00
|
|
|
|
// trivial_at_end is 1 if the two histograms only have one element that is
|
|
|
|
|
// non-zero: both the zero-th one, or both the last one.
|
2015-12-16 18:52:05 +01:00
|
|
|
|
static WEBP_INLINE double GetCombinedEntropy(const uint32_t* const X,
|
|
|
|
|
const uint32_t* const Y,
|
2016-09-28 13:22:13 +02:00
|
|
|
|
int length, int trivial_at_end) {
|
2015-12-16 18:52:05 +01:00
|
|
|
|
VP8LStreaks stats;
|
2016-09-28 13:22:13 +02:00
|
|
|
|
if (trivial_at_end) {
|
|
|
|
|
// This configuration is due to palettization that transforms an indexed
|
|
|
|
|
// pixel into 0xff000000 | (pixel << 8) in VP8LBundleColorMap.
|
|
|
|
|
// BitsEntropyRefine is 0 for histograms with only one non-zero value.
|
|
|
|
|
// Only FinalHuffmanCost needs to be evaluated.
|
|
|
|
|
memset(&stats, 0, sizeof(stats));
|
|
|
|
|
// Deal with the non-zero value at index 0 or length-1.
|
|
|
|
|
stats.streaks[1][0] += 1;
|
|
|
|
|
// Deal with the following/previous zero streak.
|
|
|
|
|
stats.counts[0] += 1;
|
|
|
|
|
stats.streaks[0][1] += length - 1;
|
|
|
|
|
return FinalHuffmanCost(&stats);
|
|
|
|
|
} else {
|
|
|
|
|
VP8LBitEntropy bit_entropy;
|
|
|
|
|
VP8LGetCombinedEntropyUnrefined(X, Y, length, &bit_entropy, &stats);
|
2015-12-16 18:52:05 +01:00
|
|
|
|
|
2016-09-28 13:22:13 +02:00
|
|
|
|
return BitsEntropyRefine(&bit_entropy) + FinalHuffmanCost(&stats);
|
|
|
|
|
}
|
2015-12-16 18:52:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Estimates the Entropy + Huffman + other block overhead size cost.
|
|
|
|
|
double VP8LHistogramEstimateBits(const VP8LHistogram* const p) {
|
|
|
|
|
return
|
|
|
|
|
PopulationCost(
|
|
|
|
|
p->literal_, VP8LHistogramNumCodes(p->palette_code_bits_), NULL)
|
|
|
|
|
+ PopulationCost(p->red_, NUM_LITERAL_CODES, NULL)
|
|
|
|
|
+ PopulationCost(p->blue_, NUM_LITERAL_CODES, NULL)
|
|
|
|
|
+ PopulationCost(p->alpha_, NUM_LITERAL_CODES, NULL)
|
|
|
|
|
+ PopulationCost(p->distance_, NUM_DISTANCE_CODES, NULL)
|
|
|
|
|
+ VP8LExtraCost(p->literal_ + NUM_LITERAL_CODES, NUM_LENGTH_CODES)
|
|
|
|
|
+ VP8LExtraCost(p->distance_, NUM_DISTANCE_CODES);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-18 22:34:32 +01:00
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Various histogram combine/cost-eval functions
|
|
|
|
|
|
2014-03-03 22:49:54 +01:00
|
|
|
|
static int GetCombinedHistogramEntropy(const VP8LHistogram* const a,
|
|
|
|
|
const VP8LHistogram* const b,
|
|
|
|
|
double cost_threshold,
|
|
|
|
|
double* cost) {
|
2014-04-26 01:01:49 +02:00
|
|
|
|
const int palette_code_bits = a->palette_code_bits_;
|
2016-09-28 13:22:13 +02:00
|
|
|
|
int trivial_at_end = 0;
|
2014-04-26 01:01:49 +02:00
|
|
|
|
assert(a->palette_code_bits_ == b->palette_code_bits_);
|
2015-12-16 18:52:05 +01:00
|
|
|
|
*cost += GetCombinedEntropy(a->literal_, b->literal_,
|
2016-09-28 13:22:13 +02:00
|
|
|
|
VP8LHistogramNumCodes(palette_code_bits), 0);
|
2014-04-26 01:01:49 +02:00
|
|
|
|
*cost += VP8LExtraCostCombined(a->literal_ + NUM_LITERAL_CODES,
|
|
|
|
|
b->literal_ + NUM_LITERAL_CODES,
|
2014-04-15 12:55:20 +02:00
|
|
|
|
NUM_LENGTH_CODES);
|
2014-03-03 22:49:54 +01:00
|
|
|
|
if (*cost > cost_threshold) return 0;
|
|
|
|
|
|
2016-09-28 13:22:13 +02:00
|
|
|
|
if (a->trivial_symbol_ != VP8L_NON_TRIVIAL_SYM &&
|
|
|
|
|
a->trivial_symbol_ == b->trivial_symbol_) {
|
|
|
|
|
// A, R and B are all 0 or 0xff.
|
|
|
|
|
const uint32_t color_a = (a->trivial_symbol_ >> 24) & 0xff;
|
|
|
|
|
const uint32_t color_r = (a->trivial_symbol_ >> 16) & 0xff;
|
|
|
|
|
const uint32_t color_b = (a->trivial_symbol_ >> 0) & 0xff;
|
|
|
|
|
if ((color_a == 0 || color_a == 0xff) &&
|
|
|
|
|
(color_r == 0 || color_r == 0xff) &&
|
|
|
|
|
(color_b == 0 || color_b == 0xff)) {
|
|
|
|
|
trivial_at_end = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*cost +=
|
|
|
|
|
GetCombinedEntropy(a->red_, b->red_, NUM_LITERAL_CODES, trivial_at_end);
|
2014-03-03 22:49:54 +01:00
|
|
|
|
if (*cost > cost_threshold) return 0;
|
|
|
|
|
|
2016-09-28 13:22:13 +02:00
|
|
|
|
*cost +=
|
|
|
|
|
GetCombinedEntropy(a->blue_, b->blue_, NUM_LITERAL_CODES, trivial_at_end);
|
2014-03-03 22:49:54 +01:00
|
|
|
|
if (*cost > cost_threshold) return 0;
|
|
|
|
|
|
2016-09-28 13:22:13 +02:00
|
|
|
|
*cost += GetCombinedEntropy(a->alpha_, b->alpha_, NUM_LITERAL_CODES,
|
|
|
|
|
trivial_at_end);
|
2014-03-03 22:49:54 +01:00
|
|
|
|
if (*cost > cost_threshold) return 0;
|
|
|
|
|
|
2016-09-28 13:22:13 +02:00
|
|
|
|
*cost +=
|
|
|
|
|
GetCombinedEntropy(a->distance_, b->distance_, NUM_DISTANCE_CODES, 0);
|
2015-12-16 18:52:05 +01:00
|
|
|
|
*cost +=
|
|
|
|
|
VP8LExtraCostCombined(a->distance_, b->distance_, NUM_DISTANCE_CODES);
|
2014-03-03 22:49:54 +01:00
|
|
|
|
if (*cost > cost_threshold) return 0;
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-28 13:22:13 +02:00
|
|
|
|
static WEBP_INLINE void HistogramAdd(const VP8LHistogram* const a,
|
|
|
|
|
const VP8LHistogram* const b,
|
|
|
|
|
VP8LHistogram* const out) {
|
|
|
|
|
VP8LHistogramAdd(a, b, out);
|
|
|
|
|
out->trivial_symbol_ = (a->trivial_symbol_ == b->trivial_symbol_)
|
|
|
|
|
? a->trivial_symbol_
|
|
|
|
|
: VP8L_NON_TRIVIAL_SYM;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-18 22:34:32 +01:00
|
|
|
|
// Performs out = a + b, computing the cost C(a+b) - C(a) - C(b) while comparing
|
|
|
|
|
// to the threshold value 'cost_threshold'. The score returned is
|
|
|
|
|
// Score = C(a+b) - C(a) - C(b), where C(a) + C(b) is known and fixed.
|
|
|
|
|
// Since the previous score passed is 'cost_threshold', we only need to compare
|
|
|
|
|
// the partial cost against 'cost_threshold + C(a) + C(b)' to possibly bail-out
|
|
|
|
|
// early.
|
|
|
|
|
static double HistogramAddEval(const VP8LHistogram* const a,
|
|
|
|
|
const VP8LHistogram* const b,
|
|
|
|
|
VP8LHistogram* const out,
|
|
|
|
|
double cost_threshold) {
|
|
|
|
|
double cost = 0;
|
|
|
|
|
const double sum_cost = a->bit_cost_ + b->bit_cost_;
|
|
|
|
|
cost_threshold += sum_cost;
|
|
|
|
|
|
2014-03-03 22:49:54 +01:00
|
|
|
|
if (GetCombinedHistogramEntropy(a, b, cost_threshold, &cost)) {
|
2016-09-28 13:22:13 +02:00
|
|
|
|
HistogramAdd(a, b, out);
|
2014-03-04 09:38:14 +01:00
|
|
|
|
out->bit_cost_ = cost;
|
2014-04-27 09:49:28 +02:00
|
|
|
|
out->palette_code_bits_ = a->palette_code_bits_;
|
2014-03-03 22:49:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-18 22:34:32 +01:00
|
|
|
|
return cost - sum_cost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Same as HistogramAddEval(), except that the resulting histogram
|
|
|
|
|
// is not stored. Only the cost C(a+b) - C(a) is evaluated. We omit
|
|
|
|
|
// the term C(b) which is constant over all the evaluations.
|
|
|
|
|
static double HistogramAddThresh(const VP8LHistogram* const a,
|
|
|
|
|
const VP8LHistogram* const b,
|
|
|
|
|
double cost_threshold) {
|
|
|
|
|
double cost = -a->bit_cost_;
|
2014-03-03 22:49:54 +01:00
|
|
|
|
GetCombinedHistogramEntropy(a, b, cost_threshold, &cost);
|
2013-03-18 22:34:32 +01:00
|
|
|
|
return cost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2014-03-13 19:34:12 +01:00
|
|
|
|
// The structure to keep track of cost range for the three dominant entropy
|
|
|
|
|
// symbols.
|
|
|
|
|
// TODO(skal): Evaluate if float can be used here instead of double for
|
|
|
|
|
// representing the entropy costs.
|
|
|
|
|
typedef struct {
|
|
|
|
|
double literal_max_;
|
|
|
|
|
double literal_min_;
|
|
|
|
|
double red_max_;
|
|
|
|
|
double red_min_;
|
|
|
|
|
double blue_max_;
|
|
|
|
|
double blue_min_;
|
|
|
|
|
} DominantCostRange;
|
|
|
|
|
|
|
|
|
|
static void DominantCostRangeInit(DominantCostRange* const c) {
|
|
|
|
|
c->literal_max_ = 0.;
|
|
|
|
|
c->literal_min_ = MAX_COST;
|
|
|
|
|
c->red_max_ = 0.;
|
|
|
|
|
c->red_min_ = MAX_COST;
|
|
|
|
|
c->blue_max_ = 0.;
|
|
|
|
|
c->blue_min_ = MAX_COST;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void UpdateDominantCostRange(
|
|
|
|
|
const VP8LHistogram* const h, DominantCostRange* const c) {
|
|
|
|
|
if (c->literal_max_ < h->literal_cost_) c->literal_max_ = h->literal_cost_;
|
|
|
|
|
if (c->literal_min_ > h->literal_cost_) c->literal_min_ = h->literal_cost_;
|
|
|
|
|
if (c->red_max_ < h->red_cost_) c->red_max_ = h->red_cost_;
|
|
|
|
|
if (c->red_min_ > h->red_cost_) c->red_min_ = h->red_cost_;
|
|
|
|
|
if (c->blue_max_ < h->blue_cost_) c->blue_max_ = h->blue_cost_;
|
|
|
|
|
if (c->blue_min_ > h->blue_cost_) c->blue_min_ = h->blue_cost_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void UpdateHistogramCost(VP8LHistogram* const h) {
|
Change Entropy based Histogram Combine heuristic.
Don't combine the Histograms that have trivial (single valued A, R & B)
symbols.
Following is the compression savings data along with compression time (before
& after) per image.
Before After
bpp, rate(MP/s) bpp, rate(MP/s)
Q=25, method = 4 2.508, 1.807 2.499, 1.916
Q=50, method = 4 2.460, 1.488 2.456, 1.512
Q=75, method = 4 2.452, 1.078 2.450, 1.092
Q=25, method = 5 2.505, 1.398 2.496, 1.383
Q=50, method = 5 2.458, 1.170 2.453, 1.143
Q=75, method = 5 2.453, 0.886 2.450, 0.855
This change provides 0.1-0.4% compression gains and speeds up the lossless
compression for the default method=4 (the drop in compression speed is between 1-3.5% for method=5).
Change-Id: Idfd88c2092f37afacd26a97097b3053f8183953a
2014-09-30 22:40:38 +02:00
|
|
|
|
uint32_t alpha_sym, red_sym, blue_sym;
|
2015-12-17 16:25:34 +01:00
|
|
|
|
const double alpha_cost =
|
|
|
|
|
PopulationCost(h->alpha_, NUM_LITERAL_CODES, &alpha_sym);
|
2014-03-14 19:18:52 +01:00
|
|
|
|
const double distance_cost =
|
2015-12-16 18:52:05 +01:00
|
|
|
|
PopulationCost(h->distance_, NUM_DISTANCE_CODES, NULL) +
|
2014-04-15 12:55:20 +02:00
|
|
|
|
VP8LExtraCost(h->distance_, NUM_DISTANCE_CODES);
|
2014-03-13 19:34:12 +01:00
|
|
|
|
const int num_codes = VP8LHistogramNumCodes(h->palette_code_bits_);
|
2015-12-16 18:52:05 +01:00
|
|
|
|
h->literal_cost_ = PopulationCost(h->literal_, num_codes, NULL) +
|
2014-04-26 01:01:49 +02:00
|
|
|
|
VP8LExtraCost(h->literal_ + NUM_LITERAL_CODES,
|
|
|
|
|
NUM_LENGTH_CODES);
|
2015-12-16 18:52:05 +01:00
|
|
|
|
h->red_cost_ = PopulationCost(h->red_, NUM_LITERAL_CODES, &red_sym);
|
|
|
|
|
h->blue_cost_ = PopulationCost(h->blue_, NUM_LITERAL_CODES, &blue_sym);
|
2014-03-13 19:34:12 +01:00
|
|
|
|
h->bit_cost_ = h->literal_cost_ + h->red_cost_ + h->blue_cost_ +
|
|
|
|
|
alpha_cost + distance_cost;
|
2014-11-20 22:46:36 +01:00
|
|
|
|
if ((alpha_sym | red_sym | blue_sym) == VP8L_NON_TRIVIAL_SYM) {
|
|
|
|
|
h->trivial_symbol_ = VP8L_NON_TRIVIAL_SYM;
|
Change Entropy based Histogram Combine heuristic.
Don't combine the Histograms that have trivial (single valued A, R & B)
symbols.
Following is the compression savings data along with compression time (before
& after) per image.
Before After
bpp, rate(MP/s) bpp, rate(MP/s)
Q=25, method = 4 2.508, 1.807 2.499, 1.916
Q=50, method = 4 2.460, 1.488 2.456, 1.512
Q=75, method = 4 2.452, 1.078 2.450, 1.092
Q=25, method = 5 2.505, 1.398 2.496, 1.383
Q=50, method = 5 2.458, 1.170 2.453, 1.143
Q=75, method = 5 2.453, 0.886 2.450, 0.855
This change provides 0.1-0.4% compression gains and speeds up the lossless
compression for the default method=4 (the drop in compression speed is between 1-3.5% for method=5).
Change-Id: Idfd88c2092f37afacd26a97097b3053f8183953a
2014-09-30 22:40:38 +02:00
|
|
|
|
} else {
|
|
|
|
|
h->trivial_symbol_ =
|
|
|
|
|
((uint32_t)alpha_sym << 24) | (red_sym << 16) | (blue_sym << 0);
|
|
|
|
|
}
|
2014-03-13 19:34:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int GetBinIdForEntropy(double min, double max, double val) {
|
2016-06-08 13:38:00 +02:00
|
|
|
|
const double range = max - min;
|
|
|
|
|
if (range > 0.) {
|
|
|
|
|
const double delta = val - min;
|
|
|
|
|
return (int)((NUM_PARTITIONS - 1e-6) * delta / range);
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2014-03-13 19:34:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-08 13:38:00 +02:00
|
|
|
|
static int GetHistoBinIndex(const VP8LHistogram* const h,
|
|
|
|
|
const DominantCostRange* const c, int low_effort) {
|
|
|
|
|
int bin_id = GetBinIdForEntropy(c->literal_min_, c->literal_max_,
|
|
|
|
|
h->literal_cost_);
|
2014-12-19 18:33:17 +01:00
|
|
|
|
assert(bin_id < NUM_PARTITIONS);
|
2016-06-08 13:38:00 +02:00
|
|
|
|
if (!low_effort) {
|
|
|
|
|
bin_id = bin_id * NUM_PARTITIONS
|
|
|
|
|
+ GetBinIdForEntropy(c->red_min_, c->red_max_, h->red_cost_);
|
|
|
|
|
bin_id = bin_id * NUM_PARTITIONS
|
|
|
|
|
+ GetBinIdForEntropy(c->blue_min_, c->blue_max_, h->blue_cost_);
|
|
|
|
|
assert(bin_id < BIN_SIZE);
|
|
|
|
|
}
|
2014-03-13 19:34:12 +01:00
|
|
|
|
return bin_id;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-13 21:56:30 +01:00
|
|
|
|
// Construct the histograms from backward references.
|
2014-03-13 19:34:12 +01:00
|
|
|
|
static void HistogramBuild(
|
|
|
|
|
int xsize, int histo_bits, const VP8LBackwardRefs* const backward_refs,
|
2014-05-08 23:13:07 +02:00
|
|
|
|
VP8LHistogramSet* const image_histo) {
|
2012-04-30 14:18:50 +02:00
|
|
|
|
int x = 0, y = 0;
|
2012-06-08 20:52:31 +02:00
|
|
|
|
const int histo_xsize = VP8LSubSampleSize(xsize, histo_bits);
|
2014-05-08 23:13:07 +02:00
|
|
|
|
VP8LHistogram** const histograms = image_histo->histograms;
|
2014-05-05 20:11:55 +02:00
|
|
|
|
VP8LRefsCursor c = VP8LRefsCursorInit(backward_refs);
|
2012-06-08 20:52:31 +02:00
|
|
|
|
assert(histo_bits > 0);
|
2014-05-05 20:11:55 +02:00
|
|
|
|
while (VP8LRefsCursorOk(&c)) {
|
|
|
|
|
const PixOrCopy* const v = c.cur_pos;
|
2012-06-08 20:52:31 +02:00
|
|
|
|
const int ix = (y >> histo_bits) * histo_xsize + (x >> histo_bits);
|
|
|
|
|
VP8LHistogramAddSinglePixOrCopy(histograms[ix], v);
|
2012-04-30 14:18:50 +02:00
|
|
|
|
x += PixOrCopyLength(v);
|
2012-04-03 16:24:25 +02:00
|
|
|
|
while (x >= xsize) {
|
|
|
|
|
x -= xsize;
|
|
|
|
|
++y;
|
|
|
|
|
}
|
2014-05-05 20:11:55 +02:00
|
|
|
|
VP8LRefsCursorNext(&c);
|
2012-04-03 16:24:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-08 23:13:07 +02:00
|
|
|
|
// Copies the histograms and computes its bit_cost.
|
|
|
|
|
static void HistogramCopyAndAnalyze(
|
|
|
|
|
VP8LHistogramSet* const orig_histo, VP8LHistogramSet* const image_histo) {
|
2014-03-13 19:34:12 +01:00
|
|
|
|
int i;
|
2014-05-08 23:13:07 +02:00
|
|
|
|
const int histo_size = orig_histo->size;
|
|
|
|
|
VP8LHistogram** const orig_histograms = orig_histo->histograms;
|
|
|
|
|
VP8LHistogram** const histograms = image_histo->histograms;
|
2014-03-13 19:34:12 +01:00
|
|
|
|
for (i = 0; i < histo_size; ++i) {
|
2014-05-08 23:13:07 +02:00
|
|
|
|
VP8LHistogram* const histo = orig_histograms[i];
|
|
|
|
|
UpdateHistogramCost(histo);
|
|
|
|
|
// Copy histograms from orig_histo[] to image_histo[].
|
|
|
|
|
HistogramCopy(histo, histograms[i]);
|
2014-03-13 19:34:12 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-08 23:13:07 +02:00
|
|
|
|
// Partition histograms to different entropy bins for three dominant (literal,
|
2014-03-13 19:34:12 +01:00
|
|
|
|
// red and blue) symbol costs and compute the histogram aggregate bit_cost.
|
2014-12-19 18:33:17 +01:00
|
|
|
|
static void HistogramAnalyzeEntropyBin(VP8LHistogramSet* const image_histo,
|
2016-06-17 09:52:36 +02:00
|
|
|
|
uint16_t* const bin_map,
|
|
|
|
|
int low_effort) {
|
2014-03-13 19:34:12 +01:00
|
|
|
|
int i;
|
2014-05-08 23:13:07 +02:00
|
|
|
|
VP8LHistogram** const histograms = image_histo->histograms;
|
|
|
|
|
const int histo_size = image_histo->size;
|
2014-03-13 21:56:30 +01:00
|
|
|
|
DominantCostRange cost_range;
|
|
|
|
|
DominantCostRangeInit(&cost_range);
|
2014-03-13 19:34:12 +01:00
|
|
|
|
|
2014-03-13 21:56:30 +01:00
|
|
|
|
// Analyze the dominant (literal, red and blue) entropy costs.
|
|
|
|
|
for (i = 0; i < histo_size; ++i) {
|
2016-06-17 09:52:36 +02:00
|
|
|
|
UpdateDominantCostRange(histograms[i], &cost_range);
|
2014-03-13 21:56:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// bin-hash histograms on three of the dominant (literal, red and blue)
|
2016-06-17 09:52:36 +02:00
|
|
|
|
// symbol costs and store the resulting bin_id for each histogram.
|
2014-03-13 21:56:30 +01:00
|
|
|
|
for (i = 0; i < histo_size; ++i) {
|
2016-06-17 09:52:36 +02:00
|
|
|
|
bin_map[i] = GetHistoBinIndex(histograms[i], &cost_range, low_effort);
|
2014-03-13 19:34:12 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-20 17:58:04 +02:00
|
|
|
|
// Compact image_histo[] by merging some histograms with same bin_id together if
|
|
|
|
|
// it's advantageous.
|
2014-12-17 15:31:22 +01:00
|
|
|
|
static VP8LHistogram* HistogramCombineEntropyBin(
|
|
|
|
|
VP8LHistogramSet* const image_histo,
|
|
|
|
|
VP8LHistogram* cur_combo,
|
2016-06-20 17:58:04 +02:00
|
|
|
|
const uint16_t* const bin_map, int bin_map_size, int num_bins,
|
2014-12-19 18:33:17 +01:00
|
|
|
|
double combine_cost_factor, int low_effort) {
|
2014-05-08 23:13:07 +02:00
|
|
|
|
VP8LHistogram** const histograms = image_histo->histograms;
|
2016-06-20 17:58:04 +02:00
|
|
|
|
int idx;
|
|
|
|
|
// Work in-place: processed histograms are put at the beginning of
|
|
|
|
|
// image_histo[]. At the end, we just have to truncate the array.
|
|
|
|
|
int size = 0;
|
|
|
|
|
struct {
|
|
|
|
|
int16_t first; // position of the histogram that accumulates all
|
|
|
|
|
// histograms with the same bin_id
|
|
|
|
|
uint16_t num_combine_failures; // number of combine failures per bin_id
|
|
|
|
|
} bin_info[BIN_SIZE];
|
|
|
|
|
|
|
|
|
|
assert(num_bins <= BIN_SIZE);
|
|
|
|
|
for (idx = 0; idx < num_bins; ++idx) {
|
|
|
|
|
bin_info[idx].first = -1;
|
|
|
|
|
bin_info[idx].num_combine_failures = 0;
|
|
|
|
|
}
|
2014-03-13 19:34:12 +01:00
|
|
|
|
|
2016-06-20 17:58:04 +02:00
|
|
|
|
for (idx = 0; idx < bin_map_size; ++idx) {
|
|
|
|
|
const int bin_id = bin_map[idx];
|
|
|
|
|
const int first = bin_info[bin_id].first;
|
|
|
|
|
assert(size <= idx);
|
|
|
|
|
if (first == -1) {
|
|
|
|
|
// just move histogram #idx to its final position
|
|
|
|
|
histograms[size] = histograms[idx];
|
|
|
|
|
bin_info[bin_id].first = size++;
|
|
|
|
|
} else if (low_effort) {
|
2016-09-28 13:22:13 +02:00
|
|
|
|
HistogramAdd(histograms[idx], histograms[first], histograms[first]);
|
2016-06-20 17:58:04 +02:00
|
|
|
|
} else {
|
|
|
|
|
// try to merge #idx into #first (both share the same bin_id)
|
|
|
|
|
const double bit_cost = histograms[idx]->bit_cost_;
|
|
|
|
|
const double bit_cost_thresh = -bit_cost * combine_cost_factor;
|
|
|
|
|
const double curr_cost_diff =
|
|
|
|
|
HistogramAddEval(histograms[first], histograms[idx],
|
|
|
|
|
cur_combo, bit_cost_thresh);
|
|
|
|
|
if (curr_cost_diff < bit_cost_thresh) {
|
|
|
|
|
// Try to merge two histograms only if the combo is a trivial one or
|
|
|
|
|
// the two candidate histograms are already non-trivial.
|
|
|
|
|
// For some images, 'try_combine' turns out to be false for a lot of
|
|
|
|
|
// histogram pairs. In that case, we fallback to combining
|
|
|
|
|
// histograms as usual to avoid increasing the header size.
|
|
|
|
|
const int try_combine =
|
|
|
|
|
(cur_combo->trivial_symbol_ != VP8L_NON_TRIVIAL_SYM) ||
|
|
|
|
|
((histograms[idx]->trivial_symbol_ == VP8L_NON_TRIVIAL_SYM) &&
|
|
|
|
|
(histograms[first]->trivial_symbol_ == VP8L_NON_TRIVIAL_SYM));
|
|
|
|
|
const int max_combine_failures = 32;
|
|
|
|
|
if (try_combine ||
|
|
|
|
|
bin_info[bin_id].num_combine_failures >= max_combine_failures) {
|
|
|
|
|
// move the (better) merged histogram to its final slot
|
|
|
|
|
HistogramSwap(&cur_combo, &histograms[first]);
|
|
|
|
|
} else {
|
|
|
|
|
histograms[size++] = histograms[idx];
|
|
|
|
|
++bin_info[bin_id].num_combine_failures;
|
2014-03-13 19:34:12 +01:00
|
|
|
|
}
|
2016-06-20 17:58:04 +02:00
|
|
|
|
} else {
|
|
|
|
|
histograms[size++] = histograms[idx];
|
2014-03-13 19:34:12 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-20 17:58:04 +02:00
|
|
|
|
}
|
|
|
|
|
image_histo->size = size;
|
|
|
|
|
if (low_effort) {
|
|
|
|
|
// for low_effort case, update the final cost when everything is merged
|
|
|
|
|
for (idx = 0; idx < size; ++idx) {
|
|
|
|
|
UpdateHistogramCost(histograms[idx]);
|
2014-12-19 18:33:17 +01:00
|
|
|
|
}
|
2014-03-13 19:34:12 +01:00
|
|
|
|
}
|
2014-12-17 15:31:22 +01:00
|
|
|
|
return cur_combo;
|
2014-03-13 19:34:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 20:31:24 +01:00
|
|
|
|
// Implement a Lehmer random number generator with a multiplicative constant of
|
|
|
|
|
// 48271 and a modulo constant of 2^31 − 1.
|
2016-08-17 00:02:43 +02:00
|
|
|
|
static uint32_t MyRand(uint32_t* const seed) {
|
2017-02-22 05:12:17 +01:00
|
|
|
|
*seed = (uint32_t)(((uint64_t)(*seed) * 48271u) % 2147483647u);
|
2017-02-17 20:31:24 +01:00
|
|
|
|
assert(*seed > 0);
|
2012-05-22 09:13:01 +02:00
|
|
|
|
return *seed;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 15:26:56 +02:00
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Histogram pairs priority queue
|
|
|
|
|
|
|
|
|
|
// Pair of histograms. Negative idx1 value means that pair is out-of-date.
|
|
|
|
|
typedef struct {
|
|
|
|
|
int idx1;
|
|
|
|
|
int idx2;
|
|
|
|
|
double cost_diff;
|
|
|
|
|
double cost_combo;
|
|
|
|
|
} HistogramPair;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2015-12-10 11:08:20 +01:00
|
|
|
|
HistogramPair* queue;
|
2014-09-26 15:26:56 +02:00
|
|
|
|
int size;
|
2015-12-10 11:08:20 +01:00
|
|
|
|
int max_size;
|
|
|
|
|
} HistoQueue;
|
|
|
|
|
|
|
|
|
|
static int HistoQueueInit(HistoQueue* const histo_queue, const int max_index) {
|
|
|
|
|
histo_queue->size = 0;
|
|
|
|
|
// max_index^2 for the queue size is safe. If you look at
|
|
|
|
|
// HistogramCombineGreedy, and imagine that UpdateQueueFront always pushes
|
|
|
|
|
// data to the queue, you insert at most:
|
|
|
|
|
// - max_index*(max_index-1)/2 (the first two for loops)
|
|
|
|
|
// - max_index - 1 in the last for loop at the first iteration of the while
|
|
|
|
|
// loop, max_index - 2 at the second iteration ... therefore
|
|
|
|
|
// max_index*(max_index-1)/2 overall too
|
|
|
|
|
histo_queue->max_size = max_index * max_index;
|
|
|
|
|
// We allocate max_size + 1 because the last element at index "size" is
|
|
|
|
|
// used as temporary data (and it could be up to max_size).
|
2016-10-05 09:39:08 +02:00
|
|
|
|
histo_queue->queue = (HistogramPair*)WebPSafeMalloc(
|
|
|
|
|
histo_queue->max_size + 1, sizeof(*histo_queue->queue));
|
2015-12-10 11:08:20 +01:00
|
|
|
|
return histo_queue->queue != NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void HistoQueueClear(HistoQueue* const histo_queue) {
|
|
|
|
|
assert(histo_queue != NULL);
|
|
|
|
|
WebPSafeFree(histo_queue->queue);
|
2014-09-26 15:26:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-22 14:57:29 +01:00
|
|
|
|
// Pop a specific pair in the queue by replacing it with the last one
|
|
|
|
|
// and shrinking the queue.
|
|
|
|
|
static void HistoQueuePopPair(HistoQueue* const histo_queue,
|
|
|
|
|
HistogramPair* const pair) {
|
|
|
|
|
assert(pair >= histo_queue->queue &&
|
|
|
|
|
pair < (histo_queue->queue + histo_queue->size));
|
|
|
|
|
assert(histo_queue->size > 0);
|
|
|
|
|
*pair = histo_queue->queue[histo_queue->size - 1];
|
|
|
|
|
--histo_queue->size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check whether a pair in the queue should be updated as head or not.
|
|
|
|
|
static void HistoQueueUpdateHead(HistoQueue* const histo_queue,
|
|
|
|
|
HistogramPair* const pair) {
|
|
|
|
|
assert(pair->cost_diff < 0.);
|
|
|
|
|
assert(pair >= histo_queue->queue &&
|
|
|
|
|
pair < (histo_queue->queue + histo_queue->size));
|
|
|
|
|
assert(histo_queue->size > 0);
|
|
|
|
|
if (pair->cost_diff < histo_queue->queue[0].cost_diff) {
|
|
|
|
|
// Replace the best pair.
|
|
|
|
|
const HistogramPair tmp = histo_queue->queue[0];
|
|
|
|
|
histo_queue->queue[0] = *pair;
|
|
|
|
|
*pair = tmp;
|
2014-09-26 15:26:56 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-22 14:57:29 +01:00
|
|
|
|
// Create a pair from indices "idx1" and "idx2" provided its cost
|
|
|
|
|
// is inferior to "threshold", a negative entropy.
|
|
|
|
|
// It returns the cost of the pair, or 0. if it superior to threshold.
|
|
|
|
|
static double HistoQueuePush(HistoQueue* const histo_queue,
|
|
|
|
|
VP8LHistogram** const histograms, int idx1,
|
|
|
|
|
int idx2, double threshold) {
|
|
|
|
|
const VP8LHistogram* h1;
|
|
|
|
|
const VP8LHistogram* h2;
|
|
|
|
|
HistogramPair pair;
|
2016-06-12 23:28:56 +02:00
|
|
|
|
double sum_cost;
|
|
|
|
|
|
2017-02-22 14:57:29 +01:00
|
|
|
|
assert(threshold <= 0.);
|
2014-09-26 15:26:56 +02:00
|
|
|
|
if (idx1 > idx2) {
|
|
|
|
|
const int tmp = idx2;
|
|
|
|
|
idx2 = idx1;
|
|
|
|
|
idx1 = tmp;
|
|
|
|
|
}
|
2017-02-22 14:57:29 +01:00
|
|
|
|
pair.idx1 = idx1;
|
|
|
|
|
pair.idx2 = idx2;
|
2016-06-12 23:28:56 +02:00
|
|
|
|
h1 = histograms[idx1];
|
|
|
|
|
h2 = histograms[idx2];
|
|
|
|
|
sum_cost = h1->bit_cost_ + h2->bit_cost_;
|
2017-02-22 14:57:29 +01:00
|
|
|
|
pair.cost_combo = 0.;
|
|
|
|
|
GetCombinedHistogramEntropy(h1, h2, sum_cost + threshold, &pair.cost_combo);
|
|
|
|
|
pair.cost_diff = pair.cost_combo - sum_cost;
|
|
|
|
|
|
|
|
|
|
// Do not even consider the pair if it does not improve the entropy.
|
|
|
|
|
if (pair.cost_diff >= threshold) return 0.;
|
|
|
|
|
|
|
|
|
|
// We cannot add more elements than the capacity.
|
|
|
|
|
assert(histo_queue->size < histo_queue->max_size);
|
|
|
|
|
histo_queue->queue[histo_queue->size++] = pair;
|
|
|
|
|
HistoQueueUpdateHead(histo_queue, &histo_queue->queue[histo_queue->size - 1]);
|
|
|
|
|
|
|
|
|
|
return pair.cost_diff;
|
2014-09-26 15:26:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-22 14:57:29 +01:00
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2014-09-26 15:26:56 +02:00
|
|
|
|
// Combines histograms by continuously choosing the one with the highest cost
|
|
|
|
|
// reduction.
|
2016-06-12 23:28:56 +02:00
|
|
|
|
static int HistogramCombineGreedy(VP8LHistogramSet* const image_histo) {
|
2014-09-26 15:26:56 +02:00
|
|
|
|
int ok = 0;
|
|
|
|
|
int image_histo_size = image_histo->size;
|
|
|
|
|
int i, j;
|
|
|
|
|
VP8LHistogram** const histograms = image_histo->histograms;
|
|
|
|
|
// Indexes of remaining histograms.
|
2016-10-05 09:39:08 +02:00
|
|
|
|
int* const clusters =
|
|
|
|
|
(int*)WebPSafeMalloc(image_histo_size, sizeof(*clusters));
|
2015-12-10 11:08:20 +01:00
|
|
|
|
// Priority queue of histogram pairs.
|
|
|
|
|
HistoQueue histo_queue;
|
2014-09-26 15:26:56 +02:00
|
|
|
|
|
2015-12-10 11:08:20 +01:00
|
|
|
|
if (!HistoQueueInit(&histo_queue, image_histo_size) || clusters == NULL) {
|
2014-09-26 15:26:56 +02:00
|
|
|
|
goto End;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < image_histo_size; ++i) {
|
|
|
|
|
// Initialize clusters indexes.
|
|
|
|
|
clusters[i] = i;
|
|
|
|
|
for (j = i + 1; j < image_histo_size; ++j) {
|
|
|
|
|
// Initialize positions array.
|
2017-02-22 14:57:29 +01:00
|
|
|
|
HistoQueuePush(&histo_queue, histograms, i, j, 0.);
|
2014-09-26 15:26:56 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-10 11:08:20 +01:00
|
|
|
|
while (image_histo_size > 1 && histo_queue.size > 0) {
|
|
|
|
|
const int idx1 = histo_queue.queue[0].idx1;
|
|
|
|
|
const int idx2 = histo_queue.queue[0].idx2;
|
2016-09-28 13:22:13 +02:00
|
|
|
|
HistogramAdd(histograms[idx2], histograms[idx1], histograms[idx1]);
|
2015-12-10 11:08:20 +01:00
|
|
|
|
histograms[idx1]->bit_cost_ = histo_queue.queue[0].cost_combo;
|
2014-09-26 15:26:56 +02:00
|
|
|
|
// Remove merged histogram.
|
|
|
|
|
for (i = 0; i + 1 < image_histo_size; ++i) {
|
|
|
|
|
if (clusters[i] >= idx2) {
|
|
|
|
|
clusters[i] = clusters[i + 1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
--image_histo_size;
|
|
|
|
|
|
2017-02-22 14:57:29 +01:00
|
|
|
|
// Remove pairs intersecting the just combined best pair.
|
|
|
|
|
for (i = 0; i < histo_queue.size;) {
|
2015-12-10 11:08:20 +01:00
|
|
|
|
HistogramPair* const p = histo_queue.queue + i;
|
|
|
|
|
if (p->idx1 == idx1 || p->idx2 == idx1 ||
|
|
|
|
|
p->idx1 == idx2 || p->idx2 == idx2) {
|
2017-02-22 14:57:29 +01:00
|
|
|
|
HistoQueuePopPair(&histo_queue, p);
|
|
|
|
|
} else {
|
|
|
|
|
HistoQueueUpdateHead(&histo_queue, p);
|
|
|
|
|
++i;
|
2015-12-10 11:08:20 +01:00
|
|
|
|
}
|
2014-09-26 15:26:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-10 11:08:20 +01:00
|
|
|
|
// Push new pairs formed with combined histogram to the queue.
|
2014-09-26 15:26:56 +02:00
|
|
|
|
for (i = 0; i < image_histo_size; ++i) {
|
|
|
|
|
if (clusters[i] != idx1) {
|
2017-02-22 14:57:29 +01:00
|
|
|
|
HistoQueuePush(&histo_queue, histograms, idx1, clusters[i], 0.);
|
2014-09-26 15:26:56 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Move remaining histograms to the beginning of the array.
|
|
|
|
|
for (i = 0; i < image_histo_size; ++i) {
|
2014-12-17 15:31:22 +01:00
|
|
|
|
if (i != clusters[i]) { // swap the two histograms
|
|
|
|
|
HistogramSwap(&histograms[i], &histograms[clusters[i]]);
|
2014-09-26 15:26:56 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
image_histo->size = image_histo_size;
|
|
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
|
|
End:
|
|
|
|
|
WebPSafeFree(clusters);
|
2015-12-10 11:08:20 +01:00
|
|
|
|
HistoQueueClear(&histo_queue);
|
2014-09-26 15:26:56 +02:00
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-06 21:04:03 +01:00
|
|
|
|
// Perform histogram aggregation using a stochastic approach.
|
|
|
|
|
// 'do_greedy' is set to 1 if a greedy approach needs to be performed
|
|
|
|
|
// afterwards, 0 otherwise.
|
2016-06-12 23:28:56 +02:00
|
|
|
|
static void HistogramCombineStochastic(VP8LHistogramSet* const image_histo,
|
|
|
|
|
VP8LHistogram* tmp_histo,
|
2017-02-17 20:31:24 +01:00
|
|
|
|
VP8LHistogram* best_combo,
|
2017-02-06 21:04:03 +01:00
|
|
|
|
int min_cluster_size, int* do_greedy) {
|
2014-03-13 19:34:12 +01:00
|
|
|
|
int iter;
|
2017-02-17 20:31:24 +01:00
|
|
|
|
uint32_t seed = 1;
|
2012-04-03 16:24:25 +02:00
|
|
|
|
int tries_with_no_success = 0;
|
2014-05-08 23:13:07 +02:00
|
|
|
|
int image_histo_size = image_histo->size;
|
2017-02-17 20:31:24 +01:00
|
|
|
|
const int outer_iters = image_histo_size;
|
2014-03-13 19:34:12 +01:00
|
|
|
|
const int num_tries_no_success = outer_iters / 2;
|
2014-05-08 23:13:07 +02:00
|
|
|
|
VP8LHistogram** const histograms = image_histo->histograms;
|
2012-04-27 21:37:50 +02:00
|
|
|
|
|
2014-05-08 23:13:07 +02:00
|
|
|
|
// Collapse similar histograms in 'image_histo'.
|
2017-02-06 21:04:03 +01:00
|
|
|
|
*do_greedy = (image_histo->size <= min_cluster_size);
|
2014-09-26 15:26:56 +02:00
|
|
|
|
++min_cluster_size;
|
2017-02-06 21:04:03 +01:00
|
|
|
|
for (iter = 0; iter < outer_iters && image_histo_size >= min_cluster_size &&
|
|
|
|
|
++tries_with_no_success < num_tries_no_success;
|
2014-03-13 19:34:12 +01:00
|
|
|
|
++iter) {
|
2012-04-30 14:18:50 +02:00
|
|
|
|
double best_cost_diff = 0.;
|
2013-03-18 22:34:32 +01:00
|
|
|
|
int best_idx1 = -1, best_idx2 = 1;
|
2012-04-27 21:37:50 +02:00
|
|
|
|
int j;
|
2017-02-17 20:31:24 +01:00
|
|
|
|
const uint32_t rand_range = (image_histo_size - 1) * image_histo_size;
|
|
|
|
|
// 6/10 was chosen empirically.
|
|
|
|
|
// TODO(vrabaud): use less magic constants in that code.
|
|
|
|
|
const int num_tries = (6 * image_histo_size) / 10;
|
|
|
|
|
|
2017-02-06 21:04:03 +01:00
|
|
|
|
// If the stochastic method has not worked for a while (10 iterations) and
|
|
|
|
|
// if it requires less iterations to finish off with a greedy approach, go
|
|
|
|
|
// for it.
|
|
|
|
|
// With the greedy approach, each histogram is compared to the other ones,
|
|
|
|
|
// hence (image_histo_size-1)*image_histo_size/2 overall comparisons.
|
|
|
|
|
// Then, at each iteration, the best pair is merged and compared to all
|
|
|
|
|
// the other ones, adding (image_histo_size-2)*(image_histo_size-1)/2 more
|
|
|
|
|
// comparisons. Overall: (image_histo_size-1)^2 comparisons.
|
|
|
|
|
*do_greedy |= (tries_with_no_success > 10) &&
|
|
|
|
|
((image_histo_size - 1) * (image_histo_size - 1) <
|
|
|
|
|
num_tries * (outer_iters - iter));
|
|
|
|
|
if (*do_greedy) break;
|
2016-08-19 14:32:56 +02:00
|
|
|
|
|
2012-11-06 01:44:19 +01:00
|
|
|
|
for (j = 0; j < num_tries; ++j) {
|
2012-04-27 21:37:50 +02:00
|
|
|
|
double curr_cost_diff;
|
2017-02-17 20:31:24 +01:00
|
|
|
|
// Choose two different histograms at random and try to combine them.
|
|
|
|
|
const uint32_t tmp = MyRand(&seed) % rand_range;
|
|
|
|
|
const uint32_t idx1 = tmp / (image_histo_size - 1);
|
|
|
|
|
uint32_t idx2 = tmp % (image_histo_size - 1);
|
|
|
|
|
if (idx2 >= idx1) ++idx2;
|
2014-03-13 19:34:12 +01:00
|
|
|
|
|
2012-04-27 21:37:50 +02:00
|
|
|
|
// Calculate cost reduction on combining.
|
2014-05-08 23:13:07 +02:00
|
|
|
|
curr_cost_diff = HistogramAddEval(histograms[idx1], histograms[idx2],
|
2014-12-17 15:31:22 +01:00
|
|
|
|
tmp_histo, best_cost_diff);
|
2016-08-19 14:32:56 +02:00
|
|
|
|
if (curr_cost_diff < best_cost_diff) { // found a better pair?
|
2014-12-17 15:31:22 +01:00
|
|
|
|
HistogramSwap(&best_combo, &tmp_histo);
|
2012-04-27 21:37:50 +02:00
|
|
|
|
best_cost_diff = curr_cost_diff;
|
|
|
|
|
best_idx1 = idx1;
|
|
|
|
|
best_idx2 = idx2;
|
2012-04-03 16:24:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-04-27 21:37:50 +02:00
|
|
|
|
|
2013-03-18 22:34:32 +01:00
|
|
|
|
if (best_idx1 >= 0) {
|
2014-12-17 15:31:22 +01:00
|
|
|
|
HistogramSwap(&best_combo, &histograms[best_idx1]);
|
2012-04-30 14:18:50 +02:00
|
|
|
|
// swap best_idx2 slot with last one (which is now unused)
|
2014-05-08 23:13:07 +02:00
|
|
|
|
--image_histo_size;
|
|
|
|
|
if (best_idx2 != image_histo_size) {
|
2014-12-17 15:31:22 +01:00
|
|
|
|
HistogramSwap(&histograms[image_histo_size], &histograms[best_idx2]);
|
2014-05-08 23:13:07 +02:00
|
|
|
|
histograms[image_histo_size] = NULL;
|
2012-04-30 14:18:50 +02:00
|
|
|
|
}
|
2012-04-03 16:24:25 +02:00
|
|
|
|
tries_with_no_success = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-08 23:13:07 +02:00
|
|
|
|
image_histo->size = image_histo_size;
|
2017-02-06 21:04:03 +01:00
|
|
|
|
*do_greedy |= (image_histo->size <= min_cluster_size);
|
2012-04-03 16:24:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-30 14:18:50 +02:00
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Histogram refinement
|
|
|
|
|
|
2012-04-30 21:46:22 +02:00
|
|
|
|
// Find the best 'out' histogram for each of the 'in' histograms.
|
2012-04-30 14:18:50 +02:00
|
|
|
|
// Note: we assume that out[]->bit_cost_ is already up-to-date.
|
2016-06-08 13:36:04 +02:00
|
|
|
|
static void HistogramRemap(const VP8LHistogramSet* const in,
|
|
|
|
|
const VP8LHistogramSet* const out,
|
2012-04-30 21:46:22 +02:00
|
|
|
|
uint16_t* const symbols) {
|
2012-04-03 16:24:25 +02:00
|
|
|
|
int i;
|
2016-06-08 13:36:04 +02:00
|
|
|
|
VP8LHistogram** const in_histo = in->histograms;
|
|
|
|
|
VP8LHistogram** const out_histo = out->histograms;
|
|
|
|
|
const int in_size = in->size;
|
|
|
|
|
const int out_size = out->size;
|
|
|
|
|
if (out_size > 1) {
|
|
|
|
|
for (i = 0; i < in_size; ++i) {
|
2014-09-04 01:58:49 +02:00
|
|
|
|
int best_out = 0;
|
2016-06-08 13:36:04 +02:00
|
|
|
|
double best_bits = MAX_COST;
|
2014-09-04 01:58:49 +02:00
|
|
|
|
int k;
|
2016-06-08 13:36:04 +02:00
|
|
|
|
for (k = 0; k < out_size; ++k) {
|
2014-09-04 01:58:49 +02:00
|
|
|
|
const double cur_bits =
|
2016-06-08 13:36:04 +02:00
|
|
|
|
HistogramAddThresh(out_histo[k], in_histo[i], best_bits);
|
|
|
|
|
if (k == 0 || cur_bits < best_bits) {
|
2014-09-04 01:58:49 +02:00
|
|
|
|
best_bits = cur_bits;
|
|
|
|
|
best_out = k;
|
|
|
|
|
}
|
2012-04-03 16:24:25 +02:00
|
|
|
|
}
|
2014-09-04 01:58:49 +02:00
|
|
|
|
symbols[i] = best_out;
|
2012-04-03 16:24:25 +02:00
|
|
|
|
}
|
2014-08-27 00:44:04 +02:00
|
|
|
|
} else {
|
2016-06-08 13:36:04 +02:00
|
|
|
|
assert(out_size == 1);
|
|
|
|
|
for (i = 0; i < in_size; ++i) {
|
2014-08-27 00:44:04 +02:00
|
|
|
|
symbols[i] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-04-03 16:24:25 +02:00
|
|
|
|
|
|
|
|
|
// Recompute each out based on raw and symbols.
|
2016-06-08 13:36:04 +02:00
|
|
|
|
for (i = 0; i < out_size; ++i) {
|
|
|
|
|
HistogramClear(out_histo[i]);
|
2012-04-03 16:24:25 +02:00
|
|
|
|
}
|
2014-03-13 19:34:12 +01:00
|
|
|
|
|
2016-06-08 13:36:04 +02:00
|
|
|
|
for (i = 0; i < in_size; ++i) {
|
2014-05-08 23:13:07 +02:00
|
|
|
|
const int idx = symbols[i];
|
2016-09-28 13:22:13 +02:00
|
|
|
|
HistogramAdd(in_histo[i], out_histo[idx], out_histo[idx]);
|
2012-04-03 16:24:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-04-11 11:52:13 +02:00
|
|
|
|
|
2014-03-28 15:06:54 +01:00
|
|
|
|
static double GetCombineCostFactor(int histo_size, int quality) {
|
|
|
|
|
double combine_cost_factor = 0.16;
|
2014-10-29 22:52:43 +01:00
|
|
|
|
if (quality < 90) {
|
|
|
|
|
if (histo_size > 256) combine_cost_factor /= 2.;
|
|
|
|
|
if (histo_size > 512) combine_cost_factor /= 2.;
|
|
|
|
|
if (histo_size > 1024) combine_cost_factor /= 2.;
|
|
|
|
|
if (quality <= 50) combine_cost_factor /= 2.;
|
|
|
|
|
}
|
2014-03-28 15:06:54 +01:00
|
|
|
|
return combine_cost_factor;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-30 14:18:50 +02:00
|
|
|
|
int VP8LGetHistoImageSymbols(int xsize, int ysize,
|
|
|
|
|
const VP8LBackwardRefs* const refs,
|
2014-12-19 18:33:17 +01:00
|
|
|
|
int quality, int low_effort,
|
|
|
|
|
int histo_bits, int cache_bits,
|
2014-05-08 23:13:07 +02:00
|
|
|
|
VP8LHistogramSet* const image_histo,
|
2014-12-17 15:31:22 +01:00
|
|
|
|
VP8LHistogramSet* const tmp_histos,
|
2012-04-30 14:18:50 +02:00
|
|
|
|
uint16_t* const histogram_symbols) {
|
2012-04-26 11:55:14 +02:00
|
|
|
|
int ok = 0;
|
2012-04-27 21:37:50 +02:00
|
|
|
|
const int histo_xsize = histo_bits ? VP8LSubSampleSize(xsize, histo_bits) : 1;
|
|
|
|
|
const int histo_ysize = histo_bits ? VP8LSubSampleSize(ysize, histo_bits) : 1;
|
2014-05-08 23:13:07 +02:00
|
|
|
|
const int image_histo_raw_size = histo_xsize * histo_ysize;
|
|
|
|
|
VP8LHistogramSet* const orig_histo =
|
|
|
|
|
VP8LAllocateHistogramSet(image_histo_raw_size, cache_bits);
|
2014-12-17 15:31:22 +01:00
|
|
|
|
VP8LHistogram* cur_combo;
|
2016-06-17 09:52:36 +02:00
|
|
|
|
// Don't attempt linear bin-partition heuristic for
|
|
|
|
|
// histograms of small sizes (as bin_map will be very sparse) and
|
|
|
|
|
// maximum quality q==100 (to preserve the compression gains at that level).
|
2016-06-20 17:58:04 +02:00
|
|
|
|
const int entropy_combine_num_bins = low_effort ? NUM_PARTITIONS : BIN_SIZE;
|
2014-12-19 18:33:17 +01:00
|
|
|
|
const int entropy_combine =
|
|
|
|
|
(orig_histo->size > entropy_combine_num_bins * 2) && (quality < 100);
|
2012-04-26 11:55:14 +02:00
|
|
|
|
|
2014-12-17 15:31:22 +01:00
|
|
|
|
if (orig_histo == NULL) goto Error;
|
2014-03-13 19:34:12 +01:00
|
|
|
|
|
2014-05-08 23:13:07 +02:00
|
|
|
|
// Construct the histograms from backward references.
|
|
|
|
|
HistogramBuild(xsize, histo_bits, refs, orig_histo);
|
|
|
|
|
// Copies the histograms and computes its bit_cost.
|
|
|
|
|
HistogramCopyAndAnalyze(orig_histo, image_histo);
|
2014-03-13 19:34:12 +01:00
|
|
|
|
|
2014-12-17 15:31:22 +01:00
|
|
|
|
cur_combo = tmp_histos->histograms[1]; // pick up working slot
|
2014-12-19 18:33:17 +01:00
|
|
|
|
if (entropy_combine) {
|
2016-06-17 09:52:36 +02:00
|
|
|
|
const int bin_map_size = orig_histo->size;
|
2016-06-20 17:58:04 +02:00
|
|
|
|
// Reuse histogram_symbols storage. By definition, it's guaranteed to be ok.
|
2016-06-20 11:19:25 +02:00
|
|
|
|
uint16_t* const bin_map = histogram_symbols;
|
2014-03-28 15:06:54 +01:00
|
|
|
|
const double combine_cost_factor =
|
2014-05-08 23:13:07 +02:00
|
|
|
|
GetCombineCostFactor(image_histo_raw_size, quality);
|
2016-06-17 09:52:36 +02:00
|
|
|
|
|
2014-12-19 18:33:17 +01:00
|
|
|
|
HistogramAnalyzeEntropyBin(orig_histo, bin_map, low_effort);
|
2014-05-08 23:13:07 +02:00
|
|
|
|
// Collapse histograms with similar entropy.
|
2016-06-17 09:52:36 +02:00
|
|
|
|
cur_combo = HistogramCombineEntropyBin(image_histo, cur_combo,
|
|
|
|
|
bin_map, bin_map_size,
|
|
|
|
|
entropy_combine_num_bins,
|
2014-12-19 18:33:17 +01:00
|
|
|
|
combine_cost_factor, low_effort);
|
2014-03-13 19:34:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-19 18:33:17 +01:00
|
|
|
|
// Don't combine the histograms using stochastic and greedy heuristics for
|
|
|
|
|
// low-effort compression mode.
|
|
|
|
|
if (!low_effort || !entropy_combine) {
|
2014-09-26 15:26:56 +02:00
|
|
|
|
const float x = quality / 100.f;
|
2014-09-27 09:36:01 +02:00
|
|
|
|
// cubic ramp between 1 and MAX_HISTO_GREEDY:
|
|
|
|
|
const int threshold_size = (int)(1 + (x * x * x) * (MAX_HISTO_GREEDY - 1));
|
2017-02-06 21:04:03 +01:00
|
|
|
|
int do_greedy;
|
2016-06-12 23:28:56 +02:00
|
|
|
|
HistogramCombineStochastic(image_histo, tmp_histos->histograms[0],
|
2017-02-17 20:31:24 +01:00
|
|
|
|
cur_combo, threshold_size, &do_greedy);
|
2017-02-06 21:04:03 +01:00
|
|
|
|
if (do_greedy && !HistogramCombineGreedy(image_histo)) {
|
2014-09-26 15:26:56 +02:00
|
|
|
|
goto Error;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-13 19:34:12 +01:00
|
|
|
|
|
2014-12-19 18:33:17 +01:00
|
|
|
|
// TODO(vikasa): Optimize HistogramRemap for low-effort compression mode also.
|
2012-04-30 21:46:22 +02:00
|
|
|
|
// Find the optimal map from original histograms to the final ones.
|
2014-05-08 23:13:07 +02:00
|
|
|
|
HistogramRemap(orig_histo, image_histo, histogram_symbols);
|
2014-03-13 19:34:12 +01:00
|
|
|
|
|
2012-04-26 11:55:14 +02:00
|
|
|
|
ok = 1;
|
|
|
|
|
|
2014-03-13 21:56:30 +01:00
|
|
|
|
Error:
|
2014-05-08 23:13:07 +02:00
|
|
|
|
VP8LFreeHistogramSet(orig_histo);
|
2012-04-26 11:55:14 +02:00
|
|
|
|
return ok;
|
|
|
|
|
}
|