www.pudn.com > AVS_M_ver10.rar > cod_hf.c


/* 
*********************************************************************** 
* COPYRIGHT AND WARRANTY INFORMATION 
* 
* Copyright 2007  Audio Video Coding Standard, Part ¢ú 
* 
* This software module was developed by AVS Audio sub-group 
* 
* DISCLAIMER OF WARRANTY 
* 
* These software programs are available to the users without any 
* license fee or royalty on an "as is" basis. The AVS disclaims 
* any and all warranties, whether express, implied, or statutory, 
* including any implied warranties of merchantability or of fitness 
* for a particular purpose. In no event shall the contributors or  
* the AVS be liable for any incidental, punitive, or consequential 
* damages of any kind whatsoever arising from the use of this program. 
* 
* This disclaimer of warranty extends to the user of this program 
* and user's customers, employees, agents, transferees, successors, 
* and assigns. 
* 
* The AVS does not represent or warrant that the program furnished 
* hereunder are free of infringement of any third-party patents. 
* Commercial implementations of AVS, including shareware, may be 
* subject to royalty fees to patent holders. Information regarding 
* the AVS patent policy is available from the AVS Web site at 
* http://www.avs.org.cn 
* 
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE AVS PATENT POLICY. 
************************************************************************ 
*/ 
 
#include  
#include  
#include  
#include  
#include "../include/amr_plus.h" 
  /*----------- CODE AND SYNTHESIZE UPPER-BAND SIGNAL (synth_hf[])  ---------- 
    bitstream pointer = ptr 
  coding modes (coder type=ACELP,TCX and frame length=20,40,80 ms) for upper-band are copied from lower-band 
  HF coding mode depends on bit allocation to HFs (nbits_hf) : 
  nbits_hf = 4*16=64 
       the lower-band excitation is randomized, folded, and shaped first in time-domain 
       with subframe gains then in frequency domain with a LP filter (plus energy smoothing) 
  nbits_hf = 4*16+640=704 
       the lower-band is quantized by ACELP/TCX model, using the same mode as lower band. 
   */ 
 /*------------------------------------------------------* 
  * HF analysis, quantization & synthesis                * 
  *------------------------------------------------------*/ 
