FFmpeg
Loading...
Searching...
No Matches
vlc.c File Reference
#include <inttypes.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "libavutil/attributes.h"
#include "libavutil/avassert.h"
#include "libavutil/error.h"
#include "libavutil/internal.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/log.h"
#include "libavutil/macros.h"
#include "libavutil/mem.h"
#include "libavutil/qsort.h"
#include "libavutil/reverse.h"
#include "vlc.h"

Go to the source code of this file.

Data Structures

struct  VLCcode
 

Macros

#define GET_DATA(v, table, i, wrap, size)
 
#define LOCALBUF_ELEMS   1500
 
#define COPY(condition)
 

Functions

static int alloc_table (VLC *vlc, int size, int use_static)
 
static av_always_inline uint32_t bitswap_32 (uint32_t x)
 
static int vlc_common_init (VLC *vlc, int nb_bits, int nb_codes, VLCcode **buf, int flags)
 
static int compare_vlcspec (const void *a, const void *b)
 
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])
 
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().
 
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 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)
 
av_cold const VLCElemff_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)
 
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)
 
av_cold const VLCElemff_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)
 
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)
 
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)
 
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()
 
void ff_vlc_free_multi (VLC_MULTI *vlc)
 
void ff_vlc_free (VLC *vlc)
 

Macro Definition Documentation

◆ GET_DATA

#define GET_DATA ( v,
table,
i,
wrap,
size )
Value:
{ \
const uint8_t *ptr = (const uint8_t *)table + i * wrap; \
switch(size) { \
default: \
av_unreachable("Only uint8/16/32_t are used"); \
case 1: \
v = *(const uint8_t *)ptr; \
break; \
case 2: \
v = *(const uint16_t *)ptr; \
break; \
case 4: \
v = *(const uint32_t *)ptr; \
break; \
} \
}
#define wrap(func)
Definition neontest.h:65
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static const uint16_t table[]
Definition prosumer.c:203
int size

Definition at line 41 of file vlc.c.

Referenced by ff_vlc_init_from_lengths(), and ff_vlc_init_multi_from_lengths().

◆ LOCALBUF_ELEMS

#define LOCALBUF_ELEMS   1500

◆ COPY

#define COPY ( condition)
Value:
for (int i = 0; i < nb_codes; i++) { \
unsigned len; \
GET_DATA(len, bits, i, bits_wrap, bits_size); \
if (!(condition)) \
continue; \
if (len > 3*nb_bits || len > 32) { \
av_log(NULL, AV_LOG_ERROR, "Too long VLC (%u) in vlc_init\n", len);\
if (buf != localbuf) \
av_free(buf); \
return AVERROR(EINVAL); \
} \
buf[j].bits = len; \
GET_DATA(buf[j].code, codes, i, codes_wrap, codes_size); \
if (buf[j].code >= (1LL<<buf[j].bits)) { \
av_log(NULL, AV_LOG_ERROR, "Invalid code %"PRIx32" for %d in " \
"vlc_init\n", buf[j].code, i); \
if (buf != localbuf) \
av_free(buf); \
return AVERROR(EINVAL); \
} \
buf[j].code = bitswap_32(buf[j].code); \
else \
buf[j].code <<= 32 - buf[j].bits; \
if (symbols) \
GET_DATA(buf[j].symbol, symbols, i, symbols_wrap, symbols_size) \
else \
buf[j].symbol = i; \
j++; \
}
#define bits_size
Definition bitstream.h:114
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define NULL
Definition coverity.c:32
static const uint8_t bits[8]
Definition fastaudio.c:100
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const uint8_t * code
Definition spdifenc.c:433
static av_always_inline uint32_t bitswap_32(uint32_t x)
Definition vlc.c:82
#define VLC_INIT_INPUT_LE
Definition vlc.h:194
int len

Referenced by ff_vlc_init_sparse().

Function Documentation

◆ alloc_table()

static int alloc_table ( VLC * vlc,
int size,
int use_static )
static

