Commit | Line | Data |
---|---|---|
de6d9b64 FB |
1 | /* |
2 | * Common bit i/o utils | |
ff4ec49e | 3 | * Copyright (c) 2000, 2001 Fabrice Bellard. |
8f2ab833 | 4 | * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> |
de6d9b64 | 5 | * |
7b94177e DB |
6 | * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at> |
7 | * | |
b78e7197 DB |
8 | * This file is part of FFmpeg. |
9 | * | |
10 | * FFmpeg is free software; you can redistribute it and/or | |
ff4ec49e FB |
11 | * modify it under the terms of the GNU Lesser General Public |
12 | * License as published by the Free Software Foundation; either | |
b78e7197 | 13 | * version 2.1 of the License, or (at your option) any later version. |
de6d9b64 | 14 | * |
b78e7197 | 15 | * FFmpeg is distributed in the hope that it will be useful, |
de6d9b64 | 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ff4ec49e FB |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 | * Lesser General Public License for more details. | |
de6d9b64 | 19 | * |
ff4ec49e | 20 | * You should have received a copy of the GNU Lesser General Public |
b78e7197 | 21 | * License along with FFmpeg; if not, write to the Free Software |
5509bffa | 22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
de6d9b64 | 23 | */ |
983e3246 MN |
24 | |
25 | /** | |
caa336b4 MN |
26 | * @file bitstream.c |
27 | * bitstream api. | |
983e3246 | 28 | */ |
115329f1 | 29 | |
df595131 | 30 | #include "avcodec.h" |
caa336b4 | 31 | #include "bitstream.h" |
c81f0349 | 32 | |
beebfdb1 PI |
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. | |
671adb17 MC |
39 | * @deprecated. Code which uses ff_realloc_static is broken/missdesigned |
40 | * and should correctly use static arrays | |
beebfdb1 | 41 | */ |
0769498a | 42 | attribute_deprecated void *ff_realloc_static(void *ptr, unsigned int size); |
beebfdb1 | 43 | |
de6d9b64 FB |
44 | void align_put_bits(PutBitContext *s) |
45 | { | |
17592475 MN |
46 | #ifdef ALT_BITSTREAM_WRITER |
47 | put_bits(s,( - s->index) & 7,0); | |
48 | #else | |
d8cf5aea | 49 | put_bits(s,s->bit_left & 7,0); |
17592475 | 50 | #endif |
de6d9b64 FB |
51 | } |
52 | ||
344b825c | 53 | void ff_put_string(PutBitContext * pbc, char *s, int put_zero) |
9717dad8 MN |
54 | { |
55 | while(*s){ | |
56 | put_bits(pbc, 8, *s); | |
57 | s++; | |
58 | } | |
99683a30 MN |
59 | if(put_zero) |
60 | put_bits(pbc, 8, 0); | |
9717dad8 MN |
61 | } |
62 | ||
de6d9b64 FB |
63 | /* VLC decoding */ |
64 | ||
65 | //#define DEBUG_VLC | |
66 | ||
67 | #define GET_DATA(v, table, i, wrap, size) \ | |
68 | {\ | |
0c1a9eda | 69 | const uint8_t *ptr = (const uint8_t *)table + i * wrap;\ |
de6d9b64 FB |
70 | switch(size) {\ |
71 | case 1:\ | |
0c1a9eda | 72 | v = *(const uint8_t *)ptr;\ |
de6d9b64 FB |
73 | break;\ |
74 | case 2:\ | |
0c1a9eda | 75 | v = *(const uint16_t *)ptr;\ |
de6d9b64 FB |
76 | break;\ |
77 | default:\ | |
0c1a9eda | 78 | v = *(const uint32_t *)ptr;\ |
de6d9b64 FB |
79 | break;\ |
80 | }\ | |
81 | } | |
82 | ||
83 | ||
073c2593 | 84 | static int alloc_table(VLC *vlc, int size, int use_static) |
de6d9b64 FB |
85 | { |
86 | int index; | |
87 | index = vlc->table_size; | |
88 | vlc->table_size += size; | |
89 | if (vlc->table_size > vlc->table_allocated) { | |
90 | vlc->table_allocated += (1 << vlc->bits); | |
073c2593 | 91 | if(use_static) |
5dad0282 | 92 | vlc->table = ff_realloc_static(vlc->table, |
073c2593 BP |
93 | sizeof(VLC_TYPE) * 2 * vlc->table_allocated); |
94 | else | |
95 | vlc->table = av_realloc(vlc->table, | |
96 | sizeof(VLC_TYPE) * 2 * vlc->table_allocated); | |
8db1a1dd | 97 | if (!vlc->table) |
de6d9b64 FB |
98 | return -1; |
99 | } | |
100 | return index; | |
101 | } | |
102 | ||
8db1a1dd | 103 | static int build_table(VLC *vlc, int table_nb_bits, |
de6d9b64 FB |
104 | int nb_codes, |
105 | const void *bits, int bits_wrap, int bits_size, | |
106 | const void *codes, int codes_wrap, int codes_size, | |
b613bacc | 107 | const void *symbols, int symbols_wrap, int symbols_size, |
cea27ac7 | 108 | uint32_t code_prefix, int n_prefix, int flags) |
de6d9b64 | 109 | { |
b613bacc | 110 | int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2, symbol; |
0c1a9eda | 111 | uint32_t code; |
8db1a1dd | 112 | VLC_TYPE (*table)[2]; |
de6d9b64 FB |
113 | |
114 | table_size = 1 << table_nb_bits; | |
cea27ac7 | 115 | table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_STATIC); |
de6d9b64 | 116 | #ifdef DEBUG_VLC |
b8a99745 | 117 | av_log(NULL,AV_LOG_DEBUG,"new table index=%d size=%d code_prefix=%x n=%d\n", |
de6d9b64 FB |
118 | table_index, table_size, code_prefix, n_prefix); |
119 | #endif | |
120 | if (table_index < 0) | |
121 | return -1; | |
8db1a1dd | 122 | table = &vlc->table[table_index]; |
de6d9b64 FB |
123 | |
124 | for(i=0;i<table_size;i++) { | |
8db1a1dd MN |
125 | table[i][1] = 0; //bits |
126 | table[i][0] = -1; //codes | |
de6d9b64 FB |
127 | } |
128 | ||
129 | /* first pass: map codes and compute auxillary table sizes */ | |
130 | for(i=0;i<nb_codes;i++) { | |
131 | GET_DATA(n, bits, i, bits_wrap, bits_size); | |
132 | GET_DATA(code, codes, i, codes_wrap, codes_size); | |
133 | /* we accept tables with holes */ | |
134 | if (n <= 0) | |
135 | continue; | |
b613bacc LM |
136 | if (!symbols) |
137 | symbol = i; | |
138 | else | |
139 | GET_DATA(symbol, symbols, i, symbols_wrap, symbols_size); | |
de6d9b64 | 140 | #if defined(DEBUG_VLC) && 0 |
b8a99745 | 141 | av_log(NULL,AV_LOG_DEBUG,"i=%d n=%d code=0x%x\n", i, n, code); |
de6d9b64 FB |
142 | #endif |
143 | /* if code matches the prefix, it is in the table */ | |
144 | n -= n_prefix; | |
cea27ac7 MN |
145 | if(flags & INIT_VLC_LE) |
146 | code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1); | |
147 | else | |
148 | code_prefix2= code >> n; | |
149 | if (n > 0 && code_prefix2 == code_prefix) { | |
de6d9b64 FB |
150 | if (n <= table_nb_bits) { |
151 | /* no need to add another table */ | |
152 | j = (code << (table_nb_bits - n)) & (table_size - 1); | |
153 | nb = 1 << (table_nb_bits - n); | |
154 | for(k=0;k<nb;k++) { | |
cea27ac7 MN |
155 | if(flags & INIT_VLC_LE) |
156 | j = (code >> n_prefix) + (k<<n); | |
de6d9b64 | 157 | #ifdef DEBUG_VLC |
3d0ef6dd | 158 | av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n", |
de6d9b64 FB |
159 | j, i, n); |
160 | #endif | |
8db1a1dd | 161 | if (table[j][1] /*bits*/ != 0) { |
9b879566 | 162 | av_log(NULL, AV_LOG_ERROR, "incorrect codes\n"); |
9fe5a7b8 | 163 | return -1; |
de6d9b64 | 164 | } |
8db1a1dd | 165 | table[j][1] = n; //bits |
b613bacc | 166 | table[j][0] = symbol; |
de6d9b64 FB |
167 | j++; |
168 | } | |
169 | } else { | |
170 | n -= table_nb_bits; | |
cea27ac7 | 171 | j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1); |
de6d9b64 | 172 | #ifdef DEBUG_VLC |
b8a99745 | 173 | av_log(NULL,AV_LOG_DEBUG,"%4x: n=%d (subtable)\n", |
de6d9b64 FB |
174 | j, n); |
175 | #endif | |
176 | /* compute table size */ | |
8db1a1dd | 177 | n1 = -table[j][1]; //bits |
de6d9b64 FB |
178 | if (n > n1) |
179 | n1 = n; | |
8db1a1dd | 180 | table[j][1] = -n1; //bits |
de6d9b64 FB |
181 | } |
182 | } | |
183 | } | |
184 | ||
185 | /* second pass : fill auxillary tables recursively */ | |
186 | for(i=0;i<table_size;i++) { | |
8db1a1dd | 187 | n = table[i][1]; //bits |
de6d9b64 FB |
188 | if (n < 0) { |
189 | n = -n; | |
190 | if (n > table_nb_bits) { | |
191 | n = table_nb_bits; | |
8db1a1dd | 192 | table[i][1] = -n; //bits |
de6d9b64 FB |
193 | } |
194 | index = build_table(vlc, n, nb_codes, | |
195 | bits, bits_wrap, bits_size, | |
196 | codes, codes_wrap, codes_size, | |
b613bacc | 197 | symbols, symbols_wrap, symbols_size, |
cea27ac7 MN |
198 | (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i), |
199 | n_prefix + table_nb_bits, flags); | |
de6d9b64 FB |
200 | if (index < 0) |
201 | return -1; | |
202 | /* note: realloc has been done, so reload tables */ | |
8db1a1dd | 203 | table = &vlc->table[table_index]; |
6300c80a | 204 | table[i][0] = index; //code |
de6d9b64 FB |
205 | } |
206 | } | |
207 | return table_index; | |
208 | } | |
209 | ||
210 | ||
4e66ab3b FB |
211 | /* Build VLC decoding tables suitable for use with get_vlc(). |
212 | ||
213 | 'nb_bits' set thee decoding table size (2^nb_bits) entries. The | |
214 | bigger it is, the faster is the decoding. But it should not be too | |
215 | big to save memory and L1 cache. '9' is a good compromise. | |
115329f1 | 216 | |
4e66ab3b FB |
217 | 'nb_codes' : number of vlcs codes |
218 | ||
219 | 'bits' : table which gives the size (in bits) of each vlc code. | |
220 | ||
221 | 'codes' : table which gives the bit pattern of of each vlc code. | |
222 | ||
b613bacc LM |
223 | 'symbols' : table which gives the values to be returned from get_vlc(). |
224 | ||
4e66ab3b FB |
225 | 'xxx_wrap' : give the number of bytes between each entry of the |
226 | 'bits' or 'codes' tables. | |
227 | ||
228 | 'xxx_size' : gives the number of bytes of each entry of the 'bits' | |
229 | or 'codes' tables. | |
230 | ||
231 | 'wrap' and 'size' allows to use any memory configuration and types | |
b613bacc | 232 | (byte/word/long) to store the 'bits', 'codes', and 'symbols' tables. |
073c2593 BP |
233 | |
234 | 'use_static' should be set to 1 for tables, which should be freed | |
235 | with av_free_static(), 0 if free_vlc() will be used. | |
4e66ab3b | 236 | */ |
b613bacc | 237 | int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes, |
de6d9b64 | 238 | const void *bits, int bits_wrap, int bits_size, |
073c2593 | 239 | const void *codes, int codes_wrap, int codes_size, |
b613bacc | 240 | const void *symbols, int symbols_wrap, int symbols_size, |
d7645fb9 | 241 | int flags) |
de6d9b64 FB |
242 | { |
243 | vlc->bits = nb_bits; | |
d7645fb9 | 244 | if(!(flags & INIT_VLC_USE_STATIC)) { |
073c2593 BP |
245 | vlc->table = NULL; |
246 | vlc->table_allocated = 0; | |
247 | vlc->table_size = 0; | |
248 | } else { | |
249 | /* Static tables are initially always NULL, return | |
250 | if vlc->table != NULL to avoid double allocation */ | |
251 | if(vlc->table) | |
252 | return 0; | |
253 | } | |
254 | ||
de6d9b64 | 255 | #ifdef DEBUG_VLC |
b8a99745 | 256 | av_log(NULL,AV_LOG_DEBUG,"build table nb_codes=%d\n", nb_codes); |
de6d9b64 FB |
257 | #endif |
258 | ||
259 | if (build_table(vlc, nb_bits, nb_codes, | |
260 | bits, bits_wrap, bits_size, | |
261 | codes, codes_wrap, codes_size, | |
b613bacc | 262 | symbols, symbols_wrap, symbols_size, |
d7645fb9 | 263 | 0, 0, flags) < 0) { |
85d366fd | 264 | av_freep(&vlc->table); |
de6d9b64 FB |
265 | return -1; |
266 | } | |
267 | return 0; | |
268 | } | |
269 | ||
270 | ||
271 | void free_vlc(VLC *vlc) | |
272 | { | |
85d366fd | 273 | av_freep(&vlc->table); |
de6d9b64 FB |
274 | } |
275 |