2013-10-23 11:06:13 +00:00
|
|
|
// Copyright 2013 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// Functions to estimate the bit cost of Huffman trees.
|
|
|
|
|
|
|
|
#ifndef BROTLI_ENC_BIT_COST_H_
|
|
|
|
#define BROTLI_ENC_BIT_COST_H_
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "./entropy_encode.h"
|
|
|
|
#include "./fast_log.h"
|
|
|
|
|
|
|
|
namespace brotli {
|
|
|
|
|
2015-03-27 13:20:35 +00:00
|
|
|
static inline double BitsEntropy(const int *population, int size) {
|
|
|
|
int sum = 0;
|
|
|
|
double retval = 0;
|
|
|
|
const int *population_end = population + size;
|
|
|
|
int p;
|
|
|
|
if (size & 1) {
|
|
|
|
goto odd_number_of_elements_left;
|
|
|
|
}
|
|
|
|
while (population < population_end) {
|
|
|
|
p = *population++;
|
|
|
|
sum += p;
|
|
|
|
retval -= p * FastLog2(p);
|
|
|
|
odd_number_of_elements_left:
|
|
|
|
p = *population++;
|
|
|
|
sum += p;
|
|
|
|
retval -= p * FastLog2(p);
|
|
|
|
}
|
2015-04-23 13:35:16 +00:00
|
|
|
if (sum) retval += sum * FastLog2(sum);
|
2015-03-27 13:20:35 +00:00
|
|
|
if (retval < sum) {
|
|
|
|
// At least one bit per literal is needed.
|
|
|
|
retval = sum;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2013-10-23 11:06:13 +00:00
|
|
|
static const int kHuffmanExtraBits[kCodeLengthCodes] = {
|
2013-12-12 12:18:04 +00:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3,
|
2013-10-23 11:06:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline int HuffmanTreeBitCost(const int* counts, const uint8_t* depth) {
|
|
|
|
int nbits = 0;
|
|
|
|
for (int i = 0; i < kCodeLengthCodes; ++i) {
|
|
|
|
nbits += counts[i] * (depth[i] + kHuffmanExtraBits[i]);
|
|
|
|
}
|
|
|
|
return nbits;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int HuffmanTreeBitCost(
|
|
|
|
const Histogram<kCodeLengthCodes>& histogram,
|
|
|
|
const EntropyCode<kCodeLengthCodes>& entropy) {
|
|
|
|
return HuffmanTreeBitCost(&histogram.data_[0], &entropy.depth_[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int HuffmanBitCost(const uint8_t* depth, int length) {
|
|
|
|
int max_depth = 1;
|
|
|
|
int histogram[kCodeLengthCodes] = { 0 };
|
|
|
|
int tail_start = 0;
|
2014-03-20 13:32:35 +00:00
|
|
|
int prev_value = 8;
|
2013-10-23 11:06:13 +00:00
|
|
|
// compute histogram of compacted huffman tree
|
|
|
|
for (int i = 0; i < length;) {
|
|
|
|
const int value = depth[i];
|
|
|
|
if (value > max_depth) {
|
|
|
|
max_depth = value;
|
|
|
|
}
|
|
|
|
int reps = 1;
|
|
|
|
for (int k = i + 1; k < length && depth[k] == value; ++k) {
|
|
|
|
++reps;
|
|
|
|
}
|
|
|
|
i += reps;
|
2014-03-20 13:32:35 +00:00
|
|
|
if (i == length && value == 0)
|
|
|
|
break;
|
2013-10-23 11:06:13 +00:00
|
|
|
if (value == 0) {
|
2013-12-12 12:18:04 +00:00
|
|
|
if (reps < 3) {
|
2013-10-23 11:06:13 +00:00
|
|
|
histogram[0] += reps;
|
2013-12-12 12:18:04 +00:00
|
|
|
} else {
|
2014-03-20 13:32:35 +00:00
|
|
|
reps -= 2;
|
|
|
|
while (reps > 0) {
|
2013-12-12 12:18:04 +00:00
|
|
|
++histogram[17];
|
|
|
|
reps >>= 3;
|
|
|
|
}
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tail_start = i;
|
2014-03-20 13:32:35 +00:00
|
|
|
if (value != prev_value) {
|
|
|
|
++histogram[value];
|
|
|
|
--reps;
|
|
|
|
}
|
|
|
|
prev_value = value;
|
2013-12-12 12:18:04 +00:00
|
|
|
if (reps < 3) {
|
2013-10-23 11:06:13 +00:00
|
|
|
histogram[value] += reps;
|
2013-12-12 12:18:04 +00:00
|
|
|
} else {
|
2014-03-20 13:32:35 +00:00
|
|
|
reps -= 2;
|
|
|
|
while (reps > 0) {
|
2013-12-12 12:18:04 +00:00
|
|
|
++histogram[16];
|
|
|
|
reps >>= 2;
|
|
|
|
}
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create huffman tree of huffman tree
|
|
|
|
uint8_t cost[kCodeLengthCodes] = { 0 };
|
2014-10-15 12:01:36 +00:00
|
|
|
CreateHuffmanTree(histogram, kCodeLengthCodes, 7, 9, cost);
|
2013-10-23 11:06:13 +00:00
|
|
|
// account for rle extra bits
|
|
|
|
cost[16] += 2;
|
|
|
|
cost[17] += 3;
|
|
|
|
|
|
|
|
int tree_size = 0;
|
2014-03-20 13:32:35 +00:00
|
|
|
int bits = 18 + 2 * max_depth; // huffman tree of huffman tree cost
|
2013-10-23 11:06:13 +00:00
|
|
|
for (int i = 0; i < kCodeLengthCodes; ++i) {
|
|
|
|
bits += histogram[i] * cost[i]; // huffman tree bit cost
|
|
|
|
tree_size += histogram[i];
|
|
|
|
}
|
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<int kSize>
|
|
|
|
double PopulationCost(const Histogram<kSize>& histogram) {
|
|
|
|
if (histogram.total_count_ == 0) {
|
2013-12-17 16:17:57 +00:00
|
|
|
return 12;
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
|
|
|
int count = 0;
|
2013-11-15 18:02:17 +00:00
|
|
|
for (int i = 0; i < kSize && count < 5; ++i) {
|
2013-10-23 11:06:13 +00:00
|
|
|
if (histogram.data_[i] > 0) {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
if (count == 1) {
|
2013-12-17 16:17:57 +00:00
|
|
|
return 12;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
|
|
|
if (count == 2) {
|
2013-12-17 16:17:57 +00:00
|
|
|
return 20 + histogram.total_count_;
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
|
|
|
uint8_t depth[kSize] = { 0 };
|
2014-10-15 12:01:36 +00:00
|
|
|
CreateHuffmanTree(&histogram.data_[0], kSize, 15, 9, depth);
|
2013-11-15 18:02:17 +00:00
|
|
|
int bits = 0;
|
2013-10-23 11:06:13 +00:00
|
|
|
for (int i = 0; i < kSize; ++i) {
|
|
|
|
bits += histogram.data_[i] * depth[i];
|
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
if (count == 3) {
|
2013-12-17 16:17:57 +00:00
|
|
|
bits += 28;
|
|
|
|
} else if (count == 4) {
|
|
|
|
bits += 37;
|
2013-11-15 18:02:17 +00:00
|
|
|
} else {
|
|
|
|
bits += HuffmanBitCost(depth, kSize);
|
|
|
|
}
|
2013-10-23 11:06:13 +00:00
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace brotli
|
|
|
|
|
|
|
|
#endif // BROTLI_ENC_BIT_COST_H_
|