FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp6.c
Date: 2021-09-24 03:35:22
Exec Total Coverage
Lines: 406 434 93.5%
Branches: 260 294 88.4%

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