Revision 5ce117c3

View differences:

Changelog
56 56
- MacIntel support
57 57
- AVISynth support
58 58
- VMware video decoder
59
- VP5 video decoder
60
- VP6 video decoder
59 61

  
60 62
version 0.4.9-pre1:
61 63

  
MAINTAINERS
164 164
  vcr1.c                                Michael Niedermayer
165 165
  vmnc.c                                Kostya Shishkov
166 166
  vp3*                                  Mike Melanson
167
  vp5                                   Aurelien Jacobs
168
  vp6                                   Aurelien Jacobs
167 169
  vqavideo.c                            Mike Melanson
168 170
  wmv2.c                                Michael Niedermayer
169 171
  wnv1.c                                Kostya Shishkov
doc/ffmpeg-doc.texi
778 778
@item Sorenson Video 1       @tab  X  @tab  X @tab fourcc: SVQ1
779 779
@item Sorenson Video 3       @tab     @tab  X @tab fourcc: SVQ3
780 780
@item On2 VP3                @tab     @tab  X @tab still experimental
781
@item On2 VP5                @tab     @tab  X @tab fourcc: VP50
782
@item On2 VP6                @tab     @tab  X @tab fourcc: VP62
781 783
@item Theora                 @tab     @tab  X @tab still experimental
782 784
@item Intel Indeo 3          @tab     @tab  X
783 785
@item FLV                    @tab  X  @tab  X @tab Sorenson H.263 used in Flash
libavcodec/Makefile
123 123
OBJS-$(CONFIG_VMNC_DECODER)            += vmnc.o
124 124
OBJS-$(CONFIG_VORBIS_DECODER)          += vorbis.o
125 125
OBJS-$(CONFIG_VP3_DECODER)             += vp3.o
126
OBJS-$(CONFIG_VP5_DECODER)             += vp5.o vp56.o vp56data.o
127
OBJS-$(CONFIG_VP6_DECODER)             += vp6.o vp56.o vp56data.o
126 128
OBJS-$(CONFIG_VQA_DECODER)             += vqavideo.o
127 129
OBJS-$(CONFIG_WMAV1_DECODER)           += wmadec.o
128 130
OBJS-$(CONFIG_WMAV2_DECODER)           += wmadec.o
libavcodec/allcodecs.c
371 371
#ifdef CONFIG_THEORA_DECODER
372 372
    register_avcodec(&theora_decoder);
373 373
#endif //CONFIG_THEORA_DECODER
374
#ifdef CONFIG_VP5_DECODER
375
    register_avcodec(&vp5_decoder);
376
#endif //CONFIG_VP5_DECODER
377
#ifdef CONFIG_VP6_DECODER
378
    register_avcodec(&vp6_decoder);
379
#endif //CONFIG_VP6_DECODER
380
#ifdef CONFIG_VP6F_DECODER
381
    register_avcodec(&vp6f_decoder);
382
#endif //CONFIG_VP6F_DECODER
374 383
#ifdef CONFIG_ASV1_DECODER
375 384
    register_avcodec(&asv1_decoder);
376 385
#endif //CONFIG_ASV1_DECODER
libavcodec/avcodec.h
17 17
#define AV_STRINGIFY(s)         AV_TOSTRING(s)
18 18
#define AV_TOSTRING(s) #s
19 19

  
20
#define LIBAVCODEC_VERSION_INT  ((51<<16)+(13<<8)+0)
21
#define LIBAVCODEC_VERSION      51.13.0
20
#define LIBAVCODEC_VERSION_INT  ((51<<16)+(14<<8)+0)
21
#define LIBAVCODEC_VERSION      51.14.0
22 22
#define LIBAVCODEC_BUILD        LIBAVCODEC_VERSION_INT
23 23

  
24 24
#define LIBAVCODEC_IDENT        "Lavc" AV_STRINGIFY(LIBAVCODEC_VERSION)
......
121 121
    CODEC_ID_CAVS,
122 122
    CODEC_ID_JPEG2000,
123 123
    CODEC_ID_VMNC,
124
    CODEC_ID_VP5,
125
    CODEC_ID_VP6,
126
    CODEC_ID_VP6F,
124 127

  
125 128
    /* various pcm "codecs" */
126 129
    CODEC_ID_PCM_S16LE= 0x10000,
......
2192 2195
extern AVCodec indeo3_decoder;
2193 2196
extern AVCodec vp3_decoder;
2194 2197
extern AVCodec theora_decoder;
2198
extern AVCodec vp5_decoder;
2199
extern AVCodec vp6_decoder;
2200
extern AVCodec vp6f_decoder;
2195 2201
extern AVCodec amr_nb_decoder;
2196 2202
extern AVCodec amr_nb_encoder;
2197 2203
extern AVCodec amr_wb_encoder;
libavcodec/vp5.c
1
/**
2
 * @file vp5.c
3
 * VP5 compatible video decoder
4
 *
5
 * Copyright (C) 2006  Aurelien Jacobs <aurel@gnuage.org>
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
 */
21

  
22
#include <stdlib.h>
23
#include <string.h>
24
#include <inttypes.h>
25

  
26
#include "avcodec.h"
27
#include "dsputil.h"
28
#include "bitstream.h"
29
#include "mpegvideo.h"
30

  
31
#include "vp56.h"
32
#include "vp56data.h"
33
#include "vp5data.h"
34

  
35

  
36
static int vp5_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size,
37
                            int *golden_frame)
38
{
39
    vp56_range_coder_t *c = &s->c;
40
    int rows, cols;
41

  
42
    vp56_init_range_decoder(&s->c, buf, buf_size);
43
    s->frames[VP56_FRAME_CURRENT].key_frame = !vp56_rac_get(c);
44
    vp56_rac_get(c);
45
    vp56_init_dequant(s, vp56_rac_gets(c, 6));
46
    if (s->frames[VP56_FRAME_CURRENT].key_frame)
47
    {
48
        vp56_rac_gets(c, 8);
49
        if(vp56_rac_gets(c, 5) > 5)
50
            return 0;
51
        vp56_rac_gets(c, 2);
52
        if (vp56_rac_get(c)) {
53
            av_log(s->avctx, AV_LOG_ERROR, "interlacing not supported\n");
54
            return 0;
55
        }
56
        rows = vp56_rac_gets(c, 8);  /* number of stored macroblock rows */
57
        cols = vp56_rac_gets(c, 8);  /* number of stored macroblock cols */
58
        vp56_rac_gets(c, 8);  /* number of displayed macroblock rows */
59
        vp56_rac_gets(c, 8);  /* number of displayed macroblock cols */
60
        vp56_rac_gets(c, 2);
61
        if (16*cols != s->avctx->coded_width ||
62
            16*rows != s->avctx->coded_height) {
63
            avcodec_set_dimensions(s->avctx, 16*cols, 16*rows);
64
            return 2;
65
        }
66
    }
67
    return 1;
68
}
69

  
70
/* Gives very similar result than the vp6 version except in a few cases */
71
static int vp5_adjust(int v, int t)
72
{
73
    int s2, s1 = v >> 31;
74
    v ^= s1;
75
    v -= s1;
76
    v *= v < 2*t;
77
    v -= t;
78
    s2 = v >> 31;
79
    v ^= s2;
80
    v -= s2;
81
    v = t - v;
82
    v += s1;
83
    v ^= s1;
84
    return v;
85
}
86

  
87
static void vp5_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vector)
88
{
89
    vp56_range_coder_t *c = &s->c;
90
    int comp, di;
91

  
92
    for (comp=0; comp<2; comp++) {
93
        int delta = 0;
94
        if (vp56_rac_get_prob(c, s->vector_model_dct[comp])) {
95
            int sign = vp56_rac_get_prob(c, s->vector_model_sig[comp]);
96
            di  = vp56_rac_get_prob(c, s->vector_model_pdi[comp][0]);
97
            di |= vp56_rac_get_prob(c, s->vector_model_pdi[comp][1]) << 1;
98
            delta = vp56_rac_get_tree(c, vp56_pva_tree,
99
                                      s->vector_model_pdv[comp]);
100
            delta = di | (delta << 2);
101
            delta = (delta ^ -sign) + sign;
102
        }
103
        if (!comp)
104
            vector->x = delta;
105
        else
106
            vector->y = delta;
107
    }
108
}
109

  
110
static void vp5_parse_vector_models(vp56_context_t *s)
111
{
112
    vp56_range_coder_t *c = &s->c;
113
    int comp, node;
114

  
115
    for (comp=0; comp<2; comp++) {
116
        if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][0]))
