[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.4 - (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 :     // header stuff
115 :     int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding,
116 :     uint32_t * quant, uint32_t * fcode, uint32_t * intra_dc_threshold);
117 :    
118 :    
119 :     void BitstreamWriteVolHeader(Bitstream * const bs,
120 : suxen_drol 1.4 const MBParam * pParam,
121 :     const FRAMEINFO * frame);
122 : Isibaar 1.1
123 :     void BitstreamWriteVopHeader(Bitstream * const bs,
124 : suxen_drol 1.4 const MBParam * pParam,
125 :     const FRAMEINFO * frame);
126 : Isibaar 1.1
127 :     /* initialise bitstream structure */
128 :    
129 :     static void __inline BitstreamInit(Bitstream * const bs,
130 :     void * const bitstream,
131 :     uint32_t length)
132 :     {
133 :     uint32_t tmp;
134 :    
135 :     bs->start = bs->tail = (uint32_t*)bitstream;
136 :    
137 :     tmp = *(uint32_t *)bitstream;
138 :     #ifndef ARCH_IS_BIG_ENDIAN
139 :     BSWAP(tmp);
140 :     #endif
141 :     bs->bufa = tmp;
142 :    
143 :     tmp = *((uint32_t *)bitstream + 1);
144 :     #ifndef ARCH_IS_BIG_ENDIAN
145 :     BSWAP(tmp);
146 :     #endif
147 :     bs->bufb = tmp;
148 :    
149 :     bs->buf = 0;
150 :     bs->pos = 0;
151 :     bs->length = length;
152 :     }
153 :    
154 :    
155 :     /* reset bitstream state */
156 :    
157 :     static void __inline BitstreamReset(Bitstream * const bs)
158 :     {
159 :     uint32_t tmp;
160 :    
161 :     bs->tail = bs->start;
162 :    
163 :     tmp = *bs->start;
164 :     #ifndef ARCH_IS_BIG_ENDIAN
165 :     BSWAP(tmp);
166 :     #endif
167 :     bs->bufa = tmp;
168 :    
169 :     tmp = *(bs->start + 1);
170 :     #ifndef ARCH_IS_BIG_ENDIAN
171 :     BSWAP(tmp);
172 :     #endif
173 :     bs->bufb = tmp;
174 :    
175 :     bs->buf = 0;
176 :     bs->pos = 0;
177 :     }
178 :    
179 :    
180 :     /* reads n bits from bitstream without changing the stream pos */
181 :    
182 :     static uint32_t __inline BitstreamShowBits(Bitstream * const bs,
183 :     const uint32_t bits)
184 :     {
185 :     int nbit = (bits + bs->pos) - 32;
186 :     if (nbit > 0)
187 :     {
188 :     return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) |
189 :     (bs->bufb >> (32 - nbit));
190 :     }
191 :     else
192 :     {
193 :     return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits);
194 :     }
195 :     }
196 :    
197 :    
198 :     /* skip n bits forward in bitstream */
199 :    
200 :     static __inline void BitstreamSkip(Bitstream * const bs, const uint32_t bits)
201 :     {
202 :     bs->pos += bits;
203 :    
204 :     if (bs->pos >= 32)
205 :     {
206 :     uint32_t tmp;
207 :    
208 :     bs->bufa = bs->bufb;
209 :     tmp = *((uint32_t *)bs->tail + 2);
210 :     #ifndef ARCH_IS_BIG_ENDIAN
211 :     BSWAP(tmp);
212 :     #endif
213 :     bs->bufb = tmp;
214 :     bs->tail++;
215 :     bs->pos -= 32;
216 :     }
217 :     }
218 :    
219 :    
220 :     /* move forward to the next byte boundary */
221 :    
222 :     static __inline void BitstreamByteAlign(Bitstream * const bs)
223 :     {
224 :     uint32_t remainder = bs->pos % 8;
225 :     if (remainder)
226 :     {
227 :     BitstreamSkip(bs, 8 - remainder);
228 :     }
229 :     }
230 :    
231 :    
232 :     /* bitstream length (unit bits) */
233 :    
234 :     static uint32_t __inline BitstreamPos(const Bitstream * const bs)
235 :     {
236 :     return 8 * ((uint32_t)bs->tail - (uint32_t)bs->start) + bs->pos;
237 :     }
238 :    
239 :    
240 :     /* flush the bitstream & return length (unit bytes)
241 :     NOTE: assumes no futher bitstream functions will be called.
242 :     */
243 :    
244 :     static uint32_t __inline BitstreamLength(Bitstream * const bs)
245 :     {
246 :     uint32_t len = (uint32_t) bs->tail - (uint32_t) bs->start;
247 :    
248 :     if (bs->pos)
249 :     {
250 :     uint32_t b = bs->buf;
251 :     #ifndef ARCH_IS_BIG_ENDIAN
252 :     BSWAP(b);
253 :     #endif
254 :     *bs->tail = b;
255 :    
256 :     len += (bs->pos + 7) / 8;
257 :     }
258 :    
259 :     return len;
260 :     }
261 :    
262 :    
263 :     /* move bitstream position forward by n bits and write out buffer if needed */
264 :    
265 :     static void __inline BitstreamForward(Bitstream * const bs, const uint32_t bits)
266 :     {
267 :     bs->pos += bits;
268 :    
269 :     if (bs->pos >= 32)
270 :     {
271 :     uint32_t b = bs->buf;
272 :     #ifndef ARCH_IS_BIG_ENDIAN
273 :     BSWAP(b);
274 :     #endif
275 :     *bs->tail++ = b;
276 :     bs->buf = 0;
277 :     bs->pos -= 32;
278 :     }
279 :     }
280 :    
281 :    
282 :     /* pad bitstream to the next byte boundary */
283 :    
284 :     static void __inline BitstreamPad(Bitstream * const bs)
285 :     {
286 :     uint32_t remainder = bs->pos % 8;
287 :    
288 :     if (remainder)
289 :     {
290 :     BitstreamForward(bs, 8 - remainder);
291 :     }
292 :     }
293 :    
294 :    
295 :     /* read n bits from bitstream */
296 :    
297 :     static uint32_t __inline BitstreamGetBits(Bitstream * const bs,
298 :     const uint32_t n)
299 :     {
300 :     uint32_t ret = BitstreamShowBits(bs, n);
301 :     BitstreamSkip(bs, n);
302 :     return ret;
303 :     }
304 :    
305 :    
306 :     /* read single bit from bitstream */
307 :    
308 :     static uint32_t __inline BitstreamGetBit(Bitstream * const bs)
309 :     {
310 :     return BitstreamGetBits(bs, 1);
311 :     }
312 :    
313 :    
314 :     /* write single bit to bitstream */
315 :    
316 :     static void __inline BitstreamPutBit(Bitstream * const bs,
317 :     const uint32_t bit)
318 :     {
319 :     if (bit)
320 :     bs->buf |= (0x80000000 >> bs->pos);
321 :    
322 :     BitstreamForward(bs, 1);
323 :     }
324 :    
325 :    
326 :     /* write n bits to bitstream */
327 :    
328 :     static void __inline BitstreamPutBits(Bitstream * const bs,
329 :     const uint32_t value,
330 :     const uint32_t size)
331 :     {
332 :     uint32_t shift = 32 - bs->pos - size;
333 :    
334 :     if (shift <= 32) {
335 :     bs->buf |= value << shift;
336 :     BitstreamForward(bs, size);
337 :     } else {
338 :     uint32_t remainder;
339 :    
340 :     shift = size - (32 - bs->pos);
341 :     bs->buf |= value >> shift;
342 :     BitstreamForward(bs, size - shift);
343 :     remainder = shift;
344 :    
345 :     shift = 32 - shift;
346 :    
347 :     bs->buf |= value << shift;
348 :     BitstreamForward(bs, remainder);
349 :     }
350 :     }
351 :    
352 :     #endif /* _BITSTREAM_H_ */

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