Coverage Report

Created: 2025-08-28 07:12

/src/ffmpeg/libavcodec/vp6.c
Line
Count
Source (jump to first uncovered line)
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
44.7M
#define AC_DC_HUFF_BITS   10
45
20.3M
#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
530k
{
52
530k
    VPXRangeCoder *c = &s->c;
53
530k
    int parse_filter_info = 0;
54
530k
    int coeff_offset = 0;
55
530k
    int vrt_shift = 0;
56
530k
    int sub_version;
57
530k
    int rows, cols;
58
530k
    int res = 0;
59
530k
    int ret;
60
530k
    int separated_coeff = buf[0] & 1;
61
62
530k
    if (!(buf[0] & 0x80))
63
56.7k
        s->frames[VP56_FRAME_CURRENT]->flags |= AV_FRAME_FLAG_KEY;
64
473k
    else
65
473k
        s->frames[VP56_FRAME_CURRENT]->flags &= ~AV_FRAME_FLAG_KEY;
66
530k
    ff_vp56_init_dequant(s, (buf[0] >> 1) & 0x3F);
67
68
530k
    if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
69
56.7k
        sub_version = buf[1] >> 3;
70
56.7k
        if (sub_version > 8)
71
1.79k
            return AVERROR_INVALIDDATA;
72
54.9k
        s->filter_header = buf[1] & 0x06;
73
54.9k
        s->interlaced = buf[1] & 1;
74
54.9k
        if (s->interlaced)
75
3.06k
            s->def_coeff_reorder = vp6_il_coeff_reorder;
76
51.9k
        else
77
51.9k
            s->def_coeff_reorder = vp6_def_coeff_reorder;
78
54.9k
        if (separated_coeff || !s->filter_header) {
79
17.0k
            coeff_offset = AV_RB16(buf+2) - 2;
80
17.0k
            buf += 2;
81
17.0k
            buf_size -= 2;
82
17.0k
        }
83
84
54.9k
        rows = buf[2];  /* number of stored macroblock rows */
85
54.9k
        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
54.9k
        if (!rows || !cols) {
89
14.9k
            av_log(s->avctx, AV_LOG_ERROR, "Invalid size %dx%d\n", cols << 4, rows << 4);
90
14.9k
            return AVERROR_INVALIDDATA;
91
14.9k
        }
92
93
40.0k
        if (!s->macroblocks || /* first frame */
94
40.0k
            16*cols != s->avctx->coded_width ||
95
40.0k
            16*rows != s->avctx->coded_height) {
96
29.4k
            if (s->avctx->extradata_size == 0 &&
97
29.4k
                FFALIGN(s->avctx->width,  16) == 16 * cols &&
98
29.4k
                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
29.4k
            } else {
105
29.4k
                ret = ff_set_dimensions(s->avctx, 16 * cols, 16 * rows);
106
29.4k
                if (ret < 0)
107
0
                    return ret;
108
109
29.4k
                if (s->avctx->extradata_size == 1) {
110
903
                    s->avctx->width  -= s->avctx->extradata[0] >> 4;
111
903
                    s->avctx->height -= s->avctx->extradata[0] & 0x0F;
112
903
                }
113
29.4k
            }
114
29.4k
            res = VP56_SIZE_CHANGE;
115
29.4k
        }
116
117
40.0k
        ret = ff_vpx_init_range_decoder(c, buf+6, buf_size-6);
118
40.0k
        if (ret < 0)
119
2.94k
            goto fail;
120
37.1k
        vp56_rac_gets(c, 2);
121
122
37.1k
        parse_filter_info = s->filter_header;
123
37.1k
        if (sub_version < 8)
124
22.0k
            vrt_shift = 5;
125
37.1k
        s->sub_version = sub_version;
126
37.1k
        s->golden_frame = 0;
127
473k
    } else {
128
473k
        if (!s->sub_version || !s->avctx->coded_width || !s->avctx->coded_height)
129
6.57k
            return AVERROR_INVALIDDATA;
130
131
467k
        if (separated_coeff || !s->filter_header) {
132
14.5k
            coeff_offset = AV_RB16(buf+1) - 2;
133
14.5k
            buf += 2;
134
14.5k
            buf_size -= 2;
135
14.5k
        }
136
467k
        ret = ff_vpx_init_range_decoder(c, buf+1, buf_size-1);
137
467k
        if (ret < 0)
138
3.28k
            return ret;
139
140
463k
        s->golden_frame = vpx_rac_get(c);
141
463k
        if (s->filter_header) {
142
460k
            s->deblock_filtering = vpx_rac_get(c);
143
460k
            if (s->deblock_filtering)
144
286k
                vpx_rac_get(c);
145
460k
            if (s->sub_version > 7)
146
138k
                parse_filter_info = vpx_rac_get(c);
147
460k
        }
148
463k
    }
