Remove the data parameter from draw_slice(). It's unneeded and often more
[ffmpeg-lucabe.git] / libavcodec / bitstream.c
blobb74775e9c0b3888d61dbecdc81cb40b991bc01ef
1 /*
2 * Common bit i/o utils
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
8 * This file is part of FFmpeg.
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.
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.
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
25 /**
26 * @file bitstream.c
27 * bitstream api.
30 #include "avcodec.h"
31 #include "bitstream.h"
33 /**
34 * Same as av_mallocz_static(), but does a realloc.
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
42 attribute_deprecated void *ff_realloc_static(void *ptr, unsigned int size);
44 void align_put_bits(PutBitContext *s)
46 #ifdef ALT_BITSTREAM_WRITER
47 put_bits(s,( - s->index) & 7,0);
48 #else
49 put_bits(s,s->bit_left & 7,0);
50 #endif
53 void ff_put_string(PutBitContext * pbc, const char *s, int put_zero)
55 while(*s){
56 put_bits(pbc, 8, *s);
57 s++;
59 if(put_zero)
60 put_bits(pbc, 8, 0);
63 void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
65 const uint16_t *srcw= (const uint16_t*)src;
66 int words= length>>4;
67 int bits= length&15;
68 int i;
70 if(length==0) return;
72 if(ENABLE_SMALL || words < 16 || put_bits_count(pb)&7){
73 for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
74 }else{
75 for(i=0; put_bits_count(pb)&31; i++)
76 put_bits(pb, 8, src[i]);
77 flush_put_bits(pb);
78 memcpy(pbBufPtr(pb), src+i, 2*words-i);
79 skip_put_bytes(pb, 2*words-i);
82 put_bits(pb, bits, be2me_16(srcw[words])>>(16-bits));
85 /* VLC decoding */
87 //#define DEBUG_VLC
89 #define GET_DATA(v, table, i, wrap, size) \
91 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
92 switch(size) {\
93 case 1:\
94 v = *(const uint8_t *)ptr;\
95 break;\
96 case 2:\
97 v = *(const uint16_t *)ptr;\
98 break;\
99 default:\
100 v = *(const uint32_t *)ptr;\
101 break;\
106 static int alloc_table(VLC *vlc, int size, int use_static)
108 int index;
109 index = vlc->table_size;
110 vlc->table_size += size;
111 if (vlc->table_size > vlc->table_allocated) {
112 vlc->table_allocated += (1 << vlc->bits);
113 if(use_static)
114 vlc->table = ff_realloc_static(vlc->table,
115 sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
116 else
117 vlc->table = av_realloc(vlc->table,
118 sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
119 if (!vlc->table)
120 return -1;
122 return index;
125 static int build_table(VLC *vlc, int table_nb_bits,
126 int nb_codes,
127 const void *bits, int bits_wrap, int bits_size,
128 const void *codes, int codes_wrap, int codes_size,
129 const void *symbols, int symbols_wrap, int symbols_size,
130 uint32_t code_prefix, int n_prefix, int flags)
132 int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2, symbol;
133 uint32_t code;
134 VLC_TYPE (*table)[2];
136 table_size = 1 << table_nb_bits;
137 table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_STATIC);
138 #ifdef DEBUG_VLC
139 av_log(NULL,AV_LOG_DEBUG,"new table index=%d size=%d code_prefix=%x n=%d\n",
140 table_index, table_size, code_prefix, n_prefix);
141 #endif
142 if (table_index < 0)
143 return -1;
144 table = &vlc->table[table_index];
146 for(i=0;i<table_size;i++) {
147 table[i][1] = 0; //bits
148 table[i][0] = -1; //codes
151 /* first pass: map codes and compute auxillary table sizes */
152 for(i=0;i<nb_codes;i++) {
153 GET_DATA(n, bits, i, bits_wrap, bits_size);
154 GET_DATA(code, codes, i, codes_wrap, codes_size);
155 /* we accept tables with holes */
156 if (n <= 0)
157 continue;
158 if (!symbols)
159 symbol = i;
160 else
161 GET_DATA(symbol, symbols, i, symbols_wrap, symbols_size);
162 #if defined(DEBUG_VLC) && 0
163 av_log(NULL,AV_LOG_DEBUG,"i=%d n=%d code=0x%x\n", i, n, code);
164 #endif
165 /* if code matches the prefix, it is in the table */
166 n -= n_prefix;
167 if(flags & INIT_VLC_LE)
168 code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1);
169 else
170 code_prefix2= code >> n;
171 if (n > 0 && code_prefix2 == code_prefix) {
172 if (n <= table_nb_bits) {
173 /* no need to add another table */
174 j = (code << (table_nb_bits - n)) & (table_size - 1);
175 nb = 1 << (table_nb_bits - n);
176 for(k=0;k<nb;k++) {
177 if(flags & INIT_VLC_LE)
178 j = (code >> n_prefix) + (k<<n);
179 #ifdef DEBUG_VLC
180 av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
181 j, i, n);
182 #endif
183 if (table[j][1] /*bits*/ != 0) {
184 av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
185 return -1;
187 table[j][1] = n; //bits
188 table[j][0] = symbol;
189 j++;
191 } else {
192 n -= table_nb_bits;
193 j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1);
194 #ifdef DEBUG_VLC
195 av_log(NULL,AV_LOG_DEBUG,"%4x: n=%d (subtable)\n",
196 j, n);
197 #endif
198 /* compute table size */
199 n1 = -table[j][1]; //bits
200 if (n > n1)
201 n1 = n;
202 table[j][1] = -n1; //bits
207 /* second pass : fill auxillary tables recursively */
208 for(i=0;i<table_size;i++) {
209 n = table[i][1]; //bits
210 if (n < 0) {
211 n = -n;
212 if (n > table_nb_bits) {
213 n = table_nb_bits;
214 table[i][1] = -n; //bits
216 index = build_table(vlc, n, nb_codes,
217 bits, bits_wrap, bits_size,
218 codes, codes_wrap, codes_size,
219 symbols, symbols_wrap, symbols_size,
220 (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
221 n_prefix + table_nb_bits, flags);
222 if (index < 0)
223 return -1;
224 /* note: realloc has been done, so reload tables */
225 table = &vlc->table[table_index];
226 table[i][0] = index; //code
229 return table_index;
233 /* Build VLC decoding tables suitable for use with get_vlc().
235 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
236 bigger it is, the faster is the decoding. But it should not be too
237 big to save memory and L1 cache. '9' is a good compromise.
239 'nb_codes' : number of vlcs codes
241 'bits' : table which gives the size (in bits) of each vlc code.
243 'codes' : table which gives the bit pattern of of each vlc code.
245 'symbols' : table which gives the values to be returned from get_vlc().
247 'xxx_wrap' : give the number of bytes between each entry of the
248 'bits' or 'codes' tables.
250 'xxx_size' : gives the number of bytes of each entry of the 'bits'
251 or 'codes' tables.
253 'wrap' and 'size' allows to use any memory configuration and types
254 (byte/word/long) to store the 'bits', 'codes', and 'symbols' tables.
256 'use_static' should be set to 1 for tables, which should be freed
257 with av_free_static(), 0 if free_vlc() will be used.
259 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
260 const void *bits, int bits_wrap, int bits_size,
261 const void *codes, int codes_wrap, int codes_size,
262 const void *symbols, int symbols_wrap, int symbols_size,
263 int flags)
265 vlc->bits = nb_bits;
266 if(!(flags & INIT_VLC_USE_STATIC)) {
267 vlc->table = NULL;
268 vlc->table_allocated = 0;
269 vlc->table_size = 0;
270 } else {
271 /* Static tables are initially always NULL, return
272 if vlc->table != NULL to avoid double allocation */
273 if(vlc->table)
274 return 0;
277 #ifdef DEBUG_VLC
278 av_log(NULL,AV_LOG_DEBUG,"build table nb_codes=%d\n", nb_codes);
279 #endif
281 if (build_table(vlc, nb_bits, nb_codes,
282 bits, bits_wrap, bits_size,
283 codes, codes_wrap, codes_size,
284 symbols, symbols_wrap, symbols_size,
285 0, 0, flags) < 0) {
286 av_freep(&vlc->table);
287 return -1;
289 return 0;
293 void free_vlc(VLC *vlc)
295 av_freep(&vlc->table);