From fdccaaddcfe1c55d48ef98a5b531387147c6f9a3 Mon Sep 17 00:00:00 2001 From: Vikas Arora Date: Mon, 2 Apr 2012 10:58:36 +0000 Subject: [PATCH] Fixing nits - Const Handling of picture object, removed bitwriter from encoder. Change-Id: Id943854de09324de81cca615ada960390c4b8152 --- src/enc/picture.c | 22 ++++++++++----- src/enc/vp8l.c | 61 +++++++++++++++++++++--------------------- src/enc/vp8li.h | 4 +-- src/enc/webpenc.c | 2 +- src/utils/bit_writer.h | 4 +-- src/webp/encode.h | 2 +- 6 files changed, 51 insertions(+), 44 deletions(-) diff --git a/src/enc/picture.c b/src/enc/picture.c index 9885d2be..75fc9038 100644 --- a/src/enc/picture.c +++ b/src/enc/picture.c @@ -105,13 +105,15 @@ int WebPPictureAlloc(WebPPicture* const picture) { mem += uv0_size; } } else { + const uint64_t argb_size = (uint64_t)width * height; + const uint64_t total_size = argb_size * sizeof(*picture->argb); if (width <= 0 || height <= 0 || - width >= 0x4000 || height >= 0x4000) { + argb_size >= (1ULL << 40) || + (size_t)total_size != total_size) { return 0; } WebPPictureFree(picture); // erase previous buffer - picture->argb = (uint32_t*)malloc(width * height * - sizeof(*picture->argb)); + picture->argb = (uint32_t*)malloc(total_size); if (picture->argb == NULL) return 0; picture->argb_stride = width; } @@ -526,8 +528,11 @@ static int Import(WebPPicture* const picture, for (y = 0; y < height; ++y) { for (x = 0; x < width; ++x) { const int offset = step * x + y * rgb_stride; - const uint32_t argb = 0xff000000 | (r_ptr[offset] << 16) | - (g_ptr[offset] << 8) | (b_ptr[offset]); + const uint32_t argb = + 0xff000000 | + (r_ptr[offset] << 16) | + (g_ptr[offset] << 8) | + (b_ptr[offset]); picture->argb[x + y * picture->argb_stride] = argb; } } @@ -537,8 +542,11 @@ static int Import(WebPPicture* const picture, for (y = 0; y < height; ++y) { for (x = 0; x < width; ++x) { const int offset = step * x + y * rgb_stride; - const uint32_t argb = (a_ptr[offset] << 24) | (r_ptr[offset] << 16) | - (g_ptr[offset] << 8) | (b_ptr[offset]); + const uint32_t argb = + (a_ptr[offset] << 24) | + (r_ptr[offset] << 16) | + (g_ptr[offset] << 8) | + (b_ptr[offset]); picture->argb[x + y * picture->argb_stride] = argb; } } diff --git a/src/enc/vp8l.c b/src/enc/vp8l.c index 228ee446..40157bc3 100644 --- a/src/enc/vp8l.c +++ b/src/enc/vp8l.c @@ -5,7 +5,7 @@ // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ // ----------------------------------------------------------------------------- // -// main entry for the decoder +// main entry for the lossless encoder. // // Author: Vikas Arora (vikaas.arora@gmail.com) // @@ -52,7 +52,7 @@ static int ApplyCrossColorFilter(VP8LEncoder* const enc) { return 1; } -static void EvalEmergingPalette(VP8LEncoder* const enc) { +static void EvalColorCache(VP8LEncoder* const enc) { (void)enc; } @@ -81,13 +81,14 @@ static WebPEncodingError WriteRiffHeader(VP8LEncoder* const enc, return VP8_ENC_OK; } -static WebPEncodingError WriteImage(VP8LEncoder* const enc) { +static WebPEncodingError WriteImage(VP8LEncoder* const enc, + VP8LBitWriter* const bw) { size_t riff_size, vp8l_size, webpll_size, pad; - WebPPicture* const pic = enc->pic_; + const WebPPicture* const pic = enc->pic_; WebPEncodingError err = VP8_ENC_OK; - const uint8_t* const webpll_data = VP8LBitWriterFinish(&enc->bw_); + const uint8_t* const webpll_data = VP8LBitWriterFinish(bw); - webpll_size = VP8LBitWriterNumBytes(&enc->bw_); + webpll_size = VP8LBitWriterNumBytes(bw); vp8l_size = SIGNATURE_SIZE + webpll_size; pad = vp8l_size & 1; vp8l_size += pad; @@ -111,14 +112,12 @@ static WebPEncodingError WriteImage(VP8LEncoder* const enc) { return VP8_ENC_OK; Error: - WebPEncodingSetError(pic, err); return err; } static VP8LEncoder* InitVP8LEncoder(const WebPConfig* const config, WebPPicture* const picture) { VP8LEncoder* enc; - const int kEstmatedEncodeSize = (picture->width * picture->height) >> 1; (void)config; enc = (VP8LEncoder*)malloc(sizeof(*enc)); @@ -133,23 +132,18 @@ static VP8LEncoder* InitVP8LEncoder(const WebPConfig* const config, // TODO: Use config.quality to initialize histo_bits_ and transform_bits_. enc->histo_bits_ = 4; enc->transform_bits_ = 4; - VP8LBitWriterInit(&enc->bw_, kEstmatedEncodeSize); return enc; } -static WebPEncodingError WriteImageSize(VP8LEncoder* const enc) { - WebPEncodingError status = VP8_ENC_OK; +static void WriteImageSize(VP8LEncoder* const enc, VP8LBitWriter* const bw) { WebPPicture* const pic = enc->pic_; const int width = pic->width - 1; - const int height = pic->height - 1; - if (width < 0x4000 && height < 0x4000) { - VP8LWriteBits(&enc->bw_, kImageSizeBits, width); - VP8LWriteBits(&enc->bw_, kImageSizeBits, height); - } else { - status = VP8_ENC_ERROR_BAD_DIMENSION; - } - return status; + const int height = pic->height -1; + assert(width < WEBP_MAX_DIMENSION && height < WEBP_MAX_DIMENSION); + + VP8LWriteBits(bw, kImageSizeBits, width); + VP8LWriteBits(bw, kImageSizeBits, height); } static void DeleteVP8LEncoder(VP8LEncoder* enc) { @@ -161,11 +155,22 @@ int VP8LEncodeImage(const WebPConfig* const config, int ok = 0; VP8LEncoder* enc = NULL; WebPEncodingError err = VP8_ENC_OK; + VP8LBitWriter bw; if (config == NULL || picture == NULL) return 0; - if (picture->argb == NULL) return 0; + + if (picture->argb == NULL) { + err = VP8_ENC_ERROR_NULL_PARAMETER; + goto Error; + } + enc = InitVP8LEncoder(config, picture); - if (enc == NULL) goto Error; + if (enc == NULL) { + err = VP8_ENC_ERROR_NULL_PARAMETER; + goto Error; + } + + VP8LBitWriterInit(&bw, (picture->width * picture->height) >> 1); // --------------------------------------------------------------------------- // Analyze image (entropy, num_palettes etc) @@ -177,11 +182,7 @@ int VP8LEncodeImage(const WebPConfig* const config, } // Write image size. - err = WriteImageSize(enc); - if (err != VP8_ENC_OK) { - ok = 0; - goto Error; - } + WriteImageSize(enc, &bw); // --------------------------------------------------------------------------- // Apply transforms and write transform data. @@ -196,8 +197,8 @@ int VP8LEncodeImage(const WebPConfig* const config, if (!ApplyCrossColorFilter(enc)) goto Error; } - if (enc->use_emerging_palette_) { - EvalEmergingPalette(enc); + if (enc->use_color_cache) { + EvalColorCache(enc); } // --------------------------------------------------------------------------- @@ -206,14 +207,14 @@ int VP8LEncodeImage(const WebPConfig* const config, ok = EncodeImageInternal(enc); if (!ok) goto Error; - err = WriteImage(enc); + err = WriteImage(enc, &bw); if (err != VP8_ENC_OK) { ok = 0; goto Error; } Error: - VP8LBitWriterDestroy(&enc->bw_); + VP8LBitWriterDestroy(&bw); DeleteVP8LEncoder(enc); if (!ok) { // TODO(vikasa): err is not set for all error paths. Set default err. diff --git a/src/enc/vp8li.h b/src/enc/vp8li.h index e46e2d2f..fd2a1bf6 100644 --- a/src/enc/vp8li.h +++ b/src/enc/vp8li.h @@ -44,9 +44,7 @@ typedef struct { int num_palette_colors; int use_predict_; int use_cross_color_; - int use_emerging_palette_; - - VP8LBitWriter bw_; + int use_color_cache; } VP8LEncoder; //------------------------------------------------------------------------------ diff --git a/src/enc/webpenc.c b/src/enc/webpenc.c index 00a3d5cc..dc183333 100644 --- a/src/enc/webpenc.c +++ b/src/enc/webpenc.c @@ -367,7 +367,7 @@ int WebPEncode(const WebPConfig* const config, WebPPicture* const pic) { if (pic->argb == NULL) return WebPEncodingSetError(pic, VP8_ENC_ERROR_NULL_PARAMETER); - ok = VP8LEncodeImage(config, pic); + ok = VP8LEncodeImage(config, pic); // Sets pic->error in case of problem. } return ok; diff --git a/src/utils/bit_writer.h b/src/utils/bit_writer.h index 40ea86fb..f7ca0849 100644 --- a/src/utils/bit_writer.h +++ b/src/utils/bit_writer.h @@ -90,7 +90,7 @@ static WEBP_INLINE uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw) { return bw->buf_; } -// Returns 1 on success. +// Returns 0 in case of memory allocation error. int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size); void VP8LBitWriterDestroy(VP8LBitWriter* const bw); @@ -111,7 +111,7 @@ void VP8LBitWriterDestroy(VP8LBitWriter* const bw); // For n bits, we take the last 5 bytes, OR that with high bits in BYTE-0, // and locate the rest in BYTE+1 and BYTE+2. // -// returns 1 on success. +// VP8LBitWriter's error_ flag is set in case of memory allocation error. void VP8LWriteBits(VP8LBitWriter* const bw, int n_bits, uint32_t bits); //------------------------------------------------------------------------------ diff --git a/src/webp/encode.h b/src/webp/encode.h index 8704810f..28a1d6bf 100644 --- a/src/webp/encode.h +++ b/src/webp/encode.h @@ -218,7 +218,7 @@ struct WebPPicture { WebPProgressHook progress_hook; // if not NULL, called while encoding. - int use_argb_input; // Flag for encoder to read argb pixels as input. + int use_argb_input; // Flag for encoder to use argb pixels as input. uint32_t* argb; // Pointer to argb (32 bit) plane. int argb_stride; // This is stride in pixels units, not bytes. };