Definition at line 60 of file vlc.c.

Referenced by build_table(), and mp_decode_layer2().

◆ bitswap_32()

static av_always_inline uint32_t bitswap_32 ( uint32_t x)
static

Definition at line 82 of file vlc.c.

Referenced by build_table().

◆ vlc_common_init()

static int vlc_common_init ( VLC * vlc,
int nb_bits,
int nb_codes,
VLCcode ** buf,
int flags )
static

◆ compare_vlcspec()

static int compare_vlcspec ( const void * a,
const void * b )
static

Definition at line 118 of file vlc.c.

Referenced by ff_vlc_init_sparse().

◆ build_table()

static int build_table ( VLC * vlc,
int table_nb_bits,
int nb_codes,
VLCcode * codes,
int flags )
static

Build VLC decoding tables suitable for use with get_vlc().

Parameters
vlcthe context to be initialized
table_nb_bitsmax length of vlc codes to store directly in this table (Longer codes are delegated to subtables.)
nb_codesnumber of elements in codes[]
codesdescriptions of the vlc codes These must be ordered such that codes going into the same subtable are contiguous. Sorting by VLCcode.code is sufficient, though not necessary.

Definition at line 138 of file vlc.c.

Referenced by build_table(), and vlc_common_end().

◆ vlc_common_end()

static int vlc_common_end ( VLC * vlc,
int nb_bits,
int nb_codes,
VLCcode * codes,
int flags,
VLCcode localbuf[LOCALBUF_ELEMS] )
static

◆ ff_vlc_init_sparse()

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().

Parameters
[in,out]vlcThe VLC to be initialized; table and table_allocated must have been set when initializing a static VLC, otherwise this will be treated as uninitialized.
[in]nb_bitsThe number of bits to use for the VLC table; higher values take up more memory and cache, but allow to read codes with fewer reads. Corresponds to the bits parameter of get_vlc2().
[in]nb_codesThe number of provided bits, codes and (if supplied) symbol entries.
[in]bitsThe lengths (in bits) of the codes. Entries > 0 correspond to valid codes; entries == 0 will be skipped.
[in]bits_wrapStride (in bytes) of the bits table.
[in]codes_sizeSize of the bits. 1, 2 and 4 are supported.
[in]codesTable which gives the bit pattern of of each vlc code.
[in]codes_wrapStride (in bytes) of the codes table.
[in]codes_sizeSize of the codes. 1, 2 and 4 are supported.
[in]symbolsThe symbols, i.e. what is returned from get_vlc2() when the corresponding code is encountered. May be NULL, then 0, 1, 2, 3, 4,... will be used.
[in]symbols_wrapStride (in bytes) of the symbols table.
[in]symbols_sizeSize of the symbols. 1 and 2 are supported.
[in]flagsA combination of the VLC_INIT_* flags.

'wrap' and 'size' make it possible to use any memory configuration and types (byte/word/int) to store the 'bits', 'codes', and 'symbols' tables.

Definition at line 250 of file vlc.c.

Referenced by build_huff(), build_vlc(), build_vlc(), dnxhd_init_vlc(), ff_vlc_init_table_sparse(), ff_vlc_init_tables_sparse(), generate_joint_tables(), huf_build_dec_table(), init_ralf_vlc(), read_hufftable(), and rv34_gen_vlc_ext().

◆ ff_vlc_init_from_lengths()

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()

This function takes lengths and symbols and calculates the codes from them. For this the input lengths and symbols have to be sorted according to "left nodes in the corresponding tree first".

