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