Path: blob/master/thirdparty/libwebp/src/dec/vp8_dec.c
21372 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 <assert.h>14#include <stdlib.h>15#include <string.h>1617#include "src/dec/alphai_dec.h"18#include "src/dec/common_dec.h"19#include "src/dec/vp8_dec.h"20#include "src/dec/vp8i_dec.h"21#include "src/dec/vp8li_dec.h"22#include "src/dec/webpi_dec.h"23#include "src/dsp/cpu.h"24#include "src/dsp/dsp.h"25#include "src/utils/bit_reader_inl_utils.h"26#include "src/utils/bit_reader_utils.h"27#include "src/utils/thread_utils.h"28#include "src/utils/utils.h"29#include "src/webp/decode.h"30#include "src/webp/format_constants.h"31#include "src/webp/types.h"3233//------------------------------------------------------------------------------3435int WebPGetDecoderVersion(void) {36return (DEC_MAJ_VERSION << 16) | (DEC_MIN_VERSION << 8) | DEC_REV_VERSION;37}3839//------------------------------------------------------------------------------40// Signature and pointer-to-function for GetCoeffs() variants below.4142typedef int (*GetCoeffsFunc)(VP8BitReader* const br,43const VP8BandProbas* const prob[],44int ctx, const quant_t dq, int n, int16_t* out);45static volatile GetCoeffsFunc GetCoeffs = NULL;4647static void InitGetCoeffs(void);4849//------------------------------------------------------------------------------50// VP8Decoder5152static void SetOk(VP8Decoder* const dec) {53dec->status = VP8_STATUS_OK;54dec->error_msg = "OK";55}5657int VP8InitIoInternal(VP8Io* const io, int version) {58if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {59return 0; // mismatch error60}61if (io != NULL) {62memset(io, 0, sizeof(*io));63}64return 1;65}6667VP8Decoder* VP8New(void) {68VP8Decoder* const dec = (VP8Decoder*)WebPSafeCalloc(1ULL, sizeof(*dec));69if (dec != NULL) {70SetOk(dec);71WebPGetWorkerInterface()->Init(&dec->worker);72dec->ready = 0;73dec->num_parts_minus_one = 0;74InitGetCoeffs();75}76return dec;77}7879VP8StatusCode VP8Status(VP8Decoder* const dec) {80if (!dec) return VP8_STATUS_INVALID_PARAM;81return dec->status;82}8384const char* VP8StatusMessage(VP8Decoder* const dec) {85if (dec == NULL) return "no object";86if (!dec->error_msg) return "OK";87return dec->error_msg;88}8990void VP8Delete(VP8Decoder* const dec) {91if (dec != NULL) {92VP8Clear(dec);93WebPSafeFree(dec);94}95}9697int VP8SetError(VP8Decoder* const dec,98VP8StatusCode error, const char* const msg) {99// VP8_STATUS_SUSPENDED is only meaningful in incremental decoding.100assert(dec->incremental || error != VP8_STATUS_SUSPENDED);101// The oldest error reported takes precedence over the new one.102if (dec->status == VP8_STATUS_OK) {103dec->status = error;104dec->error_msg = msg;105dec->ready = 0;106}107return 0;108}109110//------------------------------------------------------------------------------111112int VP8CheckSignature(const uint8_t* const data, size_t data_size) {113return (data_size >= 3 &&114data[0] == 0x9d && data[1] == 0x01 && data[2] == 0x2a);115}116117int VP8GetInfo(const uint8_t* data, size_t data_size, size_t chunk_size,118int* const width, int* const height) {119if (data == NULL || data_size < VP8_FRAME_HEADER_SIZE) {120return 0; // not enough data121}122// check signature123if (!VP8CheckSignature(data + 3, data_size - 3)) {124return 0; // Wrong signature.125} else {126const uint32_t bits = data[0] | (data[1] << 8) | (data[2] << 16);127const int key_frame = !(bits & 1);128const int w = ((data[7] << 8) | data[6]) & 0x3fff;129const int h = ((data[9] << 8) | data[8]) & 0x3fff;130131if (!key_frame) { // Not a keyframe.132return 0;133}134135if (((bits >> 1) & 7) > 3) {136return 0; // unknown profile137}138if (!((bits >> 4) & 1)) {139return 0; // first frame is invisible!140}141if (((bits >> 5)) >= chunk_size) { // partition_length142return 0; // inconsistent size information.143}144if (w == 0 || h == 0) {145return 0; // We don't support both width and height to be zero.146}147148if (width) {149*width = w;150}151if (height) {152*height = h;153}154155return 1;156}157}158159//------------------------------------------------------------------------------160// Header parsing161162static void ResetSegmentHeader(VP8SegmentHeader* const hdr) {163assert(hdr != NULL);164hdr->use_segment = 0;165hdr->update_map = 0;166hdr->absolute_delta = 1;167memset(hdr->quantizer, 0, sizeof(hdr->quantizer));168memset(hdr->filter_strength, 0, sizeof(hdr->filter_strength));169}170171// Paragraph 9.3172static int ParseSegmentHeader(VP8BitReader* br,173VP8SegmentHeader* hdr, VP8Proba* proba) {174assert(br != NULL);175assert(hdr != NULL);176hdr->use_segment = VP8Get(br, "global-header");177if (hdr->use_segment) {178hdr->update_map = VP8Get(br, "global-header");179if (VP8Get(br, "global-header")) { // update data180int s;181hdr->absolute_delta = VP8Get(br, "global-header");182for (s = 0; s < NUM_MB_SEGMENTS; ++s) {183hdr->quantizer[s] = VP8Get(br, "global-header") ?184VP8GetSignedValue(br, 7, "global-header") : 0;185}186for (s = 0; s < NUM_MB_SEGMENTS; ++s) {187hdr->filter_strength[s] = VP8Get(br, "global-header") ?188VP8GetSignedValue(br, 6, "global-header") : 0;189}190}191if (hdr->update_map) {192int s;193for (s = 0; s < MB_FEATURE_TREE_PROBS; ++s) {194proba->segments[s] = VP8Get(br, "global-header") ?195VP8GetValue(br, 8, "global-header") : 255u;196}197}198} else {199hdr->update_map = 0;200}201return !br->eof;202}203204// Paragraph 9.5205// If we don't have all the necessary data in 'buf', this function returns206// VP8_STATUS_SUSPENDED in incremental decoding, VP8_STATUS_NOT_ENOUGH_DATA207// otherwise.208// In incremental decoding, this case is not necessarily an error. Still, no209// bitreader is ever initialized to make it possible to read unavailable memory.210// If we don't even have the partitions' sizes, then VP8_STATUS_NOT_ENOUGH_DATA211// is returned, and this is an unrecoverable error.212// If the partitions were positioned ok, VP8_STATUS_OK is returned.213static VP8StatusCode ParsePartitions(VP8Decoder* const dec,214const uint8_t* buf, size_t size) {215VP8BitReader* const br = &dec->br;216const uint8_t* sz = buf;217const uint8_t* buf_end = buf + size;218const uint8_t* part_start;219size_t size_left = size;220size_t last_part;221size_t p;222223dec->num_parts_minus_one = (1 << VP8GetValue(br, 2, "global-header")) - 1;224last_part = dec->num_parts_minus_one;225if (size < 3 * last_part) {226// we can't even read the sizes with sz[]! That's a failure.227return VP8_STATUS_NOT_ENOUGH_DATA;228}229part_start = buf + last_part * 3;230size_left -= last_part * 3;231for (p = 0; p < last_part; ++p) {232size_t psize = sz[0] | (sz[1] << 8) | (sz[2] << 16);233if (psize > size_left) psize = size_left;234VP8InitBitReader(dec->parts + p, part_start, psize);235part_start += psize;236size_left -= psize;237sz += 3;238}239VP8InitBitReader(dec->parts + last_part, part_start, size_left);240if (part_start < buf_end) return VP8_STATUS_OK;241return dec->incremental242? VP8_STATUS_SUSPENDED // Init is ok, but there's not enough data243: VP8_STATUS_NOT_ENOUGH_DATA;244}245246// Paragraph 9.4247static int ParseFilterHeader(VP8BitReader* br, VP8Decoder* const dec) {248VP8FilterHeader* const hdr = &dec->filter_hdr;249hdr->simple = VP8Get(br, "global-header");250hdr->level = VP8GetValue(br, 6, "global-header");251hdr->sharpness = VP8GetValue(br, 3, "global-header");252hdr->use_lf_delta = VP8Get(br, "global-header");253if (hdr->use_lf_delta) {254if (VP8Get(br, "global-header")) { // update lf-delta?255int i;256for (i = 0; i < NUM_REF_LF_DELTAS; ++i) {257if (VP8Get(br, "global-header")) {258hdr->ref_lf_delta[i] = VP8GetSignedValue(br, 6, "global-header");259}260}261for (i = 0; i < NUM_MODE_LF_DELTAS; ++i) {262if (VP8Get(br, "global-header")) {263hdr->mode_lf_delta[i] = VP8GetSignedValue(br, 6, "global-header");264}265}266}267}268dec->filter_type = (hdr->level == 0) ? 0 : hdr->simple ? 1 : 2;269return !br->eof;270}271272// Topmost call273int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {274const uint8_t* buf;275size_t buf_size;276VP8FrameHeader* frm_hdr;277VP8PictureHeader* pic_hdr;278VP8BitReader* br;279VP8StatusCode status;280281if (dec == NULL) {282return 0;283}284SetOk(dec);285if (io == NULL) {286return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,287"null VP8Io passed to VP8GetHeaders()");288}289buf = io->data;290buf_size = io->data_size;291if (buf_size < 4) {292return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,293"Truncated header.");294}295296// Paragraph 9.1297{298const uint32_t bits = buf[0] | (buf[1] << 8) | (buf[2] << 16);299frm_hdr = &dec->frm_hdr;300frm_hdr->key_frame = !(bits & 1);301frm_hdr->profile = (bits >> 1) & 7;302frm_hdr->show = (bits >> 4) & 1;303frm_hdr->partition_length = (bits >> 5);304if (frm_hdr->profile > 3) {305return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,306"Incorrect keyframe parameters.");307}308if (!frm_hdr->show) {309return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,310"Frame not displayable.");311}312buf += 3;313buf_size -= 3;314}315316pic_hdr = &dec->pic_hdr;317if (frm_hdr->key_frame) {318// Paragraph 9.2319if (buf_size < 7) {320return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,321"cannot parse picture header");322}323if (!VP8CheckSignature(buf, buf_size)) {324return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,325"Bad code word");326}327pic_hdr->width = ((buf[4] << 8) | buf[3]) & 0x3fff;328pic_hdr->xscale = buf[4] >> 6; // ratio: 1, 5/4 5/3 or 2329pic_hdr->height = ((buf[6] << 8) | buf[5]) & 0x3fff;330pic_hdr->yscale = buf[6] >> 6;331buf += 7;332buf_size -= 7;333334dec->mb_w = (pic_hdr->width + 15) >> 4;335dec->mb_h = (pic_hdr->height + 15) >> 4;336337// Setup default output area (can be later modified during io->setup())338io->width = pic_hdr->width;339io->height = pic_hdr->height;340// IMPORTANT! use some sane dimensions in crop* and scaled* fields.341// So they can be used interchangeably without always testing for342// 'use_cropping'.343io->use_cropping = 0;344io->crop_top = 0;345io->crop_left = 0;346io->crop_right = io->width;347io->crop_bottom = io->height;348io->use_scaling = 0;349io->scaled_width = io->width;350io->scaled_height = io->height;351352io->mb_w = io->width; // for soundness353io->mb_h = io->height; // ditto354355VP8ResetProba(&dec->proba);356ResetSegmentHeader(&dec->segment_hdr);357}358359// Check if we have all the partition #0 available, and initialize dec->br360// to read this partition (and this partition only).361if (frm_hdr->partition_length > buf_size) {362return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,363"bad partition length");364}365366br = &dec->br;367VP8InitBitReader(br, buf, frm_hdr->partition_length);368buf += frm_hdr->partition_length;369buf_size -= frm_hdr->partition_length;370371if (frm_hdr->key_frame) {372pic_hdr->colorspace = VP8Get(br, "global-header");373pic_hdr->clamp_type = VP8Get(br, "global-header");374}375if (!ParseSegmentHeader(br, &dec->segment_hdr, &dec->proba)) {376return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,377"cannot parse segment header");378}379// Filter specs380if (!ParseFilterHeader(br, dec)) {381return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,382"cannot parse filter header");383}384status = ParsePartitions(dec, buf, buf_size);385if (status != VP8_STATUS_OK) {386return VP8SetError(dec, status, "cannot parse partitions");387}388389// quantizer change390VP8ParseQuant(dec);391392// Frame buffer marking393if (!frm_hdr->key_frame) {394return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,395"Not a key frame.");396}397398VP8Get(br, "global-header"); // ignore the value of 'update_proba'399400VP8ParseProba(br, dec);401402// sanitized state403dec->ready = 1;404return 1;405}406407//------------------------------------------------------------------------------408// Residual decoding (Paragraph 13.2 / 13.3)409410static const uint8_t kCat3[] = { 173, 148, 140, 0 };411static const uint8_t kCat4[] = { 176, 155, 140, 135, 0 };412static const uint8_t kCat5[] = { 180, 157, 141, 134, 130, 0 };413static const uint8_t kCat6[] =414{ 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0 };415static const uint8_t* const kCat3456[] = { kCat3, kCat4, kCat5, kCat6 };416static const uint8_t kZigzag[16] = {4170, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15418};419420// See section 13-2: https://datatracker.ietf.org/doc/html/rfc6386#section-13.2421static int GetLargeValue(VP8BitReader* const br, const uint8_t* const p) {422int v;423if (!VP8GetBit(br, p[3], "coeffs")) {424if (!VP8GetBit(br, p[4], "coeffs")) {425v = 2;426} else {427v = 3 + VP8GetBit(br, p[5], "coeffs");428}429} else {430if (!VP8GetBit(br, p[6], "coeffs")) {431if (!VP8GetBit(br, p[7], "coeffs")) {432v = 5 + VP8GetBit(br, 159, "coeffs");433} else {434v = 7 + 2 * VP8GetBit(br, 165, "coeffs");435v += VP8GetBit(br, 145, "coeffs");436}437} else {438const uint8_t* tab;439const int bit1 = VP8GetBit(br, p[8], "coeffs");440const int bit0 = VP8GetBit(br, p[9 + bit1], "coeffs");441const int cat = 2 * bit1 + bit0;442v = 0;443for (tab = kCat3456[cat]; *tab; ++tab) {444v += v + VP8GetBit(br, *tab, "coeffs");445}446v += 3 + (8 << cat);447}448}449return v;450}451452// Returns the position of the last non-zero coeff plus one453static int GetCoeffsFast(VP8BitReader* const br,454const VP8BandProbas* const prob[],455int ctx, const quant_t dq, int n, int16_t* out) {456const uint8_t* p = prob[n]->probas[ctx];457for (; n < 16; ++n) {458if (!VP8GetBit(br, p[0], "coeffs")) {459return n; // previous coeff was last non-zero coeff460}461while (!VP8GetBit(br, p[1], "coeffs")) { // sequence of zero coeffs462p = prob[++n]->probas[0];463if (n == 16) return 16;464}465{ // non zero coeff466const VP8ProbaArray* const p_ctx = &prob[n + 1]->probas[0];467int v;468if (!VP8GetBit(br, p[2], "coeffs")) {469v = 1;470p = p_ctx[1];471} else {472v = GetLargeValue(br, p);473p = p_ctx[2];474}475out[kZigzag[n]] = VP8GetSigned(br, v, "coeffs") * dq[n > 0];476}477}478return 16;479}480481// This version of GetCoeffs() uses VP8GetBitAlt() which is an alternate version482// of VP8GetBitAlt() targeting specific platforms.483static int GetCoeffsAlt(VP8BitReader* const br,484const VP8BandProbas* const prob[],485int ctx, const quant_t dq, int n, int16_t* out) {486const uint8_t* p = prob[n]->probas[ctx];487for (; n < 16; ++n) {488if (!VP8GetBitAlt(br, p[0], "coeffs")) {489return n; // previous coeff was last non-zero coeff490}491while (!VP8GetBitAlt(br, p[1], "coeffs")) { // sequence of zero coeffs492p = prob[++n]->probas[0];493if (n == 16) return 16;494}495{ // non zero coeff496const VP8ProbaArray* const p_ctx = &prob[n + 1]->probas[0];497int v;498if (!VP8GetBitAlt(br, p[2], "coeffs")) {499v = 1;500p = p_ctx[1];501} else {502v = GetLargeValue(br, p);503p = p_ctx[2];504}505out[kZigzag[n]] = VP8GetSigned(br, v, "coeffs") * dq[n > 0];506}507}508return 16;509}510511extern VP8CPUInfo VP8GetCPUInfo;512513WEBP_DSP_INIT_FUNC(InitGetCoeffs) {514if (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kSlowSSSE3)) {515GetCoeffs = GetCoeffsAlt;516} else {517GetCoeffs = GetCoeffsFast;518}519}520521static WEBP_INLINE uint32_t NzCodeBits(uint32_t nz_coeffs, int nz, int dc_nz) {522nz_coeffs <<= 2;523nz_coeffs |= (nz > 3) ? 3 : (nz > 1) ? 2 : dc_nz;524return nz_coeffs;525}526527static int ParseResiduals(VP8Decoder* const dec,528VP8MB* const mb, VP8BitReader* const token_br) {529const VP8BandProbas* (* const bands)[16 + 1] = dec->proba.bands_ptr;530const VP8BandProbas* const * ac_proba;531VP8MBData* const block = dec->mb_data + dec->mb_x;532const VP8QuantMatrix* const q = &dec->dqm[block->segment];533int16_t* dst = block->coeffs;534VP8MB* const left_mb = dec->mb_info - 1;535uint8_t tnz, lnz;536uint32_t non_zero_y = 0;537uint32_t non_zero_uv = 0;538int x, y, ch;539uint32_t out_t_nz, out_l_nz;540int first;541542memset(dst, 0, 384 * sizeof(*dst));543if (!block->is_i4x4) { // parse DC544int16_t dc[16] = { 0 };545const int ctx = mb->nz_dc + left_mb->nz_dc;546const int nz = GetCoeffs(token_br, bands[1], ctx, q->y2_mat, 0, dc);547mb->nz_dc = left_mb->nz_dc = (nz > 0);548if (nz > 1) { // more than just the DC -> perform the full transform549VP8TransformWHT(dc, dst);550} else { // only DC is non-zero -> inlined simplified transform551int i;552const int dc0 = (dc[0] + 3) >> 3;553for (i = 0; i < 16 * 16; i += 16) dst[i] = dc0;554}555first = 1;556ac_proba = bands[0];557} else {558first = 0;559ac_proba = bands[3];560}561562tnz = mb->nz & 0x0f;563lnz = left_mb->nz & 0x0f;564for (y = 0; y < 4; ++y) {565int l = lnz & 1;566uint32_t nz_coeffs = 0;567for (x = 0; x < 4; ++x) {568const int ctx = l + (tnz & 1);569const int nz = GetCoeffs(token_br, ac_proba, ctx, q->y1_mat, first, dst);570l = (nz > first);571tnz = (tnz >> 1) | (l << 7);572nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);573dst += 16;574}575tnz >>= 4;576lnz = (lnz >> 1) | (l << 7);577non_zero_y = (non_zero_y << 8) | nz_coeffs;578}579out_t_nz = tnz;580out_l_nz = lnz >> 4;581582for (ch = 0; ch < 4; ch += 2) {583uint32_t nz_coeffs = 0;584tnz = mb->nz >> (4 + ch);585lnz = left_mb->nz >> (4 + ch);586for (y = 0; y < 2; ++y) {587int l = lnz & 1;588for (x = 0; x < 2; ++x) {589const int ctx = l + (tnz & 1);590const int nz = GetCoeffs(token_br, bands[2], ctx, q->uv_mat, 0, dst);591l = (nz > 0);592tnz = (tnz >> 1) | (l << 3);593nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);594dst += 16;595}596tnz >>= 2;597lnz = (lnz >> 1) | (l << 5);598}599// Note: we don't really need the per-4x4 details for U/V blocks.600non_zero_uv |= nz_coeffs << (4 * ch);601out_t_nz |= (tnz << 4) << ch;602out_l_nz |= (lnz & 0xf0) << ch;603}604mb->nz = out_t_nz;605left_mb->nz = out_l_nz;606607block->non_zero_y = non_zero_y;608block->non_zero_uv = non_zero_uv;609610// We look at the mode-code of each block and check if some blocks have less611// than three non-zero coeffs (code < 2). This is to avoid dithering flat and612// empty blocks.613block->dither = (non_zero_uv & 0xaaaa) ? 0 : q->dither;614615return !(non_zero_y | non_zero_uv); // will be used for further optimization616}617618//------------------------------------------------------------------------------619// Main loop620621int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br) {622VP8MB* const left = dec->mb_info - 1;623VP8MB* const mb = dec->mb_info + dec->mb_x;624VP8MBData* const block = dec->mb_data + dec->mb_x;625int skip = dec->use_skip_proba ? block->skip : 0;626627if (!skip) {628skip = ParseResiduals(dec, mb, token_br);629} else {630left->nz = mb->nz = 0;631if (!block->is_i4x4) {632left->nz_dc = mb->nz_dc = 0;633}634block->non_zero_y = 0;635block->non_zero_uv = 0;636block->dither = 0;637}638639if (dec->filter_type > 0) { // store filter info640VP8FInfo* const finfo = dec->f_info + dec->mb_x;641*finfo = dec->fstrengths[block->segment][block->is_i4x4];642finfo->f_inner |= !skip;643}644645return !token_br->eof;646}647648void VP8InitScanline(VP8Decoder* const dec) {649VP8MB* const left = dec->mb_info - 1;650left->nz = 0;651left->nz_dc = 0;652memset(dec->intra_l, B_DC_PRED, sizeof(dec->intra_l));653dec->mb_x = 0;654}655656static int ParseFrame(VP8Decoder* const dec, VP8Io* io) {657for (dec->mb_y = 0; dec->mb_y < dec->br_mb_y; ++dec->mb_y) {658// Parse bitstream for this row.659VP8BitReader* const token_br =660&dec->parts[dec->mb_y & dec->num_parts_minus_one];661if (!VP8ParseIntraModeRow(&dec->br, dec)) {662return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,663"Premature end-of-partition0 encountered.");664}665for (; dec->mb_x < dec->mb_w; ++dec->mb_x) {666if (!VP8DecodeMB(dec, token_br)) {667return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,668"Premature end-of-file encountered.");669}670}671VP8InitScanline(dec); // Prepare for next scanline672673// Reconstruct, filter and emit the row.674if (!VP8ProcessRow(dec, io)) {675return VP8SetError(dec, VP8_STATUS_USER_ABORT, "Output aborted.");676}677}678if (dec->mt_method > 0) {679if (!WebPGetWorkerInterface()->Sync(&dec->worker)) return 0;680}681682return 1;683}684685// Main entry point686int VP8Decode(VP8Decoder* const dec, VP8Io* const io) {687int ok = 0;688if (dec == NULL) {689return 0;690}691if (io == NULL) {692return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,693"NULL VP8Io parameter in VP8Decode().");694}695696if (!dec->ready) {697if (!VP8GetHeaders(dec, io)) {698return 0;699}700}701assert(dec->ready);702703// Finish setting up the decoding parameter. Will call io->setup().704ok = (VP8EnterCritical(dec, io) == VP8_STATUS_OK);705if (ok) { // good to go.706// Will allocate memory and prepare everything.707if (ok) ok = VP8InitFrame(dec, io);708709// Main decoding loop710if (ok) ok = ParseFrame(dec, io);711712// Exit.713ok &= VP8ExitCritical(dec, io);714}715716if (!ok) {717VP8Clear(dec);718return 0;719}720721dec->ready = 0;722return ok;723}724725void VP8Clear(VP8Decoder* const dec) {726if (dec == NULL) {727return;728}729WebPGetWorkerInterface()->End(&dec->worker);730WebPDeallocateAlphaMemory(dec);731WebPSafeFree(dec->mem);732dec->mem = NULL;733dec->mem_size = 0;734memset(&dec->br, 0, sizeof(dec->br));735dec->ready = 0;736}737738//------------------------------------------------------------------------------739740741