📄 layer3.c
字号:
* short (count1table) values */ for(;l3 && (part2remain+num > 0);l3--) { struct newhuff *h = htc+gr_info->count1table_select; register short *val = h->table,a; REFRESH_MASK; while((a=*val++)<0) { if (mask < 0) val -= a; num--; mask <<= 1; } if(part2remain+num <= 0) { num -= part2remain+num; break; } for(i=0;i<4;i++) { if(!(i & 1)) { if(!mc) { mc = *m++; cb = *m++;#ifdef CUT_HF if(cb == 21) { fprintf(stderr,"c"); v = 0.0; } else#endif v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift]; } mc--; } if ( (a & (0x8>>i)) ) { max = cb; if(part2remain+num <= 0) { break; } if(mask < 0) *xrpnt++ = -v; else *xrpnt++ = v; num--; mask <<= 1; } else *xrpnt++ = 0.0; } } gr_info->maxbandl = max+1; gr_info->maxb = longLimit[sfreq][gr_info->maxbandl]; } part2remain += num;// backbits(num); bitindex -= num; wordpointer += (bitindex>>3); bitindex &= 0x7; num = 0; while(xrpnt < &xr[SBLIMIT][0]) *xrpnt++ = 0.0; while( part2remain > 16 ) { getbits(16); /* Dismiss stuffing Bits */ part2remain -= 16; } if(part2remain > 0) getbits(part2remain); else if(part2remain < 0) { fprintf(stderr,"mpg123: Can't rewind stream by %d bits!\n",-part2remain); return 1; /* -> error */ } return 0;}/* * III_stereo: calculate real channel values for Joint-I-Stereo-mode */static void III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT],int *scalefac, struct gr_info_s *gr_info,int sfreq,int ms_stereo,int lsf){ real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf; struct bandInfoStruct *bi = &bandInfo[sfreq]; const real *tab1,*tab2; int tab; static const real *tabs[3][2][2] = { { { tan1_1,tan2_1 } , { tan1_2,tan2_2 } }, { { pow1_1[0],pow2_1[0] } , { pow1_2[0],pow2_2[0] } } , { { pow1_1[1],pow2_1[1] } , { pow1_2[1],pow2_2[1] } } }; tab = lsf + (gr_info->scalefac_compress & lsf); tab1 = tabs[tab][ms_stereo][0]; tab2 = tabs[tab][ms_stereo][1];#if 0 if(lsf) { int p = gr_info->scalefac_compress & 0x1; if(ms_stereo) { tab1 = pow1_2[p]; tab2 = pow2_2[p]; } else { tab1 = pow1_1[p]; tab2 = pow2_1[p]; } } else { if(ms_stereo) { tab1 = tan1_2; tab2 = tan2_2; } else { tab1 = tan1_1; tab2 = tan2_1; } }#endif// printf("III_i_st: tab1=%p tab2=%p tab=%d ms=%d \n", tab1, tab2, tab, ms_stereo); if (gr_info->block_type == 2) { int lwin,do_l = 0; if( gr_info->mixed_block_flag ) do_l = 1; for (lwin=0;lwin<3;lwin++) { /* process each window */ /* get first band with zero values */ int is_p,sb,idx,sfb = gr_info->maxband[lwin]; /* sfb is minimal 3 for mixed mode */ if(sfb > 3) do_l = 0; for(;sfb<12;sfb++) { is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ if(is_p != 7) { real t1,t2; sb = bi->shortDiff[sfb]; idx = bi->shortIdx[sfb] + lwin; t1 = tab1[is_p]; t2 = tab2[is_p]; for (; sb > 0; sb--,idx+=3) { real v = xr[0][idx]; xr[0][idx] = REAL_MUL(v, t1); xr[1][idx] = REAL_MUL(v, t2); } } }#if 1/* in the original: copy 10 to 11 , here: copy 11 to 12 maybe still wrong??? (copy 12 to 13?) */ is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ sb = bi->shortDiff[12]; idx = bi->shortIdx[12] + lwin;#else is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ sb = bi->shortDiff[11]; idx = bi->shortIdx[11] + lwin;#endif if(is_p != 7) { real t1,t2; t1 = tab1[is_p]; t2 = tab2[is_p]; for ( ; sb > 0; sb--,idx+=3 ) { real v = xr[0][idx]; xr[0][idx] = REAL_MUL(v, t1); xr[1][idx] = REAL_MUL(v, t2); } } } /* end for(lwin; .. ; . ) *//* also check l-part, if ALL bands in the three windows are 'empty' * and mode = mixed_mode */ if (do_l) { int sfb = gr_info->maxbandl; int idx = bi->longIdx[sfb]; for ( ; sfb<8; sfb++ ) { int sb = bi->longDiff[sfb]; int is_p = scalefac[sfb]; /* scale: 0-15 */ if(is_p != 7) { real t1,t2; t1 = tab1[is_p]; t2 = tab2[is_p]; for ( ; sb > 0; sb--,idx++) { real v = xr[0][idx]; xr[0][idx] = REAL_MUL(v, t1); xr[1][idx] = REAL_MUL(v, t2); } } else idx += sb; } } } else { /* ((gr_info->block_type != 2)) */ int sfb = gr_info->maxbandl; int is_p,idx = bi->longIdx[sfb];/* hmm ... maybe the maxbandl stuff for i-stereo is buggy? */ if(sfb <= 21) { for ( ; sfb<21; sfb++) { int sb = bi->longDiff[sfb]; is_p = scalefac[sfb]; /* scale: 0-15 */ if(is_p != 7) { real t1,t2; t1 = tab1[is_p]; t2 = tab2[is_p]; for ( ; sb > 0; sb--,idx++) { real v = xr[0][idx]; xr[0][idx] = REAL_MUL(v, t1); xr[1][idx] = REAL_MUL(v, t2); } } else idx += sb; } is_p = scalefac[20]; if(is_p != 7) { /* copy l-band 20 to l-band 21 */ int sb; real t1 = tab1[is_p],t2 = tab2[is_p]; for ( sb = bi->longDiff[21]; sb > 0; sb--,idx++ ) { real v = xr[0][idx]; xr[0][idx] = REAL_MUL(v, t1); xr[1][idx] = REAL_MUL(v, t2); } } } /* end: if(sfb <= 21) */ } /* ... */}static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info) { int sblim; if(gr_info->block_type == 2) { if(!gr_info->mixed_block_flag) return; sblim = 1; } else { sblim = gr_info->maxb-1; } /* 31 alias-reduction operations between each pair of sub-bands */ /* with 8 butterflies between each pair */ { int sb; real *xr1=(real *) xr[1]; for(sb=sblim;sb;sb--,xr1+=10) { int ss; real *cs=aa_cs,*ca=aa_ca; real *xr2 = xr1; for(ss=7;ss>=0;ss--) { /* upper and lower butterfly inputs */ register real bu = *--xr2,bd = *xr1; *xr2 = (bu * (*cs) ) - (bd * (*ca) ); *xr1++ = (bd * (*cs++) ) + (bu * (*ca++) ); } } }}#include "dct64.c"#include "dct36.c"#include "dct12.c"#include "decod386.c"/* * III_hybrid */ static dct36_func_t dct36_func; static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT],real tsOut[SSLIMIT][SBLIMIT], int ch,struct gr_info_s *gr_info){ real *tspnt = (real *) tsOut; static real block[2][2][SBLIMIT*SSLIMIT] = { { { 0, } } }; static int blc[2]={0,0}; real *rawout1,*rawout2; int bt; int sb = 0; { int b = blc[ch]; rawout1=block[b][ch]; b=-b+1; rawout2=block[b][ch]; blc[ch] = b; } if(gr_info->mixed_block_flag) { sb = 2; (*dct36_func)(fsIn[0],rawout1,rawout2,win[0],tspnt); (*dct36_func)(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1); rawout1 += 36; rawout2 += 36; tspnt += 2; } bt = gr_info->block_type; if(bt == 2) { for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) { dct12(fsIn[sb],rawout1,rawout2,win[2],tspnt); dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1); } } else { for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) { (*dct36_func)(fsIn[sb],rawout1,rawout2,win[bt],tspnt); (*dct36_func)(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1); } } for(;sb<SBLIMIT;sb++,tspnt++) { int i; for(i=0;i<SSLIMIT;i++) { tspnt[i*SBLIMIT] = *rawout1++; *rawout2++ = 0.0; } }}/* * main layer3 handler *//* int do_layer3(struct frame *fr,int outmode,struct audio_info_struct *ai) */static int do_layer3(struct frame *fr,int single){ int gr, ch, ss,clip=0; int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */ struct III_sideinfo sideinfo; int stereo = fr->stereo; int ms_stereo,i_stereo; int sfreq = fr->sampling_frequency; int stereo1,granules;// if (fr->error_protection) getbits(16); /* skip crc */ if(stereo == 1) { /* stream is mono */ stereo1 = 1; single = 0; } else if(single >= 0) /* stream is stereo, but force to mono */ stereo1 = 1; else stereo1 = 2; if(fr->mode == MPG_MD_JOINT_STEREO) { ms_stereo = (fr->mode_ext & 0x2)>>1; i_stereo = fr->mode_ext & 0x1; } else ms_stereo = i_stereo = 0; if(!III_get_side_info(&sideinfo,stereo,ms_stereo,sfreq,single,fr->lsf)) return -1; set_pointer(sideinfo.main_data_begin); granules = (fr->lsf) ? 1 : 2; for (gr=0;gr<granules;gr++){ static real hybridIn[2][SBLIMIT][SSLIMIT]; static real hybridOut[2][SSLIMIT][SBLIMIT]; { struct gr_info_s *gr_info = &(sideinfo.ch[0].gr[gr]); long part2bits; if(fr->lsf) part2bits = III_get_scale_factors_2(scalefacs[0],gr_info,0); else part2bits = III_get_scale_factors_1(scalefacs[0],gr_info); if(III_dequantize_sample(hybridIn[0], scalefacs[0],gr_info,sfreq,part2bits)) return clip; } if(stereo == 2) { struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]); long part2bits; if(fr->lsf) part2bits = III_get_scale_factors_2(scalefacs[1],gr_info,i_stereo); else part2bits = III_get_scale_factors_1(scalefacs[1],gr_info); if(III_dequantize_sample(hybridIn[1],scalefacs[1],gr_info,sfreq,part2bits)) return clip; if(ms_stereo) { int i; int maxb = sideinfo.ch[0].gr[gr].maxb; if(sideinfo.ch[1].gr[gr].maxb > maxb) maxb = sideinfo.ch[1].gr[gr].maxb; for(i=0;i<SSLIMIT*maxb;i++) { real tmp0 = ((real *)hybridIn[0])[i]; real tmp1 = ((real *)hybridIn[1])[i]; ((real *)hybridIn[0])[i] = tmp0 + tmp1; ((real *)hybridIn[1])[i] = tmp0 - tmp1; } } if(i_stereo) III_i_stereo(hybridIn,scalefacs[1],gr_info,sfreq,ms_stereo,fr->lsf); if(ms_stereo || i_stereo || (single == 3) ) { if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb) sideinfo.ch[0].gr[gr].maxb = gr_info->maxb; else gr_info->maxb = sideinfo.ch[0].gr[gr].maxb; } switch(single) { case 3: { register int i; register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1]; for(i=0;i<SSLIMIT*gr_info->maxb;i++,in0++) *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */ break; } case 1: { register int i; register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1]; for(i=0;i<SSLIMIT*gr_info->maxb;i++) *in0++ = *in1++; break; } } } // if(stereo == 2) for(ch=0;ch<stereo1;ch++) { struct gr_info_s *gr_info = &(sideinfo.ch[ch].gr[gr]); III_antialias(hybridIn[ch],gr_info); III_hybrid(hybridIn[ch], hybridOut[ch], ch,gr_info); } for(ss=0;ss<SSLIMIT;ss++) { if(single >= 0) { clip += (fr->synth_mono)(hybridOut[0][ss],pcm_sample,&pcm_point); } else { int p1 = pcm_point; clip += (fr->synth)(hybridOut[0][ss],0,pcm_sample,&p1); clip += (fr->synth)(hybridOut[1][ss],1,pcm_sample,&pcm_point); } } } return clip;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -