Coverage Report

Created: 2026-04-01 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/vp6.c
Line
Count
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.9M
#define AC_DC_HUFF_BITS   10
45
16.4M
#define RUN_HUFF_BITS      8
46
47
static int vp6_parse_coeff(VP56Context *s);
48
static int vp6_parse_coeff_huffman(VP56Context *s);
49
50
static int vp6_parse_header(VP56Context *s, const uint8_t *buf, int buf_size)
51
335k
{
52
335k
    VPXRangeCoder *c = &s->c;
53
335k
    int parse_filter_info = 0;
54
335k
    int coeff_offset = 0;
55
335k
    int vrt_shift = 0;
56
335k
    int sub_version;
57
335k
    int rows, cols;
58
335k
    int res = 0;
59
335k
    int ret;
60
335k
    int separated_coeff = buf[0] & 1;
61
62
335k
    if (!(buf[0] & 0x80))
63
56.6k
        s->frames[VP56_FRAME_CURRENT]->flags |= AV_FRAME_FLAG_KEY;
64
278k
    else
65
278k
        s->frames[VP56_FRAME_CURRENT]->flags &= ~AV_FRAME_FLAG_KEY;
66
335k
    ff_vp56_init_dequant(s, (buf[0] >> 1) & 0x3F);
67
68
335k
    if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
69
56.6k
        sub_version = buf[1] >> 3;
70
56.6k
        if (sub_version > 8)
71
1.68k
            return AVERROR_INVALIDDATA;
72
55.0k
        s->filter_header = buf[1] & 0x06;
73
55.0k
        s->interlaced = buf[1] & 1;
74
55.0k
        if (s->interlaced)
75
3.86k
            s->def_coeff_reorder = vp6_il_coeff_reorder;
76
51.1k
        else
77
51.1k
            s->def_coeff_reorder = vp6_def_coeff_reorder;
78
55.0k
        if (separated_coeff || !s->filter_header) {
79
19.5k
            coeff_offset = AV_RB16(buf+2) - 2;
80
19.5k
            buf += 2;
81
19.5k
            buf_size -= 2;
82
19.5k
        }
83
84
55.0k
        rows = buf[2];  /* number of stored macroblock rows */
85
55.0k
        cols = buf[3];  /* number of stored macroblock cols */
86
        /* buf[4] is number of displayed macroblock rows */
87
        /* buf[5] is number of displayed macroblock cols */
88
55.0k
        if (!rows || !cols) {
89
16.0k
            av_log(s->avctx, AV_LOG_ERROR, "Invalid size %dx%d\n", cols << 4, rows << 4);
90
16.0k
            return AVERROR_INVALIDDATA;
91
16.0k
        }
92
93
38.9k
        if (!s->macroblocks || /* first frame */
94
26.3k
            16*cols != s->avctx->coded_width ||
95
28.7k
            16*rows != s->avctx->coded_height) {
96
28.7k
            if (s->avctx->extradata_size == 0 &&
97
26.9k
                FFALIGN(s->avctx->width,  16) == 16 * cols &&
98
7.53k
                FFALIGN(s->avctx->height, 16) == 16 * rows) {
99
                // We assume this is properly signalled container cropping,
100
                // in an F4V file. Just set the coded_width/height, don't
101
                // touch the cropped ones.
102
7
                s->avctx->coded_width  = 16 * cols;
103
7
                s->avctx->coded_height = 16 * rows;
104
28.7k
            } else {
105
28.7k
                ret = ff_set_dimensions(s->avctx, 16 * cols, 16 * rows);
106
28.7k
                if (ret < 0)
107
0
                    return ret;
108
109
28.7k
                if (s->avctx->extradata_size == 1) {
110
820
                    s->avctx->width  -= s->avctx->extradata[0] >> 4;
111
820
                    s->avctx->height -= s->avctx->extradata[0] & 0x0F;
112
820
                }
113
28.7k
            }
114
28.7k
            res = VP56_SIZE_CHANGE;
115
28.7k
        }
116
117
38.9k
        ret = ff_vpx_init_range_decoder(c, buf+6, buf_size-6);
118
38.9k
        if (ret < 0)
119
3.07k
            goto fail;
120
35.8k
        vp56_rac_gets(c, 2);
121
122
35.8k
        parse_filter_info = s->filter_header;
123
35.8k
        if (sub_version < 8)
124
21.6k
            vrt_shift = 5;
125
35.8k
        s->sub_version = sub_version;
126
35.8k
        s->golden_frame = 0;
127
278k
    } else {
128
278k
        if (!s->sub_version || !s->avctx->coded_width || !s->avctx->coded_height)
129
5.56k
            return AVERROR_INVALIDDATA;
130
131
273k
        if (separated_coeff || !s->filter_header) {
132
24.1k
            coeff_offset = AV_RB16(buf+1) - 2;
133
24.1k
            buf += 2;
134
24.1k
            buf_size -= 2;
135
24.1k
        }
136
273k
        ret = ff_vpx_init_range_decoder(c, buf+1, buf_size-1);
137
273k
        if (ret < 0)
138
5.66k
            return ret;
139
140
267k
        s->golden_frame = vpx_rac_get(c);
141
267k
        if (s->filter_header) {
142
261k
            s->deblock_filtering = vpx_rac_get(c);
143
261k
            if (s->deblock_filtering)
144
200k
                vpx_rac_get(c);
145
261k
            if (s->sub_version > 7)
146
145k
                parse_filter_info = vpx_rac_get(c);
147
261k
        }
148
267k
    }
