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

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