[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.16 - (view) (download)

1 : edgomez 1.11 /*****************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - Bitstream reader/writer inlined functions and constants-
5 :     *
6 : suxen_drol 1.13 * Copyright (C) 2001-2002 - Peter Ross <pross@xvid.org>
7 : edgomez 1.11 *
8 : edgomez 1.14 * This file is part of XviD, a free MPEG-4 video encoder/decoder
9 :     *
10 :     * XviD is free software; you can redistribute it and/or modify it
11 :     * under the terms of the GNU General Public License as published by
12 :     * the Free Software Foundation; either version 2 of the License, or
13 : edgomez 1.11 * (at your option) any later version.
14 :     *
15 :     * This program is distributed in the hope that it will be useful,
16 : edgomez 1.14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : edgomez 1.11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 :     * GNU General Public License for more details.
19 :     *
20 :     * You should have received a copy of the GNU General Public License
21 : edgomez 1.14 * along with this program; if not, write to the Free Software
22 : edgomez 1.11 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 :     *
24 : edgomez 1.14 * Under section 8 of the GNU General Public License, the copyright
25 :     * holders of XVID explicitly forbid distribution in the following
26 :     * countries:
27 :     *
28 :     * - Japan
29 :     * - United States of America
30 :     *
31 :     * Linking XviD statically or dynamically with other modules is making a
32 :     * combined work based on XviD. Thus, the terms and conditions of the
33 :     * GNU General Public License cover the whole combination.
34 :     *
35 :     * As a special exception, the copyright holders of XviD give you
36 :     * permission to link XviD with independent modules that communicate with
37 :     * XviD solely through the VFW1.1 and DShow interfaces, regardless of the
38 :     * license terms of these independent modules, and to copy and distribute
39 :     * the resulting combined work under terms of your choice, provided that
40 :     * every copy of the combined work is accompanied by a complete copy of
41 :     * the source code of XviD (the version of XviD used to produce the
42 :     * combined work), being distributed under the terms of the GNU General
43 :     * Public License plus this exception. An independent module is a module
44 :     * which is not derived from or based on XviD.
45 :     *
46 :     * Note that people who make modified versions of XviD are not obligated
47 :     * to grant this special exception for their modified versions; it is
48 :     * their choice whether to do so. The GNU General Public License gives
49 :     * permission to release a modified version without this exception; this
50 :     * exception also makes it possible to release a modified version which
51 :     * carries forward this exception.
52 :     *
53 : edgomez 1.16 * $Id: bitstream.h,v 1.15 2002/11/26 23:44:10 edgomez Exp $
54 : edgomez 1.11 *
55 :     ****************************************************************************/
56 : Isibaar 1.1
57 :     #ifndef _BITSTREAM_H_
58 :     #define _BITSTREAM_H_
59 :    
60 :     #include "../portab.h"
61 :     #include "../decoder.h"
62 :     #include "../encoder.h"
63 :    
64 : edgomez 1.11
65 :     /*****************************************************************************
66 :     * Constants
67 :     ****************************************************************************/
68 :    
69 : edgomez 1.15 /* comment any #defs we dont use */
70 : Isibaar 1.1
71 :     #define VIDOBJ_START_CODE 0x00000100 /* ..0x0000011f */
72 :     #define VIDOBJLAY_START_CODE 0x00000120 /* ..0x0000012f */
73 :     #define VISOBJSEQ_START_CODE 0x000001b0
74 :     #define VISOBJSEQ_STOP_CODE 0x000001b1 /* ??? */
75 :     #define USERDATA_START_CODE 0x000001b2
76 :     #define GRPOFVOP_START_CODE 0x000001b3
77 : edgomez 1.15 /*#define VIDSESERR_ERROR_CODE 0x000001b4 */
78 : Isibaar 1.1 #define VISOBJ_START_CODE 0x000001b5
79 : edgomez 1.15 /*#define SLICE_START_CODE 0x000001b7 */
80 :     /*#define EXT_START_CODE 0x000001b8 */
81 : Isibaar 1.1
82 :    
83 :     #define VISOBJ_TYPE_VIDEO 1
84 : edgomez 1.15 /*#define VISOBJ_TYPE_STILLTEXTURE 2 */
85 :     /*#define VISOBJ_TYPE_MESH 3 */
86 :     /*#define VISOBJ_TYPE_FBA 4 */
87 :     /*#define VISOBJ_TYPE_3DMESH 5 */
88 : Isibaar 1.1
89 :    
90 :     #define VIDOBJLAY_TYPE_SIMPLE 1
91 : edgomez 1.15 /*#define VIDOBJLAY_TYPE_SIMPLE_SCALABLE 2 */
92 : Isibaar 1.1 #define VIDOBJLAY_TYPE_CORE 3
93 :     #define VIDOBJLAY_TYPE_MAIN 4
94 :    
95 :    
96 : edgomez 1.15 /*#define VIDOBJLAY_AR_SQUARE 1 */
97 :     /*#define VIDOBJLAY_AR_625TYPE_43 2 */
98 :     /*#define VIDOBJLAY_AR_525TYPE_43 3 */
99 :     /*#define VIDOBJLAY_AR_625TYPE_169 8 */
100 :     /*#define VIDOBJLAY_AR_525TYPE_169 9 */
101 : Isibaar 1.1 #define VIDOBJLAY_AR_EXTPAR 15
102 :    
103 :    
104 :     #define VIDOBJLAY_SHAPE_RECTANGULAR 0
105 :     #define VIDOBJLAY_SHAPE_BINARY 1
106 :     #define VIDOBJLAY_SHAPE_BINARY_ONLY 2
107 :     #define VIDOBJLAY_SHAPE_GRAYSCALE 3
108 :    
109 :     #define VO_START_CODE 0x8
110 :     #define VOL_START_CODE 0x12
111 :     #define VOP_START_CODE 0x1b6
112 :    
113 :     #define READ_MARKER() BitstreamSkip(bs, 1)
114 :     #define WRITE_MARKER() BitstreamPutBit(bs, 1)
115 :    
116 : edgomez 1.15 /* vop coding types */
117 :     /* intra, prediction, backward, sprite, not_coded */
118 : Isibaar 1.1 #define I_VOP 0
119 :     #define P_VOP 1
120 :     #define B_VOP 2
121 :     #define S_VOP 3
122 :     #define N_VOP 4
123 :    
124 : edgomez 1.15 /* resync-specific */
125 : suxen_drol 1.9 #define NUMBITS_VP_RESYNC_MARKER 17
126 :     #define RESYNC_MARKER 1
127 :    
128 :    
129 : edgomez 1.11 /*****************************************************************************
130 :     * Prototypes
131 :     ****************************************************************************/
132 :    
133 : suxen_drol 1.9 int
134 : suxen_drol 1.10 read_video_packet_header(Bitstream *bs, const int addbits, int * quant);
135 : suxen_drol 1.9
136 : Isibaar 1.1
137 : edgomez 1.15 /* header stuff */
138 : edgomez 1.6 int BitstreamReadHeaders(Bitstream * bs,
139 :     DECODER * dec,
140 :     uint32_t * rounding,
141 :     uint32_t * quant,
142 :     uint32_t * fcode_forward,
143 :     uint32_t * fcode_backward,
144 :     uint32_t * intra_dc_threshold);
145 : Isibaar 1.1
146 :    
147 :     void BitstreamWriteVolHeader(Bitstream * const bs,
148 : edgomez 1.6 const MBParam * pParam,
149 :     const FRAMEINFO * frame);
150 : Isibaar 1.1
151 :     void BitstreamWriteVopHeader(Bitstream * const bs,
152 : edgomez 1.6 const MBParam * pParam,
153 : suxen_drol 1.8 const FRAMEINFO * frame,
154 :     int vop_coded);
155 : Isibaar 1.1
156 : edgomez 1.11 /*****************************************************************************
157 :     * Inlined functions
158 :     ****************************************************************************/
159 :    
160 : Isibaar 1.1 /* initialise bitstream structure */
161 :    
162 : edgomez 1.6 static void __inline
163 :     BitstreamInit(Bitstream * const bs,
164 :     void *const bitstream,
165 :     uint32_t length)
166 : Isibaar 1.1 {
167 :     uint32_t tmp;
168 :    
169 : edgomez 1.6 bs->start = bs->tail = (uint32_t *) bitstream;
170 : Isibaar 1.1
171 : edgomez 1.6 tmp = *(uint32_t *) bitstream;
172 : Isibaar 1.1 #ifndef ARCH_IS_BIG_ENDIAN
173 :     BSWAP(tmp);
174 :     #endif
175 :     bs->bufa = tmp;
176 :    
177 : edgomez 1.6 tmp = *((uint32_t *) bitstream + 1);
178 : Isibaar 1.1 #ifndef ARCH_IS_BIG_ENDIAN
179 :     BSWAP(tmp);
180 :     #endif
181 :     bs->bufb = tmp;
182 :    
183 :     bs->buf = 0;
184 :     bs->pos = 0;
185 :     bs->length = length;
186 :     }
187 :    
188 :    
189 :     /* reset bitstream state */
190 :    
191 : edgomez 1.6 static void __inline
192 :     BitstreamReset(Bitstream * const bs)
193 : Isibaar 1.1 {
194 :     uint32_t tmp;
195 :    
196 :     bs->tail = bs->start;
197 :    
198 :     tmp = *bs->start;
199 :     #ifndef ARCH_IS_BIG_ENDIAN
200 :     BSWAP(tmp);
201 :     #endif
202 :     bs->bufa = tmp;
203 :    
204 :     tmp = *(bs->start + 1);
205 :     #ifndef ARCH_IS_BIG_ENDIAN
206 :     BSWAP(tmp);
207 :     #endif
208 :     bs->bufb = tmp;
209 :    
210 :     bs->buf = 0;
211 : edgomez 1.6 bs->pos = 0;
212 : Isibaar 1.1 }
213 :    
214 :    
215 :     /* reads n bits from bitstream without changing the stream pos */
216 :    
217 : edgomez 1.6 static uint32_t __inline
218 :     BitstreamShowBits(Bitstream * const bs,
219 :     const uint32_t bits)
220 : Isibaar 1.1 {
221 :     int nbit = (bits + bs->pos) - 32;
222 : edgomez 1.6
223 :     if (nbit > 0) {
224 :     return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) | (bs->
225 :     bufb >> (32 -
226 :     nbit));
227 :     } else {
228 : Isibaar 1.1 return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits);
229 :     }
230 :     }
231 :    
232 :    
233 :     /* skip n bits forward in bitstream */
234 :    
235 : edgomez 1.11 static void __inline
236 : edgomez 1.6 BitstreamSkip(Bitstream * const bs,
237 :     const uint32_t bits)
238 : Isibaar 1.1 {
239 :     bs->pos += bits;
240 :    
241 : edgomez 1.6 if (bs->pos >= 32) {
242 : Isibaar 1.1 uint32_t tmp;
243 :    
244 :     bs->bufa = bs->bufb;
245 : edgomez 1.6 tmp = *((uint32_t *) bs->tail + 2);
246 : Isibaar 1.1 #ifndef ARCH_IS_BIG_ENDIAN
247 :     BSWAP(tmp);
248 :     #endif
249 :     bs->bufb = tmp;
250 :     bs->tail++;
251 :     bs->pos -= 32;
252 :     }
253 :     }
254 : suxen_drol 1.9
255 :    
256 : edgomez 1.15 /* number of bits to next byte alignment */
257 : edgomez 1.11 static uint32_t __inline
258 : suxen_drol 1.9 BitstreamNumBitsToByteAlign(Bitstream *bs)
259 :     {
260 :     uint32_t n = (32 - bs->pos) % 8;
261 :     return n == 0 ? 8 : n;
262 :     }
263 :    
264 :    
265 : edgomez 1.15 /* show nbits from next byte alignment */
266 : edgomez 1.11 static uint32_t __inline
267 : suxen_drol 1.9 BitstreamShowBitsFromByteAlign(Bitstream *bs, int bits)
268 :     {
269 :     int bspos = bs->pos + BitstreamNumBitsToByteAlign(bs);
270 :     int nbit = (bits + bspos) - 32;
271 :    
272 :     if (bspos >= 32) {
273 :     return bs->bufb >> (32 - nbit);
274 :     } else if (nbit > 0) {
275 :     return ((bs->bufa & (0xffffffff >> bspos)) << nbit) | (bs->
276 :     bufb >> (32 -
277 :     nbit));
278 :     } else {
279 :     return (bs->bufa & (0xffffffff >> bspos)) >> (32 - bspos - bits);
280 :     }
281 :    
282 :     }
283 :    
284 : Isibaar 1.1
285 :    
286 :     /* move forward to the next byte boundary */
287 :    
288 : edgomez 1.11 static void __inline
289 : edgomez 1.6 BitstreamByteAlign(Bitstream * const bs)
290 : Isibaar 1.1 {
291 :     uint32_t remainder = bs->pos % 8;
292 : edgomez 1.6
293 :     if (remainder) {
294 : Isibaar 1.1 BitstreamSkip(bs, 8 - remainder);
295 :     }
296 :     }
297 :    
298 :    
299 :     /* bitstream length (unit bits) */
300 :    
301 : edgomez 1.6 static uint32_t __inline
302 :     BitstreamPos(const Bitstream * const bs)
303 : Isibaar 1.1 {
304 : edgomez 1.16 return((uint32_t)(8*((ptr_t)bs->tail - (ptr_t)bs->start) + bs->pos));
305 : Isibaar 1.1 }
306 :    
307 :    
308 :     /* flush the bitstream & return length (unit bytes)
309 :     NOTE: assumes no futher bitstream functions will be called.
310 :     */
311 :    
312 : edgomez 1.6 static uint32_t __inline
313 :     BitstreamLength(Bitstream * const bs)
314 : Isibaar 1.1 {
315 : edgomez 1.16 uint32_t len = (uint32_t)((ptr_t)bs->tail - (ptr_t)bs->start);
316 : Isibaar 1.1
317 : edgomez 1.6 if (bs->pos) {
318 : Isibaar 1.1 uint32_t b = bs->buf;
319 : edgomez 1.6
320 : Isibaar 1.1 #ifndef ARCH_IS_BIG_ENDIAN
321 :     BSWAP(b);
322 :     #endif
323 :     *bs->tail = b;
324 :    
325 :     len += (bs->pos + 7) / 8;
326 : edgomez 1.6 }
327 : Isibaar 1.1
328 :     return len;
329 :     }
330 :    
331 :    
332 :     /* move bitstream position forward by n bits and write out buffer if needed */
333 :    
334 : edgomez 1.6 static void __inline
335 :     BitstreamForward(Bitstream * const bs,
336 :     const uint32_t bits)
337 : Isibaar 1.1 {
338 : edgomez 1.6 bs->pos += bits;
339 : Isibaar 1.1
340 : edgomez 1.6 if (bs->pos >= 32) {
341 : Isibaar 1.1 uint32_t b = bs->buf;
342 : edgomez 1.6
343 : Isibaar 1.1 #ifndef ARCH_IS_BIG_ENDIAN
344 :     BSWAP(b);
345 :     #endif
346 :     *bs->tail++ = b;
347 :     bs->buf = 0;
348 :     bs->pos -= 32;
349 : edgomez 1.6 }
350 : Isibaar 1.1 }
351 :    
352 :    
353 :     /* pad bitstream to the next byte boundary */
354 :    
355 : edgomez 1.6 static void __inline
356 :     BitstreamPad(Bitstream * const bs)
357 : Isibaar 1.1 {
358 :     uint32_t remainder = bs->pos % 8;
359 :    
360 : edgomez 1.6 if (remainder) {
361 : Isibaar 1.1 BitstreamForward(bs, 8 - remainder);
362 : edgomez 1.6 }
363 : Isibaar 1.1 }
364 :    
365 :    
366 :     /* read n bits from bitstream */
367 :    
368 : edgomez 1.6 static uint32_t __inline
369 :     BitstreamGetBits(Bitstream * const bs,
370 :     const uint32_t n)
371 : Isibaar 1.1 {
372 :     uint32_t ret = BitstreamShowBits(bs, n);
373 : edgomez 1.6
374 : Isibaar 1.1 BitstreamSkip(bs, n);
375 :     return ret;
376 :     }
377 :    
378 :    
379 :     /* read single bit from bitstream */
380 :    
381 : edgomez 1.6 static uint32_t __inline
382 :     BitstreamGetBit(Bitstream * const bs)
383 : Isibaar 1.1 {
384 :     return BitstreamGetBits(bs, 1);
385 :     }
386 :    
387 :    
388 :     /* write single bit to bitstream */
389 :    
390 : edgomez 1.6 static void __inline
391 :     BitstreamPutBit(Bitstream * const bs,
392 :     const uint32_t bit)
393 : Isibaar 1.1 {
394 : edgomez 1.6 if (bit)
395 : Isibaar 1.1 bs->buf |= (0x80000000 >> bs->pos);
396 :    
397 : edgomez 1.6 BitstreamForward(bs, 1);
398 : Isibaar 1.1 }
399 :    
400 :    
401 :     /* write n bits to bitstream */
402 :    
403 : edgomez 1.6 static void __inline
404 :     BitstreamPutBits(Bitstream * const bs,
405 :     const uint32_t value,
406 :     const uint32_t size)
407 : Isibaar 1.1 {
408 :     uint32_t shift = 32 - bs->pos - size;
409 :    
410 :     if (shift <= 32) {
411 :     bs->buf |= value << shift;
412 :     BitstreamForward(bs, size);
413 :     } else {
414 :     uint32_t remainder;
415 :    
416 :     shift = size - (32 - bs->pos);
417 :     bs->buf |= value >> shift;
418 :     BitstreamForward(bs, size - shift);
419 :     remainder = shift;
420 :    
421 :     shift = 32 - shift;
422 : edgomez 1.6
423 : Isibaar 1.1 bs->buf |= value << shift;
424 :     BitstreamForward(bs, remainder);
425 :     }
426 :     }
427 :    
428 : edgomez 1.11 #endif /* _BITSTREAM_H_ */

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