149
150
303k
    if (parse_filter_info) {
151
123k
        if (vpx_rac_get(c)) {
152
65.9k
            s->filter_mode = 2;
153
65.9k
            s->sample_variance_threshold = vp56_rac_gets(c, 5) << vrt_shift;
154
65.9k
            s->max_vector_length = 2 << vp56_rac_gets(c, 3);
155
65.9k
        } else if (vpx_rac_get(c)) {
156
42.6k
            s->filter_mode = 1;
157
42.6k
        } else {
158
14.6k
            s->filter_mode = 0;
159
14.6k
        }
160
123k
        if (s->sub_version > 7)
161
104k
            s->filter_selection = vp56_rac_gets(c, 4);
162
19.1k
        else
163
19.1k
            s->filter_selection = 16;
164
123k
    }
165
166
303k
    s->use_huffman = vpx_rac_get(c);
167
168
303k
    s->parse_coeff = vp6_parse_coeff;
169
303k
    if (coeff_offset) {
170
24.1k
        buf      += coeff_offset;
171
24.1k
        buf_size -= coeff_offset;
172
24.1k
        if (buf_size < 0) {
173
2.63k
            ret = AVERROR_INVALIDDATA;
174
2.63k
            goto fail;
175
2.63k
        }
176
21.5k
        if (s->use_huffman) {
177
12.1k
            s->parse_coeff = vp6_parse_coeff_huffman;
178
12.1k
            ret = init_get_bits8(&s->gb, buf, buf_size);
179
12.1k
            if (ret < 0)
180
0
                return ret;
181
12.1k
        } else {
182
9.41k
            ret = ff_vpx_init_range_decoder(&s->cc, buf, buf_size);
183
9.41k
            if (ret < 0)
184
803
                goto fail;
185
8.61k
            s->ccp = &s->cc;
186
8.61k
        }
187
279k
    } else {
188
279k
        s->ccp = &s->c;
189
279k
    }
190
191
299k
    return res;
192
6.51k
fail:
193
6.51k
    if (res == VP56_SIZE_CHANGE)
194
3.92k
        ff_set_dimensions(s->avctx, 0, 0);
195
6.51k
    return ret;
