xref: /third_party/node/deps/brotli/c/enc/metablock.h (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci/* Copyright 2015 Google Inc. All Rights Reserved.
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci   Distributed under MIT license.
41cb0ef41Sopenharmony_ci   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
51cb0ef41Sopenharmony_ci*/
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci/* Algorithms for distributing the literals and commands of a metablock between
81cb0ef41Sopenharmony_ci   block types and contexts. */
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#ifndef BROTLI_ENC_METABLOCK_H_
111cb0ef41Sopenharmony_ci#define BROTLI_ENC_METABLOCK_H_
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci#include "../common/context.h"
141cb0ef41Sopenharmony_ci#include "../common/platform.h"
151cb0ef41Sopenharmony_ci#include <brotli/types.h>
161cb0ef41Sopenharmony_ci#include "./block_splitter.h"
171cb0ef41Sopenharmony_ci#include "./command.h"
181cb0ef41Sopenharmony_ci#include "./histogram.h"
191cb0ef41Sopenharmony_ci#include "./memory.h"
201cb0ef41Sopenharmony_ci#include "./quality.h"
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus)
231cb0ef41Sopenharmony_ciextern "C" {
241cb0ef41Sopenharmony_ci#endif
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_citypedef struct MetaBlockSplit {
271cb0ef41Sopenharmony_ci  BlockSplit literal_split;
281cb0ef41Sopenharmony_ci  BlockSplit command_split;
291cb0ef41Sopenharmony_ci  BlockSplit distance_split;
301cb0ef41Sopenharmony_ci  uint32_t* literal_context_map;
311cb0ef41Sopenharmony_ci  size_t literal_context_map_size;
321cb0ef41Sopenharmony_ci  uint32_t* distance_context_map;
331cb0ef41Sopenharmony_ci  size_t distance_context_map_size;
341cb0ef41Sopenharmony_ci  HistogramLiteral* literal_histograms;
351cb0ef41Sopenharmony_ci  size_t literal_histograms_size;
361cb0ef41Sopenharmony_ci  HistogramCommand* command_histograms;
371cb0ef41Sopenharmony_ci  size_t command_histograms_size;
381cb0ef41Sopenharmony_ci  HistogramDistance* distance_histograms;
391cb0ef41Sopenharmony_ci  size_t distance_histograms_size;
401cb0ef41Sopenharmony_ci} MetaBlockSplit;
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_cistatic BROTLI_INLINE void InitMetaBlockSplit(MetaBlockSplit* mb) {
431cb0ef41Sopenharmony_ci  BrotliInitBlockSplit(&mb->literal_split);
441cb0ef41Sopenharmony_ci  BrotliInitBlockSplit(&mb->command_split);
451cb0ef41Sopenharmony_ci  BrotliInitBlockSplit(&mb->distance_split);
461cb0ef41Sopenharmony_ci  mb->literal_context_map = 0;
471cb0ef41Sopenharmony_ci  mb->literal_context_map_size = 0;
481cb0ef41Sopenharmony_ci  mb->distance_context_map = 0;
491cb0ef41Sopenharmony_ci  mb->distance_context_map_size = 0;
501cb0ef41Sopenharmony_ci  mb->literal_histograms = 0;
511cb0ef41Sopenharmony_ci  mb->literal_histograms_size = 0;
521cb0ef41Sopenharmony_ci  mb->command_histograms = 0;
531cb0ef41Sopenharmony_ci  mb->command_histograms_size = 0;
541cb0ef41Sopenharmony_ci  mb->distance_histograms = 0;
551cb0ef41Sopenharmony_ci  mb->distance_histograms_size = 0;
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_cistatic BROTLI_INLINE void DestroyMetaBlockSplit(
591cb0ef41Sopenharmony_ci    MemoryManager* m, MetaBlockSplit* mb) {
601cb0ef41Sopenharmony_ci  BrotliDestroyBlockSplit(m, &mb->literal_split);
611cb0ef41Sopenharmony_ci  BrotliDestroyBlockSplit(m, &mb->command_split);
621cb0ef41Sopenharmony_ci  BrotliDestroyBlockSplit(m, &mb->distance_split);
631cb0ef41Sopenharmony_ci  BROTLI_FREE(m, mb->literal_context_map);
641cb0ef41Sopenharmony_ci  BROTLI_FREE(m, mb->distance_context_map);
651cb0ef41Sopenharmony_ci  BROTLI_FREE(m, mb->literal_histograms);
661cb0ef41Sopenharmony_ci  BROTLI_FREE(m, mb->command_histograms);
671cb0ef41Sopenharmony_ci  BROTLI_FREE(m, mb->distance_histograms);
681cb0ef41Sopenharmony_ci}
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci/* Uses the slow shortest-path block splitter and does context clustering.
711cb0ef41Sopenharmony_ci   The distance parameters are dynamically selected based on the commands
721cb0ef41Sopenharmony_ci   which get recomputed under the new distance parameters. The new distance
731cb0ef41Sopenharmony_ci   parameters are stored into *params. */
741cb0ef41Sopenharmony_ciBROTLI_INTERNAL void BrotliBuildMetaBlock(MemoryManager* m,
751cb0ef41Sopenharmony_ci                                          const uint8_t* ringbuffer,
761cb0ef41Sopenharmony_ci                                          const size_t pos,
771cb0ef41Sopenharmony_ci                                          const size_t mask,
781cb0ef41Sopenharmony_ci                                          BrotliEncoderParams* params,
791cb0ef41Sopenharmony_ci                                          uint8_t prev_byte,
801cb0ef41Sopenharmony_ci                                          uint8_t prev_byte2,
811cb0ef41Sopenharmony_ci                                          Command* cmds,
821cb0ef41Sopenharmony_ci                                          size_t num_commands,
831cb0ef41Sopenharmony_ci                                          ContextType literal_context_mode,
841cb0ef41Sopenharmony_ci                                          MetaBlockSplit* mb);
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci/* Uses a fast greedy block splitter that tries to merge current block with the
871cb0ef41Sopenharmony_ci   last or the second last block and uses a static context clustering which
881cb0ef41Sopenharmony_ci   is the same for all block types. */
891cb0ef41Sopenharmony_ciBROTLI_INTERNAL void BrotliBuildMetaBlockGreedy(
901cb0ef41Sopenharmony_ci    MemoryManager* m, const uint8_t* ringbuffer, size_t pos, size_t mask,
911cb0ef41Sopenharmony_ci    uint8_t prev_byte, uint8_t prev_byte2, ContextLut literal_context_lut,
921cb0ef41Sopenharmony_ci    size_t num_contexts, const uint32_t* static_context_map,
931cb0ef41Sopenharmony_ci    const Command* commands, size_t n_commands, MetaBlockSplit* mb);
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ciBROTLI_INTERNAL void BrotliOptimizeHistograms(uint32_t num_distance_codes,
961cb0ef41Sopenharmony_ci                                              MetaBlockSplit* mb);
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ciBROTLI_INTERNAL void BrotliInitDistanceParams(BrotliEncoderParams* params,
991cb0ef41Sopenharmony_ci    uint32_t npostfix, uint32_t ndirect);
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus)
1021cb0ef41Sopenharmony_ci}  /* extern "C" */
1031cb0ef41Sopenharmony_ci#endif
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci#endif  /* BROTLI_ENC_METABLOCK_H_ */
106