1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * This file is part of FFmpeg.
3cabdff1aSopenharmony_ci *
4cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
5cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
6cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
7cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version.
8cabdff1aSopenharmony_ci *
9cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
10cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
11cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12cabdff1aSopenharmony_ci * Lesser General Public License for more details.
13cabdff1aSopenharmony_ci *
14cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
15cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
16cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17cabdff1aSopenharmony_ci */
18cabdff1aSopenharmony_ci
19cabdff1aSopenharmony_ci#ifndef AVCODEC_VLC_H
20cabdff1aSopenharmony_ci#define AVCODEC_VLC_H
21cabdff1aSopenharmony_ci
22cabdff1aSopenharmony_ci#include <stdint.h>
23cabdff1aSopenharmony_ci
24cabdff1aSopenharmony_ci// When changing this, be sure to also update tableprint_vlc.h accordingly.
25cabdff1aSopenharmony_citypedef int16_t VLCBaseType;
26cabdff1aSopenharmony_ci
27cabdff1aSopenharmony_citypedef struct VLCElem {
28cabdff1aSopenharmony_ci    VLCBaseType sym, len;
29cabdff1aSopenharmony_ci} VLCElem;
30cabdff1aSopenharmony_ci
31cabdff1aSopenharmony_citypedef struct VLC {
32cabdff1aSopenharmony_ci    int bits;
33cabdff1aSopenharmony_ci    VLCElem *table;
34cabdff1aSopenharmony_ci    int table_size, table_allocated;
35cabdff1aSopenharmony_ci} VLC;
36cabdff1aSopenharmony_ci
37cabdff1aSopenharmony_citypedef struct RL_VLC_ELEM {
38cabdff1aSopenharmony_ci    int16_t level;
39cabdff1aSopenharmony_ci    int8_t len;
40cabdff1aSopenharmony_ci    uint8_t run;
41cabdff1aSopenharmony_ci} RL_VLC_ELEM;
42cabdff1aSopenharmony_ci
43cabdff1aSopenharmony_ci#define init_vlc(vlc, nb_bits, nb_codes,                \
44cabdff1aSopenharmony_ci                 bits, bits_wrap, bits_size,            \
45cabdff1aSopenharmony_ci                 codes, codes_wrap, codes_size,         \
46cabdff1aSopenharmony_ci                 flags)                                 \
47cabdff1aSopenharmony_ci    ff_init_vlc_sparse(vlc, nb_bits, nb_codes,          \
48cabdff1aSopenharmony_ci                       bits, bits_wrap, bits_size,      \
49cabdff1aSopenharmony_ci                       codes, codes_wrap, codes_size,   \
50cabdff1aSopenharmony_ci                       NULL, 0, 0, flags)
51cabdff1aSopenharmony_ci
52cabdff1aSopenharmony_ciint ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
53cabdff1aSopenharmony_ci                       const void *bits, int bits_wrap, int bits_size,
54cabdff1aSopenharmony_ci                       const void *codes, int codes_wrap, int codes_size,
55cabdff1aSopenharmony_ci                       const void *symbols, int symbols_wrap, int symbols_size,
56cabdff1aSopenharmony_ci                       int flags);
57cabdff1aSopenharmony_ci
58cabdff1aSopenharmony_ci/**
59cabdff1aSopenharmony_ci * Build VLC decoding tables suitable for use with get_vlc2()
60cabdff1aSopenharmony_ci *
61cabdff1aSopenharmony_ci * This function takes lengths and symbols and calculates the codes from them.
62cabdff1aSopenharmony_ci * For this the input lengths and symbols have to be sorted according to "left
63cabdff1aSopenharmony_ci * nodes in the corresponding tree first".
64cabdff1aSopenharmony_ci *
65cabdff1aSopenharmony_ci * @param[in,out] vlc      The VLC to be initialized; table and table_allocated
66cabdff1aSopenharmony_ci *                         must have been set when initializing a static VLC,
67cabdff1aSopenharmony_ci *                         otherwise this will be treated as uninitialized.
68cabdff1aSopenharmony_ci * @param[in] nb_bits      The number of bits to use for the VLC table;
69cabdff1aSopenharmony_ci *                         higher values take up more memory and cache, but
70cabdff1aSopenharmony_ci *                         allow to read codes with fewer reads.
71cabdff1aSopenharmony_ci * @param[in] nb_codes     The number of provided length and (if supplied) symbol
72cabdff1aSopenharmony_ci *                         entries.
73cabdff1aSopenharmony_ci * @param[in] lens         The lengths of the codes. Entries > 0 correspond to
74cabdff1aSopenharmony_ci *                         valid codes; entries == 0 will be skipped and entries
75cabdff1aSopenharmony_ci *                         with len < 0 indicate that the tree is incomplete and
76cabdff1aSopenharmony_ci *                         has an open end of length -len at this position.
77cabdff1aSopenharmony_ci * @param[in] lens_wrap    Stride (in bytes) of the lengths.
78cabdff1aSopenharmony_ci * @param[in] symbols      The symbols, i.e. what is returned from get_vlc2()
79cabdff1aSopenharmony_ci *                         when the corresponding code is encountered.
80cabdff1aSopenharmony_ci *                         May be NULL, then 0, 1, 2, 3, 4,... will be used.
81cabdff1aSopenharmony_ci * @param[in] symbols_wrap Stride (in bytes) of the symbols.
82cabdff1aSopenharmony_ci * @param[in] symbols_size Size of the symbols. 1 and 2 are supported.
83cabdff1aSopenharmony_ci * @param[in] offset       An offset to apply to all the valid symbols.
84cabdff1aSopenharmony_ci * @param[in] flags        A combination of the INIT_VLC_* flags; notice that
85cabdff1aSopenharmony_ci *                         INIT_VLC_INPUT_LE is pointless and ignored.
86cabdff1aSopenharmony_ci */
87cabdff1aSopenharmony_ciint ff_init_vlc_from_lengths(VLC *vlc, int nb_bits, int nb_codes,
88cabdff1aSopenharmony_ci                             const int8_t *lens, int lens_wrap,
89cabdff1aSopenharmony_ci                             const void *symbols, int symbols_wrap, int symbols_size,
90cabdff1aSopenharmony_ci                             int offset, int flags, void *logctx);
91cabdff1aSopenharmony_ci
92cabdff1aSopenharmony_civoid ff_free_vlc(VLC *vlc);
93cabdff1aSopenharmony_ci
94cabdff1aSopenharmony_ci/* If INIT_VLC_INPUT_LE is set, the LSB bit of the codes used to
95cabdff1aSopenharmony_ci * initialize the VLC table is the first bit to be read. */
96cabdff1aSopenharmony_ci#define INIT_VLC_INPUT_LE       2
97cabdff1aSopenharmony_ci/* If set the VLC is intended for a little endian bitstream reader. */
98cabdff1aSopenharmony_ci#define INIT_VLC_OUTPUT_LE      8
99cabdff1aSopenharmony_ci#define INIT_VLC_LE             (INIT_VLC_INPUT_LE | INIT_VLC_OUTPUT_LE)
100cabdff1aSopenharmony_ci#define INIT_VLC_USE_NEW_STATIC 4
101cabdff1aSopenharmony_ci#define INIT_VLC_STATIC_OVERLONG (1 | INIT_VLC_USE_NEW_STATIC)
102cabdff1aSopenharmony_ci
103cabdff1aSopenharmony_ci#define INIT_CUSTOM_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g,      \
104cabdff1aSopenharmony_ci                                      h, i, j, flags, static_size)         \
105cabdff1aSopenharmony_ci    do {                                                                   \
106cabdff1aSopenharmony_ci        static VLCElem table[static_size];                                 \
107cabdff1aSopenharmony_ci        (vlc)->table           = table;                                    \
108cabdff1aSopenharmony_ci        (vlc)->table_allocated = static_size;                              \
109cabdff1aSopenharmony_ci        ff_init_vlc_sparse(vlc, bits, a, b, c, d, e, f, g, h, i, j,        \
110cabdff1aSopenharmony_ci                           flags | INIT_VLC_USE_NEW_STATIC);               \
111cabdff1aSopenharmony_ci    } while (0)
112cabdff1aSopenharmony_ci
113cabdff1aSopenharmony_ci#define INIT_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, h, i, j, static_size) \
114cabdff1aSopenharmony_ci    INIT_CUSTOM_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g,          \
115cabdff1aSopenharmony_ci                                  h, i, j, 0, static_size)
116cabdff1aSopenharmony_ci
117cabdff1aSopenharmony_ci#define INIT_LE_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, h, i, j, static_size) \
118cabdff1aSopenharmony_ci    INIT_CUSTOM_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g,          \
119cabdff1aSopenharmony_ci                                  h, i, j, INIT_VLC_LE, static_size)
120cabdff1aSopenharmony_ci
121cabdff1aSopenharmony_ci#define INIT_CUSTOM_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, flags, static_size) \
122cabdff1aSopenharmony_ci    INIT_CUSTOM_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g,          \
123cabdff1aSopenharmony_ci                                  NULL, 0, 0, flags, static_size)
124cabdff1aSopenharmony_ci
125cabdff1aSopenharmony_ci#define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size)       \
126cabdff1aSopenharmony_ci    INIT_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, NULL, 0, 0, static_size)
127cabdff1aSopenharmony_ci
128cabdff1aSopenharmony_ci#define INIT_LE_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
129cabdff1aSopenharmony_ci    INIT_LE_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, NULL, 0, 0, static_size)
130cabdff1aSopenharmony_ci
131cabdff1aSopenharmony_ci#define INIT_VLC_STATIC_FROM_LENGTHS(vlc, bits, nb_codes, lens, len_wrap,  \
132cabdff1aSopenharmony_ci                                     symbols, symbols_wrap, symbols_size,  \
133cabdff1aSopenharmony_ci                                     offset, flags, static_size)           \
134cabdff1aSopenharmony_ci    do {                                                                   \
135cabdff1aSopenharmony_ci        static VLCElem table[static_size];                                 \
136cabdff1aSopenharmony_ci        (vlc)->table           = table;                                    \
137cabdff1aSopenharmony_ci        (vlc)->table_allocated = static_size;                              \
138cabdff1aSopenharmony_ci        ff_init_vlc_from_lengths(vlc, bits, nb_codes, lens, len_wrap,      \
139cabdff1aSopenharmony_ci                                 symbols, symbols_wrap, symbols_size,      \
140cabdff1aSopenharmony_ci                                 offset, flags | INIT_VLC_USE_NEW_STATIC,  \
141cabdff1aSopenharmony_ci                                 NULL);                                    \
142cabdff1aSopenharmony_ci    } while (0)
143cabdff1aSopenharmony_ci
144cabdff1aSopenharmony_ci#endif /* AVCODEC_VLC_H */
145