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 |
****************************************************************************/ |
****************************************************************************/ |
28 |
#ifndef _BITSTREAM_H_ |
#ifndef _BITSTREAM_H_ |
29 |
#define _BITSTREAM_H_ |
#define _BITSTREAM_H_ |
30 |
|
|
|
#include <stdio.h> |
|
|
|
|
31 |
#include "../portab.h" |
#include "../portab.h" |
32 |
#include "../decoder.h" |
#include "../decoder.h" |
33 |
#include "../encoder.h" |
#include "../encoder.h" |
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 |
|
#define VIDOBJLAY_TYPE_ASP 17 |
75 |
|
|
76 |
|
|
77 |
/*#define VIDOBJLAY_AR_SQUARE 1 */ |
/*#define VIDOBJLAY_AR_SQUARE 1 */ |
87 |
#define VIDOBJLAY_SHAPE_BINARY_ONLY 2 |
#define VIDOBJLAY_SHAPE_BINARY_ONLY 2 |
88 |
#define VIDOBJLAY_SHAPE_GRAYSCALE 3 |
#define VIDOBJLAY_SHAPE_GRAYSCALE 3 |
89 |
|
|
90 |
#define VO_START_CODE 0x8 |
|
91 |
#define VOL_START_CODE 0x12 |
#define SPRITE_NONE 0 |
92 |
#define VOP_START_CODE 0x1b6 |
#define SPRITE_STATIC 1 |
93 |
|
#define SPRITE_GMC 2 |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
#define READ_MARKER() BitstreamSkip(bs, 1) |
#define READ_MARKER() BitstreamSkip(bs, 1) |
98 |
#define WRITE_MARKER() BitstreamPutBit(bs, 1) |
#define WRITE_MARKER() BitstreamPutBit(bs, 1) |
114 |
* Prototypes |
* Prototypes |
115 |
****************************************************************************/ |
****************************************************************************/ |
116 |
|
|
117 |
int |
int read_video_packet_header(Bitstream *bs, |
118 |
read_video_packet_header(Bitstream *bs, const int addbits, int * quant); |
DECODER * dec, |
119 |
|
const int addbits, |
120 |
|
int *quant, |
121 |
|
int *fcode_forward, |
122 |
|
int *fcode_backward, |
123 |
|
int *intra_dc_threshold); |
124 |
|
|
125 |
/* header stuff */ |
/* header stuff */ |
126 |
int BitstreamReadHeaders(Bitstream * bs, |
int BitstreamReadHeaders(Bitstream * bs, |
127 |
DECODER * dec, |
DECODER * dec, |
128 |
uint32_t * rounding, |
uint32_t * rounding, |
129 |
|
uint32_t * reduced_resolution, |
130 |
uint32_t * quant, |
uint32_t * quant, |
131 |
uint32_t * fcode_forward, |
uint32_t * fcode_forward, |
132 |
uint32_t * fcode_backward, |
uint32_t * fcode_backward, |
133 |
uint32_t * intra_dc_threshold); |
uint32_t * intra_dc_threshold, |
134 |
|
WARPPOINTS * gmc_warp); |
135 |
|
|
136 |
|
|
137 |
void BitstreamWriteVolHeader(Bitstream * const bs, |
void BitstreamWriteVolHeader(Bitstream * const bs, |
138 |
const MBParam * pParam, |
const MBParam * pParam); |
|
const FRAMEINFO * 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 |
|
|
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; |
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 |
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_ */ |