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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 890 - (view) (download)

1 : edgomez 851 /******************************************************************************
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 : Isibaar 3
30 : edgomez 851 /******************************************************************************
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 :     * 28.06.2002 addded BitstreamNumBitsToByteAlign() *
45 :     * BitstreamShowBitsFromByteAlign() *
46 :     * 26.03.2002 interlacing support - modified putvol/vopheaders paramters *
47 :     * 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 : Isibaar 3 #ifndef _BITSTREAM_H_
54 :     #define _BITSTREAM_H_
55 :    
56 :     #include "../portab.h"
57 :     #include "../decoder.h"
58 :     #include "../encoder.h"
59 :    
60 : edgomez 851 // comment any #defs we dont use
61 : edgomez 466
62 : edgomez 851 /* start codes */
63 : edgomez 466
64 : Isibaar 3 #define VIDOBJ_START_CODE 0x00000100 /* ..0x0000011f */
65 :     #define VIDOBJLAY_START_CODE 0x00000120 /* ..0x0000012f */
66 :     #define VISOBJSEQ_START_CODE 0x000001b0
67 :     #define VISOBJSEQ_STOP_CODE 0x000001b1 /* ??? */
68 :     #define USERDATA_START_CODE 0x000001b2
69 :     #define GRPOFVOP_START_CODE 0x000001b3
70 : edgomez 851 //#define VIDSESERR_ERROR_CODE 0x000001b4
71 : Isibaar 3 #define VISOBJ_START_CODE 0x000001b5
72 : edgomez 851 #define VOP_START_CODE 0x000001b6
73 :     //#define STUFFING_START_CODE 0x000001c3
74 : Isibaar 3
75 :    
76 :     #define VISOBJ_TYPE_VIDEO 1
77 : edgomez 851 //#define VISOBJ_TYPE_STILLTEXTURE 2
78 :     //#define VISOBJ_TYPE_MESH 3
79 :     //#define VISOBJ_TYPE_FBA 4
80 :     //#define VISOBJ_TYPE_3DMESH 5
81 : Isibaar 3
82 :    
83 :     #define VIDOBJLAY_TYPE_SIMPLE 1
84 : edgomez 851 //#define VIDOBJLAY_TYPE_SIMPLE_SCALABLE 2
85 : Isibaar 3 #define VIDOBJLAY_TYPE_CORE 3
86 :     #define VIDOBJLAY_TYPE_MAIN 4
87 : edgomez 851 //#define VIDOBJLAY_TYPE_NBIT 5
88 :     //#define VIDOBJLAY_TYPE_ANIM_TEXT 6
89 :     //#define VIDOBJLAY_TYPE_ANIM_MESH 7
90 :     //#define VIDOBJLAY_TYPE_SIMPLE_FACE 8
91 :     //#define VIDOBJLAY_TYPE_STILL_SCALABLE 9
92 :     #define VIDOBJLAY_TYPE_ART_SIMPLE 10
93 :     //#define VIDOBJLAY_TYPE_CORE_SCALABLE 11
94 :     #define VIDOBJLAY_TYPE_ACE 12
95 :     //#define VIDOBJLAY_TYPE_SIMPLE_FBA 13
96 : Isibaar 3
97 :    
98 : edgomez 851 //#define VIDOBJLAY_AR_SQUARE 1
99 :     //#define VIDOBJLAY_AR_625TYPE_43 2
100 :     //#define VIDOBJLAY_AR_525TYPE_43 3
101 :     //#define VIDOBJLAY_AR_625TYPE_169 8
102 :     //#define VIDOBJLAY_AR_525TYPE_169 9
103 : Isibaar 3 #define VIDOBJLAY_AR_EXTPAR 15
104 :    
105 :    
106 :     #define VIDOBJLAY_SHAPE_RECTANGULAR 0
107 :     #define VIDOBJLAY_SHAPE_BINARY 1
108 :     #define VIDOBJLAY_SHAPE_BINARY_ONLY 2
109 :     #define VIDOBJLAY_SHAPE_GRAYSCALE 3
110 :    
111 :    
112 : edgomez 851 #define SPRITE_NONE 0
113 :     #define SPRITE_STATIC 1
114 :     #define SPRITE_GMC 2
115 :    
116 :    
117 :    
118 : Isibaar 3 #define READ_MARKER() BitstreamSkip(bs, 1)
119 :     #define WRITE_MARKER() BitstreamPutBit(bs, 1)
120 :    
121 : edgomez 851 // vop coding types
122 :     // intra, prediction, backward, sprite, not_coded
123 : Isibaar 3 #define I_VOP 0
124 :     #define P_VOP 1
125 :     #define B_VOP 2
126 :     #define S_VOP 3
127 :     #define N_VOP 4
128 :    
129 : edgomez 851 // resync-specific
130 : suxen_drol 248 #define NUMBITS_VP_RESYNC_MARKER 17
131 :     #define RESYNC_MARKER 1
132 : Isibaar 3
133 : suxen_drol 248
134 : edgomez 851 int read_video_packet_header(Bitstream *bs,
135 :     DECODER * dec,
136 :     const int addbits,
137 :     int * quant,
138 :     int * fcode_forward,
139 :     int * fcode_backward,
140 :     int * intra_dc_threshold);
141 : edgomez 466
142 : suxen_drol 248
143 : edgomez 851 // header stuff
144 : edgomez 195 int BitstreamReadHeaders(Bitstream * bs,
145 :     DECODER * dec,
146 :     uint32_t * rounding,
147 : edgomez 851 uint32_t * reduced_resolution,
148 : edgomez 195 uint32_t * quant,
149 :     uint32_t * fcode_forward,
150 :     uint32_t * fcode_backward,
151 : edgomez 851 uint32_t * intra_dc_threshold,
152 :     WARPPOINTS * gmc_warp);
153 : Isibaar 3
154 :    
155 :     void BitstreamWriteVolHeader(Bitstream * const bs,
156 : suxen_drol 890 const MBParam * pParam);
157 : Isibaar 3
158 :     void BitstreamWriteVopHeader(Bitstream * const bs,
159 : edgomez 195 const MBParam * pParam,
160 : edgomez 851 const FRAMEINFO * const frame,
161 : suxen_drol 229 int vop_coded);
162 : Isibaar 3
163 : edgomez 851 void BitstreamWriteUserData(Bitstream * const bs,
164 :     uint8_t * data,
165 :     const int length);
166 : edgomez 466
167 : Isibaar 3 /* initialise bitstream structure */
168 :    
169 : edgomez 195 static void __inline
170 :     BitstreamInit(Bitstream * const bs,
171 :     void *const bitstream,
172 :     uint32_t length)
173 : Isibaar 3 {
174 :     uint32_t tmp;
175 :    
176 : edgomez 195 bs->start = bs->tail = (uint32_t *) bitstream;
177 : Isibaar 3
178 : edgomez 195 tmp = *(uint32_t *) bitstream;
179 : Isibaar 3 #ifndef ARCH_IS_BIG_ENDIAN
180 :     BSWAP(tmp);
181 :     #endif
182 :     bs->bufa = tmp;
183 :    
184 : edgomez 195 tmp = *((uint32_t *) bitstream + 1);
185 : Isibaar 3 #ifndef ARCH_IS_BIG_ENDIAN
186 :     BSWAP(tmp);
187 :     #endif
188 :     bs->bufb = tmp;
189 :    
190 :     bs->buf = 0;
191 :     bs->pos = 0;
192 :     bs->length = length;
193 :     }
194 :    
195 :    
196 :     /* reset bitstream state */
197 :    
198 : edgomez 195 static void __inline
199 :     BitstreamReset(Bitstream * const bs)
200 : Isibaar 3 {
201 :     uint32_t tmp;
202 :    
203 :     bs->tail = bs->start;
204 :    
205 :     tmp = *bs->start;
206 :     #ifndef ARCH_IS_BIG_ENDIAN
207 :     BSWAP(tmp);
208 :     #endif
209 :     bs->bufa = tmp;
210 :    
211 :     tmp = *(bs->start + 1);
212 :     #ifndef ARCH_IS_BIG_ENDIAN
213 :     BSWAP(tmp);
214 :     #endif
215 :     bs->bufb = tmp;
216 :    
217 :     bs->buf = 0;
218 : edgomez 195 bs->pos = 0;
219 : Isibaar 3 }
220 :    
221 :    
222 :     /* reads n bits from bitstream without changing the stream pos */
223 :    
224 : edgomez 195 static uint32_t __inline
225 :     BitstreamShowBits(Bitstream * const bs,
226 :     const uint32_t bits)
227 : Isibaar 3 {
228 :     int nbit = (bits + bs->pos) - 32;
229 : edgomez 195
230 :     if (nbit > 0) {
231 :     return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) | (bs->
232 :     bufb >> (32 -
233 :     nbit));
234 :     } else {
235 : Isibaar 3 return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits);
236 :     }
237 :     }
238 :    
239 :    
240 :     /* skip n bits forward in bitstream */
241 :    
242 : edgomez 851 static __inline void
243 : edgomez 195 BitstreamSkip(Bitstream * const bs,
244 :     const uint32_t bits)
245 : Isibaar 3 {
246 :     bs->pos += bits;
247 :    
248 : edgomez 195 if (bs->pos >= 32) {
249 : Isibaar 3 uint32_t tmp;
250 :    
251 :     bs->bufa = bs->bufb;
252 : edgomez 195 tmp = *((uint32_t *) bs->tail + 2);
253 : Isibaar 3 #ifndef ARCH_IS_BIG_ENDIAN
254 :     BSWAP(tmp);
255 :     #endif
256 :     bs->bufb = tmp;
257 :     bs->tail++;
258 :     bs->pos -= 32;
259 :     }
260 :     }
261 :    
262 :    
263 : edgomez 851 // number of bits to next byte alignment
264 :     static __inline uint32_t
265 : suxen_drol 248 BitstreamNumBitsToByteAlign(Bitstream *bs)
266 :     {
267 :     uint32_t n = (32 - bs->pos) % 8;
268 :     return n == 0 ? 8 : n;
269 :     }
270 :    
271 :    
272 : edgomez 851 // show nbits from next byte alignment
273 :     static __inline uint32_t
274 : suxen_drol 248 BitstreamShowBitsFromByteAlign(Bitstream *bs, int bits)
275 :     {
276 :     int bspos = bs->pos + BitstreamNumBitsToByteAlign(bs);
277 :     int nbit = (bits + bspos) - 32;
278 :    
279 :     if (bspos >= 32) {
280 :     return bs->bufb >> (32 - nbit);
281 :     } else if (nbit > 0) {
282 :     return ((bs->bufa & (0xffffffff >> bspos)) << nbit) | (bs->
283 :     bufb >> (32 -
284 :     nbit));
285 :     } else {
286 :     return (bs->bufa & (0xffffffff >> bspos)) >> (32 - bspos - bits);
287 :     }
288 :    
289 :     }
290 :    
291 :    
292 :    
293 : Isibaar 3 /* move forward to the next byte boundary */
294 :    
295 : edgomez 851 static __inline void
296 : edgomez 195 BitstreamByteAlign(Bitstream * const bs)
297 : Isibaar 3 {
298 :     uint32_t remainder = bs->pos % 8;
299 : edgomez 195
300 :     if (remainder) {
301 : Isibaar 3 BitstreamSkip(bs, 8 - remainder);
302 :     }
303 :     }
304 :    
305 :    
306 :     /* bitstream length (unit bits) */
307 :    
308 : edgomez 195 static uint32_t __inline
309 :     BitstreamPos(const Bitstream * const bs)
310 : Isibaar 3 {
311 : edgomez 851 return 8 * ((ptr_t)bs->tail - (ptr_t)bs->start) + bs->pos;
312 : Isibaar 3 }
313 :    
314 :    
315 :     /* flush the bitstream & return length (unit bytes)
316 :     NOTE: assumes no futher bitstream functions will be called.
317 :     */
318 :    
319 : edgomez 195 static uint32_t __inline
320 :     BitstreamLength(Bitstream * const bs)
321 : Isibaar 3 {
322 : edgomez 851 uint32_t len = (ptr_t) bs->tail - (ptr_t) bs->start;
323 : Isibaar 3
324 : edgomez 195 if (bs->pos) {
325 : Isibaar 3 uint32_t b = bs->buf;
326 : edgomez 195
327 : Isibaar 3 #ifndef ARCH_IS_BIG_ENDIAN
328 :     BSWAP(b);
329 :     #endif
330 :     *bs->tail = b;
331 :    
332 :     len += (bs->pos + 7) / 8;
333 : edgomez 195 }
334 : Isibaar 3
335 :     return len;
336 :     }
337 :    
338 :    
339 :     /* move bitstream position forward by n bits and write out buffer if needed */
340 :    
341 : edgomez 195 static void __inline
342 :     BitstreamForward(Bitstream * const bs,
343 :     const uint32_t bits)
344 : Isibaar 3 {
345 : edgomez 195 bs->pos += bits;
346 : Isibaar 3
347 : edgomez 195 if (bs->pos >= 32) {
348 : Isibaar 3 uint32_t b = bs->buf;
349 : edgomez 195
350 : Isibaar 3 #ifndef ARCH_IS_BIG_ENDIAN
351 :     BSWAP(b);
352 :     #endif
353 :     *bs->tail++ = b;
354 :     bs->buf = 0;
355 :     bs->pos -= 32;
356 : edgomez 195 }
357 : Isibaar 3 }
358 :    
359 :    
360 :     /* read n bits from bitstream */
361 :    
362 : edgomez 195 static uint32_t __inline
363 :     BitstreamGetBits(Bitstream * const bs,
364 :     const uint32_t n)
365 : Isibaar 3 {
366 :     uint32_t ret = BitstreamShowBits(bs, n);
367 : edgomez 195
368 : Isibaar 3 BitstreamSkip(bs, n);
369 :     return ret;
370 :     }
371 :    
372 :    
373 :     /* read single bit from bitstream */
374 :    
375 : edgomez 195 static uint32_t __inline
376 :     BitstreamGetBit(Bitstream * const bs)
377 : Isibaar 3 {
378 :     return BitstreamGetBits(bs, 1);
379 :     }
380 :    
381 :    
382 :     /* write single bit to bitstream */
383 :    
384 : edgomez 195 static void __inline
385 :     BitstreamPutBit(Bitstream * const bs,
386 :     const uint32_t bit)
387 : Isibaar 3 {
388 : edgomez 195 if (bit)
389 : Isibaar 3 bs->buf |= (0x80000000 >> bs->pos);
390 :    
391 : edgomez 195 BitstreamForward(bs, 1);
392 : Isibaar 3 }
393 :    
394 :    
395 :     /* write n bits to bitstream */
396 :    
397 : edgomez 195 static void __inline
398 :     BitstreamPutBits(Bitstream * const bs,
399 :     const uint32_t value,
400 :     const uint32_t size)
401 : Isibaar 3 {
402 :     uint32_t shift = 32 - bs->pos - size;
403 :    
404 :     if (shift <= 32) {
405 :     bs->buf |= value << shift;
406 :     BitstreamForward(bs, size);
407 :     } else {
408 :     uint32_t remainder;
409 :    
410 :     shift = size - (32 - bs->pos);
411 :     bs->buf |= value >> shift;
412 :     BitstreamForward(bs, size - shift);
413 :     remainder = shift;
414 :    
415 :     shift = 32 - shift;
416 : edgomez 195
417 : Isibaar 3 bs->buf |= value << shift;
418 :     BitstreamForward(bs, remainder);
419 :     }
420 :     }
421 :    
422 : edgomez 851
423 :     static const int stuffing_codes[8] =
424 :     {
425 :     /* nbits stuffing code */
426 :     0, /* 1 0 */
427 :     1, /* 2 01 */
428 :     3, /* 3 011 */
429 :     7, /* 4 0111 */
430 :     0xf, /* 5 01111 */
431 :     0x1f, /* 6 011111 */
432 :     0x3f, /* 7 0111111 */
433 :     0x7f, /* 8 01111111 */
434 :     };
435 :    
436 :     /* pad bitstream to the next byte boundary */
437 :    
438 :     static void __inline
439 :     BitstreamPad(Bitstream * const bs)
440 :     {
441 :     int bits = 8 - (bs->pos % 8);
442 :     if (bits < 8)
443 :     {
444 :     BitstreamPutBits(bs, stuffing_codes[bits - 1], bits);
445 :     }
446 :     }
447 :    
448 :    
449 :     /* pad bitstream to the next byte boundary
450 :     alway pad: even if currently at the byte boundary */
451 :    
452 :     static void __inline
453 :     BitstreamPadAlways(Bitstream * const bs)
454 :     {
455 :     int bits = 8 - (bs->pos % 8);
456 :     BitstreamPutBits(bs, stuffing_codes[bits - 1], bits);
457 :     }
458 :    
459 :    
460 :     #endif /* _BITSTREAM_H_ */

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