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