FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp3.c
Date: 2022-07-04 19:11:22
Exec Total Coverage
Lines: 1240 1540 80.5%
Branches: 813 1116 72.8%

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