void init_coder_hf(Coder_StState *st) 
{ 
  int i; 
  /* Static vectors to zero */ 
  set_zero(st->mem_hf1, MHF); 
  set_zero(st->mem_hf2, MHF); 
  set_zero(st->mem_hf3, MHF); 
  set_zero(st->past_q_isf_hf, MHF); 
  st->old_gain = 1.0; 
  /* isp initialization */ 
  for (i=0; iispold_hf[i] = (float)cos(3.141592654*(float)(i+1)/(float)MHF); 
  st->ispold_hf[MHF-1] = 0.045f; 
  mvr2r(st->ispold_hf, st->ispold_q_hf, MHF); 
 return; 
} 
void coder_hf( 
  int mod[],         /* (i) : mode for each 20ms frame (mode[4]     */ 
  float AqLF[],      /* (i) : Q coeff in lower band (AdLF[16])      */  
  float speech[],    /* (i) : speech vector [-M..L_FRAME_PLUS]           */  
  float speech_hf[], /* (i) : HF speech vec [-MHF..L_FRAME_PLUS+L_NEXT]  */  
  float synth_hf[],  /* (o) : HF synthesis  [0..L_FRAME_PLUS]            */  
  float window[],    /* (i) : window for LPC analysis               */ 
  int param[],       /* (o) : parameters (NB_DIV*NPRM_HF_DIV)       */ 
  int fscale, 
  Coder_StState *st) /* i/o : coder memory state                    */ 
{ 
  /* LPC coefficients */ 
  float r[MHF+1];                 /* Autocorrelations of windowed speech  */ 
  float A[(NB_SUBFR+1)*(MHF+1)]; 
  float Aq[(NB_SUBFR+1)*(MHF+1)]; 
  float *p_A, *p_Aq, Ap[MHF+1]; 
  float ispnew[NB_DIV*MHF];         /* LSPs at 4nd subframe                 */ 
  float ispnew_q[MHF];              /* LSPs at 4nd subframe                 */ 
  float isfnew[MHF]; 
  float gain_hf[NB_SUBFR], gain4[4]; 
  float HF[L_SUBFR]; 
  float buf[MHF+L_SUBFR]; 
  /* Scalars */ 
  int   i, j, k, i_subfr, sf, nsf, index, mode, *prm; 
  float tmp, gain, ener; 
 /*---------------------------------------------------------------* 
  *  Perform HF LP analysis four times (every 20 ms)              * 
  *  - autocorrelation + lag windowing                            * 
  *  - Levinson-Durbin algorithm to find a[]                      * 
  *  - convert a[] to isp[]                                       * 
  *  - interpol isp[]                                             * 
  *---------------------------------------------------------------*/ 
  for (k=0; kispold_hf, MHF); 
    int_lpc_np1(st->ispold_hf, &ispnew[k*MHF], &A[k*4*(MHF+1)], 4, MHF); 
    mvr2r(&ispnew[k*MHF], st->ispold_hf, MHF); 
  } 
  p_A = A; 
  for (k=0; kpast_q_isf_hf, prm, st->mean_isf_hf, st->dico1_isf_hf); 
    prm += 2; 
    /* Convert isfs to the cosine domain */ 
    isf2isp(isfnew, ispnew_q, MHF); 
    /* interpol quantized isp for local synthesis */ 
    int_lpc_np1(st->ispold_q_hf, ispnew_q, Aq, nsf, MHF); 
    mvr2r(ispnew_q, st->ispold_q_hf, MHF); 
    /* gain factor to match amplitude at 6.4kHz */ 
    gain = match_gain_6k4(&AqLF[((k*4)+nsf)*(M+1)], &Aq[nsf*(MHF+1)]); 
    int_gain(st->old_gain, gain, gain_hf, nsf); 
    st->old_gain = gain; 
   /*------------------------------------------------------* 
    * find gain in weighted domain                         * 
    *------------------------------------------------------*/ 
    p_Aq = Aq; 
    for (sf=0; sfmem_hf1, buf, MHF); 
      E_UTIL_synthesisPlus(p_Aq, MHF, buf+MHF, buf+MHF, L_SUBFR, st->mem_hf1, 1); 
      E_LPC_a_weight(p_A, Ap, GAMMA_HF, MHF); 
      E_UTIL_residuPlus(p_A, MHF, buf+MHF, HF, L_SUBFR); 
      E_UTIL_synthesisPlus(Ap, MHF, HF, HF, L_SUBFR, st->mem_hf2, 1); 
      E_UTIL_residuPlus(p_A, MHF, &speech_hf[i_subfr], buf, L_SUBFR); 
      E_UTIL_synthesisPlus(Ap, MHF, buf, buf, L_SUBFR, st->mem_hf3, 1); 
      ener = 0.001f; 
      for (i=0; i>2]; 
        /* quantize gain at: -10.5, -7.5, -4.5, -1.5, +1.5, +4.5, +7.5, +10.5 dB */ 
        tmp = gain_hf[i] - gain; 
        index = (int)floor(((10.5 + tmp) / 3.0) + 0.5); 
        if (index > 7) { 
          index = 7; 
        } 
        if (index < 0) { 
          index = 0; 
        } 
        prm[i] = index; 
        gain_hf[i] = gain + (3.0f*((float)index)) - 10.5f; 
      } 
      prm += nsf; 
    }         
    else if (mode == 2)   /* TCX 40 */ 
    { 
      for (i=0; i<4; i++) 
      { 
        tmp = 0.0; 
        for (j=0; j<2; j++) { 
          tmp += gain_hf[(2*i)+j]; 
        } 
        gain4[i] = 0.5f*tmp; 
      } 
      q_gain_hf(gain4, gain4, prm); 
      prm++; 
      for (i=0; i>1]; 
        /* quantize gain at: -4.5, -1.5, +1.5, +4.5 dB */ 
        tmp = gain_hf[i] - gain; 
        index = (int)floor(((4.5 + tmp) / 3.0) + 0.5); 
        if (index > 3) { 
          index = 3; 
        } 
        if (index < 0) { 
          index = 0; 
        } 
        prm[i] = index; 
        gain_hf[i] = gain + (3.0f*((float)index)) - 4.5f; 
      } 
      prm += nsf; 
    }         
    else     /* ACELP/TCX 20 */ 
    { 
      q_gain_hf(gain_hf, gain_hf, prm); 
      prm++; 
    } 
    if (mode == 2) { 
      k++; 
    } else if (mode == 3) { 
      k+=3; 
    } 
  } 
  return; 
}