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.

3171 lines
130KB

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