From cf5eb8ad19eb5d558b980a648c77bf3056ad7434 Mon Sep 17 00:00:00 2001 From: Pascal Massimino Date: Tue, 29 Apr 2014 03:32:39 -0700 Subject: [PATCH] remove some uint64_t casts and use. We use automatic int->uint64_t promotion where applicable. (uint64_t should be kept only for overflow checking and memory alloc). Change-Id: I1f41b0f73e2e6380e7d65cc15c1f730696862125 --- src/enc/analysis.c | 2 +- src/enc/backward_references.c | 9 ++++----- src/enc/histogram.c | 24 ++++++++++++------------ src/enc/histogram.h | 2 +- src/enc/vp8l.c | 28 +++++++++++++--------------- 5 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/enc/analysis.c b/src/enc/analysis.c index 1a5021db..919a74a9 100644 --- a/src/enc/analysis.c +++ b/src/enc/analysis.c @@ -30,7 +30,7 @@ static void SmoothSegmentMap(VP8Encoder* const enc) { const int w = enc->mb_w_; const int h = enc->mb_h_; const int majority_cnt_3_x_3_grid = 5; - uint8_t* const tmp = (uint8_t*)WebPSafeMalloc((uint64_t)w * h, sizeof(*tmp)); + uint8_t* const tmp = (uint8_t*)WebPSafeMalloc(w * h, sizeof(*tmp)); assert((uint64_t)(w * h) == (uint64_t)w * h); // no overflow, as per spec if (tmp == NULL) return; diff --git a/src/enc/backward_references.c b/src/enc/backward_references.c index dd00fff9..b4b77e9e 100644 --- a/src/enc/backward_references.c +++ b/src/enc/backward_references.c @@ -89,8 +89,7 @@ VP8LBackwardRefs* VP8LBackwardRefsNew(int max_size) { } ClearBackwardRefs(refs); refs->max_size = 0; - refs->refs = (PixOrCopy*)WebPSafeMalloc((uint64_t)max_size, - sizeof(*refs->refs)); + refs->refs = (PixOrCopy*)WebPSafeMalloc(max_size, sizeof(*refs->refs)); if (refs->refs == NULL) { WebPSafeFree(refs); return NULL; @@ -135,7 +134,7 @@ VP8LHashChain* VP8LHashChainNew(int size) { if (p == NULL) { return NULL; } - p->chain_ = (int*)WebPSafeMalloc((uint64_t)size, sizeof(*p->chain_)); + p->chain_ = (int*)WebPSafeMalloc(size, sizeof(*p->chain_)); if (p->chain_ == NULL) { WebPSafeFree(p); return NULL; @@ -505,7 +504,7 @@ static int BackwardReferencesHashChainDistanceOnly( const int pix_count = xsize * ysize; const int use_color_cache = (cache_bits > 0); float* const cost = - (float*)WebPSafeMalloc((uint64_t)pix_count, sizeof(*cost)); + (float*)WebPSafeMalloc(pix_count, sizeof(*cost)); CostModel* cost_model = (CostModel*)WebPSafeMalloc(1ULL, sizeof(*cost_model)); VP8LColorCache hashers; const double mul0 = (recursive_cost_model != 0) ? 1.0 : 0.68; @@ -721,7 +720,7 @@ static int BackwardReferencesTraceBackwards(int xsize, int ysize, uint32_t* chosen_path = NULL; int chosen_path_size = 0; uint32_t* dist_array = - (uint32_t*)WebPSafeMalloc((uint64_t)dist_array_size, sizeof(*dist_array)); + (uint32_t*)WebPSafeMalloc(dist_array_size, sizeof(*dist_array)); if (dist_array == NULL) goto Error; diff --git a/src/enc/histogram.c b/src/enc/histogram.c index 511b71c4..c80a14c6 100644 --- a/src/enc/histogram.c +++ b/src/enc/histogram.c @@ -31,7 +31,7 @@ static void HistogramClear(VP8LHistogram* const p) { uint32_t* const literal = p->literal_; const int cache_bits = p->palette_code_bits_; - const uint64_t histo_size = VP8LGetHistogramSize(cache_bits); + const int histo_size = VP8LGetHistogramSize(cache_bits); memset(p, 0, histo_size); p->palette_code_bits_ = cache_bits; p->literal_ = literal; @@ -41,17 +41,17 @@ static void HistogramCopy(const VP8LHistogram* const src, VP8LHistogram* const dst) { uint32_t* const dst_literal = dst->literal_; const int dst_cache_bits = dst->palette_code_bits_; - const uint64_t histo_size = VP8LGetHistogramSize(dst_cache_bits); + const int histo_size = VP8LGetHistogramSize(dst_cache_bits); assert(src->palette_code_bits_ == dst_cache_bits); memcpy(dst, src, histo_size); dst->literal_ = dst_literal; } -uint64_t VP8LGetHistogramSize(int cache_bits) { - const uint64_t literal_size = VP8LHistogramNumCodes(cache_bits); - const uint64_t total_size = (uint64_t)sizeof(VP8LHistogram) - + literal_size * sizeof(int); - return total_size; +int VP8LGetHistogramSize(int cache_bits) { + const int literal_size = VP8LHistogramNumCodes(cache_bits); + const size_t total_size = sizeof(VP8LHistogram) + sizeof(int) * literal_size; + assert(total_size <= (size_t)0x7fffffff); + return (int)total_size; } void VP8LFreeHistogram(VP8LHistogram* const histo) { @@ -87,7 +87,7 @@ void VP8LHistogramInit(VP8LHistogram* const p, int palette_code_bits) { VP8LHistogram* VP8LAllocateHistogram(int cache_bits) { VP8LHistogram* histo = NULL; - const uint64_t total_size = VP8LGetHistogramSize(cache_bits); + const int total_size = VP8LGetHistogramSize(cache_bits); uint8_t* const memory = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*memory)); if (memory == NULL) return NULL; histo = (VP8LHistogram*)memory; @@ -100,9 +100,9 @@ VP8LHistogram* VP8LAllocateHistogram(int cache_bits) { VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits) { int i; VP8LHistogramSet* set; - const uint64_t total_size = sizeof(*set) - + (uint64_t)size * sizeof(*set->histograms) - + (uint64_t)size * VP8LGetHistogramSize(cache_bits); + const size_t total_size = sizeof(*set) + + sizeof(*set->histograms) * size + + (size_t)VP8LGetHistogramSize(cache_bits) * size; uint8_t* memory = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*memory)); if (memory == NULL) return NULL; @@ -710,7 +710,7 @@ int VP8LGetHistoImageSymbols(int xsize, int ysize, // Higher qualities (> 90), to preserve the compression gains at those // quality settings. if (init_histo->size > 2 * BIN_SIZE && quality < 90) { - const int bin_map_size = (uint64_t)bin_depth * BIN_SIZE; + const int bin_map_size = bin_depth * BIN_SIZE; bin_map = (int16_t*)WebPSafeCalloc(bin_map_size, sizeof(*bin_map)); if (bin_map == NULL) goto Error; } diff --git a/src/enc/histogram.h b/src/enc/histogram.h index 9a4bb0ec..1cf4c547 100644 --- a/src/enc/histogram.h +++ b/src/enc/histogram.h @@ -63,7 +63,7 @@ void VP8LHistogramCreate(VP8LHistogram* const p, int palette_code_bits); // Return the size of the histogram for a given palette_code_bits. -uint64_t VP8LGetHistogramSize(int palette_code_bits); +int VP8LGetHistogramSize(int palette_code_bits); // Set the palette_code_bits and reset the stats. void VP8LHistogramInit(VP8LHistogram* const p, int palette_code_bits); diff --git a/src/enc/vp8l.c b/src/enc/vp8l.c index 76a771d0..18f0c7a9 100644 --- a/src/enc/vp8l.c +++ b/src/enc/vp8l.c @@ -516,8 +516,7 @@ static int EncodeImageNoHuffman(VP8LBitWriter* const bw, } } - tokens = (HuffmanTreeToken*)WebPSafeMalloc((uint64_t)max_tokens, - sizeof(*tokens)); + tokens = (HuffmanTreeToken*)WebPSafeMalloc(max_tokens, sizeof(*tokens)); if (tokens == NULL) goto Error; // Store Huffman codes. @@ -561,7 +560,7 @@ static int EncodeImageInternal(VP8LBitWriter* const bw, VP8LBackwardRefs* refs = NULL; VP8LBackwardRefs* best_refs; uint16_t* const histogram_symbols = - (uint16_t*)WebPSafeMalloc((uint64_t)histogram_image_xysize, + (uint16_t*)WebPSafeMalloc(histogram_image_xysize, sizeof(*histogram_symbols)); assert(histogram_bits >= MIN_HUFFMAN_BITS); assert(histogram_bits <= MAX_HUFFMAN_BITS); @@ -618,7 +617,7 @@ static int EncodeImageInternal(VP8LBitWriter* const bw, VP8LWriteBits(bw, 1, write_histogram_image); if (write_histogram_image) { uint32_t* const histogram_argb = - (uint32_t*)WebPSafeMalloc((uint64_t)histogram_image_xysize, + (uint32_t*)WebPSafeMalloc(histogram_image_xysize, sizeof(*histogram_argb)); int max_index = 0; uint32_t i; @@ -656,7 +655,7 @@ static int EncodeImageInternal(VP8LBitWriter* const bw, max_tokens = codes->num_symbols; } } - tokens = (HuffmanTreeToken*)WebPSafeMalloc((uint64_t)max_tokens, + tokens = (HuffmanTreeToken*)WebPSafeMalloc(max_tokens, sizeof(*tokens)); if (tokens == NULL) goto Error; for (i = 0; i < 5 * histogram_image_size; ++i) { @@ -844,11 +843,11 @@ static WebPEncodingError AllocateTransformBuffer(VP8LEncoder* const enc, const int tile_size = 1 << enc->transform_bits_; const uint64_t image_size = width * height; const uint64_t argb_scratch_size = tile_size * width + width; - const uint64_t transform_data_size = - (uint64_t)VP8LSubSampleSize(width, enc->transform_bits_) * - (uint64_t)VP8LSubSampleSize(height, enc->transform_bits_); + const int transform_data_size = + VP8LSubSampleSize(width, enc->transform_bits_) * + VP8LSubSampleSize(height, enc->transform_bits_); const uint64_t total_size = - image_size + argb_scratch_size + transform_data_size; + image_size + argb_scratch_size + (uint64_t)transform_data_size; uint32_t* mem = (uint32_t*)WebPSafeMalloc(total_size, sizeof(*mem)); if (mem == NULL) { err = VP8_ENC_ERROR_OUT_OF_MEMORY; @@ -947,7 +946,7 @@ static WebPEncodingError EncodePalette(VP8LBitWriter* const bw, if (err != VP8_ENC_OK) goto Error; dst = enc->argb_; - row = (uint8_t*)WebPSafeMalloc((uint64_t)width, sizeof(*row)); + row = (uint8_t*)WebPSafeMalloc(width, sizeof(*row)); if (row == NULL) return VP8_ENC_ERROR_OUT_OF_MEMORY; ApplyPalette(src, dst, pic->argb_stride, enc->current_width_, @@ -975,14 +974,13 @@ static WebPEncodingError EncodePalette(VP8LBitWriter* const bw, // ----------------------------------------------------------------------------- static int GetHistoBits(int method, int use_palette, int width, int height) { - const uint64_t hist_size = VP8LGetHistogramSize(MAX_COLOR_CACHE_BITS); + const int hist_size = VP8LGetHistogramSize(MAX_COLOR_CACHE_BITS); // Make tile size a function of encoding method (Range: 0 to 6). int histo_bits = (use_palette ? 9 : 7) - method; while (1) { - const uint64_t huff_image_size = VP8LSubSampleSize(width, histo_bits) * - VP8LSubSampleSize(height, histo_bits) * - hist_size; - if (huff_image_size <= MAX_HUFF_IMAGE_SIZE) break; + const int huff_image_size = VP8LSubSampleSize(width, histo_bits) * + VP8LSubSampleSize(height, histo_bits); + if ((uint64_t)huff_image_size * hist_size <= MAX_HUFF_IMAGE_SIZE) break; ++histo_bits; } return (histo_bits < MIN_HUFFMAN_BITS) ? MIN_HUFFMAN_BITS :