xref: /aosp_15_r20/external/cronet/third_party/brotli/enc/compress_fragment_two_pass.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 /* Function for fast encoding of an input fragment, independently from the input
8    history. This function uses two-pass processing: in the first pass we save
9    the found backward matches and literal bytes into a buffer, and in the
10    second pass we emit them into the bit stream using prefix codes built based
11    on the actual command and literal byte histograms. */
12 
13 #ifndef BROTLI_ENC_COMPRESS_FRAGMENT_TWO_PASS_H_
14 #define BROTLI_ENC_COMPRESS_FRAGMENT_TWO_PASS_H_
15 
16 #include "../common/constants.h"
17 #include "../common/platform.h"
18 #include <brotli/types.h>
19 #include "entropy_encode.h"
20 
21 #if defined(__cplusplus) || defined(c_plusplus)
22 extern "C" {
23 #endif
24 
25 /* TODO(eustas): turn to macro. */
26 static const size_t kCompressFragmentTwoPassBlockSize = 1 << 17;
27 
28 typedef struct BrotliTwoPassArena {
29   uint32_t lit_histo[256];
30   uint8_t lit_depth[256];
31   uint16_t lit_bits[256];
32 
33   uint32_t cmd_histo[128];
34   uint8_t cmd_depth[128];
35   uint16_t cmd_bits[128];
36 
37   /* BuildAndStoreCommandPrefixCode */
38   HuffmanTree tmp_tree[2 * BROTLI_NUM_LITERAL_SYMBOLS + 1];
39   uint8_t tmp_depth[BROTLI_NUM_COMMAND_SYMBOLS];
40   uint16_t tmp_bits[64];
41 } BrotliTwoPassArena;
42 
43 /* Compresses "input" string to the "*storage" buffer as one or more complete
44    meta-blocks, and updates the "*storage_ix" bit position.
45 
46    If "is_last" is 1, emits an additional empty last meta-block.
47 
48    REQUIRES: "input_size" is greater than zero, or "is_last" is 1.
49    REQUIRES: "input_size" is less or equal to maximal metablock size (1 << 24).
50    REQUIRES: "command_buf" and "literal_buf" point to at least
51               kCompressFragmentTwoPassBlockSize long arrays.
52    REQUIRES: All elements in "table[0..table_size-1]" are initialized to zero.
53    REQUIRES: "table_size" is a power of two
54    OUTPUT: maximal copy distance <= |input_size|
55    OUTPUT: maximal copy distance <= BROTLI_MAX_BACKWARD_LIMIT(18) */
56 BROTLI_INTERNAL void BrotliCompressFragmentTwoPass(BrotliTwoPassArena* s,
57                                                    const uint8_t* input,
58                                                    size_t input_size,
59                                                    BROTLI_BOOL is_last,
60                                                    uint32_t* command_buf,
61                                                    uint8_t* literal_buf,
62                                                    int* table,
63                                                    size_t table_size,
64                                                    size_t* storage_ix,
65                                                    uint8_t* storage);
66 
67 #if defined(__cplusplus) || defined(c_plusplus)
68 }  /* extern "C" */
69 #endif
70 
71 #endif  /* BROTLI_ENC_COMPRESS_FRAGMENT_TWO_PASS_H_ */
72