[svn] / branches / dev-api-3 / xvidcore / src / bitstream / bitstream.h Repository:
ViewVC logotype

Annotation of /branches/dev-api-3/xvidcore/src/bitstream/bitstream.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 631 - (view) (download)

1 : Isibaar 3 /******************************************************************************
2 :     * *
3 :     * This file is part of XviD, a free MPEG-4 video encoder/decoder *
4 :     * *
5 :     * XviD is an implementation of a part of one or more MPEG-4 Video tools *
6 :     * as specified in ISO/IEC 14496-2 standard. Those intending to use this *
7 :     * software module in hardware or software products are advised that its *
8 :     * use may infringe existing patents or copyrights, and any such use *
9 :     * would be at such party's own risk. The original developer of this *
10 :     * software module and his/her company, and subsequent editors and their *
11 :     * companies, will have no liability for use of this software or *
12 :     * modifications or derivatives thereof. *
13 :     * *
14 :     * XviD is free software; you can redistribute it and/or modify it *
15 :     * under the terms of the GNU General Public License as published by *
16 :     * the Free Software Foundation; either version 2 of the License, or *
17 :     * (at your option) any later version. *
18 :     * *
19 :     * XviD is distributed in the hope that it will be useful, but *
20 :     * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 :     * GNU General Public License for more details. *
23 :     * *
24 :     * You should have received a copy of the GNU General Public License *
25 :     * along with this program; if not, write to the Free Software *
26 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
27 :     * *
28 :     ******************************************************************************/
29 :    
30 :     /******************************************************************************
31 :     * *
32 :     * bitstream.h *
33 :     * *
34 :     * Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> *
35 :     * *
36 :     * For more information visit the XviD homepage: http://www.xvid.org *
37 :     * *
38 :     ******************************************************************************/
39 :    
40 :     /******************************************************************************
41 :     * *
42 :     * Revision history: *
43 :     * *
44 : suxen_drol 248 * 28.06.2002 addded BitstreamNumBitsToByteAlign() *
45 :     * BitstreamShowBitsFromByteAlign() *
46 :     * 26.03.2002 interlacing support - modified putvol/vopheaders paramters *
47 : Isibaar 3 * 04.03.2002 putbits speedup (Isibaar) *
48 :     * 03.03.2002 merged BITREADER and BITWRITER (Isibaar) *
49 :     * 16.12.2001 inital version *
50 :     * *
51 :     ******************************************************************************/
52 :    
53 :     #ifndef _BITSTREAM_H_
54 :     #define _BITSTREAM_H_
55 :    
56 :     #include "../portab.h"
57 :     #include "../decoder.h"
58 :     #include "../encoder.h"
59 :    
60 :     // comment any #defs we dont use
61 :    
62 :     #define VIDOBJ_START_CODE 0x00000100 /* ..0x0000011f */
63 :     #define VIDOBJLAY_START_CODE 0x00000120 /* ..0x0000012f */
64 :     #define VISOBJSEQ_START_CODE 0x000001b0
65 :     #define VISOBJSEQ_STOP_CODE 0x000001b1 /* ??? */
66 :     #define USERDATA_START_CODE 0x000001b2
67 :     #define GRPOFVOP_START_CODE 0x000001b3
68 : edgomez 195 //#define VIDSESERR_ERROR_CODE 0x000001b4
69 : Isibaar 3 #define VISOBJ_START_CODE 0x000001b5
70 : edgomez 195 //#define SLICE_START_CODE 0x000001b7
71 :     //#define EXT_START_CODE 0x000001b8
72 : Isibaar 3
73 :    
74 :     #define VISOBJ_TYPE_VIDEO 1
75 : edgomez 195 //#define VISOBJ_TYPE_STILLTEXTURE 2
76 :     //#define VISOBJ_TYPE_MESH 3
77 :     //#define VISOBJ_TYPE_FBA 4
78 :     //#define VISOBJ_TYPE_3DMESH 5
79 : Isibaar 3
80 :    
81 :     #define VIDOBJLAY_TYPE_SIMPLE 1
82 : edgomez 195 //#define VIDOBJLAY_TYPE_SIMPLE_SCALABLE 2
83 : Isibaar 3 #define VIDOBJLAY_TYPE_CORE 3
84 :     #define VIDOBJLAY_TYPE_MAIN 4
85 : suxen_drol 631 //#define VIDOBJLAY_TYPE_NBIT 5
86 :     //#define VIDOBJLAY_TYPE_ANIM_TEXT 6
87 :     //#define VIDOBJLAY_TYPE_ANIM_MESH 7
88 :     //#define VIDOBJLAY_TYPE_SIMPLE_FACE 8
89 :     //#define VIDOBJLAY_TYPE_STILL_SCALABLE 9
90 :     #define VIDOBJLAY_TYPE_ART_SIMPLE 10
91 :     //#define VIDOBJLAY_TYPE_CORE_SCALABLE 11
92 :     #define VIDOBJLAY_TYPE_ACE 12
93 :     //#define VIDOBJLAY_TYPE_SIMPLE_FBA 13
94 : Isibaar 3
95 :    
96 : edgomez 195 //#define VIDOBJLAY_AR_SQUARE 1
97 :     //#define VIDOBJLAY_AR_625TYPE_43 2
98 :     //#define VIDOBJLAY_AR_525TYPE_43 3
99 :     //#define VIDOBJLAY_AR_625TYPE_169 8
100 :     //#define VIDOBJLAY_AR_525TYPE_169 9
101 : Isibaar 3 #define VIDOBJLAY_AR_EXTPAR 15
102 :    
103 :    
104 :     #define VIDOBJLAY_SHAPE_RECTANGULAR 0
105 :     #define VIDOBJLAY_SHAPE_BINARY 1
106 :     #define VIDOBJLAY_SHAPE_BINARY_ONLY 2
107 :     #define VIDOBJLAY_SHAPE_GRAYSCALE 3
108 :    
109 : suxen_drol 631
110 :     #define SPRITE_NONE 0
111 :     #define SPRITE_STATIC 1
112 :     #define SPRITE_GMC 2
113 :    
114 :    
115 : Isibaar 3 #define VO_START_CODE 0x8
116 :     #define VOL_START_CODE 0x12
117 :     #define VOP_START_CODE 0x1b6
118 :    
119 :     #define READ_MARKER() BitstreamSkip(bs, 1)
120 :     #define WRITE_MARKER() BitstreamPutBit(bs, 1)
121 :    
122 :     // vop coding types
123 :     // intra, prediction, backward, sprite, not_coded
124 :     #define I_VOP 0
125 :     #define P_VOP 1
126 :     #define B_VOP 2
127 :     #define S_VOP 3
128 :     #define N_VOP 4
129 :    
130 : suxen_drol 248 // resync-specific
131 :     #define NUMBITS_VP_RESYNC_MARKER 17
132 :     #define RESYNC_MARKER 1
133 : Isibaar 3
134 : suxen_drol 248
135 : suxen_drol 631 int read_video_packet_header(Bitstream *bs,
136 :     DECODER * dec,
137 :     const int addbits,
138 :     int * quant,
139 :     int * fcode_forward,
140 :     int * fcode_backward,
141 :     int * intra_dc_threshold);
142 : suxen_drol 248
143 :    
144 : Isibaar 3 // header stuff
145 : edgomez 195 int BitstreamReadHeaders(Bitstream * bs,
146 :     DECODER * dec,
147 :     uint32_t * rounding,
148 : suxen_drol 631 uint32_t * reduced_resolution,
149 : edgomez 195 uint32_t * quant,
150 :     uint32_t * fcode_forward,
151 :     uint32_t * fcode_backward,
152 : suxen_drol 631 uint32_t * intra_dc_threshold,
153 :     VECTOR * gmc_mv);
154 : Isibaar 3
155 :    
156 :     void BitstreamWriteVolHeader(Bitstream * const bs,
157 : edgomez 195 const MBParam * pParam,
158 : chl 619 const FRAMEINFO * const frame);
159 : Isibaar 3
160 :     void BitstreamWriteVopHeader(Bitstream * const bs,
161 : edgomez 195 const MBParam * pParam,
162 : chl 619 const FRAMEINFO * const frame,
163 : suxen_drol 229 int vop_coded);
164 : Isibaar 3
165 : suxen_drol 229 void BitstreamWriteUserData(Bitstream * const bs,
166 :     uint8_t * data,
167 :     const int length);
168 :    
169 : Isibaar 3 /* initialise bitstream structure */
170 :    
171 : edgomez 195 static void __inline
172 :     BitstreamInit(Bitstream * const bs,
173 :     void *const bitstream,
174 :     uint32_t length)
175 : Isibaar 3 {
176 :     uint32_t tmp;
177 :    
178 : edgomez 195 bs->start = bs->tail = (uint32_t *) bitstream;
179 : Isibaar 3
180 : edgomez 195 tmp = *(uint32_t *) bitstream;
181 : Isibaar 3 #ifndef ARCH_IS_BIG_ENDIAN
182 :     BSWAP(tmp);
183 :     #endif
184 :     bs->bufa = tmp;
185 :    
186 : edgomez 195 tmp = *((uint32_t *) bitstream + 1);
187 : Isibaar 3 #ifndef ARCH_IS_BIG_ENDIAN
188 :     BSWAP(tmp);
189 :     #endif
190 :     bs->bufb = tmp;
191 :    
192 :     bs->buf = 0;
193 :     bs->pos = 0;
194 :     bs->length = length;
195 :     }
196 :    
197 :    
198 :     /* reset bitstream state */
199 :    
200 : edgomez 195 static void __inline
201 :     BitstreamReset(Bitstream * const bs)
202 : Isibaar 3 {
203 :     uint32_t tmp;
204 :    
205 :     bs->tail = bs->start;
206 :    
207 :     tmp = *bs->start;
208 :     #ifndef ARCH_IS_BIG_ENDIAN
209 :     BSWAP(tmp);
210 :     #endif
211 :     bs->bufa = tmp;
212 :    
213 :     tmp = *(bs->start + 1);
214 :     #ifndef ARCH_IS_BIG_ENDIAN
215 :     BSWAP(tmp);
216 :     #endif
217 :     bs->bufb = tmp;
218 :    
219 :     bs->buf = 0;
220 : edgomez 195 bs->pos = 0;
221 : Isibaar 3 }
222 :    
223 :    
224 :     /* reads n bits from bitstream without changing the stream pos */
225 :    
226 : edgomez 195 static uint32_t __inline
227 :     BitstreamShowBits(Bitstream * const bs,
228 :     const uint32_t bits)
229 : Isibaar 3 {
230 :     int nbit = (bits + bs->pos) - 32;
231 : edgomez 195
232 :     if (nbit > 0) {
233 :     return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) | (bs->
234 :     bufb >> (32 -
235 :     nbit));
236 :     } else {
237 : Isibaar 3 return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits);
238 :     }
239 :     }
240 :    
241 :    
242 :     /* skip n bits forward in bitstream */
243 :    
244 : edgomez 195 static __inline void
245 :     BitstreamSkip(Bitstream * const bs,
246 :     const uint32_t bits)
247 : Isibaar 3 {
248 :     bs->pos += bits;
249 :    
250 : edgomez 195 if (bs->pos >= 32) {
251 : Isibaar 3 uint32_t tmp;
252 :    
253 :     bs->bufa = bs->bufb;
254 : edgomez 195 tmp = *((uint32_t *) bs->tail + 2);
255 : Isibaar 3 #ifndef ARCH_IS_BIG_ENDIAN
256 :     BSWAP(tmp);
257 :     #endif
258 :     bs->bufb = tmp;
259 :     bs->tail++;
260 :     bs->pos -= 32;
261 :     }
262 :     }
263 :    
264 :    
265 : suxen_drol 248 // number of bits to next byte alignment
266 :     static __inline uint32_t
267 :     BitstreamNumBitsToByteAlign(Bitstream *bs)
268 :     {
269 :     uint32_t n = (32 - bs->pos) % 8;
270 :     return n == 0 ? 8 : n;
271 :     }
272 :    
273 :    
274 :     // show nbits from next byte alignment
275 :     static __inline uint32_t
276 :     BitstreamShowBitsFromByteAlign(Bitstream *bs, int bits)
277 :     {
278 :     int bspos = bs->pos + BitstreamNumBitsToByteAlign(bs);
279 :     int nbit = (bits + bspos) - 32;
280 :    
281 :     if (bspos >= 32) {
282 :     return bs->bufb >> (32 - nbit);
283 :     } else if (nbit > 0) {
284 :     return ((bs->bufa & (0xffffffff >> bspos)) << nbit) | (bs->
285 :     bufb >> (32 -
286 :     nbit));
287 :     } else {
288 :     return (bs->bufa & (0xffffffff >> bspos)) >> (32 - bspos - bits);
289 :     }
290 :    
291 :     }
292 :    
293 :    
294 :    
295 : Isibaar 3 /* move forward to the next byte boundary */
296 :    
297 : edgomez 195 static __inline void
298 :     BitstreamByteAlign(Bitstream * const bs)
299 : Isibaar 3 {
300 :     uint32_t remainder = bs->pos % 8;
301 : edgomez 195
302 :     if (remainder) {
303 : Isibaar 3 BitstreamSkip(bs, 8 - remainder);
304 :     }
305 :     }
306 :    
307 :    
308 :     /* bitstream length (unit bits) */
309 :    
310 : edgomez 195 static uint32_t __inline
311 :     BitstreamPos(const Bitstream * const bs)
312 : Isibaar 3 {
313 : Isibaar 209 return 8 * ((ptr_t)bs->tail - (ptr_t)bs->start) + bs->pos;
314 : Isibaar 3 }
315 :    
316 :    
317 :     /* flush the bitstream & return length (unit bytes)
318 :     NOTE: assumes no futher bitstream functions will be called.
319 :     */
320 :    
321 : edgomez 195 static uint32_t __inline
322 :     BitstreamLength(Bitstream * const bs)
323 : Isibaar 3 {
324 : Isibaar 209 uint32_t len = (ptr_t) bs->tail - (ptr_t) bs->start;
325 : Isibaar 3
326 : edgomez 195 if (bs->pos) {
327 : Isibaar 3 uint32_t b = bs->buf;
328 : edgomez 195
329 : Isibaar 3 #ifndef ARCH_IS_BIG_ENDIAN
330 :     BSWAP(b);
331 :     #endif
332 :     *bs->tail = b;
333 :    
334 :     len += (bs->pos + 7) / 8;
335 : edgomez 195 }
336 : Isibaar 3
337 :     return len;
338 :     }
339 :    
340 :    
341 :     /* move bitstream position forward by n bits and write out buffer if needed */
342 :    
343 : edgomez 195 static void __inline
344 :     BitstreamForward(Bitstream * const bs,
345 :     const uint32_t bits)
346 : Isibaar 3 {
347 : edgomez 195 bs->pos += bits;
348 : Isibaar 3
349 : edgomez 195 if (bs->pos >= 32) {
350 : Isibaar 3 uint32_t b = bs->buf;
351 : edgomez 195
352 : Isibaar 3 #ifndef ARCH_IS_BIG_ENDIAN
353 :     BSWAP(b);
354 :     #endif
355 :     *bs->tail++ = b;
356 :     bs->buf = 0;
357 :     bs->pos -= 32;
358 : edgomez 195 }
359 : Isibaar 3 }
360 :    
361 :    
362 :     /* read n bits from bitstream */
363 :    
364 : edgomez 195 static uint32_t __inline
365 :     BitstreamGetBits(Bitstream * const bs,
366 :     const uint32_t n)
367 : Isibaar 3 {
368 :     uint32_t ret = BitstreamShowBits(bs, n);
369 : edgomez 195
370 : Isibaar 3 BitstreamSkip(bs, n);
371 :     return ret;
372 :     }
373 :    
374 :    
375 :     /* read single bit from bitstream */
376 :    
377 : edgomez 195 static uint32_t __inline
378 :     BitstreamGetBit(Bitstream * const bs)
379 : Isibaar 3 {
380 :     return BitstreamGetBits(bs, 1);
381 :     }
382 :    
383 :    
384 :     /* write single bit to bitstream */
385 :    
386 : edgomez 195 static void __inline
387 :     BitstreamPutBit(Bitstream * const bs,
388 :     const uint32_t bit)
389 : Isibaar 3 {
390 : edgomez 195 if (bit)
391 : Isibaar 3 bs->buf |= (0x80000000 >> bs->pos);
392 :    
393 : edgomez 195 BitstreamForward(bs, 1);
394 : Isibaar 3 }
395 :    
396 :    
397 :     /* write n bits to bitstream */
398 :    
399 : edgomez 195 static void __inline
400 :     BitstreamPutBits(Bitstream * const bs,
401 :     const uint32_t value,
402 :     const uint32_t size)
403 : Isibaar 3 {
404 :     uint32_t shift = 32 - bs->pos - size;
405 :    
406 :     if (shift <= 32) {
407 :     bs->buf |= value << shift;
408 :     BitstreamForward(bs, size);
409 :     } else {
410 :     uint32_t remainder;
411 :    
412 :     shift = size - (32 - bs->pos);
413 :     bs->buf |= value >> shift;
414 :     BitstreamForward(bs, size - shift);
415 :     remainder = shift;
416 :    
417 :     shift = 32 - shift;
418 : edgomez 195
419 : Isibaar 3 bs->buf |= value << shift;
420 :     BitstreamForward(bs, remainder);
421 :     }
422 :     }
423 :    
424 : suxen_drol 577
425 :     static const int stuffing_codes[8] =
426 :     {
427 :     /* nbits stuffing code */
428 :     0, /* 1 0 */
429 :     1, /* 2 01 */
430 :     3, /* 3 011 */
431 :     7, /* 4 0111 */
432 :     0xf, /* 5 01111 */
433 :     0x1f, /* 6 011111 */
434 :     0x3f, /* 7 0111111 */
435 :     0x7f, /* 8 01111111 */
436 :     };
437 :    
438 :     /* pad bitstream to the next byte boundary */
439 :    
440 :     static void __inline
441 :     BitstreamPad(Bitstream * const bs)
442 :     {
443 :     int bits = 8 - (bs->pos % 8);
444 :     if (bits < 8)
445 :     {
446 :     BitstreamPutBits(bs, stuffing_codes[bits - 1], bits);
447 :     }
448 :     }
449 :    
450 :    
451 :     /* pad bitstream to the next byte boundary
452 :     alway pad: even if currently at the byte boundary */
453 :    
454 :     static void __inline
455 :     BitstreamPadAlways(Bitstream * const bs)
456 :     {
457 :     int bits = 8 - (bs->pos % 8);
458 :     BitstreamPutBits(bs, stuffing_codes[bits - 1], bits);
459 :     }
460 :    
461 :    
462 : edgomez 195 #endif /* _BITSTREAM_H_ */

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4