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