From 352a4f49aba02a5e31bc812c91db6d6dc71df482 Mon Sep 17 00:00:00 2001 From: Urvang Joshi Date: Fri, 13 Apr 2012 07:14:16 +0000 Subject: [PATCH] Get rid of PackLiteralBitLengths() [and in turn a malloc]. Also, a few related const fixes. Change-Id: I229519b1c34d41c78d9ad2403f1e25feab3c9d93 --- src/enc/vp8l.c | 49 +++++++------------------------------- src/utils/huffman_encode.c | 2 +- src/utils/huffman_encode.h | 5 ++-- 3 files changed, 12 insertions(+), 44 deletions(-) diff --git a/src/enc/vp8l.c b/src/enc/vp8l.c index 2b002722..762d0456 100644 --- a/src/enc/vp8l.c +++ b/src/enc/vp8l.c @@ -507,31 +507,6 @@ static void ShiftHistogramImage(uint32_t* image , int image_size) { } } -static int PackLiteralBitLengths(const uint8_t* bit_lengths, - int cache_bits, int use_color_cache, - int* new_length_size, - uint8_t** new_lengths) { - int i; - int num_codes = 256; - const int cache_size = 1 << cache_bits; - *new_length_size = 256 + kLengthCodes; - if (use_color_cache) { - *new_length_size += cache_size; - } - *new_lengths = (uint8_t*)malloc(*new_length_size); - if (*new_lengths == NULL) { - return 0; - } - num_codes += kLengthCodes; - if (use_color_cache) { - num_codes += cache_size; - } - for (i = 0; i < num_codes; ++i) { - (*new_lengths)[i] = bit_lengths[i]; - } - return 1; -} - static void ClearHuffmanTreeIfOnlyOneSymbol(const int num_symbols, uint8_t* lengths, uint16_t* symbols) { @@ -597,7 +572,8 @@ static void StoreHuffmanTreeToBitMask( } static int StoreHuffmanCode(VP8LBitWriter* const bw, - uint8_t* bit_lengths, int bit_lengths_size) { + const uint8_t* const bit_lengths, + int bit_lengths_size) { int i; int ok = 0; int count = 0; @@ -844,24 +820,17 @@ static int EncodeImageInternal(VP8LBitWriter* const bw, // Store Huffman codes. for (i = 0; i < histogram_image_size; ++i) { int k; - int literal_lengths_size; - uint8_t* literal_lengths; - // TODO(vikasa): Evaluate and remove the call to PackLiteralBitLengths. - if (!PackLiteralBitLengths(bit_lengths[5 * i], cache_bits, use_color_cache, - &literal_lengths_size, &literal_lengths)) { - goto Error; - } - if (!StoreHuffmanCode(bw, literal_lengths, literal_lengths_size)) { - goto Error; - } - free(literal_lengths); - for (k = 1; k < 5; ++k) { - if (!StoreHuffmanCode(bw, bit_lengths[5 * i + k], - bit_lengths_sizes[5 * i + k])) { + for (k = 0; k < 5; ++k) { + const uint8_t* const cur_bit_lengths = bit_lengths[5 * i + k]; + const int cur_bit_lengths_size = (k == 0) ? + 256 + kLengthCodes + (1 << cache_bits) : + bit_lengths_sizes[5 * i + k]; + if (!StoreHuffmanCode(bw, cur_bit_lengths, cur_bit_lengths_size)) { goto Error; } } } + // Free combined histograms. DeleteHistograms(histogram_image_size, histogram_image); diff --git a/src/utils/huffman_encode.c b/src/utils/huffman_encode.c index 8152898f..7d88187f 100644 --- a/src/utils/huffman_encode.c +++ b/src/utils/huffman_encode.c @@ -241,7 +241,7 @@ static void WriteHuffmanTreeRepetitionsZeros( } } -void VP8LCreateCompressedHuffmanTree(const uint8_t* depth, +void VP8LCreateCompressedHuffmanTree(const uint8_t* const depth, int depth_size, int* num_symbols, uint8_t* tree, diff --git a/src/utils/huffman_encode.h b/src/utils/huffman_encode.h index 9bd717be..c931142d 100644 --- a/src/utils/huffman_encode.h +++ b/src/utils/huffman_encode.h @@ -34,10 +34,9 @@ extern "C" { int VP8LCreateHuffmanTree(const int* data, const int length, const int tree_limit, uint8_t* depth); -// Write a huffman tree from bit depths into the deflate representation -// of a Huffman tree. In deflate, the generated Huffman tree is to be +// Write a huffman tree from bit depths. The generated Huffman tree is // compressed once more using a Huffman tree. -void VP8LCreateCompressedHuffmanTree(const uint8_t* depth, int len, +void VP8LCreateCompressedHuffmanTree(const uint8_t* const depth, int len, int* num_symbols, uint8_t* tree, uint8_t* extra_bits_data);