⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 decode.c

📁 mpeg layerI II III, support window and linux
💻 C
📖 第 1 页 / 共 5 页
字号:
    }}/*************************** Layer II stuff ************************/void II_buffer_sample(bs,sample,bit_alloc,fr_ps)unsigned int FAR sample[2][3][SBLIMIT];unsigned int bit_alloc[2][SBLIMIT];Bit_stream_struc *bs;frame_params *fr_ps;{    int i,j,k,m;    int stereo = fr_ps->stereo;    int sblimit = fr_ps->sblimit;    int jsbound = fr_ps->jsbound;    al_table *alloc = fr_ps->alloc;    printf("debug : layer II buffer sample\n");    for (i=0;i<sblimit;i++) for (j=0;j<((i<jsbound)?stereo:1);j++) {        if (bit_alloc[j][i]) {            /* check for grouping in subband */            if ((*alloc)[i][bit_alloc[j][i]].group==3)                for (m=0;m<3;m++) {                    k = (*alloc)[i][bit_alloc[j][i]].bits;                    sample[j][m][i] = (unsigned int) getbits(bs,k);		    printf("bound=%d,ch=%d,direct sample,sample[%d]=0x%x,bitnum=%d\n",i,j,m,sample[j][m][i],k);                }            else {              /* bit_alloc = 3, 5, 9 */                unsigned int nlevels, c=0;                nlevels = (*alloc)[i][bit_alloc[j][i]].steps;                k=(*alloc)[i][bit_alloc[j][i]].bits;                c = (unsigned int) getbits(bs, k);		printf("bound=%d,ch=%d,coded sample,sample=0x%x\n",i,j,c);                for (k=0;k<3;k++) {                    sample[j][k][i] = c % nlevels;                    c /= nlevels;		    printf("bound=%d,ch=%d,deccode sample %d =0x%x\n",i,j,k,sample[j][k][i]);                }            }        }        else {                  /* for no sample transmitted */            for (k=0;k<3;k++) sample[j][k][i] = 0;	    printf("bound=%d,ch=%d, sample 0_1_2=0\n",i,j);        }        if(stereo == 2 && i>= jsbound) /* joint stereo : copy L to R */ {            for (k=0;k<3;k++) sample[1][k][i] = sample[0][k][i];	    printf("copy L to R, bound=%d,sample=0x%x\n",i,sample[0][k][i]);	}	    }    for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++) {	for (k=0;k<3;k++)        		 sample[j][k][i] = 0;	 printf("bound=%d,ch=%d, sample 0_1_2=0\n",i,j);    }}/**************************************************************/*/*   Restore the compressed sample to a factional number./*   first complement the MSB of the sample/*    for layer I :/*    Use s = (s' + 2^(-nb+1) ) * 2^nb / (2^nb-1)/*   for Layer II :/*   Use the formula s = s' * c + d/*/**************************************************************/static double c[17] = { 1.33333333333, 1.60000000000, 1.14285714286,                        1.77777777777, 1.06666666666, 1.03225806452,                        1.01587301587, 1.00787401575, 1.00392156863,                        1.00195694716, 1.00097751711, 1.00048851979,                        1.00024420024, 1.00012208522, 1.00006103888,                        1.00003051851, 1.00001525902 };static double d[17] = { 0.500000000, 0.500000000, 0.250000000, 0.500000000,                        0.125000000, 0.062500000, 0.031250000, 0.015625000,                        0.007812500, 0.003906250, 0.001953125, 0.0009765625,                        0.00048828125, 0.00024414063, 0.00012207031,                        0.00006103516, 0.00003051758 };/************************** Layer II stuff ************************/void II_dequantize_sample(sample, bit_alloc, fraction, fr_ps)unsigned int FAR sample[2][3][SBLIMIT];unsigned int bit_alloc[2][SBLIMIT];double FAR fraction[2][3][SBLIMIT];frame_params *fr_ps;{    int i, j, k, x;    int stereo = fr_ps->stereo;    int sblimit = fr_ps->sblimit;    al_table *alloc = fr_ps->alloc;    printf("II dequantize\n");    for (i=0;i<sblimit;i++)  for (j=0;j<3;j++) for (k=0;k<stereo;k++) {        if (bit_alloc[k][i]) {            /* locate MSB in the sample */            x = 0;#ifndef MS_DOS            while ((1L<<x) < (*alloc)[i][bit_alloc[k][i]].steps) x++;#else            /* microsoft C thinks an int is a short */            while (( (unsigned long) (1L<<(long)x) <                    (unsigned long)( (*alloc)[i][bit_alloc[k][i]].steps)                    ) && ( x < 16) ) x++;#endif            /* MSB inversion */            if (((sample[k][j][i] >> x-1) & 1) == 1)                fraction[k][j][i] = 0.0;            else  fraction[k][j][i] = -1.0;            /* Form a 2's complement sample */            fraction[k][j][i] += (double) (sample[k][j][i] & ((1<<x-1)-1)) /                (double) (1L<<x-1);            /* Dequantize the sample */            fraction[k][j][i] += d[(*alloc)[i][bit_alloc[k][i]].quant];            fraction[k][j][i] *= c[(*alloc)[i][bit_alloc[k][i]].quant];        }        else fraction[k][j][i] = 0.0;	printf("sb=%d, ch=%d, grp = %d,sample =%d,x=%d,c=%f,d=%f,fract = %f\n",i,k,j,sample[k][j][i],x,c[(*alloc)[i][bit_alloc[k][i]].quant],d[(*alloc)[i][bit_alloc[k][i]].quant],fraction[k][j][i]);    }    for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<3;j++) for(k=0;k<stereo;k++)        fraction[k][j][i] = 0.0;}/***************************** Layer I stuff ***********************/void I_dequantize_sample(sample, fraction, bit_alloc, fr_ps)unsigned int FAR sample[2][3][SBLIMIT];unsigned int bit_alloc[2][SBLIMIT];double FAR fraction[2][3][SBLIMIT];frame_params *fr_ps;{    int i, nb, k;    int stereo = fr_ps->stereo;    int sblimit = fr_ps->sblimit;    printf("L1 deauantize\n");    for (i=0;i<SBLIMIT;i++)        for (k=0;k<stereo;k++) {            if (bit_alloc[k][i]) {                nb = bit_alloc[k][i] + 1;                if (((sample[k][0][i] >> nb-1) & 1) == 1) fraction[k][0][i] = 0.0;                else fraction[k][0][i] = -1.0;                fraction[k][0][i] += (double) (sample[k][0][i] & ((1<<nb-1)-1)) /                    (double) (1L<<nb-1);                fraction[k][0][i] =                    (double) (fraction[k][0][i]+1.0/(double)(1L<<nb-1)) *                        (double) (1L<<nb) / (double) ((1L<<nb)-1);            }            else fraction[k][0][i] = 0.0;            printf("bound = %d, channel = %d, fraction = %lf\n",i,k,fraction[k][0][i]);        }}/************************************************************/*/*   Restore the original value of the sample ie multiply/*    the fraction value by its scalefactor./*/************************************************************//************************* Layer II Stuff **********************/void II_denormalize_sample(fraction, scale_index,fr_ps,x)double FAR fraction[2][3][SBLIMIT];unsigned int scale_index[2][3][SBLIMIT];frame_params *fr_ps;int x;{    int i,j,k;    int stereo = fr_ps->stereo;    int sblimit = fr_ps->sblimit;    printf("II_denormalization\n");    for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) {	printf("f0=%f, f1=%f, f2=%f, m=%f\n",fraction[j][0][i],fraction[j][1][i],fraction[j][2][i],multiple[scale_index[j][x][i]]);	fraction[j][0][i] *= multiple[scale_index[j][x][i]];        fraction[j][1][i] *= multiple[scale_index[j][x][i]];        fraction[j][2][i] *= multiple[scale_index[j][x][i]];	printf("mf0=%f, mf1=%f, mf2=%f\n",fraction[j][0][i],fraction[j][1][i],fraction[j][2][i]);    }}/**************************** Layer I stuff ******************************/void I_denormalize_sample(fraction,scale_index,fr_ps)double FAR fraction[2][3][SBLIMIT];unsigned int scale_index[2][3][SBLIMIT];frame_params *fr_ps;{    int i,j,k;    int stereo = fr_ps->stereo;    int sblimit = fr_ps->sblimit;    printf("L1 denormalize\n");    for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++) {        fraction[j][0][i] *= multiple[scale_index[j][0][i]];        printf("bound = %d, channel = %d, fraction = %lf, multi = %lf\n",i,j, fraction[j][0][i], multiple[scale_index[j][0][i]]);    }}/*****************************************************************/*/* The following are the subband synthesis routines. They apply/* to both layer I and layer II stereo or mono. The user has to/* decide what parameters are to be passed to the routines./*/***************************************************************//*************************************************************/*/*   Pass the subband sample through the synthesis window/*/**************************************************************//* create in synthesis filter */void create_syn_filter(filter,fp)double FAR filter[64][SBLIMIT];FILE *fp;{    register int i,k;    for (i=0; i<64; i++)        for (k=0; k<32; k++) {            if ((filter[i][k] = 1e9*cos((double)((PI64*i+PI4)*(2*k+1)))) >= 0)                modf(filter[i][k]+0.5, &filter[i][k]);            else                modf(filter[i][k]-0.5, &filter[i][k]);            filter[i][k] *= 1e-9;                    }}/***************************************************************/*/*   Window the restored sample/*/***************************************************************//* read in synthesis window */void read_syn_window(window)double FAR window[HAN_SIZE];{    int i,j[4];    FILE *fp;    double f[4];    char t[150];    if (!(fp = OpenTableFile("dewindow") )) {        printf("Please check synthesis window table 'dewindow'\n");        exit(1);    }    for (i=0;i<512;i+=4) {        fgets(t, 150, fp);        sscanf(t,"D[%d] = %lf D[%d] = %lf D[%d] = %lf D[%d] = %lf\n",               j, f,j+1,f+1,j+2,f+2,j+3,f+3);        if (i==j[0]) {            window[i] = f[0];            window[i+1] = f[1];            window[i+2] = f[2];            window[i+3] = f[3];        }        else {            printf("Check index in synthesis window table\n");            exit(1);        }        fgets(t,150,fp);    }    fclose(fp);    }int SubBandSynthesis (bandPtr, channel, samples,fp)double *bandPtr;int channel;OUTPUT_TYPE *samples;FILE *fp;{    register int i,j,k;    register double *bufOffsetPtr, sum;    register long foo;    static int init = 1;    typedef double NN[64][32];    static NN FAR *filter;    typedef double BB[2][2*HAN_SIZE];    static BB FAR *buf;    static int bufOffset[2] = {64,64};    static double FAR *window;    int clip = 0;               /* count & return how many samples clipped */    //printf("SubBandSynthesis, channel = %d\n",channel);    if (init) {        buf = (BB FAR *) mem_alloc(sizeof(BB),"BB");        filter = (NN FAR *) mem_alloc(sizeof(NN), "NN");        create_syn_filter(*filter,fp);        window = (double FAR *) mem_alloc(sizeof(double) * HAN_SIZE, "WIN");        read_syn_window(window);        init = 0;    }/*    if (channel == 0) */    bufOffset[channel] = (bufOffset[channel] - 64) & 0x3ff;    bufOffsetPtr = &((*buf)[channel][bufOffset[channel]]);        //printf("Han_aize = %d, bufoffset  = 0x%x\n",HAN_SIZE,bufOffset[channel]);    //for(i=0;i<32;i++) 	      //printf("banddata[%d]=%f\n",i,bandPtr[i]);    for (i=0; i<64; i++) {        sum = 0;        for (k=0; k<32; k++) {            sum += bandPtr[k] * (*filter)[i][k];         }        bufOffsetPtr[i] = sum;	//printf("V[%d] = %f\n",i,sum);            }    /*  S(i,j) = D(j+32i) * U(j+32i+((i+1)>>1)*64)  */    /*  samples(i,j) = MWindow(j+32i) * bufPtr(j+32i+((i+1)>>1)*64)  */    for (j=0; j<32; j++) {        sum = 0;        for (i=0; i<16; i++) {            k = j + (i<<5);            sum += window[k] * (*buf) [channel] [( (k + ( ((i+1)>>1) <<6) ) +                                                  bufOffset[channel]) & 0x3ff];        }/*      Casting truncates towards zero for both positive and negative numbers,        the result is cross-over distortion,  1995-07-12 shn */        if(sum > 0)        {          foo = (long)(sum * (double) SCALE + (double)0.5);        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -