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
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2012-04-03 16:24:25 +02:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "./backward_references.h"
|
|
|
|
#include "./histogram.h"
|
2012-04-10 09:00:36 +02:00
|
|
|
#include "../dsp/lossless.h"
|
2012-08-01 21:06:04 +02:00
|
|
|
#include "../utils/utils.h"
|
2012-04-03 16:24:25 +02:00
|
|
|
|
2012-04-30 21:46:22 +02:00
|
|
|
static void HistogramClear(VP8LHistogram* const p) {
|
|
|
|
memset(p->literal_, 0, sizeof(p->literal_));
|
|
|
|
memset(p->red_, 0, sizeof(p->red_));
|
|
|
|
memset(p->blue_, 0, sizeof(p->blue_));
|
|
|
|
memset(p->alpha_, 0, sizeof(p->alpha_));
|
|
|
|
memset(p->distance_, 0, sizeof(p->distance_));
|
|
|
|
p->bit_cost_ = 0;
|
|
|
|
}
|
|
|
|
|
2012-06-08 20:52:31 +02:00
|
|
|
void VP8LHistogramStoreRefs(const VP8LBackwardRefs* const refs,
|
|
|
|
VP8LHistogram* const histo) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < refs->size; ++i) {
|
|
|
|
VP8LHistogramAddSinglePixOrCopy(histo, &refs->refs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
VP8LHistogram* bulk;
|
2012-10-03 21:09:38 +02:00
|
|
|
const uint64_t total_size = sizeof(*set)
|
|
|
|
+ (uint64_t)size * sizeof(*set->histograms)
|
|
|
|
+ (uint64_t)size * sizeof(**set->histograms);
|
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);
|
|
|
|
bulk = (VP8LHistogram*)memory;
|
|
|
|
set->max_size = size;
|
|
|
|
set->size = size;
|
2012-04-30 14:18:50 +02:00
|
|
|
for (i = 0; i < size; ++i) {
|
2012-04-30 21:46:22 +02:00
|
|
|
set->histograms[i] = bulk + i;
|
|
|
|
VP8LHistogramInit(set->histograms[i], cache_bits);
|
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)) {
|
2012-05-24 14:15:05 +02:00
|
|
|
int literal_ix = 256 + 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);
|
2012-06-08 20:52:31 +02:00
|
|
|
++histo->literal_[256 + 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-03 22:49:54 +01:00
|
|
|
static WEBP_INLINE double BitsEntropyRefine(int nonzeros, int sum, int max_val,
|
|
|
|
double retval) {
|
2012-04-03 16:24:25 +02:00
|
|
|
double mix;
|
|
|
|
if (nonzeros < 5) {
|
|
|
|
if (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 (nonzeros == 2) {
|
|
|
|
return 0.99 * sum + 0.01 * retval;
|
|
|
|
}
|
|
|
|
// 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 (nonzeros == 3) {
|
|
|
|
mix = 0.95;
|
|
|
|
} else {
|
|
|
|
mix = 0.7; // nonzeros == 4.
|
|
|
|
}
|
2012-08-02 03:22:06 +02:00
|
|
|
} else {
|
|
|
|
mix = 0.627;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-08-02 03:22:06 +02:00
|
|
|
|
2012-04-03 16:24:25 +02:00
|
|
|
{
|
|
|
|
double min_limit = 2 * sum - max_val;
|
|
|
|
min_limit = mix * min_limit + (1.0 - mix) * retval;
|
2012-08-02 03:22:06 +02:00
|
|
|
return (retval < min_limit) ? min_limit : retval;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-03 22:49:54 +01:00
|
|
|
static double BitsEntropy(const int* const array, int n) {
|
|
|
|
double retval = 0.;
|
|
|
|
int sum = 0;
|
|
|
|
int nonzeros = 0;
|
|
|
|
int max_val = 0;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
if (array[i] != 0) {
|
|
|
|
sum += array[i];
|
|
|
|
++nonzeros;
|
|
|
|
retval -= VP8LFastSLog2(array[i]);
|
|
|
|
if (max_val < array[i]) {
|
|
|
|
max_val = array[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
retval += VP8LFastSLog2(sum);
|
|
|
|
return BitsEntropyRefine(nonzeros, sum, max_val, retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double BitsEntropyCombined(const int* const X, const int* const Y,
|
|
|
|
int n) {
|
|
|
|
double retval = 0.;
|
|
|
|
int sum = 0;
|
|
|
|
int nonzeros = 0;
|
|
|
|
int max_val = 0;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
const int xy = X[i] + Y[i];
|
|
|
|
if (xy != 0) {
|
|
|
|
sum += xy;
|
|
|
|
++nonzeros;
|
|
|
|
retval -= VP8LFastSLog2(xy);
|
|
|
|
if (max_val < xy) {
|
|
|
|
max_val = xy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
retval += VP8LFastSLog2(sum);
|
|
|
|
return BitsEntropyRefine(nonzeros, sum, max_val, retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static WEBP_INLINE double InitialHuffmanCost(void) {
|
2012-04-03 16:24:25 +02:00
|
|
|
// 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;
|
2014-03-03 22:49:54 +01:00
|
|
|
return kHuffmanCodeOfHuffmanCodeSize - kSmallBias;
|
2014-03-04 09:38:14 +01:00
|
|
|
}
|
2014-03-03 22:49:54 +01:00
|
|
|
|
|
|
|
static WEBP_INLINE double HuffmanCostRefine(int streak, int val) {
|
|
|
|
double retval;
|
2014-03-04 09:38:14 +01:00
|
|
|
if (streak > 3) {
|
2014-03-03 22:49:54 +01:00
|
|
|
if (val == 0) {
|
|
|
|
retval = 1.5625 + 0.234375 * streak;
|
2012-04-03 16:24:25 +02:00
|
|
|
} else {
|
2014-03-04 09:38:14 +01:00
|
|
|
retval = 2.578125 + 0.703125 * streak;
|
|
|
|
}
|
|
|
|
} else {
|
2014-03-03 22:49:54 +01:00
|
|
|
if (val == 0) {
|
|
|
|
retval = 1.796875 * streak;
|
2014-03-04 09:38:14 +01:00
|
|
|
} else {
|
2014-03-03 22:49:54 +01:00
|
|
|
retval = 3.28125 * streak;
|
|
|
|
}
|
2014-03-04 09:38:14 +01:00
|
|
|
}
|
2014-03-03 22:49:54 +01:00
|
|
|
return retval;
|
2014-03-04 09:38:14 +01:00
|
|
|
}
|
2014-03-03 22:49:54 +01:00
|
|
|
|
|
|
|
// Returns the cost encode the rle-encoded entropy code.
|
|
|
|
// The constants in this function are experimental.
|
|
|
|
static double HuffmanCost(const int* const population, int length) {
|
|
|
|
int streak = 0;
|
|
|
|
int i = 0;
|
|
|
|
double retval = InitialHuffmanCost();
|
|
|
|
for (; i < length - 1; ++i) {
|
|
|
|
++streak;
|
|
|
|
if (population[i] == population[i + 1]) {
|
|
|
|
continue;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2014-03-03 22:49:54 +01:00
|
|
|
retval += HuffmanCostRefine(streak, population[i]);
|
2012-04-03 16:24:25 +02:00
|
|
|
streak = 0;
|
|
|
|
}
|
2014-03-03 22:49:54 +01:00
|
|
|
retval += HuffmanCostRefine(++streak, population[i]);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double HuffmanCostCombined(const int* const X, const int* const Y,
|
|
|
|
int length) {
|
|
|
|
int streak = 0;
|
|
|
|
int i = 0;
|
|
|
|
double retval = InitialHuffmanCost();
|
|
|
|
for (; i < length - 1; ++i) {
|
|
|
|
const int xy = X[i] + Y[i];
|
|
|
|
const int xy_next = X[i + 1] + Y[i + 1];
|
2012-04-03 16:24:25 +02:00
|
|
|
++streak;
|
2014-03-03 22:49:54 +01:00
|
|
|
if (xy == xy_next) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
retval += HuffmanCostRefine(streak, xy);
|
|
|
|
streak = 0;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2014-03-03 22:49:54 +01:00
|
|
|
retval += HuffmanCostRefine(++streak, X[i] + Y[i]);
|
2012-04-03 16:24:25 +02:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2013-03-18 22:34:32 +01:00
|
|
|
static double PopulationCost(const int* const population, int length) {
|
|
|
|
return BitsEntropy(population, length) + HuffmanCost(population, length);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
2014-03-03 22:49:54 +01:00
|
|
|
static double GetCombinedEntropy(const int* const X, const int* const Y,
|
|
|
|
int length) {
|
|
|
|
return BitsEntropyCombined(X, Y, length) + HuffmanCostCombined(X, Y, length);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-18 22:34:32 +01:00
|
|
|
static double ExtraCost(const int* const population, int length) {
|
|
|
|
int i;
|
|
|
|
double cost = 0.;
|
|
|
|
for (i = 2; i < length - 2; ++i) cost += (i >> 1) * population[i + 2];
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2014-03-03 22:49:54 +01:00
|
|
|
static double ExtraCostCombined(const int* const X, const int* const Y,
|
|
|
|
int length) {
|
|
|
|
int i;
|
|
|
|
double cost = 0.;
|
|
|
|
for (i = 2; i < length - 2; ++i) {
|
|
|
|
const int xy = X[i + 2] + Y[i + 2];
|
|
|
|
cost += (i >> 1) * xy;
|
|
|
|
}
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2013-03-18 22:34:32 +01:00
|
|
|
// Estimates the Entropy + Huffman + other block overhead size cost.
|
2012-07-17 04:13:01 +02:00
|
|
|
double VP8LHistogramEstimateBits(const VP8LHistogram* const p) {
|
2014-03-03 22:49:54 +01:00
|
|
|
return
|
|
|
|
PopulationCost(p->literal_, VP8LHistogramNumCodes(p->palette_code_bits_))
|
2014-03-04 09:38:14 +01:00
|
|
|
+ PopulationCost(p->red_, 256)
|
|
|
|
+ PopulationCost(p->blue_, 256)
|
|
|
|
+ PopulationCost(p->alpha_, 256)
|
|
|
|
+ PopulationCost(p->distance_, NUM_DISTANCE_CODES)
|
|
|
|
+ ExtraCost(p->literal_ + 256, NUM_LENGTH_CODES)
|
|
|
|
+ ExtraCost(p->distance_, NUM_DISTANCE_CODES);
|
2013-03-18 22:34:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
double VP8LHistogramEstimateBitsBulk(const VP8LHistogram* const p) {
|
2014-03-03 22:49:54 +01:00
|
|
|
return
|
|
|
|
BitsEntropy(p->literal_, VP8LHistogramNumCodes(p->palette_code_bits_))
|
2014-03-04 09:38:14 +01:00
|
|
|
+ BitsEntropy(p->red_, 256)
|
|
|
|
+ BitsEntropy(p->blue_, 256)
|
|
|
|
+ BitsEntropy(p->alpha_, 256)
|
|
|
|
+ BitsEntropy(p->distance_, NUM_DISTANCE_CODES)
|
|
|
|
+ ExtraCost(p->literal_ + 256, NUM_LENGTH_CODES)
|
|
|
|
+ ExtraCost(p->distance_, NUM_DISTANCE_CODES);
|
2013-03-18 22:34:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Various histogram combine/cost-eval functions
|
|
|
|
|
|
|
|
// Adds 'in' histogram to 'out'
|
|
|
|
static void HistogramAdd(const VP8LHistogram* const in,
|
|
|
|
VP8LHistogram* const out) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < PIX_OR_COPY_CODES_MAX; ++i) {
|
|
|
|
out->literal_[i] += in->literal_[i];
|
|
|
|
}
|
|
|
|
for (i = 0; i < NUM_DISTANCE_CODES; ++i) {
|
|
|
|
out->distance_[i] += in->distance_[i];
|
|
|
|
}
|
|
|
|
for (i = 0; i < 256; ++i) {
|
|
|
|
out->red_[i] += in->red_[i];
|
|
|
|
out->blue_[i] += in->blue_[i];
|
|
|
|
out->alpha_[i] += in->alpha_[i];
|
|
|
|
}
|
2012-07-17 04:13:01 +02:00
|
|
|
}
|
|
|
|
|
2014-03-03 22:49:54 +01:00
|
|
|
static int GetCombinedHistogramEntropy(const VP8LHistogram* const a,
|
|
|
|
const VP8LHistogram* const b,
|
|
|
|
double cost_threshold,
|
|
|
|
double* cost) {
|
|
|
|
const int palette_code_bits =
|
|
|
|
(a->palette_code_bits_ > b->palette_code_bits_) ? a->palette_code_bits_ :
|
|
|
|
b->palette_code_bits_;
|
|
|
|
*cost += GetCombinedEntropy(a->literal_, b->literal_,
|
|
|
|
VP8LHistogramNumCodes(palette_code_bits));
|
|
|
|
*cost += ExtraCostCombined(a->literal_ + 256, b->literal_ + 256,
|
|
|
|
NUM_LENGTH_CODES);
|
|
|
|
if (*cost > cost_threshold) return 0;
|
|
|
|
|
|
|
|
*cost += GetCombinedEntropy(a->red_, b->red_, 256);
|
|
|
|
if (*cost > cost_threshold) return 0;
|
|
|
|
|
|
|
|
*cost += GetCombinedEntropy(a->blue_, b->blue_, 256);
|
|
|
|
if (*cost > cost_threshold) return 0;
|
|
|
|
|
|
|
|
*cost += GetCombinedEntropy(a->distance_, b->distance_, NUM_DISTANCE_CODES);
|
|
|
|
*cost += ExtraCostCombined(a->distance_, b->distance_, NUM_DISTANCE_CODES);
|
|
|
|
if (*cost > cost_threshold) return 0;
|
|
|
|
|
|
|
|
*cost += GetCombinedEntropy(a->alpha_, b->alpha_, 256);
|
|
|
|
if (*cost > cost_threshold) return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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_;
|
|
|
|
int i;
|
|
|
|
cost_threshold += sum_cost;
|
|
|
|
|
2014-03-03 22:49:54 +01:00
|
|
|
if (GetCombinedHistogramEntropy(a, b, cost_threshold, &cost)) {
|
2014-03-04 09:38:14 +01:00
|
|
|
for (i = 0; i < PIX_OR_COPY_CODES_MAX; ++i) {
|
|
|
|
out->literal_[i] = a->literal_[i] + b->literal_[i];
|
|
|
|
}
|
|
|
|
for (i = 0; i < NUM_DISTANCE_CODES; ++i) {
|
|
|
|
out->distance_[i] = a->distance_[i] + b->distance_[i];
|
|
|
|
}
|
2014-03-03 22:49:54 +01:00
|
|
|
for (i = 0; i < 256; ++i) {
|
|
|
|
out->red_[i] = a->red_[i] + b->red_[i];
|
|
|
|
out->blue_[i] = a->blue_[i] + b->blue_[i];
|
|
|
|
out->alpha_[i] = a->alpha_[i] + b->alpha_[i];
|
|
|
|
}
|
|
|
|
out->palette_code_bits_ = (a->palette_code_bits_ > b->palette_code_bits_) ?
|
|
|
|
a->palette_code_bits_ : b->palette_code_bits_;
|
2014-03-04 09:38:14 +01:00
|
|
|
out->bit_cost_ = cost;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2012-04-30 14:18:50 +02:00
|
|
|
static void HistogramBuildImage(int xsize, int histo_bits,
|
|
|
|
const VP8LBackwardRefs* const backward_refs,
|
2012-04-30 21:46:22 +02:00
|
|
|
VP8LHistogramSet* const image) {
|
2012-04-03 16:24:25 +02:00
|
|
|
int i;
|
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);
|
|
|
|
VP8LHistogram** const histograms = image->histograms;
|
|
|
|
assert(histo_bits > 0);
|
2012-04-27 10:04:57 +02:00
|
|
|
for (i = 0; i < backward_refs->size; ++i) {
|
2012-04-30 14:18:50 +02:00
|
|
|
const PixOrCopy* const v = &backward_refs->refs[i];
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-22 09:13:01 +02:00
|
|
|
static uint32_t MyRand(uint32_t *seed) {
|
|
|
|
*seed *= 16807U;
|
|
|
|
if (*seed == 0) {
|
|
|
|
*seed = 1;
|
|
|
|
}
|
|
|
|
return *seed;
|
|
|
|
}
|
|
|
|
|
2012-04-30 21:46:22 +02:00
|
|
|
static int HistogramCombine(const VP8LHistogramSet* const in,
|
2012-11-06 01:44:19 +01:00
|
|
|
VP8LHistogramSet* const out, int iter_mult,
|
2012-10-17 00:42:15 +02:00
|
|
|
int num_pairs, int num_tries_no_success) {
|
2012-04-03 16:24:25 +02:00
|
|
|
int ok = 0;
|
2012-04-30 14:18:50 +02:00
|
|
|
int i, iter;
|
2012-05-22 09:13:01 +02:00
|
|
|
uint32_t seed = 0;
|
2012-04-03 16:24:25 +02:00
|
|
|
int tries_with_no_success = 0;
|
2012-04-30 21:46:22 +02:00
|
|
|
int out_size = in->size;
|
2012-10-17 00:42:15 +02:00
|
|
|
const int outer_iters = in->size * iter_mult;
|
2012-11-06 01:44:19 +01:00
|
|
|
const int min_cluster_size = 2;
|
2012-04-30 14:18:50 +02:00
|
|
|
VP8LHistogram* const histos = (VP8LHistogram*)malloc(2 * sizeof(*histos));
|
|
|
|
VP8LHistogram* cur_combo = histos + 0; // trial merged histogram
|
|
|
|
VP8LHistogram* best_combo = histos + 1; // best merged histogram so far
|
|
|
|
if (histos == NULL) goto End;
|
2012-04-27 21:37:50 +02:00
|
|
|
|
2012-04-30 14:18:50 +02:00
|
|
|
// Copy histograms from in[] to out[].
|
2012-04-30 21:46:22 +02:00
|
|
|
assert(in->size <= out->size);
|
|
|
|
for (i = 0; i < in->size; ++i) {
|
|
|
|
in->histograms[i]->bit_cost_ = VP8LHistogramEstimateBits(in->histograms[i]);
|
|
|
|
*out->histograms[i] = *in->histograms[i];
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-04-27 21:37:50 +02:00
|
|
|
|
|
|
|
// Collapse similar histograms in 'out'.
|
2012-04-30 14:18:50 +02:00
|
|
|
for (iter = 0; iter < outer_iters && out_size >= min_cluster_size; ++iter) {
|
|
|
|
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;
|
2012-11-06 01:44:19 +01:00
|
|
|
const int num_tries = (num_pairs < out_size) ? num_pairs : out_size;
|
2012-05-22 09:13:01 +02:00
|
|
|
seed += iter;
|
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;
|
|
|
|
// Choose two histograms at random and try to combine them.
|
2012-05-22 09:13:01 +02:00
|
|
|
const uint32_t idx1 = MyRand(&seed) % out_size;
|
2013-03-18 22:34:32 +01:00
|
|
|
const uint32_t tmp = (j & 7) + 1;
|
2012-05-22 09:13:01 +02:00
|
|
|
const uint32_t diff = (tmp < 3) ? tmp : MyRand(&seed) % (out_size - 1);
|
|
|
|
const uint32_t idx2 = (idx1 + diff + 1) % out_size;
|
2012-04-27 21:37:50 +02:00
|
|
|
if (idx1 == idx2) {
|
2012-04-03 16:24:25 +02:00
|
|
|
continue;
|
|
|
|
}
|
2012-04-27 21:37:50 +02:00
|
|
|
// Calculate cost reduction on combining.
|
2013-03-18 22:34:32 +01:00
|
|
|
curr_cost_diff = HistogramAddEval(out->histograms[idx1],
|
|
|
|
out->histograms[idx2],
|
|
|
|
cur_combo, best_cost_diff);
|
|
|
|
if (curr_cost_diff < best_cost_diff) { // found a better pair?
|
2012-04-30 14:18:50 +02:00
|
|
|
{ // swap cur/best combo histograms
|
2012-05-15 22:17:34 +02:00
|
|
|
VP8LHistogram* const tmp_histo = cur_combo;
|
2012-04-30 14:18:50 +02:00
|
|
|
cur_combo = best_combo;
|
2012-05-15 22:17:34 +02:00
|
|
|
best_combo = tmp_histo;
|
2012-04-30 14:18:50 +02:00
|
|
|
}
|
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) {
|
2012-04-30 21:46:22 +02:00
|
|
|
*out->histograms[best_idx1] = *best_combo;
|
2012-04-30 14:18:50 +02:00
|
|
|
// swap best_idx2 slot with last one (which is now unused)
|
2012-04-27 21:37:50 +02:00
|
|
|
--out_size;
|
2012-04-30 14:18:50 +02:00
|
|
|
if (best_idx2 != out_size) {
|
2012-04-30 21:46:22 +02:00
|
|
|
out->histograms[best_idx2] = out->histograms[out_size];
|
|
|
|
out->histograms[out_size] = NULL; // just for sanity check.
|
2012-04-30 14:18:50 +02:00
|
|
|
}
|
2012-04-03 16:24:25 +02:00
|
|
|
tries_with_no_success = 0;
|
|
|
|
}
|
2012-10-17 00:42:15 +02:00
|
|
|
if (++tries_with_no_success >= num_tries_no_success) {
|
2012-04-03 16:24:25 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-04-30 21:46:22 +02:00
|
|
|
out->size = out_size;
|
2012-04-03 16:24:25 +02:00
|
|
|
ok = 1;
|
2012-04-30 14:18:50 +02:00
|
|
|
|
2012-04-27 21:37:50 +02:00
|
|
|
End:
|
2012-04-30 14:18:50 +02:00
|
|
|
free(histos);
|
2012-04-03 16:24:25 +02:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2012-04-30 14:18:50 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Histogram refinement
|
|
|
|
|
2013-03-18 22:34:32 +01:00
|
|
|
// What is the bit cost of moving square_histogram from cur_symbol to candidate.
|
2012-04-10 05:56:07 +02:00
|
|
|
static double HistogramDistance(const VP8LHistogram* const square_histogram,
|
2013-03-18 22:34:32 +01:00
|
|
|
const VP8LHistogram* const candidate,
|
|
|
|
double cost_threshold) {
|
|
|
|
return HistogramAddThresh(candidate, square_histogram, cost_threshold);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2012-04-30 21:46:22 +02:00
|
|
|
static void HistogramRemap(const VP8LHistogramSet* const in,
|
|
|
|
const VP8LHistogramSet* const out,
|
|
|
|
uint16_t* const symbols) {
|
2012-04-03 16:24:25 +02:00
|
|
|
int i;
|
2012-04-30 21:46:22 +02:00
|
|
|
for (i = 0; i < in->size; ++i) {
|
2012-04-03 16:24:25 +02:00
|
|
|
int best_out = 0;
|
2013-03-18 22:34:32 +01:00
|
|
|
double best_bits =
|
|
|
|
HistogramDistance(in->histograms[i], out->histograms[0], 1.e38);
|
2012-04-03 16:24:25 +02:00
|
|
|
int k;
|
2012-04-30 21:46:22 +02:00
|
|
|
for (k = 1; k < out->size; ++k) {
|
|
|
|
const double cur_bits =
|
2013-03-18 22:34:32 +01:00
|
|
|
HistogramDistance(in->histograms[i], out->histograms[k], best_bits);
|
2012-04-03 16:24:25 +02:00
|
|
|
if (cur_bits < best_bits) {
|
|
|
|
best_bits = cur_bits;
|
|
|
|
best_out = k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
symbols[i] = best_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recompute each out based on raw and symbols.
|
2012-04-30 21:46:22 +02:00
|
|
|
for (i = 0; i < out->size; ++i) {
|
|
|
|
HistogramClear(out->histograms[i]);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-04-30 21:46:22 +02:00
|
|
|
for (i = 0; i < in->size; ++i) {
|
2013-03-18 22:34:32 +01:00
|
|
|
HistogramAdd(in->histograms[i], out->histograms[symbols[i]]);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-11 11:52:13 +02:00
|
|
|
|
2012-04-30 14:18:50 +02:00
|
|
|
int VP8LGetHistoImageSymbols(int xsize, int ysize,
|
|
|
|
const VP8LBackwardRefs* const refs,
|
|
|
|
int quality, int histo_bits, int cache_bits,
|
2012-04-30 21:46:22 +02:00
|
|
|
VP8LHistogramSet* const image_in,
|
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;
|
|
|
|
const int histo_image_raw_size = histo_xsize * histo_ysize;
|
2012-11-06 01:44:19 +01:00
|
|
|
|
2012-10-17 00:42:15 +02:00
|
|
|
// Heuristic params for HistogramCombine().
|
|
|
|
const int num_tries_no_success = 8 + (quality >> 1);
|
2012-11-06 01:44:19 +01:00
|
|
|
const int iter_mult = (quality < 27) ? 1 : 1 + ((quality - 27) >> 4);
|
2013-03-05 23:05:00 +01:00
|
|
|
const int num_pairs = (quality < 25) ? 10 : (5 * quality) >> 3;
|
2012-10-17 00:42:15 +02:00
|
|
|
|
2012-04-30 21:46:22 +02:00
|
|
|
VP8LHistogramSet* const image_out =
|
|
|
|
VP8LAllocateHistogramSet(histo_image_raw_size, cache_bits);
|
|
|
|
if (image_out == NULL) return 0;
|
2012-04-26 11:55:14 +02:00
|
|
|
|
2012-04-27 21:37:50 +02:00
|
|
|
// Build histogram image.
|
2012-04-30 21:46:22 +02:00
|
|
|
HistogramBuildImage(xsize, histo_bits, refs, image_out);
|
2012-04-26 11:55:14 +02:00
|
|
|
// Collapse similar histograms.
|
2012-11-06 01:44:19 +01:00
|
|
|
if (!HistogramCombine(image_out, image_in, iter_mult, num_pairs,
|
|
|
|
num_tries_no_success)) {
|
2012-04-26 11:55:14 +02:00
|
|
|
goto Error;
|
|
|
|
}
|
2012-04-30 21:46:22 +02:00
|
|
|
// Find the optimal map from original histograms to the final ones.
|
|
|
|
HistogramRemap(image_out, image_in, histogram_symbols);
|
2012-04-26 11:55:14 +02:00
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
Error:
|
2012-04-30 21:46:22 +02:00
|
|
|
free(image_out);
|
2012-04-26 11:55:14 +02:00
|
|
|
return ok;
|
|
|
|
}
|