Coverage Report

Created: 2024-09-06 07:53

/src/ffmpeg/libavcodec/vp5.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
 * VP5 compatible video decoder
24
 */
25
26
#include <string.h>
27
28
#include "avcodec.h"
29
#include "codec_internal.h"
30
#include "decode.h"
31
32
#include "vp56.h"
33
#include "vp56data.h"
34
#include "vp5data.h"
35
#include "vpx_rac.h"
36
37
38
static int vp5_parse_header(VP56Context *s, const uint8_t *buf, int buf_size)
39
106k
{
40
106k
    VPXRangeCoder *c = &s->c;
41
106k
    int rows, cols;
42
106k
    int ret;
43
44
106k
    ret = ff_vpx_init_range_decoder(&s->c, buf, buf_size);
45
106k
    if (ret < 0)
46
0
        return ret;
47
106k
    if (!vpx_rac_get(c))
48
13.6k
        s->frames[VP56_FRAME_CURRENT]->flags |= AV_FRAME_FLAG_KEY;
49
92.7k
    else
50
92.7k
        s->frames[VP56_FRAME_CURRENT]->flags &= ~AV_FRAME_FLAG_KEY;
51
106k
    vpx_rac_get(c);
52
106k
    ff_vp56_init_dequant(s, vp56_rac_gets(c, 6));
53
106k
    if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY)
54
13.6k
    {
55
13.6k
        int render_x, render_y;
56
57
13.6k
        vp56_rac_gets(c, 8);
58
13.6k
        if(vp56_rac_gets(c, 5) > 5)
59
1.14k
            return AVERROR_INVALIDDATA;
60
12.4k
        vp56_rac_gets(c, 2);
61
12.4k
        if (vpx_rac_get(c)) {
62
483
            avpriv_report_missing_feature(s->avctx, "Interlacing");
63
483
            return AVERROR_PATCHWELCOME;
64
483
        }
65
11.9k
        rows = vp56_rac_gets(c, 8);  /* number of stored macroblock rows */
66
11.9k
        cols = vp56_rac_gets(c, 8);  /* number of stored macroblock cols */
67
11.9k
        if (!rows || !cols) {
68
3.68k
            av_log(s->avctx, AV_LOG_ERROR, "Invalid size %dx%d\n",
69
3.68k
                   cols << 4, rows << 4);
70
3.68k
            return AVERROR_INVALIDDATA;
71
3.68k
        }
72
8.30k
        render_y = vp56_rac_gets(c, 8);  /* number of displayed macroblock rows */
73
8.30k
        render_x = vp56_rac_gets(c, 8);  /* number of displayed macroblock cols */
74
8.30k
        if (render_x == 0 || render_x > cols ||
75
8.30k
            render_y == 0 || render_y > rows)
76
2.00k
            return AVERROR_INVALIDDATA;
77
6.30k
        vp56_rac_gets(c, 2);
78
6.30k
        if (!s->macroblocks || /* first frame */
79
6.30k
            16*cols != s->avctx->coded_width ||
80
6.30k
            16*rows != s->avctx->coded_height) {
81
5.23k
            int ret = ff_set_dimensions(s->avctx, 16 * cols, 16 * rows);
82
5.23k
            if (ret < 0)
83
0
                return ret;
84
5.23k
            return VP56_SIZE_CHANGE;
85
5.23k
        }
86
92.7k
    } else if (!s->macroblocks)
87
2.05k
        return AVERROR_INVALIDDATA;
88
91.7k
    return 0;
