Subversion Repositories Kolibri OS

Rev

Rev 1905 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.         layer3.c: the layer 3 decoder
  3.  
  4.         copyright 1995-2009 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.         /* Making those two signed int as workaround for open64/pathscale/sun compilers, and also for consistency, since they're worked on together with other signed variables. */
  60.         int maxband[3];
  61.         int 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.         unsigned short longIdx[23];
  83.         unsigned char longDiff[22];
  84.         unsigned short shortIdx[14];
  85.         unsigned char shortDiff[13];
  86. };
  87.  
  88. /* Techy details about our friendly MPEG data. Fairly constant over the years;-) */
  89. static 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 unsigned char *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(!fr->to_ignore && 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.                         if(NOQUIET) 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.                                 if(NOQUIET) 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.                                         /* r0c+1+r1c+1 == 22, always. */
  498.                                         r1c = 20 - r0c;
  499.                                         gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
  500.                                         gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
  501.                                 }
  502.                                 else
  503.                                 {
  504.                                         gr_info->region1start = 54>>1;
  505.                                         gr_info->region2start = 576>>1;
  506.                                 }
  507.                         }
  508.                 }
  509.                 else
  510.                 {
  511.                         int i,r0c,r1c;
  512.                         for (i=0; i<3; i++)
  513.                         gr_info->table_select[i] = getbits_fast(fr, 5);
  514.  
  515.                         r0c = getbits_fast(fr, 4); /* 0 .. 15 */
  516.                         r1c = getbits_fast(fr, 3); /* 0 .. 7 */
  517.                         gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
  518.  
  519.                         /* max(r0c+r1c+2) = 15+7+2 = 24 */
  520.                         if(r0c+1+r1c+1 > 22) gr_info->region2start = 576>>1;
  521.                         else gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
  522.  
  523.                         gr_info->block_type = 0;
  524.                         gr_info->mixed_block_flag = 0;
  525.                 }
  526.                 if(!fr->lsf) gr_info->preflag = get1bit(fr);
  527.  
  528.                 gr_info->scalefac_scale = get1bit(fr);
  529.                 gr_info->count1table_select = get1bit(fr);
  530.         }
  531.         return 0;
  532. }
  533.  
  534.  
  535. /* read scalefactors */
  536. static int III_get_scale_factors_1(mpg123_handle *fr, int *scf,struct gr_info_s *gr_info,int ch,int gr)
  537. {
  538.         const unsigned char slen[2][16] =
  539.         {
  540.                 {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
  541.                 {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
  542.         };
  543.         int numbits;
  544.         int num0 = slen[0][gr_info->scalefac_compress];
  545.         int num1 = slen[1][gr_info->scalefac_compress];
  546.  
  547.         if(gr_info->block_type == 2)
  548.         {
  549.                 int i=18;
  550.                 numbits = (num0 + num1) * 18;
  551.  
  552.                 if(gr_info->mixed_block_flag)
  553.                 {
  554.                         for (i=8;i;i--)
  555.                         *scf++ = getbits_fast(fr, num0);
  556.  
  557.                         i = 9;
  558.                         numbits -= num0; /* num0 * 17 + num1 * 18 */
  559.                 }
  560.  
  561.                 for(;i;i--) *scf++ = getbits_fast(fr, num0);
  562.  
  563.                 for(i = 18; i; i--) *scf++ = getbits_fast(fr, num1);
  564.  
  565.                 *scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */
  566.         }
  567.         else
  568.         {
  569.                 int i;
  570.                 int scfsi = gr_info->scfsi;
  571.  
  572.                 if(scfsi < 0)
  573.                 { /* scfsi < 0 => granule == 0 */
  574.                         for(i=11;i;i--) *scf++ = getbits_fast(fr, num0);
  575.  
  576.                         for(i=10;i;i--) *scf++ = getbits_fast(fr, num1);
  577.  
  578.                         numbits = (num0 + num1) * 10 + num0;
  579.                         *scf++ = 0;
  580.                 }
  581.                 else
  582.                 {
  583.                         numbits = 0;
  584.                         if(!(scfsi & 0x8))
  585.                         {
  586.                                 for (i=0;i<6;i++) *scf++ = getbits_fast(fr, num0);
  587.  
  588.                                 numbits += num0 * 6;
  589.                         }
  590.                         else scf += 6;
  591.  
  592.                         if(!(scfsi & 0x4))
  593.                         {
  594.                                 for (i=0;i<5;i++) *scf++ = getbits_fast(fr, num0);
  595.  
  596.                                 numbits += num0 * 5;
  597.                         }
  598.                         else scf += 5;
  599.  
  600.                         if(!(scfsi & 0x2))
  601.                         {
  602.                                 for(i=0;i<5;i++) *scf++ = getbits_fast(fr, num1);
  603.  
  604.                                 numbits += num1 * 5;
  605.                         }
  606.                         else scf += 5;
  607.  
  608.                         if(!(scfsi & 0x1))
  609.                         {
  610.                                 for (i=0;i<5;i++) *scf++ = getbits_fast(fr, num1);
  611.  
  612.                                 numbits += num1 * 5;
  613.                         }
  614.                         else scf += 5;
  615.  
  616.                         *scf++ = 0;  /* no l[21] in original sources */
  617.                 }
  618.         }
  619.         return numbits;
  620. }
  621.  
  622.  
  623. static int III_get_scale_factors_2(mpg123_handle *fr, int *scf,struct gr_info_s *gr_info,int i_stereo)
  624. {
  625.         const unsigned char *pnt;
  626.         int i,j,n=0,numbits=0;
  627.         unsigned int slen;
  628.  
  629.         const unsigned char stab[3][6][4] =
  630.         {
  631.                 {
  632.                         { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0},
  633.                         { 7, 7, 7,0 } , { 6, 6, 6,3 } , {  8, 8,5,0}
  634.                 },
  635.                 {
  636.                         { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0},
  637.                         {12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0}
  638.                 },
  639.                 {
  640.                         { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0},
  641.                         { 6,15,12,0 } , { 6,12, 9,6 } , {  6,18,9,0}
  642.                 }
  643.         };
  644.  
  645.         if(i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */
  646.         slen = i_slen2[gr_info->scalefac_compress>>1];
  647.         else
  648.         slen = n_slen2[gr_info->scalefac_compress];
  649.  
  650.         gr_info->preflag = (slen>>15) & 0x1;
  651.  
  652.         n = 0;  
  653.         if( gr_info->block_type == 2 )
  654.         {
  655.                 n++;
  656.                 if(gr_info->mixed_block_flag) n++;
  657.         }
  658.  
  659.         pnt = stab[n][(slen>>12)&0x7];
  660.  
  661.         for(i=0;i<4;i++)
  662.         {
  663.                 int num = slen & 0x7;
  664.                 slen >>= 3;
  665.                 if(num)
  666.                 {
  667.                         for(j=0;j<(int)(pnt[i]);j++) *scf++ = getbits_fast(fr, num);
  668.  
  669.                         numbits += pnt[i] * num;
  670.                 }
  671.                 else
  672.                 for(j=0;j<(int)(pnt[i]);j++) *scf++ = 0;
  673.         }
  674.  
  675.         n = (n << 1) + 1;
  676.         for(i=0;i<n;i++) *scf++ = 0;
  677.  
  678.         return numbits;
  679. }
  680.  
  681. static unsigned char pretab_choice[2][22] =
  682. {
  683.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  684.         {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0}
  685. };
  686.  
  687. /*
  688.         Dequantize samples
  689.         ...includes Huffman decoding
  690. */
  691.  
  692. /* 24 is enough because tab13 has max. a 19 bit huffvector */
  693. #define BITSHIFT ((sizeof(long)-1)*8)
  694. #define REFRESH_MASK \
  695.         while(num < BITSHIFT) { \
  696.                 mask |= ((unsigned long)getbyte(fr))<<(BITSHIFT-num); \
  697.                 num += 8; \
  698.                 part2remain -= 8; }
  699.  
  700. static int III_dequantize_sample(mpg123_handle *fr, real xr[SBLIMIT][SSLIMIT],int *scf, struct gr_info_s *gr_info,int sfreq,int part2bits)
  701. {
  702.         int shift = 1 + gr_info->scalefac_scale;
  703.         real *xrpnt = (real *) xr;
  704.         int l[3],l3;
  705.         int part2remain = gr_info->part2_3_length - part2bits;
  706.         int *me;
  707. #ifdef REAL_IS_FIXED
  708.         int gainpow2_scale_idx = 378;
  709. #endif
  710.  
  711.         /* mhipp tree has this split up a bit... */
  712.         int num=getbitoffset(fr);
  713.         long mask;
  714.         /* We must split this, because for num==0 the shift is undefined if you do it in one step. */
  715.         mask  = ((unsigned long) getbits(fr, num))<<BITSHIFT;
  716.         mask <<= 8-num;
  717.         part2remain -= num;
  718.  
  719.         {
  720.                 int bv       = gr_info->big_values;
  721.                 int region1  = gr_info->region1start;
  722.                 int region2  = gr_info->region2start;
  723.                 l3 = ((576>>1)-bv)>>1;  
  724.  
  725.                 /* we may lose the 'odd' bit here !! check this later again */
  726.                 if(bv <= region1)
  727.                 {
  728.                         l[0] = bv;
  729.                         l[1] = 0;
  730.                         l[2] = 0;
  731.                 }
  732.                 else
  733.                 {
  734.                         l[0] = region1;
  735.                         if(bv <= region2)
  736.                         {
  737.                                 l[1] = bv - l[0];
  738.                                 l[2] = 0;
  739.                         }
  740.                         else
  741.                         {
  742.                                 l[1] = region2 - l[0];
  743.                                 l[2] = bv - region2;
  744.                         }
  745.                 }
  746.         }
  747.  
  748.         if(gr_info->block_type == 2)
  749.         {
  750.                 /* decoding with short or mixed mode BandIndex table */
  751.                 int i,max[4];
  752.                 int step=0,lwin=3,cb=0;
  753.                 register real v = 0.0;
  754.                 register int *m,mc;
  755.  
  756.                 if(gr_info->mixed_block_flag)
  757.                 {
  758.                         max[3] = -1;
  759.                         max[0] = max[1] = max[2] = 2;
  760.                         m = map[sfreq][0];
  761.                         me = mapend[sfreq][0];
  762.                 }
  763.                 else
  764.                 {
  765.                         max[0] = max[1] = max[2] = max[3] = -1;
  766.                         /* max[3] not really needed in this case */
  767.                         m = map[sfreq][1];
  768.                         me = mapend[sfreq][1];
  769.                 }
  770.  
  771.                 mc = 0;
  772.                 for(i=0;i<2;i++)
  773.                 {
  774.                         int lp = l[i];
  775.                         const struct newhuff *h = ht+gr_info->table_select[i];
  776.                         for(;lp;lp--,mc--)
  777.                         {
  778.                                 register int x,y;
  779.                                 if( (!mc) )
  780.                                 {
  781.                                         mc    = *m++;
  782.                                         xrpnt = ((real *) xr) + (*m++);
  783.                                         lwin  = *m++;
  784.                                         cb    = *m++;
  785.                                         if(lwin == 3)
  786.                                         {
  787. #ifdef REAL_IS_FIXED
  788.                                                 gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
  789. #endif
  790.                                                 v = gr_info->pow2gain[(*scf++) << shift];
  791.                                                 step = 1;
  792.                                         }
  793.                                         else
  794.                                         {
  795. #ifdef REAL_IS_FIXED
  796.                                                 gainpow2_scale_idx = (int)(gr_info->full_gain[lwin] + (*scf << shift) - fr->gainpow2);
  797. #endif
  798.                                                 v = gr_info->full_gain[lwin][(*scf++) << shift];
  799.                                                 step = 3;
  800.                                         }
  801.                                 }
  802.                                 {
  803.                                         const short *val = h->table;
  804.                                         REFRESH_MASK;
  805.                                         while((y=*val++)<0)
  806.                                         {
  807.                                                 if (mask < 0) val -= y;
  808.  
  809.                                                 num--;
  810.                                                 mask <<= 1;
  811.                                         }
  812.                                         x = y >> 4;
  813.                                         y &= 0xf;
  814.                                 }
  815.                                 if(x == 15 && h->linbits)
  816.                                 {
  817.                                         max[lwin] = cb;
  818.                                         REFRESH_MASK;
  819.                                         x += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits);
  820.                                         num -= h->linbits+1;
  821.                                         mask <<= h->linbits;
  822.                                         if(mask < 0) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
  823.                                         else         *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
  824.  
  825.                                         mask <<= 1;
  826.                                 }
  827.                                 else if(x)
  828.                                 {
  829.                                         max[lwin] = cb;
  830.                                         if(mask < 0) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
  831.                                         else         *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
  832.  
  833.                                         num--;
  834.                                         mask <<= 1;
  835.                                 }
  836.                                 else *xrpnt = DOUBLE_TO_REAL(0.0);
  837.  
  838.                                 xrpnt += step;
  839.                                 if(y == 15 && h->linbits)
  840.                                 {
  841.                                         max[lwin] = cb;
  842.                                         REFRESH_MASK;
  843.                                         y += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits);
  844.                                         num -= h->linbits+1;
  845.                                         mask <<= h->linbits;
  846.                                         if(mask < 0) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
  847.                                         else         *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
  848.  
  849.                                         mask <<= 1;
  850.                                 }
  851.                                 else if(y)
  852.                                 {
  853.                                         max[lwin] = cb;
  854.                                         if(mask < 0) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
  855.                                         else         *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
  856.  
  857.                                         num--;
  858.                                         mask <<= 1;
  859.                                 }
  860.                                 else *xrpnt = DOUBLE_TO_REAL(0.0);
  861.  
  862.                                 xrpnt += step;
  863.                         }
  864.                 }
  865.  
  866.                 for(;l3 && (part2remain+num > 0);l3--)
  867.                 {
  868.                         const struct newhuff* h;
  869.                         const short* val;
  870.                         register short a;
  871.                         /*
  872.                                 This is only a humble hack to prevent a special segfault.
  873.                                 More insight into the real workings is still needed.
  874.                                 Especially why there are (valid?) files that make xrpnt exceed the array with 4 bytes without segfaulting, more seems to be really bad, though.
  875.                         */
  876.                         #ifdef DEBUG
  877.                         if(!(xrpnt < &xr[SBLIMIT][0]))
  878.                         {
  879.                                 if(VERBOSE) debug2("attempted soft xrpnt overflow (%p !< %p) ?", (void*) xrpnt, (void*) &xr[SBLIMIT][0]);
  880.                         }
  881.                         #endif
  882.                         if(!(xrpnt < &xr[SBLIMIT][0]+5))
  883.                         {
  884.                                 if(NOQUIET) error2("attempted xrpnt overflow (%p !< %p)", (void*) xrpnt, (void*) &xr[SBLIMIT][0]);
  885.                                 return 2;
  886.                         }
  887.                         h = htc+gr_info->count1table_select;
  888.                         val = h->table;
  889.  
  890.                         REFRESH_MASK;
  891.                         while((a=*val++)<0)
  892.                         {
  893.                                 if(mask < 0) val -= a;
  894.  
  895.                                 num--;
  896.                                 mask <<= 1;
  897.                         }
  898.                         if(part2remain+num <= 0)
  899.                         {
  900.                                 num -= part2remain+num;
  901.                                 break;
  902.                         }
  903.  
  904.                         for(i=0;i<4;i++)
  905.                         {
  906.                                 if(!(i & 1))
  907.                                 {
  908.                                         if(!mc)
  909.                                         {
  910.                                                 mc = *m++;
  911.                                                 xrpnt = ((real *) xr) + (*m++);
  912.                                                 lwin = *m++;
  913.                                                 cb = *m++;
  914.                                                 if(lwin == 3)
  915.                                                 {
  916. #ifdef REAL_IS_FIXED
  917.                                                         gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
  918. #endif
  919.                                                         v = gr_info->pow2gain[(*scf++) << shift];
  920.                                                         step = 1;
  921.                                                 }
  922.                                                 else
  923.                                                 {
  924. #ifdef REAL_IS_FIXED
  925.                                                         gainpow2_scale_idx = (int)(gr_info->full_gain[lwin] + (*scf << shift) - fr->gainpow2);
  926. #endif
  927.                                                         v = gr_info->full_gain[lwin][(*scf++) << shift];
  928.                                                         step = 3;
  929.                                                 }
  930.                                         }
  931.                                         mc--;
  932.                                 }
  933.                                 if( (a & (0x8>>i)) )
  934.                                 {
  935.                                         max[lwin] = cb;
  936.                                         if(part2remain+num <= 0)
  937.                                         break;
  938.  
  939.                                         if(mask < 0) *xrpnt = -REAL_SCALE_LAYER3(v, gainpow2_scale_idx);
  940.                                         else         *xrpnt =  REAL_SCALE_LAYER3(v, gainpow2_scale_idx);
  941.  
  942.                                         num--;
  943.                                         mask <<= 1;
  944.                                 }
  945.                                 else *xrpnt = DOUBLE_TO_REAL(0.0);
  946.  
  947.                                 xrpnt += step;
  948.                         }
  949.                 }
  950.  
  951.                 if(lwin < 3)
  952.                 { /* short band? */
  953.                         while(1)
  954.                         {
  955.                                 for(;mc > 0;mc--)
  956.                                 {
  957.                                         *xrpnt = DOUBLE_TO_REAL(0.0); xrpnt += 3; /* short band -> step=3 */
  958.                                         *xrpnt = DOUBLE_TO_REAL(0.0); xrpnt += 3;
  959.                                 }
  960.                                 if(m >= me)
  961.                                 break;
  962.  
  963.                                 mc    = *m++;
  964.                                 xrpnt = ((real *) xr) + *m++;
  965.                                 if(*m++ == 0)
  966.                                 break; /* optimize: field will be set to zero at the end of the function */
  967.  
  968.                                 m++; /* cb */
  969.                         }
  970.                 }
  971.  
  972.                 gr_info->maxband[0] = max[0]+1;
  973.                 gr_info->maxband[1] = max[1]+1;
  974.                 gr_info->maxband[2] = max[2]+1;
  975.                 gr_info->maxbandl   = max[3]+1;
  976.  
  977.                 {
  978.                         int rmax = max[0] > max[1] ? max[0] : max[1];
  979.                         rmax = (rmax > max[2] ? rmax : max[2]) + 1;
  980.                         gr_info->maxb = rmax ? fr->shortLimit[sfreq][rmax] : fr->longLimit[sfreq][max[3]+1];
  981.                 }
  982.  
  983.         }
  984.         else
  985.         {
  986.                 /* decoding with 'long' BandIndex table (block_type != 2) */
  987.                 const unsigned char *pretab = pretab_choice[gr_info->preflag];
  988.                 int i,max = -1;
  989.                 int cb = 0;
  990.                 int *m = map[sfreq][2];
  991.                 register real v = 0.0;
  992.                 int mc = 0;
  993.  
  994.                 /* long hash table values */
  995.                 for(i=0;i<3;i++)
  996.                 {
  997.                         int lp = l[i];
  998.                         const struct newhuff *h = ht+gr_info->table_select[i];
  999.  
  1000.                         for(;lp;lp--,mc--)
  1001.                         {
  1002.                                 int x,y;
  1003.                                 if(!mc)
  1004.                                 {
  1005.                                         mc = *m++;
  1006.                                         cb = *m++;
  1007. #ifdef CUT_SFB21
  1008.                                         if(cb == 21)
  1009.                                                 v = 0.0;
  1010.                                         else
  1011. #endif
  1012.                                         {
  1013. #ifdef REAL_IS_FIXED
  1014.                                                 gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
  1015. #endif
  1016.                                                 v = gr_info->pow2gain[(*(scf++) + (*pretab++)) << shift];
  1017.                                         }
  1018.                                 }
  1019.                                 {
  1020.                                         const short *val = h->table;
  1021.                                         REFRESH_MASK;
  1022.                                         while((y=*val++)<0)
  1023.                                         {
  1024.                                                 if (mask < 0) val -= y;
  1025.  
  1026.                                                 num--;
  1027.                                                 mask <<= 1;
  1028.                                         }
  1029.                                         x = y >> 4;
  1030.                                         y &= 0xf;
  1031.                                 }
  1032.  
  1033.                                 if(x == 15 && h->linbits)
  1034.                                 {
  1035.                                         max = cb;
  1036.                                         REFRESH_MASK;
  1037.                                         x += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits);
  1038.                                         num -= h->linbits+1;
  1039.                                         mask <<= h->linbits;
  1040.                                         if(mask < 0) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
  1041.                                         else         *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
  1042.  
  1043.                                         mask <<= 1;
  1044.                                 }
  1045.                                 else if(x)
  1046.                                 {
  1047.                                         max = cb;
  1048.                                         if(mask < 0) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
  1049.                                         else         *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
  1050.                                         num--;
  1051.  
  1052.                                         mask <<= 1;
  1053.                                 }
  1054.                                 else *xrpnt++ = DOUBLE_TO_REAL(0.0);
  1055.  
  1056.                                 if(y == 15 && h->linbits)
  1057.                                 {
  1058.                                         max = cb;
  1059.                                         REFRESH_MASK;
  1060.                                         y += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits);
  1061.                                         num -= h->linbits+1;
  1062.                                         mask <<= h->linbits;
  1063.                                         if(mask < 0) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
  1064.                                         else         *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
  1065.  
  1066.                                         mask <<= 1;
  1067.                                 }
  1068.                                 else if(y)
  1069.                                 {
  1070.                                         max = cb;
  1071.                                         if(mask < 0) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
  1072.                                         else         *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
  1073.  
  1074.                                         num--;
  1075.                                         mask <<= 1;
  1076.                                 }
  1077.                                 else *xrpnt++ = DOUBLE_TO_REAL(0.0);
  1078.                         }
  1079.                 }
  1080.  
  1081.                 /* short (count1table) values */
  1082.                 for(;l3 && (part2remain+num > 0);l3--)
  1083.                 {
  1084.                         const struct newhuff *h = htc+gr_info->count1table_select;
  1085.                         const short *val = h->table;
  1086.                         register short a;
  1087.  
  1088.                         REFRESH_MASK;
  1089.                         while((a=*val++)<0)
  1090.                         {
  1091.                                 if (mask < 0) val -= a;
  1092.  
  1093.                                 num--;
  1094.                                 mask <<= 1;
  1095.                         }
  1096.                         if(part2remain+num <= 0)
  1097.                         {
  1098.                                 num -= part2remain+num;
  1099.                                 break;
  1100.                         }
  1101.  
  1102.                         for(i=0;i<4;i++)
  1103.                         {
  1104.                                 if(!(i & 1))
  1105.                                 {
  1106.                                         if(!mc)
  1107.                                         {
  1108.                                                 mc = *m++;
  1109.                                                 cb = *m++;
  1110. #ifdef CUT_SFB21
  1111.                                                 if(cb == 21)
  1112.                                                         v = 0.0;
  1113.                                                 else
  1114. #endif
  1115.                                                 {
  1116. #ifdef REAL_IS_FIXED
  1117.                                                         gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
  1118. #endif
  1119.                                                         v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
  1120.                                                 }
  1121.                                         }
  1122.                                         mc--;
  1123.                                 }
  1124.                                 if( (a & (0x8>>i)) )
  1125.                                 {
  1126.                                         max = cb;
  1127.                                         if(part2remain+num <= 0)
  1128.                                         break;
  1129.  
  1130.                                         if(mask < 0) *xrpnt++ = -REAL_SCALE_LAYER3(v, gainpow2_scale_idx);
  1131.                                         else         *xrpnt++ =  REAL_SCALE_LAYER3(v, gainpow2_scale_idx);
  1132.  
  1133.                                         num--;
  1134.                                         mask <<= 1;
  1135.                                 }
  1136.                                 else *xrpnt++ = DOUBLE_TO_REAL(0.0);
  1137.                         }
  1138.                 }
  1139.  
  1140.                 gr_info->maxbandl = max+1;
  1141.                 gr_info->maxb = fr->longLimit[sfreq][gr_info->maxbandl];
  1142.         }
  1143.  
  1144.         part2remain += num;
  1145.         backbits(fr, num);
  1146.         num = 0;
  1147.  
  1148.         while(xrpnt < &xr[SBLIMIT][0])
  1149.         *xrpnt++ = DOUBLE_TO_REAL(0.0);
  1150.  
  1151.         while( part2remain > 16 )
  1152.         {
  1153.                 skipbits(fr, 16); /* Dismiss stuffing Bits */
  1154.                 part2remain -= 16;
  1155.         }
  1156.         if(part2remain > 0) skipbits(fr, part2remain);
  1157.         else if(part2remain < 0)
  1158.         {
  1159.                 debug1("Can't rewind stream by %d bits!",-part2remain);
  1160.                 return 1; /* -> error */
  1161.         }
  1162.         return 0;
  1163. }
  1164.  
  1165.  
  1166. /* calculate real channel values for Joint-I-Stereo-mode */
  1167. 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)
  1168. {
  1169.         real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf;
  1170.         const struct bandInfoStruct *bi = &bandInfo[sfreq];
  1171.  
  1172.         const real *tab1,*tab2;
  1173.  
  1174. #if 1
  1175.         int tab;
  1176. /* TODO: optimize as static */
  1177.         const real *tabs[3][2][2] =
  1178.         {
  1179.                 { { tan1_1,tan2_1 }       , { tan1_2,tan2_2 } },
  1180.                 { { pow1_1[0],pow2_1[0] } , { pow1_2[0],pow2_2[0] } },
  1181.                 { { pow1_1[1],pow2_1[1] } , { pow1_2[1],pow2_2[1] } }
  1182.         };
  1183.  
  1184.         tab = lsf + (gr_info->scalefac_compress & lsf);
  1185.         tab1 = tabs[tab][ms_stereo][0];
  1186.         tab2 = tabs[tab][ms_stereo][1];
  1187. #else
  1188.         if(lsf)
  1189.         {
  1190.                 int p = gr_info->scalefac_compress & 0x1;
  1191.                 if(ms_stereo)
  1192.                 {
  1193.                         tab1 = pow1_2[p];
  1194.                         tab2 = pow2_2[p];
  1195.                 }
  1196.                 else
  1197.                 {
  1198.                         tab1 = pow1_1[p];
  1199.                         tab2 = pow2_1[p];
  1200.                 }
  1201.         }
  1202.         else
  1203.         {
  1204.                 if(ms_stereo)
  1205.                 {
  1206.                         tab1 = tan1_2;
  1207.                         tab2 = tan2_2;
  1208.                 }
  1209.                 else
  1210.                 {
  1211.                         tab1 = tan1_1;
  1212.                         tab2 = tan2_1;
  1213.                 }
  1214.         }
  1215. #endif
  1216.  
  1217.         if(gr_info->block_type == 2)
  1218.         {
  1219.                 int lwin,do_l = 0;
  1220.                 if( gr_info->mixed_block_flag ) do_l = 1;
  1221.  
  1222.                 for(lwin=0;lwin<3;lwin++)
  1223.                 { /* process each window */
  1224.                         /* get first band with zero values */
  1225.                         int is_p,sb,idx,sfb = gr_info->maxband[lwin];  /* sfb is minimal 3 for mixed mode */
  1226.                         if(sfb > 3) do_l = 0;
  1227.  
  1228.                         for(;sfb<12;sfb++)
  1229.                         {
  1230.                                 is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
  1231.                                 if(is_p != 7)
  1232.                                 {
  1233.                                         real t1,t2;
  1234.                                         sb  = bi->shortDiff[sfb];
  1235.                                         idx = bi->shortIdx[sfb] + lwin;
  1236.                                         t1  = tab1[is_p]; t2 = tab2[is_p];
  1237.                                         for (; sb > 0; sb--,idx+=3)
  1238.                                         {
  1239.                                                 real v = xr[0][idx];
  1240.                                                 xr[0][idx] = REAL_MUL_15(v, t1);
  1241.                                                 xr[1][idx] = REAL_MUL_15(v, t2);
  1242.                                         }
  1243.                                 }
  1244.                         }
  1245.  
  1246. #if 1
  1247. /* in the original: copy 10 to 11 , here: copy 11 to 12
  1248. maybe still wrong??? (copy 12 to 13?) */
  1249.                         is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
  1250.                         sb   = bi->shortDiff[12];
  1251.                         idx  = bi->shortIdx[12] + lwin;
  1252. #else
  1253.                         is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
  1254.                         sb   = bi->shortDiff[11];
  1255.                         idx  = bi->shortIdx[11] + lwin;
  1256. #endif
  1257.                         if(is_p != 7)
  1258.                         {
  1259.                                 real t1,t2;
  1260.                                 t1 = tab1[is_p]; t2 = tab2[is_p];
  1261.                                 for( ; sb > 0; sb--,idx+=3 )
  1262.                                 {  
  1263.                                         real v = xr[0][idx];
  1264.                                         xr[0][idx] = REAL_MUL_15(v, t1);
  1265.                                         xr[1][idx] = REAL_MUL_15(v, t2);
  1266.                                 }
  1267.                         }
  1268.                 } /* end for(lwin; .. ; . ) */
  1269.  
  1270.                 /* also check l-part, if ALL bands in the three windows are 'empty' and mode = mixed_mode */
  1271.                 if(do_l)
  1272.                 {
  1273.                         int sfb = gr_info->maxbandl;
  1274.                         int idx;
  1275.                         if(sfb > 21) return; /* similarity fix related to CVE-2006-1655 */
  1276.  
  1277.                         idx = bi->longIdx[sfb];
  1278.                         for( ; sfb<8; sfb++ )
  1279.                         {
  1280.                                 int sb = bi->longDiff[sfb];
  1281.                                 int is_p = scalefac[sfb]; /* scale: 0-15 */
  1282.                                 if(is_p != 7)
  1283.                                 {
  1284.                                         real t1,t2;
  1285.                                         t1 = tab1[is_p]; t2 = tab2[is_p];
  1286.                                         for( ; sb > 0; sb--,idx++)
  1287.                                         {
  1288.                                                 real v = xr[0][idx];
  1289.                                                 xr[0][idx] = REAL_MUL_15(v, t1);
  1290.                                                 xr[1][idx] = REAL_MUL_15(v, t2);
  1291.                                         }
  1292.                                 }
  1293.                                 else idx += sb;
  1294.                         }
  1295.                 }    
  1296.         }
  1297.         else
  1298.         { /* ((gr_info->block_type != 2)) */
  1299.                 int sfb = gr_info->maxbandl;
  1300.                 int is_p,idx;
  1301.                 if(sfb > 21) return; /* tightened fix for CVE-2006-1655 */
  1302.  
  1303.                 idx = bi->longIdx[sfb];
  1304.                 for ( ; sfb<21; sfb++)
  1305.                 {
  1306.                         int sb = bi->longDiff[sfb];
  1307.                         is_p = scalefac[sfb]; /* scale: 0-15 */
  1308.                         if(is_p != 7)
  1309.                         {
  1310.                                 real t1,t2;
  1311.                                 t1 = tab1[is_p]; t2 = tab2[is_p];
  1312.                                 for( ; sb > 0; sb--,idx++)
  1313.                                 {
  1314.                                          real v = xr[0][idx];
  1315.                                          xr[0][idx] = REAL_MUL_15(v, t1);
  1316.                                          xr[1][idx] = REAL_MUL_15(v, t2);
  1317.                                 }
  1318.                         }
  1319.                         else idx += sb;
  1320.                 }
  1321.  
  1322.                 is_p = scalefac[20];
  1323.                 if(is_p != 7)
  1324.                 {  /* copy l-band 20 to l-band 21 */
  1325.                         int sb;
  1326.                         real t1 = tab1[is_p],t2 = tab2[is_p];
  1327.  
  1328.                         for( sb = bi->longDiff[21]; sb > 0; sb--,idx++ )
  1329.                         {
  1330.                                 real v = xr[0][idx];
  1331.                                 xr[0][idx] = REAL_MUL_15(v, t1);
  1332.                                 xr[1][idx] = REAL_MUL_15(v, t2);
  1333.                         }
  1334.                 }
  1335.         }
  1336. }
  1337.  
  1338.  
  1339. static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info)
  1340. {
  1341.         int sblim;
  1342.  
  1343.         if(gr_info->block_type == 2)
  1344.         {
  1345.                         if(!gr_info->mixed_block_flag) return;
  1346.  
  1347.                         sblim = 1;
  1348.         }
  1349.         else sblim = gr_info->maxb-1;
  1350.  
  1351.         /* 31 alias-reduction operations between each pair of sub-bands */
  1352.         /* with 8 butterflies between each pair                         */
  1353.  
  1354.         {
  1355.                 int sb;
  1356.                 real *xr1=(real *) xr[1];
  1357.  
  1358.                 for(sb=sblim; sb; sb--,xr1+=10)
  1359.                 {
  1360.                         int ss;
  1361.                         real *cs=aa_cs,*ca=aa_ca;
  1362.                         real *xr2 = xr1;
  1363.  
  1364.                         for(ss=7;ss>=0;ss--)
  1365.                         { /* upper and lower butterfly inputs */
  1366.                                 register real bu = *--xr2,bd = *xr1;
  1367.                                 *xr2   = REAL_MUL(bu, *cs) - REAL_MUL(bd, *ca);
  1368.                                 *xr1++ = REAL_MUL(bd, *cs++) + REAL_MUL(bu, *ca++);
  1369.                         }
  1370.                 }
  1371.         }
  1372. }
  1373.  
  1374. /*
  1375.         This is an optimized DCT from Jeff Tsay's maplay 1.2+ package.
  1376.         Saved one multiplication by doing the 'twiddle factor' stuff
  1377.         together with the window mul. (MH)
  1378.  
  1379.         This uses Byeong Gi Lee's Fast Cosine Transform algorithm, but the
  1380.         9 point IDCT needs to be reduced further. Unfortunately, I don't
  1381.         know how to do that, because 9 is not an even number. - Jeff.
  1382.  
  1383.         Original Message:
  1384.  
  1385.         9 Point Inverse Discrete Cosine Transform
  1386.  
  1387.         This piece of code is Copyright 1997 Mikko Tommila and is freely usable
  1388.         by anybody. The algorithm itself is of course in the public domain.
  1389.  
  1390.         Again derived heuristically from the 9-point WFTA.
  1391.  
  1392.         The algorithm is optimized (?) for speed, not for small rounding errors or
  1393.         good readability.
  1394.  
  1395.         36 additions, 11 multiplications
  1396.  
  1397.         Again this is very likely sub-optimal.
  1398.  
  1399.         The code is optimized to use a minimum number of temporary variables,
  1400.         so it should compile quite well even on 8-register Intel x86 processors.
  1401.         This makes the code quite obfuscated and very difficult to understand.
  1402.  
  1403.         References:
  1404.         [1] S. Winograd: "On Computing the Discrete Fourier Transform",
  1405.             Mathematics of Computation, Volume 32, Number 141, January 1978,
  1406.             Pages 175-199
  1407. */
  1408.  
  1409. /* Calculation of the inverse MDCT
  1410.    used to be static without 3dnow - does that really matter? */
  1411. void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf)
  1412. {
  1413. #ifdef NEW_DCT9
  1414.         real tmp[18];
  1415. #endif
  1416.  
  1417.         {
  1418.                 register real *in = inbuf;
  1419.  
  1420.                 in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14];
  1421.                 in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11];
  1422.                 in[11]+=in[10]; in[10]+=in[9];  in[9] +=in[8];
  1423.                 in[8] +=in[7];  in[7] +=in[6];  in[6] +=in[5];
  1424.                 in[5] +=in[4];  in[4] +=in[3];  in[3] +=in[2];
  1425.                 in[2] +=in[1];  in[1] +=in[0];
  1426.  
  1427.                 in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9];
  1428.                 in[9] +=in[7];  in[7] +=in[5];  in[5] +=in[3];  in[3] +=in[1];
  1429.  
  1430.  
  1431. #ifdef NEW_DCT9
  1432. #if 1
  1433.                 {
  1434.                         real t3;
  1435.                         {
  1436.                                 real t0, t1, t2;
  1437.  
  1438.                                 t0 = REAL_MUL(COS6_2, (in[8] + in[16] - in[4]));
  1439.                                 t1 = REAL_MUL(COS6_2, in[12]);
  1440.  
  1441.                                 t3 = in[0];
  1442.                                 t2 = t3 - t1 - t1;
  1443.                                 tmp[1] = tmp[7] = t2 - t0;
  1444.                                 tmp[4]          = t2 + t0 + t0;
  1445.                                 t3 += t1;
  1446.  
  1447.                                 t2 = REAL_MUL(COS6_1, (in[10] + in[14] - in[2]));
  1448.                                 tmp[1] -= t2;
  1449.                                 tmp[7] += t2;
  1450.                         }
  1451.                         {
  1452.                                 real t0, t1, t2;
  1453.  
  1454.                                 t0 = REAL_MUL(cos9[0], (in[4] + in[8] ));
  1455.                                 t1 = REAL_MUL(cos9[1], (in[8] - in[16]));
  1456.                                 t2 = REAL_MUL(cos9[2], (in[4] + in[16]));
  1457.  
  1458.                                 tmp[2] = tmp[6] = t3 - t0      - t2;
  1459.                                 tmp[0] = tmp[8] = t3 + t0 + t1;
  1460.                                 tmp[3] = tmp[5] = t3      - t1 + t2;
  1461.                         }
  1462.                 }
  1463.                 {
  1464.                         real t1, t2, t3;
  1465.  
  1466.                         t1 = REAL_MUL(cos18[0], (in[2]  + in[10]));
  1467.                         t2 = REAL_MUL(cos18[1], (in[10] - in[14]));
  1468.                         t3 = REAL_MUL(COS6_1,    in[6]);
  1469.  
  1470.                         {
  1471.                                 real t0 = t1 + t2 + t3;
  1472.                                 tmp[0] += t0;
  1473.                                 tmp[8] -= t0;
  1474.                         }
  1475.  
  1476.                         t2 -= t3;
  1477.                         t1 -= t3;
  1478.  
  1479.                         t3 = REAL_MUL(cos18[2], (in[2] + in[14]));
  1480.  
  1481.                         t1 += t3;
  1482.                         tmp[3] += t1;
  1483.                         tmp[5] -= t1;
  1484.  
  1485.                         t2 -= t3;
  1486.                         tmp[2] += t2;
  1487.                         tmp[6] -= t2;
  1488.                 }
  1489.  
  1490. #else
  1491.                 {
  1492.                         real t0, t1, t2, t3, t4, t5, t6, t7;
  1493.  
  1494.                         t1 = REAL_MUL(COS6_2, in[12]);
  1495.                         t2 = REAL_MUL(COS6_2, (in[8] + in[16] - in[4]));
  1496.  
  1497.                         t3 = in[0] + t1;
  1498.                         t4 = in[0] - t1 - t1;
  1499.                         t5     = t4 - t2;
  1500.                         tmp[4] = t4 + t2 + t2;
  1501.  
  1502.                         t0 = REAL_MUL(cos9[0], (in[4] + in[8]));
  1503.                         t1 = REAL_MUL(cos9[1], (in[8] - in[16]));
  1504.  
  1505.                         t2 = REAL_MUL(cos9[2], (in[4] + in[16]));
  1506.  
  1507.                         t6 = t3 - t0 - t2;
  1508.                         t0 += t3 + t1;
  1509.                         t3 += t2 - t1;
  1510.  
  1511.                         t2 = REAL_MUL(cos18[0], (in[2]  + in[10]));
  1512.                         t4 = REAL_MUL(cos18[1], (in[10] - in[14]));
  1513.                         t7 = REAL_MUL(COS6_1, in[6]);
  1514.  
  1515.                         t1 = t2 + t4 + t7;
  1516.                         tmp[0] = t0 + t1;
  1517.                         tmp[8] = t0 - t1;
  1518.                         t1 = REAL_MUL(cos18[2], (in[2] + in[14]));
  1519.                         t2 += t1 - t7;
  1520.  
  1521.                         tmp[3] = t3 + t2;
  1522.                         t0 = REAL_MUL(COS6_1, (in[10] + in[14] - in[2]));
  1523.                         tmp[5] = t3 - t2;
  1524.  
  1525.                         t4 -= t1 + t7;
  1526.  
  1527.                         tmp[1] = t5 - t0;
  1528.                         tmp[7] = t5 + t0;
  1529.                         tmp[2] = t6 + t4;
  1530.                         tmp[6] = t6 - t4;
  1531.                 }
  1532. #endif
  1533.  
  1534.                 {
  1535.                         real t0, t1, t2, t3, t4, t5, t6, t7;
  1536.  
  1537.                         t1 = REAL_MUL(COS6_2, in[13]);
  1538.                         t2 = REAL_MUL(COS6_2, (in[9] + in[17] - in[5]));
  1539.  
  1540.                         t3 = in[1] + t1;
  1541.                         t4 = in[1] - t1 - t1;
  1542.                         t5 = t4 - t2;
  1543.  
  1544.                         t0 = REAL_MUL(cos9[0], (in[5] + in[9]));
  1545.                         t1 = REAL_MUL(cos9[1], (in[9] - in[17]));
  1546.  
  1547.                         tmp[13] = REAL_MUL((t4 + t2 + t2), tfcos36[17-13]);
  1548.                         t2 = REAL_MUL(cos9[2], (in[5] + in[17]));
  1549.  
  1550.                         t6 = t3 - t0 - t2;
  1551.                         t0 += t3 + t1;
  1552.                         t3 += t2 - t1;
  1553.  
  1554.                         t2 = REAL_MUL(cos18[0], (in[3]  + in[11]));
  1555.                         t4 = REAL_MUL(cos18[1], (in[11] - in[15]));
  1556.                         t7 = REAL_MUL(COS6_1, in[7]);
  1557.  
  1558.                         t1 = t2 + t4 + t7;
  1559.                         tmp[17] = REAL_MUL((t0 + t1), tfcos36[17-17]);
  1560.                         tmp[9]  = REAL_MUL((t0 - t1), tfcos36[17-9]);
  1561.                         t1 = REAL_MUL(cos18[2], (in[3] + in[15]));
  1562.                         t2 += t1 - t7;
  1563.  
  1564.                         tmp[14] = REAL_MUL((t3 + t2), tfcos36[17-14]);
  1565.                         t0 = REAL_MUL(COS6_1, (in[11] + in[15] - in[3]));
  1566.                         tmp[12] = REAL_MUL((t3 - t2), tfcos36[17-12]);
  1567.  
  1568.                         t4 -= t1 + t7;
  1569.  
  1570.                         tmp[16] = REAL_MUL((t5 - t0), tfcos36[17-16]);
  1571.                         tmp[10] = REAL_MUL((t5 + t0), tfcos36[17-10]);
  1572.                         tmp[15] = REAL_MUL((t6 + t4), tfcos36[17-15]);
  1573.                         tmp[11] = REAL_MUL((t6 - t4), tfcos36[17-11]);
  1574.                 }
  1575.  
  1576. #define MACRO(v) { \
  1577.                 real tmpval; \
  1578.                 tmpval = tmp[(v)] + tmp[17-(v)]; \
  1579.                 out2[9+(v)] = REAL_MUL(tmpval, w[27+(v)]); \
  1580.                 out2[8-(v)] = REAL_MUL(tmpval, w[26-(v)]); \
  1581.                 tmpval = tmp[(v)] - tmp[17-(v)]; \
  1582.                 ts[SBLIMIT*(8-(v))] = out1[8-(v)] + REAL_MUL(tmpval, w[8-(v)]); \
  1583.                 ts[SBLIMIT*(9+(v))] = out1[9+(v)] + REAL_MUL(tmpval, w[9+(v)]); }
  1584.  
  1585.                 {
  1586.                         register real *out2 = o2;
  1587.                         register real *w = wintab;
  1588.                         register real *out1 = o1;
  1589.                         register real *ts = tsbuf;
  1590.  
  1591.                         MACRO(0);
  1592.                         MACRO(1);
  1593.                         MACRO(2);
  1594.                         MACRO(3);
  1595.                         MACRO(4);
  1596.                         MACRO(5);
  1597.                         MACRO(6);
  1598.                         MACRO(7);
  1599.                         MACRO(8);
  1600.                 }
  1601.  
  1602. #else
  1603.  
  1604.                 {
  1605.  
  1606. #define MACRO0(v) { \
  1607.         real tmp; \
  1608.         out2[9+(v)] = REAL_MUL((tmp = sum0 + sum1), w[27+(v)]); \
  1609.         out2[8-(v)] = REAL_MUL(tmp, w[26-(v)]);   } \
  1610.         sum0 -= sum1; \
  1611.         ts[SBLIMIT*(8-(v))] = out1[8-(v)] + REAL_MUL(sum0, w[8-(v)]); \
  1612.         ts[SBLIMIT*(9+(v))] = out1[9+(v)] + REAL_MUL(sum0, w[9+(v)]);
  1613. #define MACRO1(v) { \
  1614.         real sum0,sum1; \
  1615.         sum0 = tmp1a + tmp2a; \
  1616.         sum1 = REAL_MUL((tmp1b + tmp2b), tfcos36[(v)]); \
  1617.         MACRO0(v); }
  1618. #define MACRO2(v) { \
  1619.         real sum0,sum1; \
  1620.         sum0 = tmp2a - tmp1a; \
  1621.         sum1 = REAL_MUL((tmp2b - tmp1b), tfcos36[(v)]); \
  1622.         MACRO0(v); }
  1623.  
  1624.                         register const real *c = COS9;
  1625.                         register real *out2 = o2;
  1626.                         register real *w = wintab;
  1627.                         register real *out1 = o1;
  1628.                         register real *ts = tsbuf;
  1629.  
  1630.                         real ta33,ta66,tb33,tb66;
  1631.  
  1632.                         ta33 = REAL_MUL(in[2*3+0], c[3]);
  1633.                         ta66 = REAL_MUL(in[2*6+0], c[6]);
  1634.                         tb33 = REAL_MUL(in[2*3+1], c[3]);
  1635.                         tb66 = REAL_MUL(in[2*6+1], c[6]);
  1636.  
  1637.                         {
  1638.                                 real tmp1a,tmp2a,tmp1b,tmp2b;
  1639.                                 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]);
  1640.                                 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]);
  1641.                                 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]);
  1642.                                 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]);
  1643.  
  1644.                                 MACRO1(0);
  1645.                                 MACRO2(8);
  1646.                         }
  1647.  
  1648.                         {
  1649.                                 real tmp1a,tmp2a,tmp1b,tmp2b;
  1650.                                 tmp1a = REAL_MUL(( in[2*1+0] - in[2*5+0] - in[2*7+0] ), c[3]);
  1651.                                 tmp1b = REAL_MUL(( in[2*1+1] - in[2*5+1] - in[2*7+1] ), c[3]);
  1652.                                 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];
  1653.                                 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];
  1654.  
  1655.                                 MACRO1(1);
  1656.                                 MACRO2(7);
  1657.                         }
  1658.  
  1659.                         {
  1660.                                 real tmp1a,tmp2a,tmp1b,tmp2b;
  1661.                                 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]);
  1662.                                 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]);
  1663.                                 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]);
  1664.                                 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]);
  1665.  
  1666.                                 MACRO1(2);
  1667.                                 MACRO2(6);
  1668.                         }
  1669.  
  1670.                         {
  1671.                                 real tmp1a,tmp2a,tmp1b,tmp2b;
  1672.                                 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]);
  1673.                                 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]);
  1674.                                 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]);
  1675.                                 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]);
  1676.  
  1677.                                 MACRO1(3);
  1678.                                 MACRO2(5);
  1679.                         }
  1680.  
  1681.                         {
  1682.                                 real sum0,sum1;
  1683.                                 sum0 =  in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0];
  1684.                                 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]);
  1685.                                 MACRO0(4);
  1686.                         }
  1687.                 }
  1688. #endif
  1689.  
  1690.         }
  1691. }
  1692.  
  1693.  
  1694. /* new DCT12 */
  1695. static void dct12(real *in,real *rawout1,real *rawout2,register real *wi,register real *ts)
  1696. {
  1697. #define DCT12_PART1 \
  1698.         in5 = in[5*3];  \
  1699.         in5 += (in4 = in[4*3]); \
  1700.         in4 += (in3 = in[3*3]); \
  1701.         in3 += (in2 = in[2*3]); \
  1702.         in2 += (in1 = in[1*3]); \
  1703.         in1 += (in0 = in[0*3]); \
  1704.         \
  1705.         in5 += in3; in3 += in1; \
  1706.         \
  1707.         in2 = REAL_MUL(in2, COS6_1); \
  1708.         in3 = REAL_MUL(in3, COS6_1);
  1709.  
  1710. #define DCT12_PART2 \
  1711.         in0 += REAL_MUL(in4, COS6_2); \
  1712.         \
  1713.         in4 = in0 + in2; \
  1714.         in0 -= in2;      \
  1715.         \
  1716.         in1 += REAL_MUL(in5, COS6_2); \
  1717.         \
  1718.         in5 = REAL_MUL((in1 + in3), tfcos12[0]); \
  1719.         in1 = REAL_MUL((in1 - in3), tfcos12[2]); \
  1720.         \
  1721.         in3 = in4 + in5; \
  1722.         in4 -= in5;      \
  1723.         \
  1724.         in2 = in0 + in1; \
  1725.         in0 -= in1;
  1726.  
  1727.         {
  1728.                 real in0,in1,in2,in3,in4,in5;
  1729.                 register real *out1 = rawout1;
  1730.                 ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2];
  1731.                 ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5];
  1732.  
  1733.                 DCT12_PART1
  1734.  
  1735.                 {
  1736.                         real tmp0,tmp1 = (in0 - in4);
  1737.                         {
  1738.                                 real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]);
  1739.                                 tmp0 = tmp1 + tmp2;
  1740.                                 tmp1 -= tmp2;
  1741.                         }
  1742.                         ts[(17-1)*SBLIMIT] = out1[17-1] + REAL_MUL(tmp0, wi[11-1]);
  1743.                         ts[(12+1)*SBLIMIT] = out1[12+1] + REAL_MUL(tmp0, wi[6+1]);
  1744.                         ts[(6 +1)*SBLIMIT] = out1[6 +1] + REAL_MUL(tmp1, wi[1]);
  1745.                         ts[(11-1)*SBLIMIT] = out1[11-1] + REAL_MUL(tmp1, wi[5-1]);
  1746.                 }
  1747.  
  1748.                 DCT12_PART2
  1749.  
  1750.                 ts[(17-0)*SBLIMIT] = out1[17-0] + REAL_MUL(in2, wi[11-0]);
  1751.                 ts[(12+0)*SBLIMIT] = out1[12+0] + REAL_MUL(in2, wi[6+0]);
  1752.                 ts[(12+2)*SBLIMIT] = out1[12+2] + REAL_MUL(in3, wi[6+2]);
  1753.                 ts[(17-2)*SBLIMIT] = out1[17-2] + REAL_MUL(in3, wi[11-2]);
  1754.  
  1755.                 ts[(6 +0)*SBLIMIT]  = out1[6+0] + REAL_MUL(in0, wi[0]);
  1756.                 ts[(11-0)*SBLIMIT] = out1[11-0] + REAL_MUL(in0, wi[5-0]);
  1757.                 ts[(6 +2)*SBLIMIT]  = out1[6+2] + REAL_MUL(in4, wi[2]);
  1758.                 ts[(11-2)*SBLIMIT] = out1[11-2] + REAL_MUL(in4, wi[5-2]);
  1759.         }
  1760.  
  1761.         in++;
  1762.  
  1763.         {
  1764.                 real in0,in1,in2,in3,in4,in5;
  1765.                 register real *out2 = rawout2;
  1766.  
  1767.                 DCT12_PART1
  1768.  
  1769.                 {
  1770.                         real tmp0,tmp1 = (in0 - in4);
  1771.                         {
  1772.                                 real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]);
  1773.                                 tmp0 = tmp1 + tmp2;
  1774.                                 tmp1 -= tmp2;
  1775.                         }
  1776.                         out2[5-1] = REAL_MUL(tmp0, wi[11-1]);
  1777.                         out2[0+1] = REAL_MUL(tmp0, wi[6+1]);
  1778.                         ts[(12+1)*SBLIMIT] += REAL_MUL(tmp1, wi[1]);
  1779.                         ts[(17-1)*SBLIMIT] += REAL_MUL(tmp1, wi[5-1]);
  1780.                 }
  1781.  
  1782.                 DCT12_PART2
  1783.  
  1784.                 out2[5-0] = REAL_MUL(in2, wi[11-0]);
  1785.                 out2[0+0] = REAL_MUL(in2, wi[6+0]);
  1786.                 out2[0+2] = REAL_MUL(in3, wi[6+2]);
  1787.                 out2[5-2] = REAL_MUL(in3, wi[11-2]);
  1788.  
  1789.                 ts[(12+0)*SBLIMIT] += REAL_MUL(in0, wi[0]);
  1790.                 ts[(17-0)*SBLIMIT] += REAL_MUL(in0, wi[5-0]);
  1791.                 ts[(12+2)*SBLIMIT] += REAL_MUL(in4, wi[2]);
  1792.                 ts[(17-2)*SBLIMIT] += REAL_MUL(in4, wi[5-2]);
  1793.         }
  1794.  
  1795.         in++;
  1796.  
  1797.         {
  1798.                 real in0,in1,in2,in3,in4,in5;
  1799.                 register real *out2 = rawout2;
  1800.                 out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0;
  1801.  
  1802.                 DCT12_PART1
  1803.  
  1804.                 {
  1805.                         real tmp0,tmp1 = (in0 - in4);
  1806.                         {
  1807.                                 real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]);
  1808.                                 tmp0 = tmp1 + tmp2;
  1809.                                 tmp1 -= tmp2;
  1810.                         }
  1811.                         out2[11-1] = REAL_MUL(tmp0, wi[11-1]);
  1812.                         out2[6 +1] = REAL_MUL(tmp0, wi[6+1]);
  1813.                         out2[0+1] += REAL_MUL(tmp1, wi[1]);
  1814.                         out2[5-1] += REAL_MUL(tmp1, wi[5-1]);
  1815.                 }
  1816.  
  1817.                 DCT12_PART2
  1818.  
  1819.                 out2[11-0] = REAL_MUL(in2, wi[11-0]);
  1820.                 out2[6 +0] = REAL_MUL(in2, wi[6+0]);
  1821.                 out2[6 +2] = REAL_MUL(in3, wi[6+2]);
  1822.                 out2[11-2] = REAL_MUL(in3, wi[11-2]);
  1823.  
  1824.                 out2[0+0] += REAL_MUL(in0, wi[0]);
  1825.                 out2[5-0] += REAL_MUL(in0, wi[5-0]);
  1826.                 out2[0+2] += REAL_MUL(in4, wi[2]);
  1827.                 out2[5-2] += REAL_MUL(in4, wi[5-2]);
  1828.         }
  1829. }
  1830.  
  1831.  
  1832. static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT], real tsOut[SSLIMIT][SBLIMIT], int ch,struct gr_info_s *gr_info, mpg123_handle *fr)
  1833. {
  1834.         real (*block)[2][SBLIMIT*SSLIMIT] = fr->hybrid_block;
  1835.         int *blc = fr->hybrid_blc;
  1836.  
  1837.         real *tspnt = (real *) tsOut;
  1838.         real *rawout1,*rawout2;
  1839.         int bt = 0;
  1840.         size_t sb = 0;
  1841.  
  1842.         {
  1843.                 int b = blc[ch];
  1844.                 rawout1=block[b][ch];
  1845.                 b=-b+1;
  1846.                 rawout2=block[b][ch];
  1847.                 blc[ch] = b;
  1848.         }
  1849.  
  1850.         if(gr_info->mixed_block_flag)
  1851.         {
  1852.                 sb = 2;
  1853.                 opt_dct36(fr)(fsIn[0],rawout1,rawout2,win[0],tspnt);
  1854.                 opt_dct36(fr)(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1);
  1855.                 rawout1 += 36; rawout2 += 36; tspnt += 2;
  1856.         }
  1857.  
  1858.         bt = gr_info->block_type;
  1859.         if(bt == 2)
  1860.         {
  1861.                 for(; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36)
  1862.                 {
  1863.                         dct12(fsIn[sb]  ,rawout1   ,rawout2   ,win[2] ,tspnt);
  1864.                         dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1);
  1865.                 }
  1866.         }
  1867.         else
  1868.         {
  1869.                 for(; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36)
  1870.                 {
  1871.                         opt_dct36(fr)(fsIn[sb],rawout1,rawout2,win[bt],tspnt);
  1872.                         opt_dct36(fr)(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1);
  1873.                 }
  1874.         }
  1875.  
  1876.         for(;sb<SBLIMIT;sb++,tspnt++)
  1877.         {
  1878.                 int i;
  1879.                 for(i=0;i<SSLIMIT;i++)
  1880.                 {
  1881.                         tspnt[i*SBLIMIT] = *rawout1++;
  1882.                         *rawout2++ = DOUBLE_TO_REAL(0.0);
  1883.                 }
  1884.         }
  1885. }
  1886.  
  1887.  
  1888. /* And at the end... the main layer3 handler */
  1889. int do_layer3(mpg123_handle *fr)
  1890. {
  1891.         int gr, ch, ss,clip=0;
  1892.         int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
  1893.         struct III_sideinfo sideinfo;
  1894.         int stereo = fr->stereo;
  1895.         int single = fr->single;
  1896.         int ms_stereo,i_stereo;
  1897.         int sfreq = fr->sampling_frequency;
  1898.         int stereo1,granules;
  1899.  
  1900.         if(stereo == 1)
  1901.         { /* stream is mono */
  1902.                 stereo1 = 1;
  1903.                 single = SINGLE_LEFT;
  1904.         }
  1905.         else if(single != SINGLE_STEREO) /* stream is stereo, but force to mono */
  1906.         stereo1 = 1;
  1907.         else
  1908.         stereo1 = 2;
  1909.  
  1910.         if(fr->mode == MPG_MD_JOINT_STEREO)
  1911.         {
  1912.                 ms_stereo = (fr->mode_ext & 0x2)>>1;
  1913.                 i_stereo  = fr->mode_ext & 0x1;
  1914.         }
  1915.         else ms_stereo = i_stereo = 0;
  1916.  
  1917.         granules = fr->lsf ? 1 : 2;
  1918.  
  1919.         /* quick hack to keep the music playing */
  1920.         /* after having seen this nasty test file... */
  1921.         if(III_get_side_info(fr, &sideinfo,stereo,ms_stereo,sfreq,single))
  1922.         {
  1923.                 if(NOQUIET) error("bad frame - unable to get valid sideinfo");
  1924.                 return clip;
  1925.         }
  1926.  
  1927.         set_pointer(fr,sideinfo.main_data_begin);
  1928.  
  1929.         for(gr=0;gr<granules;gr++)
  1930.         {
  1931.                 /*  hybridIn[2][SBLIMIT][SSLIMIT] */
  1932.                 real (*hybridIn)[SBLIMIT][SSLIMIT] = fr->layer3.hybrid_in;
  1933.                 /*  hybridOut[2][SSLIMIT][SBLIMIT] */
  1934.                 real (*hybridOut)[SSLIMIT][SBLIMIT] = fr->layer3.hybrid_out;
  1935.  
  1936.                 {
  1937.                         struct gr_info_s *gr_info = &(sideinfo.ch[0].gr[gr]);
  1938.                         long part2bits;
  1939.                         if(fr->lsf)
  1940.                         part2bits = III_get_scale_factors_2(fr, scalefacs[0],gr_info,0);
  1941.                         else
  1942.                         part2bits = III_get_scale_factors_1(fr, scalefacs[0],gr_info,0,gr);
  1943.  
  1944.                         if(III_dequantize_sample(fr, hybridIn[0], scalefacs[0],gr_info,sfreq,part2bits))
  1945.                         {
  1946.                                 if(VERBOSE2) error("dequantization failed!");
  1947.                                 return clip;
  1948.                         }
  1949.                 }
  1950.  
  1951.                 if(stereo == 2)
  1952.                 {
  1953.                         struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]);
  1954.                         long part2bits;
  1955.                         if(fr->lsf)
  1956.                         part2bits = III_get_scale_factors_2(fr, scalefacs[1],gr_info,i_stereo);
  1957.                         else
  1958.                         part2bits = III_get_scale_factors_1(fr, scalefacs[1],gr_info,1,gr);
  1959.  
  1960.                         if(III_dequantize_sample(fr, hybridIn[1],scalefacs[1],gr_info,sfreq,part2bits))
  1961.                         {
  1962.                                 if(VERBOSE2) error("dequantization failed!");
  1963.                                 return clip;
  1964.                         }
  1965.  
  1966.                         if(ms_stereo)
  1967.                         {
  1968.                                 int i;
  1969.                                 unsigned int maxb = sideinfo.ch[0].gr[gr].maxb;
  1970.                                 if(sideinfo.ch[1].gr[gr].maxb > maxb) maxb = sideinfo.ch[1].gr[gr].maxb;
  1971.  
  1972.                                 for(i=0;i<SSLIMIT*(int)maxb;i++)
  1973.                                 {
  1974.                                         real tmp0 = ((real *)hybridIn[0])[i];
  1975.                                         real tmp1 = ((real *)hybridIn[1])[i];
  1976.                                         ((real *)hybridIn[0])[i] = tmp0 + tmp1;
  1977.                                         ((real *)hybridIn[1])[i] = tmp0 - tmp1;
  1978.                                 }
  1979.                         }
  1980.  
  1981.                         if(i_stereo) III_i_stereo(hybridIn,scalefacs[1],gr_info,sfreq,ms_stereo,fr->lsf);
  1982.  
  1983.                         if(ms_stereo || i_stereo || (single == SINGLE_MIX) )
  1984.                         {
  1985.                                 if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb)
  1986.                                 sideinfo.ch[0].gr[gr].maxb = gr_info->maxb;
  1987.                                 else
  1988.                                 gr_info->maxb = sideinfo.ch[0].gr[gr].maxb;
  1989.                         }
  1990.  
  1991.                         switch(single)
  1992.                         {
  1993.                                 case SINGLE_MIX:
  1994.                                 {
  1995.                                         register int i;
  1996.                                         register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
  1997.                                         for(i=0;i<SSLIMIT*(int)gr_info->maxb;i++,in0++)
  1998.                                         *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */
  1999.                                 }
  2000.                                 break;
  2001.                                 case SINGLE_RIGHT:
  2002.                                 {
  2003.                                         register int i;
  2004.                                         register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
  2005.                                         for(i=0;i<SSLIMIT*(int)gr_info->maxb;i++)
  2006.                                         *in0++ = *in1++;
  2007.                                 }
  2008.                                 break;
  2009.                         }
  2010.                 }
  2011.  
  2012.                 for(ch=0;ch<stereo1;ch++)
  2013.                 {
  2014.                         struct gr_info_s *gr_info = &(sideinfo.ch[ch].gr[gr]);
  2015.                         III_antialias(hybridIn[ch],gr_info);
  2016.                         III_hybrid(hybridIn[ch], hybridOut[ch], ch,gr_info, fr);
  2017.                 }
  2018.  
  2019. #ifdef OPT_I486
  2020.                 if(single != SINGLE_STEREO || fr->af.encoding != MPG123_ENC_SIGNED_16 || fr->down_sample != 0)
  2021.                 {
  2022. #endif
  2023.                 for(ss=0;ss<SSLIMIT;ss++)
  2024.                 {
  2025.                         if(single != SINGLE_STEREO)
  2026.                         clip += (fr->synth_mono)(hybridOut[0][ss], fr);
  2027.                         else
  2028.                         clip += (fr->synth_stereo)(hybridOut[0][ss], hybridOut[1][ss], fr);
  2029.  
  2030.                 }
  2031. #ifdef OPT_I486
  2032.                 } else
  2033.                 {
  2034.                         /* Only stereo, 16 bits benefit from the 486 optimization. */
  2035.                         ss=0;
  2036.                         while(ss < SSLIMIT)
  2037.                         {
  2038.                                 int n;
  2039.                                 n=(fr->buffer.size - fr->buffer.fill) / (2*2*32);
  2040.                                 if(n > (SSLIMIT-ss)) n=SSLIMIT-ss;
  2041.  
  2042.                                 /* Clip counting makes no sense with this function. */
  2043.                                 absynth_1to1_i486(hybridOut[0][ss], 0, fr, n);
  2044.                                 absynth_1to1_i486(hybridOut[1][ss], 1, fr, n);
  2045.                                 ss+=n;
  2046.                                 fr->buffer.fill+=(2*2*32)*n;
  2047.                         }
  2048.                 }
  2049. #endif
  2050.         }
  2051.  
  2052.         return clip;
  2053. }
  2054.