196
303k
}
197
198
static void vp6_coeff_order_table_init(VP56Context *s)
199
123k
{
200
123k
    int i, pos, idx = 1;
201
202
123k
    s->modelp->coeff_index_to_pos[0] = 0;
203
2.09M
    for (i=0; i<16; i++)
204
126M
        for (pos=1; pos<64; pos++)
205
124M
            if (s->modelp->coeff_reorder[pos] == i)
206
7.75M
                s->modelp->coeff_index_to_pos[idx++] = pos;
207
208
8.00M
    for (idx = 0; idx < 64; idx++) {
209
7.88M
        int max = 0;
210
264M
        for (i = 0; i <= idx; i++) {
211
256M
            int v = s->modelp->coeff_index_to_pos[i];
212
256M
            if (v > max)
213
162M
                max = v;
214
256M
        }
215
7.88M
        if (s->sub_version > 6)
216
3.03M
            max++;
217
7.88M
        s->modelp->coeff_index_to_idct_selector[idx] = max;
218
7.88M
    }
219
123k
}
220
221
static void vp6_default_models_init(VP56Context *s)
222
29.4k
{
223
29.4k
    VP56Model *model = s->modelp;
224
225
29.4k
    model->vector_dct[0] = 0xA2;
226
29.4k
    model->vector_dct[1] = 0xA4;
227
29.4k
    model->vector_sig[0] = 0x80;
228
29.4k
    model->vector_sig[1] = 0x80;
229
230
29.4k
    memcpy(model->mb_types_stats, ff_vp56_def_mb_types_stats, sizeof(model->mb_types_stats));
231
29.4k
    memcpy(model->vector_fdv, vp6_def_fdv_vector_model, sizeof(model->vector_fdv));
232
29.4k
    memcpy(model->vector_pdv, vp6_def_pdv_vector_model, sizeof(model->vector_pdv));
233
29.4k
    memcpy(model->coeff_runv, vp6_def_runv_coeff_model, sizeof(model->coeff_runv));
234
29.4k
    memcpy(model->coeff_reorder, s->def_coeff_reorder, sizeof(model->coeff_reorder));
235
236
29.4k
    vp6_coeff_order_table_init(s);
237
29.4k
}
238
239
static void vp6_parse_vector_models(VP56Context *s)
240
251k
{
241
251k
    VPXRangeCoder *c = &s->c;
242
251k
    VP56Model *model = s->modelp;
243
251k
    int comp, node;
244
245
754k
    for (comp=0; comp<2; comp++) {
246
503k
        if (vpx_rac_get_prob_branchy(c, vp6_sig_dct_pct[comp][0]))
247
93.4k
            model->vector_dct[comp] = vp56_rac_gets_nn(c, 7);
248
503k
        if (vpx_rac_get_prob_branchy(c, vp6_sig_dct_pct[comp][1]))
249
51.7k
            model->vector_sig[comp] = vp56_rac_gets_nn(c, 7);
250
503k
    }
251
252
754k
    for (comp=0; comp<2; comp++)
253
4.02M
        for (node=0; node<7; node++)
254
3.52M
            if (vpx_rac_get_prob_branchy(c, vp6_pdv_pct[comp][node]))
255
393k
                model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
256
257
754k
    for (comp=0; comp<2; comp++)
258
4.52M
        for (node=0; node<8; node++)
259
4.02M
            if (vpx_rac_get_prob_branchy(c, vp6_fdv_pct[comp][node]))
260
390k
                model->vector_fdv[comp][node] = vp56_rac_gets_nn(c, 7);
261
251k
}
262
263
/* nodes must ascend by count, but with descending symbol order */
264
static int vp6_huff_cmp(const void *va, const void *vb)
265
94.0M
{
266
94.0M
    const Node *a = va, *b = vb;
267
94.0M
    return (a->count - b->count)*16 + (b->sym - a->sym);
268
94.0M
}
269
270
static int vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[],
271
                               const uint8_t *map, unsigned size,
272
                               int nb_bits, VLC *vlc)
273
2.79M
{
274
2.79M
    Node nodes[2*VP6_MAX_HUFF_SIZE], *tmp = &nodes[size];
275
2.79M
    int a, b, i;
276
277
    /* first compute probabilities from model */
278
2.79M
    tmp[0].count = 256;
279
32.9M
    for (i=0; i<size-1; i++) {
280
30.1M
        a = tmp[i].count *        coeff_model[i]  >> 8;
281
30.1M
        b = tmp[i].count * (255 - coeff_model[i]) >> 8;
282
30.1M
        nodes[map[2*i  ]].count = a + !a;
283
30.1M
        nodes[map[2*i+1]].count = b + !b;
284
30.1M
    }
285
286
2.79M
    ff_vlc_free(vlc);
287
    /* then build the huffman tree according to probabilities */
288
2.79M
    return ff_huff_build_tree(s->avctx, vlc, size, nb_bits,
289
2.79M
                              nodes, vp6_huff_cmp,
290
2.79M
                              FF_HUFFMAN_FLAG_HNODE_FIRST);
291
2.79M
}
292
293
static int vp6_parse_coeff_models(VP56Context *s)
294
281k
{
295
281k
    VPXRangeCoder *c = &s->c;
296
281k
    VP56Model *model = s->modelp;
297
281k
    int def_prob[11];
298
281k
    int node, cg, ctx, pos;
299
281k
    int ct;    /* code type */
300
281k
    int pt;    /* plane type (0 for Y, 1 for U or V) */
301
281k
    int ret;
302
303
281k
    memset(def_prob, 0x80, sizeof(def_prob));
304
305
843k
    for (pt=0; pt<2; pt++)
306
6.74M
        for (node=0; node<11; node++)
307
6.18M
            if (vpx_rac_get_prob_branchy(c, vp6_dccv_pct[pt][node])) {
308
946k
                def_prob[node] = vp56_rac_gets_nn(c, 7);
309
946k
                model->coeff_dccv[pt][node] = def_prob[node];
310
5.23M
            } else if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
311
505k
                model->coeff_dccv[pt][node] = def_prob[node];
312
505k
            }
