Commit 70aa2868 authored by Sandesh Venkatesh's avatar Sandesh Venkatesh
Browse files

Merge branch 'dec_tcx_noiseshaping_igf_subfuncs_fxd' into 'main'

Remaining decoder-tcx-noiseshaping-igf sub-funcs converted to fxd

See merge request !172
parents d7f4d65f 92007490
Loading
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
@@ -1124,6 +1124,44 @@ void IGFCommonFuncsMDCTSquareSpec(const Word16 sqrtBgn,

}

/**********************************************************************/ /*
calculate the MDCT square spectrum in the IGF range (for IVAS)
**************************************************************************/
void IGFCommonFuncsMDCTSquareSpec_ivas( const Word16 sqrtBgn,            /**< in: Q0  | start MDCT subband index       */
                                        const Word16 sqrtEnd,            /**< in: Q0  | stop  MDCT subband index       */
                                        const Word32 *mdctSpec,          /**< in: Q31 | MDCT spectrum to square        */
                                        const Word16 mdctSpec_e,         /**< in:     | exponent of mdctSpectrum       */
                                        Word32 *mdctSquareSpec,          /**< out:Q31 | MDCT square spectrum           */
                                        Word16 *mdctSquareSpec_e,        /**< out:    | exponent of mdctSquareSpec     */
                                        Word16 indexOffset               /**< in: Q0  | index offset                   */
)
{
    Word16 i;
    Word16 j;
    Word16 s1;
    Word16 tmp;


    /* get headroom, only in IGF range */
    s1 = getScaleFactor32( mdctSpec + sqrtBgn, sub( sqrtEnd, sqrtBgn ) );

    /* set new exponent */
    *mdctSquareSpec_e = add( shl( sub( mdctSpec_e, s1 ), 1 ), 1 );
    move16();

    /* MDCT square spectrum: MDCT^2 */
    j = add( sqrtBgn, indexOffset ); /* handle indexOffset with care, otherwise memory overruns may occur! */


    FOR( i = sqrtBgn; i < sqrtEnd; i++ )
    {
        tmp = extract_h( L_shl( mdctSpec[i], s1 ) );
        mdctSquareSpec[j++] = L_mult0( tmp, tmp );
        move32();
    }
}


