1 /******************************************************************************
2 ** kjmp2 -- a minimal MPEG-1/2 Audio Layer II decoder library **
3 ** version 1.1 **
4 *******************************************************************************
5 ** Copyright (C) 2006-2013 Martin J. Fiedler <martin.fiedler@gmx.net> **
6 ** **
7 ** This software is provided 'as-is', without any express or implied **
8 ** warranty. In no event will the authors be held liable for any damages **
9 ** arising from the use of this software. **
10 ** **
11 ** Permission is granted to anyone to use this software for any purpose, **
12 ** including commercial applications, and to alter it and redistribute it **
13 ** freely, subject to the following restrictions: **
14 ** 1. The origin of this software must not be misrepresented; you must not **
15 ** claim that you wrote the original software. If you use this software **
16 ** in a product, an acknowledgment in the product documentation would **
17 ** be appreciated but is not required. **
18 ** 2. Altered source versions must be plainly marked as such, and must not **
19 ** be misrepresented as being the original software. **
20 ** 3. This notice may not be removed or altered from any source **
21 ** distribution. **
22 ******************************************************************************/
23
24 #include <math.h>
25
26 #include "kjmp2.h"
27
28 #ifdef _MSC_VER
29 #define FASTCALL __fastcall
30 #else
31 #define FASTCALL
32 #endif
33
34 ////////////////////////////////////////////////////////////////////////////////
35 // TABLES AND CONSTANTS //
36 ////////////////////////////////////////////////////////////////////////////////
37
38 // mode constants
39 #define STEREO 0
40 #define JOINT_STEREO 1
41 #define DUAL_CHANNEL 2
42 #define MONO 3
43
44 // sample rate table
45 static const unsigned short sample_rates[8] = {
46 44100, 48000, 32000, 0, // MPEG-1
47 22050, 24000, 16000, 0 // MPEG-2
48 };
49
50 // bitrate table
51 static const short bitrates[28] = {
52 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, // MPEG-1
53 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160 // MPEG-2
54 };
55
56 // scale factor base values (24-bit fixed-point)
57 static const int scf_base[3] = { 0x02000000, 0x01965FEA, 0x01428A30 };
58
59 // synthesis window
60 static const int D[512] = {
61 0x00000, 0x00000, 0x00000, 0x00000, 0x00000, 0x00000, 0x00000,-0x00001,
62 -0x00001,-0x00001,-0x00001,-0x00002,-0x00002,-0x00003,-0x00003,-0x00004,
63 -0x00004,-0x00005,-0x00006,-0x00006,-0x00007,-0x00008,-0x00009,-0x0000A,
64 -0x0000C,-0x0000D,-0x0000F,-0x00010,-0x00012,-0x00014,-0x00017,-0x00019,
65 -0x0001C,-0x0001E,-0x00022,-0x00025,-0x00028,-0x0002C,-0x00030,-0x00034,
66 -0x00039,-0x0003E,-0x00043,-0x00048,-0x0004E,-0x00054,-0x0005A,-0x00060,
67 -0x00067,-0x0006E,-0x00074,-0x0007C,-0x00083,-0x0008A,-0x00092,-0x00099,
68 -0x000A0,-0x000A8,-0x000AF,-0x000B6,-0x000BD,-0x000C3,-0x000C9,-0x000CF,
69 0x000D5, 0x000DA, 0x000DE, 0x000E1, 0x000E3, 0x000E4, 0x000E4, 0x000E3,
70 0x000E0, 0x000DD, 0x000D7, 0x000D0, 0x000C8, 0x000BD, 0x000B1, 0x000A3,
71 0x00092, 0x0007F, 0x0006A, 0x00053, 0x00039, 0x0001D,-0x00001,-0x00023,
72 -0x00047,-0x0006E,-0x00098,-0x000C4,-0x000F3,-0x00125,-0x0015A,-0x00190,
73 -0x001CA,-0x00206,-0x00244,-0x00284,-0x002C6,-0x0030A,-0x0034F,-0x00396,
74 -0x003DE,-0x00427,-0x00470,-0x004B9,-0x00502,-0x0054B,-0x00593,-0x005D9,
75 -0x0061E,-0x00661,-0x006A1,-0x006DE,-0x00718,-0x0074D,-0x0077E,-0x007A9,
76 -0x007D0,-0x007EF,-0x00808,-0x0081A,-0x00824,-0x00826,-0x0081F,-0x0080E,
77 0x007F5, 0x007D0, 0x007A0, 0x00765, 0x0071E, 0x006CB, 0x0066C, 0x005FF,
78 0x00586, 0x00500, 0x0046B, 0x003CA, 0x0031A, 0x0025D, 0x00192, 0x000B9,
79 -0x0002C,-0x0011F,-0x00220,-0x0032D,-0x00446,-0x0056B,-0x0069B,-0x007D5,
80 -0x00919,-0x00A66,-0x00BBB,-0x00D16,-0x00E78,-0x00FDE,-0x01148,-0x012B3,
81 -0x01420,-0x0158C,-0x016F6,-0x0185C,-0x019BC,-0x01B16,-0x01C66,-0x01DAC,
82 -0x01EE5,-0x02010,-0x0212A,-0x02232,-0x02325,-0x02402,-0x024C7,-0x02570,
83 -0x025FE,-0x0266D,-0x026BB,-0x026E6,-0x026ED,-0x026CE,-0x02686,-0x02615,
84 -0x02577,-0x024AC,-0x023B2,-0x02287,-0x0212B,-0x01F9B,-0x01DD7,-0x01BDD,
85 0x019AE, 0x01747, 0x014A8, 0x011D1, 0x00EC0, 0x00B77, 0x007F5, 0x0043A,
86 0x00046,-0x003E5,-0x00849,-0x00CE3,-0x011B4,-0x016B9,-0x01BF1,-0x0215B,
87 -0x026F6,-0x02CBE,-0x032B3,-0x038D3,-0x03F1A,-0x04586,-0x04C15,-0x052C4,
88 -0x05990,-0x06075,-0x06771,-0x06E80,-0x0759F,-0x07CCA,-0x083FE,-0x08B37,
89 -0x09270,-0x099A7,-0x0A0D7,-0x0A7FD,-0x0AF14,-0x0B618,-0x0BD05,-0x0C3D8,
90 -0x0CA8C,-0x0D11D,-0x0D789,-0x0DDC9,-0x0E3DC,-0x0E9BD,-0x0EF68,-0x0F4DB,
91 -0x0FA12,-0x0FF09,-0x103BD,-0x1082C,-0x10C53,-0x1102E,-0x113BD,-0x116FB,
92 -0x119E8,-0x11C82,-0x11EC6,-0x120B3,-0x12248,-0x12385,-0x12467,-0x124EF,
93 0x1251E, 0x124F0, 0x12468, 0x12386, 0x12249, 0x120B4, 0x11EC7, 0x11C83,
94 0x119E9, 0x116FC, 0x113BE, 0x1102F, 0x10C54, 0x1082D, 0x103BE, 0x0FF0A,
95 0x0FA13, 0x0F4DC, 0x0EF69, 0x0E9BE, 0x0E3DD, 0x0DDCA, 0x0D78A, 0x0D11E,
96 0x0CA8D, 0x0C3D9, 0x0BD06, 0x0B619, 0x0AF15, 0x0A7FE, 0x0A0D8, 0x099A8,
97 0x09271, 0x08B38, 0x083FF, 0x07CCB, 0x075A0, 0x06E81, 0x06772, 0x06076,
98 0x05991, 0x052C5, 0x04C16, 0x04587, 0x03F1B, 0x038D4, 0x032B4, 0x02CBF,
99 0x026F7, 0x0215C, 0x01BF2, 0x016BA, 0x011B5, 0x00CE4, 0x0084A, 0x003E6,
100 -0x00045,-0x00439,-0x007F4,-0x00B76,-0x00EBF,-0x011D0,-0x014A7,-0x01746,
101 0x019AE, 0x01BDE, 0x01DD8, 0x01F9C, 0x0212C, 0x02288, 0x023B3, 0x024AD,
102 0x02578, 0x02616, 0x02687, 0x026CF, 0x026EE, 0x026E7, 0x026BC, 0x0266E,
103 0x025FF, 0x02571, 0x024C8, 0x02403, 0x02326, 0x02233, 0x0212B, 0x02011,
104 0x01EE6, 0x01DAD, 0x01C67, 0x01B17, 0x019BD, 0x0185D, 0x016F7, 0x0158D,
105 0x01421, 0x012B4, 0x01149, 0x00FDF, 0x00E79, 0x00D17, 0x00BBC, 0x00A67,
106 0x0091A, 0x007D6, 0x0069C, 0x0056C, 0x00447, 0x0032E, 0x00221, 0x00120,
107 0x0002D,-0x000B8,-0x00191,-0x0025C,-0x00319,-0x003C9,-0x0046A,-0x004FF,
108 -0x00585,-0x005FE,-0x0066B,-0x006CA,-0x0071D,-0x00764,-0x0079F,-0x007CF,
109 0x007F5, 0x0080F, 0x00820, 0x00827, 0x00825, 0x0081B, 0x00809, 0x007F0,
110 0x007D1, 0x007AA, 0x0077F, 0x0074E, 0x00719, 0x006DF, 0x006A2, 0x00662,
111 0x0061F, 0x005DA, 0x00594, 0x0054C, 0x00503, 0x004BA, 0x00471, 0x00428,
112 0x003DF, 0x00397, 0x00350, 0x0030B, 0x002C7, 0x00285, 0x00245, 0x00207,
113 0x001CB, 0x00191, 0x0015B, 0x00126, 0x000F4, 0x000C5, 0x00099, 0x0006F,
114 0x00048, 0x00024, 0x00002,-0x0001C,-0x00038,-0x00052,-0x00069,-0x0007E,
115 -0x00091,-0x000A2,-0x000B0,-0x000BC,-0x000C7,-0x000CF,-0x000D6,-0x000DC,
116 -0x000DF,-0x000E2,-0x000E3,-0x000E3,-0x000E2,-0x000E0,-0x000DD,-0x000D9,
117 0x000D5, 0x000D0, 0x000CA, 0x000C4, 0x000BE, 0x000B7, 0x000B0, 0x000A9,
118 0x000A1, 0x0009A, 0x00093, 0x0008B, 0x00084, 0x0007D, 0x00075, 0x0006F,
119 0x00068, 0x00061, 0x0005B, 0x00055, 0x0004F, 0x00049, 0x00044, 0x0003F,
120 0x0003A, 0x00035, 0x00031, 0x0002D, 0x00029, 0x00026, 0x00023, 0x0001F,
121 0x0001D, 0x0001A, 0x00018, 0x00015, 0x00013, 0x00011, 0x00010, 0x0000E,
122 0x0000D, 0x0000B, 0x0000A, 0x00009, 0x00008, 0x00007, 0x00007, 0x00006,
123 0x00005, 0x00005, 0x00004, 0x00004, 0x00003, 0x00003, 0x00002, 0x00002,
124 0x00002, 0x00002, 0x00001, 0x00001, 0x00001, 0x00001, 0x00001, 0x00001
125 };
126
127
128 ///////////// Table 3-B.2: Possible quantization per subband ///////////////////
129
130 // quantizer lookup, step 1: bitrate classes
131 static const char quant_lut_step1[2][16] = {
132 // 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384 <- bitrate
133 { 0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, // mono
134 // 16, 24, 28, 32, 40, 48, 56, 64, 80, 96,112,128,160,192 <- BR / chan
135 { 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2 } // stereo
136 };
137
138 // quantizer lookup, step 2: bitrate class, sample rate -> B2 table idx, sblimit
139 #define QUANT_TAB_A (27 | 64) // Table 3-B.2a: high-rate, sblimit = 27
140 #define QUANT_TAB_B (30 | 64) // Table 3-B.2b: high-rate, sblimit = 30
141 #define QUANT_TAB_C 8 // Table 3-B.2c: low-rate, sblimit = 8
142 #define QUANT_TAB_D 12 // Table 3-B.2d: low-rate, sblimit = 12
143 static const char quant_lut_step2[3][4] = {
144 // 44.1 kHz, 48 kHz, 32 kHz
145 { QUANT_TAB_C, QUANT_TAB_C, QUANT_TAB_D }, // 32 - 48 kbit/sec/ch
146 { QUANT_TAB_A, QUANT_TAB_A, QUANT_TAB_A }, // 56 - 80 kbit/sec/ch
147 { QUANT_TAB_B, QUANT_TAB_A, QUANT_TAB_B }, // 96+ kbit/sec/ch
148 };
149
150 // quantizer lookup, step 3: B2 table, subband -> nbal, row index
151 // (upper 4 bits: nbal, lower 4 bits: row index)
152 static const char quant_lut_step3[3][32] = {
153 // low-rate table (3-B.2c and 3-B.2d)
154 { 0x44,0x44, // SB 0 - 1
155 0x34,0x34,0x34,0x34,0x34,0x34,0x34,0x34,0x34,0x34 // SB 2 - 12
156 },
157 // high-rate table (3-B.2a and 3-B.2b)
158 { 0x43,0x43,0x43, // SB 0 - 2
159 0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42, // SB 3 - 10
160 0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31, // SB 11 - 22
161 0x20,0x20,0x20,0x20,0x20,0x20,0x20 // SB 23 - 29
162 },
163 // MPEG-2 LSR table (B.2 in ISO 13818-3)
164 { 0x45,0x45,0x45,0x45, // SB 0 - 3
165 0x34,0x34,0x34,0x34,0x34,0x34,0x34, // SB 4 - 10
166 0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24, // SB 11 -
167 0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24 // - 29
168 }
169 };
170
171 // quantizer lookup, step 4: table row, allocation[] value -> quant table index
172 static const char quant_lut_step4[6][16] = {
173 { 0, 1, 2, 17 },
174 { 0, 1, 2, 3, 4, 5, 6, 17 },
175 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17 },
176 { 0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 },
177 { 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17 },
178 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
179 };
180
181 // quantizer specification structure
182 struct quantizer_spec {
183 unsigned short nlevels;
184 unsigned char grouping;
185 unsigned char cw_bits;
186 };
187
188 // quantizer table
189 static const struct quantizer_spec quantizer_table[17] = {
190 { 3, 1, 5 }, // 1
191 { 5, 1, 7 }, // 2
192 { 7, 0, 3 }, // 3
193 { 9, 1, 10 }, // 4
194 { 15, 0, 4 }, // 5
195 { 31, 0, 5 }, // 6
196 { 63, 0, 6 }, // 7
197 { 127, 0, 7 }, // 8
198 { 255, 0, 8 }, // 9
199 { 511, 0, 9 }, // 10
200 { 1023, 0, 10 }, // 11
201 { 2047, 0, 11 }, // 12
202 { 4095, 0, 12 }, // 13
203 { 8191, 0, 13 }, // 14
204 { 16383, 0, 14 }, // 15
205 { 32767, 0, 15 }, // 16
206 { 65535, 0, 16 } // 17
207 };
208
209
210 ////////////////////////////////////////////////////////////////////////////////
211 // STATIC VARIABLES AND FUNCTIONS //
212 ////////////////////////////////////////////////////////////////////////////////
213
214 #define KJMP2_MAGIC 0x32706D
215
216 static int initialized = 0;
217 static int bit_window;
218 static int bits_in_window;
219 static const unsigned char *frame_pos;
220
221 #define show_bits(bit_count) (bit_window >> (24 - (bit_count)))
222
get_bits(int bit_count)223 static int FASTCALL get_bits(int bit_count) {
224 int result = show_bits(bit_count);
225 bit_window = (bit_window << bit_count) & 0xFFFFFF;
226 bits_in_window -= bit_count;
227 while (bits_in_window < 16) {
228 bit_window |= (*frame_pos++) << (16 - bits_in_window);
229 bits_in_window += 8;
230 }
231 return result;
232 }
233
234
235 ////////////////////////////////////////////////////////////////////////////////
236 // INITIALIZATION //
237 ////////////////////////////////////////////////////////////////////////////////
238
239 static int N[64][32]; // N[i][j] as 8-bit fixed-point
240
kjmp2_init(kjmp2_context_t * mp2)241 void kjmp2_init(kjmp2_context_t *mp2) {
242 int i, j;
243 // check if global initialization is required
244 if (!initialized) {
245 int *nptr = &N[0][0];
246 // compute N[i][j]
247 for (i = 0; i < 64; ++i)
248 for (j = 0; j < 32; ++j)
249 *nptr++ = (int) (256.0 * cos(((16 + i) * ((j << 1) + 1)) * 0.0490873852123405));
250 initialized = 1;
251 }
252
253 // perform local initialization: clean the context and put the magic in it
254 for (i = 0; i < 2; ++i)
255 for (j = 1023; j >= 0; --j)
256 mp2->V[i][j] = 0;
257 mp2->Voffs = 0;
258 mp2->id = KJMP2_MAGIC;
259 }
260
kjmp2_get_sample_rate(const unsigned char * frame)261 int kjmp2_get_sample_rate(const unsigned char *frame) {
262 if (!frame)
263 return 0;
264 if (( frame[0] != 0xFF) // no valid syncword?
265 || ((frame[1] & 0xF6) != 0xF4) // no MPEG-1/2 Audio Layer II?
266 || ((frame[2] - 0x10) >= 0xE0)) // invalid bitrate?
267 return 0;
268 return sample_rates[(((frame[1] & 0x08) >> 1) ^ 4) // MPEG-1/2 switch
269 + ((frame[2] >> 2) & 3)]; // actual rate
270 }
271
272
273 ////////////////////////////////////////////////////////////////////////////////
274 // DECODE HELPER FUNCTIONS //
275 ////////////////////////////////////////////////////////////////////////////////
276
read_allocation(int sb,int b2_table)277 static const struct quantizer_spec* FASTCALL read_allocation(int sb, int b2_table) {
278 int table_idx = quant_lut_step3[b2_table][sb];
279 table_idx = quant_lut_step4[table_idx & 15][get_bits(table_idx >> 4)];
280 return table_idx ? (&quantizer_table[table_idx - 1]) : 0;
281 }
282
283
read_samples(const struct quantizer_spec * q,int scalefactor,int * sample)284 static void FASTCALL read_samples(const struct quantizer_spec *q, int scalefactor, int *sample) {
285 int idx, adj, scale;
286 register int val;
287 if (!q) {
288 // no bits allocated for this subband
289 sample[0] = sample[1] = sample[2] = 0;
290 return;
291 }
292
293 // resolve scalefactor
294 if (scalefactor == 63) {
295 scalefactor = 0;
296 } else {
297 adj = scalefactor / 3;
298 scalefactor = (scf_base[scalefactor % 3] + ((1 << adj) >> 1)) >> adj;
299 }
300
301 // decode samples
302 adj = q->nlevels;
303 if (q->grouping) {
304 // decode grouped samples
305 val = get_bits(q->cw_bits);
306 sample[0] = val % adj;
307 val /= adj;
308 sample[1] = val % adj;
309 sample[2] = val / adj;
310 } else {
311 // decode direct samples
312 for(idx = 0; idx < 3; ++idx)
313 sample[idx] = get_bits(q->cw_bits);
314 }
315
316 // postmultiply samples
317 scale = 65536 / (adj + 1);
318 adj = ((adj + 1) >> 1) - 1;
319 for (idx = 0; idx < 3; ++idx) {
320 // step 1: renormalization to [-1..1]
321 val = (adj - sample[idx]) * scale;
322 // step 2: apply scalefactor
323 sample[idx] = ( val * (scalefactor >> 12) // upper part
324 + ((val * (scalefactor & 4095) + 2048) >> 12)) // lower part
325 >> 12; // scale adjust
326 }
327 }
328
329
330 ////////////////////////////////////////////////////////////////////////////////
331 // FRAME DECODE FUNCTION //
332 ////////////////////////////////////////////////////////////////////////////////
333
334 static const struct quantizer_spec *allocation[2][32];
335 static int scfsi[2][32];
336 static int scalefactor[2][32][3];
337 static int sample[2][32][3];
338 static int U[512];
339
kjmp2_decode_frame(kjmp2_context_t * mp2,const unsigned char * frame,signed short * pcm)340 unsigned long kjmp2_decode_frame(
341 kjmp2_context_t *mp2,
342 const unsigned char *frame,
343 signed short *pcm
344 ) {
345 unsigned bit_rate_index_minus1;
346 unsigned sampling_frequency;
347 unsigned padding_bit;
348 unsigned mode;
349 unsigned long frame_size;
350 int bound, sblimit;
351 int sb, ch, gr, part, idx, nch, i, j, sum;
352 int table_idx;
353
354 // general sanity check
355 if (!initialized || !mp2 || (mp2->id != KJMP2_MAGIC) || !frame)
356 return 0;
357
358 // check for valid header: syncword OK, MPEG-Audio Layer 2
359 if ((frame[0] != 0xFF) || ((frame[1] & 0xF6) != 0xF4))
360 return 0;
361
362 // set up the bitstream reader
363 bit_window = frame[2] << 16;
364 bits_in_window = 8;
365 frame_pos = &frame[3];
366
367 // read the rest of the header
368 bit_rate_index_minus1 = get_bits(4) - 1;
369 if (bit_rate_index_minus1 > 13)
370 return 0; // invalid bit rate or 'free format'
371 sampling_frequency = get_bits(2);
372 if (sampling_frequency == 3)
373 return 0;
374 if ((frame[1] & 0x08) == 0) { // MPEG-2
375 sampling_frequency += 4;
376 bit_rate_index_minus1 += 14;
377 }
378 padding_bit = get_bits(1);
379 get_bits(1); // discard private_bit
380 mode = get_bits(2);
381
382 // parse the mode_extension, set up the stereo bound
383 if (mode == JOINT_STEREO) {
384 bound = (get_bits(2) + 1) << 2;
385 } else {
386 get_bits(2);
387 bound = (mode == MONO) ? 0 : 32;
388 }
389
390 // discard the last 4 bits of the header and the CRC value, if present
391 get_bits(4);
392 if ((frame[1] & 1) == 0)
393 get_bits(16);
394
395 // compute the frame size
396 frame_size = (144000 * bitrates[bit_rate_index_minus1]
397 / sample_rates[sampling_frequency]) + padding_bit;
398 if (!pcm)
399 return frame_size; // no decoding
400
401 // prepare the quantizer table lookups
402 if (sampling_frequency & 4) {
403 // MPEG-2 (LSR)
404 table_idx = 2;
405 sblimit = 30;
406 } else {
407 // MPEG-1
408 table_idx = (mode == MONO) ? 0 : 1;
409 table_idx = quant_lut_step1[table_idx][bit_rate_index_minus1];
410 table_idx = quant_lut_step2[table_idx][sampling_frequency];
411 sblimit = table_idx & 63;
412 table_idx >>= 6;
413 }
414 if (bound > sblimit)
415 bound = sblimit;
416
417 // read the allocation information
418 for (sb = 0; sb < bound; ++sb)
419 for (ch = 0; ch < 2; ++ch)
420 allocation[ch][sb] = read_allocation(sb, table_idx);
421 for (sb = bound; sb < sblimit; ++sb)
422 allocation[0][sb] = allocation[1][sb] = read_allocation(sb, table_idx);
423
424 // read scale factor selector information
425 nch = (mode == MONO) ? 1 : 2;
426 for (sb = 0; sb < sblimit; ++sb) {
427 for (ch = 0; ch < nch; ++ch)
428 if (allocation[ch][sb])
429 scfsi[ch][sb] = get_bits(2);
430 if (mode == MONO)
431 scfsi[1][sb] = scfsi[0][sb];
432 }
433
434 // read scale factors
435 for (sb = 0; sb < sblimit; ++sb) {
436 for (ch = 0; ch < nch; ++ch)
437 if (allocation[ch][sb]) {
438 switch (scfsi[ch][sb]) {
439 case 0: scalefactor[ch][sb][0] = get_bits(6);
440 scalefactor[ch][sb][1] = get_bits(6);
441 scalefactor[ch][sb][2] = get_bits(6);
442 break;
443 case 1: scalefactor[ch][sb][0] =
444 scalefactor[ch][sb][1] = get_bits(6);
445 scalefactor[ch][sb][2] = get_bits(6);
446 break;
447 case 2: scalefactor[ch][sb][0] =
448 scalefactor[ch][sb][1] =
449 scalefactor[ch][sb][2] = get_bits(6);
450 break;
451 case 3: scalefactor[ch][sb][0] = get_bits(6);
452 scalefactor[ch][sb][1] =
453 scalefactor[ch][sb][2] = get_bits(6);
454 break;
455 }
456 }
457 if (mode == MONO)
458 for (part = 0; part < 3; ++part)
459 scalefactor[1][sb][part] = scalefactor[0][sb][part];
460 }
461
462 // coefficient input and reconstruction
463 for (part = 0; part < 3; ++part)
464 for (gr = 0; gr < 4; ++gr) {
465
466 // read the samples
467 for (sb = 0; sb < bound; ++sb)
468 for (ch = 0; ch < 2; ++ch)
469 read_samples(allocation[ch][sb], scalefactor[ch][sb][part], &sample[ch][sb][0]);
470 for (sb = bound; sb < sblimit; ++sb) {
471 read_samples(allocation[0][sb], scalefactor[0][sb][part], &sample[0][sb][0]);
472 for (idx = 0; idx < 3; ++idx)
473 sample[1][sb][idx] = sample[0][sb][idx];
474 }
475 for (ch = 0; ch < 2; ++ch)
476 for (sb = sblimit; sb < 32; ++sb)
477 for (idx = 0; idx < 3; ++idx)
478 sample[ch][sb][idx] = 0;
479
480 // synthesis loop
481 for (idx = 0; idx < 3; ++idx) {
482 // shifting step
483 mp2->Voffs = table_idx = (mp2->Voffs - 64) & 1023;
484
485 for (ch = 0; ch < 2; ++ch) {
486 // matrixing
487 for (i = 0; i < 64; ++i) {
488 sum = 0;
489 for (j = 0; j < 32; ++j)
490 sum += N[i][j] * sample[ch][j][idx]; // 8b*15b=23b
491 // intermediate value is 28 bit (23 + 5), clamp to 14b
492 mp2->V[ch][table_idx + i] = (sum + 8192) >> 14;
493 }
494
495 // construction of U
496 for (i = 0; i < 8; ++i)
497 for (j = 0; j < 32; ++j) {
498 U[(i << 6) + j] = mp2->V[ch][(table_idx + (i << 7) + j ) & 1023];
499 U[(i << 6) + j + 32] = mp2->V[ch][(table_idx + (i << 7) + j + 96) & 1023];
500 }
501
502 // apply window
503 for (i = 0; i < 512; ++i)
504 U[i] = (U[i] * D[i] + 32) >> 6;
505
506 // output samples
507 for (j = 0; j < 32; ++j) {
508 sum = 0;
509 for (i = 0; i < 16; ++i)
510 sum -= U[(i << 5) + j];
511 sum = (sum + 8) >> 4;
512 if (sum < -32768) sum = -32768;
513 if (sum > 32767) sum = 32767;
514 pcm[(idx << 6) | (j << 1) | ch] = (signed short) sum;
515 }
516 } // end of synthesis channel loop
517 } // end of synthesis sub-block loop
518
519 // adjust PCM output pointer: decoded 3 * 32 = 96 stereo samples
520 pcm += 192;
521
522 } // decoding of the granule finished
523
524 return frame_size;
525 }
526