1 /* NOLINT(build/header_guard) */
2 /* Copyright 2013 Google Inc. All Rights Reserved.
4 Distributed under MIT license.
5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
8 /* template parameters: FN */
10 #define HistogramType FN(Histogram)
12 double FN(BrotliPopulationCost
)(const HistogramType
* histogram
) {
13 static const double kOneSymbolHistogramCost
= 12;
14 static const double kTwoSymbolHistogramCost
= 20;
15 static const double kThreeSymbolHistogramCost
= 28;
16 static const double kFourSymbolHistogramCost
= 37;
17 const size_t data_size
= FN(HistogramDataSize
)();
22 if (histogram
->total_count_
== 0) {
23 return kOneSymbolHistogramCost
;
25 for (i
= 0; i
< data_size
; ++i
) {
26 if (histogram
->data_
[i
] > 0) {
33 return kOneSymbolHistogramCost
;
36 return (kTwoSymbolHistogramCost
+ (double)histogram
->total_count_
);
39 const uint32_t histo0
= histogram
->data_
[s
[0]];
40 const uint32_t histo1
= histogram
->data_
[s
[1]];
41 const uint32_t histo2
= histogram
->data_
[s
[2]];
42 const uint32_t histomax
=
43 BROTLI_MAX(uint32_t, histo0
, BROTLI_MAX(uint32_t, histo1
, histo2
));
44 return (kThreeSymbolHistogramCost
+
45 2 * (histo0
+ histo1
+ histo2
) - histomax
);
51 for (i
= 0; i
< 4; ++i
) {
52 histo
[i
] = histogram
->data_
[s
[i
]];
55 for (i
= 0; i
< 4; ++i
) {
57 for (j
= i
+ 1; j
< 4; ++j
) {
58 if (histo
[j
] > histo
[i
]) {
59 BROTLI_SWAP(uint32_t, histo
, j
, i
);
63 h23
= histo
[2] + histo
[3];
64 histomax
= BROTLI_MAX(uint32_t, h23
, histo
[0]);
65 return (kFourSymbolHistogramCost
+
66 3 * h23
+ 2 * (histo
[0] + histo
[1]) - histomax
);
70 /* In this loop we compute the entropy of the histogram and simultaneously
71 build a simplified histogram of the code length codes where we use the
72 zero repeat code 17, but we don't use the non-zero repeat code 16. */
74 uint32_t depth_histo
[BROTLI_CODE_LENGTH_CODES
] = { 0 };
75 const double log2total
= FastLog2(histogram
->total_count_
);
76 for (i
= 0; i
< data_size
;) {
77 if (histogram
->data_
[i
] > 0) {
78 /* Compute -log2(P(symbol)) = -log2(count(symbol)/total_count) =
79 = log2(total_count) - log2(count(symbol)) */
80 double log2p
= log2total
- FastLog2(histogram
->data_
[i
]);
81 /* Approximate the bit depth by round(-log2(P(symbol))) */
82 size_t depth
= (size_t)(log2p
+ 0.5);
83 bits
+= histogram
->data_
[i
] * log2p
;
87 if (depth
> max_depth
) {
93 /* Compute the run length of zeros and add the appropriate number of 0
94 and 17 code length codes to the code length code histogram. */
97 for (k
= i
+ 1; k
< data_size
&& histogram
->data_
[k
] == 0; ++k
) {
101 if (i
== data_size
) {
102 /* Don't add any cost for the last zero run, since these are encoded
107 depth_histo
[0] += reps
;
111 ++depth_histo
[BROTLI_REPEAT_ZERO_CODE_LENGTH
];
112 /* Add the 3 extra bits for the 17 code length code. */
119 /* Add the estimated encoding cost of the code length code histogram. */
120 bits
+= (double)(18 + 2 * max_depth
);
121 /* Add the entropy of the code length code histogram. */
122 bits
+= BitsEntropy(depth_histo
, BROTLI_CODE_LENGTH_CODES
);