3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Bitstream reader/writer inlined functions and constants- |
* - Bitstream reader/writer inlined functions and constants- |
5 |
* |
* |
6 |
* Copyright (C) 2001-2002 - Peter Ross <pross@xvid.org> |
* Copyright (C) 2001-2003 - Peter Ross <pross@xvid.org> |
7 |
* |
* |
8 |
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
9 |
* |
* |
21 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
* |
* |
|
* Under section 8 of the GNU General Public License, the copyright |
|
|
* holders of XVID explicitly forbid distribution in the following |
|
|
* countries: |
|
|
* |
|
|
* - Japan |
|
|
* - United States of America |
|
|
* |
|
|
* Linking XviD statically or dynamically with other modules is making a |
|
|
* combined work based on XviD. Thus, the terms and conditions of the |
|
|
* GNU General Public License cover the whole combination. |
|
|
* |
|
|
* As a special exception, the copyright holders of XviD give you |
|
|
* permission to link XviD with independent modules that communicate with |
|
|
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
|
|
* license terms of these independent modules, and to copy and distribute |
|
|
* the resulting combined work under terms of your choice, provided that |
|
|
* every copy of the combined work is accompanied by a complete copy of |
|
|
* the source code of XviD (the version of XviD used to produce the |
|
|
* combined work), being distributed under the terms of the GNU General |
|
|
* Public License plus this exception. An independent module is a module |
|
|
* which is not derived from or based on XviD. |
|
|
* |
|
|
* Note that people who make modified versions of XviD are not obligated |
|
|
* to grant this special exception for their modified versions; it is |
|
|
* their choice whether to do so. The GNU General Public License gives |
|
|
* permission to release a modified version without this exception; this |
|
|
* exception also makes it possible to release a modified version which |
|
|
* carries forward this exception. |
|
|
* |
|
24 |
* $Id$ |
* $Id$ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
47 |
#define GRPOFVOP_START_CODE 0x000001b3 |
#define GRPOFVOP_START_CODE 0x000001b3 |
48 |
/*#define VIDSESERR_ERROR_CODE 0x000001b4 */ |
/*#define VIDSESERR_ERROR_CODE 0x000001b4 */ |
49 |
#define VISOBJ_START_CODE 0x000001b5 |
#define VISOBJ_START_CODE 0x000001b5 |
50 |
/*#define SLICE_START_CODE 0x000001b7 */ |
#define VOP_START_CODE 0x000001b6 |
51 |
/*#define EXT_START_CODE 0x000001b8 */ |
/*#define STUFFING_START_CODE 0x000001c3 */ |
52 |
|
|
53 |
|
|
54 |
#define VISOBJ_TYPE_VIDEO 1 |
#define VISOBJ_TYPE_VIDEO 1 |
62 |
/*#define VIDOBJLAY_TYPE_SIMPLE_SCALABLE 2 */ |
/*#define VIDOBJLAY_TYPE_SIMPLE_SCALABLE 2 */ |
63 |
#define VIDOBJLAY_TYPE_CORE 3 |
#define VIDOBJLAY_TYPE_CORE 3 |
64 |
#define VIDOBJLAY_TYPE_MAIN 4 |
#define VIDOBJLAY_TYPE_MAIN 4 |
65 |
|
/*#define VIDOBJLAY_TYPE_NBIT 5 */ |
66 |
|
/*#define VIDOBJLAY_TYPE_ANIM_TEXT 6 */ |
67 |
|
/*#define VIDOBJLAY_TYPE_ANIM_MESH 7 */ |
68 |
|
/*#define VIDOBJLAY_TYPE_SIMPLE_FACE 8 */ |
69 |
|
/*#define VIDOBJLAY_TYPE_STILL_SCALABLE 9 */ |
70 |
|
#define VIDOBJLAY_TYPE_ART_SIMPLE 10 |
71 |
|
/*#define VIDOBJLAY_TYPE_CORE_SCALABLE 11 */ |
72 |
|
#define VIDOBJLAY_TYPE_ACE 12 |
73 |
|
/*#define VIDOBJLAY_TYPE_SIMPLE_FBA 13 */ |
74 |
|
|
75 |
|
|
76 |
/*#define VIDOBJLAY_AR_SQUARE 1 */ |
/*#define VIDOBJLAY_AR_SQUARE 1 */ |
86 |
#define VIDOBJLAY_SHAPE_BINARY_ONLY 2 |
#define VIDOBJLAY_SHAPE_BINARY_ONLY 2 |
87 |
#define VIDOBJLAY_SHAPE_GRAYSCALE 3 |
#define VIDOBJLAY_SHAPE_GRAYSCALE 3 |
88 |
|
|
89 |
#define VO_START_CODE 0x8 |
|
90 |
#define VOL_START_CODE 0x12 |
#define SPRITE_NONE 0 |
91 |
#define VOP_START_CODE 0x1b6 |
#define SPRITE_STATIC 1 |
92 |
|
#define SPRITE_GMC 2 |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
#define READ_MARKER() BitstreamSkip(bs, 1) |
#define READ_MARKER() BitstreamSkip(bs, 1) |
97 |
#define WRITE_MARKER() BitstreamPutBit(bs, 1) |
#define WRITE_MARKER() BitstreamPutBit(bs, 1) |
113 |
* Prototypes |
* Prototypes |
114 |
****************************************************************************/ |
****************************************************************************/ |
115 |
|
|
116 |
int |
int read_video_packet_header(Bitstream *bs, |
117 |
read_video_packet_header(Bitstream *bs, const int addbits, int * quant); |
DECODER * dec, |
118 |
|
const int addbits, |
119 |
|
int *quant, |
120 |
|
int *fcode_forward, |
121 |
|
int *fcode_backward, |
122 |
|
int *intra_dc_threshold); |
123 |
|
|
124 |
/* header stuff */ |
/* header stuff */ |
125 |
int BitstreamReadHeaders(Bitstream * bs, |
int BitstreamReadHeaders(Bitstream * bs, |
126 |
DECODER * dec, |
DECODER * dec, |
127 |
uint32_t * rounding, |
uint32_t * rounding, |
128 |
|
uint32_t * reduced_resolution, |
129 |
uint32_t * quant, |
uint32_t * quant, |
130 |
uint32_t * fcode_forward, |
uint32_t * fcode_forward, |
131 |
uint32_t * fcode_backward, |
uint32_t * fcode_backward, |
132 |
uint32_t * intra_dc_threshold); |
uint32_t * intra_dc_threshold, |
133 |
|
WARPPOINTS * gmc_warp); |
134 |
|
|
135 |
|
|
136 |
void BitstreamWriteVolHeader(Bitstream * const bs, |
void BitstreamWriteVolHeader(Bitstream * const bs, |
137 |
const MBParam * pParam, |
const MBParam * pParam, |
138 |
const FRAMEINFO * frame); |
const FRAMEINFO * const frame); |
139 |
|
|
140 |
void BitstreamWriteVopHeader(Bitstream * const bs, |
void BitstreamWriteVopHeader(Bitstream * const bs, |
141 |
const MBParam * pParam, |
const MBParam * pParam, |
142 |
const FRAMEINFO * frame, |
const FRAMEINFO * const frame, |
143 |
int vop_coded); |
int vop_coded); |
144 |
|
|
145 |
/***************************************************************************** |
void BitstreamWriteUserData(Bitstream * const bs, |
146 |
* Inlined functions |
uint8_t * data, |
147 |
****************************************************************************/ |
const int length); |
148 |
|
|
149 |
/* initialise bitstream structure */ |
/* initialise bitstream structure */ |
150 |
|
|
154 |
uint32_t length) |
uint32_t length) |
155 |
{ |
{ |
156 |
uint32_t tmp; |
uint32_t tmp; |
157 |
|
size_t bitpos; |
158 |
|
ptr_t adjbitstream = (ptr_t)bitstream; |
159 |
|
|
160 |
bs->start = bs->tail = (uint32_t *) bitstream; |
/* |
161 |
|
* Start the stream on a uint32_t boundary, by rounding down to the |
162 |
|
* previous uint32_t and skipping the intervening bytes. |
163 |
|
*/ |
164 |
|
bitpos = ((sizeof(uint32_t)-1) & (size_t)bitstream); |
165 |
|
adjbitstream = adjbitstream - bitpos; |
166 |
|
bs->start = bs->tail = (uint32_t *) adjbitstream; |
167 |
|
|
168 |
tmp = *(uint32_t *) bitstream; |
tmp = *bs->start; |
169 |
#ifndef ARCH_IS_BIG_ENDIAN |
#ifndef ARCH_IS_BIG_ENDIAN |
170 |
BSWAP(tmp); |
BSWAP(tmp); |
171 |
#endif |
#endif |
172 |
bs->bufa = tmp; |
bs->bufa = tmp; |
173 |
|
|
174 |
tmp = *((uint32_t *) bitstream + 1); |
tmp = *(bs->start + 1); |
175 |
#ifndef ARCH_IS_BIG_ENDIAN |
#ifndef ARCH_IS_BIG_ENDIAN |
176 |
BSWAP(tmp); |
BSWAP(tmp); |
177 |
#endif |
#endif |
178 |
bs->bufb = tmp; |
bs->bufb = tmp; |
179 |
|
|
180 |
bs->buf = 0; |
bs->buf = 0; |
181 |
bs->pos = 0; |
bs->pos = bs->initpos = bitpos*8; |
182 |
bs->length = length; |
bs->length = length; |
183 |
} |
} |
184 |
|
|
205 |
bs->bufb = tmp; |
bs->bufb = tmp; |
206 |
|
|
207 |
bs->buf = 0; |
bs->buf = 0; |
208 |
bs->pos = 0; |
bs->pos = bs->initpos; |
209 |
} |
} |
210 |
|
|
211 |
|
|
229 |
|
|
230 |
/* skip n bits forward in bitstream */ |
/* skip n bits forward in bitstream */ |
231 |
|
|
232 |
static void __inline |
static __inline void |
233 |
BitstreamSkip(Bitstream * const bs, |
BitstreamSkip(Bitstream * const bs, |
234 |
const uint32_t bits) |
const uint32_t bits) |
235 |
{ |
{ |
251 |
|
|
252 |
|
|
253 |
/* number of bits to next byte alignment */ |
/* number of bits to next byte alignment */ |
254 |
static uint32_t __inline |
static __inline uint32_t |
255 |
BitstreamNumBitsToByteAlign(Bitstream *bs) |
BitstreamNumBitsToByteAlign(Bitstream *bs) |
256 |
{ |
{ |
257 |
uint32_t n = (32 - bs->pos) % 8; |
uint32_t n = (32 - bs->pos) % 8; |
260 |
|
|
261 |
|
|
262 |
/* show nbits from next byte alignment */ |
/* show nbits from next byte alignment */ |
263 |
static uint32_t __inline |
static __inline uint32_t |
264 |
BitstreamShowBitsFromByteAlign(Bitstream *bs, int bits) |
BitstreamShowBitsFromByteAlign(Bitstream *bs, int bits) |
265 |
{ |
{ |
266 |
int bspos = bs->pos + BitstreamNumBitsToByteAlign(bs); |
int bspos = bs->pos + BitstreamNumBitsToByteAlign(bs); |
282 |
|
|
283 |
/* move forward to the next byte boundary */ |
/* move forward to the next byte boundary */ |
284 |
|
|
285 |
static void __inline |
static __inline void |
286 |
BitstreamByteAlign(Bitstream * const bs) |
BitstreamByteAlign(Bitstream * const bs) |
287 |
{ |
{ |
288 |
uint32_t remainder = bs->pos % 8; |
uint32_t remainder = bs->pos % 8; |
298 |
static uint32_t __inline |
static uint32_t __inline |
299 |
BitstreamPos(const Bitstream * const bs) |
BitstreamPos(const Bitstream * const bs) |
300 |
{ |
{ |
301 |
return 8 * ((ptr_t)bs->tail - (ptr_t)bs->start) + bs->pos; |
return((uint32_t)(8*((ptr_t)bs->tail - (ptr_t)bs->start) + bs->pos - bs->initpos)); |
302 |
} |
} |
303 |
|
|
304 |
|
|
305 |
/* flush the bitstream & return length (unit bytes) |
/* |
306 |
NOTE: assumes no futher bitstream functions will be called. |
* flush the bitstream & return length (unit bytes) |
307 |
|
* NOTE: assumes no futher bitstream functions will be called. |
308 |
*/ |
*/ |
309 |
|
|
310 |
static uint32_t __inline |
static uint32_t __inline |
311 |
BitstreamLength(Bitstream * const bs) |
BitstreamLength(Bitstream * const bs) |
312 |
{ |
{ |
313 |
uint32_t len = (ptr_t) bs->tail - (ptr_t) bs->start; |
uint32_t len = (uint32_t)((ptr_t)bs->tail - (ptr_t)bs->start); |
314 |
|
|
315 |
if (bs->pos) { |
if (bs->pos) { |
316 |
uint32_t b = bs->buf; |
uint32_t b = bs->buf; |
323 |
len += (bs->pos + 7) / 8; |
len += (bs->pos + 7) / 8; |
324 |
} |
} |
325 |
|
|
326 |
|
/* initpos is always on a byte boundary */ |
327 |
|
if (bs->initpos) |
328 |
|
len -= bs->initpos/8; |
329 |
|
|
330 |
return len; |
return len; |
331 |
} |
} |
332 |
|
|
351 |
} |
} |
352 |
} |
} |
353 |
|
|
|
|
|
|
/* pad bitstream to the next byte boundary */ |
|
|
|
|
|
static void __inline |
|
|
BitstreamPad(Bitstream * const bs) |
|
|
{ |
|
|
uint32_t remainder = bs->pos % 8; |
|
|
|
|
|
if (remainder) { |
|
|
BitstreamForward(bs, 8 - remainder); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
354 |
/* read n bits from bitstream */ |
/* read n bits from bitstream */ |
355 |
|
|
356 |
static uint32_t __inline |
static uint32_t __inline |
413 |
} |
} |
414 |
} |
} |
415 |
|
|
416 |
|
static const int stuffing_codes[8] = |
417 |
|
{ |
418 |
|
/* nbits stuffing code */ |
419 |
|
0, /* 1 0 */ |
420 |
|
1, /* 2 01 */ |
421 |
|
3, /* 3 011 */ |
422 |
|
7, /* 4 0111 */ |
423 |
|
0xf, /* 5 01111 */ |
424 |
|
0x1f, /* 6 011111 */ |
425 |
|
0x3f, /* 7 0111111 */ |
426 |
|
0x7f, /* 8 01111111 */ |
427 |
|
}; |
428 |
|
|
429 |
|
/* pad bitstream to the next byte boundary */ |
430 |
|
|
431 |
|
static void __inline |
432 |
|
BitstreamPad(Bitstream * const bs) |
433 |
|
{ |
434 |
|
int bits = 8 - (bs->pos % 8); |
435 |
|
if (bits < 8) |
436 |
|
BitstreamPutBits(bs, stuffing_codes[bits - 1], bits); |
437 |
|
} |
438 |
|
|
439 |
|
|
440 |
|
/* |
441 |
|
* pad bitstream to the next byte boundary |
442 |
|
* alway pad: even if currently at the byte boundary |
443 |
|
*/ |
444 |
|
|
445 |
|
static void __inline |
446 |
|
BitstreamPadAlways(Bitstream * const bs) |
447 |
|
{ |
448 |
|
int bits = 8 - (bs->pos % 8); |
449 |
|
BitstreamPutBits(bs, stuffing_codes[bits - 1], bits); |
450 |
|
} |
451 |
|
|
452 |
#endif /* _BITSTREAM_H_ */ |
#endif /* _BITSTREAM_H_ */ |