1 /*
2 * layer3.c: Mpeg Layer-3 audio decoder
3 *
4 * Copyright (C) 1999-2010 The L.A.M.E. project
5 *
6 * Initially written by Michael Hipp, see also AUTHORS and README.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
23 /* $Id$ */
24
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28
29 #include <stdlib.h>
30 #include "common.h"
31 #include "huffman.h"
32 #include "lame.h"
33 #include "machine.h"
34 #include "encoder.h"
35 #include "lame-analysis.h"
36 #include "decode_i386.h"
37 #include "layer3.h"
38
39 #ifdef WITH_DMALLOC
40 #include <dmalloc.h>
41 #endif
42
43
44 static int gd_are_hip_tables_layer3_initialized = 0;
45
46 static real ispow[8207];
47 static real aa_ca[8], aa_cs[8];
48 static real COS1[12][6];
49 static real win[4][36];
50 static real win1[4][36];
51 static real gainpow2[256 + 118 + 4];
52 static real COS9[9];
53 static real COS6_1, COS6_2;
54 static real tfcos36[9];
55 static real tfcos12[3];
56
57 struct bandInfoStruct {
58 short longIdx[23];
59 short longDiff[22];
60 short shortIdx[14];
61 short shortDiff[13];
62 };
63
64 static int longLimit[9][23];
65 static int shortLimit[9][14];
66
67 /* *INDENT-OFF* */
68
69 static const struct bandInfoStruct bandInfo[9] = {
70
71 /* MPEG 1.0 */
72 { {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
73 {4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158},
74 {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},
75 {4,4,4,4,6,8,10,12,14,18,22,30,56} } ,
76
77 { {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576},
78 {4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192},
79 {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},
80 {4,4,4,4,6,6,10,12,14,16,20,26,66} } ,
81
82 { {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576} ,
83 {4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26} ,
84 {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} ,
85 {4,4,4,4,6,8,12,16,20,26,34,42,12} } ,
86
87 /* MPEG 2.0 */
88 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
89 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } ,
90 {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} ,
91 {4,4,4,6,6,8,10,14,18,26,32,42,18 } } ,
92 /* docs: 332. mpg123: 330 */
93 { {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,332,394,464,540,576},
94 {6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,54,62,70,76,36 } ,
95 {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} ,
96 {4,4,4,6,8,10,12,14,18,24,32,44,12 } } ,
97
98 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
99 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 },
100 {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},
101 {4,4,4,6,8,10,12,14,18,24,30,40,18 } } ,
102 /* MPEG 2.5 */
103 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
104 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
105 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
106 {4,4,4,6,8,10,12,14,18,24,30,40,18} },
107 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
108 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
109 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
110 {4,4,4,6,8,10,12,14,18,24,30,40,18} },
111 { {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576},
112 {12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2},
113 {0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576},
114 {8,8,8,12,16,20,24,28,36,2,2,2,26} } ,
115 };
116 /* *INDENT-ON* */
117
118 static int mapbuf0[9][152];
119 static int mapbuf1[9][156];
120 static int mapbuf2[9][44];
121 static int *map[9][3];
122 static int *mapend[9][3];
123
124 static unsigned int n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
125 static unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
126
127 static real tan1_1[16], tan2_1[16], tan1_2[16], tan2_2[16];
128 static real pow1_1[2][16], pow2_1[2][16], pow1_2[2][16], pow2_2[2][16];
129
130 static unsigned int
get1bit(PMPSTR mp)131 get1bit(PMPSTR mp)
132 {
133 unsigned char rval;
134 rval = *mp->wordpointer << mp->bitindex;
135
136 mp->bitindex++;
137 mp->wordpointer += (mp->bitindex >> 3);
138 mp->bitindex &= 7;
139
140 return rval >> 7;
141 }
142
143 static real
get_gain(real const* gain_ptr, int idx, int* overflow)144 get_gain(real const* gain_ptr, int idx, int* overflow)
145 {
146 static const real* const gainpow2_end_ptr = gainpow2 + (sizeof(gainpow2)/sizeof(gainpow2[0])) -1;
147 real const * ptr = &gain_ptr[idx];
148 if (&gain_ptr[idx] > gainpow2_end_ptr) {
149 ptr = gainpow2_end_ptr;
150 if (overflow) *overflow = 1;
151 }
152 return *ptr;
153 }
154
155
156 /*
157 * init tables for layer-3
158 */
159 void
hip_init_tables_layer3(void)160 hip_init_tables_layer3(void)
161 {
162 int i, j, k;
163
164 if (gd_are_hip_tables_layer3_initialized) {
165 return;
166 }
167 gd_are_hip_tables_layer3_initialized = 1;
168
169 for (i = -256; i < 118 + 4; i++)
170 gainpow2[i + 256] = pow((double) 2.0, -0.25 * (double) (i + 210));
171
172 for (i = 0; i < 8207; i++)
173 ispow[i] = pow((double) i, (double) 4.0 / 3.0);
174
175 for (i = 0; i < 8; i++) {
176 static const double Ci[8] = { -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037 };
177 double sq = sqrt(1.0 + Ci[i] * Ci[i]);
178 aa_cs[i] = 1.0 / sq;
179 aa_ca[i] = Ci[i] / sq;
180 }
181
182 for (i = 0; i < 18; i++) {
183 win[0][i] = win[1][i] =
184 0.5 * sin(M_PI / 72.0 * (double) (2 * (i + 0) + 1)) / cos(M_PI *
185 (double) (2 * (i + 0) +
186 19) / 72.0);
187 win[0][i + 18] = win[3][i + 18] =
188 0.5 * sin(M_PI / 72.0 * (double) (2 * (i + 18) + 1)) / cos(M_PI *
189 (double) (2 * (i + 18) +
190 19) / 72.0);
191 }
192 for (i = 0; i < 6; i++) {
193 win[1][i + 18] = 0.5 / cos(M_PI * (double) (2 * (i + 18) + 19) / 72.0);
194 win[3][i + 12] = 0.5 / cos(M_PI * (double) (2 * (i + 12) + 19) / 72.0);
195 win[1][i + 24] =
196 0.5 * sin(M_PI / 24.0 * (double) (2 * i + 13)) / cos(M_PI *
197 (double) (2 * (i + 24) +
198 19) / 72.0);
199 win[1][i + 30] = win[3][i] = 0.0;
200 win[3][i + 6] =
201 0.5 * sin(M_PI / 24.0 * (double) (2 * i + 1)) / cos(M_PI * (double) (2 * (i + 6) + 19) /
202 72.0);
203 }
204
205 for (i = 0; i < 9; i++)
206 COS9[i] = cos(M_PI / 18.0 * (double) i);
207
208 for (i = 0; i < 9; i++)
209 tfcos36[i] = 0.5 / cos(M_PI * (double) (i * 2 + 1) / 36.0);
210 for (i = 0; i < 3; i++)
211 tfcos12[i] = 0.5 / cos(M_PI * (double) (i * 2 + 1) / 12.0);
212
213 COS6_1 = cos(M_PI / 6.0 * (double) 1);
214 COS6_2 = cos(M_PI / 6.0 * (double) 2);
215
216 for (i = 0; i < 12; i++) {
217 win[2][i] =
218 0.5 * sin(M_PI / 24.0 * (double) (2 * i + 1)) / cos(M_PI * (double) (2 * i + 7) / 24.0);
219 for (j = 0; j < 6; j++)
220 COS1[i][j] = cos(M_PI / 24.0 * (double) ((2 * i + 7) * (2 * j + 1)));
221 }
222
223 for (j = 0; j < 4; j++) {
224 static int const len[4] = { 36, 36, 12, 36 };
225 for (i = 0; i < len[j]; i += 2)
226 win1[j][i] = +win[j][i];
227 for (i = 1; i < len[j]; i += 2)
228 win1[j][i] = -win[j][i];
229 }
230
231 for (i = 0; i < 16; i++) {
232 double t = tan((double) i * M_PI / 12.0);
233 tan1_1[i] = t / (1.0 + t);
234 tan2_1[i] = 1.0 / (1.0 + t);
235 tan1_2[i] = M_SQRT2 * t / (1.0 + t);
236 tan2_2[i] = M_SQRT2 / (1.0 + t);
237
238 for (j = 0; j < 2; j++) {
239 double base = pow(2.0, -0.25 * (j + 1.0));
240 double p1 = 1.0, p2 = 1.0;
241 if (i > 0) {
242 if (i & 1)
243 p1 = pow(base, (i + 1.0) * 0.5);
244 else
245 p2 = pow(base, i * 0.5);
246 }
247 pow1_1[j][i] = p1;
248 pow2_1[j][i] = p2;
249 pow1_2[j][i] = M_SQRT2 * p1;
250 pow2_2[j][i] = M_SQRT2 * p2;
251 }
252 }
253
254 for (j = 0; j < 9; j++) {
255 struct bandInfoStruct const *bi = (struct bandInfoStruct const *) &bandInfo[j];
256 int *mp;
257 int cb, lwin;
258 short const *bdf;
259 int switch_idx = (j < 3) ? 8 : 6;
260
261 mp = map[j][0] = mapbuf0[j];
262 bdf = bi->longDiff;
263 for (i = 0, cb = 0; cb < switch_idx; cb++, i += *bdf++) {
264 *mp++ = (*bdf) >> 1;
265 *mp++ = i;
266 *mp++ = 3;
267 *mp++ = cb;
268 }
269 bdf = bi->shortDiff + 3;
270 for (cb = 3; cb < 13; cb++) {
271 int l = (*bdf++) >> 1;
272 for (lwin = 0; lwin < 3; lwin++) {
273 *mp++ = l;
274 *mp++ = i + lwin;
275 *mp++ = lwin;
276 *mp++ = cb;
277 }
278 i += 6 * l;
279 }
280 mapend[j][0] = mp;
281
282 mp = map[j][1] = mapbuf1[j];
283 bdf = bi->shortDiff + 0;
284 for (i = 0, cb = 0; cb < 13; cb++) {
285 int l = (*bdf++) >> 1;
286 for (lwin = 0; lwin < 3; lwin++) {
287 *mp++ = l;
288 *mp++ = i + lwin;
289 *mp++ = lwin;
290 *mp++ = cb;
291 }
292 i += 6 * l;
293 }
294 mapend[j][1] = mp;
295
296 mp = map[j][2] = mapbuf2[j];
297 bdf = bi->longDiff;
298 for (cb = 0; cb < 22; cb++) {
299 *mp++ = (*bdf++) >> 1;
300 *mp++ = cb;
301 }
302 mapend[j][2] = mp;
303
304 }
305
306 for (j = 0; j < 9; j++) {
307 for (i = 0; i < 23; i++) {
308 longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
309 if (longLimit[j][i] > SBLIMIT)
310 longLimit[j][i] = SBLIMIT;
311 }
312 for (i = 0; i < 14; i++) {
313 shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
314 if (shortLimit[j][i] > SBLIMIT)
315 shortLimit[j][i] = SBLIMIT;
316 }
317 }
318
319 for (i = 0; i < 5; i++) {
320 for (j = 0; j < 6; j++) {
321 for (k = 0; k < 6; k++) {
322 int n = k + j * 6 + i * 36;
323 i_slen2[n] = i | (j << 3) | (k << 6) | (3 << 12);
324 }
325 }
326 }
327 for (i = 0; i < 4; i++) {
328 for (j = 0; j < 4; j++) {
329 for (k = 0; k < 4; k++) {
330 int n = k + j * 4 + i * 16;
331 i_slen2[n + 180] = i | (j << 3) | (k << 6) | (4 << 12);
332 }
333 }
334 }
335 for (i = 0; i < 4; i++) {
336 for (j = 0; j < 3; j++) {
337 int n = j + i * 3;
338 i_slen2[n + 244] = i | (j << 3) | (5 << 12);
339 n_slen2[n + 500] = i | (j << 3) | (2 << 12) | (1 << 15);
340 }
341 }
342
343 for (i = 0; i < 5; i++) {
344 for (j = 0; j < 5; j++) {
345 for (k = 0; k < 4; k++) {
346 int l;
347 for (l = 0; l < 4; l++) {
348 int n = l + k * 4 + j * 16 + i * 80;
349 n_slen2[n] = i | (j << 3) | (k << 6) | (l << 9) | (0 << 12);
350 }
351 }
352 }
353 }
354 for (i = 0; i < 5; i++) {
355 for (j = 0; j < 5; j++) {
356 for (k = 0; k < 4; k++) {
357 int n = k + j * 4 + i * 20;
358 n_slen2[n + 400] = i | (j << 3) | (k << 6) | (1 << 12);
359 }
360 }
361 }
362 }
363
364 /*
365 * read additional side information
366 */
367
368 static void
III_get_side_info_1(PMPSTR mp, int stereo, int ms_stereo, long sfreq, int single)369 III_get_side_info_1(PMPSTR mp, int stereo,
370 int ms_stereo, long sfreq, int single)
371 {
372 int ch, gr;
373 int powdiff = (single == 3) ? 4 : 0;
374
375 mp->sideinfo.main_data_begin = getbits(mp, 9);
376 if (stereo == 1)
377 mp->sideinfo.private_bits = getbits_fast(mp, 5);
378 else
379 mp->sideinfo.private_bits = getbits_fast(mp, 3);
380
381 for (ch = 0; ch < stereo; ch++) {
382 mp->sideinfo.ch[ch].gr[0].scfsi = -1;
383 mp->sideinfo.ch[ch].gr[1].scfsi = getbits_fast(mp, 4);
384 }
385
386 for (gr = 0; gr < 2; gr++) {
387 for (ch = 0; ch < stereo; ch++) {
388 struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[gr]);
389
390 gr_infos->part2_3_length = getbits(mp, 12);
391 gr_infos->big_values = getbits_fast(mp, 9);
392 if (gr_infos->big_values > 288) {
393 lame_report_fnc(mp->report_err, "big_values too large! %i\n", gr_infos->big_values);
394 gr_infos->big_values = 288;
395 }
396 {
397 unsigned int qss = getbits_fast(mp, 8);
398 gr_infos->pow2gain = gainpow2 + 256 - qss + powdiff;
399 if (mp->pinfo != NULL) {
400 mp->pinfo->qss[gr][ch] = qss;
401 }
402 }
403 if (ms_stereo)
404 gr_infos->pow2gain += 2;
405 gr_infos->scalefac_compress = getbits_fast(mp, 4);
406 /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
407 if (get1bit(mp)) {
408 int i;
409 gr_infos->block_type = getbits_fast(mp, 2);
410 gr_infos->mixed_block_flag = get1bit(mp);
411 gr_infos->table_select[0] = getbits_fast(mp, 5);
412 gr_infos->table_select[1] = getbits_fast(mp, 5);
413
414
415 /*
416 * table_select[2] not needed, because there is no region2,
417 * but to satisfy some verifications tools we set it either.
418 */
419 gr_infos->table_select[2] = 0;
420 for (i = 0; i < 3; i++) {
421 unsigned int sbg = (getbits_fast(mp, 3) << 3);
422 gr_infos->full_gain[i] = gr_infos->pow2gain + sbg;
423 if (mp->pinfo != NULL)
424 mp->pinfo->sub_gain[gr][ch][i] = sbg / 8;
425 }
426
427 if (gr_infos->block_type == 0) {
428 lame_report_fnc(mp->report_err, "Blocktype == 0 and window-switching == 1 not allowed.\n");
429 /* error seems to be very good recoverable, so don't exit */
430 /* exit(1); */
431 }
432 /* region_count/start parameters are implicit in this case. */
433 gr_infos->region1start = 36 >> 1;
434 gr_infos->region2start = 576 >> 1;
435 }
436 else {
437 unsigned int i, r0c, r1c, region0index, region1index;
438 for (i = 0; i < 3; i++)
439 gr_infos->table_select[i] = getbits_fast(mp, 5);
440 r0c = getbits_fast(mp, 4);
441 r1c = getbits_fast(mp, 3);
442 region0index = r0c+1;
443 if (region0index > 22) {
444 lame_report_fnc(mp->report_err, "region0index=%d > 22\n", region0index);
445 region0index = 22;
446 }
447 region1index = r0c+1 + r1c+1;
448 if (region1index > 22) {
449 lame_report_fnc(mp->report_err, "region1index=%d > 22\n", region1index);
450 region1index = 22;
451 }
452 gr_infos->region1start = bandInfo[sfreq].longIdx[region0index] >> 1;
453 gr_infos->region2start = bandInfo[sfreq].longIdx[region1index] >> 1;
454 gr_infos->block_type = 0;
455 gr_infos->mixed_block_flag = 0;
456 }
457 gr_infos->preflag = get1bit(mp);
458 gr_infos->scalefac_scale = get1bit(mp);
459 gr_infos->count1table_select = get1bit(mp);
460 }
461 }
462 }
463
464 /*
465 * Side Info for MPEG 2.0 / LSF
466 */
467 static void
III_get_side_info_2(PMPSTR mp, int stereo, int ms_stereo, long sfreq, int single)468 III_get_side_info_2(PMPSTR mp, int stereo, int ms_stereo, long sfreq, int single)
469 {
470 int ch;
471 int powdiff = (single == 3) ? 4 : 0;
472
473 mp->sideinfo.main_data_begin = getbits(mp, 8);
474
475 if (stereo == 1)
476 mp->sideinfo.private_bits = get1bit(mp);
477 else
478 mp->sideinfo.private_bits = getbits_fast(mp, 2);
479
480 for (ch = 0; ch < stereo; ch++) {
481 struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[0]);
482 unsigned int qss;
483
484 gr_infos->part2_3_length = getbits(mp, 12);
485 gr_infos->big_values = getbits_fast(mp, 9);
486 if (gr_infos->big_values > 288) {
487 lame_report_fnc(mp->report_err, "big_values too large! %i\n", gr_infos->big_values);
488 gr_infos->big_values = 288;
489 }
490 qss = getbits_fast(mp, 8);
491 gr_infos->pow2gain = gainpow2 + 256 - qss + powdiff;
492 if (mp->pinfo != NULL) {
493 mp->pinfo->qss[0][ch] = qss;
494 }
495
496
497 if (ms_stereo)
498 gr_infos->pow2gain += 2;
499 gr_infos->scalefac_compress = getbits(mp, 9);
500 /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
501 if (get1bit(mp)) {
502 int i;
503 gr_infos->block_type = getbits_fast(mp, 2);
504 gr_infos->mixed_block_flag = get1bit(mp);
505 gr_infos->table_select[0] = getbits_fast(mp, 5);
506 gr_infos->table_select[1] = getbits_fast(mp, 5);
507 /*
508 * table_select[2] not needed, because there is no region2,
509 * but to satisfy some verifications tools we set it either.
510 */
511 gr_infos->table_select[2] = 0;
512 for (i = 0; i < 3; i++) {
513 unsigned int sbg = (getbits_fast(mp, 3) << 3);
514 gr_infos->full_gain[i] = gr_infos->pow2gain + sbg;
515 if (mp->pinfo != NULL)
516 mp->pinfo->sub_gain[0][ch][i] = sbg / 8;
517
518 }
519
520 if (gr_infos->block_type == 0) {
521 lame_report_fnc(mp->report_err, "Blocktype == 0 and window-switching == 1 not allowed.\n");
522 /* error seems to be very good recoverable, so don't exit */
523 /* exit(1); */
524 }
525 /* region_count/start parameters are implicit in this case. */
526 if (gr_infos->block_type == 2) {
527 if (gr_infos->mixed_block_flag == 0)
528 gr_infos->region1start = 36 >> 1;
529 else
530 gr_infos->region1start = 48 >> 1;
531 }
532 else
533 gr_infos->region1start = 54 >> 1;
534 if (sfreq == 8)
535 gr_infos->region1start *= 2;
536 gr_infos->region2start = 576 >> 1;
537 }
538 else {
539 unsigned int i, r0c, r1c, region0index, region1index;
540 for (i = 0; i < 3; i++)
541 gr_infos->table_select[i] = getbits_fast(mp, 5);
542 r0c = getbits_fast(mp, 4);
543 r1c = getbits_fast(mp, 3);
544 region0index = r0c+1;
545 if (region0index > 22) {
546 lame_report_fnc(mp->report_err, "region0index=%d > 22\n", region0index);
547 region0index = 22;
548 }
549 region1index = r0c+1 + r1c+1;
550 if (region1index > 22) {
551 lame_report_fnc(mp->report_err, "region1index=%d > 22\n", region1index);
552 region1index = 22;
553 }
554 gr_infos->region1start = bandInfo[sfreq].longIdx[region0index] >> 1;
555 gr_infos->region2start = bandInfo[sfreq].longIdx[region1index] >> 1;
556 gr_infos->block_type = 0;
557 gr_infos->mixed_block_flag = 0;
558 }
559 gr_infos->scalefac_scale = get1bit(mp);
560 gr_infos->count1table_select = get1bit(mp);
561 }
562 }
563
564 /*
565 * read scalefactors
566 */
567
568 static int
III_get_scale_factors_1(PMPSTR mp, int *scf, struct gr_info_s *gr_infos)569 III_get_scale_factors_1(PMPSTR mp, int *scf, struct gr_info_s *gr_infos)
570 {
571 static const unsigned char slen[2][16] = {
572 {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
573 {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
574 };
575 int numbits;
576 int num0 = slen[0][gr_infos->scalefac_compress];
577 int num1 = slen[1][gr_infos->scalefac_compress];
578
579 if (gr_infos->block_type == 2) {
580 int i = 18;
581 numbits = (num0 + num1) * 18;
582
583 if (gr_infos->mixed_block_flag) {
584 for (i = 8; i; i--)
585 *scf++ = getbits_fast(mp, num0);
586 i = 9;
587 numbits -= num0; /* num0 * 17 + num1 * 18 */
588 }
589
590 for (; i; i--)
591 *scf++ = getbits_fast(mp, num0);
592 for (i = 18; i; i--)
593 *scf++ = getbits_fast(mp, num1);
594 *scf++ = 0;
595 *scf++ = 0;
596 *scf++ = 0; /* short[13][0..2] = 0 */
597 }
598 else {
599 int i;
600 int scfsi = gr_infos->scfsi;
601
602 if (scfsi < 0) { /* scfsi < 0 => granule == 0 */
603 for (i = 11; i; i--)
604 *scf++ = getbits_fast(mp, num0);
605 for (i = 10; i; i--)
606 *scf++ = getbits_fast(mp, num1);
607 numbits = (num0 + num1) * 10 + num0;
608 }
609 else {
610 numbits = 0;
611 if (!(scfsi & 0x8)) {
612 for (i = 6; i; i--)
613 *scf++ = getbits_fast(mp, num0);
614 numbits += num0 * 6;
615 }
616 else {
617 scf += 6;
618 }
619
620 if (!(scfsi & 0x4)) {
621 for (i = 5; i; i--)
622 *scf++ = getbits_fast(mp, num0);
623 numbits += num0 * 5;
624 }
625 else {
626 scf += 5;
627 }
628
629 if (!(scfsi & 0x2)) {
630 for (i = 5; i; i--)
631 *scf++ = getbits_fast(mp, num1);
632 numbits += num1 * 5;
633 }
634 else {
635 scf += 5;
636 }
637
638 if (!(scfsi & 0x1)) {
639 for (i = 5; i; i--)
640 *scf++ = getbits_fast(mp, num1);
641 numbits += num1 * 5;
642 }
643 else {
644 scf += 5;
645 }
646 }
647
648 *scf++ = 0; /* no l[21] in original sources */
649 }
650 return numbits;
651 }
652
653
654 static int
III_get_scale_factors_2(PMPSTR mp, int *scf, struct gr_info_s *gr_infos, int i_stereo)655 III_get_scale_factors_2(PMPSTR mp, int *scf, struct gr_info_s *gr_infos, int i_stereo)
656 {
657 unsigned char const *pnt;
658 int i, j;
659 unsigned int slen;
660 int n = 0;
661 int numbits = 0;
662
663 /* *INDENT-OFF* */
664 static const unsigned char stab[3][6][4] = {
665 { { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0} ,
666 { 7, 7, 7,0 } , { 6, 6, 6,3 } , { 8, 8,5,0} } ,
667 { { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0} ,
668 {12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0} } ,
669 { { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0} ,
670 { 6,15,12,0 } , { 6,12, 9,6 } , { 6,18,9,0} } };
671 /* *INDENT-ON* */
672
673 if (i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */
674 slen = i_slen2[gr_infos->scalefac_compress >> 1];
675 else
676 slen = n_slen2[gr_infos->scalefac_compress];
677
678 gr_infos->preflag = (slen >> 15) & 0x1;
679
680 n = 0;
681 if (gr_infos->block_type == 2) {
682 n++;
683 if (gr_infos->mixed_block_flag)
684 n++;
685 }
686
687 pnt = (unsigned char const *) stab[n][(slen >> 12) & 0x7];
688
689 for (i = 0; i < 4; i++) {
690 int num = slen & 0x7;
691 slen >>= 3;
692 if (num) {
693 for (j = 0; j < (int) (pnt[i]); j++)
694 *scf++ = getbits_fast(mp, num);
695 numbits += pnt[i] * num;
696 }
697 else {
698 for (j = 0; j < (int) (pnt[i]); j++)
699 *scf++ = 0;
700 }
701 }
702
703 n = (n << 1) + 1;
704 for (i = 0; i < n; i++)
705 *scf++ = 0;
706
707 return numbits;
708 }
709
710 /* *INDENT-OFF* */
711 static const int pretab1 [22] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0}; /* char enough ? */
712 static const int pretab2 [22] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
713 /* *INDENT-ON* */
714
715 /*
716 * don't forget to apply the same changes to III_dequantize_sample_ms() !!!
717 */
718 static int
III_dequantize_sample(PMPSTR mp, real xr[SBLIMIT][SSLIMIT], int *scf, struct gr_info_s *gr_infos, int sfreq, int part2bits)719 III_dequantize_sample(PMPSTR mp, real xr[SBLIMIT][SSLIMIT], int *scf,
720 struct gr_info_s *gr_infos, int sfreq, int part2bits)
721 {
722 int shift = 1 + gr_infos->scalefac_scale;
723 real *xrpnt = (real *) xr, xr_value=0;
724 int l[3], l3;
725 int part2remain = gr_infos->part2_3_length - part2bits;
726 int *me;
727 real const * const xr_endptr = &xr[SBLIMIT-1][SSLIMIT-1];
728
729 int isbug = 0;
730 int bobug = 0;
731 int bobug_sb = 0, bobug_l3=0;
732 #define BUFFER_OVERFLOW_BUG() if(!bobug){bobug=1;bobug_sb=cb;bobug_l3=l3;}else
733
734 /* lame_report_fnc(mp->report_dbg,"part2remain = %d, gr_infos->part2_3_length = %d, part2bits = %d\n",
735 part2remain, gr_infos->part2_3_length, part2bits); */
736
737 {
738 int i;
739
740 for (i = (&xr[SBLIMIT][0] - xrpnt) >> 1; i > 0; i--) {
741 *xrpnt++ = 0.0;
742 *xrpnt++ = 0.0;
743 }
744
745 xrpnt = (real *) xr;
746 }
747
748 {
749 int bv = gr_infos->big_values;
750 int region1 = gr_infos->region1start;
751 int region2 = gr_infos->region2start;
752
753 l3 = ((576 >> 1) - bv) >> 1;
754 /*
755 * we may lose the 'odd' bit here !!
756 * check this later again
757 */
758 if (bv <= region1) {
759 l[0] = bv;
760 l[1] = 0;
761 l[2] = 0;
762 }
763 else {
764 l[0] = region1;
765 if (bv <= region2) {
766 l[1] = bv - l[0];
767 l[2] = 0;
768 }
769 else {
770 l[1] = region2 - l[0];
771 l[2] = bv - region2;
772 }
773 }
774 }
775 /* MDH crash fix */
776 {
777 int i;
778 for (i = 0; i < 3; i++) {
779 if (l[i] < 0) {
780 lame_report_fnc(mp->report_err, "hip: Bogus region length (%d)\n", l[i]);
781 l[i] = 0;
782 }
783 }
784 }
785 /* end MDH crash fix */
786
787 if (gr_infos->block_type == 2) {
788 /*
789 * decoding with short or mixed mode BandIndex table
790 */
791 int i, max[4];
792 int step = 0, lwin = 0, cb = 0;
793 real v = 0.0;
794 int *m, mc;
795
796 if (gr_infos->mixed_block_flag) {
797 max[3] = -1;
798 max[0] = max[1] = max[2] = 2;
799 m = map[sfreq][0];
800 me = mapend[sfreq][0];
801 }
802 else {
803 max[0] = max[1] = max[2] = max[3] = -1;
804 /* max[3] not really needed in this case */
805 m = map[sfreq][1];
806 me = mapend[sfreq][1];
807 }
808
809 mc = 0;
810 for (i = 0; i < 2; i++) {
811 int lp = l[i];
812 struct newhuff const *h = (struct newhuff const *) (ht + gr_infos->table_select[i]);
813 for (; lp; lp--, mc--) {
814 int x, y;
815 if ((!mc)) {
816 mc = *m++;
817 xrpnt = ((real *) xr) + (*m++);
818 lwin = *m++;
819 cb = *m++;
820 if (lwin == 3) {
821 v = get_gain(gr_infos->pow2gain, (*scf++) << shift, &isbug);
822 step = 1;
823 }
824 else {
825 v = get_gain(gr_infos->full_gain[lwin], (*scf++) << shift, &isbug);
826 step = 3;
827 }
828 }
829 {
830 short const *val = (short const *) h->table;
831 while ((y = *val++) < 0) {
832 if (get1bit(mp))
833 val -= y;
834 part2remain--;
835 }
836 x = y >> 4;
837 y &= 0xf;
838 }
839 if (x == 15) {
840 max[lwin] = cb;
841 part2remain -= h->linbits + 1;
842 x += getbits(mp, (int) h->linbits);
843 if (get1bit(mp))
844 xr_value = -ispow[x] * v;
845 else
846 xr_value = ispow[x] * v;
847 }
848 else if (x) {
849 max[lwin] = cb;
850 if (get1bit(mp))
851 xr_value = -ispow[x] * v;
852 else
853 xr_value = ispow[x] * v;
854 part2remain--;
855 }
856 else
857 xr_value = 0.0;
858
859 if (xrpnt <= xr_endptr)
860 *xrpnt = xr_value;
861 else
862 BUFFER_OVERFLOW_BUG();
863 xrpnt += step;
864 if (y == 15) {
865 max[lwin] = cb;
866 part2remain -= h->linbits + 1;
867 y += getbits(mp, (int) h->linbits);
868 if (get1bit(mp))
869 xr_value = -ispow[y] * v;
870 else
871 xr_value = ispow[y] * v;
872 }
873 else if (y) {
874 max[lwin] = cb;
875 if (get1bit(mp))
876 xr_value = -ispow[y] * v;
877 else
878 xr_value = ispow[y] * v;
879 part2remain--;
880 }
881 else
882 xr_value = 0.0;
883
884 if (xrpnt <= xr_endptr)
885 *xrpnt = xr_value;
886 else
887 BUFFER_OVERFLOW_BUG();
888 xrpnt += step;
889 }
890 }
891 for (; (l3 > 0) && (part2remain > 0); l3--) {
892 struct newhuff const *h = (struct newhuff const *) (htc + gr_infos->count1table_select);
893 short const *val = (short const *) h->table;
894 short a;
895
896 while ((a = *val++) < 0) {
897 part2remain--;
898 if (part2remain < 0) {
899 part2remain++;
900 a = 0;
901 break;
902 }
903 if (get1bit(mp))
904 val -= a;
905 }
906 for (i = 0; i < 4; i++) {
907 if (!(i & 1)) {
908 if (!mc) {
909 mc = *m++;
910 xrpnt = ((real *) xr) + (*m++);
911 lwin = *m++;
912 cb = *m++;
913 if (lwin == 3) {
914 v = get_gain(gr_infos->pow2gain, (*scf++) << shift, &isbug);
915 step = 1;
916 }
917 else {
918 v = get_gain(gr_infos->full_gain[lwin], (*scf++) << shift, &isbug);
919 step = 3;
920 }
921 }
922 mc--;
923 }
924 if ((a & (0x8 >> i))) {
925 max[lwin] = cb;
926 part2remain--;
927 if (part2remain < 0) {
928 part2remain++;
929 break;
930 }
931 if (get1bit(mp))
932 xr_value = -v;
933 else
934 xr_value = v;
935 }
936 else
937 xr_value = 0.0;
938
939 if (xrpnt <= xr_endptr)
940 *xrpnt = xr_value;
941 else
942 BUFFER_OVERFLOW_BUG();
943 xrpnt += step;
944 }
945 }
946 while (m < me) {
947 if (!mc) {
948 mc = *m++;
949 xrpnt = ((real *) xr) + *m++;
950 if ((*m++) == 3)
951 step = 1;
952 else
953 step = 3;
954 m++; /* cb */
955 }
956 mc--;
957 if (xrpnt <= xr_endptr)
958 *xrpnt = 0.0;
959 else
960 BUFFER_OVERFLOW_BUG();
961 xrpnt += step;
962 if (xrpnt <= xr_endptr)
963 *xrpnt = 0.0;
964 else
965 BUFFER_OVERFLOW_BUG();
966 xrpnt += step;
967 /* we could add a little opt. here:
968 * if we finished a band for window 3 or a long band
969 * further bands could copied in a simple loop without a
970 * special 'map' decoding
971 */
972 }
973
974 gr_infos->maxband[0] = max[0] + 1;
975 gr_infos->maxband[1] = max[1] + 1;
976 gr_infos->maxband[2] = max[2] + 1;
977 gr_infos->maxbandl = max[3] + 1;
978
979 {
980 int rmax = max[0] > max[1] ? max[0] : max[1];
981 rmax = (rmax > max[2] ? rmax : max[2]) + 1;
982 gr_infos->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3] + 1];
983 }
984
985 }
986 else {
987 /*
988 * decoding with 'long' BandIndex table (block_type != 2)
989 */
990 int const *pretab = (int const *) (gr_infos->preflag ? pretab1 : pretab2);
991 int i, max = -1;
992 int cb = 0;
993 int *m = map[sfreq][2];
994 real v = 0.0;
995 int mc = 0;
996
997 /*
998 * long hash table values
999 */
1000 for (i = 0; i < 3; i++) {
1001 int lp = l[i];
1002 struct newhuff const *h = (struct newhuff const *) (ht + gr_infos->table_select[i]);
1003
1004 for (; lp; lp--, mc--) {
1005 int x, y;
1006
1007 if (!mc) {
1008 mc = *m++;
1009 v = get_gain(gr_infos->pow2gain, ((*scf++) + (*pretab++)) << shift, &isbug);
1010 cb = *m++;
1011 }
1012 {
1013 short const *val = (short const *) h->table;
1014 while ((y = *val++) < 0) {
1015 if (get1bit(mp))
1016 val -= y;
1017 part2remain--;
1018 }
1019 x = y >> 4;
1020 y &= 0xf;
1021 }
1022 if (x == 15) {
1023 max = cb;
1024 part2remain -= h->linbits + 1;
1025 x += getbits(mp, (int) h->linbits);
1026 if (get1bit(mp))
1027 xr_value = -ispow[x] * v;
1028 else
1029 xr_value = ispow[x] * v;
1030 }
1031 else if (x) {
1032 max = cb;
1033 if (get1bit(mp))
1034 xr_value = -ispow[x] * v;
1035 else
1036 xr_value = ispow[x] * v;
1037 part2remain--;
1038 }
1039 else
1040 xr_value = 0.0;
1041
1042 if (xrpnt <= xr_endptr)
1043 *xrpnt++ = xr_value;
1044 else
1045 BUFFER_OVERFLOW_BUG();
1046
1047 if (y == 15) {
1048 max = cb;
1049 part2remain -= h->linbits + 1;
1050 y += getbits(mp, (int) h->linbits);
1051 if (get1bit(mp))
1052 xr_value = -ispow[y] * v;
1053 else
1054 xr_value = ispow[y] * v;
1055 }
1056 else if (y) {
1057 max = cb;
1058 if (get1bit(mp))
1059 xr_value = -ispow[y] * v;
1060 else
1061 xr_value = ispow[y] * v;
1062 part2remain--;
1063 }
1064 else
1065 xr_value = 0.0;
1066
1067 if (xrpnt <= xr_endptr)
1068 *xrpnt++ = xr_value;
1069 else
1070 BUFFER_OVERFLOW_BUG();
1071 }
1072 }
1073
1074 /*
1075 * short (count1table) values
1076 */
1077 for (; l3 && (part2remain > 0); l3--) {
1078 struct newhuff const *h = (struct newhuff const *) (htc + gr_infos->count1table_select);
1079 short const *val = (short const *) h->table;
1080 short a;
1081
1082 while ((a = *val++) < 0) {
1083 part2remain--;
1084 if (part2remain < 0) {
1085 part2remain++;
1086 a = 0;
1087 break;
1088 }
1089 if (get1bit(mp))
1090 val -= a;
1091 }
1092 for (i = 0; i < 4; i++) {
1093 if (!(i & 1)) {
1094 if (!mc) {
1095 mc = *m++;
1096 cb = *m++;
1097 v = get_gain(gr_infos->pow2gain, ((*scf++) + (*pretab++)) << shift, &isbug);
1098 }
1099 mc--;
1100 }
1101 if ((a & (0x8 >> i))) {
1102 max = cb;
1103 part2remain--;
1104 if (part2remain < 0) {
1105 part2remain++;
1106 break;
1107 }
1108 if (get1bit(mp))
1109 xr_value = -v;
1110 else
1111 xr_value = v;
1112 }
1113 else
1114 xr_value = 0.0;
1115
1116 if (xrpnt <= xr_endptr)
1117 *xrpnt++ = xr_value;
1118 else
1119 BUFFER_OVERFLOW_BUG();
1120 }
1121 }
1122
1123 /*
1124 * zero part
1125 */
1126 while (xrpnt <= xr_endptr)
1127 *xrpnt++ = 0.0;
1128
1129 gr_infos->maxbandl = max + 1;
1130 gr_infos->maxb = longLimit[sfreq][gr_infos->maxbandl];
1131 }
1132 #undef BUFFER_OVERFLOW_BUG
1133 if (bobug) {
1134 /* well, there was a bug report, where this happened!
1135 The problem was, that mixed blocks summed up to over 576,
1136 because of a wrong long/short switching index.
1137 It's likely, that the buffer overflow is fixed now, after correcting mixed block map.
1138 */
1139 lame_report_fnc
1140 (mp->report_err
1141 ,"hip: OOPS, part2remain=%d l3=%d cb=%d bv=%d region1=%d region2=%d b-type=%d mixed=%d\n"
1142 ,part2remain
1143 ,bobug_l3
1144 ,bobug_sb
1145 ,gr_infos->big_values
1146 ,gr_infos->region1start
1147 ,gr_infos->region2start
1148 ,gr_infos->block_type
1149 ,gr_infos->mixed_block_flag
1150 );
1151 }
1152 if (isbug) {
1153 /* there is a bug report, where there is trouble with IS coded short block gain.
1154 Is intensity stereo coding implementation correct? Likely not.
1155 */
1156 int i_stereo = 0;
1157 if (mp->fr.mode == MPG_MD_JOINT_STEREO) {
1158 i_stereo = mp->fr.mode_ext & 0x1;
1159 }
1160 lame_report_fnc
1161 (mp->report_err
1162 ,"hip: OOPS, 'gainpow2' buffer overflow lsf=%d i-stereo=%d b-type=%d mixed=%d\n"
1163 ,mp->fr.lsf
1164 ,i_stereo
1165 ,gr_infos->block_type
1166 ,gr_infos->mixed_block_flag
1167 );
1168 }
1169
1170 while (part2remain > 16) {
1171 getbits(mp, 16); /* Dismiss stuffing Bits */
1172 part2remain -= 16;
1173 }
1174 if (part2remain > 0)
1175 getbits(mp, part2remain);
1176 else if (part2remain < 0) {
1177 lame_report_fnc(mp->report_err, "hip: Can't rewind stream by %d bits!\n", -part2remain);
1178 return 1; /* -> error */
1179 }
1180 return 0;
1181 }
1182
1183 /* intensity position, transmitted via a scalefactor value, allowed range is 0 - 15 */
1184 static
scalefac_to_is_pos(int sf)1185 int scalefac_to_is_pos(int sf)
1186 {
1187 if (0 <= sf && sf <= 15)
1188 return sf;
1189 return (sf < 0 ? 0 : 15);
1190 }
1191
1192 /*
1193 * III_stereo: calculate real channel values for Joint-I-Stereo-mode
1194 */
1195 static void
III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT], int *scalefac, struct gr_info_s *gr_infos, int sfreq, int ms_stereo, int lsf)1196 III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT], int *scalefac,
1197 struct gr_info_s *gr_infos, int sfreq, int ms_stereo, int lsf)
1198 {
1199 real(*xr)[SBLIMIT * SSLIMIT] = (real(*)[SBLIMIT * SSLIMIT]) xr_buf;
1200 struct bandInfoStruct const *bi = (struct bandInfoStruct const *) &bandInfo[sfreq];
1201 real *tabl1, *tabl2;
1202
1203 if (lsf) {
1204 int p = gr_infos->scalefac_compress & 0x1;
1205 if (ms_stereo) {
1206 tabl1 = pow1_2[p];
1207 tabl2 = pow2_2[p];
1208 }
1209 else {
1210 tabl1 = pow1_1[p];
1211 tabl2 = pow2_1[p];
1212 }
1213 }
1214 else {
1215 if (ms_stereo) {
1216 tabl1 = tan1_2;
1217 tabl2 = tan2_2;
1218 }
1219 else {
1220 tabl1 = tan1_1;
1221 tabl2 = tan2_1;
1222 }
1223 }
1224
1225 if (gr_infos->block_type == 2) {
1226 int lwin, do_l = 0;
1227 if (gr_infos->mixed_block_flag)
1228 do_l = 1;
1229
1230 for (lwin = 0; lwin < 3; lwin++) { /* process each window */
1231 /* get first band with zero values */
1232 int is_p, sb, idx, sfb = gr_infos->maxband[lwin]; /* sfb is minimal 3 for mixed mode */
1233 if (sfb > 3)
1234 do_l = 0;
1235
1236 for (; sfb < 12; sfb++) {
1237 is_p = scalefac[sfb * 3 + lwin - gr_infos->mixed_block_flag]; /* scale: 0-15 */
1238 is_p = scalefac_to_is_pos(is_p);
1239 if (is_p != 7) {
1240 real t1, t2;
1241 sb = bi->shortDiff[sfb];
1242 idx = bi->shortIdx[sfb] + lwin;
1243 t1 = tabl1[is_p];
1244 t2 = tabl2[is_p];
1245 for (; sb > 0; sb--, idx += 3) {
1246 real v = xr[0][idx];
1247 xr[0][idx] = v * t1;
1248 xr[1][idx] = v * t2;
1249 }
1250 }
1251 }
1252
1253 #if 1
1254 /* in the original: copy 10 to 11 , here: copy 11 to 12
1255 maybe still wrong??? (copy 12 to 13?) */
1256 is_p = scalefac[11 * 3 + lwin - gr_infos->mixed_block_flag]; /* scale: 0-15 */
1257 sb = bi->shortDiff[12];
1258 idx = bi->shortIdx[12] + lwin;
1259 #else
1260 is_p = scalefac[10 * 3 + lwin - gr_infos->mixed_block_flag]; /* scale: 0-15 */
1261 sb = bi->shortDiff[11];
1262 idx = bi->shortIdx[11] + lwin;
1263 #endif
1264 is_p = scalefac_to_is_pos(is_p);
1265 if (is_p != 7) {
1266 real t1, t2;
1267 t1 = tabl1[is_p];
1268 t2 = tabl2[is_p];
1269 for (; sb > 0; sb--, idx += 3) {
1270 real v = xr[0][idx];
1271 xr[0][idx] = v * t1;
1272 xr[1][idx] = v * t2;
1273 }
1274 }
1275 } /* end for(lwin; .. ; . ) */
1276
1277 if (do_l) {
1278 /* also check l-part, if ALL bands in the three windows are 'empty'
1279 * and mode = mixed_mode
1280 */
1281 int sfb = gr_infos->maxbandl;
1282 int idx = bi->longIdx[sfb];
1283
1284 for (; sfb < 8; sfb++) {
1285 int sb = bi->longDiff[sfb];
1286 int is_p = scalefac[sfb]; /* scale: 0-15 */
1287 is_p = scalefac_to_is_pos(is_p);
1288 if (is_p != 7) {
1289 real t1, t2;
1290 t1 = tabl1[is_p];
1291 t2 = tabl2[is_p];
1292 for (; sb > 0; sb--, idx++) {
1293 real v = xr[0][idx];
1294 xr[0][idx] = v * t1;
1295 xr[1][idx] = v * t2;
1296 }
1297 }
1298 else
1299 idx += sb;
1300 }
1301 }
1302 }
1303 else { /* ((gr_infos->block_type != 2)) */
1304
1305 int sfb = gr_infos->maxbandl;
1306 int is_p, idx = bi->longIdx[sfb];
1307 for (; sfb < 21; sfb++) {
1308 int sb = bi->longDiff[sfb];
1309 is_p = scalefac[sfb]; /* scale: 0-15 */
1310 is_p = scalefac_to_is_pos(is_p);
1311 if (is_p != 7) {
1312 real t1, t2;
1313 t1 = tabl1[is_p];
1314 t2 = tabl2[is_p];
1315 for (; sb > 0; sb--, idx++) {
1316 real v = xr[0][idx];
1317 xr[0][idx] = v * t1;
1318 xr[1][idx] = v * t2;
1319 }
1320 }
1321 else
1322 idx += sb;
1323 }
1324
1325 is_p = scalefac[20]; /* copy l-band 20 to l-band 21 */
1326 is_p = scalefac_to_is_pos(is_p);
1327 idx = bi->longIdx[21];
1328 if (is_p != 7) {
1329 int sb;
1330 real t1 = tabl1[is_p], t2 = tabl2[is_p];
1331
1332 for (sb = bi->longDiff[21]; sb > 0; sb--, idx++) {
1333 real v = xr[0][idx];
1334 xr[0][idx] = v * t1;
1335 xr[1][idx] = v * t2;
1336 }
1337 }
1338 } /* ... */
1339 }
1340
1341 static void
III_antialias(real xr[SBLIMIT][SSLIMIT], struct gr_info_s *gr_infos)1342 III_antialias(real xr[SBLIMIT][SSLIMIT], struct gr_info_s *gr_infos)
1343 {
1344 int sblim;
1345
1346 if (gr_infos->block_type == 2) {
1347 if (!gr_infos->mixed_block_flag)
1348 return;
1349 sblim = 1;
1350 }
1351 else {
1352 sblim = gr_infos->maxb - 1;
1353 }
1354
1355 /* 31 alias-reduction operations between each pair of sub-bands */
1356 /* with 8 butterflies between each pair */
1357
1358 {
1359 int sb;
1360 real *xr1 = (real *) xr[1];
1361
1362 for (sb = sblim; sb; sb--, xr1 += 10) {
1363 int ss;
1364 real *cs = aa_cs, *ca = aa_ca;
1365 real *xr2 = xr1;
1366
1367 for (ss = 7; ss >= 0; ss--) { /* upper and lower butterfly inputs */
1368 real bu = *--xr2, bd = *xr1;
1369 *xr2 = (bu * (*cs)) - (bd * (*ca));
1370 *xr1++ = (bd * (*cs++)) + (bu * (*ca++));
1371 }
1372 }
1373 }
1374 }
1375
1376
1377 /* *INDENT-OFF* */
1378
1379 /*
1380 DCT insipired by Jeff Tsay's DCT from the maplay package
1381 this is an optimized version with manual unroll.
1382
1383 References:
1384 [1] S. Winograd: "On Computing the Discrete Fourier Transform",
1385 Mathematics of Computation, Volume 32, Number 141, January 1978,
1386 Pages 175-199
1387 */
1388
dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf)1389 static void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf)
1390 {
1391 {
1392 real *in = inbuf;
1393
1394 in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14];
1395 in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11];
1396 in[11]+=in[10]; in[10]+=in[9]; in[9] +=in[8];
1397 in[8] +=in[7]; in[7] +=in[6]; in[6] +=in[5];
1398 in[5] +=in[4]; in[4] +=in[3]; in[3] +=in[2];
1399 in[2] +=in[1]; in[1] +=in[0];
1400
1401 in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9];
1402 in[9] +=in[7]; in[7] +=in[5]; in[5] +=in[3]; in[3] +=in[1];
1403
1404 {
1405
1406 #define MACRO0(v) { \
1407 real tmp; \
1408 out2[9+(v)] = (tmp = sum0 + sum1) * w[27+(v)]; \
1409 out2[8-(v)] = tmp * w[26-(v)]; } \
1410 sum0 -= sum1; \
1411 ts[SBLIMIT*(8-(v))] = out1[8-(v)] + sum0 * w[8-(v)]; \
1412 ts[SBLIMIT*(9+(v))] = out1[9+(v)] + sum0 * w[9+(v)];
1413 #define MACRO1(v) { \
1414 real sum0,sum1; \
1415 sum0 = tmp1a + tmp2a; \
1416 sum1 = (tmp1b + tmp2b) * tfcos36[(v)]; \
1417 MACRO0(v); }
1418 #define MACRO2(v) { \
1419 real sum0,sum1; \
1420 sum0 = tmp2a - tmp1a; \
1421 sum1 = (tmp2b - tmp1b) * tfcos36[(v)]; \
1422 MACRO0(v); }
1423
1424 const real *c = COS9;
1425 real *out2 = o2;
1426 real *w = wintab;
1427 real *out1 = o1;
1428 real *ts = tsbuf;
1429
1430 real ta33,ta66,tb33,tb66;
1431
1432 ta33 = in[2*3+0] * c[3];
1433 ta66 = in[2*6+0] * c[6];
1434 tb33 = in[2*3+1] * c[3];
1435 tb66 = in[2*6+1] * c[6];
1436
1437 {
1438 real tmp1a,tmp2a,tmp1b,tmp2b;
1439 tmp1a = in[2*1+0] * c[1] + ta33 + in[2*5+0] * c[5] + in[2*7+0] * c[7];
1440 tmp1b = in[2*1+1] * c[1] + tb33 + in[2*5+1] * c[5] + in[2*7+1] * c[7];
1441 tmp2a = in[2*0+0] + in[2*2+0] * c[2] + in[2*4+0] * c[4] + ta66 + in[2*8+0] * c[8];
1442 tmp2b = in[2*0+1] + in[2*2+1] * c[2] + in[2*4+1] * c[4] + tb66 + in[2*8+1] * c[8];
1443
1444 MACRO1(0);
1445 MACRO2(8);
1446 }
1447
1448 {
1449 real tmp1a,tmp2a,tmp1b,tmp2b;
1450 tmp1a = ( in[2*1+0] - in[2*5+0] - in[2*7+0] ) * c[3];
1451 tmp1b = ( in[2*1+1] - in[2*5+1] - in[2*7+1] ) * c[3];
1452 tmp2a = ( in[2*2+0] - in[2*4+0] - in[2*8+0] ) * c[6] - in[2*6+0] + in[2*0+0];
1453 tmp2b = ( in[2*2+1] - in[2*4+1] - in[2*8+1] ) * c[6] - in[2*6+1] + in[2*0+1];
1454
1455 MACRO1(1);
1456 MACRO2(7);
1457 }
1458
1459 {
1460 real tmp1a,tmp2a,tmp1b,tmp2b;
1461 tmp1a = in[2*1+0] * c[5] - ta33 - in[2*5+0] * c[7] + in[2*7+0] * c[1];
1462 tmp1b = in[2*1+1] * c[5] - tb33 - in[2*5+1] * c[7] + in[2*7+1] * c[1];
1463 tmp2a = in[2*0+0] - in[2*2+0] * c[8] - in[2*4+0] * c[2] + ta66 + in[2*8+0] * c[4];
1464 tmp2b = in[2*0+1] - in[2*2+1] * c[8] - in[2*4+1] * c[2] + tb66 + in[2*8+1] * c[4];
1465
1466 MACRO1(2);
1467 MACRO2(6);
1468 }
1469
1470 {
1471 real tmp1a,tmp2a,tmp1b,tmp2b;
1472 tmp1a = in[2*1+0] * c[7] - ta33 + in[2*5+0] * c[1] - in[2*7+0] * c[5];
1473 tmp1b = in[2*1+1] * c[7] - tb33 + in[2*5+1] * c[1] - in[2*7+1] * c[5];
1474 tmp2a = in[2*0+0] - in[2*2+0] * c[4] + in[2*4+0] * c[8] + ta66 - in[2*8+0] * c[2];
1475 tmp2b = in[2*0+1] - in[2*2+1] * c[4] + in[2*4+1] * c[8] + tb66 - in[2*8+1] * c[2];
1476
1477 MACRO1(3);
1478 MACRO2(5);
1479 }
1480
1481 {
1482 real sum0,sum1;
1483 sum0 = in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0];
1484 sum1 = (in[2*0+1] - in[2*2+1] + in[2*4+1] - in[2*6+1] + in[2*8+1] ) * tfcos36[4];
1485 MACRO0(4);
1486 }
1487 }
1488
1489 }
1490 }
1491
1492
1493 /*
1494 * new DCT12
1495 */
dct12(real *in,real *rawout1,real *rawout2,real *wi,real *ts)1496 static void dct12(real *in,real *rawout1,real *rawout2,real *wi,real *ts)
1497 {
1498 #define DCT12_PART1 \
1499 in5 = in[5*3]; \
1500 in5 += (in4 = in[4*3]); \
1501 in4 += (in3 = in[3*3]); \
1502 in3 += (in2 = in[2*3]); \
1503 in2 += (in1 = in[1*3]); \
1504 in1 += (in0 = in[0*3]); \
1505 \
1506 in5 += in3; in3 += in1; \
1507 \
1508 in2 *= COS6_1; \
1509 in3 *= COS6_1; \
1510
1511 #define DCT12_PART2 \
1512 in0 += in4 * COS6_2; \
1513 \
1514 in4 = in0 + in2; \
1515 in0 -= in2; \
1516 \
1517 in1 += in5 * COS6_2; \
1518 \
1519 in5 = (in1 + in3) * tfcos12[0]; \
1520 in1 = (in1 - in3) * tfcos12[2]; \
1521 \
1522 in3 = in4 + in5; \
1523 in4 -= in5; \
1524 \
1525 in2 = in0 + in1; \
1526 in0 -= in1;
1527
1528
1529 {
1530 real in0,in1,in2,in3,in4,in5;
1531 real *out1 = rawout1;
1532 ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2];
1533 ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5];
1534
1535 DCT12_PART1
1536
1537 {
1538 real tmp0,tmp1 = (in0 - in4);
1539 {
1540 real tmp2 = (in1 - in5) * tfcos12[1];
1541 tmp0 = tmp1 + tmp2;
1542 tmp1 -= tmp2;
1543 }
1544 ts[(17-1)*SBLIMIT] = out1[17-1] + tmp0 * wi[11-1];
1545 ts[(12+1)*SBLIMIT] = out1[12+1] + tmp0 * wi[6+1];
1546 ts[(6 +1)*SBLIMIT] = out1[6 +1] + tmp1 * wi[1];
1547 ts[(11-1)*SBLIMIT] = out1[11-1] + tmp1 * wi[5-1];
1548 }
1549
1550 DCT12_PART2
1551
1552 ts[(17-0)*SBLIMIT] = out1[17-0] + in2 * wi[11-0];
1553 ts[(12+0)*SBLIMIT] = out1[12+0] + in2 * wi[6+0];
1554 ts[(12+2)*SBLIMIT] = out1[12+2] + in3 * wi[6+2];
1555 ts[(17-2)*SBLIMIT] = out1[17-2] + in3 * wi[11-2];
1556
1557 ts[(6+0)*SBLIMIT] = out1[6+0] + in0 * wi[0];
1558 ts[(11-0)*SBLIMIT] = out1[11-0] + in0 * wi[5-0];
1559 ts[(6+2)*SBLIMIT] = out1[6+2] + in4 * wi[2];
1560 ts[(11-2)*SBLIMIT] = out1[11-2] + in4 * wi[5-2];
1561 }
1562
1563 in++;
1564
1565 {
1566 real in0,in1,in2,in3,in4,in5;
1567 real *out2 = rawout2;
1568
1569 DCT12_PART1
1570
1571 {
1572 real tmp0,tmp1 = (in0 - in4);
1573 {
1574 real tmp2 = (in1 - in5) * tfcos12[1];
1575 tmp0 = tmp1 + tmp2;
1576 tmp1 -= tmp2;
1577 }
1578 out2[5-1] = tmp0 * wi[11-1];
1579 out2[0+1] = tmp0 * wi[6+1];
1580 ts[(12+1)*SBLIMIT] += tmp1 * wi[1];
1581 ts[(17-1)*SBLIMIT] += tmp1 * wi[5-1];
1582 }
1583
1584 DCT12_PART2
1585
1586 out2[5-0] = in2 * wi[11-0];
1587 out2[0+0] = in2 * wi[6+0];
1588 out2[0+2] = in3 * wi[6+2];
1589 out2[5-2] = in3 * wi[11-2];
1590
1591 ts[(12+0)*SBLIMIT] += in0 * wi[0];
1592 ts[(17-0)*SBLIMIT] += in0 * wi[5-0];
1593 ts[(12+2)*SBLIMIT] += in4 * wi[2];
1594 ts[(17-2)*SBLIMIT] += in4 * wi[5-2];
1595 }
1596
1597 in++;
1598
1599 {
1600 real in0,in1,in2,in3,in4,in5;
1601 real *out2 = rawout2;
1602 out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0;
1603
1604 DCT12_PART1
1605
1606 {
1607 real tmp0,tmp1 = (in0 - in4);
1608 {
1609 real tmp2 = (in1 - in5) * tfcos12[1];
1610 tmp0 = tmp1 + tmp2;
1611 tmp1 -= tmp2;
1612 }
1613 out2[11-1] = tmp0 * wi[11-1];
1614 out2[6 +1] = tmp0 * wi[6+1];
1615 out2[0+1] += tmp1 * wi[1];
1616 out2[5-1] += tmp1 * wi[5-1];
1617 }
1618
1619 DCT12_PART2
1620
1621 out2[11-0] = in2 * wi[11-0];
1622 out2[6 +0] = in2 * wi[6+0];
1623 out2[6 +2] = in3 * wi[6+2];
1624 out2[11-2] = in3 * wi[11-2];
1625
1626 out2[0+0] += in0 * wi[0];
1627 out2[5-0] += in0 * wi[5-0];
1628 out2[0+2] += in4 * wi[2];
1629 out2[5-2] += in4 * wi[5-2];
1630 }
1631 }
1632 /* *INDENT-ON* */
1633
1634 /*
1635 * III_hybrid
1636 */
1637 static void
III_hybrid(PMPSTR mp, real fsIn[SBLIMIT][SSLIMIT], real tsOut[SSLIMIT][SBLIMIT], int ch, struct gr_info_s *gr_infos)1638 III_hybrid(PMPSTR mp, real fsIn[SBLIMIT][SSLIMIT], real tsOut[SSLIMIT][SBLIMIT],
1639 int ch, struct gr_info_s *gr_infos)
1640 {
1641 real *tspnt = (real *) tsOut;
1642 real(*block)[2][SBLIMIT * SSLIMIT] = mp->hybrid_block;
1643 int *blc = mp->hybrid_blc;
1644 real *rawout1, *rawout2;
1645 int bt;
1646 int sb = 0;
1647
1648 {
1649 int b = blc[ch];
1650 rawout1 = block[b][ch];
1651 b = -b + 1;
1652 rawout2 = block[b][ch];
1653 blc[ch] = b;
1654 }
1655
1656
1657 if (gr_infos->mixed_block_flag) {
1658 sb = 2;
1659 dct36(fsIn[0], rawout1, rawout2, win[0], tspnt);
1660 dct36(fsIn[1], rawout1 + 18, rawout2 + 18, win1[0], tspnt + 1);
1661 rawout1 += 36;
1662 rawout2 += 36;
1663 tspnt += 2;
1664 }
1665
1666 bt = gr_infos->block_type;
1667 if (bt == 2) {
1668 for (; sb < (int) gr_infos->maxb; sb += 2, tspnt += 2, rawout1 += 36, rawout2 += 36) {
1669 dct12(fsIn[sb], rawout1, rawout2, win[2], tspnt);
1670 dct12(fsIn[sb + 1], rawout1 + 18, rawout2 + 18, win1[2], tspnt + 1);
1671 }
1672 }
1673 else {
1674 for (; sb < (int) gr_infos->maxb; sb += 2, tspnt += 2, rawout1 += 36, rawout2 += 36) {
1675 dct36(fsIn[sb], rawout1, rawout2, win[bt], tspnt);
1676 dct36(fsIn[sb + 1], rawout1 + 18, rawout2 + 18, win1[bt], tspnt + 1);
1677 }
1678 }
1679
1680 for (; sb < SBLIMIT; sb++, tspnt++) {
1681 int i;
1682 for (i = 0; i < SSLIMIT; i++) {
1683 tspnt[i * SBLIMIT] = *rawout1++;
1684 *rawout2++ = 0.0;
1685 }
1686 }
1687 }
1688
1689 /*
1690 * main layer3 handler
1691 */
1692
1693 int
layer3_audiodata_precedesframes(PMPSTR mp)1694 layer3_audiodata_precedesframes(PMPSTR mp)
1695 {
1696 int audioDataInFrame;
1697 int framesToBacktrack;
1698
1699 /* specific to Layer 3, since Layer 1 & 2 the audio data starts at the frame that describes it. */
1700 /* determine how many bytes and therefore bitstream frames the audio data precedes it's matching frame */
1701 /* lame_report_fnc(mp->report_err, "hip: main_data_begin = %d, mp->bsize %d, mp->fsizeold %d, mp->ssize %d\n",
1702 sideinfo.main_data_begin, mp->bsize, mp->fsizeold, mp->ssize); */
1703 /* compute the number of frames to backtrack, 4 for the header, ssize already holds the CRC */
1704 /* TODO Erroneously assumes current frame is same as previous frame. */
1705 audioDataInFrame = mp->bsize - 4 - mp->ssize;
1706 framesToBacktrack = (mp->sideinfo.main_data_begin + audioDataInFrame - 1) / audioDataInFrame;
1707 /* lame_report_fnc(mp->report_err, "hip: audioDataInFrame %d framesToBacktrack %d\n", audioDataInFrame, framesToBacktrack); */
1708 return framesToBacktrack;
1709 }
1710
1711 int
decode_layer3_sideinfo(PMPSTR mp)1712 decode_layer3_sideinfo(PMPSTR mp)
1713 {
1714 struct frame *fr = &mp->fr;
1715 int stereo = fr->stereo;
1716 int single = fr->single;
1717 int ms_stereo;
1718 int sfreq = fr->sampling_frequency;
1719 int granules;
1720 int ch, gr, databits;
1721
1722 if (stereo == 1) { /* stream is mono */
1723 single = 0;
1724 }
1725
1726 if (fr->mode == MPG_MD_JOINT_STEREO) {
1727 ms_stereo = fr->mode_ext & 0x2;
1728 }
1729 else
1730 ms_stereo = 0;
1731
1732
1733 if (fr->lsf) {
1734 granules = 1;
1735 III_get_side_info_2(mp, stereo, ms_stereo, sfreq, single);
1736 }
1737 else {
1738 granules = 2;
1739 III_get_side_info_1(mp, stereo, ms_stereo, sfreq, single);
1740 }
1741
1742 databits = 0;
1743 for (gr = 0; gr < granules; ++gr) {
1744 for (ch = 0; ch < stereo; ++ch) {
1745 struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[gr]);
1746 databits += gr_infos->part2_3_length;
1747 }
1748 }
1749 return databits - 8 * mp->sideinfo.main_data_begin;
1750 }
1751
1752
1753
1754 int
decode_layer3_frame(PMPSTR mp, unsigned char *pcm_sample, int *pcm_point, int (*synth_1to1_mono_ptr) (PMPSTR, real *, unsigned char *, int *), int (*synth_1to1_ptr) (PMPSTR, real *, int, unsigned char *, int *))1755 decode_layer3_frame(PMPSTR mp, unsigned char *pcm_sample, int *pcm_point,
1756 int (*synth_1to1_mono_ptr) (PMPSTR, real *, unsigned char *, int *),
1757 int (*synth_1to1_ptr) (PMPSTR, real *, int, unsigned char *, int *))
1758 {
1759 int gr, ch, ss, clip = 0;
1760 int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
1761 /* struct III_sideinfo sideinfo; */
1762 struct frame *fr = &(mp->fr);
1763 int stereo = fr->stereo;
1764 int single = fr->single;
1765 int ms_stereo, i_stereo;
1766 int sfreq = fr->sampling_frequency;
1767 int stereo1, granules;
1768 real hybridIn[2][SBLIMIT][SSLIMIT];
1769 real hybridOut[2][SSLIMIT][SBLIMIT];
1770
1771 if (set_pointer(mp, (int) mp->sideinfo.main_data_begin) == MP3_ERR)
1772 return 0;
1773
1774 if (stereo == 1) { /* stream is mono */
1775 stereo1 = 1;
1776 single = 0;
1777 }
1778 else if (single >= 0) /* stream is stereo, but force to mono */
1779 stereo1 = 1;
1780 else
1781 stereo1 = 2;
1782
1783 if (fr->mode == MPG_MD_JOINT_STEREO) {
1784 ms_stereo = fr->mode_ext & 0x2;
1785 i_stereo = fr->mode_ext & 0x1;
1786 }
1787 else
1788 ms_stereo = i_stereo = 0;
1789
1790
1791 if (fr->lsf) {
1792 granules = 1;
1793 }
1794 else {
1795 granules = 2;
1796 }
1797
1798 for (gr = 0; gr < granules; gr++) {
1799
1800 {
1801 struct gr_info_s *gr_infos = &(mp->sideinfo.ch[0].gr[gr]);
1802 long part2bits;
1803
1804 if (fr->lsf)
1805 part2bits = III_get_scale_factors_2(mp, scalefacs[0], gr_infos, 0);
1806 else {
1807 part2bits = III_get_scale_factors_1(mp, scalefacs[0], gr_infos);
1808 }
1809 if (mp->pinfo != NULL) {
1810 int i;
1811 mp->pinfo->sfbits[gr][0] = part2bits;
1812 for (i = 0; i < 39; i++)
1813 mp->pinfo->sfb_s[gr][0][i] = scalefacs[0][i];
1814 }
1815
1816 /* lame_report_fnc(mp->report_err, "calling III dequantize sample 1 gr_infos->part2_3_length %d\n", gr_infos->part2_3_length); */
1817 if (III_dequantize_sample(mp, hybridIn[0], scalefacs[0], gr_infos, sfreq, part2bits))
1818 return clip;
1819 }
1820 if (stereo == 2) {
1821 struct gr_info_s *gr_infos = &(mp->sideinfo.ch[1].gr[gr]);
1822 long part2bits;
1823 if (fr->lsf)
1824 part2bits = III_get_scale_factors_2(mp, scalefacs[1], gr_infos, i_stereo);
1825 else {
1826 part2bits = III_get_scale_factors_1(mp, scalefacs[1], gr_infos);
1827 }
1828 if (mp->pinfo != NULL) {
1829 int i;
1830 mp->pinfo->sfbits[gr][1] = part2bits;
1831 for (i = 0; i < 39; i++)
1832 mp->pinfo->sfb_s[gr][1][i] = scalefacs[1][i];
1833 }
1834
1835 /* lame_report_fnc(mp->report_err, "calling III dequantize sample 2 gr_infos->part2_3_length %d\n", gr_infos->part2_3_length); */
1836 if (III_dequantize_sample(mp, hybridIn[1], scalefacs[1], gr_infos, sfreq, part2bits))
1837 return clip;
1838
1839 if (ms_stereo) {
1840 int i;
1841 for (i = 0; i < SBLIMIT * SSLIMIT; i++) {
1842 real tmp0, tmp1;
1843 tmp0 = ((real *) hybridIn[0])[i];
1844 tmp1 = ((real *) hybridIn[1])[i];
1845 ((real *) hybridIn[1])[i] = tmp0 - tmp1;
1846 ((real *) hybridIn[0])[i] = tmp0 + tmp1;
1847 }
1848 }
1849
1850 if (i_stereo)
1851 III_i_stereo(hybridIn, scalefacs[1], gr_infos, sfreq, ms_stereo, fr->lsf);
1852
1853 if (ms_stereo || i_stereo || (single == 3)) {
1854 if (gr_infos->maxb > mp->sideinfo.ch[0].gr[gr].maxb)
1855 mp->sideinfo.ch[0].gr[gr].maxb = gr_infos->maxb;
1856 else
1857 gr_infos->maxb = mp->sideinfo.ch[0].gr[gr].maxb;
1858 }
1859
1860 switch (single) {
1861 case 3:
1862 {
1863 int i;
1864 real *in0 = (real *) hybridIn[0], *in1 = (real *) hybridIn[1];
1865 for (i = 0; i < (int) (SSLIMIT * gr_infos->maxb); i++, in0++)
1866 *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */
1867 }
1868 break;
1869 case 1:
1870 {
1871 int i;
1872 real *in0 = (real *) hybridIn[0], *in1 = (real *) hybridIn[1];
1873 for (i = 0; i < (int) (SSLIMIT * gr_infos->maxb); i++)
1874 *in0++ = *in1++;
1875 }
1876 break;
1877 }
1878 }
1879
1880 if (mp->pinfo != NULL) {
1881 int i, sb;
1882 float ifqstep;
1883
1884 mp->pinfo->bitrate = tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index];
1885 mp->pinfo->sampfreq = freqs[sfreq];
1886 mp->pinfo->emph = fr->emphasis;
1887 mp->pinfo->crc = fr->error_protection;
1888 mp->pinfo->padding = fr->padding;
1889 mp->pinfo->stereo = fr->stereo;
1890 mp->pinfo->js = (fr->mode == MPG_MD_JOINT_STEREO);
1891 mp->pinfo->ms_stereo = ms_stereo;
1892 mp->pinfo->i_stereo = i_stereo;
1893 mp->pinfo->maindata = mp->sideinfo.main_data_begin;
1894
1895 for (ch = 0; ch < stereo1; ch++) {
1896 struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[gr]);
1897 mp->pinfo->big_values[gr][ch] = gr_infos->big_values;
1898 mp->pinfo->scalefac_scale[gr][ch] = gr_infos->scalefac_scale;
1899 mp->pinfo->mixed[gr][ch] = gr_infos->mixed_block_flag;
1900 mp->pinfo->mpg123blocktype[gr][ch] = gr_infos->block_type;
1901 mp->pinfo->mainbits[gr][ch] = gr_infos->part2_3_length;
1902 mp->pinfo->preflag[gr][ch] = gr_infos->preflag;
1903 if (gr == 1)
1904 mp->pinfo->scfsi[ch] = gr_infos->scfsi;
1905 }
1906
1907
1908 for (ch = 0; ch < stereo1; ch++) {
1909 struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[gr]);
1910 ifqstep = (mp->pinfo->scalefac_scale[gr][ch] == 0) ? .5 : 1.0;
1911 if (2 == gr_infos->block_type) {
1912 for (i = 0; i < 3; i++) {
1913 for (sb = 0; sb < 12; sb++) {
1914 int j = 3 * sb + i;
1915 /*
1916 is_p = scalefac[sfb*3+lwin-gr_infos->mixed_block_flag];
1917 */
1918 /* scalefac was copied into pinfo->sfb_s[] above */
1919 mp->pinfo->sfb_s[gr][ch][j] =
1920 -ifqstep * mp->pinfo->sfb_s[gr][ch][j - gr_infos->mixed_block_flag];
1921 mp->pinfo->sfb_s[gr][ch][j] -= 2 * (mp->pinfo->sub_gain[gr][ch][i]);
1922 }
1923 mp->pinfo->sfb_s[gr][ch][3 * sb + i] =
1924 -2 * (mp->pinfo->sub_gain[gr][ch][i]);
1925 }
1926 }
1927 else {
1928 for (sb = 0; sb < 21; sb++) {
1929 /* scalefac was copied into pinfo->sfb[] above */
1930 mp->pinfo->sfb[gr][ch][sb] = mp->pinfo->sfb_s[gr][ch][sb];
1931 if (gr_infos->preflag)
1932 mp->pinfo->sfb[gr][ch][sb] += pretab1[sb];
1933 mp->pinfo->sfb[gr][ch][sb] *= -ifqstep;
1934 }
1935 mp->pinfo->sfb[gr][ch][21] = 0;
1936 }
1937 }
1938
1939
1940
1941 for (ch = 0; ch < stereo1; ch++) {
1942 int j = 0;
1943 for (sb = 0; sb < SBLIMIT; sb++)
1944 for (ss = 0; ss < SSLIMIT; ss++, j++)
1945 mp->pinfo->mpg123xr[gr][ch][j] = hybridIn[ch][sb][ss];
1946 }
1947 }
1948
1949
1950 for (ch = 0; ch < stereo1; ch++) {
1951 struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[gr]);
1952 III_antialias(hybridIn[ch], gr_infos);
1953 III_hybrid(mp, hybridIn[ch], hybridOut[ch], ch, gr_infos);
1954 }
1955
1956 for (ss = 0; ss < SSLIMIT; ss++) {
1957 if (single >= 0) {
1958 clip += (*synth_1to1_mono_ptr) (mp, hybridOut[0][ss], pcm_sample, pcm_point);
1959 }
1960 else {
1961 int p1 = *pcm_point;
1962 clip += (*synth_1to1_ptr) (mp, hybridOut[0][ss], 0, pcm_sample, &p1);
1963 clip += (*synth_1to1_ptr) (mp, hybridOut[1][ss], 1, pcm_sample, pcm_point);
1964 }
1965 }
1966 }
1967
1968 return clip;
1969 }
1970