FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp3.c
Date: 2021-09-26 18:22:30
Exec Total Coverage
Lines: 1241 1538 80.7%
Branches: 813 1114 73.0%

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