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

📄 ntt_scale_coder.c

📁 C写的MPEG4音频源代码(G.723/G.729)
💻 C
📖 第 1 页 / 共 3 页
字号:
    current_block = ntt_BLOCK_SHORT;    fw_nbit = 0;    break; default:        fprintf( stderr,"Fatal error! %d: no such window type.\n", index->w_type );        exit(1);}  /* LPC spectrum */  n_pr = ntt_N_PR_SCL;  lsp_code   = index->nttDataScl->lsp_code_scl;  lsp_fgcode = index->nttDataScl->lsp_fgcode_scl;  lsp_csize[0] = ntt_NC0_SCL;  lsp_csize[1] = ntt_NC1_SCL;  lsp_cdim[0]= ntt_N_PR_SCL;  lsp_cdim[1]= ntt_N_PR_SCL;  block_size = nfr*nsf ; /* tsushima add */  p_w_type[iscl] = index->nttDataScl->prev_w_type;  /*--- Encoding tools ---*/  /* LPC spectrum encoding */ {     for (i_ch=0;i_ch<n_ch;i_ch++){       band_l[i_ch] = 	 index->nttDataScl->ac_btm[iscl][i_ch][mat_shift[iscl][i_ch]];       band_u[i_ch] = 	 index->nttDataScl->ac_top[iscl][i_ch][mat_shift[iscl][i_ch]];     }    ntt_scale_enc_lpc_spectrum(nfr, nsf, index->block_size_samples,			     n_ch, n_pr, lsp_code, lsp_fgcode,			     index->nttDataScl->prev_lsp_code[iscl],			     index->nttDataScl->ma_np,                             spectrum, index->w_type, current_block,                             index->lsp, lpc_spectrum, iscl,                             band_l, band_u, cos_TT);  }  /* pre process (bandwidth setting) */  for (i_ch=0;i_ch<n_ch;i_ch++){ /* corrected by TSUSHIMA */    top=i_ch*index->block_size_samples;    ntt_freq_domain_pre_process(nfr, nsf, band_l[i_ch], band_u[i_ch],				spectrum+top, lpc_spectrum_org+top,				spectrum+top, lpc_spectrum_mod+top); /* correct*/    ntt_freq_domain_pre_process(nfr, nsf, band_l[i_ch], band_u[i_ch],				spectrum+top, lpc_spectrum+top,				spectrum+top, lpc_spectrum+top); /*correct*/  }  ntt_zerod(index->block_size_samples*index->numChannel, pitch_sequence);  /* Bark-scale envelope encoding */  if(fw_nbit == 0){       for(ifr=0; ifr<nfr*nsf*n_ch; ifr++){	    bark_env[ifr]=1.0;       }       for (i_ch=0; i_ch<n_ch; i_ch++){	    top = i_ch * block_size;	    for(isf=0; isf<nsf; isf++){		 subtop = top + isf*nfr;		 /* make input data */		 for (ismp=0; ismp<nfr; ismp++){		      tc[ismp+subtop] =			   spectrum[ismp+subtop] * lpc_spectrum[ismp+subtop]				- pitch_sequence[ismp+subtop];		 }	    }       }  }else{     ntt_scale_enc_bark_env(nfr, nsf, n_ch, n_crb, bark_tbl,		      fw_ndiv, fw_cb_size, fw_length,		      fw_codev, fw_len_max, fw_prcpt,		      index->nttDataScl->prev_fw_code[iscl],		      &(p_w_type[iscl]),		      spectrum, lpc_spectrum, pitch_sequence, current_block,		      tc, pwt, index->fw, index->fw_alf, bark_env, band_l);     if(index->w_type == EIGHT_SHORT_SEQUENCE)	    index->nttDataScl->prev_w_type = ntt_BLOCK_SHORT;     else   index->nttDataScl->prev_w_type = ntt_BLOCK_LONG;  }  /* gain encoding */  ntt_scale_enc_gain_t(nfr, nsf, n_ch, iscl, bark_env, tc, index->pow, gain);}void ntt_scale_enc_bark_env(/* Parameters */                      int    nfr,                      int    nsf,                      int    n_ch,                      int    n_crb,                      int    *bark_tbl,                      int    ndiv,                      int    cb_size,                      int    length,                      double *codev,                      int    len_max,                      double prcpt,                      int    prev_fw_code[],		      /*		      double *env_memory,                      double *pdot,                      */		      int    *p_w_type,                      /* Input */                      double spectrum[],                      double lpc_spectrum[],                      double pitch_sequence[],                      int    current_block,                      /* Output */                      double tc[ntt_T_FR_MAX],                      double pwt[ntt_T_FR_MAX],                      int    index_fw[],                      int    ind_fw_alf[],                      double bark_env[],                      double band_lower[]){  /*--- Variables ---*/  int    ismp, i_ch, top, isf, subtop, pred_sw, block_size;  /*--- Initialization ---*/  block_size = nfr * nsf;  /*--- Encoding process ---*/  for (i_ch=0; i_ch<n_ch; i_ch++){    top = i_ch * block_size;    /* make weight */    for (ismp=0; ismp<block_size; ismp++){      /*pwt[ismp+top] = 1./lpc_spectrum[ismp+top];*/      pwt[ismp+top] = 1. / pow(lpc_spectrum[ismp+top], 0.5);  }    /* set MA prediction switch */    if (*p_w_type == current_block) pred_sw = 1;    else                           pred_sw = 0;    for(isf=0; isf<nsf; isf++){      subtop = top + isf*nfr;      /* make input data */      for (ismp=0; ismp<nfr; ismp++){        tc[ismp+subtop] =          spectrum[ismp+subtop] * lpc_spectrum[ismp+subtop]            - pitch_sequence[ismp+subtop];    }   /* envelope coding */      ntt_scale_fwpred(nfr, n_crb, bark_tbl, ndiv,                 cb_size, length, codev, len_max, prcpt,                 prev_fw_code+(i_ch*nsf+isf)*ndiv,		 /*env_memory, pdot, */                 pwt+subtop, bark_env+subtop, tc+subtop,                 &index_fw[(i_ch*nsf+isf)*ndiv],                 &ind_fw_alf[isf+i_ch*nsf], i_ch, pred_sw, band_lower[i_ch]);      pred_sw = 1;  }}  *p_w_type = current_block;}void ntt_scale_fwpred(int    nfr,  /* Param:  block size */                int    n_crb,       /* Param:  number of Bark-scale band */                int    *bark_tbl, /* Param:  Bark-scale table */                int    ndiv,        /* Param:  number of interleave division */                int    cb_size,     /* Param:  codebook size */                int    length,      /* Param:  codevector length */                double *codev,      /* Param:  codebook */                int    len_max,     /* Param:  codevector memory length */                double prcpt,                int    prev_fw_code[],		/*double *penv_tmp,*//*Param: memory for Bark-scale envelope*/                /*double *pdot_tmp,*//*Param: memory for env. calculation */                double iwt[],       /* Input:  LPC envelope */                double pred[],      /* In/Out: Prediction factor */                double tc[],        /* Input:  Residual signal */                int    index_fw[],  /* Output: Envelope VQ indices */                int    *ind_fw_alf, /* Output: AR prediction coefficient index */                int    i_sup,       /* Input:  Channel number */                int    pred_sw,     /* Input:  prediction switch */                double band_lower){    /*--- Variables ---*/    double        bwt[ntt_N_CRB_MAX], env[ntt_N_CRB_MAX];    double        fwgain, alf, alfq;    double        penv[ntt_N_CRB_MAX];    register double acc, cdot, acc2, col0, col1, dtmp;    int           top, top2, ismp, iblow, ibhigh, ib;    int   n_crb_eff;    float ftmp;    /*--- Initialization ---*/    top  = i_sup*nfr;    top2 = i_sup*n_crb;    ntt_fwex( prev_fw_code, ndiv, length, codev, len_max, penv );    /*--- Calculate envelope ---*/    ftmp = (float)band_lower;    ftmp *= (float)nfr;    iblow= (int)ftmp; /*iblow=0;*/ fwgain=0.; ib=0;	  n_crb_eff=0;    for (ib=0; ib<n_crb; ib++){        ibhigh = bark_tbl[ib +i_sup*n_crb ];	/* reset the accumulations */        acc  = 0.0;        acc2 = 0.0;        /* bandle the input coefficient lines into the bark-scale band */        for(ismp=iblow; ismp<ibhigh; ismp++){            cdot =  tc[ismp]*tc[ismp];/* calc. power */            acc +=  cdot; 	    acc2 = acc2 + iwt[ismp];  /* accumulate the weighting factor */        }        env[ib] = sqrt(acc/(double)(ibhigh-iblow)) + 0.1; /* envelope in bark*/        bwt[ib] = acc2;         /* weighting factor in bark scale */        if(env[ib]> 0.1) {	  n_crb_eff++;	  fwgain += env[ib];      /* accumulate the envelope power */        }        iblow = ibhigh;    }/* gain to normalize the bark-scale envelope */    fwgain = (double) n_crb_eff / fwgain;    /*--- Normalize the envelope, and remove the offset ---*/    for (ib=0; ib<n_crb_eff; ib++){        env[ib] = env[ib]*fwgain - 1.0;    }    for (ib=n_crb_eff; ib<n_crb; ib++){        env[ib] = 0.0;    }    /*--- Calculate the AR prediction coefficient and quantize it ---*/    col0 = 0.1;    col1 = 0.;    if (pred_sw == 1){      /* calculate auto correlation and cross correlation */      for (ib=0; ib<n_crb; ib++){        col0 += penv[ib]*penv[ib];        col1 += penv[ib]*env[ib];    }      alf = col1/col0;   /* normalize the cross correlation coefficient */      ntt_fwalfenc( alf, &alfq, ind_fw_alf ); /* quantize the cor. coefficient */      /* AR prediction (cancellation using cor. coeff.) */      for ( ib=0; ib<n_crb; ib++ ){        env[ib] -= alfq*penv[ib];    }  }    else{ /* in stop frame */      alf = 0.;      ntt_fwalfenc( alf, &alfq, ind_fw_alf );  }    /*--- Quantization of the envelope ---*/    ntt_fwvq(ndiv, cb_size, length, codev, len_max, prcpt,             env, bwt, index_fw);    /*--- Local decoding ---*/    ntt_fwex( index_fw, ndiv, length, codev, len_max, env );    for(ib=ndiv*length; ib<n_crb; ib++) env[ib] = 0.;    for(ismp=0;ismp<nfr*band_lower;ismp++){      pred[ismp]=1.0;  }    for(ismp=bark_tbl[n_crb-1  +i_sup*n_crb];ismp<nfr;ismp++){      pred[ismp]=1.0;  }    for(ib=0, ismp=(int)ftmp; ib<n_crb; ib++){        ibhigh = bark_tbl[ib  +i_sup*n_crb ];        dtmp = ntt_max(env[ib]+alfq*penv[ib]+1., ntt_FW_LLIM);        while(ismp<ibhigh){            pred[ismp] = dtmp;            ismp++;        }    }    /*--- Store the current envelope ---    ntt_movdd(n_crb, env, penv); */}void ntt_scale_enc_gain_t(/* Parameters */                    int    nfr,                    int    nsf,                    int    n_ch,                    int    iscl,                    /* Input */                    double bark_env[],                    double tc[],                    /* Output */                    int    index_pow[],                    double gain[]){  int    ismp, top, subtop, isf, iptop, gtop, i_ch, block_size;  double pwr;  double sub_pwr_tmp, sub_pwr[ntt_N_SHRT_MAX];  double g_gain;  block_size = nfr * nsf;  /*--- Encoding process ---*/  for (i_ch=0; i_ch<n_ch; i_ch++){    top = i_ch * block_size;    /*--- Gain calculation ---*/    pwr = 0.1;    for(isf=0; isf<nsf; isf++){      subtop = top + isf*nfr;      for ( ismp=0; ismp<nfr; ismp++ ){        tc[ismp+subtop] /= bark_env[ismp+subtop];    }      sub_pwr_tmp        =(ntt_dotdd(nfr, tc+subtop, tc+subtop)+0.1)/(double)nfr;      pwr += sub_pwr_tmp;      sub_pwr[isf] =sqrt( sub_pwr_tmp );  }    pwr = sqrt(pwr/(double)nsf);    /*--- Quantization ---*/    if (isf == 1){      /* global gain */      ntt_enc_sq_gain(pwr, ntt_AMP_MAX_SCL, ntt_MU_SCL,                      ntt_STEP_SCL, ntt_AMP_NM,                      &index_pow[i_ch],&gain[i_ch]);  }    else{      /* global gain */      iptop = (nsf+1)*i_ch;      gtop  = nsf*i_ch;      ntt_enc_sq_gain(pwr, ntt_AMP_MAX_SCL, ntt_MU_SCL,                      ntt_STEP_SCL, ntt_AMP_NM,                      &index_pow[iptop],&g_gain);      /*      gain[gtop] = g_gain;      */      /* subband gain */      for(isf=0; isf<nsf; isf++){        subtop = top + isf*nfr;        ntt_enc_sq_gain(sub_pwr[isf]/(g_gain+0.0000001),                      ntt_SUB_AMP_MAX_SCL, ntt_MU_SCL,                      ntt_SUB_STEP_SCL, ntt_SUB_AMP_NM,                     &index_pow[iptop+isf+1],&gain[gtop+isf]);                gain[gtop+isf] *= g_gain;    }  }}}void ntt_enc_sq_gain(              double power,      /* Input  --- Power to be encoded */              double  amp_max,              double  mu,              double  step,              double  amp_nm,              int    *index_pow, /* Output --- Power code index */              double *gain)      /* Output --- Gain */{    /*--- Variables --*/    double mu_power, dmu_power, dec_power;    mu_power = ntt_mulaw(power, amp_max, mu);         /* mu-Law power */    *index_pow = (int)floor(mu_power/ step);               /* quantization */    dmu_power = *index_pow * step + step/2.;        /* decoded mu-Law power */    dec_power = ntt_mulawinv(dmu_power, amp_max, mu); /* decoded power */    *gain= dec_power / amp_nm;                        /* calculate gain */}/* T.Ishikawa 980813 */#define mat_ERR_WEI_0 0.5#define mat_ERR_WEI_1 1.2#define mat_ERR_WEI_2 2.0#define mat_ERR_WEI_3 2.5#define mat_PERCEPT_POW 0.45#define mat_SHORT_DELAY 16#define mat_SPEECH 20int mat_scale_lay_shift2(double spectrum[],			double spectrum_org[],			int iscl,			ntt_INDEX *index){	int ii,jj,top, i_ch;	double err_max,err[4];	static int mat_shift_max[6], mat_shift_min[6];	double tmp_spect[ntt_N_FR_MAX];	/* T.Ishikawa 980804 */	static int short_delay_flag,short_count,short_flag;	double lpc_spect_tmp[ntt_T_FR_MAX],bark_env_tmp[ntt_T_FR_MAX];	double lpc_spect_org[ntt_T_FR_MAX],bark_env_org[ntt_T_FR_MAX];	double pitch_sequence_tmp[ntt_T_FR_MAX];	double pitch_sequence_org[ntt_T_FR_MAX];	double gain_tmp[ntt_T_SHRT_MAX];	double gain_org[ntt_T_SHRT_MAX];        double acc;	   double mat_wei[4];	ntt_zerod(ntt_T_FR_MAX,pitch_sequence_tmp);	ntt_zerod(ntt_T_FR_MAX,pitch_sequence_org);	ntt_zerod(ntt_T_SHRT_MAX,gain_tmp);	ntt_zerod(ntt_T_SHRT_MAX,gain_org);	ntt_zerod(ntt_T_FR_MAX,lpc_spect_tmp);	ntt_zerod(ntt_T_FR_MAX,lpc_spect_org);	ntt_zerod(ntt_T_FR_MAX,bark_env_tmp);	ntt_zerod(ntt_T_FR_MAX,bark_env_org);	/* Warning!!! mat_shift_min[0] & mat_shift_max[0] ==> Enh-1 */	mat_shift_min[0]=0;	mat_shift_min[1]=1;	mat_shift_min[2]=1;	mat_shift_min[3]=0;	mat_shift_min[4]=0;	mat_shift_min[5]=0;	mat_shift_max[0]= 2 /*1 test  TM98506 */;	mat_shift_max[1]= 3 /*2 test          */;	mat_shift_max[2]= 3 /*2 test          */;	mat_shift_max[3]= 3;	mat_shift_max[4]= 3;	mat_shift_max[5]= 3;/*	fprintf(stderr,"Low-pass Ver. \n");*/	if((iscl==1)&&(index->w_type==EIGHT_SHORT_SEQUENCE))		{			short_flag=1;			short_delay_flag=1;			short_count=0;		}		if(iscl==1)	short_count++;	if(short_count < mat_SHORT_DELAY )		{			short_delay_flag=1;		}	else		{			short_delay_flag=0;

⌨️ 快捷键说明

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