Path: blob/master/thirdparty/libwebp/src/dec/webp_dec.c
9912 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 <stdlib.h>1415#include "src/dec/vp8_dec.h"16#include "src/dec/vp8i_dec.h"17#include "src/dec/vp8li_dec.h"18#include "src/dec/webpi_dec.h"19#include "src/utils/utils.h"20#include "src/webp/mux_types.h" // ALPHA_FLAG21#include "src/webp/decode.h"22#include "src/webp/types.h"2324//------------------------------------------------------------------------------25// RIFF layout is:26// Offset tag27// 0...3 "RIFF" 4-byte tag28// 4...7 size of image data (including metadata) starting at offset 829// 8...11 "WEBP" our form-type signature30// The RIFF container (12 bytes) is followed by appropriate chunks:31// 12..15 "VP8 ": 4-bytes tags, signaling the use of VP8 video format32// 16..19 size of the raw VP8 image data, starting at offset 2033// 20.... the VP8 bytes34// Or,35// 12..15 "VP8L": 4-bytes tags, signaling the use of VP8L lossless format36// 16..19 size of the raw VP8L image data, starting at offset 2037// 20.... the VP8L bytes38// Or,39// 12..15 "VP8X": 4-bytes tags, describing the extended-VP8 chunk.40// 16..19 size of the VP8X chunk starting at offset 20.41// 20..23 VP8X flags bit-map corresponding to the chunk-types present.42// 24..26 Width of the Canvas Image.43// 27..29 Height of the Canvas Image.44// There can be extra chunks after the "VP8X" chunk (ICCP, ANMF, VP8, VP8L,45// XMP, EXIF ...)46// All sizes are in little-endian order.47// Note: chunk data size must be padded to multiple of 2 when written.4849// Validates the RIFF container (if detected) and skips over it.50// If a RIFF container is detected, returns:51// VP8_STATUS_BITSTREAM_ERROR for invalid header,52// VP8_STATUS_NOT_ENOUGH_DATA for truncated data if have_all_data is true,53// and VP8_STATUS_OK otherwise.54// In case there are not enough bytes (partial RIFF container), return 0 for55// *riff_size. Else return the RIFF size extracted from the header.56static VP8StatusCode ParseRIFF(const uint8_t** const data,57size_t* const data_size, int have_all_data,58size_t* const riff_size) {59assert(data != NULL);60assert(data_size != NULL);61assert(riff_size != NULL);6263*riff_size = 0; // Default: no RIFF present.64if (*data_size >= RIFF_HEADER_SIZE && !memcmp(*data, "RIFF", TAG_SIZE)) {65if (memcmp(*data + 8, "WEBP", TAG_SIZE)) {66return VP8_STATUS_BITSTREAM_ERROR; // Wrong image file signature.67} else {68const uint32_t size = GetLE32(*data + TAG_SIZE);69// Check that we have at least one chunk (i.e "WEBP" + "VP8?nnnn").70if (size < TAG_SIZE + CHUNK_HEADER_SIZE) {71return VP8_STATUS_BITSTREAM_ERROR;72}73if (size > MAX_CHUNK_PAYLOAD) {74return VP8_STATUS_BITSTREAM_ERROR;75}76if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) {77return VP8_STATUS_NOT_ENOUGH_DATA; // Truncated bitstream.78}79// We have a RIFF container. Skip it.80*riff_size = size;81*data += RIFF_HEADER_SIZE;82*data_size -= RIFF_HEADER_SIZE;83}84}85return VP8_STATUS_OK;86}8788// Validates the VP8X header and skips over it.89// Returns VP8_STATUS_BITSTREAM_ERROR for invalid VP8X header,90// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and91// VP8_STATUS_OK otherwise.92// If a VP8X chunk is found, found_vp8x is set to true and *width_ptr,93// *height_ptr and *flags_ptr are set to the corresponding values extracted94// from the VP8X chunk.95static VP8StatusCode ParseVP8X(const uint8_t** const data,96size_t* const data_size,97int* const found_vp8x,98int* const width_ptr, int* const height_ptr,99uint32_t* const flags_ptr) {100const uint32_t vp8x_size = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;101assert(data != NULL);102assert(data_size != NULL);103assert(found_vp8x != NULL);104105*found_vp8x = 0;106107if (*data_size < CHUNK_HEADER_SIZE) {108return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.109}110111if (!memcmp(*data, "VP8X", TAG_SIZE)) {112int width, height;113uint32_t flags;114const uint32_t chunk_size = GetLE32(*data + TAG_SIZE);115if (chunk_size != VP8X_CHUNK_SIZE) {116return VP8_STATUS_BITSTREAM_ERROR; // Wrong chunk size.117}118119// Verify if enough data is available to validate the VP8X chunk.120if (*data_size < vp8x_size) {121return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.122}123flags = GetLE32(*data + 8);124width = 1 + GetLE24(*data + 12);125height = 1 + GetLE24(*data + 15);126if (width * (uint64_t)height >= MAX_IMAGE_AREA) {127return VP8_STATUS_BITSTREAM_ERROR; // image is too large128}129130if (flags_ptr != NULL) *flags_ptr = flags;131if (width_ptr != NULL) *width_ptr = width;132if (height_ptr != NULL) *height_ptr = height;133// Skip over VP8X header bytes.134*data += vp8x_size;135*data_size -= vp8x_size;136*found_vp8x = 1;137}138return VP8_STATUS_OK;139}140141// Skips to the next VP8/VP8L chunk header in the data given the size of the142// RIFF chunk 'riff_size'.143// Returns VP8_STATUS_BITSTREAM_ERROR if any invalid chunk size is encountered,144// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and145// VP8_STATUS_OK otherwise.146// If an alpha chunk is found, *alpha_data and *alpha_size are set147// appropriately.148static VP8StatusCode ParseOptionalChunks(const uint8_t** const data,149size_t* const data_size,150size_t const riff_size,151const uint8_t** const alpha_data,152size_t* const alpha_size) {153const uint8_t* buf;154size_t buf_size;155uint32_t total_size = TAG_SIZE + // "WEBP".156CHUNK_HEADER_SIZE + // "VP8Xnnnn".157VP8X_CHUNK_SIZE; // data.158assert(data != NULL);159assert(data_size != NULL);160buf = *data;161buf_size = *data_size;162163assert(alpha_data != NULL);164assert(alpha_size != NULL);165*alpha_data = NULL;166*alpha_size = 0;167168while (1) {169uint32_t chunk_size;170uint32_t disk_chunk_size; // chunk_size with padding171172*data = buf;173*data_size = buf_size;174175if (buf_size < CHUNK_HEADER_SIZE) { // Insufficient data.176return VP8_STATUS_NOT_ENOUGH_DATA;177}178179chunk_size = GetLE32(buf + TAG_SIZE);180if (chunk_size > MAX_CHUNK_PAYLOAD) {181return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size.182}183// For odd-sized chunk-payload, there's one byte padding at the end.184disk_chunk_size = (CHUNK_HEADER_SIZE + chunk_size + 1) & ~1u;185total_size += disk_chunk_size;186187// Check that total bytes skipped so far does not exceed riff_size.188if (riff_size > 0 && (total_size > riff_size)) {189return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size.190}191192// Start of a (possibly incomplete) VP8/VP8L chunk implies that we have193// parsed all the optional chunks.194// Note: This check must occur before the check 'buf_size < disk_chunk_size'195// below to allow incomplete VP8/VP8L chunks.196if (!memcmp(buf, "VP8 ", TAG_SIZE) ||197!memcmp(buf, "VP8L", TAG_SIZE)) {198return VP8_STATUS_OK;199}200201if (buf_size < disk_chunk_size) { // Insufficient data.202return VP8_STATUS_NOT_ENOUGH_DATA;203}204205if (!memcmp(buf, "ALPH", TAG_SIZE)) { // A valid ALPH header.206*alpha_data = buf + CHUNK_HEADER_SIZE;207*alpha_size = chunk_size;208}209210// We have a full and valid chunk; skip it.211buf += disk_chunk_size;212buf_size -= disk_chunk_size;213}214}215216// Validates the VP8/VP8L Header ("VP8 nnnn" or "VP8L nnnn") and skips over it.217// Returns VP8_STATUS_BITSTREAM_ERROR for invalid (chunk larger than218// riff_size) VP8/VP8L header,219// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and220// VP8_STATUS_OK otherwise.221// If a VP8/VP8L chunk is found, *chunk_size is set to the total number of bytes222// extracted from the VP8/VP8L chunk header.223// The flag '*is_lossless' is set to 1 in case of VP8L chunk / raw VP8L data.224static VP8StatusCode ParseVP8Header(const uint8_t** const data_ptr,225size_t* const data_size, int have_all_data,226size_t riff_size, size_t* const chunk_size,227int* const is_lossless) {228const uint8_t* const data = *data_ptr;229const int is_vp8 = !memcmp(data, "VP8 ", TAG_SIZE);230const int is_vp8l = !memcmp(data, "VP8L", TAG_SIZE);231const uint32_t minimal_size =232TAG_SIZE + CHUNK_HEADER_SIZE; // "WEBP" + "VP8 nnnn" OR233// "WEBP" + "VP8Lnnnn"234assert(data != NULL);235assert(data_size != NULL);236assert(chunk_size != NULL);237assert(is_lossless != NULL);238239if (*data_size < CHUNK_HEADER_SIZE) {240return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.241}242243if (is_vp8 || is_vp8l) {244// Bitstream contains VP8/VP8L header.245const uint32_t size = GetLE32(data + TAG_SIZE);246if ((riff_size >= minimal_size) && (size > riff_size - minimal_size)) {247return VP8_STATUS_BITSTREAM_ERROR; // Inconsistent size information.248}249if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) {250return VP8_STATUS_NOT_ENOUGH_DATA; // Truncated bitstream.251}252// Skip over CHUNK_HEADER_SIZE bytes from VP8/VP8L Header.253*chunk_size = size;254*data_ptr += CHUNK_HEADER_SIZE;255*data_size -= CHUNK_HEADER_SIZE;256*is_lossless = is_vp8l;257} else {258// Raw VP8/VP8L bitstream (no header).259*is_lossless = VP8LCheckSignature(data, *data_size);260*chunk_size = *data_size;261}262263return VP8_STATUS_OK;264}265266//------------------------------------------------------------------------------267268// Fetch '*width', '*height', '*has_alpha' and fill out 'headers' based on269// 'data'. All the output parameters may be NULL. If 'headers' is NULL only the270// minimal amount will be read to fetch the remaining parameters.271// If 'headers' is non-NULL this function will attempt to locate both alpha272// data (with or without a VP8X chunk) and the bitstream chunk (VP8/VP8L).273// Note: The following chunk sequences (before the raw VP8/VP8L data) are274// considered valid by this function:275// RIFF + VP8(L)276// RIFF + VP8X + (optional chunks) + VP8(L)277// ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose.278// VP8(L) <-- Not a valid WebP format: only allowed for internal purpose.279static VP8StatusCode ParseHeadersInternal(const uint8_t* data,280size_t data_size,281int* const width,282int* const height,283int* const has_alpha,284int* const has_animation,285int* const format,286WebPHeaderStructure* const headers) {287int canvas_width = 0;288int canvas_height = 0;289int image_width = 0;290int image_height = 0;291int found_riff = 0;292int found_vp8x = 0;293int animation_present = 0;294const int have_all_data = (headers != NULL) ? headers->have_all_data : 0;295296VP8StatusCode status;297WebPHeaderStructure hdrs;298299if (data == NULL || data_size < RIFF_HEADER_SIZE) {300return VP8_STATUS_NOT_ENOUGH_DATA;301}302memset(&hdrs, 0, sizeof(hdrs));303hdrs.data = data;304hdrs.data_size = data_size;305306// Skip over RIFF header.307status = ParseRIFF(&data, &data_size, have_all_data, &hdrs.riff_size);308if (status != VP8_STATUS_OK) {309return status; // Wrong RIFF header / insufficient data.310}311found_riff = (hdrs.riff_size > 0);312313// Skip over VP8X.314{315uint32_t flags = 0;316status = ParseVP8X(&data, &data_size, &found_vp8x,317&canvas_width, &canvas_height, &flags);318if (status != VP8_STATUS_OK) {319return status; // Wrong VP8X / insufficient data.320}321animation_present = !!(flags & ANIMATION_FLAG);322if (!found_riff && found_vp8x) {323// Note: This restriction may be removed in the future, if it becomes324// necessary to send VP8X chunk to the decoder.325return VP8_STATUS_BITSTREAM_ERROR;326}327if (has_alpha != NULL) *has_alpha = !!(flags & ALPHA_FLAG);328if (has_animation != NULL) *has_animation = animation_present;329if (format != NULL) *format = 0; // default = undefined330331image_width = canvas_width;332image_height = canvas_height;333if (found_vp8x && animation_present && headers == NULL) {334status = VP8_STATUS_OK;335goto ReturnWidthHeight; // Just return features from VP8X header.336}337}338339if (data_size < TAG_SIZE) {340status = VP8_STATUS_NOT_ENOUGH_DATA;341goto ReturnWidthHeight;342}343344// Skip over optional chunks if data started with "RIFF + VP8X" or "ALPH".345if ((found_riff && found_vp8x) ||346(!found_riff && !found_vp8x && !memcmp(data, "ALPH", TAG_SIZE))) {347status = ParseOptionalChunks(&data, &data_size, hdrs.riff_size,348&hdrs.alpha_data, &hdrs.alpha_data_size);349if (status != VP8_STATUS_OK) {350goto ReturnWidthHeight; // Invalid chunk size / insufficient data.351}352}353354// Skip over VP8/VP8L header.355status = ParseVP8Header(&data, &data_size, have_all_data, hdrs.riff_size,356&hdrs.compressed_size, &hdrs.is_lossless);357if (status != VP8_STATUS_OK) {358goto ReturnWidthHeight; // Wrong VP8/VP8L chunk-header / insufficient data.359}360if (hdrs.compressed_size > MAX_CHUNK_PAYLOAD) {361return VP8_STATUS_BITSTREAM_ERROR;362}363364if (format != NULL && !animation_present) {365*format = hdrs.is_lossless ? 2 : 1;366}367368if (!hdrs.is_lossless) {369if (data_size < VP8_FRAME_HEADER_SIZE) {370status = VP8_STATUS_NOT_ENOUGH_DATA;371goto ReturnWidthHeight;372}373// Validates raw VP8 data.374if (!VP8GetInfo(data, data_size, (uint32_t)hdrs.compressed_size,375&image_width, &image_height)) {376return VP8_STATUS_BITSTREAM_ERROR;377}378} else {379if (data_size < VP8L_FRAME_HEADER_SIZE) {380status = VP8_STATUS_NOT_ENOUGH_DATA;381goto ReturnWidthHeight;382}383// Validates raw VP8L data.384if (!VP8LGetInfo(data, data_size, &image_width, &image_height, has_alpha)) {385return VP8_STATUS_BITSTREAM_ERROR;386}387}388// Validates image size coherency.389if (found_vp8x) {390if (canvas_width != image_width || canvas_height != image_height) {391return VP8_STATUS_BITSTREAM_ERROR;392}393}394if (headers != NULL) {395*headers = hdrs;396headers->offset = data - headers->data;397assert((uint64_t)(data - headers->data) < MAX_CHUNK_PAYLOAD);398assert(headers->offset == headers->data_size - data_size);399}400ReturnWidthHeight:401if (status == VP8_STATUS_OK ||402(status == VP8_STATUS_NOT_ENOUGH_DATA && found_vp8x && headers == NULL)) {403if (has_alpha != NULL) {404// If the data did not contain a VP8X/VP8L chunk the only definitive way405// to set this is by looking for alpha data (from an ALPH chunk).406*has_alpha |= (hdrs.alpha_data != NULL);407}408if (width != NULL) *width = image_width;409if (height != NULL) *height = image_height;410return VP8_STATUS_OK;411} else {412return status;413}414}415416VP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers) {417// status is marked volatile as a workaround for a clang-3.8 (aarch64) bug418volatile VP8StatusCode status;419int has_animation = 0;420assert(headers != NULL);421// fill out headers, ignore width/height/has_alpha.422status = ParseHeadersInternal(headers->data, headers->data_size,423NULL, NULL, NULL, &has_animation,424NULL, headers);425if (status == VP8_STATUS_OK || status == VP8_STATUS_NOT_ENOUGH_DATA) {426// The WebPDemux API + libwebp can be used to decode individual427// uncomposited frames or the WebPAnimDecoder can be used to fully428// reconstruct them (see webp/demux.h).429if (has_animation) {430status = VP8_STATUS_UNSUPPORTED_FEATURE;431}432}433return status;434}435436//------------------------------------------------------------------------------437// WebPDecParams438439void WebPResetDecParams(WebPDecParams* const params) {440if (params != NULL) {441memset(params, 0, sizeof(*params));442}443}444445//------------------------------------------------------------------------------446// "Into" decoding variants447448// Main flow449WEBP_NODISCARD static VP8StatusCode DecodeInto(const uint8_t* const data,450size_t data_size,451WebPDecParams* const params) {452VP8StatusCode status;453VP8Io io;454WebPHeaderStructure headers;455456headers.data = data;457headers.data_size = data_size;458headers.have_all_data = 1;459status = WebPParseHeaders(&headers); // Process Pre-VP8 chunks.460if (status != VP8_STATUS_OK) {461return status;462}463464assert(params != NULL);465if (!VP8InitIo(&io)) {466return VP8_STATUS_INVALID_PARAM;467}468io.data = headers.data + headers.offset;469io.data_size = headers.data_size - headers.offset;470WebPInitCustomIo(params, &io); // Plug the I/O functions.471472if (!headers.is_lossless) {473VP8Decoder* const dec = VP8New();474if (dec == NULL) {475return VP8_STATUS_OUT_OF_MEMORY;476}477dec->alpha_data_ = headers.alpha_data;478dec->alpha_data_size_ = headers.alpha_data_size;479480// Decode bitstream header, update io->width/io->height.481if (!VP8GetHeaders(dec, &io)) {482status = dec->status_; // An error occurred. Grab error status.483} else {484// Allocate/check output buffers.485status = WebPAllocateDecBuffer(io.width, io.height, params->options,486params->output);487if (status == VP8_STATUS_OK) { // Decode488// This change must be done before calling VP8Decode()489dec->mt_method_ = VP8GetThreadMethod(params->options, &headers,490io.width, io.height);491VP8InitDithering(params->options, dec);492if (!VP8Decode(dec, &io)) {493status = dec->status_;494}495}496}497VP8Delete(dec);498} else {499VP8LDecoder* const dec = VP8LNew();500if (dec == NULL) {501return VP8_STATUS_OUT_OF_MEMORY;502}503if (!VP8LDecodeHeader(dec, &io)) {504status = dec->status_; // An error occurred. Grab error status.505} else {506// Allocate/check output buffers.507status = WebPAllocateDecBuffer(io.width, io.height, params->options,508params->output);509if (status == VP8_STATUS_OK) { // Decode510if (!VP8LDecodeImage(dec)) {511status = dec->status_;512}513}514}515VP8LDelete(dec);516}517518if (status != VP8_STATUS_OK) {519WebPFreeDecBuffer(params->output);520} else {521if (params->options != NULL && params->options->flip) {522// This restores the original stride values if options->flip was used523// during the call to WebPAllocateDecBuffer above.524status = WebPFlipBuffer(params->output);525}526}527return status;528}529530// Helpers531WEBP_NODISCARD static uint8_t* DecodeIntoRGBABuffer(WEBP_CSP_MODE colorspace,532const uint8_t* const data,533size_t data_size,534uint8_t* const rgba,535int stride, size_t size) {536WebPDecParams params;537WebPDecBuffer buf;538if (rgba == NULL || !WebPInitDecBuffer(&buf)) {539return NULL;540}541WebPResetDecParams(¶ms);542params.output = &buf;543buf.colorspace = colorspace;544buf.u.RGBA.rgba = rgba;545buf.u.RGBA.stride = stride;546buf.u.RGBA.size = size;547buf.is_external_memory = 1;548if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) {549return NULL;550}551return rgba;552}553554uint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size,555uint8_t* output, size_t size, int stride) {556return DecodeIntoRGBABuffer(MODE_RGB, data, data_size, output, stride, size);557}558559uint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size,560uint8_t* output, size_t size, int stride) {561return DecodeIntoRGBABuffer(MODE_RGBA, data, data_size, output, stride, size);562}563564uint8_t* WebPDecodeARGBInto(const uint8_t* data, size_t data_size,565uint8_t* output, size_t size, int stride) {566return DecodeIntoRGBABuffer(MODE_ARGB, data, data_size, output, stride, size);567}568569uint8_t* WebPDecodeBGRInto(const uint8_t* data, size_t data_size,570uint8_t* output, size_t size, int stride) {571return DecodeIntoRGBABuffer(MODE_BGR, data, data_size, output, stride, size);572}573574uint8_t* WebPDecodeBGRAInto(const uint8_t* data, size_t data_size,575uint8_t* output, size_t size, int stride) {576return DecodeIntoRGBABuffer(MODE_BGRA, data, data_size, output, stride, size);577}578579uint8_t* WebPDecodeYUVInto(const uint8_t* data, size_t data_size,580uint8_t* luma, size_t luma_size, int luma_stride,581uint8_t* u, size_t u_size, int u_stride,582uint8_t* v, size_t v_size, int v_stride) {583WebPDecParams params;584WebPDecBuffer output;585if (luma == NULL || !WebPInitDecBuffer(&output)) return NULL;586WebPResetDecParams(¶ms);587params.output = &output;588output.colorspace = MODE_YUV;589output.u.YUVA.y = luma;590output.u.YUVA.y_stride = luma_stride;591output.u.YUVA.y_size = luma_size;592output.u.YUVA.u = u;593output.u.YUVA.u_stride = u_stride;594output.u.YUVA.u_size = u_size;595output.u.YUVA.v = v;596output.u.YUVA.v_stride = v_stride;597output.u.YUVA.v_size = v_size;598output.is_external_memory = 1;599if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) {600return NULL;601}602return luma;603}604605//------------------------------------------------------------------------------606607WEBP_NODISCARD static uint8_t* Decode(WEBP_CSP_MODE mode,608const uint8_t* const data,609size_t data_size, int* const width,610int* const height,611WebPDecBuffer* const keep_info) {612WebPDecParams params;613WebPDecBuffer output;614615if (!WebPInitDecBuffer(&output)) {616return NULL;617}618WebPResetDecParams(¶ms);619params.output = &output;620output.colorspace = mode;621622// Retrieve (and report back) the required dimensions from bitstream.623if (!WebPGetInfo(data, data_size, &output.width, &output.height)) {624return NULL;625}626if (width != NULL) *width = output.width;627if (height != NULL) *height = output.height;628629// Decode630if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) {631return NULL;632}633if (keep_info != NULL) { // keep track of the side-info634WebPCopyDecBuffer(&output, keep_info);635}636// return decoded samples (don't clear 'output'!)637return WebPIsRGBMode(mode) ? output.u.RGBA.rgba : output.u.YUVA.y;638}639640uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size,641int* width, int* height) {642return Decode(MODE_RGB, data, data_size, width, height, NULL);643}644645uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size,646int* width, int* height) {647return Decode(MODE_RGBA, data, data_size, width, height, NULL);648}649650uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size,651int* width, int* height) {652return Decode(MODE_ARGB, data, data_size, width, height, NULL);653}654655uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size,656int* width, int* height) {657return Decode(MODE_BGR, data, data_size, width, height, NULL);658}659660uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size,661int* width, int* height) {662return Decode(MODE_BGRA, data, data_size, width, height, NULL);663}664665uint8_t* WebPDecodeYUV(const uint8_t* data, size_t data_size,666int* width, int* height, uint8_t** u, uint8_t** v,667int* stride, int* uv_stride) {668// data, width and height are checked by Decode().669if (u == NULL || v == NULL || stride == NULL || uv_stride == NULL) {670return NULL;671}672673{674WebPDecBuffer output; // only to preserve the side-infos675uint8_t* const out = Decode(MODE_YUV, data, data_size,676width, height, &output);677678if (out != NULL) {679const WebPYUVABuffer* const buf = &output.u.YUVA;680*u = buf->u;681*v = buf->v;682*stride = buf->y_stride;683*uv_stride = buf->u_stride;684assert(buf->u_stride == buf->v_stride);685}686return out;687}688}689690static void DefaultFeatures(WebPBitstreamFeatures* const features) {691assert(features != NULL);692memset(features, 0, sizeof(*features));693}694695static VP8StatusCode GetFeatures(const uint8_t* const data, size_t data_size,696WebPBitstreamFeatures* const features) {697if (features == NULL || data == NULL) {698return VP8_STATUS_INVALID_PARAM;699}700DefaultFeatures(features);701702// Only parse enough of the data to retrieve the features.703return ParseHeadersInternal(data, data_size,704&features->width, &features->height,705&features->has_alpha, &features->has_animation,706&features->format, NULL);707}708709//------------------------------------------------------------------------------710// WebPGetInfo()711712int WebPGetInfo(const uint8_t* data, size_t data_size,713int* width, int* height) {714WebPBitstreamFeatures features;715716if (GetFeatures(data, data_size, &features) != VP8_STATUS_OK) {717return 0;718}719720if (width != NULL) {721*width = features.width;722}723if (height != NULL) {724*height = features.height;725}726727return 1;728}729730//------------------------------------------------------------------------------731// Advance decoding API732733int WebPInitDecoderConfigInternal(WebPDecoderConfig* config,734int version) {735if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {736return 0; // version mismatch737}738if (config == NULL) {739return 0;740}741memset(config, 0, sizeof(*config));742DefaultFeatures(&config->input);743if (!WebPInitDecBuffer(&config->output)) {744return 0;745}746return 1;747}748749VP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, size_t data_size,750WebPBitstreamFeatures* features,751int version) {752if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {753return VP8_STATUS_INVALID_PARAM; // version mismatch754}755if (features == NULL) {756return VP8_STATUS_INVALID_PARAM;757}758return GetFeatures(data, data_size, features);759}760761VP8StatusCode WebPDecode(const uint8_t* data, size_t data_size,762WebPDecoderConfig* config) {763WebPDecParams params;764VP8StatusCode status;765766if (config == NULL) {767return VP8_STATUS_INVALID_PARAM;768}769770status = GetFeatures(data, data_size, &config->input);771if (status != VP8_STATUS_OK) {772if (status == VP8_STATUS_NOT_ENOUGH_DATA) {773return VP8_STATUS_BITSTREAM_ERROR; // Not-enough-data treated as error.774}775return status;776}777778WebPResetDecParams(¶ms);779params.options = &config->options;780params.output = &config->output;781if (WebPAvoidSlowMemory(params.output, &config->input)) {782// decoding to slow memory: use a temporary in-mem buffer to decode into.783WebPDecBuffer in_mem_buffer;784if (!WebPInitDecBuffer(&in_mem_buffer)) {785return VP8_STATUS_INVALID_PARAM;786}787in_mem_buffer.colorspace = config->output.colorspace;788in_mem_buffer.width = config->input.width;789in_mem_buffer.height = config->input.height;790params.output = &in_mem_buffer;791status = DecodeInto(data, data_size, ¶ms);792if (status == VP8_STATUS_OK) { // do the slow-copy793status = WebPCopyDecBufferPixels(&in_mem_buffer, &config->output);794}795WebPFreeDecBuffer(&in_mem_buffer);796} else {797status = DecodeInto(data, data_size, ¶ms);798}799800return status;801}802803//------------------------------------------------------------------------------804// Cropping and rescaling.805806int WebPCheckCropDimensions(int image_width, int image_height,807int x, int y, int w, int h) {808return !(x < 0 || y < 0 || w <= 0 || h <= 0 ||809x >= image_width || w > image_width || w > image_width - x ||810y >= image_height || h > image_height || h > image_height - y);811}812813int WebPIoInitFromOptions(const WebPDecoderOptions* const options,814VP8Io* const io, WEBP_CSP_MODE src_colorspace) {815const int W = io->width;816const int H = io->height;817int x = 0, y = 0, w = W, h = H;818819// Cropping820io->use_cropping = (options != NULL) && options->use_cropping;821if (io->use_cropping) {822w = options->crop_width;823h = options->crop_height;824x = options->crop_left;825y = options->crop_top;826if (!WebPIsRGBMode(src_colorspace)) { // only snap for YUV420827x &= ~1;828y &= ~1;829}830if (!WebPCheckCropDimensions(W, H, x, y, w, h)) {831return 0; // out of frame boundary error832}833}834io->crop_left = x;835io->crop_top = y;836io->crop_right = x + w;837io->crop_bottom = y + h;838io->mb_w = w;839io->mb_h = h;840841// Scaling842io->use_scaling = (options != NULL) && options->use_scaling;843if (io->use_scaling) {844int scaled_width = options->scaled_width;845int scaled_height = options->scaled_height;846if (!WebPRescalerGetScaledDimensions(w, h, &scaled_width, &scaled_height)) {847return 0;848}849io->scaled_width = scaled_width;850io->scaled_height = scaled_height;851}852853// Filter854io->bypass_filtering = (options != NULL) && options->bypass_filtering;855856// Fancy upsampler857#ifdef FANCY_UPSAMPLING858io->fancy_upsampling = (options == NULL) || (!options->no_fancy_upsampling);859#endif860861if (io->use_scaling) {862// disable filter (only for large downscaling ratio).863io->bypass_filtering |= (io->scaled_width < W * 3 / 4) &&864(io->scaled_height < H * 3 / 4);865io->fancy_upsampling = 0;866}867return 1;868}869870//------------------------------------------------------------------------------871872873