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