Parameters
[in,out]vlcThe VLC to be initialized; table and table_allocated must have been set when initializing a static VLC, otherwise this will be treated as uninitialized.
[in]nb_bitsThe number of bits to use for the VLC table; higher values take up more memory and cache, but allow to read codes with fewer reads.
[in]nb_codesThe number of provided length and (if supplied) symbol entries.
[in]lensThe lengths of the codes. Entries > 0 correspond to valid codes; entries == 0 will be skipped and entries with len < 0 indicate that the tree is incomplete and has an open end of length -len at this position.
[in]lens_wrapStride (in bytes) of the lengths.
[in]symbolsThe symbols, i.e. what is returned from get_vlc2() when the corresponding code is encountered. May be NULL, then 0, 1, 2, 3, 4,... will be used.
[in]symbols_wrapStride (in bytes) of the symbols.
[in]symbols_sizeSize of the symbols. 1 and 2 are supported.
[in]offsetAn offset to apply to all the valid symbols.
[in]flagsA combination of the VLC_INIT_* flags; notice that VLC_INIT_INPUT_LE is pointless and ignored.

Definition at line 306 of file vlc.c.

Referenced by atrac3_init_static_data(), build_canonical_huff(), build_huff_tree(), build_vlc(), build_vlc(), build_vlc(), build_vlc(), cfhd_init_vlc(), dv_init_static(), ff_mjpeg_build_vlc(), ff_vlc_init_table_from_lengths(), ff_vlc_init_tables_from_lengths(), huff_reader_build_canonical(), mp_decode_frame(), mss4_init_vlc(), on2avc_decode_init(), qdmc_init_static_data(), read_code_table(), read_simple_vlc_prefix(), read_vlc_prefix(), smacker_decode_header_tree(), smka_decode_frame(), tm2_build_huff_table(), tscc2_init_vlc(), vp3_decode_init(), and wma_decode_init().

◆ ff_vlc_init_table_from_lengths()

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 )

Definition at line 353 of file vlc.c.

Referenced by rv10_build_vlc().

◆ ff_vlc_init_tables_from_lengths()

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 )

◆ ff_vlc_init_table_sparse()

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 )

Definition at line 384 of file vlc.c.

Referenced by ff_init_2d_vlc_rl(), ff_rl_init_vlc(), and init_vlcs().

◆ ff_vlc_init_tables_sparse()

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 )

◆ add_level()

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 )
static

Definition at line 421 of file vlc.c.

Referenced by add_level(), and vlc_multi_gen().

◆ vlc_multi_gen()

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 )
static

Definition at line 465 of file vlc.c.

Referenced by ff_vlc_init_multi_from_lengths().

◆ ff_vlc_init_multi_from_lengths()

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()

This function takes lengths and symbols and calculates the codes from them. For this the input lengths and symbols have to be sorted according to "left nodes in the corresponding tree first".

Parameters
[in,out]vlcThe VLC to be initialized; table and table_allocated must have been set when initializing a static VLC, otherwise this will be treated as uninitialized.
[in,out]multiThe VLC_MULTI to be initialized; table and table_allocated must have been set when initializing a static VLC, otherwise this will be treated as uninitialized.
[in]nb_bitsThe number of bits to use for the VLC table; higher values take up more memory and cache, but allow to read codes with fewer reads.
[in]nb_elemsThe max possible number of elements.
[in]nb_codesThe number of provided length and (if supplied) symbol entries.
[in]lensThe lengths of the codes. Entries > 0 correspond to valid codes; entries == 0 will be skipped and entries with len < 0 indicate that the tree is incomplete and has an open end of length -len at this position.
[in]lens_wrapStride (in bytes) of the lengths.
[in]symbolsThe symbols, i.e. what is returned from get_vlc2() when the corresponding code is encountered. May be NULL, then 0, 1, 2, 3, 4,... will be used.
[in]symbols_wrapStride (in bytes) of the symbols.
[in]symbols_sizeSize of the symbols. 1 and 2 are supported.
[in]offsetAn offset to apply to all the valid symbols.
[in]flagsA combination of the VLC_INIT_* flags; notice that VLC_INIT_INPUT_LE is pointless and ignored.

Definition at line 517 of file vlc.c.

Referenced by build_huff(), and huff_build().

◆ ff_vlc_free_multi()

void ff_vlc_free_multi ( VLC_MULTI * vlc)

◆ ff_vlc_free()