blendish: cosmetix
[iv.d.git] / minimp3.d
blob6661ea448e7e0680da55dc4bad4294887b400978
1 /*
2 * MPEG Audio Layer III decoder
3 * Copyright (c) 2001, 2002 Fabrice Bellard,
4 * (c) 2007 Martin J. Fiedler
6 * D conversion by Ketmar // Invisible Vector
8 * This file is a stripped-down version of the MPEG Audio decoder from
9 * the FFmpeg libavcodec library.
11 * FFmpeg and minimp3 are free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * FFmpeg and minimp3 are distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 module iv.minimp3 /*is aliced*/;
26 import iv.alice;
28 /* code sample:
29 auto fi = File(args[1]);
31 auto reader = delegate (void[] buf) {
32 auto rd = fi.rawRead(buf[]);
33 return cast(int)rd.length;
36 auto mp3 = new MP3Decoder(reader);
38 if (!mp3.valid) {
39 writeln("invalid MP3 file!");
40 return;
43 writeln("sample rate: ", mp3.sampleRate);
44 writeln("channels : ", mp3.channels);
46 auto fo = File("z00.raw", "w");
47 while (mp3.valid) {
48 fo.rawWrite(mp3.frameSamples);
49 mp3.decodeNextFrame(reader);
51 fo.close();
54 /* determining mp3 duration with scanning:
55 auto fi = File(args.length > 1 ? args[1] : FileName);
57 auto info = mp3Scan((void[] buf) {
58 auto rd = fi.rawRead(buf[]);
59 return cast(uint)rd.length;
60 });
62 if (!info.valid) {
63 writeln("invalid MP3 file!");
64 } else {
65 writeln("sample rate: ", info.sampleRate);
66 writeln("channels : ", info.channels);
67 writeln("samples : ", info.samples);
68 auto seconds = info.samples/info.sampleRate;
69 writefln("time: %2s:%02s", seconds/60, seconds%60);
74 // ////////////////////////////////////////////////////////////////////////// //
75 alias MP3Decoder = MP3DecoderImpl!true;
76 alias MP3DecoderNoGC = MP3DecoderImpl!false;
79 // ////////////////////////////////////////////////////////////////////////// //
80 // see iv.mp3scan
82 struct MP3Info {
83 uint sampleRate;
84 ubyte channels;
85 ulong samples;
87 @property bool valid () const pure nothrow @safe @nogc { return (sampleRate != 0); }
91 MP3Info mp3Scan(RDG) (scope RDG rdg) if (is(typeof({
92 ubyte[2] buf;
93 int rd = rdg(buf[]);
94 }))) {
95 MP3Info info;
96 bool eofhit;
97 ubyte[4096] inbuf;
98 enum inbufsize = cast(uint)inbuf.length;
99 uint inbufpos, inbufused;
100 mp3_context_t* s = cast(mp3_context_t*)libc_calloc(mp3_context_t.sizeof, 1);
101 if (s is null) return info;
102 scope(exit) libc_free(s);
103 bool skipTagCheck;
104 int headersCount;
106 void readMoreData () {
107 if (inbufused-inbufpos < 1441) {
108 import core.stdc.string : memmove;
109 auto left = inbufused-inbufpos;
110 if (inbufpos > 0) memmove(inbuf.ptr, inbuf.ptr+inbufpos, left);
111 inbufpos = 0;
112 inbufused = left;
113 // read more bytes
114 left = inbufsize-inbufused;
115 int rd = rdg(inbuf[inbufused..inbufused+left]);
116 if (rd <= 0) {
117 eofhit = true;
118 } else {
119 inbufused += rd;
124 // now skip frames
125 while (!eofhit) {
126 readMoreData();
127 if (eofhit && inbufused-inbufpos < 1024) break;
128 auto left = inbufused-inbufpos;
129 // check for tags
130 if (!skipTagCheck) {
131 skipTagCheck = true;
132 if (left >= 10) {
133 // check for ID3v2
134 if (inbuf.ptr[0] == 'I' && inbuf.ptr[1] == 'D' && inbuf.ptr[2] == '3' && inbuf.ptr[3] != 0xff && inbuf.ptr[4] != 0xff &&
135 ((inbuf.ptr[6]|inbuf.ptr[7]|inbuf.ptr[8]|inbuf.ptr[9])&0x80) == 0) { // see ID3v2 specs
136 // get tag size
137 uint sz = (inbuf.ptr[9]|(inbuf.ptr[8]<<7)|(inbuf.ptr[7]<<14)|(inbuf.ptr[6]<<21))+10;
138 // skip `sz` bytes, it's a tag
139 while (sz > 0 && !eofhit) {
140 readMoreData();
141 left = inbufused-inbufpos;
142 if (left > sz) left = sz;
143 inbufpos += left;
144 sz -= left;
146 if (eofhit) break;
147 continue;
150 } else {
151 if (inbuf.ptr[0] == 'T' && inbuf.ptr[1] == 'A' && inbuf.ptr[2] == 'G') {
152 // this may be ID3v1, just skip 128 bytes
153 uint sz = 128;
154 while (sz > 0 && !eofhit) {
155 readMoreData();
156 left = inbufused-inbufpos;
157 if (left > sz) left = sz;
158 inbufpos += left;
159 sz -= left;
161 if (eofhit) break;
162 continue;
165 int res = mp3_skip_frame(s, inbuf.ptr+inbufpos, left);
166 if (res < 0) {
167 // can't decode frame
168 if (inbufused-inbufpos < 1024) inbufpos = inbufused; else inbufpos += 1024;
169 } else {
170 if (headersCount < 6) ++headersCount;
171 if (!info.valid) {
172 if (s.sample_rate < 1024 || s.sample_rate > 96000) break;
173 if (s.nb_channels < 1 || s.nb_channels > 2) break;
174 info.sampleRate = s.sample_rate;
175 info.channels = cast(ubyte)s.nb_channels;
177 info.samples += s.sample_count;
178 inbufpos += res;
181 //{ import core.stdc.stdio : printf; printf("%d\n", headersCount); }
182 if (headersCount < 6) info = info.init;
183 return info;
188 // ////////////////////////////////////////////////////////////////////////// //
189 final class MP3DecoderImpl(bool allowGC) {
190 public:
191 enum MaxSamplesPerFrame = 1152*2;
193 // read bytes into the buffer, return number of bytes read or 0 for EOF, -1 on error
194 // will never be called with empty buffer, or buffer more than 128KB
195 static if (allowGC) {
196 alias ReadBufFn = int delegate (void[] buf);
197 } else {
198 alias ReadBufFn = int delegate (void[] buf) nothrow @nogc;
201 public:
202 static struct mp3_info_t {
203 int sample_rate;
204 int channels;
205 int audio_bytes; // generated amount of audio per frame
208 private:
209 void* dec;
210 //ReadBufFn readBuf;
211 ubyte* inbuf;
212 uint inbufsize; // will allocate enough bytes for one frame
213 uint inbufpos, inbufused;
214 bool eofhit;
215 short[MaxSamplesPerFrame] samples;
216 uint scanLeft = 256*1024+16; // how much bytes we should scan (max ID3 size is 256KB)
218 static if (allowGC) mixin(ObjectCodeMixin); else mixin("nothrow @nogc: "~ObjectCodeMixin);
221 private enum ObjectCodeMixin = q{
222 private:
223 uint ensureBytes (scope ReadBufFn readBuf, uint size) {
224 import core.stdc.string : memmove;
225 for (;;) {
226 assert(inbufused >= inbufpos);
227 uint left = inbufused-inbufpos;
228 if (left >= size) return size;
229 if (eofhit) return left;
230 if (left > 0) {
231 if (inbufpos > 0) memmove(inbuf, inbuf+inbufpos, left);
232 inbufused = left;
233 } else {
234 inbufused = 0;
236 inbufpos = 0;
237 //{ import std.conv : to; assert(size > inbufused, "size="~to!string(size)~"; inbufpos="~to!string(inbufpos)~"; inbufused="~to!string(inbufused)~"; inbufsize="~to!string(inbufsize)); }
238 assert(size > inbufused);
239 left = size-inbufused;
240 assert(left > 0);
241 if (inbufsize < inbufused+left) {
242 auto np = libc_realloc(inbuf, inbufused+left);
243 if (np is null) assert(0, "out of memory"); //FIXME
244 inbufsize = inbufused+left;
245 inbuf = cast(ubyte*)np;
247 auto rd = readBuf(inbuf[inbufused..inbufused+left]);
248 if (rd > left) assert(0, "mp3 reader returned too many bytes");
249 if (rd <= 0) eofhit = true; else inbufused += rd;
253 void removeBytes (uint size) {
254 if (size == 0) return;
255 if (size > inbufused-inbufpos) {
256 //assert(0, "the thing that should not be");
257 // we will come here when we are scanning for MP3 frame and no more bytes left
258 eofhit = true;
259 inbufpos = inbufused;
260 } else {
261 inbufpos += size;
265 private:
266 mp3_info_t info;
267 bool curFrameIsOk;
268 bool skipTagCheck;
270 private:
271 bool decodeOneFrame (scope ReadBufFn readBuf, bool first=false) {
272 for (;;) {
273 if (!eofhit && inbufused-inbufpos < 1441) ensureBytes(readBuf, 64*1024);
274 int res, size = -1;
276 // check for tags
277 if (!skipTagCheck) {
278 skipTagCheck = false;
279 if (inbufused-inbufpos >= 10) {
280 // check for ID3v2
281 if (inbuf[inbufpos+0] == 'I' && inbuf[inbufpos+1] == 'D' && inbuf[inbufpos+2] == '3' && inbuf[inbufpos+3] != 0xff && inbuf[inbufpos+4] != 0xff &&
282 ((inbuf[inbufpos+6]|inbuf[inbufpos+7]|inbuf[inbufpos+8]|inbuf[inbufpos+9])&0x80) == 0) { // see ID3v2 specs
283 // get tag size
284 uint sz = (inbuf[inbufpos+9]|(inbuf[inbufpos+8]<<7)|(inbuf[inbufpos+7]<<14)|(inbuf[inbufpos+6]<<21))+10;
285 // skip `sz` bytes, it's a tag
286 while (sz > 0 && !eofhit) {
287 ensureBytes(readBuf, 64*1024);
288 auto left = inbufused-inbufpos;
289 if (left > sz) left = sz;
290 removeBytes(left);
291 sz -= left;
293 if (eofhit) { curFrameIsOk = false; return false; }
294 continue;
297 } else {
298 if (inbuf[inbufpos+0] == 'T' && inbuf[inbufpos+1] == 'A' && inbuf[inbufpos+2] == 'G') {
299 // this may be ID3v1, just skip 128 bytes
300 uint sz = 128;
301 while (sz > 0 && !eofhit) {
302 ensureBytes(readBuf, 64*1024);
303 auto left = inbufused-inbufpos;
304 if (left > sz) left = sz;
305 removeBytes(left);
306 sz -= left;
308 if (eofhit) { curFrameIsOk = false; return false; }
309 continue;
313 mp3_context_t* s = cast(mp3_context_t*)dec;
314 res = mp3_decode_frame(s, /*cast(int16_t*)out_*/samples.ptr, &size, inbuf+inbufpos, /*bytes*/inbufused-inbufpos);
315 if (res < 0) {
316 // can't decode frame
317 if (scanLeft >= 1024) {
318 scanLeft -= 1024;
319 removeBytes(1024);
320 continue;
322 curFrameIsOk = false;
323 return false;
325 info.audio_bytes = size;
326 if (first) {
327 info.sample_rate = s.sample_rate;
328 info.channels = s.nb_channels;
329 if ((info.sample_rate < 1024 || info.sample_rate > 96000) ||
330 (info.channels < 1 || info.channels > 2) ||
331 (info.audio_bytes < 2 || info.audio_bytes > MaxSamplesPerFrame*2 || info.audio_bytes%2 != 0))
333 curFrameIsOk = false;
334 return false;
336 curFrameIsOk = true;
337 } else {
338 if ((s.sample_rate < 1024 || s.sample_rate > 96000) ||
339 (s.nb_channels < 1 || s.nb_channels > 2) ||
340 (size < 2 || size > MaxSamplesPerFrame*2 || size%2 != 0))
342 curFrameIsOk = false;
343 } else {
344 curFrameIsOk = true;
347 if (curFrameIsOk) {
348 scanLeft = 256*1024+16;
349 removeBytes(s.frame_size);
350 return /*s.frame_size*/true;
352 if (scanLeft >= 1024) {
353 scanLeft -= 1024;
354 removeBytes(1024);
355 continue;
357 return false;
361 public:
362 this (scope ReadBufFn reader) {
363 static if (allowGC) {
364 if (reader is null) throw new Exception("reader is null");
365 } else {
366 if (reader is null) assert(0, "reader is null");
368 //readBuf = reader;
369 dec = libc_calloc(mp3_context_t.sizeof, 1);
370 if (dec is null) assert(0, "out of memory"); // no, really! ;-)
371 //mp3_decode_init(cast(mp3_context_t*)dec);
372 if (!decodeOneFrame(reader, true)) close();
375 ~this () { close(); }
377 void close () {
378 if (dec !is null) { libc_free(dec); dec = null; }
379 if (inbuf !is null) { libc_free(inbuf); inbuf = null; }
380 info.audio_bytes = 0;
383 // restart decoding
384 void restart (scope ReadBufFn reader) {
385 inbufpos = inbufused = 0;
386 eofhit = false;
387 info.audio_bytes = 0;
388 scanLeft = 256*1024+16;
389 skipTagCheck = false;
390 if (!decodeOneFrame(reader, true)) close();
393 // empty read buffers and decode next frame; should be used to sync after seeking in input stream
394 void sync (scope ReadBufFn reader) {
395 inbufpos = inbufused = 0;
396 eofhit = false;
397 info.audio_bytes = 0;
398 scanLeft = 256*1024+16;
399 skipTagCheck = false;
400 if (!decodeOneFrame(reader)) close();
403 bool decodeNextFrame (scope ReadBufFn reader) {
404 if (!valid) return false;
405 static if (allowGC) scope(failure) close();
406 if (reader is null) return false;
407 if (!decodeOneFrame(reader)) {
408 close();
409 return false;
411 return true;
414 @property bool valid () const pure nothrow @safe @nogc { return (dec !is null && curFrameIsOk); }
415 @property uint sampleRate () const pure nothrow @safe @nogc { return (valid ? info.sample_rate : 0); }
416 @property ubyte channels () const pure nothrow @safe @nogc { return (valid ? cast(ubyte)info.channels : 0); }
417 @property int samplesInFrame () const pure nothrow @safe @nogc { return (valid ? cast(ubyte)info.audio_bytes : 0); }
419 @property short[] frameSamples () nothrow @nogc {
420 if (!valid) return null;
421 return samples[0..info.audio_bytes/2];
426 // ////////////////////////////////////////////////////////////////////////// //
427 private:
428 nothrow @nogc:
429 import core.stdc.stdlib : libc_calloc = calloc, libc_malloc = malloc, libc_realloc = realloc, libc_free = free;
430 import core.stdc.string : libc_memcpy = memcpy, libc_memset = memset, libc_memmove = memmove;
432 import std.math : libc_pow = pow, libc_frexp = frexp, tan, M_PI = PI, sqrt, cos, sin;
435 void* libc_calloc (usize nmemb, usize count) {
436 import core.stdc.stdlib : calloc;
437 import core.stdc.stdio : printf;
438 printf("calloc(%zu, %zu)\n", nmemb, count);
439 return calloc(nmemb, count);
442 void* libc_malloc (usize count) {
443 import core.stdc.stdlib : malloc;
444 import core.stdc.stdio : printf;
445 printf("malloc(%zu)\n", count+1024*1024);
446 return malloc(count);
449 void* libc_realloc (void* ptr, usize count) {
450 import core.stdc.stdlib : realloc;
451 import core.stdc.stdio : printf;
452 printf("realloc(%p, %zu)\n", ptr, count);
453 return realloc(ptr, count+1024*1024);
456 void libc_free (void* ptr) {
457 import core.stdc.stdlib : free;
458 import core.stdc.stdio : printf;
459 printf("free(%p)\n", ptr);
460 return free(ptr);
464 enum MP3_FRAME_SIZE = 1152;
465 enum MP3_MAX_CODED_FRAME_SIZE = 1792;
466 enum MP3_MAX_CHANNELS = 2;
467 enum SBLIMIT = 32;
469 enum MP3_STEREO = 0;
470 enum MP3_JSTEREO = 1;
471 enum MP3_DUAL = 2;
472 enum MP3_MONO = 3;
474 enum SAME_HEADER_MASK = (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19));
476 enum FRAC_BITS = 15;
477 enum WFRAC_BITS = 14;
479 enum OUT_MAX = (32767);
480 enum OUT_MIN = (-32768);
481 enum OUT_SHIFT = (WFRAC_BITS + FRAC_BITS - 15);
483 enum MODE_EXT_MS_STEREO = 2;
484 enum MODE_EXT_I_STEREO = 1;
486 enum FRAC_ONE = (1 << FRAC_BITS);
487 //enum FIX(a) ((int)((a) * FRAC_ONE))
488 enum FIXR(double a) = (cast(int)((a) * FRAC_ONE + 0.5));
489 int FIXRx(double a) { static if (__VERSION__ > 2067) pragma(inline, true); return (cast(int)((a) * FRAC_ONE + 0.5)); }
490 //enum FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
491 enum FIXHR(double a) = (cast(int)((a) * (1L<<32) + 0.5));
492 int FIXHRx() (double a) { static if (__VERSION__ > 2067) pragma(inline, true); return (cast(int)((a) * (1L<<32) + 0.5)); }
494 long MULL() (int a, int b) { static if (__VERSION__ > 2067) pragma(inline, true); return ((cast(long)(a) * cast(long)(b)) >> FRAC_BITS); }
495 long MULH() (int a, int b) { static if (__VERSION__ > 2067) pragma(inline, true); return ((cast(long)(a) * cast(long)(b)) >> 32); }
496 auto MULS(T) (T ra, T rb) { static if (__VERSION__ > 2067) pragma(inline, true); return ((ra) * (rb)); }
498 enum ISQRT2 = FIXR!(0.70710678118654752440);
500 enum HEADER_SIZE = 4;
501 enum BACKSTEP_SIZE = 512;
502 enum EXTRABYTES = 24;
505 // ////////////////////////////////////////////////////////////////////////// //
506 alias VLC_TYPE = short;
507 alias VT2 = VLC_TYPE[2];
509 alias int8_t = byte;
510 alias int16_t = short;
511 alias int32_t = int;
512 alias int64_t = long;
514 alias uint8_t = ubyte;
515 alias uint16_t = ushort;
516 alias uint32_t = uint;
517 alias uint64_t = ulong;
519 struct bitstream_t {
520 const(ubyte)* buffer, buffer_end;
521 int index;
522 int size_in_bits;
525 struct vlc_t {
526 int bits;
527 //VLC_TYPE (*table)[2]; ///< code, bits
528 VT2* table;
529 int table_size, table_allocated;
532 struct mp3_context_t {
533 uint8_t[2*BACKSTEP_SIZE+EXTRABYTES] last_buf;
534 int last_buf_size;
535 int frame_size;
536 uint32_t free_format_next_header;
537 int error_protection;
538 int sample_rate;
539 int sample_rate_index;
540 int bit_rate;
541 bitstream_t gb;
542 bitstream_t in_gb;
543 int nb_channels;
544 int sample_count;
545 int mode;
546 int mode_ext;
547 int lsf;
548 int16_t[512*2][MP3_MAX_CHANNELS] synth_buf;
549 int[MP3_MAX_CHANNELS] synth_buf_offset;
550 int32_t[SBLIMIT][36][MP3_MAX_CHANNELS] sb_samples;
551 int32_t[SBLIMIT*18][MP3_MAX_CHANNELS] mdct_buf;
552 int dither_state;
553 uint last_header; //&0xffff0c00u;
556 struct granule_t {
557 uint8_t scfsi;
558 int part2_3_length;
559 int big_values;
560 int global_gain;
561 int scalefac_compress;
562 uint8_t block_type;
563 uint8_t switch_point;
564 int[3] table_select;
565 int[3] subblock_gain;
566 uint8_t scalefac_scale;
567 uint8_t count1table_select;
568 int[3] region_size;
569 int preflag;
570 int short_start, long_end;
571 uint8_t[40] scale_factors;
572 int32_t[SBLIMIT * 18] sb_hybrid;
575 struct huff_table_t {
576 int xsize;
577 immutable(uint8_t)* bits;
578 immutable(uint16_t)* codes;
581 __gshared vlc_t[16] huff_vlc;
582 __gshared vlc_t[2] huff_quad_vlc;
583 __gshared uint16_t[23][9] band_index_long;
584 enum TABLE_4_3_SIZE = (8191 + 16)*4;
585 __gshared int8_t* table_4_3_exp;
586 __gshared uint32_t* table_4_3_value;
587 __gshared uint32_t[512] exp_table;
588 __gshared uint32_t[16][512] expval_table;
589 __gshared int32_t[16][2] is_table;
590 __gshared int32_t[16][2][2] is_table_lsf;
591 __gshared int32_t[4][8] csa_table;
592 __gshared float[4][8] csa_table_float;
593 __gshared int32_t[36][8] mdct_win;
594 __gshared int16_t[512] window;
597 // ////////////////////////////////////////////////////////////////////////// //
598 static immutable uint16_t[15][2] mp3_bitrate_tab = [
599 [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 ],
600 [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160]
603 static immutable uint16_t[3] mp3_freq_tab = [ 44100, 48000, 32000 ];
605 static immutable int32_t[257] mp3_enwindow = [
606 0, -1, -1, -1, -1, -1, -1, -2,
607 -2, -2, -2, -3, -3, -4, -4, -5,
608 -5, -6, -7, -7, -8, -9, -10, -11,
609 -13, -14, -16, -17, -19, -21, -24, -26,
610 -29, -31, -35, -38, -41, -45, -49, -53,
611 -58, -63, -68, -73, -79, -85, -91, -97,
612 -104, -111, -117, -125, -132, -139, -147, -154,
613 -161, -169, -176, -183, -190, -196, -202, -208,
614 213, 218, 222, 225, 227, 228, 228, 227,
615 224, 221, 215, 208, 200, 189, 177, 163,
616 146, 127, 106, 83, 57, 29, -2, -36,
617 -72, -111, -153, -197, -244, -294, -347, -401,
618 -459, -519, -581, -645, -711, -779, -848, -919,
619 -991, -1064, -1137, -1210, -1283, -1356, -1428, -1498,
620 -1567, -1634, -1698, -1759, -1817, -1870, -1919, -1962,
621 -2001, -2032, -2057, -2075, -2085, -2087, -2080, -2063,
622 2037, 2000, 1952, 1893, 1822, 1739, 1644, 1535,
623 1414, 1280, 1131, 970, 794, 605, 402, 185,
624 -45, -288, -545, -814, -1095, -1388, -1692, -2006,
625 -2330, -2663, -3004, -3351, -3705, -4063, -4425, -4788,
626 -5153, -5517, -5879, -6237, -6589, -6935, -7271, -7597,
627 -7910, -8209, -8491, -8755, -8998, -9219, -9416, -9585,
628 -9727, -9838, -9916, -9959, -9966, -9935, -9863, -9750,
629 -9592, -9389, -9139, -8840, -8492, -8092, -7640, -7134,
630 6574, 5959, 5288, 4561, 3776, 2935, 2037, 1082,
631 70, -998, -2122, -3300, -4533, -5818, -7154, -8540,
632 -9975,-11455,-12980,-14548,-16155,-17799,-19478,-21189,
633 -22929,-24694,-26482,-28289,-30112,-31947,-33791,-35640,
634 -37489,-39336,-41176,-43006,-44821,-46617,-48390,-50137,
635 -51853,-53534,-55178,-56778,-58333,-59838,-61289,-62684,
636 -64019,-65290,-66494,-67629,-68692,-69679,-70590,-71420,
637 -72169,-72835,-73415,-73908,-74313,-74630,-74856,-74992,
638 75038,
641 static immutable uint8_t[16][2] slen_table = [
642 [ 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 ],
643 [ 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 ],
646 static immutable uint8_t[4][3][6] lsf_nsf_table = [
647 [ [ 6, 5, 5, 5 ], [ 9, 9, 9, 9 ], [ 6, 9, 9, 9 ] ],
648 [ [ 6, 5, 7, 3 ], [ 9, 9, 12, 6 ], [ 6, 9, 12, 6 ] ],
649 [ [ 11, 10, 0, 0 ], [ 18, 18, 0, 0 ], [ 15, 18, 0, 0 ] ],
650 [ [ 7, 7, 7, 0 ], [ 12, 12, 12, 0 ], [ 6, 15, 12, 0 ] ],
651 [ [ 6, 6, 6, 3 ], [ 12, 9, 9, 6 ], [ 6, 12, 9, 6 ] ],
652 [ [ 8, 8, 5, 0 ], [ 15, 12, 9, 0 ], [ 6, 18, 9, 0 ] ],
655 static immutable uint16_t[4] mp3_huffcodes_1 = [ 0x0001, 0x0001, 0x0001, 0x0000, ];
657 static immutable uint8_t[4] mp3_huffbits_1 = [ 1, 3, 2, 3, ];
659 static immutable uint16_t[9] mp3_huffcodes_2 = [ 0x0001, 0x0002, 0x0001, 0x0003, 0x0001, 0x0001, 0x0003, 0x0002, 0x0000, ];
661 static immutable uint8_t[9] mp3_huffbits_2 = [ 1, 3, 6, 3, 3, 5, 5, 5, 6, ];
663 static immutable uint16_t[9] mp3_huffcodes_3 = [ 0x0003, 0x0002, 0x0001, 0x0001, 0x0001, 0x0001, 0x0003, 0x0002, 0x0000, ];
665 static immutable uint8_t[9] mp3_huffbits_3 = [ 2, 2, 6, 3, 2, 5, 5, 5, 6, ];
667 static immutable uint16_t[16] mp3_huffcodes_5 = [
668 0x0001, 0x0002, 0x0006, 0x0005, 0x0003, 0x0001, 0x0004, 0x0004,
669 0x0007, 0x0005, 0x0007, 0x0001, 0x0006, 0x0001, 0x0001, 0x0000,
672 static immutable uint8_t[16] mp3_huffbits_5 = [
673 1, 3, 6, 7, 3, 3, 6, 7,
674 6, 6, 7, 8, 7, 6, 7, 8,
677 static immutable uint16_t[16] mp3_huffcodes_6 = [
678 0x0007, 0x0003, 0x0005, 0x0001, 0x0006, 0x0002, 0x0003, 0x0002,
679 0x0005, 0x0004, 0x0004, 0x0001, 0x0003, 0x0003, 0x0002, 0x0000,
682 static immutable uint8_t[16] mp3_huffbits_6 = [
683 3, 3, 5, 7, 3, 2, 4, 5,
684 4, 4, 5, 6, 6, 5, 6, 7,
687 static immutable uint16_t[36] mp3_huffcodes_7 = [
688 0x0001, 0x0002, 0x000a, 0x0013, 0x0010, 0x000a, 0x0003, 0x0003,
689 0x0007, 0x000a, 0x0005, 0x0003, 0x000b, 0x0004, 0x000d, 0x0011,
690 0x0008, 0x0004, 0x000c, 0x000b, 0x0012, 0x000f, 0x000b, 0x0002,
691 0x0007, 0x0006, 0x0009, 0x000e, 0x0003, 0x0001, 0x0006, 0x0004,
692 0x0005, 0x0003, 0x0002, 0x0000,
695 static immutable uint8_t[36] mp3_huffbits_7 = [
696 1, 3, 6, 8, 8, 9, 3, 4,
697 6, 7, 7, 8, 6, 5, 7, 8,
698 8, 9, 7, 7, 8, 9, 9, 9,
699 7, 7, 8, 9, 9, 10, 8, 8,
700 9, 10, 10, 10,
703 static immutable uint16_t[36] mp3_huffcodes_8 = [
704 0x0003, 0x0004, 0x0006, 0x0012, 0x000c, 0x0005, 0x0005, 0x0001,
705 0x0002, 0x0010, 0x0009, 0x0003, 0x0007, 0x0003, 0x0005, 0x000e,
706 0x0007, 0x0003, 0x0013, 0x0011, 0x000f, 0x000d, 0x000a, 0x0004,
707 0x000d, 0x0005, 0x0008, 0x000b, 0x0005, 0x0001, 0x000c, 0x0004,
708 0x0004, 0x0001, 0x0001, 0x0000,
711 static immutable uint8_t[36] mp3_huffbits_8 = [
712 2, 3, 6, 8, 8, 9, 3, 2,
713 4, 8, 8, 8, 6, 4, 6, 8,
714 8, 9, 8, 8, 8, 9, 9, 10,
715 8, 7, 8, 9, 10, 10, 9, 8,
716 9, 9, 11, 11,
719 static immutable uint16_t[36] mp3_huffcodes_9 = [
720 0x0007, 0x0005, 0x0009, 0x000e, 0x000f, 0x0007, 0x0006, 0x0004,
721 0x0005, 0x0005, 0x0006, 0x0007, 0x0007, 0x0006, 0x0008, 0x0008,
722 0x0008, 0x0005, 0x000f, 0x0006, 0x0009, 0x000a, 0x0005, 0x0001,
723 0x000b, 0x0007, 0x0009, 0x0006, 0x0004, 0x0001, 0x000e, 0x0004,
724 0x0006, 0x0002, 0x0006, 0x0000,
727 static immutable uint8_t[36] mp3_huffbits_9 = [
728 3, 3, 5, 6, 8, 9, 3, 3,
729 4, 5, 6, 8, 4, 4, 5, 6,
730 7, 8, 6, 5, 6, 7, 7, 8,
731 7, 6, 7, 7, 8, 9, 8, 7,
732 8, 8, 9, 9,
735 static immutable uint16_t[64] mp3_huffcodes_10 = [
736 0x0001, 0x0002, 0x000a, 0x0017, 0x0023, 0x001e, 0x000c, 0x0011,
737 0x0003, 0x0003, 0x0008, 0x000c, 0x0012, 0x0015, 0x000c, 0x0007,
738 0x000b, 0x0009, 0x000f, 0x0015, 0x0020, 0x0028, 0x0013, 0x0006,
739 0x000e, 0x000d, 0x0016, 0x0022, 0x002e, 0x0017, 0x0012, 0x0007,
740 0x0014, 0x0013, 0x0021, 0x002f, 0x001b, 0x0016, 0x0009, 0x0003,
741 0x001f, 0x0016, 0x0029, 0x001a, 0x0015, 0x0014, 0x0005, 0x0003,
742 0x000e, 0x000d, 0x000a, 0x000b, 0x0010, 0x0006, 0x0005, 0x0001,
743 0x0009, 0x0008, 0x0007, 0x0008, 0x0004, 0x0004, 0x0002, 0x0000,
746 static immutable uint8_t[64] mp3_huffbits_10 = [
747 1, 3, 6, 8, 9, 9, 9, 10,
748 3, 4, 6, 7, 8, 9, 8, 8,
749 6, 6, 7, 8, 9, 10, 9, 9,
750 7, 7, 8, 9, 10, 10, 9, 10,
751 8, 8, 9, 10, 10, 10, 10, 10,
752 9, 9, 10, 10, 11, 11, 10, 11,
753 8, 8, 9, 10, 10, 10, 11, 11,
754 9, 8, 9, 10, 10, 11, 11, 11,
757 static immutable uint16_t[64] mp3_huffcodes_11 = [
758 0x0003, 0x0004, 0x000a, 0x0018, 0x0022, 0x0021, 0x0015, 0x000f,
759 0x0005, 0x0003, 0x0004, 0x000a, 0x0020, 0x0011, 0x000b, 0x000a,
760 0x000b, 0x0007, 0x000d, 0x0012, 0x001e, 0x001f, 0x0014, 0x0005,
761 0x0019, 0x000b, 0x0013, 0x003b, 0x001b, 0x0012, 0x000c, 0x0005,
762 0x0023, 0x0021, 0x001f, 0x003a, 0x001e, 0x0010, 0x0007, 0x0005,
763 0x001c, 0x001a, 0x0020, 0x0013, 0x0011, 0x000f, 0x0008, 0x000e,
764 0x000e, 0x000c, 0x0009, 0x000d, 0x000e, 0x0009, 0x0004, 0x0001,
765 0x000b, 0x0004, 0x0006, 0x0006, 0x0006, 0x0003, 0x0002, 0x0000,
768 static immutable uint8_t[64] mp3_huffbits_11 = [
769 2, 3, 5, 7, 8, 9, 8, 9,
770 3, 3, 4, 6, 8, 8, 7, 8,
771 5, 5, 6, 7, 8, 9, 8, 8,
772 7, 6, 7, 9, 8, 10, 8, 9,
773 8, 8, 8, 9, 9, 10, 9, 10,
774 8, 8, 9, 10, 10, 11, 10, 11,
775 8, 7, 7, 8, 9, 10, 10, 10,
776 8, 7, 8, 9, 10, 10, 10, 10,
779 static immutable uint16_t[64] mp3_huffcodes_12 = [
780 0x0009, 0x0006, 0x0010, 0x0021, 0x0029, 0x0027, 0x0026, 0x001a,
781 0x0007, 0x0005, 0x0006, 0x0009, 0x0017, 0x0010, 0x001a, 0x000b,
782 0x0011, 0x0007, 0x000b, 0x000e, 0x0015, 0x001e, 0x000a, 0x0007,
783 0x0011, 0x000a, 0x000f, 0x000c, 0x0012, 0x001c, 0x000e, 0x0005,
784 0x0020, 0x000d, 0x0016, 0x0013, 0x0012, 0x0010, 0x0009, 0x0005,
785 0x0028, 0x0011, 0x001f, 0x001d, 0x0011, 0x000d, 0x0004, 0x0002,
786 0x001b, 0x000c, 0x000b, 0x000f, 0x000a, 0x0007, 0x0004, 0x0001,
787 0x001b, 0x000c, 0x0008, 0x000c, 0x0006, 0x0003, 0x0001, 0x0000,
790 static immutable uint8_t[64] mp3_huffbits_12 = [
791 4, 3, 5, 7, 8, 9, 9, 9,
792 3, 3, 4, 5, 7, 7, 8, 8,
793 5, 4, 5, 6, 7, 8, 7, 8,
794 6, 5, 6, 6, 7, 8, 8, 8,
795 7, 6, 7, 7, 8, 8, 8, 9,
796 8, 7, 8, 8, 8, 9, 8, 9,
797 8, 7, 7, 8, 8, 9, 9, 10,
798 9, 8, 8, 9, 9, 9, 9, 10,
801 static immutable uint16_t[256] mp3_huffcodes_13 = [
802 0x0001, 0x0005, 0x000e, 0x0015, 0x0022, 0x0033, 0x002e, 0x0047,
803 0x002a, 0x0034, 0x0044, 0x0034, 0x0043, 0x002c, 0x002b, 0x0013,
804 0x0003, 0x0004, 0x000c, 0x0013, 0x001f, 0x001a, 0x002c, 0x0021,
805 0x001f, 0x0018, 0x0020, 0x0018, 0x001f, 0x0023, 0x0016, 0x000e,
806 0x000f, 0x000d, 0x0017, 0x0024, 0x003b, 0x0031, 0x004d, 0x0041,
807 0x001d, 0x0028, 0x001e, 0x0028, 0x001b, 0x0021, 0x002a, 0x0010,
808 0x0016, 0x0014, 0x0025, 0x003d, 0x0038, 0x004f, 0x0049, 0x0040,
809 0x002b, 0x004c, 0x0038, 0x0025, 0x001a, 0x001f, 0x0019, 0x000e,
810 0x0023, 0x0010, 0x003c, 0x0039, 0x0061, 0x004b, 0x0072, 0x005b,
811 0x0036, 0x0049, 0x0037, 0x0029, 0x0030, 0x0035, 0x0017, 0x0018,
812 0x003a, 0x001b, 0x0032, 0x0060, 0x004c, 0x0046, 0x005d, 0x0054,
813 0x004d, 0x003a, 0x004f, 0x001d, 0x004a, 0x0031, 0x0029, 0x0011,
814 0x002f, 0x002d, 0x004e, 0x004a, 0x0073, 0x005e, 0x005a, 0x004f,
815 0x0045, 0x0053, 0x0047, 0x0032, 0x003b, 0x0026, 0x0024, 0x000f,
816 0x0048, 0x0022, 0x0038, 0x005f, 0x005c, 0x0055, 0x005b, 0x005a,
817 0x0056, 0x0049, 0x004d, 0x0041, 0x0033, 0x002c, 0x002b, 0x002a,
818 0x002b, 0x0014, 0x001e, 0x002c, 0x0037, 0x004e, 0x0048, 0x0057,
819 0x004e, 0x003d, 0x002e, 0x0036, 0x0025, 0x001e, 0x0014, 0x0010,
820 0x0035, 0x0019, 0x0029, 0x0025, 0x002c, 0x003b, 0x0036, 0x0051,
821 0x0042, 0x004c, 0x0039, 0x0036, 0x0025, 0x0012, 0x0027, 0x000b,
822 0x0023, 0x0021, 0x001f, 0x0039, 0x002a, 0x0052, 0x0048, 0x0050,
823 0x002f, 0x003a, 0x0037, 0x0015, 0x0016, 0x001a, 0x0026, 0x0016,
824 0x0035, 0x0019, 0x0017, 0x0026, 0x0046, 0x003c, 0x0033, 0x0024,
825 0x0037, 0x001a, 0x0022, 0x0017, 0x001b, 0x000e, 0x0009, 0x0007,
826 0x0022, 0x0020, 0x001c, 0x0027, 0x0031, 0x004b, 0x001e, 0x0034,
827 0x0030, 0x0028, 0x0034, 0x001c, 0x0012, 0x0011, 0x0009, 0x0005,
828 0x002d, 0x0015, 0x0022, 0x0040, 0x0038, 0x0032, 0x0031, 0x002d,
829 0x001f, 0x0013, 0x000c, 0x000f, 0x000a, 0x0007, 0x0006, 0x0003,
830 0x0030, 0x0017, 0x0014, 0x0027, 0x0024, 0x0023, 0x0035, 0x0015,
831 0x0010, 0x0017, 0x000d, 0x000a, 0x0006, 0x0001, 0x0004, 0x0002,
832 0x0010, 0x000f, 0x0011, 0x001b, 0x0019, 0x0014, 0x001d, 0x000b,
833 0x0011, 0x000c, 0x0010, 0x0008, 0x0001, 0x0001, 0x0000, 0x0001,
836 static immutable uint8_t[256] mp3_huffbits_13 = [
837 1, 4, 6, 7, 8, 9, 9, 10,
838 9, 10, 11, 11, 12, 12, 13, 13,
839 3, 4, 6, 7, 8, 8, 9, 9,
840 9, 9, 10, 10, 11, 12, 12, 12,
841 6, 6, 7, 8, 9, 9, 10, 10,
842 9, 10, 10, 11, 11, 12, 13, 13,
843 7, 7, 8, 9, 9, 10, 10, 10,
844 10, 11, 11, 11, 11, 12, 13, 13,
845 8, 7, 9, 9, 10, 10, 11, 11,
846 10, 11, 11, 12, 12, 13, 13, 14,
847 9, 8, 9, 10, 10, 10, 11, 11,
848 11, 11, 12, 11, 13, 13, 14, 14,
849 9, 9, 10, 10, 11, 11, 11, 11,
850 11, 12, 12, 12, 13, 13, 14, 14,
851 10, 9, 10, 11, 11, 11, 12, 12,
852 12, 12, 13, 13, 13, 14, 16, 16,
853 9, 8, 9, 10, 10, 11, 11, 12,
854 12, 12, 12, 13, 13, 14, 15, 15,
855 10, 9, 10, 10, 11, 11, 11, 13,
856 12, 13, 13, 14, 14, 14, 16, 15,
857 10, 10, 10, 11, 11, 12, 12, 13,
858 12, 13, 14, 13, 14, 15, 16, 17,
859 11, 10, 10, 11, 12, 12, 12, 12,
860 13, 13, 13, 14, 15, 15, 15, 16,
861 11, 11, 11, 12, 12, 13, 12, 13,
862 14, 14, 15, 15, 15, 16, 16, 16,
863 12, 11, 12, 13, 13, 13, 14, 14,
864 14, 14, 14, 15, 16, 15, 16, 16,
865 13, 12, 12, 13, 13, 13, 15, 14,
866 14, 17, 15, 15, 15, 17, 16, 16,
867 12, 12, 13, 14, 14, 14, 15, 14,
868 15, 15, 16, 16, 19, 18, 19, 16,
871 static immutable uint16_t[256] mp3_huffcodes_15 = [
872 0x0007, 0x000c, 0x0012, 0x0035, 0x002f, 0x004c, 0x007c, 0x006c,
873 0x0059, 0x007b, 0x006c, 0x0077, 0x006b, 0x0051, 0x007a, 0x003f,
874 0x000d, 0x0005, 0x0010, 0x001b, 0x002e, 0x0024, 0x003d, 0x0033,
875 0x002a, 0x0046, 0x0034, 0x0053, 0x0041, 0x0029, 0x003b, 0x0024,
876 0x0013, 0x0011, 0x000f, 0x0018, 0x0029, 0x0022, 0x003b, 0x0030,
877 0x0028, 0x0040, 0x0032, 0x004e, 0x003e, 0x0050, 0x0038, 0x0021,
878 0x001d, 0x001c, 0x0019, 0x002b, 0x0027, 0x003f, 0x0037, 0x005d,
879 0x004c, 0x003b, 0x005d, 0x0048, 0x0036, 0x004b, 0x0032, 0x001d,
880 0x0034, 0x0016, 0x002a, 0x0028, 0x0043, 0x0039, 0x005f, 0x004f,
881 0x0048, 0x0039, 0x0059, 0x0045, 0x0031, 0x0042, 0x002e, 0x001b,
882 0x004d, 0x0025, 0x0023, 0x0042, 0x003a, 0x0034, 0x005b, 0x004a,
883 0x003e, 0x0030, 0x004f, 0x003f, 0x005a, 0x003e, 0x0028, 0x0026,
884 0x007d, 0x0020, 0x003c, 0x0038, 0x0032, 0x005c, 0x004e, 0x0041,
885 0x0037, 0x0057, 0x0047, 0x0033, 0x0049, 0x0033, 0x0046, 0x001e,
886 0x006d, 0x0035, 0x0031, 0x005e, 0x0058, 0x004b, 0x0042, 0x007a,
887 0x005b, 0x0049, 0x0038, 0x002a, 0x0040, 0x002c, 0x0015, 0x0019,
888 0x005a, 0x002b, 0x0029, 0x004d, 0x0049, 0x003f, 0x0038, 0x005c,
889 0x004d, 0x0042, 0x002f, 0x0043, 0x0030, 0x0035, 0x0024, 0x0014,
890 0x0047, 0x0022, 0x0043, 0x003c, 0x003a, 0x0031, 0x0058, 0x004c,
891 0x0043, 0x006a, 0x0047, 0x0036, 0x0026, 0x0027, 0x0017, 0x000f,
892 0x006d, 0x0035, 0x0033, 0x002f, 0x005a, 0x0052, 0x003a, 0x0039,
893 0x0030, 0x0048, 0x0039, 0x0029, 0x0017, 0x001b, 0x003e, 0x0009,
894 0x0056, 0x002a, 0x0028, 0x0025, 0x0046, 0x0040, 0x0034, 0x002b,
895 0x0046, 0x0037, 0x002a, 0x0019, 0x001d, 0x0012, 0x000b, 0x000b,
896 0x0076, 0x0044, 0x001e, 0x0037, 0x0032, 0x002e, 0x004a, 0x0041,
897 0x0031, 0x0027, 0x0018, 0x0010, 0x0016, 0x000d, 0x000e, 0x0007,
898 0x005b, 0x002c, 0x0027, 0x0026, 0x0022, 0x003f, 0x0034, 0x002d,
899 0x001f, 0x0034, 0x001c, 0x0013, 0x000e, 0x0008, 0x0009, 0x0003,
900 0x007b, 0x003c, 0x003a, 0x0035, 0x002f, 0x002b, 0x0020, 0x0016,
901 0x0025, 0x0018, 0x0011, 0x000c, 0x000f, 0x000a, 0x0002, 0x0001,
902 0x0047, 0x0025, 0x0022, 0x001e, 0x001c, 0x0014, 0x0011, 0x001a,
903 0x0015, 0x0010, 0x000a, 0x0006, 0x0008, 0x0006, 0x0002, 0x0000,
906 static immutable uint8_t[256] mp3_huffbits_15 = [
907 3, 4, 5, 7, 7, 8, 9, 9,
908 9, 10, 10, 11, 11, 11, 12, 13,
909 4, 3, 5, 6, 7, 7, 8, 8,
910 8, 9, 9, 10, 10, 10, 11, 11,
911 5, 5, 5, 6, 7, 7, 8, 8,
912 8, 9, 9, 10, 10, 11, 11, 11,
913 6, 6, 6, 7, 7, 8, 8, 9,
914 9, 9, 10, 10, 10, 11, 11, 11,
915 7, 6, 7, 7, 8, 8, 9, 9,
916 9, 9, 10, 10, 10, 11, 11, 11,
917 8, 7, 7, 8, 8, 8, 9, 9,
918 9, 9, 10, 10, 11, 11, 11, 12,
919 9, 7, 8, 8, 8, 9, 9, 9,
920 9, 10, 10, 10, 11, 11, 12, 12,
921 9, 8, 8, 9, 9, 9, 9, 10,
922 10, 10, 10, 10, 11, 11, 11, 12,
923 9, 8, 8, 9, 9, 9, 9, 10,
924 10, 10, 10, 11, 11, 12, 12, 12,
925 9, 8, 9, 9, 9, 9, 10, 10,
926 10, 11, 11, 11, 11, 12, 12, 12,
927 10, 9, 9, 9, 10, 10, 10, 10,
928 10, 11, 11, 11, 11, 12, 13, 12,
929 10, 9, 9, 9, 10, 10, 10, 10,
930 11, 11, 11, 11, 12, 12, 12, 13,
931 11, 10, 9, 10, 10, 10, 11, 11,
932 11, 11, 11, 11, 12, 12, 13, 13,
933 11, 10, 10, 10, 10, 11, 11, 11,
934 11, 12, 12, 12, 12, 12, 13, 13,
935 12, 11, 11, 11, 11, 11, 11, 11,
936 12, 12, 12, 12, 13, 13, 12, 13,
937 12, 11, 11, 11, 11, 11, 11, 12,
938 12, 12, 12, 12, 13, 13, 13, 13,
941 static immutable uint16_t[256] mp3_huffcodes_16 = [
942 0x0001, 0x0005, 0x000e, 0x002c, 0x004a, 0x003f, 0x006e, 0x005d,
943 0x00ac, 0x0095, 0x008a, 0x00f2, 0x00e1, 0x00c3, 0x0178, 0x0011,
944 0x0003, 0x0004, 0x000c, 0x0014, 0x0023, 0x003e, 0x0035, 0x002f,
945 0x0053, 0x004b, 0x0044, 0x0077, 0x00c9, 0x006b, 0x00cf, 0x0009,
946 0x000f, 0x000d, 0x0017, 0x0026, 0x0043, 0x003a, 0x0067, 0x005a,
947 0x00a1, 0x0048, 0x007f, 0x0075, 0x006e, 0x00d1, 0x00ce, 0x0010,
948 0x002d, 0x0015, 0x0027, 0x0045, 0x0040, 0x0072, 0x0063, 0x0057,
949 0x009e, 0x008c, 0x00fc, 0x00d4, 0x00c7, 0x0183, 0x016d, 0x001a,
950 0x004b, 0x0024, 0x0044, 0x0041, 0x0073, 0x0065, 0x00b3, 0x00a4,
951 0x009b, 0x0108, 0x00f6, 0x00e2, 0x018b, 0x017e, 0x016a, 0x0009,
952 0x0042, 0x001e, 0x003b, 0x0038, 0x0066, 0x00b9, 0x00ad, 0x0109,
953 0x008e, 0x00fd, 0x00e8, 0x0190, 0x0184, 0x017a, 0x01bd, 0x0010,
954 0x006f, 0x0036, 0x0034, 0x0064, 0x00b8, 0x00b2, 0x00a0, 0x0085,
955 0x0101, 0x00f4, 0x00e4, 0x00d9, 0x0181, 0x016e, 0x02cb, 0x000a,
956 0x0062, 0x0030, 0x005b, 0x0058, 0x00a5, 0x009d, 0x0094, 0x0105,
957 0x00f8, 0x0197, 0x018d, 0x0174, 0x017c, 0x0379, 0x0374, 0x0008,
958 0x0055, 0x0054, 0x0051, 0x009f, 0x009c, 0x008f, 0x0104, 0x00f9,
959 0x01ab, 0x0191, 0x0188, 0x017f, 0x02d7, 0x02c9, 0x02c4, 0x0007,
960 0x009a, 0x004c, 0x0049, 0x008d, 0x0083, 0x0100, 0x00f5, 0x01aa,
961 0x0196, 0x018a, 0x0180, 0x02df, 0x0167, 0x02c6, 0x0160, 0x000b,
962 0x008b, 0x0081, 0x0043, 0x007d, 0x00f7, 0x00e9, 0x00e5, 0x00db,
963 0x0189, 0x02e7, 0x02e1, 0x02d0, 0x0375, 0x0372, 0x01b7, 0x0004,
964 0x00f3, 0x0078, 0x0076, 0x0073, 0x00e3, 0x00df, 0x018c, 0x02ea,
965 0x02e6, 0x02e0, 0x02d1, 0x02c8, 0x02c2, 0x00df, 0x01b4, 0x0006,
966 0x00ca, 0x00e0, 0x00de, 0x00da, 0x00d8, 0x0185, 0x0182, 0x017d,
967 0x016c, 0x0378, 0x01bb, 0x02c3, 0x01b8, 0x01b5, 0x06c0, 0x0004,
968 0x02eb, 0x00d3, 0x00d2, 0x00d0, 0x0172, 0x017b, 0x02de, 0x02d3,
969 0x02ca, 0x06c7, 0x0373, 0x036d, 0x036c, 0x0d83, 0x0361, 0x0002,
970 0x0179, 0x0171, 0x0066, 0x00bb, 0x02d6, 0x02d2, 0x0166, 0x02c7,
971 0x02c5, 0x0362, 0x06c6, 0x0367, 0x0d82, 0x0366, 0x01b2, 0x0000,
972 0x000c, 0x000a, 0x0007, 0x000b, 0x000a, 0x0011, 0x000b, 0x0009,
973 0x000d, 0x000c, 0x000a, 0x0007, 0x0005, 0x0003, 0x0001, 0x0003,
976 static immutable uint8_t[256] mp3_huffbits_16 = [
977 1, 4, 6, 8, 9, 9, 10, 10,
978 11, 11, 11, 12, 12, 12, 13, 9,
979 3, 4, 6, 7, 8, 9, 9, 9,
980 10, 10, 10, 11, 12, 11, 12, 8,
981 6, 6, 7, 8, 9, 9, 10, 10,
982 11, 10, 11, 11, 11, 12, 12, 9,
983 8, 7, 8, 9, 9, 10, 10, 10,
984 11, 11, 12, 12, 12, 13, 13, 10,
985 9, 8, 9, 9, 10, 10, 11, 11,
986 11, 12, 12, 12, 13, 13, 13, 9,
987 9, 8, 9, 9, 10, 11, 11, 12,
988 11, 12, 12, 13, 13, 13, 14, 10,
989 10, 9, 9, 10, 11, 11, 11, 11,
990 12, 12, 12, 12, 13, 13, 14, 10,
991 10, 9, 10, 10, 11, 11, 11, 12,
992 12, 13, 13, 13, 13, 15, 15, 10,
993 10, 10, 10, 11, 11, 11, 12, 12,
994 13, 13, 13, 13, 14, 14, 14, 10,
995 11, 10, 10, 11, 11, 12, 12, 13,
996 13, 13, 13, 14, 13, 14, 13, 11,
997 11, 11, 10, 11, 12, 12, 12, 12,
998 13, 14, 14, 14, 15, 15, 14, 10,
999 12, 11, 11, 11, 12, 12, 13, 14,
1000 14, 14, 14, 14, 14, 13, 14, 11,
1001 12, 12, 12, 12, 12, 13, 13, 13,
1002 13, 15, 14, 14, 14, 14, 16, 11,
1003 14, 12, 12, 12, 13, 13, 14, 14,
1004 14, 16, 15, 15, 15, 17, 15, 11,
1005 13, 13, 11, 12, 14, 14, 13, 14,
1006 14, 15, 16, 15, 17, 15, 14, 11,
1007 9, 8, 8, 9, 9, 10, 10, 10,
1008 11, 11, 11, 11, 11, 11, 11, 8,
1011 static immutable uint16_t[256] mp3_huffcodes_24 = [
1012 0x000f, 0x000d, 0x002e, 0x0050, 0x0092, 0x0106, 0x00f8, 0x01b2,
1013 0x01aa, 0x029d, 0x028d, 0x0289, 0x026d, 0x0205, 0x0408, 0x0058,
1014 0x000e, 0x000c, 0x0015, 0x0026, 0x0047, 0x0082, 0x007a, 0x00d8,
1015 0x00d1, 0x00c6, 0x0147, 0x0159, 0x013f, 0x0129, 0x0117, 0x002a,
1016 0x002f, 0x0016, 0x0029, 0x004a, 0x0044, 0x0080, 0x0078, 0x00dd,
1017 0x00cf, 0x00c2, 0x00b6, 0x0154, 0x013b, 0x0127, 0x021d, 0x0012,
1018 0x0051, 0x0027, 0x004b, 0x0046, 0x0086, 0x007d, 0x0074, 0x00dc,
1019 0x00cc, 0x00be, 0x00b2, 0x0145, 0x0137, 0x0125, 0x010f, 0x0010,
1020 0x0093, 0x0048, 0x0045, 0x0087, 0x007f, 0x0076, 0x0070, 0x00d2,
1021 0x00c8, 0x00bc, 0x0160, 0x0143, 0x0132, 0x011d, 0x021c, 0x000e,
1022 0x0107, 0x0042, 0x0081, 0x007e, 0x0077, 0x0072, 0x00d6, 0x00ca,
1023 0x00c0, 0x00b4, 0x0155, 0x013d, 0x012d, 0x0119, 0x0106, 0x000c,
1024 0x00f9, 0x007b, 0x0079, 0x0075, 0x0071, 0x00d7, 0x00ce, 0x00c3,
1025 0x00b9, 0x015b, 0x014a, 0x0134, 0x0123, 0x0110, 0x0208, 0x000a,
1026 0x01b3, 0x0073, 0x006f, 0x006d, 0x00d3, 0x00cb, 0x00c4, 0x00bb,
1027 0x0161, 0x014c, 0x0139, 0x012a, 0x011b, 0x0213, 0x017d, 0x0011,
1028 0x01ab, 0x00d4, 0x00d0, 0x00cd, 0x00c9, 0x00c1, 0x00ba, 0x00b1,
1029 0x00a9, 0x0140, 0x012f, 0x011e, 0x010c, 0x0202, 0x0179, 0x0010,
1030 0x014f, 0x00c7, 0x00c5, 0x00bf, 0x00bd, 0x00b5, 0x00ae, 0x014d,
1031 0x0141, 0x0131, 0x0121, 0x0113, 0x0209, 0x017b, 0x0173, 0x000b,
1032 0x029c, 0x00b8, 0x00b7, 0x00b3, 0x00af, 0x0158, 0x014b, 0x013a,
1033 0x0130, 0x0122, 0x0115, 0x0212, 0x017f, 0x0175, 0x016e, 0x000a,
1034 0x028c, 0x015a, 0x00ab, 0x00a8, 0x00a4, 0x013e, 0x0135, 0x012b,
1035 0x011f, 0x0114, 0x0107, 0x0201, 0x0177, 0x0170, 0x016a, 0x0006,
1036 0x0288, 0x0142, 0x013c, 0x0138, 0x0133, 0x012e, 0x0124, 0x011c,
1037 0x010d, 0x0105, 0x0200, 0x0178, 0x0172, 0x016c, 0x0167, 0x0004,
1038 0x026c, 0x012c, 0x0128, 0x0126, 0x0120, 0x011a, 0x0111, 0x010a,
1039 0x0203, 0x017c, 0x0176, 0x0171, 0x016d, 0x0169, 0x0165, 0x0002,
1040 0x0409, 0x0118, 0x0116, 0x0112, 0x010b, 0x0108, 0x0103, 0x017e,
1041 0x017a, 0x0174, 0x016f, 0x016b, 0x0168, 0x0166, 0x0164, 0x0000,
1042 0x002b, 0x0014, 0x0013, 0x0011, 0x000f, 0x000d, 0x000b, 0x0009,
1043 0x0007, 0x0006, 0x0004, 0x0007, 0x0005, 0x0003, 0x0001, 0x0003,
1046 static immutable uint8_t[256] mp3_huffbits_24 = [
1047 4, 4, 6, 7, 8, 9, 9, 10,
1048 10, 11, 11, 11, 11, 11, 12, 9,
1049 4, 4, 5, 6, 7, 8, 8, 9,
1050 9, 9, 10, 10, 10, 10, 10, 8,
1051 6, 5, 6, 7, 7, 8, 8, 9,
1052 9, 9, 9, 10, 10, 10, 11, 7,
1053 7, 6, 7, 7, 8, 8, 8, 9,
1054 9, 9, 9, 10, 10, 10, 10, 7,
1055 8, 7, 7, 8, 8, 8, 8, 9,
1056 9, 9, 10, 10, 10, 10, 11, 7,
1057 9, 7, 8, 8, 8, 8, 9, 9,
1058 9, 9, 10, 10, 10, 10, 10, 7,
1059 9, 8, 8, 8, 8, 9, 9, 9,
1060 9, 10, 10, 10, 10, 10, 11, 7,
1061 10, 8, 8, 8, 9, 9, 9, 9,
1062 10, 10, 10, 10, 10, 11, 11, 8,
1063 10, 9, 9, 9, 9, 9, 9, 9,
1064 9, 10, 10, 10, 10, 11, 11, 8,
1065 10, 9, 9, 9, 9, 9, 9, 10,
1066 10, 10, 10, 10, 11, 11, 11, 8,
1067 11, 9, 9, 9, 9, 10, 10, 10,
1068 10, 10, 10, 11, 11, 11, 11, 8,
1069 11, 10, 9, 9, 9, 10, 10, 10,
1070 10, 10, 10, 11, 11, 11, 11, 8,
1071 11, 10, 10, 10, 10, 10, 10, 10,
1072 10, 10, 11, 11, 11, 11, 11, 8,
1073 11, 10, 10, 10, 10, 10, 10, 10,
1074 11, 11, 11, 11, 11, 11, 11, 8,
1075 12, 10, 10, 10, 10, 10, 10, 11,
1076 11, 11, 11, 11, 11, 11, 11, 8,
1077 8, 7, 7, 7, 7, 7, 7, 7,
1078 7, 7, 7, 8, 8, 8, 8, 4,
1081 static immutable huff_table_t[16] mp3_huff_tables = [
1082 huff_table_t( 1, null, null ),
1083 huff_table_t( 2, mp3_huffbits_1.ptr, mp3_huffcodes_1.ptr ),
1084 huff_table_t( 3, mp3_huffbits_2.ptr, mp3_huffcodes_2.ptr ),
1085 huff_table_t( 3, mp3_huffbits_3.ptr, mp3_huffcodes_3.ptr ),
1086 huff_table_t( 4, mp3_huffbits_5.ptr, mp3_huffcodes_5.ptr ),
1087 huff_table_t( 4, mp3_huffbits_6.ptr, mp3_huffcodes_6.ptr ),
1088 huff_table_t( 6, mp3_huffbits_7.ptr, mp3_huffcodes_7.ptr ),
1089 huff_table_t( 6, mp3_huffbits_8.ptr, mp3_huffcodes_8.ptr ),
1090 huff_table_t( 6, mp3_huffbits_9.ptr, mp3_huffcodes_9.ptr ),
1091 huff_table_t( 8, mp3_huffbits_10.ptr, mp3_huffcodes_10.ptr ),
1092 huff_table_t( 8, mp3_huffbits_11.ptr, mp3_huffcodes_11.ptr ),
1093 huff_table_t( 8, mp3_huffbits_12.ptr, mp3_huffcodes_12.ptr ),
1094 huff_table_t( 16, mp3_huffbits_13.ptr, mp3_huffcodes_13.ptr ),
1095 huff_table_t( 16, mp3_huffbits_15.ptr, mp3_huffcodes_15.ptr ),
1096 huff_table_t( 16, mp3_huffbits_16.ptr, mp3_huffcodes_16.ptr ),
1097 huff_table_t( 16, mp3_huffbits_24.ptr, mp3_huffcodes_24.ptr ),
1100 static immutable uint8_t[2][32] mp3_huff_data = [
1101 [ 0, 0 ],
1102 [ 1, 0 ],
1103 [ 2, 0 ],
1104 [ 3, 0 ],
1105 [ 0, 0 ],
1106 [ 4, 0 ],
1107 [ 5, 0 ],
1108 [ 6, 0 ],
1109 [ 7, 0 ],
1110 [ 8, 0 ],
1111 [ 9, 0 ],
1112 [ 10, 0 ],
1113 [ 11, 0 ],
1114 [ 12, 0 ],
1115 [ 0, 0 ],
1116 [ 13, 0 ],
1117 [ 14, 1 ],
1118 [ 14, 2 ],
1119 [ 14, 3 ],
1120 [ 14, 4 ],
1121 [ 14, 6 ],
1122 [ 14, 8 ],
1123 [ 14, 10 ],
1124 [ 14, 13 ],
1125 [ 15, 4 ],
1126 [ 15, 5 ],
1127 [ 15, 6 ],
1128 [ 15, 7 ],
1129 [ 15, 8 ],
1130 [ 15, 9 ],
1131 [ 15, 11 ],
1132 [ 15, 13 ],
1135 static immutable uint8_t[16][2] mp3_quad_codes = [
1136 [ 1, 5, 4, 5, 6, 5, 4, 4, 7, 3, 6, 0, 7, 2, 3, 1, ],
1137 [ 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, ],
1140 static immutable uint8_t[16][2] mp3_quad_bits = [
1141 [ 1, 4, 4, 5, 4, 6, 5, 6, 4, 5, 5, 6, 5, 6, 6, 6, ],
1142 [ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ],
1145 static immutable uint8_t[22][9] band_size_long = [
1146 [ 4, 4, 4, 4, 4, 4, 6, 6, 8, 8, 10,
1147 12, 16, 20, 24, 28, 34, 42, 50, 54, 76, 158, ], /* 44100 */
1148 [ 4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 10,
1149 12, 16, 18, 22, 28, 34, 40, 46, 54, 54, 192, ], /* 48000 */
1150 [ 4, 4, 4, 4, 4, 4, 6, 6, 8, 10, 12,
1151 16, 20, 24, 30, 38, 46, 56, 68, 84, 102, 26, ], /* 32000 */
1152 [ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
1153 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, ], /* 22050 */
1154 [ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
1155 18, 22, 26, 32, 38, 46, 52, 64, 70, 76, 36, ], /* 24000 */
1156 [ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
1157 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, ], /* 16000 */
1158 [ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
1159 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, ], /* 11025 */
1160 [ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
1161 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, ], /* 12000 */
1162 [ 12, 12, 12, 12, 12, 12, 16, 20, 24, 28, 32,
1163 40, 48, 56, 64, 76, 90, 2, 2, 2, 2, 2, ], /* 8000 */
1166 static immutable uint8_t[13][9] band_size_short = [
1167 [ 4, 4, 4, 4, 6, 8, 10, 12, 14, 18, 22, 30, 56, ], /* 44100 */
1168 [ 4, 4, 4, 4, 6, 6, 10, 12, 14, 16, 20, 26, 66, ], /* 48000 */
1169 [ 4, 4, 4, 4, 6, 8, 12, 16, 20, 26, 34, 42, 12, ], /* 32000 */
1170 [ 4, 4, 4, 6, 6, 8, 10, 14, 18, 26, 32, 42, 18, ], /* 22050 */
1171 [ 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 32, 44, 12, ], /* 24000 */
1172 [ 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18, ], /* 16000 */
1173 [ 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18, ], /* 11025 */
1174 [ 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18, ], /* 12000 */
1175 [ 8, 8, 8, 12, 16, 20, 24, 28, 36, 2, 2, 2, 26, ], /* 8000 */
1178 static immutable uint8_t[22][2] mp3_pretab = [
1179 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
1180 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0 ],
1183 static immutable float[8] ci_table = [
1184 -0.6f, -0.535f, -0.33f, -0.185f, -0.095f, -0.041f, -0.0142f, -0.0037f,
1187 enum C1 = FIXHR!(0.98480775301220805936/2);
1188 enum C2 = FIXHR!(0.93969262078590838405/2);
1189 enum C3 = FIXHR!(0.86602540378443864676/2);
1190 enum C4 = FIXHR!(0.76604444311897803520/2);
1191 enum C5 = FIXHR!(0.64278760968653932632/2);
1192 enum C6 = FIXHR!(0.5/2);
1193 enum C7 = FIXHR!(0.34202014332566873304/2);
1194 enum C8 = FIXHR!(0.17364817766693034885/2);
1196 static immutable int[9] icos36 = [
1197 FIXR!(0.50190991877167369479),
1198 FIXR!(0.51763809020504152469), //0
1199 FIXR!(0.55168895948124587824),
1200 FIXR!(0.61038729438072803416),
1201 FIXR!(0.70710678118654752439), //1
1202 FIXR!(0.87172339781054900991),
1203 FIXR!(1.18310079157624925896),
1204 FIXR!(1.93185165257813657349), //2
1205 FIXR!(5.73685662283492756461),
1208 static immutable int[9] icos36h = [
1209 FIXHR!(0.50190991877167369479/2),
1210 FIXHR!(0.51763809020504152469/2), //0
1211 FIXHR!(0.55168895948124587824/2),
1212 FIXHR!(0.61038729438072803416/2),
1213 FIXHR!(0.70710678118654752439/2), //1
1214 FIXHR!(0.87172339781054900991/2),
1215 FIXHR!(1.18310079157624925896/4),
1216 FIXHR!(1.93185165257813657349/4), //2
1217 //FIXHR!(5.73685662283492756461),
1220 ////////////////////////////////////////////////////////////////////////////////
1222 int unaligned32_be (const(uint8_t)* p) {
1223 static if (__VERSION__ > 2067) pragma(inline, true);
1224 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
1227 enum MIN_CACHE_BITS = 25;
1229 enum NEG_SSR32(string a, string s) = "((cast( int32_t)("~a~"))>>(32-("~s~")))";
1230 enum NEG_USR32(string a, string s) = "((cast(uint32_t)("~a~"))>>(32-("~s~")))";
1232 enum OPEN_READER(string name, string gb) =
1233 "int "~name~"_index = ("~gb~").index;\n"~
1234 "int "~name~"_cache = 0;\n";
1236 enum CLOSE_READER(string name, string gb) = "("~gb~").index = "~name~"_index;";
1238 enum UPDATE_CACHE(string name, string gb) = name~"_cache = unaligned32_be(&(("~gb~").buffer["~name~"_index>>3])) << ("~name~"_index&0x07);";
1240 enum SKIP_CACHE(string name, string gb, string num) = name~"_cache <<= ("~num~");";
1242 enum SKIP_COUNTER(string name, string gb, string num) = name~"_index += ("~num~");";
1244 enum SKIP_BITS(string name, string gb, string num) = "{"~SKIP_CACHE!(name, gb, num)~SKIP_COUNTER!(name, gb, num)~"}";
1246 enum LAST_SKIP_BITS(string name, string gb, string num) = SKIP_COUNTER!(name, gb, num);
1247 enum LAST_SKIP_CACHE(string name, string gb, string num) = "{}";
1249 enum SHOW_UBITS(string name, string gb, string num) = NEG_USR32!(name~"_cache", num);
1251 enum SHOW_SBITS(string name, string gb, string num) = NEG_SSR32(name~"_cache", num);
1253 enum GET_CACHE(string name, string gb) = "(cast(uint32_t)"~name~"_cache)";
1255 int get_bits_count() (const(bitstream_t)* s) { static if (__VERSION__ > 2067) pragma(inline, true); return s.index; }
1257 void skip_bits_long (bitstream_t* s, int n) { static if (__VERSION__ > 2067) pragma(inline, true); s.index += n; }
1258 //#define skip_bits skip_bits_long
1259 alias skip_bits = skip_bits_long;
1261 void init_get_bits (bitstream_t* s, const(uint8_t)* buffer, int bit_size) {
1262 int buffer_size = (bit_size+7)>>3;
1263 if (buffer_size < 0 || bit_size < 0) {
1264 buffer_size = bit_size = 0;
1265 buffer = null;
1267 s.buffer = buffer;
1268 s.size_in_bits = bit_size;
1269 s.buffer_end = buffer + buffer_size;
1270 s.index = 0;
1273 uint get_bits (bitstream_t* s, int n){
1274 int tmp;
1275 mixin(OPEN_READER!("re", "s"));
1276 mixin(UPDATE_CACHE!("re", "s"));
1277 tmp = mixin(SHOW_UBITS!("re", "s", "n"));
1278 mixin(LAST_SKIP_BITS!("re", "s", "n"));
1279 mixin(CLOSE_READER!("re", "s"));
1280 return tmp;
1283 int get_bitsz (bitstream_t* s, int n) {
1284 static if (__VERSION__ > 2067) pragma(inline, true);
1285 return (n == 0 ? 0 : get_bits(s, n));
1288 uint get_bits1 (bitstream_t* s) {
1289 int index = s.index;
1290 uint8_t result = s.buffer[index>>3];
1291 result <<= (index&0x07);
1292 result >>= 8 - 1;
1293 ++index;
1294 s.index = index;
1295 return result;
1298 void align_get_bits (bitstream_t* s) {
1299 int n = (-get_bits_count(s)) & 7;
1300 if (n) skip_bits(s, n);
1303 enum GET_DATA(string v, string table, string i, string wrap, string size) =
1304 "{\n"~
1305 " const(uint8_t)* ptr = cast(const(uint8_t)*)"~table~"+"~i~"*"~wrap~";\n"~
1306 " switch ("~size~") {\n"~
1307 " case 1: "~v~" = *cast(const(uint8_t)*)ptr; break;\n"~
1308 " case 2: "~v~" = *cast(const(uint16_t)*)ptr; break;\n"~
1309 " default: "~v~" = *cast(const(uint32_t)*)ptr; break;\n"~
1310 " }\n"~
1311 "}\n"~
1314 int alloc_table (vlc_t* vlc, int size) {
1315 int index;
1316 index = vlc.table_size;
1317 vlc.table_size += size;
1318 if (vlc.table_size > vlc.table_allocated) {
1319 vlc.table_allocated += (1 << vlc.bits);
1320 vlc.table = cast(VT2*)libc_realloc(vlc.table, VT2.sizeof * vlc.table_allocated);
1321 if (!vlc.table) return -1;
1323 return index;
1327 int build_table (
1328 vlc_t* vlc, int table_nb_bits,
1329 int nb_codes,
1330 const(void)* bits, int bits_wrap, int bits_size,
1331 const(void)* codes, int codes_wrap, int codes_size,
1332 uint32_t code_prefix, int n_prefix
1334 int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2;
1335 uint32_t code;
1336 //VLC_TYPE (*table)[2];
1337 VT2* table;
1339 table_size = 1 << table_nb_bits;
1340 table_index = alloc_table(vlc, table_size);
1341 if (table_index < 0) return -1;
1342 table = &vlc.table[table_index];
1344 for (i = 0; i < table_size; i++) {
1345 table[i][1] = 0; //bits
1346 table[i][0] = -1; //codes
1349 for (i = 0; i < nb_codes; i++) {
1350 mixin(GET_DATA!("n", "bits", "i", "bits_wrap", "bits_size"));
1351 mixin(GET_DATA!("code", "codes", "i", "codes_wrap", "codes_size"));
1352 if (n <= 0) continue;
1353 n -= n_prefix;
1354 code_prefix2 = code >> n;
1355 if (n > 0 && code_prefix2 == code_prefix) {
1356 if (n <= table_nb_bits) {
1357 j = (code << (table_nb_bits - n)) & (table_size - 1);
1358 nb = 1 << (table_nb_bits - n);
1359 for(k=0;k<nb;k++) {
1360 if (table[j][1] /*bits*/ != 0) {
1361 return -1;
1363 table[j][1] = cast(short)n; //bits
1364 table[j][0] = cast(short)i; //code
1365 j++;
1367 } else {
1368 n -= table_nb_bits;
1369 j = (code >> n) & ((1 << table_nb_bits) - 1);
1370 n1 = -cast(int)table[j][1]; //bits
1371 if (n > n1)
1372 n1 = n;
1373 table[j][1] = cast(short)(-n1); //bits
1377 for(i=0;i<table_size;i++) {
1378 n = table[i][1]; //bits
1379 if (n < 0) {
1380 n = -n;
1381 if (n > table_nb_bits) {
1382 n = table_nb_bits;
1383 table[i][1] = cast(short)(-n); //bits
1385 index = build_table(vlc, n, nb_codes,
1386 bits, bits_wrap, bits_size,
1387 codes, codes_wrap, codes_size,
1388 (code_prefix << table_nb_bits) | i,
1389 n_prefix + table_nb_bits);
1390 if (index < 0)
1391 return -1;
1392 table = &vlc.table[table_index];
1393 table[i][0] = cast(short)index; //code
1396 return table_index;
1399 int init_vlc(
1400 vlc_t *vlc, int nb_bits, int nb_codes,
1401 const void *bits, int bits_wrap, int bits_size,
1402 const void *codes, int codes_wrap, int codes_size
1404 vlc.bits = nb_bits;
1405 if (build_table(vlc, nb_bits, nb_codes,
1406 bits, bits_wrap, bits_size,
1407 codes, codes_wrap, codes_size,
1408 0, 0) < 0) {
1409 libc_free(vlc.table);
1410 return -1;
1412 return 0;
1415 enum GET_VLC(string code, string name, string gb, string table, string bits, string max_depth) =
1416 "{\n"~
1417 " int n, index, nb_bits;\n"~
1418 "\n"~
1419 " index= "~SHOW_UBITS!(name, gb, bits)~";\n"~
1420 " "~code~" = "~table~"[index][0];\n"~
1421 " n = "~table~"[index][1];\n"~
1422 "\n"~
1423 " if ("~max_depth~" > 1 && n < 0){\n"~
1424 " "~LAST_SKIP_BITS!(name, gb, bits)~"\n"~
1425 " "~UPDATE_CACHE!(name, gb)~"\n"~
1426 "\n"~
1427 " nb_bits = -n;\n"~
1428 "\n"~
1429 " index= "~SHOW_UBITS!(name, gb, "nb_bits")~" + "~code~";\n"~
1430 " "~code~" = "~table~"[index][0];\n"~
1431 " n = "~table~"[index][1];\n"~
1432 " if ("~max_depth~" > 2 && n < 0){\n"~
1433 " "~LAST_SKIP_BITS!(name, gb, "nb_bits")~"\n"~
1434 " "~UPDATE_CACHE!(name, gb)~"\n"~
1435 "\n"~
1436 " nb_bits = -n;\n"~
1437 "\n"~
1438 " index= "~SHOW_UBITS!(name, gb, "nb_bits")~" + "~code~";\n"~
1439 " "~code~" = "~table~"[index][0];\n"~
1440 " n = "~table~"[index][1];\n"~
1441 " }\n"~
1442 " }\n"~
1443 " "~SKIP_BITS!(name, gb, "n")~"\n"~
1444 "}\n"~
1447 int get_vlc2(bitstream_t *s, VT2* table, int bits, int max_depth) {
1448 int code;
1450 mixin(OPEN_READER!("re", "s"));
1451 mixin(UPDATE_CACHE!("re", "s"));
1453 mixin(GET_VLC!("code", "re", "s", "table", "bits", "max_depth"));
1455 mixin(CLOSE_READER!("re", "s"));
1456 return code;
1459 void switch_buffer (mp3_context_t *s, int *pos, int *end_pos, int *end_pos2) {
1460 if(s.in_gb.buffer && *pos >= s.gb.size_in_bits){
1461 s.gb= s.in_gb;
1462 s.in_gb.buffer=null;
1463 skip_bits_long(&s.gb, *pos - *end_pos);
1464 *end_pos2=
1465 *end_pos= *end_pos2 + get_bits_count(&s.gb) - *pos;
1466 *pos= get_bits_count(&s.gb);
1471 // ////////////////////////////////////////////////////////////////////////// //
1472 int mp3_check_header(uint32_t header){
1473 //pragma(inline, true);
1474 /* header */
1475 if ((header & 0xffe00000) != 0xffe00000) return -1;
1476 /* layer check */
1477 if ((header & (3<<17)) != (1 << 17)) return -1;
1478 /* bit rate */
1479 if ((header & (0xf<<12)) == 0xf<<12) return -1;
1480 /* frequency */
1481 if ((header & (3<<10)) == 3<<10) return -1;
1482 return 0;
1486 void lsf_sf_expand (int *slen, int sf, int n1, int n2, int n3) {
1487 if (n3) {
1488 slen[3] = sf % n3;
1489 sf /= n3;
1490 } else {
1491 slen[3] = 0;
1493 if (n2) {
1494 slen[2] = sf % n2;
1495 sf /= n2;
1496 } else {
1497 slen[2] = 0;
1499 slen[1] = sf % n1;
1500 sf /= n1;
1501 slen[0] = sf;
1504 int l3_unscale(int value, int exponent)
1506 uint m;
1507 int e;
1509 e = table_4_3_exp [4*value + (exponent&3)];
1510 m = table_4_3_value[4*value + (exponent&3)];
1511 e -= (exponent >> 2);
1512 if (e > 31)
1513 return 0;
1514 m = (m + (1 << (e-1))) >> e;
1516 return m;
1519 int round_sample(int *sum) {
1520 int sum1;
1521 sum1 = (*sum) >> OUT_SHIFT;
1522 *sum &= (1<<OUT_SHIFT)-1;
1523 if (sum1 < OUT_MIN)
1524 sum1 = OUT_MIN;
1525 else if (sum1 > OUT_MAX)
1526 sum1 = OUT_MAX;
1527 return sum1;
1530 void exponents_from_scale_factors (mp3_context_t *s, granule_t *g, int16_t *exponents) {
1531 const(uint8_t)* bstab, pretab;
1532 int len, i, j, k, l, v0, shift, gain;
1533 int[3] gains;
1534 int16_t *exp_ptr;
1536 exp_ptr = exponents;
1537 gain = g.global_gain - 210;
1538 shift = g.scalefac_scale + 1;
1540 bstab = band_size_long[s.sample_rate_index].ptr;
1541 pretab = mp3_pretab[g.preflag].ptr;
1542 for(i=0;i<g.long_end;i++) {
1543 v0 = gain - ((g.scale_factors[i] + pretab[i]) << shift) + 400;
1544 len = bstab[i];
1545 for(j=len;j>0;j--)
1546 *exp_ptr++ = cast(short)v0;
1549 if (g.short_start < 13) {
1550 bstab = band_size_short[s.sample_rate_index].ptr;
1551 gains[0] = gain - (g.subblock_gain[0] << 3);
1552 gains[1] = gain - (g.subblock_gain[1] << 3);
1553 gains[2] = gain - (g.subblock_gain[2] << 3);
1554 k = g.long_end;
1555 for(i=g.short_start;i<13;i++) {
1556 len = bstab[i];
1557 for(l=0;l<3;l++) {
1558 v0 = gains[l] - (g.scale_factors[k++] << shift) + 400;
1559 for(j=len;j>0;j--)
1560 *exp_ptr++ = cast(short)v0;
1566 void reorder_block(mp3_context_t *s, granule_t *g)
1568 int i, j, len;
1569 int32_t* ptr, dst, ptr1;
1570 int32_t[576] tmp;
1572 if (g.block_type != 2)
1573 return;
1575 if (g.switch_point) {
1576 if (s.sample_rate_index != 8) {
1577 ptr = g.sb_hybrid.ptr + 36;
1578 } else {
1579 ptr = g.sb_hybrid.ptr + 48;
1581 } else {
1582 ptr = g.sb_hybrid.ptr;
1585 for(i=g.short_start;i<13;i++) {
1586 len = band_size_short[s.sample_rate_index][i];
1587 ptr1 = ptr;
1588 dst = tmp.ptr;
1589 for(j=len;j>0;j--) {
1590 *dst++ = ptr[0*len];
1591 *dst++ = ptr[1*len];
1592 *dst++ = ptr[2*len];
1593 ptr++;
1595 ptr+=2*len;
1596 libc_memcpy(ptr1, tmp.ptr, len * 3 * (*ptr1).sizeof);
1600 void compute_antialias(mp3_context_t *s, granule_t *g) {
1601 enum INT_AA(string j) =
1602 "tmp0 = ptr[-1-"~j~"];\n"~
1603 "tmp1 = ptr[ "~j~"];\n"~
1604 "tmp2= cast(int)MULH(tmp0 + tmp1, csa[0+4*"~j~"]);\n"~
1605 "ptr[-1-"~j~"] = cast(int)(4*(tmp2 - MULH(tmp1, csa[2+4*"~j~"])));\n"~
1606 "ptr[ "~j~"] = cast(int)(4*(tmp2 + MULH(tmp0, csa[3+4*"~j~"])));\n";
1608 int32_t* ptr, csa;
1609 int n, i;
1611 /* we antialias only "long" bands */
1612 if (g.block_type == 2) {
1613 if (!g.switch_point)
1614 return;
1615 /* XXX: check this for 8000Hz case */
1616 n = 1;
1617 } else {
1618 n = SBLIMIT - 1;
1621 ptr = g.sb_hybrid.ptr + 18;
1622 for(i = n;i > 0;i--) {
1623 int tmp0, tmp1, tmp2;
1624 csa = &csa_table[0][0];
1626 mixin(INT_AA!("0"));
1627 mixin(INT_AA!("1"));
1628 mixin(INT_AA!("2"));
1629 mixin(INT_AA!("3"));
1630 mixin(INT_AA!("4"));
1631 mixin(INT_AA!("5"));
1632 mixin(INT_AA!("6"));
1633 mixin(INT_AA!("7"));
1635 ptr += 18;
1639 void compute_stereo (mp3_context_t *s, granule_t *g0, granule_t *g1) {
1640 int i, j, k, l;
1641 int32_t v1, v2;
1642 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1643 int32_t[16]* is_tab;
1644 int32_t* tab0, tab1;
1645 int[3] non_zero_found_short;
1647 if (s.mode_ext & MODE_EXT_I_STEREO) {
1648 if (!s.lsf) {
1649 is_tab = is_table.ptr;
1650 sf_max = 7;
1651 } else {
1652 is_tab = is_table_lsf[g1.scalefac_compress & 1].ptr;
1653 sf_max = 16;
1656 tab0 = g0.sb_hybrid.ptr + 576;
1657 tab1 = g1.sb_hybrid.ptr + 576;
1659 non_zero_found_short[0] = 0;
1660 non_zero_found_short[1] = 0;
1661 non_zero_found_short[2] = 0;
1662 k = (13 - g1.short_start) * 3 + g1.long_end - 3;
1663 for(i = 12;i >= g1.short_start;i--) {
1664 /* for last band, use previous scale factor */
1665 if (i != 11)
1666 k -= 3;
1667 len = band_size_short[s.sample_rate_index][i];
1668 for(l=2;l>=0;l--) {
1669 tab0 -= len;
1670 tab1 -= len;
1671 if (!non_zero_found_short[l]) {
1672 /* test if non zero band. if so, stop doing i-stereo */
1673 for(j=0;j<len;j++) {
1674 if (tab1[j] != 0) {
1675 non_zero_found_short[l] = 1;
1676 goto found1;
1679 sf = g1.scale_factors[k + l];
1680 if (sf >= sf_max)
1681 goto found1;
1683 v1 = is_tab[0][sf];
1684 v2 = is_tab[1][sf];
1685 for(j=0;j<len;j++) {
1686 tmp0 = tab0[j];
1687 tab0[j] = cast(int)MULL(tmp0, v1);
1688 tab1[j] = cast(int)MULL(tmp0, v2);
1690 } else {
1691 found1:
1692 if (s.mode_ext & MODE_EXT_MS_STEREO) {
1693 /* lower part of the spectrum : do ms stereo
1694 if enabled */
1695 for(j=0;j<len;j++) {
1696 tmp0 = tab0[j];
1697 tmp1 = tab1[j];
1698 tab0[j] = cast(int)MULL(tmp0 + tmp1, ISQRT2);
1699 tab1[j] = cast(int)MULL(tmp0 - tmp1, ISQRT2);
1706 non_zero_found = non_zero_found_short[0] |
1707 non_zero_found_short[1] |
1708 non_zero_found_short[2];
1710 for(i = g1.long_end - 1;i >= 0;i--) {
1711 len = band_size_long[s.sample_rate_index][i];
1712 tab0 -= len;
1713 tab1 -= len;
1714 /* test if non zero band. if so, stop doing i-stereo */
1715 if (!non_zero_found) {
1716 for(j=0;j<len;j++) {
1717 if (tab1[j] != 0) {
1718 non_zero_found = 1;
1719 goto found2;
1722 /* for last band, use previous scale factor */
1723 k = (i == 21) ? 20 : i;
1724 sf = g1.scale_factors[k];
1725 if (sf >= sf_max)
1726 goto found2;
1727 v1 = is_tab[0][sf];
1728 v2 = is_tab[1][sf];
1729 for(j=0;j<len;j++) {
1730 tmp0 = tab0[j];
1731 tab0[j] = cast(int)MULL(tmp0, v1);
1732 tab1[j] = cast(int)MULL(tmp0, v2);
1734 } else {
1735 found2:
1736 if (s.mode_ext & MODE_EXT_MS_STEREO) {
1737 /* lower part of the spectrum : do ms stereo
1738 if enabled */
1739 for(j=0;j<len;j++) {
1740 tmp0 = tab0[j];
1741 tmp1 = tab1[j];
1742 tab0[j] = cast(int)MULL(tmp0 + tmp1, ISQRT2);
1743 tab1[j] = cast(int)MULL(tmp0 - tmp1, ISQRT2);
1748 } else if (s.mode_ext & MODE_EXT_MS_STEREO) {
1749 /* ms stereo ONLY */
1750 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1751 global gain */
1752 tab0 = g0.sb_hybrid.ptr;
1753 tab1 = g1.sb_hybrid.ptr;
1754 for(i=0;i<576;i++) {
1755 tmp0 = tab0[i];
1756 tmp1 = tab1[i];
1757 tab0[i] = tmp0 + tmp1;
1758 tab1[i] = tmp0 - tmp1;
1763 int huffman_decode (mp3_context_t *s, granule_t *g, int16_t *exponents, int end_pos2) {
1764 int s_index;
1765 int i;
1766 int last_pos, bits_left;
1767 vlc_t* vlc;
1768 int end_pos = s.gb.size_in_bits;
1769 if (end_pos2 < end_pos) end_pos = end_pos2;
1771 /* low frequencies (called big values) */
1772 s_index = 0;
1773 for(i=0;i<3;i++) {
1774 int j, k, l, linbits;
1775 j = g.region_size[i];
1776 if (j == 0)
1777 continue;
1778 /* select vlc table */
1779 k = g.table_select[i];
1780 l = mp3_huff_data[k][0];
1781 linbits = mp3_huff_data[k][1];
1782 vlc = &huff_vlc[l];
1784 if(!l){
1785 libc_memset(&g.sb_hybrid[s_index], 0, (g.sb_hybrid[0]).sizeof*2*j);
1786 s_index += 2*j;
1787 continue;
1790 /* read huffcode and compute each couple */
1791 for(;j>0;j--) {
1792 int exponent, x, y, v;
1793 int pos= get_bits_count(&s.gb);
1795 if (pos >= end_pos){
1796 switch_buffer(s, &pos, &end_pos, &end_pos2);
1797 if(pos >= end_pos)
1798 break;
1800 y = get_vlc2(&s.gb, vlc.table, 7, 3);
1802 if(!y){
1803 g.sb_hybrid[s_index ] =
1804 g.sb_hybrid[s_index+1] = 0;
1805 s_index += 2;
1806 continue;
1809 exponent= exponents[s_index];
1811 if(y&16){
1812 x = y >> 5;
1813 y = y & 0x0f;
1814 if (x < 15){
1815 v = expval_table[ exponent ][ x ];
1816 }else{
1817 x += get_bitsz(&s.gb, linbits);
1818 v = l3_unscale(x, exponent);
1820 if (get_bits1(&s.gb))
1821 v = -v;
1822 g.sb_hybrid[s_index] = v;
1823 if (y < 15){
1824 v = expval_table[ exponent ][ y ];
1825 }else{
1826 y += get_bitsz(&s.gb, linbits);
1827 v = l3_unscale(y, exponent);
1829 if (get_bits1(&s.gb))
1830 v = -v;
1831 g.sb_hybrid[s_index+1] = v;
1832 }else{
1833 x = y >> 5;
1834 y = y & 0x0f;
1835 x += y;
1836 if (x < 15){
1837 v = expval_table[ exponent ][ x ];
1838 }else{
1839 x += get_bitsz(&s.gb, linbits);
1840 v = l3_unscale(x, exponent);
1842 if (get_bits1(&s.gb))
1843 v = -v;
1844 g.sb_hybrid[s_index+!!y] = v;
1845 g.sb_hybrid[s_index+ !y] = 0;
1847 s_index+=2;
1851 /* high frequencies */
1852 vlc = &huff_quad_vlc[g.count1table_select];
1853 last_pos=0;
1854 while (s_index <= 572) {
1855 int pos, code;
1856 pos = get_bits_count(&s.gb);
1857 if (pos >= end_pos) {
1858 if (pos > end_pos2 && last_pos){
1859 /* some encoders generate an incorrect size for this
1860 part. We must go back into the data */
1861 s_index -= 4;
1862 skip_bits_long(&s.gb, last_pos - pos);
1863 break;
1865 switch_buffer(s, &pos, &end_pos, &end_pos2);
1866 if(pos >= end_pos)
1867 break;
1869 last_pos= pos;
1871 code = get_vlc2(&s.gb, vlc.table, vlc.bits, 1);
1872 g.sb_hybrid[s_index+0]=
1873 g.sb_hybrid[s_index+1]=
1874 g.sb_hybrid[s_index+2]=
1875 g.sb_hybrid[s_index+3]= 0;
1876 while(code){
1877 static immutable int[16] idxtab = [3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0];
1878 int v;
1879 int pos_= s_index+idxtab[code];
1880 code ^= 8>>idxtab[code];
1881 v = exp_table[ exponents[pos_] ];
1882 if(get_bits1(&s.gb))
1883 v = -v;
1884 g.sb_hybrid[pos_] = v;
1886 s_index+=4;
1889 if (s_index >= g.sb_hybrid.length) {
1890 import core.stdc.stdio : printf;
1891 printf("s_index=%u; len=%u; len=%u\n", cast(uint)s_index, cast(uint)g.sb_hybrid.length, cast(uint)((g.sb_hybrid[0]).sizeof*(576 - s_index)));
1892 assert(0);
1895 if ((g.sb_hybrid[0]).sizeof*(576 - s_index) > 0) {
1896 libc_memset(&g.sb_hybrid[s_index], 0, (g.sb_hybrid[0]).sizeof*(576 - s_index));
1899 /* skip extension bits */
1900 bits_left = end_pos2 - get_bits_count(&s.gb);
1901 if (bits_left < 0) {
1902 return -1;
1904 skip_bits_long(&s.gb, bits_left);
1906 i= get_bits_count(&s.gb);
1907 switch_buffer(s, &i, &end_pos, &end_pos2);
1909 return 0;
1913 // ////////////////////////////////////////////////////////////////////////// //
1914 void imdct12(int *out_, int *in_)
1916 int in0, in1, in2, in3, in4, in5, t1, t2;
1918 in0= in_[0*3];
1919 in1= in_[1*3] + in_[0*3];
1920 in2= in_[2*3] + in_[1*3];
1921 in3= in_[3*3] + in_[2*3];
1922 in4= in_[4*3] + in_[3*3];
1923 in5= in_[5*3] + in_[4*3];
1924 in5 += in3;
1925 in3 += in1;
1927 in2= cast(int)MULH(2*in2, C3);
1928 in3= cast(int)MULH(4*in3, C3);
1930 t1 = in0 - in4;
1931 t2 = cast(int)MULH(2*(in1 - in5), icos36h[4]);
1933 out_[ 7]=
1934 out_[10]= t1 + t2;
1935 out_[ 1]=
1936 out_[ 4]= t1 - t2;
1938 in0 += in4>>1;
1939 in4 = in0 + in2;
1940 in5 += 2*in1;
1941 in1 = cast(int)MULH(in5 + in3, icos36h[1]);
1942 out_[ 8]=
1943 out_[ 9]= in4 + in1;
1944 out_[ 2]=
1945 out_[ 3]= in4 - in1;
1947 in0 -= in2;
1948 in5 = cast(int)MULH(2*(in5 - in3), icos36h[7]);
1949 out_[ 0]=
1950 out_[ 5]= in0 - in5;
1951 out_[ 6]=
1952 out_[11]= in0 + in5;
1955 void imdct36(int *out_, int *buf, int *in_, int *win)
1957 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1958 int[18] tmp;
1959 int* tmp1, in1;
1961 for(i=17;i>=1;i--)
1962 in_[i] += in_[i-1];
1963 for(i=17;i>=3;i-=2)
1964 in_[i] += in_[i-2];
1966 for(j=0;j<2;j++) {
1967 tmp1 = tmp.ptr + j;
1968 in1 = in_ + j;
1969 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1971 t3 = in1[2*0] + (in1[2*6]>>1);
1972 t1 = in1[2*0] - in1[2*6];
1973 tmp1[ 6] = t1 - (t2>>1);
1974 tmp1[16] = t1 + t2;
1976 t0 = cast(int)MULH(2*(in1[2*2] + in1[2*4]), C2);
1977 t1 = cast(int)MULH( in1[2*4] - in1[2*8] , -2*C8);
1978 t2 = cast(int)MULH(2*(in1[2*2] + in1[2*8]), -C4);
1980 tmp1[10] = t3 - t0 - t2;
1981 tmp1[ 2] = t3 + t0 + t1;
1982 tmp1[14] = t3 + t2 - t1;
1984 tmp1[ 4] = cast(int)MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1985 t2 = cast(int)MULH(2*(in1[2*1] + in1[2*5]), C1);
1986 t3 = cast(int)MULH( in1[2*5] - in1[2*7] , -2*C7);
1987 t0 = cast(int)MULH(2*in1[2*3], C3);
1989 t1 = cast(int)MULH(2*(in1[2*1] + in1[2*7]), -C5);
1991 tmp1[ 0] = t2 + t3 + t0;
1992 tmp1[12] = t2 + t1 - t0;
1993 tmp1[ 8] = t3 - t1 - t0;
1996 i = 0;
1997 for(j=0;j<4;j++) {
1998 t0 = tmp[i];
1999 t1 = tmp[i + 2];
2000 s0 = t1 + t0;
2001 s2 = t1 - t0;
2003 t2 = tmp[i + 1];
2004 t3 = tmp[i + 3];
2005 s1 = cast(int)MULH(2*(t3 + t2), icos36h[j]);
2006 s3 = cast(int)MULL(t3 - t2, icos36[8 - j]);
2008 t0 = s0 + s1;
2009 t1 = s0 - s1;
2010 out_[(9 + j)*SBLIMIT] = cast(int)MULH(t1, win[9 + j]) + buf[9 + j];
2011 out_[(8 - j)*SBLIMIT] = cast(int)MULH(t1, win[8 - j]) + buf[8 - j];
2012 buf[9 + j] = cast(int)MULH(t0, win[18 + 9 + j]);
2013 buf[8 - j] = cast(int)MULH(t0, win[18 + 8 - j]);
2015 t0 = s2 + s3;
2016 t1 = s2 - s3;
2017 out_[(9 + 8 - j)*SBLIMIT] = cast(int)MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
2018 out_[( j)*SBLIMIT] = cast(int)MULH(t1, win[ j]) + buf[ j];
2019 buf[9 + 8 - j] = cast(int)MULH(t0, win[18 + 9 + 8 - j]);
2020 buf[ + j] = cast(int)MULH(t0, win[18 + j]);
2021 i += 4;
2024 s0 = tmp[16];
2025 s1 = cast(int)MULH(2*tmp[17], icos36h[4]);
2026 t0 = s0 + s1;
2027 t1 = s0 - s1;
2028 out_[(9 + 4)*SBLIMIT] = cast(int)MULH(t1, win[9 + 4]) + buf[9 + 4];
2029 out_[(8 - 4)*SBLIMIT] = cast(int)MULH(t1, win[8 - 4]) + buf[8 - 4];
2030 buf[9 + 4] = cast(int)MULH(t0, win[18 + 9 + 4]);
2031 buf[8 - 4] = cast(int)MULH(t0, win[18 + 8 - 4]);
2034 void compute_imdct (mp3_context_t *s, granule_t *g, int32_t *sb_samples, int32_t *mdct_buf) {
2035 int32_t* ptr, win, win1, buf, out_ptr, ptr1;
2036 int32_t[12] out2;
2037 int i, j, mdct_long_end, v, sblimit;
2039 /* find last non zero block */
2040 ptr = g.sb_hybrid.ptr + 576;
2041 ptr1 = g.sb_hybrid.ptr + 2 * 18;
2042 while (ptr >= ptr1) {
2043 ptr -= 6;
2044 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2045 if (v != 0)
2046 break;
2048 sblimit = cast(int)((ptr - g.sb_hybrid.ptr) / 18) + 1;
2050 if (g.block_type == 2) {
2051 /* XXX: check for 8000 Hz */
2052 if (g.switch_point)
2053 mdct_long_end = 2;
2054 else
2055 mdct_long_end = 0;
2056 } else {
2057 mdct_long_end = sblimit;
2060 buf = mdct_buf;
2061 ptr = g.sb_hybrid.ptr;
2062 for(j=0;j<mdct_long_end;j++) {
2063 /* apply window & overlap with previous buffer */
2064 out_ptr = sb_samples + j;
2065 /* select window */
2066 if (g.switch_point && j < 2)
2067 win1 = mdct_win[0].ptr;
2068 else
2069 win1 = mdct_win[g.block_type].ptr;
2070 /* select frequency inversion */
2071 win = win1 + ((4 * 36) & -(j & 1));
2072 imdct36(out_ptr, buf, ptr, win);
2073 out_ptr += 18*SBLIMIT;
2074 ptr += 18;
2075 buf += 18;
2077 for(j=mdct_long_end;j<sblimit;j++) {
2078 /* select frequency inversion */
2079 win = mdct_win[2].ptr + ((4 * 36) & -(j & 1));
2080 out_ptr = sb_samples + j;
2082 for(i=0; i<6; i++){
2083 *out_ptr = buf[i];
2084 out_ptr += SBLIMIT;
2086 imdct12(out2.ptr, ptr + 0);
2087 for(i=0;i<6;i++) {
2088 *out_ptr = cast(int)MULH(out2[i], win[i]) + buf[i + 6*1];
2089 buf[i + 6*2] = cast(int)MULH(out2[i + 6], win[i + 6]);
2090 out_ptr += SBLIMIT;
2092 imdct12(out2.ptr, ptr + 1);
2093 for(i=0;i<6;i++) {
2094 *out_ptr = cast(int)MULH(out2[i], win[i]) + buf[i + 6*2];
2095 buf[i + 6*0] = cast(int)MULH(out2[i + 6], win[i + 6]);
2096 out_ptr += SBLIMIT;
2098 imdct12(out2.ptr, ptr + 2);
2099 for(i=0;i<6;i++) {
2100 buf[i + 6*0] = cast(int)MULH(out2[i], win[i]) + buf[i + 6*0];
2101 buf[i + 6*1] = cast(int)MULH(out2[i + 6], win[i + 6]);
2102 buf[i + 6*2] = 0;
2104 ptr += 18;
2105 buf += 18;
2107 /* zero bands */
2108 for(j=sblimit;j<SBLIMIT;j++) {
2109 /* overlap */
2110 out_ptr = sb_samples + j;
2111 for(i=0;i<18;i++) {
2112 *out_ptr = buf[i];
2113 buf[i] = 0;
2114 out_ptr += SBLIMIT;
2116 buf += 18;
2120 enum SUM8(string sum, string op, string w, string p) =
2121 "{\n"~
2122 " "~sum~" "~op~" MULS(("~w~")[0 * 64], "~p~"[0 * 64]);\n"~
2123 " "~sum~" "~op~" MULS(("~w~")[1 * 64], "~p~"[1 * 64]);\n"~
2124 " "~sum~" "~op~" MULS(("~w~")[2 * 64], "~p~"[2 * 64]);\n"~
2125 " "~sum~" "~op~" MULS(("~w~")[3 * 64], "~p~"[3 * 64]);\n"~
2126 " "~sum~" "~op~" MULS(("~w~")[4 * 64], "~p~"[4 * 64]);\n"~
2127 " "~sum~" "~op~" MULS(("~w~")[5 * 64], "~p~"[5 * 64]);\n"~
2128 " "~sum~" "~op~" MULS(("~w~")[6 * 64], "~p~"[6 * 64]);\n"~
2129 " "~sum~" "~op~" MULS(("~w~")[7 * 64], "~p~"[7 * 64]);\n"~
2130 "}\n";
2132 enum SUM8P2(string sum1, string op1, string sum2, string op2, string w1, string w2, string p) =
2133 "{\n"~
2134 " int tmp_;\n"~
2135 " tmp_ = "~p~"[0 * 64];\n"~
2136 " "~sum1~" "~op1~" MULS(("~w1~")[0 * 64], tmp_);\n"~
2137 " "~sum2~" "~op2~" MULS(("~w2~")[0 * 64], tmp_);\n"~
2138 " tmp_ = "~p~"[1 * 64];\n"~
2139 " "~sum1~" "~op1~" MULS(("~w1~")[1 * 64], tmp_);\n"~
2140 " "~sum2~" "~op2~" MULS(("~w2~")[1 * 64], tmp_);\n"~
2141 " tmp_ = "~p~"[2 * 64];\n"~
2142 " "~sum1~" "~op1~" MULS(("~w1~")[2 * 64], tmp_);\n"~
2143 " "~sum2~" "~op2~" MULS(("~w2~")[2 * 64], tmp_);\n"~
2144 " tmp_ = "~p~"[3 * 64];\n"~
2145 " "~sum1~" "~op1~" MULS(("~w1~")[3 * 64], tmp_);\n"~
2146 " "~sum2~" "~op2~" MULS(("~w2~")[3 * 64], tmp_);\n"~
2147 " tmp_ = "~p~"[4 * 64];\n"~
2148 " "~sum1~" "~op1~" MULS(("~w1~")[4 * 64], tmp_);\n"~
2149 " "~sum2~" "~op2~" MULS(("~w2~")[4 * 64], tmp_);\n"~
2150 " tmp_ = "~p~"[5 * 64];\n"~
2151 " "~sum1~" "~op1~" MULS(("~w1~")[5 * 64], tmp_);\n"~
2152 " "~sum2~" "~op2~" MULS(("~w2~")[5 * 64], tmp_);\n"~
2153 " tmp_ = "~p~"[6 * 64];\n"~
2154 " "~sum1~" "~op1~" MULS(("~w1~")[6 * 64], tmp_);\n"~
2155 " "~sum2~" "~op2~" MULS(("~w2~")[6 * 64], tmp_);\n"~
2156 " tmp_ = "~p~"[7 * 64];\n"~
2157 " "~sum1~" "~op1~" MULS(("~w1~")[7 * 64], tmp_);\n"~
2158 " "~sum2~" "~op2~" MULS(("~w2~")[7 * 64], tmp_);\n"~
2159 "}\n";
2161 enum COS0_0 = FIXHR!(0.50060299823519630134/2);
2162 enum COS0_1 = FIXHR!(0.50547095989754365998/2);
2163 enum COS0_2 = FIXHR!(0.51544730992262454697/2);
2164 enum COS0_3 = FIXHR!(0.53104259108978417447/2);
2165 enum COS0_4 = FIXHR!(0.55310389603444452782/2);
2166 enum COS0_5 = FIXHR!(0.58293496820613387367/2);
2167 enum COS0_6 = FIXHR!(0.62250412303566481615/2);
2168 enum COS0_7 = FIXHR!(0.67480834145500574602/2);
2169 enum COS0_8 = FIXHR!(0.74453627100229844977/2);
2170 enum COS0_9 = FIXHR!(0.83934964541552703873/2);
2171 enum COS0_10 = FIXHR!(0.97256823786196069369/2);
2172 enum COS0_11 = FIXHR!(1.16943993343288495515/4);
2173 enum COS0_12 = FIXHR!(1.48416461631416627724/4);
2174 enum COS0_13 = FIXHR!(2.05778100995341155085/8);
2175 enum COS0_14 = FIXHR!(3.40760841846871878570/8);
2176 enum COS0_15 = FIXHR!(10.19000812354805681150/32);
2178 enum COS1_0 = FIXHR!(0.50241928618815570551/2);
2179 enum COS1_1 = FIXHR!(0.52249861493968888062/2);
2180 enum COS1_2 = FIXHR!(0.56694403481635770368/2);
2181 enum COS1_3 = FIXHR!(0.64682178335999012954/2);
2182 enum COS1_4 = FIXHR!(0.78815462345125022473/2);
2183 enum COS1_5 = FIXHR!(1.06067768599034747134/4);
2184 enum COS1_6 = FIXHR!(1.72244709823833392782/4);
2185 enum COS1_7 = FIXHR!(5.10114861868916385802/16);
2187 enum COS2_0 = FIXHR!(0.50979557910415916894/2);
2188 enum COS2_1 = FIXHR!(0.60134488693504528054/2);
2189 enum COS2_2 = FIXHR!(0.89997622313641570463/2);
2190 enum COS2_3 = FIXHR!(2.56291544774150617881/8);
2192 enum COS3_0 = FIXHR!(0.54119610014619698439/2);
2193 enum COS3_1 = FIXHR!(1.30656296487637652785/4);
2195 enum COS4_0 = FIXHR!(0.70710678118654752439/2);
2197 enum BF(string a, string b, string c, string s) =
2198 "{\n"~
2199 " tmp0 = tab["~a~"] + tab["~b~"];\n"~
2200 " tmp1 = tab["~a~"] - tab["~b~"];\n"~
2201 " tab["~a~"] = tmp0;\n"~
2202 " tab["~b~"] = cast(int)MULH(tmp1<<("~s~"), "~c~");\n"~
2203 "}\n";
2205 enum BF1(string a, string b, string c, string d) =
2206 "{\n"~
2207 " "~BF!(a, b, "COS4_0", "1")~"\n"~
2208 " "~BF!(c, d,"-COS4_0", "1")~"\n"~
2209 " tab["~c~"] += tab["~d~"];\n"~
2210 "}\n";
2212 enum BF2(string a, string b, string c, string d) =
2213 "{\n"~
2214 " "~BF!(a, b, "COS4_0", "1")~"\n"~
2215 " "~BF!(c, d,"-COS4_0", "1")~"\n"~
2216 " tab["~c~"] += tab["~d~"];\n"~
2217 " tab["~a~"] += tab["~c~"];\n"~
2218 " tab["~c~"] += tab["~b~"];\n"~
2219 " tab["~b~"] += tab["~d~"];\n"~
2220 "}\n";
2222 void dct32(int32_t *out_, int32_t *tab) {
2223 int tmp0, tmp1;
2225 /* pass 1 */
2226 mixin(BF!("0", "31", "COS0_0", "1"));
2227 mixin(BF!("15", "16", "COS0_15", "5"));
2228 /* pass 2 */
2229 mixin(BF!("0", "15", "COS1_0", "1"));
2230 mixin(BF!("16", "31", "-COS1_0", "1"));
2231 /* pass 1 */
2232 mixin(BF!("7", "24", "COS0_7", "1"));
2233 mixin(BF!("8", "23", "COS0_8", "1"));
2234 /* pass 2 */
2235 mixin(BF!("7", "8", "COS1_7", "4"));
2236 mixin(BF!("23", "24", "-COS1_7", "4"));
2237 /* pass 3 */
2238 mixin(BF!("0", "7", "COS2_0", "1"));
2239 mixin(BF!("8", "15", "-COS2_0", "1"));
2240 mixin(BF!("16", "23", "COS2_0", "1"));
2241 mixin(BF!("24", "31", "-COS2_0", "1"));
2242 /* pass 1 */
2243 mixin(BF!("3", "28", "COS0_3", "1"));
2244 mixin(BF!("12", "19", "COS0_12", "2"));
2245 /* pass 2 */
2246 mixin(BF!("3", "12", "COS1_3", "1"));
2247 mixin(BF!("19", "28", "-COS1_3", "1"));
2248 /* pass 1 */
2249 mixin(BF!("4", "27", "COS0_4", "1"));
2250 mixin(BF!("11", "20", "COS0_11", "2"));
2251 /* pass 2 */
2252 mixin(BF!("4", "11", "COS1_4", "1"));
2253 mixin(BF!("20", "27", "-COS1_4", "1"));
2254 /* pass 3 */
2255 mixin(BF!("3", "4", "COS2_3", "3"));
2256 mixin(BF!("11", "12", "-COS2_3", "3"));
2257 mixin(BF!("19", "20", "COS2_3", "3"));
2258 mixin(BF!("27", "28", "-COS2_3", "3"));
2259 /* pass 4 */
2260 mixin(BF!("0", "3", "COS3_0", "1"));
2261 mixin(BF!("4", "7", "-COS3_0", "1"));
2262 mixin(BF!("8", "11", "COS3_0", "1"));
2263 mixin(BF!("12", "15", "-COS3_0", "1"));
2264 mixin(BF!("16", "19", "COS3_0", "1"));
2265 mixin(BF!("20", "23", "-COS3_0", "1"));
2266 mixin(BF!("24", "27", "COS3_0", "1"));
2267 mixin(BF!("28", "31", "-COS3_0", "1"));
2271 /* pass 1 */
2272 mixin(BF!("1", "30", "COS0_1", "1"));
2273 mixin(BF!("14", "17", "COS0_14", "3"));
2274 /* pass 2 */
2275 mixin(BF!("1", "14", "COS1_1", "1"));
2276 mixin(BF!("17", "30", "-COS1_1", "1"));
2277 /* pass 1 */
2278 mixin(BF!("6", "25", "COS0_6", "1"));
2279 mixin(BF!("9", "22", "COS0_9", "1"));
2280 /* pass 2 */
2281 mixin(BF!("6", "9", "COS1_6", "2"));
2282 mixin(BF!("22", "25", "-COS1_6", "2"));
2283 /* pass 3 */
2284 mixin(BF!("1", "6", "COS2_1", "1"));
2285 mixin(BF!("9", "14", "-COS2_1", "1"));
2286 mixin(BF!("17", "22", "COS2_1", "1"));
2287 mixin(BF!("25", "30", "-COS2_1", "1"));
2289 /* pass 1 */
2290 mixin(BF!("2", "29", "COS0_2", "1"));
2291 mixin(BF!("13", "18", "COS0_13", "3"));
2292 /* pass 2 */
2293 mixin(BF!("2", "13", "COS1_2", "1"));
2294 mixin(BF!("18", "29", "-COS1_2", "1"));
2295 /* pass 1 */
2296 mixin(BF!("5", "26", "COS0_5", "1"));
2297 mixin(BF!("10", "21", "COS0_10", "1"));
2298 /* pass 2 */
2299 mixin(BF!("5", "10", "COS1_5", "2"));
2300 mixin(BF!("21", "26", "-COS1_5", "2"));
2301 /* pass 3 */
2302 mixin(BF!("2", "5", "COS2_2", "1"));
2303 mixin(BF!("10", "13", "-COS2_2", "1"));
2304 mixin(BF!("18", "21", "COS2_2", "1"));
2305 mixin(BF!("26", "29", "-COS2_2", "1"));
2306 /* pass 4 */
2307 mixin(BF!("1", "2", "COS3_1", "2"));
2308 mixin(BF!("5", "6", "-COS3_1", "2"));
2309 mixin(BF!("9", "10", "COS3_1", "2"));
2310 mixin(BF!("13", "14", "-COS3_1", "2"));
2311 mixin(BF!("17", "18", "COS3_1", "2"));
2312 mixin(BF!("21", "22", "-COS3_1", "2"));
2313 mixin(BF!("25", "26", "COS3_1", "2"));
2314 mixin(BF!("29", "30", "-COS3_1", "2"));
2316 /* pass 5 */
2317 mixin(BF1!("0", "1", "2", "3"));
2318 mixin(BF2!("4", "5", "6", "7"));
2319 mixin(BF1!("8", "9", "10", "11"));
2320 mixin(BF2!("12", "13", "14", "15"));
2321 mixin(BF1!("16", "17", "18", "19"));
2322 mixin(BF2!("20", "21", "22", "23"));
2323 mixin(BF1!("24", "25", "26", "27"));
2324 mixin(BF2!("28", "29", "30", "31"));
2326 /* pass 6 */
2328 tab[8] += tab[12];
2329 tab[12] += tab[10];
2330 tab[10] += tab[14];
2331 tab[14] += tab[9];
2332 tab[9] += tab[13];
2333 tab[13] += tab[11];
2334 tab[11] += tab[15];
2336 out_[ 0] = tab[0];
2337 out_[16] = tab[1];
2338 out_[ 8] = tab[2];
2339 out_[24] = tab[3];
2340 out_[ 4] = tab[4];
2341 out_[20] = tab[5];
2342 out_[12] = tab[6];
2343 out_[28] = tab[7];
2344 out_[ 2] = tab[8];
2345 out_[18] = tab[9];
2346 out_[10] = tab[10];
2347 out_[26] = tab[11];
2348 out_[ 6] = tab[12];
2349 out_[22] = tab[13];
2350 out_[14] = tab[14];
2351 out_[30] = tab[15];
2353 tab[24] += tab[28];
2354 tab[28] += tab[26];
2355 tab[26] += tab[30];
2356 tab[30] += tab[25];
2357 tab[25] += tab[29];
2358 tab[29] += tab[27];
2359 tab[27] += tab[31];
2361 out_[ 1] = tab[16] + tab[24];
2362 out_[17] = tab[17] + tab[25];
2363 out_[ 9] = tab[18] + tab[26];
2364 out_[25] = tab[19] + tab[27];
2365 out_[ 5] = tab[20] + tab[28];
2366 out_[21] = tab[21] + tab[29];
2367 out_[13] = tab[22] + tab[30];
2368 out_[29] = tab[23] + tab[31];
2369 out_[ 3] = tab[24] + tab[20];
2370 out_[19] = tab[25] + tab[21];
2371 out_[11] = tab[26] + tab[22];
2372 out_[27] = tab[27] + tab[23];
2373 out_[ 7] = tab[28] + tab[18];
2374 out_[23] = tab[29] + tab[19];
2375 out_[15] = tab[30] + tab[17];
2376 out_[31] = tab[31];
2379 void mp3_synth_filter(
2380 int16_t *synth_buf_ptr, int *synth_buf_offset,
2381 int16_t *window, int *dither_state,
2382 int16_t *samples, int incr,
2383 int32_t* sb_samples/*[SBLIMIT]*/
2385 int32_t[32] tmp;
2386 int16_t *synth_buf;
2387 const(int16_t)* w, w2, p;
2388 int j, offset, v;
2389 int16_t *samples2;
2390 int sum, sum2;
2392 dct32(tmp.ptr, sb_samples);
2394 offset = *synth_buf_offset;
2395 synth_buf = synth_buf_ptr + offset;
2397 for(j=0;j<32;j++) {
2398 v = tmp[j];
2399 /* NOTE: can cause a loss in precision if very high amplitude
2400 sound */
2401 if (v > 32767)
2402 v = 32767;
2403 else if (v < -32768)
2404 v = -32768;
2405 synth_buf[j] = cast(short)v;
2407 /* copy to avoid wrap */
2408 libc_memcpy(synth_buf + 512, synth_buf, 32 * int16_t.sizeof);
2410 samples2 = samples + 31 * incr;
2411 w = window;
2412 w2 = window + 31;
2414 sum = *dither_state;
2415 p = synth_buf + 16;
2416 mixin(SUM8!("sum", "+=", "w", "p"));
2417 p = synth_buf + 48;
2418 mixin(SUM8!("sum", "-=", "w + 32", "p"));
2419 *samples = cast(short)round_sample(&sum);
2420 samples += incr;
2421 w++;
2423 /* we calculate two samples at the same time to avoid one memory
2424 access per two sample */
2425 for(j=1;j<16;j++) {
2426 sum2 = 0;
2427 p = synth_buf + 16 + j;
2428 mixin(SUM8P2!("sum", "+=", "sum2", "-=", "w", "w2", "p"));
2429 p = synth_buf + 48 - j;
2430 mixin(SUM8P2!("sum", "-=", "sum2", "-=", "w + 32", "w2 + 32", "p"));
2432 *samples = cast(short)round_sample(&sum);
2433 samples += incr;
2434 sum += sum2;
2435 *samples2 = cast(short)round_sample(&sum);
2436 samples2 -= incr;
2437 w++;
2438 w2--;
2441 p = synth_buf + 32;
2442 mixin(SUM8!("sum", "-=", "w + 32", "p"));
2443 *samples = cast(short)round_sample(&sum);
2444 *dither_state= sum;
2446 offset = (offset - 32) & 511;
2447 *synth_buf_offset = offset;
2451 // ////////////////////////////////////////////////////////////////////////// //
2452 int decode_header(mp3_context_t *s, uint32_t header) {
2453 static immutable short[4][4] sampleCount = [
2454 [0, 576, 1152, 384], // v2.5
2455 [0, 0, 0, 0], // reserved
2456 [0, 576, 1152, 384], // v2
2457 [0, 1152, 1152, 384], // v1
2459 ubyte mpid = (header>>19)&0x03;
2460 ubyte layer = (header>>17)&0x03;
2462 s.sample_count = sampleCount.ptr[mpid].ptr[layer];
2464 int sample_rate, frame_size, mpeg25, padding;
2465 int sample_rate_index, bitrate_index;
2466 if (header & (1<<20)) {
2467 s.lsf = (header & (1<<19)) ? 0 : 1;
2468 mpeg25 = 0;
2469 } else {
2470 s.lsf = 1;
2471 mpeg25 = 1;
2474 sample_rate_index = (header >> 10) & 3;
2475 sample_rate = mp3_freq_tab[sample_rate_index] >> (s.lsf + mpeg25);
2476 sample_rate_index += 3 * (s.lsf + mpeg25);
2477 s.sample_rate_index = sample_rate_index;
2478 s.error_protection = ((header >> 16) & 1) ^ 1;
2479 s.sample_rate = sample_rate;
2481 bitrate_index = (header >> 12) & 0xf;
2482 padding = (header >> 9) & 1;
2483 s.mode = (header >> 6) & 3;
2484 s.mode_ext = (header >> 4) & 3;
2485 s.nb_channels = (s.mode == MP3_MONO) ? 1 : 2;
2487 if (bitrate_index != 0) {
2488 frame_size = mp3_bitrate_tab[s.lsf][bitrate_index];
2489 s.bit_rate = frame_size * 1000;
2490 s.frame_size = (frame_size * 144000) / (sample_rate << s.lsf) + padding;
2491 } else {
2492 /* if no frame size computed, signal it */
2493 return 1;
2495 return 0;
2498 int mp_decode_layer3(mp3_context_t *s) {
2499 int nb_granules, main_data_begin, private_bits;
2500 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
2501 granule_t *g;
2502 static granule_t[2][2] granules;
2503 static int16_t[576] exponents;
2504 const(uint8_t)* ptr;
2506 if (s.lsf) {
2507 main_data_begin = get_bits(&s.gb, 8);
2508 private_bits = get_bits(&s.gb, s.nb_channels);
2509 nb_granules = 1;
2510 } else {
2511 main_data_begin = get_bits(&s.gb, 9);
2512 if (s.nb_channels == 2)
2513 private_bits = get_bits(&s.gb, 3);
2514 else
2515 private_bits = get_bits(&s.gb, 5);
2516 nb_granules = 2;
2517 for(ch=0;ch<s.nb_channels;ch++) {
2518 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2519 granules[ch][1].scfsi = cast(ubyte)get_bits(&s.gb, 4);
2523 for(gr=0;gr<nb_granules;gr++) {
2524 for(ch=0;ch<s.nb_channels;ch++) {
2525 g = &granules[ch][gr];
2526 g.part2_3_length = get_bits(&s.gb, 12);
2527 g.big_values = get_bits(&s.gb, 9);
2528 g.global_gain = get_bits(&s.gb, 8);
2529 /* if MS stereo only is selected, we precompute the
2530 1/sqrt(2) renormalization factor */
2531 if ((s.mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2532 MODE_EXT_MS_STEREO)
2533 g.global_gain -= 2;
2534 if (s.lsf)
2535 g.scalefac_compress = get_bits(&s.gb, 9);
2536 else
2537 g.scalefac_compress = get_bits(&s.gb, 4);
2538 blocksplit_flag = get_bits(&s.gb, 1);
2539 if (blocksplit_flag) {
2540 g.block_type = cast(ubyte)get_bits(&s.gb, 2);
2541 if (g.block_type == 0)
2542 return -1;
2543 g.switch_point = cast(ubyte)get_bits(&s.gb, 1);
2544 for(i=0;i<2;i++)
2545 g.table_select[i] = get_bits(&s.gb, 5);
2546 for(i=0;i<3;i++)
2547 g.subblock_gain[i] = get_bits(&s.gb, 3);
2548 /* compute huffman coded region sizes */
2549 if (g.block_type == 2)
2550 g.region_size[0] = (36 / 2);
2551 else {
2552 if (s.sample_rate_index <= 2)
2553 g.region_size[0] = (36 / 2);
2554 else if (s.sample_rate_index != 8)
2555 g.region_size[0] = (54 / 2);
2556 else
2557 g.region_size[0] = (108 / 2);
2559 g.region_size[1] = (576 / 2);
2560 } else {
2561 int region_address1, region_address2, l;
2562 g.block_type = 0;
2563 g.switch_point = 0;
2564 for(i=0;i<3;i++)
2565 g.table_select[i] = get_bits(&s.gb, 5);
2566 /* compute huffman coded region sizes */
2567 region_address1 = get_bits(&s.gb, 4);
2568 region_address2 = get_bits(&s.gb, 3);
2569 g.region_size[0] =
2570 band_index_long[s.sample_rate_index][region_address1 + 1] >> 1;
2571 l = region_address1 + region_address2 + 2;
2572 /* should not overflow */
2573 if (l > 22)
2574 l = 22;
2575 g.region_size[1] =
2576 band_index_long[s.sample_rate_index][l] >> 1;
2578 /* convert region offsets to region sizes and truncate
2579 size to big_values */
2580 g.region_size[2] = (576 / 2);
2581 j = 0;
2582 for(i=0;i<3;i++) {
2583 k = g.region_size[i];
2584 if (g.big_values < k) k = g.big_values;
2585 g.region_size[i] = k - j;
2586 j = k;
2589 /* compute band indexes */
2590 if (g.block_type == 2) {
2591 if (g.switch_point) {
2592 /* if switched mode, we handle the 36 first samples as
2593 long blocks. For 8000Hz, we handle the 48 first
2594 exponents as long blocks (XXX: check this!) */
2595 if (s.sample_rate_index <= 2)
2596 g.long_end = 8;
2597 else if (s.sample_rate_index != 8)
2598 g.long_end = 6;
2599 else
2600 g.long_end = 4; /* 8000 Hz */
2602 g.short_start = 2 + (s.sample_rate_index != 8);
2603 } else {
2604 g.long_end = 0;
2605 g.short_start = 0;
2607 } else {
2608 g.short_start = 13;
2609 g.long_end = 22;
2612 g.preflag = 0;
2613 if (!s.lsf)
2614 g.preflag = get_bits(&s.gb, 1);
2615 g.scalefac_scale = cast(ubyte)get_bits(&s.gb, 1);
2616 g.count1table_select = cast(ubyte)get_bits(&s.gb, 1);
2620 ptr = s.gb.buffer + (get_bits_count(&s.gb)>>3);
2621 /* now we get bits from the main_data_begin offset */
2622 if(main_data_begin > s.last_buf_size){
2623 s.last_buf_size= main_data_begin;
2626 libc_memcpy(s.last_buf.ptr + s.last_buf_size, ptr, EXTRABYTES);
2627 s.in_gb= s.gb;
2628 init_get_bits(&s.gb, s.last_buf.ptr + s.last_buf_size - main_data_begin, main_data_begin*8);
2630 for(gr=0;gr<nb_granules;gr++) {
2631 for(ch=0;ch<s.nb_channels;ch++) {
2632 g = &granules[ch][gr];
2634 bits_pos = get_bits_count(&s.gb);
2636 if (!s.lsf) {
2637 uint8_t *sc;
2638 int slen, slen1, slen2;
2640 /* MPEG1 scale factors */
2641 slen1 = slen_table[0][g.scalefac_compress];
2642 slen2 = slen_table[1][g.scalefac_compress];
2643 if (g.block_type == 2) {
2644 n = g.switch_point ? 17 : 18;
2645 j = 0;
2646 if(slen1){
2647 for(i=0;i<n;i++)
2648 g.scale_factors[j++] = cast(ubyte)get_bits(&s.gb, slen1);
2649 }else{
2650 libc_memset(cast(void*) &g.scale_factors[j], 0, n);
2651 j += n;
2652 // for(i=0;i<n;i++)
2653 // g.scale_factors[j++] = 0;
2655 if(slen2){
2656 for(i=0;i<18;i++)
2657 g.scale_factors[j++] = cast(ubyte)get_bits(&s.gb, slen2);
2658 for(i=0;i<3;i++)
2659 g.scale_factors[j++] = 0;
2660 }else{
2661 for(i=0;i<21;i++)
2662 g.scale_factors[j++] = 0;
2664 } else {
2665 sc = granules[ch][0].scale_factors.ptr;
2666 j = 0;
2667 for(k=0;k<4;k++) {
2668 n = (k == 0 ? 6 : 5);
2669 if ((g.scfsi & (0x8 >> k)) == 0) {
2670 slen = (k < 2) ? slen1 : slen2;
2671 if(slen){
2672 for(i=0;i<n;i++)
2673 g.scale_factors[j++] = cast(ubyte)get_bits(&s.gb, slen);
2674 }else{
2675 libc_memset(cast(void*) &g.scale_factors[j], 0, n);
2676 j += n;
2677 // for(i=0;i<n;i++)
2678 // g.scale_factors[j++] = 0;
2680 } else {
2681 /* simply copy from last granule */
2682 for(i=0;i<n;i++) {
2683 g.scale_factors[j] = sc[j];
2684 j++;
2688 g.scale_factors[j++] = 0;
2690 } else {
2691 int tindex, tindex2, sl, sf;
2692 int[4] slen;
2694 /* LSF scale factors */
2695 if (g.block_type == 2) {
2696 tindex = g.switch_point ? 2 : 1;
2697 } else {
2698 tindex = 0;
2700 sf = g.scalefac_compress;
2701 if ((s.mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2702 /* intensity stereo case */
2703 sf >>= 1;
2704 if (sf < 180) {
2705 lsf_sf_expand(slen.ptr, sf, 6, 6, 0);
2706 tindex2 = 3;
2707 } else if (sf < 244) {
2708 lsf_sf_expand(slen.ptr, sf - 180, 4, 4, 0);
2709 tindex2 = 4;
2710 } else {
2711 lsf_sf_expand(slen.ptr, sf - 244, 3, 0, 0);
2712 tindex2 = 5;
2714 } else {
2715 /* normal case */
2716 if (sf < 400) {
2717 lsf_sf_expand(slen.ptr, sf, 5, 4, 4);
2718 tindex2 = 0;
2719 } else if (sf < 500) {
2720 lsf_sf_expand(slen.ptr, sf - 400, 5, 4, 0);
2721 tindex2 = 1;
2722 } else {
2723 lsf_sf_expand(slen.ptr, sf - 500, 3, 0, 0);
2724 tindex2 = 2;
2725 g.preflag = 1;
2729 j = 0;
2730 for(k=0;k<4;k++) {
2731 n = lsf_nsf_table[tindex2][tindex][k];
2732 sl = slen[k];
2733 if(sl){
2734 for(i=0;i<n;i++)
2735 g.scale_factors[j++] = cast(ubyte)get_bits(&s.gb, sl);
2736 }else{
2737 libc_memset(cast(void*) &g.scale_factors[j], 0, n);
2738 j += n;
2739 // for(i=0;i<n;i++)
2740 // g.scale_factors[j++] = 0;
2743 /* XXX: should compute exact size */
2744 libc_memset(cast(void*) &g.scale_factors[j], 0, 40 - j);
2745 // for(;j<40;j++)
2746 // g.scale_factors[j] = 0;
2749 exponents_from_scale_factors(s, g, exponents.ptr);
2751 /* read Huffman coded residue */
2752 if (huffman_decode(s, g, exponents.ptr,
2753 bits_pos + g.part2_3_length) < 0)
2754 return -1;
2755 } /* ch */
2757 if (s.nb_channels == 2)
2758 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2760 for(ch=0;ch<s.nb_channels;ch++) {
2761 g = &granules[ch][gr];
2762 reorder_block(s, g);
2763 compute_antialias(s, g);
2764 compute_imdct(s, g, &s.sb_samples[ch][18 * gr][0], s.mdct_buf[ch].ptr);
2766 } /* gr */
2767 return nb_granules * 18;
2770 int mp3_decode_main(
2771 mp3_context_t *s,
2772 int16_t *samples, const uint8_t *buf, int buf_size
2774 int i, nb_frames, ch;
2775 int16_t *samples_ptr;
2777 init_get_bits(&s.gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2779 if (s.error_protection)
2780 get_bits(&s.gb, 16);
2782 nb_frames = mp_decode_layer3(s);
2784 s.last_buf_size=0;
2785 if(s.in_gb.buffer){
2786 align_get_bits(&s.gb);
2787 i= (s.gb.size_in_bits - get_bits_count(&s.gb))>>3;
2788 if(i >= 0 && i <= BACKSTEP_SIZE){
2789 libc_memmove(s.last_buf.ptr, s.gb.buffer + (get_bits_count(&s.gb)>>3), i);
2790 s.last_buf_size=i;
2792 s.gb= s.in_gb;
2795 align_get_bits(&s.gb);
2796 i= (s.gb.size_in_bits - get_bits_count(&s.gb))>>3;
2798 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2799 i = buf_size - HEADER_SIZE;
2800 if (BACKSTEP_SIZE < i) i = BACKSTEP_SIZE;
2802 libc_memcpy(s.last_buf.ptr + s.last_buf_size, s.gb.buffer + buf_size - HEADER_SIZE - i, i);
2803 s.last_buf_size += i;
2805 /* apply the synthesis filter */
2806 for(ch=0;ch<s.nb_channels;ch++) {
2807 samples_ptr = samples + ch;
2808 for(i=0;i<nb_frames;i++) {
2809 mp3_synth_filter(
2810 s.synth_buf[ch].ptr, &(s.synth_buf_offset[ch]),
2811 window.ptr, &s.dither_state,
2812 samples_ptr, s.nb_channels,
2813 s.sb_samples[ch][i].ptr
2815 samples_ptr += 32 * s.nb_channels;
2818 return nb_frames * 32 * cast(int)uint16_t.sizeof * s.nb_channels;
2822 // ////////////////////////////////////////////////////////////////////////// //
2823 shared static this () {
2824 auto res = mp3_decode_init();
2825 if (res < 0) assert(0, "mp3 initialization failed");
2828 int mp3_decode_init () {
2829 int i, j, k;
2831 if (true) {
2832 /* synth init */
2833 for(i=0;i<257;i++) {
2834 int v;
2835 v = mp3_enwindow[i];
2836 static if (WFRAC_BITS < 16) {
2837 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
2839 window[i] = cast(short)v;
2840 if ((i & 63) != 0)
2841 v = -v;
2842 if (i != 0)
2843 window[512 - i] = cast(short)v;
2846 /* huffman decode tables */
2847 for(i=1;i<16;i++) {
2848 const huff_table_t *h = &mp3_huff_tables[i];
2849 int xsize, x, y;
2850 uint n;
2851 uint8_t[512] tmp_bits;
2852 uint16_t[512] tmp_codes;
2854 libc_memset(tmp_bits.ptr, 0, tmp_bits.sizeof);
2855 libc_memset(tmp_codes.ptr, 0, tmp_codes.sizeof);
2857 xsize = h.xsize;
2858 n = xsize * xsize;
2860 j = 0;
2861 for(x=0;x<xsize;x++) {
2862 for(y=0;y<xsize;y++){
2863 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h.bits [j ];
2864 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h.codes[j++];
2868 init_vlc(&huff_vlc[i], 7, 512,
2869 tmp_bits.ptr, 1, 1, tmp_codes.ptr, 2, 2);
2871 for(i=0;i<2;i++) {
2872 init_vlc(&huff_quad_vlc[i], (i == 0 ? 7 : 4), 16,
2873 mp3_quad_bits[i].ptr, 1, 1, mp3_quad_codes[i].ptr, 1, 1);
2876 for(i=0;i<9;i++) {
2877 k = 0;
2878 for(j=0;j<22;j++) {
2879 band_index_long[i][j] = cast(ushort)k;
2880 k += band_size_long[i][j];
2882 band_index_long[i][22] = cast(ushort)k;
2885 /* compute n ^ (4/3) and store it in mantissa/exp format */
2886 table_4_3_exp= cast(byte*)libc_malloc(TABLE_4_3_SIZE * (table_4_3_exp[0]).sizeof);
2887 if(!table_4_3_exp)
2888 return -1;
2889 table_4_3_value= cast(uint*)libc_malloc(TABLE_4_3_SIZE * (table_4_3_value[0]).sizeof);
2890 if(!table_4_3_value)
2891 return -1;
2893 for(i=1;i<TABLE_4_3_SIZE;i++) {
2894 double f, fm;
2895 int e, m;
2896 f = libc_pow(cast(double)(i/4), 4.0 / 3.0) * libc_pow(2, (i&3)*0.25);
2897 fm = libc_frexp(f, e);
2898 m = cast(uint32_t)(fm*(1L<<31) + 0.5);
2899 e+= FRAC_BITS - 31 + 5 - 100;
2900 table_4_3_value[i] = m;
2901 table_4_3_exp[i] = cast(byte)(-e);
2903 for(i=0; i<512*16; i++){
2904 int exponent= (i>>4);
2905 double f= libc_pow(i&15, 4.0 / 3.0) * libc_pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
2906 expval_table[exponent][i&15]= cast(uint)f;
2907 if((i&15)==1)
2908 exp_table[exponent]= cast(uint)f;
2911 for(i=0;i<7;i++) {
2912 float f;
2913 int v;
2914 if (i != 6) {
2915 f = tan(cast(double)i * M_PI / 12.0);
2916 v = FIXRx(f / (1.0 + f));
2917 } else {
2918 v = FIXR!(1.0);
2920 is_table[0][i] = v;
2921 is_table[1][6 - i] = v;
2923 for(i=7;i<16;i++)
2924 is_table[0][i] = is_table[1][i] = cast(int)0.0;
2926 for(i=0;i<16;i++) {
2927 double f;
2928 int e, k_;
2930 for(j=0;j<2;j++) {
2931 e = -(j + 1) * ((i + 1) >> 1);
2932 f = libc_pow(2.0, e / 4.0);
2933 k_ = i & 1;
2934 is_table_lsf[j][k_ ^ 1][i] = FIXRx(f);
2935 is_table_lsf[j][k_][i] = FIXR!(1.0);
2939 for(i=0;i<8;i++) {
2940 float ci, cs, ca;
2941 ci = ci_table[i];
2942 cs = 1.0 / sqrt(1.0 + ci * ci);
2943 ca = cs * ci;
2944 csa_table[i][0] = FIXHRx(cs/4);
2945 csa_table[i][1] = FIXHRx(ca/4);
2946 csa_table[i][2] = FIXHRx(ca/4) + FIXHRx(cs/4);
2947 csa_table[i][3] = FIXHRx(ca/4) - FIXHRx(cs/4);
2948 csa_table_float[i][0] = cs;
2949 csa_table_float[i][1] = ca;
2950 csa_table_float[i][2] = ca + cs;
2951 csa_table_float[i][3] = ca - cs;
2954 /* compute mdct windows */
2955 for(i=0;i<36;i++) {
2956 for(j=0; j<4; j++){
2957 double d;
2959 if(j==2 && i%3 != 1)
2960 continue;
2962 d= sin(M_PI * (i + 0.5) / 36.0);
2963 if(j==1){
2964 if (i>=30) d= 0;
2965 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
2966 else if(i>=18) d= 1;
2967 }else if(j==3){
2968 if (i< 6) d= 0;
2969 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
2970 else if(i< 18) d= 1;
2972 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
2973 if(j==2)
2974 mdct_win[j][i/3] = FIXHRx((d / (1<<5)));
2975 else
2976 mdct_win[j][i ] = FIXHRx((d / (1<<5)));
2979 for(j=0;j<4;j++) {
2980 for(i=0;i<36;i+=2) {
2981 mdct_win[j + 4][i] = mdct_win[j][i];
2982 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
2985 //init = 1;
2987 return 0;
2990 int mp3_decode_frame (mp3_context_t *s, int16_t *out_samples, int *data_size, uint8_t *buf, int buf_size) {
2991 uint32_t header;
2992 int out_size;
2993 int extra_bytes = 0;
2995 retry:
2996 if (buf_size < HEADER_SIZE) return -1;
2998 header = (buf[0]<<24)|(buf[1]<<16)|(buf[2]<<8)|buf[3];
2999 if (mp3_check_header(header) < 0){
3000 ++buf;
3001 --buf_size;
3002 ++extra_bytes;
3003 goto retry;
3006 if (s.last_header && (header&0xffff0c00u) != s.last_header) {
3007 ++buf;
3008 --buf_size;
3009 ++extra_bytes;
3010 goto retry;
3013 if (decode_header(s, header) == 1) {
3014 s.frame_size = -1;
3015 return -1;
3018 if (s.frame_size<=0 || s.frame_size > buf_size) return -1; // incomplete frame
3019 if (s.frame_size < buf_size) buf_size = s.frame_size;
3021 out_size = mp3_decode_main(s, out_samples, buf, buf_size);
3022 if (out_size >= 0) {
3023 *data_size = out_size;
3024 s.last_header = header&0xffff0c00u;
3026 // else: Error while decoding MPEG audio frame.
3027 s.frame_size += extra_bytes;
3028 return buf_size;
3033 int mp3_skip_frame (mp3_context_t *s, uint8_t *buf, int buf_size) {
3034 uint32_t header;
3035 int out_size;
3036 int extra_bytes = 0;
3038 retry:
3039 if (buf_size < HEADER_SIZE) return -1;
3041 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
3042 if (mp3_check_header(header) < 0) {
3043 ++buf;
3044 --buf_size;
3045 ++extra_bytes;
3046 goto retry;
3049 if (s.last_header && (header&0xffff0c00u) != s.last_header) {
3050 ++buf;
3051 --buf_size;
3052 ++extra_bytes;
3053 goto retry;
3056 if (decode_header(s, header) == 1) {
3057 s.frame_size = -1;
3058 return -1;
3061 if (s.frame_size <= 0 || s.frame_size > buf_size) return -1; // incomplete frame
3062 if (s.frame_size < buf_size) buf_size = s.frame_size;
3063 s.last_header = header&0xffff0c00u;
3064 s.frame_size += extra_bytes;
3065 return buf_size;