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