/src/opus/silk/fixed/encode_frame_FIX.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*********************************************************************** |
2 | | Copyright (c) 2006-2011, Skype Limited. All rights reserved. |
3 | | Redistribution and use in source and binary forms, with or without |
4 | | modification, are permitted provided that the following conditions |
5 | | are met: |
6 | | - Redistributions of source code must retain the above copyright notice, |
7 | | this list of conditions and the following disclaimer. |
8 | | - Redistributions in binary form must reproduce the above copyright |
9 | | notice, this list of conditions and the following disclaimer in the |
10 | | documentation and/or other materials provided with the distribution. |
11 | | - Neither the name of Internet Society, IETF or IETF Trust, nor the |
12 | | names of specific contributors, may be used to endorse or promote |
13 | | products derived from this software without specific prior written |
14 | | permission. |
15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 | | POSSIBILITY OF SUCH DAMAGE. |
26 | | ***********************************************************************/ |
27 | | |
28 | | #ifdef HAVE_CONFIG_H |
29 | | #include "config.h" |
30 | | #endif |
31 | | |
32 | | #include <stdlib.h> |
33 | | #include "main_FIX.h" |
34 | | #include "stack_alloc.h" |
35 | | #include "tuning_parameters.h" |
36 | | |
37 | | /* Low Bitrate Redundancy (LBRR) encoding. Reuse all parameters but encode with lower bitrate */ |
38 | | static OPUS_INLINE void silk_LBRR_encode_FIX( |
39 | | silk_encoder_state_FIX *psEnc, /* I/O Pointer to Silk FIX encoder state */ |
40 | | silk_encoder_control_FIX *psEncCtrl, /* I/O Pointer to Silk FIX encoder control struct */ |
41 | | const opus_int16 x16[], /* I Input signal */ |
42 | | opus_int condCoding /* I The type of conditional coding used so far for this frame */ |
43 | | ); |
44 | | |
45 | | void silk_encode_do_VAD_FIX( |
46 | | silk_encoder_state_FIX *psEnc, /* I/O Pointer to Silk FIX encoder state */ |
47 | | opus_int activity /* I Decision of Opus voice activity detector */ |
48 | | ) |
49 | 240k | { |
50 | 240k | const opus_int activity_threshold = SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ); |
51 | | |
52 | | /****************************/ |
53 | | /* Voice Activity Detection */ |
54 | | /****************************/ |
55 | 240k | silk_VAD_GetSA_Q8( &psEnc->sCmn, psEnc->sCmn.inputBuf + 1, psEnc->sCmn.arch ); |
56 | | /* If Opus VAD is inactive and Silk VAD is active: lower Silk VAD to just under the threshold */ |
57 | 240k | if( activity == VAD_NO_ACTIVITY && psEnc->sCmn.speech_activity_Q8 >= activity_threshold ) { |
58 | 228 | psEnc->sCmn.speech_activity_Q8 = activity_threshold - 1; |
59 | 228 | } |
60 | | |
61 | | /**************************************************/ |
62 | | /* Convert speech activity into VAD and DTX flags */ |
63 | | /**************************************************/ |
64 | 240k | if( psEnc->sCmn.speech_activity_Q8 < activity_threshold ) { |
65 | 48.1k | psEnc->sCmn.indices.signalType = TYPE_NO_VOICE_ACTIVITY; |
66 | 48.1k | psEnc->sCmn.noSpeechCounter++; |
67 | 48.1k | if( psEnc->sCmn.noSpeechCounter <= NB_SPEECH_FRAMES_BEFORE_DTX ) { |
68 | 48.1k | psEnc->sCmn.inDTX = 0; |
69 | 48.1k | } else if( psEnc->sCmn.noSpeechCounter > MAX_CONSECUTIVE_DTX + NB_SPEECH_FRAMES_BEFORE_DTX ) { |
70 | 0 | psEnc->sCmn.noSpeechCounter = NB_SPEECH_FRAMES_BEFORE_DTX; |
71 | 0 | psEnc->sCmn.inDTX = 0; |
72 | 0 | } |
73 | 48.1k | psEnc->sCmn.VAD_flags[ psEnc->sCmn.nFramesEncoded ] = 0; |
74 | 192k | } else { |
75 | 192k | psEnc->sCmn.noSpeechCounter = 0; |
76 | 192k | psEnc->sCmn.inDTX = 0; |
77 | 192k | psEnc->sCmn.indices.signalType = TYPE_UNVOICED; |
78 | 192k | psEnc->sCmn.VAD_flags[ psEnc->sCmn.nFramesEncoded ] = 1; |
79 | 192k | } |
80 | 240k | } |
81 | | |
82 | | /****************/ |
83 | | /* Encode frame */ |
84 | | /****************/ |
85 | | opus_int silk_encode_frame_FIX( |
86 | | silk_encoder_state_FIX *psEnc, /* I/O Pointer to Silk FIX encoder state */ |
87 | | opus_int32 *pnBytesOut, /* O Pointer to number of payload bytes; */ |
88 | | ec_enc *psRangeEnc, /* I/O compressor data structure */ |
89 | | opus_int condCoding, /* I The type of conditional coding to use */ |
90 | | opus_int maxBits, /* I If > 0: maximum number of output bits */ |
91 | | opus_int useCBR /* I Flag to force constant-bitrate operation */ |
92 | | ) |
93 | 240k | { |
94 | 240k | silk_encoder_control_FIX sEncCtrl; |
95 | 240k | opus_int i, iter, maxIter, found_upper, found_lower, ret = 0; |
96 | 240k | opus_int16 *x_frame; |
97 | 240k | ec_enc sRangeEnc_copy, sRangeEnc_copy2; |
98 | 240k | silk_nsq_state sNSQ_copy, sNSQ_copy2; |
99 | 240k | opus_int32 seed_copy, nBits, nBits_lower, nBits_upper, gainMult_lower, gainMult_upper; |
100 | 240k | opus_int32 gainsID, gainsID_lower, gainsID_upper; |
101 | 240k | opus_int16 gainMult_Q8; |
102 | 240k | opus_int16 ec_prevLagIndex_copy; |
103 | 240k | opus_int ec_prevSignalType_copy; |
104 | 240k | opus_int8 LastGainIndex_copy2; |
105 | 240k | opus_int gain_lock[ MAX_NB_SUBFR ] = {0}; |
106 | 240k | opus_int16 best_gain_mult[ MAX_NB_SUBFR ]; |
107 | 240k | opus_int best_sum[ MAX_NB_SUBFR ]; |
108 | 240k | opus_int bits_margin; |
109 | 240k | SAVE_STACK; |
110 | | |
111 | | /* For CBR, 5 bits below budget is close enough. For VBR, allow up to 25% below the cap if we initially busted the budget. */ |
112 | 240k | bits_margin = useCBR ? 5 : maxBits/4; |
113 | | /* This is totally unnecessary but many compilers (including gcc) are too dumb to realise it */ |
114 | 240k | LastGainIndex_copy2 = nBits_lower = nBits_upper = gainMult_lower = gainMult_upper = 0; |
115 | | |
116 | 240k | psEnc->sCmn.indices.Seed = psEnc->sCmn.frameCounter++ & 3; |
117 | | |
118 | | /**************************************************************/ |
119 | | /* Set up Input Pointers, and insert frame in input buffer */ |
120 | | /*************************************************************/ |
121 | | /* start of frame to encode */ |
122 | 240k | x_frame = psEnc->x_buf + psEnc->sCmn.ltp_mem_length; |
123 | | |
124 | | /***************************************/ |
125 | | /* Ensure smooth bandwidth transitions */ |
126 | | /***************************************/ |
127 | 240k | silk_LP_variable_cutoff( &psEnc->sCmn.sLP, psEnc->sCmn.inputBuf + 1, psEnc->sCmn.frame_length ); |
128 | | |
129 | | /*******************************************/ |
130 | | /* Copy new frame to front of input buffer */ |
131 | | /*******************************************/ |
132 | 240k | silk_memcpy( x_frame + LA_SHAPE_MS * psEnc->sCmn.fs_kHz, psEnc->sCmn.inputBuf + 1, psEnc->sCmn.frame_length * sizeof( opus_int16 ) ); |
133 | | |
134 | 240k | if( !psEnc->sCmn.prefillFlag ) { |
135 | 240k | VARDECL( opus_int16, res_pitch ); |
136 | 240k | VARDECL( opus_uint8, ec_buf_copy ); |
137 | 240k | opus_int16 *res_pitch_frame; |
138 | | |
139 | 240k | ALLOC( res_pitch, |
140 | 240k | psEnc->sCmn.la_pitch + psEnc->sCmn.frame_length |
141 | 240k | + psEnc->sCmn.ltp_mem_length, opus_int16 ); |
142 | | /* start of pitch LPC residual frame */ |
143 | 240k | res_pitch_frame = res_pitch + psEnc->sCmn.ltp_mem_length; |
144 | | |
145 | | /*****************************************/ |
146 | | /* Find pitch lags, initial LPC analysis */ |
147 | | /*****************************************/ |
148 | 240k | silk_find_pitch_lags_FIX( psEnc, &sEncCtrl, res_pitch, x_frame - psEnc->sCmn.ltp_mem_length, psEnc->sCmn.arch ); |
149 | | |
150 | | /************************/ |
151 | | /* Noise shape analysis */ |
152 | | /************************/ |
153 | 240k | silk_noise_shape_analysis_FIX( psEnc, &sEncCtrl, res_pitch_frame, x_frame, psEnc->sCmn.arch ); |
154 | | |
155 | | /***************************************************/ |
156 | | /* Find linear prediction coefficients (LPC + LTP) */ |
157 | | /***************************************************/ |
158 | 240k | silk_find_pred_coefs_FIX( psEnc, &sEncCtrl, res_pitch_frame, x_frame, condCoding ); |
159 | | |
160 | | /****************************************/ |
161 | | /* Process gains */ |
162 | | /****************************************/ |
163 | 240k | silk_process_gains_FIX( psEnc, &sEncCtrl, condCoding ); |
164 | | |
165 | | /****************************************/ |
166 | | /* Low Bitrate Redundant Encoding */ |
167 | | /****************************************/ |
168 | 240k | silk_LBRR_encode_FIX( psEnc, &sEncCtrl, x_frame, condCoding ); |
169 | | |
170 | | /* Loop over quantizer and entropy coding to control bitrate */ |
171 | 240k | maxIter = 6; |
172 | 240k | gainMult_Q8 = SILK_FIX_CONST( 1, 8 ); |
173 | 240k | found_lower = 0; |
174 | 240k | found_upper = 0; |
175 | 240k | gainsID = silk_gains_ID( psEnc->sCmn.indices.GainsIndices, psEnc->sCmn.nb_subfr ); |
176 | 240k | gainsID_lower = -1; |
177 | 240k | gainsID_upper = -1; |
178 | | /* Copy part of the input state */ |
179 | 240k | silk_memcpy( &sRangeEnc_copy, psRangeEnc, sizeof( ec_enc ) ); |
180 | 240k | silk_memcpy( &sNSQ_copy, &psEnc->sCmn.sNSQ, sizeof( silk_nsq_state ) ); |
181 | 240k | seed_copy = psEnc->sCmn.indices.Seed; |
182 | 240k | ec_prevLagIndex_copy = psEnc->sCmn.ec_prevLagIndex; |
183 | 240k | ec_prevSignalType_copy = psEnc->sCmn.ec_prevSignalType; |
184 | 240k | ALLOC( ec_buf_copy, 1275, opus_uint8 ); |
185 | 745k | for( iter = 0; ; iter++ ) { |
186 | 745k | if( gainsID == gainsID_lower ) { |
187 | 103k | nBits = nBits_lower; |
188 | 642k | } else if( gainsID == gainsID_upper ) { |
189 | 17.7k | nBits = nBits_upper; |
190 | 624k | } else { |
191 | | /* Restore part of the input state */ |
192 | 624k | if( iter > 0 ) { |
193 | 384k | silk_memcpy( psRangeEnc, &sRangeEnc_copy, sizeof( ec_enc ) ); |
194 | 384k | silk_memcpy( &psEnc->sCmn.sNSQ, &sNSQ_copy, sizeof( silk_nsq_state ) ); |
195 | 384k | psEnc->sCmn.indices.Seed = seed_copy; |
196 | 384k | psEnc->sCmn.ec_prevLagIndex = ec_prevLagIndex_copy; |
197 | 384k | psEnc->sCmn.ec_prevSignalType = ec_prevSignalType_copy; |
198 | 384k | } |
199 | | |
200 | | /*****************************************/ |
201 | | /* Noise shaping quantization */ |
202 | | /*****************************************/ |
203 | 624k | if( psEnc->sCmn.nStatesDelayedDecision > 1 || psEnc->sCmn.warping_Q16 > 0 ) { |
204 | 465k | silk_NSQ_del_dec( &psEnc->sCmn, &psEnc->sCmn.sNSQ, &psEnc->sCmn.indices, x_frame, psEnc->sCmn.pulses, |
205 | 465k | sEncCtrl.PredCoef_Q12[ 0 ], sEncCtrl.LTPCoef_Q14, sEncCtrl.AR_Q13, sEncCtrl.HarmShapeGain_Q14, |
206 | 465k | sEncCtrl.Tilt_Q14, sEncCtrl.LF_shp_Q14, sEncCtrl.Gains_Q16, sEncCtrl.pitchL, sEncCtrl.Lambda_Q10, sEncCtrl.LTP_scale_Q14, |
207 | 465k | psEnc->sCmn.arch ); |
208 | 465k | } else { |
209 | 159k | silk_NSQ( &psEnc->sCmn, &psEnc->sCmn.sNSQ, &psEnc->sCmn.indices, x_frame, psEnc->sCmn.pulses, |
210 | 159k | sEncCtrl.PredCoef_Q12[ 0 ], sEncCtrl.LTPCoef_Q14, sEncCtrl.AR_Q13, sEncCtrl.HarmShapeGain_Q14, |
211 | 159k | sEncCtrl.Tilt_Q14, sEncCtrl.LF_shp_Q14, sEncCtrl.Gains_Q16, sEncCtrl.pitchL, sEncCtrl.Lambda_Q10, sEncCtrl.LTP_scale_Q14, |
212 | 159k | psEnc->sCmn.arch); |
213 | 159k | } |
214 | | |
215 | 624k | if ( iter == maxIter && !found_lower ) { |
216 | 16.9k | silk_memcpy( &sRangeEnc_copy2, psRangeEnc, sizeof( ec_enc ) ); |
217 | 16.9k | } |
218 | | |
219 | | /****************************************/ |
220 | | /* Encode Parameters */ |
221 | | /****************************************/ |
222 | 624k | silk_encode_indices( &psEnc->sCmn, psRangeEnc, psEnc->sCmn.nFramesEncoded, 0, condCoding ); |
223 | | |
224 | | /****************************************/ |
225 | | /* Encode Excitation Signal */ |
226 | | /****************************************/ |
227 | 624k | silk_encode_pulses( psRangeEnc, psEnc->sCmn.indices.signalType, psEnc->sCmn.indices.quantOffsetType, |
228 | 624k | psEnc->sCmn.pulses, psEnc->sCmn.frame_length ); |
229 | | |
230 | 624k | nBits = ec_tell( psRangeEnc ); |
231 | | |
232 | | /* If we still bust after the last iteration, do some damage control. */ |
233 | 624k | if ( iter == maxIter && !found_lower && nBits > maxBits ) { |
234 | 15.8k | silk_memcpy( psRangeEnc, &sRangeEnc_copy2, sizeof( ec_enc ) ); |
235 | | |
236 | | /* Keep gains the same as the last frame. */ |
237 | 15.8k | psEnc->sShape.LastGainIndex = sEncCtrl.lastGainIndexPrev; |
238 | 78.9k | for ( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) { |
239 | 63.1k | psEnc->sCmn.indices.GainsIndices[ i ] = 4; |
240 | 63.1k | } |
241 | 15.8k | if (condCoding != CODE_CONDITIONALLY) { |
242 | 13.1k | psEnc->sCmn.indices.GainsIndices[ 0 ] = sEncCtrl.lastGainIndexPrev; |
243 | 13.1k | } |
244 | 15.8k | psEnc->sCmn.ec_prevLagIndex = ec_prevLagIndex_copy; |
245 | 15.8k | psEnc->sCmn.ec_prevSignalType = ec_prevSignalType_copy; |
246 | | /* Clear all pulses. */ |
247 | 3.15M | for ( i = 0; i < psEnc->sCmn.frame_length; i++ ) { |
248 | 3.13M | psEnc->sCmn.pulses[ i ] = 0; |
249 | 3.13M | } |
250 | | |
251 | 15.8k | silk_encode_indices( &psEnc->sCmn, psRangeEnc, psEnc->sCmn.nFramesEncoded, 0, condCoding ); |
252 | | |
253 | 15.8k | silk_encode_pulses( psRangeEnc, psEnc->sCmn.indices.signalType, psEnc->sCmn.indices.quantOffsetType, |
254 | 15.8k | psEnc->sCmn.pulses, psEnc->sCmn.frame_length ); |
255 | | |
256 | 15.8k | nBits = ec_tell( psRangeEnc ); |
257 | 15.8k | } |
258 | | |
259 | 624k | if( useCBR == 0 && iter == 0 && nBits <= maxBits ) { |
260 | 146k | break; |
261 | 146k | } |
262 | 624k | } |
263 | | |
264 | 598k | if( iter == maxIter ) { |
265 | 77.6k | if( found_lower && ( gainsID == gainsID_lower || nBits > maxBits ) ) { |
266 | | /* Restore output state from earlier iteration that did meet the bitrate budget */ |
267 | 28.0k | silk_memcpy( psRangeEnc, &sRangeEnc_copy2, sizeof( ec_enc ) ); |
268 | 28.0k | celt_assert( sRangeEnc_copy2.offs <= 1275 ); |
269 | 28.0k | silk_memcpy( psRangeEnc->buf, ec_buf_copy, sRangeEnc_copy2.offs ); |
270 | 28.0k | silk_memcpy( &psEnc->sCmn.sNSQ, &sNSQ_copy2, sizeof( silk_nsq_state ) ); |
271 | 28.0k | psEnc->sShape.LastGainIndex = LastGainIndex_copy2; |
272 | 28.0k | } |
273 | 77.6k | break; |
274 | 77.6k | } |
275 | | |
276 | 521k | if( nBits > maxBits ) { |
277 | 153k | if( found_lower == 0 && iter >= 2 ) { |
278 | | /* Adjust the quantizer's rate/distortion tradeoff and discard previous "upper" results */ |
279 | 75.1k | sEncCtrl.Lambda_Q10 = silk_ADD_RSHIFT32( sEncCtrl.Lambda_Q10, sEncCtrl.Lambda_Q10, 1 ); |
280 | 75.1k | found_upper = 0; |
281 | 75.1k | gainsID_upper = -1; |
282 | 78.3k | } else { |
283 | 78.3k | found_upper = 1; |
284 | 78.3k | nBits_upper = nBits; |
285 | 78.3k | gainMult_upper = gainMult_Q8; |
286 | 78.3k | gainsID_upper = gainsID; |
287 | 78.3k | } |
288 | 367k | } else if( nBits < maxBits - bits_margin ) { |
289 | 351k | found_lower = 1; |
290 | 351k | nBits_lower = nBits; |
291 | 351k | gainMult_lower = gainMult_Q8; |
292 | 351k | if( gainsID != gainsID_lower ) { |
293 | 269k | gainsID_lower = gainsID; |
294 | | /* Copy part of the output state */ |
295 | 269k | silk_memcpy( &sRangeEnc_copy2, psRangeEnc, sizeof( ec_enc ) ); |
296 | 269k | celt_assert( psRangeEnc->offs <= 1275 ); |
297 | 269k | silk_memcpy( ec_buf_copy, psRangeEnc->buf, psRangeEnc->offs ); |
298 | 269k | silk_memcpy( &sNSQ_copy2, &psEnc->sCmn.sNSQ, sizeof( silk_nsq_state ) ); |
299 | 269k | LastGainIndex_copy2 = psEnc->sShape.LastGainIndex; |
300 | 269k | } |
301 | 351k | } else { |
302 | | /* Close enough */ |
303 | 16.1k | break; |
304 | 16.1k | } |
305 | | |
306 | 504k | if ( !found_lower && nBits > maxBits ) { |
307 | 131k | int j; |
308 | 652k | for ( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) { |
309 | 521k | int sum=0; |
310 | 26.7M | for ( j = i*psEnc->sCmn.subfr_length; j < (i+1)*psEnc->sCmn.subfr_length; j++ ) { |
311 | 26.2M | sum += abs( psEnc->sCmn.pulses[j] ); |
312 | 26.2M | } |
313 | 521k | if ( iter == 0 || (sum < best_sum[i] && !gain_lock[i]) ) { |
314 | 238k | best_sum[i] = sum; |
315 | 238k | best_gain_mult[i] = gainMult_Q8; |
316 | 282k | } else { |
317 | 282k | gain_lock[i] = 1; |
318 | 282k | } |
319 | 521k | } |
320 | 131k | } |
321 | 504k | if( ( found_lower & found_upper ) == 0 ) { |
322 | | /* Adjust gain according to high-rate rate/distortion curve */ |
323 | 466k | if( nBits > maxBits ) { |
324 | 131k | gainMult_Q8 = silk_min_32( 1024, gainMult_Q8*3/2 ); |
325 | 335k | } else { |
326 | 335k | gainMult_Q8 = silk_max_32( 64, gainMult_Q8*4/5 ); |
327 | 335k | } |
328 | 466k | } else { |
329 | | /* Adjust gain by interpolating */ |
330 | 38.0k | gainMult_Q8 = gainMult_lower + silk_DIV32_16( silk_MUL( gainMult_upper - gainMult_lower, maxBits - nBits_lower ), nBits_upper - nBits_lower ); |
331 | | /* New gain multiplier must be between 25% and 75% of old range (note that gainMult_upper < gainMult_lower) */ |
332 | 38.0k | if( gainMult_Q8 > silk_ADD_RSHIFT32( gainMult_lower, gainMult_upper - gainMult_lower, 2 ) ) { |
333 | 5.70k | gainMult_Q8 = silk_ADD_RSHIFT32( gainMult_lower, gainMult_upper - gainMult_lower, 2 ); |
334 | 5.70k | } else |
335 | 32.3k | if( gainMult_Q8 < silk_SUB_RSHIFT32( gainMult_upper, gainMult_upper - gainMult_lower, 2 ) ) { |
336 | 9.94k | gainMult_Q8 = silk_SUB_RSHIFT32( gainMult_upper, gainMult_upper - gainMult_lower, 2 ); |
337 | 9.94k | } |
338 | 38.0k | } |
339 | | |
340 | 2.18M | for( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) { |
341 | 1.67M | opus_int16 tmp; |
342 | 1.67M | if ( gain_lock[i] ) { |
343 | 286k | tmp = best_gain_mult[i]; |
344 | 1.38M | } else { |
345 | 1.38M | tmp = gainMult_Q8; |
346 | 1.38M | } |
347 | 1.67M | sEncCtrl.Gains_Q16[ i ] = silk_LSHIFT_SAT32( silk_SMULWB( sEncCtrl.GainsUnq_Q16[ i ], tmp ), 8 ); |
348 | 1.67M | } |
349 | | |
350 | | /* Quantize gains */ |
351 | 504k | psEnc->sShape.LastGainIndex = sEncCtrl.lastGainIndexPrev; |
352 | 504k | silk_gains_quant( psEnc->sCmn.indices.GainsIndices, sEncCtrl.Gains_Q16, |
353 | 504k | &psEnc->sShape.LastGainIndex, condCoding == CODE_CONDITIONALLY, psEnc->sCmn.nb_subfr ); |
354 | | |
355 | | /* Unique identifier of gains vector */ |
356 | 504k | gainsID = silk_gains_ID( psEnc->sCmn.indices.GainsIndices, psEnc->sCmn.nb_subfr ); |
357 | 504k | } |
358 | 240k | } |
359 | | |
360 | | /* Update input buffer */ |
361 | 240k | silk_memmove( psEnc->x_buf, &psEnc->x_buf[ psEnc->sCmn.frame_length ], |
362 | 240k | ( psEnc->sCmn.ltp_mem_length + LA_SHAPE_MS * psEnc->sCmn.fs_kHz ) * sizeof( opus_int16 ) ); |
363 | | |
364 | | /* Exit without entropy coding */ |
365 | 240k | if( psEnc->sCmn.prefillFlag ) { |
366 | | /* No payload */ |
367 | 0 | *pnBytesOut = 0; |
368 | 0 | RESTORE_STACK; |
369 | 0 | return ret; |
370 | 0 | } |
371 | | |
372 | | /* Parameters needed for next frame */ |
373 | 240k | psEnc->sCmn.prevLag = sEncCtrl.pitchL[ psEnc->sCmn.nb_subfr - 1 ]; |
374 | 240k | psEnc->sCmn.prevSignalType = psEnc->sCmn.indices.signalType; |
375 | | |
376 | | /****************************************/ |
377 | | /* Finalize payload */ |
378 | | /****************************************/ |
379 | 240k | psEnc->sCmn.first_frame_after_reset = 0; |
380 | | /* Payload size */ |
381 | 240k | *pnBytesOut = silk_RSHIFT( ec_tell( psRangeEnc ) + 7, 3 ); |
382 | | |
383 | 240k | RESTORE_STACK; |
384 | 240k | return ret; |
385 | 240k | } |
386 | | |
387 | | /* Low-Bitrate Redundancy (LBRR) encoding. Reuse all parameters but encode excitation at lower bitrate */ |
388 | | static OPUS_INLINE void silk_LBRR_encode_FIX( |
389 | | silk_encoder_state_FIX *psEnc, /* I/O Pointer to Silk FIX encoder state */ |
390 | | silk_encoder_control_FIX *psEncCtrl, /* I/O Pointer to Silk FIX encoder control struct */ |
391 | | const opus_int16 x16[], /* I Input signal */ |
392 | | opus_int condCoding /* I The type of conditional coding used so far for this frame */ |
393 | | ) |
394 | 240k | { |
395 | 240k | opus_int32 TempGains_Q16[ MAX_NB_SUBFR ]; |
396 | 240k | SideInfoIndices *psIndices_LBRR = &psEnc->sCmn.indices_LBRR[ psEnc->sCmn.nFramesEncoded ]; |
397 | 240k | silk_nsq_state sNSQ_LBRR; |
398 | | |
399 | | /*******************************************/ |
400 | | /* Control use of inband LBRR */ |
401 | | /*******************************************/ |
402 | 240k | if( psEnc->sCmn.LBRR_enabled && psEnc->sCmn.speech_activity_Q8 > SILK_FIX_CONST( LBRR_SPEECH_ACTIVITY_THRES, 8 ) ) { |
403 | 114k | psEnc->sCmn.LBRR_flags[ psEnc->sCmn.nFramesEncoded ] = 1; |
404 | | |
405 | | /* Copy noise shaping quantizer state and quantization indices from regular encoding */ |
406 | 114k | silk_memcpy( &sNSQ_LBRR, &psEnc->sCmn.sNSQ, sizeof( silk_nsq_state ) ); |
407 | 114k | silk_memcpy( psIndices_LBRR, &psEnc->sCmn.indices, sizeof( SideInfoIndices ) ); |
408 | | |
409 | | /* Save original gains */ |
410 | 114k | silk_memcpy( TempGains_Q16, psEncCtrl->Gains_Q16, psEnc->sCmn.nb_subfr * sizeof( opus_int32 ) ); |
411 | | |
412 | 114k | if( psEnc->sCmn.nFramesEncoded == 0 || psEnc->sCmn.LBRR_flags[ psEnc->sCmn.nFramesEncoded - 1 ] == 0 ) { |
413 | | /* First frame in packet or previous frame not LBRR coded */ |
414 | 90.6k | psEnc->sCmn.LBRRprevLastGainIndex = psEnc->sShape.LastGainIndex; |
415 | | |
416 | | /* Increase Gains to get target LBRR rate */ |
417 | 90.6k | psIndices_LBRR->GainsIndices[ 0 ] = psIndices_LBRR->GainsIndices[ 0 ] + psEnc->sCmn.LBRR_GainIncreases; |
418 | 90.6k | psIndices_LBRR->GainsIndices[ 0 ] = silk_min_int( psIndices_LBRR->GainsIndices[ 0 ], N_LEVELS_QGAIN - 1 ); |
419 | 90.6k | } |
420 | | |
421 | | /* Decode to get gains in sync with decoder */ |
422 | | /* Overwrite unquantized gains with quantized gains */ |
423 | 114k | silk_gains_dequant( psEncCtrl->Gains_Q16, psIndices_LBRR->GainsIndices, |
424 | 114k | &psEnc->sCmn.LBRRprevLastGainIndex, condCoding == CODE_CONDITIONALLY, psEnc->sCmn.nb_subfr ); |
425 | | |
426 | | /*****************************************/ |
427 | | /* Noise shaping quantization */ |
428 | | /*****************************************/ |
429 | 114k | if( psEnc->sCmn.nStatesDelayedDecision > 1 || psEnc->sCmn.warping_Q16 > 0 ) { |
430 | 92.0k | silk_NSQ_del_dec( &psEnc->sCmn, &sNSQ_LBRR, psIndices_LBRR, x16, |
431 | 92.0k | psEnc->sCmn.pulses_LBRR[ psEnc->sCmn.nFramesEncoded ], psEncCtrl->PredCoef_Q12[ 0 ], psEncCtrl->LTPCoef_Q14, |
432 | 92.0k | psEncCtrl->AR_Q13, psEncCtrl->HarmShapeGain_Q14, psEncCtrl->Tilt_Q14, psEncCtrl->LF_shp_Q14, |
433 | 92.0k | psEncCtrl->Gains_Q16, psEncCtrl->pitchL, psEncCtrl->Lambda_Q10, psEncCtrl->LTP_scale_Q14, psEnc->sCmn.arch ); |
434 | 92.0k | } else { |
435 | 22.8k | silk_NSQ( &psEnc->sCmn, &sNSQ_LBRR, psIndices_LBRR, x16, |
436 | 22.8k | psEnc->sCmn.pulses_LBRR[ psEnc->sCmn.nFramesEncoded ], psEncCtrl->PredCoef_Q12[ 0 ], psEncCtrl->LTPCoef_Q14, |
437 | 22.8k | psEncCtrl->AR_Q13, psEncCtrl->HarmShapeGain_Q14, psEncCtrl->Tilt_Q14, psEncCtrl->LF_shp_Q14, |
438 | 22.8k | psEncCtrl->Gains_Q16, psEncCtrl->pitchL, psEncCtrl->Lambda_Q10, psEncCtrl->LTP_scale_Q14, psEnc->sCmn.arch ); |
439 | 22.8k | } |
440 | | |
441 | | /* Restore original gains */ |
442 | 114k | silk_memcpy( psEncCtrl->Gains_Q16, TempGains_Q16, psEnc->sCmn.nb_subfr * sizeof( opus_int32 ) ); |
443 | 114k | } |
444 | 240k | } |