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