Path: blob/master/thirdparty/libwebp/src/dec/idec_dec.c
21136 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 <stdlib.h>15#include <string.h>1617#include "src/dec/alphai_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/bit_reader_utils.h"23#include "src/utils/thread_utils.h"24#include "src/utils/utils.h"25#include "src/webp/decode.h"26#include "src/webp/format_constants.h"27#include "src/webp/types.h"2829// In append mode, buffer allocations increase as multiples of this value.30// Needs to be a power of 2.31#define CHUNK_SIZE 409632#define MAX_MB_SIZE 40963334//------------------------------------------------------------------------------35// Data structures for memory and states3637// Decoding states. State normally flows as:38// WEBP_HEADER->VP8_HEADER->VP8_PARTS0->VP8_DATA->DONE for a lossy image, and39// WEBP_HEADER->VP8L_HEADER->VP8L_DATA->DONE for a lossless image.40// If there is any error the decoder goes into state ERROR.41typedef enum {42STATE_WEBP_HEADER, // All the data before that of the VP8/VP8L chunk.43STATE_VP8_HEADER, // The VP8 Frame header (within the VP8 chunk).44STATE_VP8_PARTS0,45STATE_VP8_DATA,46STATE_VP8L_HEADER,47STATE_VP8L_DATA,48STATE_DONE,49STATE_ERROR50} DecState;5152// Operating state for the MemBuffer53typedef enum {54MEM_MODE_NONE = 0,55MEM_MODE_APPEND,56MEM_MODE_MAP57} MemBufferMode;5859// storage for partition #0 and partial data (in a rolling fashion)60typedef struct {61MemBufferMode mode; // Operation mode62size_t start; // start location of the data to be decoded63size_t end; // end location64size_t buf_size; // size of the allocated buffer65uint8_t* buf; // We don't own this buffer in case WebPIUpdate()6667size_t part0_size; // size of partition #068const uint8_t* part0_buf; // buffer to store partition #069} MemBuffer;7071struct WebPIDecoder {72DecState state; // current decoding state73WebPDecParams params; // Params to store output info74int is_lossless; // for down-casting 'dec'.75void* dec; // either a VP8Decoder or a VP8LDecoder instance76VP8Io io;7778MemBuffer mem; // input memory buffer.79WebPDecBuffer output; // output buffer (when no external one is supplied,80// or if the external one has slow-memory)81WebPDecBuffer* final_output; // Slow-memory output to copy to eventually.82size_t chunk_size; // Compressed VP8/VP8L size extracted from Header.8384int last_mb_y; // last row reached for intra-mode decoding85};8687// MB context to restore in case VP8DecodeMB() fails88typedef struct {89VP8MB left;90VP8MB info;91VP8BitReader token_br;92} MBContext;9394//------------------------------------------------------------------------------95// MemBuffer: incoming data handling9697static WEBP_INLINE size_t MemDataSize(const MemBuffer* mem) {98return (mem->end - mem->start);99}100101// Check if we need to preserve the compressed alpha data, as it may not have102// been decoded yet.103static int NeedCompressedAlpha(const WebPIDecoder* const idec) {104if (idec->state == STATE_WEBP_HEADER) {105// We haven't parsed the headers yet, so we don't know whether the image is106// lossy or lossless. This also means that we haven't parsed the ALPH chunk.107return 0;108}109if (idec->is_lossless) {110return 0; // ALPH chunk is not present for lossless images.111} else {112const VP8Decoder* const dec = (VP8Decoder*)idec->dec;113assert(dec != NULL); // Must be true as idec->state != STATE_WEBP_HEADER.114return (dec->alpha_data != NULL) && !dec->is_alpha_decoded;115}116}117118static void DoRemap(WebPIDecoder* const idec, ptrdiff_t offset) {119MemBuffer* const mem = &idec->mem;120const uint8_t* const new_base = mem->buf + mem->start;121// note: for VP8, setting up idec->io is only really needed at the beginning122// of the decoding, till partition #0 is complete.123idec->io.data = new_base;124idec->io.data_size = MemDataSize(mem);125126if (idec->dec != NULL) {127if (!idec->is_lossless) {128VP8Decoder* const dec = (VP8Decoder*)idec->dec;129const uint32_t last_part = dec->num_parts_minus_one;130if (offset != 0) {131uint32_t p;132for (p = 0; p <= last_part; ++p) {133VP8RemapBitReader(dec->parts + p, offset);134}135// Remap partition #0 data pointer to new offset, but only in MAP136// mode (in APPEND mode, partition #0 is copied into a fixed memory).137if (mem->mode == MEM_MODE_MAP) {138VP8RemapBitReader(&dec->br, offset);139}140}141{142const uint8_t* const last_start = dec->parts[last_part].buf;143// 'last_start' will be NULL when 'idec->state' is < STATE_VP8_PARTS0144// and through a portion of that state (when there isn't enough data to145// parse the partitions). The bitreader is only used meaningfully when146// there is enough data to begin parsing partition 0.147if (last_start != NULL) {148VP8BitReaderSetBuffer(&dec->parts[last_part], last_start,149mem->buf + mem->end - last_start);150}151}152if (NeedCompressedAlpha(idec)) {153ALPHDecoder* const alph_dec = dec->alph_dec;154dec->alpha_data += offset;155if (alph_dec != NULL && alph_dec->vp8l_dec != NULL) {156if (alph_dec->method == ALPHA_LOSSLESS_COMPRESSION) {157VP8LDecoder* const alph_vp8l_dec = alph_dec->vp8l_dec;158assert(dec->alpha_data_size >= ALPHA_HEADER_LEN);159VP8LBitReaderSetBuffer(&alph_vp8l_dec->br,160dec->alpha_data + ALPHA_HEADER_LEN,161dec->alpha_data_size - ALPHA_HEADER_LEN);162} else { // alph_dec->method == ALPHA_NO_COMPRESSION163// Nothing special to do in this case.164}165}166}167} else { // Resize lossless bitreader168VP8LDecoder* const dec = (VP8LDecoder*)idec->dec;169VP8LBitReaderSetBuffer(&dec->br, new_base, MemDataSize(mem));170}171}172}173174// Appends data to the end of MemBuffer->buf. It expands the allocated memory175// size if required and also updates VP8BitReader's if new memory is allocated.176WEBP_NODISCARD static int AppendToMemBuffer(WebPIDecoder* const idec,177const uint8_t* const data,178size_t data_size) {179VP8Decoder* const dec = (VP8Decoder*)idec->dec;180MemBuffer* const mem = &idec->mem;181const int need_compressed_alpha = NeedCompressedAlpha(idec);182const uint8_t* const old_start =183(mem->buf == NULL) ? NULL : mem->buf + mem->start;184const uint8_t* const old_base =185need_compressed_alpha ? dec->alpha_data : old_start;186assert(mem->buf != NULL || mem->start == 0);187assert(mem->mode == MEM_MODE_APPEND);188if (data_size > MAX_CHUNK_PAYLOAD) {189// security safeguard: trying to allocate more than what the format190// allows for a chunk should be considered a smoke smell.191return 0;192}193194if (mem->end + data_size > mem->buf_size) { // Need some free memory195const size_t new_mem_start = old_start - old_base;196const size_t current_size = MemDataSize(mem) + new_mem_start;197const uint64_t new_size = (uint64_t)current_size + data_size;198const uint64_t extra_size = (new_size + CHUNK_SIZE - 1) & ~(CHUNK_SIZE - 1);199uint8_t* const new_buf =200(uint8_t*)WebPSafeMalloc(extra_size, sizeof(*new_buf));201if (new_buf == NULL) return 0;202if (old_base != NULL) memcpy(new_buf, old_base, current_size);203WebPSafeFree(mem->buf);204mem->buf = new_buf;205mem->buf_size = (size_t)extra_size;206mem->start = new_mem_start;207mem->end = current_size;208}209210assert(mem->buf != NULL);211memcpy(mem->buf + mem->end, data, data_size);212mem->end += data_size;213assert(mem->end <= mem->buf_size);214215DoRemap(idec, mem->buf + mem->start - old_start);216return 1;217}218219WEBP_NODISCARD static int RemapMemBuffer(WebPIDecoder* const idec,220const uint8_t* const data,221size_t data_size) {222MemBuffer* const mem = &idec->mem;223const uint8_t* const old_buf = mem->buf;224const uint8_t* const old_start =225(old_buf == NULL) ? NULL : old_buf + mem->start;226assert(old_buf != NULL || mem->start == 0);227assert(mem->mode == MEM_MODE_MAP);228229if (data_size < mem->buf_size) return 0; // can't remap to a shorter buffer!230231mem->buf = (uint8_t*)data;232mem->end = mem->buf_size = data_size;233234DoRemap(idec, mem->buf + mem->start - old_start);235return 1;236}237238static void InitMemBuffer(MemBuffer* const mem) {239mem->mode = MEM_MODE_NONE;240mem->buf = NULL;241mem->buf_size = 0;242mem->part0_buf = NULL;243mem->part0_size = 0;244}245246static void ClearMemBuffer(MemBuffer* const mem) {247assert(mem);248if (mem->mode == MEM_MODE_APPEND) {249WebPSafeFree(mem->buf);250WebPSafeFree((void*)mem->part0_buf);251}252}253254WEBP_NODISCARD static int CheckMemBufferMode(MemBuffer* const mem,255MemBufferMode expected) {256if (mem->mode == MEM_MODE_NONE) {257mem->mode = expected; // switch to the expected mode258} else if (mem->mode != expected) {259return 0; // we mixed the modes => error260}261assert(mem->mode == expected); // mode is ok262return 1;263}264265// To be called last.266WEBP_NODISCARD static VP8StatusCode FinishDecoding(WebPIDecoder* const idec) {267const WebPDecoderOptions* const options = idec->params.options;268WebPDecBuffer* const output = idec->params.output;269270idec->state = STATE_DONE;271if (options != NULL && options->flip) {272const VP8StatusCode status = WebPFlipBuffer(output);273if (status != VP8_STATUS_OK) return status;274}275if (idec->final_output != NULL) {276const VP8StatusCode status = WebPCopyDecBufferPixels(277output, idec->final_output); // do the slow-copy278WebPFreeDecBuffer(&idec->output);279if (status != VP8_STATUS_OK) return status;280*output = *idec->final_output;281idec->final_output = NULL;282}283return VP8_STATUS_OK;284}285286//------------------------------------------------------------------------------287// Macroblock-decoding contexts288289static void SaveContext(const VP8Decoder* dec, const VP8BitReader* token_br,290MBContext* const context) {291context->left = dec->mb_info[-1];292context->info = dec->mb_info[dec->mb_x];293context->token_br = *token_br;294}295296static void RestoreContext(const MBContext* context, VP8Decoder* const dec,297VP8BitReader* const token_br) {298dec->mb_info[-1] = context->left;299dec->mb_info[dec->mb_x] = context->info;300*token_br = context->token_br;301}302303//------------------------------------------------------------------------------304305static VP8StatusCode IDecError(WebPIDecoder* const idec, VP8StatusCode error) {306if (idec->state == STATE_VP8_DATA) {307// Synchronize the thread, clean-up and check for errors.308(void)VP8ExitCritical((VP8Decoder*)idec->dec, &idec->io);309}310idec->state = STATE_ERROR;311return error;312}313314static void ChangeState(WebPIDecoder* const idec, DecState new_state,315size_t consumed_bytes) {316MemBuffer* const mem = &idec->mem;317idec->state = new_state;318mem->start += consumed_bytes;319assert(mem->start <= mem->end);320idec->io.data = mem->buf + mem->start;321idec->io.data_size = MemDataSize(mem);322}323324// Headers325static VP8StatusCode DecodeWebPHeaders(WebPIDecoder* const idec) {326MemBuffer* const mem = &idec->mem;327const uint8_t* data = mem->buf + mem->start;328size_t curr_size = MemDataSize(mem);329VP8StatusCode status;330WebPHeaderStructure headers;331332headers.data = data;333headers.data_size = curr_size;334headers.have_all_data = 0;335status = WebPParseHeaders(&headers);336if (status == VP8_STATUS_NOT_ENOUGH_DATA) {337return VP8_STATUS_SUSPENDED; // We haven't found a VP8 chunk yet.338} else if (status != VP8_STATUS_OK) {339return IDecError(idec, status);340}341342idec->chunk_size = headers.compressed_size;343idec->is_lossless = headers.is_lossless;344if (!idec->is_lossless) {345VP8Decoder* const dec = VP8New();346if (dec == NULL) {347return VP8_STATUS_OUT_OF_MEMORY;348}349dec->incremental = 1;350idec->dec = dec;351dec->alpha_data = headers.alpha_data;352dec->alpha_data_size = headers.alpha_data_size;353ChangeState(idec, STATE_VP8_HEADER, headers.offset);354} else {355VP8LDecoder* const dec = VP8LNew();356if (dec == NULL) {357return VP8_STATUS_OUT_OF_MEMORY;358}359idec->dec = dec;360ChangeState(idec, STATE_VP8L_HEADER, headers.offset);361}362return VP8_STATUS_OK;363}364365static VP8StatusCode DecodeVP8FrameHeader(WebPIDecoder* const idec) {366const uint8_t* data = idec->mem.buf + idec->mem.start;367const size_t curr_size = MemDataSize(&idec->mem);368int width, height;369uint32_t bits;370371if (curr_size < VP8_FRAME_HEADER_SIZE) {372// Not enough data bytes to extract VP8 Frame Header.373return VP8_STATUS_SUSPENDED;374}375if (!VP8GetInfo(data, curr_size, idec->chunk_size, &width, &height)) {376return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);377}378379bits = data[0] | (data[1] << 8) | (data[2] << 16);380idec->mem.part0_size = (bits >> 5) + VP8_FRAME_HEADER_SIZE;381382idec->io.data = data;383idec->io.data_size = curr_size;384idec->state = STATE_VP8_PARTS0;385return VP8_STATUS_OK;386}387388// Partition #0389static VP8StatusCode CopyParts0Data(WebPIDecoder* const idec) {390VP8Decoder* const dec = (VP8Decoder*)idec->dec;391VP8BitReader* const br = &dec->br;392const size_t part_size = br->buf_end - br->buf;393MemBuffer* const mem = &idec->mem;394assert(!idec->is_lossless);395assert(mem->part0_buf == NULL);396// the following is a format limitation, no need for runtime check:397assert(part_size <= mem->part0_size);398if (part_size == 0) { // can't have zero-size partition #0399return VP8_STATUS_BITSTREAM_ERROR;400}401if (mem->mode == MEM_MODE_APPEND) {402// We copy and grab ownership of the partition #0 data.403uint8_t* const part0_buf = (uint8_t*)WebPSafeMalloc(1ULL, part_size);404if (part0_buf == NULL) {405return VP8_STATUS_OUT_OF_MEMORY;406}407memcpy(part0_buf, br->buf, part_size);408mem->part0_buf = part0_buf;409VP8BitReaderSetBuffer(br, part0_buf, part_size);410} else {411// Else: just keep pointers to the partition #0's data in dec->br.412}413mem->start += part_size;414return VP8_STATUS_OK;415}416417static VP8StatusCode DecodePartition0(WebPIDecoder* const idec) {418VP8Decoder* const dec = (VP8Decoder*)idec->dec;419VP8Io* const io = &idec->io;420const WebPDecParams* const params = &idec->params;421WebPDecBuffer* const output = params->output;422423// Wait till we have enough data for the whole partition #0424if (MemDataSize(&idec->mem) < idec->mem.part0_size) {425return VP8_STATUS_SUSPENDED;426}427428if (!VP8GetHeaders(dec, io)) {429const VP8StatusCode status = dec->status;430if (status == VP8_STATUS_SUSPENDED ||431status == VP8_STATUS_NOT_ENOUGH_DATA) {432// treating NOT_ENOUGH_DATA as SUSPENDED state433return VP8_STATUS_SUSPENDED;434}435return IDecError(idec, status);436}437438// Allocate/Verify output buffer now439dec->status = WebPAllocateDecBuffer(io->width, io->height, params->options,440output);441if (dec->status != VP8_STATUS_OK) {442return IDecError(idec, dec->status);443}444// This change must be done before calling VP8InitFrame()445dec->mt_method = VP8GetThreadMethod(params->options, NULL,446io->width, io->height);447VP8InitDithering(params->options, dec);448449dec->status = CopyParts0Data(idec);450if (dec->status != VP8_STATUS_OK) {451return IDecError(idec, dec->status);452}453454// Finish setting up the decoding parameters. Will call io->setup().455if (VP8EnterCritical(dec, io) != VP8_STATUS_OK) {456return IDecError(idec, dec->status);457}458459// Note: past this point, teardown() must always be called460// in case of error.461idec->state = STATE_VP8_DATA;462// Allocate memory and prepare everything.463if (!VP8InitFrame(dec, io)) {464return IDecError(idec, dec->status);465}466return VP8_STATUS_OK;467}468469// Remaining partitions470static VP8StatusCode DecodeRemaining(WebPIDecoder* const idec) {471VP8Decoder* const dec = (VP8Decoder*)idec->dec;472VP8Io* const io = &idec->io;473474// Make sure partition #0 has been read before, to set dec to ready.475if (!dec->ready) {476return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);477}478for (; dec->mb_y < dec->mb_h; ++dec->mb_y) {479if (idec->last_mb_y != dec->mb_y) {480if (!VP8ParseIntraModeRow(&dec->br, dec)) {481// note: normally, error shouldn't occur since we already have the whole482// partition0 available here in DecodeRemaining(). Reaching EOF while483// reading intra modes really means a BITSTREAM_ERROR.484return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);485}486idec->last_mb_y = dec->mb_y;487}488for (; dec->mb_x < dec->mb_w; ++dec->mb_x) {489VP8BitReader* const token_br =490&dec->parts[dec->mb_y & dec->num_parts_minus_one];491MBContext context;492SaveContext(dec, token_br, &context);493if (!VP8DecodeMB(dec, token_br)) {494// We shouldn't fail when MAX_MB data was available495if (dec->num_parts_minus_one == 0 &&496MemDataSize(&idec->mem) > MAX_MB_SIZE) {497return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);498}499// Synchronize the threads.500if (dec->mt_method > 0) {501if (!WebPGetWorkerInterface()->Sync(&dec->worker)) {502return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);503}504}505RestoreContext(&context, dec, token_br);506return VP8_STATUS_SUSPENDED;507}508// Release buffer only if there is only one partition509if (dec->num_parts_minus_one == 0) {510idec->mem.start = token_br->buf - idec->mem.buf;511assert(idec->mem.start <= idec->mem.end);512}513}514VP8InitScanline(dec); // Prepare for next scanline515516// Reconstruct, filter and emit the row.517if (!VP8ProcessRow(dec, io)) {518return IDecError(idec, VP8_STATUS_USER_ABORT);519}520}521// Synchronize the thread and check for errors.522if (!VP8ExitCritical(dec, io)) {523idec->state = STATE_ERROR; // prevent re-entry in IDecError524return IDecError(idec, VP8_STATUS_USER_ABORT);525}526dec->ready = 0;527return FinishDecoding(idec);528}529530static VP8StatusCode ErrorStatusLossless(WebPIDecoder* const idec,531VP8StatusCode status) {532if (status == VP8_STATUS_SUSPENDED || status == VP8_STATUS_NOT_ENOUGH_DATA) {533return VP8_STATUS_SUSPENDED;534}535return IDecError(idec, status);536}537538static VP8StatusCode DecodeVP8LHeader(WebPIDecoder* const idec) {539VP8Io* const io = &idec->io;540VP8LDecoder* const dec = (VP8LDecoder*)idec->dec;541const WebPDecParams* const params = &idec->params;542WebPDecBuffer* const output = params->output;543size_t curr_size = MemDataSize(&idec->mem);544assert(idec->is_lossless);545546// Wait until there's enough data for decoding header.547if (curr_size < (idec->chunk_size >> 3)) {548dec->status = VP8_STATUS_SUSPENDED;549return ErrorStatusLossless(idec, dec->status);550}551552if (!VP8LDecodeHeader(dec, io)) {553if (dec->status == VP8_STATUS_BITSTREAM_ERROR &&554curr_size < idec->chunk_size) {555dec->status = VP8_STATUS_SUSPENDED;556}557return ErrorStatusLossless(idec, dec->status);558}559// Allocate/verify output buffer now.560dec->status = WebPAllocateDecBuffer(io->width, io->height, params->options,561output);562if (dec->status != VP8_STATUS_OK) {563return IDecError(idec, dec->status);564}565566idec->state = STATE_VP8L_DATA;567return VP8_STATUS_OK;568}569570static VP8StatusCode DecodeVP8LData(WebPIDecoder* const idec) {571VP8LDecoder* const dec = (VP8LDecoder*)idec->dec;572const size_t curr_size = MemDataSize(&idec->mem);573assert(idec->is_lossless);574575// Switch to incremental decoding if we don't have all the bytes available.576dec->incremental = (curr_size < idec->chunk_size);577578if (!VP8LDecodeImage(dec)) {579return ErrorStatusLossless(idec, dec->status);580}581assert(dec->status == VP8_STATUS_OK || dec->status == VP8_STATUS_SUSPENDED);582return (dec->status == VP8_STATUS_SUSPENDED) ? dec->status583: FinishDecoding(idec);584}585586// Main decoding loop587static VP8StatusCode IDecode(WebPIDecoder* idec) {588VP8StatusCode status = VP8_STATUS_SUSPENDED;589590if (idec->state == STATE_WEBP_HEADER) {591status = DecodeWebPHeaders(idec);592} else {593if (idec->dec == NULL) {594return VP8_STATUS_SUSPENDED; // can't continue if we have no decoder.595}596}597if (idec->state == STATE_VP8_HEADER) {598status = DecodeVP8FrameHeader(idec);599}600if (idec->state == STATE_VP8_PARTS0) {601status = DecodePartition0(idec);602}603if (idec->state == STATE_VP8_DATA) {604const VP8Decoder* const dec = (VP8Decoder*)idec->dec;605if (dec == NULL) {606return VP8_STATUS_SUSPENDED; // can't continue if we have no decoder.607}608status = DecodeRemaining(idec);609}610if (idec->state == STATE_VP8L_HEADER) {611status = DecodeVP8LHeader(idec);612}613if (idec->state == STATE_VP8L_DATA) {614status = DecodeVP8LData(idec);615}616return status;617}618619//------------------------------------------------------------------------------620// Internal constructor621622WEBP_NODISCARD static WebPIDecoder* NewDecoder(623WebPDecBuffer* const output_buffer,624const WebPBitstreamFeatures* const features) {625WebPIDecoder* idec = (WebPIDecoder*)WebPSafeCalloc(1ULL, sizeof(*idec));626if (idec == NULL) {627return NULL;628}629630idec->state = STATE_WEBP_HEADER;631idec->chunk_size = 0;632633idec->last_mb_y = -1;634635InitMemBuffer(&idec->mem);636if (!WebPInitDecBuffer(&idec->output) || !VP8InitIo(&idec->io)) {637WebPSafeFree(idec);638return NULL;639}640641WebPResetDecParams(&idec->params);642if (output_buffer == NULL || WebPAvoidSlowMemory(output_buffer, features)) {643idec->params.output = &idec->output;644idec->final_output = output_buffer;645if (output_buffer != NULL) {646idec->params.output->colorspace = output_buffer->colorspace;647}648} else {649idec->params.output = output_buffer;650idec->final_output = NULL;651}652WebPInitCustomIo(&idec->params, &idec->io); // Plug the I/O functions.653654return idec;655}656657//------------------------------------------------------------------------------658// Public functions659660WebPIDecoder* WebPINewDecoder(WebPDecBuffer* output_buffer) {661return NewDecoder(output_buffer, NULL);662}663664WebPIDecoder* WebPIDecode(const uint8_t* data, size_t data_size,665WebPDecoderConfig* config) {666WebPIDecoder* idec;667WebPBitstreamFeatures tmp_features;668WebPBitstreamFeatures* const features =669(config == NULL) ? &tmp_features : &config->input;670memset(&tmp_features, 0, sizeof(tmp_features));671672// Parse the bitstream's features, if requested:673if (data != NULL && data_size > 0) {674if (WebPGetFeatures(data, data_size, features) != VP8_STATUS_OK) {675return NULL;676}677}678679// Create an instance of the incremental decoder680idec = (config != NULL) ? NewDecoder(&config->output, features)681: NewDecoder(NULL, features);682if (idec == NULL) {683return NULL;684}685// Finish initialization686if (config != NULL) {687idec->params.options = &config->options;688}689return idec;690}691692void WebPIDelete(WebPIDecoder* idec) {693if (idec == NULL) return;694if (idec->dec != NULL) {695if (!idec->is_lossless) {696if (idec->state == STATE_VP8_DATA) {697// Synchronize the thread, clean-up and check for errors.698// TODO(vrabaud) do we care about the return result?699(void)VP8ExitCritical((VP8Decoder*)idec->dec, &idec->io);700}701VP8Delete((VP8Decoder*)idec->dec);702} else {703VP8LDelete((VP8LDecoder*)idec->dec);704}705}706ClearMemBuffer(&idec->mem);707WebPFreeDecBuffer(&idec->output);708WebPSafeFree(idec);709}710711//------------------------------------------------------------------------------712// Wrapper toward WebPINewDecoder713714WebPIDecoder* WebPINewRGB(WEBP_CSP_MODE csp, uint8_t* output_buffer,715size_t output_buffer_size, int output_stride) {716const int is_external_memory = (output_buffer != NULL) ? 1 : 0;717WebPIDecoder* idec;718719if (csp >= MODE_YUV) return NULL;720if (is_external_memory == 0) { // Overwrite parameters to sane values.721output_buffer_size = 0;722output_stride = 0;723} else { // A buffer was passed. Validate the other params.724if (output_stride == 0 || output_buffer_size == 0) {725return NULL; // invalid parameter.726}727}728idec = WebPINewDecoder(NULL);729if (idec == NULL) return NULL;730idec->output.colorspace = csp;731idec->output.is_external_memory = is_external_memory;732idec->output.u.RGBA.rgba = output_buffer;733idec->output.u.RGBA.stride = output_stride;734idec->output.u.RGBA.size = output_buffer_size;735return idec;736}737738WebPIDecoder* WebPINewYUVA(uint8_t* luma, size_t luma_size, int luma_stride,739uint8_t* u, size_t u_size, int u_stride,740uint8_t* v, size_t v_size, int v_stride,741uint8_t* a, size_t a_size, int a_stride) {742const int is_external_memory = (luma != NULL) ? 1 : 0;743WebPIDecoder* idec;744WEBP_CSP_MODE colorspace;745746if (is_external_memory == 0) { // Overwrite parameters to sane values.747luma_size = u_size = v_size = a_size = 0;748luma_stride = u_stride = v_stride = a_stride = 0;749u = v = a = NULL;750colorspace = MODE_YUVA;751} else { // A luma buffer was passed. Validate the other parameters.752if (u == NULL || v == NULL) return NULL;753if (luma_size == 0 || u_size == 0 || v_size == 0) return NULL;754if (luma_stride == 0 || u_stride == 0 || v_stride == 0) return NULL;755if (a != NULL) {756if (a_size == 0 || a_stride == 0) return NULL;757}758colorspace = (a == NULL) ? MODE_YUV : MODE_YUVA;759}760761idec = WebPINewDecoder(NULL);762if (idec == NULL) return NULL;763764idec->output.colorspace = colorspace;765idec->output.is_external_memory = is_external_memory;766idec->output.u.YUVA.y = luma;767idec->output.u.YUVA.y_stride = luma_stride;768idec->output.u.YUVA.y_size = luma_size;769idec->output.u.YUVA.u = u;770idec->output.u.YUVA.u_stride = u_stride;771idec->output.u.YUVA.u_size = u_size;772idec->output.u.YUVA.v = v;773idec->output.u.YUVA.v_stride = v_stride;774idec->output.u.YUVA.v_size = v_size;775idec->output.u.YUVA.a = a;776idec->output.u.YUVA.a_stride = a_stride;777idec->output.u.YUVA.a_size = a_size;778return idec;779}780781WebPIDecoder* WebPINewYUV(uint8_t* luma, size_t luma_size, int luma_stride,782uint8_t* u, size_t u_size, int u_stride,783uint8_t* v, size_t v_size, int v_stride) {784return WebPINewYUVA(luma, luma_size, luma_stride,785u, u_size, u_stride,786v, v_size, v_stride,787NULL, 0, 0);788}789790//------------------------------------------------------------------------------791792static VP8StatusCode IDecCheckStatus(const WebPIDecoder* const idec) {793assert(idec);794if (idec->state == STATE_ERROR) {795return VP8_STATUS_BITSTREAM_ERROR;796}797if (idec->state == STATE_DONE) {798return VP8_STATUS_OK;799}800return VP8_STATUS_SUSPENDED;801}802803VP8StatusCode WebPIAppend(WebPIDecoder* idec,804const uint8_t* data, size_t data_size) {805VP8StatusCode status;806if (idec == NULL || data == NULL) {807return VP8_STATUS_INVALID_PARAM;808}809status = IDecCheckStatus(idec);810if (status != VP8_STATUS_SUSPENDED) {811return status;812}813// Check mixed calls between RemapMemBuffer and AppendToMemBuffer.814if (!CheckMemBufferMode(&idec->mem, MEM_MODE_APPEND)) {815return VP8_STATUS_INVALID_PARAM;816}817// Append data to memory buffer818if (!AppendToMemBuffer(idec, data, data_size)) {819return VP8_STATUS_OUT_OF_MEMORY;820}821return IDecode(idec);822}823824VP8StatusCode WebPIUpdate(WebPIDecoder* idec,825const uint8_t* data, size_t data_size) {826VP8StatusCode status;827if (idec == NULL || data == NULL) {828return VP8_STATUS_INVALID_PARAM;829}830status = IDecCheckStatus(idec);831if (status != VP8_STATUS_SUSPENDED) {832return status;833}834// Check mixed calls between RemapMemBuffer and AppendToMemBuffer.835if (!CheckMemBufferMode(&idec->mem, MEM_MODE_MAP)) {836return VP8_STATUS_INVALID_PARAM;837}838// Make the memory buffer point to the new buffer839if (!RemapMemBuffer(idec, data, data_size)) {840return VP8_STATUS_INVALID_PARAM;841}842return IDecode(idec);843}844845//------------------------------------------------------------------------------846847static const WebPDecBuffer* GetOutputBuffer(const WebPIDecoder* const idec) {848if (idec == NULL || idec->dec == NULL) {849return NULL;850}851if (idec->state <= STATE_VP8_PARTS0) {852return NULL;853}854if (idec->final_output != NULL) {855return NULL; // not yet slow-copied856}857return idec->params.output;858}859860const WebPDecBuffer* WebPIDecodedArea(const WebPIDecoder* idec,861int* left, int* top,862int* width, int* height) {863const WebPDecBuffer* const src = GetOutputBuffer(idec);864if (left != NULL) *left = 0;865if (top != NULL) *top = 0;866if (src != NULL) {867if (width != NULL) *width = src->width;868if (height != NULL) *height = idec->params.last_y;869} else {870if (width != NULL) *width = 0;871if (height != NULL) *height = 0;872}873return src;874}875876WEBP_NODISCARD uint8_t* WebPIDecGetRGB(const WebPIDecoder* idec, int* last_y,877int* width, int* height, int* stride) {878const WebPDecBuffer* const src = GetOutputBuffer(idec);879if (src == NULL) return NULL;880if (src->colorspace >= MODE_YUV) {881return NULL;882}883884if (last_y != NULL) *last_y = idec->params.last_y;885if (width != NULL) *width = src->width;886if (height != NULL) *height = src->height;887if (stride != NULL) *stride = src->u.RGBA.stride;888889return src->u.RGBA.rgba;890}891892WEBP_NODISCARD uint8_t* WebPIDecGetYUVA(const WebPIDecoder* idec, int* last_y,893uint8_t** u, uint8_t** v, uint8_t** a,894int* width, int* height, int* stride,895int* uv_stride, int* a_stride) {896const WebPDecBuffer* const src = GetOutputBuffer(idec);897if (src == NULL) return NULL;898if (src->colorspace < MODE_YUV) {899return NULL;900}901902if (last_y != NULL) *last_y = idec->params.last_y;903if (u != NULL) *u = src->u.YUVA.u;904if (v != NULL) *v = src->u.YUVA.v;905if (a != NULL) *a = src->u.YUVA.a;906if (width != NULL) *width = src->width;907if (height != NULL) *height = src->height;908if (stride != NULL) *stride = src->u.YUVA.y_stride;909if (uv_stride != NULL) *uv_stride = src->u.YUVA.u_stride;910if (a_stride != NULL) *a_stride = src->u.YUVA.a_stride;911912return src->u.YUVA.y;913}914915int WebPISetIOHooks(WebPIDecoder* const idec,916VP8IoPutHook put,917VP8IoSetupHook setup,918VP8IoTeardownHook teardown,919void* user_data) {920if (idec == NULL || idec->state > STATE_WEBP_HEADER) {921return 0;922}923924idec->io.put = put;925idec->io.setup = setup;926idec->io.teardown = teardown;927idec->io.opaque = user_data;928929return 1;930}931932933