FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp3.c
Date: 2024-04-26 14:42:52
Exec Total Coverage
Lines: 1232 1517 81.2%
Functions: 41 44 93.2%
Branches: 805 1084 74.3%

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