[cvs] / xvidcore / src / bitstream / bitstream.h Repository:
ViewVC logotype

Annotation of /xvidcore/src/bitstream/bitstream.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17 - (view) (download)

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

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