41#define GET_DATA(v, table, i, wrap, size) \
43 const uint8_t *ptr = (const uint8_t *)table + i * wrap; \
46 av_unreachable("Only uint8/16/32_t are used"); \
48 v = *(const uint8_t *)ptr; \
51 v = *(const uint16_t *)ptr; \
54 v = *(const uint32_t *)ptr; \
80#define LOCALBUF_ELEMS 1500
121 return (sa->
code >> 1) - (sb->code >> 1);
141 int table_size, table_index;
144 if (table_nb_bits > 30)
146 table_size = 1 << table_nb_bits;
148 ff_dlog(
NULL,
"new table index=%d size=%d\n", table_index, table_size);
154 for (
int i = 0;
i < nb_codes;
i++) {
155 int n = codes[
i].
bits;
159 if (n <= table_nb_bits) {
161 int j =
code >> (32 - table_nb_bits);
162 int nb = 1 << (table_nb_bits - n);
169 for (
int k = 0; k < nb; k++) {
171 int oldsym =
table[j].sym;
173 if ((
bits || oldsym) && (
bits != n || oldsym != symbol)) {
178 table[j].sym = symbol;
184 int index, subtable_bits, j, k;
191 for (k =
i + 1; k < nb_codes; k++) {
192 n = codes[k].
bits - table_nb_bits;
199 codes[k].
code =
code << table_nb_bits;
200 subtable_bits =
FFMAX(subtable_bits, n);
202 subtable_bits =
FFMIN(subtable_bits, table_nb_bits);
204 table[j].len = -subtable_bits;
206 j, codes[
i].
bits + table_nb_bits);
221 for (
int i = 0;
i < table_size;
i++) {
240 if (codes != localbuf)
252 const void *codes,
int codes_wrap,
int codes_size,
253 const void *symbols,
int symbols_wrap,
int symbols_size,
265#define COPY(condition)\
266 for (int i = 0; i < nb_codes; i++) { \
268 GET_DATA(len, bits, i, bits_wrap, bits_size); \
271 if (len > 3*nb_bits || len > 32) { \
272 av_log(NULL, AV_LOG_ERROR, "Too long VLC (%u) in vlc_init\n", len);\
273 if (buf != localbuf) \
275 return AVERROR(EINVAL); \
278 GET_DATA(buf[j].code, codes, i, codes_wrap, codes_size); \
279 if (buf[j].code >= (1LL<<buf[j].bits)) { \
280 av_log(NULL, AV_LOG_ERROR, "Invalid code %"PRIx32" for %d in " \
281 "vlc_init\n", buf[j].code, i); \
282 if (buf != localbuf) \
284 return AVERROR(EINVAL); \
286 if (flags & VLC_INIT_INPUT_LE) \
287 buf[j].code = bitswap_32(buf[j].code); \
289 buf[j].code <<= 32 - buf[j].bits; \
291 GET_DATA(buf[j].symbol, symbols, i, symbols_wrap, symbols_size) \
307 const int8_t *lens,
int lens_wrap,
308 const void *symbols,
int symbols_wrap,
int symbols_size,
313 int ret, j, len_max =
FFMIN(32, 3 * nb_bits);
320 for (
int i = 0;
i < nb_codes;
i++, lens += lens_wrap) {
327 GET_DATA(sym, symbols,
i, symbols_wrap, symbols_size)
332 }
else if (
len < 0) {
336 if (
len > len_max ||
code & ((1U << (32 -
len)) - 1)) {
341 if (
code > UINT32_MAX + 1ULL) {
354 int nb_bits,
int nb_codes,
355 const int8_t *lens,
int lens_wrap,
356 const void *symbols,
int symbols_wrap,
int symbols_size,
359 VLC vlc = { .table =
table, .table_allocated = table_size };
362 symbols, symbols_wrap, symbols_size,
367 int nb_bits,
int nb_codes,
368 const int8_t *lens,
int lens_wrap,
369 const void *symbols,
int symbols_wrap,
int symbols_size,
372 VLC vlc = { .table =
state->table, .table_allocated =
state->size };
375 symbols, symbols_wrap, symbols_size,
385 int nb_bits,
int nb_codes,
387 const void *codes,
int codes_wrap,
int codes_size,
388 const void *symbols,
int symbols_wrap,
int symbols_size,
391 VLC vlc = { .table =
table, .table_allocated = table_size };
395 codes, codes_wrap, codes_size,
396 symbols, symbols_wrap, symbols_size,
401 int nb_bits,
int nb_codes,
403 const void *codes,
int codes_wrap,
int codes_size,
404 const void *symbols,
int symbols_wrap,
int symbols_size,
407 VLC vlc = { .table =
state->table, .table_allocated =
state->size };
411 codes, codes_wrap, codes_size,
412 symbols, symbols_wrap, symbols_size,
422 const int num,
const int numbits,
424 uint32_t curcode,
int curlen,
425 int curlimit,
int curlevel,
426 const int minlen,
const int max,
430 for (
int i = num-1;
i >=
max;
i--) {
431 for (
int j = 0; j < 2; j++) {
440 code = curcode + (buf[t].
code >> curlen);
441 newlimit = curlimit - l;
443 if (is16bit) info.
val16[curlevel] = sym;
444 else info.
val8[curlevel] = sym&0xFF;
447 uint32_t
val =
code >> (32 - numbits);
448 uint32_t nb =
val + (1U << (numbits - l));
450 info.
num = curlevel+1;
453 levelcnt[curlevel-1]++;
456 if (curlevel+1 < max_symbols && newlimit >= minlen) {
458 code, l, newlimit, curlevel+1,
459 minlen,
max, levelcnt, info);
466 const int is16bit,
const int nb_codes,
const int numbits,
469 int minbits, maxbits,
max;
474 for (
int j = 0; j < 1<<numbits; j++) {
477 j += (1 << (numbits - single->
table[j].
len)) - 1;
484 for (
int n = nb_codes - count0; n < nb_codes; n++) {
490 for (
max = nb_codes;
max > nb_codes - count0;
max--) {
495 if (buf[
max - 1].
bits+minbits > numbits)
499 for (
int j = 0; j < 1<<numbits; j++) {
509 0, 0,
FFMIN(maxbits, numbits), 0, minbits,
max, count, info);
512 count[0], count[1], count[2], count[3], count[4], minbits,
max);
518 int nb_codes,
const int8_t *lens,
int lens_wrap,
519 const void *symbols,
int symbols_wrap,
int symbols_size,
524 int ret, j, len_max =
FFMIN(32, 3 * nb_bits);
535 for (
int i = 0;
i < nb_codes;
i++, lens += lens_wrap) {
542 GET_DATA(sym, symbols,
i, symbols_wrap, symbols_size)
547 }
else if (
len < 0) {
551 if (
len > len_max ||
code & ((1U << (32 -
len)) - 1)) {
556 if (
code > UINT32_MAX + 1ULL) {
static double val(void *priv, double ch)
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
static struct @346255127015250356166251341105367306144006377143 state
static const uint8_t bits[8]
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Macro definitions for various function/variable attributes.
common internal API header
const uint8_t ff_reverse[256]
Utility Preprocessor macros.
Memory handling functions.
static int inc(int num, int period)
static const uint16_t table[]
static const unsigned code_prefix[]
#define AV_QSORT(p, num, type, cmp)
Quicksort This sort is fast, and fully inplace but not stable and it is possible to construct input t...
For static VLCs, the number of bits can often be hardcoded at each get_vlc2() callsite.
uint16_t val16[VLC_MULTI_MAX_SYMBOLS/2]
uint8_t val8[VLC_MULTI_MAX_SYMBOLS]
uint32_t code
codeword, with the first bit-to-be-read in the msb (even if intended for a little-endian bitstream re...
#define av_malloc_array(a, b)
#define av_realloc_f(p, o, n)
#define avpriv_request_sample(...)
#define GET_DATA(v, table, i, wrap, size)
av_cold void ff_vlc_init_table_from_lengths(VLCElem table[], int table_size, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags)
static void add_level(VLC_MULTI_ELEM *table, const int is16bit, const int num, const int numbits, const VLCcode *buf, uint32_t curcode, int curlen, int curlimit, int curlevel, const int minlen, const int max, unsigned *levelcnt, VLC_MULTI_ELEM info)
int ff_vlc_init_multi_from_lengths(VLC *vlc, VLC_MULTI *multi, int nb_bits, int nb_elems, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags, void *logctx)
Build VLC decoding tables suitable for use with get_vlc_multi()
int ff_vlc_init_sparse(VLC *vlc, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
Build VLC decoding tables suitable for use with get_vlc2().
static int vlc_multi_gen(VLC_MULTI_ELEM *table, const VLC *single, const int is16bit, const int nb_codes, const int numbits, VLCcode *buf, void *logctx)
void ff_vlc_free_multi(VLC_MULTI *vlc)
av_cold void ff_vlc_init_table_sparse(VLCElem table[], int table_size, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
static int vlc_common_init(VLC *vlc, int nb_bits, int nb_codes, VLCcode **buf, int flags)
static int alloc_table(VLC *vlc, int size, int use_static)
int ff_vlc_init_from_lengths(VLC *vlc, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
av_cold const VLCElem * ff_vlc_init_tables_from_lengths(VLCInitState *state, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags)
static int compare_vlcspec(const void *a, const void *b)
void ff_vlc_free(VLC *vlc)
static int build_table(VLC *vlc, int table_nb_bits, int nb_codes, VLCcode *codes, int flags)
Build VLC decoding tables suitable for use with get_vlc().
static int vlc_common_end(VLC *vlc, int nb_bits, int nb_codes, VLCcode *codes, int flags, VLCcode localbuf[LOCALBUF_ELEMS])
static av_always_inline uint32_t bitswap_32(uint32_t x)
av_cold const VLCElem * ff_vlc_init_tables_sparse(VLCInitState *state, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
#define VLC_INIT_OUTPUT_LE
#define VLC_INIT_USE_STATIC
#define VLC_INIT_STATIC_OVERLONG
#define VLC_MULTI_MAX_SYMBOLS