From 781c01f42188093af1bd887805fd9b48b2e5dd87 Mon Sep 17 00:00:00 2001 From: Vikas Arora Date: Fri, 27 Apr 2012 05:51:13 +0000 Subject: [PATCH] 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 --- src/dec/vp8l.c | 30 +++++++++++------------------- src/enc/vp8l.c | 25 ++++++++++++------------- 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/src/dec/vp8l.c b/src/dec/vp8l.c index 5be21965..d73ffe27 100644 --- a/src/dec/vp8l.c +++ b/src/dec/vp8l.c @@ -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; - 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; - } + 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] = 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); diff --git a/src/enc/vp8l.c b/src/enc/vp8l.c index 50b20f36..c666d7ea 100644 --- a/src/enc/vp8l.c +++ b/src/enc/vp8l.c @@ -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;