Mercurial > libavcodec.hg
annotate vp56.h @ 12029:934968bd410d libavcodec
renormalize VP5/6/7/8 range coder without loop
author | stefang |
---|---|
date | Wed, 30 Jun 2010 22:05:29 +0000 |
parents | f2007d7c3f1d |
children | 5578dcdf030c |
rev | line source |
---|---|
3695 | 1 /** |
11644
7dd2a45249a9
Remove explicit filename from Doxygen @file commands.
diego
parents:
11369
diff
changeset
|
2 * @file |
3695 | 3 * VP5 and VP6 compatible video decoder (common features) |
4 * | |
5 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org> | |
6 * | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3759
diff
changeset
|
7 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3759
diff
changeset
|
8 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3759
diff
changeset
|
9 * FFmpeg is free software; you can redistribute it and/or |
3695 | 10 * modify it under the terms of the GNU Lesser General Public |
11 * License as published by the Free Software Foundation; either | |
12 * version 2.1 of the License, or (at your option) any later version. | |
13 * | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3759
diff
changeset
|
14 * FFmpeg is distributed in the hope that it will be useful, |
3695 | 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 * Lesser General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU Lesser General Public | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3759
diff
changeset
|
20 * License along with FFmpeg; if not, write to the Free Software |
5215 | 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
3695 | 22 */ |
23 | |
7760 | 24 #ifndef AVCODEC_VP56_H |
25 #define AVCODEC_VP56_H | |
3695 | 26 |
27 #include "vp56data.h" | |
28 #include "dsputil.h" | |
9428 | 29 #include "get_bits.h" |
5089 | 30 #include "bytestream.h" |
12029 | 31 #include "cabac.h" |
11665 | 32 #include "vp56dsp.h" |
3695 | 33 |
8299 | 34 typedef struct vp56_context VP56Context; |
35 typedef struct vp56_mv VP56mv; | |
3695 | 36 |
8299 | 37 typedef void (*VP56ParseVectorAdjustment)(VP56Context *s, |
8300 | 38 VP56mv *vect); |
8299 | 39 typedef void (*VP56Filter)(VP56Context *s, uint8_t *dst, uint8_t *src, |
8300 | 40 int offset1, int offset2, int stride, |
41 VP56mv mv, int mask, int select, int luma); | |
8299 | 42 typedef void (*VP56ParseCoeff)(VP56Context *s); |
43 typedef void (*VP56DefaultModelsInit)(VP56Context *s); | |
44 typedef void (*VP56ParseVectorModels)(VP56Context *s); | |
45 typedef void (*VP56ParseCoeffModels)(VP56Context *s); | |
8300 | 46 typedef int (*VP56ParseHeader)(VP56Context *s, const uint8_t *buf, |
47 int buf_size, int *golden_frame); | |
3695 | 48 |
49 typedef struct { | |
50 int high; | |
51 int bits; | |
6297 | 52 const uint8_t *buffer; |
9919
c7c1c6b35a73
vp56dec: ensure range coder won't read past the end of input buffer
aurel
parents:
9428
diff
changeset
|
53 const uint8_t *end; |
3695 | 54 unsigned long code_word; |
8299 | 55 } VP56RangeCoder; |
3695 | 56 |
57 typedef struct { | |
58 uint8_t not_null_dc; | |
8299 | 59 VP56Frame ref_frame; |
3695 | 60 DCTELEM dc_coeff; |
8299 | 61 } VP56RefDc; |
3695 | 62 |
63 struct vp56_mv { | |
64 int x; | |
65 int y; | |
66 }; | |
67 | |
68 typedef struct { | |
69 uint8_t type; | |
8299 | 70 VP56mv mv; |
71 } VP56Macroblock; | |
3695 | 72 |
5711 | 73 typedef struct { |
74 uint8_t coeff_reorder[64]; /* used in vp6 only */ | |
75 uint8_t coeff_index_to_pos[64]; /* used in vp6 only */ | |
76 uint8_t vector_sig[2]; /* delta sign */ | |
77 uint8_t vector_dct[2]; /* delta coding types */ | |
78 uint8_t vector_pdi[2][2]; /* predefined delta init */ | |
79 uint8_t vector_pdv[2][7]; /* predefined delta values */ | |
80 uint8_t vector_fdv[2][8]; /* 8 bit delta value definition */ | |
81 uint8_t coeff_dccv[2][11]; /* DC coeff value */ | |
82 uint8_t coeff_ract[2][3][6][11]; /* Run/AC coding type and AC coeff value */ | |
83 uint8_t coeff_acct[2][3][3][6][5];/* vp5 only AC coding type for coding group < 3 */ | |
84 uint8_t coeff_dcct[2][36][5]; /* DC coeff coding type */ | |
85 uint8_t coeff_runv[2][14]; /* run value (vp6 only) */ | |
86 uint8_t mb_type[3][10][10]; /* model for decoding MB type */ | |
87 uint8_t mb_types_stats[3][10][2];/* contextual, next MB type stats */ | |
8304 | 88 } VP56Model; |
5711 | 89 |
3695 | 90 struct vp56_context { |
91 AVCodecContext *avctx; | |
92 DSPContext dsp; | |
11665 | 93 VP56DSPContext vp56dsp; |
3695 | 94 ScanTable scantable; |
5714
314be1cfdcb0
add a new vp6a codec (add alpha plan support to vp6)
aurel
parents:
5711
diff
changeset
|
95 AVFrame frames[4]; |
314be1cfdcb0
add a new vp6a codec (add alpha plan support to vp6)
aurel
parents:
5711
diff
changeset
|
96 AVFrame *framep[6]; |
3695 | 97 uint8_t *edge_emu_buffer_alloc; |
98 uint8_t *edge_emu_buffer; | |
8299 | 99 VP56RangeCoder c; |
100 VP56RangeCoder cc; | |
101 VP56RangeCoder *ccp; | |
4308 | 102 int sub_version; |
3695 | 103 |
104 /* frame info */ | |
5714
314be1cfdcb0
add a new vp6a codec (add alpha plan support to vp6)
aurel
parents:
5711
diff
changeset
|
105 int plane_width[4]; |
314be1cfdcb0
add a new vp6a codec (add alpha plan support to vp6)
aurel
parents:
5711
diff
changeset
|
106 int plane_height[4]; |
3695 | 107 int mb_width; /* number of horizontal MB */ |
108 int mb_height; /* number of vertical MB */ | |
109 int block_offset[6]; | |
110 | |
111 int quantizer; | |
112 uint16_t dequant_dc; | |
113 uint16_t dequant_ac; | |
11053
c57e72227d7d
Make VP5 and VP6 decoders output a qscale table to allow for more automatic
reimar
parents:
10961
diff
changeset
|
114 int8_t *qscale_table; |
3695 | 115 |
116 /* DC predictors management */ | |
8299 | 117 VP56RefDc *above_blocks; |
118 VP56RefDc left_block[4]; | |
3695 | 119 int above_block_idx[6]; |
120 DCTELEM prev_dc[3][3]; /* [plan][ref_frame] */ | |
121 | |
122 /* blocks / macroblock */ | |
8299 | 123 VP56mb mb_type; |
124 VP56Macroblock *macroblocks; | |
11369 | 125 DECLARE_ALIGNED(16, DCTELEM, block_coeff)[6][64]; |
3695 | 126 |
127 /* motion vectors */ | |
8299 | 128 VP56mv mv[6]; /* vectors for each block in MB */ |
129 VP56mv vector_candidate[2]; | |
3695 | 130 int vector_candidate_pos; |
131 | |
132 /* filtering hints */ | |
4348 | 133 int filter_header; /* used in vp6 only */ |
3695 | 134 int deblock_filtering; |
135 int filter_selection; | |
136 int filter_mode; | |
137 int max_vector_length; | |
138 int sample_variance_threshold; | |
139 | |
140 uint8_t coeff_ctx[4][64]; /* used in vp5 only */ | |
141 uint8_t coeff_ctx_last[4]; /* used in vp5 only */ | |
142 | |
5714
314be1cfdcb0
add a new vp6a codec (add alpha plan support to vp6)
aurel
parents:
5711
diff
changeset
|
143 int has_alpha; |
314be1cfdcb0
add a new vp6a codec (add alpha plan support to vp6)
aurel
parents:
5711
diff
changeset
|
144 |
3695 | 145 /* upside-down flipping hints */ |
146 int flip; /* are we flipping ? */ | |
147 int frbi; /* first row block index in MB */ | |
148 int srbi; /* second row block index in MB */ | |
5714
314be1cfdcb0
add a new vp6a codec (add alpha plan support to vp6)
aurel
parents:
5711
diff
changeset
|
149 int stride[4]; /* stride for each plan */ |
3695 | 150 |
151 const uint8_t *vp56_coord_div; | |
8299 | 152 VP56ParseVectorAdjustment parse_vector_adjustment; |
153 VP56Filter filter; | |
154 VP56ParseCoeff parse_coeff; | |
155 VP56DefaultModelsInit default_models_init; | |
156 VP56ParseVectorModels parse_vector_models; | |
157 VP56ParseCoeffModels parse_coeff_models; | |
158 VP56ParseHeader parse_header; | |
5711 | 159 |
8304 | 160 VP56Model *modelp; |
161 VP56Model models[2]; | |
5821 | 162 |
163 /* huffman decoding */ | |
164 int use_huffman; | |
165 GetBitContext gb; | |
166 VLC dccv_vlc[2]; | |
167 VLC runv_vlc[2]; | |
168 VLC ract_vlc[2][3][6]; | |
169 unsigned int nb_null[2][2]; /* number of consecutive NULL DC/AC */ | |
3695 | 170 }; |
171 | |
172 | |
5714
314be1cfdcb0
add a new vp6a codec (add alpha plan support to vp6)
aurel
parents:
5711
diff
changeset
|
173 void vp56_init(AVCodecContext *avctx, int flip, int has_alpha); |
3695 | 174 int vp56_free(AVCodecContext *avctx); |
8299 | 175 void vp56_init_dequant(VP56Context *s, int quantizer); |
3695 | 176 int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size, |
9356
2983bd7deaf5
fix vp5/vp6 decoding by using new prototype for decode function
aurel
parents:
8718
diff
changeset
|
177 AVPacket *avpkt); |
3695 | 178 |
179 | |
180 /** | |
181 * vp56 specific range coder implementation | |
182 */ | |
183 | |
8299 | 184 static inline void vp56_init_range_decoder(VP56RangeCoder *c, |
6297 | 185 const uint8_t *buf, int buf_size) |
3695 | 186 { |
187 c->high = 255; | |
188 c->bits = 8; | |
189 c->buffer = buf; | |
9919
c7c1c6b35a73
vp56dec: ensure range coder won't read past the end of input buffer
aurel
parents:
9428
diff
changeset
|
190 c->end = buf + buf_size; |
5089 | 191 c->code_word = bytestream_get_be16(&c->buffer); |
3695 | 192 } |
193 | |
8299 | 194 static inline int vp56_rac_get_prob(VP56RangeCoder *c, uint8_t prob) |
3695 | 195 { |
11920 | 196 unsigned int low = 1 + (((c->high - 1) * prob) >> 8); |
3695 | 197 unsigned int low_shift = low << 8; |
198 int bit = c->code_word >= low_shift; | |
12029 | 199 int shift; |
3695 | 200 |
201 if (bit) { | |
202 c->high -= low; | |
203 c->code_word -= low_shift; | |
204 } else { | |
205 c->high = low; | |
206 } | |
207 | |
208 /* normalize */ | |
12029 | 209 shift = ff_h264_norm_shift[c->high] - 1; |
210 c->high <<= shift; | |
211 c->code_word <<= shift; | |
212 c->bits -= shift; | |
213 if(c->bits <= 0 && c->buffer < c->end) { | |
214 c->code_word |= *c->buffer++ << -c->bits; | |
215 c->bits += 8; | |
3695 | 216 } |
217 return bit; | |
218 } | |
219 | |
8299 | 220 static inline int vp56_rac_get(VP56RangeCoder *c) |
3695 | 221 { |
222 /* equiprobable */ | |
223 int low = (c->high + 1) >> 1; | |
224 unsigned int low_shift = low << 8; | |
225 int bit = c->code_word >= low_shift; | |
226 if (bit) { | |
227 c->high = (c->high - low) << 1; | |
228 c->code_word -= low_shift; | |
229 } else { | |
230 c->high = low << 1; | |
231 } | |
232 | |
233 /* normalize */ | |
234 c->code_word <<= 1; | |
9919
c7c1c6b35a73
vp56dec: ensure range coder won't read past the end of input buffer
aurel
parents:
9428
diff
changeset
|
235 if (--c->bits == 0 && c->buffer < c->end) { |
3695 | 236 c->bits = 8; |
237 c->code_word |= *c->buffer++; | |
238 } | |
239 return bit; | |
240 } | |
241 | |
11921 | 242 // rounding is different than vp56_rac_get, is vp56_rac_get wrong? |
243 static inline int vp8_rac_get(VP56RangeCoder *c) | |
244 { | |
245 return vp56_rac_get_prob(c, 128); | |
246 } | |
247 | |
8299 | 248 static inline int vp56_rac_gets(VP56RangeCoder *c, int bits) |
3695 | 249 { |
250 int value = 0; | |
251 | |
252 while (bits--) { | |
253 value = (value << 1) | vp56_rac_get(c); | |
254 } | |
255 | |
256 return value; | |
257 } | |
258 | |
11921 | 259 static inline int vp8_rac_get_uint(VP56RangeCoder *c, int bits) |
260 { | |
261 int value = 0; | |
262 | |
263 while (bits--) { | |
264 value = (value << 1) | vp8_rac_get(c); | |
265 } | |
266 | |
267 return value; | |
268 } | |
269 | |
270 // fixme: add 1 bit to all the calls to this? | |
271 static inline int vp8_rac_get_sint(VP56RangeCoder *c, int bits) | |
272 { | |
273 int v; | |
274 | |
275 if (!vp8_rac_get(c)) | |
276 return 0; | |
277 | |
278 v = vp8_rac_get_uint(c, bits); | |
279 | |
280 if (vp8_rac_get(c)) | |
281 v = -v; | |
282 | |
283 return v; | |
284 } | |
285 | |
286 // P(7) | |
8299 | 287 static inline int vp56_rac_gets_nn(VP56RangeCoder *c, int bits) |
3695 | 288 { |
289 int v = vp56_rac_gets(c, 7) << 1; | |
290 return v + !v; | |
291 } | |
292 | |
11921 | 293 static inline int vp8_rac_get_nn(VP56RangeCoder *c) |
294 { | |
295 int v = vp8_rac_get_uint(c, 7) << 1; | |
296 return v + !v; | |
297 } | |
298 | |
8299 | 299 static inline int vp56_rac_get_tree(VP56RangeCoder *c, |
300 const VP56Tree *tree, | |
3695 | 301 const uint8_t *probs) |
302 { | |
303 while (tree->val > 0) { | |
304 if (vp56_rac_get_prob(c, probs[tree->prob_idx])) | |
305 tree += tree->val; | |
306 else | |
307 tree++; | |
308 } | |
309 return -tree->val; | |
310 } | |
311 | |
11921 | 312 /** |
313 * This is identical to vp8_rac_get_tree except for the possibility of starting | |
314 * on a node other than the root node, needed for coeff decode where this is | |
315 * used to save a bit after a 0 token (by disallowing EOB to immediately follow.) | |
316 */ | |
317 static inline int vp8_rac_get_tree_with_offset(VP56RangeCoder *c, const int8_t (*tree)[2], | |
318 const uint8_t *probs, int i) | |
319 { | |
320 do { | |
321 i = tree[i][vp56_rac_get_prob(c, probs[i])]; | |
322 } while (i > 0); | |
323 | |
324 return -i; | |
325 } | |
326 | |
327 // how probabilities are associated with decisions is different I think | |
328 // well, the new scheme fits in the old but this way has one fewer branches per decision | |
329 static inline int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t (*tree)[2], | |
330 const uint8_t *probs) | |
331 { | |
332 return vp8_rac_get_tree_with_offset(c, tree, probs, 0); | |
333 } | |
334 | |
335 // DCTextra | |
336 static inline int vp8_rac_get_coeff(VP56RangeCoder *c, const uint8_t *prob) | |
337 { | |
338 int v = 0; | |
339 | |
340 do { | |
341 v = (v<<1) + vp56_rac_get_prob(c, *prob++); | |
342 } while (*prob); | |
343 | |
344 return v; | |
345 } | |
346 | |
7760 | 347 #endif /* AVCODEC_VP56_H */ |