home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / Audio / MPEG / Layer3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  33.3 KB  |  1,338 lines

  1. #include <crtdbg.h>
  2. #include <string.h>
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6.  
  7. #include "AMPDecoder.h"
  8.  
  9. ////////////////////////////////////////////////////////////////////////////
  10.  
  11. //#define RDTSC_PROFILE
  12.  
  13. #ifdef RDTSC_PROFILE
  14.  
  15.     #include <windows.h>
  16.  
  17.     static long p_lasttime;
  18.     static long p_frames=0;
  19.     static __int64 p_total=0;
  20.     static __int64 p_header=0;
  21.     static __int64 p_scalefac=0;
  22.     static __int64 p_huffdec=0;
  23.     static __int64 p_dequan=0;
  24.     static __int64 p_stereo=0;
  25.     static __int64 p_antialias=0;
  26.     static __int64 p_hybrid=0;
  27.     static __int64 p_polyphase1=0;
  28.     static __int64 p_polyphase2=0;
  29.  
  30.     static void __inline profile_set(int) {
  31.         __asm {
  32.             rdtsc
  33.             mov        p_lasttime,eax
  34.         };
  35.     }
  36.  
  37.     static void __inline profile_add(__int64& counter) {
  38.         long diff;
  39.  
  40.         __asm {
  41.             rdtsc
  42.             sub        eax,p_lasttime
  43.             mov        diff,eax
  44.         }
  45.  
  46.         counter += diff;
  47.         p_total += diff;
  48.  
  49.         __asm {
  50.             rdtsc
  51.             mov        p_lasttime,eax
  52.  
  53.         }
  54.     }
  55. #else
  56.  
  57.     #define profile_set(x)
  58.     #define profile_add(x)
  59.  
  60. #endif
  61.  
  62. ////////////////////////////////////////////////////////////////////////////
  63.  
  64. int AMPDecoder::L3_GetBits(int bits) {
  65.     unsigned char *ptr = l3bitptr + (l3bitidx>>3);
  66.     unsigned long bitheap;
  67.  
  68.     bitheap = ((unsigned long)ptr[0] << 24)
  69.             + ((unsigned long)ptr[1] << 16)
  70.             + ((unsigned long)ptr[2] << 8)
  71.             + (unsigned long)ptr[3];
  72.  
  73.     bitheap <<= l3bitidx & 7;
  74.     bitheap >>= 32-bits;
  75.  
  76.     l3bitidx += bits;
  77.  
  78.     return (int)bitheap;
  79. }
  80.  
  81. void AMPDecoder::L3_GetSideInfo() {
  82.     unsigned char heap[32];
  83.     int gr, ch, i;
  84.     
  85.     l3bitptr = heap;
  86.     l3bitidx = 0;
  87.  
  88.     if (sideinfo_size != pSource->read(heap, sideinfo_size))
  89.         throw (int)ERR_EOF;
  90.  
  91.     // process side info
  92.  
  93.     if (is_mpeg2) {
  94.         sideinfo.main_data_begin        = L3_GetBits(8);
  95.         if (mode == MODE_MONO)
  96.             sideinfo.private_bits        = L3_GetBits(1);
  97.         else
  98.             sideinfo.private_bits        = L3_GetBits(2);
  99.     } else {
  100.         sideinfo.main_data_begin        = L3_GetBits(9);
  101.         if (mode == MODE_MONO)
  102.             sideinfo.private_bits        = L3_GetBits(5);
  103.         else
  104.             sideinfo.private_bits        = L3_GetBits(3);
  105.  
  106.         // read in scale factor selectors
  107.  
  108.         for(ch=0; ch<channels; ch++)
  109.             for(i=0; i<4; i++)
  110.                 sideinfo.ch[ch].scfsi[i] = L3_GetBits(1);
  111.  
  112.     }
  113.  
  114.     // read in data per region/channel
  115.  
  116.     for(gr=0; gr<(is_mpeg2?1:2); gr++) {
  117.         for(ch=0; ch<channels; ch++) {
  118.             sideinfo.ch[ch].gr[gr].part2_3_length = L3_GetBits(12);
  119.             sideinfo.ch[ch].gr[gr].big_values = L3_GetBits(9);
  120.             sideinfo.ch[ch].gr[gr].global_gain = L3_GetBits(8);
  121.  
  122.             if (is_mpeg2)
  123.                 sideinfo.ch[ch].gr[gr].scalefac_compress = L3_GetBits(9);
  124.             else
  125.                 sideinfo.ch[ch].gr[gr].scalefac_compress = L3_GetBits(4);
  126.  
  127.             sideinfo.ch[ch].gr[gr].window_switching_flag = L3_GetBits(1);
  128.             if (sideinfo.ch[ch].gr[gr].window_switching_flag) {
  129.                 sideinfo.ch[ch].gr[gr].block_type = L3_GetBits(2);
  130.                 sideinfo.ch[ch].gr[gr].mixed_block_flag = L3_GetBits(1);
  131.                 for (i=0; i<2; i++)
  132.                     sideinfo.ch[ch].gr[gr].table_select[i] = L3_GetBits(5);
  133.                 for (i=0; i<3; i++)
  134.                     sideinfo.ch[ch].gr[gr].subblock_gain[i] = L3_GetBits(3);
  135.                
  136.                 /* Set region_count parameters since they are implicit in this case. */
  137.             
  138.                 if (sideinfo.ch[ch].gr[gr].block_type == 0)
  139.                     throw (int)ERR_INTERNAL;    // bad?
  140.                 else if (sideinfo.ch[ch].gr[gr].block_type == 2
  141.                      && sideinfo.ch[ch].gr[gr].mixed_block_flag == 0)
  142.                     sideinfo.ch[ch].gr[gr].region0_count = 8; /* MI 9; */
  143.                 else
  144.                     sideinfo.ch[ch].gr[gr].region0_count = 7; /* MI 8; */
  145.  
  146.                 sideinfo.ch[ch].gr[gr].region1_count = 20 -
  147.                         sideinfo.ch[ch].gr[gr].region0_count;
  148.             } else {
  149.                 for (i=0; i<3; i++)
  150.                     sideinfo.ch[ch].gr[gr].table_select[i] = L3_GetBits(5);
  151.                 sideinfo.ch[ch].gr[gr].region0_count = L3_GetBits(4);
  152.                 sideinfo.ch[ch].gr[gr].region1_count = L3_GetBits(3);
  153.                 sideinfo.ch[ch].gr[gr].block_type = 0;
  154.             }
  155.  
  156.             if (!is_mpeg2)
  157.                 sideinfo.ch[ch].gr[gr].preflag = L3_GetBits(1);
  158.  
  159.             sideinfo.ch[ch].gr[gr].scalefac_scale = L3_GetBits(1);
  160.             sideinfo.ch[ch].gr[gr].count1table_select = L3_GetBits(1);
  161.         }
  162.     }
  163. }
  164.  
  165. void AMPDecoder::L3_PrereadFrame() {
  166.     fillbits(frame_size);
  167. }
  168.  
  169. bool AMPDecoder::L3_DecodeFrame() {
  170.     int gr, ch, sb, ss;
  171.  
  172.     profile_set(0);
  173.  
  174.     // bring in new bits
  175.  
  176.     fillbits(frame_size);
  177.  
  178.     // rewind read point back to beginning of data for this frame
  179.  
  180. //_RPT2(0,"Layer III: added %ld bytes, rewind to %ld\n", frame_size, sideinfo.main_data_begin + frame_size);
  181.  
  182.     rewind(sideinfo.main_data_begin + frame_size);
  183.  
  184.     profile_add(p_header);
  185.  
  186.     // process granules.
  187.     //
  188.     // 2 for MPEG-1, 1 for MPEG-2
  189.  
  190.     for (gr=0;gr<(is_mpeg2 ? 1 : 2);gr++) {
  191.         float lr[2][SBLIMIT][SSLIMIT],ro[SBLIMIT][SSLIMIT];
  192.         III_scalefac_t scalefac;
  193.         int sample_end[2];
  194.         int new_end;
  195.  
  196.         // process each channel in the granule.
  197.         
  198.         for (ch=0; ch<channels; ch++) {
  199.             long int is[SBLIMIT*SSLIMIT];   /* Quantized samples. */
  200.             int part2_start;
  201.             part2_start = tellbits();
  202.             int limit;
  203.  
  204.             // decode scale factors
  205.  
  206.             if (is_mpeg2)
  207.                 L3_GetScaleFactors2(&scalefac, ch);
  208.             else
  209.                 L3_GetScaleFactors1(&scalefac, gr, ch);
  210.  
  211.             profile_add(p_scalefac);
  212.  
  213.             // decode samples
  214.  
  215.             limit = L3_HuffmanDecode(is, ch, gr, part2_start);
  216.  
  217.             profile_add(p_huffdec);
  218.  
  219.             // dequantize samples
  220.  
  221.             sample_end[ch] = L3_DequantizeSample(is, (float *)lr[ch], &scalefac[ch],
  222.                 &sideinfo.ch[ch].gr[gr], ch, limit);
  223.  
  224.             profile_add(p_dequan);
  225.         }
  226.  
  227.         // process joint stereo in lr
  228.  
  229.         new_end = L3_Stereo((float (*)[576])lr, &scalefac, &sideinfo.ch[0].gr[gr], sample_end[0], sample_end[1]);
  230.  
  231.         if (new_end > 0)
  232.             sample_end[0] = sample_end[1] = new_end;
  233.  
  234.         profile_add(p_stereo);
  235.  
  236.         float polyPhaseIn[2][SSLIMIT][SBLIMIT];
  237.             
  238.         for (ch=0; ch<channels; ch++) {
  239.             
  240.             // reorder samples; use 'hybridOut' as our out here, to reuse memory
  241.  
  242.             L3_Reorder((float *)lr[ch],(float *)ro,&sideinfo.ch[ch].gr[gr]);
  243.  
  244.             // antialiasing
  245.  
  246.             L3_Antialias(lr[ch], /* Antialias butterflies. */
  247.                 &sideinfo.ch[ch].gr[gr]);
  248.  
  249.             profile_add(p_antialias);
  250.  
  251.             // Hybrid synthesis.
  252.  
  253.             L3_Hybrid(lr[ch], polyPhaseIn[ch], ch, &sideinfo.ch[ch].gr[gr], sample_end[ch]);
  254.  
  255.             profile_add(p_hybrid);
  256.         }
  257.  
  258.         if (mode == MODE_MONO)
  259.             for(ss=0; ss<18; ss++)
  260.                 polyphase(polyPhaseIn[0][ss], NULL, psDest + 32*ss + 576*gr, !!(ss&1));
  261.         else
  262.             for(ss=0; ss<18; ss++)
  263.                 polyphase(polyPhaseIn[0][ss], polyPhaseIn[1][ss], psDest + 64*ss + 1152*gr, !!(ss&1));
  264.  
  265.         profile_add(p_polyphase2);
  266.     }
  267.  
  268. #ifdef RDTSC_PROFILE
  269.  
  270.     if (!(++p_frames & 127)) {
  271.         static char buf[256];
  272.  
  273.         sprintf(buf, "%d frames: total %I64d, header %d%%, scalefac %d%%, huffdec %d%%, dequan %d%%, stereo %d%%, antialias %d%%, hybrid %d%%, poly %d%%/%d%%\n"
  274.                 ,p_frames
  275.                 ,p_total
  276.                 ,(long)((p_header*100)/p_total)
  277.                 ,(long)((p_scalefac*100)/p_total)
  278.                 ,(long)((p_huffdec*100)/p_total)
  279.                 ,(long)((p_dequan*100)/p_total)
  280.                 ,(long)((p_stereo*100)/p_total)
  281.                 ,(long)((p_antialias*100)/p_total)
  282.                 ,(long)((p_hybrid*100)/p_total)
  283.                 ,(long)((p_polyphase1*100)/p_total)
  284.                 ,(long)((p_polyphase2*100)/p_total)
  285.                 );
  286.         OutputDebugString(buf);
  287.     }
  288. #endif
  289.  
  290.     // set sample count
  291.  
  292.     lSampleCount = 1152;
  293.     if (is_mpeg2)
  294.         lSampleCount = 576;
  295.  
  296.     if (channels>1)
  297.         lSampleCount <<=1;
  298.  
  299. //    _RPT2(0,"Layer III: terminated with %ld bits left (%ld bytes)\n", tellbits(), tellbits()/8);
  300.              
  301.     return true;
  302. }
  303.  
  304. struct {
  305.    int l[5];
  306.    int s[3];} sfbtable = {{0, 6, 11, 16, 21},
  307.                           {0, 6, 12}};
  308.                          
  309. const int slen1[2][16] = {
  310.     {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
  311.     {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
  312. };
  313.  
  314. void AMPDecoder::L3_GetScaleFactors1(III_scalefac_t *scalefac,
  315.                             int gr,
  316.                             int ch) {
  317.  
  318.     int sfb, i, window;
  319.     struct gr_info_s *gr_info = &sideinfo.ch[ch].gr[gr];
  320.  
  321.     if (gr_info->window_switching_flag && (gr_info->block_type == 2)) { 
  322.         if (gr_info->mixed_block_flag) { /* MIXED */ /* NEW - ag 11/25 */
  323.  
  324.             for (sfb = 0; sfb < 8; sfb++) {
  325.                 (*scalefac)[ch].l[sfb] = getbits(slen1[0][gr_info->scalefac_compress]);
  326.             }
  327.  
  328.             for (sfb = 3; sfb < 6; sfb++)
  329.                 for (window=0; window<3; window++) {
  330.                     (*scalefac)[ch].s[window][sfb] = getbits(slen1[0][gr_info->scalefac_compress]);
  331.                 }
  332.  
  333.             for (sfb = 6; sfb < 12; sfb++)
  334.                 for (window=0; window<3; window++) {
  335.                     (*scalefac)[ch].s[window][sfb] = getbits(slen1[1][gr_info->scalefac_compress]);
  336.                 }
  337.                     
  338.             for (window=0; window<3; window++)
  339.                 (*scalefac)[ch].s[window][12] = 0;
  340.  
  341.         } else {  /* SHORT*/
  342.  
  343.             for (i=0; i<2; i++) 
  344.                 for (sfb = sfbtable.s[i]; sfb < sfbtable.s[i+1]; sfb++)
  345.                     for (window=0; window<3; window++) {
  346.                         (*scalefac)[ch].s[window][sfb] = getbits(slen1[i][gr_info->scalefac_compress]);
  347.                     }
  348.  
  349.             for (window=0; window<3; window++)
  350.                 (*scalefac)[ch].s[window][12] = 0;
  351.         }
  352.     } else {   /* LONG types 0,1,3 */
  353.  
  354.         for (i=0; i<2; i++) {
  355.             if ((sideinfo.ch[ch].scfsi[i] == 0) || (gr == 0))
  356.                 for (sfb = sfbtable.l[i]; sfb < sfbtable.l[i+1]; sfb++) {
  357.                     (*scalefac)[ch].l[sfb] = getbits(slen1[0][gr_info->scalefac_compress]);
  358.                 }
  359.         }
  360.         for (i=2; i<4; i++) {
  361.             if ((sideinfo.ch[ch].scfsi[i] == 0) || (gr == 0))
  362.                 for (sfb = sfbtable.l[i]; sfb < sfbtable.l[i+1]; sfb++) {
  363.                     (*scalefac)[ch].l[sfb] = getbits(slen1[1][gr_info->scalefac_compress]);
  364.                 }
  365.         }
  366.         (*scalefac)[ch].l[21] = 0; 
  367.         (*scalefac)[ch].l[22] = 0; 
  368.     }
  369.  
  370. }
  371.  
  372. void AMPDecoder::L3_GetScaleFactors2(III_scalefac_t *scalefac, int ch) {
  373.     static const int sfbblockindex[6][3][4]={
  374.         {{ 6, 5, 5, 5},{ 9, 9, 9, 9},{ 6, 9, 9, 9}},
  375.         {{ 6, 5, 7, 3},{ 9, 9,12, 6},{ 6, 9,12, 6}},
  376.         {{11,10, 0, 0},{18,18, 0, 0},{15,18, 0, 0}},
  377.         {{ 7, 7, 7, 0},{12,12,12, 0},{ 6,15,12, 0}},
  378.         {{ 6, 6, 6, 3},{12, 9, 9, 6},{ 6,12, 9, 6}},
  379.         {{ 8, 8, 5, 0},{15,12, 9, 0},{ 6,18, 9, 0}}
  380.     };
  381.  
  382.     int sb[54];
  383.     struct gr_info_s *gi=&(sideinfo.ch[ch].gr[0]);
  384.     struct III_scalefac1_t *sf=(&(*scalefac)[ch]);
  385.  
  386.     {
  387.         int blocktypenumber,sc;
  388.         int blocknumber;
  389.         
  390.         if(gi->block_type==2)blocktypenumber=1+gi->mixed_block_flag;
  391.         else blocktypenumber=0;
  392.         
  393.         sc=gi->scalefac_compress;
  394.         if(!((mode_ext==1 || mode_ext==3) && (ch==1)))
  395.         {
  396.             if(sc<400) {
  397.                 slen[0]=(sc>>4)/5;
  398.                 slen[1]=(sc>>4)%5;
  399.                 slen[2]=(sc%16)>>2;
  400.                 slen[3]=(sc%4);
  401.                 gi->preflag=0;
  402.                 blocknumber=0;
  403.             } else if(sc<500) {
  404.                 sc-=400;
  405.                 slen[0]=(sc>>2)/5;
  406.                 slen[1]=(sc>>2)%5;
  407.                 slen[2]=sc%4;
  408.                 slen[3]=0;
  409.                 gi->preflag=0;
  410.                 blocknumber=1;
  411.             } else {
  412.                 sc-=500;
  413.                 slen[0]=sc/3;
  414.                 slen[1]=sc%3;
  415.                 slen[2]=0;
  416.                 slen[3]=0;
  417.                 gi->preflag=1;
  418.                 blocknumber=2;
  419.             }
  420.         } else {
  421.             sc>>=1;
  422.             if(sc<180) {
  423.                 slen[0]=sc/36;
  424.                 slen[1]=(sc%36)/6;
  425.                 slen[2]=(sc%36)%6;
  426.                 slen[3]=0;
  427.                 gi->preflag=0;
  428.                 blocknumber=3;
  429.             } else if(sc<244) {
  430.                 sc-=180;
  431.                 slen[0]=(sc%64)>>4;
  432.                 slen[1]=(sc%16)>>2;
  433.                 slen[2]=sc%4;
  434.                 slen[3]=0;
  435.                 gi->preflag=0;
  436.                 blocknumber=4;
  437.             } else {
  438.                 sc-=244;
  439.                 slen[0]=sc/3;
  440.                 slen[1]=sc%3;
  441.                 slen[2]=
  442.                     slen[3]=0;
  443.                 gi->preflag=0;
  444.                 blocknumber=5;
  445.             }
  446.         }
  447.         
  448.         {
  449.             int i,j,k;
  450.             const int *si;
  451.             
  452.             scale_block_indexes=sfbblockindex[blocknumber][blocktypenumber];
  453.             for(i=0;i<45;i++)sb[i]=0;
  454.             
  455.             for(k=i=0;i<4;i++)
  456.                 for(j=0;j<scale_block_indexes[i];j++,k++)
  457.                     if(slen[i]==0)sb[k]=0;
  458.                     else sb[k]=getbits(slen[i]);
  459.         }
  460.     }
  461.     
  462.     
  463.     {
  464.         int sfb,window;
  465.         int k=0;
  466.         
  467.         if(gi->window_switching_flag && (gi->block_type==2))
  468.         {
  469.             if(gi->mixed_block_flag)
  470.             {
  471.                 for(sfb=0;sfb<8;sfb++)sf->l[sfb]=sb[k++];
  472.                 sfb=3;
  473.             }
  474.             else sfb=0;
  475.             
  476.             for(;sfb<12;sfb++)
  477.                 for(window=0;window<3;window++)
  478.                     sf->s[window][sfb]=sb[k++];
  479.                 
  480.                 sf->s[0][12]=sf->s[1][12]=sf->s[2][12]=0;
  481.         }
  482.         else
  483.         {
  484.             for(sfb=0;sfb<21;sfb++)
  485.                 sf->l[sfb]=sb[k++];
  486.             sf->l[21]=sf->l[22]=0;
  487.         }
  488.     }
  489. }
  490.  
  491. static struct  {
  492.     int l[23];
  493.     int s[14];} sfBandIndex[2][3] =   
  494.     {
  495.         {
  496.             {{0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576},
  497.              {0,4,8,12,16,22,30,40,52,66,84,106,136,192}},
  498.             {{0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576},
  499.              {0,4,8,12,16,22,28,38,50,64,80,100,126,192}},
  500.             {{0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
  501.              {0,4,8,12,16,22,30,42,58,78,104,138,180,192}},
  502.         },
  503.         {
  504.             {{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  505.              {0,4,8,12,18,24,32,42,56,74,100,132,174,192}},
  506.             {{0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
  507.              {0,4,8,12,18,26,36,48,62,80,104,136,180,192}},
  508.             {{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  509.              {0,4,8,12,18,26,36,48,62,80,104,134,174,192}},
  510.         }
  511.    };
  512.  
  513. int AMPDecoder::L3_HuffmanDecode(long int is[SBLIMIT*SSLIMIT],
  514.                         int ch,
  515.                         int gr,
  516.                         int part2_start) {
  517.  
  518.     int i, x, y;
  519.     int v, w;
  520.     int h;
  521.     int region1Start;
  522.     int region2Start;
  523.     int bt = sideinfo.ch[ch].gr[gr].window_switching_flag && (sideinfo.ch[ch].gr[gr].block_type == 2);
  524.         
  525.     /* Find region boundary for short block case. */
  526.     
  527.     if ( (sideinfo.ch[ch].gr[gr].window_switching_flag) && 
  528.         (sideinfo.ch[ch].gr[gr].block_type == 2) ) { 
  529.         
  530.         /* Region2. */
  531.         
  532.         region1Start = 36;  /* sfb[9/3]*3=36 */
  533.         region2Start = 576; /* No Region2 for short block case. */
  534.     }
  535.     
  536.     
  537.     else {          /* Find region boundary for long block case. */
  538.         
  539.         region1Start = sfBandIndex[is_mpeg2][sr_index].l[sideinfo.ch[ch].gr[gr].region0_count + 1]; /* MI */
  540.         region2Start = sfBandIndex[is_mpeg2][sr_index].l[sideinfo.ch[ch].gr[gr].region0_count + sideinfo.ch[ch].gr[gr].region1_count + 2]; /* MI */
  541.     }
  542.     
  543. //_RPT1(0,"%d bits left (bigvalues)\n", tellbits());
  544.     
  545.     /* Read bigvalues area. */
  546.  
  547.     if (region1Start > sideinfo.ch[ch].gr[gr].big_values*2)
  548.         region1Start = sideinfo.ch[ch].gr[gr].big_values*2;
  549.  
  550.     if (region2Start > sideinfo.ch[ch].gr[gr].big_values*2)
  551.         region2Start = sideinfo.ch[ch].gr[gr].big_values*2;
  552.  
  553.     h = sideinfo.ch[ch].gr[gr].table_select[0];
  554.  
  555.     if (h)
  556.         L3_GetHuffmanBig(h, &is[0], region1Start);
  557.     else
  558.         memset(is, 0, sizeof(is[0])*region1Start);
  559.  
  560.     i = region1Start;
  561.  
  562.     if (i < region2Start) {
  563.         h = sideinfo.ch[ch].gr[gr].table_select[1];
  564.  
  565.         if (h)
  566.             L3_GetHuffmanBig(h, &is[i], region2Start - i);
  567.         else
  568.             memset(is+i, 0, sizeof(is[0])*(region2Start-i));
  569.     }
  570.  
  571.     i = region2Start;
  572.  
  573.     if (i < sideinfo.ch[ch].gr[gr].big_values*2) {
  574.         h = sideinfo.ch[ch].gr[gr].table_select[2];
  575.  
  576.         if (h)
  577.             L3_GetHuffmanBig(h, &is[i], sideinfo.ch[ch].gr[gr].big_values*2 - i);
  578.         else
  579.             memset(is+i, 0, sizeof(is[0])*(sideinfo.ch[ch].gr[gr].big_values*2-i));
  580.     }
  581.  
  582.     i = sideinfo.ch[ch].gr[gr].big_values*2;
  583.  
  584.     /* Read count1 area. */
  585.  
  586.     if (sideinfo.ch[ch].gr[gr].count1table_select) {
  587.         int count;
  588.  
  589.         // fixed 4-bit table
  590.  
  591.         i = L3_GetHuffmanCount1_33(&is[i], i, part2_start - sideinfo.ch[ch].gr[gr].part2_3_length);
  592.  
  593.     } else {
  594.         // vbr huffman table
  595.  
  596.         i = L3_GetHuffmanCount1_32(&is[i], i, part2_start - sideinfo.ch[ch].gr[gr].part2_3_length);
  597.     }
  598.  
  599. //    _RPT2(0,"%d big values, %d small values\n", sideinfo.ch[ch].gr[gr].big_values*2, i - sideinfo.ch[ch].gr[gr].big_values*2);
  600.  
  601.  
  602.     if (tellbits() < part2_start - sideinfo.ch[ch].gr[gr].part2_3_length)
  603.         i -= 4;
  604.  
  605. //_RPT2(0,"%d bits left, attempting rewind to %d\n", tellbits(), part2_start - sideinfo.ch[ch].gr[gr].part2_3_length);
  606.  
  607.     rewindbits(part2_start - sideinfo.ch[ch].gr[gr].part2_3_length);
  608.     
  609.     /* Zero out rest. */
  610.  
  611.     return i;
  612. }
  613.  
  614. static const int pretab[22] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0};
  615.  
  616. // pow43[15 + i] = abs(i) ^ (4/3) * sign(i)
  617.  
  618. static const double pow43[32]={
  619.     -36.99318111495705,
  620.     -33.74199169845322,
  621.     -30.56735094036985,
  622.     -27.47314182127996,
  623.     -24.46378099626247,
  624.     -21.54434690031884,
  625.     -18.72075440746714,
  626.     -16.00000000000000,
  627.     -13.39051827940672,
  628.     -10.90272355699284,
  629.     -8.549879733383484,
  630.     -6.349604207872798,
  631.     -4.326748710922225,
  632.     -2.519842099789746,
  633.     -1.000000000000000,
  634.      0,
  635.      1.000000000000000,
  636.      2.519842099789746,
  637.      4.326748710922225,
  638.      6.349604207872798,
  639.      8.549879733383484,
  640.      10.90272355699284,
  641.      13.39051827940672,
  642.      16.00000000000000,
  643.      18.72075440746714,
  644.      21.54434690031884,
  645.      24.46378099626247,
  646.      27.47314182127996,
  647.      30.56735094036985,
  648.      33.74199169845322,
  649.      36.99318111495705,
  650.      40.31747359663594,
  651. };
  652.  
  653. int AMPDecoder::L3_DequantizeSample(long int is[SBLIMIT*SSLIMIT],
  654.                             float xr[SBLIMIT*SSLIMIT],
  655.                             const III_scalefac1_t *scaleptr,
  656.                             struct gr_info_s *gr_info,
  657.                             int ch, int limit) {
  658.  
  659.     int ind,cb=0,sfreq=sr_index;
  660.     int stereo = channels;
  661.     int next_cb_boundary, next_cb2_boundary, cb_width, sign;
  662.     int last_nonzero = -1;
  663.  
  664.  
  665.     // We optimize all the pow() out of this routine.
  666.     //
  667.     //    gr_info->global_gain:        0-255 (8 bits)
  668.     //    gr_info->scalefac_scale:    0 or 1
  669.     //    gr_info->preflag:            0 or 1
  670.     //    gr_info->subblock_gain:        0-7 (3 bits)
  671.     //    scalefac[ch].s[]:            0-15 (4 bits max)
  672.     //    scalefac[ch].l[]:            0-15 (4 bits max)
  673.     //    pretab[]:                    0-3
  674.     //
  675.     //    Let f(x) = 2^(0.25*x), where f(a)f(b) = f(a+b).
  676.     //
  677.     //    xr0 = f(gr_info->global_gain - 210);
  678.     //        range: f(-210) to f(45).
  679.     //
  680.     // short blocks:
  681.     //
  682.     //        xr = xr0 * f(-8.0 * gr_info->subblock_gain[(ind - cb_begin)/cb_width]
  683.     //                -2.0 * (1.0+gr_info->scalefac_scale)
  684.     //            * (*scalefac)[ch].s[(ind - cb_begin)/cb_width][cb]);
  685.     //
  686.     //        range: f(-116) to f(0).
  687.     //
  688.     // LONG block types 0,1,3 & 1st 2 subbands of switched blocks:
  689.     //
  690.     //        xr = xr0 * f(-2.0 * (1.0+gr_info->scalefac_scale)
  691.     //            * ((*scalefac)[ch].l[cb]
  692.     //            + gr_info->preflag * pretab[cb]));
  693.     //
  694.     //        range: f(-72) to f(0).
  695.     //
  696.     //    CONCLUSION:
  697.     //
  698.     //        Necessary range is f(-326) to f(45).
  699.     //
  700.     //    We negate the sign, so it's really f(-45) to f(326).  Scaling
  701.     //    to 16-bit PCM is also done here.
  702.  
  703.     static float pow2tbl[326+45+1];
  704.     static bool init_flag = true;
  705.     double xr_val;
  706.     int        scalefac_shift = 1+gr_info->scalefac_scale;
  707.     int        preflag_mask = gr_info->preflag ? ~0 : 0;
  708.     int        cb_low, cb_high;
  709.  
  710.     if (init_flag) {
  711.         init_flag = false;
  712.  
  713.         for(int i=-45; i<=326; i++)
  714.             pow2tbl[i+45] = pow(2.0, -0.25 * i) * 16384.0;//32768.0;
  715.     }
  716.  
  717.  
  718.  
  719.     // global_gain ranges from 0-255 (8 bits)
  720.  
  721.     float *xr0_ptr = pow2tbl + 45 + 210 - (int)gr_info->global_gain;
  722.     
  723.     /* choose correct scalefactor band per block type, initalize boundary */
  724.     
  725.     bool    fShortBlock        = gr_info->window_switching_flag && (gr_info->block_type == 2);
  726.     int        iScaleThreshold = gr_info->mixed_block_flag ? 2*SSLIMIT : 0;
  727.  
  728.     next_cb_boundary = 0;
  729.     cb = -1;
  730.         
  731.     /* apply formula per block type */
  732.  
  733.     for (ind=0 ; ind < limit ; ind++) {
  734.  
  735.         if ( ind == next_cb_boundary)  { /* Adjust critical band boundary */
  736.  
  737.             if (fShortBlock) {
  738.  
  739.                 if (gr_info->mixed_block_flag) {
  740.                     if (ind == sfBandIndex[is_mpeg2][sfreq].l[8])  {
  741.                         next_cb_boundary=sfBandIndex[is_mpeg2][sfreq].s[4]*3; 
  742.                         cb = 3;
  743.                         cb_width = sfBandIndex[is_mpeg2][sfreq].s[cb+1] - sfBandIndex[is_mpeg2][sfreq].s[cb];
  744.                         
  745.                     } else if (ind < sfBandIndex[is_mpeg2][sfreq].l[8]) {
  746.  
  747.                         next_cb_boundary = sfBandIndex[is_mpeg2][sfreq].l[(++cb)+1];
  748.  
  749.                     } else {
  750.                         next_cb_boundary = sfBandIndex[is_mpeg2][sfreq].s[(++cb)+1]*3;
  751.                         cb_width = sfBandIndex[is_mpeg2][sfreq].s[cb+1] - sfBandIndex[is_mpeg2][sfreq].s[cb];
  752.                     }   
  753.                 } else {
  754.                     next_cb_boundary = sfBandIndex[is_mpeg2][sfreq].s[(++cb)+1]*3;
  755.                     cb_width = sfBandIndex[is_mpeg2][sfreq].s[cb+1] - sfBandIndex[is_mpeg2][sfreq].s[cb];
  756.                 } 
  757.  
  758.             } else /* long blocks */
  759.                 next_cb_boundary = sfBandIndex[is_mpeg2][sfreq].l[(++cb)+1];
  760.  
  761.             // block type 2?
  762.  
  763.             if (fShortBlock && ind >= iScaleThreshold) {
  764.                 xr_val = xr0_ptr[8*gr_info->subblock_gain[0] + (scaleptr->s[0][cb] << scalefac_shift)];
  765.                 cb_high = 0;
  766.                 next_cb2_boundary = ind + cb_width;
  767.             } else {
  768.  
  769.                 // long block, or first two subbands of switched blocks
  770.  
  771.                 xr_val = xr0_ptr[(scaleptr->l[cb] + (pretab[cb] & preflag_mask)) << scalefac_shift];
  772.                 next_cb2_boundary = -1;
  773.             }
  774.         }
  775.  
  776.         // Do we need to switch the scaling factor for short blocks?
  777.  
  778.         if (ind == next_cb2_boundary) {
  779.             ++cb_high;
  780.             xr_val = xr0_ptr[8*gr_info->subblock_gain[cb_high] + (scaleptr->s[cb_high][cb] << scalefac_shift)];
  781.             next_cb2_boundary += cb_width;
  782.         }
  783.         
  784.         /* Scale quantized value. */
  785.  
  786.         if (is[ind]) {
  787.             int ind2 = 15 + is[ind];
  788.             double y;
  789.  
  790.             if (ind2 & 0xffffffe0) {
  791.  
  792.                 if (ind2<0)
  793.                     y = -pow((double)-is[ind], (double)(4.0/3.0));
  794.                 else
  795.                     y = pow((double)is[ind], (double)(4.0/3.0));
  796.  
  797.             } else {
  798.  
  799.                 y = pow43[ind2];
  800.  
  801.             }
  802.  
  803.             xr[ind] = xr_val * y;
  804.  
  805.             last_nonzero = ind;
  806.         } else {
  807.             xr[ind] = 0.0;
  808.         }
  809.     }
  810.  
  811.     while(ind<SBLIMIT*SSLIMIT)
  812.         xr[ind++] = 0.0;
  813.  
  814.     return last_nonzero+1;
  815. }
  816.  
  817. // qbasic is the ultimate table generator
  818. //
  819. //    Intensity stereo for MPEG-1:
  820. //
  821. //    is_ratio[i][0] = 1/(1+y)
  822. //    is_ratio[i][1] = y/(1+y)
  823. //
  824. //    where y=tan(i * (3.1415926535897932 / 12));
  825. //
  826. //    Alternatively (thank you FreeAmp, blah Fraunhofer):
  827. //
  828. //    is_ratio[i][0] = s/(s+c)
  829. //    is_ratio[i][1] = c/(s+c)
  830. //
  831. //    where    s = sin(i * pi/12)
  832. //            c = cos(i * pi/12)
  833.  
  834. static const double is_ratio1[16][2]={
  835.  
  836. #define T(y) { (y)/(1.0+(y)), 1.0/(1.0+(y)) }
  837.  
  838.     T(0),
  839.     T( .2679491924311227),
  840.     T( .5773502691896257),
  841.     T( .9999999999999999),
  842.     T( 1.732050807568877),
  843.     T( 3.732050807568877),
  844.     T( 1.632455227761907e+16),
  845.     T(-3.732050807568879),
  846.     T(-1.732050807568878),
  847.  
  848. //    T(-1),    stupid asymptotes
  849.     { -1000000, 1000000 },
  850.  
  851.     T(-.5773502691896260),
  852.     T(-.2679491924311228),
  853.     T(-1.22514845490862e-16),
  854.     T( .2679491924311226),
  855.     T( .5773502691896256),
  856.     T( .9999999999999997),
  857.  
  858. #undef T
  859. };
  860.  
  861. // Intensity stereo for MPEG-2:
  862. //
  863. //
  864.  
  865. int AMPDecoder::L3_Stereo(
  866.                 float lr[2][SBLIMIT*SSLIMIT],
  867.                 const III_scalefac_t *scalefac,
  868.                 struct gr_info_s *gr_info,
  869.                 int nz0, int nz1) {
  870.  
  871.     int sfreq = sr_index;
  872.     int ms_stereo = (mode == MODE_JOINTSTEREO) && (mode_ext & 0x2); 
  873.     int i_stereo = (mode == MODE_JOINTSTEREO) && (mode_ext & 0x1);
  874.     int js_bound;  /* frequency line that marks the beggining of the zero part */  
  875.     int sfb,next_sfb_boundary;
  876.     int i,j,k,sb,ss,ch,is_pos[576]; 
  877.  
  878.     static float is_ratio2[2][2][64][2];
  879.     static bool is_ratio2_inited = false;
  880.  
  881.     if (!is_ratio2_inited) {
  882.         int k, n;
  883.         double t;
  884.         int intensity_scale, ms_mode, sf, sflen;
  885.         float ms_factor[2];
  886.         
  887.         
  888.         ms_factor[0] = 1.0;
  889.         ms_factor[1] = (float) sqrt(2.0);
  890.         
  891.         /* intensity stereo MPEG2 */
  892.         /* lr2[intensity_scale][ms_mode][sflen_offset+sf][left/right] */
  893.         
  894.         for (intensity_scale = 0; intensity_scale < 2; intensity_scale++)
  895.         {
  896.             t = pow(2.0, -0.25 * (1 + intensity_scale));
  897.             for (ms_mode = 0; ms_mode < 2; ms_mode++)
  898.             {
  899.                 
  900.                 n = 1;
  901.                 k = 0;
  902.                 for (sflen = 0; sflen < 6; sflen++)
  903.                 {
  904.                     for (sf = 0; sf < (n - 1); sf++, k++)
  905.                     {
  906.                         if (sf == 0)
  907.                         {
  908.                             is_ratio2[intensity_scale][ms_mode][k][0] = ms_factor[ms_mode] * 1.0f;
  909.                             is_ratio2[intensity_scale][ms_mode][k][1] = ms_factor[ms_mode] * 1.0f;
  910.                         }
  911.                         else if ((sf & 1))
  912.                         {
  913.                             is_ratio2[intensity_scale][ms_mode][k][0] =
  914.                                 (float) (ms_factor[ms_mode] * pow(t, (sf + 1) / 2));
  915.                             is_ratio2[intensity_scale][ms_mode][k][1] = ms_factor[ms_mode] * 1.0f;
  916.                         }
  917.                         else
  918.                         {
  919.                             is_ratio2[intensity_scale][ms_mode][k][0] = ms_factor[ms_mode] * 1.0f;
  920.                             is_ratio2[intensity_scale][ms_mode][k][1] =
  921.                                 (float) (ms_factor[ms_mode] * pow(t, sf / 2));
  922.                         }
  923.                     }
  924.                     
  925.                     /* illegal is_pos used to do ms processing */
  926.                     if (ms_mode == 0)
  927.                     {            /* ms_mode = 0 */
  928.                         is_ratio2[intensity_scale][ms_mode][k][0] = 1.0f;
  929.                         is_ratio2[intensity_scale][ms_mode][k][1] = 0.0f;
  930.                     }
  931.                     else
  932.                     {
  933.                         /* ms_mode = 1, in is bands is routine does ms processing */
  934.                         is_ratio2[intensity_scale][ms_mode][k][0] = 1.0f;
  935.                         is_ratio2[intensity_scale][ms_mode][k][1] = 1.0f;
  936.                     }
  937.                     k++;
  938.                     n = n + n;
  939.                 }
  940.             }
  941.         }
  942.  
  943.         is_ratio2_inited = true;
  944.     }
  945.  
  946.     if (channels<2) {
  947.         return -1;
  948.     }
  949.     
  950.     if (i_stereo) {
  951.  
  952.         /* intialization */
  953.         for ( i=0; i<576; i++ )
  954.             is_pos[i] = 7;
  955.  
  956.         if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  957.             if( gr_info->mixed_block_flag ) {
  958.                 int max_sfb = 0;
  959.                 
  960.                 for ( j=0; j<3; j++ ) {
  961.                     int sfbcnt;
  962.                     
  963.                     sfbcnt = 2;
  964.                     
  965.                     for( sfb=12; sfb >=3; sfb-- ) {
  966.                         int lines;
  967.                         
  968.                         lines = sfBandIndex[is_mpeg2][sfreq].s[sfb+1]-sfBandIndex[is_mpeg2][sfreq].s[sfb];
  969.                         i = 3*sfBandIndex[is_mpeg2][sfreq].s[sfb] + (j+1) * lines - 1;
  970.                         
  971.                         while ( lines > 0 ) {
  972.                             if ( lr[1][i] != 0.0 ) {
  973.                                 sfbcnt = sfb;
  974.                                 goto found_nonzero;
  975.                             }
  976.                             lines--;
  977.                             i--;
  978.                         }
  979.                     }
  980. found_nonzero:
  981.                     sfb = sfbcnt + 1;
  982.                     
  983.                     if ( sfb > max_sfb )
  984.                         max_sfb = sfb;
  985.                     
  986.                     while( sfb<12 )    {
  987.                         sb = sfBandIndex[is_mpeg2][sfreq].s[sfb+1]-sfBandIndex[is_mpeg2][sfreq].s[sfb];
  988.                         i = 3*sfBandIndex[is_mpeg2][sfreq].s[sfb] + j * sb;
  989.                         for ( ; sb > 0; sb--) {
  990.                             is_pos[i] = (*scalefac)[1].s[j][sfb];
  991.                             i++;
  992.                         }
  993.                         sfb++;
  994.                     }
  995.                     sb = sfBandIndex[is_mpeg2][sfreq].s[11]-sfBandIndex[is_mpeg2][sfreq].s[10];
  996.                     sfb = 3*sfBandIndex[is_mpeg2][sfreq].s[10] + j * sb;
  997.                     sb = sfBandIndex[is_mpeg2][sfreq].s[12]-sfBandIndex[is_mpeg2][sfreq].s[11];
  998.                     i = 3*sfBandIndex[is_mpeg2][sfreq].s[11] + j * sb;
  999.                     
  1000.                     for ( ; sb > 0; sb-- ) {
  1001.                         is_pos[i] = is_pos[sfb];
  1002.                         i++;
  1003.                     }
  1004.                 }
  1005.                 if ( max_sfb <= 3 ) {
  1006.                     i = 2;
  1007.                     ss = 17;
  1008.                     sb = -1;
  1009.                     while ( i >= 0 ) {
  1010.                         if ( lr[1][i*SSLIMIT+ss] != 0.0 ) {
  1011.                             sb = i*18+ss;
  1012.                             i = -1;
  1013.                         } else {
  1014.                             ss--;
  1015.                             if ( ss < 0 ) {
  1016.                                 i--;
  1017.                                 ss = 17;
  1018.                             }
  1019.                         }
  1020.                     }
  1021.                     i = 0;
  1022.  
  1023.                     while ( sfBandIndex[is_mpeg2][sfreq].l[i] <= sb )
  1024.                         i++;
  1025.  
  1026.                     sfb = i;
  1027.                     i = sfBandIndex[is_mpeg2][sfreq].l[i];
  1028.  
  1029.                     for ( ; sfb<8; sfb++ ) {
  1030.                         sb = sfBandIndex[is_mpeg2][sfreq].l[sfb+1]-sfBandIndex[is_mpeg2][sfreq].l[sfb];
  1031.                         for ( ; sb > 0; sb--) {
  1032.                             is_pos[i] = (*scalefac)[1].l[sfb];
  1033.                             i++;
  1034.                         }
  1035.                     }
  1036.                 }
  1037.             } else {
  1038.                 for ( j=0; j<3; j++ ) {
  1039.                     int sfbcnt;
  1040.                     sfbcnt = -1;
  1041.                     
  1042.                     for( sfb=12; sfb >=0; sfb-- ) {
  1043.                         int lines;
  1044.                         lines = sfBandIndex[is_mpeg2][sfreq].s[sfb+1]-sfBandIndex[is_mpeg2][sfreq].s[sfb];
  1045.                         i = 3*sfBandIndex[is_mpeg2][sfreq].s[sfb] + (j+1) * lines - 1;
  1046.                         
  1047.                         while ( lines > 0 ) {
  1048.                             if ( lr[1][i] != 0.0 ) {
  1049.                                 sfbcnt = sfb;
  1050.                                 sfb = -10;
  1051.                                 lines = -10;
  1052.                             }
  1053.                             lines--;
  1054.                             i--;
  1055.                         }
  1056.                     }
  1057.                     sfb = sfbcnt + 1;
  1058.                     
  1059.                     while( sfb<12 ) {
  1060.                         sb = sfBandIndex[is_mpeg2][sfreq].s[sfb+1]-sfBandIndex[is_mpeg2][sfreq].s[sfb];
  1061.                         i = 3*sfBandIndex[is_mpeg2][sfreq].s[sfb] + j * sb;
  1062.                         for ( ; sb > 0; sb--) {
  1063.                             is_pos[i] = (*scalefac)[1].s[j][sfb];
  1064.                                                         
  1065.                             i++;
  1066.                         }
  1067.                         sfb++;
  1068.                     }
  1069.     
  1070.                     sb = sfBandIndex[is_mpeg2][sfreq].s[11]-sfBandIndex[is_mpeg2][sfreq].s[10];
  1071.                     sfb = 3*sfBandIndex[is_mpeg2][sfreq].s[10] + j * sb;
  1072.                     sb = sfBandIndex[is_mpeg2][sfreq].s[12]-sfBandIndex[is_mpeg2][sfreq].s[11];
  1073.                     i = 3*sfBandIndex[is_mpeg2][sfreq].s[11] + j * sb;
  1074.  
  1075.                     for ( ; sb > 0; sb-- ) {
  1076.                         is_pos[i] = is_pos[sfb];
  1077.                         i++;
  1078.                     }
  1079.                 }
  1080.             }
  1081.         } else {
  1082.             i = 31;
  1083.             ss = 17;
  1084.             sb = 0;
  1085.  
  1086.             while ( i >= 0 ) {
  1087.                 if ( lr[1][i*SSLIMIT+ss] != 0.0 ) {
  1088.                     sb = i*18+ss;
  1089.                     i = -1;
  1090.                 } else {
  1091.                     ss--;
  1092.                     if ( ss < 0 ) {
  1093.                         i--;
  1094.                         ss = 17;
  1095.                     }
  1096.                 }
  1097.             }
  1098.             i = 0;
  1099.  
  1100.             while ( sfBandIndex[is_mpeg2][sfreq].l[i] <= sb )
  1101.                 i++;
  1102.             sfb = i;
  1103.             i = sfBandIndex[is_mpeg2][sfreq].l[i];
  1104.  
  1105.             for ( ; sfb<21; sfb++ )    {
  1106.                 sb = sfBandIndex[is_mpeg2][sfreq].l[sfb+1] - sfBandIndex[is_mpeg2][sfreq].l[sfb];
  1107.                 for ( ; sb > 0; sb--) {
  1108.                     is_pos[i] = (*scalefac)[1].l[sfb];
  1109.                     i++;
  1110.                 }
  1111.             }
  1112.             sfb = sfBandIndex[is_mpeg2][sfreq].l[20];
  1113.             for ( sb = 576 - sfBandIndex[is_mpeg2][sfreq].l[21]; sb > 0; sb-- )
  1114.             {
  1115.                 is_pos[i] = is_pos[sfb];
  1116.                 i++;
  1117.             }
  1118.         }
  1119.  
  1120.         if (is_mpeg2) {    // BROKEN, esp with short blocks!!
  1121.             int ms_mode = !!(mode_ext & 2);
  1122.             int intensity_scale = gr_info->scalefac_compress&1;
  1123.             int is_max[54];
  1124.  
  1125.             // figure out illegal values for each scale factor band
  1126.  
  1127.             k=0;
  1128.             for(i=0; i<4; i++) {
  1129.                 for(j=0; j<scale_block_indexes[i]; j++)
  1130.                     is_max[k++] = (1<<slen[i])-1;
  1131.             }
  1132.             while(k<54) is_max[k++]=0;
  1133.  
  1134.             sfb=0;
  1135.             next_sfb_boundary = sfBandIndex[1][sfreq].l[1];
  1136.  
  1137.             for(i=0; i<SBLIMIT*SSLIMIT; i++) {
  1138.                 double x = lr[0][i];
  1139.  
  1140.                 lr[0][i] = x*is_ratio2[intensity_scale][ms_mode][is_max[sfb]+is_pos[i]][0];
  1141.                 lr[1][i] = x*is_ratio2[intensity_scale][ms_mode][is_max[sfb]+is_pos[i]][1];
  1142.  
  1143.                 if (i == next_sfb_boundary)
  1144.                     next_sfb_boundary = sfBandIndex[1][sfreq].l[++sfb + 1];
  1145.             }
  1146.         } else {
  1147.             for(i=0; i<SBLIMIT*SSLIMIT; i++) {
  1148.                 if ( is_pos[i] == 7 ) {
  1149.                     if ( ms_stereo ) {
  1150.                         double a = lr[0][i];
  1151.                         double b = lr[1][i];
  1152.  
  1153.                         lr[0][i] = (a+b)*0.707106781186547524400844362104849; ///1.41421356;
  1154.                         lr[1][i] = (a-b)*0.707106781186547524400844362104849; ///1.41421356;
  1155.                     }
  1156.                 } else {
  1157.                     double x = lr[0][i];
  1158.  
  1159.                     lr[0][i] = x*is_ratio1[is_pos[i]][0];
  1160.                     lr[1][i] = x*is_ratio1[is_pos[i]][1];
  1161.                 }
  1162.             }
  1163.         }
  1164.  
  1165.         return nz0>nz1 ? nz0 : nz1;
  1166.     } else if (ms_stereo) {
  1167.    
  1168.         if (nz1 > nz0)
  1169.             nz0 = nz1;
  1170.  
  1171.         for(i=0; i<nz0; i++) {
  1172.             double a = lr[0][i];
  1173.             double b = lr[1][i];
  1174.  
  1175.             lr[0][i] = (a+b)*0.707106781186547524400844362104849; ///1.41421356;
  1176.             lr[1][i] = (a-b)*0.707106781186547524400844362104849; ///1.41421356;
  1177.         }
  1178.  
  1179.         return nz0;
  1180.     }
  1181.  
  1182.     return -1;
  1183. }
  1184.  
  1185. void AMPDecoder::L3_Reorder (float xr[SBLIMIT*SSLIMIT],
  1186.             float ro[SBLIMIT*SSLIMIT],
  1187.             struct gr_info_s *gr_info) {
  1188.  
  1189.     int sfreq=sr_index;
  1190.     int sfb, sfb_start, sfb_lines;
  1191.     int sb, ss, window, freq, src_line, des_line;
  1192.     int i;
  1193.     
  1194.     if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  1195.         if (gr_info->mixed_block_flag) {
  1196.             /* NO REORDER FOR LOW 2 SUBBANDS */
  1197.             for(i=0; i<2*SSLIMIT; i++)
  1198.                 ro[i] = xr[i];
  1199.  
  1200.             for(i=2*SSLIMIT; i<SBLIMIT*SSLIMIT; i++)
  1201.                 ro[i] = 0;
  1202.         
  1203.             /* REORDERING FOR REST SWITCHED SHORT */
  1204.  
  1205.             sfb_start=sfBandIndex[is_mpeg2][sfreq].s[3];
  1206.             sfb_lines=sfBandIndex[is_mpeg2][sfreq].s[4] - sfb_start;
  1207.             for(sfb=3; sfb < 13; sfb++) {
  1208.                 for(window=0; window<3; window++)
  1209.                     for(freq=0;freq<sfb_lines;freq++) {
  1210.                         src_line = sfb_start*3 + window*sfb_lines + freq; 
  1211.                         des_line = (sfb_start*3) + window + (freq*3);
  1212.                         ro[des_line] = xr[src_line];
  1213.                     }
  1214.  
  1215.                 sfb_start=sfBandIndex[is_mpeg2][sfreq].s[sfb];
  1216.                 sfb_lines=sfBandIndex[is_mpeg2][sfreq].s[sfb+1] - sfb_start;
  1217.             }
  1218.         } 
  1219.         else {  /* pure short */
  1220.             for(i=0; i<SBLIMIT*SSLIMIT; i++)
  1221.                 ro[i] = 0;
  1222.  
  1223.             sfb_start = 0;
  1224.             sfb_lines = sfBandIndex[is_mpeg2][sfreq].s[1];
  1225.  
  1226.             for(sfb=0; sfb < 13; sfb++) {
  1227.                 for(window=0; window<3; window++)
  1228.                     for(freq=0;freq<sfb_lines;freq++) {
  1229.                         src_line = sfb_start*3 + window*sfb_lines + freq; 
  1230.                         des_line = (sfb_start*3) + window + (freq*3);
  1231.                         ro[des_line] = xr[src_line];
  1232.                     }
  1233.  
  1234.                 sfb_start = sfBandIndex[is_mpeg2][sfreq].s[sfb];
  1235.                 sfb_lines = sfBandIndex[is_mpeg2][sfreq].s[sfb+1] - sfb_start;
  1236.             }
  1237.         }
  1238.  
  1239.         memcpy(xr, ro, sizeof(xr[0])*SBLIMIT*SSLIMIT);
  1240.     }
  1241. //    else {   /*long blocks */
  1242. //        memcpy(ro, xr, sizeof(xr[0])*SBLIMIT*SSLIMIT);
  1243. ////        for(i=0; i<SBLIMIT*SSLIMIT; i++)
  1244. ////                ro[i] = xr[i];
  1245. //    }
  1246. }
  1247.  
  1248. void AMPDecoder::L3_Antialias(float xr[SBLIMIT][SSLIMIT],
  1249.                     struct gr_info_s *gr_info) {
  1250.  
  1251.     static const double cs[8]={
  1252.         0.8574929166930334,
  1253.         0.8817419876991212,
  1254.         0.9496286453969656,
  1255.         0.9833145920724280,
  1256.         0.9955178161793186,
  1257.         0.9991605581318322,
  1258.         0.9998991952431355,
  1259.         0.9999931550702761,
  1260.     };
  1261.  
  1262.     static const double ca[8]={
  1263.         -0.5144957704600444,
  1264.         -0.4717319865436337,
  1265.         -0.3133774654334998,
  1266.         -0.1819132018778039,
  1267.         -0.09457419135028555,
  1268.         -0.04096558401494271,
  1269.         -0.01419856866483041,
  1270.         -0.003699974674877601,
  1271.     };
  1272.  
  1273.     double        bu,bd;  /* upper and lower butterfly inputs */
  1274.     int           ss,sb,sblim;
  1275.     
  1276.     /* clear all inputs */  
  1277.     
  1278. //    for(sb=0;sb<SBLIMIT;sb++)
  1279. //        for(ss=0;ss<SSLIMIT;ss++)
  1280. //            hybridIn[sb][ss] = xr[sb][ss];
  1281.  
  1282. /*    if  (gr_info->window_switching_flag && (gr_info->block_type == 2) &&
  1283.         !gr_info->mixed_block_flag ) {
  1284.  
  1285.         return;
  1286.     }*/
  1287.     
  1288.     if ( gr_info->window_switching_flag && gr_info->mixed_block_flag &&
  1289.         (gr_info->block_type == 2))
  1290.         sblim = 1;
  1291.     else
  1292.         sblim = SBLIMIT-1;
  1293.  
  1294.    /* 31 alias-reduction operations between each pair of sub-bands */
  1295.    /* with 8 butterflies between each pair                         */
  1296.  
  1297.    for(sb=0;sb<sblim;sb++)
  1298.        for(ss=0;ss<8;ss++) {
  1299.            bu = xr[sb][17-ss];
  1300.            bd = xr[sb+1][ss];
  1301.            xr[sb][17-ss] = (bu * cs[ss]) - (bd * ca[ss]);
  1302.            xr[sb+1][ss] = (bd * cs[ss]) + (bu * ca[ss]);
  1303.        }  
  1304. }
  1305.  
  1306. extern void inv_mdct(float *in,
  1307.                 float out[SSLIMIT][SBLIMIT],
  1308.                 float *prevblock,
  1309.                 int block_type);
  1310.  
  1311. void AMPDecoder::L3_Hybrid(float fsIn[SBLIMIT][SSLIMIT],
  1312.                 float fsOut[SSLIMIT][SBLIMIT],
  1313.                 int ch,
  1314.                 struct gr_info_s *gr_info,
  1315.                 int nonzero_entries) {
  1316.  
  1317.     int sb;
  1318.     float rawout[36];
  1319.     int bt;
  1320.     int bands;
  1321.  
  1322.     bands = (nonzero_entries + SSLIMIT-1) / SSLIMIT;
  1323.  
  1324.     for(sb=0; sb<bands; sb++) {
  1325.         bt = (gr_info->window_switching_flag && gr_info->mixed_block_flag &&
  1326.             (sb < 2)) ? 0 : gr_info->block_type; 
  1327.  
  1328.         inv_mdct(fsIn[sb], (float(*)[SBLIMIT])&fsOut[0][sb], prevblck[ch][sb], bt);
  1329.     }
  1330.  
  1331.     for(; sb<SBLIMIT; sb++) {
  1332.         for(int i=0; i<18; i++) {
  1333.             fsOut[i][sb] = prevblck[ch][sb][i];
  1334.             prevblck[ch][sb][i] = 0.0;
  1335.         }
  1336.     }
  1337. }
  1338.