149
150
500k
    if (parse_filter_info) {
151
79.8k
        if (vpx_rac_get(c)) {
152
46.9k
            s->filter_mode = 2;
153
46.9k
            s->sample_variance_threshold = vp56_rac_gets(c, 5) << vrt_shift;
154
46.9k
            s->max_vector_length = 2 << vp56_rac_gets(c, 3);
155
46.9k
        } else if (vpx_rac_get(c)) {
156
18.0k
            s->filter_mode = 1;
157
18.0k
        } else {
158
14.9k
            s->filter_mode = 0;
159
14.9k
        }
160
79.8k
        if (s->sub_version > 7)
161
60.1k
            s->filter_selection = vp56_rac_gets(c, 4);
162
19.6k
        else
163
19.6k
            s->filter_selection = 16;
164
79.8k
    }
165
166
500k
    s->use_huffman = vpx_rac_get(c);
167
168
500k
    s->parse_coeff = vp6_parse_coeff;
169
500k
    if (coeff_offset) {
170
15.9k
        buf      += coeff_offset;
171
15.9k
        buf_size -= coeff_offset;
172
15.9k
        if (buf_size < 0) {
173
1.89k
            ret = AVERROR_INVALIDDATA;
174
1.89k
            goto fail;
175
1.89k
        }
176
14.0k
        if (s->use_huffman) {
177
8.07k
            s->parse_coeff = vp6_parse_coeff_huffman;
178
8.07k
            ret = init_get_bits8(&s->gb, buf, buf_size);
179
8.07k
            if (ret < 0)
180
0
                return ret;
181
8.07k
        } else {
182
5.98k
            ret = ff_vpx_init_range_decoder(&s->cc, buf, buf_size);
183
5.98k
            if (ret < 0)
184
765
                goto fail;
185
5.21k
            s->ccp = &s->cc;
186
5.21k
        }
187
484k
    } else {
188
484k
        s->ccp = &s->c;
189
484k
    }
190
191
498k
    return res;
192
5.60k
fail:
193
5.60k
    if (res == VP56_SIZE_CHANGE)
194
3.57k
        ff_set_dimensions(s->avctx, 0, 0);
195
5.60k
    return ret;
