SILK update with LBRR and some bugfixes
[opus.git] / src_FIX / SKP_Silk_control_codec_FIX.c
1 /***********************************************************************\r
2 Copyright (c) 2006-2011, Skype Limited. All rights reserved. \r
3 Redistribution and use in source and binary forms, with or without \r
4 modification, (subject to the limitations in the disclaimer below) \r
5 are permitted provided that the following conditions are met:\r
6 - Redistributions of source code must retain the above copyright notice,\r
7 this list of conditions and the following disclaimer.\r
8 - Redistributions in binary form must reproduce the above copyright \r
9 notice, this list of conditions and the following disclaimer in the \r
10 documentation and/or other materials provided with the distribution.\r
11 - Neither the name of Skype Limited, nor the names of specific \r
12 contributors, may be used to endorse or promote products derived from \r
13 this software without specific prior written permission.\r
14 NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED \r
15 BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND \r
16 CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\r
17 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND \r
18 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE \r
19 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, \r
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF \r
22 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON \r
23 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE \r
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
26 ***********************************************************************/\r
27 \r
28 #include "SKP_Silk_main_FIX.h"\r
29 #include "SKP_Silk_setup.h"\r
30 \r
31 /* ToDo: Move the functions belowto common to be able to use them in FLP control codec also */\r
32 SKP_INLINE SKP_int SKP_Silk_setup_resamplers(\r
33     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
34     SKP_int                         fs_kHz              /* I                        */\r
35 );\r
36 \r
37 SKP_INLINE SKP_int SKP_Silk_setup_fs(\r
38     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
39     SKP_int                         fs_kHz,             /* I                        */\r
40     SKP_int                         PacketSize_ms       /* I                        */\r
41 );\r
42 \r
43 SKP_INLINE SKP_int SKP_Silk_setup_rate(\r
44     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
45     SKP_int32                       TargetRate_bps      /* I                        */\r
46 );\r
47 \r
48 /* Control encoder SNR */\r
49 SKP_int SKP_Silk_control_encoder_FIX( \r
50     SKP_Silk_encoder_state_FIX  *psEnc,                 /* I/O  Pointer to Silk encoder state           */\r
51     const SKP_int               PacketSize_ms,          /* I    Packet length (ms)                      */\r
52     const SKP_int32             TargetRate_bps,         /* I    Target max bitrate (bps)                */\r
53     const SKP_int               PacketLoss_perc,        /* I    Packet loss rate (in percent)           */\r
54     const SKP_int               Complexity              /* I    Complexity (0-10)                       */\r
55 )\r
56 {\r
57     SKP_int   fs_kHz, ret = 0;\r
58 \r
59     if( psEnc->sCmn.controlled_since_last_payload != 0 ) {\r
60         if( psEnc->sCmn.API_fs_Hz != psEnc->sCmn.prev_API_fs_Hz && psEnc->sCmn.fs_kHz > 0 ) {\r
61             /* Change in API sampling rate in the middle of encoding a packet */\r
62             ret += SKP_Silk_setup_resamplers( psEnc, psEnc->sCmn.fs_kHz );\r
63         }\r
64         return ret;\r
65     }\r
66 \r
67     /* Beyond this point we know that there are no previously coded frames in the payload buffer */\r
68 \r
69     /********************************************/\r
70     /* Determine internal sampling rate         */\r
71     /********************************************/\r
72     fs_kHz = SKP_Silk_control_audio_bandwidth( &psEnc->sCmn, TargetRate_bps );\r
73 \r
74     /********************************************/\r
75     /* Prepare resampler and buffered data      */\r
76     /********************************************/\r
77     ret += SKP_Silk_setup_resamplers( psEnc, fs_kHz );\r
78 \r
79     /********************************************/\r
80     /* Set internal sampling frequency          */\r
81     /********************************************/\r
82     ret += SKP_Silk_setup_fs( psEnc, fs_kHz, PacketSize_ms );\r
83 \r
84     /********************************************/\r
85     /* Set encoding complexity                  */\r
86     /********************************************/\r
87     ret += SKP_Silk_setup_complexity( &psEnc->sCmn, Complexity );\r
88 \r
89     /********************************************/\r
90     /* Set bitrate/coding quality               */\r
91     /********************************************/\r
92     ret += SKP_Silk_setup_rate( psEnc, TargetRate_bps );\r
93 \r
94     /********************************************/\r
95     /* Set packet loss rate measured by farend  */\r
96     /********************************************/\r
97     if( ( PacketLoss_perc < 0 ) || ( PacketLoss_perc > 100 ) ) {\r
98         ret = SKP_SILK_ENC_INVALID_LOSS_RATE;\r
99     }\r
100     psEnc->sCmn.PacketLoss_perc = PacketLoss_perc;\r
101 \r
102     /********************************************/\r
103     /* Set LBRR usage                           */\r
104     /********************************************/\r
105     ret += SKP_Silk_setup_LBRR( &psEnc->sCmn );\r
106 \r
107     psEnc->sCmn.controlled_since_last_payload = 1;\r
108 \r
109     return ret;\r
110 }\r
111 \r
112 SKP_INLINE SKP_int SKP_Silk_setup_resamplers(\r
113     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
114     SKP_int                         fs_kHz              /* I                        */\r
115 )\r
116 {\r
117     SKP_int ret = SKP_SILK_NO_ERROR;\r
118     \r
119     if( psEnc->sCmn.fs_kHz != fs_kHz || psEnc->sCmn.prev_API_fs_Hz != psEnc->sCmn.API_fs_Hz ) {\r
120 \r
121         if( psEnc->sCmn.fs_kHz == 0 ) {\r
122             /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */\r
123             ret += SKP_Silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, fs_kHz * 1000 );\r
124         } else {\r
125             /* Allocate space for worst case temporary upsampling, 8 to 48 kHz, so a factor 6 */\r
126             SKP_int16 x_buf_API_fs_Hz[ ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * ( MAX_API_FS_KHZ / 8 ) ];\r
127 \r
128             SKP_int32 nSamples_temp = SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + LA_SHAPE_MS * psEnc->sCmn.fs_kHz;\r
129 \r
130             if( SKP_SMULBB( fs_kHz, 1000 ) < psEnc->sCmn.API_fs_Hz && psEnc->sCmn.fs_kHz != 0 ) {\r
131                 /* Resample buffered data in x_buf to API_fs_Hz */\r
132 \r
133                 SKP_Silk_resampler_state_struct  temp_resampler_state;\r
134 \r
135                 /* Initialize resampler for temporary resampling of x_buf data to API_fs_Hz */\r
136                 ret += SKP_Silk_resampler_init( &temp_resampler_state, SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ), psEnc->sCmn.API_fs_Hz );\r
137 \r
138                 /* Temporary resampling of x_buf data to API_fs_Hz */\r
139                 ret += SKP_Silk_resampler( &temp_resampler_state, x_buf_API_fs_Hz, psEnc->x_buf, nSamples_temp );\r
140 \r
141                 /* Calculate number of samples that has been temporarily upsampled */\r
142                 nSamples_temp = SKP_DIV32_16( nSamples_temp * psEnc->sCmn.API_fs_Hz, SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ) );\r
143 \r
144                 /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */\r
145                 ret += SKP_Silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, SKP_SMULBB( fs_kHz, 1000 ) );\r
146 \r
147             } else {\r
148                 /* Copy data */\r
149                 SKP_memcpy( x_buf_API_fs_Hz, psEnc->x_buf, nSamples_temp * sizeof( SKP_int16 ) );\r
150             }\r
151 \r
152             if( 1000 * fs_kHz != psEnc->sCmn.API_fs_Hz ) {\r
153                 /* Correct resampler state (unless resampling by a factor 1) by resampling buffered data from API_fs_Hz to fs_kHz */\r
154                 ret += SKP_Silk_resampler( &psEnc->sCmn.resampler_state, psEnc->x_buf, x_buf_API_fs_Hz, nSamples_temp );\r
155             }\r
156         }\r
157     }\r
158 \r
159     psEnc->sCmn.prev_API_fs_Hz = psEnc->sCmn.API_fs_Hz;\r
160 \r
161     return(ret);\r
162 }\r
163 \r
164 SKP_INLINE SKP_int SKP_Silk_setup_fs(\r
165     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
166     SKP_int                         fs_kHz,             /* I                        */\r
167     SKP_int                         PacketSize_ms       /* I                        */\r
168 )\r
169 {\r
170     SKP_int ret = SKP_SILK_NO_ERROR;\r
171 \r
172     /* Set packet size */\r
173     if( PacketSize_ms != psEnc->sCmn.PacketSize_ms ) {\r
174         if( ( PacketSize_ms !=  10 ) &&\r
175             ( PacketSize_ms !=  20 ) &&\r
176             ( PacketSize_ms !=  40 ) && \r
177             ( PacketSize_ms !=  60 ) ) {\r
178             ret = SKP_SILK_ENC_PACKET_SIZE_NOT_SUPPORTED;\r
179         }\r
180         if( PacketSize_ms == 10 ) {\r
181             psEnc->sCmn.nFramesPerPacket = 1;\r
182             psEnc->sCmn.nb_subfr = MAX_NB_SUBFR >> 1;\r
183             psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );\r
184             if( psEnc->sCmn.fs_kHz == 8 ) {\r
185                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_NB_iCDF;\r
186             } else {\r
187                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_iCDF;\r
188             }\r
189         } else {\r
190             psEnc->sCmn.nFramesPerPacket = SKP_DIV32_16( PacketSize_ms, MAX_FRAME_LENGTH_MS );\r
191             psEnc->sCmn.nb_subfr = MAX_NB_SUBFR;\r
192             psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );\r
193             if( psEnc->sCmn.fs_kHz == 8 ) {\r
194                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_NB_iCDF;\r
195             } else {\r
196                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_iCDF; \r
197             }\r
198         }\r
199         psEnc->sCmn.PacketSize_ms = PacketSize_ms;\r
200     }\r
201 \r
202     /* Set internal sampling frequency */\r
203     if( psEnc->sCmn.fs_kHz != fs_kHz ) {\r
204         /* reset part of the state */\r
205         SKP_memset( &psEnc->sShape,              0, sizeof( SKP_Silk_shape_state_FIX ) );\r
206         SKP_memset( &psEnc->sPrefilt,            0, sizeof( SKP_Silk_prefilter_state_FIX ) );\r
207         SKP_memset( &psEnc->sCmn.sNSQ,           0, sizeof( SKP_Silk_nsq_state ) );\r
208         SKP_memset( &psEnc->sPred,               0, sizeof( SKP_Silk_predict_state_FIX ) );\r
209         SKP_memset( psEnc->sPred.prev_NLSFq_Q15, 0, sizeof( psEnc->sPred.prev_NLSFq_Q15 ) );\r
210 #if SWITCH_TRANSITION_FILTERING\r
211         SKP_memset( psEnc->sCmn.sLP.In_LP_State, 0, sizeof( psEnc->sCmn.sLP.In_LP_State ) );\r
212         if( psEnc->sCmn.sLP.mode == 1 ) {\r
213             /* Begin transition phase */\r
214             psEnc->sCmn.sLP.transition_frame_no = 1;\r
215         } else {\r
216             /* End transition phase */\r
217             psEnc->sCmn.sLP.transition_frame_no = 0;\r
218         }\r
219 #endif\r
220         psEnc->sCmn.inputBufIx          = 0;\r
221         psEnc->sCmn.nFramesAnalyzed     = 0;\r
222         psEnc->sCmn.TargetRate_bps      = 0; /* Ensures that psEnc->SNR_dB is recomputed */\r
223 \r
224         /* Initialize non-zero parameters */\r
225         psEnc->sCmn.prevLag                     = 100;\r
226         psEnc->sCmn.first_frame_after_reset     = 1;\r
227         psEnc->sPrefilt.lagPrev                 = 100;\r
228         psEnc->sShape.LastGainIndex             = 10;\r
229         psEnc->sCmn.sNSQ.lagPrev                = 100;\r
230         psEnc->sCmn.sNSQ.prev_inv_gain_Q16      = 65536;\r
231 \r
232         psEnc->sCmn.fs_kHz = fs_kHz;\r
233         if( psEnc->sCmn.fs_kHz == 8 ) {\r
234             psEnc->sCmn.predictLPCOrder = MIN_LPC_ORDER;\r
235             psEnc->sCmn.psNLSF_CB[ 0 ]  = &SKP_Silk_NLSF_CB0_10;\r
236             psEnc->sCmn.psNLSF_CB[ 1 ]  = &SKP_Silk_NLSF_CB1_10;\r
237             if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ){\r
238                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_NB_iCDF; \r
239             } else if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR / 2 ){\r
240                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_NB_iCDF;\r
241             } else {\r
242                 /* Unsupported number of frames */\r
243                 SKP_assert( 0 );\r
244             }\r
245         } else {\r
246             psEnc->sCmn.predictLPCOrder = MAX_LPC_ORDER;\r
247             psEnc->sCmn.psNLSF_CB[ 0 ]  = &SKP_Silk_NLSF_CB0_16;\r
248             psEnc->sCmn.psNLSF_CB[ 1 ]  = &SKP_Silk_NLSF_CB1_16;\r
249             if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ){\r
250                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_iCDF; \r
251             } else if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR / 2 ){\r
252                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_iCDF;\r
253             } else {\r
254                 /* Unsupported number of frames */\r
255                 SKP_assert( 0 );\r
256             }\r
257         }\r
258         psEnc->sCmn.subfr_length   = SUB_FRAME_LENGTH_MS * fs_kHz;\r
259         psEnc->sCmn.frame_length   = SKP_SMULBB( psEnc->sCmn.subfr_length, psEnc->sCmn.nb_subfr );\r
260         psEnc->sCmn.ltp_mem_length = SKP_SMULBB( LTP_MEM_LENGTH_MS, fs_kHz ); \r
261         psEnc->sCmn.la_pitch       = SKP_SMULBB( LA_PITCH_MS, fs_kHz );\r
262         psEnc->sPred.min_pitch_lag = SKP_SMULBB(  3, fs_kHz );\r
263         psEnc->sPred.max_pitch_lag = SKP_SMULBB( 18, fs_kHz );\r
264         if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ){\r
265             psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );\r
266         } else if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR / 2 ){\r
267             psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );\r
268         } else {\r
269             /* Unsupported number of frames */\r
270             SKP_assert( 0 );\r
271         }\r
272         if( psEnc->sCmn.fs_kHz == 16 ) {\r
273             psEnc->sCmn.mu_LTP_Q9 = SKP_FIX_CONST( MU_LTP_QUANT_WB, 9 );\r
274             psEnc->sCmn.bitrate_threshold_up    = SKP_int32_MAX;\r
275             psEnc->sCmn.bitrate_threshold_down  = WB2MB_BITRATE_BPS; \r
276             psEnc->sCmn.pitch_lag_low_bits_iCDF = SKP_Silk_uniform8_iCDF;\r
277         } else if( psEnc->sCmn.fs_kHz == 12 ) {\r
278             psEnc->sCmn.mu_LTP_Q9 = SKP_FIX_CONST( MU_LTP_QUANT_MB, 9 );\r
279             psEnc->sCmn.bitrate_threshold_up    = MB2WB_BITRATE_BPS;\r
280             psEnc->sCmn.bitrate_threshold_down  = MB2NB_BITRATE_BPS;\r
281             psEnc->sCmn.pitch_lag_low_bits_iCDF = SKP_Silk_uniform6_iCDF;\r
282         } else if( psEnc->sCmn.fs_kHz == 8 ) {\r
283             psEnc->sCmn.mu_LTP_Q9 = SKP_FIX_CONST( MU_LTP_QUANT_NB, 9 );\r
284             psEnc->sCmn.bitrate_threshold_up    = NB2MB_BITRATE_BPS;\r
285             psEnc->sCmn.bitrate_threshold_down  = 0;\r
286             psEnc->sCmn.pitch_lag_low_bits_iCDF = SKP_Silk_uniform4_iCDF;\r
287         } else {\r
288             /* unsupported sampling rate */\r
289             SKP_assert( 0 );\r
290         }\r
291     }\r
292 \r
293     /* Check that settings are valid */\r
294     SKP_assert( ( psEnc->sCmn.subfr_length * psEnc->sCmn.nb_subfr ) == psEnc->sCmn.frame_length );\r
295  \r
296     return( ret );\r
297 }\r
298 \r
299 SKP_INLINE SKP_int SKP_Silk_setup_rate(\r
300     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
301     SKP_int32                       TargetRate_bps      /* I                        */\r
302 )\r
303 {\r
304     SKP_int k, ret = SKP_SILK_NO_ERROR;\r
305     SKP_int32 frac_Q6;\r
306     const SKP_int32 *rateTable;\r
307 \r
308     /* Set bitrate/coding quality */\r
309     if( TargetRate_bps != psEnc->sCmn.TargetRate_bps ) {\r
310         psEnc->sCmn.TargetRate_bps = TargetRate_bps;\r
311 \r
312         /* If new TargetRate_bps, translate to SNR_dB value */\r
313         if( psEnc->sCmn.fs_kHz == 8 ) {\r
314             rateTable = TargetRate_table_NB;\r
315         } else if( psEnc->sCmn.fs_kHz == 12 ) {\r
316             rateTable = TargetRate_table_MB;\r
317         } else if( psEnc->sCmn.fs_kHz == 16 ) {\r
318             rateTable = TargetRate_table_WB;\r
319         } else {\r
320             SKP_assert( 0 );\r
321         }\r
322         /* Reduce bitrate for 10 ms modes in these calculations */\r
323         if( psEnc->sCmn.nb_subfr == 2 ) {\r
324             TargetRate_bps -= REDUCE_BITRATE_10_MS_BPS;\r
325         }\r
326         for( k = 1; k < TARGET_RATE_TAB_SZ; k++ ) {\r
327             /* Find bitrate interval in table and interpolate */\r
328             if( TargetRate_bps <= rateTable[ k ] ) {\r
329                 frac_Q6 = SKP_DIV32( SKP_LSHIFT( TargetRate_bps - rateTable[ k - 1 ], 6 ), \r
330                                                  rateTable[ k ] - rateTable[ k - 1 ] );\r
331                 psEnc->SNR_dB_Q7 = SKP_LSHIFT( SNR_table_Q1[ k - 1 ], 6 ) + SKP_MUL( frac_Q6, SNR_table_Q1[ k ] - SNR_table_Q1[ k - 1 ] );\r
332                 break;\r
333             }\r
334         }\r
335     }\r
336     return( ret );\r
337 }\r