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