89
106k
}
90
91
static void vp5_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
92
13.2k
{
93
13.2k
    VPXRangeCoder *c = &s->c;
94
13.2k
    VP56Model *model = s->modelp;
95
13.2k
    int comp, di;
96
97
39.8k
    for (comp=0; comp<2; comp++) {
98
26.5k
        int delta = 0;
99
26.5k
        if (vpx_rac_get_prob_branchy(c, model->vector_dct[comp])) {
100
10.9k
            int sign = vpx_rac_get_prob(c, model->vector_sig[comp]);
101
10.9k
            di  = vpx_rac_get_prob(c, model->vector_pdi[comp][0]);
102
10.9k
            di |= vpx_rac_get_prob(c, model->vector_pdi[comp][1]) << 1;
103
10.9k
            delta = vp56_rac_get_tree(c, ff_vp56_pva_tree,
104
10.9k
                                      model->vector_pdv[comp]);
105
10.9k
            delta = di | (delta << 2);
106
10.9k
            delta = (delta ^ -sign) + sign;
107
10.9k
        }
108
26.5k
        if (!comp)
109
13.2k
            vect->x = delta;
110
13.2k
        else
111
13.2k
            vect->y = delta;
112
26.5k
    }
113
13.2k
}
114
115
static void vp5_parse_vector_models(VP56Context *s)
116
89.0k
{
117
89.0k
    VPXRangeCoder *c = &s->c;
118
89.0k
    VP56Model *model = s->modelp;
119
89.0k
    int comp, node;
120
121
267k
    for (comp=0; comp<2; comp++) {
122
178k
        if (vpx_rac_get_prob_branchy(c, vp5_vmc_pct[comp][0]))
123
160k
            model->vector_dct[comp] = vp56_rac_gets_nn(c, 7);
124
178k
        if (vpx_rac_get_prob_branchy(c, vp5_vmc_pct[comp][1]))
125
160k
            model->vector_sig[comp] = vp56_rac_gets_nn(c, 7);
126
178k
        if (vpx_rac_get_prob_branchy(c, vp5_vmc_pct[comp][2]))
127
159k
            model->vector_pdi[comp][0] = vp56_rac_gets_nn(c, 7);
128
178k
        if (vpx_rac_get_prob_branchy(c, vp5_vmc_pct[comp][3]))
129
159k
            model->vector_pdi[comp][1] = vp56_rac_gets_nn(c, 7);
130
178k
    }
131
132
267k
    for (comp=0; comp<2; comp++)
133
1.42M
        for (node=0; node<7; node++)
134
1.24M
            if (vpx_rac_get_prob_branchy(c, vp5_vmc_pct[comp][4 + node]))
135
1.11M
                model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
136
89.0k
}
137
138
static int vp5_parse_coeff_models(VP56Context *s)
139
95.3k
{
140
95.3k
    VPXRangeCoder *c = &s->c;
141
95.3k
    VP56Model *model = s->modelp;
142
95.3k
    uint8_t def_prob[11];
143
95.3k
    int node, cg, ctx;
144
95.3k
    int ct;    /* code type */
145
95.3k
    int pt;    /* plane type (0 for Y, 1 for U or V) */
146
147
95.3k
    memset(def_prob, 0x80, sizeof(def_prob));
148
149
286k
    for (pt=0; pt<2; pt++)
150
2.28M
        for (node=0; node<11; node++)
151
2.09M
            if (vpx_rac_get_prob_branchy(c, vp5_dccv_pct[pt][node])) {
152
1.76M
                def_prob[node] = vp56_rac_gets_nn(c, 7);
153
1.76M
                model->coeff_dccv[pt][node] = def_prob[node];
154
1.76M
            } else if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
155
133k
                model->coeff_dccv[pt][node] = def_prob[node];
156
133k
            }
157
158
381k
    for (ct=0; ct<3; ct++)
159
858k
        for (pt=0; pt<2; pt++)
160
4.00M
            for (cg=0; cg<6; cg++)
161
41.1M
                for (node=0; node<11; node++)
162
37.7M
                    if (vpx_rac_get_prob_branchy(c, vp5_ract_pct[ct][pt][cg][node])) {
163
31.4M
                        def_prob[node] = vp56_rac_gets_nn(c, 7);
164
31.4M
                        model->coeff_ract[pt][ct][cg][node] = def_prob[node];
165
31.4M
                    } else if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
166
2.47M
                        model->coeff_ract[pt][ct][cg][node] = def_prob[node];
167
2.47M
                    }
168
169
    /* coeff_dcct is a linear combination of coeff_dccv */
170
286k
    for (pt=0; pt<2; pt++)
171
7.05M
        for (ctx=0; ctx<36; ctx++)
172
41.1M
            for (node=0; node<5; node++)
173
34.3M
                model->coeff_dcct[pt][ctx][node] = av_clip(((model->coeff_dccv[pt][node] * vp5_dccv_lc[node][ctx][0] + 128) >> 8) + vp5_dccv_lc[node][ctx][1], 1, 254);
174
175
    /* coeff_acct is a linear combination of coeff_ract */
176
381k
    for (ct=0; ct<3; ct++)
177
858k
        for (pt=0; pt<2; pt++)
178
2.28M
            for (cg=0; cg<3; cg++)
179
12.0M
                for (ctx=0; ctx<6; ctx++)
180
61.7M
                    for (node=0; node<5; node++)
181
51.4M
                        model->coeff_acct[pt][ct][cg][ctx][node] = av_clip(((model->coeff_ract[pt][ct][cg][node] * vp5_ract_lc[ct][cg][node][ctx][0] + 128) >> 8) + vp5_ract_lc[ct][cg][node][ctx][1], 1, 254);
182
95.3k
    return 0;
183
95.3k
}
184
185
static int vp5_parse_coeff(VP56Context *s)
186
1.07M
{
187
1.07M
    VPXRangeCoder *c = &s->c;
188
1.07M
    VP56Model *model = s->modelp;
189
1.07M
    uint8_t *permute = s->idct_scantable;
190
1.07M
    uint8_t *model1, *model2;
191
1.07M
    int coeff, sign, coeff_idx;
192
1.07M
    int b, i, cg, idx, ctx, ctx_last;
193
1.07M
    int pt = 0;    /* plane type (0 for Y, 1 for U or V) */
194
195
1.07M
    if (vpx_rac_is_end(c)) {
196
4.15k
        av_log(s->avctx, AV_LOG_ERROR, "End of AC stream reached in vp5_parse_coeff\n");
197
4.15k
        return AVERROR_INVALIDDATA;
198
4.15k
    }
199
200
7.47M
    for (b=0; b<6; b++) {
201
6.40M
        int ct = 1;    /* code type */
202
203
6.40M
        if (b > 3) pt = 1;
204
205
6.40M
        ctx = 6*s->coeff_ctx[ff_vp56_b6to4[b]][0]
206
6.40M
              + s->above_blocks[s->above_block_idx[b]].not_null_dc;
207
6.40M
        model1 = model->coeff_dccv[pt];
208
6.40M
        model2 = model->coeff_dcct[pt][ctx];
209
210
6.40M
        coeff_idx = 0;
211
41.2M
        for (;;) {
212
41.2M
            if (vpx_rac_get_prob_branchy(c, model2[0])) {
213
33.5M
                if (vpx_rac_get_prob_branchy(c, model2[2])) {
214
32.7M
                    if (vpx_rac_get_prob_branchy(c, model2[3])) {
215
32.4M
                        s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 4;
216
32.4M
                        idx = vp56_rac_get_tree(c, ff_vp56_pc_tree, model1);
217
32.4M
                        sign = vpx_rac_get(c);
218
32.4M
                        coeff = ff_vp56_coeff_bias[idx+5];
219
388M
                        for (i=ff_vp56_coeff_bit_length[idx]; i>=0; i--)
220
355M
                            coeff += vpx_rac_get_prob(c, ff_vp56_coeff_parse_table[idx][i]) << i;
221
32.4M
                    } else {
222
294k
                        if (vpx_rac_get_prob_branchy(c, model2[4])) {
223
126k
                            coeff = 3 + vpx_rac_get_prob(c, model1[5]);
224
126k
                            s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 3;
225
168k
                        } else {
226
168k
                            coeff = 2;
227
168k
                            s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 2;
228
168k
                        }
229
294k
                        sign = vpx_rac_get(c);
230
294k
                    }
231
32.7M
                    ct = 2;
232
32.7M
                } else {
233
755k
                    ct = 1;
234
755k
                    s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 1;
235
755k
                    sign = vpx_rac_get(c);
236
755k
                    coeff = 1;
237
755k
                }
238
33.5M
                coeff = (coeff ^ -sign) + sign;
239
33.5M
                if (coeff_idx)
240
32.8M
                    coeff *= s->dequant_ac;
241
33.5M
                s->block_coeff[b][permute[coeff_idx]] = coeff;
242
33.5M
            } else {
243
7.71M
                if (ct && !vpx_rac_get_prob_branchy(c, model2[1]))
244
5.88M
                    break;
245
1.83M
                ct = 0;
246
1.83M
                s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 0;
247
1.83M
            }
248
35.3M
            coeff_idx++;
249
35.3M
            if (coeff_idx >= 64)
250
525k
                break;
251
252
34.8M
            cg = vp5_coeff_groups[coeff_idx];
253
34.8M
            ctx = s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx];
254
34.8M
            model1 = model->coeff_ract[pt][ct][cg];
255
34.8M
            model2 = cg > 2 ? model1 : model->coeff_acct[pt][ct][cg][ctx];
256
34.8M
        }
257
258
6.40M
        ctx_last = FFMIN(s->coeff_ctx_last[ff_vp56_b6to4[b]], 24);
259
6.40M
        s->coeff_ctx_last[ff_vp56_b6to4[b]] = coeff_idx;
260
6.40M
        if (coeff_idx < ctx_last)
261
6.72M
            for (i=coeff_idx; i<=ctx_last; i++)
262
6.38M
                s->coeff_ctx[ff_vp56_b6to4[b]][i] = 5;
263
6.40M
        s->above_blocks[s->above_block_idx[b]].not_null_dc = s->coeff_ctx[ff_vp56_b6to4[b]][0];
264
6.40M
        s->idct_selector[b] = 63;
265
6.40M
    }
