2016-01-11 10:21:42 +00:00
|
|
|
/* Copyright 2015 Google Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
Distributed under MIT license.
|
|
|
|
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
|
|
|
*/
|
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Function for fast encoding of an input fragment, independently from the input
|
|
|
|
history. This function uses two-pass processing: in the first pass we save
|
|
|
|
the found backward matches and literal bytes into a buffer, and in the
|
|
|
|
second pass we emit them into the bit stream using prefix codes built based
|
|
|
|
on the actual command and literal byte histograms. */
|
2016-01-11 10:21:42 +00:00
|
|
|
|
2021-09-08 07:18:45 +00:00
|
|
|
#include "compress_fragment_two_pass.h"
|
2016-01-11 10:21:42 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#include <string.h> /* memcmp, memcpy, memset */
|
2016-01-11 11:17:44 +00:00
|
|
|
|
2022-11-17 13:03:09 +00:00
|
|
|
#include <brotli/types.h>
|
|
|
|
|
2016-10-17 15:33:12 +00:00
|
|
|
#include "../common/constants.h"
|
2017-12-12 13:33:12 +00:00
|
|
|
#include "../common/platform.h"
|
2021-09-08 07:18:45 +00:00
|
|
|
#include "bit_cost.h"
|
|
|
|
#include "brotli_bit_stream.h"
|
|
|
|
#include "entropy_encode.h"
|
|
|
|
#include "fast_log.h"
|
|
|
|
#include "find_match_length.h"
|
|
|
|
#include "write_bits.h"
|
2016-01-11 10:21:42 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2016-01-11 10:21:42 +00:00
|
|
|
|
2017-02-06 13:20:43 +00:00
|
|
|
#define MAX_DISTANCE (long)BROTLI_MAX_BACKWARD_LIMIT(18)
|
2016-09-21 15:20:36 +00:00
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
/* kHashMul32 multiplier has these properties:
|
|
|
|
* The multiplier must be odd. Otherwise we may lose the highest bit.
|
2016-10-31 13:33:59 +00:00
|
|
|
* No long streaks of ones or zeros.
|
2016-06-03 09:19:23 +00:00
|
|
|
* There is no effort to ensure that it is a prime, the oddity is enough
|
|
|
|
for this use.
|
|
|
|
* The number has been tuned heuristically against compression benchmarks. */
|
2018-02-26 14:04:36 +00:00
|
|
|
static const uint32_t kHashMul32 = 0x1E35A7BD;
|
2016-01-11 10:21:42 +00:00
|
|
|
|
2018-04-13 09:44:34 +00:00
|
|
|
static BROTLI_INLINE uint32_t Hash(const uint8_t* p,
|
|
|
|
size_t shift, size_t length) {
|
|
|
|
const uint64_t h =
|
|
|
|
(BROTLI_UNALIGNED_LOAD64LE(p) << ((8 - length) * 8)) * kHashMul32;
|
2016-06-13 09:01:04 +00:00
|
|
|
return (uint32_t)(h >> shift);
|
2016-01-11 10:21:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 09:44:34 +00:00
|
|
|
static BROTLI_INLINE uint32_t HashBytesAtOffset(uint64_t v, size_t offset,
|
|
|
|
size_t shift, size_t length) {
|
|
|
|
BROTLI_DCHECK(offset <= 8 - length);
|
2016-06-13 09:01:04 +00:00
|
|
|
{
|
2018-04-13 09:44:34 +00:00
|
|
|
const uint64_t h = ((v >> (8 * offset)) << ((8 - length) * 8)) * kHashMul32;
|
2016-06-13 09:01:04 +00:00
|
|
|
return (uint32_t)(h >> shift);
|
|
|
|
}
|
2016-01-11 10:21:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 09:44:34 +00:00
|
|
|
static BROTLI_INLINE BROTLI_BOOL IsMatch(const uint8_t* p1, const uint8_t* p2,
|
|
|
|
size_t length) {
|
|
|
|
if (BrotliUnalignedRead32(p1) == BrotliUnalignedRead32(p2)) {
|
|
|
|
if (length == 4) return BROTLI_TRUE;
|
|
|
|
return TO_BROTLI_BOOL(p1[4] == p2[4] && p1[5] == p2[5]);
|
|
|
|
}
|
|
|
|
return BROTLI_FALSE;
|
2016-01-11 10:21:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Builds a command and distance prefix code (each 64 symbols) into "depth" and
|
|
|
|
"bits" based on "histogram" and stores it into the bit stream. */
|
2021-07-29 20:29:43 +00:00
|
|
|
static void BuildAndStoreCommandPrefixCode(BrotliTwoPassArena* s,
|
|
|
|
size_t* storage_ix,
|
|
|
|
uint8_t* storage) {
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Tree size for building a tree over 64 symbols is 2 * 64 + 1. */
|
2021-11-10 09:34:39 +00:00
|
|
|
/* TODO(eustas): initialize once. */
|
2021-07-29 20:29:43 +00:00
|
|
|
memset(s->tmp_depth, 0, sizeof(s->tmp_depth));
|
|
|
|
BrotliCreateHuffmanTree(s->cmd_histo, 64, 15, s->tmp_tree, s->cmd_depth);
|
|
|
|
BrotliCreateHuffmanTree(&s->cmd_histo[64], 64, 14, s->tmp_tree,
|
|
|
|
&s->cmd_depth[64]);
|
2016-10-31 13:33:59 +00:00
|
|
|
/* We have to jump through a few hoops here in order to compute
|
2016-06-03 09:19:23 +00:00
|
|
|
the command bits because the symbols are in a different order than in
|
|
|
|
the full alphabet. This looks complicated, but having the symbols
|
|
|
|
in this order in the command bits saves a few branches in the Emit*
|
|
|
|
functions. */
|
2021-07-29 20:29:43 +00:00
|
|
|
memcpy(s->tmp_depth, s->cmd_depth + 24, 24);
|
|
|
|
memcpy(s->tmp_depth + 24, s->cmd_depth, 8);
|
|
|
|
memcpy(s->tmp_depth + 32, s->cmd_depth + 48, 8);
|
|
|
|
memcpy(s->tmp_depth + 40, s->cmd_depth + 8, 8);
|
|
|
|
memcpy(s->tmp_depth + 48, s->cmd_depth + 56, 8);
|
|
|
|
memcpy(s->tmp_depth + 56, s->cmd_depth + 16, 8);
|
|
|
|
BrotliConvertBitDepthsToSymbols(s->tmp_depth, 64, s->tmp_bits);
|
|
|
|
memcpy(s->cmd_bits, s->tmp_bits + 24, 16);
|
|
|
|
memcpy(s->cmd_bits + 8, s->tmp_bits + 40, 16);
|
|
|
|
memcpy(s->cmd_bits + 16, s->tmp_bits + 56, 16);
|
|
|
|
memcpy(s->cmd_bits + 24, s->tmp_bits, 48);
|
|
|
|
memcpy(s->cmd_bits + 48, s->tmp_bits + 32, 16);
|
|
|
|
memcpy(s->cmd_bits + 56, s->tmp_bits + 48, 16);
|
|
|
|
BrotliConvertBitDepthsToSymbols(&s->cmd_depth[64], 64, &s->cmd_bits[64]);
|
2016-01-11 10:21:42 +00:00
|
|
|
{
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Create the bit length array for the full command alphabet. */
|
2016-06-13 09:01:04 +00:00
|
|
|
size_t i;
|
2021-07-29 20:29:43 +00:00
|
|
|
memset(s->tmp_depth, 0, 64); /* only 64 first values were used */
|
|
|
|
memcpy(s->tmp_depth, s->cmd_depth + 24, 8);
|
|
|
|
memcpy(s->tmp_depth + 64, s->cmd_depth + 32, 8);
|
|
|
|
memcpy(s->tmp_depth + 128, s->cmd_depth + 40, 8);
|
|
|
|
memcpy(s->tmp_depth + 192, s->cmd_depth + 48, 8);
|
|
|
|
memcpy(s->tmp_depth + 384, s->cmd_depth + 56, 8);
|
2016-06-13 09:01:04 +00:00
|
|
|
for (i = 0; i < 8; ++i) {
|
2021-07-29 20:29:43 +00:00
|
|
|
s->tmp_depth[128 + 8 * i] = s->cmd_depth[i];
|
|
|
|
s->tmp_depth[256 + 8 * i] = s->cmd_depth[8 + i];
|
|
|
|
s->tmp_depth[448 + 8 * i] = s->cmd_depth[16 + i];
|
2016-01-11 10:21:42 +00:00
|
|
|
}
|
2021-07-29 20:29:43 +00:00
|
|
|
BrotliStoreHuffmanTree(s->tmp_depth, BROTLI_NUM_COMMAND_SYMBOLS,
|
|
|
|
s->tmp_tree, storage_ix, storage);
|
2016-01-11 10:21:42 +00:00
|
|
|
}
|
2021-07-29 20:29:43 +00:00
|
|
|
BrotliStoreHuffmanTree(&s->cmd_depth[64], 64, s->tmp_tree, storage_ix,
|
|
|
|
storage);
|
2016-01-11 10:21:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
static BROTLI_INLINE void EmitInsertLen(
|
|
|
|
uint32_t insertlen, uint32_t** commands) {
|
2016-01-11 10:21:42 +00:00
|
|
|
if (insertlen < 6) {
|
|
|
|
**commands = insertlen;
|
|
|
|
} else if (insertlen < 130) {
|
2016-06-13 09:01:04 +00:00
|
|
|
const uint32_t tail = insertlen - 2;
|
|
|
|
const uint32_t nbits = Log2FloorNonZero(tail) - 1u;
|
|
|
|
const uint32_t prefix = tail >> nbits;
|
2016-01-11 10:21:42 +00:00
|
|
|
const uint32_t inscode = (nbits << 1) + prefix + 2;
|
2016-06-13 09:01:04 +00:00
|
|
|
const uint32_t extra = tail - (prefix << nbits);
|
2016-01-11 10:21:42 +00:00
|
|
|
**commands = inscode | (extra << 8);
|
|
|
|
} else if (insertlen < 2114) {
|
2016-06-13 09:01:04 +00:00
|
|
|
const uint32_t tail = insertlen - 66;
|
|
|
|
const uint32_t nbits = Log2FloorNonZero(tail);
|
2016-01-11 10:21:42 +00:00
|
|
|
const uint32_t code = nbits + 10;
|
2016-06-13 09:01:04 +00:00
|
|
|
const uint32_t extra = tail - (1u << nbits);
|
2016-01-11 10:21:42 +00:00
|
|
|
**commands = code | (extra << 8);
|
|
|
|
} else if (insertlen < 6210) {
|
|
|
|
const uint32_t extra = insertlen - 2114;
|
|
|
|
**commands = 21 | (extra << 8);
|
|
|
|
} else if (insertlen < 22594) {
|
|
|
|
const uint32_t extra = insertlen - 6210;
|
|
|
|
**commands = 22 | (extra << 8);
|
|
|
|
} else {
|
|
|
|
const uint32_t extra = insertlen - 22594;
|
|
|
|
**commands = 23 | (extra << 8);
|
|
|
|
}
|
|
|
|
++(*commands);
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
static BROTLI_INLINE void EmitCopyLen(size_t copylen, uint32_t** commands) {
|
2016-01-11 10:21:42 +00:00
|
|
|
if (copylen < 10) {
|
2016-06-13 09:01:04 +00:00
|
|
|
**commands = (uint32_t)(copylen + 38);
|
2016-01-11 10:21:42 +00:00
|
|
|
} else if (copylen < 134) {
|
2016-06-13 09:01:04 +00:00
|
|
|
const size_t tail = copylen - 6;
|
|
|
|
const size_t nbits = Log2FloorNonZero(tail) - 1;
|
|
|
|
const size_t prefix = tail >> nbits;
|
2016-01-11 10:21:42 +00:00
|
|
|
const size_t code = (nbits << 1) + prefix + 44;
|
2016-06-13 09:01:04 +00:00
|
|
|
const size_t extra = tail - (prefix << nbits);
|
|
|
|
**commands = (uint32_t)(code | (extra << 8));
|
2016-01-11 10:21:42 +00:00
|
|
|
} else if (copylen < 2118) {
|
2016-06-13 09:01:04 +00:00
|
|
|
const size_t tail = copylen - 70;
|
|
|
|
const size_t nbits = Log2FloorNonZero(tail);
|
2016-01-11 10:21:42 +00:00
|
|
|
const size_t code = nbits + 52;
|
2016-06-14 11:42:47 +00:00
|
|
|
const size_t extra = tail - ((size_t)1 << nbits);
|
2016-06-13 09:01:04 +00:00
|
|
|
**commands = (uint32_t)(code | (extra << 8));
|
2016-01-11 10:21:42 +00:00
|
|
|
} else {
|
|
|
|
const size_t extra = copylen - 2118;
|
2016-06-13 09:01:04 +00:00
|
|
|
**commands = (uint32_t)(63 | (extra << 8));
|
2016-01-11 10:21:42 +00:00
|
|
|
}
|
|
|
|
++(*commands);
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
static BROTLI_INLINE void EmitCopyLenLastDistance(
|
|
|
|
size_t copylen, uint32_t** commands) {
|
2016-01-11 10:21:42 +00:00
|
|
|
if (copylen < 12) {
|
2016-06-13 09:01:04 +00:00
|
|
|
**commands = (uint32_t)(copylen + 20);
|
2016-01-11 10:21:42 +00:00
|
|
|
++(*commands);
|
|
|
|
} else if (copylen < 72) {
|
2016-06-13 09:01:04 +00:00
|
|
|
const size_t tail = copylen - 8;
|
|
|
|
const size_t nbits = Log2FloorNonZero(tail) - 1;
|
|
|
|
const size_t prefix = tail >> nbits;
|
2016-01-11 10:21:42 +00:00
|
|
|
const size_t code = (nbits << 1) + prefix + 28;
|
2016-06-13 09:01:04 +00:00
|
|
|
const size_t extra = tail - (prefix << nbits);
|
|
|
|
**commands = (uint32_t)(code | (extra << 8));
|
2016-01-11 10:21:42 +00:00
|
|
|
++(*commands);
|
|
|
|
} else if (copylen < 136) {
|
2016-06-13 09:01:04 +00:00
|
|
|
const size_t tail = copylen - 8;
|
|
|
|
const size_t code = (tail >> 5) + 54;
|
|
|
|
const size_t extra = tail & 31;
|
|
|
|
**commands = (uint32_t)(code | (extra << 8));
|
2016-01-11 10:21:42 +00:00
|
|
|
++(*commands);
|
|
|
|
**commands = 64;
|
|
|
|
++(*commands);
|
|
|
|
} else if (copylen < 2120) {
|
2016-06-13 09:01:04 +00:00
|
|
|
const size_t tail = copylen - 72;
|
|
|
|
const size_t nbits = Log2FloorNonZero(tail);
|
2016-01-11 10:21:42 +00:00
|
|
|
const size_t code = nbits + 52;
|
2016-06-14 11:42:47 +00:00
|
|
|
const size_t extra = tail - ((size_t)1 << nbits);
|
2016-06-13 09:01:04 +00:00
|
|
|
**commands = (uint32_t)(code | (extra << 8));
|
2016-01-11 10:21:42 +00:00
|
|
|
++(*commands);
|
|
|
|
**commands = 64;
|
|
|
|
++(*commands);
|
|
|
|
} else {
|
|
|
|
const size_t extra = copylen - 2120;
|
2016-06-13 09:01:04 +00:00
|
|
|
**commands = (uint32_t)(63 | (extra << 8));
|
2016-01-11 10:21:42 +00:00
|
|
|
++(*commands);
|
|
|
|
**commands = 64;
|
|
|
|
++(*commands);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
static BROTLI_INLINE void EmitDistance(uint32_t distance, uint32_t** commands) {
|
|
|
|
uint32_t d = distance + 3;
|
|
|
|
uint32_t nbits = Log2FloorNonZero(d) - 1;
|
|
|
|
const uint32_t prefix = (d >> nbits) & 1;
|
2016-01-11 10:21:42 +00:00
|
|
|
const uint32_t offset = (2 + prefix) << nbits;
|
|
|
|
const uint32_t distcode = 2 * (nbits - 1) + prefix + 80;
|
2016-06-13 09:01:04 +00:00
|
|
|
uint32_t extra = d - offset;
|
2016-01-11 10:21:42 +00:00
|
|
|
**commands = distcode | (extra << 8);
|
|
|
|
++(*commands);
|
|
|
|
}
|
|
|
|
|
2017-03-06 13:22:45 +00:00
|
|
|
/* REQUIRES: len <= 1 << 24. */
|
2016-06-13 09:01:04 +00:00
|
|
|
static void BrotliStoreMetaBlockHeader(
|
2016-07-26 12:41:59 +00:00
|
|
|
size_t len, BROTLI_BOOL is_uncompressed, size_t* storage_ix,
|
|
|
|
uint8_t* storage) {
|
2017-03-06 13:22:45 +00:00
|
|
|
size_t nibbles = 6;
|
2016-06-03 09:19:23 +00:00
|
|
|
/* ISLAST */
|
2016-06-13 09:01:04 +00:00
|
|
|
BrotliWriteBits(1, 0, storage_ix, storage);
|
2016-01-11 10:21:42 +00:00
|
|
|
if (len <= (1U << 16)) {
|
2017-03-06 13:22:45 +00:00
|
|
|
nibbles = 4;
|
|
|
|
} else if (len <= (1U << 20)) {
|
|
|
|
nibbles = 5;
|
2016-01-11 10:21:42 +00:00
|
|
|
}
|
2017-03-06 13:22:45 +00:00
|
|
|
BrotliWriteBits(2, nibbles - 4, storage_ix, storage);
|
|
|
|
BrotliWriteBits(nibbles * 4, len - 1, storage_ix, storage);
|
2016-06-03 09:19:23 +00:00
|
|
|
/* ISUNCOMPRESSED */
|
2016-06-13 09:01:04 +00:00
|
|
|
BrotliWriteBits(1, (uint64_t)is_uncompressed, storage_ix, storage);
|
2016-01-11 10:21:42 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 15:20:36 +00:00
|
|
|
static BROTLI_INLINE void CreateCommands(const uint8_t* input,
|
|
|
|
size_t block_size, size_t input_size, const uint8_t* base_ip, int* table,
|
2018-04-13 09:44:34 +00:00
|
|
|
size_t table_bits, size_t min_match,
|
|
|
|
uint8_t** literals, uint32_t** commands) {
|
2016-06-03 09:19:23 +00:00
|
|
|
/* "ip" is the input pointer. */
|
2016-01-11 10:21:42 +00:00
|
|
|
const uint8_t* ip = input;
|
2016-09-21 15:20:36 +00:00
|
|
|
const size_t shift = 64u - table_bits;
|
2016-01-11 10:21:42 +00:00
|
|
|
const uint8_t* ip_end = input + block_size;
|
2016-06-03 09:19:23 +00:00
|
|
|
/* "next_emit" is a pointer to the first byte that is not covered by a
|
|
|
|
previous copy. Bytes between "next_emit" and the start of the next copy or
|
|
|
|
the end of the input will be emitted as literal bytes. */
|
2016-01-11 10:21:42 +00:00
|
|
|
const uint8_t* next_emit = input;
|
|
|
|
|
|
|
|
int last_distance = -1;
|
2016-10-17 15:33:12 +00:00
|
|
|
const size_t kInputMarginBytes = BROTLI_WINDOW_GAP;
|
2016-06-13 09:01:04 +00:00
|
|
|
|
2016-10-18 14:56:39 +00:00
|
|
|
if (BROTLI_PREDICT_TRUE(block_size >= kInputMarginBytes)) {
|
2016-06-03 09:19:23 +00:00
|
|
|
/* For the last block, we need to keep a 16 bytes margin so that we can be
|
|
|
|
sure that all distances are at most window size - 16.
|
|
|
|
For all other blocks, we only need to keep a margin of 5 bytes so that
|
|
|
|
we don't go over the block size with a copy. */
|
2018-04-13 09:44:34 +00:00
|
|
|
const size_t len_limit = BROTLI_MIN(size_t, block_size - min_match,
|
2016-06-13 09:01:04 +00:00
|
|
|
input_size - kInputMarginBytes);
|
2016-01-11 10:21:42 +00:00
|
|
|
const uint8_t* ip_limit = input + len_limit;
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
uint32_t next_hash;
|
2018-04-13 09:44:34 +00:00
|
|
|
for (next_hash = Hash(++ip, shift, min_match); ; ) {
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Step 1: Scan forward in the input looking for a 6-byte-long match.
|
|
|
|
If we get close to exhausting the input then goto emit_remainder.
|
|
|
|
|
|
|
|
Heuristic match skipping: If 32 bytes are scanned with no matches
|
|
|
|
found, start looking only at every other byte. If 32 more bytes are
|
|
|
|
scanned, look at every third byte, etc.. When a match is found,
|
|
|
|
immediately go back to looking at every byte. This is a small loss
|
|
|
|
(~5% performance, ~0.1% density) for compressible data due to more
|
|
|
|
bookkeeping, but for non-compressible data (such as JPEG) it's a huge
|
|
|
|
win since the compressor quickly "realizes" the data is incompressible
|
|
|
|
and doesn't bother looking for matches everywhere.
|
|
|
|
|
|
|
|
The "skip" variable keeps track of how many bytes there are since the
|
|
|
|
last match; dividing it by 32 (ie. right-shifting by five) gives the
|
|
|
|
number of bytes to move ahead for each iteration. */
|
2016-01-11 10:21:42 +00:00
|
|
|
uint32_t skip = 32;
|
|
|
|
|
|
|
|
const uint8_t* next_ip = ip;
|
|
|
|
const uint8_t* candidate;
|
2016-06-13 09:01:04 +00:00
|
|
|
|
2017-12-12 13:33:12 +00:00
|
|
|
BROTLI_DCHECK(next_emit < ip);
|
2016-09-21 15:20:36 +00:00
|
|
|
trawl:
|
2016-01-11 10:21:42 +00:00
|
|
|
do {
|
|
|
|
uint32_t hash = next_hash;
|
|
|
|
uint32_t bytes_between_hash_lookups = skip++ >> 5;
|
2016-06-13 09:01:04 +00:00
|
|
|
ip = next_ip;
|
2018-04-13 09:44:34 +00:00
|
|
|
BROTLI_DCHECK(hash == Hash(ip, shift, min_match));
|
2016-01-11 10:21:42 +00:00
|
|
|
next_ip = ip + bytes_between_hash_lookups;
|
2016-10-18 14:56:39 +00:00
|
|
|
if (BROTLI_PREDICT_FALSE(next_ip > ip_limit)) {
|
2016-01-11 10:21:42 +00:00
|
|
|
goto emit_remainder;
|
|
|
|
}
|
2018-04-13 09:44:34 +00:00
|
|
|
next_hash = Hash(next_ip, shift, min_match);
|
2016-01-11 10:21:42 +00:00
|
|
|
candidate = ip - last_distance;
|
2018-04-13 09:44:34 +00:00
|
|
|
if (IsMatch(ip, candidate, min_match)) {
|
2016-10-18 14:56:39 +00:00
|
|
|
if (BROTLI_PREDICT_TRUE(candidate < ip)) {
|
2016-06-13 09:01:04 +00:00
|
|
|
table[hash] = (int)(ip - base_ip);
|
2016-01-11 10:21:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
candidate = base_ip + table[hash];
|
2017-12-12 13:33:12 +00:00
|
|
|
BROTLI_DCHECK(candidate >= base_ip);
|
|
|
|
BROTLI_DCHECK(candidate < ip);
|
2016-01-11 10:21:42 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
table[hash] = (int)(ip - base_ip);
|
2018-04-13 09:44:34 +00:00
|
|
|
} while (BROTLI_PREDICT_TRUE(!IsMatch(ip, candidate, min_match)));
|
2016-01-11 10:21:42 +00:00
|
|
|
|
2016-09-21 15:20:36 +00:00
|
|
|
/* Check copy distance. If candidate is not feasible, continue search.
|
|
|
|
Checking is done outside of hot loop to reduce overhead. */
|
|
|
|
if (ip - candidate > MAX_DISTANCE) goto trawl;
|
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Step 2: Emit the found match together with the literal bytes from
|
2016-10-31 13:33:59 +00:00
|
|
|
"next_emit", and then see if we can find a next match immediately
|
2016-06-03 09:19:23 +00:00
|
|
|
afterwards. Repeat until we find no match for the input
|
|
|
|
without emitting some literal bytes. */
|
2016-01-11 10:21:42 +00:00
|
|
|
|
|
|
|
{
|
2016-06-03 09:19:23 +00:00
|
|
|
/* We have a 6-byte match at ip, and we need to emit bytes in
|
|
|
|
[next_emit, ip). */
|
2016-01-11 10:21:42 +00:00
|
|
|
const uint8_t* base = ip;
|
2018-04-13 09:44:34 +00:00
|
|
|
size_t matched = min_match + FindMatchLengthWithLimit(
|
|
|
|
candidate + min_match, ip + min_match,
|
|
|
|
(size_t)(ip_end - ip) - min_match);
|
2016-06-13 09:01:04 +00:00
|
|
|
int distance = (int)(base - candidate); /* > 0 */
|
|
|
|
int insert = (int)(base - next_emit);
|
2016-01-11 10:21:42 +00:00
|
|
|
ip += matched;
|
2017-12-12 13:33:12 +00:00
|
|
|
BROTLI_DCHECK(0 == memcmp(base, candidate, matched));
|
2016-06-13 09:01:04 +00:00
|
|
|
EmitInsertLen((uint32_t)insert, commands);
|
2021-11-10 09:34:39 +00:00
|
|
|
BROTLI_LOG(("[CompressFragment] pos = %d insert = %d copy = %d\n",
|
|
|
|
(int)(next_emit - base_ip), insert, 2));
|
2016-06-13 09:01:04 +00:00
|
|
|
memcpy(*literals, next_emit, (size_t)insert);
|
2016-01-11 10:21:42 +00:00
|
|
|
*literals += insert;
|
|
|
|
if (distance == last_distance) {
|
|
|
|
**commands = 64;
|
|
|
|
++(*commands);
|
|
|
|
} else {
|
2016-06-13 09:01:04 +00:00
|
|
|
EmitDistance((uint32_t)distance, commands);
|
2016-01-11 10:21:42 +00:00
|
|
|
last_distance = distance;
|
|
|
|
}
|
|
|
|
EmitCopyLenLastDistance(matched, commands);
|
2021-11-10 09:34:39 +00:00
|
|
|
BROTLI_LOG(("[CompressFragment] pos = %d distance = %d\n"
|
|
|
|
"[CompressFragment] pos = %d insert = %d copy = %d\n"
|
|
|
|
"[CompressFragment] pos = %d distance = %d\n",
|
|
|
|
(int)(base - base_ip), (int)distance,
|
|
|
|
(int)(base - base_ip) + 2, 0, (int)matched - 2,
|
|
|
|
(int)(base - base_ip) + 2, (int)distance));
|
2016-01-11 10:21:42 +00:00
|
|
|
|
|
|
|
next_emit = ip;
|
2016-10-18 14:56:39 +00:00
|
|
|
if (BROTLI_PREDICT_FALSE(ip >= ip_limit)) {
|
2016-01-11 10:21:42 +00:00
|
|
|
goto emit_remainder;
|
|
|
|
}
|
2016-06-13 09:01:04 +00:00
|
|
|
{
|
2016-06-03 09:19:23 +00:00
|
|
|
/* We could immediately start working at ip now, but to improve
|
|
|
|
compression we first update "table" with the hashes of some
|
|
|
|
positions within the last copy. */
|
2018-04-13 09:44:34 +00:00
|
|
|
uint64_t input_bytes;
|
2016-06-13 09:01:04 +00:00
|
|
|
uint32_t cur_hash;
|
2018-04-13 09:44:34 +00:00
|
|
|
uint32_t prev_hash;
|
|
|
|
if (min_match == 4) {
|
|
|
|
input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 3);
|
|
|
|
cur_hash = HashBytesAtOffset(input_bytes, 3, shift, min_match);
|
|
|
|
prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match);
|
|
|
|
table[prev_hash] = (int)(ip - base_ip - 3);
|
|
|
|
prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match);
|
|
|
|
table[prev_hash] = (int)(ip - base_ip - 2);
|
|
|
|
prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match);
|
|
|
|
table[prev_hash] = (int)(ip - base_ip - 1);
|
|
|
|
} else {
|
|
|
|
input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 5);
|
|
|
|
prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match);
|
|
|
|
table[prev_hash] = (int)(ip - base_ip - 5);
|
|
|
|
prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match);
|
|
|
|
table[prev_hash] = (int)(ip - base_ip - 4);
|
|
|
|
prev_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match);
|
|
|
|
table[prev_hash] = (int)(ip - base_ip - 3);
|
|
|
|
input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 2);
|
|
|
|
cur_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match);
|
|
|
|
prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match);
|
|
|
|
table[prev_hash] = (int)(ip - base_ip - 2);
|
|
|
|
prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match);
|
|
|
|
table[prev_hash] = (int)(ip - base_ip - 1);
|
|
|
|
}
|
2016-06-13 09:01:04 +00:00
|
|
|
|
|
|
|
candidate = base_ip + table[cur_hash];
|
|
|
|
table[cur_hash] = (int)(ip - base_ip);
|
|
|
|
}
|
2016-01-11 10:21:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 09:44:34 +00:00
|
|
|
while (ip - candidate <= MAX_DISTANCE &&
|
|
|
|
IsMatch(ip, candidate, min_match)) {
|
2016-06-03 09:19:23 +00:00
|
|
|
/* We have a 6-byte match at ip, and no need to emit any
|
|
|
|
literal bytes prior to ip. */
|
2016-01-11 10:21:42 +00:00
|
|
|
const uint8_t* base = ip;
|
2018-04-13 09:44:34 +00:00
|
|
|
size_t matched = min_match + FindMatchLengthWithLimit(
|
|
|
|
candidate + min_match, ip + min_match,
|
|
|
|
(size_t)(ip_end - ip) - min_match);
|
2016-01-11 10:21:42 +00:00
|
|
|
ip += matched;
|
2016-06-13 09:01:04 +00:00
|
|
|
last_distance = (int)(base - candidate); /* > 0 */
|
2017-12-12 13:33:12 +00:00
|
|
|
BROTLI_DCHECK(0 == memcmp(base, candidate, matched));
|
2016-01-11 10:21:42 +00:00
|
|
|
EmitCopyLen(matched, commands);
|
2016-06-13 09:01:04 +00:00
|
|
|
EmitDistance((uint32_t)last_distance, commands);
|
2021-11-10 09:34:39 +00:00
|
|
|
BROTLI_LOG(("[CompressFragment] pos = %d insert = %d copy = %d\n"
|
|
|
|
"[CompressFragment] pos = %d distance = %d\n",
|
|
|
|
(int)(base - base_ip), 0, (int)matched,
|
|
|
|
(int)(base - base_ip), (int)last_distance));
|
2016-01-11 10:21:42 +00:00
|
|
|
|
|
|
|
next_emit = ip;
|
2016-10-18 14:56:39 +00:00
|
|
|
if (BROTLI_PREDICT_FALSE(ip >= ip_limit)) {
|
2016-01-11 10:21:42 +00:00
|
|
|
goto emit_remainder;
|
|
|
|
}
|
2016-06-13 09:01:04 +00:00
|
|
|
{
|
2016-06-03 09:19:23 +00:00
|
|
|
/* We could immediately start working at ip now, but to improve
|
|
|
|
compression we first update "table" with the hashes of some
|
|
|
|
positions within the last copy. */
|
2018-04-13 09:44:34 +00:00
|
|
|
uint64_t input_bytes;
|
2016-06-13 09:01:04 +00:00
|
|
|
uint32_t cur_hash;
|
2018-04-13 09:44:34 +00:00
|
|
|
uint32_t prev_hash;
|
|
|
|
if (min_match == 4) {
|
|
|
|
input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 3);
|
|
|
|
cur_hash = HashBytesAtOffset(input_bytes, 3, shift, min_match);
|
|
|
|
prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match);
|
|
|
|
table[prev_hash] = (int)(ip - base_ip - 3);
|
|
|
|
prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match);
|
|
|
|
table[prev_hash] = (int)(ip - base_ip - 2);
|
|
|
|
prev_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match);
|
|
|
|
table[prev_hash] = (int)(ip - base_ip - 1);
|
|
|
|
} else {
|
|
|
|
input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 5);
|
|
|
|
prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match);
|
|
|
|
table[prev_hash] = (int)(ip - base_ip - 5);
|
|
|
|
prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match);
|
|
|
|
table[prev_hash] = (int)(ip - base_ip - 4);
|
|
|
|
prev_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match);
|
|
|
|
table[prev_hash] = (int)(ip - base_ip - 3);
|
|
|
|
input_bytes = BROTLI_UNALIGNED_LOAD64LE(ip - 2);
|
|
|
|
cur_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match);
|
|
|
|
prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match);
|
|
|
|
table[prev_hash] = (int)(ip - base_ip - 2);
|
|
|
|
prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match);
|
|
|
|
table[prev_hash] = (int)(ip - base_ip - 1);
|
|
|
|
}
|
2016-06-13 09:01:04 +00:00
|
|
|
|
|
|
|
candidate = base_ip + table[cur_hash];
|
|
|
|
table[cur_hash] = (int)(ip - base_ip);
|
|
|
|
}
|
2016-01-11 10:21:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 09:44:34 +00:00
|
|
|
next_hash = Hash(++ip, shift, min_match);
|
2016-01-11 10:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
emit_remainder:
|
2017-12-12 13:33:12 +00:00
|
|
|
BROTLI_DCHECK(next_emit <= ip_end);
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Emit the remaining bytes as literals. */
|
2016-01-11 10:21:42 +00:00
|
|
|
if (next_emit < ip_end) {
|
2016-06-13 09:01:04 +00:00
|
|
|
const uint32_t insert = (uint32_t)(ip_end - next_emit);
|
2016-01-11 10:21:42 +00:00
|
|
|
EmitInsertLen(insert, commands);
|
2021-11-10 09:34:39 +00:00
|
|
|
BROTLI_LOG(("[CompressFragment] pos = %d insert = %d copy = %d\n",
|
|
|
|
(int)(next_emit - base_ip), insert, 2));
|
2016-01-11 10:21:42 +00:00
|
|
|
memcpy(*literals, next_emit, insert);
|
|
|
|
*literals += insert;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 20:29:43 +00:00
|
|
|
static void StoreCommands(BrotliTwoPassArena* s,
|
2016-06-13 09:01:04 +00:00
|
|
|
const uint8_t* literals, const size_t num_literals,
|
2016-03-19 02:18:59 +00:00
|
|
|
const uint32_t* commands, const size_t num_commands,
|
|
|
|
size_t* storage_ix, uint8_t* storage) {
|
2016-01-11 10:21:42 +00:00
|
|
|
static const uint32_t kNumExtraBits[128] = {
|
2021-07-29 20:29:43 +00:00
|
|
|
0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
|
|
|
|
6, 7, 8, 9, 10, 12, 14, 24, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
1, 1, 2, 2, 3, 3, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 9, 10, 24,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
|
|
|
|
9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16,
|
|
|
|
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24,
|
2016-01-11 10:21:42 +00:00
|
|
|
};
|
|
|
|
static const uint32_t kInsertOffset[24] = {
|
2021-07-29 20:29:43 +00:00
|
|
|
0, 1, 2, 3, 4, 5, 6, 8, 10, 14, 18, 26,
|
|
|
|
34, 50, 66, 98, 130, 194, 322, 578, 1090, 2114, 6210, 22594,
|
2016-01-11 10:21:42 +00:00
|
|
|
};
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
size_t i;
|
2021-07-29 20:29:43 +00:00
|
|
|
memset(s->lit_histo, 0, sizeof(s->lit_histo));
|
2021-11-10 09:34:39 +00:00
|
|
|
/* TODO(eustas): is that necessary? */
|
2021-07-29 20:29:43 +00:00
|
|
|
memset(s->cmd_depth, 0, sizeof(s->cmd_depth));
|
2021-11-10 09:34:39 +00:00
|
|
|
/* TODO(eustas): is that necessary? */
|
2021-07-29 20:29:43 +00:00
|
|
|
memset(s->cmd_bits, 0, sizeof(s->cmd_bits));
|
|
|
|
memset(s->cmd_histo, 0, sizeof(s->cmd_histo));
|
2016-06-13 09:01:04 +00:00
|
|
|
for (i = 0; i < num_literals; ++i) {
|
2021-07-29 20:29:43 +00:00
|
|
|
++s->lit_histo[literals[i]];
|
2016-06-13 09:01:04 +00:00
|
|
|
}
|
2021-07-29 20:29:43 +00:00
|
|
|
BrotliBuildAndStoreHuffmanTreeFast(s->tmp_tree, s->lit_histo, num_literals,
|
|
|
|
/* max_bits = */ 8, s->lit_depth,
|
|
|
|
s->lit_bits, storage_ix, storage);
|
2016-06-13 09:01:04 +00:00
|
|
|
|
|
|
|
for (i = 0; i < num_commands; ++i) {
|
2017-01-26 10:32:18 +00:00
|
|
|
const uint32_t code = commands[i] & 0xFF;
|
2017-12-12 13:33:12 +00:00
|
|
|
BROTLI_DCHECK(code < 128);
|
2021-07-29 20:29:43 +00:00
|
|
|
++s->cmd_histo[code];
|
2016-06-13 09:01:04 +00:00
|
|
|
}
|
2021-07-29 20:29:43 +00:00
|
|
|
s->cmd_histo[1] += 1;
|
|
|
|
s->cmd_histo[2] += 1;
|
|
|
|
s->cmd_histo[64] += 1;
|
|
|
|
s->cmd_histo[84] += 1;
|
|
|
|
BuildAndStoreCommandPrefixCode(s, storage_ix, storage);
|
2016-06-13 09:01:04 +00:00
|
|
|
|
|
|
|
for (i = 0; i < num_commands; ++i) {
|
2016-01-11 10:21:42 +00:00
|
|
|
const uint32_t cmd = commands[i];
|
2017-01-26 10:32:18 +00:00
|
|
|
const uint32_t code = cmd & 0xFF;
|
2016-01-11 10:21:42 +00:00
|
|
|
const uint32_t extra = cmd >> 8;
|
2017-12-12 13:33:12 +00:00
|
|
|
BROTLI_DCHECK(code < 128);
|
2021-07-29 20:29:43 +00:00
|
|
|
BrotliWriteBits(s->cmd_depth[code], s->cmd_bits[code], storage_ix, storage);
|
2016-06-13 09:01:04 +00:00
|
|
|
BrotliWriteBits(kNumExtraBits[code], extra, storage_ix, storage);
|
2016-01-11 10:21:42 +00:00
|
|
|
if (code < 24) {
|
|
|
|
const uint32_t insert = kInsertOffset[code] + extra;
|
2016-06-13 09:01:04 +00:00
|
|
|
uint32_t j;
|
|
|
|
for (j = 0; j < insert; ++j) {
|
2016-01-11 10:21:42 +00:00
|
|
|
const uint8_t lit = *literals;
|
2021-07-29 20:29:43 +00:00
|
|
|
BrotliWriteBits(s->lit_depth[lit], s->lit_bits[lit], storage_ix,
|
|
|
|
storage);
|
2016-01-11 10:21:42 +00:00
|
|
|
++literals;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
/* Acceptable loss for uncompressible speedup is 2% */
|
|
|
|
#define MIN_RATIO 0.98
|
|
|
|
#define SAMPLE_RATE 43
|
|
|
|
|
2021-07-29 20:29:43 +00:00
|
|
|
static BROTLI_BOOL ShouldCompress(BrotliTwoPassArena* s,
|
2016-07-26 12:41:59 +00:00
|
|
|
const uint8_t* input, size_t input_size, size_t num_literals) {
|
2016-06-13 09:01:04 +00:00
|
|
|
double corpus_size = (double)input_size;
|
2020-05-15 09:06:21 +00:00
|
|
|
if ((double)num_literals < MIN_RATIO * corpus_size) {
|
2016-07-26 12:41:59 +00:00
|
|
|
return BROTLI_TRUE;
|
2016-06-13 09:01:04 +00:00
|
|
|
} else {
|
|
|
|
const double max_total_bit_cost = corpus_size * 8 * MIN_RATIO / SAMPLE_RATE;
|
|
|
|
size_t i;
|
2021-07-29 20:29:43 +00:00
|
|
|
memset(s->lit_histo, 0, sizeof(s->lit_histo));
|
2016-06-13 09:01:04 +00:00
|
|
|
for (i = 0; i < input_size; i += SAMPLE_RATE) {
|
2021-07-29 20:29:43 +00:00
|
|
|
++s->lit_histo[input[i]];
|
2016-06-13 09:01:04 +00:00
|
|
|
}
|
2021-07-29 20:29:43 +00:00
|
|
|
return TO_BROTLI_BOOL(BitsEntropy(s->lit_histo, 256) < max_total_bit_cost);
|
2016-01-11 10:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 13:22:45 +00:00
|
|
|
static void RewindBitPosition(const size_t new_storage_ix,
|
|
|
|
size_t* storage_ix, uint8_t* storage) {
|
|
|
|
const size_t bitpos = new_storage_ix & 7;
|
|
|
|
const size_t mask = (1u << bitpos) - 1;
|
|
|
|
storage[new_storage_ix >> 3] &= (uint8_t)mask;
|
|
|
|
*storage_ix = new_storage_ix;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void EmitUncompressedMetaBlock(const uint8_t* input, size_t input_size,
|
|
|
|
size_t* storage_ix, uint8_t* storage) {
|
|
|
|
BrotliStoreMetaBlockHeader(input_size, 1, storage_ix, storage);
|
|
|
|
*storage_ix = (*storage_ix + 7u) & ~7u;
|
|
|
|
memcpy(&storage[*storage_ix >> 3], input, input_size);
|
|
|
|
*storage_ix += input_size << 3;
|
|
|
|
storage[*storage_ix >> 3] = 0;
|
|
|
|
}
|
|
|
|
|
2016-09-21 15:20:36 +00:00
|
|
|
static BROTLI_INLINE void BrotliCompressFragmentTwoPassImpl(
|
2021-07-29 20:29:43 +00:00
|
|
|
BrotliTwoPassArena* s, const uint8_t* input, size_t input_size,
|
2016-09-21 15:20:36 +00:00
|
|
|
BROTLI_BOOL is_last, uint32_t* command_buf, uint8_t* literal_buf,
|
2018-04-13 09:44:34 +00:00
|
|
|
int* table, size_t table_bits, size_t min_match,
|
|
|
|
size_t* storage_ix, uint8_t* storage) {
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Save the start of the first block for position and distance computations.
|
|
|
|
*/
|
2016-01-11 10:21:42 +00:00
|
|
|
const uint8_t* base_ip = input;
|
2017-03-06 13:22:45 +00:00
|
|
|
BROTLI_UNUSED(is_last);
|
2016-01-11 10:21:42 +00:00
|
|
|
|
|
|
|
while (input_size > 0) {
|
2016-06-13 09:01:04 +00:00
|
|
|
size_t block_size =
|
|
|
|
BROTLI_MIN(size_t, input_size, kCompressFragmentTwoPassBlockSize);
|
2016-01-11 10:21:42 +00:00
|
|
|
uint32_t* commands = command_buf;
|
|
|
|
uint8_t* literals = literal_buf;
|
2016-06-13 09:01:04 +00:00
|
|
|
size_t num_literals;
|
2018-04-13 09:44:34 +00:00
|
|
|
CreateCommands(input, block_size, input_size, base_ip, table,
|
|
|
|
table_bits, min_match, &literals, &commands);
|
2016-06-13 09:01:04 +00:00
|
|
|
num_literals = (size_t)(literals - literal_buf);
|
2021-07-29 20:29:43 +00:00
|
|
|
if (ShouldCompress(s, input, block_size, num_literals)) {
|
2016-06-13 09:01:04 +00:00
|
|
|
const size_t num_commands = (size_t)(commands - command_buf);
|
|
|
|
BrotliStoreMetaBlockHeader(block_size, 0, storage_ix, storage);
|
2016-06-03 09:19:23 +00:00
|
|
|
/* No block splits, no contexts. */
|
2016-06-13 09:01:04 +00:00
|
|
|
BrotliWriteBits(13, 0, storage_ix, storage);
|
2021-07-29 20:29:43 +00:00
|
|
|
StoreCommands(s, literal_buf, num_literals, command_buf, num_commands,
|
2016-01-11 10:21:42 +00:00
|
|
|
storage_ix, storage);
|
|
|
|
} else {
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Since we did not find many backward references and the entropy of
|
|
|
|
the data is close to 8 bits, we can simply emit an uncompressed block.
|
|
|
|
This makes compression speed of uncompressible data about 3x faster. */
|
2017-03-06 13:22:45 +00:00
|
|
|
EmitUncompressedMetaBlock(input, block_size, storage_ix, storage);
|
2016-01-11 10:21:42 +00:00
|
|
|
}
|
|
|
|
input += block_size;
|
|
|
|
input_size -= block_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 15:20:36 +00:00
|
|
|
#define FOR_TABLE_BITS_(X) \
|
|
|
|
X(8) X(9) X(10) X(11) X(12) X(13) X(14) X(15) X(16) X(17)
|
|
|
|
|
|
|
|
#define BAKE_METHOD_PARAM_(B) \
|
|
|
|
static BROTLI_NOINLINE void BrotliCompressFragmentTwoPassImpl ## B( \
|
2021-07-29 20:29:43 +00:00
|
|
|
BrotliTwoPassArena* s, const uint8_t* input, size_t input_size, \
|
2016-09-21 15:20:36 +00:00
|
|
|
BROTLI_BOOL is_last, uint32_t* command_buf, uint8_t* literal_buf, \
|
|
|
|
int* table, size_t* storage_ix, uint8_t* storage) { \
|
2018-04-13 09:44:34 +00:00
|
|
|
size_t min_match = (B <= 15) ? 4 : 6; \
|
2021-07-29 20:29:43 +00:00
|
|
|
BrotliCompressFragmentTwoPassImpl(s, input, input_size, is_last, command_buf,\
|
2018-04-13 09:44:34 +00:00
|
|
|
literal_buf, table, B, min_match, storage_ix, storage); \
|
2016-09-21 15:20:36 +00:00
|
|
|
}
|
|
|
|
FOR_TABLE_BITS_(BAKE_METHOD_PARAM_)
|
|
|
|
#undef BAKE_METHOD_PARAM_
|
|
|
|
|
|
|
|
void BrotliCompressFragmentTwoPass(
|
2021-07-29 20:29:43 +00:00
|
|
|
BrotliTwoPassArena* s, const uint8_t* input, size_t input_size,
|
2016-09-21 15:20:36 +00:00
|
|
|
BROTLI_BOOL is_last, uint32_t* command_buf, uint8_t* literal_buf,
|
|
|
|
int* table, size_t table_size, size_t* storage_ix, uint8_t* storage) {
|
2017-03-06 13:22:45 +00:00
|
|
|
const size_t initial_storage_ix = *storage_ix;
|
2016-09-21 15:20:36 +00:00
|
|
|
const size_t table_bits = Log2FloorNonZero(table_size);
|
|
|
|
switch (table_bits) {
|
|
|
|
#define CASE_(B) \
|
|
|
|
case B: \
|
|
|
|
BrotliCompressFragmentTwoPassImpl ## B( \
|
2021-07-29 20:29:43 +00:00
|
|
|
s, input, input_size, is_last, command_buf, \
|
2016-09-21 15:20:36 +00:00
|
|
|
literal_buf, table, storage_ix, storage); \
|
|
|
|
break;
|
|
|
|
FOR_TABLE_BITS_(CASE_)
|
|
|
|
#undef CASE_
|
2017-12-12 13:33:12 +00:00
|
|
|
default: BROTLI_DCHECK(0); break;
|
2016-09-21 15:20:36 +00:00
|
|
|
}
|
2017-03-06 13:22:45 +00:00
|
|
|
|
|
|
|
/* If output is larger than single uncompressed block, rewrite it. */
|
|
|
|
if (*storage_ix - initial_storage_ix > 31 + (input_size << 3)) {
|
|
|
|
RewindBitPosition(initial_storage_ix, storage_ix, storage);
|
|
|
|
EmitUncompressedMetaBlock(input, input_size, storage_ix, storage);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_last) {
|
|
|
|
BrotliWriteBits(1, 1, storage_ix, storage); /* islast */
|
|
|
|
BrotliWriteBits(1, 1, storage_ix, storage); /* isempty */
|
|
|
|
*storage_ix = (*storage_ix + 7u) & ~7u;
|
|
|
|
}
|
2016-09-21 15:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef FOR_TABLE_BITS_
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} /* extern "C" */
|
|
|
|
#endif
|