6323167838b31823d4563a7303a8dabcddc12865
[libav.git] / libavcodec / bitstream.c
1 /*
2 * Common bit i/o utils
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 /**
26 * @file bitstream.c
27 * bitstream api.
28 */
29
30 #include "avcodec.h"
31 #include "bitstream.h"
32
33 /**
34 * Same as av_mallocz_static(), but does a realloc.
35 *
36 * @param[in] ptr The block of memory to reallocate.
37 * @param[in] size The requested size.
38 * @return Block of memory of requested size.
39 * @deprecated. Code which uses ff_realloc_static is broken/misdesigned
40 * and should correctly use static arrays
41 */
42 attribute_deprecated av_alloc_size(2)
43 static void *ff_realloc_static(void *ptr, unsigned int size);
44
45 static unsigned int last_static = 0;
46 static unsigned int allocated_static = 0;
47 static void** array_static = NULL;
48
49 static void *av_mallocz_static(unsigned int size)
50 {
51 void *ptr = av_mallocz(size);
52
53 if(ptr){
54 array_static =av_fast_realloc(array_static, &allocated_static, sizeof(void*)*(last_static+1));
55 if(!array_static)
56 return NULL;
57 array_static[last_static++] = ptr;
58 }
59
60 return ptr;
61 }
62
63 static void *ff_realloc_static(void *ptr, unsigned int size)
64 {
65 int i;
66 if(!ptr)
67 return av_mallocz_static(size);
68 /* Look for the old ptr */
69 for(i = 0; i < last_static; i++) {
70 if(array_static[i] == ptr) {
71 array_static[i] = av_realloc(array_static[i], size);
72 return array_static[i];
73 }
74 }
75 return NULL;
76
77 }
78
79 static void av_free_static(void)
80 {
81 while(last_static){
82 av_freep(&array_static[--last_static]);
83 }
84 av_freep(&array_static);
85 }
86
87
88 void align_put_bits(PutBitContext *s)
89 {
90 #ifdef ALT_BITSTREAM_WRITER
91 put_bits(s,( - s->index) & 7,0);
92 #else
93 put_bits(s,s->bit_left & 7,0);
94 #endif
95 }
96
97 void ff_put_string(PutBitContext * pbc, const char *s, int put_zero)
98 {
99 while(*s){
100 put_bits(pbc, 8, *s);
101 s++;
102 }
103 if(put_zero)
104 put_bits(pbc, 8, 0);
105 }
106
107 void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
108 {
109 const uint16_t *srcw= (const uint16_t*)src;
110 int words= length>>4;
111 int bits= length&15;
112 int i;
113
114 if(length==0) return;
115
116 if(ENABLE_SMALL || words < 16 || put_bits_count(pb)&7){
117 for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
118 }else{
119 for(i=0; put_bits_count(pb)&31; i++)
120 put_bits(pb, 8, src[i]);
121 flush_put_bits(pb);
122 memcpy(pbBufPtr(pb), src+i, 2*words-i);
123 skip_put_bytes(pb, 2*words-i);
124 }
125
126 put_bits(pb, bits, be2me_16(srcw[words])>>(16-bits));
127 }
128
129 /* VLC decoding */
130
131 //#define DEBUG_VLC
132
133 #define GET_DATA(v, table, i, wrap, size) \
134 {\
135 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
136 switch(size) {\
137 case 1:\
138 v = *(const uint8_t *)ptr;\
139 break;\
140 case 2:\
141 v = *(const uint16_t *)ptr;\
142 break;\
143 default:\
144 v = *(const uint32_t *)ptr;\
145 break;\
146 }\
147 }
148
149
150 static int alloc_table(VLC *vlc, int size, int use_static)
151 {
152 int index;
153 index = vlc->table_size;
154 vlc->table_size += size;
155 if (vlc->table_size > vlc->table_allocated) {
156 if(use_static>1)
157 abort(); //cant do anything, init_vlc() is used with too little memory
158 vlc->table_allocated += (1 << vlc->bits);
159 if(use_static)
160 vlc->table = ff_realloc_static(vlc->table,
161 sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
162 else
163 vlc->table = av_realloc(vlc->table,
164 sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
165 if (!vlc->table)
166 return -1;
167 }
168 return index;
169 }
170
171 static int build_table(VLC *vlc, int table_nb_bits,
172 int nb_codes,
173 const void *bits, int bits_wrap, int bits_size,
174 const void *codes, int codes_wrap, int codes_size,
175 const void *symbols, int symbols_wrap, int symbols_size,
176 uint32_t code_prefix, int n_prefix, int flags)
177 {
178 int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2, symbol;
179 uint32_t code;
180 VLC_TYPE (*table)[2];
181
182 table_size = 1 << table_nb_bits;
183 table_index = alloc_table(vlc, table_size, flags & (INIT_VLC_USE_STATIC|INIT_VLC_USE_NEW_STATIC));
184 #ifdef DEBUG_VLC
185 av_log(NULL,AV_LOG_DEBUG,"new table index=%d size=%d code_prefix=%x n=%d\n",
186 table_index, table_size, code_prefix, n_prefix);
187 #endif
188 if (table_index < 0)
189 return -1;
190 table = &vlc->table[table_index];
191
192 for(i=0;i<table_size;i++) {
193 table[i][1] = 0; //bits
194 table[i][0] = -1; //codes
195 }
196
197 /* first pass: map codes and compute auxillary table sizes */
198 for(i=0;i<nb_codes;i++) {
199 GET_DATA(n, bits, i, bits_wrap, bits_size);
200 GET_DATA(code, codes, i, codes_wrap, codes_size);
201 /* we accept tables with holes */
202 if (n <= 0)
203 continue;
204 if (!symbols)
205 symbol = i;
206 else
207 GET_DATA(symbol, symbols, i, symbols_wrap, symbols_size);
208 #if defined(DEBUG_VLC) && 0
209 av_log(NULL,AV_LOG_DEBUG,"i=%d n=%d code=0x%x\n", i, n, code);
210 #endif
211 /* if code matches the prefix, it is in the table */
212 n -= n_prefix;
213 if(flags & INIT_VLC_LE)
214 code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1);
215 else
216 code_prefix2= code >> n;
217 if (n > 0 && code_prefix2 == code_prefix) {
218 if (n <= table_nb_bits) {
219 /* no need to add another table */
220 j = (code << (table_nb_bits - n)) & (table_size - 1);
221 nb = 1 << (table_nb_bits - n);
222 for(k=0;k<nb;k++) {
223 if(flags & INIT_VLC_LE)
224 j = (code >> n_prefix) + (k<<n);
225 #ifdef DEBUG_VLC
226 av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
227 j, i, n);
228 #endif
229 if (table[j][1] /*bits*/ != 0) {
230 av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
231 return -1;
232 }
233 table[j][1] = n; //bits
234 table[j][0] = symbol;
235 j++;
236 }
237 } else {
238 n -= table_nb_bits;
239 j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1);
240 #ifdef DEBUG_VLC
241 av_log(NULL,AV_LOG_DEBUG,"%4x: n=%d (subtable)\n",
242 j, n);
243 #endif
244 /* compute table size */
245 n1 = -table[j][1]; //bits
246 if (n > n1)
247 n1 = n;
248 table[j][1] = -n1; //bits
249 }
250 }
251 }
252
253 /* second pass : fill auxillary tables recursively */
254 for(i=0;i<table_size;i++) {
255 n = table[i][1]; //bits
256 if (n < 0) {
257 n = -n;
258 if (n > table_nb_bits) {
259 n = table_nb_bits;
260 table[i][1] = -n; //bits
261 }
262 index = build_table(vlc, n, nb_codes,
263 bits, bits_wrap, bits_size,
264 codes, codes_wrap, codes_size,
265 symbols, symbols_wrap, symbols_size,
266 (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
267 n_prefix + table_nb_bits, flags);
268 if (index < 0)
269 return -1;
270 /* note: realloc has been done, so reload tables */
271 table = &vlc->table[table_index];
272 table[i][0] = index; //code
273 }
274 }
275 return table_index;
276 }
277
278
279 /* Build VLC decoding tables suitable for use with get_vlc().
280
281 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
282 bigger it is, the faster is the decoding. But it should not be too
283 big to save memory and L1 cache. '9' is a good compromise.
284
285 'nb_codes' : number of vlcs codes
286
287 'bits' : table which gives the size (in bits) of each vlc code.
288
289 'codes' : table which gives the bit pattern of of each vlc code.
290
291 'symbols' : table which gives the values to be returned from get_vlc().
292
293 'xxx_wrap' : give the number of bytes between each entry of the
294 'bits' or 'codes' tables.
295
296 'xxx_size' : gives the number of bytes of each entry of the 'bits'
297 or 'codes' tables.
298
299 'wrap' and 'size' allows to use any memory configuration and types
300 (byte/word/long) to store the 'bits', 'codes', and 'symbols' tables.
301
302 'use_static' should be set to 1 for tables, which should be freed
303 with av_free_static(), 0 if free_vlc() will be used.
304 */
305 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
306 const void *bits, int bits_wrap, int bits_size,
307 const void *codes, int codes_wrap, int codes_size,
308 const void *symbols, int symbols_wrap, int symbols_size,
309 int flags)
310 {
311 vlc->bits = nb_bits;
312 if(flags & INIT_VLC_USE_NEW_STATIC){
313 if(vlc->table_size && vlc->table_size == vlc->table_allocated){
314 return 0;
315 }else if(vlc->table_size){
316 abort(); // fatal error, we are called on a partially initialized table
317 }
318 }else if(!(flags & INIT_VLC_USE_STATIC)) {
319 vlc->table = NULL;
320 vlc->table_allocated = 0;
321 vlc->table_size = 0;
322 } else {
323 /* Static tables are initially always NULL, return
324 if vlc->table != NULL to avoid double allocation */
325 if(vlc->table)
326 return 0;
327 }
328
329 #ifdef DEBUG_VLC
330 av_log(NULL,AV_LOG_DEBUG,"build table nb_codes=%d\n", nb_codes);
331 #endif
332
333 if (build_table(vlc, nb_bits, nb_codes,
334 bits, bits_wrap, bits_size,
335 codes, codes_wrap, codes_size,
336 symbols, symbols_wrap, symbols_size,
337 0, 0, flags) < 0) {
338 av_freep(&vlc->table);
339 return -1;
340 }
341 if((flags & INIT_VLC_USE_NEW_STATIC) && vlc->table_size != vlc->table_allocated)
342 av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", vlc->table_size, vlc->table_allocated);
343 return 0;
344 }
345
346
347 void free_vlc(VLC *vlc)
348 {
349 av_freep(&vlc->table);
350 }
351