QUA_GAIN.C
上传用户:meifeng08
上传日期:2013-06-18
资源大小:5304k
文件大小:19k
源码类别:

语音压缩

开发平台:

C/C++

  1. /*
  2.    ITU-T G.729A Speech Coder    ANSI-C Source Code
  3.    Version 1.1    Last modified: September 1996
  4.    Copyright (c) 1996,
  5.    AT&T, France Telecom, NTT, Universite de Sherbrooke
  6.    All rights reserved.
  7. */
  8. #include "typedef.h"
  9. #include "basic_op.h"
  10. #include "oper_32b.h"
  11. #include "ld8a.h"
  12. #include "tab_ld8a.h"
  13. static void Gbk_presel(
  14.    Word16 best_gain[],     /* (i) [0] Q9 : unquantized pitch gain     */
  15.                            /* (i) [1] Q2 : unquantized code gain      */
  16.    Word16 *cand1,          /* (o)    : index of best 1st stage vector */
  17.    Word16 *cand2,          /* (o)    : index of best 2nd stage vector */
  18.    Word16 gcode0           /* (i) Q4 : presearch for gain codebook    */
  19. );
  20. /*---------------------------------------------------------------------------*
  21.  * Function  Qua_gain                                                        *
  22.  * ~~~~~~~~~~~~~~~~~~                                                        *
  23.  * Inputs:                                                                   *
  24.  *   code[]     :Innovative codebook.                                        *
  25.  *   g_coeff[]  :Correlations compute for pitch.                             *
  26.  *   L_subfr    :Subframe length.                                            *
  27.  *                                                                           *
  28.  * Outputs:                                                                  *
  29.  *   gain_pit   :Quantized pitch gain.                                       *
  30.  *   gain_cod   :Quantized code gain.                                        *
  31.  *                                                                           *
  32.  * Return:                                                                   *
  33.  *   Index of quantization.                                                  *
  34.  *                                                                           *
  35.  *--------------------------------------------------------------------------*/
  36. Word16 Qua_gain(
  37.    Word16 code[],       /* (i) Q13 :Innovative vector.             */
  38.    Word16 g_coeff[],    /* (i)     :Correlations <xn y1> -2<y1 y1> */
  39.                         /*            <y2,y2>, -2<xn,y2>, 2<y1,y2> */
  40.    Word16 exp_coeff[],  /* (i)     :Q-Format g_coeff[]             */
  41.    Word16 L_subfr,      /* (i)     :Subframe length.               */
  42.    Word16 *gain_pit,    /* (o) Q14 :Pitch gain.                    */
  43.    Word16 *gain_cod,    /* (o) Q1  :Code gain.                     */
  44.    Word16 tameflag      /* (i)     : set to 1 if taming is needed  */
  45. )
  46. {
  47.    Word16  i, j, index1, index2;
  48.    Word16  cand1, cand2;
  49.    Word16  exp, gcode0, exp_gcode0, gcode0_org, e_min ;
  50.    Word16  nume, denom, inv_denom;
  51.    Word16  exp1,exp2,exp_nume,exp_denom,exp_inv_denom,sft,tmp;
  52.    Word16  g_pitch, g2_pitch, g_code, g2_code, g_pit_cod;
  53.    Word16  coeff[5], coeff_lsf[5];
  54.    Word16  exp_min[5];
  55.    Word32  L_gbk12;
  56.    Word32  L_tmp, L_dist_min, L_temp, L_tmp1, L_tmp2, L_acc, L_accb;
  57.    Word16  best_gain[2];
  58.         /* Gain predictor, Past quantized energies = -14.0 in Q10 */
  59.  static Word16 past_qua_en[4] = { -14336, -14336, -14336, -14336 };
  60.   /*---------------------------------------------------*
  61.    *-  energy due to innovation                       -*
  62.    *-  predicted energy                               -*
  63.    *-  predicted codebook gain => gcode0[exp_gcode0]  -*
  64.    *---------------------------------------------------*/
  65.    Gain_predict( past_qua_en, code, L_subfr, &gcode0, &exp_gcode0 );
  66.   /*-----------------------------------------------------------------*
  67.    *  pre-selection                                                  *
  68.    *-----------------------------------------------------------------*/
  69.   /*-----------------------------------------------------------------*
  70.    *  calculate best gain                                            *
  71.    *                                                                 *
  72.    *  tmp = -1./(4.*coeff[0]*coeff[2]-coeff[4]*coeff[4]) ;           *
  73.    *  best_gain[0] = (2.*coeff[2]*coeff[1]-coeff[3]*coeff[4])*tmp ;  *
  74.    *  best_gain[1] = (2.*coeff[0]*coeff[3]-coeff[1]*coeff[4])*tmp ;  *
  75.    *  gbk_presel(best_gain,&cand1,&cand2,gcode0) ;                   *
  76.    *                                                                 *
  77.    *-----------------------------------------------------------------*/
  78.   /*-----------------------------------------------------------------*
  79.    *  tmp = -1./(4.*coeff[0]*coeff[2]-coeff[4]*coeff[4]) ;           *
  80.    *-----------------------------------------------------------------*/
  81.    L_tmp1 = L_mult( g_coeff[0], g_coeff[2] );
  82.    exp1   = add( add( exp_coeff[0], exp_coeff[2] ), 1-2 );
  83.    L_tmp2 = L_mult( g_coeff[4], g_coeff[4] );
  84.    exp2   = add( add( exp_coeff[4], exp_coeff[4] ), 1 );
  85.    if( sub(exp1, exp2)>0 ){
  86.       L_tmp = L_sub( L_shr( L_tmp1, sub(exp1,exp2) ), L_tmp2 );
  87.       exp = exp2;
  88.    }
  89.    else{
  90.       L_tmp = L_sub( L_tmp1, L_shr( L_tmp2, sub(exp2,exp1) ) );
  91.       exp = exp1;
  92.    }
  93.    sft = norm_l( L_tmp );
  94.    denom = extract_h( L_shl(L_tmp, sft) );
  95.    exp_denom = sub( add( exp, sft ), 16 );
  96.    inv_denom = div_s(16384,denom);
  97.    inv_denom = negate( inv_denom );
  98.    exp_inv_denom = sub( 14+15, exp_denom );
  99.   /*-----------------------------------------------------------------*
  100.    *  best_gain[0] = (2.*coeff[2]*coeff[1]-coeff[3]*coeff[4])*tmp ;  *
  101.    *-----------------------------------------------------------------*/
  102.    L_tmp1 = L_mult( g_coeff[2], g_coeff[1] );
  103.    exp1   = add( exp_coeff[2], exp_coeff[1] );
  104.    L_tmp2 = L_mult( g_coeff[3], g_coeff[4] );
  105.    exp2   = add( add( exp_coeff[3], exp_coeff[4] ), 1 );
  106.    if( sub(exp1, exp2)>0 ){
  107.       L_tmp = L_sub( L_shr( L_tmp1, add(sub(exp1,exp2),1 )), L_shr( L_tmp2,1 ) );
  108.       exp = sub(exp2,1);
  109.    }
  110.    else{
  111.       L_tmp = L_sub( L_shr( L_tmp1,1 ), L_shr( L_tmp2, add(sub(exp2,exp1),1 )) );
  112.       exp = sub(exp1,1);
  113.    }
  114.    sft = norm_l( L_tmp );
  115.    nume = extract_h( L_shl(L_tmp, sft) );
  116.    exp_nume = sub( add( exp, sft ), 16 );
  117.    sft = sub( add( exp_nume, exp_inv_denom ), (9+16-1) );
  118.    L_acc = L_shr( L_mult( nume,inv_denom ), sft );
  119.    best_gain[0] = extract_h( L_acc );             /*-- best_gain[0]:Q9 --*/
  120.    if (tameflag == 1){
  121.      if(sub(best_gain[0], GPCLIP2) > 0) best_gain[0] = GPCLIP2;
  122.    }
  123.   /*-----------------------------------------------------------------*
  124.    *  best_gain[1] = (2.*coeff[0]*coeff[3]-coeff[1]*coeff[4])*tmp ;  *
  125.    *-----------------------------------------------------------------*/
  126.    L_tmp1 = L_mult( g_coeff[0], g_coeff[3] );
  127.    exp1   = add( exp_coeff[0], exp_coeff[3] ) ;
  128.    L_tmp2 = L_mult( g_coeff[1], g_coeff[4] );
  129.    exp2   = add( add( exp_coeff[1], exp_coeff[4] ), 1 );
  130.    if( sub(exp1, exp2)>0 ){
  131.       L_tmp = L_sub( L_shr( L_tmp1, add(sub(exp1,exp2),1) ), L_shr( L_tmp2,1 ) );
  132.       exp = sub(exp2,1);
  133.    }
  134.    else{
  135.       L_tmp = L_sub( L_shr( L_tmp1,1 ), L_shr( L_tmp2, add(sub(exp2,exp1),1) ) );
  136.       exp = sub(exp1,1);
  137.    }
  138.    sft = norm_l( L_tmp );
  139.    nume = extract_h( L_shl(L_tmp, sft) );
  140.    exp_nume = sub( add( exp, sft ), 16 );
  141.    sft = sub( add( exp_nume, exp_inv_denom ), (2+16-1) );
  142.    L_acc = L_shr( L_mult( nume,inv_denom ), sft );
  143.    best_gain[1] = extract_h( L_acc );             /*-- best_gain[1]:Q2 --*/
  144.    /*--- Change Q-format of gcode0 ( Q[exp_gcode0] -> Q4 ) ---*/
  145.    if( sub(exp_gcode0,4) >= 0 ){
  146.       gcode0_org = shr( gcode0, sub(exp_gcode0,4) );
  147.    }
  148.    else{
  149.       L_acc = L_deposit_l( gcode0 );
  150.       L_acc = L_shl( L_acc, sub( (4+16), exp_gcode0 ) );
  151.       gcode0_org = extract_h( L_acc );              /*-- gcode0_org:Q4 --*/
  152.    }
  153.   /*----------------------------------------------*
  154.    *   - presearch for gain codebook -            *
  155.    *----------------------------------------------*/
  156.    Gbk_presel(best_gain, &cand1, &cand2, gcode0_org );
  157. /*---------------------------------------------------------------------------*
  158.  *                                                                           *
  159.  * Find the best quantizer.                                                  *
  160.  *                                                                           *
  161.  *  dist_min = MAX_32;                                                       *
  162.  *  for ( i=0 ; i<NCAN1 ; i++ ){                                             *
  163.  *    for ( j=0 ; j<NCAN2 ; j++ ){                                           *
  164.  *      g_pitch = gbk1[cand1+i][0] + gbk2[cand2+j][0];                       *
  165.  *      g_code = gcode0 * (gbk1[cand1+i][1] + gbk2[cand2+j][1]);             *
  166.  *      dist = g_pitch*g_pitch * coeff[0]                                    *
  167.  *           + g_pitch         * coeff[1]                                    *
  168.  *           + g_code*g_code   * coeff[2]                                    *
  169.  *           + g_code          * coeff[3]                                    *
  170.  *           + g_pitch*g_code  * coeff[4] ;                                  *
  171.  *                                                                           *
  172.  *      if (dist < dist_min){                                                *
  173.  *        dist_min = dist;                                                   *
  174.  *        indice1 = cand1 + i ;                                              *
  175.  *        indice2 = cand2 + j ;                                              *
  176.  *      }                                                                    *
  177.  *    }                                                                      *
  178.  *  }                                                                        *
  179.  *                                                                           *
  180.  * g_pitch         = Q13                                                     *
  181.  * g_pitch*g_pitch = Q11:(13+13+1-16)                                        *
  182.  * g_code          = Q[exp_gcode0-3]:(exp_gcode0+(13-1)+1-16)                *
  183.  * g_code*g_code   = Q[2*exp_gcode0-21]:(exp_gcode0-3+exp_gcode0-3+1-16)     *
  184.  * g_pitch*g_code  = Q[exp_gcode0-5]:(13+exp_gcode0-3+1-16)                  *
  185.  *                                                                           *
  186.  * term 0: g_pitch*g_pitch*coeff[0] ;exp_min0 = 13             +exp_coeff[0] *
  187.  * term 1: g_pitch        *coeff[1] ;exp_min1 = 14             +exp_coeff[1] *
  188.  * term 2: g_code*g_code  *coeff[2] ;exp_min2 = 2*exp_gcode0-21+exp_coeff[2] *
  189.  * term 3: g_code         *coeff[3] ;exp_min3 = exp_gcode0  - 3+exp_coeff[3] *
  190.  * term 4: g_pitch*g_code *coeff[4] ;exp_min4 = exp_gcode0  - 4+exp_coeff[4] *
  191.  *                                                                           *
  192.  *---------------------------------------------------------------------------*/
  193.    exp_min[0] = add( exp_coeff[0], 13 );
  194.    exp_min[1] = add( exp_coeff[1], 14 );
  195.    exp_min[2] = add( exp_coeff[2], sub( shl( exp_gcode0, 1 ), 21 ) );
  196.    exp_min[3] = add( exp_coeff[3], sub( exp_gcode0, 3 ) );
  197.    exp_min[4] = add( exp_coeff[4], sub( exp_gcode0, 4 ) );
  198.    e_min = exp_min[0];
  199.    for(i=1; i<5; i++){
  200.       if( sub(exp_min[i], e_min) < 0 ){
  201.          e_min = exp_min[i];
  202.       }
  203.    }
  204.    /* align coeff[] and save in special 32 bit double precision */
  205.    for(i=0; i<5; i++){
  206.      j = sub( exp_min[i], e_min );
  207.      L_tmp = L_deposit_h( g_coeff[i] );
  208.      L_tmp = L_shr( L_tmp, j );          /* L_tmp:Q[exp_g_coeff[i]+16-j] */
  209.      L_Extract( L_tmp, &coeff[i], &coeff_lsf[i] );          /* DPF */
  210.    }
  211.    /* Codebook search */
  212.    L_dist_min = MAX_32;
  213.    /* initialization used only to suppress Microsoft Visual C++  warnings */
  214.    index1 = cand1;
  215.    index2 = cand2;
  216. if(tameflag == 1){
  217.    for(i=0; i<NCAN1; i++){
  218.       for(j=0; j<NCAN2; j++){
  219.          g_pitch = add( gbk1[cand1+i][0], gbk2[cand2+j][0] );     /* Q14 */
  220.          if(g_pitch < GP0999) {
  221.          L_acc = L_deposit_l( gbk1[cand1+i][1] );
  222.          L_accb = L_deposit_l( gbk2[cand2+j][1] );                /* Q13 */
  223.          L_tmp = L_add( L_acc,L_accb );
  224.          tmp = extract_l( L_shr( L_tmp,1 ) );                     /* Q12 */
  225.          g_code   = mult( gcode0, tmp );         /*  Q[exp_gcode0+12-15] */
  226.          g2_pitch = mult(g_pitch, g_pitch);                       /* Q13 */
  227.          g2_code  = mult(g_code,  g_code);       /* Q[2*exp_gcode0-6-15] */
  228.          g_pit_cod= mult(g_code,  g_pitch);      /* Q[exp_gcode0-3+14-15] */
  229.          L_tmp = Mpy_32_16(coeff[0], coeff_lsf[0], g2_pitch);
  230.          L_tmp = L_add(L_tmp, Mpy_32_16(coeff[1], coeff_lsf[1], g_pitch) );
  231.          L_tmp = L_add(L_tmp, Mpy_32_16(coeff[2], coeff_lsf[2], g2_code) );
  232.          L_tmp = L_add(L_tmp, Mpy_32_16(coeff[3], coeff_lsf[3], g_code) );
  233.          L_tmp = L_add(L_tmp, Mpy_32_16(coeff[4], coeff_lsf[4], g_pit_cod) );
  234.          L_temp = L_sub(L_tmp, L_dist_min);
  235.          if( L_temp < 0L ){
  236.             L_dist_min = L_tmp;
  237.             index1 = add(cand1,i);
  238.             index2 = add(cand2,j);
  239.          }
  240.         }
  241.       }
  242.    }
  243. }
  244. else{
  245.    for(i=0; i<NCAN1; i++){
  246.       for(j=0; j<NCAN2; j++){
  247.          g_pitch = add( gbk1[cand1+i][0], gbk2[cand2+j][0] );     /* Q14 */
  248.          L_acc = L_deposit_l( gbk1[cand1+i][1] );
  249.          L_accb = L_deposit_l( gbk2[cand2+j][1] );                /* Q13 */
  250.          L_tmp = L_add( L_acc,L_accb );
  251.          tmp = extract_l( L_shr( L_tmp,1 ) );                     /* Q12 */
  252.          g_code   = mult( gcode0, tmp );         /*  Q[exp_gcode0+12-15] */
  253.          g2_pitch = mult(g_pitch, g_pitch);                       /* Q13 */
  254.          g2_code  = mult(g_code,  g_code);       /* Q[2*exp_gcode0-6-15] */
  255.          g_pit_cod= mult(g_code,  g_pitch);      /* Q[exp_gcode0-3+14-15] */
  256.          L_tmp = Mpy_32_16(coeff[0], coeff_lsf[0], g2_pitch);
  257.          L_tmp = L_add(L_tmp, Mpy_32_16(coeff[1], coeff_lsf[1], g_pitch) );
  258.          L_tmp = L_add(L_tmp, Mpy_32_16(coeff[2], coeff_lsf[2], g2_code) );
  259.          L_tmp = L_add(L_tmp, Mpy_32_16(coeff[3], coeff_lsf[3], g_code) );
  260.          L_tmp = L_add(L_tmp, Mpy_32_16(coeff[4], coeff_lsf[4], g_pit_cod) );
  261.          L_temp = L_sub(L_tmp, L_dist_min);
  262.          if( L_temp < 0L ){
  263.             L_dist_min = L_tmp;
  264.             index1 = add(cand1,i);
  265.             index2 = add(cand2,j);
  266.          }
  267.       }
  268.    }
  269. }
  270.    /* Read the quantized gain */
  271.   /*-----------------------------------------------------------------*
  272.    * *gain_pit = gbk1[indice1][0] + gbk2[indice2][0];                *
  273.    *-----------------------------------------------------------------*/
  274.    *gain_pit = add( gbk1[index1][0], gbk2[index2][0] );      /* Q14 */
  275.   /*-----------------------------------------------------------------*
  276.    * *gain_code = (gbk1[indice1][1]+gbk2[indice2][1]) * gcode0;      *
  277.    *-----------------------------------------------------------------*/
  278.    L_acc = L_deposit_l( gbk1[index1][1] );
  279.    L_accb = L_deposit_l( gbk2[index2][1] );
  280.    L_gbk12 = L_add( L_acc, L_accb );                          /* Q13 */
  281.    tmp = extract_l( L_shr( L_gbk12,1 ) );                     /* Q12 */
  282.    L_acc = L_mult(tmp, gcode0);                /* Q[exp_gcode0+12+1] */
  283.    L_acc = L_shl(L_acc, add( negate(exp_gcode0),(-12-1+1+16) ));
  284.    *gain_cod = extract_h( L_acc );                             /* Q1 */
  285.   /*----------------------------------------------*
  286.    * update table of past quantized energies      *
  287.    *----------------------------------------------*/
  288.    Gain_update( past_qua_en, L_gbk12 );
  289.    return( add( map1[index1]*(Word16)NCODE2, map2[index2] ) );
  290. }
  291. /*---------------------------------------------------------------------------*
  292.  * Function Gbk_presel                                                       *
  293.  * ~~~~~~~~~~~~~~~~~~~                                                       *
  294.  *   - presearch for gain codebook -                                         *
  295.  *---------------------------------------------------------------------------*/
  296. static void Gbk_presel(
  297.    Word16 best_gain[],     /* (i) [0] Q9 : unquantized pitch gain     */
  298.                            /* (i) [1] Q2 : unquantized code gain      */
  299.    Word16 *cand1,          /* (o)    : index of best 1st stage vector */
  300.    Word16 *cand2,          /* (o)    : index of best 2nd stage vector */
  301.    Word16 gcode0           /* (i) Q4 : presearch for gain codebook    */
  302. )
  303. {
  304.    Word16    acc_h;
  305.    Word16    sft_x,sft_y;
  306.    Word32    L_acc,L_preg,L_cfbg,L_tmp,L_tmp_x,L_tmp_y;
  307.    Word32 L_temp;
  308.  /*--------------------------------------------------------------------------*
  309.    x = (best_gain[1]-(coef[0][0]*best_gain[0]+coef[1][1])*gcode0) * inv_coef;
  310.   *--------------------------------------------------------------------------*/
  311.    L_cfbg = L_mult( coef[0][0], best_gain[0] );        /* L_cfbg:Q20 -> !!y */
  312.    L_acc = L_shr( L_coef[1][1], 15 );                  /* L_acc:Q20     */
  313.    L_acc = L_add( L_cfbg , L_acc );
  314.    acc_h = extract_h( L_acc );                         /* acc_h:Q4      */
  315.    L_preg = L_mult( acc_h, gcode0 );                   /* L_preg:Q9     */
  316.    L_acc = L_shl( L_deposit_l( best_gain[1] ), 7 );    /* L_acc:Q9      */
  317.    L_acc = L_sub( L_acc, L_preg );
  318.    acc_h = extract_h( L_shl( L_acc,2 ) );              /* L_acc_h:Q[-5] */
  319.    L_tmp_x = L_mult( acc_h, INV_COEF );                /* L_tmp_x:Q15   */
  320.  /*--------------------------------------------------------------------------*
  321.    y = (coef[1][0]*(-coef[0][1]+best_gain[0]*coef[0][0])*gcode0
  322.                                       -coef[0][0]*best_gain[1]) * inv_coef;
  323.   *--------------------------------------------------------------------------*/
  324.    L_acc = L_shr( L_coef[0][1], 10 );                  /* L_acc:Q20   */
  325.    L_acc = L_sub( L_cfbg, L_acc );                     /* !!x -> L_cfbg:Q20 */
  326.    acc_h = extract_h( L_acc );                         /* acc_h:Q4    */
  327.    acc_h = mult( acc_h, gcode0 );                      /* acc_h:Q[-7] */
  328.    L_tmp = L_mult( acc_h, coef[1][0] );                /* L_tmp:Q10   */
  329.    L_preg = L_mult( coef[0][0], best_gain[1] );        /* L_preg:Q13  */
  330.    L_acc = L_sub( L_tmp, L_shr(L_preg,3) );            /* L_acc:Q10   */
  331.    acc_h = extract_h( L_shl( L_acc,2 ) );              /* acc_h:Q[-4] */
  332.    L_tmp_y = L_mult( acc_h, INV_COEF );                /* L_tmp_y:Q16 */
  333.    sft_y = (14+4+1)-16;         /* (Q[thr1]+Q[gcode0]+1)-Q[L_tmp_y] */
  334.    sft_x = (15+4+1)-15;         /* (Q[thr2]+Q[gcode0]+1)-Q[L_tmp_x] */
  335.    if(gcode0>0){
  336.       /*-- pre select codebook #1 --*/
  337.       *cand1 = 0 ;
  338.       do{
  339.          L_temp = L_sub( L_tmp_y, L_shr(L_mult(thr1[*cand1],gcode0),sft_y));
  340.          if(L_temp >0L  ){
  341.         (*cand1) =add(*cand1,1);
  342.      }
  343.          else               break ;
  344.       } while(sub((*cand1),(NCODE1-NCAN1))<0) ;
  345.       /*-- pre select codebook #2 --*/
  346.       *cand2 = 0 ;
  347.       do{
  348.         L_temp = L_sub( L_tmp_x , L_shr(L_mult(thr2[*cand2],gcode0),sft_x));
  349.          if( L_temp >0L) {
  350.         (*cand2) =add(*cand2,1);
  351.      }
  352.          else               break ;
  353.       } while(sub((*cand2),(NCODE2-NCAN2))<0) ;
  354.    }
  355.    else{
  356.       /*-- pre select codebook #1 --*/
  357.       *cand1 = 0 ;
  358.       do{
  359.         L_temp = L_sub(L_tmp_y ,L_shr(L_mult(thr1[*cand1],gcode0),sft_y));
  360.          if( L_temp <0L){
  361.         (*cand1) =add(*cand1,1);
  362.      }
  363.          else               break ;
  364.       } while(sub((*cand1),(NCODE1-NCAN1))) ;
  365.       /*-- pre select codebook #2 --*/
  366.       *cand2 = 0 ;
  367.       do{
  368.          L_temp =L_sub(L_tmp_x ,L_shr(L_mult(thr2[*cand2],gcode0),sft_x));
  369.          if( L_temp <0L){
  370.         (*cand2) =add(*cand2,1);
  371.      }
  372.          else               break ;
  373.       } while(sub( (*cand2),(NCODE2-NCAN2))) ;
  374.    }
  375.    return ;
  376. }