196
500k
}
197
198
static void vp6_coeff_order_table_init(VP56Context *s)
199
216k
{
200
216k
    int i, pos, idx = 1;
201
202
216k
    s->modelp->coeff_index_to_pos[0] = 0;
203
3.68M
    for (i=0; i<16; i++)
204
222M
        for (pos=1; pos<64; pos++)
205
218M
            if (s->modelp->coeff_reorder[pos] == i)
206
13.6M
                s->modelp->coeff_index_to_pos[idx++] = pos;
207
208
14.1M
    for (idx = 0; idx < 64; idx++) {
209
13.8M
        int max = 0;
210
465M
        for (i = 0; i <= idx; i++) {
211
451M
            int v = s->modelp->coeff_index_to_pos[i];
212
451M
            if (v > max)
213
224M
                max = v;
214
451M
        }
215
13.8M
        if (s->sub_version > 6)
216
5.47M
            max++;
217
13.8M
        s->modelp->coeff_index_to_idct_selector[idx] = max;
218
13.8M
    }
219
216k
}
220
221
static void vp6_default_models_init(VP56Context *s)
222
30.9k
{
223
30.9k
    VP56Model *model = s->modelp;
224
225
30.9k
    model->vector_dct[0] = 0xA2;
226
30.9k
    model->vector_dct[1] = 0xA4;
227
30.9k
    model->vector_sig[0] = 0x80;
228
30.9k
    model->vector_sig[1] = 0x80;
229
230
30.9k
    memcpy(model->mb_types_stats, ff_vp56_def_mb_types_stats, sizeof(model->mb_types_stats));
231
30.9k
    memcpy(model->vector_fdv, vp6_def_fdv_vector_model, sizeof(model->vector_fdv));
232
30.9k
    memcpy(model->vector_pdv, vp6_def_pdv_vector_model, sizeof(model->vector_pdv));
233
30.9k
    memcpy(model->coeff_runv, vp6_def_runv_coeff_model, sizeof(model->coeff_runv));
234
30.9k
    memcpy(model->coeff_reorder, s->def_coeff_reorder, sizeof(model->coeff_reorder));
235
236
30.9k
    vp6_coeff_order_table_init(s);
237
30.9k
}
238
239
static void vp6_parse_vector_models(VP56Context *s)
240
452k
{
241
452k
    VPXRangeCoder *c = &s->c;
242
452k
    VP56Model *model = s->modelp;
243
452k
    int comp, node;
244
245
1.35M
    for (comp=0; comp<2; comp++) {
246
904k
        if (vpx_rac_get_prob_branchy(c, vp6_sig_dct_pct[comp][0]))
247
159k
            model->vector_dct[comp] = vp56_rac_gets_nn(c, 7);
248
904k
        if (vpx_rac_get_prob_branchy(c, vp6_sig_dct_pct[comp][1]))
249
65.9k
            model->vector_sig[comp] = vp56_rac_gets_nn(c, 7);
250
904k
    }
251
252
1.35M
    for (comp=0; comp<2; comp++)
253
7.23M
        for (node=0; node<7; node++)
254
6.33M
            if (vpx_rac_get_prob_branchy(c, vp6_pdv_pct[comp][node]))
255
533k
                model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
256
257
1.35M
    for (comp=0; comp<2; comp++)
258
8.13M
        for (node=0; node<8; node++)
259
7.23M
            if (vpx_rac_get_prob_branchy(c, vp6_fdv_pct[comp][node]))
260
495k
                model->vector_fdv[comp][node] = vp56_rac_gets_nn(c, 7);
261
452k
}
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
71.4M
{
266
71.4M
    const Node *a = va, *b = vb;
267
71.4M
    return (a->count - b->count)*16 + (b->sym - a->sym);
268
71.4M
}
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.11M
{
274
2.11M
    Node nodes[2*VP6_MAX_HUFF_SIZE], *tmp = &nodes[size];
275
2.11M
    int a, b, i;
276
277
    /* first compute probabilities from model */
278
2.11M
    tmp[0].count = 256;
279
24.9M
    for (i=0; i<size-1; i++) {
280
22.8M
        a = tmp[i].count *        coeff_model[i]  >> 8;
281
22.8M
        b = tmp[i].count * (255 - coeff_model[i]) >> 8;
282
22.8M
        nodes[map[2*i  ]].count = a + !a;
283
22.8M
        nodes[map[2*i+1]].count = b + !b;
284
22.8M
    }
285
286
2.11M
    ff_vlc_free(vlc);
287
    /* then build the huffman tree according to probabilities */
288
2.11M
    return ff_huff_build_tree(s->avctx, vlc, size, nb_bits,
289
2.11M
                              nodes, vp6_huff_cmp,
290
2.11M
                              FF_HUFFMAN_FLAG_HNODE_FIRST);
291
2.11M
}
292
293
static int vp6_parse_coeff_models(VP56Context *s)
294
483k
{
295
483k
    VPXRangeCoder *c = &s->c;
296
483k
    VP56Model *model = s->modelp;
297
483k
    int def_prob[11];
298
483k
    int node, cg, ctx, pos;
299
483k
    int ct;    /* code type */
300
483k
    int pt;    /* plane type (0 for Y, 1 for U or V) */
301
483k
    int ret;
302
303
483k
    memset(def_prob, 0x80, sizeof(def_prob));
304
305
1.44M
    for (pt=0; pt<2; pt++)
306
11.5M
        for (node=0; node<11; node++)
307
10.6M
            if (vpx_rac_get_prob_branchy(c, vp6_dccv_pct[pt][node])) {
308
1.44M
                def_prob[node] = vp56_rac_gets_nn(c, 7);
309
1.44M
                model->coeff_dccv[pt][node] = def_prob[node];
310
9.18M
            } else if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
311
534k
                model->coeff_dccv[pt][node] = def_prob[node];
312
534k
            }
313
314
483k
    if (vpx_rac_get(c)) {
315
11.9M
        for (pos=1; pos<64; pos++)
316
11.7M
            if (vpx_rac_get_prob_branchy(c, vp6_coeff_reorder_pct[pos]))
317
4.13M
                model->coeff_reorder[pos] = vp56_rac_gets(c, 4);
318
185k
        vp6_coeff_order_table_init(s);
319
185k
    }
320
321
1.44M
    for (cg=0; cg<2; cg++)
322
14.4M
        for (node=0; node<14; node++)
323
13.5M
            if (vpx_rac_get_prob_branchy(c, vp6_runv_pct[cg][node]))
324
1.32M
                model->coeff_runv[cg][node] = vp56_rac_gets_nn(c, 7);
325
326
1.93M
    for (ct=0; ct<3; ct++)
327
4.34M
        for (pt=0; pt<2; pt++)
328
20.2M
            for (cg=0; cg<6; cg++)
329
208M
                for (node=0; node<11; node++)
330
191M
                    if (vpx_rac_get_prob_branchy(c, vp6_ract_pct[ct][pt][cg][node])) {
331
13.5M
                        def_prob[node] = vp56_rac_gets_nn(c, 7);
332
13.5M
                        model->coeff_ract[pt][ct][cg][node] = def_prob[node];
333
177M
                    } else if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
334
10.4M
                        model->coeff_ract[pt][ct][cg][node] = def_prob[node];
335
10.4M
                    }
336
337
483k
    if (s->use_huffman) {
338
226k
        for (pt=0; pt<2; pt++) {
339
151k
            ret = vp6_build_huff_tree(s, model->coeff_dccv[pt],
340
151k
                                      vp6_huff_coeff_map, 12, AC_DC_HUFF_BITS,
341
151k
                                      &s->dccv_vlc[pt]);
342
151k
            if (ret < 0)
343
0
                return ret;
344
151k
            ret = vp6_build_huff_tree(s, model->coeff_runv[pt],
345
151k
                                      vp6_huff_run_map, 9, RUN_HUFF_BITS,
346
151k
                                      &s->runv_vlc[pt]);
347
151k
            if (ret < 0)
348
0
                return ret;
349
605k
            for (ct=0; ct<3; ct++)
350
2.26M
                for (int cg = 0; cg < 4; cg++) {
351
1.81M
                    ret = vp6_build_huff_tree(s, model->coeff_ract[pt][ct][cg],
352
1.81M
                                              vp6_huff_coeff_map, 12,
353
1.81M
                                              AC_DC_HUFF_BITS,
354
1.81M
                                              &s->ract_vlc[pt][ct][cg]);
355
1.81M
                    if (ret < 0)
356
0
                        return ret;
357
1.81M
                }
358
151k
        }
359
75.6k
        memset(s->nb_null, 0, sizeof(s->nb_null));
360
407k
    } else {
361
    /* coeff_dcct is a linear combination of coeff_dccv */
362
1.22M
    for (pt=0; pt<2; pt++)
363
3.26M
        for (ctx=0; ctx<3; ctx++)
364
14.6M
            for (node=0; node<5; node++)
365
12.2M
                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
407k
    }
367
483k
    return 0;
368
483k
}
369
370
static void vp6_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
371
1.41M
{
372
1.41M
    VPXRangeCoder *c = &s->c;
373
1.41M
    VP56Model *model = s->modelp;
374
1.41M
    int comp;
375
376
1.41M
    *vect = (VP56mv) {0,0};
377
1.41M
    if (s->vector_candidate_pos < 2)
378
1.37M
        *vect = s->vector_candidate[0];
379
380
4.23M
    for (comp=0; comp<2; comp++) {
381
2.82M
        int i, delta = 0;
382
383
2.82M
        if (vpx_rac_get_prob_branchy(c, model->vector_dct[comp])) {
384
1.63M
            static const uint8_t prob_order[] = {0, 1, 2, 7, 6, 5, 4};
385
13.0M
            for (i=0; i<sizeof(prob_order); i++) {
386
11.4M
                int j = prob_order[i];
387
11.4M
                delta |= vpx_rac_get_prob(c, model->vector_fdv[comp][j])<<j;
388
11.4M
            }
389
1.63M
            if (delta & 0xF0)
390
1.09M
                delta |= vpx_rac_get_prob(c, model->vector_fdv[comp][3])<<3;
391
537k
            else
392
537k
                delta |= 8;
393
1.63M
        } else {
394
1.19M
            delta = vp56_rac_get_tree(c, ff_vp56_pva_tree,
395
1.19M
                                      model->vector_pdv[comp]);
396
1.19M
        }
397
398
2.82M
        if (delta && vpx_rac_get_prob_branchy(c, model->vector_sig[comp]))
399
1.34M
            delta = -delta;
400
401
2.82M
        if (!comp)
402
1.41M
            vect->x += delta;
403
1.41M
        else
404
1.41M
            vect->y += delta;
405
2.82M
    }
406
1.41M
}
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
2.15M
{
414
2.15M
    unsigned val = get_bits(&s->gb, 2);
415
2.15M
    if (val == 2)
416
316k
        val += get_bits(&s->gb, 2);
417
1.84M
    else if (val == 3) {
418
254k
        val = get_bits1(&s->gb) << 2;
419
254k
        val = 6+val + get_bits(&s->gb, 2+val);
420
254k
    }
421
2.15M
    return val;
422
2.15M
}
423
424
static int vp6_parse_coeff_huffman(VP56Context *s)
425
1.59M
{
426
1.59M
    VP56Model *model = s->modelp;
427
1.59M
    uint8_t *permute = s->idct_scantable;
428
1.59M
    VLC *vlc_coeff;
429
1.59M
    int sign, coeff_idx;
430
1.59M
    int b, cg, idx;
431
1.59M
    int pt = 0;    /* plane type (0 for Y, 1 for U or V) */
432
433
11.1M
    for (b=0; b<6; b++) {
434
9.57M
        int ct = 0;    /* code type */
435
9.57M
        if (b > 3) pt = 1;
436
9.57M
        vlc_coeff = &s->dccv_vlc[pt];
437
438
48.8M
        for (coeff_idx = 0;;) {
439
48.8M
            int run = 1;
440
48.8M
            if (coeff_idx<2 && s->nb_null[coeff_idx][pt]) {
441
6.05M
                s->nb_null[coeff_idx][pt]--;
442
6.05M
                if (coeff_idx)
443
3.29M
                    break;
444
42.7M
            } else {
445
42.7M
                if (get_bits_left(&s->gb) <= 0)
446
5.70k
                    return AVERROR_INVALIDDATA;
447
42.7M
                int coeff = get_vlc2(&s->gb, vlc_coeff->table, AC_DC_HUFF_BITS, 2);
448
42.7M
                if (coeff == 0) {
449
21.7M
                    if (coeff_idx) {
450
20.2M
                        int pt = (coeff_idx >= 6);
451
20.2M
                        run += get_vlc2(&s->gb, s->runv_vlc[pt].table, RUN_HUFF_BITS, 1);
452
20.2M
                        if (run >= 9)
453
390k
                            run += get_bits(&s->gb, 6);
454
20.2M
                    } else
455
1.53M
                        s->nb_null[0][pt] = vp6_get_nb_null(s);
456
21.7M
                    ct = 0;
457
21.7M
                } else if (coeff == 11) {  /* end of block */
458
4.90M
                    if (coeff_idx == 1)    /* first AC coeff ? */
459
625k
                        s->nb_null[1][pt] = vp6_get_nb_null(s);
460
4.90M
                    break;
461
16.1M
                } else {
462
16.1M
                    int coeff2 = ff_vp56_coeff_bias[coeff];
463
16.1M
                    if (coeff > 4)
464
6.16M
                        coeff2 += get_bits(&s->gb, coeff <= 9 ? coeff - 4 : 11);
465
16.1M
                    ct = 1 + (coeff2 > 1);
466
16.1M
                    sign = get_bits1(&s->gb);
467
16.1M
                    coeff2 = (coeff2 ^ -sign) + sign;
468
16.1M
                    if (coeff_idx)
469
13.2M
                        coeff2 *= s->dequant_ac;
470
16.1M
                    idx = model->coeff_index_to_pos[coeff_idx];
471
16.1M
                    s->block_coeff[b][permute[idx]] = coeff2;
472
16.1M
                }
473
42.7M
            }
474
40.6M
            coeff_idx+=run;
475
40.6M
            if (coeff_idx >= 64)
476
1.36M
                break;
477
39.2M
            cg = FFMIN(vp6_coeff_groups[coeff_idx], 3);
478
39.2M
            vlc_coeff = &s->ract_vlc[pt][ct][cg];
479
39.2M
        }
480
9.57M
        s->idct_selector[b] = model->coeff_index_to_idct_selector[FFMIN(coeff_idx, 63)];
481
9.57M
    }
482
1.59M
    return 0;
483
1.59M
}
484
485
static int vp6_parse_coeff(VP56Context *s)
486
6.29M
{
487
6.29M
    VPXRangeCoder *c = s->ccp;
488
6.29M
    VP56Model *model = s->modelp;
489
6.29M
    uint8_t *permute = s->idct_scantable;
490
6.29M
    uint8_t *model1, *model2, *model3;
491
6.29M
    int coeff, sign, coeff_idx;
492
6.29M
    int b, i, cg, idx, ctx;
493
6.29M
    int pt = 0;    /* plane type (0 for Y, 1 for U or V) */
494
495
6.29M
    if (vpx_rac_is_end(c)) {
496
17.5k
        av_log(s->avctx, AV_LOG_ERROR, "End of AC stream reached in vp6_parse_coeff\n");
497
17.5k
        return AVERROR_INVALIDDATA;
498
17.5k
    }
499
500
43.9M
    for (b=0; b<6; b++) {
501
37.6M
        int ct = 1;    /* code type */
502
37.6M
        int run = 1;
503
504
37.6M
        if (b > 3) pt = 1;
505
506
37.6M
        ctx = s->left_block[ff_vp56_b6to4[b]].not_null_dc
507
37.6M
              + s->above_blocks[s->above_block_idx[b]].not_null_dc;
508
37.6M
        model1 = model->coeff_dccv[pt];
509
37.6M
        model2 = model->coeff_dcct[pt][ctx];
510
511
37.6M
        coeff_idx = 0;
512
159M
        for (;;) {
513
159M
            if ((coeff_idx>1 && ct==0) || vpx_rac_get_prob_branchy(c, model2[0])) {
514
                /* parse a coeff */
515
87.0M
                if (vpx_rac_get_prob_branchy(c, model2[2])) {
516
86.0M
                    if (vpx_rac_get_prob_branchy(c, model2[3])) {
517
85.6M
                        idx = vp56_rac_get_tree(c, ff_vp56_pc_tree, model1);
518
85.6M
                        coeff = ff_vp56_coeff_bias[idx+5];
519
1.02G
                        for (i=ff_vp56_coeff_bit_length[idx]; i>=0; i--)
520
936M
                            coeff += vpx_rac_get_prob(c, ff_vp56_coeff_parse_table[idx][i]) << i;
521
85.6M
                    } else {
522
409k
                        if (vpx_rac_get_prob_branchy(c, model2[4]))
523
173k
                            coeff = 3 + vpx_rac_get_prob(c, model1[5]);
524
236k
                        else
525
236k
                            coeff = 2;
526
409k
                    }
527
86.0M
                    ct = 2;
528
86.0M
                } else {
529
1.01M
                    ct = 1;
530
1.01M
                    coeff = 1;
531
1.01M
                }
532
87.0M
                sign = vpx_rac_get(c);
533
87.0M
                coeff = (coeff ^ -sign) + sign;
534
87.0M
                if (coeff_idx)
535
85.4M
                    coeff *= s->dequant_ac;
536
87.0M
                idx = model->coeff_index_to_pos[coeff_idx];
537
87.0M
                s->block_coeff[b][permute[idx]] = coeff;
538
87.0M
                run = 1;
539
87.0M
            } else {
540
                /* parse a run */
541
72.7M
                ct = 0;
542
72.7M
                if (coeff_idx > 0) {
543
36.6M
                    if (!vpx_rac_get_prob_branchy(c, model2[1]))
544
36.3M
                        break;
545
546
386k
                    model3 = model->coeff_runv[coeff_idx >= 6];
547
386k
                    run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
548
386k
                    if (!run)
549
498k
                        for (run=9, i=0; i<6; i++)
550
427k
                            run += vpx_rac_get_prob(c, model3[i+8]) << i;
551
386k
                }
552
72.7M
            }
553
123M
            coeff_idx += run;
554
123M
            if (coeff_idx >= 64)
555
1.35M
                break;
556
122M
            cg = vp6_coeff_groups[coeff_idx];
557
122M
            model1 = model2 = model->coeff_ract[pt][ct][cg];
558
122M
        }
559
560
37.6M
        s->left_block[ff_vp56_b6to4[b]].not_null_dc =
561
37.6M
        s->above_blocks[s->above_block_idx[b]].not_null_dc = !!s->block_coeff[b][0];
562
37.6M
        s->idct_selector[b] = model->coeff_index_to_idct_selector[FFMIN(coeff_idx, 63)];
563
37.6M
    }
564
6.27M
    return 0;
565
6.29M
}
566
567
static int vp6_block_variance(uint8_t *src, ptrdiff_t stride)
568
316k
{
569
316k
    int sum = 0, square_sum = 0;
570
316k
    int y, x;
571
572
1.58M
    for (y=0; y<8; y+=2) {
573
6.33M
        for (x=0; x<8; x+=2) {
574
5.06M
            sum += src[x];
575
5.06M
            square_sum += src[x]*src[x];
576
5.06M
        }
577
1.26M
        src += 2*stride;
578
1.26M
    }
579
316k
    return (16*square_sum - sum*sum) >> 8;
580
316k
}
581
582
static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, ptrdiff_t stride,
583
                           int delta, const int16_t *weights)
