diff options
author | Jean-Marc Valin <jmvalin@jmvalin.ca> | 2011-10-20 08:39:41 +0400 |
---|---|---|
committer | Jean-Marc Valin <jmvalin@jmvalin.ca> | 2011-10-20 08:39:41 +0400 |
commit | 294bfec27b82f879e3c3004d31bb91bcb34014f4 (patch) | |
tree | 94a6332cc1df41ec84b453ef76ee8118b95bfd8f /silk/float | |
parent | dbf2ea841e5b022f0b6d15a606dd7288f25c35dd (diff) |
Implements hard CBR for SILK
This is achieved by running the encoding process in a loop and
padding when we don't reach the exact rate. It also implements
VBR-with-cap, which means we no longer need to artificially decrease
the SILK bandwidth when it's close to the cap.
Diffstat (limited to 'silk/float')
-rw-r--r-- | silk/float/encode_frame_FLP.c | 161 | ||||
-rw-r--r-- | silk/float/find_pred_coefs_FLP.c | 2 | ||||
-rw-r--r-- | silk/float/inner_product_FLP.c | 12 | ||||
-rw-r--r-- | silk/float/main_FLP.h | 4 | ||||
-rw-r--r-- | silk/float/pitch_analysis_core_FLP.c | 6 | ||||
-rw-r--r-- | silk/float/process_gains_FLP.c | 4 | ||||
-rw-r--r-- | silk/float/silk_float.vcxproj | 2 | ||||
-rw-r--r-- | silk/float/structs_FLP.h | 4 |
8 files changed, 159 insertions, 36 deletions
diff --git a/silk/float/encode_frame_FLP.c b/silk/float/encode_frame_FLP.c index 6c9e322e..a695d8a1 100644 --- a/silk/float/encode_frame_FLP.c +++ b/silk/float/encode_frame_FLP.c @@ -82,17 +82,31 @@ opus_int silk_encode_frame_FLP( silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ opus_int32 *pnBytesOut, /* O Number of payload bytes */ ec_enc *psRangeEnc, /* I/O compressor data structure */ - opus_int condCoding /* I The type of conditional coding to use */ + opus_int condCoding, /* I The type of conditional coding to use */ + opus_int maxBits, /* I If > 0: maximum number of output bits */ + opus_int useCBR /* I Flag to force constant-bitrate operation */ ) { silk_encoder_control_FLP sEncCtrl; - opus_int i, ret = 0; + opus_int i, iter, maxIter, found_upper, found_lower, ret = 0; silk_float *x_frame, *res_pitch_frame; silk_float xfw[ MAX_FRAME_LENGTH ]; silk_float res_pitch[ 2 * MAX_FRAME_LENGTH + LA_PITCH_MAX ]; + ec_enc sRangeEnc_copy, sRangeEnc_copy2; + silk_nsq_state sNSQ_copy, sNSQ_copy2; + opus_int32 seed_copy, nBits, nBits_lower, nBits_upper, gainMult_Q10, gainMult_lower, gainMult_upper; + opus_int16 ec_prevLagIndex_copy; + opus_int ec_prevSignalType_copy; + opus_int8 LastGainIndex_copy2; + opus_int32 pGains_Q16[ MAX_NB_SUBFR ]; + opus_uint8 ec_buf_copy[ 512 ]; TIC(ENCODE_FRAME) + /* This is totally unnecessary but many compilers (including gcc) are too dumb + to realise it */ + LastGainIndex_copy2 = nBits_lower = nBits_upper = gainMult_lower = gainMult_upper = 0; + psEnc->sCmn.indices.Seed = psEnc->sCmn.frameCounter++ & 3; /**************************************************************/ @@ -159,12 +173,128 @@ TIC(LBRR) silk_LBRR_encode_FLP( psEnc, &sEncCtrl, xfw, condCoding ); TOC(LBRR) - /*****************************************/ - /* Noise shaping quantization */ - /*****************************************/ + if ( psEnc->sCmn.prefillFlag ) + { TIC(NSQ) - silk_NSQ_wrapper_FLP( psEnc, &sEncCtrl, &psEnc->sCmn.indices, &psEnc->sCmn.sNSQ, psEnc->sCmn.pulses, xfw ); + silk_NSQ_wrapper_FLP( psEnc, &sEncCtrl, &psEnc->sCmn.indices, &psEnc->sCmn.sNSQ, psEnc->sCmn.pulses, xfw ); TOC(NSQ) + } else { + /* Loop over quantizer and entroy coding to control bitrate */ + maxIter = 5; + gainMult_Q10 = SILK_FIX_CONST( 1, 10 ); + found_lower = 0; + found_upper = 0; + for( iter = 0; ; iter++ ) { + /* Copy part of the input state */ + silk_memcpy( &sRangeEnc_copy, psRangeEnc, sizeof( ec_enc ) ); + silk_memcpy( &sNSQ_copy, &psEnc->sCmn.sNSQ, sizeof( silk_nsq_state ) ); + seed_copy = psEnc->sCmn.indices.Seed; + ec_prevLagIndex_copy = psEnc->sCmn.ec_prevLagIndex; + ec_prevSignalType_copy = psEnc->sCmn.ec_prevSignalType; + + /*****************************************/ + /* Noise shaping quantization */ + /*****************************************/ +TIC(NSQ) + silk_NSQ_wrapper_FLP( psEnc, &sEncCtrl, &psEnc->sCmn.indices, &psEnc->sCmn.sNSQ, psEnc->sCmn.pulses, xfw ); +TOC(NSQ) + + /****************************************/ + /* Encode Parameters */ + /****************************************/ +TIC(ENCODE_PARAMS) + silk_encode_indices( &psEnc->sCmn, psRangeEnc, psEnc->sCmn.nFramesEncoded, 0, condCoding ); +TOC(ENCODE_PARAMS) + + /****************************************/ + /* Encode Excitation Signal */ + /****************************************/ +TIC(ENCODE_PULSES) + silk_encode_pulses( psRangeEnc, psEnc->sCmn.indices.signalType, psEnc->sCmn.indices.quantOffsetType, + psEnc->sCmn.pulses, psEnc->sCmn.frame_length ); +TOC(ENCODE_PULSES) + + nBits = ec_tell( psRangeEnc ); + + if( useCBR == 0 && iter == 0 && nBits <= maxBits ) { + break; + } + + if( iter == maxIter ) { + if( nBits > maxBits && found_lower ) { + /* Restore output state from earlier iteration that did meet the bitrate budget */ + silk_memcpy( psRangeEnc, &sRangeEnc_copy2, sizeof( ec_enc ) ); + silk_memcpy( psRangeEnc->buf, ec_buf_copy, sRangeEnc_copy2.offs ); + silk_memcpy( &psEnc->sCmn.sNSQ, &sNSQ_copy2, sizeof( silk_nsq_state ) ); + psEnc->sShape.LastGainIndex = LastGainIndex_copy2; + } + break; + } + + if( nBits > maxBits ) { + found_upper = 1; + nBits_upper = nBits; + gainMult_upper = gainMult_Q10; + if( found_lower == 0 && iter >= 3 ) { + /* Adjust the quantizer's rate/distortion tradeoff */ + sEncCtrl.Lambda *= 1.5f; + } + } else if( nBits < maxBits - 5 ) { + found_lower = 1; + nBits_lower = nBits; + gainMult_lower = gainMult_Q10; + /* Copy part of the output state */ + silk_memcpy( &sRangeEnc_copy2, psRangeEnc, sizeof( ec_enc ) ); + silk_memcpy( ec_buf_copy, psRangeEnc->buf, psRangeEnc->offs ); + silk_memcpy( &sNSQ_copy2, &psEnc->sCmn.sNSQ, sizeof( silk_nsq_state ) ); + LastGainIndex_copy2 = psEnc->sShape.LastGainIndex; + } else { + /* Within 5 bits of budget: close enough */ + break; + } + + if( ( found_lower & found_upper ) == 0 ) { + /* Adjust gain according to high-rate rate/distortion curve */ + opus_int32 gain_factor_Q16;
+ gain_factor_Q16 = silk_log2lin( silk_LSHIFT( nBits - maxBits, 7 ) / psEnc->sCmn.frame_length + SILK_FIX_CONST( 16, 7 ) );
+ if( nBits > maxBits ) { + gain_factor_Q16 = silk_max_32( gain_factor_Q16, SILK_FIX_CONST( 1.3, 16 ) ); + } + gainMult_Q10 = silk_SMULWW( gainMult_Q10, gain_factor_Q16 );
+ } else { + /* Adjust gain by interpolating */ + gainMult_Q10 = gainMult_lower + ( ( gainMult_upper - gainMult_lower ) * ( maxBits - nBits_lower ) ) / ( nBits_upper - nBits_lower ); + /* New gain multplier must be between 25% and 75% of old range (note that gainMult_upper < gainMult_lower) */ + if( gainMult_Q10 > gainMult_lower + silk_RSHIFT32( gainMult_upper - gainMult_lower, 2 ) ) { + gainMult_Q10 = gainMult_lower + silk_RSHIFT32( gainMult_upper - gainMult_lower, 2 ); + } else + if( gainMult_Q10 < gainMult_upper - silk_RSHIFT32( gainMult_upper - gainMult_lower, 2 ) ) { + gainMult_Q10 = gainMult_upper - silk_RSHIFT32( gainMult_upper - gainMult_lower, 2 ); + } + } + + for( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) { + pGains_Q16[ i ] = silk_LSHIFT_SAT32( silk_SMULWW( sEncCtrl.GainsUnq_Q16[ i ], gainMult_Q10 ), 6 ); + } + psEnc->sShape.LastGainIndex = sEncCtrl.lastGainIndexPrev; + + /* Noise shaping quantization */ + silk_gains_quant( psEnc->sCmn.indices.GainsIndices, pGains_Q16, + &psEnc->sShape.LastGainIndex, condCoding == CODE_CONDITIONALLY, psEnc->sCmn.nb_subfr ); + + /* Overwrite unquantized gains with quantized gains and convert back to Q0 from Q16 */ + for( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) { + sEncCtrl.Gains[ i ] = pGains_Q16[ i ] / 65536.0f; + } + + /* Restore part of the input state */ + silk_memcpy( psRangeEnc, &sRangeEnc_copy, sizeof( ec_enc ) ); + silk_memcpy( &psEnc->sCmn.sNSQ, &sNSQ_copy, sizeof( silk_nsq_state ) ); + psEnc->sCmn.indices.Seed = seed_copy; + psEnc->sCmn.ec_prevLagIndex = ec_prevLagIndex_copy; + psEnc->sCmn.ec_prevSignalType = ec_prevSignalType_copy; + } + } /* Update input buffer */ silk_memmove( psEnc->x_buf, &psEnc->x_buf[ psEnc->sCmn.frame_length ], @@ -182,31 +312,15 @@ TOC(NSQ) } /****************************************/ - /* Encode Parameters */ - /****************************************/ -TIC(ENCODE_PARAMS) - silk_encode_indices( &psEnc->sCmn, psRangeEnc, psEnc->sCmn.nFramesEncoded, 0, condCoding ); -TOC(ENCODE_PARAMS) - - /****************************************/ - /* Encode Excitation Signal */ - /****************************************/ -TIC(ENCODE_PULSES) - silk_encode_pulses( psRangeEnc, psEnc->sCmn.indices.signalType, psEnc->sCmn.indices.quantOffsetType, - psEnc->sCmn.pulses, psEnc->sCmn.frame_length ); -TOC(ENCODE_PULSES) - - /****************************************/ /* Finalize payload */ /****************************************/ psEnc->sCmn.first_frame_after_reset = 0; /* Payload size */ *pnBytesOut = silk_RSHIFT( ec_tell( psRangeEnc ) + 7, 3 ); + TOC(ENCODE_FRAME) #ifdef SAVE_ALL_INTERNAL_DATA - /*DEBUG_STORE_DATA( xf.dat, pIn_HP_LP, psEnc->sCmn.frame_length * sizeof( opus_int16 ) );*/ - /*DEBUG_STORE_DATA( xfw.dat, xfw, psEnc->sCmn.frame_length * sizeof( silk_float ) );*/ DEBUG_STORE_DATA( pitchL.dat, sEncCtrl.pitchL, MAX_NB_SUBFR * sizeof( opus_int ) ); DEBUG_STORE_DATA( pitchG_quantized.dat, sEncCtrl.LTPCoef, psEnc->sCmn.nb_subfr * LTP_ORDER * sizeof( silk_float ) ); DEBUG_STORE_DATA( LTPcorr.dat, &psEnc->LTPCorr, sizeof( silk_float ) ); @@ -220,7 +334,6 @@ TOC(ENCODE_FRAME) DEBUG_STORE_DATA( per_index.dat, &psEnc->sCmn.indices.PERIndex, sizeof( opus_int8 ) ); DEBUG_STORE_DATA( PredCoef.dat, &sEncCtrl.PredCoef[ 1 ], psEnc->sCmn.predictLPCOrder * sizeof( silk_float ) ); DEBUG_STORE_DATA( ltp_scale_idx.dat, &psEnc->sCmn.indices.LTP_scaleIndex, sizeof( opus_int8 ) ); - /*DEBUG_STORE_DATA( xq.dat, psEnc->sCmn.sNSQ.xqBuf, psEnc->sCmn.frame_length * sizeof( silk_float ) );*/ #endif return ret; } diff --git a/silk/float/find_pred_coefs_FLP.c b/silk/float/find_pred_coefs_FLP.c index 04881855..90a4e467 100644 --- a/silk/float/find_pred_coefs_FLP.c +++ b/silk/float/find_pred_coefs_FLP.c @@ -112,7 +112,7 @@ TOC(LSF_quant); silk_residual_energy_FLP( psEncCtrl->ResNrg, LPC_in_pre, psEncCtrl->PredCoef, psEncCtrl->Gains, psEnc->sCmn.subfr_length, psEnc->sCmn.nb_subfr, psEnc->sCmn.predictLPCOrder ); - /* Copy to prediction struct for use in next frame for fluctuation reduction */ + /* Copy to prediction struct for use in next frame for interpolation */ silk_memcpy( psEnc->sCmn.prev_NLSFq_Q15, NLSF_Q15, sizeof( psEnc->sCmn.prev_NLSFq_Q15 ) ); } diff --git a/silk/float/inner_product_FLP.c b/silk/float/inner_product_FLP.c index 4ed56a35..a8f04d73 100644 --- a/silk/float/inner_product_FLP.c +++ b/silk/float/inner_product_FLP.c @@ -42,18 +42,18 @@ double silk_inner_product_FLP( /* O result */ double result; /* 4x unrolled loop */ - result = 0.0f; + result = 0.0; dataSize4 = dataSize & 0xFFFC; for( i = 0; i < dataSize4; i += 4 ) { - result += data1[ i + 0 ] * data2[ i + 0 ] + - data1[ i + 1 ] * data2[ i + 1 ] + - data1[ i + 2 ] * data2[ i + 2 ] + - data1[ i + 3 ] * data2[ i + 3 ]; + result += data1[ i + 0 ] * (double)data2[ i + 0 ] + + data1[ i + 1 ] * (double)data2[ i + 1 ] + + data1[ i + 2 ] * (double)data2[ i + 2 ] + + data1[ i + 3 ] * (double)data2[ i + 3 ]; } /* add any remaining products */ for( ; i < dataSize; i++ ) { - result += data1[ i ] * data2[ i ]; + result += data1[ i ] * (double)data2[ i ]; } return result; diff --git a/silk/float/main_FLP.h b/silk/float/main_FLP.h index 20a766b1..07e31ab6 100644 --- a/silk/float/main_FLP.h +++ b/silk/float/main_FLP.h @@ -64,7 +64,9 @@ opus_int silk_encode_frame_FLP( silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ opus_int32 *pnBytesOut, /* O Number of payload bytes; */ ec_enc *psRangeEnc, /* I/O compressor data structure */ - opus_int condCoding /* I The type of conditional coding to use */ + opus_int condCoding, /* I The type of conditional coding to use */ + opus_int maxBits, /* I If > 0: maximum number of output bits */ + opus_int useCBR /* I Flag to force constant-bitrate operation */ ); /* Initializes the Silk encoder state */ diff --git a/silk/float/pitch_analysis_core_FLP.c b/silk/float/pitch_analysis_core_FLP.c index 8e661590..4108d63d 100644 --- a/silk/float/pitch_analysis_core_FLP.c +++ b/silk/float/pitch_analysis_core_FLP.c @@ -202,8 +202,8 @@ opus_int silk_pitch_analysis_core_FLP( /* O voicing estimate: 0 voiced, 1 unvoic /* Add contribution of new sample and remove contribution from oldest sample */ normalizer += - basis_ptr[ 0 ] * basis_ptr[ 0 ] - - basis_ptr[ sf_length_8kHz ] * basis_ptr[ sf_length_8kHz ]; + basis_ptr[ 0 ] * (double)basis_ptr[ 0 ] - + basis_ptr[ sf_length_8kHz ] * (double)basis_ptr[ sf_length_8kHz ]; C[ 0 ][ d ] += (silk_float)(cross_corr / sqrt( normalizer )); } /* Update target pointer */ @@ -225,7 +225,7 @@ opus_int silk_pitch_analysis_core_FLP( /* O voicing estimate: 0 voiced, 1 unvoic target_ptr = &frame_4kHz[ silk_SMULBB( sf_length_4kHz, nb_subfr ) ]; energy = 1000.0f; for( i = 0; i < silk_LSHIFT( sf_length_4kHz, 2 ); i++ ) { - energy += target_ptr[i] * target_ptr[i]; + energy += target_ptr[i] * (double)target_ptr[i]; } threshold = Cmax * Cmax; if( energy / 16.0f > threshold ) { diff --git a/silk/float/process_gains_FLP.c b/silk/float/process_gains_FLP.c index 2c7dfdd0..0708a5c2 100644 --- a/silk/float/process_gains_FLP.c +++ b/silk/float/process_gains_FLP.c @@ -67,6 +67,10 @@ void silk_process_gains_FLP( pGains_Q16[ k ] = ( opus_int32 ) ( psEncCtrl->Gains[ k ] * 65536.0f ); } + /* Save unquantized gains and gain Index */ + silk_memcpy( psEncCtrl->GainsUnq_Q16, pGains_Q16, psEnc->sCmn.nb_subfr * sizeof( opus_int32 ) ); + psEncCtrl->lastGainIndexPrev = psShapeSt->LastGainIndex; + /* Noise shaping quantization */ silk_gains_quant( psEnc->sCmn.indices.GainsIndices, pGains_Q16, &psShapeSt->LastGainIndex, condCoding == CODE_CONDITIONALLY, psEnc->sCmn.nb_subfr ); diff --git a/silk/float/silk_float.vcxproj b/silk/float/silk_float.vcxproj index abcdaf05..44539478 100644 --- a/silk/float/silk_float.vcxproj +++ b/silk/float/silk_float.vcxproj @@ -127,4 +127,4 @@ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
-</Project>
+</Project>
\ No newline at end of file diff --git a/silk/float/structs_FLP.h b/silk/float/structs_FLP.h index 99264c97..dd776b5c 100644 --- a/silk/float/structs_FLP.h +++ b/silk/float/structs_FLP.h @@ -107,6 +107,10 @@ typedef struct { silk_float predGain; silk_float LTPredCodGain; silk_float ResNrg[ MAX_NB_SUBFR ]; /* Residual energy per subframe */ + + /* Parameters for CBR mode */ + opus_int32 GainsUnq_Q16[ MAX_NB_SUBFR ]; + opus_int8 lastGainIndexPrev; } silk_encoder_control_FLP; /************************/ |