2015-11-27 10:27:11 +00:00
|
|
|
/* Copyright 2013 Google Inc. All Rights Reserved.
|
|
|
|
|
2015-12-11 10:11:51 +00:00
|
|
|
Distributed under MIT license.
|
2015-11-27 10:27:11 +00:00
|
|
|
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
|
|
|
*/
|
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Block split point selection utilities. */
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2021-09-08 07:18:45 +00:00
|
|
|
#include "block_splitter.h"
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#include <string.h> /* memcpy, memset */
|
2013-10-23 11:06:13 +00:00
|
|
|
|
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 "cluster.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "fast_log.h"
|
|
|
|
#include "histogram.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "quality.h"
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-01-07 15:27:49 +00:00
|
|
|
static const size_t kMaxLiteralHistograms = 100;
|
|
|
|
static const size_t kMaxCommandHistograms = 50;
|
2014-10-28 11:50:33 +00:00
|
|
|
static const double kLiteralBlockSwitchCost = 28.1;
|
2013-10-23 11:06:13 +00:00
|
|
|
static const double kCommandBlockSwitchCost = 13.5;
|
|
|
|
static const double kDistanceBlockSwitchCost = 14.6;
|
2016-01-07 15:27:49 +00:00
|
|
|
static const size_t kLiteralStrideLength = 70;
|
|
|
|
static const size_t kCommandStrideLength = 40;
|
2021-01-18 09:56:39 +00:00
|
|
|
static const size_t kDistanceStrideLength = 40;
|
2016-01-07 15:27:49 +00:00
|
|
|
static const size_t kSymbolsPerLiteralHistogram = 544;
|
|
|
|
static const size_t kSymbolsPerCommandHistogram = 530;
|
|
|
|
static const size_t kSymbolsPerDistanceHistogram = 544;
|
|
|
|
static const size_t kMinLengthForBlockSplitting = 128;
|
|
|
|
static const size_t kIterMulForRefining = 2;
|
|
|
|
static const size_t kMinItersForRefining = 100;
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
static size_t CountLiterals(const Command* cmds, const size_t num_commands) {
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Count how many we have. */
|
2013-10-23 11:06:13 +00:00
|
|
|
size_t total_length = 0;
|
2016-06-13 09:01:04 +00:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < num_commands; ++i) {
|
2014-10-28 12:25:22 +00:00
|
|
|
total_length += cmds[i].insert_len_;
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
2016-06-13 09:01:04 +00:00
|
|
|
return total_length;
|
|
|
|
}
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
static void CopyLiteralsToByteArray(const Command* cmds,
|
|
|
|
const size_t num_commands,
|
|
|
|
const uint8_t* data,
|
|
|
|
const size_t offset,
|
|
|
|
const size_t mask,
|
|
|
|
uint8_t* literals) {
|
2013-10-23 11:06:13 +00:00
|
|
|
size_t pos = 0;
|
2015-07-30 15:42:02 +00:00
|
|
|
size_t from_pos = offset & mask;
|
2016-06-13 09:01:04 +00:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < num_commands; ++i) {
|
2015-07-30 15:42:02 +00:00
|
|
|
size_t insert_len = cmds[i].insert_len_;
|
|
|
|
if (from_pos + insert_len > mask) {
|
|
|
|
size_t head_size = mask + 1 - from_pos;
|
2016-06-13 09:01:04 +00:00
|
|
|
memcpy(literals + pos, data + from_pos, head_size);
|
2015-07-30 15:42:02 +00:00
|
|
|
from_pos = 0;
|
|
|
|
pos += head_size;
|
|
|
|
insert_len -= head_size;
|
|
|
|
}
|
|
|
|
if (insert_len > 0) {
|
2016-06-13 09:01:04 +00:00
|
|
|
memcpy(literals + pos, data + from_pos, insert_len);
|
2015-07-30 15:42:02 +00:00
|
|
|
pos += insert_len;
|
|
|
|
}
|
2016-06-13 09:01:04 +00:00
|
|
|
from_pos = (from_pos + insert_len + CommandCopyLen(&cmds[i])) & mask;
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 13:57:15 +00:00
|
|
|
static BROTLI_INLINE uint32_t MyRand(uint32_t* seed) {
|
|
|
|
/* Initial seed should be 7. In this case, loop length is (1 << 29). */
|
2015-02-25 17:19:51 +00:00
|
|
|
*seed *= 16807U;
|
|
|
|
return *seed;
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
static BROTLI_INLINE double BitCost(size_t count) {
|
2016-01-07 15:27:49 +00:00
|
|
|
return count == 0 ? -2.0 : FastLog2(count);
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#define HISTOGRAMS_PER_BATCH 64
|
|
|
|
#define CLUSTERS_PER_BATCH 16
|
|
|
|
|
|
|
|
#define FN(X) X ## Literal
|
|
|
|
#define DataType uint8_t
|
|
|
|
/* NOLINTNEXTLINE(build/include) */
|
2021-09-08 07:18:45 +00:00
|
|
|
#include "block_splitter_inc.h"
|
2016-06-13 09:01:04 +00:00
|
|
|
#undef DataType
|
|
|
|
#undef FN
|
|
|
|
|
|
|
|
#define FN(X) X ## Command
|
|
|
|
#define DataType uint16_t
|
|
|
|
/* NOLINTNEXTLINE(build/include) */
|
2021-09-08 07:18:45 +00:00
|
|
|
#include "block_splitter_inc.h"
|
2016-06-13 09:01:04 +00:00
|
|
|
#undef FN
|
|
|
|
|
|
|
|
#define FN(X) X ## Distance
|
|
|
|
/* NOLINTNEXTLINE(build/include) */
|
2021-09-08 07:18:45 +00:00
|
|
|
#include "block_splitter_inc.h"
|
2016-06-13 09:01:04 +00:00
|
|
|
#undef DataType
|
|
|
|
#undef FN
|
|
|
|
|
|
|
|
void BrotliInitBlockSplit(BlockSplit* self) {
|
|
|
|
self->num_types = 0;
|
|
|
|
self->num_blocks = 0;
|
|
|
|
self->types = 0;
|
|
|
|
self->lengths = 0;
|
|
|
|
self->types_alloc_size = 0;
|
|
|
|
self->lengths_alloc_size = 0;
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
void BrotliDestroyBlockSplit(MemoryManager* m, BlockSplit* self) {
|
|
|
|
BROTLI_FREE(m, self->types);
|
|
|
|
BROTLI_FREE(m, self->lengths);
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 09:56:39 +00:00
|
|
|
/* Extracts literals, command distance and prefix codes, then applies
|
|
|
|
* SplitByteVector to create partitioning. */
|
2016-06-13 09:01:04 +00:00
|
|
|
void BrotliSplitBlock(MemoryManager* m,
|
|
|
|
const Command* cmds,
|
|
|
|
const size_t num_commands,
|
|
|
|
const uint8_t* data,
|
|
|
|
const size_t pos,
|
|
|
|
const size_t mask,
|
2016-07-26 12:41:59 +00:00
|
|
|
const BrotliEncoderParams* params,
|
2016-06-13 09:01:04 +00:00
|
|
|
BlockSplit* literal_split,
|
|
|
|
BlockSplit* insert_and_copy_split,
|
|
|
|
BlockSplit* dist_split) {
|
2016-03-15 09:50:16 +00:00
|
|
|
{
|
2016-06-13 09:01:04 +00:00
|
|
|
size_t literals_count = CountLiterals(cmds, num_commands);
|
|
|
|
uint8_t* literals = BROTLI_ALLOC(m, uint8_t, literals_count);
|
2019-05-03 09:51:11 +00:00
|
|
|
if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(literals)) return;
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Create a continuous array of literals. */
|
2016-06-13 09:01:04 +00:00
|
|
|
CopyLiteralsToByteArray(cmds, num_commands, data, pos, mask, literals);
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Create the block split on the array of literals.
|
2021-01-18 09:56:39 +00:00
|
|
|
* Literal histograms can have alphabet size up to 256.
|
|
|
|
* Though, to accomodate context modeling, less than half of maximum size
|
|
|
|
* is allowed. */
|
2016-06-13 09:01:04 +00:00
|
|
|
SplitByteVectorLiteral(
|
|
|
|
m, literals, literals_count,
|
2016-03-15 09:50:16 +00:00
|
|
|
kSymbolsPerLiteralHistogram, kMaxLiteralHistograms,
|
2016-07-26 12:41:59 +00:00
|
|
|
kLiteralStrideLength, kLiteralBlockSwitchCost, params,
|
2016-03-15 09:50:16 +00:00
|
|
|
literal_split);
|
2016-06-13 09:01:04 +00:00
|
|
|
if (BROTLI_IS_OOM(m)) return;
|
|
|
|
BROTLI_FREE(m, literals);
|
2021-01-18 09:56:39 +00:00
|
|
|
/* NB: this might be a good place for injecting extra splitting without
|
|
|
|
* increasing encoder complexity; however, output parition would be less
|
|
|
|
* optimal than one produced with forced splitting inside
|
|
|
|
* SplitByteVector (FindBlocks / ClusterBlocks). */
|
2016-03-15 09:50:16 +00:00
|
|
|
}
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-03-15 09:50:16 +00:00
|
|
|
{
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Compute prefix codes for commands. */
|
2016-06-13 09:01:04 +00:00
|
|
|
uint16_t* insert_and_copy_codes = BROTLI_ALLOC(m, uint16_t, num_commands);
|
|
|
|
size_t i;
|
2019-05-03 09:51:11 +00:00
|
|
|
if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(insert_and_copy_codes)) return;
|
2016-06-13 09:01:04 +00:00
|
|
|
for (i = 0; i < num_commands; ++i) {
|
2016-03-15 09:50:16 +00:00
|
|
|
insert_and_copy_codes[i] = cmds[i].cmd_prefix_;
|
|
|
|
}
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Create the block split on the array of command prefixes. */
|
2016-06-13 09:01:04 +00:00
|
|
|
SplitByteVectorCommand(
|
|
|
|
m, insert_and_copy_codes, num_commands,
|
2016-03-15 09:50:16 +00:00
|
|
|
kSymbolsPerCommandHistogram, kMaxCommandHistograms,
|
2016-07-26 12:41:59 +00:00
|
|
|
kCommandStrideLength, kCommandBlockSwitchCost, params,
|
2016-03-15 09:50:16 +00:00
|
|
|
insert_and_copy_split);
|
2016-06-13 09:01:04 +00:00
|
|
|
if (BROTLI_IS_OOM(m)) return;
|
2021-11-10 09:34:39 +00:00
|
|
|
/* TODO(eustas): reuse for distances? */
|
2016-06-13 09:01:04 +00:00
|
|
|
BROTLI_FREE(m, insert_and_copy_codes);
|
2016-03-15 09:50:16 +00:00
|
|
|
}
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-03-15 09:50:16 +00:00
|
|
|
{
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Create a continuous array of distance prefixes. */
|
2016-06-13 09:01:04 +00:00
|
|
|
uint16_t* distance_prefixes = BROTLI_ALLOC(m, uint16_t, num_commands);
|
|
|
|
size_t j = 0;
|
|
|
|
size_t i;
|
2019-05-03 09:51:11 +00:00
|
|
|
if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(distance_prefixes)) return;
|
2016-06-13 09:01:04 +00:00
|
|
|
for (i = 0; i < num_commands; ++i) {
|
|
|
|
const Command* cmd = &cmds[i];
|
|
|
|
if (CommandCopyLen(cmd) && cmd->cmd_prefix_ >= 128) {
|
2018-02-26 14:04:36 +00:00
|
|
|
distance_prefixes[j++] = cmd->dist_prefix_ & 0x3FF;
|
2016-03-15 09:50:16 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Create the block split on the array of distance prefixes. */
|
2016-06-13 09:01:04 +00:00
|
|
|
SplitByteVectorDistance(
|
|
|
|
m, distance_prefixes, j,
|
2016-03-15 09:50:16 +00:00
|
|
|
kSymbolsPerDistanceHistogram, kMaxCommandHistograms,
|
2021-01-18 09:56:39 +00:00
|
|
|
kDistanceStrideLength, kDistanceBlockSwitchCost, params,
|
2016-03-15 09:50:16 +00:00
|
|
|
dist_split);
|
2016-06-13 09:01:04 +00:00
|
|
|
if (BROTLI_IS_OOM(m)) return;
|
|
|
|
BROTLI_FREE(m, distance_prefixes);
|
2016-03-15 09:50:16 +00:00
|
|
|
}
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 09:34:39 +00:00
|
|
|
#if defined(BROTLI_TEST)
|
|
|
|
size_t CountLiteralsForTest(const Command*, const size_t);
|
|
|
|
size_t CountLiteralsForTest(const Command* cmds, const size_t num_commands) {
|
|
|
|
return CountLiterals(cmds, num_commands);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CopyLiteralsToByteArrayForTest(const Command*,
|
|
|
|
const size_t, const uint8_t*, const size_t, const size_t, uint8_t*);
|
|
|
|
void CopyLiteralsToByteArrayForTest(const Command* cmds,
|
|
|
|
const size_t num_commands, const uint8_t* data, const size_t offset,
|
|
|
|
const size_t mask, uint8_t* literals) {
|
|
|
|
CopyLiteralsToByteArray(cmds, num_commands, data, offset, mask, literals);
|
|
|
|
}
|
|
|
|
#endif
|
2016-06-13 09:01:04 +00:00
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} /* extern "C" */
|
|
|
|
#endif
|