📄 cod_ld8c.c
字号:
/* ITU-T G.729 Annex C+ - Reference C code for floating point implementation of G.729 Annex C+ (integration of Annexes B, D and E) Version 2.1 of October 1999*//* File : CODLD8CP.C*//*-----------------------------------------------------------------* * Functions coder_ld8c and init_coder_ld8c * * ~~~~~~~~~~ ~~~~~~~~~~~~~~~ * *-----------------------------------------------------------------*/#include <math.h>#include "typedef.h"#include "cst_ld8c.h"#include "tab_ld8k.h" /*-----------------------------------------------------------* * Coder constant parameters (defined in "ld8k.h") * *-----------------------------------------------------------* * L_WINDOW : LPC analysis window size. * * L_NEXT : Samples of next frame needed for autocor. * * L_FRAME : Frame size. * * L_SUBFR : Sub-frame size. * * M : LPC order. * * MP1 : LPC order+1 * * L_TOTAL : Total size of speech buffer. * * PIT_MIN : Minimum pitch lag. * * PIT_MAX : Maximum pitch lag. * * L_INTERPOL : Length of filter for interpolation * *-----------------------------------------------------------*/#define AVG(a,b,c,d) (int)(((a)+(b)+(c)+(d))/((F)4.0)+(F)0.5)/*---------------------------------------------------------------------------- * init_coder_ld8c - initialization of variables for the encoder *---------------------------------------------------------------------------- */void init_coder_ld8c( struct cod_state_t *state, int dtx_enable /* input : DTX enable flag */){/*-----------------------------------------------------------------------** Initialize pointers to speech vector. ** ** ** |--------------------|-------------|-------------|------------| ** previous speech sf1 sf2 L_NEXT ** ** <---------------- Total speech vector (L_TOTAL) -----------> ** | <------------ LPC analysis window (L_WINDOW) -----------> ** | | <-- present frame (L_FRAME) --> ** old_speech | <-- new speech (L_FRAME) --> ** p_wind | | ** speech | ** new_speech **-----------------------------------------------------------------------*/ state->new_speech = state->old_speech + L_TOTAL - L_FRAME; /* New speech */ state->speech = state->new_speech - L_NEXT; /* Present frame */ state->p_window = state->old_speech + L_TOTAL - L_WINDOW; /* For LPC window */ /* Initialize static pointers */ state->wsp = state->old_wsp + PIT_MAX; state->exc = state->old_exc + PIT_MAX + L_INTERPOL; state->zero = state->ai_zero + MP1; state->error = state->mem_err + M; /* Static vectors to zero */ set_zero(state->old_speech, L_TOTAL); set_zero(state->old_exc, PIT_MAX+L_INTERPOL); set_zero(state->old_wsp, PIT_MAX); set_zero(state->mem_syn, M); set_zero(state->mem_w, M); set_zero(state->mem_w0, M); set_zero(state->mem_err, M); set_zero(state->zero, L_SUBFR); state->sharp = SHARPMIN; /* Initialize lsp_old_q[] */ copy(lsp_old_reset, state->lsp_old, M); copy(lsp_old_reset, state->lsp_old_q, M); lsp_codw_reset(&state->lsp_s); init_exc_err(state->cng_s.exc_err); /* For G.729B */ /* Initialize VAD/DTX parameters */ if (dtx_enable) { state->pastVad = 1; state->ppastVad = 1; state->seed = INIT_SEED; vad_init(&state->vad_s); init_lsfq_noise(&state->cng_s.lsfq_s); } state->d4i40_17_extra = 0; gain_past_reset(&state->gain_s); //init_bwd_dominant(&state->bwd_s); //init_musdetect(&state->mus_s); init_pwf(&state->pwf_s);}/*---------------------------------------------------------------------------- * coder_ld8c - encoder routine ( speech data should be in new_speech ) *---------------------------------------------------------------------------- */void coder_ld8c( struct cod_state_t *state, int ana[], /* output: analysis parameters */ int frame, /* input : frame counter */ int vad_enable /* input : DTX enable flag */){ /* LPC coefficients */ FLOAT r[NP+1]; /* Autocorrelations low and hi */ FLOAT A_t[(MP1)*2]; /* A(z) unquantized for the 2 subframes */ FLOAT Aq_t[(MP1)*2]; /* A(z) quantized for the 2 subframes */ FLOAT Ap1[MP1]; /* A(z) with spectral expansion */ FLOAT Ap2[MP1]; /* A(z) with spectral expansion */ FLOAT *A, *Aq; /* Pointer on A_t and Aq_t */ /* LSP coefficients */ FLOAT lsp_new[M], lsp_new_q[M]; /* LSPs at 2th subframe */ FLOAT lsf_int[M]; /* Interpolated LSF 1st subframe. */ FLOAT lsf_new[M]; /* Variable added for adaptive gamma1 and gamma2 of the PWF */ FLOAT rc[M]; /* Reflection coefficients */ FLOAT gamma1[2]; /* Gamma1 for 1st and 2nd subframes */ FLOAT gamma2[2]; /* Gamma2 for 1st and 2nd subframes */ /* Other vectors */ FLOAT synth[L_FRAME]; /* Buffer for synthesis speech */ FLOAT h1[L_SUBFR]; /* Impulse response h1[] */ FLOAT xn[L_SUBFR]; /* Target vector for pitch search */ FLOAT xn2[L_SUBFR]; /* Target vector for codebook search */ FLOAT code[L_SUBFR]; /* Fixed codebook excitation */ FLOAT y1[L_SUBFR]; /* Filtered adaptive excitation */ FLOAT y2[L_SUBFR]; /* Filtered fixed codebook excitation */ FLOAT g_coeff[5]; /* Correlations between xn, y1, & y2: <y1,y1>, <xn,y1>, <y2,y2>, <xn,y2>,<y1,y2>*/ FLOAT freq_cur[M]; /* Scalars */ int i, j, i_gamma, i_subfr; int T_op, t0, t0_min, t0_max, t0_frac; int index, taming; FLOAT gain_pit, gain_code; /* For G.729B */ FLOAT r_nbe[MP1]; FLOAT lsfq_mem[MA_NP][M]; int Vad; FLOAT Energy_db;/*------------------------------------------------------------------------* * - Perform LPC analysis: * * * autocorrelation + lag windowing * * * Levinson-durbin algorithm to find a[] * * * convert a[] to lsp[] * * * quantize and code the LSPs * * * find the interpolated LSPs and convert to a[] for the 2 * * subframes (both quantized and unquantized) * *------------------------------------------------------------------------*/ /* LP analysis */ autocorr(state->p_window, NP, r); /* Autocorrelations */ copy(r, r_nbe, MP1); lag_window(NP, r); /* Lag windowing */ levinson(r, &A_t[MP1], rc); /* Levinson Durbin */ az_lsp(&A_t[MP1], lsp_new, state->lsp_old); /* From A(z) to lsp */ /* For G.729B */ /* ------ VAD ------- */ if (vad_enable) { lsp_lsf(lsp_new, lsf_new, M); vad(&state->vad_s, rc[1], lsf_new, r, state->p_window, frame, state->pastVad, state->ppastVad, &Vad, &Energy_db); update_cng(&state->cng_s, r_nbe, Vad); } else Vad = 1; /*--------------------------------------------------------------------* * Find interpolated LPC parameters in all subframes (unquantized) * * The interpolated parameters are in array A_t[] of size (M+1)*4 * *--------------------------------------------------------------------*/ int_lpc(state->lsp_old, lsp_new, lsf_int, lsf_new, A_t); copy(lsp_new, state->lsp_old, M); /*----------------------------------------------------------------------* * - Find the weighting factors * *----------------------------------------------------------------------*/ perc_var(&state->pwf_s, gamma1, gamma2, lsf_int, lsf_new, rc); /*----------------------------------------------------------------------* * - Find the weighted input speech w_sp[] for the whole speech frame * * - Find the open-loop pitch delay for the whole speech frame * * - Set the range for searching closed-loop pitch in 1st subframe * *----------------------------------------------------------------------*/ weight_az(&A_t[0], gamma1[0], M, Ap1); weight_az(&A_t[0], gamma2[0], M, Ap2); residue(M, Ap1, &state->speech[0], &state->wsp[0], L_SUBFR); syn_filte(M, Ap2, &state->wsp[0], &state->wsp[0], L_SUBFR, state->mem_w, 1); weight_az(&A_t[MP1], gamma1[1], M, Ap1); weight_az(&A_t[MP1], gamma2[1], M, Ap2); residue(M, Ap1, &state->speech[L_SUBFR], &state->wsp[L_SUBFR], L_SUBFR); syn_filte(M, Ap2, &state->wsp[L_SUBFR], &state->wsp[L_SUBFR], L_SUBFR, state->mem_w, 1); /* ---------------------- */ /* Case of Inactive frame */ /* ---------------------- */ if (!Vad) { get_freq_prev((const FLOAT (*)[M])state->lsp_s.freq_prev, lsfq_mem); cod_cng(&state->cng_s, state->exc, state->pastVad, state->lsp_old_q, Aq_t, ana, lsfq_mem, &state->seed); update_freq_prev(state->lsp_s.freq_prev, (const FLOAT (*)[M])lsfq_mem); state->ppastVad = state->pastVad; state->pastVad = Vad; /* UPDATE wsp, mem_w, mem_syn, mem_err, and mem_w0 */ A = A_t; /* pointer to interpolated LPC parameters */ Aq = Aq_t; /* pointer to interpolated quantized LPC parameters */ i_gamma = 0; for (i_subfr=0; i_subfr < L_FRAME; i_subfr += L_SUBFR) { weight_az(A, gamma1[i_gamma], M, Ap1); weight_az(A, gamma2[i_gamma], M, Ap2); i_gamma++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -