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