Subversion Repositories Kolibri OS

Rev

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

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