/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 | 3.85k | static void SetOk(VP8Decoder* const dec) { |
54 | 3.85k | dec->status = VP8_STATUS_OK; |
55 | 3.85k | dec->error_msg = "OK"; |
56 | 3.85k | } |
57 | | |
58 | 3.59k | int VP8InitIoInternal(VP8Io* const io, int version) { |
59 | 3.59k | if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) { |
60 | 0 | return 0; // mismatch error |
61 | 0 | } |
62 | 3.59k | if (io != NULL) { |
63 | 3.59k | memset(io, 0, sizeof(*io)); |
64 | 3.59k | } |
65 | 3.59k | return 1; |
66 | 3.59k | } |
67 | | |
68 | 1.92k | VP8Decoder* VP8New(void) { |
69 | 1.92k | VP8Decoder* const dec = (VP8Decoder*)WebPSafeCalloc(1ULL, sizeof(*dec)); |
70 | 1.92k | if (dec != NULL) { |
71 | 1.92k | SetOk(dec); |
72 | 1.92k | WebPGetWorkerInterface()->Init(&dec->worker); |
73 | 1.92k | dec->ready = 0; |
74 | 1.92k | dec->num_parts_minus_one = 0; |
75 | 1.92k | InitGetCoeffs(); |
76 | 1.92k | } |
77 | 1.92k | return dec; |
78 | 1.92k | } |
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 | 1.92k | void VP8Delete(VP8Decoder* const dec) { |
92 | 1.92k | if (dec != NULL) { |
93 | 1.92k | VP8Clear(dec); |
94 | 1.92k | WebPSafeFree(dec); |
95 | 1.92k | } |
96 | 1.92k | } |
97 | | |
98 | | int VP8SetError(VP8Decoder* const dec, |
99 | 1.81k | VP8StatusCode error, const char* const msg) { |
100 | | // VP8_STATUS_SUSPENDED is only meaningful in incremental decoding. |
101 | 1.81k | assert(dec->incremental || error != VP8_STATUS_SUSPENDED); |
102 | | // The oldest error reported takes precedence over the new one. |
103 | 1.81k | if (dec->status == VP8_STATUS_OK) { |
104 | 1.81k | dec->status = error; |
105 | 1.81k | dec->error_msg = msg; |
106 | 1.81k | dec->ready = 0; |
107 | 1.81k | } |
108 | 1.81k | return 0; |
109 | 1.81k | } |
110 | | |
111 | | //------------------------------------------------------------------------------ |
112 | | |
113 | 5.82k | int VP8CheckSignature(const uint8_t* const data, size_t data_size) { |
114 | 5.82k | return (data_size >= 3 && |
115 | 5.82k | data[0] == 0x9d && data[1] == 0x01 && data[2] == 0x2a); |
116 | 5.82k | } |
117 | | |
118 | | int VP8GetInfo(const uint8_t* data, size_t data_size, size_t chunk_size, |
119 | 3.89k | int* const width, int* const height) { |
120 | 3.89k | if (data == NULL || data_size < VP8_FRAME_HEADER_SIZE) { |
121 | 0 | return 0; // not enough data |
122 | 0 | } |
123 | | // check signature |
124 | 3.89k | if (!VP8CheckSignature(data + 3, data_size - 3)) { |
125 | 19 | return 0; // Wrong signature. |
126 | 3.87k | } else { |
127 | 3.87k | const uint32_t bits = data[0] | (data[1] << 8) | (data[2] << 16); |
128 | 3.87k | const int key_frame = !(bits & 1); |
129 | 3.87k | const int w = ((data[7] << 8) | data[6]) & 0x3fff; |
130 | 3.87k | const int h = ((data[9] << 8) | data[8]) & 0x3fff; |
131 | | |
132 | 3.87k | if (!key_frame) { // Not a keyframe. |
133 | 2 | return 0; |
134 | 2 | } |
135 | | |
136 | 3.87k | if (((bits >> 1) & 7) > 3) { |
137 | 1 | return 0; // unknown profile |
138 | 1 | } |
139 | 3.87k | if (!((bits >> 4) & 1)) { |
140 | 1 | return 0; // first frame is invisible! |
141 | 1 | } |
142 | 3.87k | if (((bits >> 5)) >= chunk_size) { // partition_length |
143 | 4 | return 0; // inconsistent size information. |
144 | 4 | } |
145 | 3.87k | if (w == 0 || h == 0) { |
146 | 3 | return 0; // We don't support both width and height to be zero. |
147 | 3 | } |
148 | | |
149 | 3.86k | if (width) { |
150 | 3.86k | *width = w; |
151 | 3.86k | } |
152 | 3.86k | if (height) { |
153 | 3.86k | *height = h; |
154 | 3.86k | } |
155 | | |
156 | 3.86k | return 1; |
157 | 3.87k | } |
158 | 3.89k | } |
159 | | |
160 | | //------------------------------------------------------------------------------ |
161 | | // Header parsing |
162 | | |
163 | 1.92k | static void ResetSegmentHeader(VP8SegmentHeader* const hdr) { |
164 | 1.92k | assert(hdr != NULL); |
165 | 1.92k | hdr->use_segment = 0; |
166 | 1.92k | hdr->update_map = 0; |
167 | 1.92k | hdr->absolute_delta = 1; |
168 | 1.92k | memset(hdr->quantizer, 0, sizeof(hdr->quantizer)); |
169 | 1.92k | memset(hdr->filter_strength, 0, sizeof(hdr->filter_strength)); |
170 | 1.92k | } |
171 | | |
172 | | // Paragraph 9.3 |
173 | | static int ParseSegmentHeader(VP8BitReader* br, |
174 | 1.92k | VP8SegmentHeader* hdr, VP8Proba* proba) { |
175 | 1.92k | assert(br != NULL); |
176 | 1.92k | assert(hdr != NULL); |
177 | 1.92k | hdr->use_segment = VP8Get(br, "global-header"); |
178 | 1.92k | if (hdr->use_segment) { |
179 | 326 | hdr->update_map = VP8Get(br, "global-header"); |
180 | 326 | if (VP8Get(br, "global-header")) { // update data |
181 | 203 | int s; |
182 | 203 | hdr->absolute_delta = VP8Get(br, "global-header"); |
183 | 1.01k | for (s = 0; s < NUM_MB_SEGMENTS; ++s) { |
184 | 812 | hdr->quantizer[s] = VP8Get(br, "global-header") ? |
185 | 467 | VP8GetSignedValue(br, 7, "global-header") : 0; |
186 | 812 | } |
187 | 1.01k | for (s = 0; s < NUM_MB_SEGMENTS; ++s) { |
188 | 812 | hdr->filter_strength[s] = VP8Get(br, "global-header") ? |
189 | 425 | VP8GetSignedValue(br, 6, "global-header") : 0; |
190 | 812 | } |
191 | 203 | } |
192 | 326 | if (hdr->update_map) { |
193 | 235 | int s; |
194 | 940 | for (s = 0; s < MB_FEATURE_TREE_PROBS; ++s) { |
195 | 705 | proba->segments[s] = VP8Get(br, "global-header") ? |
196 | 366 | VP8GetValue(br, 8, "global-header") : 255u; |
197 | 705 | } |
198 | 235 | } |
199 | 1.59k | } else { |
200 | 1.59k | hdr->update_map = 0; |
201 | 1.59k | } |
202 | 1.92k | return !br->eof; |
203 | 1.92k | } |
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 | 1.87k | const uint8_t* buf, size_t size) { |
216 | 1.87k | VP8BitReader* const br = &dec->br; |
217 | 1.87k | const uint8_t* sz = buf; |
218 | 1.87k | const uint8_t* buf_end = buf + size; |
219 | 1.87k | const uint8_t* part_start; |
220 | 1.87k | size_t size_left = size; |
221 | 1.87k | size_t last_part; |
222 | 1.87k | size_t p; |
223 | | |
224 | 1.87k | dec->num_parts_minus_one = (1 << VP8GetValue(br, 2, "global-header")) - 1; |
225 | 1.87k | last_part = dec->num_parts_minus_one; |
226 | 1.87k | if (size < 3 * last_part) { |
227 | | // we can't even read the sizes with sz[]! That's a failure. |
228 | 6 | return VP8_STATUS_NOT_ENOUGH_DATA; |
229 | 6 | } |
230 | 1.86k | part_start = buf + last_part * 3; |
231 | 1.86k | size_left -= last_part * 3; |
232 | 2.22k | for (p = 0; p < last_part; ++p) { |
233 | 356 | size_t psize = sz[0] | (sz[1] << 8) | (sz[2] << 16); |
234 | 356 | if (psize > size_left) psize = size_left; |
235 | 356 | VP8InitBitReader(dec->parts + p, part_start, psize); |
236 | 356 | part_start += psize; |
237 | 356 | size_left -= psize; |
238 | 356 | sz += 3; |
239 | 356 | } |
240 | 1.86k | VP8InitBitReader(dec->parts + last_part, part_start, size_left); |
241 | 1.86k | if (part_start < buf_end) return VP8_STATUS_OK; |
242 | 46 | return dec->incremental |
243 | 46 | ? VP8_STATUS_SUSPENDED // Init is ok, but there's not enough data |
244 | 46 | : VP8_STATUS_NOT_ENOUGH_DATA; |
245 | 1.86k | } |
246 | | |
247 | | // Paragraph 9.4 |
248 | 1.89k | static int ParseFilterHeader(VP8BitReader* br, VP8Decoder* const dec) { |
249 | 1.89k | VP8FilterHeader* const hdr = &dec->filter_hdr; |
250 | 1.89k | hdr->simple = VP8Get(br, "global-header"); |
251 | 1.89k | hdr->level = VP8GetValue(br, 6, "global-header"); |
252 | 1.89k | hdr->sharpness = VP8GetValue(br, 3, "global-header"); |
253 | 1.89k | hdr->use_lf_delta = VP8Get(br, "global-header"); |
254 | 1.89k | if (hdr->use_lf_delta) { |
255 | 421 | if (VP8Get(br, "global-header")) { // update lf-delta? |
256 | 142 | int i; |
257 | 710 | for (i = 0; i < NUM_REF_LF_DELTAS; ++i) { |
258 | 568 | if (VP8Get(br, "global-header")) { |
259 | 303 | hdr->ref_lf_delta[i] = VP8GetSignedValue(br, 6, "global-header"); |
260 | 303 | } |
261 | 568 | } |
262 | 710 | for (i = 0; i < NUM_MODE_LF_DELTAS; ++i) { |
263 | 568 | if (VP8Get(br, "global-header")) { |
264 | 281 | hdr->mode_lf_delta[i] = VP8GetSignedValue(br, 6, "global-header"); |
265 | 281 | } |
266 | 568 | } |
267 | 142 | } |
268 | 421 | } |
269 | 1.89k | dec->filter_type = (hdr->level == 0) ? 0 : hdr->simple ? 1 : 2; |
270 | 1.89k | return !br->eof; |
271 | 1.89k | } |
272 | | |
273 | | // Topmost call |
274 | 1.92k | int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) { |
275 | 1.92k | const uint8_t* buf; |
276 | 1.92k | size_t buf_size; |
277 | 1.92k | VP8FrameHeader* frm_hdr; |
278 | 1.92k | VP8PictureHeader* pic_hdr; |
279 | 1.92k | VP8BitReader* br; |
280 | 1.92k | VP8StatusCode status; |
281 | | |
282 | 1.92k | if (dec == NULL) { |
283 | 0 | return 0; |
284 | 0 | } |
285 | 1.92k | SetOk(dec); |
286 | 1.92k | if (io == NULL) { |
287 | 0 | return VP8SetError(dec, VP8_STATUS_INVALID_PARAM, |
288 | 0 | "null VP8Io passed to VP8GetHeaders()"); |
289 | 0 | } |
290 | 1.92k | buf = io->data; |
291 | 1.92k | buf_size = io->data_size; |
292 | 1.92k | 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 | 1.92k | { |
299 | 1.92k | const uint32_t bits = buf[0] | (buf[1] << 8) | (buf[2] << 16); |
300 | 1.92k | frm_hdr = &dec->frm_hdr; |
301 | 1.92k | frm_hdr->key_frame = !(bits & 1); |
302 | 1.92k | frm_hdr->profile = (bits >> 1) & 7; |
303 | 1.92k | frm_hdr->show = (bits >> 4) & 1; |
304 | 1.92k | frm_hdr->partition_length = (bits >> 5); |
305 | 1.92k | if (frm_hdr->profile > 3) { |
306 | 0 | return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR, |
307 | 0 | "Incorrect keyframe parameters."); |
308 | 0 | } |
309 | 1.92k | if (!frm_hdr->show) { |
310 | 0 | return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE, |
311 | 0 | "Frame not displayable."); |
312 | 0 | } |
313 | 1.92k | buf += 3; |
314 | 1.92k | buf_size -= 3; |
315 | 1.92k | } |
316 | | |
317 | 0 | pic_hdr = &dec->pic_hdr; |
318 | 1.92k | if (frm_hdr->key_frame) { |
319 | | // Paragraph 9.2 |
320 | 1.92k | if (buf_size < 7) { |
321 | 0 | return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, |
322 | 0 | "cannot parse picture header"); |
323 | 0 | } |
324 | 1.92k | if (!VP8CheckSignature(buf, buf_size)) { |
325 | 0 | return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR, |
326 | 0 | "Bad code word"); |
327 | 0 | } |
328 | 1.92k | pic_hdr->width = ((buf[4] << 8) | buf[3]) & 0x3fff; |
329 | 1.92k | pic_hdr->xscale = buf[4] >> 6; // ratio: 1, 5/4 5/3 or 2 |
330 | 1.92k | pic_hdr->height = ((buf[6] << 8) | buf[5]) & 0x3fff; |
331 | 1.92k | pic_hdr->yscale = buf[6] >> 6; |
332 | 1.92k | buf += 7; |
333 | 1.92k | buf_size -= 7; |
334 | | |
335 | 1.92k | dec->mb_w = (pic_hdr->width + 15) >> 4; |
336 | 1.92k | dec->mb_h = (pic_hdr->height + 15) >> 4; |
337 | | |
338 | | // Setup default output area (can be later modified during io->setup()) |
339 | 1.92k | io->width = pic_hdr->width; |
340 | 1.92k | 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 | 1.92k | io->use_cropping = 0; |
345 | 1.92k | io->crop_top = 0; |
346 | 1.92k | io->crop_left = 0; |
347 | 1.92k | io->crop_right = io->width; |
348 | 1.92k | io->crop_bottom = io->height; |
349 | 1.92k | io->use_scaling = 0; |
350 | 1.92k | io->scaled_width = io->width; |
351 | 1.92k | io->scaled_height = io->height; |
352 | | |
353 | 1.92k | io->mb_w = io->width; // for soundness |
354 | 1.92k | io->mb_h = io->height; // ditto |
355 | | |
356 | 1.92k | VP8ResetProba(&dec->proba); |
357 | 1.92k | ResetSegmentHeader(&dec->segment_hdr); |
358 | 1.92k | } |
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 | 1.92k | if (frm_hdr->partition_length > buf_size) { |
363 | 4 | return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, |
364 | 4 | "bad partition length"); |
365 | 4 | } |
366 | | |
367 | 1.92k | br = &dec->br; |
368 | 1.92k | VP8InitBitReader(br, buf, frm_hdr->partition_length); |
369 | 1.92k | buf += frm_hdr->partition_length; |
370 | 1.92k | buf_size -= frm_hdr->partition_length; |
371 | | |
372 | 1.92k | if (frm_hdr->key_frame) { |
373 | 1.92k | pic_hdr->colorspace = VP8Get(br, "global-header"); |
374 | 1.92k | pic_hdr->clamp_type = VP8Get(br, "global-header"); |
375 | 1.92k | } |
376 | 1.92k | if (!ParseSegmentHeader(br, &dec->segment_hdr, &dec->proba)) { |
377 | 34 | return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR, |
378 | 34 | "cannot parse segment header"); |
379 | 34 | } |
380 | | // Filter specs |
381 | 1.89k | if (!ParseFilterHeader(br, dec)) { |
382 | 17 | return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR, |
383 | 17 | "cannot parse filter header"); |
384 | 17 | } |
385 | 1.87k | status = ParsePartitions(dec, buf, buf_size); |
386 | 1.87k | if (status != VP8_STATUS_OK) { |
387 | 52 | return VP8SetError(dec, status, "cannot parse partitions"); |
388 | 52 | } |
389 | | |
390 | | // quantizer change |
391 | 1.82k | VP8ParseQuant(dec); |
392 | | |
393 | | // Frame buffer marking |
394 | 1.82k | if (!frm_hdr->key_frame) { |
395 | 0 | return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE, |
396 | 0 | "Not a key frame."); |
397 | 0 | } |
398 | | |
399 | 1.82k | VP8Get(br, "global-header"); // ignore the value of 'update_proba' |
400 | | |
401 | 1.82k | VP8ParseProba(br, dec); |
402 | | |
403 | | // sanitized state |
404 | 1.82k | dec->ready = 1; |
405 | 1.82k | return 1; |
406 | 1.82k | } |
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 | 943k | static int GetLargeValue(VP8BitReader* const br, const uint8_t* const p) { |
423 | 943k | int v; |
424 | 943k | if (!VP8GetBit(br, p[3], "coeffs")) { |
425 | 746k | if (!VP8GetBit(br, p[4], "coeffs")) { |
426 | 510k | v = 2; |
427 | 510k | } else { |
428 | 235k | v = 3 + VP8GetBit(br, p[5], "coeffs"); |
429 | 235k | } |
430 | 746k | } else { |
431 | 196k | if (!VP8GetBit(br, p[6], "coeffs")) { |
432 | 139k | if (!VP8GetBit(br, p[7], "coeffs")) { |
433 | 60.8k | v = 5 + VP8GetBit(br, 159, "coeffs"); |
434 | 78.9k | } else { |
435 | 78.9k | v = 7 + 2 * VP8GetBit(br, 165, "coeffs"); |
436 | 78.9k | v += VP8GetBit(br, 145, "coeffs"); |
437 | 78.9k | } |
438 | 139k | } else { |
439 | 56.9k | const uint8_t* tab; |
440 | 56.9k | const int bit1 = VP8GetBit(br, p[8], "coeffs"); |
441 | 56.9k | const int bit0 = VP8GetBit(br, p[9 + bit1], "coeffs"); |
442 | 56.9k | const int cat = 2 * bit1 + bit0; |
443 | 56.9k | v = 0; |
444 | 502k | for (tab = kCat3456[cat]; *tab; ++tab) { |
445 | 445k | v += v + VP8GetBit(br, *tab, "coeffs"); |
446 | 445k | } |
447 | 56.9k | v += 3 + (8 << cat); |
448 | 56.9k | } |
449 | 196k | } |
450 | 943k | return v; |
451 | 943k | } |
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 | 6.41M | int ctx, const quant_t dq, int n, int16_t* out) { |
457 | 6.41M | const uint8_t* p = prob[n]->probas[ctx]; |
458 | 10.0M | for (; n < 16; ++n) { |
459 | 10.0M | if (!VP8GetBit(br, p[0], "coeffs")) { |
460 | 6.41M | return n; // previous coeff was last non-zero coeff |
461 | 6.41M | } |
462 | 5.16M | while (!VP8GetBit(br, p[1], "coeffs")) { // sequence of zero coeffs |
463 | 1.55M | p = prob[++n]->probas[0]; |
464 | 1.55M | if (n == 16) return 16; |
465 | 1.55M | } |
466 | 3.61M | { // non zero coeff |
467 | 3.61M | const VP8ProbaArray* const p_ctx = &prob[n + 1]->probas[0]; |
468 | 3.61M | int v; |
469 | 3.61M | if (!VP8GetBit(br, p[2], "coeffs")) { |
470 | 2.67M | v = 1; |
471 | 2.67M | p = p_ctx[1]; |
472 | 2.67M | } else { |
473 | 943k | v = GetLargeValue(br, p); |
474 | 943k | p = p_ctx[2]; |
475 | 943k | } |
476 | 3.61M | out[kZigzag[n]] = VP8GetSigned(br, v, "coeffs") * dq[n > 0]; |
477 | 3.61M | } |
478 | 3.61M | } |
479 | 5.71k | return 16; |
480 | 6.41M | } |
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 | 1 | WEBP_DSP_INIT_FUNC(InitGetCoeffs) { |
515 | 1 | if (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kSlowSSSE3)) { |
516 | 0 | GetCoeffs = GetCoeffsAlt; |
517 | 1 | } else { |
518 | 1 | GetCoeffs = GetCoeffsFast; |
519 | 1 | } |
520 | 1 | } |
521 | | |
522 | 6.38M | static WEBP_INLINE uint32_t NzCodeBits(uint32_t nz_coeffs, int nz, int dc_nz) { |
523 | 6.38M | nz_coeffs <<= 2; |
524 | 6.38M | nz_coeffs |= (nz > 3) ? 3 : (nz > 1) ? 2 : dc_nz; |
525 | 6.38M | return nz_coeffs; |
526 | 6.38M | } |
527 | | |
528 | | static int ParseResiduals(VP8Decoder* const dec, |
529 | 266k | VP8MB* const mb, VP8BitReader* const token_br) { |
530 | 266k | const VP8BandProbas* (* const bands)[16 + 1] = dec->proba.bands_ptr; |
531 | 266k | const VP8BandProbas* const * ac_proba; |
532 | 266k | VP8MBData* const block = dec->mb_data + dec->mb_x; |
533 | 266k | const VP8QuantMatrix* const q = &dec->dqm[block->segment]; |
534 | 266k | int16_t* dst = block->coeffs; |
535 | 266k | VP8MB* const left_mb = dec->mb_info - 1; |
536 | 266k | uint8_t tnz, lnz; |
537 | 266k | uint32_t non_zero_y = 0; |
538 | 266k | uint32_t non_zero_uv = 0; |
539 | 266k | int x, y, ch; |
540 | 266k | uint32_t out_t_nz, out_l_nz; |
541 | 266k | int first; |
542 | | |
543 | 266k | memset(dst, 0, 384 * sizeof(*dst)); |
544 | 266k | if (!block->is_i4x4) { // parse DC |
545 | 34.2k | int16_t dc[16] = { 0 }; |
546 | 34.2k | const int ctx = mb->nz_dc + left_mb->nz_dc; |
547 | 34.2k | const int nz = GetCoeffs(token_br, bands[1], ctx, q->y2_mat, 0, dc); |
548 | 34.2k | mb->nz_dc = left_mb->nz_dc = (nz > 0); |
549 | 34.2k | if (nz > 1) { // more than just the DC -> perform the full transform |
550 | 4.96k | VP8TransformWHT(dc, dst); |
551 | 29.3k | } else { // only DC is non-zero -> inlined simplified transform |
552 | 29.3k | int i; |
553 | 29.3k | const int dc0 = (dc[0] + 3) >> 3; |
554 | 498k | for (i = 0; i < 16 * 16; i += 16) dst[i] = dc0; |
555 | 29.3k | } |
556 | 34.2k | first = 1; |
557 | 34.2k | ac_proba = bands[0]; |
558 | 231k | } else { |
559 | 231k | first = 0; |
560 | 231k | ac_proba = bands[3]; |
561 | 231k | } |
562 | | |
563 | 266k | tnz = mb->nz & 0x0f; |
564 | 266k | lnz = left_mb->nz & 0x0f; |
565 | 1.33M | for (y = 0; y < 4; ++y) { |
566 | 1.06M | int l = lnz & 1; |
567 | 1.06M | uint32_t nz_coeffs = 0; |
568 | 5.32M | for (x = 0; x < 4; ++x) { |
569 | 4.25M | const int ctx = l + (tnz & 1); |
570 | 4.25M | const int nz = GetCoeffs(token_br, ac_proba, ctx, q->y1_mat, first, dst); |
571 | 4.25M | l = (nz > first); |
572 | 4.25M | tnz = (tnz >> 1) | (l << 7); |
573 | 4.25M | nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0); |
574 | 4.25M | dst += 16; |
575 | 4.25M | } |
576 | 1.06M | tnz >>= 4; |
577 | 1.06M | lnz = (lnz >> 1) | (l << 7); |
578 | 1.06M | non_zero_y = (non_zero_y << 8) | nz_coeffs; |
579 | 1.06M | } |
580 | 266k | out_t_nz = tnz; |
581 | 266k | out_l_nz = lnz >> 4; |
582 | | |
583 | 798k | for (ch = 0; ch < 4; ch += 2) { |
584 | 532k | uint32_t nz_coeffs = 0; |
585 | 532k | tnz = mb->nz >> (4 + ch); |
586 | 532k | lnz = left_mb->nz >> (4 + ch); |
587 | 1.59M | for (y = 0; y < 2; ++y) { |
588 | 1.06M | int l = lnz & 1; |
589 | 3.19M | for (x = 0; x < 2; ++x) { |
590 | 2.12M | const int ctx = l + (tnz & 1); |
591 | 2.12M | const int nz = GetCoeffs(token_br, bands[2], ctx, q->uv_mat, 0, dst); |
592 | 2.12M | l = (nz > 0); |
593 | 2.12M | tnz = (tnz >> 1) | (l << 3); |
594 | 2.12M | nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0); |
595 | 2.12M | dst += 16; |
596 | 2.12M | } |
597 | 1.06M | tnz >>= 2; |
598 | 1.06M | lnz = (lnz >> 1) | (l << 5); |
599 | 1.06M | } |
600 | | // Note: we don't really need the per-4x4 details for U/V blocks. |
601 | 532k | non_zero_uv |= nz_coeffs << (4 * ch); |
602 | 532k | out_t_nz |= (tnz << 4) << ch; |
603 | 532k | out_l_nz |= (lnz & 0xf0) << ch; |
604 | 532k | } |
605 | 266k | mb->nz = out_t_nz; |
606 | 266k | left_mb->nz = out_l_nz; |
607 | | |
608 | 266k | block->non_zero_y = non_zero_y; |
609 | 266k | 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 | 266k | block->dither = (non_zero_uv & 0xaaaa) ? 0 : q->dither; |
615 | | |
616 | 266k | return !(non_zero_y | non_zero_uv); // will be used for further optimization |
617 | 266k | } |
618 | | |
619 | | //------------------------------------------------------------------------------ |
620 | | // Main loop |
621 | | |
622 | 321k | int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br) { |
623 | 321k | VP8MB* const left = dec->mb_info - 1; |
624 | 321k | VP8MB* const mb = dec->mb_info + dec->mb_x; |
625 | 321k | VP8MBData* const block = dec->mb_data + dec->mb_x; |
626 | 321k | int skip = dec->use_skip_proba ? block->skip : 0; |
627 | | |
628 | 321k | if (!skip) { |
629 | 266k | skip = ParseResiduals(dec, mb, token_br); |
630 | 266k | } else { |
631 | 55.3k | left->nz = mb->nz = 0; |
632 | 55.3k | if (!block->is_i4x4) { |
633 | 43.0k | left->nz_dc = mb->nz_dc = 0; |
634 | 43.0k | } |
635 | 55.3k | block->non_zero_y = 0; |
636 | 55.3k | block->non_zero_uv = 0; |
637 | 55.3k | block->dither = 0; |
638 | 55.3k | } |
639 | | |
640 | 321k | if (dec->filter_type > 0) { // store filter info |
641 | 316k | VP8FInfo* const finfo = dec->f_info + dec->mb_x; |
642 | 316k | *finfo = dec->fstrengths[block->segment][block->is_i4x4]; |
643 | 316k | finfo->f_inner |= !skip; |
644 | 316k | } |
645 | | |
646 | 321k | return !token_br->eof; |
647 | 321k | } |
648 | | |
649 | 27.4k | void VP8InitScanline(VP8Decoder* const dec) { |
650 | 27.4k | VP8MB* const left = dec->mb_info - 1; |
651 | 27.4k | left->nz = 0; |
652 | 27.4k | left->nz_dc = 0; |
653 | 27.4k | memset(dec->intra_l, B_DC_PRED, sizeof(dec->intra_l)); |
654 | 27.4k | dec->mb_x = 0; |
655 | 27.4k | } |
656 | | |
657 | 1.82k | static int ParseFrame(VP8Decoder* const dec, VP8Io* io) { |
658 | 27.4k | for (dec->mb_y = 0; dec->mb_y < dec->br_mb_y; ++dec->mb_y) { |
659 | | // Parse bitstream for this row. |
660 | 27.3k | VP8BitReader* const token_br = |
661 | 27.3k | &dec->parts[dec->mb_y & dec->num_parts_minus_one]; |
662 | 27.3k | if (!VP8ParseIntraModeRow(&dec->br, dec)) { |
663 | 898 | return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, |
664 | 898 | "Premature end-of-partition0 encountered."); |
665 | 898 | } |
666 | 347k | for (; dec->mb_x < dec->mb_w; ++dec->mb_x) { |
667 | 321k | if (!VP8DecodeMB(dec, token_br)) { |
668 | 811 | return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, |
669 | 811 | "Premature end-of-file encountered."); |
670 | 811 | } |
671 | 321k | } |
672 | 25.6k | VP8InitScanline(dec); // Prepare for next scanline |
673 | | |
674 | | // Reconstruct, filter and emit the row. |
675 | 25.6k | if (!VP8ProcessRow(dec, io)) { |
676 | 0 | return VP8SetError(dec, VP8_STATUS_USER_ABORT, "Output aborted."); |
677 | 0 | } |
678 | 25.6k | } |
679 | 112 | if (dec->mt_method > 0) { |
680 | 0 | if (!WebPGetWorkerInterface()->Sync(&dec->worker)) return 0; |
681 | 0 | } |
682 | | |
683 | 112 | return 1; |
684 | 112 | } |
685 | | |
686 | | // Main entry point |
687 | 1.82k | int VP8Decode(VP8Decoder* const dec, VP8Io* const io) { |
688 | 1.82k | int ok = 0; |
689 | 1.82k | if (dec == NULL) { |
690 | 0 | return 0; |
691 | 0 | } |
692 | 1.82k | if (io == NULL) { |
693 | 0 | return VP8SetError(dec, VP8_STATUS_INVALID_PARAM, |
694 | 0 | "NULL VP8Io parameter in VP8Decode()."); |
695 | 0 | } |
696 | | |
697 | 1.82k | if (!dec->ready) { |
698 | 0 | if (!VP8GetHeaders(dec, io)) { |
699 | 0 | return 0; |
700 | 0 | } |
701 | 0 | } |
702 | 1.82k | assert(dec->ready); |
703 | | |
704 | | // Finish setting up the decoding parameter. Will call io->setup(). |
705 | 1.82k | ok = (VP8EnterCritical(dec, io) == VP8_STATUS_OK); |
706 | 1.82k | if (ok) { // good to go. |
707 | | // Will allocate memory and prepare everything. |
708 | 1.82k | if (ok) ok = VP8InitFrame(dec, io); |
709 | | |
710 | | // Main decoding loop |
711 | 1.82k | if (ok) ok = ParseFrame(dec, io); |
712 | | |
713 | | // Exit. |
714 | 1.82k | ok &= VP8ExitCritical(dec, io); |
715 | 1.82k | } |
716 | | |
717 | 1.82k | if (!ok) { |
718 | 1.70k | VP8Clear(dec); |
719 | 1.70k | return 0; |
720 | 1.70k | } |
721 | | |
722 | 112 | dec->ready = 0; |
723 | 112 | return ok; |
724 | 1.82k | } |
725 | | |
726 | 3.63k | void VP8Clear(VP8Decoder* const dec) { |
727 | 3.63k | if (dec == NULL) { |
728 | 0 | return; |
729 | 0 | } |
730 | 3.63k | WebPGetWorkerInterface()->End(&dec->worker); |
731 | 3.63k | WebPDeallocateAlphaMemory(dec); |
732 | 3.63k | WebPSafeFree(dec->mem); |
733 | 3.63k | dec->mem = NULL; |
734 | 3.63k | dec->mem_size = 0; |
735 | 3.63k | memset(&dec->br, 0, sizeof(dec->br)); |
736 | 3.63k | dec->ready = 0; |
737 | 3.63k | } |
738 | | |
739 | | //------------------------------------------------------------------------------ |