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