opus_int silk_InitEncoder( /* O Returns error code */ void *encState, /* I/O State */ int arch, /* I Run-time architecture */ silk_EncControlStruct *encStatus /* O Encoder Status */ ) { silk_encoder *psEnc; opus_int n, ret = SILK_NO_ERROR; psEnc = (silk_encoder *)encState; /* Reset encoder */ silk_memset( psEnc, 0, sizeof( silk_encoder ) ); for( n = 0; n < ENCODER_NUM_CHANNELS; n++ ) { if( ret += silk_init_encoder( &psEnc->state_Fxx[ n ], arch ) ) { silk_assert( 0 ); } } psEnc->nChannelsAPI = 1; psEnc->nChannelsInternal = 1; /* Read control structure */ if( ret += silk_QueryEncoder( encState, encStatus ) ) { silk_assert( 0 ); } return ret; }
/* Compute quantization errors for an LPC_order element input vector for a VQ codebook */ void silk_NLSF_VQ( opus_int32 err_Q26[], /* O Quantization errors [K] */ const opus_int16 in_Q15[], /* I Input vectors to be quantized [LPC_order] */ const opus_uint8 pCB_Q8[], /* I Codebook vectors [K*LPC_order] */ const opus_int K, /* I Number of codebook vectors */ const opus_int LPC_order /* I Number of LPCs */ ) { opus_int i, m; opus_int32 diff_Q15, sum_error_Q30, sum_error_Q26; silk_assert( LPC_order <= 16 ); silk_assert( ( LPC_order & 1 ) == 0 ); /* Loop over codebook */ for( i = 0; i < K; i++ ) { sum_error_Q26 = 0; for( m = 0; m < LPC_order; m += 2 ) { /* Compute weighted squared quantization error for index m */ diff_Q15 = silk_SUB_LSHIFT32( in_Q15[ m ], (opus_int32)*pCB_Q8++, 7 ); /* range: [ -32767 : 32767 ]*/ sum_error_Q30 = silk_SMULBB( diff_Q15, diff_Q15 ); /* Compute weighted squared quantization error for index m + 1 */ diff_Q15 = silk_SUB_LSHIFT32( in_Q15[m + 1], (opus_int32)*pCB_Q8++, 7 ); /* range: [ -32767 : 32767 ]*/ sum_error_Q30 = silk_SMLABB( sum_error_Q30, diff_Q15, diff_Q15 ); sum_error_Q26 = silk_ADD_RSHIFT32( sum_error_Q26, sum_error_Q30, 4 ); silk_assert( sum_error_Q26 >= 0 ); silk_assert( sum_error_Q30 >= 0 ); } err_Q26[ i ] = sum_error_Q26; } }
static OPUS_INLINE void silk_LDL_FLP( silk_float *A, /* I/O Pointer to Symetric Square Matrix */ opus_int M, /* I Size of Matrix */ silk_float *L, /* I/O Pointer to Square Upper triangular Matrix */ silk_float *Dinv /* I/O Pointer to vector holding the inverse diagonal elements of D */ ) { opus_int i, j, k, loop_count, err = 1; silk_float *ptr1, *ptr2; double temp, diag_min_value; silk_float v[ MAX_MATRIX_SIZE ] = { 0 }, D[ MAX_MATRIX_SIZE ]; /* temp arrays*/ silk_assert(M <= MAX_MATRIX_SIZE); diag_min_value = FIND_LTP_COND_FAC * 0.5f * (A[ 0 ] + A[ M * M - 1 ]); for(loop_count = 0; loop_count < M && err == 1; loop_count++) { err = 0; for(j = 0; j < M; j++) { ptr1 = matrix_adr(L, j, 0, M); temp = matrix_ptr(A, j, j, M); /* element in row j column j*/ for(i = 0; i < j; i++) { v[ i ] = ptr1[ i ] * D[ i ]; temp -= ptr1[ i ] * v[ i ]; } if(temp < diag_min_value) { /* Badly conditioned matrix: add white noise and run again */ temp = (loop_count + 1) * diag_min_value - temp; for(i = 0; i < M; i++) { matrix_ptr(A, i, i, M) += (silk_float)temp; } err = 1; break; } D[ j ] = (silk_float)temp; Dinv[ j ] = (silk_float)(1.0f / temp); matrix_ptr(L, j, j, M) = 1.0f; ptr1 = matrix_adr(A, j, 0, M); ptr2 = matrix_adr(L, j + 1, 0, M); for(i = j + 1; i < M; i++) { temp = 0.0; for(k = 0; k < j; k++) { temp += ptr2[ k ] * v[ k ]; } matrix_ptr(L, i, j, M) = (silk_float)((ptr1[ i ] - temp) * Dinv[ j ]); ptr2 += M; /* go to next column*/ } } } silk_assert(err == 0); }
/* Autocorrelations for a warped frequency axis */ void silk_warped_autocorrelation_FIX( opus_int32 *corr, /* O Result [order + 1] */ opus_int *scale, /* O Scaling of the correlation vector */ const opus_int16 *input, /* I Input data to correlate */ const opus_int warping_Q16, /* I Warping coefficient */ const opus_int length, /* I Length of input */ const opus_int order /* I Correlation order (even) */ ) { opus_int n, i, lsh; opus_int32 tmp1_QS, tmp2_QS; opus_int32 state_QS[ MAX_SHAPE_LPC_ORDER + 1 ] = { 0 }; opus_int64 corr_QC[ MAX_SHAPE_LPC_ORDER + 1 ] = { 0 }; /* Order must be even */ silk_assert( ( order & 1 ) == 0 ); silk_assert( 2 * QS - QC >= 0 ); /* Loop over samples */ for( n = 0; n < length; n++ ) { tmp1_QS = silk_LSHIFT32( (opus_int32)input[ n ], QS ); /* Loop over allpass sections */ for( i = 0; i < order; i += 2 ) { /* Output of allpass section */ tmp2_QS = silk_SMLAWB( state_QS[ i ], state_QS[ i + 1 ] - tmp1_QS, warping_Q16 ); state_QS[ i ] = tmp1_QS; corr_QC[ i ] += silk_RSHIFT64( silk_SMULL( tmp1_QS, state_QS[ 0 ] ), 2 * QS - QC ); /* Output of allpass section */ tmp1_QS = silk_SMLAWB( state_QS[ i + 1 ], state_QS[ i + 2 ] - tmp2_QS, warping_Q16 ); state_QS[ i + 1 ] = tmp2_QS; corr_QC[ i + 1 ] += silk_RSHIFT64( silk_SMULL( tmp2_QS, state_QS[ 0 ] ), 2 * QS - QC ); } state_QS[ order ] = tmp1_QS; corr_QC[ order ] += silk_RSHIFT64( silk_SMULL( tmp1_QS, state_QS[ 0 ] ), 2 * QS - QC ); } lsh = silk_CLZ64( corr_QC[ 0 ] ) - 35; lsh = silk_LIMIT( lsh, -12 - QC, 30 - QC ); *scale = -( QC + lsh ); silk_assert( *scale >= -30 && *scale <= 12 ); if( lsh >= 0 ) { for( i = 0; i < order + 1; i++ ) { corr[ i ] = (opus_int32)silk_CHECK_FIT32( silk_LSHIFT64( corr_QC[ i ], lsh ) ); } } else { for( i = 0; i < order + 1; i++ ) { corr[ i ] = (opus_int32)silk_CHECK_FIT32( silk_RSHIFT64( corr_QC[ i ], -lsh ) ); } } silk_assert( corr_QC[ 0 ] >= 0 ); /* If breaking, decrease QC*/ }
/* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */ silk_float silk_residual_energy_covar_FLP( /* O Weighted residual energy */ const silk_float *c, /* I Filter coefficients */ silk_float *wXX, /* I/O Weighted correlation matrix, reg. out */ const silk_float *wXx, /* I Weighted correlation vector */ const silk_float wxx, /* I Weighted correlation value */ const opus_int D /* I Dimension */ ) { opus_int i, j, k; silk_float tmp, nrg = 0.0f, regularization; /* Safety checks */ silk_assert( D >= 0 ); regularization = REGULARIZATION_FACTOR * ( wXX[ 0 ] + wXX[ D * D - 1 ] ); for( k = 0; k < MAX_ITERATIONS_RESIDUAL_NRG; k++ ) { nrg = wxx; tmp = 0.0f; for( i = 0; i < D; i++ ) { tmp += wXx[ i ] * c[ i ]; } nrg -= 2.0f * tmp; /* compute c' * wXX * c, assuming wXX is symmetric */ for( i = 0; i < D; i++ ) { tmp = 0.0f; for( j = i + 1; j < D; j++ ) { tmp += matrix_c_ptr( wXX, i, j, D ) * c[ j ]; } nrg += c[ i ] * ( 2.0f * tmp + matrix_c_ptr( wXX, i, i, D ) * c[ i ] ); } if( nrg > 0 ) { break; } else { /* Add white noise */ for( i = 0; i < D; i++ ) { matrix_c_ptr( wXX, i, i, D ) += regularization; } /* Increase noise for next run */ regularization *= 2.0f; } } if( k == MAX_ITERATIONS_RESIDUAL_NRG ) { silk_assert( nrg == 0 ); nrg = 1.0f; } return nrg; }
/* sum of squares of a silk_float array, with result as double */ double silk_energy_FLP( const silk_float *data, opus_int dataSize ) { opus_int i, dataSize4; double result; /* 4x unrolled loop */ result = 0.0; dataSize4 = dataSize & 0xFFFC; for( i = 0; i < dataSize4; i += 4 ) { result += data[ i + 0 ] * (double)data[ i + 0 ] + data[ i + 1 ] * (double)data[ i + 1 ] + data[ i + 2 ] * (double)data[ i + 2 ] + data[ i + 3 ] * (double)data[ i + 3 ]; } /* add any remaining products */ for( ; i < dataSize; i++ ) { result += data[ i ] * (double)data[ i ]; } silk_assert( result >= 0.0 ); return result; }
/* Shell decoder, operates on one shell code frame of 16 pulses */ void silk_shell_decoder( opus_int *pulses0, /* O data: nonnegative pulse amplitudes */ ec_dec *psRangeDec, /* I/O Compressor data structure */ const opus_int pulses4 /* I number of pulses per pulse-subframe */ ) { opus_int pulses3[ 2 ], pulses2[ 4 ], pulses1[ 8 ]; /* this function operates on one shell code frame of 16 pulses */ silk_assert( SHELL_CODEC_FRAME_LENGTH == 16 ); decode_split( &pulses3[ 0 ], &pulses3[ 1 ], psRangeDec, pulses4, silk_shell_code_table3 ); decode_split( &pulses2[ 0 ], &pulses2[ 1 ], psRangeDec, pulses3[ 0 ], silk_shell_code_table2 ); decode_split( &pulses1[ 0 ], &pulses1[ 1 ], psRangeDec, pulses2[ 0 ], silk_shell_code_table1 ); decode_split( &pulses0[ 0 ], &pulses0[ 1 ], psRangeDec, pulses1[ 0 ], silk_shell_code_table0 ); decode_split( &pulses0[ 2 ], &pulses0[ 3 ], psRangeDec, pulses1[ 1 ], silk_shell_code_table0 ); decode_split( &pulses1[ 2 ], &pulses1[ 3 ], psRangeDec, pulses2[ 1 ], silk_shell_code_table1 ); decode_split( &pulses0[ 4 ], &pulses0[ 5 ], psRangeDec, pulses1[ 2 ], silk_shell_code_table0 ); decode_split( &pulses0[ 6 ], &pulses0[ 7 ], psRangeDec, pulses1[ 3 ], silk_shell_code_table0 ); decode_split( &pulses2[ 2 ], &pulses2[ 3 ], psRangeDec, pulses3[ 1 ], silk_shell_code_table2 ); decode_split( &pulses1[ 4 ], &pulses1[ 5 ], psRangeDec, pulses2[ 2 ], silk_shell_code_table1 ); decode_split( &pulses0[ 8 ], &pulses0[ 9 ], psRangeDec, pulses1[ 4 ], silk_shell_code_table0 ); decode_split( &pulses0[ 10 ], &pulses0[ 11 ], psRangeDec, pulses1[ 5 ], silk_shell_code_table0 ); decode_split( &pulses1[ 6 ], &pulses1[ 7 ], psRangeDec, pulses2[ 3 ], silk_shell_code_table1 ); decode_split( &pulses0[ 12 ], &pulses0[ 13 ], psRangeDec, pulses1[ 6 ], silk_shell_code_table0 ); decode_split( &pulses0[ 14 ], &pulses0[ 15 ], psRangeDec, pulses1[ 7 ], silk_shell_code_table0 ); }
/* Calculates correlation vector X'*t */ void silk_corrVector_FIX( const opus_int16 *x, /* I x vector [L + order - 1] used to form data matrix X */ const opus_int16 *t, /* I Target vector [L] */ const opus_int L, /* I Length of vectors */ const opus_int order, /* I Max lag for correlation */ opus_int32 *Xt, /* O Pointer to X'*t correlation vector [order] */ const opus_int rshifts /* I Right shifts of correlations */ ) { opus_int lag, i; const opus_int16 *ptr1, *ptr2; opus_int32 inner_prod; ptr1 = &x[ order - 1 ]; /* Points to first sample of column 0 of X: X[:,0] */ ptr2 = t; /* Calculate X'*t */ if( rshifts > 0 ) { /* Right shifting used */ for( lag = 0; lag < order; lag++ ) { inner_prod = 0; for( i = 0; i < L; i++ ) { inner_prod += silk_RSHIFT32( silk_SMULBB( ptr1[ i ], ptr2[i] ), rshifts ); } Xt[ lag ] = inner_prod; /* X[:,lag]'*t */ ptr1--; /* Go to next column of X */ } } else { silk_assert( rshifts == 0 ); for( lag = 0; lag < order; lag++ ) { Xt[ lag ] = silk_inner_prod_aligned( ptr1, ptr2, L ); /* X[:,lag]'*t */ ptr1--; /* Go to next column of X */ } } }
/* Interpolate two vectors */ void silk_interpolate( opus_int16 xi[ MAX_LPC_ORDER ], /* O interpolated vector */ const opus_int16 x0[ MAX_LPC_ORDER ], /* I first vector */ const opus_int16 x1[ MAX_LPC_ORDER ], /* I second vector */ const opus_int ifact_Q2, /* I interp. factor, weight on 2nd vector */ const opus_int d /* I number of parameters */ ) { opus_int i; silk_assert( ifact_Q2 >= 0 ); silk_assert( ifact_Q2 <= 4 ); for( i = 0; i < d; i++ ) { xi[ i ] = (opus_int16)silk_ADD_RSHIFT( x0[ i ], silk_SMULBB( x1[ i ] - x0[ i ], ifact_Q2 ), 2 ); } }
/* This function is only used by the fixed-point build */ void silk_insertion_sort_decreasing_int16( opus_int16 *a, /* I/O Unsorted / Sorted vector */ opus_int *idx, /* O Index vector for the sorted elements */ const opus_int L, /* I Vector length */ const opus_int K /* I Number of correctly sorted positions */ ) { opus_int i, j; opus_int value; /* Safety checks */ silk_assert( K > 0 ); silk_assert( L > 0 ); silk_assert( L >= K ); /* Write start indices in index vector */ for( i = 0; i < K; i++ ) { idx[ i ] = i; } /* Sort vector elements by value, decreasing order */ for( i = 1; i < K; i++ ) { value = a[ i ]; for( j = i - 1; ( j >= 0 ) && ( value > a[ j ] ); j-- ) { a[ j + 1 ] = a[ j ]; /* Shift value */ idx[ j + 1 ] = idx[ j ]; /* Shift index */ } a[ j + 1 ] = value; /* Write value */ idx[ j + 1 ] = i; /* Write index */ } /* If less than L values are asked for, check the remaining values, */ /* but only spend CPU to ensure that the K first values are correct */ for( i = K; i < L; i++ ) { value = a[ i ]; if( value > a[ K - 1 ] ) { for( j = K - 2; ( j >= 0 ) && ( value > a[ j ] ); j-- ) { a[ j + 1 ] = a[ j ]; /* Shift value */ idx[ j + 1 ] = idx[ j ]; /* Shift index */ } a[ j + 1 ] = value; /* Write value */ idx[ j + 1 ] = i; /* Write index */ } } }
void silk_LPC_analysis_filter( opus_int16 *out, /* O Output signal */ const opus_int16 *in, /* I Input signal */ const opus_int16 *B, /* I MA prediction coefficients, Q12 [order] */ const opus_int32 len, /* I Signal length */ const opus_int32 d /* I Filter order */ ) { opus_int ix, j; opus_int32 out32_Q12, out32; const opus_int16 *in_ptr; silk_assert( d >= 6 ); silk_assert( (d & 1) == 0 ); silk_assert( d <= len ); for( ix = d; ix < len; ix++ ) { in_ptr = &in[ ix - 1 ]; out32_Q12 = silk_SMULBB( in_ptr[ 0 ], B[ 0 ] ); /* Allowing wrap around so that two wraps can cancel each other. The rare cases where the result wraps around can only be triggered by invalid streams*/ out32_Q12 = silk_SMLABB_ovflw( out32_Q12, in_ptr[ -1 ], B[ 1 ] ); out32_Q12 = silk_SMLABB_ovflw( out32_Q12, in_ptr[ -2 ], B[ 2 ] ); out32_Q12 = silk_SMLABB_ovflw( out32_Q12, in_ptr[ -3 ], B[ 3 ] ); out32_Q12 = silk_SMLABB_ovflw( out32_Q12, in_ptr[ -4 ], B[ 4 ] ); out32_Q12 = silk_SMLABB_ovflw( out32_Q12, in_ptr[ -5 ], B[ 5 ] ); for( j = 6; j < d; j += 2 ) { out32_Q12 = silk_SMLABB_ovflw( out32_Q12, in_ptr[ -j ], B[ j ] ); out32_Q12 = silk_SMLABB_ovflw( out32_Q12, in_ptr[ -j - 1 ], B[ j + 1 ] ); } /* Subtract prediction */ out32_Q12 = silk_SUB32_ovflw( silk_LSHIFT( (opus_int32)in_ptr[ 1 ], 12 ), out32_Q12 ); /* Scale to Q0 */ out32 = silk_RSHIFT_ROUND( out32_Q12, 12 ); /* Saturate output */ out[ ix ] = (opus_int16)silk_SAT16( out32 ); } /* Set first d output samples to zero */ silk_memset( out, 0, d * sizeof( opus_int16 ) ); }
/* uses SMLAWB(), requiring armv5E and higher. */ opus_int32 silk_schur( /* O Returns residual energy */ opus_int16 *rc_Q15, /* O reflection coefficients [order] Q15 */ const opus_int32 *c, /* I correlations [order+1] */ const opus_int32 order /* I prediction order */ ) { opus_int k, n, lz; opus_int32 C[ SILK_MAX_ORDER_LPC + 1 ][ 2 ]; opus_int32 Ctmp1, Ctmp2, rc_tmp_Q15; silk_assert( order==6||order==8||order==10||order==12||order==14||order==16 ); /* Get number of leading zeros */ lz = silk_CLZ32( c[ 0 ] ); /* Copy correlations and adjust level to Q30 */ if( lz < 2 ) { /* lz must be 1, so shift one to the right */ for( k = 0; k < order + 1; k++ ) { C[ k ][ 0 ] = C[ k ][ 1 ] = silk_RSHIFT( c[ k ], 1 ); } } else if( lz > 2 ) { /* Shift to the left */ lz -= 2; for( k = 0; k < order + 1; k++ ) { C[ k ][ 0 ] = C[ k ][ 1 ] = silk_LSHIFT( c[ k ], lz ); } } else { /* No need to shift */ for( k = 0; k < order + 1; k++ ) { C[ k ][ 0 ] = C[ k ][ 1 ] = c[ k ]; } } for( k = 0; k < order; k++ ) { /* Get reflection coefficient */ rc_tmp_Q15 = -silk_DIV32_16( C[ k + 1 ][ 0 ], silk_max_32( silk_RSHIFT( C[ 0 ][ 1 ], 15 ), 1 ) ); /* Clip (shouldn't happen for properly conditioned inputs) */ rc_tmp_Q15 = silk_SAT16( rc_tmp_Q15 ); /* Store */ rc_Q15[ k ] = (opus_int16)rc_tmp_Q15; /* Update correlations */ for( n = 0; n < order - k; n++ ) { Ctmp1 = C[ n + k + 1 ][ 0 ]; Ctmp2 = C[ n ][ 1 ]; C[ n + k + 1 ][ 0 ] = silk_SMLAWB( Ctmp1, silk_LSHIFT( Ctmp2, 1 ), rc_tmp_Q15 ); C[ n ][ 1 ] = silk_SMLAWB( Ctmp2, silk_LSHIFT( Ctmp1, 1 ), rc_tmp_Q15 ); } } /* return residual energy */ return C[ 0 ][ 1 ]; }
/* Entropy code the mid/side quantization indices */ void silk_stereo_encode_pred( ec_enc *psRangeEnc, /* I/O Compressor data structure */ opus_int8 ix[ 2 ][ 3 ] /* I Quantization indices */ ) { opus_int n; /* Entropy coding */ n = 5 * ix[ 0 ][ 2 ] + ix[ 1 ][ 2 ]; silk_assert( n < 25 ); ec_enc_icdf( psRangeEnc, n, silk_stereo_pred_joint_iCDF, 8 ); for( n = 0; n < 2; n++ ) { silk_assert( ix[ n ][ 0 ] < 3 ); silk_assert( ix[ n ][ 1 ] < STEREO_QUANT_SUB_STEPS ); ec_enc_icdf( psRangeEnc, ix[ n ][ 0 ], silk_uniform3_iCDF, 8 ); ec_enc_icdf( psRangeEnc, ix[ n ][ 1 ], silk_uniform5_iCDF, 8 ); } }
/* Uses SMULL(), available on armv4 */ opus_int32 silk_schur64( /* O returns residual energy */ opus_int32 rc_Q16[], /* O Reflection coefficients [order] Q16 */ const opus_int32 c[], /* I Correlations [order+1] */ opus_int32 order /* I Prediction order */ ) { opus_int k, n; opus_int32 C[ SILK_MAX_ORDER_LPC + 1 ][ 2 ]; opus_int32 Ctmp1_Q30, Ctmp2_Q30, rc_tmp_Q31; silk_assert( order==6||order==8||order==10||order==12||order==14||order==16 ); /* Check for invalid input */ if( c[ 0 ] <= 0 ) { silk_memset( rc_Q16, 0, order * sizeof( opus_int32 ) ); return 0; } for( k = 0; k < order + 1; k++ ) { C[ k ][ 0 ] = C[ k ][ 1 ] = c[ k ]; } for( k = 0; k < order; k++ ) { /* Check that we won't be getting an unstable rc, otherwise stop here. */ if (silk_abs_int32(C[ k + 1 ][ 0 ]) >= C[ 0 ][ 1 ]) { if ( C[ k + 1 ][ 0 ] > 0 ) { rc_Q16[ k ] = -SILK_FIX_CONST( .99f, 16 ); } else { rc_Q16[ k ] = SILK_FIX_CONST( .99f, 16 ); } k++; break; } /* Get reflection coefficient: divide two Q30 values and get result in Q31 */ rc_tmp_Q31 = silk_DIV32_varQ( -C[ k + 1 ][ 0 ], C[ 0 ][ 1 ], 31 ); /* Save the output */ rc_Q16[ k ] = silk_RSHIFT_ROUND( rc_tmp_Q31, 15 ); /* Update correlations */ for( n = 0; n < order - k; n++ ) { Ctmp1_Q30 = C[ n + k + 1 ][ 0 ]; Ctmp2_Q30 = C[ n ][ 1 ]; /* Multiply and add the highest int32 */ C[ n + k + 1 ][ 0 ] = Ctmp1_Q30 + silk_SMMUL( silk_LSHIFT( Ctmp2_Q30, 1 ), rc_tmp_Q31 ); C[ n ][ 1 ] = Ctmp2_Q30 + silk_SMMUL( silk_LSHIFT( Ctmp1_Q30, 1 ), rc_tmp_Q31 ); } } for(; k < order; k++ ) { rc_Q16[ k ] = 0; } return silk_max_32( 1, C[ 0 ][ 1 ] ); }
/* 2 -> sine window from pi/2 to pi */ void silk_apply_sine_window_FLP( silk_float px_win[], /* O Pointer to windowed signal */ const silk_float px[], /* I Pointer to input signal */ const opus_int win_type, /* I Selects a window type */ const opus_int length /* I Window length, multiple of 4 */ ) { opus_int k; silk_float freq, c, S0, S1; silk_assert( win_type == 1 || win_type == 2 ); /* Length must be multiple of 4 */ silk_assert( ( length & 3 ) == 0 ); freq = PI / ( length + 1 ); /* Approximation of 2 * cos(f) */ c = 2.0f - freq * freq; /* Initialize state */ if( win_type < 2 ) { /* Start from 0 */ S0 = 0.0f; /* Approximation of sin(f) */ S1 = freq; } else { /* Start from 1 */ S0 = 1.0f; /* Approximation of cos(f) */ S1 = 0.5f * c; } /* Uses the recursive equation: sin(n*f) = 2 * cos(f) * sin((n-1)*f) - sin((n-2)*f) */ /* 4 samples at a time */ for( k = 0; k < length; k += 4 ) { px_win[ k + 0 ] = px[ k + 0 ] * 0.5f * ( S0 + S1 ); px_win[ k + 1 ] = px[ k + 1 ] * S1; S0 = c * S1 - S0; px_win[ k + 2 ] = px[ k + 2 ] * 0.5f * ( S1 + S0 ); px_win[ k + 3 ] = px[ k + 3 ] * S0; S1 = c * S0 - S1; } }
/* Prefilter for finding Quantizer input signal */ static OPUS_INLINE void silk_prefilt_FIX( silk_prefilter_state_FIX *P, /* I/O state */ opus_int32 st_res_Q12[], /* I short term residual signal */ opus_int32 xw_Q3[], /* O prefiltered signal */ opus_int32 HarmShapeFIRPacked_Q12, /* I Harmonic shaping coeficients */ opus_int Tilt_Q14, /* I Tilt shaping coeficient */ opus_int32 LF_shp_Q14, /* I Low-frequancy shaping coeficients */ opus_int lag, /* I Lag for harmonic shaping */ opus_int length /* I Length of signals */ ) { opus_int i, idx, LTP_shp_buf_idx; opus_int32 n_LTP_Q12, n_Tilt_Q10, n_LF_Q10; opus_int32 sLF_MA_shp_Q12, sLF_AR_shp_Q12; opus_int16 *LTP_shp_buf; /* To speed up use temp variables instead of using the struct */ LTP_shp_buf = P->sLTP_shp; LTP_shp_buf_idx = P->sLTP_shp_buf_idx; sLF_AR_shp_Q12 = P->sLF_AR_shp_Q12; sLF_MA_shp_Q12 = P->sLF_MA_shp_Q12; for (i = 0; i < length; i++) { if (lag > 0) { /* unrolled loop */ silk_assert(HARM_SHAPE_FIR_TAPS == 3); idx = lag + LTP_shp_buf_idx; n_LTP_Q12 = silk_SMULBB(LTP_shp_buf[(idx - HARM_SHAPE_FIR_TAPS / 2 - 1) & LTP_MASK], HarmShapeFIRPacked_Q12); n_LTP_Q12 = silk_SMLABT(n_LTP_Q12, LTP_shp_buf[(idx - HARM_SHAPE_FIR_TAPS / 2) & LTP_MASK], HarmShapeFIRPacked_Q12); n_LTP_Q12 = silk_SMLABB(n_LTP_Q12, LTP_shp_buf[(idx - HARM_SHAPE_FIR_TAPS / 2 + 1) & LTP_MASK], HarmShapeFIRPacked_Q12); } else { n_LTP_Q12 = 0; } n_Tilt_Q10 = silk_SMULWB(sLF_AR_shp_Q12, Tilt_Q14); n_LF_Q10 = silk_SMLAWB(silk_SMULWT(sLF_AR_shp_Q12, LF_shp_Q14), sLF_MA_shp_Q12, LF_shp_Q14); sLF_AR_shp_Q12 = silk_SUB32(st_res_Q12[i], silk_LSHIFT(n_Tilt_Q10, 2)); sLF_MA_shp_Q12 = silk_SUB32(sLF_AR_shp_Q12, silk_LSHIFT(n_LF_Q10, 2)); LTP_shp_buf_idx = (LTP_shp_buf_idx - 1) & LTP_MASK; LTP_shp_buf[LTP_shp_buf_idx] = (opus_int16) silk_SAT16( silk_RSHIFT_ROUND(sLF_MA_shp_Q12, 12)); xw_Q3[i] = silk_RSHIFT_ROUND(silk_SUB32(sLF_MA_shp_Q12, n_LTP_Q12), 9); } /* Copy temp variable back to state */ P->sLF_AR_shp_Q12 = sLF_AR_shp_Q12; P->sLF_MA_shp_Q12 = sLF_MA_shp_Q12; P->sLTP_shp_buf_idx = LTP_shp_buf_idx; }
/* Input and output sampling rate are at most 48000 Hz */ opus_int silk_resampler( silk_resampler_state_struct *S, /* I/O Resampler state */ opus_int16 out[], /* O Output signal */ const opus_int16 in[], /* I Input signal */ opus_int32 inLen /* I Number of input samples */ ) { opus_int nSamples; /* Need at least 1 ms of input data */ silk_assert( inLen >= S->Fs_in_kHz ); /* Delay can't exceed the 1 ms of buffering */ silk_assert( S->inputDelay <= S->Fs_in_kHz ); nSamples = S->Fs_in_kHz - S->inputDelay; /* Copy to delay buffer */ silk_memcpy( &S->delayBuf[ S->inputDelay ], in, nSamples * sizeof( opus_int16 ) ); switch( S->resampler_function ) { case USE_silk_resampler_private_up2_HQ_wrapper: silk_resampler_private_up2_HQ_wrapper( S, out, S->delayBuf, S->Fs_in_kHz ); silk_resampler_private_up2_HQ_wrapper( S, &out[ S->Fs_out_kHz ], &in[ nSamples ], inLen - S->Fs_in_kHz ); break; case USE_silk_resampler_private_IIR_FIR: silk_resampler_private_IIR_FIR( S, out, S->delayBuf, S->Fs_in_kHz ); silk_resampler_private_IIR_FIR( S, &out[ S->Fs_out_kHz ], &in[ nSamples ], inLen - S->Fs_in_kHz ); break; case USE_silk_resampler_private_down_FIR: silk_resampler_private_down_FIR( S, out, S->delayBuf, S->Fs_in_kHz ); silk_resampler_private_down_FIR( S, &out[ S->Fs_out_kHz ], &in[ nSamples ], inLen - S->Fs_in_kHz ); break; default: silk_memcpy( out, S->delayBuf, S->Fs_in_kHz * sizeof( opus_int16 ) ); silk_memcpy( &out[ S->Fs_out_kHz ], &in[ nSamples ], ( inLen - S->Fs_in_kHz ) * sizeof( opus_int16 ) ); } /* Copy to delay buffer */ silk_memcpy( S->delayBuf, &in[ inLen - S->inputDelay ], S->inputDelay * sizeof( opus_int16 ) ); return 0; }
/* Helper function, interpolates the filter taps */ static OPUS_INLINE void silk_LP_interpolate_filter_taps( opus_int32 B_Q28[ TRANSITION_NB ], opus_int32 A_Q28[ TRANSITION_NA ], const opus_int ind, const opus_int32 fac_Q16 ) { opus_int nb, na; if( ind < TRANSITION_INT_NUM - 1 ) { if( fac_Q16 > 0 ) { if( fac_Q16 < 32768 ) { /* fac_Q16 is in range of a 16-bit int */ /* Piece-wise linear interpolation of B and A */ for( nb = 0; nb < TRANSITION_NB; nb++ ) { B_Q28[ nb ] = silk_SMLAWB( silk_Transition_LP_B_Q28[ ind ][ nb ], silk_Transition_LP_B_Q28[ ind + 1 ][ nb ] - silk_Transition_LP_B_Q28[ ind ][ nb ], fac_Q16 ); } for( na = 0; na < TRANSITION_NA; na++ ) { A_Q28[ na ] = silk_SMLAWB( silk_Transition_LP_A_Q28[ ind ][ na ], silk_Transition_LP_A_Q28[ ind + 1 ][ na ] - silk_Transition_LP_A_Q28[ ind ][ na ], fac_Q16 ); } } else { /* ( fac_Q16 - ( 1 << 16 ) ) is in range of a 16-bit int */ silk_assert( fac_Q16 - ( 1 << 16 ) == silk_SAT16( fac_Q16 - ( 1 << 16 ) ) ); /* Piece-wise linear interpolation of B and A */ for( nb = 0; nb < TRANSITION_NB; nb++ ) { B_Q28[ nb ] = silk_SMLAWB( silk_Transition_LP_B_Q28[ ind + 1 ][ nb ], silk_Transition_LP_B_Q28[ ind + 1 ][ nb ] - silk_Transition_LP_B_Q28[ ind ][ nb ], fac_Q16 - ( (opus_int32)1 << 16 ) ); } for( na = 0; na < TRANSITION_NA; na++ ) { A_Q28[ na ] = silk_SMLAWB( silk_Transition_LP_A_Q28[ ind + 1 ][ na ], silk_Transition_LP_A_Q28[ ind + 1 ][ na ] - silk_Transition_LP_A_Q28[ ind ][ na ], fac_Q16 - ( (opus_int32)1 << 16 ) ); } } } else { silk_memcpy( B_Q28, silk_Transition_LP_B_Q28[ ind ], TRANSITION_NB * sizeof( opus_int32 ) ); silk_memcpy( A_Q28, silk_Transition_LP_A_Q28[ ind ], TRANSITION_NA * sizeof( opus_int32 ) ); } } else { silk_memcpy( B_Q28, silk_Transition_LP_B_Q28[ TRANSITION_INT_NUM - 1 ], TRANSITION_NB * sizeof( opus_int32 ) ); silk_memcpy( A_Q28, silk_Transition_LP_A_Q28[ TRANSITION_INT_NUM - 1 ], TRANSITION_NA * sizeof( opus_int32 ) ); } }
void silk_LPC_analysis_filter_FLP( silk_float r_LPC[], /* O LPC residual signal */ const silk_float PredCoef[], /* I LPC coefficients */ const silk_float s[], /* I Input signal */ const opus_int length, /* I Length of input signal */ const opus_int Order /* I LPC order */ ) { silk_assert( Order <= length ); switch( Order ) { case 6: silk_LPC_analysis_filter6_FLP( r_LPC, PredCoef, s, length ); break; case 8: silk_LPC_analysis_filter8_FLP( r_LPC, PredCoef, s, length ); break; case 10: silk_LPC_analysis_filter10_FLP( r_LPC, PredCoef, s, length ); break; case 12: silk_LPC_analysis_filter12_FLP( r_LPC, PredCoef, s, length ); break; case 16: silk_LPC_analysis_filter16_FLP( r_LPC, PredCoef, s, length ); break; default: silk_assert( 0 ); break; } /* Set first Order output samples to zero */ silk_memset( r_LPC, 0, Order * sizeof( silk_float ) ); }
/* * Prefilter for finding Quantizer input signal */ static OPUS_INLINE void silk_prefilt_FLP( silk_prefilter_state_FLP *P, /* I/O state */ silk_float st_res[], /* I */ silk_float xw[], /* O */ silk_float *HarmShapeFIR, /* I */ silk_float Tilt, /* I */ silk_float LF_MA_shp, /* I */ silk_float LF_AR_shp, /* I */ opus_int lag, /* I */ opus_int length /* I */ ) { opus_int i; opus_int idx, LTP_shp_buf_idx; silk_float n_Tilt, n_LF, n_LTP; silk_float sLF_AR_shp, sLF_MA_shp; silk_float *LTP_shp_buf; /* To speed up use temp variables instead of using the struct */ LTP_shp_buf = P->sLTP_shp; LTP_shp_buf_idx = P->sLTP_shp_buf_idx; sLF_AR_shp = P->sLF_AR_shp; sLF_MA_shp = P->sLF_MA_shp; for( i = 0; i < length; i++ ) { if( lag > 0 ) { silk_assert( HARM_SHAPE_FIR_TAPS == 3 ); idx = lag + LTP_shp_buf_idx; n_LTP = LTP_shp_buf[ ( idx - HARM_SHAPE_FIR_TAPS / 2 - 1) & LTP_MASK ] * HarmShapeFIR[ 0 ]; n_LTP += LTP_shp_buf[ ( idx - HARM_SHAPE_FIR_TAPS / 2 ) & LTP_MASK ] * HarmShapeFIR[ 1 ]; n_LTP += LTP_shp_buf[ ( idx - HARM_SHAPE_FIR_TAPS / 2 + 1) & LTP_MASK ] * HarmShapeFIR[ 2 ]; } else { n_LTP = 0; } n_Tilt = sLF_AR_shp * Tilt; n_LF = sLF_AR_shp * LF_AR_shp + sLF_MA_shp * LF_MA_shp; sLF_AR_shp = st_res[ i ] - n_Tilt; sLF_MA_shp = sLF_AR_shp - n_LF; LTP_shp_buf_idx = ( LTP_shp_buf_idx - 1 ) & LTP_MASK; LTP_shp_buf[ LTP_shp_buf_idx ] = sLF_MA_shp; xw[ i ] = sLF_MA_shp - n_LTP; } /* Copy temp variable back to state */ P->sLF_AR_shp = sLF_AR_shp; P->sLF_MA_shp = sLF_MA_shp; P->sLTP_shp_buf_idx = LTP_shp_buf_idx; }
/* Deactivate by setting psEncC->mode = 0; */ void silk_LP_variable_cutoff( silk_LP_state *psLP, /* I/O LP filter state */ opus_int16 *frame, /* I/O Low-pass filtered output signal */ const opus_int frame_length /* I Frame length */ ) { opus_int32 B_Q28[ TRANSITION_NB ], A_Q28[ TRANSITION_NA ], fac_Q16 = 0; opus_int ind = 0; silk_assert( psLP->transition_frame_no >= 0 && psLP->transition_frame_no <= TRANSITION_FRAMES ); /* Run filter if needed */ if( psLP->mode != 0 ) { /* Calculate index and interpolation factor for interpolation */ #if( TRANSITION_INT_STEPS == 64 ) fac_Q16 = silk_LSHIFT( TRANSITION_FRAMES - psLP->transition_frame_no, 16 - 6 ); #else fac_Q16 = silk_DIV32_16( silk_LSHIFT( TRANSITION_FRAMES - psLP->transition_frame_no, 16 ), TRANSITION_FRAMES ); #endif ind = silk_RSHIFT( fac_Q16, 16 ); fac_Q16 -= silk_LSHIFT( ind, 16 ); silk_assert( ind >= 0 ); silk_assert( ind < TRANSITION_INT_NUM ); /* Interpolate filter coefficients */ silk_LP_interpolate_filter_taps( B_Q28, A_Q28, ind, fac_Q16 ); /* Update transition frame number for next frame */ psLP->transition_frame_no = silk_LIMIT( psLP->transition_frame_no + psLP->mode, 0, TRANSITION_FRAMES ); /* ARMA low-pass filtering */ silk_assert( TRANSITION_NB == 3 && TRANSITION_NA == 2 ); silk_biquad_alt( frame, B_Q28, A_Q28, psLP->In_LP_State, frame, frame_length, 1); } }
/* Processing of gains */ void silk_process_gains_FIX( silk_encoder_state_FIX *psEnc, /* I/O Encoder state */ silk_encoder_control_FIX *psEncCtrl, /* I/O Encoder control */ opus_int condCoding /* I The type of conditional coding to use */ ) { silk_shape_state_FIX *psShapeSt = &psEnc->sShape; opus_int k; opus_int32 s_Q16, InvMaxSqrVal_Q16, gain, gain_squared, ResNrg, ResNrgPart, quant_offset_Q10; /* Gain reduction when LTP coding gain is high */ if( psEnc->sCmn.indices.signalType == TYPE_VOICED ) { /*s = -0.5f * silk_sigmoid( 0.25f * ( psEncCtrl->LTPredCodGain - 12.0f ) ); */ s_Q16 = -silk_sigm_Q15( silk_RSHIFT_ROUND( psEncCtrl->LTPredCodGain_Q7 - SILK_FIX_CONST( 12.0, 7 ), 4 ) ); for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) { psEncCtrl->Gains_Q16[ k ] = silk_SMLAWB( psEncCtrl->Gains_Q16[ k ], psEncCtrl->Gains_Q16[ k ], s_Q16 ); } } /* Limit the quantized signal */ /* InvMaxSqrVal = pow( 2.0f, 0.33f * ( 21.0f - SNR_dB ) ) / subfr_length; */ InvMaxSqrVal_Q16 = silk_DIV32_16( silk_log2lin( silk_SMULWB( SILK_FIX_CONST( 21 + 16 / 0.33, 7 ) - psEnc->sCmn.SNR_dB_Q7, SILK_FIX_CONST( 0.33, 16 ) ) ), psEnc->sCmn.subfr_length ); for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) { /* Soft limit on ratio residual energy and squared gains */ ResNrg = psEncCtrl->ResNrg[ k ]; ResNrgPart = silk_SMULWW( ResNrg, InvMaxSqrVal_Q16 ); if( psEncCtrl->ResNrgQ[ k ] > 0 ) { ResNrgPart = silk_RSHIFT_ROUND( ResNrgPart, psEncCtrl->ResNrgQ[ k ] ); } else { if( ResNrgPart >= silk_RSHIFT( silk_int32_MAX, -psEncCtrl->ResNrgQ[ k ] ) ) { ResNrgPart = silk_int32_MAX; } else { ResNrgPart = silk_LSHIFT( ResNrgPart, -psEncCtrl->ResNrgQ[ k ] ); } } gain = psEncCtrl->Gains_Q16[ k ]; gain_squared = silk_ADD_SAT32( ResNrgPart, silk_SMMUL( gain, gain ) ); if( gain_squared < silk_int16_MAX ) { /* recalculate with higher precision */ gain_squared = silk_SMLAWW( silk_LSHIFT( ResNrgPart, 16 ), gain, gain ); silk_assert( gain_squared > 0 ); gain = silk_SQRT_APPROX( gain_squared ); /* Q8 */ gain = silk_min( gain, silk_int32_MAX >> 8 ); psEncCtrl->Gains_Q16[ k ] = silk_LSHIFT_SAT32( gain, 8 ); /* Q16 */ } else {
/* Find least-squares prediction gain for one signal based on another and quantize it */ opus_int32 silk_stereo_find_predictor( /* O Returns predictor in Q13 */ opus_int32 *ratio_Q14, /* O Ratio of residual and mid energies */ const opus_int16 x[], /* I Basis signal */ const opus_int16 y[], /* I Target signal */ opus_int32 mid_res_amp_Q0[], /* I/O Smoothed mid, residual norms */ opus_int length, /* I Number of samples */ opus_int smooth_coef_Q16 /* I Smoothing coefficient */ ) { opus_int scale, scale1, scale2; opus_int32 nrgx, nrgy, corr, pred_Q13, pred2_Q10; /* Find predictor */ silk_sum_sqr_shift( &nrgx, &scale1, x, length ); silk_sum_sqr_shift( &nrgy, &scale2, y, length ); scale = silk_max_int( scale1, scale2 ); scale = scale + ( scale & 1 ); /* make even */ nrgy = silk_RSHIFT32( nrgy, scale - scale2 ); nrgx = silk_RSHIFT32( nrgx, scale - scale1 ); nrgx = silk_max_int( nrgx, 1 ); corr = silk_inner_prod_aligned_scale( x, y, scale, length ); pred_Q13 = silk_DIV32_varQ( corr, nrgx, 13 ); pred_Q13 = silk_LIMIT( pred_Q13, -(1 << 14), 1 << 14 ); pred2_Q10 = silk_SMULWB( pred_Q13, pred_Q13 ); /* Faster update for signals with large prediction parameters */ smooth_coef_Q16 = (opus_int)silk_max_int( smooth_coef_Q16, silk_abs( pred2_Q10 ) ); /* Smoothed mid and residual norms */ silk_assert( smooth_coef_Q16 < 32768 ); scale = silk_RSHIFT( scale, 1 ); mid_res_amp_Q0[ 0 ] = silk_SMLAWB( mid_res_amp_Q0[ 0 ], silk_LSHIFT( silk_SQRT_APPROX( nrgx ), scale ) - mid_res_amp_Q0[ 0 ], smooth_coef_Q16 ); /* Residual energy = nrgy - 2 * pred * corr + pred^2 * nrgx */ nrgy = silk_SUB_LSHIFT32( nrgy, silk_SMULWB( corr, pred_Q13 ), 3 + 1 ); nrgy = silk_ADD_LSHIFT32( nrgy, silk_SMULWB( nrgx, pred2_Q10 ), 6 ); mid_res_amp_Q0[ 1 ] = silk_SMLAWB( mid_res_amp_Q0[ 1 ], silk_LSHIFT( silk_SQRT_APPROX( nrgy ), scale ) - mid_res_amp_Q0[ 1 ], smooth_coef_Q16 ); /* Ratio of smoothed residual and mid norms */ *ratio_Q14 = silk_DIV32_varQ( mid_res_amp_Q0[ 1 ], silk_max( mid_res_amp_Q0[ 0 ], 1 ), 14 ); *ratio_Q14 = silk_LIMIT( *ratio_Q14, 0, 32767 ); return pred_Q13; }
void silk_insertion_sort_increasing_all_values_int16( opus_int16 *a, /* I/O Unsorted / Sorted vector */ const opus_int L /* I Vector length */ ) { opus_int value; opus_int i, j; /* Safety checks */ silk_assert( L > 0 ); /* Sort vector elements by value, increasing order */ for( i = 1; i < L; i++ ) { value = a[ i ]; for( j = i - 1; ( j >= 0 ) && ( value < a[ j ] ); j-- ) { a[ j + 1 ] = a[ j ]; /* Shift value */ } a[ j + 1 ] = value; /* Write value */ } }
void silk_warped_LPC_analysis_filter_FIX( opus_int32 state[], /* I/O State [order + 1] */ opus_int32 res_Q2[], /* O Residual signal [length] */ const opus_int16 coef_Q13[], /* I Coefficients [order] */ const opus_int16 input[], /* I Input signal [length] */ const opus_int16 lambda_Q16, /* I Warping factor */ const opus_int length, /* I Length of input signal */ const opus_int order /* I Filter order (even) */ ) { opus_int n, i; opus_int32 acc_Q11, tmp1, tmp2; /* Order must be even */ silk_assert( ( order & 1 ) == 0 ); for( n = 0; n < length; n++ ) { /* Output of lowpass section */ tmp2 = silk_SMLAWB( state[ 0 ], state[ 1 ], lambda_Q16 ); state[ 0 ] = silk_LSHIFT( input[ n ], 14 ); /* Output of allpass section */ tmp1 = silk_SMLAWB( state[ 1 ], state[ 2 ] - tmp2, lambda_Q16 ); state[ 1 ] = tmp2; acc_Q11 = silk_RSHIFT( order, 1 ); acc_Q11 = silk_SMLAWB( acc_Q11, tmp2, coef_Q13[ 0 ] ); /* Loop over allpass sections */ for( i = 2; i < order; i += 2 ) { /* Output of allpass section */ tmp2 = silk_SMLAWB( state[ i ], state[ i + 1 ] - tmp1, lambda_Q16 ); state[ i ] = tmp1; acc_Q11 = silk_SMLAWB( acc_Q11, tmp1, coef_Q13[ i - 1 ] ); /* Output of allpass section */ tmp1 = silk_SMLAWB( state[ i + 1 ], state[ i + 2 ] - tmp2, lambda_Q16 ); state[ i + 1 ] = tmp2; acc_Q11 = silk_SMLAWB( acc_Q11, tmp2, coef_Q13[ i ] ); } state[ order ] = tmp1; acc_Q11 = silk_SMLAWB( acc_Q11, tmp1, coef_Q13[ order - 1 ] ); res_Q2[ n ] = silk_LSHIFT( (opus_int32)input[ n ], 2 ) - silk_RSHIFT_ROUND( acc_Q11, 9 ); } }
static void silk_warped_LPC_analysis_filter_FLP( silk_float state[], /* I/O State [order + 1] */ silk_float res[], /* O Residual signal [length] */ const silk_float coef[], /* I Coefficients [order] */ const silk_float input[], /* I Input signal [length] */ const silk_float lambda, /* I Warping factor */ const opus_int length, /* I Length of input signal */ const opus_int order /* I Filter order (even) */ ) { opus_int n, i; silk_float acc, tmp1, tmp2; /* Order must be even */ silk_assert( ( order & 1 ) == 0 ); for( n = 0; n < length; n++ ) { /* Output of lowpass section */ tmp2 = state[ 0 ] + lambda * state[ 1 ]; state[ 0 ] = input[ n ]; /* Output of allpass section */ tmp1 = state[ 1 ] + lambda * ( state[ 2 ] - tmp2 ); state[ 1 ] = tmp2; acc = coef[ 0 ] * tmp2; /* Loop over allpass sections */ for( i = 2; i < order; i += 2 ) { /* Output of allpass section */ tmp2 = state[ i ] + lambda * ( state[ i + 1 ] - tmp1 ); state[ i ] = tmp1; acc += coef[ i - 1 ] * tmp1; /* Output of allpass section */ tmp1 = state[ i + 1 ] + lambda * ( state[ i + 2 ] - tmp2 ); state[ i + 1 ] = tmp2; acc += coef[ i ] * tmp2; } state[ order ] = tmp1; acc += coef[ order - 1 ] * tmp1; res[ n ] = input[ n ] - acc; } }
/* Solves Ax = b, assuming A is symmetric */ void silk_solve_LDL_FIX( opus_int32 *A, /* I Pointer to symetric square matrix A */ opus_int M, /* I Size of matrix */ const opus_int32 *b, /* I Pointer to b vector */ opus_int32 *x_Q16 /* O Pointer to x solution vector */ ) { VARDECL( opus_int32, L_Q16 ); opus_int32 Y[ MAX_MATRIX_SIZE ]; inv_D_t inv_D[ MAX_MATRIX_SIZE ]; SAVE_STACK; silk_assert( M <= MAX_MATRIX_SIZE ); ALLOC( L_Q16, M * M, opus_int32 ); /*************************************************** Factorize A by LDL such that A = L*D*L', where L is lower triangular with ones on diagonal ****************************************************/ silk_LDL_factorize_FIX( A, M, L_Q16, inv_D ); /**************************************************** * substitute D*L'*x = Y. ie: L*D*L'*x = b => L*Y = b <=> Y = inv(L)*b ******************************************************/ silk_LS_SolveFirst_FIX( L_Q16, M, b, Y ); /**************************************************** D*L'*x = Y <=> L'*x = inv(D)*Y, because D is diagonal just multiply with 1/d_i ****************************************************/ silk_LS_divide_Q16_FIX( Y, inv_D, M ); /**************************************************** x = inv(L') * inv(D) * Y *****************************************************/ silk_LS_SolveLast_FIX( L_Q16, M, Y, x_Q16 ); RESTORE_STACK; }
/* Autocorrelations for a warped frequency axis */ void silk_warped_autocorrelation_FLP( silk_float *corr, /* O Result [order + 1] */ const silk_float *input, /* I Input data to correlate */ const silk_float warping, /* I Warping coefficient */ const opus_int length, /* I Length of input */ const opus_int order /* I Correlation order (even) */ ) { opus_int n, i; double tmp1, tmp2; double state[ MAX_SHAPE_LPC_ORDER + 1 ] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; double C[ MAX_SHAPE_LPC_ORDER + 1 ] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; /* Order must be even */ silk_assert( ( order & 1 ) == 0 ); /* Loop over samples */ for( n = 0; n < length; n++ ) { tmp1 = input[ n ]; /* Loop over allpass sections */ for( i = 0; i < order; i += 2 ) { /* Output of allpass section */ tmp2 = state[ i ] + warping * ( state[ i + 1 ] - tmp1 ); state[ i ] = tmp1; C[ i ] += state[ 0 ] * tmp1; /* Output of allpass section */ tmp1 = state[ i + 1 ] + warping * ( state[ i + 2 ] - tmp2 ); state[ i + 1 ] = tmp2; C[ i + 1 ] += state[ 0 ] * tmp2; } state[ order ] = tmp1; C[ order ] += state[ 0 ] * tmp1; } /* Copy correlations in silk_float output format */ for( i = 0; i < order + 1; i++ ) { corr[ i ] = ( silk_float )C[ i ]; } }
/* Shell encoder, operates on one shell code frame of 16 pulses */ void silk_shell_encoder( ec_enc *psRangeEnc, /* I/O compressor data structure */ const opus_int *pulses0 /* I data: nonnegative pulse amplitudes */ ) { opus_int pulses1[ 8 ], pulses2[ 4 ], pulses3[ 2 ], pulses4[ 1 ]; /* this function operates on one shell code frame of 16 pulses */ silk_assert( SHELL_CODEC_FRAME_LENGTH == 16 ); /* tree representation per pulse-subframe */ combine_pulses( pulses1, pulses0, 8 ); combine_pulses( pulses2, pulses1, 4 ); combine_pulses( pulses3, pulses2, 2 ); combine_pulses( pulses4, pulses3, 1 ); encode_split( psRangeEnc, pulses3[ 0 ], pulses4[ 0 ], silk_shell_code_table3 ); encode_split( psRangeEnc, pulses2[ 0 ], pulses3[ 0 ], silk_shell_code_table2 ); encode_split( psRangeEnc, pulses1[ 0 ], pulses2[ 0 ], silk_shell_code_table1 ); encode_split( psRangeEnc, pulses0[ 0 ], pulses1[ 0 ], silk_shell_code_table0 ); encode_split( psRangeEnc, pulses0[ 2 ], pulses1[ 1 ], silk_shell_code_table0 ); encode_split( psRangeEnc, pulses1[ 2 ], pulses2[ 1 ], silk_shell_code_table1 ); encode_split( psRangeEnc, pulses0[ 4 ], pulses1[ 2 ], silk_shell_code_table0 ); encode_split( psRangeEnc, pulses0[ 6 ], pulses1[ 3 ], silk_shell_code_table0 ); encode_split( psRangeEnc, pulses2[ 2 ], pulses3[ 1 ], silk_shell_code_table2 ); encode_split( psRangeEnc, pulses1[ 4 ], pulses2[ 2 ], silk_shell_code_table1 ); encode_split( psRangeEnc, pulses0[ 8 ], pulses1[ 4 ], silk_shell_code_table0 ); encode_split( psRangeEnc, pulses0[ 10 ], pulses1[ 5 ], silk_shell_code_table0 ); encode_split( psRangeEnc, pulses1[ 6 ], pulses2[ 3 ], silk_shell_code_table1 ); encode_split( psRangeEnc, pulses0[ 12 ], pulses1[ 6 ], silk_shell_code_table0 ); encode_split( psRangeEnc, pulses0[ 14 ], pulses1[ 7 ], silk_shell_code_table0 ); }
/********************************************************************** * Function to solve linear equation Ax = b, when A is a MxM * symmetric square matrix - using LDL factorisation **********************************************************************/ void silk_solve_LDL_FLP( silk_float *A, /* I/O Symmetric square matrix, out: reg. */ const opus_int M, /* I Size of matrix */ const silk_float *b, /* I Pointer to b vector */ silk_float *x /* O Pointer to x solution vector */ ) { opus_int i; silk_float L[ MAX_MATRIX_SIZE ][ MAX_MATRIX_SIZE ]; silk_float T[ MAX_MATRIX_SIZE ]; silk_float Dinv[ MAX_MATRIX_SIZE ]; /* inverse diagonal elements of D*/ silk_assert(M <= MAX_MATRIX_SIZE); /*************************************************** Factorize A by LDL such that A = L*D*(L^T), where L is lower triangular with ones on diagonal ****************************************************/ silk_LDL_FLP(A, M, &L[ 0 ][ 0 ], Dinv); /**************************************************** * substitute D*(L^T) = T. ie: L*D*(L^T)*x = b => L*T = b <=> T = inv(L)*b ******************************************************/ silk_SolveWithLowerTriangularWdiagOnes_FLP(&L[ 0 ][ 0 ], M, b, T); /**************************************************** D*(L^T)*x = T <=> (L^T)*x = inv(D)*T, because D is diagonal just multiply with 1/d_i ****************************************************/ for(i = 0; i < M; i++) { T[ i ] = T[ i ] * Dinv[ i ]; } /**************************************************** x = inv(L') * inv(D) * T *****************************************************/ silk_SolveWithUpperTriangularFromLowerWdiagOnes_FLP(&L[ 0 ][ 0 ], M, T, x); }