313
314
281k
    if (vpx_rac_get(c)) {
315
5.99M
        for (pos=1; pos<64; pos++)
316
5.90M
            if (vpx_rac_get_prob_branchy(c, vp6_coeff_reorder_pct[pos]))
317
2.50M
                model->coeff_reorder[pos] = vp56_rac_gets(c, 4);
318
93.6k
        vp6_coeff_order_table_init(s);
319
93.6k
    }
320
321
843k
    for (cg=0; cg<2; cg++)
322
8.43M
        for (node=0; node<14; node++)
323
7.87M
            if (vpx_rac_get_prob_branchy(c, vp6_runv_pct[cg][node]))
324
902k
                model->coeff_runv[cg][node] = vp56_rac_gets_nn(c, 7);
325
326
1.12M
    for (ct=0; ct<3; ct++)
327
2.53M
        for (pt=0; pt<2; pt++)
328
11.8M
            for (cg=0; cg<6; cg++)
329
121M
                for (node=0; node<11; node++)
330
111M
                    if (vpx_rac_get_prob_branchy(c, vp6_ract_pct[ct][pt][cg][node])) {
331
11.0M
                        def_prob[node] = vp56_rac_gets_nn(c, 7);
332
11.0M
                        model->coeff_ract[pt][ct][cg][node] = def_prob[node];
333
100M
                    } else if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
334
9.91M
                        model->coeff_ract[pt][ct][cg][node] = def_prob[node];
335
9.91M
                    }
336
337
281k
    if (s->use_huffman) {
338
299k
        for (pt=0; pt<2; pt++) {
339
199k
            ret = vp6_build_huff_tree(s, model->coeff_dccv[pt],
340
199k
                                      vp6_huff_coeff_map, 12, AC_DC_HUFF_BITS,
341
199k
                                      &s->dccv_vlc[pt]);
342
199k
            if (ret < 0)
343
0
                return ret;
344
199k
            ret = vp6_build_huff_tree(s, model->coeff_runv[pt],
345
199k
                                      vp6_huff_run_map, 9, RUN_HUFF_BITS,
346
199k
                                      &s->runv_vlc[pt]);
347
199k
            if (ret < 0)
348
0
                return ret;
349
798k
            for (ct=0; ct<3; ct++)
350
2.99M
                for (int cg = 0; cg < 4; cg++) {
351
2.39M
                    ret = vp6_build_huff_tree(s, model->coeff_ract[pt][ct][cg],
352
2.39M
                                              vp6_huff_coeff_map, 12,
353
2.39M
                                              AC_DC_HUFF_BITS,
354
2.39M
                                              &s->ract_vlc[pt][ct][cg]);
355
2.39M
                    if (ret < 0)
356
0
                        return ret;
357
2.39M
                }
358
199k
        }
359
99.8k
        memset(s->nb_null, 0, sizeof(s->nb_null));
360
181k
    } else {
361
    /* coeff_dcct is a linear combination of coeff_dccv */
362
543k
    for (pt=0; pt<2; pt++)
363
1.45M
        for (ctx=0; ctx<3; ctx++)
364
6.52M
            for (node=0; node<5; node++)
365
5.43M
                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);
366
181k
    }
367
281k
    return 0;
368
281k
}
369
370
static void vp6_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
371
1.45M
{
372
1.45M
    VPXRangeCoder *c = &s->c;
373
1.45M
    VP56Model *model = s->modelp;
374
1.45M
    int comp;
375
376
1.45M
    *vect = (VP56mv) {0,0};
377
1.45M
    if (s->vector_candidate_pos < 2)
378
1.41M
        *vect = s->vector_candidate[0];
379
380
4.37M
    for (comp=0; comp<2; comp++) {
381
2.91M
        int i, delta = 0;
382
383
2.91M
        if (vpx_rac_get_prob_branchy(c, model->vector_dct[comp])) {
384
1.74M
            static const uint8_t prob_order[] = {0, 1, 2, 7, 6, 5, 4};
385
13.9M
            for (i=0; i<sizeof(prob_order); i++) {
386
12.2M
                int j = prob_order[i];
387
12.2M
                delta |= vpx_rac_get_prob(c, model->vector_fdv[comp][j])<<j;
388
12.2M
            }
389
1.74M
            if (delta & 0xF0)
390
1.18M
                delta |= vpx_rac_get_prob(c, model->vector_fdv[comp][3])<<3;
391
562k
            else
392
562k
                delta |= 8;
393
1.74M
        } else {
394
1.16M
            delta = vp56_rac_get_tree(c, ff_vp56_pva_tree,
395
1.16M
                                      model->vector_pdv[comp]);
396
1.16M
        }
397
398
2.91M
        if (delta && vpx_rac_get_prob_branchy(c, model->vector_sig[comp]))
399
1.34M
            delta = -delta;
400
401
2.91M
        if (!comp)
402
1.45M
            vect->x += delta;
403
1.45M
        else
404
1.45M
            vect->y += delta;
405
2.91M
    }
406
1.45M
}
407
408
/**
409
 * Read number of consecutive blocks with null DC or AC.
410
 * This value is < 74.
411
 */
