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

1 : edgomez 1.11 /*****************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - Bitstream reader/writer inlined functions and constants-
5 :     *
6 :     * Copyright (C) 2001-2002 - Peter Ross <pross@cs.rmit.edu.au>
7 :     *
8 :     * This program is an implementation of a part of one or more MPEG-4
9 :     * Video tools as specified in ISO/IEC 14496-2 standard. Those intending
10 :     * to use this software module in hardware or software products are
11 :     * advised that its use may infringe existing patents or copyrights, and
12 :     * 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 :     * editors and their companies, will have no liability for use of this
15 :     * software or modifications or derivatives thereof.
16 :     *
17 :     * 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 :     * the Free Software Foundation ; either version 2 of the License, or
20 :     * (at your option) any later version.
21 :     *
22 :     * This program is distributed in the hope that it will be useful,
23 :     * but WITHOUT ANY WARRANTY ; without even the implied warranty of
24 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 :     * GNU General Public License for more details.
26 :     *
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 : edgomez 1.12 * $Id: bitstream.h,v 1.11 2002/09/10 22:25:23 edgomez Exp $
32 : edgomez 1.11 *
33 :     ****************************************************************************/
34 : Isibaar 1.1
35 :     #ifndef _BITSTREAM_H_
36 :     #define _BITSTREAM_H_
37 :    
38 :     #include "../portab.h"
39 :     #include "../decoder.h"
40 :     #include "../encoder.h"
41 :    
42 : edgomez 1.11
43 :     /*****************************************************************************
44 :     * Constants
45 :     ****************************************************************************/
46 :    
47 : Isibaar 1.1 // comment any #defs we dont use
48 :    
49 :     #define VIDOBJ_START_CODE 0x00000100 /* ..0x0000011f */
50 :     #define VIDOBJLAY_START_CODE 0x00000120 /* ..0x0000012f */
51 :     #define VISOBJSEQ_START_CODE 0x000001b0
52 :     #define VISOBJSEQ_STOP_CODE 0x000001b1 /* ??? */
53 :     #define USERDATA_START_CODE 0x000001b2
54 :     #define GRPOFVOP_START_CODE 0x000001b3
55 : edgomez 1.6 //#define VIDSESERR_ERROR_CODE 0x000001b4
56 : Isibaar 1.1 #define VISOBJ_START_CODE 0x000001b5
57 : edgomez 1.6 //#define SLICE_START_CODE 0x000001b7
58 :     //#define EXT_START_CODE 0x000001b8
59 : Isibaar 1.1
60 :    
61 :     #define VISOBJ_TYPE_VIDEO 1
62 : edgomez 1.6 //#define VISOBJ_TYPE_STILLTEXTURE 2
63 :     //#define VISOBJ_TYPE_MESH 3
64 :     //#define VISOBJ_TYPE_FBA 4
65 :     //#define VISOBJ_TYPE_3DMESH 5
66 : Isibaar 1.1
67 :    
68 :     #define VIDOBJLAY_TYPE_SIMPLE 1
69 : edgomez 1.6 //#define VIDOBJLAY_TYPE_SIMPLE_SCALABLE 2
70 : Isibaar 1.1 #define VIDOBJLAY_TYPE_CORE 3
71 :     #define VIDOBJLAY_TYPE_MAIN 4
72 :    
73 :    
74 : edgomez 1.6 //#define VIDOBJLAY_AR_SQUARE 1
75 :     //#define VIDOBJLAY_AR_625TYPE_43 2
76 :     //#define VIDOBJLAY_AR_525TYPE_43 3
77 :     //#define VIDOBJLAY_AR_625TYPE_169 8
78 :     //#define VIDOBJLAY_AR_525TYPE_169 9
79 : Isibaar 1.1 #define VIDOBJLAY_AR_EXTPAR 15
80 :    
81 :    
82 :     #define VIDOBJLAY_SHAPE_RECTANGULAR 0
83 :     #define VIDOBJLAY_SHAPE_BINARY 1
84 :     #define VIDOBJLAY_SHAPE_BINARY_ONLY 2
85 :     #define VIDOBJLAY_SHAPE_GRAYSCALE 3
86 :    
87 :     #define VO_START_CODE 0x8
88 :     #define VOL_START_CODE 0x12
89 :     #define VOP_START_CODE 0x1b6
90 :    
91 :     #define READ_MARKER() BitstreamSkip(bs, 1)
92 :     #define WRITE_MARKER() BitstreamPutBit(bs, 1)
93 :    
94 :     // vop coding types
95 :     // intra, prediction, backward, sprite, not_coded
96 :     #define I_VOP 0
97 :     #define P_VOP 1
98 :     #define B_VOP 2
99 :     #define S_VOP 3
100 :     #define N_VOP 4
101 :    
102 : suxen_drol 1.9 // resync-specific
103 :     #define NUMBITS_VP_RESYNC_MARKER 17
104 :     #define RESYNC_MARKER 1
105 :    
106 :    
107 : edgomez 1.11 /*****************************************************************************
108 :     * Prototypes
109 :     ****************************************************************************/
110 :    
111 : suxen_drol 1.9 int
112 : suxen_drol 1.10 read_video_packet_header(Bitstream *bs, const int addbits, int * quant);
113 : suxen_drol 1.9
114 : Isibaar 1.1
115 :     // header stuff
116 : edgomez 1.6 int BitstreamReadHeaders(Bitstream * bs,
117 :     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 : Isibaar 1.1
124 :    
125 :     void BitstreamWriteVolHeader(Bitstream * const bs,
126 : edgomez 1.6 const MBParam * pParam,
127 :     const FRAMEINFO * frame);
128 : Isibaar 1.1
129 :     void BitstreamWriteVopHeader(Bitstream * const bs,
130 : edgomez 1.6 const MBParam * pParam,
131 : suxen_drol 1.8 const FRAMEINFO * frame,
132 :     int vop_coded);
133 : Isibaar 1.1
134 : edgomez 1.11 /*****************************************************************************
135 :     * Inlined functions
136 :     ****************************************************************************/
137 :    
138 : Isibaar 1.1 /* initialise bitstream structure */
139 :    
140 : edgomez 1.6 static void __inline
141 :     BitstreamInit(Bitstream * const bs,
142 :     void *const bitstream,
143 :     uint32_t length)
144 : Isibaar 1.1 {
145 :     uint32_t tmp;
146 :    
147 : edgomez 1.6 bs->start = bs->tail = (uint32_t *) bitstream;
148 : Isibaar 1.1
149 : edgomez 1.6 tmp = *(uint32_t *) bitstream;
150 : Isibaar 1.1 #ifndef ARCH_IS_BIG_ENDIAN
151 :     BSWAP(tmp);
152 :     #endif
153 :     bs->bufa = tmp;
154 :    
155 : edgomez 1.6 tmp = *((uint32_t *) bitstream + 1);
156 : Isibaar 1.1 #ifndef ARCH_IS_BIG_ENDIAN
157 :     BSWAP(tmp);
158 :     #endif
159 :     bs->bufb = tmp;
160 :    
161 :     bs->buf = 0;
162 :     bs->pos = 0;
163 :     bs->length = length;
164 :     }
165 :    
166 :    
167 :     /* reset bitstream state */
168 :    
169 : edgomez 1.6 static void __inline
170 :     BitstreamReset(Bitstream * const bs)
171 : Isibaar 1.1 {
172 :     uint32_t tmp;
173 :    
174 :     bs->tail = bs->start;
175 :    
176 :     tmp = *bs->start;
177 :     #ifndef ARCH_IS_BIG_ENDIAN
178 :     BSWAP(tmp);
179 :     #endif
180 :     bs->bufa = tmp;
181 :    
182 :     tmp = *(bs->start + 1);
183 :     #ifndef ARCH_IS_BIG_ENDIAN
184 :     BSWAP(tmp);
185 :     #endif
186 :     bs->bufb = tmp;
187 :    
188 :     bs->buf = 0;
189 : edgomez 1.6 bs->pos = 0;
190 : Isibaar 1.1 }
191 :    
192 :    
193 :     /* reads n bits from bitstream without changing the stream pos */
194 :    
195 : edgomez 1.6 static uint32_t __inline
196 :     BitstreamShowBits(Bitstream * const bs,
197 :     const uint32_t bits)
198 : Isibaar 1.1 {
199 :     int nbit = (bits + bs->pos) - 32;
200 : edgomez 1.6
201 :     if (nbit > 0) {
202 :     return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) | (bs->
203 :     bufb >> (32 -
204 :     nbit));
205 :     } else {
206 : Isibaar 1.1 return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits);
207 :     }
208 :     }
209 :    
210 :    
211 :     /* skip n bits forward in bitstream */
212 :    
213 : edgomez 1.11 static void __inline
214 : edgomez 1.6 BitstreamSkip(Bitstream * const bs,
215 :     const uint32_t bits)
216 : Isibaar 1.1 {
217 :     bs->pos += bits;
218 :    
219 : edgomez 1.6 if (bs->pos >= 32) {
220 : Isibaar 1.1 uint32_t tmp;
221 :    
222 :     bs->bufa = bs->bufb;
223 : edgomez 1.6 tmp = *((uint32_t *) bs->tail + 2);
224 : Isibaar 1.1 #ifndef ARCH_IS_BIG_ENDIAN
225 :     BSWAP(tmp);
226 :     #endif
227 :     bs->bufb = tmp;
228 :     bs->tail++;
229 :     bs->pos -= 32;
230 :     }
231 :     }
232 : suxen_drol 1.9
233 :    
234 :     // number of bits to next byte alignment
235 : edgomez 1.11 static uint32_t __inline
236 : suxen_drol 1.9 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 : edgomez 1.11 static uint32_t __inline
245 : suxen_drol 1.9 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 : Isibaar 1.1
263 :    
264 :     /* move forward to the next byte boundary */
265 :    
266 : edgomez 1.11 static void __inline
267 : edgomez 1.6 BitstreamByteAlign(Bitstream * const bs)
268 : Isibaar 1.1 {
269 :     uint32_t remainder = bs->pos % 8;
270 : edgomez 1.6
271 :     if (remainder) {
272 : Isibaar 1.1 BitstreamSkip(bs, 8 - remainder);
273 :     }
274 :     }
275 :    
276 :    
277 :     /* bitstream length (unit bits) */
278 :    
279 : edgomez 1.6 static uint32_t __inline
280 :     BitstreamPos(const Bitstream * const bs)
281 : Isibaar 1.1 {
282 : Isibaar 1.7 return 8 * ((ptr_t)bs->tail - (ptr_t)bs->start) + bs->pos;
283 : Isibaar 1.1 }
284 :    
285 :    
286 :     /* flush the bitstream & return length (unit bytes)
287 :     NOTE: assumes no futher bitstream functions will be called.
288 :     */
289 :    
290 : edgomez 1.6 static uint32_t __inline
291 :     BitstreamLength(Bitstream * const bs)
292 : Isibaar 1.1 {
293 : Isibaar 1.7 uint32_t len = (ptr_t) bs->tail - (ptr_t) bs->start;
294 : Isibaar 1.1
295 : edgomez 1.6 if (bs->pos) {
296 : Isibaar 1.1 uint32_t b = bs->buf;
297 : edgomez 1.6
298 : Isibaar 1.1 #ifndef ARCH_IS_BIG_ENDIAN
299 :     BSWAP(b);
300 :     #endif
301 :     *bs->tail = b;
302 :    
303 :     len += (bs->pos + 7) / 8;
304 : edgomez 1.6 }
305 : Isibaar 1.1
306 :     return len;
307 :     }
308 :    
309 :    
310 :     /* move bitstream position forward by n bits and write out buffer if needed */
311 :    
312 : edgomez 1.6 static void __inline
313 :     BitstreamForward(Bitstream * const bs,
314 :     const uint32_t bits)
315 : Isibaar 1.1 {
316 : edgomez 1.6 bs->pos += bits;
317 : Isibaar 1.1
318 : edgomez 1.6 if (bs->pos >= 32) {
319 : Isibaar 1.1 uint32_t b = bs->buf;
320 : edgomez 1.6
321 : Isibaar 1.1 #ifndef ARCH_IS_BIG_ENDIAN
322 :     BSWAP(b);
323 :     #endif
324 :     *bs->tail++ = b;
325 :     bs->buf = 0;
326 :     bs->pos -= 32;
327 : edgomez 1.6 }
328 : Isibaar 1.1 }
329 :    
330 :    
331 :     /* pad bitstream to the next byte boundary */
332 :    
333 : edgomez 1.6 static void __inline
334 :     BitstreamPad(Bitstream * const bs)
335 : Isibaar 1.1 {
336 :     uint32_t remainder = bs->pos % 8;
337 :    
338 : edgomez 1.6 if (remainder) {
339 : Isibaar 1.1 BitstreamForward(bs, 8 - remainder);
340 : edgomez 1.6 }
341 : Isibaar 1.1 }
342 :    
343 :    
344 :     /* read n bits from bitstream */
345 :    
346 : edgomez 1.6 static uint32_t __inline
347 :     BitstreamGetBits(Bitstream * const bs,
348 :     const uint32_t n)
349 : Isibaar 1.1 {
350 :     uint32_t ret = BitstreamShowBits(bs, n);
351 : edgomez 1.6
352 : Isibaar 1.1 BitstreamSkip(bs, n);
353 :     return ret;
354 :     }
355 :    
356 :    
357 :     /* read single bit from bitstream */
358 :    
359 : edgomez 1.6 static uint32_t __inline
360 :     BitstreamGetBit(Bitstream * const bs)
361 : Isibaar 1.1 {
362 :     return BitstreamGetBits(bs, 1);
363 :     }
364 :    
365 :    
366 :     /* write single bit to bitstream */
367 :    
368 : edgomez 1.6 static void __inline
369 :     BitstreamPutBit(Bitstream * const bs,
370 :     const uint32_t bit)
371 : Isibaar 1.1 {
372 : edgomez 1.6 if (bit)
373 : Isibaar 1.1 bs->buf |= (0x80000000 >> bs->pos);
374 :    
375 : edgomez 1.6 BitstreamForward(bs, 1);
376 : Isibaar 1.1 }
377 :    
378 :    
379 :     /* write n bits to bitstream */
380 :    
381 : edgomez 1.6 static void __inline
382 :     BitstreamPutBits(Bitstream * const bs,
383 :     const uint32_t value,
384 :     const uint32_t size)
385 : Isibaar 1.1 {
386 :     uint32_t shift = 32 - bs->pos - size;
387 :    
388 :     if (shift <= 32) {
389 :     bs->buf |= value << shift;
390 :     BitstreamForward(bs, size);
391 :     } else {
392 :     uint32_t remainder;
393 :    
394 :     shift = size - (32 - bs->pos);
395 :     bs->buf |= value >> shift;
396 :     BitstreamForward(bs, size - shift);
397 :     remainder = shift;
398 :    
399 :     shift = 32 - shift;
400 : edgomez 1.6
401 : Isibaar 1.1 bs->buf |= value << shift;
402 :     BitstreamForward(bs, remainder);
403 :     }
404 :     }
405 :    
406 : edgomez 1.11 #endif /* _BITSTREAM_H_ */

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