584
112k
{
585
112k
    int x, y;
586
587
1.00M
    for (y=0; y<8; y++) {
588
8.07M
        for (x=0; x<8; x++) {
589
7.17M
            dst[x] = av_clip_uint8((  src[x-delta  ] * weights[0]
590
7.17M
                                 + src[x        ] * weights[1]
591
7.17M
                                 + src[x+delta  ] * weights[2]
592
7.17M
                                 + src[x+2*delta] * weights[3] + 64) >> 7);
593
7.17M
        }
594
896k
        src += stride;
595
896k
        dst += stride;
596
896k
    }
597
112k
}
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.58M
{
602
2.58M
    uint8_t *tmp = s->edge_emu_buffer+16;
603
2.58M
    s->h264chroma.put_h264_chroma_pixels_tab[0](tmp, src, stride, 9, h_weight, 0);
604
2.58M
    s->h264chroma.put_h264_chroma_pixels_tab[0](dst, tmp, stride, 8, 0, v_weight);
605
2.58M
}
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.72M
{
611
4.72M
    int filter4 = 0;
612
4.72M
    int x8 = mv.x & mask;
613
4.72M
    int y8 = mv.y & mask;
614
615
4.72M
    if (luma) {
616
3.43M
        x8 *= 2;
617
3.43M
        y8 *= 2;
618
3.43M
        filter4 = s->filter_mode;
619
3.43M
        if (filter4 == 2) {
620
2.48M
            if (s->max_vector_length &&
621
2.48M
                (FFABS(mv.x) > s->max_vector_length ||
622
2.48M
                 FFABS(mv.y) > s->max_vector_length)) {
623
2.16M
                filter4 = 0;
624
2.16M
            } else if (s->sample_variance_threshold
625
326k
                       && (vp6_block_variance(src+offset1, stride)
626
316k
                           < s->sample_variance_threshold)) {
627
293k
                filter4 = 0;
628
293k
            }
629
2.48M
        }
630
3.43M
    }
631
632
4.72M
    if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
633
3.02M
        offset1 = offset2;
634
3.02M
    }