412
static unsigned vp6_get_nb_null(VP56Context *s)
413
1.99M
{
414
1.99M
    unsigned val = get_bits(&s->gb, 2);
415
1.99M
    if (val == 2)
416
187k
        val += get_bits(&s->gb, 2);
417
1.80M
    else if (val == 3) {
418
176k
        val = get_bits1(&s->gb) << 2;
419
176k
        val = 6+val + get_bits(&s->gb, 2+val);
420
176k
    }
421
1.99M
    return val;
422
1.99M
}
423
424
static int vp6_parse_coeff_huffman(VP56Context *s)
425
1.43M
{
426
1.43M
    VP56Model *model = s->modelp;
427
1.43M
    uint8_t *permute = s->idct_scantable;
428
1.43M
    VLC *vlc_coeff;
429
1.43M
    int sign, coeff_idx;
430
1.43M
    int b, cg, idx;
431
1.43M
    int pt = 0;    /* plane type (0 for Y, 1 for U or V) */
432
433
10.0M
    for (b=0; b<6; b++) {
434
8.62M
        int ct = 0;    /* code type */
435
8.62M
        if (b > 3) pt = 1;
436
8.62M
        vlc_coeff = &s->dccv_vlc[pt];
437
438
48.5M
        for (coeff_idx = 0;;) {
439
48.5M
            int run = 1;
440
48.5M
            if (coeff_idx<2 && s->nb_null[coeff_idx][pt]) {
441
5.23M
                s->nb_null[coeff_idx][pt]--;
442
5.23M
                if (coeff_idx)
443
2.75M
                    break;
444
43.3M
            } else {
445
43.3M
                if (get_bits_left(&s->gb) <= 0)
446
6.87k
                    return AVERROR_INVALIDDATA;
447
43.3M
                int coeff = get_vlc2(&s->gb, vlc_coeff->table, AC_DC_HUFF_BITS, 2);
448
43.3M
                if (coeff == 0) {
449
17.2M
                    if (coeff_idx) {
450
16.2M
                        int pt = (coeff_idx >= 6);
451
16.2M
                        run += get_vlc2(&s->gb, s->runv_vlc[pt].table, RUN_HUFF_BITS, 1);
452
16.2M
                        if (run >= 9)
453
184k
                            run += get_bits(&s->gb, 6);
454
16.2M
                    } else
455
1.07M
                        s->nb_null[0][pt] = vp6_get_nb_null(s);
456
17.2M
                    ct = 0;
457
26.0M
                } else if (coeff == 11) {  /* end of block */
458
4.72M
                    if (coeff_idx == 1)    /* first AC coeff ? */
459
924k
                        s->nb_null[1][pt] = vp6_get_nb_null(s);
460
4.72M
                    break;
461
21.3M
                } else {
462
21.3M
                    int coeff2 = ff_vp56_coeff_bias[coeff];
463
21.3M
                    if (coeff > 4)
464
8.92M
                        coeff2 += get_bits(&s->gb, coeff <= 9 ? coeff - 4 : 11);
465
21.3M
                    ct = 1 + (coeff2 > 1);
466
21.3M
                    sign = get_bits1(&s->gb);
467
21.3M
                    coeff2 = (coeff2 ^ -sign) + sign;
468
21.3M
                    if (coeff_idx)
469
18.5M
                        coeff2 *= s->dequant_ac;
470
21.3M
                    idx = model->coeff_index_to_pos[coeff_idx];
471
21.3M
                    s->block_coeff[b][permute[idx]] = coeff2;
472
21.3M
                }
473
43.3M
            }
474
41.0M
            coeff_idx+=run;
475
41.0M
            if (coeff_idx >= 64)
476
1.14M
                break;
477
39.9M
            cg = FFMIN(vp6_coeff_groups[coeff_idx], 3);
478
39.9M
            vlc_coeff = &s->ract_vlc[pt][ct][cg];
479
39.9M
        }
480
8.61M
        s->idct_selector[b] = model->coeff_index_to_idct_selector[FFMIN(coeff_idx, 63)];
481
8.61M
    }
482
1.43M
    return 0;
483
1.43M
}
484
485
static int vp6_parse_coeff(VP56Context *s)
486
5.99M
{
487
5.99M
    VPXRangeCoder *c = s->ccp;
488
5.99M
    VP56Model *model = s->modelp;
489
5.99M
    uint8_t *permute = s->idct_scantable;
490
5.99M
    uint8_t *model1, *model2, *model3;
491
5.99M
    int coeff, sign, coeff_idx;
492
5.99M
    int b, i, cg, idx, ctx;
493
5.99M
    int pt = 0;    /* plane type (0 for Y, 1 for U or V) */
494
495
5.99M
    if (vpx_rac_is_end(c)) {
496
18.0k
        av_log(s->avctx, AV_LOG_ERROR, "End of AC stream reached in vp6_parse_coeff\n");
497
18.0k
        return AVERROR_INVALIDDATA;
498
18.0k
    }
499
500
41.8M
    for (b=0; b<6; b++) {
501
35.8M
        int ct = 1;    /* code type */
502
35.8M
        int run = 1;
503
504
35.8M
        if (b > 3) pt = 1;
505
506
35.8M
        ctx = s->left_block[ff_vp56_b6to4[b]].not_null_dc
507
35.8M
              + s->above_blocks[s->above_block_idx[b]].not_null_dc;
508
35.8M
        model1 = model->coeff_dccv[pt];
509
35.8M
        model2 = model->coeff_dcct[pt][ctx];
510
511
35.8M
        coeff_idx = 0;
512
144M
        for (;;) {
513
144M
            if ((coeff_idx>1 && ct==0) || vpx_rac_get_prob_branchy(c, model2[0])) {
514
                /* parse a coeff */
515
75.0M
                if (vpx_rac_get_prob_branchy(c, model2[2])) {
516
73.4M
                    if (vpx_rac_get_prob_branchy(c, model2[3])) {
517
72.6M
                        idx = vp56_rac_get_tree(c, ff_vp56_pc_tree, model1);
518
72.6M
                        coeff = ff_vp56_coeff_bias[idx+5];
519
861M
                        for (i=ff_vp56_coeff_bit_length[idx]; i>=0; i--)
520
788M
                            coeff += vpx_rac_get_prob(c, ff_vp56_coeff_parse_table[idx][i]) << i;
521
72.6M
                    } else {
522
807k
                        if (vpx_rac_get_prob_branchy(c, model2[4]))
523
373k
                            coeff = 3 + vpx_rac_get_prob(c, model1[5]);
524
434k
                        else
525
434k
                            coeff = 2;
526
807k
                    }
527
73.4M
                    ct = 2;
528
73.4M
                } else {
529
1.63M
                    ct = 1;
530
1.63M
                    coeff = 1;
531
1.63M
                }
532
75.0M
                sign = vpx_rac_get(c);
533
75.0M
                coeff = (coeff ^ -sign) + sign;
534
75.0M
                if (coeff_idx)
535
73.3M
                    coeff *= s->dequant_ac;
536
75.0M
                idx = model->coeff_index_to_pos[coeff_idx];
537
75.0M
                s->block_coeff[b][permute[idx]] = coeff;
538
75.0M
                run = 1;
539
75.0M
            } else {
540
                /* parse a run */
541
69.5M
                ct = 0;
542
69.5M
                if (coeff_idx > 0) {
543
35.4M
                    if (!vpx_rac_get_prob_branchy(c, model2[1]))
544
34.6M
                        break;
545
546
809k
                    model3 = model->coeff_runv[coeff_idx >= 6];
547
809k
                    run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
548
809k
                    if (!run)
549
1.05M
                        for (run=9, i=0; i<6; i++)
550
902k
                            run += vpx_rac_get_prob(c, model3[i+8]) << i;
551
809k
                }
552
69.5M
            }
553
109M
            coeff_idx += run;
554
109M
            if (coeff_idx >= 64)
555
1.15M
                break;
556
108M
            cg = vp6_coeff_groups[coeff_idx];
557
108M
            model1 = model2 = model->coeff_ract[pt][ct][cg];
558
108M
        }
559
560
35.8M
        s->left_block[ff_vp56_b6to4[b]].not_null_dc =
561
35.8M
        s->above_blocks[s->above_block_idx[b]].not_null_dc = !!s->block_coeff[b][0];
562
35.8M
        s->idct_selector[b] = model->coeff_index_to_idct_selector[FFMIN(coeff_idx, 63)];
563
35.8M
    }
564
5.97M
    return 0;
565
5.99M
}
566
567
static int vp6_block_variance(uint8_t *src, ptrdiff_t stride)
568
317k
{
569
317k
    int sum = 0, square_sum = 0;
570
317k
    int y, x;
571
572
1.58M
    for (y=0; y<8; y+=2) {
573
6.35M
        for (x=0; x<8; x+=2) {
574
5.08M
            sum += src[x];
575
5.08M
            square_sum += src[x]*src[x];
576
5.08M
        }
577
1.27M
        src += 2*stride;
578
1.27M
    }
579
317k
    return (16*square_sum - sum*sum) >> 8;
580
317k
}
581
582
static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, ptrdiff_t stride,
583
                           int delta, const int16_t *weights)
