From 5c841807a9b62dbce162183a7dd4e1e859f6f476 Mon Sep 17 00:00:00 2001 From: Sandesh Venkatesh Date: Thu, 26 Jun 2025 11:45:48 +0530 Subject: [PATCH] Bug fix in front_vad_fx for lp_speech, precision improvements in msvq_enc --- lib_com/basop_util.c | 20 +++ lib_com/basop_util.h | 1 + lib_enc/ivas_front_vad_fx.c | 4 +- lib_enc/lsf_msvq_ma_enc_fx.c | 332 ++++++++++++++++++++++++++++++++++- lib_enc/prot_fx_enc.h | 16 ++ 5 files changed, 367 insertions(+), 6 deletions(-) diff --git a/lib_com/basop_util.c b/lib_com/basop_util.c index 7903d3580..4c7ebea26 100644 --- a/lib_com/basop_util.c +++ b/lib_com/basop_util.c @@ -1916,6 +1916,26 @@ Word16 findIndexOfMinWord32( Word32 *x, const Word16 len ) return indx; } +Word16 findIndexOfMinWord64( Word64 *x, const Word16 len ) +{ + Word16 i, indx; + + + indx = 0; + move16(); + FOR( i = 1; i < len; i++ ) + { + if ( LT_64( x[i], x[indx] ) ) + { + indx = i; + move16(); + } + } + + + return indx; +} + Word16 imult1616( Word16 x, Word16 y ) { diff --git a/lib_com/basop_util.h b/lib_com/basop_util.h index b5c69e1dd..c95bf8100 100644 --- a/lib_com/basop_util.h +++ b/lib_com/basop_util.h @@ -536,6 +536,7 @@ Word16 findIndexOfMinWord16( Word16 *x, const Word16 len ); \return index of min Word32 */ Word16 findIndexOfMinWord32( Word32 *x, const Word16 len ); +Word16 findIndexOfMinWord64( Word64 *x, const Word16 len ); /****************************************************************************/ /*! diff --git a/lib_enc/ivas_front_vad_fx.c b/lib_enc/ivas_front_vad_fx.c index 963e363b2..ec6a918b9 100644 --- a/lib_enc/ivas_front_vad_fx.c +++ b/lib_enc/ivas_front_vad_fx.c @@ -203,8 +203,6 @@ ivas_error front_vad_fx( move16(); FOR( n = 0; n < n_chan; n++ ) { - hFrontVads[n]->lp_speech_fx = shr( hFrontVads[n]->lp_speech_fx, 1 ); - move16(); scale_inp = s_min( sts[n]->q_inp, hCPE->hFrontVad[n]->q_mem_decim ); Scale_sig( sts[n]->input_fx, input_frame, sub( scale_inp, sts[n]->q_inp ) ); @@ -389,7 +387,7 @@ ivas_error front_vad_create_fx( } wb_vad_init_ivas_fx( hFrontVad->hVAD ); - hFrontVad->lp_speech_fx = 11520; // Q9/* Initialize the long-term active speech level in dB */ + hFrontVad->lp_speech_fx = 11520; // 45.0f in Q8 /* Initialize the long-term active speech level in dB */ move16(); hFrontVad->lp_noise_fx = 0; /* Initialize the long-term noise level in dB */ move16(); diff --git a/lib_enc/lsf_msvq_ma_enc_fx.c b/lib_enc/lsf_msvq_ma_enc_fx.c index cf855bd19..6056c0c46 100644 --- a/lib_enc/lsf_msvq_ma_enc_fx.c +++ b/lib_enc/lsf_msvq_ma_enc_fx.c @@ -468,6 +468,33 @@ static void depack_sub_values_fx( Word16 *pTmp, const Word16 *p1, const Word16 * } +static Word64 depack_mul_values_fx64( Word32 *Tmp, const Word16 *w, const Word16 *cbp, const Word16 N ) +{ + Word16 i, val0, val1, val2, val3; + Word64 en; + + en = 0; + move32(); + FOR( i = 0; i < N; i += 4 ) + { + depack_4_values( cbp + i_mult( shr( i, 2 ), 3 ), val0, val1, val2, val3 ) + Tmp[i + 0] = L_mult0( w[i + 0], val0 ); // Q8 * Q2.56 + move16(); + en = W_mac_32_16( en, Tmp[i + 0], val0 ); // Q8 * Q2.56 * 2.56 * Q1 + Tmp[i + 1] = L_mult0( w[i + 1], val1 ); + move16(); + en = W_mac_32_16( en, Tmp[i + 1], val1 ); + Tmp[i + 2] = L_mult0( w[i + 2], val2 ); + move16(); + en = W_mac_32_16( en, Tmp[i + 2], val2 ); + Tmp[i + 3] = L_mult0( w[i + 3], val3 ); + move16(); + en = W_mac_32_16( en, Tmp[i + 3], val3 ); + } + + return en; // Q8 * Q2.56 * 2.56 * Q1 +} + /*--------------------------------------------------------------------------* * msvq_enc_find_p_max_8() * @@ -522,6 +549,54 @@ static Word16 msvq_enc_find_p_max_8_fx( Word32 dist[] ) } +static Word16 msvq_enc_find_p_max_8_fx64( Word64 dist[] ) +{ + Word16 p_max; + + p_max = 0; + move16(); + + BASOP_SATURATE_WARNING_OFF_EVS + if ( GT_64( dist[1], dist[p_max] ) ) + { + p_max = 1; + move16(); + } + if ( GT_64( dist[2], dist[p_max] ) ) + { + p_max = 2; + move16(); + } + if ( GT_64( dist[3], dist[p_max] ) ) + { + p_max = 3; + move16(); + } + if ( GT_64( dist[4], dist[p_max] ) ) + { + p_max = 4; + move16(); + } + if ( GT_64( dist[5], dist[p_max] ) ) + { + p_max = 5; + move16(); + } + if ( GT_64( dist[6], dist[p_max] ) ) + { + p_max = 6; + move16(); + } + if ( GT_64( dist[7], dist[p_max] ) ) + { + p_max = 7; + move16(); + } + BASOP_SATURATE_WARNING_ON_EVS + return p_max; +} + + /*--------------------------------------------------------------------------* * msvq_enc_find_p_max_6() * @@ -565,6 +640,43 @@ static Word16 msvq_enc_find_p_max_6_fx( Word32 dist[] ) return p_max; } +static Word16 msvq_enc_find_p_max_6_fx64( Word64 dist[] ) +{ + Word16 p_max; + + p_max = 0; + move16(); + + BASOP_SATURATE_WARNING_OFF_EVS + if ( GT_64( dist[1], dist[p_max] ) ) + { + p_max = 1; + move16(); + } + if ( GT_64( dist[2], dist[p_max] ) ) + { + p_max = 2; + move16(); + } + if ( GT_64( dist[3], dist[p_max] ) ) + { + p_max = 3; + move16(); + } + if ( GT_64( dist[4], dist[p_max] ) ) + { + p_max = 4; + move16(); + } + if ( GT_64( dist[5], dist[p_max] ) ) + { + p_max = 5; + move16(); + } + BASOP_SATURATE_WARNING_ON_EVS + return p_max; +} + /*--------------------------------------------------------------------------* * msvq_enc_fx() @@ -787,7 +899,222 @@ void msvq_enc_fx( return; } +void msvq_enc_lsf_fx64( + const Word16 *const *cb, /* i : Codebook (indexed cb[*stages][levels][p]) (10Q5 * 1.28) */ + const Word16 dims[], /* i : Dimension of each codebook stage (NULL: full dim.) */ + const Word16 offs[], /* i : Starting dimension of each codebook stage (NULL: 0) */ + const Word16 u[], /* i : Vector to be encoded (prediction and mean removed)(Q14Q1*1.28) */ + const Word16 *levels, /* i : Number of levels in each stage */ + const Word16 maxC, /* i : Tree search size (number of candidates kept from */ + /* one stage to the next == M-best) */ + const Word16 stages, /* i : Number of stages */ + const Word16 w[], /* i : Weights Q8*/ + const Word16 N, /* i : Vector dimension */ + const Word16 maxN, /* i : Codebook dimension */ + Word16 Idx[] /* o : Indices */ +) +{ + Word16 j; + const Word16 *cbp; + Word16 p2i; + Word16 resid_buf[2 * LSFMBEST_MAX * M_MAX], *resid[2]; + Word16 *pTmp, *p1; + Word16 *indices[2], m, s, c, c2, p_max, i; + Word32 Tmp32[M_MAX]; + Word16 idx_buf[2 * LSFMBEST_MAX * MAX_VQ_STAGES_USED], parents[LSFMBEST_MAX]; + Word64 *dist_64[2], en64, tmp64; + Word64 dist_buf_64[2 * LSFMBEST_MAX]; + Word16 ( *func_ptr64 )( Word64 * ); + Word16 N34; + Word16 n, maxn, start; + + /*----------------------------------------------------------------* + * Allocate memory for previous (parent) and current nodes. + * Parent node is indexed [0], current node is indexed [1]. + *----------------------------------------------------------------*/ + indices[0] = idx_buf; + indices[1] = idx_buf + maxC * stages; /*move16();*/ + /*vr_iset(0, idx_buf, 2*stages*maxC);*/ + set16_fx( idx_buf, 0, (Word16) ( 2 * stages * maxC ) ); + + resid[0] = resid_buf; + resid[1] = resid_buf + maxC * N; /*move16();*/ + + dist_64[0] = dist_buf_64; + dist_64[1] = dist_buf_64 + maxC; /*move16();*/ + + /*vr_iset(0, parents, maxC);*/ + set16_fx( parents, 0, maxC ); + + + func_ptr64 = msvq_enc_find_p_max_6_fx64; + move16(); + if ( EQ_16( maxC, 8 ) ) + { + func_ptr64 = msvq_enc_find_p_max_8_fx64; + move16(); + } + + /*----------------------------------------------------------------* + * LSF weights are normalized, so it is always better to multiply it first + * Set up inital distance vector + *----------------------------------------------------------------*/ + /* Q0/16 * Qw_norm/16 << 1 >> 16 => Qwnorm-15/16 * Q0/16 << 1 => Qwnorm-14/32 * 6.5536 */ + Word64 ss2_64; + ss2_64 = W_mult_32_16( L_mult0( u[0], w[0] ), u[0] ); + // Q8 * Q2.56 * 2.56 * Q1 + FOR( j = 1; j < N; j++ ) + { + ss2_64 = W_mac_32_16( ss2_64, L_mult0( u[j], w[j] ), u[j] ); + } + + /* Set up inital error (residual) vectors */ + pTmp = resid[1]; /*move16();*/ + FOR( c = 0; c < maxC; c++ ) + { + Copy( u, pTmp + c * N, N ); + dist_64[1][c] = ss2_64; + move64(); + } + + /* Loop over all stages */ + m = 1; + move16(); + FOR( s = 0; s < stages; s++ ) + { + /* codebook pointer is set to point to first stage */ + cbp = cb[s]; /*3Q12*1.28*/ + move16(); + + /* Set up pointers to parent and current nodes */ + swap( indices[0], indices[1], Word16 * ); + move16(); + move16(); + move16(); + move16(); + swap( resid[0], resid[1], Word16 * ); + move16(); + move16(); + move16(); + swap( dist_64[0], dist_64[1], Word64 * ); + move64(); + move64(); + move64(); + + /* p_max points to maximum distortion node (worst of best) */ + p_max = 0; + move16(); + + n = N; + move16(); + maxn = maxN; + move16(); + if ( dims ) + { + n = dims[s]; + move16(); + } + if ( dims ) + { + maxn = n; + move16(); + } + + assert( ( maxn % 4 ) == 0 ); + N34 = mult( maxn, 24576 /*0.75f Q15*/ ); + start = 0; + move16(); + if ( offs ) + { + start = offs[s]; + move16(); + } + + set32_fx( Tmp32, 0, start ); + set32_fx( Tmp32 + start + n, 0, sub( N, add( start, n ) ) ); + + /* Set distortions to a large value */ + FOR( j = 0; j < maxC; j++ ) + { + dist_64[1][j] = LLONG_MAX; + move64(); + } + + FOR( j = 0; j < levels[s]; j++ ) + { + /* Compute weighted codebook element and its energy */ + en64 = depack_mul_values_fx64( Tmp32 + start, w + start, cbp, n ); // Q8 + // en64: Q8 * Q2.56 * Q2.56 * q1 + // Tmp: 2.56 * Q8 + + cbp += N34; /* pointer is incremented */ + + /* Iterate over all parent nodes */ + FOR( c = 0; c < m; c++ ) + { + pTmp = &resid[0][c * N]; // this resid buffer is initial lsf values + /*tmp = (*pTmp++) * Tmp[0];*/ + Word64 t164 = 0; + move64(); + t164 = W_mult_32_16( Tmp32[0], pTmp[0] ); // 2.56 * Q8 * Q2.56 * Q1 + // Tmp32: Q8 * Q2.56 + FOR( i = 1; i < N; i++ ) + { + t164 = W_mac_32_16( t164, Tmp32[i], pTmp[i] ); // 2.56 * Q8 * Q2.56 * Q1 + } + + tmp64 = W_add( dist_64[0][c], W_sub( en64, W_shl( t164, 1 ) ) ); + t164 = W_sub( tmp64, dist_64[1][p_max] ); + IF( t164 <= 0 ) + { + /* Replace worst */ + dist_64[1][p_max] = tmp64; + move64(); + indices[1][p_max * stages + s] = j; + move16(); + parents[p_max] = c; + move16(); + + p_max = ( *func_ptr64 )( dist_64[1] ); + + } /*IF (L_sub(tmp,dist[1][p_max]) < 0) */ + } /* FOR (c=0; c