FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp6.c
Date: 2024-04-12 08:31:17
Exec Total Coverage
Lines: 408 438 93.2%
Functions: 19 19 100.0%
Branches: 264 300 88.0%

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