Skip to content

Commit

Permalink
Use BROTLI_MAX_STATIC_CONTEXTS instead of magic constants in encode.c
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 615341475
  • Loading branch information
Brotli authored and Copybara-Service committed Mar 13, 2024
1 parent ccec962 commit 9717649
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
9 changes: 5 additions & 4 deletions c/enc/encode.c
Expand Up @@ -351,7 +351,7 @@ static BROTLI_BOOL ShouldUseComplexStaticContextMap(const uint8_t* input,
size_t sink;
size_t i;
ContextLut utf8_lut = BROTLI_CONTEXT_LUT(CONTEXT_UTF8);
memset(arena, 0, sizeof(arena[0]) * 32 * 14);
memset(arena, 0, sizeof(arena[0]) * 32 * (BROTLI_MAX_STATIC_CONTEXTS + 1));
for (; start_pos + 64 <= end_pos; start_pos += 4096) {
const size_t stride_end_pos = start_pos + 64;
uint8_t prev2 = input[start_pos & mask];
Expand All @@ -372,7 +372,7 @@ static BROTLI_BOOL ShouldUseComplexStaticContextMap(const uint8_t* input,
}
entropy[1] = ShannonEntropy(combined_histo, 32, &sink);
entropy[2] = 0;
for (i = 0; i < 13; ++i) {
for (i = 0; i < BROTLI_MAX_STATIC_CONTEXTS; ++i) {
entropy[2] += ShannonEntropy(context_histo + (i << 5), 32, &sink);
}
entropy[0] = 1.0 / (double)total;
Expand All @@ -388,7 +388,7 @@ static BROTLI_BOOL ShouldUseComplexStaticContextMap(const uint8_t* input,
if (entropy[2] > 3.0 || entropy[1] - entropy[2] < 0.2) {
return BROTLI_FALSE;
} else {
*num_literal_contexts = 13;
*num_literal_contexts = BROTLI_MAX_STATIC_CONTEXTS;
*literal_context_map = kStaticContextMapComplexUTF8;
return BROTLI_TRUE;
}
Expand Down Expand Up @@ -532,7 +532,8 @@ static void WriteMetaBlockInternal(MemoryManager* m,
const uint32_t* literal_context_map = NULL;
if (!params->disable_literal_context_modeling) {
/* TODO(eustas): pull to higher level and reuse. */
uint32_t* arena = BROTLI_ALLOC(m, uint32_t, 14 * 32);
uint32_t* arena =
BROTLI_ALLOC(m, uint32_t, 32 * (BROTLI_MAX_STATIC_CONTEXTS + 1));
if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(arena)) return;
DecideOverLiteralContextModeling(
data, wrapped_last_flush_pos, bytes, mask, params->quality,
Expand Down
2 changes: 0 additions & 2 deletions c/enc/metablock.c
Expand Up @@ -298,8 +298,6 @@ void BrotliBuildMetaBlock(MemoryManager* m,
#include "metablock_inc.h" /* NOLINT(build/include) */
#undef FN

#define BROTLI_MAX_STATIC_CONTEXTS 13

/* Greedy block splitter for one block category (literal, command or distance).
Gathers histograms for all context buckets. */
typedef struct ContextBlockSplitter {
Expand Down
2 changes: 2 additions & 0 deletions c/enc/metablock.h
Expand Up @@ -24,6 +24,8 @@
extern "C" {
#endif

#define BROTLI_MAX_STATIC_CONTEXTS 13

typedef struct MetaBlockSplit {
BlockSplit literal_split;
BlockSplit command_split;
Expand Down

0 comments on commit 9717649

Please sign in to comment.