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

📄 qua_lsp.c

📁 这是G.723和G.729的音频编解码的源代码
💻 C
字号:
/* Version 2.0    Last modified: 6/30/95 */

/*-------------------------------------------------------------------*
 * Function  Qua_lsp:                                                *
 *           ~~~~~~~~                                                *
 *-------------------------------------------------------------------*/
#include "typedef.h"
#include "basic_op.h"
#include "count.h"

#include "ld8k.h"
#include "tab_ld8k.h"

void Qua_lsp(
  Word16 lsp[],       /* (i) Q15 : Unquantized LSP            */
  Word16 lsp_q[],     /* (o) Q15 : Quantized LSP              */
  Word16 ana[]        /* (o)     : indexes                    */
)
{
  Word16 lsf[M], lsf_q[M];  /* domain 0.0<= lsf <PI in Q13 */

  /* Convert LSPs to LSFs */
  Lsp_lsf2(lsp, lsf, M);

  Lsp_qua_cs(lsf, lsf_q, ana );

  /* Convert LSFs to LSPs */
  Lsf_lsp2(lsf_q, lsp_q, M);

  return;
}


/* static memory */
static Word16 freq_prev[MA_NP][N_PR];    /* Q13:previous LSP vector       */
static Word16 freq_prev_reset[N_PR] = {  /* Q13:previous LSP vector(init) */
  2339, 4679, 7018, 9358, 11698, 14037, 16377, 18717, 21056, 23396
};     /* PI*(float)(j+1)/(float)(N_PR+1) */


void Lsp_encw_reset(
  void
)
{
  Word16 i;

  for(i=0; i<MA_NP; i++)
    Copy( &freq_prev_reset[0], &freq_prev[i][0], N_PR );
}


void Lsp_qua_cs(
  Word16 flsp_in[N_PR],    /* (i) Q13 : Original LSP parameters    */
  Word16 lspq_out[N_PR],   /* (o) Q13 : Quantized LSP parameters   */
  Word16 *code             /* (o)     : codes of the selected LSP  */
)
{
  Word16 wegt[N_PR];       /* Q11->normalized : weighting coefficients */

  Get_wegt( flsp_in, wegt );

  Relspwed( flsp_in, wegt, lspq_out, lspcb1, lspcb2, fg,
    freq_prev, fg_sum, fg_sum_inv, code);
}

void Relspwed(
  Word16 lsp[],                     /* (i) Q13 : unquantized LSP parameters */
  Word16 wegt[],                    /* (i) norm: weighting coefficients     */
  Word16 lspq[],                    /* (o) Q13 : quantized LSP parameters   */
  Word16 lspcb1[][N_PR],            /* (i) Q13 : first stage LSP codebook   */
  Word16 lspcb2[][N_PR],            /* (i) Q13 : Second stage LSP codebook  */
  Word16 fg[MODE][MA_NP][N_PR],     /* (i) Q15 : MA prediction coefficients */
  Word16 freq_prev[MA_NP][N_PR],    /* (i) Q13 : previous LSP vector        */
  Word16 fg_sum[MODE][N_PR],        /* (i) Q15 : present MA prediction coef.*/
  Word16 fg_sum_inv[MODE][N_PR],    /* (i) Q12 : inverse coef.              */
  Word16 code_ana[]                 /* (o)     : codes of the selected LSP  */
)
{
  Word16 mode, j;
  Word16 index, mode_index;
  Word16 cand[MODE], cand_cur;
  Word16 tindex1[MODE], tindex2[MODE];
  Word32 L_tdist[MODE];         /* Q26 */
  Word16 rbuf[N_PR];            /* Q13 */
  Word16 buf[N_PR];             /* Q13 */

  for(mode = 0; mode<MODE; mode++) {
    Lsp_prev_extract(lsp, rbuf, fg[mode], freq_prev, fg_sum_inv[mode]);

    Lsp_pre_select(rbuf, lspcb1, &cand_cur );
    cand[mode] = cand_cur;              move16();

    Lsp_select_1(rbuf, lspcb1[cand_cur], wegt, lspcb2, &index);

    tindex1[mode] = index;              move16();

    for( j = 0 ; j < N_PR/2 ; j++ ) {
      buf[j] = add( lspcb1[cand_cur][j], lspcb2[index][j] );
      move16();
    }

    Lsp_expand_1(buf, GAP1);

    Lsp_select_2(rbuf, lspcb1[cand_cur], wegt, lspcb2, buf[N_PR/2 -1], &index);

    tindex2[mode] = index;            move16();

    for( j = N_PR/2 ; j < N_PR ; j++ ) {
      buf[j] = add( lspcb1[cand_cur][j], lspcb2[index][j] );
      move16();
    }
    Lsp_expand_2(buf, GAP1);

    Lsp_expand_1_2(buf, GAP2);

    Lsp_get_tdist(wegt, buf, &L_tdist[mode], rbuf, fg_sum[mode]);
  }

  Lsp_last_select(L_tdist, &mode_index);

  code_ana[0] = shl( mode_index,NC0_B ) | cand[mode_index];
  code_ana[1] = shl( tindex1[mode_index],NC1_B ) | tindex2[mode_index];
  logic16();  logic16();

  Lsp_get_quant(lspcb1, lspcb2, cand[mode_index],
      tindex1[mode_index], tindex2[mode_index],
      fg[mode_index], freq_prev, lspq, fg_sum[mode_index]) ;

  return;
}


