]> git.sesse.net Git - ffmpeg/blob - libavcodec/vp56.h
Merge commit '8b8899ac3233b4f7af83ded0dc032fad8902d714'
[ffmpeg] / libavcodec / vp56.h
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 and VP6 compatible video decoder (common features)
24  */
25
26 #ifndef AVCODEC_VP56_H
27 #define AVCODEC_VP56_H
28
29 #include "vp56data.h"
30 #include "dsputil.h"
31 #include "get_bits.h"
32 #include "bytestream.h"
33 #include "vp3dsp.h"
34 #include "vp56dsp.h"
35
36 typedef struct vp56_context VP56Context;
37
38 typedef struct VP56mv {
39     DECLARE_ALIGNED(4, int16_t, x);
40     int16_t y;
41 } VP56mv;
42
43 typedef void (*VP56ParseVectorAdjustment)(VP56Context *s,
44                                           VP56mv *vect);
45 typedef void (*VP56Filter)(VP56Context *s, uint8_t *dst, uint8_t *src,
46                            int offset1, int offset2, int stride,
47                            VP56mv mv, int mask, int select, int luma);
48 typedef void (*VP56ParseCoeff)(VP56Context *s);
49 typedef void (*VP56DefaultModelsInit)(VP56Context *s);
50 typedef void (*VP56ParseVectorModels)(VP56Context *s);
51 typedef int  (*VP56ParseCoeffModels)(VP56Context *s);
52 typedef int  (*VP56ParseHeader)(VP56Context *s, const uint8_t *buf,
53                                 int buf_size);
54
55 typedef struct VP56RangeCoder {
56     int high;
57     int bits; /* stored negated (i.e. negative "bits" is a positive number of
58                  bits left) in order to eliminate a negate in cache refilling */
59     const uint8_t *buffer;
60     const uint8_t *end;
61     unsigned int code_word;
62 } VP56RangeCoder;
63
64 typedef struct VP56RefDc {
65     uint8_t not_null_dc;
66     VP56Frame ref_frame;
67     DCTELEM dc_coeff;
68 } VP56RefDc;
69
70 typedef struct VP56Macroblock {
71     uint8_t type;
72     VP56mv mv;
73 } VP56Macroblock;
74
75 typedef struct VP56Model {
76     uint8_t coeff_reorder[64];       /* used in vp6 only */
77     uint8_t coeff_index_to_pos[64];  /* used in vp6 only */
78     uint8_t vector_sig[2];           /* delta sign */
79     uint8_t vector_dct[2];           /* delta coding types */
80     uint8_t vector_pdi[2][2];        /* predefined delta init */
81     uint8_t vector_pdv[2][7];        /* predefined delta values */
82     uint8_t vector_fdv[2][8];        /* 8 bit delta value definition */
83     uint8_t coeff_dccv[2][11];       /* DC coeff value */
84     uint8_t coeff_ract[2][3][6][11]; /* Run/AC coding type and AC coeff value */
85     uint8_t coeff_acct[2][3][3][6][5];/* vp5 only AC coding type for coding group < 3 */
86     uint8_t coeff_dcct[2][36][5];    /* DC coeff coding type */
87     uint8_t coeff_runv[2][14];       /* run value (vp6 only) */
88     uint8_t mb_type[3][10][10];      /* model for decoding MB type */
89     uint8_t mb_types_stats[3][10][2];/* contextual, next MB type stats */
90 } VP56Model;
91
92 struct vp56_context {
93     AVCodecContext *avctx;
94     DSPContext dsp;
95     VP3DSPContext vp3dsp;
96     VP56DSPContext vp56dsp;
97     ScanTable scantable;
98     AVFrame frames[4];
99     AVFrame *framep[6];
100     uint8_t *edge_emu_buffer_alloc;
101     uint8_t *edge_emu_buffer;
102     VP56RangeCoder c;
103     VP56RangeCoder cc;
104     VP56RangeCoder *ccp;
105     int sub_version;
106
107     /* frame info */
108     int golden_frame;
109     int plane_width[4];
110     int plane_height[4];
111     int mb_width;   /* number of horizontal MB */
112     int mb_height;  /* number of vertical MB */
113     int block_offset[6];
114
115     int quantizer;
116     uint16_t dequant_dc;
117     uint16_t dequant_ac;
118     int8_t *qscale_table;
119
120     /* DC predictors management */
121     VP56RefDc *above_blocks;
122     VP56RefDc left_block[4];
123     int above_block_idx[6];
124     DCTELEM prev_dc[3][3];    /* [plan][ref_frame] */
125
126     /* blocks / macroblock */
127     VP56mb mb_type;
128     VP56Macroblock *macroblocks;
129     DECLARE_ALIGNED(16, DCTELEM, block_coeff)[6][64];
130
131     /* motion vectors */
132     VP56mv mv[6];  /* vectors for each block in MB */
133     VP56mv vector_candidate[2];
134     int vector_candidate_pos;
135
136     /* filtering hints */
137     int filter_header;               /* used in vp6 only */
138     int deblock_filtering;
139     int filter_selection;
140     int filter_mode;
141     int max_vector_length;
142     int sample_variance_threshold;
143
144     uint8_t coeff_ctx[4][64];              /* used in vp5 only */
145     uint8_t coeff_ctx_last[4];             /* used in vp5 only */
146
147     int has_alpha;
148
149     /* upside-down flipping hints */
150     int flip;  /* are we flipping ? */
151     int frbi;  /* first row block index in MB */
152     int srbi;  /* second row block index in MB */
153     int stride[4];  /* stride for each plan */
154
155     const uint8_t *vp56_coord_div;
156     VP56ParseVectorAdjustment parse_vector_adjustment;
157     VP56Filter filter;
158     VP56ParseCoeff parse_coeff;
159     VP56DefaultModelsInit default_models_init;
160     VP56ParseVectorModels parse_vector_models;
161     VP56ParseCoeffModels parse_coeff_models;
162     VP56ParseHeader parse_header;
163
164     /* for "slice" parallelism between YUV and A */
165     VP56Context *alpha_context;
166
167     VP56Model *modelp;
168     VP56Model model;
169
170     /* huffman decoding */
171     int use_huffman;
172     GetBitContext gb;
173     VLC dccv_vlc[2];
174     VLC runv_vlc[2];
175     VLC ract_vlc[2][3][6];
176     unsigned int nb_null[2][2];       /* number of consecutive NULL DC/AC */
177 };
178
179
180 void ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha);
181 void ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s,
182                           int flip, int has_alpha);
183 int ff_vp56_free(AVCodecContext *avctx);
184 int ff_vp56_free_context(VP56Context *s);
185 void ff_vp56_init_dequant(VP56Context *s, int quantizer);
186 int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
187                          AVPacket *avpkt);
188
189
190 /**
191  * vp56 specific range coder implementation
192  */
193
194 extern const uint8_t ff_vp56_norm_shift[256];
195 void ff_vp56_init_range_decoder(VP56RangeCoder *c, const uint8_t *buf, int buf_size);
196
197 static av_always_inline unsigned int vp56_rac_renorm(VP56RangeCoder *c)
198 {
199     int shift = ff_vp56_norm_shift[c->high];
200     int bits = c->bits;
201     unsigned int code_word = c->code_word;
202
203     c->high   <<= shift;
204     code_word <<= shift;
205     bits       += shift;
206     if(bits >= 0 && c->buffer < c->end) {
207         code_word |= bytestream_get_be16(&c->buffer) << bits;
208         bits -= 16;
209     }
210     c->bits = bits;
211     return code_word;
212 }
213
214 #if   ARCH_ARM
215 #include "arm/vp56_arith.h"
216 #elif ARCH_X86
217 #include "x86/vp56_arith.h"
218 #endif
219
220 #ifndef vp56_rac_get_prob
221 #define vp56_rac_get_prob vp56_rac_get_prob
222 static av_always_inline int vp56_rac_get_prob(VP56RangeCoder *c, uint8_t prob)
223 {
224     unsigned int code_word = vp56_rac_renorm(c);
225     unsigned int low = 1 + (((c->high - 1) * prob) >> 8);
226     unsigned int low_shift = low << 16;
227     int bit = code_word >= low_shift;
228
229     c->high = bit ? c->high - low : low;
230     c->code_word = bit ? code_word - low_shift : code_word;
231
232     return bit;
233 }
234 #endif
235
236 #ifndef vp56_rac_get_prob_branchy
237 // branchy variant, to be used where there's a branch based on the bit decoded
238 static av_always_inline int vp56_rac_get_prob_branchy(VP56RangeCoder *c, int prob)
239 {
240     unsigned long code_word = vp56_rac_renorm(c);
241     unsigned low = 1 + (((c->high - 1) * prob) >> 8);
242     unsigned low_shift = low << 16;
243
244     if (code_word >= low_shift) {
245         c->high     -= low;
246         c->code_word = code_word - low_shift;
247         return 1;
248     }
249
250     c->high = low;
251     c->code_word = code_word;
252     return 0;
253 }
254 #endif
255
256 static av_always_inline int vp56_rac_get(VP56RangeCoder *c)
257 {
258     unsigned int code_word = vp56_rac_renorm(c);
259     /* equiprobable */
260     int low = (c->high + 1) >> 1;
261     unsigned int low_shift = low << 16;
262     int bit = code_word >= low_shift;
263     if (bit) {
264         c->high   -= low;
265         code_word -= low_shift;
266     } else {
267         c->high = low;
268     }
269
270     c->code_word = code_word;
271     return bit;
272 }
273
274 // rounding is different than vp56_rac_get, is vp56_rac_get wrong?
275 static av_always_inline int vp8_rac_get(VP56RangeCoder *c)
276 {
277     return vp56_rac_get_prob(c, 128);
278 }
279
280 static av_unused int vp56_rac_gets(VP56RangeCoder *c, int bits)
281 {
282     int value = 0;
283
284     while (bits--) {
285         value = (value << 1) | vp56_rac_get(c);
286     }
287
288     return value;
289 }
290
291 static av_unused int vp8_rac_get_uint(VP56RangeCoder *c, int bits)
292 {
293     int value = 0;
294
295     while (bits--) {
296         value = (value << 1) | vp8_rac_get(c);
297     }
298
299     return value;
300 }
301
302 // fixme: add 1 bit to all the calls to this?
303 static av_unused int vp8_rac_get_sint(VP56RangeCoder *c, int bits)
304 {
305     int v;
306
307     if (!vp8_rac_get(c))
308         return 0;
309
310     v = vp8_rac_get_uint(c, bits);
311
312     if (vp8_rac_get(c))
313         v = -v;
314
315     return v;
316 }
317
318 // P(7)
319 static av_unused int vp56_rac_gets_nn(VP56RangeCoder *c, int bits)
320 {
321     int v = vp56_rac_gets(c, 7) << 1;
322     return v + !v;
323 }
324
325 static av_unused int vp8_rac_get_nn(VP56RangeCoder *c)
326 {
327     int v = vp8_rac_get_uint(c, 7) << 1;
328     return v + !v;
329 }
330
331 static av_always_inline
332 int vp56_rac_get_tree(VP56RangeCoder *c,
333                       const VP56Tree *tree,
334                       const uint8_t *probs)
335 {
336     while (tree->val > 0) {
337         if (vp56_rac_get_prob(c, probs[tree->prob_idx]))
338             tree += tree->val;
339         else
340             tree++;
341     }
342     return -tree->val;
343 }
344
345 // how probabilities are associated with decisions is different I think
346 // well, the new scheme fits in the old but this way has one fewer branches per decision
347 static av_always_inline int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t (*tree)[2],
348                                    const uint8_t *probs)
349 {
350     int i = 0;
351
352     do {
353         i = tree[i][vp56_rac_get_prob(c, probs[i])];
354     } while (i > 0);
355
356     return -i;
357 }
358
359 // DCTextra
360 static av_always_inline int vp8_rac_get_coeff(VP56RangeCoder *c, const uint8_t *prob)
361 {
362     int v = 0;
363
364     do {
365         v = (v<<1) + vp56_rac_get_prob(c, *prob++);
366     } while (*prob);
367
368     return v;
369 }
370
371 #endif /* AVCODEC_VP56_H */