Commit e1b2b4fb authored by vaillancour's avatar vaillancour
Browse files

cleanup in pitch_ol

parent cf7f073c
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -109,6 +109,7 @@
#define FIX_2391_INIT_HQ_GENERIC_OFFSET                 /* FhG/Eri: basop issue 2391: make sure hq_generic_offset is initialized inside hq_hr_dec_fx() */
#define FIX_2397_COPY_AQ_MDCT_CORE_BFI                  /* FhG: prevent copying of uninit memory in MDCT stereo core if bfi is set */
#define HARMONIZE_TBE                                   /* VA: harmonize core-coder TBE function duplications */
#define FIX_2403_COMBINE_PITCH_OL                     /* VA : basop 2403, reusing common code between EVS and IVAS in pitch_ol */

/* #################### End BE switches ################################## */

+341 −6
Original line number Diff line number Diff line
@@ -57,7 +57,9 @@ static Word16 pitch_coherence_fx( Word16 pitch0, Word16 pitch1, Word16 fac_max,
static Word32 Dot_product12_OL( Word16 *sum1, const Word16 x[], const Word16 y[], const Word16 lg, const Word16 lg2, Word16 *exp, Word16 *exp2 );

static Word32 Dot_product12_OL_back( Word16 *sum1, const Word16 x[], const Word16 y[], const Word16 lg, const Word16 lg2, Word16 *exp, Word16 *exp2 );

#ifdef FIX_2403_COMBINE_PITCH_OL
static void pitch_ol_resolve_fx( Word16 pitch[3], Word16 voicing[3], Word16 *old_pitch, Word16 *old_corr, Word16 *old_thres, Word16 *delta_pit, const Word16 element_mode, const Word16 corr_shift, const Word16 relE, const Word16 Opt_SC_VBR, Word16 pitchX[NHFR][2 * NSECT], Word16 corX[NHFR][2 * NSECT], Word16 scaledX[NHFR][2 * NSECT], const Word16 sect0, const Word16 *pit_max, const Word16 pit_min_coding );
#endif
    /*-----------------------------------------------------------------*
 * pitch_ol_init()
 *
@@ -79,7 +81,303 @@ void pitch_ol_init_fx(
    *old_corr = 0;
    move16();
}
#ifdef FIX_2403_COMBINE_PITCH_OL
static void pitch_ol_resolve_fx(
    Word16 pitch[3],                 /* o  : open loop pitch lag for each half-frame Q0                     */
    Word16 voicing[3],               /* o  : maximum normalized correlation for each half-frame Q15         */
    Word16 *old_pitch,               /* i/o: pitch of the 2nd half-frame of previous frame Q0               */
    Word16 *old_corr,                /* i/o: correlation of old_pitch Q15                                   */
    Word16 *old_thres,               /* i/o: maximum correlation weighting with respect to past Q15 or Q14) */
    Word16 *delta_pit,               /* i/o: old pitch extrapolation correction Q0                          */
    const Word16 element_mode,       /* i  : if element_mode != EVS old_thres is in Q14                     */
    const Word16 corr_shift,         /* i  : normalized correlation correction Q15                          */
    const Word16 relE,               /* i  : relative frame energy Q8                                       */
    const Word16 Opt_SC_VBR,         /* i  : SC-VBR flag                                                    */
    Word16 pitchX[NHFR][2 * NSECT],  /* i  : pitch candidates Q0                                            */
    Word16 corX[NHFR][2 * NSECT],    /* i  : correlation candidates Q15                                     */
    Word16 scaledX[NHFR][2 * NSECT], /* i/o: scaled correlation candidates Q12                              */
    const Word16 sect0,              /* i  : section offset                                                 */
    const Word16 *pit_max,           /* i  : maximum pitch delays (array)                                   */
    const Word16 pit_min_coding      /* i  : minimum pitch delay for coding                                 */
)
{
    Word16 i, ind, ind1, ind_corX, ind1_corX;
    Word16 fac, ftmp, k, tmp16, diff, cnt;
    Word16 add_sect0, sub_sect0;
    Word16 coh_flag, coh_flag1;
    Word16 cor_mean;
    Word32 Ltmp;

    Word16 pitch_tmp[2 * NHFR];
    Word16 ind_tmp[2 * NHFR];
    Word16 cor_tmp[2 * NHFR];
    Word16 thres1[2 * NHFR];
    Word16 tmp_buf[NHFR + 1];
    /*-----------------------------------------------------------------*
     * Favor a smaller delay if it happens that it has its multiple
     * in the longer-delay sections  (harmonics check)
     *-----------------------------------------------------------------*/

    FOR( i = 0; i < 2; i++ ) /* loop for the 2 half-frames */
    {
        fac = THRES0;
        move16();
        find_mult_fx( &fac, pitchX[i][2], pitchX[i][3], pit_max[7], &scaledX[i][2], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 3rd section */
        find_mult_fx( &fac, pitchX[i][1], pitchX[i][2], pit_max[5], &scaledX[i][1], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 2nd section */
        test();
        IF( ( sect0 == 0 ) && GE_16( shl( pitchX[i][0], 1 ), pit_min_coding ) )
        {
            find_mult_fx( &fac, pitchX[i][0], pitchX[i][1], pit_max[3], &scaledX[i][0], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 2nd section */
        }
        fac = THRES0;
        move16();
        find_mult_fx( &fac, pitchX[i][NSECT + 2], pitchX[i][NSECT + 3], pit_max[7], &scaledX[i][NSECT + 2], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 3rd section */
        find_mult_fx( &fac, pitchX[i][NSECT + 1], pitchX[i][NSECT + 2], pit_max[6], &scaledX[i][NSECT + 1], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 2nd section */
        test();
        IF( ( sect0 == 0 ) && GE_16( shl( pitchX[i][NSECT + 0], 1 ), pit_min_coding ) )
        {
            find_mult_fx( &fac, pitchX[i][NSECT + 0], pitchX[i][NSECT + 1], pit_max[4], &scaledX[i][NSECT + 0], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 2nd section */
        }
    }

    fac = THRES0;
    move16();                                                                                                        /* the look-ahead */
    find_mult_fx( &fac, pitchX[i][2], pitchX[i][3], pit_max[7], &scaledX[i][2], old_pitch, old_corr, 2, 2 );         /* Multiples in 3rd section */
    find_mult_fx( &fac, pitchX[i][1], pitchX[i][2], pit_max[5], &scaledX[i][1], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 2nd section */
    test();
    IF( ( sect0 == 0 ) && GE_16( shl( pitchX[i][0], 1 ), pit_min_coding ) )
    {
        find_mult_fx( &fac, pitchX[i][0], pitchX[i][1], pit_max[3], &scaledX[i][0], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 2nd section */
    }
    fac = THRES0;
    move16();
    find_mult_fx( &fac, pitchX[i][NSECT + 2], pitchX[i][NSECT + 3], pit_max[7], &scaledX[i][NSECT + 2], old_pitch, old_corr, 2, 2 );         /* Multiples in 3rd section */
    find_mult_fx( &fac, pitchX[i][NSECT + 1], pitchX[i][NSECT + 2], pit_max[6], &scaledX[i][NSECT + 1], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 2nd section */
    test();
    IF( ( sect0 == 0 ) && GE_16( shl( pitchX[i][NSECT + 0], 1 ), pit_min_coding ) )
    {
        find_mult_fx( &fac, pitchX[i][NSECT + 0], pitchX[i][NSECT + 1], pit_max[4], &scaledX[i][NSECT + 0], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 2nd section */ /* Multiples in 2nd section */
    }

    /*-----------------------------------------------------------------*
     * Do 1st estimate for pitch values
     * Adjust the normalized correlation using estimated noise level
     * Compute the maximum scaling for the neighbour correlation
     * reinforcement
     *-----------------------------------------------------------------*/
    add_sect0 = add( NSECT, sect0 );
    sub_sect0 = sub( NSECT, sect0 );
    FOR( i = 0; i < NHFR; i++ )
    {
        /* 1st set of pitch candidates */
        ind = add( maximum_fx( scaledX[i] + sect0, sub_sect0, &ftmp ), sect0 );
        ind_tmp[i] = ind;
        move16();
        pitch_tmp[i] = pitchX[i][ind];
        move16();
        cor_tmp[i] = add_sat( corX[i][ind], corr_shift );
        move16();

        /* Higher is the neighbour's correlation, higher is the weighting */
        /* operands are Q15, result is Q15 */
        thres1[i] = mult( THRES1, cor_tmp[i] );
        move16();

        /* 2nd set of pitch candidates */
        ind1 = add( maximum_fx( scaledX[i] + add_sect0, sub_sect0, &ftmp ), add_sect0 );
        ind_tmp[i + NHFR] = ind1;
        move16();
        pitch_tmp[i + NHFR] = pitchX[i][ind1];
        move16();
        cor_tmp[i + NHFR] = add_sat( corX[i][ind1], corr_shift );
        move16();

        /* Higher is the neighbour's correlation, higher is the weighting */
        /* operands are Q15, result is Q15 */
        thres1[i + NHFR] = mult( THRES1, cor_tmp[i + NHFR] );
        move16();
    }
    /*-----------------------------------------------------------------*
     * Take into account previous and next pitch values of the present
     * frame and look-ahead. Choose the pitch lags and normalize
     * correlations for each half-frame & look-ahead
     *-----------------------------------------------------------------*/

    pitch_neighbour_fx( sect0, pitch_tmp, pitchX, cor_tmp, scaledX, thres1, ind_tmp );
    FOR( i = 0; i < NHFR; i++ )
    {
        ind = add( maximum_fx( scaledX[i] + sect0, sub_sect0, &ftmp ), sect0 );
        ind_corX = add( maximum_fx( corX[i] + sect0, sub_sect0, &ftmp ), sect0 );

        ind1 = add( maximum_fx( scaledX[i] + add_sect0, sub_sect0, &ftmp ), add_sect0 );
        ind1_corX = add( maximum_fx( corX[i] + add_sect0, sub_sect0, &ftmp ), add_sect0 );

        if ( GT_16( scaledX[i][ind1], scaledX[i][ind] ) )
        {
            ind = ind1;
            move16();
        }
        test();
        if ( Opt_SC_VBR && GT_16( corX[i][ind1_corX], corX[i][ind_corX] ) )
        {
            ind_corX = ind1_corX;
            move16();
        }
        test();
        test();
        test();
        IF( Opt_SC_VBR && ( LT_16( mult( pitchX[i][ind], 13107 /*0.4 in Q15*/ ), pitchX[i][ind_corX] ) ) &&
            ( GT_16( mult( pitchX[i][ind], 19661 /*0.6 in Q15*/ ), pitchX[i][ind_corX] ) ) &&
            ( GE_16( corX[i][ind_corX], 29491 /*0.9 in Q15*/ ) ) )
        {
            pitch[i] = pitchX[i][ind_corX];
            move16();
            voicing[i] = corX[i][ind_corX];
            move16();
        }
        ELSE
        {
            pitch[i] = pitchX[i][ind];
            move16();
            voicing[i] = corX[i][ind];
            move16();
        }
    }

    /*-----------------------------------------------------------------*
     * Increase the threshold for correlation reinforcement with
     * the past if correlation high and pitch stable
     *-----------------------------------------------------------------*/

    /* all Q15 here */
    /* cor_mean = 0.5f * (voicing[0] + voicing[1]) + corr_shift; */
    Ltmp = L_mult( voicing[0], 16384 /*.5 Q15*/ );
    Ltmp = L_mac( Ltmp, voicing[1], 16384 /*.5 Q15*/ );
    cor_mean = round_fx( L_add( Ltmp, corr_shift ) );

    /* pitch unstable in present frame or from previous frame or normalized correlation too low */
    coh_flag = pitch_coherence_fx( pitch[0], pitch[1], COH_FAC, DELTA_COH );
    move16();
    coh_flag1 = pitch_coherence_fx( pitch[0], *old_pitch, COH_FAC, DELTA_COH );
    move16();

    test();
    test();
    test();
    IF( ( coh_flag == 0 ) || ( coh_flag1 == 0 ) || ( LT_16( cor_mean, CORR_TH0 ) ) || ( LT_16( relE, THR_relE ) ) )
    {
        /* Reset the threshold */
        *old_thres = 0;
        move16();
    }
    ELSE
    {
        /* The threshold increase is directly dependent on normalized correlation */
        /* *old_thres += (0.16f * cor_mean); */
        IF( element_mode != EVS_MONO )
        {
            *old_thres = round_fx( L_mac0( L_deposit_h( *old_thres ), 5243 /* 0.16f in Q15 */, cor_mean ) ); // Q14
        }
        ELSE
        {
            *old_thres = round_fx( L_mac( L_deposit_h( *old_thres ), 5243 /* 0.16f in Q15 */, cor_mean ) ); // Q15
        }
        move16();
    }
    IF( element_mode != EVS_MONO )
    {
        *old_thres = s_min( *old_thres, 11469 /* 0.7f in Q14 */ ); // Q14
    }
    ELSE
    {
        *old_thres = s_min( *old_thres, THRES3 /* 0.7f in Q14 */ ); // Q15
    }
    move16();

    IF( GT_16( voicing[1], voicing[0] ) )
    {
        *old_corr = voicing[1];
        move16();
    }
    ELSE
    {
        *old_corr = cor_mean;
        move16();
    }

    /*-----------------------------------------------------------------*
     * Extrapolate the pitch value for the next frame by estimating
     * the pitch evolution. This value is added to the old_pitch
     * in the next frame and is then used when the normalized
     * correlation is reinforced by the past estimate
     *-----------------------------------------------------------------*/
    tmp_buf[0] = *old_pitch;
    move16();
    FOR( i = 0; i < NHFR; i++ )
    {
        tmp_buf[i + 1] = pitch[i];
        move16();
    }

    *delta_pit = 0;
    move16();
    cnt = 0;
    move16();

    FOR( i = 0; i < NHFR; i++ )
    {
        diff = sub( tmp_buf[i + 1], tmp_buf[i] );
        move16();
        coh_flag = pitch_coherence_fx( tmp_buf[i], tmp_buf[i + 1], COH_FAC, DELTA_COH );

        if ( coh_flag != 0 )
        {
            *delta_pit = add( *delta_pit, diff );
            move16();
        }
        cnt = add( cnt, coh_flag );
    }
    if ( EQ_16( cnt, 2 ) )
    {
        /* *delta_pit /= 2; */
        *delta_pit = shr( *delta_pit, 1 );
        move16();
    }
    IF( EQ_16( cnt, 3 ) )
    {
        k = *delta_pit;
        move16();
        /* *delta_pit /= 3; */
        if ( k < 0 )
        {
            *delta_pit = mult( *delta_pit, -32768 /*-1 Q15*/ );
            move16();
        }
        tmp16 = mult( *delta_pit, 10923 /*1/3 Q15*/ );
        if ( k < 0 )
        {
            tmp16 = mult( tmp16, -32768 /*-1 Q15*/ );
        }
        *delta_pit = tmp16;
        move16();
    }

    /*--------------------------------------------------------------*
     * Update old pitch, upsample pitch,
     *--------------------------------------------------------------*/

    *old_pitch = pitch[1];
    move16();

    FOR( i = 0; i < NHFR; i++ )
    {
        /* compensate decimation */
        pitch[i] = i_mult2( pitch[i], OPL_DECIM );
        move16();
    }
}

#endif

/*==================================================================================*/
/* FUNCTION : pitch_ol_fx()                                       */
@@ -166,12 +464,18 @@ void pitch_ol_fx(
    Word16 *pt_exp1, *pt_exp2, *pt_exp3, *pt_exp4;
    Word16 *pt1, *pt2, *pt3, *pt4, *pt5, *pt6;
    Word16 *pt_cor0, *pt_cor1, *pt_cor2, *pt_cor3, *pt_cor4, *pt_cor5, *pt_cor6;
#ifndef FIX_2403_COMBINE_PITCH_OL
    Word16 thres1[6];
    Word16 diff, cnt, ind, ind1, offset, offset1, offset_la, offset_la1, coh_flag, coh_flag1;
    Word16 ind_corX, ind1_corX;

    Word16 i, j, k, m, pit_min, pit_min1, sect0, subsect0, add_sect0, sub_sect0, old_tmp, old_tmp1, len_x, len_x1;
#else
    Word16 cnt, ind, ind1, offset, offset1, offset_la, offset_la1;
    Word16 i, j, k, m, pit_min, pit_min1, sect0, subsect0, old_tmp, old_tmp1, len_x, len_x1;
#endif
    Word16 len_temp;
#ifndef FIX_2403_COMBINE_PITCH_OL
    Word16 pitchX[NHFR][2 * NSECT], pitch_tmp[2 * NHFR], ind_tmp[2 * NHFR], tmp_buf[NHFR + 1];

    Word16 enr0[NSECT], enr0_exp[NSECT], enr0_1[NSECT], enr0_1_exp[NSECT], enr1, enr1_exp, enr2_exp;
@@ -179,6 +483,14 @@ void pitch_ol_fx(
    Word16 fac, tmp16, tmp16_2;
    Word16 qCorX, qScaledX;
    Word16 scaledX[NHFR][2 * NSECT], corX[NHFR][2 * NSECT], cor_tmp[2 * NHFR], cor_mean;
#else
    Word16 pitchX[NHFR][2 * NSECT];
    Word16 enr0[NSECT], enr0_exp[NSECT], enr0_1[NSECT], enr0_1_exp[NSECT], enr1, enr1_exp, enr2_exp;
    Word32 enr, enr2, Ltmp;
    Word16 tmp16, tmp16_2;
    Word16 qCorX, qScaledX;
    Word16 scaledX[NHFR][2 * NSECT], corX[NHFR][2 * NSECT];
#endif
    const Word16 *len, *len1, *sublen, *sublen1, *pit_max, *sec_length, *sec_length1;

    Word16 pit_min_coding;
@@ -801,7 +1113,9 @@ void pitch_ol_fx(

        } /* FOR j < NSECT */
    }     /* FOR i < NHFR */

#ifdef FIX_2403_COMBINE_PITCH_OL
    pitch_ol_resolve_fx( pitch, voicing, old_pitch, old_corr, old_thres, delta_pit, EVS_MONO, corr_shift, relE, Opt_SC_VBR, pitchX, corX, scaledX, sect0, pit_max, pit_min_coding );
#else
    /*-----------------------------------------------------------------*
     * Favor a smaller delay if it happens that it has its multiple
     * in the longer-delay sections  (harmonics check)
@@ -1050,7 +1364,7 @@ void pitch_ol_fx(
        pitch[i] = i_mult2( pitch[i], OPL_DECIM );
        move16();
    }

#endif
    return;
}

@@ -1072,7 +1386,11 @@ void pitch_ol_ivas_fx(
    Word16 qwsp              /* i  : Q factor for wsp                                               Q0  */
)
{
#ifndef FIX_2403_COMBINE_PITCH_OL
    Word16 ftmp, old_wsp2[( L_WSP - L_INTERPOL ) / OPL_DECIM], *wsp2;
#else
    Word16 old_wsp2[( L_WSP - L_INTERPOL ) / OPL_DECIM], *wsp2;
#endif
    Word16 tmp_mem[3];

    Word16 scale1[2 * DELTA_COH - 1];
@@ -1082,6 +1400,7 @@ void pitch_ol_ivas_fx(
    Word16 *pt1, *pt2, *pt3, *pt4, *pt5, *pt6;
    Word16 *pt_cor0, *pt_cor1, *pt_cor2, *pt_cor3, *pt_cor4;
    Word16 *pt_cor0_exp, *pt_cor1_exp, *pt_cor2_exp, *pt_cor3_exp, *pt_cor4_exp;
#ifndef FIX_2403_COMBINE_PITCH_OL
    Word16 thres1[6];
    Word16 diff, cnt, ind, ind1, offset, offset1, offset_la, offset_la1, coh_flag, coh_flag1;
    Word16 ind_corX, ind1_corX;
@@ -1096,6 +1415,20 @@ void pitch_ol_ivas_fx(
    Word32 Ltmp;
    Word16 fac, tmp16, tmp16_2;
    Word16 scaledX[NHFR][2 * NSECT], corX[NHFR][2 * NSECT], cor_tmp[2 * NHFR], cor_mean;
#else
    Word16 cnt, ind, ind1, offset, offset1, offset_la, offset_la1;
    Word16 i, j, k, m, pit_min, pit_min1, sect0, subsect0, old_tmp, old_tmp1, len_x, len_x1;
    Word16 len_temp;
    Word16 pitchX[NHFR][2 * NSECT];

    Word16 enr0_exp[NSECT], enr0_1_exp[NSECT], enr1_exp;
    Word32 enr0[NSECT], enr0_1[NSECT], enr1;
    Word64 temp, temp1;
    Word32 Ltmp;
    Word16 tmp16, tmp16_2;
    Word16 scaledX[NHFR][2 * NSECT], corX[NHFR][2 * NSECT];
#endif

    const Word16 *len, *len1, *sublen, *sublen1, *pit_max, *sec_length, *sec_length1;

    Word16 pit_min_coding;
@@ -1757,7 +2090,9 @@ void pitch_ol_ivas_fx(

        } /* FOR j < NSECT */
    }     /* FOR i < NHFR */

#ifdef FIX_2403_COMBINE_PITCH_OL
    pitch_ol_resolve_fx( pitch, voicing, old_pitch, old_corr, old_thres, delta_pit, IVAS_SCE, corr_shift, relE, Opt_SC_VBR, pitchX, corX, scaledX, sect0, pit_max, pit_min_coding ); /* any element mode other than EVS_MONO is OK here */
#else
    /*-----------------------------------------------------------------*
     * Favor a smaller delay if it happens that it has its multiple
     * in the longer-delay sections  (harmonics check)
@@ -2007,7 +2342,7 @@ void pitch_ol_ivas_fx(
        pitch[i] = i_mult2( pitch[i], OPL_DECIM );
        move16();
    }

#endif
    return;
}