584
135k
{
585
135k
    int x, y;
586
587
1.22M
    for (y=0; y<8; y++) {
588
9.77M
        for (x=0; x<8; x++) {
589
8.68M
            dst[x] = av_clip_uint8((  src[x-delta  ] * weights[0]
590
8.68M
                                 + src[x        ] * weights[1]
591
8.68M
                                 + src[x+delta  ] * weights[2]
592
8.68M
                                 + src[x+2*delta] * weights[3] + 64) >> 7);
593
8.68M
        }
594
1.08M
        src += stride;
595
1.08M
        dst += stride;
596
1.08M
    }
597
135k
}
598
599
static void vp6_filter_diag2(VP56Context *s, uint8_t *dst, uint8_t *src,
600
                             ptrdiff_t stride, int h_weight, int v_weight)
601
2.41M
{
602
2.41M
    uint8_t *tmp = s->edge_emu_buffer+16;
603
2.41M
    s->h264chroma.put_h264_chroma_pixels_tab[0](tmp, src, stride, 9, h_weight, 0);
604
2.41M
    s->h264chroma.put_h264_chroma_pixels_tab[0](dst, tmp, stride, 8, 0, v_weight);
605
2.41M
}
606
607
static void vp6_filter(VP56Context *s, uint8_t *dst, uint8_t *src,
608
                       int offset1, int offset2, ptrdiff_t stride,
609
                       VP56mv mv, int mask, int select, int luma)
