block_splitter.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* Copyright 2013 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* Block split point selection utilities. */
  6. #include "./block_splitter.h"
  7. #include <string.h> /* memcpy, memset */
  8. #include "../common/platform.h"
  9. #include "./bit_cost.h"
  10. #include "./cluster.h"
  11. #include "./command.h"
  12. #include "./fast_log.h"
  13. #include "./histogram.h"
  14. #include "./memory.h"
  15. #include "./quality.h"
  16. #if defined(__cplusplus) || defined(c_plusplus)
  17. extern "C" {
  18. #endif
  19. static const size_t kMaxLiteralHistograms = 100;
  20. static const size_t kMaxCommandHistograms = 50;
  21. static const double kLiteralBlockSwitchCost = 28.1;
  22. static const double kCommandBlockSwitchCost = 13.5;
  23. static const double kDistanceBlockSwitchCost = 14.6;
  24. static const size_t kLiteralStrideLength = 70;
  25. static const size_t kCommandStrideLength = 40;
  26. static const size_t kSymbolsPerLiteralHistogram = 544;
  27. static const size_t kSymbolsPerCommandHistogram = 530;
  28. static const size_t kSymbolsPerDistanceHistogram = 544;
  29. static const size_t kMinLengthForBlockSplitting = 128;
  30. static const size_t kIterMulForRefining = 2;
  31. static const size_t kMinItersForRefining = 100;
  32. static size_t CountLiterals(const Command* cmds, const size_t num_commands) {
  33. /* Count how many we have. */
  34. size_t total_length = 0;
  35. size_t i;
  36. for (i = 0; i < num_commands; ++i) {
  37. total_length += cmds[i].insert_len_;
  38. }
  39. return total_length;
  40. }
  41. static void CopyLiteralsToByteArray(const Command* cmds,
  42. const size_t num_commands,
  43. const uint8_t* data,
  44. const size_t offset,
  45. const size_t mask,
  46. uint8_t* literals) {
  47. size_t pos = 0;
  48. size_t from_pos = offset & mask;
  49. size_t i;
  50. for (i = 0; i < num_commands; ++i) {
  51. size_t insert_len = cmds[i].insert_len_;
  52. if (from_pos + insert_len > mask) {
  53. size_t head_size = mask + 1 - from_pos;
  54. memcpy(literals + pos, data + from_pos, head_size);
  55. from_pos = 0;
  56. pos += head_size;
  57. insert_len -= head_size;
  58. }
  59. if (insert_len > 0) {
  60. memcpy(literals + pos, data + from_pos, insert_len);
  61. pos += insert_len;
  62. }
  63. from_pos = (from_pos + insert_len + CommandCopyLen(&cmds[i])) & mask;
  64. }
  65. }
  66. static BROTLI_INLINE uint32_t MyRand(uint32_t* seed) {
  67. /* Initial seed should be 7. In this case, loop length is (1 << 29). */
  68. *seed *= 16807U;
  69. return *seed;
  70. }
  71. static BROTLI_INLINE double BitCost(size_t count) {
  72. return count == 0 ? -2.0 : FastLog2(count);
  73. }
  74. #define HISTOGRAMS_PER_BATCH 64
  75. #define CLUSTERS_PER_BATCH 16
  76. #define FN(X) X ## Literal
  77. #define DataType uint8_t
  78. /* NOLINTNEXTLINE(build/include) */
  79. #include "./block_splitter_inc.h"
  80. #undef DataType
  81. #undef FN
  82. #define FN(X) X ## Command
  83. #define DataType uint16_t
  84. /* NOLINTNEXTLINE(build/include) */
  85. #include "./block_splitter_inc.h"
  86. #undef FN
  87. #define FN(X) X ## Distance
  88. /* NOLINTNEXTLINE(build/include) */
  89. #include "./block_splitter_inc.h"
  90. #undef DataType
  91. #undef FN
  92. void BrotliInitBlockSplit(BlockSplit* self) {
  93. self->num_types = 0;
  94. self->num_blocks = 0;
  95. self->types = 0;
  96. self->lengths = 0;
  97. self->types_alloc_size = 0;
  98. self->lengths_alloc_size = 0;
  99. }
  100. void BrotliDestroyBlockSplit(MemoryManager* m, BlockSplit* self) {
  101. BROTLI_FREE(m, self->types);
  102. BROTLI_FREE(m, self->lengths);
  103. }
  104. void BrotliSplitBlock(MemoryManager* m,
  105. const Command* cmds,
  106. const size_t num_commands,
  107. const uint8_t* data,
  108. const size_t pos,
  109. const size_t mask,
  110. const BrotliEncoderParams* params,
  111. BlockSplit* literal_split,
  112. BlockSplit* insert_and_copy_split,
  113. BlockSplit* dist_split) {
  114. {
  115. size_t literals_count = CountLiterals(cmds, num_commands);
  116. uint8_t* literals = BROTLI_ALLOC(m, uint8_t, literals_count);
  117. if (BROTLI_IS_OOM(m)) return;
  118. /* Create a continuous array of literals. */
  119. CopyLiteralsToByteArray(cmds, num_commands, data, pos, mask, literals);
  120. /* Create the block split on the array of literals.
  121. Literal histograms have alphabet size 256. */
  122. SplitByteVectorLiteral(
  123. m, literals, literals_count,
  124. kSymbolsPerLiteralHistogram, kMaxLiteralHistograms,
  125. kLiteralStrideLength, kLiteralBlockSwitchCost, params,
  126. literal_split);
  127. if (BROTLI_IS_OOM(m)) return;
  128. BROTLI_FREE(m, literals);
  129. }
  130. {
  131. /* Compute prefix codes for commands. */
  132. uint16_t* insert_and_copy_codes = BROTLI_ALLOC(m, uint16_t, num_commands);
  133. size_t i;
  134. if (BROTLI_IS_OOM(m)) return;
  135. for (i = 0; i < num_commands; ++i) {
  136. insert_and_copy_codes[i] = cmds[i].cmd_prefix_;
  137. }
  138. /* Create the block split on the array of command prefixes. */
  139. SplitByteVectorCommand(
  140. m, insert_and_copy_codes, num_commands,
  141. kSymbolsPerCommandHistogram, kMaxCommandHistograms,
  142. kCommandStrideLength, kCommandBlockSwitchCost, params,
  143. insert_and_copy_split);
  144. if (BROTLI_IS_OOM(m)) return;
  145. /* TODO: reuse for distances? */
  146. BROTLI_FREE(m, insert_and_copy_codes);
  147. }
  148. {
  149. /* Create a continuous array of distance prefixes. */
  150. uint16_t* distance_prefixes = BROTLI_ALLOC(m, uint16_t, num_commands);
  151. size_t j = 0;
  152. size_t i;
  153. if (BROTLI_IS_OOM(m)) return;
  154. for (i = 0; i < num_commands; ++i) {
  155. const Command* cmd = &cmds[i];
  156. if (CommandCopyLen(cmd) && cmd->cmd_prefix_ >= 128) {
  157. distance_prefixes[j++] = cmd->dist_prefix_ & 0x3FF;
  158. }
  159. }
  160. /* Create the block split on the array of distance prefixes. */
  161. SplitByteVectorDistance(
  162. m, distance_prefixes, j,
  163. kSymbolsPerDistanceHistogram, kMaxCommandHistograms,
  164. kCommandStrideLength, kDistanceBlockSwitchCost, params,
  165. dist_split);
  166. if (BROTLI_IS_OOM(m)) return;
  167. BROTLI_FREE(m, distance_prefixes);
  168. }
  169. }
  170. #if defined(__cplusplus) || defined(c_plusplus)
  171. } /* extern "C" */
  172. #endif