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