histogram.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /* Build per-context histograms of literals, commands and distance codes. */
  6. #include "./histogram.h"
  7. #include "../common/context.h"
  8. #include "./block_splitter.h"
  9. #include "./command.h"
  10. #if defined(__cplusplus) || defined(c_plusplus)
  11. extern "C" {
  12. #endif
  13. typedef struct BlockSplitIterator {
  14. const BlockSplit* split_; /* Not owned. */
  15. size_t idx_;
  16. size_t type_;
  17. size_t length_;
  18. } BlockSplitIterator;
  19. static void InitBlockSplitIterator(BlockSplitIterator* self,
  20. const BlockSplit* split) {
  21. self->split_ = split;
  22. self->idx_ = 0;
  23. self->type_ = 0;
  24. self->length_ = split->lengths ? split->lengths[0] : 0;
  25. }
  26. static void BlockSplitIteratorNext(BlockSplitIterator* self) {
  27. if (self->length_ == 0) {
  28. ++self->idx_;
  29. self->type_ = self->split_->types[self->idx_];
  30. self->length_ = self->split_->lengths[self->idx_];
  31. }
  32. --self->length_;
  33. }
  34. void BrotliBuildHistogramsWithContext(
  35. const Command* cmds, const size_t num_commands,
  36. const BlockSplit* literal_split, const BlockSplit* insert_and_copy_split,
  37. const BlockSplit* dist_split, const uint8_t* ringbuffer, size_t start_pos,
  38. size_t mask, uint8_t prev_byte, uint8_t prev_byte2,
  39. const ContextType* context_modes, HistogramLiteral* literal_histograms,
  40. HistogramCommand* insert_and_copy_histograms,
  41. HistogramDistance* copy_dist_histograms) {
  42. size_t pos = start_pos;
  43. BlockSplitIterator literal_it;
  44. BlockSplitIterator insert_and_copy_it;
  45. BlockSplitIterator dist_it;
  46. size_t i;
  47. InitBlockSplitIterator(&literal_it, literal_split);
  48. InitBlockSplitIterator(&insert_and_copy_it, insert_and_copy_split);
  49. InitBlockSplitIterator(&dist_it, dist_split);
  50. for (i = 0; i < num_commands; ++i) {
  51. const Command* cmd = &cmds[i];
  52. size_t j;
  53. BlockSplitIteratorNext(&insert_and_copy_it);
  54. HistogramAddCommand(&insert_and_copy_histograms[insert_and_copy_it.type_],
  55. cmd->cmd_prefix_);
  56. /* TODO: unwrap iterator blocks. */
  57. for (j = cmd->insert_len_; j != 0; --j) {
  58. size_t context;
  59. BlockSplitIteratorNext(&literal_it);
  60. context = literal_it.type_;
  61. if (context_modes) {
  62. ContextLut lut = BROTLI_CONTEXT_LUT(context_modes[context]);
  63. context = (context << BROTLI_LITERAL_CONTEXT_BITS) +
  64. BROTLI_CONTEXT(prev_byte, prev_byte2, lut);
  65. }
  66. HistogramAddLiteral(&literal_histograms[context],
  67. ringbuffer[pos & mask]);
  68. prev_byte2 = prev_byte;
  69. prev_byte = ringbuffer[pos & mask];
  70. ++pos;
  71. }
  72. pos += CommandCopyLen(cmd);
  73. if (CommandCopyLen(cmd)) {
  74. prev_byte2 = ringbuffer[(pos - 2) & mask];
  75. prev_byte = ringbuffer[(pos - 1) & mask];
  76. if (cmd->cmd_prefix_ >= 128) {
  77. size_t context;
  78. BlockSplitIteratorNext(&dist_it);
  79. context = (dist_it.type_ << BROTLI_DISTANCE_CONTEXT_BITS) +
  80. CommandDistanceContext(cmd);
  81. HistogramAddDistance(&copy_dist_histograms[context],
  82. cmd->dist_prefix_ & 0x3FF);
  83. }
  84. }
  85. }
  86. }
  87. #if defined(__cplusplus) || defined(c_plusplus)
  88. } /* extern "C" */
  89. #endif