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.

3189 lines
132KB

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