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