xref: /aosp_15_r20/external/brotli/c/enc/bit_cost_inc.h (revision f4ee7fba7774faf2a30f13154332c0a06550dbc4)
1*f4ee7fbaSAndroid Build Coastguard Worker /* NOLINT(build/header_guard) */
2*f4ee7fbaSAndroid Build Coastguard Worker /* Copyright 2013 Google Inc. All Rights Reserved.
3*f4ee7fbaSAndroid Build Coastguard Worker 
4*f4ee7fbaSAndroid Build Coastguard Worker    Distributed under MIT license.
5*f4ee7fbaSAndroid Build Coastguard Worker    See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
6*f4ee7fbaSAndroid Build Coastguard Worker */
7*f4ee7fbaSAndroid Build Coastguard Worker 
8*f4ee7fbaSAndroid Build Coastguard Worker /* template parameters: FN */
9*f4ee7fbaSAndroid Build Coastguard Worker 
10*f4ee7fbaSAndroid Build Coastguard Worker #define HistogramType FN(Histogram)
11*f4ee7fbaSAndroid Build Coastguard Worker 
FN(BrotliPopulationCost)12*f4ee7fbaSAndroid Build Coastguard Worker double FN(BrotliPopulationCost)(const HistogramType* histogram) {
13*f4ee7fbaSAndroid Build Coastguard Worker   static const double kOneSymbolHistogramCost = 12;
14*f4ee7fbaSAndroid Build Coastguard Worker   static const double kTwoSymbolHistogramCost = 20;
15*f4ee7fbaSAndroid Build Coastguard Worker   static const double kThreeSymbolHistogramCost = 28;
16*f4ee7fbaSAndroid Build Coastguard Worker   static const double kFourSymbolHistogramCost = 37;
17*f4ee7fbaSAndroid Build Coastguard Worker   const size_t data_size = FN(HistogramDataSize)();
18*f4ee7fbaSAndroid Build Coastguard Worker   int count = 0;
19*f4ee7fbaSAndroid Build Coastguard Worker   size_t s[5];
20*f4ee7fbaSAndroid Build Coastguard Worker   double bits = 0.0;
21*f4ee7fbaSAndroid Build Coastguard Worker   size_t i;
22*f4ee7fbaSAndroid Build Coastguard Worker   if (histogram->total_count_ == 0) {
23*f4ee7fbaSAndroid Build Coastguard Worker     return kOneSymbolHistogramCost;
24*f4ee7fbaSAndroid Build Coastguard Worker   }
25*f4ee7fbaSAndroid Build Coastguard Worker   for (i = 0; i < data_size; ++i) {
26*f4ee7fbaSAndroid Build Coastguard Worker     if (histogram->data_[i] > 0) {
27*f4ee7fbaSAndroid Build Coastguard Worker       s[count] = i;
28*f4ee7fbaSAndroid Build Coastguard Worker       ++count;
29*f4ee7fbaSAndroid Build Coastguard Worker       if (count > 4) break;
30*f4ee7fbaSAndroid Build Coastguard Worker     }
31*f4ee7fbaSAndroid Build Coastguard Worker   }
32*f4ee7fbaSAndroid Build Coastguard Worker   if (count == 1) {
33*f4ee7fbaSAndroid Build Coastguard Worker     return kOneSymbolHistogramCost;
34*f4ee7fbaSAndroid Build Coastguard Worker   }
35*f4ee7fbaSAndroid Build Coastguard Worker   if (count == 2) {
36*f4ee7fbaSAndroid Build Coastguard Worker     return (kTwoSymbolHistogramCost + (double)histogram->total_count_);
37*f4ee7fbaSAndroid Build Coastguard Worker   }
38*f4ee7fbaSAndroid Build Coastguard Worker   if (count == 3) {
39*f4ee7fbaSAndroid Build Coastguard Worker     const uint32_t histo0 = histogram->data_[s[0]];
40*f4ee7fbaSAndroid Build Coastguard Worker     const uint32_t histo1 = histogram->data_[s[1]];
41*f4ee7fbaSAndroid Build Coastguard Worker     const uint32_t histo2 = histogram->data_[s[2]];
42*f4ee7fbaSAndroid Build Coastguard Worker     const uint32_t histomax =
43*f4ee7fbaSAndroid Build Coastguard Worker         BROTLI_MAX(uint32_t, histo0, BROTLI_MAX(uint32_t, histo1, histo2));
44*f4ee7fbaSAndroid Build Coastguard Worker     return (kThreeSymbolHistogramCost +
45*f4ee7fbaSAndroid Build Coastguard Worker             2 * (histo0 + histo1 + histo2) - histomax);
46*f4ee7fbaSAndroid Build Coastguard Worker   }
47*f4ee7fbaSAndroid Build Coastguard Worker   if (count == 4) {
48*f4ee7fbaSAndroid Build Coastguard Worker     uint32_t histo[4];
49*f4ee7fbaSAndroid Build Coastguard Worker     uint32_t h23;
50*f4ee7fbaSAndroid Build Coastguard Worker     uint32_t histomax;
51*f4ee7fbaSAndroid Build Coastguard Worker     for (i = 0; i < 4; ++i) {
52*f4ee7fbaSAndroid Build Coastguard Worker       histo[i] = histogram->data_[s[i]];
53*f4ee7fbaSAndroid Build Coastguard Worker     }
54*f4ee7fbaSAndroid Build Coastguard Worker     /* Sort */
55*f4ee7fbaSAndroid Build Coastguard Worker     for (i = 0; i < 4; ++i) {
56*f4ee7fbaSAndroid Build Coastguard Worker       size_t j;
57*f4ee7fbaSAndroid Build Coastguard Worker       for (j = i + 1; j < 4; ++j) {
58*f4ee7fbaSAndroid Build Coastguard Worker         if (histo[j] > histo[i]) {
59*f4ee7fbaSAndroid Build Coastguard Worker           BROTLI_SWAP(uint32_t, histo, j, i);
60*f4ee7fbaSAndroid Build Coastguard Worker         }
61*f4ee7fbaSAndroid Build Coastguard Worker       }
62*f4ee7fbaSAndroid Build Coastguard Worker     }
63*f4ee7fbaSAndroid Build Coastguard Worker     h23 = histo[2] + histo[3];
64*f4ee7fbaSAndroid Build Coastguard Worker     histomax = BROTLI_MAX(uint32_t, h23, histo[0]);
65*f4ee7fbaSAndroid Build Coastguard Worker     return (kFourSymbolHistogramCost +
66*f4ee7fbaSAndroid Build Coastguard Worker             3 * h23 + 2 * (histo[0] + histo[1]) - histomax);
67*f4ee7fbaSAndroid Build Coastguard Worker   }
68*f4ee7fbaSAndroid Build Coastguard Worker 
69*f4ee7fbaSAndroid Build Coastguard Worker   {
70*f4ee7fbaSAndroid Build Coastguard Worker     /* In this loop we compute the entropy of the histogram and simultaneously
71*f4ee7fbaSAndroid Build Coastguard Worker        build a simplified histogram of the code length codes where we use the
72*f4ee7fbaSAndroid Build Coastguard Worker        zero repeat code 17, but we don't use the non-zero repeat code 16. */
73*f4ee7fbaSAndroid Build Coastguard Worker     size_t max_depth = 1;
74*f4ee7fbaSAndroid Build Coastguard Worker     uint32_t depth_histo[BROTLI_CODE_LENGTH_CODES] = { 0 };
75*f4ee7fbaSAndroid Build Coastguard Worker     const double log2total = FastLog2(histogram->total_count_);
76*f4ee7fbaSAndroid Build Coastguard Worker     for (i = 0; i < data_size;) {
77*f4ee7fbaSAndroid Build Coastguard Worker       if (histogram->data_[i] > 0) {
78*f4ee7fbaSAndroid Build Coastguard Worker         /* Compute -log2(P(symbol)) = -log2(count(symbol)/total_count) =
79*f4ee7fbaSAndroid Build Coastguard Worker                                     = log2(total_count) - log2(count(symbol)) */
80*f4ee7fbaSAndroid Build Coastguard Worker         double log2p = log2total - FastLog2(histogram->data_[i]);
81*f4ee7fbaSAndroid Build Coastguard Worker         /* Approximate the bit depth by round(-log2(P(symbol))) */
82*f4ee7fbaSAndroid Build Coastguard Worker         size_t depth = (size_t)(log2p + 0.5);
83*f4ee7fbaSAndroid Build Coastguard Worker         bits += histogram->data_[i] * log2p;
84*f4ee7fbaSAndroid Build Coastguard Worker         if (depth > 15) {
85*f4ee7fbaSAndroid Build Coastguard Worker           depth = 15;
86*f4ee7fbaSAndroid Build Coastguard Worker         }
87*f4ee7fbaSAndroid Build Coastguard Worker         if (depth > max_depth) {
88*f4ee7fbaSAndroid Build Coastguard Worker           max_depth = depth;
89*f4ee7fbaSAndroid Build Coastguard Worker         }
90*f4ee7fbaSAndroid Build Coastguard Worker         ++depth_histo[depth];
91*f4ee7fbaSAndroid Build Coastguard Worker         ++i;
92*f4ee7fbaSAndroid Build Coastguard Worker       } else {
93*f4ee7fbaSAndroid Build Coastguard Worker         /* Compute the run length of zeros and add the appropriate number of 0
94*f4ee7fbaSAndroid Build Coastguard Worker            and 17 code length codes to the code length code histogram. */
95*f4ee7fbaSAndroid Build Coastguard Worker         uint32_t reps = 1;
96*f4ee7fbaSAndroid Build Coastguard Worker         size_t k;
97*f4ee7fbaSAndroid Build Coastguard Worker         for (k = i + 1; k < data_size && histogram->data_[k] == 0; ++k) {
98*f4ee7fbaSAndroid Build Coastguard Worker           ++reps;
99*f4ee7fbaSAndroid Build Coastguard Worker         }
100*f4ee7fbaSAndroid Build Coastguard Worker         i += reps;
101*f4ee7fbaSAndroid Build Coastguard Worker         if (i == data_size) {
102*f4ee7fbaSAndroid Build Coastguard Worker           /* Don't add any cost for the last zero run, since these are encoded
103*f4ee7fbaSAndroid Build Coastguard Worker              only implicitly. */
104*f4ee7fbaSAndroid Build Coastguard Worker           break;
105*f4ee7fbaSAndroid Build Coastguard Worker         }
106*f4ee7fbaSAndroid Build Coastguard Worker         if (reps < 3) {
107*f4ee7fbaSAndroid Build Coastguard Worker           depth_histo[0] += reps;
108*f4ee7fbaSAndroid Build Coastguard Worker         } else {
109*f4ee7fbaSAndroid Build Coastguard Worker           reps -= 2;
110*f4ee7fbaSAndroid Build Coastguard Worker           while (reps > 0) {
111*f4ee7fbaSAndroid Build Coastguard Worker             ++depth_histo[BROTLI_REPEAT_ZERO_CODE_LENGTH];
112*f4ee7fbaSAndroid Build Coastguard Worker             /* Add the 3 extra bits for the 17 code length code. */
113*f4ee7fbaSAndroid Build Coastguard Worker             bits += 3;
114*f4ee7fbaSAndroid Build Coastguard Worker             reps >>= 3;
115*f4ee7fbaSAndroid Build Coastguard Worker           }
116*f4ee7fbaSAndroid Build Coastguard Worker         }
117*f4ee7fbaSAndroid Build Coastguard Worker       }
118*f4ee7fbaSAndroid Build Coastguard Worker     }
119*f4ee7fbaSAndroid Build Coastguard Worker     /* Add the estimated encoding cost of the code length code histogram. */
120*f4ee7fbaSAndroid Build Coastguard Worker     bits += (double)(18 + 2 * max_depth);
121*f4ee7fbaSAndroid Build Coastguard Worker     /* Add the entropy of the code length code histogram. */
122*f4ee7fbaSAndroid Build Coastguard Worker     bits += BitsEntropy(depth_histo, BROTLI_CODE_LENGTH_CODES);
123*f4ee7fbaSAndroid Build Coastguard Worker   }
124*f4ee7fbaSAndroid Build Coastguard Worker   return bits;
125*f4ee7fbaSAndroid Build Coastguard Worker }
126*f4ee7fbaSAndroid Build Coastguard Worker 
127*f4ee7fbaSAndroid Build Coastguard Worker #undef HistogramType
128