RSeries astromech firmware
mp3_decoder.h
Go to the documentation of this file.
1 // based om helix mp3 decoder
2 #pragma once
3 #pragma GCC optimize ("O3")
4 
5 #include "Arduino.h"
6 #include "assert.h"
7 
8 static const uint8_t m_HUFF_PAIRTABS =32;
9 static const uint8_t m_BLOCK_SIZE =18;
10 static const uint8_t m_NBANDS =32;
11 static const uint8_t m_MAX_REORDER_SAMPS =(192-126)*3; // largest critical band for short blocks (see sfBandTable)
12 static const uint16_t m_VBUF_LENGTH =17*2* m_NBANDS; // for double-sized vbuf FIFO
13 static const uint8_t m_MAX_SCFBD =4; // max scalefactor bands per channel
14 static const uint16_t m_MAINBUF_SIZE =1940;
15 static const uint8_t m_MAX_NGRAN =2; // max granules
16 static const uint8_t m_MAX_NCHAN =2; // max channels
17 static const uint16_t m_MAX_NSAMP =576; // max samples per channel, per granule
18 
19 enum {
33 
34  ERR_UNKNOWN = -9999
35 };
36 
37 typedef struct MP3FrameInfo {
38  int bitrate;
39  int nChans;
40  int samprate;
43  int layer;
44  int version;
46 
47 typedef struct SFBandTable {
48  int/*short*/ l[23];
49  int/*short*/ s[14];
51 
52 typedef struct BitStreamInfo {
53  unsigned char *bytePtr;
54  unsigned int iCache;
56  int nBytes;
58 
59 typedef enum { /* map these to the corresponding 2-bit values in the frame header */
60  Stereo = 0x00, /* two independent channels, but L and R frames might have different # of bits */
61  Joint = 0x01, /* coupled channels - layer III: mix of M-S and intensity, Layers I/II: intensity and direct coding only */
62  Dual = 0x02, /* two independent channels, L and R always have exactly 1/2 the total bitrate */
63  Mono = 0x03 /* one channel */
64 } StereoMode_t;
65 
66 typedef enum { /* map to 0,1,2 to make table indexing easier */
67  MPEG1 = 0,
68  MPEG2 = 1,
69  MPEG25 = 2
71 
72 typedef struct FrameHeader {
73  int layer; /* layer index (1, 2, or 3) */
74  int crc; /* CRC flag: 0 = disabled, 1 = enabled */
75  int brIdx; /* bitrate index (0 - 15) */
76  int srIdx; /* sample rate index (0 - 2) */
77  int paddingBit; /* padding flag: 0 = no padding, 1 = single pad byte */
78  int privateBit; /* unused */
79  int modeExt; /* used to decipher joint stereo mode */
80  int copyFlag; /* copyright flag: 0 = no, 1 = yes */
81  int origFlag; /* original flag: 0 = copy, 1 = original */
82  int emphasis; /* deemphasis mode */
83  int CRCWord; /* CRC word (16 bits, 0 if crc not enabled) */
85 
86 typedef struct SideInfoSub {
87  int part23Length; /* number of bits in main data */
88  int nBigvals; /* 2x this = first set of Huffman cw's (maximum amplitude can be > 1) */
89  int globalGain; /* overall gain for dequantizer */
90  int sfCompress; /* unpacked to figure out number of bits in scale factors */
91  int winSwitchFlag; /* window switching flag */
92  int blockType; /* block type */
93  int mixedBlock; /* 0 = regular block (all short or long), 1 = mixed block */
94  int tableSelect[3]; /* index of Huffman tables for the big values regions */
95  int subBlockGain[3]; /* subblock gain offset, relative to global gain */
96  int region0Count; /* 1+region0Count = num scale factor bands in first region of bigvals */
97  int region1Count; /* 1+region1Count = num scale factor bands in second region of bigvals */
98  int preFlag; /* for optional high frequency boost */
99  int sfactScale; /* scaling of the scalefactors */
100  int count1TableSelect; /* index of Huffman table for quad codewords */
101 } SideInfoSub_t;
102 
103 typedef struct SideInfo {
106  int scfsi[m_MAX_NCHAN][m_MAX_SCFBD]; /* 4 scalefactor bands per channel */
107 } SideInfo_t;
108 
109 typedef struct {
110  int cbType; /* pure long = 0, pure short = 1, mixed = 2 */
111  int cbEndS[3]; /* number nonzero short cb's, per subbblock */
112  int cbEndSMax; /* max of cbEndS[] */
113  int cbEndL; /* number nonzero long cb's */
115 
116 typedef struct DequantInfo {
117  int workBuf[m_MAX_REORDER_SAMPS]; /* workbuf for reordering short blocks */
118 } DequantInfo_t;
119 
120 typedef struct HuffmanInfo {
121  int huffDecBuf[m_MAX_NCHAN][m_MAX_NSAMP]; /* used both for decoded Huffman values and dequantized coefficients */
122  int nonZeroBound[m_MAX_NCHAN]; /* number of coeffs in huffDecBuf[ch] which can be > 0 */
123  int gb[m_MAX_NCHAN]; /* minimum number of guard bits in huffDecBuf[ch] */
124 } HuffmanInfo_t;
125 
126 typedef enum HuffTabType {
134 } HuffTabType_t;
135 
136 typedef struct HuffTabLookup {
137  int linBits;
138  int tabType; /*HuffTabType*/
140 
141 typedef struct IMDCTInfo {
142  int outBuf[m_MAX_NCHAN][m_BLOCK_SIZE][m_NBANDS]; /* output of IMDCT */
143  int overBuf[m_MAX_NCHAN][m_MAX_NSAMP / 2]; /* overlap-add buffer (by symmetry, only need 1/2 size) */
144  int numPrevIMDCT[m_MAX_NCHAN]; /* how many IMDCT's calculated in this channel on prev. granule */
145  int prevType[m_MAX_NCHAN];
146  int prevWinSwitch[m_MAX_NCHAN];
147  int gb[m_MAX_NCHAN];
148 } IMDCTInfo_t;
149 
150 typedef struct BlockCount {
154  int prevType;
157  int gbIn;
158  int gbOut;
159 } BlockCount_t;
160 
161 typedef struct ScaleFactorInfoSub { /* max bits in scalefactors = 5, so use char's to save space */
162  char l[23]; /* [band] */
163  char s[13][3]; /* [band][window] */
165 
166 typedef struct ScaleFactorJS { /* used in MPEG 2, 2.5 intensity (joint) stereo only */
168  int slen[4];
169  int nr[4];
171 
172 /* NOTE - could get by with smaller vbuf if memory is more important than speed
173  * (in Subband, instead of replicating each block in FDCT32 you would do a memmove on the
174  * last 15 blocks to shift them down one, a hardware style FIFO)
175  */
176 typedef struct SubbandInfo {
177  int vbuf[m_MAX_NCHAN * m_VBUF_LENGTH]; /* vbuf for fast DCT-based synthesis PQMF - double size for speed (no modulo indexing) */
178  int vindex; /* internal index for tracking position in vbuf */
179 } SubbandInfo_t;
180 
181 typedef struct MP3DecInfo {
182  /* buffer which must be large enough to hold largest possible main_data section */
183  unsigned char mainBuf[m_MAINBUF_SIZE];
184  /* special info for "free" bitrate files */
187  /* user-accessible info */
188  int bitrate;
189  int nChans;
190  int samprate;
191  int nGrans; /* granules per frame */
192  int nGranSamps; /* samples per granule */
193  int nSlots;
194  int layer;
195 
198  int part23Length[m_MAX_NGRAN][m_MAX_NCHAN];
199 } MP3DecInfo_t;
200 
201 
202 
203 
204 /* format = Q31
205  * #define M_PI 3.14159265358979323846
206  * double u = 2.0 * M_PI / 9.0;
207  * float c0 = sqrt(3.0) / 2.0;
208  * float c1 = cos(u);
209  * float c2 = cos(2*u);
210  * float c3 = sin(u);
211  * float c4 = sin(2*u);
212  */
213 
214 const int c9_0 = 0x6ed9eba1;
215 const int c9_1 = 0x620dbe8b;
216 const int c9_2 = 0x163a1a7e;
217 const int c9_3 = 0x5246dd49;
218 const int c9_4 = 0x7e0e2e32;
219 
220 
221 
222 const int c3_0 = 0x6ed9eba1; /* format = Q31, cos(pi/6) */
223 const int c6[3] = { 0x7ba3751d, 0x5a82799a, 0x2120fb83 }; /* format = Q31, cos(((0:2) + 0.5) * (pi/6)) */
224 
225 /* format = Q31
226  * cos(((0:8) + 0.5) * (pi/18))
227  */
228 const uint32_t c18[9] = { 0x7f834ed0, 0x7ba3751d, 0x7401e4c1, 0x68d9f964, 0x5a82799a, 0x496af3e2, 0x36185aee, 0x2120fb83, 0x0b27eb5c};
229 
230 /* scale factor lengths (num bits) */
231 const char m_SFLenTab[16][2] = { {0, 0}, {0, 1}, {0, 2}, {0, 3}, {3, 0}, {1, 1}, {1, 2}, {1, 3},
232  {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}, {4, 2}, {4, 3}};
233 
234 /* NRTab[size + 3*is_right][block type][partition]
235  * block type index: 0 = (bt0,bt1,bt3), 1 = bt2 non-mixed, 2 = bt2 mixed
236  * partition: scale factor groups (sfb1 through sfb4)
237  * for block type = 2 (mixed or non-mixed) / by 3 is rolled into this table
238  * (for 3 short blocks per long block)
239  * see 2.4.3.2 in MPEG 2 (low sample rate) spec
240  * stuff rolled into this table:
241  * NRTab[x][1][y] --> (NRTab[x][1][y]) / 3
242  * NRTab[x][2][>=1] --> (NRTab[x][2][>=1]) / 3 (first partition is long block)
243  */
244 const char NRTab[6][3][4] = {
245  {{ 6, 5, 5, 5}, {3, 3, 3, 3}, {6, 3, 3, 3}},
246  {{ 6, 5, 7, 3}, {3, 3, 4, 2}, {6, 3, 4, 2}},
247  {{11, 10, 0, 0}, {6, 6, 0, 0}, {6, 3, 6, 0}},
248  {{ 7, 7, 7, 0}, {4, 4, 4, 0}, {6, 5, 4, 0}},
249  {{ 6, 6, 6, 3}, {4, 3, 3, 2}, {6, 4, 3, 2}},
250  {{ 8, 8, 5, 0}, {5, 4, 3, 0}, {6, 6, 3, 0}}
251 };
252 
253 
254 
255 /* optional pre-emphasis for high-frequency scale factor bands */
256 const char preTab[22] = { 0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0 };
257 
258 /* pow(2,-i/4) for i=0..3, Q31 format */
259 const int pow14[4] PROGMEM = {
260  0x7fffffff, 0x6ba27e65, 0x5a82799a, 0x4c1bf829
261 };
262 
263 
264 /*
265  * Minimax polynomial approximation to pow(x, 4/3), over the range
266  * poly43lo: x = [0.5, 0.7071]
267  * poly43hi: x = [0.7071, 1.0]
268  *
269  * Relative error < 1E-7
270  * Coefs are scaled by 4, 2, 1, 0.5, 0.25
271  */
272 const unsigned int poly43lo[5] PROGMEM = { 0x29a0bda9, 0xb02e4828, 0x5957aa1b, 0x236c498d, 0xff581859 };
273 const unsigned int poly43hi[5] PROGMEM = { 0x10852163, 0xd333f6a4, 0x46e9408b, 0x27c2cef0, 0xfef577b4 };
274 
275 /* pow(2, i*4/3) as exp and frac */
276 const int pow2exp[8] PROGMEM = { 14, 13, 11, 10, 9, 7, 6, 5 };
277 
278 const int pow2frac[8] PROGMEM = {
279  0x6597fa94, 0x50a28be6, 0x7fffffff, 0x6597fa94,
280  0x50a28be6, 0x7fffffff, 0x6597fa94, 0x50a28be6
281 };
282 
283 const uint16_t m_HUFF_OFFSET_01= 0;
284 const uint16_t m_HUFF_OFFSET_02= 9 + m_HUFF_OFFSET_01;
285 const uint16_t m_HUFF_OFFSET_03= 65 + m_HUFF_OFFSET_02;
286 const uint16_t m_HUFF_OFFSET_05= 65 + m_HUFF_OFFSET_03;
287 const uint16_t m_HUFF_OFFSET_06=257 + m_HUFF_OFFSET_05;
288 const uint16_t m_HUFF_OFFSET_07=129 + m_HUFF_OFFSET_06;
289 const uint16_t m_HUFF_OFFSET_08=110 + m_HUFF_OFFSET_07;
290 const uint16_t m_HUFF_OFFSET_09=280 + m_HUFF_OFFSET_08;
291 const uint16_t m_HUFF_OFFSET_10= 93 + m_HUFF_OFFSET_09;
292 const uint16_t m_HUFF_OFFSET_11=320 + m_HUFF_OFFSET_10;
293 const uint16_t m_HUFF_OFFSET_12=296 + m_HUFF_OFFSET_11;
294 const uint16_t m_HUFF_OFFSET_13=185 + m_HUFF_OFFSET_12;
295 const uint16_t m_HUFF_OFFSET_15=497 + m_HUFF_OFFSET_13;
296 const uint16_t m_HUFF_OFFSET_16=580 + m_HUFF_OFFSET_15;
297 const uint16_t m_HUFF_OFFSET_24=651 + m_HUFF_OFFSET_16;
298 
299 const int huffTabOffset[m_HUFF_PAIRTABS] PROGMEM = {
308 
309 const HuffTabLookup_t huffTabLookup[m_HUFF_PAIRTABS] PROGMEM = {
310  { 0, noBits },
311  { 0, oneShot },
312  { 0, oneShot },
313  { 0, oneShot },
314  { 0, invalidTab },
315  { 0, oneShot },
316  { 0, oneShot },
317  { 0, loopNoLinbits },
318  { 0, loopNoLinbits },
319  { 0, loopNoLinbits },
320  { 0, loopNoLinbits },
321  { 0, loopNoLinbits },
322  { 0, loopNoLinbits },
323  { 0, loopNoLinbits },
324  { 0, invalidTab },
325  { 0, loopNoLinbits },
326  { 1, loopLinbits },
327  { 2, loopLinbits },
328  { 3, loopLinbits },
329  { 4, loopLinbits },
330  { 6, loopLinbits },
331  { 8, loopLinbits },
332  { 10, loopLinbits },
333  { 13, loopLinbits },
334  { 4, loopLinbits },
335  { 5, loopLinbits },
336  { 6, loopLinbits },
337  { 7, loopLinbits },
338  { 8, loopLinbits },
339  { 9, loopLinbits },
340  { 11, loopLinbits },
341  { 13, loopLinbits },
342 };
343 
344 
345 const int quadTabOffset[2] PROGMEM = {0, 64};
346 const int quadTabMaxBits[2] PROGMEM = {6, 4};
347 
348 /* indexing = [version][samplerate index]
349  * sample rate of frame (Hz)
350  */
351 const int samplerateTab[3][3] PROGMEM = {
352  { 44100, 48000, 32000 }, /* MPEG-1 */
353  { 22050, 24000, 16000 }, /* MPEG-2 */
354  { 11025, 12000, 8000 }, /* MPEG-2.5 */
355 };
356 
357 
358 
359 /* indexing = [version][layer]
360  * number of samples in one frame (per channel)
361  */
362 const int/*short*/samplesPerFrameTab[3][3] PROGMEM = { { 384, 1152, 1152 }, /* MPEG1 */
363 { 384, 1152, 576 }, /* MPEG2 */
364 { 384, 1152, 576 }, /* MPEG2.5 */
365 };
366 
367 /* layers 1, 2, 3 */
368 const short bitsPerSlotTab[3] = { 32, 8, 8 };
369 
370 /* indexing = [version][mono/stereo]
371  * number of bytes in side info section of bitstream
372  */
373 const int/*short*/sideBytesTab[3][2] PROGMEM = { { 17, 32 }, /* MPEG-1: mono, stereo */
374 { 9, 17 }, /* MPEG-2: mono, stereo */
375 { 9, 17 }, /* MPEG-2.5: mono, stereo */
376 };
377 
378 /* indexing = [version][sampleRate][long (.l) or short (.s) block]
379  * sfBandTable[v][s].l[cb] = index of first bin in critical band cb (long blocks)
380  * sfBandTable[v][s].s[cb] = index of first bin in critical band cb (short blocks)
381  */
382 const SFBandTable_t sfBandTable[3][3] PROGMEM = {
383  { /* MPEG-1 (44, 48, 32 kHz) */
384  { {0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 52, 62, 74, 90, 110, 134, 162, 196, 238, 288, 342, 418, 576 },
385  {0, 4, 8, 12, 16, 22, 30, 40, 52, 66, 84, 106, 136, 192} },
386  { {0, 4, 8, 12, 16, 20, 24, 30, 36, 42, 50, 60, 72, 88, 106, 128, 156, 190, 230, 276, 330, 384, 576 },
387  {0, 4, 8, 12, 16, 22, 28, 38, 50, 64, 80, 100, 126, 192} },
388  { {0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 54, 66, 82, 102, 126, 156, 194, 240, 296, 364, 448, 550, 576 },
389  {0, 4, 8, 12, 16, 22, 30, 42, 58, 78, 104, 138, 180, 192} } },
390  { /* MPEG-2 (22, 24, 16 kHz) */
391  { {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576 },
392  {0, 4, 8, 12, 18, 24, 32, 42, 56, 74, 100, 132, 174, 192} },
393  { {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 114, 136, 162, 194, 232, 278, 332, 394, 464, 540, 576 },
394  {0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 136, 180, 192} },
395  { {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576 },
396  {0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192} }, },
397  { /* MPEG-2.5 (11, 12, 8 kHz) */
398  { {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576 },
399  {0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192 } },
400  { {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576 },
401  {0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192 } },
402  { {0, 12, 24, 36, 48, 60, 72, 88, 108, 132, 160, 192, 232, 280, 336, 400, 476, 566, 568, 570, 572, 574, 576 },
403  {0, 8, 16, 24, 36, 52, 72, 96, 124, 160, 162, 164, 166, 192 } }, },
404 };
405 
406 
407 /* indexing = [intensity scale on/off][left/right]
408  * format = Q30, range = [0.0, 1.414]
409  *
410  * illegal intensity position scalefactors (see comments on ISFMpeg1)
411  */
412 const int ISFIIP[2][2] PROGMEM = {
413  {0x40000000, 0x00000000}, /* mid-side off */
414  {0x40000000, 0x40000000}, /* mid-side on */
415 };
416 
417 const unsigned char uniqueIDTab[8] = {0x5f, 0x4b, 0x43, 0x5f, 0x5f, 0x4a, 0x52, 0x5f};
418 
419 /* anti-alias coefficients - see spec Annex B, table 3-B.9
420  * csa[0][i] = CSi, csa[1][i] = CAi
421  * format = Q31
422  */
423 const uint32_t csa[8][2] PROGMEM = {
424  {0x6dc253f0, 0xbe2500aa},
425  {0x70dcebe4, 0xc39e4949},
426  {0x798d6e73, 0xd7e33f4a},
427  {0x7ddd40a7, 0xe8b71176},
428  {0x7f6d20b7, 0xf3e4fe2f},
429  {0x7fe47e40, 0xfac1a3c7},
430  {0x7ffcb263, 0xfe2ebdc6},
431  {0x7fffc694, 0xff86c25d},
432 };
433 
434 /* format = Q30, right shifted by 12 (sign bits only in top 12 - undo this when rounding to short)
435  * this is to enable early-terminating multiplies on ARM
436  * range = [-1.144287109, 1.144989014]
437  * max gain of filter (per output sample) ~= 2.731
438  *
439  * new (properly sign-flipped) values
440  * - these actually are correct to 32 bits, (floating-pt coefficients in spec
441  * chosen such that only ~20 bits are required)
442  *
443  * Reordering - see table 3-B.3 in spec (appendix B)
444  *
445  * polyCoef[i] =
446  * D[ 0, 32, 64, ... 480], i = [ 0, 15]
447  * D[ 1, 33, 65, ... 481], i = [ 16, 31]
448  * D[ 2, 34, 66, ... 482], i = [ 32, 47]
449  * ...
450  * D[15, 47, 79, ... 495], i = [240,255]
451  *
452  * also exploits symmetry: D[i] = -D[512 - i], for i = [1, 255]
453  *
454  * polyCoef[256, 257, ... 263] are for special case of sample 16 (out of 0)
455  * see PolyphaseStereo() and PolyphaseMono()
456  */
457 
458 // prototypes
459 bool MP3Decoder_AllocateBuffers(void);
461 int MP3Decode( unsigned char *inbuf, int *bytesLeft, short *outbuf, int useSize);
462 void MP3GetLastFrameInfo();
463 int MP3GetNextFrameInfo(unsigned char *buf);
464 int MP3FindSyncWord(unsigned char *buf, int nBytes);
465 int MP3GetSampRate();
466 int MP3GetChannels();
467 int MP3GetBitsPerSample();
468 int MP3GetBitrate();
469 int MP3GetOutputSamps();
470 int MP3GetDecodedSamps();
471 
472 //internally used
473 void MP3Decoder_ClearBuffer(void);
474 void PolyphaseMono(short *pcm, int *vbuf, const uint32_t *coefBase);
475 void PolyphaseStereo(short *pcm, int *vbuf, const uint32_t *coefBase);
476 void SetBitstreamPointer(BitStreamInfo_t *bsi, int nBytes, unsigned char *buf);
477 unsigned int GetBits(BitStreamInfo_t *bsi, int nBits);
478 int CalcBitsUsed(BitStreamInfo_t *bsi, unsigned char *startBuf, int startOffset);
479 int DequantChannel(int *sampleBuf, int *workBuf, int *nonZeroBound, SideInfoSub_t *sis, ScaleFactorInfoSub_t *sfis, CriticalBandInfo_t *cbi);
480 void MidSideProc(int x[m_MAX_NCHAN][m_MAX_NSAMP], int nSamps, int mOut[2]);
481 void IntensityProcMPEG1(int x[m_MAX_NCHAN][m_MAX_NSAMP], int nSamps, ScaleFactorInfoSub_t *sfis, CriticalBandInfo_t *cbi, int midSideFlag, int mixFlag, int mOut[2]);
482 void IntensityProcMPEG2(int x[m_MAX_NCHAN][m_MAX_NSAMP], int nSamps, ScaleFactorInfoSub_t *sfis, CriticalBandInfo_t *cbi, ScaleFactorJS_t *sfjs, int midSideFlag, int mixFlag, int mOut[2]);
483 void FDCT32(int *x, int *d, int offset, int oddBlock, int gb);// __attribute__ ((section (".data")));
484 void FreeBuffers();
485 int CheckPadBit();
486 int UnpackFrameHeader(unsigned char *buf);
487 int UnpackSideInfo(unsigned char *buf);
488 int DecodeHuffman( unsigned char *buf, int *bitOffset, int huffBlockBits, int gr, int ch);
489 int MP3Dequantize( int gr);
490 int IMDCT( int gr, int ch);
491 int UnpackScaleFactors( unsigned char *buf, int *bitOffset, int bitsAvail, int gr, int ch);
492 int Subband(short *pcmBuf);
493 short ClipToShort(int x, int fracBits);
495 void UnpackSFMPEG1(BitStreamInfo_t *bsi, SideInfoSub_t *sis, ScaleFactorInfoSub_t *sfis, int *scfsi, int gr, ScaleFactorInfoSub_t *sfisGr0);
496 void UnpackSFMPEG2(BitStreamInfo_t *bsi, SideInfoSub_t *sis, ScaleFactorInfoSub_t *sfis, int gr, int ch, int modeExt, ScaleFactorJS_t *sfjs);
497 int MP3FindFreeSync(unsigned char *buf, unsigned char firstFH[4], int nBytes);
498 void MP3ClearBadFrame( short *outbuf);
499 int DecodeHuffmanPairs(int *xy, int nVals, int tabIdx, int bitsLeft, unsigned char *buf, int bitOffset);
500 int DecodeHuffmanQuads(int *vwxy, int nVals, int tabIdx, int bitsLeft, unsigned char *buf, int bitOffset);
501 int DequantBlock(int *inbuf, int *outbuf, int num, int scale);
502 void AntiAlias(int *x, int nBfly);
503 void WinPrevious(int *xPrev, int *xPrevWin, int btPrev);
504 int FreqInvertRescale(int *y, int *xPrev, int blockIdx, int es);
505 void idct9(int *x);
506 int IMDCT36(int *xCurr, int *xPrev, int *y, int btCurr, int btPrev, int blockIdx, int gb);
507 void imdct12(int *x, int *out);
508 int IMDCT12x3(int *xCurr, int *xPrev, int *y, int btPrev, int blockIdx, int gb);
509 int HybridTransform(int *xCurr, int *xPrev, int y[m_BLOCK_SIZE][m_NBANDS], SideInfoSub_t *sis, BlockCount_t *bc);
510 inline uint64_t SAR64(uint64_t x, int n) {return x >> n;}
511 inline int MULSHIFT32(int x, int y) { int z; z = (uint64_t) x * (uint64_t) y >> 32; return z;}
512 inline uint64_t MADD64(uint64_t sum64, int x, int y) {sum64 += (uint64_t) x * (uint64_t) y; return sum64;}/* returns 64-bit value in [edx:eax] */
513 inline int CLZ(int x){int numZeros; if (!x) return(sizeof(int) * 8); numZeros = 0; while (!(x & 0x80000000)){numZeros++; x <<= 1;} return numZeros;}
514 inline uint64_t xSAR64(uint64_t x, int n){return x >> n;}
515 inline int FASTABS(int x){ int sign; sign=x>>(sizeof(int)*8-1); x^=sign; x-=sign; return x;}
516 
517 
SubbandInfo_t
struct SubbandInfo SubbandInfo_t
MP3FrameInfo::samprate
int samprate
Definition: mp3_decoder.h:40
FrameHeader_t
struct FrameHeader FrameHeader_t
idct9
void idct9(int *x)
bitsPerSlotTab
const short bitsPerSlotTab[3]
Definition: mp3_decoder.h:368
DequantInfo::workBuf
int workBuf[m_MAX_REORDER_SAMPS]
Definition: mp3_decoder.h:117
FrameHeader::privateBit
int privateBit
Definition: mp3_decoder.h:78
ERR_MP3_INVALID_SUBBAND
@ ERR_MP3_INVALID_SUBBAND
Definition: mp3_decoder.h:32
MP3DecInfo::nGrans
int nGrans
Definition: mp3_decoder.h:191
SideInfoSub::sfactScale
int sfactScale
Definition: mp3_decoder.h:99
MP3DecInfo::samprate
int samprate
Definition: mp3_decoder.h:190
MP3ClearBadFrame
void MP3ClearBadFrame(short *outbuf)
MP3DecInfo::part23Length
int part23Length[m_MAX_NGRAN][m_MAX_NCHAN]
Definition: mp3_decoder.h:198
FASTABS
int FASTABS(int x)
Definition: mp3_decoder.h:515
SideInfoSub_t
struct SideInfoSub SideInfoSub_t
ScaleFactorJS_t
struct ScaleFactorJS ScaleFactorJS_t
HybridTransform
int HybridTransform(int *xCurr, int *xPrev, int y[m_BLOCK_SIZE][m_NBANDS], SideInfoSub_t *sis, BlockCount_t *bc)
MP3GetLastFrameInfo
void MP3GetLastFrameInfo()
BitStreamInfo_t
struct BitStreamInfo BitStreamInfo_t
MP3DecInfo::nChans
int nChans
Definition: mp3_decoder.h:189
BlockCount
Definition: mp3_decoder.h:150
CriticalBandInfo_t::cbEndL
int cbEndL
Definition: mp3_decoder.h:113
ERR_MP3_INVALID_FRAMEHEADER
@ ERR_MP3_INVALID_FRAMEHEADER
Definition: mp3_decoder.h:26
ScaleFactorInfoSub_t
struct ScaleFactorInfoSub ScaleFactorInfoSub_t
SAR64
uint64_t SAR64(uint64_t x, int n)
Definition: mp3_decoder.h:510
UnpackScaleFactors
int UnpackScaleFactors(unsigned char *buf, int *bitOffset, int bitsAvail, int gr, int ch)
BlockCount::currWinSwitch
int currWinSwitch
Definition: mp3_decoder.h:156
BlockCount::nBlocksPrev
int nBlocksPrev
Definition: mp3_decoder.h:153
Mono
@ Mono
Definition: mp3_decoder.h:63
HuffmanInfo::gb
int gb[m_MAX_NCHAN]
Definition: mp3_decoder.h:123
SideInfoSub
Definition: mp3_decoder.h:86
DequantInfo
Definition: mp3_decoder.h:116
noBits
@ noBits
Definition: mp3_decoder.h:127
MP3DecInfo::bitrate
int bitrate
Definition: mp3_decoder.h:188
HuffTabType_t
enum HuffTabType HuffTabType_t
HuffTabLookup::linBits
int linBits
Definition: mp3_decoder.h:137
MADD64
uint64_t MADD64(uint64_t sum64, int x, int y)
Definition: mp3_decoder.h:512
RefillBitstreamCache
void RefillBitstreamCache(BitStreamInfo_t *bsi)
Stereo
@ Stereo
Definition: mp3_decoder.h:60
ScaleFactorJS::intensityScale
int intensityScale
Definition: mp3_decoder.h:167
ERR_UNKNOWN
@ ERR_UNKNOWN
Definition: mp3_decoder.h:34
FrameHeader
Definition: mp3_decoder.h:72
c9_2
const int c9_2
Definition: mp3_decoder.h:216
uniqueIDTab
const unsigned char uniqueIDTab[8]
Definition: mp3_decoder.h:417
BitStreamInfo
Definition: mp3_decoder.h:52
DecodeHuffmanPairs
int DecodeHuffmanPairs(int *xy, int nVals, int tabIdx, int bitsLeft, unsigned char *buf, int bitOffset)
MP3FindSyncWord
int MP3FindSyncWord(unsigned char *buf, int nBytes)
FrameHeader::brIdx
int brIdx
Definition: mp3_decoder.h:75
IMDCTInfo::overBuf
int overBuf[m_MAX_NCHAN][m_MAX_NSAMP/2]
Definition: mp3_decoder.h:143
SideInfoSub::subBlockGain
int subBlockGain[3]
Definition: mp3_decoder.h:95
BlockCount::prevWinSwitch
int prevWinSwitch
Definition: mp3_decoder.h:155
MP3DecInfo
Definition: mp3_decoder.h:181
IMDCTInfo
Definition: mp3_decoder.h:141
BlockCount::gbOut
int gbOut
Definition: mp3_decoder.h:158
loopLinbits
@ loopLinbits
Definition: mp3_decoder.h:130
m_HUFF_OFFSET_08
const uint16_t m_HUFF_OFFSET_08
Definition: mp3_decoder.h:289
MP3Decoder_AllocateBuffers
bool MP3Decoder_AllocateBuffers(void)
Joint
@ Joint
Definition: mp3_decoder.h:61
ScaleFactorJS::slen
int slen[4]
Definition: mp3_decoder.h:168
c9_4
const int c9_4
Definition: mp3_decoder.h:218
HuffmanInfo::huffDecBuf
int huffDecBuf[m_MAX_NCHAN][m_MAX_NSAMP]
Definition: mp3_decoder.h:121
ScaleFactorJS
Definition: mp3_decoder.h:166
m_HUFF_OFFSET_06
const uint16_t m_HUFF_OFFSET_06
Definition: mp3_decoder.h:287
SideInfoSub::blockType
int blockType
Definition: mp3_decoder.h:92
ERR_MP3_OUT_OF_MEMORY
@ ERR_MP3_OUT_OF_MEMORY
Definition: mp3_decoder.h:24
PolyphaseMono
void PolyphaseMono(short *pcm, int *vbuf, const uint32_t *coefBase)
SideInfoSub::mixedBlock
int mixedBlock
Definition: mp3_decoder.h:93
MP3GetChannels
int MP3GetChannels()
MP3GetDecodedSamps
int MP3GetDecodedSamps()
xSAR64
uint64_t xSAR64(uint64_t x, int n)
Definition: mp3_decoder.h:514
CalcBitsUsed
int CalcBitsUsed(BitStreamInfo_t *bsi, unsigned char *startBuf, int startOffset)
IMDCTInfo::outBuf
int outBuf[m_MAX_NCHAN][m_BLOCK_SIZE][m_NBANDS]
Definition: mp3_decoder.h:142
UnpackSFMPEG2
void UnpackSFMPEG2(BitStreamInfo_t *bsi, SideInfoSub_t *sis, ScaleFactorInfoSub_t *sfis, int gr, int ch, int modeExt, ScaleFactorJS_t *sfjs)
MP3DecInfo::layer
int layer
Definition: mp3_decoder.h:194
IMDCTInfo::prevWinSwitch
int prevWinSwitch[m_MAX_NCHAN]
Definition: mp3_decoder.h:146
IntensityProcMPEG1
void IntensityProcMPEG1(int x[m_MAX_NCHAN][m_MAX_NSAMP], int nSamps, ScaleFactorInfoSub_t *sfis, CriticalBandInfo_t *cbi, int midSideFlag, int mixFlag, int mOut[2])
CheckPadBit
int CheckPadBit()
SideInfo_t
struct SideInfo SideInfo_t
NRTab
const char NRTab[6][3][4]
Definition: mp3_decoder.h:244
SFBandTable
Definition: mp3_decoder.h:47
SideInfoSub::part23Length
int part23Length
Definition: mp3_decoder.h:87
MP3Dequantize
int MP3Dequantize(int gr)
AntiAlias
void AntiAlias(int *x, int nBfly)
IMDCT
int IMDCT(int gr, int ch)
quadB
@ quadB
Definition: mp3_decoder.h:132
BitStreamInfo::nBytes
int nBytes
Definition: mp3_decoder.h:56
HuffTabLookup_t
struct HuffTabLookup HuffTabLookup_t
MULSHIFT32
int MULSHIFT32(int x, int y)
Definition: mp3_decoder.h:511
m_HUFF_OFFSET_12
const uint16_t m_HUFF_OFFSET_12
Definition: mp3_decoder.h:293
ScaleFactorInfoSub
Definition: mp3_decoder.h:161
DecodeHuffmanQuads
int DecodeHuffmanQuads(int *vwxy, int nVals, int tabIdx, int bitsLeft, unsigned char *buf, int bitOffset)
ERR_MP3_INVALID_IMDCT
@ ERR_MP3_INVALID_IMDCT
Definition: mp3_decoder.h:31
MP3FrameInfo_t
struct MP3FrameInfo MP3FrameInfo_t
SideInfoSub::winSwitchFlag
int winSwitchFlag
Definition: mp3_decoder.h:91
m_HUFF_OFFSET_13
const uint16_t m_HUFF_OFFSET_13
Definition: mp3_decoder.h:294
c9_3
const int c9_3
Definition: mp3_decoder.h:217
m_HUFF_OFFSET_07
const uint16_t m_HUFF_OFFSET_07
Definition: mp3_decoder.h:288
MP3DecInfo::nSlots
int nSlots
Definition: mp3_decoder.h:193
ERR_MP3_NULL_POINTER
@ ERR_MP3_NULL_POINTER
Definition: mp3_decoder.h:25
imdct12
void imdct12(int *x, int *out)
BitStreamInfo::cachedBits
int cachedBits
Definition: mp3_decoder.h:55
MPEGVersion_t
MPEGVersion_t
Definition: mp3_decoder.h:66
ERR_MP3_INVALID_HUFFCODES
@ ERR_MP3_INVALID_HUFFCODES
Definition: mp3_decoder.h:29
invalidTab
@ invalidTab
Definition: mp3_decoder.h:133
m_HUFF_OFFSET_01
const uint16_t m_HUFF_OFFSET_01
Definition: mp3_decoder.h:283
SubbandInfo::vbuf
int vbuf[m_MAX_NCHAN *m_VBUF_LENGTH]
Definition: mp3_decoder.h:177
m_HUFF_OFFSET_16
const uint16_t m_HUFF_OFFSET_16
Definition: mp3_decoder.h:296
oneShot
@ oneShot
Definition: mp3_decoder.h:128
Dual
@ Dual
Definition: mp3_decoder.h:62
m_HUFF_OFFSET_05
const uint16_t m_HUFF_OFFSET_05
Definition: mp3_decoder.h:286
SetBitstreamPointer
void SetBitstreamPointer(BitStreamInfo_t *bsi, int nBytes, unsigned char *buf)
IMDCT36
int IMDCT36(int *xCurr, int *xPrev, int *y, int btCurr, int btPrev, int blockIdx, int gb)
HuffmanInfo
Definition: mp3_decoder.h:120
SideInfo::privateBits
int privateBits
Definition: mp3_decoder.h:105
FrameHeader::emphasis
int emphasis
Definition: mp3_decoder.h:82
m_HUFF_OFFSET_03
const uint16_t m_HUFF_OFFSET_03
Definition: mp3_decoder.h:285
c3_0
const int c3_0
Definition: mp3_decoder.h:222
SubbandInfo::vindex
int vindex
Definition: mp3_decoder.h:178
FrameHeader::CRCWord
int CRCWord
Definition: mp3_decoder.h:83
m_HUFF_OFFSET_02
const uint16_t m_HUFF_OFFSET_02
Definition: mp3_decoder.h:284
FrameHeader::crc
int crc
Definition: mp3_decoder.h:74
SFBandTable::l
int l[23]
Definition: mp3_decoder.h:48
CriticalBandInfo_t::cbEndSMax
int cbEndSMax
Definition: mp3_decoder.h:112
IMDCTInfo_t
struct IMDCTInfo IMDCTInfo_t
IMDCT12x3
int IMDCT12x3(int *xCurr, int *xPrev, int *y, int btPrev, int blockIdx, int gb)
FreqInvertRescale
int FreqInvertRescale(int *y, int *xPrev, int blockIdx, int es)
m_HUFF_OFFSET_15
const uint16_t m_HUFF_OFFSET_15
Definition: mp3_decoder.h:295
MP3DecInfo::nGranSamps
int nGranSamps
Definition: mp3_decoder.h:192
ERR_MP3_INVALID_SIDEINFO
@ ERR_MP3_INVALID_SIDEINFO
Definition: mp3_decoder.h:27
MP3Decoder_FreeBuffers
void MP3Decoder_FreeBuffers()
FrameHeader::layer
int layer
Definition: mp3_decoder.h:73
IntensityProcMPEG2
void IntensityProcMPEG2(int x[m_MAX_NCHAN][m_MAX_NSAMP], int nSamps, ScaleFactorInfoSub_t *sfis, CriticalBandInfo_t *cbi, ScaleFactorJS_t *sfjs, int midSideFlag, int mixFlag, int mOut[2])
ERR_MP3_INVALID_DEQUANTIZE
@ ERR_MP3_INVALID_DEQUANTIZE
Definition: mp3_decoder.h:30
SideInfoSub::region0Count
int region0Count
Definition: mp3_decoder.h:96
c9_0
const int c9_0
Definition: mp3_decoder.h:214
FrameHeader::origFlag
int origFlag
Definition: mp3_decoder.h:81
SideInfoSub::count1TableSelect
int count1TableSelect
Definition: mp3_decoder.h:100
HuffTabLookup::tabType
int tabType
Definition: mp3_decoder.h:138
c9_1
const int c9_1
Definition: mp3_decoder.h:215
ScaleFactorInfoSub::l
char l[23]
Definition: mp3_decoder.h:162
FrameHeader::copyFlag
int copyFlag
Definition: mp3_decoder.h:80
UnpackSFMPEG1
void UnpackSFMPEG1(BitStreamInfo_t *bsi, SideInfoSub_t *sis, ScaleFactorInfoSub_t *sfis, int *scfsi, int gr, ScaleFactorInfoSub_t *sfisGr0)
StereoMode_t
StereoMode_t
Definition: mp3_decoder.h:59
HuffTabType
HuffTabType
Definition: mp3_decoder.h:126
MP3DecInfo::mainDataBytes
int mainDataBytes
Definition: mp3_decoder.h:197
MP3FrameInfo::nChans
int nChans
Definition: mp3_decoder.h:39
UnpackFrameHeader
int UnpackFrameHeader(unsigned char *buf)
MP3DecInfo::freeBitrateFlag
int freeBitrateFlag
Definition: mp3_decoder.h:185
ScaleFactorInfoSub::s
char s[13][3]
Definition: mp3_decoder.h:163
MP3GetNextFrameInfo
int MP3GetNextFrameInfo(unsigned char *buf)
BlockCount::prevType
int prevType
Definition: mp3_decoder.h:154
MP3GetBitsPerSample
int MP3GetBitsPerSample()
SideInfoSub::preFlag
int preFlag
Definition: mp3_decoder.h:98
HuffmanInfo::nonZeroBound
int nonZeroBound[m_MAX_NCHAN]
Definition: mp3_decoder.h:122
MP3FrameInfo::bitsPerSample
int bitsPerSample
Definition: mp3_decoder.h:41
MP3FrameInfo::outputSamps
int outputSamps
Definition: mp3_decoder.h:42
CLZ
int CLZ(int x)
Definition: mp3_decoder.h:513
m_HUFF_OFFSET_11
const uint16_t m_HUFF_OFFSET_11
Definition: mp3_decoder.h:292
ClipToShort
short ClipToShort(int x, int fracBits)
IMDCTInfo::prevType
int prevType[m_MAX_NCHAN]
Definition: mp3_decoder.h:145
loopNoLinbits
@ loopNoLinbits
Definition: mp3_decoder.h:129
FDCT32
void FDCT32(int *x, int *d, int offset, int oddBlock, int gb)
c18
const uint32_t c18[9]
Definition: mp3_decoder.h:228
MP3GetOutputSamps
int MP3GetOutputSamps()
DecodeHuffman
int DecodeHuffman(unsigned char *buf, int *bitOffset, int huffBlockBits, int gr, int ch)
m_SFLenTab
const char m_SFLenTab[16][2]
Definition: mp3_decoder.h:231
ERR_MP3_FREE_BITRATE_SYNC
@ ERR_MP3_FREE_BITRATE_SYNC
Definition: mp3_decoder.h:23
MP3FrameInfo
Definition: mp3_decoder.h:37
FrameHeader::paddingBit
int paddingBit
Definition: mp3_decoder.h:77
MP3FrameInfo::bitrate
int bitrate
Definition: mp3_decoder.h:38
SideInfoSub::tableSelect
int tableSelect[3]
Definition: mp3_decoder.h:94
MP3FrameInfo::layer
int layer
Definition: mp3_decoder.h:43
GetBits
unsigned int GetBits(BitStreamInfo_t *bsi, int nBits)
m_HUFF_OFFSET_09
const uint16_t m_HUFF_OFFSET_09
Definition: mp3_decoder.h:290
MP3GetBitrate
int MP3GetBitrate()
MP3DecInfo::mainDataBegin
int mainDataBegin
Definition: mp3_decoder.h:196
c6
const int c6[3]
Definition: mp3_decoder.h:223
IMDCTInfo::numPrevIMDCT
int numPrevIMDCT[m_MAX_NCHAN]
Definition: mp3_decoder.h:144
SideInfo::scfsi
int scfsi[m_MAX_NCHAN][m_MAX_SCFBD]
Definition: mp3_decoder.h:106
WinPrevious
void WinPrevious(int *xPrev, int *xPrevWin, int btPrev)
ERR_MP3_INVALID_SCALEFACT
@ ERR_MP3_INVALID_SCALEFACT
Definition: mp3_decoder.h:28
PolyphaseStereo
void PolyphaseStereo(short *pcm, int *vbuf, const uint32_t *coefBase)
m_HUFF_OFFSET_24
const uint16_t m_HUFF_OFFSET_24
Definition: mp3_decoder.h:297
HuffmanInfo_t
struct HuffmanInfo HuffmanInfo_t
SideInfo
Definition: mp3_decoder.h:103
ScaleFactorJS::nr
int nr[4]
Definition: mp3_decoder.h:169
MP3FindFreeSync
int MP3FindFreeSync(unsigned char *buf, unsigned char firstFH[4], int nBytes)
SideInfoSub::nBigvals
int nBigvals
Definition: mp3_decoder.h:88
DequantInfo_t
struct DequantInfo DequantInfo_t
SubbandInfo
Definition: mp3_decoder.h:176
ERR_MP3_NONE
@ ERR_MP3_NONE
Definition: mp3_decoder.h:20
SideInfoSub::sfCompress
int sfCompress
Definition: mp3_decoder.h:90
IMDCTInfo::gb
int gb[m_MAX_NCHAN]
Definition: mp3_decoder.h:147
BlockCount::nBlocksLong
int nBlocksLong
Definition: mp3_decoder.h:151
MidSideProc
void MidSideProc(int x[m_MAX_NCHAN][m_MAX_NSAMP], int nSamps, int mOut[2])
DequantChannel
int DequantChannel(int *sampleBuf, int *workBuf, int *nonZeroBound, SideInfoSub_t *sis, ScaleFactorInfoSub_t *sfis, CriticalBandInfo_t *cbi)
MP3DecInfo_t
struct MP3DecInfo MP3DecInfo_t
FreeBuffers
void FreeBuffers()
FrameHeader::srIdx
int srIdx
Definition: mp3_decoder.h:76
SideInfo::mainDataBegin
int mainDataBegin
Definition: mp3_decoder.h:104
PROGMEM
const int pow14[4] PROGMEM
Definition: mp3_decoder.h:259
SFBandTable::s
int s[14]
Definition: mp3_decoder.h:49
MPEG25
@ MPEG25
Definition: mp3_decoder.h:69
MP3DecInfo::freeBitrateSlots
int freeBitrateSlots
Definition: mp3_decoder.h:186
MPEG2
@ MPEG2
Definition: mp3_decoder.h:68
MPEG1
@ MPEG1
Definition: mp3_decoder.h:67
m_HUFF_OFFSET_10
const uint16_t m_HUFF_OFFSET_10
Definition: mp3_decoder.h:291
UnpackSideInfo
int UnpackSideInfo(unsigned char *buf)
quadA
@ quadA
Definition: mp3_decoder.h:131
BlockCount::gbIn
int gbIn
Definition: mp3_decoder.h:157
SideInfoSub::region1Count
int region1Count
Definition: mp3_decoder.h:97
ERR_MP3_MAINDATA_UNDERFLOW
@ ERR_MP3_MAINDATA_UNDERFLOW
Definition: mp3_decoder.h:22
Subband
int Subband(short *pcmBuf)
ERR_MP3_INDATA_UNDERFLOW
@ ERR_MP3_INDATA_UNDERFLOW
Definition: mp3_decoder.h:21
BitStreamInfo::bytePtr
unsigned char * bytePtr
Definition: mp3_decoder.h:53
SFBandTable_t
struct SFBandTable SFBandTable_t
CriticalBandInfo_t::cbType
int cbType
Definition: mp3_decoder.h:110
SideInfoSub::globalGain
int globalGain
Definition: mp3_decoder.h:89
preTab
const char preTab[22]
Definition: mp3_decoder.h:256
MP3FrameInfo::version
int version
Definition: mp3_decoder.h:44
MP3Decode
int MP3Decode(unsigned char *inbuf, int *bytesLeft, short *outbuf, int useSize)
DequantBlock
int DequantBlock(int *inbuf, int *outbuf, int num, int scale)
CriticalBandInfo_t
Definition: mp3_decoder.h:109
FrameHeader::modeExt
int modeExt
Definition: mp3_decoder.h:79
BlockCount_t
struct BlockCount BlockCount_t
BlockCount::nBlocksTotal
int nBlocksTotal
Definition: mp3_decoder.h:152
HuffTabLookup
Definition: mp3_decoder.h:136
BitStreamInfo::iCache
unsigned int iCache
Definition: mp3_decoder.h:54
MP3Decoder_ClearBuffer
void MP3Decoder_ClearBuffer(void)
MP3GetSampRate
int MP3GetSampRate()
MP3DecInfo::mainBuf
unsigned char mainBuf[m_MAINBUF_SIZE]
Definition: mp3_decoder.h:183