📄 cod_main.c
字号:
/*------------------------------------------------------------------------*
* COD_MAIN.C *
*------------------------------------------------------------------------*
* Performs the main encoder routine *
*------------------------------------------------------------------------*/
/*___________________________________________________________________________
| |
| Fixed-point C simulation of AMR WB ACELP coding algorithm with 20 ms |
| speech frames for wideband speech signals. |
|___________________________________________________________________________|
*/
#include <stdio.h>
#include <stdlib.h>
#include "typedef.h"
#include "basic_op.h"
#include "oper_32b.h"
#include "math_op.h"
#include "cnst.h"
#include "acelp.h"
#include "cod_main.h"
#include "bits.h"
#include "count.h"
#include "main.h"
/* LPC interpolation coef {0.45, 0.8, 0.96, 1.0}; in Q15 */
static Word16 interpol_frac[NB_SUBFR] = {14746, 26214, 31457, 32767};
/* isp tables for initialization */
static Word16 isp_init[M] =
{
32138, 30274, 27246, 23170, 18205, 12540, 6393, 0,
-6393, -12540, -18205, -23170, -27246, -30274, -32138, 1475
};
static Word16 isf_init[M] =
{
1024, 2048, 3072, 4096, 5120, 6144, 7168, 8192,
9216, 10240, 11264, 12288, 13312, 14336, 15360, 3840
};
/* High Band encoding */
static const Word16 HP_gain[16] =
{
3624, 4673, 5597, 6479, 7425, 8378, 9324, 10264,
11210, 12206, 13391, 14844, 16770, 19655, 24289, 32728
};
static Word16 synthesis(
Word16 Aq[], /* A(z) : quantized Az */
Word16 exc[], /* (i) : excitation at 12kHz */
Word16 Q_new, /* (i) : scaling performed on exc */
Word16 synth16k[], /* (o) : 16kHz synthesis signal */
Coder_State * st /* (i/o) : State structure */
);
/*-----------------------------------------------------------------*
* Funtion init_coder *
* ~~~~~~~~~~ *
* ->Initialization of variables for the coder section. *
*-----------------------------------------------------------------*/
void Init_coder(void **spe_state)
{
Coder_State *st;
*spe_state = NULL;
/*-------------------------------------------------------------------------*
* Memory allocation for coder state. *
*-------------------------------------------------------------------------*/
if ((st = (Coder_State *) malloc(sizeof(Coder_State))) == NULL)
{
printf("Can not malloc Coder_State structure!\n");
return;
}
st->vadSt = NULL; move16();
st->dtx_encSt = NULL; move16();
wb_vad_init(&(st->vadSt));
dtx_enc_init(&(st->dtx_encSt), isf_init);
Reset_encoder((void *) st, 1);
*spe_state = (void *) st;
return;
}
void Reset_encoder(void *st, Word16 reset_all)
{
Word16 i;
Coder_State *cod_state;
cod_state = (Coder_State *) st;
Set_zero(cod_state->old_exc, PIT_MAX + L_INTERPOL);
Set_zero(cod_state->mem_syn, M);
Set_zero(cod_state->past_isfq, M);
cod_state->mem_w0 = 0; move16();
cod_state->tilt_code = 0; move16();
cod_state->first_frame = 1; move16();
Init_gp_clip(cod_state->gp_clip);
cod_state->L_gc_thres = 0; move16();
if (reset_all != 0)
{
/* Static vectors to zero */
Set_zero(cod_state->old_speech, L_TOTAL - L_FRAME);
Set_zero(cod_state->old_wsp, (PIT_MAX / OPL_DECIM));
Set_zero(cod_state->mem_decim2, 3);
/* routines initialization */
Init_Decim_12k8(cod_state->mem_decim);
Init_HP50_12k8(cod_state->mem_sig_in);
Init_Levinson(cod_state->mem_levinson);
Init_Q_gain2(cod_state->qua_gain);
Init_Hp_wsp(cod_state->hp_wsp_mem);
/* isp initialization */
Copy(isp_init, cod_state->ispold, M);
Copy(isp_init, cod_state->ispold_q, M);
/* variable initialization */
cod_state->mem_preemph = 0; move16();
cod_state->mem_wsp = 0; move16();
cod_state->Q_old = 15; move16();
cod_state->Q_max[0] = 15; move16();
cod_state->Q_max[1] = 15; move16();
cod_state->old_wsp_max = 0; move16();
cod_state->old_wsp_shift = 0; move16();
/* pitch ol initialization */
cod_state->old_T0_med = 40; move16();
cod_state->ol_gain = 0; move16();
cod_state->ada_w = 0; move16();
cod_state->ol_wght_flg = 0; move16();
for (i = 0; i < 5; i++)
{
cod_state->old_ol_lag[i] = 40; move16();
}
Set_zero(cod_state->old_hp_wsp, (L_FRAME / 2) / OPL_DECIM + (PIT_MAX / OPL_DECIM));
Set_zero(cod_state->mem_syn_hf, M);
Set_zero(cod_state->mem_syn_hi, M);
Set_zero(cod_state->mem_syn_lo, M);
Init_HP50_12k8(cod_state->mem_sig_out);
Init_Filt_6k_7k(cod_state->mem_hf);
Init_HP400_12k8(cod_state->mem_hp400);
Copy(isf_init, cod_state->isfold, M);
cod_state->mem_deemph = 0; move16();
cod_state->seed2 = 21845; move16();
Init_Filt_6k_7k(cod_state->mem_hf2);
cod_state->gain_alpha = 32767; move16();
cod_state->vad_hist = 0;
wb_vad_reset(cod_state->vadSt);
dtx_enc_reset(cod_state->dtx_encSt, isf_init);
}
return;
}
void Close_coder(void *spe_state)
{
wb_vad_exit(&(((Coder_State *) spe_state)->vadSt));
dtx_enc_exit(&(((Coder_State *) spe_state)->dtx_encSt));
free(spe_state);
return;
}
/*-----------------------------------------------------------------*
* Funtion coder *
* ~~~~~ *
* ->Main coder routine. *
* *
*-----------------------------------------------------------------*/
void coder(
Word16 * mode, /* input : used mode */
Word16 speech16k[], /* input : 320 new speech samples (at 16 kHz) */
Word16 prms[], /* output: output parameters */
Word16 * ser_size, /* output: bit rate of the used mode */
void *spe_state, /* i/o : State structure */
Word16 allow_dtx /* input : DTX ON/OFF */
)
{
/* Coder states */
Coder_State *st;
/* Speech vector */
Word16 old_speech[L_TOTAL];
Word16 *new_speech, *speech, *p_window;
/* Weighted speech vector */
Word16 old_wsp[L_FRAME + (PIT_MAX / OPL_DECIM)];
Word16 *wsp;
/* Excitation vector */
Word16 old_exc[(L_FRAME + 1) + PIT_MAX + L_INTERPOL];
Word16 *exc;
/* LPC coefficients */
Word16 r_h[M + 1], r_l[M + 1]; /* Autocorrelations of windowed speech */
Word16 rc[M]; /* Reflection coefficients. */
Word16 Ap[M + 1]; /* A(z) with spectral expansion */
Word16 ispnew[M]; /* immittance spectral pairs at 4nd sfr */
Word16 ispnew_q[M]; /* quantized ISPs at 4nd subframe */
Word16 isf[M]; /* ISF (frequency domain) at 4nd sfr */
Word16 *p_A, *p_Aq; /* ptr to A(z) for the 4 subframes */
Word16 A[NB_SUBFR * (M + 1)]; /* A(z) unquantized for the 4 subframes */
Word16 Aq[NB_SUBFR * (M + 1)]; /* A(z) quantized for the 4 subframes */
/* Other vectors */
Word16 xn[L_SUBFR]; /* Target vector for pitch search */
Word16 xn2[L_SUBFR]; /* Target vector for codebook search */
Word16 dn[L_SUBFR]; /* Correlation between xn2 and h1 */
Word16 cn[L_SUBFR]; /* Target vector in residual domain */
Word16 h1[L_SUBFR]; /* Impulse response vector */
Word16 h2[L_SUBFR]; /* Impulse response vector */
Word16 code[L_SUBFR]; /* Fixed codebook excitation */
Word16 y1[L_SUBFR]; /* Filtered adaptive excitation */
Word16 y2[L_SUBFR]; /* Filtered adaptive excitation */
Word16 error[M + L_SUBFR]; /* error of quantization */
Word16 synth[L_SUBFR]; /* 12.8kHz synthesis vector */
Word16 exc2[L_FRAME]; /* excitation vector */
Word16 buf[L_FRAME]; /* VAD buffer */
/* Scalars */
Word16 i, j, i_subfr, select, pit_flag, clip_gain, vad_flag;
Word16 codec_mode;
Word16 T_op, T_op2, T0, T0_min, T0_max, T0_frac, index;
Word16 gain_pit, gain_code, g_coeff[4], g_coeff2[4];
Word16 tmp, gain1, gain2, exp, Q_new, mu, shift, max;
Word16 voice_fac;
Word16 indice[8];
Word32 L_tmp, L_gain_code, L_max;
Word16 code2[L_SUBFR]; /* Fixed codebook excitation */
Word16 stab_fac, fac, gain_code_lo;
Word16 corr_gain;
st = (Coder_State *) spe_state;
*ser_size = nb_of_bits[*mode]; move16();
codec_mode = *mode; move16();
/*--------------------------------------------------------------------------*
* Initialize pointers to speech vector. *
* *
* *
* |-------|-------|-------|-------|-------|-------| *
* past sp sf1 sf2 sf3 sf4 L_NEXT *
* <------- Total speech buffer (L_TOTAL) ------> *
* old_speech *
* <------- LPC analysis window (L_WINDOW) ------> *
* | <-- present frame (L_FRAME) ----> *
* p_window | <----- new speech (L_FRAME) ----> *
* | | *
* speech | *
* new_speech *
*--------------------------------------------------------------------------*/
new_speech = old_speech + L_TOTAL - L_FRAME - L_FILT; move16(); /* New speech */
speech = old_speech + L_TOTAL - L_FRAME - L_NEXT; move16(); /* Present frame */
p_window = old_speech + L_TOTAL - L_WINDOW; move16();
exc = old_exc + PIT_MAX + L_INTERPOL; move16();
wsp = old_wsp + (PIT_MAX / OPL_DECIM); move16();
/* copy coder memory state into working space (internal memory for DSP) */
Copy(st->old_speech, old_speech, L_TOTAL - L_FRAME);
Copy(st->old_wsp, old_wsp, PIT_MAX / OPL_DECIM);
Copy(st->old_exc, old_exc, PIT_MAX + L_INTERPOL);
/*---------------------------------------------------------------*
* Down sampling signal from 16kHz to 12.8kHz *
* -> The signal is extended by L_FILT samples (padded to zero) *
* to avoid additional delay (L_FILT samples) in the coder. *
* The last L_FILT samples are approximated after decimation and *
* are used (and windowed) only in autocorrelations. *
*---------------------------------------------------------------*/
Decim_12k8(speech16k, L_FRAME16k, new_speech, st->mem_decim);
/* last L_FILT samples for autocorrelation window */
Copy(st->mem_decim, code, 2 * L_FILT16k);
Set_zero(error, L_FILT16k); /* set next sample to zero */
Decim_12k8(error, L_FILT16k, new_speech + L_FRAME, code);
/*---------------------------------------------------------------*
* Perform 50Hz HP filtering of input signal. *
*---------------------------------------------------------------*/
HP50_12k8(new_speech, L_FRAME, st->mem_sig_in);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -