GetHuffBitLengthsAndCodes: reduce level of indirection

arrays can be passed directly as only their members are being modified.
this also reduces the allocation for bit_codes[] by taking the
sizeof(type)=2 rather than sizeof(ptr)=4/8 in one case.

Change-Id: Idad20cead58c218b58d90b71699374fefd01cad9
This commit is contained in:
James Zern 2012-05-07 19:15:10 -07:00
parent b5f2a9ed49
commit e3302cfd73

View File

@ -347,8 +347,8 @@ static int OptimizeHuffmanForRle(int length, int* counts) {
// TODO(vikasa): Wrap bit_codes and bit_lengths in a Struct. // TODO(vikasa): Wrap bit_codes and bit_lengths in a Struct.
static int GetHuffBitLengthsAndCodes( static int GetHuffBitLengthsAndCodes(
const VP8LHistogramSet* const histogram_image, const VP8LHistogramSet* const histogram_image,
int use_color_cache, int** bit_length_sizes, int use_color_cache, int* const bit_length_sizes,
uint16_t*** bit_codes, uint8_t*** bit_lengths) { uint16_t** const bit_codes, uint8_t** const bit_lengths) {
int i, k; int i, k;
int ok = 1; int ok = 1;
const int histogram_image_size = histogram_image->size; const int histogram_image_size = histogram_image->size;
@ -357,11 +357,11 @@ static int GetHuffBitLengthsAndCodes(
const int num_literals = VP8LHistogramNumCodes(histo); const int num_literals = VP8LHistogramNumCodes(histo);
k = 0; k = 0;
// TODO(vikasa): Alloc one big buffer instead of allocating in the loop. // TODO(vikasa): Alloc one big buffer instead of allocating in the loop.
(*bit_length_sizes)[5 * i] = num_literals; bit_length_sizes[5 * i] = num_literals;
(*bit_lengths)[5 * i] = (uint8_t*)calloc(num_literals, 1); bit_lengths[5 * i] = (uint8_t*)calloc(num_literals, 1);
(*bit_codes)[5 * i] = (uint16_t*) bit_codes[5 * i] = (uint16_t*)
malloc(num_literals * sizeof(*(*bit_codes)[5 * i])); malloc(num_literals * sizeof(*bit_codes[5 * i]));
if ((*bit_lengths)[5 * i] == NULL || (*bit_codes)[5 * i] == NULL) { if (bit_lengths[5 * i] == NULL || bit_codes[5 * i] == NULL) {
ok = 0; ok = 0;
goto Error; goto Error;
} }
@ -382,35 +382,32 @@ static int GetHuffBitLengthsAndCodes(
// Create a Huffman tree (in the form of bit lengths) for each component. // Create a Huffman tree (in the form of bit lengths) for each component.
ok = ok && VP8LCreateHuffmanTree(histo->literal_, num_literals, ok = ok && VP8LCreateHuffmanTree(histo->literal_, num_literals,
15, (*bit_lengths)[5 * i]); 15, bit_lengths[5 * i]);
for (k = 1; k < 5; ++k) { for (k = 1; k < 5; ++k) {
int val = 256; const int val = (k == 4) ? DISTANCE_CODES_MAX : 256;
if (k == 4) { bit_length_sizes[5 * i + k] = val;
val = DISTANCE_CODES_MAX; bit_lengths[5 * i + k] = (uint8_t*)calloc(val, 1);
} bit_codes[5 * i + k] = (uint16_t*)calloc(val, sizeof(*bit_codes[0]));
(*bit_length_sizes)[5 * i + k] = val; if (bit_lengths[5 * i + k] == NULL ||
(*bit_lengths)[5 * i + k] = (uint8_t*)calloc(val, 1); bit_codes[5 * i + k] == NULL) {
(*bit_codes)[5 * i + k] = (uint16_t*)calloc(val, sizeof(bit_codes[0]));
if ((*bit_lengths)[5 * i + k] == NULL ||
(*bit_codes)[5 * i + k] == NULL) {
ok = 0; ok = 0;
goto Error; goto Error;
} }
} }
ok = ok && ok = ok &&
VP8LCreateHuffmanTree(histo->red_, 256, 15, VP8LCreateHuffmanTree(histo->red_, 256, 15,
(*bit_lengths)[5 * i + 1]) && bit_lengths[5 * i + 1]) &&
VP8LCreateHuffmanTree(histo->blue_, 256, 15, VP8LCreateHuffmanTree(histo->blue_, 256, 15,
(*bit_lengths)[5 * i + 2]) && bit_lengths[5 * i + 2]) &&
VP8LCreateHuffmanTree(histo->alpha_, 256, 15, VP8LCreateHuffmanTree(histo->alpha_, 256, 15,
(*bit_lengths)[5 * i + 3]) && bit_lengths[5 * i + 3]) &&
VP8LCreateHuffmanTree(histo->distance_, DISTANCE_CODES_MAX, 15, VP8LCreateHuffmanTree(histo->distance_, DISTANCE_CODES_MAX, 15,
(*bit_lengths)[5 * i + 4]); bit_lengths[5 * i + 4]);
// Create the actual bit codes for the bit lengths. // Create the actual bit codes for the bit lengths.
for (k = 0; k < 5; ++k) { for (k = 0; k < 5; ++k) {
int ix = 5 * i + k; int ix = 5 * i + k;
VP8LConvertBitDepthsToSymbols((*bit_lengths)[ix], (*bit_length_sizes)[ix], VP8LConvertBitDepthsToSymbols(bit_lengths[ix], bit_length_sizes[ix],
(*bit_codes)[ix]); bit_codes[ix]);
} }
} }
return ok; return ok;
@ -419,8 +416,8 @@ static int GetHuffBitLengthsAndCodes(
{ {
int idx; int idx;
for (idx = 0; idx <= 5 * i + k; ++idx) { for (idx = 0; idx <= 5 * i + k; ++idx) {
free((*bit_lengths)[idx]); free(bit_lengths[idx]);
free((*bit_codes)[idx]); free(bit_codes[idx]);
} }
} }
return 0; return 0;
@ -709,8 +706,7 @@ static int EncodeImageInternal(VP8LBitWriter* const bw,
sizeof(*bit_codes)); sizeof(*bit_codes));
if (bit_lengths_sizes == NULL || bit_lengths == NULL || bit_codes == NULL || if (bit_lengths_sizes == NULL || bit_lengths == NULL || bit_codes == NULL ||
!GetHuffBitLengthsAndCodes(histogram_image, use_color_cache, !GetHuffBitLengthsAndCodes(histogram_image, use_color_cache,
&bit_lengths_sizes, bit_lengths_sizes, bit_codes, bit_lengths)) {
&bit_codes, &bit_lengths)) {
goto Error; goto Error;
} }