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 #ifdef FIXED_POINT
49 #include "fixed/structs_FIX.h"
50 #else
51 #include "float/structs_FLP.h"
52 #endif
53
54 #define MAX_ENCODER_BUFFER 480
55
56 #ifndef DISABLE_FLOAT_API
57 #define PSEUDO_SNR_THRESHOLD 316.23f /* 10^(25/10) */
58 #endif
59
60 typedef struct {
61 opus_val32 XX, XY, YY;
62 opus_val16 smoothed_width;
63 opus_val16 max_follower;
64 } StereoWidthState;
65
66 struct OpusEncoder {
67 int celt_enc_offset;
68 int silk_enc_offset;
69 silk_EncControlStruct silk_mode;
70 int application;
71 int channels;
72 int delay_compensation;
73 int force_channels;
74 int signal_type;
75 int user_bandwidth;
76 int max_bandwidth;
77 int user_forced_mode;
78 int voice_ratio;
79 opus_int32 Fs;
80 int use_vbr;
81 int vbr_constraint;
82 int variable_duration;
83 opus_int32 bitrate_bps;
84 opus_int32 user_bitrate_bps;
85 int lsb_depth;
86 int encoder_buffer;
87 int lfe;
88 int arch;
89 int use_dtx; /* general DTX for both SILK and CELT */
90 int fec_config;
91 #ifndef DISABLE_FLOAT_API
92 TonalityAnalysisState analysis;
93 #endif
94
95 #define OPUS_ENCODER_RESET_START stream_channels
96 int stream_channels;
97 opus_int16 hybrid_stereo_width_Q14;
98 opus_int32 variable_HP_smth2_Q15;
99 opus_val16 prev_HB_gain;
100 opus_val32 hp_mem[4];
101 int mode;
102 int prev_mode;
103 int prev_channels;
104 int prev_framesize;
105 int bandwidth;
106 /* Bandwidth determined automatically from the rate (before any other adjustment) */
107 int auto_bandwidth;
108 int silk_bw_switch;
109 /* Sampling rate (at the API level) */
110 int first;
111 opus_val16 * energy_masking;
112 StereoWidthState width_mem;
113 opus_val16 delay_buffer[MAX_ENCODER_BUFFER*2];
114 #ifndef DISABLE_FLOAT_API
115 int detected_bandwidth;
116 int nb_no_activity_ms_Q1;
117 opus_val32 peak_signal_energy;
118 #endif
119 int nonfinal_frame; /* current frame is not the final in a packet */
120 opus_uint32 rangeFinal;
121 };
122
123 /* Transition tables for the voice and music. First column is the
124 middle (memoriless) threshold. The second column is the hysteresis
125 (difference with the middle) */
126 static const opus_int32 mono_voice_bandwidth_thresholds[8] = {
127 9000, 700, /* NB<->MB */
128 9000, 700, /* MB<->WB */
129 13500, 1000, /* WB<->SWB */
130 14000, 2000, /* SWB<->FB */
131 };
132 static const opus_int32 mono_music_bandwidth_thresholds[8] = {
133 9000, 700, /* NB<->MB */
134 9000, 700, /* MB<->WB */
135 11000, 1000, /* WB<->SWB */
136 12000, 2000, /* SWB<->FB */
137 };
138 static const opus_int32 stereo_voice_bandwidth_thresholds[8] = {
139 9000, 700, /* NB<->MB */
140 9000, 700, /* MB<->WB */
141 13500, 1000, /* WB<->SWB */
142 14000, 2000, /* SWB<->FB */
143 };
144 static const opus_int32 stereo_music_bandwidth_thresholds[8] = {
145 9000, 700, /* NB<->MB */
146 9000, 700, /* MB<->WB */
147 11000, 1000, /* WB<->SWB */
148 12000, 2000, /* SWB<->FB */
149 };
150 /* Threshold bit-rates for switching between mono and stereo */
151 static const opus_int32 stereo_voice_threshold = 19000;
152 static const opus_int32 stereo_music_threshold = 17000;
153
154 /* Threshold bit-rate for switching between SILK/hybrid and CELT-only */
155 static const opus_int32 mode_thresholds[2][2] = {
156 /* voice */ /* music */
157 { 64000, 10000}, /* mono */
158 { 44000, 10000}, /* stereo */
159 };
160
161 static const opus_int32 fec_thresholds[] = {
162 12000, 1000, /* NB */
163 14000, 1000, /* MB */
164 16000, 1000, /* WB */
165 20000, 1000, /* SWB */
166 22000, 1000, /* FB */
167 };
168
opus_encoder_get_size(int channels)169 int opus_encoder_get_size(int channels)
170 {
171 int silkEncSizeBytes, celtEncSizeBytes;
172 int ret;
173 if (channels<1 || channels > 2)
174 return 0;
175 ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
176 if (ret)
177 return 0;
178 silkEncSizeBytes = align(silkEncSizeBytes);
179 celtEncSizeBytes = celt_encoder_get_size(channels);
180 return align(sizeof(OpusEncoder))+silkEncSizeBytes+celtEncSizeBytes;
181 }
182
opus_encoder_init(OpusEncoder * st,opus_int32 Fs,int channels,int application)183 int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int application)
184 {
185 void *silk_enc;
186 CELTEncoder *celt_enc;
187 int err;
188 int ret, silkEncSizeBytes;
189
190 if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2)||
191 (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO
192 && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY))
193 return OPUS_BAD_ARG;
194
195 OPUS_CLEAR((char*)st, opus_encoder_get_size(channels));
196 /* Create SILK encoder */
197 ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
198 if (ret)
199 return OPUS_BAD_ARG;
200 silkEncSizeBytes = align(silkEncSizeBytes);
201 st->silk_enc_offset = align(sizeof(OpusEncoder));
202 st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes;
203 silk_enc = (char*)st+st->silk_enc_offset;
204 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
205
206 st->stream_channels = st->channels = channels;
207
208 st->Fs = Fs;
209
210 st->arch = opus_select_arch();
211
212 ret = silk_InitEncoder( silk_enc, st->arch, &st->silk_mode );
213 if(ret)return OPUS_INTERNAL_ERROR;
214
215 /* default SILK parameters */
216 st->silk_mode.nChannelsAPI = channels;
217 st->silk_mode.nChannelsInternal = channels;
218 st->silk_mode.API_sampleRate = st->Fs;
219 st->silk_mode.maxInternalSampleRate = 16000;
220 st->silk_mode.minInternalSampleRate = 8000;
221 st->silk_mode.desiredInternalSampleRate = 16000;
222 st->silk_mode.payloadSize_ms = 20;
223 st->silk_mode.bitRate = 25000;
224 st->silk_mode.packetLossPercentage = 0;
225 st->silk_mode.complexity = 9;
226 st->silk_mode.useInBandFEC = 0;
227 st->silk_mode.useDTX = 0;
228 st->silk_mode.useCBR = 0;
229 st->silk_mode.reducedDependency = 0;
230
231 /* Create CELT encoder */
232 /* Initialize CELT encoder */
233 err = celt_encoder_init(celt_enc, Fs, channels, st->arch);
234 if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR;
235
236 celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0));
237 celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(st->silk_mode.complexity));
238
239 st->use_vbr = 1;
240 /* Makes constrained VBR the default (safer for real-time use) */
241 st->vbr_constraint = 1;
242 st->user_bitrate_bps = OPUS_AUTO;
243 st->bitrate_bps = 3000+Fs*channels;
244 st->application = application;
245 st->signal_type = OPUS_AUTO;
246 st->user_bandwidth = OPUS_AUTO;
247 st->max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
248 st->force_channels = OPUS_AUTO;
249 st->user_forced_mode = OPUS_AUTO;
250 st->voice_ratio = -1;
251 st->encoder_buffer = st->Fs/100;
252 st->lsb_depth = 24;
253 st->variable_duration = OPUS_FRAMESIZE_ARG;
254
255 /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead
256 + 1.5 ms for SILK resamplers and stereo prediction) */
257 st->delay_compensation = st->Fs/250;
258
259 st->hybrid_stereo_width_Q14 = 1 << 14;
260 st->prev_HB_gain = Q15ONE;
261 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
262 st->first = 1;
263 st->mode = MODE_HYBRID;
264 st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
265
266 #ifndef DISABLE_FLOAT_API
267 tonality_analysis_init(&st->analysis, st->Fs);
268 st->analysis.application = st->application;
269 #endif
270
271 return OPUS_OK;
272 }
273
gen_toc(int mode,int framerate,int bandwidth,int channels)274 static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channels)
275 {
276 int period;
277 unsigned char toc;
278 period = 0;
279 while (framerate < 400)
280 {
281 framerate <<= 1;
282 period++;
283 }
284 if (mode == MODE_SILK_ONLY)
285 {
286 toc = (bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5;
287 toc |= (period-2)<<3;
288 } else if (mode == MODE_CELT_ONLY)
289 {
290 int tmp = bandwidth-OPUS_BANDWIDTH_MEDIUMBAND;
291 if (tmp < 0)
292 tmp = 0;
293 toc = 0x80;
294 toc |= tmp << 5;
295 toc |= period<<3;
296 } else /* Hybrid */
297 {
298 toc = 0x60;
299 toc |= (bandwidth-OPUS_BANDWIDTH_SUPERWIDEBAND)<<4;
300 toc |= (period-2)<<3;
301 }
302 toc |= (channels==2)<<2;
303 return toc;
304 }
305
306 #ifndef FIXED_POINT
silk_biquad_float(const opus_val16 * in,const opus_int32 * B_Q28,const opus_int32 * A_Q28,opus_val32 * S,opus_val16 * out,const opus_int32 len,int stride)307 static void silk_biquad_float(
308 const opus_val16 *in, /* I: Input signal */
309 const opus_int32 *B_Q28, /* I: MA coefficients [3] */
310 const opus_int32 *A_Q28, /* I: AR coefficients [2] */
311 opus_val32 *S, /* I/O: State vector [2] */
312 opus_val16 *out, /* O: Output signal */
313 const opus_int32 len, /* I: Signal length (must be even) */
314 int stride
315 )
316 {
317 /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */
318 opus_int k;
319 opus_val32 vout;
320 opus_val32 inval;
321 opus_val32 A[2], B[3];
322
323 A[0] = (opus_val32)(A_Q28[0] * (1.f/((opus_int32)1<<28)));
324 A[1] = (opus_val32)(A_Q28[1] * (1.f/((opus_int32)1<<28)));
325 B[0] = (opus_val32)(B_Q28[0] * (1.f/((opus_int32)1<<28)));
326 B[1] = (opus_val32)(B_Q28[1] * (1.f/((opus_int32)1<<28)));
327 B[2] = (opus_val32)(B_Q28[2] * (1.f/((opus_int32)1<<28)));
328
329 /* Negate A_Q28 values and split in two parts */
330
331 for( k = 0; k < len; k++ ) {
332 /* S[ 0 ], S[ 1 ]: Q12 */
333 inval = in[ k*stride ];
334 vout = S[ 0 ] + B[0]*inval;
335
336 S[ 0 ] = S[1] - vout*A[0] + B[1]*inval;
337
338 S[ 1 ] = - vout*A[1] + B[2]*inval + VERY_SMALL;
339
340 /* Scale back to Q0 and saturate */
341 out[ k*stride ] = vout;
342 }
343 }
344 #endif
345
hp_cutoff(const opus_val16 * in,opus_int32 cutoff_Hz,opus_val16 * out,opus_val32 * hp_mem,int len,int channels,opus_int32 Fs,int arch)346 static void hp_cutoff(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs, int arch)
347 {
348 opus_int32 B_Q28[ 3 ], A_Q28[ 2 ];
349 opus_int32 Fc_Q19, r_Q28, r_Q22;
350 (void)arch;
351
352 silk_assert( cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ) );
353 Fc_Q19 = silk_DIV32_16( silk_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 );
354 silk_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 );
355
356 r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - silk_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q19 );
357
358 /* b = r * [ 1; -2; 1 ]; */
359 /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */
360 B_Q28[ 0 ] = r_Q28;
361 B_Q28[ 1 ] = silk_LSHIFT( -r_Q28, 1 );
362 B_Q28[ 2 ] = r_Q28;
363
364 /* -r * ( 2 - Fc * Fc ); */
365 r_Q22 = silk_RSHIFT( r_Q28, 6 );
366 A_Q28[ 0 ] = silk_SMULWW( r_Q22, silk_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CONST( 2.0, 22 ) );
367 A_Q28[ 1 ] = silk_SMULWW( r_Q22, r_Q22 );
368
369 #ifdef FIXED_POINT
370 if( channels == 1 ) {
371 silk_biquad_alt_stride1( in, B_Q28, A_Q28, hp_mem, out, len );
372 } else {
373 silk_biquad_alt_stride2( in, B_Q28, A_Q28, hp_mem, out, len, arch );
374 }
375 #else
376 silk_biquad_float( in, B_Q28, A_Q28, hp_mem, out, len, channels );
377 if( channels == 2 ) {
378 silk_biquad_float( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels );
379 }
380 #endif
381 }
382
383 #ifdef FIXED_POINT
dc_reject(const opus_val16 * in,opus_int32 cutoff_Hz,opus_val16 * out,opus_val32 * hp_mem,int len,int channels,opus_int32 Fs)384 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)
385 {
386 int c, i;
387 int shift;
388
389 /* Approximates -round(log2(6.3*cutoff_Hz/Fs)) */
390 shift=celt_ilog2(Fs/(cutoff_Hz*4));
391 for (c=0;c<channels;c++)
392 {
393 for (i=0;i<len;i++)
394 {
395 opus_val32 x, y;
396 x = SHL32(EXTEND32(in[channels*i+c]), 14);
397 y = x-hp_mem[2*c];
398 hp_mem[2*c] = hp_mem[2*c] + PSHR32(x - hp_mem[2*c], shift);
399 out[channels*i+c] = EXTRACT16(SATURATE(PSHR32(y, 14), 32767));
400 }
401 }
402 }
403
404 #else
dc_reject(const opus_val16 * in,opus_int32 cutoff_Hz,opus_val16 * out,opus_val32 * hp_mem,int len,int channels,opus_int32 Fs)405 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)
406 {
407 int i;
408 float coef, coef2;
409 coef = 6.3f*cutoff_Hz/Fs;
410 coef2 = 1-coef;
411 if (channels==2)
412 {
413 float m0, m2;
414 m0 = hp_mem[0];
415 m2 = hp_mem[2];
416 for (i=0;i<len;i++)
417 {
418 opus_val32 x0, x1, out0, out1;
419 x0 = in[2*i+0];
420 x1 = in[2*i+1];
421 out0 = x0-m0;
422 out1 = x1-m2;
423 m0 = coef*x0 + VERY_SMALL + coef2*m0;
424 m2 = coef*x1 + VERY_SMALL + coef2*m2;
425 out[2*i+0] = out0;
426 out[2*i+1] = out1;
427 }
428 hp_mem[0] = m0;
429 hp_mem[2] = m2;
430 } else {
431 float m0;
432 m0 = hp_mem[0];
433 for (i=0;i<len;i++)
434 {
435 opus_val32 x, y;
436 x = in[i];
437 y = x-m0;
438 m0 = coef*x + VERY_SMALL + coef2*m0;
439 out[i] = y;
440 }
441 hp_mem[0] = m0;
442 }
443 }
444 #endif
445
stereo_fade(const opus_val16 * in,opus_val16 * out,opus_val16 g1,opus_val16 g2,int overlap48,int frame_size,int channels,const opus_val16 * window,opus_int32 Fs)446 static void stereo_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, opus_val16 g2,
447 int overlap48, int frame_size, int channels, const opus_val16 *window, opus_int32 Fs)
448 {
449 int i;
450 int overlap;
451 int inc;
452 inc = 48000/Fs;
453 overlap=overlap48/inc;
454 g1 = Q15ONE-g1;
455 g2 = Q15ONE-g2;
456 for (i=0;i<overlap;i++)
457 {
458 opus_val32 diff;
459 opus_val16 g, w;
460 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
461 g = SHR32(MAC16_16(MULT16_16(w,g2),
462 Q15ONE-w, g1), 15);
463 diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]));
464 diff = MULT16_16_Q15(g, diff);
465 out[i*channels] = out[i*channels] - diff;
466 out[i*channels+1] = out[i*channels+1] + diff;
467 }
468 for (;i<frame_size;i++)
469 {
470 opus_val32 diff;
471 diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]));
472 diff = MULT16_16_Q15(g2, diff);
473 out[i*channels] = out[i*channels] - diff;
474 out[i*channels+1] = out[i*channels+1] + diff;
475 }
476 }
477
gain_fade(const opus_val16 * in,opus_val16 * out,opus_val16 g1,opus_val16 g2,int overlap48,int frame_size,int channels,const opus_val16 * window,opus_int32 Fs)478 static void gain_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, opus_val16 g2,
479 int overlap48, int frame_size, int channels, const opus_val16 *window, opus_int32 Fs)
480 {
481 int i;
482 int inc;
483 int overlap;
484 int c;
485 inc = 48000/Fs;
486 overlap=overlap48/inc;
487 if (channels==1)
488 {
489 for (i=0;i<overlap;i++)
490 {
491 opus_val16 g, w;
492 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
493 g = SHR32(MAC16_16(MULT16_16(w,g2),
494 Q15ONE-w, g1), 15);
495 out[i] = MULT16_16_Q15(g, in[i]);
496 }
497 } else {
498 for (i=0;i<overlap;i++)
499 {
500 opus_val16 g, w;
501 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
502 g = SHR32(MAC16_16(MULT16_16(w,g2),
503 Q15ONE-w, g1), 15);
504 out[i*2] = MULT16_16_Q15(g, in[i*2]);
505 out[i*2+1] = MULT16_16_Q15(g, in[i*2+1]);
506 }
507 }
508 c=0;do {
509 for (i=overlap;i<frame_size;i++)
510 {
511 out[i*channels+c] = MULT16_16_Q15(g2, in[i*channels+c]);
512 }
513 }
514 while (++c<channels);
515 }
516
opus_encoder_create(opus_int32 Fs,int channels,int application,int * error)517 OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error)
518 {
519 int ret;
520 OpusEncoder *st;
521 if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2)||
522 (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO
523 && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY))
524 {
525 if (error)
526 *error = OPUS_BAD_ARG;
527 return NULL;
528 }
529 st = (OpusEncoder *)opus_alloc(opus_encoder_get_size(channels));
530 if (st == NULL)
531 {
532 if (error)
533 *error = OPUS_ALLOC_FAIL;
534 return NULL;
535 }
536 ret = opus_encoder_init(st, Fs, channels, application);
537 if (error)
538 *error = ret;
539 if (ret != OPUS_OK)
540 {
541 opus_free(st);
542 st = NULL;
543 }
544 return st;
545 }
546
user_bitrate_to_bitrate(OpusEncoder * st,int frame_size,int max_data_bytes)547 static opus_int32 user_bitrate_to_bitrate(OpusEncoder *st, int frame_size, int max_data_bytes)
548 {
549 if(!frame_size)frame_size=st->Fs/400;
550 if (st->user_bitrate_bps==OPUS_AUTO)
551 return 60*st->Fs/frame_size + st->Fs*st->channels;
552 else if (st->user_bitrate_bps==OPUS_BITRATE_MAX)
553 return max_data_bytes*8*st->Fs/frame_size;
554 else
555 return st->user_bitrate_bps;
556 }
557
558 #ifndef DISABLE_FLOAT_API
559 #ifdef FIXED_POINT
560 #define PCM2VAL(x) FLOAT2INT16(x)
561 #else
562 #define PCM2VAL(x) SCALEIN(x)
563 #endif
564
downmix_float(const void * _x,opus_val32 * y,int subframe,int offset,int c1,int c2,int C)565 void downmix_float(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C)
566 {
567 const float *x;
568 int j;
569
570 x = (const float *)_x;
571 for (j=0;j<subframe;j++)
572 y[j] = PCM2VAL(x[(j+offset)*C+c1]);
573 if (c2>-1)
574 {
575 for (j=0;j<subframe;j++)
576 y[j] += PCM2VAL(x[(j+offset)*C+c2]);
577 } else if (c2==-2)
578 {
579 int c;
580 for (c=1;c<C;c++)
581 {
582 for (j=0;j<subframe;j++)
583 y[j] += PCM2VAL(x[(j+offset)*C+c]);
584 }
585 }
586 }
587 #endif
588
downmix_int(const void * _x,opus_val32 * y,int subframe,int offset,int c1,int c2,int C)589 void downmix_int(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C)
590 {
591 const opus_int16 *x;
592 int j;
593
594 x = (const opus_int16 *)_x;
595 for (j=0;j<subframe;j++)
596 y[j] = x[(j+offset)*C+c1];
597 if (c2>-1)
598 {
599 for (j=0;j<subframe;j++)
600 y[j] += x[(j+offset)*C+c2];
601 } else if (c2==-2)
602 {
603 int c;
604 for (c=1;c<C;c++)
605 {
606 for (j=0;j<subframe;j++)
607 y[j] += x[(j+offset)*C+c];
608 }
609 }
610 }
611
frame_size_select(opus_int32 frame_size,int variable_duration,opus_int32 Fs)612 opus_int32 frame_size_select(opus_int32 frame_size, int variable_duration, opus_int32 Fs)
613 {
614 int new_size;
615 if (frame_size<Fs/400)
616 return -1;
617 if (variable_duration == OPUS_FRAMESIZE_ARG)
618 new_size = frame_size;
619 else if (variable_duration >= OPUS_FRAMESIZE_2_5_MS && variable_duration <= OPUS_FRAMESIZE_120_MS)
620 {
621 if (variable_duration <= OPUS_FRAMESIZE_40_MS)
622 new_size = (Fs/400)<<(variable_duration-OPUS_FRAMESIZE_2_5_MS);
623 else
624 new_size = (variable_duration-OPUS_FRAMESIZE_2_5_MS-2)*Fs/50;
625 }
626 else
627 return -1;
628 if (new_size>frame_size)
629 return -1;
630 if (400*new_size!=Fs && 200*new_size!=Fs && 100*new_size!=Fs &&
631 50*new_size!=Fs && 25*new_size!=Fs && 50*new_size!=3*Fs &&
632 50*new_size!=4*Fs && 50*new_size!=5*Fs && 50*new_size!=6*Fs)
633 return -1;
634 return new_size;
635 }
636
compute_stereo_width(const opus_val16 * pcm,int frame_size,opus_int32 Fs,StereoWidthState * mem)637 opus_val16 compute_stereo_width(const opus_val16 *pcm, int frame_size, opus_int32 Fs, StereoWidthState *mem)
638 {
639 opus_val32 xx, xy, yy;
640 opus_val16 sqrt_xx, sqrt_yy;
641 opus_val16 qrrt_xx, qrrt_yy;
642 int frame_rate;
643 int i;
644 opus_val16 short_alpha;
645
646 frame_rate = Fs/frame_size;
647 short_alpha = Q15ONE - MULT16_16(25, Q15ONE)/IMAX(50,frame_rate);
648 xx=xy=yy=0;
649 /* Unroll by 4. The frame size is always a multiple of 4 *except* for
650 2.5 ms frames at 12 kHz. Since this setting is very rare (and very
651 stupid), we just discard the last two samples. */
652 for (i=0;i<frame_size-3;i+=4)
653 {
654 opus_val32 pxx=0;
655 opus_val32 pxy=0;
656 opus_val32 pyy=0;
657 opus_val16 x, y;
658 x = pcm[2*i];
659 y = pcm[2*i+1];
660 pxx = SHR32(MULT16_16(x,x),2);
661 pxy = SHR32(MULT16_16(x,y),2);
662 pyy = SHR32(MULT16_16(y,y),2);
663 x = pcm[2*i+2];
664 y = pcm[2*i+3];
665 pxx += SHR32(MULT16_16(x,x),2);
666 pxy += SHR32(MULT16_16(x,y),2);
667 pyy += SHR32(MULT16_16(y,y),2);
668 x = pcm[2*i+4];
669 y = pcm[2*i+5];
670 pxx += SHR32(MULT16_16(x,x),2);
671 pxy += SHR32(MULT16_16(x,y),2);
672 pyy += SHR32(MULT16_16(y,y),2);
673 x = pcm[2*i+6];
674 y = pcm[2*i+7];
675 pxx += SHR32(MULT16_16(x,x),2);
676 pxy += SHR32(MULT16_16(x,y),2);
677 pyy += SHR32(MULT16_16(y,y),2);
678
679 xx += SHR32(pxx, 10);
680 xy += SHR32(pxy, 10);
681 yy += SHR32(pyy, 10);
682 }
683 #ifndef FIXED_POINT
684 if (!(xx < 1e9f) || celt_isnan(xx) || !(yy < 1e9f) || celt_isnan(yy))
685 {
686 xy = xx = yy = 0;
687 }
688 #endif
689 mem->XX += MULT16_32_Q15(short_alpha, xx-mem->XX);
690 mem->XY += MULT16_32_Q15(short_alpha, xy-mem->XY);
691 mem->YY += MULT16_32_Q15(short_alpha, yy-mem->YY);
692 mem->XX = MAX32(0, mem->XX);
693 mem->XY = MAX32(0, mem->XY);
694 mem->YY = MAX32(0, mem->YY);
695 if (MAX32(mem->XX, mem->YY)>QCONST16(8e-4f, 18))
696 {
697 opus_val16 corr;
698 opus_val16 ldiff;
699 opus_val16 width;
700 sqrt_xx = celt_sqrt(mem->XX);
701 sqrt_yy = celt_sqrt(mem->YY);
702 qrrt_xx = celt_sqrt(sqrt_xx);
703 qrrt_yy = celt_sqrt(sqrt_yy);
704 /* Inter-channel correlation */
705 mem->XY = MIN32(mem->XY, sqrt_xx*sqrt_yy);
706 corr = SHR32(frac_div32(mem->XY,EPSILON+MULT16_16(sqrt_xx,sqrt_yy)),16);
707 /* Approximate loudness difference */
708 ldiff = MULT16_16(Q15ONE, ABS16(qrrt_xx-qrrt_yy))/(EPSILON+qrrt_xx+qrrt_yy);
709 width = MULT16_16_Q15(celt_sqrt(QCONST32(1.f,30)-MULT16_16(corr,corr)), ldiff);
710 /* Smoothing over one second */
711 mem->smoothed_width += (width-mem->smoothed_width)/frame_rate;
712 /* Peak follower */
713 mem->max_follower = MAX16(mem->max_follower-QCONST16(.02f,15)/frame_rate, mem->smoothed_width);
714 }
715 /*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);*/
716 return EXTRACT16(MIN32(Q15ONE, MULT16_16(20, mem->max_follower)));
717 }
718
decide_fec(int useInBandFEC,int PacketLoss_perc,int last_fec,int mode,int * bandwidth,opus_int32 rate)719 static int decide_fec(int useInBandFEC, int PacketLoss_perc, int last_fec, int mode, int *bandwidth, opus_int32 rate)
720 {
721 int orig_bandwidth;
722 if (!useInBandFEC || PacketLoss_perc == 0 || mode == MODE_CELT_ONLY)
723 return 0;
724 orig_bandwidth = *bandwidth;
725 for (;;)
726 {
727 opus_int32 hysteresis;
728 opus_int32 LBRR_rate_thres_bps;
729 /* Compute threshold for using FEC at the current bandwidth setting */
730 LBRR_rate_thres_bps = fec_thresholds[2*(*bandwidth - OPUS_BANDWIDTH_NARROWBAND)];
731 hysteresis = fec_thresholds[2*(*bandwidth - OPUS_BANDWIDTH_NARROWBAND) + 1];
732 if (last_fec == 1) LBRR_rate_thres_bps -= hysteresis;
733 if (last_fec == 0) LBRR_rate_thres_bps += hysteresis;
734 LBRR_rate_thres_bps = silk_SMULWB( silk_MUL( LBRR_rate_thres_bps,
735 125 - silk_min( PacketLoss_perc, 25 ) ), SILK_FIX_CONST( 0.01, 16 ) );
736 /* If loss <= 5%, we look at whether we have enough rate to enable FEC.
737 If loss > 5%, we decrease the bandwidth until we can enable FEC. */
738 if (rate > LBRR_rate_thres_bps)
739 return 1;
740 else if (PacketLoss_perc <= 5)
741 return 0;
742 else if (*bandwidth > OPUS_BANDWIDTH_NARROWBAND)
743 (*bandwidth)--;
744 else
745 break;
746 }
747 /* Couldn't find any bandwidth to enable FEC, keep original bandwidth. */
748 *bandwidth = orig_bandwidth;
749 return 0;
750 }
751
compute_silk_rate_for_hybrid(int rate,int bandwidth,int frame20ms,int vbr,int fec,int channels)752 static int compute_silk_rate_for_hybrid(int rate, int bandwidth, int frame20ms, int vbr, int fec, int channels) {
753 int entry;
754 int i;
755 int N;
756 int silk_rate;
757 static int rate_table[][5] = {
758 /* |total| |-------- SILK------------|
759 |-- No FEC -| |--- FEC ---|
760 10ms 20ms 10ms 20ms */
761 { 0, 0, 0, 0, 0},
762 {12000, 10000, 10000, 11000, 11000},
763 {16000, 13500, 13500, 15000, 15000},
764 {20000, 16000, 16000, 18000, 18000},
765 {24000, 18000, 18000, 21000, 21000},
766 {32000, 22000, 22000, 28000, 28000},
767 {64000, 38000, 38000, 50000, 50000}
768 };
769 /* Do the allocation per-channel. */
770 rate /= channels;
771 entry = 1 + frame20ms + 2*fec;
772 N = sizeof(rate_table)/sizeof(rate_table[0]);
773 for (i=1;i<N;i++)
774 {
775 if (rate_table[i][0] > rate) break;
776 }
777 if (i == N)
778 {
779 silk_rate = rate_table[i-1][entry];
780 /* For now, just give 50% of the extra bits to SILK. */
781 silk_rate += (rate-rate_table[i-1][0])/2;
782 } else {
783 opus_int32 lo, hi, x0, x1;
784 lo = rate_table[i-1][entry];
785 hi = rate_table[i][entry];
786 x0 = rate_table[i-1][0];
787 x1 = rate_table[i][0];
788 silk_rate = (lo*(x1-rate) + hi*(rate-x0))/(x1-x0);
789 }
790 if (!vbr)
791 {
792 /* Tiny boost to SILK for CBR. We should probably tune this better. */
793 silk_rate += 100;
794 }
795 if (bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND)
796 silk_rate += 300;
797 silk_rate *= channels;
798 /* Small adjustment for stereo (calibrated for 32 kb/s, haven't tried other bitrates). */
799 if (channels == 2 && rate >= 12000)
800 silk_rate -= 1000;
801 return silk_rate;
802 }
803
804 /* Returns the equivalent bitrate corresponding to 20 ms frames,
805 complexity 10 VBR operation. */
compute_equiv_rate(opus_int32 bitrate,int channels,int frame_rate,int vbr,int mode,int complexity,int loss)806 static opus_int32 compute_equiv_rate(opus_int32 bitrate, int channels,
807 int frame_rate, int vbr, int mode, int complexity, int loss)
808 {
809 opus_int32 equiv;
810 equiv = bitrate;
811 /* Take into account overhead from smaller frames. */
812 if (frame_rate > 50)
813 equiv -= (40*channels+20)*(frame_rate - 50);
814 /* CBR is about a 8% penalty for both SILK and CELT. */
815 if (!vbr)
816 equiv -= equiv/12;
817 /* Complexity makes about 10% difference (from 0 to 10) in general. */
818 equiv = equiv * (90+complexity)/100;
819 if (mode == MODE_SILK_ONLY || mode == MODE_HYBRID)
820 {
821 /* SILK complexity 0-1 uses the non-delayed-decision NSQ, which
822 costs about 20%. */
823 if (complexity<2)
824 equiv = equiv*4/5;
825 equiv -= equiv*loss/(6*loss + 10);
826 } else if (mode == MODE_CELT_ONLY) {
827 /* CELT complexity 0-4 doesn't have the pitch filter, which costs
828 about 10%. */
829 if (complexity<5)
830 equiv = equiv*9/10;
831 } else {
832 /* Mode not known yet */
833 /* Half the SILK loss*/
834 equiv -= equiv*loss/(12*loss + 20);
835 }
836 return equiv;
837 }
838
839 #ifndef DISABLE_FLOAT_API
840
is_digital_silence(const opus_val16 * pcm,int frame_size,int channels,int lsb_depth)841 int is_digital_silence(const opus_val16* pcm, int frame_size, int channels, int lsb_depth)
842 {
843 int silence = 0;
844 opus_val32 sample_max = 0;
845 #ifdef MLP_TRAINING
846 return 0;
847 #endif
848 sample_max = celt_maxabs16(pcm, frame_size*channels);
849
850 #ifdef FIXED_POINT
851 silence = (sample_max == 0);
852 (void)lsb_depth;
853 #else
854 silence = (sample_max <= (opus_val16) 1 / (1 << lsb_depth));
855 #endif
856
857 return silence;
858 }
859
860 #ifdef FIXED_POINT
compute_frame_energy(const opus_val16 * pcm,int frame_size,int channels,int arch)861 static opus_val32 compute_frame_energy(const opus_val16 *pcm, int frame_size, int channels, int arch)
862 {
863 int i;
864 opus_val32 sample_max;
865 int max_shift;
866 int shift;
867 opus_val32 energy = 0;
868 int len = frame_size*channels;
869 (void)arch;
870 /* Max amplitude in the signal */
871 sample_max = celt_maxabs16(pcm, len);
872
873 /* Compute the right shift required in the MAC to avoid an overflow */
874 max_shift = celt_ilog2(len);
875 shift = IMAX(0, (celt_ilog2(sample_max) << 1) + max_shift - 28);
876
877 /* Compute the energy */
878 for (i=0; i<len; i++)
879 energy += SHR32(MULT16_16(pcm[i], pcm[i]), shift);
880
881 /* Normalize energy by the frame size and left-shift back to the original position */
882 energy /= len;
883 energy = SHL32(energy, shift);
884
885 return energy;
886 }
887 #else
compute_frame_energy(const opus_val16 * pcm,int frame_size,int channels,int arch)888 static opus_val32 compute_frame_energy(const opus_val16 *pcm, int frame_size, int channels, int arch)
889 {
890 int len = frame_size*channels;
891 return celt_inner_prod(pcm, pcm, len, arch)/len;
892 }
893 #endif
894
895 /* Decides if DTX should be turned on (=1) or off (=0) */
decide_dtx_mode(opus_int activity,int * nb_no_activity_ms_Q1,int frame_size_ms_Q1)896 static int decide_dtx_mode(opus_int activity, /* indicates if this frame contains speech/music */
897 int *nb_no_activity_ms_Q1, /* number of consecutive milliseconds with no activity, in Q1 */
898 int frame_size_ms_Q1 /* number of miliseconds in this update, in Q1 */
899 )
900
901 {
902 if (!activity)
903 {
904 /* The number of consecutive DTX frames should be within the allowed bounds.
905 Note that the allowed bound is defined in the SILK headers and assumes 20 ms
906 frames. As this function can be called with any frame length, a conversion to
907 milliseconds is done before the comparisons. */
908 (*nb_no_activity_ms_Q1) += frame_size_ms_Q1;
909 if (*nb_no_activity_ms_Q1 > NB_SPEECH_FRAMES_BEFORE_DTX*20*2)
910 {
911 if (*nb_no_activity_ms_Q1 <= (NB_SPEECH_FRAMES_BEFORE_DTX + MAX_CONSECUTIVE_DTX)*20*2)
912 /* Valid frame for DTX! */
913 return 1;
914 else
915 (*nb_no_activity_ms_Q1) = NB_SPEECH_FRAMES_BEFORE_DTX*20*2;
916 }
917 } else
918 (*nb_no_activity_ms_Q1) = 0;
919
920 return 0;
921 }
922
923 #endif
924
encode_multiframe_packet(OpusEncoder * st,const opus_val16 * pcm,int nb_frames,int frame_size,unsigned char * data,opus_int32 out_data_bytes,int to_celt,int lsb_depth,int float_api)925 static opus_int32 encode_multiframe_packet(OpusEncoder *st,
926 const opus_val16 *pcm,
927 int nb_frames,
928 int frame_size,
929 unsigned char *data,
930 opus_int32 out_data_bytes,
931 int to_celt,
932 int lsb_depth,
933 int float_api)
934 {
935 int i;
936 int ret = 0;
937 VARDECL(unsigned char, tmp_data);
938 int bak_mode, bak_bandwidth, bak_channels, bak_to_mono;
939 VARDECL(OpusRepacketizer, rp);
940 int max_header_bytes;
941 opus_int32 bytes_per_frame;
942 opus_int32 cbr_bytes;
943 opus_int32 repacketize_len;
944 int tmp_len;
945 ALLOC_STACK;
946
947 /* Worst cases:
948 * 2 frames: Code 2 with different compressed sizes
949 * >2 frames: Code 3 VBR */
950 max_header_bytes = nb_frames == 2 ? 3 : (2+(nb_frames-1)*2);
951
952 if (st->use_vbr || st->user_bitrate_bps==OPUS_BITRATE_MAX)
953 repacketize_len = out_data_bytes;
954 else {
955 cbr_bytes = 3*st->bitrate_bps/(3*8*st->Fs/(frame_size*nb_frames));
956 repacketize_len = IMIN(cbr_bytes, out_data_bytes);
957 }
958 bytes_per_frame = IMIN(1276, 1+(repacketize_len-max_header_bytes)/nb_frames);
959
960 ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char);
961 ALLOC(rp, 1, OpusRepacketizer);
962 opus_repacketizer_init(rp);
963
964 bak_mode = st->user_forced_mode;
965 bak_bandwidth = st->user_bandwidth;
966 bak_channels = st->force_channels;
967
968 st->user_forced_mode = st->mode;
969 st->user_bandwidth = st->bandwidth;
970 st->force_channels = st->stream_channels;
971
972 bak_to_mono = st->silk_mode.toMono;
973 if (bak_to_mono)
974 st->force_channels = 1;
975 else
976 st->prev_channels = st->stream_channels;
977
978 for (i=0;i<nb_frames;i++)
979 {
980 st->silk_mode.toMono = 0;
981 st->nonfinal_frame = i<(nb_frames-1);
982
983 /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */
984 if (to_celt && i==nb_frames-1)
985 st->user_forced_mode = MODE_CELT_ONLY;
986
987 tmp_len = opus_encode_native(st, pcm+i*(st->channels*frame_size), frame_size,
988 tmp_data+i*bytes_per_frame, bytes_per_frame, lsb_depth, NULL, 0, 0, 0, 0,
989 NULL, float_api);
990
991 if (tmp_len<0)
992 {
993 RESTORE_STACK;
994 return OPUS_INTERNAL_ERROR;
995 }
996
997 ret = opus_repacketizer_cat(rp, tmp_data+i*bytes_per_frame, tmp_len);
998
999 if (ret<0)
1000 {
1001 RESTORE_STACK;
1002 return OPUS_INTERNAL_ERROR;
1003 }
1004 }
1005
1006 ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketize_len, 0, !st->use_vbr);
1007
1008 if (ret<0)
1009 {
1010 RESTORE_STACK;
1011 return OPUS_INTERNAL_ERROR;
1012 }
1013
1014 /* Discard configs that were forced locally for the purpose of repacketization */
1015 st->user_forced_mode = bak_mode;
1016 st->user_bandwidth = bak_bandwidth;
1017 st->force_channels = bak_channels;
1018 st->silk_mode.toMono = bak_to_mono;
1019
1020 RESTORE_STACK;
1021 return ret;
1022 }
1023
compute_redundancy_bytes(opus_int32 max_data_bytes,opus_int32 bitrate_bps,int frame_rate,int channels)1024 static int compute_redundancy_bytes(opus_int32 max_data_bytes, opus_int32 bitrate_bps, int frame_rate, int channels)
1025 {
1026 int redundancy_bytes_cap;
1027 int redundancy_bytes;
1028 opus_int32 redundancy_rate;
1029 int base_bits;
1030 opus_int32 available_bits;
1031 base_bits = (40*channels+20);
1032
1033 /* Equivalent rate for 5 ms frames. */
1034 redundancy_rate = bitrate_bps + base_bits*(200 - frame_rate);
1035 /* For VBR, further increase the bitrate if we can afford it. It's pretty short
1036 and we'll avoid artefacts. */
1037 redundancy_rate = 3*redundancy_rate/2;
1038 redundancy_bytes = redundancy_rate/1600;
1039
1040 /* Compute the max rate we can use given CBR or VBR with cap. */
1041 available_bits = max_data_bytes*8 - 2*base_bits;
1042 redundancy_bytes_cap = (available_bits*240/(240+48000/frame_rate) + base_bits)/8;
1043 redundancy_bytes = IMIN(redundancy_bytes, redundancy_bytes_cap);
1044 /* It we can't get enough bits for redundancy to be worth it, rely on the decoder PLC. */
1045 if (redundancy_bytes > 4 + 8*channels)
1046 redundancy_bytes = IMIN(257, redundancy_bytes);
1047 else
1048 redundancy_bytes = 0;
1049 return redundancy_bytes;
1050 }
1051
opus_encode_native(OpusEncoder * st,const opus_val16 * pcm,int frame_size,unsigned char * data,opus_int32 out_data_bytes,int lsb_depth,const void * analysis_pcm,opus_int32 analysis_size,int c1,int c2,int analysis_channels,downmix_func downmix,int float_api)1052 opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
1053 unsigned char *data, opus_int32 out_data_bytes, int lsb_depth,
1054 const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2,
1055 int analysis_channels, downmix_func downmix, int float_api)
1056 {
1057 void *silk_enc;
1058 CELTEncoder *celt_enc;
1059 int i;
1060 int ret=0;
1061 opus_int32 nBytes;
1062 ec_enc enc;
1063 int bytes_target;
1064 int prefill=0;
1065 int start_band = 0;
1066 int redundancy = 0;
1067 int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */
1068 int celt_to_silk = 0;
1069 VARDECL(opus_val16, pcm_buf);
1070 int nb_compr_bytes;
1071 int to_celt = 0;
1072 opus_uint32 redundant_rng = 0;
1073 int cutoff_Hz, hp_freq_smth1;
1074 int voice_est; /* Probability of voice in Q7 */
1075 opus_int32 equiv_rate;
1076 int delay_compensation;
1077 int frame_rate;
1078 opus_int32 max_rate; /* Max bitrate we're allowed to use */
1079 int curr_bandwidth;
1080 opus_val16 HB_gain;
1081 opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */
1082 int total_buffer;
1083 opus_val16 stereo_width;
1084 const CELTMode *celt_mode;
1085 #ifndef DISABLE_FLOAT_API
1086 AnalysisInfo analysis_info;
1087 int analysis_read_pos_bak=-1;
1088 int analysis_read_subframe_bak=-1;
1089 int is_silence = 0;
1090 #endif
1091 opus_int activity = VAD_NO_DECISION;
1092
1093 VARDECL(opus_val16, tmp_prefill);
1094
1095 ALLOC_STACK;
1096
1097 max_data_bytes = IMIN(1276, out_data_bytes);
1098
1099 st->rangeFinal = 0;
1100 if (frame_size <= 0 || max_data_bytes <= 0)
1101 {
1102 RESTORE_STACK;
1103 return OPUS_BAD_ARG;
1104 }
1105
1106 /* Cannot encode 100 ms in 1 byte */
1107 if (max_data_bytes==1 && st->Fs==(frame_size*10))
1108 {
1109 RESTORE_STACK;
1110 return OPUS_BUFFER_TOO_SMALL;
1111 }
1112
1113 silk_enc = (char*)st+st->silk_enc_offset;
1114 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1115 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1116 delay_compensation = 0;
1117 else
1118 delay_compensation = st->delay_compensation;
1119
1120 lsb_depth = IMIN(lsb_depth, st->lsb_depth);
1121
1122 celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1123 #ifndef DISABLE_FLOAT_API
1124 analysis_info.valid = 0;
1125 #ifdef FIXED_POINT
1126 if (st->silk_mode.complexity >= 10 && st->Fs>=16000)
1127 #else
1128 if (st->silk_mode.complexity >= 7 && st->Fs>=16000)
1129 #endif
1130 {
1131 is_silence = is_digital_silence(pcm, frame_size, st->channels, lsb_depth);
1132 analysis_read_pos_bak = st->analysis.read_pos;
1133 analysis_read_subframe_bak = st->analysis.read_subframe;
1134 run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame_size,
1135 c1, c2, analysis_channels, st->Fs,
1136 lsb_depth, downmix, &analysis_info);
1137
1138 /* Track the peak signal energy */
1139 if (!is_silence && analysis_info.activity_probability > DTX_ACTIVITY_THRESHOLD)
1140 st->peak_signal_energy = MAX32(MULT16_32_Q15(QCONST16(0.999f, 15), st->peak_signal_energy),
1141 compute_frame_energy(pcm, frame_size, st->channels, st->arch));
1142 } else if (st->analysis.initialized) {
1143 tonality_analysis_reset(&st->analysis);
1144 }
1145 #else
1146 (void)analysis_pcm;
1147 (void)analysis_size;
1148 (void)c1;
1149 (void)c2;
1150 (void)analysis_channels;
1151 (void)downmix;
1152 #endif
1153
1154 #ifndef DISABLE_FLOAT_API
1155 /* Reset voice_ratio if this frame is not silent or if analysis is disabled.
1156 * Otherwise, preserve voice_ratio from the last non-silent frame */
1157 if (!is_silence)
1158 st->voice_ratio = -1;
1159
1160 if (is_silence)
1161 {
1162 activity = !is_silence;
1163 } else if (analysis_info.valid)
1164 {
1165 activity = analysis_info.activity_probability >= DTX_ACTIVITY_THRESHOLD;
1166 if (!activity)
1167 {
1168 /* Mark as active if this noise frame is sufficiently loud */
1169 opus_val32 noise_energy = compute_frame_energy(pcm, frame_size, st->channels, st->arch);
1170 activity = st->peak_signal_energy < (PSEUDO_SNR_THRESHOLD * noise_energy);
1171 }
1172 }
1173
1174 st->detected_bandwidth = 0;
1175 if (analysis_info.valid)
1176 {
1177 int analysis_bandwidth;
1178 if (st->signal_type == OPUS_AUTO)
1179 {
1180 float prob;
1181 if (st->prev_mode == 0)
1182 prob = analysis_info.music_prob;
1183 else if (st->prev_mode == MODE_CELT_ONLY)
1184 prob = analysis_info.music_prob_max;
1185 else
1186 prob = analysis_info.music_prob_min;
1187 st->voice_ratio = (int)floor(.5+100*(1-prob));
1188 }
1189
1190 analysis_bandwidth = analysis_info.bandwidth;
1191 if (analysis_bandwidth<=12)
1192 st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1193 else if (analysis_bandwidth<=14)
1194 st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1195 else if (analysis_bandwidth<=16)
1196 st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1197 else if (analysis_bandwidth<=18)
1198 st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1199 else
1200 st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1201 }
1202 #else
1203 st->voice_ratio = -1;
1204 #endif
1205
1206 if (st->channels==2 && st->force_channels!=1)
1207 stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_mem);
1208 else
1209 stereo_width = 0;
1210 total_buffer = delay_compensation;
1211 st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
1212
1213 frame_rate = st->Fs/frame_size;
1214 if (!st->use_vbr)
1215 {
1216 int cbrBytes;
1217 /* Multiply by 12 to make sure the division is exact. */
1218 int frame_rate12 = 12*st->Fs/frame_size;
1219 /* We need to make sure that "int" values always fit in 16 bits. */
1220 cbrBytes = IMIN( (12*st->bitrate_bps/8 + frame_rate12/2)/frame_rate12, max_data_bytes);
1221 st->bitrate_bps = cbrBytes*(opus_int32)frame_rate12*8/12;
1222 /* Make sure we provide at least one byte to avoid failing. */
1223 max_data_bytes = IMAX(1, cbrBytes);
1224 }
1225 if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8
1226 || (frame_rate<50 && (max_data_bytes*frame_rate<300 || st->bitrate_bps < 2400)))
1227 {
1228 /*If the space is too low to do something useful, emit 'PLC' frames.*/
1229 int tocmode = st->mode;
1230 int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth;
1231 int packet_code = 0;
1232 int num_multiframes = 0;
1233
1234 if (tocmode==0)
1235 tocmode = MODE_SILK_ONLY;
1236 if (frame_rate>100)
1237 tocmode = MODE_CELT_ONLY;
1238 /* 40 ms -> 2 x 20 ms if in CELT_ONLY or HYBRID mode */
1239 if (frame_rate==25 && tocmode!=MODE_SILK_ONLY)
1240 {
1241 frame_rate = 50;
1242 packet_code = 1;
1243 }
1244
1245 /* >= 60 ms frames */
1246 if (frame_rate<=16)
1247 {
1248 /* 1 x 60 ms, 2 x 40 ms, 2 x 60 ms */
1249 if (out_data_bytes==1 || (tocmode==MODE_SILK_ONLY && frame_rate!=10))
1250 {
1251 tocmode = MODE_SILK_ONLY;
1252
1253 packet_code = frame_rate <= 12;
1254 frame_rate = frame_rate == 12 ? 25 : 16;
1255 }
1256 else
1257 {
1258 num_multiframes = 50/frame_rate;
1259 frame_rate = 50;
1260 packet_code = 3;
1261 }
1262 }
1263
1264 if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND)
1265 bw=OPUS_BANDWIDTH_WIDEBAND;
1266 else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND)
1267 bw=OPUS_BANDWIDTH_NARROWBAND;
1268 else if (tocmode==MODE_HYBRID&&bw<=OPUS_BANDWIDTH_SUPERWIDEBAND)
1269 bw=OPUS_BANDWIDTH_SUPERWIDEBAND;
1270
1271 data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels);
1272 data[0] |= packet_code;
1273
1274 ret = packet_code <= 1 ? 1 : 2;
1275
1276 max_data_bytes = IMAX(max_data_bytes, ret);
1277
1278 if (packet_code==3)
1279 data[1] = num_multiframes;
1280
1281 if (!st->use_vbr)
1282 {
1283 ret = opus_packet_pad(data, ret, max_data_bytes);
1284 if (ret == OPUS_OK)
1285 ret = max_data_bytes;
1286 else
1287 ret = OPUS_INTERNAL_ERROR;
1288 }
1289 RESTORE_STACK;
1290 return ret;
1291 }
1292 max_rate = frame_rate*max_data_bytes*8;
1293
1294 /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */
1295 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->channels, st->Fs/frame_size,
1296 st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1297
1298 if (st->signal_type == OPUS_SIGNAL_VOICE)
1299 voice_est = 127;
1300 else if (st->signal_type == OPUS_SIGNAL_MUSIC)
1301 voice_est = 0;
1302 else if (st->voice_ratio >= 0)
1303 {
1304 voice_est = st->voice_ratio*327>>8;
1305 /* For AUDIO, never be more than 90% confident of having speech */
1306 if (st->application == OPUS_APPLICATION_AUDIO)
1307 voice_est = IMIN(voice_est, 115);
1308 } else if (st->application == OPUS_APPLICATION_VOIP)
1309 voice_est = 115;
1310 else
1311 voice_est = 48;
1312
1313 if (st->force_channels!=OPUS_AUTO && st->channels == 2)
1314 {
1315 st->stream_channels = st->force_channels;
1316 } else {
1317 #ifdef FUZZING
1318 (void)stereo_music_threshold;
1319 (void)stereo_voice_threshold;
1320 /* Random mono/stereo decision */
1321 if (st->channels == 2 && (rand()&0x1F)==0)
1322 st->stream_channels = 3-st->stream_channels;
1323 #else
1324 /* Rate-dependent mono-stereo decision */
1325 if (st->channels == 2)
1326 {
1327 opus_int32 stereo_threshold;
1328 stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14);
1329 if (st->stream_channels == 2)
1330 stereo_threshold -= 1000;
1331 else
1332 stereo_threshold += 1000;
1333 st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
1334 } else {
1335 st->stream_channels = st->channels;
1336 }
1337 #endif
1338 }
1339 /* Update equivalent rate for channels decision. */
1340 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1341 st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1342
1343 /* Allow SILK DTX if DTX is enabled but the generalized DTX cannot be used,
1344 e.g. because of the complexity setting or sample rate. */
1345 #ifndef DISABLE_FLOAT_API
1346 st->silk_mode.useDTX = st->use_dtx && !(analysis_info.valid || is_silence);
1347 #else
1348 st->silk_mode.useDTX = st->use_dtx;
1349 #endif
1350
1351 /* Mode selection depending on application and signal type */
1352 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1353 {
1354 st->mode = MODE_CELT_ONLY;
1355 } else if (st->user_forced_mode == OPUS_AUTO)
1356 {
1357 #ifdef FUZZING
1358 (void)stereo_width;
1359 (void)mode_thresholds;
1360 /* Random mode switching */
1361 if ((rand()&0xF)==0)
1362 {
1363 if ((rand()&0x1)==0)
1364 st->mode = MODE_CELT_ONLY;
1365 else
1366 st->mode = MODE_SILK_ONLY;
1367 } else {
1368 if (st->prev_mode==MODE_CELT_ONLY)
1369 st->mode = MODE_CELT_ONLY;
1370 else
1371 st->mode = MODE_SILK_ONLY;
1372 }
1373 #else
1374 opus_int32 mode_voice, mode_music;
1375 opus_int32 threshold;
1376
1377 /* Interpolate based on stereo width */
1378 mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[0][0])
1379 + MULT16_32_Q15(stereo_width,mode_thresholds[1][0]));
1380 mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[1][1])
1381 + MULT16_32_Q15(stereo_width,mode_thresholds[1][1]));
1382 /* Interpolate based on speech/music probability */
1383 threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14);
1384 /* Bias towards SILK for VoIP because of some useful features */
1385 if (st->application == OPUS_APPLICATION_VOIP)
1386 threshold += 8000;
1387
1388 /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/
1389 /* Hysteresis */
1390 if (st->prev_mode == MODE_CELT_ONLY)
1391 threshold -= 4000;
1392 else if (st->prev_mode>0)
1393 threshold += 4000;
1394
1395 st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY;
1396
1397 /* When FEC is enabled and there's enough packet loss, use SILK.
1398 Unless the FEC is set to 2, in which case we don't switch to SILK if we're confident we have music. */
1399 if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (128-voice_est)>>4 && (st->fec_config != 2 || voice_est > 25))
1400 st->mode = MODE_SILK_ONLY;
1401 /* When encoding voice and DTX is enabled but the generalized DTX cannot be used,
1402 use SILK in order to make use of its DTX. */
1403 if (st->silk_mode.useDTX && voice_est > 100)
1404 st->mode = MODE_SILK_ONLY;
1405 #endif
1406
1407 /* If max_data_bytes represents less than 6 kb/s, switch to CELT-only mode */
1408 if (max_data_bytes < (frame_rate > 50 ? 9000 : 6000)*frame_size / (st->Fs * 8))
1409 st->mode = MODE_CELT_ONLY;
1410 } else {
1411 st->mode = st->user_forced_mode;
1412 }
1413
1414 /* Override the chosen mode to make sure we meet the requested frame size */
1415 if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100)
1416 st->mode = MODE_CELT_ONLY;
1417 if (st->lfe)
1418 st->mode = MODE_CELT_ONLY;
1419
1420 if (st->prev_mode > 0 &&
1421 ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ||
1422 (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)))
1423 {
1424 redundancy = 1;
1425 celt_to_silk = (st->mode != MODE_CELT_ONLY);
1426 if (!celt_to_silk)
1427 {
1428 /* Switch to SILK/hybrid if frame size is 10 ms or more*/
1429 if (frame_size >= st->Fs/100)
1430 {
1431 st->mode = st->prev_mode;
1432 to_celt = 1;
1433 } else {
1434 redundancy=0;
1435 }
1436 }
1437 }
1438
1439 /* When encoding multiframes, we can ask for a switch to CELT only in the last frame. This switch
1440 * is processed above as the requested mode shouldn't interrupt stereo->mono transition. */
1441 if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0
1442 && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)
1443 {
1444 /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */
1445 st->silk_mode.toMono = 1;
1446 st->stream_channels = 2;
1447 } else {
1448 st->silk_mode.toMono = 0;
1449 }
1450
1451 /* Update equivalent rate with mode decision. */
1452 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1453 st->use_vbr, st->mode, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1454
1455 if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
1456 {
1457 silk_EncControlStruct dummy;
1458 silk_InitEncoder( silk_enc, st->arch, &dummy);
1459 prefill=1;
1460 }
1461
1462 /* Automatic (rate-dependent) bandwidth selection */
1463 if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch)
1464 {
1465 const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds;
1466 opus_int32 bandwidth_thresholds[8];
1467 int bandwidth = OPUS_BANDWIDTH_FULLBAND;
1468
1469 if (st->channels==2 && st->force_channels!=1)
1470 {
1471 voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds;
1472 music_bandwidth_thresholds = stereo_music_bandwidth_thresholds;
1473 } else {
1474 voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds;
1475 music_bandwidth_thresholds = mono_music_bandwidth_thresholds;
1476 }
1477 /* Interpolate bandwidth thresholds depending on voice estimation */
1478 for (i=0;i<8;i++)
1479 {
1480 bandwidth_thresholds[i] = music_bandwidth_thresholds[i]
1481 + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14);
1482 }
1483 do {
1484 int threshold, hysteresis;
1485 threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)];
1486 hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1];
1487 if (!st->first)
1488 {
1489 if (st->auto_bandwidth >= bandwidth)
1490 threshold -= hysteresis;
1491 else
1492 threshold += hysteresis;
1493 }
1494 if (equiv_rate >= threshold)
1495 break;
1496 } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND);
1497 /* We don't use mediumband anymore, except when explicitly requested or during
1498 mode transitions. */
1499 if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1500 bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1501 st->bandwidth = st->auto_bandwidth = bandwidth;
1502 /* Prevents any transition to SWB/FB until the SILK layer has fully
1503 switched to WB mode and turned the variable LP filter off */
1504 if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1505 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1506 }
1507
1508 if (st->bandwidth>st->max_bandwidth)
1509 st->bandwidth = st->max_bandwidth;
1510
1511 if (st->user_bandwidth != OPUS_AUTO)
1512 st->bandwidth = st->user_bandwidth;
1513
1514 /* This prevents us from using hybrid at unsafe CBR/max rates */
1515 if (st->mode != MODE_CELT_ONLY && max_rate < 15000)
1516 {
1517 st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND);
1518 }
1519
1520 /* Prevents Opus from wasting bits on frequencies that are above
1521 the Nyquist rate of the input signal */
1522 if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND)
1523 st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1524 if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1525 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1526 if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND)
1527 st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1528 if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
1529 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1530 #ifndef DISABLE_FLOAT_API
1531 /* Use detected bandwidth to reduce the encoded bandwidth. */
1532 if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO)
1533 {
1534 int min_detected_bandwidth;
1535 /* Makes bandwidth detection more conservative just in case the detector
1536 gets it wrong when we could have coded a high bandwidth transparently.
1537 When operating in SILK/hybrid mode, we don't go below wideband to avoid
1538 more complicated switches that require redundancy. */
1539 if (equiv_rate <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1540 min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1541 else if (equiv_rate <= 24000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1542 min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1543 else if (equiv_rate <= 30000*st->stream_channels)
1544 min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1545 else if (equiv_rate <= 44000*st->stream_channels)
1546 min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1547 else
1548 min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1549
1550 st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwidth);
1551 st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth);
1552 }
1553 #endif
1554 st->silk_mode.LBRR_coded = decide_fec(st->silk_mode.useInBandFEC, st->silk_mode.packetLossPercentage,
1555 st->silk_mode.LBRR_coded, st->mode, &st->bandwidth, equiv_rate);
1556 celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth));
1557
1558 /* CELT mode doesn't support mediumband, use wideband instead */
1559 if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1560 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1561 if (st->lfe)
1562 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1563
1564 curr_bandwidth = st->bandwidth;
1565
1566 /* Chooses the appropriate mode for speech
1567 *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */
1568 if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1569 st->mode = MODE_HYBRID;
1570 if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND)
1571 st->mode = MODE_SILK_ONLY;
1572
1573 /* Can't support higher than >60 ms frames, and >20 ms when in Hybrid or CELT-only modes */
1574 if ((frame_size > st->Fs/50 && (st->mode != MODE_SILK_ONLY)) || frame_size > 3*st->Fs/50)
1575 {
1576 int enc_frame_size;
1577 int nb_frames;
1578
1579 if (st->mode == MODE_SILK_ONLY)
1580 {
1581 if (frame_size == 2*st->Fs/25) /* 80 ms -> 2x 40 ms */
1582 enc_frame_size = st->Fs/25;
1583 else if (frame_size == 3*st->Fs/25) /* 120 ms -> 2x 60 ms */
1584 enc_frame_size = 3*st->Fs/50;
1585 else /* 100 ms -> 5x 20 ms */
1586 enc_frame_size = st->Fs/50;
1587 }
1588 else
1589 enc_frame_size = st->Fs/50;
1590
1591 nb_frames = frame_size/enc_frame_size;
1592
1593 #ifndef DISABLE_FLOAT_API
1594 if (analysis_read_pos_bak!= -1)
1595 {
1596 st->analysis.read_pos = analysis_read_pos_bak;
1597 st->analysis.read_subframe = analysis_read_subframe_bak;
1598 }
1599 #endif
1600
1601 ret = encode_multiframe_packet(st, pcm, nb_frames, enc_frame_size, data,
1602 out_data_bytes, to_celt, lsb_depth, float_api);
1603
1604 RESTORE_STACK;
1605 return ret;
1606 }
1607
1608 /* For the first frame at a new SILK bandwidth */
1609 if (st->silk_bw_switch)
1610 {
1611 redundancy = 1;
1612 celt_to_silk = 1;
1613 st->silk_bw_switch = 0;
1614 /* Do a prefill without reseting the sampling rate control. */
1615 prefill=2;
1616 }
1617
1618 /* If we decided to go with CELT, make sure redundancy is off, no matter what
1619 we decided earlier. */
1620 if (st->mode == MODE_CELT_ONLY)
1621 redundancy = 0;
1622
1623 if (redundancy)
1624 {
1625 redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
1626 if (redundancy_bytes == 0)
1627 redundancy = 0;
1628 }
1629
1630 /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */
1631 bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1;
1632
1633 data += 1;
1634
1635 ec_enc_init(&enc, data, max_data_bytes-1);
1636
1637 ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_val16);
1638 OPUS_COPY(pcm_buf, &st->delay_buffer[(st->encoder_buffer-total_buffer)*st->channels], total_buffer*st->channels);
1639
1640 if (st->mode == MODE_CELT_ONLY)
1641 hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
1642 else
1643 hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15;
1644
1645 st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15,
1646 hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) );
1647
1648 /* convert from log scale to Hertz */
1649 cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
1650
1651 if (st->application == OPUS_APPLICATION_VOIP)
1652 {
1653 hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs, st->arch);
1654 } else {
1655 dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
1656 }
1657 #ifndef FIXED_POINT
1658 if (float_api)
1659 {
1660 opus_val32 sum;
1661 sum = celt_inner_prod(&pcm_buf[total_buffer*st->channels], &pcm_buf[total_buffer*st->channels], frame_size*st->channels, st->arch);
1662 /* This should filter out both NaNs and ridiculous signals that could
1663 cause NaNs further down. */
1664 if (!(sum < 1e9f) || celt_isnan(sum))
1665 {
1666 OPUS_CLEAR(&pcm_buf[total_buffer*st->channels], frame_size*st->channels);
1667 st->hp_mem[0] = st->hp_mem[1] = st->hp_mem[2] = st->hp_mem[3] = 0;
1668 }
1669 }
1670 #endif
1671
1672
1673 /* SILK processing */
1674 HB_gain = Q15ONE;
1675 if (st->mode != MODE_CELT_ONLY)
1676 {
1677 opus_int32 total_bitRate, celt_rate;
1678 #ifdef FIXED_POINT
1679 const opus_int16 *pcm_silk;
1680 #else
1681 VARDECL(opus_int16, pcm_silk);
1682 ALLOC(pcm_silk, st->channels*frame_size, opus_int16);
1683 #endif
1684
1685 /* Distribute bits between SILK and CELT */
1686 total_bitRate = 8 * bytes_target * frame_rate;
1687 if( st->mode == MODE_HYBRID ) {
1688 /* Base rate for SILK */
1689 st->silk_mode.bitRate = compute_silk_rate_for_hybrid(total_bitRate,
1690 curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
1691 st->stream_channels);
1692 if (!st->energy_masking)
1693 {
1694 /* Increasingly attenuate high band when it gets allocated fewer bits */
1695 celt_rate = total_bitRate - st->silk_mode.bitRate;
1696 HB_gain = Q15ONE - SHR32(celt_exp2(-celt_rate * QCONST16(1.f/1024, 10)), 1);
1697 }
1698 } else {
1699 /* SILK gets all bits */
1700 st->silk_mode.bitRate = total_bitRate;
1701 }
1702
1703 /* Surround masking for SILK */
1704 if (st->energy_masking && st->use_vbr && !st->lfe)
1705 {
1706 opus_val32 mask_sum=0;
1707 opus_val16 masking_depth;
1708 opus_int32 rate_offset;
1709 int c;
1710 int end = 17;
1711 opus_int16 srate = 16000;
1712 if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
1713 {
1714 end = 13;
1715 srate = 8000;
1716 } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1717 {
1718 end = 15;
1719 srate = 12000;
1720 }
1721 for (c=0;c<st->channels;c++)
1722 {
1723 for(i=0;i<end;i++)
1724 {
1725 opus_val16 mask;
1726 mask = MAX16(MIN16(st->energy_masking[21*c+i],
1727 QCONST16(.5f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT));
1728 if (mask > 0)
1729 mask = HALF16(mask);
1730 mask_sum += mask;
1731 }
1732 }
1733 /* Conservative rate reduction, we cut the masking in half */
1734 masking_depth = mask_sum / end*st->channels;
1735 masking_depth += QCONST16(.2f, DB_SHIFT);
1736 rate_offset = (opus_int32)PSHR32(MULT16_16(srate, masking_depth), DB_SHIFT);
1737 rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3);
1738 /* Split the rate change between the SILK and CELT part for hybrid. */
1739 if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPUS_BANDWIDTH_FULLBAND)
1740 st->silk_mode.bitRate += 3*rate_offset/5;
1741 else
1742 st->silk_mode.bitRate += rate_offset;
1743 }
1744
1745 st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
1746 st->silk_mode.nChannelsAPI = st->channels;
1747 st->silk_mode.nChannelsInternal = st->stream_channels;
1748 if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
1749 st->silk_mode.desiredInternalSampleRate = 8000;
1750 } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
1751 st->silk_mode.desiredInternalSampleRate = 12000;
1752 } else {
1753 celt_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND );
1754 st->silk_mode.desiredInternalSampleRate = 16000;
1755 }
1756 if( st->mode == MODE_HYBRID ) {
1757 /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
1758 st->silk_mode.minInternalSampleRate = 16000;
1759 } else {
1760 st->silk_mode.minInternalSampleRate = 8000;
1761 }
1762
1763 st->silk_mode.maxInternalSampleRate = 16000;
1764 if (st->mode == MODE_SILK_ONLY)
1765 {
1766 opus_int32 effective_max_rate = max_rate;
1767 if (frame_rate > 50)
1768 effective_max_rate = effective_max_rate*2/3;
1769 if (effective_max_rate < 8000)
1770 {
1771 st->silk_mode.maxInternalSampleRate = 12000;
1772 st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate);
1773 }
1774 if (effective_max_rate < 7000)
1775 {
1776 st->silk_mode.maxInternalSampleRate = 8000;
1777 st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate);
1778 }
1779 }
1780
1781 st->silk_mode.useCBR = !st->use_vbr;
1782
1783 /* Call SILK encoder for the low band */
1784
1785 /* Max bits for SILK, counting ToC, redundancy bytes, and optionally redundancy. */
1786 st->silk_mode.maxBits = (max_data_bytes-1)*8;
1787 if (redundancy && redundancy_bytes >= 2)
1788 {
1789 /* Counting 1 bit for redundancy position and 20 bits for flag+size (only for hybrid). */
1790 st->silk_mode.maxBits -= redundancy_bytes*8 + 1;
1791 if (st->mode == MODE_HYBRID)
1792 st->silk_mode.maxBits -= 20;
1793 }
1794 if (st->silk_mode.useCBR)
1795 {
1796 if (st->mode == MODE_HYBRID)
1797 {
1798 st->silk_mode.maxBits = IMIN(st->silk_mode.maxBits, st->silk_mode.bitRate * frame_size / st->Fs);
1799 }
1800 } else {
1801 /* Constrained VBR. */
1802 if (st->mode == MODE_HYBRID)
1803 {
1804 /* Compute SILK bitrate corresponding to the max total bits available */
1805 opus_int32 maxBitRate = compute_silk_rate_for_hybrid(st->silk_mode.maxBits*st->Fs / frame_size,
1806 curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
1807 st->stream_channels);
1808 st->silk_mode.maxBits = maxBitRate * frame_size / st->Fs;
1809 }
1810 }
1811
1812 if (prefill)
1813 {
1814 opus_int32 zero=0;
1815 int prefill_offset;
1816 /* Use a smooth onset for the SILK prefill to avoid the encoder trying to encode
1817 a discontinuity. The exact location is what we need to avoid leaving any "gap"
1818 in the audio when mixing with the redundant CELT frame. Here we can afford to
1819 overwrite st->delay_buffer because the only thing that uses it before it gets
1820 rewritten is tmp_prefill[] and even then only the part after the ramp really
1821 gets used (rather than sent to the encoder and discarded) */
1822 prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensation-st->Fs/400);
1823 gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_offset,
1824 0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_mode->window, st->Fs);
1825 OPUS_CLEAR(st->delay_buffer, prefill_offset);
1826 #ifdef FIXED_POINT
1827 pcm_silk = st->delay_buffer;
1828 #else
1829 for (i=0;i<st->encoder_buffer*st->channels;i++)
1830 pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]);
1831 #endif
1832 silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, prefill, activity );
1833 /* Prevent a second switch in the real encode call. */
1834 st->silk_mode.opusCanSwitch = 0;
1835 }
1836
1837 #ifdef FIXED_POINT
1838 pcm_silk = pcm_buf+total_buffer*st->channels;
1839 #else
1840 for (i=0;i<frame_size*st->channels;i++)
1841 pcm_silk[i] = FLOAT2INT16(pcm_buf[total_buffer*st->channels + i]);
1842 #endif
1843 ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0, activity );
1844 if( ret ) {
1845 /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
1846 /* Handle error */
1847 RESTORE_STACK;
1848 return OPUS_INTERNAL_ERROR;
1849 }
1850
1851 /* Extract SILK internal bandwidth for signaling in first byte */
1852 if( st->mode == MODE_SILK_ONLY ) {
1853 if( st->silk_mode.internalSampleRate == 8000 ) {
1854 curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1855 } else if( st->silk_mode.internalSampleRate == 12000 ) {
1856 curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1857 } else if( st->silk_mode.internalSampleRate == 16000 ) {
1858 curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1859 }
1860 } else {
1861 celt_assert( st->silk_mode.internalSampleRate == 16000 );
1862 }
1863
1864 st->silk_mode.opusCanSwitch = st->silk_mode.switchReady && !st->nonfinal_frame;
1865
1866 if (nBytes==0)
1867 {
1868 st->rangeFinal = 0;
1869 data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
1870 RESTORE_STACK;
1871 return 1;
1872 }
1873
1874 /* FIXME: How do we allocate the redundancy for CBR? */
1875 if (st->silk_mode.opusCanSwitch)
1876 {
1877 redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
1878 redundancy = (redundancy_bytes != 0);
1879 celt_to_silk = 0;
1880 st->silk_bw_switch = 1;
1881 }
1882 }
1883
1884 /* CELT processing */
1885 {
1886 int endband=21;
1887
1888 switch(curr_bandwidth)
1889 {
1890 case OPUS_BANDWIDTH_NARROWBAND:
1891 endband = 13;
1892 break;
1893 case OPUS_BANDWIDTH_MEDIUMBAND:
1894 case OPUS_BANDWIDTH_WIDEBAND:
1895 endband = 17;
1896 break;
1897 case OPUS_BANDWIDTH_SUPERWIDEBAND:
1898 endband = 19;
1899 break;
1900 case OPUS_BANDWIDTH_FULLBAND:
1901 endband = 21;
1902 break;
1903 }
1904 celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband));
1905 celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels));
1906 }
1907 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
1908 if (st->mode != MODE_SILK_ONLY)
1909 {
1910 opus_val32 celt_pred=2;
1911 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
1912 /* We may still decide to disable prediction later */
1913 if (st->silk_mode.reducedDependency)
1914 celt_pred = 0;
1915 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred));
1916
1917 if (st->mode == MODE_HYBRID)
1918 {
1919 if( st->use_vbr ) {
1920 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps-st->silk_mode.bitRate));
1921 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0));
1922 }
1923 } else {
1924 if (st->use_vbr)
1925 {
1926 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
1927 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint));
1928 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps));
1929 }
1930 }
1931 }
1932
1933 ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16);
1934 if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0)
1935 {
1936 OPUS_COPY(tmp_prefill, &st->delay_buffer[(st->encoder_buffer-total_buffer-st->Fs/400)*st->channels], st->channels*st->Fs/400);
1937 }
1938
1939 if (st->channels*(st->encoder_buffer-(frame_size+total_buffer)) > 0)
1940 {
1941 OPUS_MOVE(st->delay_buffer, &st->delay_buffer[st->channels*frame_size], st->channels*(st->encoder_buffer-frame_size-total_buffer));
1942 OPUS_COPY(&st->delay_buffer[st->channels*(st->encoder_buffer-frame_size-total_buffer)],
1943 &pcm_buf[0],
1944 (frame_size+total_buffer)*st->channels);
1945 } else {
1946 OPUS_COPY(st->delay_buffer, &pcm_buf[(frame_size+total_buffer-st->encoder_buffer)*st->channels], st->encoder_buffer*st->channels);
1947 }
1948 /* gain_fade() and stereo_fade() need to be after the buffer copying
1949 because we don't want any of this to affect the SILK part */
1950 if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) {
1951 gain_fade(pcm_buf, pcm_buf,
1952 st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs);
1953 }
1954 st->prev_HB_gain = HB_gain;
1955 if (st->mode != MODE_HYBRID || st->stream_channels==1)
1956 {
1957 if (equiv_rate > 32000)
1958 st->silk_mode.stereoWidth_Q14 = 16384;
1959 else if (equiv_rate < 16000)
1960 st->silk_mode.stereoWidth_Q14 = 0;
1961 else
1962 st->silk_mode.stereoWidth_Q14 = 16384 - 2048*(opus_int32)(32000-equiv_rate)/(equiv_rate-14000);
1963 }
1964 if( !st->energy_masking && st->channels == 2 ) {
1965 /* Apply stereo width reduction (at low bitrates) */
1966 if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) {
1967 opus_val16 g1, g2;
1968 g1 = st->hybrid_stereo_width_Q14;
1969 g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
1970 #ifdef FIXED_POINT
1971 g1 = g1==16384 ? Q15ONE : SHL16(g1,1);
1972 g2 = g2==16384 ? Q15ONE : SHL16(g2,1);
1973 #else
1974 g1 *= (1.f/16384);
1975 g2 *= (1.f/16384);
1976 #endif
1977 stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
1978 frame_size, st->channels, celt_mode->window, st->Fs);
1979 st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
1980 }
1981 }
1982
1983 if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) <= 8*(max_data_bytes-1))
1984 {
1985 /* For SILK mode, the redundancy is inferred from the length */
1986 if (st->mode == MODE_HYBRID)
1987 ec_enc_bit_logp(&enc, redundancy, 12);
1988 if (redundancy)
1989 {
1990 int max_redundancy;
1991 ec_enc_bit_logp(&enc, celt_to_silk, 1);
1992 if (st->mode == MODE_HYBRID)
1993 {
1994 /* Reserve the 8 bits needed for the redundancy length,
1995 and at least a few bits for CELT if possible */
1996 max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+8+3+7)>>3);
1997 }
1998 else
1999 max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3);
2000 /* Target the same bit-rate for redundancy as for the rest,
2001 up to a max of 257 bytes */
2002 redundancy_bytes = IMIN(max_redundancy, redundancy_bytes);
2003 redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes));
2004 if (st->mode == MODE_HYBRID)
2005 ec_enc_uint(&enc, redundancy_bytes-2, 256);
2006 }
2007 } else {
2008 redundancy = 0;
2009 }
2010
2011 if (!redundancy)
2012 {
2013 st->silk_bw_switch = 0;
2014 redundancy_bytes = 0;
2015 }
2016 if (st->mode != MODE_CELT_ONLY)start_band=17;
2017
2018 if (st->mode == MODE_SILK_ONLY)
2019 {
2020 ret = (ec_tell(&enc)+7)>>3;
2021 ec_enc_done(&enc);
2022 nb_compr_bytes = ret;
2023 } else {
2024 nb_compr_bytes = (max_data_bytes-1)-redundancy_bytes;
2025 ec_enc_shrink(&enc, nb_compr_bytes);
2026 }
2027
2028 #ifndef DISABLE_FLOAT_API
2029 if (redundancy || st->mode != MODE_SILK_ONLY)
2030 celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(&analysis_info));
2031 #endif
2032 if (st->mode == MODE_HYBRID) {
2033 SILKInfo info;
2034 info.signalType = st->silk_mode.signalType;
2035 info.offset = st->silk_mode.offset;
2036 celt_encoder_ctl(celt_enc, CELT_SET_SILK_INFO(&info));
2037 }
2038
2039 /* 5 ms redundant frame for CELT->SILK */
2040 if (redundancy && celt_to_silk)
2041 {
2042 int err;
2043 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2044 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2045 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2046 err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL);
2047 if (err < 0)
2048 {
2049 RESTORE_STACK;
2050 return OPUS_INTERNAL_ERROR;
2051 }
2052 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2053 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2054 }
2055
2056 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band));
2057
2058 if (st->mode != MODE_SILK_ONLY)
2059 {
2060 if (st->mode != st->prev_mode && st->prev_mode > 0)
2061 {
2062 unsigned char dummy[2];
2063 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2064
2065 /* Prefilling */
2066 celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL);
2067 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2068 }
2069 /* If false, we already busted the budget and we'll end up with a "PLC frame" */
2070 if (ec_tell(&enc) <= 8*nb_compr_bytes)
2071 {
2072 /* Set the bitrate again if it was overridden in the redundancy code above*/
2073 if (redundancy && celt_to_silk && st->mode==MODE_HYBRID && st->use_vbr)
2074 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps-st->silk_mode.bitRate));
2075 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(st->use_vbr));
2076 ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc);
2077 if (ret < 0)
2078 {
2079 RESTORE_STACK;
2080 return OPUS_INTERNAL_ERROR;
2081 }
2082 /* Put CELT->SILK redundancy data in the right place. */
2083 if (redundancy && celt_to_silk && st->mode==MODE_HYBRID && st->use_vbr)
2084 {
2085 OPUS_MOVE(data+ret, data+nb_compr_bytes, redundancy_bytes);
2086 nb_compr_bytes = nb_compr_bytes+redundancy_bytes;
2087 }
2088 }
2089 }
2090
2091 /* 5 ms redundant frame for SILK->CELT */
2092 if (redundancy && !celt_to_silk)
2093 {
2094 int err;
2095 unsigned char dummy[2];
2096 int N2, N4;
2097 N2 = st->Fs/200;
2098 N4 = st->Fs/400;
2099
2100 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2101 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2102 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2103 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2104 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2105
2106 if (st->mode == MODE_HYBRID)
2107 {
2108 /* Shrink packet to what the encoder actually used. */
2109 nb_compr_bytes = ret;
2110 ec_enc_shrink(&enc, nb_compr_bytes);
2111 }
2112 /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */
2113 celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL);
2114
2115 err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL);
2116 if (err < 0)
2117 {
2118 RESTORE_STACK;
2119 return OPUS_INTERNAL_ERROR;
2120 }
2121 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2122 }
2123
2124
2125
2126 /* Signalling the mode in the first byte */
2127 data--;
2128 data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2129
2130 st->rangeFinal = enc.rng ^ redundant_rng;
2131
2132 if (to_celt)
2133 st->prev_mode = MODE_CELT_ONLY;
2134 else
2135 st->prev_mode = st->mode;
2136 st->prev_channels = st->stream_channels;
2137 st->prev_framesize = frame_size;
2138
2139 st->first = 0;
2140
2141 /* DTX decision */
2142 #ifndef DISABLE_FLOAT_API
2143 if (st->use_dtx && (analysis_info.valid || is_silence))
2144 {
2145 if (decide_dtx_mode(activity, &st->nb_no_activity_ms_Q1, 2*1000*frame_size/st->Fs))
2146 {
2147 st->rangeFinal = 0;
2148 data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2149 RESTORE_STACK;
2150 return 1;
2151 }
2152 } else {
2153 st->nb_no_activity_ms_Q1 = 0;
2154 }
2155 #endif
2156
2157 /* In the unlikely case that the SILK encoder busted its target, tell
2158 the decoder to call the PLC */
2159 if (ec_tell(&enc) > (max_data_bytes-1)*8)
2160 {
2161 if (max_data_bytes < 2)
2162 {
2163 RESTORE_STACK;
2164 return OPUS_BUFFER_TOO_SMALL;
2165 }
2166 data[1] = 0;
2167 ret = 1;
2168 st->rangeFinal = 0;
2169 } else if (st->mode==MODE_SILK_ONLY&&!redundancy)
2170 {
2171 /*When in LPC only mode it's perfectly
2172 reasonable to strip off trailing zero bytes as
2173 the required range decoder behavior is to
2174 fill these in. This can't be done when the MDCT
2175 modes are used because the decoder needs to know
2176 the actual length for allocation purposes.*/
2177 while(ret>2&&data[ret]==0)ret--;
2178 }
2179 /* Count ToC and redundancy */
2180 ret += 1+redundancy_bytes;
2181 if (!st->use_vbr)
2182 {
2183 if (opus_packet_pad(data, ret, max_data_bytes) != OPUS_OK)
2184 {
2185 RESTORE_STACK;
2186 return OPUS_INTERNAL_ERROR;
2187 }
2188 ret = max_data_bytes;
2189 }
2190 RESTORE_STACK;
2191 return ret;
2192 }
2193
2194 #ifdef FIXED_POINT
2195
2196 #ifndef DISABLE_FLOAT_API
opus_encode_float(OpusEncoder * st,const float * pcm,int analysis_frame_size,unsigned char * data,opus_int32 max_data_bytes)2197 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
2198 unsigned char *data, opus_int32 max_data_bytes)
2199 {
2200 int i, ret;
2201 int frame_size;
2202 VARDECL(opus_int16, in);
2203 ALLOC_STACK;
2204
2205 frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2206 if (frame_size <= 0)
2207 {
2208 RESTORE_STACK;
2209 return OPUS_BAD_ARG;
2210 }
2211 ALLOC(in, frame_size*st->channels, opus_int16);
2212
2213 for (i=0;i<frame_size*st->channels;i++)
2214 in[i] = FLOAT2INT16(pcm[i]);
2215 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
2216 pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1);
2217 RESTORE_STACK;
2218 return ret;
2219 }
2220 #endif
2221
opus_encode(OpusEncoder * st,const opus_int16 * pcm,int analysis_frame_size,unsigned char * data,opus_int32 out_data_bytes)2222 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
2223 unsigned char *data, opus_int32 out_data_bytes)
2224 {
2225 int frame_size;
2226 frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2227 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 16,
2228 pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 0);
2229 }
2230
2231 #else
opus_encode(OpusEncoder * st,const opus_int16 * pcm,int analysis_frame_size,unsigned char * data,opus_int32 max_data_bytes)2232 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
2233 unsigned char *data, opus_int32 max_data_bytes)
2234 {
2235 int i, ret;
2236 int frame_size;
2237 VARDECL(float, in);
2238 ALLOC_STACK;
2239
2240 frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2241 if (frame_size <= 0)
2242 {
2243 RESTORE_STACK;
2244 return OPUS_BAD_ARG;
2245 }
2246 ALLOC(in, frame_size*st->channels, float);
2247
2248 for (i=0;i<frame_size*st->channels;i++)
2249 in[i] = (1.0f/32768)*pcm[i];
2250 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
2251 pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 0);
2252 RESTORE_STACK;
2253 return ret;
2254 }
opus_encode_float(OpusEncoder * st,const float * pcm,int analysis_frame_size,unsigned char * data,opus_int32 out_data_bytes)2255 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
2256 unsigned char *data, opus_int32 out_data_bytes)
2257 {
2258 int frame_size;
2259 frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2260 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 24,
2261 pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1);
2262 }
2263 #endif
2264
2265
opus_encoder_ctl(OpusEncoder * st,int request,...)2266 int opus_encoder_ctl(OpusEncoder *st, int request, ...)
2267 {
2268 int ret;
2269 CELTEncoder *celt_enc;
2270 va_list ap;
2271
2272 ret = OPUS_OK;
2273 va_start(ap, request);
2274
2275 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
2276
2277 switch (request)
2278 {
2279 case OPUS_SET_APPLICATION_REQUEST:
2280 {
2281 opus_int32 value = va_arg(ap, opus_int32);
2282 if ( (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_AUDIO
2283 && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2284 || (!st->first && st->application != value))
2285 {
2286 ret = OPUS_BAD_ARG;
2287 break;
2288 }
2289 st->application = value;
2290 #ifndef DISABLE_FLOAT_API
2291 st->analysis.application = value;
2292 #endif
2293 }
2294 break;
2295 case OPUS_GET_APPLICATION_REQUEST:
2296 {
2297 opus_int32 *value = va_arg(ap, opus_int32*);
2298 if (!value)
2299 {
2300 goto bad_arg;
2301 }
2302 *value = st->application;
2303 }
2304 break;
2305 case OPUS_SET_BITRATE_REQUEST:
2306 {
2307 opus_int32 value = va_arg(ap, opus_int32);
2308 if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX)
2309 {
2310 if (value <= 0)
2311 goto bad_arg;
2312 else if (value <= 500)
2313 value = 500;
2314 else if (value > (opus_int32)300000*st->channels)
2315 value = (opus_int32)300000*st->channels;
2316 }
2317 st->user_bitrate_bps = value;
2318 }
2319 break;
2320 case OPUS_GET_BITRATE_REQUEST:
2321 {
2322 opus_int32 *value = va_arg(ap, opus_int32*);
2323 if (!value)
2324 {
2325 goto bad_arg;
2326 }
2327 *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276);
2328 }
2329 break;
2330 case OPUS_SET_FORCE_CHANNELS_REQUEST:
2331 {
2332 opus_int32 value = va_arg(ap, opus_int32);
2333 if((value<1 || value>st->channels) && value != OPUS_AUTO)
2334 {
2335 goto bad_arg;
2336 }
2337 st->force_channels = value;
2338 }
2339 break;
2340 case OPUS_GET_FORCE_CHANNELS_REQUEST:
2341 {
2342 opus_int32 *value = va_arg(ap, opus_int32*);
2343 if (!value)
2344 {
2345 goto bad_arg;
2346 }
2347 *value = st->force_channels;
2348 }
2349 break;
2350 case OPUS_SET_MAX_BANDWIDTH_REQUEST:
2351 {
2352 opus_int32 value = va_arg(ap, opus_int32);
2353 if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND)
2354 {
2355 goto bad_arg;
2356 }
2357 st->max_bandwidth = value;
2358 if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2359 st->silk_mode.maxInternalSampleRate = 8000;
2360 } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2361 st->silk_mode.maxInternalSampleRate = 12000;
2362 } else {
2363 st->silk_mode.maxInternalSampleRate = 16000;
2364 }
2365 }
2366 break;
2367 case OPUS_GET_MAX_BANDWIDTH_REQUEST:
2368 {
2369 opus_int32 *value = va_arg(ap, opus_int32*);
2370 if (!value)
2371 {
2372 goto bad_arg;
2373 }
2374 *value = st->max_bandwidth;
2375 }
2376 break;
2377 case OPUS_SET_BANDWIDTH_REQUEST:
2378 {
2379 opus_int32 value = va_arg(ap, opus_int32);
2380 if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO)
2381 {
2382 goto bad_arg;
2383 }
2384 st->user_bandwidth = value;
2385 if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2386 st->silk_mode.maxInternalSampleRate = 8000;
2387 } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2388 st->silk_mode.maxInternalSampleRate = 12000;
2389 } else {
2390 st->silk_mode.maxInternalSampleRate = 16000;
2391 }
2392 }
2393 break;
2394 case OPUS_GET_BANDWIDTH_REQUEST:
2395 {
2396 opus_int32 *value = va_arg(ap, opus_int32*);
2397 if (!value)
2398 {
2399 goto bad_arg;
2400 }
2401 *value = st->bandwidth;
2402 }
2403 break;
2404 case OPUS_SET_DTX_REQUEST:
2405 {
2406 opus_int32 value = va_arg(ap, opus_int32);
2407 if(value<0 || value>1)
2408 {
2409 goto bad_arg;
2410 }
2411 st->use_dtx = value;
2412 }
2413 break;
2414 case OPUS_GET_DTX_REQUEST:
2415 {
2416 opus_int32 *value = va_arg(ap, opus_int32*);
2417 if (!value)
2418 {
2419 goto bad_arg;
2420 }
2421 *value = st->use_dtx;
2422 }
2423 break;
2424 case OPUS_SET_COMPLEXITY_REQUEST:
2425 {
2426 opus_int32 value = va_arg(ap, opus_int32);
2427 if(value<0 || value>10)
2428 {
2429 goto bad_arg;
2430 }
2431 st->silk_mode.complexity = value;
2432 celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value));
2433 }
2434 break;
2435 case OPUS_GET_COMPLEXITY_REQUEST:
2436 {
2437 opus_int32 *value = va_arg(ap, opus_int32*);
2438 if (!value)
2439 {
2440 goto bad_arg;
2441 }
2442 *value = st->silk_mode.complexity;
2443 }
2444 break;
2445 case OPUS_SET_INBAND_FEC_REQUEST:
2446 {
2447 opus_int32 value = va_arg(ap, opus_int32);
2448 if(value<0 || value>2)
2449 {
2450 goto bad_arg;
2451 }
2452 st->fec_config = value;
2453 st->silk_mode.useInBandFEC = (value != 0);
2454 }
2455 break;
2456 case OPUS_GET_INBAND_FEC_REQUEST:
2457 {
2458 opus_int32 *value = va_arg(ap, opus_int32*);
2459 if (!value)
2460 {
2461 goto bad_arg;
2462 }
2463 *value = st->fec_config;
2464 }
2465 break;
2466 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2467 {
2468 opus_int32 value = va_arg(ap, opus_int32);
2469 if (value < 0 || value > 100)
2470 {
2471 goto bad_arg;
2472 }
2473 st->silk_mode.packetLossPercentage = value;
2474 celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value));
2475 }
2476 break;
2477 case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
2478 {
2479 opus_int32 *value = va_arg(ap, opus_int32*);
2480 if (!value)
2481 {
2482 goto bad_arg;
2483 }
2484 *value = st->silk_mode.packetLossPercentage;
2485 }
2486 break;
2487 case OPUS_SET_VBR_REQUEST:
2488 {
2489 opus_int32 value = va_arg(ap, opus_int32);
2490 if(value<0 || value>1)
2491 {
2492 goto bad_arg;
2493 }
2494 st->use_vbr = value;
2495 st->silk_mode.useCBR = 1-value;
2496 }
2497 break;
2498 case OPUS_GET_VBR_REQUEST:
2499 {
2500 opus_int32 *value = va_arg(ap, opus_int32*);
2501 if (!value)
2502 {
2503 goto bad_arg;
2504 }
2505 *value = st->use_vbr;
2506 }
2507 break;
2508 case OPUS_SET_VOICE_RATIO_REQUEST:
2509 {
2510 opus_int32 value = va_arg(ap, opus_int32);
2511 if (value<-1 || value>100)
2512 {
2513 goto bad_arg;
2514 }
2515 st->voice_ratio = value;
2516 }
2517 break;
2518 case OPUS_GET_VOICE_RATIO_REQUEST:
2519 {
2520 opus_int32 *value = va_arg(ap, opus_int32*);
2521 if (!value)
2522 {
2523 goto bad_arg;
2524 }
2525 *value = st->voice_ratio;
2526 }
2527 break;
2528 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2529 {
2530 opus_int32 value = va_arg(ap, opus_int32);
2531 if(value<0 || value>1)
2532 {
2533 goto bad_arg;
2534 }
2535 st->vbr_constraint = value;
2536 }
2537 break;
2538 case OPUS_GET_VBR_CONSTRAINT_REQUEST:
2539 {
2540 opus_int32 *value = va_arg(ap, opus_int32*);
2541 if (!value)
2542 {
2543 goto bad_arg;
2544 }
2545 *value = st->vbr_constraint;
2546 }
2547 break;
2548 case OPUS_SET_SIGNAL_REQUEST:
2549 {
2550 opus_int32 value = va_arg(ap, opus_int32);
2551 if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNAL_MUSIC)
2552 {
2553 goto bad_arg;
2554 }
2555 st->signal_type = value;
2556 }
2557 break;
2558 case OPUS_GET_SIGNAL_REQUEST:
2559 {
2560 opus_int32 *value = va_arg(ap, opus_int32*);
2561 if (!value)
2562 {
2563 goto bad_arg;
2564 }
2565 *value = st->signal_type;
2566 }
2567 break;
2568 case OPUS_GET_LOOKAHEAD_REQUEST:
2569 {
2570 opus_int32 *value = va_arg(ap, opus_int32*);
2571 if (!value)
2572 {
2573 goto bad_arg;
2574 }
2575 *value = st->Fs/400;
2576 if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2577 *value += st->delay_compensation;
2578 }
2579 break;
2580 case OPUS_GET_SAMPLE_RATE_REQUEST:
2581 {
2582 opus_int32 *value = va_arg(ap, opus_int32*);
2583 if (!value)
2584 {
2585 goto bad_arg;
2586 }
2587 *value = st->Fs;
2588 }
2589 break;
2590 case OPUS_GET_FINAL_RANGE_REQUEST:
2591 {
2592 opus_uint32 *value = va_arg(ap, opus_uint32*);
2593 if (!value)
2594 {
2595 goto bad_arg;
2596 }
2597 *value = st->rangeFinal;
2598 }
2599 break;
2600 case OPUS_SET_LSB_DEPTH_REQUEST:
2601 {
2602 opus_int32 value = va_arg(ap, opus_int32);
2603 if (value<8 || value>24)
2604 {
2605 goto bad_arg;
2606 }
2607 st->lsb_depth=value;
2608 }
2609 break;
2610 case OPUS_GET_LSB_DEPTH_REQUEST:
2611 {
2612 opus_int32 *value = va_arg(ap, opus_int32*);
2613 if (!value)
2614 {
2615 goto bad_arg;
2616 }
2617 *value = st->lsb_depth;
2618 }
2619 break;
2620 case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST:
2621 {
2622 opus_int32 value = va_arg(ap, opus_int32);
2623 if (value != OPUS_FRAMESIZE_ARG && value != OPUS_FRAMESIZE_2_5_MS &&
2624 value != OPUS_FRAMESIZE_5_MS && value != OPUS_FRAMESIZE_10_MS &&
2625 value != OPUS_FRAMESIZE_20_MS && value != OPUS_FRAMESIZE_40_MS &&
2626 value != OPUS_FRAMESIZE_60_MS && value != OPUS_FRAMESIZE_80_MS &&
2627 value != OPUS_FRAMESIZE_100_MS && value != OPUS_FRAMESIZE_120_MS)
2628 {
2629 goto bad_arg;
2630 }
2631 st->variable_duration = value;
2632 }
2633 break;
2634 case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST:
2635 {
2636 opus_int32 *value = va_arg(ap, opus_int32*);
2637 if (!value)
2638 {
2639 goto bad_arg;
2640 }
2641 *value = st->variable_duration;
2642 }
2643 break;
2644 case OPUS_SET_PREDICTION_DISABLED_REQUEST:
2645 {
2646 opus_int32 value = va_arg(ap, opus_int32);
2647 if (value > 1 || value < 0)
2648 goto bad_arg;
2649 st->silk_mode.reducedDependency = value;
2650 }
2651 break;
2652 case OPUS_GET_PREDICTION_DISABLED_REQUEST:
2653 {
2654 opus_int32 *value = va_arg(ap, opus_int32*);
2655 if (!value)
2656 goto bad_arg;
2657 *value = st->silk_mode.reducedDependency;
2658 }
2659 break;
2660 case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
2661 {
2662 opus_int32 value = va_arg(ap, opus_int32);
2663 if(value<0 || value>1)
2664 {
2665 goto bad_arg;
2666 }
2667 celt_encoder_ctl(celt_enc, OPUS_SET_PHASE_INVERSION_DISABLED(value));
2668 }
2669 break;
2670 case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
2671 {
2672 opus_int32 *value = va_arg(ap, opus_int32*);
2673 if (!value)
2674 {
2675 goto bad_arg;
2676 }
2677 celt_encoder_ctl(celt_enc, OPUS_GET_PHASE_INVERSION_DISABLED(value));
2678 }
2679 break;
2680 case OPUS_RESET_STATE:
2681 {
2682 void *silk_enc;
2683 silk_EncControlStruct dummy;
2684 char *start;
2685 silk_enc = (char*)st+st->silk_enc_offset;
2686 #ifndef DISABLE_FLOAT_API
2687 tonality_analysis_reset(&st->analysis);
2688 #endif
2689
2690 start = (char*)&st->OPUS_ENCODER_RESET_START;
2691 OPUS_CLEAR(start, sizeof(OpusEncoder) - (start - (char*)st));
2692
2693 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2694 silk_InitEncoder( silk_enc, st->arch, &dummy );
2695 st->stream_channels = st->channels;
2696 st->hybrid_stereo_width_Q14 = 1 << 14;
2697 st->prev_HB_gain = Q15ONE;
2698 st->first = 1;
2699 st->mode = MODE_HYBRID;
2700 st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
2701 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
2702 }
2703 break;
2704 case OPUS_SET_FORCE_MODE_REQUEST:
2705 {
2706 opus_int32 value = va_arg(ap, opus_int32);
2707 if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO)
2708 {
2709 goto bad_arg;
2710 }
2711 st->user_forced_mode = value;
2712 }
2713 break;
2714 case OPUS_SET_LFE_REQUEST:
2715 {
2716 opus_int32 value = va_arg(ap, opus_int32);
2717 st->lfe = value;
2718 ret = celt_encoder_ctl(celt_enc, OPUS_SET_LFE(value));
2719 }
2720 break;
2721 case OPUS_SET_ENERGY_MASK_REQUEST:
2722 {
2723 opus_val16 *value = va_arg(ap, opus_val16*);
2724 st->energy_masking = value;
2725 ret = celt_encoder_ctl(celt_enc, OPUS_SET_ENERGY_MASK(value));
2726 }
2727 break;
2728 case OPUS_GET_IN_DTX_REQUEST:
2729 {
2730 opus_int32 *value = va_arg(ap, opus_int32*);
2731 if (!value)
2732 {
2733 goto bad_arg;
2734 }
2735 if (st->silk_mode.useDTX && (st->prev_mode == MODE_SILK_ONLY || st->prev_mode == MODE_HYBRID)) {
2736 /* DTX determined by Silk. */
2737 silk_encoder *silk_enc = (silk_encoder*)(void *)((char*)st+st->silk_enc_offset);
2738 *value = silk_enc->state_Fxx[0].sCmn.noSpeechCounter >= NB_SPEECH_FRAMES_BEFORE_DTX;
2739 /* Stereo: check second channel unless only the middle channel was encoded. */
2740 if(*value == 1 && st->silk_mode.nChannelsInternal == 2 && silk_enc->prev_decode_only_middle == 0) {
2741 *value = silk_enc->state_Fxx[1].sCmn.noSpeechCounter >= NB_SPEECH_FRAMES_BEFORE_DTX;
2742 }
2743 }
2744 #ifndef DISABLE_FLOAT_API
2745 else if (st->use_dtx) {
2746 /* DTX determined by Opus. */
2747 *value = st->nb_no_activity_ms_Q1 >= NB_SPEECH_FRAMES_BEFORE_DTX*20*2;
2748 }
2749 #endif
2750 else {
2751 *value = 0;
2752 }
2753 }
2754 break;
2755 case CELT_GET_MODE_REQUEST:
2756 {
2757 const CELTMode ** value = va_arg(ap, const CELTMode**);
2758 if (!value)
2759 {
2760 goto bad_arg;
2761 }
2762 ret = celt_encoder_ctl(celt_enc, CELT_GET_MODE(value));
2763 }
2764 break;
2765 default:
2766 /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/
2767 ret = OPUS_UNIMPLEMENTED;
2768 break;
2769 }
2770 va_end(ap);
2771 return ret;
2772 bad_arg:
2773 va_end(ap);
2774 return OPUS_BAD_ARG;
2775 }
2776
opus_encoder_destroy(OpusEncoder * st)2777 void opus_encoder_destroy(OpusEncoder *st)
2778 {
2779 opus_free(st);
2780 }
2781