610
4.47M
{
611
4.47M
    int filter4 = 0;
612
4.47M
    int x8 = mv.x & mask;
613
4.47M
    int y8 = mv.y & mask;
614
615
4.47M
    if (luma) {
616
3.08M
        x8 *= 2;
617
3.08M
        y8 *= 2;
618
3.08M
        filter4 = s->filter_mode;
619
3.08M
        if (filter4 == 2) {
620
1.79M
            if (s->max_vector_length &&
621
1.79M
                (FFABS(mv.x) > s->max_vector_length ||
622
1.47M
                 FFABS(mv.y) > s->max_vector_length)) {
623
1.47M
                filter4 = 0;
624
1.47M
            } else if (s->sample_variance_threshold
625
317k
                       && (vp6_block_variance(src+offset1, stride)
626
317k
                           < s->sample_variance_threshold)) {
627
300k
                filter4 = 0;
628
300k
            }
629
1.79M
        }
630
3.08M
    }
631
632
4.47M
    if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
633
2.77M
        offset1 = offset2;
634
2.77M
    }
635
636
4.47M
    if (filter4) {
637
290k
        if (!y8) {                      /* left or right combine */
638
68.4k
            vp6_filter_hv4(dst, src+offset1, stride, 1,
639
68.4k
                           vp6_block_copy_filter[select][x8]);
640
221k
        } else if (!x8) {               /* above or below combine */
641
67.2k
            vp6_filter_hv4(dst, src+offset1, stride, stride,
642
67.2k
                           vp6_block_copy_filter[select][y8]);
643
154k
        } else {
644
154k
            s->vp6dsp.vp6_filter_diag4(dst, src+offset1+((mv.x^mv.y)>>31), stride,
645
154k
                             vp6_block_copy_filter[select][x8],
646
154k
                             vp6_block_copy_filter[select][y8]);
647
154k
        }
648
4.18M
    } else {
649
4.18M
        if (!x8 || !y8) {
650
1.77M
            s->h264chroma.put_h264_chroma_pixels_tab[0](dst, src + offset1, stride, 8, x8, y8);
651
2.41M
        } else {
652
2.41M
            vp6_filter_diag2(s, dst, src+offset1 + ((mv.x^mv.y)>>31), stride, x8, y8);
653
2.41M
        }
654
4.18M
    }
655
4.47M
}
656
657
static av_cold int vp6_decode_init_context(AVCodecContext *avctx,
658
                                           VP56Context *s, int flip, int has_alpha)
