home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / AACD / Sound / LAME / Source / loop.c < prev    next >
C/C++ Source or Header  |  1999-06-03  |  84KB  |  3,035 lines

  1. /**********************************************************************
  2.  * ISO MPEG Audio Subgroup Software Simulation Group (1996)
  3.  * ISO 13818-3 MPEG-2 Audio Encoder - Lower Sampling Frequency Extension
  4.  *
  5.  * $Id: loop.c,v 1.2 1997/01/19 22:28:29 rowlands Exp $ 
  6.  *
  7.  * $Log: loop.c,v $
  8.  * Revision 1.2  1997/01/19 22:28:29  rowlands
  9.  * Layer 3 bug fixes from Seymour Shlien
  10.  *
  11.  * Revision 1.1  1996/02/14 04:04:23  rowlands
  12.  * Initial revision
  13.  *
  14.  * Received from Mike Coleman
  15.  **********************************************************************/
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <math.h>
  20. #include <assert.h>
  21. #include "globalflags.h"
  22. #include "common.h"
  23. #include "l3side.h"
  24. #include "loop.h"
  25. #include "huffman.h"
  26. #include "l3bitstream.h"
  27. #include "reservoir.h"
  28. #include "loop-pvt.h"
  29. #include "gtkanal.h"
  30.  
  31.  
  32.  
  33. void VBR_on_pe(frame_params *fr_ps, layer *info, III_side_info_t *l3_side, 
  34.            int VBRbits[2][2],
  35.            double pe[][2], int *mean_bits)
  36.  
  37.   int extrabits=0;
  38.   int nshort=0;
  39.   int index,gr,ch;
  40.   int bitsPerFrame;
  41.   int stereo = fr_ps->stereo;  
  42.   int fullframebits;
  43.  
  44.   getframebits(info,stereo,&bitsPerFrame,mean_bits);
  45.   fullframebits= ResvFrameBegin( fr_ps, l3_side, *mean_bits, bitsPerFrame );
  46.   
  47.   
  48.   for (gr=0;gr<2;gr++)
  49.     for (ch=0;ch<2;ch++) {
  50.       int shortblock = (l3_side->gr[gr].ch[ch].tt.block_type==2);
  51.       if (shortblock) nshort ++;
  52.       VBRbits[gr][ch] = fullframebits/4;
  53.     }
  54.   index = nshort;
  55.   /* if (index>3) index=3; */
  56.   
  57.   if (index>0) {
  58.     info->bitrate_index += index;
  59.     /* nshort=1: min 160kbs.  (1 shortblock granule)
  60.      * nshort=2: min 190kbs.  (2 shortblock granules)
  61.      * nshort=3: min 220kbs   (3 shortblock granules)
  62.      * nshort=4: min 256kbs   (4 shortblock granules) */
  63.     if (info->bitrate_index < 9 + index) info->bitrate_index = 9+index;  
  64.     if (info->bitrate_index > 14) info->bitrate_index=14;
  65.     
  66.     getframebits(info,stereo,&bitsPerFrame,mean_bits);
  67.     extrabits= ResvFrameBegin( fr_ps, l3_side, *mean_bits, bitsPerFrame );
  68.     extrabits -= fullframebits;
  69.   }
  70.   /* divide extra bits based on PE */
  71.   if (extrabits > 0) {
  72.     double pe2[2][2],pe_tot=0;
  73.     for (gr=0;gr<2;gr++) {
  74.       for (ch=0;ch<2;ch++) {
  75.     int shortblock = (l3_side->gr[gr].ch[ch].tt.block_type==2);
  76.     pe2[gr][ch] = Min(pe[gr][ch],2800.);
  77.     if (shortblock)  pe2[gr][ch] = Max(pe2[gr][ch],1800.0);
  78.     pe_tot += pe2[gr][ch];
  79.       }
  80.     }
  81.     for (gr=0;gr<2;gr++) {
  82.       for (ch=0;ch<2;ch++) {
  83.     VBRbits[gr][ch] += (extrabits*pe2[gr][ch])/pe_tot;
  84.       }
  85.     }
  86.   }
  87. }
  88.  
  89.  
  90.  
  91.  
  92. void VBR_on_over(frame_params *fr_ps, layer *info, III_side_info_t *l3_side, 
  93.            int VBRbits[2][2],
  94.            int over[2][2], int *mean_bits)
  95.  
  96.   int extrabits=0;
  97.   int gr,ch;
  98.   int bitsPerFrame;
  99.   int stereo = fr_ps->stereo;  
  100.   int fullframebits;
  101.  
  102.   getframebits(info,stereo,&bitsPerFrame,mean_bits);
  103.   fullframebits= ResvFrameBegin( fr_ps, l3_side, *mean_bits, bitsPerFrame );
  104.   
  105.   info->bitrate_index ++;
  106.   getframebits(info,stereo,&bitsPerFrame,mean_bits);
  107.   extrabits= ResvFrameBegin( fr_ps, l3_side, *mean_bits, bitsPerFrame );
  108.   extrabits -= fullframebits;
  109.  
  110.   /* divide extra bits based on OVER */
  111.   if (extrabits > 0) {
  112.     double over2[2][2],over_tot=0;
  113.     for (gr=0;gr<2;gr++) {
  114.       for (ch=0;ch<2;ch++) {
  115.     over2[gr][ch] = over[2][2] + 1;
  116.     over_tot += over2[gr][ch];
  117.       }
  118.     }
  119.     for (gr=0;gr<2;gr++) {
  120.       for (ch=0;ch<2;ch++) {
  121.     VBRbits[gr][ch] += (extrabits*over2[gr][ch])/over_tot;
  122.       }
  123.     }
  124.   }
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. /* #define DEBUG */
  138. /* #define DEBUGSC */
  139.  
  140. int scale_bitcount_lsf( III_scalefac_t *scalefac, gr_info *cod_info,
  141.                     int gr, int ch );
  142.  
  143.  
  144. /* turn on chris' xrpow speedups */
  145. static int xrpow_flag=0;
  146.  
  147. static int convert_mdct, convert_psy, reduce_sidechannel;
  148. /*
  149. 4 possibilities:
  150.  
  151. 1   MDCT input L/R, quantize L/R,   psy-model thresholds: L/R    -m s
  152. 2   MDCT input L/R, quantize M/S,   psy-model thresholds: L/R    -m j
  153. 3   MDCT input M/S, quantize M/S,   psy-model thresholds: M/S    -m f 
  154. 4   MDCT input L/R, quantize M/S,   psy-model thresholds: M/S     coming soon
  155.  
  156. 1:  convert_mdct = 0, convert_psy=0,  reduce_sidechannel=0
  157. 2:  convert_mdct = 1, convert_psy=1,  reduce_sidechannel=1
  158. 3:  convert_mdct = 0, convert_psy=0,  reduce_sidechannel=1
  159. 4:  convert_mdct = 1, convert_psy=0,  reduce_sidechannel=1
  160.  
  161. if (convert_mdct), then iteration_loop will quantize M/S data from
  162. the L/R input MDCT coefficients.
  163.  
  164. if (convert_psy), then calc_noise will compute the noise for the L/R
  165. channels from M/S MDCT data and L/R psy-model threshold information.
  166. Distortion in ether L or R channel will be marked as distortion in
  167. both Mid and Side channels.  
  168.  
  169. if (reduce_sidechannel) then outer_loop will allocate less bits
  170. to the side channel and more bits to the mid channel based on relative 
  171. energies.
  172. */
  173.  
  174.  
  175.  
  176.  
  177. /*
  178.   Here are MPEG1 Table B.8 and MPEG2 Table B.1
  179.   -- Layer III scalefactor bands.
  180.   Index into this using a method such as:
  181.     idx  = fr_ps->header->sampling_frequency
  182.            + (fr_ps->header->version * 3)
  183. */
  184.  
  185. struct scalefac_struct sfBandIndex[6] =
  186. {
  187.  
  188.   { /* Table B.2.b: 22.05 kHz */
  189.     {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  190.     {0,4,8,12,18,24,32,42,56,74,100,132,174,192}
  191.   },
  192.   { /* Table B.2.c: 24 kHz */
  193.     {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
  194.     {0,4,8,12,18,26,36,48,62,80,104,136,180,192}
  195.   },
  196.   { /* Table B.2.a: 16 kHz */
  197.     {0,6,12,18,24,30,36,44,45,66,80,96,116,140,168,200,238,248,336,396,464,522,576},
  198.     {0,4,8,12,18,26,36,48,62,80,104,134,174,192}
  199.   },
  200.   { /* Table B.8.b: 44.1 kHz */
  201.     {0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576},
  202.     {0,4,8,12,16,22,30,40,52,66,84,106,136,192}
  203.   },
  204.   { /* Table B.8.c: 48 kHz */
  205.     {0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576},
  206.     {0,4,8,12,16,22,28,38,50,64,80,100,126,192}
  207.   },
  208.   { /* Table B.8.a: 32 kHz */
  209.     {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
  210.     {0,4,8,12,16,22,30,42,58,78,104,138,180,192}
  211.   }
  212. };
  213.  
  214. /*
  215.   The following table is used to implement the scalefactor
  216.   partitioning for MPEG2 as described in section
  217.   2.4.3.2 of the IS. The indexing corresponds to the
  218.   way the tables are presented in the IS:
  219.  
  220.   [table_number][row_in_table][column of nr_of_sfb]
  221. */
  222. static unsigned nr_of_sfb_block[6][3][4] =
  223. {
  224.   {
  225.     {6, 5, 5, 5},
  226.     {9, 9, 9, 9},
  227.     {6, 9, 9, 9}
  228.   },
  229.   {
  230.     {6, 5, 7, 3},
  231.     {9, 9, 12, 6},
  232.     {6, 9, 12, 6}
  233.   },
  234.   {
  235.     {11, 10, 0, 0},
  236.     {18, 18, 0, 0},
  237.     {15,18,0,0}
  238.   },
  239.   {
  240.     {7, 7, 7, 0},
  241.     {12, 12, 12, 0},
  242.     {6, 15, 12, 0}
  243.   },
  244.   {
  245.     {6, 6, 6, 3},
  246.     {12, 9, 9, 6},
  247.     {6, 12, 9, 6}
  248.   },
  249.   {
  250.     {8, 8, 5, 0},
  251.     {15,12,9,0},
  252.     {6,18,9,0}
  253.   }
  254. };
  255.  
  256. /*
  257.   table of largest scalefactors for MPEG2
  258. */
  259. static unsigned max_sfac_tab[6][4] =
  260. {
  261.     {4, 4, 3, 3},
  262.     {4, 4, 3, 0},
  263.     {3, 2, 0, 0},
  264.     {4, 5, 5, 0},
  265.     {3, 3, 3, 0},
  266.     {2, 2, 0, 0}
  267. };
  268.  
  269. /* Table B.6: layer3 preemphasis */
  270. int  pretab[21] =
  271. {
  272.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  273.     1, 1, 1, 1, 2, 2, 3, 3, 3, 2
  274. };
  275.  
  276. /* This is the scfsi_band table from 2.4.2.7 of the IS */
  277. int scfsi_band_long[5] = { 0, 6, 11, 16, 21 };
  278.  
  279. int *scalefac_band_long  = &sfBandIndex[3].l[0];
  280. int *scalefac_band_short = &sfBandIndex[3].s[0];
  281.  
  282.  
  283. void ms_convert(double xr[2][576],double xr_org[2][576])
  284. {
  285.   int i;
  286.   for ( i = 0; i < 576; i++ ) {
  287.     xr[0][i] = (xr_org[0][i]+xr_org[1][i])/sqrt(2.0);
  288.     xr[1][i] = (xr_org[0][i]-xr_org[1][i])/sqrt(2.0);
  289.   }
  290. }
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298. /************************************************************************/
  299. /*  iteration_loop()                                                    */
  300. /************************************************************************/
  301. void
  302. iteration_loop( double pe[][2], double ms_ener_ratio[2],
  303.         double xr_org[2][2][576], III_psy_ratio *ratio,
  304.         III_side_info_t *l3_side, int l3_enc[2][2][576],
  305.         double xr_dec[2][2][576],
  306.         III_scalefac_t *scalefac, frame_params *fr_ps)
  307. {
  308.   static int firstcall = 1;
  309.   III_psy_xmin l3_xmin;
  310.   III_psy_xmin l3_xmin_save;
  311.   gr_info *cod_info;
  312.   layer *info;
  313.   int *main_data_begin;
  314.   int VBRbits[2][2];
  315.   int best_over[2][2];
  316.   int bitsPerFrame;
  317.   int mean_bits;
  318.   int stereo = fr_ps->stereo;
  319.   
  320.   int ch, gr, i, mode_gr, bit_rate;
  321.   double xr[2][2][576];
  322.   
  323.   
  324.   main_data_begin = &l3_side->main_data_begin;
  325.   l3_side->resvDrain = 0;
  326.   
  327.   
  328.   if ( firstcall ) {
  329.     *main_data_begin = 0;
  330.     firstcall = 0;
  331.   }
  332.   
  333.   
  334.   info = fr_ps->header;
  335.   mode_gr = (info->version == 1) ? 2 : 1;
  336.   bit_rate = bitrate[info->version][info->lay-1][info->bitrate_index];
  337.  
  338.   scalefac_band_long  = &sfBandIndex[info->sampling_frequency + (info->version * 3)].l[0];
  339.   scalefac_band_short = &sfBandIndex[info->sampling_frequency + (info->version * 3)].s[0];
  340.   
  341.   
  342.   convert_mdct=0;
  343.   convert_psy=0;
  344.   reduce_sidechannel=0;
  345.   if (info->mode_ext==2) {
  346.     convert_mdct = 1;
  347.     convert_psy = 1;
  348.     reduce_sidechannel=1;
  349.   }
  350.   if (force_ms) {
  351.     convert_mdct = 0;
  352.     convert_psy = 0;
  353.     reduce_sidechannel=1;
  354.   }
  355.   
  356.  
  357.   /* remove coefficients in scalefactor band 21 (12 for short blocks)
  358.    * FhG does this for bps <= 128kbs, so we will too.  
  359.    * This amounds to a 16kHz low-pass filter.  If that offends you, you
  360.    * probably should not be encoding at 128kbs!
  361.    * There is no ratio[21] or xfsf[21], so when these coefficients are
  362.    * included they are just quantized as is.
  363.    */
  364.   if (gpsycho && sfb21) {
  365.     if (bit_rate <= 64*stereo) {
  366.       for (ch =0 ; ch < 2 ; ch++)
  367.     for ( gr = 0; gr < mode_gr; gr++ ) {
  368.       int shortblock = (l3_side->gr[gr].ch[0].tt.block_type==2);
  369.       if (shortblock) {
  370.         int j;
  371.         for (j=0; j<3; j++) {
  372.           int start = scalefac_band_short[ SFB_SMAX-1 ];
  373.           for ( i = start; i < 192; i++ ) {
  374.         int i0 = 3*i+j; 
  375.         xr_org[gr][ch][i0]=0;
  376.           }
  377.         }
  378.       }else{
  379.         int start = scalefac_band_long[ SFB_LMAX-1 ];
  380.         for ( i = start; i < 576; i++ ) xr_org[gr][ch][i]=0;
  381.       }
  382.     }
  383.     }
  384.   }
  385.   
  386.   
  387.   /* dont use xrpow for ISO psy model */
  388.   if (!gpsycho) xrpow_flag=0;
  389.   
  390.   
  391.   
  392.   /* some intializations. */
  393.   memset((char *) &l3_xmin, 0, sizeof(l3_xmin));
  394.   for ( gr = 0; gr < mode_gr; gr++ ){
  395.     for ( ch = 0; ch < stereo; ch++ ){
  396.       cod_info = (gr_info *) &(l3_side->gr[gr].ch[ch]);
  397.       gr_deco(cod_info);
  398.       calc_xmin( xr_org, ratio, cod_info, &l3_xmin, gr, ch );
  399.     }
  400.   }
  401.   memcpy(&l3_xmin_save,&l3_xmin,sizeof(l3_xmin));
  402.  
  403.   /* dont bother with scfsi */
  404.   for ( ch = 0; ch < stereo; ch++ )
  405.     for ( i = 0; i < 4; i++ )
  406.       l3_side->scfsi[ch][i] = 0;
  407.   
  408.  
  409.   /* copy data to be quantized into xr */
  410.   if (convert_mdct) for ( gr = 0; gr < mode_gr; gr++ ) ms_convert(xr[gr],xr_org[gr]);
  411.   else memcpy(xr,xr_org,sizeof(xr));   
  412.  
  413.   
  414.  
  415.   if (VBR) {
  416.     VBR_on_pe(fr_ps,info,l3_side,VBRbits,pe,&mean_bits);
  417.   }else{
  418.     getframebits(info,stereo,&bitsPerFrame,&mean_bits);
  419.     ResvFrameBegin( fr_ps, l3_side, mean_bits, bitsPerFrame );
  420.   }
  421.  
  422.  
  423.  
  424.   /* quantize! */
  425.   if (gpsycho) {
  426.     do {
  427.       int max_over;
  428.       
  429.       for ( gr = 0; gr < mode_gr; gr++ )    
  430.     outer_loop( xr, mean_bits, VBRbits, bit_rate, best_over[gr],
  431.             &l3_xmin,l3_enc, fr_ps, 
  432.             scalefac,gr,stereo, l3_side, ratio, pe, ms_ener_ratio);
  433.       if (!VBR) break;
  434.  
  435.       /* see if we should try a higher bitrate quantization */
  436.       max_over=0;
  437.       for ( gr = 0; gr < mode_gr; gr++ ) 
  438.     for (ch=0 ; ch < stereo ; ch ++ ) {
  439.       if (best_over[gr][ch]>max_over) max_over=best_over[gr][ch];
  440.     }
  441.       if (max_over <= VBR_q) break;
  442.  
  443.       VBR_on_over(fr_ps,info,l3_side,VBRbits,best_over,&mean_bits);
  444.       /* restore data for another call to outer_loop */
  445.       if (convert_mdct) for ( gr = 0; gr < mode_gr; gr++ ) ms_convert(xr[gr],xr_org[gr]);
  446.       else memcpy(xr,xr_org,sizeof(xr));   
  447.       memcpy(&l3_xmin,&l3_xmin_save,sizeof(l3_xmin));
  448.       
  449.     } while (info->bitrate_index < 14);  /* max allowed 320 kbs */
  450.  
  451.   }else{
  452.     /* ISO outer_loop */
  453.     for ( gr = 0; gr < mode_gr; gr++ )    
  454.       outer_loop_old( xr, mean_bits, &l3_xmin,l3_enc, fr_ps, 
  455.               scalefac,gr,stereo, l3_side, ratio, pe );
  456.   }
  457.     
  458.   if (VBR) {
  459.     /* update reservoir status after FINAL quantization/bitrate */
  460.     for ( gr = 0; gr < mode_gr; gr++ ) {
  461.       for (ch=0 ; ch < stereo ; ch ++ ) {
  462.         gr_info *cod_info = &l3_side->gr[gr].ch[ch].tt;
  463.     ResvAdjust( fr_ps, cod_info, l3_side, mean_bits );
  464.       }
  465.     }
  466.   }
  467.   
  468.   /* set the sign of l3_enc */
  469.   for ( gr = 0; gr < mode_gr; gr++ )
  470.     for ( ch =  0; ch < stereo; ch++ )
  471.       {
  472.     int *pi = &l3_enc[gr][ch][0];
  473.     
  474.     for ( i = 0; i < 576; i++)         {
  475.       double pr = xr[gr][ch][i];
  476.       if ( (pr < 0) && (pi[i] > 0) )   pi[i] *= -1;
  477.     }
  478.       }
  479.   ResvFrameEnd( fr_ps, l3_side, mean_bits );
  480. }
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490. /************************************************************************/
  491. /*  quantanf_init                                                       */
  492. /************************************************************************/
  493. int quantanf_init( double xr[576] )
  494. /* Function: Calculate the first quantization step quantanf.       */
  495. {
  496.     int i, tp = 0;
  497.     double system_const, minlimit;
  498.     double sfm = 0.0, sum1 = 0.0, sum2 = 0.0;
  499.     
  500.     system_const = 8.0;
  501.     minlimit = -100.0;
  502.  
  503.     for ( i = 0; i < 576; i++ )
  504.     {
  505.         if ( xr[i] != 0 )
  506.     {
  507.             double tpd = xr[i] * xr[i];
  508.             sum1 += log( tpd );
  509.             sum2 += tpd;
  510.         }
  511.     }
  512.     if ( sum2 != 0.0 )
  513.     {
  514.         sfm = exp( sum1 / 576.0 ) / (sum2 / 576.0);
  515.         tp = nint( system_const * log(sfm) );
  516.     if ( tp < minlimit )
  517.         tp = minlimit;
  518. #ifdef DEBUG
  519.         printf(" quantanf = %d (quantanf_init)\n",tp );
  520. #endif
  521.     }
  522.       return(tp-70.0); /* SS 19-12-96. Starting value of
  523.                           global_gain or quantizerStepSize 
  524.                           has to be reduced for iteration_loop
  525.                        */
  526. }
  527.  
  528.  
  529.   
  530. /************************************************************************/
  531. /*  outer_loop                                                         */
  532. /************************************************************************/
  533. /*  Function: The outer iteration loop controls the masking conditions  */
  534. /*  of all scalefactorbands. It computes the best scalefac and          */
  535. /*  global gain. This module calls the inner iteration loop             */
  536. /************************************************************************/
  537. void outer_loop(
  538.     double xr[2][2][576],        /*  could be L/R OR MID/SIDE */
  539.     int mean_bits,
  540.     int VBRbits[2][2],
  541.     int bit_rate,
  542.     int best_over[2],
  543.     III_psy_xmin  *l3_xmin,   /* the allowed distortion of the scalefactor */
  544.     int l3_enc[2][2][576],    /* vector of quantized values ix(0..575) */
  545.     frame_params *fr_ps,
  546.     III_scalefac_t *scalefac, /* scalefactors */
  547.     int gr, int stereo, III_side_info_t *l3_side,
  548.     III_psy_ratio *ratio, double pe[2][2], double ms_ener_ratio[2])
  549. {
  550.   int status[2],notdone[2]={0,0},count[2]={0,0},bits_found[2];
  551.   int targ_bits[2],real_bits[2],tbits,extra_bits; 
  552.   int scalesave_l[2][CBLIMIT], scalesave_s[2][CBLIMIT][3];
  553.   int sfb, bits, huff_bits, save_preflag[2], save_compress[2];
  554.   double xfsf[2][4][CBLIMIT];
  555.   double xrpow[2][2][576],temp;
  556.   int distort[2][4][CBLIMIT];
  557.   int save_l3_enc[2][576];  
  558.   int save_real_bits[2];
  559.   int i,over[2], iteration, ch, mstoggle;
  560.   double best_noise[2];
  561.   int better[2];
  562.   double tot_noise[2];
  563.  
  564.   
  565.   gr_info save_cod_info[2];
  566.   gr_info *cod_info[2];  
  567.   
  568.   cod_info[0] = &l3_side->gr[gr].ch[0].tt;
  569.   cod_info[1] = &l3_side->gr[gr].ch[1].tt;
  570.     
  571.   for (ch=0 ; ch < stereo ; ch ++ ){
  572.  
  573.     /* if ( info->version == 1 )
  574.       calc_scfsi( xr[gr][ch], l3_side, &l3_xmin, ch, gr ); 
  575.     */
  576.         
  577.     
  578.     /* reset of iteration variables */
  579.     
  580.     for ( sfb = 0; sfb < SFB_LMAX; sfb++ )
  581.       scalefac->l[gr][ch][sfb] = 0;
  582.     for ( sfb = 0; sfb < SFB_SMAX; sfb++ )
  583.       for ( i = 0; i < 3; i++ )
  584.     scalefac->s[gr][ch][sfb][i] = 0;
  585.     
  586.     for ( i = 0; i < 4; i++ )
  587.       cod_info[ch]->slen[i] = 0;
  588.     cod_info[ch]->sfb_partition_table = &nr_of_sfb_block[0][0][0];
  589.     
  590.     cod_info[ch]->part2_3_length    = 0;
  591.     cod_info[ch]->big_values        = 0;
  592.     cod_info[ch]->count1            = 0;
  593.     cod_info[ch]->scalefac_compress = 0;
  594.     cod_info[ch]->table_select[0]   = 0;
  595.     cod_info[ch]->table_select[1]   = 0;
  596.     cod_info[ch]->table_select[2]   = 0;
  597.     cod_info[ch]->subblock_gain[0]  = 0;
  598.     cod_info[ch]->subblock_gain[1]  = 0;
  599.     cod_info[ch]->subblock_gain[2]  = 0;
  600.     cod_info[ch]->region0_count     = 0;
  601.     cod_info[ch]->region1_count     = 0;
  602.     cod_info[ch]->part2_length      = 0;
  603.     cod_info[ch]->preflag           = 0;
  604.     cod_info[ch]->scalefac_scale    = 0;
  605.     cod_info[ch]->quantizerStepSize = 0.0;
  606.     cod_info[ch]->count1table_select= 0;
  607.     cod_info[ch]->address1          = 0;
  608.     cod_info[ch]->address2          = 0;
  609.     cod_info[ch]->address3          = 0;
  610.   }
  611.  
  612.  
  613.   
  614.   for (ch=0 ; ch < stereo ; ch ++) {
  615.     count[ch]=0;
  616.     best_over[ch] = 100;
  617.     best_noise[ch] = 1e30;
  618.     for (i=0; i<576; i++) 
  619.       if ( fabs(xr[gr][ch][i]) > 0 ) count[ch]++; 
  620.     notdone[ch]=count[ch];
  621.   }
  622.   
  623. #if 0
  624.   /* compute subblock gains */
  625.   for (ch=0 ; ch < stereo ; ch ++) {
  626.     int j,b;  double en[3],mx;
  627.     if ((cod_info[ch]->block_type ==2) && count[ch]) {
  628.       /* estimate energy within each subblock */
  629.       for (b=0; b<3; b++) en[b]=0;
  630.       for ( i=0,j = 0; j < 192; j++ ) {
  631.     for (b=0; b<3; b++) {
  632.       en[b]+=xr[gr][ch][i]*xr[gr][ch][i];
  633.       i++;
  634.     }
  635.       }
  636.       mx = 1e-12;
  637.       for (b=0; b<3; b++) mx=Max(mx,en[b]);
  638.       for (b=0; b<3; b++) en[b] = Max(en[b],1e-12)/mx;
  639.       printf("ener = %4.2f  %4.2f  %4.2f  \n",en[0],en[1],en[2]);
  640.       /* pick gain so that 2^(2gain)*en[0] = 1  */
  641.       /* gain = .5* log( 1/en[0] )/log(2) = -.5*log(en[])/log(2) */
  642.       for (b=0; b<3; b++) {
  643.     cod_info[ch]->subblock_gain[b]=nint(-.5*log(en[b])/log(2.0));
  644.     if (cod_info[ch]->subblock_gain[b] > 3) 
  645.       cod_info[ch]->subblock_gain[b]=3;
  646.     if (cod_info[ch]->subblock_gain[b] < 0) 
  647.       cod_info[ch]->subblock_gain[b]=0;
  648.       }
  649.     }
  650.   }
  651. #endif
  652.   
  653.   
  654.   if (VBR) {
  655.     for (ch=0 ; ch < stereo ; ch ++ )
  656.       targ_bits[ch]=VBRbits[gr][ch];
  657.  
  658.   }else { 
  659.     int add_bits[2]; 
  660.     double bits_needed;
  661.  
  662.     /* allocate targ_bits for granule */
  663.     ResvMaxBits2( mean_bits, &tbits, &extra_bits, gr);
  664.  
  665.     for (ch=0 ; ch < stereo ; ch ++ )
  666.       targ_bits[ch]=tbits/stereo;
  667.     
  668.     /******************************************************************
  669.      * allocate extra bits from reservoir based on PE 
  670.      ******************************************************************/
  671.     bits=0;
  672.     
  673.     for (ch=0; ch<stereo; ch++) {
  674.       
  675.       /* extra bits based on PE > 700 */
  676.       add_bits[ch]=(pe[gr][ch]-700)/2.0;  /* 3.0; */
  677.       if (convert_psy) bits_needed = (pe[gr][0]+pe[gr][1]-1400)/2.0;
  678.       
  679.       /* short blocks need extra, no matter what the pe */
  680.       if (cod_info[ch]->block_type==2) 
  681.     if (add_bits[ch]<500) add_bits[ch]=500;
  682.       
  683.       if (add_bits[ch] < 0) add_bits[ch]=0;
  684.       bits += add_bits[ch];
  685.     }
  686.     for (ch=0; ch<stereo; ch++) {
  687.       if (bits > extra_bits) add_bits[ch] = (extra_bits*add_bits[ch])/bits;
  688.       targ_bits[ch] = targ_bits[ch] + add_bits[ch];
  689.     }
  690.     for (ch=0; ch<stereo; ch++) 
  691.       extra_bits -= add_bits[ch];
  692.   }
  693.  
  694.  
  695.  
  696.   if (reduce_sidechannel) {
  697.     /*  ms_ener_ratio = 0:  allocate 66/33  mid/side  fac=.33  
  698.      *  ms_ener_ratio =.5:  allocate 50/50 mid/side   fac= 0 */
  699.     /* 75/25 split is fac=.5 */
  700.     /* float fac = .50*(.5-ms_ener_ratio[gr])/.5;*/
  701.     float fac = .33*(.5-ms_ener_ratio[gr])/.5;
  702.     targ_bits[0] += targ_bits[1]*fac;
  703.     targ_bits[1] -= targ_bits[1]*fac;
  704.   }
  705.   
  706.   /* dont allow to many bits per channel */  
  707.   for (ch=0; ch<stereo; ch++) {
  708.     int max_bits = mean_bits/2 + 1200;
  709.     if (targ_bits[ch] > max_bits) {
  710.       extra_bits += (targ_bits[ch] - max_bits);
  711.       targ_bits[ch] = max_bits;
  712.     }
  713.   }
  714.   
  715.   
  716.   
  717.   
  718.   /* BEGIN MAIN LOOP */
  719.   iteration = 0;
  720.   mstoggle=0;        
  721.  
  722.   /* compute initial quantization step */
  723.   for (ch=0 ; ch < stereo ; ch ++ )
  724.     if (count[ch]) {
  725.       if (xrpow_flag) {
  726.     for(i=0;i<576;i++)
  727.       {
  728.         temp=fabs(xr[gr][ch][i]);
  729.         xrpow[gr][ch][i]=sqrt(sqrt(temp)*temp);
  730.       }
  731.       }
  732.       bits_found[ch]=bin_search_StepSize(targ_bits[ch],-211.0,46,
  733.         l3_enc[gr][ch],xr[gr][ch],xrpow[gr][ch],cod_info[ch]); 
  734.     }
  735.   
  736.  
  737.  
  738.  
  739.   while ( (notdone[0] || notdone[1])  ) {
  740.     int pre_just_turned_on[2];
  741.     iteration ++;
  742.     for (ch=0 ; ch < stereo ; ch ++ ) 
  743.     /* inner_loop starts with the initial quantization step computed above
  744.      * and slowly increases until the bits < huff_bits.
  745.      * Thus is it important not to start with too large of an inital
  746.      * quantization step.  Too small is ok, but inner_loop will take longer 
  747.      */
  748.     for (ch=0 ; ch < stereo ; ch ++ ) 
  749.       if (notdone[ch]) {
  750.     cod_info[ch]->part2_length = part2_length( scalefac, fr_ps, gr, ch, l3_side );
  751.     huff_bits = targ_bits[ch] - cod_info[ch]->part2_length;
  752.     if (huff_bits < 0) {
  753.       fprintf(stderr,"ERROR: outer_loop(): huff_bits < 0. \n");
  754.       exit(-5);
  755.     }
  756.     /* if this is the first iteration, see if we can reuse the quantization
  757.      * computed in bin_search_StepSize above */
  758.     if (iteration==1) {
  759.       if(bits_found[ch]>huff_bits) {
  760.         cod_info[ch]->quantizerStepSize+=1.0;
  761.         real_bits[ch] = inner_loop( xr, xrpow, l3_enc, huff_bits, cod_info[ch], gr, ch );
  762.       } else real_bits[ch]=bits_found[ch];
  763.     }
  764.     else 
  765.       real_bits[ch]=inner_loop( xr, xrpow, l3_enc, huff_bits, cod_info[ch], gr, ch );
  766.       }
  767.  
  768.     if (convert_psy) 
  769.       /* mid/side coefficiets, l/r thresholds */
  770.       calc_noise2( &xr[gr][0], &l3_enc[gr][0], cod_info, xfsf,
  771.          distort, l3_xmin,gr,stereo,over,tot_noise);
  772.     else
  773.       /* coefficients and thresholds both l/r (or both mid/side) */
  774.       for (ch=0; ch<stereo; ch++)
  775.     if (notdone[ch])
  776.       over[ch]=calc_noise1( xr[gr][ch], l3_enc[gr][ch], cod_info[ch], 
  777.           xfsf[ch],distort[ch], l3_xmin,gr,ch, tot_noise[ch]);
  778.  
  779.  
  780.     
  781.     /* check if this quantization is better the our saved quantization */
  782.     for (ch=0 ; ch < stereo ; ch ++ ) {
  783.       better[ch]=0;
  784.       if (notdone[ch]) {
  785.     if (fast_mode) 
  786.       if (convert_psy)
  787.         better[ch] = (tot_noise[0]+tot_noise[1]) < 
  788.                             (best_noise[0]+best_noise[1]);
  789.       else
  790.         better[ch] = (tot_noise[ch] < best_noise[ch]);
  791.     else 
  792.       if (convert_psy) 
  793.         better[ch] = (over[0]+over[1]) <= (best_over[0]+best_over[1]);
  794.       else
  795.         better[ch] = (over[ch] <= best_over[ch]);
  796.       }
  797.     }
  798.     /* save data so we can restore this quantization later */    
  799.     for (ch=0 ; ch < stereo ; ch ++ ) {
  800.       if (better[ch]) {
  801.     best_noise[ch]=tot_noise[ch];
  802.     best_over[ch]=over[ch];
  803.     if (notdone[ch]) {
  804.       for ( sfb = 0; sfb < CBLIMIT; sfb++ ) /* save scaling factors */
  805.         scalesave_l[ch][sfb] = scalefac->l[gr][ch][sfb];
  806.       
  807.       for ( sfb = 0; sfb < SFB_SMAX; sfb++ )
  808.         for ( i = 0; i < 3; i++ )
  809.           scalesave_s[ch][sfb][i] = scalefac->s[gr][ch][sfb][i];
  810.       
  811.       save_preflag[ch]  = cod_info[ch]->preflag;
  812.       save_compress[ch] = cod_info[ch]->scalefac_compress;
  813.       
  814.       memcpy(save_l3_enc[ch],l3_enc[gr][ch],sizeof(l3_enc[gr][ch]));   
  815.       memcpy(&save_cod_info[ch],cod_info[ch],sizeof(save_cod_info[ch]));
  816.       save_real_bits[ch]=real_bits[ch];
  817.  
  818. #ifdef HAVEGTK
  819.       if (gtkflag) {
  820.         for ( i = 0; i < 3; i++ ) {
  821.           for ( sfb = cod_info[ch]->sfb_smax; sfb < 12; sfb++ )  {
  822.         pinfo->xfsf_s[gr][ch][3*sfb+i] =  
  823.           pinfo->thr_s[gr][ch][3*sfb+i]*xfsf[ch][i+1][sfb]/
  824.           (1e-20+l3_xmin->s[gr][ch][sfb][i]);
  825.           }
  826.         }
  827.         for ( sfb = 0; sfb < cod_info[ch]->sfb_lmax; sfb++ )   {
  828.           pinfo->xfsf[gr][ch][sfb] =  
  829.         pinfo->thr[gr][ch][sfb]*xfsf[ch][0][sfb]/
  830.         (1e-20 + l3_xmin->l[gr][ch][sfb]);
  831.         }
  832.         pinfo->over[gr][ch]=over[ch];
  833.         pinfo->noise[gr][ch]=tot_noise[ch];
  834.       }
  835. #endif
  836.       
  837.  
  838.     }
  839.       }
  840.     }
  841.  
  842.     /* if no bands with distortion, we are done */
  843.     for (ch=0 ; ch < stereo ; ch ++ ) 
  844.       if (notdone[ch]) {
  845.     if (convert_psy) 
  846.       notdone[ch] = (over[0] || over[1]);
  847.     else
  848.       notdone[ch] = over[ch];
  849.       }
  850.  
  851.  
  852.  
  853.  
  854.     /* see if we should apply preemphasis */
  855.     for (ch=0 ; ch < stereo ; ch ++ ) {
  856.       pre_just_turned_on[ch]=0;
  857.       if (notdone[ch]) pre_just_turned_on[ch]=
  858.      preemphasis2( xr[gr][ch], xrpow[gr][ch], l3_xmin, 
  859.            gr, ch, l3_side,    distort[ch]);
  860.     }
  861.     
  862.     
  863.     /* if we didn't just apply pre-emph, let us see if we should 
  864.      * amplify some scale factor bands */
  865.     for (ch=0 ; ch < stereo ; ch ++ ) 
  866.       if (notdone[ch] && (!pre_just_turned_on[ch]) ) {
  867.     if (convert_psy)  {
  868.       if (ch == mstoggle)  /* only amplify mstoggle channel */
  869.         amp_scalefac_bands2( xr[gr][ch], xrpow[gr][ch], l3_xmin,
  870.              l3_side, scalefac, gr, ch, iteration,distort[ch]);
  871.       /* next time around, amplify other channel */
  872.       if (ch == (stereo-1)) mstoggle = 1-mstoggle;  
  873.       
  874.     }else{
  875.       amp_scalefac_bands2( xr[gr][ch], xrpow[gr][ch], l3_xmin,
  876.         l3_side, scalefac, gr, ch, iteration,distort[ch]);
  877.     }
  878.       }
  879.     
  880.     
  881.     /* check to make sure we have not amplified too much */
  882.     for (ch=0 ; ch < stereo ; ch ++ ) {
  883.       if (notdone[ch]) {
  884.     if ( (status[ch] = loop_break(scalefac, cod_info[ch], gr, ch)) == 0 )
  885.       if ( fr_ps->header->version == 1 )
  886.         status[ch] = scale_bitcount( scalefac, cod_info[ch], gr, ch );
  887.       else
  888.         status[ch] = scale_bitcount_lsf( scalefac, cod_info[ch], gr, ch );
  889.     notdone[ch] = ( status[ch] == 0 );
  890.       }
  891.     }
  892.   }    /* done with main iteration */
  893.   
  894.  
  895.   
  896.   
  897.   /* restore some data */
  898.   for (ch=0 ; ch < stereo ; ch ++ ) {
  899.     if (count[ch] ) {
  900.       cod_info[ch]->preflag = save_preflag[ch];
  901.       cod_info[ch]->scalefac_compress = save_compress[ch];
  902.       
  903.       for ( sfb = 0; sfb < CBLIMIT; sfb++ ) {
  904.     scalefac->l[gr][ch][sfb] = scalesave_l[ch][sfb];    
  905.       }
  906.       
  907.       for ( i = 0; i < 3; i++ )
  908.     for ( sfb = 0; sfb < SFB_SMAX; sfb++ ) {
  909.       scalefac->s[gr][ch][sfb][i] = scalesave_s[ch][sfb][i];    
  910.     }
  911.  
  912.       { 
  913.     real_bits[ch]=save_real_bits[ch];  
  914.     memcpy(l3_enc[gr][ch],save_l3_enc[ch],sizeof(l3_enc[gr][ch]));   
  915.     memcpy(cod_info[ch],&save_cod_info[ch],sizeof(save_cod_info[ch]));
  916.     
  917.     if ( fr_ps->header->version == 1 )
  918.       status[ch] = scale_bitcount( scalefac, cod_info[ch], gr, ch );
  919.     else
  920.       status[ch] = scale_bitcount_lsf( scalefac, cod_info[ch], gr, ch );
  921.     if (status[ch]) {
  922.       printf("Error recomputing scalefac_compress...this should not happen");
  923.       exit(-10);
  924.     }
  925.       }
  926.       cod_info[ch]->part2_length   = part2_length( scalefac, fr_ps, gr, ch, l3_side );
  927.       cod_info[ch]->part2_3_length = cod_info[ch]->part2_length + real_bits[ch];
  928. #ifdef HAVEGTK
  929.       if (gtkflag)
  930.     pinfo->LAMEmainbits[gr][ch]=cod_info[ch]->part2_3_length;
  931. #endif
  932.     }      
  933.   }
  934.   
  935.   /* finish up */
  936.   for (ch=0 ; ch < stereo ; ch ++ ) {
  937.     if (!VBR) ResvAdjust( fr_ps, cod_info[ch], l3_side, mean_bits );
  938.     cod_info[ch]->global_gain = nint( cod_info[ch]->quantizerStepSize + 210.0 );
  939.     assert( cod_info[ch]->global_gain < 256 );
  940.   }
  941.  
  942.  
  943.  
  944.  
  945.  
  946. }
  947.  
  948.  
  949.  
  950.  
  951.  
  952.   
  953. /************************************************************************/
  954. /*  outer_loop                                                         */
  955. /************************************************************************/
  956. /*  Function: The outer iteration loop controls the masking conditions  */
  957. /*  of all scalefactorbands. It computes the best scalefac and          */
  958. /*  global gain. This module calls the inner iteration loop             */
  959. /************************************************************************/
  960. void outer_loop_old(
  961.     double xr[2][2][576],        /*  could be L/R OR MID/SIDE */
  962.     int mean_bits,
  963.     III_psy_xmin  *l3_xmin,   /* the allowed distortion of the scalefactor */
  964.     int l3_enc[2][2][576],    /* vector of quantized values ix(0..575) */
  965.     frame_params *fr_ps,
  966.     III_scalefac_t *scalefac, /* scalefactors */
  967.     int gr, int stereo, III_side_info_t *l3_side,
  968.     III_psy_ratio *ratio, double pe[2][2])
  969. {
  970.     int status ;
  971.     int max_bits[2]; 
  972.     int scalesave_l[CBLIMIT], scalesave_s[CBLIMIT][3];
  973.     int sfb, bits, huff_bits, save_preflag, save_compress;
  974.     double xfsf[4][CBLIMIT];
  975.     int i, over, iteration, ch;
  976.     int do_ch[2];
  977.  
  978.   gr_info *cod_info[2];  
  979.   cod_info[0] = &l3_side->gr[gr].ch[0].tt;
  980.   cod_info[1] = &l3_side->gr[gr].ch[1].tt;
  981.   
  982.   for (ch=0 ; ch < stereo ; ch ++ ){
  983.  
  984.     /* if ( info->version == 1 )
  985.       calc_scfsi( xr[gr][ch], l3_side, &l3_xmin, ch, gr );   */
  986.         
  987.     
  988.     /* reset of iteration variables */
  989.     
  990.     for ( sfb = 0; sfb < 21; sfb++ )
  991.       scalefac->l[gr][ch][sfb] = 0;
  992.     for ( sfb = 0; sfb < 13; sfb++ )
  993.       for ( i = 0; i < 3; i++ )
  994.     scalefac->s[gr][ch][sfb][i] = 0;
  995.     
  996.     for ( i = 0; i < 4; i++ )
  997.       cod_info[ch]->slen[i] = 0;
  998.     cod_info[ch]->sfb_partition_table = &nr_of_sfb_block[0][0][0];
  999.     
  1000.     cod_info[ch]->part2_3_length    = 0;
  1001.     cod_info[ch]->big_values        = 0;
  1002.     cod_info[ch]->count1            = 0;
  1003.     cod_info[ch]->scalefac_compress = 0;
  1004.     cod_info[ch]->table_select[0]   = 0;
  1005.     cod_info[ch]->table_select[1]   = 0;
  1006.     cod_info[ch]->table_select[2]   = 0;
  1007.     cod_info[ch]->subblock_gain[0]  = 0;
  1008.     cod_info[ch]->subblock_gain[1]  = 0;
  1009.     cod_info[ch]->subblock_gain[2]  = 0;
  1010.     cod_info[ch]->region0_count     = 0;
  1011.     cod_info[ch]->region1_count     = 0;
  1012.     cod_info[ch]->part2_length      = 0;
  1013.     cod_info[ch]->preflag           = 0;
  1014.     cod_info[ch]->scalefac_scale    = 0;
  1015.     cod_info[ch]->quantizerStepSize = 0.0;
  1016.     cod_info[ch]->count1table_select= 0;
  1017.   }
  1018.  
  1019.  
  1020.     /* all spectral values zero ? */
  1021.     for (ch=0 ; ch < stereo ; ch ++ )
  1022.       do_ch[ch]=(fabs(xr_max(xr[gr][ch], 0, 576)) != 0.0);  
  1023.  
  1024.  
  1025.     for (ch=0 ; ch < stereo ; ch ++ )
  1026.       if (do_ch[ch])
  1027.     cod_info[ch]->quantizerStepSize=(double) quantanf_init( xr[gr][ch] );    
  1028.     
  1029.  
  1030.     for (ch=0 ; ch < stereo ; ch ++ ) {
  1031.       if (do_ch[ch]) {
  1032.       /* calculation of number of available bit( per granule ) */
  1033.       max_bits[ch] = ResvMaxBits( fr_ps, l3_side, &pe[gr][ch], mean_bits,gr,ch);
  1034.       
  1035.       bin_search_StepSize(max_bits[ch],cod_info[ch]->quantizerStepSize,200,
  1036.               l3_enc[gr][ch],xr[gr][ch],xr[gr][ch],cod_info[ch]); 
  1037.     
  1038.       
  1039.       iteration = 0;
  1040.       do {
  1041.     iteration += 1;
  1042.  
  1043.     cod_info[ch]->part2_length = part2_length( scalefac, fr_ps, gr, ch, l3_side );
  1044.     huff_bits = max_bits[ch] - cod_info[ch]->part2_length;
  1045.     bits = inner_loop( xr, xr, l3_enc, huff_bits, cod_info[ch], gr, ch );
  1046.     
  1047.     /* distortion calculation */
  1048.     calc_noise( &xr[gr][ch][0], &l3_enc[gr][ch][0], cod_info[ch], xfsf); 
  1049.  
  1050.     
  1051. #ifdef HAVEGTK
  1052.       if (gtkflag) {
  1053.     for ( sfb = 0; sfb < cod_info[ch]->sfb_lmax; sfb++ ) {
  1054.       pinfo->xfsf[gr][ch][sfb] =  
  1055.         pinfo->thr[gr][ch][sfb]*xfsf[0][sfb]/
  1056.         (1e-20 + l3_xmin->l[gr][ch][sfb]);
  1057.     }
  1058.     for ( i = 0; i < 3; i++ ) {
  1059.       for ( sfb = cod_info[ch]->sfb_smax; sfb < 12; sfb++ )  {
  1060.         pinfo->xfsf_s[gr][ch][3*sfb+i] =  
  1061.         pinfo->thr_s[gr][ch][3*sfb+i]*xfsf[i+1][sfb]/
  1062.         (1e-20 + l3_xmin->s[gr][ch][sfb][i]);
  1063.       }
  1064.     }
  1065.     pinfo->noise[gr][ch]=0;
  1066.       }
  1067. #endif
  1068.     
  1069.     
  1070.     for ( sfb = 0; sfb < CBLIMIT; sfb++ ) /* save scaling factors */
  1071.       scalesave_l[sfb] = scalefac->l[gr][ch][sfb];
  1072.     
  1073.     for ( sfb = 0; sfb < SFB_SMAX; sfb++ )
  1074.       for ( i = 0; i < 3; i++ )
  1075.         scalesave_s[sfb][i] = scalefac->s[gr][ch][sfb][i];
  1076.     
  1077.     save_preflag  = cod_info[ch]->preflag;
  1078.     save_compress = cod_info[ch]->scalefac_compress;
  1079.     
  1080.     preemphasis( &xr[gr][ch][0], xfsf, l3_xmin, gr, ch, l3_side );
  1081.     
  1082.     
  1083.     over = amp_scalefac_bands( &xr[gr][ch][0], xfsf, l3_xmin,
  1084.                    l3_side, scalefac, gr, ch, iteration);
  1085.     
  1086.  
  1087. #ifdef HAVEGTK
  1088.       if (gtkflag) {
  1089.     pinfo->over[gr][ch]=over;
  1090.       }
  1091. #endif
  1092.     
  1093.     if ( (status = loop_break(scalefac, cod_info[ch], gr, ch)) == 0 )
  1094.       {
  1095.         if ( fr_ps->header->version == 1 )
  1096.           status = scale_bitcount( scalefac, cod_info[ch], gr, ch );
  1097.         else
  1098.           status = scale_bitcount_lsf( scalefac, cod_info[ch], gr, ch );
  1099.       }
  1100.       }
  1101.       while ( (status == 0) && (over > 0) ); 
  1102.       
  1103.       
  1104.       cod_info[ch]->preflag = save_preflag;
  1105.       cod_info[ch]->scalefac_compress = save_compress;
  1106.       
  1107.       for ( sfb = 0; sfb < 21; sfb++ )
  1108.     scalefac->l[gr][ch][sfb] = scalesave_l[sfb];    
  1109.       
  1110.       for ( i = 0; i < 3; i++ )
  1111.     for ( sfb = 0; sfb < 12; sfb++ )
  1112.       scalefac->s[gr][ch][sfb][i] = scalesave_s[sfb][i];    
  1113.       
  1114.       cod_info[ch]->part2_length   = part2_length( scalefac, fr_ps, gr, ch, l3_side );
  1115.       cod_info[ch]->part2_3_length = cod_info[ch]->part2_length + bits;
  1116. #ifdef HAVEGTK
  1117.       if (gtkflag){
  1118.     pinfo->LAMEmainbits[gr][ch]=cod_info[ch]->part2_3_length;
  1119.       }
  1120. #endif
  1121.       } /* end do_ch[] */
  1122.  
  1123.       ResvAdjust( fr_ps, cod_info[ch], l3_side, mean_bits );
  1124.       
  1125.       cod_info[ch]->global_gain = nint( cod_info[ch]->quantizerStepSize + 210.0 );
  1126.       assert( cod_info[ch]->global_gain < 256 );
  1127.     } /* end ch loop */
  1128. }
  1129.  
  1130.  
  1131.  
  1132.  
  1133.  
  1134.  
  1135.  
  1136. /*************************************************************************** 
  1137.  *         inner_loop                                                      * 
  1138.  *************************************************************************** 
  1139.  * The code selects the best quantizerStepSize for a particular set
  1140.  * of scalefacs                                                            */
  1141.  
  1142. int
  1143. inner_loop( double xr[2][2][576], double xrpow[2][2][576],
  1144.         int l3_enc[2][2][576], int max_bits,
  1145.         gr_info *cod_info, int gr, int ch )
  1146. {
  1147.     int bits;
  1148.  
  1149.     assert( max_bits >= 0 );
  1150.     cod_info->quantizerStepSize -= 1.0;;
  1151.     do
  1152.     {
  1153.       cod_info->quantizerStepSize += 1.0;
  1154.       if (xrpow_flag)
  1155.     quantize_xrpow( xrpow[gr][ch], l3_enc[gr][ch], cod_info );
  1156.       else
  1157.     quantize( xr[gr][ch], l3_enc[gr][ch], cod_info );
  1158.       bits= count_bits(l3_enc[gr][ch],cod_info);
  1159.     }
  1160.     while ( bits > max_bits );
  1161.     return bits;
  1162. }
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168.  
  1169. /***************************************************************************/ 
  1170. /*        part2_length                                                     */ 
  1171. /***************************************************************************/ 
  1172.  
  1173. /* calculates the number of bits needed to encode the scalefacs in the     */
  1174. /* main data block                                                         */
  1175.  
  1176. int part2_length( III_scalefac_t *scalefac, frame_params *fr_ps,
  1177.           int gr, int ch, III_side_info_t *si )
  1178. {
  1179.     int slen1, slen2,  bits, partition;
  1180.     gr_info *gi = &si->gr[gr].ch[ch].tt;
  1181.  
  1182.     bits = 0;
  1183.     if ( fr_ps->header->version == 1 )
  1184.     {
  1185.     static int slen1_tab[16] = { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 };
  1186.     static int slen2_tab[16] = { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 };
  1187.  
  1188.     slen1 = slen1_tab[ gi->scalefac_compress ];
  1189.     slen2 = slen2_tab[ gi->scalefac_compress ];
  1190.  
  1191.     if ( (gi->window_switching_flag == 1) && (gi->block_type == 2) )
  1192.     {
  1193.         bits += (18 * slen1) + (18 * slen2);
  1194.     }
  1195.     else
  1196.     {
  1197.         if ( (gr == 0) || (si->scfsi[ch][0] == 0) )
  1198.         bits += (6 * slen1);
  1199.  
  1200.         if ( (gr == 0) || (si->scfsi[ch][1] == 0) )
  1201.         /*  bits += (6 * slen1);  This is wrong SS 19-12-96 */
  1202.         bits += (5 * slen1);
  1203.  
  1204.         if ( (gr == 0) || (si->scfsi[ch][2] == 0) )
  1205.         /*  bits += (6 * slen1);   This is wrong SS 19-12-96 */
  1206.         bits += (5 * slen2);
  1207.  
  1208.         if ( (gr == 0) || (si->scfsi[ch][3] == 0) )
  1209.         /* bits += (6 * slen1);   This is wrong SS 19-12-96 */
  1210.         bits += (5 * slen2);
  1211.     }
  1212.     }
  1213.     else
  1214.     {   /* MPEG 2 */
  1215.     assert( gi->sfb_partition_table );
  1216.     for ( partition = 0; partition < 4; partition++ )
  1217.         bits += gi->slen[partition] * gi->sfb_partition_table[partition];
  1218.     }
  1219.     return bits;
  1220. }
  1221.  
  1222.  
  1223.  
  1224. /*************************************************************************/
  1225. /*            scale_bitcount                                             */
  1226. /*************************************************************************/
  1227.  
  1228. /* Also calculates the number of bits necessary to code the scalefactors. */
  1229.  
  1230. int scale_bitcount( III_scalefac_t *scalefac, gr_info *cod_info,
  1231.         int gr, int ch )
  1232. {
  1233.     int i, k, sfb, max_slen1 = 0, max_slen2 = 0, /*a, b, */ ep = 2;
  1234.  
  1235.     static int slen1[16] = { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 };
  1236.     static int slen2[16] = { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 };
  1237.  
  1238.     if ( cod_info->window_switching_flag != 0 && cod_info->block_type == 2 )
  1239.     {
  1240.             /* a = 18; b = 18;  */
  1241.             for ( i = 0; i < 3; i++ )
  1242.             {
  1243.                 for ( sfb = 0; sfb < 6; sfb++ )
  1244.                     if ( scalefac->s[gr][ch][sfb][i] > max_slen1 )
  1245.                         max_slen1 = scalefac->s[gr][ch][sfb][i];
  1246.                 for (sfb = 6; sfb < 12; sfb++ )
  1247.                     if ( scalefac->s[gr][ch][sfb][i] > max_slen2 )
  1248.                         max_slen2 = scalefac->s[gr][ch][sfb][i];
  1249.             }
  1250.     }
  1251.     else
  1252.     { /* block_type == 1,2,or 3 */
  1253.         /* a = 11; b = 10;   */
  1254.         for ( sfb = 0; sfb < 11; sfb++ )
  1255.             if ( scalefac->l[gr][ch][sfb] > max_slen1 )
  1256.                 max_slen1 = scalefac->l[gr][ch][sfb];
  1257.         for ( sfb = 11; sfb < 21; sfb++ )
  1258.             if ( scalefac->l[gr][ch][sfb] > max_slen2 )
  1259.                 max_slen2 = scalefac->l[gr][ch][sfb];
  1260.     }
  1261.  
  1262.     for ( k = 0; k < 16; k++ )
  1263.     {
  1264.         if ( (max_slen1 < (1<<slen1[k])) && (max_slen2 < (1<<slen2[k])) )
  1265.         { 
  1266.             ep = 0;
  1267.             break;
  1268.         } 
  1269.     }
  1270.  
  1271.     if ( ep == 0 )
  1272.         cod_info->scalefac_compress = k;
  1273.     return ep;
  1274. }
  1275.  
  1276.  
  1277.  
  1278.  
  1279. /*************************************************************************/
  1280. /*            scale_bitcount_lsf                                         */
  1281. /*************************************************************************/
  1282.  
  1283. /* Also counts the number of bits to encode the scalefacs but for MPEG 2 */ 
  1284. /* Lower sampling frequencies  (24, 22.05 and 16 kHz.)                   */
  1285.  
  1286. /*  This is reverse-engineered from section 2.4.3.2 of the MPEG2 IS,     */
  1287. /* "Audio Decoding Layer III"                                            */
  1288.  
  1289. int scale_bitcount_lsf( III_scalefac_t *scalefac, gr_info *cod_info,
  1290.             int gr, int ch )
  1291. {
  1292.     int table_number, row_in_table, partition, nr_sfb, window, over;
  1293.     int i, sfb, max_sfac[ 4 ];
  1294.     unsigned *partition_table;
  1295.  
  1296.     /*
  1297.       Set partition table. Note that should try to use table one,
  1298.       but do not yet...
  1299.     */
  1300.     if ( cod_info->preflag )
  1301.     table_number = 2;
  1302.     else
  1303.     table_number = 0;
  1304.  
  1305.     for ( i = 0; i < 4; i++ )
  1306.     max_sfac[i] = 0;
  1307.  
  1308.     if ( cod_info->window_switching_flag != 0 && cod_info->block_type == 2 )
  1309.     {
  1310.         row_in_table = 1;
  1311.         partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
  1312.         for ( sfb = 0, partition = 0; partition < 4; partition++ )
  1313.         {
  1314.         nr_sfb = partition_table[ partition ] / 3;
  1315.         for ( i = 0; i < nr_sfb; i++, sfb++ )
  1316.             for ( window = 0; window < 3; window++ )
  1317.             if ( scalefac->s[gr][ch][sfb][window] > max_sfac[partition] )
  1318.                 max_sfac[partition] = scalefac->s[gr][ch][sfb][window];
  1319.         }
  1320.     }
  1321.     else
  1322.     {
  1323.     row_in_table = 0;
  1324.     partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
  1325.     partition = 0;
  1326.     for ( sfb = 0, partition = 0; partition < 4; partition++ )
  1327.     {
  1328.         nr_sfb = partition_table[ partition ];
  1329.         for ( i = 0; i < nr_sfb; i++, sfb++ )
  1330.         if ( scalefac->l[gr][ch][sfb] > max_sfac[partition] )
  1331.             max_sfac[partition] = scalefac->l[gr][ch][sfb];
  1332.     }
  1333.     }
  1334.  
  1335.     for ( over = 0, partition = 0; partition < 4; partition++ )
  1336.     {
  1337.     if ( max_sfac[partition] > max_sfac_tab[table_number][partition] )
  1338.         over++;
  1339.     }
  1340.     if ( !over )
  1341.     {
  1342.     /*
  1343.       Since no bands have been over-amplified, we can set scalefac_compress
  1344.       and slen[] for the formatter
  1345.     */
  1346.     static int log2tab[] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
  1347.  
  1348.     unsigned slen1, slen2, slen3, slen4;
  1349.  
  1350.         cod_info->sfb_partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
  1351.     for ( partition = 0; partition < 4; partition++ )
  1352.         cod_info->slen[partition] = log2tab[max_sfac[partition]];
  1353.  
  1354.     /* set scalefac_compress */
  1355.     slen1 = cod_info->slen[ 0 ];
  1356.     slen2 = cod_info->slen[ 1 ];
  1357.     slen3 = cod_info->slen[ 2 ];
  1358.     slen4 = cod_info->slen[ 3 ];
  1359.  
  1360.     switch ( table_number )
  1361.     {
  1362.       case 0:
  1363.         cod_info->scalefac_compress = (((slen1 * 5) + slen2) << 4)
  1364.         + (slen3 << 2)
  1365.         + slen4;
  1366.         break;
  1367.  
  1368.       case 1:
  1369.         cod_info->scalefac_compress = 400
  1370.         + (((slen1 * 5) + slen2) << 2)
  1371.         + slen3;
  1372.         break;
  1373.  
  1374.       case 2:
  1375.         cod_info->scalefac_compress = 500 + (slen1 * 3) + slen2;
  1376.         break;
  1377.  
  1378.       default:
  1379.         fprintf( stderr, "intensity stereo not implemented yet\n" );
  1380.         exit( EXIT_FAILURE );
  1381.         break;
  1382.     }
  1383.     }
  1384. #ifdef DEBUG
  1385.     if ( over ) 
  1386.         printf( "---WARNING !! Amplification of some bands over limits\n" );
  1387. #endif
  1388.     return over;
  1389. }
  1390.  
  1391.  
  1392. /*************************************************************************/
  1393. /*            calc_noise                                                 */
  1394. /*************************************************************************/
  1395.  
  1396. /*   Function: Calculate the distortion introduced by the quantization   */
  1397. /*   in each scale factor band.                                          */
  1398.  
  1399. void calc_noise( double xr[576], int ix[576], gr_info *cod_info,
  1400.         double xfsf[4][CBLIMIT])
  1401. {
  1402.     int start, end, sfb, l, i;
  1403.     double sum,step,bw;
  1404.  
  1405.     D192_3 *xr_s;
  1406.     I192_3 *ix_s;
  1407.  
  1408.     #define PRECALC_SIZE 1024 /* WAS 256 !!! */
  1409.     static double pow43[PRECALC_SIZE];
  1410.     static int init=0;
  1411.  
  1412.     if (init==0) {
  1413.       init++;
  1414.       for(i=0;i<PRECALC_SIZE;i++)
  1415.         pow43[i] = pow((double)i, 4.0/3.0);
  1416.     }
  1417.       
  1418.     xr_s = (D192_3 *) xr;
  1419.     ix_s = (I192_3 *) ix;
  1420.  
  1421.     step = pow( 2.0, (cod_info->quantizerStepSize) * 0.25 );
  1422.     for ( sfb = 0; sfb < cod_info->sfb_lmax; sfb++ )
  1423.     {
  1424.         start = scalefac_band_long[ sfb ];
  1425.         end   = scalefac_band_long[ sfb+1 ];
  1426.     bw = end - start;
  1427.  
  1428.         for ( sum = 0.0, l = start; l < end; l++ )
  1429.         {
  1430.             double temp;
  1431.         if (ix[l]<PRECALC_SIZE)
  1432.           temp = fabs(xr[l]) - pow43[ix[l]] * step;
  1433.         else
  1434.             {   
  1435.                temp = fabs(xr[l]) - pow((double)ix[l],4.0/3.0)*step;
  1436.            /*   printf("EHHHHHHH !?!?! ---> %d\n",ix[l]); .. table is not big enough */
  1437.             }
  1438.           sum += temp * temp; 
  1439.         }
  1440.         xfsf[0][sfb] = sum / bw;
  1441.     }
  1442.  
  1443.     for ( i = 0; i < 3; i++ )
  1444.     {
  1445.         step = pow( 2.0, (cod_info->quantizerStepSize) * 0.25 ); /* subblock_gain ? */
  1446.         for ( sfb = cod_info->sfb_smax; sfb < 12; sfb++ )
  1447.         {
  1448.             start = scalefac_band_short[ sfb ];
  1449.             end   = scalefac_band_short[ sfb+1 ];
  1450.         bw = end - start;
  1451.             
  1452.             for ( sum = 0.0, l = start; l < end; l++ )
  1453.             {
  1454.                 double temp;
  1455.         if((*ix_s)[l][i]<PRECALC_SIZE)
  1456.                     temp = fabs((*xr_s)[l][i]) - pow43[(*ix_s)[l][i]] * step;
  1457.                 else
  1458.                 {
  1459.                     temp = fabs((*xr_s)[l][i]) - pow((double)(*ix_s)[l][i],4.0/3.0)*step;
  1460.             /*    printf("EHHHHHHH !?!?! ---> %d\n",(*ix_s)[l][i]); */
  1461.                 }
  1462.                 sum += temp * temp;
  1463.             }       
  1464.             xfsf[i+1][sfb] = sum / bw;
  1465.         }
  1466.     }
  1467. }
  1468.  
  1469. /*************************************************************************/
  1470. /*            calc_noise                                                 */
  1471. /*************************************************************************/
  1472.  
  1473. /*   Function: Improved calc_noise for a single channel   */
  1474. int calc_noise1( double xr[576], int ix[576], gr_info *cod_info,
  1475.         double xfsf[4][CBLIMIT], int distort[4][CBLIMIT],
  1476.             III_psy_xmin *l3_xmin,int gr, int ch, double noise)
  1477.  
  1478. {
  1479.     int start, end, sfb, l, i, over=0;
  1480.     double sum,step,bw;
  1481.  
  1482.     D192_3 *xr_s;
  1483.     I192_3 *ix_s;
  1484.  
  1485.     #define PRECALC_SIZE 1024 /* WAS 256 !!! */
  1486.     static double pow43[PRECALC_SIZE];
  1487.     static int init=0;
  1488.     noise=0;
  1489.  
  1490.     if (init==0) {
  1491.       init++;
  1492.       for(i=0;i<PRECALC_SIZE;i++)
  1493.         pow43[i] = pow((double)i, 4.0/3.0);
  1494.     }
  1495.       
  1496.     xr_s = (D192_3 *) xr;
  1497.     ix_s = (I192_3 *) ix;
  1498.  
  1499.     step = pow( 2.0, (cod_info->quantizerStepSize) * 0.25 );
  1500.     for ( sfb = 0; sfb < cod_info->sfb_lmax; sfb++ )
  1501.     {
  1502.         start = scalefac_band_long[ sfb ];
  1503.         end   = scalefac_band_long[ sfb+1 ];
  1504.     bw = end - start;
  1505.  
  1506.         for ( sum = 0.0, l = start; l < end; l++ )
  1507.         {
  1508.             double temp;
  1509.         if (ix[l]<PRECALC_SIZE)
  1510.           temp = fabs(xr[l]) - pow43[ix[l]] * step;
  1511.         else
  1512.             {   
  1513.                temp = fabs(xr[l]) - pow((double)ix[l],4.0/3.0)*step;
  1514.            /*   printf("EHHHHHHH !?!?! ---> %d\n",ix[l]); .. table is not big enough */
  1515.             }
  1516.           sum += temp * temp; 
  1517.         }
  1518.         xfsf[0][sfb] = sum / bw;
  1519.     distort[0][sfb] = ( xfsf[0][sfb] > l3_xmin->l[gr][ch][sfb] );
  1520.     if (distort[0][sfb]) over++;
  1521.     noise += xfsf[0][sfb];
  1522.     }
  1523.  
  1524.  
  1525.     for ( i = 0; i < 3; i++ )
  1526.     {
  1527.         step = pow( 2.0, (cod_info->quantizerStepSize) * 0.25 ); /* subblock_gain ? */
  1528.     if (cod_info->subblock_gain[i] )
  1529.       step *= pow(2.0,-2.0*cod_info->subblock_gain[i]);
  1530.  
  1531.         for ( sfb = cod_info->sfb_smax; sfb < 12; sfb++ )
  1532.         {
  1533.             start = scalefac_band_short[ sfb ];
  1534.             end   = scalefac_band_short[ sfb+1 ];
  1535.         bw = end - start;
  1536.             
  1537.             for ( sum = 0.0, l = start; l < end; l++ )
  1538.             {
  1539.                 double temp;
  1540.         if((*ix_s)[l][i]<PRECALC_SIZE)
  1541.                     temp = fabs((*xr_s)[l][i]) - pow43[(*ix_s)[l][i]] * step;
  1542.                 else
  1543.                 {
  1544.                     temp = fabs((*xr_s)[l][i]) - pow((double)(*ix_s)[l][i],4.0/3.0)*step;
  1545.             /*    printf("EHHHHHHH !?!?! ---> %d\n",(*ix_s)[l][i]); */
  1546.                 }
  1547.                 sum += temp * temp;
  1548.             }       
  1549.             xfsf[i+1][sfb] = sum / bw;
  1550.         distort[i+1][sfb] = 
  1551.         ( xfsf[i+1][sfb] > l3_xmin->s[gr][ch][sfb][i] );
  1552.         if (distort[i+1][sfb]) over++;
  1553.         noise += xfsf[i+1][sfb];
  1554.         }
  1555.     }
  1556. return over;
  1557. }
  1558.  
  1559.  
  1560.  
  1561. /*************************************************************************/
  1562. /*            calc_noise2                                                */
  1563. /*************************************************************************/
  1564. /*   Improved version of calc_noise for dual channel.  This routine is */
  1565. /*   used when you are quantizaing mid and side channels using masking */
  1566. /*   thresholds from L and R channels */
  1567.  
  1568. void calc_noise2( double xr[2][576], int ix[2][576], gr_info *cod_info[2],
  1569.         double xfsf[2][4][CBLIMIT], int distort[2][4][CBLIMIT],
  1570.             III_psy_xmin *l3_xmin,int gr,int stereo, int over[2], 
  1571.             double noise[2])
  1572. {
  1573.     int start, end, sfb, l, i;
  1574.     double sum[2],step_s[3][2],step[2],bw;
  1575.  
  1576.     D192_3 *xr_s[2];
  1577.     I192_3 *ix_s[2];
  1578.  
  1579.     #define PRECALC_SIZE 1024 /* WAS 256 !!! */
  1580.     static double pow43[PRECALC_SIZE];
  1581.     int ch;
  1582.     static int init=0;
  1583.     double diff[2], qcoeff;
  1584.  
  1585.     if (init==0) {
  1586.       init++;
  1587.       for(i=0;i<PRECALC_SIZE;i++)
  1588.         pow43[i] = pow((double)i, 4.0/3.0);
  1589.     }
  1590.     
  1591.     
  1592.  
  1593.     /* calc_noise2: we can assume block types of both channels must be the same */
  1594.     if (cod_info[0]->block_type != 2) {
  1595.     for (ch=0 ; ch < stereo ; ch ++ ) {
  1596.       over[ch]=0;
  1597.       noise[ch]=0;
  1598.       step[ch] = pow( 2.0, (cod_info[ch]->quantizerStepSize) * 0.25 );
  1599.     }
  1600.     for ( sfb = 0; sfb < SFB_LMAX-1; sfb++ ) {
  1601.  
  1602.       start = scalefac_band_long[ sfb ];
  1603.       end   = scalefac_band_long[ sfb+1 ];
  1604.       bw = end - start;
  1605.       
  1606.       for (ch=0 ; ch < stereo ; ch ++ ) sum[ch]=0;
  1607.  
  1608.       for ( l = start; l < end; l++ ) {
  1609.  
  1610.     for (ch=0 ; ch < stereo ; ch ++ ) {
  1611.       qcoeff= (ix[ch][l] < PRECALC_SIZE) ?
  1612.         pow43[ix[ch][l]] * step[ch]: 
  1613.         pow((double)ix[ch][l],4.0/3.0)*step[ch];  
  1614.       if (xr[ch][l]<0) qcoeff=-qcoeff;
  1615.       diff[ch]=xr[ch][l]-qcoeff;
  1616.     }
  1617.     sum[0] += (diff[0]+diff[1])*(diff[0]+diff[1])/(2.0);
  1618.     sum[1] += (diff[0]-diff[1])*(diff[0]-diff[1])/(2.0);
  1619.       }
  1620.       for (ch=0 ; ch < stereo ; ch ++ ) {
  1621.     xfsf[ch][0][sfb] = sum[ch] / bw;
  1622.     distort[ch][0][sfb] = ( xfsf[ch][0][sfb] > l3_xmin->l[gr][ch][sfb] );
  1623.     if (distort[ch][0][sfb]) over[ch]++;
  1624.     noise[ch] += xfsf[ch][0][sfb];
  1625.       }
  1626.  
  1627.       /* if there is audible distortion in left or right channel, set flags
  1628.        * to denote distortion in both mid and side channels */
  1629.       for (ch=0 ; ch < stereo ; ch ++ ) {
  1630.     distort[ch][0][sfb] = (distort[0][0][sfb] || distort[1][0][sfb]);
  1631.       }
  1632.     }
  1633.     }
  1634.  
  1635.     /* calc_noise2: we can assume block types of both channels must be the same */
  1636.     if (cod_info[0]->block_type == 2) {
  1637.     for (ch=0 ; ch < stereo ; ch ++ ) {
  1638.  
  1639.       for (i=0;i<3;i++){
  1640.     step_s[i][ch] = pow( 2.0, (cod_info[ch]->quantizerStepSize) * 0.25 ); /* subblock_gain ? */
  1641.     if (cod_info[ch]->subblock_gain[i] )
  1642.       step_s[i][ch] *= pow(2.0,-2.0*cod_info[ch]->subblock_gain[i]);
  1643.       }
  1644.  
  1645.       over[ch] = 0;
  1646.       xr_s[ch] = (D192_3 *) xr[ch];
  1647.       ix_s[ch] = (I192_3 *) ix[ch];
  1648.     }
  1649.  
  1650.     for ( sfb = 0 ; sfb < SFB_SMAX-1; sfb++ ) {
  1651.       start = scalefac_band_short[ sfb ];
  1652.       end   = scalefac_band_short[ sfb+1 ];
  1653.       bw = end - start;
  1654.       for ( i = 0; i < 3; i++ ) {        
  1655.     for (ch=0 ; ch < stereo ; ch ++ ) sum[ch] = 0.0;
  1656.     for ( l = start; l < end; l++ )       {
  1657.       for (ch=0 ; ch < stereo ; ch ++ ) {
  1658.         qcoeff = ((*ix_s[ch])[l][i]<PRECALC_SIZE) ?
  1659.           pow43[(*ix_s[ch])[l][i]] * step_s[i][ch] :
  1660.           pow((double)(*ix_s[ch])[l][i],4.0/3.0)*step_s[i][ch]; 
  1661.         if ((*xr_s[ch])[l][i] < 0) qcoeff=-qcoeff;
  1662.         diff[ch] = (*xr_s[ch])[l][i] - qcoeff; 
  1663.       }
  1664.       sum[0] += (diff[0]+diff[1])*(diff[0]+diff[1])/(2.0);
  1665.       sum[1] += (diff[0]-diff[1])*(diff[0]-diff[1])/(2.0);
  1666.     }
  1667.     for (ch=0 ; ch < stereo ; ch ++ ) {
  1668.       xfsf[ch][i+1][sfb] = sum[ch] / bw;
  1669.       distort[ch][i+1][sfb] = 
  1670.         ( xfsf[ch][i+1][sfb] > l3_xmin->s[gr][ch][sfb][i] );
  1671.       if (distort[ch][i+1][sfb]) over[ch]++;
  1672.       noise[ch] = xfsf[ch][i+1][sfb];
  1673.     }
  1674.     /* if there is audible distortion in left or right channel, set flags
  1675.      * to denote distortion in both mid and side channels */
  1676.     for (ch=0 ; ch < stereo ; ch ++ ) 
  1677.       distort[ch][i+1][sfb] = 
  1678.         (distort[0][i+1][sfb]  || distort[1][i+1][sfb]  );
  1679.       }
  1680.     }
  1681.     }
  1682. }
  1683.     
  1684.  
  1685.  
  1686.  
  1687. /*************************************************************************/
  1688. /*            calc_xmin                                                  */
  1689. /*************************************************************************/
  1690.  
  1691. /*
  1692.   Calculate the allowed distortion for each scalefactor band,
  1693.   as determined by the psychoacoustic model.
  1694.   xmin(sb) = ratio(sb) * en(sb) / bw(sb)
  1695. */
  1696.  
  1697. void calc_xmin( double xr[2][2][576], III_psy_ratio *ratio,
  1698.        gr_info *cod_info, III_psy_xmin *l3_xmin,
  1699.        int gr, int ch )
  1700. {
  1701.     int start, end, sfb, l, b;
  1702.     double en0, bw;
  1703.  
  1704.     D192_3 *xr_s;
  1705.  
  1706.     xr_s = (D192_3 *) xr[gr][ch] ;
  1707.  
  1708.     for ( sfb = cod_info->sfb_smax; sfb < SFB_SMAX - 1; sfb++ )
  1709.     {
  1710.         start = scalefac_band_short[ sfb ];
  1711.         end   = scalefac_band_short[ sfb + 1 ];
  1712.     bw = end - start;
  1713.         for ( b = 0; b < 3; b++ )
  1714.         {
  1715.             for ( en0 = 0.0, l = start; l < end; l++ )
  1716.                 en0 += (*xr_s)[l][b] * (*xr_s)[l][b];
  1717.             l3_xmin->s[gr][ch][sfb][b] = ratio->s[gr][ch][sfb][b] * en0 / bw;
  1718.         }
  1719.     }
  1720.  
  1721.     for ( sfb = 0; sfb < cod_info->sfb_lmax; sfb++ )
  1722.     {
  1723.         start = scalefac_band_long[ sfb ];
  1724.         end   = scalefac_band_long[ sfb+1 ];
  1725.     bw = end - start;
  1726.  
  1727.         for ( en0 = 0.0, l = start; l < end; l++ )
  1728.             en0 += xr[gr][ch][l] * xr[gr][ch][l];
  1729.         l3_xmin->l[gr][ch][sfb] = ratio->l[gr][ch][sfb] * en0 / bw;
  1730.     }
  1731. }
  1732.  
  1733.  
  1734.  
  1735. /*************************************************************************/
  1736. /*            loop_break                                                 */
  1737. /*************************************************************************/
  1738.  
  1739. /*  Function: Returns zero if there is a scalefac which has not been
  1740.     amplified. Otherwise it returns one. 
  1741. */
  1742.  
  1743. int loop_break( III_scalefac_t *scalefac, gr_info *cod_info,
  1744.         int gr, int ch )
  1745. {
  1746.     int i, sfb;
  1747.  
  1748.     for ( sfb = 0; sfb < cod_info->sfb_lmax; sfb++ )
  1749.         if ( scalefac->l[gr][ch][sfb] == 0 )
  1750.         return 0;
  1751.  
  1752.     for ( sfb = cod_info->sfb_smax; sfb < 12; sfb++ )
  1753.         for ( i = 0; i < 3; i++ )
  1754.             if ( scalefac->s[gr][ch][sfb][i] == 0 )
  1755.         return 0;
  1756.  
  1757.     return 1;
  1758. }
  1759.  
  1760.  
  1761.  
  1762. /*************************************************************************/
  1763. /*            preemphasis                                                */
  1764. /*************************************************************************/
  1765.  
  1766. /*
  1767.   See ISO 11172-3  section  C.1.5.4.3.4
  1768. */
  1769.  
  1770. void preemphasis( double xr[576], double xfsf[4][CBLIMIT],
  1771.          III_psy_xmin  *l3_xmin,
  1772.          int gr, int ch, III_side_info_t *l3_side )
  1773. {
  1774.     int i, sfb, start, end, scfsi_band, over;
  1775.     double ifqstep;
  1776.     gr_info *cod_info = &l3_side->gr[gr].ch[ch].tt;
  1777.  
  1778.     if ( gr == 1 )
  1779.     {
  1780.     /*
  1781.       If the second granule is being coded and scfsi is active in
  1782.       at least one scfsi_band, the preemphasis in the second granule
  1783.       is set equal to the setting in the first granule
  1784.     */
  1785.     for ( scfsi_band = 0; scfsi_band < 4; scfsi_band++ )
  1786.         if ( l3_side->scfsi[ch][scfsi_band] )
  1787.         {
  1788.         cod_info->preflag = l3_side->gr[0].ch[ch].tt.preflag;
  1789.         return;
  1790.         }
  1791.     
  1792.     }
  1793.  
  1794.     /*
  1795.       Preemphasis is switched on if in all the upper four scalefactor
  1796.       bands the actual distortion exceeds the threshold after the
  1797.       first call of the inner loop
  1798.     */
  1799.     if ( cod_info->block_type != 2 && cod_info->preflag == 0 )
  1800.     {    
  1801.     over = 0;
  1802.     for ( sfb = 17; sfb < 21; sfb++ )
  1803.         if ( xfsf[0][sfb] > l3_xmin->l[gr][ch][sfb] )
  1804.         over++;
  1805.  
  1806.     if (over == 4 )
  1807.     {
  1808.         double t;
  1809.         cod_info->preflag = 1;
  1810.         ifqstep = ( cod_info->scalefac_scale == 0 ) ? sqrt(2.) : 2.0;
  1811.         for ( sfb = 0; sfb < cod_info->sfb_lmax; sfb++ )
  1812.         {
  1813.         t=pow( ifqstep, (double) pretab[sfb] );
  1814.         l3_xmin->l[gr][ch][sfb] *= t*t;
  1815.         start = scalefac_band_long[ sfb ];
  1816.         end   = scalefac_band_long[ sfb+1 ];
  1817.         for( i = start; i < end; i++ )
  1818.             xr[i]*=t;
  1819.         }
  1820.     }
  1821.     }
  1822. }
  1823.  
  1824.  
  1825. /*************************************************************************/
  1826. /*            amp_scalefac_bands                                         */
  1827. /*************************************************************************/
  1828.  
  1829. /* 
  1830.   Amplify the scalefactor bands that violate the masking threshold.
  1831.   See ISO 11172-3 Section C.1.5.4.3.5
  1832. */
  1833.  
  1834. int amp_scalefac_bands( double xr[576], double xfsf[4][CBLIMIT],
  1835.             III_psy_xmin *l3_xmin, III_side_info_t *l3_side,
  1836.             III_scalefac_t *scalefac,
  1837.             int gr, int ch, int iteration)
  1838. {
  1839.     int start, end, l, sfb, i, scfsi_band, over = 0;
  1840.     double ifqstep, ifqstep2;
  1841.     D192_3 *xr_s;
  1842.     gr_info *cod_info, *gr0;
  1843.     int copySF, preventSF;
  1844.     cod_info = &l3_side->gr[gr].ch[ch].tt;
  1845.     gr0      = &l3_side->gr[0].ch[ch].tt;
  1846.  
  1847.     xr_s = (D192_3 *) xr;
  1848.     copySF = 0;
  1849.     preventSF = 0;
  1850.  
  1851.  
  1852.     if ( cod_info->scalefac_scale == 0 )
  1853.     ifqstep = sqrt( 2.0 );
  1854.     else
  1855.     ifqstep = 2.0;
  1856.  
  1857.     if ( gr == 1 )
  1858.     {
  1859.     /*
  1860.       If the second granule is being coded and scfsi is active in at
  1861.       least one scfsi_band...
  1862.     */
  1863.     for ( scfsi_band = 0; scfsi_band < 4; scfsi_band++ )
  1864.         if ( l3_side->scfsi[ch][scfsi_band] )
  1865.         {
  1866.         /*
  1867.           a) ifqstep has to be set similar to the
  1868.            first granule...
  1869.         */
  1870.         if ( gr0->scalefac_scale == 0 )
  1871.             ifqstep = sqrt( 2.0 );
  1872.         else
  1873.             ifqstep = 2.0;
  1874.  
  1875.         if ( iteration == 1 )
  1876.         {
  1877.             /*
  1878.               b) If it is the first iteration, the scalefactors
  1879.               of scalefactor bands in which scfsi is enabled
  1880.               must be taken from the first granule
  1881.             */  
  1882.             copySF = 1;
  1883.         }
  1884.         else
  1885.         {
  1886.             /*
  1887.               c) If it is not the first iteration, the amplification
  1888.               must be prevented for scalefactor bands in which
  1889.               scfsi is enabled
  1890.             */
  1891.             preventSF = 1;
  1892.         }
  1893.         break;
  1894.         }
  1895.     
  1896.     }
  1897.  
  1898.     ifqstep2 = ifqstep * ifqstep;
  1899.     scfsi_band = 0;
  1900.     
  1901.     for ( sfb = 0; sfb < cod_info->sfb_lmax; sfb++ )
  1902.     {
  1903.     if ( copySF || preventSF )
  1904.     {
  1905.         if ( sfb == scfsi_band_long[scfsi_band + 1] )
  1906.         scfsi_band += 1;
  1907.         if ( l3_side->scfsi[ch][scfsi_band] )
  1908.         {
  1909.         if ( copySF )
  1910.             scalefac->l[gr][ch][sfb] = scalefac->l[0][ch][sfb];
  1911.         continue;
  1912.         }
  1913.     }        
  1914.  
  1915.  
  1916.     if ( xfsf[0][sfb] > l3_xmin->l[gr][ch][sfb] )
  1917.     {
  1918.         over++;
  1919.         l3_xmin->l[gr][ch][sfb] *= ifqstep2;
  1920.         scalefac->l[gr][ch][sfb]++;
  1921.         start = scalefac_band_long[sfb];
  1922.         end   = scalefac_band_long[sfb+1];
  1923.         for ( l = start; l < end; l++ )
  1924.         xr[l] *= ifqstep;
  1925.     }
  1926.     }
  1927.  
  1928.     /*
  1929.       Note that scfsi is not enabled for frames containing
  1930.       short blocks
  1931.     */
  1932.     for ( i = 0; i < 3; i++ )
  1933.       for ( sfb = cod_info->sfb_smax; sfb < 12; sfb++ ) {
  1934.             if ( xfsf[i+1][sfb] > l3_xmin->s[gr][ch][sfb][i] )
  1935.             {
  1936.                 over++;
  1937.                 l3_xmin->s[gr][ch][sfb][i] *= ifqstep2;
  1938.                 scalefac->s[gr][ch][sfb][i]++;
  1939. #ifdef DEBUGSC
  1940.                 printf( "cod_info->scalefac[%d][%d] = %d (amp_scale)\n",
  1941.                         i,sfb,scalefac->s[gr][ch][sfb][i] );
  1942. #endif
  1943.                 start = scalefac_band_short[sfb];
  1944.                 end   = scalefac_band_short[sfb+1];
  1945.                 for ( l = start; l < end; l++ )
  1946.                     (*xr_s)[l][i] *= ifqstep;
  1947.             }
  1948.       }
  1949.     return over;
  1950. }
  1951.  
  1952.  
  1953.  
  1954. /*************************************************************************/
  1955. /*            preemphasis                                                */
  1956. /*************************************************************************/
  1957.  
  1958. /*
  1959.   See ISO 11172-3  section  C.1.5.4.3.4
  1960. */
  1961. int preemphasis2( double xr[576], double xrpow[576], 
  1962.      III_psy_xmin  *l3_xmin,
  1963.      int gr, int ch, III_side_info_t *l3_side, int distort[4][CBLIMIT] )
  1964. {
  1965.     int i, sfb, start, end, scfsi_band, over;
  1966.     double ifqstep;
  1967.     gr_info *cod_info = &l3_side->gr[gr].ch[ch].tt;
  1968.  
  1969.     if ( gr == 1 )
  1970.     {
  1971.     /*
  1972.       If the second granule is being coded and scfsi is active in
  1973.       at least one scfsi_band, the preemphasis in the second granule
  1974.       is set equal to the setting in the first granule
  1975.     */
  1976.     for ( scfsi_band = 0; scfsi_band < 4; scfsi_band++ )
  1977.         if ( l3_side->scfsi[ch][scfsi_band] )
  1978.         {
  1979.         cod_info->preflag = l3_side->gr[0].ch[ch].tt.preflag;
  1980.         return 0;
  1981.         }
  1982.     
  1983.     }
  1984.  
  1985.     /*
  1986.       Preemphasis is switched on if in all the upper four scalefactor
  1987.       bands the actual distortion exceeds the threshold after the
  1988.       first call of the inner loop
  1989.     */
  1990.     over = 0;
  1991.     if ( cod_info->block_type != 2 && cod_info->preflag == 0 )
  1992.     {    
  1993.     for ( sfb = 17; sfb < 21; sfb++ )
  1994.         if ( distort[0][sfb] ) over++;
  1995.  
  1996.     if (over == 4 )
  1997.     {
  1998.         double t,t34;
  1999.         cod_info->preflag = 1;
  2000.         ifqstep = ( cod_info->scalefac_scale == 0 ) ? sqrt(2.) : 2.0;
  2001.         for ( sfb = 11; sfb < cod_info->sfb_lmax; sfb++ )
  2002.         {
  2003.         t=pow( ifqstep, (double) pretab[sfb] );
  2004.         t34=sqrt(sqrt(t)*t);
  2005.         l3_xmin->l[gr][ch][sfb] *= t*t;
  2006.         start = scalefac_band_long[ sfb ];
  2007.         end   = scalefac_band_long[ sfb+1 ];
  2008.         for( i = start; i < end; i++ ) xr[i]*=t;
  2009.         if (xrpow_flag)
  2010.           for( i = start; i < end; i++ ) xrpow[i]*=t34;
  2011.         }
  2012.     }
  2013.     }
  2014.     return (over == 4);
  2015. }
  2016.  
  2017.  
  2018.  
  2019.  
  2020. /*************************************************************************/
  2021. /*            amp_scalefac_bands                                         */
  2022. /*************************************************************************/
  2023.  
  2024. /* 
  2025.   Amplify the scalefactor bands that violate the masking threshold.
  2026.   See ISO 11172-3 Section C.1.5.4.3.5
  2027. */
  2028. int amp_scalefac_bands2( double xr[576], double xrpow[576], 
  2029.             III_psy_xmin *l3_xmin, III_side_info_t *l3_side,
  2030.             III_scalefac_t *scalefac,
  2031.             int gr, int ch, int iteration, int distort[4][CBLIMIT])
  2032. {
  2033.     int start, end, l, sfb, i, scfsi_band, over = 0;
  2034.     double ifqstep, ifqstep2, ifqstep34;
  2035.     D192_3 *xr_s;
  2036.     D192_3 *xrpow_s;
  2037.     gr_info *cod_info, *gr0;
  2038.     int copySF, preventSF;
  2039.     cod_info = &l3_side->gr[gr].ch[ch].tt;
  2040.     gr0      = &l3_side->gr[0].ch[ch].tt;
  2041.  
  2042.     xr_s = (D192_3 *) xr;
  2043.     xrpow_s = (D192_3 *) xrpow;
  2044.     copySF = 0;
  2045.     preventSF = 0;
  2046.  
  2047.  
  2048.     if ( cod_info->scalefac_scale == 0 )
  2049.     ifqstep = sqrt( 2.0 );
  2050.     else
  2051.     ifqstep = 2.0;
  2052.  
  2053.     if ( gr == 1 )
  2054.     {
  2055.     /*
  2056.       If the second granule is being coded and scfsi is active in at
  2057.       least one scfsi_band...
  2058.     */
  2059.     for ( scfsi_band = 0; scfsi_band < 4; scfsi_band++ )
  2060.         if ( l3_side->scfsi[ch][scfsi_band] )
  2061.         {
  2062.         /*
  2063.           a) ifqstep has to be set similar to the
  2064.            first granule...
  2065.         */
  2066.         if ( gr0->scalefac_scale == 0 )
  2067.             ifqstep = sqrt( 2.0 );
  2068.         else
  2069.             ifqstep = 2.0;
  2070.  
  2071.         if ( iteration == 1 )
  2072.         {
  2073.             /*
  2074.               b) If it is the first iteration, the scalefactors
  2075.               of scalefactor bands in which scfsi is enabled
  2076.               must be taken from the first granule
  2077.             */  
  2078.             copySF = 1;
  2079.         }
  2080.         else
  2081.         {
  2082.             /*
  2083.               c) If it is not the first iteration, the amplification
  2084.               must be prevented for scalefactor bands in which
  2085.               scfsi is enabled
  2086.             */
  2087.             preventSF = 1;
  2088.         }
  2089.         break;
  2090.         }
  2091.     
  2092.     }
  2093.  
  2094.     ifqstep2 = ifqstep * ifqstep;
  2095.     scfsi_band = 0;
  2096.     ifqstep34=sqrt(sqrt(ifqstep)*ifqstep);
  2097.     
  2098.     for ( sfb = 0; sfb < cod_info->sfb_lmax; sfb++ )
  2099.     {
  2100.     if ( copySF || preventSF )
  2101.     {
  2102.         if ( sfb == scfsi_band_long[scfsi_band + 1] )
  2103.         scfsi_band += 1;
  2104.         if ( l3_side->scfsi[ch][scfsi_band] )
  2105.         {
  2106.         if ( copySF )
  2107.             scalefac->l[gr][ch][sfb] = scalefac->l[0][ch][sfb];
  2108.         continue;
  2109.         }
  2110.     }        
  2111.  
  2112.  
  2113.     if ( distort[0][sfb]  ) 
  2114.     {
  2115.         over++;
  2116.         l3_xmin->l[gr][ch][sfb] *= ifqstep2;
  2117.         scalefac->l[gr][ch][sfb]++;
  2118.         start = scalefac_band_long[sfb];
  2119.         end   = scalefac_band_long[sfb+1];
  2120.         for ( l = start; l < end; l++ ) xr[l] *= ifqstep;
  2121.         if (xrpow_flag)
  2122.           for ( l = start; l < end; l++ ) xrpow[l] *= ifqstep34;
  2123.     }
  2124.     }
  2125.  
  2126.     /*
  2127.       Note that scfsi is not enabled for frames containing
  2128.       short blocks
  2129.     */
  2130.     for ( i = 0; i < 3; i++ )
  2131.       for ( sfb = cod_info->sfb_smax; sfb < 12; sfb++ ) {
  2132.             if ( distort[i+1][sfb])
  2133.             {
  2134.                 over++;
  2135.                 l3_xmin->s[gr][ch][sfb][i] *= ifqstep2;
  2136.                 scalefac->s[gr][ch][sfb][i]++;
  2137. #ifdef DEBUGSC
  2138.                 printf( "cod_info->scalefac[%d][%d] = %d (amp_scale)\n",
  2139.                         i,sfb,scalefac->s[gr][ch][sfb][i] );
  2140. #endif
  2141.                 start = scalefac_band_short[sfb];
  2142.                 end   = scalefac_band_short[sfb+1];
  2143.                 for ( l = start; l < end; l++ ) (*xr_s)[l][i] *= ifqstep;
  2144.         if (xrpow_flag) 
  2145.           for ( l = start; l < end; l++ ) (*xrpow_s)[l][i] *= ifqstep34;
  2146.             }
  2147.       }
  2148.     return over;
  2149. }
  2150.  
  2151.  
  2152.  
  2153.  
  2154.  
  2155.  
  2156. /*************************************************************************/
  2157. /*            quantize                                                   */
  2158. /*************************************************************************/
  2159.  
  2160. /*
  2161.   Function: Quantization of the vector xr ( -> ix)
  2162. */
  2163.  
  2164. void quantize( double xr[576], int ix[576], gr_info *cod_info )
  2165. {
  2166.     register int i,b;
  2167.     int j;
  2168.     double quantizerStepSize; /* double step */
  2169.     double istep_l,istep[3],temp;
  2170.     static int init=0;
  2171. #define LUTABSIZE 10000
  2172.     static int lutab[LUTABSIZE];
  2173.  
  2174.     if (init==0) {
  2175.       init++;
  2176.       for (i=0;i<LUTABSIZE;i++) {
  2177.     lutab[i]=nint(pow((double)i/10.0,0.75)-0.0946);
  2178.       }
  2179.       for (i=1;i<LUTABSIZE;i++) 
  2180.     if ((lutab[i]-lutab[i-1])==1) {  /* we have a change over this interval */
  2181.       lutab[i-1]=-1;
  2182.     }
  2183.     }
  2184.  
  2185.     quantizerStepSize = cod_info->quantizerStepSize;
  2186.     /* if subblock gain is used (short blocks), this should be: */
  2187.     /* step = pow( 2.0, (quantizerStepSize - 8.0 * (double) cod_info->subblock_gain[b]) * -0.25 ); */
  2188.     /* step = pow( 2.0, quantizerStepSize * -0.25) * 
  2189.        pow( 2.0, 2*(double) cod_info->subblock_gain[b]) */
  2190.  
  2191.     if ( quantizerStepSize == 0.0 )
  2192.     istep_l = 1.0;
  2193.     else
  2194.     istep_l = pow ( 2.0, quantizerStepSize * -0.25 );
  2195.  
  2196.     for (b=0;b<3;b++) {
  2197.       if ((cod_info->block_type==2))
  2198.     istep[b] = istep_l * pow(2.0,2* (double) cod_info->subblock_gain[b]);
  2199.       else istep[b] = istep_l;
  2200.     }
  2201.  
  2202.  
  2203.     i=0;
  2204.     for (j=0;j<192;j++) 
  2205.       for (b=0;b<3;b++) {
  2206.     temp=istep[b]*fabs(xr[i]); /* step always positive -> temp always postive */
  2207.     if (temp<0.499996)
  2208.       ix[i]=0;
  2209.     else
  2210.        if (temp<1.862955)
  2211.      ix[i]=1;
  2212.        else
  2213.        if (temp<3.565282)
  2214.      ix[i]=2;
  2215.        else
  2216.        if (temp<5.506396)
  2217.      ix[i]=3;
  2218.        else
  2219.        if (temp<7.638304)
  2220.      ix[i]=4;
  2221.        else
  2222.        if (temp<9.931741)
  2223.      ix[i]=5;
  2224.        else
  2225.        if (temp<1000.0) {
  2226.      ix[i]=lutab[(int)(temp*10.0)];
  2227.      if (ix[i]==-1) /* too close to an interface, calculate exact value */
  2228.        ix[i] = (int)( sqrt(sqrt(temp)*temp) + 0.4054);
  2229.        }
  2230.        else
  2231.      ix[i] = (int)( sqrt(sqrt(temp)*temp) + 0.4054); 
  2232.        i++;
  2233.       }
  2234.  
  2235. }
  2236.  
  2237.  
  2238. void quantize_xrpow( double xr[576], int ix[576], gr_info *cod_info )
  2239. {
  2240.   /* quantize on xr^(3/4) instead of xr */
  2241.     register int i,b,j;
  2242.     double quantizerStepSize;
  2243.     double istep_l,istep[3];
  2244.  
  2245.     quantizerStepSize = cod_info->quantizerStepSize;
  2246.  
  2247.     /*    istep_l = pow ( 2.0, quantizerStepSize * -0.25 ); */
  2248.     istep_l = pow ( 2.0, quantizerStepSize * -0.1875 );
  2249.  
  2250.  
  2251.     for (b=0;b<3;b++) {
  2252.       if ((cod_info->block_type==2))
  2253.     istep[b] = istep_l * pow(2.0,1.5* (double) cod_info->subblock_gain[b]);
  2254.       else istep[b] = istep_l;
  2255.     }
  2256.  
  2257.     i=0;
  2258.     for (j=0;j<192;j++) 
  2259.       for (b=0;b<3;b++) {
  2260.         ix[i] = (int)( istep[b]*xr[i]  + 0.4054);
  2261.         i++;
  2262.     }
  2263.  
  2264. }
  2265.  
  2266.  
  2267.  
  2268.  
  2269.  
  2270.  
  2271.  
  2272.  
  2273. /*************************************************************************/
  2274. /*            ix_max                                                     */
  2275. /*************************************************************************/
  2276.  
  2277. /*
  2278.   Function: Calculate the maximum of ix from 0 to 575
  2279. */
  2280.  
  2281. int ix_max( int ix[576], unsigned int begin, unsigned int end )
  2282. {
  2283.     int i, max = 0;
  2284.  
  2285.     for ( i = begin; i < end; i++ )
  2286.     {
  2287.         int x =  ix[i];
  2288.         if ( x > max )
  2289.             max = x;
  2290.     }
  2291.     return max;
  2292. }
  2293.  
  2294.  
  2295. /*************************************************************************/
  2296. /*            xr_max                                                     */
  2297. /*************************************************************************/
  2298.  
  2299. /*
  2300.   Function: Calculate the maximum of xr[576]  from 0 to 575
  2301. */
  2302.  
  2303. double xr_max( double xr[576], unsigned int begin, unsigned int end )
  2304. {
  2305.     int i;
  2306.     double max = 0.0, temp;
  2307.  
  2308.     for ( i = begin; i < end; i++ )
  2309.         if( (temp = fabs(xr[i])) > max )
  2310.         max = temp;
  2311.     return max;
  2312. }
  2313.  
  2314.  
  2315.  
  2316. /*        Noiseless coding  -- Huffman coding   */
  2317.  
  2318.  
  2319. /*************************************************************************/
  2320. /*            calc_runlen                                                */
  2321. /*************************************************************************/
  2322.  
  2323. /*
  2324. Function: Calculation of  count1, big_values
  2325. (Partitions ix into big values, quadruples and zeros).
  2326. */
  2327.  
  2328. void calc_runlen( int ix[576], gr_info *cod_info )
  2329. {
  2330.     int i;
  2331.  
  2332.     if ( cod_info->window_switching_flag && (cod_info->block_type == 2) )
  2333.     {  /* short blocks */
  2334.         cod_info->count1 = 0;
  2335.         cod_info->big_values = 288;
  2336.     }
  2337.     else
  2338.     {
  2339.         for ( i = 576; i > 1; i -= 2 )
  2340.         if ( ix[i-1] | ix[i-2] ) break;
  2341.  
  2342.         cod_info->count1 = 0 ;
  2343.         for ( ; i > 3; i -= 4 )
  2344.         if ( (ix[i-1] | ix[i-2] | ix[i-3] | ix[i-4]) <= 1 )
  2345.                 cod_info->count1++;
  2346.             else
  2347.                 break;
  2348.         
  2349.         cod_info->big_values = i/2;
  2350.     }
  2351. }
  2352.  
  2353.  
  2354.  
  2355. /*************************************************************************/
  2356. /*            count1_bitcount                                            */
  2357. /*************************************************************************/
  2358.  
  2359. /*
  2360.   Determines the number of bits to encode the quadruples.
  2361. */
  2362.  
  2363. int count1_bitcount( int ix[ 576 ], gr_info *cod_info )
  2364. {
  2365.     int abs_and_sign( int *x );
  2366.  
  2367.     int p, i, k, bitsum_count1;
  2368.     int v, w, x, y, signbits;
  2369.     int sum0 = 0, sum1 = 0;
  2370.  
  2371.     for ( i = cod_info->big_values * 2, k = 0; k < cod_info->count1; i += 4, k++ )
  2372.     { 
  2373.     v = ix[i];
  2374.     w = ix[i+1];
  2375.     x = ix[i+2];
  2376.     y = ix[i+3];
  2377.  
  2378.       /*
  2379.         v = ix[ i ];
  2380.         w = ix[ i + 1 ];
  2381.         x = ix[ i + 2 ];
  2382.         y = ix[ i + 3 ];
  2383.         
  2384.  
  2385.         abs_and_sign( &v );
  2386.         abs_and_sign( &w );
  2387.         abs_and_sign( &x );
  2388.         abs_and_sign( &y );
  2389.     */
  2390.         p = v + (w << 1) + (x << 2) + (y << 3);
  2391.         
  2392.         signbits = 0;
  2393.  
  2394.         if ( v != 0 )
  2395.             signbits ++;
  2396.         if ( w != 0 )
  2397.             signbits ++;
  2398.         if ( x != 0 )
  2399.             signbits ++;
  2400.         if ( y != 0 )
  2401.             signbits ++;
  2402.  
  2403.         sum0 += signbits;
  2404.         sum1 += signbits;
  2405.  
  2406.         sum0 += ht[ 32 ].hlen[ p ];
  2407.         sum1 += ht[ 33 ].hlen[ p ];
  2408.     }
  2409.  
  2410.     if ( sum0 < sum1 )
  2411.     {
  2412.         bitsum_count1 = sum0;
  2413.         cod_info->count1table_select = 0;
  2414.     }
  2415.     else
  2416.     {
  2417.         bitsum_count1 = sum1;
  2418.         cod_info->count1table_select = 1;
  2419.     }
  2420.     return( bitsum_count1 );
  2421. }
  2422.  
  2423.  
  2424.  
  2425.  
  2426.  
  2427. struct
  2428. {
  2429.     unsigned region0_count;
  2430.     unsigned region1_count;
  2431. } subdv_table[ 23 ] =
  2432. {
  2433. {0, 0}, /* 0 bands */
  2434. {0, 0}, /* 1 bands */
  2435. {0, 0}, /* 2 bands */
  2436. {0, 0}, /* 3 bands */
  2437. {0, 0}, /* 4 bands */
  2438. {0, 1}, /* 5 bands */
  2439. {1, 1}, /* 6 bands */
  2440. {1, 1}, /* 7 bands */
  2441. {1, 2}, /* 8 bands */
  2442. {2, 2}, /* 9 bands */
  2443. {2, 3}, /* 10 bands */
  2444. {2, 3}, /* 11 bands */
  2445. {3, 4}, /* 12 bands */
  2446. {3, 4}, /* 13 bands */
  2447. {3, 4}, /* 14 bands */
  2448. {4, 5}, /* 15 bands */
  2449. {4, 5}, /* 16 bands */
  2450. {4, 6}, /* 17 bands */
  2451. {5, 6}, /* 18 bands */
  2452. {5, 6}, /* 19 bands */
  2453. {5, 7}, /* 20 bands */
  2454. {6, 7}, /* 21 bands */
  2455. {6, 7}, /* 22 bands */
  2456. };
  2457.  
  2458.  
  2459.  
  2460.  
  2461. /*************************************************************************/
  2462. /*            subdivide                                                  */
  2463. /*************************************************************************/
  2464.  
  2465. /* presumable subdivides the bigvalue region which will
  2466.    use separate Huffman tables.
  2467. */
  2468.  
  2469. void subdivide( gr_info *cod_info )
  2470. {
  2471.     int scfb_anz = 0;
  2472.     int bigvalues_region;
  2473.     
  2474.     if ( cod_info->big_values == 0 )
  2475.     { /* no big_values region */
  2476.         cod_info->region0_count = 0;
  2477.         cod_info->region1_count = 0;
  2478.     }
  2479.     else
  2480.     {
  2481.         bigvalues_region = 2 * cod_info->big_values;
  2482.  
  2483.         if ( (cod_info->window_switching_flag == 0) )
  2484.         { /* long blocks */
  2485.             int thiscount, index;
  2486.             /* Calculate scfb_anz */
  2487.             while ( scalefac_band_long[scfb_anz] < bigvalues_region )
  2488.                 scfb_anz++;
  2489.             assert( scfb_anz < 23 );
  2490.  
  2491.             cod_info->region0_count = subdv_table[scfb_anz].region0_count;
  2492.             thiscount = cod_info->region0_count;
  2493.             index = thiscount + 1;
  2494.             while ( thiscount && (scalefac_band_long[index] > bigvalues_region) )
  2495.             {
  2496.                 thiscount -= 1;
  2497.                 index -= 1;
  2498.             }
  2499.             cod_info->region0_count = thiscount;
  2500.  
  2501.             cod_info->region1_count = subdv_table[scfb_anz].region1_count;
  2502.             index = cod_info->region0_count + cod_info->region1_count + 2;
  2503.             thiscount = cod_info->region1_count;
  2504.             while ( thiscount && (scalefac_band_long[index] > bigvalues_region) )
  2505.             {
  2506.                 thiscount -= 1;
  2507.                 index -= 1;
  2508.             }
  2509.             cod_info->region1_count = thiscount;
  2510.             cod_info->address1 = scalefac_band_long[cod_info->region0_count+1];
  2511.             cod_info->address2 = scalefac_band_long[cod_info->region0_count
  2512.                                                     + cod_info->region1_count + 2 ];
  2513.             cod_info->address3 = bigvalues_region;
  2514.         }
  2515.         else
  2516.         {
  2517.             if ( (cod_info->block_type == 2)  )
  2518.             { 
  2519.                 cod_info->region0_count =  8;
  2520.                 cod_info->region1_count =  36;
  2521.                 cod_info->address1 = 36;
  2522.                 cod_info->address2 = bigvalues_region;
  2523.                 cod_info->address3 = 0;  
  2524.             }
  2525.             else
  2526.             {
  2527.                 cod_info->region0_count = 7;
  2528.                 cod_info->region1_count = 13;
  2529.                 cod_info->address1 = scalefac_band_long[cod_info->region0_count+1];
  2530.                 cod_info->address2 = bigvalues_region;
  2531.                 cod_info->address3 = 0;
  2532.             }
  2533.         }
  2534.     }
  2535. }
  2536.  
  2537.  
  2538.  
  2539.  
  2540. /*************************************************************************/
  2541. /*            new_choose table                                           */
  2542. /*************************************************************************/
  2543.  
  2544. /*
  2545.   Choose the Huffman table that will encode ix[begin..end] with
  2546.   the fewest bits.
  2547.  
  2548.   Note: This code contains knowledge about the sizes and characteristics
  2549.   of the Huffman tables as defined in the IS (Table B.7), and will not work
  2550.   with any arbitrary tables.
  2551. */
  2552.  
  2553. int new_choose_table( int ix[576], unsigned int begin, unsigned int end, int * s )
  2554. {
  2555.     int i, max;
  2556.     int choice[ 2 ];
  2557.     int sum[ 2 ];
  2558.  
  2559.     max = ix_max( ix, begin, end );
  2560.  
  2561.     if ( max == 0 )
  2562.         return 0;
  2563.  
  2564.     choice[ 0 ] = 0;
  2565.     choice[ 1 ] = 0;
  2566.  
  2567.     if ( max < 15 )
  2568.     {
  2569.     /* try tables with no linbits */
  2570.         for ( i = 0; i < 14; i++ )
  2571.             if ( ht[i].xlen > max )
  2572.         {
  2573.         choice[ 0 ] = i;
  2574.                 break;
  2575.         }
  2576.  
  2577.     sum[ 0 ] = count_bit( ix, begin, end, choice[0] );
  2578.  
  2579.     switch ( choice[0] )
  2580.     {
  2581.       case 2:
  2582.         sum[ 1 ] = count_bit( ix, begin, end, 3 );
  2583.         if ( sum[1] <= sum[0] )
  2584.         {
  2585.         sum[0]=sum[1];
  2586.         choice[ 0 ] = 3;
  2587.         }
  2588.         break;
  2589.  
  2590.       case 5:
  2591.         sum[ 1 ] = count_bit( ix, begin, end, 6 );
  2592.         if ( sum[1] <= sum[0] )
  2593.         {
  2594.         sum[0]=sum[1];
  2595.         choice[ 0 ] = 6;
  2596.         }
  2597.         break;
  2598.  
  2599.       case 7:
  2600.         sum[ 1 ] = count_bit( ix, begin, end, 8 );
  2601.         if ( sum[1] <= sum[0] )
  2602.         {
  2603.         choice[ 0 ] = 8;
  2604.         sum[ 0 ] = sum[ 1 ];
  2605.         }
  2606.         sum[ 1 ] = count_bit( ix, begin, end, 9 );
  2607.         if ( sum[1] <= sum[0] )
  2608.         {
  2609.         sum[0]=sum[1];
  2610.         choice[ 0 ] = 9;
  2611.         }
  2612.         break;
  2613.  
  2614.       case 10:
  2615.         sum[ 1 ] = count_bit( ix, begin, end, 11 );
  2616.         if ( sum[1] <= sum[0] )
  2617.         {
  2618.         choice[ 0 ] = 11;
  2619.         sum[ 0 ] = sum[ 1 ];
  2620.         }
  2621.         sum[ 1 ] = count_bit( ix, begin, end, 12 );
  2622.         if ( sum[1] <= sum[0] )
  2623.         {
  2624.         sum[0]=sum[1];
  2625.         choice[ 0 ] = 12;
  2626.         }
  2627.         break;
  2628.  
  2629.       case 13:
  2630.         sum[ 1 ] = count_bit( ix, begin, end, 15 );
  2631.         if ( sum[1] <= sum[0] )
  2632.         {
  2633.         sum[0]=sum[1];
  2634.         choice[ 0 ] = 15;
  2635.         }
  2636.         break;
  2637.  
  2638.       default:
  2639.         break;
  2640.     }
  2641.     }
  2642.     else
  2643.     {
  2644.     /* try tables with linbits */
  2645.     max -= 15;
  2646.     
  2647.     for ( i = 15; i < 24; i++ )
  2648.     {
  2649.         if ( ht[i].linmax >= max )
  2650.         {
  2651.         choice[ 0 ] = i;
  2652.         break;
  2653.         }
  2654.     }
  2655.     for ( i = 24; i < 32; i++ )
  2656.     {
  2657.         if ( ht[i].linmax >= max )
  2658.         {
  2659.         choice[ 1 ] = i;
  2660.         break;
  2661.         }
  2662.     }
  2663.     
  2664.     sum[ 0 ] = count_bit( ix, begin, end, choice[0] );
  2665.     sum[ 1 ] = count_bit( ix, begin, end, choice[1] );
  2666.     if ( sum[1] < sum[0] )
  2667.     {
  2668.         sum[0]=sum[1];
  2669.         choice[ 0 ] = choice[ 1 ];
  2670.     }
  2671.     }
  2672.     *s=sum[0];
  2673.     return choice[ 0 ];
  2674. }
  2675.  
  2676.  
  2677. /*************************************************************************/
  2678. /*            choose table                                               */
  2679. /*************************************************************************/
  2680.  
  2681. int choose_table( int max )
  2682. {
  2683.     int  i, choice;
  2684.  
  2685.     if ( max == 0 )
  2686.         return 0;
  2687.     
  2688.     choice = 0;
  2689.  
  2690.     if ( max < 15 )
  2691.     {
  2692.         for ( i = 0; i < 15; i++ )
  2693.         {
  2694.             if ( ht[i].xlen > max )
  2695.             {
  2696.         choice = i;
  2697.         break;
  2698.             }
  2699.         }
  2700.     }
  2701.     else
  2702.     {    
  2703.     max -= 15;
  2704.     for (i = 15; i < 32; i++ )
  2705.     {
  2706.         if ( ht[i].linmax >= max )
  2707.         {
  2708.         choice = i;
  2709.         break;
  2710.         }
  2711.     }
  2712.     }
  2713.     assert( choice );
  2714.     return choice;
  2715. }
  2716.  
  2717.  
  2718.  
  2719. /*************************************************************************/
  2720. /*            bigv_bitcount                                              */
  2721. /*************************************************************************/
  2722.  
  2723. /*
  2724. Function: Select huffman code tables for bigvalues regions 
  2725. Function: Count the number of bits necessary to code the bigvalues region.
  2726. */
  2727.  
  2728. int bigv_bitcount( int ix[576], gr_info *cod_info )
  2729. {
  2730.     int bits = 0;
  2731.  
  2732.     cod_info->table_select[0] = 0;
  2733.     cod_info->table_select[1] = 0;
  2734.     cod_info->table_select[2] = 0;
  2735.     
  2736.     if ( cod_info->window_switching_flag && (cod_info->block_type == 2) )
  2737.     {
  2738.         /*
  2739.           Within each scalefactor band, data is given for successive
  2740.           time windows, beginning with window 0 and ending with window 2.
  2741.           Within each window, the quantized values are then arranged in
  2742.           order of increasing frequency...
  2743.           */
  2744.         int sfb, window, line, start, end, max1, max2, x, y;
  2745.         int region1Start;
  2746.         int *pmax;
  2747.         I192_3 *ix_s;
  2748.  
  2749.  
  2750.         region1Start = 12;
  2751.         max1 = max2 = 0;
  2752.  
  2753.         for ( sfb = 0; sfb < 13; sfb++ )
  2754.         {
  2755.             start = scalefac_band_short[ sfb ];
  2756.             end   = scalefac_band_short[ sfb+1 ];
  2757.             
  2758.             if ( start < region1Start )
  2759.                 pmax = &max1;
  2760.             else
  2761.                 pmax = &max2;
  2762.             
  2763.             for ( window = 0; window < 3; window++ )
  2764.                 for ( line = start; line < end; line += 2 )
  2765.                 {
  2766.                     x = ix[ (line * 3) + window ];
  2767.                     y = ix[ ((line + 1) * 3) + window ];
  2768.                     *pmax = *pmax > x ? *pmax : x;
  2769.                     *pmax = *pmax > y ? *pmax : y;
  2770.                 }
  2771.         }
  2772.         cod_info->table_select[0] = choose_table( max1 );
  2773.         cod_info->table_select[1] = choose_table( max2 );
  2774.  
  2775.         /*
  2776.           Within each scalefactor band, data is given for successive
  2777.           time windows, beginning with window 0 and ending with window 2.
  2778.           Within each window, the quantized values are then arranged in
  2779.           order of increasing frequency...
  2780.           */
  2781.  
  2782.             sfb = 0;
  2783.  
  2784.         ix_s = (I192_3 *) &ix[0];
  2785.  
  2786.         for ( ; sfb < 13; sfb++ )
  2787.         {
  2788.             unsigned tableindex = 100;
  2789.  
  2790.             start = scalefac_band_short[ sfb ];
  2791.             end   = scalefac_band_short[ sfb+1 ];
  2792.  
  2793.             if ( start < 12 )
  2794.                 tableindex = cod_info->table_select[ 0 ];
  2795.             else
  2796.                 tableindex = cod_info->table_select[ 1 ];
  2797.  
  2798.             for ( window = 0; window < 3; window++ )
  2799.                 for ( line = start; line < end; line += 2 )
  2800.                 {
  2801.                     unsigned int code, ext;
  2802.                     int cbits, xbits;
  2803.                     int x = (*ix_s)[line][window];
  2804.                     int y = (*ix_s)[line + 1][window];
  2805.                     bits += HuffmanCode( tableindex, x, y, &code, &ext, &cbits, &xbits );
  2806.                 }
  2807.         }
  2808.     }
  2809.     else
  2810.     {
  2811.         unsigned int table;
  2812.         int s;
  2813.         
  2814.         if ( cod_info->address1 > 0 )
  2815.         {
  2816.             table = cod_info->table_select[0] = new_choose_table( ix, 0, cod_info->address1, &s );
  2817.             if( table != 0 )  /* region0 */ 
  2818.                bits += s;
  2819.         }
  2820.  
  2821.         if ( cod_info->address2 > cod_info->address1 )
  2822.         {
  2823.             table = cod_info->table_select[1] = new_choose_table( ix, cod_info->address1, cod_info->address2, &s );
  2824.            if( table != 0 )  /* region1 */ 
  2825.                bits += s;
  2826.         }
  2827.  
  2828.         if ( cod_info->big_values * 2 > cod_info->address2 )
  2829.         {
  2830.             table = cod_info->table_select[2] = new_choose_table( ix, cod_info->address2, cod_info->big_values * 2, &s );
  2831.            if( table != 0 )  /* region2 */ 
  2832.                bits += s;
  2833.         }
  2834.     }
  2835.     return bits;
  2836. }
  2837.  
  2838.  
  2839.  
  2840. /*************************************************************************/
  2841. /*            count_bit                                                  */
  2842. /*************************************************************************/
  2843.  
  2844. /*
  2845.  Function: Count the number of bits necessary to code the subregion. 
  2846. */
  2847.  
  2848. int count_bit( int ix[576], unsigned int start, unsigned int end, unsigned int table )
  2849. {
  2850.     unsigned            linbits, ylen;
  2851.     register int        i, sum;
  2852.     register int        x,y;
  2853.     struct huffcodetab *h;
  2854.  
  2855.     if(table==0) return 0;
  2856.  
  2857.     h   = &(ht[table]);
  2858.     sum = 0;
  2859.  
  2860.     ylen    = h->ylen;
  2861.     linbits = h->linbits;
  2862.  
  2863.     if(table>15)
  2864.     { /* ESC-table is used */
  2865.         for(i=start;i<end;i+=2)
  2866.         {
  2867.             x = ix[i];
  2868.             y = ix[i+1];
  2869.             if(x>14)
  2870.             {
  2871.                 x = 15;
  2872.                 sum += linbits;
  2873.             }
  2874.             if(y>14)
  2875.             {
  2876.                 y = 15;
  2877.                 sum += linbits;
  2878.             }
  2879.  
  2880.             sum += h->hlen[(x*ylen)+y];
  2881.  
  2882.             if(x!=0) sum++;
  2883.             if(y!=0) sum++;
  2884.         }
  2885.     }
  2886.     else
  2887.     { /* No ESC-words */
  2888.         for(i=start;i<end;i+=2)
  2889.         {
  2890.             x = ix[i];
  2891.             y = ix[i+1];
  2892.  
  2893.             sum  += h->hlen[(x*ylen)+y];
  2894.  
  2895.             if(x!=0) sum++;
  2896.             if(y!=0) sum++;
  2897.         }
  2898.     }
  2899.  
  2900.     return sum;
  2901. }
  2902.  
  2903.  
  2904.  
  2905. #ifndef HAVE_NINT
  2906. int
  2907. nint( double in )
  2908. {
  2909.     int    temp;
  2910.  
  2911.     if( in < 0 )  temp = (int)(in - 0.5);
  2912.     else    temp = (int)(in + 0.5);
  2913.  
  2914.     return(temp);
  2915. }
  2916.  
  2917. /*
  2918. double
  2919. aint(double in) {
  2920.     return((long) in);
  2921. }
  2922. */
  2923. #endif
  2924.  
  2925.  
  2926.  
  2927. /*
  2928.   Seymour's comment:  Jan 8 1995
  2929.   When mixed_block_flag is set, the low subbands 0-1 undergo the long
  2930.   window transform and are each split into 18 frequency lines, while
  2931.   the remaining 30 subbands undergo the short window transform and are
  2932.   each split into 6 frequency lines. A problem now arises, as neither
  2933.   the short or long scale factor bands apply to this mixed spectrum.
  2934.   The standard resolves this situation by using the first 8 long scale
  2935.   factor bands for the low spectrum and the short scale factor bands
  2936.   in the range of 3 to 11 (inclusive) for the remaining frequency lines.
  2937.   These scale factor bands do not match exactly to the 0-1 subbands
  2938.   for all sampling frequencies (32,44.1 and 48 kHz); however they
  2939.   were designed so that there would not be a frequency gap or overlap
  2940.   at the switch over point. (Note multiply short frequency lines by 3
  2941.   to account for wider frequency line.) 
  2942.   */
  2943.  
  2944. /* Mark Taylor's comment: 4/99:  ISO code cannot produces mixed blocks,
  2945.  * Fhg Code also never seems to use them, so no need to add them
  2946.  * to this code 
  2947.  */
  2948. /*************************************************************************/
  2949. /*            gr_deco                                                    */
  2950. /*************************************************************************/
  2951.  
  2952. void gr_deco( gr_info *cod_info )
  2953. {
  2954.     if ( cod_info->window_switching_flag != 0 && cod_info->block_type == 2 )
  2955.         {
  2956.             cod_info->sfb_lmax = 0; /* No sb*/
  2957.             cod_info->sfb_smax = 0;
  2958.         }
  2959.     else
  2960.     {
  2961.       /* MPEG 1 doesnt use last scalefactor band? */
  2962.         cod_info->sfb_lmax = SFB_LMAX - 1;
  2963.         cod_info->sfb_smax = SFB_SMAX - 1;    /* No sb */
  2964.     }
  2965. }
  2966.  
  2967.  
  2968.  
  2969.  
  2970.  
  2971.  
  2972.  
  2973.  
  2974. /* The following optional code written by Seymour Shlien
  2975.    will speed up the outer_loop code which is called
  2976.    by iteration_loop. When BIN_SEARCH is defined, the
  2977.    outer_loop function precedes the call to the function inner_loop
  2978.    with a call to bin_search gain defined below, which
  2979.    returns a good starting quantizerStepSize.
  2980. */
  2981.  
  2982. int count_bits(ix,cod_info)  
  2983. int  *ix; /*  I576  *ix; */
  2984. gr_info *cod_info;
  2985. {
  2986. int bits,i;
  2987.   for ( i = 0; i < 576; i++ )
  2988.   {
  2989.      if ( ix[i] > 8191 + 14)
  2990.     return 100000;        /* report unsuitable quantizer */
  2991.   }
  2992.   calc_runlen(ix,cod_info);        /*count1,big_values*/
  2993.   bits = count1_bitcount(ix, cod_info); /*count1_table selection*/
  2994.   subdivide(cod_info);            /* bigvalues sfb division */
  2995.   bits += bigv_bitcount(ix,cod_info);    /* bit count */
  2996. /* printf("\nglobal_gain = %f  bits= %d ",cod_info->quantizerStepSize,bits);*/
  2997. return bits;
  2998. }
  2999.  
  3000.  
  3001.  
  3002. int bin_search_StepSize(int desired_rate, double start, int bot, int *ix,
  3003.            double xrs[576], double xrspow[576], gr_info * cod_info)
  3004. {
  3005. int top,next,last;
  3006. int bit;
  3007. top = start;
  3008. next = start;
  3009. do
  3010.   {
  3011.   last = next;
  3012.   next = (top+bot)/2.0;
  3013.   cod_info->quantizerStepSize = next;
  3014.   if (xrpow_flag) {
  3015.     quantize_xrpow(xrspow,ix,cod_info);
  3016.   }  else {
  3017.     quantize(xrs,ix,cod_info);
  3018.   }
  3019.   bit = count_bits(ix,cod_info);
  3020.   if (bit>desired_rate) top = next;
  3021.   else bot = next;
  3022.   // printf("\n%f %f %f %f %d %d",start,next, top,bot,bit,desired_rate);
  3023.   }
  3024.   while ((bit != desired_rate) && abs(last - next) > 1);
  3025. //printf("\n done  %f %d %d",next,bit,desired_rate);
  3026.  
  3027. return bit;
  3028. }
  3029.  
  3030.  
  3031.  
  3032.  
  3033.