Path: blob/master/thirdparty/libwebp/src/dec/vp8_dec.c
9913 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 entry for the decoder10//11// Author: Skal ([email protected])1213#include <stdlib.h>1415#include "src/dec/alphai_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/bit_reader_inl_utils.h"20#include "src/utils/utils.h"2122//------------------------------------------------------------------------------2324int WebPGetDecoderVersion(void) {25return (DEC_MAJ_VERSION << 16) | (DEC_MIN_VERSION << 8) | DEC_REV_VERSION;26}2728//------------------------------------------------------------------------------29// Signature and pointer-to-function for GetCoeffs() variants below.3031typedef int (*GetCoeffsFunc)(VP8BitReader* const br,32const VP8BandProbas* const prob[],33int ctx, const quant_t dq, int n, int16_t* out);34static volatile GetCoeffsFunc GetCoeffs = NULL;3536static void InitGetCoeffs(void);3738//------------------------------------------------------------------------------39// VP8Decoder4041static void SetOk(VP8Decoder* const dec) {42dec->status_ = VP8_STATUS_OK;43dec->error_msg_ = "OK";44}4546int VP8InitIoInternal(VP8Io* const io, int version) {47if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {48return 0; // mismatch error49}50if (io != NULL) {51memset(io, 0, sizeof(*io));52}53return 1;54}5556VP8Decoder* VP8New(void) {57VP8Decoder* const dec = (VP8Decoder*)WebPSafeCalloc(1ULL, sizeof(*dec));58if (dec != NULL) {59SetOk(dec);60WebPGetWorkerInterface()->Init(&dec->worker_);61dec->ready_ = 0;62dec->num_parts_minus_one_ = 0;63InitGetCoeffs();64}65return dec;66}6768VP8StatusCode VP8Status(VP8Decoder* const dec) {69if (!dec) return VP8_STATUS_INVALID_PARAM;70return dec->status_;71}7273const char* VP8StatusMessage(VP8Decoder* const dec) {74if (dec == NULL) return "no object";75if (!dec->error_msg_) return "OK";76return dec->error_msg_;77}7879void VP8Delete(VP8Decoder* const dec) {80if (dec != NULL) {81VP8Clear(dec);82WebPSafeFree(dec);83}84}8586int VP8SetError(VP8Decoder* const dec,87VP8StatusCode error, const char* const msg) {88// VP8_STATUS_SUSPENDED is only meaningful in incremental decoding.89assert(dec->incremental_ || error != VP8_STATUS_SUSPENDED);90// The oldest error reported takes precedence over the new one.91if (dec->status_ == VP8_STATUS_OK) {92dec->status_ = error;93dec->error_msg_ = msg;94dec->ready_ = 0;95}96return 0;97}9899//------------------------------------------------------------------------------100101int VP8CheckSignature(const uint8_t* const data, size_t data_size) {102return (data_size >= 3 &&103data[0] == 0x9d && data[1] == 0x01 && data[2] == 0x2a);104}105106int VP8GetInfo(const uint8_t* data, size_t data_size, size_t chunk_size,107int* const width, int* const height) {108if (data == NULL || data_size < VP8_FRAME_HEADER_SIZE) {109return 0; // not enough data110}111// check signature112if (!VP8CheckSignature(data + 3, data_size - 3)) {113return 0; // Wrong signature.114} else {115const uint32_t bits = data[0] | (data[1] << 8) | (data[2] << 16);116const int key_frame = !(bits & 1);117const int w = ((data[7] << 8) | data[6]) & 0x3fff;118const int h = ((data[9] << 8) | data[8]) & 0x3fff;119120if (!key_frame) { // Not a keyframe.121return 0;122}123124if (((bits >> 1) & 7) > 3) {125return 0; // unknown profile126}127if (!((bits >> 4) & 1)) {128return 0; // first frame is invisible!129}130if (((bits >> 5)) >= chunk_size) { // partition_length131return 0; // inconsistent size information.132}133if (w == 0 || h == 0) {134return 0; // We don't support both width and height to be zero.135}136137if (width) {138*width = w;139}140if (height) {141*height = h;142}143144return 1;145}146}147148//------------------------------------------------------------------------------149// Header parsing150151static void ResetSegmentHeader(VP8SegmentHeader* const hdr) {152assert(hdr != NULL);153hdr->use_segment_ = 0;154hdr->update_map_ = 0;155hdr->absolute_delta_ = 1;156memset(hdr->quantizer_, 0, sizeof(hdr->quantizer_));157memset(hdr->filter_strength_, 0, sizeof(hdr->filter_strength_));158}159160// Paragraph 9.3161static int ParseSegmentHeader(VP8BitReader* br,162VP8SegmentHeader* hdr, VP8Proba* proba) {163assert(br != NULL);164assert(hdr != NULL);165hdr->use_segment_ = VP8Get(br, "global-header");166if (hdr->use_segment_) {167hdr->update_map_ = VP8Get(br, "global-header");168if (VP8Get(br, "global-header")) { // update data169int s;170hdr->absolute_delta_ = VP8Get(br, "global-header");171for (s = 0; s < NUM_MB_SEGMENTS; ++s) {172hdr->quantizer_[s] = VP8Get(br, "global-header") ?173VP8GetSignedValue(br, 7, "global-header") : 0;174}175for (s = 0; s < NUM_MB_SEGMENTS; ++s) {176hdr->filter_strength_[s] = VP8Get(br, "global-header") ?177VP8GetSignedValue(br, 6, "global-header") : 0;178}179}180if (hdr->update_map_) {181int s;182for (s = 0; s < MB_FEATURE_TREE_PROBS; ++s) {183proba->segments_[s] = VP8Get(br, "global-header") ?184VP8GetValue(br, 8, "global-header") : 255u;185}186}187} else {188hdr->update_map_ = 0;189}190return !br->eof_;191}192193// Paragraph 9.5194// If we don't have all the necessary data in 'buf', this function returns195// VP8_STATUS_SUSPENDED in incremental decoding, VP8_STATUS_NOT_ENOUGH_DATA196// otherwise.197// In incremental decoding, this case is not necessarily an error. Still, no198// bitreader is ever initialized to make it possible to read unavailable memory.199// If we don't even have the partitions' sizes, then VP8_STATUS_NOT_ENOUGH_DATA200// is returned, and this is an unrecoverable error.201// If the partitions were positioned ok, VP8_STATUS_OK is returned.202static VP8StatusCode ParsePartitions(VP8Decoder* const dec,203const uint8_t* buf, size_t size) {204VP8BitReader* const br = &dec->br_;205const uint8_t* sz = buf;206const uint8_t* buf_end = buf + size;207const uint8_t* part_start;208size_t size_left = size;209size_t last_part;210size_t p;211212dec->num_parts_minus_one_ = (1 << VP8GetValue(br, 2, "global-header")) - 1;213last_part = dec->num_parts_minus_one_;214if (size < 3 * last_part) {215// we can't even read the sizes with sz[]! That's a failure.216return VP8_STATUS_NOT_ENOUGH_DATA;217}218part_start = buf + last_part * 3;219size_left -= last_part * 3;220for (p = 0; p < last_part; ++p) {221size_t psize = sz[0] | (sz[1] << 8) | (sz[2] << 16);222if (psize > size_left) psize = size_left;223VP8InitBitReader(dec->parts_ + p, part_start, psize);224part_start += psize;225size_left -= psize;226sz += 3;227}228VP8InitBitReader(dec->parts_ + last_part, part_start, size_left);229if (part_start < buf_end) return VP8_STATUS_OK;230return dec->incremental_231? VP8_STATUS_SUSPENDED // Init is ok, but there's not enough data232: VP8_STATUS_NOT_ENOUGH_DATA;233}234235// Paragraph 9.4236static int ParseFilterHeader(VP8BitReader* br, VP8Decoder* const dec) {237VP8FilterHeader* const hdr = &dec->filter_hdr_;238hdr->simple_ = VP8Get(br, "global-header");239hdr->level_ = VP8GetValue(br, 6, "global-header");240hdr->sharpness_ = VP8GetValue(br, 3, "global-header");241hdr->use_lf_delta_ = VP8Get(br, "global-header");242if (hdr->use_lf_delta_) {243if (VP8Get(br, "global-header")) { // update lf-delta?244int i;245for (i = 0; i < NUM_REF_LF_DELTAS; ++i) {246if (VP8Get(br, "global-header")) {247hdr->ref_lf_delta_[i] = VP8GetSignedValue(br, 6, "global-header");248}249}250for (i = 0; i < NUM_MODE_LF_DELTAS; ++i) {251if (VP8Get(br, "global-header")) {252hdr->mode_lf_delta_[i] = VP8GetSignedValue(br, 6, "global-header");253}254}255}256}257dec->filter_type_ = (hdr->level_ == 0) ? 0 : hdr->simple_ ? 1 : 2;258return !br->eof_;259}260261// Topmost call262int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {263const uint8_t* buf;264size_t buf_size;265VP8FrameHeader* frm_hdr;266VP8PictureHeader* pic_hdr;267VP8BitReader* br;268VP8StatusCode status;269270if (dec == NULL) {271return 0;272}273SetOk(dec);274if (io == NULL) {275return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,276"null VP8Io passed to VP8GetHeaders()");277}278buf = io->data;279buf_size = io->data_size;280if (buf_size < 4) {281return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,282"Truncated header.");283}284285// Paragraph 9.1286{287const uint32_t bits = buf[0] | (buf[1] << 8) | (buf[2] << 16);288frm_hdr = &dec->frm_hdr_;289frm_hdr->key_frame_ = !(bits & 1);290frm_hdr->profile_ = (bits >> 1) & 7;291frm_hdr->show_ = (bits >> 4) & 1;292frm_hdr->partition_length_ = (bits >> 5);293if (frm_hdr->profile_ > 3) {294return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,295"Incorrect keyframe parameters.");296}297if (!frm_hdr->show_) {298return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,299"Frame not displayable.");300}301buf += 3;302buf_size -= 3;303}304305pic_hdr = &dec->pic_hdr_;306if (frm_hdr->key_frame_) {307// Paragraph 9.2308if (buf_size < 7) {309return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,310"cannot parse picture header");311}312if (!VP8CheckSignature(buf, buf_size)) {313return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,314"Bad code word");315}316pic_hdr->width_ = ((buf[4] << 8) | buf[3]) & 0x3fff;317pic_hdr->xscale_ = buf[4] >> 6; // ratio: 1, 5/4 5/3 or 2318pic_hdr->height_ = ((buf[6] << 8) | buf[5]) & 0x3fff;319pic_hdr->yscale_ = buf[6] >> 6;320buf += 7;321buf_size -= 7;322323dec->mb_w_ = (pic_hdr->width_ + 15) >> 4;324dec->mb_h_ = (pic_hdr->height_ + 15) >> 4;325326// Setup default output area (can be later modified during io->setup())327io->width = pic_hdr->width_;328io->height = pic_hdr->height_;329// IMPORTANT! use some sane dimensions in crop_* and scaled_* fields.330// So they can be used interchangeably without always testing for331// 'use_cropping'.332io->use_cropping = 0;333io->crop_top = 0;334io->crop_left = 0;335io->crop_right = io->width;336io->crop_bottom = io->height;337io->use_scaling = 0;338io->scaled_width = io->width;339io->scaled_height = io->height;340341io->mb_w = io->width; // for soundness342io->mb_h = io->height; // ditto343344VP8ResetProba(&dec->proba_);345ResetSegmentHeader(&dec->segment_hdr_);346}347348// Check if we have all the partition #0 available, and initialize dec->br_349// to read this partition (and this partition only).350if (frm_hdr->partition_length_ > buf_size) {351return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,352"bad partition length");353}354355br = &dec->br_;356VP8InitBitReader(br, buf, frm_hdr->partition_length_);357buf += frm_hdr->partition_length_;358buf_size -= frm_hdr->partition_length_;359360if (frm_hdr->key_frame_) {361pic_hdr->colorspace_ = VP8Get(br, "global-header");362pic_hdr->clamp_type_ = VP8Get(br, "global-header");363}364if (!ParseSegmentHeader(br, &dec->segment_hdr_, &dec->proba_)) {365return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,366"cannot parse segment header");367}368// Filter specs369if (!ParseFilterHeader(br, dec)) {370return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,371"cannot parse filter header");372}373status = ParsePartitions(dec, buf, buf_size);374if (status != VP8_STATUS_OK) {375return VP8SetError(dec, status, "cannot parse partitions");376}377378// quantizer change379VP8ParseQuant(dec);380381// Frame buffer marking382if (!frm_hdr->key_frame_) {383return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,384"Not a key frame.");385}386387VP8Get(br, "global-header"); // ignore the value of update_proba_388389VP8ParseProba(br, dec);390391// sanitized state392dec->ready_ = 1;393return 1;394}395396//------------------------------------------------------------------------------397// Residual decoding (Paragraph 13.2 / 13.3)398399static const uint8_t kCat3[] = { 173, 148, 140, 0 };400static const uint8_t kCat4[] = { 176, 155, 140, 135, 0 };401static const uint8_t kCat5[] = { 180, 157, 141, 134, 130, 0 };402static const uint8_t kCat6[] =403{ 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0 };404static const uint8_t* const kCat3456[] = { kCat3, kCat4, kCat5, kCat6 };405static const uint8_t kZigzag[16] = {4060, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15407};408409// See section 13-2: https://datatracker.ietf.org/doc/html/rfc6386#section-13.2410static int GetLargeValue(VP8BitReader* const br, const uint8_t* const p) {411int v;412if (!VP8GetBit(br, p[3], "coeffs")) {413if (!VP8GetBit(br, p[4], "coeffs")) {414v = 2;415} else {416v = 3 + VP8GetBit(br, p[5], "coeffs");417}418} else {419if (!VP8GetBit(br, p[6], "coeffs")) {420if (!VP8GetBit(br, p[7], "coeffs")) {421v = 5 + VP8GetBit(br, 159, "coeffs");422} else {423v = 7 + 2 * VP8GetBit(br, 165, "coeffs");424v += VP8GetBit(br, 145, "coeffs");425}426} else {427const uint8_t* tab;428const int bit1 = VP8GetBit(br, p[8], "coeffs");429const int bit0 = VP8GetBit(br, p[9 + bit1], "coeffs");430const int cat = 2 * bit1 + bit0;431v = 0;432for (tab = kCat3456[cat]; *tab; ++tab) {433v += v + VP8GetBit(br, *tab, "coeffs");434}435v += 3 + (8 << cat);436}437}438return v;439}440441// Returns the position of the last non-zero coeff plus one442static int GetCoeffsFast(VP8BitReader* const br,443const VP8BandProbas* const prob[],444int ctx, const quant_t dq, int n, int16_t* out) {445const uint8_t* p = prob[n]->probas_[ctx];446for (; n < 16; ++n) {447if (!VP8GetBit(br, p[0], "coeffs")) {448return n; // previous coeff was last non-zero coeff449}450while (!VP8GetBit(br, p[1], "coeffs")) { // sequence of zero coeffs451p = prob[++n]->probas_[0];452if (n == 16) return 16;453}454{ // non zero coeff455const VP8ProbaArray* const p_ctx = &prob[n + 1]->probas_[0];456int v;457if (!VP8GetBit(br, p[2], "coeffs")) {458v = 1;459p = p_ctx[1];460} else {461v = GetLargeValue(br, p);462p = p_ctx[2];463}464out[kZigzag[n]] = VP8GetSigned(br, v, "coeffs") * dq[n > 0];465}466}467return 16;468}469470// This version of GetCoeffs() uses VP8GetBitAlt() which is an alternate version471// of VP8GetBitAlt() targeting specific platforms.472static int GetCoeffsAlt(VP8BitReader* const br,473const VP8BandProbas* const prob[],474int ctx, const quant_t dq, int n, int16_t* out) {475const uint8_t* p = prob[n]->probas_[ctx];476for (; n < 16; ++n) {477if (!VP8GetBitAlt(br, p[0], "coeffs")) {478return n; // previous coeff was last non-zero coeff479}480while (!VP8GetBitAlt(br, p[1], "coeffs")) { // sequence of zero coeffs481p = prob[++n]->probas_[0];482if (n == 16) return 16;483}484{ // non zero coeff485const VP8ProbaArray* const p_ctx = &prob[n + 1]->probas_[0];486int v;487if (!VP8GetBitAlt(br, p[2], "coeffs")) {488v = 1;489p = p_ctx[1];490} else {491v = GetLargeValue(br, p);492p = p_ctx[2];493}494out[kZigzag[n]] = VP8GetSigned(br, v, "coeffs") * dq[n > 0];495}496}497return 16;498}499500extern VP8CPUInfo VP8GetCPUInfo;501502WEBP_DSP_INIT_FUNC(InitGetCoeffs) {503if (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kSlowSSSE3)) {504GetCoeffs = GetCoeffsAlt;505} else {506GetCoeffs = GetCoeffsFast;507}508}509510static WEBP_INLINE uint32_t NzCodeBits(uint32_t nz_coeffs, int nz, int dc_nz) {511nz_coeffs <<= 2;512nz_coeffs |= (nz > 3) ? 3 : (nz > 1) ? 2 : dc_nz;513return nz_coeffs;514}515516static int ParseResiduals(VP8Decoder* const dec,517VP8MB* const mb, VP8BitReader* const token_br) {518const VP8BandProbas* (* const bands)[16 + 1] = dec->proba_.bands_ptr_;519const VP8BandProbas* const * ac_proba;520VP8MBData* const block = dec->mb_data_ + dec->mb_x_;521const VP8QuantMatrix* const q = &dec->dqm_[block->segment_];522int16_t* dst = block->coeffs_;523VP8MB* const left_mb = dec->mb_info_ - 1;524uint8_t tnz, lnz;525uint32_t non_zero_y = 0;526uint32_t non_zero_uv = 0;527int x, y, ch;528uint32_t out_t_nz, out_l_nz;529int first;530531memset(dst, 0, 384 * sizeof(*dst));532if (!block->is_i4x4_) { // parse DC533int16_t dc[16] = { 0 };534const int ctx = mb->nz_dc_ + left_mb->nz_dc_;535const int nz = GetCoeffs(token_br, bands[1], ctx, q->y2_mat_, 0, dc);536mb->nz_dc_ = left_mb->nz_dc_ = (nz > 0);537if (nz > 1) { // more than just the DC -> perform the full transform538VP8TransformWHT(dc, dst);539} else { // only DC is non-zero -> inlined simplified transform540int i;541const int dc0 = (dc[0] + 3) >> 3;542for (i = 0; i < 16 * 16; i += 16) dst[i] = dc0;543}544first = 1;545ac_proba = bands[0];546} else {547first = 0;548ac_proba = bands[3];549}550551tnz = mb->nz_ & 0x0f;552lnz = left_mb->nz_ & 0x0f;553for (y = 0; y < 4; ++y) {554int l = lnz & 1;555uint32_t nz_coeffs = 0;556for (x = 0; x < 4; ++x) {557const int ctx = l + (tnz & 1);558const int nz = GetCoeffs(token_br, ac_proba, ctx, q->y1_mat_, first, dst);559l = (nz > first);560tnz = (tnz >> 1) | (l << 7);561nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);562dst += 16;563}564tnz >>= 4;565lnz = (lnz >> 1) | (l << 7);566non_zero_y = (non_zero_y << 8) | nz_coeffs;567}568out_t_nz = tnz;569out_l_nz = lnz >> 4;570571for (ch = 0; ch < 4; ch += 2) {572uint32_t nz_coeffs = 0;573tnz = mb->nz_ >> (4 + ch);574lnz = left_mb->nz_ >> (4 + ch);575for (y = 0; y < 2; ++y) {576int l = lnz & 1;577for (x = 0; x < 2; ++x) {578const int ctx = l + (tnz & 1);579const int nz = GetCoeffs(token_br, bands[2], ctx, q->uv_mat_, 0, dst);580l = (nz > 0);581tnz = (tnz >> 1) | (l << 3);582nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);583dst += 16;584}585tnz >>= 2;586lnz = (lnz >> 1) | (l << 5);587}588// Note: we don't really need the per-4x4 details for U/V blocks.589non_zero_uv |= nz_coeffs << (4 * ch);590out_t_nz |= (tnz << 4) << ch;591out_l_nz |= (lnz & 0xf0) << ch;592}593mb->nz_ = out_t_nz;594left_mb->nz_ = out_l_nz;595596block->non_zero_y_ = non_zero_y;597block->non_zero_uv_ = non_zero_uv;598599// We look at the mode-code of each block and check if some blocks have less600// than three non-zero coeffs (code < 2). This is to avoid dithering flat and601// empty blocks.602block->dither_ = (non_zero_uv & 0xaaaa) ? 0 : q->dither_;603604return !(non_zero_y | non_zero_uv); // will be used for further optimization605}606607//------------------------------------------------------------------------------608// Main loop609610int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br) {611VP8MB* const left = dec->mb_info_ - 1;612VP8MB* const mb = dec->mb_info_ + dec->mb_x_;613VP8MBData* const block = dec->mb_data_ + dec->mb_x_;614int skip = dec->use_skip_proba_ ? block->skip_ : 0;615616if (!skip) {617skip = ParseResiduals(dec, mb, token_br);618} else {619left->nz_ = mb->nz_ = 0;620if (!block->is_i4x4_) {621left->nz_dc_ = mb->nz_dc_ = 0;622}623block->non_zero_y_ = 0;624block->non_zero_uv_ = 0;625block->dither_ = 0;626}627628if (dec->filter_type_ > 0) { // store filter info629VP8FInfo* const finfo = dec->f_info_ + dec->mb_x_;630*finfo = dec->fstrengths_[block->segment_][block->is_i4x4_];631finfo->f_inner_ |= !skip;632}633634return !token_br->eof_;635}636637void VP8InitScanline(VP8Decoder* const dec) {638VP8MB* const left = dec->mb_info_ - 1;639left->nz_ = 0;640left->nz_dc_ = 0;641memset(dec->intra_l_, B_DC_PRED, sizeof(dec->intra_l_));642dec->mb_x_ = 0;643}644645static int ParseFrame(VP8Decoder* const dec, VP8Io* io) {646for (dec->mb_y_ = 0; dec->mb_y_ < dec->br_mb_y_; ++dec->mb_y_) {647// Parse bitstream for this row.648VP8BitReader* const token_br =649&dec->parts_[dec->mb_y_ & dec->num_parts_minus_one_];650if (!VP8ParseIntraModeRow(&dec->br_, dec)) {651return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,652"Premature end-of-partition0 encountered.");653}654for (; dec->mb_x_ < dec->mb_w_; ++dec->mb_x_) {655if (!VP8DecodeMB(dec, token_br)) {656return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,657"Premature end-of-file encountered.");658}659}660VP8InitScanline(dec); // Prepare for next scanline661662// Reconstruct, filter and emit the row.663if (!VP8ProcessRow(dec, io)) {664return VP8SetError(dec, VP8_STATUS_USER_ABORT, "Output aborted.");665}666}667if (dec->mt_method_ > 0) {668if (!WebPGetWorkerInterface()->Sync(&dec->worker_)) return 0;669}670671return 1;672}673674// Main entry point675int VP8Decode(VP8Decoder* const dec, VP8Io* const io) {676int ok = 0;677if (dec == NULL) {678return 0;679}680if (io == NULL) {681return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,682"NULL VP8Io parameter in VP8Decode().");683}684685if (!dec->ready_) {686if (!VP8GetHeaders(dec, io)) {687return 0;688}689}690assert(dec->ready_);691692// Finish setting up the decoding parameter. Will call io->setup().693ok = (VP8EnterCritical(dec, io) == VP8_STATUS_OK);694if (ok) { // good to go.695// Will allocate memory and prepare everything.696if (ok) ok = VP8InitFrame(dec, io);697698// Main decoding loop699if (ok) ok = ParseFrame(dec, io);700701// Exit.702ok &= VP8ExitCritical(dec, io);703}704705if (!ok) {706VP8Clear(dec);707return 0;708}709710dec->ready_ = 0;711return ok;712}713714void VP8Clear(VP8Decoder* const dec) {715if (dec == NULL) {716return;717}718WebPGetWorkerInterface()->End(&dec->worker_);719WebPDeallocateAlphaMemory(dec);720WebPSafeFree(dec->mem_);721dec->mem_ = NULL;722dec->mem_size_ = 0;723memset(&dec->br_, 0, sizeof(dec->br_));724dec->ready_ = 0;725}726727//------------------------------------------------------------------------------728729730