📄 encode.c
字号:
double FAR filter[SBLIMIT][64];{ register int i,k; for (i=0; i<32; i++) for (k=0; k<64; k++) { if ((filter[i][k] = 1e9*cos((double)((2*i+1)*(16-k)*PI64))) >= 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; }}/************************************************************************** filter_subband()** PURPOSE: Calculates the analysis filter bank coefficients** SEMANTICS:* The windowed samples #z# is filtered by the digital filter matrix #m#* to produce the subband samples #s#. This done by first selectively* picking out values from the windowed samples, and then multiplying* them by the filter matrix, producing 32 subband samples.*************************************************************************/ void filter_subband(z,s)double FAR z[HAN_SIZE], s[SBLIMIT];{ double y[64]; int i,j;static char init = 0; typedef double MM[SBLIMIT][64];static MM FAR *m;#ifdef MS_DOS long SIZE_OF_MM; SIZE_OF_MM = SBLIMIT*64; SIZE_OF_MM *= 8; if (!init) { m = (MM FAR *) mem_alloc(SIZE_OF_MM, "filter"); create_ana_filter(*m); init = 1; }#else if (!init) { m = (MM FAR *) mem_alloc(sizeof(MM), "filter"); create_ana_filter(*m); init = 1; }#endif for (i=0;i<64;i++) for (j=0, y[i] = 0;j<8;j++) y[i] += z[i+64*j]; for (i=0;i<SBLIMIT;i++) for (j=0, s[i]= 0;j<64;j++) s[i] += (*m)[i][j] * y[j];}/************************************************************************* encode_info()** PURPOSE: Puts the syncword and header information on the output* bitstream.*************************************************************************/ void encode_info(fr_ps,bs)frame_params *fr_ps;Bit_stream_struc *bs;{ layer *info = fr_ps->header; putbits(bs,0xfff,12); /* syncword 12 bits */ put1bit(bs,info->version); /* ID 1 bit */ putbits(bs,4-info->lay,2); /* layer 2 bits */ put1bit(bs,!info->error_protection); /* bit set => no err prot */ putbits(bs,info->bitrate_index,4); putbits(bs,info->sampling_frequency,2); put1bit(bs,info->padding); put1bit(bs,info->extension); /* private_bit */ putbits(bs,info->mode,2); putbits(bs,info->mode_ext,2); put1bit(bs,info->copyright); put1bit(bs,info->original); putbits(bs,info->emphasis,2);} /************************************************************************** mod()** PURPOSE: Returns the absolute value of its argument*************************************************************************/ double mod(a)double a;{ return (a > 0) ? a : -a;} /************************************************************************** I_combine_LR (Layer I)* II_combine_LR (Layer II)** PURPOSE:Combines left and right channels into a mono channel** SEMANTICS: The average of left and right subband samples is put into* #joint_sample#** Layer I and II differ in frame length and # subbands used*************************************************************************/ void I_combine_LR(sb_sample, joint_sample)double FAR sb_sample[2][3][SCALE_BLOCK][SBLIMIT];double FAR joint_sample[3][SCALE_BLOCK][SBLIMIT];{ /* make a filtered mono for joint stereo */ int sb, smp; for(sb = 0; sb<SBLIMIT; ++sb) for(smp = 0; smp<SCALE_BLOCK; ++smp) joint_sample[0][smp][sb] = .5 * (sb_sample[0][0][smp][sb] + sb_sample[1][0][smp][sb]);} void II_combine_LR(sb_sample, joint_sample, sblimit)double FAR sb_sample[2][3][SCALE_BLOCK][SBLIMIT];double FAR joint_sample[3][SCALE_BLOCK][SBLIMIT];int sblimit;{ /* make a filtered mono for joint stereo */ int sb, smp, sufr; for(sb = 0; sb<sblimit; ++sb) for(smp = 0; smp<SCALE_BLOCK; ++smp) for(sufr = 0; sufr<3; ++sufr) joint_sample[sufr][smp][sb] = .5 * (sb_sample[0][sufr][smp][sb] + sb_sample[1][sufr][smp][sb]);} /************************************************************************** I_scale_factor_calc (Layer I)* II_scale_factor_calc (Layer II)** PURPOSE:For each subband, calculate the scale factor for each set* of the 12 subband samples** SEMANTICS: Pick the scalefactor #multiple[]# just larger than the* absolute value of the peak subband sample of 12 samples,* and store the corresponding scalefactor index in #scalar#.** Layer II has three sets of 12-subband samples for a given* subband.*************************************************************************/ void I_scale_factor_calc(sb_sample,scalar,stereo)double FAR sb_sample[][3][SCALE_BLOCK][SBLIMIT];unsigned int scalar[][3][SBLIMIT];int stereo;{ int i,j, k; double s[SBLIMIT]; for (k=0;k<stereo;k++) { for (i=0;i<SBLIMIT;i++) for (j=1, s[i] = mod(sb_sample[k][0][0][i]);j<SCALE_BLOCK;j++) if (mod(sb_sample[k][0][j][i]) > s[i]) s[i] = mod(sb_sample[k][0][j][i]); for (i=0;i<SBLIMIT;i++) for (j=SCALE_RANGE-2,scalar[k][0][i]=0;j>=0;j--) /* $A 6/16/92 */ if (s[i] <= multiple[j]) { scalar[k][0][i] = j; break; } }}/******************************** Layer II ******************************/ void II_scale_factor_calc(sb_sample,scalar,stereo,sblimit)double FAR sb_sample[][3][SCALE_BLOCK][SBLIMIT];unsigned int scalar[][3][SBLIMIT];int stereo,sblimit;{ int i,j, k,t; double s[SBLIMIT]; for (k=0;k<stereo;k++) for (t=0;t<3;t++) { for (i=0;i<sblimit;i++) for (j=1, s[i] = mod(sb_sample[k][t][0][i]);j<SCALE_BLOCK;j++) if (mod(sb_sample[k][t][j][i]) > s[i]) s[i] = mod(sb_sample[k][t][j][i]); for (i=0;i<sblimit;i++) for (j=SCALE_RANGE-2,scalar[k][t][i]=0;j>=0;j--) /* $A 6/16/92 */ if (s[i] <= multiple[j]) { scalar[k][t][i] = j; break; } for (i=sblimit;i<SBLIMIT;i++) scalar[k][t][i] = SCALE_RANGE-1; }}/************************************************************************** pick_scale (Layer II)** PURPOSE:For each subband, puts the smallest scalefactor of the 3* associated with a frame into #max_sc#. This is used* used by Psychoacoustic Model I.* (I would recommend changin max_sc to min_sc)*************************************************************************/ void pick_scale(scalar, fr_ps, max_sc)unsigned int scalar[2][3][SBLIMIT];frame_params *fr_ps;double FAR max_sc[2][SBLIMIT];{ int i,j,k,max; int stereo = fr_ps->stereo; int sblimit = fr_ps->sblimit; for (k=0;k<stereo;k++) for (i=0;i<sblimit;max_sc[k][i] = multiple[max],i++) for (j=1, max = scalar[k][0][i];j<3;j++) if (max > scalar[k][j][i]) max = scalar[k][j][i]; for (i=sblimit;i<SBLIMIT;i++) max_sc[0][i] = max_sc[1][i] = 1E-20;}/************************************************************************** put_scale (Layer I)** PURPOSE:Sets #max_sc# to the scalefactor index in #scalar.* This is used by Psychoacoustic Model I*************************************************************************/ void put_scale(scalar, fr_ps, max_sc)unsigned int scalar[2][3][SBLIMIT];frame_params *fr_ps;double FAR max_sc[2][SBLIMIT];{ int i,j,k, max; int stereo = fr_ps->stereo; int sblimit = fr_ps->sblimit; for (k=0;k<stereo;k++) for (i=0;i<SBLIMIT;i++) max_sc[k][i] = multiple[scalar[k][0][i]];} /************************************************************************** II_transmission_pattern (Layer II only)** PURPOSE:For a given subband, determines whether to send 1, 2, or* all 3 of the scalefactors, and fills in the scalefactor* select information accordingly** SEMANTICS: The subbands and channels are classified based on how much* the scalefactors changes over its three values (corresponding* to the 3 sets of 12 samples per subband). The classification* will send 1 or 2 scalefactors instead of three if the scalefactors* do not change much. The scalefactor select information,* #scfsi#, is filled in accordingly.*************************************************************************/ void II_transmission_pattern(scalar, scfsi, fr_ps)unsigned int scalar[2][3][SBLIMIT];unsigned int scfsi[2][SBLIMIT];frame_params *fr_ps;{ int stereo = fr_ps->stereo; int sblimit = fr_ps->sblimit; int dscf[2]; int class[2],i,j,k;static int pattern[5][5] = {0x123, 0x122, 0x122, 0x133, 0x123, 0x113, 0x111, 0x111, 0x444, 0x113, 0x111, 0x111, 0x111, 0x333, 0x113, 0x222, 0x222, 0x222, 0x333, 0x123, 0x123, 0x122, 0x122, 0x133, 0x123}; for (k=0;k<stereo;k++) for (i=0;i<sblimit;i++) { dscf[0] = (scalar[k][0][i]-scalar[k][1][i]); dscf[1] = (scalar[k][1][i]-scalar[k][2][i]); for (j=0;j<2;j++) { if (dscf[j]<=-3) class[j] = 0; else if (dscf[j] > -3 && dscf[j] <0) class[j] = 1; else if (dscf[j] == 0) class[j] = 2; else if (dscf[j] > 0 && dscf[j] < 3) class[j] = 3; else class[j] = 4; } switch (pattern[class[0]][class[1]]) { case 0x123 : scfsi[k][i] = 0; break; case 0x122 : scfsi[k][i] = 3; scalar[k][2][i] = scalar[k][1][i]; break; case 0x133 : scfsi[k][i] = 3; scalar[k][1][i] = scalar[k][2][i]; break; case 0x113 : scfsi[k][i] = 1; scalar[k][1][i] = scalar[k][0][i]; break; case 0x111 : scfsi[k][i] = 2; scalar[k][1][i] = scalar[k][2][i] = scalar[k][0][i]; break; case 0x222 : scfsi[k][i] = 2; scalar[k][0][i] = scalar[k][2][i] = scalar[k][1][i]; break; case 0x333 : scfsi[k][i] = 2; scalar[k][0][i] = scalar[k][1][i] = scalar[k][2][i]; break; case 0x444 : scfsi[k][i] = 2; if (scalar[k][0][i] > scalar[k][2][i]) scalar[k][0][i] = scalar[k][2][i]; scalar[k][1][i] = scalar[k][2][i] = scalar[k][0][i]; } }} /************************************************************************** I_encode_scale (Layer I)* II_encode_scale (Layer II)** PURPOSE:The encoded scalar factor information is arranged and* queued into the output fifo to be transmitted.** For Layer II, the three scale factors associated with* a given subband and channel are transmitted in accordance* with the scfsi, which is transmitted first.*************************************************************************/ void I_encode_scale(scalar, bit_alloc, fr_ps, bs)unsigned int scalar[2][3][SBLIMIT];unsigned int bit_alloc[2][SBLIMIT];frame_params *fr_ps;Bit_stream_struc *bs;{ int stereo = fr_ps->stereo; int sblimit = fr_ps->sblimit; int i,j; for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++) if (bit_alloc[j][i]) putbits(bs,scalar[j][0][i],6);} /***************************** Layer II ********************************/ void II_encode_scale(bit_alloc, scfsi, scalar, fr_ps, bs)unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT];unsigned int scalar[2][3][SBLIMIT];frame_params *fr_ps;Bit_stream_struc *bs;{ int stereo = fr_ps->stereo; int sblimit = fr_ps->sblimit;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -