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 static 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(float activity_probability,int * nb_no_activity_frames,opus_val32 peak_signal_energy,const opus_val16 * pcm,int frame_size,int channels,int is_silence,int arch)895 static int decide_dtx_mode(float activity_probability, /* probability that current frame contains speech/music */
896 int *nb_no_activity_frames, /* number of consecutive frames with no activity */
897 opus_val32 peak_signal_energy, /* peak energy of desired signal detected so far */
898 const opus_val16 *pcm, /* input pcm signal */
899 int frame_size, /* frame size */
900 int channels,
901 int is_silence, /* only digital silence detected in this frame */
902 int arch
903 )
904 {
905 opus_val32 noise_energy;
906
907 if (!is_silence)
908 {
909 if (activity_probability < DTX_ACTIVITY_THRESHOLD) /* is noise */
910 {
911 noise_energy = compute_frame_energy(pcm, frame_size, channels, arch);
912
913 /* but is sufficiently quiet */
914 is_silence = peak_signal_energy >= (PSEUDO_SNR_THRESHOLD * noise_energy);
915 }
916 }
917
918 if (is_silence)
919 {
920 /* The number of consecutive DTX frames should be within the allowed bounds */
921 (*nb_no_activity_frames)++;
922
923 if (*nb_no_activity_frames > NB_SPEECH_FRAMES_BEFORE_DTX)
924 {
925 if (*nb_no_activity_frames <= (NB_SPEECH_FRAMES_BEFORE_DTX + MAX_CONSECUTIVE_DTX))
926 /* Valid frame for DTX! */
927 return 1;
928 else
929 (*nb_no_activity_frames) = NB_SPEECH_FRAMES_BEFORE_DTX;
930 }
931 } else
932 (*nb_no_activity_frames) = 0;
933
934 return 0;
935 }
936
937 #endif
938
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)939 static opus_int32 encode_multiframe_packet(OpusEncoder *st,
940 const opus_val16 *pcm,
941 int nb_frames,
942 int frame_size,
943 unsigned char *data,
944 opus_int32 out_data_bytes,
945 int to_celt,
946 int lsb_depth,
947 int float_api)
948 {
949 int i;
950 int ret = 0;
951 VARDECL(unsigned char, tmp_data);
952 int bak_mode, bak_bandwidth, bak_channels, bak_to_mono;
953 VARDECL(OpusRepacketizer, rp);
954 int max_header_bytes;
955 opus_int32 bytes_per_frame;
956 opus_int32 cbr_bytes;
957 opus_int32 repacketize_len;
958 int tmp_len;
959 ALLOC_STACK;
960
961 /* Worst cases:
962 * 2 frames: Code 2 with different compressed sizes
963 * >2 frames: Code 3 VBR */
964 max_header_bytes = nb_frames == 2 ? 3 : (2+(nb_frames-1)*2);
965
966 if (st->use_vbr || st->user_bitrate_bps==OPUS_BITRATE_MAX)
967 repacketize_len = out_data_bytes;
968 else {
969 cbr_bytes = 3*st->bitrate_bps/(3*8*st->Fs/(frame_size*nb_frames));
970 repacketize_len = IMIN(cbr_bytes, out_data_bytes);
971 }
972 bytes_per_frame = IMIN(1276, 1+(repacketize_len-max_header_bytes)/nb_frames);
973
974 ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char);
975 ALLOC(rp, 1, OpusRepacketizer);
976 opus_repacketizer_init(rp);
977
978 bak_mode = st->user_forced_mode;
979 bak_bandwidth = st->user_bandwidth;
980 bak_channels = st->force_channels;
981
982 st->user_forced_mode = st->mode;
983 st->user_bandwidth = st->bandwidth;
984 st->force_channels = st->stream_channels;
985
986 bak_to_mono = st->silk_mode.toMono;
987 if (bak_to_mono)
988 st->force_channels = 1;
989 else
990 st->prev_channels = st->stream_channels;
991
992 for (i=0;i<nb_frames;i++)
993 {
994 st->silk_mode.toMono = 0;
995 st->nonfinal_frame = i<(nb_frames-1);
996
997 /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */
998 if (to_celt && i==nb_frames-1)
999 st->user_forced_mode = MODE_CELT_ONLY;
1000
1001 tmp_len = opus_encode_native(st, pcm+i*(st->channels*frame_size), frame_size,
1002 tmp_data+i*bytes_per_frame, bytes_per_frame, lsb_depth, NULL, 0, 0, 0, 0,
1003 NULL, float_api);
1004
1005 if (tmp_len<0)
1006 {
1007 RESTORE_STACK;
1008 return OPUS_INTERNAL_ERROR;
1009 }
1010
1011 ret = opus_repacketizer_cat(rp, tmp_data+i*bytes_per_frame, tmp_len);
1012
1013 if (ret<0)
1014 {
1015 RESTORE_STACK;
1016 return OPUS_INTERNAL_ERROR;
1017 }
1018 }
1019
1020 ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketize_len, 0, !st->use_vbr);
1021
1022 if (ret<0)
1023 {
1024 RESTORE_STACK;
1025 return OPUS_INTERNAL_ERROR;
1026 }
1027
1028 /* Discard configs that were forced locally for the purpose of repacketization */
1029 st->user_forced_mode = bak_mode;
1030 st->user_bandwidth = bak_bandwidth;
1031 st->force_channels = bak_channels;
1032 st->silk_mode.toMono = bak_to_mono;
1033
1034 RESTORE_STACK;
1035 return ret;
1036 }
1037
compute_redundancy_bytes(opus_int32 max_data_bytes,opus_int32 bitrate_bps,int frame_rate,int channels)1038 static int compute_redundancy_bytes(opus_int32 max_data_bytes, opus_int32 bitrate_bps, int frame_rate, int channels)
1039 {
1040 int redundancy_bytes_cap;
1041 int redundancy_bytes;
1042 opus_int32 redundancy_rate;
1043 int base_bits;
1044 opus_int32 available_bits;
1045 base_bits = (40*channels+20);
1046
1047 /* Equivalent rate for 5 ms frames. */
1048 redundancy_rate = bitrate_bps + base_bits*(200 - frame_rate);
1049 /* For VBR, further increase the bitrate if we can afford it. It's pretty short
1050 and we'll avoid artefacts. */
1051 redundancy_rate = 3*redundancy_rate/2;
1052 redundancy_bytes = redundancy_rate/1600;
1053
1054 /* Compute the max rate we can use given CBR or VBR with cap. */
1055 available_bits = max_data_bytes*8 - 2*base_bits;
1056 redundancy_bytes_cap = (available_bits*240/(240+48000/frame_rate) + base_bits)/8;
1057 redundancy_bytes = IMIN(redundancy_bytes, redundancy_bytes_cap);
1058 /* It we can't get enough bits for redundancy to be worth it, rely on the decoder PLC. */
1059 if (redundancy_bytes > 4 + 8*channels)
1060 redundancy_bytes = IMIN(257, redundancy_bytes);
1061 else
1062 redundancy_bytes = 0;
1063 return redundancy_bytes;
1064 }
1065
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)1066 opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
1067 unsigned char *data, opus_int32 out_data_bytes, int lsb_depth,
1068 const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2,
1069 int analysis_channels, downmix_func downmix, int float_api)
1070 {
1071 void *silk_enc;
1072 CELTEncoder *celt_enc;
1073 int i;
1074 int ret=0;
1075 opus_int32 nBytes;
1076 ec_enc enc;
1077 int bytes_target;
1078 int prefill=0;
1079 int start_band = 0;
1080 int redundancy = 0;
1081 int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */
1082 int celt_to_silk = 0;
1083 VARDECL(opus_val16, pcm_buf);
1084 int nb_compr_bytes;
1085 int to_celt = 0;
1086 opus_uint32 redundant_rng = 0;
1087 int cutoff_Hz, hp_freq_smth1;
1088 int voice_est; /* Probability of voice in Q7 */
1089 opus_int32 equiv_rate;
1090 int delay_compensation;
1091 int frame_rate;
1092 opus_int32 max_rate; /* Max bitrate we're allowed to use */
1093 int curr_bandwidth;
1094 opus_val16 HB_gain;
1095 opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */
1096 int total_buffer;
1097 opus_val16 stereo_width;
1098 const CELTMode *celt_mode;
1099 #ifndef DISABLE_FLOAT_API
1100 AnalysisInfo analysis_info;
1101 int analysis_read_pos_bak=-1;
1102 int analysis_read_subframe_bak=-1;
1103 int is_silence = 0;
1104 #endif
1105 VARDECL(opus_val16, tmp_prefill);
1106
1107 ALLOC_STACK;
1108
1109 max_data_bytes = IMIN(1276, out_data_bytes);
1110
1111 st->rangeFinal = 0;
1112 if (frame_size <= 0 || max_data_bytes <= 0)
1113 {
1114 RESTORE_STACK;
1115 return OPUS_BAD_ARG;
1116 }
1117
1118 /* Cannot encode 100 ms in 1 byte */
1119 if (max_data_bytes==1 && st->Fs==(frame_size*10))
1120 {
1121 RESTORE_STACK;
1122 return OPUS_BUFFER_TOO_SMALL;
1123 }
1124
1125 silk_enc = (char*)st+st->silk_enc_offset;
1126 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1127 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1128 delay_compensation = 0;
1129 else
1130 delay_compensation = st->delay_compensation;
1131
1132 lsb_depth = IMIN(lsb_depth, st->lsb_depth);
1133
1134 celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1135 #ifndef DISABLE_FLOAT_API
1136 analysis_info.valid = 0;
1137 #ifdef FIXED_POINT
1138 if (st->silk_mode.complexity >= 10 && st->Fs>=16000)
1139 #else
1140 if (st->silk_mode.complexity >= 7 && st->Fs>=16000)
1141 #endif
1142 {
1143 if (is_digital_silence(pcm, frame_size, st->channels, lsb_depth))
1144 {
1145 is_silence = 1;
1146 } else {
1147 analysis_read_pos_bak = st->analysis.read_pos;
1148 analysis_read_subframe_bak = st->analysis.read_subframe;
1149 run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame_size,
1150 c1, c2, analysis_channels, st->Fs,
1151 lsb_depth, downmix, &analysis_info);
1152 }
1153
1154 /* Track the peak signal energy */
1155 if (!is_silence && analysis_info.activity_probability > DTX_ACTIVITY_THRESHOLD)
1156 st->peak_signal_energy = MAX32(MULT16_32_Q15(QCONST16(0.999f, 15), st->peak_signal_energy),
1157 compute_frame_energy(pcm, frame_size, st->channels, st->arch));
1158 }
1159 #else
1160 (void)analysis_pcm;
1161 (void)analysis_size;
1162 (void)c1;
1163 (void)c2;
1164 (void)analysis_channels;
1165 (void)downmix;
1166 #endif
1167
1168 #ifndef DISABLE_FLOAT_API
1169 /* Reset voice_ratio if this frame is not silent or if analysis is disabled.
1170 * Otherwise, preserve voice_ratio from the last non-silent frame */
1171 if (!is_silence)
1172 st->voice_ratio = -1;
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 /* Random mono/stereo decision */
1319 if (st->channels == 2 && (rand()&0x1F)==0)
1320 st->stream_channels = 3-st->stream_channels;
1321 #else
1322 /* Rate-dependent mono-stereo decision */
1323 if (st->channels == 2)
1324 {
1325 opus_int32 stereo_threshold;
1326 stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14);
1327 if (st->stream_channels == 2)
1328 stereo_threshold -= 1000;
1329 else
1330 stereo_threshold += 1000;
1331 st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
1332 } else {
1333 st->stream_channels = st->channels;
1334 }
1335 #endif
1336 }
1337 /* Update equivalent rate for channels decision. */
1338 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1339 st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1340
1341 /* Mode selection depending on application and signal type */
1342 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1343 {
1344 st->mode = MODE_CELT_ONLY;
1345 } else if (st->user_forced_mode == OPUS_AUTO)
1346 {
1347 #ifdef FUZZING
1348 /* Random mode switching */
1349 if ((rand()&0xF)==0)
1350 {
1351 if ((rand()&0x1)==0)
1352 st->mode = MODE_CELT_ONLY;
1353 else
1354 st->mode = MODE_SILK_ONLY;
1355 } else {
1356 if (st->prev_mode==MODE_CELT_ONLY)
1357 st->mode = MODE_CELT_ONLY;
1358 else
1359 st->mode = MODE_SILK_ONLY;
1360 }
1361 #else
1362 opus_int32 mode_voice, mode_music;
1363 opus_int32 threshold;
1364
1365 /* Interpolate based on stereo width */
1366 mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[0][0])
1367 + MULT16_32_Q15(stereo_width,mode_thresholds[1][0]));
1368 mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[1][1])
1369 + MULT16_32_Q15(stereo_width,mode_thresholds[1][1]));
1370 /* Interpolate based on speech/music probability */
1371 threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14);
1372 /* Bias towards SILK for VoIP because of some useful features */
1373 if (st->application == OPUS_APPLICATION_VOIP)
1374 threshold += 8000;
1375
1376 /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/
1377 /* Hysteresis */
1378 if (st->prev_mode == MODE_CELT_ONLY)
1379 threshold -= 4000;
1380 else if (st->prev_mode>0)
1381 threshold += 4000;
1382
1383 st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY;
1384
1385 /* When FEC is enabled and there's enough packet loss, use SILK */
1386 if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (128-voice_est)>>4)
1387 st->mode = MODE_SILK_ONLY;
1388 /* When encoding voice and DTX is enabled but the generalized DTX cannot be used,
1389 because of complexity and sampling frequency settings, switch to SILK DTX and
1390 set the encoder to SILK mode */
1391 #ifndef DISABLE_FLOAT_API
1392 st->silk_mode.useDTX = st->use_dtx && !(analysis_info.valid || is_silence);
1393 #else
1394 st->silk_mode.useDTX = st->use_dtx;
1395 #endif
1396 if (st->silk_mode.useDTX && voice_est > 100)
1397 st->mode = MODE_SILK_ONLY;
1398 #endif
1399
1400 /* If max_data_bytes represents less than 6 kb/s, switch to CELT-only mode */
1401 if (max_data_bytes < (frame_rate > 50 ? 9000 : 6000)*frame_size / (st->Fs * 8))
1402 st->mode = MODE_CELT_ONLY;
1403 } else {
1404 st->mode = st->user_forced_mode;
1405 }
1406
1407 /* Override the chosen mode to make sure we meet the requested frame size */
1408 if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100)
1409 st->mode = MODE_CELT_ONLY;
1410 if (st->lfe)
1411 st->mode = MODE_CELT_ONLY;
1412
1413 if (st->prev_mode > 0 &&
1414 ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ||
1415 (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)))
1416 {
1417 redundancy = 1;
1418 celt_to_silk = (st->mode != MODE_CELT_ONLY);
1419 if (!celt_to_silk)
1420 {
1421 /* Switch to SILK/hybrid if frame size is 10 ms or more*/
1422 if (frame_size >= st->Fs/100)
1423 {
1424 st->mode = st->prev_mode;
1425 to_celt = 1;
1426 } else {
1427 redundancy=0;
1428 }
1429 }
1430 }
1431
1432 /* When encoding multiframes, we can ask for a switch to CELT only in the last frame. This switch
1433 * is processed above as the requested mode shouldn't interrupt stereo->mono transition. */
1434 if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0
1435 && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)
1436 {
1437 /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */
1438 st->silk_mode.toMono = 1;
1439 st->stream_channels = 2;
1440 } else {
1441 st->silk_mode.toMono = 0;
1442 }
1443
1444 /* Update equivalent rate with mode decision. */
1445 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1446 st->use_vbr, st->mode, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1447
1448 if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
1449 {
1450 silk_EncControlStruct dummy;
1451 silk_InitEncoder( silk_enc, st->arch, &dummy);
1452 prefill=1;
1453 }
1454
1455 /* Automatic (rate-dependent) bandwidth selection */
1456 if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch)
1457 {
1458 const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds;
1459 opus_int32 bandwidth_thresholds[8];
1460 int bandwidth = OPUS_BANDWIDTH_FULLBAND;
1461
1462 if (st->channels==2 && st->force_channels!=1)
1463 {
1464 voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds;
1465 music_bandwidth_thresholds = stereo_music_bandwidth_thresholds;
1466 } else {
1467 voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds;
1468 music_bandwidth_thresholds = mono_music_bandwidth_thresholds;
1469 }
1470 /* Interpolate bandwidth thresholds depending on voice estimation */
1471 for (i=0;i<8;i++)
1472 {
1473 bandwidth_thresholds[i] = music_bandwidth_thresholds[i]
1474 + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14);
1475 }
1476 do {
1477 int threshold, hysteresis;
1478 threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)];
1479 hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1];
1480 if (!st->first)
1481 {
1482 if (st->auto_bandwidth >= bandwidth)
1483 threshold -= hysteresis;
1484 else
1485 threshold += hysteresis;
1486 }
1487 if (equiv_rate >= threshold)
1488 break;
1489 } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND);
1490 /* We don't use mediumband anymore, except when explicitly requested or during
1491 mode transitions. */
1492 if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1493 bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1494 st->bandwidth = st->auto_bandwidth = bandwidth;
1495 /* Prevents any transition to SWB/FB until the SILK layer has fully
1496 switched to WB mode and turned the variable LP filter off */
1497 if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1498 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1499 }
1500
1501 if (st->bandwidth>st->max_bandwidth)
1502 st->bandwidth = st->max_bandwidth;
1503
1504 if (st->user_bandwidth != OPUS_AUTO)
1505 st->bandwidth = st->user_bandwidth;
1506
1507 /* This prevents us from using hybrid at unsafe CBR/max rates */
1508 if (st->mode != MODE_CELT_ONLY && max_rate < 15000)
1509 {
1510 st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND);
1511 }
1512
1513 /* Prevents Opus from wasting bits on frequencies that are above
1514 the Nyquist rate of the input signal */
1515 if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND)
1516 st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1517 if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1518 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1519 if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND)
1520 st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1521 if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
1522 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1523 #ifndef DISABLE_FLOAT_API
1524 /* Use detected bandwidth to reduce the encoded bandwidth. */
1525 if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO)
1526 {
1527 int min_detected_bandwidth;
1528 /* Makes bandwidth detection more conservative just in case the detector
1529 gets it wrong when we could have coded a high bandwidth transparently.
1530 When operating in SILK/hybrid mode, we don't go below wideband to avoid
1531 more complicated switches that require redundancy. */
1532 if (equiv_rate <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1533 min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1534 else if (equiv_rate <= 24000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1535 min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1536 else if (equiv_rate <= 30000*st->stream_channels)
1537 min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1538 else if (equiv_rate <= 44000*st->stream_channels)
1539 min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1540 else
1541 min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1542
1543 st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwidth);
1544 st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth);
1545 }
1546 #endif
1547 st->silk_mode.LBRR_coded = decide_fec(st->silk_mode.useInBandFEC, st->silk_mode.packetLossPercentage,
1548 st->silk_mode.LBRR_coded, st->mode, &st->bandwidth, equiv_rate);
1549 celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth));
1550
1551 /* CELT mode doesn't support mediumband, use wideband instead */
1552 if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1553 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1554 if (st->lfe)
1555 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1556
1557 curr_bandwidth = st->bandwidth;
1558
1559 /* Chooses the appropriate mode for speech
1560 *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */
1561 if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1562 st->mode = MODE_HYBRID;
1563 if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND)
1564 st->mode = MODE_SILK_ONLY;
1565
1566 /* Can't support higher than >60 ms frames, and >20 ms when in Hybrid or CELT-only modes */
1567 if ((frame_size > st->Fs/50 && (st->mode != MODE_SILK_ONLY)) || frame_size > 3*st->Fs/50)
1568 {
1569 int enc_frame_size;
1570 int nb_frames;
1571
1572 if (st->mode == MODE_SILK_ONLY)
1573 {
1574 if (frame_size == 2*st->Fs/25) /* 80 ms -> 2x 40 ms */
1575 enc_frame_size = st->Fs/25;
1576 else if (frame_size == 3*st->Fs/25) /* 120 ms -> 2x 60 ms */
1577 enc_frame_size = 3*st->Fs/50;
1578 else /* 100 ms -> 5x 20 ms */
1579 enc_frame_size = st->Fs/50;
1580 }
1581 else
1582 enc_frame_size = st->Fs/50;
1583
1584 nb_frames = frame_size/enc_frame_size;
1585
1586 #ifndef DISABLE_FLOAT_API
1587 if (analysis_read_pos_bak!= -1)
1588 {
1589 st->analysis.read_pos = analysis_read_pos_bak;
1590 st->analysis.read_subframe = analysis_read_subframe_bak;
1591 }
1592 #endif
1593
1594 ret = encode_multiframe_packet(st, pcm, nb_frames, enc_frame_size, data,
1595 out_data_bytes, to_celt, lsb_depth, float_api);
1596
1597 RESTORE_STACK;
1598 return ret;
1599 }
1600
1601 /* For the first frame at a new SILK bandwidth */
1602 if (st->silk_bw_switch)
1603 {
1604 redundancy = 1;
1605 celt_to_silk = 1;
1606 st->silk_bw_switch = 0;
1607 /* Do a prefill without reseting the sampling rate control. */
1608 prefill=2;
1609 }
1610
1611 /* If we decided to go with CELT, make sure redundancy is off, no matter what
1612 we decided earlier. */
1613 if (st->mode == MODE_CELT_ONLY)
1614 redundancy = 0;
1615
1616 if (redundancy)
1617 {
1618 redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
1619 if (redundancy_bytes == 0)
1620 redundancy = 0;
1621 }
1622
1623 /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */
1624 bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1;
1625
1626 data += 1;
1627
1628 ec_enc_init(&enc, data, max_data_bytes-1);
1629
1630 ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_val16);
1631 OPUS_COPY(pcm_buf, &st->delay_buffer[(st->encoder_buffer-total_buffer)*st->channels], total_buffer*st->channels);
1632
1633 if (st->mode == MODE_CELT_ONLY)
1634 hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
1635 else
1636 hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15;
1637
1638 st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15,
1639 hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) );
1640
1641 /* convert from log scale to Hertz */
1642 cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
1643
1644 if (st->application == OPUS_APPLICATION_VOIP)
1645 {
1646 hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs, st->arch);
1647 } else {
1648 dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
1649 }
1650 #ifndef FIXED_POINT
1651 if (float_api)
1652 {
1653 opus_val32 sum;
1654 sum = celt_inner_prod(&pcm_buf[total_buffer*st->channels], &pcm_buf[total_buffer*st->channels], frame_size*st->channels, st->arch);
1655 /* This should filter out both NaNs and ridiculous signals that could
1656 cause NaNs further down. */
1657 if (!(sum < 1e9f) || celt_isnan(sum))
1658 {
1659 OPUS_CLEAR(&pcm_buf[total_buffer*st->channels], frame_size*st->channels);
1660 st->hp_mem[0] = st->hp_mem[1] = st->hp_mem[2] = st->hp_mem[3] = 0;
1661 }
1662 }
1663 #endif
1664
1665
1666 /* SILK processing */
1667 HB_gain = Q15ONE;
1668 if (st->mode != MODE_CELT_ONLY)
1669 {
1670 opus_int32 total_bitRate, celt_rate;
1671 opus_int activity;
1672 #ifdef FIXED_POINT
1673 const opus_int16 *pcm_silk;
1674 #else
1675 VARDECL(opus_int16, pcm_silk);
1676 ALLOC(pcm_silk, st->channels*frame_size, opus_int16);
1677 #endif
1678
1679 activity = VAD_NO_DECISION;
1680 #ifndef DISABLE_FLOAT_API
1681 if( analysis_info.valid ) {
1682 /* Inform SILK about the Opus VAD decision */
1683 activity = ( analysis_info.activity_probability >= DTX_ACTIVITY_THRESHOLD );
1684 }
1685 #endif
1686
1687 /* Distribute bits between SILK and CELT */
1688 total_bitRate = 8 * bytes_target * frame_rate;
1689 if( st->mode == MODE_HYBRID ) {
1690 /* Base rate for SILK */
1691 st->silk_mode.bitRate = compute_silk_rate_for_hybrid(total_bitRate,
1692 curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
1693 st->stream_channels);
1694 if (!st->energy_masking)
1695 {
1696 /* Increasingly attenuate high band when it gets allocated fewer bits */
1697 celt_rate = total_bitRate - st->silk_mode.bitRate;
1698 HB_gain = Q15ONE - SHR32(celt_exp2(-celt_rate * QCONST16(1.f/1024, 10)), 1);
1699 }
1700 } else {
1701 /* SILK gets all bits */
1702 st->silk_mode.bitRate = total_bitRate;
1703 }
1704
1705 /* Surround masking for SILK */
1706 if (st->energy_masking && st->use_vbr && !st->lfe)
1707 {
1708 opus_val32 mask_sum=0;
1709 opus_val16 masking_depth;
1710 opus_int32 rate_offset;
1711 int c;
1712 int end = 17;
1713 opus_int16 srate = 16000;
1714 if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
1715 {
1716 end = 13;
1717 srate = 8000;
1718 } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1719 {
1720 end = 15;
1721 srate = 12000;
1722 }
1723 for (c=0;c<st->channels;c++)
1724 {
1725 for(i=0;i<end;i++)
1726 {
1727 opus_val16 mask;
1728 mask = MAX16(MIN16(st->energy_masking[21*c+i],
1729 QCONST16(.5f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT));
1730 if (mask > 0)
1731 mask = HALF16(mask);
1732 mask_sum += mask;
1733 }
1734 }
1735 /* Conservative rate reduction, we cut the masking in half */
1736 masking_depth = mask_sum / end*st->channels;
1737 masking_depth += QCONST16(.2f, DB_SHIFT);
1738 rate_offset = (opus_int32)PSHR32(MULT16_16(srate, masking_depth), DB_SHIFT);
1739 rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3);
1740 /* Split the rate change between the SILK and CELT part for hybrid. */
1741 if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPUS_BANDWIDTH_FULLBAND)
1742 st->silk_mode.bitRate += 3*rate_offset/5;
1743 else
1744 st->silk_mode.bitRate += rate_offset;
1745 }
1746
1747 st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
1748 st->silk_mode.nChannelsAPI = st->channels;
1749 st->silk_mode.nChannelsInternal = st->stream_channels;
1750 if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
1751 st->silk_mode.desiredInternalSampleRate = 8000;
1752 } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
1753 st->silk_mode.desiredInternalSampleRate = 12000;
1754 } else {
1755 celt_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND );
1756 st->silk_mode.desiredInternalSampleRate = 16000;
1757 }
1758 if( st->mode == MODE_HYBRID ) {
1759 /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
1760 st->silk_mode.minInternalSampleRate = 16000;
1761 } else {
1762 st->silk_mode.minInternalSampleRate = 8000;
1763 }
1764
1765 st->silk_mode.maxInternalSampleRate = 16000;
1766 if (st->mode == MODE_SILK_ONLY)
1767 {
1768 opus_int32 effective_max_rate = max_rate;
1769 if (frame_rate > 50)
1770 effective_max_rate = effective_max_rate*2/3;
1771 if (effective_max_rate < 8000)
1772 {
1773 st->silk_mode.maxInternalSampleRate = 12000;
1774 st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate);
1775 }
1776 if (effective_max_rate < 7000)
1777 {
1778 st->silk_mode.maxInternalSampleRate = 8000;
1779 st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate);
1780 }
1781 }
1782
1783 st->silk_mode.useCBR = !st->use_vbr;
1784
1785 /* Call SILK encoder for the low band */
1786
1787 /* Max bits for SILK, counting ToC, redundancy bytes, and optionally redundancy. */
1788 st->silk_mode.maxBits = (max_data_bytes-1)*8;
1789 if (redundancy && redundancy_bytes >= 2)
1790 {
1791 /* Counting 1 bit for redundancy position and 20 bits for flag+size (only for hybrid). */
1792 st->silk_mode.maxBits -= redundancy_bytes*8 + 1;
1793 if (st->mode == MODE_HYBRID)
1794 st->silk_mode.maxBits -= 20;
1795 }
1796 if (st->silk_mode.useCBR)
1797 {
1798 if (st->mode == MODE_HYBRID)
1799 {
1800 st->silk_mode.maxBits = IMIN(st->silk_mode.maxBits, st->silk_mode.bitRate * frame_size / st->Fs);
1801 }
1802 } else {
1803 /* Constrained VBR. */
1804 if (st->mode == MODE_HYBRID)
1805 {
1806 /* Compute SILK bitrate corresponding to the max total bits available */
1807 opus_int32 maxBitRate = compute_silk_rate_for_hybrid(st->silk_mode.maxBits*st->Fs / frame_size,
1808 curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
1809 st->stream_channels);
1810 st->silk_mode.maxBits = maxBitRate * frame_size / st->Fs;
1811 }
1812 }
1813
1814 if (prefill)
1815 {
1816 opus_int32 zero=0;
1817 int prefill_offset;
1818 /* Use a smooth onset for the SILK prefill to avoid the encoder trying to encode
1819 a discontinuity. The exact location is what we need to avoid leaving any "gap"
1820 in the audio when mixing with the redundant CELT frame. Here we can afford to
1821 overwrite st->delay_buffer because the only thing that uses it before it gets
1822 rewritten is tmp_prefill[] and even then only the part after the ramp really
1823 gets used (rather than sent to the encoder and discarded) */
1824 prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensation-st->Fs/400);
1825 gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_offset,
1826 0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_mode->window, st->Fs);
1827 OPUS_CLEAR(st->delay_buffer, prefill_offset);
1828 #ifdef FIXED_POINT
1829 pcm_silk = st->delay_buffer;
1830 #else
1831 for (i=0;i<st->encoder_buffer*st->channels;i++)
1832 pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]);
1833 #endif
1834 silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, prefill, activity );
1835 /* Prevent a second switch in the real encode call. */
1836 st->silk_mode.opusCanSwitch = 0;
1837 }
1838
1839 #ifdef FIXED_POINT
1840 pcm_silk = pcm_buf+total_buffer*st->channels;
1841 #else
1842 for (i=0;i<frame_size*st->channels;i++)
1843 pcm_silk[i] = FLOAT2INT16(pcm_buf[total_buffer*st->channels + i]);
1844 #endif
1845 ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0, activity );
1846 if( ret ) {
1847 /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
1848 /* Handle error */
1849 RESTORE_STACK;
1850 return OPUS_INTERNAL_ERROR;
1851 }
1852
1853 /* Extract SILK internal bandwidth for signaling in first byte */
1854 if( st->mode == MODE_SILK_ONLY ) {
1855 if( st->silk_mode.internalSampleRate == 8000 ) {
1856 curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1857 } else if( st->silk_mode.internalSampleRate == 12000 ) {
1858 curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1859 } else if( st->silk_mode.internalSampleRate == 16000 ) {
1860 curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1861 }
1862 } else {
1863 celt_assert( st->silk_mode.internalSampleRate == 16000 );
1864 }
1865
1866 st->silk_mode.opusCanSwitch = st->silk_mode.switchReady && !st->nonfinal_frame;
1867
1868 if (nBytes==0)
1869 {
1870 st->rangeFinal = 0;
1871 data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
1872 RESTORE_STACK;
1873 return 1;
1874 }
1875
1876 /* FIXME: How do we allocate the redundancy for CBR? */
1877 if (st->silk_mode.opusCanSwitch)
1878 {
1879 redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
1880 redundancy = (redundancy_bytes != 0);
1881 celt_to_silk = 0;
1882 st->silk_bw_switch = 1;
1883 }
1884 }
1885
1886 /* CELT processing */
1887 {
1888 int endband=21;
1889
1890 switch(curr_bandwidth)
1891 {
1892 case OPUS_BANDWIDTH_NARROWBAND:
1893 endband = 13;
1894 break;
1895 case OPUS_BANDWIDTH_MEDIUMBAND:
1896 case OPUS_BANDWIDTH_WIDEBAND:
1897 endband = 17;
1898 break;
1899 case OPUS_BANDWIDTH_SUPERWIDEBAND:
1900 endband = 19;
1901 break;
1902 case OPUS_BANDWIDTH_FULLBAND:
1903 endband = 21;
1904 break;
1905 }
1906 celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband));
1907 celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels));
1908 }
1909 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
1910 if (st->mode != MODE_SILK_ONLY)
1911 {
1912 opus_val32 celt_pred=2;
1913 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
1914 /* We may still decide to disable prediction later */
1915 if (st->silk_mode.reducedDependency)
1916 celt_pred = 0;
1917 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred));
1918
1919 if (st->mode == MODE_HYBRID)
1920 {
1921 if( st->use_vbr ) {
1922 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps-st->silk_mode.bitRate));
1923 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0));
1924 }
1925 } else {
1926 if (st->use_vbr)
1927 {
1928 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
1929 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint));
1930 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps));
1931 }
1932 }
1933 }
1934
1935 ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16);
1936 if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0)
1937 {
1938 OPUS_COPY(tmp_prefill, &st->delay_buffer[(st->encoder_buffer-total_buffer-st->Fs/400)*st->channels], st->channels*st->Fs/400);
1939 }
1940
1941 if (st->channels*(st->encoder_buffer-(frame_size+total_buffer)) > 0)
1942 {
1943 OPUS_MOVE(st->delay_buffer, &st->delay_buffer[st->channels*frame_size], st->channels*(st->encoder_buffer-frame_size-total_buffer));
1944 OPUS_COPY(&st->delay_buffer[st->channels*(st->encoder_buffer-frame_size-total_buffer)],
1945 &pcm_buf[0],
1946 (frame_size+total_buffer)*st->channels);
1947 } else {
1948 OPUS_COPY(st->delay_buffer, &pcm_buf[(frame_size+total_buffer-st->encoder_buffer)*st->channels], st->encoder_buffer*st->channels);
1949 }
1950 /* gain_fade() and stereo_fade() need to be after the buffer copying
1951 because we don't want any of this to affect the SILK part */
1952 if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) {
1953 gain_fade(pcm_buf, pcm_buf,
1954 st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs);
1955 }
1956 st->prev_HB_gain = HB_gain;
1957 if (st->mode != MODE_HYBRID || st->stream_channels==1)
1958 {
1959 if (equiv_rate > 32000)
1960 st->silk_mode.stereoWidth_Q14 = 16384;
1961 else if (equiv_rate < 16000)
1962 st->silk_mode.stereoWidth_Q14 = 0;
1963 else
1964 st->silk_mode.stereoWidth_Q14 = 16384 - 2048*(opus_int32)(32000-equiv_rate)/(equiv_rate-14000);
1965 }
1966 if( !st->energy_masking && st->channels == 2 ) {
1967 /* Apply stereo width reduction (at low bitrates) */
1968 if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) {
1969 opus_val16 g1, g2;
1970 g1 = st->hybrid_stereo_width_Q14;
1971 g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
1972 #ifdef FIXED_POINT
1973 g1 = g1==16384 ? Q15ONE : SHL16(g1,1);
1974 g2 = g2==16384 ? Q15ONE : SHL16(g2,1);
1975 #else
1976 g1 *= (1.f/16384);
1977 g2 *= (1.f/16384);
1978 #endif
1979 stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
1980 frame_size, st->channels, celt_mode->window, st->Fs);
1981 st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
1982 }
1983 }
1984
1985 if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) <= 8*(max_data_bytes-1))
1986 {
1987 /* For SILK mode, the redundancy is inferred from the length */
1988 if (st->mode == MODE_HYBRID)
1989 ec_enc_bit_logp(&enc, redundancy, 12);
1990 if (redundancy)
1991 {
1992 int max_redundancy;
1993 ec_enc_bit_logp(&enc, celt_to_silk, 1);
1994 if (st->mode == MODE_HYBRID)
1995 {
1996 /* Reserve the 8 bits needed for the redundancy length,
1997 and at least a few bits for CELT if possible */
1998 max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+8+3+7)>>3);
1999 }
2000 else
2001 max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3);
2002 /* Target the same bit-rate for redundancy as for the rest,
2003 up to a max of 257 bytes */
2004 redundancy_bytes = IMIN(max_redundancy, redundancy_bytes);
2005 redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes));
2006 if (st->mode == MODE_HYBRID)
2007 ec_enc_uint(&enc, redundancy_bytes-2, 256);
2008 }
2009 } else {
2010 redundancy = 0;
2011 }
2012
2013 if (!redundancy)
2014 {
2015 st->silk_bw_switch = 0;
2016 redundancy_bytes = 0;
2017 }
2018 if (st->mode != MODE_CELT_ONLY)start_band=17;
2019
2020 if (st->mode == MODE_SILK_ONLY)
2021 {
2022 ret = (ec_tell(&enc)+7)>>3;
2023 ec_enc_done(&enc);
2024 nb_compr_bytes = ret;
2025 } else {
2026 nb_compr_bytes = (max_data_bytes-1)-redundancy_bytes;
2027 ec_enc_shrink(&enc, nb_compr_bytes);
2028 }
2029
2030 #ifndef DISABLE_FLOAT_API
2031 if (redundancy || st->mode != MODE_SILK_ONLY)
2032 celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(&analysis_info));
2033 #endif
2034 if (st->mode == MODE_HYBRID) {
2035 SILKInfo info;
2036 info.signalType = st->silk_mode.signalType;
2037 info.offset = st->silk_mode.offset;
2038 celt_encoder_ctl(celt_enc, CELT_SET_SILK_INFO(&info));
2039 }
2040
2041 /* 5 ms redundant frame for CELT->SILK */
2042 if (redundancy && celt_to_silk)
2043 {
2044 int err;
2045 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2046 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2047 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2048 err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL);
2049 if (err < 0)
2050 {
2051 RESTORE_STACK;
2052 return OPUS_INTERNAL_ERROR;
2053 }
2054 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2055 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2056 }
2057
2058 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band));
2059
2060 if (st->mode != MODE_SILK_ONLY)
2061 {
2062 if (st->mode != st->prev_mode && st->prev_mode > 0)
2063 {
2064 unsigned char dummy[2];
2065 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2066
2067 /* Prefilling */
2068 celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL);
2069 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2070 }
2071 /* If false, we already busted the budget and we'll end up with a "PLC frame" */
2072 if (ec_tell(&enc) <= 8*nb_compr_bytes)
2073 {
2074 /* Set the bitrate again if it was overridden in the redundancy code above*/
2075 if (redundancy && celt_to_silk && st->mode==MODE_HYBRID && st->use_vbr)
2076 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps-st->silk_mode.bitRate));
2077 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(st->use_vbr));
2078 ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc);
2079 if (ret < 0)
2080 {
2081 RESTORE_STACK;
2082 return OPUS_INTERNAL_ERROR;
2083 }
2084 /* Put CELT->SILK redundancy data in the right place. */
2085 if (redundancy && celt_to_silk && st->mode==MODE_HYBRID && st->use_vbr)
2086 {
2087 OPUS_MOVE(data+ret, data+nb_compr_bytes, redundancy_bytes);
2088 nb_compr_bytes = nb_compr_bytes+redundancy_bytes;
2089 }
2090 }
2091 }
2092
2093 /* 5 ms redundant frame for SILK->CELT */
2094 if (redundancy && !celt_to_silk)
2095 {
2096 int err;
2097 unsigned char dummy[2];
2098 int N2, N4;
2099 N2 = st->Fs/200;
2100 N4 = st->Fs/400;
2101
2102 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2103 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2104 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2105 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2106 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2107
2108 if (st->mode == MODE_HYBRID)
2109 {
2110 /* Shrink packet to what the encoder actually used. */
2111 nb_compr_bytes = ret;
2112 ec_enc_shrink(&enc, nb_compr_bytes);
2113 }
2114 /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */
2115 celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL);
2116
2117 err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL);
2118 if (err < 0)
2119 {
2120 RESTORE_STACK;
2121 return OPUS_INTERNAL_ERROR;
2122 }
2123 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2124 }
2125
2126
2127
2128 /* Signalling the mode in the first byte */
2129 data--;
2130 data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2131
2132 st->rangeFinal = enc.rng ^ redundant_rng;
2133
2134 if (to_celt)
2135 st->prev_mode = MODE_CELT_ONLY;
2136 else
2137 st->prev_mode = st->mode;
2138 st->prev_channels = st->stream_channels;
2139 st->prev_framesize = frame_size;
2140
2141 st->first = 0;
2142
2143 /* DTX decision */
2144 #ifndef DISABLE_FLOAT_API
2145 if (st->use_dtx && (analysis_info.valid || is_silence))
2146 {
2147 if (decide_dtx_mode(analysis_info.activity_probability, &st->nb_no_activity_frames,
2148 st->peak_signal_energy, pcm, frame_size, st->channels, is_silence, st->arch))
2149 {
2150 st->rangeFinal = 0;
2151 data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2152 RESTORE_STACK;
2153 return 1;
2154 }
2155 }
2156 #endif
2157
2158 /* In the unlikely case that the SILK encoder busted its target, tell
2159 the decoder to call the PLC */
2160 if (ec_tell(&enc) > (max_data_bytes-1)*8)
2161 {
2162 if (max_data_bytes < 2)
2163 {
2164 RESTORE_STACK;
2165 return OPUS_BUFFER_TOO_SMALL;
2166 }
2167 data[1] = 0;
2168 ret = 1;
2169 st->rangeFinal = 0;
2170 } else if (st->mode==MODE_SILK_ONLY&&!redundancy)
2171 {
2172 /*When in LPC only mode it's perfectly
2173 reasonable to strip off trailing zero bytes as
2174 the required range decoder behavior is to
2175 fill these in. This can't be done when the MDCT
2176 modes are used because the decoder needs to know
2177 the actual length for allocation purposes.*/
2178 while(ret>2&&data[ret]==0)ret--;
2179 }
2180 /* Count ToC and redundancy */
2181 ret += 1+redundancy_bytes;
2182 if (!st->use_vbr)
2183 {
2184 if (opus_packet_pad(data, ret, max_data_bytes) != OPUS_OK)
2185 {
2186 RESTORE_STACK;
2187 return OPUS_INTERNAL_ERROR;
2188 }
2189 ret = max_data_bytes;
2190 }
2191 RESTORE_STACK;
2192 return ret;
2193 }
2194
2195 #ifdef FIXED_POINT
2196
2197 #ifndef DISABLE_FLOAT_API
opus_encode_float(OpusEncoder * st,const float * pcm,int analysis_frame_size,unsigned char * data,opus_int32 max_data_bytes)2198 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
2199 unsigned char *data, opus_int32 max_data_bytes)
2200 {
2201 int i, ret;
2202 int frame_size;
2203 VARDECL(opus_int16, in);
2204 ALLOC_STACK;
2205
2206 frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2207 if (frame_size <= 0)
2208 {
2209 RESTORE_STACK;
2210 return OPUS_BAD_ARG;
2211 }
2212 ALLOC(in, frame_size*st->channels, opus_int16);
2213
2214 for (i=0;i<frame_size*st->channels;i++)
2215 in[i] = FLOAT2INT16(pcm[i]);
2216 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
2217 pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1);
2218 RESTORE_STACK;
2219 return ret;
2220 }
2221 #endif
2222
opus_encode(OpusEncoder * st,const opus_int16 * pcm,int analysis_frame_size,unsigned char * data,opus_int32 out_data_bytes)2223 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
2224 unsigned char *data, opus_int32 out_data_bytes)
2225 {
2226 int frame_size;
2227 frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2228 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 16,
2229 pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 0);
2230 }
2231
2232 #else
opus_encode(OpusEncoder * st,const opus_int16 * pcm,int analysis_frame_size,unsigned char * data,opus_int32 max_data_bytes)2233 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
2234 unsigned char *data, opus_int32 max_data_bytes)
2235 {
2236 int i, ret;
2237 int frame_size;
2238 VARDECL(float, in);
2239 ALLOC_STACK;
2240
2241 frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2242 if (frame_size <= 0)
2243 {
2244 RESTORE_STACK;
2245 return OPUS_BAD_ARG;
2246 }
2247 ALLOC(in, frame_size*st->channels, float);
2248
2249 for (i=0;i<frame_size*st->channels;i++)
2250 in[i] = (1.0f/32768)*pcm[i];
2251 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
2252 pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 0);
2253 RESTORE_STACK;
2254 return ret;
2255 }
opus_encode_float(OpusEncoder * st,const float * pcm,int analysis_frame_size,unsigned char * data,opus_int32 out_data_bytes)2256 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
2257 unsigned char *data, opus_int32 out_data_bytes)
2258 {
2259 int frame_size;
2260 frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2261 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 24,
2262 pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1);
2263 }
2264 #endif
2265
2266
opus_encoder_ctl(OpusEncoder * st,int request,...)2267 int opus_encoder_ctl(OpusEncoder *st, int request, ...)
2268 {
2269 int ret;
2270 CELTEncoder *celt_enc;
2271 va_list ap;
2272
2273 ret = OPUS_OK;
2274 va_start(ap, request);
2275
2276 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
2277
2278 switch (request)
2279 {
2280 case OPUS_SET_APPLICATION_REQUEST:
2281 {
2282 opus_int32 value = va_arg(ap, opus_int32);
2283 if ( (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_AUDIO
2284 && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2285 || (!st->first && st->application != value))
2286 {
2287 ret = OPUS_BAD_ARG;
2288 break;
2289 }
2290 st->application = value;
2291 #ifndef DISABLE_FLOAT_API
2292 st->analysis.application = value;
2293 #endif
2294 }
2295 break;
2296 case OPUS_GET_APPLICATION_REQUEST:
2297 {
2298 opus_int32 *value = va_arg(ap, opus_int32*);
2299 if (!value)
2300 {
2301 goto bad_arg;
2302 }
2303 *value = st->application;
2304 }
2305 break;
2306 case OPUS_SET_BITRATE_REQUEST:
2307 {
2308 opus_int32 value = va_arg(ap, opus_int32);
2309 if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX)
2310 {
2311 if (value <= 0)
2312 goto bad_arg;
2313 else if (value <= 500)
2314 value = 500;
2315 else if (value > (opus_int32)300000*st->channels)
2316 value = (opus_int32)300000*st->channels;
2317 }
2318 st->user_bitrate_bps = value;
2319 }
2320 break;
2321 case OPUS_GET_BITRATE_REQUEST:
2322 {
2323 opus_int32 *value = va_arg(ap, opus_int32*);
2324 if (!value)
2325 {
2326 goto bad_arg;
2327 }
2328 *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276);
2329 }
2330 break;
2331 case OPUS_SET_FORCE_CHANNELS_REQUEST:
2332 {
2333 opus_int32 value = va_arg(ap, opus_int32);
2334 if((value<1 || value>st->channels) && value != OPUS_AUTO)
2335 {
2336 goto bad_arg;
2337 }
2338 st->force_channels = value;
2339 }
2340 break;
2341 case OPUS_GET_FORCE_CHANNELS_REQUEST:
2342 {
2343 opus_int32 *value = va_arg(ap, opus_int32*);
2344 if (!value)
2345 {
2346 goto bad_arg;
2347 }
2348 *value = st->force_channels;
2349 }
2350 break;
2351 case OPUS_SET_MAX_BANDWIDTH_REQUEST:
2352 {
2353 opus_int32 value = va_arg(ap, opus_int32);
2354 if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND)
2355 {
2356 goto bad_arg;
2357 }
2358 st->max_bandwidth = value;
2359 if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2360 st->silk_mode.maxInternalSampleRate = 8000;
2361 } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2362 st->silk_mode.maxInternalSampleRate = 12000;
2363 } else {
2364 st->silk_mode.maxInternalSampleRate = 16000;
2365 }
2366 }
2367 break;
2368 case OPUS_GET_MAX_BANDWIDTH_REQUEST:
2369 {
2370 opus_int32 *value = va_arg(ap, opus_int32*);
2371 if (!value)
2372 {
2373 goto bad_arg;
2374 }
2375 *value = st->max_bandwidth;
2376 }
2377 break;
2378 case OPUS_SET_BANDWIDTH_REQUEST:
2379 {
2380 opus_int32 value = va_arg(ap, opus_int32);
2381 if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO)
2382 {
2383 goto bad_arg;
2384 }
2385 st->user_bandwidth = value;
2386 if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2387 st->silk_mode.maxInternalSampleRate = 8000;
2388 } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2389 st->silk_mode.maxInternalSampleRate = 12000;
2390 } else {
2391 st->silk_mode.maxInternalSampleRate = 16000;
2392 }
2393 }
2394 break;
2395 case OPUS_GET_BANDWIDTH_REQUEST:
2396 {
2397 opus_int32 *value = va_arg(ap, opus_int32*);
2398 if (!value)
2399 {
2400 goto bad_arg;
2401 }
2402 *value = st->bandwidth;
2403 }
2404 break;
2405 case OPUS_SET_DTX_REQUEST:
2406 {
2407 opus_int32 value = va_arg(ap, opus_int32);
2408 if(value<0 || value>1)
2409 {
2410 goto bad_arg;
2411 }
2412 st->use_dtx = value;
2413 }
2414 break;
2415 case OPUS_GET_DTX_REQUEST:
2416 {
2417 opus_int32 *value = va_arg(ap, opus_int32*);
2418 if (!value)
2419 {
2420 goto bad_arg;
2421 }
2422 *value = st->use_dtx;
2423 }
2424 break;
2425 case OPUS_SET_COMPLEXITY_REQUEST:
2426 {
2427 opus_int32 value = va_arg(ap, opus_int32);
2428 if(value<0 || value>10)
2429 {
2430 goto bad_arg;
2431 }
2432 st->silk_mode.complexity = value;
2433 celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value));
2434 }
2435 break;
2436 case OPUS_GET_COMPLEXITY_REQUEST:
2437 {
2438 opus_int32 *value = va_arg(ap, opus_int32*);
2439 if (!value)
2440 {
2441 goto bad_arg;
2442 }
2443 *value = st->silk_mode.complexity;
2444 }
2445 break;
2446 case OPUS_SET_INBAND_FEC_REQUEST:
2447 {
2448 opus_int32 value = va_arg(ap, opus_int32);
2449 if(value<0 || value>1)
2450 {
2451 goto bad_arg;
2452 }
2453 st->silk_mode.useInBandFEC = value;
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->silk_mode.useInBandFEC;
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 celt_encoder_ctl(celt_enc, OPUS_SET_EXPERT_FRAME_DURATION(value));
2633 }
2634 break;
2635 case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST:
2636 {
2637 opus_int32 *value = va_arg(ap, opus_int32*);
2638 if (!value)
2639 {
2640 goto bad_arg;
2641 }
2642 *value = st->variable_duration;
2643 }
2644 break;
2645 case OPUS_SET_PREDICTION_DISABLED_REQUEST:
2646 {
2647 opus_int32 value = va_arg(ap, opus_int32);
2648 if (value > 1 || value < 0)
2649 goto bad_arg;
2650 st->silk_mode.reducedDependency = value;
2651 }
2652 break;
2653 case OPUS_GET_PREDICTION_DISABLED_REQUEST:
2654 {
2655 opus_int32 *value = va_arg(ap, opus_int32*);
2656 if (!value)
2657 goto bad_arg;
2658 *value = st->silk_mode.reducedDependency;
2659 }
2660 break;
2661 case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
2662 {
2663 opus_int32 value = va_arg(ap, opus_int32);
2664 if(value<0 || value>1)
2665 {
2666 goto bad_arg;
2667 }
2668 celt_encoder_ctl(celt_enc, OPUS_SET_PHASE_INVERSION_DISABLED(value));
2669 }
2670 break;
2671 case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
2672 {
2673 opus_int32 *value = va_arg(ap, opus_int32*);
2674 if (!value)
2675 {
2676 goto bad_arg;
2677 }
2678 celt_encoder_ctl(celt_enc, OPUS_GET_PHASE_INVERSION_DISABLED(value));
2679 }
2680 break;
2681 case OPUS_RESET_STATE:
2682 {
2683 void *silk_enc;
2684 silk_EncControlStruct dummy;
2685 char *start;
2686 silk_enc = (char*)st+st->silk_enc_offset;
2687 #ifndef DISABLE_FLOAT_API
2688 tonality_analysis_reset(&st->analysis);
2689 #endif
2690
2691 start = (char*)&st->OPUS_ENCODER_RESET_START;
2692 OPUS_CLEAR(start, sizeof(OpusEncoder) - (start - (char*)st));
2693
2694 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2695 silk_InitEncoder( silk_enc, st->arch, &dummy );
2696 st->stream_channels = st->channels;
2697 st->hybrid_stereo_width_Q14 = 1 << 14;
2698 st->prev_HB_gain = Q15ONE;
2699 st->first = 1;
2700 st->mode = MODE_HYBRID;
2701 st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
2702 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
2703 }
2704 break;
2705 case OPUS_SET_FORCE_MODE_REQUEST:
2706 {
2707 opus_int32 value = va_arg(ap, opus_int32);
2708 if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO)
2709 {
2710 goto bad_arg;
2711 }
2712 st->user_forced_mode = value;
2713 }
2714 break;
2715 case OPUS_SET_LFE_REQUEST:
2716 {
2717 opus_int32 value = va_arg(ap, opus_int32);
2718 st->lfe = value;
2719 ret = celt_encoder_ctl(celt_enc, OPUS_SET_LFE(value));
2720 }
2721 break;
2722 case OPUS_SET_ENERGY_MASK_REQUEST:
2723 {
2724 opus_val16 *value = va_arg(ap, opus_val16*);
2725 st->energy_masking = value;
2726 ret = celt_encoder_ctl(celt_enc, OPUS_SET_ENERGY_MASK(value));
2727 }
2728 break;
2729
2730 case CELT_GET_MODE_REQUEST:
2731 {
2732 const CELTMode ** value = va_arg(ap, const CELTMode**);
2733 if (!value)
2734 {
2735 goto bad_arg;
2736 }
2737 ret = celt_encoder_ctl(celt_enc, CELT_GET_MODE(value));
2738 }
2739 break;
2740 default:
2741 /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/
2742 ret = OPUS_UNIMPLEMENTED;
2743 break;
2744 }
2745 va_end(ap);
2746 return ret;
2747 bad_arg:
2748 va_end(ap);
2749 return OPUS_BAD_ARG;
2750 }
2751
opus_encoder_destroy(OpusEncoder * st)2752 void opus_encoder_destroy(OpusEncoder *st)
2753 {
2754 opus_free(st);
2755 }
2756