FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp3.c
Date: 2025-02-06 06:25:49
Exec Total Coverage
Lines: 1232 1516 81.3%
Functions: 41 44 93.2%
Branches: 802 1080 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 "libavutil/refstruct.h"
54 #include "thread.h"
55 #include "videodsp.h"
56 #include "vp3data.h"
57 #include "vp4data.h"
58 #include "vp3dsp.h"
59 #include "xiph.h"
60
61 #define VP3_MV_VLC_BITS 6
62 #define VP4_MV_VLC_BITS 6
63 #define SUPERBLOCK_VLC_BITS 6
64
65 #define FRAGMENT_PIXELS 8
66
67 // FIXME split things out into their own arrays
68 typedef struct Vp3Fragment {
69 int16_t dc;
70 uint8_t coding_method;
71 uint8_t qpi;
72 } Vp3Fragment;
73
74 #define SB_NOT_CODED 0
75 #define SB_PARTIALLY_CODED 1
76 #define SB_FULLY_CODED 2
77
78 // This is the maximum length of a single long bit run that can be encoded
79 // for superblock coding or block qps. Theora special-cases this to read a
80 // bit instead of flipping the current bit to allow for runs longer than 4129.
81 #define MAXIMUM_LONG_BIT_RUN 4129
82
83 #define MODE_INTER_NO_MV 0
84 #define MODE_INTRA 1
85 #define MODE_INTER_PLUS_MV 2
86 #define MODE_INTER_LAST_MV 3
87 #define MODE_INTER_PRIOR_LAST 4
88 #define MODE_USING_GOLDEN 5
89 #define MODE_GOLDEN_MV 6
90 #define MODE_INTER_FOURMV 7
91 #define CODING_MODE_COUNT 8
92
93 /* special internal mode */
94 #define MODE_COPY 8
95
96 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb);
97 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb);
98
99
100 /* There are 6 preset schemes, plus a free-form scheme */
101 static const int ModeAlphabet[6][CODING_MODE_COUNT] = {
102 /* scheme 1: Last motion vector dominates */
103 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
104 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
105 MODE_INTRA, MODE_USING_GOLDEN,
106 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
107
108 /* scheme 2 */
109 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
110 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
111 MODE_INTRA, MODE_USING_GOLDEN,
112 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
113
114 /* scheme 3 */
115 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
116 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
117 MODE_INTRA, MODE_USING_GOLDEN,
118 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
119
120 /* scheme 4 */
121 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
122 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
123 MODE_INTRA, MODE_USING_GOLDEN,
124 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
125
126 /* scheme 5: No motion vector dominates */
127 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
128 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
129 MODE_INTRA, MODE_USING_GOLDEN,
130 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
131
132 /* scheme 6 */
133 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
134 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
135 MODE_INTER_PLUS_MV, MODE_INTRA,
136 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
137 };
138
139 static const uint8_t hilbert_offset[16][2] = {
140 { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
141 { 0, 2 }, { 0, 3 }, { 1, 3 }, { 1, 2 },
142 { 2, 2 }, { 2, 3 }, { 3, 3 }, { 3, 2 },
143 { 3, 1 }, { 2, 1 }, { 2, 0 }, { 3, 0 }
144 };
145
146 enum {
147 VP4_DC_INTRA = 0,
148 VP4_DC_INTER = 1,
149 VP4_DC_GOLDEN = 2,
150 NB_VP4_DC_TYPES,
151 VP4_DC_UNDEFINED = NB_VP4_DC_TYPES
152 };
153
154 static const uint8_t vp4_pred_block_type_map[8] = {
155 [MODE_INTER_NO_MV] = VP4_DC_INTER,
156 [MODE_INTRA] = VP4_DC_INTRA,
157 [MODE_INTER_PLUS_MV] = VP4_DC_INTER,
158 [MODE_INTER_LAST_MV] = VP4_DC_INTER,
159 [MODE_INTER_PRIOR_LAST] = VP4_DC_INTER,
160 [MODE_USING_GOLDEN] = VP4_DC_GOLDEN,
161 [MODE_GOLDEN_MV] = VP4_DC_GOLDEN,
162 [MODE_INTER_FOURMV] = VP4_DC_INTER,
163 };
164
165 static VLCElem superblock_run_length_vlc[88]; /* version < 2 */
166 static VLCElem fragment_run_length_vlc[56]; /* version < 2 */
167 static VLCElem motion_vector_vlc[112]; /* version < 2 */
168
169 // The VP4 tables reuse this vlc.
170 static VLCElem mode_code_vlc[24 + 2108 * CONFIG_VP4_DECODER];
171
172 #if CONFIG_VP4_DECODER
173 static const VLCElem *vp4_mv_vlc_table[2][7]; /* version >= 2 */
174 static const VLCElem *block_pattern_vlc[2]; /* version >= 2 */
175 #endif
176
177 typedef struct {
178 int dc;
179 int type;
180 } VP4Predictor;
181
182 #define MIN_DEQUANT_VAL 2
183
184 typedef struct HuffEntry {
185 uint8_t len, sym;
186 } HuffEntry;
187
188 typedef struct HuffTable {
189 HuffEntry entries[32];
190 uint8_t nb_entries;
191 } HuffTable;
192
193 typedef struct CoeffVLCs {
194 const VLCElem *vlc_tabs[80];
195 VLC vlcs[80];
196 } CoeffVLCs;
197
198 typedef struct Vp3DecodeContext {
199 AVCodecContext *avctx;
200 int theora, theora_tables, theora_header;
201 int version;
202 int width, height;
203 int chroma_x_shift, chroma_y_shift;
204 ProgressFrame golden_frame;
205 ProgressFrame last_frame;
206 ProgressFrame current_frame;
207 int keyframe;
208 uint8_t idct_permutation[64];
209 uint8_t idct_scantable[64];
210 HpelDSPContext hdsp;
211 VideoDSPContext vdsp;
212 VP3DSPContext vp3dsp;
213 DECLARE_ALIGNED(16, int16_t, block)[64];
214 int flipped_image;
215 int last_slice_end;
216 int skip_loop_filter;
217
218 int qps[3];
219 int nqps;
220 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 av_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 av_assert1(!(x_offset > 8 + x_subpel && y_offset > 8 + y_subpel));
2005
2006 29467 s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1,
2007 loop_stride, stride,
2008 12, 12, src_x - 1, src_y - 1,
2009 plane_width,
2010 plane_height);
2011
2012
2/2
✓ Branch 0 taken 25000 times.
✓ Branch 1 taken 4467 times.
29467 if (x_offset <= 8 + x_subpel)
2013 25000 ff_vp3dsp_h_loop_filter_12(loop + x_offset, loop_stride, bounding_values);
2014
2015
2/2
✓ Branch 0 taken 17032 times.
✓ Branch 1 taken 12435 times.
29467 if (y_offset <= 8 + y_subpel)
2016 17032 ff_vp3dsp_v_loop_filter_12(loop + y_offset*loop_stride, loop_stride, bounding_values);
2017
2018 } else {
2019
2020 6505 x_offset = -x & 7;
2021 6505 y_offset = -y & 7;
2022
2023
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)
2024 118 return 0;
2025
2026 6387 s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1,
2027 loop_stride, stride,
2028 12, 12, src_x - 1, src_y - 1,
2029 plane_width,
2030 plane_height);
2031
2032 #define safe_loop_filter(name, ptr, stride, bounding_values) \
2033 if ((uintptr_t)(ptr) & 7) \
2034 s->vp3dsp.name##_unaligned(ptr, stride, bounding_values); \
2035 else \
2036 s->vp3dsp.name(ptr, stride, bounding_values);
2037
2038
2/2
✓ Branch 0 taken 5671 times.
✓ Branch 1 taken 716 times.
6387 if (x_offset)
2039
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);
2040
2041
2/2
✓ Branch 0 taken 3055 times.
✓ Branch 1 taken 3332 times.
6387 if (y_offset)
2042
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);
2043 }
2044
2045
2/2
✓ Branch 0 taken 322686 times.
✓ Branch 1 taken 35854 times.
358540 for (int i = 0; i < 9; i++)
2046 322686 memcpy(temp + i*stride, loop + (i + 1) * loop_stride + 1, 9);
2047
2048 35854 return 1;
2049 }
2050 #endif
2051
2052 /*
2053 * Perform the final rendering for a particular slice of data.
2054 * The slice number ranges from 0..(c_superblock_height - 1).
2055 */
2056 1068 static void render_slice(Vp3DecodeContext *s, int slice)
2057 {
2058 1068 int16_t *block = s->block;
2059 1068 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
2060 /* When decoding keyframes, the earlier frames may not be available,
2061 * so we just use the current frame in this case instead;
2062 * it also avoid using undefined pointer arithmetic. Nothing is
2063 * ever read from these frames in case of a keyframe. */
2064
2/2
✓ Branch 0 taken 1012 times.
✓ Branch 1 taken 56 times.
1068 const AVFrame *last_frame = s->last_frame.f ?
2065 s->last_frame.f : s->current_frame.f;
2066
2/2
✓ Branch 0 taken 1012 times.
✓ Branch 1 taken 56 times.
1068 const AVFrame *golden_frame = s->golden_frame.f ?
2067 s->golden_frame.f : s->current_frame.f;
2068 int motion_halfpel_index;
2069 int first_pixel;
2070
2071
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1068 times.
1068 if (slice >= s->c_superblock_height)
2072 return;
2073
2074
2/2
✓ Branch 0 taken 3204 times.
✓ Branch 1 taken 1068 times.
4272 for (int plane = 0; plane < 3; plane++) {
2075 3204 uint8_t *output_plane = s->current_frame.f->data[plane] +
2076 3204 s->data_offset[plane];
2077 3204 const uint8_t *last_plane = last_frame->data[plane] +
2078 3204 s->data_offset[plane];
2079 3204 const uint8_t *golden_plane = golden_frame->data[plane] +
2080 3204 s->data_offset[plane];
2081 3204 ptrdiff_t stride = s->current_frame.f->linesize[plane];
2082
3/4
✓ Branch 0 taken 2136 times.
✓ Branch 1 taken 1068 times.
✓ Branch 2 taken 2136 times.
✗ Branch 3 not taken.
3204 int plane_width = s->width >> (plane && s->chroma_x_shift);
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_height = s->height >> (plane && s->chroma_y_shift);
2084 3204 const int8_t (*motion_val)[2] = s->motion_val[!!plane];
2085
2086
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);
2087
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);
2088 3204 int slice_width = plane ? s->c_superblock_width
2089
2/2
✓ Branch 0 taken 2136 times.
✓ Branch 1 taken 1068 times.
3204 : s->y_superblock_width;
2090
2091 3204 int fragment_width = s->fragment_width[!!plane];
2092 3204 int fragment_height = s->fragment_height[!!plane];
2093 3204 int fragment_start = s->fragment_start[plane];
2094
2095
2/2
✓ Branch 0 taken 1068 times.
✓ Branch 1 taken 2136 times.
4272 int do_await = !plane && HAVE_THREADS &&
2096
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1068 times.
1068 (s->avctx->active_thread_type & FF_THREAD_FRAME);
2097
2098
1/2
✓ Branch 0 taken 3204 times.
✗ Branch 1 not taken.
3204 if (!s->flipped_image)
2099 3204 stride = -stride;
2100 if (CONFIG_GRAY && plane && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2101 continue;
2102
2103 /* for each superblock row in the slice (both of them)... */
2104
2/2
✓ Branch 0 taken 4272 times.
✓ Branch 1 taken 3204 times.
7476 for (; sb_y < slice_height; sb_y++) {
2105 /* for each superblock in a row... */
2106
2/2
✓ Branch 0 taken 125288 times.
✓ Branch 1 taken 4272 times.
129560 for (int sb_x = 0; sb_x < slice_width; sb_x++) {
2107 /* for each block in a superblock... */
2108
2/2
✓ Branch 0 taken 2004608 times.
✓ Branch 1 taken 125288 times.
2129896 for (int j = 0; j < 16; j++) {
2109 2004608 int x = 4 * sb_x + hilbert_offset[j][0];
2110 2004608 int y = 4 * sb_y + hilbert_offset[j][1];
2111 2004608 int fragment = y * fragment_width + x;
2112
2113 2004608 int i = fragment_start + fragment;
2114
2115 // bounds check
2116
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)
2117 156416 continue;
2118
2119 1848192 first_pixel = 8 * y * stride + 8 * x;
2120
2121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1848192 times.
1848192 if (do_await &&
2122 s->all_fragments[i].coding_method != MODE_INTRA)
2123 await_reference_row(s, &s->all_fragments[i],
2124 motion_val[fragment][1],
2125 (16 * y) >> s->chroma_y_shift);
2126
2127 /* transform if this block was coded */
2128
2/2
✓ Branch 0 taken 595828 times.
✓ Branch 1 taken 1252364 times.
1848192 if (s->all_fragments[i].coding_method != MODE_COPY) {
2129 const uint8_t *motion_source;
2130
2/2
✓ Branch 0 taken 592182 times.
✓ Branch 1 taken 3646 times.
595828 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
2131
2/2
✓ Branch 0 taken 4039 times.
✓ Branch 1 taken 588143 times.
592182 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
2132 7685 motion_source = golden_plane;
2133 else
2134 588143 motion_source = last_plane;
2135
2136 595828 motion_source += first_pixel;
2137 595828 motion_halfpel_index = 0;
2138
2139 /* sort out the motion vector if this fragment is coded
2140 * using a motion vector method */
2141
2/2
✓ Branch 0 taken 377836 times.
✓ Branch 1 taken 217992 times.
595828 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
2142
2/2
✓ Branch 0 taken 374190 times.
✓ Branch 1 taken 3646 times.
377836 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
2143 int src_x, src_y;
2144 374190 int standard_mc = 1;
2145 374190 motion_x = motion_val[fragment][0];
2146 374190 motion_y = motion_val[fragment][1];
2147 #if CONFIG_VP4_DECODER
2148
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) {
2149 11931 motion_x = (motion_x >> 1) | (motion_x & 1);
2150 11931 motion_y = (motion_y >> 1) | (motion_y & 1);
2151 }
2152 #endif
2153
2154 374190 src_x = (motion_x >> 1) + 8 * x;
2155 374190 src_y = (motion_y >> 1) + 8 * y;
2156
2157 374190 motion_halfpel_index = motion_x & 0x01;
2158 374190 motion_source += (motion_x >> 1);
2159
2160 374190 motion_halfpel_index |= (motion_y & 0x01) << 1;
2161 374190 motion_source += ((motion_y >> 1) * stride);
2162
2163 #if CONFIG_VP4_DECODER
2164
2/2
✓ Branch 0 taken 36568 times.
✓ Branch 1 taken 337622 times.
374190 if (s->version >= 2) {
2165 36568 uint8_t *temp = s->edge_emu_buffer;
2166
1/2
✓ Branch 0 taken 36568 times.
✗ Branch 1 not taken.
36568 if (stride < 0)
2167 36568 temp -= 8 * stride;