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.

3154 lines
130KB

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