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

📄 decode.c

📁 mepg 1 layer3软解码源代码,实现了对MP3文件的软件解码
💻 C
📖 第 1 页 / 共 4 页
字号:
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;    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;          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;    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;}/************************************************************/*/*   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;    for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) {        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]];    }}/**************************** 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;    for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;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)double FAR filter[64][SBLIMIT];{    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)double *bandPtr;int channel;short *samples;{    register int i,j,k;    register double *bufOffsetPtr, sum;    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 */    if (init) {        buf = (BB FAR *) mem_alloc(sizeof(BB),"BB");        filter = (NN FAR *) mem_alloc(sizeof(NN), "NN");        create_syn_filter(*filter);        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]]);    for (i=0; i<64; i++) {        sum = 0;        for (k=0; k<32; k++)            sum += bandPtr[k] * (*filter)[i][k];        bufOffsetPtr[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];        }/*   {long foo = (sum > 0) ? sum * SCALE + 0.5 : sum * SCALE - 0.5; */     {long foo = sum * SCALE;     if (foo >= (long) SCALE)      {samples[j] = SCALE-1; ++clip;}     else if (foo < (long) -SCALE) {samples[j] = -SCALE;  ++clip;}     else                           samples[j] = foo; }    }    return(clip);}void out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames)short FAR pcm_sample[2][SSLIMIT][SBLIMIT];int num;frame_params *fr_ps;int done;FILE *outFile;unsigned long *psampFrames;{    int i,j,l;    int stereo = fr_ps->stereo;    int sblimit = fr_ps->sblimit;    static short int outsamp[1600];    static long k = 0;    if (!done)        for (i=0;i<num;i++) for (j=0;j<SBLIMIT;j++) {            (*psampFrames)++;            for (l=0;l<stereo;l++) {                if (!(k%1600) && k) {                    fwrite(outsamp,2,1600,outFile);                    k = 0;                }                outsamp[k++] = pcm_sample[l][i][j];            }        }    else {        fwrite(outsamp,2,(int)k,outFile);        k = 0;    }}void  buffer_CRC(bs, old_crc)Bit_stream_struc  *bs;unsigned int  *old_crc;{    *old_crc = getbits(bs, 16);}void  recover_CRC_error(pcm_sample, error_count, fr_ps, outFile, psampFrames)short FAR pcm_sample[2][SSLIMIT][SBLIMIT];int error_count;frame_params *fr_ps;FILE *outFile;unsigned long *psampFrames;{    int  stereo = fr_ps->stereo;    int  num, done, i;    int  samplesPerFrame, samplesPerSlot;    layer  *hdr = fr_ps->header;    long  offset;    short  *temp;    num = 3;    if (hdr->lay == 1) num = 1;    samplesPerSlot = SBLIMIT * num * stereo;    samplesPerFrame = samplesPerSlot * 32;    if (error_count == 1) {     /* replicate previous error_free frame */        done = 1;        /* flush out fifo */        out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);        /* go back to the beginning of the previous frame */        offset = sizeof(short int) * samplesPerFrame;        fseek(outFile, -offset, SEEK_CUR);        done = 0;        for (i = 0; i < SCALE_BLOCK; i++) {            fread(pcm_sample, 2, samplesPerSlot, outFile);            out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);        }    }    else{                       /* mute the frame */        temp = (short*) pcm_sample;        done = 0;        for (i = 0; i < 2*3*SBLIMIT; i++)            *temp++ = MUTE;     /* MUTE value is in decoder.h */        for (i = 0; i < SCALE_BLOCK; i++)            out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);    }}/************************* Layer III routines **********************/void III_get_side_info(bs, si, fr_ps)Bit_stream_struc *bs;III_side_info_t *si;frame_params *fr_ps;{   int ch, gr, i;   int stereo = fr_ps->stereo;      si->main_data_begin = getbits(bs, 9);   if (stereo == 1)      si->private_bits = getbits(bs,5);      else si->private_bits = getbits(bs,3);   for (ch=0; ch<stereo; ch++)      for (i=0; i<4; i++)      si->ch[ch].scfsi[i] = get1bit(bs);   for (gr=0; gr<2; gr++) {      for (ch=0; ch<stereo; ch++) {         si->ch[ch].gr[gr].part2_3_length = getbits(bs, 12);         si->ch[ch].gr[gr].big_values = getbits(bs, 9);         si->ch[ch].gr[gr].global_gain = getbits(bs, 8);         si->ch[ch].gr[gr].scalefac_compress = getbits(bs, 4);         si->ch[ch].gr[gr].window_switching_flag = get1bit(bs);         if (si->ch[ch].gr[gr].window_switching_flag) {            si->ch[ch].gr[gr].block_type = getbits(bs, 2);            si->ch[ch].gr[gr].mixed_block_flag = get1bit(bs);            for (i=0; i<2; i++)               si->ch[ch].gr[gr].table_select[i] = getbits(bs, 5);            for (i=0; i<3; i++)               si->ch[ch].gr[gr].subblock_gain[i] = getbits(bs, 3);                           /* Set region_count parameters since they are implicit in this case. */                        if (si->ch[ch].gr[gr].block_type == 0) {               printf("Side info bad: block_type == 0 in split block.\n");               exit(0);               }            else if (si->ch[ch].gr[gr].block_type == 2                     && si->ch[ch].gr[gr].mixed_block_flag == 0)               si->ch[ch].gr[gr].region0_count = 8; /* MI 9; */            else si->ch[ch].gr[gr].region0_count = 7; /* MI 8; */            si->ch[ch].gr[gr].region1_count = 20 -				      si->ch[ch].gr[gr].region0_count;            }         else {            for (i=0; i<3; i++)               si->ch[ch].gr[gr].table_select[i] = getbits(bs, 5);            si->ch[ch].gr[gr].region0_count = getbits(bs, 4);            si->ch[ch].gr[gr].region1_count = getbits(bs, 3);            si->ch[ch].gr[gr].block_type = 0;            }               si->ch[ch].gr[gr].preflag = get1bit(bs);         si->ch[ch].gr[gr].scalefac_scale = get1bit(bs);         si->ch[ch].gr[gr].count1table_select = get1bit(bs);         }      }}void III_put_side_info(bs, si, fr_ps)frame_params *fr_ps;Bit_stream_struc *bs;

⌨️ 快捷键说明

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