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

📄 dec_main.c

📁 通讯协议
💻 C
📖 第 1 页 / 共 3 页
字号:
/*------------------------------------------------------------------------*
 *                         DEC_MAIN.C                                     *
 *------------------------------------------------------------------------*
 * Performs the main decoder 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 "cnst.h"
#include "acelp.h"
#include "dec_main.h"
#include "bits.h"
#include "count.h"
#include "math_op.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};

/* 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
};

/* 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
};

static void 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     */
     Word16 prms,                          /* (i)   : parameter                  */
     Word16 HfIsf[],
     Word16 nb_bits,
     Word16 newDTXState,
     Decoder_State * st,                   /* (i/o) : State structure            */
     Word16 bfi                            /* (i)   : bad frame indicator        */
);

/*-----------------------------------------------------------------*
 *   Funtion  init_decoder	                                       *
 *            ~~~~~~~~~~~~                                         *
 *   ->Initialization of variables for the decoder section.        *
 *-----------------------------------------------------------------*/

void Init_decoder(void **spd_state)
{
    /* Decoder states */
    Decoder_State *st;

    *spd_state = NULL;

    /*-------------------------------------------------------------------------*
     * Memory allocation for coder state.                                      *
     *-------------------------------------------------------------------------*/

    test();
    if ((st = (Decoder_State *) malloc(sizeof(Decoder_State))) == NULL)
    {
        printf("Can not malloc Decoder_State structure!\n");
        return;
    }
    st->dtx_decSt = NULL;
    dtx_dec_init(&st->dtx_decSt, isf_init);

    Reset_decoder((void *) st, 1);

    *spd_state = (void *) st;

    return;
}

void Reset_decoder(void *st, Word16 reset_all)
{
    Word16 i;

    Decoder_State *dec_state;

    dec_state = (Decoder_State *) st;

    Set_zero(dec_state->old_exc, PIT_MAX + L_INTERPOL);
    Set_zero(dec_state->past_isfq, M);

    dec_state->old_T0_frac = 0;            move16();  /* old pitch value = 64.0 */
    dec_state->old_T0 = 64;                move16();
    dec_state->first_frame = 1;            move16();
    dec_state->L_gc_thres = 0;             move16();
    dec_state->tilt_code = 0;              move16();

    Init_Phase_dispersion(dec_state->disp_mem);

    /* scaling memories for excitation */
    dec_state->Q_old = Q_MAX;              move16();
    dec_state->Qsubfr[3] = Q_MAX;          move16();
    dec_state->Qsubfr[2] = Q_MAX;          move16();
    dec_state->Qsubfr[1] = Q_MAX;          move16();
    dec_state->Qsubfr[0] = Q_MAX;          move16();

    if (reset_all != 0)
    {
        /* routines initialization */

        Init_D_gain2(dec_state->dec_gain);
        Init_Oversamp_16k(dec_state->mem_oversamp);
        Init_HP50_12k8(dec_state->mem_sig_out);
        Init_Filt_6k_7k(dec_state->mem_hf);
        Init_Filt_7k(dec_state->mem_hf3);
        Init_HP400_12k8(dec_state->mem_hp400);
        Init_Lagconc(dec_state->lag_hist);

        /* isp initialization */

        Copy(isp_init, dec_state->ispold, M);
        Copy(isf_init, dec_state->isfold, M);
        for (i = 0; i < L_MEANBUF; i++)
            Copy(isf_init, &dec_state->isf_buf[i * M], M);
        /* variable initialization */

        dec_state->mem_deemph = 0;         move16();

        dec_state->seed = 21845;           move16();  /* init random with 21845 */
        dec_state->seed2 = 21845;          move16();
        dec_state->seed3 = 21845;          move16();

        dec_state->state = 0;              move16();
        dec_state->prev_bfi = 0;           move16();

        /* Static vectors to zero */

        Set_zero(dec_state->mem_syn_hf, M16k);
        Set_zero(dec_state->mem_syn_hi, M);
        Set_zero(dec_state->mem_syn_lo, M);

        dtx_dec_reset(dec_state->dtx_decSt, isf_init);
        dec_state->vad_hist = 0;           move16();

    }
    return;
}

