Parent Directory
|
Revision Log
Revision 1.6 - (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 : | const FRAMEINFO * frame); | ||
131 : | Isibaar | 1.1 | |
132 : | /* initialise bitstream structure */ | ||
133 : | |||
134 : | edgomez | 1.6 | static void __inline |
135 : | BitstreamInit(Bitstream * const bs, | ||
136 : | void *const bitstream, | ||
137 : | uint32_t length) | ||
138 : | Isibaar | 1.1 | { |
139 : | uint32_t tmp; | ||
140 : | |||
141 : | edgomez | 1.6 | bs->start = bs->tail = (uint32_t *) bitstream; |
142 : | Isibaar | 1.1 | |
143 : | edgomez | 1.6 | tmp = *(uint32_t *) bitstream; |
144 : | Isibaar | 1.1 | #ifndef ARCH_IS_BIG_ENDIAN |
145 : | BSWAP(tmp); | ||
146 : | #endif | ||
147 : | bs->bufa = tmp; | ||
148 : | |||
149 : | edgomez | 1.6 | tmp = *((uint32_t *) bitstream + 1); |
150 : | Isibaar | 1.1 | #ifndef ARCH_IS_BIG_ENDIAN |
151 : | BSWAP(tmp); | ||
152 : | #endif | ||
153 : | bs->bufb = tmp; | ||
154 : | |||
155 : | bs->buf = 0; | ||
156 : | bs->pos = 0; | ||
157 : | bs->length = length; | ||
158 : | } | ||
159 : | |||
160 : | |||
161 : | /* reset bitstream state */ | ||
162 : | |||
163 : | edgomez | 1.6 | static void __inline |
164 : | BitstreamReset(Bitstream * const bs) | ||
165 : | Isibaar | 1.1 | { |
166 : | uint32_t tmp; | ||
167 : | |||
168 : | bs->tail = bs->start; | ||
169 : | |||
170 : | tmp = *bs->start; | ||
171 : | #ifndef ARCH_IS_BIG_ENDIAN | ||
172 : | BSWAP(tmp); | ||
173 : | #endif | ||
174 : | bs->bufa = tmp; | ||
175 : | |||
176 : | tmp = *(bs->start + 1); | ||
177 : | #ifndef ARCH_IS_BIG_ENDIAN | ||
178 : | BSWAP(tmp); | ||
179 : | #endif | ||
180 : | bs->bufb = tmp; | ||
181 : | |||
182 : | bs->buf = 0; | ||
183 : | edgomez | 1.6 | bs->pos = 0; |
184 : | Isibaar | 1.1 | } |
185 : | |||
186 : | |||
187 : | /* reads n bits from bitstream without changing the stream pos */ | ||
188 : | |||
189 : | edgomez | 1.6 | static uint32_t __inline |
190 : | BitstreamShowBits(Bitstream * const bs, | ||
191 : | const uint32_t bits) | ||
192 : | Isibaar | 1.1 | { |
193 : | int nbit = (bits + bs->pos) - 32; | ||
194 : | edgomez | 1.6 | |
195 : | if (nbit > 0) { | ||
196 : | return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) | (bs-> | ||
197 : | bufb >> (32 - | ||
198 : | nbit)); | ||
199 : | } else { | ||
200 : | Isibaar | 1.1 | return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits); |
201 : | } | ||
202 : | } | ||
203 : | |||
204 : | |||
205 : | /* skip n bits forward in bitstream */ | ||
206 : | |||
207 : | edgomez | 1.6 | static __inline void |
208 : | BitstreamSkip(Bitstream * const bs, | ||
209 : | const uint32_t bits) | ||
210 : | Isibaar | 1.1 | { |
211 : | bs->pos += bits; | ||
212 : | |||
213 : | edgomez | 1.6 | if (bs->pos >= 32) { |
214 : | Isibaar | 1.1 | uint32_t tmp; |
215 : | |||
216 : | bs->bufa = bs->bufb; | ||
217 : | edgomez | 1.6 | tmp = *((uint32_t *) bs->tail + 2); |
218 : | Isibaar | 1.1 | #ifndef ARCH_IS_BIG_ENDIAN |
219 : | BSWAP(tmp); | ||
220 : | #endif | ||
221 : | bs->bufb = tmp; | ||
222 : | bs->tail++; | ||
223 : | bs->pos -= 32; | ||
224 : | } | ||
225 : | } | ||
226 : | |||
227 : | |||
228 : | /* move forward to the next byte boundary */ | ||
229 : | |||
230 : | edgomez | 1.6 | static __inline void |
231 : | BitstreamByteAlign(Bitstream * const bs) | ||
232 : | Isibaar | 1.1 | { |
233 : | uint32_t remainder = bs->pos % 8; | ||
234 : | edgomez | 1.6 | |
235 : | if (remainder) { | ||
236 : | Isibaar | 1.1 | BitstreamSkip(bs, 8 - remainder); |
237 : | } | ||
238 : | } | ||
239 : | |||
240 : | |||
241 : | /* bitstream length (unit bits) */ | ||
242 : | |||
243 : | edgomez | 1.6 | static uint32_t __inline |
244 : | BitstreamPos(const Bitstream * const bs) | ||
245 : | Isibaar | 1.1 | { |
246 : | edgomez | 1.6 | return 8 * ((uint32_t) bs->tail - (uint32_t) bs->start) + bs->pos; |
247 : | Isibaar | 1.1 | } |
248 : | |||
249 : | |||
250 : | /* flush the bitstream & return length (unit bytes) | ||
251 : | NOTE: assumes no futher bitstream functions will be called. | ||
252 : | */ | ||
253 : | |||
254 : | edgomez | 1.6 | static uint32_t __inline |
255 : | BitstreamLength(Bitstream * const bs) | ||
256 : | Isibaar | 1.1 | { |
257 : | uint32_t len = (uint32_t) bs->tail - (uint32_t) bs->start; | ||
258 : | |||
259 : | edgomez | 1.6 | if (bs->pos) { |
260 : | Isibaar | 1.1 | uint32_t b = bs->buf; |
261 : | edgomez | 1.6 | |
262 : | Isibaar | 1.1 | #ifndef ARCH_IS_BIG_ENDIAN |
263 : | BSWAP(b); | ||
264 : | #endif | ||
265 : | *bs->tail = b; | ||
266 : | |||
267 : | len += (bs->pos + 7) / 8; | ||
268 : | edgomez | 1.6 | } |
269 : | Isibaar | 1.1 | |
270 : | return len; | ||
271 : | } | ||
272 : | |||
273 : | |||
274 : | /* move bitstream position forward by n bits and write out buffer if needed */ | ||
275 : | |||
276 : | edgomez | 1.6 | static void __inline |
277 : | BitstreamForward(Bitstream * const bs, | ||
278 : | const uint32_t bits) | ||
279 : | Isibaar | 1.1 | { |
280 : | edgomez | 1.6 | bs->pos += bits; |
281 : | Isibaar | 1.1 | |
282 : | edgomez | 1.6 | if (bs->pos >= 32) { |
283 : | Isibaar | 1.1 | uint32_t b = bs->buf; |
284 : | edgomez | 1.6 | |
285 : | Isibaar | 1.1 | #ifndef ARCH_IS_BIG_ENDIAN |
286 : | BSWAP(b); | ||
287 : | #endif | ||
288 : | *bs->tail++ = b; | ||
289 : | bs->buf = 0; | ||
290 : | bs->pos -= 32; | ||
291 : | edgomez | 1.6 | } |
292 : | Isibaar | 1.1 | } |
293 : | |||
294 : | |||
295 : | /* pad bitstream to the next byte boundary */ | ||
296 : | |||
297 : | edgomez | 1.6 | static void __inline |
298 : | BitstreamPad(Bitstream * const bs) | ||
299 : | Isibaar | 1.1 | { |
300 : | uint32_t remainder = bs->pos % 8; | ||
301 : | |||
302 : | edgomez | 1.6 | if (remainder) { |
303 : | Isibaar | 1.1 | BitstreamForward(bs, 8 - remainder); |
304 : | edgomez | 1.6 | } |
305 : | Isibaar | 1.1 | } |
306 : | |||
307 : | |||
308 : | /* read n bits from bitstream */ | ||
309 : | |||
310 : | edgomez | 1.6 | static uint32_t __inline |
311 : | BitstreamGetBits(Bitstream * const bs, | ||
312 : | const uint32_t n) | ||
313 : | Isibaar | 1.1 | { |
314 : | uint32_t ret = BitstreamShowBits(bs, n); | ||
315 : | edgomez | 1.6 | |
316 : | Isibaar | 1.1 | BitstreamSkip(bs, n); |
317 : | return ret; | ||
318 : | } | ||
319 : | |||
320 : | |||
321 : | /* read single bit from bitstream */ | ||
322 : | |||
323 : | edgomez | 1.6 | static uint32_t __inline |
324 : | BitstreamGetBit(Bitstream * const bs) | ||
325 : | Isibaar | 1.1 | { |
326 : | return BitstreamGetBits(bs, 1); | ||
327 : | } | ||
328 : | |||
329 : | |||
330 : | /* write single bit to bitstream */ | ||
331 : | |||
332 : | edgomez | 1.6 | static void __inline |
333 : | BitstreamPutBit(Bitstream * const bs, | ||
334 : | const uint32_t bit) | ||
335 : | Isibaar | 1.1 | { |
336 : | edgomez | 1.6 | if (bit) |
337 : | Isibaar | 1.1 | bs->buf |= (0x80000000 >> bs->pos); |
338 : | |||
339 : | edgomez | 1.6 | BitstreamForward(bs, 1); |
340 : | Isibaar | 1.1 | } |
341 : | |||
342 : | |||
343 : | /* write n bits to bitstream */ | ||
344 : | |||
345 : | edgomez | 1.6 | static void __inline |
346 : | BitstreamPutBits(Bitstream * const bs, | ||
347 : | const uint32_t value, | ||
348 : | const uint32_t size) | ||
349 : | Isibaar | 1.1 | { |
350 : | uint32_t shift = 32 - bs->pos - size; | ||
351 : | |||
352 : | if (shift <= 32) { | ||
353 : | bs->buf |= value << shift; | ||
354 : | BitstreamForward(bs, size); | ||
355 : | } else { | ||
356 : | uint32_t remainder; | ||
357 : | |||
358 : | shift = size - (32 - bs->pos); | ||
359 : | bs->buf |= value >> shift; | ||
360 : | BitstreamForward(bs, size - shift); | ||
361 : | remainder = shift; | ||
362 : | |||
363 : | shift = 32 - shift; | ||
364 : | edgomez | 1.6 | |
365 : | Isibaar | 1.1 | bs->buf |= value << shift; |
366 : | BitstreamForward(bs, remainder); | ||
367 : | } | ||
368 : | } | ||
369 : | |||
370 : | edgomez | 1.6 | #endif /* _BITSTREAM_H_ */ |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |