mirror of
https://github.com/webmproject/libwebp.git
synced 2024-11-20 04:18:26 +01:00
Simple Huffman code changes.
No empty trees are codified with the simple Huffman code. The simple Huffman code is simplified to be either a 1-bit code or 8-bit code for symbols. Change-Id: I3e2813027b5a643862729339303d80197c497aff
This commit is contained in:
parent
a2849bc502
commit
781c01f421
@ -238,25 +238,17 @@ static int ReadHuffmanCode(int alphabet_size, VP8LDecoder* const dec,
|
||||
int symbols[2];
|
||||
int codes[2];
|
||||
int code_lengths[2];
|
||||
const int nbits = VP8LReadBits(br, 3);
|
||||
const int num_symbols = 1 + ((nbits == 0) ? 0 : VP8LReadBits(br, 1));
|
||||
|
||||
if (nbits == 0) {
|
||||
symbols[0] = 0;
|
||||
const int num_symbols = VP8LReadBits(br, 1) + 1;
|
||||
const int first_symbol_len_code = VP8LReadBits(br, 1);
|
||||
// The first code is either 1 bit or 8 bit code.
|
||||
symbols[0] = VP8LReadBits(br, (first_symbol_len_code == 0) ? 1 : 8);
|
||||
codes[0] = 0;
|
||||
code_lengths[0] = 0;
|
||||
} else {
|
||||
const int num_bits = (nbits - 1) * 2 + 4;
|
||||
int i;
|
||||
for (i = 0; i < num_symbols; ++i) {
|
||||
symbols[i] = VP8LReadBits(br, num_bits);
|
||||
if (symbols[i] >= alphabet_size) {
|
||||
dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
|
||||
return 0;
|
||||
}
|
||||
codes[i] = i;
|
||||
code_lengths[i] = num_symbols - 1;
|
||||
}
|
||||
code_lengths[0] = num_symbols - 1;
|
||||
// The second code (if present), is always 8 bit long.
|
||||
if (num_symbols == 2) {
|
||||
symbols[1] = VP8LReadBits(br, 8);
|
||||
codes[1] = 1;
|
||||
code_lengths[1] = num_symbols - 1;
|
||||
}
|
||||
ok = HuffmanTreeBuildExplicit(tree, code_lengths, codes,
|
||||
symbols, num_symbols);
|
||||
|
@ -531,20 +531,19 @@ static int StoreHuffmanCode(VP8LBitWriter* const bw,
|
||||
++count;
|
||||
}
|
||||
}
|
||||
if (count <= 2) {
|
||||
int num_bits = 4;
|
||||
// 0, 1 or 2 symbols to encode.
|
||||
VP8LWriteBits(bw, 1, 1);
|
||||
if (count == 0) {
|
||||
VP8LWriteBits(bw, 3, 0);
|
||||
ok = 1;
|
||||
goto End;
|
||||
}
|
||||
while (symbols[count - 1] >= (1 << num_bits)) num_bits += 2;
|
||||
VP8LWriteBits(bw, 3, (num_bits - 4) / 2 + 1);
|
||||
if (count == 0) count = 1;
|
||||
if (count <= 2 && symbols[0] < 256 && symbols[1] < 256) {
|
||||
VP8LWriteBits(bw, 1, 1); // Small tree marker to encode 1 or 2 symbols.
|
||||
VP8LWriteBits(bw, 1, count - 1);
|
||||
for (i = 0; i < count; ++i) {
|
||||
VP8LWriteBits(bw, num_bits, symbols[i]);
|
||||
if (symbols[0] <= 1) {
|
||||
VP8LWriteBits(bw, 1, 0); // Code bit for small (1 bit) symbol value.
|
||||
VP8LWriteBits(bw, 1, symbols[0]);
|
||||
} else {
|
||||
VP8LWriteBits(bw, 1, 1);
|
||||
VP8LWriteBits(bw, 8, symbols[0]);
|
||||
}
|
||||
if (count == 2) {
|
||||
VP8LWriteBits(bw, 8, symbols[1]);
|
||||
}
|
||||
ok = 1;
|
||||
goto End;
|
||||
|
Loading…
Reference in New Issue
Block a user