home *** CD-ROM | disk | FTP | other *** search
/ BUG 2 / BUGCD1997_01.iso / mulmedia / mpegwin4 / _setup.1 / LAYER3.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-23  |  25.3 KB  |  891 lines

  1. #define STRICT
  2. #define WIN32_LEAN_AND_MEAN
  3. /*
  4. #define NOMCX
  5. #define NOIME
  6. #define NOGDI
  7. #define NOUSER
  8. #define NOSOUND
  9. #define NOCOMM
  10. #define NODRIVERS
  11. #define OEMRESOURCE
  12. #define NONLS
  13. #define NOSERVICE
  14. #define NOKANJI
  15. #define NOMINMAX
  16. #define NOLOGERROR
  17. #define NOPROFILER
  18. #define NOMEMMGR
  19. #define NOLFILEIO
  20. #define NOOPENFILE
  21. #define NORESOURCE
  22. #define NOATOM
  23. #define NOLANGUAGE
  24. //#define NOLSTRING
  25. #define NODBCS
  26. #define NOKEYBOARDINFO
  27. #define NOGDICAPMASKS
  28. #define NOCOLOR
  29. #define NOGDIOBJ
  30. #define NODRAWTEXT
  31. #define NOTEXTMETRIC
  32. #define NOSCALABLEFONT
  33. #define NOBITMAP
  34. #define NORASTEROPS
  35. #define NOMETAFILE
  36. #define NOSYSMETRICS
  37. #define NOSYSTEMPARAMSINFO
  38. #define NOMSG
  39. #define NOWINSTYLES
  40. #define NOWINOFFSETS
  41. #define NOSHOWWINDOW
  42. #define NODEFERWINDOWPOS
  43. #define NOVIRTUALKEYCODES
  44. #define NOKEYSTATES
  45. #define NOWH
  46. #define NOMENUS
  47. #define NOSCROLL
  48. #define NOCLIPBOARD
  49. #define NOICONS
  50. //#define NOMB
  51. #define NOSYSCOMMANDS
  52. #define NOMDI
  53. #define NOCTLMGR
  54. #define NOWINMESSAGES
  55. #define NOHELP
  56. #define _WINUSER_ */
  57. #include <windows.h>
  58. #include <stdlib.h>
  59.  
  60. #include "all.h"
  61. #include "l3type.h"
  62. #include "ibitstr.h"
  63. #include "obuffer.h"
  64. #include "bit_res.h"
  65. #include "header.h"
  66. #include "synfilt.h"
  67. #include "math.h"
  68. #include "huffman.h"
  69. #include "layer3.h"
  70.  
  71. // Layer III routines
  72.  
  73. struct {
  74.     int l[5];
  75.     int s[3];} sfbtable = {{0, 6, 11, 16, 21},
  76.                                   {0, 6, 12}};
  77.  
  78. int slen[2][16] = {{0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
  79.                          {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}};
  80.  
  81. void III_get_scale_factors(III_scalefac_t *scalefac, III_side_info_t *si,
  82.                                     Bit_Reserve *br, int gr, int ch)
  83. {
  84.     int sfb, i, window;
  85.     gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
  86.  
  87.      if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  88.         if (gr_info->mixed_block_flag) { /* MIXED */ /* NEW - ag 11/25 */
  89.             for (sfb = 0; sfb < 8; sfb++)
  90.                 (*scalefac)[ch].l[sfb] = br->hgetbits(
  91.                       slen[0][gr_info->scalefac_compress]);
  92.             for (sfb = 3; sfb < 6; sfb++)
  93.                 for (window=0; window<3; window++)
  94.                     (*scalefac)[ch].s[window][sfb] = br->hgetbits(
  95.                       slen[0][gr_info->scalefac_compress]);
  96.             for (sfb = 6; sfb < 12; sfb++)
  97.                 for (window=0; window<3; window++)
  98.                     (*scalefac)[ch].s[window][sfb] = br->hgetbits(
  99.                       slen[1][gr_info->scalefac_compress]);
  100.             for (sfb=12,window=0; window<3; window++)
  101.                 (*scalefac)[ch].s[window][sfb] = 0;
  102.         }
  103.         else {  /* SHORT*/
  104.             for (i=0; i<2; i++)
  105.                 for (sfb = sfbtable.s[i]; sfb < sfbtable.s[i+1]; sfb++)
  106.                     for (window=0; window<3; window++)
  107.                         (*scalefac)[ch].s[window][sfb] = br->hgetbits(
  108.                           slen[i][gr_info->scalefac_compress]);
  109.             for (sfb=12,window=0; window<3; window++)
  110.                 (*scalefac)[ch].s[window][sfb] = 0;
  111.         }
  112.      }
  113.      else {   /* LONG types 0,1,3 */
  114.           for (i=0; i<4; i++) {
  115.               if ((si->ch[ch].scfsi[i] == 0) || (gr == 0))
  116.                   for (sfb = sfbtable.l[i]; sfb < sfbtable.l[i+1]; sfb++)
  117.                         (*scalefac)[ch].l[sfb] = br->hgetbits(
  118.                       slen[(i<2)?0:1][gr_info->scalefac_compress]);
  119.           }
  120.           (*scalefac)[ch].l[22] = 0;
  121.      }
  122. }
  123.  
  124. struct  {
  125.     int l[23];
  126.     int s[14];} sfBandIndex[3] =
  127.     {{{0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576},
  128.       {0,4,8,12,16,22,30,40,52,66,84,106,136,192}},
  129.      {{0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576},
  130.       {0,4,8,12,16,22,28,38,50,64,80,100,126,192}},
  131.      {{0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
  132.       {0,4,8,12,16,22,30,42,58,78,104,138,180,192}}};
  133.  
  134. void III_hufman_decode(int is[SBLIMIT][SSLIMIT], III_side_info_t *si, int ch, int gr,
  135.                               int part2_start, Header *header, Bit_Reserve *br)
  136. {
  137.     int i, x, y;
  138.     int v, w;
  139.     struct huffcodetab *h;
  140.     int region1Start;                    
  141.     int region2Start;
  142. //    int bt = (*si).ch[ch].gr[gr].window_switching_flag && ((*si).ch[ch].gr[gr].block_type == 2);
  143.  
  144.  
  145.     char numstr[32];
  146. //    MessageBox(NULL, "initializing huffman table", "initializing huffman table",
  147. //                  MB_OK);
  148.  
  149.     initialize_huffman();
  150.  
  151. //    MessageBox(NULL, "huffman table initialized ok", "huffman table initialized ok",
  152. //                  MB_OK);
  153.  
  154.  
  155.     /* Find region boundary for short block case. */
  156.  
  157.     if ( ((*si).ch[ch].gr[gr].window_switching_flag) &&
  158.           ((*si).ch[ch].gr[gr].block_type == 2) ) {
  159.  
  160.  
  161.         /* Region2. */
  162.  
  163.         region1Start = 36;  /* sfb[9/3]*3=36 */
  164.         region2Start = 576; /* No Region2 for short block case. */
  165.     }
  166.  
  167.     else {          /* Find region boundary for long block case. */
  168.  
  169.         region1Start = sfBandIndex[header->sample_frequency()]
  170.                                     .l[(*si).ch[ch].gr[gr].region0_count + 1]; /* MI */
  171.         region2Start = sfBandIndex[header->sample_frequency()]
  172.                                         .l[(*si).ch[ch].gr[gr].region0_count +
  173.                                         (*si).ch[ch].gr[gr].region1_count + 2]; /* MI */
  174.         }
  175.  
  176. //    MessageBox(NULL, "looked at side info", "huffman decode",
  177. //                  MB_OK);
  178.  
  179.  
  180.     /* Read bigvalues area. */
  181.     for (i=0; i<(*si).ch[ch].gr[gr].big_values*2; i+=2) {
  182.         if      (i<region1Start) h = &ht[(*si).ch[ch].gr[gr].table_select[0]];
  183.         else if (i<region2Start) h = &ht[(*si).ch[ch].gr[gr].table_select[1]];
  184.               else                h = &ht[(*si).ch[ch].gr[gr].table_select[2]];
  185.  
  186. /*      if (i>560)
  187.         MessageBox(NULL, "about to do huff decoder", itoa(i, numstr, 10),
  188.                   MB_OK); */
  189.  
  190.         huffman_decoder(h, &x, &y, &v, &w, br);
  191.  
  192.         is[i/SSLIMIT][i%SSLIMIT] = x;
  193.         is[(i+1)/SSLIMIT][(i+1)%SSLIMIT] = y;
  194.         }
  195.  
  196. //    MessageBox(NULL, "out of loop", "huffman decoder",
  197. //                  MB_OK);
  198.  
  199.  
  200.     /* Read count1 area. */
  201.     h = &ht[(*si).ch[ch].gr[gr].count1table_select+32];
  202.     while ((br->hsstell() < part2_start + (*si).ch[ch].gr[gr].part2_3_length ) &&
  203.       ( i < SSLIMIT*SBLIMIT )) {
  204.         huffman_decoder(h, &x, &y, &v, &w, br);
  205.         is[i/SSLIMIT][i%SSLIMIT] = v;
  206.         is[(i+1)/SSLIMIT][(i+1)%SSLIMIT] = w;
  207.         is[(i+2)/SSLIMIT][(i+2)%SSLIMIT] = x;
  208.         is[(i+3)/SSLIMIT][(i+3)%SSLIMIT] = y;
  209.         i += 4;
  210.         }
  211.  
  212.     if (br->hsstell() > part2_start + (*si).ch[ch].gr[gr].part2_3_length)
  213.     {  i -=4;
  214.         br->rewindNbits(br->hsstell()-part2_start - (*si).ch[ch].gr[gr].part2_3_length);
  215.     }
  216.  
  217.     /* Dismiss stuffing Bits */
  218.     if (br->hsstell() < part2_start + (*si).ch[ch].gr[gr].part2_3_length )
  219.         br->hgetbits( part2_start + (*si).ch[ch].gr[gr].part2_3_length - br->hsstell());
  220.  
  221.     /* Zero out rest. */
  222.     for (; i<SSLIMIT*SBLIMIT; i++)
  223.         is[i/SSLIMIT][i%SSLIMIT] = 0;
  224.  
  225. }
  226.  
  227. int pretab[22] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0};
  228.  
  229. void III_dequantize_sample(int is[SBLIMIT][SSLIMIT], real xr[SBLIMIT][SSLIMIT],
  230.                                     III_scalefac_t *scalefac, gr_info_s *gr_info,
  231.                                     int ch, Header *header)
  232. {
  233. //    int ss,sb,cb=0,sfreq=fr_ps->header->sampling_frequency;
  234. //    int stereo = fr_ps->stereo;
  235.  
  236.     int ss, sb, cb=0, sfreq=header->sample_frequency();
  237. //    int stereo = ((header->mode() == single_channel) ? 1 : 2);
  238.  
  239.     int next_cb_boundary, cb_begin, cb_width, sign;
  240.  
  241.     /* choose correct scalefactor band per block type, initalize boundary */
  242.  
  243.     if (gr_info->window_switching_flag && (gr_info->block_type == 2) )
  244.         if (gr_info->mixed_block_flag)
  245.             next_cb_boundary=sfBandIndex[sfreq].l[1];  /* LONG blocks: 0,1,3 */
  246.         else {
  247.             next_cb_boundary=sfBandIndex[sfreq].s[1]*3; /* pure SHORT block */
  248.      cb_width = sfBandIndex[sfreq].s[1];
  249.      cb_begin = 0;
  250.         }
  251.     else
  252.         next_cb_boundary=sfBandIndex[sfreq].l[1];  /* LONG blocks: 0,1,3 */
  253.  
  254.     /* apply formula per block type */
  255.  
  256.     for (sb=0 ; sb < SBLIMIT ; sb++)
  257.         for (ss=0 ; ss < SSLIMIT ; ss++) {
  258.  
  259.             if ( (sb*18)+ss == next_cb_boundary)  { /* Adjust critical band boundary */
  260.                 if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  261.                     if (gr_info->mixed_block_flag)  {
  262.                         if (((sb*18)+ss) == sfBandIndex[sfreq].l[8])  {
  263.                             next_cb_boundary=sfBandIndex[sfreq].s[4]*3;
  264.                             cb = 3;
  265.                             cb_width = sfBandIndex[sfreq].s[cb+1] -
  266.                                           sfBandIndex[sfreq].s[cb];
  267.                             cb_begin = sfBandIndex[sfreq].s[cb]*3;
  268.                         }
  269.                         else if (((sb*18)+ss) < sfBandIndex[sfreq].l[8])
  270.                             next_cb_boundary = sfBandIndex[sfreq].l[(++cb)+1];
  271.                         else {
  272.                             next_cb_boundary = sfBandIndex[sfreq].s[(++cb)+1]*3;
  273.                             cb_width = sfBandIndex[sfreq].s[cb+1] -
  274.                                                 sfBandIndex[sfreq].s[cb];
  275.                             cb_begin = sfBandIndex[sfreq].s[cb]*3;
  276.                         }
  277.                     }
  278.                     else  {
  279.                         next_cb_boundary = sfBandIndex[sfreq].s[(++cb)+1]*3;
  280.                         cb_width = sfBandIndex[sfreq].s[cb+1] -
  281.                                          sfBandIndex[sfreq].s[cb];
  282.                      cb_begin = sfBandIndex[sfreq].s[cb]*3;
  283.                     }
  284.                 }
  285.                 else /* long blocks */
  286.                     next_cb_boundary = sfBandIndex[sfreq].l[(++cb)+1];
  287.             }
  288.  
  289.             /* Compute overall (global) scaling. */
  290.  
  291.             xr[sb][ss] = pow( 2.0 , (0.25 * (gr_info->global_gain - 210.0)));
  292.  
  293.             /* Do long/short dependent scaling operations. */
  294.  
  295.             if (gr_info->window_switching_flag && (
  296.                 ((gr_info->block_type == 2) && (gr_info->mixed_block_flag == 0)) ||
  297.                 ((gr_info->block_type == 2) && gr_info->mixed_block_flag && (sb >= 2)) )) {
  298.  
  299.                 xr[sb][ss] *= pow(2.0, 0.25 * -8.0 *
  300.                           gr_info->subblock_gain[(((sb*18)+ss) - cb_begin)/cb_width]);
  301.                 xr[sb][ss] *= pow(2.0, 0.25 * -2.0 * (1.0+gr_info->scalefac_scale)
  302.                   * (*scalefac)[ch].s[(((sb*18)+ss) - cb_begin)/cb_width][cb]);
  303.             }
  304.             else {   /* LONG block types 0,1,3 & 1st 2 subbands of switched blocks */
  305.                 xr[sb][ss] *= pow(2.0, -0.5 * (1.0+gr_info->scalefac_scale)
  306.                                                      * ((*scalefac)[ch].l[cb]
  307.                                                      + gr_info->preflag * pretab[cb]));
  308.             }
  309.  
  310.             /* Scale quantized value. */
  311.  
  312.             sign = (is[sb][ss]<0) ? 1 : 0;
  313.             xr[sb][ss] *= pow( (double) abs(is[sb][ss]), ((double)4.0/3.0) );
  314.             if (sign) xr[sb][ss] = -xr[sb][ss];
  315.         }
  316. }
  317.  
  318. void III_reorder (real xr[SBLIMIT][SSLIMIT], real ro[SBLIMIT][SSLIMIT],
  319.                         gr_info_s *gr_info, Header *header)
  320. /*
  321. double xr[SBLIMIT][SSLIMIT];
  322. double ro[SBLIMIT][SSLIMIT];
  323. struct gr_info_s *gr_info;
  324. frame_params *fr_ps; */
  325. {
  326.     int sfreq=header->sample_frequency();
  327.     int sfb, sfb_start, sfb_lines;
  328.     int sb, ss, window, freq, src_line, des_line;
  329.  
  330.     for(sb=0;sb<SBLIMIT;sb++)
  331.         for(ss=0;ss<SSLIMIT;ss++)
  332.             ro[sb][ss] = 0.0;
  333.  
  334.     if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  335.         if (gr_info->mixed_block_flag) {
  336.             /* NO REORDER FOR LOW 2 SUBBANDS */
  337.             for (sb=0 ; sb < 2 ; sb++)
  338.                 for (ss=0 ; ss < SSLIMIT ; ss++) {
  339.                     ro[sb][ss] = xr[sb][ss];
  340.                 }
  341.             /* REORDERING FOR REST SWITCHED SHORT */
  342.             for(sfb=3,sfb_start=sfBandIndex[sfreq].s[3],
  343.                 sfb_lines=sfBandIndex[sfreq].s[4] - sfb_start;
  344.                 sfb < 13; sfb++,sfb_start=sfBandIndex[sfreq].s[sfb],
  345.                 (sfb_lines=sfBandIndex[sfreq].s[sfb+1] - sfb_start))
  346.                     for(window=0; window<3; window++)
  347.                         for(freq=0;freq<sfb_lines;freq++) {
  348.                             src_line = sfb_start*3 + window*sfb_lines + freq;
  349.                             des_line = (sfb_start*3) + window + (freq*3);
  350.                             ro[des_line/SSLIMIT][des_line%SSLIMIT] =
  351.                                                 xr[src_line/SSLIMIT][src_line%SSLIMIT];
  352.                     }
  353.         }
  354.         else {  /* pure short */
  355.             for(sfb=0,sfb_start=0,sfb_lines=sfBandIndex[sfreq].s[1];
  356.                 sfb < 13; sfb++,sfb_start=sfBandIndex[sfreq].s[sfb],
  357.                 (sfb_lines=sfBandIndex[sfreq].s[sfb+1] - sfb_start))
  358.                     for(window=0; window<3; window++)
  359.                         for(freq=0;freq<sfb_lines;freq++) {
  360.                             src_line = sfb_start*3 + window*sfb_lines + freq;
  361.                             des_line = (sfb_start*3) + window + (freq*3);
  362.                             ro[des_line/SSLIMIT][des_line%SSLIMIT] =
  363.                                                 xr[src_line/SSLIMIT][src_line%SSLIMIT];
  364.                     }
  365.         }
  366.     }
  367.     else {   /*long blocks */
  368.         for (sb=0 ; sb < SBLIMIT ; sb++)
  369.             for (ss=0 ; ss < SSLIMIT ; ss++)
  370.                 ro[sb][ss] = xr[sb][ss];
  371.     }
  372. }
  373.  
  374. #define PI   3.141593
  375. #define PI12 0.2617994
  376. #define PI36 0.08726646
  377. #define PI72 0.04363323
  378.  
  379. void III_stereo(real xr[2][SBLIMIT][SSLIMIT], real lr[2][SBLIMIT][SSLIMIT],
  380.                      III_scalefac_t *scalefac, gr_info_s *gr_info, Header *header)
  381. /*
  382. double xr[2][SBLIMIT][SSLIMIT];
  383. double lr[2][SBLIMIT][SSLIMIT];
  384. III_scalefac_t *scalefac;
  385. struct gr_info_s *gr_info;
  386. frame_params *fr_ps; */
  387. {
  388. //    int sfreq = fr_ps->header->sampling_frequency;
  389.     int sfreq = header->sample_frequency();
  390. //    int stereo = fr_ps->stereo;
  391.     int stereo = (header->mode() == single_channel) ? 1 : 2;
  392. /*    int ms_stereo = (fr_ps->header->mode == MPG_MD_JOINT_STEREO) &&
  393.                          (fr_ps->header->mode_ext & 0x2); */
  394.     int ms_stereo = (header->mode() == joint_stereo) &&
  395.                          (header->mode_extension() & 0x2);
  396. /*    int i_stereo = (fr_ps->header->mode == MPG_MD_JOINT_STEREO) &&
  397.                         (fr_ps->header->mode_ext & 0x1); */
  398.     int i_stereo = (header->mode() == joint_stereo) &&
  399.                         (header->mode_extension() & 0x1);
  400. //    int js_bound;  /* frequency line that marks the beggining of the zero part */
  401. //    int sfb,next_sfb_boundary;
  402.     int sfb;
  403.     int i,j,sb,ss,ch,is_pos[1152];
  404.     real is_ratio[1152];
  405.  
  406.     /* intialization */
  407.     for ( i=0; i<576; i++ )
  408.         is_pos[i] = 7;
  409.  
  410.     if ((stereo == 2) && i_stereo )
  411.     {  if (gr_info->window_switching_flag && (gr_info->block_type == 2))
  412.         {  if( gr_info->mixed_block_flag )
  413.             {  int max_sfb = 0;
  414.  
  415.                 for ( j=0; j<3; j++ )
  416.                 {  int sfbcnt;
  417.                     sfbcnt = 2;
  418.                     for( sfb=12; sfb >=3; sfb-- )
  419.                     {  int lines;
  420.                         lines = sfBandIndex[sfreq].s[sfb+1]-sfBandIndex[sfreq].s[sfb];
  421.                         i = 3*sfBandIndex[sfreq].s[sfb] + (j+1) * lines - 1;
  422.                         while ( lines > 0 )
  423.                         {  if ( xr[1][i/SSLIMIT][i%SSLIMIT] != 0.0 )
  424.                             {  sfbcnt = sfb;
  425.                                 sfb = -10;
  426.                                 lines = -10;
  427.                             }
  428.                             lines--;
  429.                             i--;
  430.                         }
  431.                     }
  432.                     sfb = sfbcnt + 1;
  433.  
  434.                     if ( sfb > max_sfb )
  435.                         max_sfb = sfb;
  436.  
  437.                     while( sfb<12 )
  438.                     {  sb = sfBandIndex[sfreq].s[sfb+1]-sfBandIndex[sfreq].s[sfb];
  439.                         i = 3*sfBandIndex[sfreq].s[sfb] + j * sb;
  440.                         for ( ; sb > 0; sb--)
  441.                         {  is_pos[i] = (*scalefac)[1].s[j][sfb];
  442.                             if ( is_pos[i] != 7 )
  443.                                 is_ratio[i] = tan( is_pos[i] * PI12);
  444.                             i++;
  445.                         }
  446.                         sfb++;
  447.                     }
  448.                     sb = sfBandIndex[sfreq].s[11]-sfBandIndex[sfreq].s[10];
  449.                     sfb = 3*sfBandIndex[sfreq].s[10] + j * sb;
  450.                     sb = sfBandIndex[sfreq].s[12]-sfBandIndex[sfreq].s[11];
  451.                     i = 3*sfBandIndex[sfreq].s[11] + j * sb;
  452.                     for ( ; sb > 0; sb-- )
  453.                     {  is_pos[i] = is_pos[sfb];
  454.                         is_ratio[i] = is_ratio[sfb];
  455.                         i++;
  456.                     }
  457.                  }
  458.                  if ( max_sfb <= 3 )
  459.                  {  i = 2;
  460.                      ss = 17;
  461.                      sb = -1;
  462.                      while ( i >= 0 )
  463.                      {  if ( xr[1][i][ss] != 0.0 )
  464.                          {  sb = i*18+ss;
  465.                              i = -1;
  466.                          } else
  467.                          {  ss--;
  468.                              if ( ss < 0 )
  469.                              {  i--;
  470.                                  ss = 17;
  471.                              }
  472.                          }
  473.                      }
  474.                      i = 0;
  475.                      while ( sfBandIndex[sfreq].l[i] <= sb )
  476.                          i++;
  477.                      sfb = i;
  478.                      i = sfBandIndex[sfreq].l[i];
  479.                      for ( ; sfb<8; sfb++ )
  480.                      {  sb = sfBandIndex[sfreq].l[sfb+1]-sfBandIndex[sfreq].l[sfb];
  481.                          for ( ; sb > 0; sb--)
  482.                          {  is_pos[i] = (*scalefac)[1].l[sfb];
  483.                              if ( is_pos[i] != 7 )
  484.                                  is_ratio[i] = tan( is_pos[i] * PI12);
  485.                              i++;
  486.                          }
  487.                      }
  488.                 }
  489.             } else
  490.             {  for ( j=0; j<3; j++ )
  491.                 {  int sfbcnt;
  492.                     sfbcnt = -1;
  493.                     for( sfb=12; sfb >=0; sfb-- )
  494.                     {  int lines;
  495.                         lines = sfBandIndex[sfreq].s[sfb+1]-sfBandIndex[sfreq].s[sfb];
  496.                         i = 3*sfBandIndex[sfreq].s[sfb] + (j+1) * lines - 1;
  497.                         while ( lines > 0 )
  498.                         {  if ( xr[1][i/SSLIMIT][i%SSLIMIT] != 0.0 )
  499.                             {  sfbcnt = sfb;
  500.                                 sfb = -10;
  501.                                 lines = -10;
  502.                             }
  503.                             lines--;
  504.                             i--;
  505.                         }
  506.                     }
  507.                     sfb = sfbcnt + 1;
  508.                     while( sfb<12 )
  509.                     {  sb = sfBandIndex[sfreq].s[sfb+1]-sfBandIndex[sfreq].s[sfb];
  510.                         i = 3*sfBandIndex[sfreq].s[sfb] + j * sb;
  511.                         for ( ; sb > 0; sb--)
  512.                         {  is_pos[i] = (*scalefac)[1].s[j][sfb];
  513.                             if ( is_pos[i] != 7 )
  514.                                 is_ratio[i] = tan( is_pos[i] * PI12);
  515.                             i++;
  516.                         }
  517.                         sfb++;
  518.                     }
  519.  
  520.                     sb = sfBandIndex[sfreq].s[11]-sfBandIndex[sfreq].s[10];
  521.                     sfb = 3*sfBandIndex[sfreq].s[10] + j * sb;
  522.                     sb = sfBandIndex[sfreq].s[12]-sfBandIndex[sfreq].s[11];
  523.                     i = 3*sfBandIndex[sfreq].s[11] + j * sb;
  524.                     for ( ; sb > 0; sb-- )        // problem here?
  525.                     {  is_pos[i] = is_pos[sfb];
  526.                         is_ratio[i] = is_ratio[sfb];
  527.                         i++;
  528.                     }
  529.                 }
  530.             }
  531.         } else
  532.         {  i = 31;
  533.             ss = 17;
  534.             sb = 0;
  535.             while ( i >= 0 )
  536.             {  if ( xr[1][i][ss] != 0.0 )
  537.                 {  sb = i*18+ss;
  538.                     i = -1;
  539.                 } else
  540.                 {  ss--;
  541.                     if ( ss < 0 )
  542.                     {  i--;
  543.                         ss = 17;
  544.                     }
  545.                 }
  546.             }
  547.             i = 0;
  548.             while ( sfBandIndex[sfreq].l[i] <= sb )
  549.                 i++;
  550.             sfb = i;
  551.             i = sfBandIndex[sfreq].l[i];
  552.             for ( ; sfb<21; sfb++ )
  553.             {  sb = sfBandIndex[sfreq].l[sfb+1] - sfBandIndex[sfreq].l[sfb];
  554.                 for ( ; sb > 0; sb--)      // problem is here
  555.                 {  is_pos[i] = (*scalefac)[1].l[sfb];
  556.                     if ( is_pos[i] != 7 )
  557.                         is_ratio[i] = tan( is_pos[i] * PI12);
  558.                     i++;
  559.                 }
  560.             }
  561.             sfb = sfBandIndex[sfreq].l[20];
  562.             for ( sb = 576 - sfBandIndex[sfreq].l[21]; sb > 0; sb-- )
  563.             {  is_pos[i] = is_pos[sfb];
  564.                 is_ratio[i] = is_ratio[sfb];
  565.                 i++;
  566.             }
  567.         }
  568.     }
  569.  
  570.     for(ch=0;ch<2;ch++)
  571.         for(sb=0;sb<SBLIMIT;sb++)
  572.             for(ss=0;ss<SSLIMIT;ss++)
  573.                 lr[ch][sb][ss] = 0;
  574.  
  575.     if (stereo==2)
  576.         for(sb=0;sb<SBLIMIT;sb++)
  577.             for(ss=0;ss<SSLIMIT;ss++) {
  578.                 i = (sb*18)+ss;
  579.                 if ( is_pos[i] == 7 ) {
  580.                     if ( ms_stereo ) {
  581.                         lr[0][sb][ss] = (xr[0][sb][ss]+xr[1][sb][ss])/1.41421356;
  582.                         lr[1][sb][ss] = (xr[0][sb][ss]-xr[1][sb][ss])/1.41421356;
  583.                     }
  584.                     else {
  585.                         lr[0][sb][ss] = xr[0][sb][ss];
  586.                         lr[1][sb][ss] = xr[1][sb][ss];
  587.                     }
  588.                 }
  589.                 else if (i_stereo ) {
  590.                     lr[0][sb][ss] = xr[0][sb][ss] * (is_ratio[i]/(1+is_ratio[i]));
  591.                     lr[1][sb][ss] = xr[0][sb][ss] * (1/(1+is_ratio[i]));
  592.                 }
  593. /*                else {
  594.                     printf("Error in streo processing\n");
  595.                 } */
  596.             }
  597.     else  /* mono , bypass xr[0][][] to lr[0][][]*/
  598.         for(sb=0;sb<SBLIMIT;sb++)
  599.             for(ss=0;ss<SSLIMIT;ss++)
  600.                 lr[0][sb][ss] = xr[0][sb][ss];
  601. }
  602.  
  603.  
  604. real Ci[8]={-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
  605.  
  606. void III_antialias(real xr[SBLIMIT][SSLIMIT], real hybridIn[SBLIMIT][SSLIMIT],
  607.                          gr_info_s *gr_info)
  608. /*
  609. double xr[SBLIMIT][SSLIMIT];
  610. double hybridIn[SBLIMIT][SSLIMIT];
  611. struct gr_info_s *gr_info;
  612. frame_params *fr_ps; */
  613. {
  614.     static int    init = 1;
  615.     static real   ca[8],cs[8];
  616.     real          bu,bd;  /* upper and lower butterfly inputs */
  617.     int           ss,sb,sblim;
  618.  
  619.     if (init) {
  620.         int i;
  621.         real    sq;
  622.         for (i=0;i<8;i++) {
  623.             sq=sqrt(1.0+Ci[i]*Ci[i]);
  624.             cs[i] = 1.0/sq;
  625.             ca[i] = Ci[i]/sq;
  626.         }
  627.         init = 0;
  628.     }
  629.  
  630.     /* clear all inputs */
  631.  
  632.      for(sb=0;sb<SBLIMIT;sb++)
  633.          for(ss=0;ss<SSLIMIT;ss++)
  634.              hybridIn[sb][ss] = xr[sb][ss];
  635.  
  636.     if  (gr_info->window_switching_flag && (gr_info->block_type == 2) &&
  637.          !gr_info->mixed_block_flag ) return;
  638.  
  639.     if ( gr_info->window_switching_flag && gr_info->mixed_block_flag &&
  640.       (gr_info->block_type == 2))
  641.         sblim = 1;
  642.     else
  643.         sblim = SBLIMIT-1;
  644.  
  645.     /* 31 alias-reduction operations between each pair of sub-bands */
  646.     /* with 8 butterflies between each pair                         */
  647.  
  648.     for(sb=0;sb<sblim;sb++)
  649.         for(ss=0;ss<8;ss++) {
  650.             bu = xr[sb][17-ss];
  651.             bd = xr[sb+1][ss];
  652.             hybridIn[sb][17-ss] = (bu * cs[ss]) - (bd * ca[ss]);
  653.             hybridIn[sb+1][ss] = (bd * cs[ss]) + (bu * ca[ss]);
  654.             }
  655. }
  656.  
  657. void inv_mdct(real *in, real *out, int block_type)
  658. /*
  659. double in[18];
  660. double out[36];
  661. int block_type; */
  662. {
  663. /*------------------------------------------------------------------*/
  664. /*                                                                  */
  665. /*    Function: Calculation of the inverse MDCT                     */
  666. /*    In the case of short blocks the 3 output vectors are already  */
  667. /*    overlapped and added in this modul.                           */
  668. /*                                                                  */
  669. /*    New layer3                                                    */
  670. /*                                                                  */
  671. /*------------------------------------------------------------------*/
  672.  
  673. //int     k,i,m,N,p;
  674. int     i, m, N, p;
  675. real    tmp[12],sum;
  676. static  real  win[4][36];
  677. static  int init=0;
  678. static  real COS[4*36];
  679.  
  680.      if(init==0){
  681.  
  682.      /* type 0 */
  683.         for(i=0;i<36;i++)
  684.             win[0][i] = sin( PI36 *(i+0.5) );
  685.  
  686.      /* type 1*/
  687.         for(i=0;i<18;i++)
  688.             win[1][i] = sin( PI36 *(i+0.5) );
  689.         for(i=18;i<24;i++)
  690.             win[1][i] = 1.0;
  691.         for(i=24;i<30;i++)
  692.             win[1][i] = sin( PI12 *(i+0.5-18) );
  693.         for(i=30;i<36;i++)
  694.             win[1][i] = 0.0;
  695.  
  696.      /* type 3*/
  697.         for(i=0;i<6;i++)
  698.             win[3][i] = 0.0;
  699.         for(i=6;i<12;i++)
  700.             win[3][i] = sin( PI12 *(i+0.5-6) );
  701.         for(i=12;i<18;i++)
  702.             win[3][i] =1.0;
  703.         for(i=18;i<36;i++)
  704.             win[3][i] = sin( PI36*(i+0.5) );
  705.  
  706.      /* type 2*/
  707.         for(i=0;i<12;i++)
  708.             win[2][i] = sin( PI12*(i+0.5) ) ;
  709.         for(i=12;i<36;i++)
  710.             win[2][i] = 0.0 ;
  711.  
  712.         for (i=0; i<4*36; i++)
  713.             COS[i] = cos(PI72 * i);
  714.  
  715.         init++;
  716.      }
  717.  
  718.      for(i=0;i<36;i++)
  719.          out[i]=0;
  720.  
  721.      if(block_type == 2){
  722.          N=12;
  723.          for(i=0;i<3;i++){
  724.              for(p= 0;p<N;p++){
  725.                  sum = 0.0;
  726.                  for(m=0;m<N/2;m++)
  727.                      sum += in[i+3*m] * cos( PI/(N<<1)*(2*p+1+N/2)*((m<<1)+1) );
  728.                  tmp[p] = sum * win[block_type][p] ;
  729.              }
  730.              for(p=0;p<N;p++)
  731.                  out[6*i+p+6] += tmp[p];
  732.          }
  733.      }
  734.      else{
  735.         N=36;
  736.         for(p= 0;p<N;p++){
  737.             sum = 0.0;
  738.             for(m=0;m<N/2;m++)
  739.               sum += in[m] * COS[((2*p+1+N/2)*((m<<1)+1))%(4*36)];
  740.             out[p] = sum * win[block_type][p];
  741.         }
  742.      }
  743. }
  744.  
  745. void III_hybrid(real *fsIn, real *tsOut, int sb, int ch,
  746.                       gr_info_s *gr_info)
  747. /*
  748. double fsIn[SSLIMIT];
  749. double tsOut[SSLIMIT];
  750. int sb, ch;
  751. struct gr_info_s *gr_info;
  752. frame_params *fr_ps; */
  753. {
  754.     int ss;
  755.     real rawout[36];
  756.     static real prevblck[2][SBLIMIT][SSLIMIT];
  757.     static int init = 1;
  758.     int bt;
  759.  
  760.     if (init) {
  761.         int i,j,k;
  762.  
  763.         for(i=0;i<2;i++)
  764.             for(j=0;j<SBLIMIT;j++)
  765.                 for(k=0;k<SSLIMIT;k++)
  766.                     prevblck[i][j][k]=0.0;
  767.         init = 0;
  768.     }
  769.  
  770.     bt = (gr_info->window_switching_flag && gr_info->mixed_block_flag &&
  771.              (sb < 2)) ? 0 : gr_info->block_type;
  772.  
  773.     inv_mdct( fsIn, rawout, bt);
  774.  
  775.     /* overlap addition */
  776.     for(ss=0; ss<SSLIMIT; ss++) {
  777.         tsOut[ss] = rawout[ss] + prevblck[ch][sb][ss];
  778.         prevblck[ch][sb][ss] = rawout[ss+18];
  779.     }
  780. }
  781.  
  782. void l3decoder(Ibitstream *stream, Header *header, Bit_Reserve *br,
  783.                     III_side_info_t *pIII_side_info, III_scalefac_t *pIII_scalefac,
  784.                     SynthesisFilter *filter1, SynthesisFilter *filter2,
  785.                     Obuffer *buffer)
  786. {
  787.      int nSlots;
  788.      int gr, ch, ss, sb, main_data_end, flush_main ;
  789.      int  bytes_to_discard ;
  790.      static int frame_start = 0; // problem for next time we load a Layer III file
  791.  
  792.      char scratch[128];
  793.      char numstr[16];
  794.  
  795.      uint32 channels = (header->mode() == single_channel) ? 1 : 2;
  796.  
  797.      int bitsPerSlot = 8;
  798.  
  799.      stream->get_side_info(channels, pIII_side_info);
  800.  
  801.      nSlots = header->slots();
  802.  
  803.      for (; nSlots > 0; nSlots--)  // read main data.
  804.          br->hputbuf((unsigned int) stream->get_bits(8));
  805.  
  806.      main_data_end = br->hsstell() / 8; // of previous frame
  807.  
  808.      if ( flush_main=(br->hsstell() % bitsPerSlot) ) {
  809.             br->hgetbits((int)(bitsPerSlot - flush_main));
  810.             main_data_end++;
  811.      }
  812.  
  813.      bytes_to_discard = frame_start - main_data_end
  814.                               - pIII_side_info->main_data_begin ;
  815.      if( main_data_end > 4096 ) {
  816.             frame_start -= 4096;
  817.             br->rewindNbytes( 4096 );
  818.      }
  819.  
  820.      frame_start += header->slots();
  821.  
  822.  if (bytes_to_discard < 0) {
  823. //            printf("Not enough main data to decode frame %d.  Frame discarded.\n",
  824. //                                frameNum - 1);
  825. //            break;
  826.             return;
  827.      }
  828.  
  829.      for (; bytes_to_discard > 0; bytes_to_discard--) br->hgetbits(8);
  830.  
  831.      for (gr=0;gr<2;gr++) {
  832.             real lr[2][SBLIMIT][SSLIMIT],ro[2][SBLIMIT][SSLIMIT];
  833.  
  834.             for (ch=0; ch<channels; ch++) {
  835.               int is[SBLIMIT][SSLIMIT];   // Quantized samples.
  836.               int part2_start= br->hsstell();
  837.               III_get_scale_factors(pIII_scalefac,pIII_side_info, br, gr, ch);
  838. //              MessageBox(NULL, "scalefactors", "scalefactors ok", MB_OK);
  839.               III_hufman_decode(is, pIII_side_info, ch, gr, part2_start,
  840.                                       header, br);
  841. //              MessageBox(NULL, "huffman", "huffman decode ok", MB_OK);
  842.               III_dequantize_sample(is, ro[ch], pIII_scalefac,
  843.                                           &(pIII_side_info->ch[ch].gr[gr]), ch, header);
  844. //              MessageBox(NULL, "dequantize", "dequantize sample ok", MB_OK);
  845.             }
  846.  
  847. //            MessageBox(NULL, "stereo", "about to do stereo", MB_OK);
  848.             III_stereo(ro,lr, pIII_scalefac,
  849.                           &(pIII_side_info->ch[0].gr[gr]), header);
  850. //            MessageBox(NULL, "stereo", "stereo ok", MB_OK);
  851.  
  852.             for (ch=0; ch<channels; ch++) {
  853.                     real re[SBLIMIT][SSLIMIT];
  854.                     real hybridIn[SBLIMIT][SSLIMIT];// Hybrid filter input
  855.                     real hybridOut[SBLIMIT][SSLIMIT];// Hybrid filter out
  856.                     III_reorder (lr[ch],re,&(pIII_side_info->ch[ch].gr[gr]),
  857.                                      header);
  858.                     III_antialias(re, hybridIn, // Antialias butterflies.
  859.                                       &(pIII_side_info->ch[ch].gr[gr]));
  860.                     for (sb=0; sb<SBLIMIT; sb++) { // Hybrid synthesis.
  861.                          III_hybrid(hybridIn[sb], hybridOut[sb], sb, ch,
  862.                                         &(pIII_side_info->ch[ch].gr[gr]));
  863.                     }
  864.                     // change these limits to ss=1; ss<18; ss+=2
  865.                     //                               sb=1; sb<SBLIMIT; sb+=2
  866.                     // no if required
  867.                     for (ss=0;ss<18;ss++) // Frequency inversion for polyphase.
  868.                           for (sb=0; sb<SBLIMIT; sb++)
  869.                               if ((ss%2) && (sb%2))
  870.                                   hybridOut[sb][ss] = -hybridOut[sb][ss];
  871.  
  872.                     if (ch == 0)
  873.                       for (ss=0;ss<18;ss++) { // Polyphase synthesis
  874.                             for (sb=0; sb<SBLIMIT; sb++)
  875.                                 filter1->input_sample(hybridOut[sb][ss], sb);
  876.                             filter1->calculate_pcm_samples(buffer);
  877.                       }
  878.                     else
  879.                       for (ss=0;ss<18;ss++) { // Polyphase synthesis
  880.                             for (sb=0; sb<SBLIMIT; sb++)
  881.                                 filter2->input_sample(hybridOut[sb][ss], sb);
  882.                             filter2->calculate_pcm_samples(buffer);
  883.                       }
  884.             }    // channels
  885.      }    // granule
  886.  
  887.      buffer->write_buffer (1);        // write to stdout
  888. }
  889.  
  890.  
  891.