Convert all CRLF in the SILK code, tabs to spaces, and trailing
[opus.git] / silk / float / silk_encode_frame_FLP.c
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, (subject to the limitations in the disclaimer below)
5 are permitted provided that the following conditions 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 Skype Limited, nor the names of specific
12 contributors, may be used to endorse or promote products derived from
13 this software without specific prior written permission.
14 NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
15 BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
16 CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
17 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***********************************************************************/
27
28 #include "silk_main_FLP.h"
29 #include "silk_tuning_parameters.h"
30
31 /****************/
32 /* Encode frame */
33 /****************/
34 opus_int silk_encode_frame_FLP(
35     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
36     opus_int32                       *pnBytesOut,        /*   O  Number of payload bytes                 */
37     ec_enc                          *psRangeEnc         /* I/O  compressor data structure               */
38 )
39 {
40     silk_encoder_control_FLP sEncCtrl;
41     opus_int     i, ret = 0;
42     SKP_float   *x_frame, *res_pitch_frame;
43     SKP_float   xfw[ MAX_FRAME_LENGTH ];
44     SKP_float   res_pitch[ 2 * MAX_FRAME_LENGTH + LA_PITCH_MAX ];
45
46 TIC(ENCODE_FRAME)
47
48     psEnc->sCmn.indices.Seed = psEnc->sCmn.frameCounter++ & 3;
49
50     /**************************************************************/
51     /* Setup Input Pointers, and insert frame in input buffer    */
52     /*************************************************************/
53     /* pointers aligned with start of frame to encode */
54     x_frame         = psEnc->x_buf + psEnc->sCmn.ltp_mem_length;    /* start of frame to encode */
55     res_pitch_frame = res_pitch    + psEnc->sCmn.ltp_mem_length;    /* start of pitch LPC residual frame */
56
57     /****************************/
58     /* Voice Activity Detection */
59     /****************************/
60 TIC(VAD)
61     ret = silk_VAD_GetSA_Q8( &psEnc->sCmn, psEnc->sCmn.inputBuf - 1 );
62 TOC(VAD)
63
64     /**************************************************/
65     /* Convert speech activity into VAD and DTX flags */
66     /**************************************************/
67     if( psEnc->sCmn.nFramesEncoded == 0 ) {
68         psEnc->sCmn.inDTX = psEnc->sCmn.useDTX;
69     }
70     if( psEnc->sCmn.speech_activity_Q8 < SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ) ) {
71         psEnc->sCmn.indices.signalType = TYPE_NO_VOICE_ACTIVITY;
72         psEnc->sCmn.noSpeechCounter++;
73         if( psEnc->sCmn.noSpeechCounter < NB_SPEECH_FRAMES_BEFORE_DTX ) {
74             psEnc->sCmn.inDTX = 0;
75         } else if( psEnc->sCmn.noSpeechCounter > MAX_CONSECUTIVE_DTX + NB_SPEECH_FRAMES_BEFORE_DTX ) {
76             psEnc->sCmn.noSpeechCounter = NB_SPEECH_FRAMES_BEFORE_DTX;
77             psEnc->sCmn.inDTX           = 0;
78         }
79         psEnc->sCmn.VAD_flags[ psEnc->sCmn.nFramesEncoded ] = 0;
80     } else {
81         psEnc->sCmn.noSpeechCounter    = 0;
82         psEnc->sCmn.inDTX              = 0;
83         psEnc->sCmn.indices.signalType = TYPE_UNVOICED;
84         psEnc->sCmn.VAD_flags[ psEnc->sCmn.nFramesEncoded ] = 1;
85     }
86
87     /***************************************/
88     /* Ensure smooth bandwidth transitions */
89     /***************************************/
90     silk_LP_variable_cutoff( &psEnc->sCmn.sLP, psEnc->sCmn.inputBuf - 1, psEnc->sCmn.frame_length );
91
92     /*******************************************/
93     /* Copy new frame to front of input buffer */
94     /*******************************************/
95     SKP_short2float_array( x_frame + LA_SHAPE_MS * psEnc->sCmn.fs_kHz, psEnc->sCmn.inputBuf - 1, psEnc->sCmn.frame_length );
96
97     /* Add tiny signal to avoid high CPU load from denormalized floating point numbers */
98     for( i = 0; i < 8; i++ ) {
99         x_frame[ LA_SHAPE_MS * psEnc->sCmn.fs_kHz + i * ( psEnc->sCmn.frame_length >> 3 ) ] += ( 1 - ( i & 2 ) ) * 1e-6f;
100     }
101
102     /*****************************************/
103     /* Find pitch lags, initial LPC analysis */
104     /*****************************************/
105 TIC(FIND_PITCH)
106     silk_find_pitch_lags_FLP( psEnc, &sEncCtrl, res_pitch, x_frame );
107 TOC(FIND_PITCH)
108
109     /************************/
110     /* Noise shape analysis */
111     /************************/
112 TIC(NOISE_SHAPE_ANALYSIS)
113     silk_noise_shape_analysis_FLP( psEnc, &sEncCtrl, res_pitch_frame, x_frame );
114 TOC(NOISE_SHAPE_ANALYSIS)
115
116     /***************************************************/
117     /* Find linear prediction coefficients (LPC + LTP) */
118     /***************************************************/
119 TIC(FIND_PRED_COEF)
120     silk_find_pred_coefs_FLP( psEnc, &sEncCtrl, res_pitch, x_frame );
121 TOC(FIND_PRED_COEF)
122
123     /****************************************/
124     /* Process gains                        */
125     /****************************************/
126 TIC(PROCESS_GAINS)
127     silk_process_gains_FLP( psEnc, &sEncCtrl );
128 TOC(PROCESS_GAINS)
129
130     /*****************************************/
131     /* Prefiltering for noise shaper         */
132     /*****************************************/
133 TIC(PREFILTER)
134     silk_prefilter_FLP( psEnc, &sEncCtrl, xfw, x_frame );
135 TOC(PREFILTER)
136
137     /****************************************/
138     /* Low Bitrate Redundant Encoding       */
139     /****************************************/
140 TIC(LBRR)
141     silk_LBRR_encode_FLP( psEnc, &sEncCtrl, xfw );
142 TOC(LBRR)
143
144     /*****************************************/
145     /* Noise shaping quantization            */
146     /*****************************************/
147 TIC(NSQ)
148     silk_NSQ_wrapper_FLP( psEnc, &sEncCtrl, &psEnc->sCmn.indices, &psEnc->sCmn.sNSQ, psEnc->sCmn.pulses, xfw );
149 TOC(NSQ)
150
151     /* Update input buffer */
152     SKP_memmove( psEnc->x_buf, &psEnc->x_buf[ psEnc->sCmn.frame_length ],
153         ( psEnc->sCmn.ltp_mem_length + LA_SHAPE_MS * psEnc->sCmn.fs_kHz ) * sizeof( SKP_float ) );
154
155     /* Parameters needed for next frame */
156     psEnc->sCmn.prevLag        = sEncCtrl.pitchL[ psEnc->sCmn.nb_subfr - 1 ];
157     psEnc->sCmn.prevSignalType = psEnc->sCmn.indices.signalType;
158
159     /* Exit without entropy coding */
160     if( psEnc->sCmn.prefillFlag ) {
161         /* No payload */
162         *pnBytesOut = 0;
163         return ret;
164     }
165
166     /****************************************/
167     /* Encode Parameters                    */
168     /****************************************/
169 TIC(ENCODE_PARAMS)
170     silk_encode_indices( &psEnc->sCmn, psRangeEnc, psEnc->sCmn.nFramesEncoded, 0 );
171 TOC(ENCODE_PARAMS)
172
173     /****************************************/
174     /* Encode Excitation Signal             */
175     /****************************************/
176 TIC(ENCODE_PULSES)
177     silk_encode_pulses( psRangeEnc, psEnc->sCmn.indices.signalType, psEnc->sCmn.indices.quantOffsetType,
178         psEnc->sCmn.pulses, psEnc->sCmn.frame_length );
179 TOC(ENCODE_PULSES)
180
181     /****************************************/
182     /* Finalize payload                     */
183     /****************************************/
184     psEnc->sCmn.first_frame_after_reset = 0;
185     if( ++psEnc->sCmn.nFramesEncoded >= psEnc->sCmn.nFramesPerPacket ) {
186         /* Payload size */
187         *pnBytesOut = SKP_RSHIFT( ec_tell( psRangeEnc ) + 7, 3 );
188
189         /* Reset the number of frames in payload buffer */
190         psEnc->sCmn.nFramesEncoded = 0;
191     } else {
192         /* No payload this time */
193         *pnBytesOut = 0;
194     }
195 TOC(ENCODE_FRAME)
196
197 #ifdef SAVE_ALL_INTERNAL_DATA
198     //DEBUG_STORE_DATA( xf.dat,                   pIn_HP_LP,                           psEnc->sCmn.frame_length * sizeof( opus_int16 ) );
199     //DEBUG_STORE_DATA( xfw.dat,                  xfw,                                 psEnc->sCmn.frame_length * sizeof( SKP_float ) );
200     DEBUG_STORE_DATA( pitchL.dat,               sEncCtrl.pitchL,                                 MAX_NB_SUBFR * sizeof( opus_int   ) );
201     DEBUG_STORE_DATA( pitchG_quantized.dat,     sEncCtrl.LTPCoef,            psEnc->sCmn.nb_subfr * LTP_ORDER * sizeof( SKP_float ) );
202     DEBUG_STORE_DATA( LTPcorr.dat,              &psEnc->LTPCorr,                                                sizeof( SKP_float ) );
203     DEBUG_STORE_DATA( gains.dat,                sEncCtrl.Gains,                          psEnc->sCmn.nb_subfr * sizeof( SKP_float ) );
204     DEBUG_STORE_DATA( gains_indices.dat,        &psEnc->sCmn.indices.GainsIndices,       psEnc->sCmn.nb_subfr * sizeof( opus_int8  ) );
205     DEBUG_STORE_DATA( quantOffsetType.dat,      &psEnc->sCmn.indices.quantOffsetType,                           sizeof( opus_int8  ) );
206     DEBUG_STORE_DATA( speech_activity_q8.dat,   &psEnc->sCmn.speech_activity_Q8,                                sizeof( opus_int   ) );
207     DEBUG_STORE_DATA( signalType.dat,           &psEnc->sCmn.indices.signalType,                                sizeof( opus_int8  ) );
208     DEBUG_STORE_DATA( lag_index.dat,            &psEnc->sCmn.indices.lagIndex,                                  sizeof( opus_int16 ) );
209     DEBUG_STORE_DATA( contour_index.dat,        &psEnc->sCmn.indices.contourIndex,                              sizeof( opus_int8  ) );
210     DEBUG_STORE_DATA( per_index.dat,            &psEnc->sCmn.indices.PERIndex,                                  sizeof( opus_int8  ) );
211     DEBUG_STORE_DATA( PredCoef.dat,             &sEncCtrl.PredCoef[ 1 ],          psEnc->sCmn.predictLPCOrder * sizeof( SKP_float ) );
212     DEBUG_STORE_DATA( ltp_scale_idx.dat,        &psEnc->sCmn.indices.LTP_scaleIndex,                            sizeof( opus_int8   ) );
213 //  DEBUG_STORE_DATA( xq.dat,                   psEnc->sCmn.sNSQ.xqBuf,                psEnc->sCmn.frame_length * sizeof( SKP_float ) );
214 #endif
215     return ret;
216 }
217
218 /* Low-Bitrate Redundancy (LBRR) encoding. Reuse all parameters but encode excitation at lower bitrate  */
219 void silk_LBRR_encode_FLP(
220     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
221     silk_encoder_control_FLP        *psEncCtrl,         /* I/O  Encoder control FLP                     */
222     const SKP_float                 xfw[]               /* I    Input signal                            */
223 )
224 {
225     opus_int     k;
226     opus_int32   Gains_Q16[ MAX_NB_SUBFR ];
227     SKP_float   TempGains[ MAX_NB_SUBFR ];
228     SideInfoIndices *psIndices_LBRR = &psEnc->sCmn.indices_LBRR[ psEnc->sCmn.nFramesEncoded ];
229     silk_nsq_state sNSQ_LBRR;
230
231     /*******************************************/
232     /* Control use of inband LBRR              */
233     /*******************************************/
234     if( psEnc->sCmn.LBRR_enabled && psEnc->sCmn.speech_activity_Q8 > SILK_FIX_CONST( LBRR_SPEECH_ACTIVITY_THRES, 8 ) ) {
235         psEnc->sCmn.LBRR_flags[ psEnc->sCmn.nFramesEncoded ] = 1;
236
237         /* Copy noise shaping quantizer state and quantization indices from regular encoding */
238         SKP_memcpy( &sNSQ_LBRR, &psEnc->sCmn.sNSQ, sizeof( silk_nsq_state ) );
239         SKP_memcpy( psIndices_LBRR, &psEnc->sCmn.indices, sizeof( SideInfoIndices ) );
240
241         /* Save original gains */
242         SKP_memcpy( TempGains, psEncCtrl->Gains, psEnc->sCmn.nb_subfr * sizeof( SKP_float ) );
243
244         if( psEnc->sCmn.nFramesEncoded == 0 || psEnc->sCmn.LBRR_flags[ psEnc->sCmn.nFramesEncoded - 1 ] == 0 ) {
245             /* First frame in packet or previous frame not LBRR coded */
246             psEnc->sCmn.LBRRprevLastGainIndex = psEnc->sShape.LastGainIndex;
247
248             /* Increase Gains to get target LBRR rate */
249             psIndices_LBRR->GainsIndices[ 0 ] += psEnc->sCmn.LBRR_GainIncreases;
250             psIndices_LBRR->GainsIndices[ 0 ] = SKP_min_int( psIndices_LBRR->GainsIndices[ 0 ], N_LEVELS_QGAIN - 1 );
251         }
252
253         /* Decode to get gains in sync with decoder */
254         silk_gains_dequant( Gains_Q16, psIndices_LBRR->GainsIndices,
255             &psEnc->sCmn.LBRRprevLastGainIndex, psEnc->sCmn.nFramesEncoded, psEnc->sCmn.nb_subfr );
256
257         /* Overwrite unquantized gains with quantized gains and convert back to Q0 from Q16 */
258         for( k = 0; k <  psEnc->sCmn.nb_subfr; k++ ) {
259             psEncCtrl->Gains[ k ] = Gains_Q16[ k ] * ( 1.0f / 65536.0f );
260         }
261
262         /*****************************************/
263         /* Noise shaping quantization            */
264         /*****************************************/
265         silk_NSQ_wrapper_FLP( psEnc, psEncCtrl, psIndices_LBRR, &sNSQ_LBRR,
266             psEnc->sCmn.pulses_LBRR[ psEnc->sCmn.nFramesEncoded ], xfw );
267
268         /* Restore original gains */
269         SKP_memcpy( psEncCtrl->Gains, TempGains, psEnc->sCmn.nb_subfr * sizeof( SKP_float ) );
270     }
271 }