659
17.0k
{
660
17.0k
    int ret = ff_vp56_init_context(avctx, s, flip, has_alpha);
661
17.0k
    if (ret < 0)
662
0
        return ret;
663
664
17.0k
    ff_vp6dsp_init(&s->vp6dsp);
665
666
17.0k
    s->deblock_filtering = 0;
667
17.0k
    s->vp56_coord_div = vp6_coord_div;
668
17.0k
    s->parse_vector_adjustment = vp6_parse_vector_adjustment;
669
17.0k
    s->filter = vp6_filter;
670
17.0k
    s->default_models_init = vp6_default_models_init;
671
17.0k
    s->parse_vector_models = vp6_parse_vector_models;
672
17.0k
    s->parse_coeff_models = vp6_parse_coeff_models;
673
17.0k
    s->parse_header = vp6_parse_header;
674
675
17.0k
    return 0;
676
17.0k
}
677
678
static av_cold int vp6_decode_init(AVCodecContext *avctx)
679
12.6k
{
680
12.6k
    VP56Context *s = avctx->priv_data;
681
12.6k
    int ret;
682
683
12.6k
    ret = vp6_decode_init_context(avctx, s, avctx->codec_id == AV_CODEC_ID_VP6,
684
12.6k
                                  avctx->codec_id == AV_CODEC_ID_VP6A);
685
12.6k
    if (ret < 0)
686
0
        return ret;
687
688
12.6k
    if (s->has_alpha) {
689
        /* Can only happen for ff_vp6a_decoder */
690
4.38k
        s->alpha_context = &s[1];
691
4.38k
        ret = vp6_decode_init_context(avctx, s->alpha_context,
692
4.38k
                                      s->flip == -1, s->has_alpha);
693
4.38k
        if (ret < 0)
694
0
            return ret;
695
4.38k
    }
696
697
12.6k
    return 0;
698
12.6k
}
699
700
static av_cold void vp6_decode_free_context(VP56Context *s);
701
702
static av_cold int vp6_decode_free(AVCodecContext *avctx)
703
12.6k
{
704
12.6k
    VP56Context *s = avctx->priv_data;
705
706
12.6k
    vp6_decode_free_context(s);
707
708
12.6k
    if (s->alpha_context) {
709
4.38k
        vp6_decode_free_context(s->alpha_context);
710
4.38k
        s->alpha_context = NULL;
711
4.38k
    }
712
713
12.6k
    return 0;
714
12.6k
}
715
716
static av_cold void vp6_decode_free_context(VP56Context *s)
717
17.0k
{
718
17.0k
    ff_vp56_free_context(s);
719
720
51.0k
    for (int pt = 0; pt < 2; ++pt) {
721
34.0k
        ff_vlc_free(&s->dccv_vlc[pt]);
722
34.0k
        ff_vlc_free(&s->runv_vlc[pt]);
723
136k
        for (int ct = 0; ct < 3; ++ct)
724
510k
            for (int cg = 0; cg < 4; ++cg)
725
408k
                ff_vlc_free(&s->ract_vlc[pt][ct][cg]);
726
34.0k
    }
727
17.0k
}
728
729
const FFCodec ff_vp6_decoder = {
730
    .p.name         = "vp6",
731
    CODEC_LONG_NAME("On2 VP6"),
732
    .p.type         = AVMEDIA_TYPE_VIDEO,
733
    .p.id           = AV_CODEC_ID_VP6,
734
    .priv_data_size = sizeof(VP56Context),
735
    .init           = vp6_decode_init,
736
    .close          = vp6_decode_free,
737
    FF_CODEC_DECODE_CB(ff_vp56_decode_frame),
738
    .p.capabilities = AV_CODEC_CAP_DR1,
739
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
740
};
741
742
/* flash version, not flipped upside-down */
743
const FFCodec ff_vp6f_decoder = {
744
    .p.name         = "vp6f",
745
    CODEC_LONG_NAME("On2 VP6 (Flash version)"),
746
    .p.type         = AVMEDIA_TYPE_VIDEO,
747
    .p.id           = AV_CODEC_ID_VP6F,
748
    .priv_data_size = sizeof(VP56Context),
749
    .init           = vp6_decode_init,
750
    .close          = vp6_decode_free,
751
    FF_CODEC_DECODE_CB(ff_vp56_decode_frame),
752
    .p.capabilities = AV_CODEC_CAP_DR1,
753
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
754
};
755
756
/* flash version, not flipped upside-down, with alpha channel */
757
const FFCodec ff_vp6a_decoder = {
758
    .p.name         = "vp6a",
759
    CODEC_LONG_NAME("On2 VP6 (Flash version, with alpha channel)"),
760
    .p.type         = AVMEDIA_TYPE_VIDEO,
761
    .p.id           = AV_CODEC_ID_VP6A,
762
    .priv_data_size = 2 /* Main context + alpha context */ * sizeof(VP56Context),
763
    .init           = vp6_decode_init,
764
    .close          = vp6_decode_free,
765
    FF_CODEC_DECODE_CB(ff_vp56_decode_frame),
766
    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
767
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
768
};