1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/***********************************************************************
Copyright (c) 2006-2010, Skype Limited. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, (subject to the limitations in the disclaimer below)
are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of Skype Limited, nor the names of specific
contributors, may be used to endorse or promote products derived from
this software without specific prior written permission.
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***********************************************************************/
#include "SKP_Silk_main_FLP.h"
#include "SKP_Silk_setup_complexity.h"
SKP_INLINE SKP_int SKP_Silk_setup_resamplers(
SKP_Silk_encoder_state_FLP *psEnc, /* I/O Pointer to Silk encoder state FLP */
SKP_int fs_kHz /* I Internal sampling rate (kHz) */
);
SKP_INLINE SKP_int SKP_Silk_setup_packetsize(
SKP_Silk_encoder_state_FLP *psEnc, /* I/O Pointer to Silk encoder state FLP */
SKP_int PacketSize_ms /* I Packet length (ms) */
);
SKP_INLINE SKP_int SKP_Silk_setup_fs(
SKP_Silk_encoder_state_FLP *psEnc, /* I/O */
SKP_int fs_kHz, /* I */
SKP_int PacketSize_ms /* I */
);
SKP_INLINE SKP_int SKP_Silk_setup_rate(
SKP_Silk_encoder_state_FLP *psEnc, /* I/O Pointer to Silk encoder state FLP */
SKP_int TargetRate_bps /* I Target max bitrate */
);
SKP_INLINE SKP_int SKP_Silk_setup_LBRR(
SKP_Silk_encoder_state_FLP *psEnc /* I/O Pointer to Silk encoder state FLP */
);
/* Control encoder */
SKP_int SKP_Silk_control_encoder_FLP(
SKP_Silk_encoder_state_FLP *psEnc, /* I/O Pointer to Silk encoder state FLP */
const SKP_int PacketSize_ms, /* I Packet length (ms) */
const SKP_int32 TargetRate_bps, /* I Target max bitrate (bps) */
const SKP_int PacketLoss_perc, /* I Packet loss rate (in percent) */
const SKP_int Complexity /* I Complexity (0->low; 1->medium; 2->high) */
)
{
SKP_int fs_kHz, ret = 0;
if( psEnc->sCmn.controlled_since_last_payload != 0 ) {
if( psEnc->sCmn.API_fs_Hz != psEnc->sCmn.prev_API_fs_Hz && psEnc->sCmn.fs_kHz > 0 ) {
/* Change in API sampling rate in the middle of encoding a packet */
ret += SKP_Silk_setup_resamplers( psEnc, psEnc->sCmn.fs_kHz );
}
return ret;
}
/* Beyond this point we know that there are no previously coded frames in the payload buffer */
/********************************************/
/* Determine internal sampling rate */
/********************************************/
fs_kHz = SKP_Silk_control_audio_bandwidth( &psEnc->sCmn, TargetRate_bps );
/********************************************/
/* Prepare resampler and buffered data */
/********************************************/
ret += SKP_Silk_setup_resamplers( psEnc, fs_kHz );
/********************************************/
/* Set internal sampling frequency */
/********************************************/
ret += SKP_Silk_setup_fs( psEnc, fs_kHz, PacketSize_ms );
/********************************************/
/* Set encoding complexity */
/********************************************/
ret += SKP_Silk_setup_complexity( &psEnc->sCmn, Complexity );
/********************************************/
/* Set bitrate/coding quality */
/********************************************/
ret += SKP_Silk_setup_rate( psEnc, TargetRate_bps );
/********************************************/
/* Set packet loss rate measured by farend */
/********************************************/
if( ( PacketLoss_perc < 0 ) || ( PacketLoss_perc > 100 ) ) {
ret = SKP_SILK_ENC_INVALID_LOSS_RATE;
}
psEnc->sCmn.PacketLoss_perc = PacketLoss_perc;
/********************************************/
/* Set LBRR usage */
/********************************************/
ret += SKP_Silk_setup_LBRR( psEnc );
psEnc->sCmn.controlled_since_last_payload = 1;
return ret;
}
/* Control low bitrate redundancy usage */
void SKP_Silk_LBRR_ctrl_FLP(
SKP_Silk_encoder_state_FLP *psEnc, /* I Encoder state FLP */
SKP_Silk_encoder_control *psEncCtrlC /* I/O Encoder control */
)
{
SKP_int LBRR_usage;
if( psEnc->sCmn.LBRR_enabled ) {
/* Control LBRR */
/* Usage Control based on sensitivity and packet loss caracteristics */
/* For now only enable adding to next for active frames. Make more complex later */
LBRR_usage = SKP_SILK_NO_LBRR;
if( psEnc->speech_activity > LBRR_SPEECH_ACTIVITY_THRES && psEnc->sCmn.PacketLoss_perc > LBRR_LOSS_THRES ) {
LBRR_usage = SKP_SILK_LBRR;
}
psEncCtrlC->LBRR_usage = LBRR_usage;
} else {
psEncCtrlC->LBRR_usage = SKP_SILK_NO_LBRR;
}
}
SKP_INLINE SKP_int SKP_Silk_setup_packetsize(
SKP_Silk_encoder_state_FLP *psEnc, /* I/O */
SKP_int PacketSize_ms /* I */
)
{
SKP_int ret = SKP_SILK_NO_ERROR;
if( ( PacketSize_ms != 10 ) &&
( PacketSize_ms != 20 ) &&
( PacketSize_ms != 40 ) &&
( PacketSize_ms != 60 ) ) {
ret = SKP_SILK_ENC_PACKET_SIZE_NOT_SUPPORTED;
} else {
if( PacketSize_ms != psEnc->sCmn.PacketSize_ms ) {
if( PacketSize_ms == 10 ) {
/* Only allowed when the payload buffer is empty */
psEnc->sCmn.nb_subfr = MAX_NB_SUBFR >> 1;
psEnc->sCmn.PacketSize_ms = PacketSize_ms;
psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, psEnc->sCmn.fs_kHz );
/* Packet length changes. Reset LBRR buffer */
SKP_Silk_LBRR_reset( &psEnc->sCmn );
} else{
psEnc->sCmn.nb_subfr = MAX_NB_SUBFR;
psEnc->sCmn.PacketSize_ms = PacketSize_ms;
psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, psEnc->sCmn.fs_kHz );
/* Packet length changes. Reset LBRR buffer */
SKP_Silk_LBRR_reset( &psEnc->sCmn );
}
}
psEnc->sCmn.frame_length = SKP_SMULBB( psEnc->sCmn.subfr_length, psEnc->sCmn.nb_subfr );
}
return(ret);
}
SKP_INLINE SKP_int SKP_Silk_setup_resamplers(
SKP_Silk_encoder_state_FLP *psEnc, /* I/O Pointer to Silk encoder state FLP */
SKP_int fs_kHz /* I External (API) sampling rate (Hz) */
)
{
SKP_int ret = SKP_SILK_NO_ERROR;
if( psEnc->sCmn.fs_kHz != fs_kHz || psEnc->sCmn.prev_API_fs_Hz != psEnc->sCmn.API_fs_Hz ) {
if( psEnc->sCmn.fs_kHz == 0 ) {
/* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */
ret += SKP_Silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, fs_kHz * 1000 );
} else {
/* Allocate space for worst case temporary upsampling, 8 to 48 kHz, so a factor 6 */
SKP_int16 x_buf_API_fs_Hz[ ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * ( MAX_API_FS_KHZ / 8 ) ];
SKP_int16 x_bufFIX[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ];
SKP_int32 nSamples_temp = 2 * psEnc->sCmn.frame_length + LA_SHAPE_MS * psEnc->sCmn.fs_kHz;
SKP_float2short_array( x_bufFIX, psEnc->x_buf, nSamples_temp );
if( fs_kHz * 1000 < psEnc->sCmn.API_fs_Hz && psEnc->sCmn.fs_kHz != 0 ) {
/* Resample buffered data in x_buf to API_fs_Hz */
SKP_Silk_resampler_state_struct temp_resampler_state;
/* Initialize resampler for temporary resampling of x_buf data to API_fs_Hz */
ret += SKP_Silk_resampler_init( &temp_resampler_state, psEnc->sCmn.fs_kHz * 1000, psEnc->sCmn.API_fs_Hz );
/* Temporary resampling of x_buf data to API_fs_Hz */
ret += SKP_Silk_resampler( &temp_resampler_state, x_buf_API_fs_Hz, x_bufFIX, nSamples_temp );
/* Calculate number of samples that has been temporarily upsampled */
nSamples_temp = SKP_DIV32_16( nSamples_temp * psEnc->sCmn.API_fs_Hz, psEnc->sCmn.fs_kHz * 1000 );
/* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */
ret += SKP_Silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, fs_kHz * 1000 );
} else {
/* Copy data */
SKP_memcpy( x_buf_API_fs_Hz, x_bufFIX, nSamples_temp * sizeof( SKP_int16 ) );
}
if( 1000 * fs_kHz != psEnc->sCmn.API_fs_Hz ) {
/* Correct resampler state (unless resampling by a factor 1) by resampling buffered data from API_fs_Hz to fs_kHz */
ret += SKP_Silk_resampler( &psEnc->sCmn.resampler_state, x_bufFIX, x_buf_API_fs_Hz, nSamples_temp );
}
SKP_short2float_array( psEnc->x_buf, x_bufFIX, ( 2 * MAX_FRAME_LENGTH_MS + LA_SHAPE_MS ) * fs_kHz );
}
}
psEnc->sCmn.prev_API_fs_Hz = psEnc->sCmn.API_fs_Hz;
return(ret);
}
SKP_INLINE SKP_int SKP_Silk_setup_fs(
SKP_Silk_encoder_state_FLP *psEnc, /* I/O */
SKP_int fs_kHz, /* I */
SKP_int PacketSize_ms /* I */
)
{
SKP_int ret = SKP_SILK_NO_ERROR;
/* Set internal sampling frequency */
if( psEnc->sCmn.fs_kHz != fs_kHz ) {
/* reset part of the state */
SKP_memset( &psEnc->sShape, 0, sizeof( SKP_Silk_shape_state_FLP ) );
SKP_memset( &psEnc->sPrefilt, 0, sizeof( SKP_Silk_prefilter_state_FLP ) );
SKP_memset( &psEnc->sNSQ, 0, sizeof( SKP_Silk_nsq_state ) );
SKP_memset( &psEnc->sPred, 0, sizeof( SKP_Silk_predict_state_FLP ) );
SKP_memset( psEnc->sNSQ.xq, 0, ( 2 * MAX_FRAME_LENGTH ) * sizeof( SKP_int16 ) );
SKP_memset( psEnc->sNSQ_LBRR.xq, 0, ( 2 * MAX_FRAME_LENGTH ) * sizeof( SKP_int16 ) );
SKP_memset( psEnc->sCmn.LBRR_buffer, 0, MAX_LBRR_DELAY * sizeof( SKP_SILK_LBRR_struct ) );
#if SWITCH_TRANSITION_FILTERING
SKP_memset( psEnc->sCmn.sLP.In_LP_State, 0, 2 * sizeof( SKP_int32 ) );
if( psEnc->sCmn.sLP.mode == 1 ) {
/* Begin transition phase */
psEnc->sCmn.sLP.transition_frame_no = 1;
} else {
/* End transition phase */
psEnc->sCmn.sLP.transition_frame_no = 0;
}
#endif
psEnc->sCmn.inputBufIx = 0;
psEnc->sCmn.nFramesInPayloadBuf = 0;
psEnc->sCmn.nBytesInPayloadBuf = 0;
psEnc->sCmn.oldest_LBRR_idx = 0;
psEnc->sCmn.TargetRate_bps = 0; /* Ensures that psEnc->SNR_dB is recomputed */
SKP_memset( psEnc->sPred.prev_NLSFq, 0, MAX_LPC_ORDER * sizeof( SKP_float ) );
/* Initialize non-zero parameters */
psEnc->sCmn.prevLag = 100;
psEnc->sCmn.prev_sigtype = SIG_TYPE_UNVOICED;
psEnc->sCmn.first_frame_after_reset = 1;
psEnc->sPrefilt.lagPrev = 100;
psEnc->sShape.LastGainIndex = 1;
psEnc->sNSQ.lagPrev = 100;
psEnc->sNSQ.prev_inv_gain_Q16 = 65536;
psEnc->sNSQ_LBRR.prev_inv_gain_Q16 = 65536;
psEnc->sCmn.fs_kHz = fs_kHz;
if( psEnc->sCmn.fs_kHz == 8 ) {
psEnc->sCmn.predictLPCOrder = MIN_LPC_ORDER;
psEnc->sCmn.psNLSF_CB[ 0 ] = &SKP_Silk_NLSF_CB0_10;
psEnc->sCmn.psNLSF_CB[ 1 ] = &SKP_Silk_NLSF_CB1_10;
psEnc->psNLSF_CB_FLP[ 0 ] = &SKP_Silk_NLSF_CB0_10_FLP;
psEnc->psNLSF_CB_FLP[ 1 ] = &SKP_Silk_NLSF_CB1_10_FLP;
} else {
psEnc->sCmn.predictLPCOrder = MAX_LPC_ORDER;
psEnc->sCmn.psNLSF_CB[ 0 ] = &SKP_Silk_NLSF_CB0_16;
psEnc->sCmn.psNLSF_CB[ 1 ] = &SKP_Silk_NLSF_CB1_16;
psEnc->psNLSF_CB_FLP[ 0 ] = &SKP_Silk_NLSF_CB0_16_FLP;
psEnc->psNLSF_CB_FLP[ 1 ] = &SKP_Silk_NLSF_CB1_16_FLP;
}
psEnc->sCmn.subfr_length = SUB_FRAME_LENGTH_MS * fs_kHz;
psEnc->sCmn.ltp_mem_length = LTP_MEM_LENGTH_MS * fs_kHz;
psEnc->sCmn.la_pitch = LA_PITCH_MS * fs_kHz;
psEnc->sPred.min_pitch_lag = 3 * fs_kHz;
psEnc->sPred.max_pitch_lag = 18 * fs_kHz;
if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ){
psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );
} else {
psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );
}
if( psEnc->sCmn.fs_kHz == 24 ) {
psEnc->mu_LTP = MU_LTP_QUANT_SWB;
psEnc->sCmn.bitrate_threshold_up = SKP_int32_MAX;
psEnc->sCmn.bitrate_threshold_down = SWB2WB_BITRATE_BPS;
} else if( psEnc->sCmn.fs_kHz == 16 ) {
psEnc->mu_LTP = MU_LTP_QUANT_WB;
psEnc->sCmn.bitrate_threshold_up = WB2SWB_BITRATE_BPS;
psEnc->sCmn.bitrate_threshold_down = WB2MB_BITRATE_BPS;
} else if( psEnc->sCmn.fs_kHz == 12 ) {
psEnc->mu_LTP = MU_LTP_QUANT_MB;
psEnc->sCmn.bitrate_threshold_up = MB2WB_BITRATE_BPS;
psEnc->sCmn.bitrate_threshold_down = MB2NB_BITRATE_BPS;
} else {
psEnc->mu_LTP = MU_LTP_QUANT_NB;
psEnc->sCmn.bitrate_threshold_up = NB2MB_BITRATE_BPS;
psEnc->sCmn.bitrate_threshold_down = 0;
}
psEnc->sCmn.fs_kHz_changed = 1;
}
/********************************************/
/* Set packet size */
/********************************************/
ret += SKP_Silk_setup_packetsize( psEnc, PacketSize_ms );
/* Check that settings are valid */
SKP_assert( ( psEnc->sCmn.subfr_length * psEnc->sCmn.nb_subfr ) == psEnc->sCmn.frame_length );
return( ret );
}
SKP_INLINE SKP_int SKP_Silk_setup_rate(
SKP_Silk_encoder_state_FLP *psEnc, /* I/O Pointer to Silk encoder state FLP */
SKP_int TargetRate_bps /* I Target max bitrate */
)
{
SKP_int k, ret = SKP_SILK_NO_ERROR;
SKP_float frac;
const SKP_int32 *rateTable;
/* Set bitrate/coding quality */
if( TargetRate_bps != psEnc->sCmn.TargetRate_bps ) {
psEnc->sCmn.TargetRate_bps = TargetRate_bps;
/* If new TargetRate_bps, translate to SNR_dB value */
if( psEnc->sCmn.fs_kHz == 8 ) {
rateTable = TargetRate_table_NB;
} else if( psEnc->sCmn.fs_kHz == 12 ) {
rateTable = TargetRate_table_MB;
} else if( psEnc->sCmn.fs_kHz == 16 ) {
rateTable = TargetRate_table_WB;
} else {
rateTable = TargetRate_table_SWB;
}
for( k = 1; k < TARGET_RATE_TAB_SZ; k++ ) {
/* Find bitrate interval in table and interpolate */
if( TargetRate_bps < rateTable[ k ] ) {
frac = (SKP_float)( TargetRate_bps - rateTable[ k - 1 ] ) /
(SKP_float)( rateTable[ k ] - rateTable[ k - 1 ] );
psEnc->SNR_dB = 0.5f * ( SNR_table_Q1[ k - 1 ] + frac * ( SNR_table_Q1[ k ] - SNR_table_Q1[ k - 1 ] ) );
break;
}
}
}
return( ret );
}
SKP_INLINE SKP_int SKP_Silk_setup_LBRR(
SKP_Silk_encoder_state_FLP *psEnc /* I/O Pointer to Silk encoder state FLP */
)
{
SKP_int ret = SKP_SILK_NO_ERROR;
#if USE_LBRR
SKP_int32 LBRRRate_thres_bps;
if( psEnc->sCmn.useInBandFEC < 0 || psEnc->sCmn.useInBandFEC > 1 ) {
ret = SKP_SILK_ENC_INVALID_INBAND_FEC_SETTING;
}
psEnc->sCmn.LBRR_enabled = psEnc->sCmn.useInBandFEC;
if( psEnc->sCmn.fs_kHz == 8 ) {
LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS - 9000;
} else if( psEnc->sCmn.fs_kHz == 12 ) {
LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS - 6000;;
} else if( psEnc->sCmn.fs_kHz == 16 ) {
LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS - 3000;
} else {
LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS;
}
if( psEnc->sCmn.TargetRate_bps >= LBRRRate_thres_bps ) {
/* Set gain increase / rate reduction for LBRR usage */
/* Coarsely tuned with PESQ for now. */
/* Linear regression coefs G = 8 - 0.5 * loss */
/* Meaning that at 16% loss main rate and redundant rate is the same, -> G = 0 */
psEnc->sCmn.LBRR_GainIncreases = SKP_max_int( 8 - SKP_RSHIFT( psEnc->sCmn.PacketLoss_perc, 1 ), 0 );
/* Set main stream rate compensation */
if( psEnc->sCmn.LBRR_enabled && psEnc->sCmn.PacketLoss_perc > LBRR_LOSS_THRES ) {
/* Tuned to give approx same mean / weighted bitrate as no inband FEC */
psEnc->inBandFEC_SNR_comp = 6.0f - 0.5f * psEnc->sCmn.LBRR_GainIncreases;
} else {
psEnc->inBandFEC_SNR_comp = 0;
psEnc->sCmn.LBRR_enabled = 0;
}
} else {
psEnc->inBandFEC_SNR_comp = 0;
psEnc->sCmn.LBRR_enabled = 0;
}
#else
if( psEnc->sCmn.LBRR_enabled != 0 ) {
ret = SKP_SILK_ENC_INVALID_INBAND_FEC_SETTING;
psEnc->sCmn.LBRR_enabled = 0;
}
#endif
return ret;
}
|