11cb0ef41Sopenharmony_ci/* Copyright 2016 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/** 81cb0ef41Sopenharmony_ci * @file 91cb0ef41Sopenharmony_ci * Common constants used in decoder and encoder API. 101cb0ef41Sopenharmony_ci */ 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci#ifndef BROTLI_COMMON_CONSTANTS_H_ 131cb0ef41Sopenharmony_ci#define BROTLI_COMMON_CONSTANTS_H_ 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci#include "./platform.h" 161cb0ef41Sopenharmony_ci#include <brotli/port.h> 171cb0ef41Sopenharmony_ci#include <brotli/types.h> 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ci/* Specification: 7.3. Encoding of the context map */ 201cb0ef41Sopenharmony_ci#define BROTLI_CONTEXT_MAP_MAX_RLE 16 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci/* Specification: 2. Compressed representation overview */ 231cb0ef41Sopenharmony_ci#define BROTLI_MAX_NUMBER_OF_BLOCK_TYPES 256 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci/* Specification: 3.3. Alphabet sizes: insert-and-copy length */ 261cb0ef41Sopenharmony_ci#define BROTLI_NUM_LITERAL_SYMBOLS 256 271cb0ef41Sopenharmony_ci#define BROTLI_NUM_COMMAND_SYMBOLS 704 281cb0ef41Sopenharmony_ci#define BROTLI_NUM_BLOCK_LEN_SYMBOLS 26 291cb0ef41Sopenharmony_ci#define BROTLI_MAX_CONTEXT_MAP_SYMBOLS (BROTLI_MAX_NUMBER_OF_BLOCK_TYPES + \ 301cb0ef41Sopenharmony_ci BROTLI_CONTEXT_MAP_MAX_RLE) 311cb0ef41Sopenharmony_ci#define BROTLI_MAX_BLOCK_TYPE_SYMBOLS (BROTLI_MAX_NUMBER_OF_BLOCK_TYPES + 2) 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci/* Specification: 3.5. Complex prefix codes */ 341cb0ef41Sopenharmony_ci#define BROTLI_REPEAT_PREVIOUS_CODE_LENGTH 16 351cb0ef41Sopenharmony_ci#define BROTLI_REPEAT_ZERO_CODE_LENGTH 17 361cb0ef41Sopenharmony_ci#define BROTLI_CODE_LENGTH_CODES (BROTLI_REPEAT_ZERO_CODE_LENGTH + 1) 371cb0ef41Sopenharmony_ci/* "code length of 8 is repeated" */ 381cb0ef41Sopenharmony_ci#define BROTLI_INITIAL_REPEATED_CODE_LENGTH 8 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci/* "Large Window Brotli" */ 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci/** 431cb0ef41Sopenharmony_ci * The theoretical maximum number of distance bits specified for large window 441cb0ef41Sopenharmony_ci * brotli, for 64-bit encoders and decoders. Even when in practice 32-bit 451cb0ef41Sopenharmony_ci * encoders and decoders only support up to 30 max distance bits, the value is 461cb0ef41Sopenharmony_ci * set to 62 because it affects the large window brotli file format. 471cb0ef41Sopenharmony_ci * Specifically, it affects the encoding of simple huffman tree for distances, 481cb0ef41Sopenharmony_ci * see Specification RFC 7932 chapter 3.4. 491cb0ef41Sopenharmony_ci */ 501cb0ef41Sopenharmony_ci#define BROTLI_LARGE_MAX_DISTANCE_BITS 62U 511cb0ef41Sopenharmony_ci#define BROTLI_LARGE_MIN_WBITS 10 521cb0ef41Sopenharmony_ci/** 531cb0ef41Sopenharmony_ci * The maximum supported large brotli window bits by the encoder and decoder. 541cb0ef41Sopenharmony_ci * Large window brotli allows up to 62 bits, however the current encoder and 551cb0ef41Sopenharmony_ci * decoder, designed for 32-bit integers, only support up to 30 bits maximum. 561cb0ef41Sopenharmony_ci */ 571cb0ef41Sopenharmony_ci#define BROTLI_LARGE_MAX_WBITS 30 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci/* Specification: 4. Encoding of distances */ 601cb0ef41Sopenharmony_ci#define BROTLI_NUM_DISTANCE_SHORT_CODES 16 611cb0ef41Sopenharmony_ci/** 621cb0ef41Sopenharmony_ci * Maximal number of "postfix" bits. 631cb0ef41Sopenharmony_ci * 641cb0ef41Sopenharmony_ci * Number of "postfix" bits is stored as 2 bits in meta-block header. 651cb0ef41Sopenharmony_ci */ 661cb0ef41Sopenharmony_ci#define BROTLI_MAX_NPOSTFIX 3 671cb0ef41Sopenharmony_ci#define BROTLI_MAX_NDIRECT 120 681cb0ef41Sopenharmony_ci#define BROTLI_MAX_DISTANCE_BITS 24U 691cb0ef41Sopenharmony_ci#define BROTLI_DISTANCE_ALPHABET_SIZE(NPOSTFIX, NDIRECT, MAXNBITS) ( \ 701cb0ef41Sopenharmony_ci BROTLI_NUM_DISTANCE_SHORT_CODES + (NDIRECT) + \ 711cb0ef41Sopenharmony_ci ((MAXNBITS) << ((NPOSTFIX) + 1))) 721cb0ef41Sopenharmony_ci/* BROTLI_NUM_DISTANCE_SYMBOLS == 1128 */ 731cb0ef41Sopenharmony_ci#define BROTLI_NUM_DISTANCE_SYMBOLS \ 741cb0ef41Sopenharmony_ci BROTLI_DISTANCE_ALPHABET_SIZE( \ 751cb0ef41Sopenharmony_ci BROTLI_MAX_NDIRECT, BROTLI_MAX_NPOSTFIX, BROTLI_LARGE_MAX_DISTANCE_BITS) 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci/* ((1 << 26) - 4) is the maximal distance that can be expressed in RFC 7932 781cb0ef41Sopenharmony_ci brotli stream using NPOSTFIX = 0 and NDIRECT = 0. With other NPOSTFIX and 791cb0ef41Sopenharmony_ci NDIRECT values distances up to ((1 << 29) + 88) could be expressed. */ 801cb0ef41Sopenharmony_ci#define BROTLI_MAX_DISTANCE 0x3FFFFFC 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci/* ((1 << 31) - 4) is the safe distance limit. Using this number as a limit 831cb0ef41Sopenharmony_ci allows safe distance calculation without overflows, given the distance 841cb0ef41Sopenharmony_ci alphabet size is limited to corresponding size 851cb0ef41Sopenharmony_ci (see kLargeWindowDistanceCodeLimits). */ 861cb0ef41Sopenharmony_ci#define BROTLI_MAX_ALLOWED_DISTANCE 0x7FFFFFFC 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci/* Specification: 4. Encoding of Literal Insertion Lengths and Copy Lengths */ 901cb0ef41Sopenharmony_ci#define BROTLI_NUM_INS_COPY_CODES 24 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci/* 7.1. Context modes and context ID lookup for literals */ 931cb0ef41Sopenharmony_ci/* "context IDs for literals are in the range of 0..63" */ 941cb0ef41Sopenharmony_ci#define BROTLI_LITERAL_CONTEXT_BITS 6 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci/* 7.2. Context ID for distances */ 971cb0ef41Sopenharmony_ci#define BROTLI_DISTANCE_CONTEXT_BITS 2 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci/* 9.1. Format of the Stream Header */ 1001cb0ef41Sopenharmony_ci/* Number of slack bytes for window size. Don't confuse 1011cb0ef41Sopenharmony_ci with BROTLI_NUM_DISTANCE_SHORT_CODES. */ 1021cb0ef41Sopenharmony_ci#define BROTLI_WINDOW_GAP 16 1031cb0ef41Sopenharmony_ci#define BROTLI_MAX_BACKWARD_LIMIT(W) (((size_t)1 << (W)) - BROTLI_WINDOW_GAP) 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_citypedef struct BrotliDistanceCodeLimit { 1061cb0ef41Sopenharmony_ci uint32_t max_alphabet_size; 1071cb0ef41Sopenharmony_ci uint32_t max_distance; 1081cb0ef41Sopenharmony_ci} BrotliDistanceCodeLimit; 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci/* This function calculates maximal size of distance alphabet, such that the 1111cb0ef41Sopenharmony_ci distances greater than the given values can not be represented. 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci This limits are designed to support fast and safe 32-bit decoders. 1141cb0ef41Sopenharmony_ci "32-bit" means that signed integer values up to ((1 << 31) - 1) could be 1151cb0ef41Sopenharmony_ci safely expressed. 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci Brotli distance alphabet symbols do not represent consecutive distance 1181cb0ef41Sopenharmony_ci ranges. Each distance alphabet symbol (excluding direct distances and short 1191cb0ef41Sopenharmony_ci codes), represent interleaved (for NPOSTFIX > 0) range of distances. 1201cb0ef41Sopenharmony_ci A "group" of consecutive (1 << NPOSTFIX) symbols represent non-interleaved 1211cb0ef41Sopenharmony_ci range. Two consecutive groups require the same amount of "extra bits". 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci It is important that distance alphabet represents complete "groups". 1241cb0ef41Sopenharmony_ci To avoid complex logic on encoder side about interleaved ranges 1251cb0ef41Sopenharmony_ci it was decided to restrict both sides to complete distance code "groups". 1261cb0ef41Sopenharmony_ci */ 1271cb0ef41Sopenharmony_ciBROTLI_UNUSED_FUNCTION BrotliDistanceCodeLimit BrotliCalculateDistanceCodeLimit( 1281cb0ef41Sopenharmony_ci uint32_t max_distance, uint32_t npostfix, uint32_t ndirect) { 1291cb0ef41Sopenharmony_ci BrotliDistanceCodeLimit result; 1301cb0ef41Sopenharmony_ci /* Marking this function as unused, because not all files 1311cb0ef41Sopenharmony_ci including "constants.h" use it -> compiler warns about that. */ 1321cb0ef41Sopenharmony_ci BROTLI_UNUSED(&BrotliCalculateDistanceCodeLimit); 1331cb0ef41Sopenharmony_ci if (max_distance <= ndirect) { 1341cb0ef41Sopenharmony_ci /* This case never happens / exists only for the sake of completeness. */ 1351cb0ef41Sopenharmony_ci result.max_alphabet_size = max_distance + BROTLI_NUM_DISTANCE_SHORT_CODES; 1361cb0ef41Sopenharmony_ci result.max_distance = max_distance; 1371cb0ef41Sopenharmony_ci return result; 1381cb0ef41Sopenharmony_ci } else { 1391cb0ef41Sopenharmony_ci /* The first prohibited value. */ 1401cb0ef41Sopenharmony_ci uint32_t forbidden_distance = max_distance + 1; 1411cb0ef41Sopenharmony_ci /* Subtract "directly" encoded region. */ 1421cb0ef41Sopenharmony_ci uint32_t offset = forbidden_distance - ndirect - 1; 1431cb0ef41Sopenharmony_ci uint32_t ndistbits = 0; 1441cb0ef41Sopenharmony_ci uint32_t tmp; 1451cb0ef41Sopenharmony_ci uint32_t half; 1461cb0ef41Sopenharmony_ci uint32_t group; 1471cb0ef41Sopenharmony_ci /* Postfix for the last dcode in the group. */ 1481cb0ef41Sopenharmony_ci uint32_t postfix = (1u << npostfix) - 1; 1491cb0ef41Sopenharmony_ci uint32_t extra; 1501cb0ef41Sopenharmony_ci uint32_t start; 1511cb0ef41Sopenharmony_ci /* Remove postfix and "head-start". */ 1521cb0ef41Sopenharmony_ci offset = (offset >> npostfix) + 4; 1531cb0ef41Sopenharmony_ci /* Calculate the number of distance bits. */ 1541cb0ef41Sopenharmony_ci tmp = offset / 2; 1551cb0ef41Sopenharmony_ci /* Poor-man's log2floor, to avoid extra dependencies. */ 1561cb0ef41Sopenharmony_ci while (tmp != 0) {ndistbits++; tmp = tmp >> 1;} 1571cb0ef41Sopenharmony_ci /* One bit is covered with subrange addressing ("half"). */ 1581cb0ef41Sopenharmony_ci ndistbits--; 1591cb0ef41Sopenharmony_ci /* Find subrange. */ 1601cb0ef41Sopenharmony_ci half = (offset >> ndistbits) & 1; 1611cb0ef41Sopenharmony_ci /* Calculate the "group" part of dcode. */ 1621cb0ef41Sopenharmony_ci group = ((ndistbits - 1) << 1) | half; 1631cb0ef41Sopenharmony_ci /* Calculated "group" covers the prohibited distance value. */ 1641cb0ef41Sopenharmony_ci if (group == 0) { 1651cb0ef41Sopenharmony_ci /* This case is added for correctness; does not occur for limit > 128. */ 1661cb0ef41Sopenharmony_ci result.max_alphabet_size = ndirect + BROTLI_NUM_DISTANCE_SHORT_CODES; 1671cb0ef41Sopenharmony_ci result.max_distance = ndirect; 1681cb0ef41Sopenharmony_ci return result; 1691cb0ef41Sopenharmony_ci } 1701cb0ef41Sopenharmony_ci /* Decrement "group", so it is the last permitted "group". */ 1711cb0ef41Sopenharmony_ci group--; 1721cb0ef41Sopenharmony_ci /* After group was decremented, ndistbits and half must be recalculated. */ 1731cb0ef41Sopenharmony_ci ndistbits = (group >> 1) + 1; 1741cb0ef41Sopenharmony_ci /* The last available distance in the subrange has all extra bits set. */ 1751cb0ef41Sopenharmony_ci extra = (1u << ndistbits) - 1; 1761cb0ef41Sopenharmony_ci /* Calculate region start. NB: ndistbits >= 1. */ 1771cb0ef41Sopenharmony_ci start = (1u << (ndistbits + 1)) - 4; 1781cb0ef41Sopenharmony_ci /* Move to subregion. */ 1791cb0ef41Sopenharmony_ci start += (group & 1) << ndistbits; 1801cb0ef41Sopenharmony_ci /* Calculate the alphabet size. */ 1811cb0ef41Sopenharmony_ci result.max_alphabet_size = ((group << npostfix) | postfix) + ndirect + 1821cb0ef41Sopenharmony_ci BROTLI_NUM_DISTANCE_SHORT_CODES + 1; 1831cb0ef41Sopenharmony_ci /* Calculate the maximal distance representable by alphabet. */ 1841cb0ef41Sopenharmony_ci result.max_distance = ((start + extra) << npostfix) + postfix + ndirect + 1; 1851cb0ef41Sopenharmony_ci return result; 1861cb0ef41Sopenharmony_ci } 1871cb0ef41Sopenharmony_ci} 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci/* Represents the range of values belonging to a prefix code: 1901cb0ef41Sopenharmony_ci [offset, offset + 2^nbits) */ 1911cb0ef41Sopenharmony_citypedef struct { 1921cb0ef41Sopenharmony_ci uint16_t offset; 1931cb0ef41Sopenharmony_ci uint8_t nbits; 1941cb0ef41Sopenharmony_ci} BrotliPrefixCodeRange; 1951cb0ef41Sopenharmony_ci 1961cb0ef41Sopenharmony_ci/* "Soft-private", it is exported, but not "advertised" as API. */ 1971cb0ef41Sopenharmony_ciBROTLI_COMMON_API extern const BrotliPrefixCodeRange 1981cb0ef41Sopenharmony_ci _kBrotliPrefixCodeRanges[BROTLI_NUM_BLOCK_LEN_SYMBOLS]; 1991cb0ef41Sopenharmony_ci 2001cb0ef41Sopenharmony_ci#endif /* BROTLI_COMMON_CONSTANTS_H_ */ 201