Sync with mplayer's stuff
[libav.git] / libavcodec / common.c
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
32 #include "../bswap.h"
33
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
124 /* for jpeg : espace 0xff with 0x00 after it */
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 */
202 unsigned int get_bits(GetBitContext *s, int n)
203 {
204 unsigned int val;
205 int bit_cnt;
206 unsigned int bit_buf;
207 UINT8 *buf_ptr;
208
209 #ifdef STATS
210 st_bit_counts[st_current_index] += n;
211 #endif
212
213 bit_cnt = s->bit_cnt;
214 bit_buf = s->bit_buf;
215
216 bit_cnt -= n;
217 if (bit_cnt >= 0) {
218 /* most common case here */
219 val = bit_buf >> (32 - n);
220 bit_buf <<= n;
221 } else {
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) {
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) |
232 (buf_ptr[-2] << 8) |
233 (buf_ptr[-1]);
234 #endif
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
404 /* wrap and size allow to handle most types of storage. */
405 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
406 const void *bits, int bits_wrap, int bits_size,
407 const void *codes, int codes_wrap, int codes_size)
408 {
409 vlc->bits = nb_bits;
410 vlc->table_bits = NULL;
411 vlc->table_codes = NULL;
412 vlc->table_allocated = 0;
413 vlc->table_size = 0;
414 #ifdef DEBUG_VLC
415 printf("build table nb_codes=%d\n", nb_codes);
416 #endif
417
418 if (build_table(vlc, nb_bits, nb_codes,
419 bits, bits_wrap, bits_size,
420 codes, codes_wrap, codes_size,
421 0, 0) < 0) {
422 if (vlc->table_bits)
423 free(vlc->table_bits);
424 if (vlc->table_codes)
425 free(vlc->table_codes);
426 return -1;
427 }
428 return 0;
429 }
430
431
432 void free_vlc(VLC *vlc)
433 {
434 free(vlc->table_bits);
435 free(vlc->table_codes);
436 }
437
438 int get_vlc(GetBitContext *s, VLC *vlc)
439 {
440 int bit_cnt, code, n, nb_bits, index;
441 UINT32 bit_buf;
442 INT16 *table_codes;
443 INT8 *table_bits;
444 UINT8 *buf_ptr;
445
446 SAVE_BITS(s);
447 nb_bits = vlc->bits;
448 table_codes = vlc->table_codes;
449 table_bits = vlc->table_bits;
450 for(;;) {
451 SHOW_BITS(s, index, nb_bits);
452 code = table_codes[index];
453 n = table_bits[index];
454 if (n > 0) {
455 /* most common case */
456 FLUSH_BITS(n);
457 #ifdef STATS
458 st_bit_counts[st_current_index] += n;
459 #endif
460 break;
461 } else if (n == 0) {
462 return -1;
463 } else {
464 FLUSH_BITS(nb_bits);
465 #ifdef STATS
466 st_bit_counts[st_current_index] += nb_bits;
467 #endif
468 nb_bits = -n;
469 table_codes = vlc->table_codes + code;
470 table_bits = vlc->table_bits + code;
471 }
472 }
473 RESTORE_BITS(s);
474 return code;
475 }