FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp3.c
Date: 2026-01-23 14:02:51
Exec Total Coverage
Lines: 1225 1500 81.7%
Functions: 40 43 93.0%
Branches: 795 1064 74.7%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2003-2004 The FFmpeg project
3 * Copyright (C) 2019 Peter Ross
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * On2 VP3/VP4 Video Decoder
25 *
26 * VP3 Video Decoder by Mike Melanson (mike at multimedia.cx)
27 * For more information about the VP3 coding process, visit:
28 * http://wiki.multimedia.cx/index.php?title=On2_VP3
29 *
30 * Theora decoder by Alex Beregszaszi
31 */
32
33 #include "config_components.h"
34
35 #include <stddef.h>
36 #include <string.h>
37
38 #include "libavutil/attributes.h"
39 #include "libavutil/emms.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/mem.h"
42 #include "libavutil/mem_internal.h"
43 #include "libavutil/thread.h"
44
45 #include "avcodec.h"
46 #include "codec_internal.h"
47 #include "decode.h"
48 #include "get_bits.h"
49 #include "hpeldsp.h"
50 #include "jpegquanttables.h"
51 #include "mathops.h"
52 #include "progressframe.h"
53 #include "libavutil/refstruct.h"
54 #include "thread.h"
55 #include "videodsp.h"
56 #include "vp3data.h"
57 #include "vp4data.h"
58 #include "vp3dsp.h"
59 #include "xiph.h"
60
61 #define VP3_MV_VLC_BITS 6
62 #define VP4_MV_VLC_BITS 6
63 #define SUPERBLOCK_VLC_BITS 6
64
65 #define FRAGMENT_PIXELS 8
66
67 // FIXME split things out into their own arrays
68 typedef struct Vp3Fragment {
69 int16_t dc;
70 uint8_t coding_method;
71 uint8_t qpi;
72 } Vp3Fragment;
73
74 #define SB_NOT_CODED 0
75 #define SB_PARTIALLY_CODED 1
76 #define SB_FULLY_CODED 2
77
78 // This is the maximum length of a single long bit run that can be encoded
79 // for superblock coding or block qps. Theora special-cases this to read a
80 // bit instead of flipping the current bit to allow for runs longer than 4129.
81 #define MAXIMUM_LONG_BIT_RUN 4129
82
83 #define MODE_INTER_NO_MV 0
84 #define MODE_INTRA 1
85 #define MODE_INTER_PLUS_MV 2
86 #define MODE_INTER_LAST_MV 3
87 #define MODE_INTER_PRIOR_LAST 4
88 #define MODE_USING_GOLDEN 5
89 #define MODE_GOLDEN_MV 6
90 #define MODE_INTER_FOURMV 7
91 #define CODING_MODE_COUNT 8
92
93 /* special internal mode */
94 #define MODE_COPY 8
95
96 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb);
97 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb);
98
99
100 /* There are 6 preset schemes, plus a free-form scheme */
101 static const int ModeAlphabet[6][CODING_MODE_COUNT] = {
102 /* scheme 1: Last motion vector dominates */
103 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
104 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
105 MODE_INTRA, MODE_USING_GOLDEN,
106 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
107
108 /* scheme 2 */
109 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
110 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
111 MODE_INTRA, MODE_USING_GOLDEN,
112 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
113
114 /* scheme 3 */
115 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
116 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
117 MODE_INTRA, MODE_USING_GOLDEN,
118 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
119
120 /* scheme 4 */
121 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
122 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
123 MODE_INTRA, MODE_USING_GOLDEN,
124 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
125
126 /* scheme 5: No motion vector dominates */
127 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
128 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
129 MODE_INTRA, MODE_USING_GOLDEN,
130 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
131
132 /* scheme 6 */
133 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
134 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
135 MODE_INTER_PLUS_MV, MODE_INTRA,
136 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
137 };
138
139 static const uint8_t hilbert_offset[16][2] = {
140 { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
141 { 0, 2 }, { 0, 3 }, { 1, 3 }, { 1, 2 },
142 { 2, 2 }, { 2, 3 }, { 3, 3 }, { 3, 2 },
143 { 3, 1 }, { 2, 1 }, { 2, 0 }, { 3, 0 }
144 };
145
146 enum {
147 VP4_DC_INTRA = 0,
148 VP4_DC_INTER = 1,
149 VP4_DC_GOLDEN = 2,
150 NB_VP4_DC_TYPES,
151 VP4_DC_UNDEFINED = NB_VP4_DC_TYPES
152 };
153
154 static const uint8_t vp4_pred_block_type_map[8] = {
155 [MODE_INTER_NO_MV] = VP4_DC_INTER,
156 [MODE_INTRA] = VP4_DC_INTRA,
157 [MODE_INTER_PLUS_MV] = VP4_DC_INTER,
158 [MODE_INTER_LAST_MV] = VP4_DC_INTER,
159 [MODE_INTER_PRIOR_LAST] = VP4_DC_INTER,
160 [MODE_USING_GOLDEN] = VP4_DC_GOLDEN,
161 [MODE_GOLDEN_MV] = VP4_DC_GOLDEN,
162 [MODE_INTER_FOURMV] = VP4_DC_INTER,
163 };
164
165 static VLCElem superblock_run_length_vlc[88]; /* version < 2 */
166 static VLCElem fragment_run_length_vlc[56]; /* version < 2 */
167 static VLCElem motion_vector_vlc[112]; /* version < 2 */
168
169 // The VP4 tables reuse this vlc.
170 static VLCElem mode_code_vlc[24 + 2108 * CONFIG_VP4_DECODER];
171
172 #if CONFIG_VP4_DECODER
173 static const VLCElem *vp4_mv_vlc_table[2][7]; /* version >= 2 */
174 static const VLCElem *block_pattern_vlc[2]; /* version >= 2 */
175 #endif
176
177 typedef struct {
178 int dc;
179 int type;
180 } VP4Predictor;
181
182 #define MIN_DEQUANT_VAL 2
183
184 typedef struct HuffEntry {
185 uint8_t len, sym;
186 } HuffEntry;
187
188 typedef struct HuffTable {
189 HuffEntry entries[32];
190 uint8_t nb_entries;
191 } HuffTable;
192
193 typedef struct CoeffVLCs {
194 const VLCElem *vlc_tabs[80];
195 VLC vlcs[80];
196 } CoeffVLCs;
197
198 typedef struct Vp3DecodeContext {
199 AVCodecContext *avctx;
200 int theora, theora_tables, theora_header;
201 int version;
202 int width, height;
203 int chroma_x_shift, chroma_y_shift;
204 ProgressFrame golden_frame;
205 ProgressFrame last_frame;
206 ProgressFrame current_frame;
207 int keyframe;
208 uint8_t idct_permutation[64];
209 uint8_t idct_scantable[64];
210 HpelDSPContext hdsp;
211 VideoDSPContext vdsp;
212 VP3DSPContext vp3dsp;
213 DECLARE_ALIGNED(16, int16_t, block)[64];
214 int flipped_image;
215 int last_slice_end;
216 int skip_loop_filter;
217
218 int qps[3];
219 int nqps;
220
221 int superblock_count;
222 int y_superblock_width;
223 int y_superblock_height;
224 int y_superblock_count;
225 int c_superblock_width;
226 int c_superblock_height;
227 int c_superblock_count;
228 int u_superblock_start;
229 int v_superblock_start;
230 unsigned char *superblock_coding;
231
232 int macroblock_count; /* y macroblock count */
233 int macroblock_width;
234 int macroblock_height;
235 int c_macroblock_count;
236 int c_macroblock_width;
237 int c_macroblock_height;
238 int yuv_macroblock_count; /* y+u+v macroblock count */
239
240 int fragment_count;
241 int fragment_width[2];
242 int fragment_height[2];
243
244 Vp3Fragment *all_fragments;
245 int fragment_start[3];
246 int data_offset[3];
247 uint8_t offset_x;
248 uint8_t offset_y;
249 int offset_x_warned;
250
251 int8_t (*motion_val[2])[2];
252
253 /* tables */
254 uint16_t coded_dc_scale_factor[2][64];
255 uint32_t coded_ac_scale_factor[64];
256 uint8_t base_matrix[384][64];
257 uint8_t qr_count[2][3];
258 uint8_t qr_size[2][3][64];
259 uint16_t qr_base[2][3][64];
260
261 /**
262 * This is a list of all tokens in bitstream order. Reordering takes place
263 * by pulling from each level during IDCT. As a consequence, IDCT must be
264 * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
265 * otherwise. The 32 different tokens with up to 12 bits of extradata are
266 * collapsed into 3 types, packed as follows:
267 * (from the low to high bits)
268 *
269 * 2 bits: type (0,1,2)
270 * 0: EOB run, 14 bits for run length (12 needed)
271 * 1: zero run, 7 bits for run length
272 * 7 bits for the next coefficient (3 needed)
273 * 2: coefficient, 14 bits (11 needed)
274 *
275 * Coefficients are signed, so are packed in the highest bits for automatic
276 * sign extension.
277 */
278 int16_t *dct_tokens[3][64];
279 int16_t *dct_tokens_base;
280 #define TOKEN_EOB(eob_run) ((eob_run) << 2)
281 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) * 512) + ((zero_run) << 2) + 1)
282 #define TOKEN_COEFF(coeff) (((coeff) * 4) + 2)
283
284 /**
285 * number of blocks that contain DCT coefficients at
286 * the given level or higher
287 */
288 int num_coded_frags[3][64];
289 int total_num_coded_frags;
290
291 /* this is a list of indexes into the all_fragments array indicating
292 * which of the fragments are coded */
293 int *coded_fragment_list[3];
294
295 int *kf_coded_fragment_list;
296 int *nkf_coded_fragment_list;
297 int num_kf_coded_fragment[3];
298
299 /**
300 * The first 16 of the following VLCs are for the dc coefficients;
301 * the others are four groups of 16 VLCs each for ac coefficients.
302 * This is a RefStruct reference to share these VLCs between threads.
303 */
304 CoeffVLCs *coeff_vlc;
305
306 /* these arrays need to be on 16-byte boundaries since SSE2 operations
307 * index into them */
308 DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; ///< qmat[qpi][is_inter][plane]
309
310 /* This table contains superblock_count * 16 entries. Each set of 16
311 * numbers corresponds to the fragment indexes 0..15 of the superblock.
312 * An entry will be -1 to indicate that no entry corresponds to that
313 * index. */
314 int *superblock_fragments;
315
316 /* This is an array that indicates how a particular macroblock
317 * is coded. */
318 unsigned char *macroblock_coding;
319
320 uint8_t *edge_emu_buffer;
321
322 /* Huffman decode */
323 HuffTable huffman_table[5 * 16];
324
325 uint8_t filter_limit_values[64];
326 DECLARE_ALIGNED(16, int, bounding_values_array)[256 + 4];
327
328 VP4Predictor * dc_pred_row; /* dc_pred_row[y_superblock_width * 4] */
329 } Vp3DecodeContext;
330
331 /************************************************************************
332 * VP3 specific functions
333 ************************************************************************/
334
335 70 static av_cold void free_tables(AVCodecContext *avctx)
336 {
337 70 Vp3DecodeContext *s = avctx->priv_data;
338
339 70 av_freep(&s->superblock_coding);
340 70 av_freep(&s->all_fragments);
341 70 av_freep(&s->nkf_coded_fragment_list);
342 70 av_freep(&s->kf_coded_fragment_list);
343 70 av_freep(&s->dct_tokens_base);
344 70 av_freep(&s->superblock_fragments);
345 70 av_freep(&s->macroblock_coding);
346 70 av_freep(&s->dc_pred_row);
347 70 av_freep(&s->motion_val[0]);
348 70 av_freep(&s->motion_val[1]);
349 70 }
350
351 35 static av_cold void vp3_decode_flush(AVCodecContext *avctx)
352 {
353 35 Vp3DecodeContext *s = avctx->priv_data;
354
355 35 ff_progress_frame_unref(&s->golden_frame);
356 35 ff_progress_frame_unref(&s->last_frame);
357 35 ff_progress_frame_unref(&s->current_frame);
358 35 }
359
360 35 static av_cold int vp3_decode_end(AVCodecContext *avctx)
361 {
362 35 Vp3DecodeContext *s = avctx->priv_data;
363
364 35 free_tables(avctx);
365 35 av_freep(&s->edge_emu_buffer);
366
367 35 s->theora_tables = 0;
368
369 /* release all frames */
370 35 vp3_decode_flush(avctx);
371
372 35 av_refstruct_unref(&s->coeff_vlc);
373
374 35 return 0;
375 }
376
377 /**
378 * This function sets up all of the various blocks mappings:
379 * superblocks <-> fragments, macroblocks <-> fragments,
380 * superblocks <-> macroblocks
381 *
382 * @return 0 is successful; returns 1 if *anything* went wrong.
383 */
384 35 static int init_block_mapping(Vp3DecodeContext *s)
385 {
386 35 int j = 0;
387
388
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 35 times.
140 for (int plane = 0; plane < 3; plane++) {
389 105 int sb_width = plane ? s->c_superblock_width
390
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 35 times.
105 : s->y_superblock_width;
391 105 int sb_height = plane ? s->c_superblock_height
392
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 35 times.
105 : s->y_superblock_height;
393 105 int frag_width = s->fragment_width[!!plane];
394 105 int frag_height = s->fragment_height[!!plane];
395
396
2/2
✓ Branch 0 taken 1653 times.
✓ Branch 1 taken 105 times.
1758 for (int sb_y = 0; sb_y < sb_height; sb_y++)
397
2/2
✓ Branch 0 taken 46399 times.
✓ Branch 1 taken 1653 times.
48052 for (int sb_x = 0; sb_x < sb_width; sb_x++)
398
2/2
✓ Branch 0 taken 742384 times.
✓ Branch 1 taken 46399 times.
788783 for (int i = 0; i < 16; i++) {
399 742384 int x = 4 * sb_x + hilbert_offset[i][0];
400 742384 int y = 4 * sb_y + hilbert_offset[i][1];
401
402
4/4
✓ Branch 0 taken 738864 times.
✓ Branch 1 taken 3520 times.
✓ Branch 2 taken 717114 times.
✓ Branch 3 taken 21750 times.
742384 if (x < frag_width && y < frag_height)
403 717114 s->superblock_fragments[j++] = s->fragment_start[plane] +
404 717114 y * frag_width + x;
405 else
406 25270 s->superblock_fragments[j++] = -1;
407 }
408 }
409
410 35 return 0; /* successful path out */
411 }
412
413 /*
414 * This function sets up the dequantization tables used for a particular
415 * frame.
416 */
417 62 static void init_dequantizer(Vp3DecodeContext *s, int qpi)
418 {
419 62 int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
420
421
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 62 times.
186 for (int inter = 0; inter < 2; inter++) {
422
2/2
✓ Branch 0 taken 372 times.
✓ Branch 1 taken 124 times.
496 for (int plane = 0; plane < 3; plane++) {
423 372 int dc_scale_factor = s->coded_dc_scale_factor[!!plane][s->qps[qpi]];
424 372 int sum = 0, bmi, bmj, qistart, qri;
425
1/2
✓ Branch 0 taken 408 times.
✗ Branch 1 not taken.
408 for (qri = 0; qri < s->qr_count[inter][plane]; qri++) {
426 408 sum += s->qr_size[inter][plane][qri];
427
2/2
✓ Branch 0 taken 372 times.
✓ Branch 1 taken 36 times.
408 if (s->qps[qpi] <= sum)
428 372 break;
429 }
430 372 qistart = sum - s->qr_size[inter][plane][qri];
431 372 bmi = s->qr_base[inter][plane][qri];
432 372 bmj = s->qr_base[inter][plane][qri + 1];
433
2/2
✓ Branch 0 taken 23808 times.
✓ Branch 1 taken 372 times.
24180 for (int i = 0; i < 64; i++) {
434 23808 int coeff = (2 * (sum - s->qps[qpi]) * s->base_matrix[bmi][i] -
435 23808 2 * (qistart - s->qps[qpi]) * s->base_matrix[bmj][i] +
436 23808 s->qr_size[inter][plane][qri]) /
437 23808 (2 * s->qr_size[inter][plane][qri]);
438
439 23808 int qmin = 8 << (inter + !i);
440
2/2
✓ Branch 0 taken 23436 times.
✓ Branch 1 taken 372 times.
23808 int qscale = i ? ac_scale_factor : dc_scale_factor;
441 23808 int qbias = (1 + inter) * 3;
442
2/2
✓ Branch 0 taken 23436 times.
✓ Branch 1 taken 372 times.
47616 s->qmat[qpi][inter][plane][s->idct_permutation[i]] =
443
2/2
✓ Branch 0 taken 21924 times.
✓ Branch 1 taken 1512 times.
23808 (i == 0 || s->version < 2) ? av_clip((qscale * coeff) / 100 * 4, qmin, 4096)
444 1512 : (qscale * (coeff - qbias) / 100 + qbias) * 4;
445 }
446 /* all DC coefficients use the same quant so as not to interfere
447 * with DC prediction */
448 372 s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
449 }
450 }
451 62 }
452
453 /*
454 * This function initializes the loop filter boundary limits if the frame's
455 * quality index is different from the previous frame's.
456 *
457 * The filter_limit_values may not be larger than 127.
458 */
459 62 static void init_loop_filter(Vp3DecodeContext *s)
460 {
461 62 ff_vp3dsp_set_bounding_values(s->bounding_values_array, s->filter_limit_values[s->qps[0]]);
462 62 }
463
464 /*
465 * This function unpacks all of the superblock/macroblock/fragment coding
466 * information from the bitstream.
467 */
468 140 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
469 {
470 140 const int superblock_starts[3] = {
471 140 0, s->u_superblock_start, s->v_superblock_start
472 };
473 140 int bit = 0;
474 140 int current_superblock = 0;
475 140 int current_run = 0;
476 140 int num_partial_superblocks = 0;
477
478 int current_fragment;
479 140 int plane0_num_coded_frags = 0;
480
481
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 136 times.
140 if (s->keyframe) {
482 4 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
483 } else {
484 /* unpack the list of partially-coded superblocks */
485 136 bit = get_bits1(gb) ^ 1;
486 136 current_run = 0;
487
488
3/4
✓ Branch 0 taken 8545 times.
✓ Branch 1 taken 136 times.
✓ Branch 3 taken 8545 times.
✗ Branch 4 not taken.
8681 while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {
489
3/4
✓ Branch 0 taken 1475 times.
✓ Branch 1 taken 7070 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1475 times.
8545 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
490 bit = get_bits1(gb);
491 else
492 8545 bit ^= 1;
493
494 8545 current_run = get_vlc2(gb, superblock_run_length_vlc,
495 SUPERBLOCK_VLC_BITS, 2);
496
2/2
✓ Branch 0 taken 437 times.
✓ Branch 1 taken 8108 times.
8545 if (current_run == 34)
497 437 current_run += get_bits(gb, 12);
498
499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8545 times.
8545 if (current_run > s->superblock_count - current_superblock) {
500 av_log(s->avctx, AV_LOG_ERROR,
501 "Invalid partially coded superblock run length\n");
502 return -1;
503 }
504
505 8545 memset(s->superblock_coding + current_superblock, bit, current_run);
506
507 8545 current_superblock += current_run;
508
2/2
✓ Branch 0 taken 4237 times.
✓ Branch 1 taken 4308 times.
8545 if (bit)
509 4237 num_partial_superblocks += current_run;
510 }
511
512 /* unpack the list of fully coded superblocks if any of the blocks were
513 * not marked as partially coded in the previous step */
514
1/2
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
136 if (num_partial_superblocks < s->superblock_count) {
515 136 int superblocks_decoded = 0;
516
517 136 current_superblock = 0;
518 136 bit = get_bits1(gb) ^ 1;
519 136 current_run = 0;
520
521
3/4
✓ Branch 0 taken 1052 times.
✓ Branch 1 taken 136 times.
✓ Branch 2 taken 1052 times.
✗ Branch 3 not taken.
2240 while (superblocks_decoded < s->superblock_count - num_partial_superblocks &&
522 1052 get_bits_left(gb) > 0) {
523
4/4
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 939 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 107 times.
1052 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
524 6 bit = get_bits1(gb);
525 else
526 1046 bit ^= 1;
527
528 1052 current_run = get_vlc2(gb, superblock_run_length_vlc,
529 SUPERBLOCK_VLC_BITS, 2);
530
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 834 times.
1052 if (current_run == 34)
531 218 current_run += get_bits(gb, 12);
532
533
2/2
✓ Branch 0 taken 104900 times.
✓ Branch 1 taken 1052 times.
105952 for (int j = 0; j < current_run; current_superblock++) {
534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104900 times.
104900 if (current_superblock >= s->superblock_count) {
535 av_log(s->avctx, AV_LOG_ERROR,
536 "Invalid fully coded superblock run length\n");
537 return -1;
538 }
539
540 /* skip any superblocks already marked as partially coded */
541
2/2
✓ Branch 0 taken 88554 times.
✓ Branch 1 taken 16346 times.
104900 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
542 88554 s->superblock_coding[current_superblock] = 2 * bit;
543 88554 j++;
544 }
545 }
546 1052 superblocks_decoded += current_run;
547 }
548 }
549
550 /* if there were partial blocks, initialize bitstream for
551 * unpacking fragment codings */
552
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 8 times.
136 if (num_partial_superblocks) {
553 128 current_run = 0;
554 128 bit = get_bits1(gb);
555 /* toggle the bit because as soon as the first run length is
556 * fetched the bit will be toggled again */
557 128 bit ^= 1;
558 }
559 }
560
561 /* figure out which fragments are coded; iterate through each
562 * superblock (all planes) */
563 140 s->total_num_coded_frags = 0;
564 140 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
565
566 280 s->coded_fragment_list[0] = s->keyframe ? s->kf_coded_fragment_list
567
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 136 times.
140 : s->nkf_coded_fragment_list;
568
569
2/2
✓ Branch 0 taken 420 times.
✓ Branch 1 taken 140 times.
560 for (int plane = 0; plane < 3; plane++) {
570 420 int sb_start = superblock_starts[plane];
571 420 int sb_end = sb_start + (plane ? s->c_superblock_count
572
2/2
✓ Branch 0 taken 280 times.
✓ Branch 1 taken 140 times.
420 : s->y_superblock_count);
573 420 int num_coded_frags = 0;
574
575
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 408 times.
420 if (s->keyframe) {
576
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (s->num_kf_coded_fragment[plane] == -1) {
577
2/2
✓ Branch 0 taken 10526 times.
✓ Branch 1 taken 12 times.
10538 for (int i = sb_start; i < sb_end; i++) {
578 /* iterate through all 16 fragments in a superblock */
579
2/2
✓ Branch 0 taken 168416 times.
✓ Branch 1 taken 10526 times.
178942 for (int j = 0; j < 16; j++) {
580 /* if the fragment is in bounds, check its coding status */
581 168416 current_fragment = s->superblock_fragments[i * 16 + j];
582
2/2
✓ Branch 0 taken 162492 times.
✓ Branch 1 taken 5924 times.
168416 if (current_fragment != -1) {
583 162492 s->coded_fragment_list[plane][num_coded_frags++] =
584 current_fragment;
585 }
586 }
587 }
588 12 s->num_kf_coded_fragment[plane] = num_coded_frags;
589 } else
590 num_coded_frags = s->num_kf_coded_fragment[plane];
591 } else {
592
3/4
✓ Branch 0 taken 105186 times.
✓ Branch 1 taken 408 times.
✓ Branch 3 taken 105186 times.
✗ Branch 4 not taken.
105594 for (int i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
593
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 105186 times.
105186 if (get_bits_left(gb) < plane0_num_coded_frags >> 2) {
594 return AVERROR_INVALIDDATA;
595 }
596 /* iterate through all 16 fragments in a superblock */
597
2/2
✓ Branch 0 taken 1682976 times.
✓ Branch 1 taken 105186 times.
1788162 for (int j = 0; j < 16; j++) {
598 /* if the fragment is in bounds, check its coding status */
599 1682976 current_fragment = s->superblock_fragments[i * 16 + j];
600
2/2
✓ Branch 0 taken 1598148 times.
✓ Branch 1 taken 84828 times.
1682976 if (current_fragment != -1) {
601 1598148 int coded = s->superblock_coding[i];
602
603
2/2
✓ Branch 0 taken 241844 times.
✓ Branch 1 taken 1356304 times.
1598148 if (coded == SB_PARTIALLY_CODED) {
604 /* fragment may or may not be coded; this is the case
605 * that cares about the fragment coding runs */
606
2/2
✓ Branch 0 taken 70601 times.
✓ Branch 1 taken 171243 times.
241844 if (current_run-- == 0) {
607 70601 bit ^= 1;
608 70601 current_run = get_vlc2(gb, fragment_run_length_vlc, 5, 2);
609 }
610 241844 coded = bit;
611 }
612
613
2/2
✓ Branch 0 taken 370084 times.
✓ Branch 1 taken 1228064 times.
1598148 if (coded) {
614 /* default mode; actual mode will be decoded in
615 * the next phase */
616 370084 s->all_fragments[current_fragment].coding_method =
617 MODE_INTER_NO_MV;
618 370084 s->coded_fragment_list[plane][num_coded_frags++] =
619 current_fragment;
620 } else {
621 /* not coded; copy this fragment from the prior frame */
622 1228064 s->all_fragments[current_fragment].coding_method =
623 MODE_COPY;
624 }
625 }
626 }
627 }
628 }
629
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 280 times.
420 if (!plane)
630 140 plane0_num_coded_frags = num_coded_frags;
631 420 s->total_num_coded_frags += num_coded_frags;
632
2/2
✓ Branch 0 taken 26880 times.
✓ Branch 1 taken 420 times.
27300 for (int i = 0; i < 64; i++)
633 26880 s->num_coded_frags[plane][i] = num_coded_frags;
634
2/2
✓ Branch 0 taken 280 times.
✓ Branch 1 taken 140 times.
420 if (plane < 2)
635 280 s->coded_fragment_list[plane + 1] = s->coded_fragment_list[plane] +
636 num_coded_frags;
637 }
638 140 return 0;
639 }
640
641 #define BLOCK_X (2 * mb_x + (k & 1))
642 #define BLOCK_Y (2 * mb_y + (k >> 1))
643
644 #if CONFIG_VP4_DECODER
645 /**
646 * @return number of blocks, or > yuv_macroblock_count on error.
647 * return value is always >= 1.
648 */
649 7007 static int vp4_get_mb_count(Vp3DecodeContext *s, GetBitContext *gb)
650 {
651 7007 int v = 1;
652 int bits;
653
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7007 times.
7007 while ((bits = show_bits(gb, 9)) == 0x1ff) {
654 skip_bits(gb, 9);
655 v += 256;
656 if (v > s->yuv_macroblock_count) {
657 av_log(s->avctx, AV_LOG_ERROR, "Invalid run length\n");
658 return v;
659 }
660 }
661 #define body(n) { \
662 skip_bits(gb, 2 + n); \
663 v += (1 << n) + get_bits(gb, n); }
664 #define thresh(n) (0x200 - (0x80 >> n))
665 #define else_if(n) else if (bits < thresh(n)) body(n)
666
2/2
✓ Branch 0 taken 2784 times.
✓ Branch 1 taken 4223 times.
7007 if (bits < 0x100) {
667 2784 skip_bits(gb, 1);
668
2/2
✓ Branch 0 taken 1763 times.
✓ Branch 1 taken 2460 times.
4223 } else if (bits < thresh(0)) {
669 1763 skip_bits(gb, 2);
670 1763 v += 1;
671 }
672
2/2
✓ Branch 0 taken 994 times.
✓ Branch 1 taken 1466 times.
2460 else_if(1)
673
2/2
✓ Branch 0 taken 667 times.
✓ Branch 1 taken 799 times.
1466 else_if(2)
674
2/2
✓ Branch 0 taken 483 times.
✓ Branch 1 taken 316 times.
799 else_if(3)
675
2/2
✓ Branch 0 taken 224 times.
✓ Branch 1 taken 92 times.
316 else_if(4)
676
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 4 times.
92 else_if(5)
677
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 else_if(6)
678 else body(7)
679 #undef body
680 #undef thresh
681 #undef else_if
682 7007 return v;
683 }
684
685 4556 static int vp4_get_block_pattern(GetBitContext *gb, int *next_block_pattern_table)
686 {
687 4556 int v = get_vlc2(gb, block_pattern_vlc[*next_block_pattern_table], 5, 1);
688 4556 *next_block_pattern_table = vp4_block_pattern_table_selector[v];
689 4556 return v + 1;
690 }
691
692 24 static int vp4_unpack_macroblocks(Vp3DecodeContext *s, GetBitContext *gb)
693 {
694 int fragment;
695 int next_block_pattern_table;
696 int bit, current_run, has_partial;
697
698 24 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
699
700
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 22 times.
24 if (s->keyframe)
701 2 return 0;
702
703 22 has_partial = 0;
704 22 bit = get_bits1(gb);
705
2/2
✓ Branch 0 taken 4132 times.
✓ Branch 1 taken 22 times.
4154 for (int i = 0; i < s->yuv_macroblock_count; i += current_run) {
706
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4132 times.
4132 if (get_bits_left(gb) <= 0)
707 return AVERROR_INVALIDDATA;
708 4132 current_run = vp4_get_mb_count(s, gb);
709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4132 times.
4132 if (current_run > s->yuv_macroblock_count - i)
710 return -1;
711 4132 memset(s->superblock_coding + i, 2 * bit, current_run);
712 4132 bit ^= 1;
713 4132 has_partial |= bit;
714 }
715
716
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (has_partial) {
717
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 if (get_bits_left(gb) <= 0)
718 return AVERROR_INVALIDDATA;
719 22 bit = get_bits1(gb);
720 22 current_run = vp4_get_mb_count(s, gb);
721
2/2
✓ Branch 0 taken 20064 times.
✓ Branch 1 taken 22 times.
20086 for (int i = 0; i < s->yuv_macroblock_count; i++) {
722
2/2
✓ Branch 0 taken 8403 times.
✓ Branch 1 taken 11661 times.
20064 if (!s->superblock_coding[i]) {
723
2/2
✓ Branch 0 taken 2853 times.
✓ Branch 1 taken 5550 times.
8403 if (!current_run) {
724 2853 bit ^= 1;
725 2853 current_run = vp4_get_mb_count(s, gb);
726 }
727 8403 s->superblock_coding[i] = bit;
728 8403 current_run--;
729 }
730 }
731
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (current_run) /* handle situation when vp4_get_mb_count() fails */
732 return -1;
733 }
734
735 22 next_block_pattern_table = 0;
736
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 22 times.
88 for (int plane = 0, i = 0; plane < 3; plane++) {
737
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 22 times.
66 int sb_width = plane ? s->c_superblock_width : s->y_superblock_width;
738
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 22 times.
66 int sb_height = plane ? s->c_superblock_height : s->y_superblock_height;
739
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 22 times.
66 int mb_width = plane ? s->c_macroblock_width : s->macroblock_width;
740
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 22 times.
66 int mb_height = plane ? s->c_macroblock_height : s->macroblock_height;
741 66 int fragment_width = s->fragment_width[!!plane];
742 66 int fragment_height = s->fragment_height[!!plane];
743
744
2/2
✓ Branch 0 taken 352 times.
✓ Branch 1 taken 66 times.
418 for (int sb_y = 0; sb_y < sb_height; sb_y++) {
745
2/2
✓ Branch 0 taken 5104 times.
✓ Branch 1 taken 352 times.
5456 for (int sb_x = 0; sb_x < sb_width; sb_x++) {
746
2/2
✓ Branch 0 taken 20416 times.
✓ Branch 1 taken 5104 times.
25520 for (int j = 0; j < 4; j++) {
747 20416 int mb_x = 2 * sb_x + (j >> 1);
748 20416 int mb_y = 2 * sb_y + (j >> 1) ^ (j & 1);
749 int mb_coded, pattern, coded;
750
751
3/4
✓ Branch 0 taken 20064 times.
✓ Branch 1 taken 352 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20064 times.
20416 if (mb_x >= mb_width || mb_y >= mb_height)
752 352 continue;
753
754 20064 mb_coded = s->superblock_coding[i++];
755
756
2/2
✓ Branch 0 taken 11661 times.
✓ Branch 1 taken 8403 times.
20064 if (mb_coded == SB_FULLY_CODED)
757 11661 pattern = 0xF;
758
2/2
✓ Branch 0 taken 4556 times.
✓ Branch 1 taken 3847 times.
8403 else if (mb_coded == SB_PARTIALLY_CODED)
759 4556 pattern = vp4_get_block_pattern(gb, &next_block_pattern_table);
760 else
761 3847 pattern = 0;
762
763
2/2
✓ Branch 0 taken 80256 times.
✓ Branch 1 taken 20064 times.
100320 for (int k = 0; k < 4; k++) {
764
2/4
✓ Branch 0 taken 80256 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 80256 times.
80256 if (BLOCK_X >= fragment_width || BLOCK_Y >= fragment_height)
765 continue;
766 80256 fragment = s->fragment_start[plane] + BLOCK_Y * fragment_width + BLOCK_X;
767 80256 coded = pattern & (8 >> k);
768 /* MODE_INTER_NO_MV is the default for coded fragments.
769 the actual method is decoded in the next phase. */
770
2/2
✓ Branch 0 taken 55956 times.
✓ Branch 1 taken 24300 times.
80256 s->all_fragments[fragment].coding_method = coded ? MODE_INTER_NO_MV : MODE_COPY;
771 }
772 }
773 }
774 }
775 }
776 22 return 0;
777 }
778 #endif
779
780 /*
781 * This function unpacks all the coding mode data for individual macroblocks
782 * from the bitstream.
783 */
784 164 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
785 {
786 int scheme;
787 int current_macroblock;
788 int current_fragment;
789 int coding_mode;
790 int custom_mode_alphabet[CODING_MODE_COUNT];
791 const int *alphabet;
792 Vp3Fragment *frag;
793
794
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 158 times.
164 if (s->keyframe) {
795
2/2
✓ Branch 0 taken 169788 times.
✓ Branch 1 taken 6 times.
169794 for (int i = 0; i < s->fragment_count; i++)
796 169788 s->all_fragments[i].coding_method = MODE_INTRA;
797 } else {
798 /* fetch the mode coding scheme for this frame */
799 158 scheme = get_bits(gb, 3);
800
801 /* is it a custom coding scheme? */
802
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 135 times.
158 if (scheme == 0) {
803
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 23 times.
207 for (int i = 0; i < 8; i++)
804 184 custom_mode_alphabet[i] = MODE_INTER_NO_MV;
805
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 23 times.
207 for (int i = 0; i < 8; i++)
806 184 custom_mode_alphabet[get_bits(gb, 3)] = i;
807 23 alphabet = custom_mode_alphabet;
808 } else
809 135 alphabet = ModeAlphabet[scheme - 1];
810
811 /* iterate through all of the macroblocks that contain 1 or more
812 * coded fragments */
813
2/2
✓ Branch 0 taken 1880 times.
✓ Branch 1 taken 158 times.
2038 for (int sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
814
2/2
✓ Branch 0 taken 72050 times.
✓ Branch 1 taken 1880 times.
73930 for (int sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
815
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72050 times.
72050 if (get_bits_left(gb) <= 0)
816 return -1;
817
818
2/2
✓ Branch 0 taken 288200 times.
✓ Branch 1 taken 72050 times.
360250 for (int j = 0; j < 4; j++) {
819 int k;
820 288200 int mb_x = 2 * sb_x + (j >> 1);
821 288200 int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
822 288200 current_macroblock = mb_y * s->macroblock_width + mb_x;
823
824
2/2
✓ Branch 0 taken 287276 times.
✓ Branch 1 taken 924 times.
288200 if (mb_x >= s->macroblock_width ||
825
2/2
✓ Branch 0 taken 7542 times.
✓ Branch 1 taken 279734 times.
287276 mb_y >= s->macroblock_height)
826 8466 continue;
827
828 /* coding modes are only stored if the macroblock has
829 * at least one luma block coded, otherwise it must be
830 * INTER_NO_MV */
831
2/2
✓ Branch 0 taken 878591 times.
✓ Branch 1 taken 195385 times.
1073976 for (k = 0; k < 4; k++) {
832 878591 current_fragment = BLOCK_Y *
833 878591 s->fragment_width[0] + BLOCK_X;
834
2/2
✓ Branch 0 taken 84349 times.
✓ Branch 1 taken 794242 times.
878591 if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
835 84349 break;
836 }
837
2/2
✓ Branch 0 taken 195385 times.
✓ Branch 1 taken 84349 times.
279734 if (k == 4) {
838 195385 s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
839 195385 continue;
840 }
841
842 /* mode 7 means get 3 bits for each coding mode */
843
2/2
✓ Branch 0 taken 2750 times.
✓ Branch 1 taken 81599 times.
84349 if (scheme == 7)
844 2750 coding_mode = get_bits(gb, 3);
845 else
846 81599 coding_mode = alphabet[get_vlc2(gb, mode_code_vlc, 4, 2)];
847
848 84349 s->macroblock_coding[current_macroblock] = coding_mode;
849
2/2
✓ Branch 0 taken 337396 times.
✓ Branch 1 taken 84349 times.
421745 for (k = 0; k < 4; k++) {
850 337396 frag = s->all_fragments + BLOCK_Y * s->fragment_width[0] + BLOCK_X;
851
2/2
✓ Branch 0 taken 307251 times.
✓ Branch 1 taken 30145 times.
337396 if (frag->coding_method != MODE_COPY)
852 307251 frag->coding_method = coding_mode;
853 }
854
855 #define SET_CHROMA_MODES \
856 if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
857 frag[s->fragment_start[1]].coding_method = coding_mode; \
858 if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
859 frag[s->fragment_start[2]].coding_method = coding_mode;
860
861
1/2
✓ Branch 0 taken 84349 times.
✗ Branch 1 not taken.
84349 if (s->chroma_y_shift) {
862 84349 frag = s->all_fragments + mb_y *
863 84349 s->fragment_width[1] + mb_x;
864
4/4
✓ Branch 0 taken 59715 times.
✓ Branch 1 taken 24634 times.
✓ Branch 2 taken 56198 times.
✓ Branch 3 taken 28151 times.
84349 SET_CHROMA_MODES
865 } else if (s->chroma_x_shift) {
866 frag = s->all_fragments +
867 2 * mb_y * s->fragment_width[1] + mb_x;
868 for (k = 0; k < 2; k++) {
869 SET_CHROMA_MODES
870 frag += s->fragment_width[1];
871 }
872 } else {
873 for (k = 0; k < 4; k++) {
874 frag = s->all_fragments +
875 BLOCK_Y * s->fragment_width[1] + BLOCK_X;
876 SET_CHROMA_MODES
877 }
878 }
879 }
880 }
881 }
882 }
883
884 164 return 0;
885 }
886
887 3758 static int vp4_get_mv(GetBitContext *gb, int axis, int last_motion)
888 {
889 #if CONFIG_VP4_DECODER
890 3758 int v = get_vlc2(gb, vp4_mv_vlc_table[axis][vp4_mv_table_selector[FFABS(last_motion)]],
891 VP4_MV_VLC_BITS, 2);
892
2/2
✓ Branch 0 taken 1082 times.
✓ Branch 1 taken 2676 times.
3758 return last_motion < 0 ? -v : v;
893 #else
894 return 0;
895 #endif
896 }
897
898 /*
899 * This function unpacks all the motion vectors for the individual
900 * macroblocks from the bitstream.
901 */
902 164 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
903 {
904 int coding_mode;
905 int motion_x[4];
906 int motion_y[4];
907 164 int last_motion_x = 0;
908 164 int last_motion_y = 0;
909 164 int prior_last_motion_x = 0;
910 164 int prior_last_motion_y = 0;
911 164 int last_gold_motion_x = 0;
912 164 int last_gold_motion_y = 0;
913 int current_macroblock;
914 int current_fragment;
915 int frag;
916
917
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 158 times.
164 if (s->keyframe)
918 6 return 0;
919
920 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme; 2 is VP4 code scheme */
921
2/2
✓ Branch 0 taken 136 times.
✓ Branch 1 taken 22 times.
158 coding_mode = s->version < 2 ? get_bits1(gb) : 2;
922
923 /* iterate through all of the macroblocks that contain 1 or more
924 * coded fragments */
925
2/2
✓ Branch 0 taken 1880 times.
✓ Branch 1 taken 158 times.
2038 for (int sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
926
2/2
✓ Branch 0 taken 72050 times.
✓ Branch 1 taken 1880 times.
73930 for (int sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
927
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72050 times.
72050 if (get_bits_left(gb) <= 0)
928 return -1;
929
930
2/2
✓ Branch 0 taken 288200 times.
✓ Branch 1 taken 72050 times.
360250 for (int j = 0; j < 4; j++) {
931 288200 int mb_x = 2 * sb_x + (j >> 1);
932 288200 int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
933 288200 current_macroblock = mb_y * s->macroblock_width + mb_x;
934
935
2/2
✓ Branch 0 taken 287276 times.
✓ Branch 1 taken 924 times.
288200 if (mb_x >= s->macroblock_width ||
936
2/2
✓ Branch 0 taken 279734 times.
✓ Branch 1 taken 7542 times.
287276 mb_y >= s->macroblock_height ||
937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 279734 times.
279734 s->macroblock_coding[current_macroblock] == MODE_COPY)
938 8466 continue;
939
940
6/6
✓ Branch 0 taken 711 times.
✓ Branch 1 taken 14184 times.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 42307 times.
✓ Branch 4 taken 11934 times.
✓ Branch 5 taken 210535 times.
279734 switch (s->macroblock_coding[current_macroblock]) {
941 711 case MODE_GOLDEN_MV:
942
2/2
✓ Branch 0 taken 337 times.
✓ Branch 1 taken 374 times.
711 if (coding_mode == 2) { /* VP4 */
943 337 last_gold_motion_x = motion_x[0] = vp4_get_mv(gb, 0, last_gold_motion_x);
944 337 last_gold_motion_y = motion_y[0] = vp4_get_mv(gb, 1, last_gold_motion_y);
945 337 break;
946 } /* otherwise fall through */
947 case MODE_INTER_PLUS_MV:
948 /* all 6 fragments use the same motion vector */
949
2/2
✓ Branch 0 taken 13074 times.
✓ Branch 1 taken 1484 times.
14558 if (coding_mode == 0) {
950 13074 motion_x[0] = get_vlc2(gb, motion_vector_vlc,
951 VP3_MV_VLC_BITS, 2);
952 13074 motion_y[0] = get_vlc2(gb, motion_vector_vlc,
953 VP3_MV_VLC_BITS, 2);
954
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 1442 times.
1484 } else if (coding_mode == 1) {
955 42 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
956 42 motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
957 } else { /* VP4 */
958 1442 motion_x[0] = vp4_get_mv(gb, 0, last_motion_x);
959 1442 motion_y[0] = vp4_get_mv(gb, 1, last_motion_y);
960 }
961
962 /* vector maintenance, only on MODE_INTER_PLUS_MV */
963
2/2
✓ Branch 0 taken 14184 times.
✓ Branch 1 taken 374 times.
14558 if (s->macroblock_coding[current_macroblock] == MODE_INTER_PLUS_MV) {
964 14184 prior_last_motion_x = last_motion_x;
965 14184 prior_last_motion_y = last_motion_y;
966 14184 last_motion_x = motion_x[0];
967 14184 last_motion_y = motion_y[0];
968 }
969 14558 break;
970
971 63 case MODE_INTER_FOURMV:
972 /* vector maintenance */
973 63 prior_last_motion_x = last_motion_x;
974 63 prior_last_motion_y = last_motion_y;
975
976 /* fetch 4 vectors from the bitstream, one for each
977 * Y fragment, then average for the C fragment vectors */
978
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 63 times.
315 for (int k = 0; k < 4; k++) {
979 252 current_fragment = BLOCK_Y * s->fragment_width[0] + BLOCK_X;
980
1/2
✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
252 if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
981
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 204 times.
252 if (coding_mode == 0) {
982 48 motion_x[k] = get_vlc2(gb, motion_vector_vlc,
983 VP3_MV_VLC_BITS, 2);
984 48 motion_y[k] = get_vlc2(gb, motion_vector_vlc,
985 VP3_MV_VLC_BITS, 2);
986
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 100 times.
204 } else if (coding_mode == 1) {
987 104 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
988 104 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
989 } else { /* VP4 */
990 100 motion_x[k] = vp4_get_mv(gb, 0, prior_last_motion_x);
991 100 motion_y[k] = vp4_get_mv(gb, 1, prior_last_motion_y);
992 }
993 252 last_motion_x = motion_x[k];
994 252 last_motion_y = motion_y[k];
995 } else {
996 motion_x[k] = 0;
997 motion_y[k] = 0;
998 }
999 }
1000 63 break;
1001
1002 42307 case MODE_INTER_LAST_MV:
1003 /* all 6 fragments use the last motion vector */
1004 42307 motion_x[0] = last_motion_x;
1005 42307 motion_y[0] = last_motion_y;
1006
1007 /* no vector maintenance (last vector remains the
1008 * last vector) */
1009 42307 break;
1010
1011 11934 case MODE_INTER_PRIOR_LAST:
1012 /* all 6 fragments use the motion vector prior to the
1013 * last motion vector */
1014 11934 motion_x[0] = prior_last_motion_x;
1015 11934 motion_y[0] = prior_last_motion_y;
1016
1017 /* vector maintenance */
1018 11934 prior_last_motion_x = last_motion_x;
1019 11934 prior_last_motion_y = last_motion_y;
1020 11934 last_motion_x = motion_x[0];
1021 11934 last_motion_y = motion_y[0];
1022 11934 break;
1023
1024 210535 default:
1025 /* covers intra, inter without MV, golden without MV */
1026 210535 motion_x[0] = 0;
1027 210535 motion_y[0] = 0;
1028
1029 /* no vector maintenance */
1030 210535 break;
1031 }
1032
1033 /* assign the motion vectors to the correct fragments */
1034
2/2
✓ Branch 0 taken 1118936 times.
✓ Branch 1 taken 279734 times.
1398670 for (int k = 0; k < 4; k++) {
1035 1118936 current_fragment =
1036 1118936 BLOCK_Y * s->fragment_width[0] + BLOCK_X;
1037
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 1118684 times.
1118936 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1038 252 s->motion_val[0][current_fragment][0] = motion_x[k];
1039 252 s->motion_val[0][current_fragment][1] = motion_y[k];
1040 } else {
1041 1118684 s->motion_val[0][current_fragment][0] = motion_x[0];
1042 1118684 s->motion_val[0][current_fragment][1] = motion_y[0];
1043 }
1044 }
1045
1046
1/2
✓ Branch 0 taken 279734 times.
✗ Branch 1 not taken.
279734 if (s->chroma_y_shift) {
1047
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 279671 times.
279734 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1048
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 32 times.
63 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] +
1049 motion_x[2] + motion_x[3], 2);
1050
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 28 times.
63 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] +
1051 motion_y[2] + motion_y[3], 2);
1052 }
1053
2/2
✓ Branch 0 taken 266358 times.
✓ Branch 1 taken 13376 times.
279734 if (s->version <= 2) {
1054 266358 motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
1055 266358 motion_y[0] = (motion_y[0] >> 1) | (motion_y[0] & 1);
1056 }
1057 279734 frag = mb_y * s->fragment_width[1] + mb_x;
1058 279734 s->motion_val[1][frag][0] = motion_x[0];
1059 279734 s->motion_val[1][frag][1] = motion_y[0];
1060 } else if (s->chroma_x_shift) {
1061 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1062 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
1063 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
1064 motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
1065 motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
1066 } else {
1067 motion_x[1] = motion_x[0];
1068 motion_y[1] = motion_y[0];
1069 }
1070 if (s->version <= 2) {
1071 motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
1072 motion_x[1] = (motion_x[1] >> 1) | (motion_x[1] & 1);
1073 }
1074 frag = 2 * mb_y * s->fragment_width[1] + mb_x;
1075 for (int k = 0; k < 2; k++) {
1076 s->motion_val[1][frag][0] = motion_x[k];
1077 s->motion_val[1][frag][1] = motion_y[k];
1078 frag += s->fragment_width[1];
1079 }
1080 } else {
1081 for (int k = 0; k < 4; k++) {
1082 frag = BLOCK_Y * s->fragment_width[1] + BLOCK_X;
1083 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1084 s->motion_val[1][frag][0] = motion_x[k];
1085 s->motion_val[1][frag][1] = motion_y[k];
1086 } else {
1087 s->motion_val[1][frag][0] = motion_x[0];
1088 s->motion_val[1][frag][1] = motion_y[0];
1089 }
1090 }
1091 }
1092 }
1093 }
1094 }
1095
1096 158 return 0;
1097 }
1098
1099 164 static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
1100 {
1101 164 int num_blocks = s->total_num_coded_frags;
1102
1103
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 164 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
164 for (int qpi = 0; qpi < s->nqps - 1 && num_blocks > 0; qpi++) {
1104 int i = 0, blocks_decoded = 0, num_blocks_at_qpi = 0;
1105 int bit, run_length;
1106
1107 bit = get_bits1(gb) ^ 1;
1108 run_length = 0;
1109
1110 do {
1111 if (run_length == MAXIMUM_LONG_BIT_RUN)
1112 bit = get_bits1(gb);
1113 else
1114 bit ^= 1;
1115
1116 run_length = get_vlc2(gb, superblock_run_length_vlc,
1117 SUPERBLOCK_VLC_BITS, 2);
1118 if (run_length == 34)
1119 run_length += get_bits(gb, 12);
1120 blocks_decoded += run_length;
1121
1122 if (!bit)
1123 num_blocks_at_qpi += run_length;
1124
1125 for (int j = 0; j < run_length; i++) {
1126 if (i >= s->total_num_coded_frags)
1127 return -1;
1128
1129 if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
1130 s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
1131 j++;
1132 }
1133 }
1134 } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
1135
1136 num_blocks -= num_blocks_at_qpi;
1137 }
1138
1139 164 return 0;
1140 }
1141
1142 259052 static inline int get_eob_run(GetBitContext *gb, int token)
1143 {
1144 259052 int v = eob_run_table[token].base;
1145
2/2
✓ Branch 0 taken 33652 times.
✓ Branch 1 taken 225400 times.
259052 if (eob_run_table[token].bits)
1146 33652 v += get_bits(gb, eob_run_table[token].bits);
1147 259052 return v;
1148 }
1149
1150 2755489 static inline int get_coeff(GetBitContext *gb, int token, int16_t *coeff)
1151 {
1152 int bits_to_get, zero_run;
1153
1154 2755489 bits_to_get = coeff_get_bits[token];
1155
2/2
✓ Branch 0 taken 1617513 times.
✓ Branch 1 taken 1137976 times.
2755489 if (bits_to_get)
1156 1617513 bits_to_get = get_bits(gb, bits_to_get);
1157 2755489 *coeff = coeff_tables[token][bits_to_get];
1158
1159 2755489 zero_run = zero_run_base[token];
1160
2/2
✓ Branch 0 taken 342682 times.
✓ Branch 1 taken 2412807 times.
2755489 if (zero_run_get_bits[token])
1161 342682 zero_run += get_bits(gb, zero_run_get_bits[token]);
1162
1163 2755489 return zero_run;
1164 }
1165
1166 /*
1167 * This function is called by unpack_dct_coeffs() to extract the VLCs from
1168 * the bitstream. The VLCs encode tokens which are used to unpack DCT
1169 * data. This function unpacks all the VLCs for either the Y plane or both
1170 * C planes, and is called for DC coefficients or different AC coefficient
1171 * levels (since different coefficient types require different VLC tables.
1172 *
1173 * This function returns a residual eob run. E.g, if a particular token gave
1174 * instructions to EOB the next 5 fragments and there were only 2 fragments
1175 * left in the current fragment range, 3 would be returned so that it could
1176 * be passed into the next call to this same function.
1177 */
1178 26880 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1179 const VLCElem *vlc_table, int coeff_index,
1180 int plane,
1181 int eob_run)
1182 {
1183 26880 int j = 0;
1184 int token;
1185 26880 int zero_run = 0;
1186 26880 int16_t coeff = 0;
1187 int blocks_ended;
1188 26880 int coeff_i = 0;
1189 26880 int num_coeffs = s->num_coded_frags[plane][coeff_index];
1190 26880 int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
1191
1192 /* local references to structure members to avoid repeated dereferences */
1193 26880 const int *coded_fragment_list = s->coded_fragment_list[plane];
1194 26880 Vp3Fragment *all_fragments = s->all_fragments;
1195
1196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26880 times.
26880 if (num_coeffs < 0) {
1197 av_log(s->avctx, AV_LOG_ERROR,
1198 "Invalid number of coefficients at level %d\n", coeff_index);
1199 return AVERROR_INVALIDDATA;
1200 }
1201
1202
2/2
✓ Branch 0 taken 3487 times.
✓ Branch 1 taken 23393 times.
26880 if (eob_run > num_coeffs) {
1203 3487 coeff_i =
1204 3487 blocks_ended = num_coeffs;
1205 3487 eob_run -= num_coeffs;
1206 } else {
1207 23393 coeff_i =
1208 23393 blocks_ended = eob_run;
1209 23393 eob_run = 0;
1210 }
1211
1212 // insert fake EOB token to cover the split between planes or zzi
1213
2/2
✓ Branch 0 taken 2019 times.
✓ Branch 1 taken 24861 times.
26880 if (blocks_ended)
1214 2019 dct_tokens[j++] = blocks_ended << 2;
1215
1216
3/4
✓ Branch 0 taken 2060240 times.
✓ Branch 1 taken 26880 times.
✓ Branch 3 taken 2060240 times.
✗ Branch 4 not taken.
2087120 while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
1217 /* decode a VLC into a token */
1218 2060240 token = get_vlc2(gb, vlc_table, 11, 3);
1219 /* use the token to get a zero run, a coefficient, and an eob run */
1220
2/2
✓ Branch 0 taken 220061 times.
✓ Branch 1 taken 1840179 times.
2060240 if ((unsigned) token <= 6U) {
1221 220061 eob_run = get_eob_run(gb, token);
1222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 220061 times.
220061 if (!eob_run)
1223 eob_run = INT_MAX;
1224
1225 // record only the number of blocks ended in this plane,
1226 // any spill will be recorded in the next plane.
1227
2/2
✓ Branch 0 taken 1656 times.
✓ Branch 1 taken 218405 times.
220061 if (eob_run > num_coeffs - coeff_i) {
1228 1656 dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
1229 1656 blocks_ended += num_coeffs - coeff_i;
1230 1656 eob_run -= num_coeffs - coeff_i;
1231 1656 coeff_i = num_coeffs;
1232 } else {
1233 218405 dct_tokens[j++] = TOKEN_EOB(eob_run);
1234 218405 blocks_ended += eob_run;
1235 218405 coeff_i += eob_run;
1236 218405 eob_run = 0;
1237 }
1238
1/2
✓ Branch 0 taken 1840179 times.
✗ Branch 1 not taken.
1840179 } else if (token >= 0) {
1239 1840179 zero_run = get_coeff(gb, token, &coeff);
1240
1241
2/2
✓ Branch 0 taken 601916 times.
✓ Branch 1 taken 1238263 times.
1840179 if (zero_run) {
1242 601916 dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
1243 } else {
1244 // Save DC into the fragment structure. DC prediction is
1245 // done in raster order, so the actual DC can't be in with
1246 // other tokens. We still need the token in dct_tokens[]
1247 // however, or else the structure collapses on itself.
1248
2/2
✓ Branch 0 taken 241967 times.
✓ Branch 1 taken 996296 times.
1238263 if (!coeff_index)
1249 241967 all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
1250
1251 1238263 dct_tokens[j++] = TOKEN_COEFF(coeff);
1252 }
1253
1254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1840179 times.
1840179 if (coeff_index + zero_run > 64) {
1255 av_log(s->avctx, AV_LOG_DEBUG,
1256 "Invalid zero run of %d with %d coeffs left\n",
1257 zero_run, 64 - coeff_index);
1258 zero_run = 64 - coeff_index;
1259 }
1260
1261 // zero runs code multiple coefficients,
1262 // so don't try to decode coeffs for those higher levels
1263
2/2
✓ Branch 0 taken 2541089 times.
✓ Branch 1 taken 1840179 times.
4381268 for (int i = coeff_index + 1; i <= coeff_index + zero_run; i++)
1264 2541089 s->num_coded_frags[plane][i]--;
1265 1840179 coeff_i++;
1266 } else {
1267 av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1268 return -1;
1269 }
1270 }
1271
1272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26880 times.
26880 if (blocks_ended > s->num_coded_frags[plane][coeff_index])
1273 av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
1274
1275 // decrement the number of blocks that have higher coefficients for each
1276 // EOB run at this level
1277
2/2
✓ Branch 0 taken 8292 times.
✓ Branch 1 taken 18588 times.
26880 if (blocks_ended)
1278
2/2
✓ Branch 0 taken 372769 times.
✓ Branch 1 taken 8292 times.
381061 for (int i = coeff_index + 1; i < 64; i++)
1279 372769 s->num_coded_frags[plane][i] -= blocks_ended;
1280
1281 // setup the next buffer
1282
2/2
✓ Branch 0 taken 17920 times.
✓ Branch 1 taken 8960 times.
26880 if (plane < 2)
1283 17920 s->dct_tokens[plane + 1][coeff_index] = dct_tokens + j;
1284
2/2
✓ Branch 0 taken 8820 times.
✓ Branch 1 taken 140 times.
8960 else if (coeff_index < 63)
1285 8820 s->dct_tokens[0][coeff_index + 1] = dct_tokens + j;
1286
1287 26880 return eob_run;
1288 }
1289
1290 static void reverse_dc_prediction(Vp3DecodeContext *s,
1291 int first_fragment,
1292 int fragment_width,
1293 int fragment_height);
1294 /*
1295 * This function unpacks all of the DCT coefficient data from the
1296 * bitstream.
1297 */
1298 140 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1299 {
1300 140 const VLCElem *const *coeff_vlc = s->coeff_vlc->vlc_tabs;
1301 int dc_y_table;
1302 int dc_c_table;
1303 int ac_y_table;
1304 int ac_c_table;
1305 140 int residual_eob_run = 0;
1306 const VLCElem *y_tables[64], *c_tables[64];
1307
1308 140 s->dct_tokens[0][0] = s->dct_tokens_base;
1309
1310
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
140 if (get_bits_left(gb) < 16)
1311 return AVERROR_INVALIDDATA;
1312
1313 /* fetch the DC table indexes */
1314 140 dc_y_table = get_bits(gb, 4);
1315 140 dc_c_table = get_bits(gb, 4);
1316
1317 /* unpack the Y plane DC coefficients */
1318 140 residual_eob_run = unpack_vlcs(s, gb, coeff_vlc[dc_y_table], 0,
1319 0, residual_eob_run);
1320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140 times.
140 if (residual_eob_run < 0)
1321 return residual_eob_run;
1322
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
140 if (get_bits_left(gb) < 8)
1323 return AVERROR_INVALIDDATA;
1324
1325 /* reverse prediction of the Y-plane DC coefficients */
1326 140 reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
1327
1328 /* unpack the C plane DC coefficients */
1329 140 residual_eob_run = unpack_vlcs(s, gb, coeff_vlc[dc_c_table], 0,
1330 1, residual_eob_run);
1331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140 times.
140 if (residual_eob_run < 0)
1332 return residual_eob_run;
1333 140 residual_eob_run = unpack_vlcs(s, gb, coeff_vlc[dc_c_table], 0,
1334 2, residual_eob_run);
1335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140 times.
140 if (residual_eob_run < 0)
1336 return residual_eob_run;
1337
1338 /* reverse prediction of the C-plane DC coefficients */
1339
1/2
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
140 if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1340 140 reverse_dc_prediction(s, s->fragment_start[1],
1341 s->fragment_width[1], s->fragment_height[1]);
1342 140 reverse_dc_prediction(s, s->fragment_start[2],
1343 s->fragment_width[1], s->fragment_height[1]);
1344 }
1345
1346
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
140 if (get_bits_left(gb) < 8)
1347 return AVERROR_INVALIDDATA;
1348 /* fetch the AC table indexes */
1349 140 ac_y_table = get_bits(gb, 4);
1350 140 ac_c_table = get_bits(gb, 4);
1351
1352 /* build tables of AC VLC tables */
1353
2/2
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 140 times.
840 for (int i = 1; i <= 5; i++) {
1354 /* AC VLC table group 1 */
1355 700 y_tables[i] = coeff_vlc[ac_y_table + 16];
1356 700 c_tables[i] = coeff_vlc[ac_c_table + 16];
1357 }
1358
2/2
✓ Branch 0 taken 1260 times.
✓ Branch 1 taken 140 times.
1400 for (int i = 6; i <= 14; i++) {
1359 /* AC VLC table group 2 */
1360 1260 y_tables[i] = coeff_vlc[ac_y_table + 32];
1361 1260 c_tables[i] = coeff_vlc[ac_c_table + 32];
1362 }
1363
2/2
✓ Branch 0 taken 1820 times.
✓ Branch 1 taken 140 times.
1960 for (int i = 15; i <= 27; i++) {
1364 /* AC VLC table group 3 */
1365 1820 y_tables[i] = coeff_vlc[ac_y_table + 48];
1366 1820 c_tables[i] = coeff_vlc[ac_c_table + 48];
1367 }
1368
2/2
✓ Branch 0 taken 5040 times.
✓ Branch 1 taken 140 times.
5180 for (int i = 28; i <= 63; i++) {
1369 /* AC VLC table group 4 */
1370 5040 y_tables[i] = coeff_vlc[ac_y_table + 64];
1371 5040 c_tables[i] = coeff_vlc[ac_c_table + 64];
1372 }
1373
1374 /* decode all AC coefficients */
1375
2/2
✓ Branch 0 taken 8820 times.
✓ Branch 1 taken 140 times.
8960 for (int i = 1; i <= 63; i++) {
1376 8820 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
1377 0, residual_eob_run);
1378
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8820 times.
8820 if (residual_eob_run < 0)
1379 return residual_eob_run;
1380
1381 8820 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1382 1, residual_eob_run);
1383
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8820 times.
8820 if (residual_eob_run < 0)
1384 return residual_eob_run;
1385 8820 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1386 2, residual_eob_run);
1387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8820 times.
8820 if (residual_eob_run < 0)
1388 return residual_eob_run;
1389 }
1390
1391 140 return 0;
1392 }
1393
1394 #if CONFIG_VP4_DECODER
1395 /**
1396 * eob_tracker[] is instead of TOKEN_EOB(value)
1397 * a dummy TOKEN_EOB(0) value is used to make vp3_dequant work
1398 *
1399 * @return < 0 on error
1400 */
1401 63252 static int vp4_unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1402 const VLCElem *const vlc_tables[64],
1403 int plane, int eob_tracker[64], int fragment)
1404 {
1405 int token;
1406 63252 int zero_run = 0;
1407 63252 int16_t coeff = 0;
1408 63252 int coeff_i = 0;
1409 int eob_run;
1410
1411
2/2
✓ Branch 0 taken 954301 times.
✓ Branch 1 taken 19171 times.
973472 while (!eob_tracker[coeff_i]) {
1412
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 954301 times.
954301 if (get_bits_left(gb) < 1)
1413 return AVERROR_INVALIDDATA;
1414
1415 954301 token = get_vlc2(gb, vlc_tables[coeff_i], 11, 3);
1416
1417 /* use the token to get a zero run, a coefficient, and an eob run */
1418
2/2
✓ Branch 0 taken 38991 times.
✓ Branch 1 taken 915310 times.
954301 if ((unsigned) token <= 6U) {
1419 38991 eob_run = get_eob_run(gb, token);
1420 38991 *s->dct_tokens[plane][coeff_i]++ = TOKEN_EOB(0);
1421 38991 eob_tracker[coeff_i] = eob_run - 1;
1422 38991 return 0;
1423
1/2
✓ Branch 0 taken 915310 times.
✗ Branch 1 not taken.
915310 } else if (token >= 0) {
1424 915310 zero_run = get_coeff(gb, token, &coeff);
1425
1426
2/2
✓ Branch 0 taken 311952 times.
✓ Branch 1 taken 603358 times.
915310 if (zero_run) {
1427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 311952 times.
311952 if (coeff_i + zero_run > 64) {
1428 av_log(s->avctx, AV_LOG_DEBUG,
1429 "Invalid zero run of %d with %d coeffs left\n",
1430 zero_run, 64 - coeff_i);
1431 zero_run = 64 - coeff_i;
1432 }
1433 311952 *s->dct_tokens[plane][coeff_i]++ = TOKEN_ZERO_RUN(coeff, zero_run);
1434 311952 coeff_i += zero_run;
1435 } else {
1436
2/2
✓ Branch 0 taken 40795 times.
✓ Branch 1 taken 562563 times.
603358 if (!coeff_i)
1437 40795 s->all_fragments[fragment].dc = coeff;
1438
1439 603358 *s->dct_tokens[plane][coeff_i]++ = TOKEN_COEFF(coeff);
1440 }
1441 915310 coeff_i++;
1442
2/2
✓ Branch 0 taken 5090 times.
✓ Branch 1 taken 910220 times.
915310 if (coeff_i >= 64) /* > 64 occurs when there is a zero_run overflow */
1443 5090 return 0; /* stop */
1444 } else {
1445 av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1446 return -1;
1447 }
1448 }
1449 19171 *s->dct_tokens[plane][coeff_i]++ = TOKEN_EOB(0);
1450 19171 eob_tracker[coeff_i]--;
1451 19171 return 0;
1452 }
1453
1454 95328 static void vp4_dc_predictor_reset(VP4Predictor *p)
1455 {
1456 95328 p->dc = 0;
1457 95328 p->type = VP4_DC_UNDEFINED;
1458 95328 }
1459
1460 5568 static void vp4_dc_pred_before(const Vp3DecodeContext *s, VP4Predictor dc_pred[6][6], int sb_x)
1461 {
1462
2/2
✓ Branch 0 taken 22272 times.
✓ Branch 1 taken 5568 times.
27840 for (int i = 0; i < 4; i++)
1463 22272 dc_pred[0][i + 1] = s->dc_pred_row[sb_x * 4 + i];
1464
1465
2/2
✓ Branch 0 taken 22272 times.
✓ Branch 1 taken 5568 times.
27840 for (int j = 1; j < 5; j++)
1466
2/2
✓ Branch 0 taken 89088 times.
✓ Branch 1 taken 22272 times.
111360 for (int i = 0; i < 4; i++)
1467 89088 vp4_dc_predictor_reset(&dc_pred[j][i + 1]);
1468 5568 }
1469
1470 5568 static void vp4_dc_pred_after(Vp3DecodeContext *s, VP4Predictor dc_pred[6][6], int sb_x)
1471 {
1472
2/2
✓ Branch 0 taken 22272 times.
✓ Branch 1 taken 5568 times.
27840 for (int i = 0; i < 4; i++)
1473 22272 s->dc_pred_row[sb_x * 4 + i] = dc_pred[4][i + 1];
1474
1475
2/2
✓ Branch 0 taken 22272 times.
✓ Branch 1 taken 5568 times.
27840 for (int i = 1; i < 5; i++)
1476 22272 dc_pred[i][0] = dc_pred[i][4];
1477 5568 }
1478
1479 /* note: dc_pred points to the current block */
1480 63252 static int vp4_dc_pred(const Vp3DecodeContext *s, const VP4Predictor * dc_pred, const int * last_dc, int type, int plane)
1481 {
1482 63252 int count = 0;
1483 63252 int dc = 0;
1484
1485
2/2
✓ Branch 0 taken 29726 times.
✓ Branch 1 taken 33526 times.
63252 if (dc_pred[-6].type == type) {
1486 29726 dc += dc_pred[-6].dc;
1487 29726 count++;
1488 }
1489
1490
2/2
✓ Branch 0 taken 19581 times.
✓ Branch 1 taken 43671 times.
63252 if (dc_pred[6].type == type) {
1491 19581 dc += dc_pred[6].dc;
1492 19581 count++;
1493 }
1494
1495
4/4
✓ Branch 0 taken 56133 times.
✓ Branch 1 taken 7119 times.
✓ Branch 2 taken 38121 times.
✓ Branch 3 taken 18012 times.
63252 if (count != 2 && dc_pred[-1].type == type) {
1496 38121 dc += dc_pred[-1].dc;
1497 38121 count++;
1498 }
1499
1500
4/4
✓ Branch 0 taken 32714 times.
✓ Branch 1 taken 30538 times.
✓ Branch 2 taken 2073 times.
✓ Branch 3 taken 30641 times.
63252 if (count != 2 && dc_pred[1].type == type) {
1501 2073 dc += dc_pred[1].dc;
1502 2073 count++;
1503 }
1504
1505 /* using division instead of shift to correctly handle negative values */
1506
2/2
✓ Branch 0 taken 32080 times.
✓ Branch 1 taken 31172 times.
63252 return count == 2 ? dc / 2 : last_dc[type];
1507 }
1508
1509 48 static void vp4_set_tokens_base(Vp3DecodeContext *s)
1510 {
1511 48 int16_t *base = s->dct_tokens_base;
1512
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 48 times.
192 for (int plane = 0; plane < 3; plane++) {
1513
2/2
✓ Branch 0 taken 9216 times.
✓ Branch 1 taken 144 times.
9360 for (int i = 0; i < 64; i++) {
1514 9216 s->dct_tokens[plane][i] = base;
1515 9216 base += s->fragment_width[!!plane] * s->fragment_height[!!plane];
1516 }
1517 }
1518 48 }
1519
1520 24 static int vp4_unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1521 {
1522 24 const VLCElem *const *coeff_vlc = s->coeff_vlc->vlc_tabs;
1523 int dc_y_table;
1524 int dc_c_table;
1525 int ac_y_table;
1526 int ac_c_table;
1527 const VLCElem *tables[2][64];
1528 int eob_tracker[64];
1529 VP4Predictor dc_pred[6][6];
1530 int last_dc[NB_VP4_DC_TYPES];
1531
1532
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if (get_bits_left(gb) < 16)
1533 return AVERROR_INVALIDDATA;
1534
1535 /* fetch the DC table indexes */
1536 24 dc_y_table = get_bits(gb, 4);
1537 24 dc_c_table = get_bits(gb, 4);
1538
1539 24 ac_y_table = get_bits(gb, 4);
1540 24 ac_c_table = get_bits(gb, 4);
1541
1542 /* build tables of DC/AC VLC tables */
1543
1544 /* DC table group */
1545 24 tables[0][0] = coeff_vlc[dc_y_table];
1546 24 tables[1][0] = coeff_vlc[dc_c_table];
1547
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 24 times.
144 for (int i = 1; i <= 5; i++) {
1548 /* AC VLC table group 1 */
1549 120 tables[0][i] = coeff_vlc[ac_y_table + 16];
1550 120 tables[1][i] = coeff_vlc[ac_c_table + 16];
1551 }
1552
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 24 times.
240 for (int i = 6; i <= 14; i++) {
1553 /* AC VLC table group 2 */
1554 216 tables[0][i] = coeff_vlc[ac_y_table + 32];
1555 216 tables[1][i] = coeff_vlc[ac_c_table + 32];
1556 }
1557
2/2
✓ Branch 0 taken 312 times.
✓ Branch 1 taken 24 times.
336 for (int i = 15; i <= 27; i++) {
1558 /* AC VLC table group 3 */
1559 312 tables[0][i] = coeff_vlc[ac_y_table + 48];
1560 312 tables[1][i] = coeff_vlc[ac_c_table + 48];
1561 }
1562
2/2
✓ Branch 0 taken 864 times.
✓ Branch 1 taken 24 times.
888 for (int i = 28; i <= 63; i++) {
1563 /* AC VLC table group 4 */
1564 864 tables[0][i] = coeff_vlc[ac_y_table + 64];
1565 864 tables[1][i] = coeff_vlc[ac_c_table + 64];
1566 }
1567
1568 24 vp4_set_tokens_base(s);
1569
1570 24 memset(last_dc, 0, sizeof(last_dc));
1571
1572
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 24 times.
96 for (int plane = 0; plane < ((s->avctx->flags & AV_CODEC_FLAG_GRAY) ? 1 : 3); plane++) {
1573 72 memset(eob_tracker, 0, sizeof(eob_tracker));
1574
1575 /* initialise dc prediction */
1576
2/2
✓ Branch 0 taken 3648 times.
✓ Branch 1 taken 72 times.
3720 for (int i = 0; i < s->fragment_width[!!plane]; i++)
1577 3648 vp4_dc_predictor_reset(&s->dc_pred_row[i]);
1578
1579
2/2
✓ Branch 0 taken 432 times.
✓ Branch 1 taken 72 times.
504 for (int j = 0; j < 6; j++)
1580
2/2
✓ Branch 0 taken 2592 times.
✓ Branch 1 taken 432 times.
3024 for (int i = 0; i < 6; i++)
1581 2592 vp4_dc_predictor_reset(&dc_pred[j][i]);
1582
1583
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 72 times.
456 for (int sb_y = 0; sb_y * 4 < s->fragment_height[!!plane]; sb_y++) {
1584
2/2
✓ Branch 0 taken 5568 times.
✓ Branch 1 taken 384 times.
5952 for (int sb_x = 0; sb_x *4 < s->fragment_width[!!plane]; sb_x++) {
1585 5568 vp4_dc_pred_before(s, dc_pred, sb_x);
1586
2/2
✓ Branch 0 taken 89088 times.
✓ Branch 1 taken 5568 times.
94656 for (int j = 0; j < 16; j++) {
1587 89088 int hx = hilbert_offset[j][0];
1588 89088 int hy = hilbert_offset[j][1];
1589 89088 int x = 4 * sb_x + hx;
1590 89088 int y = 4 * sb_y + hy;
1591 89088 VP4Predictor *this_dc_pred = &dc_pred[hy + 1][hx + 1];
1592 int fragment, dc_block_type;
1593
1594
3/4
✓ Branch 0 taken 87552 times.
✓ Branch 1 taken 1536 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 87552 times.
89088 if (x >= s->fragment_width[!!plane] || y >= s->fragment_height[!!plane])
1595 1536 continue;
1596
1597 87552 fragment = s->fragment_start[plane] + y * s->fragment_width[!!plane] + x;
1598
1599
2/2
✓ Branch 0 taken 24300 times.
✓ Branch 1 taken 63252 times.
87552 if (s->all_fragments[fragment].coding_method == MODE_COPY)
1600 24300 continue;
1601
1602
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 63252 times.
63252 if (vp4_unpack_vlcs(s, gb, tables[!!plane], plane, eob_tracker, fragment) < 0)
1603 return -1;
1604
1605 63252 dc_block_type = vp4_pred_block_type_map[s->all_fragments[fragment].coding_method];
1606
1607 63252 s->all_fragments[fragment].dc +=
1608 63252 vp4_dc_pred(s, this_dc_pred, last_dc, dc_block_type, plane);
1609
1610 63252 this_dc_pred->type = dc_block_type,
1611 63252 this_dc_pred->dc = last_dc[dc_block_type] = s->all_fragments[fragment].dc;
1612 }
1613 5568 vp4_dc_pred_after(s, dc_pred, sb_x);
1614 }
1615 }
1616 }
1617
1618 24 vp4_set_tokens_base(s);
1619
1620 24 return 0;
1621 }
1622 #endif
1623
1624 /*
1625 * This function reverses the DC prediction for each coded fragment in
1626 * the frame. Much of this function is adapted directly from the original
1627 * VP3 source code.
1628 */
1629 #define COMPATIBLE_FRAME(x) \
1630 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1631 #define DC_COEFF(u) s->all_fragments[u].dc
1632
1633 420 static void reverse_dc_prediction(Vp3DecodeContext *s,
1634 int first_fragment,
1635 int fragment_width,
1636 int fragment_height)
1637 {
1638 #define PUL 8
1639 #define PU 4
1640 #define PUR 2
1641 #define PL 1
1642
1643 420 int i = first_fragment;
1644
1645 int predicted_dc;
1646
1647 /* DC values for the left, up-left, up, and up-right fragments */
1648 int vl, vul, vu, vur;
1649
1650 /* indexes for the left, up-left, up, and up-right fragments */
1651 int l, ul, u, ur;
1652
1653 /*
1654 * The 6 fields mean:
1655 * 0: up-left multiplier
1656 * 1: up multiplier
1657 * 2: up-right multiplier
1658 * 3: left multiplier
1659 */
1660 static const int predictor_transform[16][4] = {
1661 { 0, 0, 0, 0 },
1662 { 0, 0, 0, 128 }, // PL
1663 { 0, 0, 128, 0 }, // PUR
1664 { 0, 0, 53, 75 }, // PUR|PL
1665 { 0, 128, 0, 0 }, // PU
1666 { 0, 64, 0, 64 }, // PU |PL
1667 { 0, 128, 0, 0 }, // PU |PUR
1668 { 0, 0, 53, 75 }, // PU |PUR|PL
1669 { 128, 0, 0, 0 }, // PUL
1670 { 0, 0, 0, 128 }, // PUL|PL
1671 { 64, 0, 64, 0 }, // PUL|PUR
1672 { 0, 0, 53, 75 }, // PUL|PUR|PL
1673 { 0, 128, 0, 0 }, // PUL|PU
1674 { -104, 116, 0, 116 }, // PUL|PU |PL
1675 { 24, 80, 24, 0 }, // PUL|PU |PUR
1676 { -104, 116, 0, 116 } // PUL|PU |PUR|PL
1677 };
1678
1679 /* This table shows which types of blocks can use other blocks for
1680 * prediction. For example, INTRA is the only mode in this table to
1681 * have a frame number of 0. That means INTRA blocks can only predict
1682 * from other INTRA blocks. There are 2 golden frame coding types;
1683 * blocks encoding in these modes can only predict from other blocks
1684 * that were encoded with these 1 of these 2 modes. */
1685 static const unsigned char compatible_frame[9] = {
1686 1, /* MODE_INTER_NO_MV */
1687 0, /* MODE_INTRA */
1688 1, /* MODE_INTER_PLUS_MV */
1689 1, /* MODE_INTER_LAST_MV */
1690 1, /* MODE_INTER_PRIOR_MV */
1691 2, /* MODE_USING_GOLDEN */
1692 2, /* MODE_GOLDEN_MV */
1693 1, /* MODE_INTER_FOUR_MV */
1694 3 /* MODE_COPY */
1695 };
1696 int current_frame_type;
1697
1698 /* there is a last DC predictor for each of the 3 frame types */
1699 short last_dc[3];
1700
1701 420 int transform = 0;
1702
1703 420 vul =
1704 420 vu =
1705 420 vur =
1706 420 vl = 0;
1707 420 last_dc[0] =
1708 420 last_dc[1] =
1709 420 last_dc[2] = 0;
1710
1711 /* for each fragment row... */
1712
2/2
✓ Branch 0 taken 13872 times.
✓ Branch 1 taken 420 times.
14292 for (int y = 0; y < fragment_height; y++) {
1713 /* for each fragment in a row... */
1714
2/2
✓ Branch 0 taken 1760640 times.
✓ Branch 1 taken 13872 times.
1774512 for (int x = 0; x < fragment_width; x++, i++) {
1715
1716 /* reverse prediction if this block was coded */
1717
2/2
✓ Branch 0 taken 532576 times.
✓ Branch 1 taken 1228064 times.
1760640 if (s->all_fragments[i].coding_method != MODE_COPY) {
1718 532576 current_frame_type =
1719 532576 compatible_frame[s->all_fragments[i].coding_method];
1720
1721 532576 transform = 0;
1722
2/2
✓ Branch 0 taken 528445 times.
✓ Branch 1 taken 4131 times.
532576 if (x) {
1723 528445 l = i - 1;
1724 528445 vl = DC_COEFF(l);
1725
2/2
✓ Branch 0 taken 483186 times.
✓ Branch 1 taken 45259 times.
528445 if (COMPATIBLE_FRAME(l))
1726 483186 transform |= PL;
1727 }
1728
2/2
✓ Branch 0 taken 513468 times.
✓ Branch 1 taken 19108 times.
532576 if (y) {
1729 513468 u = i - fragment_width;
1730 513468 vu = DC_COEFF(u);
1731
2/2
✓ Branch 0 taken 476069 times.
✓ Branch 1 taken 37399 times.
513468 if (COMPATIBLE_FRAME(u))
1732 476069 transform |= PU;
1733
2/2
✓ Branch 0 taken 509661 times.
✓ Branch 1 taken 3807 times.
513468 if (x) {
1734 509661 ul = i - fragment_width - 1;
1735 509661 vul = DC_COEFF(ul);
1736
2/2
✓ Branch 0 taken 461368 times.
✓ Branch 1 taken 48293 times.
509661 if (COMPATIBLE_FRAME(ul))
1737 461368 transform |= PUL;
1738 }
1739
2/2
✓ Branch 0 taken 505812 times.
✓ Branch 1 taken 7656 times.
513468 if (x + 1 < fragment_width) {
1740 505812 ur = i - fragment_width + 1;
1741 505812 vur = DC_COEFF(ur);
1742
2/2
✓ Branch 0 taken 459980 times.
✓ Branch 1 taken 45832 times.
505812 if (COMPATIBLE_FRAME(ur))
1743 459980 transform |= PUR;
1744 }
1745 }
1746
1747
2/2
✓ Branch 0 taken 10817 times.
✓ Branch 1 taken 521759 times.
532576 if (transform == 0) {
1748 /* if there were no fragments to predict from, use last
1749 * DC saved */
1750 10817 predicted_dc = last_dc[current_frame_type];
1751 } else {
1752 /* apply the appropriate predictor transform */
1753 521759 predicted_dc =
1754 521759 (predictor_transform[transform][0] * vul) +
1755 521759 (predictor_transform[transform][1] * vu) +
1756 521759 (predictor_transform[transform][2] * vur) +
1757 521759 (predictor_transform[transform][3] * vl);
1758
1759 521759 predicted_dc /= 128;
1760
1761 /* check for outranging on the [ul u l] and
1762 * [ul u ur l] predictors */
1763
4/4
✓ Branch 0 taken 107805 times.
✓ Branch 1 taken 413954 times.
✓ Branch 2 taken 20757 times.
✓ Branch 3 taken 87048 times.
521759 if ((transform == 15) || (transform == 13)) {
1764
2/2
✓ Branch 0 taken 2351 times.
✓ Branch 1 taken 432360 times.
434711 if (FFABS(predicted_dc - vu) > 128)
1765 2351 predicted_dc = vu;
1766
2/2
✓ Branch 0 taken 1111 times.
✓ Branch 1 taken 431249 times.
432360 else if (FFABS(predicted_dc - vl) > 128)
1767 1111 predicted_dc = vl;
1768
2/2
✓ Branch 0 taken 2253 times.
✓ Branch 1 taken 428996 times.
431249 else if (FFABS(predicted_dc - vul) > 128)
1769 2253 predicted_dc = vul;
1770 }
1771 }
1772
1773 /* at long last, apply the predictor */
1774 532576 DC_COEFF(i) += predicted_dc;
1775 /* save the DC */
1776 532576 last_dc[current_frame_type] = DC_COEFF(i);
1777 }
1778 }
1779 }
1780 420 }
1781
1782 2700 static void apply_loop_filter(Vp3DecodeContext *s, int plane,
1783 int ystart, int yend)
1784 {
1785 2700 int *bounding_values = s->bounding_values_array + 127;
1786
1787 2700 int width = s->fragment_width[!!plane];
1788 2700 int height = s->fragment_height[!!plane];
1789 2700 int fragment = s->fragment_start[plane] + ystart * width;
1790 2700 ptrdiff_t stride = s->current_frame.f->linesize[plane];
1791 2700 uint8_t *plane_data = s->current_frame.f->data[plane];
1792
1/2
✓ Branch 0 taken 2700 times.
✗ Branch 1 not taken.
2700 if (!s->flipped_image)
1793 2700 stride = -stride;
1794 2700 plane_data += s->data_offset[plane] + 8 * ystart * stride;
1795
1796
2/2
✓ Branch 0 taken 7830 times.
✓ Branch 1 taken 2700 times.
10530 for (int y = ystart; y < yend; y++) {
1797
2/2
✓ Branch 0 taken 478880 times.
✓ Branch 1 taken 7830 times.
486710 for (int x = 0; x < width; x++) {
1798 /* This code basically just deblocks on the edges of coded blocks.
1799 * However, it has to be much more complicated because of the
1800 * brain damaged deblock ordering used in VP3/Theora. Order matters
1801 * because some pixels get filtered twice. */
1802
2/2
✓ Branch 0 taken 367025 times.
✓ Branch 1 taken 111855 times.
478880 if (s->all_fragments[fragment].coding_method != MODE_COPY) {
1803 /* do not perform left edge filter for left columns frags */
1804
2/2
✓ Branch 0 taken 363601 times.
✓ Branch 1 taken 3424 times.
367025 if (x > 0) {
1805 363601 s->vp3dsp.h_loop_filter(
1806 363601 plane_data + 8 * x,
1807 stride, bounding_values);
1808 }
1809
1810 /* do not perform top edge filter for top row fragments */
1811
2/2
✓ Branch 0 taken 349625 times.
✓ Branch 1 taken 17400 times.
367025 if (y > 0) {
1812 349625 s->vp3dsp.v_loop_filter(
1813 349625 plane_data + 8 * x,
1814 stride, bounding_values);
1815 }
1816
1817 /* do not perform right edge filter for right column
1818 * fragments or if right fragment neighbor is also coded
1819 * in this frame (it will be filtered in next iteration) */
1820
2/2
✓ Branch 0 taken 359725 times.
✓ Branch 1 taken 7300 times.
367025 if ((x < width - 1) &&
1821
2/2
✓ Branch 0 taken 32268 times.
✓ Branch 1 taken 327457 times.
359725 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
1822 32268 s->vp3dsp.h_loop_filter(
1823 32268 plane_data + 8 * x + 8,
1824 stride, bounding_values);
1825 }
1826
1827 /* do not perform bottom edge filter for bottom row
1828 * fragments or if bottom fragment neighbor is also coded
1829 * in this frame (it will be filtered in the next row) */
1830
2/2
✓ Branch 0 taken 352272 times.
✓ Branch 1 taken 14753 times.
367025 if ((y < height - 1) &&
1831
2/2
✓ Branch 0 taken 32946 times.
✓ Branch 1 taken 319326 times.
352272 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
1832 32946 s->vp3dsp.v_loop_filter(
1833 32946 plane_data + 8 * x + 8 * stride,
1834 stride, bounding_values);
1835 }
1836 }
1837
1838 478880 fragment++;
1839 }
1840 7830 plane_data += 8 * stride;
1841 }
1842 2700 }
1843
1844 /**
1845 * Pull DCT tokens from the 64 levels to decode and dequant the coefficients
1846 * for the next block in coding order
1847 */
1848 595828 static inline int vp3_dequant(Vp3DecodeContext *s, const Vp3Fragment *frag,
1849 int plane, int inter, int16_t block[64])
1850 {
1851 595828 const int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
1852 595828 const uint8_t *perm = s->idct_scantable;
1853 595828 int i = 0;
1854
1855 do {
1856 3343039 int token = *s->dct_tokens[plane][i];
1857
3/4
✓ Branch 0 taken 587550 times.
✓ Branch 1 taken 913868 times.
✓ Branch 2 taken 1841621 times.
✗ Branch 3 not taken.
3343039 switch (token & 3) {
1858 587550 case 0: // EOB
1859
2/2
✓ Branch 0 taken 280242 times.
✓ Branch 1 taken 307308 times.
587550 if (--token < 4) // 0-3 are token types so the EOB run must now be 0
1860 280242 s->dct_tokens[plane][i]++;
1861 else
1862 307308 *s->dct_tokens[plane][i] = token & ~3;
1863 587550 goto end;
1864 913868 case 1: // zero run
1865 913868 s->dct_tokens[plane][i]++;
1866 913868 i += (token >> 2) & 0x7f;
1867
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 913868 times.
913868 if (i > 63) {
1868 av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
1869 return i;
1870 }
1871 913868 block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1872 913868 i++;
1873 913868 break;
1874 1841621 case 2: // coeff
1875 1841621 block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1876 1841621 s->dct_tokens[plane][i++]++;
1877 1841621 break;
1878 default: // shouldn't happen
1879 return i;
1880 }
1881
2/2
✓ Branch 0 taken 2747211 times.
✓ Branch 1 taken 8278 times.
2755489 } while (i < 64);
1882 // return value is expected to be a valid level
1883 8278 i--;
1884 595828 end:
1885 // the actual DC+prediction is in the fragment structure
1886 595828 block[0] = frag->dc * s->qmat[0][inter][plane][0];
1887 595828 return i;
1888 }
1889
1890 /**
1891 * called when all pixels up to row y are complete
1892 */
1893 1232 static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
1894 {
1895 int h, cy;
1896 int offset[AV_NUM_DATA_POINTERS];
1897
1898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1232 times.
1232 if (HAVE_THREADS && s->avctx->active_thread_type & FF_THREAD_FRAME) {
1899 int y_flipped = s->flipped_image ? s->height - y : y;
1900
1901 /* At the end of the frame, report INT_MAX instead of the height of
1902 * the frame. This makes the other threads' ff_thread_await_progress()
1903 * calls cheaper, because they don't have to clip their values. */
1904 ff_progress_frame_report(&s->current_frame,
1905 y_flipped == s->height ? INT_MAX
1906 : y_flipped - 1);
1907 }
1908
1909
1/2
✓ Branch 0 taken 1232 times.
✗ Branch 1 not taken.
1232 if (!s->avctx->draw_horiz_band)
1910 1232 return;
1911
1912 h = y - s->last_slice_end;
1913 s->last_slice_end = y;
1914 y -= h;
1915
1916 if (!s->flipped_image)
1917 y = s->height - y - h;
1918
1919 cy = y >> s->chroma_y_shift;
1920 offset[0] = s->current_frame.f->linesize[0] * y;
1921 offset[1] = s->current_frame.f->linesize[1] * cy;
1922 offset[2] = s->current_frame.f->linesize[2] * cy;
1923 for (int i = 3; i < AV_NUM_DATA_POINTERS; i++)
1924 offset[i] = 0;
1925
1926 emms_c();
1927 s->avctx->draw_horiz_band(s->avctx, s->current_frame.f, offset, y, 3, h);
1928 }
1929
1930 /**
1931 * Wait for the reference frame of the current fragment.
1932 * The progress value is in luma pixel rows.
1933 */
1934 static void await_reference_row(Vp3DecodeContext *s, const Vp3Fragment *fragment,
1935 int motion_y, int y)
1936 {
1937 const ProgressFrame *ref_frame;
1938 int ref_row;
1939 int border = motion_y & 1;
1940
1941 if (fragment->coding_method == MODE_USING_GOLDEN ||
1942 fragment->coding_method == MODE_GOLDEN_MV)
1943 ref_frame = &s->golden_frame;
1944 else
1945 ref_frame = &s->last_frame;
1946
1947 ref_row = y + (motion_y >> 1);
1948 ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
1949
1950 ff_progress_frame_await(ref_frame, ref_row);
1951 }
1952
1953 #if CONFIG_VP4_DECODER
1954 /**
1955 * @return non-zero if temp (edge_emu_buffer) was populated
1956 */
1957 36568 static int vp4_mc_loop_filter(Vp3DecodeContext *s, int plane, int motion_x, int motion_y, int bx, int by,
1958 const uint8_t *motion_source, ptrdiff_t stride,
1959 int src_x, int src_y, uint8_t *temp)
1960 {
1961
2/2
✓ Branch 0 taken 11931 times.
✓ Branch 1 taken 24637 times.
36568 int motion_shift = plane ? 4 : 2;
1962
2/2
✓ Branch 0 taken 11931 times.
✓ Branch 1 taken 24637 times.
36568 int subpel_mask = plane ? 3 : 1;
1963 36568 int *bounding_values = s->bounding_values_array + 127;
1964
1965 int x, y;
1966 int x2, y2;
1967 int x_subpel, y_subpel;
1968 int x_offset, y_offset;
1969
1970
2/2
✓ Branch 0 taken 11931 times.
✓ Branch 1 taken 24637 times.
36568 int block_width = plane ? 8 : 16;
1971
3/4
✓ Branch 0 taken 11931 times.
✓ Branch 1 taken 24637 times.
✓ Branch 2 taken 11931 times.
✗ Branch 3 not taken.
36568 int plane_width = s->width >> (plane && s->chroma_x_shift);
1972
3/4
✓ Branch 0 taken 11931 times.
✓ Branch 1 taken 24637 times.
✓ Branch 2 taken 11931 times.
✗ Branch 3 not taken.
36568 int plane_height = s->height >> (plane && s->chroma_y_shift);
1973
1974 #define loop_stride 12
1975 uint8_t loop[12 * loop_stride];
1976
1977 /* using division instead of shift to correctly handle negative values */
1978 36568 x = 8 * bx + motion_x / motion_shift;
1979 36568 y = 8 * by + motion_y / motion_shift;
1980
1981 36568 x_subpel = motion_x & subpel_mask;
1982 36568 y_subpel = motion_y & subpel_mask;
1983
1984
4/4
✓ Branch 0 taken 13794 times.
✓ Branch 1 taken 22774 times.
✓ Branch 2 taken 7289 times.
✓ Branch 3 taken 6505 times.
36568 if (x_subpel || y_subpel) {
1985 30063 x--;
1986 30063 y--;
1987
1988
2/2
✓ Branch 0 taken 22774 times.
✓ Branch 1 taken 7289 times.
30063 if (x_subpel)
1989
3/4
✓ Branch 0 taken 7016 times.
✓ Branch 1 taken 15758 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7016 times.
22774 x = FFMIN(x, x + FFSIGN(motion_x));
1990
1991
2/2
✓ Branch 0 taken 15343 times.
✓ Branch 1 taken 14720 times.
30063 if (y_subpel)
1992
3/4
✓ Branch 0 taken 4104 times.
✓ Branch 1 taken 11239 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4104 times.
15343 y = FFMIN(y, y + FFSIGN(motion_y));
1993
1994 30063 x2 = x + block_width;
1995 30063 y2 = y + block_width;
1996
1997
6/8
✓ Branch 0 taken 30063 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29757 times.
✓ Branch 3 taken 306 times.
✓ Branch 4 taken 29757 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 290 times.
✓ Branch 7 taken 29467 times.
30063 if (x2 < 0 || x2 >= plane_width || y2 < 0 || y2 >= plane_height)
1998 596 return 0;
1999
2000 29467 x_offset = (-(x + 2) & 7) + 2;
2001 29467 y_offset = (-(y + 2) & 7) + 2;
2002
2003 av_assert1(!(x_offset > 8 + x_subpel && y_offset > 8 + y_subpel));
2004
2005 29467 s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1,
2006 loop_stride, stride,
2007 12, 12, src_x - 1, src_y - 1,
2008 plane_width,
2009 plane_height);
2010
2011
2/2
✓ Branch 0 taken 25000 times.
✓ Branch 1 taken 4467 times.
29467 if (x_offset <= 8 + x_subpel)
2012 25000 ff_vp3dsp_h_loop_filter_12(loop + x_offset, loop_stride, bounding_values);
2013
2014
2/2
✓ Branch 0 taken 17032 times.
✓ Branch 1 taken 12435 times.
29467 if (y_offset <= 8 + y_subpel)
2015 17032 ff_vp3dsp_v_loop_filter_12(loop + y_offset*loop_stride, loop_stride, bounding_values);
2016
2017 } else {
2018
2019 6505 x_offset = -x & 7;
2020 6505 y_offset = -y & 7;
2021
2022
4/4
✓ Branch 0 taken 834 times.
✓ Branch 1 taken 5671 times.
✓ Branch 2 taken 118 times.
✓ Branch 3 taken 716 times.
6505 if (!x_offset && !y_offset)
2023 118 return 0;
2024
2025 6387 s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1,
2026 loop_stride, stride,
2027 12, 12, src_x - 1, src_y - 1,
2028 plane_width,
2029 plane_height);
2030
2031 #define safe_loop_filter(name, ptr, stride, bounding_values) \
2032 if (VP3_LOOP_FILTER_NO_UNALIGNED_SUPPORT && (uintptr_t)(ptr) & 7) \
2033 s->vp3dsp.name##_unaligned(ptr, stride, bounding_values); \
2034 else \
2035 s->vp3dsp.name(ptr, stride, bounding_values);
2036
2037
2/2
✓ Branch 0 taken 5671 times.
✓ Branch 1 taken 716 times.
6387 if (x_offset)
2038 5671 safe_loop_filter(h_loop_filter, loop + loop_stride + x_offset + 1, loop_stride, bounding_values);
2039
2040
2/2
✓ Branch 0 taken 3055 times.
✓ Branch 1 taken 3332 times.
6387 if (y_offset)
2041 3055 safe_loop_filter(v_loop_filter, loop + (y_offset + 1)*loop_stride + 1, loop_stride, bounding_values);
2042 }
2043
2044
2/2
✓ Branch 0 taken 322686 times.
✓ Branch 1 taken 35854 times.
358540 for (int i = 0; i < 9; i++)
2045 322686 memcpy(temp + i*stride, loop + (i + 1) * loop_stride + 1, 9);
2046
2047 35854 return 1;
2048 }
2049 #endif
2050
2051 /*
2052 * Perform the final rendering for a particular slice of data.
2053 * The slice number ranges from 0..(c_superblock_height - 1).
2054 */
2055 1068 static void render_slice(Vp3DecodeContext *s, int slice)
2056 {
2057 1068 int16_t *block = s->block;
2058 1068 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
2059 /* When decoding keyframes, the earlier frames may not be available,
2060 * so we just use the current frame in this case instead;
2061 * it also avoid using undefined pointer arithmetic. Nothing is
2062 * ever read from these frames in case of a keyframe. */
2063
2/2
✓ Branch 0 taken 1012 times.
✓ Branch 1 taken 56 times.
1068 const AVFrame *last_frame = s->last_frame.f ?
2064 s->last_frame.f : s->current_frame.f;
2065
1/2
✓ Branch 0 taken 1068 times.
✗ Branch 1 not taken.
1068 const AVFrame *golden_frame = s->golden_frame.f ?
2066 s->golden_frame.f : s->current_frame.f;
2067 int motion_halfpel_index;
2068 int first_pixel;
2069
2070
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1068 times.
1068 if (slice >= s->c_superblock_height)
2071 return;
2072
2073
2/2
✓ Branch 0 taken 3204 times.
✓ Branch 1 taken 1068 times.
4272 for (int plane = 0; plane < 3; plane++) {
2074 3204 uint8_t *output_plane = s->current_frame.f->data[plane] +
2075 3204 s->data_offset[plane];
2076 3204 const uint8_t *last_plane = last_frame->data[plane] +
2077 3204 s->data_offset[plane];
2078 3204 const uint8_t *golden_plane = golden_frame->data[plane] +
2079 3204 s->data_offset[plane];
2080 3204 ptrdiff_t stride = s->current_frame.f->linesize[plane];
2081
3/4
✓ Branch 0 taken 2136 times.
✓ Branch 1 taken 1068 times.
✓ Branch 2 taken 2136 times.
✗ Branch 3 not taken.
3204 int plane_width = s->width >> (plane && s->chroma_x_shift);
2082
3/4
✓ Branch 0 taken 2136 times.
✓ Branch 1 taken 1068 times.
✓ Branch 2 taken 2136 times.
✗ Branch 3 not taken.
3204 int plane_height = s->height >> (plane && s->chroma_y_shift);
2083 3204 const int8_t (*motion_val)[2] = s->motion_val[!!plane];
2084
2085
3/4
✓ Branch 0 taken 1068 times.
✓ Branch 1 taken 2136 times.
✓ Branch 2 taken 1068 times.
✗ Branch 3 not taken.
3204 int sb_y = slice << (!plane && s->chroma_y_shift);
2086
3/4
✓ Branch 0 taken 1068 times.
✓ Branch 1 taken 2136 times.
✓ Branch 2 taken 1068 times.
✗ Branch 3 not taken.
3204 int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
2087 3204 int slice_width = plane ? s->c_superblock_width
2088
2/2
✓ Branch 0 taken 2136 times.
✓ Branch 1 taken 1068 times.
3204 : s->y_superblock_width;
2089
2090 3204 int fragment_width = s->fragment_width[!!plane];
2091 3204 int fragment_height = s->fragment_height[!!plane];
2092 3204 int fragment_start = s->fragment_start[plane];
2093
2094
2/2
✓ Branch 0 taken 1068 times.
✓ Branch 1 taken 2136 times.
4272 int do_await = !plane && HAVE_THREADS &&
2095
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1068 times.
1068 (s->avctx->active_thread_type & FF_THREAD_FRAME);
2096
2097
1/2
✓ Branch 0 taken 3204 times.
✗ Branch 1 not taken.
3204 if (!s->flipped_image)
2098 3204 stride = -stride;
2099 if (CONFIG_GRAY && plane && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2100 continue;
2101
2102 /* for each superblock row in the slice (both of them)... */
2103
2/2
✓ Branch 0 taken 4272 times.
✓ Branch 1 taken 3204 times.
7476 for (; sb_y < slice_height; sb_y++) {
2104 /* for each superblock in a row... */
2105
2/2
✓ Branch 0 taken 125288 times.
✓ Branch 1 taken 4272 times.
129560 for (int sb_x = 0; sb_x < slice_width; sb_x++) {
2106 /* for each block in a superblock... */
2107
2/2
✓ Branch 0 taken 2004608 times.
✓ Branch 1 taken 125288 times.
2129896 for (int j = 0; j < 16; j++) {
2108 2004608 int x = 4 * sb_x + hilbert_offset[j][0];
2109 2004608 int y = 4 * sb_y + hilbert_offset[j][1];
2110 2004608 int fragment = y * fragment_width + x;
2111
2112 2004608 int i = fragment_start + fragment;
2113
2114 // bounds check
2115
4/4
✓ Branch 0 taken 1992192 times.
✓ Branch 1 taken 12416 times.
✓ Branch 2 taken 144000 times.
✓ Branch 3 taken 1848192 times.
2004608 if (x >= fragment_width || y >= fragment_height)
2116 156416 continue;
2117
2118 1848192 first_pixel = 8 * y * stride + 8 * x;
2119
2120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1848192 times.
1848192 if (do_await &&
2121 s->all_fragments[i].coding_method != MODE_INTRA)
2122 await_reference_row(s, &s->all_fragments[i],
2123 motion_val[fragment][1],
2124 (16 * y) >> s->chroma_y_shift);
2125
2126 /* transform if this block was coded */
2127
2/2
✓ Branch 0 taken 595828 times.
✓ Branch 1 taken 1252364 times.
1848192 if (s->all_fragments[i].coding_method != MODE_COPY) {
2128 const uint8_t *motion_source;
2129
2/2
✓ Branch 0 taken 592182 times.
✓ Branch 1 taken 3646 times.
595828 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
2130
2/2
✓ Branch 0 taken 4039 times.
✓ Branch 1 taken 588143 times.
592182 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
2131 7685 motion_source = golden_plane;
2132 else
2133 588143 motion_source = last_plane;
2134
2135 595828 motion_source += first_pixel;
2136 595828 motion_halfpel_index = 0;
2137
2138 /* sort out the motion vector if this fragment is coded
2139 * using a motion vector method */
2140
2/2
✓ Branch 0 taken 377836 times.
✓ Branch 1 taken 217992 times.
595828 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
2141
2/2
✓ Branch 0 taken 374190 times.
✓ Branch 1 taken 3646 times.
377836 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
2142 int src_x, src_y;
2143 374190 int standard_mc = 1;
2144 374190 motion_x = motion_val[fragment][0];
2145 374190 motion_y = motion_val[fragment][1];
2146 #if CONFIG_VP4_DECODER
2147
4/4
✓ Branch 0 taken 105580 times.
✓ Branch 1 taken 268610 times.
✓ Branch 2 taken 11931 times.
✓ Branch 3 taken 93649 times.
374190 if (plane && s->version >= 2) {
2148 11931 motion_x = (motion_x >> 1) | (motion_x & 1);
2149 11931 motion_y = (motion_y >> 1) | (motion_y & 1);
2150 }
2151 #endif
2152
2153 374190 src_x = (motion_x >> 1) + 8 * x;
2154 374190 src_y = (motion_y >> 1) + 8 * y;
2155
2156 374190 motion_halfpel_index = motion_x & 0x01;
2157 374190 motion_source += (motion_x >> 1);
2158
2159 374190 motion_halfpel_index |= (motion_y & 0x01) << 1;
2160 374190 motion_source += ((motion_y >> 1) * stride);
2161
2162 #if CONFIG_VP4_DECODER
2163
2/2
✓ Branch 0 taken 36568 times.
✓ Branch 1 taken 337622 times.
374190 if (s->version >= 2) {
2164 36568 uint8_t *temp = s->edge_emu_buffer;
2165
1/2
✓ Branch 0 taken 36568 times.
✗ Branch 1 not taken.
36568 if (stride < 0)
2166 36568 temp -= 8 * stride;
2167
2/2
✓ Branch 1 taken 35854 times.
✓ Branch 2 taken 714 times.
36568 if (vp4_mc_loop_filter(s, plane, motion_val[fragment][0], motion_val[fragment][1], x, y, motion_source, stride, src_x, src_y, temp)) {
2168 35854 motion_source = temp;
2169 35854 standard_mc = 0;
2170 }
2171 }
2172 #endif
2173
2174
4/4
✓ Branch 0 taken 338336 times.
✓ Branch 1 taken 35854 times.
✓ Branch 2 taken 336975 times.
✓ Branch 3 taken 1361 times.
374190 if (standard_mc && (
2175
2/2
✓ Branch 0 taken 322220 times.
✓ Branch 1 taken 14755 times.
336975 src_x < 0 || src_y < 0 ||
2176
2/2
✓ Branch 0 taken 315161 times.
✓ Branch 1 taken 7059 times.
322220 src_x + 9 >= plane_width ||
2177
2/2
✓ Branch 0 taken 11611 times.
✓ Branch 1 taken 303550 times.
315161 src_y + 9 >= plane_height)) {
2178 34786 uint8_t *temp = s->edge_emu_buffer;
2179
1/2
✓ Branch 0 taken 34786 times.
✗ Branch 1 not taken.
34786 if (stride < 0)
2180 34786 temp -= 8 * stride;
2181
2182 34786 s->vdsp.emulated_edge_mc(temp, motion_source,
2183 stride, stride,
2184 9, 9, src_x, src_y,
2185 plane_width,
2186 plane_height);
2187 34786 motion_source = temp;
2188 }
2189 }
2190
2191 /* first, take care of copying a block from either the
2192 * previous or the golden frame */
2193
2/2
✓ Branch 0 taken 411494 times.
✓ Branch 1 taken 184334 times.
595828 if (s->all_fragments[i].coding_method != MODE_INTRA) {
2194 /* Note, it is possible to implement all MC cases
2195 * with put_no_rnd_pixels_l2 which would look more
2196 * like the VP3 source but this would be slower as
2197 * put_no_rnd_pixels_tab is better optimized */
2198
2/2
✓ Branch 0 taken 293916 times.
✓ Branch 1 taken 117578 times.
411494 if (motion_halfpel_index != 3) {
2199 293916 s->hdsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
2200 output_plane + first_pixel,
2201 motion_source, stride, 8);
2202 } else {
2203 /* d is 0 if motion_x and _y have the same sign,
2204 * else -1 */
2205 117578 int d = (motion_x ^ motion_y) >> 31;
2206 117578 s->vp3dsp.put_no_rnd_pixels_l2(output_plane + first_pixel,
2207 117578 motion_source - d,
2208 117578 motion_source + stride + 1 + d,
2209 stride, 8);
2210 }
2211 }
2212
2213 /* invert DCT and place (or add) in final output */
2214
2215
2/2
✓ Branch 0 taken 184334 times.
✓ Branch 1 taken 411494 times.
595828 if (s->all_fragments[i].coding_method == MODE_INTRA) {
2216 184334 vp3_dequant(s, s->all_fragments + i,
2217 plane, 0, block);
2218 184334 s->vp3dsp.idct_put(output_plane + first_pixel,
2219 stride,
2220 block);
2221 } else {
2222
2/2
✓ Branch 1 taken 284346 times.
✓ Branch 2 taken 127148 times.
411494 if (vp3_dequant(s, s->all_fragments + i,
2223 plane, 1, block)) {
2224 284346 s->vp3dsp.idct_add(output_plane + first_pixel,
2225 stride,
2226 block);
2227 } else {
2228 127148 s->vp3dsp.idct_dc_add(output_plane + first_pixel,
2229 stride, block);
2230 }
2231 }
2232 } else {
2233 /* copy directly from the previous frame */
2234 1252364 s->hdsp.put_pixels_tab[1][0](
2235 output_plane + first_pixel,
2236 last_plane + first_pixel,
2237 stride, 8);
2238 }
2239 }
2240 }
2241
2242 // Filter up to the last row in the superblock row
2243
4/4
✓ Branch 0 taken 3888 times.
✓ Branch 1 taken 384 times.
✓ Branch 2 taken 2280 times.
✓ Branch 3 taken 1608 times.
4272 if (s->version < 2 && !s->skip_loop_filter)
2244 2280 apply_loop_filter(s, plane, 4 * sb_y - !!sb_y,
2245
2/2
✓ Branch 0 taken 456 times.
✓ Branch 1 taken 1824 times.
2280 FFMIN(4 * sb_y + 3, fragment_height - 1));
2246 }
2247 }
2248
2249 /* this looks like a good place for slice dispatch... */
2250 /* algorithm:
2251 * if (slice == s->macroblock_height - 1)
2252 * dispatch (both last slice & 2nd-to-last slice);
2253 * else if (slice > 0)
2254 * dispatch (slice - 1);
2255 */
2256
2257
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 928 times.
1068 vp3_draw_horiz_band(s, FFMIN((32 << s->chroma_y_shift) * (slice + 1) - 16,
2258 s->height - 16));
2259 }
2260
2261 11 static av_cold void init_tables_once(void)
2262 {
2263 11 VLCInitState state = VLC_INIT_STATE(mode_code_vlc);
2264
2265 11 VLC_INIT_STATIC_TABLE_FROM_LENGTHS(superblock_run_length_vlc,
2266 SUPERBLOCK_VLC_BITS, 34,
2267 superblock_run_length_vlc_lens, 1,
2268 NULL, 0, 0, 1, 0);
2269
2270 11 VLC_INIT_STATIC_TABLE_FROM_LENGTHS(fragment_run_length_vlc, 5, 30,
2271 fragment_run_length_vlc_len, 1,
2272 NULL, 0, 0, 0, 0);
2273
2274 11 VLC_INIT_STATIC_TABLE_FROM_LENGTHS(motion_vector_vlc, VP3_MV_VLC_BITS, 63,
2275 &motion_vector_vlc_table[0][1], 2,
2276 &motion_vector_vlc_table[0][0], 2, 1,
2277 -31, 0);
2278
2279 11 ff_vlc_init_tables_from_lengths(&state, 4, 8,
2280 mode_code_vlc_len, 1,
2281 NULL, 0, 0, 0, 0);
2282
2283 #if CONFIG_VP4_DECODER
2284
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 11 times.
33 for (int j = 0; j < 2; j++)
2285
2/2
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 22 times.
176 for (int i = 0; i < 7; i++) {
2286 154 vp4_mv_vlc_table[j][i] =
2287 154 ff_vlc_init_tables_from_lengths(&state, VP4_MV_VLC_BITS, 63,
2288 154 &vp4_mv_vlc[j][i][0][1], 2,
2289 154 &vp4_mv_vlc[j][i][0][0], 2, 1,
2290 -31, 0);
2291 }
2292
2293 /* version >= 2 */
2294
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 11 times.
33 for (int i = 0; i < 2; i++) {
2295 22 block_pattern_vlc[i] =
2296 22 ff_vlc_init_tables(&state, 5, 14,
2297 22 &vp4_block_pattern_vlc[i][0][1], 2, 1,
2298 22 &vp4_block_pattern_vlc[i][0][0], 2, 1, 0);
2299 }
2300 #endif
2301 11 }
2302
2303 /// Allocate tables for per-frame data in Vp3DecodeContext
2304 35 static av_cold int allocate_tables(AVCodecContext *avctx)
2305 {
2306 35 Vp3DecodeContext *s = avctx->priv_data;
2307 int y_fragment_count, c_fragment_count;
2308
2309 35 free_tables(avctx);
2310
2311 35 y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
2312 35 c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
2313
2314 /* superblock_coding is used by unpack_superblocks (VP3/Theora) and vp4_unpack_macroblocks (VP4) */
2315 35 s->superblock_coding = av_mallocz(FFMAX(s->superblock_count, s->yuv_macroblock_count));
2316 35 s->all_fragments = av_calloc(s->fragment_count, sizeof(*s->all_fragments));
2317
2318 35 s-> kf_coded_fragment_list = av_calloc(s->fragment_count, sizeof(int));
2319 35 s->nkf_coded_fragment_list = av_calloc(s->fragment_count, sizeof(int));
2320 35 memset(s-> num_kf_coded_fragment, -1, sizeof(s-> num_kf_coded_fragment));
2321
2322 35 s->dct_tokens_base = av_calloc(s->fragment_count,
2323 64 * sizeof(*s->dct_tokens_base));
2324 35 s->motion_val[0] = av_calloc(y_fragment_count, sizeof(*s->motion_val[0]));
2325 35 s->motion_val[1] = av_calloc(c_fragment_count, sizeof(*s->motion_val[1]));
2326
2327 /* work out the block mapping tables */
2328 35 s->superblock_fragments = av_calloc(s->superblock_count, 16 * sizeof(int));
2329 35 s->macroblock_coding = av_mallocz(s->macroblock_count + 1);
2330
2331 35 s->dc_pred_row = av_malloc_array(s->y_superblock_width * 4, sizeof(*s->dc_pred_row));
2332
2333
2/4
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
35 if (!s->superblock_coding || !s->all_fragments ||
2334
2/4
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
35 !s->dct_tokens_base || !s->kf_coded_fragment_list ||
2335
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 !s->nkf_coded_fragment_list ||
2336
2/4
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
35 !s->superblock_fragments || !s->macroblock_coding ||
2337
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 !s->dc_pred_row ||
2338
2/4
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 35 times.
35 !s->motion_val[0] || !s->motion_val[1]) {
2339 return -1;
2340 }
2341
2342 35 init_block_mapping(s);
2343
2344 35 return 0;
2345 }
2346
2347
2348 35 static av_cold void free_vlc_tables(AVRefStructOpaque unused, void *obj)
2349 {
2350 35 CoeffVLCs *vlcs = obj;
2351
2352
2/2
✓ Branch 0 taken 2800 times.
✓ Branch 1 taken 35 times.
2835 for (int i = 0; i < FF_ARRAY_ELEMS(vlcs->vlcs); i++)
2353 2800 ff_vlc_free(&vlcs->vlcs[i]);
2354 35 }
2355
2356 35 static av_cold int vp3_decode_init(AVCodecContext *avctx)
2357 {
2358 static AVOnce init_static_once = AV_ONCE_INIT;
2359 35 Vp3DecodeContext *s = avctx->priv_data;
2360 int ret;
2361 int c_width;
2362 int c_height;
2363 int y_fragment_count, c_fragment_count;
2364
2365
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 33 times.
35 if (avctx->codec_tag == MKTAG('V', 'P', '4', '0')) {
2366 2 s->version = 3;
2367 #if !CONFIG_VP4_DECODER
2368 av_log(avctx, AV_LOG_ERROR, "This build does not support decoding VP4.\n");
2369 return AVERROR_DECODER_NOT_FOUND;
2370 #endif
2371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 } else if (avctx->codec_tag == MKTAG('V', 'P', '3', '0'))
2372 s->version = 0;
2373 else
2374 33 s->version = 1;
2375
2376 35 s->avctx = avctx;
2377 35 s->width = FFALIGN(avctx->coded_width, 16);
2378 35 s->height = FFALIGN(avctx->coded_height, 16);
2379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (s->width < 18)
2380 return AVERROR_PATCHWELCOME;
2381
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 30 times.
35 if (avctx->codec_id != AV_CODEC_ID_THEORA)
2382 5 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2383 35 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
2384 35 ff_hpeldsp_init(&s->hdsp, avctx->flags | AV_CODEC_FLAG_BITEXACT);
2385 35 ff_videodsp_init(&s->vdsp, 8);
2386 35 ff_vp3dsp_init(&s->vp3dsp);
2387
2388
2/2
✓ Branch 0 taken 2240 times.
✓ Branch 1 taken 35 times.
2275 for (int i = 0; i < 64; i++) {
2389 #define TRANSPOSE(x) (((x) >> 3) | (((x) & 7) << 3))
2390 2240 s->idct_permutation[i] = TRANSPOSE(i);
2391 2240 s->idct_scantable[i] = TRANSPOSE(ff_zigzag_direct[i]);
2392 #undef TRANSPOSE
2393 }
2394
2395 /* initialize to an impossible value which will force a recalculation
2396 * in the first frame decode */
2397
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 35 times.
140 for (int i = 0; i < 3; i++)
2398 105 s->qps[i] = -1;
2399
2400 35 ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
2401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (ret)
2402 return ret;
2403
2404 35 s->y_superblock_width = (s->width + 31) / 32;
2405 35 s->y_superblock_height = (s->height + 31) / 32;
2406 35 s->y_superblock_count = s->y_superblock_width * s->y_superblock_height;
2407
2408 /* work out the dimensions for the C planes */
2409 35 c_width = s->width >> s->chroma_x_shift;
2410 35 c_height = s->height >> s->chroma_y_shift;
2411 35 s->c_superblock_width = (c_width + 31) / 32;
2412 35 s->c_superblock_height = (c_height + 31) / 32;
2413 35 s->c_superblock_count = s->c_superblock_width * s->c_superblock_height;
2414
2415 35 s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2);
2416 35 s->u_superblock_start = s->y_superblock_count;
2417 35 s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
2418
2419 35 s->macroblock_width = (s->width + 15) / 16;
2420 35 s->macroblock_height = (s->height + 15) / 16;
2421 35 s->macroblock_count = s->macroblock_width * s->macroblock_height;
2422 35 s->c_macroblock_width = (c_width + 15) / 16;
2423 35 s->c_macroblock_height = (c_height + 15) / 16;
2424 35 s->c_macroblock_count = s->c_macroblock_width * s->c_macroblock_height;
2425 35 s->yuv_macroblock_count = s->macroblock_count + 2 * s->c_macroblock_count;
2426
2427 35 s->fragment_width[0] = s->width / FRAGMENT_PIXELS;
2428 35 s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
2429 35 s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift;
2430 35 s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift;
2431
2432 /* fragment count covers all 8x8 blocks for all 3 planes */
2433 35 y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
2434 35 c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
2435 35 s->fragment_count = y_fragment_count + 2 * c_fragment_count;
2436 35 s->fragment_start[1] = y_fragment_count;
2437 35 s->fragment_start[2] = y_fragment_count + c_fragment_count;
2438
2439
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 30 times.
35 if (!s->theora_tables) {
2440
2/2
✓ Branch 0 taken 320 times.
✓ Branch 1 taken 5 times.
325 for (int i = 0; i < 64; i++) {
2441
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 128 times.
320 s->coded_dc_scale_factor[0][i] = s->version < 2 ? vp31_dc_scale_factor[i] : vp4_y_dc_scale_factor[i];
2442
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 128 times.
320 s->coded_dc_scale_factor[1][i] = s->version < 2 ? vp31_dc_scale_factor[i] : vp4_uv_dc_scale_factor[i];
2443
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 128 times.
320 s->coded_ac_scale_factor[i] = s->version < 2 ? vp31_ac_scale_factor[i] : vp4_ac_scale_factor[i];
2444
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 128 times.
320 s->base_matrix[0][i] = s->version < 2 ? vp31_intra_y_dequant[i] : vp4_generic_dequant[i];
2445
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 128 times.
320 s->base_matrix[1][i] = s->version < 2 ? ff_mjpeg_std_chrominance_quant_tbl[i] : vp4_generic_dequant[i];
2446
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 128 times.
320 s->base_matrix[2][i] = s->version < 2 ? vp31_inter_dequant[i] : vp4_generic_dequant[i];
2447
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 128 times.
320 s->filter_limit_values[i] = s->version < 2 ? vp31_filter_limit_values[i] : vp4_filter_limit_values[i];
2448 }
2449
2450
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
15 for (int inter = 0; inter < 2; inter++) {
2451
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
40 for (int plane = 0; plane < 3; plane++) {
2452 30 s->qr_count[inter][plane] = 1;
2453 30 s->qr_size[inter][plane][0] = 63;
2454 30 s->qr_base[inter][plane][0] =
2455 30 s->qr_base[inter][plane][1] = 2 * inter + (!!plane) * !inter;
2456 }
2457 }
2458 }
2459
2460
1/2
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
35 if (ff_thread_sync_ref(avctx, offsetof(Vp3DecodeContext, coeff_vlc)) != FF_THREAD_IS_COPY) {
2461 35 CoeffVLCs *vlcs = av_refstruct_alloc_ext(sizeof(*s->coeff_vlc), 0,
2462 NULL, free_vlc_tables);
2463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (!vlcs)
2464 return AVERROR(ENOMEM);
2465
2466 35 s->coeff_vlc = vlcs;
2467
2468
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 30 times.
35 if (!s->theora_tables) {
2469 const uint8_t (*bias_tabs)[32][2];
2470
2471 /* init VLC tables */
2472
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 bias_tabs = CONFIG_VP4_DECODER && s->version >= 2 ? vp4_bias : vp3_bias;
2473
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 5 times.
405 for (int i = 0; i < FF_ARRAY_ELEMS(vlcs->vlcs); i++) {
2474 400 ret = ff_vlc_init_from_lengths(&vlcs->vlcs[i], 11, 32,
2475 400 &bias_tabs[i][0][1], 2,
2476 400 &bias_tabs[i][0][0], 2, 1,
2477 0, 0, avctx);
2478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 if (ret < 0)
2479 return ret;
2480 400 vlcs->vlc_tabs[i] = vlcs->vlcs[i].table;
2481 }
2482 } else {
2483
2/2
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 30 times.
2430 for (int i = 0; i < FF_ARRAY_ELEMS(vlcs->vlcs); i++) {
2484 2400 const HuffTable *tab = &s->huffman_table[i];
2485
2486 2400 ret = ff_vlc_init_from_lengths(&vlcs->vlcs[i], 11, tab->nb_entries,
2487 2400 &tab->entries[0].len, sizeof(*tab->entries),
2488 2400 &tab->entries[0].sym, sizeof(*tab->entries), 1,
2489 0, 0, avctx);
2490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2400 times.
2400 if (ret < 0)
2491 return ret;
2492 2400 vlcs->vlc_tabs[i] = vlcs->vlcs[i].table;
2493 }
2494 }
2495 }
2496
2497 35 ff_thread_once(&init_static_once, init_tables_once);
2498
2499 35 return allocate_tables(avctx);
2500 }
2501
2502 #if HAVE_THREADS
2503 static void ref_frames(Vp3DecodeContext *dst, const Vp3DecodeContext *src)
2504 {
2505 ff_progress_frame_replace(&dst->current_frame, &src->current_frame);
2506 ff_progress_frame_replace(&dst->golden_frame, &src->golden_frame);
2507 }
2508
2509 static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
2510 {
2511 Vp3DecodeContext *s = dst->priv_data;
2512 const Vp3DecodeContext *s1 = src->priv_data;
2513 int qps_changed = 0;
2514
2515 // copy previous frame data
2516 ref_frames(s, s1);
2517
2518 if (s != s1) {
2519 // copy qscale data if necessary
2520 for (int i = 0; i < 3; i++) {
2521 if (s->qps[i] != s1->qps[1]) {
2522 qps_changed = 1;
2523 memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i]));
2524 }
2525 }
2526
2527 if (s->qps[0] != s1->qps[0])
2528 memcpy(&s->bounding_values_array, &s1->bounding_values_array,
2529 sizeof(s->bounding_values_array));
2530
2531 if (qps_changed) {
2532 memcpy(s->qps, s1->qps, sizeof(s->qps));
2533 s->nqps = s1->nqps;
2534 }
2535 }
2536 return 0;
2537 }
2538 #endif
2539
2540 164 static int vp3_decode_frame(AVCodecContext *avctx, AVFrame *frame,
2541 int *got_frame, AVPacket *avpkt)
2542 {
2543 164 const uint8_t *buf = avpkt->data;
2544 164 int buf_size = avpkt->size;
2545 164 Vp3DecodeContext *s = avctx->priv_data;
2546 GetBitContext gb;
2547 int ret;
2548
2549
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 164 times.
164 if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
2550 return ret;
2551
2552 #if CONFIG_THEORA_DECODER
2553
3/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 138 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 26 times.
164 if (s->theora && get_bits1(&gb)) {
2554 int type = get_bits(&gb, 7);
2555 skip_bits_long(&gb, 6*8); /* "theora" */
2556
2557 if (s->avctx->active_thread_type&FF_THREAD_FRAME) {
2558 av_log(avctx, AV_LOG_ERROR, "midstream reconfiguration with multithreading is unsupported, try -threads 1\n");
2559 return AVERROR_PATCHWELCOME;
2560 }
2561 if (type == 0) {
2562 vp3_decode_end(avctx);
2563 ret = theora_decode_header(avctx, &gb);
2564
2565 if (ret >= 0)
2566 ret = vp3_decode_init(avctx);
2567 if (ret < 0) {
2568 vp3_decode_end(avctx);
2569 return ret;
2570 }
2571 return buf_size;
2572 } else if (type == 2) {
2573 vp3_decode_end(avctx);
2574 ret = theora_decode_tables(avctx, &gb);
2575 if (ret >= 0)
2576 ret = vp3_decode_init(avctx);
2577 if (ret < 0) {
2578 vp3_decode_end(avctx);
2579 return ret;
2580 }
2581 return buf_size;
2582 }
2583
2584 av_log(avctx, AV_LOG_ERROR,
2585 "Header packet passed to frame decoder, skipping\n");
2586 return -1;
2587 }
2588 #endif
2589
2590 164 s->keyframe = !get_bits1(&gb);
2591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 164 times.
164 if (!s->all_fragments) {
2592 av_log(avctx, AV_LOG_ERROR, "Data packet without prior valid headers\n");
2593 return -1;
2594 }
2595
2/2
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 26 times.
164 if (!s->theora)
2596 138 skip_bits(&gb, 1);
2597
2598 int last_qps[3];
2599
2/2
✓ Branch 0 taken 492 times.
✓ Branch 1 taken 164 times.
656 for (int i = 0; i < 3; i++)
2600 492 last_qps[i] = s->qps[i];
2601
2602 164 s->nqps = 0;
2603 do {
2604 164 s->qps[s->nqps++] = get_bits(&gb, 6);
2605
4/6
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 138 times.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 26 times.
164 } while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb));
2606
2/2
✓ Branch 0 taken 328 times.
✓ Branch 1 taken 164 times.
492 for (int i = s->nqps; i < 3; i++)
2607 328 s->qps[i] = -1;
2608
2609
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 164 times.
164 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2610 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%"PRId64": Q index = %d\n",
2611 s->keyframe ? "key" : "", avctx->frame_num + 1, s->qps[0]);
2612
2613
3/4
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 138 times.
302 s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
2614 138 avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL
2615
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 135 times.
138 : AVDISCARD_NONKEY);
2616
2617
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 102 times.
164 if (s->qps[0] != last_qps[0])
2618 62 init_loop_filter(s);
2619
2620
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 164 times.
328 for (int i = 0; i < s->nqps; i++)
2621 // reinit all dequantizers if the first one changed, because
2622 // the DC of the first quantizer must be used for all matrices
2623
3/4
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 62 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 102 times.
164 if (s->qps[i] != last_qps[i] || s->qps[0] != last_qps[0])
2624 62 init_dequantizer(s, i);
2625
2626
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 164 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
164 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
2627 return buf_size;
2628
2629 164 ret = ff_progress_frame_get_buffer(avctx, &s->last_frame,
2630 AV_GET_BUFFER_FLAG_REF);
2631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 164 times.
164 if (ret < 0) {
2632 // Don't goto error here, as one can't report progress on or
2633 // unref a non-existent frame.
2634 return ret;
2635 }
2636 164 FFSWAP(ProgressFrame, s->last_frame, s->current_frame);
2637 328 s->current_frame.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
2638
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 158 times.
164 : AV_PICTURE_TYPE_P;
2639
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 158 times.
164 if (s->keyframe)
2640 6 s->current_frame.f->flags |= AV_FRAME_FLAG_KEY;
2641 else
2642 158 s->current_frame.f->flags &= ~AV_FRAME_FLAG_KEY;
2643
2644
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 159 times.
164 if (!s->edge_emu_buffer) {
2645 5 s->edge_emu_buffer = av_malloc(9 * FFABS(s->current_frame.f->linesize[0]));
2646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!s->edge_emu_buffer) {
2647 ret = AVERROR(ENOMEM);
2648 goto error;
2649 }
2650 }
2651
2652
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 158 times.
164 if (s->keyframe) {
2653
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (!s->theora) {
2654 3 skip_bits(&gb, 4); /* width code */
2655 3 skip_bits(&gb, 4); /* height code */
2656
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (s->version) {
2657 3 int version = get_bits(&gb, 5);
2658 #if !CONFIG_VP4_DECODER
2659 if (version >= 2) {
2660 av_log(avctx, AV_LOG_ERROR, "This build does not support decoding VP4.\n");
2661 ret = AVERROR_DECODER_NOT_FOUND;
2662 goto error;
2663 }
2664 #endif
2665 3 s->version = version;
2666
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (avctx->frame_num == 0)
2667 2 av_log(s->avctx, AV_LOG_DEBUG,
2668 "VP version: %d\n", s->version);
2669 }
2670 }
2671
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6 if (s->version || s->theora) {
2672
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (get_bits1(&gb))
2673 av_log(s->avctx, AV_LOG_ERROR,
2674 "Warning, unsupported keyframe coding type?!\n");
2675 6 skip_bits(&gb, 2); /* reserved? */
2676
2677 #if CONFIG_VP4_DECODER
2678
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (s->version >= 2) {
2679 int mb_height, mb_width;
2680 int mb_width_mul, mb_width_div, mb_height_mul, mb_height_div;
2681
2682 2 mb_height = get_bits(&gb, 8);
2683 2 mb_width = get_bits(&gb, 8);
2684
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (mb_height != s->macroblock_height ||
2685
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 mb_width != s->macroblock_width)
2686 avpriv_request_sample(s->avctx, "macroblock dimension mismatch");
2687
2688 2 mb_width_mul = get_bits(&gb, 5);
2689 2 mb_width_div = get_bits(&gb, 3);
2690 2 mb_height_mul = get_bits(&gb, 5);
2691 2 mb_height_div = get_bits(&gb, 3);
2692
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
2 if (mb_width_mul != 1 || mb_width_div != 1 || mb_height_mul != 1 || mb_height_div != 1)
2693 avpriv_request_sample(s->avctx, "unexpected macroblock dimension multiplier/divider");
2694
2695
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (get_bits(&gb, 2))
2696 avpriv_request_sample(s->avctx, "unknown bits");
2697 }
2698 #endif
2699 }
2700 6 ff_progress_frame_replace(&s->golden_frame, &s->current_frame);
2701 } else {
2702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 158 times.
158 if (!s->golden_frame.f) {
2703 av_log(s->avctx, AV_LOG_WARNING,
2704 "vp3: first frame not a keyframe\n");
2705
2706 if ((ret = ff_progress_frame_get_buffer(avctx, &s->golden_frame,
2707 AV_GET_BUFFER_FLAG_REF)) < 0)
2708 goto error;
2709 s->golden_frame.f->pict_type = AV_PICTURE_TYPE_I;
2710 ff_progress_frame_replace(&s->last_frame, &s->golden_frame);
2711 ff_progress_frame_report(&s->golden_frame, INT_MAX);
2712 }
2713 }
2714 164 ff_thread_finish_setup(avctx);
2715
2716 164 memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
2717
2718
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 24 times.
164 if (s->version < 2) {
2719
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
140 if ((ret = unpack_superblocks(s, &gb)) < 0) {
2720 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
2721 goto error;
2722 }
2723 #if CONFIG_VP4_DECODER
2724 } else {
2725
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if ((ret = vp4_unpack_macroblocks(s, &gb)) < 0) {
2726 av_log(s->avctx, AV_LOG_ERROR, "error in vp4_unpack_macroblocks\n");
2727 goto error;
2728 }
2729 #endif
2730 }
2731
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 164 times.
164 if ((ret = unpack_modes(s, &gb)) < 0) {
2732 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
2733 goto error;
2734 }
2735
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 164 times.
164 if (ret = unpack_vectors(s, &gb)) {
2736 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
2737 goto error;
2738 }
2739
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 164 times.
164 if ((ret = unpack_block_qpis(s, &gb)) < 0) {
2740 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
2741 goto error;
2742 }
2743
2744
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 24 times.
164 if (s->version < 2) {
2745
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
140 if ((ret = unpack_dct_coeffs(s, &gb)) < 0) {
2746 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
2747 goto error;
2748 }
2749 #if CONFIG_VP4_DECODER
2750 } else {
2751
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if ((ret = vp4_unpack_dct_coeffs(s, &gb)) < 0) {
2752 av_log(s->avctx, AV_LOG_ERROR, "error in vp4_unpack_dct_coeffs\n");
2753 goto error;
2754 }
2755 #endif
2756 }
2757
2758
2/2
✓ Branch 0 taken 492 times.
✓ Branch 1 taken 164 times.
656 for (int i = 0; i < 3; i++) {
2759
3/4
✓ Branch 0 taken 328 times.
✓ Branch 1 taken 164 times.
✓ Branch 2 taken 328 times.
✗ Branch 3 not taken.
492 int height = s->height >> (i && s->chroma_y_shift);
2760
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 492 times.
492 if (s->flipped_image)
2761 s->data_offset[i] = 0;
2762 else
2763 492 s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i];
2764 }
2765
2766 164 s->last_slice_end = 0;
2767
2/2
✓ Branch 0 taken 1068 times.
✓ Branch 1 taken 164 times.
1232 for (int i = 0; i < s->c_superblock_height; i++)
2768 1068 render_slice(s, i);
2769
2770 // filter the last row
2771
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 24 times.
164 if (s->version < 2)
2772
2/2
✓ Branch 0 taken 420 times.
✓ Branch 1 taken 140 times.
560 for (int i = 0; i < 3; i++) {
2773
3/4
✓ Branch 0 taken 280 times.
✓ Branch 1 taken 140 times.
✓ Branch 2 taken 280 times.
✗ Branch 3 not taken.
420 int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1;
2774 420 apply_loop_filter(s, i, row, row + 1);
2775 }
2776 164 vp3_draw_horiz_band(s, s->height);
2777
2778 164 ff_progress_frame_unref(&s->last_frame);
2779
2780 /* output frame, offset as needed */
2781
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 164 times.
164 if ((ret = av_frame_ref(frame, s->current_frame.f)) < 0)
2782 return ret;
2783
2784 164 frame->crop_left = s->offset_x;
2785 164 frame->crop_right = avctx->coded_width - avctx->width - s->offset_x;
2786 164 frame->crop_top = s->offset_y;
2787 164 frame->crop_bottom = avctx->coded_height - avctx->height - s->offset_y;
2788
2789 164 *got_frame = 1;
2790
2791 164 return buf_size;
2792
2793 error:
2794 ff_progress_frame_report(&s->current_frame, INT_MAX);
2795 ff_progress_frame_unref(&s->last_frame);
2796
2797 return ret;
2798 }
2799
2800 151200 static int read_huffman_tree(HuffTable *huff, GetBitContext *gb, int length,
2801 AVCodecContext *avctx)
2802 {
2803
2/2
✓ Branch 1 taken 76800 times.
✓ Branch 2 taken 74400 times.
151200 if (get_bits1(gb)) {
2804 int token;
2805
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76800 times.
76800 if (huff->nb_entries >= 32) { /* overflow */
2806 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2807 return -1;
2808 }
2809 76800 token = get_bits(gb, 5);
2810 ff_dlog(avctx, "code length %d, curr entry %d, token %d\n",
2811 length, huff->nb_entries, token);
2812 76800 huff->entries[huff->nb_entries++] = (HuffEntry){ length, token };
2813 } else {
2814 /* The following bound follows from the fact that nb_entries <= 32. */
2815
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74400 times.
74400 if (length >= 31) { /* overflow */
2816 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2817 return -1;
2818 }
2819 74400 length++;
2820
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 74400 times.
74400 if (read_huffman_tree(huff, gb, length, avctx))
2821 return -1;
2822
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 74400 times.
74400 if (read_huffman_tree(huff, gb, length, avctx))
2823 return -1;
2824 }
2825 151200 return 0;
2826 }
2827
2828 #if CONFIG_THEORA_DECODER
2829 static const enum AVPixelFormat theora_pix_fmts[4] = {
2830 AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P
2831 };
2832
2833 30 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
2834 {
2835 30 Vp3DecodeContext *s = avctx->priv_data;
2836 int visible_width, visible_height, colorspace;
2837 30 uint8_t offset_x = 0, offset_y = 0;
2838 int ret;
2839 AVRational fps, aspect;
2840
2841
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if (get_bits_left(gb) < 206)
2842 return AVERROR_INVALIDDATA;
2843
2844 30 s->theora_header = 0;
2845 30 s->theora = get_bits(gb, 24);
2846 30 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
2847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (!s->theora) {
2848 s->theora = 1;
2849 avpriv_request_sample(s->avctx, "theora 0");
2850 }
2851
2852 /* 3.2.0 aka alpha3 has the same frame orientation as original vp3
2853 * but previous versions have the image flipped relative to vp3 */
2854
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (s->theora < 0x030200) {
2855 s->flipped_image = 1;
2856 av_log(avctx, AV_LOG_DEBUG,
2857 "Old (<alpha3) Theora bitstream, flipped image\n");
2858 }
2859
2860 30 visible_width =
2861 30 s->width = get_bits(gb, 16) << 4;
2862 30 visible_height =
2863 30 s->height = get_bits(gb, 16) << 4;
2864
2865
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (s->theora >= 0x030200) {
2866 30 visible_width = get_bits(gb, 24);
2867 30 visible_height = get_bits(gb, 24);
2868
2869 30 offset_x = get_bits(gb, 8); /* offset x */
2870 30 offset_y = get_bits(gb, 8); /* offset y, from bottom */
2871 }
2872
2873 /* sanity check */
2874
1/2
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
30 if (av_image_check_size(visible_width, visible_height, 0, avctx) < 0 ||
2875
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 visible_width + offset_x > s->width ||
2876
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
30 visible_height + offset_y > s->height ||
2877 visible_width < 18
2878 ) {
2879 av_log(avctx, AV_LOG_ERROR,
2880 "Invalid frame dimensions - w:%d h:%d x:%d y:%d (%dx%d).\n",
2881 visible_width, visible_height, offset_x, offset_y,
2882 s->width, s->height);
2883 return AVERROR_INVALIDDATA;
2884 }
2885
2886 30 fps.num = get_bits_long(gb, 32);
2887 30 fps.den = get_bits_long(gb, 32);
2888
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
30 if (fps.num && fps.den) {
2889
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
30 if (fps.num < 0 || fps.den < 0) {
2890 av_log(avctx, AV_LOG_ERROR, "Invalid framerate\n");
2891 return AVERROR_INVALIDDATA;
2892 }
2893 30 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
2894 30 fps.den, fps.num, 1 << 30);
2895 }
2896
2897 30 aspect.num = get_bits(gb, 24);
2898 30 aspect.den = get_bits(gb, 24);
2899
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
30 if (aspect.num && aspect.den) {
2900 6 av_reduce(&avctx->sample_aspect_ratio.num,
2901 &avctx->sample_aspect_ratio.den,
2902 6 aspect.num, aspect.den, 1 << 30);
2903 6 ff_set_sar(avctx, avctx->sample_aspect_ratio);
2904 }
2905
2906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (s->theora < 0x030200)
2907 skip_bits(gb, 5); /* keyframe frequency force */
2908 30 colorspace = get_bits(gb, 8);
2909 30 skip_bits(gb, 24); /* bitrate */
2910
2911 30 skip_bits(gb, 6); /* quality hint */
2912
2913
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (s->theora >= 0x030200) {
2914 30 skip_bits(gb, 5); /* keyframe frequency force */
2915 30 avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)];
2916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
2917 av_log(avctx, AV_LOG_ERROR, "Invalid pixel format\n");
2918 return AVERROR_INVALIDDATA;
2919 }
2920 30 skip_bits(gb, 3); /* reserved */
2921 } else
2922 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2923
2924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (s->width < 18)
2925 return AVERROR_PATCHWELCOME;
2926 30 ret = ff_set_dimensions(avctx, s->width, s->height);
2927
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (ret < 0)
2928 return ret;
2929
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (!(avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP)) {
2930 30 avctx->width = visible_width;
2931 30 avctx->height = visible_height;
2932 // translate offsets from theora axis ([0,0] lower left)
2933 // to normal axis ([0,0] upper left)
2934 30 s->offset_x = offset_x;
2935 30 s->offset_y = s->height - visible_height - offset_y;
2936 }
2937
2938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (colorspace == 1)
2939 avctx->color_primaries = AVCOL_PRI_BT470M;
2940
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 else if (colorspace == 2)
2941 avctx->color_primaries = AVCOL_PRI_BT470BG;
2942
2943
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
30 if (colorspace == 1 || colorspace == 2) {
2944 avctx->colorspace = AVCOL_SPC_BT470BG;
2945 avctx->color_trc = AVCOL_TRC_BT709;
2946 }
2947
2948 30 s->theora_header = 1;
2949 30 return 0;
2950 }
2951
2952 30 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
2953 {
2954 30 Vp3DecodeContext *s = avctx->priv_data;
2955 int n, matrices, ret;
2956
2957
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (!s->theora_header)
2958 return AVERROR_INVALIDDATA;
2959
2960
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (s->theora >= 0x030200) {
2961 30 n = get_bits(gb, 3);
2962 /* loop filter limit values table */
2963
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (n)
2964
2/2
✓ Branch 0 taken 1920 times.
✓ Branch 1 taken 30 times.
1950 for (int i = 0; i < 64; i++)
2965 1920 s->filter_limit_values[i] = get_bits(gb, n);
2966 }
2967
2968
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (s->theora >= 0x030200)
2969 30 n = get_bits(gb, 4) + 1;
2970 else
2971 n = 16;
2972 /* quality threshold table */
2973
2/2
✓ Branch 0 taken 1920 times.
✓ Branch 1 taken 30 times.
1950 for (int i = 0; i < 64; i++)
2974 1920 s->coded_ac_scale_factor[i] = get_bits(gb, n);
2975
2976
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (s->theora >= 0x030200)
2977 30 n = get_bits(gb, 4) + 1;
2978 else
2979 n = 16;
2980 /* dc scale factor table */
2981
2/2
✓ Branch 0 taken 1920 times.
✓ Branch 1 taken 30 times.
1950 for (int i = 0; i < 64; i++)
2982 1920 s->coded_dc_scale_factor[0][i] =
2983 1920 s->coded_dc_scale_factor[1][i] = get_bits(gb, n);
2984
2985
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (s->theora >= 0x030200)
2986 30 matrices = get_bits(gb, 9) + 1;
2987 else
2988 matrices = 3;
2989
2990
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (matrices > 384) {
2991 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
2992 return -1;
2993 }
2994
2995
2/2
✓ Branch 0 taken 387 times.
✓ Branch 1 taken 30 times.
417 for (int j = 0; j < matrices; j++)
2996
2/2
✓ Branch 0 taken 24768 times.
✓ Branch 1 taken 387 times.
25155 for (int i = 0; i < 64; i++)
2997 24768 s->base_matrix[j][i] = get_bits(gb, 8);
2998
2999
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 30 times.
90 for (int inter = 0; inter <= 1; inter++) {
3000
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 60 times.
240 for (int plane = 0; plane <= 2; plane++) {
3001 180 int newqr = 1;
3002
4/4
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 30 times.
180 if (inter || plane > 0)
3003 150 newqr = get_bits1(gb);
3004
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 66 times.
180 if (!newqr) {
3005 int qtj, plj;
3006
4/4
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 30 times.
✓ Branch 3 taken 72 times.
✓ Branch 4 taken 12 times.
114 if (inter && get_bits1(gb)) {
3007 72 qtj = 0;
3008 72 plj = plane;
3009 } else {
3010 42 qtj = (3 * inter + plane - 1) / 3;
3011 42 plj = (plane + 2) % 3;
3012 }
3013 114 s->qr_count[inter][plane] = s->qr_count[qtj][plj];
3014 114 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj],
3015 sizeof(s->qr_size[0][0]));
3016 114 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj],
3017 sizeof(s->qr_base[0][0]));
3018 } else {
3019 66 int qri = 0;
3020 66 int qi = 0;
3021
3022 360 for (;;) {
3023 426 int i = get_bits(gb, av_log2(matrices - 1) + 1);
3024
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 426 times.
426 if (i >= matrices) {
3025 av_log(avctx, AV_LOG_ERROR,
3026 "invalid base matrix index\n");
3027 return -1;
3028 }
3029 426 s->qr_base[inter][plane][qri] = i;
3030
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 360 times.
426 if (qi >= 63)
3031 66 break;
3032 360 i = get_bits(gb, av_log2(63 - qi) + 1) + 1;
3033 360 s->qr_size[inter][plane][qri++] = i;
3034 360 qi += i;
3035 }
3036
3037
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (qi > 63) {
3038 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
3039 return -1;
3040 }
3041 66 s->qr_count[inter][plane] = qri;
3042 }
3043 }
3044 }
3045
3046 /* Huffman tables */
3047
2/2
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 30 times.
2430 for (int i = 0; i < FF_ARRAY_ELEMS(s->huffman_table); i++) {
3048 2400 s->huffman_table[i].nb_entries = 0;
3049
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2400 times.
2400 if ((ret = read_huffman_tree(&s->huffman_table[i], gb, 0, avctx)) < 0)
3050 return ret;
3051 }
3052
3053 30 s->theora_tables = 1;
3054
3055 30 return 0;
3056 }
3057
3058 30 static av_cold int theora_decode_init(AVCodecContext *avctx)
3059 {
3060 30 Vp3DecodeContext *s = avctx->priv_data;
3061 GetBitContext gb;
3062 int ptype;
3063 const uint8_t *header_start[3];
3064 int header_len[3];
3065 int ret;
3066
3067 30 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
3068
3069 30 s->theora = 1;
3070
3071
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (!avctx->extradata_size) {
3072 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
3073 return -1;
3074 }
3075
3076
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if (avpriv_split_xiph_headers(avctx->extradata, avctx->extradata_size,
3077 42, header_start, header_len) < 0) {
3078 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
3079 return -1;
3080 }
3081
3082
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 30 times.
120 for (int i = 0; i < 3; i++) {
3083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
90 if (header_len[i] <= 0)
3084 continue;
3085 90 ret = init_get_bits8(&gb, header_start[i], header_len[i]);
3086
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
90 if (ret < 0)
3087 return ret;
3088
3089 90 ptype = get_bits(&gb, 8);
3090
3091
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
90 if (!(ptype & 0x80)) {
3092 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
3093 // return -1;
3094 }
3095
3096 // FIXME: Check for this as well.
3097 90 skip_bits_long(&gb, 6 * 8); /* "theora" */
3098
3099
3/4
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
90 switch (ptype) {
3100 30 case 0x80:
3101
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if (theora_decode_header(avctx, &gb) < 0)
3102 return -1;
3103 30 break;
3104 30 case 0x81:
3105 // FIXME: is this needed? it breaks sometimes
3106 // theora_decode_comments(avctx, gb);
3107 30 break;
3108 30 case 0x82:
3109
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if (theora_decode_tables(avctx, &gb))
3110 return -1;
3111 30 break;
3112 default:
3113 av_log(avctx, AV_LOG_ERROR,
3114 "Unknown Theora config packet: %d\n", ptype & ~0x80);
3115 break;
3116 }
3117
3/4
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 60 times.
90 if (ptype != 0x81 && get_bits_left(&gb) >= 8U)
3118 av_log(avctx, AV_LOG_WARNING,
3119 "%d bits left in packet %X\n",
3120 get_bits_left(&gb), ptype);
3121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
90 if (s->theora < 0x030200)
3122 break;
3123 }
3124
3125 30 return vp3_decode_init(avctx);
3126 }
3127
3128 const FFCodec ff_theora_decoder = {
3129 .p.name = "theora",
3130 CODEC_LONG_NAME("Theora"),
3131 .p.type = AVMEDIA_TYPE_VIDEO,
3132 .p.id = AV_CODEC_ID_THEORA,
3133 .priv_data_size = sizeof(Vp3DecodeContext),
3134 .init = theora_decode_init,
3135 .close = vp3_decode_end,
3136 FF_CODEC_DECODE_CB(vp3_decode_frame),
3137 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND |
3138 AV_CODEC_CAP_FRAME_THREADS,
3139 .flush = vp3_decode_flush,
3140 UPDATE_THREAD_CONTEXT(vp3_update_thread_context),
3141 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
3142 FF_CODEC_CAP_EXPORTS_CROPPING |
3143 FF_CODEC_CAP_USES_PROGRESSFRAMES,
3144 };
3145 #endif
3146
3147 const FFCodec ff_vp3_decoder = {
3148 .p.name = "vp3",
3149 CODEC_LONG_NAME("On2 VP3"),
3150 .p.type = AVMEDIA_TYPE_VIDEO,
3151 .p.id = AV_CODEC_ID_VP3,
3152 .priv_data_size = sizeof(Vp3DecodeContext),
3153 .init = vp3_decode_init,
3154 .close = vp3_decode_end,
3155 FF_CODEC_DECODE_CB(vp3_decode_frame),
3156 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND |
3157 AV_CODEC_CAP_FRAME_THREADS,
3158 .flush = vp3_decode_flush,
3159 UPDATE_THREAD_CONTEXT(vp3_update_thread_context),
3160 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
3161 FF_CODEC_CAP_USES_PROGRESSFRAMES,
3162 };
3163
3164 #if CONFIG_VP4_DECODER
3165 const FFCodec ff_vp4_decoder = {
3166 .p.name = "vp4",
3167 CODEC_LONG_NAME("On2 VP4"),
3168 .p.type = AVMEDIA_TYPE_VIDEO,
3169 .p.id = AV_CODEC_ID_VP4,
3170 .priv_data_size = sizeof(Vp3DecodeContext),
3171 .init = vp3_decode_init,
3172 .close = vp3_decode_end,
3173 FF_CODEC_DECODE_CB(vp3_decode_frame),
3174 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND |
3175 AV_CODEC_CAP_FRAME_THREADS,
3176 .flush = vp3_decode_flush,
3177 UPDATE_THREAD_CONTEXT(vp3_update_thread_context),
3178 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
3179 FF_CODEC_CAP_USES_PROGRESSFRAMES,
3180 };
3181 #endif
3182