bit_cost_inc.h 4.0 KB

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