block_splitter.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <assert.h>
  8. #include <string.h> /* memcpy, memset */
  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 "./port.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 unsigned int MyRand(unsigned int* seed) {
  67. *seed *= 16807U;
  68. if (*seed == 0) {
  69. *seed = 1;
  70. }
  71. return *seed;
  72. }
  73. static BROTLI_INLINE double BitCost(size_t count) {
  74. return count == 0 ? -2.0 : FastLog2(count);
  75. }
  76. #define HISTOGRAMS_PER_BATCH 64
  77. #define CLUSTERS_PER_BATCH 16
  78. #define FN(X) X ## Literal
  79. #define DataType uint8_t
  80. /* NOLINTNEXTLINE(build/include) */
  81. #include "./block_splitter_inc.h"
  82. #undef DataType
  83. #undef FN
  84. #define FN(X) X ## Command
  85. #define DataType uint16_t
  86. /* NOLINTNEXTLINE(build/include) */
  87. #include "./block_splitter_inc.h"
  88. #undef FN
  89. #define FN(X) X ## Distance
  90. /* NOLINTNEXTLINE(build/include) */
  91. #include "./block_splitter_inc.h"
  92. #undef DataType
  93. #undef FN
  94. void BrotliInitBlockSplit(BlockSplit* self) {
  95. self->num_types = 0;
  96. self->num_blocks = 0;
  97. self->types = 0;
  98. self->lengths = 0;
  99. self->types_alloc_size = 0;
  100. self->lengths_alloc_size = 0;
  101. }
  102. void BrotliDestroyBlockSplit(MemoryManager* m, BlockSplit* self) {
  103. BROTLI_FREE(m, self->types);
  104. BROTLI_FREE(m, self->lengths);
  105. }
  106. void BrotliSplitBlock(MemoryManager* m,
  107. const Command* cmds,
  108. const size_t num_commands,
  109. const uint8_t* data,
  110. const size_t pos,
  111. const size_t mask,
  112. const int quality,
  113. BlockSplit* literal_split,
  114. BlockSplit* insert_and_copy_split,
  115. BlockSplit* dist_split) {
  116. {
  117. size_t literals_count = CountLiterals(cmds, num_commands);
  118. uint8_t* literals = BROTLI_ALLOC(m, uint8_t, literals_count);
  119. if (BROTLI_IS_OOM(m)) return;
  120. /* Create a continuous array of literals. */
  121. CopyLiteralsToByteArray(cmds, num_commands, data, pos, mask, literals);
  122. /* Create the block split on the array of literals.
  123. Literal histograms have alphabet size 256. */
  124. SplitByteVectorLiteral(
  125. m, literals, literals_count,
  126. kSymbolsPerLiteralHistogram, kMaxLiteralHistograms,
  127. kLiteralStrideLength, kLiteralBlockSwitchCost, quality,
  128. literal_split);
  129. if (BROTLI_IS_OOM(m)) return;
  130. BROTLI_FREE(m, literals);
  131. }
  132. {
  133. /* Compute prefix codes for commands. */
  134. uint16_t* insert_and_copy_codes = BROTLI_ALLOC(m, uint16_t, num_commands);
  135. size_t i;
  136. if (BROTLI_IS_OOM(m)) return;
  137. for (i = 0; i < num_commands; ++i) {
  138. insert_and_copy_codes[i] = cmds[i].cmd_prefix_;
  139. }
  140. /* Create the block split on the array of command prefixes. */
  141. SplitByteVectorCommand(
  142. m, insert_and_copy_codes, num_commands,
  143. kSymbolsPerCommandHistogram, kMaxCommandHistograms,
  144. kCommandStrideLength, kCommandBlockSwitchCost, quality,
  145. insert_and_copy_split);
  146. if (BROTLI_IS_OOM(m)) return;
  147. /* TODO: reuse for distances? */
  148. BROTLI_FREE(m, insert_and_copy_codes);
  149. }
  150. {
  151. /* Create a continuous array of distance prefixes. */
  152. uint16_t* distance_prefixes = BROTLI_ALLOC(m, uint16_t, num_commands);
  153. size_t j = 0;
  154. size_t i;
  155. if (BROTLI_IS_OOM(m)) return;
  156. for (i = 0; i < num_commands; ++i) {
  157. const Command* cmd = &cmds[i];
  158. if (CommandCopyLen(cmd) && cmd->cmd_prefix_ >= 128) {
  159. distance_prefixes[j++] = cmd->dist_prefix_;
  160. }
  161. }
  162. /* Create the block split on the array of distance prefixes. */
  163. SplitByteVectorDistance(
  164. m, distance_prefixes, j,
  165. kSymbolsPerDistanceHistogram, kMaxCommandHistograms,
  166. kCommandStrideLength, kDistanceBlockSwitchCost, quality,
  167. dist_split);
  168. if (BROTLI_IS_OOM(m)) return;
  169. BROTLI_FREE(m, distance_prefixes);
  170. }
  171. }
  172. #if defined(__cplusplus) || defined(c_plusplus)
  173. } /* extern "C" */
  174. #endif