Mercurial > libavcodec.hg
comparison ivi_common.h @ 11054:3b79a8709f43 libavcodec
Indeo Video Interactive 4 and 5 common code and DSP functions.
Reviewed and corrected by myself because there were no other volunteers in the
last weeks.
author | kostya |
---|---|
date | Sat, 30 Jan 2010 08:10:17 +0000 |
parents | |
children | 0b2618b091df |
comparison
equal
deleted
inserted
replaced
11053:c57e72227d7d | 11054:3b79a8709f43 |
---|---|
1 /* | |
2 * common functions for Indeo Video Interactive codecs (Indeo4 and Indeo5) | |
3 * | |
4 * Copyright (c) 2009 Maxim Poliakovski | |
5 * | |
6 * This file is part of FFmpeg. | |
7 * | |
8 * FFmpeg is free software; you can redistribute it and/or | |
9 * modify it under the terms of the GNU Lesser General Public | |
10 * License as published by the Free Software Foundation; either | |
11 * version 2.1 of the License, or (at your option) any later version. | |
12 * | |
13 * FFmpeg is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Lesser General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Lesser General Public | |
19 * License along with FFmpeg; if not, write to the Free Software | |
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 */ | |
22 | |
23 /** | |
24 * @file libavcodec/ivi_common.h | |
25 * This file contains structures and macros shared by both Indeo4 and | |
26 * Indeo5 decoders. | |
27 */ | |
28 | |
29 #ifndef AVCODEC_IVI_COMMON_H | |
30 #define AVCODEC_IVI_COMMON_H | |
31 | |
32 #include "avcodec.h" | |
33 #include "get_bits.h" | |
34 #include <stdint.h> | |
35 | |
36 #define IVI_DEBUG 0 | |
37 | |
38 #define IVI_VLC_BITS 13 ///< max number of bits of the ivi's huffman codes | |
39 | |
40 /** | |
41 * huffman codebook descriptor | |
42 */ | |
43 typedef struct { | |
44 int32_t num_rows; | |
45 uint8_t xbits[16]; | |
46 } IVIHuffDesc; | |
47 | |
48 extern const IVIHuffDesc ff_ivi_mb_huff_desc[8]; ///< static macroblock huffman tables | |
49 extern const IVIHuffDesc ff_ivi_blk_huff_desc[8]; ///< static block huffman tables | |
50 | |
51 | |
52 /** | |
53 * run-value (RLE) table descriptor | |
54 */ | |
55 typedef struct { | |
56 uint8_t eob_sym; ///< end of block symbol | |
57 uint8_t esc_sym; ///< escape symbol | |
58 uint8_t runtab[256]; | |
59 int8_t valtab[256]; | |
60 } RVMapDesc; | |
61 | |
62 extern const RVMapDesc ff_ivi_rvmap_tabs[9]; | |
63 | |
64 | |
65 /** | |
66 * information for Indeo macroblock (16x16, 8x8 or 4x4) | |
67 */ | |
68 typedef struct { | |
69 int16_t xpos; | |
70 int16_t ypos; | |
71 uint32_t buf_offs; ///< address in the output buffer for this mb | |
72 uint8_t type; ///< macroblock type: 0 - INTRA, 1 - INTER | |
73 uint8_t cbp; ///< coded block pattern | |
74 uint8_t q_delta; ///< quant delta | |
75 int8_t mv_x; ///< motion vector (x component) | |
76 int8_t mv_y; ///< motion vector (y component) | |
77 } IVIMbInfo; | |
78 | |
79 | |
80 /** | |
81 * information for Indeo tile | |
82 */ | |
83 typedef struct { | |
84 int xpos; | |
85 int ypos; | |
86 int width; | |
87 int height; | |
88 int is_empty; ///< = 1 if this tile doesn't contain any data | |
89 int data_size; ///< size of the data in bytes | |
90 int num_MBs; ///< number of macroblocks in this tile | |
91 IVIMbInfo *mbs; ///< array of macroblock descriptors | |
92 IVIMbInfo *ref_mbs; ///< ptr to the macroblock descriptors of the reference tile | |
93 } IVITile; | |
94 | |
95 | |
96 /** | |
97 * information for Indeo wavelet band | |
98 */ | |
99 typedef struct { | |
100 int plane; ///< plane number this band belongs to | |
101 int band_num; ///< band number | |
102 int width; | |
103 int height; | |
104 const uint8_t *data_ptr; ///< ptr to the first byte of the band data | |
105 int data_size; ///< size of the band data | |
106 int16_t *buf; ///< pointer to the output buffer for this band | |
107 int16_t *ref_buf; ///< pointer to the reference frame buffer (for motion compensation) | |
108 int16_t *bufs[3]; ///< array of pointers to the band buffers | |
109 int pitch; ///< pitch associated with the buffers above | |
110 int is_empty; ///< = 1 if this band doesn't contain any data | |
111 int mb_size; ///< macroblock size | |
112 int blk_size; ///< block size | |
113 int is_halfpel; ///< precision of the motion compensation: 0 - fullpel, 1 - halfpel | |
114 int inherit_mv; ///< tells if motion vector is inherited from reference macroblock | |
115 int inherit_qdelta; ///< tells if quantiser delta is inherited from reference macroblock | |
116 int qdelta_present; ///< tells if Qdelta signal is present in the bitstream (Indeo5 only) | |
117 int quant_mat; ///< dequant matrix index | |
118 int glob_quant; ///< quant base for this band | |
119 const uint8_t *scan; ///< ptr to the scan pattern | |
120 | |
121 int huff_sel; ///< huffman table for this band | |
122 IVIHuffDesc huff_desc; ///< table descriptor associated with the selector above | |
123 VLC *blk_vlc; ///< ptr to the vlc table for decoding block data | |
124 VLC blk_vlc_cust; ///< custom block vlc table | |
125 | |
126 uint16_t *dequant_intra; ///< ptr to dequant tables for intra blocks | |
127 uint16_t *dequant_inter; ///< ptr dequant tables for inter blocks | |
128 int num_corr; ///< number of correction entries | |
129 uint8_t corr[61*2]; ///< rvmap correction pairs | |
130 int rvmap_sel; ///< rvmap table selector | |
131 RVMapDesc *rv_map; ///< ptr to the RLE table for this band | |
132 int num_tiles; ///< number of tiles in this band | |
133 IVITile *tiles; ///< array of tile descriptors | |
134 void (*inv_transform)(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags); ///< inverse transform function pointer | |
135 void (*dc_transform) (const int32_t *in, int16_t *out, uint32_t pitch, int blk_size); ///< dc transform function pointer, it may be NULL | |
136 int is_2d_trans; ///< 1 indicates that the two-dimensional inverse transform is used | |
137 int32_t checksum; ///< for debug purposes | |
138 int checksum_present; | |
139 int bufsize; ///< band buffer size in bytes | |
140 const uint8_t *intra_base; ///< quantization matrix for intra blocks | |
141 const uint8_t *inter_base; ///< quantization matrix for inter blocks | |
142 const uint8_t *intra_scale; ///< quantization coefficient for intra blocks | |
143 const uint8_t *inter_scale; ///< quantization coefficient for inter blocks | |
144 } IVIBandDesc; | |
145 | |
146 | |
147 /** | |
148 * color plane (luma or chroma) information | |
149 */ | |
150 typedef struct { | |
151 uint16_t width; | |
152 uint16_t height; | |
153 uint8_t num_bands; ///< number of bands this plane subdivided into | |
154 IVIBandDesc *bands; ///< array of band descriptors | |
155 } IVIPlaneDesc; | |
156 | |
157 | |
158 typedef struct { | |
159 uint16_t pic_width; | |
160 uint16_t pic_height; | |
161 uint16_t chroma_width; | |
162 uint16_t chroma_height; | |
163 uint16_t tile_width; | |
164 uint16_t tile_height; | |
165 uint8_t luma_bands; | |
166 uint8_t chroma_bands; | |
167 } IVIPicConfig; | |
168 | |
169 /** compares some properties of two pictures */ | |
170 static inline int ivi_pic_config_cmp(IVIPicConfig *str1, IVIPicConfig *str2) | |
171 { | |
172 return (str1->pic_width != str2->pic_width || str1->pic_height != str2->pic_height || | |
173 str1->chroma_width != str2->chroma_width || str1->chroma_height != str2->chroma_height || | |
174 str1->tile_width != str2->tile_width || str1->tile_height != str2->tile_height || | |
175 str1->luma_bands != str2->luma_bands || str1->chroma_bands != str2->chroma_bands); | |
176 } | |
177 | |
178 /** calculate number of tiles in a stride */ | |
179 #define IVI_NUM_TILES(stride, tile_size) (((stride) + (tile_size) - 1) / (tile_size)) | |
180 | |
181 /** calculate number of macroblocks in a tile */ | |
182 #define IVI_MBs_PER_TILE(tile_width, tile_height, mb_size) \ | |
183 ((((tile_width) + (mb_size) - 1) / (mb_size)) * (((tile_height) + (mb_size) - 1) / (mb_size))) | |
184 | |
185 /** convert unsigned values into signed ones (the sign is in the LSB) */ | |
186 /* TODO: find a way to calculate this without the conditional using bit magic */ | |
187 #define IVI_TOSIGNED(val) (-(((val) >> 1) ^ -((val) & 1))) | |
188 | |
189 /** scales motion vector */ | |
190 static inline int ivi_scale_mv(int mv, int mv_scale) | |
191 { | |
192 return (mv + (mv > 0) + (mv_scale - 1)) >> mv_scale; | |
193 } | |
194 | |
195 /** | |
196 * Generates a huffman codebook from the given descriptor | |
197 * and converts it into the FFmpeg VLC table. | |
198 * | |
199 * @param cb [in] pointer to codebook descriptor | |
200 * @param vlc [out] where to place the generated VLC table | |
201 * @param flag [in] flag: 1 - for static or 0 for dynamic tables | |
202 * @return result code: 0 - OK, -1 = error (invalid codebook descriptor) | |
203 */ | |
204 int ff_ivi_create_huff_from_desc(const IVIHuffDesc *cb, VLC *vlc, int flag); | |
205 | |
206 /** | |
207 * Decodes a huffman codebook descriptor from the bitstream. | |
208 * | |
209 * @param gb [in,out] the GetBit context | |
210 * @param desc [out] ptr to descriptor to be filled with data | |
211 * @return selector indicating huffman table: | |
212 * (0...6 - predefined, 7 - custom one supplied with the bitstream) | |
213 */ | |
214 int ff_ivi_dec_huff_desc(GetBitContext *gb, IVIHuffDesc *desc); | |
215 | |
216 /** | |
217 * Compares two huffman codebook descriptors. | |
218 * | |
219 * @param desc1 [in] ptr to the 1st descriptor to compare | |
220 * @param desc2 [in] ptr to the 2nd descriptor to compare | |
221 * @return comparison result: 0 - equal, 1 - not equal | |
222 */ | |
223 int ff_ivi_huff_desc_cmp(const IVIHuffDesc *desc1, const IVIHuffDesc *desc2); | |
224 | |
225 /** | |
226 * Copies huffman codebook descriptors. | |
227 * | |
228 * @param dst [out] ptr to the destination descriptor | |
229 * @param src [in] ptr to the source descriptor | |
230 */ | |
231 void ff_ivi_huff_desc_copy(IVIHuffDesc *dst, const IVIHuffDesc *src); | |
232 | |
233 /** | |
234 * Initializes planes (prepares descriptors, allocates buffers etc). | |
235 * | |
236 * @param planes [in,out] pointer to the array of the plane descriptors | |
237 * @param cfg [in] pointer to the ivi_pic_config structure describing picture layout | |
238 * @return result code: 0 - OK | |
239 */ | |
240 int ff_ivi_init_planes(IVIPlaneDesc *planes, const IVIPicConfig *cfg); | |
241 | |
242 /** | |
243 * Frees planes, bands and macroblocks buffers. | |
244 * | |
245 * @param planes [in] pointer to the array of the plane descriptors | |
246 */ | |
247 void ff_ivi_free_buffers(IVIPlaneDesc *planes); | |
248 | |
249 /** | |
250 * Initializes tile and macroblock descriptors. | |
251 * | |
252 * @param planes [in,out] pointer to the array of the plane descriptors | |
253 * @param tile_width [in] tile width | |
254 * @param tile_height [in] tile height | |
255 * @return result code: 0 - OK | |
256 */ | |
257 int ff_ivi_init_tiles(IVIPlaneDesc *planes, int tile_width, int tile_height); | |
258 | |
259 /** | |
260 * Decodes size of the tile data. | |
261 * The size is stored as a variable-length field having the following format: | |
262 * if (tile_data_size < 255) than this field is only one byte long | |
263 * if (tile_data_size >= 255) than this field four is byte long: 0xFF X1 X2 X3 | |
264 * where X1-X3 is size of the tile data | |
265 * | |
266 * @param gb [in,out] the GetBit context | |
267 * @return size of the tile data in bytes | |
268 */ | |
269 int ff_ivi_dec_tile_data_size(GetBitContext *gb); | |
270 | |
271 /** | |
272 * Decodes block data: | |
273 * extracts huffman-coded transform coefficients from the bitstream, | |
274 * dequantizes them, applies inverse transform and motion compensation | |
275 * in order to reconstruct the picture. | |
276 * | |
277 * @param gb [in,out] the GetBit context | |
278 * @param band [in] pointer to the band descriptor | |
279 * @param tile [in] pointer to the tile descriptor | |
280 * @return result code: 0 - OK, -1 = error (corrupted blocks data) | |
281 */ | |
282 int ff_ivi_decode_blocks(GetBitContext *gb, IVIBandDesc *band, IVITile *tile); | |
283 | |
284 /** | |
285 * Handles empty tiles by performing data copying and motion | |
286 * compensation respectively. | |
287 * | |
288 * @param avctx [in] ptr to the AVCodecContext | |
289 * @param band [in] pointer to the band descriptor | |
290 * @param tile [in] pointer to the tile descriptor | |
291 * @param mv_scale [in] scaling factor for motion vectors | |
292 */ | |
293 void ff_ivi_process_empty_tile(AVCodecContext *avctx, IVIBandDesc *band, | |
294 IVITile *tile, int32_t mv_scale); | |
295 | |
296 /** | |
297 * Converts and outputs the current plane. | |
298 * This conversion is done by adding back the bias value of 128 | |
299 * (subtracted in the encoder) and clipping the result. | |
300 * | |
301 * @param plane [in] pointer to the descriptor of the plane being processed | |
302 * @param dst [out] pointer to the buffer receiving converted pixels | |
303 * @param dst_pitch [in] pitch for moving to the next y line | |
304 */ | |
305 void ff_ivi_output_plane(IVIPlaneDesc *plane, uint8_t *dst, int dst_pitch); | |
306 | |
307 #if IVI_DEBUG | |
308 /** | |
309 * Calculates band checksum from band data. | |
310 */ | |
311 uint16_t ivi_calc_band_checksum (IVIBandDesc *band); | |
312 | |
313 /** | |
314 * Verifies that band data lies in range. | |
315 */ | |
316 int ivi_check_band (IVIBandDesc *band, const uint8_t *ref, int pitch); | |
317 #endif | |
318 | |
319 #endif /* AVCODEC_IVI_COMMON_H */ |