• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2013 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 /* This class models a sequence of literals and a backward reference copy. */
8 
9 #ifndef BROTLI_ENC_COMMAND_H_
10 #define BROTLI_ENC_COMMAND_H_
11 
12 #include <brotli/types.h>
13 
14 #include "../common/constants.h"
15 #include "../common/platform.h"
16 #include "fast_log.h"
17 #include "params.h"
18 #include "prefix.h"
19 
20 #if defined(__cplusplus) || defined(c_plusplus)
21 extern "C" {
22 #endif
23 
24 BROTLI_INTERNAL extern const uint32_t
25     kBrotliInsBase[BROTLI_NUM_INS_COPY_CODES];
26 BROTLI_INTERNAL extern const uint32_t
27     kBrotliInsExtra[BROTLI_NUM_INS_COPY_CODES];
28 BROTLI_INTERNAL extern const uint32_t
29     kBrotliCopyBase[BROTLI_NUM_INS_COPY_CODES];
30 BROTLI_INTERNAL extern const uint32_t
31     kBrotliCopyExtra[BROTLI_NUM_INS_COPY_CODES];
32 
GetInsertLengthCode(size_t insertlen)33 static BROTLI_INLINE uint16_t GetInsertLengthCode(size_t insertlen) {
34   if (insertlen < 6) {
35     return (uint16_t)insertlen;
36   } else if (insertlen < 130) {
37     uint32_t nbits = Log2FloorNonZero(insertlen - 2) - 1u;
38     return (uint16_t)((nbits << 1) + ((insertlen - 2) >> nbits) + 2);
39   } else if (insertlen < 2114) {
40     return (uint16_t)(Log2FloorNonZero(insertlen - 66) + 10);
41   } else if (insertlen < 6210) {
42     return 21u;
43   } else if (insertlen < 22594) {
44     return 22u;
45   } else {
46     return 23u;
47   }
48 }
49 
GetCopyLengthCode(size_t copylen)50 static BROTLI_INLINE uint16_t GetCopyLengthCode(size_t copylen) {
51   if (copylen < 10) {
52     return (uint16_t)(copylen - 2);
53   } else if (copylen < 134) {
54     uint32_t nbits = Log2FloorNonZero(copylen - 6) - 1u;
55     return (uint16_t)((nbits << 1) + ((copylen - 6) >> nbits) + 4);
56   } else if (copylen < 2118) {
57     return (uint16_t)(Log2FloorNonZero(copylen - 70) + 12);
58   } else {
59     return 23u;
60   }
61 }
62 
CombineLengthCodes(uint16_t inscode,uint16_t copycode,BROTLI_BOOL use_last_distance)63 static BROTLI_INLINE uint16_t CombineLengthCodes(
64     uint16_t inscode, uint16_t copycode, BROTLI_BOOL use_last_distance) {
65   uint16_t bits64 =
66       (uint16_t)((copycode & 0x7u) | ((inscode & 0x7u) << 3u));
67   if (use_last_distance && inscode < 8u && copycode < 16u) {
68     return (copycode < 8u) ? bits64 : (bits64 | 64u);
69   } else {
70     /* Specification: 5 Encoding of ... (last table) */
71     /* offset = 2 * index, where index is in range [0..8] */
72     uint32_t offset = 2u * ((copycode >> 3u) + 3u * (inscode >> 3u));
73     /* All values in specification are K * 64,
74        where   K = [2, 3, 6, 4, 5, 8, 7, 9, 10],
75            i + 1 = [1, 2, 3, 4, 5, 6, 7, 8,  9],
76        K - i - 1 = [1, 1, 3, 0, 0, 2, 0, 1,  2] = D.
77        All values in D require only 2 bits to encode.
78        Magic constant is shifted 6 bits left, to avoid final multiplication. */
79     offset = (offset << 5u) + 0x40u + ((0x520D40u >> offset) & 0xC0u);
80     return (uint16_t)(offset | bits64);
81   }
82 }
83 
GetLengthCode(size_t insertlen,size_t copylen,BROTLI_BOOL use_last_distance,uint16_t * code)84 static BROTLI_INLINE void GetLengthCode(size_t insertlen, size_t copylen,
85                                         BROTLI_BOOL use_last_distance,
86                                         uint16_t* code) {
87   uint16_t inscode = GetInsertLengthCode(insertlen);
88   uint16_t copycode = GetCopyLengthCode(copylen);
89   *code = CombineLengthCodes(inscode, copycode, use_last_distance);
90 }
91 
GetInsertBase(uint16_t inscode)92 static BROTLI_INLINE uint32_t GetInsertBase(uint16_t inscode) {
93   return kBrotliInsBase[inscode];
94 }
95 
GetInsertExtra(uint16_t inscode)96 static BROTLI_INLINE uint32_t GetInsertExtra(uint16_t inscode) {
97   return kBrotliInsExtra[inscode];
98 }
99 
GetCopyBase(uint16_t copycode)100 static BROTLI_INLINE uint32_t GetCopyBase(uint16_t copycode) {
101   return kBrotliCopyBase[copycode];
102 }
103 
GetCopyExtra(uint16_t copycode)104 static BROTLI_INLINE uint32_t GetCopyExtra(uint16_t copycode) {
105   return kBrotliCopyExtra[copycode];
106 }
107 
108 typedef struct Command {
109   uint32_t insert_len_;
110   /* Stores copy_len in low 25 bits and copy_code - copy_len in high 7 bit. */
111   uint32_t copy_len_;
112   /* Stores distance extra bits. */
113   uint32_t dist_extra_;
114   uint16_t cmd_prefix_;
115   /* Stores distance code in low 10 bits
116      and number of extra bits in high 6 bits. */
117   uint16_t dist_prefix_;
118 } Command;
119 
120 /* distance_code is e.g. 0 for same-as-last short code, or 16 for offset 1. */
InitCommand(Command * self,const BrotliDistanceParams * dist,size_t insertlen,size_t copylen,int copylen_code_delta,size_t distance_code)121 static BROTLI_INLINE void InitCommand(Command* self,
122     const BrotliDistanceParams* dist, size_t insertlen,
123     size_t copylen, int copylen_code_delta, size_t distance_code) {
124   /* Don't rely on signed int representation, use honest casts. */
125   uint32_t delta = (uint8_t)((int8_t)copylen_code_delta);
126   self->insert_len_ = (uint32_t)insertlen;
127   self->copy_len_ = (uint32_t)(copylen | (delta << 25));
128   /* The distance prefix and extra bits are stored in this Command as if
129      npostfix and ndirect were 0, they are only recomputed later after the
130      clustering if needed. */
131   PrefixEncodeCopyDistance(
132       distance_code, dist->num_direct_distance_codes,
133       dist->distance_postfix_bits, &self->dist_prefix_, &self->dist_extra_);
134   GetLengthCode(
135       insertlen, (size_t)((int)copylen + copylen_code_delta),
136       TO_BROTLI_BOOL((self->dist_prefix_ & 0x3FF) == 0), &self->cmd_prefix_);
137 }
138 
InitInsertCommand(Command * self,size_t insertlen)139 static BROTLI_INLINE void InitInsertCommand(Command* self, size_t insertlen) {
140   self->insert_len_ = (uint32_t)insertlen;
141   self->copy_len_ = 4 << 25;
142   self->dist_extra_ = 0;
143   self->dist_prefix_ = BROTLI_NUM_DISTANCE_SHORT_CODES;
144   GetLengthCode(insertlen, 4, BROTLI_FALSE, &self->cmd_prefix_);
145 }
146 
CommandRestoreDistanceCode(const Command * self,const BrotliDistanceParams * dist)147 static BROTLI_INLINE uint32_t CommandRestoreDistanceCode(
148     const Command* self, const BrotliDistanceParams* dist) {
149   if ((self->dist_prefix_ & 0x3FFu) <
150       BROTLI_NUM_DISTANCE_SHORT_CODES + dist->num_direct_distance_codes) {
151     return self->dist_prefix_ & 0x3FFu;
152   } else {
153     uint32_t dcode = self->dist_prefix_ & 0x3FFu;
154     uint32_t nbits = self->dist_prefix_ >> 10;
155     uint32_t extra = self->dist_extra_;
156     uint32_t postfix_mask = (1U << dist->distance_postfix_bits) - 1U;
157     uint32_t hcode = (dcode - dist->num_direct_distance_codes -
158         BROTLI_NUM_DISTANCE_SHORT_CODES) >>
159         dist->distance_postfix_bits;
160     uint32_t lcode = (dcode - dist->num_direct_distance_codes -
161         BROTLI_NUM_DISTANCE_SHORT_CODES) & postfix_mask;
162     uint32_t offset = ((2U + (hcode & 1U)) << nbits) - 4U;
163     return ((offset + extra) << dist->distance_postfix_bits) + lcode +
164         dist->num_direct_distance_codes + BROTLI_NUM_DISTANCE_SHORT_CODES;
165   }
166 }
167 
CommandDistanceContext(const Command * self)168 static BROTLI_INLINE uint32_t CommandDistanceContext(const Command* self) {
169   uint32_t r = self->cmd_prefix_ >> 6;
170   uint32_t c = self->cmd_prefix_ & 7;
171   if ((r == 0 || r == 2 || r == 4 || r == 7) && (c <= 2)) {
172     return c;
173   }
174   return 3;
175 }
176 
CommandCopyLen(const Command * self)177 static BROTLI_INLINE uint32_t CommandCopyLen(const Command* self) {
178   return self->copy_len_ & 0x1FFFFFF;
179 }
180 
CommandCopyLenCode(const Command * self)181 static BROTLI_INLINE uint32_t CommandCopyLenCode(const Command* self) {
182   uint32_t modifier = self->copy_len_ >> 25;
183   int32_t delta = (int8_t)((uint8_t)(modifier | ((modifier & 0x40) << 1)));
184   return (uint32_t)((int32_t)(self->copy_len_ & 0x1FFFFFF) + delta);
185 }
186 
187 #if defined(__cplusplus) || defined(c_plusplus)
188 }  /* extern "C" */
189 #endif
190 
191 #endif  /* BROTLI_ENC_COMMAND_H_ */
192