266
1.06M
    return 0;
267
1.07M
}
268
269
static void vp5_default_models_init(VP56Context *s)
270
6.26k
{
271
6.26k
    VP56Model *model = s->modelp;
272
6.26k
    int i;
273
274
18.7k
    for (i=0; i<2; i++) {
275
12.5k
        model->vector_sig[i] = 0x80;
276
12.5k
        model->vector_dct[i] = 0x80;
277
12.5k
        model->vector_pdi[i][0] = 0x55;
278
12.5k
        model->vector_pdi[i][1] = 0x80;
279
12.5k
    }
280
6.26k
    memcpy(model->mb_types_stats, ff_vp56_def_mb_types_stats, sizeof(model->mb_types_stats));
281
6.26k
    memset(model->vector_pdv, 0x80, sizeof(model->vector_pdv));
282
6.26k
}
283
284
static av_cold int vp5_decode_init(AVCodecContext *avctx)
285
3.23k
{
286
3.23k
    VP56Context *s = avctx->priv_data;
287
3.23k
    int ret;
288
289
3.23k
    if ((ret = ff_vp56_init_context(avctx, s, 1, 0)) < 0)
290
0
        return ret;
291
3.23k
    ff_vp5dsp_init(&s->vp56dsp);
292
3.23k
    s->vp56_coord_div = vp5_coord_div;
293
3.23k
    s->parse_vector_adjustment = vp5_parse_vector_adjustment;
294
3.23k
    s->parse_coeff = vp5_parse_coeff;
295
3.23k
    s->default_models_init = vp5_default_models_init;
296
3.23k
    s->parse_vector_models = vp5_parse_vector_models;
297
3.23k
    s->parse_coeff_models = vp5_parse_coeff_models;
298
3.23k
    s->parse_header = vp5_parse_header;
299
300
3.23k
    return 0;
301
3.23k
}
302
303
static av_cold int vp56_free(AVCodecContext *avctx)
304
3.23k
{
305
3.23k
    VP56Context *const s = avctx->priv_data;
306
3.23k
    return ff_vp56_free_context(s);
307
3.23k
}
308
309
const FFCodec ff_vp5_decoder = {
310
    .p.name         = "vp5",
311
    CODEC_LONG_NAME("On2 VP5"),
312
    .p.type         = AVMEDIA_TYPE_VIDEO,
313
    .p.id           = AV_CODEC_ID_VP5,
314
    .priv_data_size = sizeof(VP56Context),
315
    .init           = vp5_decode_init,
316
    .close          = vp56_free,
317
    FF_CODEC_DECODE_CB(ff_vp56_decode_frame),
318
    .p.capabilities = AV_CODEC_CAP_DR1,
319
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
320
};