|
4 | 4 | See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
5 | 5 | */
|
6 | 6 |
|
| 7 | +/** |
| 8 | + * @file |
| 9 | + * Common constants used in decoder and encoder API. |
| 10 | + */ |
| 11 | + |
7 | 12 | #ifndef BROTLI_COMMON_CONSTANTS_H_
|
8 | 13 | #define BROTLI_COMMON_CONSTANTS_H_
|
9 | 14 |
|
| 15 | +#include "./platform.h" |
| 16 | +#include <brotli/types.h> |
| 17 | + |
10 | 18 | /* Specification: 7.3. Encoding of the context map */
|
11 | 19 | #define BROTLI_CONTEXT_MAP_MAX_RLE 16
|
12 | 20 |
|
|
29 | 37 | #define BROTLI_INITIAL_REPEATED_CODE_LENGTH 8
|
30 | 38 |
|
31 | 39 | /* "Large Window Brotli" */
|
| 40 | + |
| 41 | +/** |
| 42 | + * The theoretical maximum number of distance bits specified for large window |
| 43 | + * brotli, for 64-bit encoders and decoders. Even when in practice 32-bit |
| 44 | + * encoders and decoders only support up to 30 max distance bits, the value is |
| 45 | + * set to 62 because it affects the large window brotli file format. |
| 46 | + * Specifically, it affects the encoding of simple huffman tree for distances, |
| 47 | + * see Specification RFC 7932 chapter 3.4. |
| 48 | + */ |
32 | 49 | #define BROTLI_LARGE_MAX_DISTANCE_BITS 62U
|
33 | 50 | #define BROTLI_LARGE_MIN_WBITS 10
|
| 51 | +/** |
| 52 | + * The maximum supported large brotli window bits by the encoder and decoder. |
| 53 | + * Large window brotli allows up to 62 bits, however the current encoder and |
| 54 | + * decoder, designed for 32-bit integers, only support up to 30 bits maximum. |
| 55 | + */ |
34 | 56 | #define BROTLI_LARGE_MAX_WBITS 30
|
35 | 57 |
|
36 | 58 | /* Specification: 4. Encoding of distances */
|
37 | 59 | #define BROTLI_NUM_DISTANCE_SHORT_CODES 16
|
| 60 | +/** |
| 61 | + * Maximal number of "postfix" bits. |
| 62 | + * |
| 63 | + * Number of "postfix" bits is stored as 2 bits in meta-block header. |
| 64 | + */ |
38 | 65 | #define BROTLI_MAX_NPOSTFIX 3
|
39 | 66 | #define BROTLI_MAX_NDIRECT 120
|
40 | 67 | #define BROTLI_MAX_DISTANCE_BITS 24U
|
|
45 | 72 | #define BROTLI_NUM_DISTANCE_SYMBOLS \
|
46 | 73 | BROTLI_DISTANCE_ALPHABET_SIZE( \
|
47 | 74 | BROTLI_MAX_NDIRECT, BROTLI_MAX_NPOSTFIX, BROTLI_LARGE_MAX_DISTANCE_BITS)
|
| 75 | + |
| 76 | +/* ((1 << 26) - 4) is the maximal distance that can be expressed in RFC 7932 |
| 77 | + brotli stream using NPOSTFIX = 0 and NDIRECT = 0. With other NPOSTFIX and |
| 78 | + NDIRECT values distances up to ((1 << 29) + 88) could be expressed. */ |
48 | 79 | #define BROTLI_MAX_DISTANCE 0x3FFFFFC
|
| 80 | + |
| 81 | +/* ((1 << 31) - 4) is the safe distance limit. Using this number as a limit |
| 82 | + allows safe distance calculation without overflows, given the distance |
| 83 | + alphabet size is limited to corresponding size |
| 84 | + (see kLargeWindowDistanceCodeLimits). */ |
49 | 85 | #define BROTLI_MAX_ALLOWED_DISTANCE 0x7FFFFFFC
|
50 | 86 |
|
51 | 87 | /* 7.1. Context modes and context ID lookup for literals */
|
|
61 | 97 | #define BROTLI_WINDOW_GAP 16
|
62 | 98 | #define BROTLI_MAX_BACKWARD_LIMIT(W) (((size_t)1 << (W)) - BROTLI_WINDOW_GAP)
|
63 | 99 |
|
| 100 | +typedef struct BrotliDistanceCodeLimit { |
| 101 | + uint32_t max_alphabet_size; |
| 102 | + uint32_t max_distance; |
| 103 | +} BrotliDistanceCodeLimit; |
| 104 | + |
| 105 | +/* This function calculates maximal size of distance alphabet, such that the |
| 106 | + distances greater than the given values can not be represented. |
| 107 | +
|
| 108 | + This limits are designed to support fast and safe 32-bit decoders. |
| 109 | + "32-bit" means that signed integer values up to ((1 << 31) - 1) could be |
| 110 | + safely expressed. |
| 111 | +
|
| 112 | + Brotli distance alphabet symbols do not represent consecutive distance |
| 113 | + ranges. Each distance alphabet symbol (excluding direct distances and short |
| 114 | + codes), represent interleaved (for NPOSTFIX > 0) range of distances. |
| 115 | + A "group" of consecutive (1 << NPOSTFIX) symbols represent non-interleaved |
| 116 | + range. Two consecutive groups require the same amount of "extra bits". |
| 117 | +
|
| 118 | + It is important that distance alphabet represents complete "groups". |
| 119 | + To avoid complex logic on encoder side about interleaved ranges |
| 120 | + it was decided to restrict both sides to complete distance code "groups". |
| 121 | + */ |
| 122 | +BROTLI_UNUSED_FUNCTION BrotliDistanceCodeLimit BrotliCalculateDistanceCodeLimit( |
| 123 | + uint32_t max_distance, uint32_t npostfix, uint32_t ndirect) { |
| 124 | + BrotliDistanceCodeLimit result; |
| 125 | + /* Marking this function as unused, because not all files |
| 126 | + including "constants.h" use it -> compiler warns about that. */ |
| 127 | + BROTLI_UNUSED(&BrotliCalculateDistanceCodeLimit); |
| 128 | + if (max_distance <= ndirect) { |
| 129 | + /* This case never happens / exists only for the sake of completeness. */ |
| 130 | + result.max_alphabet_size = max_distance + BROTLI_NUM_DISTANCE_SHORT_CODES; |
| 131 | + result.max_distance = max_distance; |
| 132 | + return result; |
| 133 | + } else { |
| 134 | + /* The first prohibited value. */ |
| 135 | + uint32_t forbidden_distance = max_distance + 1; |
| 136 | + /* Subtract "directly" encoded region. */ |
| 137 | + uint32_t offset = forbidden_distance - ndirect - 1; |
| 138 | + uint32_t ndistbits = 0; |
| 139 | + uint32_t tmp; |
| 140 | + uint32_t half; |
| 141 | + uint32_t group; |
| 142 | + /* Postfix for the last dcode in the group. */ |
| 143 | + uint32_t postfix = (1u << npostfix) - 1; |
| 144 | + uint32_t extra; |
| 145 | + uint32_t start; |
| 146 | + /* Remove postfix and "head-start". */ |
| 147 | + offset = (offset >> npostfix) + 4; |
| 148 | + /* Calculate the number of distance bits. */ |
| 149 | + tmp = offset / 2; |
| 150 | + /* Poor-man's log2floor, to avoid extra dependencies. */ |
| 151 | + while (tmp != 0) {ndistbits++; tmp = tmp >> 1;} |
| 152 | + /* One bit is covered with subrange addressing ("half"). */ |
| 153 | + ndistbits--; |
| 154 | + /* Find subrange. */ |
| 155 | + half = (offset >> ndistbits) & 1; |
| 156 | + /* Calculate the "group" part of dcode. */ |
| 157 | + group = ((ndistbits - 1) << 1) | half; |
| 158 | + /* Calculated "group" covers the prohibited distance value. */ |
| 159 | + if (group == 0) { |
| 160 | + /* This case is added for correctness; does not occur for limit > 128. */ |
| 161 | + result.max_alphabet_size = ndirect + BROTLI_NUM_DISTANCE_SHORT_CODES; |
| 162 | + result.max_distance = ndirect; |
| 163 | + return result; |
| 164 | + } |
| 165 | + /* Decrement "group", so it is the last permitted "group". */ |
| 166 | + group--; |
| 167 | + /* After group was decremented, ndistbits and half must be recalculated. */ |
| 168 | + ndistbits = (group >> 1) + 1; |
| 169 | + /* The last available distance in the subrange has all extra bits set. */ |
| 170 | + extra = (1u << ndistbits) - 1; |
| 171 | + /* Calculate region start. NB: ndistbits >= 1. */ |
| 172 | + start = (1u << (ndistbits + 1)) - 4; |
| 173 | + /* Move to subregion. */ |
| 174 | + start += (group & 1) << ndistbits; |
| 175 | + /* Calculate the alphabet size. */ |
| 176 | + result.max_alphabet_size = ((group << npostfix) | postfix) + ndirect + |
| 177 | + BROTLI_NUM_DISTANCE_SHORT_CODES + 1; |
| 178 | + /* Calculate the maximal distance representable by alphabet. */ |
| 179 | + result.max_distance = ((start + extra) << npostfix) + postfix + ndirect + 1; |
| 180 | + return result; |
| 181 | + } |
| 182 | +} |
| 183 | + |
64 | 184 | #endif /* BROTLI_COMMON_CONSTANTS_H_ */
|
0 commit comments