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

语音压缩

开发平台:

C/C++

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