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