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