DV audio decoder by Roman Shaposhnick
[libav.git] / libavcodec / common.c
CommitLineData
de6d9b64
FB
1/*
2 * Common bit i/o utils
ff4ec49e 3 * Copyright (c) 2000, 2001 Fabrice Bellard.
de6d9b64 4 *
ff4ec49e
FB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
de6d9b64 9 *
ff4ec49e 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
ff4ec49e
FB
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27a3e2c5 18 *
17592475 19 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
de6d9b64 20 */
df595131 21#include "avcodec.h"
a9b3f630 22
f36db5df
MN
23const UINT8 ff_sqrt_tab[128]={
24 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
25 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
26 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
27 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11
28};
29
de6d9b64
FB
30void init_put_bits(PutBitContext *s,
31 UINT8 *buffer, int buffer_size,
32 void *opaque,
33 void (*write_data)(void *, UINT8 *, int))
34{
35 s->buf = buffer;
de6d9b64 36 s->buf_end = s->buf + buffer_size;
de6d9b64 37 s->data_out_size = 0;
17592475
MN
38 if(write_data!=NULL)
39 {
40 fprintf(stderr, "write Data callback is not supported\n");
41 }
d8cf5aea
MN
42#ifdef ALT_BITSTREAM_WRITER
43 s->index=0;
44 ((uint32_t*)(s->buf))[0]=0;
45// memset(buffer, 0, buffer_size);
17592475 46#else
17592475 47 s->buf_ptr = s->buf;
d8cf5aea 48 s->bit_left=32;
17592475
MN
49 s->bit_buf=0;
50#endif
de6d9b64
FB
51}
52
de6d9b64 53/* return the number of bits output */
1a565432 54INT64 get_bit_count(PutBitContext *s)
de6d9b64 55{
17592475
MN
56#ifdef ALT_BITSTREAM_WRITER
57 return s->data_out_size * 8 + s->index;
58#else
d8cf5aea 59 return (s->buf_ptr - s->buf + s->data_out_size) * 8 + 32 - (INT64)s->bit_left;
17592475 60#endif
de6d9b64
FB
61}
62
63void align_put_bits(PutBitContext *s)
64{
17592475
MN
65#ifdef ALT_BITSTREAM_WRITER
66 put_bits(s,( - s->index) & 7,0);
67#else
d8cf5aea 68 put_bits(s,s->bit_left & 7,0);
17592475 69#endif
de6d9b64
FB
70}
71
72/* pad the end of the output stream with zeros */
73void flush_put_bits(PutBitContext *s)
74{
17592475
MN
75#ifdef ALT_BITSTREAM_WRITER
76 align_put_bits(s);
77#else
d8cf5aea
MN
78 s->bit_buf<<= s->bit_left;
79 while (s->bit_left < 32) {
de6d9b64
FB
80 /* XXX: should test end of buffer */
81 *s->buf_ptr++=s->bit_buf >> 24;
82 s->bit_buf<<=8;
d8cf5aea 83 s->bit_left+=8;
de6d9b64 84 }
d8cf5aea 85 s->bit_left=32;
de6d9b64 86 s->bit_buf=0;
17592475 87#endif
de6d9b64
FB
88}
89
9717dad8
MN
90void put_string(PutBitContext * pbc, char *s)
91{
92 while(*s){
93 put_bits(pbc, 8, *s);
94 s++;
95 }
96 put_bits(pbc, 8, 0);
97}
98
de6d9b64
FB
99/* bit input functions */
100
8db1a1dd 101void init_get_bits(GetBitContext *s,
68f593b4 102 UINT8 *buffer, int bit_size)
de6d9b64 103{
68f593b4
MN
104 const int buffer_size= (bit_size+7)>>3;
105
27a3e2c5 106 s->buffer= buffer;
68f593b4 107 s->size_in_bits= bit_size;
8db1a1dd
MN
108 s->buffer_end= buffer + buffer_size;
109#ifdef ALT_BITSTREAM_READER
110 s->index=0;
111#elif defined LIBMPEG2_BITSTREAM_READER
112 s->buffer_ptr = buffer;
113 s->bit_count = 16;
114 s->cache = 0;
115#elif defined A32_BITSTREAM_READER
116 s->buffer_ptr = (uint32_t*)buffer;
117 s->bit_count = 32;
118 s->cache0 = 0;
119 s->cache1 = 0;
de6d9b64 120#endif
2931ecb9 121 {
8db1a1dd
MN
122 OPEN_READER(re, s)
123 UPDATE_CACHE(re, s)
124// UPDATE_CACHE(re, s)
125 CLOSE_READER(re, s)
de6d9b64 126 }
8db1a1dd
MN
127#ifdef A32_BITSTREAM_READER
128 s->cache1 = 0;
27a3e2c5 129#endif
8db1a1dd 130}
de6d9b64
FB
131
132void align_get_bits(GetBitContext *s)
133{
8db1a1dd
MN
134 int n= (-get_bits_count(s)) & 7;
135 if(n) skip_bits(s, n);
de6d9b64 136}
27a3e2c5 137
18f77016 138int check_marker(GetBitContext *s, const char *msg)
49c9325f
MN
139{
140 int bit= get_bits1(s);
141 if(!bit) printf("Marker bit missing %s\n", msg);
142
143 return bit;
144}
145
de6d9b64
FB
146/* VLC decoding */
147
148//#define DEBUG_VLC
149
150#define GET_DATA(v, table, i, wrap, size) \
151{\
18f77016 152 const UINT8 *ptr = (UINT8 *)table + i * wrap;\
de6d9b64
FB
153 switch(size) {\
154 case 1:\
155 v = *(UINT8 *)ptr;\
156 break;\
157 case 2:\
158 v = *(UINT16 *)ptr;\
159 break;\
160 default:\
161 v = *(UINT32 *)ptr;\
162 break;\
163 }\
164}
165
166
167static int alloc_table(VLC *vlc, int size)
168{
169 int index;
170 index = vlc->table_size;
171 vlc->table_size += size;
172 if (vlc->table_size > vlc->table_allocated) {
173 vlc->table_allocated += (1 << vlc->bits);
8e1e6f31
FB
174 vlc->table = av_realloc(vlc->table,
175 sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
8db1a1dd 176 if (!vlc->table)
de6d9b64
FB
177 return -1;
178 }
179 return index;
180}
181
8db1a1dd 182static int build_table(VLC *vlc, int table_nb_bits,
de6d9b64
FB
183 int nb_codes,
184 const void *bits, int bits_wrap, int bits_size,
185 const void *codes, int codes_wrap, int codes_size,
186 UINT32 code_prefix, int n_prefix)
187{
188 int i, j, k, n, table_size, table_index, nb, n1, index;
189 UINT32 code;
8db1a1dd 190 VLC_TYPE (*table)[2];
de6d9b64
FB
191
192 table_size = 1 << table_nb_bits;
193 table_index = alloc_table(vlc, table_size);
194#ifdef DEBUG_VLC
8db1a1dd 195 printf("new table index=%d size=%d code_prefix=%x n=%d\n",
de6d9b64
FB
196 table_index, table_size, code_prefix, n_prefix);
197#endif
198 if (table_index < 0)
199 return -1;
8db1a1dd 200 table = &vlc->table[table_index];
de6d9b64
FB
201
202 for(i=0;i<table_size;i++) {
8db1a1dd
MN
203 table[i][1] = 0; //bits
204 table[i][0] = -1; //codes
de6d9b64
FB
205 }
206
207 /* first pass: map codes and compute auxillary table sizes */
208 for(i=0;i<nb_codes;i++) {
209 GET_DATA(n, bits, i, bits_wrap, bits_size);
210 GET_DATA(code, codes, i, codes_wrap, codes_size);
211 /* we accept tables with holes */
212 if (n <= 0)
213 continue;
214#if defined(DEBUG_VLC) && 0
215 printf("i=%d n=%d code=0x%x\n", i, n, code);
216#endif
217 /* if code matches the prefix, it is in the table */
218 n -= n_prefix;
219 if (n > 0 && (code >> n) == code_prefix) {
220 if (n <= table_nb_bits) {
221 /* no need to add another table */
222 j = (code << (table_nb_bits - n)) & (table_size - 1);
223 nb = 1 << (table_nb_bits - n);
224 for(k=0;k<nb;k++) {
225#ifdef DEBUG_VLC
226 printf("%4x: code=%d n=%d\n",
227 j, i, n);
228#endif
8db1a1dd 229 if (table[j][1] /*bits*/ != 0) {
de6d9b64
FB
230 fprintf(stderr, "incorrect codes\n");
231 exit(1);
232 }
8db1a1dd
MN
233 table[j][1] = n; //bits
234 table[j][0] = i; //code
de6d9b64
FB
235 j++;
236 }
237 } else {
238 n -= table_nb_bits;
239 j = (code >> n) & ((1 << table_nb_bits) - 1);
240#ifdef DEBUG_VLC
241 printf("%4x: n=%d (subtable)\n",
242 j, n);
243#endif
244 /* compute table size */
8db1a1dd 245 n1 = -table[j][1]; //bits
de6d9b64
FB
246 if (n > n1)
247 n1 = n;
8db1a1dd 248 table[j][1] = -n1; //bits
de6d9b64
FB
249 }
250 }
251 }
252
253 /* second pass : fill auxillary tables recursively */
254 for(i=0;i<table_size;i++) {
8db1a1dd 255 n = table[i][1]; //bits
de6d9b64
FB
256 if (n < 0) {
257 n = -n;
258 if (n > table_nb_bits) {
259 n = table_nb_bits;
8db1a1dd 260 table[i][1] = -n; //bits
de6d9b64
FB
261 }
262 index = build_table(vlc, n, nb_codes,
263 bits, bits_wrap, bits_size,
264 codes, codes_wrap, codes_size,
265 (code_prefix << table_nb_bits) | i,
266 n_prefix + table_nb_bits);
267 if (index < 0)
268 return -1;
269 /* note: realloc has been done, so reload tables */
8db1a1dd 270 table = &vlc->table[table_index];
6300c80a 271 table[i][0] = index; //code
de6d9b64
FB
272 }
273 }
274 return table_index;
275}
276
277
4e66ab3b
FB
278/* Build VLC decoding tables suitable for use with get_vlc().
279
280 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
281 bigger it is, the faster is the decoding. But it should not be too
282 big to save memory and L1 cache. '9' is a good compromise.
283
284 'nb_codes' : number of vlcs codes
285
286 'bits' : table which gives the size (in bits) of each vlc code.
287
288 'codes' : table which gives the bit pattern of of each vlc code.
289
290 'xxx_wrap' : give the number of bytes between each entry of the
291 'bits' or 'codes' tables.
292
293 'xxx_size' : gives the number of bytes of each entry of the 'bits'
294 or 'codes' tables.
295
296 'wrap' and 'size' allows to use any memory configuration and types
297 (byte/word/long) to store the 'bits' and 'codes' tables.
298*/
de6d9b64
FB
299int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
300 const void *bits, int bits_wrap, int bits_size,
301 const void *codes, int codes_wrap, int codes_size)
302{
303 vlc->bits = nb_bits;
8db1a1dd 304 vlc->table = NULL;
de6d9b64
FB
305 vlc->table_allocated = 0;
306 vlc->table_size = 0;
307#ifdef DEBUG_VLC
308 printf("build table nb_codes=%d\n", nb_codes);
309#endif
310
311 if (build_table(vlc, nb_bits, nb_codes,
312 bits, bits_wrap, bits_size,
313 codes, codes_wrap, codes_size,
314 0, 0) < 0) {
8db1a1dd 315 av_free(vlc->table);
de6d9b64
FB
316 return -1;
317 }
318 return 0;
319}
320
321
322void free_vlc(VLC *vlc)
323{
8db1a1dd 324 av_free(vlc->table);
de6d9b64
FB
325}
326
9dbcbd92
MN
327int ff_gcd(int a, int b){
328 if(b) return ff_gcd(b, a%b);
329 else return a;
330}
5d3cea3a
MN
331
332void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max){
333 double best_diff=1E10, diff;
334 int best_denom=1, best_nom=1;
335 int nom, denom, gcd;
336
337 //brute force here, perhaps we should try continued fractions if we need large max ...
338 for(denom=1; denom<=max; denom++){
339 nom= (int)(f*denom + 0.5);
340 if(nom<=0 || nom>max) continue;
341
342 diff= ABS( f - (double)nom / (double)denom );
343 if(diff < best_diff){
344 best_diff= diff;
345 best_nom= nom;
346 best_denom= denom;
347 }
348 }
349
350 gcd= ff_gcd(best_nom, best_denom);
351 best_nom /= gcd;
352 best_denom /= gcd;
353
354 *nom_arg= best_nom;
355 *denom_arg= best_denom;
356}