Parent Directory | Revision Log
Revision 3 - (view) (download)
1 : | Isibaar | 3 | /****************************************************************************** |
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 : | * 04.03.2002 putbits speedup (Isibaar) * | ||
45 : | * 03.03.2002 merged BITREADER and BITWRITER (Isibaar) * | ||
46 : | * 16.12.2001 inital version * | ||
47 : | * * | ||
48 : | ******************************************************************************/ | ||
49 : | |||
50 : | #ifndef _BITSTREAM_H_ | ||
51 : | #define _BITSTREAM_H_ | ||
52 : | |||
53 : | #include "../portab.h" | ||
54 : | #include "../decoder.h" | ||
55 : | #include "../encoder.h" | ||
56 : | |||
57 : | // comment any #defs we dont use | ||
58 : | |||
59 : | #define VIDOBJ_START_CODE 0x00000100 /* ..0x0000011f */ | ||
60 : | #define VIDOBJLAY_START_CODE 0x00000120 /* ..0x0000012f */ | ||
61 : | #define VISOBJSEQ_START_CODE 0x000001b0 | ||
62 : | #define VISOBJSEQ_STOP_CODE 0x000001b1 /* ??? */ | ||
63 : | #define USERDATA_START_CODE 0x000001b2 | ||
64 : | #define GRPOFVOP_START_CODE 0x000001b3 | ||
65 : | //#define VIDSESERR_ERROR_CODE 0x000001b4 | ||
66 : | #define VISOBJ_START_CODE 0x000001b5 | ||
67 : | //#define SLICE_START_CODE 0x000001b7 | ||
68 : | //#define EXT_START_CODE 0x000001b8 | ||
69 : | |||
70 : | |||
71 : | #define VISOBJ_TYPE_VIDEO 1 | ||
72 : | //#define VISOBJ_TYPE_STILLTEXTURE 2 | ||
73 : | //#define VISOBJ_TYPE_MESH 3 | ||
74 : | //#define VISOBJ_TYPE_FBA 4 | ||
75 : | //#define VISOBJ_TYPE_3DMESH 5 | ||
76 : | |||
77 : | |||
78 : | #define VIDOBJLAY_TYPE_SIMPLE 1 | ||
79 : | //#define VIDOBJLAY_TYPE_SIMPLE_SCALABLE 2 | ||
80 : | #define VIDOBJLAY_TYPE_CORE 3 | ||
81 : | #define VIDOBJLAY_TYPE_MAIN 4 | ||
82 : | |||
83 : | |||
84 : | //#define VIDOBJLAY_AR_SQUARE 1 | ||
85 : | //#define VIDOBJLAY_AR_625TYPE_43 2 | ||
86 : | //#define VIDOBJLAY_AR_525TYPE_43 3 | ||
87 : | //#define VIDOBJLAY_AR_625TYPE_169 8 | ||
88 : | //#define VIDOBJLAY_AR_525TYPE_169 9 | ||
89 : | #define VIDOBJLAY_AR_EXTPAR 15 | ||
90 : | |||
91 : | |||
92 : | #define VIDOBJLAY_SHAPE_RECTANGULAR 0 | ||
93 : | #define VIDOBJLAY_SHAPE_BINARY 1 | ||
94 : | #define VIDOBJLAY_SHAPE_BINARY_ONLY 2 | ||
95 : | #define VIDOBJLAY_SHAPE_GRAYSCALE 3 | ||
96 : | |||
97 : | #define VO_START_CODE 0x8 | ||
98 : | #define VOL_START_CODE 0x12 | ||
99 : | #define VOP_START_CODE 0x1b6 | ||
100 : | |||
101 : | #define READ_MARKER() BitstreamSkip(bs, 1) | ||
102 : | #define WRITE_MARKER() BitstreamPutBit(bs, 1) | ||
103 : | |||
104 : | // vop coding types | ||
105 : | // intra, prediction, backward, sprite, not_coded | ||
106 : | #define I_VOP 0 | ||
107 : | #define P_VOP 1 | ||
108 : | #define B_VOP 2 | ||
109 : | #define S_VOP 3 | ||
110 : | #define N_VOP 4 | ||
111 : | |||
112 : | |||
113 : | typedef struct | ||
114 : | { | ||
115 : | uint32_t bufa; | ||
116 : | uint32_t bufb; | ||
117 : | uint32_t buf; | ||
118 : | uint32_t pos; | ||
119 : | uint32_t *tail; | ||
120 : | uint32_t *start; | ||
121 : | uint32_t length; | ||
122 : | } | ||
123 : | Bitstream; | ||
124 : | |||
125 : | |||
126 : | // header stuff | ||
127 : | int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, | ||
128 : | uint32_t * quant, uint32_t * fcode, uint32_t * intra_dc_threshold); | ||
129 : | |||
130 : | |||
131 : | void BitstreamWriteVolHeader(Bitstream * const bs, | ||
132 : | const int width, | ||
133 : | const int height, | ||
134 : | const int quant_type); | ||
135 : | |||
136 : | |||
137 : | void BitstreamWriteVopHeader(Bitstream * const bs, | ||
138 : | VOP_TYPE prediction_type, | ||
139 : | const int rounding_type, | ||
140 : | const uint32_t quant, | ||
141 : | const uint32_t fcode); | ||
142 : | |||
143 : | |||
144 : | /* initialise bitstream structure */ | ||
145 : | |||
146 : | static void __inline BitstreamInit(Bitstream * const bs, | ||
147 : | void * const bitstream, | ||
148 : | uint32_t length) | ||
149 : | { | ||
150 : | uint32_t tmp; | ||
151 : | |||
152 : | bs->start = bs->tail = (uint32_t*)bitstream; | ||
153 : | |||
154 : | tmp = *(uint32_t *)bitstream; | ||
155 : | #ifndef ARCH_IS_BIG_ENDIAN | ||
156 : | BSWAP(tmp); | ||
157 : | #endif | ||
158 : | bs->bufa = tmp; | ||
159 : | |||
160 : | tmp = *((uint32_t *)bitstream + 1); | ||
161 : | #ifndef ARCH_IS_BIG_ENDIAN | ||
162 : | BSWAP(tmp); | ||
163 : | #endif | ||
164 : | bs->bufb = tmp; | ||
165 : | |||
166 : | bs->buf = 0; | ||
167 : | bs->pos = 0; | ||
168 : | bs->length = length; | ||
169 : | } | ||
170 : | |||
171 : | |||
172 : | /* reset bitstream state */ | ||
173 : | |||
174 : | static void __inline BitstreamReset(Bitstream * const bs) | ||
175 : | { | ||
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 : | bs->pos = 0; | ||
194 : | } | ||
195 : | |||
196 : | |||
197 : | /* reads n bits from bitstream without changing the stream pos */ | ||
198 : | |||
199 : | static uint32_t __inline BitstreamShowBits(Bitstream * const bs, | ||
200 : | const uint32_t bits) | ||
201 : | { | ||
202 : | int nbit = (bits + bs->pos) - 32; | ||
203 : | if (nbit > 0) | ||
204 : | { | ||
205 : | return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) | | ||
206 : | (bs->bufb >> (32 - nbit)); | ||
207 : | } | ||
208 : | else | ||
209 : | { | ||
210 : | return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits); | ||
211 : | } | ||
212 : | } | ||
213 : | |||
214 : | |||
215 : | /* skip n bits forward in bitstream */ | ||
216 : | |||
217 : | static __inline void BitstreamSkip(Bitstream * const bs, const uint32_t bits) | ||
218 : | { | ||
219 : | bs->pos += bits; | ||
220 : | |||
221 : | if (bs->pos >= 32) | ||
222 : | { | ||
223 : | uint32_t tmp; | ||
224 : | |||
225 : | bs->bufa = bs->bufb; | ||
226 : | tmp = *((uint32_t *)bs->tail + 2); | ||
227 : | #ifndef ARCH_IS_BIG_ENDIAN | ||
228 : | BSWAP(tmp); | ||
229 : | #endif | ||
230 : | bs->bufb = tmp; | ||
231 : | bs->tail++; | ||
232 : | bs->pos -= 32; | ||
233 : | } | ||
234 : | } | ||
235 : | |||
236 : | |||
237 : | /* move forward to the next byte boundary */ | ||
238 : | |||
239 : | static __inline void BitstreamByteAlign(Bitstream * const bs) | ||
240 : | { | ||
241 : | uint32_t remainder = bs->pos % 8; | ||
242 : | if (remainder) | ||
243 : | { | ||
244 : | BitstreamSkip(bs, 8 - remainder); | ||
245 : | } | ||
246 : | } | ||
247 : | |||
248 : | |||
249 : | /* bitstream length (unit bits) */ | ||
250 : | |||
251 : | static uint32_t __inline BitstreamPos(const Bitstream * const bs) | ||
252 : | { | ||
253 : | return 8 * ((uint32_t)bs->tail - (uint32_t)bs->start) + bs->pos; | ||
254 : | } | ||
255 : | |||
256 : | |||
257 : | /* flush the bitstream & return length (unit bytes) | ||
258 : | NOTE: assumes no futher bitstream functions will be called. | ||
259 : | */ | ||
260 : | |||
261 : | static uint32_t __inline BitstreamLength(Bitstream * const bs) | ||
262 : | { | ||
263 : | uint32_t len = (uint32_t) bs->tail - (uint32_t) bs->start; | ||
264 : | |||
265 : | if (bs->pos) | ||
266 : | { | ||
267 : | uint32_t b = bs->buf; | ||
268 : | #ifndef ARCH_IS_BIG_ENDIAN | ||
269 : | BSWAP(b); | ||
270 : | #endif | ||
271 : | *bs->tail = b; | ||
272 : | |||
273 : | len += (bs->pos + 7) / 8; | ||
274 : | } | ||
275 : | |||
276 : | return len; | ||
277 : | } | ||
278 : | |||
279 : | |||
280 : | /* move bitstream position forward by n bits and write out buffer if needed */ | ||
281 : | |||
282 : | static void __inline BitstreamForward(Bitstream * const bs, const uint32_t bits) | ||
283 : | { | ||
284 : | bs->pos += bits; | ||
285 : | |||
286 : | if (bs->pos >= 32) | ||
287 : | { | ||
288 : | uint32_t b = bs->buf; | ||
289 : | #ifndef ARCH_IS_BIG_ENDIAN | ||
290 : | BSWAP(b); | ||
291 : | #endif | ||
292 : | *bs->tail++ = b; | ||
293 : | bs->buf = 0; | ||
294 : | bs->pos -= 32; | ||
295 : | } | ||
296 : | } | ||
297 : | |||
298 : | |||
299 : | /* pad bitstream to the next byte boundary */ | ||
300 : | |||
301 : | static void __inline BitstreamPad(Bitstream * const bs) | ||
302 : | { | ||
303 : | uint32_t remainder = bs->pos % 8; | ||
304 : | |||
305 : | if (remainder) | ||
306 : | { | ||
307 : | BitstreamForward(bs, 8 - remainder); | ||
308 : | } | ||
309 : | } | ||
310 : | |||
311 : | |||
312 : | /* read n bits from bitstream */ | ||
313 : | |||
314 : | static uint32_t __inline BitstreamGetBits(Bitstream * const bs, | ||
315 : | const uint32_t n) | ||
316 : | { | ||
317 : | uint32_t ret = BitstreamShowBits(bs, n); | ||
318 : | BitstreamSkip(bs, n); | ||
319 : | return ret; | ||
320 : | } | ||
321 : | |||
322 : | |||
323 : | /* read single bit from bitstream */ | ||
324 : | |||
325 : | static uint32_t __inline BitstreamGetBit(Bitstream * const bs) | ||
326 : | { | ||
327 : | return BitstreamGetBits(bs, 1); | ||
328 : | } | ||
329 : | |||
330 : | |||
331 : | /* write single bit to bitstream */ | ||
332 : | |||
333 : | static void __inline BitstreamPutBit(Bitstream * const bs, | ||
334 : | const uint32_t bit) | ||
335 : | { | ||
336 : | if (bit) | ||
337 : | bs->buf |= (0x80000000 >> bs->pos); | ||
338 : | |||
339 : | BitstreamForward(bs, 1); | ||
340 : | } | ||
341 : | |||
342 : | |||
343 : | /* write n bits to bitstream */ | ||
344 : | |||
345 : | static void __inline BitstreamPutBits(Bitstream * const bs, | ||
346 : | const uint32_t value, | ||
347 : | const uint32_t size) | ||
348 : | { | ||
349 : | uint32_t shift = 32 - bs->pos - size; | ||
350 : | |||
351 : | if (shift <= 32) { | ||
352 : | bs->buf |= value << shift; | ||
353 : | BitstreamForward(bs, size); | ||
354 : | } else { | ||
355 : | uint32_t remainder; | ||
356 : | |||
357 : | shift = size - (32 - bs->pos); | ||
358 : | bs->buf |= value >> shift; | ||
359 : | BitstreamForward(bs, size - shift); | ||
360 : | remainder = shift; | ||
361 : | |||
362 : | shift = 32 - shift; | ||
363 : | |||
364 : | bs->buf |= value << shift; | ||
365 : | BitstreamForward(bs, remainder); | ||
366 : | } | ||
367 : | } | ||
368 : | |||
369 : | #endif /* _BITSTREAM_H_ */ |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |