aarch64: Add assembly support for -fsanitize=hwaddress tagged globals.
[libav.git] / libavcodec / vlc.h
blob8ac52381b936f7f2c5446ed9d697d65e0beca084
1 /*
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
19 #ifndef AVCODEC_VLC_H
20 #define AVCODEC_VLC_H
22 #include <stdint.h>
24 #include "bitstream.h"
26 #define VLC_TYPE int16_t
28 typedef struct VLC {
29 int bits;
30 VLC_TYPE (*table)[2]; ///< code, bits
31 int table_size, table_allocated;
32 } VLC;
34 typedef struct RL_VLC_ELEM {
35 int16_t level;
36 int8_t len;
37 uint8_t run;
38 } RL_VLC_ELEM;
40 #define init_vlc(vlc, nb_bits, nb_codes, \
41 bits, bits_wrap, bits_size, \
42 codes, codes_wrap, codes_size, \
43 flags) \
44 ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
45 bits, bits_wrap, bits_size, \
46 codes, codes_wrap, codes_size, \
47 NULL, 0, 0, flags)
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,
53 int flags);
54 void ff_free_vlc(VLC *vlc);
56 #define INIT_VLC_LE 2
57 #define INIT_VLC_USE_NEW_STATIC 4
59 #define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
60 do { \
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); \
65 } while (0)
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,
69 VLC_TYPE (*table)[2])
71 unsigned idx;
73 *nb_bits = -*n;
74 idx = bitstream_peek(bc, *nb_bits) + code;
75 *n = table[idx][1];
77 return table[idx][0];
80 /**
81 * Parse a VLC 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
89 * is undefined. */
90 static inline int bitstream_read_vlc(BitstreamContext *bc, VLC_TYPE (*table)[2],
91 int bits, int max_depth)
93 int nb_bits;
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);
108 return code;
111 #define BITSTREAM_RL_VLC(level, run, bc, table, bits, max_depth) \
112 do { \
113 int n, nb_bits; \
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); \
121 nb_bits = -n; \
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); \
128 nb_bits = -n; \
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); \
137 } while (0)
139 #endif /* AVCODEC_VLC_H */