2 * This file is part of Libav.
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "bitstream.h"
26 #define VLC_TYPE int16_t
30 VLC_TYPE (*table
)[2]; ///< code, bits
31 int table_size
, table_allocated
;
34 typedef struct RL_VLC_ELEM
{
40 #define init_vlc(vlc, nb_bits, nb_codes, \
41 bits, bits_wrap, bits_size, \
42 codes, codes_wrap, codes_size, \
44 ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
45 bits, bits_wrap, bits_size, \
46 codes, codes_wrap, codes_size, \
49 int ff_init_vlc_sparse(VLC
*vlc
, int nb_bits
, int nb_codes
,
50 const void *bits
, int bits_wrap
, int bits_size
,
51 const void *codes
, int codes_wrap
, int codes_size
,
52 const void *symbols
, int symbols_wrap
, int symbols_size
,
54 void ff_free_vlc(VLC
*vlc
);
57 #define INIT_VLC_USE_NEW_STATIC 4
59 #define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
61 static VLC_TYPE table[static_size][2]; \
62 (vlc)->table = table; \
63 (vlc)->table_allocated = static_size; \
64 init_vlc(vlc, bits, a, b, c, d, e, f, g, INIT_VLC_USE_NEW_STATIC); \
67 /* Return the LUT element for the given bitstream configuration. */
68 static inline int set_idx(BitstreamContext
*bc
, int code
, int *n
, int *nb_bits
,
74 idx
= bitstream_peek(bc
, *nb_bits
) + code
;
82 * @param bits is the number of bits which will be read at once, must be
83 * identical to nb_bits in init_vlc()
84 * @param max_depth is the number of times bits bits must be read to completely
85 * read the longest VLC code
86 * = (max_vlc_length + bits - 1) / bits
87 * If the VLC code is invalid and max_depth = 1, then no bits will be removed.
88 * If the VLC code is invalid and max_depth > 1, then the number of bits removed
90 static inline int bitstream_read_vlc(BitstreamContext
*bc
, VLC_TYPE (*table
)[2],
91 int bits
, int max_depth
)
94 unsigned idx
= bitstream_peek(bc
, bits
);
95 int code
= table
[idx
][0];
96 int n
= table
[idx
][1];
98 if (max_depth
> 1 && n
< 0) {
99 skip_remaining(bc
, bits
);
100 code
= set_idx(bc
, code
, &n
, &nb_bits
, table
);
101 if (max_depth
> 2 && n
< 0) {
102 skip_remaining(bc
, nb_bits
);
103 code
= set_idx(bc
, code
, &n
, &nb_bits
, table
);
106 skip_remaining(bc
, n
);
111 #define BITSTREAM_RL_VLC(level, run, bc, table, bits, max_depth) \
114 unsigned index = bitstream_peek(bc, bits); \
115 level = table[index].level; \
116 n = table[index].len; \
118 if (max_depth > 1 && n < 0) { \
119 bitstream_skip(bc, bits); \
123 index = bitstream_peek(bc, nb_bits) + level; \
124 level = table[index].level; \
125 n = table[index].len; \
126 if (max_depth > 2 && n < 0) { \
127 bitstream_skip(bc, nb_bits); \
130 index = bitstream_peek(bc, nb_bits) + level; \
131 level = table[index].level; \
132 n = table[index].len; \
135 run = table[index].run; \
136 bitstream_skip(bc, n); \
139 #endif /* AVCODEC_VLC_H */