The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3147 lines
131KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. /*
  19. IMPORTANT DISCLAIMER: By choosing to enable the JUCE_USE_MP3AUDIOFORMAT flag and
  20. to compile this MP3 code into your software, you do so AT YOUR OWN RISK! By doing so,
  21. you are agreeing that Raw Material Software is in no way responsible for any patent,
  22. copyright, or other legal issues that you may suffer as a result.
  23. The code in juce_MP3AudioFormat.cpp is NOT guaranteed to be free from infringements of 3rd-party
  24. intellectual property. If you wish to use it, please seek your own independent advice about the
  25. legality of doing so. If you are not willing to accept full responsibility for the consequences
  26. of using this code, then do not enable the JUCE_USE_MP3AUDIOFORMAT setting.
  27. */
  28. #if JUCE_USE_MP3AUDIOFORMAT
  29. namespace MP3Decoder
  30. {
  31. struct AllocationTable { int16 bits, d; };
  32. const struct AllocationTable allocTable0[] =
  33. {
  34. {4, 0}, {5, 3}, {3, -3}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {14, -8191}, {15, -16383}, {16, -32767},
  35. {4, 0}, {5, 3}, {3, -3}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {14, -8191}, {15, -16383}, {16, -32767},
  36. {4, 0}, {5, 3}, {3, -3}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {14, -8191}, {15, -16383}, {16, -32767},
  37. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {16, -32767},
  38. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {16, -32767},
  39. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {16, -32767},
  40. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {16, -32767},
  41. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {16, -32767},
  42. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {16, -32767},
  43. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {16, -32767},
  44. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {16, -32767},
  45. {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767}, {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767},
  46. {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767}, {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767},
  47. {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767}, {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767},
  48. {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767}, {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767},
  49. {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767}, {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767},
  50. {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767}, {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767},
  51. {2, 0}, {5, 3}, {7, 5}, {16, -32767}, {2, 0}, {5, 3}, {7, 5}, {16, -32767}, {2, 0}, {5, 3}, {7, 5}, {16, -32767}, {2, 0}, {5, 3}, {7, 5}, {16, -32767}
  52. };
  53. const struct AllocationTable allocTable1[] =
  54. {
  55. {4, 0}, {5, 3}, {3, -3}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {14, -8191}, {15, -16383}, {16, -32767},
  56. {4, 0}, {5, 3}, {3, -3}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {14, -8191}, {15, -16383}, {16, -32767},
  57. {4, 0}, {5, 3}, {3, -3}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {14, -8191}, {15, -16383}, {16, -32767},
  58. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {16, -32767},
  59. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {16, -32767},
  60. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {16, -32767},
  61. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {16, -32767},
  62. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {16, -32767},
  63. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {16, -32767},
  64. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {16, -32767},
  65. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {16, -32767},
  66. {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767}, {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767},
  67. {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767}, {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767},
  68. {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767}, {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767},
  69. {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767}, {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767},
  70. {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767}, {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767},
  71. {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767}, {3, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {16, -32767},
  72. {2, 0}, {5, 3}, {7, 5}, {16, -32767}, {2, 0}, {5, 3}, {7, 5}, {16, -32767}, {2, 0}, {5, 3}, {7, 5}, {16, -32767}, {2, 0}, {5, 3}, {7, 5}, {16, -32767},
  73. {2, 0}, {5, 3}, {7, 5}, {16, -32767}, {2, 0}, {5, 3}, {7, 5}, {16, -32767}, {2, 0}, {5, 3}, {7, 5}, {16, -32767}
  74. };
  75. const struct AllocationTable allocTable2[] =
  76. {
  77. {4, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {14, -8191}, {15, -16383},
  78. {4, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {14, -8191}, {15, -16383},
  79. {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63},
  80. {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63},
  81. {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}
  82. };
  83. const struct AllocationTable allocTable3[] =
  84. {
  85. {4, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {14, -8191}, {15, -16383},
  86. {4, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {14, -8191}, {15, -16383},
  87. {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63},
  88. {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63},
  89. {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63},
  90. {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63},
  91. {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}
  92. };
  93. const struct AllocationTable allocTable4[] =
  94. {
  95. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {14, -8191},
  96. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {14, -8191},
  97. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {14, -8191},
  98. {4, 0}, {5, 3}, {7, 5}, {3, -3}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {8, -127}, {9, -255}, {10, -511}, {11, -1023}, {12, -2047}, {13, -4095}, {14, -8191},
  99. {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63},
  100. {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63},
  101. {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63},
  102. {3, 0}, {5, 3}, {7, 5}, {10, 9}, {4, -7}, {5, -15}, {6, -31}, {7, -63}, {2, 0}, {5, 3}, {7, 5}, {10, 9}, {2, 0}, {5, 3}, {7, 5}, {10, 9},
  103. {2, 0}, {5, 3}, {7, 5}, {10, 9}, {2, 0}, {5, 3}, {7, 5}, {10, 9}, {2, 0}, {5, 3}, {7, 5}, {10, 9}, {2, 0}, {5, 3}, {7, 5}, {10, 9},
  104. {2, 0}, {5, 3}, {7, 5}, {10, 9}, {2, 0}, {5, 3}, {7, 5}, {10, 9}, {2, 0}, {5, 3}, {7, 5}, {10, 9}, {2, 0}, {5, 3}, {7, 5}, {10, 9},
  105. {2, 0}, {5, 3}, {7, 5}, {10, 9}, {2, 0}, {5, 3}, {7, 5}, {10, 9}, {2, 0}, {5, 3}, {7, 5}, {10, 9}, {2, 0}, {5, 3}, {7, 5}, {10, 9},
  106. {2, 0}, {5, 3}, {7, 5}, {10, 9}, {2, 0}, {5, 3}, {7, 5}, {10, 9}, {2, 0}, {5, 3}, {7, 5}, {10, 9}, {2, 0}, {5, 3}, {7, 5}, {10, 9},
  107. {2, 0}, {5, 3}, {7, 5}, {10, 9}
  108. };
  109. struct BandInfoStruct
  110. {
  111. int16 longIndex[23];
  112. int16 longDiff[22];
  113. int16 shortIndex[14];
  114. int16 shortDiff[13];
  115. };
  116. const BandInfoStruct bandInfo[9] =
  117. {
  118. { {0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 52, 62, 74, 90, 110, 134, 162, 196, 238, 288, 342, 418, 576},
  119. {4, 4, 4, 4, 4, 4, 6, 6, 8, 8, 10, 12, 16, 20, 24, 28, 34, 42, 50, 54, 76, 158},
  120. {0, 4 * 3, 8 * 3, 12 * 3, 16 * 3, 22 * 3, 30 * 3, 40 * 3, 52 * 3, 66 * 3, 84 * 3, 106 * 3, 136 * 3, 192 * 3},
  121. {4, 4, 4, 4, 6, 8, 10, 12, 14, 18, 22, 30, 56} },
  122. { {0, 4, 8, 12, 16, 20, 24, 30, 36, 42, 50, 60, 72, 88, 106, 128, 156, 190, 230, 276, 330, 384, 576},
  123. {4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 10, 12, 16, 18, 22, 28, 34, 40, 46, 54, 54, 192},
  124. {0, 4 * 3, 8 * 3, 12 * 3, 16 * 3, 22 * 3, 28 * 3, 38 * 3, 50 * 3, 64 * 3, 80 * 3, 100 * 3, 126 * 3, 192 * 3},
  125. {4, 4, 4, 4, 6, 6, 10, 12, 14, 16, 20, 26, 66} },
  126. { {0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 54, 66, 82, 102, 126, 156, 194, 240, 296, 364, 448, 550, 576},
  127. {4, 4, 4, 4, 4, 4, 6, 6, 8, 10, 12, 16, 20, 24, 30, 38, 46, 56, 68, 84, 102, 26},
  128. {0, 4 * 3, 8 * 3, 12 * 3, 16 * 3, 22 * 3, 30 * 3, 42 * 3, 58 * 3, 78 * 3, 104 * 3, 138 * 3, 180 * 3, 192 * 3},
  129. {4, 4, 4, 4, 6, 8, 12, 16, 20, 26, 34, 42, 12} },
  130. { {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576},
  131. {6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54 },
  132. {0, 4 * 3, 8 * 3, 12 * 3, 18 * 3, 24 * 3, 32 * 3, 42 * 3, 56 * 3, 74 * 3, 100 * 3, 132 * 3, 174 * 3, 192 * 3},
  133. {4, 4, 4, 6, 6, 8, 10, 14, 18, 26, 32, 42, 18 } },
  134. { {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 114, 136, 162, 194, 232, 278, 332, 394, 464, 540, 576},
  135. {6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16, 18, 22, 26, 32, 38, 46, 54, 62, 70, 76, 36 },
  136. {0, 4 * 3, 8 * 3, 12 * 3, 18 * 3, 26 * 3, 36 * 3, 48 * 3, 62 * 3, 80 * 3, 104 * 3, 136 * 3, 180 * 3, 192 * 3},
  137. {4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 32, 44, 12 } },
  138. { {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576},
  139. {6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54 },
  140. {0, 4 * 3, 8 * 3, 12 * 3, 18 * 3, 26 * 3, 36 * 3, 48 * 3, 62 * 3, 80 * 3, 104 * 3, 134 * 3, 174 * 3, 192 * 3},
  141. {4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18 } },
  142. { {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576},
  143. {6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54},
  144. {0, 12, 24, 36, 54, 78, 108, 144, 186, 240, 312, 402, 522, 576},
  145. {4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18} },
  146. { {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576},
  147. {6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54},
  148. {0, 12, 24, 36, 54, 78, 108, 144, 186, 240, 312, 402, 522, 576},
  149. {4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18} },
  150. { {0, 12, 24, 36, 48, 60, 72, 88, 108, 132, 160, 192, 232, 280, 336, 400, 476, 566, 568, 570, 572, 574, 576},
  151. {12, 12, 12, 12, 12, 12, 16, 20, 24, 28, 32, 40, 48, 56, 64, 76, 90, 2, 2, 2, 2, 2},
  152. {0, 24, 48, 72, 108, 156, 216, 288, 372, 480, 486, 492, 498, 576},
  153. {8, 8, 8, 12, 16, 20, 24, 28, 36, 2, 2, 2, 26} }
  154. };
  155. const double decodeWindow[] =
  156. {
  157. 0.000000000, -0.000015259, -0.000015259, -0.000015259, -0.000015259, -0.000015259, -0.000015259, -0.000030518,
  158. -0.000030518, -0.000030518, -0.000030518, -0.000045776, -0.000045776, -0.000061035, -0.000061035, -0.000076294,
  159. -0.000076294, -0.000091553, -0.000106812, -0.000106812, -0.000122070, -0.000137329, -0.000152588, -0.000167847,
  160. -0.000198364, -0.000213623, -0.000244141, -0.000259399, -0.000289917, -0.000320435, -0.000366211, -0.000396729,
  161. -0.000442505, -0.000473022, -0.000534058, -0.000579834, -0.000625610, -0.000686646, -0.000747681, -0.000808716,
  162. -0.000885010, -0.000961304, -0.001037598, -0.001113892, -0.001205444, -0.001296997, -0.001388550, -0.001480103,
  163. -0.001586914, -0.001693726, -0.001785278, -0.001907349, -0.002014160, -0.002120972, -0.002243042, -0.002349854,
  164. -0.002456665, -0.002578735, -0.002685547, -0.002792358, -0.002899170, -0.002990723, -0.003082275, -0.003173828,
  165. -0.003250122, -0.003326416, -0.003387451, -0.003433228, -0.003463745, -0.003479004, -0.003479004, -0.003463745,
  166. -0.003417969, -0.003372192, -0.003280640, -0.003173828, -0.003051758, -0.002883911, -0.002700806, -0.002487183,
  167. -0.002227783, -0.001937866, -0.001617432, -0.001266479, -0.000869751, -0.000442505, 0.000030518, 0.000549316,
  168. 0.001098633, 0.001693726, 0.002334595, 0.003005981, 0.003723145, 0.004486084, 0.005294800, 0.006118774,
  169. 0.007003784, 0.007919312, 0.008865356, 0.009841919, 0.010848999, 0.011886597, 0.012939453, 0.014022827,
  170. 0.015121460, 0.016235352, 0.017349243, 0.018463135, 0.019577026, 0.020690918, 0.021789551, 0.022857666,
  171. 0.023910522, 0.024932861, 0.025909424, 0.026840210, 0.027725220, 0.028533936, 0.029281616, 0.029937744,
  172. 0.030532837, 0.031005859, 0.031387329, 0.031661987, 0.031814575, 0.031845093, 0.031738281, 0.031478882,
  173. 0.031082153, 0.030517578, 0.029785156, 0.028884888, 0.027801514, 0.026535034, 0.025085449, 0.023422241,
  174. 0.021575928, 0.019531250, 0.017257690, 0.014801025, 0.012115479, 0.009231567, 0.006134033, 0.002822876,
  175. -0.000686646, -0.004394531, -0.008316040, -0.012420654, -0.016708374, -0.021179199, -0.025817871, -0.030609131,
  176. -0.035552979, -0.040634155, -0.045837402, -0.051132202, -0.056533813, -0.061996460, -0.067520142, -0.073059082,
  177. -0.078628540, -0.084182739, -0.089706421, -0.095169067, -0.100540161, -0.105819702, -0.110946655, -0.115921021,
  178. -0.120697021, -0.125259399, -0.129562378, -0.133590698, -0.137298584, -0.140670776, -0.143676758, -0.146255493,
  179. -0.148422241, -0.150115967, -0.151306152, -0.151962280, -0.152069092, -0.151596069, -0.150497437, -0.148773193,
  180. -0.146362305, -0.143264771, -0.139450073, -0.134887695, -0.129577637, -0.123474121, -0.116577148, -0.108856201,
  181. -0.100311279, -0.090927124, -0.080688477, -0.069595337, -0.057617187, -0.044784546, -0.031082153, -0.016510010,
  182. -0.001068115, 0.015228271, 0.032379150, 0.050354004, 0.069168091, 0.088775635, 0.109161377, 0.130310059,
  183. 0.152206421, 0.174789429, 0.198059082, 0.221984863, 0.246505737, 0.271591187, 0.297210693, 0.323318481,
  184. 0.349868774, 0.376800537, 0.404083252, 0.431655884, 0.459472656, 0.487472534, 0.515609741, 0.543823242,
  185. 0.572036743, 0.600219727, 0.628295898, 0.656219482, 0.683914185, 0.711318970, 0.738372803, 0.765029907,
  186. 0.791213989, 0.816864014, 0.841949463, 0.866363525, 0.890090942, 0.913055420, 0.935195923, 0.956481934,
  187. 0.976852417, 0.996246338, 1.014617920, 1.031936646, 1.048156738, 1.063217163, 1.077117920, 1.089782715,
  188. 1.101211548, 1.111373901, 1.120223999, 1.127746582, 1.133926392, 1.138763428, 1.142211914, 1.144287109,
  189. 1.144989014
  190. };
  191. const int16 huffmanTab0[] = { 0 };
  192. const int16 huffmanTab1[] = { -5,-3,-1,17,1,16,0 };
  193. const int16 huffmanTab2[] = { -15,-11,-9,-5,-3,-1,34,2,18,-1,33,32,17,-1,1,16,0 };
  194. const int16 huffmanTab3[] = { -13,-11,-9,-5,-3,-1,34,2,18,-1,33,32,16,17,-1,1,0 };
  195. const int16 huffmanTab5[] = { -29,-25,-23,-15,-7,-5,-3,-1,51,35,50,49,-3,-1,19,3,-1,48,34,-3,-1,18,33,-1,2,32,17,-1,1,16,0 };
  196. const int16 huffmanTab6[] = { -25,-19,-13,-9,-5,-3,-1,51,3,35,-1,50,48,-1,19,49,-3,-1,34,2,18,-3,-1,33,32,1,-1,17,-1,16,0 };
  197. const int16 huffmanTab7[] =
  198. {
  199. -69,-65,-57,-39,-29,-17,-11,-7,-3,-1,85,69,-1,84,83,-1,53,68,-3,-1,37,82,21,-5,-1,81,-1,5,52,-1,80,-1,67,51,
  200. -5,-3,-1,36,66,20,-1,65,64,-11,-7,-3,-1,4,35,-1,50,3,-1,19,49,-3,-1,48,34,18,-5,-1,33,-1,2,32,17,-1,1,16,0
  201. };
  202. const int16 huffmanTab8[] =
  203. {
  204. -65,-63,-59,-45,-31,-19,-13,-7,-5,-3,-1,85,84,69,83,-3,-1,53,68,37,-3,-1,82,5,21,-5,-1,81,-1,52,67,-3,-1,80,
  205. 51,36,-5,-3,-1,66,20,65,-3,-1,4,64,-1,35,50,-9,-7,-3,-1,19,49,-1,3,48,34,-1,2,32,-1,18,33,17,-3,-1,1,16,0
  206. };
  207. const int16 huffmanTab9[] =
  208. {
  209. -63,-53,-41,-29,-19,-11,-5,-3,-1,85,69,53,-1,83,-1,84,5,-3,-1,68,37,-1,82,21,-3,-1,81,52,-1,67,-1,80,4,-7,-3,
  210. -1,36,66,-1,51,64,-1,20,65,-5,-3,-1,35,50,19,-1,49,-1,3,48,-5,-3,-1,34,2,18,-1,33,32,-3,-1,17,1,-1,16,0
  211. };
  212. const int16 huffmanTab10[] =
  213. {
  214. -125,-121,-111,-83,-55,-35,-21,-13,-7,-3,-1,119,103,-1,118,87,-3,-1,117,102,71,-3,-1,116,86,-1,101,55,-9,-3,
  215. -1,115,70,-3,-1,85,84,99,-1,39,114,-11,-5,-3,-1,100,7,112,-1,98,-1,69,53,-5,-1,6,-1,83,68,23,-17,-5,-1,113,
  216. -1,54,38,-5,-3,-1,37,82,21,-1,81,-1,52,67,-3,-1,22,97,-1,96,-1,5,80,-19,-11,-7,-3,-1,36,66,-1,51,4,-1,20,
  217. 65,-3,-1,64,35,-1,50,3,-3,-1,19,49,-1,48,34,-7,-3,-1,18,33,-1,2,32,17,-1,1,16,0
  218. };
  219. const int16 huffmanTab11[] =
  220. {
  221. -121,-113,-89,-59,-43,-27,-17,-7,-3,-1,119,103,-1,118,117,-3,-1,102,71,-1,116,-1,87,85,-5,-3,-1,86,101,55,
  222. -1,115,70,-9,-7,-3,-1,69,84,-1,53,83,39,-1,114,-1,100,7,-5,-1,113,-1,23,112,-3,-1,54,99,-1,96,-1,68,37,-13,
  223. -7,-5,-3,-1,82,5,21,98,-3,-1,38,6,22,-5,-1,97,-1,81,52,-5,-1,80,-1,67,51,-1,36,66,-15,-11,-7,-3,-1,20,65,
  224. -1,4,64,-1,35,50,-1,19,49,-5,-3,-1,3,48,34,33,-5,-1,18,-1,2,32,17,-3,-1,1,16,0
  225. };
  226. const int16 huffmanTab12[] =
  227. {
  228. -115,-99,-73,-45,-27,-17,-9,-5,-3,-1,119,103,118,-1,87,117,-3,-1,102,71,-1,116,101,-3,-1,86,55,-3,-1,115,
  229. 85,39,-7,-3,-1,114,70,-1,100,23,-5,-1,113,-1,7,112,-1,54,99,-13,-9,-3,-1,69,84,-1,68,-1,6,5,-1,38,98,-5,
  230. -1,97,-1,22,96,-3,-1,53,83,-1,37,82,-17,-7,-3,-1,21,81,-1,52,67,-5,-3,-1,80,4,36,-1,66,20,-3,-1,51,65,-1,
  231. 35,50,-11,-7,-5,-3,-1,64,3,48,19,-1,49,34,-1,18,33,-7,-5,-3,-1,2,32,0,17,-1,1,16
  232. };
  233. const int16 huffmanTab13[] =
  234. {
  235. -509,-503,-475,-405,-333,-265,-205,-153,-115,-83,-53,-35,-21,-13,-9,-7,-5,-3,-1,254,252,253,237,255,-1,239,223,
  236. -3,-1,238,207,-1,222,191,-9,-3,-1,251,206,-1,220,-1,175,233,-1,236,221,-9,-5,-3,-1,250,205,190,-1,235,159,-3,
  237. -1,249,234,-1,189,219,-17,-9,-3,-1,143,248,-1,204,-1,174,158,-5,-1,142,-1,127,126,247,-5,-1,218,-1,173,188,-3,
  238. -1,203,246,111,-15,-7,-3,-1,232,95,-1,157,217,-3,-1,245,231,-1,172,187,-9,-3,-1,79,244,-3,-1,202,230,243,-1,
  239. 63,-1,141,216,-21,-9,-3,-1,47,242,-3,-1,110,156,15,-5,-3,-1,201,94,171,-3,-1,125,215,78,-11,-5,-3,-1,200,214,
  240. 62,-1,185,-1,155,170,-1,31,241,-23,-13,-5,-1,240,-1,186,229,-3,-1,228,140,-1,109,227,-5,-1,226,-1,46,14,-1,30,
  241. 225,-15,-7,-3,-1,224,93,-1,213,124,-3,-1,199,77,-1,139,184,-7,-3,-1,212,154,-1,169,108,-1,198,61,-37,-21,-9,-5,
  242. -3,-1,211,123,45,-1,210,29,-5,-1,183,-1,92,197,-3,-1,153,122,195,-7,-5,-3,-1,167,151,75,209,-3,-1,13,208,-1,
  243. 138,168,-11,-7,-3,-1,76,196,-1,107,182,-1,60,44,-3,-1,194,91,-3,-1,181,137,28,-43,-23,-11,-5,-1,193,-1,152,12,
  244. -1,192,-1,180,106,-5,-3,-1,166,121,59,-1,179,-1,136,90,-11,-5,-1,43,-1,165,105,-1,164,-1,120,135,-5,-1,148,-1,
  245. 119,118,178,-11,-3,-1,27,177,-3,-1,11,176,-1,150,74,-7,-3,-1,58,163,-1,89,149,-1,42,162,-47,-23,-9,-3,-1,26,
  246. 161,-3,-1,10,104,160,-5,-3,-1,134,73,147,-3,-1,57,88,-1,133,103,-9,-3,-1,41,146,-3,-1,87,117,56,-5,-1,131,-1,
  247. 102,71,-3,-1,116,86,-1,101,115,-11,-3,-1,25,145,-3,-1,9,144,-1,72,132,-7,-5,-1,114,-1,70,100,40,-1,130,24,-41,
  248. -27,-11,-5,-3,-1,55,39,23,-1,113,-1,85,7,-7,-3,-1,112,54,-1,99,69,-3,-1,84,38,-1,98,53,-5,-1,129,-1,8,128,-3,
  249. -1,22,97,-1,6,96,-13,-9,-5,-3,-1,83,68,37,-1,82,5,-1,21,81,-7,-3,-1,52,67,-1,80,36,-3,-1,66,51,20,-19,-11,
  250. -5,-1,65,-1,4,64,-3,-1,35,50,19,-3,-1,49,3,-1,48,34,-3,-1,18,33,-1,2,32,-3,-1,17,1,16,0
  251. };
  252. const int16 huffmanTab15[] =
  253. {
  254. -495,-445,-355,-263,-183,-115,-77,-43,-27,-13,-7,-3,-1,255,239,-1,254,223,-1,238,-1,253,207,-7,-3,-1,252,222,-1,
  255. 237,191,-1,251,-1,206,236,-7,-3,-1,221,175,-1,250,190,-3,-1,235,205,-1,220,159,-15,-7,-3,-1,249,234,-1,189,219,
  256. -3,-1,143,248,-1,204,158,-7,-3,-1,233,127,-1,247,173,-3,-1,218,188,-1,111,-1,174,15,-19,-11,-3,-1,203,246,
  257. -3,-1,142,232,-1,95,157,-3,-1,245,126,-1,231,172,-9,-3,-1,202,187,-3,-1,217,141,79,-3,-1,244,63,-1,243,216,
  258. -33,-17,-9,-3,-1,230,47,-1,242,-1,110,240,-3,-1,31,241,-1,156,201,-7,-3,-1,94,171,-1,186,229,-3,-1,125,215,
  259. -1,78,228,-15,-7,-3,-1,140,200,-1,62,109,-3,-1,214,227,-1,155,185,-7,-3,-1,46,170,-1,226,30,-5,-1,225,-1,14,
  260. 224,-1,93,213,-45,-25,-13,-7,-3,-1,124,199,-1,77,139,-1,212,-1,184,154,-7,-3,-1,169,108,-1,198,61,-1,211,210,
  261. -9,-5,-3,-1,45,13,29,-1,123,183,-5,-1,209,-1,92,208,-1,197,138,-17,-7,-3,-1,168,76,-1,196,107,-5,-1,182,-1,
  262. 153,12,-1,60,195,-9,-3,-1,122,167,-1,166,-1,192,11,-1,194,-1,44,91,-55,-29,-15,-7,-3,-1,181,28,-1,137,152,-3,
  263. -1,193,75,-1,180,106,-5,-3,-1,59,121,179,-3,-1,151,136,-1,43,90,-11,-5,-1,178,-1,165,27,-1,177,-1,176,105,-7,
  264. -3,-1,150,74,-1,164,120,-3,-1,135,58,163,-17,-7,-3,-1,89,149,-1,42,162,-3,-1,26,161,-3,-1,10,160,104,-7,-3,
  265. -1,134,73,-1,148,57,-5,-1,147,-1,119,9,-1,88,133,-53,-29,-13,-7,-3,-1,41,103,-1,118,146,-1,145,-1,25,144,-7,
  266. -3,-1,72,132,-1,87,117,-3,-1,56,131,-1,102,71,-7,-3,-1,40,130,-1,24,129,-7,-3,-1,116,8,-1,128,86,-3,-1,101,
  267. 55,-1,115,70,-17,-7,-3,-1,39,114,-1,100,23,-3,-1,85,113,-3,-1,7,112,54,-7,-3,-1,99,69,-1,84,38,-3,-1,98,22,
  268. -3,-1,6,96,53,-33,-19,-9,-5,-1,97,-1,83,68,-1,37,82,-3,-1,21,81,-3,-1,5,80,52,-7,-3,-1,67,36,-1,66,51,-1,
  269. 65,-1,20,4,-9,-3,-1,35,50,-3,-1,64,3,19,-3,-1,49,48,34,-9,-7,-3,-1,18,33,-1,2,32,17,-3,-1,1,16,0
  270. };
  271. const int16 huffmanTab16[] =
  272. {
  273. -509,-503,-461,-323,-103,-37,-27,-15,-7,-3,-1,239,254,-1,223,253,-3,-1,207,252,-1,191,251,-5,-1,175,-1,250,159,
  274. -3,-1,249,248,143,-7,-3,-1,127,247,-1,111,246,255,-9,-5,-3,-1,95,245,79,-1,244,243,-53,-1,240,-1,63,-29,-19,
  275. -13,-7,-5,-1,206,-1,236,221,222,-1,233,-1,234,217,-1,238,-1,237,235,-3,-1,190,205,-3,-1,220,219,174,-11,-5,
  276. -1,204,-1,173,218,-3,-1,126,172,202,-5,-3,-1,201,125,94,189,242,-93,-5,-3,-1,47,15,31,-1,241,-49,-25,-13,
  277. -5,-1,158,-1,188,203,-3,-1,142,232,-1,157,231,-7,-3,-1,187,141,-1,216,110,-1,230,156,-13,-7,-3,-1,171,186,
  278. -1,229,215,-1,78,-1,228,140,-3,-1,200,62,-1,109,-1,214,155,-19,-11,-5,-3,-1,185,170,225,-1,212,-1,184,169,
  279. -5,-1,123,-1,183,208,227,-7,-3,-1,14,224,-1,93,213,-3,-1,124,199,-1,77,139,-75,-45,-27,-13,-7,-3,-1,154,
  280. 108,-1,198,61,-3,-1,92,197,13,-7,-3,-1,138,168,-1,153,76,-3,-1,182,122,60,-11,-5,-3,-1,91,137,28,-1,192,-1,
  281. 152,121,-1,226,-1,46,30,-15,-7,-3,-1,211,45,-1,210,209,-5,-1,59,-1,151,136,29,-7,-3,-1,196,107,-1,195,167,-1,
  282. 44,-1,194,181,-23,-13,-7,-3,-1,193,12,-1,75,180,-3,-1,106,166,179,-5,-3,-1,90,165,43,-1,178,27,-13,-5,-1,177,
  283. -1,11,176,-3,-1,105,150,-1,74,164,-5,-3,-1,120,135,163,-3,-1,58,89,42,-97,-57,-33,-19,-11,-5,-3,-1,149,104,161,
  284. -3,-1,134,119,148,-5,-3,-1,73,87,103,162,-5,-1,26,-1,10,160,-3,-1,57,147,-1,88,133,-9,-3,-1,41,146,-3,-1,118,
  285. 9,25,-5,-1,145,-1,144,72,-3,-1,132,117,-1,56,131,-21,-11,-5,-3,-1,102,40,130,-3,-1,71,116,24,-3,-1,129,128,-3,
  286. -1,8,86,55,-9,-5,-1,115,-1,101,70,-1,39,114,-5,-3,-1,100,85,7,23,-23,-13,-5,-1,113,-1,112,54,-3,-1,99,69,-1,
  287. 84,38,-3,-1,98,22,-1,97,-1,6,96,-9,-5,-1,83,-1,53,68,-1,37,82,-1,81,-1,21,5,-33,-23,-13,-7,-3,-1,52,67,-1,80,
  288. 36,-3,-1,66,51,20,-5,-1,65,-1,4,64,-1,35,50,-3,-1,19,49,-3,-1,3,48,34,-3,-1,18,33,-1,2,32,-3,-1,17,1,16,0
  289. };
  290. const int16 huffmanTab24[] =
  291. {
  292. -451,-117,-43,-25,-15,-7,-3,-1,239,254,-1,223,253,-3,-1,207,252,-1,191,251,-5,-1,250,-1,175,159,-1,249,248,-9,
  293. -5,-3,-1,143,127,247,-1,111,246,-3,-1,95,245,-1,79,244,-71,-7,-3,-1,63,243,-1,47,242,-5,-1,241,-1,31,240,-25,-9,
  294. -1,15,-3,-1,238,222,-1,237,206,-7,-3,-1,236,221,-1,190,235,-3,-1,205,220,-1,174,234,-15,-7,-3,-1,189,219,-1,204,
  295. 158,-3,-1,233,173,-1,218,188,-7,-3,-1,203,142,-1,232,157,-3,-1,217,126,-1,231,172,255,-235,-143,-77,-45,-25,-15,
  296. -7,-3,-1,202,187,-1,141,216,-5,-3,-1,14,224,13,230,-5,-3,-1,110,156,201,-1,94,186,-9,-5,-1,229,-1,171,125,-1,215,
  297. 228,-3,-1,140,200,-3,-1,78,46,62,-15,-7,-3,-1,109,214,-1,227,155,-3,-1,185,170,-1,226,30,-7,-3,-1,225,93,-1,213,124,
  298. -3,-1,199,77,-1,139,184,-31,-15,-7,-3,-1,212,154,-1,169,108,-3,-1,198,61,-1,211,45,-7,-3,-1,210,29,-1,123,183,-3,-1,
  299. 209,92,-1,197,138,-17,-7,-3,-1,168,153,-1,76,196,-3,-1,107,182,-3,-1,208,12,60,-7,-3,-1,195,122,-1,167,44,-3,-1,194,
  300. 91,-1,181,28,-57,-35,-19,-7,-3,-1,137,152,-1,193,75,-5,-3,-1,192,11,59,-3,-1,176,10,26,-5,-1,180,-1,106,166,-3,-1,121,
  301. 151,-3,-1,160,9,144,-9,-3,-1,179,136,-3,-1,43,90,178,-7,-3,-1,165,27,-1,177,105,-1,150,164,-17,-9,-5,-3,-1,74,120,135,
  302. -1,58,163,-3,-1,89,149,-1,42,162,-7,-3,-1,161,104,-1,134,119,-3,-1,73,148,-1,57,147,-63,-31,-15,-7,-3,-1,88,133,-1,41,
  303. 103,-3,-1,118,146,-1,25,145,-7,-3,-1,72,132,-1,87,117,-3,-1,56,131,-1,102,40,-17,-7,-3,-1,130,24,-1,71,116,-5,-1,129,
  304. -1,8,128,-1,86,101,-7,-5,-1,23,-1,7,112,115,-3,-1,55,39,114,-15,-7,-3,-1,70,100,-1,85,113,-3,-1,54,99,-1,69,84,-7,-3,
  305. -1,38,98,-1,22,97,-5,-3,-1,6,96,53,-1,83,68,-51,-37,-23,-15,-9,-3,-1,37,82,-1,21,-1,5,80,-1,81,-1,52,67,-3,-1,36,66,
  306. -1,51,20,-9,-5,-1,65,-1,4,64,-1,35,50,-1,19,49,-7,-5,-3,-1,3,48,34,18,-1,33,-1,2,32,-3,-1,17,1,-1,16,0
  307. };
  308. struct BitsToTableMap
  309. {
  310. uint32 bits;
  311. const int16* table;
  312. };
  313. const BitsToTableMap huffmanTables1[] =
  314. {
  315. { 0, huffmanTab0 }, { 0, huffmanTab1 }, { 0, huffmanTab2 }, { 0, huffmanTab3 },
  316. { 0, huffmanTab0 }, { 0, huffmanTab5 }, { 0, huffmanTab6 }, { 0, huffmanTab7 },
  317. { 0, huffmanTab8 }, { 0, huffmanTab9 }, { 0, huffmanTab10 }, { 0, huffmanTab11 },
  318. { 0, huffmanTab12 }, { 0, huffmanTab13 }, { 0, huffmanTab0 }, { 0, huffmanTab15 },
  319. { 1, huffmanTab16 }, { 2, huffmanTab16 }, { 3, huffmanTab16 }, { 4, huffmanTab16 },
  320. { 6, huffmanTab16 }, { 8, huffmanTab16 }, { 10, huffmanTab16 }, { 13, huffmanTab16 },
  321. { 4, huffmanTab24 }, { 5, huffmanTab24 }, { 6, huffmanTab24 }, { 7, huffmanTab24 },
  322. { 8, huffmanTab24 }, { 9, huffmanTab24 }, { 11, huffmanTab24 }, { 13, huffmanTab24 }
  323. };
  324. const int16 huffmanTabC0[] = { -29,-21,-13,-7,-3,-1,11,15,-1,13,14,-3,-1,7,5,9,-3,-1,6,3,-1,10,12,-3,-1,2,1,-1,4,8,0 };
  325. const int16 huffmanTabC1[] = { -15,-7,-3,-1,15,14,-1,13,12,-3,-1,11,10,-1,9,8,-7,-3,-1,7,6,-1,5,4,-3,-1,3,2,-1,1,0 };
  326. const BitsToTableMap huffmanTables2[] = { { 0, huffmanTabC0 }, { 0, huffmanTabC1 } };
  327. //==============================================================================
  328. struct VBRTagData
  329. {
  330. bool read (const uint8* data) noexcept
  331. {
  332. flags = 0;
  333. const int layer = (data[1] >> 1) & 3;
  334. if (layer != 1)
  335. return false;
  336. const int type = (data[1] >> 3) & 1;
  337. const int sampleRateIndex = (data[2] >> 2) & 3;
  338. const int mode = (data[3] >> 6) & 3;
  339. static const int bitRates[3][16] =
  340. {
  341. { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, -1 }, // MPEG2
  342. { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, -1 }, // MPEG1
  343. { 0, 8, 16, 24, 32, 40, 48, 56, 64, -1, -1, -1, -1, -1, -1, -1 }, // MPEG 2.5
  344. };
  345. const int bitrate = bitRates[type][((data[2] >> 4) & 15)];
  346. const int sampleRates[3][4] =
  347. {
  348. { 22050, 24000, 16000, -1 }, // MPEG2
  349. { 44100, 48000, 32000, -1 }, // MPEG1
  350. { 11025, 12000, 8000, -1 }, // MPEG2.5
  351. };
  352. if ((data[1] >> 4) == 0xe)
  353. sampleRate = sampleRates[2][sampleRateIndex];
  354. else
  355. sampleRate = sampleRates[type][sampleRateIndex];
  356. data += type != 0 ? (mode != 3 ? (32 + 4) : (17 + 4))
  357. : (mode != 3 ? (17 + 4) : (9 + 4));
  358. if (! isVbrTag (data))
  359. return false;
  360. data += 4;
  361. flags = ByteOrder::bigEndianInt (data);
  362. data += 4;
  363. if (flags & 1)
  364. {
  365. frames = ByteOrder::bigEndianInt (data);
  366. data += 4;
  367. }
  368. if (flags & 2)
  369. {
  370. bytes = ByteOrder::bigEndianInt (data);
  371. data += 4;
  372. }
  373. if (flags & 4)
  374. {
  375. if (toc != nullptr)
  376. for (int i = 0; i < 100; ++i)
  377. toc[i] = data[i];
  378. data += 100;
  379. }
  380. vbrScale = -1;
  381. if (flags & 8)
  382. {
  383. vbrScale = ByteOrder::bigEndianInt (data);
  384. data += 4;
  385. }
  386. headersize = ((type + 1) * 72000 * bitrate) / sampleRate;
  387. return true;
  388. }
  389. uint8 toc[100];
  390. int sampleRate, flags, frames, bytes, vbrScale, headersize;
  391. private:
  392. static bool isVbrTag (const uint8* const d) noexcept
  393. {
  394. return (d[0] == 'X' && d[1] == 'i' && d[2] == 'n' && d[3] == 'g')
  395. || (d[0] == 'I' && d[1] == 'n' && d[2] == 'f' && d[3] == 'o');
  396. }
  397. };
  398. //==============================================================================
  399. struct MP3Frame
  400. {
  401. MP3Frame()
  402. {
  403. zeromem (this, sizeof (MP3Frame));
  404. single = -1;
  405. }
  406. void selectLayer2Table()
  407. {
  408. static const int translate[3][2][16] =
  409. {
  410. { { 0, 2, 2, 2, 2, 2, 2, 0, 0, 0, 1, 1, 1, 1, 1, 0 }, { 0, 2, 2, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 } },
  411. { { 0, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
  412. { { 0, 3, 3, 3, 3, 3, 3, 0, 0, 0, 1, 1, 1, 1, 1, 0 }, { 0, 3, 3, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 } }
  413. };
  414. static const AllocationTable* const tables[] = { allocTable0, allocTable1, allocTable2, allocTable3, allocTable4 };
  415. static const int limits[] = { 27, 30, 8, 12, 30 };
  416. const int index = lsf ? 4 : translate[sampleRateIndex][2 - numChannels][bitrateIndex];
  417. layer2SubBandLimit = limits [index];
  418. allocationTable = tables [index];
  419. }
  420. int getFrequency() const noexcept
  421. {
  422. const int frequencies[] = { 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000 };
  423. return frequencies [sampleRateIndex];
  424. }
  425. void decodeHeader (const uint32 header)
  426. {
  427. jassert (((header >> 10) & 3) != 3);
  428. mpeg25 = (header & (1 << 20)) == 0;
  429. lsf = mpeg25 ? 1 : ((header & (1 << 19)) ? 0 : 1);
  430. layer = 4 - ((header >> 17) & 3);
  431. sampleRateIndex = mpeg25 ? (6 + ((header >> 10) & 3)) : (((header >> 10) & 3) + (lsf * 3));
  432. crc16FollowsHeader = ((header >> 16) & 1) == 0;
  433. bitrateIndex = (header >> 12) & 15;
  434. padding = (header >> 9) & 1;
  435. mode = (header >> 6) & 3;
  436. modeExt = (header >> 4) & 3;
  437. //extension = (header >> 8) & 1;
  438. //copyright = (header >> 3) & 1;
  439. //original = (header >> 2) & 1;
  440. //emphasis = header & 3;
  441. numChannels = (mode == 3) ? 1 : 2;
  442. static const int frameSizes [2][3][16] =
  443. {
  444. { { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448 },
  445. { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 },
  446. { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 } },
  447. { { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256 },
  448. { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160 },
  449. { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160 } }
  450. };
  451. switch (layer)
  452. {
  453. case 1: frameSize = (((frameSizes[lsf][0][bitrateIndex] * 12000) / getFrequency() + padding) * 4) - 4; break;
  454. case 2: frameSize = (frameSizes[lsf][1][bitrateIndex] * 144000) / getFrequency() + (padding - 4); break;
  455. case 3: frameSize = (bitrateIndex == 0) ? 0 : ((frameSizes[lsf][2][bitrateIndex] * 144000) / (getFrequency() << lsf) + (padding - 4)); break;
  456. default: break;
  457. }
  458. }
  459. int layer, frameSize, numChannels, single;
  460. int lsf; // 0 = mpeg-1, 1 = mpeg-2/LSF
  461. bool mpeg25; // true = mpeg-2.5, false = mpeg-1/2
  462. bool crc16FollowsHeader;
  463. int bitrateIndex, sampleRateIndex, padding;
  464. int mode, modeExt, layer2SubBandLimit;
  465. enum { downSampleLimit = 32 };
  466. const AllocationTable* allocationTable;
  467. };
  468. //==============================================================================
  469. struct Constants
  470. {
  471. Constants()
  472. {
  473. cosTables[0] = cos64; cosTables[1] = cos32; cosTables[2] = cos16; cosTables[3] = cos8; cosTables[4] = cos4;
  474. initDecodeTables();
  475. initLayer2Tables();
  476. initLayer3Tables();
  477. }
  478. const uint8* getGroupTable (const int16 d1, const int index) const noexcept
  479. {
  480. switch (d1)
  481. {
  482. case 3: return &group3tab [3 * jmin (index, 3 * 3 * 3)];
  483. case 5: return &group5tab [3 * jmin (index, 5 * 5 * 5)];
  484. case 9: return &group9tab [3 * jmin (index, 9 * 9 * 9)];
  485. default: break;
  486. }
  487. static const uint8 dummy[] = { 0, 0, 0 };
  488. return dummy;
  489. }
  490. float muls[27][64];
  491. float nToThe4Over3[8207];
  492. float antiAliasingCa[8], antiAliasingCs[8];
  493. float win[4][36];
  494. float win1[4][36];
  495. float powToGains[256 + 118 + 4];
  496. int longLimit[9][23];
  497. int shortLimit[9][14];
  498. float tan1_1[16], tan2_1[16], tan1_2[16], tan2_2[16];
  499. float pow1_1[2][16], pow2_1[2][16], pow1_2[2][16], pow2_2[2][16];
  500. int* map[9][3];
  501. int* mapEnd[9][3];
  502. uint32 nLength2[512];
  503. uint32 iLength2[256];
  504. float decodeWin[512 + 32];
  505. float* cosTables[5];
  506. private:
  507. int mapbuf0[9][152];
  508. int mapbuf1[9][156];
  509. int mapbuf2[9][44];
  510. float cos64[16], cos32[8], cos16[4], cos8[2], cos4[1];
  511. uint8 group3tab [32 * 3];
  512. uint8 group5tab [128 * 3];
  513. uint8 group9tab [1024 * 3];
  514. void initDecodeTables()
  515. {
  516. int i, j, scaleval = -1;
  517. float* table = decodeWin;
  518. for (i = 0; i < 5; ++i)
  519. {
  520. int kr = 0x10 >> i;
  521. int divv = 0x40 >> i;
  522. float* costab = cosTables[i];
  523. for (int k = 0; k < kr; ++k)
  524. costab[k] = (float) (1.0 / (2.0 * cos (double_Pi * (k * 2 + 1) / divv)));
  525. }
  526. for (i = 0, j = 0; i < 256; ++i, ++j, table += 32)
  527. {
  528. if (table < decodeWin + 512 + 16)
  529. table[16] = table[0] = (float) (decodeWindow[j] * scaleval);
  530. if (i % 32 == 31)
  531. table -= 1023;
  532. if (i % 64 == 63)
  533. scaleval = -scaleval;
  534. }
  535. for (; i < 512; ++i, --j, table += 32)
  536. {
  537. if (table < decodeWin + 512 + 16)
  538. table[16] = table[0] = (float) (decodeWindow[j] * scaleval);
  539. if (i % 32 == 31) table -= 1023;
  540. if (i % 64 == 63) scaleval = -scaleval;
  541. }
  542. }
  543. void initLayer2Tables()
  544. {
  545. static const uint8 base[3][9] =
  546. {
  547. { 1, 0, 2 },
  548. { 17, 18, 0, 19, 20 },
  549. { 21, 1, 22, 23, 0, 24, 25, 2, 26 }
  550. };
  551. static const int tableLengths[] = { 3, 5, 9 };
  552. static uint8* tables[] = { group3tab, group5tab, group9tab };
  553. for (int i = 0; i < 3; ++i)
  554. {
  555. uint8* table = tables[i];
  556. const int len = tableLengths[i];
  557. for (int j = 0; j < len; ++j)
  558. for (int k = 0; k < len; ++k)
  559. for (int l = 0; l < len; ++l)
  560. {
  561. *table++ = base[i][l];
  562. *table++ = base[i][k];
  563. *table++ = base[i][j];
  564. }
  565. }
  566. for (int k = 0; k < 27; ++k)
  567. {
  568. static const double multipliers[] =
  569. {
  570. 0, -2.0 / 3.0, 2.0 / 3.0, 2.0 / 7.0, 2.0 / 15.0, 2.0 / 31.0, 2.0 / 63.0, 2.0 / 127.0, 2.0 / 255.0,
  571. 2.0 / 511.0, 2.0 / 1023.0, 2.0 / 2047.0, 2.0 / 4095.0, 2.0 / 8191.0, 2.0 / 16383.0, 2.0 / 32767.0, 2.0 / 65535.0,
  572. -4.0 / 5.0, -2.0 / 5.0, 2.0 / 5.0, 4.0 / 5.0, -8.0 / 9.0, -4.0 / 9.0, -2.0 / 9.0, 2.0 / 9.0, 4.0 / 9.0, 8.0 / 9.0
  573. };
  574. float* table = muls[k];
  575. for (int j = 3, i = 0; i < 63; ++i, --j)
  576. *table++ = (float) (multipliers[k] * pow (2.0, j / 3.0));
  577. *table++ = 0;
  578. }
  579. }
  580. void initLayer3Tables()
  581. {
  582. int i, j;
  583. for (i = -256; i < 118 + 4; ++i)
  584. powToGains[i + 256] = (float) pow (2.0, -0.25 * (i + 210));
  585. for (i = 0; i < 8207; ++i)
  586. nToThe4Over3[i] = (float) pow ((double) i, 4.0 / 3.0);
  587. for (i = 0; i < 8; ++i)
  588. {
  589. static double Ci[] = { -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037 };
  590. const double sq = sqrt (1.0 + Ci[i] * Ci[i]);
  591. antiAliasingCs[i] = (float) (1.0 / sq);
  592. antiAliasingCa[i] = (float) (Ci[i] / sq);
  593. }
  594. for (i = 0; i < 18; ++i)
  595. {
  596. win[0][i] = win[1][i] = (float) (0.5 * sin (double_Pi / 72.0 * (2 * i + 1)) / cos (double_Pi * (2 * i + 19) / 72.0));
  597. win[0][i + 18] = win[3][i + 18] = (float) (0.5 * sin (double_Pi / 72.0 * (2 * (i + 18) + 1)) / cos (double_Pi * (2 * (i + 18) + 19) / 72.0));
  598. }
  599. const double piOver72 = double_Pi;
  600. for (i = 0; i < 6; ++i)
  601. {
  602. win[1][i + 18] = (float) (0.5 / cos (piOver72 * (2 * (i + 18) + 19)));
  603. win[3][i + 12] = (float) (0.5 / cos (piOver72 * (2 * (i + 12) + 19)));
  604. win[1][i + 24] = (float) (0.5 * sin (double_Pi / 24.0 * (2 * i + 13)) / cos (piOver72 * (2 * (i + 24) + 19)));
  605. win[1][i + 30] = win[3][i] = 0;
  606. win[3][i + 6] = (float) (0.5 * sin (double_Pi / 24.0 * (2 * i + 1)) / cos (piOver72 * (2 * (i + 6) + 19)));
  607. }
  608. for (i = 0; i < 12; ++i)
  609. win[2][i] = (float) (0.5 * sin (double_Pi / 24.0 * (2 * i + 1)) / cos (double_Pi * (2 * i + 7) / 24.0));
  610. for (j = 0; j < 4; ++j)
  611. {
  612. static const int len[4] = { 36, 36, 12, 36 };
  613. for (i = 0; i < len[j]; i += 2) win1[j][i] = win[j][i];
  614. for (i = 1; i < len[j]; i += 2) win1[j][i] = -win[j][i];
  615. }
  616. const double sqrt2 = 1.41421356237309504880168872420969808;
  617. for (i = 0; i < 16; ++i)
  618. {
  619. const double t = tan (i * double_Pi / 12.0);
  620. tan1_1[i] = (float) (t / (1.0 + t));
  621. tan2_1[i] = (float) (1.0 / (1.0 + t));
  622. tan1_2[i] = (float) (sqrt2 * t / (1.0 + t));
  623. tan2_2[i] = (float) (sqrt2 / (1.0 + t));
  624. for (j = 0; j < 2; ++j)
  625. {
  626. double p1 = 1.0, p2 = 1.0;
  627. if (i > 0)
  628. {
  629. const double base = pow (2.0, -0.25 * (j + 1));
  630. if (i & 1)
  631. p1 = pow (base, (i + 1) * 0.5);
  632. else
  633. p2 = pow (base, i * 0.5);
  634. }
  635. pow1_1[j][i] = (float) p1;
  636. pow2_1[j][i] = (float) p2;
  637. pow1_2[j][i] = (float) (sqrt2 * p1);
  638. pow2_2[j][i] = (float) (sqrt2 * p2);
  639. }
  640. }
  641. for (j = 0; j < 9; ++j)
  642. {
  643. const BandInfoStruct& bi = bandInfo[j];
  644. int cb;
  645. int* mp = map[j][0] = mapbuf0[j];
  646. const int16* bdf = bi.longDiff;
  647. for (i = 0, cb = 0; cb < 8; ++cb, i += *bdf++)
  648. {
  649. *mp++ = (*bdf) >> 1;
  650. *mp++ = i;
  651. *mp++ = 3;
  652. *mp++ = cb;
  653. }
  654. bdf = bi.shortDiff + 3;
  655. for (cb = 3; cb < 13; ++cb)
  656. {
  657. const int l = (*bdf++) >> 1;
  658. for (int lwin = 0; lwin < 3; ++lwin)
  659. {
  660. *mp++ = l;
  661. *mp++ = i + lwin;
  662. *mp++ = lwin;
  663. *mp++ = cb;
  664. }
  665. i += 6 * l;
  666. }
  667. mapEnd[j][0] = mp;
  668. mp = map[j][1] = mapbuf1[j];
  669. bdf = bi.shortDiff;
  670. for (i = 0, cb = 0; cb < 13; ++cb)
  671. {
  672. const int l = (*bdf++) >> 1;
  673. for (int lwin = 0; lwin < 3; ++lwin)
  674. {
  675. *mp++ = l;
  676. *mp++ = i + lwin;
  677. *mp++ = lwin;
  678. *mp++ = cb;
  679. }
  680. i += 6 * l;
  681. }
  682. mapEnd[j][1] = mp;
  683. mp = map[j][2] = mapbuf2[j];
  684. bdf = bi.longDiff;
  685. for (cb = 0; cb < 22; ++cb)
  686. {
  687. *mp++ = (*bdf++) >> 1;
  688. *mp++ = cb;
  689. }
  690. mapEnd[j][2] = mp;
  691. }
  692. for (j = 0; j < 9; ++j)
  693. {
  694. for (i = 0; i < 23; ++i) longLimit[j][i] = jmin (32, (bandInfo[j].longIndex[i] - 1 + 8) / 18 + 1);
  695. for (i = 0; i < 14; ++i) shortLimit[j][i] = jmin (32, (bandInfo[j].shortIndex[i] - 1) / 18 + 1);
  696. }
  697. for (i = 0; i < 5; ++i)
  698. for (j = 0; j < 6; ++j)
  699. for (int k = 0; k < 6; ++k)
  700. {
  701. const int n = k + j * 6 + i * 36;
  702. iLength2[n] = i | (j << 3) | (k << 6) | (3 << 12);
  703. }
  704. for (i = 0; i < 4; ++i)
  705. for (j = 0; j < 4; ++j)
  706. for (int k = 0; k < 4; ++k)
  707. {
  708. const int n = k + j * 4 + i * 16;
  709. iLength2[n + 180] = i | (j << 3) | (k << 6) | (4 << 12);
  710. }
  711. for (i = 0; i < 4; ++i)
  712. for (j = 0; j < 3; ++j)
  713. {
  714. const int n = j + i * 3;
  715. iLength2[n + 244] = i | (j << 3) | (5 << 12);
  716. nLength2[n + 500] = i | (j << 3) | (2 << 12) | (1 << 15);
  717. }
  718. for (i = 0; i < 5; ++i)
  719. for (j = 0; j < 5; ++j)
  720. for (int k = 0; k < 4; ++k)
  721. for (int l = 0; l < 4; ++l)
  722. {
  723. const int n = l + k * 4 + j * 16 + i * 80;
  724. nLength2[n] = i | (j << 3) | (k << 6) | (l << 9) | (0 << 12);
  725. }
  726. for (i = 0; i < 5; ++i)
  727. for (j = 0; j < 5; ++j)
  728. for (int k = 0; k < 4; ++k)
  729. {
  730. const int n = k + j * 4 + i * 20;
  731. nLength2[n + 400] = i | (j << 3) | (k << 6) | (1 << 12);
  732. }
  733. }
  734. };
  735. static const Constants constants;
  736. //==============================================================================
  737. struct Layer3SideInfo
  738. {
  739. struct Info
  740. {
  741. void doAntialias (float xr[32][18]) const noexcept
  742. {
  743. float* xr1 = xr[1];
  744. int sb;
  745. if (blockType == 2)
  746. {
  747. if (mixedBlockFlag == 0)
  748. return;
  749. sb = 1;
  750. }
  751. else
  752. sb = maxb - 1;
  753. for (; sb != 0; --sb, xr1 += 10)
  754. {
  755. const float* cs = constants.antiAliasingCs;
  756. const float* ca = constants.antiAliasingCa;
  757. float* xr2 = xr1;
  758. for (int ss = 7; ss >= 0; --ss)
  759. {
  760. const float bu = *--xr2, bd = *xr1;
  761. *xr2 = (bu * *cs) - (bd * *ca);
  762. *xr1++ = (bd * *cs++) + (bu * *ca++);
  763. }
  764. }
  765. }
  766. void doIStereo (float xrBuffer[2][32][18], const int* const scaleFactors,
  767. const int sampleRate, const bool msStereo, const int lsf) const noexcept
  768. {
  769. float (*xr) [32 * 18] = (float (*) [32 * 18]) xrBuffer;
  770. const BandInfoStruct& bi = bandInfo[sampleRate];
  771. const float* tabl1, *tabl2;
  772. if (lsf != 0)
  773. {
  774. const int p = scaleFactorCompression & 1;
  775. if (msStereo)
  776. {
  777. tabl1 = constants.pow1_2[p];
  778. tabl2 = constants.pow2_2[p];
  779. }
  780. else
  781. {
  782. tabl1 = constants.pow1_1[p];
  783. tabl2 = constants.pow2_1[p];
  784. }
  785. }
  786. else
  787. {
  788. if (msStereo)
  789. {
  790. tabl1 = constants.tan1_2;
  791. tabl2 = constants.tan2_2;
  792. }
  793. else
  794. {
  795. tabl1 = constants.tan1_1;
  796. tabl2 = constants.tan2_1;
  797. }
  798. }
  799. if (blockType == 2)
  800. {
  801. bool doL = mixedBlockFlag != 0;
  802. for (int lwin = 0; lwin < 3; ++lwin)
  803. {
  804. int sfb = maxBand[lwin];
  805. doL = doL && (sfb <= 3);
  806. for (; sfb < 12; ++sfb)
  807. {
  808. const int p = scaleFactors[sfb * 3 + lwin - mixedBlockFlag];
  809. if (p != 7)
  810. {
  811. const float t1 = tabl1[p];
  812. const float t2 = tabl2[p];
  813. int sb = bi.shortDiff[sfb];
  814. int index = sb + lwin;
  815. for (; sb > 0; --sb, index += 3)
  816. {
  817. float v = xr[0][index];
  818. xr[0][index] = v * t1;
  819. xr[1][index] = v * t2;
  820. }
  821. }
  822. }
  823. const int p = scaleFactors[11 * 3 + lwin - mixedBlockFlag];
  824. if (p != 7)
  825. {
  826. const float t1 = tabl1[p];
  827. const float t2 = tabl2[p];
  828. int sb = bi.shortDiff[12];
  829. int index = sb + lwin;
  830. for (; sb > 0; --sb, index += 3)
  831. {
  832. float v = xr[0][index];
  833. xr[0][index] = v * t1;
  834. xr[1][index] = v * t2;
  835. }
  836. }
  837. }
  838. if (doL)
  839. {
  840. int index = bi.longIndex[maxBandl];
  841. for (int sfb = maxBandl; sfb < 8; ++sfb)
  842. {
  843. int sb = bi.longDiff[sfb];
  844. const int p = scaleFactors[sfb];
  845. if (p != 7)
  846. {
  847. const float t1 = tabl1[p];
  848. const float t2 = tabl2[p];
  849. for (; sb > 0; --sb, ++index)
  850. {
  851. float v = xr[0][index];
  852. xr[0][index] = v * t1;
  853. xr[1][index] = v * t2;
  854. }
  855. }
  856. else
  857. index += sb;
  858. }
  859. }
  860. }
  861. else
  862. {
  863. int index = bi.longIndex[maxBandl];
  864. for (int sfb = maxBandl; sfb < 21; ++sfb)
  865. {
  866. int sb = bi.longDiff[sfb];
  867. const int p = scaleFactors[sfb];
  868. if (p != 7)
  869. {
  870. const float t1 = tabl1[p];
  871. const float t2 = tabl2[p];
  872. for (; sb > 0; --sb, ++index)
  873. {
  874. const float v = xr[0][index];
  875. xr[0][index] = v * t1;
  876. xr[1][index] = v * t2;
  877. }
  878. }
  879. else
  880. index += sb;
  881. }
  882. const int p = scaleFactors[20];
  883. if (p != 7)
  884. {
  885. const float t1 = tabl1[p], t2 = tabl2[p];
  886. for (int sb = bi.longDiff[21]; sb > 0; --sb, ++index)
  887. {
  888. const float v = xr[0][index];
  889. xr[0][index] = v * t1;
  890. xr[1][index] = v * t2;
  891. }
  892. }
  893. }
  894. }
  895. int scfsi;
  896. uint32 part2_3Length, bigValues;
  897. uint32 scaleFactorCompression, blockType, mixedBlockFlag;
  898. uint32 tableSelect[3];
  899. uint32 maxBand[3];
  900. uint32 maxBandl, maxb, region1Start, region2Start;
  901. uint32 preflag, scaleFactorScale, count1TableSelect;
  902. const float* fullGain[3];
  903. const float* pow2gain;
  904. };
  905. struct InfoPair { Info gr[2]; };
  906. InfoPair ch[2];
  907. uint32 mainDataStart, privateBits;
  908. };
  909. //==============================================================================
  910. namespace DCT
  911. {
  912. enum { SBLIMIT = 32 };
  913. static const float cos6_1 = 0.866025388f;
  914. static const float cos6_2 = 0.5f;
  915. static const float cos9[] = { 1.0f, 0.98480773f, 0.939692616f, 0.866025388f, 0.766044438f, 0.642787635f, 0.5f, 0.342020154f, 0.173648179f };
  916. static const float cos36[] = { 0.501909912f, 0.517638087f, 0.551688969f, 0.610387266f, 0.707106769f, 0.871723413f, 1.18310082f, 1.93185163f, 5.73685646f };
  917. static const float cos12[] = { 0.517638087f, 0.707106769f, 1.93185163f };
  918. inline void dct36_0 (const int v, float* const ts, float* const out1, float* const out2,
  919. const float* const wintab, float sum0, const float sum1) noexcept
  920. {
  921. const float tmp = sum0 + sum1;
  922. out2[9 + v] = tmp * wintab[27 + v];
  923. out2[8 - v] = tmp * wintab[26 - v];
  924. sum0 -= sum1;
  925. ts[SBLIMIT * (8 - v)] = out1[8 - v] + sum0 * wintab[8 - v];
  926. ts[SBLIMIT * (9 + v)] = out1[9 + v] + sum0 * wintab[9 + v];
  927. }
  928. inline void dct36_1 (const int v, float* const ts, float* const out1, float* const out2, const float* const wintab,
  929. const float tmp1a, const float tmp1b, const float tmp2a, const float tmp2b) noexcept
  930. {
  931. dct36_0 (v, ts, out1, out2, wintab, tmp1a + tmp2a, (tmp1b + tmp2b) * cos36[v]);
  932. }
  933. inline void dct36_2 (const int v, float* const ts, float* const out1, float* const out2, const float* const wintab,
  934. const float tmp1a, const float tmp1b, const float tmp2a, const float tmp2b) noexcept
  935. {
  936. dct36_0 (v, ts, out1, out2, wintab, tmp2a - tmp1a, (tmp2b - tmp1b) * cos36[v]);
  937. }
  938. void dct36 (float* const in, float* const out1, float* const out2, const float* const wintab, float* const ts) noexcept
  939. {
  940. in[17] += in[16]; in[16] += in[15]; in[15] += in[14]; in[14] += in[13]; in[13] += in[12];
  941. in[12] += in[11]; in[11] += in[10]; in[10] += in[9]; in[9] += in[8]; in[8] += in[7];
  942. in[7] += in[6]; in[6] += in[5]; in[5] += in[4]; in[4] += in[3]; in[3] += in[2];
  943. in[2] += in[1]; in[1] += in[0]; in[17] += in[15]; in[15] += in[13]; in[13] += in[11];
  944. in[11] += in[9]; in[9] += in[7]; in[7] += in[5]; in[5] += in[3]; in[3] += in[1];
  945. const float ta33 = in[6] * cos9[3];
  946. const float ta66 = in[12] * cos9[6];
  947. const float tb33 = in[7] * cos9[3];
  948. const float tb66 = in[13] * cos9[6];
  949. {
  950. const float tmp1a = in[2] * cos9[1] + ta33 + in[10] * cos9[5] + in[14] * cos9[7];
  951. const float tmp1b = in[3] * cos9[1] + tb33 + in[11] * cos9[5] + in[15] * cos9[7];
  952. const float tmp2a = in[0] + in[4] * cos9[2] + in[8] * cos9[4] + ta66 + in[16] * cos9[8];
  953. const float tmp2b = in[1] + in[5] * cos9[2] + in[9] * cos9[4] + tb66 + in[17] * cos9[8];
  954. dct36_1 (0, ts, out1, out2, wintab, tmp1a, tmp1b, tmp2a, tmp2b);
  955. dct36_2 (8, ts, out1, out2, wintab, tmp1a, tmp1b, tmp2a, tmp2b);
  956. }
  957. {
  958. const float tmp1a = (in[2] - in[10] - in[14]) * cos9[3];
  959. const float tmp1b = (in[3] - in[11] - in[15]) * cos9[3];
  960. const float tmp2a = (in[4] - in[8] - in[16]) * cos9[6] - in[12] + in[0];
  961. const float tmp2b = (in[5] - in[9] - in[17]) * cos9[6] - in[13] + in[1];
  962. dct36_1 (1, ts, out1, out2, wintab, tmp1a, tmp1b, tmp2a, tmp2b);
  963. dct36_2 (7, ts, out1, out2, wintab, tmp1a, tmp1b, tmp2a, tmp2b);
  964. }
  965. {
  966. const float tmp1a = in[2] * cos9[5] - ta33 - in[10] * cos9[7] + in[14] * cos9[1];
  967. const float tmp1b = in[3] * cos9[5] - tb33 - in[11] * cos9[7] + in[15] * cos9[1];
  968. const float tmp2a = in[0] - in[4] * cos9[8] - in[8] * cos9[2] + ta66 + in[16] * cos9[4];
  969. const float tmp2b = in[1] - in[5] * cos9[8] - in[9] * cos9[2] + tb66 + in[17] * cos9[4];
  970. dct36_1 (2, ts, out1, out2, wintab, tmp1a, tmp1b, tmp2a, tmp2b);
  971. dct36_2 (6, ts, out1, out2, wintab, tmp1a, tmp1b, tmp2a, tmp2b);
  972. }
  973. {
  974. const float tmp1a = in[2] * cos9[7] - ta33 + in[10] * cos9[1] - in[14] * cos9[5];
  975. const float tmp1b = in[3] * cos9[7] - tb33 + in[11] * cos9[1] - in[15] * cos9[5];
  976. const float tmp2a = in[0] - in[4] * cos9[4] + in[8] * cos9[8] + ta66 - in[16] * cos9[2];
  977. const float tmp2b = in[1] - in[5] * cos9[4] + in[9] * cos9[8] + tb66 - in[17] * cos9[2];
  978. dct36_1 (3, ts, out1, out2, wintab, tmp1a, tmp1b, tmp2a, tmp2b);
  979. dct36_2 (5, ts, out1, out2, wintab, tmp1a, tmp1b, tmp2a, tmp2b);
  980. }
  981. const float sum0 = in[0] - in[4] + in[8] - in[12] + in[16];
  982. const float sum1 = (in[1] - in[5] + in[9] - in[13] + in[17]) * cos36[4];
  983. dct36_0 (4, ts, out1, out2, wintab, sum0, sum1);
  984. }
  985. struct DCT12Inputs
  986. {
  987. float in0, in1, in2, in3, in4, in5;
  988. inline DCT12Inputs (const float* const in) noexcept
  989. {
  990. in5 = in[5*3] + (in4 = in[4*3]);
  991. in4 += (in3 = in[3*3]);
  992. in3 += (in2 = in[2*3]);
  993. in2 += (in1 = in[1*3]);
  994. in1 += (in0 = in[0*3]);
  995. in5 += in3; in3 += in1;
  996. in2 *= cos6_1;
  997. in3 *= cos6_1;
  998. }
  999. inline void process() noexcept
  1000. {
  1001. in0 += in4 * cos6_2;
  1002. in4 = in0 + in2; in0 -= in2;
  1003. in1 += in5 * cos6_2;
  1004. in5 = (in1 + in3) * cos12[0];
  1005. in1 = (in1 - in3) * cos12[2];
  1006. in3 = in4 + in5; in4 -= in5;
  1007. in2 = in0 + in1; in0 -= in1;
  1008. }
  1009. };
  1010. void dct12 (const float* in, float* const out1, float* const out2, const float* wi, float* ts) noexcept
  1011. {
  1012. {
  1013. ts[0] = out1[0];
  1014. ts[SBLIMIT * 1] = out1[1];
  1015. ts[SBLIMIT * 2] = out1[2];
  1016. ts[SBLIMIT * 3] = out1[3];
  1017. ts[SBLIMIT * 4] = out1[4];
  1018. ts[SBLIMIT * 5] = out1[5];
  1019. DCT12Inputs inputs (in);
  1020. {
  1021. float tmp1 = (inputs.in0 - inputs.in4);
  1022. const float tmp2 = (inputs.in1 - inputs.in5) * cos12[1];
  1023. const float tmp0 = tmp1 + tmp2;
  1024. tmp1 -= tmp2;
  1025. ts[16 * SBLIMIT] = out1[16] + tmp0 * wi[10];
  1026. ts[13 * SBLIMIT] = out1[13] + tmp0 * wi[7];
  1027. ts[7 * SBLIMIT] = out1[7] + tmp1 * wi[1];
  1028. ts[10 * SBLIMIT] = out1[10] + tmp1 * wi[4];
  1029. }
  1030. inputs.process();
  1031. ts[17 * SBLIMIT] = out1[17] + inputs.in2 * wi[11];
  1032. ts[12 * SBLIMIT] = out1[12] + inputs.in2 * wi[6];
  1033. ts[14 * SBLIMIT] = out1[14] + inputs.in3 * wi[8];
  1034. ts[15 * SBLIMIT] = out1[15] + inputs.in3 * wi[9];
  1035. ts[6 * SBLIMIT] = out1[6] + inputs.in0 * wi[0];
  1036. ts[11 * SBLIMIT] = out1[11] + inputs.in0 * wi[5];
  1037. ts[8 * SBLIMIT] = out1[8] + inputs.in4 * wi[2];
  1038. ts[9 * SBLIMIT] = out1[9] + inputs.in4 * wi[3];
  1039. }
  1040. {
  1041. DCT12Inputs inputs (++in);
  1042. float tmp1 = (inputs.in0 - inputs.in4);
  1043. const float tmp2 = (inputs.in1 - inputs.in5) * cos12[1];
  1044. const float tmp0 = tmp1 + tmp2;
  1045. tmp1 -= tmp2;
  1046. out2[4] = tmp0 * wi[10];
  1047. out2[1] = tmp0 * wi[7];
  1048. ts[13 * SBLIMIT] += tmp1 * wi[1];
  1049. ts[16 * SBLIMIT] += tmp1 * wi[4];
  1050. inputs.process();
  1051. out2[5] = inputs.in2 * wi[11];
  1052. out2[0] = inputs.in2 * wi[6];
  1053. out2[2] = inputs.in3 * wi[8];
  1054. out2[3] = inputs.in3 * wi[9];
  1055. ts[12 * SBLIMIT] += inputs.in0 * wi[0];
  1056. ts[17 * SBLIMIT] += inputs.in0 * wi[5];
  1057. ts[14 * SBLIMIT] += inputs.in4 * wi[2];
  1058. ts[15 * SBLIMIT] += inputs.in4 * wi[5 - 2];
  1059. }
  1060. {
  1061. DCT12Inputs inputs (++in);
  1062. out2[12] = out2[13] = out2[14] = out2[15] = out2[16] = out2[17] = 0;
  1063. float tmp1 = (inputs.in0 - inputs.in4);
  1064. const float tmp2 = (inputs.in1 - inputs.in5) * cos12[1];
  1065. const float tmp0 = tmp1 + tmp2;
  1066. tmp1 -= tmp2;
  1067. out2[10] = tmp0 * wi[10];
  1068. out2[7] = tmp0 * wi[7];
  1069. out2[1] += tmp1 * wi[1];
  1070. out2[4] += tmp1 * wi[4];
  1071. inputs.process();
  1072. out2[11] = inputs.in2 * wi[11];
  1073. out2[6] = inputs.in2 * wi[6];
  1074. out2[8] = inputs.in3 * wi[8];
  1075. out2[9] = inputs.in3 * wi[9];
  1076. out2[0] += inputs.in0 * wi[0];
  1077. out2[5] += inputs.in0 * wi[5];
  1078. out2[2] += inputs.in4 * wi[2];
  1079. out2[3] += inputs.in4 * wi[3];
  1080. }
  1081. }
  1082. void dct64 (float* const out0, float* const out1, float* const b1, float* const b2, const float* const samples) noexcept
  1083. {
  1084. {
  1085. const float* const costab = constants.cosTables[0];
  1086. b1[0x00] = samples[0x00] + samples[0x1F]; b1[0x1F] = (samples[0x00] - samples[0x1F]) * costab[0x0];
  1087. b1[0x01] = samples[0x01] + samples[0x1E]; b1[0x1E] = (samples[0x01] - samples[0x1E]) * costab[0x1];
  1088. b1[0x02] = samples[0x02] + samples[0x1D]; b1[0x1D] = (samples[0x02] - samples[0x1D]) * costab[0x2];
  1089. b1[0x03] = samples[0x03] + samples[0x1C]; b1[0x1C] = (samples[0x03] - samples[0x1C]) * costab[0x3];
  1090. b1[0x04] = samples[0x04] + samples[0x1B]; b1[0x1B] = (samples[0x04] - samples[0x1B]) * costab[0x4];
  1091. b1[0x05] = samples[0x05] + samples[0x1A]; b1[0x1A] = (samples[0x05] - samples[0x1A]) * costab[0x5];
  1092. b1[0x06] = samples[0x06] + samples[0x19]; b1[0x19] = (samples[0x06] - samples[0x19]) * costab[0x6];
  1093. b1[0x07] = samples[0x07] + samples[0x18]; b1[0x18] = (samples[0x07] - samples[0x18]) * costab[0x7];
  1094. b1[0x08] = samples[0x08] + samples[0x17]; b1[0x17] = (samples[0x08] - samples[0x17]) * costab[0x8];
  1095. b1[0x09] = samples[0x09] + samples[0x16]; b1[0x16] = (samples[0x09] - samples[0x16]) * costab[0x9];
  1096. b1[0x0A] = samples[0x0A] + samples[0x15]; b1[0x15] = (samples[0x0A] - samples[0x15]) * costab[0xA];
  1097. b1[0x0B] = samples[0x0B] + samples[0x14]; b1[0x14] = (samples[0x0B] - samples[0x14]) * costab[0xB];
  1098. b1[0x0C] = samples[0x0C] + samples[0x13]; b1[0x13] = (samples[0x0C] - samples[0x13]) * costab[0xC];
  1099. b1[0x0D] = samples[0x0D] + samples[0x12]; b1[0x12] = (samples[0x0D] - samples[0x12]) * costab[0xD];
  1100. b1[0x0E] = samples[0x0E] + samples[0x11]; b1[0x11] = (samples[0x0E] - samples[0x11]) * costab[0xE];
  1101. b1[0x0F] = samples[0x0F] + samples[0x10]; b1[0x10] = (samples[0x0F] - samples[0x10]) * costab[0xF];
  1102. }
  1103. {
  1104. const float* const costab = constants.cosTables[1];
  1105. b2[0x00] = b1[0x00] + b1[0x0F]; b2[0x0F] = (b1[0x00] - b1[0x0F]) * costab[0];
  1106. b2[0x01] = b1[0x01] + b1[0x0E]; b2[0x0E] = (b1[0x01] - b1[0x0E]) * costab[1];
  1107. b2[0x02] = b1[0x02] + b1[0x0D]; b2[0x0D] = (b1[0x02] - b1[0x0D]) * costab[2];
  1108. b2[0x03] = b1[0x03] + b1[0x0C]; b2[0x0C] = (b1[0x03] - b1[0x0C]) * costab[3];
  1109. b2[0x04] = b1[0x04] + b1[0x0B]; b2[0x0B] = (b1[0x04] - b1[0x0B]) * costab[4];
  1110. b2[0x05] = b1[0x05] + b1[0x0A]; b2[0x0A] = (b1[0x05] - b1[0x0A]) * costab[5];
  1111. b2[0x06] = b1[0x06] + b1[0x09]; b2[0x09] = (b1[0x06] - b1[0x09]) * costab[6];
  1112. b2[0x07] = b1[0x07] + b1[0x08]; b2[0x08] = (b1[0x07] - b1[0x08]) * costab[7];
  1113. b2[0x10] = b1[0x10] + b1[0x1F]; b2[0x1F] = (b1[0x1F] - b1[0x10]) * costab[0];
  1114. b2[0x11] = b1[0x11] + b1[0x1E]; b2[0x1E] = (b1[0x1E] - b1[0x11]) * costab[1];
  1115. b2[0x12] = b1[0x12] + b1[0x1D]; b2[0x1D] = (b1[0x1D] - b1[0x12]) * costab[2];
  1116. b2[0x13] = b1[0x13] + b1[0x1C]; b2[0x1C] = (b1[0x1C] - b1[0x13]) * costab[3];
  1117. b2[0x14] = b1[0x14] + b1[0x1B]; b2[0x1B] = (b1[0x1B] - b1[0x14]) * costab[4];
  1118. b2[0x15] = b1[0x15] + b1[0x1A]; b2[0x1A] = (b1[0x1A] - b1[0x15]) * costab[5];
  1119. b2[0x16] = b1[0x16] + b1[0x19]; b2[0x19] = (b1[0x19] - b1[0x16]) * costab[6];
  1120. b2[0x17] = b1[0x17] + b1[0x18]; b2[0x18] = (b1[0x18] - b1[0x17]) * costab[7];
  1121. }
  1122. {
  1123. const float* const costab = constants.cosTables[2];
  1124. b1[0x00] = b2[0x00] + b2[0x07]; b1[0x07] = (b2[0x00] - b2[0x07]) * costab[0];
  1125. b1[0x01] = b2[0x01] + b2[0x06]; b1[0x06] = (b2[0x01] - b2[0x06]) * costab[1];
  1126. b1[0x02] = b2[0x02] + b2[0x05]; b1[0x05] = (b2[0x02] - b2[0x05]) * costab[2];
  1127. b1[0x03] = b2[0x03] + b2[0x04]; b1[0x04] = (b2[0x03] - b2[0x04]) * costab[3];
  1128. b1[0x08] = b2[0x08] + b2[0x0F]; b1[0x0F] = (b2[0x0F] - b2[0x08]) * costab[0];
  1129. b1[0x09] = b2[0x09] + b2[0x0E]; b1[0x0E] = (b2[0x0E] - b2[0x09]) * costab[1];
  1130. b1[0x0A] = b2[0x0A] + b2[0x0D]; b1[0x0D] = (b2[0x0D] - b2[0x0A]) * costab[2];
  1131. b1[0x0B] = b2[0x0B] + b2[0x0C]; b1[0x0C] = (b2[0x0C] - b2[0x0B]) * costab[3];
  1132. b1[0x10] = b2[0x10] + b2[0x17]; b1[0x17] = (b2[0x10] - b2[0x17]) * costab[0];
  1133. b1[0x11] = b2[0x11] + b2[0x16]; b1[0x16] = (b2[0x11] - b2[0x16]) * costab[1];
  1134. b1[0x12] = b2[0x12] + b2[0x15]; b1[0x15] = (b2[0x12] - b2[0x15]) * costab[2];
  1135. b1[0x13] = b2[0x13] + b2[0x14]; b1[0x14] = (b2[0x13] - b2[0x14]) * costab[3];
  1136. b1[0x18] = b2[0x18] + b2[0x1F]; b1[0x1F] = (b2[0x1F] - b2[0x18]) * costab[0];
  1137. b1[0x19] = b2[0x19] + b2[0x1E]; b1[0x1E] = (b2[0x1E] - b2[0x19]) * costab[1];
  1138. b1[0x1A] = b2[0x1A] + b2[0x1D]; b1[0x1D] = (b2[0x1D] - b2[0x1A]) * costab[2];
  1139. b1[0x1B] = b2[0x1B] + b2[0x1C]; b1[0x1C] = (b2[0x1C] - b2[0x1B]) * costab[3];
  1140. }
  1141. {
  1142. const float cos0 = constants.cosTables[3][0];
  1143. const float cos1 = constants.cosTables[3][1];
  1144. b2[0x00] = b1[0x00] + b1[0x03]; b2[0x03] = (b1[0x00] - b1[0x03]) * cos0;
  1145. b2[0x01] = b1[0x01] + b1[0x02]; b2[0x02] = (b1[0x01] - b1[0x02]) * cos1;
  1146. b2[0x04] = b1[0x04] + b1[0x07]; b2[0x07] = (b1[0x07] - b1[0x04]) * cos0;
  1147. b2[0x05] = b1[0x05] + b1[0x06]; b2[0x06] = (b1[0x06] - b1[0x05]) * cos1;
  1148. b2[0x08] = b1[0x08] + b1[0x0B]; b2[0x0B] = (b1[0x08] - b1[0x0B]) * cos0;
  1149. b2[0x09] = b1[0x09] + b1[0x0A]; b2[0x0A] = (b1[0x09] - b1[0x0A]) * cos1;
  1150. b2[0x0C] = b1[0x0C] + b1[0x0F]; b2[0x0F] = (b1[0x0F] - b1[0x0C]) * cos0;
  1151. b2[0x0D] = b1[0x0D] + b1[0x0E]; b2[0x0E] = (b1[0x0E] - b1[0x0D]) * cos1;
  1152. b2[0x10] = b1[0x10] + b1[0x13]; b2[0x13] = (b1[0x10] - b1[0x13]) * cos0;
  1153. b2[0x11] = b1[0x11] + b1[0x12]; b2[0x12] = (b1[0x11] - b1[0x12]) * cos1;
  1154. b2[0x14] = b1[0x14] + b1[0x17]; b2[0x17] = (b1[0x17] - b1[0x14]) * cos0;
  1155. b2[0x15] = b1[0x15] + b1[0x16]; b2[0x16] = (b1[0x16] - b1[0x15]) * cos1;
  1156. b2[0x18] = b1[0x18] + b1[0x1B]; b2[0x1B] = (b1[0x18] - b1[0x1B]) * cos0;
  1157. b2[0x19] = b1[0x19] + b1[0x1A]; b2[0x1A] = (b1[0x19] - b1[0x1A]) * cos1;
  1158. b2[0x1C] = b1[0x1C] + b1[0x1F]; b2[0x1F] = (b1[0x1F] - b1[0x1C]) * cos0;
  1159. b2[0x1D] = b1[0x1D] + b1[0x1E]; b2[0x1E] = (b1[0x1E] - b1[0x1D]) * cos1;
  1160. }
  1161. {
  1162. const float cos0 = constants.cosTables[4][0];
  1163. b1[0x00] = b2[0x00] + b2[0x01]; b1[0x01] = (b2[0x00] - b2[0x01]) * cos0;
  1164. b1[0x02] = b2[0x02] + b2[0x03]; b1[0x03] = (b2[0x03] - b2[0x02]) * cos0; b1[0x02] += b1[0x03];
  1165. b1[0x04] = b2[0x04] + b2[0x05]; b1[0x05] = (b2[0x04] - b2[0x05]) * cos0;
  1166. b1[0x06] = b2[0x06] + b2[0x07]; b1[0x07] = (b2[0x07] - b2[0x06]) * cos0;
  1167. b1[0x06] += b1[0x07]; b1[0x04] += b1[0x06]; b1[0x06] += b1[0x05]; b1[0x05] += b1[0x07];
  1168. b1[0x08] = b2[0x08] + b2[0x09]; b1[0x09] = (b2[0x08] - b2[0x09]) * cos0;
  1169. b1[0x0A] = b2[0x0A] + b2[0x0B]; b1[0x0B] = (b2[0x0B] - b2[0x0A]) * cos0; b1[0x0A] += b1[0x0B];
  1170. b1[0x0C] = b2[0x0C] + b2[0x0D]; b1[0x0D] = (b2[0x0C] - b2[0x0D]) * cos0;
  1171. b1[0x0E] = b2[0x0E] + b2[0x0F]; b1[0x0F] = (b2[0x0F] - b2[0x0E]) * cos0;
  1172. b1[0x0E] += b1[0x0F]; b1[0x0C] += b1[0x0E]; b1[0x0E] += b1[0x0D]; b1[0x0D] += b1[0x0F];
  1173. b1[0x10] = b2[0x10] + b2[0x11]; b1[0x11] = (b2[0x10] - b2[0x11]) * cos0;
  1174. b1[0x12] = b2[0x12] + b2[0x13]; b1[0x13] = (b2[0x13] - b2[0x12]) * cos0; b1[0x12] += b1[0x13];
  1175. b1[0x14] = b2[0x14] + b2[0x15]; b1[0x15] = (b2[0x14] - b2[0x15]) * cos0;
  1176. b1[0x16] = b2[0x16] + b2[0x17]; b1[0x17] = (b2[0x17] - b2[0x16]) * cos0;
  1177. b1[0x16] += b1[0x17]; b1[0x14] += b1[0x16]; b1[0x16] += b1[0x15]; b1[0x15] += b1[0x17];
  1178. b1[0x18] = b2[0x18] + b2[0x19]; b1[0x19] = (b2[0x18] - b2[0x19]) * cos0;
  1179. b1[0x1A] = b2[0x1A] + b2[0x1B]; b1[0x1B] = (b2[0x1B] - b2[0x1A]) * cos0; b1[0x1A] += b1[0x1B];
  1180. b1[0x1C] = b2[0x1C] + b2[0x1D]; b1[0x1D] = (b2[0x1C] - b2[0x1D]) * cos0;
  1181. b1[0x1E] = b2[0x1E] + b2[0x1F]; b1[0x1F] = (b2[0x1F] - b2[0x1E]) * cos0;
  1182. b1[0x1E] += b1[0x1F]; b1[0x1C] += b1[0x1E]; b1[0x1E] += b1[0x1D]; b1[0x1D] += b1[0x1F];
  1183. }
  1184. out0[0x10 * 16] = b1[0x00]; out0[0x10 * 12] = b1[0x04]; out0[0x10 * 8] = b1[0x02]; out0[0x10 * 4] = b1[0x06];
  1185. out0[0] = b1[0x01]; out1[0] = b1[0x01]; out1[0x10 * 4] = b1[0x05]; out1[0x10 * 8] = b1[0x03];
  1186. out1[0x10 * 12] = b1[0x07];
  1187. b1[0x08] += b1[0x0C]; out0[0x10 * 14] = b1[0x08]; b1[0x0C] += b1[0x0a]; out0[0x10 * 10] = b1[0x0C];
  1188. b1[0x0A] += b1[0x0E]; out0[0x10 * 6] = b1[0x0A]; b1[0x0E] += b1[0x09]; out0[0x10 * 2] = b1[0x0E];
  1189. b1[0x09] += b1[0x0D]; out1[0x10 * 2] = b1[0x09]; b1[0x0D] += b1[0x0B]; out1[0x10 * 6] = b1[0x0D];
  1190. b1[0x0B] += b1[0x0F]; out1[0x10 * 10] = b1[0x0B]; out1[0x10 * 14] = b1[0x0F];
  1191. b1[0x18] += b1[0x1C]; out0[0x10 * 15] = b1[0x10] + b1[0x18]; out0[0x10 * 13] = b1[0x18] + b1[0x14];
  1192. b1[0x1C] += b1[0x1a]; out0[0x10 * 11] = b1[0x14] + b1[0x1C]; out0[0x10 * 9] = b1[0x1C] + b1[0x12];
  1193. b1[0x1A] += b1[0x1E]; out0[0x10 * 7] = b1[0x12] + b1[0x1A]; out0[0x10 * 5] = b1[0x1A] + b1[0x16];
  1194. b1[0x1E] += b1[0x19]; out0[0x10 * 3] = b1[0x16] + b1[0x1E]; out0[0x10 * 1] = b1[0x1E] + b1[0x11];
  1195. b1[0x19] += b1[0x1D]; out1[0x10 * 1] = b1[0x11] + b1[0x19]; out1[0x10 * 3] = b1[0x19] + b1[0x15];
  1196. b1[0x1D] += b1[0x1B]; out1[0x10 * 5] = b1[0x15] + b1[0x1D]; out1[0x10 * 7] = b1[0x1D] + b1[0x13];
  1197. b1[0x1B] += b1[0x1F]; out1[0x10 * 9] = b1[0x13] + b1[0x1B]; out1[0x10 * 11] = b1[0x1B] + b1[0x17];
  1198. out1[0x10 * 13] = b1[0x17] + b1[0x1F]; out1[0x10 * 15] = b1[0x1F];
  1199. }
  1200. void dct64 (float* const a, float* const b, const float* const c) noexcept
  1201. {
  1202. float temp[64];
  1203. dct64 (a, b, temp, temp + 32, c);
  1204. }
  1205. }
  1206. //==============================================================================
  1207. struct MP3Stream
  1208. {
  1209. MP3Stream (InputStream& source)
  1210. : stream (source, 8192),
  1211. numFrames (0), currentFrameIndex (0), vbrHeaderFound (false)
  1212. {
  1213. reset();
  1214. }
  1215. int decodeNextBlock (float* const out0, float* const out1, int& done)
  1216. {
  1217. if (! headerParsed)
  1218. {
  1219. int nextFrameOffset = scanForNextFrameHeader (false);
  1220. if (lastFrameSize == -1 || needToSyncBitStream)
  1221. {
  1222. needToSyncBitStream = false;
  1223. readVBRHeader();
  1224. if (vbrHeaderFound)
  1225. return 1;
  1226. }
  1227. if (nextFrameOffset < 0)
  1228. return -1;
  1229. if (nextFrameOffset > 0)
  1230. {
  1231. int size;
  1232. wasFreeFormat = false;
  1233. needToSyncBitStream = true;
  1234. size = (int) (bufferPointer - (bufferSpace[bufferSpaceIndex] + 512));
  1235. if (size > 2880)
  1236. {
  1237. size = 0;
  1238. bufferPointer = bufferSpace[bufferSpaceIndex] + 512;
  1239. }
  1240. const int toSkip = (size + nextFrameOffset) - 2880;
  1241. if (toSkip > 0)
  1242. {
  1243. stream.skipNextBytes (toSkip);
  1244. nextFrameOffset -= toSkip;
  1245. }
  1246. stream.read (bufferPointer, nextFrameOffset);
  1247. lastFrameSize += nextFrameOffset;
  1248. }
  1249. frame.decodeHeader (stream.readIntBigEndian());
  1250. headerParsed = true;
  1251. frameSize = frame.frameSize;
  1252. isFreeFormat = (frameSize == 0);
  1253. sideInfoSize = frame.lsf != 0 ? ((frame.numChannels == 1) ? 9 : 17)
  1254. : ((frame.numChannels == 1) ? 17 : 32);
  1255. if (frame.crc16FollowsHeader)
  1256. sideInfoSize += 2;
  1257. bufferSpaceIndex = 1 - bufferSpaceIndex;
  1258. bufferPointer = bufferSpace[bufferSpaceIndex] + 512;
  1259. bitIndex = 0;
  1260. if (lastFrameSize == -1)
  1261. return 1;
  1262. }
  1263. if (! sideParsed)
  1264. {
  1265. if (frame.layer == 3)
  1266. {
  1267. stream.read (bufferPointer, sideInfoSize);
  1268. if (frame.crc16FollowsHeader)
  1269. getBits (16);
  1270. const int bits = jmax (0, decodeLayer3SideInfo());
  1271. dataSize = (bits + 7) / 8;
  1272. if (! isFreeFormat)
  1273. dataSize = jmin (dataSize, frame.frameSize - sideInfoSize);
  1274. }
  1275. else
  1276. {
  1277. dataSize = frame.frameSize;
  1278. sideInfoSize = 0;
  1279. }
  1280. sideParsed = true;
  1281. }
  1282. int result = 1;
  1283. if (! dataParsed)
  1284. {
  1285. stream.read (bufferPointer, dataSize);
  1286. if (out0 != nullptr)
  1287. {
  1288. if (frame.layer < 3 && frame.crc16FollowsHeader)
  1289. getBits (16);
  1290. switch (frame.layer)
  1291. {
  1292. case 1: decodeLayer1Frame (out0, out1, done); break;
  1293. case 2: decodeLayer2Frame (out0, out1, done); break;
  1294. case 3: decodeLayer3Frame (out0, out1, done); break;
  1295. default: break;
  1296. }
  1297. }
  1298. bufferPointer = bufferSpace[bufferSpaceIndex] + 512 + sideInfoSize + dataSize;
  1299. dataParsed = true;
  1300. result = 0;
  1301. }
  1302. if (isFreeFormat)
  1303. {
  1304. if (wasFreeFormat)
  1305. {
  1306. frameSize = lastFrameSizeNoPadding + frame.padding;
  1307. }
  1308. else
  1309. {
  1310. const int nextFrameOffset = scanForNextFrameHeader (true);
  1311. if (nextFrameOffset < 0)
  1312. return result;
  1313. frameSize = nextFrameOffset + sideInfoSize + dataSize;
  1314. lastFrameSizeNoPadding = frameSize - frame.padding;
  1315. }
  1316. }
  1317. if (result == 0)
  1318. return result;
  1319. int bytes = frameSize - (sideInfoSize + dataSize);
  1320. if (bytes > 0)
  1321. {
  1322. const int toSkip = bytes - 512;
  1323. if (toSkip > 0)
  1324. {
  1325. stream.skipNextBytes (toSkip);
  1326. bytes -= toSkip;
  1327. frameSize -= toSkip;
  1328. }
  1329. stream.read (bufferPointer, bytes);
  1330. bufferPointer += bytes;
  1331. }
  1332. lastFrameSize = frameSize;
  1333. wasFreeFormat = isFreeFormat;
  1334. frameSize = 0;
  1335. headerParsed = sideParsed = dataParsed = false;
  1336. return result;
  1337. }
  1338. bool seek (int frameIndex)
  1339. {
  1340. frameIndex = jmax (0, frameIndex);
  1341. while (frameIndex >= frameStreamPositions.size() * storedStartPosInterval)
  1342. {
  1343. int dummy = 0;
  1344. const int result = decodeNextBlock (nullptr, nullptr, dummy);
  1345. if (result < 0)
  1346. return false;
  1347. else if (result > 0)
  1348. break;
  1349. }
  1350. frameIndex = jmin (frameIndex & ~(storedStartPosInterval - 1),
  1351. frameStreamPositions.size() * storedStartPosInterval - 1);
  1352. stream.setPosition (frameStreamPositions.getUnchecked (frameIndex / storedStartPosInterval));
  1353. currentFrameIndex = frameIndex;
  1354. reset();
  1355. return true;
  1356. }
  1357. MP3Frame frame;
  1358. VBRTagData vbrTagData;
  1359. BufferedInputStream stream;
  1360. int numFrames, currentFrameIndex;
  1361. bool vbrHeaderFound;
  1362. private:
  1363. bool headerParsed, sideParsed, dataParsed, needToSyncBitStream;
  1364. bool isFreeFormat, wasFreeFormat;
  1365. int sideInfoSize, dataSize;
  1366. int frameSize, lastFrameSize, lastFrameSizeNoPadding;
  1367. int bufferSpaceIndex;
  1368. Layer3SideInfo sideinfo;
  1369. uint8 bufferSpace[2][2880 + 1024];
  1370. uint8* bufferPointer;
  1371. int bitIndex, synthBo;
  1372. float hybridBlock[2][2][32 * 18];
  1373. int hybridBlockIndex[2];
  1374. float synthBuffers[2][2][0x110];
  1375. float hybridIn[2][32][18];
  1376. float hybridOut[2][18][32];
  1377. void reset() noexcept
  1378. {
  1379. headerParsed = sideParsed = dataParsed = isFreeFormat = wasFreeFormat = false;
  1380. lastFrameSize = -1;
  1381. needToSyncBitStream = true;
  1382. frameSize = sideInfoSize = dataSize = frameSize = bitIndex = 0;
  1383. lastFrameSizeNoPadding = bufferSpaceIndex = 0;
  1384. bufferPointer = bufferSpace[bufferSpaceIndex] + 512;
  1385. synthBo = 1;
  1386. zerostruct (sideinfo);
  1387. zeromem (bufferSpace, sizeof (bufferSpace));
  1388. zeromem (hybridBlock, sizeof (hybridBlock));
  1389. zeromem (hybridBlockIndex, sizeof (hybridBlockIndex));
  1390. zeromem (synthBuffers, sizeof (synthBuffers));
  1391. }
  1392. enum { storedStartPosInterval = 4 };
  1393. Array<int64> frameStreamPositions;
  1394. struct SideInfoLayer1
  1395. {
  1396. uint8 allocation[32][2];
  1397. uint8 scaleFactor[32][2];
  1398. };
  1399. struct SideInfoLayer2
  1400. {
  1401. uint8 allocation[32][2];
  1402. uint8 scaleFactor[32][2][3];
  1403. };
  1404. static bool isValidHeader (const uint32 header, const int oldLayer) noexcept
  1405. {
  1406. const int newLayer = 4 - ((header >> 17) & 3);
  1407. return (header & 0xffe00000) == 0xffe00000
  1408. && newLayer != 4
  1409. && (oldLayer <= 0 || newLayer == oldLayer)
  1410. && ((header >> 12) & 15) != 15
  1411. && ((header >> 10) & 3) != 3
  1412. && (header & 3) != 2;
  1413. }
  1414. bool rollBackBufferPointer (int backstep) noexcept
  1415. {
  1416. if (lastFrameSize < 0 && backstep > 0)
  1417. return false;
  1418. const uint8* oldBuffer = bufferSpace[1 - bufferSpaceIndex] + 512;
  1419. bufferPointer -= backstep;
  1420. if (backstep != 0)
  1421. memcpy (bufferPointer, oldBuffer + lastFrameSize - backstep, (size_t) backstep);
  1422. bitIndex = 0;
  1423. return true;
  1424. }
  1425. uint32 getBits (const int numBits) noexcept
  1426. {
  1427. if (numBits <= 0 || bufferPointer == nullptr)
  1428. return 0;
  1429. const uint32 result = ((((((bufferPointer[0] << 8) | bufferPointer[1]) << 8)
  1430. | bufferPointer[2]) << bitIndex) & 0xffffff) >> (24 - numBits);
  1431. bitIndex += numBits;
  1432. bufferPointer += (bitIndex >> 3);
  1433. bitIndex &= 7;
  1434. return result;
  1435. }
  1436. uint32 getOneBit() noexcept
  1437. {
  1438. const uint8 result = (uint8) (*bufferPointer << bitIndex);
  1439. ++bitIndex;
  1440. bufferPointer += (bitIndex >> 3);
  1441. bitIndex &= 7;
  1442. return result >> 7;
  1443. }
  1444. uint32 getBitsUnchecked (const int numBits) noexcept
  1445. {
  1446. const uint32 result = ((((bufferPointer[0] << 8) | bufferPointer[1]) << bitIndex) & 0xffff) >> (16 - numBits);
  1447. bitIndex += numBits;
  1448. bufferPointer += (bitIndex >> 3);
  1449. bitIndex &= 7;
  1450. return result;
  1451. }
  1452. inline uint8 getBitsUint8 (const int numBits) noexcept { return (uint8) getBitsUnchecked (numBits); }
  1453. inline uint16 getBitsUint16 (const int numBits) noexcept { return (uint16) getBitsUnchecked (numBits); }
  1454. int scanForNextFrameHeader (const bool checkTypeAgainstLastFrame) noexcept
  1455. {
  1456. const int64 oldPos = stream.getPosition();
  1457. int offset = -3;
  1458. uint32 header = 0;
  1459. for (;;)
  1460. {
  1461. if (stream.isExhausted() || stream.getPosition() > oldPos + 32768)
  1462. {
  1463. offset = -1;
  1464. break;
  1465. }
  1466. header = (header << 8) | (uint8) stream.readByte();
  1467. if (offset >= 0 && isValidHeader (header, frame.layer))
  1468. {
  1469. if (! checkTypeAgainstLastFrame)
  1470. break;
  1471. const bool mpeg25 = (header & (1 << 20)) == 0;
  1472. const int lsf = mpeg25 ? 1 : ((header & (1 << 19)) ? 0 : 1);
  1473. const int sampleRateIndex = mpeg25 ? (6 + ((header >> 10) & 3)) : (((header >> 10) & 3) + (lsf * 3));
  1474. const int mode = (header >> 6) & 3;
  1475. const int numChannels = (mode == 3) ? 1 : 2;
  1476. if (numChannels == frame.numChannels && lsf == frame.lsf
  1477. && mpeg25 == frame.mpeg25 && sampleRateIndex == frame.sampleRateIndex)
  1478. break;
  1479. }
  1480. ++offset;
  1481. }
  1482. if (offset >= 0)
  1483. {
  1484. if ((currentFrameIndex & (storedStartPosInterval - 1)) == 0)
  1485. frameStreamPositions.set (currentFrameIndex / storedStartPosInterval, oldPos + offset);
  1486. ++currentFrameIndex;
  1487. }
  1488. stream.setPosition (oldPos);
  1489. return offset;
  1490. }
  1491. void readVBRHeader()
  1492. {
  1493. int64 oldPos = stream.getPosition();
  1494. uint8 xing[194];
  1495. stream.read (xing, sizeof (xing));
  1496. vbrHeaderFound = vbrTagData.read (xing);
  1497. if (vbrHeaderFound)
  1498. {
  1499. numFrames = vbrTagData.frames;
  1500. oldPos += jmax (vbrTagData.headersize, 1);
  1501. }
  1502. stream.setPosition (oldPos);
  1503. }
  1504. void decodeLayer1Frame (float* const pcm0, float* const pcm1, int& samplesDone) noexcept
  1505. {
  1506. float fraction[2][32];
  1507. SideInfoLayer1 si;
  1508. layer1Step1 (si);
  1509. const int single = (frame.numChannels == 1 || frame.single == 3) ? 0 : frame.single;
  1510. if (single >= 0)
  1511. {
  1512. for (int i = 0; i < 12; ++i)
  1513. {
  1514. layer1Step2 (si, fraction);
  1515. synthesise (fraction[single], 0, pcm0, samplesDone);
  1516. }
  1517. }
  1518. else
  1519. {
  1520. for (int i = 0; i < 12; ++i)
  1521. {
  1522. layer1Step2 (si, fraction);
  1523. synthesiseStereo (fraction[0], fraction[1], pcm0, pcm1, samplesDone);
  1524. }
  1525. }
  1526. }
  1527. void decodeLayer2Frame (float* const pcm0, float* const pcm1, int& samplesDone)
  1528. {
  1529. float fraction[2][4][32];
  1530. frame.selectLayer2Table();
  1531. SideInfoLayer2 si;
  1532. layer2Step1 (si);
  1533. const int single = (frame.numChannels == 1 || frame.single == 3) ? 0 : frame.single;
  1534. if (single >= 0)
  1535. {
  1536. for (int i = 0; i < 12; ++i)
  1537. {
  1538. layer2Step2 (si, i >> 2, fraction);
  1539. for (int j = 0; j < 3; ++j)
  1540. synthesise (fraction[single][j], 0, pcm0, samplesDone);
  1541. }
  1542. }
  1543. else
  1544. {
  1545. for (int i = 0; i < 12; ++i)
  1546. {
  1547. layer2Step2 (si, i >> 2, fraction);
  1548. for (int j = 0; j < 3; ++j)
  1549. synthesiseStereo (fraction[0][j], fraction[1][j], pcm0, pcm1, samplesDone);
  1550. }
  1551. }
  1552. }
  1553. void decodeLayer3Frame (float* const pcm0, float* const pcm1, int& samplesDone) noexcept
  1554. {
  1555. if (! rollBackBufferPointer ((int) sideinfo.mainDataStart))
  1556. return;
  1557. const int single = frame.numChannels == 1 ? 0 : frame.single;
  1558. const int numChans = (frame.numChannels == 1 || single >= 0) ? 1 : 2;
  1559. const bool msStereo = (frame.mode == 1) && (frame.modeExt & 2) != 0;
  1560. const bool iStereo = (frame.mode == 1) && (frame.modeExt & 1) != 0;
  1561. const int granules = frame.lsf ? 1 : 2;
  1562. int scaleFactors[2][39];
  1563. for (int gr = 0; gr < granules; ++gr)
  1564. {
  1565. {
  1566. Layer3SideInfo::Info& granule = sideinfo.ch[0].gr[gr];
  1567. const int part2bits = frame.lsf ? getLayer3ScaleFactors2 (scaleFactors[0], granule, 0)
  1568. : getLayer3ScaleFactors1 (scaleFactors[0], granule);
  1569. if (layer3DequantizeSample (hybridIn[0], scaleFactors[0], granule, frame.sampleRateIndex, part2bits))
  1570. return;
  1571. }
  1572. if (frame.numChannels == 2)
  1573. {
  1574. Layer3SideInfo::Info& granule = sideinfo.ch[1].gr[gr];
  1575. const int part2bits = frame.lsf ? getLayer3ScaleFactors2 (scaleFactors[1], granule, iStereo)
  1576. : getLayer3ScaleFactors1 (scaleFactors[1], granule);
  1577. if (layer3DequantizeSample (hybridIn[1], scaleFactors[1], granule, frame.sampleRateIndex, part2bits))
  1578. return;
  1579. if (msStereo)
  1580. {
  1581. for (int i = 0; i < 32 * 18; ++i)
  1582. {
  1583. const float tmp0 = ((const float*) hybridIn[0]) [i];
  1584. const float tmp1 = ((const float*) hybridIn[1]) [i];
  1585. ((float*) hybridIn[1]) [i] = tmp0 - tmp1;
  1586. ((float*) hybridIn[0]) [i] = tmp0 + tmp1;
  1587. }
  1588. }
  1589. if (iStereo)
  1590. granule.doIStereo (hybridIn, scaleFactors[1], frame.sampleRateIndex, msStereo, frame.lsf);
  1591. if (msStereo || iStereo || single == 3)
  1592. {
  1593. if (granule.maxb > sideinfo.ch[0].gr[gr].maxb)
  1594. sideinfo.ch[0].gr[gr].maxb = granule.maxb;
  1595. else
  1596. granule.maxb = sideinfo.ch[0].gr[gr].maxb;
  1597. }
  1598. switch (single)
  1599. {
  1600. case 3:
  1601. {
  1602. float* in0 = (float*) hybridIn[0];
  1603. const float* in1 = (const float*) hybridIn[1];
  1604. for (int i = 0; i < (int) (18 * granule.maxb); ++i, ++in0)
  1605. *in0 = (*in0 + *in1++);
  1606. }
  1607. break;
  1608. case 1:
  1609. {
  1610. float* in0 = (float*) hybridIn[0];
  1611. const float* in1 = (const float*) hybridIn[1];
  1612. for (int i = 0; i < (int) (18 * granule.maxb); ++i)
  1613. *in0++ = *in1++;
  1614. }
  1615. break;
  1616. }
  1617. }
  1618. for (int ch = 0; ch < numChans; ++ch)
  1619. {
  1620. const Layer3SideInfo::Info& granule = sideinfo.ch[ch].gr[gr];
  1621. granule.doAntialias (hybridIn[ch]);
  1622. layer3Hybrid (hybridIn[ch], hybridOut[ch], ch, granule);
  1623. }
  1624. for (int ss = 0; ss < 18; ++ss)
  1625. {
  1626. if (single >= 0)
  1627. synthesise (hybridOut[0][ss], 0, pcm0, samplesDone);
  1628. else
  1629. synthesiseStereo (hybridOut[0][ss], hybridOut[1][ss], pcm0, pcm1, samplesDone);
  1630. }
  1631. }
  1632. }
  1633. int decodeLayer3SideInfo() noexcept
  1634. {
  1635. const int numChannels = frame.numChannels;
  1636. const int sampleRate = frame.sampleRateIndex;
  1637. const int single = (numChannels == 1) ? 0 : frame.single;
  1638. const bool msStereo = (frame.mode == 1) && (frame.modeExt & 2) != 0;
  1639. const int granules = frame.lsf ? 1 : 2;
  1640. if (frame.lsf == 0)
  1641. getLayer3SideInfo1 (numChannels, msStereo, sampleRate, single);
  1642. else
  1643. getLayer3SideInfo2 (numChannels, msStereo, sampleRate, single);
  1644. int databits = 0;
  1645. for (int gr = 0; gr < granules; ++gr)
  1646. for (int ch = 0; ch < numChannels; ++ch)
  1647. databits += sideinfo.ch[ch].gr[gr].part2_3Length;
  1648. return databits - 8 * sideinfo.mainDataStart;
  1649. }
  1650. void layer1Step1 (SideInfoLayer1& si) noexcept
  1651. {
  1652. zerostruct (si);
  1653. int i, jsbound = (frame.mode == 1) ? (frame.modeExt << 2) + 4 : 32;
  1654. if (frame.numChannels == 2)
  1655. {
  1656. for (i = 0; i < jsbound; ++i)
  1657. {
  1658. si.allocation[i][0] = getBitsUint8 (4);
  1659. si.allocation[i][1] = getBitsUint8 (4);
  1660. }
  1661. for (i = jsbound; i < 32; ++i)
  1662. si.allocation[i][0] = si.allocation[i][1] = getBitsUint8 (4);
  1663. for (i = 0; i < 32; ++i)
  1664. {
  1665. si.scaleFactor[i][0] = si.allocation[i][0] ? getBitsUint8 (6) : 0;
  1666. si.scaleFactor[i][1] = si.allocation[i][1] ? getBitsUint8 (6) : 0;
  1667. }
  1668. }
  1669. else
  1670. {
  1671. for (i = 0; i < 32; ++i)
  1672. si.allocation[i][0] = getBitsUint8 (4);
  1673. for (i = 0; i < 32; ++i)
  1674. si.scaleFactor[i][0] = si.allocation[i][0] ? getBitsUint8 (6) : 0;
  1675. }
  1676. }
  1677. void layer1Step2 (SideInfoLayer1& si, float fraction[2][32]) noexcept
  1678. {
  1679. if (frame.numChannels == 2)
  1680. {
  1681. int i, jsbound = (frame.mode == 1) ? (frame.modeExt << 2) + 4 : 32;
  1682. for (i = 0; i < jsbound; ++i)
  1683. {
  1684. const uint8 n0 = si.allocation[i][0];
  1685. const uint8 n1 = si.allocation[i][1];
  1686. fraction[0][i] = n0 > 0 ? (float) (((-1 << n0) + getBitsUint16 (n0 + 1) + 1) * constants.muls[n0 + 1][si.scaleFactor[i][0]]) : 0;
  1687. fraction[1][i] = n1 > 0 ? (float) (((-1 << n1) + getBitsUint16 (n1 + 1) + 1) * constants.muls[n1 + 1][si.scaleFactor[i][1]]) : 0;
  1688. }
  1689. for (i = jsbound; i < 32; ++i)
  1690. {
  1691. const uint8 n = si.allocation[i][0];
  1692. if (n > 0)
  1693. {
  1694. const uint32 w = ((-1 << n) + getBitsUint16 (n + 1) + 1);
  1695. fraction[0][i] = (float) (w * constants.muls[n + 1][si.scaleFactor[i][0]]);
  1696. fraction[1][i] = (float) (w * constants.muls[n + 1][si.scaleFactor[i][1]]);
  1697. }
  1698. else
  1699. fraction[0][i] = fraction[1][i] = 0;
  1700. }
  1701. }
  1702. else
  1703. {
  1704. for (int i = 0; i < 32; ++i)
  1705. {
  1706. const uint8 n = si.allocation[i][0];
  1707. const uint8 j = si.scaleFactor[i][0];
  1708. if (n > 0)
  1709. fraction[0][i] = (float) (((-1 << n) + getBitsUint16 (n + 1) + 1) * constants.muls[n + 1][j]);
  1710. else
  1711. fraction[0][i] = 0;
  1712. }
  1713. }
  1714. }
  1715. void layer2Step1 (SideInfoLayer2& si) noexcept
  1716. {
  1717. zerostruct (si);
  1718. const int sblimit = frame.layer2SubBandLimit;
  1719. const int jsbound = (frame.mode == 1) ? (frame.modeExt << 2) + 4 : frame.layer2SubBandLimit;
  1720. const AllocationTable* allocTable = frame.allocationTable;
  1721. uint8 scfsi[32][2];
  1722. if (frame.numChannels == 2)
  1723. {
  1724. for (int i = 0; i < jsbound; ++i)
  1725. {
  1726. const int16 step = allocTable->bits;
  1727. allocTable += (1 << step);
  1728. si.allocation[i][0] = getBitsUint8 (step);
  1729. si.allocation[i][1] = getBitsUint8 (step);
  1730. }
  1731. for (int i = jsbound; i < sblimit; ++i)
  1732. {
  1733. const int16 step = allocTable->bits;
  1734. const uint8 b0 = getBitsUint8 (step);
  1735. allocTable += (1 << step);
  1736. si.allocation[i][0] = b0;
  1737. si.allocation[i][1] = b0;
  1738. }
  1739. for (int i = 0; i < sblimit; ++i)
  1740. {
  1741. scfsi[i][0] = si.allocation[i][0] ? getBitsUint8 (2) : 0;
  1742. scfsi[i][1] = si.allocation[i][1] ? getBitsUint8 (2) : 0;
  1743. }
  1744. }
  1745. else
  1746. {
  1747. for (int i = 0; i < sblimit; ++i)
  1748. {
  1749. const int16 step = allocTable->bits;
  1750. allocTable += (1 << step);
  1751. si.allocation[i][0] = getBitsUint8 (step);
  1752. }
  1753. for (int i = 0; i < sblimit; ++i)
  1754. scfsi[i][0] = si.allocation[i][0] ? getBitsUint8 (2) : 0;
  1755. }
  1756. for (int i = 0; i < sblimit; ++i)
  1757. {
  1758. for (int ch = 0; ch < frame.numChannels; ++ch)
  1759. {
  1760. uint8 s0 = 0, s1 = 0, s2 = 0;
  1761. if (si.allocation[i][ch])
  1762. {
  1763. switch (scfsi[i][ch])
  1764. {
  1765. case 0:
  1766. s0 = getBitsUint8 (6);
  1767. s1 = getBitsUint8 (6);
  1768. s2 = getBitsUint8 (6);
  1769. break;
  1770. case 1:
  1771. s1 = s0 = getBitsUint8 (6);
  1772. s2 = getBitsUint8 (6);
  1773. break;
  1774. case 2:
  1775. s2 = s1 = s0 = getBitsUint8 (6);
  1776. break;
  1777. case 3:
  1778. s0 = getBitsUint8 (6);
  1779. s2 = s1 = getBitsUint8 (6);
  1780. break;
  1781. default:
  1782. break;
  1783. }
  1784. }
  1785. si.scaleFactor[i][ch][0] = s0;
  1786. si.scaleFactor[i][ch][1] = s1;
  1787. si.scaleFactor[i][ch][2] = s2;
  1788. }
  1789. }
  1790. }
  1791. void layer2Step2 (SideInfoLayer2& si, const int gr, float fraction[2][4][32]) noexcept
  1792. {
  1793. const AllocationTable* allocTable = frame.allocationTable;
  1794. const int jsbound = (frame.mode == 1) ? (frame.modeExt << 2) + 4 : frame.layer2SubBandLimit;
  1795. for (int i = 0; i < jsbound; ++i)
  1796. {
  1797. const int16 step = allocTable->bits;
  1798. for (int ch = 0; ch < frame.numChannels; ++ch)
  1799. {
  1800. const uint8 ba = si.allocation[i][ch];
  1801. if (ba != 0)
  1802. {
  1803. const uint8 x1 = jmin ((uint8) 63, si.scaleFactor[i][ch][gr]);
  1804. const AllocationTable* const alloc2 = allocTable + ba;
  1805. const int16 k = jmin ((int16) 16, alloc2->bits);
  1806. const int16 d1 = alloc2->d;
  1807. if (d1 < 0)
  1808. {
  1809. const double cm = constants.muls[k][x1];
  1810. fraction[ch][0][i] = (float) ((getBits (k) + d1) * cm);
  1811. fraction[ch][1][i] = (float) ((getBits (k) + d1) * cm);
  1812. fraction[ch][2][i] = (float) ((getBits (k) + d1) * cm);
  1813. }
  1814. else
  1815. {
  1816. const uint8* const tab = constants.getGroupTable (d1, getBits (k));
  1817. fraction[ch][0][i] = (float) constants.muls[tab[0]][x1];
  1818. fraction[ch][1][i] = (float) constants.muls[tab[1]][x1];
  1819. fraction[ch][2][i] = (float) constants.muls[tab[2]][x1];
  1820. }
  1821. }
  1822. else
  1823. {
  1824. fraction[ch][0][i] = fraction[ch][1][i] = fraction[ch][2][i] = 0;
  1825. }
  1826. }
  1827. allocTable += (1 << step);
  1828. }
  1829. for (int i = jsbound; i < frame.layer2SubBandLimit; ++i)
  1830. {
  1831. const int16 step = allocTable->bits;
  1832. const uint8 ba = si.allocation[i][0];
  1833. if (ba != 0)
  1834. {
  1835. const AllocationTable* const alloc2 = allocTable + ba;
  1836. int16 k = alloc2->bits;
  1837. int16 d1 = alloc2->d;
  1838. k = (k <= 16) ? k : 16;
  1839. if (d1 < 0)
  1840. {
  1841. const int v0 = getBits (k);
  1842. const int v1 = getBits (k);
  1843. const int v2 = getBits (k);
  1844. for (int ch = 0; ch < frame.numChannels; ++ch)
  1845. {
  1846. const uint8 x1 = jmin ((uint8) 63, si.scaleFactor[i][ch][gr]);
  1847. const double cm = constants.muls[k][x1];
  1848. fraction[ch][0][i] = (float) ((v0 + d1) * cm);
  1849. fraction[ch][1][i] = (float) ((v1 + d1) * cm);
  1850. fraction[ch][2][i] = (float) ((v2 + d1) * cm);
  1851. }
  1852. }
  1853. else
  1854. {
  1855. const uint8* const tab = constants.getGroupTable (d1, getBits (k));
  1856. const uint8 k0 = tab[0];
  1857. const uint8 k1 = tab[1];
  1858. const uint8 k2 = tab[2];
  1859. for (int ch = 0; ch < frame.numChannels; ++ch)
  1860. {
  1861. const uint8 x1 = jmin ((uint8) 63, si.scaleFactor[i][ch][gr]);
  1862. fraction[ch][0][i] = (float) constants.muls[k0][x1];
  1863. fraction[ch][1][i] = (float) constants.muls[k1][x1];
  1864. fraction[ch][2][i] = (float) constants.muls[k2][x1];
  1865. }
  1866. }
  1867. }
  1868. else
  1869. {
  1870. fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] = 0;
  1871. fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0;
  1872. }
  1873. allocTable += (1 << step);
  1874. }
  1875. for (int ch = 0; ch < frame.numChannels; ++ch)
  1876. for (int i = frame.layer2SubBandLimit; i < 32; ++i)
  1877. fraction[ch][0][i] = fraction[ch][1][i] = fraction[ch][2][i] = 0;
  1878. }
  1879. void getLayer3SideInfo1 (const int stereo, const bool msStereo, const int sampleRate, const int single) noexcept
  1880. {
  1881. const int powdiff = (single == 3) ? 4 : 0;
  1882. sideinfo.mainDataStart = getBits (9);
  1883. sideinfo.privateBits = getBitsUnchecked (stereo == 1 ? 5 : 3);
  1884. for (int ch = 0; ch < stereo; ++ch)
  1885. {
  1886. sideinfo.ch[ch].gr[0].scfsi = -1;
  1887. sideinfo.ch[ch].gr[1].scfsi = getBitsUnchecked (4);
  1888. }
  1889. for (int gr = 0; gr < 2; ++gr)
  1890. {
  1891. for (int ch = 0; ch < stereo; ++ch)
  1892. {
  1893. Layer3SideInfo::Info& granule = sideinfo.ch[ch].gr[gr];
  1894. granule.part2_3Length = getBits (12);
  1895. granule.bigValues = jmin (288, (int) getBitsUnchecked (9));
  1896. const int qss = getBitsUnchecked (8);
  1897. granule.pow2gain = constants.powToGains + 256 - qss + powdiff;
  1898. if (msStereo)
  1899. granule.pow2gain += 2;
  1900. granule.scaleFactorCompression = getBitsUnchecked (4);
  1901. if (getOneBit())
  1902. {
  1903. granule.blockType = getBitsUnchecked (2);
  1904. granule.mixedBlockFlag = getOneBit();
  1905. granule.tableSelect[0] = getBitsUnchecked (5);
  1906. granule.tableSelect[1] = getBitsUnchecked (5);
  1907. granule.tableSelect[2] = 0;
  1908. for (int i = 0; i < 3; ++i)
  1909. {
  1910. const int sbg = (getBitsUnchecked (3) << 3);
  1911. granule.fullGain[i] = granule.pow2gain + sbg;
  1912. }
  1913. granule.region1Start = 36 >> 1;
  1914. granule.region2Start = 576 >> 1;
  1915. }
  1916. else
  1917. {
  1918. for (int i = 0; i < 3; ++i)
  1919. granule.tableSelect[i] = getBitsUnchecked (5);
  1920. const int r0c = getBitsUnchecked (4);
  1921. const int r1c = getBitsUnchecked (3);
  1922. const int region0index = jmin (22, r0c + 1);
  1923. const int region1index = jmin (22, r0c + 1 + r1c + 1);
  1924. granule.region1Start = bandInfo[sampleRate].longIndex[region0index] >> 1;
  1925. granule.region2Start = bandInfo[sampleRate].longIndex[region1index] >> 1;
  1926. granule.blockType = 0;
  1927. granule.mixedBlockFlag = 0;
  1928. }
  1929. granule.preflag = getOneBit();
  1930. granule.scaleFactorScale = getOneBit();
  1931. granule.count1TableSelect = getOneBit();
  1932. }
  1933. }
  1934. }
  1935. void getLayer3SideInfo2 (const int stereo, const bool msStereo, const int sampleRate, const int single) noexcept
  1936. {
  1937. const int powdiff = (single == 3) ? 4 : 0;
  1938. sideinfo.mainDataStart = getBits (8);
  1939. sideinfo.privateBits = stereo == 1 ? getOneBit() : getBitsUnchecked (2);
  1940. for (int ch = 0; ch < stereo; ++ch)
  1941. {
  1942. Layer3SideInfo::Info& granule = sideinfo.ch[ch].gr[0];
  1943. granule.part2_3Length = getBits (12);
  1944. granule.bigValues = jmin (288, (int) getBitsUnchecked (9));
  1945. const uint32 qss = getBitsUnchecked (8);
  1946. granule.pow2gain = constants.powToGains + 256 - qss + powdiff;
  1947. if (msStereo)
  1948. granule.pow2gain += 2;
  1949. granule.scaleFactorCompression = getBits (9);
  1950. if (getOneBit())
  1951. {
  1952. granule.blockType = getBitsUnchecked (2);
  1953. granule.mixedBlockFlag = getOneBit();
  1954. granule.tableSelect[0] = getBitsUnchecked (5);
  1955. granule.tableSelect[1] = getBitsUnchecked (5);
  1956. granule.tableSelect[2] = 0;
  1957. for (int i = 0; i < 3; ++i)
  1958. {
  1959. const uint32 sbg = (getBitsUnchecked (3) << 3);
  1960. granule.fullGain[i] = granule.pow2gain + sbg;
  1961. }
  1962. if (granule.blockType == 0)
  1963. {}
  1964. if (granule.blockType == 2)
  1965. granule.region1Start = sampleRate == 8 ? 36 : (36 >> 1);
  1966. else
  1967. granule.region1Start = sampleRate == 8 ? (108 >> 1) : (54 >> 1);
  1968. granule.region2Start = 576 >> 1;
  1969. }
  1970. else
  1971. {
  1972. for (int i = 0; i < 3; ++i)
  1973. granule.tableSelect[i] = getBitsUnchecked (5);
  1974. const int r0c = getBitsUnchecked (4);
  1975. const int r1c = getBitsUnchecked (3);
  1976. const int region0index = jmin (22, r0c + 1);
  1977. const int region1index = jmin (22, r0c + 1 + r1c + 1);
  1978. granule.region1Start = bandInfo[sampleRate].longIndex[region0index] >> 1;
  1979. granule.region2Start = bandInfo[sampleRate].longIndex[region1index] >> 1;
  1980. granule.blockType = 0;
  1981. granule.mixedBlockFlag = 0;
  1982. }
  1983. granule.scaleFactorScale = getOneBit();
  1984. granule.count1TableSelect = getOneBit();
  1985. }
  1986. }
  1987. int getLayer3ScaleFactors1 (int* scf, const Layer3SideInfo::Info& granule) noexcept
  1988. {
  1989. static const uint8 lengths[2][16] =
  1990. {
  1991. { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 },
  1992. { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 }
  1993. };
  1994. int numBits;
  1995. const int num0 = lengths[0][granule.scaleFactorCompression];
  1996. const int num1 = lengths[1][granule.scaleFactorCompression];
  1997. if (granule.blockType == 2)
  1998. {
  1999. int i = 18;
  2000. numBits = (num0 + num1) * 18;
  2001. if (granule.mixedBlockFlag)
  2002. {
  2003. for (int j = 8; --j >= 0;) *scf++ = getBitsUnchecked (num0);
  2004. numBits -= num0;
  2005. i = 9;
  2006. }
  2007. for (; --i >= 0;) *scf++ = getBitsUnchecked (num0);
  2008. for (i = 18; --i >= 0;) *scf++ = getBitsUnchecked (num1);
  2009. *scf++ = 0;
  2010. *scf++ = 0;
  2011. *scf++ = 0;
  2012. }
  2013. else
  2014. {
  2015. const int scfsi = granule.scfsi;
  2016. if (scfsi < 0)
  2017. {
  2018. for (int i = 11; --i >= 0;) *scf++ = getBitsUnchecked (num0);
  2019. for (int j = 10; --j >= 0;) *scf++ = getBitsUnchecked (num1);
  2020. numBits = (num0 + num1) * 10 + num0;
  2021. }
  2022. else
  2023. {
  2024. numBits = 0;
  2025. if ((scfsi & 8) == 0)
  2026. {
  2027. for (int i = 6; --i >= 0;) *scf++ = getBitsUnchecked (num0);
  2028. numBits += num0 * 6;
  2029. }
  2030. else
  2031. scf += 6;
  2032. if ((scfsi & 4) == 0)
  2033. {
  2034. for (int i = 5; --i >= 0;) *scf++ = getBitsUnchecked (num0);
  2035. numBits += num0 * 5;
  2036. }
  2037. else
  2038. scf += 5;
  2039. if ((scfsi & 2) == 0)
  2040. {
  2041. for (int i = 5; --i >= 0;) *scf++ = getBitsUnchecked (num1);
  2042. numBits += num1 * 5;
  2043. }
  2044. else
  2045. scf += 5;
  2046. if ((scfsi & 1) == 0)
  2047. {
  2048. for (int i = 5; --i >= 0;) *scf++ = getBitsUnchecked (num1);
  2049. numBits += num1 * 5;
  2050. }
  2051. else
  2052. scf += 5;
  2053. }
  2054. *scf = 0;
  2055. }
  2056. return numBits;
  2057. }
  2058. int getLayer3ScaleFactors2 (int* scf, Layer3SideInfo::Info& granule, const bool iStereo) noexcept
  2059. {
  2060. static const uint8 scaleTable[3][6][4] =
  2061. {
  2062. { { 6, 5, 5, 5 }, { 6, 5, 7, 3 }, { 11, 10, 0, 0 }, { 7, 7, 7, 0 }, { 6, 6, 6, 3 }, { 8, 8, 5, 0 } },
  2063. { { 9, 9, 9, 9 }, { 9, 9, 12, 6 }, { 18, 18, 0, 0 }, { 12, 12, 12, 0 }, { 12, 9, 9, 6 }, { 15, 12, 9, 0 } },
  2064. { { 6, 9, 9, 9 }, { 6, 9, 12, 6 }, { 15, 18, 0, 0 }, { 6, 15, 12, 0 }, { 6, 12, 9, 6 }, { 6, 18, 9, 0 } }
  2065. };
  2066. uint32 len = iStereo ? constants.iLength2 [granule.scaleFactorCompression >> 1]
  2067. : constants.nLength2 [granule.scaleFactorCompression];
  2068. granule.preflag = (len >> 15) & 1;
  2069. int n = 0;
  2070. if (granule.blockType == 2)
  2071. {
  2072. ++n;
  2073. if (granule.mixedBlockFlag)
  2074. ++n;
  2075. }
  2076. const uint8* const data = scaleTable[n][(len >> 12) & 7];
  2077. int i, numBits = 0;
  2078. for (i = 0; i < 4; ++i)
  2079. {
  2080. int num = len & 7;
  2081. len >>= 3;
  2082. if (num)
  2083. {
  2084. for (int j = 0; j < (int) (data[i]); ++j)
  2085. *scf++ = getBitsUnchecked (num);
  2086. numBits += data[i] * num;
  2087. }
  2088. else
  2089. {
  2090. for (int j = 0; j < (int) (data[i]); ++j)
  2091. *scf++ = 0;
  2092. }
  2093. }
  2094. n = (n << 1) + 1;
  2095. for (i = 0; i < n; ++i)
  2096. *scf++ = 0;
  2097. return numBits;
  2098. }
  2099. bool layer3DequantizeSample (float xr[32][18], int* scf, Layer3SideInfo::Info& granule, int sampleRate, int part2bits) noexcept
  2100. {
  2101. const int shift = 1 + granule.scaleFactorScale;
  2102. float* xrpnt = (float*) xr;
  2103. int part2remain = granule.part2_3Length - part2bits;
  2104. zeromem (xrpnt, sizeof (float) * (&xr[32][0] - xrpnt));
  2105. const int bv = granule.bigValues;
  2106. const int region1 = granule.region1Start;
  2107. const int region2 = granule.region2Start;
  2108. int l3 = ((576 >> 1) - bv) >> 1;
  2109. int l[3];
  2110. if (bv <= region1)
  2111. {
  2112. l[0] = bv;
  2113. l[1] = 0;
  2114. l[2] = 0;
  2115. }
  2116. else
  2117. {
  2118. l[0] = region1;
  2119. if (bv <= region2)
  2120. {
  2121. l[1] = bv - l[0];
  2122. l[2] = 0;
  2123. }
  2124. else
  2125. {
  2126. l[1] = region2 - l[0];
  2127. l[2] = bv - region2;
  2128. }
  2129. }
  2130. for (int i = 0; i < 3; ++i)
  2131. if (l[i] < 0)
  2132. l[i] = 0;
  2133. if (granule.blockType == 2)
  2134. {
  2135. int max[4];
  2136. int step = 0, lwin = 0, cb = 0, mc = 0;
  2137. float v = 0;
  2138. int* map;
  2139. int* mapEnd;
  2140. if (granule.mixedBlockFlag)
  2141. {
  2142. max[3] = -1;
  2143. max[0] = max[1] = max[2] = 2;
  2144. map = constants.map [sampleRate][0];
  2145. mapEnd = constants.mapEnd [sampleRate][0];
  2146. }
  2147. else
  2148. {
  2149. max[0] = max[1] = max[2] = max[3] = -1;
  2150. map = constants.map [sampleRate][1];
  2151. mapEnd = constants.mapEnd [sampleRate][1];
  2152. }
  2153. for (int i = 0; i < 2; ++i)
  2154. {
  2155. const BitsToTableMap* h = huffmanTables1 + granule.tableSelect[i];
  2156. for (int lp = l[i]; lp != 0; --lp, --mc)
  2157. {
  2158. int x, y;
  2159. if (mc == 0)
  2160. {
  2161. mc = *map++;
  2162. xrpnt = ((float*) xr) + (*map++);
  2163. lwin = *map++;
  2164. cb = *map++;
  2165. if (lwin == 3)
  2166. {
  2167. v = granule.pow2gain[ (*scf++) << shift];
  2168. step = 1;
  2169. }
  2170. else
  2171. {
  2172. v = granule.fullGain[lwin][ (*scf++) << shift];
  2173. step = 3;
  2174. }
  2175. }
  2176. const int16* val = h->table;
  2177. while ((y = *val++) < 0)
  2178. {
  2179. if (getOneBit())
  2180. val -= y;
  2181. --part2remain;
  2182. }
  2183. x = y >> 4;
  2184. y &= 15;
  2185. if (x == 15)
  2186. {
  2187. max[lwin] = cb;
  2188. part2remain -= h->bits + 1;
  2189. x += getBits ((int) h->bits);
  2190. *xrpnt = constants.nToThe4Over3[x] * (getOneBit() ? -v : v);
  2191. }
  2192. else if (x)
  2193. {
  2194. max[lwin] = cb;
  2195. *xrpnt = constants.nToThe4Over3[x] * (getOneBit() ? -v : v);
  2196. --part2remain;
  2197. }
  2198. else
  2199. *xrpnt = 0;
  2200. xrpnt += step;
  2201. if (y == 15)
  2202. {
  2203. max[lwin] = cb;
  2204. part2remain -= h->bits + 1;
  2205. y += getBits ((int) h->bits);
  2206. *xrpnt = constants.nToThe4Over3[y] * (getOneBit() ? -v : v);
  2207. }
  2208. else if (y)
  2209. {
  2210. max[lwin] = cb;
  2211. *xrpnt = constants.nToThe4Over3[y] * (getOneBit() ? -v : v);
  2212. --part2remain;
  2213. }
  2214. else
  2215. *xrpnt = 0;
  2216. xrpnt += step;
  2217. }
  2218. }
  2219. for (; l3 && (part2remain > 0); --l3)
  2220. {
  2221. const BitsToTableMap* h = huffmanTables2 + granule.count1TableSelect;
  2222. const int16* val = h->table;
  2223. int16 a;
  2224. while ((a = *val++) < 0)
  2225. {
  2226. if (part2remain <= 0)
  2227. {
  2228. a = 0;
  2229. break;
  2230. }
  2231. --part2remain;
  2232. if (getOneBit())
  2233. val -= a;
  2234. }
  2235. for (int i = 0; i < 4; ++i)
  2236. {
  2237. if ((i & 1) == 0)
  2238. {
  2239. if (mc == 0)
  2240. {
  2241. mc = *map++;
  2242. xrpnt = ((float*) xr) + (*map++);
  2243. lwin = *map++;
  2244. cb = *map++;
  2245. if (lwin == 3)
  2246. {
  2247. v = granule.pow2gain[ (*scf++) << shift];
  2248. step = 1;
  2249. }
  2250. else
  2251. {
  2252. v = granule.fullGain[lwin][ (*scf++) << shift];
  2253. step = 3;
  2254. }
  2255. }
  2256. --mc;
  2257. }
  2258. if ((a & (8 >> i)))
  2259. {
  2260. max[lwin] = cb;
  2261. if (part2remain == 0)
  2262. break;
  2263. --part2remain;
  2264. *xrpnt = getOneBit() ? -v : v;
  2265. }
  2266. else
  2267. *xrpnt = 0;
  2268. xrpnt += step;
  2269. }
  2270. }
  2271. while (map < mapEnd)
  2272. {
  2273. if (mc == 0)
  2274. {
  2275. mc = *map++;
  2276. xrpnt = ((float*) xr) + *map++;
  2277. step = (*map++ == 3) ? 1 : 3;
  2278. ++map;
  2279. }
  2280. --mc;
  2281. *xrpnt = 0; xrpnt += step;
  2282. *xrpnt = 0; xrpnt += step;
  2283. }
  2284. granule.maxBand[0] = max[0] + 1;
  2285. granule.maxBand[1] = max[1] + 1;
  2286. granule.maxBand[2] = max[2] + 1;
  2287. granule.maxBandl = max[3] + 1;
  2288. const int rmax = jmax (max[0], max[1], max[3]) + 1;
  2289. granule.maxb = rmax ? constants.shortLimit[sampleRate][rmax]
  2290. : constants.longLimit[sampleRate][max[3] + 1];
  2291. }
  2292. else
  2293. {
  2294. static const int pretab1[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0 };
  2295. static const int pretab2[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  2296. const int* pretab = (const int*) (granule.preflag ? pretab1 : pretab2);
  2297. int max = -1, cb = 0, mc = 0;
  2298. int* map = constants.map [sampleRate][2];
  2299. float v = 0;
  2300. for (int i = 0; i < 3; ++i)
  2301. {
  2302. const BitsToTableMap* h = huffmanTables1 + granule.tableSelect[i];
  2303. for (int lp = l[i]; lp != 0; --lp, --mc)
  2304. {
  2305. if (mc == 0)
  2306. {
  2307. mc = *map++;
  2308. v = granule.pow2gain [((*scf++) + (*pretab++)) << shift];
  2309. cb = *map++;
  2310. }
  2311. const int16* val = h->table;
  2312. int y;
  2313. while ((y = *val++) < 0)
  2314. {
  2315. if (getOneBit()) val -= y;
  2316. --part2remain;
  2317. }
  2318. int x = y >> 4;
  2319. y &= 15;
  2320. if (x == 15)
  2321. {
  2322. max = cb;
  2323. part2remain -= h->bits + 1;
  2324. x += getBits ((int) h->bits);
  2325. *xrpnt++ = constants.nToThe4Over3[x] * (getOneBit() ? -v : v);
  2326. }
  2327. else if (x)
  2328. {
  2329. max = cb;
  2330. *xrpnt++ = constants.nToThe4Over3[x] * (getOneBit() ? -v : v);
  2331. --part2remain;
  2332. }
  2333. else
  2334. *xrpnt++ = 0;
  2335. if (y == 15)
  2336. {
  2337. max = cb;
  2338. part2remain -= h->bits + 1;
  2339. y += getBits ((int) h->bits);
  2340. *xrpnt++ = constants.nToThe4Over3[y] * (getOneBit() ? -v : v);
  2341. }
  2342. else if (y)
  2343. {
  2344. max = cb;
  2345. *xrpnt++ = constants.nToThe4Over3[y] * (getOneBit() ? -v : v);
  2346. --part2remain;
  2347. }
  2348. else
  2349. *xrpnt++ = 0;
  2350. }
  2351. }
  2352. for (; l3 && part2remain > 0; --l3)
  2353. {
  2354. const BitsToTableMap* h = huffmanTables2 + granule.count1TableSelect;
  2355. const int16* values = h->table;
  2356. int16 a;
  2357. while ((a = *values++) < 0)
  2358. {
  2359. if (part2remain <= 0)
  2360. {
  2361. a = 0;
  2362. break;
  2363. }
  2364. --part2remain;
  2365. if (getOneBit())
  2366. values -= a;
  2367. }
  2368. for (int i = 0; i < 4; ++i)
  2369. {
  2370. if ((i & 1) == 0)
  2371. {
  2372. if (mc == 0)
  2373. {
  2374. mc = *map++;
  2375. cb = *map++;
  2376. v = granule.pow2gain [((*scf++) + (*pretab++)) << shift];
  2377. }
  2378. --mc;
  2379. }
  2380. if ((a & (0x8 >> i)))
  2381. {
  2382. max = cb;
  2383. if (part2remain <= 0)
  2384. break;
  2385. --part2remain;
  2386. *xrpnt++ = getOneBit() ? -v : v;
  2387. }
  2388. else
  2389. *xrpnt++ = 0;
  2390. }
  2391. }
  2392. zeromem (xrpnt, sizeof (float) * (&xr[32][0] - xrpnt));
  2393. granule.maxBandl = max + 1;
  2394. granule.maxb = constants.longLimit[sampleRate][granule.maxBandl];
  2395. }
  2396. while (part2remain > 16)
  2397. {
  2398. getBits (16);
  2399. part2remain -= 16;
  2400. }
  2401. if (part2remain > 0)
  2402. getBits (part2remain);
  2403. else if (part2remain < 0)
  2404. return true;
  2405. return false;
  2406. }
  2407. void layer3Hybrid (float fsIn[32][18], float tsOut[18][32], int ch, const Layer3SideInfo::Info& granule) noexcept
  2408. {
  2409. float* ts = (float*) tsOut;
  2410. float* rawout1, *rawout2;
  2411. int sb = 0;
  2412. {
  2413. int b = hybridBlockIndex[ch];
  2414. rawout1 = hybridBlock[b][ch];
  2415. b = 1 - b;
  2416. rawout2 = hybridBlock[b][ch];
  2417. hybridBlockIndex[ch] = b;
  2418. }
  2419. if (granule.mixedBlockFlag)
  2420. {
  2421. sb = 2;
  2422. DCT::dct36 (fsIn[0], rawout1, rawout2, constants.win[0], ts);
  2423. DCT::dct36 (fsIn[1], rawout1 + 18, rawout2 + 18, constants.win1[0], ts + 1);
  2424. rawout1 += 36;
  2425. rawout2 += 36;
  2426. ts += 2;
  2427. }
  2428. const int bt = granule.blockType;
  2429. if (bt == 2)
  2430. {
  2431. for (; sb < (int) granule.maxb; sb += 2, ts += 2, rawout1 += 36, rawout2 += 36)
  2432. {
  2433. DCT::dct12 (fsIn[sb], rawout1, rawout2, constants.win[2], ts);
  2434. DCT::dct12 (fsIn[sb + 1], rawout1 + 18, rawout2 + 18, constants.win1[2], ts + 1);
  2435. }
  2436. }
  2437. else
  2438. {
  2439. for (; sb < (int) granule.maxb; sb += 2, ts += 2, rawout1 += 36, rawout2 += 36)
  2440. {
  2441. DCT::dct36 (fsIn[sb], rawout1, rawout2, constants.win[bt], ts);
  2442. DCT::dct36 (fsIn[sb + 1], rawout1 + 18, rawout2 + 18, constants.win1[bt], ts + 1);
  2443. }
  2444. }
  2445. for (; sb < 32; ++sb, ++ts)
  2446. {
  2447. for (int i = 0; i < 18; ++i)
  2448. {
  2449. ts[i * 32] = *rawout1++;
  2450. *rawout2++ = 0;
  2451. }
  2452. }
  2453. }
  2454. void synthesiseStereo (const float* bandPtr0, const float* bandPtr1, float* out0, float* out1, int& samplesDone) noexcept
  2455. {
  2456. int dummy = samplesDone;
  2457. synthesise (bandPtr0, 0, out0, dummy);
  2458. synthesise (bandPtr1, 1, out1, samplesDone);
  2459. }
  2460. void synthesise (const float* bandPtr, const int channel, float* out, int& samplesDone)
  2461. {
  2462. out += samplesDone;
  2463. const int bo = channel == 0 ? ((synthBo - 1) & 15) : synthBo;
  2464. float (*buf)[0x110] = synthBuffers[channel];
  2465. float* b0;
  2466. int j, bo1 = bo;
  2467. if (bo & 1)
  2468. {
  2469. b0 = buf[0];
  2470. DCT::dct64 (buf[1] + ((bo + 1) & 15), buf[0] + bo, bandPtr);
  2471. }
  2472. else
  2473. {
  2474. ++bo1;
  2475. b0 = buf[1];
  2476. DCT::dct64 (buf[0] + bo, buf[1] + bo1, bandPtr);
  2477. }
  2478. synthBo = bo;
  2479. const float* window = constants.decodeWin + 16 - bo1;
  2480. for (j = 16; j != 0; --j, b0 += 16, window += 32)
  2481. {
  2482. float sum = window[0] * b0[0]; sum -= window[1] * b0[1];
  2483. sum += window[2] * b0[2]; sum -= window[3] * b0[3];
  2484. sum += window[4] * b0[4]; sum -= window[5] * b0[5];
  2485. sum += window[6] * b0[6]; sum -= window[7] * b0[7];
  2486. sum += window[8] * b0[8]; sum -= window[9] * b0[9];
  2487. sum += window[10] * b0[10]; sum -= window[11] * b0[11];
  2488. sum += window[12] * b0[12]; sum -= window[13] * b0[13];
  2489. sum += window[14] * b0[14]; sum -= window[15] * b0[15];
  2490. *out++ = sum;
  2491. }
  2492. {
  2493. float sum = window[0] * b0[0]; sum += window[2] * b0[2];
  2494. sum += window[4] * b0[4]; sum += window[6] * b0[6];
  2495. sum += window[8] * b0[8]; sum += window[10] * b0[10];
  2496. sum += window[12] * b0[12]; sum += window[14] * b0[14];
  2497. *out++ = sum;
  2498. b0 -= 16; window -= 32;
  2499. window += bo1 << 1;
  2500. }
  2501. for (j = 15; j != 0; --j, b0 -= 16, window -= 32)
  2502. {
  2503. float sum = -window[-1] * b0[0]; sum -= window[-2] * b0[1];
  2504. sum -= window[-3] * b0[2]; sum -= window[-4] * b0[3];
  2505. sum -= window[-5] * b0[4]; sum -= window[-6] * b0[5];
  2506. sum -= window[-7] * b0[6]; sum -= window[-8] * b0[7];
  2507. sum -= window[-9] * b0[8]; sum -= window[-10] * b0[9];
  2508. sum -= window[-11] * b0[10]; sum -= window[-12] * b0[11];
  2509. sum -= window[-13] * b0[12]; sum -= window[-14] * b0[13];
  2510. sum -= window[-15] * b0[14]; sum -= window[0] * b0[15];
  2511. *out++ = sum;
  2512. }
  2513. samplesDone += 32;
  2514. }
  2515. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MP3Stream)
  2516. };
  2517. //==============================================================================
  2518. static const char* const mp3FormatName = "MP3 file";
  2519. static const char* const mp3Extensions[] = { ".mp3", nullptr };
  2520. //==============================================================================
  2521. class MP3Reader : public AudioFormatReader
  2522. {
  2523. public:
  2524. MP3Reader (InputStream* const in)
  2525. : AudioFormatReader (in, TRANS (mp3FormatName)),
  2526. stream (*in), currentPosition (0),
  2527. decodedStart (0), decodedEnd (0)
  2528. {
  2529. skipID3();
  2530. const int64 streamPos = stream.stream.getPosition();
  2531. if (readNextBlock())
  2532. {
  2533. bitsPerSample = 32;
  2534. usesFloatingPointData = true;
  2535. sampleRate = stream.frame.getFrequency();
  2536. numChannels = stream.frame.numChannels;
  2537. lengthInSamples = findLength (streamPos);
  2538. }
  2539. }
  2540. //==============================================================================
  2541. bool readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
  2542. int64 startSampleInFile, int numSamples)
  2543. {
  2544. jassert (destSamples != nullptr);
  2545. if (currentPosition != startSampleInFile)
  2546. {
  2547. if (! stream.seek ((int) (startSampleInFile / 1152 - 1)))
  2548. {
  2549. currentPosition = -1;
  2550. createEmptyDecodedData();
  2551. }
  2552. else
  2553. {
  2554. decodedStart = decodedEnd = 0;
  2555. const int64 streamPos = stream.currentFrameIndex * 1152;
  2556. int toSkip = (int) (startSampleInFile - streamPos);
  2557. jassert (toSkip >= 0);
  2558. while (toSkip > 0)
  2559. {
  2560. if (! readNextBlock())
  2561. {
  2562. createEmptyDecodedData();
  2563. break;
  2564. }
  2565. const int numReady = decodedEnd - decodedStart;
  2566. if (numReady > toSkip)
  2567. {
  2568. decodedStart += toSkip;
  2569. break;
  2570. }
  2571. toSkip -= numReady;
  2572. }
  2573. currentPosition = startSampleInFile;
  2574. }
  2575. }
  2576. while (numSamples > 0)
  2577. {
  2578. if (decodedEnd <= decodedStart && ! readNextBlock())
  2579. {
  2580. for (int i = 2; --i >= 0;)
  2581. if (destSamples[i] != nullptr)
  2582. zeromem (destSamples[i] + startOffsetInDestBuffer, sizeof (float) * numSamples);
  2583. return false;
  2584. }
  2585. const int numToCopy = jmin (decodedEnd - decodedStart, numSamples);
  2586. float* const* const dst = reinterpret_cast <float**> (destSamples);
  2587. memcpy (dst[0] + startOffsetInDestBuffer, decoded0 + decodedStart, sizeof (float) * numToCopy);
  2588. if (dst[1] != nullptr)
  2589. memcpy (dst[1] + startOffsetInDestBuffer, (numChannels < 2 ? decoded0 : decoded1) + decodedStart, sizeof (float) * numToCopy);
  2590. startOffsetInDestBuffer += numToCopy;
  2591. decodedStart += numToCopy;
  2592. currentPosition += numToCopy;
  2593. numSamples -= numToCopy;
  2594. }
  2595. return true;
  2596. }
  2597. private:
  2598. MP3Stream stream;
  2599. int64 currentPosition;
  2600. enum { decodedDataSize = 1152 };
  2601. float decoded0 [decodedDataSize], decoded1 [decodedDataSize];
  2602. int decodedStart, decodedEnd;
  2603. void createEmptyDecodedData() noexcept
  2604. {
  2605. zeromem (decoded0, sizeof (decoded0));
  2606. zeromem (decoded1, sizeof (decoded1));
  2607. decodedStart = 0;
  2608. decodedEnd = decodedDataSize;
  2609. }
  2610. bool readNextBlock()
  2611. {
  2612. for (int attempts = 10; --attempts >= 0;)
  2613. {
  2614. int samplesDone = 0;
  2615. const int result = stream.decodeNextBlock (decoded0, decoded1, samplesDone);
  2616. if (result > 0 && stream.stream.isExhausted())
  2617. {
  2618. createEmptyDecodedData();
  2619. return true;
  2620. }
  2621. else if (result <= 0)
  2622. {
  2623. decodedStart = 0;
  2624. decodedEnd = samplesDone;
  2625. return result == 0;
  2626. }
  2627. }
  2628. return false;
  2629. }
  2630. void skipID3()
  2631. {
  2632. const int64 originalPosition = stream.stream.getPosition();
  2633. const uint32 firstWord = stream.stream.readInt();
  2634. if ((firstWord & 0xffffff) == 0x334449)
  2635. {
  2636. uint8 buffer[6];
  2637. if (stream.stream.read (buffer, 6) == 6
  2638. && buffer[0] != 0xff
  2639. && ((buffer[2] | buffer[3] | buffer[4] | buffer[5]) & 0x80) == 0)
  2640. {
  2641. const int length = (((uint32) buffer[2]) << 21)
  2642. | (((uint32) buffer[3]) << 14)
  2643. | (((uint32) buffer[4]) << 7)
  2644. | ((uint32) buffer[5]);
  2645. stream.stream.skipNextBytes (length);
  2646. return;
  2647. }
  2648. }
  2649. stream.stream.setPosition (originalPosition);
  2650. }
  2651. int64 findLength (int64 streamStartPos)
  2652. {
  2653. int64 numFrames = stream.numFrames;
  2654. if (numFrames <= 0)
  2655. {
  2656. const int64 streamSize = stream.stream.getTotalLength();
  2657. if (streamSize > 0)
  2658. numFrames = (streamSize - streamStartPos) / (stream.frame.frameSize);
  2659. }
  2660. return numFrames * 1152;
  2661. }
  2662. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MP3Reader)
  2663. };
  2664. }
  2665. //==============================================================================
  2666. MP3AudioFormat::MP3AudioFormat()
  2667. : AudioFormat (MP3Decoder::mp3FormatName, StringArray (MP3Decoder::mp3Extensions))
  2668. {}
  2669. MP3AudioFormat::~MP3AudioFormat() {}
  2670. Array<int> MP3AudioFormat::getPossibleSampleRates() { return Array<int>(); }
  2671. Array<int> MP3AudioFormat::getPossibleBitDepths() { return Array<int>(); }
  2672. bool MP3AudioFormat::canDoStereo() { return true; }
  2673. bool MP3AudioFormat::canDoMono() { return true; }
  2674. bool MP3AudioFormat::isCompressed() { return true; }
  2675. StringArray MP3AudioFormat::getQualityOptions() { return StringArray(); }
  2676. AudioFormatReader* MP3AudioFormat::createReaderFor (InputStream* sourceStream, const bool deleteStreamIfOpeningFails)
  2677. {
  2678. ScopedPointer<MP3Decoder::MP3Reader> r (new MP3Decoder::MP3Reader (sourceStream));
  2679. if (r->lengthInSamples > 0)
  2680. return r.release();
  2681. if (! deleteStreamIfOpeningFails)
  2682. r->input = nullptr;
  2683. return nullptr;
  2684. }
  2685. AudioFormatWriter* MP3AudioFormat::createWriterFor (OutputStream*, double /*sampleRateToUse*/,
  2686. unsigned int /*numberOfChannels*/, int /*bitsPerSample*/,
  2687. const StringPairArray& /*metadataValues*/, int /*qualityOptionIndex*/)
  2688. {
  2689. return nullptr;
  2690. }
  2691. #endif