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