[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.11 - (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 :     * $Id$
32 :     *
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 :    
134 :     void BitstreamWriteUserData(Bitstream * const bs,
135 :     uint8_t * data,
136 :     const int length);
137 : Isibaar 1.1
138 : edgomez 1.11 /*****************************************************************************
139 :     * Inlined functions
140 :     ****************************************************************************/
141 :    
142 : Isibaar 1.1 /* initialise bitstream structure */
143 :    
144 : edgomez 1.6 static void __inline
145 :     BitstreamInit(Bitstream * const bs,
146 :     void *const bitstream,
147 :     uint32_t length)
148 : Isibaar 1.1 {
149 :     uint32_t tmp;
150 :    
151 : edgomez 1.6 bs->start = bs->tail = (uint32_t *) bitstream;
152 : Isibaar 1.1
153 : edgomez 1.6 tmp = *(uint32_t *) bitstream;
154 : Isibaar 1.1 #ifndef ARCH_IS_BIG_ENDIAN
155 :     BSWAP(tmp);
156 :     #endif
157 :     bs->bufa = tmp;
158 :    
159 : edgomez 1.6 tmp = *((uint32_t *) bitstream + 1);
160 : Isibaar 1.1 #ifndef ARCH_IS_BIG_ENDIAN
161 :     BSWAP(tmp);
162 :     #endif
163 :     bs->bufb = tmp;
164 :    
165 :     bs->buf = 0;
166 :     bs->pos = 0;
167 :     bs->length = length;
168 :     }
169 :    
170 :    
171 :     /* reset bitstream state */
172 :    
173 : edgomez 1.6 static void __inline
174 :     BitstreamReset(Bitstream * const bs)
175 : Isibaar 1.1 {
176 :     uint32_t tmp;
177 :    
178 :     bs->tail = bs->start;
179 :    
180 :     tmp = *bs->start;
181 :     #ifndef ARCH_IS_BIG_ENDIAN
182 :     BSWAP(tmp);
183 :     #endif
184 :     bs->bufa = tmp;
185 :    
186 :     tmp = *(bs->start + 1);
187 :     #ifndef ARCH_IS_BIG_ENDIAN
188 :     BSWAP(tmp);
189 :     #endif
190 :     bs->bufb = tmp;
191 :    
192 :     bs->buf = 0;
193 : edgomez 1.6 bs->pos = 0;
194 : Isibaar 1.1 }
195 :    
196 :    
197 :     /* reads n bits from bitstream without changing the stream pos */
198 :    
199 : edgomez 1.6 static uint32_t __inline
200 :     BitstreamShowBits(Bitstream * const bs,
201 :     const uint32_t bits)
202 : Isibaar 1.1 {
203 :     int nbit = (bits + bs->pos) - 32;
204 : edgomez 1.6
205 :     if (nbit > 0) {
206 :     return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) | (bs->
207 :     bufb >> (32 -
208 :     nbit));
209 :     } else {
210 : Isibaar 1.1 return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits);
211 :     }
212 :     }
213 :    
214 :    
215 :     /* skip n bits forward in bitstream */
216 :    
217 : edgomez 1.11 static void __inline
218 : edgomez 1.6 BitstreamSkip(Bitstream * const bs,
219 :     const uint32_t bits)
220 : Isibaar 1.1 {
221 :     bs->pos += bits;
222 :    
223 : edgomez 1.6 if (bs->pos >= 32) {
224 : Isibaar 1.1 uint32_t tmp;
225 :    
226 :     bs->bufa = bs->bufb;
227 : edgomez 1.6 tmp = *((uint32_t *) bs->tail + 2);
228 : Isibaar 1.1 #ifndef ARCH_IS_BIG_ENDIAN
229 :     BSWAP(tmp);
230 :     #endif
231 :     bs->bufb = tmp;
232 :     bs->tail++;
233 :     bs->pos -= 32;
234 :     }
235 :     }
236 : suxen_drol 1.9
237 :    
238 :     // number of bits to next byte alignment
239 : edgomez 1.11 static uint32_t __inline
240 : suxen_drol 1.9 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 : edgomez 1.11 static uint32_t __inline
249 : suxen_drol 1.9 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 : Isibaar 1.1
267 :    
268 :     /* move forward to the next byte boundary */
269 :    
270 : edgomez 1.11 static void __inline
271 : edgomez 1.6 BitstreamByteAlign(Bitstream * const bs)
272 : Isibaar 1.1 {
273 :     uint32_t remainder = bs->pos % 8;
274 : edgomez 1.6
275 :     if (remainder) {
276 : Isibaar 1.1 BitstreamSkip(bs, 8 - remainder);
277 :     }
278 :     }
279 :    
280 :    
281 :     /* bitstream length (unit bits) */
282 :    
283 : edgomez 1.6 static uint32_t __inline
284 :     BitstreamPos(const Bitstream * const bs)
285 : Isibaar 1.1 {
286 : Isibaar 1.7 return 8 * ((ptr_t)bs->tail - (ptr_t)bs->start) + bs->pos;
287 : Isibaar 1.1 }
288 :    
289 :    
290 :     /* flush the bitstream & return length (unit bytes)
291 :     NOTE: assumes no futher bitstream functions will be called.
292 :     */
293 :    
294 : edgomez 1.6 static uint32_t __inline
295 :     BitstreamLength(Bitstream * const bs)
296 : Isibaar 1.1 {
297 : Isibaar 1.7 uint32_t len = (ptr_t) bs->tail - (ptr_t) bs->start;
298 : Isibaar 1.1
299 : edgomez 1.6 if (bs->pos) {
300 : Isibaar 1.1 uint32_t b = bs->buf;
301 : edgomez 1.6
302 : Isibaar 1.1 #ifndef ARCH_IS_BIG_ENDIAN
303 :     BSWAP(b);
304 :     #endif
305 :     *bs->tail = b;
306 :    
307 :     len += (bs->pos + 7) / 8;
308 : edgomez 1.6 }
309 : Isibaar 1.1
310 :     return len;
311 :     }
312 :    
313 :    
314 :     /* move bitstream position forward by n bits and write out buffer if needed */
315 :    
316 : edgomez 1.6 static void __inline
317 :     BitstreamForward(Bitstream * const bs,
318 :     const uint32_t bits)
319 : Isibaar 1.1 {
320 : edgomez 1.6 bs->pos += bits;
321 : Isibaar 1.1
322 : edgomez 1.6 if (bs->pos >= 32) {
323 : Isibaar 1.1 uint32_t b = bs->buf;
324 : edgomez 1.6
325 : Isibaar 1.1 #ifndef ARCH_IS_BIG_ENDIAN
326 :     BSWAP(b);
327 :     #endif
328 :     *bs->tail++ = b;
329 :     bs->buf = 0;
330 :     bs->pos -= 32;
331 : edgomez 1.6 }
332 : Isibaar 1.1 }
333 :    
334 :    
335 :     /* pad bitstream to the next byte boundary */
336 :    
337 : edgomez 1.6 static void __inline
338 :     BitstreamPad(Bitstream * const bs)
339 : Isibaar 1.1 {
340 :     uint32_t remainder = bs->pos % 8;
341 :    
342 : edgomez 1.6 if (remainder) {
343 : Isibaar 1.1 BitstreamForward(bs, 8 - remainder);
344 : edgomez 1.6 }
345 : Isibaar 1.1 }
346 :    
347 :    
348 :     /* read n bits from bitstream */
349 :    
350 : edgomez 1.6 static uint32_t __inline
351 :     BitstreamGetBits(Bitstream * const bs,
352 :     const uint32_t n)
353 : Isibaar 1.1 {
354 :     uint32_t ret = BitstreamShowBits(bs, n);
355 : edgomez 1.6
356 : Isibaar 1.1 BitstreamSkip(bs, n);
357 :     return ret;
358 :     }
359 :    
360 :    
361 :     /* read single bit from bitstream */
362 :    
363 : edgomez 1.6 static uint32_t __inline
364 :     BitstreamGetBit(Bitstream * const bs)
365 : Isibaar 1.1 {
366 :     return BitstreamGetBits(bs, 1);
367 :     }
368 :    
369 :    
370 :     /* write single bit to bitstream */
371 :    
372 : edgomez 1.6 static void __inline
373 :     BitstreamPutBit(Bitstream * const bs,
374 :     const uint32_t bit)
375 : Isibaar 1.1 {
376 : edgomez 1.6 if (bit)
377 : Isibaar 1.1 bs->buf |= (0x80000000 >> bs->pos);
378 :    
379 : edgomez 1.6 BitstreamForward(bs, 1);
380 : Isibaar 1.1 }
381 :    
382 :    
383 :     /* write n bits to bitstream */
384 :    
385 : edgomez 1.6 static void __inline
386 :     BitstreamPutBits(Bitstream * const bs,
387 :     const uint32_t value,
388 :     const uint32_t size)
389 : Isibaar 1.1 {
390 :     uint32_t shift = 32 - bs->pos - size;
391 :    
392 :     if (shift <= 32) {
393 :     bs->buf |= value << shift;
394 :     BitstreamForward(bs, size);
395 :     } else {
396 :     uint32_t remainder;
397 :    
398 :     shift = size - (32 - bs->pos);
399 :     bs->buf |= value >> shift;
400 :     BitstreamForward(bs, size - shift);
401 :     remainder = shift;
402 :    
403 :     shift = 32 - shift;
404 : edgomez 1.6
405 : Isibaar 1.1 bs->buf |= value << shift;
406 :     BitstreamForward(bs, remainder);
407 :     }
408 :     }
409 :    
410 : edgomez 1.11 #endif /* _BITSTREAM_H_ */

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