metablock.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Copyright 2015 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. /* Algorithms for distributing the literals and commands of a metablock between
  6. block types and contexts. */
  7. #ifndef BROTLI_ENC_METABLOCK_H_
  8. #define BROTLI_ENC_METABLOCK_H_
  9. #include "../common/context.h"
  10. #include "../common/platform.h"
  11. #include <brotli/types.h>
  12. #include "./block_splitter.h"
  13. #include "./command.h"
  14. #include "./histogram.h"
  15. #include "./memory.h"
  16. #include "./quality.h"
  17. #if defined(__cplusplus) || defined(c_plusplus)
  18. extern "C" {
  19. #endif
  20. typedef struct MetaBlockSplit {
  21. BlockSplit literal_split;
  22. BlockSplit command_split;
  23. BlockSplit distance_split;
  24. uint32_t* literal_context_map;
  25. size_t literal_context_map_size;
  26. uint32_t* distance_context_map;
  27. size_t distance_context_map_size;
  28. HistogramLiteral* literal_histograms;
  29. size_t literal_histograms_size;
  30. HistogramCommand* command_histograms;
  31. size_t command_histograms_size;
  32. HistogramDistance* distance_histograms;
  33. size_t distance_histograms_size;
  34. } MetaBlockSplit;
  35. static BROTLI_INLINE void InitMetaBlockSplit(MetaBlockSplit* mb) {
  36. BrotliInitBlockSplit(&mb->literal_split);
  37. BrotliInitBlockSplit(&mb->command_split);
  38. BrotliInitBlockSplit(&mb->distance_split);
  39. mb->literal_context_map = 0;
  40. mb->literal_context_map_size = 0;
  41. mb->distance_context_map = 0;
  42. mb->distance_context_map_size = 0;
  43. mb->literal_histograms = 0;
  44. mb->literal_histograms_size = 0;
  45. mb->command_histograms = 0;
  46. mb->command_histograms_size = 0;
  47. mb->distance_histograms = 0;
  48. mb->distance_histograms_size = 0;
  49. }
  50. static BROTLI_INLINE void DestroyMetaBlockSplit(
  51. MemoryManager* m, MetaBlockSplit* mb) {
  52. BrotliDestroyBlockSplit(m, &mb->literal_split);
  53. BrotliDestroyBlockSplit(m, &mb->command_split);
  54. BrotliDestroyBlockSplit(m, &mb->distance_split);
  55. BROTLI_FREE(m, mb->literal_context_map);
  56. BROTLI_FREE(m, mb->distance_context_map);
  57. BROTLI_FREE(m, mb->literal_histograms);
  58. BROTLI_FREE(m, mb->command_histograms);
  59. BROTLI_FREE(m, mb->distance_histograms);
  60. }
  61. /* Uses the slow shortest-path block splitter and does context clustering.
  62. The distance parameters are dynamically selected based on the commands
  63. which get recomputed under the new distance parameters. The new distance
  64. parameters are stored into *params. */
  65. BROTLI_INTERNAL void BrotliBuildMetaBlock(MemoryManager* m,
  66. const uint8_t* ringbuffer,
  67. const size_t pos,
  68. const size_t mask,
  69. BrotliEncoderParams* params,
  70. uint8_t prev_byte,
  71. uint8_t prev_byte2,
  72. Command* cmds,
  73. size_t num_commands,
  74. ContextType literal_context_mode,
  75. MetaBlockSplit* mb);
  76. /* Uses a fast greedy block splitter that tries to merge current block with the
  77. last or the second last block and uses a static context clustering which
  78. is the same for all block types. */
  79. BROTLI_INTERNAL void BrotliBuildMetaBlockGreedy(
  80. MemoryManager* m, const uint8_t* ringbuffer, size_t pos, size_t mask,
  81. uint8_t prev_byte, uint8_t prev_byte2, ContextLut literal_context_lut,
  82. size_t num_contexts, const uint32_t* static_context_map,
  83. const Command* commands, size_t n_commands, MetaBlockSplit* mb);
  84. BROTLI_INTERNAL void BrotliOptimizeHistograms(uint32_t num_distance_codes,
  85. MetaBlockSplit* mb);
  86. BROTLI_INTERNAL void BrotliInitDistanceParams(BrotliEncoderParams* params,
  87. uint32_t npostfix, uint32_t ndirect);
  88. #if defined(__cplusplus) || defined(c_plusplus)
  89. } /* extern "C" */
  90. #endif
  91. #endif /* BROTLI_ENC_METABLOCK_H_ */