Path: blob/master/3rdparty/libwebp/src/dec/webp_dec.c
16358 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/vp8i_dec.h"16#include "src/dec/vp8li_dec.h"17#include "src/dec/webpi_dec.h"18#include "src/utils/utils.h"19#include "src/webp/mux_types.h" // ALPHA_FLAG2021//------------------------------------------------------------------------------22// RIFF layout is:23// Offset tag24// 0...3 "RIFF" 4-byte tag25// 4...7 size of image data (including metadata) starting at offset 826// 8...11 "WEBP" our form-type signature27// The RIFF container (12 bytes) is followed by appropriate chunks:28// 12..15 "VP8 ": 4-bytes tags, signaling the use of VP8 video format29// 16..19 size of the raw VP8 image data, starting at offset 2030// 20.... the VP8 bytes31// Or,32// 12..15 "VP8L": 4-bytes tags, signaling the use of VP8L lossless format33// 16..19 size of the raw VP8L image data, starting at offset 2034// 20.... the VP8L bytes35// Or,36// 12..15 "VP8X": 4-bytes tags, describing the extended-VP8 chunk.37// 16..19 size of the VP8X chunk starting at offset 20.38// 20..23 VP8X flags bit-map corresponding to the chunk-types present.39// 24..26 Width of the Canvas Image.40// 27..29 Height of the Canvas Image.41// There can be extra chunks after the "VP8X" chunk (ICCP, ANMF, VP8, VP8L,42// XMP, EXIF ...)43// All sizes are in little-endian order.44// Note: chunk data size must be padded to multiple of 2 when written.4546// Validates the RIFF container (if detected) and skips over it.47// If a RIFF container is detected, returns:48// VP8_STATUS_BITSTREAM_ERROR for invalid header,49// VP8_STATUS_NOT_ENOUGH_DATA for truncated data if have_all_data is true,50// and VP8_STATUS_OK otherwise.51// In case there are not enough bytes (partial RIFF container), return 0 for52// *riff_size. Else return the RIFF size extracted from the header.53static VP8StatusCode ParseRIFF(const uint8_t** const data,54size_t* const data_size, int have_all_data,55size_t* const riff_size) {56assert(data != NULL);57assert(data_size != NULL);58assert(riff_size != NULL);5960*riff_size = 0; // Default: no RIFF present.61if (*data_size >= RIFF_HEADER_SIZE && !memcmp(*data, "RIFF", TAG_SIZE)) {62if (memcmp(*data + 8, "WEBP", TAG_SIZE)) {63return VP8_STATUS_BITSTREAM_ERROR; // Wrong image file signature.64} else {65const uint32_t size = GetLE32(*data + TAG_SIZE);66// Check that we have at least one chunk (i.e "WEBP" + "VP8?nnnn").67if (size < TAG_SIZE + CHUNK_HEADER_SIZE) {68return VP8_STATUS_BITSTREAM_ERROR;69}70if (size > MAX_CHUNK_PAYLOAD) {71return VP8_STATUS_BITSTREAM_ERROR;72}73if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) {74return VP8_STATUS_NOT_ENOUGH_DATA; // Truncated bitstream.75}76// We have a RIFF container. Skip it.77*riff_size = size;78*data += RIFF_HEADER_SIZE;79*data_size -= RIFF_HEADER_SIZE;80}81}82return VP8_STATUS_OK;83}8485// Validates the VP8X header and skips over it.86// Returns VP8_STATUS_BITSTREAM_ERROR for invalid VP8X header,87// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and88// VP8_STATUS_OK otherwise.89// If a VP8X chunk is found, found_vp8x is set to true and *width_ptr,90// *height_ptr and *flags_ptr are set to the corresponding values extracted91// from the VP8X chunk.92static VP8StatusCode ParseVP8X(const uint8_t** const data,93size_t* const data_size,94int* const found_vp8x,95int* const width_ptr, int* const height_ptr,96uint32_t* const flags_ptr) {97const uint32_t vp8x_size = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;98assert(data != NULL);99assert(data_size != NULL);100assert(found_vp8x != NULL);101102*found_vp8x = 0;103104if (*data_size < CHUNK_HEADER_SIZE) {105return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.106}107108if (!memcmp(*data, "VP8X", TAG_SIZE)) {109int width, height;110uint32_t flags;111const uint32_t chunk_size = GetLE32(*data + TAG_SIZE);112if (chunk_size != VP8X_CHUNK_SIZE) {113return VP8_STATUS_BITSTREAM_ERROR; // Wrong chunk size.114}115116// Verify if enough data is available to validate the VP8X chunk.117if (*data_size < vp8x_size) {118return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.119}120flags = GetLE32(*data + 8);121width = 1 + GetLE24(*data + 12);122height = 1 + GetLE24(*data + 15);123if (width * (uint64_t)height >= MAX_IMAGE_AREA) {124return VP8_STATUS_BITSTREAM_ERROR; // image is too large125}126127if (flags_ptr != NULL) *flags_ptr = flags;128if (width_ptr != NULL) *width_ptr = width;129if (height_ptr != NULL) *height_ptr = height;130// Skip over VP8X header bytes.131*data += vp8x_size;132*data_size -= vp8x_size;133*found_vp8x = 1;134}135return VP8_STATUS_OK;136}137138// Skips to the next VP8/VP8L chunk header in the data given the size of the139// RIFF chunk 'riff_size'.140// Returns VP8_STATUS_BITSTREAM_ERROR if any invalid chunk size is encountered,141// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and142// VP8_STATUS_OK otherwise.143// If an alpha chunk is found, *alpha_data and *alpha_size are set144// appropriately.145static VP8StatusCode ParseOptionalChunks(const uint8_t** const data,146size_t* const data_size,147size_t const riff_size,148const uint8_t** const alpha_data,149size_t* const alpha_size) {150const uint8_t* buf;151size_t buf_size;152uint32_t total_size = TAG_SIZE + // "WEBP".153CHUNK_HEADER_SIZE + // "VP8Xnnnn".154VP8X_CHUNK_SIZE; // data.155assert(data != NULL);156assert(data_size != NULL);157buf = *data;158buf_size = *data_size;159160assert(alpha_data != NULL);161assert(alpha_size != NULL);162*alpha_data = NULL;163*alpha_size = 0;164165while (1) {166uint32_t chunk_size;167uint32_t disk_chunk_size; // chunk_size with padding168169*data = buf;170*data_size = buf_size;171172if (buf_size < CHUNK_HEADER_SIZE) { // Insufficient data.173return VP8_STATUS_NOT_ENOUGH_DATA;174}175176chunk_size = GetLE32(buf + TAG_SIZE);177if (chunk_size > MAX_CHUNK_PAYLOAD) {178return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size.179}180// For odd-sized chunk-payload, there's one byte padding at the end.181disk_chunk_size = (CHUNK_HEADER_SIZE + chunk_size + 1) & ~1;182total_size += disk_chunk_size;183184// Check that total bytes skipped so far does not exceed riff_size.185if (riff_size > 0 && (total_size > riff_size)) {186return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size.187}188189// Start of a (possibly incomplete) VP8/VP8L chunk implies that we have190// parsed all the optional chunks.191// Note: This check must occur before the check 'buf_size < disk_chunk_size'192// below to allow incomplete VP8/VP8L chunks.193if (!memcmp(buf, "VP8 ", TAG_SIZE) ||194!memcmp(buf, "VP8L", TAG_SIZE)) {195return VP8_STATUS_OK;196}197198if (buf_size < disk_chunk_size) { // Insufficient data.199return VP8_STATUS_NOT_ENOUGH_DATA;200}201202if (!memcmp(buf, "ALPH", TAG_SIZE)) { // A valid ALPH header.203*alpha_data = buf + CHUNK_HEADER_SIZE;204*alpha_size = chunk_size;205}206207// We have a full and valid chunk; skip it.208buf += disk_chunk_size;209buf_size -= disk_chunk_size;210}211}212213// Validates the VP8/VP8L Header ("VP8 nnnn" or "VP8L nnnn") and skips over it.214// Returns VP8_STATUS_BITSTREAM_ERROR for invalid (chunk larger than215// riff_size) VP8/VP8L header,216// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and217// VP8_STATUS_OK otherwise.218// If a VP8/VP8L chunk is found, *chunk_size is set to the total number of bytes219// extracted from the VP8/VP8L chunk header.220// The flag '*is_lossless' is set to 1 in case of VP8L chunk / raw VP8L data.221static VP8StatusCode ParseVP8Header(const uint8_t** const data_ptr,222size_t* const data_size, int have_all_data,223size_t riff_size, size_t* const chunk_size,224int* const is_lossless) {225const uint8_t* const data = *data_ptr;226const int is_vp8 = !memcmp(data, "VP8 ", TAG_SIZE);227const int is_vp8l = !memcmp(data, "VP8L", TAG_SIZE);228const uint32_t minimal_size =229TAG_SIZE + CHUNK_HEADER_SIZE; // "WEBP" + "VP8 nnnn" OR230// "WEBP" + "VP8Lnnnn"231assert(data != NULL);232assert(data_size != NULL);233assert(chunk_size != NULL);234assert(is_lossless != NULL);235236if (*data_size < CHUNK_HEADER_SIZE) {237return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.238}239240if (is_vp8 || is_vp8l) {241// Bitstream contains VP8/VP8L header.242const uint32_t size = GetLE32(data + TAG_SIZE);243if ((riff_size >= minimal_size) && (size > riff_size - minimal_size)) {244return VP8_STATUS_BITSTREAM_ERROR; // Inconsistent size information.245}246if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) {247return VP8_STATUS_NOT_ENOUGH_DATA; // Truncated bitstream.248}249// Skip over CHUNK_HEADER_SIZE bytes from VP8/VP8L Header.250*chunk_size = size;251*data_ptr += CHUNK_HEADER_SIZE;252*data_size -= CHUNK_HEADER_SIZE;253*is_lossless = is_vp8l;254} else {255// Raw VP8/VP8L bitstream (no header).256*is_lossless = VP8LCheckSignature(data, *data_size);257*chunk_size = *data_size;258}259260return VP8_STATUS_OK;261}262263//------------------------------------------------------------------------------264265// Fetch '*width', '*height', '*has_alpha' and fill out 'headers' based on266// 'data'. All the output parameters may be NULL. If 'headers' is NULL only the267// minimal amount will be read to fetch the remaining parameters.268// If 'headers' is non-NULL this function will attempt to locate both alpha269// data (with or without a VP8X chunk) and the bitstream chunk (VP8/VP8L).270// Note: The following chunk sequences (before the raw VP8/VP8L data) are271// considered valid by this function:272// RIFF + VP8(L)273// RIFF + VP8X + (optional chunks) + VP8(L)274// ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose.275// VP8(L) <-- Not a valid WebP format: only allowed for internal purpose.276static VP8StatusCode ParseHeadersInternal(const uint8_t* data,277size_t data_size,278int* const width,279int* const height,280int* const has_alpha,281int* const has_animation,282int* const format,283WebPHeaderStructure* const headers) {284int canvas_width = 0;285int canvas_height = 0;286int image_width = 0;287int image_height = 0;288int found_riff = 0;289int found_vp8x = 0;290int animation_present = 0;291const int have_all_data = (headers != NULL) ? headers->have_all_data : 0;292293VP8StatusCode status;294WebPHeaderStructure hdrs;295296if (data == NULL || data_size < RIFF_HEADER_SIZE) {297return VP8_STATUS_NOT_ENOUGH_DATA;298}299memset(&hdrs, 0, sizeof(hdrs));300hdrs.data = data;301hdrs.data_size = data_size;302303// Skip over RIFF header.304status = ParseRIFF(&data, &data_size, have_all_data, &hdrs.riff_size);305if (status != VP8_STATUS_OK) {306return status; // Wrong RIFF header / insufficient data.307}308found_riff = (hdrs.riff_size > 0);309310// Skip over VP8X.311{312uint32_t flags = 0;313status = ParseVP8X(&data, &data_size, &found_vp8x,314&canvas_width, &canvas_height, &flags);315if (status != VP8_STATUS_OK) {316return status; // Wrong VP8X / insufficient data.317}318animation_present = !!(flags & ANIMATION_FLAG);319if (!found_riff && found_vp8x) {320// Note: This restriction may be removed in the future, if it becomes321// necessary to send VP8X chunk to the decoder.322return VP8_STATUS_BITSTREAM_ERROR;323}324if (has_alpha != NULL) *has_alpha = !!(flags & ALPHA_FLAG);325if (has_animation != NULL) *has_animation = animation_present;326if (format != NULL) *format = 0; // default = undefined327328image_width = canvas_width;329image_height = canvas_height;330if (found_vp8x && animation_present && headers == NULL) {331status = VP8_STATUS_OK;332goto ReturnWidthHeight; // Just return features from VP8X header.333}334}335336if (data_size < TAG_SIZE) {337status = VP8_STATUS_NOT_ENOUGH_DATA;338goto ReturnWidthHeight;339}340341// Skip over optional chunks if data started with "RIFF + VP8X" or "ALPH".342if ((found_riff && found_vp8x) ||343(!found_riff && !found_vp8x && !memcmp(data, "ALPH", TAG_SIZE))) {344status = ParseOptionalChunks(&data, &data_size, hdrs.riff_size,345&hdrs.alpha_data, &hdrs.alpha_data_size);346if (status != VP8_STATUS_OK) {347goto ReturnWidthHeight; // Invalid chunk size / insufficient data.348}349}350351// Skip over VP8/VP8L header.352status = ParseVP8Header(&data, &data_size, have_all_data, hdrs.riff_size,353&hdrs.compressed_size, &hdrs.is_lossless);354if (status != VP8_STATUS_OK) {355goto ReturnWidthHeight; // Wrong VP8/VP8L chunk-header / insufficient data.356}357if (hdrs.compressed_size > MAX_CHUNK_PAYLOAD) {358return VP8_STATUS_BITSTREAM_ERROR;359}360361if (format != NULL && !animation_present) {362*format = hdrs.is_lossless ? 2 : 1;363}364365if (!hdrs.is_lossless) {366if (data_size < VP8_FRAME_HEADER_SIZE) {367status = VP8_STATUS_NOT_ENOUGH_DATA;368goto ReturnWidthHeight;369}370// Validates raw VP8 data.371if (!VP8GetInfo(data, data_size, (uint32_t)hdrs.compressed_size,372&image_width, &image_height)) {373return VP8_STATUS_BITSTREAM_ERROR;374}375} else {376if (data_size < VP8L_FRAME_HEADER_SIZE) {377status = VP8_STATUS_NOT_ENOUGH_DATA;378goto ReturnWidthHeight;379}380// Validates raw VP8L data.381if (!VP8LGetInfo(data, data_size, &image_width, &image_height, has_alpha)) {382return VP8_STATUS_BITSTREAM_ERROR;383}384}385// Validates image size coherency.386if (found_vp8x) {387if (canvas_width != image_width || canvas_height != image_height) {388return VP8_STATUS_BITSTREAM_ERROR;389}390}391if (headers != NULL) {392*headers = hdrs;393headers->offset = data - headers->data;394assert((uint64_t)(data - headers->data) < MAX_CHUNK_PAYLOAD);395assert(headers->offset == headers->data_size - data_size);396}397ReturnWidthHeight:398if (status == VP8_STATUS_OK ||399(status == VP8_STATUS_NOT_ENOUGH_DATA && found_vp8x && headers == NULL)) {400if (has_alpha != NULL) {401// If the data did not contain a VP8X/VP8L chunk the only definitive way402// to set this is by looking for alpha data (from an ALPH chunk).403*has_alpha |= (hdrs.alpha_data != NULL);404}405if (width != NULL) *width = image_width;406if (height != NULL) *height = image_height;407return VP8_STATUS_OK;408} else {409return status;410}411}412413VP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers) {414// status is marked volatile as a workaround for a clang-3.8 (aarch64) bug415volatile VP8StatusCode status;416int has_animation = 0;417assert(headers != NULL);418// fill out headers, ignore width/height/has_alpha.419status = ParseHeadersInternal(headers->data, headers->data_size,420NULL, NULL, NULL, &has_animation,421NULL, headers);422if (status == VP8_STATUS_OK || status == VP8_STATUS_NOT_ENOUGH_DATA) {423// The WebPDemux API + libwebp can be used to decode individual424// uncomposited frames or the WebPAnimDecoder can be used to fully425// reconstruct them (see webp/demux.h).426if (has_animation) {427status = VP8_STATUS_UNSUPPORTED_FEATURE;428}429}430return status;431}432433//------------------------------------------------------------------------------434// WebPDecParams435436void WebPResetDecParams(WebPDecParams* const params) {437if (params != NULL) {438memset(params, 0, sizeof(*params));439}440}441442//------------------------------------------------------------------------------443// "Into" decoding variants444445// Main flow446static VP8StatusCode DecodeInto(const uint8_t* const data, size_t data_size,447WebPDecParams* const params) {448VP8StatusCode status;449VP8Io io;450WebPHeaderStructure headers;451452headers.data = data;453headers.data_size = data_size;454headers.have_all_data = 1;455status = WebPParseHeaders(&headers); // Process Pre-VP8 chunks.456if (status != VP8_STATUS_OK) {457return status;458}459460assert(params != NULL);461VP8InitIo(&io);462io.data = headers.data + headers.offset;463io.data_size = headers.data_size - headers.offset;464WebPInitCustomIo(params, &io); // Plug the I/O functions.465466if (!headers.is_lossless) {467VP8Decoder* const dec = VP8New();468if (dec == NULL) {469return VP8_STATUS_OUT_OF_MEMORY;470}471dec->alpha_data_ = headers.alpha_data;472dec->alpha_data_size_ = headers.alpha_data_size;473474// Decode bitstream header, update io->width/io->height.475if (!VP8GetHeaders(dec, &io)) {476status = dec->status_; // An error occurred. Grab error status.477} else {478// Allocate/check output buffers.479status = WebPAllocateDecBuffer(io.width, io.height, params->options,480params->output);481if (status == VP8_STATUS_OK) { // Decode482// This change must be done before calling VP8Decode()483dec->mt_method_ = VP8GetThreadMethod(params->options, &headers,484io.width, io.height);485VP8InitDithering(params->options, dec);486if (!VP8Decode(dec, &io)) {487status = dec->status_;488}489}490}491VP8Delete(dec);492} else {493VP8LDecoder* const dec = VP8LNew();494if (dec == NULL) {495return VP8_STATUS_OUT_OF_MEMORY;496}497if (!VP8LDecodeHeader(dec, &io)) {498status = dec->status_; // An error occurred. Grab error status.499} else {500// Allocate/check output buffers.501status = WebPAllocateDecBuffer(io.width, io.height, params->options,502params->output);503if (status == VP8_STATUS_OK) { // Decode504if (!VP8LDecodeImage(dec)) {505status = dec->status_;506}507}508}509VP8LDelete(dec);510}511512if (status != VP8_STATUS_OK) {513WebPFreeDecBuffer(params->output);514} else {515if (params->options != NULL && params->options->flip) {516// This restores the original stride values if options->flip was used517// during the call to WebPAllocateDecBuffer above.518status = WebPFlipBuffer(params->output);519}520}521return status;522}523524// Helpers525static uint8_t* DecodeIntoRGBABuffer(WEBP_CSP_MODE colorspace,526const uint8_t* const data,527size_t data_size,528uint8_t* const rgba,529int stride, size_t size) {530WebPDecParams params;531WebPDecBuffer buf;532if (rgba == NULL) {533return NULL;534}535WebPInitDecBuffer(&buf);536WebPResetDecParams(¶ms);537params.output = &buf;538buf.colorspace = colorspace;539buf.u.RGBA.rgba = rgba;540buf.u.RGBA.stride = stride;541buf.u.RGBA.size = size;542buf.is_external_memory = 1;543if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) {544return NULL;545}546return rgba;547}548549uint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size,550uint8_t* output, size_t size, int stride) {551return DecodeIntoRGBABuffer(MODE_RGB, data, data_size, output, stride, size);552}553554uint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size,555uint8_t* output, size_t size, int stride) {556return DecodeIntoRGBABuffer(MODE_RGBA, data, data_size, output, stride, size);557}558559uint8_t* WebPDecodeARGBInto(const uint8_t* data, size_t data_size,560uint8_t* output, size_t size, int stride) {561return DecodeIntoRGBABuffer(MODE_ARGB, data, data_size, output, stride, size);562}563564uint8_t* WebPDecodeBGRInto(const uint8_t* data, size_t data_size,565uint8_t* output, size_t size, int stride) {566return DecodeIntoRGBABuffer(MODE_BGR, data, data_size, output, stride, size);567}568569uint8_t* WebPDecodeBGRAInto(const uint8_t* data, size_t data_size,570uint8_t* output, size_t size, int stride) {571return DecodeIntoRGBABuffer(MODE_BGRA, data, data_size, output, stride, size);572}573574uint8_t* WebPDecodeYUVInto(const uint8_t* data, size_t data_size,575uint8_t* luma, size_t luma_size, int luma_stride,576uint8_t* u, size_t u_size, int u_stride,577uint8_t* v, size_t v_size, int v_stride) {578WebPDecParams params;579WebPDecBuffer output;580if (luma == NULL) return NULL;581WebPInitDecBuffer(&output);582WebPResetDecParams(¶ms);583params.output = &output;584output.colorspace = MODE_YUV;585output.u.YUVA.y = luma;586output.u.YUVA.y_stride = luma_stride;587output.u.YUVA.y_size = luma_size;588output.u.YUVA.u = u;589output.u.YUVA.u_stride = u_stride;590output.u.YUVA.u_size = u_size;591output.u.YUVA.v = v;592output.u.YUVA.v_stride = v_stride;593output.u.YUVA.v_size = v_size;594output.is_external_memory = 1;595if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) {596return NULL;597}598return luma;599}600601//------------------------------------------------------------------------------602603static uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* const data,604size_t data_size, int* const width, int* const height,605WebPDecBuffer* const keep_info) {606WebPDecParams params;607WebPDecBuffer output;608609WebPInitDecBuffer(&output);610WebPResetDecParams(¶ms);611params.output = &output;612output.colorspace = mode;613614// Retrieve (and report back) the required dimensions from bitstream.615if (!WebPGetInfo(data, data_size, &output.width, &output.height)) {616return NULL;617}618if (width != NULL) *width = output.width;619if (height != NULL) *height = output.height;620621// Decode622if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) {623return NULL;624}625if (keep_info != NULL) { // keep track of the side-info626WebPCopyDecBuffer(&output, keep_info);627}628// return decoded samples (don't clear 'output'!)629return WebPIsRGBMode(mode) ? output.u.RGBA.rgba : output.u.YUVA.y;630}631632uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size,633int* width, int* height) {634return Decode(MODE_RGB, data, data_size, width, height, NULL);635}636637uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size,638int* width, int* height) {639return Decode(MODE_RGBA, data, data_size, width, height, NULL);640}641642uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size,643int* width, int* height) {644return Decode(MODE_ARGB, data, data_size, width, height, NULL);645}646647uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size,648int* width, int* height) {649return Decode(MODE_BGR, data, data_size, width, height, NULL);650}651652uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size,653int* width, int* height) {654return Decode(MODE_BGRA, data, data_size, width, height, NULL);655}656657uint8_t* WebPDecodeYUV(const uint8_t* data, size_t data_size,658int* width, int* height, uint8_t** u, uint8_t** v,659int* stride, int* uv_stride) {660WebPDecBuffer output; // only to preserve the side-infos661uint8_t* const out = Decode(MODE_YUV, data, data_size,662width, height, &output);663664if (out != NULL) {665const WebPYUVABuffer* const buf = &output.u.YUVA;666*u = buf->u;667*v = buf->v;668*stride = buf->y_stride;669*uv_stride = buf->u_stride;670assert(buf->u_stride == buf->v_stride);671}672return out;673}674675static void DefaultFeatures(WebPBitstreamFeatures* const features) {676assert(features != NULL);677memset(features, 0, sizeof(*features));678}679680static VP8StatusCode GetFeatures(const uint8_t* const data, size_t data_size,681WebPBitstreamFeatures* const features) {682if (features == NULL || data == NULL) {683return VP8_STATUS_INVALID_PARAM;684}685DefaultFeatures(features);686687// Only parse enough of the data to retrieve the features.688return ParseHeadersInternal(data, data_size,689&features->width, &features->height,690&features->has_alpha, &features->has_animation,691&features->format, NULL);692}693694//------------------------------------------------------------------------------695// WebPGetInfo()696697int WebPGetInfo(const uint8_t* data, size_t data_size,698int* width, int* height) {699WebPBitstreamFeatures features;700701if (GetFeatures(data, data_size, &features) != VP8_STATUS_OK) {702return 0;703}704705if (width != NULL) {706*width = features.width;707}708if (height != NULL) {709*height = features.height;710}711712return 1;713}714715//------------------------------------------------------------------------------716// Advance decoding API717718int WebPInitDecoderConfigInternal(WebPDecoderConfig* config,719int version) {720if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {721return 0; // version mismatch722}723if (config == NULL) {724return 0;725}726memset(config, 0, sizeof(*config));727DefaultFeatures(&config->input);728WebPInitDecBuffer(&config->output);729return 1;730}731732VP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, size_t data_size,733WebPBitstreamFeatures* features,734int version) {735if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {736return VP8_STATUS_INVALID_PARAM; // version mismatch737}738if (features == NULL) {739return VP8_STATUS_INVALID_PARAM;740}741return GetFeatures(data, data_size, features);742}743744VP8StatusCode WebPDecode(const uint8_t* data, size_t data_size,745WebPDecoderConfig* config) {746WebPDecParams params;747VP8StatusCode status;748749if (config == NULL) {750return VP8_STATUS_INVALID_PARAM;751}752753status = GetFeatures(data, data_size, &config->input);754if (status != VP8_STATUS_OK) {755if (status == VP8_STATUS_NOT_ENOUGH_DATA) {756return VP8_STATUS_BITSTREAM_ERROR; // Not-enough-data treated as error.757}758return status;759}760761WebPResetDecParams(¶ms);762params.options = &config->options;763params.output = &config->output;764if (WebPAvoidSlowMemory(params.output, &config->input)) {765// decoding to slow memory: use a temporary in-mem buffer to decode into.766WebPDecBuffer in_mem_buffer;767WebPInitDecBuffer(&in_mem_buffer);768in_mem_buffer.colorspace = config->output.colorspace;769in_mem_buffer.width = config->input.width;770in_mem_buffer.height = config->input.height;771params.output = &in_mem_buffer;772status = DecodeInto(data, data_size, ¶ms);773if (status == VP8_STATUS_OK) { // do the slow-copy774status = WebPCopyDecBufferPixels(&in_mem_buffer, &config->output);775}776WebPFreeDecBuffer(&in_mem_buffer);777} else {778status = DecodeInto(data, data_size, ¶ms);779}780781return status;782}783784//------------------------------------------------------------------------------785// Cropping and rescaling.786787int WebPIoInitFromOptions(const WebPDecoderOptions* const options,788VP8Io* const io, WEBP_CSP_MODE src_colorspace) {789const int W = io->width;790const int H = io->height;791int x = 0, y = 0, w = W, h = H;792793// Cropping794io->use_cropping = (options != NULL) && (options->use_cropping > 0);795if (io->use_cropping) {796w = options->crop_width;797h = options->crop_height;798x = options->crop_left;799y = options->crop_top;800if (!WebPIsRGBMode(src_colorspace)) { // only snap for YUV420801x &= ~1;802y &= ~1;803}804if (x < 0 || y < 0 || w <= 0 || h <= 0 || x + w > W || y + h > H) {805return 0; // out of frame boundary error806}807}808io->crop_left = x;809io->crop_top = y;810io->crop_right = x + w;811io->crop_bottom = y + h;812io->mb_w = w;813io->mb_h = h;814815// Scaling816io->use_scaling = (options != NULL) && (options->use_scaling > 0);817if (io->use_scaling) {818int scaled_width = options->scaled_width;819int scaled_height = options->scaled_height;820if (!WebPRescalerGetScaledDimensions(w, h, &scaled_width, &scaled_height)) {821return 0;822}823io->scaled_width = scaled_width;824io->scaled_height = scaled_height;825}826827// Filter828io->bypass_filtering = (options != NULL) && options->bypass_filtering;829830// Fancy upsampler831#ifdef FANCY_UPSAMPLING832io->fancy_upsampling = (options == NULL) || (!options->no_fancy_upsampling);833#endif834835if (io->use_scaling) {836// disable filter (only for large downscaling ratio).837io->bypass_filtering = (io->scaled_width < W * 3 / 4) &&838(io->scaled_height < H * 3 / 4);839io->fancy_upsampling = 0;840}841return 1;842}843844//------------------------------------------------------------------------------845846847