void Lsp_pre_select(
  Word16 rbuf[],              /* (i) Q13 : target vetor             */
  Word16 lspcb1[][N_PR],      /* (i) Q13 : first stage LSP codebook */
  Word16 *cand                /* (o)     : selected code            */
)
{
  Word16 i, j;
  Word16 tmp;                 /* Q13 */
  Word32 L_dmin;              /* Q26 */
  Word32 L_tmp;               /* Q26 */
  Word32 L_temp;

                        /* avoid the worst case. (all over flow) */
  *cand = 0;                    move16();
  L_dmin = MAX_32;              move32();
  for ( i = 0 ; i < NC0 ; i++ ) {
    L_tmp = 0;                  move32();
    for ( j = 0 ; j < N_PR ; j++ ) {
      tmp = sub(rbuf[j], lspcb1[i][j]);
      L_tmp = L_mac( L_tmp, tmp, tmp );
    }

    test();
    L_temp = L_sub(L_tmp,L_dmin);
    if (  L_temp< 0L) {
      L_dmin = L_tmp;           move32();
      *cand = i;                move16();
    }
  }
  return;
}




void Lsp_select_1(
  Word16 rbuf[],              /* (i) Q13 : target vector             */
  Word16 lspcb1[],            /* (i) Q13 : first stage lsp codebook  */
  Word16 wegt[],              /* (i) norm: weighting coefficients    */
  Word16 lspcb2[][N_PR],      /* (i) Q13 : second stage lsp codebook */
  Word16 *index               /* (o)     : selected codebook index   */
)
{
  Word16 j, k1;
  Word16 buf[N_PR];           /* Q13 */
  Word32 L_dist;              /* Q26 */
  Word32 L_dmin;              /* Q26 */
  Word16 tmp,tmp2;            /* Q13 */
  Word32 L_acc ;
  Word32 L_temp;

                           /* avoid the worst case. (all over flow) */
  *index = 0;                           move16();
  L_dmin = MAX_32;                      move32();
  for ( k1 = 0 ; k1 < NC1 ; k1++ ) {
    for ( j = 0 ; j < N_PR/2 ; j++ ) {
      buf[j] = add( lspcb1[j], lspcb2[k1][j] );
      move16();
  }

    Lsp_expand_1(buf, GAP1);

    L_dist = 0;                         move32();
    for ( j = 0 ; j < N_PR/2 ; j++ ) {
      tmp = sub( rbuf[j], buf[j] );
      L_acc = L_mult( wegt[j], tmp );
      tmp2 = extract_h( L_shl( L_acc, 3 ) );
      L_dist = L_mac( L_dist, tmp2, tmp );
    }

    test();
    L_temp =L_sub(L_dist,L_dmin);
    if ( L_temp <0L ) {
      L_dmin = L_dist;                  move32();
      *index = k1;                      move16();
    }
  }
  return;
}



void Lsp_select_2(
  Word16 rbuf[],              /* (i) Q13 : target vector             */
  Word16 lspcb1[],            /* (i) Q13 : first stage lsp codebook  */
  Word16 wegt[],              /* (i) norm: weighting coef.           */
  Word16 lspcb2[][N_PR],      /* (i) Q13 : second stage lsp codebook */
  Word16 bufsave,             /* (i) Q13 : the value of boundary     */
  Word16 *index               /* (o)     : selected codebook index   */
)
{
  Word16 j, k1;
  Word16 buf[N_PR];           /* Q13 */
  Word32 L_dist;              /* Q26 */
  Word32 L_dmin;              /* Q26 */
  Word16 tmp,tmp2;            /* Q13 */
  Word32 L_acc ;
  Word32 L_temp;

                            /* avoid the worst case. (all over flow) */
  *index = 0;                               move16();
  L_dmin = MAX_32;                          move32();
  for ( k1 = 0 ; k1 < NC1 ; k1++ ) {
    buf[N_PR/2-1] = bufsave;                move16();
    for ( j = N_PR/2 ; j < N_PR ; j++ ) {
      buf[j] = add( lspcb1[j], lspcb2[k1][j] );
      move16();
    }
    Lsp_expand_2(buf, GAP1);

    L_dist = 0;                             move32();
    for ( j = N_PR/2 ; j < N_PR ; j++ ) {
      tmp = sub( rbuf[j], buf[j] );
      L_acc = L_mult( wegt[j], tmp );
      tmp2 = extract_h( L_shl( L_acc, 3 ) );
      L_dist = L_mac( L_dist, tmp2, tmp );
    }

    test();
    L_temp = L_sub(L_dist, L_dmin);
    if ( L_temp <0L ) {
      L_dmin = L_dist;                      move32();
      *index = k1;                          move16();
    }
  }
  return;
}

void Lsp_get_tdist(
  Word16 wegt[],        /* (i) norm: weight coef.                */
  Word16 buf[],         /* (i) Q13 : candidate LSP vector        */
  Word32 *L_tdist,      /* (o) Q27 : distortion                  */
  Word16 rbuf[],        /* (i) Q13 : target vector               */
  Word16 fg_sum[]       /* (i) Q15 : present MA prediction coef. */
)
{
  Word16 j;
  Word16 tmp, tmp2;     /* Q13 */
  Word32 L_acc;         /* Q25 */

  *L_tdist = 0;                 move32();
  for ( j = 0 ; j < N_PR ; j++ ) {
    /* tmp = (buf - rbuf)*fg_sum */
    tmp = sub( buf[j], rbuf[j] );
    tmp = mult( tmp, fg_sum[j] );

    /* *L_tdist += wegt * tmp * tmp */
    L_acc = L_mult( wegt[j], tmp );
    tmp2 = extract_h( L_shl( L_acc, 4 ) );
    *L_tdist = L_mac( *L_tdist, tmp2, tmp );
  }

  return;
}



void Lsp_last_select(
  Word32 L_tdist[],     /* (i) Q27 : distortion         */
  Word16 *mode_index    /* (o)     : the selected mode  */
)
{
    Word32 L_temp;
  *mode_index = 0;              move16();
  test();
  L_temp =L_sub(L_tdist[1] ,L_tdist[0]);
  if (  L_temp<0L){
    *mode_index = 1;            move16();
  }
  return;
}

void Get_wegt(
  Word16 flsp[],    /* (i) Q13 : M LSP parameters  */
  Word16 wegt[]     /* (o) Q11->norm : M weighting coefficients */
)
{
  Word16 i;
  Word16 tmp;
  Word32 L_acc;
  Word16 sft;
  Word16 buf[N_PR]; /* in Q13 */


  /* */
  buf[0] = sub( flsp[1], (PI04+8192) );           /* 8192:1.0(Q13) */
  move16();

  for ( i = 1 ; i < N_PR-1 ; i++ ) {
    tmp = sub( flsp[i+1], flsp[i-1] );
    buf[i] = sub( tmp, 8192 );
    move16();
  }

  buf[N_PR-1] = sub( (PI92-8192), flsp[N_PR-2] );
  move16();

  /* */
  for ( i = 0 ; i < N_PR ; i++ ) {
    test();
    if ( buf[i] > 0 ){
      wegt[i] = 2048;       move16();             /* 2048:1.0(Q11) */
    }
    else {
      L_acc = L_mult( buf[i], buf[i] );           /* L_acc in Q27 */
      tmp = extract_h( L_shl( L_acc, 2 ) );       /* tmp in Q13 */

      L_acc = L_mult( tmp, CONST10 );             /* L_acc in Q25 */
      tmp = extract_h( L_shl( L_acc, 2 ) );       /* tmp in Q11 */

      wegt[i] = add( tmp, 2048 );                 /* wegt in Q11 */
      move16();
    }
  }


  /* */
  L_acc = L_mult( wegt[4], CONST12 );             /* L_acc in Q26 */
  wegt[4] = extract_h( L_shl( L_acc, 1 ) );       /* wegt in Q11 */
  move16();

  L_acc = L_mult( wegt[5], CONST12 );             /* L_acc in Q26 */
  wegt[5] = extract_h( L_shl( L_acc, 1 ) );       /* wegt in Q11 */
  move16();

  /* wegt: Q11 -> normalized */
  tmp = 0;                  move16();
  for ( i = 0; i < N_PR; i++ ) {
    test();
    if ( sub(wegt[i], tmp) > 0 ) {
      tmp = wegt[i];        move16();
    }
  }

  sft = norm_s(tmp);
  for ( i = 0; i < N_PR; i++ ) {
    wegt[i] = shl(wegt[i], sft);                  /* wegt in Q(11+sft) */
    move16();
  }

  return;
}

⌨️ 快捷键说明

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