117
            s->vector_model_dct[comp] = vp56_rac_gets_nn(c, 7);
118
        if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][1]))
119
            s->vector_model_sig[comp] = vp56_rac_gets_nn(c, 7);
120
        if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][2]))
121
            s->vector_model_pdi[comp][0] = vp56_rac_gets_nn(c, 7);
122
        if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][3]))
123
            s->vector_model_pdi[comp][1] = vp56_rac_gets_nn(c, 7);
124
    }
125

  
126
    for (comp=0; comp<2; comp++)
127
        for (node=0; node<7; node++)
128
            if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][4 + node]))
129
                s->vector_model_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
130
}
131

  
132
static void vp5_parse_coeff_models(vp56_context_t *s)
133
{
134
    vp56_range_coder_t *c = &s->c;
135
    uint8_t def_prob[11];
136
    int node, cg, ctx;
137
    int ct;    /* code type */
138
    int pt;    /* plane type (0 for Y, 1 for U or V) */
139

  
140
    memset(def_prob, 0x80, sizeof(def_prob));
141

  
142
    for (pt=0; pt<2; pt++)
143
        for (node=0; node<11; node++)
144
            if (vp56_rac_get_prob(c, vp5_dccv_pct[pt][node])) {
145
                def_prob[node] = vp56_rac_gets_nn(c, 7);
146
                s->coeff_model_dccv[pt][node] = def_prob[node];
147
            } else if (s->frames[VP56_FRAME_CURRENT].key_frame) {
148
                s->coeff_model_dccv[pt][node] = def_prob[node];
149
            }
150

  
151
    for (ct=0; ct<3; ct++)
152
        for (pt=0; pt<2; pt++)
153
            for (cg=0; cg<6; cg++)
154
                for (node=0; node<11; node++)
155
                    if (vp56_rac_get_prob(c, vp5_ract_pct[ct][pt][cg][node])) {
156
                        def_prob[node] = vp56_rac_gets_nn(c, 7);
157
                        s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
158
                    } else if (s->frames[VP56_FRAME_CURRENT].key_frame) {
159
                        s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
160
                    }
161

  
162
    /* coeff_model_dcct is a linear combination of coeff_model_dccv */
163
    for (pt=0; pt<2; pt++)
164
        for (ctx=0; ctx<36; ctx++)
165
            for (node=0; node<5; node++)
166
                s->coeff_model_dcct[pt][ctx][node] = clip(((s->coeff_model_dccv[pt][node] * vp5_dccv_lc[node][ctx][0] + 128) >> 8) + vp5_dccv_lc[node][ctx][1], 1, 254);
167

  
168
    /* coeff_model_acct is a linear combination of coeff_model_ract */
169
    for (ct=0; ct<3; ct++)
170
        for (pt=0; pt<2; pt++)
171
            for (cg=0; cg<3; cg++)
172
                for (ctx=0; ctx<6; ctx++)
173
                    for (node=0; node<5; node++)
174
                        s->coeff_model_acct[pt][ct][cg][ctx][node] = clip(((s->coeff_model_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);
175
}
176

  
177
static void vp5_parse_coeff(vp56_context_t *s)
178
{
179
    vp56_range_coder_t *c = &s->c;
180
    uint8_t *permute = s->scantable.permutated;
181
    uint8_t *model, *model2;
182
    int coeff, sign, coeff_idx;
183
    int b, i, cg, idx, ctx, ctx_last;
184
    int pt = 0;    /* plane type (0 for Y, 1 for U or V) */
185

  
186
    for (b=0; b<6; b++) {
187
        int ct = 1;    /* code type */
188

  
189
        if (b > 3) pt = 1;
190

  
191
        ctx = 6*s->coeff_ctx[vp56_b6to4[b]][0]
192
              + s->above_blocks[s->above_block_idx[b]].not_null_dc;
193
        model = s->coeff_model_dccv[pt];
194
        model2 = s->coeff_model_dcct[pt][ctx];
195

  
196
        for (coeff_idx=0; coeff_idx<64; ) {
197
            if (vp56_rac_get_prob(c, model2[0])) {
198
                if (vp56_rac_get_prob(c, model2[2])) {
199
                    if (vp56_rac_get_prob(c, model2[3])) {
200
                        s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 4;
201
                        idx = vp56_rac_get_tree(c, vp56_pc_tree, model);
202
                        sign = vp56_rac_get(c);
203
                        coeff = vp56_coeff_bias[idx];
204
                        for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
205
                            coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
206
                    } else {
207
                        if (vp56_rac_get_prob(c, model2[4])) {
208
                            coeff = 3 + vp56_rac_get_prob(c, model[5]);
209
                            s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 3;
210
                        } else {
211
                            coeff = 2;
212
                            s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 2;
213
                        }
214
                        sign = vp56_rac_get(c);
215
                    }
216
                    ct = 2;
217
                } else {
218
                    ct = 1;
219
                    s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 1;
220
                    sign = vp56_rac_get(c);
221
                    coeff = 1;
222
                }
223
                coeff = (coeff ^ -sign) + sign;
224
                if (coeff_idx)
225
                    coeff *= s->dequant_ac;
226
                s->block_coeff[b][permute[coeff_idx]] = coeff;
227
            } else {
228
                if (ct && !vp56_rac_get_prob(c, model2[1]))
229
                    break;
230
                ct = 0;
231
                s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 0;
232
            }
233

  
234
            cg = vp5_coeff_groups[++coeff_idx];
235
            ctx = s->coeff_ctx[vp56_b6to4[b]][coeff_idx];
236
            model = s->coeff_model_ract[pt][ct][cg];
237
            model2 = cg > 2 ? model : s->coeff_model_acct[pt][ct][cg][ctx];
238
        }
239

  
240
        ctx_last = FFMIN(s->coeff_ctx_last[vp56_b6to4[b]], 24);
241
        s->coeff_ctx_last[vp56_b6to4[b]] = coeff_idx;
242
        if (coeff_idx < ctx_last)
243
            for (i=coeff_idx; i<=ctx_last; i++)
244
                s->coeff_ctx[vp56_b6to4[b]][i] = 5;
245
        s->above_blocks[s->above_block_idx[b]].not_null_dc = s->coeff_ctx[vp56_b6to4[b]][0];
246
    }
247
}
248

  
249
static void vp5_default_models_init(vp56_context_t *s)
250
{
251
    int i;
252

  
253
    for (i=0; i<2; i++) {
254
        s->vector_model_sig[i] = 0x80;
255
        s->vector_model_dct[i] = 0x80;
256
        s->vector_model_pdi[i][0] = 0x55;
257
        s->vector_model_pdi[i][1] = 0x80;
258
    }
259
    memcpy(s->mb_types_stats, vp56_def_mb_types_stats, sizeof(s->mb_types_stats));
260
    memset(s->vector_model_pdv, 0x80, sizeof(s->vector_model_pdv));
261
}
262

  
263
static int vp5_decode_init(AVCodecContext *avctx)
264
{
265
    vp56_context_t *s = avctx->priv_data;
266

  
267
    vp56_init(s, avctx, 1);
268
    s->vp56_coord_div = vp5_coord_div;
269
    s->parse_vector_adjustment = vp5_parse_vector_adjustment;
270
    s->adjust = vp5_adjust;
271
    s->parse_coeff = vp5_parse_coeff;
272
    s->default_models_init = vp5_default_models_init;
273
    s->parse_vector_models = vp5_parse_vector_models;
274
    s->parse_coeff_models = vp5_parse_coeff_models;
275
    s->parse_header = vp5_parse_header;
276

  
277
    return 0;
278
}
279

  
280
AVCodec vp5_decoder = {
281
    "vp5",
282
    CODEC_TYPE_VIDEO,
283
    CODEC_ID_VP5,
284
    sizeof(vp56_context_t),
285
    vp5_decode_init,
286
    NULL,
287
    vp56_free,
288
    vp56_decode_frame,
289
};
libavcodec/vp56.c
1
/**
2
 * @file vp56.c
3
 * VP5 and VP6 compatible video decoder (common features)
4
 *
5
 * Copyright (C) 2006  Aurelien Jacobs <aurel@gnuage.org>
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
 */
21

  
22
#include "avcodec.h"
23

  
24
#include "vp56.h"
25
#include "vp56data.h"
26

  
27

  
28
void vp56_init_dequant(vp56_context_t *s, int quantizer)
29
{
30
    s->quantizer = quantizer;
31
    s->dequant_dc = vp56_dc_dequant[quantizer] << 2;
32
    s->dequant_ac = vp56_ac_dequant[quantizer] << 2;
33
}
34

  
35
static int vp56_get_vectors_predictors(vp56_context_t *s, int row, int col,
36
                                       vp56_frame_t ref_frame)
37
{
38
    int nb_pred = 0;
39
    vp56_mv_t vector[2] = {{0,0}, {0,0}};
40
    int pos, offset;
41
    vp56_mv_t mvp;
42

  
43
    for (pos=0; pos<12; pos++) {
44
        mvp.x = col + vp56_candidate_predictor_pos[pos][0];
45
        mvp.y = row + vp56_candidate_predictor_pos[pos][1];
46
        if (mvp.x < 0 || mvp.x >= s->mb_width ||
47
            mvp.y < 0 || mvp.y >= s->mb_height)
48
            continue;
49
        offset = mvp.x + s->mb_width*mvp.y;
50

  
51
        if (vp56_reference_frame[s->macroblocks[offset].type] != ref_frame)
52
            continue;
53
        if ((s->macroblocks[offset].mv.x == vector[0].x &&
54
             s->macroblocks[offset].mv.y == vector[0].y) ||
55
            (s->macroblocks[offset].mv.x == 0 &&
56
             s->macroblocks[offset].mv.y == 0))
57
            continue;
58

  
59
        vector[nb_pred++] = s->macroblocks[offset].mv;
60
        if (nb_pred > 1) {
61
            nb_pred = -1;
62
            break;
63
        }
64
        s->vector_candidate_pos = pos;
65
    }
66

  
67
    s->vector_candidate[0] = vector[0];
68
    s->vector_candidate[1] = vector[1];
69

  
70
    return nb_pred+1;
71
}
72

  
73
static void vp56_parse_mb_type_models(vp56_context_t *s)
74
{
75
    vp56_range_coder_t *c = &s->c;
76
    int i, ctx, type;
77

  
78
    for (ctx=0; ctx<3; ctx++) {
79
        if (vp56_rac_get_prob(c, 174)) {
80
            int idx = vp56_rac_gets(c, 4);
81
            memcpy(s->mb_types_stats[ctx],vp56_pre_def_mb_type_stats[idx][ctx],
82
                   sizeof(s->mb_types_stats[ctx]));
83
        }
84
        if (vp56_rac_get_prob(c, 254)) {
85
            for (type=0; type<10; type++) {
86
                for(i=0; i<2; i++) {
87
                    if (vp56_rac_get_prob(c, 205)) {
88
                        int delta, sign = vp56_rac_get(c);
89

  
90
                        delta = vp56_rac_get_tree(c, vp56_pmbtm_tree,
91
                                                  vp56_mb_type_model_model);
92
                        if (!delta)
93
                            delta = 4 * vp56_rac_gets(c, 7);
94
                        s->mb_types_stats[ctx][type][i] += (delta ^ -sign) + sign;
95
                    }
96
                }
97
            }
98
        }
99
    }
100

  
101
    /* compute MB type probability tables based on previous MB type */
102
    for (ctx=0; ctx<3; ctx++) {
103
        int p[10];
104

  
105
        for (type=0; type<10; type++)
106
            p[type] = 100 * s->mb_types_stats[ctx][type][1];
107

  
108
        for (type=0; type<10; type++) {
109
            int p02, p34, p0234, p17, p56, p89, p5689, p156789;
110

  
111
            /* conservative MB type probability */
112
            s->mb_type_model[ctx][type][0] = 255 - (255 * s->mb_types_stats[ctx][type][0]) / (1 + s->mb_types_stats[ctx][type][0] + s->mb_types_stats[ctx][type][1]);
113

  
114
            p[type] = 0;    /* same MB type => weight is null */
115

  
116
            /* binary tree parsing probabilities */
117
            p02 = p[0] + p[2];
118
            p34 = p[3] + p[4];
119
            p0234 = p02 + p34;
120
            p17 = p[1] + p[7];
121
            p56 = p[5] + p[6];
122
            p89 = p[8] + p[9];
123
            p5689 = p56 + p89;
124
            p156789 = p17 + p5689;
125

  
126
            s->mb_type_model[ctx][type][1] = 1 + 255 * p0234/(1+p0234+p156789);
127
            s->mb_type_model[ctx][type][2] = 1 + 255 * p02  / (1+p0234);
128
            s->mb_type_model[ctx][type][3] = 1 + 255 * p17  / (1+p156789);
129
            s->mb_type_model[ctx][type][4] = 1 + 255 * p[0] / (1+p02);
130
            s->mb_type_model[ctx][type][5] = 1 + 255 * p[3] / (1+p34);
131
            s->mb_type_model[ctx][type][6] = 1 + 255 * p[1] / (1+p17);
132
            s->mb_type_model[ctx][type][7] = 1 + 255 * p56  / (1+p5689);
133
            s->mb_type_model[ctx][type][8] = 1 + 255 * p[5] / (1+p56);
134
            s->mb_type_model[ctx][type][9] = 1 + 255 * p[8] / (1+p89);
135

  
136
            /* restore initial value */
137
            p[type] = 100 * s->mb_types_stats[ctx][type][1];
138
        }
139
    }
140
}
141

  
142
static vp56_mb_t vp56_parse_mb_type(vp56_context_t *s,
143
                                    vp56_mb_t prev_type, int ctx)
144
{
145
    uint8_t *mb_type_model = s->mb_type_model[ctx][prev_type];
146
    vp56_range_coder_t *c = &s->c;
147

  
148
    if (vp56_rac_get_prob(c, mb_type_model[0]))
149
        return prev_type;
150
    else
151
        return vp56_rac_get_tree(c, vp56_pmbt_tree, mb_type_model);
152
}
153

  
154
static void vp56_decode_4mv(vp56_context_t *s, int row, int col)
155
{
156
    vp56_mv_t mv = {0,0};
157
    int type[4];
158
    int b;
159

  
160
    /* parse each block type */
161
    for (b=0; b<4; b++) {
162
        type[b] = vp56_rac_gets(&s->c, 2);
163
        if (type[b])
164
            type[b]++;  /* only returns 0, 2, 3 or 4 (all INTER_PF) */
165
    }
166

  
167
    /* get vectors */
168
    for (b=0; b<4; b++) {
169
        switch (type[b]) {
170
            case VP56_MB_INTER_NOVEC_PF:
171
                s->mv[b] = (vp56_mv_t) {0,0};
172
                break;
173
            case VP56_MB_INTER_DELTA_PF:
174
                s->parse_vector_adjustment(s, &s->mv[b]);
175
                break;
176
            case VP56_MB_INTER_V1_PF:
177
                s->mv[b] = s->vector_candidate[0];
178
                break;
179
            case VP56_MB_INTER_V2_PF:
180
                s->mv[b] = s->vector_candidate[1];
181
                break;
182
        }
183
        mv.x += s->mv[b].x;
184
        mv.y += s->mv[b].y;
185
    }
186

  
187
    /* this is the one selected for the whole MB for prediction */
188
    s->macroblocks[row * s->mb_width + col].mv = s->mv[3];
189

  
190
    /* chroma vectors are average luma vectors */
191
    if (s->avctx->codec->id == CODEC_ID_VP5) {
192
        s->mv[4].x = s->mv[5].x = RSHIFT(mv.x,2);
193
        s->mv[4].y = s->mv[5].y = RSHIFT(mv.y,2);
194
    } else {
195
        s->mv[4] = s->mv[5] = (vp56_mv_t) {mv.x/4, mv.y/4};
196
    }
197
}
198

  
199
static vp56_mb_t vp56_decode_mv(vp56_context_t *s, int row, int col)
200
{
201
    vp56_mv_t *mv, vector = {0,0};
202
    int ctx, b;
203

  
204
    ctx = vp56_get_vectors_predictors(s, row, col, VP56_FRAME_PREVIOUS);
205
    s->mb_type = vp56_parse_mb_type(s, s->mb_type, ctx);
206
    s->macroblocks[row * s->mb_width + col].type = s->mb_type;
207

  
208
    switch (s->mb_type) {
209
        case VP56_MB_INTER_V1_PF:
210
            mv = &s->vector_candidate[0];
211
            break;
212

  
213
        case VP56_MB_INTER_V2_PF:
214
            mv = &s->vector_candidate[1];
215
            break;
216

  
217
        case VP56_MB_INTER_V1_GF:
218
            vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
219
            mv = &s->vector_candidate[0];
220
            break;
221

  
222
        case VP56_MB_INTER_V2_GF:
223
            vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
224
            mv = &s->vector_candidate[1];
225
            break;
226

  
227
        case VP56_MB_INTER_DELTA_PF:
228
            s->parse_vector_adjustment(s, &vector);
229
            mv = &vector;
230
            break;
231

  
232
        case VP56_MB_INTER_DELTA_GF:
233
            vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
234
            s->parse_vector_adjustment(s, &vector);
235
            mv = &vector;
236
            break;
237

  
238
        case VP56_MB_INTER_4V:
239
            vp56_decode_4mv(s, row, col);
240
            return s->mb_type;
241

  
242
        default:
243
            mv = &vector;
244
            break;
245
    }
246

  
247
    s->macroblocks[row*s->mb_width + col].mv = *mv;
248

  
249
    /* same vector for all blocks */
250
    for (b=0; b<6; b++)
251
        s->mv[b] = *mv;
252

  
253
    return s->mb_type;
254
}
255

  
256
static void vp56_add_predictors_dc(vp56_context_t *s, vp56_frame_t ref_frame)
257
{
258
    int idx = s->scantable.permutated[0];
259
    int i;
260

  
261
    for (i=0; i<6; i++) {
262
        vp56_ref_dc_t *ab = &s->above_blocks[s->above_block_idx[i]];
263
        vp56_ref_dc_t *lb = &s->left_block[vp56_b6to4[i]];
264
        int count = 0;
265
        int dc = 0;
266

  
267
        if (ref_frame == lb->ref_frame) {
268
            dc += lb->dc_coeff;
269
            count++;
270
        }
271
        if (ref_frame == ab->ref_frame) {
272
            dc += ab->dc_coeff;
273
            count++;
274
        }
275
        if (s->avctx->codec->id == CODEC_ID_VP5) {
276
            if (count < 2 && ref_frame == ab[-1].ref_frame) {
277
                dc += ab[-1].dc_coeff;
278
                count++;
279
            }
280
            if (count < 2 && ref_frame == ab[1].ref_frame) {
281
                dc += ab[1].dc_coeff;
282
                count++;
283
            }
284
        }
285
        if (count == 0)
286
            dc = s->prev_dc[vp56_b6to3[i]][ref_frame];
287
        else if (count == 2)
288
            dc /= 2;
289

  
290
        s->block_coeff[i][idx] += dc;
291
        s->prev_dc[vp56_b6to3[i]][ref_frame] = s->block_coeff[i][idx];
292
        ab->dc_coeff = s->block_coeff[i][idx];
293
        ab->ref_frame = ref_frame;
294
        lb->dc_coeff = s->block_coeff[i][idx];
295
        lb->ref_frame = ref_frame;
296
        s->block_coeff[i][idx] *= s->dequant_dc;
297
    }
298
}
299

  
300
static void vp56_edge_filter(vp56_context_t *s, uint8_t *yuv,
301
                             int pix_inc, int line_inc, int t)
302
{
303
    int pix2_inc = 2 * pix_inc;
304
    int i, v;
305

  
306
    for (i=0; i<12; i++) {
307
        v = (yuv[-pix2_inc] + 3*(yuv[0]-yuv[-pix_inc]) - yuv[pix_inc] + 4) >>3;
308
        v = s->adjust(v, t);
309
        yuv[-pix_inc] = clip_uint8(yuv[-pix_inc] + v);
310
        yuv[0] = clip_uint8(yuv[0] - v);
311
        yuv += line_inc;
312
    }
313
}
314

  
315
static void vp56_deblock_filter(vp56_context_t *s, uint8_t *yuv,
316
                                int stride, int dx, int dy)
317
{
318
    int t = vp56_filter_threshold[s->quantizer];
319
    if (dx)  vp56_edge_filter(s, yuv +         10-dx ,      1, stride, t);
320
    if (dy)  vp56_edge_filter(s, yuv + stride*(10-dy), stride,      1, t);
321
}
322

  
323
static void vp56_mc(vp56_context_t *s, int b, uint8_t *src,
324
                    int stride, int x, int y)
325
{
326
    int plane = vp56_b6to3[b];
327
    uint8_t *dst= s->frames[VP56_FRAME_CURRENT].data[plane]+s->block_offset[b];
328
    uint8_t *src_block;
329
    int src_offset;
330
    int overlap_offset = 0;
331
    int mask = s->vp56_coord_div[b] - 1;
332
    int deblock_filtering = s->deblock_filtering;
333
    int dx;
334
    int dy;
335

  
336
    if (s->avctx->skip_loop_filter >= AVDISCARD_ALL ||
337
        (s->avctx->skip_loop_filter >= AVDISCARD_NONKEY
338
         && !s->frames[VP56_FRAME_CURRENT].key_frame))
339
        deblock_filtering = 0;
340

  
341
    dx = s->mv[b].x / s->vp56_coord_div[b];
342
    dy = s->mv[b].y / s->vp56_coord_div[b];
343

  
344
    if (b >= 4) {
345
        x /= 2;
346
        y /= 2;
347
    }
348
    x += dx - 2;
349
    y += dy - 2;
350

  
351
    if (x<0 || x+12>=s->plane_width[plane] ||
352
        y<0 || y+12>=s->plane_height[plane]) {
353
        ff_emulated_edge_mc(s->edge_emu_buffer,
354
                            src + s->block_offset[b] + (dy-2)*stride + (dx-2),
355
                            stride, 12, 12, x, y,
356
                            s->plane_width[plane],
357
                            s->plane_height[plane]);
358
        src_block = s->edge_emu_buffer;
359
        src_offset = 2 + 2*stride;
360
    } else if (deblock_filtering) {
361
        /* only need a 12x12 block, but there is no such dsp function, */
362
        /* so copy a 16x12 block */
363
        s->dsp.put_pixels_tab[0][0](s->edge_emu_buffer,
364
                                    src + s->block_offset[b] + (dy-2)*stride + (dx-2),
365
                                    stride, 12);
366
        src_block = s->edge_emu_buffer;
367
        src_offset = 2 + 2*stride;
368
    } else {
369
        src_block = src;
370
        src_offset = s->block_offset[b] + dy*stride + dx;
371
    }
372

  
373
    if (deblock_filtering)
374
        vp56_deblock_filter(s, src_block, stride, dx&7, dy&7);
375

  
376
    if (s->mv[b].x & mask)
377
        overlap_offset += (s->mv[b].x > 0) ? 1 : -1;
378
    if (s->mv[b].y & mask)
379
        overlap_offset += (s->mv[b].y > 0) ? stride : -stride;
380

  
381
    if (overlap_offset) {
382
        if (s->filter)
383
            s->filter(s, dst, src_block, src_offset, src_offset+overlap_offset,
384
                      stride, s->mv[b], mask, s->filter_selection, b<4);
385
        else
386
            s->dsp.put_no_rnd_pixels_l2[1](dst, src_block+src_offset,
387
                                           src_block+src_offset+overlap_offset,
388
                                           stride, 8);
389
    } else {
390
        s->dsp.put_pixels_tab[1][0](dst, src_block+src_offset, stride, 8);
391
    }
392
}
393

  
394
static void vp56_decode_mb(vp56_context_t *s, int row, int col)
395
{
396
    AVFrame *frame_current, *frame_ref;
397
    vp56_mb_t mb_type;
398
    vp56_frame_t ref_frame;
399
    int b, plan, off;
400

  
401
    if (s->frames[VP56_FRAME_CURRENT].key_frame)
402
        mb_type = VP56_MB_INTRA;
403
    else
404
        mb_type = vp56_decode_mv(s, row, col);
405
    ref_frame = vp56_reference_frame[mb_type];
406

  
407
    memset(s->block_coeff, 0, sizeof(s->block_coeff));
408

  
409
    s->parse_coeff(s);
410

  
411
    vp56_add_predictors_dc(s, ref_frame);
412

  
413
    frame_current = &s->frames[VP56_FRAME_CURRENT];
414
    frame_ref = &s->frames[ref_frame];
415

  
416
    switch (mb_type) {
417
        case VP56_MB_INTRA:
418
            for (b=0; b<6; b++) {
419
                plan = vp56_b6to3[b];
420
                s->dsp.idct_put(frame_current->data[plan] + s->block_offset[b],
421
                                s->stride[plan], s->block_coeff[b]);
422
            }
423
            break;
424

  
425
        case VP56_MB_INTER_NOVEC_PF:
426
        case VP56_MB_INTER_NOVEC_GF:
427
            for (b=0; b<6; b++) {
428
                plan = vp56_b6to3[b];
429
                off = s->block_offset[b];
430
                s->dsp.put_pixels_tab[1][0](frame_current->data[plan] + off,
431
                                            frame_ref->data[plan] + off,
432
                                            s->stride[plan], 8);
433
                s->dsp.idct_add(frame_current->data[plan] + off,
434
                                s->stride[plan], s->block_coeff[b]);
435
            }
436
            break;
437

  
438
        case VP56_MB_INTER_DELTA_PF:
439
        case VP56_MB_INTER_V1_PF:
440
        case VP56_MB_INTER_V2_PF:
441
        case VP56_MB_INTER_DELTA_GF:
442
        case VP56_MB_INTER_4V:
443
        case VP56_MB_INTER_V1_GF:
444
        case VP56_MB_INTER_V2_GF:
445
            for (b=0; b<6; b++) {
446
                int x_off = b==1 || b==3 ? 8 : 0;
447
                int y_off = b==2 || b==3 ? 8 : 0;
448
                plan = vp56_b6to3[b];
449
                vp56_mc(s, b, frame_ref->data[plan], s->stride[plan],
450
                        16*col+x_off, 16*row+y_off);
451
                s->dsp.idct_add(frame_current->data[plan] + s->block_offset[b],
452
                                s->stride[plan], s->block_coeff[b]);
453
            }
454
            break;
455
    }
456
}
457

  
458
static int vp56_size_changed(AVCodecContext *avctx, vp56_context_t *s)
459
{
460
    int stride = s->frames[VP56_FRAME_CURRENT].linesize[0];
461
    int i;
462

  
463
    s->plane_width[0] = s->avctx->width;
464
    s->plane_width[1] = s->plane_width[2] = s->avctx->width/2;
465
    s->plane_height[0] = s->avctx->height;
466
    s->plane_height[1] = s->plane_height[2] = s->avctx->height/2;
467

  
468
    for (i=0; i<3; i++)
469
        s->stride[i] = s->flip * s->frames[VP56_FRAME_CURRENT].linesize[i];
470

  
471
    s->mb_width = (s->avctx->width+15) / 16;
472
    s->mb_height = (s->avctx->height+15) / 16;
473

  
474
    if (s->mb_width > 1000 || s->mb_height > 1000) {
475
        av_log(avctx, AV_LOG_ERROR, "picture too big\n");
476
        return -1;
477
    }
478

  
479
    s->above_blocks = av_realloc(s->above_blocks,
480
                                 (4*s->mb_width+6) * sizeof(*s->above_blocks));
481
    s->macroblocks = av_realloc(s->macroblocks,
482
                                s->mb_width*s->mb_height*sizeof(*s->macroblocks));
483
    s->edge_emu_buffer_alloc = av_realloc(s->edge_emu_buffer_alloc, 16*stride);
484
    s->edge_emu_buffer = s->edge_emu_buffer_alloc;
485
    if (s->flip < 0)
486
        s->edge_emu_buffer += 15 * stride;
487

  
488
    return 0;
489
}
490

  
491
int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
492
                      uint8_t *buf, int buf_size)
493
{
494
    vp56_context_t *s = avctx->priv_data;
495
    AVFrame *const p = &s->frames[VP56_FRAME_CURRENT];
496
    AVFrame *picture = data;
497
    int mb_row, mb_col, mb_row_flip, mb_offset = 0;
498
    int block, y, uv, stride_y, stride_uv;
499
    int golden_frame = 0;
500
    int res;
501

  
502
    res = s->parse_header(s, buf, buf_size, &golden_frame);
503
    if (!res)
504
        return -1;
505

  
506
    p->reference = 1;
507
    if (avctx->get_buffer(avctx, p) < 0) {
508
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
509
        return -1;
510
    }
511

  
512
    if (res == 2)
513
        if (vp56_size_changed(avctx, s)) {
514
            avctx->release_buffer(avctx, p);
515
            return -1;
516
        }
517

  
518
    if (p->key_frame) {
519
        p->pict_type = FF_I_TYPE;
520
        s->default_models_init(s);
521
        for (block=0; block<s->mb_height*s->mb_width; block++)
522
            s->macroblocks[block].type = VP56_MB_INTRA;
523
    } else {
524
        p->pict_type = FF_P_TYPE;
525
        vp56_parse_mb_type_models(s);
526
        s->parse_vector_models(s);
527
        s->mb_type = VP56_MB_INTER_NOVEC_PF;
528
    }
529

  
530
    s->parse_coeff_models(s);
531

  
532
    memset(s->prev_dc, 0, sizeof(s->prev_dc));
533
    s->prev_dc[1][VP56_FRAME_CURRENT] = 128;
534
    s->prev_dc[2][VP56_FRAME_CURRENT] = 128;
535

  
536
    for (block=0; block < 4*s->mb_width+6; block++) {
537
        s->above_blocks[block].ref_frame = -1;
538
        s->above_blocks[block].dc_coeff = 0;
539
        s->above_blocks[block].not_null_dc = 0;
540
    }
541
    s->above_blocks[2*s->mb_width + 2].ref_frame = 0;
542
    s->above_blocks[3*s->mb_width + 4].ref_frame = 0;
543

  
544
    stride_y  = p->linesize[0];
545
    stride_uv = p->linesize[1];
546

  
547
    if (s->flip < 0)
548
        mb_offset = 7;
549

  
550
    /* main macroblocks loop */
551
    for (mb_row=0; mb_row<s->mb_height; mb_row++) {
552
        if (s->flip < 0)
553
            mb_row_flip = s->mb_height - mb_row - 1;
554
        else
555
            mb_row_flip = mb_row;
556

  
557
        for (block=0; block<4; block++) {
558
            s->left_block[block].ref_frame = -1;
559
            s->left_block[block].dc_coeff = 0;
560
            s->left_block[block].not_null_dc = 0;
561
            memset(s->coeff_ctx[block], 0, 64*sizeof(s->coeff_ctx[block][0]));
562
        }
563
        memset(s->coeff_ctx_last, 24, sizeof(s->coeff_ctx_last));
564

  
565
        s->above_block_idx[0] = 1;
566
        s->above_block_idx[1] = 2;
567
        s->above_block_idx[2] = 1;
568
        s->above_block_idx[3] = 2;
569
        s->above_block_idx[4] = 2*s->mb_width + 2 + 1;
570
        s->above_block_idx[5] = 3*s->mb_width + 4 + 1;
571

  
572
        s->block_offset[s->frbi] = (mb_row_flip*16 + mb_offset) * stride_y;
573
        s->block_offset[s->srbi] = s->block_offset[s->frbi] + 8*stride_y;
574
        s->block_offset[1] = s->block_offset[0] + 8;
575
        s->block_offset[3] = s->block_offset[2] + 8;
576
        s->block_offset[4] = (mb_row_flip*8 + mb_offset) * stride_uv;
577
        s->block_offset[5] = s->block_offset[4];
578

  
579
        for (mb_col=0; mb_col<s->mb_width; mb_col++) {
580
            vp56_decode_mb(s, mb_row, mb_col);
581

  
582
            for (y=0; y<4; y++) {
583
                s->above_block_idx[y] += 2;
584
                s->block_offset[y] += 16;
585
            }
586

  
587
            for (uv=4; uv<6; uv++) {
588
                s->above_block_idx[uv] += 1;
589
                s->block_offset[uv] += 8;
590
            }
591
        }
592
    }
593

  
594
    if (s->frames[VP56_FRAME_PREVIOUS].data[0]
595
        && (s->frames[VP56_FRAME_PREVIOUS].data[0]
596
            != s->frames[VP56_FRAME_GOLDEN].data[0])) {
597
        avctx->release_buffer(avctx, &s->frames[VP56_FRAME_PREVIOUS]);
598
    }
599
    if (p->key_frame || golden_frame) {
600
        if (s->frames[VP56_FRAME_GOLDEN].data[0])
601
            avctx->release_buffer(avctx, &s->frames[VP56_FRAME_GOLDEN]);
602
        s->frames[VP56_FRAME_GOLDEN] = *p;
603
    }
604
    s->frames[VP56_FRAME_PREVIOUS] = *p;
605

  
606
    *picture = *p;
607
    *data_size = sizeof(AVPicture);
608

  
609
    return buf_size;
610
}
611

  
612
void vp56_init(vp56_context_t *s, AVCodecContext *avctx, int flip)
613
{
614
    int i;
615

  
616
    s->avctx = avctx;
617
    avctx->pix_fmt = PIX_FMT_YUV420P;
618

  
619
    if (s->avctx->idct_algo == FF_IDCT_AUTO)
620
        s->avctx->idct_algo = FF_IDCT_VP3;
621
    dsputil_init(&s->dsp, s->avctx);
622
    ff_init_scantable(s->dsp.idct_permutation, &s->scantable,ff_zigzag_direct);
623

  
624
    avcodec_set_dimensions(s->avctx, 0, 0);
625

  
626
    for (i=0; i<3; i++)
627
        s->frames[i].data[0] = NULL;
628
    s->edge_emu_buffer_alloc = NULL;
629

  
630
    s->above_blocks = NULL;
631
    s->macroblocks = NULL;
632
    s->quantizer = -1;
633
    s->deblock_filtering = 1;
634

  
635
    s->filter = NULL;
636

  
637
    if (flip) {
638
        s->flip = -1;
639
        s->frbi = 2;
640
        s->srbi = 0;
641
    } else {
642
        s->flip = 1;
643
        s->frbi = 0;
644
        s->srbi = 2;
645
    }
646
}
647

  
648
int vp56_free(AVCodecContext *avctx)
649
{
650
    vp56_context_t *s = avctx->priv_data;
651

  
652
    av_free(s->above_blocks);
653
    av_free(s->macroblocks);
654
    av_free(s->edge_emu_buffer_alloc);
655
    if (s->frames[VP56_FRAME_GOLDEN].data[0]
656
        && (s->frames[VP56_FRAME_PREVIOUS].data[0]
657
            != s->frames[VP56_FRAME_GOLDEN].data[0]))
658
        avctx->release_buffer(avctx, &s->frames[VP56_FRAME_GOLDEN]);
659
    if (s->frames[VP56_FRAME_PREVIOUS].data[0])
660
        avctx->release_buffer(avctx, &s->frames[VP56_FRAME_PREVIOUS]);
661
    return 0;
662
}
libavcodec/vp56.h
1
/**
2
 * @file vp56.h
3
 * VP5 and VP6 compatible video decoder (common features)
4
 *
5
 * Copyright (C) 2006  Aurelien Jacobs <aurel@gnuage.org>
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
 */
21

  
22
#ifndef VP56_H
23
#define VP56_H
24

  
25
#include <stdint.h>
26

  
27
#include "vp56data.h"
28
#include "dsputil.h"
29
#include "mpegvideo.h"
30

  
31

  
32
typedef struct vp56_context vp56_context_t;
33
typedef struct vp56_mv vp56_mv_t;
34

  
35
typedef void (*vp56_parse_vector_adjustment_t)(vp56_context_t *s,
36
                                               vp56_mv_t *vector);
37
typedef int (*vp56_adjust_t)(int v, int t);
38
typedef void (*vp56_filter_t)(vp56_context_t *s, uint8_t *dst, uint8_t *src,
39
                              int offset1, int offset2, int stride,
40
                              vp56_mv_t mv, int mask, int select, int luma);
41
typedef void (*vp56_parse_coeff_t)(vp56_context_t *s);
42
typedef void (*vp56_default_models_init_t)(vp56_context_t *s);
43
typedef void (*vp56_parse_vector_models_t)(vp56_context_t *s);
44
typedef void (*vp56_parse_coeff_models_t)(vp56_context_t *s);
45
typedef int (*vp56_parse_header_t)(vp56_context_t *s, uint8_t *buf,
46
                                   int buf_size, int *golden_frame);
47

  
48
typedef struct {
49
    int high;
50
    int bits;
51
    const uint8_t *buffer;
52
    unsigned long code_word;
53
} vp56_range_coder_t;
54

  
55
typedef struct {
56
    uint8_t not_null_dc;
57
    vp56_frame_t ref_frame;
58
    DCTELEM dc_coeff;
59
} vp56_ref_dc_t;
60

  
61
struct vp56_mv {
62
    int x;
63
    int y;
64
};
65

  
66
typedef struct {
67
    uint8_t type;
68
    vp56_mv_t mv;
69
} vp56_macroblock_t;
70

  
71
struct vp56_context {
72
    AVCodecContext *avctx;
73
    DSPContext dsp;
74
    ScanTable scantable;
75
    AVFrame frames[3];
76
    uint8_t *edge_emu_buffer_alloc;
77
    uint8_t *edge_emu_buffer;
78
    vp56_range_coder_t c;
79

  
80
    /* frame info */
81
    int plane_width[3];
82
    int plane_height[3];
83
    int mb_width;   /* number of horizontal MB */
84
    int mb_height;  /* number of vertical MB */
85
    int block_offset[6];
86

  
87
    int quantizer;
88
    uint16_t dequant_dc;
89
    uint16_t dequant_ac;
90

  
91
    /* DC predictors management */
92
    vp56_ref_dc_t *above_blocks;
93
    vp56_ref_dc_t left_block[4];
94
    int above_block_idx[6];
95
    DCTELEM prev_dc[3][3];    /* [plan][ref_frame] */
96

  
97
    /* blocks / macroblock */
98
    vp56_mb_t mb_type;
99
    vp56_macroblock_t *macroblocks;
100
    DECLARE_ALIGNED_16(DCTELEM, block_coeff[6][64]);
101
    uint8_t coeff_reorder[64];       /* used in vp6 only */
102
    uint8_t coeff_index_to_pos[64];  /* used in vp6 only */
103

  
104
    /* motion vectors */
105
    vp56_mv_t mv[6];  /* vectors for each block in MB */
106
    vp56_mv_t vector_candidate[2];
107
    int vector_candidate_pos;
108

  
109
    /* filtering hints */
110
    int deblock_filtering;
111
    int filter_selection;
112
    int filter_mode;
113
    int max_vector_length;
114
    int sample_variance_threshold;
115

  
116
    /* AC models */
117
    uint8_t vector_model_sig[2];           /* delta sign */
118
    uint8_t vector_model_dct[2];           /* delta coding types */
119
    uint8_t vector_model_pdi[2][2];        /* predefined delta init */
120
    uint8_t vector_model_pdv[2][7];        /* predefined delta values */
121
    uint8_t vector_model_fdv[2][8];        /* 8 bit delta value definition */
122
    uint8_t mb_type_model[3][10][10];      /* model for decoding MB type */
123
    uint8_t coeff_model_dccv[2][11];       /* DC coeff value */
124
    uint8_t coeff_model_ract[2][3][6][11]; /* Run/AC coding type and AC coeff value */
125
    uint8_t coeff_model_acct[2][3][3][6][5];/* vp5 only AC coding type for coding group < 3 */
126
    uint8_t coeff_model_dcct[2][36][5];    /* DC coeff coding type */
127
    uint8_t coeff_model_runv[2][14];       /* run value (vp6 only) */
128
    uint8_t mb_types_stats[3][10][2];      /* contextual, next MB type stats */
129
    uint8_t coeff_ctx[4][64];              /* used in vp5 only */
130
    uint8_t coeff_ctx_last[4];             /* used in vp5 only */
131

  
132
    /* upside-down flipping hints */
133
    int flip;  /* are we flipping ? */
134
    int frbi;  /* first row block index in MB */
135
    int srbi;  /* second row block index in MB */
136
    int stride[3];  /* stride for each plan */
137

  
138
    const uint8_t *vp56_coord_div;
139
    vp56_parse_vector_adjustment_t parse_vector_adjustment;
140
    vp56_adjust_t adjust;
141
    vp56_filter_t filter;
142
    vp56_parse_coeff_t parse_coeff;
143
    vp56_default_models_init_t default_models_init;
144
    vp56_parse_vector_models_t parse_vector_models;
145
    vp56_parse_coeff_models_t parse_coeff_models;
146
    vp56_parse_header_t parse_header;
147
};
148

  
149

  
150
void vp56_init(vp56_context_t *s, AVCodecContext *avctx, int flip);
151
int vp56_free(AVCodecContext *avctx);
152
void vp56_init_dequant(vp56_context_t *s, int quantizer);
153
int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
154
                      uint8_t *buf, int buf_size);
155

  
156

  
157
/**
158
 * vp56 specific range coder implementation
159
 */
160

  
161
static inline void vp56_init_range_decoder(vp56_range_coder_t *c,
162
                                           const uint8_t *buf, int buf_size)
163
{
164
    c->high = 255;
165
    c->bits = 8;
166
    c->buffer = buf;
167
    c->code_word = *c->buffer++ << 8;
168
    c->code_word |= *c->buffer++;
169
}
170

  
171
static inline int vp56_rac_get_prob(vp56_range_coder_t *c, uint8_t prob)
172
{
173
    unsigned int low = 1 + (((c->high - 1) * prob) / 256);
174
    unsigned int low_shift = low << 8;
175
    int bit = c->code_word >= low_shift;
176

  
177
    if (bit) {
178
        c->high -= low;
179
        c->code_word -= low_shift;
180
    } else {
181
        c->high = low;
182
    }
183

  
184
    /* normalize */
185
    while (c->high < 128) {
186
        c->high <<= 1;
187
        c->code_word <<= 1;
188
        if (--c->bits == 0) {
189
            c->bits = 8;
190
            c->code_word |= *c->buffer++;
191
        }
192
    }
193
    return bit;
194
}
195

  
196
static inline int vp56_rac_get(vp56_range_coder_t *c)
197
{
198
    /* equiprobable */
199
    int low = (c->high + 1) >> 1;
200
    unsigned int low_shift = low << 8;
201
    int bit = c->code_word >= low_shift;
202
    if (bit) {
203
        c->high = (c->high - low) << 1;
204
        c->code_word -= low_shift;
205
    } else {
206
        c->high = low << 1;
207
    }
208

  
209
    /* normalize */
210
    c->code_word <<= 1;
211
    if (--c->bits == 0) {
212
        c->bits = 8;
213
        c->code_word |= *c->buffer++;
214
    }
215
    return bit;
216
}
217

  
218
static inline int vp56_rac_gets(vp56_range_coder_t *c, int bits)
219
{
220
    int value = 0;
221

  
222
    while (bits--) {
223
        value = (value << 1) | vp56_rac_get(c);
224
    }
225

  
226
    return value;
227
}
228

  
229
static inline int vp56_rac_gets_nn(vp56_range_coder_t *c, int bits)
230
{
231
    int v = vp56_rac_gets(c, 7) << 1;
232
    return v + !v;
233
}
234

  
235
static inline int vp56_rac_get_tree(vp56_range_coder_t *c,
236
                                    const vp56_tree_t *tree,
237
                                    const uint8_t *probs)
238
{
239
    while (tree->val > 0) {
240
        if (vp56_rac_get_prob(c, probs[tree->prob_idx]))
241
            tree += tree->val;
242
        else
243
            tree++;
244
    }
245
    return -tree->val;
246
}
247

  
248
#endif /* VP56_H */
libavcodec/vp56data.c
1
/**
2
 * @file vp56data.c
3
 * VP5 and VP6 compatible video decoder (common data)
4
 *
5
 * Copyright (C) 2006  Aurelien Jacobs <aurel@gnuage.org>
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
 */
21

  
22
#include <inttypes.h>
23
#include "vp56data.h"
24

  
25
const uint8_t vp56_b6to3[] = { 0, 0, 0, 0, 1, 2 };
26
const uint8_t vp56_b6to4[] = { 0, 0, 1, 1, 2, 3 };
27

  
28
const uint8_t vp56_coeff_parse_table[6][11] = {
29
    { 159,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0 },
30
    { 145, 165,   0,   0,   0,   0,   0,   0,   0,   0,   0 },
31
    { 140, 148, 173,   0,   0,   0,   0,   0,   0,   0,   0 },
32
    { 135, 140, 155, 176,   0,   0,   0,   0,   0,   0,   0 },
33
    { 130, 134, 141, 157, 180,   0,   0,   0,   0,   0,   0 },
34
    { 129, 130, 133, 140, 153, 177, 196, 230, 243, 254, 254 },
35
};
36

  
37
const uint8_t vp56_def_mb_types_stats[3][10][2] = {
38
    { {  69, 42 }, {   1,  2 }, {  1,   7 }, {  44, 42 }, {  6, 22 },
39
      {   1,  3 }, {   0,  2 }, {  1,   5 }, {   0,  1 }, {  0,  0 }, },
40
    { { 229,  8 }, {   1,  1 }, {  0,   8 }, {   0,  0 }, {  0,  0 },
41
      {   1,  2 }, {   0,  1 }, {  0,   0 }, {   1,  1 }, {  0,  0 }, },
42
    { { 122, 35 }, {   1,  1 }, {  1,   6 }, {  46, 34 }, {  0,  0 },
43
      {   1,  2 }, {   0,  1 }, {  0,   1 }, {   1,  1 }, {  0,  0 }, },
44
};
45

  
46
const vp56_tree_t vp56_pva_tree[] = {
47
    { 8, 0},
48
    { 4, 1},
49
    { 2, 2}, {-0}, {-1},
50
    { 2, 3}, {-2}, {-3},
51
    { 4, 4},
52
    { 2, 5}, {-4}, {-5},
53
    { 2, 6}, {-6}, {-7},
54
};
55

  
56
const vp56_tree_t vp56_pc_tree[] = {
57
    { 4, 6},
58
    { 2, 7}, {-0}, {-1},
59
    { 4, 8},
60
    { 2, 9}, {-2}, {-3},
61
    { 2,10}, {-4}, {-5},
62
};
63

  
64
const uint8_t vp56_coeff_bias[] = { 5, 7, 11, 19, 35, 67 };
65
const uint8_t vp56_coeff_bit_length[] = { 0, 1, 2, 3, 4, 10 };
libavcodec/vp56data.h
1
/**
2
 * @file vp56data.h
3
 * VP5 and VP6 compatible video decoder (common data)
4
 *
5
 * Copyright (C) 2006  Aurelien Jacobs <aurel@gnuage.org>
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
 */
21

  
22
#ifndef VP56DATA_H
23
#define VP56DATA_H
24

  
25
#include <inttypes.h>
26

  
27
typedef enum {
28
    VP56_FRAME_CURRENT  = 0,
29
    VP56_FRAME_PREVIOUS = 1,
30
    VP56_FRAME_GOLDEN   = 2,
31
} vp56_frame_t;
32

  
33
typedef enum {
34
    VP56_MB_INTER_NOVEC_PF = 0,  /**< Inter MB, no vector, from previous frame */
35
    VP56_MB_INTRA          = 1,  /**< Intra MB */
36
    VP56_MB_INTER_DELTA_PF = 2,  /**< Inter MB, above/left vector + delta, from previous frame */
37
    VP56_MB_INTER_V1_PF    = 3,  /**< Inter MB, first vector, from previous frame */
38
    VP56_MB_INTER_V2_PF    = 4,  /**< Inter MB, second vector, from previous frame */
39
    VP56_MB_INTER_NOVEC_GF = 5,  /**< Inter MB, no vector, from golden frame */
40
    VP56_MB_INTER_DELTA_GF = 6,  /**< Inter MB, above/left vector + delta, from golden frame */
41
    VP56_MB_INTER_4V       = 7,  /**< Inter MB, 4 vectors, from previous frame */
42
    VP56_MB_INTER_V1_GF    = 8,  /**< Inter MB, first vector, from golden frame */
43
    VP56_MB_INTER_V2_GF    = 9,  /**< Inter MB, second vector, from golden frame */
44
} vp56_mb_t;
45

  
46
typedef struct {
47
  int8_t val;
48
  int8_t prob_idx;
49
} vp56_tree_t;
50

  
51
extern const uint8_t vp56_b6to3[];
52
extern const uint8_t vp56_b6to4[];
53
extern const uint8_t vp56_coeff_parse_table[6][11];
54
extern const uint8_t vp56_def_mb_types_stats[3][10][2];
55
extern const vp56_tree_t vp56_pva_tree[];
56
extern const vp56_tree_t vp56_pc_tree[];
57
extern const uint8_t vp56_coeff_bias[];
58
extern const uint8_t vp56_coeff_bit_length[];
59

  
60
static const vp56_frame_t vp56_reference_frame[] = {
61
    VP56_FRAME_PREVIOUS,  /* VP56_MB_INTER_NOVEC_PF */
62
    VP56_FRAME_CURRENT,   /* VP56_MB_INTRA */
63
    VP56_FRAME_PREVIOUS,  /* VP56_MB_INTER_DELTA_PF */
64
    VP56_FRAME_PREVIOUS,  /* VP56_MB_INTER_V1_PF */
65
    VP56_FRAME_PREVIOUS,  /* VP56_MB_INTER_V2_PF */
66
    VP56_FRAME_GOLDEN,    /* VP56_MB_INTER_NOVEC_GF */
67
    VP56_FRAME_GOLDEN,    /* VP56_MB_INTER_DELTA_GF */
68
    VP56_FRAME_PREVIOUS,  /* VP56_MB_INTER_4V */
69
    VP56_FRAME_GOLDEN,    /* VP56_MB_INTER_V1_GF */
70
    VP56_FRAME_GOLDEN,    /* VP56_MB_INTER_V2_GF */
71
};
72

  
73
static const uint8_t vp56_ac_dequant[64] = {
74
    94, 92, 90, 88, 86, 82, 78, 74,
75
    70, 66, 62, 58, 54, 53, 52, 51,
76
    50, 49, 48, 47, 46, 45, 44, 43,
77
    42, 40, 39, 37, 36, 35, 34, 33,
78
    32, 31, 30, 29, 28, 27, 26, 25,
79
    24, 23, 22, 21, 20, 19, 18, 17,
80
    16, 15, 14, 13, 12, 11, 10,  9,
81
     8,  7,  6,  5,  4,  3,  2,  1,
82
};
83

  
84
static const uint8_t vp56_dc_dequant[64] = {
85
    47, 47, 47, 47, 45, 43, 43, 43,
86
    43, 43, 42, 41, 41, 40, 40, 40,
87
    40, 35, 35, 35, 35, 33, 33, 33,
88
    33, 32, 32, 32, 27, 27, 26, 26,
89
    25, 25, 24, 24, 23, 23, 19, 19,
90
    19, 19, 18, 18, 17, 16, 16, 16,
91
    16, 16, 15, 11, 11, 11, 10, 10,
92
     9,  8,  7,  5,  3,  3,  2,  2,
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff