1*b2055c35SXin Li // Copyright 2010 Google Inc. All Rights Reserved.
2*b2055c35SXin Li //
3*b2055c35SXin Li // Use of this source code is governed by a BSD-style license
4*b2055c35SXin Li // that can be found in the COPYING file in the root of the source
5*b2055c35SXin Li // tree. An additional intellectual property rights grant can be found
6*b2055c35SXin Li // in the file PATENTS. All contributing project authors may
7*b2055c35SXin Li // be found in the AUTHORS file in the root of the source tree.
8*b2055c35SXin Li // -----------------------------------------------------------------------------
9*b2055c35SXin Li //
10*b2055c35SXin Li // main entry for the decoder
11*b2055c35SXin Li //
12*b2055c35SXin Li // Author: Skal ([email protected])
13*b2055c35SXin Li
14*b2055c35SXin Li #include <stdlib.h>
15*b2055c35SXin Li
16*b2055c35SXin Li #include "src/dec/alphai_dec.h"
17*b2055c35SXin Li #include "src/dec/vp8i_dec.h"
18*b2055c35SXin Li #include "src/dec/vp8li_dec.h"
19*b2055c35SXin Li #include "src/dec/webpi_dec.h"
20*b2055c35SXin Li #include "src/utils/bit_reader_inl_utils.h"
21*b2055c35SXin Li #include "src/utils/utils.h"
22*b2055c35SXin Li
23*b2055c35SXin Li //------------------------------------------------------------------------------
24*b2055c35SXin Li
WebPGetDecoderVersion(void)25*b2055c35SXin Li int WebPGetDecoderVersion(void) {
26*b2055c35SXin Li return (DEC_MAJ_VERSION << 16) | (DEC_MIN_VERSION << 8) | DEC_REV_VERSION;
27*b2055c35SXin Li }
28*b2055c35SXin Li
29*b2055c35SXin Li //------------------------------------------------------------------------------
30*b2055c35SXin Li // Signature and pointer-to-function for GetCoeffs() variants below.
31*b2055c35SXin Li
32*b2055c35SXin Li typedef int (*GetCoeffsFunc)(VP8BitReader* const br,
33*b2055c35SXin Li const VP8BandProbas* const prob[],
34*b2055c35SXin Li int ctx, const quant_t dq, int n, int16_t* out);
35*b2055c35SXin Li static volatile GetCoeffsFunc GetCoeffs = NULL;
36*b2055c35SXin Li
37*b2055c35SXin Li static void InitGetCoeffs(void);
38*b2055c35SXin Li
39*b2055c35SXin Li //------------------------------------------------------------------------------
40*b2055c35SXin Li // VP8Decoder
41*b2055c35SXin Li
SetOk(VP8Decoder * const dec)42*b2055c35SXin Li static void SetOk(VP8Decoder* const dec) {
43*b2055c35SXin Li dec->status_ = VP8_STATUS_OK;
44*b2055c35SXin Li dec->error_msg_ = "OK";
45*b2055c35SXin Li }
46*b2055c35SXin Li
VP8InitIoInternal(VP8Io * const io,int version)47*b2055c35SXin Li int VP8InitIoInternal(VP8Io* const io, int version) {
48*b2055c35SXin Li if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
49*b2055c35SXin Li return 0; // mismatch error
50*b2055c35SXin Li }
51*b2055c35SXin Li if (io != NULL) {
52*b2055c35SXin Li memset(io, 0, sizeof(*io));
53*b2055c35SXin Li }
54*b2055c35SXin Li return 1;
55*b2055c35SXin Li }
56*b2055c35SXin Li
VP8New(void)57*b2055c35SXin Li VP8Decoder* VP8New(void) {
58*b2055c35SXin Li VP8Decoder* const dec = (VP8Decoder*)WebPSafeCalloc(1ULL, sizeof(*dec));
59*b2055c35SXin Li if (dec != NULL) {
60*b2055c35SXin Li SetOk(dec);
61*b2055c35SXin Li WebPGetWorkerInterface()->Init(&dec->worker_);
62*b2055c35SXin Li dec->ready_ = 0;
63*b2055c35SXin Li dec->num_parts_minus_one_ = 0;
64*b2055c35SXin Li InitGetCoeffs();
65*b2055c35SXin Li }
66*b2055c35SXin Li return dec;
67*b2055c35SXin Li }
68*b2055c35SXin Li
VP8Status(VP8Decoder * const dec)69*b2055c35SXin Li VP8StatusCode VP8Status(VP8Decoder* const dec) {
70*b2055c35SXin Li if (!dec) return VP8_STATUS_INVALID_PARAM;
71*b2055c35SXin Li return dec->status_;
72*b2055c35SXin Li }
73*b2055c35SXin Li
VP8StatusMessage(VP8Decoder * const dec)74*b2055c35SXin Li const char* VP8StatusMessage(VP8Decoder* const dec) {
75*b2055c35SXin Li if (dec == NULL) return "no object";
76*b2055c35SXin Li if (!dec->error_msg_) return "OK";
77*b2055c35SXin Li return dec->error_msg_;
78*b2055c35SXin Li }
79*b2055c35SXin Li
VP8Delete(VP8Decoder * const dec)80*b2055c35SXin Li void VP8Delete(VP8Decoder* const dec) {
81*b2055c35SXin Li if (dec != NULL) {
82*b2055c35SXin Li VP8Clear(dec);
83*b2055c35SXin Li WebPSafeFree(dec);
84*b2055c35SXin Li }
85*b2055c35SXin Li }
86*b2055c35SXin Li
VP8SetError(VP8Decoder * const dec,VP8StatusCode error,const char * const msg)87*b2055c35SXin Li int VP8SetError(VP8Decoder* const dec,
88*b2055c35SXin Li VP8StatusCode error, const char* const msg) {
89*b2055c35SXin Li // VP8_STATUS_SUSPENDED is only meaningful in incremental decoding.
90*b2055c35SXin Li assert(dec->incremental_ || error != VP8_STATUS_SUSPENDED);
91*b2055c35SXin Li // The oldest error reported takes precedence over the new one.
92*b2055c35SXin Li if (dec->status_ == VP8_STATUS_OK) {
93*b2055c35SXin Li dec->status_ = error;
94*b2055c35SXin Li dec->error_msg_ = msg;
95*b2055c35SXin Li dec->ready_ = 0;
96*b2055c35SXin Li }
97*b2055c35SXin Li return 0;
98*b2055c35SXin Li }
99*b2055c35SXin Li
100*b2055c35SXin Li //------------------------------------------------------------------------------
101*b2055c35SXin Li
VP8CheckSignature(const uint8_t * const data,size_t data_size)102*b2055c35SXin Li int VP8CheckSignature(const uint8_t* const data, size_t data_size) {
103*b2055c35SXin Li return (data_size >= 3 &&
104*b2055c35SXin Li data[0] == 0x9d && data[1] == 0x01 && data[2] == 0x2a);
105*b2055c35SXin Li }
106*b2055c35SXin Li
VP8GetInfo(const uint8_t * data,size_t data_size,size_t chunk_size,int * const width,int * const height)107*b2055c35SXin Li int VP8GetInfo(const uint8_t* data, size_t data_size, size_t chunk_size,
108*b2055c35SXin Li int* const width, int* const height) {
109*b2055c35SXin Li if (data == NULL || data_size < VP8_FRAME_HEADER_SIZE) {
110*b2055c35SXin Li return 0; // not enough data
111*b2055c35SXin Li }
112*b2055c35SXin Li // check signature
113*b2055c35SXin Li if (!VP8CheckSignature(data + 3, data_size - 3)) {
114*b2055c35SXin Li return 0; // Wrong signature.
115*b2055c35SXin Li } else {
116*b2055c35SXin Li const uint32_t bits = data[0] | (data[1] << 8) | (data[2] << 16);
117*b2055c35SXin Li const int key_frame = !(bits & 1);
118*b2055c35SXin Li const int w = ((data[7] << 8) | data[6]) & 0x3fff;
119*b2055c35SXin Li const int h = ((data[9] << 8) | data[8]) & 0x3fff;
120*b2055c35SXin Li
121*b2055c35SXin Li if (!key_frame) { // Not a keyframe.
122*b2055c35SXin Li return 0;
123*b2055c35SXin Li }
124*b2055c35SXin Li
125*b2055c35SXin Li if (((bits >> 1) & 7) > 3) {
126*b2055c35SXin Li return 0; // unknown profile
127*b2055c35SXin Li }
128*b2055c35SXin Li if (!((bits >> 4) & 1)) {
129*b2055c35SXin Li return 0; // first frame is invisible!
130*b2055c35SXin Li }
131*b2055c35SXin Li if (((bits >> 5)) >= chunk_size) { // partition_length
132*b2055c35SXin Li return 0; // inconsistent size information.
133*b2055c35SXin Li }
134*b2055c35SXin Li if (w == 0 || h == 0) {
135*b2055c35SXin Li return 0; // We don't support both width and height to be zero.
136*b2055c35SXin Li }
137*b2055c35SXin Li
138*b2055c35SXin Li if (width) {
139*b2055c35SXin Li *width = w;
140*b2055c35SXin Li }
141*b2055c35SXin Li if (height) {
142*b2055c35SXin Li *height = h;
143*b2055c35SXin Li }
144*b2055c35SXin Li
145*b2055c35SXin Li return 1;
146*b2055c35SXin Li }
147*b2055c35SXin Li }
148*b2055c35SXin Li
149*b2055c35SXin Li //------------------------------------------------------------------------------
150*b2055c35SXin Li // Header parsing
151*b2055c35SXin Li
ResetSegmentHeader(VP8SegmentHeader * const hdr)152*b2055c35SXin Li static void ResetSegmentHeader(VP8SegmentHeader* const hdr) {
153*b2055c35SXin Li assert(hdr != NULL);
154*b2055c35SXin Li hdr->use_segment_ = 0;
155*b2055c35SXin Li hdr->update_map_ = 0;
156*b2055c35SXin Li hdr->absolute_delta_ = 1;
157*b2055c35SXin Li memset(hdr->quantizer_, 0, sizeof(hdr->quantizer_));
158*b2055c35SXin Li memset(hdr->filter_strength_, 0, sizeof(hdr->filter_strength_));
159*b2055c35SXin Li }
160*b2055c35SXin Li
161*b2055c35SXin Li // Paragraph 9.3
ParseSegmentHeader(VP8BitReader * br,VP8SegmentHeader * hdr,VP8Proba * proba)162*b2055c35SXin Li static int ParseSegmentHeader(VP8BitReader* br,
163*b2055c35SXin Li VP8SegmentHeader* hdr, VP8Proba* proba) {
164*b2055c35SXin Li assert(br != NULL);
165*b2055c35SXin Li assert(hdr != NULL);
166*b2055c35SXin Li hdr->use_segment_ = VP8Get(br, "global-header");
167*b2055c35SXin Li if (hdr->use_segment_) {
168*b2055c35SXin Li hdr->update_map_ = VP8Get(br, "global-header");
169*b2055c35SXin Li if (VP8Get(br, "global-header")) { // update data
170*b2055c35SXin Li int s;
171*b2055c35SXin Li hdr->absolute_delta_ = VP8Get(br, "global-header");
172*b2055c35SXin Li for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
173*b2055c35SXin Li hdr->quantizer_[s] = VP8Get(br, "global-header") ?
174*b2055c35SXin Li VP8GetSignedValue(br, 7, "global-header") : 0;
175*b2055c35SXin Li }
176*b2055c35SXin Li for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
177*b2055c35SXin Li hdr->filter_strength_[s] = VP8Get(br, "global-header") ?
178*b2055c35SXin Li VP8GetSignedValue(br, 6, "global-header") : 0;
179*b2055c35SXin Li }
180*b2055c35SXin Li }
181*b2055c35SXin Li if (hdr->update_map_) {
182*b2055c35SXin Li int s;
183*b2055c35SXin Li for (s = 0; s < MB_FEATURE_TREE_PROBS; ++s) {
184*b2055c35SXin Li proba->segments_[s] = VP8Get(br, "global-header") ?
185*b2055c35SXin Li VP8GetValue(br, 8, "global-header") : 255u;
186*b2055c35SXin Li }
187*b2055c35SXin Li }
188*b2055c35SXin Li } else {
189*b2055c35SXin Li hdr->update_map_ = 0;
190*b2055c35SXin Li }
191*b2055c35SXin Li return !br->eof_;
192*b2055c35SXin Li }
193*b2055c35SXin Li
194*b2055c35SXin Li // Paragraph 9.5
195*b2055c35SXin Li // If we don't have all the necessary data in 'buf', this function returns
196*b2055c35SXin Li // VP8_STATUS_SUSPENDED in incremental decoding, VP8_STATUS_NOT_ENOUGH_DATA
197*b2055c35SXin Li // otherwise.
198*b2055c35SXin Li // In incremental decoding, this case is not necessarily an error. Still, no
199*b2055c35SXin Li // bitreader is ever initialized to make it possible to read unavailable memory.
200*b2055c35SXin Li // If we don't even have the partitions' sizes, then VP8_STATUS_NOT_ENOUGH_DATA
201*b2055c35SXin Li // is returned, and this is an unrecoverable error.
202*b2055c35SXin Li // If the partitions were positioned ok, VP8_STATUS_OK is returned.
ParsePartitions(VP8Decoder * const dec,const uint8_t * buf,size_t size)203*b2055c35SXin Li static VP8StatusCode ParsePartitions(VP8Decoder* const dec,
204*b2055c35SXin Li const uint8_t* buf, size_t size) {
205*b2055c35SXin Li VP8BitReader* const br = &dec->br_;
206*b2055c35SXin Li const uint8_t* sz = buf;
207*b2055c35SXin Li const uint8_t* buf_end = buf + size;
208*b2055c35SXin Li const uint8_t* part_start;
209*b2055c35SXin Li size_t size_left = size;
210*b2055c35SXin Li size_t last_part;
211*b2055c35SXin Li size_t p;
212*b2055c35SXin Li
213*b2055c35SXin Li dec->num_parts_minus_one_ = (1 << VP8GetValue(br, 2, "global-header")) - 1;
214*b2055c35SXin Li last_part = dec->num_parts_minus_one_;
215*b2055c35SXin Li if (size < 3 * last_part) {
216*b2055c35SXin Li // we can't even read the sizes with sz[]! That's a failure.
217*b2055c35SXin Li return VP8_STATUS_NOT_ENOUGH_DATA;
218*b2055c35SXin Li }
219*b2055c35SXin Li part_start = buf + last_part * 3;
220*b2055c35SXin Li size_left -= last_part * 3;
221*b2055c35SXin Li for (p = 0; p < last_part; ++p) {
222*b2055c35SXin Li size_t psize = sz[0] | (sz[1] << 8) | (sz[2] << 16);
223*b2055c35SXin Li if (psize > size_left) psize = size_left;
224*b2055c35SXin Li VP8InitBitReader(dec->parts_ + p, part_start, psize);
225*b2055c35SXin Li part_start += psize;
226*b2055c35SXin Li size_left -= psize;
227*b2055c35SXin Li sz += 3;
228*b2055c35SXin Li }
229*b2055c35SXin Li VP8InitBitReader(dec->parts_ + last_part, part_start, size_left);
230*b2055c35SXin Li if (part_start < buf_end) return VP8_STATUS_OK;
231*b2055c35SXin Li return dec->incremental_
232*b2055c35SXin Li ? VP8_STATUS_SUSPENDED // Init is ok, but there's not enough data
233*b2055c35SXin Li : VP8_STATUS_NOT_ENOUGH_DATA;
234*b2055c35SXin Li }
235*b2055c35SXin Li
236*b2055c35SXin Li // Paragraph 9.4
ParseFilterHeader(VP8BitReader * br,VP8Decoder * const dec)237*b2055c35SXin Li static int ParseFilterHeader(VP8BitReader* br, VP8Decoder* const dec) {
238*b2055c35SXin Li VP8FilterHeader* const hdr = &dec->filter_hdr_;
239*b2055c35SXin Li hdr->simple_ = VP8Get(br, "global-header");
240*b2055c35SXin Li hdr->level_ = VP8GetValue(br, 6, "global-header");
241*b2055c35SXin Li hdr->sharpness_ = VP8GetValue(br, 3, "global-header");
242*b2055c35SXin Li hdr->use_lf_delta_ = VP8Get(br, "global-header");
243*b2055c35SXin Li if (hdr->use_lf_delta_) {
244*b2055c35SXin Li if (VP8Get(br, "global-header")) { // update lf-delta?
245*b2055c35SXin Li int i;
246*b2055c35SXin Li for (i = 0; i < NUM_REF_LF_DELTAS; ++i) {
247*b2055c35SXin Li if (VP8Get(br, "global-header")) {
248*b2055c35SXin Li hdr->ref_lf_delta_[i] = VP8GetSignedValue(br, 6, "global-header");
249*b2055c35SXin Li }
250*b2055c35SXin Li }
251*b2055c35SXin Li for (i = 0; i < NUM_MODE_LF_DELTAS; ++i) {
252*b2055c35SXin Li if (VP8Get(br, "global-header")) {
253*b2055c35SXin Li hdr->mode_lf_delta_[i] = VP8GetSignedValue(br, 6, "global-header");
254*b2055c35SXin Li }
255*b2055c35SXin Li }
256*b2055c35SXin Li }
257*b2055c35SXin Li }
258*b2055c35SXin Li dec->filter_type_ = (hdr->level_ == 0) ? 0 : hdr->simple_ ? 1 : 2;
259*b2055c35SXin Li return !br->eof_;
260*b2055c35SXin Li }
261*b2055c35SXin Li
262*b2055c35SXin Li // Topmost call
VP8GetHeaders(VP8Decoder * const dec,VP8Io * const io)263*b2055c35SXin Li int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
264*b2055c35SXin Li const uint8_t* buf;
265*b2055c35SXin Li size_t buf_size;
266*b2055c35SXin Li VP8FrameHeader* frm_hdr;
267*b2055c35SXin Li VP8PictureHeader* pic_hdr;
268*b2055c35SXin Li VP8BitReader* br;
269*b2055c35SXin Li VP8StatusCode status;
270*b2055c35SXin Li
271*b2055c35SXin Li if (dec == NULL) {
272*b2055c35SXin Li return 0;
273*b2055c35SXin Li }
274*b2055c35SXin Li SetOk(dec);
275*b2055c35SXin Li if (io == NULL) {
276*b2055c35SXin Li return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,
277*b2055c35SXin Li "null VP8Io passed to VP8GetHeaders()");
278*b2055c35SXin Li }
279*b2055c35SXin Li buf = io->data;
280*b2055c35SXin Li buf_size = io->data_size;
281*b2055c35SXin Li if (buf_size < 4) {
282*b2055c35SXin Li return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
283*b2055c35SXin Li "Truncated header.");
284*b2055c35SXin Li }
285*b2055c35SXin Li
286*b2055c35SXin Li // Paragraph 9.1
287*b2055c35SXin Li {
288*b2055c35SXin Li const uint32_t bits = buf[0] | (buf[1] << 8) | (buf[2] << 16);
289*b2055c35SXin Li frm_hdr = &dec->frm_hdr_;
290*b2055c35SXin Li frm_hdr->key_frame_ = !(bits & 1);
291*b2055c35SXin Li frm_hdr->profile_ = (bits >> 1) & 7;
292*b2055c35SXin Li frm_hdr->show_ = (bits >> 4) & 1;
293*b2055c35SXin Li frm_hdr->partition_length_ = (bits >> 5);
294*b2055c35SXin Li if (frm_hdr->profile_ > 3) {
295*b2055c35SXin Li return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
296*b2055c35SXin Li "Incorrect keyframe parameters.");
297*b2055c35SXin Li }
298*b2055c35SXin Li if (!frm_hdr->show_) {
299*b2055c35SXin Li return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,
300*b2055c35SXin Li "Frame not displayable.");
301*b2055c35SXin Li }
302*b2055c35SXin Li buf += 3;
303*b2055c35SXin Li buf_size -= 3;
304*b2055c35SXin Li }
305*b2055c35SXin Li
306*b2055c35SXin Li pic_hdr = &dec->pic_hdr_;
307*b2055c35SXin Li if (frm_hdr->key_frame_) {
308*b2055c35SXin Li // Paragraph 9.2
309*b2055c35SXin Li if (buf_size < 7) {
310*b2055c35SXin Li return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
311*b2055c35SXin Li "cannot parse picture header");
312*b2055c35SXin Li }
313*b2055c35SXin Li if (!VP8CheckSignature(buf, buf_size)) {
314*b2055c35SXin Li return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
315*b2055c35SXin Li "Bad code word");
316*b2055c35SXin Li }
317*b2055c35SXin Li pic_hdr->width_ = ((buf[4] << 8) | buf[3]) & 0x3fff;
318*b2055c35SXin Li pic_hdr->xscale_ = buf[4] >> 6; // ratio: 1, 5/4 5/3 or 2
319*b2055c35SXin Li pic_hdr->height_ = ((buf[6] << 8) | buf[5]) & 0x3fff;
320*b2055c35SXin Li pic_hdr->yscale_ = buf[6] >> 6;
321*b2055c35SXin Li buf += 7;
322*b2055c35SXin Li buf_size -= 7;
323*b2055c35SXin Li
324*b2055c35SXin Li dec->mb_w_ = (pic_hdr->width_ + 15) >> 4;
325*b2055c35SXin Li dec->mb_h_ = (pic_hdr->height_ + 15) >> 4;
326*b2055c35SXin Li
327*b2055c35SXin Li // Setup default output area (can be later modified during io->setup())
328*b2055c35SXin Li io->width = pic_hdr->width_;
329*b2055c35SXin Li io->height = pic_hdr->height_;
330*b2055c35SXin Li // IMPORTANT! use some sane dimensions in crop_* and scaled_* fields.
331*b2055c35SXin Li // So they can be used interchangeably without always testing for
332*b2055c35SXin Li // 'use_cropping'.
333*b2055c35SXin Li io->use_cropping = 0;
334*b2055c35SXin Li io->crop_top = 0;
335*b2055c35SXin Li io->crop_left = 0;
336*b2055c35SXin Li io->crop_right = io->width;
337*b2055c35SXin Li io->crop_bottom = io->height;
338*b2055c35SXin Li io->use_scaling = 0;
339*b2055c35SXin Li io->scaled_width = io->width;
340*b2055c35SXin Li io->scaled_height = io->height;
341*b2055c35SXin Li
342*b2055c35SXin Li io->mb_w = io->width; // for soundness
343*b2055c35SXin Li io->mb_h = io->height; // ditto
344*b2055c35SXin Li
345*b2055c35SXin Li VP8ResetProba(&dec->proba_);
346*b2055c35SXin Li ResetSegmentHeader(&dec->segment_hdr_);
347*b2055c35SXin Li }
348*b2055c35SXin Li
349*b2055c35SXin Li // Check if we have all the partition #0 available, and initialize dec->br_
350*b2055c35SXin Li // to read this partition (and this partition only).
351*b2055c35SXin Li if (frm_hdr->partition_length_ > buf_size) {
352*b2055c35SXin Li return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
353*b2055c35SXin Li "bad partition length");
354*b2055c35SXin Li }
355*b2055c35SXin Li
356*b2055c35SXin Li br = &dec->br_;
357*b2055c35SXin Li VP8InitBitReader(br, buf, frm_hdr->partition_length_);
358*b2055c35SXin Li buf += frm_hdr->partition_length_;
359*b2055c35SXin Li buf_size -= frm_hdr->partition_length_;
360*b2055c35SXin Li
361*b2055c35SXin Li if (frm_hdr->key_frame_) {
362*b2055c35SXin Li pic_hdr->colorspace_ = VP8Get(br, "global-header");
363*b2055c35SXin Li pic_hdr->clamp_type_ = VP8Get(br, "global-header");
364*b2055c35SXin Li }
365*b2055c35SXin Li if (!ParseSegmentHeader(br, &dec->segment_hdr_, &dec->proba_)) {
366*b2055c35SXin Li return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
367*b2055c35SXin Li "cannot parse segment header");
368*b2055c35SXin Li }
369*b2055c35SXin Li // Filter specs
370*b2055c35SXin Li if (!ParseFilterHeader(br, dec)) {
371*b2055c35SXin Li return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
372*b2055c35SXin Li "cannot parse filter header");
373*b2055c35SXin Li }
374*b2055c35SXin Li status = ParsePartitions(dec, buf, buf_size);
375*b2055c35SXin Li if (status != VP8_STATUS_OK) {
376*b2055c35SXin Li return VP8SetError(dec, status, "cannot parse partitions");
377*b2055c35SXin Li }
378*b2055c35SXin Li
379*b2055c35SXin Li // quantizer change
380*b2055c35SXin Li VP8ParseQuant(dec);
381*b2055c35SXin Li
382*b2055c35SXin Li // Frame buffer marking
383*b2055c35SXin Li if (!frm_hdr->key_frame_) {
384*b2055c35SXin Li return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,
385*b2055c35SXin Li "Not a key frame.");
386*b2055c35SXin Li }
387*b2055c35SXin Li
388*b2055c35SXin Li VP8Get(br, "global-header"); // ignore the value of update_proba_
389*b2055c35SXin Li
390*b2055c35SXin Li VP8ParseProba(br, dec);
391*b2055c35SXin Li
392*b2055c35SXin Li // sanitized state
393*b2055c35SXin Li dec->ready_ = 1;
394*b2055c35SXin Li return 1;
395*b2055c35SXin Li }
396*b2055c35SXin Li
397*b2055c35SXin Li //------------------------------------------------------------------------------
398*b2055c35SXin Li // Residual decoding (Paragraph 13.2 / 13.3)
399*b2055c35SXin Li
400*b2055c35SXin Li static const uint8_t kCat3[] = { 173, 148, 140, 0 };
401*b2055c35SXin Li static const uint8_t kCat4[] = { 176, 155, 140, 135, 0 };
402*b2055c35SXin Li static const uint8_t kCat5[] = { 180, 157, 141, 134, 130, 0 };
403*b2055c35SXin Li static const uint8_t kCat6[] =
404*b2055c35SXin Li { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0 };
405*b2055c35SXin Li static const uint8_t* const kCat3456[] = { kCat3, kCat4, kCat5, kCat6 };
406*b2055c35SXin Li static const uint8_t kZigzag[16] = {
407*b2055c35SXin Li 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15
408*b2055c35SXin Li };
409*b2055c35SXin Li
410*b2055c35SXin Li // See section 13-2: https://datatracker.ietf.org/doc/html/rfc6386#section-13.2
GetLargeValue(VP8BitReader * const br,const uint8_t * const p)411*b2055c35SXin Li static int GetLargeValue(VP8BitReader* const br, const uint8_t* const p) {
412*b2055c35SXin Li int v;
413*b2055c35SXin Li if (!VP8GetBit(br, p[3], "coeffs")) {
414*b2055c35SXin Li if (!VP8GetBit(br, p[4], "coeffs")) {
415*b2055c35SXin Li v = 2;
416*b2055c35SXin Li } else {
417*b2055c35SXin Li v = 3 + VP8GetBit(br, p[5], "coeffs");
418*b2055c35SXin Li }
419*b2055c35SXin Li } else {
420*b2055c35SXin Li if (!VP8GetBit(br, p[6], "coeffs")) {
421*b2055c35SXin Li if (!VP8GetBit(br, p[7], "coeffs")) {
422*b2055c35SXin Li v = 5 + VP8GetBit(br, 159, "coeffs");
423*b2055c35SXin Li } else {
424*b2055c35SXin Li v = 7 + 2 * VP8GetBit(br, 165, "coeffs");
425*b2055c35SXin Li v += VP8GetBit(br, 145, "coeffs");
426*b2055c35SXin Li }
427*b2055c35SXin Li } else {
428*b2055c35SXin Li const uint8_t* tab;
429*b2055c35SXin Li const int bit1 = VP8GetBit(br, p[8], "coeffs");
430*b2055c35SXin Li const int bit0 = VP8GetBit(br, p[9 + bit1], "coeffs");
431*b2055c35SXin Li const int cat = 2 * bit1 + bit0;
432*b2055c35SXin Li v = 0;
433*b2055c35SXin Li for (tab = kCat3456[cat]; *tab; ++tab) {
434*b2055c35SXin Li v += v + VP8GetBit(br, *tab, "coeffs");
435*b2055c35SXin Li }
436*b2055c35SXin Li v += 3 + (8 << cat);
437*b2055c35SXin Li }
438*b2055c35SXin Li }
439*b2055c35SXin Li return v;
440*b2055c35SXin Li }
441*b2055c35SXin Li
442*b2055c35SXin Li // Returns the position of the last non-zero coeff plus one
GetCoeffsFast(VP8BitReader * const br,const VP8BandProbas * const prob[],int ctx,const quant_t dq,int n,int16_t * out)443*b2055c35SXin Li static int GetCoeffsFast(VP8BitReader* const br,
444*b2055c35SXin Li const VP8BandProbas* const prob[],
445*b2055c35SXin Li int ctx, const quant_t dq, int n, int16_t* out) {
446*b2055c35SXin Li const uint8_t* p = prob[n]->probas_[ctx];
447*b2055c35SXin Li for (; n < 16; ++n) {
448*b2055c35SXin Li if (!VP8GetBit(br, p[0], "coeffs")) {
449*b2055c35SXin Li return n; // previous coeff was last non-zero coeff
450*b2055c35SXin Li }
451*b2055c35SXin Li while (!VP8GetBit(br, p[1], "coeffs")) { // sequence of zero coeffs
452*b2055c35SXin Li p = prob[++n]->probas_[0];
453*b2055c35SXin Li if (n == 16) return 16;
454*b2055c35SXin Li }
455*b2055c35SXin Li { // non zero coeff
456*b2055c35SXin Li const VP8ProbaArray* const p_ctx = &prob[n + 1]->probas_[0];
457*b2055c35SXin Li int v;
458*b2055c35SXin Li if (!VP8GetBit(br, p[2], "coeffs")) {
459*b2055c35SXin Li v = 1;
460*b2055c35SXin Li p = p_ctx[1];
461*b2055c35SXin Li } else {
462*b2055c35SXin Li v = GetLargeValue(br, p);
463*b2055c35SXin Li p = p_ctx[2];
464*b2055c35SXin Li }
465*b2055c35SXin Li out[kZigzag[n]] = VP8GetSigned(br, v, "coeffs") * dq[n > 0];
466*b2055c35SXin Li }
467*b2055c35SXin Li }
468*b2055c35SXin Li return 16;
469*b2055c35SXin Li }
470*b2055c35SXin Li
471*b2055c35SXin Li // This version of GetCoeffs() uses VP8GetBitAlt() which is an alternate version
472*b2055c35SXin Li // of VP8GetBitAlt() targeting specific platforms.
GetCoeffsAlt(VP8BitReader * const br,const VP8BandProbas * const prob[],int ctx,const quant_t dq,int n,int16_t * out)473*b2055c35SXin Li static int GetCoeffsAlt(VP8BitReader* const br,
474*b2055c35SXin Li const VP8BandProbas* const prob[],
475*b2055c35SXin Li int ctx, const quant_t dq, int n, int16_t* out) {
476*b2055c35SXin Li const uint8_t* p = prob[n]->probas_[ctx];
477*b2055c35SXin Li for (; n < 16; ++n) {
478*b2055c35SXin Li if (!VP8GetBitAlt(br, p[0], "coeffs")) {
479*b2055c35SXin Li return n; // previous coeff was last non-zero coeff
480*b2055c35SXin Li }
481*b2055c35SXin Li while (!VP8GetBitAlt(br, p[1], "coeffs")) { // sequence of zero coeffs
482*b2055c35SXin Li p = prob[++n]->probas_[0];
483*b2055c35SXin Li if (n == 16) return 16;
484*b2055c35SXin Li }
485*b2055c35SXin Li { // non zero coeff
486*b2055c35SXin Li const VP8ProbaArray* const p_ctx = &prob[n + 1]->probas_[0];
487*b2055c35SXin Li int v;
488*b2055c35SXin Li if (!VP8GetBitAlt(br, p[2], "coeffs")) {
489*b2055c35SXin Li v = 1;
490*b2055c35SXin Li p = p_ctx[1];
491*b2055c35SXin Li } else {
492*b2055c35SXin Li v = GetLargeValue(br, p);
493*b2055c35SXin Li p = p_ctx[2];
494*b2055c35SXin Li }
495*b2055c35SXin Li out[kZigzag[n]] = VP8GetSigned(br, v, "coeffs") * dq[n > 0];
496*b2055c35SXin Li }
497*b2055c35SXin Li }
498*b2055c35SXin Li return 16;
499*b2055c35SXin Li }
500*b2055c35SXin Li
501*b2055c35SXin Li extern VP8CPUInfo VP8GetCPUInfo;
502*b2055c35SXin Li
WEBP_DSP_INIT_FUNC(InitGetCoeffs)503*b2055c35SXin Li WEBP_DSP_INIT_FUNC(InitGetCoeffs) {
504*b2055c35SXin Li if (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kSlowSSSE3)) {
505*b2055c35SXin Li GetCoeffs = GetCoeffsAlt;
506*b2055c35SXin Li } else {
507*b2055c35SXin Li GetCoeffs = GetCoeffsFast;
508*b2055c35SXin Li }
509*b2055c35SXin Li }
510*b2055c35SXin Li
NzCodeBits(uint32_t nz_coeffs,int nz,int dc_nz)511*b2055c35SXin Li static WEBP_INLINE uint32_t NzCodeBits(uint32_t nz_coeffs, int nz, int dc_nz) {
512*b2055c35SXin Li nz_coeffs <<= 2;
513*b2055c35SXin Li nz_coeffs |= (nz > 3) ? 3 : (nz > 1) ? 2 : dc_nz;
514*b2055c35SXin Li return nz_coeffs;
515*b2055c35SXin Li }
516*b2055c35SXin Li
ParseResiduals(VP8Decoder * const dec,VP8MB * const mb,VP8BitReader * const token_br)517*b2055c35SXin Li static int ParseResiduals(VP8Decoder* const dec,
518*b2055c35SXin Li VP8MB* const mb, VP8BitReader* const token_br) {
519*b2055c35SXin Li const VP8BandProbas* (* const bands)[16 + 1] = dec->proba_.bands_ptr_;
520*b2055c35SXin Li const VP8BandProbas* const * ac_proba;
521*b2055c35SXin Li VP8MBData* const block = dec->mb_data_ + dec->mb_x_;
522*b2055c35SXin Li const VP8QuantMatrix* const q = &dec->dqm_[block->segment_];
523*b2055c35SXin Li int16_t* dst = block->coeffs_;
524*b2055c35SXin Li VP8MB* const left_mb = dec->mb_info_ - 1;
525*b2055c35SXin Li uint8_t tnz, lnz;
526*b2055c35SXin Li uint32_t non_zero_y = 0;
527*b2055c35SXin Li uint32_t non_zero_uv = 0;
528*b2055c35SXin Li int x, y, ch;
529*b2055c35SXin Li uint32_t out_t_nz, out_l_nz;
530*b2055c35SXin Li int first;
531*b2055c35SXin Li
532*b2055c35SXin Li memset(dst, 0, 384 * sizeof(*dst));
533*b2055c35SXin Li if (!block->is_i4x4_) { // parse DC
534*b2055c35SXin Li int16_t dc[16] = { 0 };
535*b2055c35SXin Li const int ctx = mb->nz_dc_ + left_mb->nz_dc_;
536*b2055c35SXin Li const int nz = GetCoeffs(token_br, bands[1], ctx, q->y2_mat_, 0, dc);
537*b2055c35SXin Li mb->nz_dc_ = left_mb->nz_dc_ = (nz > 0);
538*b2055c35SXin Li if (nz > 1) { // more than just the DC -> perform the full transform
539*b2055c35SXin Li VP8TransformWHT(dc, dst);
540*b2055c35SXin Li } else { // only DC is non-zero -> inlined simplified transform
541*b2055c35SXin Li int i;
542*b2055c35SXin Li const int dc0 = (dc[0] + 3) >> 3;
543*b2055c35SXin Li for (i = 0; i < 16 * 16; i += 16) dst[i] = dc0;
544*b2055c35SXin Li }
545*b2055c35SXin Li first = 1;
546*b2055c35SXin Li ac_proba = bands[0];
547*b2055c35SXin Li } else {
548*b2055c35SXin Li first = 0;
549*b2055c35SXin Li ac_proba = bands[3];
550*b2055c35SXin Li }
551*b2055c35SXin Li
552*b2055c35SXin Li tnz = mb->nz_ & 0x0f;
553*b2055c35SXin Li lnz = left_mb->nz_ & 0x0f;
554*b2055c35SXin Li for (y = 0; y < 4; ++y) {
555*b2055c35SXin Li int l = lnz & 1;
556*b2055c35SXin Li uint32_t nz_coeffs = 0;
557*b2055c35SXin Li for (x = 0; x < 4; ++x) {
558*b2055c35SXin Li const int ctx = l + (tnz & 1);
559*b2055c35SXin Li const int nz = GetCoeffs(token_br, ac_proba, ctx, q->y1_mat_, first, dst);
560*b2055c35SXin Li l = (nz > first);
561*b2055c35SXin Li tnz = (tnz >> 1) | (l << 7);
562*b2055c35SXin Li nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);
563*b2055c35SXin Li dst += 16;
564*b2055c35SXin Li }
565*b2055c35SXin Li tnz >>= 4;
566*b2055c35SXin Li lnz = (lnz >> 1) | (l << 7);
567*b2055c35SXin Li non_zero_y = (non_zero_y << 8) | nz_coeffs;
568*b2055c35SXin Li }
569*b2055c35SXin Li out_t_nz = tnz;
570*b2055c35SXin Li out_l_nz = lnz >> 4;
571*b2055c35SXin Li
572*b2055c35SXin Li for (ch = 0; ch < 4; ch += 2) {
573*b2055c35SXin Li uint32_t nz_coeffs = 0;
574*b2055c35SXin Li tnz = mb->nz_ >> (4 + ch);
575*b2055c35SXin Li lnz = left_mb->nz_ >> (4 + ch);
576*b2055c35SXin Li for (y = 0; y < 2; ++y) {
577*b2055c35SXin Li int l = lnz & 1;
578*b2055c35SXin Li for (x = 0; x < 2; ++x) {
579*b2055c35SXin Li const int ctx = l + (tnz & 1);
580*b2055c35SXin Li const int nz = GetCoeffs(token_br, bands[2], ctx, q->uv_mat_, 0, dst);
581*b2055c35SXin Li l = (nz > 0);
582*b2055c35SXin Li tnz = (tnz >> 1) | (l << 3);
583*b2055c35SXin Li nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);
584*b2055c35SXin Li dst += 16;
585*b2055c35SXin Li }
586*b2055c35SXin Li tnz >>= 2;
587*b2055c35SXin Li lnz = (lnz >> 1) | (l << 5);
588*b2055c35SXin Li }
589*b2055c35SXin Li // Note: we don't really need the per-4x4 details for U/V blocks.
590*b2055c35SXin Li non_zero_uv |= nz_coeffs << (4 * ch);
591*b2055c35SXin Li out_t_nz |= (tnz << 4) << ch;
592*b2055c35SXin Li out_l_nz |= (lnz & 0xf0) << ch;
593*b2055c35SXin Li }
594*b2055c35SXin Li mb->nz_ = out_t_nz;
595*b2055c35SXin Li left_mb->nz_ = out_l_nz;
596*b2055c35SXin Li
597*b2055c35SXin Li block->non_zero_y_ = non_zero_y;
598*b2055c35SXin Li block->non_zero_uv_ = non_zero_uv;
599*b2055c35SXin Li
600*b2055c35SXin Li // We look at the mode-code of each block and check if some blocks have less
601*b2055c35SXin Li // than three non-zero coeffs (code < 2). This is to avoid dithering flat and
602*b2055c35SXin Li // empty blocks.
603*b2055c35SXin Li block->dither_ = (non_zero_uv & 0xaaaa) ? 0 : q->dither_;
604*b2055c35SXin Li
605*b2055c35SXin Li return !(non_zero_y | non_zero_uv); // will be used for further optimization
606*b2055c35SXin Li }
607*b2055c35SXin Li
608*b2055c35SXin Li //------------------------------------------------------------------------------
609*b2055c35SXin Li // Main loop
610*b2055c35SXin Li
VP8DecodeMB(VP8Decoder * const dec,VP8BitReader * const token_br)611*b2055c35SXin Li int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br) {
612*b2055c35SXin Li VP8MB* const left = dec->mb_info_ - 1;
613*b2055c35SXin Li VP8MB* const mb = dec->mb_info_ + dec->mb_x_;
614*b2055c35SXin Li VP8MBData* const block = dec->mb_data_ + dec->mb_x_;
615*b2055c35SXin Li int skip = dec->use_skip_proba_ ? block->skip_ : 0;
616*b2055c35SXin Li
617*b2055c35SXin Li if (!skip) {
618*b2055c35SXin Li skip = ParseResiduals(dec, mb, token_br);
619*b2055c35SXin Li } else {
620*b2055c35SXin Li left->nz_ = mb->nz_ = 0;
621*b2055c35SXin Li if (!block->is_i4x4_) {
622*b2055c35SXin Li left->nz_dc_ = mb->nz_dc_ = 0;
623*b2055c35SXin Li }
624*b2055c35SXin Li block->non_zero_y_ = 0;
625*b2055c35SXin Li block->non_zero_uv_ = 0;
626*b2055c35SXin Li block->dither_ = 0;
627*b2055c35SXin Li }
628*b2055c35SXin Li
629*b2055c35SXin Li if (dec->filter_type_ > 0) { // store filter info
630*b2055c35SXin Li VP8FInfo* const finfo = dec->f_info_ + dec->mb_x_;
631*b2055c35SXin Li *finfo = dec->fstrengths_[block->segment_][block->is_i4x4_];
632*b2055c35SXin Li finfo->f_inner_ |= !skip;
633*b2055c35SXin Li }
634*b2055c35SXin Li
635*b2055c35SXin Li return !token_br->eof_;
636*b2055c35SXin Li }
637*b2055c35SXin Li
VP8InitScanline(VP8Decoder * const dec)638*b2055c35SXin Li void VP8InitScanline(VP8Decoder* const dec) {
639*b2055c35SXin Li VP8MB* const left = dec->mb_info_ - 1;
640*b2055c35SXin Li left->nz_ = 0;
641*b2055c35SXin Li left->nz_dc_ = 0;
642*b2055c35SXin Li memset(dec->intra_l_, B_DC_PRED, sizeof(dec->intra_l_));
643*b2055c35SXin Li dec->mb_x_ = 0;
644*b2055c35SXin Li }
645*b2055c35SXin Li
ParseFrame(VP8Decoder * const dec,VP8Io * io)646*b2055c35SXin Li static int ParseFrame(VP8Decoder* const dec, VP8Io* io) {
647*b2055c35SXin Li for (dec->mb_y_ = 0; dec->mb_y_ < dec->br_mb_y_; ++dec->mb_y_) {
648*b2055c35SXin Li // Parse bitstream for this row.
649*b2055c35SXin Li VP8BitReader* const token_br =
650*b2055c35SXin Li &dec->parts_[dec->mb_y_ & dec->num_parts_minus_one_];
651*b2055c35SXin Li if (!VP8ParseIntraModeRow(&dec->br_, dec)) {
652*b2055c35SXin Li return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
653*b2055c35SXin Li "Premature end-of-partition0 encountered.");
654*b2055c35SXin Li }
655*b2055c35SXin Li for (; dec->mb_x_ < dec->mb_w_; ++dec->mb_x_) {
656*b2055c35SXin Li if (!VP8DecodeMB(dec, token_br)) {
657*b2055c35SXin Li return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
658*b2055c35SXin Li "Premature end-of-file encountered.");
659*b2055c35SXin Li }
660*b2055c35SXin Li }
661*b2055c35SXin Li VP8InitScanline(dec); // Prepare for next scanline
662*b2055c35SXin Li
663*b2055c35SXin Li // Reconstruct, filter and emit the row.
664*b2055c35SXin Li if (!VP8ProcessRow(dec, io)) {
665*b2055c35SXin Li return VP8SetError(dec, VP8_STATUS_USER_ABORT, "Output aborted.");
666*b2055c35SXin Li }
667*b2055c35SXin Li }
668*b2055c35SXin Li if (dec->mt_method_ > 0) {
669*b2055c35SXin Li if (!WebPGetWorkerInterface()->Sync(&dec->worker_)) return 0;
670*b2055c35SXin Li }
671*b2055c35SXin Li
672*b2055c35SXin Li return 1;
673*b2055c35SXin Li }
674*b2055c35SXin Li
675*b2055c35SXin Li // Main entry point
VP8Decode(VP8Decoder * const dec,VP8Io * const io)676*b2055c35SXin Li int VP8Decode(VP8Decoder* const dec, VP8Io* const io) {
677*b2055c35SXin Li int ok = 0;
678*b2055c35SXin Li if (dec == NULL) {
679*b2055c35SXin Li return 0;
680*b2055c35SXin Li }
681*b2055c35SXin Li if (io == NULL) {
682*b2055c35SXin Li return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,
683*b2055c35SXin Li "NULL VP8Io parameter in VP8Decode().");
684*b2055c35SXin Li }
685*b2055c35SXin Li
686*b2055c35SXin Li if (!dec->ready_) {
687*b2055c35SXin Li if (!VP8GetHeaders(dec, io)) {
688*b2055c35SXin Li return 0;
689*b2055c35SXin Li }
690*b2055c35SXin Li }
691*b2055c35SXin Li assert(dec->ready_);
692*b2055c35SXin Li
693*b2055c35SXin Li // Finish setting up the decoding parameter. Will call io->setup().
694*b2055c35SXin Li ok = (VP8EnterCritical(dec, io) == VP8_STATUS_OK);
695*b2055c35SXin Li if (ok) { // good to go.
696*b2055c35SXin Li // Will allocate memory and prepare everything.
697*b2055c35SXin Li if (ok) ok = VP8InitFrame(dec, io);
698*b2055c35SXin Li
699*b2055c35SXin Li // Main decoding loop
700*b2055c35SXin Li if (ok) ok = ParseFrame(dec, io);
701*b2055c35SXin Li
702*b2055c35SXin Li // Exit.
703*b2055c35SXin Li ok &= VP8ExitCritical(dec, io);
704*b2055c35SXin Li }
705*b2055c35SXin Li
706*b2055c35SXin Li if (!ok) {
707*b2055c35SXin Li VP8Clear(dec);
708*b2055c35SXin Li return 0;
709*b2055c35SXin Li }
710*b2055c35SXin Li
711*b2055c35SXin Li dec->ready_ = 0;
712*b2055c35SXin Li return ok;
713*b2055c35SXin Li }
714*b2055c35SXin Li
VP8Clear(VP8Decoder * const dec)715*b2055c35SXin Li void VP8Clear(VP8Decoder* const dec) {
716*b2055c35SXin Li if (dec == NULL) {
717*b2055c35SXin Li return;
718*b2055c35SXin Li }
719*b2055c35SXin Li WebPGetWorkerInterface()->End(&dec->worker_);
720*b2055c35SXin Li WebPDeallocateAlphaMemory(dec);
721*b2055c35SXin Li WebPSafeFree(dec->mem_);
722*b2055c35SXin Li dec->mem_ = NULL;
723*b2055c35SXin Li dec->mem_size_ = 0;
724*b2055c35SXin Li memset(&dec->br_, 0, sizeof(dec->br_));
725*b2055c35SXin Li dec->ready_ = 0;
726*b2055c35SXin Li }
727*b2055c35SXin Li
728*b2055c35SXin Li //------------------------------------------------------------------------------
729