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

📄 cod_main.c

📁 通讯协议
💻 C
📖 第 1 页 / 共 5 页
字号:
                    index = add(sub(add(shl(T0, 1), shr(T0_frac, 1)), (PIT_FR2 * 2)), ((PIT_FR2 - PIT_MIN) * 4));
                } else
                {
                    index = add(add(sub(T0, PIT_FR1_9b), ((PIT_FR2 - PIT_MIN) * 4)), ((PIT_FR1_9b - PIT_FR2) * 2));
                }

                Parm_serial(index, 9, &prms);

                /* find T0_min and T0_max for subframe 2 and 4 */

                T0_min = sub(T0, 8);
                test();
                if (sub(T0_min, PIT_MIN) < 0)
                {
                    T0_min = PIT_MIN;      move16();
                }
                T0_max = add(T0_min, 15);
                test();
                if (sub(T0_max, PIT_MAX) > 0)
                {
                    T0_max = PIT_MAX;      move16();
                    T0_min = sub(T0_max, 15);
                }
            } else
            {                              /* if subframe 2 or 4 */
                /*--------------------------------------------------------------*
                 * The pitch range for subframe 2 or 4 is encoded with 6 bits:  *
                 *   T0_min  to T0_max     resolution 1/4 (frac = 0,1,2 or 3)   *
                 *--------------------------------------------------------------*/

                i = sub(T0, T0_min);
                index = add(shl(i, 2), T0_frac);

                Parm_serial(index, 6, &prms);
            }
        }

        /*-----------------------------------------------------------------*
         * Gain clipping test to avoid unstable synthesis on frame erasure *
         *-----------------------------------------------------------------*/

        clip_gain = Gp_clip(st->gp_clip);

        /*-----------------------------------------------------------------*
         * - find unity gain pitch excitation (adaptive codebook entry)    *
         *   with fractional interpolation.                                *
         * - find filtered pitch exc. y1[]=exc[] convolved with h1[])      *
         * - compute pitch gain1                                           *
         *-----------------------------------------------------------------*/

        /* find pitch exitation */

        Pred_lt4(&exc[i_subfr], T0, T0_frac, L_SUBFR + 1);
        test();
        if (sub(*ser_size, NBBITS_9k) > 0)
        {
            Convolve(&exc[i_subfr], h1, y1, L_SUBFR);
            gain1 = G_pitch(xn, y1, g_coeff, L_SUBFR);

            /* clip gain if necessary to avoid problem at decoder */
            test();test();
            if ((clip_gain != 0) && (sub(gain1, GP_CLIP) > 0))
            {
                gain1 = GP_CLIP;           move16();
            }
            /* find energy of new target xn2[] */
            Updt_tar(xn, dn, y1, gain1, L_SUBFR);       /* dn used temporary */
        } else
        {
            gain1 = 0;                     move16();
        }

        /*-----------------------------------------------------------------*
         * - find pitch excitation filtered by 1st order LP filter.        *
         * - find filtered pitch exc. y2[]=exc[] convolved with h1[])      *
         * - compute pitch gain2                                           *
         *-----------------------------------------------------------------*/

        /* find pitch excitation with lp filter */
        for (i = 0; i < L_SUBFR; i++)
        {
            L_tmp = L_mult(5898, exc[i - 1 + i_subfr]);
            L_tmp = L_mac(L_tmp, 20972, exc[i + i_subfr]);
            L_tmp = L_mac(L_tmp, 5898, exc[i + 1 + i_subfr]);
            code[i] = round(L_tmp);        move16();
        }

        Convolve(code, h1, y2, L_SUBFR);
        gain2 = G_pitch(xn, y2, g_coeff2, L_SUBFR);

        /* clip gain if necessary to avoid problem at decoder */
        test();test();
        if ((clip_gain != 0) && (sub(gain2, GP_CLIP) > 0))
        {
            gain2 = GP_CLIP;               move16();
        }
        /* find energy of new target xn2[] */
        Updt_tar(xn, xn2, y2, gain2, L_SUBFR);

        /*-----------------------------------------------------------------*
         * use the best prediction (minimise quadratic error).             *
         *-----------------------------------------------------------------*/

        select = 0;                        move16();
        test();
        if (sub(*ser_size, NBBITS_9k) > 0)
        {
            L_tmp = 0L;                    move32();
            for (i = 0; i < L_SUBFR; i++)
                L_tmp = L_mac(L_tmp, dn[i], dn[i]);
            for (i = 0; i < L_SUBFR; i++)
                L_tmp = L_msu(L_tmp, xn2[i], xn2[i]);

            test();
            if (L_tmp <= 0)
            {
                select = 1;                move16();
            }
            Parm_serial(select, 1, &prms);
        }
        test();
        if (select == 0)
        {
            /* use the lp filter for pitch excitation prediction */
            gain_pit = gain2;              move16();
            Copy(code, &exc[i_subfr], L_SUBFR);
            Copy(y2, y1, L_SUBFR);
            Copy(g_coeff2, g_coeff, 4);
        } else
        {
            /* no filter used for pitch excitation prediction */
            gain_pit = gain1;              move16();
            Copy(dn, xn2, L_SUBFR);        /* target vector for codebook search */
        }

        /*-----------------------------------------------------------------*
         * - update cn[] for codebook search                               *
         *-----------------------------------------------------------------*/

        Updt_tar(cn, cn, &exc[i_subfr], gain_pit, L_SUBFR);

        Scale_sig(cn, L_SUBFR, shift);     /* scaling of cn[] to limit dynamic at 12 bits */

        /*-----------------------------------------------------------------*
         * - include fixed-gain pitch contribution into impulse resp. h1[] *
         *-----------------------------------------------------------------*/

        tmp = 0;                           move16();
        Preemph(h2, st->tilt_code, L_SUBFR, &tmp);

        test();
        if (T0_frac > 2)
            T0 = add(T0, 1);
        Pit_shrp(h2, T0, PIT_SHARP, L_SUBFR);

        /*-----------------------------------------------------------------*
         * - Correlation between target xn2[] and impulse response h1[]    *
         * - Innovative codebook search                                    *
         *-----------------------------------------------------------------*/

        cor_h_x(h2, xn2, dn);

        test();test();test();test();test();test();test();
        if (sub(*ser_size, NBBITS_7k) <= 0)
        {
            ACELP_2t64_fx(dn, cn, h2, code, y2, indice);

            Parm_serial(indice[0], 12, &prms);
        } else if (sub(*ser_size, NBBITS_9k) <= 0)
        {
            ACELP_4t64_fx(dn, cn, h2, code, y2, 20, *ser_size, indice);

            Parm_serial(indice[0], 5, &prms);
            Parm_serial(indice[1], 5, &prms);
            Parm_serial(indice[2], 5, &prms);
            Parm_serial(indice[3], 5, &prms);
        } else if (sub(*ser_size, NBBITS_12k) <= 0)
        {
            ACELP_4t64_fx(dn, cn, h2, code, y2, 36, *ser_size, indice);

            Parm_serial(indice[0], 9, &prms);
            Parm_serial(indice[1], 9, &prms);
            Parm_serial(indice[2], 9, &prms);
            Parm_serial(indice[3], 9, &prms);
        } else if (sub(*ser_size, NBBITS_14k) <= 0)
        {
            ACELP_4t64_fx(dn, cn, h2, code, y2, 44, *ser_size, indice);

            Parm_serial(indice[0], 13, &prms);
            Parm_serial(indice[1], 13, &prms);
            Parm_serial(indice[2], 9, &prms);
            Parm_serial(indice[3], 9, &prms);
        } else if (sub(*ser_size, NBBITS_16k) <= 0)
        {
            ACELP_4t64_fx(dn, cn, h2, code, y2, 52, *ser_size, indice);

            Parm_serial(indice[0], 13, &prms);
            Parm_serial(indice[1], 13, &prms);
            Parm_serial(indice[2], 13, &prms);
            Parm_serial(indice[3], 13, &prms);
        } else if (sub(*ser_size, NBBITS_18k) <= 0)
        {
            ACELP_4t64_fx(dn, cn, h2, code, y2, 64, *ser_size, indice);

            Parm_serial(indice[0], 2, &prms);
            Parm_serial(indice[1], 2, &prms);
            Parm_serial(indice[2], 2, &prms);
            Parm_serial(indice[3], 2, &prms);
            Parm_serial(indice[4], 14, &prms);
            Parm_serial(indice[5], 14, &prms);
            Parm_serial(indice[6], 14, &prms);
            Parm_serial(indice[7], 14, &prms);
        } else if (sub(*ser_size, NBBITS_20k) <= 0)
        {
            ACELP_4t64_fx(dn, cn, h2, code, y2, 72, *ser_size, indice);

            Parm_serial(indice[0], 10, &prms);
            Parm_serial(indice[1], 10, &prms);
            Parm_serial(indice[2], 2, &prms);
            Parm_serial(indice[3], 2, &prms);
            Parm_serial(indice[4], 10, &prms);
            Parm_serial(indice[5], 10, &prms);
            Parm_serial(indice[6], 14, &prms);
            Parm_serial(indice[7], 14, &prms);
        } else
        {
            ACELP_4t64_fx(dn, cn, h2, code, y2, 88, *ser_size, indice);

            Parm_serial(indice[0], 11, &prms);
            Parm_serial(indice[1], 11, &prms);
            Parm_serial(indice[2], 11, &prms);
            Parm_serial(indice[3], 11, &prms);
            Parm_serial(indice[4], 11, &prms);
            Parm_serial(indice[5], 11, &prms);
            Parm_serial(indice[6], 11, &prms);
            Parm_serial(indice[7], 11, &prms);
        }

        /*-------------------------------------------------------*
         * - Add the fixed-gain pitch contribution to code[].    *
         *-------------------------------------------------------*/

        tmp = 0;                           move16();
        Preemph(code, st->tilt_code, L_SUBFR, &tmp);

        Pit_shrp(code, T0, PIT_SHARP, L_SUBFR);

        /*----------------------------------------------------------*
         *  - Compute the fixed codebook gain                       *
         *  - quantize fixed codebook gain                          *
         *----------------------------------------------------------*/

        test();
        if (sub(*ser_size, NBBITS_9k) <= 0)
        {
            index = Q_gain2(xn, y1, add(Q_new, shift), y2, code, g_coeff, L_SUBFR, 6,
                &gain_pit, &L_gain_code, clip_gain, st->qua_gain);
            Parm_serial(index, 6, &prms);
        } else
        {
            index = Q_gain2(xn, y1, add(Q_new, shift), y2, code, g_coeff, L_SUBFR, 7,
                &gain_pit, &L_gain_code, clip_gain, st->qua_gain);
            Parm_serial(index, 7, &prms);
        }

        /* test quantized gain of pitch for pitch clipping algorithm */
        Gp_clip_test_gain_pit(gain_pit, st->gp_clip);

        L_tmp = L_shl(L_gain_code, Q_new); /* saturation can occur here */
        gain_code = round(L_tmp);          /* scaled gain_code with Qnew */

        /*----------------------------------------------------------*
         * Update parameters for the next subframe.                 *
         * - tilt of code: 0.0 (unvoiced) to 0.5 (voiced)           *
         *----------------------------------------------------------*/

        /* find voice factor in Q15 (1=voiced, -1=unvoiced) */

        Copy(&exc[i_subfr], exc2, L_SUBFR);
        Scale_sig(exc2, L_SUBFR, shift);

        voice_fac = voice_factor(exc2, shift, gain_pit, code, gain_code, L_SUBFR);

        /* tilt of code for next subframe: 0.5=voiced, 0=unvoiced */

        st->tilt_code = add(shr(voice_fac, 2), 8192);   move16();

        /*------------------------------------------------------*
         * - Update filter's memory "mem_w0" for finding the    *
         *   target vector in the next subframe.                *
         * - Find the total excitation                          *
         * - Find synthesis speech to update mem_syn[].         *
         *------------------------------------------------------*/

        /* y2 in Q9, gain_pit in Q14 */
        L_tmp = L_mult(gain_code, y2[L_SUBFR - 1]);
        L_tmp = L_shl(L_tmp, add(5, shift));
        L_tmp = L_negate(L_tmp);
        L_tmp = L_mac(L_tmp, xn[L_SUBFR - 1], 16384);
        L_tmp = L_msu(L_tmp, y1[L_SUBFR - 1], gain_pit);
        L_tmp = L_shl(L_tmp, sub(1, shift));
        st->mem_w0 = round(L_tmp);         move16();

        if (sub(*ser_size, NBBITS_24k) >= 0)
            Copy(&exc[i_subfr], exc2, L_SUBFR);

        for (i = 0; i < L_SUBFR; i++)
        {
            /* code in Q9, gain_pit in Q14 */
            L_tmp = L_mult(gain_code, code[i]);
            L_tmp = L_shl(L_tmp, 5);
            L_tmp = L_mac(L_tmp, exc[i + i_subfr], gain_pit);
            L_tmp = L_shl(L_tmp, 1);       /* saturation can occur here */
            exc[i + i_subfr] = round(L_tmp);    move16();
        }

        Syn_filt(p_Aq, M, &exc[i_subfr], synth, L_SUBFR, st->mem_syn, 1);

        if (sub(*ser_size, NBBITS_24k) >= 0)
        {
            /*------------------------------------------------------------*
             * phase dispersion to enhance noise in low bit rate          *
             *------------------------------------------------------------*/

            /* L_gain_code in Q16 */

⌨️ 快捷键说明

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