2 * layer3.c: Mpeg Layer-3 audio decoder
4 * Copyright (C) 1999-2010 The L.A.M.E. project
6 * Initially written by Michael Hipp, see also AUTHORS and README.
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.
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.
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.
23 /* $Id: layer3.c,v 1.63 2011/03/05 14:06:54 robert Exp $ */
35 #include "lame-analysis.h"
36 #include "decode_i386.h"
44 static int gd_are_hip_tables_layer3_initialized = 0;
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];
53 static real COS6_1, COS6_2;
54 static real tfcos36[9];
55 static real tfcos12[3];
57 struct bandInfoStruct {
64 static int longLimit[9][23];
65 static int shortLimit[9][14];
69 static const struct bandInfoStruct bandInfo[9] = {
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} } ,
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} } ,
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} } ,
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 } } ,
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 } } ,
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} } ,
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];
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 */
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];
134 rval = *mp->wordpointer << mp->bitindex;
137 mp->wordpointer += (mp->bitindex >> 3);
147 * init tables for layer-3
150 hip_init_tables_layer3(void)
154 if (gd_are_hip_tables_layer3_initialized) {
157 gd_are_hip_tables_layer3_initialized = 1;
159 for (i = -256; i < 118 + 4; i++)
160 gainpow2[i + 256] = pow((double) 2.0, -0.25 * (double) (i + 210));
162 for (i = 0; i < 8207; i++)
163 ispow[i] = pow((double) i, (double) 4.0 / 3.0);
165 for (i = 0; i < 8; i++) {
166 static double Ci[8] = { -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037 };
167 double sq = sqrt(1.0 + Ci[i] * Ci[i]);
169 aa_ca[i] = Ci[i] / sq;
172 for (i = 0; i < 18; i++) {
173 win[0][i] = win[1][i] =
174 0.5 * sin(M_PI / 72.0 * (double) (2 * (i + 0) + 1)) / cos(M_PI *
175 (double) (2 * (i + 0) +
177 win[0][i + 18] = win[3][i + 18] =
178 0.5 * sin(M_PI / 72.0 * (double) (2 * (i + 18) + 1)) / cos(M_PI *
179 (double) (2 * (i + 18) +
182 for (i = 0; i < 6; i++) {
183 win[1][i + 18] = 0.5 / cos(M_PI * (double) (2 * (i + 18) + 19) / 72.0);
184 win[3][i + 12] = 0.5 / cos(M_PI * (double) (2 * (i + 12) + 19) / 72.0);
186 0.5 * sin(M_PI / 24.0 * (double) (2 * i + 13)) / cos(M_PI *
187 (double) (2 * (i + 24) +
189 win[1][i + 30] = win[3][i] = 0.0;
191 0.5 * sin(M_PI / 24.0 * (double) (2 * i + 1)) / cos(M_PI * (double) (2 * (i + 6) + 19) /
195 for (i = 0; i < 9; i++)
196 COS9[i] = cos(M_PI / 18.0 * (double) i);
198 for (i = 0; i < 9; i++)
199 tfcos36[i] = 0.5 / cos(M_PI * (double) (i * 2 + 1) / 36.0);
200 for (i = 0; i < 3; i++)
201 tfcos12[i] = 0.5 / cos(M_PI * (double) (i * 2 + 1) / 12.0);
203 COS6_1 = cos(M_PI / 6.0 * (double) 1);
204 COS6_2 = cos(M_PI / 6.0 * (double) 2);
206 for (i = 0; i < 12; i++) {
208 0.5 * sin(M_PI / 24.0 * (double) (2 * i + 1)) / cos(M_PI * (double) (2 * i + 7) / 24.0);
209 for (j = 0; j < 6; j++)
210 COS1[i][j] = cos(M_PI / 24.0 * (double) ((2 * i + 7) * (2 * j + 1)));
213 for (j = 0; j < 4; j++) {
214 static int const len[4] = { 36, 36, 12, 36 };
215 for (i = 0; i < len[j]; i += 2)
216 win1[j][i] = +win[j][i];
217 for (i = 1; i < len[j]; i += 2)
218 win1[j][i] = -win[j][i];
221 for (i = 0; i < 16; i++) {
222 double t = tan((double) i * M_PI / 12.0);
223 tan1_1[i] = t / (1.0 + t);
224 tan2_1[i] = 1.0 / (1.0 + t);
225 tan1_2[i] = M_SQRT2 * t / (1.0 + t);
226 tan2_2[i] = M_SQRT2 / (1.0 + t);
228 for (j = 0; j < 2; j++) {
229 double base = pow(2.0, -0.25 * (j + 1.0));
230 double p1 = 1.0, p2 = 1.0;
233 p1 = pow(base, (i + 1.0) * 0.5);
235 p2 = pow(base, i * 0.5);
239 pow1_2[j][i] = M_SQRT2 * p1;
240 pow2_2[j][i] = M_SQRT2 * p2;
244 for (j = 0; j < 9; j++) {
245 struct bandInfoStruct const *bi = (struct bandInfoStruct const *) &bandInfo[j];
250 mp = map[j][0] = mapbuf0[j];
252 for (i = 0, cb = 0; cb < 8; cb++, i += *bdf++) {
258 bdf = bi->shortDiff + 3;
259 for (cb = 3; cb < 13; cb++) {
260 int l = (*bdf++) >> 1;
261 for (lwin = 0; lwin < 3; lwin++) {
271 mp = map[j][1] = mapbuf1[j];
272 bdf = bi->shortDiff + 0;
273 for (i = 0, cb = 0; cb < 13; cb++) {
274 int l = (*bdf++) >> 1;
275 for (lwin = 0; lwin < 3; lwin++) {
285 mp = map[j][2] = mapbuf2[j];
287 for (cb = 0; cb < 22; cb++) {
288 *mp++ = (*bdf++) >> 1;
295 for (j = 0; j < 9; j++) {
296 for (i = 0; i < 23; i++) {
297 longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
298 if (longLimit[j][i] > SBLIMIT)
299 longLimit[j][i] = SBLIMIT;
301 for (i = 0; i < 14; i++) {
302 shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
303 if (shortLimit[j][i] > SBLIMIT)
304 shortLimit[j][i] = SBLIMIT;
308 for (i = 0; i < 5; i++) {
309 for (j = 0; j < 6; j++) {
310 for (k = 0; k < 6; k++) {
311 int n = k + j * 6 + i * 36;
312 i_slen2[n] = i | (j << 3) | (k << 6) | (3 << 12);
316 for (i = 0; i < 4; i++) {
317 for (j = 0; j < 4; j++) {
318 for (k = 0; k < 4; k++) {
319 int n = k + j * 4 + i * 16;
320 i_slen2[n + 180] = i | (j << 3) | (k << 6) | (4 << 12);
324 for (i = 0; i < 4; i++) {
325 for (j = 0; j < 3; j++) {
327 i_slen2[n + 244] = i | (j << 3) | (5 << 12);
328 n_slen2[n + 500] = i | (j << 3) | (2 << 12) | (1 << 15);
332 for (i = 0; i < 5; i++) {
333 for (j = 0; j < 5; j++) {
334 for (k = 0; k < 4; k++) {
336 for (l = 0; l < 4; l++) {
337 int n = l + k * 4 + j * 16 + i * 80;
338 n_slen2[n] = i | (j << 3) | (k << 6) | (l << 9) | (0 << 12);
343 for (i = 0; i < 5; i++) {
344 for (j = 0; j < 5; j++) {
345 for (k = 0; k < 4; k++) {
346 int n = k + j * 4 + i * 20;
347 n_slen2[n + 400] = i | (j << 3) | (k << 6) | (1 << 12);
354 * read additional side information
358 III_get_side_info_1(PMPSTR mp, int stereo,
359 int ms_stereo, long sfreq, int single)
362 int powdiff = (single == 3) ? 4 : 0;
364 mp->sideinfo.main_data_begin = getbits(mp, 9);
366 mp->sideinfo.private_bits = getbits_fast(mp, 5);
368 mp->sideinfo.private_bits = getbits_fast(mp, 3);
370 for (ch = 0; ch < stereo; ch++) {
371 mp->sideinfo.ch[ch].gr[0].scfsi = -1;
372 mp->sideinfo.ch[ch].gr[1].scfsi = getbits_fast(mp, 4);
375 for (gr = 0; gr < 2; gr++) {
376 for (ch = 0; ch < stereo; ch++) {
377 struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[gr]);
379 gr_infos->part2_3_length = getbits(mp, 12);
380 gr_infos->big_values = getbits_fast(mp, 9);
381 if (gr_infos->big_values > 288) {
382 lame_report_fnc(mp->report_err, "big_values too large! %i\n", gr_infos->big_values);
383 gr_infos->big_values = 288;
386 unsigned int qss = getbits_fast(mp, 8);
387 gr_infos->pow2gain = gainpow2 + 256 - qss + powdiff;
388 if (mp->pinfo != NULL) {
389 mp->pinfo->qss[gr][ch] = qss;
393 gr_infos->pow2gain += 2;
394 gr_infos->scalefac_compress = getbits_fast(mp, 4);
395 /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
398 gr_infos->block_type = getbits_fast(mp, 2);
399 gr_infos->mixed_block_flag = get1bit(mp);
400 gr_infos->table_select[0] = getbits_fast(mp, 5);
401 gr_infos->table_select[1] = getbits_fast(mp, 5);
405 * table_select[2] not needed, because there is no region2,
406 * but to satisfy some verifications tools we set it either.
408 gr_infos->table_select[2] = 0;
409 for (i = 0; i < 3; i++) {
410 unsigned int sbg = (getbits_fast(mp, 3) << 3);
411 gr_infos->full_gain[i] = gr_infos->pow2gain + sbg;
412 if (mp->pinfo != NULL)
413 mp->pinfo->sub_gain[gr][ch][i] = sbg / 8;
416 if (gr_infos->block_type == 0) {
417 lame_report_fnc(mp->report_err, "Blocktype == 0 and window-switching == 1 not allowed.\n");
418 /* error seems to be very good recoverable, so don't exit */
421 /* region_count/start parameters are implicit in this case. */
422 gr_infos->region1start = 36 >> 1;
423 gr_infos->region2start = 576 >> 1;
426 unsigned int i, r0c, r1c, region0index, region1index;
427 for (i = 0; i < 3; i++)
428 gr_infos->table_select[i] = getbits_fast(mp, 5);
429 r0c = getbits_fast(mp, 4);
430 r1c = getbits_fast(mp, 3);
431 region0index = r0c+1;
432 if (region0index > 22) {
434 lame_report_fnc(mp->report_err, "region0index > 22\n");
436 region1index = r0c+1 + r1c+1;
437 if (region1index > 22) {
439 lame_report_fnc(mp->report_err, "region1index > 22\n");
441 gr_infos->region1start = bandInfo[sfreq].longIdx[region0index] >> 1;
442 gr_infos->region2start = bandInfo[sfreq].longIdx[region1index] >> 1;
443 gr_infos->block_type = 0;
444 gr_infos->mixed_block_flag = 0;
446 gr_infos->preflag = get1bit(mp);
447 gr_infos->scalefac_scale = get1bit(mp);
448 gr_infos->count1table_select = get1bit(mp);
454 * Side Info for MPEG 2.0 / LSF
457 III_get_side_info_2(PMPSTR mp, int stereo, int ms_stereo, long sfreq, int single)
460 int powdiff = (single == 3) ? 4 : 0;
462 mp->sideinfo.main_data_begin = getbits(mp, 8);
465 mp->sideinfo.private_bits = get1bit(mp);
467 mp->sideinfo.private_bits = getbits_fast(mp, 2);
469 for (ch = 0; ch < stereo; ch++) {
470 struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[0]);
473 gr_infos->part2_3_length = getbits(mp, 12);
474 gr_infos->big_values = getbits_fast(mp, 9);
475 if (gr_infos->big_values > 288) {
476 lame_report_fnc(mp->report_err, "big_values too large! %i\n", gr_infos->big_values);
477 gr_infos->big_values = 288;
479 qss = getbits_fast(mp, 8);
480 gr_infos->pow2gain = gainpow2 + 256 - qss + powdiff;
481 if (mp->pinfo != NULL) {
482 mp->pinfo->qss[0][ch] = qss;
487 gr_infos->pow2gain += 2;
488 gr_infos->scalefac_compress = getbits(mp, 9);
489 /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
492 gr_infos->block_type = getbits_fast(mp, 2);
493 gr_infos->mixed_block_flag = get1bit(mp);
494 gr_infos->table_select[0] = getbits_fast(mp, 5);
495 gr_infos->table_select[1] = getbits_fast(mp, 5);
497 * table_select[2] not needed, because there is no region2,
498 * but to satisfy some verifications tools we set it either.
500 gr_infos->table_select[2] = 0;
501 for (i = 0; i < 3; i++) {
502 unsigned int sbg = (getbits_fast(mp, 3) << 3);
503 gr_infos->full_gain[i] = gr_infos->pow2gain + sbg;
504 if (mp->pinfo != NULL)
505 mp->pinfo->sub_gain[0][ch][i] = sbg / 8;
509 if (gr_infos->block_type == 0) {
510 lame_report_fnc(mp->report_err, "Blocktype == 0 and window-switching == 1 not allowed.\n");
511 /* error seems to be very good recoverable, so don't exit */
514 /* region_count/start parameters are implicit in this case. */
515 /* check this again! */
516 if (gr_infos->block_type == 2) {
518 gr_infos->region1start = 36;
520 gr_infos->region1start = 36 >> 1;
523 /* check this for 2.5 and sfreq=8 */
524 gr_infos->region1start = 108 >> 1;
526 gr_infos->region1start = 54 >> 1;
527 gr_infos->region2start = 576 >> 1;
530 unsigned int i, r0c, r1c, region0index, region1index;
531 for (i = 0; i < 3; i++)
532 gr_infos->table_select[i] = getbits_fast(mp, 5);
533 r0c = getbits_fast(mp, 4);
534 r1c = getbits_fast(mp, 3);
535 region0index = r0c+1;
536 if (region0index > 22) {
538 lame_report_fnc(mp->report_err, "region0index > 22\n");
540 region1index = r0c+1 + r1c+1;
541 if (region1index > 22) {
543 lame_report_fnc(mp->report_err, "region1index > 22\n");
545 gr_infos->region1start = bandInfo[sfreq].longIdx[region0index] >> 1;
546 gr_infos->region2start = bandInfo[sfreq].longIdx[region1index] >> 1;
547 gr_infos->block_type = 0;
548 gr_infos->mixed_block_flag = 0;
550 gr_infos->scalefac_scale = get1bit(mp);
551 gr_infos->count1table_select = get1bit(mp);
560 III_get_scale_factors_1(PMPSTR mp, int *scf, struct gr_info_s *gr_infos)
562 static const unsigned char slen[2][16] = {
563 {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
564 {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
567 int num0 = slen[0][gr_infos->scalefac_compress];
568 int num1 = slen[1][gr_infos->scalefac_compress];
570 if (gr_infos->block_type == 2) {
572 numbits = (num0 + num1) * 18;
574 if (gr_infos->mixed_block_flag) {
576 *scf++ = getbits_fast(mp, num0);
578 numbits -= num0; /* num0 * 17 + num1 * 18 */
582 *scf++ = getbits_fast(mp, num0);
584 *scf++ = getbits_fast(mp, num1);
587 *scf++ = 0; /* short[13][0..2] = 0 */
591 int scfsi = gr_infos->scfsi;
593 if (scfsi < 0) { /* scfsi < 0 => granule == 0 */
595 *scf++ = getbits_fast(mp, num0);
597 *scf++ = getbits_fast(mp, num1);
598 numbits = (num0 + num1) * 10 + num0;
602 if (!(scfsi & 0x8)) {
604 *scf++ = getbits_fast(mp, num0);
611 if (!(scfsi & 0x4)) {
613 *scf++ = getbits_fast(mp, num0);
620 if (!(scfsi & 0x2)) {
622 *scf++ = getbits_fast(mp, num1);
629 if (!(scfsi & 0x1)) {
631 *scf++ = getbits_fast(mp, num1);
639 *scf++ = 0; /* no l[21] in original sources */
646 III_get_scale_factors_2(PMPSTR mp, int *scf, struct gr_info_s *gr_infos, int i_stereo)
648 unsigned char const *pnt;
655 static const unsigned char stab[3][6][4] = {
656 { { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0} ,
657 { 7, 7, 7,0 } , { 6, 6, 6,3 } , { 8, 8,5,0} } ,
658 { { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0} ,
659 {12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0} } ,
660 { { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0} ,
661 { 6,15,12,0 } , { 6,12, 9,6 } , { 6,18,9,0} } };
664 if (i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */
665 slen = i_slen2[gr_infos->scalefac_compress >> 1];
667 slen = n_slen2[gr_infos->scalefac_compress];
669 gr_infos->preflag = (slen >> 15) & 0x1;
672 if (gr_infos->block_type == 2) {
674 if (gr_infos->mixed_block_flag)
678 pnt = (unsigned char const *) stab[n][(slen >> 12) & 0x7];
680 for (i = 0; i < 4; i++) {
681 int num = slen & 0x7;
684 for (j = 0; j < (int) (pnt[i]); j++)
685 *scf++ = getbits_fast(mp, num);
686 numbits += pnt[i] * num;
689 for (j = 0; j < (int) (pnt[i]); j++)
695 for (i = 0; i < n; i++)
702 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 ? */
703 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};
707 * don't forget to apply the same changes to III_dequantize_sample_ms() !!!
710 III_dequantize_sample(PMPSTR mp, real xr[SBLIMIT][SSLIMIT], int *scf,
711 struct gr_info_s *gr_infos, int sfreq, int part2bits)
713 int shift = 1 + gr_infos->scalefac_scale;
714 real *xrpnt = (real *) xr;
716 int part2remain = gr_infos->part2_3_length - part2bits;
719 /* lame_report_fnc(mp->report_dbg,"part2remain = %d, gr_infos->part2_3_length = %d, part2bits = %d\n",
720 part2remain, gr_infos->part2_3_length, part2bits); */
725 for (i = (&xr[SBLIMIT][0] - xrpnt) >> 1; i > 0; i--) {
734 int bv = gr_infos->big_values;
735 int region1 = gr_infos->region1start;
736 int region2 = gr_infos->region2start;
738 l3 = ((576 >> 1) - bv) >> 1;
740 * we may lose the 'odd' bit here !!
741 * check this later again
755 l[1] = region2 - l[0];
763 for (i = 0; i < 3; i++) {
765 lame_report_fnc(mp->report_err, "hip: Bogus region length (%d)\n", l[i]);
770 /* end MDH crash fix */
772 if (gr_infos->block_type == 2) {
774 * decoding with short or mixed mode BandIndex table
777 int step = 0, lwin = 0, cb = 0;
781 if (gr_infos->mixed_block_flag) {
783 max[0] = max[1] = max[2] = 2;
785 me = mapend[sfreq][0];
788 max[0] = max[1] = max[2] = max[3] = -1;
789 /* max[3] not really needed in this case */
791 me = mapend[sfreq][1];
795 for (i = 0; i < 2; i++) {
797 struct newhuff const *h = (struct newhuff const *) (ht + gr_infos->table_select[i]);
798 for (; lp; lp--, mc--) {
802 xrpnt = ((real *) xr) + (*m++);
806 v = gr_infos->pow2gain[(*scf++) << shift];
810 v = gr_infos->full_gain[lwin][(*scf++) << shift];
815 short const *val = (short const *) h->table;
816 while ((y = *val++) < 0) {
826 part2remain -= h->linbits + 1;
827 x += getbits(mp, (int) h->linbits);
829 *xrpnt = -ispow[x] * v;
831 *xrpnt = ispow[x] * v;
836 *xrpnt = -ispow[x] * v;
838 *xrpnt = ispow[x] * v;
846 part2remain -= h->linbits + 1;
847 y += getbits(mp, (int) h->linbits);
849 *xrpnt = -ispow[y] * v;
851 *xrpnt = ispow[y] * v;
856 *xrpnt = -ispow[y] * v;
858 *xrpnt = ispow[y] * v;
866 for (; l3 && (part2remain > 0); l3--) {
867 struct newhuff const *h = (struct newhuff const *) (htc + gr_infos->count1table_select);
868 short const *val = (short const *) h->table;
871 while ((a = *val++) < 0) {
873 if (part2remain < 0) {
881 for (i = 0; i < 4; i++) {
885 xrpnt = ((real *) xr) + (*m++);
889 v = gr_infos->pow2gain[(*scf++) << shift];
893 v = gr_infos->full_gain[lwin][(*scf++) << shift];
899 if ((a & (0x8 >> i))) {
902 if (part2remain < 0) {
920 xrpnt = ((real *) xr) + *m++;
932 /* we could add a little opt. here:
933 * if we finished a band for window 3 or a long band
934 * further bands could copied in a simple loop without a
935 * special 'map' decoding
939 gr_infos->maxband[0] = max[0] + 1;
940 gr_infos->maxband[1] = max[1] + 1;
941 gr_infos->maxband[2] = max[2] + 1;
942 gr_infos->maxbandl = max[3] + 1;
945 int rmax = max[0] > max[1] ? max[0] : max[1];
946 rmax = (rmax > max[2] ? rmax : max[2]) + 1;
947 gr_infos->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3] + 1];
953 * decoding with 'long' BandIndex table (block_type != 2)
955 int const *pretab = (int const *) (gr_infos->preflag ? pretab1 : pretab2);
958 int *m = map[sfreq][2];
963 * long hash table values
965 for (i = 0; i < 3; i++) {
967 struct newhuff const *h = (struct newhuff const *) (ht + gr_infos->table_select[i]);
969 for (; lp; lp--, mc--) {
974 v = gr_infos->pow2gain[((*scf++) + (*pretab++)) << shift];
978 short const *val = (short const *) h->table;
979 while ((y = *val++) < 0) {
989 part2remain -= h->linbits + 1;
990 x += getbits(mp, (int) h->linbits);
992 *xrpnt++ = -ispow[x] * v;
994 *xrpnt++ = ispow[x] * v;
999 *xrpnt++ = -ispow[x] * v;
1001 *xrpnt++ = ispow[x] * v;
1009 part2remain -= h->linbits + 1;
1010 y += getbits(mp, (int) h->linbits);
1012 *xrpnt++ = -ispow[y] * v;
1014 *xrpnt++ = ispow[y] * v;
1019 *xrpnt++ = -ispow[y] * v;
1021 *xrpnt++ = ispow[y] * v;
1030 * short (count1table) values
1032 for (; l3 && (part2remain > 0); l3--) {
1033 struct newhuff const *h = (struct newhuff const *) (htc + gr_infos->count1table_select);
1034 short const *val = (short const *) h->table;
1037 while ((a = *val++) < 0) {
1039 if (part2remain < 0) {
1047 for (i = 0; i < 4; i++) {
1052 v = gr_infos->pow2gain[((*scf++) + (*pretab++)) << shift];
1056 if ((a & (0x8 >> i))) {
1059 if (part2remain < 0) {
1076 for (i = (&xr[SBLIMIT][0] - xrpnt) >> 1; i; i--) {
1081 gr_infos->maxbandl = max + 1;
1082 gr_infos->maxb = longLimit[sfreq][gr_infos->maxbandl];
1085 while (part2remain > 16) {
1086 getbits(mp, 16); /* Dismiss stuffing Bits */
1089 if (part2remain > 0)
1090 getbits(mp, part2remain);
1091 else if (part2remain < 0) {
1092 lame_report_fnc(mp->report_err, "hip: Can't rewind stream by %d bits!\n", -part2remain);
1093 return 1; /* -> error */
1100 * III_stereo: calculate real channel values for Joint-I-Stereo-mode
1103 III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT], int *scalefac,
1104 struct gr_info_s *gr_infos, int sfreq, int ms_stereo, int lsf)
1106 real(*xr)[SBLIMIT * SSLIMIT] = (real(*)[SBLIMIT * SSLIMIT]) xr_buf;
1107 struct bandInfoStruct const *bi = (struct bandInfoStruct const *) &bandInfo[sfreq];
1108 real *tabl1, *tabl2;
1111 int p = gr_infos->scalefac_compress & 0x1;
1132 if (gr_infos->block_type == 2) {
1134 if (gr_infos->mixed_block_flag)
1137 for (lwin = 0; lwin < 3; lwin++) { /* process each window */
1138 /* get first band with zero values */
1139 int is_p, sb, idx, sfb = gr_infos->maxband[lwin]; /* sfb is minimal 3 for mixed mode */
1143 for (; sfb < 12; sfb++) {
1144 is_p = scalefac[sfb * 3 + lwin - gr_infos->mixed_block_flag]; /* scale: 0-15 */
1147 sb = bi->shortDiff[sfb];
1148 idx = bi->shortIdx[sfb] + lwin;
1151 for (; sb > 0; sb--, idx += 3) {
1152 real v = xr[0][idx];
1153 xr[0][idx] = v * t1;
1154 xr[1][idx] = v * t2;
1160 /* in the original: copy 10 to 11 , here: copy 11 to 12
1161 maybe still wrong??? (copy 12 to 13?) */
1162 is_p = scalefac[11 * 3 + lwin - gr_infos->mixed_block_flag]; /* scale: 0-15 */
1163 sb = bi->shortDiff[12];
1164 idx = bi->shortIdx[12] + lwin;
1166 is_p = scalefac[10 * 3 + lwin - gr_infos->mixed_block_flag]; /* scale: 0-15 */
1167 sb = bi->shortDiff[11];
1168 idx = bi->shortIdx[11] + lwin;
1174 for (; sb > 0; sb--, idx += 3) {
1175 real v = xr[0][idx];
1176 xr[0][idx] = v * t1;
1177 xr[1][idx] = v * t2;
1180 } /* end for(lwin; .. ; . ) */
1183 /* also check l-part, if ALL bands in the three windows are 'empty'
1184 * and mode = mixed_mode
1186 int sfb = gr_infos->maxbandl;
1187 int idx = bi->longIdx[sfb];
1189 for (; sfb < 8; sfb++) {
1190 int sb = bi->longDiff[sfb];
1191 int is_p = scalefac[sfb]; /* scale: 0-15 */
1196 for (; sb > 0; sb--, idx++) {
1197 real v = xr[0][idx];
1198 xr[0][idx] = v * t1;
1199 xr[1][idx] = v * t2;
1207 else { /* ((gr_infos->block_type != 2)) */
1209 int sfb = gr_infos->maxbandl;
1210 int is_p, idx = bi->longIdx[sfb];
1211 for (; sfb < 21; sfb++) {
1212 int sb = bi->longDiff[sfb];
1213 is_p = scalefac[sfb]; /* scale: 0-15 */
1218 for (; sb > 0; sb--, idx++) {
1219 real v = xr[0][idx];
1220 xr[0][idx] = v * t1;
1221 xr[1][idx] = v * t2;
1228 is_p = scalefac[20]; /* copy l-band 20 to l-band 21 */
1231 real t1 = tabl1[is_p], t2 = tabl2[is_p];
1233 for (sb = bi->longDiff[21]; sb > 0; sb--, idx++) {
1234 real v = xr[0][idx];
1235 xr[0][idx] = v * t1;
1236 xr[1][idx] = v * t2;
1243 III_antialias(real xr[SBLIMIT][SSLIMIT], struct gr_info_s *gr_infos)
1247 if (gr_infos->block_type == 2) {
1248 if (!gr_infos->mixed_block_flag)
1253 sblim = gr_infos->maxb - 1;
1256 /* 31 alias-reduction operations between each pair of sub-bands */
1257 /* with 8 butterflies between each pair */
1261 real *xr1 = (real *) xr[1];
1263 for (sb = sblim; sb; sb--, xr1 += 10) {
1265 real *cs = aa_cs, *ca = aa_ca;
1268 for (ss = 7; ss >= 0; ss--) { /* upper and lower butterfly inputs */
1269 real bu = *--xr2, bd = *xr1;
1270 *xr2 = (bu * (*cs)) - (bd * (*ca));
1271 *xr1++ = (bd * (*cs++)) + (bu * (*ca++));
1281 DCT insipired by Jeff Tsay's DCT from the maplay package
1282 this is an optimized version with manual unroll.
1285 [1] S. Winograd: "On Computing the Discrete Fourier Transform",
1286 Mathematics of Computation, Volume 32, Number 141, January 1978,
1290 static void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf)
1295 in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14];
1296 in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11];
1297 in[11]+=in[10]; in[10]+=in[9]; in[9] +=in[8];
1298 in[8] +=in[7]; in[7] +=in[6]; in[6] +=in[5];
1299 in[5] +=in[4]; in[4] +=in[3]; in[3] +=in[2];
1300 in[2] +=in[1]; in[1] +=in[0];
1302 in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9];
1303 in[9] +=in[7]; in[7] +=in[5]; in[5] +=in[3]; in[3] +=in[1];
1307 #define MACRO0(v) { \
1309 out2[9+(v)] = (tmp = sum0 + sum1) * w[27+(v)]; \
1310 out2[8-(v)] = tmp * w[26-(v)]; } \
1312 ts[SBLIMIT*(8-(v))] = out1[8-(v)] + sum0 * w[8-(v)]; \
1313 ts[SBLIMIT*(9+(v))] = out1[9+(v)] + sum0 * w[9+(v)];
1314 #define MACRO1(v) { \
1316 sum0 = tmp1a + tmp2a; \
1317 sum1 = (tmp1b + tmp2b) * tfcos36[(v)]; \
1319 #define MACRO2(v) { \
1321 sum0 = tmp2a - tmp1a; \
1322 sum1 = (tmp2b - tmp1b) * tfcos36[(v)]; \
1325 const real *c = COS9;
1331 real ta33,ta66,tb33,tb66;
1333 ta33 = in[2*3+0] * c[3];
1334 ta66 = in[2*6+0] * c[6];
1335 tb33 = in[2*3+1] * c[3];
1336 tb66 = in[2*6+1] * c[6];
1339 real tmp1a,tmp2a,tmp1b,tmp2b;
1340 tmp1a = in[2*1+0] * c[1] + ta33 + in[2*5+0] * c[5] + in[2*7+0] * c[7];
1341 tmp1b = in[2*1+1] * c[1] + tb33 + in[2*5+1] * c[5] + in[2*7+1] * c[7];
1342 tmp2a = in[2*0+0] + in[2*2+0] * c[2] + in[2*4+0] * c[4] + ta66 + in[2*8+0] * c[8];
1343 tmp2b = in[2*0+1] + in[2*2+1] * c[2] + in[2*4+1] * c[4] + tb66 + in[2*8+1] * c[8];
1350 real tmp1a,tmp2a,tmp1b,tmp2b;
1351 tmp1a = ( in[2*1+0] - in[2*5+0] - in[2*7+0] ) * c[3];
1352 tmp1b = ( in[2*1+1] - in[2*5+1] - in[2*7+1] ) * c[3];
1353 tmp2a = ( in[2*2+0] - in[2*4+0] - in[2*8+0] ) * c[6] - in[2*6+0] + in[2*0+0];
1354 tmp2b = ( in[2*2+1] - in[2*4+1] - in[2*8+1] ) * c[6] - in[2*6+1] + in[2*0+1];
1361 real tmp1a,tmp2a,tmp1b,tmp2b;
1362 tmp1a = in[2*1+0] * c[5] - ta33 - in[2*5+0] * c[7] + in[2*7+0] * c[1];
1363 tmp1b = in[2*1+1] * c[5] - tb33 - in[2*5+1] * c[7] + in[2*7+1] * c[1];
1364 tmp2a = in[2*0+0] - in[2*2+0] * c[8] - in[2*4+0] * c[2] + ta66 + in[2*8+0] * c[4];
1365 tmp2b = in[2*0+1] - in[2*2+1] * c[8] - in[2*4+1] * c[2] + tb66 + in[2*8+1] * c[4];
1372 real tmp1a,tmp2a,tmp1b,tmp2b;
1373 tmp1a = in[2*1+0] * c[7] - ta33 + in[2*5+0] * c[1] - in[2*7+0] * c[5];
1374 tmp1b = in[2*1+1] * c[7] - tb33 + in[2*5+1] * c[1] - in[2*7+1] * c[5];
1375 tmp2a = in[2*0+0] - in[2*2+0] * c[4] + in[2*4+0] * c[8] + ta66 - in[2*8+0] * c[2];
1376 tmp2b = in[2*0+1] - in[2*2+1] * c[4] + in[2*4+1] * c[8] + tb66 - in[2*8+1] * c[2];
1384 sum0 = in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0];
1385 sum1 = (in[2*0+1] - in[2*2+1] + in[2*4+1] - in[2*6+1] + in[2*8+1] ) * tfcos36[4];
1397 static void dct12(real *in,real *rawout1,real *rawout2,real *wi,real *ts)
1399 #define DCT12_PART1 \
1401 in5 += (in4 = in[4*3]); \
1402 in4 += (in3 = in[3*3]); \
1403 in3 += (in2 = in[2*3]); \
1404 in2 += (in1 = in[1*3]); \
1405 in1 += (in0 = in[0*3]); \
1407 in5 += in3; in3 += in1; \
1412 #define DCT12_PART2 \
1413 in0 += in4 * COS6_2; \
1418 in1 += in5 * COS6_2; \
1420 in5 = (in1 + in3) * tfcos12[0]; \
1421 in1 = (in1 - in3) * tfcos12[2]; \
1431 real in0,in1,in2,in3,in4,in5;
1432 real *out1 = rawout1;
1433 ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2];
1434 ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5];
1439 real tmp0,tmp1 = (in0 - in4);
1441 real tmp2 = (in1 - in5) * tfcos12[1];
1445 ts[(17-1)*SBLIMIT] = out1[17-1] + tmp0 * wi[11-1];
1446 ts[(12+1)*SBLIMIT] = out1[12+1] + tmp0 * wi[6+1];
1447 ts[(6 +1)*SBLIMIT] = out1[6 +1] + tmp1 * wi[1];
1448 ts[(11-1)*SBLIMIT] = out1[11-1] + tmp1 * wi[5-1];
1453 ts[(17-0)*SBLIMIT] = out1[17-0] + in2 * wi[11-0];
1454 ts[(12+0)*SBLIMIT] = out1[12+0] + in2 * wi[6+0];
1455 ts[(12+2)*SBLIMIT] = out1[12+2] + in3 * wi[6+2];
1456 ts[(17-2)*SBLIMIT] = out1[17-2] + in3 * wi[11-2];
1458 ts[(6+0)*SBLIMIT] = out1[6+0] + in0 * wi[0];
1459 ts[(11-0)*SBLIMIT] = out1[11-0] + in0 * wi[5-0];
1460 ts[(6+2)*SBLIMIT] = out1[6+2] + in4 * wi[2];
1461 ts[(11-2)*SBLIMIT] = out1[11-2] + in4 * wi[5-2];
1467 real in0,in1,in2,in3,in4,in5;
1468 real *out2 = rawout2;
1473 real tmp0,tmp1 = (in0 - in4);
1475 real tmp2 = (in1 - in5) * tfcos12[1];
1479 out2[5-1] = tmp0 * wi[11-1];
1480 out2[0+1] = tmp0 * wi[6+1];
1481 ts[(12+1)*SBLIMIT] += tmp1 * wi[1];
1482 ts[(17-1)*SBLIMIT] += tmp1 * wi[5-1];
1487 out2[5-0] = in2 * wi[11-0];
1488 out2[0+0] = in2 * wi[6+0];
1489 out2[0+2] = in3 * wi[6+2];
1490 out2[5-2] = in3 * wi[11-2];
1492 ts[(12+0)*SBLIMIT] += in0 * wi[0];
1493 ts[(17-0)*SBLIMIT] += in0 * wi[5-0];
1494 ts[(12+2)*SBLIMIT] += in4 * wi[2];
1495 ts[(17-2)*SBLIMIT] += in4 * wi[5-2];
1501 real in0,in1,in2,in3,in4,in5;
1502 real *out2 = rawout2;
1503 out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0;
1508 real tmp0,tmp1 = (in0 - in4);
1510 real tmp2 = (in1 - in5) * tfcos12[1];
1514 out2[11-1] = tmp0 * wi[11-1];
1515 out2[6 +1] = tmp0 * wi[6+1];
1516 out2[0+1] += tmp1 * wi[1];
1517 out2[5-1] += tmp1 * wi[5-1];
1522 out2[11-0] = in2 * wi[11-0];
1523 out2[6 +0] = in2 * wi[6+0];
1524 out2[6 +2] = in3 * wi[6+2];
1525 out2[11-2] = in3 * wi[11-2];
1527 out2[0+0] += in0 * wi[0];
1528 out2[5-0] += in0 * wi[5-0];
1529 out2[0+2] += in4 * wi[2];
1530 out2[5-2] += in4 * wi[5-2];
1539 III_hybrid(PMPSTR mp, real fsIn[SBLIMIT][SSLIMIT], real tsOut[SSLIMIT][SBLIMIT],
1540 int ch, struct gr_info_s *gr_infos)
1542 real *tspnt = (real *) tsOut;
1543 real(*block)[2][SBLIMIT * SSLIMIT] = mp->hybrid_block;
1544 int *blc = mp->hybrid_blc;
1545 real *rawout1, *rawout2;
1551 rawout1 = block[b][ch];
1553 rawout2 = block[b][ch];
1558 if (gr_infos->mixed_block_flag) {
1560 dct36(fsIn[0], rawout1, rawout2, win[0], tspnt);
1561 dct36(fsIn[1], rawout1 + 18, rawout2 + 18, win1[0], tspnt + 1);
1567 bt = gr_infos->block_type;
1569 for (; sb < (int) gr_infos->maxb; sb += 2, tspnt += 2, rawout1 += 36, rawout2 += 36) {
1570 dct12(fsIn[sb], rawout1, rawout2, win[2], tspnt);
1571 dct12(fsIn[sb + 1], rawout1 + 18, rawout2 + 18, win1[2], tspnt + 1);
1575 for (; sb < (int) gr_infos->maxb; sb += 2, tspnt += 2, rawout1 += 36, rawout2 += 36) {
1576 dct36(fsIn[sb], rawout1, rawout2, win[bt], tspnt);
1577 dct36(fsIn[sb + 1], rawout1 + 18, rawout2 + 18, win1[bt], tspnt + 1);
1581 for (; sb < SBLIMIT; sb++, tspnt++) {
1583 for (i = 0; i < SSLIMIT; i++) {
1584 tspnt[i * SBLIMIT] = *rawout1++;
1591 * main layer3 handler
1595 layer3_audiodata_precedesframes(PMPSTR mp)
1597 int audioDataInFrame;
1598 int framesToBacktrack;
1600 /* specific to Layer 3, since Layer 1 & 2 the audio data starts at the frame that describes it. */
1601 /* determine how many bytes and therefore bitstream frames the audio data precedes it's matching frame */
1602 /* lame_report_fnc(mp->report_err, "hip: main_data_begin = %d, mp->bsize %d, mp->fsizeold %d, mp->ssize %d\n",
1603 sideinfo.main_data_begin, mp->bsize, mp->fsizeold, mp->ssize); */
1604 /* compute the number of frames to backtrack, 4 for the header, ssize already holds the CRC */
1605 /* TODO Erroneously assumes current frame is same as previous frame. */
1606 audioDataInFrame = mp->bsize - 4 - mp->ssize;
1607 framesToBacktrack = (mp->sideinfo.main_data_begin + audioDataInFrame - 1) / audioDataInFrame;
1608 /* lame_report_fnc(mp->report_err, "hip: audioDataInFrame %d framesToBacktrack %d\n", audioDataInFrame, framesToBacktrack); */
1609 return framesToBacktrack;
1613 decode_layer3_sideinfo(PMPSTR mp)
1615 struct frame *fr = &mp->fr;
1616 int stereo = fr->stereo;
1617 int single = fr->single;
1619 int sfreq = fr->sampling_frequency;
1621 int ch, gr, databits;
1623 if (stereo == 1) { /* stream is mono */
1627 if (fr->mode == MPG_MD_JOINT_STEREO) {
1628 ms_stereo = fr->mode_ext & 0x2;
1636 III_get_side_info_2(mp, stereo, ms_stereo, sfreq, single);
1640 III_get_side_info_1(mp, stereo, ms_stereo, sfreq, single);
1644 for (gr = 0; gr < granules; ++gr) {
1645 for (ch = 0; ch < stereo; ++ch) {
1646 struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[gr]);
1647 databits += gr_infos->part2_3_length;
1650 return databits - 8 * mp->sideinfo.main_data_begin;
1656 decode_layer3_frame(PMPSTR mp, unsigned char *pcm_sample, int *pcm_point,
1657 int (*synth_1to1_mono_ptr) (PMPSTR, real *, unsigned char *, int *),
1658 int (*synth_1to1_ptr) (PMPSTR, real *, int, unsigned char *, int *))
1660 int gr, ch, ss, clip = 0;
1661 int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
1662 /* struct III_sideinfo sideinfo; */
1663 struct frame *fr = &(mp->fr);
1664 int stereo = fr->stereo;
1665 int single = fr->single;
1666 int ms_stereo, i_stereo;
1667 int sfreq = fr->sampling_frequency;
1668 int stereo1, granules;
1670 if (set_pointer(mp, (int) mp->sideinfo.main_data_begin) == MP3_ERR)
1673 if (stereo == 1) { /* stream is mono */
1677 else if (single >= 0) /* stream is stereo, but force to mono */
1682 if (fr->mode == MPG_MD_JOINT_STEREO) {
1683 ms_stereo = fr->mode_ext & 0x2;
1684 i_stereo = fr->mode_ext & 0x1;
1687 ms_stereo = i_stereo = 0;
1697 for (gr = 0; gr < granules; gr++) {
1698 static real hybridIn[2][SBLIMIT][SSLIMIT];
1699 static real hybridOut[2][SSLIMIT][SBLIMIT];
1702 struct gr_info_s *gr_infos = &(mp->sideinfo.ch[0].gr[gr]);
1706 part2bits = III_get_scale_factors_2(mp, scalefacs[0], gr_infos, 0);
1708 part2bits = III_get_scale_factors_1(mp, scalefacs[0], gr_infos);
1711 if (mp->pinfo != NULL) {
1713 mp->pinfo->sfbits[gr][0] = part2bits;
1714 for (i = 0; i < 39; i++)
1715 mp->pinfo->sfb_s[gr][0][i] = scalefacs[0][i];
1718 /* lame_report_fnc(mp->report_err, "calling III dequantize sample 1 gr_infos->part2_3_length %d\n", gr_infos->part2_3_length); */
1719 if (III_dequantize_sample(mp, hybridIn[0], scalefacs[0], gr_infos, sfreq, part2bits))
1723 struct gr_info_s *gr_infos = &(mp->sideinfo.ch[1].gr[gr]);
1726 part2bits = III_get_scale_factors_2(mp, scalefacs[1], gr_infos, i_stereo);
1728 part2bits = III_get_scale_factors_1(mp, scalefacs[1], gr_infos);
1730 if (mp->pinfo != NULL) {
1732 mp->pinfo->sfbits[gr][1] = part2bits;
1733 for (i = 0; i < 39; i++)
1734 mp->pinfo->sfb_s[gr][1][i] = scalefacs[1][i];
1737 /* lame_report_fnc(mp->report_err, "calling III dequantize sample 2 gr_infos->part2_3_length %d\n", gr_infos->part2_3_length); */
1738 if (III_dequantize_sample(mp, hybridIn[1], scalefacs[1], gr_infos, sfreq, part2bits))
1743 for (i = 0; i < SBLIMIT * SSLIMIT; i++) {
1745 tmp0 = ((real *) hybridIn[0])[i];
1746 tmp1 = ((real *) hybridIn[1])[i];
1747 ((real *) hybridIn[1])[i] = tmp0 - tmp1;
1748 ((real *) hybridIn[0])[i] = tmp0 + tmp1;
1753 III_i_stereo(hybridIn, scalefacs[1], gr_infos, sfreq, ms_stereo, fr->lsf);
1755 if (ms_stereo || i_stereo || (single == 3)) {
1756 if (gr_infos->maxb > mp->sideinfo.ch[0].gr[gr].maxb)
1757 mp->sideinfo.ch[0].gr[gr].maxb = gr_infos->maxb;
1759 gr_infos->maxb = mp->sideinfo.ch[0].gr[gr].maxb;
1766 real *in0 = (real *) hybridIn[0], *in1 = (real *) hybridIn[1];
1767 for (i = 0; i < (int) (SSLIMIT * gr_infos->maxb); i++, in0++)
1768 *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */
1774 real *in0 = (real *) hybridIn[0], *in1 = (real *) hybridIn[1];
1775 for (i = 0; i < (int) (SSLIMIT * gr_infos->maxb); i++)
1782 if (mp->pinfo != NULL) {
1786 mp->pinfo->bitrate = tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index];
1787 mp->pinfo->sampfreq = freqs[sfreq];
1788 mp->pinfo->emph = fr->emphasis;
1789 mp->pinfo->crc = fr->error_protection;
1790 mp->pinfo->padding = fr->padding;
1791 mp->pinfo->stereo = fr->stereo;
1792 mp->pinfo->js = (fr->mode == MPG_MD_JOINT_STEREO);
1793 mp->pinfo->ms_stereo = ms_stereo;
1794 mp->pinfo->i_stereo = i_stereo;
1795 mp->pinfo->maindata = mp->sideinfo.main_data_begin;
1797 for (ch = 0; ch < stereo1; ch++) {
1798 struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[gr]);
1799 mp->pinfo->big_values[gr][ch] = gr_infos->big_values;
1800 mp->pinfo->scalefac_scale[gr][ch] = gr_infos->scalefac_scale;
1801 mp->pinfo->mixed[gr][ch] = gr_infos->mixed_block_flag;
1802 mp->pinfo->mpg123blocktype[gr][ch] = gr_infos->block_type;
1803 mp->pinfo->mainbits[gr][ch] = gr_infos->part2_3_length;
1804 mp->pinfo->preflag[gr][ch] = gr_infos->preflag;
1806 mp->pinfo->scfsi[ch] = gr_infos->scfsi;
1810 for (ch = 0; ch < stereo1; ch++) {
1811 struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[gr]);
1812 ifqstep = (mp->pinfo->scalefac_scale[gr][ch] == 0) ? .5 : 1.0;
1813 if (2 == gr_infos->block_type) {
1814 for (i = 0; i < 3; i++) {
1815 for (sb = 0; sb < 12; sb++) {
1818 is_p = scalefac[sfb*3+lwin-gr_infos->mixed_block_flag];
1820 /* scalefac was copied into pinfo->sfb_s[] above */
1821 mp->pinfo->sfb_s[gr][ch][j] =
1822 -ifqstep * mp->pinfo->sfb_s[gr][ch][j - gr_infos->mixed_block_flag];
1823 mp->pinfo->sfb_s[gr][ch][j] -= 2 * (mp->pinfo->sub_gain[gr][ch][i]);
1825 mp->pinfo->sfb_s[gr][ch][3 * sb + i] =
1826 -2 * (mp->pinfo->sub_gain[gr][ch][i]);
1830 for (sb = 0; sb < 21; sb++) {
1831 /* scalefac was copied into pinfo->sfb[] above */
1832 mp->pinfo->sfb[gr][ch][sb] = mp->pinfo->sfb_s[gr][ch][sb];
1833 if (gr_infos->preflag)
1834 mp->pinfo->sfb[gr][ch][sb] += pretab1[sb];
1835 mp->pinfo->sfb[gr][ch][sb] *= -ifqstep;
1837 mp->pinfo->sfb[gr][ch][21] = 0;
1843 for (ch = 0; ch < stereo1; ch++) {
1845 for (sb = 0; sb < SBLIMIT; sb++)
1846 for (ss = 0; ss < SSLIMIT; ss++, j++)
1847 mp->pinfo->mpg123xr[gr][ch][j] = hybridIn[ch][sb][ss];
1852 for (ch = 0; ch < stereo1; ch++) {
1853 struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[gr]);
1854 III_antialias(hybridIn[ch], gr_infos);
1855 III_hybrid(mp, hybridIn[ch], hybridOut[ch], ch, gr_infos);
1858 for (ss = 0; ss < SSLIMIT; ss++) {
1860 clip += (*synth_1to1_mono_ptr) (mp, hybridOut[0][ss], pcm_sample, pcm_point);
1863 int p1 = *pcm_point;
1864 clip += (*synth_1to1_ptr) (mp, hybridOut[0][ss], 0, pcm_sample, &p1);
1865 clip += (*synth_1to1_ptr) (mp, hybridOut[1][ss], 1, pcm_sample, pcm_point);