FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp6.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 412 439 93.8%
Functions: 19 19 100.0%
Branches: 267 300 89.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 1018 static int vp6_parse_header(VP56Context *s, const uint8_t *buf, int buf_size)
49 {
50 1018 VPXRangeCoder *c = &s->c;
51 1018 int parse_filter_info = 0;
52 1018 int coeff_offset = 0;
53 1018 int vrt_shift = 0;
54 int sub_version;
55 int rows, cols;
56 1018 int res = 0;
57 int ret;
58 1018 int separated_coeff = buf[0] & 1;
59
60
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 977 times.
1018 if (!(buf[0] & 0x80))
61 41 s->frames[VP56_FRAME_CURRENT]->flags |= AV_FRAME_FLAG_KEY;
62 else
63 977 s->frames[VP56_FRAME_CURRENT]->flags &= ~AV_FRAME_FLAG_KEY;
64 1018 ff_vp56_init_dequant(s, (buf[0] >> 1) & 0x3F);
65
66
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 977 times.
1018 if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
67 41 sub_version = buf[1] >> 3;
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 if (sub_version > 8)
69 return AVERROR_INVALIDDATA;
70 41 s->filter_header = buf[1] & 0x06;
71 41 s->interlaced = buf[1] & 1;
72
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 39 times.
41 if (s->interlaced)
73 2 s->def_coeff_reorder = vp6_il_coeff_reorder;
74 else
75 39 s->def_coeff_reorder = vp6_def_coeff_reorder;
76
3/4
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
41 if (separated_coeff || !s->filter_header) {
77 13 coeff_offset = AV_RB16(buf+2) - 2;
78 13 buf += 2;
79 13 buf_size -= 2;
80 }
81
82 41 rows = buf[2]; /* number of stored macroblock rows */
83 41 cols = buf[3]; /* number of stored macroblock cols */
84 /* buf[4] is number of displayed macroblock rows */
85 /* buf[5] is number of displayed macroblock cols */
86
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 41 times.
41 if (!rows || !cols) {
87 av_log(s->avctx, AV_LOG_ERROR, "Invalid size %dx%d\n", cols << 4, rows << 4);
88 return AVERROR_INVALIDDATA;
89 }
90
91
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 13 times.
41 if (!s->macroblocks || /* first frame */
92
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 16*cols != s->avctx->coded_width ||
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 16*rows != s->avctx->coded_height) {
94
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 2 times.
13 if (s->avctx->extradata_size == 0 &&
95
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5 times.
11 FFALIGN(s->avctx->width, 16) == 16 * cols &&
96
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 FFALIGN(s->avctx->height, 16) == 16 * rows) {
97 // We assume this is properly signalled container cropping,
98 // in an F4V file. Just set the coded_width/height, don't
99 // touch the cropped ones.
100 6 s->avctx->coded_width = 16 * cols;
101 6 s->avctx->coded_height = 16 * rows;
102 } else {
103 7 ret = ff_set_dimensions(s->avctx, 16 * cols, 16 * rows);
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ret < 0)
105 return ret;
106
107
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (s->avctx->extradata_size == 1) {
108 2 s->avctx->width -= s->avctx->extradata[0] >> 4;
109 2 s->avctx->height -= s->avctx->extradata[0] & 0x0F;
110 }
111 }
112 13 res = VP56_SIZE_CHANGE;
113 }
114
115 41 ret = ff_vpx_init_range_decoder(c, buf+6, buf_size-6);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 if (ret < 0)
117 goto fail;
118 41 vp56_rac_gets(c, 2);
119
120 41 parse_filter_info = s->filter_header;
121
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 25 times.
41 if (sub_version < 8)
122 16 vrt_shift = 5;
123 41 s->sub_version = sub_version;
124 41 s->golden_frame = 0;
125 } else {
126
3/6
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 977 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 977 times.
977 if (!s->sub_version || !s->avctx->coded_width || !s->avctx->coded_height)
127 return AVERROR_INVALIDDATA;
128
129
3/4
✓ Branch 0 taken 551 times.
✓ Branch 1 taken 426 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 551 times.
977 if (separated_coeff || !s->filter_header) {
130 426 coeff_offset = AV_RB16(buf+1) - 2;
131 426 buf += 2;
132 426 buf_size -= 2;
133 }
134 977 ret = ff_vpx_init_range_decoder(c, buf+1, buf_size-1);
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 977 times.
977 if (ret < 0)
136 return ret;
137
138 977 s->golden_frame = vpx_rac_get(c);
139
2/2
✓ Branch 0 taken 551 times.
✓ Branch 1 taken 426 times.
977 if (s->filter_header) {
140 551 s->deblock_filtering = vpx_rac_get(c);
141
1/2
✓ Branch 0 taken 551 times.
✗ Branch 1 not taken.
551 if (s->deblock_filtering)
142 551 vpx_rac_get(c);
143
2/2
✓ Branch 0 taken 429 times.
✓ Branch 1 taken 122 times.
551 if (s->sub_version > 7)
144 429 parse_filter_info = vpx_rac_get(c);
145 }
146 }
147
148
2/2
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 851 times.
1018 if (parse_filter_info) {
149
1/2
✓ Branch 1 taken 167 times.
✗ Branch 2 not taken.
167 if (vpx_rac_get(c)) {
150 167 s->filter_mode = 2;
151 167 s->sample_variance_threshold = vp56_rac_gets(c, 5) << vrt_shift;
152 167 s->max_vector_length = 2 << vp56_rac_gets(c, 3);
153 } else if (vpx_rac_get(c)) {
154 s->filter_mode = 1;
155 } else {
156 s->filter_mode = 0;
157 }
158
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 3 times.
167 if (s->sub_version > 7)
159 164 s->filter_selection = vp56_rac_gets(c, 4);
160 else
161 3 s->filter_selection = 16;
162 }
163
164 1018 s->use_huffman = vpx_rac_get(c);
165
166 1018 s->parse_coeff = vp6_parse_coeff;
167
2/2
✓ Branch 0 taken 439 times.
✓ Branch 1 taken 579 times.
1018 if (coeff_offset) {
168 439 buf += coeff_offset;
169 439 buf_size -= coeff_offset;
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 439 times.
439 if (buf_size < 0) {
171 ret = AVERROR_INVALIDDATA;
172 goto fail;
173 }
174
2/2
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 322 times.
439 if (s->use_huffman) {
175 117 s->parse_coeff = vp6_parse_coeff_huffman;
176 117 ret = init_get_bits8(&s->gb, buf, buf_size);
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117 times.
117 if (ret < 0)
178 return ret;
179 } else {
180 322 ret = ff_vpx_init_range_decoder(&s->cc, buf, buf_size);
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 322 times.
322 if (ret < 0)
182 goto fail;
183 322 s->ccp = &s->cc;
184 }
185 } else {
186 579 s->ccp = &s->c;
187 }
188
189 1018 return res;
190 fail:
191 if (res == VP56_SIZE_CHANGE)
192 ff_set_dimensions(s->avctx, 0, 0);
193 return ret;
194 }
195
196 344 static void vp6_coeff_order_table_init(VP56Context *s)
197 {
198 344 int i, pos, idx = 1;
199
200 344 s->modelp->coeff_index_to_pos[0] = 0;
201
2/2
✓ Branch 0 taken 5504 times.
✓ Branch 1 taken 344 times.
5848 for (i=0; i<16; i++)
202
2/2
✓ Branch 0 taken 346752 times.
✓ Branch 1 taken 5504 times.
352256 for (pos=1; pos<64; pos++)
203
2/2
✓ Branch 0 taken 21672 times.
✓ Branch 1 taken 325080 times.
346752 if (s->modelp->coeff_reorder[pos] == i)
204 21672 s->modelp->coeff_index_to_pos[idx++] = pos;
205
206
2/2
✓ Branch 0 taken 22016 times.
✓ Branch 1 taken 344 times.
22360 for (idx = 0; idx < 64; idx++) {
207 22016 int max = 0;
208
2/2
✓ Branch 0 taken 715520 times.
✓ Branch 1 taken 22016 times.
737536 for (i = 0; i <= idx; i++) {
209 715520 int v = s->modelp->coeff_index_to_pos[i];
210
2/2
✓ Branch 0 taken 257658 times.
✓ Branch 1 taken 457862 times.
715520 if (v > max)
211 257658 max = v;
212 }
213
2/2
✓ Branch 0 taken 2560 times.
✓ Branch 1 taken 19456 times.
22016 if (s->sub_version > 6)
214 2560 max++;
215 22016 s->modelp->coeff_index_to_idct_selector[idx] = max;
216 }
217 344 }
218
219 41 static void vp6_default_models_init(VP56Context *s)
220 {
221 41 VP56Model *model = s->modelp;
222
223 41 model->vector_dct[0] = 0xA2;
224 41 model->vector_dct[1] = 0xA4;
225 41 model->vector_sig[0] = 0x80;
226 41 model->vector_sig[1] = 0x80;
227
228 41 memcpy(model->mb_types_stats, ff_vp56_def_mb_types_stats, sizeof(model->mb_types_stats));
229 41 memcpy(model->vector_fdv, vp6_def_fdv_vector_model, sizeof(model->vector_fdv));
230 41 memcpy(model->vector_pdv, vp6_def_pdv_vector_model, sizeof(model->vector_pdv));
231 41 memcpy(model->coeff_runv, vp6_def_runv_coeff_model, sizeof(model->coeff_runv));
232 41 memcpy(model->coeff_reorder, s->def_coeff_reorder, sizeof(model->coeff_reorder));
233
234 41 vp6_coeff_order_table_init(s);
235 41 }
236
237 977 static void vp6_parse_vector_models(VP56Context *s)
238 {
239 977 VPXRangeCoder *c = &s->c;
240 977 VP56Model *model = s->modelp;
241 int comp, node;
242
243
2/2
✓ Branch 0 taken 1954 times.
✓ Branch 1 taken 977 times.
2931 for (comp=0; comp<2; comp++) {
244
2/2
✓ Branch 1 taken 109 times.
✓ Branch 2 taken 1845 times.
1954 if (vpx_rac_get_prob_branchy(c, vp6_sig_dct_pct[comp][0]))
245 109 model->vector_dct[comp] = vp56_rac_gets_nn(c, 7);
246
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 1937 times.
1954 if (vpx_rac_get_prob_branchy(c, vp6_sig_dct_pct[comp][1]))
247 17 model->vector_sig[comp] = vp56_rac_gets_nn(c, 7);
248 }
249
250
2/2
✓ Branch 0 taken 1954 times.
✓ Branch 1 taken 977 times.
2931 for (comp=0; comp<2; comp++)
251
2/2
✓ Branch 0 taken 13678 times.
✓ Branch 1 taken 1954 times.
15632 for (node=0; node<7; node++)
252
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 13654 times.
13678 if (vpx_rac_get_prob_branchy(c, vp6_pdv_pct[comp][node]))
253 24 model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
254
255
2/2
✓ Branch 0 taken 1954 times.
✓ Branch 1 taken 977 times.
2931 for (comp=0; comp<2; comp++)
256
2/2
✓ Branch 0 taken 15632 times.
✓ Branch 1 taken 1954 times.
17586 for (node=0; node<8; node++)
257
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 15622 times.
15632 if (vpx_rac_get_prob_branchy(c, vp6_fdv_pct[comp][node]))
258 10 model->vector_fdv[comp][node] = vp56_rac_gets_nn(c, 7);
259 977 }
260
261 /* nodes must ascend by count, but with descending symbol order */
262 149929 static int vp6_huff_cmp(const void *va, const void *vb)
263 {
264 149929 const Node *a = va, *b = vb;
265 149929 return (a->count - b->count)*16 + (b->sym - a->sym);
266 }
267
268 4680 static int vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[],
269 const uint8_t *map, unsigned size, VLC *vlc)
270 {
271 4680 Node nodes[2*VP6_MAX_HUFF_SIZE], *tmp = &nodes[size];
272 int a, b, i;
273
274 /* first compute probabilities from model */
275 4680 tmp[0].count = 256;
276
2/2
✓ Branch 0 taken 50778 times.
✓ Branch 1 taken 4680 times.
55458 for (i=0; i<size-1; i++) {
277 50778 a = tmp[i].count * coeff_model[i] >> 8;
278 50778 b = tmp[i].count * (255 - coeff_model[i]) >> 8;
279 50778 nodes[map[2*i ]].count = a + !a;
280 50778 nodes[map[2*i+1]].count = b + !b;
281 }
282
283 4680 ff_vlc_free(vlc);
284 /* then build the huffman tree according to probabilities */
285 4680 return ff_huff_build_tree(s->avctx, vlc, size, FF_HUFFMAN_BITS,
286 nodes, vp6_huff_cmp,
287 FF_HUFFMAN_FLAG_HNODE_FIRST);
288 }
289
290 1018 static int vp6_parse_coeff_models(VP56Context *s)
291 {
292 1018 VPXRangeCoder *c = &s->c;
293 1018 VP56Model *model = s->modelp;
294 int def_prob[11];
295 int node, cg, ctx, pos;
296 int ct; /* code type */
297 int pt; /* plane type (0 for Y, 1 for U or V) */
298
299 1018 memset(def_prob, 0x80, sizeof(def_prob));
300
301
2/2
✓ Branch 0 taken 2036 times.
✓ Branch 1 taken 1018 times.
3054 for (pt=0; pt<2; pt++)
302
2/2
✓ Branch 0 taken 22396 times.
✓ Branch 1 taken 2036 times.
24432 for (node=0; node<11; node++)
303
2/2
✓ Branch 1 taken 1216 times.
✓ Branch 2 taken 21180 times.
22396 if (vpx_rac_get_prob_branchy(c, vp6_dccv_pct[pt][node])) {
304 1216 def_prob[node] = vp56_rac_gets_nn(c, 7);
305 1216 model->coeff_dccv[pt][node] = def_prob[node];
306
2/2
✓ Branch 0 taken 601 times.
✓ Branch 1 taken 20579 times.
21180 } else if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
307 601 model->coeff_dccv[pt][node] = def_prob[node];
308 }
309
310
2/2
✓ Branch 1 taken 303 times.
✓ Branch 2 taken 715 times.
1018 if (vpx_rac_get(c)) {
311
2/2
✓ Branch 0 taken 19089 times.
✓ Branch 1 taken 303 times.
19392 for (pos=1; pos<64; pos++)
312
2/2
✓ Branch 1 taken 15976 times.
✓ Branch 2 taken 3113 times.
19089 if (vpx_rac_get_prob_branchy(c, vp6_coeff_reorder_pct[pos]))
313 15976 model->coeff_reorder[pos] = vp56_rac_gets(c, 4);
314 303 vp6_coeff_order_table_init(s);
315 }
316
317
2/2
✓ Branch 0 taken 2036 times.
✓ Branch 1 taken 1018 times.
3054 for (cg=0; cg<2; cg++)
318
2/2
✓ Branch 0 taken 28504 times.
✓ Branch 1 taken 2036 times.
30540 for (node=0; node<14; node++)
319
2/2
✓ Branch 1 taken 887 times.
✓ Branch 2 taken 27617 times.
28504 if (vpx_rac_get_prob_branchy(c, vp6_runv_pct[cg][node]))
320 887 model->coeff_runv[cg][node] = vp56_rac_gets_nn(c, 7);
321
322
2/2
✓ Branch 0 taken 3054 times.
✓ Branch 1 taken 1018 times.
4072 for (ct=0; ct<3; ct++)
323
2/2
✓ Branch 0 taken 6108 times.
✓ Branch 1 taken 3054 times.
9162 for (pt=0; pt<2; pt++)
324
2/2
✓ Branch 0 taken 36648 times.
✓ Branch 1 taken 6108 times.
42756 for (cg=0; cg<6; cg++)
325
2/2
✓ Branch 0 taken 403128 times.
✓ Branch 1 taken 36648 times.
439776 for (node=0; node<11; node++)
326
2/2
✓ Branch 1 taken 5427 times.
✓ Branch 2 taken 397701 times.
403128 if (vpx_rac_get_prob_branchy(c, vp6_ract_pct[ct][pt][cg][node])) {
327 5427 def_prob[node] = vp56_rac_gets_nn(c, 7);
328 5427 model->coeff_ract[pt][ct][cg][node] = def_prob[node];
329
2/2
✓ Branch 0 taken 14606 times.
✓ Branch 1 taken 383095 times.
397701 } else if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
330 14606 model->coeff_ract[pt][ct][cg][node] = def_prob[node];
331 }
332
333
2/2
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 901 times.
1018 if (s->use_huffman) {
334
2/2
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 117 times.
351 for (pt=0; pt<2; pt++) {
335
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 234 times.
234 if (vp6_build_huff_tree(s, model->coeff_dccv[pt],
336 vp6_huff_coeff_map, 12, &s->dccv_vlc[pt]))
337 return -1;
338
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 234 times.
234 if (vp6_build_huff_tree(s, model->coeff_runv[pt],
339 vp6_huff_run_map, 9, &s->runv_vlc[pt]))
340 return -1;
341
2/2
✓ Branch 0 taken 702 times.
✓ Branch 1 taken 234 times.
936 for (ct=0; ct<3; ct++)
342
2/2
✓ Branch 0 taken 4212 times.
✓ Branch 1 taken 702 times.
4914 for (cg = 0; cg < 6; cg++)
343
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4212 times.
4212 if (vp6_build_huff_tree(s, model->coeff_ract[pt][ct][cg],
344 vp6_huff_coeff_map, 12,
345 &s->ract_vlc[pt][ct][cg]))
346 return -1;
347 }
348 117 memset(s->nb_null, 0, sizeof(s->nb_null));
349 } else {
350 /* coeff_dcct is a linear combination of coeff_dccv */
351
2/2
✓ Branch 0 taken 1802 times.
✓ Branch 1 taken 901 times.
2703 for (pt=0; pt<2; pt++)
352
2/2
✓ Branch 0 taken 5406 times.
✓ Branch 1 taken 1802 times.
7208 for (ctx=0; ctx<3; ctx++)
353
2/2
✓ Branch 0 taken 27030 times.
✓ Branch 1 taken 5406 times.
32436 for (node=0; node<5; node++)
354 27030 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);
355 }
356 1018 return 0;
357 }
358
359 19437 static void vp6_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
360 {
361 19437 VPXRangeCoder *c = &s->c;
362 19437 VP56Model *model = s->modelp;
363 int comp;
364
365 19437 *vect = (VP56mv) {0,0};
366
2/2
✓ Branch 0 taken 16654 times.
✓ Branch 1 taken 2783 times.
19437 if (s->vector_candidate_pos < 2)
367 16654 *vect = s->vector_candidate[0];
368
369
2/2
✓ Branch 0 taken 38874 times.
✓ Branch 1 taken 19437 times.
58311 for (comp=0; comp<2; comp++) {
370 38874 int i, delta = 0;
371
372
2/2
✓ Branch 1 taken 8755 times.
✓ Branch 2 taken 30119 times.
38874 if (vpx_rac_get_prob_branchy(c, model->vector_dct[comp])) {
373 static const uint8_t prob_order[] = {0, 1, 2, 7, 6, 5, 4};
374
2/2
✓ Branch 0 taken 61285 times.
✓ Branch 1 taken 8755 times.
70040 for (i=0; i<sizeof(prob_order); i++) {
375 61285 int j = prob_order[i];
376 61285 delta |= vpx_rac_get_prob(c, model->vector_fdv[comp][j])<<j;
377 }
378
2/2
✓ Branch 0 taken 4348 times.
✓ Branch 1 taken 4407 times.
8755 if (delta & 0xF0)
379 4348 delta |= vpx_rac_get_prob(c, model->vector_fdv[comp][3])<<3;
380 else
381 4407 delta |= 8;
382 } else {
383 30119 delta = vp56_rac_get_tree(c, ff_vp56_pva_tree,
384 30119 model->vector_pdv[comp]);
385 }
386
387
4/4
✓ Branch 0 taken 29701 times.
✓ Branch 1 taken 9173 times.
✓ Branch 3 taken 14330 times.
✓ Branch 4 taken 15371 times.
38874 if (delta && vpx_rac_get_prob_branchy(c, model->vector_sig[comp]))
388 14330 delta = -delta;
389
390
2/2
✓ Branch 0 taken 19437 times.
✓ Branch 1 taken 19437 times.
38874 if (!comp)
391 19437 vect->x += delta;
392 else
393 19437 vect->y += delta;
394 }
395 19437 }
396
397 /**
398 * Read number of consecutive blocks with null DC or AC.
399 * This value is < 74.
400 */
401 28576 static unsigned vp6_get_nb_null(VP56Context *s)
402 {
403 28576 unsigned val = get_bits(&s->gb, 2);
404
2/2
✓ Branch 0 taken 4892 times.
✓ Branch 1 taken 23684 times.
28576 if (val == 2)
405 4892 val += get_bits(&s->gb, 2);
406
2/2
✓ Branch 0 taken 806 times.
✓ Branch 1 taken 22878 times.
23684 else if (val == 3) {
407 806 val = get_bits1(&s->gb) << 2;
408 806 val = 6+val + get_bits(&s->gb, 2+val);
409 }
410 28576 return val;
411 }
412
413 16848 static int vp6_parse_coeff_huffman(VP56Context *s)
414 {
415 16848 VP56Model *model = s->modelp;
416 16848 uint8_t *permute = s->idct_scantable;
417 VLC *vlc_coeff;
418 int coeff, sign, coeff_idx;
419 int b, cg, idx;
420 16848 int pt = 0; /* plane type (0 for Y, 1 for U or V) */
421
422
2/2
✓ Branch 0 taken 101088 times.
✓ Branch 1 taken 16848 times.
117936 for (b=0; b<6; b++) {
423 101088 int ct = 0; /* code type */
424
2/2
✓ Branch 0 taken 33696 times.
✓ Branch 1 taken 67392 times.
101088 if (b > 3) pt = 1;
425 101088 vlc_coeff = &s->dccv_vlc[pt];
426
427 101088 for (coeff_idx = 0;;) {
428 1161120 int run = 1;
429
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]) {
430 28871 s->nb_null[coeff_idx][pt]--;
431
2/2
✓ Branch 0 taken 11362 times.
✓ Branch 1 taken 17509 times.
28871 if (coeff_idx)
432 11362 break;
433 } else {
434
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1132249 times.
1132249 if (get_bits_left(&s->gb) <= 0)
435 return AVERROR_INVALIDDATA;
436 1132249 coeff = get_vlc2(&s->gb, vlc_coeff->table, FF_HUFFMAN_BITS, 3);
437
2/2
✓ Branch 0 taken 345601 times.
✓ Branch 1 taken 786648 times.
1132249 if (coeff == 0) {
438
2/2
✓ Branch 0 taken 326823 times.
✓ Branch 1 taken 18778 times.
345601 if (coeff_idx) {
439 326823 int pt = (coeff_idx >= 6);
440 326823 run += get_vlc2(&s->gb, s->runv_vlc[pt].table, FF_HUFFMAN_BITS, 3);
441
2/2
✓ Branch 0 taken 60162 times.
✓ Branch 1 taken 266661 times.
326823 if (run >= 9)
442 60162 run += get_bits(&s->gb, 6);
443 } else
444 18778 s->nb_null[0][pt] = vp6_get_nb_null(s);
445 345601 ct = 0;
446
2/2
✓ Branch 0 taken 88872 times.
✓ Branch 1 taken 697776 times.
786648 } else if (coeff == 11) { /* end of block */
447
2/2
✓ Branch 0 taken 9798 times.
✓ Branch 1 taken 79074 times.
88872 if (coeff_idx == 1) /* first AC coeff ? */
448 9798 s->nb_null[1][pt] = vp6_get_nb_null(s);
449 88872 break;
450 } else {
451 697776 int coeff2 = ff_vp56_coeff_bias[coeff];
452
2/2
✓ Branch 0 taken 39048 times.
✓ Branch 1 taken 658728 times.
697776 if (coeff > 4)
453
2/2
✓ Branch 0 taken 38623 times.
✓ Branch 1 taken 425 times.
39048 coeff2 += get_bits(&s->gb, coeff <= 9 ? coeff - 4 : 11);
454
2/2
✓ Branch 0 taken 188610 times.
✓ Branch 1 taken 509166 times.
697776 ct = 1 + (coeff2 > 1);
455 697776 sign = get_bits1(&s->gb);
456 697776 coeff2 = (coeff2 ^ -sign) + sign;
457
2/2
✓ Branch 0 taken 632975 times.
✓ Branch 1 taken 64801 times.
697776 if (coeff_idx)
458 632975 coeff2 *= s->dequant_ac;
459 697776 idx = model->coeff_index_to_pos[coeff_idx];
460 697776 s->block_coeff[b][permute[idx]] = coeff2;
461 }
462 }
463 1060886 coeff_idx+=run;
464
2/2
✓ Branch 0 taken 854 times.
✓ Branch 1 taken 1060032 times.
1060886 if (coeff_idx >= 64)
465 854 break;
466 1060032 cg = FFMIN(vp6_coeff_groups[coeff_idx], 3);
467 1060032 vlc_coeff = &s->ract_vlc[pt][ct][cg];
468 }
469 101088 s->idct_selector[b] = model->coeff_index_to_idct_selector[FFMIN(coeff_idx, 63)];
470 }
471 16848 return 0;
472 }
473
474 80666 static int vp6_parse_coeff(VP56Context *s)
475 {
476 80666 VPXRangeCoder *c = s->ccp;
477 80666 VP56Model *model = s->modelp;
478 80666 uint8_t *permute = s->idct_scantable;
479 uint8_t *model1, *model2, *model3;
480 int coeff, sign, coeff_idx;
481 int b, i, cg, idx, ctx;
482 80666 int pt = 0; /* plane type (0 for Y, 1 for U or V) */
483
484
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 80666 times.
80666 if (vpx_rac_is_end(c)) {
485 av_log(s->avctx, AV_LOG_ERROR, "End of AC stream reached in vp6_parse_coeff\n");
486 return AVERROR_INVALIDDATA;
487 }
488
489
2/2
✓ Branch 0 taken 483996 times.
✓ Branch 1 taken 80666 times.
564662 for (b=0; b<6; b++) {
490 483996 int ct = 1; /* code type */
491 483996 int run = 1;
492
493
2/2
✓ Branch 0 taken 161332 times.
✓ Branch 1 taken 322664 times.
483996 if (b > 3) pt = 1;
494
495 483996 ctx = s->left_block[ff_vp56_b6to4[b]].not_null_dc
496 483996 + s->above_blocks[s->above_block_idx[b]].not_null_dc;
497 483996 model1 = model->coeff_dccv[pt];
498 483996 model2 = model->coeff_dcct[pt][ctx];
499
500 483996 coeff_idx = 0;
501 for (;;) {
502
6/6
✓ Branch 0 taken 1171525 times.
✓ Branch 1 taken 967992 times.
✓ Branch 2 taken 799185 times.
✓ Branch 3 taken 372340 times.
✓ Branch 5 taken 514322 times.
✓ Branch 6 taken 1252855 times.
2139517 if ((coeff_idx>1 && ct==0) || vpx_rac_get_prob_branchy(c, model2[0])) {
503 /* parse a coeff */
504
2/2
✓ Branch 1 taken 250960 times.
✓ Branch 2 taken 635702 times.
886662 if (vpx_rac_get_prob_branchy(c, model2[2])) {
505
2/2
✓ Branch 1 taken 48252 times.
✓ Branch 2 taken 202708 times.
250960 if (vpx_rac_get_prob_branchy(c, model2[3])) {
506 48252 idx = vp56_rac_get_tree(c, ff_vp56_pc_tree, model1);
507 48252 coeff = ff_vp56_coeff_bias[idx+5];
508
2/2
✓ Branch 0 taken 96960 times.
✓ Branch 1 taken 48252 times.
145212 for (i=ff_vp56_coeff_bit_length[idx]; i>=0; i--)
509 96960 coeff += vpx_rac_get_prob(c, ff_vp56_coeff_parse_table[idx][i]) << i;
510 } else {
511
2/2
✓ Branch 1 taken 69904 times.
✓ Branch 2 taken 132804 times.
202708 if (vpx_rac_get_prob_branchy(c, model2[4]))
512 69904 coeff = 3 + vpx_rac_get_prob(c, model1[5]);
513 else
514 132804 coeff = 2;
515 }
516 250960 ct = 2;
517 } else {
518 635702 ct = 1;
519 635702 coeff = 1;
520 }
521 886662 sign = vpx_rac_get(c);
522 886662 coeff = (coeff ^ -sign) + sign;
523
2/2
✓ Branch 0 taken 801605 times.
✓ Branch 1 taken 85057 times.
886662 if (coeff_idx)
524 801605 coeff *= s->dequant_ac;
525 886662 idx = model->coeff_index_to_pos[coeff_idx];
526 886662 s->block_coeff[b][permute[idx]] = coeff;
527 886662 run = 1;
528 } else {
529 /* parse a run */
530 1252855 ct = 0;
531
2/2
✓ Branch 0 taken 853916 times.
✓ Branch 1 taken 398939 times.
1252855 if (coeff_idx > 0) {
532
2/2
✓ Branch 1 taken 481576 times.
✓ Branch 2 taken 372340 times.
853916 if (!vpx_rac_get_prob_branchy(c, model2[1]))
533 481576 break;
534
535 372340 model3 = model->coeff_runv[coeff_idx >= 6];
536 372340 run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
537
2/2
✓ Branch 0 taken 52959 times.
✓ Branch 1 taken 319381 times.
372340 if (!run)
538
2/2
✓ Branch 0 taken 317754 times.
✓ Branch 1 taken 52959 times.
370713 for (run=9, i=0; i<6; i++)
539 317754 run += vpx_rac_get_prob(c, model3[i+8]) << i;
540 }
541 }
542 1657941 coeff_idx += run;
543
2/2
✓ Branch 0 taken 2420 times.
✓ Branch 1 taken 1655521 times.
1657941 if (coeff_idx >= 64)
544 2420 break;
545 1655521 cg = vp6_coeff_groups[coeff_idx];
546 1655521 model1 = model2 = model->coeff_ract[pt][ct][cg];
547 }
548
549 483996 s->left_block[ff_vp56_b6to4[b]].not_null_dc =
550 483996 s->above_blocks[s->above_block_idx[b]].not_null_dc = !!s->block_coeff[b][0];
551 483996 s->idct_selector[b] = model->coeff_index_to_idct_selector[FFMIN(coeff_idx, 63)];
552 }
553 80666 return 0;
554 }
555
556 85227 static int vp6_block_variance(uint8_t *src, ptrdiff_t stride)
557 {
558 85227 int sum = 0, square_sum = 0;
559 int y, x;
560
561
2/2
✓ Branch 0 taken 340908 times.
✓ Branch 1 taken 85227 times.
426135 for (y=0; y<8; y+=2) {
562
2/2
✓ Branch 0 taken 1363632 times.
✓ Branch 1 taken 340908 times.
1704540 for (x=0; x<8; x+=2) {
563 1363632 sum += src[x];
564 1363632 square_sum += src[x]*src[x];
565 }
566 340908 src += 2*stride;
567 }
568 85227 return (16*square_sum - sum*sum) >> 8;
569 }
570
571 32865 static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, ptrdiff_t stride,
572 int delta, const int16_t *weights)
573 {
574 int x, y;
575
576
2/2
✓ Branch 0 taken 262920 times.
✓ Branch 1 taken 32865 times.
295785 for (y=0; y<8; y++) {
577
2/2
✓ Branch 0 taken 2103360 times.
✓ Branch 1 taken 262920 times.
2366280 for (x=0; x<8; x++) {
578 2103360 dst[x] = av_clip_uint8(( src[x-delta ] * weights[0]
579 2103360 + src[x ] * weights[1]
580 2103360 + src[x+delta ] * weights[2]
581 2103360 + src[x+2*delta] * weights[3] + 64) >> 7);
582 }
583 262920 src += stride;
584 262920 dst += stride;
585 }
586 32865 }
587
588 54829 static void vp6_filter_diag2(VP56Context *s, uint8_t *dst, uint8_t *src,
589 ptrdiff_t stride, int h_weight, int v_weight)
590 {
591 54829 uint8_t *tmp = s->edge_emu_buffer+16;
592 54829 s->h264chroma.put_h264_chroma_pixels_tab[0](tmp, src, stride, 9, h_weight, 0);
593 54829 s->h264chroma.put_h264_chroma_pixels_tab[0](dst, tmp, stride, 8, 0, v_weight);
594 54829 }
595
596 177939 static void vp6_filter(VP56Context *s, uint8_t *dst, uint8_t *src,
597 int offset1, int offset2, ptrdiff_t stride,
598 VP56mv mv, int mask, int select, int luma)
599 {
600 177939 int filter4 = 0;
601 177939 int x8 = mv.x & mask;
602 177939 int y8 = mv.y & mask;
603
604
2/2
✓ Branch 0 taken 120449 times.
✓ Branch 1 taken 57490 times.
177939 if (luma) {
605 120449 x8 *= 2;
606 120449 y8 *= 2;
607 120449 filter4 = s->filter_mode;
608
2/2
✓ Branch 0 taken 105652 times.
✓ Branch 1 taken 14797 times.
120449 if (filter4 == 2) {
609
1/2
✓ Branch 0 taken 105652 times.
✗ Branch 1 not taken.
105652 if (s->max_vector_length &&
610
2/2
✓ Branch 0 taken 92887 times.
✓ Branch 1 taken 12765 times.
105652 (FFABS(mv.x) > s->max_vector_length ||
611
2/2
✓ Branch 0 taken 7660 times.
✓ Branch 1 taken 85227 times.
92887 FFABS(mv.y) > s->max_vector_length)) {
612 20425 filter4 = 0;
613
1/2
✓ Branch 0 taken 85227 times.
✗ Branch 1 not taken.
85227 } else if (s->sample_variance_threshold
614 85227 && (vp6_block_variance(src+offset1, stride)
615
2/2
✓ Branch 0 taken 25016 times.
✓ Branch 1 taken 60211 times.
85227 < s->sample_variance_threshold)) {
616 25016 filter4 = 0;
617 }
618 }
619 }
620
621
8/8
✓ Branch 0 taken 136552 times.
✓ Branch 1 taken 41387 times.
✓ Branch 2 taken 61409 times.
✓ Branch 3 taken 75143 times.
✓ Branch 4 taken 41387 times.
✓ Branch 5 taken 61409 times.
✓ Branch 6 taken 15077 times.
✓ Branch 7 taken 26310 times.
177939 if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
622 90220 offset1 = offset2;
623 }
624
625
2/2
✓ Branch 0 taken 60211 times.
✓ Branch 1 taken 117728 times.
177939 if (filter4) {
626
2/2
✓ Branch 0 taken 12003 times.
✓ Branch 1 taken 48208 times.
60211 if (!y8) { /* left or right combine */
627 12003 vp6_filter_hv4(dst, src+offset1, stride, 1,
628 12003 vp6_block_copy_filter[select][x8]);
629
2/2
✓ Branch 0 taken 20862 times.
✓ Branch 1 taken 27346 times.
48208 } else if (!x8) { /* above or below combine */
630 20862 vp6_filter_hv4(dst, src+offset1, stride, stride,
631 20862 vp6_block_copy_filter[select][y8]);
632 } else {
633 27346 s->vp56dsp.vp6_filter_diag4(dst, src+offset1+((mv.x^mv.y)>>31), stride,
634 27346 vp6_block_copy_filter[select][x8],
635 27346 vp6_block_copy_filter[select][y8]);
636 }
637 } else {
638
4/4
✓ Branch 0 taken 84213 times.
✓ Branch 1 taken 33515 times.
✓ Branch 2 taken 29384 times.
✓ Branch 3 taken 54829 times.
117728 if (!x8 || !y8) {
639 62899 s->h264chroma.put_h264_chroma_pixels_tab[0](dst, src + offset1, stride, 8, x8, y8);
640 } else {
641 54829 vp6_filter_diag2(s, dst, src+offset1 + ((mv.x^mv.y)>>31), stride, x8, y8);
642 }
643 }
644 177939 }
645
646 21 static av_cold int vp6_decode_init_context(AVCodecContext *avctx,
647 VP56Context *s, int flip, int has_alpha)
648 {
649 21 int ret = ff_vp56_init_context(avctx, s, flip, has_alpha);
650
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0)
651 return ret;
652
653 21 ff_vp6dsp_init(&s->vp56dsp);
654
655 21 s->deblock_filtering = 0;
656 21 s->vp56_coord_div = vp6_coord_div;
657 21 s->parse_vector_adjustment = vp6_parse_vector_adjustment;
658 21 s->filter = vp6_filter;
659 21 s->default_models_init = vp6_default_models_init;
660 21 s->parse_vector_models = vp6_parse_vector_models;
661 21 s->parse_coeff_models = vp6_parse_coeff_models;
662 21 s->parse_header = vp6_parse_header;
663
664 21 return 0;
665 }
666
667 17 static av_cold int vp6_decode_init(AVCodecContext *avctx)
668 {
669 17 VP56Context *s = avctx->priv_data;
670 int ret;
671
672 17 ret = vp6_decode_init_context(avctx, s, avctx->codec_id == AV_CODEC_ID_VP6,
673 17 avctx->codec_id == AV_CODEC_ID_VP6A);
674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ret < 0)
675 return ret;
676
677
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 13 times.
17 if (s->has_alpha) {
678 /* Can only happen for ff_vp6a_decoder */
679 4 s->alpha_context = &s[1];
680 4 ret = vp6_decode_init_context(avctx, s->alpha_context,
681 4 s->flip == -1, s->has_alpha);
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
683 return ret;
684 }
685
686 17 return 0;
687 }
688
689 static av_cold void vp6_decode_free_context(VP56Context *s);
690
691 17 static av_cold int vp6_decode_free(AVCodecContext *avctx)
692 {
693 17 VP56Context *s = avctx->priv_data;
694
695 17 vp6_decode_free_context(s);
696
697
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 13 times.
17 if (s->alpha_context) {
698 4 vp6_decode_free_context(s->alpha_context);
699 4 s->alpha_context = NULL;
700 }
701
702 17 return 0;
703 }
704
705 21 static av_cold void vp6_decode_free_context(VP56Context *s)
706 {
707 int pt, ct, cg;
708
709 21 ff_vp56_free_context(s);
710
711
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 21 times.
63 for (pt=0; pt<2; pt++) {
712 42 ff_vlc_free(&s->dccv_vlc[pt]);
713 42 ff_vlc_free(&s->runv_vlc[pt]);
714
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 42 times.
168 for (ct=0; ct<3; ct++)
715
2/2
✓ Branch 0 taken 756 times.
✓ Branch 1 taken 126 times.
882 for (cg=0; cg<6; cg++)
716 756 ff_vlc_free(&s->ract_vlc[pt][ct][cg]);
717 }
718 21 }
719
720 const FFCodec ff_vp6_decoder = {
721 .p.name = "vp6",
722 CODEC_LONG_NAME("On2 VP6"),
723 .p.type = AVMEDIA_TYPE_VIDEO,
724 .p.id = AV_CODEC_ID_VP6,
725 .priv_data_size = sizeof(VP56Context),
726 .init = vp6_decode_init,
727 .close = vp6_decode_free,
728 FF_CODEC_DECODE_CB(ff_vp56_decode_frame),
729 .p.capabilities = AV_CODEC_CAP_DR1,
730 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
731 };
732
733 /* flash version, not flipped upside-down */
734 const FFCodec ff_vp6f_decoder = {
735 .p.name = "vp6f",
736 CODEC_LONG_NAME("On2 VP6 (Flash version)"),
737 .p.type = AVMEDIA_TYPE_VIDEO,
738 .p.id = AV_CODEC_ID_VP6F,
739 .priv_data_size = sizeof(VP56Context),
740 .init = vp6_decode_init,
741 .close = vp6_decode_free,
742 FF_CODEC_DECODE_CB(ff_vp56_decode_frame),
743 .p.capabilities = AV_CODEC_CAP_DR1,
744 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
745 };
746
747 /* flash version, not flipped upside-down, with alpha channel */
748 const FFCodec ff_vp6a_decoder = {
749 .p.name = "vp6a",
750 CODEC_LONG_NAME("On2 VP6 (Flash version, with alpha channel)"),
751 .p.type = AVMEDIA_TYPE_VIDEO,
752 .p.id = AV_CODEC_ID_VP6A,
753 .priv_data_size = 2 /* Main context + alpha context */ * sizeof(VP56Context),
754 .init = vp6_decode_init,
755 .close = vp6_decode_free,
756 FF_CODEC_DECODE_CB(ff_vp56_decode_frame),
757 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
758 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
759 };
760