void Close_decoder(void *spd_state)
{
    dtx_dec_exit(&(((Decoder_State *) spd_state)->dtx_decSt));
    free(spd_state);
    return;
}

/*-----------------------------------------------------------------*
 *   Funtion decoder		                                       *
 *           ~~~~~~~	                                           *
 *   ->Main decoder routine.                                       *
 *                                                                 *
 *-----------------------------------------------------------------*/

void decoder(
     Word16 mode,                          /* input : used mode                     */
     Word16 prms[],                        /* input : parameter vector              */
     Word16 synth16k[],                    /* output: synthesis speech              */
     Word16 * frame_length,                /* output:  lenght of the frame          */
     void *spd_state,                      /* i/o   : State structure               */
     Word16 frame_type                     /* input : received frame type           */
)
{

    /* Decoder states */
    Decoder_State *st;

    /* Excitation vector */
    Word16 old_exc[(L_FRAME + 1) + PIT_MAX + L_INTERPOL];
    Word16 *exc;

    /* LPC coefficients */

    Word16 *p_Aq;                          /* ptr to A(z) for the 4 subframes      */
    Word16 Aq[NB_SUBFR * (M + 1)];         /* A(z)   quantized for the 4 subframes */
    Word16 ispnew[M];                      /* immittance spectral pairs at 4nd sfr */
    Word16 isf[M];                         /* ISF (frequency domain) at 4nd sfr    */
    Word16 code[L_SUBFR];                  /* algebraic codevector                 */
    Word16 code2[L_SUBFR];                 /* algebraic codevector                 */
    Word16 exc2[L_FRAME];                  /* excitation vector                    */

    Word16 fac, stab_fac, voice_fac, Q_new = 0;
    Word32 L_tmp, L_gain_code;

    /* Scalars */

    Word16 i, j, i_subfr, index, ind[8], max, tmp;
    Word16 T0, T0_frac, pit_flag, T0_max, select, T0_min = 0;
    Word16 gain_pit, gain_code, gain_code_lo;
    Word16 newDTXState, bfi, unusable_frame, nb_bits;
    Word16 vad_flag;
    Word16 pit_sharp;
    Word16 excp[L_SUBFR];
    Word16 isf_tmp[M];
    Word16 HfIsf[M16k];

    Word16 corr_gain = 0;

    st = (Decoder_State *) spd_state;

    /* mode verification */

    nb_bits = nb_of_bits[mode];            move16();

    *frame_length = L_FRAME16k;            move16();

    /* find the new  DTX state  SPEECH OR DTX */
    newDTXState = rx_dtx_handler(st->dtx_decSt, frame_type);

    test();
    if (sub(newDTXState, SPEECH) != 0)
    {
        dtx_dec(st->dtx_decSt, exc2, newDTXState, isf, &prms);
    }
    /* SPEECH action state machine  */
    test();test();
    if ((sub(frame_type, RX_SPEECH_BAD) == 0) ||
        (sub(frame_type, RX_SPEECH_PROBABLY_DEGRADED) == 0))
    {
        /* bfi for all index, bits are not usable */
        bfi = 1;                           move16();
        unusable_frame = 0;                move16();
    } else if ((sub(frame_type, RX_NO_DATA) == 0) ||
    		   (sub(frame_type, RX_SPEECH_LOST) == 0))
    {
        /* bfi only for lsf, gains and pitch period */
        bfi = 1;                           move16();
        unusable_frame = 1;                move16();
    } else
    {
        bfi = 0;                           move16();
        unusable_frame = 0;                move16();
    }
    test();
    if (bfi != 0)
    {
        st->state = add(st->state, 1);     move16();
        test();
        if (sub(st->state, 6) > 0)
        {
            st->state = 6;                 move16();
        }
    } else
    {
        st->state = shr(st->state, 1);     move16();
    }

    /* If this frame is the first speech frame after CNI period,     */
    /* set the BFH state machine to an appropriate state depending   */
    /* on whether there was DTX muting before start of speech or not */
    /* If there was DTX muting, the first speech frame is muted.     */
    /* If there was no DTX muting, the first speech frame is not     */
    /* muted. The BFH state machine starts from state 5, however, to */
    /* keep the audible noise resulting from a SID frame which is    */
    /* erroneously interpreted as a good speech frame as small as    */
    /* possible (the decoder output in this case is quickly muted)   */
    test();test();
    if (sub(st->dtx_decSt->dtxGlobalState, DTX) == 0)
    {
        st->state = 5;                     move16();
        st->prev_bfi = 0;                  move16();
    } else if (sub(st->dtx_decSt->dtxGlobalState, DTX_MUTE) == 0)
    {
        st->state = 5;                     move16();
        st->prev_bfi = 1;                  move16();
    }
    test();
    if (sub(newDTXState, SPEECH) == 0)
    {
        vad_flag = Serial_parm(1, &prms);
        test();
        if (bfi == 0)
        {
            test();
            if (vad_flag == 0)
            {
                st->vad_hist = add(st->vad_hist, 1);    move16();
            } else
            {
                st->vad_hist = 0;          move16();
            }
        }
    }
    /*----------------------------------------------------------------------*
     *                              DTX-CNG                                 *
     *----------------------------------------------------------------------*/
    test();
    if (sub(newDTXState, SPEECH) != 0)     /* CNG mode */
    {
        /* increase slightly energy of noise below 200 Hz */

        /* Convert ISFs to the cosine domain */
        Isf_isp(isf, ispnew, M);

        Isp_Az(ispnew, Aq, M, 1);

        Copy(st->isfold, isf_tmp, M);

        for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
        {
            j = shr(i_subfr, 6);
            for (i = 0; i < M; i++)
            {
                L_tmp = L_mult(isf_tmp[i], sub(32767, interpol_frac[j]));
                L_tmp = L_mac(L_tmp, isf[i], interpol_frac[j]);
                HfIsf[i] = round(L_tmp);   move16();
            }
            synthesis(Aq, &exc2[i_subfr], 0, &synth16k[i_subfr * 5 / 4], (short) 1, HfIsf, nb_bits, newDTXState, st, bfi);
        }

        /* reset speech coder memories */
        Reset_decoder(st, 0);

        Copy(isf, st->isfold, M);

        st->prev_bfi = bfi;                move16();
        st->dtx_decSt->dtxGlobalState = newDTXState;    move16();

        return;
    }
    /*----------------------------------------------------------------------*
     *                               ACELP                                  *
     *----------------------------------------------------------------------*/

    /* copy coder memory state into working space (internal memory for DSP) */

    Copy(st->old_exc, old_exc, PIT_MAX + L_INTERPOL);
    exc = old_exc + PIT_MAX + L_INTERPOL;  move16();

    /* Decode the ISFs */
    test();
    if (sub(nb_bits, NBBITS_7k) <= 0)
    {
        ind[0] = Serial_parm(8, &prms);    move16();
        ind[1] = Serial_parm(8, &prms);    move16();
        ind[2] = Serial_parm(7, &prms);    move16();
        ind[3] = Serial_parm(7, &prms);    move16();
        ind[4] = Serial_parm(6, &prms);    move16();

        Dpisf_2s_36b(ind, isf, st->past_isfq, st->isfold, st->isf_buf, bfi, 1);
    } else
    {
        ind[0] = Serial_parm(8, &prms);    move16();
        ind[1] = Serial_parm(8, &prms);    move16();
        ind[2] = Serial_parm(6, &prms);    move16();
        ind[3] = Serial_parm(7, &prms);    move16();
        ind[4] = Serial_parm(7, &prms);    move16();
        ind[5] = Serial_parm(5, &prms);    move16();
        ind[6] = Serial_parm(5, &prms);    move16();

        Dpisf_2s_46b(ind, isf, st->past_isfq, st->isfold, st->isf_buf, bfi, 1);
    }

    /* Convert ISFs to the cosine domain */

⌨️ 快捷键说明

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