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
31 #include "bitstream.h"
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);
49 put_bits(s
,s
->bit_left
& 7,0);
53 void ff_put_string(PutBitContext
* pbc
, const char *s
, int put_zero
)
63 void ff_copy_bits(PutBitContext
*pb
, const uint8_t *src
, int length
)
65 const uint16_t *srcw
= (const uint16_t*)src
;
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
]));
75 for(i
=0; put_bits_count(pb
)&31; i
++)
76 put_bits(pb
, 8, src
[i
]);
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
));
89 #define GET_DATA(v, table, i, wrap, size) \
91 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
94 v = *(const uint8_t *)ptr;\
97 v = *(const uint16_t *)ptr;\
100 v = *(const uint32_t *)ptr;\
106 static int alloc_table(VLC
*vlc
, int size
, int use_static
)
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
);
114 vlc
->table
= ff_realloc_static(vlc
->table
,
115 sizeof(VLC_TYPE
) * 2 * vlc
->table_allocated
);
117 vlc
->table
= av_realloc(vlc
->table
,
118 sizeof(VLC_TYPE
) * 2 * vlc
->table_allocated
);
125 static int build_table(VLC
*vlc
, int table_nb_bits
,
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
;
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
);
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
);
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 */
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
);
165 /* if code matches the prefix, it is in the table */
167 if(flags
& INIT_VLC_LE
)
168 code_prefix2
= code
& (n_prefix
>=32 ? 0xffffffff : (1 << n_prefix
)-1);
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
);
177 if(flags
& INIT_VLC_LE
)
178 j
= (code
>> n_prefix
) + (k
<<n
);
180 av_log(NULL
, AV_LOG_DEBUG
, "%4x: code=%d n=%d\n",
183 if (table
[j
][1] /*bits*/ != 0) {
184 av_log(NULL
, AV_LOG_ERROR
, "incorrect codes\n");
187 table
[j
][1] = n
; //bits
188 table
[j
][0] = symbol
;
193 j
= (code
>> ((flags
& INIT_VLC_LE
) ? n_prefix
: n
)) & ((1 << table_nb_bits
) - 1);
195 av_log(NULL
,AV_LOG_DEBUG
,"%4x: n=%d (subtable)\n",
198 /* compute table size */
199 n1
= -table
[j
][1]; //bits
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
212 if (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
);
224 /* note: realloc has been done, so reload tables */
225 table
= &vlc
->table
[table_index
];
226 table
[i
][0] = index
; //code
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'
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
,
266 if(!(flags
& INIT_VLC_USE_STATIC
)) {
268 vlc
->table_allocated
= 0;
271 /* Static tables are initially always NULL, return
272 if vlc->table != NULL to avoid double allocation */
278 av_log(NULL
,AV_LOG_DEBUG
,"build table nb_codes=%d\n", nb_codes
);
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
,
286 av_freep(&vlc
->table
);
293 void free_vlc(VLC
*vlc
)
295 av_freep(&vlc
->table
);