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