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