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

1 : Isibaar 1.1 /******************************************************************************
2 :     * *
3 :     * This file is part of XviD, a free MPEG-4 video encoder/decoder *
4 :     * *
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 *
7 :     * software module in hardware or software products are advised that its *
8 :     * use may infringe existing patents or copyrights, and any such use *
9 :     * would be at such party's own risk. The original developer of this *
10 :     * software module and his/her company, and subsequent editors and their *
11 :     * companies, will have no liability for use of this software or *
12 :     * modifications or derivatives thereof. *
13 :     * *
14 :     * XviD is free software; you can redistribute it and/or modify it *
15 :     * under the terms of the GNU General Public License as published by *
16 :     * the Free Software Foundation; either version 2 of the License, or *
17 :     * (at your option) any later version. *
18 :     * *
19 :     * XviD is distributed in the hope that it will be useful, but *
20 :     * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 :     * GNU General Public License for more details. *
23 :     * *
24 :     * You should have received a copy of the GNU General Public License *
25 :     * along with this program; if not, write to the Free Software *
26 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
27 :     * *
28 :     ******************************************************************************/
29 :    
30 :     /******************************************************************************
31 :     * *
32 :     * bitstream.h *
33 :     * *
34 :     * Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> *
35 :     * *
36 :     * For more information visit the XviD homepage: http://www.xvid.org *
37 :     * *
38 :     ******************************************************************************/
39 :    
40 :     /******************************************************************************
41 :     * *
42 :     * Revision history: *
43 :     * *
44 : h 1.2 * 26.03.2002 interlacing support - modified putvol/vopheaders paramters
45 : Isibaar 1.1 * 04.03.2002 putbits speedup (Isibaar) *
46 :     * 03.03.2002 merged BITREADER and BITWRITER (Isibaar) *
47 :     * 16.12.2001 inital version *
48 :     * *
49 :     ******************************************************************************/
50 :    
51 :     #ifndef _BITSTREAM_H_
52 :     #define _BITSTREAM_H_
53 :    
54 :     #include "../portab.h"
55 :     #include "../decoder.h"
56 :     #include "../encoder.h"
57 :    
58 :     // comment any #defs we dont use
59 :    
60 :     #define VIDOBJ_START_CODE 0x00000100 /* ..0x0000011f */
61 :     #define VIDOBJLAY_START_CODE 0x00000120 /* ..0x0000012f */
62 :     #define VISOBJSEQ_START_CODE 0x000001b0
63 :     #define VISOBJSEQ_STOP_CODE 0x000001b1 /* ??? */
64 :     #define USERDATA_START_CODE 0x000001b2
65 :     #define GRPOFVOP_START_CODE 0x000001b3
66 :     //#define VIDSESERR_ERROR_CODE 0x000001b4
67 :     #define VISOBJ_START_CODE 0x000001b5
68 :     //#define SLICE_START_CODE 0x000001b7
69 :     //#define EXT_START_CODE 0x000001b8
70 :    
71 :    
72 :     #define VISOBJ_TYPE_VIDEO 1
73 :     //#define VISOBJ_TYPE_STILLTEXTURE 2
74 :     //#define VISOBJ_TYPE_MESH 3
75 :     //#define VISOBJ_TYPE_FBA 4
76 :     //#define VISOBJ_TYPE_3DMESH 5
77 :    
78 :    
79 :     #define VIDOBJLAY_TYPE_SIMPLE 1
80 :     //#define VIDOBJLAY_TYPE_SIMPLE_SCALABLE 2
81 :     #define VIDOBJLAY_TYPE_CORE 3
82 :     #define VIDOBJLAY_TYPE_MAIN 4
83 :    
84 :    
85 :     //#define VIDOBJLAY_AR_SQUARE 1
86 :     //#define VIDOBJLAY_AR_625TYPE_43 2
87 :     //#define VIDOBJLAY_AR_525TYPE_43 3
88 :     //#define VIDOBJLAY_AR_625TYPE_169 8
89 :     //#define VIDOBJLAY_AR_525TYPE_169 9
90 :     #define VIDOBJLAY_AR_EXTPAR 15
91 :    
92 :    
93 :     #define VIDOBJLAY_SHAPE_RECTANGULAR 0
94 :     #define VIDOBJLAY_SHAPE_BINARY 1
95 :     #define VIDOBJLAY_SHAPE_BINARY_ONLY 2
96 :     #define VIDOBJLAY_SHAPE_GRAYSCALE 3
97 :    
98 :     #define VO_START_CODE 0x8
99 :     #define VOL_START_CODE 0x12
100 :     #define VOP_START_CODE 0x1b6
101 :    
102 :     #define READ_MARKER() BitstreamSkip(bs, 1)
103 :     #define WRITE_MARKER() BitstreamPutBit(bs, 1)
104 :    
105 :     // vop coding types
106 :     // intra, prediction, backward, sprite, not_coded
107 :     #define I_VOP 0
108 :     #define P_VOP 1
109 :     #define B_VOP 2
110 :     #define S_VOP 3
111 :     #define N_VOP 4
112 :    
113 :    
114 :     typedef struct
115 :     {
116 :     uint32_t bufa;
117 :     uint32_t bufb;
118 :     uint32_t buf;
119 :     uint32_t pos;
120 :     uint32_t *tail;
121 :     uint32_t *start;
122 :     uint32_t length;
123 :     }
124 :     Bitstream;
125 :    
126 :    
127 :     // header stuff
128 :     int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding,
129 :     uint32_t * quant, uint32_t * fcode, uint32_t * intra_dc_threshold);
130 :    
131 :    
132 :     void BitstreamWriteVolHeader(Bitstream * const bs,
133 : h 1.2 const MBParam * pParam);
134 : Isibaar 1.1
135 :    
136 :     void BitstreamWriteVopHeader(Bitstream * const bs,
137 : h 1.2 const MBParam * pParam);
138 : Isibaar 1.1
139 :    
140 :     /* initialise bitstream structure */
141 :    
142 :     static void __inline BitstreamInit(Bitstream * const bs,
143 :     void * const bitstream,
144 :     uint32_t length)
145 :     {
146 :     uint32_t tmp;
147 :    
148 :     bs->start = bs->tail = (uint32_t*)bitstream;
149 :    
150 :     tmp = *(uint32_t *)bitstream;
151 :     #ifndef ARCH_IS_BIG_ENDIAN
152 :     BSWAP(tmp);
153 :     #endif
154 :     bs->bufa = tmp;
155 :    
156 :     tmp = *((uint32_t *)bitstream + 1);
157 :     #ifndef ARCH_IS_BIG_ENDIAN
158 :     BSWAP(tmp);
159 :     #endif
160 :     bs->bufb = tmp;
161 :    
162 :     bs->buf = 0;
163 :     bs->pos = 0;
164 :     bs->length = length;
165 :     }
166 :    
167 :    
168 :     /* reset bitstream state */
169 :    
170 :     static void __inline BitstreamReset(Bitstream * const bs)
171 :     {
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 :     bs->pos = 0;
190 :     }
191 :    
192 :    
193 :     /* reads n bits from bitstream without changing the stream pos */
194 :    
195 :     static uint32_t __inline BitstreamShowBits(Bitstream * const bs,
196 :     const uint32_t bits)
197 :     {
198 :     int nbit = (bits + bs->pos) - 32;
199 :     if (nbit > 0)
200 :     {
201 :     return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) |
202 :     (bs->bufb >> (32 - nbit));
203 :     }
204 :     else
205 :     {
206 :     return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits);
207 :     }
208 :     }
209 :    
210 :    
211 :     /* skip n bits forward in bitstream */
212 :    
213 :     static __inline void BitstreamSkip(Bitstream * const bs, const uint32_t bits)
214 :     {
215 :     bs->pos += bits;
216 :    
217 :     if (bs->pos >= 32)
218 :     {
219 :     uint32_t tmp;
220 :    
221 :     bs->bufa = bs->bufb;
222 :     tmp = *((uint32_t *)bs->tail + 2);
223 :     #ifndef ARCH_IS_BIG_ENDIAN
224 :     BSWAP(tmp);
225 :     #endif
226 :     bs->bufb = tmp;
227 :     bs->tail++;
228 :     bs->pos -= 32;
229 :     }
230 :     }
231 :    
232 :    
233 :     /* move forward to the next byte boundary */
234 :    
235 :     static __inline void BitstreamByteAlign(Bitstream * const bs)
236 :     {
237 :     uint32_t remainder = bs->pos % 8;
238 :     if (remainder)
239 :     {
240 :     BitstreamSkip(bs, 8 - remainder);
241 :     }
242 :     }
243 :    
244 :    
245 :     /* bitstream length (unit bits) */
246 :    
247 :     static uint32_t __inline BitstreamPos(const Bitstream * const bs)
248 :     {
249 :     return 8 * ((uint32_t)bs->tail - (uint32_t)bs->start) + bs->pos;
250 :     }
251 :    
252 :    
253 :     /* flush the bitstream & return length (unit bytes)
254 :     NOTE: assumes no futher bitstream functions will be called.
255 :     */
256 :    
257 :     static uint32_t __inline BitstreamLength(Bitstream * const bs)
258 :     {
259 :     uint32_t len = (uint32_t) bs->tail - (uint32_t) bs->start;
260 :    
261 :     if (bs->pos)
262 :     {
263 :     uint32_t b = bs->buf;
264 :     #ifndef ARCH_IS_BIG_ENDIAN
265 :     BSWAP(b);
266 :     #endif
267 :     *bs->tail = b;
268 :    
269 :     len += (bs->pos + 7) / 8;
270 :     }
271 :    
272 :     return len;
273 :     }
274 :    
275 :    
276 :     /* move bitstream position forward by n bits and write out buffer if needed */
277 :    
278 :     static void __inline BitstreamForward(Bitstream * const bs, const uint32_t bits)
279 :     {
280 :     bs->pos += bits;
281 :    
282 :     if (bs->pos >= 32)
283 :     {
284 :     uint32_t b = bs->buf;
285 :     #ifndef ARCH_IS_BIG_ENDIAN
286 :     BSWAP(b);
287 :     #endif
288 :     *bs->tail++ = b;
289 :     bs->buf = 0;
290 :     bs->pos -= 32;
291 :     }
292 :     }
293 :    
294 :    
295 :     /* pad bitstream to the next byte boundary */
296 :    
297 :     static void __inline BitstreamPad(Bitstream * const bs)
298 :     {
299 :     uint32_t remainder = bs->pos % 8;
300 :    
301 :     if (remainder)
302 :     {
303 :     BitstreamForward(bs, 8 - remainder);
304 :     }
305 :     }
306 :    
307 :    
308 :     /* read n bits from bitstream */
309 :    
310 :     static uint32_t __inline BitstreamGetBits(Bitstream * const bs,
311 :     const uint32_t n)
312 :     {
313 :     uint32_t ret = BitstreamShowBits(bs, n);
314 :     BitstreamSkip(bs, n);
315 :     return ret;
316 :     }
317 :    
318 :    
319 :     /* read single bit from bitstream */
320 :    
321 :     static uint32_t __inline BitstreamGetBit(Bitstream * const bs)
322 :     {
323 :     return BitstreamGetBits(bs, 1);
324 :     }
325 :    
326 :    
327 :     /* write single bit to bitstream */
328 :    
329 :     static void __inline BitstreamPutBit(Bitstream * const bs,
330 :     const uint32_t bit)
331 :     {
332 :     if (bit)
333 :     bs->buf |= (0x80000000 >> bs->pos);
334 :    
335 :     BitstreamForward(bs, 1);
336 :     }
337 :    
338 :    
339 :     /* write n bits to bitstream */
340 :    
341 :     static void __inline BitstreamPutBits(Bitstream * const bs,
342 :     const uint32_t value,
343 :     const uint32_t size)
344 :     {
345 :     uint32_t shift = 32 - bs->pos - size;
346 :    
347 :     if (shift <= 32) {
348 :     bs->buf |= value << shift;
349 :     BitstreamForward(bs, size);
350 :     } else {
351 :     uint32_t remainder;
352 :    
353 :     shift = size - (32 - bs->pos);
354 :     bs->buf |= value >> shift;
355 :     BitstreamForward(bs, size - shift);
356 :     remainder = shift;
357 :    
358 :     shift = 32 - shift;
359 :    
360 :     bs->buf |= value << shift;
361 :     BitstreamForward(bs, remainder);
362 :     }
363 :     }
364 :    
365 :     #endif /* _BITSTREAM_H_ */

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