Merge branch 'maint'
[paraslash.git] / bitstream.c
1 /*
2 * Common bit I/O utils.
3 *
4 * Extracted 2009 from mplayer 2009-02-10 libavcodec/bitstream.c.
5 *
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>
9 *
10 * Licensed under the GNU Lesser General Public License.
11 * For licencing details see COPYING.LIB.
12 */
13
14 /** \file bitstream.c Bitstream API for the wma decoder. */
15
16 #include <regex.h>
17
18 #include "para.h"
19 #include "error.h"
20 #include "string.h"
21 #include "wma.h"
22 #include "bitstream.h"
23
24 /** Read an 8, 16, or 32 bit entity from a VLC table. */
25 #define GET_DATA(v, table, i, size) \
26 {\
27 const uint8_t *ptr = (const uint8_t *)table + i * size; \
28 switch (size) { \
29 case 1: \
30 v = *(const uint8_t *)ptr; \
31 break; \
32 case 2: \
33 v = *(const uint16_t *)ptr; \
34 break; \
35 default: \
36 v = *(const uint32_t *)ptr; \
37 break; \
38 } \
39 }
40
41 static void alloc_table(struct vlc *vlc, int size)
42 {
43 vlc->table_size += size;
44 if (vlc->table_size > vlc->table_allocated) {
45 vlc->table_allocated += (1 << vlc->bits);
46 vlc->table = para_realloc(vlc->table,
47 sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
48 }
49 }
50
51 static int build_table(struct vlc *vlc, int table_nb_bits, int nb_codes,
52 const void *bits, const void *codes, int codes_size,
53 uint32_t code_prefix, int n_prefix)
54 {
55 int i, j, k, n, table_size, table_index, nb, n1, idx;
56 uint32_t code;
57 VLC_TYPE(*table)[2];
58
59 table_size = 1 << table_nb_bits;
60 table_index = vlc->table_size;
61 alloc_table(vlc, table_size);
62 table = &vlc->table[table_index];
63
64 for (i = 0; i < table_size; i++) {
65 table[i][1] = 0; /* bits */
66 table[i][0] = -1; /* codes */
67 }
68
69 /* map codes and compute auxiliary table sizes */
70 for (i = 0; i < nb_codes; i++) {
71 GET_DATA(n, bits, i, 1);
72 /* we accept tables with holes */
73 n -= n_prefix;
74 if (n <= 0)
75 continue;
76 GET_DATA(code, codes, i, codes_size);
77 /* if code matches the prefix, it is in the table */
78 if ((code >> n) != code_prefix)
79 continue;
80 if (n <= table_nb_bits) {
81 /* no need to add another table */
82 j = (code << (table_nb_bits - n)) & (table_size - 1);
83 nb = 1 << (table_nb_bits - n);
84 for (k = 0; k < nb; k++) {
85 assert(table[j][1] == 0); /* incorrect code */
86 table[j][1] = n; /* bits */
87 table[j][0] = i;
88 j++;
89 }
90 } else {
91 n -= table_nb_bits;
92 j = (code >> n) & ((1 << table_nb_bits) - 1);
93 /* compute table size */
94 n1 = -table[j][1]; /* bits */
95 if (n > n1)
96 n1 = n;
97 table[j][1] = -n1; /* bits */
98 }
99 }
100
101 /* fill auxiliary tables recursively */
102 for (i = 0; i < table_size; i++) {
103 n = table[i][1]; /* bits */
104 if (n < 0) {
105 n = -n;
106 if (n > table_nb_bits) {
107 n = table_nb_bits;
108 table[i][1] = -n; /* bits */
109 }
110 idx = build_table(vlc, n, nb_codes, bits, codes,
111 codes_size, (code_prefix << table_nb_bits) | i,
112 n_prefix + table_nb_bits);
113 /* vlc->table might have changed */
114 table = &vlc->table[table_index];
115 table[i][0] = idx; /* code */
116 }
117 }
118 return table_index;
119 }
120
121 /**
122 * Build VLC decoding tables suitable for use with get_vlc().
123 *
124 * \param vlc The structure to be initialized.
125 * \param nb_bits Set the decoding table size (2^nb_bits) entries.
126 * \param nb_codes Number of vlc codes.
127 * \param bits Table which gives the size (in bits) of each vlc code.
128 * \param codes Table which gives the bit pattern of of each vlc code.
129 * \param codes_size The number of bytes of each entry of the \a codes tables.
130 *
131 * The bigger \a nb_bits is, the faster is the decoding. But it should not be
132 * too big to save memory and L1 cache. '9' is a good compromise.
133 *
134 * The wrap and size parameters allow to use any memory configuration and
135 * types (byte/word/long) to store the bits and codes tables.
136 */
137 void init_vlc(struct vlc *vlc, int nb_bits, int nb_codes, const void *bits,
138 const void *codes, int codes_size)
139 {
140 PARA_INFO_LOG("nb_codes: %d\n", nb_codes);
141 vlc->bits = nb_bits;
142 vlc->table = NULL;
143 vlc->table_allocated = 0;
144 vlc->table_size = 0;
145 build_table(vlc, nb_bits, nb_codes, bits, codes, codes_size, 0, 0);
146 }
147
148 /**
149 * Deallocate all resources of a VLC table.
150 *
151 * \param vlc Pointer to an initialized vlc structure.
152 *
153 * The table given by \a vlc must have been initialized earlier via \ref
154 * init_vlc().
155 */
156 void free_vlc(struct vlc *vlc)
157 {
158 freep(&vlc->table);
159 }
160
161 /**
162 * Parse a vlc code.
163 *
164 * \param gbc The getbit context structure.
165 * \param table The vlc tables to use.
166 * \param bits The number of bits which will be read at once.
167 * \param max_depth The number of times \a bits bits must be read to completely
168 * read the longest vlc code = (max_vlc_length + bits - 1) / bits.
169 *
170 * The \a bits parameter must be identical to the \a nb_bits value supplied to
171 * \ref init_vlc().
172 *
173 * \return The vlc code.
174 */
175 int get_vlc(struct getbit_context *gbc, VLC_TYPE(*table)[2], int bits,
176 int max_depth)
177 {
178 int n, idx, nb_bits, code;
179
180 idx = show_bits(gbc, bits);
181 code = table[idx][0];
182 n = table[idx][1];
183 if (max_depth > 1 && n < 0) {
184 skip_bits(gbc, bits);
185 nb_bits = -n;
186 idx = show_bits(gbc, nb_bits) + code;
187 code = table[idx][0];
188 n = table[idx][1];
189 if (max_depth > 2 && n < 0) {
190 skip_bits(gbc, nb_bits);
191 nb_bits = -n;
192 idx = show_bits(gbc, nb_bits) + code;
193 code = table[idx][0];
194 n = table[idx][1];
195 }
196 }
197 skip_bits(gbc, n);
198 return code >= 0? code : -E_VLC;
199 }