FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp3.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 1248 1547 80.7%
Functions: 42 46 91.3%
Branches: 813 1114 73.0%

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_internal.h"
41 #include "libavutil/thread.h"
42
43 #include "avcodec.h"
44 #include "codec_internal.h"
45 #include "decode.h"
46 #include "get_bits.h"
47 #include "hpeldsp.h"
48 #include "internal.h"
49 #include "jpegquanttables.h"
50 #include "mathops.h"
51 #include "refstruct.h"
52 #include "thread.h"
53 #include "threadframe.h"
54 #include "videodsp.h"
55 #include "vp3data.h"
56 #include "vp4data.h"
57 #include "vp3dsp.h"
58 #include "xiph.h"
59
60 #define VP3_MV_VLC_BITS 6
61 #define VP4_MV_VLC_BITS 6
62 #define SUPERBLOCK_VLC_BITS 6
63
64 #define FRAGMENT_PIXELS 8
65
66 // FIXME split things out into their own arrays
67 typedef struct Vp3Fragment {
68 int16_t dc;
69 uint8_t coding_method;
70 uint8_t qpi;
71 } Vp3Fragment;
72
73 #define SB_NOT_CODED 0
74 #define SB_PARTIALLY_CODED 1
75 #define SB_FULLY_CODED 2
76
77 // This is the maximum length of a single long bit run that can be encoded
78 // for superblock coding or block qps. Theora special-cases this to read a
79 // bit instead of flipping the current bit to allow for runs longer than 4129.
80 #define MAXIMUM_LONG_BIT_RUN 4129
81
82 #define MODE_INTER_NO_MV 0
83 #define MODE_INTRA 1
84 #define MODE_INTER_PLUS_MV 2
85 #define MODE_INTER_LAST_MV 3
86 #define MODE_INTER_PRIOR_LAST 4
87 #define MODE_USING_GOLDEN 5
88 #define MODE_GOLDEN_MV 6
89 #define MODE_INTER_FOURMV 7
90 #define CODING_MODE_COUNT 8
91
92 /* special internal mode */
93 #define MODE_COPY 8
94
95 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb);
96 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb);
97
98
99 /* There are 6 preset schemes, plus a free-form scheme */
100 static const int ModeAlphabet[6][CODING_MODE_COUNT] = {
101 /* scheme 1: Last motion vector dominates */
102 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
103 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
104 MODE_INTRA, MODE_USING_GOLDEN,
105 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
106
107 /* scheme 2 */
108 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
109 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
110 MODE_INTRA, MODE_USING_GOLDEN,
111 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
112
113 /* scheme 3 */
114 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
115 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
116 MODE_INTRA, MODE_USING_GOLDEN,
117 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
118
119 /* scheme 4 */
120 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
121 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
122 MODE_INTRA, MODE_USING_GOLDEN,
123 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
124
125 /* scheme 5: No motion vector dominates */
126 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
127 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
128 MODE_INTRA, MODE_USING_GOLDEN,
129 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
130
131 /* scheme 6 */
132 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
133 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
134 MODE_INTER_PLUS_MV, MODE_INTRA,
135 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
136 };
137
138 static const uint8_t hilbert_offset[16][2] = {
139 { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
140 { 0, 2 }, { 0, 3 }, { 1, 3 }, { 1, 2 },
141 { 2, 2 }, { 2, 3 }, { 3, 3 }, { 3, 2 },
142 { 3, 1 }, { 2, 1 }, { 2, 0 }, { 3, 0 }
143 };
144
145 enum {
146 VP4_DC_INTRA = 0,
147 VP4_DC_INTER = 1,
148 VP4_DC_GOLDEN = 2,
149 NB_VP4_DC_TYPES,
150 VP4_DC_UNDEFINED = NB_VP4_DC_TYPES
151 };
152
153 static const uint8_t vp4_pred_block_type_map[8] = {
154 [MODE_INTER_NO_MV] = VP4_DC_INTER,
155 [MODE_INTRA] = VP4_DC_INTRA,
156 [MODE_INTER_PLUS_MV] = VP4_DC_INTER,
157 [MODE_INTER_LAST_MV] = VP4_DC_INTER,
158 [MODE_INTER_PRIOR_LAST] = VP4_DC_INTER,
159 [MODE_USING_GOLDEN] = VP4_DC_GOLDEN,
160 [MODE_GOLDEN_MV] = VP4_DC_GOLDEN,
161 [MODE_INTER_FOURMV] = VP4_DC_INTER,
162 };
163
164 static VLCElem superblock_run_length_vlc[88]; /* version < 2 */
165 static VLCElem fragment_run_length_vlc[56]; /* version < 2 */
166 static VLCElem motion_vector_vlc[112]; /* version < 2 */
167
168 // The VP4 tables reuse this vlc.
169 static VLCElem mode_code_vlc[24 + 2108 * CONFIG_VP4_DECODER];
170
171 #if CONFIG_VP4_DECODER
172 static const VLCElem *vp4_mv_vlc_table[2][7]; /* version >= 2 */
173 static const VLCElem *block_pattern_vlc[2]; /* version >= 2 */
174 #endif
175
176 typedef struct {
177 int dc;
178 int type;
179 } VP4Predictor;
180
181 #define MIN_DEQUANT_VAL 2
182
183 typedef struct HuffEntry {
184 uint8_t len, sym;
185 } HuffEntry;
186
187 typedef struct HuffTable {
188 HuffEntry entries[32];
189 uint8_t nb_entries;
190 } HuffTable;
191
192 typedef struct CoeffVLCs {
193 const VLCElem *vlc_tabs[80];
194 VLC vlcs[80];
195 } CoeffVLCs;
196
197 typedef struct Vp3DecodeContext {
198 AVCodecContext *avctx;
199 int theora, theora_tables, theora_header;
200 int version;
201 int width, height;
202 int chroma_x_shift, chroma_y_shift;
203 ThreadFrame golden_frame;
204 ThreadFrame last_frame;
205 ThreadFrame current_frame;
206 int keyframe;
207 uint8_t idct_permutation[64];
208 uint8_t idct_scantable[64];
209 HpelDSPContext hdsp;
210 VideoDSPContext vdsp;
211 VP3DSPContext vp3dsp;
212 DECLARE_ALIGNED(16, int16_t, block)[64];
213 int flipped_image;
214 int last_slice_end;
215 int skip_loop_filter;
216
217 int qps[3];
218 int nqps;
219 int last_qps[3];
220
221 int superblock_count;
222 int y_superblock_width;
223 int y_superblock_height;
224 int y_superblock_count;
225 int c_superblock_width;
226 int c_superblock_height;
227 int c_superblock_count;
228 int u_superblock_start;
229 int v_superblock_start;
230 unsigned char *superblock_coding;
231
232 int macroblock_count; /* y macroblock count */
233 int macroblock_width;
234 int macroblock_height;
235 int c_macroblock_count;
236 int c_macroblock_width;
237 int c_macroblock_height;
238 int yuv_macroblock_count; /* y+u+v macroblock count */
239
240 int fragment_count;
241 int fragment_width[2];
242 int fragment_height[2];
243
244 Vp3Fragment *all_fragments;
245 int fragment_start[3];
246 int data_offset[3];
247 uint8_t offset_x;
248 uint8_t offset_y;
249 int offset_x_warned;
250
251 int8_t (*motion_val[2])[2];
252
253 /* tables */
254 uint16_t coded_dc_scale_factor[2][64];
255 uint32_t coded_ac_scale_factor[64];
256 uint8_t base_matrix[384][64];
257 uint8_t qr_count[2][3];
258 uint8_t qr_size[2][3][64];
259 uint16_t qr_base[2][3][64];
260
261 /**
262 * This is a list of all tokens in bitstream order. Reordering takes place
263 * by pulling from each level during IDCT. As a consequence, IDCT must be
264 * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
265 * otherwise. The 32 different tokens with up to 12 bits of extradata are
266 * collapsed into 3 types, packed as follows:
267 * (from the low to high bits)
268 *
269 * 2 bits: type (0,1,2)
270 * 0: EOB run, 14 bits for run length (12 needed)
271 * 1: zero run, 7 bits for run length
272 * 7 bits for the next coefficient (3 needed)
273 * 2: coefficient, 14 bits (11 needed)
274 *
275 * Coefficients are signed, so are packed in the highest bits for automatic
276 * sign extension.
277 */
278 int16_t *dct_tokens[3][64];
279 int16_t *dct_tokens_base;
280 #define TOKEN_EOB(eob_run) ((eob_run) << 2)
281 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) * 512) + ((zero_run) << 2) + 1)
282 #define TOKEN_COEFF(coeff) (((coeff) * 4) + 2)
283
284 /**
285 * number of blocks that contain DCT coefficients at
286 * the given level or higher
287 */
288 int num_coded_frags[3][64];
289 int total_num_coded_frags;
290
291 /* this is a list of indexes into the all_fragments array indicating
292 * which of the fragments are coded */
293 int *coded_fragment_list[3];
294
295 int *kf_coded_fragment_list;
296 int *nkf_coded_fragment_list;
297 int num_kf_coded_fragment[3];
298
299 /**
300 * The first 16 of the following VLCs are for the dc coefficients;
301 * the others are four groups of 16 VLCs each for ac coefficients.
302 * This is a RefStruct reference to share these VLCs between threads.
303 */
304 CoeffVLCs *coeff_vlc;
305
306 /* these arrays need to be on 16-byte boundaries since SSE2 operations
307 * index into them */
308 DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; ///< qmat[qpi][is_inter][plane]
309
310 /* This table contains superblock_count * 16 entries. Each set of 16
311 * numbers corresponds to the fragment indexes 0..15 of the superblock.
312 * An entry will be -1 to indicate that no entry corresponds to that
313 * index. */
314 int *superblock_fragments;
315
316 /* This is an array that indicates how a particular macroblock
317 * is coded. */
318 unsigned char *macroblock_coding;
319
320 uint8_t *edge_emu_buffer;
321
322 /* Huffman decode */
323 HuffTable huffman_table[5 * 16];
324
325 uint8_t filter_limit_values[64];
326 DECLARE_ALIGNED(8, int, bounding_values_array)[256 + 2];
327
328 VP4Predictor * dc_pred_row; /* dc_pred_row[y_superblock_width * 4] */
329 } Vp3DecodeContext;
330
331 /************************************************************************
332 * VP3 specific functions
333 ************************************************************************/
334
335 68 static av_cold void free_tables(AVCodecContext *avctx)
336 {
337 68 Vp3DecodeContext *s = avctx->priv_data;
338
339 68 av_freep(&s->superblock_coding);
340 68 av_freep(&s->all_fragments);
341 68 av_freep(&s->nkf_coded_fragment_list);
342 68 av_freep(&s->kf_coded_fragment_list);
343 68 av_freep(&s->dct_tokens_base);
344 68 av_freep(&s->superblock_fragments);
345 68 av_freep(&s->macroblock_coding);
346 68 av_freep(&s->dc_pred_row);
347 68 av_freep(&s->motion_val[0]);
348 68 av_freep(&s->motion_val[1]);
349 68 }
350
351 34 static void vp3_decode_flush(AVCodecContext *avctx)
352 {
353 34 Vp3DecodeContext *s = avctx->priv_data;
354
355
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (s->golden_frame.f)
356 34 ff_thread_release_ext_buffer(&s->golden_frame);
357
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (s->last_frame.f)
358 34 ff_thread_release_ext_buffer(&s->last_frame);
359
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (s->current_frame.f)
360 34 ff_thread_release_ext_buffer(&s->current_frame);
361 34 }
362
363 34 static av_cold int vp3_decode_end(AVCodecContext *avctx)
364 {
365 34 Vp3DecodeContext *s = avctx->priv_data;
366
367 34 free_tables(avctx);
368 34 av_freep(&s->edge_emu_buffer);
369
370 34 s->theora_tables = 0;
371
372 /* release all frames */
373 34 vp3_decode_flush(avctx);
374 34 av_frame_free(&s->current_frame.f);
375 34 av_frame_free(&s->last_frame.f);
376 34 av_frame_free(&s->golden_frame.f);
377
378 34 ff_refstruct_unref(&s->coeff_vlc);
379
380 34 return 0;
381 }
382
383 /**
384 * This function sets up all of the various blocks mappings:
385 * superblocks <-> fragments, macroblocks <-> fragments,
386 * superblocks <-> macroblocks
387 *
388 * @return 0 is successful; returns 1 if *anything* went wrong.
389 */
390 34 static int init_block_mapping(Vp3DecodeContext *s)
391 {
392 34 int j = 0;
393
394
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 34 times.
136 for (int plane = 0; plane < 3; plane++) {
395 102 int sb_width = plane ? s->c_superblock_width
396
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 34 times.
102 : s->y_superblock_width;
397 102 int sb_height = plane ? s->c_superblock_height
398
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 34 times.
102 : s->y_superblock_height;
399 102 int frag_width = s->fragment_width[!!plane];
400 102 int frag_height = s->fragment_height[!!plane];
401
402
2/2
✓ Branch 0 taken 1637 times.
✓ Branch 1 taken 102 times.
1739 for (int sb_y = 0; sb_y < sb_height; sb_y++)
403
2/2
✓ Branch 0 taken 46279 times.
✓ Branch 1 taken 1637 times.
47916 for (int sb_x = 0; sb_x < sb_width; sb_x++)
404
2/2
✓ Branch 0 taken 740464 times.
✓ Branch 1 taken 46279 times.
786743 for (int i = 0; i < 16; i++) {
405 740464 int x = 4 * sb_x + hilbert_offset[i][0];
406 740464 int y = 4 * sb_y + hilbert_offset[i][1];
407
408
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)
409 715314 s->superblock_fragments[j++] = s->fragment_start[plane] +
410 715314 y * frag_width + x;
411 else
412 25150 s->superblock_fragments[j++] = -1;
413 }
414 }
415
416 34 return 0; /* successful path out */
417 }
418
419 /*
420 * This function sets up the dequantization tables used for a particular
421 * frame.
422 */
423 62 static void init_dequantizer(Vp3DecodeContext *s, int qpi)
424 {
425 62 int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
426
427
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 62 times.
186 for (int inter = 0; inter < 2; inter++) {
428
2/2
✓ Branch 0 taken 372 times.
✓ Branch 1 taken 124 times.
496 for (int plane = 0; plane < 3; plane++) {
429 372 int dc_scale_factor = s->coded_dc_scale_factor[!!plane][s->qps[qpi]];
430 372 int sum = 0, bmi, bmj, qistart, qri;
431
1/2
✓ Branch 0 taken 408 times.
✗ Branch 1 not taken.
408 for (qri = 0; qri < s->qr_count[inter][plane]; qri++) {
432 408 sum += s->qr_size[inter][plane][qri];
433
2/2
✓ Branch 0 taken 372 times.
✓ Branch 1 taken 36 times.
408 if (s->qps[qpi] <= sum)
434 372 break;
435 }
436 372 qistart = sum - s->qr_size[inter][plane][qri];
437 372 bmi = s->qr_base[inter][plane][qri];
438 372 bmj = s->qr_base[inter][plane][qri + 1];
439
2/2
✓ Branch 0 taken 23808 times.
✓ Branch 1 taken 372 times.
24180 for (int i = 0; i < 64; i++) {
440 23808 int coeff = (2 * (sum - s->qps[qpi]) * s->base_matrix[bmi][i] -
441 23808 2 * (qistart - s->qps[qpi]) * s->base_matrix[bmj][i] +
442 23808 s->qr_size[inter][plane][qri]) /
443 23808 (2 * s->qr_size[inter][plane][qri]);
444
445 23808 int qmin = 8 << (inter + !i);
446
2/2
✓ Branch 0 taken 23436 times.
✓ Branch 1 taken 372 times.
23808 int qscale = i ? ac_scale_factor : dc_scale_factor;
447 23808 int qbias = (1 + inter) * 3;
448
2/2
✓ Branch 0 taken 23436 times.
✓ Branch 1 taken 372 times.
47616 s->qmat[qpi][inter][plane][s->idct_permutation[i]] =
449
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)
450 1512 : (qscale * (coeff - qbias) / 100 + qbias) * 4;
451 }
452 /* all DC coefficients use the same quant so as not to interfere
453 * with DC prediction */
454 372 s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
455 }
456 }
457 62 }
458
459 /*
460 * This function initializes the loop filter boundary limits if the frame's
461 * quality index is different from the previous frame's.
462 *
463 * The filter_limit_values may not be larger than 127.
464 */
465 62 static void init_loop_filter(Vp3DecodeContext *s)
466 {
467 62 ff_vp3dsp_set_bounding_values(s->bounding_values_array, s->filter_limit_values[s->qps[0]]);
468 62 }
469
470 /*
471 * This function unpacks all of the superblock/macroblock/fragment coding
472 * information from the bitstream.
473 */
474 140 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
475 {
476 140 const int superblock_starts[3] = {
477 140 0, s->u_superblock_start, s->v_superblock_start
478 };
479 140 int bit = 0;
480 140 int current_superblock = 0;
481 140 int current_run = 0;
482 140 int num_partial_superblocks = 0;
483
484 int current_fragment;
485 140 int plane0_num_coded_frags = 0;
486
487
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 136 times.
140 if (s->keyframe) {
488 4 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
489 } else {
490 /* unpack the list of partially-coded superblocks */
491 136 bit = get_bits1(gb) ^ 1;
492 136 current_run = 0;
493
494
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) {
495
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)
496 bit = get_bits1(gb);
497 else
498 8545 bit ^= 1;
499
500 8545 current_run = get_vlc2(gb, superblock_run_length_vlc,
501 SUPERBLOCK_VLC_BITS, 2);
502
2/2
✓ Branch 0 taken 437 times.
✓ Branch 1 taken 8108 times.
8545 if (current_run == 34)
503 437 current_run += get_bits(gb, 12);
504
505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8545 times.
8545 if (current_run > s->superblock_count - current_superblock) {
506 av_log(s->avctx, AV_LOG_ERROR,
507 "Invalid partially coded superblock run length\n");
508 return -1;
509 }
510
511 8545 memset(s->superblock_coding + current_superblock, bit, current_run);
512
513 8545 current_superblock += current_run;
514
2/2
✓ Branch 0 taken 4237 times.
✓ Branch 1 taken 4308 times.
8545 if (bit)
515 4237 num_partial_superblocks += current_run;
516 }
517
518 /* unpack the list of fully coded superblocks if any of the blocks were
519 * not marked as partially coded in the previous step */
520
1/2
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
136 if (num_partial_superblocks < s->superblock_count) {
521 136 int superblocks_decoded = 0;
522
523 136 current_superblock = 0;
524 136 bit = get_bits1(gb) ^ 1;
525 136 current_run = 0;
526
527
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 &&
528 1052 get_bits_left(gb) > 0) {
529
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)
530 6 bit = get_bits1(gb);
531 else
532 1046 bit ^= 1;
533
534 1052 current_run = get_vlc2(gb, superblock_run_length_vlc,
535 SUPERBLOCK_VLC_BITS, 2);
536
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 834 times.
1052 if (current_run == 34)
537 218 current_run += get_bits(gb, 12);
538
539
2/2
✓ Branch 0 taken 104900 times.
✓ Branch 1 taken 1052 times.
105952 for (int j = 0; j < current_run; current_superblock++) {
540
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104900 times.
104900 if (current_superblock >= s->superblock_count) {
541 av_log(s->avctx, AV_LOG_ERROR,
542 "Invalid fully coded superblock run length\n");
543 return -1;
544 }
545
546 /* skip any superblocks already marked as partially coded */
547
2/2
✓ Branch 0 taken 88554 times.
✓ Branch 1 taken 16346 times.
104900 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
548 88554 s->superblock_coding[current_superblock] = 2 * bit;
549 88554 j++;
550 }
551 }
552 1052 superblocks_decoded += current_run;
553 }
554 }
555
556 /* if there were partial blocks, initialize bitstream for
557 * unpacking fragment codings */
558
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 8 times.
136 if (num_partial_superblocks) {
559 128 current_run = 0;
560 128 bit = get_bits1(gb);
561 /* toggle the bit because as soon as the first run length is
562 * fetched the bit will be toggled again */
563 128 bit ^= 1;
564 }
565 }
566
567 /* figure out which fragments are coded; iterate through each
568 * superblock (all planes) */
569 140 s->total_num_coded_frags = 0;
570 140 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
571
572 280 s->coded_fragment_list[0] = s->keyframe ? s->kf_coded_fragment_list
573
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 136 times.
140 : s->nkf_coded_fragment_list;
574
575
2/2
✓ Branch 0 taken 420 times.
✓ Branch 1 taken 140 times.
560 for (int plane = 0; plane < 3; plane++) {
576 420 int sb_start = superblock_starts[plane];
577 420 int sb_end = sb_start + (plane ? s->c_superblock_count
578
2/2
✓ Branch 0 taken 280 times.
✓ Branch 1 taken 140 times.
420 : s->y_superblock_count);
579 420 int num_coded_frags = 0;
580
581
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 408 times.
420 if (s->keyframe) {
582
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (s->num_kf_coded_fragment[plane] == -1) {
583
2/2
✓ Branch 0 taken 10526 times.
✓ Branch 1 taken 12 times.
10538 for (int i = sb_start; i < sb_end; i++) {
584 /* iterate through all 16 fragments in a superblock */
585
2/2
✓ Branch 0 taken 168416 times.
✓ Branch 1 taken 10526 times.
178942 for (int j = 0; j < 16; j++) {
586 /* if the fragment is in bounds, check its coding status */
587 168416 current_fragment = s->superblock_fragments[i * 16 + j];
588
2/2
✓ Branch 0 taken 162492 times.
✓ Branch 1 taken 5924 times.
168416 if (current_fragment != -1) {
589 162492 s->coded_fragment_list[plane][num_coded_frags++] =
590 current_fragment;
591 }
592 }
593 }
594 12 s->num_kf_coded_fragment[plane] = num_coded_frags;
595 } else
596 num_coded_frags = s->num_kf_coded_fragment[plane];
597 } else {
598
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++) {
599
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 105186 times.
105186 if (get_bits_left(gb) < plane0_num_coded_frags >> 2) {
600 return AVERROR_INVALIDDATA;
601 }
602 /* iterate through all 16 fragments in a superblock */
603
2/2
✓ Branch 0 taken 1682976 times.
✓ Branch 1 taken 105186 times.
1788162 for (int j = 0; j < 16; j++) {
604 /* if the fragment is in bounds, check its coding status */
605 1682976 current_fragment = s->superblock_fragments[i * 16 + j];
606
2/2
✓ Branch 0 taken 1598148 times.
✓ Branch 1 taken 84828 times.
1682976 if (current_fragment != -1) {
607 1598148 int coded = s->superblock_coding[i];
608
609
2/2
✓ Branch 0 taken 241844 times.
✓ Branch 1 taken 1356304 times.
1598148 if (coded == SB_PARTIALLY_CODED) {
610 /* fragment may or may not be coded; this is the case
611 * that cares about the fragment coding runs */
612
2/2
✓ Branch 0 taken 70601 times.
✓ Branch 1 taken 171243 times.
241844 if (current_run-- == 0) {
613 70601 bit ^= 1;
614 70601 current_run = get_vlc2(gb, fragment_run_length_vlc, 5, 2);
615 }
616 241844 coded = bit;
617 }
618
619
2/2
✓ Branch 0 taken 370084 times.
✓ Branch 1 taken 1228064 times.
1598148 if (coded) {
620 /* default mode; actual mode will be decoded in
621 * the next phase */
622 370084 s->all_fragments[current_fragment].coding_method =
623 MODE_INTER_NO_MV;
624 370084 s->coded_fragment_list[plane][num_coded_frags++] =
625 current_fragment;
626 } else {
627 /* not coded; copy this fragment from the prior frame */
628 1228064 s->all_fragments[current_fragment].coding_method =
629 MODE_COPY;
630 }
631 }
632 }
633 }
634 }
635
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 280 times.
420 if (!plane)
636 140 plane0_num_coded_frags = num_coded_frags;
637 420 s->total_num_coded_frags += num_coded_frags;
638
2/2
✓ Branch 0 taken 26880 times.
✓ Branch 1 taken 420 times.
27300 for (int i = 0; i < 64; i++)
639 26880 s->num_coded_frags[plane][i] = num_coded_frags;
640
2/2
✓ Branch 0 taken 280 times.
✓ Branch 1 taken 140 times.
420 if (plane < 2)
641 280 s->coded_fragment_list[plane + 1] = s->coded_fragment_list[plane] +
642 num_coded_frags;
643 }
644 140 return 0;
645 }
646
647 #define BLOCK_X (2 * mb_x + (k & 1))
648 #define BLOCK_Y (2 * mb_y + (k >> 1))
649
650 #if CONFIG_VP4_DECODER
651 /**
652 * @return number of blocks, or > yuv_macroblock_count on error.
653 * return value is always >= 1.
654 */
655 7007 static int vp4_get_mb_count(Vp3DecodeContext *s, GetBitContext *gb)
656 {
657 7007 int v = 1;
658 int bits;
659
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7007 times.
7007 while ((bits = show_bits(gb, 9)) == 0x1ff) {
660 skip_bits(gb, 9);
661 v += 256;
662 if (v > s->yuv_macroblock_count) {
663 av_log(s->avctx, AV_LOG_ERROR, "Invalid run length\n");
664 return v;
665 }
666 }
667 #define body(n) { \
668 skip_bits(gb, 2 + n); \
669 v += (1 << n) + get_bits(gb, n); }
670 #define thresh(n) (0x200 - (0x80 >> n))
671 #define else_if(n) else if (bits < thresh(n)) body(n)
672
2/2
✓ Branch 0 taken 2784 times.
✓ Branch 1 taken 4223 times.
7007 if (bits < 0x100) {
673 2784 skip_bits(gb, 1);
674
2/2
✓ Branch 0 taken 1763 times.
✓ Branch 1 taken 2460 times.
4223 } else if (bits < thresh(0)) {
675 1763 skip_bits(gb, 2);
676 1763 v += 1;
677 }
678
2/2
✓ Branch 0 taken 994 times.
✓ Branch 1 taken 1466 times.
2460 else_if(1)
679
2/2
✓ Branch 0 taken 667 times.
✓ Branch 1 taken 799 times.
1466 else_if(2)
680
2/2
✓ Branch 0 taken 483 times.
✓ Branch 1 taken 316 times.
799 else_if(3)
681
2/2
✓ Branch 0 taken 224 times.
✓ Branch 1 taken 92 times.
316 else_if(4)
682
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 4 times.
92 else_if(5)
683
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 else_if(6)
684 else body(7)
685 #undef body
686 #undef thresh
687 #undef else_if
688 7007 return v;
689 }
690
691 4556 static int vp4_get_block_pattern(GetBitContext *gb, int *next_block_pattern_table)
692 {
693 4556 int v = get_vlc2(gb, block_pattern_vlc[*next_block_pattern_table], 5, 1);
694 4556 *next_block_pattern_table = vp4_block_pattern_table_selector[v];
695 4556 return v + 1;
696 }
697
698 24 static int vp4_unpack_macroblocks(Vp3DecodeContext *s, GetBitContext *gb)
699 {
700 int fragment;
701 int next_block_pattern_table;
702 int bit, current_run, has_partial;
703
704 24 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
705
706
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 22 times.
24 if (s->keyframe)
707 2 return 0;
708
709 22 has_partial = 0;
710 22 bit = get_bits1(gb);
711
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) {
712
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4132 times.
4132 if (get_bits_left(gb) <= 0)
713 return AVERROR_INVALIDDATA;
714 4132 current_run = vp4_get_mb_count(s, gb);
715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4132 times.
4132 if (current_run > s->yuv_macroblock_count - i)
716 return -1;
717 4132 memset(s->superblock_coding + i, 2 * bit, current_run);
718 4132 bit ^= 1;
719 4132 has_partial |= bit;
720 }
721
722
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (has_partial) {
723
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 if (get_bits_left(gb) <= 0)
724 return AVERROR_INVALIDDATA;
725 22 bit = get_bits1(gb);
726 22 current_run = vp4_get_mb_count(s, gb);
727
2/2
✓ Branch 0 taken 20064 times.
✓ Branch 1 taken 22 times.
20086 for (int i = 0; i < s->yuv_macroblock_count; i++) {
728
2/2
✓ Branch 0 taken 8403 times.
✓ Branch 1 taken 11661 times.
20064 if (!s->superblock_coding[i]) {
729
2/2
✓ Branch 0 taken 2853 times.
✓ Branch 1 taken 5550 times.
8403 if (!current_run) {
730 2853 bit ^= 1;
731 2853 current_run = vp4_get_mb_count(s, gb);
732 }
733 8403 s->superblock_coding[i] = bit;
734 8403 current_run--;
735 }
736 }
737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (current_run) /* handle situation when vp4_get_mb_count() fails */
738 return -1;
739 }
740
741 22 next_block_pattern_table = 0;
742
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 22 times.
88 for (int plane = 0, i = 0; plane < 3; plane++) {
743
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;
744
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;
745
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 22 times.
66 int mb_width = plane ? s->c_macroblock_width : s->macroblock_width;
746
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 22 times.
66 int mb_height = plane ? s->c_macroblock_height : s->macroblock_height;
747 66 int fragment_width = s->fragment_width[!!plane];
748 66 int fragment_height = s->fragment_height[!!plane];
749
750
2/2
✓ Branch 0 taken 352 times.
✓ Branch 1 taken 66 times.
418 for (int sb_y = 0; sb_y < sb_height; sb_y++) {
751
2/2
✓ Branch 0 taken 5104 times.
✓ Branch 1 taken 352 times.
5456 for (int sb_x = 0; sb_x < sb_width; sb_x++) {
752
2/2
✓ Branch 0 taken 20416 times.
✓ Branch 1 taken 5104 times.
25520 for (int j = 0; j < 4; j++) {
753 20416 int mb_x = 2 * sb_x + (j >> 1);
754 20416 int mb_y = 2 * sb_y + (j >> 1) ^ (j & 1);
755 int mb_coded, pattern, coded;
756
757
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)
758 352 continue;
759
760 20064 mb_coded = s->superblock_coding[i++];
761
762
2/2
✓ Branch 0 taken 11661 times.
✓ Branch 1 taken 8403 times.
20064 if (mb_coded == SB_FULLY_CODED)
763 11661 pattern = 0xF;
764
2/2
✓ Branch 0 taken 4556 times.
✓ Branch 1 taken 3847 times.
8403 else if (mb_coded == SB_PARTIALLY_CODED)
765 4556 pattern = vp4_get_block_pattern(gb, &next_block_pattern_table);
766 else
767 3847 pattern = 0;
768
769
2/2
✓ Branch 0 taken 80256 times.
✓ Branch 1 taken 20064 times.
100320 for (int k = 0; k < 4; k++) {
770
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)
771 continue;
772 80256 fragment = s->fragment_start[plane] + BLOCK_Y * fragment_width + BLOCK_X;
773 80256 coded = pattern & (8 >> k);
774 /* MODE_INTER_NO_MV is the default for coded fragments.
775 the actual method is decoded in the next phase. */
776
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;
777 }
778 }
779 }
780 }
781 }
782 22 return 0;
783 }
784 #endif
785
786 /*
787 * This function unpacks all the coding mode data for individual macroblocks
788 * from the bitstream.
789 */
790 164 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
791 {
792 int scheme;
793 int current_macroblock;
794 int current_fragment;
795 int coding_mode;
796 int custom_mode_alphabet[CODING_MODE_COUNT];
797 const int *alphabet;
798 Vp3Fragment *frag;
799
800
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 158 times.
164 if (s->keyframe) {
801
2/2
✓ Branch 0 taken 169788 times.
✓ Branch 1 taken 6 times.
169794 for (int i = 0; i < s->fragment_count; i++)
802 169788 s->all_fragments[i].coding_method = MODE_INTRA;
803 } else {
804 /* fetch the mode coding scheme for this frame */
805 158 scheme = get_bits(gb, 3);
806
807 /* is it a custom coding scheme? */
808
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 135 times.
158 if (scheme == 0) {
809
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 23 times.
207 for (int i = 0; i < 8; i++)
810 184 custom_mode_alphabet[i] = MODE_INTER_NO_MV;
811
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 23 times.
207 for (int i = 0; i < 8; i++)
812 184 custom_mode_alphabet[get_bits(gb, 3)] = i;
813 23 alphabet = custom_mode_alphabet;
814 } else
815 135 alphabet = ModeAlphabet[scheme - 1];
816
817 /* iterate through all of the macroblocks that contain 1 or more
818 * coded fragments */
819
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++) {
820
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++) {
821
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72050 times.
72050 if (get_bits_left(gb) <= 0)
822 return -1;
823
824
2/2
✓ Branch 0 taken 288200 times.
✓ Branch 1 taken 72050 times.
360250 for (int j = 0; j < 4; j++) {
825 int k;
826 288200 int mb_x = 2 * sb_x + (j >> 1);
827 288200 int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
828 288200 current_macroblock = mb_y * s->macroblock_width + mb_x;
829
830
2/2
✓ Branch 0 taken 287276 times.
✓ Branch 1 taken 924 times.
288200 if (mb_x >= s->macroblock_width ||
831
2/2
✓ Branch 0 taken 7542 times.
✓ Branch 1 taken 279734 times.
287276 mb_y >= s->macroblock_height)
832 8466 continue;
833
834 /* coding modes are only stored if the macroblock has
835 * at least one luma block coded, otherwise it must be
836 * INTER_NO_MV */
837
2/2
✓ Branch 0 taken 878591 times.
✓ Branch 1 taken 195385 times.
1073976 for (k = 0; k < 4; k++) {
838 878591 current_fragment = BLOCK_Y *
839 878591 s->fragment_width[0] + BLOCK_X;
840
2/2
✓ Branch 0 taken 84349 times.
✓ Branch 1 taken 794242 times.
878591 if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
841 84349 break;
842 }
843
2/2
✓ Branch 0 taken 195385 times.
✓ Branch 1 taken 84349 times.
279734 if (k == 4) {
844 195385 s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
845 195385 continue;
846 }
847
848 /* mode 7 means get 3 bits for each coding mode */
849
2/2
✓ Branch 0 taken 2750 times.
✓ Branch 1 taken 81599 times.
84349 if (scheme == 7)
850 2750 coding_mode = get_bits(gb, 3);
851 else
852 81599 coding_mode = alphabet[get_vlc2(gb, mode_code_vlc, 4, 2)];
853
854 84349 s->macroblock_coding[current_macroblock] = coding_mode;
855
2/2
✓ Branch 0 taken 337396 times.
✓ Branch 1 taken 84349 times.
421745 for (k = 0; k < 4; k++) {
856 337396 frag = s->all_fragments + BLOCK_Y * s->fragment_width[0] + BLOCK_X;
857
2/2
✓ Branch 0 taken 307251 times.
✓ Branch 1 taken 30145 times.
337396 if (frag->coding_method != MODE_COPY)
858 307251 frag->coding_method = coding_mode;
859 }
860
861 #define SET_CHROMA_MODES \
862 if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
863 frag[s->fragment_start[1]].coding_method = coding_mode; \
864 if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
865 frag[s->fragment_start[2]].coding_method = coding_mode;
866
867
1/2
✓ Branch 0 taken 84349 times.
✗ Branch 1 not taken.
84349 if (s->chroma_y_shift) {
868 84349 frag = s->all_fragments + mb_y *
869 84349 s->fragment_width[1] + mb_x;
870
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
871 } else if (s->chroma_x_shift) {
872 frag = s->all_fragments +
873 2 * mb_y * s->fragment_width[1] + mb_x;
874 for (k = 0; k < 2; k++) {
875 SET_CHROMA_MODES
876 frag += s->fragment_width[1];
877 }
878 } else {
879 for (k = 0; k < 4; k++) {
880 frag = s->all_fragments +
881 BLOCK_Y * s->fragment_width[1] + BLOCK_X;
882 SET_CHROMA_MODES
883 }
884 }
885 }
886 }
887 }
888 }
889
890 164 return 0;
891 }
892
893 3758 static int vp4_get_mv(GetBitContext *gb, int axis, int last_motion)
894 {
895 #if CONFIG_VP4_DECODER
896 3758 int v = get_vlc2(gb, vp4_mv_vlc_table[axis][vp4_mv_table_selector[FFABS(last_motion)]],
897 VP4_MV_VLC_BITS, 2);
898
2/2
✓ Branch 0 taken 1082 times.
✓ Branch 1 taken 2676 times.
3758 return last_motion < 0 ? -v : v;
899 #else
900 return 0;
901 #endif
902 }
903
904 /*
905 * This function unpacks all the motion vectors for the individual
906 * macroblocks from the bitstream.
907 */
908 164 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
909 {
910 int coding_mode;
911 int motion_x[4];
912 int motion_y[4];
913 164 int last_motion_x = 0;
914 164 int last_motion_y = 0;
915 164 int prior_last_motion_x = 0;
916 164 int prior_last_motion_y = 0;
917 164 int last_gold_motion_x = 0;
918 164 int last_gold_motion_y = 0;
919 int current_macroblock;
920 int current_fragment;
921 int frag;
922
923
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 158 times.
164 if (s->keyframe)
924 6 return 0;
925
926 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme; 2 is VP4 code scheme */
927
2/2
✓ Branch 0 taken 136 times.
✓ Branch 1 taken 22 times.
158 coding_mode = s->version < 2 ? get_bits1(gb) : 2;
928
929 /* iterate through all of the macroblocks that contain 1 or more
930 * coded fragments */
931
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++) {
932
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++) {
933
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72050 times.
72050 if (get_bits_left(gb) <= 0)
934 return -1;
935
936
2/2
✓ Branch 0 taken 288200 times.
✓ Branch 1 taken 72050 times.
360250 for (int j = 0; j < 4; j++) {
937 288200 int mb_x = 2 * sb_x + (j >> 1);
938 288200 int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
939 288200 current_macroblock = mb_y * s->macroblock_width + mb_x;
940
941
2/2
✓ Branch 0 taken 287276 times.
✓ Branch 1 taken 924 times.
288200 if (mb_x >= s->macroblock_width ||
942
2/2
✓ Branch 0 taken 279734 times.
✓ Branch 1 taken 7542 times.
287276 mb_y >= s->macroblock_height ||
943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 279734 times.
279734 s->macroblock_coding[current_macroblock] == MODE_COPY)
944 8466 continue;
945
946
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]) {
947 711 case MODE_GOLDEN_MV:
948
2/2
✓ Branch 0 taken 337 times.
✓ Branch 1 taken 374 times.
711 if (coding_mode == 2) { /* VP4 */
949 337 last_gold_motion_x = motion_x[0] = vp4_get_mv(gb, 0, last_gold_motion_x);
950 337 last_gold_motion_y = motion_y[0] = vp4_get_mv(gb, 1, last_gold_motion_y);
951 337 break;
952 } /* otherwise fall through */
953 case MODE_INTER_PLUS_MV:
954 /* all 6 fragments use the same motion vector */
955
2/2
✓ Branch 0 taken 13074 times.
✓ Branch 1 taken 1484 times.
14558 if (coding_mode == 0) {
956 13074 motion_x[0] = get_vlc2(gb, motion_vector_vlc,
957 VP3_MV_VLC_BITS, 2);
958 13074 motion_y[0] = get_vlc2(gb, motion_vector_vlc,
959 VP3_MV_VLC_BITS, 2);
960
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 1442 times.
1484 } else if (coding_mode == 1) {
961 42 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
962 42 motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
963 } else { /* VP4 */
964 1442 motion_x[0] = vp4_get_mv(gb, 0, last_motion_x);
965 1442 motion_y[0] = vp4_get_mv(gb, 1, last_motion_y);
966 }
967
968 /* vector maintenance, only on MODE_INTER_PLUS_MV */
969
2/2
✓ Branch 0 taken 14184 times.
✓ Branch 1 taken 374 times.
14558 if (s->macroblock_coding[current_macroblock] == MODE_INTER_PLUS_MV) {
970 14184 prior_last_motion_x = last_motion_x;
971 14184 prior_last_motion_y = last_motion_y;
972 14184 last_motion_x = motion_x[0];
973 14184 last_motion_y = motion_y[0];
974 }
975 14558 break;
976
977 63 case MODE_INTER_FOURMV:
978 /* vector maintenance */
979 63 prior_last_motion_x = last_motion_x;
980 63 prior_last_motion_y = last_motion_y;
981
982 /* fetch 4 vectors from the bitstream, one for each
983 * Y fragment, then average for the C fragment vectors */
984
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 63 times.
315 for (int k = 0; k < 4; k++) {
985 252 current_fragment = BLOCK_Y * s->fragment_width[0] + BLOCK_X;
986
1/2
✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
252 if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
987
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 204 times.
252 if (coding_mode == 0) {
988 48 motion_x[k] = get_vlc2(gb, motion_vector_vlc,
989 VP3_MV_VLC_BITS, 2);
990 48 motion_y[k] = get_vlc2(gb, motion_vector_vlc,
991 VP3_MV_VLC_BITS, 2);
992
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 100 times.
204 } else if (coding_mode == 1) {
993 104 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
994 104 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
995 } else { /* VP4 */
996 100 motion_x[k] = vp4_get_mv(gb, 0, prior_last_motion_x);
997 100 motion_y[k] = vp4_get_mv(gb, 1, prior_last_motion_y);
998 }
999 252 last_motion_x = motion_x[k];
1000 252 last_motion_y = motion_y[k];
1001 } else {
1002 motion_x[k] = 0;
1003 motion_y[k] = 0;
1004 }
1005 }
1006 63 break;
1007
1008 42307 case MODE_INTER_LAST_MV:
1009 /* all 6 fragments use the last motion vector */
1010 42307 motion_x[0] = last_motion_x;
1011 42307 motion_y[0] = last_motion_y;
1012
1013 /* no vector maintenance (last vector remains the
1014 * last vector) */
1015 42307 break;
1016
1017 11934 case MODE_INTER_PRIOR_LAST:
1018 /* all 6 fragments use the motion vector prior to the
1019 * last motion vector */
1020 11934 motion_x[0] = prior_last_motion_x;
1021 11934 motion_y[0] = prior_last_motion_y;
1022
1023 /* vector maintenance */
1024 11934 prior_last_motion_x = last_motion_x;
1025 11934 prior_last_motion_y = last_motion_y;
1026 11934 last_motion_x = motion_x[0];
1027 11934 last_motion_y = motion_y[0];
1028 11934 break;
1029
1030 210535 default:
1031 /* covers intra, inter without MV, golden without MV */
1032 210535 motion_x[0] = 0;
1033 210535 motion_y[0] = 0;
1034
1035 /* no vector maintenance */
1036 210535 break;
1037 }
1038
1039 /* assign the motion vectors to the correct fragments */
1040
2/2
✓ Branch 0 taken 1118936 times.
✓ Branch 1 taken 279734 times.
1398670 for (int k = 0; k < 4; k++) {
1041 1118936 current_fragment =
1042 1118936 BLOCK_Y * s->fragment_width[0] + BLOCK_X;
1043
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 1118684 times.
1118936 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1044 252 s->motion_val[0][current_fragment][0] = motion_x[k];
1045 252 s->motion_val[0][current_fragment][1] = motion_y[k];
1046 } else {
1047 1118684 s->motion_val[0][current_fragment][0] = motion_x[0];
1048 1118684 s->motion_val[0][current_fragment][1] = motion_y[0];
1049 }
1050 }
1051
1052
1/2
✓ Branch 0 taken 279734 times.
✗ Branch 1 not taken.
279734 if (s->chroma_y_shift) {
1053
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 279671 times.
279734 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1054
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 32 times.
63 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] +
1055 motion_x[2] + motion_x[3], 2);
1056
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 28 times.
63 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] +
1057 motion_y[2] + motion_y[3], 2);
1058 }
1059
2/2
✓ Branch 0 taken 266358 times.
✓ Branch 1 taken 13376 times.
279734 if (s->version <= 2) {
1060 266358 motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
1061 266358 motion_y[0] = (motion_y[0] >> 1) | (motion_y[0] & 1);
1062 }
1063 279734 frag = mb_y * s->fragment_width[1] + mb_x;
1064 279734 s->motion_val[1][frag][0] = motion_x[0];
1065 279734 s->motion_val[1][frag][1] = motion_y[0];
1066 } else if (s->chroma_x_shift) {
1067 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1068 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
1069 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
1070 motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
1071 motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
1072 } else {
1073 motion_x[1] = motion_x[0];
1074 motion_y[1] = motion_y[0];
1075 }
1076 if (s->version <= 2) {
1077 motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
1078 motion_x[1] = (motion_x[1] >> 1) | (motion_x[1] & 1);
1079 }
1080 frag = 2 * mb_y * s->fragment_width[1] + mb_x;
1081 for (int k = 0; k < 2; k++) {
1082 s->motion_val[1][frag][0] = motion_x[k];
1083 s->motion_val[1][frag][1] = motion_y[k];
1084 frag += s->fragment_width[1];
1085 }
1086 } else {
1087 for (int k = 0; k < 4; k++) {
1088 frag = BLOCK_Y * s->fragment_width[1] + BLOCK_X;
1089 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1090 s->motion_val[1][frag][0] = motion_x[k];
1091 s->motion_val[1][frag][1] = motion_y[k];
1092 } else {
1093 s->motion_val[1][frag][0] = motion_x[0];
1094 s->motion_val[1][frag][1] = motion_y[0];
1095 }
1096 }
1097 }
1098 }
1099 }
1100 }
1101
1102 158 return 0;
1103 }
1104
1105 164 static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
1106 {
1107 164 int num_blocks = s->total_num_coded_frags;
1108
1109
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++) {
1110 int i = 0, blocks_decoded = 0, num_blocks_at_qpi = 0;
1111 int bit, run_length;
1112
1113 bit = get_bits1(gb) ^ 1;
1114 run_length = 0;
1115
1116 do {
1117 if (run_length == MAXIMUM_LONG_BIT_RUN)
1118 bit = get_bits1(gb);
1119 else
1120 bit ^= 1;
1121
1122 run_length = get_vlc2(gb, superblock_run_length_vlc,
1123 SUPERBLOCK_VLC_BITS, 2);
1124 if (run_length == 34)
1125 run_length += get_bits(gb, 12);
1126 blocks_decoded += run_length;
1127
1128 if (!bit)
1129 num_blocks_at_qpi += run_length;
1130
1131 for (int j = 0; j < run_length; i++) {
1132 if (i >= s->total_num_coded_frags)
1133 return -1;
1134
1135 if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
1136 s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
1137 j++;
1138 }
1139 }
1140 } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
1141
1142 num_blocks -= num_blocks_at_qpi;
1143 }
1144
1145 164 return 0;
1146 }
1147
1148 259052 static inline int get_eob_run(GetBitContext *gb, int token)
1149 {
1150 259052 int v = eob_run_table[token].base;
1151
2/2
✓ Branch 0 taken 33652 times.
✓ Branch 1 taken 225400 times.
259052 if (eob_run_table[token].bits)
1152 33652 v += get_bits(gb, eob_run_table[token].bits);
1153 259052 return v;
1154 }
1155
1156 2755489 static inline int get_coeff(GetBitContext *gb, int token, int16_t *coeff)
1157 {
1158 int bits_to_get, zero_run;
1159
1160 2755489 bits_to_get = coeff_get_bits[token];
1161
2/2
✓ Branch 0 taken 1617513 times.
✓ Branch 1 taken 1137976 times.
2755489 if (bits_to_get)
1162 1617513 bits_to_get = get_bits(gb, bits_to_get);
1163 2755489 *coeff = coeff_tables[token][bits_to_get];
1164
1165 2755489 zero_run = zero_run_base[token];
1166
2/2
✓ Branch 0 taken 342682 times.
✓ Branch 1 taken 2412807 times.
2755489 if (zero_run_get_bits[token])
1167 342682 zero_run += get_bits(gb, zero_run_get_bits[token]);
1168
1169 2755489 return zero_run;
1170 }
1171
1172 /*
1173 * This function is called by unpack_dct_coeffs() to extract the VLCs from
1174 * the bitstream. The VLCs encode tokens which are used to unpack DCT
1175 * data. This function unpacks all the VLCs for either the Y plane or both
1176 * C planes, and is called for DC coefficients or different AC coefficient
1177 * levels (since different coefficient types require different VLC tables.
1178 *
1179 * This function returns a residual eob run. E.g, if a particular token gave
1180 * instructions to EOB the next 5 fragments and there were only 2 fragments
1181 * left in the current fragment range, 3 would be returned so that it could
1182 * be passed into the next call to this same function.
1183 */
1184 26880 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1185 const VLCElem *vlc_table, int coeff_index,
1186 int plane,
1187 int eob_run)
1188 {
1189 26880 int j = 0;
1190 int token;
1191 26880 int zero_run = 0;
1192 26880 int16_t coeff = 0;
1193 int blocks_ended;
1194 26880 int coeff_i = 0;
1195 26880 int num_coeffs = s->num_coded_frags[plane][coeff_index];
1196 26880 int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
1197
1198 /* local references to structure members to avoid repeated dereferences */
1199 26880 const int *coded_fragment_list = s->coded_fragment_list[plane];
1200 26880 Vp3Fragment *all_fragments = s->all_fragments;
1201
1202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26880 times.
26880 if (num_coeffs < 0) {
1203 av_log(s->avctx, AV_LOG_ERROR,
1204 "Invalid number of coefficients at level %d\n", coeff_index);
1205 return AVERROR_INVALIDDATA;
1206 }
1207
1208
2/2
✓ Branch 0 taken 3487 times.
✓ Branch 1 taken 23393 times.
26880 if (eob_run > num_coeffs) {
1209 3487 coeff_i =
1210 3487 blocks_ended = num_coeffs;
1211 3487 eob_run -= num_coeffs;
1212 } else {
1213 23393 coeff_i =
1214 23393 blocks_ended = eob_run;
1215 23393 eob_run = 0;
1216 }
1217
1218 // insert fake EOB token to cover the split between planes or zzi
1219
2/2
✓ Branch 0 taken 2019 times.
✓ Branch 1 taken 24861 times.
26880 if (blocks_ended)
1220 2019 dct_tokens[j++] = blocks_ended << 2;
1221
1222
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) {
1223 /* decode a VLC into a token */
1224 2060240 token = get_vlc2(gb, vlc_table, 11, 3);
1225 /* use the token to get a zero run, a coefficient, and an eob run */
1226
2/2
✓ Branch 0 taken 220061 times.
✓ Branch 1 taken 1840179 times.
2060240 if ((unsigned) token <= 6U) {
1227 220061 eob_run = get_eob_run(gb, token);
1228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 220061 times.
220061 if (!eob_run)
1229 eob_run = INT_MAX;
1230
1231 // record only the number of blocks ended in this plane,
1232 // any spill will be recorded in the next plane.
1233
2/2
✓ Branch 0 taken 1656 times.
✓ Branch 1 taken 218405 times.
220061 if (eob_run > num_coeffs - coeff_i) {
1234 1656 dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
1235 1656 blocks_ended += num_coeffs - coeff_i;
1236 1656 eob_run -= num_coeffs - coeff_i;
1237 1656 coeff_i = num_coeffs;
1238 } else {
1239 218405 dct_tokens[j++] = TOKEN_EOB(eob_run);
1240 218405 blocks_ended += eob_run;
1241 218405 coeff_i += eob_run;
1242 218405 eob_run = 0;
1243 }
1244
1/2
✓ Branch 0 taken 1840179 times.
✗ Branch 1 not taken.
1840179 } else if (token >= 0) {
1245 1840179 zero_run = get_coeff(gb, token, &coeff);
1246
1247
2/2
✓ Branch 0 taken 601916 times.
✓ Branch 1 taken 1238263 times.
1840179 if (zero_run) {
1248 601916 dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
1249 } else {
1250 // Save DC into the fragment structure. DC prediction is
1251 // done in raster order, so the actual DC can't be in with
1252 // other tokens. We still need the token in dct_tokens[]
1253 // however, or else the structure collapses on itself.
1254
2/2
✓ Branch 0 taken 241967 times.
✓ Branch 1 taken 996296 times.
1238263 if (!coeff_index)
1255 241967 all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
1256
1257 1238263 dct_tokens[j++] = TOKEN_COEFF(coeff);
1258 }
1259
1260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1840179 times.
1840179 if (coeff_index + zero_run > 64) {
1261 av_log(s->avctx, AV_LOG_DEBUG,
1262 "Invalid zero run of %d with %d coeffs left\n",
1263 zero_run, 64 - coeff_index);
1264 zero_run = 64 - coeff_index;
1265 }
1266
1267 // zero runs code multiple coefficients,
1268 // so don't try to decode coeffs for those higher levels
1269
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++)
1270 2541089 s->num_coded_frags[plane][i]--;
1271 1840179 coeff_i++;
1272 } else {
1273 av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1274 return -1;
1275 }
1276 }
1277
1278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26880 times.
26880 if (blocks_ended > s->num_coded_frags[plane][coeff_index])
1279 av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
1280
1281 // decrement the number of blocks that have higher coefficients for each
1282 // EOB run at this level
1283
2/2
✓ Branch 0 taken 8292 times.
✓ Branch 1 taken 18588 times.
26880 if (blocks_ended)
1284
2/2
✓ Branch 0 taken 372769 times.
✓ Branch 1 taken 8292 times.
381061 for (int i = coeff_index + 1; i < 64; i++)
1285 372769 s->num_coded_frags[plane][i] -= blocks_ended;
1286
1287 // setup the next buffer
1288
2/2
✓ Branch 0 taken 17920 times.
✓ Branch 1 taken 8960 times.
26880 if (plane < 2)
1289 17920 s->dct_tokens[plane + 1][coeff_index] = dct_tokens + j;
1290
2/2
✓ Branch 0 taken 8820 times.
✓ Branch 1 taken 140 times.
8960 else if (coeff_index < 63)
1291 8820 s->dct_tokens[0][coeff_index + 1] = dct_tokens + j;
1292
1293 26880 return eob_run;
1294 }
1295
1296 static void reverse_dc_prediction(Vp3DecodeContext *s,
1297 int first_fragment,
1298 int fragment_width,
1299 int fragment_height);
1300 /*
1301 * This function unpacks all of the DCT coefficient data from the
1302 * bitstream.
1303 */
1304 140 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1305 {
1306 140 const VLCElem *const *coeff_vlc = s->coeff_vlc->vlc_tabs;
1307 int dc_y_table;
1308 int dc_c_table;
1309 int ac_y_table;
1310 int ac_c_table;
1311 140 int residual_eob_run = 0;
1312 const VLCElem *y_tables[64], *c_tables[64];
1313
1314 140 s->dct_tokens[0][0] = s->dct_tokens_base;
1315
1316
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
140 if (get_bits_left(gb) < 16)
1317 return AVERROR_INVALIDDATA;
1318
1319 /* fetch the DC table indexes */
1320 140 dc_y_table = get_bits(gb, 4);
1321 140 dc_c_table = get_bits(gb, 4);
1322
1323 /* unpack the Y plane DC coefficients */
1324 140 residual_eob_run = unpack_vlcs(s, gb, coeff_vlc[dc_y_table], 0,
1325 0, residual_eob_run);
1326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140 times.
140 if (residual_eob_run < 0)
1327 return residual_eob_run;
1328
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
140 if (get_bits_left(gb) < 8)
1329 return AVERROR_INVALIDDATA;
1330
1331 /* reverse prediction of the Y-plane DC coefficients */
1332 140 reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
1333
1334 /* unpack the C plane DC coefficients */
1335 140 residual_eob_run = unpack_vlcs(s, gb, coeff_vlc[dc_c_table], 0,
1336 1, residual_eob_run);
1337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140 times.
140 if (residual_eob_run < 0)
1338 return residual_eob_run;
1339 140 residual_eob_run = unpack_vlcs(s, gb, coeff_vlc[dc_c_table], 0,
1340 2, residual_eob_run);
1341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140 times.
140 if (residual_eob_run < 0)
1342 return residual_eob_run;
1343
1344 /* reverse prediction of the C-plane DC coefficients */
1345
1/2
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
140 if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1346 140 reverse_dc_prediction(s, s->fragment_start[1],
1347 s->fragment_width[1], s->fragment_height[1]);
1348 140 reverse_dc_prediction(s, s->fragment_start[2],
1349 s->fragment_width[1], s->fragment_height[1]);
1350 }
1351
1352
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
140 if (get_bits_left(gb) < 8)
1353 return AVERROR_INVALIDDATA;
1354 /* fetch the AC table indexes */
1355 140 ac_y_table = get_bits(gb, 4);
1356 140 ac_c_table = get_bits(gb, 4);
1357
1358 /* build tables of AC VLC tables */
1359
2/2
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 140 times.
840 for (int i = 1; i <= 5; i++) {
1360 /* AC VLC table group 1 */
1361 700 y_tables[i] = coeff_vlc[ac_y_table + 16];
1362 700 c_tables[i] = coeff_vlc[ac_c_table + 16];
1363 }
1364
2/2
✓ Branch 0 taken 1260 times.
✓ Branch 1 taken 140 times.
1400 for (int i = 6; i <= 14; i++) {
1365 /* AC VLC table group 2 */
1366 1260 y_tables[i] = coeff_vlc[ac_y_table + 32];
1367 1260 c_tables[i] = coeff_vlc[ac_c_table + 32];
1368 }
1369
2/2
✓ Branch 0 taken 1820 times.
✓ Branch 1 taken 140 times.
1960 for (int i = 15; i <= 27; i++) {
1370 /* AC VLC table group 3 */
1371 1820 y_tables[i] = coeff_vlc[ac_y_table + 48];
1372 1820 c_tables[i] = coeff_vlc[ac_c_table + 48];
1373 }
1374
2/2
✓ Branch 0 taken 5040 times.
✓ Branch 1 taken 140 times.
5180 for (int i = 28; i <= 63; i++) {
1375 /* AC VLC table group 4 */
1376 5040 y_tables[i] = coeff_vlc[ac_y_table + 64];
1377 5040 c_tables[i] = coeff_vlc[ac_c_table + 64];
1378 }
1379
1380 /* decode all AC coefficients */
1381
2/2
✓ Branch 0 taken 8820 times.
✓ Branch 1 taken 140 times.
8960 for (int i = 1; i <= 63; i++) {
1382 8820 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
1383 0, 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
1387 8820 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1388 1, residual_eob_run);
1389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8820 times.
8820 if (residual_eob_run < 0)
1390 return residual_eob_run;
1391 8820 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1392 2, residual_eob_run);
1393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8820 times.
8820 if (residual_eob_run < 0)
1394 return residual_eob_run;
1395 }
1396
1397 140 return 0;
1398 }
1399
1400 #if CONFIG_VP4_DECODER
1401 /**
1402 * eob_tracker[] is instead of TOKEN_EOB(value)
1403 * a dummy TOKEN_EOB(0) value is used to make vp3_dequant work
1404 *
1405 * @return < 0 on error
1406 */
1407 63252 static int vp4_unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1408 const VLCElem *const vlc_tables[64],
1409 int plane, int eob_tracker[64], int fragment)
1410 {
1411 int token;
1412 63252 int zero_run = 0;
1413 63252 int16_t coeff = 0;
1414 63252 int coeff_i = 0;
1415 int eob_run;
1416
1417
2/2
✓ Branch 0 taken 954301 times.
✓ Branch 1 taken 19171 times.
973472 while (!eob_tracker[coeff_i]) {
1418
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 954301 times.
954301 if (get_bits_left(gb) < 1)
1419 return AVERROR_INVALIDDATA;
1420
1421 954301 token = get_vlc2(gb, vlc_tables[coeff_i], 11, 3);
1422
1423 /* use the token to get a zero run, a coefficient, and an eob run */
1424
2/2
✓ Branch 0 taken 38991 times.
✓ Branch 1 taken 915310 times.
954301 if ((unsigned) token <= 6U) {
1425 38991 eob_run = get_eob_run(gb, token);
1426 38991 *s->dct_tokens[plane][coeff_i]++ = TOKEN_EOB(0);
1427 38991 eob_tracker[coeff_i] = eob_run - 1;
1428 38991 return 0;
1429
1/2
✓ Branch 0 taken 915310 times.
✗ Branch 1 not taken.
915310 } else if (token >= 0) {
1430 915310 zero_run = get_coeff(gb, token, &coeff);
1431
1432
2/2
✓ Branch 0 taken 311952 times.
✓ Branch 1 taken 603358 times.
915310 if (zero_run) {
1433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 311952 times.
311952 if (coeff_i + zero_run > 64) {
1434 av_log(s->avctx, AV_LOG_DEBUG,
1435 "Invalid zero run of %d with %d coeffs left\n",
1436 zero_run, 64 - coeff_i);
1437 zero_run = 64 - coeff_i;
1438 }
1439 311952 *s->dct_tokens[plane][coeff_i]++ = TOKEN_ZERO_RUN(coeff, zero_run);
1440 311952 coeff_i += zero_run;
1441 } else {
1442
2/2
✓ Branch 0 taken 40795 times.
✓ Branch 1 taken 562563 times.
603358 if (!coeff_i)
1443 40795 s->all_fragments[fragment].dc = coeff;
1444
1445 603358 *s->dct_tokens[plane][coeff_i]++ = TOKEN_COEFF(coeff);
1446 }
1447 915310 coeff_i++;
1448
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 */
1449 5090 return 0; /* stop */
1450 } else {
1451 av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1452 return -1;
1453 }
1454 }
1455 19171 *s->dct_tokens[plane][coeff_i]++ = TOKEN_EOB(0);
1456 19171 eob_tracker[coeff_i]--;
1457 19171 return 0;
1458 }
1459
1460 95328 static void vp4_dc_predictor_reset(VP4Predictor *p)
1461 {
1462 95328 p->dc = 0;
1463 95328 p->type = VP4_DC_UNDEFINED;
1464 95328 }
1465
1466 5568 static void vp4_dc_pred_before(const Vp3DecodeContext *s, VP4Predictor dc_pred[6][6], int sb_x)
1467 {
1468
2/2
✓ Branch 0 taken 22272 times.
✓ Branch 1 taken 5568 times.
27840 for (int i = 0; i < 4; i++)
1469 22272 dc_pred[0][i + 1] = s->dc_pred_row[sb_x * 4 + i];
1470
1471
2/2
✓ Branch 0 taken 22272 times.
✓ Branch 1 taken 5568 times.
27840 for (int j = 1; j < 5; j++)
1472
2/2
✓ Branch 0 taken 89088 times.
✓ Branch 1 taken 22272 times.
111360 for (int i = 0; i < 4; i++)
1473 89088 vp4_dc_predictor_reset(&dc_pred[j][i + 1]);
1474 5568 }
1475
1476 5568 static void vp4_dc_pred_after(Vp3DecodeContext *s, VP4Predictor dc_pred[6][6], int sb_x)
1477 {
1478
2/2
✓ Branch 0 taken 22272 times.
✓ Branch 1 taken 5568 times.
27840 for (int i = 0; i < 4; i++)
1479 22272 s->dc_pred_row[sb_x * 4 + i] = dc_pred[4][i + 1];
1480
1481
2/2
✓ Branch 0 taken 22272 times.
✓ Branch 1 taken 5568 times.
27840 for (int i = 1; i < 5; i++)
1482 22272 dc_pred[i][0] = dc_pred[i][4];
1483 5568 }
1484
1485 /* note: dc_pred points to the current block */
1486 63252 static int vp4_dc_pred(const Vp3DecodeContext *s, const VP4Predictor * dc_pred, const int * last_dc, int type, int plane)
1487 {
1488 63252 int count = 0;
1489 63252 int dc = 0;
1490
1491
2/2
✓ Branch 0 taken 29726 times.
✓ Branch 1 taken 33526 times.
63252 if (dc_pred[-6].type == type) {
1492 29726 dc += dc_pred[-6].dc;
1493 29726 count++;
1494 }
1495
1496
2/2
✓ Branch 0 taken 19581 times.
✓ Branch 1 taken 43671 times.
63252 if (dc_pred[6].type == type) {
1497 19581 dc += dc_pred[6].dc;
1498 19581 count++;
1499 }
1500
1501
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) {
1502 38121 dc += dc_pred[-1].dc;
1503 38121 count++;
1504 }
1505
1506
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) {
1507 2073 dc += dc_pred[1].dc;
1508 2073 count++;
1509 }
1510
1511 /* using division instead of shift to correctly handle negative values */
1512
2/2
✓ Branch 0 taken 32080 times.
✓ Branch 1 taken 31172 times.
63252 return count == 2 ? dc / 2 : last_dc[type];
1513 }
1514
1515 48 static void vp4_set_tokens_base(Vp3DecodeContext *s)
1516 {
1517 48 int16_t *base = s->dct_tokens_base;
1518
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 48 times.
192 for (int plane = 0; plane < 3; plane++) {
1519
2/2
✓ Branch 0 taken 9216 times.
✓ Branch 1 taken 144 times.
9360 for (int i = 0; i < 64; i++) {
1520 9216 s->dct_tokens[plane][i] = base;
1521 9216 base += s->fragment_width[!!plane] * s->fragment_height[!!plane];
1522 }
1523 }
1524 48 }
1525
1526 24 static int vp4_unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1527 {
1528 24 const VLCElem *const *coeff_vlc = s->coeff_vlc->vlc_tabs;
1529 int dc_y_table;
1530 int dc_c_table;
1531 int ac_y_table;
1532 int ac_c_table;
1533 const VLCElem *tables[2][64];
1534 int eob_tracker[64];
1535 VP4Predictor dc_pred[6][6];
1536 int last_dc[NB_VP4_DC_TYPES];
1537
1538
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if (get_bits_left(gb) < 16)
1539 return AVERROR_INVALIDDATA;
1540
1541 /* fetch the DC table indexes */
1542 24 dc_y_table = get_bits(gb, 4);
1543 24 dc_c_table = get_bits(gb, 4);
1544
1545 24 ac_y_table = get_bits(gb, 4);
1546 24 ac_c_table = get_bits(gb, 4);
1547
1548 /* build tables of DC/AC VLC tables */
1549
1550 /* DC table group */
1551 24 tables[0][0] = coeff_vlc[dc_y_table];
1552 24 tables[1][0] = coeff_vlc[dc_c_table];
1553
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 24 times.
144 for (int i = 1; i <= 5; i++) {
1554 /* AC VLC table group 1 */
1555 120 tables[0][i] = coeff_vlc[ac_y_table + 16];
1556 120 tables[1][i] = coeff_vlc[ac_c_table + 16];
1557 }
1558
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 24 times.
240 for (int i = 6; i <= 14; i++) {
1559 /* AC VLC table group 2 */
1560 216 tables[0][i] = coeff_vlc[ac_y_table + 32];
1561 216 tables[1][i] = coeff_vlc[ac_c_table + 32];
1562 }
1563
2/2
✓ Branch 0 taken 312 times.
✓ Branch 1 taken 24 times.
336 for (int i = 15; i <= 27; i++) {
1564 /* AC VLC table group 3 */
1565 312 tables[0][i] = coeff_vlc[ac_y_table + 48];
1566 312 tables[1][i] = coeff_vlc[ac_c_table + 48];
1567 }
1568
2/2
✓ Branch 0 taken 864 times.
✓ Branch 1 taken 24 times.
888 for (int i = 28; i <= 63; i++) {
1569 /* AC VLC table group 4 */
1570 864 tables[0][i] = coeff_vlc[ac_y_table + 64];
1571 864 tables[1][i] = coeff_vlc[ac_c_table + 64];
1572 }
1573
1574 24 vp4_set_tokens_base(s);
1575
1576 24 memset(last_dc, 0, sizeof(last_dc));
1577
1578
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++) {
1579 72 memset(eob_tracker, 0, sizeof(eob_tracker));
1580
1581 /* initialise dc prediction */
1582
2/2
✓ Branch 0 taken 3648 times.
✓ Branch 1 taken 72 times.
3720 for (int i = 0; i < s->fragment_width[!!plane]; i++)
1583 3648 vp4_dc_predictor_reset(&s->dc_pred_row[i]);
1584
1585
2/2
✓ Branch 0 taken 432 times.
✓ Branch 1 taken 72 times.
504 for (int j = 0; j < 6; j++)
1586
2/2
✓ Branch 0 taken 2592 times.
✓ Branch 1 taken 432 times.
3024 for (int i = 0; i < 6; i++)
1587 2592 vp4_dc_predictor_reset(&dc_pred[j][i]);
1588
1589
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++) {
1590
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++) {
1591 5568 vp4_dc_pred_before(s, dc_pred, sb_x);
1592
2/2
✓ Branch 0 taken 89088 times.
✓ Branch 1 taken 5568 times.
94656 for (int j = 0; j < 16; j++) {
1593 89088 int hx = hilbert_offset[j][0];
1594 89088 int hy = hilbert_offset[j][1];
1595 89088 int x = 4 * sb_x + hx;
1596 89088 int y = 4 * sb_y + hy;
1597 89088 VP4Predictor *this_dc_pred = &dc_pred[hy + 1][hx + 1];
1598 int fragment, dc_block_type;
1599
1600
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])
1601 1536 continue;
1602
1603 87552 fragment = s->fragment_start[plane] + y * s->fragment_width[!!plane] + x;
1604
1605
2/2
✓ Branch 0 taken 24300 times.
✓ Branch 1 taken 63252 times.
87552 if (s->all_fragments[fragment].coding_method == MODE_COPY)
1606 24300 continue;
1607
1608
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)
1609 return -1;
1610
1611 63252 dc_block_type = vp4_pred_block_type_map[s->all_fragments[fragment].coding_method];
1612
1613 63252 s->all_fragments[fragment].dc +=
1614 63252 vp4_dc_pred(s, this_dc_pred, last_dc, dc_block_type, plane);
1615
1616 63252 this_dc_pred->type = dc_block_type,
1617 63252 this_dc_pred->dc = last_dc[dc_block_type] = s->all_fragments[fragment].dc;
1618 }
1619 5568 vp4_dc_pred_after(s, dc_pred, sb_x);
1620 }
1621 }
1622 }
1623
1624 24 vp4_set_tokens_base(s);
1625
1626 24 return 0;
1627 }
1628 #endif
1629
1630 /*
1631 * This function reverses the DC prediction for each coded fragment in
1632 * the frame. Much of this function is adapted directly from the original
1633 * VP3 source code.
1634 */
1635 #define COMPATIBLE_FRAME(x) \
1636 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1637 #define DC_COEFF(u) s->all_fragments[u].dc
1638
1639 420 static void reverse_dc_prediction(Vp3DecodeContext *s,
1640 int first_fragment,
1641 int fragment_width,
1642 int fragment_height)
1643 {
1644 #define PUL 8
1645 #define PU 4
1646 #define PUR 2
1647 #define PL 1
1648
1649 420 int i = first_fragment;
1650
1651 int predicted_dc;
1652
1653 /* DC values for the left, up-left, up, and up-right fragments */
1654 int vl, vul, vu, vur;
1655
1656 /* indexes for the left, up-left, up, and up-right fragments */
1657 int l, ul, u, ur;
1658
1659 /*
1660 * The 6 fields mean:
1661 * 0: up-left multiplier
1662 * 1: up multiplier
1663 * 2: up-right multiplier
1664 * 3: left multiplier
1665 */
1666 static const int predictor_transform[16][4] = {
1667 { 0, 0, 0, 0 },
1668 { 0, 0, 0, 128 }, // PL
1669 { 0, 0, 128, 0 }, // PUR
1670 { 0, 0, 53, 75 }, // PUR|PL
1671 { 0, 128, 0, 0 }, // PU
1672 { 0, 64, 0, 64 }, // PU |PL
1673 { 0, 128, 0, 0 }, // PU |PUR
1674 { 0, 0, 53, 75 }, // PU |PUR|PL
1675 { 128, 0, 0, 0 }, // PUL
1676 { 0, 0, 0, 128 }, // PUL|PL
1677 { 64, 0, 64, 0 }, // PUL|PUR
1678 { 0, 0, 53, 75 }, // PUL|PUR|PL
1679 { 0, 128, 0, 0 }, // PUL|PU
1680 { -104, 116, 0, 116 }, // PUL|PU |PL
1681 { 24, 80, 24, 0 }, // PUL|PU |PUR
1682 { -104, 116, 0, 116 } // PUL|PU |PUR|PL
1683 };
1684
1685 /* This table shows which types of blocks can use other blocks for
1686 * prediction. For example, INTRA is the only mode in this table to
1687 * have a frame number of 0. That means INTRA blocks can only predict
1688 * from other INTRA blocks. There are 2 golden frame coding types;
1689 * blocks encoding in these modes can only predict from other blocks
1690 * that were encoded with these 1 of these 2 modes. */
1691 static const unsigned char compatible_frame[9] = {
1692 1, /* MODE_INTER_NO_MV */
1693 0, /* MODE_INTRA */
1694 1, /* MODE_INTER_PLUS_MV */
1695 1, /* MODE_INTER_LAST_MV */
1696 1, /* MODE_INTER_PRIOR_MV */
1697 2, /* MODE_USING_GOLDEN */
1698 2, /* MODE_GOLDEN_MV */
1699 1, /* MODE_INTER_FOUR_MV */
1700 3 /* MODE_COPY */
1701 };
1702 int current_frame_type;
1703
1704 /* there is a last DC predictor for each of the 3 frame types */
1705 short last_dc[3];
1706
1707 420 int transform = 0;
1708
1709 420 vul =
1710 420 vu =
1711 420 vur =
1712 420 vl = 0;
1713 420 last_dc[0] =
1714 420 last_dc[1] =
1715 420 last_dc[2] = 0;
1716
1717 /* for each fragment row... */
1718
2/2
✓ Branch 0 taken 13872 times.
✓ Branch 1 taken 420 times.
14292 for (int y = 0; y < fragment_height; y++) {
1719 /* for each fragment in a row... */
1720
2/2
✓ Branch 0 taken 1760640 times.
✓ Branch 1 taken 13872 times.
1774512 for (int x = 0; x < fragment_width; x++, i++) {
1721
1722 /* reverse prediction if this block was coded */
1723
2/2
✓ Branch 0 taken 532576 times.
✓ Branch 1 taken 1228064 times.
1760640 if (s->all_fragments[i].coding_method != MODE_COPY) {
1724 532576 current_frame_type =
1725 532576 compatible_frame[s->all_fragments[i].coding_method];
1726
1727 532576 transform = 0;
1728
2/2
✓ Branch 0 taken 528445 times.
✓ Branch 1 taken 4131 times.
532576 if (x) {
1729 528445 l = i - 1;
1730 528445 vl = DC_COEFF(l);
1731
2/2
✓ Branch 0 taken 483186 times.
✓ Branch 1 taken 45259 times.
528445 if (COMPATIBLE_FRAME(l))
1732 483186 transform |= PL;
1733 }
1734
2/2
✓ Branch 0 taken 513468 times.
✓ Branch 1 taken 19108 times.
532576 if (y) {
1735 513468 u = i - fragment_width;
1736 513468 vu = DC_COEFF(u);
1737
2/2
✓ Branch 0 taken 476069 times.
✓ Branch 1 taken 37399 times.
513468 if (COMPATIBLE_FRAME(u))
1738 476069 transform |= PU;
1739
2/2
✓ Branch 0 taken 509661 times.
✓ Branch 1 taken 3807 times.
513468 if (x) {
1740 509661 ul = i - fragment_width - 1;
1741 509661 vul = DC_COEFF(ul);
1742
2/2
✓ Branch 0 taken 461368 times.
✓ Branch 1 taken 48293 times.
509661 if (COMPATIBLE_FRAME(ul))
1743 461368 transform |= PUL;
1744 }
1745
2/2
✓ Branch 0 taken 505812 times.
✓ Branch 1 taken 7656 times.
513468 if (x + 1 < fragment_width) {
1746 505812 ur = i - fragment_width + 1;
1747 505812 vur = DC_COEFF(ur);
1748
2/2
✓ Branch 0 taken 459980 times.
✓ Branch 1 taken 45832 times.
505812 if (COMPATIBLE_FRAME(ur))
1749 459980 transform |= PUR;
1750 }
1751 }
1752
1753
2/2
✓ Branch 0 taken 10817 times.
✓ Branch 1 taken 521759 times.
532576 if (transform == 0) {
1754 /* if there were no fragments to predict from, use last
1755 * DC saved */
1756 10817 predicted_dc = last_dc[current_frame_type];
1757 } else {
1758 /* apply the appropriate predictor transform */
1759 521759 predicted_dc =
1760 521759 (predictor_transform[transform][0] * vul) +
1761 521759 (predictor_transform[transform][1] * vu) +
1762 521759 (predictor_transform[transform][2] * vur) +
1763 521759 (predictor_transform[transform][3] * vl);
1764
1765 521759 predicted_dc /= 128;
1766
1767 /* check for outranging on the [ul u l] and
1768 * [ul u ur l] predictors */
1769
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)) {
1770
2/2
✓ Branch 0 taken 2351 times.
✓ Branch 1 taken 432360 times.
434711 if (FFABS(predicted_dc - vu) > 128)
1771 2351 predicted_dc = vu;
1772
2/2
✓ Branch 0 taken 1111 times.
✓ Branch 1 taken 431249 times.
432360 else if (FFABS(predicted_dc - vl) > 128)
1773 1111 predicted_dc = vl;
1774
2/2
✓ Branch 0 taken 2253 times.
✓ Branch 1 taken 428996 times.
431249 else if (FFABS(predicted_dc - vul) > 128)
1775 2253 predicted_dc = vul;
1776 }
1777 }
1778
1779 /* at long last, apply the predictor */
1780 532576 DC_COEFF(i) += predicted_dc;
1781 /* save the DC */
1782 532576 last_dc[current_frame_type] = DC_COEFF(i);
1783 }
1784 }
1785 }
1786 420 }
1787
1788 2700 static void apply_loop_filter(Vp3DecodeContext *s, int plane,
1789 int ystart, int yend)
1790 {
1791 2700 int *bounding_values = s->bounding_values_array + 127;
1792
1793 2700 int width = s->fragment_width[!!plane];
1794 2700 int height = s->fragment_height[!!plane];
1795 2700 int fragment = s->fragment_start[plane] + ystart * width;
1796 2700 ptrdiff_t stride = s->current_frame.f->linesize[plane];
1797 2700 uint8_t *plane_data = s->current_frame.f->data[plane];
1798
1/2
✓ Branch 0 taken 2700 times.
✗ Branch 1 not taken.
2700 if (!s->flipped_image)
1799 2700 stride = -stride;
1800 2700 plane_data += s->data_offset[plane] + 8 * ystart * stride;
1801
1802
2/2
✓ Branch 0 taken 7830 times.
✓ Branch 1 taken 2700 times.
10530 for (int y = ystart; y < yend; y++) {
1803
2/2
✓ Branch 0 taken 478880 times.
✓ Branch 1 taken 7830 times.
486710 for (int x = 0; x < width; x++) {
1804 /* This code basically just deblocks on the edges of coded blocks.
1805 * However, it has to be much more complicated because of the
1806 * brain damaged deblock ordering used in VP3/Theora. Order matters
1807 * because some pixels get filtered twice. */
1808
2/2
✓ Branch 0 taken 367025 times.
✓ Branch 1 taken 111855 times.
478880 if (s->all_fragments[fragment].coding_method != MODE_COPY) {
1809 /* do not perform left edge filter for left columns frags */
1810
2/2
✓ Branch 0 taken 363601 times.
✓ Branch 1 taken 3424 times.
367025 if (x > 0) {
1811 363601 s->vp3dsp.h_loop_filter(
1812 363601 plane_data + 8 * x,
1813 stride, bounding_values);
1814 }
1815
1816 /* do not perform top edge filter for top row fragments */
1817
2/2
✓ Branch 0 taken 349625 times.
✓ Branch 1 taken 17400 times.
367025 if (y > 0) {
1818 349625 s->vp3dsp.v_loop_filter(
1819 349625 plane_data + 8 * x,
1820 stride, bounding_values);
1821 }
1822
1823 /* do not perform right edge filter for right column
1824 * fragments or if right fragment neighbor is also coded
1825 * in this frame (it will be filtered in next iteration) */
1826
2/2
✓ Branch 0 taken 359725 times.
✓ Branch 1 taken 7300 times.
367025 if ((x < width - 1) &&
1827
2/2
✓ Branch 0 taken 32268 times.
✓ Branch 1 taken 327457 times.
359725 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
1828 32268 s->vp3dsp.h_loop_filter(
1829 32268 plane_data + 8 * x + 8,
1830 stride, bounding_values);
1831 }
1832
1833 /* do not perform bottom edge filter for bottom row
1834 * fragments or if bottom fragment neighbor is also coded
1835 * in this frame (it will be filtered in the next row) */
1836
2/2
✓ Branch 0 taken 352272 times.
✓ Branch 1 taken 14753 times.
367025 if ((y < height - 1) &&
1837
2/2
✓ Branch 0 taken 32946 times.
✓ Branch 1 taken 319326 times.
352272 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
1838 32946 s->vp3dsp.v_loop_filter(
1839 32946 plane_data + 8 * x + 8 * stride,
1840 stride, bounding_values);
1841 }
1842 }
1843
1844 478880 fragment++;
1845 }
1846 7830 plane_data += 8 * stride;
1847 }
1848 2700 }
1849
1850 /**
1851 * Pull DCT tokens from the 64 levels to decode and dequant the coefficients
1852 * for the next block in coding order
1853 */
1854 595828 static inline int vp3_dequant(Vp3DecodeContext *s, const Vp3Fragment *frag,
1855 int plane, int inter, int16_t block[64])
1856 {
1857 595828 const int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
1858 595828 const uint8_t *perm = s->idct_scantable;
1859 595828 int i = 0;
1860
1861 do {
1862 3343039 int token = *s->dct_tokens[plane][i];
1863
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) {
1864 587550 case 0: // EOB
1865
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
1866 280242 s->dct_tokens[plane][i]++;
1867 else
1868 307308 *s->dct_tokens[plane][i] = token & ~3;
1869 587550 goto end;
1870 913868 case 1: // zero run
1871 913868 s->dct_tokens[plane][i]++;
1872 913868 i += (token >> 2) & 0x7f;
1873
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 913868 times.
913868 if (i > 63) {
1874 av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
1875 return i;
1876 }
1877 913868 block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1878 913868 i++;
1879 913868 break;
1880 1841621 case 2: // coeff
1881 1841621 block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1882 1841621 s->dct_tokens[plane][i++]++;
1883 1841621 break;
1884 default: // shouldn't happen
1885 return i;
1886 }
1887
2/2
✓ Branch 0 taken 2747211 times.
✓ Branch 1 taken 8278 times.
2755489 } while (i < 64);
1888 // return value is expected to be a valid level
1889 8278 i--;
1890 595828 end:
1891 // the actual DC+prediction is in the fragment structure
1892 595828 block[0] = frag->dc * s->qmat[0][inter][plane][0];
1893 595828 return i;
1894 }
1895
1896 /**
1897 * called when all pixels up to row y are complete
1898 */
1899 1232 static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
1900 {
1901 int h, cy;
1902 int offset[AV_NUM_DATA_POINTERS];
1903
1904
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1232 times.
1232 if (HAVE_THREADS && s->avctx->active_thread_type & FF_THREAD_FRAME) {
1905 int y_flipped = s->flipped_image ? s->height - y : y;
1906
1907 /* At the end of the frame, report INT_MAX instead of the height of
1908 * the frame. This makes the other threads' ff_thread_await_progress()
1909 * calls cheaper, because they don't have to clip their values. */
1910 ff_thread_report_progress(&s->current_frame,
1911 y_flipped == s->height ? INT_MAX
1912 : y_flipped - 1,
1913 0);
1914 }
1915
1916
1/2
✓ Branch 0 taken 1232 times.
✗ Branch 1 not taken.
1232 if (!s->avctx->draw_horiz_band)
1917 1232 return;
1918
1919 h = y - s->last_slice_end;
1920 s->last_slice_end = y;
1921 y -= h;
1922
1923 if (!s->flipped_image)
1924 y = s->height - y - h;
1925
1926 cy = y >> s->chroma_y_shift;
1927 offset[0] = s->current_frame.f->linesize[0] * y;
1928 offset[1] = s->current_frame.f->linesize[1] * cy;
1929 offset[2] = s->current_frame.f->linesize[2] * cy;
1930 for (int i = 3; i < AV_NUM_DATA_POINTERS; i++)
1931 offset[i] = 0;
1932
1933 emms_c();
1934 s->avctx->draw_horiz_band(s->avctx, s->current_frame.f, offset, y, 3, h);
1935 }
1936
1937 /**
1938 * Wait for the reference frame of the current fragment.
1939 * The progress value is in luma pixel rows.
1940 */
1941 static void await_reference_row(Vp3DecodeContext *s, const Vp3Fragment *fragment,
1942 int motion_y, int y)
1943 {
1944 const ThreadFrame *ref_frame;
1945 int ref_row;
1946 int border = motion_y & 1;
1947
1948 if (fragment->coding_method == MODE_USING_GOLDEN ||
1949 fragment->coding_method == MODE_GOLDEN_MV)
1950 ref_frame = &s->golden_frame;
1951 else
1952 ref_frame = &s->last_frame;
1953
1954 ref_row = y + (motion_y >> 1);
1955 ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
1956
1957 ff_thread_await_progress(ref_frame, ref_row, 0);
1958 }
1959
1960 #if CONFIG_VP4_DECODER
1961 /**
1962 * @return non-zero if temp (edge_emu_buffer) was populated
1963 */
1964 36568 static int vp4_mc_loop_filter(Vp3DecodeContext *s, int plane, int motion_x, int motion_y, int bx, int by,
1965 const uint8_t *motion_source, ptrdiff_t stride,
1966 int src_x, int src_y, uint8_t *temp)
1967 {
1968
2/2
✓ Branch 0 taken 11931 times.
✓ Branch 1 taken 24637 times.
36568 int motion_shift = plane ? 4 : 2;
1969
2/2
✓ Branch 0 taken 11931 times.
✓ Branch 1 taken 24637 times.
36568 int subpel_mask = plane ? 3 : 1;
1970 36568 int *bounding_values = s->bounding_values_array + 127;
1971
1972 int x, y;
1973 int x2, y2;
1974 int x_subpel, y_subpel;
1975 int x_offset, y_offset;
1976
1977
2/2
✓ Branch 0 taken 11931 times.
✓ Branch 1 taken 24637 times.
36568 int block_width = plane ? 8 : 16;
1978
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);
1979
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);
1980
1981 #define loop_stride 12
1982 uint8_t loop[12 * loop_stride];
1983
1984 /* using division instead of shift to correctly handle negative values */
1985 36568 x = 8 * bx + motion_x / motion_shift;
1986 36568 y = 8 * by + motion_y / motion_shift;
1987
1988 36568 x_subpel = motion_x & subpel_mask;
1989 36568 y_subpel = motion_y & subpel_mask;
1990
1991
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) {
1992 30063 x--;
1993 30063 y--;
1994
1995
2/2
✓ Branch 0 taken 22774 times.
✓ Branch 1 taken 7289 times.
30063 if (x_subpel)
1996
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));
1997
1998
2/2
✓ Branch 0 taken 15343 times.
✓ Branch 1 taken 14720 times.
30063 if (y_subpel)
1999
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));
2000
2001 30063 x2 = x + block_width;
2002 30063 y2 = y + block_width;
2003
2004
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)
2005 596 return 0;
2006
2007 29467 x_offset = (-(x + 2) & 7) + 2;
2008 29467 y_offset = (-(y + 2) & 7) + 2;
2009
2010
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)
2011 return 0;
2012
2013 29467 s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1,
2014 loop_stride, stride,
2015 12, 12, src_x - 1, src_y - 1,
2016 plane_width,
2017 plane_height);
2018
2019
2/2
✓ Branch 0 taken 25000 times.
✓ Branch 1 taken 4467 times.
29467 if (x_offset <= 8 + x_subpel)
2020 25000 ff_vp3dsp_h_loop_filter_12(loop + x_offset, loop_stride, bounding_values);
2021
2022
2/2
✓ Branch 0 taken 17032 times.
✓ Branch 1 taken 12435 times.
29467 if (y_offset <= 8 + y_subpel)
2023 17032 ff_vp3dsp_v_loop_filter_12(loop + y_offset*loop_stride, loop_stride, bounding_values);
2024
2025 } else {
2026
2027 6505 x_offset = -x & 7;
2028 6505 y_offset = -y & 7;
2029
2030
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)
2031 118 return 0;
2032
2033 6387 s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1,
2034 loop_stride, stride,
2035 12, 12, src_x - 1, src_y - 1,
2036 plane_width,
2037 plane_height);
2038
2039 #define safe_loop_filter(name, ptr, stride, bounding_values) \
2040 if ((uintptr_t)(ptr) & 7) \
2041 s->vp3dsp.name##_unaligned(ptr, stride, bounding_values); \
2042 else \
2043 s->vp3dsp.name(ptr, stride, bounding_values);
2044
2045
2/2
✓ Branch 0 taken 5671 times.
✓ Branch 1 taken 716 times.
6387 if (x_offset)
2046
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);
2047
2048
2/2
✓ Branch 0 taken 3055 times.
✓ Branch 1 taken 3332 times.
6387 if (y_offset)
2049
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);
2050 }
2051
2052
2/2
✓ Branch 0 taken 322686 times.
✓ Branch 1 taken 35854 times.
358540 for (int i = 0; i < 9; i++)
2053 322686 memcpy(temp + i*stride, loop + (i + 1) * loop_stride + 1, 9);
2054
2055 35854 return 1;
2056 }
2057 #endif
2058
2059 /*
2060 * Perform the final rendering for a particular slice of data.
2061 * The slice number ranges from 0..(c_superblock_height - 1).
2062 */
2063 1068 static void render_slice(Vp3DecodeContext *s, int slice)
2064 {
2065 1068 int16_t *block = s->block;
2066 1068 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
2067 /* When decoding keyframes, the earlier frames may not be available,
2068 * so to avoid using undefined pointer arithmetic on them we just
2069 * use the current frame instead. Nothing is ever read from these
2070 * frames in case of a keyframe. */
2071
2/2
✓ Branch 0 taken 1012 times.
✓ Branch 1 taken 56 times.
1068 const AVFrame *last_frame = s->last_frame.f->data[0] ?
2072 s->last_frame.f : s->current_frame.f;
2073
2/2
✓ Branch 0 taken 1012 times.
✓ Branch 1 taken 56 times.
1068 const AVFrame *golden_frame = s->golden_frame.f->data[0] ?
2074 s->golden_frame.f : s->current_frame.f;
2075 int motion_halfpel_index;
2076 int first_pixel;
2077
2078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1068 times.
1068 if (slice >= s->c_superblock_height)
2079 return;
2080
2081
2/2
✓ Branch 0 taken 3204 times.
✓ Branch 1 taken 1068 times.
4272 for (int plane = 0; plane < 3; plane++) {
2082 3204 uint8_t *output_plane = s->current_frame.f->data[plane] +
2083 3204 s->data_offset[plane];
2084 3204 const uint8_t *last_plane = last_frame->data[plane] +
2085 3204 s->data_offset[plane];
2086 3204 const uint8_t *golden_plane = golden_frame->data[plane] +
2087 3204 s->data_offset[plane];
2088 3204 ptrdiff_t stride = s->current_frame.f->linesize[plane];
2089
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);
2090
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);
2091 3204 const int8_t (*motion_val)[2] = s->motion_val[!!plane];
2092
2093
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);
2094
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);
2095 3204 int slice_width = plane ? s->c_superblock_width
2096
2/2
✓ Branch 0 taken 2136 times.
✓ Branch 1 taken 1068 times.
3204 : s->y_superblock_width;
2097
2098 3204 int fragment_width = s->fragment_width[!!plane];
2099 3204 int fragment_height = s->fragment_height[!!plane];
2100 3204 int fragment_start = s->fragment_start[plane];
2101
2102
2/2
✓ Branch 0 taken 1068 times.
✓ Branch 1 taken 2136 times.
4272 int do_await = !plane && HAVE_THREADS &&
2103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1068 times.
1068 (s->avctx->active_thread_type & FF_THREAD_FRAME);
2104
2105
1/2
✓ Branch 0 taken 3204 times.
✗ Branch 1 not taken.
3204 if (!s->flipped_image)
2106 3204 stride = -stride;
2107 if (CONFIG_GRAY && plane && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2108 continue;
2109
2110 /* for each superblock row in the slice (both of them)... */
2111
2/2
✓ Branch 0 taken 4272 times.
✓ Branch 1 taken 3204 times.
7476 for (; sb_y < slice_height; sb_y++) {
2112 /* for each superblock in a row... */
2113
2/2
✓ Branch 0 taken 125288 times.
✓ Branch 1 taken 4272 times.
129560 for (int sb_x = 0; sb_x < slice_width; sb_x++) {
2114 /* for each block in a superblock... */
2115
2/2
✓ Branch 0 taken 2004608 times.
✓ Branch 1 taken 125288 times.
2129896 for (int j = 0; j < 16; j++) {
2116 2004608 int x = 4 * sb_x + hilbert_offset[j][0];
2117 2004608 int y = 4 * sb_y + hilbert_offset[j][1];
2118 2004608 int fragment = y * fragment_width + x;
2119
2120 2004608 int i = fragment_start + fragment;
2121
2122 // bounds check
2123
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)
2124 156416 continue;
2125
2126 1848192 first_pixel = 8 * y * stride + 8 * x;
2127
2128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1848192 times.
1848192 if (do_await &&
2129 s->all_fragments[i].coding_method != MODE_INTRA)
2130 await_reference_row(s, &s->all_fragments[i],
2131 motion_val[fragment][1],
2132 (16 * y) >> s->chroma_y_shift);
2133
2134 /* transform if this block was coded */
2135
2/2
✓ Branch 0 taken 595828 times.
✓ Branch 1 taken 1252364 times.
1848192 if (s->all_fragments[i].coding_method != MODE_COPY) {
2136 const uint8_t *motion_source;
2137
2/2
✓ Branch 0 taken 592182 times.
✓ Branch 1 taken 3646 times.
595828 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
2138
2/2
✓ Branch 0 taken 4039 times.
✓ Branch 1 taken 588143 times.
592182 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
2139 7685 motion_source = golden_plane;
2140 else
2141 588143 motion_source = last_plane;
2142
2143 595828 motion_source += first_pixel;
2144 595828 motion_halfpel_index = 0;
2145
2146 /* sort out the motion vector if this fragment is coded
2147 * using a motion vector method */
2148
2/2
✓ Branch 0 taken 377836 times.
✓ Branch 1 taken 217992 times.
595828 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
2149
2/2
✓ Branch 0 taken 374190 times.
✓ Branch 1 taken 3646 times.
377836 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
2150 int src_x, src_y;
2151 374190 int standard_mc = 1;
2152 374190 motion_x = motion_val[fragment][0];
2153 374190 motion_y = motion_val[fragment][1];
2154 #if CONFIG_VP4_DECODER
2155
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) {
2156 11931 motion_x = (motion_x >> 1) | (motion_x & 1);
2157 11931 motion_y = (motion_y >> 1) | (motion_y & 1);
2158 }
2159