aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/vlc.h
blob: e63c484755aa378774a7f00fbc621d753c8d78a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef AVCODEC_VLC_H
#define AVCODEC_VLC_H

#include <stdint.h>

// When changing this, be sure to also update tableprint_vlc.h accordingly.
typedef int16_t VLCBaseType;

typedef struct VLCElem {
    VLCBaseType sym, len;
} VLCElem;

typedef struct VLC {
    int bits;
    VLCElem *table;
    int table_size, table_allocated;
} VLC;

typedef struct RL_VLC_ELEM {
    int16_t level;
    int8_t len;
    uint8_t run;
} RL_VLC_ELEM;

#define init_vlc(vlc, nb_bits, nb_codes,                \
                 bits, bits_wrap, bits_size,            \
                 codes, codes_wrap, codes_size,         \
                 flags)                                 \
    ff_init_vlc_sparse(vlc, nb_bits, nb_codes,          \
                       bits, bits_wrap, bits_size,      \
                       codes, codes_wrap, codes_size,   \
                       NULL, 0, 0, flags)

int ff_init_vlc_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()
 *
 * 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".
 *
 * @param[in,out] vlc      The VLC to be initialized; table and table_allocated
 *                         must have been set when initializing a static VLC,
 *                         otherwise this will be treated as uninitialized.
 * @param[in] nb_bits      The 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.
 * @param[in] nb_codes     The number of provided length and (if supplied) symbol
 *                         entries.
 * @param[in] lens         The 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.
 * @param[in] lens_wrap    Stride (in bytes) of the lengths.
 * @param[in] symbols      The 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.
 * @param[in] symbols_wrap Stride (in bytes) of the symbols.
 * @param[in] symbols_size Size of the symbols. 1 and 2 are supported.
 * @param[in] offset       An offset to apply to all the valid symbols.
 * @param[in] flags        A combination of the INIT_VLC_* flags; notice that
 *                         INIT_VLC_INPUT_LE is pointless and ignored.
 */
int ff_init_vlc_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);

void ff_free_vlc(VLC *vlc);

/* If INIT_VLC_INPUT_LE is set, the LSB bit of the codes used to
 * initialize the VLC table is the first bit to be read. */
#define INIT_VLC_INPUT_LE       2
/* If set the VLC is intended for a little endian bitstream reader. */
#define INIT_VLC_OUTPUT_LE      8
#define INIT_VLC_LE             (INIT_VLC_INPUT_LE | INIT_VLC_OUTPUT_LE)
#define INIT_VLC_USE_NEW_STATIC 4
#define INIT_VLC_STATIC_OVERLONG (1 | INIT_VLC_USE_NEW_STATIC)

#define INIT_CUSTOM_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g,      \
                                      h, i, j, flags, static_size)         \
    do {                                                                   \
        static VLCElem table[static_size];                                 \
        (vlc)->table           = table;                                    \
        (vlc)->table_allocated = static_size;                              \
        ff_init_vlc_sparse(vlc, bits, a, b, c, d, e, f, g, h, i, j,        \
                           flags | INIT_VLC_USE_NEW_STATIC);               \
    } while (0)

#define INIT_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, h, i, j, static_size) \
    INIT_CUSTOM_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g,          \
                                  h, i, j, 0, static_size)

#define INIT_LE_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, h, i, j, static_size) \
    INIT_CUSTOM_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g,          \
                                  h, i, j, INIT_VLC_LE, static_size)

#define INIT_CUSTOM_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, flags, static_size) \
    INIT_CUSTOM_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g,          \
                                  NULL, 0, 0, flags, static_size)

#define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size)       \
    INIT_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, NULL, 0, 0, static_size)

#define INIT_LE_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
    INIT_LE_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, NULL, 0, 0, static_size)

#define INIT_VLC_STATIC_FROM_LENGTHS(vlc, bits, nb_codes, lens, len_wrap,  \
                                     symbols, symbols_wrap, symbols_size,  \
                                     offset, flags, static_size)           \
    do {                                                                   \
        static VLCElem table[static_size];                                 \
        (vlc)->table           = table;                                    \
        (vlc)->table_allocated = static_size;                              \
        ff_init_vlc_from_lengths(vlc, bits, nb_codes, lens, len_wrap,      \
                                 symbols, symbols_wrap, symbols_size,      \
                                 offset, flags | INIT_VLC_USE_NEW_STATIC,  \
                                 NULL);                                    \
    } while (0)

#endif /* AVCODEC_VLC_H */