1 |
/****************************************************************************** |
/***************************************************************************** |
2 |
* * |
* |
3 |
* This file is part of XviD, a free MPEG-4 video encoder/decoder * |
* XVID MPEG-4 VIDEO CODEC |
4 |
* * |
* - Bitstream reader/writer inlined functions and constants- |
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 * |
* Copyright (C) 2001-2002 - Peter Ross <pross@cs.rmit.edu.au> |
7 |
* software module in hardware or software products are advised that its * |
* |
8 |
* use may infringe existing patents or copyrights, and any such use * |
* This program is an implementation of a part of one or more MPEG-4 |
9 |
* would be at such party's own risk. The original developer of this * |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
10 |
* software module and his/her company, and subsequent editors and their * |
* to use this software module in hardware or software products are |
11 |
* companies, will have no liability for use of this software or * |
* advised that its use may infringe existing patents or copyrights, and |
12 |
* modifications or derivatives thereof. * |
* any such use would be at such party's own risk. The original |
13 |
* * |
* developer of this software module and his/her company, and subsequent |
14 |
* XviD is free software; you can redistribute it and/or modify it * |
* editors and their companies, will have no liability for use of this |
15 |
* under the terms of the GNU General Public License as published by * |
* software or modifications or derivatives thereof. |
16 |
* the Free Software Foundation; either version 2 of the License, or * |
* |
17 |
* (at your option) any later version. * |
* This program is free software ; you can redistribute it and/or modify |
18 |
* * |
* it under the terms of the GNU General Public License as published by |
19 |
* XviD is distributed in the hope that it will be useful, but * |
* the Free Software Foundation ; either version 2 of the License, or |
20 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
* (at your option) any later version. |
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
* |
22 |
* GNU General Public License for more details. * |
* This program is distributed in the hope that it will be useful, |
23 |
* * |
* but WITHOUT ANY WARRANTY ; without even the implied warranty of |
24 |
* You should have received a copy of the GNU General Public License * |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25 |
* along with this program; if not, write to the Free Software * |
* GNU General Public License for more details. |
26 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
* |
27 |
* * |
* You should have received a copy of the GNU General Public License |
28 |
******************************************************************************/ |
* along with this program ; if not, write to the Free Software |
29 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
30 |
/****************************************************************************** |
* |
31 |
* * |
* $Id: bitstream.h,v 1.11 2002-09-10 22:25:23 edgomez Exp $ |
32 |
* bitstream.h * |
* |
33 |
* * |
****************************************************************************/ |
|
* Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> * |
|
|
* * |
|
|
* For more information visit the XviD homepage: http://www.xvid.org * |
|
|
* * |
|
|
******************************************************************************/ |
|
|
|
|
|
/****************************************************************************** |
|
|
* * |
|
|
* Revision history: * |
|
|
* * |
|
|
* 26.03.2002 interlacing support - modified putvol/vopheaders paramters |
|
|
* 04.03.2002 putbits speedup (Isibaar) * |
|
|
* 03.03.2002 merged BITREADER and BITWRITER (Isibaar) * |
|
|
* 16.12.2001 inital version * |
|
|
* * |
|
|
******************************************************************************/ |
|
34 |
|
|
35 |
#ifndef _BITSTREAM_H_ |
#ifndef _BITSTREAM_H_ |
36 |
#define _BITSTREAM_H_ |
#define _BITSTREAM_H_ |
39 |
#include "../decoder.h" |
#include "../decoder.h" |
40 |
#include "../encoder.h" |
#include "../encoder.h" |
41 |
|
|
42 |
|
|
43 |
|
/***************************************************************************** |
44 |
|
* Constants |
45 |
|
****************************************************************************/ |
46 |
|
|
47 |
// comment any #defs we dont use |
// comment any #defs we dont use |
48 |
|
|
49 |
#define VIDOBJ_START_CODE 0x00000100 /* ..0x0000011f */ |
#define VIDOBJ_START_CODE 0x00000100 /* ..0x0000011f */ |
99 |
#define S_VOP 3 |
#define S_VOP 3 |
100 |
#define N_VOP 4 |
#define N_VOP 4 |
101 |
|
|
102 |
|
// resync-specific |
103 |
|
#define NUMBITS_VP_RESYNC_MARKER 17 |
104 |
|
#define RESYNC_MARKER 1 |
105 |
|
|
106 |
typedef struct |
|
107 |
{ |
/***************************************************************************** |
108 |
uint32_t bufa; |
* Prototypes |
109 |
uint32_t bufb; |
****************************************************************************/ |
110 |
uint32_t buf; |
|
111 |
uint32_t pos; |
int |
112 |
uint32_t *tail; |
read_video_packet_header(Bitstream *bs, const int addbits, int * quant); |
|
uint32_t *start; |
|
|
uint32_t length; |
|
|
} |
|
|
Bitstream; |
|
113 |
|
|
114 |
|
|
115 |
// header stuff |
// header stuff |
116 |
int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, |
int BitstreamReadHeaders(Bitstream * bs, |
117 |
uint32_t * quant, uint32_t * fcode, uint32_t * intra_dc_threshold); |
DECODER * dec, |
118 |
|
uint32_t * rounding, |
119 |
|
uint32_t * quant, |
120 |
|
uint32_t * fcode_forward, |
121 |
|
uint32_t * fcode_backward, |
122 |
|
uint32_t * intra_dc_threshold); |
123 |
|
|
124 |
|
|
125 |
void BitstreamWriteVolHeader(Bitstream * const bs, |
void BitstreamWriteVolHeader(Bitstream * const bs, |
126 |
const MBParam * pParam); |
const MBParam * pParam, |
127 |
|
const FRAMEINFO * frame); |
128 |
|
|
129 |
void BitstreamWriteVopHeader(Bitstream * const bs, |
void BitstreamWriteVopHeader(Bitstream * const bs, |
130 |
const MBParam * pParam); |
const MBParam * pParam, |
131 |
|
const FRAMEINFO * frame, |
132 |
|
int vop_coded); |
133 |
|
|
134 |
|
void BitstreamWriteUserData(Bitstream * const bs, |
135 |
|
uint8_t * data, |
136 |
|
const int length); |
137 |
|
|
138 |
|
/***************************************************************************** |
139 |
|
* Inlined functions |
140 |
|
****************************************************************************/ |
141 |
|
|
142 |
/* initialise bitstream structure */ |
/* initialise bitstream structure */ |
143 |
|
|
144 |
static void __inline BitstreamInit(Bitstream * const bs, |
static void __inline |
145 |
|
BitstreamInit(Bitstream * const bs, |
146 |
void * const bitstream, |
void * const bitstream, |
147 |
uint32_t length) |
uint32_t length) |
148 |
{ |
{ |
170 |
|
|
171 |
/* reset bitstream state */ |
/* reset bitstream state */ |
172 |
|
|
173 |
static void __inline BitstreamReset(Bitstream * const bs) |
static void __inline |
174 |
|
BitstreamReset(Bitstream * const bs) |
175 |
{ |
{ |
176 |
uint32_t tmp; |
uint32_t tmp; |
177 |
|
|
196 |
|
|
197 |
/* reads n bits from bitstream without changing the stream pos */ |
/* reads n bits from bitstream without changing the stream pos */ |
198 |
|
|
199 |
static uint32_t __inline BitstreamShowBits(Bitstream * const bs, |
static uint32_t __inline |
200 |
|
BitstreamShowBits(Bitstream * const bs, |
201 |
const uint32_t bits) |
const uint32_t bits) |
202 |
{ |
{ |
203 |
int nbit = (bits + bs->pos) - 32; |
int nbit = (bits + bs->pos) - 32; |
204 |
if (nbit > 0) |
|
205 |
{ |
if (nbit > 0) { |
206 |
return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) | |
return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) | (bs-> |
207 |
(bs->bufb >> (32 - nbit)); |
bufb >> (32 - |
208 |
} |
nbit)); |
209 |
else |
} else { |
|
{ |
|
210 |
return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits); |
return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits); |
211 |
} |
} |
212 |
} |
} |
214 |
|
|
215 |
/* skip n bits forward in bitstream */ |
/* skip n bits forward in bitstream */ |
216 |
|
|
217 |
static __inline void BitstreamSkip(Bitstream * const bs, const uint32_t bits) |
static void __inline |
218 |
|
BitstreamSkip(Bitstream * const bs, |
219 |
|
const uint32_t bits) |
220 |
{ |
{ |
221 |
bs->pos += bits; |
bs->pos += bits; |
222 |
|
|
223 |
if (bs->pos >= 32) |
if (bs->pos >= 32) { |
|
{ |
|
224 |
uint32_t tmp; |
uint32_t tmp; |
225 |
|
|
226 |
bs->bufa = bs->bufb; |
bs->bufa = bs->bufb; |
235 |
} |
} |
236 |
|
|
237 |
|
|
238 |
|
// number of bits to next byte alignment |
239 |
|
static uint32_t __inline |
240 |
|
BitstreamNumBitsToByteAlign(Bitstream *bs) |
241 |
|
{ |
242 |
|
uint32_t n = (32 - bs->pos) % 8; |
243 |
|
return n == 0 ? 8 : n; |
244 |
|
} |
245 |
|
|
246 |
|
|
247 |
|
// show nbits from next byte alignment |
248 |
|
static uint32_t __inline |
249 |
|
BitstreamShowBitsFromByteAlign(Bitstream *bs, int bits) |
250 |
|
{ |
251 |
|
int bspos = bs->pos + BitstreamNumBitsToByteAlign(bs); |
252 |
|
int nbit = (bits + bspos) - 32; |
253 |
|
|
254 |
|
if (bspos >= 32) { |
255 |
|
return bs->bufb >> (32 - nbit); |
256 |
|
} else if (nbit > 0) { |
257 |
|
return ((bs->bufa & (0xffffffff >> bspos)) << nbit) | (bs-> |
258 |
|
bufb >> (32 - |
259 |
|
nbit)); |
260 |
|
} else { |
261 |
|
return (bs->bufa & (0xffffffff >> bspos)) >> (32 - bspos - bits); |
262 |
|
} |
263 |
|
|
264 |
|
} |
265 |
|
|
266 |
|
|
267 |
|
|
268 |
/* move forward to the next byte boundary */ |
/* move forward to the next byte boundary */ |
269 |
|
|
270 |
static __inline void BitstreamByteAlign(Bitstream * const bs) |
static void __inline |
271 |
|
BitstreamByteAlign(Bitstream * const bs) |
272 |
{ |
{ |
273 |
uint32_t remainder = bs->pos % 8; |
uint32_t remainder = bs->pos % 8; |
274 |
if (remainder) |
|
275 |
{ |
if (remainder) { |
276 |
BitstreamSkip(bs, 8 - remainder); |
BitstreamSkip(bs, 8 - remainder); |
277 |
} |
} |
278 |
} |
} |
280 |
|
|
281 |
/* bitstream length (unit bits) */ |
/* bitstream length (unit bits) */ |
282 |
|
|
283 |
static uint32_t __inline BitstreamPos(const Bitstream * const bs) |
static uint32_t __inline |
284 |
|
BitstreamPos(const Bitstream * const bs) |
285 |
{ |
{ |
286 |
return 8 * ((uint32_t)bs->tail - (uint32_t)bs->start) + bs->pos; |
return 8 * ((ptr_t)bs->tail - (ptr_t)bs->start) + bs->pos; |
287 |
} |
} |
288 |
|
|
289 |
|
|
291 |
NOTE: assumes no futher bitstream functions will be called. |
NOTE: assumes no futher bitstream functions will be called. |
292 |
*/ |
*/ |
293 |
|
|
294 |
static uint32_t __inline BitstreamLength(Bitstream * const bs) |
static uint32_t __inline |
295 |
|
BitstreamLength(Bitstream * const bs) |
296 |
{ |
{ |
297 |
uint32_t len = (uint32_t) bs->tail - (uint32_t) bs->start; |
uint32_t len = (ptr_t) bs->tail - (ptr_t) bs->start; |
298 |
|
|
299 |
if (bs->pos) |
if (bs->pos) { |
|
{ |
|
300 |
uint32_t b = bs->buf; |
uint32_t b = bs->buf; |
301 |
|
|
302 |
#ifndef ARCH_IS_BIG_ENDIAN |
#ifndef ARCH_IS_BIG_ENDIAN |
303 |
BSWAP(b); |
BSWAP(b); |
304 |
#endif |
#endif |
313 |
|
|
314 |
/* move bitstream position forward by n bits and write out buffer if needed */ |
/* move bitstream position forward by n bits and write out buffer if needed */ |
315 |
|
|
316 |
static void __inline BitstreamForward(Bitstream * const bs, const uint32_t bits) |
static void __inline |
317 |
|
BitstreamForward(Bitstream * const bs, |
318 |
|
const uint32_t bits) |
319 |
{ |
{ |
320 |
bs->pos += bits; |
bs->pos += bits; |
321 |
|
|
322 |
if (bs->pos >= 32) |
if (bs->pos >= 32) { |
|
{ |
|
323 |
uint32_t b = bs->buf; |
uint32_t b = bs->buf; |
324 |
|
|
325 |
#ifndef ARCH_IS_BIG_ENDIAN |
#ifndef ARCH_IS_BIG_ENDIAN |
326 |
BSWAP(b); |
BSWAP(b); |
327 |
#endif |
#endif |
334 |
|
|
335 |
/* pad bitstream to the next byte boundary */ |
/* pad bitstream to the next byte boundary */ |
336 |
|
|
337 |
static void __inline BitstreamPad(Bitstream * const bs) |
static void __inline |
338 |
|
BitstreamPad(Bitstream * const bs) |
339 |
{ |
{ |
340 |
uint32_t remainder = bs->pos % 8; |
uint32_t remainder = bs->pos % 8; |
341 |
|
|
342 |
if (remainder) |
if (remainder) { |
|
{ |
|
343 |
BitstreamForward(bs, 8 - remainder); |
BitstreamForward(bs, 8 - remainder); |
344 |
} |
} |
345 |
} |
} |
347 |
|
|
348 |
/* read n bits from bitstream */ |
/* read n bits from bitstream */ |
349 |
|
|
350 |
static uint32_t __inline BitstreamGetBits(Bitstream * const bs, |
static uint32_t __inline |
351 |
|
BitstreamGetBits(Bitstream * const bs, |
352 |
const uint32_t n) |
const uint32_t n) |
353 |
{ |
{ |
354 |
uint32_t ret = BitstreamShowBits(bs, n); |
uint32_t ret = BitstreamShowBits(bs, n); |
355 |
|
|
356 |
BitstreamSkip(bs, n); |
BitstreamSkip(bs, n); |
357 |
return ret; |
return ret; |
358 |
} |
} |
360 |
|
|
361 |
/* read single bit from bitstream */ |
/* read single bit from bitstream */ |
362 |
|
|
363 |
static uint32_t __inline BitstreamGetBit(Bitstream * const bs) |
static uint32_t __inline |
364 |
|
BitstreamGetBit(Bitstream * const bs) |
365 |
{ |
{ |
366 |
return BitstreamGetBits(bs, 1); |
return BitstreamGetBits(bs, 1); |
367 |
} |
} |
369 |
|
|
370 |
/* write single bit to bitstream */ |
/* write single bit to bitstream */ |
371 |
|
|
372 |
static void __inline BitstreamPutBit(Bitstream * const bs, |
static void __inline |
373 |
|
BitstreamPutBit(Bitstream * const bs, |
374 |
const uint32_t bit) |
const uint32_t bit) |
375 |
{ |
{ |
376 |
if (bit) |
if (bit) |
382 |
|
|
383 |
/* write n bits to bitstream */ |
/* write n bits to bitstream */ |
384 |
|
|
385 |
static void __inline BitstreamPutBits(Bitstream * const bs, |
static void __inline |
386 |
|
BitstreamPutBits(Bitstream * const bs, |
387 |
const uint32_t value, |
const uint32_t value, |
388 |
const uint32_t size) |
const uint32_t size) |
389 |
{ |
{ |