2015-11-27 10:27:11 +00:00
|
|
|
/* Copyright 2013 Google Inc. All Rights Reserved.
|
|
|
|
|
2015-12-11 10:11:51 +00:00
|
|
|
Distributed under MIT license.
|
2015-11-27 10:27:11 +00:00
|
|
|
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
|
|
|
*/
|
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Functions to estimate the bit cost of Huffman trees. */
|
2013-10-23 11:06:13 +00:00
|
|
|
|
|
|
|
#ifndef BROTLI_ENC_BIT_COST_H_
|
|
|
|
#define BROTLI_ENC_BIT_COST_H_
|
|
|
|
|
2017-12-12 13:33:12 +00:00
|
|
|
#include "../common/platform.h"
|
2016-08-23 12:40:33 +00:00
|
|
|
#include <brotli/types.h>
|
2021-09-08 07:18:45 +00:00
|
|
|
#include "fast_log.h"
|
|
|
|
#include "histogram.h"
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2018-02-26 14:04:36 +00:00
|
|
|
static BROTLI_INLINE double ShannonEntropy(
|
|
|
|
const uint32_t* population, size_t size, size_t* total) {
|
2016-01-07 15:27:49 +00:00
|
|
|
size_t sum = 0;
|
2015-03-27 13:20:35 +00:00
|
|
|
double retval = 0;
|
2018-02-26 14:04:36 +00:00
|
|
|
const uint32_t* population_end = population + size;
|
2016-01-07 15:27:49 +00:00
|
|
|
size_t p;
|
2015-03-27 13:20:35 +00:00
|
|
|
if (size & 1) {
|
|
|
|
goto odd_number_of_elements_left;
|
|
|
|
}
|
|
|
|
while (population < population_end) {
|
|
|
|
p = *population++;
|
|
|
|
sum += p;
|
2016-06-13 09:01:04 +00:00
|
|
|
retval -= (double)p * FastLog2(p);
|
2015-03-27 13:20:35 +00:00
|
|
|
odd_number_of_elements_left:
|
|
|
|
p = *population++;
|
|
|
|
sum += p;
|
2016-06-13 09:01:04 +00:00
|
|
|
retval -= (double)p * FastLog2(p);
|
2015-03-27 13:20:35 +00:00
|
|
|
}
|
2016-06-13 09:01:04 +00:00
|
|
|
if (sum) retval += (double)sum * FastLog2(sum);
|
2015-08-28 14:09:23 +00:00
|
|
|
*total = sum;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
static BROTLI_INLINE double BitsEntropy(
|
2018-02-26 14:04:36 +00:00
|
|
|
const uint32_t* population, size_t size) {
|
2016-01-07 15:27:49 +00:00
|
|
|
size_t sum;
|
2015-08-28 14:09:23 +00:00
|
|
|
double retval = ShannonEntropy(population, size, &sum);
|
2020-09-07 07:40:03 +00:00
|
|
|
if (retval < (double)sum) {
|
2016-06-03 09:19:23 +00:00
|
|
|
/* At least one bit per literal is needed. */
|
2016-06-13 09:01:04 +00:00
|
|
|
retval = (double)sum;
|
2015-03-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
BROTLI_INTERNAL double BrotliPopulationCostLiteral(const HistogramLiteral*);
|
|
|
|
BROTLI_INTERNAL double BrotliPopulationCostCommand(const HistogramCommand*);
|
|
|
|
BROTLI_INTERNAL double BrotliPopulationCostDistance(const HistogramDistance*);
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} /* extern "C" */
|
|
|
|
#endif
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
#endif /* BROTLI_ENC_BIT_COST_H_ */
|