/**********************************************************************/ /*
write bits to stream
**************************************************************************/
+1 −1
Original line number Diff line number Diff line
@@ -355,9 +355,9 @@ void decoder_tcx_noiseshaping_igf_fx(
    const Word16 L_frame,
    const Word16 L_frameTCX,
    const Word16 left_rect,
    float x[],
    Word32 *x_fx,
    Word16 *x_e,
    Word16 *x_len,
    const Word16 gainlpc2_fx[],
    const Word16 gainlpc2_e[],
    Word16 *temp_concealment_method,
+20 −0
Original line number Diff line number Diff line
@@ -5169,6 +5169,16 @@ void IGFCommonFuncsMDCTSquareSpec(const Word16 sqrtBgn,
  Word16                             indexOffset         /**< in: Q0  | index offset                   */
);

void IGFCommonFuncsMDCTSquareSpec_ivas(
  const Word16                       sqrtBgn,            /**< in: Q0  | start MDCT subband index       */
  const Word16                       sqrtEnd,            /**< in: Q0  | stop  MDCT subband index       */
  const Word32                      *mdctSpec,           /**< in: Q31 | MDCT spectrum to square        */
  const Word16                       mdctSpec_e,         /**< in:     | exponent of mdctSpectrum       */
  Word32                            *mdctSquareSpec,     /**< out:Q31 | MDCT square spectrum           */
  Word16                            *mdctSquareSpec_e,   /**< out:    | exponent of mdctSquareSpec     */
  Word16                             indexOffset         /**< in: Q0  | index offset                   */
);

void IGFCommonFuncsWriteSerialBit(
  BSTR_ENC_HANDLE hBstr,         /* i/o: encoder bitstream handle       */
  Word16* pBitOffset,         /**< out: Q0 | bit offset                       */
@@ -7977,6 +7987,16 @@ void IGFDecApplyMono(const IGF_DEC_INSTANCE_HANDLE hInstance,
  Word16                                          bfi                 /**< in:     | frame loss == 1, frame good == 0                     */

);

void IGFDecApplyMono_ivas(
    const IGF_DEC_INSTANCE_HANDLE                   hInstance,          /**< in:     | instance handle of IGF Decoder                       */
    Word32                                         *spectrum,           /**< in/out: | MDCT spectrum                                        */
    Word16                                         *spectrum_e,         /**< in/out: | exponent of spectrum                                 */
    const Word16                                    igfGridIdx,         /**< in:     | in case of CELP->TCX switching, use 1.25 framelength */
    Word16                                          bfi,                /**< in:     | frame loss == 1, frame good == 0                     */
    Word16                                          element_mode        /**< in:     | IVAS element mode                                    */
);

void IGFDecSetMode(
  const IGF_DEC_INSTANCE_HANDLE hIGFDec, /* o  : instance handle of IGF Decoder      */
  const Word32 total_brate,             /* i  : bitrate                             */
+31 −0
Original line number Diff line number Diff line
@@ -2524,7 +2524,38 @@ void decoder_tcx_IGF_mono(
            igfGridIdx = ( st->last_core == ACELP_CORE || ( left_rect && bfi ) ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM;
        }

#ifdef IVAS_FLOAT_FIXED
        Word16 len;
        Word32 x_fx[N_MAX];
        Word16 x_e;
        len = ((igfGridIdx == IGF_GRID_LB_SHORT) ? (N_MAX_TCX - IGF_START_MN) / 2 : (N_MAX_TCX - IGF_START_MN));
        IGF_GRID *hGrid = &st->hIGFDec->igfData.igfInfo.grid[igfGridIdx];
        Word16 stop = hGrid->stopLine;

        // Float to fixed
        f2me_buf(x, x_fx, &x_e, stop);
        hGrid->fFactor = float_to_fix16(hGrid->fFactor_flt, Q14);
        hGrid->lFactor = float_to_fix16(hGrid->lFactor_flt, Q14);
        hGrid->gFactor = float_to_fix16(hGrid->gFactor_flt, Q14);
        // u8bit to 16bit
        FOR(int l = 0; l < IGF_START_MX; l++)
        {
            st->hIGFDec->infoTCXNoise_evs[l] = (Word16)st->hIGFDec->infoTCXNoise[l];
        }

        IGFDecApplyMono_ivas( st->hIGFDec, x_fx, &x_e, igfGridIdx, bfi, st->element_mode );

        // Fixed to float
        me2f_buf(x_fx, x_e, x, stop);
        me2f_buf(st->hIGFDec->virtualSpec, st->hIGFDec->virtualSpec_e, st->hIGFDec->virtualSpec_float, len);
        // 16bit to u8bit
        FOR(Word16 l = 0; l < IGF_START_MX; l++)
        {
            st->hIGFDec->infoTCXNoise[l] = (uint8_t)st->hIGFDec->infoTCXNoise_evs[l];
        }
#else
        IGFDecApplyMono_flt( st->hIGFDec, x, igfGridIdx, bfi, st->element_mode );
#endif
    }

    return;
+137 −48
Original line number Diff line number Diff line
@@ -2635,7 +2635,58 @@ void decoder_tcx_fx(
#endif

#ifdef IVAS_FLOAT_FIXED
    decoder_tcx_noiseshaping_igf_fx( st, L_spec, L_frame, L_frameTCX, left_rect, &x[0], x_fx, &x_e, gainlpc2_fx, gainlpc2_e, &tmp_concealment_method, bfi );
    // Float to fix starts here
    Word16 x_len;
    IF(bfi && st->tonal_mdct_plc_active && NE_16(st->element_mode, IVAS_CPE_MDCT))
    {
        FOR(Word16 i = 0; i < st->hTonalMDCTConc->pTCI_float->numIndexes; i++)
        {
            float pd = st->hTonalMDCTConc->pTCI_float->phaseDiff_float[i];
            if (pd >= PI2)
                pd = fmodf(pd, PI2) - PI2;
            st->hTonalMDCTConc->pTCI_fix->phaseDiff[i] = float_to_fix16(pd, Q12);
        }
        FOR(Word16 i = 0; i < MAX_NUMBER_OF_IDX * GROUP_LENGTH; i++)
        {
            float pd = st->hTonalMDCTConc->pTCI_float->phase_currentFramePredicted_float[i];
            pd = fmodf(pd, PI2);
            st->hTonalMDCTConc->pTCI_fix->phase_currentFramePredicted[i] = (Word16)(pd * (1u << Q13));
        }
        FOR(Word16 i = 0; i < FDNS_NPTS; i++)
        {
            f2me_16(st->hTonalMDCTConc->secondLastBlockData.scaleFactors_float[i], &st->hTonalMDCTConc->secondLastBlockData.scaleFactors[i], &st->hTonalMDCTConc->secondLastBlockData.scaleFactors_exp[i]);
            st->hTonalMDCTConc->secondLastBlockData.scaleFactors_max_e = s_max(st->hTonalMDCTConc->secondLastBlockData.scaleFactors_max_e, st->hTonalMDCTConc->secondLastBlockData.scaleFactors_exp[i]);
        }
    }
    st->hTcxDec->tcxltp_last_gain_unmodified = float_to_fix16(st->hTcxDec->tcxltp_last_gain_unmodified_float, Q15);
    st->old_fpitch = float_to_fix(st->old_fpitch_float, Q16);
    st->hTonalMDCTConc->lastPitchLag = float_to_fix(st->hTonalMDCTConc->lastPitchLag_float, Q16);

    IF(st->enablePlcWaveadjust)
    {
        f2me_buf(st->hPlcInfo->data_reci2, st->hPlcInfo->data_reci2_fx, &st->hPlcInfo->data_reci2_scale, st->hPlcInfo->L_frameTCX);
    }

    // u8bit to 16bit
    FOR(int l = 0; l < IGF_START_MX; l++)
    {
        st->hIGFDec->infoTCXNoise_evs[l] = (Word16)st->hIGFDec->infoTCXNoise[l];
    }
    // Float to fix ends here

    decoder_tcx_noiseshaping_igf_fx( st, L_spec, L_frame, L_frameTCX, left_rect, x_fx, &x_e, &x_len, gainlpc2_fx, gainlpc2_e, &tmp_concealment_method, bfi );

    // Fixed to float starts here
    st->hTonalMDCTConc->lastPitchLag_float = fix_to_float(st->hTonalMDCTConc->lastPitchLag, Q16);
    st->hTonalMDCTConc->nFramesLost_float = fix16_to_float(st->hTonalMDCTConc->nFramesLost, Q1);
    // 16bit to u8bit
    FOR(Word16 l = 0; l < IGF_START_MX; l++)
    {
        st->hIGFDec->infoTCXNoise[l] = (uint8_t)st->hIGFDec->infoTCXNoise_evs[l];
    }
    me2f_buf(st->hIGFDec->virtualSpec, st->hIGFDec->virtualSpec_e, st->hIGFDec->virtualSpec_float, (N_MAX_TCX - IGF_START_MN));
    me2f_buf(x_fx, x_e, x, x_len);
    // Fixed to float ends here
#else
    decoder_tcx_noiseshaping_igf( st, L_spec, L_frame, L_frameTCX, left_rect, &x[0], &gainlpc2[0], &tmp_concealment_method, bfi );
#endif
@@ -3591,9 +3642,9 @@ void decoder_tcx_noiseshaping_igf_fx(
    const Word16 L_frame,
    const Word16 L_frameTCX,
    const Word16 left_rect,
    float x[],
    Word32 *x_fx,
    Word16 *x_e,
    Word16 *x_len,
    const Word16 gainlpc2_fx[],
    const Word16 gainlpc2_e[],
    Word16 *temp_concealment_method,
@@ -3602,6 +3653,8 @@ void decoder_tcx_noiseshaping_igf_fx(
{
    TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
    Word16 i;
    Word32 tmp32;
    Word8 tmp8;

    /*-----------------------------------------------------------*
     * Noise shaping in frequency domain (1/Wz)                  *
@@ -3643,6 +3696,7 @@ void decoder_tcx_noiseshaping_igf_fx(
        spec_side_x_e = *x_e;
        move16();

        /* NOTE: this function updates x till L_frame. From L_frame to L_spec, the exponent needs to be updated. */
        mdct_noiseShaping_ivas_fx(x_fx, x_e, L_frame, gainlpc2_fx, gainlpc2_e);

        frame_side_x_e = *x_e;
@@ -3683,88 +3737,123 @@ void decoder_tcx_noiseshaping_igf_fx(
        set32_fx(x_fx + L_spec, 0, sub(L_frameTCX, L_spec));

    }
    me2f_buf(x_fx, *x_e, x, L_frameTCX);

    Word16 prev_x_e, curr_x_e;
    prev_x_e = *x_e;
    move16();

    /* PLC: [TCX: Tonal Concealment] */
    test();
    test();
    IF ( bfi && st->tonal_mdct_plc_active && NE_16(st->element_mode, IVAS_CPE_MDCT) )
    {
#if 1
        // Float to fixed
        IF(!st->hTonalMDCTConc->lastBlockData.blockIsConcealed && st->hTonalMDCTConc->secondLastBlockData.tonalConcealmentActive != 0)
        TonalMDCTConceal_Apply_ivas_fx( st->hTonalMDCTConc, x_fx, x_e, st->hTcxCfg->psychParamsCurrent );

        /* If exponent has been updated after TonalMDCTConceal_Apply, then shift the spectrum to common exponent. */
        curr_x_e = *x_e;
        move16();
        IF(NE_16(curr_x_e, prev_x_e))
        {
            st->hTonalMDCTConc->nFramesLost = float_to_fix16(st->hTonalMDCTConc->nFramesLost_float, Q1);
        }
        FOR (i = 0; i < st->hTonalMDCTConc->pTCI_float->numIndexes; i++)
            *x_e = s_max(curr_x_e, prev_x_e);
            FOR(i = 0; i < L_frameTCX; i++)
            {
            float pd = st->hTonalMDCTConc->pTCI_float->phaseDiff_float[i];
            if (pd >= PI2)
                pd = fmodf(pd, PI2) - PI2;
            st->hTonalMDCTConc->pTCI_fix->phaseDiff[i] = float_to_fix16(pd, Q12);
        }
        FOR (i = 0; i < MAX_NUMBER_OF_IDX * GROUP_LENGTH; i++)
                Word16 n_idx = st->hTonalMDCTConc->pTCI_float->numIndexes;
                Word16 l_idx = st->hTonalMDCTConc->pTCI_float->lowerIndex[0];
                Word16 u_idx = st->hTonalMDCTConc->pTCI_float->upperIndex[n_idx - 1];
                test();
                IF(GE_16(i, l_idx) && LE_16(i, u_idx))
                {
            float pd = st->hTonalMDCTConc->pTCI_float->phase_currentFramePredicted_float[i];
            pd = fmodf(pd, PI2);
            st->hTonalMDCTConc->pTCI_fix->phase_currentFramePredicted[i] = (Word16)(pd * (1u << Q13));
                    x_fx[i] = L_shr(x_fx[i], sub(*x_e, curr_x_e));
                }
        FOR(i = 0; i < FDNS_NPTS; i++)
                ELSE
                {
            f2me_16(st->hTonalMDCTConc->secondLastBlockData.scaleFactors_float[i], &st->hTonalMDCTConc->secondLastBlockData.scaleFactors[i], &st->hTonalMDCTConc->secondLastBlockData.scaleFactors_exp[i]);
            st->hTonalMDCTConc->secondLastBlockData.scaleFactors_max_e = s_max(st->hTonalMDCTConc->secondLastBlockData.scaleFactors_max_e, st->hTonalMDCTConc->secondLastBlockData.scaleFactors_exp[i]);
                    x_fx[i] = L_shr(x_fx[i], sub(*x_e, prev_x_e));
                }
            }
        }
    }

        TonalMDCTConceal_Apply_ivas_fx( st->hTonalMDCTConc, x_fx, x_e, st->hTcxCfg->psychParamsCurrent );

        // Fix to float
        FOR(i = 0; i < st->hTonalMDCTConc->pTCI_float->numIndexes; i++)
    test();
    IF ( st->hTonalMDCTConc != NULL && NE_16(st->element_mode, IVAS_CPE_MDCT) )
    {
            FOR(Word16 l = st->hTonalMDCTConc->pTCI_float->lowerIndex[i]; l <= st->hTonalMDCTConc->pTCI_float->upperIndex[i]; l++)
        tmp32 = L_deposit_h(0);
        IF(GT_16(hTcxDec->tcxltp_last_gain_unmodified, 0))
        {
                x[l] = me2f(x_fx[l], *x_e);
            }
        }
        st->hTonalMDCTConc->nFramesLost_float = fix16_to_float(st->hTonalMDCTConc->nFramesLost, Q1);
#else
        TonalMDCTConceal_Apply_ivas( st->hTonalMDCTConc, x, st->hTcxCfg->psychParamsCurrent );
#endif
            tmp32 = st->old_fpitch;
            move32();
        }

    if ( st->hTonalMDCTConc != NULL && st->element_mode != IVAS_CPE_MDCT )
        tmp8 = 0;
        move16();
        test();
        IF (bfi && st->tonal_mdct_plc_active)
        {
        TonalMDCTConceal_UpdateState_ivas( st->hTonalMDCTConc, L_frameTCX, ( hTcxDec->tcxltp_last_gain_unmodified_float > 0 ) ? st->old_fpitch_float : 0, bfi, bfi && st->tonal_mdct_plc_active );
            tmp8 = 1;
            move16();
        }

        TonalMDCTConceal_UpdateState( st->hTonalMDCTConc,
            L_frameTCX,
            tmp32,
            bfi,
            tmp8 );
    }

    if ( st->enablePlcWaveadjust )
    *x_len = L_frameTCX;
    move16();

    IF ( st->enablePlcWaveadjust != 0 )
    {
        test();
        /* spectrum concealment */
        if ( bfi && *temp_concealment_method == TCX_NONTONAL )
        IF ( bfi && EQ_16(*temp_concealment_method, TCX_NONTONAL) )
        {
            concealment_decode( st->core, x, st->hPlcInfo );
            concealment_decode_fix( st->core, x_fx, x_e, &st->hPlcInfo );
        }

        /* update spectrum buffer, tonality flag, etc. */
        concealment_update( bfi, st->core, st->tonality_flag, x, st->hPlcInfo );
        concealment_update_x( bfi, st->core, st->tonality_flag, x_fx, x_e, &st->hPlcInfo );

        *x_len = s_max(*x_len, st->hPlcInfo->L_frameTCX);
    }

    /*-----------------------------------------------------------*
     * IGF                                                       *
     *-----------------------------------------------------------*/

    if ( st->igf && !( ( L_frame == st->L_frame >> 1 ) && st->tcxonly ) )
    test();
    test();
    IF(st->igf && !((EQ_16(L_frame, shr(st->L_frame, 1))) && (st->tcxonly)))
    {
        if ( st->element_mode != IVAS_CPE_MDCT )
        IF ( NE_16(st->element_mode, IVAS_CPE_MDCT) )
        {
            IGFDecApplyMono_flt( st->hIGFDec, x, ( st->last_core == ACELP_CORE || ( left_rect && bfi ) ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM, bfi, st->element_mode );
            Word16 igfGridIdx;

            test();
            test();
            IF((EQ_16(st->last_core, ACELP_CORE)) || (left_rect&&bfi))
            {
                /* packet loss after first TCX must be handled like transition frame */
                igfGridIdx = IGF_GRID_LB_TRAN;
            }
            ELSE
            {
                igfGridIdx = IGF_GRID_LB_NORM;
            }

            IGFDecApplyMono_ivas(st->hIGFDec, x_fx, x_e, igfGridIdx, bfi, st->element_mode);

            *x_len = s_max(*x_len, st->hIGFDec->igfData.igfInfo.grid[igfGridIdx].stopLine);
        }
    }

    if ( st->igf && ( ( L_frame == st->L_frame >> 1 ) && st->tcxonly ) )
    IF(st->igf && ((EQ_16(L_frame, shr(st->L_frame, 1))) && (st->tcxonly)))
    {
        if ( st->element_mode != IVAS_CPE_MDCT )
        IF(NE_16(st->element_mode, IVAS_CPE_MDCT))
        {
            IGFDecApplyMono_flt( st->hIGFDec, x, IGF_GRID_LB_SHORT, bfi, st->element_mode );
            IGFDecApplyMono_ivas(st->hIGFDec, x_fx, x_e, IGF_GRID_LB_SHORT, bfi, st->element_mode);

            *x_len = s_max(*x_len, st->hIGFDec->igfData.igfInfo.grid[IGF_GRID_LB_SHORT].stopLine);
        }
    }

Loading