home *** CD-ROM | disk | FTP | other *** search
- #define STRICT
- #define WIN32_LEAN_AND_MEAN
- /*
- #define NOMCX
- #define NOIME
- #define NOGDI
- #define NOUSER
- #define NOSOUND
- #define NOCOMM
- #define NODRIVERS
- #define OEMRESOURCE
- #define NONLS
- #define NOSERVICE
- #define NOKANJI
- #define NOMINMAX
- #define NOLOGERROR
- #define NOPROFILER
- #define NOMEMMGR
- #define NOLFILEIO
- #define NOOPENFILE
- #define NORESOURCE
- #define NOATOM
- #define NOLANGUAGE
- //#define NOLSTRING
- #define NODBCS
- #define NOKEYBOARDINFO
- #define NOGDICAPMASKS
- #define NOCOLOR
- #define NOGDIOBJ
- #define NODRAWTEXT
- #define NOTEXTMETRIC
- #define NOSCALABLEFONT
- #define NOBITMAP
- #define NORASTEROPS
- #define NOMETAFILE
- #define NOSYSMETRICS
- #define NOSYSTEMPARAMSINFO
- #define NOMSG
- #define NOWINSTYLES
- #define NOWINOFFSETS
- #define NOSHOWWINDOW
- #define NODEFERWINDOWPOS
- #define NOVIRTUALKEYCODES
- #define NOKEYSTATES
- #define NOWH
- #define NOMENUS
- #define NOSCROLL
- #define NOCLIPBOARD
- #define NOICONS
- //#define NOMB
- #define NOSYSCOMMANDS
- #define NOMDI
- #define NOCTLMGR
- #define NOWINMESSAGES
- #define NOHELP
- #define _WINUSER_ */
- #include <windows.h>
- #include <stdlib.h>
-
- #include "all.h"
- #include "l3type.h"
- #include "ibitstr.h"
- #include "obuffer.h"
- #include "bit_res.h"
- #include "header.h"
- #include "synfilt.h"
- #include "math.h"
- #include "huffman.h"
- #include "layer3.h"
-
- // Layer III routines
-
- struct {
- int l[5];
- int s[3];} sfbtable = {{0, 6, 11, 16, 21},
- {0, 6, 12}};
-
- int slen[2][16] = {{0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
- {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}};
-
- void III_get_scale_factors(III_scalefac_t *scalefac, III_side_info_t *si,
- Bit_Reserve *br, int gr, int ch)
- {
- int sfb, i, window;
- gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
-
- if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
- if (gr_info->mixed_block_flag) { /* MIXED */ /* NEW - ag 11/25 */
- for (sfb = 0; sfb < 8; sfb++)
- (*scalefac)[ch].l[sfb] = br->hgetbits(
- slen[0][gr_info->scalefac_compress]);
- for (sfb = 3; sfb < 6; sfb++)
- for (window=0; window<3; window++)
- (*scalefac)[ch].s[window][sfb] = br->hgetbits(
- slen[0][gr_info->scalefac_compress]);
- for (sfb = 6; sfb < 12; sfb++)
- for (window=0; window<3; window++)
- (*scalefac)[ch].s[window][sfb] = br->hgetbits(
- slen[1][gr_info->scalefac_compress]);
- for (sfb=12,window=0; window<3; window++)
- (*scalefac)[ch].s[window][sfb] = 0;
- }
- else { /* SHORT*/
- for (i=0; i<2; i++)
- for (sfb = sfbtable.s[i]; sfb < sfbtable.s[i+1]; sfb++)
- for (window=0; window<3; window++)
- (*scalefac)[ch].s[window][sfb] = br->hgetbits(
- slen[i][gr_info->scalefac_compress]);
- for (sfb=12,window=0; window<3; window++)
- (*scalefac)[ch].s[window][sfb] = 0;
- }
- }
- else { /* LONG types 0,1,3 */
- for (i=0; i<4; i++) {
- if ((si->ch[ch].scfsi[i] == 0) || (gr == 0))
- for (sfb = sfbtable.l[i]; sfb < sfbtable.l[i+1]; sfb++)
- (*scalefac)[ch].l[sfb] = br->hgetbits(
- slen[(i<2)?0:1][gr_info->scalefac_compress]);
- }
- (*scalefac)[ch].l[22] = 0;
- }
- }
-
- struct {
- int l[23];
- int s[14];} sfBandIndex[3] =
- {{{0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576},
- {0,4,8,12,16,22,30,40,52,66,84,106,136,192}},
- {{0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576},
- {0,4,8,12,16,22,28,38,50,64,80,100,126,192}},
- {{0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
- {0,4,8,12,16,22,30,42,58,78,104,138,180,192}}};
-
- void III_hufman_decode(int is[SBLIMIT][SSLIMIT], III_side_info_t *si, int ch, int gr,
- int part2_start, Header *header, Bit_Reserve *br)
- {
- int i, x, y;
- int v, w;
- struct huffcodetab *h;
- int region1Start;
- int region2Start;
- // int bt = (*si).ch[ch].gr[gr].window_switching_flag && ((*si).ch[ch].gr[gr].block_type == 2);
-
-
- char numstr[32];
- // MessageBox(NULL, "initializing huffman table", "initializing huffman table",
- // MB_OK);
-
- initialize_huffman();
-
- // MessageBox(NULL, "huffman table initialized ok", "huffman table initialized ok",
- // MB_OK);
-
-
- /* Find region boundary for short block case. */
-
- if ( ((*si).ch[ch].gr[gr].window_switching_flag) &&
- ((*si).ch[ch].gr[gr].block_type == 2) ) {
-
-
- /* Region2. */
-
- region1Start = 36; /* sfb[9/3]*3=36 */
- region2Start = 576; /* No Region2 for short block case. */
- }
-
- else { /* Find region boundary for long block case. */
-
- region1Start = sfBandIndex[header->sample_frequency()]
- .l[(*si).ch[ch].gr[gr].region0_count + 1]; /* MI */
- region2Start = sfBandIndex[header->sample_frequency()]
- .l[(*si).ch[ch].gr[gr].region0_count +
- (*si).ch[ch].gr[gr].region1_count + 2]; /* MI */
- }
-
- // MessageBox(NULL, "looked at side info", "huffman decode",
- // MB_OK);
-
-
- /* Read bigvalues area. */
- for (i=0; i<(*si).ch[ch].gr[gr].big_values*2; i+=2) {
- if (i<region1Start) h = &ht[(*si).ch[ch].gr[gr].table_select[0]];
- else if (i<region2Start) h = &ht[(*si).ch[ch].gr[gr].table_select[1]];
- else h = &ht[(*si).ch[ch].gr[gr].table_select[2]];
-
- /* if (i>560)
- MessageBox(NULL, "about to do huff decoder", itoa(i, numstr, 10),
- MB_OK); */
-
- huffman_decoder(h, &x, &y, &v, &w, br);
-
- is[i/SSLIMIT][i%SSLIMIT] = x;
- is[(i+1)/SSLIMIT][(i+1)%SSLIMIT] = y;
- }
-
- // MessageBox(NULL, "out of loop", "huffman decoder",
- // MB_OK);
-
-
- /* Read count1 area. */
- h = &ht[(*si).ch[ch].gr[gr].count1table_select+32];
- while ((br->hsstell() < part2_start + (*si).ch[ch].gr[gr].part2_3_length ) &&
- ( i < SSLIMIT*SBLIMIT )) {
- huffman_decoder(h, &x, &y, &v, &w, br);
- is[i/SSLIMIT][i%SSLIMIT] = v;
- is[(i+1)/SSLIMIT][(i+1)%SSLIMIT] = w;
- is[(i+2)/SSLIMIT][(i+2)%SSLIMIT] = x;
- is[(i+3)/SSLIMIT][(i+3)%SSLIMIT] = y;
- i += 4;
- }
-
- if (br->hsstell() > part2_start + (*si).ch[ch].gr[gr].part2_3_length)
- { i -=4;
- br->rewindNbits(br->hsstell()-part2_start - (*si).ch[ch].gr[gr].part2_3_length);
- }
-
- /* Dismiss stuffing Bits */
- if (br->hsstell() < part2_start + (*si).ch[ch].gr[gr].part2_3_length )
- br->hgetbits( part2_start + (*si).ch[ch].gr[gr].part2_3_length - br->hsstell());
-
- /* Zero out rest. */
- for (; i<SSLIMIT*SBLIMIT; i++)
- is[i/SSLIMIT][i%SSLIMIT] = 0;
-
- }
-
- 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};
-
- void III_dequantize_sample(int is[SBLIMIT][SSLIMIT], real xr[SBLIMIT][SSLIMIT],
- III_scalefac_t *scalefac, gr_info_s *gr_info,
- int ch, Header *header)
- {
- // int ss,sb,cb=0,sfreq=fr_ps->header->sampling_frequency;
- // int stereo = fr_ps->stereo;
-
- int ss, sb, cb=0, sfreq=header->sample_frequency();
- // int stereo = ((header->mode() == single_channel) ? 1 : 2);
-
- int next_cb_boundary, cb_begin, cb_width, sign;
-
- /* choose correct scalefactor band per block type, initalize boundary */
-
- if (gr_info->window_switching_flag && (gr_info->block_type == 2) )
- if (gr_info->mixed_block_flag)
- next_cb_boundary=sfBandIndex[sfreq].l[1]; /* LONG blocks: 0,1,3 */
- else {
- next_cb_boundary=sfBandIndex[sfreq].s[1]*3; /* pure SHORT block */
- cb_width = sfBandIndex[sfreq].s[1];
- cb_begin = 0;
- }
- else
- next_cb_boundary=sfBandIndex[sfreq].l[1]; /* LONG blocks: 0,1,3 */
-
- /* apply formula per block type */
-
- for (sb=0 ; sb < SBLIMIT ; sb++)
- for (ss=0 ; ss < SSLIMIT ; ss++) {
-
- if ( (sb*18)+ss == next_cb_boundary) { /* Adjust critical band boundary */
- if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
- if (gr_info->mixed_block_flag) {
- if (((sb*18)+ss) == sfBandIndex[sfreq].l[8]) {
- next_cb_boundary=sfBandIndex[sfreq].s[4]*3;
- cb = 3;
- cb_width = sfBandIndex[sfreq].s[cb+1] -
- sfBandIndex[sfreq].s[cb];
- cb_begin = sfBandIndex[sfreq].s[cb]*3;
- }
- else if (((sb*18)+ss) < sfBandIndex[sfreq].l[8])
- next_cb_boundary = sfBandIndex[sfreq].l[(++cb)+1];
- else {
- next_cb_boundary = sfBandIndex[sfreq].s[(++cb)+1]*3;
- cb_width = sfBandIndex[sfreq].s[cb+1] -
- sfBandIndex[sfreq].s[cb];
- cb_begin = sfBandIndex[sfreq].s[cb]*3;
- }
- }
- else {
- next_cb_boundary = sfBandIndex[sfreq].s[(++cb)+1]*3;
- cb_width = sfBandIndex[sfreq].s[cb+1] -
- sfBandIndex[sfreq].s[cb];
- cb_begin = sfBandIndex[sfreq].s[cb]*3;
- }
- }
- else /* long blocks */
- next_cb_boundary = sfBandIndex[sfreq].l[(++cb)+1];
- }
-
- /* Compute overall (global) scaling. */
-
- xr[sb][ss] = pow( 2.0 , (0.25 * (gr_info->global_gain - 210.0)));
-
- /* Do long/short dependent scaling operations. */
-
- if (gr_info->window_switching_flag && (
- ((gr_info->block_type == 2) && (gr_info->mixed_block_flag == 0)) ||
- ((gr_info->block_type == 2) && gr_info->mixed_block_flag && (sb >= 2)) )) {
-
- xr[sb][ss] *= pow(2.0, 0.25 * -8.0 *
- gr_info->subblock_gain[(((sb*18)+ss) - cb_begin)/cb_width]);
- xr[sb][ss] *= pow(2.0, 0.25 * -2.0 * (1.0+gr_info->scalefac_scale)
- * (*scalefac)[ch].s[(((sb*18)+ss) - cb_begin)/cb_width][cb]);
- }
- else { /* LONG block types 0,1,3 & 1st 2 subbands of switched blocks */
- xr[sb][ss] *= pow(2.0, -0.5 * (1.0+gr_info->scalefac_scale)
- * ((*scalefac)[ch].l[cb]
- + gr_info->preflag * pretab[cb]));
- }
-
- /* Scale quantized value. */
-
- sign = (is[sb][ss]<0) ? 1 : 0;
- xr[sb][ss] *= pow( (double) abs(is[sb][ss]), ((double)4.0/3.0) );
- if (sign) xr[sb][ss] = -xr[sb][ss];
- }
- }
-
- void III_reorder (real xr[SBLIMIT][SSLIMIT], real ro[SBLIMIT][SSLIMIT],
- gr_info_s *gr_info, Header *header)
- /*
- double xr[SBLIMIT][SSLIMIT];
- double ro[SBLIMIT][SSLIMIT];
- struct gr_info_s *gr_info;
- frame_params *fr_ps; */
- {
- int sfreq=header->sample_frequency();
- int sfb, sfb_start, sfb_lines;
- int sb, ss, window, freq, src_line, des_line;
-
- for(sb=0;sb<SBLIMIT;sb++)
- for(ss=0;ss<SSLIMIT;ss++)
- ro[sb][ss] = 0.0;
-
- if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
- if (gr_info->mixed_block_flag) {
- /* NO REORDER FOR LOW 2 SUBBANDS */
- for (sb=0 ; sb < 2 ; sb++)
- for (ss=0 ; ss < SSLIMIT ; ss++) {
- ro[sb][ss] = xr[sb][ss];
- }
- /* REORDERING FOR REST SWITCHED SHORT */
- for(sfb=3,sfb_start=sfBandIndex[sfreq].s[3],
- sfb_lines=sfBandIndex[sfreq].s[4] - sfb_start;
- sfb < 13; sfb++,sfb_start=sfBandIndex[sfreq].s[sfb],
- (sfb_lines=sfBandIndex[sfreq].s[sfb+1] - sfb_start))
- for(window=0; window<3; window++)
- for(freq=0;freq<sfb_lines;freq++) {
- src_line = sfb_start*3 + window*sfb_lines + freq;
- des_line = (sfb_start*3) + window + (freq*3);
- ro[des_line/SSLIMIT][des_line%SSLIMIT] =
- xr[src_line/SSLIMIT][src_line%SSLIMIT];
- }
- }
- else { /* pure short */
- for(sfb=0,sfb_start=0,sfb_lines=sfBandIndex[sfreq].s[1];
- sfb < 13; sfb++,sfb_start=sfBandIndex[sfreq].s[sfb],
- (sfb_lines=sfBandIndex[sfreq].s[sfb+1] - sfb_start))
- for(window=0; window<3; window++)
- for(freq=0;freq<sfb_lines;freq++) {
- src_line = sfb_start*3 + window*sfb_lines + freq;
- des_line = (sfb_start*3) + window + (freq*3);
- ro[des_line/SSLIMIT][des_line%SSLIMIT] =
- xr[src_line/SSLIMIT][src_line%SSLIMIT];
- }
- }
- }
- else { /*long blocks */
- for (sb=0 ; sb < SBLIMIT ; sb++)
- for (ss=0 ; ss < SSLIMIT ; ss++)
- ro[sb][ss] = xr[sb][ss];
- }
- }
-
- #define PI 3.141593
- #define PI12 0.2617994
- #define PI36 0.08726646
- #define PI72 0.04363323
-
- void III_stereo(real xr[2][SBLIMIT][SSLIMIT], real lr[2][SBLIMIT][SSLIMIT],
- III_scalefac_t *scalefac, gr_info_s *gr_info, Header *header)
- /*
- double xr[2][SBLIMIT][SSLIMIT];
- double lr[2][SBLIMIT][SSLIMIT];
- III_scalefac_t *scalefac;
- struct gr_info_s *gr_info;
- frame_params *fr_ps; */
- {
- // int sfreq = fr_ps->header->sampling_frequency;
- int sfreq = header->sample_frequency();
- // int stereo = fr_ps->stereo;
- int stereo = (header->mode() == single_channel) ? 1 : 2;
- /* int ms_stereo = (fr_ps->header->mode == MPG_MD_JOINT_STEREO) &&
- (fr_ps->header->mode_ext & 0x2); */
- int ms_stereo = (header->mode() == joint_stereo) &&
- (header->mode_extension() & 0x2);
- /* int i_stereo = (fr_ps->header->mode == MPG_MD_JOINT_STEREO) &&
- (fr_ps->header->mode_ext & 0x1); */
- int i_stereo = (header->mode() == joint_stereo) &&
- (header->mode_extension() & 0x1);
- // int js_bound; /* frequency line that marks the beggining of the zero part */
- // int sfb,next_sfb_boundary;
- int sfb;
- int i,j,sb,ss,ch,is_pos[1152];
- real is_ratio[1152];
-
- /* intialization */
- for ( i=0; i<576; i++ )
- is_pos[i] = 7;
-
- if ((stereo == 2) && i_stereo )
- { if (gr_info->window_switching_flag && (gr_info->block_type == 2))
- { if( gr_info->mixed_block_flag )
- { int max_sfb = 0;
-
- for ( j=0; j<3; j++ )
- { int sfbcnt;
- sfbcnt = 2;
- for( sfb=12; sfb >=3; sfb-- )
- { int lines;
- lines = sfBandIndex[sfreq].s[sfb+1]-sfBandIndex[sfreq].s[sfb];
- i = 3*sfBandIndex[sfreq].s[sfb] + (j+1) * lines - 1;
- while ( lines > 0 )
- { if ( xr[1][i/SSLIMIT][i%SSLIMIT] != 0.0 )
- { sfbcnt = sfb;
- sfb = -10;
- lines = -10;
- }
- lines--;
- i--;
- }
- }
- sfb = sfbcnt + 1;
-
- if ( sfb > max_sfb )
- max_sfb = sfb;
-
- while( sfb<12 )
- { sb = sfBandIndex[sfreq].s[sfb+1]-sfBandIndex[sfreq].s[sfb];
- i = 3*sfBandIndex[sfreq].s[sfb] + j * sb;
- for ( ; sb > 0; sb--)
- { is_pos[i] = (*scalefac)[1].s[j][sfb];
- if ( is_pos[i] != 7 )
- is_ratio[i] = tan( is_pos[i] * PI12);
- i++;
- }
- sfb++;
- }
- sb = sfBandIndex[sfreq].s[11]-sfBandIndex[sfreq].s[10];
- sfb = 3*sfBandIndex[sfreq].s[10] + j * sb;
- sb = sfBandIndex[sfreq].s[12]-sfBandIndex[sfreq].s[11];
- i = 3*sfBandIndex[sfreq].s[11] + j * sb;
- for ( ; sb > 0; sb-- )
- { is_pos[i] = is_pos[sfb];
- is_ratio[i] = is_ratio[sfb];
- i++;
- }
- }
- if ( max_sfb <= 3 )
- { i = 2;
- ss = 17;
- sb = -1;
- while ( i >= 0 )
- { if ( xr[1][i][ss] != 0.0 )
- { sb = i*18+ss;
- i = -1;
- } else
- { ss--;
- if ( ss < 0 )
- { i--;
- ss = 17;
- }
- }
- }
- i = 0;
- while ( sfBandIndex[sfreq].l[i] <= sb )
- i++;
- sfb = i;
- i = sfBandIndex[sfreq].l[i];
- for ( ; sfb<8; sfb++ )
- { sb = sfBandIndex[sfreq].l[sfb+1]-sfBandIndex[sfreq].l[sfb];
- for ( ; sb > 0; sb--)
- { is_pos[i] = (*scalefac)[1].l[sfb];
- if ( is_pos[i] != 7 )
- is_ratio[i] = tan( is_pos[i] * PI12);
- i++;
- }
- }
- }
- } else
- { for ( j=0; j<3; j++ )
- { int sfbcnt;
- sfbcnt = -1;
- for( sfb=12; sfb >=0; sfb-- )
- { int lines;
- lines = sfBandIndex[sfreq].s[sfb+1]-sfBandIndex[sfreq].s[sfb];
- i = 3*sfBandIndex[sfreq].s[sfb] + (j+1) * lines - 1;
- while ( lines > 0 )
- { if ( xr[1][i/SSLIMIT][i%SSLIMIT] != 0.0 )
- { sfbcnt = sfb;
- sfb = -10;
- lines = -10;
- }
- lines--;
- i--;
- }
- }
- sfb = sfbcnt + 1;
- while( sfb<12 )
- { sb = sfBandIndex[sfreq].s[sfb+1]-sfBandIndex[sfreq].s[sfb];
- i = 3*sfBandIndex[sfreq].s[sfb] + j * sb;
- for ( ; sb > 0; sb--)
- { is_pos[i] = (*scalefac)[1].s[j][sfb];
- if ( is_pos[i] != 7 )
- is_ratio[i] = tan( is_pos[i] * PI12);
- i++;
- }
- sfb++;
- }
-
- sb = sfBandIndex[sfreq].s[11]-sfBandIndex[sfreq].s[10];
- sfb = 3*sfBandIndex[sfreq].s[10] + j * sb;
- sb = sfBandIndex[sfreq].s[12]-sfBandIndex[sfreq].s[11];
- i = 3*sfBandIndex[sfreq].s[11] + j * sb;
- for ( ; sb > 0; sb-- ) // problem here?
- { is_pos[i] = is_pos[sfb];
- is_ratio[i] = is_ratio[sfb];
- i++;
- }
- }
- }
- } else
- { i = 31;
- ss = 17;
- sb = 0;
- while ( i >= 0 )
- { if ( xr[1][i][ss] != 0.0 )
- { sb = i*18+ss;
- i = -1;
- } else
- { ss--;
- if ( ss < 0 )
- { i--;
- ss = 17;
- }
- }
- }
- i = 0;
- while ( sfBandIndex[sfreq].l[i] <= sb )
- i++;
- sfb = i;
- i = sfBandIndex[sfreq].l[i];
- for ( ; sfb<21; sfb++ )
- { sb = sfBandIndex[sfreq].l[sfb+1] - sfBandIndex[sfreq].l[sfb];
- for ( ; sb > 0; sb--) // problem is here
- { is_pos[i] = (*scalefac)[1].l[sfb];
- if ( is_pos[i] != 7 )
- is_ratio[i] = tan( is_pos[i] * PI12);
- i++;
- }
- }
- sfb = sfBandIndex[sfreq].l[20];
- for ( sb = 576 - sfBandIndex[sfreq].l[21]; sb > 0; sb-- )
- { is_pos[i] = is_pos[sfb];
- is_ratio[i] = is_ratio[sfb];
- i++;
- }
- }
- }
-
- for(ch=0;ch<2;ch++)
- for(sb=0;sb<SBLIMIT;sb++)
- for(ss=0;ss<SSLIMIT;ss++)
- lr[ch][sb][ss] = 0;
-
- if (stereo==2)
- for(sb=0;sb<SBLIMIT;sb++)
- for(ss=0;ss<SSLIMIT;ss++) {
- i = (sb*18)+ss;
- if ( is_pos[i] == 7 ) {
- if ( ms_stereo ) {
- lr[0][sb][ss] = (xr[0][sb][ss]+xr[1][sb][ss])/1.41421356;
- lr[1][sb][ss] = (xr[0][sb][ss]-xr[1][sb][ss])/1.41421356;
- }
- else {
- lr[0][sb][ss] = xr[0][sb][ss];
- lr[1][sb][ss] = xr[1][sb][ss];
- }
- }
- else if (i_stereo ) {
- lr[0][sb][ss] = xr[0][sb][ss] * (is_ratio[i]/(1+is_ratio[i]));
- lr[1][sb][ss] = xr[0][sb][ss] * (1/(1+is_ratio[i]));
- }
- /* else {
- printf("Error in streo processing\n");
- } */
- }
- else /* mono , bypass xr[0][][] to lr[0][][]*/
- for(sb=0;sb<SBLIMIT;sb++)
- for(ss=0;ss<SSLIMIT;ss++)
- lr[0][sb][ss] = xr[0][sb][ss];
- }
-
-
- real Ci[8]={-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
-
- void III_antialias(real xr[SBLIMIT][SSLIMIT], real hybridIn[SBLIMIT][SSLIMIT],
- gr_info_s *gr_info)
- /*
- double xr[SBLIMIT][SSLIMIT];
- double hybridIn[SBLIMIT][SSLIMIT];
- struct gr_info_s *gr_info;
- frame_params *fr_ps; */
- {
- static int init = 1;
- static real ca[8],cs[8];
- real bu,bd; /* upper and lower butterfly inputs */
- int ss,sb,sblim;
-
- if (init) {
- int i;
- real sq;
- for (i=0;i<8;i++) {
- sq=sqrt(1.0+Ci[i]*Ci[i]);
- cs[i] = 1.0/sq;
- ca[i] = Ci[i]/sq;
- }
- init = 0;
- }
-
- /* clear all inputs */
-
- for(sb=0;sb<SBLIMIT;sb++)
- for(ss=0;ss<SSLIMIT;ss++)
- hybridIn[sb][ss] = xr[sb][ss];
-
- if (gr_info->window_switching_flag && (gr_info->block_type == 2) &&
- !gr_info->mixed_block_flag ) return;
-
- if ( gr_info->window_switching_flag && gr_info->mixed_block_flag &&
- (gr_info->block_type == 2))
- sblim = 1;
- else
- sblim = SBLIMIT-1;
-
- /* 31 alias-reduction operations between each pair of sub-bands */
- /* with 8 butterflies between each pair */
-
- for(sb=0;sb<sblim;sb++)
- for(ss=0;ss<8;ss++) {
- bu = xr[sb][17-ss];
- bd = xr[sb+1][ss];
- hybridIn[sb][17-ss] = (bu * cs[ss]) - (bd * ca[ss]);
- hybridIn[sb+1][ss] = (bd * cs[ss]) + (bu * ca[ss]);
- }
- }
-
- void inv_mdct(real *in, real *out, int block_type)
- /*
- double in[18];
- double out[36];
- int block_type; */
- {
- /*------------------------------------------------------------------*/
- /* */
- /* Function: Calculation of the inverse MDCT */
- /* In the case of short blocks the 3 output vectors are already */
- /* overlapped and added in this modul. */
- /* */
- /* New layer3 */
- /* */
- /*------------------------------------------------------------------*/
-
- //int k,i,m,N,p;
- int i, m, N, p;
- real tmp[12],sum;
- static real win[4][36];
- static int init=0;
- static real COS[4*36];
-
- if(init==0){
-
- /* type 0 */
- for(i=0;i<36;i++)
- win[0][i] = sin( PI36 *(i+0.5) );
-
- /* type 1*/
- for(i=0;i<18;i++)
- win[1][i] = sin( PI36 *(i+0.5) );
- for(i=18;i<24;i++)
- win[1][i] = 1.0;
- for(i=24;i<30;i++)
- win[1][i] = sin( PI12 *(i+0.5-18) );
- for(i=30;i<36;i++)
- win[1][i] = 0.0;
-
- /* type 3*/
- for(i=0;i<6;i++)
- win[3][i] = 0.0;
- for(i=6;i<12;i++)
- win[3][i] = sin( PI12 *(i+0.5-6) );
- for(i=12;i<18;i++)
- win[3][i] =1.0;
- for(i=18;i<36;i++)
- win[3][i] = sin( PI36*(i+0.5) );
-
- /* type 2*/
- for(i=0;i<12;i++)
- win[2][i] = sin( PI12*(i+0.5) ) ;
- for(i=12;i<36;i++)
- win[2][i] = 0.0 ;
-
- for (i=0; i<4*36; i++)
- COS[i] = cos(PI72 * i);
-
- init++;
- }
-
- for(i=0;i<36;i++)
- out[i]=0;
-
- if(block_type == 2){
- N=12;
- for(i=0;i<3;i++){
- for(p= 0;p<N;p++){
- sum = 0.0;
- for(m=0;m<N/2;m++)
- sum += in[i+3*m] * cos( PI/(N<<1)*(2*p+1+N/2)*((m<<1)+1) );
- tmp[p] = sum * win[block_type][p] ;
- }
- for(p=0;p<N;p++)
- out[6*i+p+6] += tmp[p];
- }
- }
- else{
- N=36;
- for(p= 0;p<N;p++){
- sum = 0.0;
- for(m=0;m<N/2;m++)
- sum += in[m] * COS[((2*p+1+N/2)*((m<<1)+1))%(4*36)];
- out[p] = sum * win[block_type][p];
- }
- }
- }
-
- void III_hybrid(real *fsIn, real *tsOut, int sb, int ch,
- gr_info_s *gr_info)
- /*
- double fsIn[SSLIMIT];
- double tsOut[SSLIMIT];
- int sb, ch;
- struct gr_info_s *gr_info;
- frame_params *fr_ps; */
- {
- int ss;
- real rawout[36];
- static real prevblck[2][SBLIMIT][SSLIMIT];
- static int init = 1;
- int bt;
-
- if (init) {
- int i,j,k;
-
- for(i=0;i<2;i++)
- for(j=0;j<SBLIMIT;j++)
- for(k=0;k<SSLIMIT;k++)
- prevblck[i][j][k]=0.0;
- init = 0;
- }
-
- bt = (gr_info->window_switching_flag && gr_info->mixed_block_flag &&
- (sb < 2)) ? 0 : gr_info->block_type;
-
- inv_mdct( fsIn, rawout, bt);
-
- /* overlap addition */
- for(ss=0; ss<SSLIMIT; ss++) {
- tsOut[ss] = rawout[ss] + prevblck[ch][sb][ss];
- prevblck[ch][sb][ss] = rawout[ss+18];
- }
- }
-
- void l3decoder(Ibitstream *stream, Header *header, Bit_Reserve *br,
- III_side_info_t *pIII_side_info, III_scalefac_t *pIII_scalefac,
- SynthesisFilter *filter1, SynthesisFilter *filter2,
- Obuffer *buffer)
- {
- int nSlots;
- int gr, ch, ss, sb, main_data_end, flush_main ;
- int bytes_to_discard ;
- static int frame_start = 0; // problem for next time we load a Layer III file
-
- char scratch[128];
- char numstr[16];
-
- uint32 channels = (header->mode() == single_channel) ? 1 : 2;
-
- int bitsPerSlot = 8;
-
- stream->get_side_info(channels, pIII_side_info);
-
- nSlots = header->slots();
-
- for (; nSlots > 0; nSlots--) // read main data.
- br->hputbuf((unsigned int) stream->get_bits(8));
-
- main_data_end = br->hsstell() / 8; // of previous frame
-
- if ( flush_main=(br->hsstell() % bitsPerSlot) ) {
- br->hgetbits((int)(bitsPerSlot - flush_main));
- main_data_end++;
- }
-
- bytes_to_discard = frame_start - main_data_end
- - pIII_side_info->main_data_begin ;
- if( main_data_end > 4096 ) {
- frame_start -= 4096;
- br->rewindNbytes( 4096 );
- }
-
- frame_start += header->slots();
-
- if (bytes_to_discard < 0) {
- // printf("Not enough main data to decode frame %d. Frame discarded.\n",
- // frameNum - 1);
- // break;
- return;
- }
-
- for (; bytes_to_discard > 0; bytes_to_discard--) br->hgetbits(8);
-
- for (gr=0;gr<2;gr++) {
- real lr[2][SBLIMIT][SSLIMIT],ro[2][SBLIMIT][SSLIMIT];
-
- for (ch=0; ch<channels; ch++) {
- int is[SBLIMIT][SSLIMIT]; // Quantized samples.
- int part2_start= br->hsstell();
- III_get_scale_factors(pIII_scalefac,pIII_side_info, br, gr, ch);
- // MessageBox(NULL, "scalefactors", "scalefactors ok", MB_OK);
- III_hufman_decode(is, pIII_side_info, ch, gr, part2_start,
- header, br);
- // MessageBox(NULL, "huffman", "huffman decode ok", MB_OK);
- III_dequantize_sample(is, ro[ch], pIII_scalefac,
- &(pIII_side_info->ch[ch].gr[gr]), ch, header);
- // MessageBox(NULL, "dequantize", "dequantize sample ok", MB_OK);
- }
-
- // MessageBox(NULL, "stereo", "about to do stereo", MB_OK);
- III_stereo(ro,lr, pIII_scalefac,
- &(pIII_side_info->ch[0].gr[gr]), header);
- // MessageBox(NULL, "stereo", "stereo ok", MB_OK);
-
- for (ch=0; ch<channels; ch++) {
- real re[SBLIMIT][SSLIMIT];
- real hybridIn[SBLIMIT][SSLIMIT];// Hybrid filter input
- real hybridOut[SBLIMIT][SSLIMIT];// Hybrid filter out
- III_reorder (lr[ch],re,&(pIII_side_info->ch[ch].gr[gr]),
- header);
- III_antialias(re, hybridIn, // Antialias butterflies.
- &(pIII_side_info->ch[ch].gr[gr]));
- for (sb=0; sb<SBLIMIT; sb++) { // Hybrid synthesis.
- III_hybrid(hybridIn[sb], hybridOut[sb], sb, ch,
- &(pIII_side_info->ch[ch].gr[gr]));
- }
- // change these limits to ss=1; ss<18; ss+=2
- // sb=1; sb<SBLIMIT; sb+=2
- // no if required
- for (ss=0;ss<18;ss++) // Frequency inversion for polyphase.
- for (sb=0; sb<SBLIMIT; sb++)
- if ((ss%2) && (sb%2))
- hybridOut[sb][ss] = -hybridOut[sb][ss];
-
- if (ch == 0)
- for (ss=0;ss<18;ss++) { // Polyphase synthesis
- for (sb=0; sb<SBLIMIT; sb++)
- filter1->input_sample(hybridOut[sb][ss], sb);
- filter1->calculate_pcm_samples(buffer);
- }
- else
- for (ss=0;ss<18;ss++) { // Polyphase synthesis
- for (sb=0; sb<SBLIMIT; sb++)
- filter2->input_sample(hybridOut[sb][ss], sb);
- filter2->calculate_pcm_samples(buffer);
- }
- } // channels
- } // granule
-
- buffer->write_buffer (1); // write to stdout
- }
-
-
-