mirror of
https://github.com/webmproject/libwebp.git
synced 2025-07-15 13:29:54 +02:00
Compare commits
3 Commits
615e58744f
...
0.5.2
Author | SHA1 | Date | |
---|---|---|---|
e1f1bce9dc | |||
90d47113bc | |||
bc99ef0699 |
204
src/dec/vp8l.c
204
src/dec/vp8l.c
@ -252,11 +252,11 @@ static int ReadHuffmanCodeLengths(
|
|||||||
int symbol;
|
int symbol;
|
||||||
int max_symbol;
|
int max_symbol;
|
||||||
int prev_code_len = DEFAULT_CODE_LENGTH;
|
int prev_code_len = DEFAULT_CODE_LENGTH;
|
||||||
HuffmanCode table[1 << LENGTHS_TABLE_BITS];
|
HuffmanTables tables;
|
||||||
|
|
||||||
if (!VP8LBuildHuffmanTable(table, LENGTHS_TABLE_BITS,
|
if (!VP8LHuffmanTablesAllocate(1 << LENGTHS_TABLE_BITS, &tables) ||
|
||||||
code_length_code_lengths,
|
!VP8LBuildHuffmanTable(&tables, LENGTHS_TABLE_BITS,
|
||||||
NUM_CODE_LENGTH_CODES)) {
|
code_length_code_lengths, NUM_CODE_LENGTH_CODES)) {
|
||||||
goto End;
|
goto End;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,7 +276,7 @@ static int ReadHuffmanCodeLengths(
|
|||||||
int code_len;
|
int code_len;
|
||||||
if (max_symbol-- == 0) break;
|
if (max_symbol-- == 0) break;
|
||||||
VP8LFillBitWindow(br);
|
VP8LFillBitWindow(br);
|
||||||
p = &table[VP8LPrefetchBits(br) & LENGTHS_TABLE_MASK];
|
p = &tables.curr_segment->start[VP8LPrefetchBits(br) & LENGTHS_TABLE_MASK];
|
||||||
VP8LSetBitPos(br, br->bit_pos_ + p->bits);
|
VP8LSetBitPos(br, br->bit_pos_ + p->bits);
|
||||||
code_len = p->value;
|
code_len = p->value;
|
||||||
if (code_len < kCodeLengthLiterals) {
|
if (code_len < kCodeLengthLiterals) {
|
||||||
@ -299,6 +299,7 @@ static int ReadHuffmanCodeLengths(
|
|||||||
ok = 1;
|
ok = 1;
|
||||||
|
|
||||||
End:
|
End:
|
||||||
|
VP8LHuffmanTablesDeallocate(&tables);
|
||||||
if (!ok) dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
|
if (!ok) dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
@ -306,7 +307,8 @@ static int ReadHuffmanCodeLengths(
|
|||||||
// 'code_lengths' is pre-allocated temporary buffer, used for creating Huffman
|
// 'code_lengths' is pre-allocated temporary buffer, used for creating Huffman
|
||||||
// tree.
|
// tree.
|
||||||
static int ReadHuffmanCode(int alphabet_size, VP8LDecoder* const dec,
|
static int ReadHuffmanCode(int alphabet_size, VP8LDecoder* const dec,
|
||||||
int* const code_lengths, HuffmanCode* const table) {
|
int* const code_lengths,
|
||||||
|
HuffmanTables* const table) {
|
||||||
int ok = 0;
|
int ok = 0;
|
||||||
int size = 0;
|
int size = 0;
|
||||||
VP8LBitReader* const br = &dec->br_;
|
VP8LBitReader* const br = &dec->br_;
|
||||||
@ -361,12 +363,18 @@ static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize,
|
|||||||
VP8LMetadata* const hdr = &dec->hdr_;
|
VP8LMetadata* const hdr = &dec->hdr_;
|
||||||
uint32_t* huffman_image = NULL;
|
uint32_t* huffman_image = NULL;
|
||||||
HTreeGroup* htree_groups = NULL;
|
HTreeGroup* htree_groups = NULL;
|
||||||
HuffmanCode* huffman_tables = NULL;
|
HuffmanTables* huffman_tables = &hdr->huffman_tables_;
|
||||||
HuffmanCode* next = NULL;
|
|
||||||
int num_htree_groups = 1;
|
int num_htree_groups = 1;
|
||||||
|
int num_htree_groups_max = 1;
|
||||||
int max_alphabet_size = 0;
|
int max_alphabet_size = 0;
|
||||||
int* code_lengths = NULL;
|
int* code_lengths = NULL;
|
||||||
const int table_size = kTableSize[color_cache_bits];
|
const int table_size = kTableSize[color_cache_bits];
|
||||||
|
int* mapping = NULL;
|
||||||
|
int ok = 0;
|
||||||
|
|
||||||
|
// Check the table has been 0 initialized (through InitMetadata).
|
||||||
|
assert(huffman_tables->root.start == NULL);
|
||||||
|
assert(huffman_tables->curr_segment == NULL);
|
||||||
|
|
||||||
if (allow_recursion && VP8LReadBits(br, 1)) {
|
if (allow_recursion && VP8LReadBits(br, 1)) {
|
||||||
// use meta Huffman codes.
|
// use meta Huffman codes.
|
||||||
@ -383,10 +391,36 @@ static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize,
|
|||||||
// The huffman data is stored in red and green bytes.
|
// The huffman data is stored in red and green bytes.
|
||||||
const int group = (huffman_image[i] >> 8) & 0xffff;
|
const int group = (huffman_image[i] >> 8) & 0xffff;
|
||||||
huffman_image[i] = group;
|
huffman_image[i] = group;
|
||||||
if (group >= num_htree_groups) {
|
if (group >= num_htree_groups_max) {
|
||||||
num_htree_groups = group + 1;
|
num_htree_groups_max = group + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Check the validity of num_htree_groups_max. If it seems too big, use a
|
||||||
|
// smaller value for later. This will prevent big memory allocations to end
|
||||||
|
// up with a bad bitstream anyway.
|
||||||
|
// The value of 1000 is totally arbitrary. We know that num_htree_groups_max
|
||||||
|
// is smaller than (1 << 16) and should be smaller than the number of pixels
|
||||||
|
// (though the format allows it to be bigger).
|
||||||
|
if (num_htree_groups_max > 1000 || num_htree_groups_max > xsize * ysize) {
|
||||||
|
// Create a mapping from the used indices to the minimal set of used
|
||||||
|
// values [0, num_htree_groups)
|
||||||
|
mapping = (int*)WebPSafeMalloc(num_htree_groups_max, sizeof(*mapping));
|
||||||
|
if (mapping == NULL) {
|
||||||
|
dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
|
||||||
|
goto Error;
|
||||||
|
}
|
||||||
|
// -1 means a value is unmapped, and therefore unused in the Huffman
|
||||||
|
// image.
|
||||||
|
memset(mapping, 0xff, num_htree_groups_max * sizeof(*mapping));
|
||||||
|
for (num_htree_groups = 0, i = 0; i < huffman_pixs; ++i) {
|
||||||
|
// Get the current mapping for the group and remap the Huffman image.
|
||||||
|
int* const mapped_group = &mapping[huffman_image[i]];
|
||||||
|
if (*mapped_group == -1) *mapped_group = num_htree_groups++;
|
||||||
|
huffman_image[i] = *mapped_group;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
num_htree_groups = num_htree_groups_max;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (br->eos_) goto Error;
|
if (br->eos_) goto Error;
|
||||||
@ -402,83 +436,99 @@ static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
huffman_tables = (HuffmanCode*)WebPSafeMalloc(num_htree_groups * table_size,
|
|
||||||
sizeof(*huffman_tables));
|
|
||||||
htree_groups = VP8LHtreeGroupsNew(num_htree_groups);
|
htree_groups = VP8LHtreeGroupsNew(num_htree_groups);
|
||||||
code_lengths = (int*)WebPSafeCalloc((uint64_t)max_alphabet_size,
|
code_lengths = (int*)WebPSafeCalloc((uint64_t)max_alphabet_size,
|
||||||
sizeof(*code_lengths));
|
sizeof(*code_lengths));
|
||||||
|
|
||||||
if (htree_groups == NULL || code_lengths == NULL || huffman_tables == NULL) {
|
if (htree_groups == NULL || code_lengths == NULL ||
|
||||||
|
!VP8LHuffmanTablesAllocate(num_htree_groups * table_size,
|
||||||
|
huffman_tables)) {
|
||||||
dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
|
dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
|
||||||
goto Error;
|
goto Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
next = huffman_tables;
|
for (i = 0; i < num_htree_groups_max; ++i) {
|
||||||
for (i = 0; i < num_htree_groups; ++i) {
|
// If the index "i" is unused in the Huffman image, just make sure the
|
||||||
HTreeGroup* const htree_group = &htree_groups[i];
|
// coefficients are valid but do not store them.
|
||||||
HuffmanCode** const htrees = htree_group->htrees;
|
if (mapping != NULL && mapping[i] == -1) {
|
||||||
int size;
|
for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
|
||||||
int total_size = 0;
|
int alphabet_size = kAlphabetSize[j];
|
||||||
int is_trivial_literal = 1;
|
if (j == 0 && color_cache_bits > 0) {
|
||||||
int max_bits = 0;
|
alphabet_size += (1 << color_cache_bits);
|
||||||
for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
|
}
|
||||||
int alphabet_size = kAlphabetSize[j];
|
// Passing in NULL so that nothing gets filled.
|
||||||
htrees[j] = next;
|
if (!ReadHuffmanCode(alphabet_size, dec, code_lengths, NULL)) {
|
||||||
if (j == 0 && color_cache_bits > 0) {
|
goto Error;
|
||||||
alphabet_size += 1 << color_cache_bits;
|
|
||||||
}
|
|
||||||
size = ReadHuffmanCode(alphabet_size, dec, code_lengths, next);
|
|
||||||
if (size == 0) {
|
|
||||||
goto Error;
|
|
||||||
}
|
|
||||||
if (is_trivial_literal && kLiteralMap[j] == 1) {
|
|
||||||
is_trivial_literal = (next->bits == 0);
|
|
||||||
}
|
|
||||||
total_size += next->bits;
|
|
||||||
next += size;
|
|
||||||
if (j <= ALPHA) {
|
|
||||||
int local_max_bits = code_lengths[0];
|
|
||||||
int k;
|
|
||||||
for (k = 1; k < alphabet_size; ++k) {
|
|
||||||
if (code_lengths[k] > local_max_bits) {
|
|
||||||
local_max_bits = code_lengths[k];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
max_bits += local_max_bits;
|
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
htree_group->is_trivial_literal = is_trivial_literal;
|
HTreeGroup* const htree_group =
|
||||||
htree_group->is_trivial_code = 0;
|
&htree_groups[(mapping == NULL) ? i : mapping[i]];
|
||||||
if (is_trivial_literal) {
|
HuffmanCode** const htrees = htree_group->htrees;
|
||||||
const int red = htrees[RED][0].value;
|
int size;
|
||||||
const int blue = htrees[BLUE][0].value;
|
int total_size = 0;
|
||||||
const int alpha = htrees[ALPHA][0].value;
|
int is_trivial_literal = 1;
|
||||||
htree_group->literal_arb =
|
int max_bits = 0;
|
||||||
((uint32_t)alpha << 24) | (red << 16) | blue;
|
for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
|
||||||
if (total_size == 0 && htrees[GREEN][0].value < NUM_LITERAL_CODES) {
|
int alphabet_size = kAlphabetSize[j];
|
||||||
htree_group->is_trivial_code = 1;
|
if (j == 0 && color_cache_bits > 0) {
|
||||||
htree_group->literal_arb |= htrees[GREEN][0].value << 8;
|
alphabet_size += (1 << color_cache_bits);
|
||||||
|
}
|
||||||
|
size =
|
||||||
|
ReadHuffmanCode(alphabet_size, dec, code_lengths, huffman_tables);
|
||||||
|
htrees[j] = huffman_tables->curr_segment->curr_table;
|
||||||
|
if (size == 0) {
|
||||||
|
goto Error;
|
||||||
|
}
|
||||||
|
if (is_trivial_literal && kLiteralMap[j] == 1) {
|
||||||
|
is_trivial_literal = (htrees[j]->bits == 0);
|
||||||
|
}
|
||||||
|
total_size += htrees[j]->bits;
|
||||||
|
huffman_tables->curr_segment->curr_table += size;
|
||||||
|
if (j <= ALPHA) {
|
||||||
|
int local_max_bits = code_lengths[0];
|
||||||
|
int k;
|
||||||
|
for (k = 1; k < alphabet_size; ++k) {
|
||||||
|
if (code_lengths[k] > local_max_bits) {
|
||||||
|
local_max_bits = code_lengths[k];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
max_bits += local_max_bits;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
htree_group->is_trivial_literal = is_trivial_literal;
|
||||||
|
htree_group->is_trivial_code = 0;
|
||||||
|
if (is_trivial_literal) {
|
||||||
|
const int red = htrees[RED][0].value;
|
||||||
|
const int blue = htrees[BLUE][0].value;
|
||||||
|
const int alpha = htrees[ALPHA][0].value;
|
||||||
|
htree_group->literal_arb = ((uint32_t)alpha << 24) | (red << 16) | blue;
|
||||||
|
if (total_size == 0 && htrees[GREEN][0].value < NUM_LITERAL_CODES) {
|
||||||
|
htree_group->is_trivial_code = 1;
|
||||||
|
htree_group->literal_arb |= htrees[GREEN][0].value << 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
htree_group->use_packed_table =
|
||||||
|
!htree_group->is_trivial_code && (max_bits < HUFFMAN_PACKED_BITS);
|
||||||
|
if (htree_group->use_packed_table) BuildPackedTable(htree_group);
|
||||||
}
|
}
|
||||||
htree_group->use_packed_table = !htree_group->is_trivial_code &&
|
|
||||||
(max_bits < HUFFMAN_PACKED_BITS);
|
|
||||||
if (htree_group->use_packed_table) BuildPackedTable(htree_group);
|
|
||||||
}
|
}
|
||||||
WebPSafeFree(code_lengths);
|
ok = 1;
|
||||||
|
|
||||||
// All OK. Finalize pointers and return.
|
// All OK. Finalize pointers.
|
||||||
hdr->huffman_image_ = huffman_image;
|
hdr->huffman_image_ = huffman_image;
|
||||||
hdr->num_htree_groups_ = num_htree_groups;
|
hdr->num_htree_groups_ = num_htree_groups;
|
||||||
hdr->htree_groups_ = htree_groups;
|
hdr->htree_groups_ = htree_groups;
|
||||||
hdr->huffman_tables_ = huffman_tables;
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
Error:
|
Error:
|
||||||
WebPSafeFree(code_lengths);
|
WebPSafeFree(code_lengths);
|
||||||
WebPSafeFree(huffman_image);
|
WebPSafeFree(mapping);
|
||||||
WebPSafeFree(huffman_tables);
|
if (!ok) {
|
||||||
VP8LHtreeGroupsFree(htree_groups);
|
WebPSafeFree(huffman_image);
|
||||||
return 0;
|
VP8LHuffmanTablesDeallocate(huffman_tables);
|
||||||
|
VP8LHtreeGroupsFree(htree_groups);
|
||||||
|
}
|
||||||
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
@ -1163,9 +1213,21 @@ static int DecodeImageData(VP8LDecoder* const dec, uint32_t* const data,
|
|||||||
assert(br->eos_ == VP8LIsEndOfStream(br));
|
assert(br->eos_ == VP8LIsEndOfStream(br));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dec->incremental_ && br->eos_ && src < src_end) {
|
br->eos_ = VP8LIsEndOfStream(br);
|
||||||
|
// In incremental decoding:
|
||||||
|
// br->eos_ && src < src_last: if 'br' reached the end of the buffer and
|
||||||
|
// 'src_last' has not been reached yet, there is not enough data. 'dec' has to
|
||||||
|
// be reset until there is more data.
|
||||||
|
// !br->eos_ && src < src_last: this cannot happen as either the buffer is
|
||||||
|
// fully read, either enough has been read to reach 'src_last'.
|
||||||
|
// src >= src_last: 'src_last' is reached, all is fine. 'src' can actually go
|
||||||
|
// beyond 'src_last' in case the image is cropped and an LZ77 goes further.
|
||||||
|
// The buffer might have been enough or there is some left. 'br->eos_' does
|
||||||
|
// not matter.
|
||||||
|
assert(!dec->incremental_ || (br->eos_ && src < src_last) || src >= src_last);
|
||||||
|
if (dec->incremental_ && br->eos_ && src < src_last) {
|
||||||
RestoreState(dec);
|
RestoreState(dec);
|
||||||
} else if (!br->eos_) {
|
} else if ((dec->incremental_ && src >= src_last) || !br->eos_) {
|
||||||
// Process the remaining rows corresponding to last row-block.
|
// Process the remaining rows corresponding to last row-block.
|
||||||
if (process_func != NULL) {
|
if (process_func != NULL) {
|
||||||
process_func(dec, row > last_row ? last_row : row);
|
process_func(dec, row > last_row ? last_row : row);
|
||||||
@ -1283,7 +1345,7 @@ static void ClearMetadata(VP8LMetadata* const hdr) {
|
|||||||
assert(hdr != NULL);
|
assert(hdr != NULL);
|
||||||
|
|
||||||
WebPSafeFree(hdr->huffman_image_);
|
WebPSafeFree(hdr->huffman_image_);
|
||||||
WebPSafeFree(hdr->huffman_tables_);
|
VP8LHuffmanTablesDeallocate(&hdr->huffman_tables_);
|
||||||
VP8LHtreeGroupsFree(hdr->htree_groups_);
|
VP8LHtreeGroupsFree(hdr->htree_groups_);
|
||||||
VP8LColorCacheClear(&hdr->color_cache_);
|
VP8LColorCacheClear(&hdr->color_cache_);
|
||||||
VP8LColorCacheClear(&hdr->saved_color_cache_);
|
VP8LColorCacheClear(&hdr->saved_color_cache_);
|
||||||
@ -1598,7 +1660,7 @@ int VP8LDecodeImage(VP8LDecoder* const dec) {
|
|||||||
// Sanity checks.
|
// Sanity checks.
|
||||||
if (dec == NULL) return 0;
|
if (dec == NULL) return 0;
|
||||||
|
|
||||||
assert(dec->hdr_.huffman_tables_ != NULL);
|
assert(dec->hdr_.huffman_tables_.root.start != NULL);
|
||||||
assert(dec->hdr_.htree_groups_ != NULL);
|
assert(dec->hdr_.htree_groups_ != NULL);
|
||||||
assert(dec->hdr_.num_htree_groups_ > 0);
|
assert(dec->hdr_.num_htree_groups_ > 0);
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ typedef struct {
|
|||||||
uint32_t *huffman_image_;
|
uint32_t *huffman_image_;
|
||||||
int num_htree_groups_;
|
int num_htree_groups_;
|
||||||
HTreeGroup *htree_groups_;
|
HTreeGroup *htree_groups_;
|
||||||
HuffmanCode *huffman_tables_;
|
HuffmanTables huffman_tables_;
|
||||||
} VP8LMetadata;
|
} VP8LMetadata;
|
||||||
|
|
||||||
typedef struct VP8LDecoder VP8LDecoder;
|
typedef struct VP8LDecoder VP8LDecoder;
|
||||||
|
@ -75,11 +75,13 @@ static WEBP_INLINE int NextTableBitSize(const int* const count,
|
|||||||
return len - root_bits;
|
return len - root_bits;
|
||||||
}
|
}
|
||||||
|
|
||||||
int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
|
// sorted[code_lengths_size] is a pre-allocated array for sorting symbols
|
||||||
const int code_lengths[], int code_lengths_size) {
|
// by code length.
|
||||||
|
static int BuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
|
||||||
|
const int code_lengths[], int code_lengths_size,
|
||||||
|
uint16_t sorted[]) {
|
||||||
HuffmanCode* table = root_table; // next available space in table
|
HuffmanCode* table = root_table; // next available space in table
|
||||||
int total_size = 1 << root_bits; // total size root table + 2nd level table
|
int total_size = 1 << root_bits; // total size root table + 2nd level table
|
||||||
int* sorted = NULL; // symbols sorted by code length
|
|
||||||
int len; // current code length
|
int len; // current code length
|
||||||
int symbol; // symbol index in original or sorted table
|
int symbol; // symbol index in original or sorted table
|
||||||
// number of codes of each length:
|
// number of codes of each length:
|
||||||
@ -89,7 +91,8 @@ int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
|
|||||||
|
|
||||||
assert(code_lengths_size != 0);
|
assert(code_lengths_size != 0);
|
||||||
assert(code_lengths != NULL);
|
assert(code_lengths != NULL);
|
||||||
assert(root_table != NULL);
|
assert((root_table != NULL && sorted != NULL) ||
|
||||||
|
(root_table == NULL && sorted == NULL));
|
||||||
assert(root_bits > 0);
|
assert(root_bits > 0);
|
||||||
|
|
||||||
// Build histogram of code lengths.
|
// Build histogram of code lengths.
|
||||||
@ -114,26 +117,26 @@ int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
|
|||||||
offset[len + 1] = offset[len] + count[len];
|
offset[len + 1] = offset[len] + count[len];
|
||||||
}
|
}
|
||||||
|
|
||||||
sorted = (int*)WebPSafeMalloc(code_lengths_size, sizeof(*sorted));
|
|
||||||
if (sorted == NULL) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort symbols by length, by symbol order within each length.
|
// Sort symbols by length, by symbol order within each length.
|
||||||
for (symbol = 0; symbol < code_lengths_size; ++symbol) {
|
for (symbol = 0; symbol < code_lengths_size; ++symbol) {
|
||||||
const int symbol_code_length = code_lengths[symbol];
|
const int symbol_code_length = code_lengths[symbol];
|
||||||
if (code_lengths[symbol] > 0) {
|
if (code_lengths[symbol] > 0) {
|
||||||
sorted[offset[symbol_code_length]++] = symbol;
|
if (sorted != NULL) {
|
||||||
|
sorted[offset[symbol_code_length]++] = symbol;
|
||||||
|
} else {
|
||||||
|
offset[symbol_code_length]++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Special case code with only one value.
|
// Special case code with only one value.
|
||||||
if (offset[MAX_ALLOWED_CODE_LENGTH] == 1) {
|
if (offset[MAX_ALLOWED_CODE_LENGTH] == 1) {
|
||||||
HuffmanCode code;
|
if (sorted != NULL) {
|
||||||
code.bits = 0;
|
HuffmanCode code;
|
||||||
code.value = (uint16_t)sorted[0];
|
code.bits = 0;
|
||||||
ReplicateValue(table, 1, total_size, code);
|
code.value = (uint16_t)sorted[0];
|
||||||
WebPSafeFree(sorted);
|
ReplicateValue(table, 1, total_size, code);
|
||||||
|
}
|
||||||
return total_size;
|
return total_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,9 +156,9 @@ int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
|
|||||||
num_nodes += num_open;
|
num_nodes += num_open;
|
||||||
num_open -= count[len];
|
num_open -= count[len];
|
||||||
if (num_open < 0) {
|
if (num_open < 0) {
|
||||||
WebPSafeFree(sorted);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
if (root_table == NULL) continue;
|
||||||
for (; count[len] > 0; --count[len]) {
|
for (; count[len] > 0; --count[len]) {
|
||||||
HuffmanCode code;
|
HuffmanCode code;
|
||||||
code.bits = (uint8_t)len;
|
code.bits = (uint8_t)len;
|
||||||
@ -172,34 +175,122 @@ int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
|
|||||||
num_nodes += num_open;
|
num_nodes += num_open;
|
||||||
num_open -= count[len];
|
num_open -= count[len];
|
||||||
if (num_open < 0) {
|
if (num_open < 0) {
|
||||||
WebPSafeFree(sorted);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
for (; count[len] > 0; --count[len]) {
|
for (; count[len] > 0; --count[len]) {
|
||||||
HuffmanCode code;
|
HuffmanCode code;
|
||||||
if ((key & mask) != low) {
|
if ((key & mask) != low) {
|
||||||
table += table_size;
|
if (root_table != NULL) table += table_size;
|
||||||
table_bits = NextTableBitSize(count, len, root_bits);
|
table_bits = NextTableBitSize(count, len, root_bits);
|
||||||
table_size = 1 << table_bits;
|
table_size = 1 << table_bits;
|
||||||
total_size += table_size;
|
total_size += table_size;
|
||||||
low = key & mask;
|
low = key & mask;
|
||||||
root_table[low].bits = (uint8_t)(table_bits + root_bits);
|
if (root_table != NULL) {
|
||||||
root_table[low].value = (uint16_t)((table - root_table) - low);
|
root_table[low].bits = (uint8_t)(table_bits + root_bits);
|
||||||
|
root_table[low].value = (uint16_t)((table - root_table) - low);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (root_table != NULL) {
|
||||||
|
code.bits = (uint8_t)(len - root_bits);
|
||||||
|
code.value = (uint16_t)sorted[symbol++];
|
||||||
|
ReplicateValue(&table[key >> root_bits], step, table_size, code);
|
||||||
}
|
}
|
||||||
code.bits = (uint8_t)(len - root_bits);
|
|
||||||
code.value = (uint16_t)sorted[symbol++];
|
|
||||||
ReplicateValue(&table[key >> root_bits], step, table_size, code);
|
|
||||||
key = GetNextKey(key, len);
|
key = GetNextKey(key, len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if tree is full.
|
// Check if tree is full.
|
||||||
if (num_nodes != 2 * offset[MAX_ALLOWED_CODE_LENGTH] - 1) {
|
if (num_nodes != 2 * offset[MAX_ALLOWED_CODE_LENGTH] - 1) {
|
||||||
WebPSafeFree(sorted);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WebPSafeFree(sorted);
|
|
||||||
return total_size;
|
return total_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Maximum code_lengths_size is 2328 (reached for 11-bit color_cache_bits).
|
||||||
|
// More commonly, the value is around ~280.
|
||||||
|
#define MAX_CODE_LENGTHS_SIZE \
|
||||||
|
((1 << MAX_CACHE_BITS) + NUM_LITERAL_CODES + NUM_LENGTH_CODES)
|
||||||
|
// Cut-off value for switching between heap and stack allocation.
|
||||||
|
#define SORTED_SIZE_CUTOFF 512
|
||||||
|
int VP8LBuildHuffmanTable(HuffmanTables* const root_table, int root_bits,
|
||||||
|
const int code_lengths[], int code_lengths_size) {
|
||||||
|
const int total_size =
|
||||||
|
BuildHuffmanTable(NULL, root_bits, code_lengths, code_lengths_size, NULL);
|
||||||
|
assert(code_lengths_size <= MAX_CODE_LENGTHS_SIZE);
|
||||||
|
if (total_size == 0 || root_table == NULL) return total_size;
|
||||||
|
|
||||||
|
if (root_table->curr_segment->curr_table + total_size >=
|
||||||
|
root_table->curr_segment->start + root_table->curr_segment->size) {
|
||||||
|
// If 'root_table' does not have enough memory, allocate a new segment.
|
||||||
|
// The available part of root_table->curr_segment is left unused because we
|
||||||
|
// need a contiguous buffer.
|
||||||
|
const int segment_size = root_table->curr_segment->size;
|
||||||
|
struct HuffmanTablesSegment* next =
|
||||||
|
(HuffmanTablesSegment*)WebPSafeMalloc(1, sizeof(*next));
|
||||||
|
if (next == NULL) return 0;
|
||||||
|
// Fill the new segment.
|
||||||
|
// We need at least 'total_size' but if that value is small, it is better to
|
||||||
|
// allocate a big chunk to prevent more allocations later. 'segment_size' is
|
||||||
|
// therefore chosen (any other arbitrary value could be chosen).
|
||||||
|
next->size = total_size > segment_size ? total_size : segment_size;
|
||||||
|
next->start =
|
||||||
|
(HuffmanCode*)WebPSafeMalloc(next->size, sizeof(*next->start));
|
||||||
|
if (next->start == NULL) {
|
||||||
|
WebPSafeFree(next);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
next->curr_table = next->start;
|
||||||
|
next->next = NULL;
|
||||||
|
// Point to the new segment.
|
||||||
|
root_table->curr_segment->next = next;
|
||||||
|
root_table->curr_segment = next;
|
||||||
|
}
|
||||||
|
if (code_lengths_size <= SORTED_SIZE_CUTOFF) {
|
||||||
|
// use local stack-allocated array.
|
||||||
|
uint16_t sorted[SORTED_SIZE_CUTOFF];
|
||||||
|
BuildHuffmanTable(root_table->curr_segment->curr_table, root_bits,
|
||||||
|
code_lengths, code_lengths_size, sorted);
|
||||||
|
} else { // rare case. Use heap allocation.
|
||||||
|
uint16_t* const sorted =
|
||||||
|
(uint16_t*)WebPSafeMalloc(code_lengths_size, sizeof(*sorted));
|
||||||
|
if (sorted == NULL) return 0;
|
||||||
|
BuildHuffmanTable(root_table->curr_segment->curr_table, root_bits,
|
||||||
|
code_lengths, code_lengths_size, sorted);
|
||||||
|
WebPSafeFree(sorted);
|
||||||
|
}
|
||||||
|
return total_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
int VP8LHuffmanTablesAllocate(int size, HuffmanTables* huffman_tables) {
|
||||||
|
// Have 'segment' point to the first segment for now, 'root'.
|
||||||
|
HuffmanTablesSegment* const root = &huffman_tables->root;
|
||||||
|
huffman_tables->curr_segment = root;
|
||||||
|
// Allocate root.
|
||||||
|
root->start = (HuffmanCode*)WebPSafeMalloc(size, sizeof(*root->start));
|
||||||
|
if (root->start == NULL) return 0;
|
||||||
|
root->curr_table = root->start;
|
||||||
|
root->next = NULL;
|
||||||
|
root->size = size;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void VP8LHuffmanTablesDeallocate(HuffmanTables* const huffman_tables) {
|
||||||
|
HuffmanTablesSegment *current, *next;
|
||||||
|
if (huffman_tables == NULL) return;
|
||||||
|
// Free the root node.
|
||||||
|
current = &huffman_tables->root;
|
||||||
|
next = current->next;
|
||||||
|
WebPSafeFree(current->start);
|
||||||
|
current->start = NULL;
|
||||||
|
current->next = NULL;
|
||||||
|
current = next;
|
||||||
|
// Free the following nodes.
|
||||||
|
while (current != NULL) {
|
||||||
|
next = current->next;
|
||||||
|
WebPSafeFree(current->start);
|
||||||
|
WebPSafeFree(current);
|
||||||
|
current = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -43,6 +43,29 @@ typedef struct {
|
|||||||
// or non-literal symbol otherwise
|
// or non-literal symbol otherwise
|
||||||
} HuffmanCode32;
|
} HuffmanCode32;
|
||||||
|
|
||||||
|
// Contiguous memory segment of HuffmanCodes.
|
||||||
|
typedef struct HuffmanTablesSegment {
|
||||||
|
HuffmanCode* start;
|
||||||
|
// Pointer to where we are writing into the segment. Starts at 'start' and
|
||||||
|
// cannot go beyond 'start' + 'size'.
|
||||||
|
HuffmanCode* curr_table;
|
||||||
|
// Pointer to the next segment in the chain.
|
||||||
|
struct HuffmanTablesSegment* next;
|
||||||
|
int size;
|
||||||
|
} HuffmanTablesSegment;
|
||||||
|
|
||||||
|
// Chained memory segments of HuffmanCodes.
|
||||||
|
typedef struct HuffmanTables {
|
||||||
|
HuffmanTablesSegment root;
|
||||||
|
// Currently processed segment. At first, this is 'root'.
|
||||||
|
HuffmanTablesSegment* curr_segment;
|
||||||
|
} HuffmanTables;
|
||||||
|
|
||||||
|
// Allocates a HuffmanTables with 'size' contiguous HuffmanCodes. Returns 0 on
|
||||||
|
// memory allocation error, 1 otherwise.
|
||||||
|
int VP8LHuffmanTablesAllocate(int size, HuffmanTables* huffman_tables);
|
||||||
|
void VP8LHuffmanTablesDeallocate(HuffmanTables* const huffman_tables);
|
||||||
|
|
||||||
#define HUFFMAN_PACKED_BITS 6
|
#define HUFFMAN_PACKED_BITS 6
|
||||||
#define HUFFMAN_PACKED_TABLE_SIZE (1u << HUFFMAN_PACKED_BITS)
|
#define HUFFMAN_PACKED_TABLE_SIZE (1u << HUFFMAN_PACKED_BITS)
|
||||||
|
|
||||||
@ -78,7 +101,7 @@ void VP8LHtreeGroupsFree(HTreeGroup* const htree_groups);
|
|||||||
// the huffman table.
|
// the huffman table.
|
||||||
// Returns built table size or 0 in case of error (invalid tree or
|
// Returns built table size or 0 in case of error (invalid tree or
|
||||||
// memory error).
|
// memory error).
|
||||||
int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
|
int VP8LBuildHuffmanTable(HuffmanTables* const root_table, int root_bits,
|
||||||
const int code_lengths[], int code_lengths_size);
|
const int code_lengths[], int code_lengths_size);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
Reference in New Issue
Block a user