1*f4ee7fbaSAndroid Build Coastguard Worker /* Copyright 2015 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 /* Function for fast encoding of an input fragment, independently from the input
8*f4ee7fbaSAndroid Build Coastguard Worker history. This function uses one-pass processing: when we find a backward
9*f4ee7fbaSAndroid Build Coastguard Worker match, we immediately emit the corresponding command and literal codes to
10*f4ee7fbaSAndroid Build Coastguard Worker the bit stream.
11*f4ee7fbaSAndroid Build Coastguard Worker
12*f4ee7fbaSAndroid Build Coastguard Worker Adapted from the CompressFragment() function in
13*f4ee7fbaSAndroid Build Coastguard Worker https://github.com/google/snappy/blob/master/snappy.cc */
14*f4ee7fbaSAndroid Build Coastguard Worker
15*f4ee7fbaSAndroid Build Coastguard Worker #include "./compress_fragment.h"
16*f4ee7fbaSAndroid Build Coastguard Worker
17*f4ee7fbaSAndroid Build Coastguard Worker #include <string.h> /* memcmp, memcpy, memset */
18*f4ee7fbaSAndroid Build Coastguard Worker
19*f4ee7fbaSAndroid Build Coastguard Worker #include "../common/constants.h"
20*f4ee7fbaSAndroid Build Coastguard Worker #include "../common/platform.h"
21*f4ee7fbaSAndroid Build Coastguard Worker #include <brotli/types.h>
22*f4ee7fbaSAndroid Build Coastguard Worker #include "./brotli_bit_stream.h"
23*f4ee7fbaSAndroid Build Coastguard Worker #include "./entropy_encode.h"
24*f4ee7fbaSAndroid Build Coastguard Worker #include "./fast_log.h"
25*f4ee7fbaSAndroid Build Coastguard Worker #include "./find_match_length.h"
26*f4ee7fbaSAndroid Build Coastguard Worker #include "./memory.h"
27*f4ee7fbaSAndroid Build Coastguard Worker #include "./write_bits.h"
28*f4ee7fbaSAndroid Build Coastguard Worker
29*f4ee7fbaSAndroid Build Coastguard Worker #if defined(__cplusplus) || defined(c_plusplus)
30*f4ee7fbaSAndroid Build Coastguard Worker extern "C" {
31*f4ee7fbaSAndroid Build Coastguard Worker #endif
32*f4ee7fbaSAndroid Build Coastguard Worker
33*f4ee7fbaSAndroid Build Coastguard Worker #define MAX_DISTANCE (long)BROTLI_MAX_BACKWARD_LIMIT(18)
34*f4ee7fbaSAndroid Build Coastguard Worker
35*f4ee7fbaSAndroid Build Coastguard Worker /* kHashMul32 multiplier has these properties:
36*f4ee7fbaSAndroid Build Coastguard Worker * The multiplier must be odd. Otherwise we may lose the highest bit.
37*f4ee7fbaSAndroid Build Coastguard Worker * No long streaks of ones or zeros.
38*f4ee7fbaSAndroid Build Coastguard Worker * There is no effort to ensure that it is a prime, the oddity is enough
39*f4ee7fbaSAndroid Build Coastguard Worker for this use.
40*f4ee7fbaSAndroid Build Coastguard Worker * The number has been tuned heuristically against compression benchmarks. */
41*f4ee7fbaSAndroid Build Coastguard Worker static const uint32_t kHashMul32 = 0x1E35A7BD;
42*f4ee7fbaSAndroid Build Coastguard Worker
Hash(const uint8_t * p,size_t shift)43*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE uint32_t Hash(const uint8_t* p, size_t shift) {
44*f4ee7fbaSAndroid Build Coastguard Worker const uint64_t h = (BROTLI_UNALIGNED_LOAD64LE(p) << 24) * kHashMul32;
45*f4ee7fbaSAndroid Build Coastguard Worker return (uint32_t)(h >> shift);
46*f4ee7fbaSAndroid Build Coastguard Worker }
47*f4ee7fbaSAndroid Build Coastguard Worker
HashBytesAtOffset(uint64_t v,int offset,size_t shift)48*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE uint32_t HashBytesAtOffset(
49*f4ee7fbaSAndroid Build Coastguard Worker uint64_t v, int offset, size_t shift) {
50*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(offset >= 0);
51*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(offset <= 3);
52*f4ee7fbaSAndroid Build Coastguard Worker {
53*f4ee7fbaSAndroid Build Coastguard Worker const uint64_t h = ((v >> (8 * offset)) << 24) * kHashMul32;
54*f4ee7fbaSAndroid Build Coastguard Worker return (uint32_t)(h >> shift);
55*f4ee7fbaSAndroid Build Coastguard Worker }
56*f4ee7fbaSAndroid Build Coastguard Worker }
57*f4ee7fbaSAndroid Build Coastguard Worker
IsMatch(const uint8_t * p1,const uint8_t * p2)58*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE BROTLI_BOOL IsMatch(const uint8_t* p1, const uint8_t* p2) {
59*f4ee7fbaSAndroid Build Coastguard Worker return TO_BROTLI_BOOL(
60*f4ee7fbaSAndroid Build Coastguard Worker BrotliUnalignedRead32(p1) == BrotliUnalignedRead32(p2) &&
61*f4ee7fbaSAndroid Build Coastguard Worker p1[4] == p2[4]);
62*f4ee7fbaSAndroid Build Coastguard Worker }
63*f4ee7fbaSAndroid Build Coastguard Worker
64*f4ee7fbaSAndroid Build Coastguard Worker /* Builds a literal prefix code into "depths" and "bits" based on the statistics
65*f4ee7fbaSAndroid Build Coastguard Worker of the "input" string and stores it into the bit stream.
66*f4ee7fbaSAndroid Build Coastguard Worker Note that the prefix code here is built from the pre-LZ77 input, therefore
67*f4ee7fbaSAndroid Build Coastguard Worker we can only approximate the statistics of the actual literal stream.
68*f4ee7fbaSAndroid Build Coastguard Worker Moreover, for long inputs we build a histogram from a sample of the input
69*f4ee7fbaSAndroid Build Coastguard Worker and thus have to assign a non-zero depth for each literal.
70*f4ee7fbaSAndroid Build Coastguard Worker Returns estimated compression ratio millibytes/char for encoding given input
71*f4ee7fbaSAndroid Build Coastguard Worker with generated code. */
BuildAndStoreLiteralPrefixCode(MemoryManager * m,const uint8_t * input,const size_t input_size,uint8_t depths[256],uint16_t bits[256],size_t * storage_ix,uint8_t * storage)72*f4ee7fbaSAndroid Build Coastguard Worker static size_t BuildAndStoreLiteralPrefixCode(MemoryManager* m,
73*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* input,
74*f4ee7fbaSAndroid Build Coastguard Worker const size_t input_size,
75*f4ee7fbaSAndroid Build Coastguard Worker uint8_t depths[256],
76*f4ee7fbaSAndroid Build Coastguard Worker uint16_t bits[256],
77*f4ee7fbaSAndroid Build Coastguard Worker size_t* storage_ix,
78*f4ee7fbaSAndroid Build Coastguard Worker uint8_t* storage) {
79*f4ee7fbaSAndroid Build Coastguard Worker uint32_t histogram[256] = { 0 };
80*f4ee7fbaSAndroid Build Coastguard Worker size_t histogram_total;
81*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
82*f4ee7fbaSAndroid Build Coastguard Worker if (input_size < (1 << 15)) {
83*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < input_size; ++i) {
84*f4ee7fbaSAndroid Build Coastguard Worker ++histogram[input[i]];
85*f4ee7fbaSAndroid Build Coastguard Worker }
86*f4ee7fbaSAndroid Build Coastguard Worker histogram_total = input_size;
87*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < 256; ++i) {
88*f4ee7fbaSAndroid Build Coastguard Worker /* We weigh the first 11 samples with weight 3 to account for the
89*f4ee7fbaSAndroid Build Coastguard Worker balancing effect of the LZ77 phase on the histogram. */
90*f4ee7fbaSAndroid Build Coastguard Worker const uint32_t adjust = 2 * BROTLI_MIN(uint32_t, histogram[i], 11u);
91*f4ee7fbaSAndroid Build Coastguard Worker histogram[i] += adjust;
92*f4ee7fbaSAndroid Build Coastguard Worker histogram_total += adjust;
93*f4ee7fbaSAndroid Build Coastguard Worker }
94*f4ee7fbaSAndroid Build Coastguard Worker } else {
95*f4ee7fbaSAndroid Build Coastguard Worker static const size_t kSampleRate = 29;
96*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < input_size; i += kSampleRate) {
97*f4ee7fbaSAndroid Build Coastguard Worker ++histogram[input[i]];
98*f4ee7fbaSAndroid Build Coastguard Worker }
99*f4ee7fbaSAndroid Build Coastguard Worker histogram_total = (input_size + kSampleRate - 1) / kSampleRate;
100*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < 256; ++i) {
101*f4ee7fbaSAndroid Build Coastguard Worker /* We add 1 to each population count to avoid 0 bit depths (since this is
102*f4ee7fbaSAndroid Build Coastguard Worker only a sample and we don't know if the symbol appears or not), and we
103*f4ee7fbaSAndroid Build Coastguard Worker weigh the first 11 samples with weight 3 to account for the balancing
104*f4ee7fbaSAndroid Build Coastguard Worker effect of the LZ77 phase on the histogram (more frequent symbols are
105*f4ee7fbaSAndroid Build Coastguard Worker more likely to be in backward references instead as literals). */
106*f4ee7fbaSAndroid Build Coastguard Worker const uint32_t adjust = 1 + 2 * BROTLI_MIN(uint32_t, histogram[i], 11u);
107*f4ee7fbaSAndroid Build Coastguard Worker histogram[i] += adjust;
108*f4ee7fbaSAndroid Build Coastguard Worker histogram_total += adjust;
109*f4ee7fbaSAndroid Build Coastguard Worker }
110*f4ee7fbaSAndroid Build Coastguard Worker }
111*f4ee7fbaSAndroid Build Coastguard Worker BrotliBuildAndStoreHuffmanTreeFast(m, histogram, histogram_total,
112*f4ee7fbaSAndroid Build Coastguard Worker /* max_bits = */ 8,
113*f4ee7fbaSAndroid Build Coastguard Worker depths, bits, storage_ix, storage);
114*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m)) return 0;
115*f4ee7fbaSAndroid Build Coastguard Worker {
116*f4ee7fbaSAndroid Build Coastguard Worker size_t literal_ratio = 0;
117*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < 256; ++i) {
118*f4ee7fbaSAndroid Build Coastguard Worker if (histogram[i]) literal_ratio += histogram[i] * depths[i];
119*f4ee7fbaSAndroid Build Coastguard Worker }
120*f4ee7fbaSAndroid Build Coastguard Worker /* Estimated encoding ratio, millibytes per symbol. */
121*f4ee7fbaSAndroid Build Coastguard Worker return (literal_ratio * 125) / histogram_total;
122*f4ee7fbaSAndroid Build Coastguard Worker }
123*f4ee7fbaSAndroid Build Coastguard Worker }
124*f4ee7fbaSAndroid Build Coastguard Worker
125*f4ee7fbaSAndroid Build Coastguard Worker /* Builds a command and distance prefix code (each 64 symbols) into "depth" and
126*f4ee7fbaSAndroid Build Coastguard Worker "bits" based on "histogram" and stores it into the bit stream. */
BuildAndStoreCommandPrefixCode(const uint32_t histogram[128],uint8_t depth[128],uint16_t bits[128],size_t * storage_ix,uint8_t * storage)127*f4ee7fbaSAndroid Build Coastguard Worker static void BuildAndStoreCommandPrefixCode(const uint32_t histogram[128],
128*f4ee7fbaSAndroid Build Coastguard Worker uint8_t depth[128], uint16_t bits[128], size_t* storage_ix,
129*f4ee7fbaSAndroid Build Coastguard Worker uint8_t* storage) {
130*f4ee7fbaSAndroid Build Coastguard Worker /* Tree size for building a tree over 64 symbols is 2 * 64 + 1. */
131*f4ee7fbaSAndroid Build Coastguard Worker HuffmanTree tree[129];
132*f4ee7fbaSAndroid Build Coastguard Worker uint8_t cmd_depth[BROTLI_NUM_COMMAND_SYMBOLS] = { 0 };
133*f4ee7fbaSAndroid Build Coastguard Worker uint16_t cmd_bits[64];
134*f4ee7fbaSAndroid Build Coastguard Worker
135*f4ee7fbaSAndroid Build Coastguard Worker BrotliCreateHuffmanTree(histogram, 64, 15, tree, depth);
136*f4ee7fbaSAndroid Build Coastguard Worker BrotliCreateHuffmanTree(&histogram[64], 64, 14, tree, &depth[64]);
137*f4ee7fbaSAndroid Build Coastguard Worker /* We have to jump through a few hoops here in order to compute
138*f4ee7fbaSAndroid Build Coastguard Worker the command bits because the symbols are in a different order than in
139*f4ee7fbaSAndroid Build Coastguard Worker the full alphabet. This looks complicated, but having the symbols
140*f4ee7fbaSAndroid Build Coastguard Worker in this order in the command bits saves a few branches in the Emit*
141*f4ee7fbaSAndroid Build Coastguard Worker functions. */
142*f4ee7fbaSAndroid Build Coastguard Worker memcpy(cmd_depth, depth, 24);
143*f4ee7fbaSAndroid Build Coastguard Worker memcpy(cmd_depth + 24, depth + 40, 8);
144*f4ee7fbaSAndroid Build Coastguard Worker memcpy(cmd_depth + 32, depth + 24, 8);
145*f4ee7fbaSAndroid Build Coastguard Worker memcpy(cmd_depth + 40, depth + 48, 8);
146*f4ee7fbaSAndroid Build Coastguard Worker memcpy(cmd_depth + 48, depth + 32, 8);
147*f4ee7fbaSAndroid Build Coastguard Worker memcpy(cmd_depth + 56, depth + 56, 8);
148*f4ee7fbaSAndroid Build Coastguard Worker BrotliConvertBitDepthsToSymbols(cmd_depth, 64, cmd_bits);
149*f4ee7fbaSAndroid Build Coastguard Worker memcpy(bits, cmd_bits, 48);
150*f4ee7fbaSAndroid Build Coastguard Worker memcpy(bits + 24, cmd_bits + 32, 16);
151*f4ee7fbaSAndroid Build Coastguard Worker memcpy(bits + 32, cmd_bits + 48, 16);
152*f4ee7fbaSAndroid Build Coastguard Worker memcpy(bits + 40, cmd_bits + 24, 16);
153*f4ee7fbaSAndroid Build Coastguard Worker memcpy(bits + 48, cmd_bits + 40, 16);
154*f4ee7fbaSAndroid Build Coastguard Worker memcpy(bits + 56, cmd_bits + 56, 16);
155*f4ee7fbaSAndroid Build Coastguard Worker BrotliConvertBitDepthsToSymbols(&depth[64], 64, &bits[64]);
156*f4ee7fbaSAndroid Build Coastguard Worker {
157*f4ee7fbaSAndroid Build Coastguard Worker /* Create the bit length array for the full command alphabet. */
158*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
159*f4ee7fbaSAndroid Build Coastguard Worker memset(cmd_depth, 0, 64); /* only 64 first values were used */
160*f4ee7fbaSAndroid Build Coastguard Worker memcpy(cmd_depth, depth, 8);
161*f4ee7fbaSAndroid Build Coastguard Worker memcpy(cmd_depth + 64, depth + 8, 8);
162*f4ee7fbaSAndroid Build Coastguard Worker memcpy(cmd_depth + 128, depth + 16, 8);
163*f4ee7fbaSAndroid Build Coastguard Worker memcpy(cmd_depth + 192, depth + 24, 8);
164*f4ee7fbaSAndroid Build Coastguard Worker memcpy(cmd_depth + 384, depth + 32, 8);
165*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < 8; ++i) {
166*f4ee7fbaSAndroid Build Coastguard Worker cmd_depth[128 + 8 * i] = depth[40 + i];
167*f4ee7fbaSAndroid Build Coastguard Worker cmd_depth[256 + 8 * i] = depth[48 + i];
168*f4ee7fbaSAndroid Build Coastguard Worker cmd_depth[448 + 8 * i] = depth[56 + i];
169*f4ee7fbaSAndroid Build Coastguard Worker }
170*f4ee7fbaSAndroid Build Coastguard Worker BrotliStoreHuffmanTree(
171*f4ee7fbaSAndroid Build Coastguard Worker cmd_depth, BROTLI_NUM_COMMAND_SYMBOLS, tree, storage_ix, storage);
172*f4ee7fbaSAndroid Build Coastguard Worker }
173*f4ee7fbaSAndroid Build Coastguard Worker BrotliStoreHuffmanTree(&depth[64], 64, tree, storage_ix, storage);
174*f4ee7fbaSAndroid Build Coastguard Worker }
175*f4ee7fbaSAndroid Build Coastguard Worker
176*f4ee7fbaSAndroid Build Coastguard Worker /* REQUIRES: insertlen < 6210 */
EmitInsertLen(size_t insertlen,const uint8_t depth[128],const uint16_t bits[128],uint32_t histo[128],size_t * storage_ix,uint8_t * storage)177*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE void EmitInsertLen(size_t insertlen,
178*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t depth[128],
179*f4ee7fbaSAndroid Build Coastguard Worker const uint16_t bits[128],
180*f4ee7fbaSAndroid Build Coastguard Worker uint32_t histo[128],
181*f4ee7fbaSAndroid Build Coastguard Worker size_t* storage_ix,
182*f4ee7fbaSAndroid Build Coastguard Worker uint8_t* storage) {
183*f4ee7fbaSAndroid Build Coastguard Worker if (insertlen < 6) {
184*f4ee7fbaSAndroid Build Coastguard Worker const size_t code = insertlen + 40;
185*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[code], bits[code], storage_ix, storage);
186*f4ee7fbaSAndroid Build Coastguard Worker ++histo[code];
187*f4ee7fbaSAndroid Build Coastguard Worker } else if (insertlen < 130) {
188*f4ee7fbaSAndroid Build Coastguard Worker const size_t tail = insertlen - 2;
189*f4ee7fbaSAndroid Build Coastguard Worker const uint32_t nbits = Log2FloorNonZero(tail) - 1u;
190*f4ee7fbaSAndroid Build Coastguard Worker const size_t prefix = tail >> nbits;
191*f4ee7fbaSAndroid Build Coastguard Worker const size_t inscode = (nbits << 1) + prefix + 42;
192*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[inscode], bits[inscode], storage_ix, storage);
193*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(nbits, tail - (prefix << nbits), storage_ix, storage);
194*f4ee7fbaSAndroid Build Coastguard Worker ++histo[inscode];
195*f4ee7fbaSAndroid Build Coastguard Worker } else if (insertlen < 2114) {
196*f4ee7fbaSAndroid Build Coastguard Worker const size_t tail = insertlen - 66;
197*f4ee7fbaSAndroid Build Coastguard Worker const uint32_t nbits = Log2FloorNonZero(tail);
198*f4ee7fbaSAndroid Build Coastguard Worker const size_t code = nbits + 50;
199*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[code], bits[code], storage_ix, storage);
200*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(nbits, tail - ((size_t)1 << nbits), storage_ix, storage);
201*f4ee7fbaSAndroid Build Coastguard Worker ++histo[code];
202*f4ee7fbaSAndroid Build Coastguard Worker } else {
203*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[61], bits[61], storage_ix, storage);
204*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(12, insertlen - 2114, storage_ix, storage);
205*f4ee7fbaSAndroid Build Coastguard Worker ++histo[61];
206*f4ee7fbaSAndroid Build Coastguard Worker }
207*f4ee7fbaSAndroid Build Coastguard Worker }
208*f4ee7fbaSAndroid Build Coastguard Worker
EmitLongInsertLen(size_t insertlen,const uint8_t depth[128],const uint16_t bits[128],uint32_t histo[128],size_t * storage_ix,uint8_t * storage)209*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE void EmitLongInsertLen(size_t insertlen,
210*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t depth[128],
211*f4ee7fbaSAndroid Build Coastguard Worker const uint16_t bits[128],
212*f4ee7fbaSAndroid Build Coastguard Worker uint32_t histo[128],
213*f4ee7fbaSAndroid Build Coastguard Worker size_t* storage_ix,
214*f4ee7fbaSAndroid Build Coastguard Worker uint8_t* storage) {
215*f4ee7fbaSAndroid Build Coastguard Worker if (insertlen < 22594) {
216*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[62], bits[62], storage_ix, storage);
217*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(14, insertlen - 6210, storage_ix, storage);
218*f4ee7fbaSAndroid Build Coastguard Worker ++histo[62];
219*f4ee7fbaSAndroid Build Coastguard Worker } else {
220*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[63], bits[63], storage_ix, storage);
221*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(24, insertlen - 22594, storage_ix, storage);
222*f4ee7fbaSAndroid Build Coastguard Worker ++histo[63];
223*f4ee7fbaSAndroid Build Coastguard Worker }
224*f4ee7fbaSAndroid Build Coastguard Worker }
225*f4ee7fbaSAndroid Build Coastguard Worker
EmitCopyLen(size_t copylen,const uint8_t depth[128],const uint16_t bits[128],uint32_t histo[128],size_t * storage_ix,uint8_t * storage)226*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE void EmitCopyLen(size_t copylen,
227*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t depth[128],
228*f4ee7fbaSAndroid Build Coastguard Worker const uint16_t bits[128],
229*f4ee7fbaSAndroid Build Coastguard Worker uint32_t histo[128],
230*f4ee7fbaSAndroid Build Coastguard Worker size_t* storage_ix,
231*f4ee7fbaSAndroid Build Coastguard Worker uint8_t* storage) {
232*f4ee7fbaSAndroid Build Coastguard Worker if (copylen < 10) {
233*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(
234*f4ee7fbaSAndroid Build Coastguard Worker depth[copylen + 14], bits[copylen + 14], storage_ix, storage);
235*f4ee7fbaSAndroid Build Coastguard Worker ++histo[copylen + 14];
236*f4ee7fbaSAndroid Build Coastguard Worker } else if (copylen < 134) {
237*f4ee7fbaSAndroid Build Coastguard Worker const size_t tail = copylen - 6;
238*f4ee7fbaSAndroid Build Coastguard Worker const uint32_t nbits = Log2FloorNonZero(tail) - 1u;
239*f4ee7fbaSAndroid Build Coastguard Worker const size_t prefix = tail >> nbits;
240*f4ee7fbaSAndroid Build Coastguard Worker const size_t code = (nbits << 1) + prefix + 20;
241*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[code], bits[code], storage_ix, storage);
242*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(nbits, tail - (prefix << nbits), storage_ix, storage);
243*f4ee7fbaSAndroid Build Coastguard Worker ++histo[code];
244*f4ee7fbaSAndroid Build Coastguard Worker } else if (copylen < 2118) {
245*f4ee7fbaSAndroid Build Coastguard Worker const size_t tail = copylen - 70;
246*f4ee7fbaSAndroid Build Coastguard Worker const uint32_t nbits = Log2FloorNonZero(tail);
247*f4ee7fbaSAndroid Build Coastguard Worker const size_t code = nbits + 28;
248*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[code], bits[code], storage_ix, storage);
249*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(nbits, tail - ((size_t)1 << nbits), storage_ix, storage);
250*f4ee7fbaSAndroid Build Coastguard Worker ++histo[code];
251*f4ee7fbaSAndroid Build Coastguard Worker } else {
252*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[39], bits[39], storage_ix, storage);
253*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(24, copylen - 2118, storage_ix, storage);
254*f4ee7fbaSAndroid Build Coastguard Worker ++histo[39];
255*f4ee7fbaSAndroid Build Coastguard Worker }
256*f4ee7fbaSAndroid Build Coastguard Worker }
257*f4ee7fbaSAndroid Build Coastguard Worker
EmitCopyLenLastDistance(size_t copylen,const uint8_t depth[128],const uint16_t bits[128],uint32_t histo[128],size_t * storage_ix,uint8_t * storage)258*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE void EmitCopyLenLastDistance(size_t copylen,
259*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t depth[128],
260*f4ee7fbaSAndroid Build Coastguard Worker const uint16_t bits[128],
261*f4ee7fbaSAndroid Build Coastguard Worker uint32_t histo[128],
262*f4ee7fbaSAndroid Build Coastguard Worker size_t* storage_ix,
263*f4ee7fbaSAndroid Build Coastguard Worker uint8_t* storage) {
264*f4ee7fbaSAndroid Build Coastguard Worker if (copylen < 12) {
265*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[copylen - 4], bits[copylen - 4], storage_ix, storage);
266*f4ee7fbaSAndroid Build Coastguard Worker ++histo[copylen - 4];
267*f4ee7fbaSAndroid Build Coastguard Worker } else if (copylen < 72) {
268*f4ee7fbaSAndroid Build Coastguard Worker const size_t tail = copylen - 8;
269*f4ee7fbaSAndroid Build Coastguard Worker const uint32_t nbits = Log2FloorNonZero(tail) - 1;
270*f4ee7fbaSAndroid Build Coastguard Worker const size_t prefix = tail >> nbits;
271*f4ee7fbaSAndroid Build Coastguard Worker const size_t code = (nbits << 1) + prefix + 4;
272*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[code], bits[code], storage_ix, storage);
273*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(nbits, tail - (prefix << nbits), storage_ix, storage);
274*f4ee7fbaSAndroid Build Coastguard Worker ++histo[code];
275*f4ee7fbaSAndroid Build Coastguard Worker } else if (copylen < 136) {
276*f4ee7fbaSAndroid Build Coastguard Worker const size_t tail = copylen - 8;
277*f4ee7fbaSAndroid Build Coastguard Worker const size_t code = (tail >> 5) + 30;
278*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[code], bits[code], storage_ix, storage);
279*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(5, tail & 31, storage_ix, storage);
280*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[64], bits[64], storage_ix, storage);
281*f4ee7fbaSAndroid Build Coastguard Worker ++histo[code];
282*f4ee7fbaSAndroid Build Coastguard Worker ++histo[64];
283*f4ee7fbaSAndroid Build Coastguard Worker } else if (copylen < 2120) {
284*f4ee7fbaSAndroid Build Coastguard Worker const size_t tail = copylen - 72;
285*f4ee7fbaSAndroid Build Coastguard Worker const uint32_t nbits = Log2FloorNonZero(tail);
286*f4ee7fbaSAndroid Build Coastguard Worker const size_t code = nbits + 28;
287*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[code], bits[code], storage_ix, storage);
288*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(nbits, tail - ((size_t)1 << nbits), storage_ix, storage);
289*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[64], bits[64], storage_ix, storage);
290*f4ee7fbaSAndroid Build Coastguard Worker ++histo[code];
291*f4ee7fbaSAndroid Build Coastguard Worker ++histo[64];
292*f4ee7fbaSAndroid Build Coastguard Worker } else {
293*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[39], bits[39], storage_ix, storage);
294*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(24, copylen - 2120, storage_ix, storage);
295*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[64], bits[64], storage_ix, storage);
296*f4ee7fbaSAndroid Build Coastguard Worker ++histo[39];
297*f4ee7fbaSAndroid Build Coastguard Worker ++histo[64];
298*f4ee7fbaSAndroid Build Coastguard Worker }
299*f4ee7fbaSAndroid Build Coastguard Worker }
300*f4ee7fbaSAndroid Build Coastguard Worker
EmitDistance(size_t distance,const uint8_t depth[128],const uint16_t bits[128],uint32_t histo[128],size_t * storage_ix,uint8_t * storage)301*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE void EmitDistance(size_t distance,
302*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t depth[128],
303*f4ee7fbaSAndroid Build Coastguard Worker const uint16_t bits[128],
304*f4ee7fbaSAndroid Build Coastguard Worker uint32_t histo[128],
305*f4ee7fbaSAndroid Build Coastguard Worker size_t* storage_ix, uint8_t* storage) {
306*f4ee7fbaSAndroid Build Coastguard Worker const size_t d = distance + 3;
307*f4ee7fbaSAndroid Build Coastguard Worker const uint32_t nbits = Log2FloorNonZero(d) - 1u;
308*f4ee7fbaSAndroid Build Coastguard Worker const size_t prefix = (d >> nbits) & 1;
309*f4ee7fbaSAndroid Build Coastguard Worker const size_t offset = (2 + prefix) << nbits;
310*f4ee7fbaSAndroid Build Coastguard Worker const size_t distcode = 2 * (nbits - 1) + prefix + 80;
311*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[distcode], bits[distcode], storage_ix, storage);
312*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(nbits, d - offset, storage_ix, storage);
313*f4ee7fbaSAndroid Build Coastguard Worker ++histo[distcode];
314*f4ee7fbaSAndroid Build Coastguard Worker }
315*f4ee7fbaSAndroid Build Coastguard Worker
EmitLiterals(const uint8_t * input,const size_t len,const uint8_t depth[256],const uint16_t bits[256],size_t * storage_ix,uint8_t * storage)316*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE void EmitLiterals(const uint8_t* input, const size_t len,
317*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t depth[256],
318*f4ee7fbaSAndroid Build Coastguard Worker const uint16_t bits[256],
319*f4ee7fbaSAndroid Build Coastguard Worker size_t* storage_ix, uint8_t* storage) {
320*f4ee7fbaSAndroid Build Coastguard Worker size_t j;
321*f4ee7fbaSAndroid Build Coastguard Worker for (j = 0; j < len; j++) {
322*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t lit = input[j];
323*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(depth[lit], bits[lit], storage_ix, storage);
324*f4ee7fbaSAndroid Build Coastguard Worker }
325*f4ee7fbaSAndroid Build Coastguard Worker }
326*f4ee7fbaSAndroid Build Coastguard Worker
327*f4ee7fbaSAndroid Build Coastguard Worker /* REQUIRES: len <= 1 << 24. */
BrotliStoreMetaBlockHeader(size_t len,BROTLI_BOOL is_uncompressed,size_t * storage_ix,uint8_t * storage)328*f4ee7fbaSAndroid Build Coastguard Worker static void BrotliStoreMetaBlockHeader(
329*f4ee7fbaSAndroid Build Coastguard Worker size_t len, BROTLI_BOOL is_uncompressed, size_t* storage_ix,
330*f4ee7fbaSAndroid Build Coastguard Worker uint8_t* storage) {
331*f4ee7fbaSAndroid Build Coastguard Worker size_t nibbles = 6;
332*f4ee7fbaSAndroid Build Coastguard Worker /* ISLAST */
333*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(1, 0, storage_ix, storage);
334*f4ee7fbaSAndroid Build Coastguard Worker if (len <= (1U << 16)) {
335*f4ee7fbaSAndroid Build Coastguard Worker nibbles = 4;
336*f4ee7fbaSAndroid Build Coastguard Worker } else if (len <= (1U << 20)) {
337*f4ee7fbaSAndroid Build Coastguard Worker nibbles = 5;
338*f4ee7fbaSAndroid Build Coastguard Worker }
339*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(2, nibbles - 4, storage_ix, storage);
340*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(nibbles * 4, len - 1, storage_ix, storage);
341*f4ee7fbaSAndroid Build Coastguard Worker /* ISUNCOMPRESSED */
342*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(1, (uint64_t)is_uncompressed, storage_ix, storage);
343*f4ee7fbaSAndroid Build Coastguard Worker }
344*f4ee7fbaSAndroid Build Coastguard Worker
UpdateBits(size_t n_bits,uint32_t bits,size_t pos,uint8_t * array)345*f4ee7fbaSAndroid Build Coastguard Worker static void UpdateBits(size_t n_bits, uint32_t bits, size_t pos,
346*f4ee7fbaSAndroid Build Coastguard Worker uint8_t* array) {
347*f4ee7fbaSAndroid Build Coastguard Worker while (n_bits > 0) {
348*f4ee7fbaSAndroid Build Coastguard Worker size_t byte_pos = pos >> 3;
349*f4ee7fbaSAndroid Build Coastguard Worker size_t n_unchanged_bits = pos & 7;
350*f4ee7fbaSAndroid Build Coastguard Worker size_t n_changed_bits = BROTLI_MIN(size_t, n_bits, 8 - n_unchanged_bits);
351*f4ee7fbaSAndroid Build Coastguard Worker size_t total_bits = n_unchanged_bits + n_changed_bits;
352*f4ee7fbaSAndroid Build Coastguard Worker uint32_t mask =
353*f4ee7fbaSAndroid Build Coastguard Worker (~((1u << total_bits) - 1u)) | ((1u << n_unchanged_bits) - 1u);
354*f4ee7fbaSAndroid Build Coastguard Worker uint32_t unchanged_bits = array[byte_pos] & mask;
355*f4ee7fbaSAndroid Build Coastguard Worker uint32_t changed_bits = bits & ((1u << n_changed_bits) - 1u);
356*f4ee7fbaSAndroid Build Coastguard Worker array[byte_pos] =
357*f4ee7fbaSAndroid Build Coastguard Worker (uint8_t)((changed_bits << n_unchanged_bits) | unchanged_bits);
358*f4ee7fbaSAndroid Build Coastguard Worker n_bits -= n_changed_bits;
359*f4ee7fbaSAndroid Build Coastguard Worker bits >>= n_changed_bits;
360*f4ee7fbaSAndroid Build Coastguard Worker pos += n_changed_bits;
361*f4ee7fbaSAndroid Build Coastguard Worker }
362*f4ee7fbaSAndroid Build Coastguard Worker }
363*f4ee7fbaSAndroid Build Coastguard Worker
RewindBitPosition(const size_t new_storage_ix,size_t * storage_ix,uint8_t * storage)364*f4ee7fbaSAndroid Build Coastguard Worker static void RewindBitPosition(const size_t new_storage_ix,
365*f4ee7fbaSAndroid Build Coastguard Worker size_t* storage_ix, uint8_t* storage) {
366*f4ee7fbaSAndroid Build Coastguard Worker const size_t bitpos = new_storage_ix & 7;
367*f4ee7fbaSAndroid Build Coastguard Worker const size_t mask = (1u << bitpos) - 1;
368*f4ee7fbaSAndroid Build Coastguard Worker storage[new_storage_ix >> 3] &= (uint8_t)mask;
369*f4ee7fbaSAndroid Build Coastguard Worker *storage_ix = new_storage_ix;
370*f4ee7fbaSAndroid Build Coastguard Worker }
371*f4ee7fbaSAndroid Build Coastguard Worker
ShouldMergeBlock(const uint8_t * data,size_t len,const uint8_t * depths)372*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_BOOL ShouldMergeBlock(
373*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* data, size_t len, const uint8_t* depths) {
374*f4ee7fbaSAndroid Build Coastguard Worker size_t histo[256] = { 0 };
375*f4ee7fbaSAndroid Build Coastguard Worker static const size_t kSampleRate = 43;
376*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
377*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < len; i += kSampleRate) {
378*f4ee7fbaSAndroid Build Coastguard Worker ++histo[data[i]];
379*f4ee7fbaSAndroid Build Coastguard Worker }
380*f4ee7fbaSAndroid Build Coastguard Worker {
381*f4ee7fbaSAndroid Build Coastguard Worker const size_t total = (len + kSampleRate - 1) / kSampleRate;
382*f4ee7fbaSAndroid Build Coastguard Worker double r = (FastLog2(total) + 0.5) * (double)total + 200;
383*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < 256; ++i) {
384*f4ee7fbaSAndroid Build Coastguard Worker r -= (double)histo[i] * (depths[i] + FastLog2(histo[i]));
385*f4ee7fbaSAndroid Build Coastguard Worker }
386*f4ee7fbaSAndroid Build Coastguard Worker return TO_BROTLI_BOOL(r >= 0.0);
387*f4ee7fbaSAndroid Build Coastguard Worker }
388*f4ee7fbaSAndroid Build Coastguard Worker }
389*f4ee7fbaSAndroid Build Coastguard Worker
390*f4ee7fbaSAndroid Build Coastguard Worker /* Acceptable loss for uncompressible speedup is 2% */
391*f4ee7fbaSAndroid Build Coastguard Worker #define MIN_RATIO 980
392*f4ee7fbaSAndroid Build Coastguard Worker
ShouldUseUncompressedMode(const uint8_t * metablock_start,const uint8_t * next_emit,const size_t insertlen,const size_t literal_ratio)393*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE BROTLI_BOOL ShouldUseUncompressedMode(
394*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* metablock_start, const uint8_t* next_emit,
395*f4ee7fbaSAndroid Build Coastguard Worker const size_t insertlen, const size_t literal_ratio) {
396*f4ee7fbaSAndroid Build Coastguard Worker const size_t compressed = (size_t)(next_emit - metablock_start);
397*f4ee7fbaSAndroid Build Coastguard Worker if (compressed * 50 > insertlen) {
398*f4ee7fbaSAndroid Build Coastguard Worker return BROTLI_FALSE;
399*f4ee7fbaSAndroid Build Coastguard Worker } else {
400*f4ee7fbaSAndroid Build Coastguard Worker return TO_BROTLI_BOOL(literal_ratio > MIN_RATIO);
401*f4ee7fbaSAndroid Build Coastguard Worker }
402*f4ee7fbaSAndroid Build Coastguard Worker }
403*f4ee7fbaSAndroid Build Coastguard Worker
EmitUncompressedMetaBlock(const uint8_t * begin,const uint8_t * end,const size_t storage_ix_start,size_t * storage_ix,uint8_t * storage)404*f4ee7fbaSAndroid Build Coastguard Worker static void EmitUncompressedMetaBlock(const uint8_t* begin, const uint8_t* end,
405*f4ee7fbaSAndroid Build Coastguard Worker const size_t storage_ix_start,
406*f4ee7fbaSAndroid Build Coastguard Worker size_t* storage_ix, uint8_t* storage) {
407*f4ee7fbaSAndroid Build Coastguard Worker const size_t len = (size_t)(end - begin);
408*f4ee7fbaSAndroid Build Coastguard Worker RewindBitPosition(storage_ix_start, storage_ix, storage);
409*f4ee7fbaSAndroid Build Coastguard Worker BrotliStoreMetaBlockHeader(len, 1, storage_ix, storage);
410*f4ee7fbaSAndroid Build Coastguard Worker *storage_ix = (*storage_ix + 7u) & ~7u;
411*f4ee7fbaSAndroid Build Coastguard Worker memcpy(&storage[*storage_ix >> 3], begin, len);
412*f4ee7fbaSAndroid Build Coastguard Worker *storage_ix += len << 3;
413*f4ee7fbaSAndroid Build Coastguard Worker storage[*storage_ix >> 3] = 0;
414*f4ee7fbaSAndroid Build Coastguard Worker }
415*f4ee7fbaSAndroid Build Coastguard Worker
416*f4ee7fbaSAndroid Build Coastguard Worker static uint32_t kCmdHistoSeed[128] = {
417*f4ee7fbaSAndroid Build Coastguard Worker 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1,
418*f4ee7fbaSAndroid Build Coastguard Worker 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
419*f4ee7fbaSAndroid Build Coastguard Worker 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
420*f4ee7fbaSAndroid Build Coastguard Worker 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
421*f4ee7fbaSAndroid Build Coastguard Worker 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
422*f4ee7fbaSAndroid Build Coastguard Worker 1, 1, 1, 1, 0, 0, 0, 0,
423*f4ee7fbaSAndroid Build Coastguard Worker };
424*f4ee7fbaSAndroid Build Coastguard Worker
BrotliCompressFragmentFastImpl(MemoryManager * m,const uint8_t * input,size_t input_size,BROTLI_BOOL is_last,int * table,size_t table_bits,uint8_t cmd_depth[128],uint16_t cmd_bits[128],size_t * cmd_code_numbits,uint8_t * cmd_code,size_t * storage_ix,uint8_t * storage)425*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE void BrotliCompressFragmentFastImpl(
426*f4ee7fbaSAndroid Build Coastguard Worker MemoryManager* m, const uint8_t* input, size_t input_size,
427*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_BOOL is_last, int* table, size_t table_bits, uint8_t cmd_depth[128],
428*f4ee7fbaSAndroid Build Coastguard Worker uint16_t cmd_bits[128], size_t* cmd_code_numbits, uint8_t* cmd_code,
429*f4ee7fbaSAndroid Build Coastguard Worker size_t* storage_ix, uint8_t* storage) {
430*f4ee7fbaSAndroid Build Coastguard Worker uint32_t cmd_histo[128];
431*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* ip_end;
432*f4ee7fbaSAndroid Build Coastguard Worker
433*f4ee7fbaSAndroid Build Coastguard Worker /* "next_emit" is a pointer to the first byte that is not covered by a
434*f4ee7fbaSAndroid Build Coastguard Worker previous copy. Bytes between "next_emit" and the start of the next copy or
435*f4ee7fbaSAndroid Build Coastguard Worker the end of the input will be emitted as literal bytes. */
436*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* next_emit = input;
437*f4ee7fbaSAndroid Build Coastguard Worker /* Save the start of the first block for position and distance computations.
438*f4ee7fbaSAndroid Build Coastguard Worker */
439*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* base_ip = input;
440*f4ee7fbaSAndroid Build Coastguard Worker
441*f4ee7fbaSAndroid Build Coastguard Worker static const size_t kFirstBlockSize = 3 << 15;
442*f4ee7fbaSAndroid Build Coastguard Worker static const size_t kMergeBlockSize = 1 << 16;
443*f4ee7fbaSAndroid Build Coastguard Worker
444*f4ee7fbaSAndroid Build Coastguard Worker const size_t kInputMarginBytes = BROTLI_WINDOW_GAP;
445*f4ee7fbaSAndroid Build Coastguard Worker const size_t kMinMatchLen = 5;
446*f4ee7fbaSAndroid Build Coastguard Worker
447*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* metablock_start = input;
448*f4ee7fbaSAndroid Build Coastguard Worker size_t block_size = BROTLI_MIN(size_t, input_size, kFirstBlockSize);
449*f4ee7fbaSAndroid Build Coastguard Worker size_t total_block_size = block_size;
450*f4ee7fbaSAndroid Build Coastguard Worker /* Save the bit position of the MLEN field of the meta-block header, so that
451*f4ee7fbaSAndroid Build Coastguard Worker we can update it later if we decide to extend this meta-block. */
452*f4ee7fbaSAndroid Build Coastguard Worker size_t mlen_storage_ix = *storage_ix + 3;
453*f4ee7fbaSAndroid Build Coastguard Worker
454*f4ee7fbaSAndroid Build Coastguard Worker uint8_t lit_depth[256];
455*f4ee7fbaSAndroid Build Coastguard Worker uint16_t lit_bits[256];
456*f4ee7fbaSAndroid Build Coastguard Worker
457*f4ee7fbaSAndroid Build Coastguard Worker size_t literal_ratio;
458*f4ee7fbaSAndroid Build Coastguard Worker
459*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* ip;
460*f4ee7fbaSAndroid Build Coastguard Worker int last_distance;
461*f4ee7fbaSAndroid Build Coastguard Worker
462*f4ee7fbaSAndroid Build Coastguard Worker const size_t shift = 64u - table_bits;
463*f4ee7fbaSAndroid Build Coastguard Worker
464*f4ee7fbaSAndroid Build Coastguard Worker BrotliStoreMetaBlockHeader(block_size, 0, storage_ix, storage);
465*f4ee7fbaSAndroid Build Coastguard Worker /* No block splits, no contexts. */
466*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(13, 0, storage_ix, storage);
467*f4ee7fbaSAndroid Build Coastguard Worker
468*f4ee7fbaSAndroid Build Coastguard Worker literal_ratio = BuildAndStoreLiteralPrefixCode(
469*f4ee7fbaSAndroid Build Coastguard Worker m, input, block_size, lit_depth, lit_bits, storage_ix, storage);
470*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m)) return;
471*f4ee7fbaSAndroid Build Coastguard Worker
472*f4ee7fbaSAndroid Build Coastguard Worker {
473*f4ee7fbaSAndroid Build Coastguard Worker /* Store the pre-compressed command and distance prefix codes. */
474*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
475*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i + 7 < *cmd_code_numbits; i += 8) {
476*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(8, cmd_code[i >> 3], storage_ix, storage);
477*f4ee7fbaSAndroid Build Coastguard Worker }
478*f4ee7fbaSAndroid Build Coastguard Worker }
479*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(*cmd_code_numbits & 7, cmd_code[*cmd_code_numbits >> 3],
480*f4ee7fbaSAndroid Build Coastguard Worker storage_ix, storage);
481*f4ee7fbaSAndroid Build Coastguard Worker
482*f4ee7fbaSAndroid Build Coastguard Worker emit_commands:
483*f4ee7fbaSAndroid Build Coastguard Worker /* Initialize the command and distance histograms. We will gather
484*f4ee7fbaSAndroid Build Coastguard Worker statistics of command and distance codes during the processing
485*f4ee7fbaSAndroid Build Coastguard Worker of this block and use it to update the command and distance
486*f4ee7fbaSAndroid Build Coastguard Worker prefix codes for the next block. */
487*f4ee7fbaSAndroid Build Coastguard Worker memcpy(cmd_histo, kCmdHistoSeed, sizeof(kCmdHistoSeed));
488*f4ee7fbaSAndroid Build Coastguard Worker
489*f4ee7fbaSAndroid Build Coastguard Worker /* "ip" is the input pointer. */
490*f4ee7fbaSAndroid Build Coastguard Worker ip = input;
491*f4ee7fbaSAndroid Build Coastguard Worker last_distance = -1;
492*f4ee7fbaSAndroid Build Coastguard Worker ip_end = input + block_size;
493*f4ee7fbaSAndroid Build Coastguard Worker
494*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_PREDICT_TRUE(block_size >= kInputMarginBytes)) {
495*f4ee7fbaSAndroid Build Coastguard Worker /* For the last block, we need to keep a 16 bytes margin so that we can be
496*f4ee7fbaSAndroid Build Coastguard Worker sure that all distances are at most window size - 16.
497*f4ee7fbaSAndroid Build Coastguard Worker For all other blocks, we only need to keep a margin of 5 bytes so that
498*f4ee7fbaSAndroid Build Coastguard Worker we don't go over the block size with a copy. */
499*f4ee7fbaSAndroid Build Coastguard Worker const size_t len_limit = BROTLI_MIN(size_t, block_size - kMinMatchLen,
500*f4ee7fbaSAndroid Build Coastguard Worker input_size - kInputMarginBytes);
501*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* ip_limit = input + len_limit;
502*f4ee7fbaSAndroid Build Coastguard Worker
503*f4ee7fbaSAndroid Build Coastguard Worker uint32_t next_hash;
504*f4ee7fbaSAndroid Build Coastguard Worker for (next_hash = Hash(++ip, shift); ; ) {
505*f4ee7fbaSAndroid Build Coastguard Worker /* Step 1: Scan forward in the input looking for a 5-byte-long match.
506*f4ee7fbaSAndroid Build Coastguard Worker If we get close to exhausting the input then goto emit_remainder.
507*f4ee7fbaSAndroid Build Coastguard Worker
508*f4ee7fbaSAndroid Build Coastguard Worker Heuristic match skipping: If 32 bytes are scanned with no matches
509*f4ee7fbaSAndroid Build Coastguard Worker found, start looking only at every other byte. If 32 more bytes are
510*f4ee7fbaSAndroid Build Coastguard Worker scanned, look at every third byte, etc.. When a match is found,
511*f4ee7fbaSAndroid Build Coastguard Worker immediately go back to looking at every byte. This is a small loss
512*f4ee7fbaSAndroid Build Coastguard Worker (~5% performance, ~0.1% density) for compressible data due to more
513*f4ee7fbaSAndroid Build Coastguard Worker bookkeeping, but for non-compressible data (such as JPEG) it's a huge
514*f4ee7fbaSAndroid Build Coastguard Worker win since the compressor quickly "realizes" the data is incompressible
515*f4ee7fbaSAndroid Build Coastguard Worker and doesn't bother looking for matches everywhere.
516*f4ee7fbaSAndroid Build Coastguard Worker
517*f4ee7fbaSAndroid Build Coastguard Worker The "skip" variable keeps track of how many bytes there are since the
518*f4ee7fbaSAndroid Build Coastguard Worker last match; dividing it by 32 (i.e. right-shifting by five) gives the
519*f4ee7fbaSAndroid Build Coastguard Worker number of bytes to move ahead for each iteration. */
520*f4ee7fbaSAndroid Build Coastguard Worker uint32_t skip = 32;
521*f4ee7fbaSAndroid Build Coastguard Worker
522*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* next_ip = ip;
523*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* candidate;
524*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(next_emit < ip);
525*f4ee7fbaSAndroid Build Coastguard Worker trawl:
526*f4ee7fbaSAndroid Build Coastguard Worker do {
527*f4ee7fbaSAndroid Build Coastguard Worker uint32_t hash = next_hash;
528*f4ee7fbaSAndroid Build Coastguard Worker uint32_t bytes_between_hash_lookups = skip++ >> 5;
529*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(hash == Hash(next_ip, shift));
530*f4ee7fbaSAndroid Build Coastguard Worker ip = next_ip;
531*f4ee7fbaSAndroid Build Coastguard Worker next_ip = ip + bytes_between_hash_lookups;
532*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_PREDICT_FALSE(next_ip > ip_limit)) {
533*f4ee7fbaSAndroid Build Coastguard Worker goto emit_remainder;
534*f4ee7fbaSAndroid Build Coastguard Worker }
535*f4ee7fbaSAndroid Build Coastguard Worker next_hash = Hash(next_ip, shift);
536*f4ee7fbaSAndroid Build Coastguard Worker candidate = ip - last_distance;
537*f4ee7fbaSAndroid Build Coastguard Worker if (IsMatch(ip, candidate)) {
538*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_PREDICT_TRUE(candidate < ip)) {
539*f4ee7fbaSAndroid Build Coastguard Worker table[hash] = (int)(ip - base_ip);
540*f4ee7fbaSAndroid Build Coastguard Worker break;
541*f4ee7fbaSAndroid Build Coastguard Worker }
542*f4ee7fbaSAndroid Build Coastguard Worker }
543*f4ee7fbaSAndroid Build Coastguard Worker candidate = base_ip + table[hash];
544*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(candidate >= base_ip);
545*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(candidate < ip);
546*f4ee7fbaSAndroid Build Coastguard Worker
547*f4ee7fbaSAndroid Build Coastguard Worker table[hash] = (int)(ip - base_ip);
548*f4ee7fbaSAndroid Build Coastguard Worker } while (BROTLI_PREDICT_TRUE(!IsMatch(ip, candidate)));
549*f4ee7fbaSAndroid Build Coastguard Worker
550*f4ee7fbaSAndroid Build Coastguard Worker /* Check copy distance. If candidate is not feasible, continue search.
551*f4ee7fbaSAndroid Build Coastguard Worker Checking is done outside of hot loop to reduce overhead. */
552*f4ee7fbaSAndroid Build Coastguard Worker if (ip - candidate > MAX_DISTANCE) goto trawl;
553*f4ee7fbaSAndroid Build Coastguard Worker
554*f4ee7fbaSAndroid Build Coastguard Worker /* Step 2: Emit the found match together with the literal bytes from
555*f4ee7fbaSAndroid Build Coastguard Worker "next_emit" to the bit stream, and then see if we can find a next match
556*f4ee7fbaSAndroid Build Coastguard Worker immediately afterwards. Repeat until we find no match for the input
557*f4ee7fbaSAndroid Build Coastguard Worker without emitting some literal bytes. */
558*f4ee7fbaSAndroid Build Coastguard Worker
559*f4ee7fbaSAndroid Build Coastguard Worker {
560*f4ee7fbaSAndroid Build Coastguard Worker /* We have a 5-byte match at ip, and we need to emit bytes in
561*f4ee7fbaSAndroid Build Coastguard Worker [next_emit, ip). */
562*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* base = ip;
563*f4ee7fbaSAndroid Build Coastguard Worker size_t matched = 5 + FindMatchLengthWithLimit(
564*f4ee7fbaSAndroid Build Coastguard Worker candidate + 5, ip + 5, (size_t)(ip_end - ip) - 5);
565*f4ee7fbaSAndroid Build Coastguard Worker int distance = (int)(base - candidate); /* > 0 */
566*f4ee7fbaSAndroid Build Coastguard Worker size_t insert = (size_t)(base - next_emit);
567*f4ee7fbaSAndroid Build Coastguard Worker ip += matched;
568*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(0 == memcmp(base, candidate, matched));
569*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_PREDICT_TRUE(insert < 6210)) {
570*f4ee7fbaSAndroid Build Coastguard Worker EmitInsertLen(insert, cmd_depth, cmd_bits, cmd_histo,
571*f4ee7fbaSAndroid Build Coastguard Worker storage_ix, storage);
572*f4ee7fbaSAndroid Build Coastguard Worker } else if (ShouldUseUncompressedMode(metablock_start, next_emit, insert,
573*f4ee7fbaSAndroid Build Coastguard Worker literal_ratio)) {
574*f4ee7fbaSAndroid Build Coastguard Worker EmitUncompressedMetaBlock(metablock_start, base, mlen_storage_ix - 3,
575*f4ee7fbaSAndroid Build Coastguard Worker storage_ix, storage);
576*f4ee7fbaSAndroid Build Coastguard Worker input_size -= (size_t)(base - input);
577*f4ee7fbaSAndroid Build Coastguard Worker input = base;
578*f4ee7fbaSAndroid Build Coastguard Worker next_emit = input;
579*f4ee7fbaSAndroid Build Coastguard Worker goto next_block;
580*f4ee7fbaSAndroid Build Coastguard Worker } else {
581*f4ee7fbaSAndroid Build Coastguard Worker EmitLongInsertLen(insert, cmd_depth, cmd_bits, cmd_histo,
582*f4ee7fbaSAndroid Build Coastguard Worker storage_ix, storage);
583*f4ee7fbaSAndroid Build Coastguard Worker }
584*f4ee7fbaSAndroid Build Coastguard Worker EmitLiterals(next_emit, insert, lit_depth, lit_bits,
585*f4ee7fbaSAndroid Build Coastguard Worker storage_ix, storage);
586*f4ee7fbaSAndroid Build Coastguard Worker if (distance == last_distance) {
587*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(cmd_depth[64], cmd_bits[64], storage_ix, storage);
588*f4ee7fbaSAndroid Build Coastguard Worker ++cmd_histo[64];
589*f4ee7fbaSAndroid Build Coastguard Worker } else {
590*f4ee7fbaSAndroid Build Coastguard Worker EmitDistance((size_t)distance, cmd_depth, cmd_bits,
591*f4ee7fbaSAndroid Build Coastguard Worker cmd_histo, storage_ix, storage);
592*f4ee7fbaSAndroid Build Coastguard Worker last_distance = distance;
593*f4ee7fbaSAndroid Build Coastguard Worker }
594*f4ee7fbaSAndroid Build Coastguard Worker EmitCopyLenLastDistance(matched, cmd_depth, cmd_bits, cmd_histo,
595*f4ee7fbaSAndroid Build Coastguard Worker storage_ix, storage);
596*f4ee7fbaSAndroid Build Coastguard Worker
597*f4ee7fbaSAndroid Build Coastguard Worker next_emit = ip;
598*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_PREDICT_FALSE(ip >= ip_limit)) {
599*f4ee7fbaSAndroid Build Coastguard Worker goto emit_remainder;
600*f4ee7fbaSAndroid Build Coastguard Worker }
601*f4ee7fbaSAndroid Build Coastguard Worker /* We could immediately start working at ip now, but to improve
602*f4ee7fbaSAndroid Build Coastguard Worker compression we first update "table" with the hashes of some positions
603*f4ee7fbaSAndroid Build Coastguard Worker within the last copy. */
604*f4ee7fbaSAndroid Build Coastguard Worker {
605*f4ee7fbaSAndroid Build Coastguard Worker uint64_t input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 3);
606*f4ee7fbaSAndroid Build Coastguard Worker uint32_t prev_hash = HashBytesAtOffset(input_bytes, 0, shift);
607*f4ee7fbaSAndroid Build Coastguard Worker uint32_t cur_hash = HashBytesAtOffset(input_bytes, 3, shift);
608*f4ee7fbaSAndroid Build Coastguard Worker table[prev_hash] = (int)(ip - base_ip - 3);
609*f4ee7fbaSAndroid Build Coastguard Worker prev_hash = HashBytesAtOffset(input_bytes, 1, shift);
610*f4ee7fbaSAndroid Build Coastguard Worker table[prev_hash] = (int)(ip - base_ip - 2);
611*f4ee7fbaSAndroid Build Coastguard Worker prev_hash = HashBytesAtOffset(input_bytes, 2, shift);
612*f4ee7fbaSAndroid Build Coastguard Worker table[prev_hash] = (int)(ip - base_ip - 1);
613*f4ee7fbaSAndroid Build Coastguard Worker
614*f4ee7fbaSAndroid Build Coastguard Worker candidate = base_ip + table[cur_hash];
615*f4ee7fbaSAndroid Build Coastguard Worker table[cur_hash] = (int)(ip - base_ip);
616*f4ee7fbaSAndroid Build Coastguard Worker }
617*f4ee7fbaSAndroid Build Coastguard Worker }
618*f4ee7fbaSAndroid Build Coastguard Worker
619*f4ee7fbaSAndroid Build Coastguard Worker while (IsMatch(ip, candidate)) {
620*f4ee7fbaSAndroid Build Coastguard Worker /* We have a 5-byte match at ip, and no need to emit any literal bytes
621*f4ee7fbaSAndroid Build Coastguard Worker prior to ip. */
622*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* base = ip;
623*f4ee7fbaSAndroid Build Coastguard Worker size_t matched = 5 + FindMatchLengthWithLimit(
624*f4ee7fbaSAndroid Build Coastguard Worker candidate + 5, ip + 5, (size_t)(ip_end - ip) - 5);
625*f4ee7fbaSAndroid Build Coastguard Worker if (ip - candidate > MAX_DISTANCE) break;
626*f4ee7fbaSAndroid Build Coastguard Worker ip += matched;
627*f4ee7fbaSAndroid Build Coastguard Worker last_distance = (int)(base - candidate); /* > 0 */
628*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(0 == memcmp(base, candidate, matched));
629*f4ee7fbaSAndroid Build Coastguard Worker EmitCopyLen(matched, cmd_depth, cmd_bits, cmd_histo,
630*f4ee7fbaSAndroid Build Coastguard Worker storage_ix, storage);
631*f4ee7fbaSAndroid Build Coastguard Worker EmitDistance((size_t)last_distance, cmd_depth, cmd_bits,
632*f4ee7fbaSAndroid Build Coastguard Worker cmd_histo, storage_ix, storage);
633*f4ee7fbaSAndroid Build Coastguard Worker
634*f4ee7fbaSAndroid Build Coastguard Worker next_emit = ip;
635*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_PREDICT_FALSE(ip >= ip_limit)) {
636*f4ee7fbaSAndroid Build Coastguard Worker goto emit_remainder;
637*f4ee7fbaSAndroid Build Coastguard Worker }
638*f4ee7fbaSAndroid Build Coastguard Worker /* We could immediately start working at ip now, but to improve
639*f4ee7fbaSAndroid Build Coastguard Worker compression we first update "table" with the hashes of some positions
640*f4ee7fbaSAndroid Build Coastguard Worker within the last copy. */
641*f4ee7fbaSAndroid Build Coastguard Worker {
642*f4ee7fbaSAndroid Build Coastguard Worker uint64_t input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 3);
643*f4ee7fbaSAndroid Build Coastguard Worker uint32_t prev_hash = HashBytesAtOffset(input_bytes, 0, shift);
644*f4ee7fbaSAndroid Build Coastguard Worker uint32_t cur_hash = HashBytesAtOffset(input_bytes, 3, shift);
645*f4ee7fbaSAndroid Build Coastguard Worker table[prev_hash] = (int)(ip - base_ip - 3);
646*f4ee7fbaSAndroid Build Coastguard Worker prev_hash = HashBytesAtOffset(input_bytes, 1, shift);
647*f4ee7fbaSAndroid Build Coastguard Worker table[prev_hash] = (int)(ip - base_ip - 2);
648*f4ee7fbaSAndroid Build Coastguard Worker prev_hash = HashBytesAtOffset(input_bytes, 2, shift);
649*f4ee7fbaSAndroid Build Coastguard Worker table[prev_hash] = (int)(ip - base_ip - 1);
650*f4ee7fbaSAndroid Build Coastguard Worker
651*f4ee7fbaSAndroid Build Coastguard Worker candidate = base_ip + table[cur_hash];
652*f4ee7fbaSAndroid Build Coastguard Worker table[cur_hash] = (int)(ip - base_ip);
653*f4ee7fbaSAndroid Build Coastguard Worker }
654*f4ee7fbaSAndroid Build Coastguard Worker }
655*f4ee7fbaSAndroid Build Coastguard Worker
656*f4ee7fbaSAndroid Build Coastguard Worker next_hash = Hash(++ip, shift);
657*f4ee7fbaSAndroid Build Coastguard Worker }
658*f4ee7fbaSAndroid Build Coastguard Worker }
659*f4ee7fbaSAndroid Build Coastguard Worker
660*f4ee7fbaSAndroid Build Coastguard Worker emit_remainder:
661*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(next_emit <= ip_end);
662*f4ee7fbaSAndroid Build Coastguard Worker input += block_size;
663*f4ee7fbaSAndroid Build Coastguard Worker input_size -= block_size;
664*f4ee7fbaSAndroid Build Coastguard Worker block_size = BROTLI_MIN(size_t, input_size, kMergeBlockSize);
665*f4ee7fbaSAndroid Build Coastguard Worker
666*f4ee7fbaSAndroid Build Coastguard Worker /* Decide if we want to continue this meta-block instead of emitting the
667*f4ee7fbaSAndroid Build Coastguard Worker last insert-only command. */
668*f4ee7fbaSAndroid Build Coastguard Worker if (input_size > 0 &&
669*f4ee7fbaSAndroid Build Coastguard Worker total_block_size + block_size <= (1 << 20) &&
670*f4ee7fbaSAndroid Build Coastguard Worker ShouldMergeBlock(input, block_size, lit_depth)) {
671*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(total_block_size > (1 << 16));
672*f4ee7fbaSAndroid Build Coastguard Worker /* Update the size of the current meta-block and continue emitting commands.
673*f4ee7fbaSAndroid Build Coastguard Worker We can do this because the current size and the new size both have 5
674*f4ee7fbaSAndroid Build Coastguard Worker nibbles. */
675*f4ee7fbaSAndroid Build Coastguard Worker total_block_size += block_size;
676*f4ee7fbaSAndroid Build Coastguard Worker UpdateBits(20, (uint32_t)(total_block_size - 1), mlen_storage_ix, storage);
677*f4ee7fbaSAndroid Build Coastguard Worker goto emit_commands;
678*f4ee7fbaSAndroid Build Coastguard Worker }
679*f4ee7fbaSAndroid Build Coastguard Worker
680*f4ee7fbaSAndroid Build Coastguard Worker /* Emit the remaining bytes as literals. */
681*f4ee7fbaSAndroid Build Coastguard Worker if (next_emit < ip_end) {
682*f4ee7fbaSAndroid Build Coastguard Worker const size_t insert = (size_t)(ip_end - next_emit);
683*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_PREDICT_TRUE(insert < 6210)) {
684*f4ee7fbaSAndroid Build Coastguard Worker EmitInsertLen(insert, cmd_depth, cmd_bits, cmd_histo,
685*f4ee7fbaSAndroid Build Coastguard Worker storage_ix, storage);
686*f4ee7fbaSAndroid Build Coastguard Worker EmitLiterals(next_emit, insert, lit_depth, lit_bits, storage_ix, storage);
687*f4ee7fbaSAndroid Build Coastguard Worker } else if (ShouldUseUncompressedMode(metablock_start, next_emit, insert,
688*f4ee7fbaSAndroid Build Coastguard Worker literal_ratio)) {
689*f4ee7fbaSAndroid Build Coastguard Worker EmitUncompressedMetaBlock(metablock_start, ip_end, mlen_storage_ix - 3,
690*f4ee7fbaSAndroid Build Coastguard Worker storage_ix, storage);
691*f4ee7fbaSAndroid Build Coastguard Worker } else {
692*f4ee7fbaSAndroid Build Coastguard Worker EmitLongInsertLen(insert, cmd_depth, cmd_bits, cmd_histo,
693*f4ee7fbaSAndroid Build Coastguard Worker storage_ix, storage);
694*f4ee7fbaSAndroid Build Coastguard Worker EmitLiterals(next_emit, insert, lit_depth, lit_bits,
695*f4ee7fbaSAndroid Build Coastguard Worker storage_ix, storage);
696*f4ee7fbaSAndroid Build Coastguard Worker }
697*f4ee7fbaSAndroid Build Coastguard Worker }
698*f4ee7fbaSAndroid Build Coastguard Worker next_emit = ip_end;
699*f4ee7fbaSAndroid Build Coastguard Worker
700*f4ee7fbaSAndroid Build Coastguard Worker next_block:
701*f4ee7fbaSAndroid Build Coastguard Worker /* If we have more data, write a new meta-block header and prefix codes and
702*f4ee7fbaSAndroid Build Coastguard Worker then continue emitting commands. */
703*f4ee7fbaSAndroid Build Coastguard Worker if (input_size > 0) {
704*f4ee7fbaSAndroid Build Coastguard Worker metablock_start = input;
705*f4ee7fbaSAndroid Build Coastguard Worker block_size = BROTLI_MIN(size_t, input_size, kFirstBlockSize);
706*f4ee7fbaSAndroid Build Coastguard Worker total_block_size = block_size;
707*f4ee7fbaSAndroid Build Coastguard Worker /* Save the bit position of the MLEN field of the meta-block header, so that
708*f4ee7fbaSAndroid Build Coastguard Worker we can update it later if we decide to extend this meta-block. */
709*f4ee7fbaSAndroid Build Coastguard Worker mlen_storage_ix = *storage_ix + 3;
710*f4ee7fbaSAndroid Build Coastguard Worker BrotliStoreMetaBlockHeader(block_size, 0, storage_ix, storage);
711*f4ee7fbaSAndroid Build Coastguard Worker /* No block splits, no contexts. */
712*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(13, 0, storage_ix, storage);
713*f4ee7fbaSAndroid Build Coastguard Worker literal_ratio = BuildAndStoreLiteralPrefixCode(
714*f4ee7fbaSAndroid Build Coastguard Worker m, input, block_size, lit_depth, lit_bits, storage_ix, storage);
715*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m)) return;
716*f4ee7fbaSAndroid Build Coastguard Worker BuildAndStoreCommandPrefixCode(cmd_histo, cmd_depth, cmd_bits,
717*f4ee7fbaSAndroid Build Coastguard Worker storage_ix, storage);
718*f4ee7fbaSAndroid Build Coastguard Worker goto emit_commands;
719*f4ee7fbaSAndroid Build Coastguard Worker }
720*f4ee7fbaSAndroid Build Coastguard Worker
721*f4ee7fbaSAndroid Build Coastguard Worker if (!is_last) {
722*f4ee7fbaSAndroid Build Coastguard Worker /* If this is not the last block, update the command and distance prefix
723*f4ee7fbaSAndroid Build Coastguard Worker codes for the next block and store the compressed forms. */
724*f4ee7fbaSAndroid Build Coastguard Worker cmd_code[0] = 0;
725*f4ee7fbaSAndroid Build Coastguard Worker *cmd_code_numbits = 0;
726*f4ee7fbaSAndroid Build Coastguard Worker BuildAndStoreCommandPrefixCode(cmd_histo, cmd_depth, cmd_bits,
727*f4ee7fbaSAndroid Build Coastguard Worker cmd_code_numbits, cmd_code);
728*f4ee7fbaSAndroid Build Coastguard Worker }
729*f4ee7fbaSAndroid Build Coastguard Worker }
730*f4ee7fbaSAndroid Build Coastguard Worker
731*f4ee7fbaSAndroid Build Coastguard Worker #define FOR_TABLE_BITS_(X) X(9) X(11) X(13) X(15)
732*f4ee7fbaSAndroid Build Coastguard Worker
733*f4ee7fbaSAndroid Build Coastguard Worker #define BAKE_METHOD_PARAM_(B) \
734*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_NOINLINE void BrotliCompressFragmentFastImpl ## B( \
735*f4ee7fbaSAndroid Build Coastguard Worker MemoryManager* m, const uint8_t* input, size_t input_size, \
736*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_BOOL is_last, int* table, uint8_t cmd_depth[128], \
737*f4ee7fbaSAndroid Build Coastguard Worker uint16_t cmd_bits[128], size_t* cmd_code_numbits, uint8_t* cmd_code, \
738*f4ee7fbaSAndroid Build Coastguard Worker size_t* storage_ix, uint8_t* storage) { \
739*f4ee7fbaSAndroid Build Coastguard Worker BrotliCompressFragmentFastImpl(m, input, input_size, is_last, table, B, \
740*f4ee7fbaSAndroid Build Coastguard Worker cmd_depth, cmd_bits, cmd_code_numbits, cmd_code, storage_ix, storage); \
741*f4ee7fbaSAndroid Build Coastguard Worker }
FOR_TABLE_BITS_(BAKE_METHOD_PARAM_)742*f4ee7fbaSAndroid Build Coastguard Worker FOR_TABLE_BITS_(BAKE_METHOD_PARAM_)
743*f4ee7fbaSAndroid Build Coastguard Worker #undef BAKE_METHOD_PARAM_
744*f4ee7fbaSAndroid Build Coastguard Worker
745*f4ee7fbaSAndroid Build Coastguard Worker void BrotliCompressFragmentFast(
746*f4ee7fbaSAndroid Build Coastguard Worker MemoryManager* m, const uint8_t* input, size_t input_size,
747*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_BOOL is_last, int* table, size_t table_size, uint8_t cmd_depth[128],
748*f4ee7fbaSAndroid Build Coastguard Worker uint16_t cmd_bits[128], size_t* cmd_code_numbits, uint8_t* cmd_code,
749*f4ee7fbaSAndroid Build Coastguard Worker size_t* storage_ix, uint8_t* storage) {
750*f4ee7fbaSAndroid Build Coastguard Worker const size_t initial_storage_ix = *storage_ix;
751*f4ee7fbaSAndroid Build Coastguard Worker const size_t table_bits = Log2FloorNonZero(table_size);
752*f4ee7fbaSAndroid Build Coastguard Worker
753*f4ee7fbaSAndroid Build Coastguard Worker if (input_size == 0) {
754*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(is_last);
755*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(1, 1, storage_ix, storage); /* islast */
756*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(1, 1, storage_ix, storage); /* isempty */
757*f4ee7fbaSAndroid Build Coastguard Worker *storage_ix = (*storage_ix + 7u) & ~7u;
758*f4ee7fbaSAndroid Build Coastguard Worker return;
759*f4ee7fbaSAndroid Build Coastguard Worker }
760*f4ee7fbaSAndroid Build Coastguard Worker
761*f4ee7fbaSAndroid Build Coastguard Worker switch (table_bits) {
762*f4ee7fbaSAndroid Build Coastguard Worker #define CASE_(B) \
763*f4ee7fbaSAndroid Build Coastguard Worker case B: \
764*f4ee7fbaSAndroid Build Coastguard Worker BrotliCompressFragmentFastImpl ## B( \
765*f4ee7fbaSAndroid Build Coastguard Worker m, input, input_size, is_last, table, cmd_depth, cmd_bits, \
766*f4ee7fbaSAndroid Build Coastguard Worker cmd_code_numbits, cmd_code, storage_ix, storage); \
767*f4ee7fbaSAndroid Build Coastguard Worker break;
768*f4ee7fbaSAndroid Build Coastguard Worker FOR_TABLE_BITS_(CASE_)
769*f4ee7fbaSAndroid Build Coastguard Worker #undef CASE_
770*f4ee7fbaSAndroid Build Coastguard Worker default: BROTLI_DCHECK(0); break;
771*f4ee7fbaSAndroid Build Coastguard Worker }
772*f4ee7fbaSAndroid Build Coastguard Worker
773*f4ee7fbaSAndroid Build Coastguard Worker /* If output is larger than single uncompressed block, rewrite it. */
774*f4ee7fbaSAndroid Build Coastguard Worker if (*storage_ix - initial_storage_ix > 31 + (input_size << 3)) {
775*f4ee7fbaSAndroid Build Coastguard Worker EmitUncompressedMetaBlock(input, input + input_size, initial_storage_ix,
776*f4ee7fbaSAndroid Build Coastguard Worker storage_ix, storage);
777*f4ee7fbaSAndroid Build Coastguard Worker }
778*f4ee7fbaSAndroid Build Coastguard Worker
779*f4ee7fbaSAndroid Build Coastguard Worker if (is_last) {
780*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(1, 1, storage_ix, storage); /* islast */
781*f4ee7fbaSAndroid Build Coastguard Worker BrotliWriteBits(1, 1, storage_ix, storage); /* isempty */
782*f4ee7fbaSAndroid Build Coastguard Worker *storage_ix = (*storage_ix + 7u) & ~7u;
783*f4ee7fbaSAndroid Build Coastguard Worker }
784*f4ee7fbaSAndroid Build Coastguard Worker }
785*f4ee7fbaSAndroid Build Coastguard Worker
786*f4ee7fbaSAndroid Build Coastguard Worker #undef FOR_TABLE_BITS_
787*f4ee7fbaSAndroid Build Coastguard Worker
788*f4ee7fbaSAndroid Build Coastguard Worker #if defined(__cplusplus) || defined(c_plusplus)
789*f4ee7fbaSAndroid Build Coastguard Worker } /* extern "C" */
790*f4ee7fbaSAndroid Build Coastguard Worker #endif
791