Mercurial > libavcodec.hg
annotate common.c @ 24:08265a63313e libavcodec
added MJPEG decoder + comments
author | glantau |
---|---|
date | Mon, 06 Aug 2001 00:47:03 +0000 |
parents | 907b67420d84 |
children | 5aa6292a1660 |
rev | line source |
---|---|
0 | 1 /* |
2 * Common bit i/o utils | |
3 * Copyright (c) 2000, 2001 Gerard Lantau. | |
4 * | |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
7 * the Free Software Foundation; either version 2 of the License, or | |
8 * (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 * GNU General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 */ | |
19 #include <stdlib.h> | |
20 #include <stdio.h> | |
21 #include <string.h> | |
22 #ifdef __FreeBSD__ | |
23 #include <sys/param.h> | |
24 #endif | |
25 #include <netinet/in.h> | |
26 #include <math.h> | |
27 #include "common.h" | |
28 | |
29 #define NDEBUG | |
30 #include <assert.h> | |
31 | |
8 | 32 #include "../bswap.h" |
33 | |
0 | 34 void init_put_bits(PutBitContext *s, |
35 UINT8 *buffer, int buffer_size, | |
36 void *opaque, | |
37 void (*write_data)(void *, UINT8 *, int)) | |
38 { | |
39 s->buf = buffer; | |
40 s->buf_ptr = s->buf; | |
41 s->buf_end = s->buf + buffer_size; | |
42 s->bit_cnt=0; | |
43 s->bit_buf=0; | |
44 s->data_out_size = 0; | |
45 s->write_data = write_data; | |
46 s->opaque = opaque; | |
47 } | |
48 | |
49 static void flush_buffer(PutBitContext *s) | |
50 { | |
51 int size; | |
52 if (s->write_data) { | |
53 size = s->buf_ptr - s->buf; | |
54 if (size > 0) | |
55 s->write_data(s->opaque, s->buf, size); | |
56 s->buf_ptr = s->buf; | |
57 s->data_out_size += size; | |
58 } | |
59 } | |
60 | |
61 void put_bits(PutBitContext *s, int n, unsigned int value) | |
62 { | |
63 unsigned int bit_buf; | |
64 int bit_cnt; | |
65 | |
66 #ifdef STATS | |
67 st_out_bit_counts[st_current_index] += n; | |
68 #endif | |
69 // printf("put_bits=%d %x\n", n, value); | |
70 assert(n == 32 || value < (1U << n)); | |
71 | |
72 bit_buf = s->bit_buf; | |
73 bit_cnt = s->bit_cnt; | |
74 | |
75 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf); | |
76 /* XXX: optimize */ | |
77 if (n < (32-bit_cnt)) { | |
78 bit_buf |= value << (32 - n - bit_cnt); | |
79 bit_cnt+=n; | |
80 } else { | |
81 bit_buf |= value >> (n + bit_cnt - 32); | |
82 *(UINT32 *)s->buf_ptr = htonl(bit_buf); | |
83 //printf("bitbuf = %08x\n", bit_buf); | |
84 s->buf_ptr+=4; | |
85 if (s->buf_ptr >= s->buf_end) | |
86 flush_buffer(s); | |
87 bit_cnt=bit_cnt + n - 32; | |
88 if (bit_cnt == 0) { | |
89 bit_buf = 0; | |
90 } else { | |
91 bit_buf = value << (32 - bit_cnt); | |
92 } | |
93 } | |
94 | |
95 s->bit_buf = bit_buf; | |
96 s->bit_cnt = bit_cnt; | |
97 } | |
98 | |
99 /* return the number of bits output */ | |
100 long long get_bit_count(PutBitContext *s) | |
101 { | |
102 return (s->buf_ptr - s->buf + s->data_out_size) * 8 + (long long)s->bit_cnt; | |
103 } | |
104 | |
105 void align_put_bits(PutBitContext *s) | |
106 { | |
107 put_bits(s,(8 - s->bit_cnt) & 7,0); | |
108 } | |
109 | |
110 /* pad the end of the output stream with zeros */ | |
111 void flush_put_bits(PutBitContext *s) | |
112 { | |
113 while (s->bit_cnt > 0) { | |
114 /* XXX: should test end of buffer */ | |
115 *s->buf_ptr++=s->bit_buf >> 24; | |
116 s->bit_buf<<=8; | |
117 s->bit_cnt-=8; | |
118 } | |
119 flush_buffer(s); | |
120 s->bit_cnt=0; | |
121 s->bit_buf=0; | |
122 } | |
123 | |
24 | 124 /* for jpeg : escape 0xff with 0x00 after it */ |
0 | 125 void jput_bits(PutBitContext *s, int n, unsigned int value) |
126 { | |
127 unsigned int bit_buf, b; | |
128 int bit_cnt, i; | |
129 | |
130 assert(n == 32 || value < (1U << n)); | |
131 | |
132 bit_buf = s->bit_buf; | |
133 bit_cnt = s->bit_cnt; | |
134 | |
135 //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf); | |
136 /* XXX: optimize */ | |
137 if (n < (32-bit_cnt)) { | |
138 bit_buf |= value << (32 - n - bit_cnt); | |
139 bit_cnt+=n; | |
140 } else { | |
141 bit_buf |= value >> (n + bit_cnt - 32); | |
142 /* handle escape */ | |
143 for(i=0;i<4;i++) { | |
144 b = (bit_buf >> 24); | |
145 *(s->buf_ptr++) = b; | |
146 if (b == 0xff) | |
147 *(s->buf_ptr++) = 0; | |
148 bit_buf <<= 8; | |
149 } | |
150 /* we flush the buffer sooner to handle worst case */ | |
151 if (s->buf_ptr >= (s->buf_end - 8)) | |
152 flush_buffer(s); | |
153 | |
154 bit_cnt=bit_cnt + n - 32; | |
155 if (bit_cnt == 0) { | |
156 bit_buf = 0; | |
157 } else { | |
158 bit_buf = value << (32 - bit_cnt); | |
159 } | |
160 } | |
161 | |
162 s->bit_buf = bit_buf; | |
163 s->bit_cnt = bit_cnt; | |
164 } | |
165 | |
166 /* pad the end of the output stream with zeros */ | |
167 void jflush_put_bits(PutBitContext *s) | |
168 { | |
169 unsigned int b; | |
170 | |
171 while (s->bit_cnt > 0) { | |
172 b = s->bit_buf >> 24; | |
173 *s->buf_ptr++ = b; | |
174 if (b == 0xff) | |
175 *s->buf_ptr++ = 0; | |
176 s->bit_buf<<=8; | |
177 s->bit_cnt-=8; | |
178 } | |
179 flush_buffer(s); | |
180 s->bit_cnt=0; | |
181 s->bit_buf=0; | |
182 } | |
183 | |
184 /* bit input functions */ | |
185 | |
186 void init_get_bits(GetBitContext *s, | |
187 UINT8 *buffer, int buffer_size) | |
188 { | |
189 s->buf = buffer; | |
190 s->buf_ptr = buffer; | |
191 s->buf_end = buffer + buffer_size; | |
192 s->bit_cnt = 0; | |
193 s->bit_buf = 0; | |
194 while (s->buf_ptr < s->buf_end && | |
195 s->bit_cnt < 32) { | |
196 s->bit_buf |= (*s->buf_ptr++ << (24 - s->bit_cnt)); | |
197 s->bit_cnt += 8; | |
198 } | |
199 } | |
200 | |
201 /* n must be >= 1 and <= 32 */ | |
20
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
202 /* also true: n > s->bit_cnt */ |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
203 unsigned int get_bits_long(GetBitContext *s, int n) |
0 | 204 { |
205 unsigned int val; | |
206 int bit_cnt; | |
207 unsigned int bit_buf; | |
208 | |
209 #ifdef STATS | |
210 st_bit_counts[st_current_index] += n; | |
211 #endif | |
212 | |
213 bit_buf = s->bit_buf; | |
20
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
214 bit_cnt = s->bit_cnt - n; |
0 | 215 |
20
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
216 // if (bit_cnt >= 0) { |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
217 // val = bit_buf >> (32 - n); |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
218 // bit_buf <<= n; |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
219 // } else |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
220 { |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
221 UINT8 *buf_ptr; |
0 | 222 val = bit_buf >> (32 - n); |
223 buf_ptr = s->buf_ptr; | |
224 buf_ptr += 4; | |
225 /* handle common case: we can read everything */ | |
226 if (buf_ptr <= s->buf_end) { | |
8 | 227 #if ARCH_X86 |
228 bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4]))); | |
229 #else | |
230 bit_buf = (buf_ptr[-4] << 24) | | |
231 (buf_ptr[-3] << 16) | | |
0 | 232 (buf_ptr[-2] << 8) | |
8 | 233 (buf_ptr[-1]); |
234 #endif | |
0 | 235 } else { |
236 buf_ptr -= 4; | |
237 bit_buf = 0; | |
238 if (buf_ptr < s->buf_end) | |
239 bit_buf |= *buf_ptr++ << 24; | |
240 if (buf_ptr < s->buf_end) | |
241 bit_buf |= *buf_ptr++ << 16; | |
242 if (buf_ptr < s->buf_end) | |
243 bit_buf |= *buf_ptr++ << 8; | |
244 if (buf_ptr < s->buf_end) | |
245 bit_buf |= *buf_ptr++; | |
246 } | |
247 s->buf_ptr = buf_ptr; | |
248 val |= bit_buf >> (32 + bit_cnt); | |
249 bit_buf <<= - bit_cnt; | |
250 bit_cnt += 32; | |
251 } | |
252 s->bit_buf = bit_buf; | |
253 s->bit_cnt = bit_cnt; | |
254 return val; | |
255 } | |
256 | |
257 void align_get_bits(GetBitContext *s) | |
258 { | |
259 int n; | |
260 n = s->bit_cnt & 7; | |
261 if (n > 0) { | |
262 get_bits(s, n); | |
263 } | |
264 } | |
265 | |
266 /* VLC decoding */ | |
267 | |
268 //#define DEBUG_VLC | |
269 | |
270 #define GET_DATA(v, table, i, wrap, size) \ | |
271 {\ | |
272 UINT8 *ptr = (UINT8 *)table + i * wrap;\ | |
273 switch(size) {\ | |
274 case 1:\ | |
275 v = *(UINT8 *)ptr;\ | |
276 break;\ | |
277 case 2:\ | |
278 v = *(UINT16 *)ptr;\ | |
279 break;\ | |
280 default:\ | |
281 v = *(UINT32 *)ptr;\ | |
282 break;\ | |
283 }\ | |
284 } | |
285 | |
286 | |
287 static int alloc_table(VLC *vlc, int size) | |
288 { | |
289 int index; | |
290 index = vlc->table_size; | |
291 vlc->table_size += size; | |
292 if (vlc->table_size > vlc->table_allocated) { | |
293 vlc->table_allocated += (1 << vlc->bits); | |
294 vlc->table_bits = realloc(vlc->table_bits, | |
295 sizeof(INT8) * vlc->table_allocated); | |
296 vlc->table_codes = realloc(vlc->table_codes, | |
297 sizeof(INT16) * vlc->table_allocated); | |
298 if (!vlc->table_bits || | |
299 !vlc->table_codes) | |
300 return -1; | |
301 } | |
302 return index; | |
303 } | |
304 | |
305 static int build_table(VLC *vlc, int table_nb_bits, | |
306 int nb_codes, | |
307 const void *bits, int bits_wrap, int bits_size, | |
308 const void *codes, int codes_wrap, int codes_size, | |
309 UINT32 code_prefix, int n_prefix) | |
310 { | |
311 int i, j, k, n, table_size, table_index, nb, n1, index; | |
312 UINT32 code; | |
313 INT8 *table_bits; | |
314 INT16 *table_codes; | |
315 | |
316 table_size = 1 << table_nb_bits; | |
317 table_index = alloc_table(vlc, table_size); | |
318 #ifdef DEBUG_VLC | |
319 printf("new table index=%d size=%d code_prefix=%x n=%d\n", | |
320 table_index, table_size, code_prefix, n_prefix); | |
321 #endif | |
322 if (table_index < 0) | |
323 return -1; | |
324 table_bits = &vlc->table_bits[table_index]; | |
325 table_codes = &vlc->table_codes[table_index]; | |
326 | |
327 for(i=0;i<table_size;i++) { | |
328 table_bits[i] = 0; | |
329 table_codes[i] = -1; | |
330 } | |
331 | |
332 /* first pass: map codes and compute auxillary table sizes */ | |
333 for(i=0;i<nb_codes;i++) { | |
334 GET_DATA(n, bits, i, bits_wrap, bits_size); | |
335 GET_DATA(code, codes, i, codes_wrap, codes_size); | |
336 /* we accept tables with holes */ | |
337 if (n <= 0) | |
338 continue; | |
339 #if defined(DEBUG_VLC) && 0 | |
340 printf("i=%d n=%d code=0x%x\n", i, n, code); | |
341 #endif | |
342 /* if code matches the prefix, it is in the table */ | |
343 n -= n_prefix; | |
344 if (n > 0 && (code >> n) == code_prefix) { | |
345 if (n <= table_nb_bits) { | |
346 /* no need to add another table */ | |
347 j = (code << (table_nb_bits - n)) & (table_size - 1); | |
348 nb = 1 << (table_nb_bits - n); | |
349 for(k=0;k<nb;k++) { | |
350 #ifdef DEBUG_VLC | |
351 printf("%4x: code=%d n=%d\n", | |
352 j, i, n); | |
353 #endif | |
354 if (table_bits[j] != 0) { | |
355 fprintf(stderr, "incorrect codes\n"); | |
356 exit(1); | |
357 } | |
358 table_bits[j] = n; | |
359 table_codes[j] = i; | |
360 j++; | |
361 } | |
362 } else { | |
363 n -= table_nb_bits; | |
364 j = (code >> n) & ((1 << table_nb_bits) - 1); | |
365 #ifdef DEBUG_VLC | |
366 printf("%4x: n=%d (subtable)\n", | |
367 j, n); | |
368 #endif | |
369 /* compute table size */ | |
370 n1 = -table_bits[j]; | |
371 if (n > n1) | |
372 n1 = n; | |
373 table_bits[j] = -n1; | |
374 } | |
375 } | |
376 } | |
377 | |
378 /* second pass : fill auxillary tables recursively */ | |
379 for(i=0;i<table_size;i++) { | |
380 n = table_bits[i]; | |
381 if (n < 0) { | |
382 n = -n; | |
383 if (n > table_nb_bits) { | |
384 n = table_nb_bits; | |
385 table_bits[i] = -n; | |
386 } | |
387 index = build_table(vlc, n, nb_codes, | |
388 bits, bits_wrap, bits_size, | |
389 codes, codes_wrap, codes_size, | |
390 (code_prefix << table_nb_bits) | i, | |
391 n_prefix + table_nb_bits); | |
392 if (index < 0) | |
393 return -1; | |
394 /* note: realloc has been done, so reload tables */ | |
395 table_bits = &vlc->table_bits[table_index]; | |
396 table_codes = &vlc->table_codes[table_index]; | |
397 table_codes[i] = index; | |
398 } | |
399 } | |
400 return table_index; | |
401 } | |
402 | |
403 | |
24 | 404 /* Build VLC decoding tables suitable for use with get_vlc(). |
405 | |
406 'nb_bits' set thee decoding table size (2^nb_bits) entries. The | |
407 bigger it is, the faster is the decoding. But it should not be too | |
408 big to save memory and L1 cache. '9' is a good compromise. | |
409 | |
410 'nb_codes' : number of vlcs codes | |
411 | |
412 'bits' : table which gives the size (in bits) of each vlc code. | |
413 | |
414 'codes' : table which gives the bit pattern of of each vlc code. | |
415 | |
416 'xxx_wrap' : give the number of bytes between each entry of the | |
417 'bits' or 'codes' tables. | |
418 | |
419 'xxx_size' : gives the number of bytes of each entry of the 'bits' | |
420 or 'codes' tables. | |
421 | |
422 'wrap' and 'size' allows to use any memory configuration and types | |
423 (byte/word/long) to store the 'bits' and 'codes' tables. | |
424 */ | |
0 | 425 int init_vlc(VLC *vlc, int nb_bits, int nb_codes, |
426 const void *bits, int bits_wrap, int bits_size, | |
427 const void *codes, int codes_wrap, int codes_size) | |
428 { | |
429 vlc->bits = nb_bits; | |
430 vlc->table_bits = NULL; | |
431 vlc->table_codes = NULL; | |
432 vlc->table_allocated = 0; | |
433 vlc->table_size = 0; | |
434 #ifdef DEBUG_VLC | |
435 printf("build table nb_codes=%d\n", nb_codes); | |
436 #endif | |
437 | |
438 if (build_table(vlc, nb_bits, nb_codes, | |
439 bits, bits_wrap, bits_size, | |
440 codes, codes_wrap, codes_size, | |
441 0, 0) < 0) { | |
442 if (vlc->table_bits) | |
443 free(vlc->table_bits); | |
444 if (vlc->table_codes) | |
445 free(vlc->table_codes); | |
446 return -1; | |
447 } | |
448 return 0; | |
449 } | |
450 | |
451 | |
452 void free_vlc(VLC *vlc) | |
453 { | |
454 free(vlc->table_bits); | |
455 free(vlc->table_codes); | |
456 } | |
457 | |
458 int get_vlc(GetBitContext *s, VLC *vlc) | |
459 { | |
460 int bit_cnt, code, n, nb_bits, index; | |
461 UINT32 bit_buf; | |
462 INT16 *table_codes; | |
463 INT8 *table_bits; | |
464 UINT8 *buf_ptr; | |
465 | |
466 SAVE_BITS(s); | |
467 nb_bits = vlc->bits; | |
468 table_codes = vlc->table_codes; | |
469 table_bits = vlc->table_bits; | |
470 for(;;) { | |
471 SHOW_BITS(s, index, nb_bits); | |
472 code = table_codes[index]; | |
473 n = table_bits[index]; | |
474 if (n > 0) { | |
475 /* most common case */ | |
476 FLUSH_BITS(n); | |
477 #ifdef STATS | |
478 st_bit_counts[st_current_index] += n; | |
479 #endif | |
480 break; | |
481 } else if (n == 0) { | |
482 return -1; | |
483 } else { | |
484 FLUSH_BITS(nb_bits); | |
485 #ifdef STATS | |
486 st_bit_counts[st_current_index] += nb_bits; | |
487 #endif | |
488 nb_bits = -n; | |
489 table_codes = vlc->table_codes + code; | |
490 table_bits = vlc->table_bits + code; | |
491 } | |
492 } | |
493 RESTORE_BITS(s); | |
494 return code; | |
495 } |