libacfutils
A general purpose library of utility functions designed to make it easier to develop addons for the X-Plane flight simulator.
Loading...
Searching...
No Matches
minimp3.c
1/*
2 * MPEG Audio Layer III decoder
3 * Copyright (c) 2001, 2002 Fabrice Bellard,
4 * (c) 2007 Martin J. Fiedler
5 *
6 * This file is a stripped-down version of the MPEG Audio decoder from
7 * the FFmpeg libavcodec library.
8 *
9 * FFmpeg and minimp3 are free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg and minimp3 are distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include "libc.h"
25#include "minimp3.h"
26
27#define MP3_FRAME_SIZE 1152
28#define MP3_MAX_CODED_FRAME_SIZE 1792
29#define MP3_MAX_CHANNELS 2
30#define SBLIMIT 32
31
32#define MP3_STEREO 0
33#define MP3_JSTEREO 1
34#define MP3_DUAL 2
35#define MP3_MONO 3
36
37#define SAME_HEADER_MASK \
38 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
39
40#define FRAC_BITS 15
41#define WFRAC_BITS 14
42
43#define OUT_MAX (32767)
44#define OUT_MIN (-32768)
45#define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
46
47#define MODE_EXT_MS_STEREO 2
48#define MODE_EXT_I_STEREO 1
49
50#define FRAC_ONE (1 << FRAC_BITS)
51#define FIX(a) ((int)((a) * FRAC_ONE))
52#define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
53#define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
54#define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
55
56#ifndef _MSC_VER
57 #define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
58 #define MULH(a,b) (((int64_t)(a) * (int64_t)(b)) >> 32)
59#else
60 static INLINE int MULL(int a, int b) {
61 int res;
62 __asm {
63 mov eax, a
64 imul b
65 shr eax, 15
66 shl edx, 17
67 or eax, edx
68 mov res, eax
69 }
70 return res;
71 }
72 static INLINE int MULH(int a, int b) {
73 int res;
74 __asm {
75 mov eax, a
76 imul b
77 mov res, edx
78 }
79 return res;
80 }
81#endif
82#define MULS(ra, rb) ((ra) * (rb))
83
84#define ISQRT2 FIXR(0.70710678118654752440)
85
86#define HEADER_SIZE 4
87#define BACKSTEP_SIZE 512
88#define EXTRABYTES 24
89
90#define VLC_TYPE int16_t
91
93
94struct _granule;
95
96typedef struct _bitstream {
97 const uint8_t *buffer, *buffer_end;
98 int index;
99 int size_in_bits;
101
102typedef struct _vlc {
103 int bits;
104 VLC_TYPE (*table)[2];
105 int table_size, table_allocated;
106} vlc_t;
107
108typedef struct _mp3_context {
109 uint8_t last_buf[2*BACKSTEP_SIZE + EXTRABYTES];
110 int last_buf_size;
111 int frame_size;
112 uint32_t free_format_next_header;
113 int error_protection;
114 int sample_rate;
115 int sample_rate_index;
116 int bit_rate;
117 bitstream_t gb;
118 bitstream_t in_gb;
119 int nb_channels;
120 int mode;
121 int mode_ext;
122 int lsf;
123 int16_t synth_buf[MP3_MAX_CHANNELS][512 * 2];
124 int synth_buf_offset[MP3_MAX_CHANNELS];
125 int32_t sb_samples[MP3_MAX_CHANNELS][36][SBLIMIT];
126 int32_t mdct_buf[MP3_MAX_CHANNELS][SBLIMIT * 18];
127 int dither_state;
129
130typedef struct _granule {
131 uint8_t scfsi;
132 int part2_3_length;
133 int big_values;
134 int global_gain;
135 int scalefac_compress;
136 uint8_t block_type;
137 uint8_t switch_point;
138 int table_select[3];
139 int subblock_gain[3];
140 uint8_t scalefac_scale;
141 uint8_t count1table_select;
142 int region_size[3];
143 int preflag;
144 int short_start, long_end;
145 uint8_t scale_factors[40];
146 int32_t sb_hybrid[SBLIMIT * 18];
147} granule_t;
148
149typedef struct _huff_table {
150 int xsize;
151 const uint8_t *bits;
152 const uint16_t *codes;
154
155static vlc_t huff_vlc[16];
156static vlc_t huff_quad_vlc[2];
157static uint16_t band_index_long[9][23];
158#define TABLE_4_3_SIZE (8191 + 16)*4
159static int8_t *table_4_3_exp;
160static uint32_t *table_4_3_value;
161static uint32_t exp_table[512];
162static uint32_t expval_table[512][16];
163static int32_t is_table[2][16];
164static int32_t is_table_lsf[2][2][16];
165static int32_t csa_table[8][4];
166static float csa_table_float[8][4];
167static int32_t mdct_win[8][36];
168static int16_t window[512];
169
171
172static const uint16_t mp3_bitrate_tab[2][15] = {
173 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 },
174 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
175};
176
177static const uint16_t mp3_freq_tab[3] = { 44100, 48000, 32000 };
178
179static const int32_t mp3_enwindow[257] = {
180 0, -1, -1, -1, -1, -1, -1, -2,
181 -2, -2, -2, -3, -3, -4, -4, -5,
182 -5, -6, -7, -7, -8, -9, -10, -11,
183 -13, -14, -16, -17, -19, -21, -24, -26,
184 -29, -31, -35, -38, -41, -45, -49, -53,
185 -58, -63, -68, -73, -79, -85, -91, -97,
186 -104, -111, -117, -125, -132, -139, -147, -154,
187 -161, -169, -176, -183, -190, -196, -202, -208,
188 213, 218, 222, 225, 227, 228, 228, 227,
189 224, 221, 215, 208, 200, 189, 177, 163,
190 146, 127, 106, 83, 57, 29, -2, -36,
191 -72, -111, -153, -197, -244, -294, -347, -401,
192 -459, -519, -581, -645, -711, -779, -848, -919,
193 -991, -1064, -1137, -1210, -1283, -1356, -1428, -1498,
194 -1567, -1634, -1698, -1759, -1817, -1870, -1919, -1962,
195 -2001, -2032, -2057, -2075, -2085, -2087, -2080, -2063,
196 2037, 2000, 1952, 1893, 1822, 1739, 1644, 1535,
197 1414, 1280, 1131, 970, 794, 605, 402, 185,
198 -45, -288, -545, -814, -1095, -1388, -1692, -2006,
199 -2330, -2663, -3004, -3351, -3705, -4063, -4425, -4788,
200 -5153, -5517, -5879, -6237, -6589, -6935, -7271, -7597,
201 -7910, -8209, -8491, -8755, -8998, -9219, -9416, -9585,
202 -9727, -9838, -9916, -9959, -9966, -9935, -9863, -9750,
203 -9592, -9389, -9139, -8840, -8492, -8092, -7640, -7134,
204 6574, 5959, 5288, 4561, 3776, 2935, 2037, 1082,
205 70, -998, -2122, -3300, -4533, -5818, -7154, -8540,
206 -9975,-11455,-12980,-14548,-16155,-17799,-19478,-21189,
207-22929,-24694,-26482,-28289,-30112,-31947,-33791,-35640,
208-37489,-39336,-41176,-43006,-44821,-46617,-48390,-50137,
209-51853,-53534,-55178,-56778,-58333,-59838,-61289,-62684,
210-64019,-65290,-66494,-67629,-68692,-69679,-70590,-71420,
211-72169,-72835,-73415,-73908,-74313,-74630,-74856,-74992,
212 75038,
213};
214
215static const uint8_t slen_table[2][16] = {
216 { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 },
217 { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 },
218};
219
220static const uint8_t lsf_nsf_table[6][3][4] = {
221 { { 6, 5, 5, 5 }, { 9, 9, 9, 9 }, { 6, 9, 9, 9 } },
222 { { 6, 5, 7, 3 }, { 9, 9, 12, 6 }, { 6, 9, 12, 6 } },
223 { { 11, 10, 0, 0 }, { 18, 18, 0, 0 }, { 15, 18, 0, 0 } },
224 { { 7, 7, 7, 0 }, { 12, 12, 12, 0 }, { 6, 15, 12, 0 } },
225 { { 6, 6, 6, 3 }, { 12, 9, 9, 6 }, { 6, 12, 9, 6 } },
226 { { 8, 8, 5, 0 }, { 15, 12, 9, 0 }, { 6, 18, 9, 0 } },
227};
228
229static const uint16_t mp3_huffcodes_1[4] = {
230 0x0001, 0x0001, 0x0001, 0x0000,
231};
232
233static const uint8_t mp3_huffbits_1[4] = {
234 1, 3, 2, 3,
235};
236
237static const uint16_t mp3_huffcodes_2[9] = {
238 0x0001, 0x0002, 0x0001, 0x0003, 0x0001, 0x0001, 0x0003, 0x0002,
239 0x0000,
240};
241
242static const uint8_t mp3_huffbits_2[9] = {
243 1, 3, 6, 3, 3, 5, 5, 5,
244 6,
245};
246
247static const uint16_t mp3_huffcodes_3[9] = {
248 0x0003, 0x0002, 0x0001, 0x0001, 0x0001, 0x0001, 0x0003, 0x0002,
249 0x0000,
250};
251
252static const uint8_t mp3_huffbits_3[9] = {
253 2, 2, 6, 3, 2, 5, 5, 5,
254 6,
255};
256
257static const uint16_t mp3_huffcodes_5[16] = {
258 0x0001, 0x0002, 0x0006, 0x0005, 0x0003, 0x0001, 0x0004, 0x0004,
259 0x0007, 0x0005, 0x0007, 0x0001, 0x0006, 0x0001, 0x0001, 0x0000,
260};
261
262static const uint8_t mp3_huffbits_5[16] = {
263 1, 3, 6, 7, 3, 3, 6, 7,
264 6, 6, 7, 8, 7, 6, 7, 8,
265};
266
267static const uint16_t mp3_huffcodes_6[16] = {
268 0x0007, 0x0003, 0x0005, 0x0001, 0x0006, 0x0002, 0x0003, 0x0002,
269 0x0005, 0x0004, 0x0004, 0x0001, 0x0003, 0x0003, 0x0002, 0x0000,
270};
271
272static const uint8_t mp3_huffbits_6[16] = {
273 3, 3, 5, 7, 3, 2, 4, 5,
274 4, 4, 5, 6, 6, 5, 6, 7,
275};
276
277static const uint16_t mp3_huffcodes_7[36] = {
278 0x0001, 0x0002, 0x000a, 0x0013, 0x0010, 0x000a, 0x0003, 0x0003,
279 0x0007, 0x000a, 0x0005, 0x0003, 0x000b, 0x0004, 0x000d, 0x0011,
280 0x0008, 0x0004, 0x000c, 0x000b, 0x0012, 0x000f, 0x000b, 0x0002,
281 0x0007, 0x0006, 0x0009, 0x000e, 0x0003, 0x0001, 0x0006, 0x0004,
282 0x0005, 0x0003, 0x0002, 0x0000,
283};
284
285static const uint8_t mp3_huffbits_7[36] = {
286 1, 3, 6, 8, 8, 9, 3, 4,
287 6, 7, 7, 8, 6, 5, 7, 8,
288 8, 9, 7, 7, 8, 9, 9, 9,
289 7, 7, 8, 9, 9, 10, 8, 8,
290 9, 10, 10, 10,
291};
292
293static const uint16_t mp3_huffcodes_8[36] = {
294 0x0003, 0x0004, 0x0006, 0x0012, 0x000c, 0x0005, 0x0005, 0x0001,
295 0x0002, 0x0010, 0x0009, 0x0003, 0x0007, 0x0003, 0x0005, 0x000e,
296 0x0007, 0x0003, 0x0013, 0x0011, 0x000f, 0x000d, 0x000a, 0x0004,
297 0x000d, 0x0005, 0x0008, 0x000b, 0x0005, 0x0001, 0x000c, 0x0004,
298 0x0004, 0x0001, 0x0001, 0x0000,
299};
300
301static const uint8_t mp3_huffbits_8[36] = {
302 2, 3, 6, 8, 8, 9, 3, 2,
303 4, 8, 8, 8, 6, 4, 6, 8,
304 8, 9, 8, 8, 8, 9, 9, 10,
305 8, 7, 8, 9, 10, 10, 9, 8,
306 9, 9, 11, 11,
307};
308
309static const uint16_t mp3_huffcodes_9[36] = {
310 0x0007, 0x0005, 0x0009, 0x000e, 0x000f, 0x0007, 0x0006, 0x0004,
311 0x0005, 0x0005, 0x0006, 0x0007, 0x0007, 0x0006, 0x0008, 0x0008,
312 0x0008, 0x0005, 0x000f, 0x0006, 0x0009, 0x000a, 0x0005, 0x0001,
313 0x000b, 0x0007, 0x0009, 0x0006, 0x0004, 0x0001, 0x000e, 0x0004,
314 0x0006, 0x0002, 0x0006, 0x0000,
315};
316
317static const uint8_t mp3_huffbits_9[36] = {
318 3, 3, 5, 6, 8, 9, 3, 3,
319 4, 5, 6, 8, 4, 4, 5, 6,
320 7, 8, 6, 5, 6, 7, 7, 8,
321 7, 6, 7, 7, 8, 9, 8, 7,
322 8, 8, 9, 9,
323};
324
325static const uint16_t mp3_huffcodes_10[64] = {
326 0x0001, 0x0002, 0x000a, 0x0017, 0x0023, 0x001e, 0x000c, 0x0011,
327 0x0003, 0x0003, 0x0008, 0x000c, 0x0012, 0x0015, 0x000c, 0x0007,
328 0x000b, 0x0009, 0x000f, 0x0015, 0x0020, 0x0028, 0x0013, 0x0006,
329 0x000e, 0x000d, 0x0016, 0x0022, 0x002e, 0x0017, 0x0012, 0x0007,
330 0x0014, 0x0013, 0x0021, 0x002f, 0x001b, 0x0016, 0x0009, 0x0003,
331 0x001f, 0x0016, 0x0029, 0x001a, 0x0015, 0x0014, 0x0005, 0x0003,
332 0x000e, 0x000d, 0x000a, 0x000b, 0x0010, 0x0006, 0x0005, 0x0001,
333 0x0009, 0x0008, 0x0007, 0x0008, 0x0004, 0x0004, 0x0002, 0x0000,
334};
335
336static const uint8_t mp3_huffbits_10[64] = {
337 1, 3, 6, 8, 9, 9, 9, 10,
338 3, 4, 6, 7, 8, 9, 8, 8,
339 6, 6, 7, 8, 9, 10, 9, 9,
340 7, 7, 8, 9, 10, 10, 9, 10,
341 8, 8, 9, 10, 10, 10, 10, 10,
342 9, 9, 10, 10, 11, 11, 10, 11,
343 8, 8, 9, 10, 10, 10, 11, 11,
344 9, 8, 9, 10, 10, 11, 11, 11,
345};
346
347static const uint16_t mp3_huffcodes_11[64] = {
348 0x0003, 0x0004, 0x000a, 0x0018, 0x0022, 0x0021, 0x0015, 0x000f,
349 0x0005, 0x0003, 0x0004, 0x000a, 0x0020, 0x0011, 0x000b, 0x000a,
350 0x000b, 0x0007, 0x000d, 0x0012, 0x001e, 0x001f, 0x0014, 0x0005,
351 0x0019, 0x000b, 0x0013, 0x003b, 0x001b, 0x0012, 0x000c, 0x0005,
352 0x0023, 0x0021, 0x001f, 0x003a, 0x001e, 0x0010, 0x0007, 0x0005,
353 0x001c, 0x001a, 0x0020, 0x0013, 0x0011, 0x000f, 0x0008, 0x000e,
354 0x000e, 0x000c, 0x0009, 0x000d, 0x000e, 0x0009, 0x0004, 0x0001,
355 0x000b, 0x0004, 0x0006, 0x0006, 0x0006, 0x0003, 0x0002, 0x0000,
356};
357
358static const uint8_t mp3_huffbits_11[64] = {
359 2, 3, 5, 7, 8, 9, 8, 9,
360 3, 3, 4, 6, 8, 8, 7, 8,
361 5, 5, 6, 7, 8, 9, 8, 8,
362 7, 6, 7, 9, 8, 10, 8, 9,
363 8, 8, 8, 9, 9, 10, 9, 10,
364 8, 8, 9, 10, 10, 11, 10, 11,
365 8, 7, 7, 8, 9, 10, 10, 10,
366 8, 7, 8, 9, 10, 10, 10, 10,
367};
368
369static const uint16_t mp3_huffcodes_12[64] = {
370 0x0009, 0x0006, 0x0010, 0x0021, 0x0029, 0x0027, 0x0026, 0x001a,
371 0x0007, 0x0005, 0x0006, 0x0009, 0x0017, 0x0010, 0x001a, 0x000b,
372 0x0011, 0x0007, 0x000b, 0x000e, 0x0015, 0x001e, 0x000a, 0x0007,
373 0x0011, 0x000a, 0x000f, 0x000c, 0x0012, 0x001c, 0x000e, 0x0005,
374 0x0020, 0x000d, 0x0016, 0x0013, 0x0012, 0x0010, 0x0009, 0x0005,
375 0x0028, 0x0011, 0x001f, 0x001d, 0x0011, 0x000d, 0x0004, 0x0002,
376 0x001b, 0x000c, 0x000b, 0x000f, 0x000a, 0x0007, 0x0004, 0x0001,
377 0x001b, 0x000c, 0x0008, 0x000c, 0x0006, 0x0003, 0x0001, 0x0000,
378};
379
380static const uint8_t mp3_huffbits_12[64] = {
381 4, 3, 5, 7, 8, 9, 9, 9,
382 3, 3, 4, 5, 7, 7, 8, 8,
383 5, 4, 5, 6, 7, 8, 7, 8,
384 6, 5, 6, 6, 7, 8, 8, 8,
385 7, 6, 7, 7, 8, 8, 8, 9,
386 8, 7, 8, 8, 8, 9, 8, 9,
387 8, 7, 7, 8, 8, 9, 9, 10,
388 9, 8, 8, 9, 9, 9, 9, 10,
389};
390
391static const uint16_t mp3_huffcodes_13[256] = {
392 0x0001, 0x0005, 0x000e, 0x0015, 0x0022, 0x0033, 0x002e, 0x0047,
393 0x002a, 0x0034, 0x0044, 0x0034, 0x0043, 0x002c, 0x002b, 0x0013,
394 0x0003, 0x0004, 0x000c, 0x0013, 0x001f, 0x001a, 0x002c, 0x0021,
395 0x001f, 0x0018, 0x0020, 0x0018, 0x001f, 0x0023, 0x0016, 0x000e,
396 0x000f, 0x000d, 0x0017, 0x0024, 0x003b, 0x0031, 0x004d, 0x0041,
397 0x001d, 0x0028, 0x001e, 0x0028, 0x001b, 0x0021, 0x002a, 0x0010,
398 0x0016, 0x0014, 0x0025, 0x003d, 0x0038, 0x004f, 0x0049, 0x0040,
399 0x002b, 0x004c, 0x0038, 0x0025, 0x001a, 0x001f, 0x0019, 0x000e,
400 0x0023, 0x0010, 0x003c, 0x0039, 0x0061, 0x004b, 0x0072, 0x005b,
401 0x0036, 0x0049, 0x0037, 0x0029, 0x0030, 0x0035, 0x0017, 0x0018,
402 0x003a, 0x001b, 0x0032, 0x0060, 0x004c, 0x0046, 0x005d, 0x0054,
403 0x004d, 0x003a, 0x004f, 0x001d, 0x004a, 0x0031, 0x0029, 0x0011,
404 0x002f, 0x002d, 0x004e, 0x004a, 0x0073, 0x005e, 0x005a, 0x004f,
405 0x0045, 0x0053, 0x0047, 0x0032, 0x003b, 0x0026, 0x0024, 0x000f,
406 0x0048, 0x0022, 0x0038, 0x005f, 0x005c, 0x0055, 0x005b, 0x005a,
407 0x0056, 0x0049, 0x004d, 0x0041, 0x0033, 0x002c, 0x002b, 0x002a,
408 0x002b, 0x0014, 0x001e, 0x002c, 0x0037, 0x004e, 0x0048, 0x0057,
409 0x004e, 0x003d, 0x002e, 0x0036, 0x0025, 0x001e, 0x0014, 0x0010,
410 0x0035, 0x0019, 0x0029, 0x0025, 0x002c, 0x003b, 0x0036, 0x0051,
411 0x0042, 0x004c, 0x0039, 0x0036, 0x0025, 0x0012, 0x0027, 0x000b,
412 0x0023, 0x0021, 0x001f, 0x0039, 0x002a, 0x0052, 0x0048, 0x0050,
413 0x002f, 0x003a, 0x0037, 0x0015, 0x0016, 0x001a, 0x0026, 0x0016,
414 0x0035, 0x0019, 0x0017, 0x0026, 0x0046, 0x003c, 0x0033, 0x0024,
415 0x0037, 0x001a, 0x0022, 0x0017, 0x001b, 0x000e, 0x0009, 0x0007,
416 0x0022, 0x0020, 0x001c, 0x0027, 0x0031, 0x004b, 0x001e, 0x0034,
417 0x0030, 0x0028, 0x0034, 0x001c, 0x0012, 0x0011, 0x0009, 0x0005,
418 0x002d, 0x0015, 0x0022, 0x0040, 0x0038, 0x0032, 0x0031, 0x002d,
419 0x001f, 0x0013, 0x000c, 0x000f, 0x000a, 0x0007, 0x0006, 0x0003,
420 0x0030, 0x0017, 0x0014, 0x0027, 0x0024, 0x0023, 0x0035, 0x0015,
421 0x0010, 0x0017, 0x000d, 0x000a, 0x0006, 0x0001, 0x0004, 0x0002,
422 0x0010, 0x000f, 0x0011, 0x001b, 0x0019, 0x0014, 0x001d, 0x000b,
423 0x0011, 0x000c, 0x0010, 0x0008, 0x0001, 0x0001, 0x0000, 0x0001,
424};
425
426static const uint8_t mp3_huffbits_13[256] = {
427 1, 4, 6, 7, 8, 9, 9, 10,
428 9, 10, 11, 11, 12, 12, 13, 13,
429 3, 4, 6, 7, 8, 8, 9, 9,
430 9, 9, 10, 10, 11, 12, 12, 12,
431 6, 6, 7, 8, 9, 9, 10, 10,
432 9, 10, 10, 11, 11, 12, 13, 13,
433 7, 7, 8, 9, 9, 10, 10, 10,
434 10, 11, 11, 11, 11, 12, 13, 13,
435 8, 7, 9, 9, 10, 10, 11, 11,
436 10, 11, 11, 12, 12, 13, 13, 14,
437 9, 8, 9, 10, 10, 10, 11, 11,
438 11, 11, 12, 11, 13, 13, 14, 14,
439 9, 9, 10, 10, 11, 11, 11, 11,
440 11, 12, 12, 12, 13, 13, 14, 14,
441 10, 9, 10, 11, 11, 11, 12, 12,
442 12, 12, 13, 13, 13, 14, 16, 16,
443 9, 8, 9, 10, 10, 11, 11, 12,
444 12, 12, 12, 13, 13, 14, 15, 15,
445 10, 9, 10, 10, 11, 11, 11, 13,
446 12, 13, 13, 14, 14, 14, 16, 15,
447 10, 10, 10, 11, 11, 12, 12, 13,
448 12, 13, 14, 13, 14, 15, 16, 17,
449 11, 10, 10, 11, 12, 12, 12, 12,
450 13, 13, 13, 14, 15, 15, 15, 16,
451 11, 11, 11, 12, 12, 13, 12, 13,
452 14, 14, 15, 15, 15, 16, 16, 16,
453 12, 11, 12, 13, 13, 13, 14, 14,
454 14, 14, 14, 15, 16, 15, 16, 16,
455 13, 12, 12, 13, 13, 13, 15, 14,
456 14, 17, 15, 15, 15, 17, 16, 16,
457 12, 12, 13, 14, 14, 14, 15, 14,
458 15, 15, 16, 16, 19, 18, 19, 16,
459};
460
461static const uint16_t mp3_huffcodes_15[256] = {
462 0x0007, 0x000c, 0x0012, 0x0035, 0x002f, 0x004c, 0x007c, 0x006c,
463 0x0059, 0x007b, 0x006c, 0x0077, 0x006b, 0x0051, 0x007a, 0x003f,
464 0x000d, 0x0005, 0x0010, 0x001b, 0x002e, 0x0024, 0x003d, 0x0033,
465 0x002a, 0x0046, 0x0034, 0x0053, 0x0041, 0x0029, 0x003b, 0x0024,
466 0x0013, 0x0011, 0x000f, 0x0018, 0x0029, 0x0022, 0x003b, 0x0030,
467 0x0028, 0x0040, 0x0032, 0x004e, 0x003e, 0x0050, 0x0038, 0x0021,
468 0x001d, 0x001c, 0x0019, 0x002b, 0x0027, 0x003f, 0x0037, 0x005d,
469 0x004c, 0x003b, 0x005d, 0x0048, 0x0036, 0x004b, 0x0032, 0x001d,
470 0x0034, 0x0016, 0x002a, 0x0028, 0x0043, 0x0039, 0x005f, 0x004f,
471 0x0048, 0x0039, 0x0059, 0x0045, 0x0031, 0x0042, 0x002e, 0x001b,
472 0x004d, 0x0025, 0x0023, 0x0042, 0x003a, 0x0034, 0x005b, 0x004a,
473 0x003e, 0x0030, 0x004f, 0x003f, 0x005a, 0x003e, 0x0028, 0x0026,
474 0x007d, 0x0020, 0x003c, 0x0038, 0x0032, 0x005c, 0x004e, 0x0041,
475 0x0037, 0x0057, 0x0047, 0x0033, 0x0049, 0x0033, 0x0046, 0x001e,
476 0x006d, 0x0035, 0x0031, 0x005e, 0x0058, 0x004b, 0x0042, 0x007a,
477 0x005b, 0x0049, 0x0038, 0x002a, 0x0040, 0x002c, 0x0015, 0x0019,
478 0x005a, 0x002b, 0x0029, 0x004d, 0x0049, 0x003f, 0x0038, 0x005c,
479 0x004d, 0x0042, 0x002f, 0x0043, 0x0030, 0x0035, 0x0024, 0x0014,
480 0x0047, 0x0022, 0x0043, 0x003c, 0x003a, 0x0031, 0x0058, 0x004c,
481 0x0043, 0x006a, 0x0047, 0x0036, 0x0026, 0x0027, 0x0017, 0x000f,
482 0x006d, 0x0035, 0x0033, 0x002f, 0x005a, 0x0052, 0x003a, 0x0039,
483 0x0030, 0x0048, 0x0039, 0x0029, 0x0017, 0x001b, 0x003e, 0x0009,
484 0x0056, 0x002a, 0x0028, 0x0025, 0x0046, 0x0040, 0x0034, 0x002b,
485 0x0046, 0x0037, 0x002a, 0x0019, 0x001d, 0x0012, 0x000b, 0x000b,
486 0x0076, 0x0044, 0x001e, 0x0037, 0x0032, 0x002e, 0x004a, 0x0041,
487 0x0031, 0x0027, 0x0018, 0x0010, 0x0016, 0x000d, 0x000e, 0x0007,
488 0x005b, 0x002c, 0x0027, 0x0026, 0x0022, 0x003f, 0x0034, 0x002d,
489 0x001f, 0x0034, 0x001c, 0x0013, 0x000e, 0x0008, 0x0009, 0x0003,
490 0x007b, 0x003c, 0x003a, 0x0035, 0x002f, 0x002b, 0x0020, 0x0016,
491 0x0025, 0x0018, 0x0011, 0x000c, 0x000f, 0x000a, 0x0002, 0x0001,
492 0x0047, 0x0025, 0x0022, 0x001e, 0x001c, 0x0014, 0x0011, 0x001a,
493 0x0015, 0x0010, 0x000a, 0x0006, 0x0008, 0x0006, 0x0002, 0x0000,
494};
495
496static const uint8_t mp3_huffbits_15[256] = {
497 3, 4, 5, 7, 7, 8, 9, 9,
498 9, 10, 10, 11, 11, 11, 12, 13,
499 4, 3, 5, 6, 7, 7, 8, 8,
500 8, 9, 9, 10, 10, 10, 11, 11,
501 5, 5, 5, 6, 7, 7, 8, 8,
502 8, 9, 9, 10, 10, 11, 11, 11,
503 6, 6, 6, 7, 7, 8, 8, 9,
504 9, 9, 10, 10, 10, 11, 11, 11,
505 7, 6, 7, 7, 8, 8, 9, 9,
506 9, 9, 10, 10, 10, 11, 11, 11,
507 8, 7, 7, 8, 8, 8, 9, 9,
508 9, 9, 10, 10, 11, 11, 11, 12,
509 9, 7, 8, 8, 8, 9, 9, 9,
510 9, 10, 10, 10, 11, 11, 12, 12,
511 9, 8, 8, 9, 9, 9, 9, 10,
512 10, 10, 10, 10, 11, 11, 11, 12,
513 9, 8, 8, 9, 9, 9, 9, 10,
514 10, 10, 10, 11, 11, 12, 12, 12,
515 9, 8, 9, 9, 9, 9, 10, 10,
516 10, 11, 11, 11, 11, 12, 12, 12,
517 10, 9, 9, 9, 10, 10, 10, 10,
518 10, 11, 11, 11, 11, 12, 13, 12,
519 10, 9, 9, 9, 10, 10, 10, 10,
520 11, 11, 11, 11, 12, 12, 12, 13,
521 11, 10, 9, 10, 10, 10, 11, 11,
522 11, 11, 11, 11, 12, 12, 13, 13,
523 11, 10, 10, 10, 10, 11, 11, 11,
524 11, 12, 12, 12, 12, 12, 13, 13,
525 12, 11, 11, 11, 11, 11, 11, 11,
526 12, 12, 12, 12, 13, 13, 12, 13,
527 12, 11, 11, 11, 11, 11, 11, 12,
528 12, 12, 12, 12, 13, 13, 13, 13,
529};
530
531static const uint16_t mp3_huffcodes_16[256] = {
532 0x0001, 0x0005, 0x000e, 0x002c, 0x004a, 0x003f, 0x006e, 0x005d,
533 0x00ac, 0x0095, 0x008a, 0x00f2, 0x00e1, 0x00c3, 0x0178, 0x0011,
534 0x0003, 0x0004, 0x000c, 0x0014, 0x0023, 0x003e, 0x0035, 0x002f,
535 0x0053, 0x004b, 0x0044, 0x0077, 0x00c9, 0x006b, 0x00cf, 0x0009,
536 0x000f, 0x000d, 0x0017, 0x0026, 0x0043, 0x003a, 0x0067, 0x005a,
537 0x00a1, 0x0048, 0x007f, 0x0075, 0x006e, 0x00d1, 0x00ce, 0x0010,
538 0x002d, 0x0015, 0x0027, 0x0045, 0x0040, 0x0072, 0x0063, 0x0057,
539 0x009e, 0x008c, 0x00fc, 0x00d4, 0x00c7, 0x0183, 0x016d, 0x001a,
540 0x004b, 0x0024, 0x0044, 0x0041, 0x0073, 0x0065, 0x00b3, 0x00a4,
541 0x009b, 0x0108, 0x00f6, 0x00e2, 0x018b, 0x017e, 0x016a, 0x0009,
542 0x0042, 0x001e, 0x003b, 0x0038, 0x0066, 0x00b9, 0x00ad, 0x0109,
543 0x008e, 0x00fd, 0x00e8, 0x0190, 0x0184, 0x017a, 0x01bd, 0x0010,
544 0x006f, 0x0036, 0x0034, 0x0064, 0x00b8, 0x00b2, 0x00a0, 0x0085,
545 0x0101, 0x00f4, 0x00e4, 0x00d9, 0x0181, 0x016e, 0x02cb, 0x000a,
546 0x0062, 0x0030, 0x005b, 0x0058, 0x00a5, 0x009d, 0x0094, 0x0105,
547 0x00f8, 0x0197, 0x018d, 0x0174, 0x017c, 0x0379, 0x0374, 0x0008,
548 0x0055, 0x0054, 0x0051, 0x009f, 0x009c, 0x008f, 0x0104, 0x00f9,
549 0x01ab, 0x0191, 0x0188, 0x017f, 0x02d7, 0x02c9, 0x02c4, 0x0007,
550 0x009a, 0x004c, 0x0049, 0x008d, 0x0083, 0x0100, 0x00f5, 0x01aa,
551 0x0196, 0x018a, 0x0180, 0x02df, 0x0167, 0x02c6, 0x0160, 0x000b,
552 0x008b, 0x0081, 0x0043, 0x007d, 0x00f7, 0x00e9, 0x00e5, 0x00db,
553 0x0189, 0x02e7, 0x02e1, 0x02d0, 0x0375, 0x0372, 0x01b7, 0x0004,
554 0x00f3, 0x0078, 0x0076, 0x0073, 0x00e3, 0x00df, 0x018c, 0x02ea,
555 0x02e6, 0x02e0, 0x02d1, 0x02c8, 0x02c2, 0x00df, 0x01b4, 0x0006,
556 0x00ca, 0x00e0, 0x00de, 0x00da, 0x00d8, 0x0185, 0x0182, 0x017d,
557 0x016c, 0x0378, 0x01bb, 0x02c3, 0x01b8, 0x01b5, 0x06c0, 0x0004,
558 0x02eb, 0x00d3, 0x00d2, 0x00d0, 0x0172, 0x017b, 0x02de, 0x02d3,
559 0x02ca, 0x06c7, 0x0373, 0x036d, 0x036c, 0x0d83, 0x0361, 0x0002,
560 0x0179, 0x0171, 0x0066, 0x00bb, 0x02d6, 0x02d2, 0x0166, 0x02c7,
561 0x02c5, 0x0362, 0x06c6, 0x0367, 0x0d82, 0x0366, 0x01b2, 0x0000,
562 0x000c, 0x000a, 0x0007, 0x000b, 0x000a, 0x0011, 0x000b, 0x0009,
563 0x000d, 0x000c, 0x000a, 0x0007, 0x0005, 0x0003, 0x0001, 0x0003,
564};
565
566static const uint8_t mp3_huffbits_16[256] = {
567 1, 4, 6, 8, 9, 9, 10, 10,
568 11, 11, 11, 12, 12, 12, 13, 9,
569 3, 4, 6, 7, 8, 9, 9, 9,
570 10, 10, 10, 11, 12, 11, 12, 8,
571 6, 6, 7, 8, 9, 9, 10, 10,
572 11, 10, 11, 11, 11, 12, 12, 9,
573 8, 7, 8, 9, 9, 10, 10, 10,
574 11, 11, 12, 12, 12, 13, 13, 10,
575 9, 8, 9, 9, 10, 10, 11, 11,
576 11, 12, 12, 12, 13, 13, 13, 9,
577 9, 8, 9, 9, 10, 11, 11, 12,
578 11, 12, 12, 13, 13, 13, 14, 10,
579 10, 9, 9, 10, 11, 11, 11, 11,
580 12, 12, 12, 12, 13, 13, 14, 10,
581 10, 9, 10, 10, 11, 11, 11, 12,
582 12, 13, 13, 13, 13, 15, 15, 10,
583 10, 10, 10, 11, 11, 11, 12, 12,
584 13, 13, 13, 13, 14, 14, 14, 10,
585 11, 10, 10, 11, 11, 12, 12, 13,
586 13, 13, 13, 14, 13, 14, 13, 11,
587 11, 11, 10, 11, 12, 12, 12, 12,
588 13, 14, 14, 14, 15, 15, 14, 10,
589 12, 11, 11, 11, 12, 12, 13, 14,
590 14, 14, 14, 14, 14, 13, 14, 11,
591 12, 12, 12, 12, 12, 13, 13, 13,
592 13, 15, 14, 14, 14, 14, 16, 11,
593 14, 12, 12, 12, 13, 13, 14, 14,
594 14, 16, 15, 15, 15, 17, 15, 11,
595 13, 13, 11, 12, 14, 14, 13, 14,
596 14, 15, 16, 15, 17, 15, 14, 11,
597 9, 8, 8, 9, 9, 10, 10, 10,
598 11, 11, 11, 11, 11, 11, 11, 8,
599};
600
601static const uint16_t mp3_huffcodes_24[256] = {
602 0x000f, 0x000d, 0x002e, 0x0050, 0x0092, 0x0106, 0x00f8, 0x01b2,
603 0x01aa, 0x029d, 0x028d, 0x0289, 0x026d, 0x0205, 0x0408, 0x0058,
604 0x000e, 0x000c, 0x0015, 0x0026, 0x0047, 0x0082, 0x007a, 0x00d8,
605 0x00d1, 0x00c6, 0x0147, 0x0159, 0x013f, 0x0129, 0x0117, 0x002a,
606 0x002f, 0x0016, 0x0029, 0x004a, 0x0044, 0x0080, 0x0078, 0x00dd,
607 0x00cf, 0x00c2, 0x00b6, 0x0154, 0x013b, 0x0127, 0x021d, 0x0012,
608 0x0051, 0x0027, 0x004b, 0x0046, 0x0086, 0x007d, 0x0074, 0x00dc,
609 0x00cc, 0x00be, 0x00b2, 0x0145, 0x0137, 0x0125, 0x010f, 0x0010,
610 0x0093, 0x0048, 0x0045, 0x0087, 0x007f, 0x0076, 0x0070, 0x00d2,
611 0x00c8, 0x00bc, 0x0160, 0x0143, 0x0132, 0x011d, 0x021c, 0x000e,
612 0x0107, 0x0042, 0x0081, 0x007e, 0x0077, 0x0072, 0x00d6, 0x00ca,
613 0x00c0, 0x00b4, 0x0155, 0x013d, 0x012d, 0x0119, 0x0106, 0x000c,
614 0x00f9, 0x007b, 0x0079, 0x0075, 0x0071, 0x00d7, 0x00ce, 0x00c3,
615 0x00b9, 0x015b, 0x014a, 0x0134, 0x0123, 0x0110, 0x0208, 0x000a,
616 0x01b3, 0x0073, 0x006f, 0x006d, 0x00d3, 0x00cb, 0x00c4, 0x00bb,
617 0x0161, 0x014c, 0x0139, 0x012a, 0x011b, 0x0213, 0x017d, 0x0011,
618 0x01ab, 0x00d4, 0x00d0, 0x00cd, 0x00c9, 0x00c1, 0x00ba, 0x00b1,
619 0x00a9, 0x0140, 0x012f, 0x011e, 0x010c, 0x0202, 0x0179, 0x0010,
620 0x014f, 0x00c7, 0x00c5, 0x00bf, 0x00bd, 0x00b5, 0x00ae, 0x014d,
621 0x0141, 0x0131, 0x0121, 0x0113, 0x0209, 0x017b, 0x0173, 0x000b,
622 0x029c, 0x00b8, 0x00b7, 0x00b3, 0x00af, 0x0158, 0x014b, 0x013a,
623 0x0130, 0x0122, 0x0115, 0x0212, 0x017f, 0x0175, 0x016e, 0x000a,
624 0x028c, 0x015a, 0x00ab, 0x00a8, 0x00a4, 0x013e, 0x0135, 0x012b,
625 0x011f, 0x0114, 0x0107, 0x0201, 0x0177, 0x0170, 0x016a, 0x0006,
626 0x0288, 0x0142, 0x013c, 0x0138, 0x0133, 0x012e, 0x0124, 0x011c,
627 0x010d, 0x0105, 0x0200, 0x0178, 0x0172, 0x016c, 0x0167, 0x0004,
628 0x026c, 0x012c, 0x0128, 0x0126, 0x0120, 0x011a, 0x0111, 0x010a,
629 0x0203, 0x017c, 0x0176, 0x0171, 0x016d, 0x0169, 0x0165, 0x0002,
630 0x0409, 0x0118, 0x0116, 0x0112, 0x010b, 0x0108, 0x0103, 0x017e,
631 0x017a, 0x0174, 0x016f, 0x016b, 0x0168, 0x0166, 0x0164, 0x0000,
632 0x002b, 0x0014, 0x0013, 0x0011, 0x000f, 0x000d, 0x000b, 0x0009,
633 0x0007, 0x0006, 0x0004, 0x0007, 0x0005, 0x0003, 0x0001, 0x0003,
634};
635
636static const uint8_t mp3_huffbits_24[256] = {
637 4, 4, 6, 7, 8, 9, 9, 10,
638 10, 11, 11, 11, 11, 11, 12, 9,
639 4, 4, 5, 6, 7, 8, 8, 9,
640 9, 9, 10, 10, 10, 10, 10, 8,
641 6, 5, 6, 7, 7, 8, 8, 9,
642 9, 9, 9, 10, 10, 10, 11, 7,
643 7, 6, 7, 7, 8, 8, 8, 9,
644 9, 9, 9, 10, 10, 10, 10, 7,
645 8, 7, 7, 8, 8, 8, 8, 9,
646 9, 9, 10, 10, 10, 10, 11, 7,
647 9, 7, 8, 8, 8, 8, 9, 9,
648 9, 9, 10, 10, 10, 10, 10, 7,
649 9, 8, 8, 8, 8, 9, 9, 9,
650 9, 10, 10, 10, 10, 10, 11, 7,
651 10, 8, 8, 8, 9, 9, 9, 9,
652 10, 10, 10, 10, 10, 11, 11, 8,
653 10, 9, 9, 9, 9, 9, 9, 9,
654 9, 10, 10, 10, 10, 11, 11, 8,
655 10, 9, 9, 9, 9, 9, 9, 10,
656 10, 10, 10, 10, 11, 11, 11, 8,
657 11, 9, 9, 9, 9, 10, 10, 10,
658 10, 10, 10, 11, 11, 11, 11, 8,
659 11, 10, 9, 9, 9, 10, 10, 10,
660 10, 10, 10, 11, 11, 11, 11, 8,
661 11, 10, 10, 10, 10, 10, 10, 10,
662 10, 10, 11, 11, 11, 11, 11, 8,
663 11, 10, 10, 10, 10, 10, 10, 10,
664 11, 11, 11, 11, 11, 11, 11, 8,
665 12, 10, 10, 10, 10, 10, 10, 11,
666 11, 11, 11, 11, 11, 11, 11, 8,
667 8, 7, 7, 7, 7, 7, 7, 7,
668 7, 7, 7, 8, 8, 8, 8, 4,
669};
670
671static const huff_table_t mp3_huff_tables[16] = {
672{ 1, NULL, NULL },
673{ 2, mp3_huffbits_1, mp3_huffcodes_1 },
674{ 3, mp3_huffbits_2, mp3_huffcodes_2 },
675{ 3, mp3_huffbits_3, mp3_huffcodes_3 },
676{ 4, mp3_huffbits_5, mp3_huffcodes_5 },
677{ 4, mp3_huffbits_6, mp3_huffcodes_6 },
678{ 6, mp3_huffbits_7, mp3_huffcodes_7 },
679{ 6, mp3_huffbits_8, mp3_huffcodes_8 },
680{ 6, mp3_huffbits_9, mp3_huffcodes_9 },
681{ 8, mp3_huffbits_10, mp3_huffcodes_10 },
682{ 8, mp3_huffbits_11, mp3_huffcodes_11 },
683{ 8, mp3_huffbits_12, mp3_huffcodes_12 },
684{ 16, mp3_huffbits_13, mp3_huffcodes_13 },
685{ 16, mp3_huffbits_15, mp3_huffcodes_15 },
686{ 16, mp3_huffbits_16, mp3_huffcodes_16 },
687{ 16, mp3_huffbits_24, mp3_huffcodes_24 },
688};
689
690static const uint8_t mp3_huff_data[32][2] = {
691{ 0, 0 },
692{ 1, 0 },
693{ 2, 0 },
694{ 3, 0 },
695{ 0, 0 },
696{ 4, 0 },
697{ 5, 0 },
698{ 6, 0 },
699{ 7, 0 },
700{ 8, 0 },
701{ 9, 0 },
702{ 10, 0 },
703{ 11, 0 },
704{ 12, 0 },
705{ 0, 0 },
706{ 13, 0 },
707{ 14, 1 },
708{ 14, 2 },
709{ 14, 3 },
710{ 14, 4 },
711{ 14, 6 },
712{ 14, 8 },
713{ 14, 10 },
714{ 14, 13 },
715{ 15, 4 },
716{ 15, 5 },
717{ 15, 6 },
718{ 15, 7 },
719{ 15, 8 },
720{ 15, 9 },
721{ 15, 11 },
722{ 15, 13 },
723};
724
725static const uint8_t mp3_quad_codes[2][16] = {
726 { 1, 5, 4, 5, 6, 5, 4, 4, 7, 3, 6, 0, 7, 2, 3, 1, },
727 { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, },
728};
729
730static const uint8_t mp3_quad_bits[2][16] = {
731 { 1, 4, 4, 5, 4, 6, 5, 6, 4, 5, 5, 6, 5, 6, 6, 6, },
732 { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, },
733};
734
735static const uint8_t band_size_long[9][22] = {
736{ 4, 4, 4, 4, 4, 4, 6, 6, 8, 8, 10,
737 12, 16, 20, 24, 28, 34, 42, 50, 54, 76, 158, }, /* 44100 */
738{ 4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 10,
739 12, 16, 18, 22, 28, 34, 40, 46, 54, 54, 192, }, /* 48000 */
740{ 4, 4, 4, 4, 4, 4, 6, 6, 8, 10, 12,
741 16, 20, 24, 30, 38, 46, 56, 68, 84, 102, 26, }, /* 32000 */
742{ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
743 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, }, /* 22050 */
744{ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
745 18, 22, 26, 32, 38, 46, 52, 64, 70, 76, 36, }, /* 24000 */
746{ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
747 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, }, /* 16000 */
748{ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
749 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, }, /* 11025 */
750{ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
751 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, }, /* 12000 */
752{ 12, 12, 12, 12, 12, 12, 16, 20, 24, 28, 32,
753 40, 48, 56, 64, 76, 90, 2, 2, 2, 2, 2, }, /* 8000 */
754};
755
756static const uint8_t band_size_short[9][13] = {
757{ 4, 4, 4, 4, 6, 8, 10, 12, 14, 18, 22, 30, 56, }, /* 44100 */
758{ 4, 4, 4, 4, 6, 6, 10, 12, 14, 16, 20, 26, 66, }, /* 48000 */
759{ 4, 4, 4, 4, 6, 8, 12, 16, 20, 26, 34, 42, 12, }, /* 32000 */
760{ 4, 4, 4, 6, 6, 8, 10, 14, 18, 26, 32, 42, 18, }, /* 22050 */
761{ 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 32, 44, 12, }, /* 24000 */
762{ 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18, }, /* 16000 */
763{ 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18, }, /* 11025 */
764{ 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18, }, /* 12000 */
765{ 8, 8, 8, 12, 16, 20, 24, 28, 36, 2, 2, 2, 26, }, /* 8000 */
766};
767
768static const uint8_t mp3_pretab[2][22] = {
769 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
770 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0 },
771};
772
773static const float ci_table[8] = {
774 -0.6f, -0.535f, -0.33f, -0.185f, -0.095f, -0.041f, -0.0142f, -0.0037f,
775};
776
777#define C1 FIXHR(0.98480775301220805936/2)
778#define C2 FIXHR(0.93969262078590838405/2)
779#define C3 FIXHR(0.86602540378443864676/2)
780#define C4 FIXHR(0.76604444311897803520/2)
781#define C5 FIXHR(0.64278760968653932632/2)
782#define C6 FIXHR(0.5/2)
783#define C7 FIXHR(0.34202014332566873304/2)
784#define C8 FIXHR(0.17364817766693034885/2)
785
786static const int icos36[9] = {
787 FIXR(0.50190991877167369479),
788 FIXR(0.51763809020504152469), //0
789 FIXR(0.55168895948124587824),
790 FIXR(0.61038729438072803416),
791 FIXR(0.70710678118654752439), //1
792 FIXR(0.87172339781054900991),
793 FIXR(1.18310079157624925896),
794 FIXR(1.93185165257813657349), //2
795 FIXR(5.73685662283492756461),
796};
797
798static const int icos36h[9] = {
799 FIXHR(0.50190991877167369479/2),
800 FIXHR(0.51763809020504152469/2), //0
801 FIXHR(0.55168895948124587824/2),
802 FIXHR(0.61038729438072803416/2),
803 FIXHR(0.70710678118654752439/2), //1
804 FIXHR(0.87172339781054900991/2),
805 FIXHR(1.18310079157624925896/4),
806 FIXHR(1.93185165257813657349/4), //2
807// FIXHR(5.73685662283492756461),
808};
809
811
812static INLINE int unaligned32_be(const uint8_t *p)
813{
814 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
815}
816
817#define MIN_CACHE_BITS 25
818
819#define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
820#define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
821
822#define OPEN_READER(name, gb) \
823 int name##_index= (gb)->index;\
824 int name##_cache= 0;\
825
826#define CLOSE_READER(name, gb)\
827 (gb)->index= name##_index;\
828
829#define UPDATE_CACHE(name, gb)\
830 name##_cache= unaligned32_be(&((gb)->buffer[name##_index>>3])) << (name##_index&0x07); \
831
832#define SKIP_CACHE(name, gb, num)\
833 name##_cache <<= (num);
834
835#define SKIP_COUNTER(name, gb, num)\
836 name##_index += (num);\
837
838#define SKIP_BITS(name, gb, num)\
839 {\
840 SKIP_CACHE(name, gb, num)\
841 SKIP_COUNTER(name, gb, num)\
842 }\
843
844#define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
845#define LAST_SKIP_CACHE(name, gb, num) ;
846
847#define SHOW_UBITS(name, gb, num)\
848 NEG_USR32(name##_cache, num)
849
850#define SHOW_SBITS(name, gb, num)\
851 NEG_SSR32(name##_cache, num)
852
853#define GET_CACHE(name, gb)\
854 ((uint32_t)name##_cache)
855
856static INLINE int get_bits_count(bitstream_t *s){
857 return s->index;
858}
859
860static INLINE void skip_bits_long(bitstream_t *s, int n){
861 s->index += n;
862}
863#define skip_bits skip_bits_long
864
865static void init_get_bits(bitstream_t *s, const uint8_t *buffer, int bit_size) {
866 int buffer_size= (bit_size+7)>>3;
867 if(buffer_size < 0 || bit_size < 0) {
868 buffer_size = bit_size = 0;
869 buffer = NULL;
870 }
871 s->buffer= buffer;
872 s->size_in_bits= bit_size;
873 s->buffer_end= buffer + buffer_size;
874 s->index=0;
875}
876
877static INLINE unsigned int get_bits(bitstream_t *s, int n){
878 register int tmp;
879 OPEN_READER(re, s)
880 UPDATE_CACHE(re, s)
881 tmp= SHOW_UBITS(re, s, n);
882 LAST_SKIP_BITS(re, s, n)
883 CLOSE_READER(re, s)
884 return tmp;
885}
886
887static INLINE int get_bitsz(bitstream_t *s, int n)
888{
889 if (n == 0)
890 return 0;
891 else
892 return get_bits(s, n);
893}
894
895static INLINE unsigned int get_bits1(bitstream_t *s){
896 int index= s->index;
897 uint8_t result= s->buffer[ index>>3 ];
898 result<<= (index&0x07);
899 result>>= 8 - 1;
900 index++;
901 s->index= index;
902 return result;
903}
904
905static INLINE void align_get_bits(bitstream_t *s)
906{
907 int n= (-get_bits_count(s)) & 7;
908 if(n) skip_bits(s, n);
909}
910
911#define GET_DATA(v, table, i, wrap, size) \
912{\
913 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
914 switch(size) {\
915 case 1:\
916 v = *(const uint8_t *)ptr;\
917 break;\
918 case 2:\
919 v = *(const uint16_t *)ptr;\
920 break;\
921 default:\
922 v = *(const uint32_t *)ptr;\
923 break;\
924 }\
925}
926
927static INLINE int alloc_table(vlc_t *vlc, int size) {
928 int index;
929 index = vlc->table_size;
930 vlc->table_size += size;
931 if (vlc->table_size > vlc->table_allocated) {
932 vlc->table_allocated += (1 << vlc->bits);
933 vlc->table = libc_realloc(vlc->table, sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
934 if (!vlc->table)
935 return -1;
936 }
937 return index;
938}
939
940static int build_table(
941 vlc_t *vlc, int table_nb_bits,
942 int nb_codes,
943 const void *bits, int bits_wrap, int bits_size,
944 const void *codes, int codes_wrap, int codes_size,
945 uint32_t code_prefix, int n_prefix
946) {
947 int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2;
948 uint32_t code;
949 VLC_TYPE (*table)[2];
950
951 table_size = 1 << table_nb_bits;
952 table_index = alloc_table(vlc, table_size);
953 if (table_index < 0)
954 return -1;
955 table = &vlc->table[table_index];
956
957 for(i=0;i<table_size;i++) {
958 table[i][1] = 0; //bits
959 table[i][0] = -1; //codes
960 }
961
962 for(i=0;i<nb_codes;i++) {
963 GET_DATA(n, bits, i, bits_wrap, bits_size);
964 GET_DATA(code, codes, i, codes_wrap, codes_size);
965 if (n <= 0)
966 continue;
967 n -= n_prefix;
968 code_prefix2= code >> n;
969 if (n > 0 && (unsigned)code_prefix2 == code_prefix) {
970 if (n <= table_nb_bits) {
971 j = (code << (table_nb_bits - n)) & (table_size - 1);
972 nb = 1 << (table_nb_bits - n);
973 for(k=0;k<nb;k++) {
974 if (table[j][1] /*bits*/ != 0) {
975 return -1;
976 }
977 table[j][1] = n; //bits
978 table[j][0] = i; //code
979 j++;
980 }
981 } else {
982 n -= table_nb_bits;
983 j = (code >> n) & ((1 << table_nb_bits) - 1);
984 n1 = -table[j][1]; //bits
985 if (n > n1)
986 n1 = n;
987 table[j][1] = -n1; //bits
988 }
989 }
990 }
991 for(i=0;i<table_size;i++) {
992 n = table[i][1]; //bits
993 if (n < 0) {
994 n = -n;
995 if (n > table_nb_bits) {
996 n = table_nb_bits;
997 table[i][1] = -n; //bits
998 }
999 index = build_table(vlc, n, nb_codes,
1000 bits, bits_wrap, bits_size,
1001 codes, codes_wrap, codes_size,
1002 (code_prefix << table_nb_bits) | i,
1003 n_prefix + table_nb_bits);
1004 if (index < 0)
1005 return -1;
1006 table = &vlc->table[table_index];
1007 table[i][0] = index; //code
1008 }
1009 }
1010 return table_index;
1011}
1012
1013static INLINE int init_vlc(
1014 vlc_t *vlc, int nb_bits, int nb_codes,
1015 const void *bits, int bits_wrap, int bits_size,
1016 const void *codes, int codes_wrap, int codes_size
1017) {
1018 vlc->bits = nb_bits;
1019 if (build_table(vlc, nb_bits, nb_codes,
1020 bits, bits_wrap, bits_size,
1021 codes, codes_wrap, codes_size,
1022 0, 0) < 0) {
1023 libc_free(vlc->table);
1024 return -1;
1025 }
1026 return 0;
1027}
1028
1029#define GET_VLC(code, name, gb, table, bits, max_depth)\
1030{\
1031 int n, index, nb_bits;\
1032\
1033 index= SHOW_UBITS(name, gb, bits);\
1034 code = table[index][0];\
1035 n = table[index][1];\
1036\
1037 if(max_depth > 1 && n < 0){\
1038 LAST_SKIP_BITS(name, gb, bits)\
1039 UPDATE_CACHE(name, gb)\
1040\
1041 nb_bits = -n;\
1042\
1043 index= SHOW_UBITS(name, gb, nb_bits) + code;\
1044 code = table[index][0];\
1045 n = table[index][1];\
1046 if(max_depth > 2 && n < 0){\
1047 LAST_SKIP_BITS(name, gb, nb_bits)\
1048 UPDATE_CACHE(name, gb)\
1049\
1050 nb_bits = -n;\
1051\
1052 index= SHOW_UBITS(name, gb, nb_bits) + code;\
1053 code = table[index][0];\
1054 n = table[index][1];\
1055 }\
1056 }\
1057 SKIP_BITS(name, gb, n)\
1058}
1059
1060static INLINE int get_vlc2(bitstream_t *s, VLC_TYPE (*table)[2], int bits, int max_depth) {
1061 int code;
1062
1063 OPEN_READER(re, s)
1064 UPDATE_CACHE(re, s)
1065
1066 GET_VLC(code, re, s, table, bits, max_depth)
1067
1068 CLOSE_READER(re, s)
1069 return code;
1070}
1071
1072static void switch_buffer(mp3_context_t *s, int *pos, int *end_pos, int *end_pos2) {
1073 if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1074 s->gb= s->in_gb;
1075 s->in_gb.buffer=NULL;
1076 skip_bits_long(&s->gb, *pos - *end_pos);
1077 *end_pos2=
1078 *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1079 *pos= get_bits_count(&s->gb);
1080 }
1081}
1082
1084
1085static INLINE int mp3_check_header(uint32_t header){
1086 /* header */
1087 if ((header & 0xffe00000) != 0xffe00000)
1088 return -1;
1089 /* layer check */
1090 if ((header & (3<<17)) != (1 << 17))
1091 return -1;
1092 /* bit rate */
1093 if ((header & (0xf<<12)) == 0xf<<12)
1094 return -1;
1095 /* frequency */
1096 if ((header & (3<<10)) == 3<<10)
1097 return -1;
1098 return 0;
1099}
1100
1101
1102static void lsf_sf_expand(
1103 int *slen, int sf, int n1, int n2, int n3
1104) {
1105 if (n3) {
1106 slen[3] = sf % n3;
1107 sf /= n3;
1108 } else {
1109 slen[3] = 0;
1110 }
1111 if (n2) {
1112 slen[2] = sf % n2;
1113 sf /= n2;
1114 } else {
1115 slen[2] = 0;
1116 }
1117 slen[1] = sf % n1;
1118 sf /= n1;
1119 slen[0] = sf;
1120}
1121
1122static INLINE int l3_unscale(int value, int exponent)
1123{
1124 unsigned int m;
1125 int e;
1126
1127 e = table_4_3_exp [4*value + (exponent&3)];
1128 m = table_4_3_value[4*value + (exponent&3)];
1129 e -= (exponent >> 2);
1130 if (e > 31)
1131 return 0;
1132 m = (m + (1 << (e-1))) >> e;
1133
1134 return m;
1135}
1136
1137static INLINE int round_sample(int *sum) {
1138 int sum1;
1139 sum1 = (*sum) >> OUT_SHIFT;
1140 *sum &= (1<<OUT_SHIFT)-1;
1141 if (sum1 < OUT_MIN)
1142 sum1 = OUT_MIN;
1143 else if (sum1 > OUT_MAX)
1144 sum1 = OUT_MAX;
1145 return sum1;
1146}
1147
1148static void exponents_from_scale_factors(
1149 mp3_context_t *s, granule_t *g, int16_t *exponents
1150) {
1151 const uint8_t *bstab, *pretab;
1152 int len, i, j, k, l, v0, shift, gain, gains[3];
1153 int16_t *exp_ptr;
1154
1155 exp_ptr = exponents;
1156 gain = g->global_gain - 210;
1157 shift = g->scalefac_scale + 1;
1158
1159 bstab = band_size_long[s->sample_rate_index];
1160 pretab = mp3_pretab[g->preflag];
1161 for(i=0;i<g->long_end;i++) {
1162 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1163 len = bstab[i];
1164 for(j=len;j>0;j--)
1165 *exp_ptr++ = v0;
1166 }
1167
1168 if (g->short_start < 13) {
1169 bstab = band_size_short[s->sample_rate_index];
1170 gains[0] = gain - (g->subblock_gain[0] << 3);
1171 gains[1] = gain - (g->subblock_gain[1] << 3);
1172 gains[2] = gain - (g->subblock_gain[2] << 3);
1173 k = g->long_end;
1174 for(i=g->short_start;i<13;i++) {
1175 len = bstab[i];
1176 for(l=0;l<3;l++) {
1177 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1178 for(j=len;j>0;j--)
1179 *exp_ptr++ = v0;
1180 }
1181 }
1182 }
1183}
1184
1185static void reorder_block(mp3_context_t *s, granule_t *g)
1186{
1187 int i, j, len;
1188 int32_t *ptr, *dst, *ptr1;
1189 int32_t tmp[576];
1190
1191 if (g->block_type != 2)
1192 return;
1193
1194 if (g->switch_point) {
1195 if (s->sample_rate_index != 8) {
1196 ptr = g->sb_hybrid + 36;
1197 } else {
1198 ptr = g->sb_hybrid + 48;
1199 }
1200 } else {
1201 ptr = g->sb_hybrid;
1202 }
1203
1204 for(i=g->short_start;i<13;i++) {
1205 len = band_size_short[s->sample_rate_index][i];
1206 ptr1 = ptr;
1207 dst = tmp;
1208 for(j=len;j>0;j--) {
1209 *dst++ = ptr[0*len];
1210 *dst++ = ptr[1*len];
1211 *dst++ = ptr[2*len];
1212 ptr++;
1213 }
1214 ptr+=2*len;
1215 libc_memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1216 }
1217}
1218
1219static void compute_antialias(mp3_context_t *s, granule_t *g) {
1220 int32_t *ptr, *csa;
1221 int n, i;
1222
1223 (void)s;
1224
1225 /* we antialias only "long" bands */
1226 if (g->block_type == 2) {
1227 if (!g->switch_point)
1228 return;
1229 /* XXX: check this for 8000Hz case */
1230 n = 1;
1231 } else {
1232 n = SBLIMIT - 1;
1233 }
1234
1235 ptr = g->sb_hybrid + 18;
1236 for(i = n;i > 0;i--) {
1237 int tmp0, tmp1, tmp2;
1238 csa = &csa_table[0][0];
1239#define INT_AA(j) \
1240 tmp0 = ptr[-1-j];\
1241 tmp1 = ptr[ j];\
1242 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1243 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1244 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1245
1246 INT_AA(0)
1247 INT_AA(1)
1248 INT_AA(2)
1249 INT_AA(3)
1250 INT_AA(4)
1251 INT_AA(5)
1252 INT_AA(6)
1253 INT_AA(7)
1254
1255 ptr += 18;
1256 }
1257}
1258
1259static void compute_stereo(
1260 mp3_context_t *s, granule_t *g0, granule_t *g1
1261) {
1262 int i, j, k, l;
1263 int32_t v1, v2;
1264 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1265 int32_t (*is_tab)[16];
1266 int32_t *tab0, *tab1;
1267 int non_zero_found_short[3];
1268
1269 if (s->mode_ext & MODE_EXT_I_STEREO) {
1270 if (!s->lsf) {
1271 is_tab = is_table;
1272 sf_max = 7;
1273 } else {
1274 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1275 sf_max = 16;
1276 }
1277
1278 tab0 = g0->sb_hybrid + 576;
1279 tab1 = g1->sb_hybrid + 576;
1280
1281 non_zero_found_short[0] = 0;
1282 non_zero_found_short[1] = 0;
1283 non_zero_found_short[2] = 0;
1284 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1285 for(i = 12;i >= g1->short_start;i--) {
1286 /* for last band, use previous scale factor */
1287 if (i != 11)
1288 k -= 3;
1289 len = band_size_short[s->sample_rate_index][i];
1290 for(l=2;l>=0;l--) {
1291 tab0 -= len;
1292 tab1 -= len;
1293 if (!non_zero_found_short[l]) {
1294 /* test if non zero band. if so, stop doing i-stereo */
1295 for(j=0;j<len;j++) {
1296 if (tab1[j] != 0) {
1297 non_zero_found_short[l] = 1;
1298 goto found1;
1299 }
1300 }
1301 sf = g1->scale_factors[k + l];
1302 if (sf >= sf_max)
1303 goto found1;
1304
1305 v1 = is_tab[0][sf];
1306 v2 = is_tab[1][sf];
1307 for(j=0;j<len;j++) {
1308 tmp0 = tab0[j];
1309 tab0[j] = MULL(tmp0, v1);
1310 tab1[j] = MULL(tmp0, v2);
1311 }
1312 } else {
1313 found1:
1314 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1315 /* lower part of the spectrum : do ms stereo
1316 if enabled */
1317 for(j=0;j<len;j++) {
1318 tmp0 = tab0[j];
1319 tmp1 = tab1[j];
1320 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1321 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1322 }
1323 }
1324 }
1325 }
1326 }
1327
1328 non_zero_found = non_zero_found_short[0] |
1329 non_zero_found_short[1] |
1330 non_zero_found_short[2];
1331
1332 for(i = g1->long_end - 1;i >= 0;i--) {
1333 len = band_size_long[s->sample_rate_index][i];
1334 tab0 -= len;
1335 tab1 -= len;
1336 /* test if non zero band. if so, stop doing i-stereo */
1337 if (!non_zero_found) {
1338 for(j=0;j<len;j++) {
1339 if (tab1[j] != 0) {
1340 non_zero_found = 1;
1341 goto found2;
1342 }
1343 }
1344 /* for last band, use previous scale factor */
1345 k = (i == 21) ? 20 : i;
1346 sf = g1->scale_factors[k];
1347 if (sf >= sf_max)
1348 goto found2;
1349 v1 = is_tab[0][sf];
1350 v2 = is_tab[1][sf];
1351 for(j=0;j<len;j++) {
1352 tmp0 = tab0[j];
1353 tab0[j] = MULL(tmp0, v1);
1354 tab1[j] = MULL(tmp0, v2);
1355 }
1356 } else {
1357 found2:
1358 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1359 /* lower part of the spectrum : do ms stereo
1360 if enabled */
1361 for(j=0;j<len;j++) {
1362 tmp0 = tab0[j];
1363 tmp1 = tab1[j];
1364 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1365 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1366 }
1367 }
1368 }
1369 }
1370 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1371 /* ms stereo ONLY */
1372 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1373 global gain */
1374 tab0 = g0->sb_hybrid;
1375 tab1 = g1->sb_hybrid;
1376 for(i=0;i<576;i++) {
1377 tmp0 = tab0[i];
1378 tmp1 = tab1[i];
1379 tab0[i] = tmp0 + tmp1;
1380 tab1[i] = tmp0 - tmp1;
1381 }
1382 }
1383}
1384
1385static int huffman_decode(
1386 mp3_context_t *s, granule_t *g, int16_t *exponents, int end_pos2
1387) {
1388 int s_index;
1389 int i;
1390 int last_pos, bits_left;
1391 vlc_t *vlc;
1392 int end_pos= s->gb.size_in_bits;
1393 if (end_pos2 < end_pos) end_pos = end_pos2;
1394
1395 /* low frequencies (called big values) */
1396 s_index = 0;
1397 for(i=0;i<3;i++) {
1398 int j, k, l, linbits;
1399 j = g->region_size[i];
1400 if (j == 0)
1401 continue;
1402 /* select vlc table */
1403 k = g->table_select[i];
1404 l = mp3_huff_data[k][0];
1405 linbits = mp3_huff_data[k][1];
1406 vlc = &huff_vlc[l];
1407
1408 if(!l){
1409 libc_memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1410 s_index += 2*j;
1411 continue;
1412 }
1413
1414 /* read huffcode and compute each couple */
1415 for(;j>0;j--) {
1416 int exponent, x, y, v;
1417 int pos= get_bits_count(&s->gb);
1418
1419 if (pos >= end_pos){
1420 switch_buffer(s, &pos, &end_pos, &end_pos2);
1421 if(pos >= end_pos)
1422 break;
1423 }
1424 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1425
1426 if(!y){
1427 g->sb_hybrid[s_index ] =
1428 g->sb_hybrid[s_index+1] = 0;
1429 s_index += 2;
1430 continue;
1431 }
1432
1433 exponent= exponents[s_index];
1434
1435 if(y&16){
1436 x = y >> 5;
1437 y = y & 0x0f;
1438 if (x < 15){
1439 v = expval_table[ exponent ][ x ];
1440 }else{
1441 x += get_bitsz(&s->gb, linbits);
1442 v = l3_unscale(x, exponent);
1443 }
1444 if (get_bits1(&s->gb))
1445 v = -v;
1446 g->sb_hybrid[s_index] = v;
1447 if (y < 15){
1448 v = expval_table[ exponent ][ y ];
1449 }else{
1450 y += get_bitsz(&s->gb, linbits);
1451 v = l3_unscale(y, exponent);
1452 }
1453 if (get_bits1(&s->gb))
1454 v = -v;
1455 g->sb_hybrid[s_index+1] = v;
1456 }else{
1457 x = y >> 5;
1458 y = y & 0x0f;
1459 x += y;
1460 if (x < 15){
1461 v = expval_table[ exponent ][ x ];
1462 }else{
1463 x += get_bitsz(&s->gb, linbits);
1464 v = l3_unscale(x, exponent);
1465 }
1466 if (get_bits1(&s->gb))
1467 v = -v;
1468 g->sb_hybrid[s_index+!!y] = v;
1469 g->sb_hybrid[s_index+ !y] = 0;
1470 }
1471 s_index+=2;
1472 }
1473 }
1474
1475 /* high frequencies */
1476 vlc = &huff_quad_vlc[g->count1table_select];
1477 last_pos=0;
1478 while (s_index <= 572) {
1479 int pos, code;
1480 pos = get_bits_count(&s->gb);
1481 if (pos >= end_pos) {
1482 if (pos > end_pos2 && last_pos){
1483 /* some encoders generate an incorrect size for this
1484 part. We must go back into the data */
1485 s_index -= 4;
1486 skip_bits_long(&s->gb, last_pos - pos);
1487 break;
1488 }
1489 switch_buffer(s, &pos, &end_pos, &end_pos2);
1490 if(pos >= end_pos)
1491 break;
1492 }
1493 last_pos= pos;
1494
1495 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1496 g->sb_hybrid[s_index+0]=
1497 g->sb_hybrid[s_index+1]=
1498 g->sb_hybrid[s_index+2]=
1499 g->sb_hybrid[s_index+3]= 0;
1500 while(code){
1501 static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1502 int v;
1503 int pos= s_index+idxtab[code];
1504 code ^= 8>>idxtab[code];
1505 v = exp_table[ exponents[pos] ];
1506 if(get_bits1(&s->gb))
1507 v = -v;
1508 g->sb_hybrid[pos] = v;
1509 }
1510 s_index+=4;
1511 }
1512 libc_memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1513
1514 /* skip extension bits */
1515 bits_left = end_pos2 - get_bits_count(&s->gb);
1516 if (bits_left < 0) {
1517 return -1;
1518 }
1519 skip_bits_long(&s->gb, bits_left);
1520
1521 i= get_bits_count(&s->gb);
1522 switch_buffer(s, &i, &end_pos, &end_pos2);
1523
1524 return 0;
1525}
1526
1528
1529static void imdct12(int *out, int *in)
1530{
1531 int in0, in1, in2, in3, in4, in5, t1, t2;
1532
1533 in0= in[0*3];
1534 in1= in[1*3] + in[0*3];
1535 in2= in[2*3] + in[1*3];
1536 in3= in[3*3] + in[2*3];
1537 in4= in[4*3] + in[3*3];
1538 in5= in[5*3] + in[4*3];
1539 in5 += in3;
1540 in3 += in1;
1541
1542 in2= MULH(2*in2, C3);
1543 in3= MULH(4*in3, C3);
1544
1545 t1 = in0 - in4;
1546 t2 = MULH(2*(in1 - in5), icos36h[4]);
1547
1548 out[ 7]=
1549 out[10]= t1 + t2;
1550 out[ 1]=
1551 out[ 4]= t1 - t2;
1552
1553 in0 += in4>>1;
1554 in4 = in0 + in2;
1555 in5 += 2*in1;
1556 in1 = MULH(in5 + in3, icos36h[1]);
1557 out[ 8]=
1558 out[ 9]= in4 + in1;
1559 out[ 2]=
1560 out[ 3]= in4 - in1;
1561
1562 in0 -= in2;
1563 in5 = MULH(2*(in5 - in3), icos36h[7]);
1564 out[ 0]=
1565 out[ 5]= in0 - in5;
1566 out[ 6]=
1567 out[11]= in0 + in5;
1568}
1569
1570static void imdct36(int *out, int *buf, int *in, int *win)
1571{
1572 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1573 int tmp[18], *tmp1, *in1;
1574
1575 for(i=17;i>=1;i--)
1576 in[i] += in[i-1];
1577 for(i=17;i>=3;i-=2)
1578 in[i] += in[i-2];
1579
1580 for(j=0;j<2;j++) {
1581 tmp1 = tmp + j;
1582 in1 = in + j;
1583 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1584
1585 t3 = in1[2*0] + (in1[2*6]>>1);
1586 t1 = in1[2*0] - in1[2*6];
1587 tmp1[ 6] = t1 - (t2>>1);
1588 tmp1[16] = t1 + t2;
1589
1590 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1591 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1592 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1593
1594 tmp1[10] = t3 - t0 - t2;
1595 tmp1[ 2] = t3 + t0 + t1;
1596 tmp1[14] = t3 + t2 - t1;
1597
1598 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1599 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1600 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1601 t0 = MULH(2*in1[2*3], C3);
1602
1603 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1604
1605 tmp1[ 0] = t2 + t3 + t0;
1606 tmp1[12] = t2 + t1 - t0;
1607 tmp1[ 8] = t3 - t1 - t0;
1608 }
1609
1610 i = 0;
1611 for(j=0;j<4;j++) {
1612 t0 = tmp[i];
1613 t1 = tmp[i + 2];
1614 s0 = t1 + t0;
1615 s2 = t1 - t0;
1616
1617 t2 = tmp[i + 1];
1618 t3 = tmp[i + 3];
1619 s1 = MULH(2*(t3 + t2), icos36h[j]);
1620 s3 = MULL(t3 - t2, icos36[8 - j]);
1621
1622 t0 = s0 + s1;
1623 t1 = s0 - s1;
1624 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1625 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1626 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1627 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1628
1629 t0 = s2 + s3;
1630 t1 = s2 - s3;
1631 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1632 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1633 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1634 buf[ + j] = MULH(t0, win[18 + j]);
1635 i += 4;
1636 }
1637
1638 s0 = tmp[16];
1639 s1 = MULH(2*tmp[17], icos36h[4]);
1640 t0 = s0 + s1;
1641 t1 = s0 - s1;
1642 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1643 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1644 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1645 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1646}
1647
1648static void compute_imdct(
1649 mp3_context_t *s, granule_t *g, int32_t *sb_samples, int32_t *mdct_buf
1650) {
1651 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
1652 int32_t out2[12];
1653 int i, j, mdct_long_end, v, sblimit;
1654
1655 (void)s;
1656
1657 /* find last non zero block */
1658 ptr = g->sb_hybrid + 576;
1659 ptr1 = g->sb_hybrid + 2 * 18;
1660 while (ptr >= ptr1) {
1661 ptr -= 6;
1662 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
1663 if (v != 0)
1664 break;
1665 }
1666 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1667
1668 if (g->block_type == 2) {
1669 /* XXX: check for 8000 Hz */
1670 if (g->switch_point)
1671 mdct_long_end = 2;
1672 else
1673 mdct_long_end = 0;
1674 } else {
1675 mdct_long_end = sblimit;
1676 }
1677
1678 buf = mdct_buf;
1679 ptr = g->sb_hybrid;
1680 for(j=0;j<mdct_long_end;j++) {
1681 /* apply window & overlap with previous buffer */
1682 out_ptr = sb_samples + j;
1683 /* select window */
1684 if (g->switch_point && j < 2)
1685 win1 = mdct_win[0];
1686 else
1687 win1 = mdct_win[g->block_type];
1688 /* select frequency inversion */
1689 win = win1 + ((4 * 36) & -(j & 1));
1690 imdct36(out_ptr, buf, ptr, win);
1691 out_ptr += 18*SBLIMIT;
1692 ptr += 18;
1693 buf += 18;
1694 }
1695 for(j=mdct_long_end;j<sblimit;j++) {
1696 /* select frequency inversion */
1697 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1698 out_ptr = sb_samples + j;
1699
1700 for(i=0; i<6; i++){
1701 *out_ptr = buf[i];
1702 out_ptr += SBLIMIT;
1703 }
1704 imdct12(out2, ptr + 0);
1705 for(i=0;i<6;i++) {
1706 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
1707 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
1708 out_ptr += SBLIMIT;
1709 }
1710 imdct12(out2, ptr + 1);
1711 for(i=0;i<6;i++) {
1712 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
1713 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
1714 out_ptr += SBLIMIT;
1715 }
1716 imdct12(out2, ptr + 2);
1717 for(i=0;i<6;i++) {
1718 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
1719 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
1720 buf[i + 6*2] = 0;
1721 }
1722 ptr += 18;
1723 buf += 18;
1724 }
1725 /* zero bands */
1726 for(j=sblimit;j<SBLIMIT;j++) {
1727 /* overlap */
1728 out_ptr = sb_samples + j;
1729 for(i=0;i<18;i++) {
1730 *out_ptr = buf[i];
1731 buf[i] = 0;
1732 out_ptr += SBLIMIT;
1733 }
1734 buf += 18;
1735 }
1736}
1737
1738#define SUM8(sum, op, w, p) \
1739{ \
1740 sum op MULS((w)[0 * 64], p[0 * 64]);\
1741 sum op MULS((w)[1 * 64], p[1 * 64]);\
1742 sum op MULS((w)[2 * 64], p[2 * 64]);\
1743 sum op MULS((w)[3 * 64], p[3 * 64]);\
1744 sum op MULS((w)[4 * 64], p[4 * 64]);\
1745 sum op MULS((w)[5 * 64], p[5 * 64]);\
1746 sum op MULS((w)[6 * 64], p[6 * 64]);\
1747 sum op MULS((w)[7 * 64], p[7 * 64]);\
1748}
1749
1750#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
1751{ \
1752 int tmp;\
1753 tmp = p[0 * 64];\
1754 sum1 op1 MULS((w1)[0 * 64], tmp);\
1755 sum2 op2 MULS((w2)[0 * 64], tmp);\
1756 tmp = p[1 * 64];\
1757 sum1 op1 MULS((w1)[1 * 64], tmp);\
1758 sum2 op2 MULS((w2)[1 * 64], tmp);\
1759 tmp = p[2 * 64];\
1760 sum1 op1 MULS((w1)[2 * 64], tmp);\
1761 sum2 op2 MULS((w2)[2 * 64], tmp);\
1762 tmp = p[3 * 64];\
1763 sum1 op1 MULS((w1)[3 * 64], tmp);\
1764 sum2 op2 MULS((w2)[3 * 64], tmp);\
1765 tmp = p[4 * 64];\
1766 sum1 op1 MULS((w1)[4 * 64], tmp);\
1767 sum2 op2 MULS((w2)[4 * 64], tmp);\
1768 tmp = p[5 * 64];\
1769 sum1 op1 MULS((w1)[5 * 64], tmp);\
1770 sum2 op2 MULS((w2)[5 * 64], tmp);\
1771 tmp = p[6 * 64];\
1772 sum1 op1 MULS((w1)[6 * 64], tmp);\
1773 sum2 op2 MULS((w2)[6 * 64], tmp);\
1774 tmp = p[7 * 64];\
1775 sum1 op1 MULS((w1)[7 * 64], tmp);\
1776 sum2 op2 MULS((w2)[7 * 64], tmp);\
1777}
1778
1779#define COS0_0 FIXHR(0.50060299823519630134/2)
1780#define COS0_1 FIXHR(0.50547095989754365998/2)
1781#define COS0_2 FIXHR(0.51544730992262454697/2)
1782#define COS0_3 FIXHR(0.53104259108978417447/2)
1783#define COS0_4 FIXHR(0.55310389603444452782/2)
1784#define COS0_5 FIXHR(0.58293496820613387367/2)
1785#define COS0_6 FIXHR(0.62250412303566481615/2)
1786#define COS0_7 FIXHR(0.67480834145500574602/2)
1787#define COS0_8 FIXHR(0.74453627100229844977/2)
1788#define COS0_9 FIXHR(0.83934964541552703873/2)
1789#define COS0_10 FIXHR(0.97256823786196069369/2)
1790#define COS0_11 FIXHR(1.16943993343288495515/4)
1791#define COS0_12 FIXHR(1.48416461631416627724/4)
1792#define COS0_13 FIXHR(2.05778100995341155085/8)
1793#define COS0_14 FIXHR(3.40760841846871878570/8)
1794#define COS0_15 FIXHR(10.19000812354805681150/32)
1795
1796#define COS1_0 FIXHR(0.50241928618815570551/2)
1797#define COS1_1 FIXHR(0.52249861493968888062/2)
1798#define COS1_2 FIXHR(0.56694403481635770368/2)
1799#define COS1_3 FIXHR(0.64682178335999012954/2)
1800#define COS1_4 FIXHR(0.78815462345125022473/2)
1801#define COS1_5 FIXHR(1.06067768599034747134/4)
1802#define COS1_6 FIXHR(1.72244709823833392782/4)
1803#define COS1_7 FIXHR(5.10114861868916385802/16)
1804
1805#define COS2_0 FIXHR(0.50979557910415916894/2)
1806#define COS2_1 FIXHR(0.60134488693504528054/2)
1807#define COS2_2 FIXHR(0.89997622313641570463/2)
1808#define COS2_3 FIXHR(2.56291544774150617881/8)
1809
1810#define COS3_0 FIXHR(0.54119610014619698439/2)
1811#define COS3_1 FIXHR(1.30656296487637652785/4)
1812
1813#define COS4_0 FIXHR(0.70710678118654752439/2)
1814
1815#define BF(a, b, c, s)\
1816{\
1817 tmp0 = tab[a] + tab[b];\
1818 tmp1 = tab[a] - tab[b];\
1819 tab[a] = tmp0;\
1820 tab[b] = MULH(tmp1<<(s), c);\
1821}
1822
1823#define BF1(a, b, c, d)\
1824{\
1825 BF(a, b, COS4_0, 1);\
1826 BF(c, d,-COS4_0, 1);\
1827 tab[c] += tab[d];\
1828}
1829
1830#define BF2(a, b, c, d)\
1831{\
1832 BF(a, b, COS4_0, 1);\
1833 BF(c, d,-COS4_0, 1);\
1834 tab[c] += tab[d];\
1835 tab[a] += tab[c];\
1836 tab[c] += tab[b];\
1837 tab[b] += tab[d];\
1838}
1839
1840#define ADD(a, b) tab[a] += tab[b]
1841
1842static void dct32(int32_t *out, int32_t *tab)
1843{
1844 int tmp0, tmp1;
1845
1846 /* pass 1 */
1847 BF( 0, 31, COS0_0 , 1);
1848 BF(15, 16, COS0_15, 5);
1849 /* pass 2 */
1850 BF( 0, 15, COS1_0 , 1);
1851 BF(16, 31,-COS1_0 , 1);
1852 /* pass 1 */
1853 BF( 7, 24, COS0_7 , 1);
1854 BF( 8, 23, COS0_8 , 1);
1855 /* pass 2 */
1856 BF( 7, 8, COS1_7 , 4);
1857 BF(23, 24,-COS1_7 , 4);
1858 /* pass 3 */
1859 BF( 0, 7, COS2_0 , 1);
1860 BF( 8, 15,-COS2_0 , 1);
1861 BF(16, 23, COS2_0 , 1);
1862 BF(24, 31,-COS2_0 , 1);
1863 /* pass 1 */
1864 BF( 3, 28, COS0_3 , 1);
1865 BF(12, 19, COS0_12, 2);
1866 /* pass 2 */
1867 BF( 3, 12, COS1_3 , 1);
1868 BF(19, 28,-COS1_3 , 1);
1869 /* pass 1 */
1870 BF( 4, 27, COS0_4 , 1);
1871 BF(11, 20, COS0_11, 2);
1872 /* pass 2 */
1873 BF( 4, 11, COS1_4 , 1);
1874 BF(20, 27,-COS1_4 , 1);
1875 /* pass 3 */
1876 BF( 3, 4, COS2_3 , 3);
1877 BF(11, 12,-COS2_3 , 3);
1878 BF(19, 20, COS2_3 , 3);
1879 BF(27, 28,-COS2_3 , 3);
1880 /* pass 4 */
1881 BF( 0, 3, COS3_0 , 1);
1882 BF( 4, 7,-COS3_0 , 1);
1883 BF( 8, 11, COS3_0 , 1);
1884 BF(12, 15,-COS3_0 , 1);
1885 BF(16, 19, COS3_0 , 1);
1886 BF(20, 23,-COS3_0 , 1);
1887 BF(24, 27, COS3_0 , 1);
1888 BF(28, 31,-COS3_0 , 1);
1889
1890
1891
1892 /* pass 1 */
1893 BF( 1, 30, COS0_1 , 1);
1894 BF(14, 17, COS0_14, 3);
1895 /* pass 2 */
1896 BF( 1, 14, COS1_1 , 1);
1897 BF(17, 30,-COS1_1 , 1);
1898 /* pass 1 */
1899 BF( 6, 25, COS0_6 , 1);
1900 BF( 9, 22, COS0_9 , 1);
1901 /* pass 2 */
1902 BF( 6, 9, COS1_6 , 2);
1903 BF(22, 25,-COS1_6 , 2);
1904 /* pass 3 */
1905 BF( 1, 6, COS2_1 , 1);
1906 BF( 9, 14,-COS2_1 , 1);
1907 BF(17, 22, COS2_1 , 1);
1908 BF(25, 30,-COS2_1 , 1);
1909
1910 /* pass 1 */
1911 BF( 2, 29, COS0_2 , 1);
1912 BF(13, 18, COS0_13, 3);
1913 /* pass 2 */
1914 BF( 2, 13, COS1_2 , 1);
1915 BF(18, 29,-COS1_2 , 1);
1916 /* pass 1 */
1917 BF( 5, 26, COS0_5 , 1);
1918 BF(10, 21, COS0_10, 1);
1919 /* pass 2 */
1920 BF( 5, 10, COS1_5 , 2);
1921 BF(21, 26,-COS1_5 , 2);
1922 /* pass 3 */
1923 BF( 2, 5, COS2_2 , 1);
1924 BF(10, 13,-COS2_2 , 1);
1925 BF(18, 21, COS2_2 , 1);
1926 BF(26, 29,-COS2_2 , 1);
1927 /* pass 4 */
1928 BF( 1, 2, COS3_1 , 2);
1929 BF( 5, 6,-COS3_1 , 2);
1930 BF( 9, 10, COS3_1 , 2);
1931 BF(13, 14,-COS3_1 , 2);
1932 BF(17, 18, COS3_1 , 2);
1933 BF(21, 22,-COS3_1 , 2);
1934 BF(25, 26, COS3_1 , 2);
1935 BF(29, 30,-COS3_1 , 2);
1936
1937 /* pass 5 */
1938 BF1( 0, 1, 2, 3);
1939 BF2( 4, 5, 6, 7);
1940 BF1( 8, 9, 10, 11);
1941 BF2(12, 13, 14, 15);
1942 BF1(16, 17, 18, 19);
1943 BF2(20, 21, 22, 23);
1944 BF1(24, 25, 26, 27);
1945 BF2(28, 29, 30, 31);
1946
1947 /* pass 6 */
1948
1949 ADD( 8, 12);
1950 ADD(12, 10);
1951 ADD(10, 14);
1952 ADD(14, 9);
1953 ADD( 9, 13);
1954 ADD(13, 11);
1955 ADD(11, 15);
1956
1957 out[ 0] = tab[0];
1958 out[16] = tab[1];
1959 out[ 8] = tab[2];
1960 out[24] = tab[3];
1961 out[ 4] = tab[4];
1962 out[20] = tab[5];
1963 out[12] = tab[6];
1964 out[28] = tab[7];
1965 out[ 2] = tab[8];
1966 out[18] = tab[9];
1967 out[10] = tab[10];
1968 out[26] = tab[11];
1969 out[ 6] = tab[12];
1970 out[22] = tab[13];
1971 out[14] = tab[14];
1972 out[30] = tab[15];
1973
1974 ADD(24, 28);
1975 ADD(28, 26);
1976 ADD(26, 30);
1977 ADD(30, 25);
1978 ADD(25, 29);
1979 ADD(29, 27);
1980 ADD(27, 31);
1981
1982 out[ 1] = tab[16] + tab[24];
1983 out[17] = tab[17] + tab[25];
1984 out[ 9] = tab[18] + tab[26];
1985 out[25] = tab[19] + tab[27];
1986 out[ 5] = tab[20] + tab[28];
1987 out[21] = tab[21] + tab[29];
1988 out[13] = tab[22] + tab[30];
1989 out[29] = tab[23] + tab[31];
1990 out[ 3] = tab[24] + tab[20];
1991 out[19] = tab[25] + tab[21];
1992 out[11] = tab[26] + tab[22];
1993 out[27] = tab[27] + tab[23];
1994 out[ 7] = tab[28] + tab[18];
1995 out[23] = tab[29] + tab[19];
1996 out[15] = tab[30] + tab[17];
1997 out[31] = tab[31];
1998}
1999
2000static void mp3_synth_filter(
2001 int16_t *synth_buf_ptr, int *synth_buf_offset,
2002 int16_t *window, int *dither_state,
2003 int16_t *samples, int incr,
2004 int32_t sb_samples[SBLIMIT]
2005) {
2006 int32_t tmp[32];
2007 register int16_t *synth_buf;
2008 register const int16_t *w, *w2, *p;
2009 int j, offset, v;
2010 int16_t *samples2;
2011 int sum, sum2;
2012
2013 dct32(tmp, sb_samples);
2014
2015 offset = *synth_buf_offset;
2016 synth_buf = synth_buf_ptr + offset;
2017
2018 for(j=0;j<32;j++) {
2019 v = tmp[j];
2020 /* NOTE: can cause a loss in precision if very high amplitude
2021 sound */
2022 if (v > 32767)
2023 v = 32767;
2024 else if (v < -32768)
2025 v = -32768;
2026 synth_buf[j] = v;
2027 }
2028 /* copy to avoid wrap */
2029 libc_memcpy(synth_buf + 512, synth_buf, 32 * sizeof(int16_t));
2030
2031 samples2 = samples + 31 * incr;
2032 w = window;
2033 w2 = window + 31;
2034
2035 sum = *dither_state;
2036 p = synth_buf + 16;
2037 SUM8(sum, +=, w, p);
2038 p = synth_buf + 48;
2039 SUM8(sum, -=, w + 32, p);
2040 *samples = round_sample(&sum);
2041 samples += incr;
2042 w++;
2043
2044 /* we calculate two samples at the same time to avoid one memory
2045 access per two sample */
2046 for(j=1;j<16;j++) {
2047 sum2 = 0;
2048 p = synth_buf + 16 + j;
2049 SUM8P2(sum, +=, sum2, -=, w, w2, p);
2050 p = synth_buf + 48 - j;
2051 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
2052
2053 *samples = round_sample(&sum);
2054 samples += incr;
2055 sum += sum2;
2056 *samples2 = round_sample(&sum);
2057 samples2 -= incr;
2058 w++;
2059 w2--;
2060 }
2061
2062 p = synth_buf + 32;
2063 SUM8(sum, -=, w + 32, p);
2064 *samples = round_sample(&sum);
2065 *dither_state= sum;
2066
2067 offset = (offset - 32) & 511;
2068 *synth_buf_offset = offset;
2069}
2070
2072
2073static int decode_header(mp3_context_t *s, uint32_t header) {
2074 int sample_rate, frame_size, mpeg25, padding;
2075 int sample_rate_index, bitrate_index;
2076 if (header & (1<<20)) {
2077 s->lsf = (header & (1<<19)) ? 0 : 1;
2078 mpeg25 = 0;
2079 } else {
2080 s->lsf = 1;
2081 mpeg25 = 1;
2082 }
2083
2084 sample_rate_index = (header >> 10) & 3;
2085 sample_rate = mp3_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
2086 sample_rate_index += 3 * (s->lsf + mpeg25);
2087 s->sample_rate_index = sample_rate_index;
2088 s->error_protection = ((header >> 16) & 1) ^ 1;
2089 s->sample_rate = sample_rate;
2090
2091 bitrate_index = (header >> 12) & 0xf;
2092 padding = (header >> 9) & 1;
2093 s->mode = (header >> 6) & 3;
2094 s->mode_ext = (header >> 4) & 3;
2095 s->nb_channels = (s->mode == MP3_MONO) ? 1 : 2;
2096
2097 if (bitrate_index != 0) {
2098 frame_size = mp3_bitrate_tab[s->lsf][bitrate_index];
2099 s->bit_rate = frame_size * 1000;
2100 s->frame_size = (frame_size * 144000) / (sample_rate << s->lsf) + padding;
2101 } else {
2102 /* if no frame size computed, signal it */
2103 return 1;
2104 }
2105 return 0;
2106}
2107
2108static int mp_decode_layer3(mp3_context_t *s) {
2109 int nb_granules, main_data_begin, private_bits;
2110 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
2111 granule_t *g;
2112 static granule_t granules[2][2];
2113 static int16_t exponents[576];
2114 const uint8_t *ptr;
2115
2116 (void)private_bits;
2117
2118 if (s->lsf) {
2119 main_data_begin = get_bits(&s->gb, 8);
2120 private_bits = get_bits(&s->gb, s->nb_channels);
2121 nb_granules = 1;
2122 } else {
2123 main_data_begin = get_bits(&s->gb, 9);
2124 if (s->nb_channels == 2)
2125 private_bits = get_bits(&s->gb, 3);
2126 else
2127 private_bits = get_bits(&s->gb, 5);
2128 nb_granules = 2;
2129 for(ch=0;ch<s->nb_channels;ch++) {
2130 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2131 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2132 }
2133 }
2134
2135 for(gr=0;gr<nb_granules;gr++) {
2136 for(ch=0;ch<s->nb_channels;ch++) {
2137 g = &granules[ch][gr];
2138 g->part2_3_length = get_bits(&s->gb, 12);
2139 g->big_values = get_bits(&s->gb, 9);
2140 g->global_gain = get_bits(&s->gb, 8);
2141 /* if MS stereo only is selected, we precompute the
2142 1/sqrt(2) renormalization factor */
2143 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2144 MODE_EXT_MS_STEREO)
2145 g->global_gain -= 2;
2146 if (s->lsf)
2147 g->scalefac_compress = get_bits(&s->gb, 9);
2148 else
2149 g->scalefac_compress = get_bits(&s->gb, 4);
2150 blocksplit_flag = get_bits(&s->gb, 1);
2151 if (blocksplit_flag) {
2152 g->block_type = get_bits(&s->gb, 2);
2153 if (g->block_type == 0)
2154 return -1;
2155 g->switch_point = get_bits(&s->gb, 1);
2156 for(i=0;i<2;i++)
2157 g->table_select[i] = get_bits(&s->gb, 5);
2158 for(i=0;i<3;i++)
2159 g->subblock_gain[i] = get_bits(&s->gb, 3);
2160 /* compute huffman coded region sizes */
2161 if (g->block_type == 2)
2162 g->region_size[0] = (36 / 2);
2163 else {
2164 if (s->sample_rate_index <= 2)
2165 g->region_size[0] = (36 / 2);
2166 else if (s->sample_rate_index != 8)
2167 g->region_size[0] = (54 / 2);
2168 else
2169 g->region_size[0] = (108 / 2);
2170 }
2171 g->region_size[1] = (576 / 2);
2172 } else {
2173 int region_address1, region_address2, l;
2174 g->block_type = 0;
2175 g->switch_point = 0;
2176 for(i=0;i<3;i++)
2177 g->table_select[i] = get_bits(&s->gb, 5);
2178 /* compute huffman coded region sizes */
2179 region_address1 = get_bits(&s->gb, 4);
2180 region_address2 = get_bits(&s->gb, 3);
2181 g->region_size[0] =
2182 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2183 l = region_address1 + region_address2 + 2;
2184 /* should not overflow */
2185 if (l > 22)
2186 l = 22;
2187 g->region_size[1] =
2188 band_index_long[s->sample_rate_index][l] >> 1;
2189 }
2190 /* convert region offsets to region sizes and truncate
2191 size to big_values */
2192 g->region_size[2] = (576 / 2);
2193 j = 0;
2194 for(i=0;i<3;i++) {
2195 k = g->region_size[i];
2196 if (g->big_values < k) k = g->big_values;
2197 g->region_size[i] = k - j;
2198 j = k;
2199 }
2200
2201 /* compute band indexes */
2202 if (g->block_type == 2) {
2203 if (g->switch_point) {
2204 /* if switched mode, we handle the 36 first samples as
2205 long blocks. For 8000Hz, we handle the 48 first
2206 exponents as long blocks (XXX: check this!) */
2207 if (s->sample_rate_index <= 2)
2208 g->long_end = 8;
2209 else if (s->sample_rate_index != 8)
2210 g->long_end = 6;
2211 else
2212 g->long_end = 4; /* 8000 Hz */
2213
2214 g->short_start = 2 + (s->sample_rate_index != 8);
2215 } else {
2216 g->long_end = 0;
2217 g->short_start = 0;
2218 }
2219 } else {
2220 g->short_start = 13;
2221 g->long_end = 22;
2222 }
2223
2224 g->preflag = 0;
2225 if (!s->lsf)
2226 g->preflag = get_bits(&s->gb, 1);
2227 g->scalefac_scale = get_bits(&s->gb, 1);
2228 g->count1table_select = get_bits(&s->gb, 1);
2229 }
2230 }
2231
2232 ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2233 /* now we get bits from the main_data_begin offset */
2234 if(main_data_begin > s->last_buf_size){
2235 s->last_buf_size= main_data_begin;
2236 }
2237
2238 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2239 s->in_gb= s->gb;
2240 init_get_bits(&s->gb, s->last_buf + s->last_buf_size - main_data_begin, main_data_begin*8);
2241
2242 for(gr=0;gr<nb_granules;gr++) {
2243 for(ch=0;ch<s->nb_channels;ch++) {
2244 g = &granules[ch][gr];
2245
2246 bits_pos = get_bits_count(&s->gb);
2247
2248 if (!s->lsf) {
2249 uint8_t *sc;
2250 int slen, slen1, slen2;
2251
2252 /* MPEG1 scale factors */
2253 slen1 = slen_table[0][g->scalefac_compress];
2254 slen2 = slen_table[1][g->scalefac_compress];
2255 if (g->block_type == 2) {
2256 n = g->switch_point ? 17 : 18;
2257 j = 0;
2258 if(slen1){
2259 for(i=0;i<n;i++)
2260 g->scale_factors[j++] = get_bits(&s->gb, slen1);
2261 }else{
2262 libc_memset((void*) &g->scale_factors[j], 0, n);
2263 j += n;
2264// for(i=0;i<n;i++)
2265// g->scale_factors[j++] = 0;
2266 }
2267 if(slen2){
2268 for(i=0;i<18;i++)
2269 g->scale_factors[j++] = get_bits(&s->gb, slen2);
2270 for(i=0;i<3;i++)
2271 g->scale_factors[j++] = 0;
2272 }else{
2273 for(i=0;i<21;i++)
2274 g->scale_factors[j++] = 0;
2275 }
2276 } else {
2277 sc = granules[ch][0].scale_factors;
2278 j = 0;
2279 for(k=0;k<4;k++) {
2280 n = (k == 0 ? 6 : 5);
2281 if ((g->scfsi & (0x8 >> k)) == 0) {
2282 slen = (k < 2) ? slen1 : slen2;
2283 if(slen){
2284 for(i=0;i<n;i++)
2285 g->scale_factors[j++] = get_bits(&s->gb, slen);
2286 }else{
2287 libc_memset((void*) &g->scale_factors[j], 0, n);
2288 j += n;
2289// for(i=0;i<n;i++)
2290// g->scale_factors[j++] = 0;
2291 }
2292 } else {
2293 /* simply copy from last granule */
2294 for(i=0;i<n;i++) {
2295 g->scale_factors[j] = sc[j];
2296 j++;
2297 }
2298 }
2299 }
2300 g->scale_factors[j++] = 0;
2301 }
2302 } else {
2303 int tindex, tindex2, slen[4], sl, sf;
2304
2305 /* LSF scale factors */
2306 if (g->block_type == 2) {
2307 tindex = g->switch_point ? 2 : 1;
2308 } else {
2309 tindex = 0;
2310 }
2311 sf = g->scalefac_compress;
2312 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2313 /* intensity stereo case */
2314 sf >>= 1;
2315 if (sf < 180) {
2316 lsf_sf_expand(slen, sf, 6, 6, 0);
2317 tindex2 = 3;
2318 } else if (sf < 244) {
2319 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2320 tindex2 = 4;
2321 } else {
2322 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2323 tindex2 = 5;
2324 }
2325 } else {
2326 /* normal case */
2327 if (sf < 400) {
2328 lsf_sf_expand(slen, sf, 5, 4, 4);
2329 tindex2 = 0;
2330 } else if (sf < 500) {
2331 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2332 tindex2 = 1;
2333 } else {
2334 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2335 tindex2 = 2;
2336 g->preflag = 1;
2337 }
2338 }
2339
2340 j = 0;
2341 for(k=0;k<4;k++) {
2342 n = lsf_nsf_table[tindex2][tindex][k];
2343 sl = slen[k];
2344 if(sl){
2345 for(i=0;i<n;i++)
2346 g->scale_factors[j++] = get_bits(&s->gb, sl);
2347 }else{
2348 libc_memset((void*) &g->scale_factors[j], 0, n);
2349 j += n;
2350// for(i=0;i<n;i++)
2351// g->scale_factors[j++] = 0;
2352 }
2353 }
2354 /* XXX: should compute exact size */
2355 libc_memset((void*) &g->scale_factors[j], 0, 40 - j);
2356// for(;j<40;j++)
2357// g->scale_factors[j] = 0;
2358 }
2359
2360 exponents_from_scale_factors(s, g, exponents);
2361
2362 /* read Huffman coded residue */
2363 if (huffman_decode(s, g, exponents,
2364 bits_pos + g->part2_3_length) < 0)
2365 return -1;
2366 } /* ch */
2367
2368 if (s->nb_channels == 2)
2369 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2370
2371 for(ch=0;ch<s->nb_channels;ch++) {
2372 g = &granules[ch][gr];
2373 reorder_block(s, g);
2374 compute_antialias(s, g);
2375 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2376 }
2377 } /* gr */
2378 return nb_granules * 18;
2379}
2380
2381static int mp3_decode_main(
2382 mp3_context_t *s,
2383 int16_t *samples, const uint8_t *buf, int buf_size
2384) {
2385 int i, nb_frames, ch;
2386 int16_t *samples_ptr;
2387
2388 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2389
2390 if (s->error_protection)
2391 get_bits(&s->gb, 16);
2392
2393 nb_frames = mp_decode_layer3(s);
2394
2395 s->last_buf_size=0;
2396 if(s->in_gb.buffer){
2397 align_get_bits(&s->gb);
2398 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2399 if(i >= 0 && i <= BACKSTEP_SIZE){
2400 libc_memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2401 s->last_buf_size=i;
2402 }
2403 s->gb= s->in_gb;
2404 }
2405
2406 align_get_bits(&s->gb);
2407 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2408
2409 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2410 i = buf_size - HEADER_SIZE;
2411 if (BACKSTEP_SIZE < i) i = BACKSTEP_SIZE;
2412 }
2413 libc_memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2414 s->last_buf_size += i;
2415
2416 /* apply the synthesis filter */
2417 for(ch=0;ch<s->nb_channels;ch++) {
2418 samples_ptr = samples + ch;
2419 for(i=0;i<nb_frames;i++) {
2420 mp3_synth_filter(
2421 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2422 window, &s->dither_state,
2423 samples_ptr, s->nb_channels,
2424 s->sb_samples[ch][i]
2425 );
2426 samples_ptr += 32 * s->nb_channels;
2427 }
2428 }
2429 return nb_frames * 32 * sizeof(uint16_t) * s->nb_channels;
2430}
2431
2433
2434static int mp3_decode_init(mp3_context_t *s) {
2435 static int init=0;
2436 int i, j, k;
2437
2438 (void)s;
2439
2440 if (!init) {
2441 /* synth init */
2442 for(i=0;i<257;i++) {
2443 int v;
2444 v = mp3_enwindow[i];
2445 #if WFRAC_BITS < 16
2446 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
2447 #endif
2448 window[i] = v;
2449 if ((i & 63) != 0)
2450 v = -v;
2451 if (i != 0)
2452 window[512 - i] = v;
2453 }
2454
2455 /* huffman decode tables */
2456 for(i=1;i<16;i++) {
2457 const huff_table_t *h = &mp3_huff_tables[i];
2458 int xsize, x, y;
2459 unsigned int n;
2460 uint8_t tmp_bits [512];
2461 uint16_t tmp_codes[512];
2462
2463 (void)n;
2464 libc_memset(tmp_bits , 0, sizeof(tmp_bits ));
2465 libc_memset(tmp_codes, 0, sizeof(tmp_codes));
2466
2467 xsize = h->xsize;
2468 n = xsize * xsize;
2469
2470 j = 0;
2471 for(x=0;x<xsize;x++) {
2472 for(y=0;y<xsize;y++){
2473 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
2474 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
2475 }
2476 }
2477
2478 init_vlc(&huff_vlc[i], 7, 512,
2479 tmp_bits, 1, 1, tmp_codes, 2, 2);
2480 }
2481 for(i=0;i<2;i++) {
2482 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
2483 mp3_quad_bits[i], 1, 1, mp3_quad_codes[i], 1, 1);
2484 }
2485
2486 for(i=0;i<9;i++) {
2487 k = 0;
2488 for(j=0;j<22;j++) {
2489 band_index_long[i][j] = k;
2490 k += band_size_long[i][j];
2491 }
2492 band_index_long[i][22] = k;
2493 }
2494
2495 /* compute n ^ (4/3) and store it in mantissa/exp format */
2496 table_4_3_exp= libc_malloc(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]));
2497 if(!table_4_3_exp)
2498 return -1;
2499 table_4_3_value= libc_malloc(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
2500 if(!table_4_3_value)
2501 return -1;
2502
2503 for(i=1;i<TABLE_4_3_SIZE;i++) {
2504 double f, fm;
2505 int e, m;
2506 f = libc_pow((double)(i/4), 4.0 / 3.0) * libc_pow(2, (i&3)*0.25);
2507 fm = libc_frexp(f, &e);
2508 m = (uint32_t)(fm*(1LL<<31) + 0.5);
2509 e+= FRAC_BITS - 31 + 5 - 100;
2510 table_4_3_value[i] = m;
2511 table_4_3_exp[i] = -e;
2512 }
2513 for(i=0; i<512*16; i++){
2514 int exponent= (i>>4);
2515 double f= libc_pow(i&15, 4.0 / 3.0) * libc_pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
2516 expval_table[exponent][i&15]= f;
2517 if((i&15)==1)
2518 exp_table[exponent]= f;
2519 }
2520
2521 for(i=0;i<7;i++) {
2522 float f;
2523 int v;
2524 if (i != 6) {
2525 f = tan((double)i * M_PI / 12.0);
2526 v = FIXR(f / (1.0 + f));
2527 } else {
2528 v = FIXR(1.0);
2529 }
2530 is_table[0][i] = v;
2531 is_table[1][6 - i] = v;
2532 }
2533 for(i=7;i<16;i++)
2534 is_table[0][i] = is_table[1][i] = 0.0;
2535
2536 for(i=0;i<16;i++) {
2537 double f;
2538 int e, k;
2539
2540 for(j=0;j<2;j++) {
2541 e = -(j + 1) * ((i + 1) >> 1);
2542 f = libc_pow(2.0, e / 4.0);
2543 k = i & 1;
2544 is_table_lsf[j][k ^ 1][i] = FIXR(f);
2545 is_table_lsf[j][k][i] = FIXR(1.0);
2546 }
2547 }
2548
2549 for(i=0;i<8;i++) {
2550 float ci, cs, ca;
2551 ci = ci_table[i];
2552 cs = 1.0 / sqrt(1.0 + ci * ci);
2553 ca = cs * ci;
2554 csa_table[i][0] = FIXHR(cs/4);
2555 csa_table[i][1] = FIXHR(ca/4);
2556 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
2557 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
2558 csa_table_float[i][0] = cs;
2559 csa_table_float[i][1] = ca;
2560 csa_table_float[i][2] = ca + cs;
2561 csa_table_float[i][3] = ca - cs;
2562 }
2563
2564 /* compute mdct windows */
2565 for(i=0;i<36;i++) {
2566 for(j=0; j<4; j++){
2567 double d;
2568
2569 if(j==2 && i%3 != 1)
2570 continue;
2571
2572 d= sin(M_PI * (i + 0.5) / 36.0);
2573 if(j==1){
2574 if (i>=30) d= 0;
2575 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
2576 else if(i>=18) d= 1;
2577 }else if(j==3){
2578 if (i< 6) d= 0;
2579 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
2580 else if(i< 18) d= 1;
2581 }
2582 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
2583 if(j==2)
2584 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
2585 else
2586 mdct_win[j][i ] = FIXHR((d / (1<<5)));
2587 }
2588 }
2589 for(j=0;j<4;j++) {
2590 for(i=0;i<36;i+=2) {
2591 mdct_win[j + 4][i] = mdct_win[j][i];
2592 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
2593 }
2594 }
2595 init = 1;
2596 }
2597 return 0;
2598}
2599
2600static int mp3_decode_frame(
2601 mp3_context_t *s,
2602 int16_t *out_samples, int *data_size,
2603 uint8_t *buf, int buf_size
2604) {
2605 uint32_t header;
2606 int out_size;
2607 int extra_bytes = 0;
2608
2609retry:
2610 if(buf_size < HEADER_SIZE)
2611 return -1;
2612
2613 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
2614 if(mp3_check_header(header) < 0){
2615 buf++;
2616 buf_size--;
2617 extra_bytes++;
2618 goto retry;
2619 }
2620
2621 if (decode_header(s, header) == 1) {
2622 s->frame_size = -1;
2623 return -1;
2624 }
2625
2626 if(s->frame_size<=0 || s->frame_size > buf_size){
2627 return -1; // incomplete frame
2628 }
2629 if(s->frame_size < buf_size) {
2630 buf_size = s->frame_size;
2631 }
2632
2633 out_size = mp3_decode_main(s, out_samples, buf, buf_size);
2634 if(out_size>=0)
2635 *data_size = out_size;
2636 // else: Error while decoding MPEG audio frame.
2637 s->frame_size += extra_bytes;
2638 return buf_size;
2639}
2640
2642
2643mp3_decoder_t mp3_create(void) {
2644 void *dec = libc_calloc(sizeof(mp3_context_t), 1);
2645 if (dec) mp3_decode_init((mp3_context_t*) dec);
2646 return (mp3_decoder_t) dec;
2647}
2648
2649void mp3_done(mp3_decoder_t *dec) {
2650 if (dec) libc_free(dec);
2651}
2652
2653int mp3_decode(mp3_decoder_t *dec, void *buf, int bytes, signed short *out, mp3_info_t *info) {
2654 int res, size = -1;
2655 mp3_context_t *s = (mp3_context_t*) dec;
2656 if (!s) return 0;
2657 res = mp3_decode_frame(s, (int16_t*) out, &size, buf, bytes);
2658 if (res < 0) return 0;
2659 if (info) {
2660 info->sample_rate = s->sample_rate;
2661 info->channels = s->nb_channels;
2662 info->audio_bytes = size;
2663 }
2664 return s->frame_size;
2665}
int16_t(* table)[2]
code, bits
Definition minimp3.c:104