Path: blob/master/3rdparty/libwebp/src/dec/idec_dec.c
16358 views
// Copyright 2011 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// Incremental decoding10//11// Author: [email protected] (Somnath Banerjee)1213#include <assert.h>14#include <string.h>15#include <stdlib.h>1617#include "src/dec/alphai_dec.h"18#include "src/dec/webpi_dec.h"19#include "src/dec/vp8i_dec.h"20#include "src/utils/utils.h"2122// In append mode, buffer allocations increase as multiples of this value.23// Needs to be a power of 2.24#define CHUNK_SIZE 409625#define MAX_MB_SIZE 40962627//------------------------------------------------------------------------------28// Data structures for memory and states2930// Decoding states. State normally flows as:31// WEBP_HEADER->VP8_HEADER->VP8_PARTS0->VP8_DATA->DONE for a lossy image, and32// WEBP_HEADER->VP8L_HEADER->VP8L_DATA->DONE for a lossless image.33// If there is any error the decoder goes into state ERROR.34typedef enum {35STATE_WEBP_HEADER, // All the data before that of the VP8/VP8L chunk.36STATE_VP8_HEADER, // The VP8 Frame header (within the VP8 chunk).37STATE_VP8_PARTS0,38STATE_VP8_DATA,39STATE_VP8L_HEADER,40STATE_VP8L_DATA,41STATE_DONE,42STATE_ERROR43} DecState;4445// Operating state for the MemBuffer46typedef enum {47MEM_MODE_NONE = 0,48MEM_MODE_APPEND,49MEM_MODE_MAP50} MemBufferMode;5152// storage for partition #0 and partial data (in a rolling fashion)53typedef struct {54MemBufferMode mode_; // Operation mode55size_t start_; // start location of the data to be decoded56size_t end_; // end location57size_t buf_size_; // size of the allocated buffer58uint8_t* buf_; // We don't own this buffer in case WebPIUpdate()5960size_t part0_size_; // size of partition #061const uint8_t* part0_buf_; // buffer to store partition #062} MemBuffer;6364struct WebPIDecoder {65DecState state_; // current decoding state66WebPDecParams params_; // Params to store output info67int is_lossless_; // for down-casting 'dec_'.68void* dec_; // either a VP8Decoder or a VP8LDecoder instance69VP8Io io_;7071MemBuffer mem_; // input memory buffer.72WebPDecBuffer output_; // output buffer (when no external one is supplied,73// or if the external one has slow-memory)74WebPDecBuffer* final_output_; // Slow-memory output to copy to eventually.75size_t chunk_size_; // Compressed VP8/VP8L size extracted from Header.7677int last_mb_y_; // last row reached for intra-mode decoding78};7980// MB context to restore in case VP8DecodeMB() fails81typedef struct {82VP8MB left_;83VP8MB info_;84VP8BitReader token_br_;85} MBContext;8687//------------------------------------------------------------------------------88// MemBuffer: incoming data handling8990static WEBP_INLINE size_t MemDataSize(const MemBuffer* mem) {91return (mem->end_ - mem->start_);92}9394// Check if we need to preserve the compressed alpha data, as it may not have95// been decoded yet.96static int NeedCompressedAlpha(const WebPIDecoder* const idec) {97if (idec->state_ == STATE_WEBP_HEADER) {98// We haven't parsed the headers yet, so we don't know whether the image is99// lossy or lossless. This also means that we haven't parsed the ALPH chunk.100return 0;101}102if (idec->is_lossless_) {103return 0; // ALPH chunk is not present for lossless images.104} else {105const VP8Decoder* const dec = (VP8Decoder*)idec->dec_;106assert(dec != NULL); // Must be true as idec->state_ != STATE_WEBP_HEADER.107return (dec->alpha_data_ != NULL) && !dec->is_alpha_decoded_;108}109}110111static void DoRemap(WebPIDecoder* const idec, ptrdiff_t offset) {112MemBuffer* const mem = &idec->mem_;113const uint8_t* const new_base = mem->buf_ + mem->start_;114// note: for VP8, setting up idec->io_ is only really needed at the beginning115// of the decoding, till partition #0 is complete.116idec->io_.data = new_base;117idec->io_.data_size = MemDataSize(mem);118119if (idec->dec_ != NULL) {120if (!idec->is_lossless_) {121VP8Decoder* const dec = (VP8Decoder*)idec->dec_;122const uint32_t last_part = dec->num_parts_minus_one_;123if (offset != 0) {124uint32_t p;125for (p = 0; p <= last_part; ++p) {126VP8RemapBitReader(dec->parts_ + p, offset);127}128// Remap partition #0 data pointer to new offset, but only in MAP129// mode (in APPEND mode, partition #0 is copied into a fixed memory).130if (mem->mode_ == MEM_MODE_MAP) {131VP8RemapBitReader(&dec->br_, offset);132}133}134{135const uint8_t* const last_start = dec->parts_[last_part].buf_;136VP8BitReaderSetBuffer(&dec->parts_[last_part], last_start,137mem->buf_ + mem->end_ - last_start);138}139if (NeedCompressedAlpha(idec)) {140ALPHDecoder* const alph_dec = dec->alph_dec_;141dec->alpha_data_ += offset;142if (alph_dec != NULL) {143if (alph_dec->method_ == ALPHA_LOSSLESS_COMPRESSION) {144VP8LDecoder* const alph_vp8l_dec = alph_dec->vp8l_dec_;145assert(alph_vp8l_dec != NULL);146assert(dec->alpha_data_size_ >= ALPHA_HEADER_LEN);147VP8LBitReaderSetBuffer(&alph_vp8l_dec->br_,148dec->alpha_data_ + ALPHA_HEADER_LEN,149dec->alpha_data_size_ - ALPHA_HEADER_LEN);150} else { // alph_dec->method_ == ALPHA_NO_COMPRESSION151// Nothing special to do in this case.152}153}154}155} else { // Resize lossless bitreader156VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;157VP8LBitReaderSetBuffer(&dec->br_, new_base, MemDataSize(mem));158}159}160}161162// Appends data to the end of MemBuffer->buf_. It expands the allocated memory163// size if required and also updates VP8BitReader's if new memory is allocated.164static int AppendToMemBuffer(WebPIDecoder* const idec,165const uint8_t* const data, size_t data_size) {166VP8Decoder* const dec = (VP8Decoder*)idec->dec_;167MemBuffer* const mem = &idec->mem_;168const int need_compressed_alpha = NeedCompressedAlpha(idec);169const uint8_t* const old_start = mem->buf_ + mem->start_;170const uint8_t* const old_base =171need_compressed_alpha ? dec->alpha_data_ : old_start;172assert(mem->mode_ == MEM_MODE_APPEND);173if (data_size > MAX_CHUNK_PAYLOAD) {174// security safeguard: trying to allocate more than what the format175// allows for a chunk should be considered a smoke smell.176return 0;177}178179if (mem->end_ + data_size > mem->buf_size_) { // Need some free memory180const size_t new_mem_start = old_start - old_base;181const size_t current_size = MemDataSize(mem) + new_mem_start;182const uint64_t new_size = (uint64_t)current_size + data_size;183const uint64_t extra_size = (new_size + CHUNK_SIZE - 1) & ~(CHUNK_SIZE - 1);184uint8_t* const new_buf =185(uint8_t*)WebPSafeMalloc(extra_size, sizeof(*new_buf));186if (new_buf == NULL) return 0;187memcpy(new_buf, old_base, current_size);188WebPSafeFree(mem->buf_);189mem->buf_ = new_buf;190mem->buf_size_ = (size_t)extra_size;191mem->start_ = new_mem_start;192mem->end_ = current_size;193}194195memcpy(mem->buf_ + mem->end_, data, data_size);196mem->end_ += data_size;197assert(mem->end_ <= mem->buf_size_);198199DoRemap(idec, mem->buf_ + mem->start_ - old_start);200return 1;201}202203static int RemapMemBuffer(WebPIDecoder* const idec,204const uint8_t* const data, size_t data_size) {205MemBuffer* const mem = &idec->mem_;206const uint8_t* const old_buf = mem->buf_;207const uint8_t* const old_start = old_buf + mem->start_;208assert(mem->mode_ == MEM_MODE_MAP);209210if (data_size < mem->buf_size_) return 0; // can't remap to a shorter buffer!211212mem->buf_ = (uint8_t*)data;213mem->end_ = mem->buf_size_ = data_size;214215DoRemap(idec, mem->buf_ + mem->start_ - old_start);216return 1;217}218219static void InitMemBuffer(MemBuffer* const mem) {220mem->mode_ = MEM_MODE_NONE;221mem->buf_ = NULL;222mem->buf_size_ = 0;223mem->part0_buf_ = NULL;224mem->part0_size_ = 0;225}226227static void ClearMemBuffer(MemBuffer* const mem) {228assert(mem);229if (mem->mode_ == MEM_MODE_APPEND) {230WebPSafeFree(mem->buf_);231WebPSafeFree((void*)mem->part0_buf_);232}233}234235static int CheckMemBufferMode(MemBuffer* const mem, MemBufferMode expected) {236if (mem->mode_ == MEM_MODE_NONE) {237mem->mode_ = expected; // switch to the expected mode238} else if (mem->mode_ != expected) {239return 0; // we mixed the modes => error240}241assert(mem->mode_ == expected); // mode is ok242return 1;243}244245// To be called last.246static VP8StatusCode FinishDecoding(WebPIDecoder* const idec) {247const WebPDecoderOptions* const options = idec->params_.options;248WebPDecBuffer* const output = idec->params_.output;249250idec->state_ = STATE_DONE;251if (options != NULL && options->flip) {252const VP8StatusCode status = WebPFlipBuffer(output);253if (status != VP8_STATUS_OK) return status;254}255if (idec->final_output_ != NULL) {256WebPCopyDecBufferPixels(output, idec->final_output_); // do the slow-copy257WebPFreeDecBuffer(&idec->output_);258*output = *idec->final_output_;259idec->final_output_ = NULL;260}261return VP8_STATUS_OK;262}263264//------------------------------------------------------------------------------265// Macroblock-decoding contexts266267static void SaveContext(const VP8Decoder* dec, const VP8BitReader* token_br,268MBContext* const context) {269context->left_ = dec->mb_info_[-1];270context->info_ = dec->mb_info_[dec->mb_x_];271context->token_br_ = *token_br;272}273274static void RestoreContext(const MBContext* context, VP8Decoder* const dec,275VP8BitReader* const token_br) {276dec->mb_info_[-1] = context->left_;277dec->mb_info_[dec->mb_x_] = context->info_;278*token_br = context->token_br_;279}280281//------------------------------------------------------------------------------282283static VP8StatusCode IDecError(WebPIDecoder* const idec, VP8StatusCode error) {284if (idec->state_ == STATE_VP8_DATA) {285VP8Io* const io = &idec->io_;286if (io->teardown != NULL) {287io->teardown(io);288}289}290idec->state_ = STATE_ERROR;291return error;292}293294static void ChangeState(WebPIDecoder* const idec, DecState new_state,295size_t consumed_bytes) {296MemBuffer* const mem = &idec->mem_;297idec->state_ = new_state;298mem->start_ += consumed_bytes;299assert(mem->start_ <= mem->end_);300idec->io_.data = mem->buf_ + mem->start_;301idec->io_.data_size = MemDataSize(mem);302}303304// Headers305static VP8StatusCode DecodeWebPHeaders(WebPIDecoder* const idec) {306MemBuffer* const mem = &idec->mem_;307const uint8_t* data = mem->buf_ + mem->start_;308size_t curr_size = MemDataSize(mem);309VP8StatusCode status;310WebPHeaderStructure headers;311312headers.data = data;313headers.data_size = curr_size;314headers.have_all_data = 0;315status = WebPParseHeaders(&headers);316if (status == VP8_STATUS_NOT_ENOUGH_DATA) {317return VP8_STATUS_SUSPENDED; // We haven't found a VP8 chunk yet.318} else if (status != VP8_STATUS_OK) {319return IDecError(idec, status);320}321322idec->chunk_size_ = headers.compressed_size;323idec->is_lossless_ = headers.is_lossless;324if (!idec->is_lossless_) {325VP8Decoder* const dec = VP8New();326if (dec == NULL) {327return VP8_STATUS_OUT_OF_MEMORY;328}329idec->dec_ = dec;330dec->alpha_data_ = headers.alpha_data;331dec->alpha_data_size_ = headers.alpha_data_size;332ChangeState(idec, STATE_VP8_HEADER, headers.offset);333} else {334VP8LDecoder* const dec = VP8LNew();335if (dec == NULL) {336return VP8_STATUS_OUT_OF_MEMORY;337}338idec->dec_ = dec;339ChangeState(idec, STATE_VP8L_HEADER, headers.offset);340}341return VP8_STATUS_OK;342}343344static VP8StatusCode DecodeVP8FrameHeader(WebPIDecoder* const idec) {345const uint8_t* data = idec->mem_.buf_ + idec->mem_.start_;346const size_t curr_size = MemDataSize(&idec->mem_);347int width, height;348uint32_t bits;349350if (curr_size < VP8_FRAME_HEADER_SIZE) {351// Not enough data bytes to extract VP8 Frame Header.352return VP8_STATUS_SUSPENDED;353}354if (!VP8GetInfo(data, curr_size, idec->chunk_size_, &width, &height)) {355return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);356}357358bits = data[0] | (data[1] << 8) | (data[2] << 16);359idec->mem_.part0_size_ = (bits >> 5) + VP8_FRAME_HEADER_SIZE;360361idec->io_.data = data;362idec->io_.data_size = curr_size;363idec->state_ = STATE_VP8_PARTS0;364return VP8_STATUS_OK;365}366367// Partition #0368static VP8StatusCode CopyParts0Data(WebPIDecoder* const idec) {369VP8Decoder* const dec = (VP8Decoder*)idec->dec_;370VP8BitReader* const br = &dec->br_;371const size_t part_size = br->buf_end_ - br->buf_;372MemBuffer* const mem = &idec->mem_;373assert(!idec->is_lossless_);374assert(mem->part0_buf_ == NULL);375// the following is a format limitation, no need for runtime check:376assert(part_size <= mem->part0_size_);377if (part_size == 0) { // can't have zero-size partition #0378return VP8_STATUS_BITSTREAM_ERROR;379}380if (mem->mode_ == MEM_MODE_APPEND) {381// We copy and grab ownership of the partition #0 data.382uint8_t* const part0_buf = (uint8_t*)WebPSafeMalloc(1ULL, part_size);383if (part0_buf == NULL) {384return VP8_STATUS_OUT_OF_MEMORY;385}386memcpy(part0_buf, br->buf_, part_size);387mem->part0_buf_ = part0_buf;388VP8BitReaderSetBuffer(br, part0_buf, part_size);389} else {390// Else: just keep pointers to the partition #0's data in dec_->br_.391}392mem->start_ += part_size;393return VP8_STATUS_OK;394}395396static VP8StatusCode DecodePartition0(WebPIDecoder* const idec) {397VP8Decoder* const dec = (VP8Decoder*)idec->dec_;398VP8Io* const io = &idec->io_;399const WebPDecParams* const params = &idec->params_;400WebPDecBuffer* const output = params->output;401402// Wait till we have enough data for the whole partition #0403if (MemDataSize(&idec->mem_) < idec->mem_.part0_size_) {404return VP8_STATUS_SUSPENDED;405}406407if (!VP8GetHeaders(dec, io)) {408const VP8StatusCode status = dec->status_;409if (status == VP8_STATUS_SUSPENDED ||410status == VP8_STATUS_NOT_ENOUGH_DATA) {411// treating NOT_ENOUGH_DATA as SUSPENDED state412return VP8_STATUS_SUSPENDED;413}414return IDecError(idec, status);415}416417// Allocate/Verify output buffer now418dec->status_ = WebPAllocateDecBuffer(io->width, io->height, params->options,419output);420if (dec->status_ != VP8_STATUS_OK) {421return IDecError(idec, dec->status_);422}423// This change must be done before calling VP8InitFrame()424dec->mt_method_ = VP8GetThreadMethod(params->options, NULL,425io->width, io->height);426VP8InitDithering(params->options, dec);427428dec->status_ = CopyParts0Data(idec);429if (dec->status_ != VP8_STATUS_OK) {430return IDecError(idec, dec->status_);431}432433// Finish setting up the decoding parameters. Will call io->setup().434if (VP8EnterCritical(dec, io) != VP8_STATUS_OK) {435return IDecError(idec, dec->status_);436}437438// Note: past this point, teardown() must always be called439// in case of error.440idec->state_ = STATE_VP8_DATA;441// Allocate memory and prepare everything.442if (!VP8InitFrame(dec, io)) {443return IDecError(idec, dec->status_);444}445return VP8_STATUS_OK;446}447448// Remaining partitions449static VP8StatusCode DecodeRemaining(WebPIDecoder* const idec) {450VP8Decoder* const dec = (VP8Decoder*)idec->dec_;451VP8Io* const io = &idec->io_;452453assert(dec->ready_);454for (; dec->mb_y_ < dec->mb_h_; ++dec->mb_y_) {455if (idec->last_mb_y_ != dec->mb_y_) {456if (!VP8ParseIntraModeRow(&dec->br_, dec)) {457// note: normally, error shouldn't occur since we already have the whole458// partition0 available here in DecodeRemaining(). Reaching EOF while459// reading intra modes really means a BITSTREAM_ERROR.460return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);461}462idec->last_mb_y_ = dec->mb_y_;463}464for (; dec->mb_x_ < dec->mb_w_; ++dec->mb_x_) {465VP8BitReader* const token_br =466&dec->parts_[dec->mb_y_ & dec->num_parts_minus_one_];467MBContext context;468SaveContext(dec, token_br, &context);469if (!VP8DecodeMB(dec, token_br)) {470// We shouldn't fail when MAX_MB data was available471if (dec->num_parts_minus_one_ == 0 &&472MemDataSize(&idec->mem_) > MAX_MB_SIZE) {473return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);474}475RestoreContext(&context, dec, token_br);476return VP8_STATUS_SUSPENDED;477}478// Release buffer only if there is only one partition479if (dec->num_parts_minus_one_ == 0) {480idec->mem_.start_ = token_br->buf_ - idec->mem_.buf_;481assert(idec->mem_.start_ <= idec->mem_.end_);482}483}484VP8InitScanline(dec); // Prepare for next scanline485486// Reconstruct, filter and emit the row.487if (!VP8ProcessRow(dec, io)) {488return IDecError(idec, VP8_STATUS_USER_ABORT);489}490}491// Synchronize the thread and check for errors.492if (!VP8ExitCritical(dec, io)) {493return IDecError(idec, VP8_STATUS_USER_ABORT);494}495dec->ready_ = 0;496return FinishDecoding(idec);497}498499static VP8StatusCode ErrorStatusLossless(WebPIDecoder* const idec,500VP8StatusCode status) {501if (status == VP8_STATUS_SUSPENDED || status == VP8_STATUS_NOT_ENOUGH_DATA) {502return VP8_STATUS_SUSPENDED;503}504return IDecError(idec, status);505}506507static VP8StatusCode DecodeVP8LHeader(WebPIDecoder* const idec) {508VP8Io* const io = &idec->io_;509VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;510const WebPDecParams* const params = &idec->params_;511WebPDecBuffer* const output = params->output;512size_t curr_size = MemDataSize(&idec->mem_);513assert(idec->is_lossless_);514515// Wait until there's enough data for decoding header.516if (curr_size < (idec->chunk_size_ >> 3)) {517dec->status_ = VP8_STATUS_SUSPENDED;518return ErrorStatusLossless(idec, dec->status_);519}520521if (!VP8LDecodeHeader(dec, io)) {522if (dec->status_ == VP8_STATUS_BITSTREAM_ERROR &&523curr_size < idec->chunk_size_) {524dec->status_ = VP8_STATUS_SUSPENDED;525}526return ErrorStatusLossless(idec, dec->status_);527}528// Allocate/verify output buffer now.529dec->status_ = WebPAllocateDecBuffer(io->width, io->height, params->options,530output);531if (dec->status_ != VP8_STATUS_OK) {532return IDecError(idec, dec->status_);533}534535idec->state_ = STATE_VP8L_DATA;536return VP8_STATUS_OK;537}538539static VP8StatusCode DecodeVP8LData(WebPIDecoder* const idec) {540VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;541const size_t curr_size = MemDataSize(&idec->mem_);542assert(idec->is_lossless_);543544// Switch to incremental decoding if we don't have all the bytes available.545dec->incremental_ = (curr_size < idec->chunk_size_);546547if (!VP8LDecodeImage(dec)) {548return ErrorStatusLossless(idec, dec->status_);549}550assert(dec->status_ == VP8_STATUS_OK || dec->status_ == VP8_STATUS_SUSPENDED);551return (dec->status_ == VP8_STATUS_SUSPENDED) ? dec->status_552: FinishDecoding(idec);553}554555// Main decoding loop556static VP8StatusCode IDecode(WebPIDecoder* idec) {557VP8StatusCode status = VP8_STATUS_SUSPENDED;558559if (idec->state_ == STATE_WEBP_HEADER) {560status = DecodeWebPHeaders(idec);561} else {562if (idec->dec_ == NULL) {563return VP8_STATUS_SUSPENDED; // can't continue if we have no decoder.564}565}566if (idec->state_ == STATE_VP8_HEADER) {567status = DecodeVP8FrameHeader(idec);568}569if (idec->state_ == STATE_VP8_PARTS0) {570status = DecodePartition0(idec);571}572if (idec->state_ == STATE_VP8_DATA) {573status = DecodeRemaining(idec);574}575if (idec->state_ == STATE_VP8L_HEADER) {576status = DecodeVP8LHeader(idec);577}578if (idec->state_ == STATE_VP8L_DATA) {579status = DecodeVP8LData(idec);580}581return status;582}583584//------------------------------------------------------------------------------585// Internal constructor586587static WebPIDecoder* NewDecoder(WebPDecBuffer* const output_buffer,588const WebPBitstreamFeatures* const features) {589WebPIDecoder* idec = (WebPIDecoder*)WebPSafeCalloc(1ULL, sizeof(*idec));590if (idec == NULL) {591return NULL;592}593594idec->state_ = STATE_WEBP_HEADER;595idec->chunk_size_ = 0;596597idec->last_mb_y_ = -1;598599InitMemBuffer(&idec->mem_);600WebPInitDecBuffer(&idec->output_);601VP8InitIo(&idec->io_);602603WebPResetDecParams(&idec->params_);604if (output_buffer == NULL || WebPAvoidSlowMemory(output_buffer, features)) {605idec->params_.output = &idec->output_;606idec->final_output_ = output_buffer;607if (output_buffer != NULL) {608idec->params_.output->colorspace = output_buffer->colorspace;609}610} else {611idec->params_.output = output_buffer;612idec->final_output_ = NULL;613}614WebPInitCustomIo(&idec->params_, &idec->io_); // Plug the I/O functions.615616return idec;617}618619//------------------------------------------------------------------------------620// Public functions621622WebPIDecoder* WebPINewDecoder(WebPDecBuffer* output_buffer) {623return NewDecoder(output_buffer, NULL);624}625626WebPIDecoder* WebPIDecode(const uint8_t* data, size_t data_size,627WebPDecoderConfig* config) {628WebPIDecoder* idec;629WebPBitstreamFeatures tmp_features;630WebPBitstreamFeatures* const features =631(config == NULL) ? &tmp_features : &config->input;632memset(&tmp_features, 0, sizeof(tmp_features));633634// Parse the bitstream's features, if requested:635if (data != NULL && data_size > 0) {636if (WebPGetFeatures(data, data_size, features) != VP8_STATUS_OK) {637return NULL;638}639}640641// Create an instance of the incremental decoder642idec = (config != NULL) ? NewDecoder(&config->output, features)643: NewDecoder(NULL, features);644if (idec == NULL) {645return NULL;646}647// Finish initialization648if (config != NULL) {649idec->params_.options = &config->options;650}651return idec;652}653654void WebPIDelete(WebPIDecoder* idec) {655if (idec == NULL) return;656if (idec->dec_ != NULL) {657if (!idec->is_lossless_) {658if (idec->state_ == STATE_VP8_DATA) {659// Synchronize the thread, clean-up and check for errors.660VP8ExitCritical((VP8Decoder*)idec->dec_, &idec->io_);661}662VP8Delete((VP8Decoder*)idec->dec_);663} else {664VP8LDelete((VP8LDecoder*)idec->dec_);665}666}667ClearMemBuffer(&idec->mem_);668WebPFreeDecBuffer(&idec->output_);669WebPSafeFree(idec);670}671672//------------------------------------------------------------------------------673// Wrapper toward WebPINewDecoder674675WebPIDecoder* WebPINewRGB(WEBP_CSP_MODE csp, uint8_t* output_buffer,676size_t output_buffer_size, int output_stride) {677const int is_external_memory = (output_buffer != NULL) ? 1 : 0;678WebPIDecoder* idec;679680if (csp >= MODE_YUV) return NULL;681if (is_external_memory == 0) { // Overwrite parameters to sane values.682output_buffer_size = 0;683output_stride = 0;684} else { // A buffer was passed. Validate the other params.685if (output_stride == 0 || output_buffer_size == 0) {686return NULL; // invalid parameter.687}688}689idec = WebPINewDecoder(NULL);690if (idec == NULL) return NULL;691idec->output_.colorspace = csp;692idec->output_.is_external_memory = is_external_memory;693idec->output_.u.RGBA.rgba = output_buffer;694idec->output_.u.RGBA.stride = output_stride;695idec->output_.u.RGBA.size = output_buffer_size;696return idec;697}698699WebPIDecoder* WebPINewYUVA(uint8_t* luma, size_t luma_size, int luma_stride,700uint8_t* u, size_t u_size, int u_stride,701uint8_t* v, size_t v_size, int v_stride,702uint8_t* a, size_t a_size, int a_stride) {703const int is_external_memory = (luma != NULL) ? 1 : 0;704WebPIDecoder* idec;705WEBP_CSP_MODE colorspace;706707if (is_external_memory == 0) { // Overwrite parameters to sane values.708luma_size = u_size = v_size = a_size = 0;709luma_stride = u_stride = v_stride = a_stride = 0;710u = v = a = NULL;711colorspace = MODE_YUVA;712} else { // A luma buffer was passed. Validate the other parameters.713if (u == NULL || v == NULL) return NULL;714if (luma_size == 0 || u_size == 0 || v_size == 0) return NULL;715if (luma_stride == 0 || u_stride == 0 || v_stride == 0) return NULL;716if (a != NULL) {717if (a_size == 0 || a_stride == 0) return NULL;718}719colorspace = (a == NULL) ? MODE_YUV : MODE_YUVA;720}721722idec = WebPINewDecoder(NULL);723if (idec == NULL) return NULL;724725idec->output_.colorspace = colorspace;726idec->output_.is_external_memory = is_external_memory;727idec->output_.u.YUVA.y = luma;728idec->output_.u.YUVA.y_stride = luma_stride;729idec->output_.u.YUVA.y_size = luma_size;730idec->output_.u.YUVA.u = u;731idec->output_.u.YUVA.u_stride = u_stride;732idec->output_.u.YUVA.u_size = u_size;733idec->output_.u.YUVA.v = v;734idec->output_.u.YUVA.v_stride = v_stride;735idec->output_.u.YUVA.v_size = v_size;736idec->output_.u.YUVA.a = a;737idec->output_.u.YUVA.a_stride = a_stride;738idec->output_.u.YUVA.a_size = a_size;739return idec;740}741742WebPIDecoder* WebPINewYUV(uint8_t* luma, size_t luma_size, int luma_stride,743uint8_t* u, size_t u_size, int u_stride,744uint8_t* v, size_t v_size, int v_stride) {745return WebPINewYUVA(luma, luma_size, luma_stride,746u, u_size, u_stride,747v, v_size, v_stride,748NULL, 0, 0);749}750751//------------------------------------------------------------------------------752753static VP8StatusCode IDecCheckStatus(const WebPIDecoder* const idec) {754assert(idec);755if (idec->state_ == STATE_ERROR) {756return VP8_STATUS_BITSTREAM_ERROR;757}758if (idec->state_ == STATE_DONE) {759return VP8_STATUS_OK;760}761return VP8_STATUS_SUSPENDED;762}763764VP8StatusCode WebPIAppend(WebPIDecoder* idec,765const uint8_t* data, size_t data_size) {766VP8StatusCode status;767if (idec == NULL || data == NULL) {768return VP8_STATUS_INVALID_PARAM;769}770status = IDecCheckStatus(idec);771if (status != VP8_STATUS_SUSPENDED) {772return status;773}774// Check mixed calls between RemapMemBuffer and AppendToMemBuffer.775if (!CheckMemBufferMode(&idec->mem_, MEM_MODE_APPEND)) {776return VP8_STATUS_INVALID_PARAM;777}778// Append data to memory buffer779if (!AppendToMemBuffer(idec, data, data_size)) {780return VP8_STATUS_OUT_OF_MEMORY;781}782return IDecode(idec);783}784785VP8StatusCode WebPIUpdate(WebPIDecoder* idec,786const uint8_t* data, size_t data_size) {787VP8StatusCode status;788if (idec == NULL || data == NULL) {789return VP8_STATUS_INVALID_PARAM;790}791status = IDecCheckStatus(idec);792if (status != VP8_STATUS_SUSPENDED) {793return status;794}795// Check mixed calls between RemapMemBuffer and AppendToMemBuffer.796if (!CheckMemBufferMode(&idec->mem_, MEM_MODE_MAP)) {797return VP8_STATUS_INVALID_PARAM;798}799// Make the memory buffer point to the new buffer800if (!RemapMemBuffer(idec, data, data_size)) {801return VP8_STATUS_INVALID_PARAM;802}803return IDecode(idec);804}805806//------------------------------------------------------------------------------807808static const WebPDecBuffer* GetOutputBuffer(const WebPIDecoder* const idec) {809if (idec == NULL || idec->dec_ == NULL) {810return NULL;811}812if (idec->state_ <= STATE_VP8_PARTS0) {813return NULL;814}815if (idec->final_output_ != NULL) {816return NULL; // not yet slow-copied817}818return idec->params_.output;819}820821const WebPDecBuffer* WebPIDecodedArea(const WebPIDecoder* idec,822int* left, int* top,823int* width, int* height) {824const WebPDecBuffer* const src = GetOutputBuffer(idec);825if (left != NULL) *left = 0;826if (top != NULL) *top = 0;827if (src != NULL) {828if (width != NULL) *width = src->width;829if (height != NULL) *height = idec->params_.last_y;830} else {831if (width != NULL) *width = 0;832if (height != NULL) *height = 0;833}834return src;835}836837uint8_t* WebPIDecGetRGB(const WebPIDecoder* idec, int* last_y,838int* width, int* height, int* stride) {839const WebPDecBuffer* const src = GetOutputBuffer(idec);840if (src == NULL) return NULL;841if (src->colorspace >= MODE_YUV) {842return NULL;843}844845if (last_y != NULL) *last_y = idec->params_.last_y;846if (width != NULL) *width = src->width;847if (height != NULL) *height = src->height;848if (stride != NULL) *stride = src->u.RGBA.stride;849850return src->u.RGBA.rgba;851}852853uint8_t* WebPIDecGetYUVA(const WebPIDecoder* idec, int* last_y,854uint8_t** u, uint8_t** v, uint8_t** a,855int* width, int* height,856int* stride, int* uv_stride, int* a_stride) {857const WebPDecBuffer* const src = GetOutputBuffer(idec);858if (src == NULL) return NULL;859if (src->colorspace < MODE_YUV) {860return NULL;861}862863if (last_y != NULL) *last_y = idec->params_.last_y;864if (u != NULL) *u = src->u.YUVA.u;865if (v != NULL) *v = src->u.YUVA.v;866if (a != NULL) *a = src->u.YUVA.a;867if (width != NULL) *width = src->width;868if (height != NULL) *height = src->height;869if (stride != NULL) *stride = src->u.YUVA.y_stride;870if (uv_stride != NULL) *uv_stride = src->u.YUVA.u_stride;871if (a_stride != NULL) *a_stride = src->u.YUVA.a_stride;872873return src->u.YUVA.y;874}875876int WebPISetIOHooks(WebPIDecoder* const idec,877VP8IoPutHook put,878VP8IoSetupHook setup,879VP8IoTeardownHook teardown,880void* user_data) {881if (idec == NULL || idec->state_ > STATE_WEBP_HEADER) {882return 0;883}884885idec->io_.put = put;886idec->io_.setup = setup;887idec->io_.teardown = teardown;888idec->io_.opaque = user_data;889890return 1;891}892893894