FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp6.c
Date: 2022-07-05 19:52:29
Exec Total Coverage
Lines: 406 436 93.1%
Branches: 262 298 87.9%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * VP6 compatible video decoder
24 *
25 * The VP6F decoder accepts an optional 1 byte extradata. It is composed of:
26 * - upper 4 bits: difference between encoded width and visible width
27 * - lower 4 bits: difference between encoded height and visible height
28 */
29
30 #include <stdlib.h>
31
32 #include "avcodec.h"
33 #include "codec_internal.h"
34 #include "get_bits.h"
35 #include "huffman.h"
36 #include "internal.h"
37
38 #include "vp56.h"
39 #include "vp56data.h"
40 #include "vp6data.h"
41
42 #define VP6_MAX_HUFF_SIZE 12
43
44 static int vp6_parse_coeff(VP56Context *s);
45 static int vp6_parse_coeff_huffman(VP56Context *s);
46
47 713 static int vp6_parse_header(VP56Context *s, const uint8_t *buf, int buf_size)
48 {
49 713 VP56RangeCoder *c = &s->c;
50 713 int parse_filter_info = 0;
51 713 int coeff_offset = 0;
52 713 int vrt_shift = 0;
53 int sub_version;
54 int rows, cols;
55 713 int res = 0;
56 int ret;
57 713 int separated_coeff = buf[0] & 1;
58
59 713 s->frames[VP56_FRAME_CURRENT]->key_frame = !(buf[0] & 0x80);
60 713 ff_vp56_init_dequant(s, (buf[0] >> 1) & 0x3F);
61
62
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 674 times.
713 if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
63 39 sub_version = buf[1] >> 3;
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (sub_version > 8)
65 return AVERROR_INVALIDDATA;
66 39 s->filter_header = buf[1] & 0x06;
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (buf[1] & 1) {
68 avpriv_report_missing_feature(s->avctx, "Interlacing");
69 return AVERROR_PATCHWELCOME;
70 }
71
3/4
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
39 if (separated_coeff || !s->filter_header) {
72 11 coeff_offset = AV_RB16(buf+2) - 2;
73 11 buf += 2;
74 11 buf_size -= 2;
75 }
76
77 39 rows = buf[2]; /* number of stored macroblock rows */
78 39 cols = buf[3]; /* number of stored macroblock cols */
79 /* buf[4] is number of displayed macroblock rows */
80 /* buf[5] is number of displayed macroblock cols */
81
2/4
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 39 times.
39 if (!rows || !cols) {
82 av_log(s->avctx, AV_LOG_ERROR, "Invalid size %dx%d\n", cols << 4, rows << 4);
83 return AVERROR_INVALIDDATA;
84 }
85
86
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 11 times.
39 if (!s->macroblocks || /* first frame */
87
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 16*cols != s->avctx->coded_width ||
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 16*rows != s->avctx->coded_height) {
89
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2 times.
11 if (s->avctx->extradata_size == 0 &&
90
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
9 FFALIGN(s->avctx->width, 16) == 16 * cols &&
91
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 FFALIGN(s->avctx->height, 16) == 16 * rows) {
92 // We assume this is properly signalled container cropping,
93 // in an F4V file. Just set the coded_width/height, don't
94 // touch the cropped ones.
95 4 s->avctx->coded_width = 16 * cols;
96 4 s->avctx->coded_height = 16 * rows;
97 } else {
98 7 ret = ff_set_dimensions(s->avctx, 16 * cols, 16 * rows);
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ret < 0)
100 return ret;
101
102
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (s->avctx->extradata_size == 1) {
103 2 s->avctx->width -= s->avctx->extradata[0] >> 4;
104 2 s->avctx->height -= s->avctx->extradata[0] & 0x0F;
105 }
106 }
107 11 res = VP56_SIZE_CHANGE;
108 }
109
110 39 ret = ff_vp56_init_range_decoder(c, buf+6, buf_size-6);
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (ret < 0)
112 goto fail;
113 39 vp56_rac_gets(c, 2);
114
115 39 parse_filter_info = s->filter_header;
116
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 25 times.
39 if (sub_version < 8)
117 14 vrt_shift = 5;
118 39 s->sub_version = sub_version;
119 39 s->golden_frame = 0;
120 } else {
121
3/6
✓ Branch 0 taken 674 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 674 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 674 times.
674 if (!s->sub_version || !s->avctx->coded_width || !s->avctx->coded_height)
122 return AVERROR_INVALIDDATA;
123
124
3/4
✓ Branch 0 taken 548 times.
✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 548 times.
674 if (separated_coeff || !s->filter_header) {
125 126 coeff_offset = AV_RB16(buf+1) - 2;
126 126 buf += 2;
127 126 buf_size -= 2;
128 }
129 674 ret = ff_vp56_init_range_decoder(c, buf+1, buf_size-1);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 674 times.
674 if (ret < 0)
131 return ret;
132
133 674 s->golden_frame = vp56_rac_get(c);
134
2/2
✓ Branch 0 taken 548 times.
✓ Branch 1 taken 126 times.
674 if (s->filter_header) {
135 548 s->deblock_filtering = vp56_rac_get(c);
136
1/2
✓ Branch 0 taken 548 times.
✗ Branch 1 not taken.
548 if (s->deblock_filtering)
137 548 vp56_rac_get(c);
138
2/2
✓ Branch 0 taken 429 times.
✓ Branch 1 taken 119 times.
548 if (s->sub_version > 7)
139 429 parse_filter_info = vp56_rac_get(c);
140 }
141 }
142
143
2/2
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 546 times.
713 if (parse_filter_info) {
144
1/2
✓ Branch 1 taken 167 times.
✗ Branch 2 not taken.
167 if (vp56_rac_get(c)) {
145 167 s->filter_mode = 2;
146 167 s->sample_variance_threshold = vp56_rac_gets(c, 5) << vrt_shift;
147 167 s->max_vector_length = 2 << vp56_rac_gets(c, 3);
148 } else if (vp56_rac_get(c)) {
149 s->filter_mode = 1;
150 } else {
151 s->filter_mode = 0;
152 }
153
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 3 times.
167 if (s->sub_version > 7)
154 164 s->filter_selection = vp56_rac_gets(c, 4);
155 else
156 3 s->filter_selection = 16;
157 }
158
159 713 s->use_huffman = vp56_rac_get(c);
160
161 713 s->parse_coeff = vp6_parse_coeff;
162
2/2
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 576 times.
713 if (coeff_offset) {
163 137 buf += coeff_offset;
164 137 buf_size -= coeff_offset;
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137 times.
137 if (buf_size < 0) {
166 ret = AVERROR_INVALIDDATA;
167 goto fail;
168 }
169
2/2
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 20 times.
137 if (s->use_huffman) {
170 117 s->parse_coeff = vp6_parse_coeff_huffman;
171 117 ret = init_get_bits8(&s->gb, buf, buf_size);
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117 times.
117 if (ret < 0)
173 return ret;
174 } else {
175 20 ret = ff_vp56_init_range_decoder(&s->cc, buf, buf_size);
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0)
177 goto fail;
178 20 s->ccp = &s->cc;
179 }
180 } else {
181 576 s->ccp = &s->c;
182 }
183
184 713 return res;
185 fail:
186 if (res == VP56_SIZE_CHANGE)
187 ff_set_dimensions(s->avctx, 0, 0);
188 return ret;
189 }
190
191 40 static void vp6_coeff_order_table_init(VP56Context *s)
192 {
193 40 int i, pos, idx = 1;
194
195 40 s->modelp->coeff_index_to_pos[0] = 0;
196
2/2
✓ Branch 0 taken 640 times.
✓ Branch 1 taken 40 times.
680 for (i=0; i<16; i++)
197
2/2
✓ Branch 0 taken 40320 times.
✓ Branch 1 taken 640 times.
40960 for (pos=1; pos<64; pos++)
198
2/2
✓ Branch 0 taken 2520 times.
✓ Branch 1 taken 37800 times.
40320 if (s->modelp->coeff_reorder[pos] == i)
199 2520 s->modelp->coeff_index_to_pos[idx++] = pos;
200
201
2/2
✓ Branch 0 taken 2560 times.
✓ Branch 1 taken 40 times.
2600 for (idx = 0; idx < 64; idx++) {
202 2560 int max = 0;
203
2/2
✓ Branch 0 taken 83200 times.
✓ Branch 1 taken 2560 times.
85760 for (i = 0; i <= idx; i++) {
204 83200 int v = s->modelp->coeff_index_to_pos[i];
205
2/2
✓ Branch 0 taken 80640 times.
✓ Branch 1 taken 2560 times.
83200 if (v > max)
206 80640 max = v;
207 }
208
1/2
✓ Branch 0 taken 2560 times.
✗ Branch 1 not taken.
2560 if (s->sub_version > 6)
209 2560 max++;
210 2560 s->modelp->coeff_index_to_idct_selector[idx] = max;
211 }
212 40 }
213
214 39 static void vp6_default_models_init(VP56Context *s)
215 {
216 39 VP56Model *model = s->modelp;
217
218 39 model->vector_dct[0] = 0xA2;
219 39 model->vector_dct[1] = 0xA4;
220 39 model->vector_sig[0] = 0x80;
221 39 model->vector_sig[1] = 0x80;
222
223 39 memcpy(model->mb_types_stats, ff_vp56_def_mb_types_stats, sizeof(model->mb_types_stats));
224 39 memcpy(model->vector_fdv, vp6_def_fdv_vector_model, sizeof(model->vector_fdv));
225 39 memcpy(model->vector_pdv, vp6_def_pdv_vector_model, sizeof(model->vector_pdv));
226 39 memcpy(model->coeff_runv, vp6_def_runv_coeff_model, sizeof(model->coeff_runv));
227 39 memcpy(model->coeff_reorder, vp6_def_coeff_reorder, sizeof(model->coeff_reorder));
228
229 39 vp6_coeff_order_table_init(s);
230 39 }
231
232 674 static void vp6_parse_vector_models(VP56Context *s)
233 {
234 674 VP56RangeCoder *c = &s->c;
235 674 VP56Model *model = s->modelp;
236 int comp, node;
237
238
2/2
✓ Branch 0 taken 1348 times.
✓ Branch 1 taken 674 times.
2022 for (comp=0; comp<2; comp++) {
239
2/2
✓ Branch 1 taken 109 times.
✓ Branch 2 taken 1239 times.
1348 if (vp56_rac_get_prob_branchy(c, vp6_sig_dct_pct[comp][0]))
240 109 model->vector_dct[comp] = vp56_rac_gets_nn(c, 7);
241
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 1331 times.
1348 if (vp56_rac_get_prob_branchy(c, vp6_sig_dct_pct[comp][1]))
242 17 model->vector_sig[comp] = vp56_rac_gets_nn(c, 7);
243 }
244
245
2/2
✓ Branch 0 taken 1348 times.
✓ Branch 1 taken 674 times.
2022 for (comp=0; comp<2; comp++)
246
2/2
✓ Branch 0 taken 9436 times.
✓ Branch 1 taken 1348 times.
10784 for (node=0; node<7; node++)
247
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 9412 times.
9436 if (vp56_rac_get_prob_branchy(c, vp6_pdv_pct[comp][node]))
248 24 model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
249
250
2/2
✓ Branch 0 taken 1348 times.
✓ Branch 1 taken 674 times.
2022 for (comp=0; comp<2; comp++)
251
2/2
✓ Branch 0 taken 10784 times.
✓ Branch 1 taken 1348 times.
12132 for (node=0; node<8; node++)
252
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 10774 times.
10784 if (vp56_rac_get_prob_branchy(c, vp6_fdv_pct[comp][node]))
253 10 model->vector_fdv[comp][node] = vp56_rac_gets_nn(c, 7);
254 674 }
255
256 /* nodes must ascend by count, but with descending symbol order */
257 149929 static int vp6_huff_cmp(const void *va, const void *vb)
258 {
259 149929 const Node *a = va, *b = vb;
260 149929 return (a->count - b->count)*16 + (b->sym - a->sym);
261 }
262
263 4680 static int vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[],
264 const uint8_t *map, unsigned size, VLC *vlc)
265 {
266 4680 Node nodes[2*VP6_MAX_HUFF_SIZE], *tmp = &nodes[size];
267 int a, b, i;
268
269 /* first compute probabilities from model */
270 4680 tmp[0].count = 256;
271
2/2
✓ Branch 0 taken 50778 times.
✓ Branch 1 taken 4680 times.
55458 for (i=0; i<size-1; i++) {
272 50778 a = tmp[i].count * coeff_model[i] >> 8;
273 50778 b = tmp[i].count * (255 - coeff_model[i]) >> 8;
274 50778 nodes[map[2*i ]].count = a + !a;
275 50778 nodes[map[2*i+1]].count = b + !b;
276 }
277
278 4680 ff_free_vlc(vlc);
279 /* then build the huffman tree according to probabilities */
280 4680 return ff_huff_build_tree(s->avctx, vlc, size, FF_HUFFMAN_BITS,
281 nodes, vp6_huff_cmp,
282 FF_HUFFMAN_FLAG_HNODE_FIRST);
283 }
284
285 713 static int vp6_parse_coeff_models(VP56Context *s)
286 {
287 713 VP56RangeCoder *c = &s->c;
288 713 VP56Model *model = s->modelp;
289 int def_prob[11];
290 int node, cg, ctx, pos;
291 int ct; /* code type */
292 int pt; /* plane type (0 for Y, 1 for U or V) */
293
294 713 memset(def_prob, 0x80, sizeof(def_prob));
295
296
2/2
✓ Branch 0 taken 1426 times.
✓ Branch 1 taken 713 times.
2139 for (pt=0; pt<2; pt++)
297
2/2
✓ Branch 0 taken 15686 times.
✓ Branch 1 taken 1426 times.
17112 for (node=0; node<11; node++)
298
2/2
✓ Branch 1 taken 1101 times.
✓ Branch 2 taken 14585 times.
15686 if (vp56_rac_get_prob_branchy(c, vp6_dccv_pct[pt][node])) {
299 1101 def_prob[node] = vp56_rac_gets_nn(c, 7);
300 1101 model->coeff_dccv[pt][node] = def_prob[node];
301
2/2
✓ Branch 0 taken 562 times.
✓ Branch 1 taken 14023 times.
14585 } else if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
302 562 model->coeff_dccv[pt][node] = def_prob[node];
303 }
304
305
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 712 times.
713 if (vp56_rac_get(c)) {
306
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 1 times.
64 for (pos=1; pos<64; pos++)
307
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
63 if (vp56_rac_get_prob_branchy(c, vp6_coeff_reorder_pct[pos]))
308 model->coeff_reorder[pos] = vp56_rac_gets(c, 4);
309 1 vp6_coeff_order_table_init(s);
310 }
311
312
2/2
✓ Branch 0 taken 1426 times.
✓ Branch 1 taken 713 times.
2139 for (cg=0; cg<2; cg++)
313
2/2
✓ Branch 0 taken 19964 times.
✓ Branch 1 taken 1426 times.
21390 for (node=0; node<14; node++)
314
2/2
✓ Branch 1 taken 812 times.
✓ Branch 2 taken 19152 times.
19964 if (vp56_rac_get_prob_branchy(c, vp6_runv_pct[cg][node]))
315 812 model->coeff_runv[cg][node] = vp56_rac_gets_nn(c, 7);
316
317
2/2
✓ Branch 0 taken 2139 times.
✓ Branch 1 taken 713 times.
2852 for (ct=0; ct<3; ct++)
318
2/2
✓ Branch 0 taken 4278 times.
✓ Branch 1 taken 2139 times.
6417 for (pt=0; pt<2; pt++)
319
2/2
✓ Branch 0 taken 25668 times.
✓ Branch 1 taken 4278 times.
29946 for (cg=0; cg<6; cg++)
320
2/2
✓ Branch 0 taken 282348 times.
✓ Branch 1 taken 25668 times.
308016 for (node=0; node<11; node++)
321
2/2
✓ Branch 1 taken 4801 times.
✓ Branch 2 taken 277547 times.
282348 if (vp56_rac_get_prob_branchy(c, vp6_ract_pct[ct][pt][cg][node])) {
322 4801 def_prob[node] = vp56_rac_gets_nn(c, 7);
323 4801 model->coeff_ract[pt][ct][cg][node] = def_prob[node];
324
2/2
✓ Branch 0 taken 13830 times.
✓ Branch 1 taken 263717 times.
277547 } else if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
325 13830 model->coeff_ract[pt][ct][cg][node] = def_prob[node];
326 }
327
328
2/2
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 596 times.
713 if (s->use_huffman) {
329
2/2
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 117 times.
351 for (pt=0; pt<2; pt++) {
330
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 234 times.
234 if (vp6_build_huff_tree(s, model->coeff_dccv[pt],
331 vp6_huff_coeff_map, 12, &s->dccv_vlc[pt]))
332 return -1;
333
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 234 times.
234 if (vp6_build_huff_tree(s, model->coeff_runv[pt],
334 vp6_huff_run_map, 9, &s->runv_vlc[pt]))
335 return -1;
336
2/2
✓ Branch 0 taken 702 times.
✓ Branch 1 taken 234 times.
936 for (ct=0; ct<3; ct++)
337
2/2
✓ Branch 0 taken 4212 times.
✓ Branch 1 taken 702 times.
4914 for (cg = 0; cg < 6; cg++)
338
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4212 times.
4212 if (vp6_build_huff_tree(s, model->coeff_ract[pt][ct][cg],
339 vp6_huff_coeff_map, 12,
340 &s->ract_vlc[pt][ct][cg]))
341 return -1;
342 }
343 117 memset(s->nb_null, 0, sizeof(s->nb_null));
344 } else {
345 /* coeff_dcct is a linear combination of coeff_dccv */
346
2/2
✓ Branch 0 taken 1192 times.
✓ Branch 1 taken 596 times.
1788 for (pt=0; pt<2; pt++)
347
2/2
✓ Branch 0 taken 3576 times.
✓ Branch 1 taken 1192 times.
4768 for (ctx=0; ctx<3; ctx++)
348
2/2
✓ Branch 0 taken 17880 times.
✓ Branch 1 taken 3576 times.
21456 for (node=0; node<5; node++)
349 17880 model->coeff_dcct[pt][ctx][node] = av_clip(((model->coeff_dccv[pt][node] * vp6_dccv_lc[ctx][node][0] + 128) >> 8) + vp6_dccv_lc[ctx][node][1], 1, 255);
350 }
351 713 return 0;
352 }
353
354 18340 static void vp6_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
355 {
356 18340 VP56RangeCoder *c = &s->c;
357 18340 VP56Model *model = s->modelp;
358 int comp;
359
360 18340 *vect = (VP56mv) {0,0};
361
2/2
✓ Branch 0 taken 15667 times.
✓ Branch 1 taken 2673 times.
18340 if (s->vector_candidate_pos < 2)
362 15667 *vect = s->vector_candidate[0];
363
364
2/2
✓ Branch 0 taken 36680 times.
✓ Branch 1 taken 18340 times.
55020 for (comp=0; comp<2; comp++) {
365 36680 int i, delta = 0;
366
367
2/2
✓ Branch 1 taken 8621 times.
✓ Branch 2 taken 28059 times.
36680 if (vp56_rac_get_prob_branchy(c, model->vector_dct[comp])) {
368 static const uint8_t prob_order[] = {0, 1, 2, 7, 6, 5, 4};
369
2/2
✓ Branch 0 taken 60347 times.
✓ Branch 1 taken 8621 times.
68968 for (i=0; i<sizeof(prob_order); i++) {
370 60347 int j = prob_order[i];
371 60347 delta |= vp56_rac_get_prob(c, model->vector_fdv[comp][j])<<j;
372 }
373
2/2
✓ Branch 0 taken 4328 times.
✓ Branch 1 taken 4293 times.
8621 if (delta & 0xF0)
374 4328 delta |= vp56_rac_get_prob(c, model->vector_fdv[comp][3])<<3;
375 else
376 4293 delta |= 8;
377 } else {
378 28059 delta = vp56_rac_get_tree(c, ff_vp56_pva_tree,
379 28059 model->vector_pdv[comp]);
380 }
381
382
4/4
✓ Branch 0 taken 28077 times.
✓ Branch 1 taken 8603 times.
✓ Branch 3 taken 13620 times.
✓ Branch 4 taken 14457 times.
36680 if (delta && vp56_rac_get_prob_branchy(c, model->vector_sig[comp]))
383 13620 delta = -delta;
384
385
2/2
✓ Branch 0 taken 18340 times.
✓ Branch 1 taken 18340 times.
36680 if (!comp)
386 18340 vect->x += delta;
387 else
388 18340 vect->y += delta;
389 }
390 18340 }
391
392 /**
393 * Read number of consecutive blocks with null DC or AC.
394 * This value is < 74.
395 */
396 28576 static unsigned vp6_get_nb_null(VP56Context *s)
397 {
398 28576 unsigned val = get_bits(&s->gb, 2);
399
2/2
✓ Branch 0 taken 4892 times.
✓ Branch 1 taken 23684 times.
28576 if (val == 2)
400 4892 val += get_bits(&s->gb, 2);
401
2/2
✓ Branch 0 taken 806 times.
✓ Branch 1 taken 22878 times.
23684 else if (val == 3) {
402 806 val = get_bits1(&s->gb) << 2;
403 806 val = 6+val + get_bits(&s->gb, 2+val);
404 }
405 28576 return val;
406 }
407
408 16848 static int vp6_parse_coeff_huffman(VP56Context *s)
409 {
410 16848 VP56Model *model = s->modelp;
411 16848 uint8_t *permute = s->idct_scantable;
412 VLC *vlc_coeff;
413 int coeff, sign, coeff_idx;
414 int b, cg, idx;
415 16848 int pt = 0; /* plane type (0 for Y, 1 for U or V) */
416
417
2/2
✓ Branch 0 taken 101088 times.
✓ Branch 1 taken 16848 times.
117936 for (b=0; b<6; b++) {
418 101088 int ct = 0; /* code type */
419
2/2
✓ Branch 0 taken 33696 times.
✓ Branch 1 taken 67392 times.
101088 if (b > 3) pt = 1;
420 101088 vlc_coeff = &s->dccv_vlc[pt];
421
422 101088 for (coeff_idx = 0;;) {
423 1161120 int run = 1;
424
4/4
✓ Branch 0 taken 202176 times.
✓ Branch 1 taken 958944 times.
✓ Branch 2 taken 28871 times.
✓ Branch 3 taken 173305 times.
1161120 if (coeff_idx<2 && s->nb_null[coeff_idx][pt]) {
425 28871 s->nb_null[coeff_idx][pt]--;
426
2/2
✓ Branch 0 taken 11362 times.
✓ Branch 1 taken 17509 times.
28871 if (coeff_idx)
427 11362 break;
428 } else {
429
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1132249 times.
1132249 if (get_bits_left(&s->gb) <= 0)
430 return AVERROR_INVALIDDATA;
431 1132249 coeff = get_vlc2(&s->gb, vlc_coeff->table, FF_HUFFMAN_BITS, 3);
432
2/2
✓ Branch 0 taken 345601 times.
✓ Branch 1 taken 786648 times.
1132249 if (coeff == 0) {
433
2/2
✓ Branch 0 taken 326823 times.
✓ Branch 1 taken 18778 times.
345601 if (coeff_idx) {
434 326823 int pt = (coeff_idx >= 6);
435 326823 run += get_vlc2(&s->gb, s->runv_vlc[pt].table, FF_HUFFMAN_BITS, 3);
436
2/2
✓ Branch 0 taken 60162 times.
✓ Branch 1 taken 266661 times.
326823 if (run >= 9)
437 60162 run += get_bits(&s->gb, 6);
438 } else
439 18778 s->nb_null[0][pt] = vp6_get_nb_null(s);
440 345601 ct = 0;
441
2/2
✓ Branch 0 taken 88872 times.
✓ Branch 1 taken 697776 times.
786648 } else if (coeff == 11) { /* end of block */
442
2/2
✓ Branch 0 taken 9798 times.
✓ Branch 1 taken 79074 times.
88872 if (coeff_idx == 1) /* first AC coeff ? */
443 9798 s->nb_null[1][pt] = vp6_get_nb_null(s);
444 88872 break;
445 } else {
446 697776 int coeff2 = ff_vp56_coeff_bias[coeff];
447
2/2
✓ Branch 0 taken 39048 times.
✓ Branch 1 taken 658728 times.
697776 if (coeff > 4)
448
2/2
✓ Branch 0 taken 38623 times.
✓ Branch 1 taken 425 times.
39048 coeff2 += get_bits(&s->gb, coeff <= 9 ? coeff - 4 : 11);
449
2/2
✓ Branch 0 taken 188610 times.
✓ Branch 1 taken 509166 times.
697776 ct = 1 + (coeff2 > 1);
450 697776 sign = get_bits1(&s->gb);
451 697776 coeff2 = (coeff2 ^ -sign) + sign;
452
2/2
✓ Branch 0 taken 632975 times.
✓ Branch 1 taken 64801 times.
697776 if (coeff_idx)
453 632975 coeff2 *= s->dequant_ac;
454 697776 idx = model->coeff_index_to_pos[coeff_idx];
455 697776 s->block_coeff[b][permute[idx]] = coeff2;
456 }
457 }
458 1060886 coeff_idx+=run;
459
2/2
✓ Branch 0 taken 854 times.
✓ Branch 1 taken 1060032 times.
1060886 if (coeff_idx >= 64)
460 854 break;
461 1060032 cg = FFMIN(vp6_coeff_groups[coeff_idx], 3);
462 1060032 vlc_coeff = &s->ract_vlc[pt][ct][cg];
463 }
464 101088 s->idct_selector[b] = model->coeff_index_to_idct_selector[FFMIN(coeff_idx, 63)];
465 }
466 16848 return 0;
467 }
468
469 78707 static int vp6_parse_coeff(VP56Context *s)
470 {
471 78707 VP56RangeCoder *c = s->ccp;
472 78707 VP56Model *model = s->modelp;
473 78707 uint8_t *permute = s->idct_scantable;
474 uint8_t *model1, *model2, *model3;
475 int coeff, sign, coeff_idx;
476 int b, i, cg, idx, ctx;
477 78707 int pt = 0; /* plane type (0 for Y, 1 for U or V) */
478
479
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 78707 times.
78707 if (vpX_rac_is_end(c)) {
480 av_log(s->avctx, AV_LOG_ERROR, "End of AC stream reached in vp6_parse_coeff\n");
481 return AVERROR_INVALIDDATA;
482 }
483
484
2/2
✓ Branch 0 taken 472242 times.
✓ Branch 1 taken 78707 times.
550949 for (b=0; b<6; b++) {
485 472242 int ct = 1; /* code type */
486 472242 int run = 1;
487
488
2/2
✓ Branch 0 taken 157414 times.
✓ Branch 1 taken 314828 times.
472242 if (b > 3) pt = 1;
489
490 472242 ctx = s->left_block[ff_vp56_b6to4[b]].not_null_dc
491 472242 + s->above_blocks[s->above_block_idx[b]].not_null_dc;
492 472242 model1 = model->coeff_dccv[pt];
493 472242 model2 = model->coeff_dcct[pt][ctx];
494
495 472242 coeff_idx = 0;
496 for (;;) {
497
6/6
✓ Branch 0 taken 894321 times.
✓ Branch 1 taken 944484 times.
✓ Branch 2 taken 593085 times.
✓ Branch 3 taken 301236 times.
✓ Branch 5 taken 370069 times.
✓ Branch 6 taken 1167500 times.
1838805 if ((coeff_idx>1 && ct==0) || vp56_rac_get_prob_branchy(c, model2[0])) {
498 /* parse a coeff */
499
2/2
✓ Branch 1 taken 159041 times.
✓ Branch 2 taken 512264 times.
671305 if (vp56_rac_get_prob_branchy(c, model2[2])) {
500
2/2
✓ Branch 1 taken 33140 times.
✓ Branch 2 taken 125901 times.
159041 if (vp56_rac_get_prob_branchy(c, model2[3])) {
501 33140 idx = vp56_rac_get_tree(c, ff_vp56_pc_tree, model1);
502 33140 coeff = ff_vp56_coeff_bias[idx+5];
503
2/2
✓ Branch 0 taken 72669 times.
✓ Branch 1 taken 33140 times.
105809 for (i=ff_vp56_coeff_bit_length[idx]; i>=0; i--)
504 72669 coeff += vp56_rac_get_prob(c, ff_vp56_coeff_parse_table[idx][i]) << i;
505 } else {
506
2/2
✓ Branch 1 taken 39387 times.
✓ Branch 2 taken 86514 times.
125901 if (vp56_rac_get_prob_branchy(c, model2[4]))
507 39387 coeff = 3 + vp56_rac_get_prob(c, model1[5]);
508 else
509 86514 coeff = 2;
510 }
511 159041 ct = 2;
512 } else {
513 512264 ct = 1;
514 512264 coeff = 1;
515 }
516 671305 sign = vp56_rac_get(c);
517 671305 coeff = (coeff ^ -sign) + sign;
518
2/2
✓ Branch 0 taken 594035 times.
✓ Branch 1 taken 77270 times.
671305 if (coeff_idx)
519 594035 coeff *= s->dequant_ac;
520 671305 idx = model->coeff_index_to_pos[coeff_idx];
521 671305 s->block_coeff[b][permute[idx]] = coeff;
522 671305 run = 1;
523 } else {
524 /* parse a run */
525 1167500 ct = 0;
526
2/2
✓ Branch 0 taken 772528 times.
✓ Branch 1 taken 394972 times.
1167500 if (coeff_idx > 0) {
527
2/2
✓ Branch 1 taken 471292 times.
✓ Branch 2 taken 301236 times.
772528 if (!vp56_rac_get_prob_branchy(c, model2[1]))
528 471292 break;
529
530 301236 model3 = model->coeff_runv[coeff_idx >= 6];
531 301236 run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
532
2/2
✓ Branch 0 taken 49039 times.
✓ Branch 1 taken 252197 times.
301236 if (!run)
533
2/2
✓ Branch 0 taken 294234 times.
✓ Branch 1 taken 49039 times.
343273 for (run=9, i=0; i<6; i++)
534 294234 run += vp56_rac_get_prob(c, model3[i+8]) << i;
535 }
536 }
537 1367513 coeff_idx += run;
538
2/2
✓ Branch 0 taken 950 times.
✓ Branch 1 taken 1366563 times.
1367513 if (coeff_idx >= 64)
539 950 break;
540 1366563 cg = vp6_coeff_groups[coeff_idx];
541 1366563 model1 = model2 = model->coeff_ract[pt][ct][cg];
542 }
543
544 472242 s->left_block[ff_vp56_b6to4[b]].not_null_dc =
545 472242 s->above_blocks[s->above_block_idx[b]].not_null_dc = !!s->block_coeff[b][0];
546 472242 s->idct_selector[b] = model->coeff_index_to_idct_selector[FFMIN(coeff_idx, 63)];
547 }
548 78707 return 0;
549 }
550
551 84892 static int vp6_block_variance(uint8_t *src, ptrdiff_t stride)
552 {
553 84892 int sum = 0, square_sum = 0;
554 int y, x;
555
556
2/2
✓ Branch 0 taken 339568 times.
✓ Branch 1 taken 84892 times.
424460 for (y=0; y<8; y+=2) {
557
2/2
✓ Branch 0 taken 1358272 times.
✓ Branch 1 taken 339568 times.
1697840 for (x=0; x<8; x+=2) {
558 1358272 sum += src[x];
559 1358272 square_sum += src[x]*src[x];
560 }
561 339568 src += 2*stride;
562 }
563 84892 return (16*square_sum - sum*sum) >> 8;
564 }
565
566 32615 static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, ptrdiff_t stride,
567 int delta, const int16_t *weights)
568 {
569 int x, y;
570
571
2/2
✓ Branch 0 taken 260920 times.
✓ Branch 1 taken 32615 times.
293535 for (y=0; y<8; y++) {
572
2/2
✓ Branch 0 taken 2087360 times.
✓ Branch 1 taken 260920 times.
2348280 for (x=0; x<8; x++) {
573 2087360 dst[x] = av_clip_uint8(( src[x-delta ] * weights[0]
574 2087360 + src[x ] * weights[1]
575 2087360 + src[x+delta ] * weights[2]
576 2087360 + src[x+2*delta] * weights[3] + 64) >> 7);
577 }
578 260920 src += stride;
579 260920 dst += stride;
580 }
581 32615 }
582
583 52432 static void vp6_filter_diag2(VP56Context *s, uint8_t *dst, uint8_t *src,
584 ptrdiff_t stride, int h_weight, int v_weight)
585 {
586 52432 uint8_t *tmp = s->edge_emu_buffer+16;
587 52432 s->h264chroma.put_h264_chroma_pixels_tab[0](tmp, src, stride, 9, h_weight, 0);
588 52432 s->h264chroma.put_h264_chroma_pixels_tab[0](dst, tmp, stride, 8, 0, v_weight);
589 52432 }
590
591 171252 static void vp6_filter(VP56Context *s, uint8_t *dst, uint8_t *src,
592 int offset1, int offset2, ptrdiff_t stride,
593 VP56mv mv, int mask, int select, int luma)
594 {
595 171252 int filter4 = 0;
596 171252 int x8 = mv.x & mask;
597 171252 int y8 = mv.y & mask;
598
599
2/2
✓ Branch 0 taken 116166 times.
✓ Branch 1 taken 55086 times.
171252 if (luma) {
600 116166 x8 *= 2;
601 116166 y8 *= 2;
602 116166 filter4 = s->filter_mode;
603
2/2
✓ Branch 0 taken 105317 times.
✓ Branch 1 taken 10849 times.
116166 if (filter4 == 2) {
604
1/2
✓ Branch 0 taken 105317 times.
✗ Branch 1 not taken.
105317 if (s->max_vector_length &&
605
2/2
✓ Branch 0 taken 92552 times.
✓ Branch 1 taken 12765 times.
105317 (FFABS(mv.x) > s->max_vector_length ||
606
2/2
✓ Branch 0 taken 7660 times.
✓ Branch 1 taken 84892 times.
92552 FFABS(mv.y) > s->max_vector_length)) {
607 20425 filter4 = 0;
608
1/2
✓ Branch 0 taken 84892 times.
✗ Branch 1 not taken.
84892 } else if (s->sample_variance_threshold
609 84892 && (vp6_block_variance(src+offset1, stride)
610
2/2
✓ Branch 0 taken 24931 times.
✓ Branch 1 taken 59961 times.
84892 < s->sample_variance_threshold)) {
611 24931 filter4 = 0;
612 }
613 }
614 }
615
616
8/8
✓ Branch 0 taken 133105 times.
✓ Branch 1 taken 38147 times.
✓ Branch 2 taken 58954 times.
✓ Branch 3 taken 74151 times.
✓ Branch 4 taken 38147 times.
✓ Branch 5 taken 58954 times.
✓ Branch 6 taken 14972 times.
✓ Branch 7 taken 23175 times.
171252 if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
617 89123 offset1 = offset2;
618 }
619
620
2/2
✓ Branch 0 taken 59961 times.
✓ Branch 1 taken 111291 times.
171252 if (filter4) {
621
2/2
✓ Branch 0 taken 12003 times.
✓ Branch 1 taken 47958 times.
59961 if (!y8) { /* left or right combine */
622 12003 vp6_filter_hv4(dst, src+offset1, stride, 1,
623 12003 vp6_block_copy_filter[select][x8]);
624
2/2
✓ Branch 0 taken 20612 times.
✓ Branch 1 taken 27346 times.
47958 } else if (!x8) { /* above or below combine */
625 20612 vp6_filter_hv4(dst, src+offset1, stride, stride,
626 20612 vp6_block_copy_filter[select][y8]);
627 } else {
628 27346 s->vp56dsp.vp6_filter_diag4(dst, src+offset1+((mv.x^mv.y)>>31), stride,
629 27346 vp6_block_copy_filter[select][x8],
630 27346 vp6_block_copy_filter[select][y8]);
631 }
632 } else {
633
4/4
✓ Branch 0 taken 78576 times.
✓ Branch 1 taken 32715 times.
✓ Branch 2 taken 26144 times.
✓ Branch 3 taken 52432 times.
111291 if (!x8 || !y8) {
634 58859 s->h264chroma.put_h264_chroma_pixels_tab[0](dst, src + offset1, stride, 8, x8, y8);
635 } else {
636 52432 vp6_filter_diag2(s, dst, src+offset1 + ((mv.x^mv.y)>>31), stride, x8, y8);
637 }
638 }
639 171252 }
640
641 17 static av_cold int vp6_decode_init_context(AVCodecContext *avctx,
642 VP56Context *s, int flip, int has_alpha)
643 {
644 17 int ret = ff_vp56_init_context(avctx, s, flip, has_alpha);
645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ret < 0)
646 return ret;
647
648 17 ff_vp6dsp_init(&s->vp56dsp);
649
650 17 s->deblock_filtering = 0;
651 17 s->vp56_coord_div = vp6_coord_div;
652 17 s->parse_vector_adjustment = vp6_parse_vector_adjustment;
653 17 s->filter = vp6_filter;
654 17 s->default_models_init = vp6_default_models_init;
655 17 s->parse_vector_models = vp6_parse_vector_models;
656 17 s->parse_coeff_models = vp6_parse_coeff_models;
657 17 s->parse_header = vp6_parse_header;
658
659 17 return 0;
660 }
661
662 13 static av_cold int vp6_decode_init(AVCodecContext *avctx)
663 {
664 13 VP56Context *s = avctx->priv_data;
665 int ret;
666
667 13 ret = vp6_decode_init_context(avctx, s, avctx->codec_id == AV_CODEC_ID_VP6,
668 13 avctx->codec_id == AV_CODEC_ID_VP6A);
669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0)
670 return ret;
671
672
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9 times.
13 if (s->has_alpha) {
673 /* Can only happen for ff_vp6a_decoder */
674 4 s->alpha_context = &s[1];
675 4 ret = vp6_decode_init_context(avctx, s->alpha_context,
676 4 s->flip == -1, s->has_alpha);
677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
678 return ret;
679 }
680
681 13 return 0;
682 }
683
684 static av_cold void vp6_decode_free_context(VP56Context *s);
685
686 13 static av_cold int vp6_decode_free(AVCodecContext *avctx)
687 {
688 13 VP56Context *s = avctx->priv_data;
689
690 13 vp6_decode_free_context(s);
691
692
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9 times.
13 if (s->alpha_context) {
693 4 vp6_decode_free_context(s->alpha_context);
694 4 s->alpha_context = NULL;
695 }
696
697 13 return 0;
698 }
699
700 17 static av_cold void vp6_decode_free_context(VP56Context *s)
701 {
702 int pt, ct, cg;
703
704 17 ff_vp56_free_context(s);
705
706
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 17 times.
51 for (pt=0; pt<2; pt++) {
707 34 ff_free_vlc(&s->dccv_vlc[pt]);
708 34 ff_free_vlc(&s->runv_vlc[pt]);
709
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 34 times.
136 for (ct=0; ct<3; ct++)
710
2/2
✓ Branch 0 taken 612 times.
✓ Branch 1 taken 102 times.
714 for (cg=0; cg<6; cg++)
711 612 ff_free_vlc(&s->ract_vlc[pt][ct][cg]);
712 }
713 17 }
714
715 const FFCodec ff_vp6_decoder = {
716 .p.name = "vp6",
717 .p.long_name = NULL_IF_CONFIG_SMALL("On2 VP6"),
718 .p.type = AVMEDIA_TYPE_VIDEO,
719 .p.id = AV_CODEC_ID_VP6,
720 .priv_data_size = sizeof(VP56Context),
721 .init = vp6_decode_init,
722 .close = vp6_decode_free,
723 FF_CODEC_DECODE_CB(ff_vp56_decode_frame),
724 .p.capabilities = AV_CODEC_CAP_DR1,
725 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
726 };
727
728 /* flash version, not flipped upside-down */
729 const FFCodec ff_vp6f_decoder = {
730 .p.name = "vp6f",
731 .p.long_name = NULL_IF_CONFIG_SMALL("On2 VP6 (Flash version)"),
732 .p.type = AVMEDIA_TYPE_VIDEO,
733 .p.id = AV_CODEC_ID_VP6F,
734 .priv_data_size = sizeof(VP56Context),
735 .init = vp6_decode_init,
736 .close = vp6_decode_free,
737 FF_CODEC_DECODE_CB(ff_vp56_decode_frame),
738 .p.capabilities = AV_CODEC_CAP_DR1,
739 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
740 };
741
742 /* flash version, not flipped upside-down, with alpha channel */
743 const FFCodec ff_vp6a_decoder = {
744 .p.name = "vp6a",
745 .p.long_name = NULL_IF_CONFIG_SMALL("On2 VP6 (Flash version, with alpha channel)"),
746 .p.type = AVMEDIA_TYPE_VIDEO,
747 .p.id = AV_CODEC_ID_VP6A,
748 .priv_data_size = 2 /* Main context + alpha context */ * sizeof(VP56Context),
749 .init = vp6_decode_init,
750 .close = vp6_decode_free,
751 FF_CODEC_DECODE_CB(ff_vp56_decode_frame),
752 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
753 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
754 };
755