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$ |
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 |
|
/***************************************************************************** |
135 |
|
* Inlined functions |
136 |
|
****************************************************************************/ |
137 |
|
|
138 |
/* initialise bitstream structure */ |
/* initialise bitstream structure */ |
139 |
|
|
140 |
static void __inline BitstreamInit(Bitstream * const bs, |
static void __inline |
141 |
|
BitstreamInit(Bitstream * const bs, |
142 |
void * const bitstream, |
void * const bitstream, |
143 |
uint32_t length) |
uint32_t length) |
144 |
{ |
{ |
166 |
|
|
167 |
/* reset bitstream state */ |
/* reset bitstream state */ |
168 |
|
|
169 |
static void __inline BitstreamReset(Bitstream * const bs) |
static void __inline |
170 |
|
BitstreamReset(Bitstream * const bs) |
171 |
{ |
{ |
172 |
uint32_t tmp; |
uint32_t tmp; |
173 |
|
|
192 |
|
|
193 |
/* reads n bits from bitstream without changing the stream pos */ |
/* reads n bits from bitstream without changing the stream pos */ |
194 |
|
|
195 |
static uint32_t __inline BitstreamShowBits(Bitstream * const bs, |
static uint32_t __inline |
196 |
|
BitstreamShowBits(Bitstream * const bs, |
197 |
const uint32_t bits) |
const uint32_t bits) |
198 |
{ |
{ |
199 |
int nbit = (bits + bs->pos) - 32; |
int nbit = (bits + bs->pos) - 32; |
200 |
if (nbit > 0) |
|
201 |
{ |
if (nbit > 0) { |
202 |
return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) | |
return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) | (bs-> |
203 |
(bs->bufb >> (32 - nbit)); |
bufb >> (32 - |
204 |
} |
nbit)); |
205 |
else |
} else { |
|
{ |
|
206 |
return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits); |
return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits); |
207 |
} |
} |
208 |
} |
} |
210 |
|
|
211 |
/* skip n bits forward in bitstream */ |
/* skip n bits forward in bitstream */ |
212 |
|
|
213 |
static __inline void BitstreamSkip(Bitstream * const bs, const uint32_t bits) |
static void __inline |
214 |
|
BitstreamSkip(Bitstream * const bs, |
215 |
|
const uint32_t bits) |
216 |
{ |
{ |
217 |
bs->pos += bits; |
bs->pos += bits; |
218 |
|
|
219 |
if (bs->pos >= 32) |
if (bs->pos >= 32) { |
|
{ |
|
220 |
uint32_t tmp; |
uint32_t tmp; |
221 |
|
|
222 |
bs->bufa = bs->bufb; |
bs->bufa = bs->bufb; |
231 |
} |
} |
232 |
|
|
233 |
|
|
234 |
|
// number of bits to next byte alignment |
235 |
|
static uint32_t __inline |
236 |
|
BitstreamNumBitsToByteAlign(Bitstream *bs) |
237 |
|
{ |
238 |
|
uint32_t n = (32 - bs->pos) % 8; |
239 |
|
return n == 0 ? 8 : n; |
240 |
|
} |
241 |
|
|
242 |
|
|
243 |
|
// show nbits from next byte alignment |
244 |
|
static uint32_t __inline |
245 |
|
BitstreamShowBitsFromByteAlign(Bitstream *bs, int bits) |
246 |
|
{ |
247 |
|
int bspos = bs->pos + BitstreamNumBitsToByteAlign(bs); |
248 |
|
int nbit = (bits + bspos) - 32; |
249 |
|
|
250 |
|
if (bspos >= 32) { |
251 |
|
return bs->bufb >> (32 - nbit); |
252 |
|
} else if (nbit > 0) { |
253 |
|
return ((bs->bufa & (0xffffffff >> bspos)) << nbit) | (bs-> |
254 |
|
bufb >> (32 - |
255 |
|
nbit)); |
256 |
|
} else { |
257 |
|
return (bs->bufa & (0xffffffff >> bspos)) >> (32 - bspos - bits); |
258 |
|
} |
259 |
|
|
260 |
|
} |
261 |
|
|
262 |
|
|
263 |
|
|
264 |
/* move forward to the next byte boundary */ |
/* move forward to the next byte boundary */ |
265 |
|
|
266 |
static __inline void BitstreamByteAlign(Bitstream * const bs) |
static void __inline |
267 |
|
BitstreamByteAlign(Bitstream * const bs) |
268 |
{ |
{ |
269 |
uint32_t remainder = bs->pos % 8; |
uint32_t remainder = bs->pos % 8; |
270 |
if (remainder) |
|
271 |
{ |
if (remainder) { |
272 |
BitstreamSkip(bs, 8 - remainder); |
BitstreamSkip(bs, 8 - remainder); |
273 |
} |
} |
274 |
} |
} |
276 |
|
|
277 |
/* bitstream length (unit bits) */ |
/* bitstream length (unit bits) */ |
278 |
|
|
279 |
static uint32_t __inline BitstreamPos(const Bitstream * const bs) |
static uint32_t __inline |
280 |
|
BitstreamPos(const Bitstream * const bs) |
281 |
{ |
{ |
282 |
return 8 * ((uint32_t)bs->tail - (uint32_t)bs->start) + bs->pos; |
return 8 * ((ptr_t)bs->tail - (ptr_t)bs->start) + bs->pos; |
283 |
} |
} |
284 |
|
|
285 |
|
|
287 |
NOTE: assumes no futher bitstream functions will be called. |
NOTE: assumes no futher bitstream functions will be called. |
288 |
*/ |
*/ |
289 |
|
|
290 |
static uint32_t __inline BitstreamLength(Bitstream * const bs) |
static uint32_t __inline |
291 |
|
BitstreamLength(Bitstream * const bs) |
292 |
{ |
{ |
293 |
uint32_t len = (uint32_t) bs->tail - (uint32_t) bs->start; |
uint32_t len = (ptr_t) bs->tail - (ptr_t) bs->start; |
294 |
|
|
295 |
if (bs->pos) |
if (bs->pos) { |
|
{ |
|
296 |
uint32_t b = bs->buf; |
uint32_t b = bs->buf; |
297 |
|
|
298 |
#ifndef ARCH_IS_BIG_ENDIAN |
#ifndef ARCH_IS_BIG_ENDIAN |
299 |
BSWAP(b); |
BSWAP(b); |
300 |
#endif |
#endif |
309 |
|
|
310 |
/* 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 */ |
311 |
|
|
312 |
static void __inline BitstreamForward(Bitstream * const bs, const uint32_t bits) |
static void __inline |
313 |
|
BitstreamForward(Bitstream * const bs, |
314 |
|
const uint32_t bits) |
315 |
{ |
{ |
316 |
bs->pos += bits; |
bs->pos += bits; |
317 |
|
|
318 |
if (bs->pos >= 32) |
if (bs->pos >= 32) { |
|
{ |
|
319 |
uint32_t b = bs->buf; |
uint32_t b = bs->buf; |
320 |
|
|
321 |
#ifndef ARCH_IS_BIG_ENDIAN |
#ifndef ARCH_IS_BIG_ENDIAN |
322 |
BSWAP(b); |
BSWAP(b); |
323 |
#endif |
#endif |
330 |
|
|
331 |
/* pad bitstream to the next byte boundary */ |
/* pad bitstream to the next byte boundary */ |
332 |
|
|
333 |
static void __inline BitstreamPad(Bitstream * const bs) |
static void __inline |
334 |
|
BitstreamPad(Bitstream * const bs) |
335 |
{ |
{ |
336 |
uint32_t remainder = bs->pos % 8; |
uint32_t remainder = bs->pos % 8; |
337 |
|
|
338 |
if (remainder) |
if (remainder) { |
|
{ |
|
339 |
BitstreamForward(bs, 8 - remainder); |
BitstreamForward(bs, 8 - remainder); |
340 |
} |
} |
341 |
} |
} |
343 |
|
|
344 |
/* read n bits from bitstream */ |
/* read n bits from bitstream */ |
345 |
|
|
346 |
static uint32_t __inline BitstreamGetBits(Bitstream * const bs, |
static uint32_t __inline |
347 |
|
BitstreamGetBits(Bitstream * const bs, |
348 |
const uint32_t n) |
const uint32_t n) |
349 |
{ |
{ |
350 |
uint32_t ret = BitstreamShowBits(bs, n); |
uint32_t ret = BitstreamShowBits(bs, n); |
351 |
|
|
352 |
BitstreamSkip(bs, n); |
BitstreamSkip(bs, n); |
353 |
return ret; |
return ret; |
354 |
} |
} |
356 |
|
|
357 |
/* read single bit from bitstream */ |
/* read single bit from bitstream */ |
358 |
|
|
359 |
static uint32_t __inline BitstreamGetBit(Bitstream * const bs) |
static uint32_t __inline |
360 |
|
BitstreamGetBit(Bitstream * const bs) |
361 |
{ |
{ |
362 |
return BitstreamGetBits(bs, 1); |
return BitstreamGetBits(bs, 1); |
363 |
} |
} |
365 |
|
|
366 |
/* write single bit to bitstream */ |
/* write single bit to bitstream */ |
367 |
|
|
368 |
static void __inline BitstreamPutBit(Bitstream * const bs, |
static void __inline |
369 |
|
BitstreamPutBit(Bitstream * const bs, |
370 |
const uint32_t bit) |
const uint32_t bit) |
371 |
{ |
{ |
372 |
if (bit) |
if (bit) |
378 |
|
|
379 |
/* write n bits to bitstream */ |
/* write n bits to bitstream */ |
380 |
|
|
381 |
static void __inline BitstreamPutBits(Bitstream * const bs, |
static void __inline |
382 |
|
BitstreamPutBits(Bitstream * const bs, |
383 |
const uint32_t value, |
const uint32_t value, |
384 |
const uint32_t size) |
const uint32_t size) |
385 |
{ |
{ |