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