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