Parent Directory
|
Revision Log
Revision 1098 - (view) (download)
1 : | edgomez | 466 | /***************************************************************************** |
2 : | * | ||
3 : | * XVID MPEG-4 VIDEO CODEC | ||
4 : | * - Bitstream reader/writer inlined functions and constants- | ||
5 : | * | ||
6 : | suxen_drol | 499 | * Copyright (C) 2001-2002 - Peter Ross <pross@xvid.org> |
7 : | edgomez | 466 | * |
8 : | edgomez | 655 | * This file is part of XviD, a free MPEG-4 video encoder/decoder |
9 : | edgomez | 466 | * |
10 : | edgomez | 655 | * XviD is free software; you can redistribute it and/or modify it |
11 : | * under the terms of the GNU General Public License as published by | ||
12 : | * the Free Software Foundation; either version 2 of the License, or | ||
13 : | edgomez | 466 | * (at your option) any later version. |
14 : | * | ||
15 : | * This program is distributed in the hope that it will be useful, | ||
16 : | edgomez | 655 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 : | edgomez | 466 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 : | * GNU General Public License for more details. | ||
19 : | * | ||
20 : | * You should have received a copy of the GNU General Public License | ||
21 : | edgomez | 655 | * along with this program; if not, write to the Free Software |
22 : | edgomez | 466 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 : | * | ||
24 : | edgomez | 655 | * Under section 8 of the GNU General Public License, the copyright |
25 : | * holders of XVID explicitly forbid distribution in the following | ||
26 : | * countries: | ||
27 : | edgomez | 466 | * |
28 : | edgomez | 655 | * - Japan |
29 : | * - United States of America | ||
30 : | * | ||
31 : | * Linking XviD statically or dynamically with other modules is making a | ||
32 : | * combined work based on XviD. Thus, the terms and conditions of the | ||
33 : | * GNU General Public License cover the whole combination. | ||
34 : | * | ||
35 : | * As a special exception, the copyright holders of XviD give you | ||
36 : | * permission to link XviD with independent modules that communicate with | ||
37 : | * XviD solely through the VFW1.1 and DShow interfaces, regardless of the | ||
38 : | * license terms of these independent modules, and to copy and distribute | ||
39 : | * the resulting combined work under terms of your choice, provided that | ||
40 : | * every copy of the combined work is accompanied by a complete copy of | ||
41 : | * the source code of XviD (the version of XviD used to produce the | ||
42 : | * combined work), being distributed under the terms of the GNU General | ||
43 : | * Public License plus this exception. An independent module is a module | ||
44 : | * which is not derived from or based on XviD. | ||
45 : | * | ||
46 : | * Note that people who make modified versions of XviD are not obligated | ||
47 : | * to grant this special exception for their modified versions; it is | ||
48 : | * their choice whether to do so. The GNU General Public License gives | ||
49 : | * permission to release a modified version without this exception; this | ||
50 : | * exception also makes it possible to release a modified version which | ||
51 : | * carries forward this exception. | ||
52 : | * | ||
53 : | edgomez | 1098 | * $Id: bitstream.h,v 1.16.2.2 2003-07-28 12:39:32 edgomez Exp $ |
54 : | edgomez | 655 | * |
55 : | edgomez | 466 | ****************************************************************************/ |
56 : | Isibaar | 3 | |
57 : | #ifndef _BITSTREAM_H_ | ||
58 : | #define _BITSTREAM_H_ | ||
59 : | |||
60 : | edgomez | 971 | #include <stdio.h> |
61 : | |||
62 : | Isibaar | 3 | #include "../portab.h" |
63 : | #include "../decoder.h" | ||
64 : | #include "../encoder.h" | ||
65 : | |||
66 : | edgomez | 466 | |
67 : | /***************************************************************************** | ||
68 : | * Constants | ||
69 : | ****************************************************************************/ | ||
70 : | |||
71 : | edgomez | 677 | /* comment any #defs we dont use */ |
72 : | Isibaar | 3 | |
73 : | #define VIDOBJ_START_CODE 0x00000100 /* ..0x0000011f */ | ||
74 : | #define VIDOBJLAY_START_CODE 0x00000120 /* ..0x0000012f */ | ||
75 : | #define VISOBJSEQ_START_CODE 0x000001b0 | ||
76 : | #define VISOBJSEQ_STOP_CODE 0x000001b1 /* ??? */ | ||
77 : | #define USERDATA_START_CODE 0x000001b2 | ||
78 : | #define GRPOFVOP_START_CODE 0x000001b3 | ||
79 : | edgomez | 677 | /*#define VIDSESERR_ERROR_CODE 0x000001b4 */ |
80 : | Isibaar | 3 | #define VISOBJ_START_CODE 0x000001b5 |
81 : | edgomez | 677 | /*#define SLICE_START_CODE 0x000001b7 */ |
82 : | /*#define EXT_START_CODE 0x000001b8 */ | ||
83 : | Isibaar | 3 | |
84 : | |||
85 : | #define VISOBJ_TYPE_VIDEO 1 | ||
86 : | edgomez | 677 | /*#define VISOBJ_TYPE_STILLTEXTURE 2 */ |
87 : | /*#define VISOBJ_TYPE_MESH 3 */ | ||
88 : | /*#define VISOBJ_TYPE_FBA 4 */ | ||
89 : | /*#define VISOBJ_TYPE_3DMESH 5 */ | ||
90 : | Isibaar | 3 | |
91 : | |||
92 : | #define VIDOBJLAY_TYPE_SIMPLE 1 | ||
93 : | edgomez | 677 | /*#define VIDOBJLAY_TYPE_SIMPLE_SCALABLE 2 */ |
94 : | Isibaar | 3 | #define VIDOBJLAY_TYPE_CORE 3 |
95 : | #define VIDOBJLAY_TYPE_MAIN 4 | ||
96 : | |||
97 : | |||
98 : | edgomez | 677 | /*#define VIDOBJLAY_AR_SQUARE 1 */ |
99 : | /*#define VIDOBJLAY_AR_625TYPE_43 2 */ | ||
100 : | /*#define VIDOBJLAY_AR_525TYPE_43 3 */ | ||
101 : | /*#define VIDOBJLAY_AR_625TYPE_169 8 */ | ||
102 : | /*#define VIDOBJLAY_AR_525TYPE_169 9 */ | ||
103 : | Isibaar | 3 | #define VIDOBJLAY_AR_EXTPAR 15 |
104 : | |||
105 : | |||
106 : | #define VIDOBJLAY_SHAPE_RECTANGULAR 0 | ||
107 : | #define VIDOBJLAY_SHAPE_BINARY 1 | ||
108 : | #define VIDOBJLAY_SHAPE_BINARY_ONLY 2 | ||
109 : | #define VIDOBJLAY_SHAPE_GRAYSCALE 3 | ||
110 : | |||
111 : | #define VO_START_CODE 0x8 | ||
112 : | #define VOL_START_CODE 0x12 | ||
113 : | #define VOP_START_CODE 0x1b6 | ||
114 : | |||
115 : | #define READ_MARKER() BitstreamSkip(bs, 1) | ||
116 : | #define WRITE_MARKER() BitstreamPutBit(bs, 1) | ||
117 : | |||
118 : | edgomez | 677 | /* vop coding types */ |
119 : | /* intra, prediction, backward, sprite, not_coded */ | ||
120 : | Isibaar | 3 | #define I_VOP 0 |
121 : | #define P_VOP 1 | ||
122 : | #define B_VOP 2 | ||
123 : | #define S_VOP 3 | ||
124 : | #define N_VOP 4 | ||
125 : | |||
126 : | edgomez | 677 | /* resync-specific */ |
127 : | suxen_drol | 248 | #define NUMBITS_VP_RESYNC_MARKER 17 |
128 : | #define RESYNC_MARKER 1 | ||
129 : | Isibaar | 3 | |
130 : | suxen_drol | 248 | |
131 : | edgomez | 466 | /***************************************************************************** |
132 : | * Prototypes | ||
133 : | ****************************************************************************/ | ||
134 : | |||
135 : | suxen_drol | 248 | int |
136 : | suxen_drol | 252 | read_video_packet_header(Bitstream *bs, const int addbits, int * quant); |
137 : | suxen_drol | 248 | |
138 : | |||
139 : | edgomez | 677 | /* header stuff */ |
140 : | edgomez | 195 | int BitstreamReadHeaders(Bitstream * bs, |
141 : | DECODER * dec, | ||
142 : | uint32_t * rounding, | ||
143 : | uint32_t * quant, | ||
144 : | uint32_t * fcode_forward, | ||
145 : | uint32_t * fcode_backward, | ||
146 : | uint32_t * intra_dc_threshold); | ||
147 : | Isibaar | 3 | |
148 : | |||
149 : | void BitstreamWriteVolHeader(Bitstream * const bs, | ||
150 : | edgomez | 195 | const MBParam * pParam, |
151 : | const FRAMEINFO * frame); | ||
152 : | Isibaar | 3 | |
153 : | void BitstreamWriteVopHeader(Bitstream * const bs, | ||
154 : | edgomez | 195 | const MBParam * pParam, |
155 : | suxen_drol | 229 | const FRAMEINFO * frame, |
156 : | int vop_coded); | ||
157 : | Isibaar | 3 | |
158 : | edgomez | 466 | /***************************************************************************** |
159 : | * Inlined functions | ||
160 : | ****************************************************************************/ | ||
161 : | |||
162 : | Isibaar | 3 | /* initialise bitstream structure */ |
163 : | |||
164 : | edgomez | 195 | static void __inline |
165 : | BitstreamInit(Bitstream * const bs, | ||
166 : | void *const bitstream, | ||
167 : | uint32_t length) | ||
168 : | Isibaar | 3 | { |
169 : | uint32_t tmp; | ||
170 : | edgomez | 971 | size_t bitpos; |
171 : | ptr_t adjbitstream = (ptr_t)bitstream; | ||
172 : | Isibaar | 3 | |
173 : | edgomez | 971 | /* |
174 : | * Start the stream on a uint32_t boundary, by rounding down to the | ||
175 : | * previous uint32_t and skipping the intervening bytes. | ||
176 : | */ | ||
177 : | bitpos = ((sizeof(uint32_t)-1) & (size_t)bitstream); | ||
178 : | adjbitstream = adjbitstream - bitpos; | ||
179 : | bs->start = bs->tail = (uint32_t *) adjbitstream; | ||
180 : | Isibaar | 3 | |
181 : | edgomez | 971 | tmp = *bs->start; |
182 : | Isibaar | 3 | #ifndef ARCH_IS_BIG_ENDIAN |
183 : | BSWAP(tmp); | ||
184 : | #endif | ||
185 : | bs->bufa = tmp; | ||
186 : | |||
187 : | edgomez | 971 | tmp = *(bs->start + 1); |
188 : | Isibaar | 3 | #ifndef ARCH_IS_BIG_ENDIAN |
189 : | BSWAP(tmp); | ||
190 : | #endif | ||
191 : | bs->bufb = tmp; | ||
192 : | |||
193 : | bs->buf = 0; | ||
194 : | edgomez | 971 | bs->pos = bs->initpos = bitpos*8; |
195 : | Isibaar | 3 | bs->length = length; |
196 : | } | ||
197 : | |||
198 : | |||
199 : | /* reset bitstream state */ | ||
200 : | |||
201 : | edgomez | 195 | static void __inline |
202 : | BitstreamReset(Bitstream * const bs) | ||
203 : | Isibaar | 3 | { |
204 : | uint32_t tmp; | ||
205 : | |||
206 : | bs->tail = bs->start; | ||
207 : | |||
208 : | tmp = *bs->start; | ||
209 : | #ifndef ARCH_IS_BIG_ENDIAN | ||
210 : | BSWAP(tmp); | ||
211 : | #endif | ||
212 : | bs->bufa = tmp; | ||
213 : | |||
214 : | tmp = *(bs->start + 1); | ||
215 : | #ifndef ARCH_IS_BIG_ENDIAN | ||
216 : | BSWAP(tmp); | ||
217 : | #endif | ||
218 : | bs->bufb = tmp; | ||
219 : | |||
220 : | bs->buf = 0; | ||
221 : | edgomez | 971 | bs->pos = bs->initpos; |
222 : | Isibaar | 3 | } |
223 : | |||
224 : | |||
225 : | /* reads n bits from bitstream without changing the stream pos */ | ||
226 : | |||
227 : | edgomez | 195 | static uint32_t __inline |
228 : | BitstreamShowBits(Bitstream * const bs, | ||
229 : | const uint32_t bits) | ||
230 : | Isibaar | 3 | { |
231 : | int nbit = (bits + bs->pos) - 32; | ||
232 : | edgomez | 195 | |
233 : | if (nbit > 0) { | ||
234 : | return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) | (bs-> | ||
235 : | bufb >> (32 - | ||
236 : | nbit)); | ||
237 : | } else { | ||
238 : | Isibaar | 3 | return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits); |
239 : | } | ||
240 : | } | ||
241 : | |||
242 : | |||
243 : | /* skip n bits forward in bitstream */ | ||
244 : | |||
245 : | edgomez | 466 | static void __inline |
246 : | edgomez | 195 | BitstreamSkip(Bitstream * const bs, |
247 : | const uint32_t bits) | ||
248 : | Isibaar | 3 | { |
249 : | bs->pos += bits; | ||
250 : | |||
251 : | edgomez | 195 | if (bs->pos >= 32) { |
252 : | Isibaar | 3 | uint32_t tmp; |
253 : | |||
254 : | bs->bufa = bs->bufb; | ||
255 : | edgomez | 195 | tmp = *((uint32_t *) bs->tail + 2); |
256 : | Isibaar | 3 | #ifndef ARCH_IS_BIG_ENDIAN |
257 : | BSWAP(tmp); | ||
258 : | #endif | ||
259 : | bs->bufb = tmp; | ||
260 : | bs->tail++; | ||
261 : | bs->pos -= 32; | ||
262 : | } | ||
263 : | } | ||
264 : | |||
265 : | |||
266 : | edgomez | 677 | /* number of bits to next byte alignment */ |
267 : | edgomez | 466 | static uint32_t __inline |
268 : | suxen_drol | 248 | BitstreamNumBitsToByteAlign(Bitstream *bs) |
269 : | { | ||
270 : | uint32_t n = (32 - bs->pos) % 8; | ||
271 : | return n == 0 ? 8 : n; | ||
272 : | } | ||
273 : | |||
274 : | |||
275 : | edgomez | 677 | /* show nbits from next byte alignment */ |
276 : | edgomez | 466 | static uint32_t __inline |
277 : | suxen_drol | 248 | BitstreamShowBitsFromByteAlign(Bitstream *bs, int bits) |
278 : | { | ||
279 : | int bspos = bs->pos + BitstreamNumBitsToByteAlign(bs); | ||
280 : | int nbit = (bits + bspos) - 32; | ||
281 : | |||
282 : | if (bspos >= 32) { | ||
283 : | return bs->bufb >> (32 - nbit); | ||
284 : | } else if (nbit > 0) { | ||
285 : | return ((bs->bufa & (0xffffffff >> bspos)) << nbit) | (bs-> | ||
286 : | bufb >> (32 - | ||
287 : | nbit)); | ||
288 : | } else { | ||
289 : | return (bs->bufa & (0xffffffff >> bspos)) >> (32 - bspos - bits); | ||
290 : | } | ||
291 : | |||
292 : | } | ||
293 : | |||
294 : | |||
295 : | |||
296 : | Isibaar | 3 | /* move forward to the next byte boundary */ |
297 : | |||
298 : | edgomez | 466 | static void __inline |
299 : | edgomez | 195 | BitstreamByteAlign(Bitstream * const bs) |
300 : | Isibaar | 3 | { |
301 : | uint32_t remainder = bs->pos % 8; | ||
302 : | edgomez | 195 | |
303 : | if (remainder) { | ||
304 : | Isibaar | 3 | BitstreamSkip(bs, 8 - remainder); |
305 : | } | ||
306 : | } | ||
307 : | |||
308 : | |||
309 : | /* bitstream length (unit bits) */ | ||
310 : | |||
311 : | edgomez | 195 | static uint32_t __inline |
312 : | BitstreamPos(const Bitstream * const bs) | ||
313 : | Isibaar | 3 | { |
314 : | edgomez | 971 | return((uint32_t)(8*((ptr_t)bs->tail - (ptr_t)bs->start) + bs->pos - bs->initpos)); |
315 : | Isibaar | 3 | } |
316 : | |||
317 : | |||
318 : | /* flush the bitstream & return length (unit bytes) | ||
319 : | NOTE: assumes no futher bitstream functions will be called. | ||
320 : | */ | ||
321 : | |||
322 : | edgomez | 195 | static uint32_t __inline |
323 : | BitstreamLength(Bitstream * const bs) | ||
324 : | Isibaar | 3 | { |
325 : | edgomez | 824 | uint32_t len = (uint32_t)((ptr_t)bs->tail - (ptr_t)bs->start); |
326 : | Isibaar | 3 | |
327 : | edgomez | 195 | if (bs->pos) { |
328 : | Isibaar | 3 | uint32_t b = bs->buf; |
329 : | edgomez | 195 | |
330 : | Isibaar | 3 | #ifndef ARCH_IS_BIG_ENDIAN |
331 : | BSWAP(b); | ||
332 : | #endif | ||
333 : | *bs->tail = b; | ||
334 : | |||
335 : | len += (bs->pos + 7) / 8; | ||
336 : | edgomez | 195 | } |
337 : | Isibaar | 3 | |
338 : | edgomez | 971 | /* initpos is always on a byte boundary */ |
339 : | if (bs->initpos) | ||
340 : | len -= bs->initpos/8; | ||
341 : | |||
342 : | Isibaar | 3 | return len; |
343 : | } | ||
344 : | |||
345 : | |||
346 : | /* move bitstream position forward by n bits and write out buffer if needed */ | ||
347 : | |||
348 : | edgomez | 195 | static void __inline |
349 : | BitstreamForward(Bitstream * const bs, | ||
350 : | const uint32_t bits) | ||
351 : | Isibaar | 3 | { |
352 : | edgomez | 195 | bs->pos += bits; |
353 : | Isibaar | 3 | |
354 : | edgomez | 195 | if (bs->pos >= 32) { |
355 : | Isibaar | 3 | uint32_t b = bs->buf; |
356 : | edgomez | 195 | |
357 : | Isibaar | 3 | #ifndef ARCH_IS_BIG_ENDIAN |
358 : | BSWAP(b); | ||
359 : | #endif | ||
360 : | *bs->tail++ = b; | ||
361 : | bs->buf = 0; | ||
362 : | bs->pos -= 32; | ||
363 : | edgomez | 195 | } |
364 : | Isibaar | 3 | } |
365 : | |||
366 : | /* read n bits from bitstream */ | ||
367 : | |||
368 : | edgomez | 195 | static uint32_t __inline |
369 : | BitstreamGetBits(Bitstream * const bs, | ||
370 : | const uint32_t n) | ||
371 : | Isibaar | 3 | { |
372 : | uint32_t ret = BitstreamShowBits(bs, n); | ||
373 : | edgomez | 195 | |
374 : | Isibaar | 3 | BitstreamSkip(bs, n); |
375 : | return ret; | ||
376 : | } | ||
377 : | |||
378 : | |||
379 : | /* read single bit from bitstream */ | ||
380 : | |||
381 : | edgomez | 195 | static uint32_t __inline |
382 : | BitstreamGetBit(Bitstream * const bs) | ||
383 : | Isibaar | 3 | { |
384 : | return BitstreamGetBits(bs, 1); | ||
385 : | } | ||
386 : | |||
387 : | |||
388 : | /* write single bit to bitstream */ | ||
389 : | |||
390 : | edgomez | 195 | static void __inline |
391 : | BitstreamPutBit(Bitstream * const bs, | ||
392 : | const uint32_t bit) | ||
393 : | Isibaar | 3 | { |
394 : | edgomez | 195 | if (bit) |
395 : | Isibaar | 3 | bs->buf |= (0x80000000 >> bs->pos); |
396 : | |||
397 : | edgomez | 195 | BitstreamForward(bs, 1); |
398 : | Isibaar | 3 | } |
399 : | |||
400 : | |||
401 : | /* write n bits to bitstream */ | ||
402 : | |||
403 : | edgomez | 195 | static void __inline |
404 : | BitstreamPutBits(Bitstream * const bs, | ||
405 : | const uint32_t value, | ||
406 : | const uint32_t size) | ||
407 : | Isibaar | 3 | { |
408 : | uint32_t shift = 32 - bs->pos - size; | ||
409 : | |||
410 : | if (shift <= 32) { | ||
411 : | bs->buf |= value << shift; | ||
412 : | BitstreamForward(bs, size); | ||
413 : | } else { | ||
414 : | uint32_t remainder; | ||
415 : | |||
416 : | shift = size - (32 - bs->pos); | ||
417 : | bs->buf |= value >> shift; | ||
418 : | BitstreamForward(bs, size - shift); | ||
419 : | remainder = shift; | ||
420 : | |||
421 : | shift = 32 - shift; | ||
422 : | edgomez | 195 | |
423 : | Isibaar | 3 | bs->buf |= value << shift; |
424 : | BitstreamForward(bs, remainder); | ||
425 : | } | ||
426 : | } | ||
427 : | |||
428 : | edgomez | 1098 | static const int stuffing_codes[8] = |
429 : | { | ||
430 : | /* nbits stuffing code */ | ||
431 : | 0, /* 1 0 */ | ||
432 : | 1, /* 2 01 */ | ||
433 : | 3, /* 3 011 */ | ||
434 : | 7, /* 4 0111 */ | ||
435 : | 0xf, /* 5 01111 */ | ||
436 : | 0x1f, /* 6 011111 */ | ||
437 : | 0x3f, /* 7 0111111 */ | ||
438 : | 0x7f, /* 8 01111111 */ | ||
439 : | }; | ||
440 : | |||
441 : | /* pad bitstream to the next byte boundary */ | ||
442 : | |||
443 : | static void __inline | ||
444 : | BitstreamPad(Bitstream * const bs) | ||
445 : | { | ||
446 : | int bits = 8 - (bs->pos % 8); | ||
447 : | if (bits < 8) | ||
448 : | BitstreamPutBits(bs, stuffing_codes[bits - 1], bits); | ||
449 : | } | ||
450 : | |||
451 : | /* | ||
452 : | * pad bitstream to the next byte boundary | ||
453 : | * alway pad: even if currently at the byte boundary | ||
454 : | */ | ||
455 : | |||
456 : | static void __inline | ||
457 : | BitstreamPadAlways(Bitstream * const bs) | ||
458 : | { | ||
459 : | int bits = 8 - (bs->pos % 8); | ||
460 : | BitstreamPutBits(bs, stuffing_codes[bits - 1], bits); | ||
461 : | } | ||
462 : | |||
463 : | edgomez | 466 | #endif /* _BITSTREAM_H_ */ |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |