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