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