1 /* Copyright 2015 Google Inc. All Rights Reserved.
2
3 Distributed under MIT license.
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5 */
6
7 /* Algorithms for distributing the literals and commands of a metablock between
8 block types and contexts. */
9
10 #ifndef BROTLI_ENC_METABLOCK_H_
11 #define BROTLI_ENC_METABLOCK_H_
12
13 #include <brotli/types.h>
14 #include "./block_splitter.h"
15 #include "./command.h"
16 #include "./context.h"
17 #include "./histogram.h"
18 #include "./memory.h"
19 #include "./port.h"
20 #include "./quality.h"
21
22 #if defined(__cplusplus) || defined(c_plusplus)
23 extern "C" {
24 #endif
25
26 typedef struct MetaBlockSplit {
27 BlockSplit literal_split;
28 BlockSplit command_split;
29 BlockSplit distance_split;
30 uint32_t* literal_context_map;
31 size_t literal_context_map_size;
32 uint32_t* distance_context_map;
33 size_t distance_context_map_size;
34 HistogramLiteral* literal_histograms;
35 size_t literal_histograms_size;
36 HistogramCommand* command_histograms;
37 size_t command_histograms_size;
38 HistogramDistance* distance_histograms;
39 size_t distance_histograms_size;
40 } MetaBlockSplit;
41
InitMetaBlockSplit(MetaBlockSplit * mb)42 static BROTLI_INLINE void InitMetaBlockSplit(MetaBlockSplit* mb) {
43 BrotliInitBlockSplit(&mb->literal_split);
44 BrotliInitBlockSplit(&mb->command_split);
45 BrotliInitBlockSplit(&mb->distance_split);
46 mb->literal_context_map = 0;
47 mb->literal_context_map_size = 0;
48 mb->distance_context_map = 0;
49 mb->distance_context_map_size = 0;
50 mb->literal_histograms = 0;
51 mb->literal_histograms_size = 0;
52 mb->command_histograms = 0;
53 mb->command_histograms_size = 0;
54 mb->distance_histograms = 0;
55 mb->distance_histograms_size = 0;
56 }
57
DestroyMetaBlockSplit(MemoryManager * m,MetaBlockSplit * mb)58 static BROTLI_INLINE void DestroyMetaBlockSplit(
59 MemoryManager* m, MetaBlockSplit* mb) {
60 BrotliDestroyBlockSplit(m, &mb->literal_split);
61 BrotliDestroyBlockSplit(m, &mb->command_split);
62 BrotliDestroyBlockSplit(m, &mb->distance_split);
63 BROTLI_FREE(m, mb->literal_context_map);
64 BROTLI_FREE(m, mb->distance_context_map);
65 BROTLI_FREE(m, mb->literal_histograms);
66 BROTLI_FREE(m, mb->command_histograms);
67 BROTLI_FREE(m, mb->distance_histograms);
68 }
69
70 /* Uses the slow shortest-path block splitter and does context clustering. */
71 BROTLI_INTERNAL void BrotliBuildMetaBlock(MemoryManager* m,
72 const uint8_t* ringbuffer,
73 const size_t pos,
74 const size_t mask,
75 const BrotliEncoderParams* params,
76 uint8_t prev_byte,
77 uint8_t prev_byte2,
78 const Command* cmds,
79 size_t num_commands,
80 ContextType literal_context_mode,
81 MetaBlockSplit* mb);
82
83 /* Uses a fast greedy block splitter that tries to merge current block with the
84 last or the second last block and uses a static context clustering which
85 is the same for all block types. */
86 BROTLI_INTERNAL void BrotliBuildMetaBlockGreedy(
87 MemoryManager* m, const uint8_t* ringbuffer, size_t pos, size_t mask,
88 uint8_t prev_byte, uint8_t prev_byte2, ContextType literal_context_mode,
89 size_t num_contexts, const uint32_t* static_context_map,
90 const Command* commands, size_t n_commands, MetaBlockSplit* mb);
91
92 BROTLI_INTERNAL void BrotliOptimizeHistograms(size_t num_direct_distance_codes,
93 size_t distance_postfix_bits,
94 MetaBlockSplit* mb);
95
96 #if defined(__cplusplus) || defined(c_plusplus)
97 } /* extern "C" */
98 #endif
99
100 #endif /* BROTLI_ENC_METABLOCK_H_ */
101