1*f4ee7fbaSAndroid Build Coastguard Worker /* Copyright 2016 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 /**
8*f4ee7fbaSAndroid Build Coastguard Worker * @file
9*f4ee7fbaSAndroid Build Coastguard Worker * Common constants used in decoder and encoder API.
10*f4ee7fbaSAndroid Build Coastguard Worker */
11*f4ee7fbaSAndroid Build Coastguard Worker
12*f4ee7fbaSAndroid Build Coastguard Worker #ifndef BROTLI_COMMON_CONSTANTS_H_
13*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_COMMON_CONSTANTS_H_
14*f4ee7fbaSAndroid Build Coastguard Worker
15*f4ee7fbaSAndroid Build Coastguard Worker #include "./platform.h"
16*f4ee7fbaSAndroid Build Coastguard Worker #include <brotli/port.h>
17*f4ee7fbaSAndroid Build Coastguard Worker #include <brotli/types.h>
18*f4ee7fbaSAndroid Build Coastguard Worker
19*f4ee7fbaSAndroid Build Coastguard Worker /* Specification: 7.3. Encoding of the context map */
20*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_CONTEXT_MAP_MAX_RLE 16
21*f4ee7fbaSAndroid Build Coastguard Worker
22*f4ee7fbaSAndroid Build Coastguard Worker /* Specification: 2. Compressed representation overview */
23*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_MAX_NUMBER_OF_BLOCK_TYPES 256
24*f4ee7fbaSAndroid Build Coastguard Worker
25*f4ee7fbaSAndroid Build Coastguard Worker /* Specification: 3.3. Alphabet sizes: insert-and-copy length */
26*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_NUM_LITERAL_SYMBOLS 256
27*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_NUM_COMMAND_SYMBOLS 704
28*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_NUM_BLOCK_LEN_SYMBOLS 26
29*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_MAX_CONTEXT_MAP_SYMBOLS (BROTLI_MAX_NUMBER_OF_BLOCK_TYPES + \
30*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_CONTEXT_MAP_MAX_RLE)
31*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_MAX_BLOCK_TYPE_SYMBOLS (BROTLI_MAX_NUMBER_OF_BLOCK_TYPES + 2)
32*f4ee7fbaSAndroid Build Coastguard Worker
33*f4ee7fbaSAndroid Build Coastguard Worker /* Specification: 3.5. Complex prefix codes */
34*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_REPEAT_PREVIOUS_CODE_LENGTH 16
35*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_REPEAT_ZERO_CODE_LENGTH 17
36*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_CODE_LENGTH_CODES (BROTLI_REPEAT_ZERO_CODE_LENGTH + 1)
37*f4ee7fbaSAndroid Build Coastguard Worker /* "code length of 8 is repeated" */
38*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_INITIAL_REPEATED_CODE_LENGTH 8
39*f4ee7fbaSAndroid Build Coastguard Worker
40*f4ee7fbaSAndroid Build Coastguard Worker /* "Large Window Brotli" */
41*f4ee7fbaSAndroid Build Coastguard Worker
42*f4ee7fbaSAndroid Build Coastguard Worker /**
43*f4ee7fbaSAndroid Build Coastguard Worker * The theoretical maximum number of distance bits specified for large window
44*f4ee7fbaSAndroid Build Coastguard Worker * brotli, for 64-bit encoders and decoders. Even when in practice 32-bit
45*f4ee7fbaSAndroid Build Coastguard Worker * encoders and decoders only support up to 30 max distance bits, the value is
46*f4ee7fbaSAndroid Build Coastguard Worker * set to 62 because it affects the large window brotli file format.
47*f4ee7fbaSAndroid Build Coastguard Worker * Specifically, it affects the encoding of simple huffman tree for distances,
48*f4ee7fbaSAndroid Build Coastguard Worker * see Specification RFC 7932 chapter 3.4.
49*f4ee7fbaSAndroid Build Coastguard Worker */
50*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_LARGE_MAX_DISTANCE_BITS 62U
51*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_LARGE_MIN_WBITS 10
52*f4ee7fbaSAndroid Build Coastguard Worker /**
53*f4ee7fbaSAndroid Build Coastguard Worker * The maximum supported large brotli window bits by the encoder and decoder.
54*f4ee7fbaSAndroid Build Coastguard Worker * Large window brotli allows up to 62 bits, however the current encoder and
55*f4ee7fbaSAndroid Build Coastguard Worker * decoder, designed for 32-bit integers, only support up to 30 bits maximum.
56*f4ee7fbaSAndroid Build Coastguard Worker */
57*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_LARGE_MAX_WBITS 30
58*f4ee7fbaSAndroid Build Coastguard Worker
59*f4ee7fbaSAndroid Build Coastguard Worker /* Specification: 4. Encoding of distances */
60*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_NUM_DISTANCE_SHORT_CODES 16
61*f4ee7fbaSAndroid Build Coastguard Worker /**
62*f4ee7fbaSAndroid Build Coastguard Worker * Maximal number of "postfix" bits.
63*f4ee7fbaSAndroid Build Coastguard Worker *
64*f4ee7fbaSAndroid Build Coastguard Worker * Number of "postfix" bits is stored as 2 bits in meta-block header.
65*f4ee7fbaSAndroid Build Coastguard Worker */
66*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_MAX_NPOSTFIX 3
67*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_MAX_NDIRECT 120
68*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_MAX_DISTANCE_BITS 24U
69*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_DISTANCE_ALPHABET_SIZE(NPOSTFIX, NDIRECT, MAXNBITS) ( \
70*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_NUM_DISTANCE_SHORT_CODES + (NDIRECT) + \
71*f4ee7fbaSAndroid Build Coastguard Worker ((MAXNBITS) << ((NPOSTFIX) + 1)))
72*f4ee7fbaSAndroid Build Coastguard Worker /* BROTLI_NUM_DISTANCE_SYMBOLS == 1128 */
73*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_NUM_DISTANCE_SYMBOLS \
74*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DISTANCE_ALPHABET_SIZE( \
75*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_MAX_NDIRECT, BROTLI_MAX_NPOSTFIX, BROTLI_LARGE_MAX_DISTANCE_BITS)
76*f4ee7fbaSAndroid Build Coastguard Worker
77*f4ee7fbaSAndroid Build Coastguard Worker /* ((1 << 26) - 4) is the maximal distance that can be expressed in RFC 7932
78*f4ee7fbaSAndroid Build Coastguard Worker brotli stream using NPOSTFIX = 0 and NDIRECT = 0. With other NPOSTFIX and
79*f4ee7fbaSAndroid Build Coastguard Worker NDIRECT values distances up to ((1 << 29) + 88) could be expressed. */
80*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_MAX_DISTANCE 0x3FFFFFC
81*f4ee7fbaSAndroid Build Coastguard Worker
82*f4ee7fbaSAndroid Build Coastguard Worker /* ((1 << 31) - 4) is the safe distance limit. Using this number as a limit
83*f4ee7fbaSAndroid Build Coastguard Worker allows safe distance calculation without overflows, given the distance
84*f4ee7fbaSAndroid Build Coastguard Worker alphabet size is limited to corresponding size
85*f4ee7fbaSAndroid Build Coastguard Worker (see kLargeWindowDistanceCodeLimits). */
86*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_MAX_ALLOWED_DISTANCE 0x7FFFFFFC
87*f4ee7fbaSAndroid Build Coastguard Worker
88*f4ee7fbaSAndroid Build Coastguard Worker
89*f4ee7fbaSAndroid Build Coastguard Worker /* Specification: 4. Encoding of Literal Insertion Lengths and Copy Lengths */
90*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_NUM_INS_COPY_CODES 24
91*f4ee7fbaSAndroid Build Coastguard Worker
92*f4ee7fbaSAndroid Build Coastguard Worker /* 7.1. Context modes and context ID lookup for literals */
93*f4ee7fbaSAndroid Build Coastguard Worker /* "context IDs for literals are in the range of 0..63" */
94*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_LITERAL_CONTEXT_BITS 6
95*f4ee7fbaSAndroid Build Coastguard Worker
96*f4ee7fbaSAndroid Build Coastguard Worker /* 7.2. Context ID for distances */
97*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_DISTANCE_CONTEXT_BITS 2
98*f4ee7fbaSAndroid Build Coastguard Worker
99*f4ee7fbaSAndroid Build Coastguard Worker /* 9.1. Format of the Stream Header */
100*f4ee7fbaSAndroid Build Coastguard Worker /* Number of slack bytes for window size. Don't confuse
101*f4ee7fbaSAndroid Build Coastguard Worker with BROTLI_NUM_DISTANCE_SHORT_CODES. */
102*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_WINDOW_GAP 16
103*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_MAX_BACKWARD_LIMIT(W) (((size_t)1 << (W)) - BROTLI_WINDOW_GAP)
104*f4ee7fbaSAndroid Build Coastguard Worker
105*f4ee7fbaSAndroid Build Coastguard Worker typedef struct BrotliDistanceCodeLimit {
106*f4ee7fbaSAndroid Build Coastguard Worker uint32_t max_alphabet_size;
107*f4ee7fbaSAndroid Build Coastguard Worker uint32_t max_distance;
108*f4ee7fbaSAndroid Build Coastguard Worker } BrotliDistanceCodeLimit;
109*f4ee7fbaSAndroid Build Coastguard Worker
110*f4ee7fbaSAndroid Build Coastguard Worker /* This function calculates maximal size of distance alphabet, such that the
111*f4ee7fbaSAndroid Build Coastguard Worker distances greater than the given values can not be represented.
112*f4ee7fbaSAndroid Build Coastguard Worker
113*f4ee7fbaSAndroid Build Coastguard Worker This limits are designed to support fast and safe 32-bit decoders.
114*f4ee7fbaSAndroid Build Coastguard Worker "32-bit" means that signed integer values up to ((1 << 31) - 1) could be
115*f4ee7fbaSAndroid Build Coastguard Worker safely expressed.
116*f4ee7fbaSAndroid Build Coastguard Worker
117*f4ee7fbaSAndroid Build Coastguard Worker Brotli distance alphabet symbols do not represent consecutive distance
118*f4ee7fbaSAndroid Build Coastguard Worker ranges. Each distance alphabet symbol (excluding direct distances and short
119*f4ee7fbaSAndroid Build Coastguard Worker codes), represent interleaved (for NPOSTFIX > 0) range of distances.
120*f4ee7fbaSAndroid Build Coastguard Worker A "group" of consecutive (1 << NPOSTFIX) symbols represent non-interleaved
121*f4ee7fbaSAndroid Build Coastguard Worker range. Two consecutive groups require the same amount of "extra bits".
122*f4ee7fbaSAndroid Build Coastguard Worker
123*f4ee7fbaSAndroid Build Coastguard Worker It is important that distance alphabet represents complete "groups".
124*f4ee7fbaSAndroid Build Coastguard Worker To avoid complex logic on encoder side about interleaved ranges
125*f4ee7fbaSAndroid Build Coastguard Worker it was decided to restrict both sides to complete distance code "groups".
126*f4ee7fbaSAndroid Build Coastguard Worker */
BrotliCalculateDistanceCodeLimit(uint32_t max_distance,uint32_t npostfix,uint32_t ndirect)127*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_UNUSED_FUNCTION BrotliDistanceCodeLimit BrotliCalculateDistanceCodeLimit(
128*f4ee7fbaSAndroid Build Coastguard Worker uint32_t max_distance, uint32_t npostfix, uint32_t ndirect) {
129*f4ee7fbaSAndroid Build Coastguard Worker BrotliDistanceCodeLimit result;
130*f4ee7fbaSAndroid Build Coastguard Worker /* Marking this function as unused, because not all files
131*f4ee7fbaSAndroid Build Coastguard Worker including "constants.h" use it -> compiler warns about that. */
132*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_UNUSED(&BrotliCalculateDistanceCodeLimit);
133*f4ee7fbaSAndroid Build Coastguard Worker if (max_distance <= ndirect) {
134*f4ee7fbaSAndroid Build Coastguard Worker /* This case never happens / exists only for the sake of completeness. */
135*f4ee7fbaSAndroid Build Coastguard Worker result.max_alphabet_size = max_distance + BROTLI_NUM_DISTANCE_SHORT_CODES;
136*f4ee7fbaSAndroid Build Coastguard Worker result.max_distance = max_distance;
137*f4ee7fbaSAndroid Build Coastguard Worker return result;
138*f4ee7fbaSAndroid Build Coastguard Worker } else {
139*f4ee7fbaSAndroid Build Coastguard Worker /* The first prohibited value. */
140*f4ee7fbaSAndroid Build Coastguard Worker uint32_t forbidden_distance = max_distance + 1;
141*f4ee7fbaSAndroid Build Coastguard Worker /* Subtract "directly" encoded region. */
142*f4ee7fbaSAndroid Build Coastguard Worker uint32_t offset = forbidden_distance - ndirect - 1;
143*f4ee7fbaSAndroid Build Coastguard Worker uint32_t ndistbits = 0;
144*f4ee7fbaSAndroid Build Coastguard Worker uint32_t tmp;
145*f4ee7fbaSAndroid Build Coastguard Worker uint32_t half;
146*f4ee7fbaSAndroid Build Coastguard Worker uint32_t group;
147*f4ee7fbaSAndroid Build Coastguard Worker /* Postfix for the last dcode in the group. */
148*f4ee7fbaSAndroid Build Coastguard Worker uint32_t postfix = (1u << npostfix) - 1;
149*f4ee7fbaSAndroid Build Coastguard Worker uint32_t extra;
150*f4ee7fbaSAndroid Build Coastguard Worker uint32_t start;
151*f4ee7fbaSAndroid Build Coastguard Worker /* Remove postfix and "head-start". */
152*f4ee7fbaSAndroid Build Coastguard Worker offset = (offset >> npostfix) + 4;
153*f4ee7fbaSAndroid Build Coastguard Worker /* Calculate the number of distance bits. */
154*f4ee7fbaSAndroid Build Coastguard Worker tmp = offset / 2;
155*f4ee7fbaSAndroid Build Coastguard Worker /* Poor-man's log2floor, to avoid extra dependencies. */
156*f4ee7fbaSAndroid Build Coastguard Worker while (tmp != 0) {ndistbits++; tmp = tmp >> 1;}
157*f4ee7fbaSAndroid Build Coastguard Worker /* One bit is covered with subrange addressing ("half"). */
158*f4ee7fbaSAndroid Build Coastguard Worker ndistbits--;
159*f4ee7fbaSAndroid Build Coastguard Worker /* Find subrange. */
160*f4ee7fbaSAndroid Build Coastguard Worker half = (offset >> ndistbits) & 1;
161*f4ee7fbaSAndroid Build Coastguard Worker /* Calculate the "group" part of dcode. */
162*f4ee7fbaSAndroid Build Coastguard Worker group = ((ndistbits - 1) << 1) | half;
163*f4ee7fbaSAndroid Build Coastguard Worker /* Calculated "group" covers the prohibited distance value. */
164*f4ee7fbaSAndroid Build Coastguard Worker if (group == 0) {
165*f4ee7fbaSAndroid Build Coastguard Worker /* This case is added for correctness; does not occur for limit > 128. */
166*f4ee7fbaSAndroid Build Coastguard Worker result.max_alphabet_size = ndirect + BROTLI_NUM_DISTANCE_SHORT_CODES;
167*f4ee7fbaSAndroid Build Coastguard Worker result.max_distance = ndirect;
168*f4ee7fbaSAndroid Build Coastguard Worker return result;
169*f4ee7fbaSAndroid Build Coastguard Worker }
170*f4ee7fbaSAndroid Build Coastguard Worker /* Decrement "group", so it is the last permitted "group". */
171*f4ee7fbaSAndroid Build Coastguard Worker group--;
172*f4ee7fbaSAndroid Build Coastguard Worker /* After group was decremented, ndistbits and half must be recalculated. */
173*f4ee7fbaSAndroid Build Coastguard Worker ndistbits = (group >> 1) + 1;
174*f4ee7fbaSAndroid Build Coastguard Worker /* The last available distance in the subrange has all extra bits set. */
175*f4ee7fbaSAndroid Build Coastguard Worker extra = (1u << ndistbits) - 1;
176*f4ee7fbaSAndroid Build Coastguard Worker /* Calculate region start. NB: ndistbits >= 1. */
177*f4ee7fbaSAndroid Build Coastguard Worker start = (1u << (ndistbits + 1)) - 4;
178*f4ee7fbaSAndroid Build Coastguard Worker /* Move to subregion. */
179*f4ee7fbaSAndroid Build Coastguard Worker start += (group & 1) << ndistbits;
180*f4ee7fbaSAndroid Build Coastguard Worker /* Calculate the alphabet size. */
181*f4ee7fbaSAndroid Build Coastguard Worker result.max_alphabet_size = ((group << npostfix) | postfix) + ndirect +
182*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_NUM_DISTANCE_SHORT_CODES + 1;
183*f4ee7fbaSAndroid Build Coastguard Worker /* Calculate the maximal distance representable by alphabet. */
184*f4ee7fbaSAndroid Build Coastguard Worker result.max_distance = ((start + extra) << npostfix) + postfix + ndirect + 1;
185*f4ee7fbaSAndroid Build Coastguard Worker return result;
186*f4ee7fbaSAndroid Build Coastguard Worker }
187*f4ee7fbaSAndroid Build Coastguard Worker }
188*f4ee7fbaSAndroid Build Coastguard Worker
189*f4ee7fbaSAndroid Build Coastguard Worker /* Represents the range of values belonging to a prefix code:
190*f4ee7fbaSAndroid Build Coastguard Worker [offset, offset + 2^nbits) */
191*f4ee7fbaSAndroid Build Coastguard Worker typedef struct {
192*f4ee7fbaSAndroid Build Coastguard Worker uint16_t offset;
193*f4ee7fbaSAndroid Build Coastguard Worker uint8_t nbits;
194*f4ee7fbaSAndroid Build Coastguard Worker } BrotliPrefixCodeRange;
195*f4ee7fbaSAndroid Build Coastguard Worker
196*f4ee7fbaSAndroid Build Coastguard Worker /* "Soft-private", it is exported, but not "advertised" as API. */
197*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_COMMON_API extern const BrotliPrefixCodeRange
198*f4ee7fbaSAndroid Build Coastguard Worker _kBrotliPrefixCodeRanges[BROTLI_NUM_BLOCK_LEN_SYMBOLS];
199*f4ee7fbaSAndroid Build Coastguard Worker
200*f4ee7fbaSAndroid Build Coastguard Worker #endif /* BROTLI_COMMON_CONSTANTS_H_ */
201