First draft of the wma decoder.
[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 = realloc(vlc->table,
55                         sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
56                 if (!vlc->table)
57                         return -E_TABLE_ALLOC;
58         }
59         return idx;
60 }
61
62 static int build_table(struct vlc *vlc, int table_nb_bits,
63                 int nb_codes, const void *bits, int bits_wrap, int bits_size,
64                 const void *codes, int codes_wrap, int codes_size,
65                 uint32_t code_prefix, int n_prefix)
66 {
67         int ret, i, j, k, n, table_size, table_index, nb, n1, idx, code_prefix2,
68                 symbol;
69         uint32_t code;
70         VLC_TYPE(*table)[2];
71
72         table_size = 1 << table_nb_bits;
73         ret = alloc_table(vlc, table_size);
74         if (ret < 0)
75                 return ret;
76         table_index = ret;
77         table = &vlc->table[table_index];
78
79         for (i = 0; i < table_size; i++) {
80                 table[i][1] = 0;        //bits
81                 table[i][0] = -1;       //codes
82         }
83
84         /* first pass: map codes and compute auxillary table sizes */
85         for (i = 0; i < nb_codes; i++) {
86                 GET_DATA(n, bits, i, bits_wrap, bits_size);
87                 GET_DATA(code, codes, i, codes_wrap, codes_size);
88                 /* we accept tables with holes */
89                 if (n <= 0)
90                         continue;
91                 symbol = i;
92                 /* if code matches the prefix, it is in the table */
93                 n -= n_prefix;
94                 code_prefix2 = code >> n;
95                 if (n > 0 && code_prefix2 == code_prefix) {
96                         if (n <= table_nb_bits) {
97                                 /* no need to add another table */
98                                 j = (code << (table_nb_bits - n)) & (table_size - 1);
99                                 nb = 1 << (table_nb_bits - n);
100                                 for (k = 0; k < nb; k++) {
101                                         if (table[j][1] /* bits */ != 0)
102                                                 return -E_BAD_CODES;
103                                         table[j][1] = n;        //bits
104                                         table[j][0] = symbol;
105                                         j++;
106                                 }
107                         } else {
108                                 n -= table_nb_bits;
109                                 j = (code >> n) & ((1 << table_nb_bits) - 1);
110                                 /* compute table size */
111                                 n1 = -table[j][1];      //bits
112                                 if (n > n1)
113                                         n1 = n;
114                                 table[j][1] = -n1;      //bits
115                         }
116                 }
117         }
118
119         /* second pass : fill auxillary tables recursively */
120         for (i = 0; i < table_size; i++) {
121                 n = table[i][1];        //bits
122                 if (n < 0) {
123                         n = -n;
124                         if (n > table_nb_bits) {
125                                 n = table_nb_bits;
126                                 table[i][1] = -n;       //bits
127                         }
128                         ret = build_table(vlc, n, nb_codes,
129                                 bits, bits_wrap, bits_size,
130                                 codes, codes_wrap, codes_size,
131                                 (code_prefix << table_nb_bits) | i,
132                                 n_prefix + table_nb_bits);
133                         if (ret < 0)
134                                 return ret;
135                         idx = ret;
136                         /* note: realloc has been done, so reload tables */
137                         table = &vlc->table[table_index];
138                         table[i][0] = idx;      //code
139                 }
140         }
141         return table_index;
142 }
143
144 /**
145  * Build VLC decoding tables suitable for use with get_vlc().
146  *
147  * \param nb_bits Set the decoding table size (2^nb_bits)
148  * entries. The bigger it is, the faster is the decoding. But
149  * it should not be too big to save memory and L1 cache. '9'
150  * is a good compromise.
151  *
152  * \param nb_codes Number of vlcs codes.
153  *
154  * \param bits Table which gives the size (in bits) of each
155  * vlc code.
156  *
157  * \param codes Table which gives the bit pattern of of each
158  * vlc code.
159  *
160  * \param bits_wrap The number of bytes between each entry of the
161  * 'bits' or 'codes' tables.
162  *
163  *
164  * \param bits_size The number of bytes of each entry of the
165  * 'bits' or 'codes' tables.
166  *
167  * \param codes_wrap Same as bits_wrap but uses the 'codes' table.
168  * \param codes_size Same as bits_size but for the 'codes' table.
169  *
170  * The wrap and size parameters allow to use any memory configuration and
171  * types (byte/word/long) to store the bits and codes tables.
172  */
173 int init_vlc(struct vlc *vlc, int nb_bits, int nb_codes,
174                 const void *bits, int bits_wrap, int bits_size,
175                 const void *codes, int codes_wrap, int codes_size)
176 {
177         int ret;
178
179         PARA_INFO_LOG("nb_codes=%d\n", nb_codes);
180         vlc->bits = nb_bits;
181         vlc->table = NULL;
182         vlc->table_allocated = 0;
183         vlc->table_size = 0;
184         ret = build_table(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size,
185                 codes, codes_wrap, codes_size, 0, 0);
186         if (ret < 0)
187                 freep(&vlc->table);
188         return ret;
189 }
190
191 void free_vlc(struct vlc *vlc)
192 {
193         freep(&vlc->table);
194 }