2 * Common bit I/O utils.
4 * Extracted 2009 from mplayer 2009-02-10 libavcodec/bitstream.c.
6 * Copyright (c) 2000, 2001 Fabrice Bellard
7 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
8 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
10 * Licensed under the GNU Lesser General Public License.
11 * For licencing details see COPYING.LIB.
15 * \file bitstream.c Bitstream API.
28 #include "bitstream.h"
30 /** Read an 8, 16, or 32 bit entity from a VLC table. */
31 #define GET_DATA(v, table, i, size) \
33 const uint8_t *ptr = (const uint8_t *)table + i * size; \
36 v = *(const uint8_t *)ptr; \
39 v = *(const uint16_t *)ptr; \
42 v = *(const uint32_t *)ptr; \
47 static void alloc_table(struct vlc *vlc, int size)
49 vlc->table_size += size;
50 if (vlc->table_size > vlc->table_allocated) {
51 vlc->table_allocated += (1 << vlc->bits);
52 vlc->table = para_realloc(vlc->table,
53 sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
57 static int build_table(struct vlc *vlc, int table_nb_bits, int nb_codes,
58 const void *bits, const void *codes, int codes_size,
59 uint32_t code_prefix, int n_prefix)
61 int i, j, k, n, table_size, table_index, nb, n1, idx;
65 table_size = 1 << table_nb_bits;
66 table_index = vlc->table_size;
67 alloc_table(vlc, table_size);
68 table = &vlc->table[table_index];
70 for (i = 0; i < table_size; i++) {
71 table[i][1] = 0; /* bits */
72 table[i][0] = -1; /* codes */
75 /* map codes and compute auxiliary table sizes */
76 for (i = 0; i < nb_codes; i++) {
77 GET_DATA(n, bits, i, 1);
78 /* we accept tables with holes */
82 GET_DATA(code, codes, i, codes_size);
83 /* if code matches the prefix, it is in the table */
84 if ((code >> n) != code_prefix)
86 if (n <= table_nb_bits) {
87 /* no need to add another table */
88 j = (code << (table_nb_bits - n)) & (table_size - 1);
89 nb = 1 << (table_nb_bits - n);
90 for (k = 0; k < nb; k++) {
91 assert(table[j][1] == 0); /* incorrect code */
92 table[j][1] = n; /* bits */
98 j = (code >> n) & ((1 << table_nb_bits) - 1);
99 /* compute table size */
100 n1 = -table[j][1]; /* bits */
103 table[j][1] = -n1; /* bits */
107 /* fill auxiliary tables recursively */
108 for (i = 0; i < table_size; i++) {
109 n = table[i][1]; /* bits */
112 if (n > table_nb_bits) {
114 table[i][1] = -n; /* bits */
116 idx = build_table(vlc, n, nb_codes, bits, codes,
117 codes_size, (code_prefix << table_nb_bits) | i,
118 n_prefix + table_nb_bits);
119 /* vlc->table might have changed */
120 table = &vlc->table[table_index];
121 table[i][0] = idx; /* code */
128 * Build VLC decoding tables suitable for use with get_vlc().
130 * \param vlc The structure to be initialized.
132 * \param nb_bits Set the decoding table size (2^nb_bits) entries. The bigger
133 * it is, the faster is the decoding. But it should not be too big to save
134 * memory and L1 cache. '9' is a good compromise.
136 * \param nb_codes Number of vlcs codes.
138 * \param bits Table which gives the size (in bits) of each vlc code.
140 * \param codes Table which gives the bit pattern of of each vlc code.
142 * \param codes_size The number of bytes of each entry of the \a codes tables.
144 * The wrap and size parameters allow to use any memory configuration and
145 * types (byte/word/long) to store the bits and codes tables.
147 void init_vlc(struct vlc *vlc, int nb_bits, int nb_codes, const void *bits,
148 const void *codes, int codes_size)
150 PARA_INFO_LOG("nb_codes: %d\n", nb_codes);
153 vlc->table_allocated = 0;
155 build_table(vlc, nb_bits, nb_codes, bits, codes, codes_size, 0, 0);
159 * Deallocate all resources of a VLC table.
161 * \param vlc Pointer to an initialized vlc structure.
163 * The table given by \a vlc must have been initialized earlier via \ref
166 void free_vlc(struct vlc *vlc)
174 * \param gbc The getbit context structure.
176 * \param table The vlc tables to use.
178 * \param bits The number of bits which will be read at once, must be
179 * identical to nb_bits in init_vlc().
181 * \param max_depth The number of times bits bits must be read to completely
182 * read the longest vlc code = (max_vlc_length + bits - 1) / bits.
184 * \return The vlc code.
186 int get_vlc(struct getbit_context *gbc, VLC_TYPE(*table)[2], int bits,
189 int n, idx, nb_bits, code;
191 idx = show_bits(gbc, bits);
192 code = table[idx][0];
194 if (max_depth > 1 && n < 0) {
195 skip_bits(gbc, bits);
197 idx = show_bits(gbc, nb_bits) + code;
198 code = table[idx][0];
200 if (max_depth > 2 && n < 0) {
201 skip_bits(gbc, nb_bits);
203 idx = show_bits(gbc, nb_bits) + code;
204 code = table[idx][0];
209 return code >= 0? code : -E_VLC;