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 #include "ffmpeg_bitstream.h"
32 const uint8_t ff_log2_run
[32]={
33 0, 0, 0, 0, 1, 1, 1, 1,
34 2, 2, 2, 2, 3, 3, 3, 3,
35 4, 4, 5, 5, 6, 6, 7, 7,
36 8, 9,10,11,12,13,14,15
40 * Same as av_mallocz_static(), but does a realloc.
42 * @param[in] ptr The block of memory to reallocate.
43 * @param[in] size The requested size.
44 * @return Block of memory of requested size.
45 * @deprecated. Code which uses ff_realloc_static is broken/misdesigned
46 * and should correctly use static arrays
50 void ff_put_string(PutBitContext
* pbc
, const char *s
, int put_zero
)
64 #define GET_DATA(v, table, i, wrap, size) \
66 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
69 v = *(const uint8_t *)ptr;\
72 v = *(const uint16_t *)ptr;\
75 v = *(const uint32_t *)ptr;\
81 static int alloc_table(VLC
*vlc
, int size
, int use_static
)
84 index
= vlc
->table_size
;
85 vlc
->table_size
+= size
;
86 if (vlc
->table_size
> vlc
->table_allocated
) {
88 DEBUGF("init_vlc() used with too little memory : table_size > allocated_memory\n");
97 static int build_table(VLC
*vlc
, int table_nb_bits
,
99 const void *bits
, int bits_wrap
, int bits_size
,
100 const void *codes
, int codes_wrap
, int codes_size
,
101 const void *symbols
, int symbols_wrap
, int symbols_size
,
102 uint32_t code_prefix
, int n_prefix
, int flags
)
104 int i
, j
, k
, n
, table_size
, table_index
, nb
, n1
, index
, code_prefix2
, symbol
;
106 VLC_TYPE (*table
)[2];
108 table_size
= 1 << table_nb_bits
;
109 table_index
= alloc_table(vlc
, table_size
, flags
& (INIT_VLC_USE_STATIC
|INIT_VLC_USE_NEW_STATIC
));
111 DEBUGF("new table index=%d size=%d code_prefix=%x n=%d\n",
112 table_index
, table_size
, code_prefix
, n_prefix
);
116 table
= &vlc
->table
[table_index
];
118 for(i
=0;i
<table_size
;i
++) {
119 table
[i
][1] = 0; //bits
120 table
[i
][0] = -1; //codes
123 /* first pass: map codes and compute auxillary table sizes */
124 for(i
=0;i
<nb_codes
;i
++) {
125 GET_DATA(n
, bits
, i
, bits_wrap
, bits_size
);
126 GET_DATA(code
, codes
, i
, codes_wrap
, codes_size
);
127 /* we accept tables with holes */
133 GET_DATA(symbol
, symbols
, i
, symbols_wrap
, symbols_size
);
134 #if defined(DEBUG_VLC) && 0
135 DEBUGF("i=%d n=%d code=0x%x\n", i
, n
, code
);
137 /* if code matches the prefix, it is in the table */
139 if(flags
& INIT_VLC_LE
)
140 code_prefix2
= code
& (n_prefix
>=32 ? (int)0xffffffff : (1 << n_prefix
)-1);
142 code_prefix2
= code
>> n
;
143 if (n
> 0 && code_prefix2
== (int)code_prefix
) {
144 if (n
<= table_nb_bits
) {
145 /* no need to add another table */
146 j
= (code
<< (table_nb_bits
- n
)) & (table_size
- 1);
147 nb
= 1 << (table_nb_bits
- n
);
149 if(flags
& INIT_VLC_LE
)
150 j
= (code
>> n_prefix
) + (k
<<n
);
152 DEBUGF("%4x: code=%d n=%d\n",
155 if (table
[j
][1] /*bits*/ != 0) {
156 DEBUGF("incorrect codes\n");
159 table
[j
][1] = n
; //bits
160 table
[j
][0] = symbol
;
165 j
= (code
>> ((flags
& INIT_VLC_LE
) ? n_prefix
: n
)) & ((1 << table_nb_bits
) - 1);
167 DEBUGF("%4x: n=%d (subtable)\n",
170 /* compute table size */
171 n1
= -table
[j
][1]; //bits
174 table
[j
][1] = -n1
; //bits
179 /* second pass : fill auxillary tables recursively */
180 for(i
=0;i
<table_size
;i
++) {
181 n
= table
[i
][1]; //bits
184 if (n
> table_nb_bits
) {
186 table
[i
][1] = -n
; //bits
188 index
= build_table(vlc
, n
, nb_codes
,
189 bits
, bits_wrap
, bits_size
,
190 codes
, codes_wrap
, codes_size
,
191 symbols
, symbols_wrap
, symbols_size
,
192 (flags
& INIT_VLC_LE
) ? (code_prefix
| (i
<< n_prefix
)) : ((code_prefix
<< table_nb_bits
) | i
),
193 n_prefix
+ table_nb_bits
, flags
);
196 /* note: realloc has been done, so reload tables */
197 table
= &vlc
->table
[table_index
];
198 table
[i
][0] = index
; //code
205 /* Build VLC decoding tables suitable for use with get_vlc().
207 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
208 bigger it is, the faster is the decoding. But it should not be too
209 big to save memory and L1 cache. '9' is a good compromise.
211 'nb_codes' : number of vlcs codes
213 'bits' : table which gives the size (in bits) of each vlc code.
215 'codes' : table which gives the bit pattern of of each vlc code.
217 'symbols' : table which gives the values to be returned from get_vlc().
219 'xxx_wrap' : give the number of bytes between each entry of the
220 'bits' or 'codes' tables.
222 'xxx_size' : gives the number of bytes of each entry of the 'bits'
225 'wrap' and 'size' allows to use any memory configuration and types
226 (byte/word/long) to store the 'bits', 'codes', and 'symbols' tables.
228 'use_static' should be set to 1 for tables, which should be freed
229 with av_free_static(), 0 if free_vlc() will be used.
231 int init_vlc_sparse(VLC
*vlc
, int nb_bits
, int nb_codes
,
232 const void *bits
, int bits_wrap
, int bits_size
,
233 const void *codes
, int codes_wrap
, int codes_size
,
234 const void *symbols
, int symbols_wrap
, int symbols_size
,
238 if(flags
& INIT_VLC_USE_NEW_STATIC
){
239 if(vlc
->table_size
&& vlc
->table_size
== vlc
->table_allocated
){
241 }else if(vlc
->table_size
){
242 return -1; // fatal error, we are called on a partially initialized table
244 }else if(!(flags
& INIT_VLC_USE_STATIC
)) {
246 vlc
->table_allocated
= 0;
249 /* Static tables are initially always NULL, return
250 if vlc->table != NULL to avoid double allocation */
256 DEBUGF("build table nb_codes=%d\n", nb_codes
);
259 if (build_table(vlc
, nb_bits
, nb_codes
,
260 bits
, bits_wrap
, bits_size
,
261 codes
, codes_wrap
, codes_size
,
262 symbols
, symbols_wrap
, symbols_size
,
267 /* Changed the following condition to be true if table_size > table_allocated. *
268 * This would be more sensible for static tables since we want warnings for *
269 * memory shortages only. */
271 if((flags
& INIT_VLC_USE_NEW_STATIC
) && vlc
->table_size
> vlc
->table_allocated
)
272 DEBUGF("needed %d had %d\n", vlc
->table_size
, vlc
->table_allocated
);