Kill the bits_wrap and bits_size parameters of init_vlc().
[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 /**
15 * \file bitstream.c Bitstream API.
16 */
17
18 #include <stdlib.h>
19 #include <inttypes.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <regex.h>
23
24 #include "para.h"
25 #include "error.h"
26 #include "string.h"
27 #include "wma.h"
28 #include "bitstream.h"
29
30 #define GET_DATA(v, table, i, wrap, size) \
31 {\
32 const uint8_t *ptr = (const uint8_t *)table + i * wrap; \
33 switch (size) { \
34 case 1: \
35 v = *(const uint8_t *)ptr; \
36 break; \
37 case 2: \
38 v = *(const uint16_t *)ptr; \
39 break; \
40 default: \
41 v = *(const uint32_t *)ptr; \
42 break; \
43 } \
44 }
45
46 static int alloc_table(struct vlc *vlc, int size)
47 {
48 int idx;
49
50 idx = vlc->table_size;
51 vlc->table_size += size;
52 if (vlc->table_size > vlc->table_allocated) {
53 vlc->table_allocated += (1 << vlc->bits);
54 vlc->table = para_realloc(vlc->table,
55 sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
56 }
57 return idx;
58 }
59
60 static int build_table(struct vlc *vlc, int table_nb_bits, int nb_codes,
61 const void *bits, const void *codes, int codes_wrap,
62 int codes_size, uint32_t code_prefix, int n_prefix)
63 {
64 int ret, i, j, k, n, table_size, table_index, nb, n1, idx, code_prefix2,
65 symbol;
66 uint32_t code;
67 VLC_TYPE(*table)[2];
68
69 table_size = 1 << table_nb_bits;
70 ret = alloc_table(vlc, table_size);
71 if (ret < 0)
72 return ret;
73 table_index = ret;
74 table = &vlc->table[table_index];
75
76 for (i = 0; i < table_size; i++) {
77 table[i][1] = 0; //bits
78 table[i][0] = -1; //codes
79 }
80
81 /* first pass: map codes and compute auxillary table sizes */
82 for (i = 0; i < nb_codes; i++) {
83 GET_DATA(n, bits, i, 1, 1);
84 GET_DATA(code, codes, i, codes_wrap, codes_size);
85 /* we accept tables with holes */
86 if (n <= 0)
87 continue;
88 symbol = i;
89 /* if code matches the prefix, it is in the table */
90 n -= n_prefix;
91 code_prefix2 = code >> n;
92 if (n > 0 && code_prefix2 == code_prefix) {
93 if (n <= table_nb_bits) {
94 /* no need to add another table */
95 j = (code << (table_nb_bits - n)) & (table_size - 1);
96 nb = 1 << (table_nb_bits - n);
97 for (k = 0; k < nb; k++) {
98 if (table[j][1] /* bits */ != 0)
99 return -E_BAD_CODES;
100 table[j][1] = n; //bits
101 table[j][0] = symbol;
102 j++;
103 }
104 } else {
105 n -= table_nb_bits;
106 j = (code >> n) & ((1 << table_nb_bits) - 1);
107 /* compute table size */
108 n1 = -table[j][1]; //bits
109 if (n > n1)
110 n1 = n;
111 table[j][1] = -n1; //bits
112 }
113 }
114 }
115
116 /* second pass : fill auxillary tables recursively */
117 for (i = 0; i < table_size; i++) {
118 n = table[i][1]; //bits
119 if (n < 0) {
120 n = -n;
121 if (n > table_nb_bits) {
122 n = table_nb_bits;
123 table[i][1] = -n; //bits
124 }
125 ret = build_table(vlc, n, nb_codes,
126 bits, codes, codes_wrap, codes_size,
127 (code_prefix << table_nb_bits) | i,
128 n_prefix + table_nb_bits);
129 if (ret < 0)
130 return ret;
131 idx = ret;
132 /* note: realloc has been done, so reload tables */
133 table = &vlc->table[table_index];
134 table[i][0] = idx; //code
135 }
136 }
137 return table_index;
138 }
139
140 /**
141 * Build VLC decoding tables suitable for use with get_vlc().
142 *
143 * \param nb_bits Set the decoding table size (2^nb_bits)
144 * entries. The bigger it is, the faster is the decoding. But
145 * it should not be too big to save memory and L1 cache. '9'
146 * is a good compromise.
147 *
148 * \param nb_codes Number of vlcs codes.
149 *
150 * \param bits Table which gives the size (in bits) of each
151 * vlc code.
152 *
153 * \param codes Table which gives the bit pattern of of each
154 * vlc code.
155 *
156 * \param codes_wrap The number of bytes between each entry of the
157 * 'codes' tables.
158 *
159 * \param codes_size The number of bytes of each entry of the
160 * 'codes' tables.
161 *
162 * The wrap and size parameters allow to use any memory configuration and
163 * types (byte/word/long) to store the bits and codes tables.
164 */
165 int init_vlc(struct vlc *vlc, int nb_bits, int nb_codes,
166 const void *bits, const void *codes, int codes_wrap,
167 int codes_size)
168 {
169 int ret;
170
171 PARA_INFO_LOG("nb_codes=%d\n", nb_codes);
172 vlc->bits = nb_bits;
173 vlc->table = NULL;
174 vlc->table_allocated = 0;
175 vlc->table_size = 0;
176 ret = build_table(vlc, nb_bits, nb_codes, bits,
177 codes, codes_wrap, codes_size, 0, 0);
178 if (ret < 0)
179 freep(&vlc->table);
180 return ret;
181 }
182
183 void free_vlc(struct vlc *vlc)
184 {
185 freep(&vlc->table);
186 }