Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.         leyer3.c: the layer 3 decoder
  3.  
  4.         copyright 1995-2008 by the mpg123 project - free software under the terms of the LGPL 2.1
  5.         see COPYING and AUTHORS files in distribution or http://mpg123.org
  6.         initially written by Michael Hipp
  7.  
  8.         Dear visitor:
  9.         If you feel you don't understand fully the works of this file, your feeling might be correct.
  10.  
  11.         Optimize-TODO: put short bands into the band-field without the stride of 3 reals
  12.         Length-optimze: unify long and short band code where it is possible
  13.  
  14.         The int-vs-pointer situation has to be cleaned up.
  15. */
  16.  
  17. #include "mpg123lib_intern.h"
  18. #include "huffman.h"
  19. #include "getbits.h"
  20. #include "debug.h"
  21.  
  22. /* define CUT_SFB21 if you want to cut-off the frequency above 16kHz */
  23. #if 0
  24. #define CUT_SFB21
  25. #endif
  26.  
  27. #ifdef REAL_IS_FIXED
  28. #define NEW_DCT9
  29. #include "l3_integer_tables.h"
  30. #else
  31. /* static one-time calculated tables... or so */
  32. static real ispow[8207];
  33. static real aa_ca[8],aa_cs[8];
  34. static real win[4][36];
  35. static real win1[4][36];
  36. real COS9[9]; /* dct36_3dnow wants to use that */
  37. static real COS6_1,COS6_2;
  38. real tfcos36[9]; /* dct36_3dnow wants to use that */
  39. static real tfcos12[3];
  40. #define NEW_DCT9
  41. #ifdef NEW_DCT9
  42. static real cos9[3],cos18[3];
  43. static real tan1_1[16],tan2_1[16],tan1_2[16],tan2_2[16];
  44. static real pow1_1[2][16],pow2_1[2][16],pow1_2[2][16],pow2_2[2][16];
  45. #endif
  46. #endif
  47.  
  48. /* Decoder state data, living on the stack of do_layer3. */
  49.  
  50. struct gr_info_s
  51. {
  52.         int scfsi;
  53.         unsigned part2_3_length;
  54.         unsigned big_values;
  55.         unsigned scalefac_compress;
  56.         unsigned block_type;
  57.         unsigned mixed_block_flag;
  58.         unsigned table_select[3];
  59.         unsigned subblock_gain[3];
  60.         unsigned maxband[3];
  61.         unsigned maxbandl;
  62.         unsigned maxb;
  63.         unsigned region1start;
  64.         unsigned region2start;
  65.         unsigned preflag;
  66.         unsigned scalefac_scale;
  67.         unsigned count1table_select;
  68.         real *full_gain[3];
  69.         real *pow2gain;
  70. };
  71.  
  72. struct III_sideinfo
  73. {
  74.         unsigned main_data_begin;
  75.         unsigned private_bits;
  76.         /* Hm, funny... struct inside struct... */
  77.         struct { struct gr_info_s gr[2]; } ch[2];
  78. };
  79.  
  80. struct bandInfoStruct
  81. {
  82.         int longIdx[23];
  83.         int longDiff[22];
  84.         int shortIdx[14];
  85.         int shortDiff[13];
  86. };
  87.  
  88. /* Techy details about our friendly MPEG data. Fairly constant over the years;-) */
  89. const struct bandInfoStruct bandInfo[9] =
  90. {
  91.         { /* MPEG 1.0 */
  92.                 {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
  93.                 {4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158},
  94.                 {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},
  95.                 {4,4,4,4,6,8,10,12,14,18,22,30,56}
  96.         },
  97.         {
  98.                 {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576},
  99.                 {4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192},
  100.                 {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},
  101.                 {4,4,4,4,6,6,10,12,14,16,20,26,66}
  102.         },
  103.         {
  104.                 {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
  105.                 {4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26},
  106.                 {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},
  107.                 {4,4,4,4,6,8,12,16,20,26,34,42,12}
  108.         },
  109.         { /* MPEG 2.0 */
  110.                 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  111.                 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } ,
  112.                 {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} ,
  113.                 {4,4,4,6,6,8,10,14,18,26,32,42,18 }
  114.         },
  115.         { /* Twiddling 3 values here (not just 330->332!) fixed bug 1895025. */
  116.                 {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,332,394,464,540,576},
  117.                 {6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,54,62,70,76,36 },
  118.                 {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},
  119.                 {4,4,4,6,8,10,12,14,18,24,32,44,12 }
  120.         },
  121.         {
  122.                 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  123.                 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 },
  124.                 {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},
  125.                 {4,4,4,6,8,10,12,14,18,24,30,40,18 }
  126.         },
  127.         { /* MPEG 2.5 */
  128.                 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  129.                 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
  130.                 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
  131.                 {4,4,4,6,8,10,12,14,18,24,30,40,18}
  132.         },
  133.         {
  134.                 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  135.                 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
  136.                 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
  137.                 {4,4,4,6,8,10,12,14,18,24,30,40,18}
  138.         },
  139.         {
  140.                 {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576},
  141.                 {12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2},
  142.                 {0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576},
  143.                 {8,8,8,12,16,20,24,28,36,2,2,2,26}
  144.         }
  145. };
  146.  
  147. static int mapbuf0[9][152];
  148. static int mapbuf1[9][156];
  149. static int mapbuf2[9][44];
  150. static int *map[9][3];
  151. static int *mapend[9][3];
  152.  
  153. static unsigned int n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
  154. static unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
  155.  
  156. /* Some helpers used in init_layer3 */
  157.  
  158. #ifdef OPT_MMXORSSE
  159. real init_layer3_gainpow2_mmx(mpg123_handle *fr, int i)
  160. {
  161.         if(!fr->p.down_sample) return DOUBLE_TO_REAL(16384.0 * pow((double)2.0,-0.25 * (double) (i+210) ));
  162.         else return DOUBLE_TO_REAL(pow((double)2.0,-0.25 * (double) (i+210)));
  163. }
  164. #endif
  165.  
  166. real init_layer3_gainpow2(mpg123_handle *fr, int i)
  167. {
  168. #if defined(REAL_IS_FIXED) && defined(PRECALC_TABLES)
  169.         return gainpow2[i+256];
  170. #else
  171.         return DOUBLE_TO_REAL_SCALE_LAYER3(pow((double)2.0,-0.25 * (double) (i+210)),i+256);
  172. #endif
  173. }
  174.  
  175.  
  176. /* init tables for layer-3 ... specific with the downsampling... */
  177. void init_layer3(void)
  178. {
  179.         int i,j,k,l;
  180.  
  181. #if !defined(REAL_IS_FIXED) || !defined(PRECALC_TABLES)
  182.         for(i=0;i<8207;i++)
  183.         ispow[i] = DOUBLE_TO_REAL_POW43(pow((double)i,(double)4.0/3.0));
  184.  
  185.         for(i=0;i<8;i++)
  186.         {
  187.                 const double Ci[8] = {-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
  188.                 double sq = sqrt(1.0+Ci[i]*Ci[i]);
  189.                 aa_cs[i] = DOUBLE_TO_REAL(1.0/sq);
  190.                 aa_ca[i] = DOUBLE_TO_REAL(Ci[i]/sq);
  191.         }
  192.  
  193.         for(i=0;i<18;i++)
  194.         {
  195.                 win[0][i]    = win[1][i]    =
  196.                         DOUBLE_TO_REAL( 0.5*sin(M_PI/72.0 * (double)(2*(i+0) +1)) / cos(M_PI * (double)(2*(i+0) +19) / 72.0) );
  197.                 win[0][i+18] = win[3][i+18] =
  198.                         DOUBLE_TO_REAL( 0.5*sin(M_PI/72.0 * (double)(2*(i+18)+1)) / cos(M_PI * (double)(2*(i+18)+19) / 72.0) );
  199.         }
  200.         for(i=0;i<6;i++)
  201.         {
  202.                 win[1][i+18] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 ));
  203.                 win[3][i+12] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 ));
  204.                 win[1][i+24] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+13) ) / cos ( M_PI * (double) (2*(i+24)+19) / 72.0 ));
  205.                 win[1][i+30] = win[3][i] = DOUBLE_TO_REAL(0.0);
  206.                 win[3][i+6 ] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+1 ) ) / cos ( M_PI * (double) (2*(i+6 )+19) / 72.0 ));
  207.         }
  208.  
  209.         for(i=0;i<9;i++)
  210.         COS9[i] = DOUBLE_TO_REAL(cos( M_PI / 18.0 * (double) i));
  211.  
  212.         for(i=0;i<9;i++)
  213.         tfcos36[i] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 ));
  214.  
  215.         for(i=0;i<3;i++)
  216.         tfcos12[i] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 ));
  217.  
  218.         COS6_1 = DOUBLE_TO_REAL(cos( M_PI / 6.0 * (double) 1));
  219.         COS6_2 = DOUBLE_TO_REAL(cos( M_PI / 6.0 * (double) 2));
  220.  
  221. #ifdef NEW_DCT9
  222.         cos9[0]  = DOUBLE_TO_REAL(cos(1.0*M_PI/9.0));
  223.         cos9[1]  = DOUBLE_TO_REAL(cos(5.0*M_PI/9.0));
  224.         cos9[2]  = DOUBLE_TO_REAL(cos(7.0*M_PI/9.0));
  225.         cos18[0] = DOUBLE_TO_REAL(cos(1.0*M_PI/18.0));
  226.         cos18[1] = DOUBLE_TO_REAL(cos(11.0*M_PI/18.0));
  227.         cos18[2] = DOUBLE_TO_REAL(cos(13.0*M_PI/18.0));
  228. #endif
  229.  
  230.         for(i=0;i<12;i++)
  231.         {
  232.                 win[2][i] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*i+7) / 24.0 ));
  233.         }
  234.  
  235.         for(i=0;i<16;i++)
  236.         {
  237.                 double t = tan( (double) i * M_PI / 12.0 );
  238.                 tan1_1[i] = DOUBLE_TO_REAL_15(t / (1.0+t));
  239.                 tan2_1[i] = DOUBLE_TO_REAL_15(1.0 / (1.0 + t));
  240.                 tan1_2[i] = DOUBLE_TO_REAL_15(M_SQRT2 * t / (1.0+t));
  241.                 tan2_2[i] = DOUBLE_TO_REAL_15(M_SQRT2 / (1.0 + t));
  242.  
  243.                 for(j=0;j<2;j++)
  244.                 {
  245.                         double base = pow(2.0,-0.25*(j+1.0));
  246.                         double p1=1.0,p2=1.0;
  247.                         if(i > 0)
  248.                         {
  249.                                 if( i & 1 ) p1 = pow(base,(i+1.0)*0.5);
  250.                                 else p2 = pow(base,i*0.5);
  251.                         }
  252.                         pow1_1[j][i] = DOUBLE_TO_REAL_15(p1);
  253.                         pow2_1[j][i] = DOUBLE_TO_REAL_15(p2);
  254.                         pow1_2[j][i] = DOUBLE_TO_REAL_15(M_SQRT2 * p1);
  255.                         pow2_2[j][i] = DOUBLE_TO_REAL_15(M_SQRT2 * p2);
  256.                 }
  257.         }
  258. #endif
  259.  
  260.         for(j=0;j<4;j++)
  261.         {
  262.                 const int len[4] = { 36,36,12,36 };
  263.                 for(i=0;i<len[j];i+=2) win1[j][i] = + win[j][i];
  264.  
  265.                 for(i=1;i<len[j];i+=2) win1[j][i] = - win[j][i];
  266.         }
  267.  
  268.         for(j=0;j<9;j++)
  269.         {
  270.                 const struct bandInfoStruct *bi = &bandInfo[j];
  271.                 int *mp;
  272.                 int cb,lwin;
  273.                 const int *bdf;
  274.  
  275.                 mp = map[j][0] = mapbuf0[j];
  276.                 bdf = bi->longDiff;
  277.                 for(i=0,cb = 0; cb < 8 ; cb++,i+=*bdf++)
  278.                 {
  279.                         *mp++ = (*bdf) >> 1;
  280.                         *mp++ = i;
  281.                         *mp++ = 3;
  282.                         *mp++ = cb;
  283.                 }
  284.                 bdf = bi->shortDiff+3;
  285.                 for(cb=3;cb<13;cb++)
  286.                 {
  287.                         int l = (*bdf++) >> 1;
  288.                         for(lwin=0;lwin<3;lwin++)
  289.                         {
  290.                                 *mp++ = l;
  291.                                 *mp++ = i + lwin;
  292.                                 *mp++ = lwin;
  293.                                 *mp++ = cb;
  294.                         }
  295.                         i += 6*l;
  296.                 }
  297.                 mapend[j][0] = mp;
  298.  
  299.                 mp = map[j][1] = mapbuf1[j];
  300.                 bdf = bi->shortDiff+0;
  301.                 for(i=0,cb=0;cb<13;cb++)
  302.                 {
  303.                         int l = (*bdf++) >> 1;
  304.                         for(lwin=0;lwin<3;lwin++)
  305.                         {
  306.                                 *mp++ = l;
  307.                                 *mp++ = i + lwin;
  308.                                 *mp++ = lwin;
  309.                                 *mp++ = cb;
  310.                         }
  311.                         i += 6*l;
  312.                 }
  313.                 mapend[j][1] = mp;
  314.  
  315.                 mp = map[j][2] = mapbuf2[j];
  316.                 bdf = bi->longDiff;
  317.                 for(cb = 0; cb < 22 ; cb++)
  318.                 {
  319.                         *mp++ = (*bdf++) >> 1;
  320.                         *mp++ = cb;
  321.                 }
  322.                 mapend[j][2] = mp;
  323.         }
  324.  
  325.         /* Now for some serious loopings! */
  326.         for(i=0;i<5;i++)
  327.         for(j=0;j<6;j++)
  328.         for(k=0;k<6;k++)
  329.         {
  330.                 int n = k + j * 6 + i * 36;
  331.                 i_slen2[n] = i|(j<<3)|(k<<6)|(3<<12);
  332.         }
  333.         for(i=0;i<4;i++)
  334.         for(j=0;j<4;j++)
  335.         for(k=0;k<4;k++)
  336.         {
  337.                 int n = k + j * 4 + i * 16;
  338.                 i_slen2[n+180] = i|(j<<3)|(k<<6)|(4<<12);
  339.         }
  340.         for(i=0;i<4;i++)
  341.         for(j=0;j<3;j++)
  342.         {
  343.                 int n = j + i * 3;
  344.                 i_slen2[n+244] = i|(j<<3) | (5<<12);
  345.                 n_slen2[n+500] = i|(j<<3) | (2<<12) | (1<<15);
  346.         }
  347.         for(i=0;i<5;i++)
  348.         for(j=0;j<5;j++)
  349.         for(k=0;k<4;k++)
  350.         for(l=0;l<4;l++)
  351.         {
  352.                 int n = l + k * 4 + j * 16 + i * 80;
  353.                 n_slen2[n] = i|(j<<3)|(k<<6)|(l<<9)|(0<<12);
  354.         }
  355.         for(i=0;i<5;i++)
  356.         for(j=0;j<5;j++)
  357.         for(k=0;k<4;k++)
  358.         {
  359.                 int n = k + j * 4 + i * 20;
  360.                 n_slen2[n+400] = i|(j<<3)|(k<<6)|(1<<12);
  361.         }
  362. }
  363.  
  364.  
  365. void init_layer3_stuff(mpg123_handle *fr, real (*gainpow2)(mpg123_handle *fr, int i))
  366. {
  367.         int i,j;
  368.  
  369.         for(i=-256;i<118+4;i++) fr->gainpow2[i+256] = gainpow2(fr,i);
  370.  
  371.         for(j=0;j<9;j++)
  372.         {
  373.                 for(i=0;i<23;i++)
  374.                 {
  375.                         fr->longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
  376.                         if(fr->longLimit[j][i] > (fr->down_sample_sblimit) )
  377.                         fr->longLimit[j][i] = fr->down_sample_sblimit;
  378.                 }
  379.                 for(i=0;i<14;i++)
  380.                 {
  381.                         fr->shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
  382.                         if(fr->shortLimit[j][i] > (fr->down_sample_sblimit) )
  383.                         fr->shortLimit[j][i] = fr->down_sample_sblimit;
  384.                 }
  385.         }
  386. }
  387.  
  388. /*
  389.         Observe!
  390.         Now come the actualy decoding routines.
  391. */
  392.  
  393. /* read additional side information (for MPEG 1 and MPEG 2) */
  394. static int III_get_side_info(mpg123_handle *fr, struct III_sideinfo *si,int stereo, int ms_stereo,long sfreq,int single)
  395. {
  396.         int ch, gr;
  397.         int powdiff = (single == SINGLE_MIX) ? 4 : 0;
  398.  
  399.         const int tabs[2][5] = { { 2,9,5,3,4 } , { 1,8,1,2,9 } };
  400.         const int *tab = tabs[fr->lsf];
  401.  
  402.         si->main_data_begin = getbits(fr, tab[1]);
  403.  
  404.         if(si->main_data_begin > fr->bitreservoir)
  405.         {
  406.                 if(VERBOSE2) fprintf(stderr, "Note: missing %d bytes in bit reservoir for frame %li\n", (int)(si->main_data_begin - fr->bitreservoir), (long)fr->num);
  407.  
  408.                 /*  overwrite main_data_begin for the really available bit reservoir */
  409.                 backbits(fr, tab[1]);
  410.                 if(fr->lsf == 0)
  411.                 {
  412.                         fr->wordpointer[0] = (unsigned char) (fr->bitreservoir >> 1);
  413.                         fr->wordpointer[1] = (unsigned char) ((fr->bitreservoir & 1) << 7);
  414.                 }
  415.                 else fr->wordpointer[0] = (unsigned char) fr->bitreservoir;
  416.  
  417.                 /* zero "side-info" data for a silence-frame
  418.                 without touching audio data used as bit reservoir for following frame */
  419.                 memset(fr->wordpointer+2, 0, fr->ssize-2);
  420.  
  421.                 /* reread the new bit reservoir offset */
  422.                 si->main_data_begin = getbits(fr, tab[1]);
  423.         }
  424.  
  425.         /* Keep track of the available data bytes for the bit reservoir.
  426.         Think: Substract the 2 crc bytes in parser already? */
  427.         fr->bitreservoir = fr->bitreservoir + fr->framesize - fr->ssize - (fr->error_protection ? 2 : 0);
  428.         /* Limit the reservoir to the max for MPEG 1.0 or 2.x . */
  429.         if(fr->bitreservoir > (unsigned int) (fr->lsf == 0 ? 511 : 255))
  430.         fr->bitreservoir = (fr->lsf == 0 ? 511 : 255);
  431.  
  432.         /* Now back into less commented territory. It's code. It works. */
  433.  
  434.         if (stereo == 1)
  435.         si->private_bits = getbits_fast(fr, tab[2]);
  436.         else
  437.         si->private_bits = getbits_fast(fr, tab[3]);
  438.  
  439.         if(!fr->lsf) for(ch=0; ch<stereo; ch++)
  440.         {
  441.                 si->ch[ch].gr[0].scfsi = -1;
  442.                 si->ch[ch].gr[1].scfsi = getbits_fast(fr, 4);
  443.         }
  444.  
  445.         for (gr=0; gr<tab[0]; gr++)
  446.         for (ch=0; ch<stereo; ch++)
  447.         {
  448.                 register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
  449.  
  450.                 gr_info->part2_3_length = getbits(fr, 12);
  451.                 gr_info->big_values = getbits(fr, 9);
  452.                 if(gr_info->big_values > 288)
  453.                 {
  454.                         error("big_values too large!");
  455.                         gr_info->big_values = 288;
  456.                 }
  457.                 gr_info->pow2gain = fr->gainpow2+256 - getbits_fast(fr, 8) + powdiff;
  458.                 if(ms_stereo) gr_info->pow2gain += 2;
  459.  
  460.                 gr_info->scalefac_compress = getbits(fr, tab[4]);
  461.  
  462.                 if(get1bit(fr))
  463.                 { /* window switch flag  */
  464.                         int i;
  465.                         gr_info->block_type       = getbits_fast(fr, 2);
  466.                         gr_info->mixed_block_flag = get1bit(fr);
  467.                         gr_info->table_select[0]  = getbits_fast(fr, 5);
  468.                         gr_info->table_select[1]  = getbits_fast(fr, 5);
  469.                         /*
  470.                                 table_select[2] not needed, because there is no region2,
  471.                                 but to satisfy some verification tools we set it either.
  472.                         */
  473.                         gr_info->table_select[2] = 0;
  474.                         for(i=0;i<3;i++)
  475.                         gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(fr, 3)<<3);
  476.  
  477.                         if(gr_info->block_type == 0)
  478.                         {
  479.                                 error("Blocktype == 0 and window-switching == 1 not allowed.");
  480.                                 return 1;
  481.                         }
  482.  
  483.                         /* region_count/start parameters are implicit in this case. */      
  484.                         if( (!fr->lsf || (gr_info->block_type == 2)) && !fr->mpeg25)
  485.                         {
  486.                                 gr_info->region1start = 36>>1;
  487.                                 gr_info->region2start = 576>>1;
  488.                         }
  489.                         else
  490.                         {
  491.                                 if(fr->mpeg25)
  492.                                 {
  493.                                         int r0c,r1c;
  494.                                         if((gr_info->block_type == 2) && (!gr_info->mixed_block_flag) ) r0c = 5;
  495.                                         else r0c = 7;
  496.  
  497.                                         r1c = 20 - r0c;
  498.                                         gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
  499.                                         gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
  500.                                 }
  501.                                 else
  502.                                 {
  503.                                         gr_info->region1start = 54>>1;
  504.                                         gr_info->region2start = 576>>1;
  505.                                 }
  506.                         }
  507.                 }
  508.                 else
  509.                 {
  510.                         int i,r0c,r1c;
  511.                         for (i=0; i<3; i++)
  512.                         gr_info->table_select[i] = getbits_fast(fr, 5);
  513.  
  514.                         r0c = getbits_fast(fr, 4);
  515.                         r1c = getbits_fast(fr, 3);
  516.                         gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
  517.                         gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
  518.  
  519.                         if(r0c + r1c + 2 > 22) gr_info->region2start = 576>>1;
  520.                         else gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
  521.  
  522.                         gr_info->block_type = 0;
  523.                         gr_info->mixed_block_flag = 0;
  524.                 }
  525.                 if(!fr->lsf) gr_info->preflag = get1bit(fr);
  526.  
  527.                 gr_info->scalefac_scale = get1bit(fr);
  528.                 gr_info->count1table_select = get1bit(fr);
  529.         }
  530.         return 0;
  531. }
  532.  
  533.  
  534. /* read scalefactors */
  535. static int III_get_scale_factors_1(mpg123_handle *fr, int *scf,struct gr_info_s *gr_info,int ch,int gr)
  536. {
  537.         const unsigned char slen[2][16] =
  538.         {
  539.                 {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
  540.                 {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
  541.         };
  542.         int numbits;
  543.         int num0 = slen[0][gr_info->scalefac_compress];
  544.         int num1 = slen[1][gr_info->scalefac_compress];
  545.  
  546.         if(gr_info->block_type == 2)
  547.         {
  548.                 int i=18;
  549.                 numbits = (num0 + num1) * 18;
  550.  
  551.                 if(gr_info->mixed_block_flag)
  552.                 {
  553.                         for (i=8;i;i--)
  554.                         *scf++ = getbits_fast(fr, num0);
  555.  
  556.                         i = 9;
  557.                         numbits -= num0; /* num0 * 17 + num1 * 18 */
  558.                 }
  559.  
  560.                 for(;i;i--) *scf++ = getbits_fast(fr, num0);
  561.  
  562.                 for(i = 18; i; i--) *scf++ = getbits_fast(fr, num1);
  563.  
  564.                 *scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */
  565.         }
  566.         else
  567.         {
  568.                 int i;
  569.                 int scfsi = gr_info->scfsi;
  570.  
  571.                 if(scfsi < 0)
  572.                 { /* scfsi < 0 => granule == 0 */
  573.                         for(i=11;i;i--) *scf++ = getbits_fast(fr, num0);
  574.  
  575.                         for(i=10;i;i--) *scf++ = getbits_fast(fr, num1);
  576.  
  577.                         numbits = (num0 + num1) * 10 + num0;
  578.                         *scf++ = 0;
  579.                 }
  580.                 else
  581.                 {
  582.                         numbits = 0;
  583.                         if(!(scfsi & 0x8))
  584.                         {
  585.                                 for (i=0;i<6;i++) *scf++ = getbits_fast(fr, num0);
  586.  
  587.                                 numbits += num0 * 6;
  588.                         }
  589.                         else scf += 6;
  590.  
  591.                         if(!(scfsi & 0x4))
  592.                         {
  593.                                 for (i=0;i<5;i++) *scf++ = getbits_fast(fr, num0);
  594.  
  595.                                 numbits += num0 * 5;
  596.                         }
  597.                         else scf += 5;
  598.  
  599.                         if(!(scfsi & 0x2))
  600.                         {
  601.                                 for(i=0;i<5;i++) *scf++ = getbits_fast(fr, num1);
  602.  
  603.                                 numbits += num1 * 5;
  604.                         }
  605.                         else scf += 5;
  606.  
  607.                         if(!(scfsi & 0x1))
  608.                         {
  609.                                 for (i=0;i<5;i++) *scf++ = getbits_fast(fr, num1);
  610.  
  611.                                 numbits += num1 * 5;
  612.                         }
  613.                         else scf += 5;
  614.  
  615.                         *scf++ = 0;  /* no l[21] in original sources */
  616.                 }
  617.         }
  618.         return numbits;
  619. }
  620.  
  621.  
  622. static int III_get_scale_factors_2(mpg123_handle *fr, int *scf,struct gr_info_s *gr_info,int i_stereo)
  623. {
  624.         const unsigned char *pnt;
  625.         int i,j,n=0,numbits=0;
  626.         unsigned int slen;
  627.  
  628.         const unsigned char stab[3][6][4] =
  629.         {
  630.                 {
  631.                         { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0},
  632.                         { 7, 7, 7,0 } , { 6, 6, 6,3 } , {  8, 8,5,0}
  633.                 },
  634.                 {
  635.                         { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0},
  636.                         {12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0}
  637.                 },
  638.                 {
  639.                         { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0},
  640.                         { 6,15,12,0 } , { 6,12, 9,6 } , {  6,18,9,0}
  641.                 }
  642.         };
  643.  
  644.         if(i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */
  645.         slen = i_slen2[gr_info->scalefac_compress>>1];
  646.         else
  647.         slen = n_slen2[gr_info->scalefac_compress];
  648.  
  649.         gr_info->preflag = (slen>>15) & 0x1;
  650.  
  651.         n = 0;  
  652.         if( gr_info->block_type == 2 )
  653.         {
  654.                 n++;
  655.                 if(gr_info->mixed_block_flag) n++;
  656.         }
  657.  
  658.         pnt = stab[n][(slen>>12)&0x7];
  659.  
  660.         for(i=0;i<4;i++)
  661.         {
  662.                 int num = slen & 0x7;
  663.                 slen >>= 3;
  664.                 if(num)
  665.                 {
  666.                         for(j=0;j<(int)(pnt[i]);j++) *scf++ = getbits_fast(fr, num);
  667.  
  668.                         numbits += pnt[i] * num;
  669.                 }
  670.                 else
  671.                 for(j=0;j<(int)(pnt[i]);j++) *scf++ = 0;
  672.         }
  673.  
  674.         n = (n << 1) + 1;
  675.         for(i=0;i<n;i++) *scf++ = 0;
  676.  
  677.         return numbits;
  678. }
  679.  
  680. 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};
  681. 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};
  682.  
  683. /*
  684.         Dequantize samples
  685.         ...includes Huffman decoding
  686. */
  687.  
  688. /* 24 is enough because tab13 has max. a 19 bit huffvector */
  689. #define BITSHIFT ((sizeof(long)-1)*8)
  690. #define REFRESH_MASK \
  691.         while(num < BITSHIFT) { \
  692.                 mask |= ((unsigned long)getbyte(fr))<<(BITSHIFT-num); \
  693.                 num += 8; \
  694.                 part2remain -= 8; }
  695.  
  696. static int III_dequantize_sample(mpg123_handle *fr, real xr[SBLIMIT][SSLIMIT],int *scf, struct gr_info_s *gr_info,int sfreq,int part2bits)
  697. {
  698.         int shift = 1 + gr_info->scalefac_scale;
  699.         real *xrpnt = (real *) xr;
  700.         int l[3],l3;
  701.         int part2remain = gr_info->part2_3_length - part2bits;
  702.         int *me;
  703. #ifdef REAL_IS_FIXED
  704.         int gainpow2_scale_idx = 378;
  705. #endif
  706.  
  707.         /* mhipp tree has this split up a bit... */
  708.         int num=getbitoffset(fr);
  709.         long mask;
  710.         /* We must split this, because for num==0 the shift is undefined if you do it in one step. */
  711.         mask  = ((unsigned long) getbits(fr, num))<<BITSHIFT;
  712.         mask <<= 8-num;
  713.         part2remain -= num;
  714.  
  715.         {
  716.                 int bv       = gr_info->big_values;
  717.                 int region1  = gr_info->region1start;
  718.                 int region2  = gr_info->region2start;
  719.                 if(region1 > region2)
  720.                 {
  721.                         /*
  722.                                 That's not optimal: it fixes a segfault with fuzzed data, but also apparently triggers where it shouldn't, see bug 1641196.
  723.                                 The benefit of not crashing / having this security risk is bigger than these few frames of a lame-3.70 file that aren't audible anyway.
  724.                                 But still, I want to know if indeed this check or the old lame is at fault.
  725.                         */
  726.                         error("You got some really nasty file there... region1>region2!");
  727.                         return 1;
  728.                 }
  729.                 l3 = ((576>>1)-bv)>>1;  
  730.  
  731.                 /* we may lose the 'odd' bit here !! check this later again */
  732.                 if(bv <= region1)
  733.                 {
  734.                         l[0] = bv;
  735.                         l[1] = 0;
  736.                         l[2] = 0;
  737.                 }
  738.                 else
  739.                 {
  740.                         l[0] = region1;
  741.                         if(bv <= region2)
  742.                         {
  743.                                 l[1] = bv - l[0];
  744.                                 l[2] = 0;
  745.                         }
  746.                         else
  747.                         {
  748.                                 l[1] = region2 - l[0];
  749.                                 l[2] = bv - region2;
  750.                         }
  751.                 }
  752.         }
  753.  
  754.         if(gr_info->block_type == 2)
  755.         {
  756.                 /* decoding with short or mixed mode BandIndex table */
  757.                 int i,max[4];
  758.                 int step=0,lwin=3,cb=0;
  759.                 register real v = 0.0;
  760.                 register int *m,mc;
  761.  
  762.                 if(gr_info->mixed_block_flag)
  763.                 {
  764.                         max[3] = -1;
  765.                         max[0] = max[1] = max[2] = 2;
  766.                         m = map[sfreq][0];
  767.                         me = mapend[sfreq][0];
  768.                 }
  769.                 else
  770.                 {
  771.                         max[0] = max[1] = max[2] = max[3] = -1;
  772.                         /* max[3] not really needed in this case */
  773.                         m = map[sfreq][1];
  774.                         me = mapend[sfreq][1];
  775.                 }
  776.  
  777.                 mc = 0;
  778.                 for(i=0;i<2;i++)
  779.                 {
  780.                         int lp = l[i];
  781.                         struct newhuff *h = ht+gr_info->table_select[i];
  782.                         for(;lp;lp--,mc--)
  783.                         {
  784.                                 register int x,y;
  785.                                 if( (!mc) )
  786.                                 {
  787.                                         mc    = *m++;
  788.                                         xrpnt = ((real *) xr) + (*m++);
  789.                                         lwin  = *m++;
  790.                                         cb    = *m++;
  791.                                         if(lwin == 3)
  792.                                         {
  793. #ifdef REAL_IS_FIXED
  794.                                                 gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
  795. #endif
  796.                                                 v = gr_info->pow2gain[(*scf++) << shift];
  797.                                                 step = 1;
  798.                                         }
  799.                                         else
  800.                                         {
  801. #ifdef REAL_IS_FIXED
  802.                                                 gainpow2_scale_idx = (int)(gr_info->full_gain[lwin] + (*scf << shift) - fr->gainpow2);
  803. #endif
  804.                                                 v = gr_info->full_gain[lwin][(*scf++) << shift];
  805.                                                 step = 3;
  806.                                         }
  807.                                 }
  808.                                 {
  809.                                         register short *val = h->table;
  810.                                         REFRESH_MASK;
  811.                                         while((y=*val++)<0)
  812.                                         {
  813.                                                 if (mask < 0) val -= y;
  814.  
  815.                                                 num--;
  816.                                                 mask <<= 1;
  817.                                         }
  818.                                         x = y >> 4;
  819.                                         y &= 0xf;
  820.                                 }
  821.                                 if(x == 15 && h->linbits)
  822.                                 {
  823.                                         max[lwin] = cb;
  824.                                         REFRESH_MASK;
  825.                                         x += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits);
  826.                                         num -= h->linbits+1;
  827.                                         mask <<= h->linbits;
  828.                                         if(mask < 0) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
  829.                                         else         *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
  830.  
  831.                                         mask <<= 1;
  832.                                 }
  833.                                 else if(x)
  834.                                 {
  835.                                         max[lwin] = cb;
  836.                                         if(mask < 0) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
  837.                                         else         *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
  838.  
  839.                                         num--;
  840.                                         mask <<= 1;
  841.                                 }
  842.                                 else *xrpnt = DOUBLE_TO_REAL(0.0);
  843.  
  844.                                 xrpnt += step;
  845.                                 if(y == 15 && h->linbits)
  846.                                 {
  847.                                         max[lwin] = cb;
  848.                                         REFRESH_MASK;
  849.                                         y += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits);
  850.                                         num -= h->linbits+1;
  851.                                         mask <<= h->linbits;
  852.                                         if(mask < 0) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
  853.                                         else         *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
  854.  
  855.                                         mask <<= 1;
  856.                                 }
  857.                                 else if(y)
  858.                                 {
  859.                                         max[lwin] = cb;
  860.                                         if(mask < 0) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
  861.                                         else         *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
  862.  
  863.                                         num--;
  864.                                         mask <<= 1;
  865.                                 }
  866.                                 else *xrpnt = DOUBLE_TO_REAL(0.0);
  867.  
  868.                                 xrpnt += step;
  869.                         }
  870.                 }
  871.  
  872.                 for(;l3 && (part2remain+num > 0);l3--)
  873.                 {
  874.                         struct newhuff* h;
  875.                         register short* val;
  876.                         register short a;
  877.                         /*
  878.                                 This is only a humble hack to prevent a special segfault.
  879.                                 More insight into the real workings is still needed.
  880.                                 Especially why there are (valid?) files that make xrpnt exceed the array with 4 bytes without segfaulting, more seems to be really bad, though.
  881.                         */
  882.                         #ifdef DEBUG
  883.                         if(!(xrpnt < &xr[SBLIMIT][0]))
  884.                         {
  885.                                 if(VERBOSE) debug2("attempted soft xrpnt overflow (%p !< %p) ?", (void*) xrpnt, (void*) &xr[SBLIMIT][0]);
  886.                         }
  887.                         #endif
  888.                         if(!(xrpnt < &xr[SBLIMIT][0]+5))
  889.                         {
  890.                                 if(NOQUIET) error2("attempted xrpnt overflow (%p !< %p)", (void*) xrpnt, (void*) &xr[SBLIMIT][0]);
  891.                                 return 2;
  892.                         }
  893.                         h = htc+gr_info->count1table_select;
  894.                         val = h->table;
  895.  
  896.                         REFRESH_MASK;
  897.                         while((a=*val++)<0)
  898.                         {
  899.                                 if(mask < 0) val -= a;
  900.  
  901.                                 num--;
  902.                                 mask <<= 1;
  903.                         }
  904.                         if(part2remain+num <= 0)
  905.                         {
  906.                                 num -= part2remain+num;
  907.                                 break;
  908.                         }
  909.  
  910.                         for(i=0;i<4;i++)
  911.                         {
  912.                                 if(!(i & 1))
  913.                                 {
  914.                                         if(!mc)
  915.                                         {
  916.                                                 mc = *m++;
  917.                                                 xrpnt = ((real *) xr) + (*m++);
  918.                                                 lwin = *m++;
  919.                                                 cb = *m++;
  920.                                                 if(lwin == 3)
  921.                                                 {
  922. #ifdef REAL_IS_FIXED
  923.                                                         gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
  924. #endif
  925.                                                         v = gr_info->pow2gain[(*scf++) << shift];
  926.                                                         step = 1;
  927.                                                 }
  928.                                                 else
  929.                                                 {
  930. #ifdef REAL_IS_FIXED
  931.                                                         gainpow2_scale_idx = (int)(gr_info->full_gain[lwin] + (*scf << shift) - fr->gainpow2);
  932. #endif
  933.                                                         v = gr_info->full_gain[lwin][(*scf++) << shift];
  934.                                                         step = 3;
  935.                                                 }
  936.                                         }
  937.                                         mc--;
  938.                                 }
  939.                                 if( (a & (0x8>>i)) )
  940.                                 {
  941.                                         max[lwin] = cb;
  942.                                         if(part2remain+num <= 0)
  943.                                         break;
  944.  
  945.                                         if(mask < 0) *xrpnt = -REAL_SCALE_LAYER3(v, gainpow2_scale_idx);
  946.                                         else         *xrpnt =  REAL_SCALE_LAYER3(v, gainpow2_scale_idx);
  947.  
  948.                                         num--;
  949.                                         mask <<= 1;
  950.                                 }
  951.                                 else *xrpnt = DOUBLE_TO_REAL(0.0);
  952.  
  953.                                 xrpnt += step;
  954.                         }
  955.                 }
  956.  
  957.                 if(lwin < 3)
  958.                 { /* short band? */
  959.                         while(1)
  960.                         {
  961.                                 for(;mc > 0;mc--)
  962.                                 {
  963.                                         *xrpnt = DOUBLE_TO_REAL(0.0); xrpnt += 3; /* short band -> step=3 */
  964.                                         *xrpnt = DOUBLE_TO_REAL(0.0); xrpnt += 3;
  965.                                 }
  966.                                 if(m >= me)
  967.                                 break;
  968.  
  969.                                 mc    = *m++;
  970.                                 xrpnt = ((real *) xr) + *m++;
  971.                                 if(*m++ == 0)
  972.                                 break; /* optimize: field will be set to zero at the end of the function */
  973.  
  974.                                 m++; /* cb */
  975.                         }
  976.                 }
  977.  
  978.                 gr_info->maxband[0] = max[0]+1;
  979.                 gr_info->maxband[1] = max[1]+1;
  980.                 gr_info->maxband[2] = max[2]+1;
  981.                 gr_info->maxbandl   = max[3]+1;
  982.  
  983.                 {
  984.                         int rmax = max[0] > max[1] ? max[0] : max[1];
  985.                         rmax = (rmax > max[2] ? rmax : max[2]) + 1;
  986.                         gr_info->maxb = rmax ? fr->shortLimit[sfreq][rmax] : fr->longLimit[sfreq][max[3]+1];
  987.                 }
  988.  
  989.         }
  990.         else
  991.         {
  992.                 /* decoding with 'long' BandIndex table (block_type != 2) */
  993.                 const int *pretab = gr_info->preflag ? pretab1 : pretab2;
  994.                 int i,max = -1;
  995.                 int cb = 0;
  996.                 int *m = map[sfreq][2];
  997.                 register real v = 0.0;
  998.                 int mc = 0;
  999.  
  1000.                 /* long hash table values */
  1001.                 for(i=0;i<3;i++)
  1002.                 {
  1003.                         int lp = l[i];
  1004.                         struct newhuff *h = ht+gr_info->table_select[i];
  1005.  
  1006.                         for(;lp;lp--,mc--)
  1007.                         {
  1008.                                 int x,y;
  1009.                                 if(!mc)
  1010.                                 {
  1011.                                         mc = *m++;
  1012.                                         cb = *m++;
  1013. #ifdef CUT_SFB21
  1014.                                         if(cb == 21)
  1015.                                                 v = 0.0;
  1016.                                         else
  1017. #endif
  1018.                                         {
  1019. #ifdef REAL_IS_FIXED
  1020.                                                 gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
  1021. #endif
  1022.                                                 v = gr_info->pow2gain[(*(scf++) + (*pretab++)) << shift];
  1023.                                         }
  1024.                                 }
  1025.                                 {
  1026.                                         register short *val = h->table;
  1027.                                         REFRESH_MASK;
  1028.                                         while((y=*val++)<0)
  1029.                                         {
  1030.                                                 if (mask < 0) val -= y;
  1031.  
  1032.                                                 num--;
  1033.                                                 mask <<= 1;
  1034.                                         }
  1035.                                         x = y >> 4;
  1036.                                         y &= 0xf;
  1037.                                 }
  1038.  
  1039.                                 if(x == 15 && h->linbits)
  1040.                                 {
  1041.                                         max = cb;
  1042.                                         REFRESH_MASK;
  1043.                                         x += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits);
  1044.                                         num -= h->linbits+1;
  1045.                                         mask <<= h->linbits;
  1046.                                         if(mask < 0) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
  1047.                                         else         *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
  1048.  
  1049.                                         mask <<= 1;
  1050.                                 }
  1051.                                 else if(x)
  1052.                                 {
  1053.                                         max = cb;
  1054.                                         if(mask < 0) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
  1055.                                         else         *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
  1056.                                         num--;
  1057.  
  1058.                                         mask <<= 1;
  1059.                                 }
  1060.                                 else *xrpnt++ = DOUBLE_TO_REAL(0.0);
  1061.  
  1062.                                 if(y == 15 && h->linbits)
  1063.                                 {
  1064.                                         max = cb;
  1065.                                         REFRESH_MASK;
  1066.                                         y += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits);
  1067.                                         num -= h->linbits+1;
  1068.                                         mask <<= h->linbits;
  1069.                                         if(mask < 0) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
  1070.                                         else         *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
  1071.  
  1072.                                         mask <<= 1;
  1073.                                 }
  1074.                                 else if(y)
  1075.                                 {
  1076.                                         max = cb;
  1077.                                         if(mask < 0) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
  1078.                                         else         *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
  1079.  
  1080.                                         num--;
  1081.                                         mask <<= 1;
  1082.                                 }
  1083.                                 else *xrpnt++ = DOUBLE_TO_REAL(0.0);
  1084.                         }
  1085.                 }
  1086.  
  1087.                 /* short (count1table) values */
  1088.                 for(;l3 && (part2remain+num > 0);l3--)
  1089.                 {
  1090.                         struct newhuff *h = htc+gr_info->count1table_select;
  1091.                         register short *val = h->table,a;
  1092.  
  1093.                         REFRESH_MASK;
  1094.                         while((a=*val++)<0)
  1095.                         {
  1096.                                 if (mask < 0) val -= a;
  1097.  
  1098.                                 num--;
  1099.                                 mask <<= 1;
  1100.                         }
  1101.                         if(part2remain+num <= 0)
  1102.                         {
  1103.                                 num -= part2remain+num;
  1104.                                 break;
  1105.                         }
  1106.  
  1107.                         for(i=0;i<4;i++)
  1108.                         {
  1109.                                 if(!(i & 1))
  1110.                                 {
  1111.                                         if(!mc)
  1112.                                         {
  1113.                                                 mc = *m++;
  1114.                                                 cb = *m++;
  1115. #ifdef CUT_SFB21
  1116.                                                 if(cb == 21)
  1117.                                                         v = 0.0;
  1118.                                                 else
  1119. #endif
  1120.                                                 {
  1121. #ifdef REAL_IS_FIXED
  1122.                                                         gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
  1123. #endif
  1124.                                                         v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
  1125.                                                 }
  1126.                                         }
  1127.                                         mc--;
  1128.                                 }
  1129.                                 if( (a & (0x8>>i)) )
  1130.                                 {
  1131.                                         max = cb;
  1132.                                         if(part2remain+num <= 0)
  1133.                                         break;
  1134.  
  1135.                                         if(mask < 0) *xrpnt++ = -REAL_SCALE_LAYER3(v, gainpow2_scale_idx);
  1136.                                         else         *xrpnt++ =  REAL_SCALE_LAYER3(v, gainpow2_scale_idx);
  1137.  
  1138.                                         num--;
  1139.                                         mask <<= 1;
  1140.                                 }
  1141.                                 else *xrpnt++ = DOUBLE_TO_REAL(0.0);
  1142.                         }
  1143.                 }
  1144.  
  1145.                 gr_info->maxbandl = max+1;
  1146.                 gr_info->maxb = fr->longLimit[sfreq][gr_info->maxbandl];
  1147.         }
  1148.  
  1149.         part2remain += num;
  1150.         backbits(fr, num);
  1151.         num = 0;
  1152.  
  1153.         while(xrpnt < &xr[SBLIMIT][0])
  1154.         *xrpnt++ = DOUBLE_TO_REAL(0.0);
  1155.  
  1156.         while( part2remain > 16 )
  1157.         {
  1158.                 skipbits(fr, 16); /* Dismiss stuffing Bits */
  1159.                 part2remain -= 16;
  1160.         }
  1161.         if(part2remain > 0) skipbits(fr, part2remain);
  1162.         else if(part2remain < 0)
  1163.         {
  1164.                 debug1("Can't rewind stream by %d bits!",-part2remain);
  1165.                 return 1; /* -> error */
  1166.         }
  1167.         return 0;
  1168. }
  1169.  
  1170.  
  1171. /* calculate real channel values for Joint-I-Stereo-mode */
  1172. static void III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT],int *scalefac, struct gr_info_s *gr_info,int sfreq,int ms_stereo,int lsf)
  1173. {
  1174.         real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf;
  1175.         const struct bandInfoStruct *bi = &bandInfo[sfreq];
  1176.  
  1177.         const real *tab1,*tab2;
  1178.  
  1179. #if 1
  1180.         int tab;
  1181. /* TODO: optimize as static */
  1182.         const real *tabs[3][2][2] =
  1183.         {
  1184.                 { { tan1_1,tan2_1 }       , { tan1_2,tan2_2 } },
  1185.                 { { pow1_1[0],pow2_1[0] } , { pow1_2[0],pow2_2[0] } },
  1186.                 { { pow1_1[1],pow2_1[1] } , { pow1_2[1],pow2_2[1] } }
  1187.         };
  1188.  
  1189.         tab = lsf + (gr_info->scalefac_compress & lsf);
  1190.         tab1 = tabs[tab][ms_stereo][0];
  1191.         tab2 = tabs[tab][ms_stereo][1];
  1192. #else
  1193.         if(lsf)
  1194.         {
  1195.                 int p = gr_info->scalefac_compress & 0x1;
  1196.                 if(ms_stereo)
  1197.                 {
  1198.                         tab1 = pow1_2[p];
  1199.                         tab2 = pow2_2[p];
  1200.                 }
  1201.                 else
  1202.                 {
  1203.                         tab1 = pow1_1[p];
  1204.                         tab2 = pow2_1[p];
  1205.                 }
  1206.         }
  1207.         else
  1208.         {
  1209.                 if(ms_stereo)
  1210.                 {
  1211.                         tab1 = tan1_2;
  1212.                         tab2 = tan2_2;
  1213.                 }
  1214.                 else
  1215.                 {
  1216.                         tab1 = tan1_1;
  1217.                         tab2 = tan2_1;
  1218.                 }
  1219.         }
  1220. #endif
  1221.  
  1222.         if(gr_info->block_type == 2)
  1223.         {
  1224.                 int lwin,do_l = 0;
  1225.                 if( gr_info->mixed_block_flag ) do_l = 1;
  1226.  
  1227.                 for(lwin=0;lwin<3;lwin++)
  1228.                 { /* process each window */
  1229.                         /* get first band with zero values */
  1230.                         int is_p,sb,idx,sfb = gr_info->maxband[lwin];  /* sfb is minimal 3 for mixed mode */
  1231.                         if(sfb > 3) do_l = 0;
  1232.  
  1233.                         for(;sfb<12;sfb++)
  1234.                         {
  1235.                                 is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
  1236.                                 if(is_p != 7)
  1237.                                 {
  1238.                                         real t1,t2;
  1239.                                         sb  = bi->shortDiff[sfb];
  1240.                                         idx = bi->shortIdx[sfb] + lwin;
  1241.                                         t1  = tab1[is_p]; t2 = tab2[is_p];
  1242.                                         for (; sb > 0; sb--,idx+=3)
  1243.                                         {
  1244.                                                 real v = xr[0][idx];
  1245.                                                 xr[0][idx] = REAL_MUL_15(v, t1);
  1246.                                                 xr[1][idx] = REAL_MUL_15(v, t2);
  1247.                                         }
  1248.                                 }
  1249.                         }
  1250.  
  1251. #if 1
  1252. /* in the original: copy 10 to 11 , here: copy 11 to 12
  1253. maybe still wrong??? (copy 12 to 13?) */
  1254.                         is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
  1255.                         sb   = bi->shortDiff[12];
  1256.                         idx  = bi->shortIdx[12] + lwin;
  1257. #else
  1258.                         is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
  1259.                         sb   = bi->shortDiff[11];
  1260.                         idx  = bi->shortIdx[11] + lwin;
  1261. #endif
  1262.                         if(is_p != 7)
  1263.                         {
  1264.                                 real t1,t2;
  1265.                                 t1 = tab1[is_p]; t2 = tab2[is_p];
  1266.                                 for( ; sb > 0; sb--,idx+=3 )
  1267.                                 {  
  1268.                                         real v = xr[0][idx];
  1269.                                         xr[0][idx] = REAL_MUL_15(v, t1);
  1270.                                         xr[1][idx] = REAL_MUL_15(v, t2);
  1271.                                 }
  1272.                         }
  1273.                 } /* end for(lwin; .. ; . ) */
  1274.  
  1275.                 /* also check l-part, if ALL bands in the three windows are 'empty' and mode = mixed_mode */
  1276.                 if(do_l)
  1277.                 {
  1278.                         int sfb = gr_info->maxbandl;
  1279.                         int idx;
  1280.                         if(sfb > 21) return; /* similarity fix related to CVE-2006-1655 */
  1281.  
  1282.                         idx = bi->longIdx[sfb];
  1283.                         for( ; sfb<8; sfb++ )
  1284.                         {
  1285.                                 int sb = bi->longDiff[sfb];
  1286.                                 int is_p = scalefac[sfb]; /* scale: 0-15 */
  1287.                                 if(is_p != 7)
  1288.                                 {
  1289.                                         real t1,t2;
  1290.                                         t1 = tab1[is_p]; t2 = tab2[is_p];
  1291.                                         for( ; sb > 0; sb--,idx++)
  1292.                                         {
  1293.                                                 real v = xr[0][idx];
  1294.                                                 xr[0][idx] = REAL_MUL_15(v, t1);
  1295.                                                 xr[1][idx] = REAL_MUL_15(v, t2);
  1296.                                         }
  1297.                                 }
  1298.                                 else idx += sb;
  1299.                         }
  1300.                 }    
  1301.         }
  1302.         else
  1303.         { /* ((gr_info->block_type != 2)) */
  1304.                 int sfb = gr_info->maxbandl;
  1305.                 int is_p,idx;
  1306.                 if(sfb > 21) return; /* tightened fix for CVE-2006-1655 */
  1307.  
  1308.                 idx = bi->longIdx[sfb];
  1309.                 for ( ; sfb<21; sfb++)
  1310.                 {
  1311.                         int sb = bi->longDiff[sfb];
  1312.                         is_p = scalefac[sfb]; /* scale: 0-15 */
  1313.                         if(is_p != 7)
  1314.                         {
  1315.                                 real t1,t2;
  1316.                                 t1 = tab1[is_p]; t2 = tab2[is_p];
  1317.                                 for( ; sb > 0; sb--,idx++)
  1318.                                 {
  1319.                                          real v = xr[0][idx];
  1320.                                          xr[0][idx] = REAL_MUL_15(v, t1);
  1321.                                          xr[1][idx] = REAL_MUL_15(v, t2);
  1322.                                 }
  1323.                         }
  1324.                         else idx += sb;
  1325.                 }
  1326.  
  1327.                 is_p = scalefac[20];
  1328.                 if(is_p != 7)
  1329.                 {  /* copy l-band 20 to l-band 21 */
  1330.                         int sb;
  1331.                         real t1 = tab1[is_p],t2 = tab2[is_p];
  1332.  
  1333.                         for( sb = bi->longDiff[21]; sb > 0; sb--,idx++ )
  1334.                         {
  1335.                                 real v = xr[0][idx];
  1336.                                 xr[0][idx] = REAL_MUL_15(v, t1);
  1337.                                 xr[1][idx] = REAL_MUL_15(v, t2);
  1338.                         }
  1339.                 }
  1340.         }
  1341. }
  1342.  
  1343.  
  1344. static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info)
  1345. {
  1346.         int sblim;
  1347.  
  1348.         if(gr_info->block_type == 2)
  1349.         {
  1350.                         if(!gr_info->mixed_block_flag) return;
  1351.  
  1352.                         sblim = 1;
  1353.         }
  1354.         else sblim = gr_info->maxb-1;
  1355.  
  1356.         /* 31 alias-reduction operations between each pair of sub-bands */
  1357.         /* with 8 butterflies between each pair                         */
  1358.  
  1359.         {
  1360.                 int sb;
  1361.                 real *xr1=(real *) xr[1];
  1362.  
  1363.                 for(sb=sblim; sb; sb--,xr1+=10)
  1364.                 {
  1365.                         int ss;
  1366.                         real *cs=aa_cs,*ca=aa_ca;
  1367.                         real *xr2 = xr1;
  1368.  
  1369.                         for(ss=7;ss>=0;ss--)
  1370.                         { /* upper and lower butterfly inputs */
  1371.                                 register real bu = *--xr2,bd = *xr1;
  1372.                                 *xr2   = REAL_MUL(bu, *cs) - REAL_MUL(bd, *ca);
  1373.                                 *xr1++ = REAL_MUL(bd, *cs++) + REAL_MUL(bu, *ca++);
  1374.                         }
  1375.                 }
  1376.         }
  1377. }
  1378.  
  1379. /*
  1380.         This is an optimized DCT from Jeff Tsay's maplay 1.2+ package.
  1381.         Saved one multiplication by doing the 'twiddle factor' stuff
  1382.         together with the window mul. (MH)
  1383.  
  1384.         This uses Byeong Gi Lee's Fast Cosine Transform algorithm, but the
  1385.         9 point IDCT needs to be reduced further. Unfortunately, I don't
  1386.         know how to do that, because 9 is not an even number. - Jeff.
  1387.  
  1388.         Original Message:
  1389.  
  1390.         9 Point Inverse Discrete Cosine Transform
  1391.  
  1392.         This piece of code is Copyright 1997 Mikko Tommila and is freely usable
  1393.         by anybody. The algorithm itself is of course in the public domain.
  1394.  
  1395.         Again derived heuristically from the 9-point WFTA.
  1396.  
  1397.         The algorithm is optimized (?) for speed, not for small rounding errors or
  1398.         good readability.
  1399.  
  1400.         36 additions, 11 multiplications
  1401.  
  1402.         Again this is very likely sub-optimal.
  1403.  
  1404.         The code is optimized to use a minimum number of temporary variables,
  1405.         so it should compile quite well even on 8-register Intel x86 processors.
  1406.         This makes the code quite obfuscated and very difficult to understand.
  1407.  
  1408.         References:
  1409.         [1] S. Winograd: "On Computing the Discrete Fourier Transform",
  1410.             Mathematics of Computation, Volume 32, Number 141, January 1978,
  1411.             Pages 175-199
  1412. */
  1413.  
  1414. /* Calculation of the inverse MDCT
  1415.    used to be static without 3dnow - does that really matter? */
  1416. void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf)
  1417. {
  1418. #ifdef NEW_DCT9
  1419.         real tmp[18];
  1420. #endif
  1421.  
  1422.         {
  1423.                 register real *in = inbuf;
  1424.  
  1425.                 in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14];
  1426.                 in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11];
  1427.                 in[11]+=in[10]; in[10]+=in[9];  in[9] +=in[8];
  1428.                 in[8] +=in[7];  in[7] +=in[6];  in[6] +=in[5];
  1429.                 in[5] +=in[4];  in[4] +=in[3];  in[3] +=in[2];
  1430.                 in[2] +=in[1];  in[1] +=in[0];
  1431.  
  1432.                 in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9];
  1433.                 in[9] +=in[7];  in[7] +=in[5];  in[5] +=in[3];  in[3] +=in[1];
  1434.  
  1435.  
  1436. #ifdef NEW_DCT9
  1437. #if 1
  1438.                 {
  1439.                         real t3;
  1440.                         {
  1441.                                 real t0, t1, t2;
  1442.  
  1443.                                 t0 = REAL_MUL(COS6_2, (in[8] + in[16] - in[4]));
  1444.                                 t1 = REAL_MUL(COS6_2, in[12]);
  1445.  
  1446.                                 t3 = in[0];
  1447.                                 t2 = t3 - t1 - t1;
  1448.                                 tmp[1] = tmp[7] = t2 - t0;
  1449.                                 tmp[4]          = t2 + t0 + t0;
  1450.                                 t3 += t1;
  1451.  
  1452.                                 t2 = REAL_MUL(COS6_1, (in[10] + in[14] - in[2]));
  1453.                                 tmp[1] -= t2;
  1454.                                 tmp[7] += t2;
  1455.                         }
  1456.                         {
  1457.                                 real t0, t1, t2;
  1458.  
  1459.                                 t0 = REAL_MUL(cos9[0], (in[4] + in[8] ));
  1460.                                 t1 = REAL_MUL(cos9[1], (in[8] - in[16]));
  1461.                                 t2 = REAL_MUL(cos9[2], (in[4] + in[16]));
  1462.  
  1463.                                 tmp[2] = tmp[6] = t3 - t0      - t2;
  1464.                                 tmp[0] = tmp[8] = t3 + t0 + t1;
  1465.                                 tmp[3] = tmp[5] = t3      - t1 + t2;
  1466.                         }
  1467.                 }
  1468.                 {
  1469.                         real t1, t2, t3;
  1470.  
  1471.                         t1 = REAL_MUL(cos18[0], (in[2]  + in[10]));
  1472.                         t2 = REAL_MUL(cos18[1], (in[10] - in[14]));
  1473.                         t3 = REAL_MUL(COS6_1,    in[6]);
  1474.  
  1475.                         {
  1476.                                 real t0 = t1 + t2 + t3;
  1477.                                 tmp[0] += t0;
  1478.                                 tmp[8] -= t0;
  1479.                         }
  1480.  
  1481.                         t2 -= t3;
  1482.                         t1 -= t3;
  1483.  
  1484.                         t3 = REAL_MUL(cos18[2], (in[2] + in[14]));
  1485.  
  1486.                         t1 += t3;
  1487.                         tmp[3] += t1;
  1488.                         tmp[5] -= t1;
  1489.  
  1490.                         t2 -= t3;
  1491.                         tmp[2] += t2;
  1492.                         tmp[6] -= t2;
  1493.                 }
  1494.  
  1495. #else
  1496.                 {
  1497.                         real t0, t1, t2, t3, t4, t5, t6, t7;
  1498.  
  1499.                         t1 = REAL_MUL(COS6_2, in[12]);
  1500.                         t2 = REAL_MUL(COS6_2, (in[8] + in[16] - in[4]));
  1501.  
  1502.                         t3 = in[0] + t1;
  1503.                         t4 = in[0] - t1 - t1;
  1504.                         t5     = t4 - t2;
  1505.                         tmp[4] = t4 + t2 + t2;
  1506.  
  1507.                         t0 = REAL_MUL(cos9[0], (in[4] + in[8]));
  1508.                         t1 = REAL_MUL(cos9[1], (in[8] - in[16]));
  1509.  
  1510.                         t2 = REAL_MUL(cos9[2], (in[4] + in[16]));
  1511.  
  1512.                         t6 = t3 - t0 - t2;
  1513.                         t0 += t3 + t1;
  1514.                         t3 += t2 - t1;
  1515.  
  1516.                         t2 = REAL_MUL(cos18[0], (in[2]  + in[10]));
  1517.                         t4 = REAL_MUL(cos18[1], (in[10] - in[14]));
  1518.                         t7 = REAL_MUL(COS6_1, in[6]);
  1519.  
  1520.                         t1 = t2 + t4 + t7;
  1521.                         tmp[0] = t0 + t1;
  1522.                         tmp[8] = t0 - t1;
  1523.                         t1 = REAL_MUL(cos18[2], (in[2] + in[14]));
  1524.                         t2 += t1 - t7;
  1525.  
  1526.                         tmp[3] = t3 + t2;
  1527.                         t0 = REAL_MUL(COS6_1, (in[10] + in[14] - in[2]));
  1528.                         tmp[5] = t3 - t2;
  1529.  
  1530.                         t4 -= t1 + t7;
  1531.  
  1532.                         tmp[1] = t5 - t0;
  1533.                         tmp[7] = t5 + t0;
  1534.                         tmp[2] = t6 + t4;
  1535.                         tmp[6] = t6 - t4;
  1536.                 }
  1537. #endif
  1538.  
  1539.                 {
  1540.                         real t0, t1, t2, t3, t4, t5, t6, t7;
  1541.  
  1542.                         t1 = REAL_MUL(COS6_2, in[13]);
  1543.                         t2 = REAL_MUL(COS6_2, (in[9] + in[17] - in[5]));
  1544.  
  1545.                         t3 = in[1] + t1;
  1546.                         t4 = in[1] - t1 - t1;
  1547.                         t5 = t4 - t2;
  1548.  
  1549.                         t0 = REAL_MUL(cos9[0], (in[5] + in[9]));
  1550.                         t1 = REAL_MUL(cos9[1], (in[9] - in[17]));
  1551.  
  1552.                         tmp[13] = REAL_MUL((t4 + t2 + t2), tfcos36[17-13]);
  1553.                         t2 = REAL_MUL(cos9[2], (in[5] + in[17]));
  1554.  
  1555.                         t6 = t3 - t0 - t2;
  1556.                         t0 += t3 + t1;
  1557.                         t3 += t2 - t1;
  1558.  
  1559.                         t2 = REAL_MUL(cos18[0], (in[3]  + in[11]));
  1560.                         t4 = REAL_MUL(cos18[1], (in[11] - in[15]));
  1561.                         t7 = REAL_MUL(COS6_1, in[7]);
  1562.  
  1563.                         t1 = t2 + t4 + t7;
  1564.                         tmp[17] = REAL_MUL((t0 + t1), tfcos36[17-17]);
  1565.                         tmp[9]  = REAL_MUL((t0 - t1), tfcos36[17-9]);
  1566.                         t1 = REAL_MUL(cos18[2], (in[3] + in[15]));
  1567.                         t2 += t1 - t7;
  1568.  
  1569.                         tmp[14] = REAL_MUL((t3 + t2), tfcos36[17-14]);
  1570.                         t0 = REAL_MUL(COS6_1, (in[11] + in[15] - in[3]));
  1571.                         tmp[12] = REAL_MUL((t3 - t2), tfcos36[17-12]);
  1572.  
  1573.                         t4 -= t1 + t7;
  1574.  
  1575.                         tmp[16] = REAL_MUL((t5 - t0), tfcos36[17-16]);
  1576.                         tmp[10] = REAL_MUL((t5 + t0), tfcos36[17-10]);
  1577.                         tmp[15] = REAL_MUL((t6 + t4), tfcos36[17-15]);
  1578.                         tmp[11] = REAL_MUL((t6 - t4), tfcos36[17-11]);
  1579.                 }
  1580.  
  1581. #define MACRO(v) { \
  1582.                 real tmpval; \
  1583.                 tmpval = tmp[(v)] + tmp[17-(v)]; \
  1584.                 out2[9+(v)] = REAL_MUL(tmpval, w[27+(v)]); \
  1585.                 out2[8-(v)] = REAL_MUL(tmpval, w[26-(v)]); \
  1586.                 tmpval = tmp[(v)] - tmp[17-(v)]; \
  1587.                 ts[SBLIMIT*(8-(v))] = out1[8-(v)] + REAL_MUL(tmpval, w[8-(v)]); \
  1588.                 ts[SBLIMIT*(9+(v))] = out1[9+(v)] + REAL_MUL(tmpval, w[9+(v)]); }
  1589.  
  1590.                 {
  1591.                         register real *out2 = o2;
  1592.                         register real *w = wintab;
  1593.                         register real *out1 = o1;
  1594.                         register real *ts = tsbuf;
  1595.  
  1596.                         MACRO(0);
  1597.                         MACRO(1);
  1598.                         MACRO(2);
  1599.                         MACRO(3);
  1600.                         MACRO(4);
  1601.                         MACRO(5);
  1602.                         MACRO(6);
  1603.                         MACRO(7);
  1604.                         MACRO(8);
  1605.                 }
  1606.  
  1607. #else
  1608.  
  1609.                 {
  1610.  
  1611. #define MACRO0(v) { \
  1612.         real tmp; \
  1613.         out2[9+(v)] = REAL_MUL((tmp = sum0 + sum1), w[27+(v)]); \
  1614.         out2[8-(v)] = REAL_MUL(tmp, w[26-(v)]);   } \
  1615.         sum0 -= sum1; \
  1616.         ts[SBLIMIT*(8-(v))] = out1[8-(v)] + REAL_MUL(sum0, w[8-(v)]); \
  1617.         ts[SBLIMIT*(9+(v))] = out1[9+(v)] + REAL_MUL(sum0, w[9+(v)]);
  1618. #define MACRO1(v) { \
  1619.         real sum0,sum1; \
  1620.         sum0 = tmp1a + tmp2a; \
  1621.         sum1 = REAL_MUL((tmp1b + tmp2b), tfcos36[(v)]); \
  1622.         MACRO0(v); }
  1623. #define MACRO2(v) { \
  1624.         real sum0,sum1; \
  1625.         sum0 = tmp2a - tmp1a; \
  1626.         sum1 = REAL_MUL((tmp2b - tmp1b), tfcos36[(v)]); \
  1627.         MACRO0(v); }
  1628.  
  1629.                         register const real *c = COS9;
  1630.                         register real *out2 = o2;
  1631.                         register real *w = wintab;
  1632.                         register real *out1 = o1;
  1633.                         register real *ts = tsbuf;
  1634.  
  1635.                         real ta33,ta66,tb33,tb66;
  1636.  
  1637.                         ta33 = REAL_MUL(in[2*3+0], c[3]);
  1638.                         ta66 = REAL_MUL(in[2*6+0], c[6]);
  1639.                         tb33 = REAL_MUL(in[2*3+1], c[3]);
  1640.                         tb66 = REAL_MUL(in[2*6+1], c[6]);
  1641.  
  1642.                         {
  1643.                                 real tmp1a,tmp2a,tmp1b,tmp2b;
  1644.                                 tmp1a = REAL_MUL(in[2*1+0], c[1]) + ta33 + REAL_MUL(in[2*5+0], c[5]) + REAL_MUL(in[2*7+0], c[7]);
  1645.                                 tmp1b = REAL_MUL(in[2*1+1], c[1]) + tb33 + REAL_MUL(in[2*5+1], c[5]) + REAL_MUL(in[2*7+1], c[7]);
  1646.                                 tmp2a = REAL_MUL(in[2*2+0], c[2]) + REAL_MUL(in[2*4+0], c[4]) + ta66 + REAL_MUL(in[2*8+0], c[8]);
  1647.                                 tmp2b = REAL_MUL(in[2*2+1], c[2]) + REAL_MUL(in[2*4+1], c[4]) + tb66 + REAL_MUL(in[2*8+1], c[8]);
  1648.  
  1649.                                 MACRO1(0);
  1650.                                 MACRO2(8);
  1651.                         }
  1652.  
  1653.                         {
  1654.                                 real tmp1a,tmp2a,tmp1b,tmp2b;
  1655.                                 tmp1a = REAL_MUL(( in[2*1+0] - in[2*5+0] - in[2*7+0] ), c[3]);
  1656.                                 tmp1b = REAL_MUL(( in[2*1+1] - in[2*5+1] - in[2*7+1] ), c[3]);
  1657.                                 tmp2a = REAL_MUL(( in[2*2+0] - in[2*4+0] - in[2*8+0] ), c[6]) - in[2*6+0] + in[2*0+0];
  1658.                                 tmp2b = REAL_MUL(( in[2*2+1] - in[2*4+1] - in[2*8+1] ), c[6]) - in[2*6+1] + in[2*0+1];
  1659.  
  1660.                                 MACRO1(1);
  1661.                                 MACRO2(7);
  1662.                         }
  1663.  
  1664.                         {
  1665.                                 real tmp1a,tmp2a,tmp1b,tmp2b;
  1666.                                 tmp1a =   REAL_MUL(in[2*1+0], c[5]) - ta33 - REAL_MUL(in[2*5+0], c[7]) + REAL_MUL(in[2*7+0], c[1]);
  1667.                                 tmp1b =   REAL_MUL(in[2*1+1], c[5]) - tb33 - REAL_MUL(in[2*5+1], c[7]) + REAL_MUL(in[2*7+1], c[1]);
  1668.                                 tmp2a = - REAL_MUL(in[2*2+0], c[8]) - REAL_MUL(in[2*4+0], c[2]) + ta66 + REAL_MUL(in[2*8+0], c[4]);
  1669.                                 tmp2b = - REAL_MUL(in[2*2+1], c[8]) - REAL_MUL(in[2*4+1], c[2]) + tb66 + REAL_MUL(in[2*8+1], c[4]);
  1670.  
  1671.                                 MACRO1(2);
  1672.                                 MACRO2(6);
  1673.                         }
  1674.  
  1675.                         {
  1676.                                 real tmp1a,tmp2a,tmp1b,tmp2b;
  1677.                                 tmp1a =   REAL_MUL(in[2*1+0], c[7]) - ta33 + REAL_MUL(in[2*5+0], c[1]) - REAL_MUL(in[2*7+0], c[5]);
  1678.                                 tmp1b =   REAL_MUL(in[2*1+1], c[7]) - tb33 + REAL_MUL(in[2*5+1], c[1]) - REAL_MUL(in[2*7+1], c[5]);
  1679.                                 tmp2a = - REAL_MUL(in[2*2+0], c[4]) + REAL_MUL(in[2*4+0], c[8]) + ta66 - REAL_MUL(in[2*8+0], c[2]);
  1680.                                 tmp2b = - REAL_MUL(in[2*2+1], c[4]) + REAL_MUL(in[2*4+1], c[8]) + tb66 - REAL_MUL(in[2*8+1], c[2]);
  1681.  
  1682.                                 MACRO1(3);
  1683.                                 MACRO2(5);
  1684.                         }
  1685.  
  1686.                         {
  1687.                                 real sum0,sum1;
  1688.                                 sum0 =  in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0];
  1689.                                 sum1 = REAL_MUL((in[2*0+1] - in[2*2+1] + in[2*4+1] - in[2*6+1] + in[2*8+1] ), tfcos36[4]);
  1690.                                 MACRO0(4);
  1691.                         }
  1692.                 }
  1693. #endif
  1694.  
  1695.         }
  1696. }
  1697.  
  1698.  
  1699. /* new DCT12 */
  1700. static void dct12(real *in,real *rawout1,real *rawout2,register real *wi,register real *ts)
  1701. {
  1702. #define DCT12_PART1 \
  1703.         in5 = in[5*3];  \
  1704.         in5 += (in4 = in[4*3]); \
  1705.         in4 += (in3 = in[3*3]); \
  1706.         in3 += (in2 = in[2*3]); \
  1707.         in2 += (in1 = in[1*3]); \
  1708.         in1 += (in0 = in[0*3]); \
  1709.         \
  1710.         in5 += in3; in3 += in1; \
  1711.         \
  1712.         in2 = REAL_MUL(in2, COS6_1); \
  1713.         in3 = REAL_MUL(in3, COS6_1);
  1714.  
  1715. #define DCT12_PART2 \
  1716.         in0 += REAL_MUL(in4, COS6_2); \
  1717.         \
  1718.         in4 = in0 + in2; \
  1719.         in0 -= in2;      \
  1720.         \
  1721.         in1 += REAL_MUL(in5, COS6_2); \
  1722.         \
  1723.         in5 = REAL_MUL((in1 + in3), tfcos12[0]); \
  1724.         in1 = REAL_MUL((in1 - in3), tfcos12[2]); \
  1725.         \
  1726.         in3 = in4 + in5; \
  1727.         in4 -= in5;      \
  1728.         \
  1729.         in2 = in0 + in1; \
  1730.         in0 -= in1;
  1731.  
  1732.         {
  1733.                 real in0,in1,in2,in3,in4,in5;
  1734.                 register real *out1 = rawout1;
  1735.                 ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2];
  1736.                 ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5];
  1737.  
  1738.                 DCT12_PART1
  1739.  
  1740.                 {
  1741.                         real tmp0,tmp1 = (in0 - in4);
  1742.                         {
  1743.                                 real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]);
  1744.                                 tmp0 = tmp1 + tmp2;
  1745.                                 tmp1 -= tmp2;
  1746.                         }
  1747.                         ts[(17-1)*SBLIMIT] = out1[17-1] + REAL_MUL(tmp0, wi[11-1]);
  1748.                         ts[(12+1)*SBLIMIT] = out1[12+1] + REAL_MUL(tmp0, wi[6+1]);
  1749.                         ts[(6 +1)*SBLIMIT] = out1[6 +1] + REAL_MUL(tmp1, wi[1]);
  1750.                         ts[(11-1)*SBLIMIT] = out1[11-1] + REAL_MUL(tmp1, wi[5-1]);
  1751.                 }
  1752.  
  1753.                 DCT12_PART2
  1754.  
  1755.                 ts[(17-0)*SBLIMIT] = out1[17-0] + REAL_MUL(in2, wi[11-0]);
  1756.                 ts[(12+0)*SBLIMIT] = out1[12+0] + REAL_MUL(in2, wi[6+0]);
  1757.                 ts[(12+2)*SBLIMIT] = out1[12+2] + REAL_MUL(in3, wi[6+2]);
  1758.                 ts[(17-2)*SBLIMIT] = out1[17-2] + REAL_MUL(in3, wi[11-2]);
  1759.  
  1760.                 ts[(6 +0)*SBLIMIT]  = out1[6+0] + REAL_MUL(in0, wi[0]);
  1761.                 ts[(11-0)*SBLIMIT] = out1[11-0] + REAL_MUL(in0, wi[5-0]);
  1762.                 ts[(6 +2)*SBLIMIT]  = out1[6+2] + REAL_MUL(in4, wi[2]);
  1763.                 ts[(11-2)*SBLIMIT] = out1[11-2] + REAL_MUL(in4, wi[5-2]);
  1764.         }
  1765.  
  1766.         in++;
  1767.  
  1768.         {
  1769.                 real in0,in1,in2,in3,in4,in5;
  1770.                 register real *out2 = rawout2;
  1771.  
  1772.                 DCT12_PART1
  1773.  
  1774.                 {
  1775.                         real tmp0,tmp1 = (in0 - in4);
  1776.                         {
  1777.                                 real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]);
  1778.                                 tmp0 = tmp1 + tmp2;
  1779.                                 tmp1 -= tmp2;
  1780.                         }
  1781.                         out2[5-1] = REAL_MUL(tmp0, wi[11-1]);
  1782.                         out2[0+1] = REAL_MUL(tmp0, wi[6+1]);
  1783.                         ts[(12+1)*SBLIMIT] += REAL_MUL(tmp1, wi[1]);
  1784.                         ts[(17-1)*SBLIMIT] += REAL_MUL(tmp1, wi[5-1]);
  1785.                 }
  1786.  
  1787.                 DCT12_PART2
  1788.  
  1789.                 out2[5-0] = REAL_MUL(in2, wi[11-0]);
  1790.                 out2[0+0] = REAL_MUL(in2, wi[6+0]);
  1791.                 out2[0+2] = REAL_MUL(in3, wi[6+2]);
  1792.                 out2[5-2] = REAL_MUL(in3, wi[11-2]);
  1793.  
  1794.                 ts[(12+0)*SBLIMIT] += REAL_MUL(in0, wi[0]);
  1795.                 ts[(17-0)*SBLIMIT] += REAL_MUL(in0, wi[5-0]);
  1796.                 ts[(12+2)*SBLIMIT] += REAL_MUL(in4, wi[2]);
  1797.                 ts[(17-2)*SBLIMIT] += REAL_MUL(in4, wi[5-2]);
  1798.         }
  1799.  
  1800.         in++;
  1801.  
  1802.         {
  1803.                 real in0,in1,in2,in3,in4,in5;
  1804.                 register real *out2 = rawout2;
  1805.                 out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0;
  1806.  
  1807.                 DCT12_PART1
  1808.  
  1809.                 {
  1810.                         real tmp0,tmp1 = (in0 - in4);
  1811.                         {
  1812.                                 real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]);
  1813.                                 tmp0 = tmp1 + tmp2;
  1814.                                 tmp1 -= tmp2;
  1815.                         }
  1816.                         out2[11-1] = REAL_MUL(tmp0, wi[11-1]);
  1817.                         out2[6 +1] = REAL_MUL(tmp0, wi[6+1]);
  1818.                         out2[0+1] += REAL_MUL(tmp1, wi[1]);
  1819.                         out2[5-1] += REAL_MUL(tmp1, wi[5-1]);
  1820.                 }
  1821.  
  1822.                 DCT12_PART2
  1823.  
  1824.                 out2[11-0] = REAL_MUL(in2, wi[11-0]);
  1825.                 out2[6 +0] = REAL_MUL(in2, wi[6+0]);
  1826.                 out2[6 +2] = REAL_MUL(in3, wi[6+2]);
  1827.                 out2[11-2] = REAL_MUL(in3, wi[11-2]);
  1828.  
  1829.                 out2[0+0] += REAL_MUL(in0, wi[0]);
  1830.                 out2[5-0] += REAL_MUL(in0, wi[5-0]);
  1831.                 out2[0+2] += REAL_MUL(in4, wi[2]);
  1832.                 out2[5-2] += REAL_MUL(in4, wi[5-2]);
  1833.         }
  1834. }
  1835.  
  1836.  
  1837. static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT], real tsOut[SSLIMIT][SBLIMIT], int ch,struct gr_info_s *gr_info, mpg123_handle *fr)
  1838. {
  1839.         real (*block)[2][SBLIMIT*SSLIMIT] = fr->hybrid_block;
  1840.         int *blc = fr->hybrid_blc;
  1841.  
  1842.         real *tspnt = (real *) tsOut;
  1843.         real *rawout1,*rawout2;
  1844.         int bt = 0;
  1845.         size_t sb = 0;
  1846.  
  1847.         {
  1848.                 int b = blc[ch];
  1849.                 rawout1=block[b][ch];
  1850.                 b=-b+1;
  1851.                 rawout2=block[b][ch];
  1852.                 blc[ch] = b;
  1853.         }
  1854.  
  1855.         if(gr_info->mixed_block_flag)
  1856.         {
  1857.                 sb = 2;
  1858.                 opt_dct36(fr)(fsIn[0],rawout1,rawout2,win[0],tspnt);
  1859.                 opt_dct36(fr)(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1);
  1860.                 rawout1 += 36; rawout2 += 36; tspnt += 2;
  1861.         }
  1862.  
  1863.         bt = gr_info->block_type;
  1864.         if(bt == 2)
  1865.         {
  1866.                 for(; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36)
  1867.                 {
  1868.                         dct12(fsIn[sb]  ,rawout1   ,rawout2   ,win[2] ,tspnt);
  1869.                         dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1);
  1870.                 }
  1871.         }
  1872.         else
  1873.         {
  1874.                 for(; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36)
  1875.                 {
  1876.                         opt_dct36(fr)(fsIn[sb],rawout1,rawout2,win[bt],tspnt);
  1877.                         opt_dct36(fr)(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1);
  1878.                 }
  1879.         }
  1880.  
  1881.         for(;sb<SBLIMIT;sb++,tspnt++)
  1882.         {
  1883.                 int i;
  1884.                 for(i=0;i<SSLIMIT;i++)
  1885.                 {
  1886.                         tspnt[i*SBLIMIT] = *rawout1++;
  1887.                         *rawout2++ = DOUBLE_TO_REAL(0.0);
  1888.                 }
  1889.         }
  1890. }
  1891.  
  1892.  
  1893. /* And at the end... the main layer3 handler */
  1894. int do_layer3(mpg123_handle *fr)
  1895. {
  1896.         int gr, ch, ss,clip=0;
  1897.         int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
  1898.         struct III_sideinfo sideinfo;
  1899.         int stereo = fr->stereo;
  1900.         int single = fr->single;
  1901.         int ms_stereo,i_stereo;
  1902.         int sfreq = fr->sampling_frequency;
  1903.         int stereo1,granules;
  1904.  
  1905.         if(stereo == 1)
  1906.         { /* stream is mono */
  1907.                 stereo1 = 1;
  1908.                 single = SINGLE_LEFT;
  1909.         }
  1910.         else if(single != SINGLE_STEREO) /* stream is stereo, but force to mono */
  1911.         stereo1 = 1;
  1912.         else
  1913.         stereo1 = 2;
  1914.  
  1915.         if(fr->mode == MPG_MD_JOINT_STEREO)
  1916.         {
  1917.                 ms_stereo = (fr->mode_ext & 0x2)>>1;
  1918.                 i_stereo  = fr->mode_ext & 0x1;
  1919.         }
  1920.         else ms_stereo = i_stereo = 0;
  1921.  
  1922.         granules = fr->lsf ? 1 : 2;
  1923.  
  1924.         /* quick hack to keep the music playing */
  1925.         /* after having seen this nasty test file... */
  1926.         if(III_get_side_info(fr, &sideinfo,stereo,ms_stereo,sfreq,single))
  1927.         {
  1928.                 if(NOQUIET) error("bad frame - unable to get valid sideinfo");
  1929.                 return clip;
  1930.         }
  1931.  
  1932.         set_pointer(fr,sideinfo.main_data_begin);
  1933.  
  1934.         for(gr=0;gr<granules;gr++)
  1935.         {
  1936.                 ALIGNED(16) real hybridIn[2][SBLIMIT][SSLIMIT];
  1937.                 ALIGNED(16) real hybridOut[2][SSLIMIT][SBLIMIT];
  1938.  
  1939.                 {
  1940.                         struct gr_info_s *gr_info = &(sideinfo.ch[0].gr[gr]);
  1941.                         long part2bits;
  1942.                         if(fr->lsf)
  1943.                         part2bits = III_get_scale_factors_2(fr, scalefacs[0],gr_info,0);
  1944.                         else
  1945.                         part2bits = III_get_scale_factors_1(fr, scalefacs[0],gr_info,0,gr);
  1946.  
  1947.                         if(III_dequantize_sample(fr, hybridIn[0], scalefacs[0],gr_info,sfreq,part2bits))
  1948.                         {
  1949.                                 if(VERBOSE2) error("dequantization failed!");
  1950.                                 return clip;
  1951.                         }
  1952.                 }
  1953.  
  1954.                 if(stereo == 2)
  1955.                 {
  1956.                         struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]);
  1957.                         long part2bits;
  1958.                         if(fr->lsf)
  1959.                         part2bits = III_get_scale_factors_2(fr, scalefacs[1],gr_info,i_stereo);
  1960.                         else
  1961.                         part2bits = III_get_scale_factors_1(fr, scalefacs[1],gr_info,1,gr);
  1962.  
  1963.                         if(III_dequantize_sample(fr, hybridIn[1],scalefacs[1],gr_info,sfreq,part2bits))
  1964.                         {
  1965.                                 if(VERBOSE2) error("dequantization failed!");
  1966.                                 return clip;
  1967.                         }
  1968.  
  1969.                         if(ms_stereo)
  1970.                         {
  1971.                                 int i;
  1972.                                 unsigned int maxb = sideinfo.ch[0].gr[gr].maxb;
  1973.                                 if(sideinfo.ch[1].gr[gr].maxb > maxb) maxb = sideinfo.ch[1].gr[gr].maxb;
  1974.  
  1975.                                 for(i=0;i<SSLIMIT*(int)maxb;i++)
  1976.                                 {
  1977.                                         real tmp0 = ((real *)hybridIn[0])[i];
  1978.                                         real tmp1 = ((real *)hybridIn[1])[i];
  1979.                                         ((real *)hybridIn[0])[i] = tmp0 + tmp1;
  1980.                                         ((real *)hybridIn[1])[i] = tmp0 - tmp1;
  1981.                                 }
  1982.                         }
  1983.  
  1984.                         if(i_stereo) III_i_stereo(hybridIn,scalefacs[1],gr_info,sfreq,ms_stereo,fr->lsf);
  1985.  
  1986.                         if(ms_stereo || i_stereo || (single == SINGLE_MIX) )
  1987.                         {
  1988.                                 if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb)
  1989.                                 sideinfo.ch[0].gr[gr].maxb = gr_info->maxb;
  1990.                                 else
  1991.                                 gr_info->maxb = sideinfo.ch[0].gr[gr].maxb;
  1992.                         }
  1993.  
  1994.                         switch(single)
  1995.                         {
  1996.                                 case SINGLE_MIX:
  1997.                                 {
  1998.                                         register int i;
  1999.                                         register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
  2000.                                         for(i=0;i<SSLIMIT*(int)gr_info->maxb;i++,in0++)
  2001.                                         *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */
  2002.                                 }
  2003.                                 break;
  2004.                                 case SINGLE_RIGHT:
  2005.                                 {
  2006.                                         register int i;
  2007.                                         register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
  2008.                                         for(i=0;i<SSLIMIT*(int)gr_info->maxb;i++)
  2009.                                         *in0++ = *in1++;
  2010.                                 }
  2011.                                 break;
  2012.                         }
  2013.                 }
  2014.  
  2015.                 for(ch=0;ch<stereo1;ch++)
  2016.                 {
  2017.                         struct gr_info_s *gr_info = &(sideinfo.ch[ch].gr[gr]);
  2018.                         III_antialias(hybridIn[ch],gr_info);
  2019.                         III_hybrid(hybridIn[ch], hybridOut[ch], ch,gr_info, fr);
  2020.                 }
  2021.  
  2022. #ifdef OPT_I486
  2023.                 if(single != SINGLE_STEREO || fr->af.encoding != MPG123_ENC_SIGNED_16 || fr->down_sample != 0)
  2024.                 {
  2025. #endif
  2026.                 for(ss=0;ss<SSLIMIT;ss++)
  2027.                 {
  2028.                         if(single != SINGLE_STEREO)
  2029.                         clip += (fr->synth_mono)(hybridOut[0][ss], fr);
  2030.                         else
  2031.                         clip += (fr->synth_stereo)(hybridOut[0][ss], hybridOut[1][ss], fr);
  2032.  
  2033.                 }
  2034. #ifdef OPT_I486
  2035.                 } else
  2036.                 {
  2037.                         /* Only stereo, 16 bits benefit from the 486 optimization. */
  2038.                         ss=0;
  2039.                         while(ss < SSLIMIT)
  2040.                         {
  2041.                                 int n;
  2042.                                 n=(fr->buffer.size - fr->buffer.fill) / (2*2*32);
  2043.                                 if(n > (SSLIMIT-ss)) n=SSLIMIT-ss;
  2044.  
  2045.                                 /* Clip counting makes no sense with this function. */
  2046.                                 absynth_1to1_i486(hybridOut[0][ss], 0, fr, n);
  2047.                                 absynth_1to1_i486(hybridOut[1][ss], 1, fr, n);
  2048.                                 ss+=n;
  2049.                                 fr->buffer.fill+=(2*2*32)*n;
  2050.                         }
  2051.                 }
  2052. #endif
  2053.         }
  2054.  
  2055.         return clip;
  2056. }
  2057.