Path: blob/master/thirdparty/libwebp/src/dec/webp_dec.c
21102 views
// Copyright 2010 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Main decoding functions for WEBP images.10//11// Author: Skal ([email protected])1213#include <assert.h>14#include <stdlib.h>15#include <string.h>1617#include "src/dec/common_dec.h"18#include "src/dec/vp8_dec.h"19#include "src/dec/vp8i_dec.h"20#include "src/dec/vp8li_dec.h"21#include "src/dec/webpi_dec.h"22#include "src/utils/rescaler_utils.h"23#include "src/utils/utils.h"24#include "src/webp/decode.h"25#include "src/webp/format_constants.h"26#include "src/webp/mux_types.h" // ALPHA_FLAG27#include "src/webp/types.h"2829//------------------------------------------------------------------------------30// RIFF layout is:31// Offset tag32// 0...3 "RIFF" 4-byte tag33// 4...7 size of image data (including metadata) starting at offset 834// 8...11 "WEBP" our form-type signature35// The RIFF container (12 bytes) is followed by appropriate chunks:36// 12..15 "VP8 ": 4-bytes tags, signaling the use of VP8 video format37// 16..19 size of the raw VP8 image data, starting at offset 2038// 20.... the VP8 bytes39// Or,40// 12..15 "VP8L": 4-bytes tags, signaling the use of VP8L lossless format41// 16..19 size of the raw VP8L image data, starting at offset 2042// 20.... the VP8L bytes43// Or,44// 12..15 "VP8X": 4-bytes tags, describing the extended-VP8 chunk.45// 16..19 size of the VP8X chunk starting at offset 20.46// 20..23 VP8X flags bit-map corresponding to the chunk-types present.47// 24..26 Width of the Canvas Image.48// 27..29 Height of the Canvas Image.49// There can be extra chunks after the "VP8X" chunk (ICCP, ANMF, VP8, VP8L,50// XMP, EXIF ...)51// All sizes are in little-endian order.52// Note: chunk data size must be padded to multiple of 2 when written.5354// Validates the RIFF container (if detected) and skips over it.55// If a RIFF container is detected, returns:56// VP8_STATUS_BITSTREAM_ERROR for invalid header,57// VP8_STATUS_NOT_ENOUGH_DATA for truncated data if have_all_data is true,58// and VP8_STATUS_OK otherwise.59// In case there are not enough bytes (partial RIFF container), return 0 for60// *riff_size. Else return the RIFF size extracted from the header.61static VP8StatusCode ParseRIFF(const uint8_t** const data,62size_t* const data_size, int have_all_data,63size_t* const riff_size) {64assert(data != NULL);65assert(data_size != NULL);66assert(riff_size != NULL);6768*riff_size = 0; // Default: no RIFF present.69if (*data_size >= RIFF_HEADER_SIZE && !memcmp(*data, "RIFF", TAG_SIZE)) {70if (memcmp(*data + 8, "WEBP", TAG_SIZE)) {71return VP8_STATUS_BITSTREAM_ERROR; // Wrong image file signature.72} else {73const uint32_t size = GetLE32(*data + TAG_SIZE);74// Check that we have at least one chunk (i.e "WEBP" + "VP8?nnnn").75if (size < TAG_SIZE + CHUNK_HEADER_SIZE) {76return VP8_STATUS_BITSTREAM_ERROR;77}78if (size > MAX_CHUNK_PAYLOAD) {79return VP8_STATUS_BITSTREAM_ERROR;80}81if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) {82return VP8_STATUS_NOT_ENOUGH_DATA; // Truncated bitstream.83}84// We have a RIFF container. Skip it.85*riff_size = size;86*data += RIFF_HEADER_SIZE;87*data_size -= RIFF_HEADER_SIZE;88}89}90return VP8_STATUS_OK;91}9293// Validates the VP8X header and skips over it.94// Returns VP8_STATUS_BITSTREAM_ERROR for invalid VP8X header,95// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and96// VP8_STATUS_OK otherwise.97// If a VP8X chunk is found, found_vp8x is set to true and *width_ptr,98// *height_ptr and *flags_ptr are set to the corresponding values extracted99// from the VP8X chunk.100static VP8StatusCode ParseVP8X(const uint8_t** const data,101size_t* const data_size,102int* const found_vp8x,103int* const width_ptr, int* const height_ptr,104uint32_t* const flags_ptr) {105const uint32_t vp8x_size = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;106assert(data != NULL);107assert(data_size != NULL);108assert(found_vp8x != NULL);109110*found_vp8x = 0;111112if (*data_size < CHUNK_HEADER_SIZE) {113return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.114}115116if (!memcmp(*data, "VP8X", TAG_SIZE)) {117int width, height;118uint32_t flags;119const uint32_t chunk_size = GetLE32(*data + TAG_SIZE);120if (chunk_size != VP8X_CHUNK_SIZE) {121return VP8_STATUS_BITSTREAM_ERROR; // Wrong chunk size.122}123124// Verify if enough data is available to validate the VP8X chunk.125if (*data_size < vp8x_size) {126return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.127}128flags = GetLE32(*data + 8);129width = 1 + GetLE24(*data + 12);130height = 1 + GetLE24(*data + 15);131if (width * (uint64_t)height >= MAX_IMAGE_AREA) {132return VP8_STATUS_BITSTREAM_ERROR; // image is too large133}134135if (flags_ptr != NULL) *flags_ptr = flags;136if (width_ptr != NULL) *width_ptr = width;137if (height_ptr != NULL) *height_ptr = height;138// Skip over VP8X header bytes.139*data += vp8x_size;140*data_size -= vp8x_size;141*found_vp8x = 1;142}143return VP8_STATUS_OK;144}145146// Skips to the next VP8/VP8L chunk header in the data given the size of the147// RIFF chunk 'riff_size'.148// Returns VP8_STATUS_BITSTREAM_ERROR if any invalid chunk size is encountered,149// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and150// VP8_STATUS_OK otherwise.151// If an alpha chunk is found, *alpha_data and *alpha_size are set152// appropriately.153static VP8StatusCode ParseOptionalChunks(const uint8_t** const data,154size_t* const data_size,155size_t const riff_size,156const uint8_t** const alpha_data,157size_t* const alpha_size) {158const uint8_t* buf;159size_t buf_size;160uint32_t total_size = TAG_SIZE + // "WEBP".161CHUNK_HEADER_SIZE + // "VP8Xnnnn".162VP8X_CHUNK_SIZE; // data.163assert(data != NULL);164assert(data_size != NULL);165buf = *data;166buf_size = *data_size;167168assert(alpha_data != NULL);169assert(alpha_size != NULL);170*alpha_data = NULL;171*alpha_size = 0;172173while (1) {174uint32_t chunk_size;175uint32_t disk_chunk_size; // chunk_size with padding176177*data = buf;178*data_size = buf_size;179180if (buf_size < CHUNK_HEADER_SIZE) { // Insufficient data.181return VP8_STATUS_NOT_ENOUGH_DATA;182}183184chunk_size = GetLE32(buf + TAG_SIZE);185if (chunk_size > MAX_CHUNK_PAYLOAD) {186return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size.187}188// For odd-sized chunk-payload, there's one byte padding at the end.189disk_chunk_size = (CHUNK_HEADER_SIZE + chunk_size + 1) & ~1u;190total_size += disk_chunk_size;191192// Check that total bytes skipped so far does not exceed riff_size.193if (riff_size > 0 && (total_size > riff_size)) {194return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size.195}196197// Start of a (possibly incomplete) VP8/VP8L chunk implies that we have198// parsed all the optional chunks.199// Note: This check must occur before the check 'buf_size < disk_chunk_size'200// below to allow incomplete VP8/VP8L chunks.201if (!memcmp(buf, "VP8 ", TAG_SIZE) ||202!memcmp(buf, "VP8L", TAG_SIZE)) {203return VP8_STATUS_OK;204}205206if (buf_size < disk_chunk_size) { // Insufficient data.207return VP8_STATUS_NOT_ENOUGH_DATA;208}209210if (!memcmp(buf, "ALPH", TAG_SIZE)) { // A valid ALPH header.211*alpha_data = buf + CHUNK_HEADER_SIZE;212*alpha_size = chunk_size;213}214215// We have a full and valid chunk; skip it.216buf += disk_chunk_size;217buf_size -= disk_chunk_size;218}219}220221// Validates the VP8/VP8L Header ("VP8 nnnn" or "VP8L nnnn") and skips over it.222// Returns VP8_STATUS_BITSTREAM_ERROR for invalid (chunk larger than223// riff_size) VP8/VP8L header,224// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and225// VP8_STATUS_OK otherwise.226// If a VP8/VP8L chunk is found, *chunk_size is set to the total number of bytes227// extracted from the VP8/VP8L chunk header.228// The flag '*is_lossless' is set to 1 in case of VP8L chunk / raw VP8L data.229static VP8StatusCode ParseVP8Header(const uint8_t** const data_ptr,230size_t* const data_size, int have_all_data,231size_t riff_size, size_t* const chunk_size,232int* const is_lossless) {233const uint8_t* const data = *data_ptr;234const int is_vp8 = !memcmp(data, "VP8 ", TAG_SIZE);235const int is_vp8l = !memcmp(data, "VP8L", TAG_SIZE);236const uint32_t minimal_size =237TAG_SIZE + CHUNK_HEADER_SIZE; // "WEBP" + "VP8 nnnn" OR238// "WEBP" + "VP8Lnnnn"239assert(data != NULL);240assert(data_size != NULL);241assert(chunk_size != NULL);242assert(is_lossless != NULL);243244if (*data_size < CHUNK_HEADER_SIZE) {245return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.246}247248if (is_vp8 || is_vp8l) {249// Bitstream contains VP8/VP8L header.250const uint32_t size = GetLE32(data + TAG_SIZE);251if ((riff_size >= minimal_size) && (size > riff_size - minimal_size)) {252return VP8_STATUS_BITSTREAM_ERROR; // Inconsistent size information.253}254if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) {255return VP8_STATUS_NOT_ENOUGH_DATA; // Truncated bitstream.256}257// Skip over CHUNK_HEADER_SIZE bytes from VP8/VP8L Header.258*chunk_size = size;259*data_ptr += CHUNK_HEADER_SIZE;260*data_size -= CHUNK_HEADER_SIZE;261*is_lossless = is_vp8l;262} else {263// Raw VP8/VP8L bitstream (no header).264*is_lossless = VP8LCheckSignature(data, *data_size);265*chunk_size = *data_size;266}267268return VP8_STATUS_OK;269}270271//------------------------------------------------------------------------------272273// Fetch '*width', '*height', '*has_alpha' and fill out 'headers' based on274// 'data'. All the output parameters may be NULL. If 'headers' is NULL only the275// minimal amount will be read to fetch the remaining parameters.276// If 'headers' is non-NULL this function will attempt to locate both alpha277// data (with or without a VP8X chunk) and the bitstream chunk (VP8/VP8L).278// Note: The following chunk sequences (before the raw VP8/VP8L data) are279// considered valid by this function:280// RIFF + VP8(L)281// RIFF + VP8X + (optional chunks) + VP8(L)282// ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose.283// VP8(L) <-- Not a valid WebP format: only allowed for internal purpose.284static VP8StatusCode ParseHeadersInternal(const uint8_t* data,285size_t data_size,286int* const width,287int* const height,288int* const has_alpha,289int* const has_animation,290int* const format,291WebPHeaderStructure* const headers) {292int canvas_width = 0;293int canvas_height = 0;294int image_width = 0;295int image_height = 0;296int found_riff = 0;297int found_vp8x = 0;298int animation_present = 0;299const int have_all_data = (headers != NULL) ? headers->have_all_data : 0;300301VP8StatusCode status;302WebPHeaderStructure hdrs;303304if (data == NULL || data_size < RIFF_HEADER_SIZE) {305return VP8_STATUS_NOT_ENOUGH_DATA;306}307memset(&hdrs, 0, sizeof(hdrs));308hdrs.data = data;309hdrs.data_size = data_size;310311// Skip over RIFF header.312status = ParseRIFF(&data, &data_size, have_all_data, &hdrs.riff_size);313if (status != VP8_STATUS_OK) {314return status; // Wrong RIFF header / insufficient data.315}316found_riff = (hdrs.riff_size > 0);317318// Skip over VP8X.319{320uint32_t flags = 0;321status = ParseVP8X(&data, &data_size, &found_vp8x,322&canvas_width, &canvas_height, &flags);323if (status != VP8_STATUS_OK) {324return status; // Wrong VP8X / insufficient data.325}326animation_present = !!(flags & ANIMATION_FLAG);327if (!found_riff && found_vp8x) {328// Note: This restriction may be removed in the future, if it becomes329// necessary to send VP8X chunk to the decoder.330return VP8_STATUS_BITSTREAM_ERROR;331}332if (has_alpha != NULL) *has_alpha = !!(flags & ALPHA_FLAG);333if (has_animation != NULL) *has_animation = animation_present;334if (format != NULL) *format = 0; // default = undefined335336image_width = canvas_width;337image_height = canvas_height;338if (found_vp8x && animation_present && headers == NULL) {339status = VP8_STATUS_OK;340goto ReturnWidthHeight; // Just return features from VP8X header.341}342}343344if (data_size < TAG_SIZE) {345status = VP8_STATUS_NOT_ENOUGH_DATA;346goto ReturnWidthHeight;347}348349// Skip over optional chunks if data started with "RIFF + VP8X" or "ALPH".350if ((found_riff && found_vp8x) ||351(!found_riff && !found_vp8x && !memcmp(data, "ALPH", TAG_SIZE))) {352status = ParseOptionalChunks(&data, &data_size, hdrs.riff_size,353&hdrs.alpha_data, &hdrs.alpha_data_size);354if (status != VP8_STATUS_OK) {355goto ReturnWidthHeight; // Invalid chunk size / insufficient data.356}357}358359// Skip over VP8/VP8L header.360status = ParseVP8Header(&data, &data_size, have_all_data, hdrs.riff_size,361&hdrs.compressed_size, &hdrs.is_lossless);362if (status != VP8_STATUS_OK) {363goto ReturnWidthHeight; // Wrong VP8/VP8L chunk-header / insufficient data.364}365if (hdrs.compressed_size > MAX_CHUNK_PAYLOAD) {366return VP8_STATUS_BITSTREAM_ERROR;367}368369if (format != NULL && !animation_present) {370*format = hdrs.is_lossless ? 2 : 1;371}372373if (!hdrs.is_lossless) {374if (data_size < VP8_FRAME_HEADER_SIZE) {375status = VP8_STATUS_NOT_ENOUGH_DATA;376goto ReturnWidthHeight;377}378// Validates raw VP8 data.379if (!VP8GetInfo(data, data_size, (uint32_t)hdrs.compressed_size,380&image_width, &image_height)) {381return VP8_STATUS_BITSTREAM_ERROR;382}383} else {384if (data_size < VP8L_FRAME_HEADER_SIZE) {385status = VP8_STATUS_NOT_ENOUGH_DATA;386goto ReturnWidthHeight;387}388// Validates raw VP8L data.389if (!VP8LGetInfo(data, data_size, &image_width, &image_height, has_alpha)) {390return VP8_STATUS_BITSTREAM_ERROR;391}392}393// Validates image size coherency.394if (found_vp8x) {395if (canvas_width != image_width || canvas_height != image_height) {396return VP8_STATUS_BITSTREAM_ERROR;397}398}399if (headers != NULL) {400*headers = hdrs;401headers->offset = data - headers->data;402assert((uint64_t)(data - headers->data) < MAX_CHUNK_PAYLOAD);403assert(headers->offset == headers->data_size - data_size);404}405ReturnWidthHeight:406if (status == VP8_STATUS_OK ||407(status == VP8_STATUS_NOT_ENOUGH_DATA && found_vp8x && headers == NULL)) {408if (has_alpha != NULL) {409// If the data did not contain a VP8X/VP8L chunk the only definitive way410// to set this is by looking for alpha data (from an ALPH chunk).411*has_alpha |= (hdrs.alpha_data != NULL);412}413if (width != NULL) *width = image_width;414if (height != NULL) *height = image_height;415return VP8_STATUS_OK;416} else {417return status;418}419}420421VP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers) {422// status is marked volatile as a workaround for a clang-3.8 (aarch64) bug423volatile VP8StatusCode status;424int has_animation = 0;425assert(headers != NULL);426// fill out headers, ignore width/height/has_alpha.427status = ParseHeadersInternal(headers->data, headers->data_size,428NULL, NULL, NULL, &has_animation,429NULL, headers);430if (status == VP8_STATUS_OK || status == VP8_STATUS_NOT_ENOUGH_DATA) {431// The WebPDemux API + libwebp can be used to decode individual432// uncomposited frames or the WebPAnimDecoder can be used to fully433// reconstruct them (see webp/demux.h).434if (has_animation) {435status = VP8_STATUS_UNSUPPORTED_FEATURE;436}437}438return status;439}440441//------------------------------------------------------------------------------442// WebPDecParams443444void WebPResetDecParams(WebPDecParams* const params) {445if (params != NULL) {446memset(params, 0, sizeof(*params));447}448}449450//------------------------------------------------------------------------------451// "Into" decoding variants452453// Main flow454WEBP_NODISCARD static VP8StatusCode DecodeInto(const uint8_t* const data,455size_t data_size,456WebPDecParams* const params) {457VP8StatusCode status;458VP8Io io;459WebPHeaderStructure headers;460461headers.data = data;462headers.data_size = data_size;463headers.have_all_data = 1;464status = WebPParseHeaders(&headers); // Process Pre-VP8 chunks.465if (status != VP8_STATUS_OK) {466return status;467}468469assert(params != NULL);470if (!VP8InitIo(&io)) {471return VP8_STATUS_INVALID_PARAM;472}473io.data = headers.data + headers.offset;474io.data_size = headers.data_size - headers.offset;475WebPInitCustomIo(params, &io); // Plug the I/O functions.476477if (!headers.is_lossless) {478VP8Decoder* const dec = VP8New();479if (dec == NULL) {480return VP8_STATUS_OUT_OF_MEMORY;481}482dec->alpha_data = headers.alpha_data;483dec->alpha_data_size = headers.alpha_data_size;484485// Decode bitstream header, update io->width/io->height.486if (!VP8GetHeaders(dec, &io)) {487status = dec->status; // An error occurred. Grab error status.488} else {489// Allocate/check output buffers.490status = WebPAllocateDecBuffer(io.width, io.height, params->options,491params->output);492if (status == VP8_STATUS_OK) { // Decode493// This change must be done before calling VP8Decode()494dec->mt_method = VP8GetThreadMethod(params->options, &headers,495io.width, io.height);496VP8InitDithering(params->options, dec);497if (!VP8Decode(dec, &io)) {498status = dec->status;499}500}501}502VP8Delete(dec);503} else {504VP8LDecoder* const dec = VP8LNew();505if (dec == NULL) {506return VP8_STATUS_OUT_OF_MEMORY;507}508if (!VP8LDecodeHeader(dec, &io)) {509status = dec->status; // An error occurred. Grab error status.510} else {511// Allocate/check output buffers.512status = WebPAllocateDecBuffer(io.width, io.height, params->options,513params->output);514if (status == VP8_STATUS_OK) { // Decode515if (!VP8LDecodeImage(dec)) {516status = dec->status;517}518}519}520VP8LDelete(dec);521}522523if (status != VP8_STATUS_OK) {524WebPFreeDecBuffer(params->output);525} else {526if (params->options != NULL && params->options->flip) {527// This restores the original stride values if options->flip was used528// during the call to WebPAllocateDecBuffer above.529status = WebPFlipBuffer(params->output);530}531}532return status;533}534535// Helpers536WEBP_NODISCARD static uint8_t* DecodeIntoRGBABuffer(WEBP_CSP_MODE colorspace,537const uint8_t* const data,538size_t data_size,539uint8_t* const rgba,540int stride, size_t size) {541WebPDecParams params;542WebPDecBuffer buf;543if (rgba == NULL || !WebPInitDecBuffer(&buf)) {544return NULL;545}546WebPResetDecParams(¶ms);547params.output = &buf;548buf.colorspace = colorspace;549buf.u.RGBA.rgba = rgba;550buf.u.RGBA.stride = stride;551buf.u.RGBA.size = size;552buf.is_external_memory = 1;553if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) {554return NULL;555}556return rgba;557}558559uint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size,560uint8_t* output, size_t size, int stride) {561return DecodeIntoRGBABuffer(MODE_RGB, data, data_size, output, stride, size);562}563564uint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size,565uint8_t* output, size_t size, int stride) {566return DecodeIntoRGBABuffer(MODE_RGBA, data, data_size, output, stride, size);567}568569uint8_t* WebPDecodeARGBInto(const uint8_t* data, size_t data_size,570uint8_t* output, size_t size, int stride) {571return DecodeIntoRGBABuffer(MODE_ARGB, data, data_size, output, stride, size);572}573574uint8_t* WebPDecodeBGRInto(const uint8_t* data, size_t data_size,575uint8_t* output, size_t size, int stride) {576return DecodeIntoRGBABuffer(MODE_BGR, data, data_size, output, stride, size);577}578579uint8_t* WebPDecodeBGRAInto(const uint8_t* data, size_t data_size,580uint8_t* output, size_t size, int stride) {581return DecodeIntoRGBABuffer(MODE_BGRA, data, data_size, output, stride, size);582}583584uint8_t* WebPDecodeYUVInto(const uint8_t* data, size_t data_size,585uint8_t* luma, size_t luma_size, int luma_stride,586uint8_t* u, size_t u_size, int u_stride,587uint8_t* v, size_t v_size, int v_stride) {588WebPDecParams params;589WebPDecBuffer output;590if (luma == NULL || !WebPInitDecBuffer(&output)) return NULL;591WebPResetDecParams(¶ms);592params.output = &output;593output.colorspace = MODE_YUV;594output.u.YUVA.y = luma;595output.u.YUVA.y_stride = luma_stride;596output.u.YUVA.y_size = luma_size;597output.u.YUVA.u = u;598output.u.YUVA.u_stride = u_stride;599output.u.YUVA.u_size = u_size;600output.u.YUVA.v = v;601output.u.YUVA.v_stride = v_stride;602output.u.YUVA.v_size = v_size;603output.is_external_memory = 1;604if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) {605return NULL;606}607return luma;608}609610//------------------------------------------------------------------------------611612WEBP_NODISCARD static uint8_t* Decode(WEBP_CSP_MODE mode,613const uint8_t* const data,614size_t data_size, int* const width,615int* const height,616WebPDecBuffer* const keep_info) {617WebPDecParams params;618WebPDecBuffer output;619620if (!WebPInitDecBuffer(&output)) {621return NULL;622}623WebPResetDecParams(¶ms);624params.output = &output;625output.colorspace = mode;626627// Retrieve (and report back) the required dimensions from bitstream.628if (!WebPGetInfo(data, data_size, &output.width, &output.height)) {629return NULL;630}631if (width != NULL) *width = output.width;632if (height != NULL) *height = output.height;633634// Decode635if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) {636return NULL;637}638if (keep_info != NULL) { // keep track of the side-info639WebPCopyDecBuffer(&output, keep_info);640}641// return decoded samples (don't clear 'output'!)642return WebPIsRGBMode(mode) ? output.u.RGBA.rgba : output.u.YUVA.y;643}644645uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size,646int* width, int* height) {647return Decode(MODE_RGB, data, data_size, width, height, NULL);648}649650uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size,651int* width, int* height) {652return Decode(MODE_RGBA, data, data_size, width, height, NULL);653}654655uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size,656int* width, int* height) {657return Decode(MODE_ARGB, data, data_size, width, height, NULL);658}659660uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size,661int* width, int* height) {662return Decode(MODE_BGR, data, data_size, width, height, NULL);663}664665uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size,666int* width, int* height) {667return Decode(MODE_BGRA, data, data_size, width, height, NULL);668}669670uint8_t* WebPDecodeYUV(const uint8_t* data, size_t data_size,671int* width, int* height, uint8_t** u, uint8_t** v,672int* stride, int* uv_stride) {673// data, width and height are checked by Decode().674if (u == NULL || v == NULL || stride == NULL || uv_stride == NULL) {675return NULL;676}677678{679WebPDecBuffer output; // only to preserve the side-infos680uint8_t* const out = Decode(MODE_YUV, data, data_size,681width, height, &output);682683if (out != NULL) {684const WebPYUVABuffer* const buf = &output.u.YUVA;685*u = buf->u;686*v = buf->v;687*stride = buf->y_stride;688*uv_stride = buf->u_stride;689assert(buf->u_stride == buf->v_stride);690}691return out;692}693}694695static void DefaultFeatures(WebPBitstreamFeatures* const features) {696assert(features != NULL);697memset(features, 0, sizeof(*features));698}699700static VP8StatusCode GetFeatures(const uint8_t* const data, size_t data_size,701WebPBitstreamFeatures* const features) {702if (features == NULL || data == NULL) {703return VP8_STATUS_INVALID_PARAM;704}705DefaultFeatures(features);706707// Only parse enough of the data to retrieve the features.708return ParseHeadersInternal(data, data_size,709&features->width, &features->height,710&features->has_alpha, &features->has_animation,711&features->format, NULL);712}713714//------------------------------------------------------------------------------715// WebPGetInfo()716717int WebPGetInfo(const uint8_t* data, size_t data_size,718int* width, int* height) {719WebPBitstreamFeatures features;720721if (GetFeatures(data, data_size, &features) != VP8_STATUS_OK) {722return 0;723}724725if (width != NULL) {726*width = features.width;727}728if (height != NULL) {729*height = features.height;730}731732return 1;733}734735//------------------------------------------------------------------------------736// Advance decoding API737738int WebPInitDecoderConfigInternal(WebPDecoderConfig* config,739int version) {740if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {741return 0; // version mismatch742}743if (config == NULL) {744return 0;745}746memset(config, 0, sizeof(*config));747DefaultFeatures(&config->input);748if (!WebPInitDecBuffer(&config->output)) {749return 0;750}751return 1;752}753754static int WebPCheckCropDimensionsBasic(int x, int y, int w, int h) {755return !(x < 0 || y < 0 || w <= 0 || h <= 0);756}757758int WebPValidateDecoderConfig(const WebPDecoderConfig* config) {759const WebPDecoderOptions* options;760if (config == NULL) return 0;761if (!IsValidColorspace(config->output.colorspace)) {762return 0;763}764765options = &config->options;766// bypass_filtering, no_fancy_upsampling, use_cropping, use_scaling,767// use_threads, flip can be any integer and are interpreted as boolean.768769// Check for cropping.770if (options->use_cropping && !WebPCheckCropDimensionsBasic(771options->crop_left, options->crop_top,772options->crop_width, options->crop_height)) {773return 0;774}775// Check for scaling.776if (options->use_scaling &&777(options->scaled_width < 0 || options->scaled_height < 0 ||778(options->scaled_width == 0 && options->scaled_height == 0))) {779return 0;780}781782// In case the WebPBitstreamFeatures has been filled in, check further.783if (config->input.width > 0 || config->input.height > 0) {784int scaled_width = options->scaled_width;785int scaled_height = options->scaled_height;786if (options->use_cropping &&787!WebPCheckCropDimensions(config->input.width, config->input.height,788options->crop_left, options->crop_top,789options->crop_width, options->crop_height)) {790return 0;791}792if (options->use_scaling && !WebPRescalerGetScaledDimensions(793config->input.width, config->input.height,794&scaled_width, &scaled_height)) {795return 0;796}797}798799// Check for dithering.800if (options->dithering_strength < 0 || options->dithering_strength > 100 ||801options->alpha_dithering_strength < 0 ||802options->alpha_dithering_strength > 100) {803return 0;804}805806return 1;807}808809VP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, size_t data_size,810WebPBitstreamFeatures* features,811int version) {812if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {813return VP8_STATUS_INVALID_PARAM; // version mismatch814}815if (features == NULL) {816return VP8_STATUS_INVALID_PARAM;817}818return GetFeatures(data, data_size, features);819}820821VP8StatusCode WebPDecode(const uint8_t* data, size_t data_size,822WebPDecoderConfig* config) {823WebPDecParams params;824VP8StatusCode status;825826if (config == NULL) {827return VP8_STATUS_INVALID_PARAM;828}829830status = GetFeatures(data, data_size, &config->input);831if (status != VP8_STATUS_OK) {832if (status == VP8_STATUS_NOT_ENOUGH_DATA) {833return VP8_STATUS_BITSTREAM_ERROR; // Not-enough-data treated as error.834}835return status;836}837838WebPResetDecParams(¶ms);839params.options = &config->options;840params.output = &config->output;841if (WebPAvoidSlowMemory(params.output, &config->input)) {842// decoding to slow memory: use a temporary in-mem buffer to decode into.843WebPDecBuffer in_mem_buffer;844if (!WebPInitDecBuffer(&in_mem_buffer)) {845return VP8_STATUS_INVALID_PARAM;846}847in_mem_buffer.colorspace = config->output.colorspace;848in_mem_buffer.width = config->input.width;849in_mem_buffer.height = config->input.height;850params.output = &in_mem_buffer;851status = DecodeInto(data, data_size, ¶ms);852if (status == VP8_STATUS_OK) { // do the slow-copy853status = WebPCopyDecBufferPixels(&in_mem_buffer, &config->output);854}855WebPFreeDecBuffer(&in_mem_buffer);856} else {857status = DecodeInto(data, data_size, ¶ms);858}859860return status;861}862863//------------------------------------------------------------------------------864// Cropping and rescaling.865866int WebPCheckCropDimensions(int image_width, int image_height,867int x, int y, int w, int h) {868return WebPCheckCropDimensionsBasic(x, y, w, h) &&869!(x >= image_width || w > image_width || w > image_width - x ||870y >= image_height || h > image_height || h > image_height - y);871}872873int WebPIoInitFromOptions(const WebPDecoderOptions* const options,874VP8Io* const io, WEBP_CSP_MODE src_colorspace) {875const int W = io->width;876const int H = io->height;877int x = 0, y = 0, w = W, h = H;878879// Cropping880io->use_cropping = (options != NULL) && options->use_cropping;881if (io->use_cropping) {882w = options->crop_width;883h = options->crop_height;884x = options->crop_left;885y = options->crop_top;886if (!WebPIsRGBMode(src_colorspace)) { // only snap for YUV420887x &= ~1;888y &= ~1;889}890if (!WebPCheckCropDimensions(W, H, x, y, w, h)) {891return 0; // out of frame boundary error892}893}894io->crop_left = x;895io->crop_top = y;896io->crop_right = x + w;897io->crop_bottom = y + h;898io->mb_w = w;899io->mb_h = h;900901// Scaling902io->use_scaling = (options != NULL) && options->use_scaling;903if (io->use_scaling) {904int scaled_width = options->scaled_width;905int scaled_height = options->scaled_height;906if (!WebPRescalerGetScaledDimensions(w, h, &scaled_width, &scaled_height)) {907return 0;908}909io->scaled_width = scaled_width;910io->scaled_height = scaled_height;911}912913// Filter914io->bypass_filtering = (options != NULL) && options->bypass_filtering;915916// Fancy upsampler917#ifdef FANCY_UPSAMPLING918io->fancy_upsampling = (options == NULL) || (!options->no_fancy_upsampling);919#endif920921if (io->use_scaling) {922// disable filter (only for large downscaling ratio).923io->bypass_filtering |= (io->scaled_width < W * 3 / 4) &&924(io->scaled_height < H * 3 / 4);925io->fancy_upsampling = 0;926}927return 1;928}929930//------------------------------------------------------------------------------931932933