11cb0ef41Sopenharmony_ci/* Copyright 2013 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/* This class models a sequence of literals and a backward reference copy. */ 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci#ifndef BROTLI_ENC_COMMAND_H_ 101cb0ef41Sopenharmony_ci#define BROTLI_ENC_COMMAND_H_ 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci#include "../common/constants.h" 131cb0ef41Sopenharmony_ci#include "../common/platform.h" 141cb0ef41Sopenharmony_ci#include <brotli/types.h> 151cb0ef41Sopenharmony_ci#include "./fast_log.h" 161cb0ef41Sopenharmony_ci#include "./params.h" 171cb0ef41Sopenharmony_ci#include "./prefix.h" 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus) 201cb0ef41Sopenharmony_ciextern "C" { 211cb0ef41Sopenharmony_ci#endif 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciBROTLI_INTERNAL extern const uint32_t 241cb0ef41Sopenharmony_ci kBrotliInsBase[BROTLI_NUM_INS_COPY_CODES]; 251cb0ef41Sopenharmony_ciBROTLI_INTERNAL extern const uint32_t 261cb0ef41Sopenharmony_ci kBrotliInsExtra[BROTLI_NUM_INS_COPY_CODES]; 271cb0ef41Sopenharmony_ciBROTLI_INTERNAL extern const uint32_t 281cb0ef41Sopenharmony_ci kBrotliCopyBase[BROTLI_NUM_INS_COPY_CODES]; 291cb0ef41Sopenharmony_ciBROTLI_INTERNAL extern const uint32_t 301cb0ef41Sopenharmony_ci kBrotliCopyExtra[BROTLI_NUM_INS_COPY_CODES]; 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint16_t GetInsertLengthCode(size_t insertlen) { 331cb0ef41Sopenharmony_ci if (insertlen < 6) { 341cb0ef41Sopenharmony_ci return (uint16_t)insertlen; 351cb0ef41Sopenharmony_ci } else if (insertlen < 130) { 361cb0ef41Sopenharmony_ci uint32_t nbits = Log2FloorNonZero(insertlen - 2) - 1u; 371cb0ef41Sopenharmony_ci return (uint16_t)((nbits << 1) + ((insertlen - 2) >> nbits) + 2); 381cb0ef41Sopenharmony_ci } else if (insertlen < 2114) { 391cb0ef41Sopenharmony_ci return (uint16_t)(Log2FloorNonZero(insertlen - 66) + 10); 401cb0ef41Sopenharmony_ci } else if (insertlen < 6210) { 411cb0ef41Sopenharmony_ci return 21u; 421cb0ef41Sopenharmony_ci } else if (insertlen < 22594) { 431cb0ef41Sopenharmony_ci return 22u; 441cb0ef41Sopenharmony_ci } else { 451cb0ef41Sopenharmony_ci return 23u; 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci} 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint16_t GetCopyLengthCode(size_t copylen) { 501cb0ef41Sopenharmony_ci if (copylen < 10) { 511cb0ef41Sopenharmony_ci return (uint16_t)(copylen - 2); 521cb0ef41Sopenharmony_ci } else if (copylen < 134) { 531cb0ef41Sopenharmony_ci uint32_t nbits = Log2FloorNonZero(copylen - 6) - 1u; 541cb0ef41Sopenharmony_ci return (uint16_t)((nbits << 1) + ((copylen - 6) >> nbits) + 4); 551cb0ef41Sopenharmony_ci } else if (copylen < 2118) { 561cb0ef41Sopenharmony_ci return (uint16_t)(Log2FloorNonZero(copylen - 70) + 12); 571cb0ef41Sopenharmony_ci } else { 581cb0ef41Sopenharmony_ci return 23u; 591cb0ef41Sopenharmony_ci } 601cb0ef41Sopenharmony_ci} 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint16_t CombineLengthCodes( 631cb0ef41Sopenharmony_ci uint16_t inscode, uint16_t copycode, BROTLI_BOOL use_last_distance) { 641cb0ef41Sopenharmony_ci uint16_t bits64 = 651cb0ef41Sopenharmony_ci (uint16_t)((copycode & 0x7u) | ((inscode & 0x7u) << 3u)); 661cb0ef41Sopenharmony_ci if (use_last_distance && inscode < 8u && copycode < 16u) { 671cb0ef41Sopenharmony_ci return (copycode < 8u) ? bits64 : (bits64 | 64u); 681cb0ef41Sopenharmony_ci } else { 691cb0ef41Sopenharmony_ci /* Specification: 5 Encoding of ... (last table) */ 701cb0ef41Sopenharmony_ci /* offset = 2 * index, where index is in range [0..8] */ 711cb0ef41Sopenharmony_ci uint32_t offset = 2u * ((copycode >> 3u) + 3u * (inscode >> 3u)); 721cb0ef41Sopenharmony_ci /* All values in specification are K * 64, 731cb0ef41Sopenharmony_ci where K = [2, 3, 6, 4, 5, 8, 7, 9, 10], 741cb0ef41Sopenharmony_ci i + 1 = [1, 2, 3, 4, 5, 6, 7, 8, 9], 751cb0ef41Sopenharmony_ci K - i - 1 = [1, 1, 3, 0, 0, 2, 0, 1, 2] = D. 761cb0ef41Sopenharmony_ci All values in D require only 2 bits to encode. 771cb0ef41Sopenharmony_ci Magic constant is shifted 6 bits left, to avoid final multiplication. */ 781cb0ef41Sopenharmony_ci offset = (offset << 5u) + 0x40u + ((0x520D40u >> offset) & 0xC0u); 791cb0ef41Sopenharmony_ci return (uint16_t)(offset | bits64); 801cb0ef41Sopenharmony_ci } 811cb0ef41Sopenharmony_ci} 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_cistatic BROTLI_INLINE void GetLengthCode(size_t insertlen, size_t copylen, 841cb0ef41Sopenharmony_ci BROTLI_BOOL use_last_distance, 851cb0ef41Sopenharmony_ci uint16_t* code) { 861cb0ef41Sopenharmony_ci uint16_t inscode = GetInsertLengthCode(insertlen); 871cb0ef41Sopenharmony_ci uint16_t copycode = GetCopyLengthCode(copylen); 881cb0ef41Sopenharmony_ci *code = CombineLengthCodes(inscode, copycode, use_last_distance); 891cb0ef41Sopenharmony_ci} 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t GetInsertBase(uint16_t inscode) { 921cb0ef41Sopenharmony_ci return kBrotliInsBase[inscode]; 931cb0ef41Sopenharmony_ci} 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t GetInsertExtra(uint16_t inscode) { 961cb0ef41Sopenharmony_ci return kBrotliInsExtra[inscode]; 971cb0ef41Sopenharmony_ci} 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t GetCopyBase(uint16_t copycode) { 1001cb0ef41Sopenharmony_ci return kBrotliCopyBase[copycode]; 1011cb0ef41Sopenharmony_ci} 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t GetCopyExtra(uint16_t copycode) { 1041cb0ef41Sopenharmony_ci return kBrotliCopyExtra[copycode]; 1051cb0ef41Sopenharmony_ci} 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_citypedef struct Command { 1081cb0ef41Sopenharmony_ci uint32_t insert_len_; 1091cb0ef41Sopenharmony_ci /* Stores copy_len in low 25 bits and copy_code - copy_len in high 7 bit. */ 1101cb0ef41Sopenharmony_ci uint32_t copy_len_; 1111cb0ef41Sopenharmony_ci /* Stores distance extra bits. */ 1121cb0ef41Sopenharmony_ci uint32_t dist_extra_; 1131cb0ef41Sopenharmony_ci uint16_t cmd_prefix_; 1141cb0ef41Sopenharmony_ci /* Stores distance code in low 10 bits 1151cb0ef41Sopenharmony_ci and number of extra bits in high 6 bits. */ 1161cb0ef41Sopenharmony_ci uint16_t dist_prefix_; 1171cb0ef41Sopenharmony_ci} Command; 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci/* distance_code is e.g. 0 for same-as-last short code, or 16 for offset 1. */ 1201cb0ef41Sopenharmony_cistatic BROTLI_INLINE void InitCommand(Command* self, 1211cb0ef41Sopenharmony_ci const BrotliDistanceParams* dist, size_t insertlen, 1221cb0ef41Sopenharmony_ci size_t copylen, int copylen_code_delta, size_t distance_code) { 1231cb0ef41Sopenharmony_ci /* Don't rely on signed int representation, use honest casts. */ 1241cb0ef41Sopenharmony_ci uint32_t delta = (uint8_t)((int8_t)copylen_code_delta); 1251cb0ef41Sopenharmony_ci self->insert_len_ = (uint32_t)insertlen; 1261cb0ef41Sopenharmony_ci self->copy_len_ = (uint32_t)(copylen | (delta << 25)); 1271cb0ef41Sopenharmony_ci /* The distance prefix and extra bits are stored in this Command as if 1281cb0ef41Sopenharmony_ci npostfix and ndirect were 0, they are only recomputed later after the 1291cb0ef41Sopenharmony_ci clustering if needed. */ 1301cb0ef41Sopenharmony_ci PrefixEncodeCopyDistance( 1311cb0ef41Sopenharmony_ci distance_code, dist->num_direct_distance_codes, 1321cb0ef41Sopenharmony_ci dist->distance_postfix_bits, &self->dist_prefix_, &self->dist_extra_); 1331cb0ef41Sopenharmony_ci GetLengthCode( 1341cb0ef41Sopenharmony_ci insertlen, (size_t)((int)copylen + copylen_code_delta), 1351cb0ef41Sopenharmony_ci TO_BROTLI_BOOL((self->dist_prefix_ & 0x3FF) == 0), &self->cmd_prefix_); 1361cb0ef41Sopenharmony_ci} 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_cistatic BROTLI_INLINE void InitInsertCommand(Command* self, size_t insertlen) { 1391cb0ef41Sopenharmony_ci self->insert_len_ = (uint32_t)insertlen; 1401cb0ef41Sopenharmony_ci self->copy_len_ = 4 << 25; 1411cb0ef41Sopenharmony_ci self->dist_extra_ = 0; 1421cb0ef41Sopenharmony_ci self->dist_prefix_ = BROTLI_NUM_DISTANCE_SHORT_CODES; 1431cb0ef41Sopenharmony_ci GetLengthCode(insertlen, 4, BROTLI_FALSE, &self->cmd_prefix_); 1441cb0ef41Sopenharmony_ci} 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t CommandRestoreDistanceCode( 1471cb0ef41Sopenharmony_ci const Command* self, const BrotliDistanceParams* dist) { 1481cb0ef41Sopenharmony_ci if ((self->dist_prefix_ & 0x3FFu) < 1491cb0ef41Sopenharmony_ci BROTLI_NUM_DISTANCE_SHORT_CODES + dist->num_direct_distance_codes) { 1501cb0ef41Sopenharmony_ci return self->dist_prefix_ & 0x3FFu; 1511cb0ef41Sopenharmony_ci } else { 1521cb0ef41Sopenharmony_ci uint32_t dcode = self->dist_prefix_ & 0x3FFu; 1531cb0ef41Sopenharmony_ci uint32_t nbits = self->dist_prefix_ >> 10; 1541cb0ef41Sopenharmony_ci uint32_t extra = self->dist_extra_; 1551cb0ef41Sopenharmony_ci uint32_t postfix_mask = (1U << dist->distance_postfix_bits) - 1U; 1561cb0ef41Sopenharmony_ci uint32_t hcode = (dcode - dist->num_direct_distance_codes - 1571cb0ef41Sopenharmony_ci BROTLI_NUM_DISTANCE_SHORT_CODES) >> 1581cb0ef41Sopenharmony_ci dist->distance_postfix_bits; 1591cb0ef41Sopenharmony_ci uint32_t lcode = (dcode - dist->num_direct_distance_codes - 1601cb0ef41Sopenharmony_ci BROTLI_NUM_DISTANCE_SHORT_CODES) & postfix_mask; 1611cb0ef41Sopenharmony_ci uint32_t offset = ((2U + (hcode & 1U)) << nbits) - 4U; 1621cb0ef41Sopenharmony_ci return ((offset + extra) << dist->distance_postfix_bits) + lcode + 1631cb0ef41Sopenharmony_ci dist->num_direct_distance_codes + BROTLI_NUM_DISTANCE_SHORT_CODES; 1641cb0ef41Sopenharmony_ci } 1651cb0ef41Sopenharmony_ci} 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t CommandDistanceContext(const Command* self) { 1681cb0ef41Sopenharmony_ci uint32_t r = self->cmd_prefix_ >> 6; 1691cb0ef41Sopenharmony_ci uint32_t c = self->cmd_prefix_ & 7; 1701cb0ef41Sopenharmony_ci if ((r == 0 || r == 2 || r == 4 || r == 7) && (c <= 2)) { 1711cb0ef41Sopenharmony_ci return c; 1721cb0ef41Sopenharmony_ci } 1731cb0ef41Sopenharmony_ci return 3; 1741cb0ef41Sopenharmony_ci} 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t CommandCopyLen(const Command* self) { 1771cb0ef41Sopenharmony_ci return self->copy_len_ & 0x1FFFFFF; 1781cb0ef41Sopenharmony_ci} 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t CommandCopyLenCode(const Command* self) { 1811cb0ef41Sopenharmony_ci uint32_t modifier = self->copy_len_ >> 25; 1821cb0ef41Sopenharmony_ci int32_t delta = (int8_t)((uint8_t)(modifier | ((modifier & 0x40) << 1))); 1831cb0ef41Sopenharmony_ci return (uint32_t)((int32_t)(self->copy_len_ & 0x1FFFFFF) + delta); 1841cb0ef41Sopenharmony_ci} 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus) 1871cb0ef41Sopenharmony_ci} /* extern "C" */ 1881cb0ef41Sopenharmony_ci#endif 1891cb0ef41Sopenharmony_ci 1901cb0ef41Sopenharmony_ci#endif /* BROTLI_ENC_COMMAND_H_ */ 191