static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) { const int w = buffer->width; const int h = buffer->height; const WEBP_CSP_MODE mode = buffer->colorspace; if (w <= 0 || h <= 0 || !IsValidColorspace(mode)) { return VP8_STATUS_INVALID_PARAM; } if (!buffer->is_external_memory && buffer->private_memory == NULL) { uint8_t* output; int uv_stride = 0, a_stride = 0; uint64_t uv_size = 0, a_size = 0, total_size; // We need memory and it hasn't been allocated yet. // => initialize output buffer, now that dimensions are known. const int stride = w * kModeBpp[mode]; const uint64_t size = (uint64_t)stride * h; if (!WebPIsRGBMode(mode)) { uv_stride = (w + 1) / 2; uv_size = (uint64_t)uv_stride * ((h + 1) / 2); if (mode == MODE_YUVA) { a_stride = w; a_size = (uint64_t)a_stride * h; } } total_size = size + 2 * uv_size + a_size; // Security/sanity checks output = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*output)); if (output == NULL) { return VP8_STATUS_OUT_OF_MEMORY; } buffer->private_memory = output; if (!WebPIsRGBMode(mode)) { // YUVA initialization WebPYUVABuffer* const buf = &buffer->u.YUVA; buf->y = output; buf->y_stride = stride; buf->y_size = (size_t)size; buf->u = output + size; buf->u_stride = uv_stride; buf->u_size = (size_t)uv_size; buf->v = output + size + uv_size; buf->v_stride = uv_stride; buf->v_size = (size_t)uv_size; if (mode == MODE_YUVA) { buf->a = output + size + 2 * uv_size; } buf->a_size = (size_t)a_size; buf->a_stride = a_stride; } else { // RGBA initialization WebPRGBABuffer* const buf = &buffer->u.RGBA; buf->rgba = output; buf->stride = stride; buf->size = (size_t)size; } } return CheckDecBuffer(buffer); }
static uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* const data, size_t data_size, int* const width, int* const height, WebPDecBuffer* const keep_info) { WebPDecParams params; WebPDecBuffer output; WebPInitDecBuffer(&output); WebPResetDecParams(¶ms); params.output = &output; output.colorspace = mode; if (!WebPGetInfo(data, data_size, &output.width, &output.height)) { return NULL; } if (width != NULL) *width = output.width; if (height != NULL) *height = output.height; if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) { return NULL; } if (keep_info != NULL) { WebPCopyDecBuffer(&output, keep_info); } return WebPIsRGBMode(mode) ? output.u.RGBA.rgba : output.u.YUVA.y; }
static uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* const data, size_t data_size, int* const width, int* const height, WebPDecBuffer* const keep_info) { WebPDecParams params; WebPDecBuffer output; WebPInitDecBuffer(&output); WebPResetDecParams(¶ms); params.output = &output; output.colorspace = mode; // Retrieve (and report back) the required dimensions from bitstream. if (!WebPGetInfo(data, data_size, &output.width, &output.height)) { return NULL; } if (width != NULL) *width = output.width; if (height != NULL) *height = output.height; // Decode if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) { return NULL; } if (keep_info != NULL) { // keep track of the side-info WebPCopyDecBuffer(&output, keep_info); } // return decoded samples (don't clear 'output'!) return WebPIsRGBMode(mode) ? output.u.RGBA.rgba : output.u.YUVA.y; }
static int CustomSetup(VP8Io* io) { WebPDecParams* const p = (WebPDecParams*)io->opaque; const WEBP_CSP_MODE colorspace = p->output->colorspace; const int is_rgb = WebPIsRGBMode(colorspace); const int is_alpha = WebPIsAlphaMode(colorspace); p->memory = NULL; p->emit = NULL; p->emit_alpha = NULL; p->emit_alpha_row = NULL; if (!WebPIoInitFromOptions(p->options, io, is_alpha ? MODE_YUV : MODE_YUVA)) { return 0; } if (is_alpha && WebPIsPremultipliedMode(colorspace)) { WebPInitUpsamplers(); } if (io->use_scaling) { #if !defined(WEBP_REDUCE_SIZE) const int ok = is_rgb ? InitRGBRescaler(io, p) : InitYUVRescaler(io, p); if (!ok) { return 0; // memory error } #else return 0; // rescaling support not compiled #endif } else { if (is_rgb) { WebPInitSamplers(); p->emit = EmitSampledRGB; // default if (io->fancy_upsampling) { #ifdef FANCY_UPSAMPLING const int uv_width = (io->mb_w + 1) >> 1; p->memory = WebPSafeMalloc(1ULL, (size_t)(io->mb_w + 2 * uv_width)); if (p->memory == NULL) { return 0; // memory error. } p->tmp_y = (uint8_t*)p->memory; p->tmp_u = p->tmp_y + io->mb_w; p->tmp_v = p->tmp_u + uv_width; p->emit = EmitFancyRGB; WebPInitUpsamplers(); #endif } } else { p->emit = EmitYUV; } if (is_alpha) { // need transparency output p->emit_alpha = (colorspace == MODE_RGBA_4444 || colorspace == MODE_rgbA_4444) ? EmitAlphaRGBA4444 : is_rgb ? EmitAlphaRGB : EmitAlphaYUV; if (is_rgb) { WebPInitAlphaProcessing(); } } }
int WebPIoInitFromOptions(const WebPDecoderOptions* const options, VP8Io* const io, WEBP_CSP_MODE src_colorspace) { const int W = io->width; const int H = io->height; int x = 0, y = 0, w = W, h = H; // Cropping io->use_cropping = (options != NULL) && (options->use_cropping > 0); if (io->use_cropping) { w = options->crop_width; h = options->crop_height; x = options->crop_left; y = options->crop_top; if (!WebPIsRGBMode(src_colorspace)) { // only snap for YUV420 x &= ~1; y &= ~1; } if (x < 0 || y < 0 || w <= 0 || h <= 0 || x + w > W || y + h > H) { return 0; // out of frame boundary error } } io->crop_left = x; io->crop_top = y; io->crop_right = x + w; io->crop_bottom = y + h; io->mb_w = w; io->mb_h = h; // Scaling io->use_scaling = (options != NULL) && (options->use_scaling > 0); if (io->use_scaling) { int scaled_width = options->scaled_width; int scaled_height = options->scaled_height; if (!WebPRescalerGetScaledDimensions(w, h, &scaled_width, &scaled_height)) { return 0; } io->scaled_width = scaled_width; io->scaled_height = scaled_height; } // Filter io->bypass_filtering = options && options->bypass_filtering; // Fancy upsampler #ifdef FANCY_UPSAMPLING io->fancy_upsampling = (options == NULL) || (!options->no_fancy_upsampling); #endif if (io->use_scaling) { // disable filter (only for large downscaling ratio). io->bypass_filtering = (io->scaled_width < W * 3 / 4) && (io->scaled_height < H * 3 / 4); io->fancy_upsampling = 0; } return 1; }
int WebPIoInitFromOptions(const WebPDecoderOptions* const options, VP8Io* const io, WEBP_CSP_MODE src_colorspace) { const int W = io->width; const int H = io->height; int x = 0, y = 0, w = W, h = H; io->use_cropping = (options != NULL) && (options->use_cropping > 0); if (io->use_cropping) { w = options->crop_width; h = options->crop_height; x = options->crop_left; y = options->crop_top; if (!WebPIsRGBMode(src_colorspace)) { x &= ~1; y &= ~1; } if (x < 0 || y < 0 || w <= 0 || h <= 0 || x + w > W || y + h > H) { return 0; } } io->crop_left = x; io->crop_top = y; io->crop_right = x + w; io->crop_bottom = y + h; io->mb_w = w; io->mb_h = h; io->use_scaling = (options != NULL) && (options->use_scaling > 0); if (io->use_scaling) { if (options->scaled_width <= 0 || options->scaled_height <= 0) { return 0; } io->scaled_width = options->scaled_width; io->scaled_height = options->scaled_height; } io->bypass_filtering = options && options->bypass_filtering; #ifdef FANCY_UPSAMPLING io->fancy_upsampling = (options == NULL) || (!options->no_fancy_upsampling); #endif if (io->use_scaling) { io->bypass_filtering = (io->scaled_width < W * 3 / 4) && (io->scaled_height < H * 3 / 4); io->fancy_upsampling = 0; } return 1; }
static int CustomSetup(VP8Io* io) { WebPDecParams* const p = (WebPDecParams*)io->opaque; const WEBP_CSP_MODE colorspace = p->output->colorspace; const int is_rgb = WebPIsRGBMode(colorspace); const int is_alpha = WebPIsAlphaMode(colorspace); p->memory = NULL; p->emit = NULL; p->emit_alpha = NULL; p->emit_alpha_row = NULL; if (!WebPIoInitFromOptions(p->options, io, is_alpha ? MODE_YUV : MODE_YUVA)) { return 0; } if (io->use_scaling) { const int ok = is_rgb ? InitRGBRescaler(io, p) : InitYUVRescaler(io, p); if (!ok) { return 0; } } else { if (is_rgb) { p->emit = EmitSampledRGB; #ifdef FANCY_UPSAMPLING if (io->fancy_upsampling) { const int uv_width = (io->mb_w + 1) >> 1; p->memory = malloc(io->mb_w + 2 * uv_width); if (p->memory == NULL) { return 0; } p->tmp_y = (uint8_t*)p->memory; p->tmp_u = p->tmp_y + io->mb_w; p->tmp_v = p->tmp_u + uv_width; p->emit = EmitFancyRGB; WebPInitUpsamplers(); } #endif } else { p->emit = EmitYUV; } if (is_alpha) { if (WebPIsPremultipliedMode(colorspace)) WebPInitPremultiply(); p->emit_alpha = (colorspace == MODE_RGBA_4444 || colorspace == MODE_rgbA_4444) ? EmitAlphaRGBA4444 : is_rgb ? EmitAlphaRGB : EmitAlphaYUV; } }
static VP8StatusCode CheckDecBuffer(const WebPDecBuffer* const buffer) { int ok = 1; const WEBP_CSP_MODE mode = buffer->colorspace; const int width = buffer->width; const int height = buffer->height; if (!IsValidColorspace(mode)) { ok = 0; } else if (!WebPIsRGBMode(mode)) { // YUV checks const WebPYUVABuffer* const buf = &buffer->u.YUVA; const int uv_width = (width + 1) / 2; const int uv_height = (height + 1) / 2; const int y_stride = abs(buf->y_stride); const int u_stride = abs(buf->u_stride); const int v_stride = abs(buf->v_stride); const int a_stride = abs(buf->a_stride); const uint64_t y_size = MIN_BUFFER_SIZE(width, height, y_stride); const uint64_t u_size = MIN_BUFFER_SIZE(uv_width, uv_height, u_stride); const uint64_t v_size = MIN_BUFFER_SIZE(uv_width, uv_height, v_stride); const uint64_t a_size = MIN_BUFFER_SIZE(width, height, a_stride); ok &= (y_size <= buf->y_size); ok &= (u_size <= buf->u_size); ok &= (v_size <= buf->v_size); ok &= (y_stride >= width); ok &= (u_stride >= uv_width); ok &= (v_stride >= uv_width); ok &= (buf->y != NULL); ok &= (buf->u != NULL); ok &= (buf->v != NULL); if (mode == MODE_YUVA) { ok &= (a_stride >= width); ok &= (a_size <= buf->a_size); ok &= (buf->a != NULL); } } else { // RGB checks const WebPRGBABuffer* const buf = &buffer->u.RGBA; const int stride = abs(buf->stride); const uint64_t size = MIN_BUFFER_SIZE(width, height, stride); ok &= (size <= buf->size); ok &= (stride >= width * kModeBpp[mode]); ok &= (buf->rgba != NULL); } return ok ? VP8_STATUS_OK : VP8_STATUS_INVALID_PARAM; }
static int CustomSetup(VP8Io* io) { WebPDecParams* const p = (WebPDecParams*)io->opaque; const WEBP_CSP_MODE colorspace = p->output->colorspace; const int is_rgb = WebPIsRGBMode(colorspace); const int is_alpha = WebPIsAlphaMode(colorspace); p->memory = NULL; p->emit = NULL; p->emit_alpha = NULL; p->emit_alpha_row = NULL; if (!WebPIoInitFromOptions(p->options, io, is_alpha ? MODE_YUV : MODE_YUVA)) { return 0; } if (is_alpha && WebPIsPremultipliedMode(colorspace)) { WebPInitUpsamplers(); } if (io->use_scaling) { const int ok = is_rgb ? InitRGBRescaler(io, p) : InitYUVRescaler(io, p); if (!ok) { return 0; // memory error } } else { if (is_rgb) { p->emit = EmitSampledRGB; // default if (io->fancy_upsampling) { #ifdef FANCY_UPSAMPLING const int uv_width = (io->mb_w + 1) >> 1; p->memory = WebPSafeMalloc(1ULL, (size_t)(io->mb_w + 2 * uv_width)); if (p->memory == NULL) { return 0; // memory error. } p->tmp_y = (uint8_t*)p->memory; p->tmp_u = p->tmp_y + io->mb_w; p->tmp_v = p->tmp_u + uv_width; p->emit = EmitFancyRGB; WebPInitUpsamplers(); #endif } else { WebPInitSamplers(); } } else {
VP8StatusCode WebPFlipBuffer(WebPDecBuffer* const buffer) { if (buffer == NULL) { return VP8_STATUS_INVALID_PARAM; } if (WebPIsRGBMode(buffer->colorspace)) { WebPRGBABuffer* const buf = &buffer->u.RGBA; buf->rgba += (buffer->height - 1) * buf->stride; buf->stride = -buf->stride; } else { WebPYUVABuffer* const buf = &buffer->u.YUVA; const int H = buffer->height; buf->y += (H - 1) * buf->y_stride; buf->y_stride = -buf->y_stride; buf->u += ((H - 1) >> 1) * buf->u_stride; buf->u_stride = -buf->u_stride; buf->v += ((H - 1) >> 1) * buf->v_stride; buf->v_stride = -buf->v_stride; if (buf->a != NULL) { buf->a += (H - 1) * buf->a_stride; buf->a_stride = -buf->a_stride; } } return VP8_STATUS_OK; }