• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "../common/context.h"
14 #include "../common/platform.h"
15 #include <brotli/types.h>
16 #include "./block_splitter.h"
17 #include "./command.h"
18 #include "./histogram.h"
19 #include "./memory.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    The distance parameters are dynamically selected based on the commands
72    which get recomputed under the new distance parameters. The new distance
73    parameters are stored into *params. */
74 BROTLI_INTERNAL void BrotliBuildMetaBlock(MemoryManager* m,
75                                           const uint8_t* ringbuffer,
76                                           const size_t pos,
77                                           const size_t mask,
78                                           BrotliEncoderParams* params,
79                                           uint8_t prev_byte,
80                                           uint8_t prev_byte2,
81                                           Command* cmds,
82                                           size_t num_commands,
83                                           ContextType literal_context_mode,
84                                           MetaBlockSplit* mb);
85 
86 /* Uses a fast greedy block splitter that tries to merge current block with the
87    last or the second last block and uses a static context clustering which
88    is the same for all block types. */
89 BROTLI_INTERNAL void BrotliBuildMetaBlockGreedy(
90     MemoryManager* m, const uint8_t* ringbuffer, size_t pos, size_t mask,
91     uint8_t prev_byte, uint8_t prev_byte2, ContextLut literal_context_lut,
92     size_t num_contexts, const uint32_t* static_context_map,
93     const Command* commands, size_t n_commands, MetaBlockSplit* mb);
94 
95 BROTLI_INTERNAL void BrotliOptimizeHistograms(uint32_t num_distance_codes,
96                                               MetaBlockSplit* mb);
97 
98 BROTLI_INTERNAL void BrotliInitDistanceParams(BrotliEncoderParams* params,
99     uint32_t npostfix, uint32_t ndirect);
100 
101 #if defined(__cplusplus) || defined(c_plusplus)
102 }  /* extern "C" */
103 #endif
104 
105 #endif  /* BROTLI_ENC_METABLOCK_H_ */
106