/src/opus/src/opus_encoder.c
Line | Count | Source |
1 | | /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited |
2 | | Written by Jean-Marc Valin and Koen Vos */ |
3 | | /* |
4 | | Redistribution and use in source and binary forms, with or without |
5 | | modification, are permitted provided that the following conditions |
6 | | are met: |
7 | | |
8 | | - Redistributions of source code must retain the above copyright |
9 | | notice, this list of conditions and the following disclaimer. |
10 | | |
11 | | - Redistributions in binary form must reproduce the above copyright |
12 | | notice, this list of conditions and the following disclaimer in the |
13 | | documentation and/or other materials provided with the distribution. |
14 | | |
15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
16 | | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
17 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
18 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
19 | | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
20 | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
21 | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
22 | | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
23 | | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
24 | | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
25 | | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | | */ |
27 | | |
28 | | #ifdef HAVE_CONFIG_H |
29 | | #include "config.h" |
30 | | #endif |
31 | | |
32 | | #include <stdarg.h> |
33 | | #include "celt.h" |
34 | | #include "entenc.h" |
35 | | #include "modes.h" |
36 | | #include "API.h" |
37 | | #include "stack_alloc.h" |
38 | | #include "float_cast.h" |
39 | | #include "opus.h" |
40 | | #include "arch.h" |
41 | | #include "pitch.h" |
42 | | #include "opus_private.h" |
43 | | #include "os_support.h" |
44 | | #include "cpu_support.h" |
45 | | #include "analysis.h" |
46 | | #include "mathops.h" |
47 | | #include "tuning_parameters.h" |
48 | | |
49 | | #ifdef ENABLE_DRED |
50 | | #include "dred_coding.h" |
51 | | #endif |
52 | | |
53 | | #ifdef FIXED_POINT |
54 | | #include "fixed/structs_FIX.h" |
55 | | #else |
56 | | #include "float/structs_FLP.h" |
57 | | #endif |
58 | | #ifdef ENABLE_OSCE_TRAINING_DATA |
59 | | #include <stdio.h> |
60 | | #endif |
61 | | |
62 | | #ifdef ENABLE_QEXT |
63 | | #define MAX_ENCODER_BUFFER 960 |
64 | | #else |
65 | 0 | #define MAX_ENCODER_BUFFER 480 |
66 | | #endif |
67 | | |
68 | 0 | #define PSEUDO_SNR_THRESHOLD 316.23f /* 10^(25/10) */ |
69 | | |
70 | | typedef struct { |
71 | | opus_val32 XX, XY, YY; |
72 | | opus_val16 smoothed_width; |
73 | | opus_val16 max_follower; |
74 | | } StereoWidthState; |
75 | | |
76 | | struct OpusEncoder { |
77 | | int celt_enc_offset; |
78 | | int silk_enc_offset; |
79 | | silk_EncControlStruct silk_mode; |
80 | | #ifdef ENABLE_DRED |
81 | | DREDEnc dred_encoder; |
82 | | #endif |
83 | | int application; |
84 | | int channels; |
85 | | int delay_compensation; |
86 | | int force_channels; |
87 | | int signal_type; |
88 | | int user_bandwidth; |
89 | | int max_bandwidth; |
90 | | int user_forced_mode; |
91 | | int voice_ratio; |
92 | | opus_int32 Fs; |
93 | | int use_vbr; |
94 | | int vbr_constraint; |
95 | | int variable_duration; |
96 | | opus_int32 bitrate_bps; |
97 | | opus_int32 user_bitrate_bps; |
98 | | int lsb_depth; |
99 | | int encoder_buffer; |
100 | | int lfe; |
101 | | int arch; |
102 | | int use_dtx; /* general DTX for both SILK and CELT */ |
103 | | int fec_config; |
104 | | #ifndef DISABLE_FLOAT_API |
105 | | TonalityAnalysisState analysis; |
106 | | #endif |
107 | | #ifdef ENABLE_QEXT |
108 | | int enable_qext; |
109 | | #endif |
110 | | |
111 | 0 | #define OPUS_ENCODER_RESET_START stream_channels |
112 | | int stream_channels; |
113 | | opus_int16 hybrid_stereo_width_Q14; |
114 | | opus_int32 variable_HP_smth2_Q15; |
115 | | opus_val16 prev_HB_gain; |
116 | | opus_val32 hp_mem[4]; |
117 | | int mode; |
118 | | int prev_mode; |
119 | | int prev_channels; |
120 | | int prev_framesize; |
121 | | int bandwidth; |
122 | | /* Bandwidth determined automatically from the rate (before any other adjustment) */ |
123 | | int auto_bandwidth; |
124 | | int silk_bw_switch; |
125 | | /* Sampling rate (at the API level) */ |
126 | | int first; |
127 | | celt_glog * energy_masking; |
128 | | StereoWidthState width_mem; |
129 | | #ifndef DISABLE_FLOAT_API |
130 | | int detected_bandwidth; |
131 | | #endif |
132 | | int nb_no_activity_ms_Q1; |
133 | | opus_val32 peak_signal_energy; |
134 | | #ifdef ENABLE_DRED |
135 | | int dred_duration; |
136 | | int dred_q0; |
137 | | int dred_dQ; |
138 | | int dred_qmax; |
139 | | int dred_target_chunks; |
140 | | unsigned char activity_mem[DRED_MAX_FRAMES*4]; /* 2.5ms resolution*/ |
141 | | #endif |
142 | | int nonfinal_frame; /* current frame is not the final in a packet */ |
143 | | opus_uint32 rangeFinal; |
144 | | /* Needs to be the last field because it may be partially or completely omitted. */ |
145 | | opus_res delay_buffer[MAX_ENCODER_BUFFER*2]; |
146 | | }; |
147 | | |
148 | | /* Transition tables for the voice and music. First column is the |
149 | | middle (memoriless) threshold. The second column is the hysteresis |
150 | | (difference with the middle) */ |
151 | | static const opus_int32 mono_voice_bandwidth_thresholds[8] = { |
152 | | 9000, 700, /* NB<->MB */ |
153 | | 9000, 700, /* MB<->WB */ |
154 | | 13500, 1000, /* WB<->SWB */ |
155 | | 14000, 2000, /* SWB<->FB */ |
156 | | }; |
157 | | static const opus_int32 mono_music_bandwidth_thresholds[8] = { |
158 | | 9000, 700, /* NB<->MB */ |
159 | | 9000, 700, /* MB<->WB */ |
160 | | 11000, 1000, /* WB<->SWB */ |
161 | | 12000, 2000, /* SWB<->FB */ |
162 | | }; |
163 | | static const opus_int32 stereo_voice_bandwidth_thresholds[8] = { |
164 | | 9000, 700, /* NB<->MB */ |
165 | | 9000, 700, /* MB<->WB */ |
166 | | 13500, 1000, /* WB<->SWB */ |
167 | | 14000, 2000, /* SWB<->FB */ |
168 | | }; |
169 | | static const opus_int32 stereo_music_bandwidth_thresholds[8] = { |
170 | | 9000, 700, /* NB<->MB */ |
171 | | 9000, 700, /* MB<->WB */ |
172 | | 11000, 1000, /* WB<->SWB */ |
173 | | 12000, 2000, /* SWB<->FB */ |
174 | | }; |
175 | | /* Threshold bit-rates for switching between mono and stereo */ |
176 | | static const opus_int32 stereo_voice_threshold = 19000; |
177 | | static const opus_int32 stereo_music_threshold = 17000; |
178 | | |
179 | | /* Threshold bit-rate for switching between SILK/hybrid and CELT-only */ |
180 | | static const opus_int32 mode_thresholds[2][2] = { |
181 | | /* voice */ /* music */ |
182 | | { 64000, 10000}, /* mono */ |
183 | | { 44000, 10000}, /* stereo */ |
184 | | }; |
185 | | |
186 | | static const opus_int32 fec_thresholds[] = { |
187 | | 12000, 1000, /* NB */ |
188 | | 14000, 1000, /* MB */ |
189 | | 16000, 1000, /* WB */ |
190 | | 20000, 1000, /* SWB */ |
191 | | 22000, 1000, /* FB */ |
192 | | }; |
193 | | |
194 | | int opus_encoder_get_size(int channels) |
195 | 0 | { |
196 | 0 | int ret; |
197 | 0 | ret = opus_encoder_init(NULL, 48000, channels, OPUS_APPLICATION_AUDIO); |
198 | 0 | if (ret < 0) |
199 | 0 | return 0; |
200 | 0 | else |
201 | 0 | return ret; |
202 | 0 | } |
203 | | |
204 | | int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int application) |
205 | 0 | { |
206 | 0 | void *silk_enc=NULL; |
207 | 0 | CELTEncoder *celt_enc=NULL; |
208 | 0 | int err; |
209 | 0 | int ret, silkEncSizeBytes, celtEncSizeBytes=0; |
210 | 0 | int tot_size; |
211 | 0 | int base_size; |
212 | |
|
213 | 0 | if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000 |
214 | | #ifdef ENABLE_QEXT |
215 | | &&Fs!=96000 |
216 | | #endif |
217 | 0 | )||(channels!=1&&channels!=2)|| |
218 | 0 | (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO |
219 | 0 | && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY |
220 | 0 | && application != OPUS_APPLICATION_RESTRICTED_SILK |
221 | 0 | && application != OPUS_APPLICATION_RESTRICTED_CELT)) |
222 | 0 | return OPUS_BAD_ARG; |
223 | | |
224 | | /* Create SILK encoder */ |
225 | 0 | ret = silk_Get_Encoder_Size( &silkEncSizeBytes, channels ); |
226 | 0 | if (ret) |
227 | 0 | return OPUS_BAD_ARG; |
228 | 0 | silkEncSizeBytes = align(silkEncSizeBytes); |
229 | 0 | if (application == OPUS_APPLICATION_RESTRICTED_CELT) |
230 | 0 | silkEncSizeBytes = 0; |
231 | 0 | if (application != OPUS_APPLICATION_RESTRICTED_SILK) |
232 | 0 | celtEncSizeBytes = celt_encoder_get_size(channels); |
233 | 0 | base_size = align(sizeof(OpusEncoder)); |
234 | 0 | if (application == OPUS_APPLICATION_RESTRICTED_SILK || application == OPUS_APPLICATION_RESTRICTED_CELT) { |
235 | 0 | base_size = align(base_size - MAX_ENCODER_BUFFER*2*sizeof(opus_res)); |
236 | 0 | } else if (channels==1) |
237 | 0 | base_size = align(base_size - MAX_ENCODER_BUFFER*sizeof(opus_res)); |
238 | 0 | tot_size = base_size+silkEncSizeBytes+celtEncSizeBytes; |
239 | 0 | if (st == NULL) { |
240 | 0 | return tot_size; |
241 | 0 | } |
242 | 0 | OPUS_CLEAR((char*)st, tot_size); |
243 | 0 | st->silk_enc_offset = base_size; |
244 | 0 | st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes; |
245 | |
|
246 | 0 | st->stream_channels = st->channels = channels; |
247 | |
|
248 | 0 | st->Fs = Fs; |
249 | |
|
250 | 0 | st->arch = opus_select_arch(); |
251 | |
|
252 | 0 | if (application != OPUS_APPLICATION_RESTRICTED_CELT) |
253 | 0 | { |
254 | 0 | silk_enc = (char*)st+st->silk_enc_offset; |
255 | 0 | ret = silk_InitEncoder( silk_enc, st->channels, st->arch, &st->silk_mode ); |
256 | 0 | } |
257 | 0 | if(ret)return OPUS_INTERNAL_ERROR; |
258 | | |
259 | | /* default SILK parameters */ |
260 | 0 | st->silk_mode.nChannelsAPI = channels; |
261 | 0 | st->silk_mode.nChannelsInternal = channels; |
262 | 0 | st->silk_mode.API_sampleRate = st->Fs; |
263 | 0 | st->silk_mode.maxInternalSampleRate = 16000; |
264 | 0 | st->silk_mode.minInternalSampleRate = 8000; |
265 | 0 | st->silk_mode.desiredInternalSampleRate = 16000; |
266 | 0 | st->silk_mode.payloadSize_ms = 20; |
267 | 0 | st->silk_mode.bitRate = 25000; |
268 | 0 | st->silk_mode.packetLossPercentage = 0; |
269 | 0 | st->silk_mode.complexity = 9; |
270 | 0 | st->silk_mode.useInBandFEC = 0; |
271 | 0 | st->silk_mode.useDRED = 0; |
272 | 0 | st->silk_mode.useDTX = 0; |
273 | 0 | st->silk_mode.useCBR = 0; |
274 | 0 | st->silk_mode.reducedDependency = 0; |
275 | | |
276 | | /* Create CELT encoder */ |
277 | | /* Initialize CELT encoder */ |
278 | 0 | if (application != OPUS_APPLICATION_RESTRICTED_SILK) |
279 | 0 | { |
280 | 0 | celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); |
281 | 0 | err = celt_encoder_init(celt_enc, Fs, channels, st->arch); |
282 | 0 | if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR; |
283 | 0 | celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0)); |
284 | 0 | celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(st->silk_mode.complexity)); |
285 | 0 | } |
286 | | |
287 | | #ifdef ENABLE_DRED |
288 | | /* Initialize DRED Encoder */ |
289 | | dred_encoder_init( &st->dred_encoder, Fs, channels ); |
290 | | #endif |
291 | | |
292 | 0 | st->use_vbr = 1; |
293 | | /* Makes constrained VBR the default (safer for real-time use) */ |
294 | 0 | st->vbr_constraint = 1; |
295 | 0 | st->user_bitrate_bps = OPUS_AUTO; |
296 | 0 | st->bitrate_bps = 3000+Fs*channels; |
297 | 0 | st->application = application; |
298 | 0 | st->signal_type = OPUS_AUTO; |
299 | 0 | st->user_bandwidth = OPUS_AUTO; |
300 | 0 | st->max_bandwidth = OPUS_BANDWIDTH_FULLBAND; |
301 | 0 | st->force_channels = OPUS_AUTO; |
302 | 0 | st->user_forced_mode = OPUS_AUTO; |
303 | 0 | st->voice_ratio = -1; |
304 | 0 | if (application != OPUS_APPLICATION_RESTRICTED_CELT && application != OPUS_APPLICATION_RESTRICTED_SILK) |
305 | 0 | st->encoder_buffer = st->Fs/100; |
306 | 0 | else |
307 | 0 | st->encoder_buffer = 0; |
308 | 0 | st->lsb_depth = 24; |
309 | 0 | st->variable_duration = OPUS_FRAMESIZE_ARG; |
310 | | |
311 | | /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead |
312 | | + 1.5 ms for SILK resamplers and stereo prediction) */ |
313 | 0 | st->delay_compensation = st->Fs/250; |
314 | |
|
315 | 0 | st->hybrid_stereo_width_Q14 = 1 << 14; |
316 | 0 | st->prev_HB_gain = Q15ONE; |
317 | 0 | st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); |
318 | 0 | st->first = 1; |
319 | 0 | st->mode = MODE_HYBRID; |
320 | 0 | st->bandwidth = OPUS_BANDWIDTH_FULLBAND; |
321 | |
|
322 | 0 | #ifndef DISABLE_FLOAT_API |
323 | 0 | tonality_analysis_init(&st->analysis, st->Fs); |
324 | 0 | st->analysis.application = st->application; |
325 | 0 | #endif |
326 | |
|
327 | 0 | return OPUS_OK; |
328 | 0 | } |
329 | | |
330 | | static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channels) |
331 | 0 | { |
332 | 0 | int period; |
333 | 0 | unsigned char toc; |
334 | 0 | period = 0; |
335 | 0 | while (framerate < 400) |
336 | 0 | { |
337 | 0 | framerate <<= 1; |
338 | 0 | period++; |
339 | 0 | } |
340 | 0 | if (mode == MODE_SILK_ONLY) |
341 | 0 | { |
342 | 0 | toc = (bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5; |
343 | 0 | toc |= (period-2)<<3; |
344 | 0 | } else if (mode == MODE_CELT_ONLY) |
345 | 0 | { |
346 | 0 | int tmp = bandwidth-OPUS_BANDWIDTH_MEDIUMBAND; |
347 | 0 | if (tmp < 0) |
348 | 0 | tmp = 0; |
349 | 0 | toc = 0x80; |
350 | 0 | toc |= tmp << 5; |
351 | 0 | toc |= period<<3; |
352 | 0 | } else /* Hybrid */ |
353 | 0 | { |
354 | 0 | toc = 0x60; |
355 | 0 | toc |= (bandwidth-OPUS_BANDWIDTH_SUPERWIDEBAND)<<4; |
356 | 0 | toc |= (period-2)<<3; |
357 | 0 | } |
358 | 0 | toc |= (channels==2)<<2; |
359 | 0 | return toc; |
360 | 0 | } |
361 | | |
362 | | #ifdef FIXED_POINT |
363 | | /* Second order ARMA filter, alternative implementation */ |
364 | | void silk_biquad_res( |
365 | | const opus_res *in, /* I input signal */ |
366 | | const opus_int32 *B_Q28, /* I MA coefficients [3] */ |
367 | | const opus_int32 *A_Q28, /* I AR coefficients [2] */ |
368 | | opus_int32 *S, /* I/O State vector [2] */ |
369 | | opus_res *out, /* O output signal */ |
370 | | const opus_int32 len, /* I signal length (must be even) */ |
371 | | int stride |
372 | | ) |
373 | | { |
374 | | /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */ |
375 | | opus_int k; |
376 | | opus_int32 inval, A0_U_Q28, A0_L_Q28, A1_U_Q28, A1_L_Q28, out32_Q14; |
377 | | |
378 | | /* Negate A_Q28 values and split in two parts */ |
379 | | A0_L_Q28 = ( -A_Q28[ 0 ] ) & 0x00003FFF; /* lower part */ |
380 | | A0_U_Q28 = silk_RSHIFT( -A_Q28[ 0 ], 14 ); /* upper part */ |
381 | | A1_L_Q28 = ( -A_Q28[ 1 ] ) & 0x00003FFF; /* lower part */ |
382 | | A1_U_Q28 = silk_RSHIFT( -A_Q28[ 1 ], 14 ); /* upper part */ |
383 | | |
384 | | for( k = 0; k < len; k++ ) { |
385 | | /* S[ 0 ], S[ 1 ]: Q12 */ |
386 | | inval = RES2INT16(in[ k*stride ]); |
387 | | out32_Q14 = silk_LSHIFT( silk_SMLAWB( S[ 0 ], B_Q28[ 0 ], inval ), 2 ); |
388 | | |
389 | | S[ 0 ] = S[1] + silk_RSHIFT_ROUND( silk_SMULWB( out32_Q14, A0_L_Q28 ), 14 ); |
390 | | S[ 0 ] = silk_SMLAWB( S[ 0 ], out32_Q14, A0_U_Q28 ); |
391 | | S[ 0 ] = silk_SMLAWB( S[ 0 ], B_Q28[ 1 ], inval); |
392 | | |
393 | | S[ 1 ] = silk_RSHIFT_ROUND( silk_SMULWB( out32_Q14, A1_L_Q28 ), 14 ); |
394 | | S[ 1 ] = silk_SMLAWB( S[ 1 ], out32_Q14, A1_U_Q28 ); |
395 | | S[ 1 ] = silk_SMLAWB( S[ 1 ], B_Q28[ 2 ], inval ); |
396 | | |
397 | | /* Scale back to Q0 and saturate */ |
398 | | out[ k*stride ] = INT16TORES( silk_SAT16( silk_RSHIFT( out32_Q14 + (1<<14) - 1, 14 ) ) ); |
399 | | } |
400 | | } |
401 | | #else |
402 | | static void silk_biquad_res( |
403 | | const opus_res *in, /* I: Input signal */ |
404 | | const opus_int32 *B_Q28, /* I: MA coefficients [3] */ |
405 | | const opus_int32 *A_Q28, /* I: AR coefficients [2] */ |
406 | | opus_val32 *S, /* I/O: State vector [2] */ |
407 | | opus_res *out, /* O: Output signal */ |
408 | | const opus_int32 len, /* I: Signal length (must be even) */ |
409 | | int stride |
410 | | ) |
411 | 0 | { |
412 | | /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */ |
413 | 0 | opus_int k; |
414 | 0 | opus_val32 vout; |
415 | 0 | opus_val32 inval; |
416 | 0 | opus_val32 A[2], B[3]; |
417 | |
|
418 | 0 | A[0] = (opus_val32)(A_Q28[0] * (1.f/((opus_int32)1<<28))); |
419 | 0 | A[1] = (opus_val32)(A_Q28[1] * (1.f/((opus_int32)1<<28))); |
420 | 0 | B[0] = (opus_val32)(B_Q28[0] * (1.f/((opus_int32)1<<28))); |
421 | 0 | B[1] = (opus_val32)(B_Q28[1] * (1.f/((opus_int32)1<<28))); |
422 | 0 | B[2] = (opus_val32)(B_Q28[2] * (1.f/((opus_int32)1<<28))); |
423 | | |
424 | | /* Negate A_Q28 values and split in two parts */ |
425 | |
|
426 | 0 | for( k = 0; k < len; k++ ) { |
427 | | /* S[ 0 ], S[ 1 ]: Q12 */ |
428 | 0 | inval = in[ k*stride ]; |
429 | 0 | vout = S[ 0 ] + B[0]*inval; |
430 | |
|
431 | 0 | S[ 0 ] = S[1] - vout*A[0] + B[1]*inval; |
432 | |
|
433 | 0 | S[ 1 ] = - vout*A[1] + B[2]*inval + VERY_SMALL; |
434 | | |
435 | | /* Scale back to Q0 and saturate */ |
436 | 0 | out[ k*stride ] = vout; |
437 | 0 | } |
438 | 0 | } |
439 | | #endif |
440 | | |
441 | | static void hp_cutoff(const opus_res *in, opus_int32 cutoff_Hz, opus_res *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs, int arch) |
442 | 0 | { |
443 | 0 | opus_int32 B_Q28[ 3 ], A_Q28[ 2 ]; |
444 | 0 | opus_int32 Fc_Q19, r_Q28, r_Q22; |
445 | 0 | (void)arch; |
446 | |
|
447 | 0 | silk_assert( cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ) ); |
448 | 0 | Fc_Q19 = silk_DIV32_16( silk_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 ); |
449 | 0 | silk_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 ); |
450 | |
|
451 | 0 | r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - silk_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q19 ); |
452 | | |
453 | | /* b = r * [ 1; -2; 1 ]; */ |
454 | | /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */ |
455 | 0 | B_Q28[ 0 ] = r_Q28; |
456 | 0 | B_Q28[ 1 ] = silk_LSHIFT( -r_Q28, 1 ); |
457 | 0 | B_Q28[ 2 ] = r_Q28; |
458 | | |
459 | | /* -r * ( 2 - Fc * Fc ); */ |
460 | 0 | r_Q22 = silk_RSHIFT( r_Q28, 6 ); |
461 | 0 | A_Q28[ 0 ] = silk_SMULWW( r_Q22, silk_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CONST( 2.0, 22 ) ); |
462 | 0 | A_Q28[ 1 ] = silk_SMULWW( r_Q22, r_Q22 ); |
463 | |
|
464 | | #if defined(FIXED_POINT) && !defined(ENABLE_RES24) |
465 | | if( channels == 1 ) { |
466 | | silk_biquad_alt_stride1( in, B_Q28, A_Q28, hp_mem, out, len ); |
467 | | } else { |
468 | | silk_biquad_alt_stride2( in, B_Q28, A_Q28, hp_mem, out, len, arch ); |
469 | | } |
470 | | #else |
471 | 0 | silk_biquad_res( in, B_Q28, A_Q28, hp_mem, out, len, channels ); |
472 | 0 | if( channels == 2 ) { |
473 | 0 | silk_biquad_res( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels ); |
474 | 0 | } |
475 | 0 | #endif |
476 | 0 | } |
477 | | |
478 | | #ifdef FIXED_POINT |
479 | | static void dc_reject(const opus_res *in, opus_int32 cutoff_Hz, opus_res *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs) |
480 | | { |
481 | | int c, i; |
482 | | int shift; |
483 | | |
484 | | /* Approximates -round(log2(6.3*cutoff_Hz/Fs)) */ |
485 | | shift=celt_ilog2(Fs/(cutoff_Hz*4)); |
486 | | for (c=0;c<channels;c++) |
487 | | { |
488 | | for (i=0;i<len;i++) |
489 | | { |
490 | | opus_val32 x, y; |
491 | | /* Saturate at +6 dBFS to avoid any wrap-around. */ |
492 | | x = SATURATE((opus_val32)in[channels*i+c], (1<<16<<RES_SHIFT)-1); |
493 | | x = SHL32(x, 14-RES_SHIFT); |
494 | | y = x-hp_mem[2*c]; |
495 | | hp_mem[2*c] = hp_mem[2*c] + PSHR32(x - hp_mem[2*c], shift); |
496 | | #ifdef ENABLE_RES24 |
497 | | /* Don't saturate if we have the headroom to avoid it. */ |
498 | | out[channels*i+c] = PSHR32(y, 14-RES_SHIFT); |
499 | | #else |
500 | | out[channels*i+c] = SATURATE(PSHR32(y, 14-RES_SHIFT), 32767); |
501 | | #endif |
502 | | } |
503 | | } |
504 | | } |
505 | | |
506 | | #else |
507 | | static void dc_reject(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs) |
508 | 0 | { |
509 | 0 | int i; |
510 | 0 | float coef, coef2; |
511 | 0 | coef = 6.3f*cutoff_Hz/Fs; |
512 | 0 | coef2 = 1-coef; |
513 | 0 | if (channels==2) |
514 | 0 | { |
515 | 0 | float m0, m2; |
516 | 0 | m0 = hp_mem[0]; |
517 | 0 | m2 = hp_mem[2]; |
518 | 0 | for (i=0;i<len;i++) |
519 | 0 | { |
520 | 0 | opus_val32 x0, x1, out0, out1; |
521 | 0 | x0 = in[2*i+0]; |
522 | 0 | x1 = in[2*i+1]; |
523 | 0 | out0 = x0-m0; |
524 | 0 | out1 = x1-m2; |
525 | 0 | m0 = coef*x0 + VERY_SMALL + coef2*m0; |
526 | 0 | m2 = coef*x1 + VERY_SMALL + coef2*m2; |
527 | 0 | out[2*i+0] = out0; |
528 | 0 | out[2*i+1] = out1; |
529 | 0 | } |
530 | 0 | hp_mem[0] = m0; |
531 | 0 | hp_mem[2] = m2; |
532 | 0 | } else { |
533 | 0 | float m0; |
534 | 0 | m0 = hp_mem[0]; |
535 | 0 | for (i=0;i<len;i++) |
536 | 0 | { |
537 | 0 | opus_val32 x, y; |
538 | 0 | x = in[i]; |
539 | 0 | y = x-m0; |
540 | 0 | m0 = coef*x + VERY_SMALL + coef2*m0; |
541 | 0 | out[i] = y; |
542 | 0 | } |
543 | 0 | hp_mem[0] = m0; |
544 | 0 | } |
545 | 0 | } |
546 | | #endif |
547 | | |
548 | | static void stereo_fade(const opus_res *in, opus_res *out, opus_val16 g1, opus_val16 g2, |
549 | | int overlap48, int frame_size, int channels, const celt_coef *window, opus_int32 Fs) |
550 | 0 | { |
551 | 0 | int i; |
552 | 0 | int overlap; |
553 | 0 | int inc; |
554 | 0 | inc = IMAX(1, 48000/Fs); |
555 | 0 | overlap=overlap48/inc; |
556 | 0 | g1 = Q15ONE-g1; |
557 | 0 | g2 = Q15ONE-g2; |
558 | 0 | for (i=0;i<overlap;i++) |
559 | 0 | { |
560 | 0 | opus_val32 diff; |
561 | 0 | opus_val16 g, w; |
562 | 0 | w = COEF2VAL16(window[i*inc]); |
563 | 0 | w = MULT16_16_Q15(w, w); |
564 | 0 | g = SHR32(MAC16_16(MULT16_16(w,g2), |
565 | 0 | Q15ONE-w, g1), 15); |
566 | 0 | diff = HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]); |
567 | 0 | diff = MULT16_RES_Q15(g, diff); |
568 | 0 | out[i*channels] = out[i*channels] - diff; |
569 | 0 | out[i*channels+1] = out[i*channels+1] + diff; |
570 | 0 | } |
571 | 0 | for (;i<frame_size;i++) |
572 | 0 | { |
573 | 0 | opus_val32 diff; |
574 | 0 | diff = HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]); |
575 | 0 | diff = MULT16_RES_Q15(g2, diff); |
576 | 0 | out[i*channels] = out[i*channels] - diff; |
577 | 0 | out[i*channels+1] = out[i*channels+1] + diff; |
578 | 0 | } |
579 | 0 | } |
580 | | |
581 | | static void gain_fade(const opus_res *in, opus_res *out, opus_val16 g1, opus_val16 g2, |
582 | | int overlap48, int frame_size, int channels, const celt_coef *window, opus_int32 Fs) |
583 | 0 | { |
584 | 0 | int i; |
585 | 0 | int inc; |
586 | 0 | int overlap; |
587 | 0 | int c; |
588 | 0 | inc = IMAX(1, 48000/Fs); |
589 | 0 | overlap=overlap48/inc; |
590 | 0 | if (channels==1) |
591 | 0 | { |
592 | 0 | for (i=0;i<overlap;i++) |
593 | 0 | { |
594 | 0 | opus_val16 g, w; |
595 | 0 | w = COEF2VAL16(window[i*inc]); |
596 | 0 | w = MULT16_16_Q15(w, w); |
597 | 0 | g = SHR32(MAC16_16(MULT16_16(w,g2), |
598 | 0 | Q15ONE-w, g1), 15); |
599 | 0 | out[i] = MULT16_RES_Q15(g, in[i]); |
600 | 0 | } |
601 | 0 | } else { |
602 | 0 | for (i=0;i<overlap;i++) |
603 | 0 | { |
604 | 0 | opus_val16 g, w; |
605 | 0 | w = COEF2VAL16(window[i*inc]); |
606 | 0 | w = MULT16_16_Q15(w, w); |
607 | 0 | g = SHR32(MAC16_16(MULT16_16(w,g2), |
608 | 0 | Q15ONE-w, g1), 15); |
609 | 0 | out[i*2] = MULT16_RES_Q15(g, in[i*2]); |
610 | 0 | out[i*2+1] = MULT16_RES_Q15(g, in[i*2+1]); |
611 | 0 | } |
612 | 0 | } |
613 | 0 | c=0;do { |
614 | 0 | for (i=overlap;i<frame_size;i++) |
615 | 0 | { |
616 | 0 | out[i*channels+c] = MULT16_RES_Q15(g2, in[i*channels+c]); |
617 | 0 | } |
618 | 0 | } |
619 | 0 | while (++c<channels); |
620 | 0 | } |
621 | | |
622 | | OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error) |
623 | 0 | { |
624 | 0 | int ret; |
625 | 0 | OpusEncoder *st; |
626 | 0 | int size; |
627 | 0 | if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000 |
628 | | #ifdef ENABLE_QEXT |
629 | | &&Fs!=96000 |
630 | | #endif |
631 | 0 | )||(channels!=1&&channels!=2)|| |
632 | 0 | (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO |
633 | 0 | && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY |
634 | 0 | && application != OPUS_APPLICATION_RESTRICTED_SILK |
635 | 0 | && application != OPUS_APPLICATION_RESTRICTED_CELT)) |
636 | 0 | { |
637 | 0 | if (error) |
638 | 0 | *error = OPUS_BAD_ARG; |
639 | 0 | return NULL; |
640 | 0 | } |
641 | 0 | size = opus_encoder_init(NULL, Fs, channels, application); |
642 | 0 | if (size <= 0) |
643 | 0 | { |
644 | 0 | if (error) |
645 | 0 | *error = OPUS_INTERNAL_ERROR; |
646 | 0 | return NULL; |
647 | 0 | } |
648 | 0 | st = (OpusEncoder *)opus_alloc(size); |
649 | 0 | if (st == NULL) |
650 | 0 | { |
651 | 0 | if (error) |
652 | 0 | *error = OPUS_ALLOC_FAIL; |
653 | 0 | return NULL; |
654 | 0 | } |
655 | 0 | ret = opus_encoder_init(st, Fs, channels, application); |
656 | 0 | if (error) |
657 | 0 | *error = ret; |
658 | 0 | if (ret != OPUS_OK) |
659 | 0 | { |
660 | 0 | opus_free(st); |
661 | 0 | st = NULL; |
662 | 0 | } |
663 | 0 | return st; |
664 | 0 | } |
665 | | |
666 | | #ifdef ENABLE_DRED |
667 | | |
668 | | static const float dred_bits_table[16] = {73.2f, 68.1f, 62.5f, 57.0f, 51.5f, 45.7f, 39.9f, 32.4f, 26.4f, 20.4f, 16.3f, 13.f, 9.3f, 8.2f, 7.2f, 6.4f}; |
669 | | static int estimate_dred_bitrate(int q0, int dQ, int qmax, int duration, opus_int32 target_bits, int *target_chunks) { |
670 | | int dred_chunks; |
671 | | int i; |
672 | | float bits; |
673 | | /* Signaling DRED costs 3 bytes. */ |
674 | | bits = 8*(3+DRED_EXPERIMENTAL_BYTES); |
675 | | /* Approximation for the size of the IS. */ |
676 | | bits += 50.f+dred_bits_table[q0]; |
677 | | dred_chunks = IMIN((duration+5)/4, DRED_NUM_REDUNDANCY_FRAMES/2); |
678 | | if (target_chunks != NULL) *target_chunks = 0; |
679 | | for (i=0;i<dred_chunks;i++) { |
680 | | int q = compute_quantizer(q0, dQ, qmax, i); |
681 | | bits += dred_bits_table[q]; |
682 | | if (target_chunks != NULL && bits < target_bits) *target_chunks = i+1; |
683 | | } |
684 | | return (int)floor(.5f+bits); |
685 | | } |
686 | | |
687 | | static opus_int32 compute_dred_bitrate(OpusEncoder *st, opus_int32 bitrate_bps, int frame_size) |
688 | | { |
689 | | float dred_frac; |
690 | | int bitrate_offset; |
691 | | opus_int32 dred_bitrate; |
692 | | opus_int32 target_dred_bitrate; |
693 | | int target_chunks; |
694 | | opus_int32 max_dred_bits; |
695 | | int q0, dQ, qmax; |
696 | | if (st->silk_mode.useInBandFEC) { |
697 | | dred_frac = MIN16(.7f, 3.f*st->silk_mode.packetLossPercentage/100.f); |
698 | | bitrate_offset = 20000; |
699 | | } else { |
700 | | if (st->silk_mode.packetLossPercentage > 5) { |
701 | | dred_frac = MIN16(.8f, .55f + st->silk_mode.packetLossPercentage/100.f); |
702 | | } else { |
703 | | dred_frac = 12*st->silk_mode.packetLossPercentage/100.f; |
704 | | } |
705 | | bitrate_offset = 12000; |
706 | | } |
707 | | /* Account for the fact that longer packets require less redundancy. */ |
708 | | dred_frac = dred_frac/(dred_frac + (1-dred_frac)*(frame_size*50.f)/st->Fs); |
709 | | /* Approximate fit based on a few experiments. Could probably be improved. */ |
710 | | q0 = IMIN(15, IMAX(4, 51 - 3*EC_ILOG(IMAX(1, bitrate_bps-bitrate_offset)))); |
711 | | dQ = bitrate_bps-bitrate_offset > 36000 ? 3 : 5; |
712 | | qmax = 15; |
713 | | target_dred_bitrate = IMAX(0, (int)(dred_frac*(bitrate_bps-bitrate_offset))); |
714 | | if (st->dred_duration > 0) { |
715 | | opus_int32 target_bits = bitrate_to_bits(target_dred_bitrate, st->Fs, frame_size); |
716 | | max_dred_bits = estimate_dred_bitrate(q0, dQ, qmax, st->dred_duration, target_bits, &target_chunks); |
717 | | } else { |
718 | | max_dred_bits = 0; |
719 | | target_chunks=0; |
720 | | } |
721 | | dred_bitrate = IMIN(target_dred_bitrate, bits_to_bitrate(max_dred_bits, st->Fs, frame_size)); |
722 | | /* If we can't afford enough bits, don't bother with DRED at all. */ |
723 | | if (target_chunks < 2) |
724 | | dred_bitrate = 0; |
725 | | st->dred_q0 = q0; |
726 | | st->dred_dQ = dQ; |
727 | | st->dred_qmax = qmax; |
728 | | st->dred_target_chunks = target_chunks; |
729 | | return dred_bitrate; |
730 | | } |
731 | | #endif |
732 | | |
733 | | static opus_int32 user_bitrate_to_bitrate(OpusEncoder *st, int frame_size, int max_data_bytes) |
734 | 0 | { |
735 | 0 | opus_int32 max_bitrate, user_bitrate; |
736 | 0 | if(!frame_size)frame_size=st->Fs/400; |
737 | 0 | max_bitrate = bits_to_bitrate(max_data_bytes*8, st->Fs, frame_size); |
738 | 0 | if (st->user_bitrate_bps==OPUS_AUTO) |
739 | 0 | user_bitrate = 60*st->Fs/frame_size + st->Fs*st->channels; |
740 | 0 | else if (st->user_bitrate_bps==OPUS_BITRATE_MAX) |
741 | 0 | user_bitrate = 1500000; |
742 | 0 | else |
743 | 0 | user_bitrate = st->user_bitrate_bps; |
744 | 0 | return IMIN(user_bitrate, max_bitrate); |
745 | 0 | } |
746 | | |
747 | | #ifndef DISABLE_FLOAT_API |
748 | | void downmix_float(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C) |
749 | 0 | { |
750 | 0 | const float *x; |
751 | 0 | int j; |
752 | |
|
753 | 0 | x = (const float *)_x; |
754 | 0 | for (j=0;j<subframe;j++) |
755 | 0 | y[j] = FLOAT2SIG(x[(j+offset)*C+c1]); |
756 | 0 | if (c2>-1) |
757 | 0 | { |
758 | 0 | for (j=0;j<subframe;j++) |
759 | 0 | y[j] += FLOAT2SIG(x[(j+offset)*C+c2]); |
760 | 0 | } else if (c2==-2) |
761 | 0 | { |
762 | 0 | int c; |
763 | 0 | for (c=1;c<C;c++) |
764 | 0 | { |
765 | 0 | for (j=0;j<subframe;j++) |
766 | 0 | y[j] += FLOAT2SIG(x[(j+offset)*C+c]); |
767 | 0 | } |
768 | 0 | } |
769 | 0 | #ifndef FIXED_POINT |
770 | | /* Cap signal to +6 dBFS to avoid problems in the analysis. */ |
771 | 0 | for (j=0;j<subframe;j++) |
772 | 0 | { |
773 | 0 | if (y[j] < -65536.f) y[j] = -65536.f; |
774 | 0 | if (y[j] > 65536.f) y[j] = 65536.f; |
775 | 0 | if (celt_isnan(y[j])) y[j] = 0; |
776 | 0 | } |
777 | 0 | #endif |
778 | 0 | } |
779 | | #endif |
780 | | |
781 | | void downmix_int(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C) |
782 | 0 | { |
783 | 0 | const opus_int16 *x; |
784 | 0 | int j; |
785 | |
|
786 | 0 | x = (const opus_int16 *)_x; |
787 | 0 | for (j=0;j<subframe;j++) |
788 | 0 | y[j] = INT16TOSIG(x[(j+offset)*C+c1]); |
789 | 0 | if (c2>-1) |
790 | 0 | { |
791 | 0 | for (j=0;j<subframe;j++) |
792 | 0 | y[j] += INT16TOSIG(x[(j+offset)*C+c2]); |
793 | 0 | } else if (c2==-2) |
794 | 0 | { |
795 | 0 | int c; |
796 | 0 | for (c=1;c<C;c++) |
797 | 0 | { |
798 | 0 | for (j=0;j<subframe;j++) |
799 | 0 | y[j] += INT16TOSIG(x[(j+offset)*C+c]); |
800 | 0 | } |
801 | 0 | } |
802 | 0 | } |
803 | | |
804 | | void downmix_int24(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C) |
805 | 0 | { |
806 | 0 | const opus_int32 *x; |
807 | 0 | int j; |
808 | |
|
809 | 0 | x = (const opus_int32 *)_x; |
810 | 0 | for (j=0;j<subframe;j++) |
811 | 0 | y[j] = INT24TOSIG(x[(j+offset)*C+c1]); |
812 | 0 | if (c2>-1) |
813 | 0 | { |
814 | 0 | for (j=0;j<subframe;j++) |
815 | 0 | y[j] += INT24TOSIG(x[(j+offset)*C+c2]); |
816 | 0 | } else if (c2==-2) |
817 | 0 | { |
818 | 0 | int c; |
819 | 0 | for (c=1;c<C;c++) |
820 | 0 | { |
821 | 0 | for (j=0;j<subframe;j++) |
822 | 0 | y[j] += INT24TOSIG(x[(j+offset)*C+c]); |
823 | 0 | } |
824 | 0 | } |
825 | 0 | } |
826 | | |
827 | | opus_int32 frame_size_select(int application, opus_int32 frame_size, int variable_duration, opus_int32 Fs) |
828 | 0 | { |
829 | 0 | int new_size; |
830 | 0 | if (frame_size<Fs/400) |
831 | 0 | return -1; |
832 | 0 | if (variable_duration == OPUS_FRAMESIZE_ARG) |
833 | 0 | new_size = frame_size; |
834 | 0 | else if (variable_duration >= OPUS_FRAMESIZE_2_5_MS && variable_duration <= OPUS_FRAMESIZE_120_MS) |
835 | 0 | { |
836 | 0 | if (variable_duration <= OPUS_FRAMESIZE_40_MS) |
837 | 0 | new_size = (Fs/400)<<(variable_duration-OPUS_FRAMESIZE_2_5_MS); |
838 | 0 | else |
839 | 0 | new_size = (variable_duration-OPUS_FRAMESIZE_2_5_MS-2)*Fs/50; |
840 | 0 | } |
841 | 0 | else |
842 | 0 | return -1; |
843 | 0 | if (new_size>frame_size) |
844 | 0 | return -1; |
845 | 0 | if (400*new_size!=Fs && 200*new_size!=Fs && 100*new_size!=Fs && |
846 | 0 | 50*new_size!=Fs && 25*new_size!=Fs && 50*new_size!=3*Fs && |
847 | 0 | 50*new_size!=4*Fs && 50*new_size!=5*Fs && 50*new_size!=6*Fs) |
848 | 0 | return -1; |
849 | 0 | if (application == OPUS_APPLICATION_RESTRICTED_SILK && new_size < Fs/100) |
850 | 0 | return -1; |
851 | 0 | return new_size; |
852 | 0 | } |
853 | | |
854 | | opus_val16 compute_stereo_width(const opus_res *pcm, int frame_size, opus_int32 Fs, StereoWidthState *mem) |
855 | 0 | { |
856 | 0 | opus_val32 xx, xy, yy; |
857 | 0 | opus_val16 sqrt_xx, sqrt_yy; |
858 | 0 | opus_val16 qrrt_xx, qrrt_yy; |
859 | 0 | int frame_rate; |
860 | 0 | int i; |
861 | 0 | opus_val16 short_alpha; |
862 | | #ifdef FIXED_POINT |
863 | | int shift = celt_ilog2(frame_size)-2; |
864 | | #endif |
865 | 0 | frame_rate = Fs/frame_size; |
866 | 0 | short_alpha = MULT16_16(25, Q15ONE)/IMAX(50,frame_rate); |
867 | 0 | xx=xy=yy=0; |
868 | | /* Unroll by 4. The frame size is always a multiple of 4 *except* for |
869 | | 2.5 ms frames at 12 kHz. Since this setting is very rare (and very |
870 | | stupid), we just discard the last two samples. */ |
871 | 0 | for (i=0;i<frame_size-3;i+=4) |
872 | 0 | { |
873 | 0 | opus_val32 pxx=0; |
874 | 0 | opus_val32 pxy=0; |
875 | 0 | opus_val32 pyy=0; |
876 | 0 | opus_val16 x, y; |
877 | 0 | x = RES2VAL16(pcm[2*i]); |
878 | 0 | y = RES2VAL16(pcm[2*i+1]); |
879 | 0 | pxx = SHR32(MULT16_16(x,x),2); |
880 | 0 | pxy = SHR32(MULT16_16(x,y),2); |
881 | 0 | pyy = SHR32(MULT16_16(y,y),2); |
882 | 0 | x = RES2VAL16(pcm[2*i+2]); |
883 | 0 | y = RES2VAL16(pcm[2*i+3]); |
884 | 0 | pxx += SHR32(MULT16_16(x,x),2); |
885 | 0 | pxy += SHR32(MULT16_16(x,y),2); |
886 | 0 | pyy += SHR32(MULT16_16(y,y),2); |
887 | 0 | x = RES2VAL16(pcm[2*i+4]); |
888 | 0 | y = RES2VAL16(pcm[2*i+5]); |
889 | 0 | pxx += SHR32(MULT16_16(x,x),2); |
890 | 0 | pxy += SHR32(MULT16_16(x,y),2); |
891 | 0 | pyy += SHR32(MULT16_16(y,y),2); |
892 | 0 | x = RES2VAL16(pcm[2*i+6]); |
893 | 0 | y = RES2VAL16(pcm[2*i+7]); |
894 | 0 | pxx += SHR32(MULT16_16(x,x),2); |
895 | 0 | pxy += SHR32(MULT16_16(x,y),2); |
896 | 0 | pyy += SHR32(MULT16_16(y,y),2); |
897 | |
|
898 | 0 | xx += SHR32(pxx, shift); |
899 | 0 | xy += SHR32(pxy, shift); |
900 | 0 | yy += SHR32(pyy, shift); |
901 | 0 | } |
902 | 0 | #ifndef FIXED_POINT |
903 | 0 | if (!(xx < 1e9f) || celt_isnan(xx) || !(yy < 1e9f) || celt_isnan(yy)) |
904 | 0 | { |
905 | 0 | xy = xx = yy = 0; |
906 | 0 | } |
907 | 0 | #endif |
908 | 0 | mem->XX += MULT16_32_Q15(short_alpha, xx-mem->XX); |
909 | | /*mem->XY += MULT16_32_Q15(short_alpha, xy-mem->XY);*/ |
910 | | /* Rewritten to avoid overflows on abrupt sign change. */ |
911 | 0 | mem->XY = MULT16_32_Q15(Q15ONE - short_alpha, mem->XY) + MULT16_32_Q15(short_alpha, xy); |
912 | 0 | mem->YY += MULT16_32_Q15(short_alpha, yy-mem->YY); |
913 | 0 | mem->XX = MAX32(0, mem->XX); |
914 | 0 | mem->XY = MAX32(0, mem->XY); |
915 | 0 | mem->YY = MAX32(0, mem->YY); |
916 | 0 | if (MAX32(mem->XX, mem->YY)>QCONST16(8e-4f, 18)) |
917 | 0 | { |
918 | 0 | opus_val16 corr; |
919 | 0 | opus_val16 ldiff; |
920 | 0 | opus_val16 width; |
921 | 0 | sqrt_xx = celt_sqrt(mem->XX); |
922 | 0 | sqrt_yy = celt_sqrt(mem->YY); |
923 | 0 | qrrt_xx = celt_sqrt(sqrt_xx); |
924 | 0 | qrrt_yy = celt_sqrt(sqrt_yy); |
925 | | /* Inter-channel correlation */ |
926 | 0 | mem->XY = MIN32(mem->XY, sqrt_xx*sqrt_yy); |
927 | 0 | corr = SHR32(frac_div32(mem->XY,EPSILON+MULT16_16(sqrt_xx,sqrt_yy)),16); |
928 | | /* Approximate loudness difference */ |
929 | 0 | ldiff = MULT16_16(Q15ONE, ABS16(qrrt_xx-qrrt_yy))/(EPSILON+qrrt_xx+qrrt_yy); |
930 | 0 | width = MULT16_16_Q15(MIN16(Q15ONE, celt_sqrt(QCONST32(1.f,30)-MULT16_16(corr,corr))), ldiff); |
931 | | /* Smoothing over one second */ |
932 | 0 | mem->smoothed_width += (width-mem->smoothed_width)/frame_rate; |
933 | | /* Peak follower */ |
934 | 0 | mem->max_follower = MAX16(mem->max_follower-QCONST16(.02f,15)/frame_rate, mem->smoothed_width); |
935 | 0 | } |
936 | | /*printf("%f %f %f %f %f ", corr/(float)Q15ONE, ldiff/(float)Q15ONE, width/(float)Q15ONE, mem->smoothed_width/(float)Q15ONE, mem->max_follower/(float)Q15ONE);*/ |
937 | 0 | return EXTRACT16(MIN32(Q15ONE, MULT16_16(20, mem->max_follower))); |
938 | 0 | } |
939 | | |
940 | | static int decide_fec(int useInBandFEC, int PacketLoss_perc, int last_fec, int mode, int *bandwidth, opus_int32 rate) |
941 | 0 | { |
942 | 0 | int orig_bandwidth; |
943 | 0 | if (!useInBandFEC || PacketLoss_perc == 0 || mode == MODE_CELT_ONLY) |
944 | 0 | return 0; |
945 | 0 | orig_bandwidth = *bandwidth; |
946 | 0 | for (;;) |
947 | 0 | { |
948 | 0 | opus_int32 hysteresis; |
949 | 0 | opus_int32 LBRR_rate_thres_bps; |
950 | | /* Compute threshold for using FEC at the current bandwidth setting */ |
951 | 0 | LBRR_rate_thres_bps = fec_thresholds[2*(*bandwidth - OPUS_BANDWIDTH_NARROWBAND)]; |
952 | 0 | hysteresis = fec_thresholds[2*(*bandwidth - OPUS_BANDWIDTH_NARROWBAND) + 1]; |
953 | 0 | if (last_fec == 1) LBRR_rate_thres_bps -= hysteresis; |
954 | 0 | if (last_fec == 0) LBRR_rate_thres_bps += hysteresis; |
955 | 0 | LBRR_rate_thres_bps = silk_SMULWB( silk_MUL( LBRR_rate_thres_bps, |
956 | 0 | 125 - silk_min( PacketLoss_perc, 25 ) ), SILK_FIX_CONST( 0.01, 16 ) ); |
957 | | /* If loss <= 5%, we look at whether we have enough rate to enable FEC. |
958 | | If loss > 5%, we decrease the bandwidth until we can enable FEC. */ |
959 | 0 | if (rate > LBRR_rate_thres_bps) |
960 | 0 | return 1; |
961 | 0 | else if (PacketLoss_perc <= 5) |
962 | 0 | return 0; |
963 | 0 | else if (*bandwidth > OPUS_BANDWIDTH_NARROWBAND) |
964 | 0 | (*bandwidth)--; |
965 | 0 | else |
966 | 0 | break; |
967 | 0 | } |
968 | | /* Couldn't find any bandwidth to enable FEC, keep original bandwidth. */ |
969 | 0 | *bandwidth = orig_bandwidth; |
970 | 0 | return 0; |
971 | 0 | } |
972 | | |
973 | 0 | static int compute_silk_rate_for_hybrid(int rate, int bandwidth, int frame20ms, int vbr, int fec, int channels) { |
974 | 0 | int entry; |
975 | 0 | int i; |
976 | 0 | int N; |
977 | 0 | int silk_rate; |
978 | 0 | static const int rate_table[][5] = { |
979 | | /* |total| |-------- SILK------------| |
980 | | |-- No FEC -| |--- FEC ---| |
981 | | 10ms 20ms 10ms 20ms */ |
982 | 0 | { 0, 0, 0, 0, 0}, |
983 | 0 | {12000, 10000, 10000, 11000, 11000}, |
984 | 0 | {16000, 13500, 13500, 15000, 15000}, |
985 | 0 | {20000, 16000, 16000, 18000, 18000}, |
986 | 0 | {24000, 18000, 18000, 21000, 21000}, |
987 | 0 | {32000, 22000, 22000, 28000, 28000}, |
988 | 0 | {64000, 38000, 38000, 50000, 50000} |
989 | 0 | }; |
990 | | /* Do the allocation per-channel. */ |
991 | 0 | rate /= channels; |
992 | 0 | entry = 1 + frame20ms + 2*fec; |
993 | 0 | N = sizeof(rate_table)/sizeof(rate_table[0]); |
994 | 0 | for (i=1;i<N;i++) |
995 | 0 | { |
996 | 0 | if (rate_table[i][0] > rate) break; |
997 | 0 | } |
998 | 0 | if (i == N) |
999 | 0 | { |
1000 | 0 | silk_rate = rate_table[i-1][entry]; |
1001 | | /* For now, just give 50% of the extra bits to SILK. */ |
1002 | 0 | silk_rate += (rate-rate_table[i-1][0])/2; |
1003 | 0 | } else { |
1004 | 0 | opus_int32 lo, hi, x0, x1; |
1005 | 0 | lo = rate_table[i-1][entry]; |
1006 | 0 | hi = rate_table[i][entry]; |
1007 | 0 | x0 = rate_table[i-1][0]; |
1008 | 0 | x1 = rate_table[i][0]; |
1009 | 0 | silk_rate = (lo*(x1-rate) + hi*(rate-x0))/(x1-x0); |
1010 | 0 | } |
1011 | 0 | if (!vbr) |
1012 | 0 | { |
1013 | | /* Tiny boost to SILK for CBR. We should probably tune this better. */ |
1014 | 0 | silk_rate += 100; |
1015 | 0 | } |
1016 | 0 | if (bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND) |
1017 | 0 | silk_rate += 300; |
1018 | 0 | silk_rate *= channels; |
1019 | | /* Small adjustment for stereo (calibrated for 32 kb/s, haven't tried other bitrates). */ |
1020 | 0 | if (channels == 2 && rate >= 12000) |
1021 | 0 | silk_rate -= 1000; |
1022 | 0 | return silk_rate; |
1023 | 0 | } |
1024 | | |
1025 | | /* Returns the equivalent bitrate corresponding to 20 ms frames, |
1026 | | complexity 10 VBR operation. */ |
1027 | | static opus_int32 compute_equiv_rate(opus_int32 bitrate, int channels, |
1028 | | int frame_rate, int vbr, int mode, int complexity, int loss) |
1029 | 0 | { |
1030 | 0 | opus_int32 equiv; |
1031 | 0 | equiv = bitrate; |
1032 | | /* Take into account overhead from smaller frames. */ |
1033 | 0 | if (frame_rate > 50) |
1034 | 0 | equiv -= (40*channels+20)*(frame_rate - 50); |
1035 | | /* CBR is about a 8% penalty for both SILK and CELT. */ |
1036 | 0 | if (!vbr) |
1037 | 0 | equiv -= equiv/12; |
1038 | | /* Complexity makes about 10% difference (from 0 to 10) in general. */ |
1039 | 0 | equiv = equiv * (90+complexity)/100; |
1040 | 0 | if (mode == MODE_SILK_ONLY || mode == MODE_HYBRID) |
1041 | 0 | { |
1042 | | /* SILK complexity 0-1 uses the non-delayed-decision NSQ, which |
1043 | | costs about 20%. */ |
1044 | 0 | if (complexity<2) |
1045 | 0 | equiv = equiv*4/5; |
1046 | 0 | equiv -= equiv*loss/(6*loss + 10); |
1047 | 0 | } else if (mode == MODE_CELT_ONLY) { |
1048 | | /* CELT complexity 0-4 doesn't have the pitch filter, which costs |
1049 | | about 10%. */ |
1050 | 0 | if (complexity<5) |
1051 | 0 | equiv = equiv*9/10; |
1052 | 0 | } else { |
1053 | | /* Mode not known yet */ |
1054 | | /* Half the SILK loss*/ |
1055 | 0 | equiv -= equiv*loss/(12*loss + 20); |
1056 | 0 | } |
1057 | 0 | return equiv; |
1058 | 0 | } |
1059 | | |
1060 | | int is_digital_silence(const opus_res* pcm, int frame_size, int channels, int lsb_depth) |
1061 | 0 | { |
1062 | 0 | int silence = 0; |
1063 | 0 | opus_val32 sample_max = 0; |
1064 | | #ifdef MLP_TRAINING |
1065 | | return 0; |
1066 | | #endif |
1067 | 0 | sample_max = celt_maxabs_res(pcm, frame_size*channels); |
1068 | |
|
1069 | | #ifdef FIXED_POINT |
1070 | | silence = (sample_max == 0); |
1071 | | (void)lsb_depth; |
1072 | | #else |
1073 | 0 | silence = (sample_max <= (opus_val16) 1 / (1 << lsb_depth)); |
1074 | 0 | #endif |
1075 | |
|
1076 | 0 | return silence; |
1077 | 0 | } |
1078 | | |
1079 | | #ifdef FIXED_POINT |
1080 | | static opus_val32 compute_frame_energy(const opus_res *pcm, int frame_size, int channels, int arch) |
1081 | | { |
1082 | | int i; |
1083 | | opus_val32 sample_max; |
1084 | | int max_shift; |
1085 | | int shift; |
1086 | | opus_val32 energy = 0; |
1087 | | int len = frame_size*channels; |
1088 | | (void)arch; |
1089 | | /* Max amplitude in the signal */ |
1090 | | sample_max = RES2INT16(celt_maxabs_res(pcm, len)); |
1091 | | |
1092 | | /* Compute the right shift required in the MAC to avoid an overflow */ |
1093 | | max_shift = celt_ilog2(len); |
1094 | | shift = IMAX(0, (celt_ilog2(1+sample_max) << 1) + max_shift - 28); |
1095 | | |
1096 | | /* Compute the energy */ |
1097 | | for (i=0; i<len; i++) |
1098 | | energy += SHR32(MULT16_16(RES2INT16(pcm[i]), RES2INT16(pcm[i])), shift); |
1099 | | |
1100 | | /* Normalize energy by the frame size and left-shift back to the original position */ |
1101 | | energy /= len; |
1102 | | energy = SHL32(energy, shift); |
1103 | | |
1104 | | return energy; |
1105 | | } |
1106 | | #else |
1107 | | static opus_val32 compute_frame_energy(const opus_val16 *pcm, int frame_size, int channels, int arch) |
1108 | 0 | { |
1109 | 0 | int len = frame_size*channels; |
1110 | 0 | return celt_inner_prod(pcm, pcm, len, arch)/len; |
1111 | 0 | } |
1112 | | #endif |
1113 | | |
1114 | | /* Decides if DTX should be turned on (=1) or off (=0) */ |
1115 | | static int decide_dtx_mode(opus_int activity, /* indicates if this frame contains speech/music */ |
1116 | | int *nb_no_activity_ms_Q1, /* number of consecutive milliseconds with no activity, in Q1 */ |
1117 | | int frame_size_ms_Q1 /* number of milliseconds in this update, in Q1 */ |
1118 | | ) |
1119 | | |
1120 | 0 | { |
1121 | 0 | if (!activity) |
1122 | 0 | { |
1123 | | /* The number of consecutive DTX frames should be within the allowed bounds. |
1124 | | Note that the allowed bound is defined in the SILK headers and assumes 20 ms |
1125 | | frames. As this function can be called with any frame length, a conversion to |
1126 | | milliseconds is done before the comparisons. */ |
1127 | 0 | (*nb_no_activity_ms_Q1) += frame_size_ms_Q1; |
1128 | 0 | if (*nb_no_activity_ms_Q1 > NB_SPEECH_FRAMES_BEFORE_DTX*20*2) |
1129 | 0 | { |
1130 | 0 | if (*nb_no_activity_ms_Q1 <= (NB_SPEECH_FRAMES_BEFORE_DTX + MAX_CONSECUTIVE_DTX)*20*2) |
1131 | | /* Valid frame for DTX! */ |
1132 | 0 | return 1; |
1133 | 0 | else |
1134 | 0 | (*nb_no_activity_ms_Q1) = NB_SPEECH_FRAMES_BEFORE_DTX*20*2; |
1135 | 0 | } |
1136 | 0 | } else |
1137 | 0 | (*nb_no_activity_ms_Q1) = 0; |
1138 | | |
1139 | 0 | return 0; |
1140 | 0 | } |
1141 | | |
1142 | | static int compute_redundancy_bytes(opus_int32 max_data_bytes, opus_int32 bitrate_bps, int frame_rate, int channels) |
1143 | 0 | { |
1144 | 0 | int redundancy_bytes_cap; |
1145 | 0 | int redundancy_bytes; |
1146 | 0 | opus_int32 redundancy_rate; |
1147 | 0 | int base_bits; |
1148 | 0 | opus_int32 available_bits; |
1149 | 0 | base_bits = (40*channels+20); |
1150 | | |
1151 | | /* Equivalent rate for 5 ms frames. */ |
1152 | 0 | redundancy_rate = bitrate_bps + base_bits*(200 - frame_rate); |
1153 | | /* For VBR, further increase the bitrate if we can afford it. It's pretty short |
1154 | | and we'll avoid artefacts. */ |
1155 | 0 | redundancy_rate = 3*redundancy_rate/2; |
1156 | 0 | redundancy_bytes = redundancy_rate/1600; |
1157 | | |
1158 | | /* Compute the max rate we can use given CBR or VBR with cap. */ |
1159 | 0 | available_bits = max_data_bytes*8 - 2*base_bits; |
1160 | 0 | redundancy_bytes_cap = (available_bits*240/(240+48000/frame_rate) + base_bits)/8; |
1161 | 0 | redundancy_bytes = IMIN(redundancy_bytes, redundancy_bytes_cap); |
1162 | | /* It we can't get enough bits for redundancy to be worth it, rely on the decoder PLC. */ |
1163 | 0 | if (redundancy_bytes > 4 + 8*channels) |
1164 | 0 | redundancy_bytes = IMIN(257, redundancy_bytes); |
1165 | 0 | else |
1166 | 0 | redundancy_bytes = 0; |
1167 | 0 | return redundancy_bytes; |
1168 | 0 | } |
1169 | | |
1170 | | static opus_int32 opus_encode_frame_native(OpusEncoder *st, const opus_res *pcm, int frame_size, |
1171 | | unsigned char *data, opus_int32 max_data_bytes, |
1172 | | int float_api, int first_frame, |
1173 | | #ifdef ENABLE_DRED |
1174 | | opus_int32 dred_bitrate_bps, |
1175 | | #endif |
1176 | | #ifndef DISABLE_FLOAT_API |
1177 | | AnalysisInfo *analysis_info, |
1178 | | #endif |
1179 | | int is_silence, int redundancy, int celt_to_silk, int prefill, |
1180 | | opus_int32 equiv_rate, int to_celt); |
1181 | | |
1182 | | opus_int32 opus_encode_native(OpusEncoder *st, const opus_res *pcm, int frame_size, |
1183 | | unsigned char *data, opus_int32 out_data_bytes, int lsb_depth, |
1184 | | const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2, |
1185 | | int analysis_channels, downmix_func downmix, int float_api) |
1186 | 0 | { |
1187 | 0 | void *silk_enc=NULL; |
1188 | 0 | CELTEncoder *celt_enc=NULL; |
1189 | 0 | int i; |
1190 | 0 | int ret=0; |
1191 | 0 | int prefill=0; |
1192 | 0 | int redundancy = 0; |
1193 | 0 | int celt_to_silk = 0; |
1194 | 0 | int to_celt = 0; |
1195 | 0 | int voice_est; /* Probability of voice in Q7 */ |
1196 | 0 | opus_int32 equiv_rate; |
1197 | 0 | int frame_rate; |
1198 | 0 | opus_int32 max_rate; /* Max bitrate we're allowed to use */ |
1199 | 0 | int curr_bandwidth; |
1200 | 0 | opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */ |
1201 | 0 | opus_int32 cbr_bytes=-1; |
1202 | 0 | opus_val16 stereo_width; |
1203 | 0 | const CELTMode *celt_mode=NULL; |
1204 | 0 | int packet_size_cap = 1276; |
1205 | 0 | #ifndef DISABLE_FLOAT_API |
1206 | 0 | AnalysisInfo analysis_info; |
1207 | 0 | int analysis_read_pos_bak=-1; |
1208 | 0 | int analysis_read_subframe_bak=-1; |
1209 | 0 | #endif |
1210 | 0 | int is_silence = 0; |
1211 | | #ifdef ENABLE_DRED |
1212 | | opus_int32 dred_bitrate_bps; |
1213 | | #endif |
1214 | 0 | ALLOC_STACK; |
1215 | |
|
1216 | | #ifdef ENABLE_QEXT |
1217 | | if (st->enable_qext) packet_size_cap = QEXT_PACKET_SIZE_CAP; |
1218 | | #endif |
1219 | | |
1220 | | /* Just avoid insane packet sizes here, but the real bounds are applied later on. */ |
1221 | 0 | max_data_bytes = IMIN(packet_size_cap*6, out_data_bytes); |
1222 | |
|
1223 | 0 | st->rangeFinal = 0; |
1224 | 0 | if (frame_size <= 0 || max_data_bytes <= 0) |
1225 | 0 | { |
1226 | 0 | RESTORE_STACK; |
1227 | 0 | return OPUS_BAD_ARG; |
1228 | 0 | } |
1229 | | |
1230 | | /* Cannot encode 100 ms in 1 byte */ |
1231 | 0 | if (max_data_bytes==1 && st->Fs==(frame_size*10)) |
1232 | 0 | { |
1233 | 0 | RESTORE_STACK; |
1234 | 0 | return OPUS_BUFFER_TOO_SMALL; |
1235 | 0 | } |
1236 | | |
1237 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_CELT) |
1238 | 0 | silk_enc = (char*)st+st->silk_enc_offset; |
1239 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
1240 | 0 | celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); |
1241 | |
|
1242 | 0 | lsb_depth = IMIN(lsb_depth, st->lsb_depth); |
1243 | |
|
1244 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
1245 | 0 | celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode)); |
1246 | 0 | is_silence = is_digital_silence(pcm, frame_size, st->channels, lsb_depth); |
1247 | 0 | #ifndef DISABLE_FLOAT_API |
1248 | 0 | analysis_info.valid = 0; |
1249 | | #ifdef FIXED_POINT |
1250 | | if (st->silk_mode.complexity >= 10 && st->Fs>=16000 && st->Fs<=48000 && st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
1251 | | #else |
1252 | 0 | if (st->silk_mode.complexity >= 7 && st->Fs>=16000 && st->Fs<=48000 && st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
1253 | 0 | #endif |
1254 | 0 | { |
1255 | 0 | analysis_read_pos_bak = st->analysis.read_pos; |
1256 | 0 | analysis_read_subframe_bak = st->analysis.read_subframe; |
1257 | 0 | run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame_size, |
1258 | 0 | c1, c2, analysis_channels, st->Fs, |
1259 | 0 | lsb_depth, downmix, &analysis_info); |
1260 | |
|
1261 | 0 | } else if (st->analysis.initialized) { |
1262 | 0 | tonality_analysis_reset(&st->analysis); |
1263 | 0 | } |
1264 | | #else |
1265 | | (void)analysis_pcm; |
1266 | | (void)analysis_size; |
1267 | | (void)c1; |
1268 | | (void)c2; |
1269 | | (void)analysis_channels; |
1270 | | (void)downmix; |
1271 | | #endif |
1272 | | |
1273 | | /* Reset voice_ratio if this frame is not silent or if analysis is disabled. |
1274 | | * Otherwise, preserve voice_ratio from the last non-silent frame */ |
1275 | 0 | if (!is_silence) |
1276 | 0 | st->voice_ratio = -1; |
1277 | 0 | #ifndef DISABLE_FLOAT_API |
1278 | 0 | st->detected_bandwidth = 0; |
1279 | 0 | if (analysis_info.valid) |
1280 | 0 | { |
1281 | 0 | int analysis_bandwidth; |
1282 | 0 | if (st->signal_type == OPUS_AUTO) |
1283 | 0 | { |
1284 | 0 | float prob; |
1285 | 0 | if (st->prev_mode == 0) |
1286 | 0 | prob = analysis_info.music_prob; |
1287 | 0 | else if (st->prev_mode == MODE_CELT_ONLY) |
1288 | 0 | prob = analysis_info.music_prob_max; |
1289 | 0 | else |
1290 | 0 | prob = analysis_info.music_prob_min; |
1291 | 0 | st->voice_ratio = (int)floor(.5+100*(1-prob)); |
1292 | 0 | } |
1293 | |
|
1294 | 0 | analysis_bandwidth = analysis_info.bandwidth; |
1295 | 0 | if (analysis_bandwidth<=12) |
1296 | 0 | st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
1297 | 0 | else if (analysis_bandwidth<=14) |
1298 | 0 | st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; |
1299 | 0 | else if (analysis_bandwidth<=16) |
1300 | 0 | st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
1301 | 0 | else if (analysis_bandwidth<=18) |
1302 | 0 | st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; |
1303 | 0 | else |
1304 | 0 | st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND; |
1305 | 0 | } |
1306 | | #else |
1307 | | st->voice_ratio = -1; |
1308 | | #endif |
1309 | | |
1310 | | /* Track the peak signal energy */ |
1311 | 0 | #ifndef DISABLE_FLOAT_API |
1312 | 0 | if (!analysis_info.valid || analysis_info.activity_probability > DTX_ACTIVITY_THRESHOLD) |
1313 | 0 | #endif |
1314 | 0 | { |
1315 | 0 | if (!is_silence) |
1316 | 0 | { |
1317 | 0 | st->peak_signal_energy = MAX32(MULT16_32_Q15(QCONST16(0.999f, 15), st->peak_signal_energy), |
1318 | 0 | compute_frame_energy(pcm, frame_size, st->channels, st->arch)); |
1319 | 0 | } |
1320 | 0 | } |
1321 | 0 | if (st->channels==2 && st->force_channels!=1) |
1322 | 0 | stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_mem); |
1323 | 0 | else |
1324 | 0 | stereo_width = 0; |
1325 | 0 | st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes); |
1326 | |
|
1327 | 0 | frame_rate = st->Fs/frame_size; |
1328 | 0 | if (!st->use_vbr) |
1329 | 0 | { |
1330 | 0 | cbr_bytes = IMIN((bitrate_to_bits(st->bitrate_bps, st->Fs, frame_size)+4)/8, max_data_bytes); |
1331 | 0 | st->bitrate_bps = bits_to_bitrate(cbr_bytes*8, st->Fs, frame_size); |
1332 | | /* Make sure we provide at least one byte to avoid failing. */ |
1333 | 0 | max_data_bytes = IMAX(1, cbr_bytes); |
1334 | 0 | } |
1335 | | #ifdef ENABLE_DRED |
1336 | | /* Allocate some of the bits to DRED if needed. */ |
1337 | | dred_bitrate_bps = compute_dred_bitrate(st, st->bitrate_bps, frame_size); |
1338 | | st->bitrate_bps -= dred_bitrate_bps; |
1339 | | #endif |
1340 | 0 | if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8 |
1341 | 0 | || (frame_rate<50 && (max_data_bytes*(opus_int32)frame_rate<300 || st->bitrate_bps < 2400))) |
1342 | 0 | { |
1343 | | /*If the space is too low to do something useful, emit 'PLC' frames.*/ |
1344 | 0 | int tocmode = st->mode; |
1345 | 0 | int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth; |
1346 | 0 | int packet_code = 0; |
1347 | 0 | int num_multiframes = 0; |
1348 | |
|
1349 | 0 | if (tocmode==0) |
1350 | 0 | tocmode = MODE_SILK_ONLY; |
1351 | 0 | if (frame_rate>100) |
1352 | 0 | tocmode = MODE_CELT_ONLY; |
1353 | | /* 40 ms -> 2 x 20 ms if in CELT_ONLY or HYBRID mode */ |
1354 | 0 | if (frame_rate==25 && tocmode!=MODE_SILK_ONLY) |
1355 | 0 | { |
1356 | 0 | frame_rate = 50; |
1357 | 0 | packet_code = 1; |
1358 | 0 | } |
1359 | | |
1360 | | /* >= 60 ms frames */ |
1361 | 0 | if (frame_rate<=16) |
1362 | 0 | { |
1363 | | /* 1 x 60 ms, 2 x 40 ms, 2 x 60 ms */ |
1364 | 0 | if (out_data_bytes==1 || (tocmode==MODE_SILK_ONLY && frame_rate!=10)) |
1365 | 0 | { |
1366 | 0 | tocmode = MODE_SILK_ONLY; |
1367 | |
|
1368 | 0 | packet_code = frame_rate <= 12; |
1369 | 0 | frame_rate = frame_rate == 12 ? 25 : 16; |
1370 | 0 | } |
1371 | 0 | else |
1372 | 0 | { |
1373 | 0 | num_multiframes = 50/frame_rate; |
1374 | 0 | frame_rate = 50; |
1375 | 0 | packet_code = 3; |
1376 | 0 | } |
1377 | 0 | } |
1378 | |
|
1379 | 0 | if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND) |
1380 | 0 | bw=OPUS_BANDWIDTH_WIDEBAND; |
1381 | 0 | else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND) |
1382 | 0 | bw=OPUS_BANDWIDTH_NARROWBAND; |
1383 | 0 | else if (tocmode==MODE_HYBRID&&bw<=OPUS_BANDWIDTH_SUPERWIDEBAND) |
1384 | 0 | bw=OPUS_BANDWIDTH_SUPERWIDEBAND; |
1385 | |
|
1386 | 0 | data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels); |
1387 | 0 | data[0] |= packet_code; |
1388 | |
|
1389 | 0 | ret = packet_code <= 1 ? 1 : 2; |
1390 | |
|
1391 | 0 | max_data_bytes = IMAX(max_data_bytes, ret); |
1392 | |
|
1393 | 0 | if (packet_code==3) |
1394 | 0 | data[1] = num_multiframes; |
1395 | |
|
1396 | 0 | if (!st->use_vbr) |
1397 | 0 | { |
1398 | 0 | ret = opus_packet_pad(data, ret, max_data_bytes); |
1399 | 0 | if (ret == OPUS_OK) |
1400 | 0 | ret = max_data_bytes; |
1401 | 0 | else |
1402 | 0 | ret = OPUS_INTERNAL_ERROR; |
1403 | 0 | } |
1404 | 0 | RESTORE_STACK; |
1405 | 0 | return ret; |
1406 | 0 | } |
1407 | 0 | max_rate = bits_to_bitrate(max_data_bytes*8, st->Fs, frame_size); |
1408 | | |
1409 | | /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */ |
1410 | 0 | equiv_rate = compute_equiv_rate(st->bitrate_bps, st->channels, st->Fs/frame_size, |
1411 | 0 | st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage); |
1412 | |
|
1413 | 0 | if (st->signal_type == OPUS_SIGNAL_VOICE) |
1414 | 0 | voice_est = 127; |
1415 | 0 | else if (st->signal_type == OPUS_SIGNAL_MUSIC) |
1416 | 0 | voice_est = 0; |
1417 | 0 | else if (st->voice_ratio >= 0) |
1418 | 0 | { |
1419 | 0 | voice_est = st->voice_ratio*327>>8; |
1420 | | /* For AUDIO, never be more than 90% confident of having speech */ |
1421 | 0 | if (st->application == OPUS_APPLICATION_AUDIO) |
1422 | 0 | voice_est = IMIN(voice_est, 115); |
1423 | 0 | } else if (st->application == OPUS_APPLICATION_VOIP) |
1424 | 0 | voice_est = 115; |
1425 | 0 | else |
1426 | 0 | voice_est = 48; |
1427 | |
|
1428 | 0 | if (st->force_channels!=OPUS_AUTO && st->channels == 2) |
1429 | 0 | { |
1430 | 0 | st->stream_channels = st->force_channels; |
1431 | 0 | } else { |
1432 | | #ifdef FUZZING |
1433 | | (void)stereo_music_threshold; |
1434 | | (void)stereo_voice_threshold; |
1435 | | /* Random mono/stereo decision */ |
1436 | | if (st->channels == 2 && (rand()&0x1F)==0) |
1437 | | st->stream_channels = 3-st->stream_channels; |
1438 | | #else |
1439 | | /* Rate-dependent mono-stereo decision */ |
1440 | 0 | if (st->channels == 2) |
1441 | 0 | { |
1442 | 0 | opus_int32 stereo_threshold; |
1443 | 0 | stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14); |
1444 | 0 | if (st->stream_channels == 2) |
1445 | 0 | stereo_threshold -= 1000; |
1446 | 0 | else |
1447 | 0 | stereo_threshold += 1000; |
1448 | 0 | st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1; |
1449 | 0 | } else { |
1450 | 0 | st->stream_channels = st->channels; |
1451 | 0 | } |
1452 | 0 | #endif |
1453 | 0 | } |
1454 | | /* Update equivalent rate for channels decision. */ |
1455 | 0 | equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size, |
1456 | 0 | st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage); |
1457 | | |
1458 | | /* Allow SILK DTX if DTX is enabled but the generalized DTX cannot be used, |
1459 | | e.g. because of the complexity setting or sample rate. */ |
1460 | 0 | #ifndef DISABLE_FLOAT_API |
1461 | 0 | st->silk_mode.useDTX = st->use_dtx && !(analysis_info.valid || is_silence); |
1462 | | #else |
1463 | | st->silk_mode.useDTX = st->use_dtx && !is_silence; |
1464 | | #endif |
1465 | | |
1466 | | /* Mode selection depending on application and signal type */ |
1467 | 0 | if (st->application == OPUS_APPLICATION_RESTRICTED_SILK) |
1468 | 0 | { |
1469 | 0 | st->mode = MODE_SILK_ONLY; |
1470 | 0 | } else if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY || st->application == OPUS_APPLICATION_RESTRICTED_CELT) |
1471 | 0 | { |
1472 | 0 | st->mode = MODE_CELT_ONLY; |
1473 | 0 | } else if (st->user_forced_mode == OPUS_AUTO) |
1474 | 0 | { |
1475 | | #ifdef FUZZING |
1476 | | (void)stereo_width; |
1477 | | (void)mode_thresholds; |
1478 | | /* Random mode switching */ |
1479 | | if ((rand()&0xF)==0) |
1480 | | { |
1481 | | if ((rand()&0x1)==0) |
1482 | | st->mode = MODE_CELT_ONLY; |
1483 | | else |
1484 | | st->mode = MODE_SILK_ONLY; |
1485 | | } else { |
1486 | | if (st->prev_mode==MODE_CELT_ONLY) |
1487 | | st->mode = MODE_CELT_ONLY; |
1488 | | else |
1489 | | st->mode = MODE_SILK_ONLY; |
1490 | | } |
1491 | | #else |
1492 | 0 | opus_int32 mode_voice, mode_music; |
1493 | 0 | opus_int32 threshold; |
1494 | | |
1495 | | /* Interpolate based on stereo width */ |
1496 | 0 | mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[0][0]) |
1497 | 0 | + MULT16_32_Q15(stereo_width,mode_thresholds[1][0])); |
1498 | 0 | mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[1][1]) |
1499 | 0 | + MULT16_32_Q15(stereo_width,mode_thresholds[1][1])); |
1500 | | /* Interpolate based on speech/music probability */ |
1501 | 0 | threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14); |
1502 | | /* Bias towards SILK for VoIP because of some useful features */ |
1503 | 0 | if (st->application == OPUS_APPLICATION_VOIP) |
1504 | 0 | threshold += 8000; |
1505 | | |
1506 | | /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/ |
1507 | | /* Hysteresis */ |
1508 | 0 | if (st->prev_mode == MODE_CELT_ONLY) |
1509 | 0 | threshold -= 4000; |
1510 | 0 | else if (st->prev_mode>0) |
1511 | 0 | threshold += 4000; |
1512 | |
|
1513 | 0 | st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY; |
1514 | | |
1515 | | /* When FEC is enabled and there's enough packet loss, use SILK. |
1516 | | Unless the FEC is set to 2, in which case we don't switch to SILK if we're confident we have music. */ |
1517 | 0 | if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (128-voice_est)>>4 && (st->fec_config != 2 || voice_est > 25)) |
1518 | 0 | st->mode = MODE_SILK_ONLY; |
1519 | | /* When encoding voice and DTX is enabled but the generalized DTX cannot be used, |
1520 | | use SILK in order to make use of its DTX. */ |
1521 | 0 | if (st->silk_mode.useDTX && voice_est > 100) |
1522 | 0 | st->mode = MODE_SILK_ONLY; |
1523 | 0 | #endif |
1524 | | |
1525 | | /* If max_data_bytes represents less than 6 kb/s, switch to CELT-only mode */ |
1526 | 0 | if (max_data_bytes < bitrate_to_bits(frame_rate > 50 ? 9000 : 6000, st->Fs, frame_size)/8) |
1527 | 0 | st->mode = MODE_CELT_ONLY; |
1528 | 0 | } else { |
1529 | 0 | st->mode = st->user_forced_mode; |
1530 | 0 | } |
1531 | | |
1532 | | /* Override the chosen mode to make sure we meet the requested frame size */ |
1533 | 0 | if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100) |
1534 | 0 | { |
1535 | 0 | celt_assert(st->application != OPUS_APPLICATION_RESTRICTED_SILK); |
1536 | 0 | st->mode = MODE_CELT_ONLY; |
1537 | 0 | } |
1538 | 0 | if (st->lfe && st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
1539 | 0 | st->mode = MODE_CELT_ONLY; |
1540 | |
|
1541 | 0 | if (st->prev_mode > 0 && |
1542 | 0 | ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) || |
1543 | 0 | (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY))) |
1544 | 0 | { |
1545 | 0 | redundancy = 1; |
1546 | 0 | celt_to_silk = (st->mode != MODE_CELT_ONLY); |
1547 | 0 | if (!celt_to_silk) |
1548 | 0 | { |
1549 | | /* Switch to SILK/hybrid if frame size is 10 ms or more*/ |
1550 | 0 | if (frame_size >= st->Fs/100) |
1551 | 0 | { |
1552 | 0 | st->mode = st->prev_mode; |
1553 | 0 | to_celt = 1; |
1554 | 0 | } else { |
1555 | 0 | redundancy=0; |
1556 | 0 | } |
1557 | 0 | } |
1558 | 0 | } |
1559 | | |
1560 | | /* When encoding multiframes, we can ask for a switch to CELT only in the last frame. This switch |
1561 | | * is processed above as the requested mode shouldn't interrupt stereo->mono transition. */ |
1562 | 0 | if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0 |
1563 | 0 | && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY) |
1564 | 0 | { |
1565 | | /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */ |
1566 | 0 | st->silk_mode.toMono = 1; |
1567 | 0 | st->stream_channels = 2; |
1568 | 0 | } else { |
1569 | 0 | st->silk_mode.toMono = 0; |
1570 | 0 | } |
1571 | | |
1572 | | /* Update equivalent rate with mode decision. */ |
1573 | 0 | equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size, |
1574 | 0 | st->use_vbr, st->mode, st->silk_mode.complexity, st->silk_mode.packetLossPercentage); |
1575 | |
|
1576 | 0 | if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) |
1577 | 0 | { |
1578 | 0 | silk_EncControlStruct dummy; |
1579 | 0 | silk_InitEncoder( silk_enc, st->channels, st->arch, &dummy); |
1580 | 0 | prefill=1; |
1581 | 0 | } |
1582 | | |
1583 | | /* Automatic (rate-dependent) bandwidth selection */ |
1584 | 0 | if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch) |
1585 | 0 | { |
1586 | 0 | const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds; |
1587 | 0 | opus_int32 bandwidth_thresholds[8]; |
1588 | 0 | int bandwidth = OPUS_BANDWIDTH_FULLBAND; |
1589 | |
|
1590 | 0 | if (st->channels==2 && st->force_channels!=1) |
1591 | 0 | { |
1592 | 0 | voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds; |
1593 | 0 | music_bandwidth_thresholds = stereo_music_bandwidth_thresholds; |
1594 | 0 | } else { |
1595 | 0 | voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds; |
1596 | 0 | music_bandwidth_thresholds = mono_music_bandwidth_thresholds; |
1597 | 0 | } |
1598 | | /* Interpolate bandwidth thresholds depending on voice estimation */ |
1599 | 0 | for (i=0;i<8;i++) |
1600 | 0 | { |
1601 | 0 | bandwidth_thresholds[i] = music_bandwidth_thresholds[i] |
1602 | 0 | + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14); |
1603 | 0 | } |
1604 | 0 | do { |
1605 | 0 | int threshold, hysteresis; |
1606 | 0 | threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)]; |
1607 | 0 | hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1]; |
1608 | 0 | if (!st->first) |
1609 | 0 | { |
1610 | 0 | if (st->auto_bandwidth >= bandwidth) |
1611 | 0 | threshold -= hysteresis; |
1612 | 0 | else |
1613 | 0 | threshold += hysteresis; |
1614 | 0 | } |
1615 | 0 | if (equiv_rate >= threshold) |
1616 | 0 | break; |
1617 | 0 | } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND); |
1618 | | /* We don't use mediumband anymore, except when explicitly requested or during |
1619 | | mode transitions. */ |
1620 | 0 | if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) |
1621 | 0 | bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
1622 | 0 | st->bandwidth = st->auto_bandwidth = bandwidth; |
1623 | | /* Prevents any transition to SWB/FB until the SILK layer has fully |
1624 | | switched to WB mode and turned the variable LP filter off */ |
1625 | 0 | if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND) |
1626 | 0 | st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
1627 | 0 | } |
1628 | |
|
1629 | 0 | if (st->bandwidth>st->max_bandwidth) |
1630 | 0 | st->bandwidth = st->max_bandwidth; |
1631 | |
|
1632 | 0 | if (st->user_bandwidth != OPUS_AUTO) |
1633 | 0 | st->bandwidth = st->user_bandwidth; |
1634 | | |
1635 | | /* This prevents us from using hybrid at unsafe CBR/max rates */ |
1636 | 0 | if (st->mode != MODE_CELT_ONLY && max_rate < 15000) |
1637 | 0 | { |
1638 | 0 | st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND); |
1639 | 0 | } |
1640 | | |
1641 | | /* Prevents Opus from wasting bits on frequencies that are above |
1642 | | the Nyquist rate of the input signal */ |
1643 | 0 | if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND) |
1644 | 0 | st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; |
1645 | 0 | if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND) |
1646 | 0 | st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
1647 | 0 | if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND) |
1648 | 0 | st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; |
1649 | 0 | if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND) |
1650 | 0 | st->bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
1651 | 0 | #ifndef DISABLE_FLOAT_API |
1652 | | /* Use detected bandwidth to reduce the encoded bandwidth. */ |
1653 | 0 | if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO) |
1654 | 0 | { |
1655 | 0 | int min_detected_bandwidth; |
1656 | | /* Makes bandwidth detection more conservative just in case the detector |
1657 | | gets it wrong when we could have coded a high bandwidth transparently. |
1658 | | When operating in SILK/hybrid mode, we don't go below wideband to avoid |
1659 | | more complicated switches that require redundancy. */ |
1660 | 0 | if (equiv_rate <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY) |
1661 | 0 | min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
1662 | 0 | else if (equiv_rate <= 24000*st->stream_channels && st->mode == MODE_CELT_ONLY) |
1663 | 0 | min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; |
1664 | 0 | else if (equiv_rate <= 30000*st->stream_channels) |
1665 | 0 | min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
1666 | 0 | else if (equiv_rate <= 44000*st->stream_channels) |
1667 | 0 | min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; |
1668 | 0 | else |
1669 | 0 | min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND; |
1670 | |
|
1671 | 0 | st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwidth); |
1672 | 0 | st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth); |
1673 | 0 | } |
1674 | 0 | #endif |
1675 | 0 | st->silk_mode.LBRR_coded = decide_fec(st->silk_mode.useInBandFEC, st->silk_mode.packetLossPercentage, |
1676 | 0 | st->silk_mode.LBRR_coded, st->mode, &st->bandwidth, equiv_rate); |
1677 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
1678 | 0 | celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth)); |
1679 | | |
1680 | | /* CELT mode doesn't support mediumband, use wideband instead */ |
1681 | 0 | if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) |
1682 | 0 | st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
1683 | 0 | if (st->lfe) |
1684 | 0 | st->bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
1685 | |
|
1686 | 0 | curr_bandwidth = st->bandwidth; |
1687 | |
|
1688 | 0 | if (st->application == OPUS_APPLICATION_RESTRICTED_SILK && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND) |
1689 | 0 | st->bandwidth = curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
1690 | | /* Chooses the appropriate mode for speech |
1691 | | *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */ |
1692 | 0 | if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND) |
1693 | 0 | st->mode = MODE_HYBRID; |
1694 | 0 | if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND) |
1695 | 0 | st->mode = MODE_SILK_ONLY; |
1696 | | |
1697 | | /* Can't support higher than >60 ms frames, and >20 ms when in Hybrid or CELT-only modes */ |
1698 | 0 | if ((frame_size > st->Fs/50 && (st->mode != MODE_SILK_ONLY)) || frame_size > 3*st->Fs/50) |
1699 | 0 | { |
1700 | 0 | int enc_frame_size; |
1701 | 0 | int nb_frames; |
1702 | 0 | VARDECL(unsigned char, tmp_data); |
1703 | 0 | VARDECL(OpusRepacketizer, rp); |
1704 | 0 | int max_header_bytes; |
1705 | 0 | opus_int32 repacketize_len; |
1706 | 0 | opus_int32 max_len_sum; |
1707 | 0 | opus_int32 tot_size=0; |
1708 | 0 | unsigned char *curr_data; |
1709 | 0 | int tmp_len; |
1710 | 0 | int dtx_count = 0; |
1711 | 0 | int bak_to_mono; |
1712 | |
|
1713 | 0 | if (st->mode == MODE_SILK_ONLY) |
1714 | 0 | { |
1715 | 0 | if (frame_size == 2*st->Fs/25) /* 80 ms -> 2x 40 ms */ |
1716 | 0 | enc_frame_size = st->Fs/25; |
1717 | 0 | else if (frame_size == 3*st->Fs/25) /* 120 ms -> 2x 60 ms */ |
1718 | 0 | enc_frame_size = 3*st->Fs/50; |
1719 | 0 | else /* 100 ms -> 5x 20 ms */ |
1720 | 0 | enc_frame_size = st->Fs/50; |
1721 | 0 | } |
1722 | 0 | else |
1723 | 0 | enc_frame_size = st->Fs/50; |
1724 | |
|
1725 | 0 | nb_frames = frame_size/enc_frame_size; |
1726 | |
|
1727 | 0 | #ifndef DISABLE_FLOAT_API |
1728 | 0 | if (analysis_read_pos_bak!= -1) |
1729 | 0 | { |
1730 | | /* Reset analysis position to the beginning of the first frame so we |
1731 | | can use it one frame at a time. */ |
1732 | 0 | st->analysis.read_pos = analysis_read_pos_bak; |
1733 | 0 | st->analysis.read_subframe = analysis_read_subframe_bak; |
1734 | 0 | } |
1735 | 0 | #endif |
1736 | | |
1737 | | /* Worst cases: |
1738 | | * 2 frames: Code 2 with different compressed sizes |
1739 | | * >2 frames: Code 3 VBR */ |
1740 | 0 | max_header_bytes = nb_frames == 2 ? 3 : (2+(nb_frames-1)*2); |
1741 | | #ifdef ENABLE_QEXT |
1742 | | /* Cover the use of the separators that are the only thing that can get us over |
1743 | | once we consider that we need to subtract the extension overhead in each |
1744 | | of the individual frames. Also consider that a separator can get our padding |
1745 | | from 254 to 255, which costs an extra length byte (at most once). */ |
1746 | | if (st->enable_qext) max_header_bytes += (nb_frames-1) + 1; |
1747 | | #endif |
1748 | |
|
1749 | 0 | if (st->use_vbr || st->user_bitrate_bps==OPUS_BITRATE_MAX) |
1750 | 0 | repacketize_len = out_data_bytes; |
1751 | 0 | else { |
1752 | 0 | celt_assert(cbr_bytes>=0); |
1753 | 0 | repacketize_len = IMIN(cbr_bytes, out_data_bytes); |
1754 | 0 | } |
1755 | 0 | max_len_sum = nb_frames + repacketize_len - max_header_bytes; |
1756 | |
|
1757 | 0 | ALLOC(tmp_data, max_len_sum, unsigned char); |
1758 | 0 | curr_data = tmp_data; |
1759 | 0 | ALLOC(rp, 1, OpusRepacketizer); |
1760 | 0 | opus_repacketizer_init(rp); |
1761 | | |
1762 | |
|
1763 | 0 | bak_to_mono = st->silk_mode.toMono; |
1764 | 0 | if (bak_to_mono) |
1765 | 0 | st->force_channels = 1; |
1766 | 0 | else |
1767 | 0 | st->prev_channels = st->stream_channels; |
1768 | |
|
1769 | 0 | for (i=0;i<nb_frames;i++) |
1770 | 0 | { |
1771 | 0 | int first_frame; |
1772 | 0 | int frame_to_celt; |
1773 | 0 | int frame_redundancy; |
1774 | 0 | opus_int32 curr_max; |
1775 | | /* Attempt DRED encoding until we have a non-DTX frame. In case of DTX refresh, |
1776 | | that allows for DRED not to be in the first frame. */ |
1777 | 0 | first_frame = (i == 0) || (i == dtx_count); |
1778 | 0 | st->silk_mode.toMono = 0; |
1779 | 0 | st->nonfinal_frame = i<(nb_frames-1); |
1780 | | |
1781 | | /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */ |
1782 | 0 | frame_to_celt = to_celt && i==nb_frames-1; |
1783 | 0 | frame_redundancy = redundancy && (frame_to_celt || (!to_celt && i==0)); |
1784 | |
|
1785 | 0 | curr_max = IMIN(bitrate_to_bits(st->bitrate_bps, st->Fs, enc_frame_size)/8, max_len_sum/nb_frames); |
1786 | | #ifdef ENABLE_DRED |
1787 | | curr_max = IMIN(curr_max, (max_len_sum-bitrate_to_bits(dred_bitrate_bps, st->Fs, frame_size)/8)/nb_frames); |
1788 | | if (first_frame) curr_max += bitrate_to_bits(dred_bitrate_bps, st->Fs, frame_size)/8; |
1789 | | #endif |
1790 | | #ifdef ENABLE_QEXT |
1791 | | /* Leave room for signaling the extension size once we repacketize. */ |
1792 | | if (st->enable_qext) |
1793 | | curr_max -= curr_max/254; |
1794 | | #endif |
1795 | 0 | curr_max = IMIN(max_len_sum-tot_size, curr_max); |
1796 | 0 | #ifndef DISABLE_FLOAT_API |
1797 | 0 | if (analysis_read_pos_bak != -1) { |
1798 | | /* Get analysis for current frame. */ |
1799 | 0 | tonality_get_info(&st->analysis, &analysis_info, enc_frame_size); |
1800 | 0 | } |
1801 | 0 | #endif |
1802 | 0 | is_silence = is_digital_silence(pcm+i*(st->channels*enc_frame_size), enc_frame_size, st->channels, lsb_depth); |
1803 | |
|
1804 | 0 | tmp_len = opus_encode_frame_native(st, pcm+i*(st->channels*enc_frame_size), enc_frame_size, curr_data, curr_max, float_api, first_frame, |
1805 | | #ifdef ENABLE_DRED |
1806 | | dred_bitrate_bps, |
1807 | | #endif |
1808 | 0 | #ifndef DISABLE_FLOAT_API |
1809 | 0 | &analysis_info, |
1810 | 0 | #endif |
1811 | 0 | is_silence, frame_redundancy, celt_to_silk, prefill, |
1812 | 0 | equiv_rate, frame_to_celt |
1813 | 0 | ); |
1814 | 0 | if (tmp_len<0) |
1815 | 0 | { |
1816 | 0 | RESTORE_STACK; |
1817 | 0 | return OPUS_INTERNAL_ERROR; |
1818 | 0 | } else if (tmp_len==1) { |
1819 | 0 | dtx_count++; |
1820 | 0 | } |
1821 | 0 | ret = opus_repacketizer_cat(rp, curr_data, tmp_len); |
1822 | |
|
1823 | 0 | if (ret<0) |
1824 | 0 | { |
1825 | 0 | RESTORE_STACK; |
1826 | 0 | return OPUS_INTERNAL_ERROR; |
1827 | 0 | } |
1828 | 0 | tot_size += tmp_len; |
1829 | 0 | curr_data += tmp_len; |
1830 | 0 | } |
1831 | 0 | ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketize_len, 0, !st->use_vbr && (dtx_count != nb_frames), NULL, 0); |
1832 | 0 | if (ret<0) |
1833 | 0 | { |
1834 | 0 | ret = OPUS_INTERNAL_ERROR; |
1835 | 0 | } |
1836 | 0 | st->silk_mode.toMono = bak_to_mono; |
1837 | 0 | RESTORE_STACK; |
1838 | 0 | return ret; |
1839 | 0 | } else { |
1840 | 0 | ret = opus_encode_frame_native(st, pcm, frame_size, data, max_data_bytes, float_api, 1, |
1841 | | #ifdef ENABLE_DRED |
1842 | | dred_bitrate_bps, |
1843 | | #endif |
1844 | 0 | #ifndef DISABLE_FLOAT_API |
1845 | 0 | &analysis_info, |
1846 | 0 | #endif |
1847 | 0 | is_silence, redundancy, celt_to_silk, prefill, |
1848 | 0 | equiv_rate, to_celt |
1849 | 0 | ); |
1850 | 0 | RESTORE_STACK; |
1851 | 0 | return ret; |
1852 | 0 | } |
1853 | 0 | } |
1854 | | |
1855 | | static opus_int32 opus_encode_frame_native(OpusEncoder *st, const opus_res *pcm, int frame_size, |
1856 | | unsigned char *data, opus_int32 orig_max_data_bytes, |
1857 | | int float_api, int first_frame, |
1858 | | #ifdef ENABLE_DRED |
1859 | | opus_int32 dred_bitrate_bps, |
1860 | | #endif |
1861 | | #ifndef DISABLE_FLOAT_API |
1862 | | AnalysisInfo *analysis_info, |
1863 | | #endif |
1864 | | int is_silence, int redundancy, int celt_to_silk, int prefill, |
1865 | | opus_int32 equiv_rate, int to_celt) |
1866 | 0 | { |
1867 | 0 | void *silk_enc=NULL; |
1868 | 0 | CELTEncoder *celt_enc=NULL; |
1869 | 0 | const CELTMode *celt_mode=NULL; |
1870 | 0 | int i; |
1871 | 0 | int ret=0; |
1872 | 0 | int max_data_bytes; |
1873 | 0 | opus_int32 nBytes; |
1874 | 0 | ec_enc enc; |
1875 | 0 | int bits_target; |
1876 | 0 | int start_band = 0; |
1877 | 0 | int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */ |
1878 | 0 | int nb_compr_bytes; |
1879 | 0 | opus_uint32 redundant_rng = 0; |
1880 | 0 | int cutoff_Hz; |
1881 | 0 | int hp_freq_smth1; |
1882 | 0 | opus_val16 HB_gain; |
1883 | 0 | int apply_padding; |
1884 | 0 | int frame_rate; |
1885 | 0 | int curr_bandwidth; |
1886 | 0 | int delay_compensation; |
1887 | 0 | int total_buffer; |
1888 | 0 | opus_int activity = VAD_NO_DECISION; |
1889 | 0 | VARDECL(opus_res, pcm_buf); |
1890 | 0 | VARDECL(opus_res, tmp_prefill); |
1891 | 0 | SAVE_STACK; |
1892 | |
|
1893 | 0 | max_data_bytes = IMIN(orig_max_data_bytes, 1276); |
1894 | 0 | st->rangeFinal = 0; |
1895 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_CELT) |
1896 | 0 | silk_enc = (char*)st+st->silk_enc_offset; |
1897 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
1898 | 0 | { |
1899 | 0 | celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); |
1900 | 0 | celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode)); |
1901 | 0 | } |
1902 | 0 | curr_bandwidth = st->bandwidth; |
1903 | 0 | if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY || st->application == OPUS_APPLICATION_RESTRICTED_CELT || st->application == OPUS_APPLICATION_RESTRICTED_SILK) |
1904 | 0 | delay_compensation = 0; |
1905 | 0 | else |
1906 | 0 | delay_compensation = st->delay_compensation; |
1907 | 0 | total_buffer = delay_compensation; |
1908 | |
|
1909 | 0 | frame_rate = st->Fs/frame_size; |
1910 | |
|
1911 | 0 | if (is_silence) |
1912 | 0 | { |
1913 | 0 | activity = !is_silence; |
1914 | 0 | } |
1915 | 0 | #ifndef DISABLE_FLOAT_API |
1916 | 0 | else if (analysis_info->valid) { |
1917 | 0 | activity = analysis_info->activity_probability >= DTX_ACTIVITY_THRESHOLD; |
1918 | 0 | if (!activity) |
1919 | 0 | { |
1920 | | /* Mark as active if this noise frame is sufficiently loud */ |
1921 | 0 | opus_val32 noise_energy = compute_frame_energy(pcm, frame_size, st->channels, st->arch); |
1922 | 0 | activity = st->peak_signal_energy < (PSEUDO_SNR_THRESHOLD * noise_energy); |
1923 | 0 | } |
1924 | 0 | } |
1925 | 0 | #endif |
1926 | 0 | else if (st->mode == MODE_CELT_ONLY) { |
1927 | 0 | opus_val32 noise_energy = compute_frame_energy(pcm, frame_size, st->channels, st->arch); |
1928 | | /* Boosting peak energy a bit because we didn't just average the active frames. */ |
1929 | 0 | activity = st->peak_signal_energy < (QCONST16(PSEUDO_SNR_THRESHOLD, 0) * (opus_val64)HALF32(noise_energy)); |
1930 | 0 | } |
1931 | | |
1932 | | /* For the first frame at a new SILK bandwidth */ |
1933 | 0 | if (st->silk_bw_switch) |
1934 | 0 | { |
1935 | 0 | redundancy = 1; |
1936 | 0 | celt_to_silk = 1; |
1937 | 0 | st->silk_bw_switch = 0; |
1938 | | /* Do a prefill without resetting the sampling rate control. */ |
1939 | 0 | prefill=2; |
1940 | 0 | } |
1941 | | |
1942 | | /* If we decided to go with CELT, make sure redundancy is off, no matter what |
1943 | | we decided earlier. */ |
1944 | 0 | if (st->mode == MODE_CELT_ONLY) |
1945 | 0 | redundancy = 0; |
1946 | |
|
1947 | 0 | if (redundancy) |
1948 | 0 | { |
1949 | 0 | redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels); |
1950 | 0 | if (redundancy_bytes == 0) |
1951 | 0 | redundancy = 0; |
1952 | 0 | } |
1953 | 0 | if (st->application == OPUS_APPLICATION_RESTRICTED_SILK) |
1954 | 0 | { |
1955 | 0 | redundancy = 0; |
1956 | 0 | redundancy_bytes = 0; |
1957 | 0 | } |
1958 | | |
1959 | | /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */ |
1960 | 0 | bits_target = IMIN(8*(max_data_bytes-redundancy_bytes), bitrate_to_bits(st->bitrate_bps, st->Fs, frame_size)) - 8; |
1961 | |
|
1962 | 0 | data += 1; |
1963 | |
|
1964 | 0 | ec_enc_init(&enc, data, orig_max_data_bytes-1); |
1965 | |
|
1966 | 0 | ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_res); |
1967 | 0 | OPUS_COPY(pcm_buf, &st->delay_buffer[(st->encoder_buffer-total_buffer)*st->channels], total_buffer*st->channels); |
1968 | |
|
1969 | 0 | if (st->mode == MODE_CELT_ONLY) |
1970 | 0 | hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); |
1971 | 0 | else |
1972 | 0 | hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15; |
1973 | |
|
1974 | 0 | st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15, |
1975 | 0 | hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) ); |
1976 | | |
1977 | | /* convert from log scale to Hertz */ |
1978 | 0 | cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) ); |
1979 | |
|
1980 | 0 | if (st->application == OPUS_APPLICATION_VOIP) |
1981 | 0 | { |
1982 | 0 | hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs, st->arch); |
1983 | |
|
1984 | | #ifdef ENABLE_OSCE_TRAINING_DATA |
1985 | | /* write out high pass filtered clean signal*/ |
1986 | | static FILE *fout =NULL; |
1987 | | if (fout == NULL) |
1988 | | { |
1989 | | fout = fopen("clean_hp.s16", "wb"); |
1990 | | } |
1991 | | |
1992 | | { |
1993 | | int idx; |
1994 | | opus_int16 tmp; |
1995 | | for (idx = 0; idx < frame_size; idx++) |
1996 | | { |
1997 | | tmp = (opus_int16) (32768 * pcm_buf[total_buffer + idx] + 0.5f); |
1998 | | fwrite(&tmp, sizeof(tmp), 1, fout); |
1999 | | } |
2000 | | } |
2001 | | #endif |
2002 | 0 | } else { |
2003 | | #ifdef ENABLE_QEXT |
2004 | | /* FIXME: Avoid glitching when we switch qext on/off dynamically. */ |
2005 | | if (st->enable_qext) OPUS_COPY(&pcm_buf[total_buffer*st->channels], pcm, frame_size*st->channels); |
2006 | | else |
2007 | | #endif |
2008 | 0 | dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs); |
2009 | 0 | } |
2010 | 0 | #ifndef FIXED_POINT |
2011 | 0 | if (float_api) |
2012 | 0 | { |
2013 | 0 | opus_val32 sum; |
2014 | 0 | sum = celt_inner_prod(&pcm_buf[total_buffer*st->channels], &pcm_buf[total_buffer*st->channels], frame_size*st->channels, st->arch); |
2015 | | /* This should filter out both NaNs and ridiculous signals that could |
2016 | | cause NaNs further down. */ |
2017 | 0 | if (!(sum < 1e9f) || celt_isnan(sum)) |
2018 | 0 | { |
2019 | 0 | OPUS_CLEAR(&pcm_buf[total_buffer*st->channels], frame_size*st->channels); |
2020 | 0 | st->hp_mem[0] = st->hp_mem[1] = st->hp_mem[2] = st->hp_mem[3] = 0; |
2021 | 0 | } |
2022 | 0 | } |
2023 | | #else |
2024 | | (void)float_api; |
2025 | | #endif |
2026 | |
|
2027 | | #ifdef ENABLE_DRED |
2028 | | /* Compute the DRED features. Needs to be before SILK because of DTX. */ |
2029 | | if ( st->dred_duration > 0 && st->dred_encoder.loaded ) { |
2030 | | int frame_size_400Hz; |
2031 | | /* DRED Encoder */ |
2032 | | dred_compute_latents( &st->dred_encoder, &pcm_buf[total_buffer*st->channels], frame_size, total_buffer, st->arch ); |
2033 | | frame_size_400Hz = frame_size*400/st->Fs; |
2034 | | OPUS_MOVE(&st->activity_mem[frame_size_400Hz], st->activity_mem, 4*DRED_MAX_FRAMES-frame_size_400Hz); |
2035 | | for (i=0;i<frame_size_400Hz;i++) |
2036 | | st->activity_mem[i] = activity; |
2037 | | } else { |
2038 | | st->dred_encoder.latents_buffer_fill = 0; |
2039 | | OPUS_CLEAR(st->activity_mem, 4*DRED_MAX_FRAMES); |
2040 | | } |
2041 | | #endif |
2042 | | |
2043 | | /* SILK processing */ |
2044 | 0 | HB_gain = Q15ONE; |
2045 | 0 | if (st->mode != MODE_CELT_ONLY) |
2046 | 0 | { |
2047 | 0 | opus_int32 total_bitRate, celt_rate; |
2048 | 0 | const opus_res *pcm_silk; |
2049 | | |
2050 | | /* Distribute bits between SILK and CELT */ |
2051 | 0 | total_bitRate = bits_to_bitrate(bits_target, st->Fs, frame_size); |
2052 | 0 | if( st->mode == MODE_HYBRID ) { |
2053 | | /* Base rate for SILK */ |
2054 | 0 | st->silk_mode.bitRate = compute_silk_rate_for_hybrid(total_bitRate, |
2055 | 0 | curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded, |
2056 | 0 | st->stream_channels); |
2057 | 0 | if (!st->energy_masking) |
2058 | 0 | { |
2059 | | /* Increasingly attenuate high band when it gets allocated fewer bits */ |
2060 | 0 | celt_rate = total_bitRate - st->silk_mode.bitRate; |
2061 | 0 | HB_gain = Q15ONE - SHR32(celt_exp2(-celt_rate * QCONST16(1.f/1024, 10)), 1); |
2062 | 0 | } |
2063 | 0 | } else { |
2064 | | /* SILK gets all bits */ |
2065 | 0 | st->silk_mode.bitRate = total_bitRate; |
2066 | 0 | } |
2067 | | |
2068 | | /* Surround masking for SILK */ |
2069 | 0 | if (st->energy_masking && st->use_vbr && !st->lfe) |
2070 | 0 | { |
2071 | 0 | opus_val32 mask_sum=0; |
2072 | 0 | celt_glog masking_depth; |
2073 | 0 | opus_int32 rate_offset; |
2074 | 0 | int c; |
2075 | 0 | int end = 17; |
2076 | 0 | opus_int16 srate = 16000; |
2077 | 0 | if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND) |
2078 | 0 | { |
2079 | 0 | end = 13; |
2080 | 0 | srate = 8000; |
2081 | 0 | } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) |
2082 | 0 | { |
2083 | 0 | end = 15; |
2084 | 0 | srate = 12000; |
2085 | 0 | } |
2086 | 0 | for (c=0;c<st->channels;c++) |
2087 | 0 | { |
2088 | 0 | for(i=0;i<end;i++) |
2089 | 0 | { |
2090 | 0 | celt_glog mask; |
2091 | 0 | mask = MAXG(MING(st->energy_masking[21*c+i], |
2092 | 0 | GCONST(.5f)), -GCONST(2.0f)); |
2093 | 0 | if (mask > 0) |
2094 | 0 | mask = HALF32(mask); |
2095 | 0 | mask_sum += mask; |
2096 | 0 | } |
2097 | 0 | } |
2098 | | /* Conservative rate reduction, we cut the masking in half */ |
2099 | 0 | masking_depth = mask_sum / end*st->channels; |
2100 | 0 | masking_depth += GCONST(.2f); |
2101 | 0 | rate_offset = (opus_int32)PSHR32(MULT16_16(srate, SHR32(masking_depth, DB_SHIFT-10)), 10); |
2102 | 0 | rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3); |
2103 | | /* Split the rate change between the SILK and CELT part for hybrid. */ |
2104 | 0 | if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPUS_BANDWIDTH_FULLBAND) |
2105 | 0 | st->silk_mode.bitRate += 3*rate_offset/5; |
2106 | 0 | else |
2107 | 0 | st->silk_mode.bitRate += rate_offset; |
2108 | 0 | } |
2109 | |
|
2110 | 0 | st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs; |
2111 | 0 | st->silk_mode.nChannelsAPI = st->channels; |
2112 | 0 | st->silk_mode.nChannelsInternal = st->stream_channels; |
2113 | 0 | if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { |
2114 | 0 | st->silk_mode.desiredInternalSampleRate = 8000; |
2115 | 0 | } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { |
2116 | 0 | st->silk_mode.desiredInternalSampleRate = 12000; |
2117 | 0 | } else { |
2118 | 0 | celt_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND ); |
2119 | 0 | st->silk_mode.desiredInternalSampleRate = 16000; |
2120 | 0 | } |
2121 | 0 | if( st->mode == MODE_HYBRID ) { |
2122 | | /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */ |
2123 | 0 | st->silk_mode.minInternalSampleRate = 16000; |
2124 | 0 | } else { |
2125 | 0 | st->silk_mode.minInternalSampleRate = 8000; |
2126 | 0 | } |
2127 | |
|
2128 | 0 | st->silk_mode.maxInternalSampleRate = 16000; |
2129 | 0 | if (st->mode == MODE_SILK_ONLY) |
2130 | 0 | { |
2131 | 0 | opus_int32 effective_max_rate = bits_to_bitrate(max_data_bytes*8, st->Fs, frame_size); |
2132 | 0 | if (frame_rate > 50) |
2133 | 0 | effective_max_rate = effective_max_rate*2/3; |
2134 | 0 | if (effective_max_rate < 8000) |
2135 | 0 | { |
2136 | 0 | st->silk_mode.maxInternalSampleRate = 12000; |
2137 | 0 | st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate); |
2138 | 0 | } |
2139 | 0 | if (effective_max_rate < 7000) |
2140 | 0 | { |
2141 | 0 | st->silk_mode.maxInternalSampleRate = 8000; |
2142 | 0 | st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate); |
2143 | 0 | } |
2144 | | #ifdef ENABLE_QEXT |
2145 | | /* At 96 kHz, we don't have the input resampler to do 8 or 12 kHz. */ |
2146 | | if (st->Fs==96000) st->silk_mode.maxInternalSampleRate = st->silk_mode.desiredInternalSampleRate = 16000; |
2147 | | #endif |
2148 | 0 | } |
2149 | |
|
2150 | 0 | st->silk_mode.useCBR = !st->use_vbr; |
2151 | | |
2152 | | /* Call SILK encoder for the low band */ |
2153 | | |
2154 | | /* Max bits for SILK, counting ToC, redundancy bytes, and optionally redundancy. */ |
2155 | 0 | st->silk_mode.maxBits = (max_data_bytes-1)*8; |
2156 | 0 | if (redundancy && redundancy_bytes >= 2) |
2157 | 0 | { |
2158 | | /* Counting 1 bit for redundancy position and 20 bits for flag+size (only for hybrid). */ |
2159 | 0 | st->silk_mode.maxBits -= redundancy_bytes*8 + 1; |
2160 | 0 | if (st->mode == MODE_HYBRID) |
2161 | 0 | st->silk_mode.maxBits -= 20; |
2162 | 0 | } |
2163 | 0 | if (st->silk_mode.useCBR) |
2164 | 0 | { |
2165 | | /* When we're in CBR mode, but we have non-SILK data to encode, switch SILK to VBR with cap to |
2166 | | save on complexity. Any variations will be absorbed by CELT and/or DRED and we can still |
2167 | | produce a constant bitrate without wasting bits. */ |
2168 | | #ifdef ENABLE_DRED |
2169 | | if (st->mode == MODE_HYBRID || dred_bitrate_bps > 0) |
2170 | | #else |
2171 | 0 | if (st->mode == MODE_HYBRID) |
2172 | 0 | #endif |
2173 | 0 | { |
2174 | | /* Allow SILK to steal up to 25% of the remaining bits */ |
2175 | 0 | opus_int16 other_bits = IMAX(0, st->silk_mode.maxBits - st->silk_mode.bitRate * frame_size / st->Fs); |
2176 | 0 | st->silk_mode.maxBits = IMAX(0, st->silk_mode.maxBits - other_bits*3/4); |
2177 | 0 | st->silk_mode.useCBR = 0; |
2178 | 0 | } |
2179 | 0 | } else { |
2180 | | /* Constrained VBR. */ |
2181 | 0 | if (st->mode == MODE_HYBRID) |
2182 | 0 | { |
2183 | | /* Compute SILK bitrate corresponding to the max total bits available */ |
2184 | 0 | opus_int32 maxBitRate = compute_silk_rate_for_hybrid(st->silk_mode.maxBits*st->Fs / frame_size, |
2185 | 0 | curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded, |
2186 | 0 | st->stream_channels); |
2187 | 0 | st->silk_mode.maxBits = bitrate_to_bits(maxBitRate, st->Fs, frame_size); |
2188 | 0 | } |
2189 | 0 | } |
2190 | |
|
2191 | 0 | if (prefill && st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
2192 | 0 | { |
2193 | 0 | opus_int32 zero=0; |
2194 | 0 | int prefill_offset; |
2195 | | /* Use a smooth onset for the SILK prefill to avoid the encoder trying to encode |
2196 | | a discontinuity. The exact location is what we need to avoid leaving any "gap" |
2197 | | in the audio when mixing with the redundant CELT frame. Here we can afford to |
2198 | | overwrite st->delay_buffer because the only thing that uses it before it gets |
2199 | | rewritten is tmp_prefill[] and even then only the part after the ramp really |
2200 | | gets used (rather than sent to the encoder and discarded) */ |
2201 | 0 | prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensation-st->Fs/400); |
2202 | 0 | gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_offset, |
2203 | 0 | 0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_mode->window, st->Fs); |
2204 | 0 | OPUS_CLEAR(st->delay_buffer, prefill_offset); |
2205 | 0 | pcm_silk = st->delay_buffer; |
2206 | 0 | silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, prefill, activity ); |
2207 | | /* Prevent a second switch in the real encode call. */ |
2208 | 0 | st->silk_mode.opusCanSwitch = 0; |
2209 | 0 | } |
2210 | |
|
2211 | 0 | pcm_silk = pcm_buf+total_buffer*st->channels; |
2212 | 0 | ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0, activity ); |
2213 | 0 | if( ret ) { |
2214 | | /*fprintf (stderr, "SILK encode error: %d\n", ret);*/ |
2215 | | /* Handle error */ |
2216 | 0 | RESTORE_STACK; |
2217 | 0 | return OPUS_INTERNAL_ERROR; |
2218 | 0 | } |
2219 | | |
2220 | | /* Extract SILK internal bandwidth for signaling in first byte */ |
2221 | 0 | if( st->mode == MODE_SILK_ONLY ) { |
2222 | 0 | if( st->silk_mode.internalSampleRate == 8000 ) { |
2223 | 0 | curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
2224 | 0 | } else if( st->silk_mode.internalSampleRate == 12000 ) { |
2225 | 0 | curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; |
2226 | 0 | } else if( st->silk_mode.internalSampleRate == 16000 ) { |
2227 | 0 | curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
2228 | 0 | } |
2229 | 0 | } else { |
2230 | 0 | celt_assert( st->silk_mode.internalSampleRate == 16000 ); |
2231 | 0 | } |
2232 | | |
2233 | 0 | st->silk_mode.opusCanSwitch = st->silk_mode.switchReady && !st->nonfinal_frame; |
2234 | |
|
2235 | 0 | if (activity == VAD_NO_DECISION) { |
2236 | 0 | activity = (st->silk_mode.signalType != TYPE_NO_VOICE_ACTIVITY); |
2237 | | #ifdef ENABLE_DRED |
2238 | | for (i=0;i<frame_size*400/st->Fs;i++) |
2239 | | st->activity_mem[i] = activity; |
2240 | | #endif |
2241 | 0 | } |
2242 | 0 | if (nBytes==0) |
2243 | 0 | { |
2244 | 0 | st->rangeFinal = 0; |
2245 | 0 | data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels); |
2246 | 0 | RESTORE_STACK; |
2247 | 0 | return 1; |
2248 | 0 | } |
2249 | | |
2250 | | /* FIXME: How do we allocate the redundancy for CBR? */ |
2251 | 0 | if (st->silk_mode.opusCanSwitch) |
2252 | 0 | { |
2253 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
2254 | 0 | { |
2255 | 0 | redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels); |
2256 | 0 | redundancy = (redundancy_bytes != 0); |
2257 | 0 | } |
2258 | 0 | celt_to_silk = 0; |
2259 | 0 | st->silk_bw_switch = 1; |
2260 | 0 | } |
2261 | 0 | } |
2262 | | |
2263 | | /* CELT processing */ |
2264 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
2265 | 0 | { |
2266 | 0 | int endband=21; |
2267 | |
|
2268 | 0 | switch(curr_bandwidth) |
2269 | 0 | { |
2270 | 0 | case OPUS_BANDWIDTH_NARROWBAND: |
2271 | 0 | endband = 13; |
2272 | 0 | break; |
2273 | 0 | case OPUS_BANDWIDTH_MEDIUMBAND: |
2274 | 0 | case OPUS_BANDWIDTH_WIDEBAND: |
2275 | 0 | endband = 17; |
2276 | 0 | break; |
2277 | 0 | case OPUS_BANDWIDTH_SUPERWIDEBAND: |
2278 | 0 | endband = 19; |
2279 | 0 | break; |
2280 | 0 | case OPUS_BANDWIDTH_FULLBAND: |
2281 | 0 | endband = 21; |
2282 | 0 | break; |
2283 | 0 | } |
2284 | 0 | celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband)); |
2285 | 0 | celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels)); |
2286 | 0 | celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX)); |
2287 | 0 | } |
2288 | 0 | if (st->mode != MODE_SILK_ONLY) |
2289 | 0 | { |
2290 | 0 | opus_val32 celt_pred=2; |
2291 | | /* We may still decide to disable prediction later */ |
2292 | 0 | if (st->silk_mode.reducedDependency) |
2293 | 0 | celt_pred = 0; |
2294 | 0 | celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred)); |
2295 | 0 | } |
2296 | |
|
2297 | 0 | ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_res); |
2298 | 0 | if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0 |
2299 | 0 | && st->application != OPUS_APPLICATION_RESTRICTED_CELT) |
2300 | 0 | { |
2301 | 0 | OPUS_COPY(tmp_prefill, &st->delay_buffer[(st->encoder_buffer-total_buffer-st->Fs/400)*st->channels], st->channels*st->Fs/400); |
2302 | 0 | } |
2303 | |
|
2304 | 0 | if (st->channels*(st->encoder_buffer-(frame_size+total_buffer)) > 0) |
2305 | 0 | { |
2306 | 0 | OPUS_MOVE(st->delay_buffer, &st->delay_buffer[st->channels*frame_size], st->channels*(st->encoder_buffer-frame_size-total_buffer)); |
2307 | 0 | OPUS_COPY(&st->delay_buffer[st->channels*(st->encoder_buffer-frame_size-total_buffer)], |
2308 | 0 | &pcm_buf[0], |
2309 | 0 | (frame_size+total_buffer)*st->channels); |
2310 | 0 | } else { |
2311 | 0 | OPUS_COPY(st->delay_buffer, &pcm_buf[(frame_size+total_buffer-st->encoder_buffer)*st->channels], st->encoder_buffer*st->channels); |
2312 | 0 | } |
2313 | | /* gain_fade() and stereo_fade() need to be after the buffer copying |
2314 | | because we don't want any of this to affect the SILK part */ |
2315 | 0 | if( ( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) && celt_mode != NULL ) { |
2316 | 0 | gain_fade(pcm_buf, pcm_buf, |
2317 | 0 | st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs); |
2318 | 0 | } |
2319 | 0 | st->prev_HB_gain = HB_gain; |
2320 | 0 | if (st->mode != MODE_HYBRID || st->stream_channels==1) |
2321 | 0 | { |
2322 | 0 | if (equiv_rate > 32000) |
2323 | 0 | st->silk_mode.stereoWidth_Q14 = 16384; |
2324 | 0 | else if (equiv_rate < 16000) |
2325 | 0 | st->silk_mode.stereoWidth_Q14 = 0; |
2326 | 0 | else |
2327 | 0 | st->silk_mode.stereoWidth_Q14 = 16384 - 2048*(opus_int32)(32000-equiv_rate)/(equiv_rate-14000); |
2328 | 0 | } |
2329 | 0 | if( !st->energy_masking && st->channels == 2 ) { |
2330 | | /* Apply stereo width reduction (at low bitrates) */ |
2331 | 0 | if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) { |
2332 | 0 | opus_val16 g1, g2; |
2333 | 0 | g1 = st->hybrid_stereo_width_Q14; |
2334 | 0 | g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14); |
2335 | | #ifdef FIXED_POINT |
2336 | | g1 = g1==16384 ? Q15ONE : SHL16(g1,1); |
2337 | | g2 = g2==16384 ? Q15ONE : SHL16(g2,1); |
2338 | | #else |
2339 | 0 | g1 *= (1.f/16384); |
2340 | 0 | g2 *= (1.f/16384); |
2341 | 0 | #endif |
2342 | 0 | if ( celt_mode != NULL ) |
2343 | 0 | { |
2344 | 0 | stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap, |
2345 | 0 | frame_size, st->channels, celt_mode->window, st->Fs); |
2346 | 0 | } |
2347 | 0 | st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14; |
2348 | 0 | } |
2349 | 0 | } |
2350 | |
|
2351 | 0 | if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) <= 8*(max_data_bytes-1)) |
2352 | 0 | { |
2353 | | /* For SILK mode, the redundancy is inferred from the length */ |
2354 | 0 | if (st->mode == MODE_HYBRID) |
2355 | 0 | ec_enc_bit_logp(&enc, redundancy, 12); |
2356 | 0 | if (redundancy) |
2357 | 0 | { |
2358 | 0 | int max_redundancy; |
2359 | 0 | ec_enc_bit_logp(&enc, celt_to_silk, 1); |
2360 | 0 | if (st->mode == MODE_HYBRID) |
2361 | 0 | { |
2362 | | /* Reserve the 8 bits needed for the redundancy length, |
2363 | | and at least a few bits for CELT if possible */ |
2364 | 0 | max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+8+3+7)>>3); |
2365 | 0 | } |
2366 | 0 | else |
2367 | 0 | max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3); |
2368 | | /* Target the same bit-rate for redundancy as for the rest, |
2369 | | up to a max of 257 bytes */ |
2370 | 0 | redundancy_bytes = IMIN(max_redundancy, redundancy_bytes); |
2371 | 0 | redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes)); |
2372 | 0 | if (st->mode == MODE_HYBRID) |
2373 | 0 | ec_enc_uint(&enc, redundancy_bytes-2, 256); |
2374 | 0 | } |
2375 | 0 | } else { |
2376 | 0 | redundancy = 0; |
2377 | 0 | } |
2378 | |
|
2379 | 0 | if (!redundancy) |
2380 | 0 | { |
2381 | 0 | st->silk_bw_switch = 0; |
2382 | 0 | redundancy_bytes = 0; |
2383 | 0 | } |
2384 | 0 | if (st->mode != MODE_CELT_ONLY)start_band=17; |
2385 | |
|
2386 | 0 | if (st->mode == MODE_SILK_ONLY) |
2387 | 0 | { |
2388 | 0 | ret = (ec_tell(&enc)+7)>>3; |
2389 | 0 | ec_enc_done(&enc); |
2390 | 0 | nb_compr_bytes = ret; |
2391 | 0 | } else { |
2392 | 0 | nb_compr_bytes = (max_data_bytes-1)-redundancy_bytes; |
2393 | | #ifdef ENABLE_QEXT |
2394 | | if (st->mode == MODE_CELT_ONLY && st->enable_qext) { |
2395 | | celt_assert(redundancy_bytes==0); |
2396 | | nb_compr_bytes = orig_max_data_bytes-1; |
2397 | | } |
2398 | | #endif |
2399 | | #ifdef ENABLE_DRED |
2400 | | if (st->dred_duration > 0) |
2401 | | { |
2402 | | int max_celt_bytes; |
2403 | | opus_int32 dred_bytes = bitrate_to_bits(dred_bitrate_bps, st->Fs, frame_size)/8; |
2404 | | /* Allow CELT to steal up to 25% of the remaining bits. */ |
2405 | | max_celt_bytes = nb_compr_bytes - dred_bytes*3/4; |
2406 | | /* But try to give CELT at least 5 bytes to prevent a mismatch with |
2407 | | the redundancy signaling. */ |
2408 | | max_celt_bytes = IMAX((ec_tell(&enc)+7)/8 + 5, max_celt_bytes); |
2409 | | /* Subject to the original max. */ |
2410 | | nb_compr_bytes = IMIN(nb_compr_bytes, max_celt_bytes); |
2411 | | } |
2412 | | #endif |
2413 | 0 | ec_enc_shrink(&enc, nb_compr_bytes); |
2414 | 0 | } |
2415 | |
|
2416 | 0 | #ifndef DISABLE_FLOAT_API |
2417 | 0 | if (redundancy || st->mode != MODE_SILK_ONLY) |
2418 | 0 | celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(analysis_info)); |
2419 | 0 | #endif |
2420 | 0 | if (st->mode == MODE_HYBRID) { |
2421 | 0 | SILKInfo info; |
2422 | 0 | info.signalType = st->silk_mode.signalType; |
2423 | 0 | info.offset = st->silk_mode.offset; |
2424 | 0 | celt_encoder_ctl(celt_enc, CELT_SET_SILK_INFO(&info)); |
2425 | 0 | } |
2426 | | |
2427 | | /* 5 ms redundant frame for CELT->SILK */ |
2428 | 0 | if (redundancy && celt_to_silk) |
2429 | 0 | { |
2430 | 0 | int err; |
2431 | 0 | celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)); |
2432 | 0 | celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0)); |
2433 | 0 | celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX)); |
2434 | 0 | err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL); |
2435 | 0 | if (err < 0) |
2436 | 0 | { |
2437 | 0 | RESTORE_STACK; |
2438 | 0 | return OPUS_INTERNAL_ERROR; |
2439 | 0 | } |
2440 | 0 | celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng)); |
2441 | 0 | celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); |
2442 | 0 | } |
2443 | | |
2444 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
2445 | 0 | celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band)); |
2446 | |
|
2447 | 0 | data[-1] = 0; |
2448 | 0 | if (st->mode != MODE_SILK_ONLY) |
2449 | 0 | { |
2450 | 0 | celt_encoder_ctl(celt_enc, OPUS_SET_VBR(st->use_vbr)); |
2451 | 0 | if (st->mode == MODE_HYBRID) |
2452 | 0 | { |
2453 | 0 | if( st->use_vbr ) { |
2454 | 0 | celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps-st->silk_mode.bitRate)); |
2455 | 0 | celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0)); |
2456 | 0 | } |
2457 | 0 | } else { |
2458 | 0 | if (st->use_vbr) |
2459 | 0 | { |
2460 | 0 | celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1)); |
2461 | 0 | celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint)); |
2462 | 0 | celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps)); |
2463 | 0 | } |
2464 | 0 | } |
2465 | | #ifdef ENABLE_DRED |
2466 | | /* When Using DRED CBR, we can actually make the CELT part VBR and have DRED pick up the slack. */ |
2467 | | if (!st->use_vbr && st->dred_duration > 0) |
2468 | | { |
2469 | | opus_int32 celt_bitrate = st->bitrate_bps; |
2470 | | celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1)); |
2471 | | celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0)); |
2472 | | if (st->mode == MODE_HYBRID) { |
2473 | | celt_bitrate -= st->silk_mode.bitRate; |
2474 | | } |
2475 | | celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(celt_bitrate)); |
2476 | | } |
2477 | | #endif |
2478 | 0 | if (st->mode != st->prev_mode && st->prev_mode > 0 && st->application != OPUS_APPLICATION_RESTRICTED_CELT) |
2479 | 0 | { |
2480 | 0 | unsigned char dummy[2]; |
2481 | 0 | celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); |
2482 | | |
2483 | | /* Prefilling */ |
2484 | 0 | celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL); |
2485 | 0 | celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0)); |
2486 | 0 | } |
2487 | | /* If false, we already busted the budget and we'll end up with a "PLC frame" */ |
2488 | 0 | if (ec_tell(&enc) <= 8*nb_compr_bytes) |
2489 | 0 | { |
2490 | | #ifdef ENABLE_QEXT |
2491 | | if (st->mode == MODE_CELT_ONLY) celt_encoder_ctl(celt_enc, OPUS_SET_QEXT(st->enable_qext)); |
2492 | | #endif |
2493 | 0 | ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc); |
2494 | | #ifdef ENABLE_QEXT |
2495 | | celt_encoder_ctl(celt_enc, OPUS_SET_QEXT(0)); |
2496 | | #endif |
2497 | 0 | if (ret < 0) |
2498 | 0 | { |
2499 | 0 | RESTORE_STACK; |
2500 | 0 | return OPUS_INTERNAL_ERROR; |
2501 | 0 | } |
2502 | | /* Put CELT->SILK redundancy data in the right place. */ |
2503 | 0 | if (redundancy && celt_to_silk && st->mode==MODE_HYBRID && nb_compr_bytes != ret) |
2504 | 0 | { |
2505 | 0 | OPUS_MOVE(data+ret, data+nb_compr_bytes, redundancy_bytes); |
2506 | 0 | nb_compr_bytes = ret+redundancy_bytes; |
2507 | 0 | } |
2508 | 0 | } |
2509 | 0 | celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&st->rangeFinal)); |
2510 | 0 | } else { |
2511 | 0 | st->rangeFinal = enc.rng; |
2512 | 0 | } |
2513 | | |
2514 | | /* 5 ms redundant frame for SILK->CELT */ |
2515 | 0 | if (redundancy && !celt_to_silk) |
2516 | 0 | { |
2517 | 0 | int err; |
2518 | 0 | unsigned char dummy[2]; |
2519 | 0 | int N2, N4; |
2520 | 0 | N2 = st->Fs/200; |
2521 | 0 | N4 = st->Fs/400; |
2522 | |
|
2523 | 0 | celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); |
2524 | 0 | celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)); |
2525 | 0 | celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0)); |
2526 | 0 | celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0)); |
2527 | 0 | celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX)); |
2528 | |
|
2529 | 0 | if (st->mode == MODE_HYBRID) |
2530 | 0 | { |
2531 | | /* Shrink packet to what the encoder actually used. */ |
2532 | 0 | nb_compr_bytes = ret; |
2533 | 0 | ec_enc_shrink(&enc, nb_compr_bytes); |
2534 | 0 | } |
2535 | | /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */ |
2536 | 0 | celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL); |
2537 | |
|
2538 | 0 | err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL); |
2539 | 0 | if (err < 0) |
2540 | 0 | { |
2541 | 0 | RESTORE_STACK; |
2542 | 0 | return OPUS_INTERNAL_ERROR; |
2543 | 0 | } |
2544 | 0 | celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng)); |
2545 | 0 | } |
2546 | | |
2547 | | |
2548 | | |
2549 | | /* Signalling the mode in the first byte */ |
2550 | 0 | data--; |
2551 | 0 | data[0] |= gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels); |
2552 | |
|
2553 | 0 | st->rangeFinal ^= redundant_rng; |
2554 | |
|
2555 | 0 | if (to_celt) |
2556 | 0 | st->prev_mode = MODE_CELT_ONLY; |
2557 | 0 | else |
2558 | 0 | st->prev_mode = st->mode; |
2559 | 0 | st->prev_channels = st->stream_channels; |
2560 | 0 | st->prev_framesize = frame_size; |
2561 | |
|
2562 | 0 | st->first = 0; |
2563 | | |
2564 | | /* DTX decision */ |
2565 | 0 | if (st->use_dtx && !st->silk_mode.useDTX) |
2566 | 0 | { |
2567 | 0 | if (decide_dtx_mode(activity, &st->nb_no_activity_ms_Q1, 2*1000*frame_size/st->Fs)) |
2568 | 0 | { |
2569 | 0 | st->rangeFinal = 0; |
2570 | 0 | data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels); |
2571 | 0 | RESTORE_STACK; |
2572 | 0 | return 1; |
2573 | 0 | } |
2574 | 0 | } else { |
2575 | 0 | st->nb_no_activity_ms_Q1 = 0; |
2576 | 0 | } |
2577 | | |
2578 | | /* In the unlikely case that the SILK encoder busted its target, tell |
2579 | | the decoder to call the PLC */ |
2580 | 0 | if (ec_tell(&enc) > (max_data_bytes-1)*8) |
2581 | 0 | { |
2582 | 0 | if (max_data_bytes < 2) |
2583 | 0 | { |
2584 | 0 | RESTORE_STACK; |
2585 | 0 | return OPUS_BUFFER_TOO_SMALL; |
2586 | 0 | } |
2587 | 0 | data[1] = 0; |
2588 | 0 | ret = 1; |
2589 | 0 | st->rangeFinal = 0; |
2590 | 0 | } else if (st->mode==MODE_SILK_ONLY&&!redundancy) |
2591 | 0 | { |
2592 | | /*When in LPC only mode it's perfectly |
2593 | | reasonable to strip off trailing zero bytes as |
2594 | | the required range decoder behavior is to |
2595 | | fill these in. This can't be done when the MDCT |
2596 | | modes are used because the decoder needs to know |
2597 | | the actual length for allocation purposes.*/ |
2598 | 0 | while(ret>2&&data[ret]==0)ret--; |
2599 | 0 | } |
2600 | | /* Count ToC and redundancy */ |
2601 | 0 | ret += 1+redundancy_bytes; |
2602 | 0 | apply_padding = !st->use_vbr; |
2603 | | #ifdef ENABLE_DRED |
2604 | | if (st->dred_duration > 0 && st->dred_encoder.loaded && first_frame) { |
2605 | | opus_extension_data extension; |
2606 | | unsigned char buf[DRED_MAX_DATA_SIZE]; |
2607 | | int dred_chunks; |
2608 | | int dred_bytes_left; |
2609 | | dred_chunks = IMIN((st->dred_duration+5)/4, DRED_NUM_REDUNDANCY_FRAMES/2); |
2610 | | if (st->use_vbr) dred_chunks = IMIN(dred_chunks, st->dred_target_chunks); |
2611 | | /* Remaining space for DRED, accounting for cost the 3 extra bytes for code 3, padding length, and extension number. */ |
2612 | | dred_bytes_left = IMIN(DRED_MAX_DATA_SIZE, orig_max_data_bytes-ret-3); |
2613 | | /* Account for the extra bytes required to signal large padding length. */ |
2614 | | dred_bytes_left -= (dred_bytes_left+1+DRED_EXPERIMENTAL_BYTES)/255; |
2615 | | /* Check whether we actually have something to encode. */ |
2616 | | if (dred_chunks >= 1 && dred_bytes_left >= DRED_MIN_BYTES+DRED_EXPERIMENTAL_BYTES) { |
2617 | | int dred_bytes; |
2618 | | #ifdef DRED_EXPERIMENTAL_VERSION |
2619 | | /* Add temporary extension type and version. |
2620 | | These bytes will be removed once extension is finalized. */ |
2621 | | buf[0] = 'D'; |
2622 | | buf[1] = DRED_EXPERIMENTAL_VERSION; |
2623 | | #endif |
2624 | | dred_bytes = dred_encode_silk_frame(&st->dred_encoder, buf+DRED_EXPERIMENTAL_BYTES, dred_chunks, dred_bytes_left-DRED_EXPERIMENTAL_BYTES, |
2625 | | st->dred_q0, st->dred_dQ, st->dred_qmax, st->activity_mem, st->arch); |
2626 | | if (dred_bytes > 0) { |
2627 | | dred_bytes += DRED_EXPERIMENTAL_BYTES; |
2628 | | celt_assert(dred_bytes <= dred_bytes_left); |
2629 | | extension.id = DRED_EXTENSION_ID; |
2630 | | extension.frame = 0; |
2631 | | extension.data = buf; |
2632 | | extension.len = dred_bytes; |
2633 | | ret = opus_packet_pad_impl(data, ret, orig_max_data_bytes, !st->use_vbr, &extension, 1); |
2634 | | if (ret < 0) |
2635 | | { |
2636 | | RESTORE_STACK; |
2637 | | return OPUS_INTERNAL_ERROR; |
2638 | | } |
2639 | | apply_padding = 0; |
2640 | | } |
2641 | | } |
2642 | | } |
2643 | | #else |
2644 | 0 | (void)first_frame; /* Avoids a warning about first_frame being unused. */ |
2645 | 0 | #endif |
2646 | 0 | if (apply_padding) |
2647 | 0 | { |
2648 | 0 | if (opus_packet_pad(data, ret, orig_max_data_bytes) != OPUS_OK) |
2649 | 0 | { |
2650 | 0 | RESTORE_STACK; |
2651 | 0 | return OPUS_INTERNAL_ERROR; |
2652 | 0 | } |
2653 | 0 | ret = orig_max_data_bytes; |
2654 | 0 | } |
2655 | 0 | RESTORE_STACK; |
2656 | 0 | return ret; |
2657 | 0 | } |
2658 | | |
2659 | | |
2660 | | |
2661 | | #if defined(FIXED_POINT) && !defined(ENABLE_RES24) |
2662 | | opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size, |
2663 | | unsigned char *data, opus_int32 max_data_bytes) |
2664 | | { |
2665 | | int frame_size; |
2666 | | frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs); |
2667 | | return opus_encode_native(st, pcm, frame_size, data, max_data_bytes, 16, |
2668 | | pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 0); |
2669 | | } |
2670 | | #else |
2671 | | opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size, |
2672 | | unsigned char *data, opus_int32 max_data_bytes) |
2673 | 0 | { |
2674 | 0 | int i, ret; |
2675 | 0 | int frame_size; |
2676 | 0 | VARDECL(opus_res, in); |
2677 | 0 | ALLOC_STACK; |
2678 | |
|
2679 | 0 | frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs); |
2680 | 0 | if (frame_size <= 0) |
2681 | 0 | { |
2682 | 0 | RESTORE_STACK; |
2683 | 0 | return OPUS_BAD_ARG; |
2684 | 0 | } |
2685 | 0 | ALLOC(in, frame_size*st->channels, opus_res); |
2686 | |
|
2687 | 0 | for (i=0;i<frame_size*st->channels;i++) |
2688 | 0 | in[i] = INT16TORES(pcm[i]); |
2689 | 0 | ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, |
2690 | 0 | pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 1); |
2691 | 0 | RESTORE_STACK; |
2692 | 0 | return ret; |
2693 | 0 | } |
2694 | | #endif |
2695 | | |
2696 | | #if defined(FIXED_POINT) && defined(ENABLE_RES24) |
2697 | | opus_int32 opus_encode24(OpusEncoder *st, const opus_int32 *pcm, int analysis_frame_size, |
2698 | | unsigned char *data, opus_int32 max_data_bytes) |
2699 | | { |
2700 | | int frame_size; |
2701 | | frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs); |
2702 | | return opus_encode_native(st, pcm, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH, |
2703 | | pcm, analysis_frame_size, 0, -2, st->channels, downmix_int24, 0); |
2704 | | } |
2705 | | #else |
2706 | | opus_int32 opus_encode24(OpusEncoder *st, const opus_int32 *pcm, int analysis_frame_size, |
2707 | | unsigned char *data, opus_int32 max_data_bytes) |
2708 | 0 | { |
2709 | 0 | int i, ret; |
2710 | 0 | int frame_size; |
2711 | 0 | VARDECL(opus_res, in); |
2712 | 0 | ALLOC_STACK; |
2713 | |
|
2714 | 0 | frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs); |
2715 | 0 | if (frame_size <= 0) |
2716 | 0 | { |
2717 | 0 | RESTORE_STACK; |
2718 | 0 | return OPUS_BAD_ARG; |
2719 | 0 | } |
2720 | 0 | ALLOC(in, frame_size*st->channels, opus_res); |
2721 | |
|
2722 | 0 | for (i=0;i<frame_size*st->channels;i++) |
2723 | 0 | in[i] = INT24TORES(pcm[i]); |
2724 | 0 | ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH, |
2725 | 0 | pcm, analysis_frame_size, 0, -2, st->channels, downmix_int24, 1); |
2726 | 0 | RESTORE_STACK; |
2727 | 0 | return ret; |
2728 | 0 | } |
2729 | | #endif |
2730 | | |
2731 | | |
2732 | | #ifndef DISABLE_FLOAT_API |
2733 | | |
2734 | | # if !defined(FIXED_POINT) |
2735 | | opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size, |
2736 | | unsigned char *data, opus_int32 out_data_bytes) |
2737 | 0 | { |
2738 | 0 | int frame_size; |
2739 | 0 | frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs); |
2740 | 0 | return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, MAX_ENCODING_DEPTH, |
2741 | 0 | pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1); |
2742 | 0 | } |
2743 | | # else |
2744 | | opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size, |
2745 | | unsigned char *data, opus_int32 max_data_bytes) |
2746 | | { |
2747 | | int i, ret; |
2748 | | int frame_size; |
2749 | | VARDECL(opus_res, in); |
2750 | | ALLOC_STACK; |
2751 | | |
2752 | | frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs); |
2753 | | if (frame_size <= 0) |
2754 | | { |
2755 | | RESTORE_STACK; |
2756 | | return OPUS_BAD_ARG; |
2757 | | } |
2758 | | ALLOC(in, frame_size*st->channels, opus_res); |
2759 | | |
2760 | | for (i=0;i<frame_size*st->channels;i++) |
2761 | | in[i] = FLOAT2RES(pcm[i]); |
2762 | | ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH, |
2763 | | pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1); |
2764 | | RESTORE_STACK; |
2765 | | return ret; |
2766 | | } |
2767 | | # endif |
2768 | | |
2769 | | #endif |
2770 | | |
2771 | | |
2772 | | int opus_encoder_ctl(OpusEncoder *st, int request, ...) |
2773 | 0 | { |
2774 | 0 | int ret; |
2775 | 0 | CELTEncoder *celt_enc=NULL; |
2776 | 0 | va_list ap; |
2777 | |
|
2778 | 0 | ret = OPUS_OK; |
2779 | 0 | va_start(ap, request); |
2780 | |
|
2781 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
2782 | 0 | celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); |
2783 | |
|
2784 | 0 | switch (request) |
2785 | 0 | { |
2786 | 0 | case OPUS_SET_APPLICATION_REQUEST: |
2787 | 0 | { |
2788 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
2789 | 0 | if (st->application == OPUS_APPLICATION_RESTRICTED_SILK || st->application == OPUS_APPLICATION_RESTRICTED_CELT) |
2790 | 0 | { |
2791 | 0 | ret = OPUS_BAD_ARG; |
2792 | 0 | break; |
2793 | 0 | } |
2794 | 0 | if ( (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_AUDIO |
2795 | 0 | && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY) |
2796 | 0 | || (!st->first && st->application != value)) |
2797 | 0 | { |
2798 | 0 | ret = OPUS_BAD_ARG; |
2799 | 0 | break; |
2800 | 0 | } |
2801 | 0 | st->application = value; |
2802 | 0 | #ifndef DISABLE_FLOAT_API |
2803 | 0 | st->analysis.application = value; |
2804 | 0 | #endif |
2805 | 0 | } |
2806 | 0 | break; |
2807 | 0 | case OPUS_GET_APPLICATION_REQUEST: |
2808 | 0 | { |
2809 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
2810 | 0 | if (!value) |
2811 | 0 | { |
2812 | 0 | goto bad_arg; |
2813 | 0 | } |
2814 | 0 | *value = st->application; |
2815 | 0 | } |
2816 | 0 | break; |
2817 | 0 | case OPUS_SET_BITRATE_REQUEST: |
2818 | 0 | { |
2819 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
2820 | 0 | if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX) |
2821 | 0 | { |
2822 | 0 | if (value <= 0) |
2823 | 0 | goto bad_arg; |
2824 | 0 | else if (value <= 500) |
2825 | 0 | value = 500; |
2826 | 0 | else if (value > (opus_int32)750000*st->channels) |
2827 | 0 | value = (opus_int32)750000*st->channels; |
2828 | 0 | } |
2829 | 0 | st->user_bitrate_bps = value; |
2830 | 0 | } |
2831 | 0 | break; |
2832 | 0 | case OPUS_GET_BITRATE_REQUEST: |
2833 | 0 | { |
2834 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
2835 | 0 | if (!value) |
2836 | 0 | { |
2837 | 0 | goto bad_arg; |
2838 | 0 | } |
2839 | 0 | *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276); |
2840 | 0 | } |
2841 | 0 | break; |
2842 | 0 | case OPUS_SET_FORCE_CHANNELS_REQUEST: |
2843 | 0 | { |
2844 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
2845 | 0 | if((value<1 || value>st->channels) && value != OPUS_AUTO) |
2846 | 0 | { |
2847 | 0 | goto bad_arg; |
2848 | 0 | } |
2849 | 0 | st->force_channels = value; |
2850 | 0 | } |
2851 | 0 | break; |
2852 | 0 | case OPUS_GET_FORCE_CHANNELS_REQUEST: |
2853 | 0 | { |
2854 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
2855 | 0 | if (!value) |
2856 | 0 | { |
2857 | 0 | goto bad_arg; |
2858 | 0 | } |
2859 | 0 | *value = st->force_channels; |
2860 | 0 | } |
2861 | 0 | break; |
2862 | 0 | case OPUS_SET_MAX_BANDWIDTH_REQUEST: |
2863 | 0 | { |
2864 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
2865 | 0 | if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) |
2866 | 0 | { |
2867 | 0 | goto bad_arg; |
2868 | 0 | } |
2869 | 0 | st->max_bandwidth = value; |
2870 | 0 | if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { |
2871 | 0 | st->silk_mode.maxInternalSampleRate = 8000; |
2872 | 0 | } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { |
2873 | 0 | st->silk_mode.maxInternalSampleRate = 12000; |
2874 | 0 | } else { |
2875 | 0 | st->silk_mode.maxInternalSampleRate = 16000; |
2876 | 0 | } |
2877 | 0 | } |
2878 | 0 | break; |
2879 | 0 | case OPUS_GET_MAX_BANDWIDTH_REQUEST: |
2880 | 0 | { |
2881 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
2882 | 0 | if (!value) |
2883 | 0 | { |
2884 | 0 | goto bad_arg; |
2885 | 0 | } |
2886 | 0 | *value = st->max_bandwidth; |
2887 | 0 | } |
2888 | 0 | break; |
2889 | 0 | case OPUS_SET_BANDWIDTH_REQUEST: |
2890 | 0 | { |
2891 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
2892 | 0 | if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO) |
2893 | 0 | { |
2894 | 0 | goto bad_arg; |
2895 | 0 | } |
2896 | 0 | st->user_bandwidth = value; |
2897 | 0 | if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { |
2898 | 0 | st->silk_mode.maxInternalSampleRate = 8000; |
2899 | 0 | } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { |
2900 | 0 | st->silk_mode.maxInternalSampleRate = 12000; |
2901 | 0 | } else { |
2902 | 0 | st->silk_mode.maxInternalSampleRate = 16000; |
2903 | 0 | } |
2904 | 0 | } |
2905 | 0 | break; |
2906 | 0 | case OPUS_GET_BANDWIDTH_REQUEST: |
2907 | 0 | { |
2908 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
2909 | 0 | if (!value) |
2910 | 0 | { |
2911 | 0 | goto bad_arg; |
2912 | 0 | } |
2913 | 0 | *value = st->bandwidth; |
2914 | 0 | } |
2915 | 0 | break; |
2916 | 0 | case OPUS_SET_DTX_REQUEST: |
2917 | 0 | { |
2918 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
2919 | 0 | if(value<0 || value>1) |
2920 | 0 | { |
2921 | 0 | goto bad_arg; |
2922 | 0 | } |
2923 | 0 | st->use_dtx = value; |
2924 | 0 | } |
2925 | 0 | break; |
2926 | 0 | case OPUS_GET_DTX_REQUEST: |
2927 | 0 | { |
2928 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
2929 | 0 | if (!value) |
2930 | 0 | { |
2931 | 0 | goto bad_arg; |
2932 | 0 | } |
2933 | 0 | *value = st->use_dtx; |
2934 | 0 | } |
2935 | 0 | break; |
2936 | 0 | case OPUS_SET_COMPLEXITY_REQUEST: |
2937 | 0 | { |
2938 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
2939 | 0 | if(value<0 || value>10) |
2940 | 0 | { |
2941 | 0 | goto bad_arg; |
2942 | 0 | } |
2943 | 0 | st->silk_mode.complexity = value; |
2944 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
2945 | 0 | celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value)); |
2946 | 0 | } |
2947 | 0 | break; |
2948 | 0 | case OPUS_GET_COMPLEXITY_REQUEST: |
2949 | 0 | { |
2950 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
2951 | 0 | if (!value) |
2952 | 0 | { |
2953 | 0 | goto bad_arg; |
2954 | 0 | } |
2955 | 0 | *value = st->silk_mode.complexity; |
2956 | 0 | } |
2957 | 0 | break; |
2958 | 0 | case OPUS_SET_INBAND_FEC_REQUEST: |
2959 | 0 | { |
2960 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
2961 | 0 | if(value<0 || value>2) |
2962 | 0 | { |
2963 | 0 | goto bad_arg; |
2964 | 0 | } |
2965 | 0 | st->fec_config = value; |
2966 | 0 | st->silk_mode.useInBandFEC = (value != 0); |
2967 | 0 | } |
2968 | 0 | break; |
2969 | 0 | case OPUS_GET_INBAND_FEC_REQUEST: |
2970 | 0 | { |
2971 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
2972 | 0 | if (!value) |
2973 | 0 | { |
2974 | 0 | goto bad_arg; |
2975 | 0 | } |
2976 | 0 | *value = st->fec_config; |
2977 | 0 | } |
2978 | 0 | break; |
2979 | 0 | case OPUS_SET_PACKET_LOSS_PERC_REQUEST: |
2980 | 0 | { |
2981 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
2982 | 0 | if (value < 0 || value > 100) |
2983 | 0 | { |
2984 | 0 | goto bad_arg; |
2985 | 0 | } |
2986 | 0 | st->silk_mode.packetLossPercentage = value; |
2987 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
2988 | 0 | celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value)); |
2989 | 0 | } |
2990 | 0 | break; |
2991 | 0 | case OPUS_GET_PACKET_LOSS_PERC_REQUEST: |
2992 | 0 | { |
2993 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
2994 | 0 | if (!value) |
2995 | 0 | { |
2996 | 0 | goto bad_arg; |
2997 | 0 | } |
2998 | 0 | *value = st->silk_mode.packetLossPercentage; |
2999 | 0 | } |
3000 | 0 | break; |
3001 | 0 | case OPUS_SET_VBR_REQUEST: |
3002 | 0 | { |
3003 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
3004 | 0 | if(value<0 || value>1) |
3005 | 0 | { |
3006 | 0 | goto bad_arg; |
3007 | 0 | } |
3008 | 0 | st->use_vbr = value; |
3009 | 0 | st->silk_mode.useCBR = 1-value; |
3010 | 0 | } |
3011 | 0 | break; |
3012 | 0 | case OPUS_GET_VBR_REQUEST: |
3013 | 0 | { |
3014 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
3015 | 0 | if (!value) |
3016 | 0 | { |
3017 | 0 | goto bad_arg; |
3018 | 0 | } |
3019 | 0 | *value = st->use_vbr; |
3020 | 0 | } |
3021 | 0 | break; |
3022 | 0 | case OPUS_SET_VOICE_RATIO_REQUEST: |
3023 | 0 | { |
3024 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
3025 | 0 | if (value<-1 || value>100) |
3026 | 0 | { |
3027 | 0 | goto bad_arg; |
3028 | 0 | } |
3029 | 0 | st->voice_ratio = value; |
3030 | 0 | } |
3031 | 0 | break; |
3032 | 0 | case OPUS_GET_VOICE_RATIO_REQUEST: |
3033 | 0 | { |
3034 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
3035 | 0 | if (!value) |
3036 | 0 | { |
3037 | 0 | goto bad_arg; |
3038 | 0 | } |
3039 | 0 | *value = st->voice_ratio; |
3040 | 0 | } |
3041 | 0 | break; |
3042 | 0 | case OPUS_SET_VBR_CONSTRAINT_REQUEST: |
3043 | 0 | { |
3044 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
3045 | 0 | if(value<0 || value>1) |
3046 | 0 | { |
3047 | 0 | goto bad_arg; |
3048 | 0 | } |
3049 | 0 | st->vbr_constraint = value; |
3050 | 0 | } |
3051 | 0 | break; |
3052 | 0 | case OPUS_GET_VBR_CONSTRAINT_REQUEST: |
3053 | 0 | { |
3054 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
3055 | 0 | if (!value) |
3056 | 0 | { |
3057 | 0 | goto bad_arg; |
3058 | 0 | } |
3059 | 0 | *value = st->vbr_constraint; |
3060 | 0 | } |
3061 | 0 | break; |
3062 | 0 | case OPUS_SET_SIGNAL_REQUEST: |
3063 | 0 | { |
3064 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
3065 | 0 | if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNAL_MUSIC) |
3066 | 0 | { |
3067 | 0 | goto bad_arg; |
3068 | 0 | } |
3069 | 0 | st->signal_type = value; |
3070 | 0 | } |
3071 | 0 | break; |
3072 | 0 | case OPUS_GET_SIGNAL_REQUEST: |
3073 | 0 | { |
3074 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
3075 | 0 | if (!value) |
3076 | 0 | { |
3077 | 0 | goto bad_arg; |
3078 | 0 | } |
3079 | 0 | *value = st->signal_type; |
3080 | 0 | } |
3081 | 0 | break; |
3082 | 0 | case OPUS_GET_LOOKAHEAD_REQUEST: |
3083 | 0 | { |
3084 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
3085 | 0 | if (!value) |
3086 | 0 | { |
3087 | 0 | goto bad_arg; |
3088 | 0 | } |
3089 | 0 | *value = st->Fs/400; |
3090 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY && st->application != OPUS_APPLICATION_RESTRICTED_CELT) |
3091 | 0 | *value += st->delay_compensation; |
3092 | 0 | } |
3093 | 0 | break; |
3094 | 0 | case OPUS_GET_SAMPLE_RATE_REQUEST: |
3095 | 0 | { |
3096 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
3097 | 0 | if (!value) |
3098 | 0 | { |
3099 | 0 | goto bad_arg; |
3100 | 0 | } |
3101 | 0 | *value = st->Fs; |
3102 | 0 | } |
3103 | 0 | break; |
3104 | 0 | case OPUS_GET_FINAL_RANGE_REQUEST: |
3105 | 0 | { |
3106 | 0 | opus_uint32 *value = va_arg(ap, opus_uint32*); |
3107 | 0 | if (!value) |
3108 | 0 | { |
3109 | 0 | goto bad_arg; |
3110 | 0 | } |
3111 | 0 | *value = st->rangeFinal; |
3112 | 0 | } |
3113 | 0 | break; |
3114 | 0 | case OPUS_SET_LSB_DEPTH_REQUEST: |
3115 | 0 | { |
3116 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
3117 | 0 | if (value<8 || value>24) |
3118 | 0 | { |
3119 | 0 | goto bad_arg; |
3120 | 0 | } |
3121 | 0 | st->lsb_depth=value; |
3122 | 0 | } |
3123 | 0 | break; |
3124 | 0 | case OPUS_GET_LSB_DEPTH_REQUEST: |
3125 | 0 | { |
3126 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
3127 | 0 | if (!value) |
3128 | 0 | { |
3129 | 0 | goto bad_arg; |
3130 | 0 | } |
3131 | 0 | *value = st->lsb_depth; |
3132 | 0 | } |
3133 | 0 | break; |
3134 | 0 | case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST: |
3135 | 0 | { |
3136 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
3137 | 0 | if (value != OPUS_FRAMESIZE_ARG && value != OPUS_FRAMESIZE_2_5_MS && |
3138 | 0 | value != OPUS_FRAMESIZE_5_MS && value != OPUS_FRAMESIZE_10_MS && |
3139 | 0 | value != OPUS_FRAMESIZE_20_MS && value != OPUS_FRAMESIZE_40_MS && |
3140 | 0 | value != OPUS_FRAMESIZE_60_MS && value != OPUS_FRAMESIZE_80_MS && |
3141 | 0 | value != OPUS_FRAMESIZE_100_MS && value != OPUS_FRAMESIZE_120_MS) |
3142 | 0 | { |
3143 | 0 | goto bad_arg; |
3144 | 0 | } |
3145 | 0 | st->variable_duration = value; |
3146 | 0 | } |
3147 | 0 | break; |
3148 | 0 | case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST: |
3149 | 0 | { |
3150 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
3151 | 0 | if (!value) |
3152 | 0 | { |
3153 | 0 | goto bad_arg; |
3154 | 0 | } |
3155 | 0 | *value = st->variable_duration; |
3156 | 0 | } |
3157 | 0 | break; |
3158 | 0 | case OPUS_SET_PREDICTION_DISABLED_REQUEST: |
3159 | 0 | { |
3160 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
3161 | 0 | if (value > 1 || value < 0) |
3162 | 0 | goto bad_arg; |
3163 | 0 | st->silk_mode.reducedDependency = value; |
3164 | 0 | } |
3165 | 0 | break; |
3166 | 0 | case OPUS_GET_PREDICTION_DISABLED_REQUEST: |
3167 | 0 | { |
3168 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
3169 | 0 | if (!value) |
3170 | 0 | goto bad_arg; |
3171 | 0 | *value = st->silk_mode.reducedDependency; |
3172 | 0 | } |
3173 | 0 | break; |
3174 | 0 | case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST: |
3175 | 0 | { |
3176 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
3177 | 0 | if(value<0 || value>1) |
3178 | 0 | { |
3179 | 0 | goto bad_arg; |
3180 | 0 | } |
3181 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
3182 | 0 | celt_encoder_ctl(celt_enc, OPUS_SET_PHASE_INVERSION_DISABLED(value)); |
3183 | 0 | } |
3184 | 0 | break; |
3185 | 0 | case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST: |
3186 | 0 | { |
3187 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
3188 | 0 | if (!value) |
3189 | 0 | { |
3190 | 0 | goto bad_arg; |
3191 | 0 | } |
3192 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
3193 | 0 | celt_encoder_ctl(celt_enc, OPUS_GET_PHASE_INVERSION_DISABLED(value)); |
3194 | 0 | else |
3195 | 0 | *value = 0; |
3196 | 0 | } |
3197 | 0 | break; |
3198 | | #ifdef ENABLE_DRED |
3199 | | case OPUS_SET_DRED_DURATION_REQUEST: |
3200 | | { |
3201 | | opus_int32 value = va_arg(ap, opus_int32); |
3202 | | if(value<0 || value>DRED_MAX_FRAMES) |
3203 | | { |
3204 | | goto bad_arg; |
3205 | | } |
3206 | | st->dred_duration = value; |
3207 | | st->silk_mode.useDRED = !!value; |
3208 | | } |
3209 | | break; |
3210 | | case OPUS_GET_DRED_DURATION_REQUEST: |
3211 | | { |
3212 | | opus_int32 *value = va_arg(ap, opus_int32*); |
3213 | | if (!value) |
3214 | | { |
3215 | | goto bad_arg; |
3216 | | } |
3217 | | *value = st->dred_duration; |
3218 | | } |
3219 | | break; |
3220 | | #endif |
3221 | | #ifdef ENABLE_QEXT |
3222 | | case OPUS_SET_QEXT_REQUEST: |
3223 | | { |
3224 | | opus_int32 value = va_arg(ap, opus_int32); |
3225 | | if(value<0 || value>1) |
3226 | | { |
3227 | | goto bad_arg; |
3228 | | } |
3229 | | st->enable_qext = value; |
3230 | | } |
3231 | | break; |
3232 | | case OPUS_GET_QEXT_REQUEST: |
3233 | | { |
3234 | | opus_int32 *value = va_arg(ap, opus_int32*); |
3235 | | if (!value) |
3236 | | { |
3237 | | goto bad_arg; |
3238 | | } |
3239 | | *value = st->enable_qext; |
3240 | | } |
3241 | | break; |
3242 | | #endif |
3243 | 0 | case OPUS_RESET_STATE: |
3244 | 0 | { |
3245 | 0 | void *silk_enc; |
3246 | 0 | silk_EncControlStruct dummy; |
3247 | 0 | char *start; |
3248 | 0 | silk_enc = (char*)st+st->silk_enc_offset; |
3249 | 0 | #ifndef DISABLE_FLOAT_API |
3250 | 0 | tonality_analysis_reset(&st->analysis); |
3251 | 0 | #endif |
3252 | |
|
3253 | 0 | start = (char*)&st->OPUS_ENCODER_RESET_START; |
3254 | 0 | OPUS_CLEAR(start, st->silk_enc_offset - (start - (char*)st)); |
3255 | |
|
3256 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
3257 | 0 | celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); |
3258 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_CELT) |
3259 | 0 | silk_InitEncoder( silk_enc, st->channels, st->arch, &dummy ); |
3260 | | #ifdef ENABLE_DRED |
3261 | | /* Initialize DRED Encoder */ |
3262 | | dred_encoder_reset( &st->dred_encoder ); |
3263 | | #endif |
3264 | 0 | st->stream_channels = st->channels; |
3265 | 0 | st->hybrid_stereo_width_Q14 = 1 << 14; |
3266 | 0 | st->prev_HB_gain = Q15ONE; |
3267 | 0 | st->first = 1; |
3268 | 0 | st->mode = MODE_HYBRID; |
3269 | 0 | st->bandwidth = OPUS_BANDWIDTH_FULLBAND; |
3270 | 0 | st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); |
3271 | 0 | } |
3272 | 0 | break; |
3273 | 0 | case OPUS_SET_FORCE_MODE_REQUEST: |
3274 | 0 | { |
3275 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
3276 | 0 | if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO) |
3277 | 0 | { |
3278 | 0 | goto bad_arg; |
3279 | 0 | } |
3280 | 0 | st->user_forced_mode = value; |
3281 | 0 | } |
3282 | 0 | break; |
3283 | 0 | case OPUS_SET_LFE_REQUEST: |
3284 | 0 | { |
3285 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
3286 | 0 | st->lfe = value; |
3287 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
3288 | 0 | ret = celt_encoder_ctl(celt_enc, OPUS_SET_LFE(value)); |
3289 | 0 | } |
3290 | 0 | break; |
3291 | 0 | case OPUS_SET_ENERGY_MASK_REQUEST: |
3292 | 0 | { |
3293 | 0 | celt_glog *value = va_arg(ap, celt_glog*); |
3294 | 0 | st->energy_masking = value; |
3295 | 0 | if (st->application != OPUS_APPLICATION_RESTRICTED_SILK) |
3296 | 0 | ret = celt_encoder_ctl(celt_enc, OPUS_SET_ENERGY_MASK(value)); |
3297 | 0 | } |
3298 | 0 | break; |
3299 | 0 | case OPUS_GET_IN_DTX_REQUEST: |
3300 | 0 | { |
3301 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
3302 | 0 | if (!value) |
3303 | 0 | { |
3304 | 0 | goto bad_arg; |
3305 | 0 | } |
3306 | 0 | if (st->silk_mode.useDTX && (st->prev_mode == MODE_SILK_ONLY || st->prev_mode == MODE_HYBRID)) { |
3307 | | /* DTX determined by Silk. */ |
3308 | 0 | silk_encoder *silk_enc = (silk_encoder*)(void *)((char*)st+st->silk_enc_offset); |
3309 | 0 | *value = silk_enc->state_Fxx[0].sCmn.noSpeechCounter >= NB_SPEECH_FRAMES_BEFORE_DTX; |
3310 | | /* Stereo: check second channel unless only the middle channel was encoded. */ |
3311 | 0 | if(*value == 1 && st->silk_mode.nChannelsInternal == 2 && silk_enc->prev_decode_only_middle == 0) { |
3312 | 0 | *value = silk_enc->state_Fxx[1].sCmn.noSpeechCounter >= NB_SPEECH_FRAMES_BEFORE_DTX; |
3313 | 0 | } |
3314 | 0 | } |
3315 | 0 | else if (st->use_dtx) { |
3316 | | /* DTX determined by Opus. */ |
3317 | 0 | *value = st->nb_no_activity_ms_Q1 >= NB_SPEECH_FRAMES_BEFORE_DTX*20*2; |
3318 | 0 | } |
3319 | 0 | else { |
3320 | 0 | *value = 0; |
3321 | 0 | } |
3322 | 0 | } |
3323 | 0 | break; |
3324 | | #ifdef USE_WEIGHTS_FILE |
3325 | | case OPUS_SET_DNN_BLOB_REQUEST: |
3326 | | { |
3327 | | const unsigned char *data = va_arg(ap, const unsigned char *); |
3328 | | opus_int32 len = va_arg(ap, opus_int32); |
3329 | | if(len<0 || data == NULL) |
3330 | | { |
3331 | | goto bad_arg; |
3332 | | } |
3333 | | #ifdef ENABLE_DRED |
3334 | | ret = dred_encoder_load_model(&st->dred_encoder, data, len); |
3335 | | #endif |
3336 | | } |
3337 | | break; |
3338 | | #endif |
3339 | 0 | case CELT_GET_MODE_REQUEST: |
3340 | 0 | { |
3341 | 0 | const CELTMode ** value = va_arg(ap, const CELTMode**); |
3342 | 0 | if (!value) |
3343 | 0 | { |
3344 | 0 | goto bad_arg; |
3345 | 0 | } |
3346 | 0 | celt_assert(celt_enc != NULL); |
3347 | 0 | ret = celt_encoder_ctl(celt_enc, CELT_GET_MODE(value)); |
3348 | 0 | } |
3349 | 0 | break; |
3350 | 0 | default: |
3351 | | /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/ |
3352 | 0 | ret = OPUS_UNIMPLEMENTED; |
3353 | 0 | break; |
3354 | 0 | } |
3355 | 0 | va_end(ap); |
3356 | 0 | return ret; |
3357 | 0 | bad_arg: |
3358 | 0 | va_end(ap); |
3359 | 0 | return OPUS_BAD_ARG; |
3360 | 0 | } |
3361 | | |
3362 | | void opus_encoder_destroy(OpusEncoder *st) |
3363 | 0 | { |
3364 | 0 | opus_free(st); |
3365 | 0 | } |