635
636
4.72M
    if (filter4) {
637
236k
        if (!y8) {                      /* left or right combine */
638
56.1k
            vp6_filter_hv4(dst, src+offset1, stride, 1,
639
56.1k
                           vp6_block_copy_filter[select][x8]);
640
180k
        } else if (!x8) {               /* above or below combine */
641
55.9k
            vp6_filter_hv4(dst, src+offset1, stride, stride,
642
55.9k
                           vp6_block_copy_filter[select][y8]);
643
124k
        } else {
644
124k
            s->vp56dsp.vp6_filter_diag4(dst, src+offset1+((mv.x^mv.y)>>31), stride,
645
124k
                             vp6_block_copy_filter[select][x8],
646
124k
                             vp6_block_copy_filter[select][y8]);
647
124k
        }
648
4.48M
    } else {
649
4.48M
        if (!x8 || !y8) {
650
1.89M
            s->h264chroma.put_h264_chroma_pixels_tab[0](dst, src + offset1, stride, 8, x8, y8);
651
2.58M
        } else {
652
2.58M
            vp6_filter_diag2(s, dst, src+offset1 + ((mv.x^mv.y)>>31), stride, x8, y8);
653
2.58M
        }
654
4.48M
    }
655
4.72M
}
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->vp56dsp);
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.44k
        s->alpha_context = &s[1];
691
4.44k
        ret = vp6_decode_init_context(avctx, s->alpha_context,
692
4.44k
                                      s->flip == -1, s->has_alpha);
693
4.44k
        if (ret < 0)
694
0
            return ret;
695
4.44k
    }
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.44k
        vp6_decode_free_context(s->alpha_context);
710
4.44k
        s->alpha_context = NULL;
711
4.44k
    }
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.1k
    for (int pt = 0; pt < 2; ++pt) {
721
34.1k
        ff_vlc_free(&s->dccv_vlc[pt]);
722
34.1k
        ff_vlc_free(&s->runv_vlc[pt]);
723
136k
        for (int ct = 0; ct < 3; ++ct)
724
511k
            for (int cg = 0; cg < 4; ++cg)
725
409k
                ff_vlc_free(&s->ract_vlc[pt][ct][cg]);
726
34.1k
    }
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
};