Squashed commit of the following:
[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 SKP_INLINE SKP_int SKP_Silk_setup_resamplers(\r
32     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
33     SKP_int                         fs_kHz              /* I                        */\r
34 );\r
35 \r
36 SKP_INLINE SKP_int SKP_Silk_setup_fs(\r
37     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
38     SKP_int                         fs_kHz,             /* I                        */\r
39     SKP_int                         PacketSize_ms       /* I                        */\r
40 );\r
41 \r
42 SKP_INLINE SKP_int SKP_Silk_setup_rate(\r
43     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
44     SKP_int32                       TargetRate_bps      /* I                        */\r
45 );\r
46 \r
47 /* Control encoder SNR */\r
48 SKP_int SKP_Silk_control_encoder_FIX( \r
49     SKP_Silk_encoder_state_FIX  *psEnc,                 /* I/O  Pointer to Silk encoder state           */\r
50     const SKP_int               PacketSize_ms,          /* I    Packet length (ms)                      */\r
51     const SKP_int32             TargetRate_bps,         /* I    Target max bitrate (bps)                */\r
52     const SKP_int               PacketLoss_perc,        /* I    Packet loss rate (in percent)           */\r
53     const SKP_int               Complexity              /* I    Complexity (0-10)                       */\r
54 )\r
55 {\r
56     SKP_int   fs_kHz, ret = 0;\r
57 \r
58     if( psEnc->sCmn.controlled_since_last_payload != 0 ) {\r
59         if( psEnc->sCmn.API_fs_Hz != psEnc->sCmn.prev_API_fs_Hz && psEnc->sCmn.fs_kHz > 0 ) {\r
60             /* Change in API sampling rate in the middle of encoding a packet */\r
61             ret += SKP_Silk_setup_resamplers( psEnc, psEnc->sCmn.fs_kHz );\r
62         }\r
63         return ret;\r
64     }\r
65 \r
66     /* Beyond this point we know that there are no previously coded frames in the payload buffer */\r
67 \r
68     /********************************************/\r
69     /* Determine internal sampling rate         */\r
70     /********************************************/\r
71     fs_kHz = SKP_Silk_control_audio_bandwidth( &psEnc->sCmn, TargetRate_bps );\r
72 \r
73     /********************************************/\r
74     /* Prepare resampler and buffered data      */\r
75     /********************************************/\r
76     ret += SKP_Silk_setup_resamplers( psEnc, fs_kHz );\r
77 \r
78     /********************************************/\r
79     /* Set internal sampling frequency          */\r
80     /********************************************/\r
81     ret += SKP_Silk_setup_fs( psEnc, fs_kHz, PacketSize_ms );\r
82 \r
83     /********************************************/\r
84     /* Set encoding complexity                  */\r
85     /********************************************/\r
86     ret += SKP_Silk_setup_complexity( &psEnc->sCmn, Complexity );\r
87 \r
88     /********************************************/\r
89     /* Set bitrate/coding quality               */\r
90     /********************************************/\r
91     ret += SKP_Silk_setup_rate( psEnc, TargetRate_bps );\r
92 \r
93     /********************************************/\r
94     /* Set packet loss rate measured by farend  */\r
95     /********************************************/\r
96     if( ( PacketLoss_perc < 0 ) || ( PacketLoss_perc > 100 ) ) {\r
97         ret = SKP_SILK_ENC_INVALID_LOSS_RATE;\r
98     }\r
99     psEnc->sCmn.PacketLoss_perc = PacketLoss_perc;\r
100 \r
101     /********************************************/\r
102     /* Set LBRR usage                           */\r
103     /********************************************/\r
104     ret += SKP_Silk_setup_LBRR( &psEnc->sCmn );\r
105 \r
106     psEnc->sCmn.controlled_since_last_payload = 1;\r
107 \r
108     return ret;\r
109 }\r
110 \r
111 SKP_INLINE SKP_int SKP_Silk_setup_resamplers(\r
112     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
113     SKP_int                         fs_kHz              /* I                        */\r
114 )\r
115 {\r
116     SKP_int ret = SKP_SILK_NO_ERROR;\r
117     \r
118     if( psEnc->sCmn.fs_kHz != fs_kHz || psEnc->sCmn.prev_API_fs_Hz != psEnc->sCmn.API_fs_Hz ) {\r
119 \r
120         if( psEnc->sCmn.fs_kHz == 0 ) {\r
121             /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */\r
122             ret += SKP_Silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, fs_kHz * 1000 );\r
123         } else {\r
124             /* Allocate space for worst case temporary upsampling, 8 to 48 kHz, so a factor 6 */\r
125             SKP_int16 x_buf_API_fs_Hz[ ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * ( MAX_API_FS_KHZ / 8 ) ];\r
126 \r
127             SKP_int32 nSamples_temp = SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + LA_SHAPE_MS * psEnc->sCmn.fs_kHz;\r
128 \r
129             if( SKP_SMULBB( fs_kHz, 1000 ) < psEnc->sCmn.API_fs_Hz && psEnc->sCmn.fs_kHz != 0 ) {\r
130                 /* Resample buffered data in x_buf to API_fs_Hz */\r
131 \r
132                 SKP_Silk_resampler_state_struct  temp_resampler_state;\r
133 \r
134                 /* Initialize resampler for temporary resampling of x_buf data to API_fs_Hz */\r
135                 ret += SKP_Silk_resampler_init( &temp_resampler_state, SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ), psEnc->sCmn.API_fs_Hz );\r
136 \r
137                 /* Temporary resampling of x_buf data to API_fs_Hz */\r
138                 ret += SKP_Silk_resampler( &temp_resampler_state, x_buf_API_fs_Hz, psEnc->x_buf, nSamples_temp );\r
139 \r
140                 /* Calculate number of samples that has been temporarily upsampled */\r
141                 nSamples_temp = SKP_DIV32_16( nSamples_temp * psEnc->sCmn.API_fs_Hz, SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ) );\r
142 \r
143                 /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */\r
144                 ret += SKP_Silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, SKP_SMULBB( fs_kHz, 1000 ) );\r
145 \r
146             } else {\r
147                 /* Copy data */\r
148                 SKP_memcpy( x_buf_API_fs_Hz, psEnc->x_buf, nSamples_temp * sizeof( SKP_int16 ) );\r
149             }\r
150 \r
151             if( 1000 * fs_kHz != psEnc->sCmn.API_fs_Hz ) {\r
152                 /* Correct resampler state (unless resampling by a factor 1) by resampling buffered data from API_fs_Hz to fs_kHz */\r
153                 ret += SKP_Silk_resampler( &psEnc->sCmn.resampler_state, psEnc->x_buf, x_buf_API_fs_Hz, nSamples_temp );\r
154             }\r
155         }\r
156     }\r
157 \r
158     psEnc->sCmn.prev_API_fs_Hz = psEnc->sCmn.API_fs_Hz;\r
159 \r
160     return(ret);\r
161 }\r
162 \r
163 SKP_INLINE SKP_int SKP_Silk_setup_fs(\r
164     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
165     SKP_int                         fs_kHz,             /* I                        */\r
166     SKP_int                         PacketSize_ms       /* I                        */\r
167 )\r
168 {\r
169     SKP_int ret = SKP_SILK_NO_ERROR;\r
170 \r
171     /* Set packet size */\r
172     if( PacketSize_ms != psEnc->sCmn.PacketSize_ms ) {\r
173         if( ( PacketSize_ms !=  10 ) &&\r
174             ( PacketSize_ms !=  20 ) &&\r
175             ( PacketSize_ms !=  40 ) && \r
176             ( PacketSize_ms !=  60 ) ) {\r
177             ret = SKP_SILK_ENC_PACKET_SIZE_NOT_SUPPORTED;\r
178         }\r
179         if( PacketSize_ms == 10 ) {\r
180             psEnc->sCmn.nFramesPerPacket = 1;\r
181             psEnc->sCmn.nb_subfr = MAX_NB_SUBFR >> 1;\r
182             psEnc->sCmn.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );\r
183             if( psEnc->sCmn.fs_kHz == 8 ) {\r
184                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_NB_iCDF;\r
185             } else {\r
186                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_iCDF;\r
187             }\r
188         } else {\r
189             psEnc->sCmn.nFramesPerPacket = SKP_DIV32_16( PacketSize_ms, MAX_FRAME_LENGTH_MS );\r
190             psEnc->sCmn.nb_subfr = MAX_NB_SUBFR;\r
191             psEnc->sCmn.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );\r
192             if( psEnc->sCmn.fs_kHz == 8 ) {\r
193                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_NB_iCDF;\r
194             } else {\r
195                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_iCDF; \r
196             }\r
197         }\r
198         psEnc->sCmn.PacketSize_ms = PacketSize_ms;\r
199     }\r
200 \r
201     /* Set internal sampling frequency */\r
202     if( psEnc->sCmn.fs_kHz != fs_kHz ) {\r
203         /* reset part of the state */\r
204         SKP_memset( &psEnc->sShape,               0, sizeof( SKP_Silk_shape_state_FIX ) );\r
205         SKP_memset( &psEnc->sPrefilt,             0, sizeof( SKP_Silk_prefilter_state_FIX ) );\r
206         SKP_memset( &psEnc->sCmn.sNSQ,            0, sizeof( SKP_Silk_nsq_state ) );\r
207         SKP_memset( psEnc->sCmn.prev_NLSFq_Q15,   0, sizeof( psEnc->sCmn.prev_NLSFq_Q15 ) );\r
208         SKP_memset( &psEnc->sCmn.sLP.In_LP_State, 0, sizeof( psEnc->sCmn.sLP.In_LP_State ) );\r
209         psEnc->sCmn.inputBufIx                  = 0;\r
210         psEnc->sCmn.nFramesAnalyzed             = 0;\r
211         psEnc->sCmn.TargetRate_bps              = 0; /* Ensures that psEnc->SNR_dB is recomputed */\r
212 \r
213         /* Initialize non-zero parameters */\r
214         psEnc->sCmn.prevLag                     = 100;\r
215         psEnc->sCmn.first_frame_after_reset     = 1;\r
216         psEnc->sPrefilt.lagPrev                 = 100;\r
217         psEnc->sShape.LastGainIndex             = 10;\r
218         psEnc->sCmn.sNSQ.lagPrev                = 100;\r
219         psEnc->sCmn.sNSQ.prev_inv_gain_Q16      = 65536;\r
220 \r
221         psEnc->sCmn.fs_kHz = fs_kHz;\r
222         if( psEnc->sCmn.fs_kHz == 8 ) {\r
223             psEnc->sCmn.predictLPCOrder = MIN_LPC_ORDER;\r
224             psEnc->sCmn.psNLSF_CB[ 0 ]  = &SKP_Silk_NLSF_CB0_10;\r
225             psEnc->sCmn.psNLSF_CB[ 1 ]  = &SKP_Silk_NLSF_CB1_10;\r
226             if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ){\r
227                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_NB_iCDF; \r
228             } else if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR / 2 ){\r
229                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_NB_iCDF;\r
230             } else {\r
231                 /* Unsupported number of frames */\r
232                 SKP_assert( 0 );\r
233             }\r
234         } else {\r
235             psEnc->sCmn.predictLPCOrder = MAX_LPC_ORDER;\r
236             psEnc->sCmn.psNLSF_CB[ 0 ]  = &SKP_Silk_NLSF_CB0_16;\r
237             psEnc->sCmn.psNLSF_CB[ 1 ]  = &SKP_Silk_NLSF_CB1_16;\r
238             if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ){\r
239                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_iCDF; \r
240             } else if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR / 2 ){\r
241                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_iCDF;\r
242             } else {\r
243                 /* Unsupported number of frames */\r
244                 SKP_assert( 0 );\r
245             }\r
246         }\r
247         psEnc->sCmn.subfr_length   = SUB_FRAME_LENGTH_MS * fs_kHz;\r
248         psEnc->sCmn.frame_length   = SKP_SMULBB( psEnc->sCmn.subfr_length, psEnc->sCmn.nb_subfr );\r
249         psEnc->sCmn.ltp_mem_length = SKP_SMULBB( LTP_MEM_LENGTH_MS, fs_kHz ); \r
250         psEnc->sCmn.la_pitch       = SKP_SMULBB( LA_PITCH_MS, fs_kHz );\r
251         psEnc->sCmn.max_pitch_lag  = SKP_SMULBB( 18, fs_kHz );\r
252         if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ){\r
253             psEnc->sCmn.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );\r
254         } else if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR / 2 ){\r
255             psEnc->sCmn.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );\r
256         } else {\r
257             /* Unsupported number of frames */\r
258             SKP_assert( 0 );\r
259         }\r
260         if( psEnc->sCmn.fs_kHz == 16 ) {\r
261             psEnc->sCmn.mu_LTP_Q9 = SKP_FIX_CONST( MU_LTP_QUANT_WB, 9 );\r
262             psEnc->sCmn.pitch_lag_low_bits_iCDF = SKP_Silk_uniform8_iCDF;\r
263         } else if( psEnc->sCmn.fs_kHz == 12 ) {\r
264             psEnc->sCmn.mu_LTP_Q9 = SKP_FIX_CONST( MU_LTP_QUANT_MB, 9 );\r
265             psEnc->sCmn.pitch_lag_low_bits_iCDF = SKP_Silk_uniform6_iCDF;\r
266         } else if( psEnc->sCmn.fs_kHz == 8 ) {\r
267             psEnc->sCmn.mu_LTP_Q9 = SKP_FIX_CONST( MU_LTP_QUANT_NB, 9 );\r
268             psEnc->sCmn.pitch_lag_low_bits_iCDF = SKP_Silk_uniform4_iCDF;\r
269         } else {\r
270             /* unsupported sampling rate */\r
271             SKP_assert( 0 );\r
272         }\r
273     }\r
274 \r
275     /* Check that settings are valid */\r
276     SKP_assert( ( psEnc->sCmn.subfr_length * psEnc->sCmn.nb_subfr ) == psEnc->sCmn.frame_length );\r
277  \r
278     return( ret );\r
279 }\r
280 \r
281 SKP_INLINE SKP_int SKP_Silk_setup_rate(\r
282     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
283     SKP_int32                       TargetRate_bps      /* I                        */\r
284 )\r
285 {\r
286     SKP_int k, ret = SKP_SILK_NO_ERROR;\r
287     SKP_int32 frac_Q6;\r
288     const SKP_int32 *rateTable;\r
289 \r
290     /* Set bitrate/coding quality */\r
291     if( TargetRate_bps != psEnc->sCmn.TargetRate_bps ) {\r
292         psEnc->sCmn.TargetRate_bps = TargetRate_bps;\r
293 \r
294         /* If new TargetRate_bps, translate to SNR_dB value */\r
295         if( psEnc->sCmn.fs_kHz == 8 ) {\r
296             rateTable = TargetRate_table_NB;\r
297         } else if( psEnc->sCmn.fs_kHz == 12 ) {\r
298             rateTable = TargetRate_table_MB;\r
299         } else if( psEnc->sCmn.fs_kHz == 16 ) {\r
300             rateTable = TargetRate_table_WB;\r
301         } else {\r
302             SKP_assert( 0 );\r
303         }\r
304         /* Reduce bitrate for 10 ms modes in these calculations */\r
305         if( psEnc->sCmn.nb_subfr == 2 ) {\r
306             TargetRate_bps -= REDUCE_BITRATE_10_MS_BPS;\r
307         }\r
308         for( k = 1; k < TARGET_RATE_TAB_SZ; k++ ) {\r
309             /* Find bitrate interval in table and interpolate */\r
310             if( TargetRate_bps <= rateTable[ k ] ) {\r
311                 frac_Q6 = SKP_DIV32( SKP_LSHIFT( TargetRate_bps - rateTable[ k - 1 ], 6 ), \r
312                                                  rateTable[ k ] - rateTable[ k - 1 ] );\r
313                 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
314                 break;\r
315             }\r
316         }\r
317     }\r
318     return( ret );\r
319 }\r