Parent Directory | Revision Log
Revision 229 - (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 : | h | 69 | * 26.03.2002 interlacing support - modified putvol/vopheaders paramters |
45 : | Isibaar | 3 | * 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 | 195 | //#define VIDSESERR_ERROR_CODE 0x000001b4 |
67 : | Isibaar | 3 | #define VISOBJ_START_CODE 0x000001b5 |
68 : | edgomez | 195 | //#define SLICE_START_CODE 0x000001b7 |
69 : | //#define EXT_START_CODE 0x000001b8 | ||
70 : | Isibaar | 3 | |
71 : | |||
72 : | #define VISOBJ_TYPE_VIDEO 1 | ||
73 : | edgomez | 195 | //#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 | 3 | |
78 : | |||
79 : | #define VIDOBJLAY_TYPE_SIMPLE 1 | ||
80 : | edgomez | 195 | //#define VIDOBJLAY_TYPE_SIMPLE_SCALABLE 2 |
81 : | Isibaar | 3 | #define VIDOBJLAY_TYPE_CORE 3 |
82 : | #define VIDOBJLAY_TYPE_MAIN 4 | ||
83 : | |||
84 : | |||
85 : | edgomez | 195 | //#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 | 3 | #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 | 195 | 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 | 3 | |
123 : | |||
124 : | void BitstreamWriteVolHeader(Bitstream * const bs, | ||
125 : | edgomez | 195 | const MBParam * pParam, |
126 : | const FRAMEINFO * frame); | ||
127 : | Isibaar | 3 | |
128 : | void BitstreamWriteVopHeader(Bitstream * const bs, | ||
129 : | edgomez | 195 | const MBParam * pParam, |
130 : | suxen_drol | 229 | const FRAMEINFO * frame, |
131 : | int vop_coded); | ||
132 : | Isibaar | 3 | |
133 : | suxen_drol | 229 | void BitstreamWriteUserData(Bitstream * const bs, |
134 : | uint8_t * data, | ||
135 : | const int length); | ||
136 : | |||
137 : | Isibaar | 3 | /* initialise bitstream structure */ |
138 : | |||
139 : | edgomez | 195 | static void __inline |
140 : | BitstreamInit(Bitstream * const bs, | ||
141 : | void *const bitstream, | ||
142 : | uint32_t length) | ||
143 : | Isibaar | 3 | { |
144 : | uint32_t tmp; | ||
145 : | |||
146 : | edgomez | 195 | bs->start = bs->tail = (uint32_t *) bitstream; |
147 : | Isibaar | 3 | |
148 : | edgomez | 195 | tmp = *(uint32_t *) bitstream; |
149 : | Isibaar | 3 | #ifndef ARCH_IS_BIG_ENDIAN |
150 : | BSWAP(tmp); | ||
151 : | #endif | ||
152 : | bs->bufa = tmp; | ||
153 : | |||
154 : | edgomez | 195 | tmp = *((uint32_t *) bitstream + 1); |
155 : | Isibaar | 3 | #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 | 195 | static void __inline |
169 : | BitstreamReset(Bitstream * const bs) | ||
170 : | Isibaar | 3 | { |
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 | 195 | bs->pos = 0; |
189 : | Isibaar | 3 | } |
190 : | |||
191 : | |||
192 : | /* reads n bits from bitstream without changing the stream pos */ | ||
193 : | |||
194 : | edgomez | 195 | static uint32_t __inline |
195 : | BitstreamShowBits(Bitstream * const bs, | ||
196 : | const uint32_t bits) | ||
197 : | Isibaar | 3 | { |
198 : | int nbit = (bits + bs->pos) - 32; | ||
199 : | edgomez | 195 | |
200 : | if (nbit > 0) { | ||
201 : | return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) | (bs-> | ||
202 : | bufb >> (32 - | ||
203 : | nbit)); | ||
204 : | } else { | ||
205 : | Isibaar | 3 | return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits); |
206 : | } | ||
207 : | } | ||
208 : | |||
209 : | |||
210 : | /* skip n bits forward in bitstream */ | ||
211 : | |||
212 : | edgomez | 195 | static __inline void |
213 : | BitstreamSkip(Bitstream * const bs, | ||
214 : | const uint32_t bits) | ||
215 : | Isibaar | 3 | { |
216 : | bs->pos += bits; | ||
217 : | |||
218 : | edgomez | 195 | if (bs->pos >= 32) { |
219 : | Isibaar | 3 | uint32_t tmp; |
220 : | |||
221 : | bs->bufa = bs->bufb; | ||
222 : | edgomez | 195 | tmp = *((uint32_t *) bs->tail + 2); |
223 : | Isibaar | 3 | #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 | 195 | static __inline void |
236 : | BitstreamByteAlign(Bitstream * const bs) | ||
237 : | Isibaar | 3 | { |
238 : | uint32_t remainder = bs->pos % 8; | ||
239 : | edgomez | 195 | |
240 : | if (remainder) { | ||
241 : | Isibaar | 3 | BitstreamSkip(bs, 8 - remainder); |
242 : | } | ||
243 : | } | ||
244 : | |||
245 : | |||
246 : | /* bitstream length (unit bits) */ | ||
247 : | |||
248 : | edgomez | 195 | static uint32_t __inline |
249 : | BitstreamPos(const Bitstream * const bs) | ||
250 : | Isibaar | 3 | { |
251 : | Isibaar | 209 | return 8 * ((ptr_t)bs->tail - (ptr_t)bs->start) + bs->pos; |
252 : | Isibaar | 3 | } |
253 : | |||
254 : | |||
255 : | /* flush the bitstream & return length (unit bytes) | ||
256 : | NOTE: assumes no futher bitstream functions will be called. | ||
257 : | */ | ||
258 : | |||
259 : | edgomez | 195 | static uint32_t __inline |
260 : | BitstreamLength(Bitstream * const bs) | ||
261 : | Isibaar | 3 | { |
262 : | Isibaar | 209 | uint32_t len = (ptr_t) bs->tail - (ptr_t) bs->start; |
263 : | Isibaar | 3 | |
264 : | edgomez | 195 | if (bs->pos) { |
265 : | Isibaar | 3 | uint32_t b = bs->buf; |
266 : | edgomez | 195 | |
267 : | Isibaar | 3 | #ifndef ARCH_IS_BIG_ENDIAN |
268 : | BSWAP(b); | ||
269 : | #endif | ||
270 : | *bs->tail = b; | ||
271 : | |||
272 : | len += (bs->pos + 7) / 8; | ||
273 : | edgomez | 195 | } |
274 : | Isibaar | 3 | |
275 : | return len; | ||
276 : | } | ||
277 : | |||
278 : | |||
279 : | /* move bitstream position forward by n bits and write out buffer if needed */ | ||
280 : | |||
281 : | edgomez | 195 | static void __inline |
282 : | BitstreamForward(Bitstream * const bs, | ||
283 : | const uint32_t bits) | ||
284 : | Isibaar | 3 | { |
285 : | edgomez | 195 | bs->pos += bits; |
286 : | Isibaar | 3 | |
287 : | edgomez | 195 | if (bs->pos >= 32) { |
288 : | Isibaar | 3 | uint32_t b = bs->buf; |
289 : | edgomez | 195 | |
290 : | Isibaar | 3 | #ifndef ARCH_IS_BIG_ENDIAN |
291 : | BSWAP(b); | ||
292 : | #endif | ||
293 : | *bs->tail++ = b; | ||
294 : | bs->buf = 0; | ||
295 : | bs->pos -= 32; | ||
296 : | edgomez | 195 | } |
297 : | Isibaar | 3 | } |
298 : | |||
299 : | |||
300 : | /* pad bitstream to the next byte boundary */ | ||
301 : | |||
302 : | edgomez | 195 | static void __inline |
303 : | BitstreamPad(Bitstream * const bs) | ||
304 : | Isibaar | 3 | { |
305 : | uint32_t remainder = bs->pos % 8; | ||
306 : | |||
307 : | edgomez | 195 | if (remainder) { |
308 : | Isibaar | 3 | BitstreamForward(bs, 8 - remainder); |
309 : | edgomez | 195 | } |
310 : | Isibaar | 3 | } |
311 : | |||
312 : | |||
313 : | /* read n bits from bitstream */ | ||
314 : | |||
315 : | edgomez | 195 | static uint32_t __inline |
316 : | BitstreamGetBits(Bitstream * const bs, | ||
317 : | const uint32_t n) | ||
318 : | Isibaar | 3 | { |
319 : | uint32_t ret = BitstreamShowBits(bs, n); | ||
320 : | edgomez | 195 | |
321 : | Isibaar | 3 | BitstreamSkip(bs, n); |
322 : | return ret; | ||
323 : | } | ||
324 : | |||
325 : | |||
326 : | /* read single bit from bitstream */ | ||
327 : | |||
328 : | edgomez | 195 | static uint32_t __inline |
329 : | BitstreamGetBit(Bitstream * const bs) | ||
330 : | Isibaar | 3 | { |
331 : | return BitstreamGetBits(bs, 1); | ||
332 : | } | ||
333 : | |||
334 : | |||
335 : | /* write single bit to bitstream */ | ||
336 : | |||
337 : | edgomez | 195 | static void __inline |
338 : | BitstreamPutBit(Bitstream * const bs, | ||
339 : | const uint32_t bit) | ||
340 : | Isibaar | 3 | { |
341 : | edgomez | 195 | if (bit) |
342 : | Isibaar | 3 | bs->buf |= (0x80000000 >> bs->pos); |
343 : | |||
344 : | edgomez | 195 | BitstreamForward(bs, 1); |
345 : | Isibaar | 3 | } |
346 : | |||
347 : | |||
348 : | /* write n bits to bitstream */ | ||
349 : | |||
350 : | edgomez | 195 | static void __inline |
351 : | BitstreamPutBits(Bitstream * const bs, | ||
352 : | const uint32_t value, | ||
353 : | const uint32_t size) | ||
354 : | Isibaar | 3 | { |
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 | 195 | |
370 : | Isibaar | 3 | bs->buf |= value << shift; |
371 : | BitstreamForward(bs, remainder); | ||
372 : | } | ||
373 : | } | ||
374 : | |||
375 : | edgomez | 195 | #endif /* _BITSTREAM_H_ */ |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |