📄 acelp_cp.c
字号:
/*
ITU-T G.729 Annex I - Reference C code for fixed point
implementation of G.729 Annex I
Version 1.1 of October 1999
*/
/*
File : acelp_cp.c
*/
/* from acelp_e.c G.729 Annex E Version 1.2 Last modified: May 1998 */
/* from acelpcod.c G.729 Annex D Version 1.2 Last modified: May 1998 */
/* from acelp_co.c G.729 Version 3.3 */
#include <stdio.h>
#include <stdlib.h>
#include "typedef.h"
#include "basic_op.h"
#include "ld8k.h"
#include "ld8cp.h"
#include "tabld8cp.h"
/* locals functions */
static void Cor_h_cp(
Word16 *H, /* (i) Q12 :Impulse response of filters */
Word16 *rr, /* (o) :Correlations of H[] */
Word16 rate /* (i) frame rate */
);
static void Cor_h_X(
Word16 h[], /* (i) Q12 :Impulse response of filters */
Word16 X[], /* (i) Q12 :Target vector */
Word16 D[] /* (o) :Correlations between h[] and D[] */
/* Normalized to 13 bits */
);
static Word16 D4i40_17( /* (o) : Index of pulses positions. */
Word16 Dn[], /* (i) : Correlations between h[] and Xn[]. */
Word16 rr[], /* (i) : Correlations of impulse response h[]. */
Word16 h[], /* (i) Q12: Impulse response of filters. */
Word16 cod[], /* (o) Q13: Selected algebraic codeword. */
Word16 y[], /* (o) Q12: Filtered algebraic codeword. */
Word16 *sign, /* (o) : Signs of 4 pulses. */
Word16 i_subfr /* (i) : subframe flag */
);
static Word16 D2i40_11( /* (o) : Index of pulses positions. */
Word16 Dn[], /* (i) : Correlations between h[] and Xn[]. */
Word16 rr[], /* (i) : Correlations of impulse response h[]. */
Word16 h[], /* (i) Q12: Impulse response of filters. */
Word16 code[], /* (o) Q13: Selected algebraic codeword. */
Word16 y[], /* (o) Q12: Filtered algebraic codeword. */
Word16 *sign /* (o) : Signs of 4 pulses. */
);
static void cor_h_x_e(
Word16 h[], /* (i) Q12 : impulse response of weighted synthesis filter */
Word16 x[], /* (i) Q0 : correlation between target and h[] */
Word16 dn[] /* (o) Q0 : correlation between target and h[] */
);
static void cor_h_vec(
Word16 h[], /* (i) scaled impulse response */
Word16 vec[], /* (i) vector to correlate with h[] */
Word16 track, /* (i) track to use */
Word16 sign[], /* (i) sign vector */
Word16 rrixix[][NB_POS], /* (i) correlation of h[x] with h[x] */
Word16 cor[] /* (o) result of correlation (NB_POS elements) */
);
static void search_ixiy(
Word16 track_x, /* (i) track of pulse 1 */
Word16 track_y, /* (i) track of pulse 2 */
Word16 *ps, /* (i/o) correlation of all fixed pulses */
Word16 *alp, /* (i/o) energy of all fixed pulses */
Word16 *ix, /* (o) position of pulse 1 */
Word16 *iy, /* (o) position of pulse 2 */
Word16 dn[], /* (i) corr. between target and h[] */
Word16 cor_x[], /* (i) corr. of pulse 1 with fixed pulses */
Word16 cor_y[], /* (i) corr. of pulse 2 with fixed pulses */
Word16 rrixiy[][MSIZE] /* (i) corr. of pulse 1 with pulse 2 */
);
static void set_sign(
Word16 fac_cn, /* (i) Q15: residual weight for sign determination */
Word16 cn[], /* (i) Q0 : residual after long term prediction */
Word16 dn[], /* (i) Q0 : correlation between target and h[] */
Word16 sign[], /* (o) Q15: sign vector (sign of each position) */
Word16 inv_sign[], /* (o) Q15: inverse of sign[] */
Word16 pos_max[], /* (o) : pos of max of correlation */
Word32 corr[] /* (o) : correlation of each track */
);
static void cor_h_e(
Word16 H[], /* (i) Q12 :Impulse response of filters */
Word16 sign[], /* (i) Q15: sign vector */
Word16 inv_sign[], /* (i) Q15: inverse of sign[] */
Word16 h[], /* (o) : scaled h[] */
Word16 h_inv[], /* (o) : inverse of scaled h[] */
Word16 rrixix[][NB_POS], /* (o) energy of h[]. */
Word16 rrixiy[][MSIZE] /* (o) correlation between 2 pulses. */
);
static void build_code(
Word16 codvec[], /* (i) : positions of each pulse */
Word16 sign[], /* (i) Q15: sign vector */
Word16 nb_of_pulse, /* (i) : number of pulses */
Word16 H[], /* (i) Q12: impulse response of weighted synthesis filter */
Word16 code[], /* (o) Q12: algebraic (fixed) codebook excitation */
Word16 y[], /* (o) Q11: filtered fixed codebook excitation */
Word16 indx[] /* (o) : index of pulses (5 words, 1 per track). */
);
static Word16 pack3(Word16 index1, Word16 index2, Word16 index3);
Word16 ACELP_Codebook( /* (o) :index of pulses positions */
Word16 x[], /* (i) :Target vector */
Word16 h[], /* (i) Q12 :Impulse response of filters */
Word16 T0, /* (i) :Pitch lag */
Word16 pitch_sharp, /* (i) Q14 :Last quantized pitch gain */
Word16 i_subfr, /* (i) :Indicator of 1st subframe, */
Word16 code[], /* (o) Q13 :Innovative codebook */
Word16 y[], /* (o) Q12 :Filtered innovative codebook */
Word16 *sign /* (o) :Signs of 4 pulses */
)
{
Word16 i, index, sharp;
Word16 Dn[L_SUBFR];
Word16 rr[DIM_RR];
/*-----------------------------------------------------------------*
* Include fixed-gain pitch contribution into impulse resp. h[] *
* Find correlations of h[] needed for the codebook search. *
*-----------------------------------------------------------------*/
sharp = shl(pitch_sharp, 1); /* From Q14 to Q15 */
if (sub(T0, L_SUBFR)<0)
for (i = T0; i < L_SUBFR; i++){ /* h[i] += pitch_sharp*h[i-T0] */
h[i] = add(h[i], mult(h[i-T0], sharp));
}
Cor_h_cp(h, rr, G729);
/*-----------------------------------------------------------------*
* Compute correlation of target vector with impulse response. *
*-----------------------------------------------------------------*/
Cor_h_X(h, x, Dn);
/*-----------------------------------------------------------------*
* Find innovative codebook. *
*-----------------------------------------------------------------*/
index = D4i40_17(Dn, rr, h, code, y, sign, i_subfr);
/*-----------------------------------------------------------------*
* Compute innovation vector gain. *
* Include fixed-gain pitch contribution into code[]. *
*-----------------------------------------------------------------*/
if(sub(T0 ,L_SUBFR) <0)
for (i = T0; i < L_SUBFR; i++) { /* code[i] += pitch_sharp*code[i-T0] */
code[i] = add(code[i], mult(code[i-T0], sharp));
}
return index;
}
Word16 ACELP_Codebook64( /* (o) :index of pulses positions */
Word16 x[], /* (i) :Target vector */
Word16 h[], /* (i) Q12 :Impulse response of filters */
Word16 T0, /* (i) :Pitch lag */
Word16 pitch_sharp, /* (i) Q14 :Last quantized pitch gain */
Word16 code[], /* (o) Q13 :Innovative codebook */
Word16 y[], /* (o) Q12 :Filtered innovative codebook */
Word16 *sign /* (o) :Signs of 4 pulses */
)
{
Word16 i, index, sharp;
Word16 Dn[L_SUBFR];
Word16 rr[DIM_RR];
/*-----------------------------------------------------------------*
* Include fixed-gain pitch contribution into impulse resp. h[] *
* Find correlations of h[] needed for the codebook search. *
*-----------------------------------------------------------------*/
sharp = shl(pitch_sharp, 1); /* From Q14 to Q15 */
if (sub(T0, L_SUBFR)<0)
for (i = T0; i < L_SUBFR; i++){ /* h[i] += pitch_sharp*h[i-T0] */
h[i] = add(h[i], mult(h[i-T0], sharp));
}
Cor_h_cp(h, rr, G729D);
/*-----------------------------------------------------------------*
* Compute correlation of target vector with impulse response. *
*-----------------------------------------------------------------*/
Cor_h_X(h, x, Dn);
/*-----------------------------------------------------------------*
* Find innovative codebook. *
*-----------------------------------------------------------------*/
index = D2i40_11(Dn, rr, h, code, y, sign);
/*-----------------------------------------------------------------*
* Compute innovation vector gain. *
* Include fixed-gain pitch contribution into code[]. *
*-----------------------------------------------------------------*/
if(sub(T0 ,L_SUBFR) <0)
for (i = T0; i < L_SUBFR; i++) { /* code[i] += pitch_sharp*code[i-T0] */
code[i] = add(code[i], mult(code[i-T0], sharp));
}
return index;
}
/*--------------------------------------------------------------------------*
* Function Corr_h_X() *
* ~~~~~~~~~~~~~~~~~~~~ *
* Compute correlations of input response h[] with the target vector X[]. *
*--------------------------------------------------------------------------*/
static void Cor_h_X(
Word16 h[], /* (i) Q12 :Impulse response of filters */
Word16 X[], /* (i) :Target vector */
Word16 D[] /* (o) :Correlations between h[] and D[] */
/* Normalized to 13 bits */
)
{
Word16 i, j;
Word32 s, max, L_temp;
Word32 y32[L_SUBFR];
/* first keep the result on 32 bits and find absolute maximum */
max = 0;
for (i = 0; i < L_SUBFR; i++)
{
s = 0;
for (j = i; j < L_SUBFR; j++)
s = L_mac(s, X[j], h[j-i]);
y32[i] = s;
s = L_abs(s);
L_temp =L_sub(s,max);
if(L_temp>0L) {
max = s;
}
}
/* Find the number of right shifts to do on y32[] */
/* so that maximum is on 13 bits */
j = norm_l(max);
if( sub(j,16) > 0) {
j = 16;
}
j = sub(18, j);
for(i=0; i<L_SUBFR; i++) {
D[i] = extract_l( L_shr(y32[i], j) );
}
return;
}
/*--------------------------------------------------------------------------*
* Function Cor_h_cp() *
* ~~~~~~~~~~~~~~~~~ *
* Compute correlations of h[] needed for the codebook search. *
*--------------------------------------------------------------------------*/
static void Cor_h_cp(
Word16 *H, /* (i) Q12 :Impulse response of filters */
Word16 *rr, /* (o) :Correlations of H[] */
Word16 rate /* (i) frame rate */
)
{
Word16 *rri0i0, *rri1i1, *rri2i2, *rri3i3, *rri4i4;
Word16 *rri0i1, *rri0i2, *rri0i3, *rri0i4;
Word16 *rri1i2, *rri1i3, *rri1i4;
Word16 *rri2i3, *rri2i4;
Word16 *p0, *p1, *p2, *p3, *p4;
Word16 *ptr_hd, *ptr_hf, *ptr_h1, *ptr_h2;
Word32 cor;
Word16 i, k, ldec, l_fin_sup, l_fin_inf;
Word16 h[L_SUBFR];
Word32 L_tmp;
Word16 lsym;
/* Scaling h[] for maximum precision */
cor = 0;
for(i=0; i<L_SUBFR; i++)
cor = L_mac(cor, H[i], H[i]);
L_tmp = L_sub(extract_h(cor),32000);
if(L_tmp>0L )
{
for(i=0; i<L_SUBFR; i++) {
h[i] = shr(H[i], 1);}
}
else
{
k = norm_l(cor);
k = shr(k, 1);
for(i=0; i<L_SUBFR; i++) {
h[i] = shl(H[i], k); }
}
/*-----------------------------------------------------------------*
* In case of G729 mode, nine cross correlations has to be *
* calculated, namely the following: *
* *
* rri0i1[], *
* rri0i2[], rri1i2[], *
* rri0i3[], rri1i3[], rri2i3[], *
* rri0i4[], rri1i4[], rri2i4[], *
* *
* In case of G729 on 6.4 kbps mode, three of the above nine cross *
* correlations are not needed for the codebook search, namely *
* rri0i2[], rri0i4[] and rri2i4[]. Two of these three 64-element *
* positions are instead used by two cross correlations needed *
* only by the 6.4 kbps mode (see D2i40_11() for details). *
*-----------------------------------------------------------------*/
/* Init pointers */
rri0i0 = rr;
rri1i1 = rri0i0 + NB_POS;
rri2i2 = rri1i1 + NB_POS;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -