1 /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited
2 Written by Jean-Marc Valin and Koen Vos */
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
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.
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.
37 #include "stack_alloc.h"
38 #include "float_cast.h"
42 #include "opus_private.h"
43 #include "os_support.h"
44 #include "cpu_support.h"
47 #include "tuning_parameters.h"
49 #include "fixed/structs_FIX.h"
51 #include "float/structs_FLP.h"
54 #define MAX_ENCODER_BUFFER 480
56 #ifndef DISABLE_FLOAT_API
57 #define PSEUDO_SNR_THRESHOLD 316.23f /* 10^(25/10) */
61 opus_val32 XX, XY, YY;
62 opus_val16 smoothed_width;
63 opus_val16 max_follower;
69 silk_EncControlStruct silk_mode;
72 int delay_compensation;
82 int variable_duration;
83 opus_int32 bitrate_bps;
84 opus_int32 user_bitrate_bps;
89 int use_dtx; /* general DTX for both SILK and CELT */
90 #ifndef DISABLE_FLOAT_API
91 TonalityAnalysisState analysis;
94 #define OPUS_ENCODER_RESET_START stream_channels
96 opus_int16 hybrid_stereo_width_Q14;
97 opus_int32 variable_HP_smth2_Q15;
98 opus_val16 prev_HB_gain;
105 /* Bandwidth determined automatically from the rate (before any other adjustment) */
108 /* Sampling rate (at the API level) */
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;
118 int nonfinal_frame; /* current frame is not the final in a packet */
119 opus_uint32 rangeFinal;
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 10000, 1000, /* NB<->MB */
127 11000, 1000, /* MB<->WB */
128 13500, 1000, /* WB<->SWB */
129 14000, 2000, /* SWB<->FB */
131 static const opus_int32 mono_music_bandwidth_thresholds[8] = {
132 10000, 1000, /* NB<->MB */
133 11000, 1000, /* MB<->WB */
134 13500, 1000, /* WB<->SWB */
135 14000, 2000, /* SWB<->FB */
137 static const opus_int32 stereo_voice_bandwidth_thresholds[8] = {
138 10000, 1000, /* NB<->MB */
139 11000, 1000, /* MB<->WB */
140 13500, 1000, /* WB<->SWB */
141 14000, 2000, /* SWB<->FB */
143 static const opus_int32 stereo_music_bandwidth_thresholds[8] = {
144 10000, 1000, /* NB<->MB */
145 11000, 1000, /* MB<->WB */
146 13500, 1000, /* WB<->SWB */
147 14000, 2000, /* SWB<->FB */
149 /* Threshold bit-rates for switching between mono and stereo */
150 static const opus_int32 stereo_voice_threshold = 24000;
151 static const opus_int32 stereo_music_threshold = 24000;
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, 16000}, /* mono */
157 { 36000, 16000}, /* stereo */
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 */
168 int opus_encoder_get_size(int channels)
170 int silkEncSizeBytes, celtEncSizeBytes;
172 if (channels<1 || channels > 2)
174 ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
177 silkEncSizeBytes = align(silkEncSizeBytes);
178 celtEncSizeBytes = celt_encoder_get_size(channels);
179 return align(sizeof(OpusEncoder))+silkEncSizeBytes+celtEncSizeBytes;
182 int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int application)
185 CELTEncoder *celt_enc;
187 int ret, silkEncSizeBytes;
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))
194 OPUS_CLEAR((char*)st, opus_encoder_get_size(channels));
195 /* Create SILK encoder */
196 ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
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);
205 st->stream_channels = st->channels = channels;
209 st->arch = opus_select_arch();
211 ret = silk_InitEncoder( silk_enc, st->arch, &st->silk_mode );
212 if(ret)return OPUS_INTERNAL_ERROR;
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;
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;
235 celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0));
236 celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(st->silk_mode.complexity));
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;
252 st->variable_duration = OPUS_FRAMESIZE_ARG;
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;
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 );
262 st->mode = MODE_HYBRID;
263 st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
265 #ifndef DISABLE_FLOAT_API
266 tonality_analysis_init(&st->analysis);
272 static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channels)
277 while (framerate < 400)
282 if (mode == MODE_SILK_ONLY)
284 toc = (bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5;
285 toc |= (period-2)<<3;
286 } else if (mode == MODE_CELT_ONLY)
288 int tmp = bandwidth-OPUS_BANDWIDTH_MEDIUMBAND;
297 toc |= (bandwidth-OPUS_BANDWIDTH_SUPERWIDEBAND)<<4;
298 toc |= (period-2)<<3;
300 toc |= (channels==2)<<2;
305 static void silk_biquad_float(
306 const opus_val16 *in, /* I: Input signal */
307 const opus_int32 *B_Q28, /* I: MA coefficients [3] */
308 const opus_int32 *A_Q28, /* I: AR coefficients [2] */
309 opus_val32 *S, /* I/O: State vector [2] */
310 opus_val16 *out, /* O: Output signal */
311 const opus_int32 len, /* I: Signal length (must be even) */
315 /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */
319 opus_val32 A[2], B[3];
321 A[0] = (opus_val32)(A_Q28[0] * (1.f/((opus_int32)1<<28)));
322 A[1] = (opus_val32)(A_Q28[1] * (1.f/((opus_int32)1<<28)));
323 B[0] = (opus_val32)(B_Q28[0] * (1.f/((opus_int32)1<<28)));
324 B[1] = (opus_val32)(B_Q28[1] * (1.f/((opus_int32)1<<28)));
325 B[2] = (opus_val32)(B_Q28[2] * (1.f/((opus_int32)1<<28)));
327 /* Negate A_Q28 values and split in two parts */
329 for( k = 0; k < len; k++ ) {
330 /* S[ 0 ], S[ 1 ]: Q12 */
331 inval = in[ k*stride ];
332 vout = S[ 0 ] + B[0]*inval;
334 S[ 0 ] = S[1] - vout*A[0] + B[1]*inval;
336 S[ 1 ] = - vout*A[1] + B[2]*inval + VERY_SMALL;
338 /* Scale back to Q0 and saturate */
339 out[ k*stride ] = vout;
344 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)
346 opus_int32 B_Q28[ 3 ], A_Q28[ 2 ];
347 opus_int32 Fc_Q19, r_Q28, r_Q22;
349 silk_assert( cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ) );
350 Fc_Q19 = silk_DIV32_16( silk_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 );
351 silk_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 );
353 r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - silk_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q19 );
355 /* b = r * [ 1; -2; 1 ]; */
356 /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */
358 B_Q28[ 1 ] = silk_LSHIFT( -r_Q28, 1 );
361 /* -r * ( 2 - Fc * Fc ); */
362 r_Q22 = silk_RSHIFT( r_Q28, 6 );
363 A_Q28[ 0 ] = silk_SMULWW( r_Q22, silk_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CONST( 2.0, 22 ) );
364 A_Q28[ 1 ] = silk_SMULWW( r_Q22, r_Q22 );
367 silk_biquad_alt( in, B_Q28, A_Q28, hp_mem, out, len, channels );
368 if( channels == 2 ) {
369 silk_biquad_alt( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels );
372 silk_biquad_float( in, B_Q28, A_Q28, hp_mem, out, len, channels );
373 if( channels == 2 ) {
374 silk_biquad_float( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels );
380 static void dc_reject(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs)
385 /* Approximates -round(log2(4.*cutoff_Hz/Fs)) */
386 shift=celt_ilog2(Fs/(cutoff_Hz*3));
387 for (c=0;c<channels;c++)
391 opus_val32 x, tmp, y;
392 x = SHL32(EXTEND32(in[channels*i+c]), 14);
395 hp_mem[2*c] = hp_mem[2*c] + PSHR32(x - hp_mem[2*c], shift);
397 y = tmp - hp_mem[2*c+1];
398 hp_mem[2*c+1] = hp_mem[2*c+1] + PSHR32(tmp - hp_mem[2*c+1], shift);
399 out[channels*i+c] = EXTRACT16(SATURATE(PSHR32(y, 14), 32767));
405 static void dc_reject(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs)
409 coef = 4.0f*cutoff_Hz/Fs;
413 float m0, m1, m2, m3;
420 opus_val32 x0, x1, tmp0, tmp1, y0, y1;
426 m0 = coef*x0 + VERY_SMALL + coef2*m0;
427 m2 = coef*x1 + VERY_SMALL + coef2*m2;
431 m1 = coef*tmp0 + VERY_SMALL + coef2*m1;
432 m3 = coef*tmp1 + VERY_SMALL + coef2*m3;
446 opus_val32 x, tmp, y;
450 m0 = coef*x + VERY_SMALL + coef2*m0;
453 m1 = coef*tmp + VERY_SMALL + coef2*m1;
462 static void stereo_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, opus_val16 g2,
463 int overlap48, int frame_size, int channels, const opus_val16 *window, opus_int32 Fs)
469 overlap=overlap48/inc;
472 for (i=0;i<overlap;i++)
476 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
477 g = SHR32(MAC16_16(MULT16_16(w,g2),
479 diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]));
480 diff = MULT16_16_Q15(g, diff);
481 out[i*channels] = out[i*channels] - diff;
482 out[i*channels+1] = out[i*channels+1] + diff;
484 for (;i<frame_size;i++)
487 diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]));
488 diff = MULT16_16_Q15(g2, diff);
489 out[i*channels] = out[i*channels] - diff;
490 out[i*channels+1] = out[i*channels+1] + diff;
494 static void gain_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, opus_val16 g2,
495 int overlap48, int frame_size, int channels, const opus_val16 *window, opus_int32 Fs)
502 overlap=overlap48/inc;
505 for (i=0;i<overlap;i++)
508 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
509 g = SHR32(MAC16_16(MULT16_16(w,g2),
511 out[i] = MULT16_16_Q15(g, in[i]);
514 for (i=0;i<overlap;i++)
517 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
518 g = SHR32(MAC16_16(MULT16_16(w,g2),
520 out[i*2] = MULT16_16_Q15(g, in[i*2]);
521 out[i*2+1] = MULT16_16_Q15(g, in[i*2+1]);
525 for (i=overlap;i<frame_size;i++)
527 out[i*channels+c] = MULT16_16_Q15(g2, in[i*channels+c]);
530 while (++c<channels);
533 OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error)
537 if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2)||
538 (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO
539 && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY))
542 *error = OPUS_BAD_ARG;
545 st = (OpusEncoder *)opus_alloc(opus_encoder_get_size(channels));
549 *error = OPUS_ALLOC_FAIL;
552 ret = opus_encoder_init(st, Fs, channels, application);
563 static opus_int32 user_bitrate_to_bitrate(OpusEncoder *st, int frame_size, int max_data_bytes)
565 if(!frame_size)frame_size=st->Fs/400;
566 if (st->user_bitrate_bps==OPUS_AUTO)
567 return 60*st->Fs/frame_size + st->Fs*st->channels;
568 else if (st->user_bitrate_bps==OPUS_BITRATE_MAX)
569 return max_data_bytes*8*st->Fs/frame_size;
571 return st->user_bitrate_bps;
574 #ifndef DISABLE_FLOAT_API
575 /* Don't use more than 60 ms for the frame size analysis */
576 #define MAX_DYNAMIC_FRAMESIZE 24
577 /* Estimates how much the bitrate will be boosted based on the sub-frame energy */
578 static float transient_boost(const float *E, const float *E_1, int LM, int maxM)
582 float sumE=0, sumE_1=0;
585 M = IMIN(maxM, (1<<LM)+1);
591 metric = sumE*sumE_1/(M*M);
593 printf("%f\n", metric);*/
594 /*return metric>10 ? 1 : 0;*/
595 /*return MAX16(0,1-exp(-.25*(metric-2.)));*/
596 return MIN16(1,(float)sqrt(MAX16(0,.05f*(metric-2))));
599 /* Viterbi decoding trying to find the best frame size combination using look-ahead
619 static int transient_viterbi(const float *E, const float *E_1, int N, int frame_cost, int rate)
622 float cost[MAX_DYNAMIC_FRAMESIZE][16];
623 int states[MAX_DYNAMIC_FRAMESIZE][16];
627 /* Take into account that we damp VBR in the 32 kb/s to 64 kb/s range. */
633 factor = (rate-80.f)/80.f;
634 /* Makes variable framesize less aggressive at lower bitrates, but I can't
635 find any valid theoretical justification for this (other than it seems
639 /* Impossible state */
645 cost[0][1<<i] = (frame_cost + rate*(1<<i))*(1+factor*transient_boost(E, E_1, i, N+1));
652 /* Follow continuations */
655 cost[i][j] = cost[i-1][j-1];
666 min_cost = cost[i-1][1];
669 float tmp = cost[i-1][(1<<(k+1))-1];
672 states[i][1<<j] = (1<<(k+1))-1;
676 curr_cost = (frame_cost + rate*(1<<j))*(1+factor*transient_boost(E+i, E_1+i, j, N-i+1));
677 cost[i][1<<j] = min_cost;
678 /* If part of the frame is outside the analysis window, only count part of the cost */
680 cost[i][1<<j] += curr_cost*(float)(N-i)/(1<<j);
682 cost[i][1<<j] += curr_cost;
687 best_cost = cost[N-1][1];
688 /* Find best end state (doesn't force a frame to end at N-1) */
691 if (cost[N-1][i]<best_cost)
693 best_cost = cost[N-1][i];
698 /* Follow transitions back */
701 /*printf("%d ", best_state);*/
702 best_state = states[i][best_state];
704 /*printf("%d\n", best_state);*/
708 static int optimize_framesize(const void *x, int len, int C, opus_int32 Fs,
709 int bitrate, opus_val16 tonality, float *mem, int buffering,
710 downmix_func downmix)
714 float e[MAX_DYNAMIC_FRAMESIZE+4];
715 float e_1[MAX_DYNAMIC_FRAMESIZE+3];
721 VARDECL(opus_val32, sub);
724 ALLOC(sub, subframe, opus_val32);
726 e_1[0]=1.f/(EPSILON+mem[0]);
729 /* Consider the CELT delay when not in restricted-lowdelay */
730 /* We assume the buffering is between 2.5 and 5 ms */
731 offset = 2*subframe - buffering;
732 celt_assert(offset>=0 && offset <= subframe);
735 e_1[1]=1.f/(EPSILON+mem[1]);
737 e_1[2]=1.f/(EPSILON+mem[2]);
743 N=IMIN(len/subframe, MAX_DYNAMIC_FRAMESIZE);
744 /* Just silencing a warning, it's really initialized later */
753 downmix(x, sub, subframe, i*subframe+offset, 0, -2, C);
756 for (j=0;j<subframe;j++)
759 tmp += (tmpx-memx)*(float)(tmpx-memx);
763 e_1[i+pos] = 1.f/tmp;
765 /* Hack to get 20 ms working with APPLICATION_AUDIO
766 The real problem is that the corresponding memory needs to use 1.5 ms
767 from this frame and 1 ms from the next frame */
768 e[i+pos] = e[i+pos-1];
770 N=IMIN(MAX_DYNAMIC_FRAMESIZE, N+2);
771 bestLM = transient_viterbi(e, e_1, N, (int)((1.f+.5f*tonality)*(60*C+40)), bitrate/400);
772 mem[0] = e[1<<bestLM];
775 mem[1] = e[(1<<bestLM)+1];
776 mem[2] = e[(1<<bestLM)+2];
783 #ifndef DISABLE_FLOAT_API
785 #define PCM2VAL(x) FLOAT2INT16(x)
787 #define PCM2VAL(x) SCALEIN(x)
789 void downmix_float(const void *_x, opus_val32 *sub, int subframe, int offset, int c1, int c2, int C)
794 x = (const float *)_x;
795 for (j=0;j<subframe;j++)
796 sub[j] = PCM2VAL(x[(j+offset)*C+c1]);
799 for (j=0;j<subframe;j++)
800 sub[j] += PCM2VAL(x[(j+offset)*C+c2]);
806 for (j=0;j<subframe;j++)
807 sub[j] += PCM2VAL(x[(j+offset)*C+c]);
811 scale = (1<<SIG_SHIFT);
819 for (j=0;j<subframe;j++)
824 void downmix_int(const void *_x, opus_val32 *sub, int subframe, int offset, int c1, int c2, int C)
829 x = (const opus_int16 *)_x;
830 for (j=0;j<subframe;j++)
831 sub[j] = x[(j+offset)*C+c1];
834 for (j=0;j<subframe;j++)
835 sub[j] += x[(j+offset)*C+c2];
841 for (j=0;j<subframe;j++)
842 sub[j] += x[(j+offset)*C+c];
846 scale = (1<<SIG_SHIFT);
854 for (j=0;j<subframe;j++)
858 opus_int32 frame_size_select(opus_int32 frame_size, int variable_duration, opus_int32 Fs)
861 if (frame_size<Fs/400)
863 if (variable_duration == OPUS_FRAMESIZE_ARG)
864 new_size = frame_size;
865 else if (variable_duration == OPUS_FRAMESIZE_VARIABLE)
867 else if (variable_duration >= OPUS_FRAMESIZE_2_5_MS && variable_duration <= OPUS_FRAMESIZE_120_MS)
869 if (variable_duration <= OPUS_FRAMESIZE_40_MS)
870 new_size = (Fs/400)<<(variable_duration-OPUS_FRAMESIZE_2_5_MS);
872 new_size = (variable_duration-OPUS_FRAMESIZE_2_5_MS-2)*Fs/50;
876 if (new_size>frame_size)
878 if (400*new_size!=Fs && 200*new_size!=Fs && 100*new_size!=Fs &&
879 50*new_size!=Fs && 25*new_size!=Fs && 50*new_size!=3*Fs &&
880 50*new_size!=4*Fs && 50*new_size!=5*Fs && 50*new_size!=6*Fs)
885 opus_int32 compute_frame_size(const void *analysis_pcm, int frame_size,
886 int variable_duration, int C, opus_int32 Fs, int bitrate_bps,
887 int delay_compensation, downmix_func downmix
888 #ifndef DISABLE_FLOAT_API
889 , float *subframe_mem
893 #ifndef DISABLE_FLOAT_API
894 if (variable_duration == OPUS_FRAMESIZE_VARIABLE && frame_size >= Fs/200)
897 LM = optimize_framesize(analysis_pcm, frame_size, C, Fs, bitrate_bps,
898 0, subframe_mem, delay_compensation, downmix);
899 while ((Fs/400<<LM)>frame_size)
901 frame_size = (Fs/400<<LM);
907 (void)delay_compensation;
911 frame_size = frame_size_select(frame_size, variable_duration, Fs);
918 opus_val16 compute_stereo_width(const opus_val16 *pcm, int frame_size, opus_int32 Fs, StereoWidthState *mem)
920 opus_val32 xx, xy, yy;
921 opus_val16 sqrt_xx, sqrt_yy;
922 opus_val16 qrrt_xx, qrrt_yy;
925 opus_val16 short_alpha;
927 frame_rate = Fs/frame_size;
928 short_alpha = Q15ONE - MULT16_16(25, Q15ONE)/IMAX(50,frame_rate);
930 /* Unroll by 4. The frame size is always a multiple of 4 *except* for
931 2.5 ms frames at 12 kHz. Since this setting is very rare (and very
932 stupid), we just discard the last two samples. */
933 for (i=0;i<frame_size-3;i+=4)
941 pxx = SHR32(MULT16_16(x,x),2);
942 pxy = SHR32(MULT16_16(x,y),2);
943 pyy = SHR32(MULT16_16(y,y),2);
946 pxx += SHR32(MULT16_16(x,x),2);
947 pxy += SHR32(MULT16_16(x,y),2);
948 pyy += SHR32(MULT16_16(y,y),2);
951 pxx += SHR32(MULT16_16(x,x),2);
952 pxy += SHR32(MULT16_16(x,y),2);
953 pyy += SHR32(MULT16_16(y,y),2);
956 pxx += SHR32(MULT16_16(x,x),2);
957 pxy += SHR32(MULT16_16(x,y),2);
958 pyy += SHR32(MULT16_16(y,y),2);
960 xx += SHR32(pxx, 10);
961 xy += SHR32(pxy, 10);
962 yy += SHR32(pyy, 10);
964 mem->XX += MULT16_32_Q15(short_alpha, xx-mem->XX);
965 mem->XY += MULT16_32_Q15(short_alpha, xy-mem->XY);
966 mem->YY += MULT16_32_Q15(short_alpha, yy-mem->YY);
967 mem->XX = MAX32(0, mem->XX);
968 mem->XY = MAX32(0, mem->XY);
969 mem->YY = MAX32(0, mem->YY);
970 if (MAX32(mem->XX, mem->YY)>QCONST16(8e-4f, 18))
975 sqrt_xx = celt_sqrt(mem->XX);
976 sqrt_yy = celt_sqrt(mem->YY);
977 qrrt_xx = celt_sqrt(sqrt_xx);
978 qrrt_yy = celt_sqrt(sqrt_yy);
979 /* Inter-channel correlation */
980 mem->XY = MIN32(mem->XY, sqrt_xx*sqrt_yy);
981 corr = SHR32(frac_div32(mem->XY,EPSILON+MULT16_16(sqrt_xx,sqrt_yy)),16);
982 /* Approximate loudness difference */
983 ldiff = MULT16_16(Q15ONE, ABS16(qrrt_xx-qrrt_yy))/(EPSILON+qrrt_xx+qrrt_yy);
984 width = MULT16_16_Q15(celt_sqrt(QCONST32(1.f,30)-MULT16_16(corr,corr)), ldiff);
985 /* Smoothing over one second */
986 mem->smoothed_width += (width-mem->smoothed_width)/frame_rate;
988 mem->max_follower = MAX16(mem->max_follower-QCONST16(.02f,15)/frame_rate, mem->smoothed_width);
990 /*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);*/
991 return EXTRACT16(MIN32(Q15ONE, MULT16_16(20, mem->max_follower)));
994 static int decide_fec(int useInBandFEC, int PacketLoss_perc, int last_fec, int mode, int *bandwidth, opus_int32 rate)
997 if (!useInBandFEC || PacketLoss_perc == 0 || mode == MODE_CELT_ONLY)
999 orig_bandwidth = *bandwidth;
1002 opus_int32 hysteresis;
1003 opus_int32 LBRR_rate_thres_bps;
1004 /* Compute threshold for using FEC at the current bandwidth setting */
1005 LBRR_rate_thres_bps = fec_thresholds[2*(*bandwidth - OPUS_BANDWIDTH_NARROWBAND)];
1006 hysteresis = fec_thresholds[2*(*bandwidth - OPUS_BANDWIDTH_NARROWBAND) + 1];
1007 if (last_fec == 1) LBRR_rate_thres_bps -= hysteresis;
1008 if (last_fec == 0) LBRR_rate_thres_bps += hysteresis;
1009 LBRR_rate_thres_bps = silk_SMULWB( silk_MUL( LBRR_rate_thres_bps,
1010 125 - silk_min( PacketLoss_perc, 25 ) ), SILK_FIX_CONST( 0.01, 16 ) );
1011 /* If loss <= 5%, we look at whether we have enough rate to enable FEC.
1012 If loss > 5%, we decrease the bandwidth until we can enable FEC. */
1013 if (rate > LBRR_rate_thres_bps)
1015 else if (PacketLoss_perc <= 5)
1017 else if (*bandwidth > OPUS_BANDWIDTH_NARROWBAND)
1022 /* Couldn't find any bandwidth to enable FEC, keep original bandwidth. */
1023 *bandwidth = orig_bandwidth;
1027 static int compute_silk_rate_for_hybrid(int rate, int bandwidth, int frame20ms, int vbr, int fec) {
1032 static int rate_table[][5] = {
1033 /* |total| |-------- SILK------------|
1034 |-- No FEC -| |--- FEC ---|
1035 10ms 20ms 10ms 20ms */
1037 {12000, 10000, 10000, 11000, 11000},
1038 {16000, 13500, 13500, 15000, 15000},
1039 {20000, 16000, 16000, 18000, 18000},
1040 {24000, 18000, 18000, 21000, 21000},
1041 {32000, 22000, 22000, 28000, 28000},
1042 {64000, 38000, 38000, 50000, 50000}
1044 entry = 1 + frame20ms + 2*fec;
1045 N = sizeof(rate_table)/sizeof(rate_table[0]);
1048 if (rate_table[i][0] > rate) break;
1052 silk_rate = rate_table[i-1][entry];
1053 /* For now, just give 50% of the extra bits to SILK. */
1054 silk_rate += (rate-rate_table[i-1][0])/2;
1056 opus_int32 lo, hi, x0, x1;
1057 lo = rate_table[i-1][entry];
1058 hi = rate_table[i][entry];
1059 x0 = rate_table[i-1][0];
1060 x1 = rate_table[i][0];
1061 silk_rate = (lo*(x1-rate) + hi*(rate-x0))/(x1-x0);
1065 /* Tiny boost to SILK for CBR. We should probably tune this better. */
1068 if (bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND)
1073 /* Returns the equivalent bitrate corresponding to 20 ms frames,
1074 complexity 10 VBR operation. */
1075 static opus_int32 compute_equiv_rate(opus_int32 bitrate, int channels,
1076 int frame_rate, int vbr, int mode, int complexity, int loss)
1080 /* Take into account overhead from smaller frames. */
1081 equiv -= (40*channels+20)*(frame_rate - 50);
1082 /* CBR is about a 8% penalty for both SILK and CELT. */
1085 /* Complexity makes about 10% difference (from 0 to 10) in general. */
1086 equiv = equiv * (90+complexity)/100;
1087 if (mode == MODE_SILK_ONLY || mode == MODE_HYBRID)
1089 /* SILK complexity 0-1 uses the non-delayed-decision NSQ, which
1093 equiv -= equiv*loss/(6*loss + 10);
1094 } else if (mode == MODE_CELT_ONLY) {
1095 /* CELT complexity 0-4 doesn't have the pitch filter, which costs
1100 /* Mode not known yet */
1101 /* Half the SILK loss*/
1102 equiv -= equiv*loss/(12*loss + 20);
1107 #ifndef DISABLE_FLOAT_API
1109 static int is_digital_silence(const opus_val16* pcm, int frame_size, int channels, int lsb_depth)
1112 opus_val32 sample_max = 0;
1114 sample_max = celt_maxabs16(pcm, frame_size*channels);
1117 silence = (sample_max == 0);
1120 silence = (sample_max <= (opus_val16) 1 / (1 << lsb_depth));
1127 static opus_val32 compute_frame_energy(const opus_val16 *pcm, int frame_size, int channels, int arch)
1130 opus_val32 sample_max;
1133 opus_val32 energy = 0;
1134 int len = frame_size*channels;
1136 /* Max amplitude in the signal */
1137 sample_max = celt_maxabs16(pcm, len);
1139 /* Compute the right shift required in the MAC to avoid an overflow */
1140 max_shift = celt_ilog2(len);
1141 shift = IMAX(0, (celt_ilog2(sample_max) << 1) + max_shift - 28);
1143 /* Compute the energy */
1144 for (i=0; i<len; i++)
1145 energy += SHR32(MULT16_16(pcm[i], pcm[i]), shift);
1147 /* Normalize energy by the frame size and left-shift back to the original position */
1149 energy = SHL32(energy, shift);
1154 static opus_val32 compute_frame_energy(const opus_val16 *pcm, int frame_size, int channels, int arch)
1156 int len = frame_size*channels;
1157 return celt_inner_prod(pcm, pcm, len, arch)/len;
1161 /* Decides if DTX should be turned on (=1) or off (=0) */
1162 static int decide_dtx_mode(float activity_probability, /* probability that current frame contains speech/music */
1163 int *nb_no_activity_frames, /* number of consecutive frames with no activity */
1164 opus_val32 peak_signal_energy, /* peak energy of desired signal detected so far */
1165 const opus_val16 *pcm, /* input pcm signal */
1166 int frame_size, /* frame size */
1168 int is_silence, /* only digital silence detected in this frame */
1173 opus_val32 noise_energy;
1174 int is_sufficiently_quiet;
1178 is_noise = activity_probability < DTX_ACTIVITY_THRESHOLD;
1181 noise_energy = compute_frame_energy(pcm, frame_size, channels, arch);
1182 is_sufficiently_quiet = peak_signal_energy >= (PSEUDO_SNR_THRESHOLD * noise_energy);
1186 if (is_silence || (is_noise && is_sufficiently_quiet))
1188 /* The number of consecutive DTX frames should be within the allowed bounds */
1189 (*nb_no_activity_frames)++;
1191 if (*nb_no_activity_frames > NB_SPEECH_FRAMES_BEFORE_DTX)
1193 if (*nb_no_activity_frames <= (NB_SPEECH_FRAMES_BEFORE_DTX + MAX_CONSECUTIVE_DTX))
1194 /* Valid frame for DTX! */
1197 (*nb_no_activity_frames) = NB_SPEECH_FRAMES_BEFORE_DTX;
1200 (*nb_no_activity_frames) = 0;
1207 static opus_int32 encode_multiframe_packet(OpusEncoder *st,
1208 const opus_val16 *pcm,
1211 unsigned char *data,
1212 opus_int32 out_data_bytes,
1219 VARDECL(unsigned char, tmp_data);
1220 int bak_mode, bak_bandwidth, bak_channels, bak_to_mono;
1221 VARDECL(OpusRepacketizer, rp);
1222 int max_header_bytes;
1223 opus_int32 bytes_per_frame;
1224 opus_int32 cbr_bytes;
1225 opus_int32 repacketize_len;
1230 * 2 frames: Code 2 with different compressed sizes
1231 * >2 frames: Code 3 VBR */
1232 max_header_bytes = nb_frames == 2 ? 3 : (2+(nb_frames-1)*2);
1234 if (st->use_vbr || st->user_bitrate_bps==OPUS_BITRATE_MAX)
1235 repacketize_len = out_data_bytes;
1237 cbr_bytes = 3*st->bitrate_bps/(3*8*st->Fs/(frame_size*nb_frames));
1238 repacketize_len = IMIN(cbr_bytes, out_data_bytes);
1240 bytes_per_frame = IMIN(1276, 1+(repacketize_len-max_header_bytes)/nb_frames);
1242 ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char);
1243 ALLOC(rp, 1, OpusRepacketizer);
1244 opus_repacketizer_init(rp);
1246 bak_mode = st->user_forced_mode;
1247 bak_bandwidth = st->user_bandwidth;
1248 bak_channels = st->force_channels;
1250 st->user_forced_mode = st->mode;
1251 st->user_bandwidth = st->bandwidth;
1252 st->force_channels = st->stream_channels;
1254 bak_to_mono = st->silk_mode.toMono;
1256 st->force_channels = 1;
1258 st->prev_channels = st->stream_channels;
1260 for (i=0;i<nb_frames;i++)
1262 st->silk_mode.toMono = 0;
1263 st->nonfinal_frame = i<(nb_frames-1);
1265 /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */
1266 if (to_celt && i==nb_frames-1)
1267 st->user_forced_mode = MODE_CELT_ONLY;
1269 tmp_len = opus_encode_native(st, pcm+i*(st->channels*frame_size), frame_size,
1270 tmp_data+i*bytes_per_frame, bytes_per_frame, lsb_depth, NULL, 0, 0, 0, 0,
1276 return OPUS_INTERNAL_ERROR;
1279 ret = opus_repacketizer_cat(rp, tmp_data+i*bytes_per_frame, tmp_len);
1284 return OPUS_INTERNAL_ERROR;
1288 /* If encoding multiframes recursively, the true number of frames is rp->nb_frames. */
1289 ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketize_len, 0, !st->use_vbr);
1294 return OPUS_INTERNAL_ERROR;
1297 /* Discard configs that were forced locally for the purpose of repacketization */
1298 st->user_forced_mode = bak_mode;
1299 st->user_bandwidth = bak_bandwidth;
1300 st->force_channels = bak_channels;
1301 st->silk_mode.toMono = bak_to_mono;
1307 opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
1308 unsigned char *data, opus_int32 out_data_bytes, int lsb_depth,
1309 const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2,
1310 int analysis_channels, downmix_func downmix, int float_api)
1313 CELTEncoder *celt_enc;
1322 int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */
1323 int celt_to_silk = 0;
1324 VARDECL(opus_val16, pcm_buf);
1327 opus_uint32 redundant_rng = 0;
1328 int cutoff_Hz, hp_freq_smth1;
1329 int voice_est; /* Probability of voice in Q7 */
1330 opus_int32 equiv_rate;
1331 int delay_compensation;
1333 opus_int32 max_rate; /* Max bitrate we're allowed to use */
1336 opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */
1338 opus_val16 stereo_width;
1339 const CELTMode *celt_mode;
1340 #ifndef DISABLE_FLOAT_API
1341 AnalysisInfo analysis_info;
1342 int analysis_read_pos_bak=-1;
1343 int analysis_read_subframe_bak=-1;
1346 VARDECL(opus_val16, tmp_prefill);
1350 max_data_bytes = IMIN(1276, out_data_bytes);
1353 if ((!st->variable_duration && 400*frame_size != st->Fs && 200*frame_size != st->Fs && 100*frame_size != st->Fs &&
1354 50*frame_size != st->Fs && 25*frame_size != st->Fs && 50*frame_size != 3*st->Fs && 50*frame_size != 4*st->Fs &&
1355 50*frame_size != 5*st->Fs && 50*frame_size != 6*st->Fs)
1356 || (400*frame_size < st->Fs)
1357 || max_data_bytes<=0
1361 return OPUS_BAD_ARG;
1364 /* Cannot encode 100 ms in 1 byte */
1365 if (max_data_bytes==1 && st->Fs==(frame_size*10))
1368 return OPUS_BUFFER_TOO_SMALL;
1371 silk_enc = (char*)st+st->silk_enc_offset;
1372 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1373 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1374 delay_compensation = 0;
1376 delay_compensation = st->delay_compensation;
1378 lsb_depth = IMIN(lsb_depth, st->lsb_depth);
1380 celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1381 #ifndef DISABLE_FLOAT_API
1382 analysis_info.valid = 0;
1384 if (st->silk_mode.complexity >= 10 && st->Fs==48000)
1386 if (st->silk_mode.complexity >= 7 && st->Fs==48000)
1389 if (is_digital_silence(pcm, frame_size, st->channels, lsb_depth))
1393 analysis_read_pos_bak = st->analysis.read_pos;
1394 analysis_read_subframe_bak = st->analysis.read_subframe;
1395 run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame_size,
1396 c1, c2, analysis_channels, st->Fs,
1397 lsb_depth, downmix, &analysis_info);
1400 /* Track the peak signal energy */
1401 if (!is_silence && analysis_info.activity_probability > DTX_ACTIVITY_THRESHOLD)
1402 st->peak_signal_energy = MAX32(MULT16_32_Q15(QCONST16(0.999, 15), st->peak_signal_energy),
1403 compute_frame_energy(pcm, frame_size, st->channels, st->arch));
1407 (void)analysis_size;
1410 #ifndef DISABLE_FLOAT_API
1411 /* Reset voice_ratio if this frame is not silent or if analysis is disabled.
1412 * Otherwise, preserve voice_ratio from the last non-silent frame */
1414 st->voice_ratio = -1;
1416 st->detected_bandwidth = 0;
1417 if (analysis_info.valid)
1419 int analysis_bandwidth;
1420 if (st->signal_type == OPUS_AUTO)
1421 st->voice_ratio = (int)floor(.5+100*(1-analysis_info.music_prob));
1423 analysis_bandwidth = analysis_info.bandwidth;
1424 if (analysis_bandwidth<=12)
1425 st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1426 else if (analysis_bandwidth<=14)
1427 st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1428 else if (analysis_bandwidth<=16)
1429 st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1430 else if (analysis_bandwidth<=18)
1431 st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1433 st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1436 st->voice_ratio = -1;
1439 if (st->channels==2 && st->force_channels!=1)
1440 stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_mem);
1443 total_buffer = delay_compensation;
1444 st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
1446 frame_rate = st->Fs/frame_size;
1450 /* Multiply by 3 to make sure the division is exact. */
1451 int frame_rate6 = 6*st->Fs/frame_size;
1452 /* We need to make sure that "int" values always fit in 16 bits. */
1453 cbrBytes = IMIN( (6*st->bitrate_bps/8 + frame_rate6/2)/frame_rate6, max_data_bytes);
1454 st->bitrate_bps = cbrBytes*(opus_int32)frame_rate6*8/6;
1455 /* Make sure we provide at least one byte to avoid failing. */
1456 max_data_bytes = IMAX(1, cbrBytes);
1458 if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8
1459 || (frame_rate<50 && (max_data_bytes*frame_rate<300 || st->bitrate_bps < 2400)))
1461 /*If the space is too low to do something useful, emit 'PLC' frames.*/
1462 int tocmode = st->mode;
1463 int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth;
1464 int packet_code = 0;
1465 int num_multiframes = 0;
1468 tocmode = MODE_SILK_ONLY;
1470 tocmode = MODE_CELT_ONLY;
1471 /* 40 ms -> 2 x 20 ms if in CELT_ONLY or HYBRID mode */
1472 if (frame_rate==25 && tocmode!=MODE_SILK_ONLY)
1478 /* >= 60 ms frames */
1481 /* 1 x 60 ms, 2 x 40 ms, 2 x 60 ms */
1482 if (out_data_bytes==1 || (tocmode==MODE_SILK_ONLY && frame_rate!=10))
1484 tocmode = MODE_SILK_ONLY;
1486 packet_code = frame_rate <= 12;
1487 frame_rate = frame_rate == 12 ? 25 : 16;
1491 num_multiframes = 50/frame_rate;
1497 if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND)
1498 bw=OPUS_BANDWIDTH_WIDEBAND;
1499 else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND)
1500 bw=OPUS_BANDWIDTH_NARROWBAND;
1501 else if (tocmode==MODE_HYBRID&&bw<=OPUS_BANDWIDTH_SUPERWIDEBAND)
1502 bw=OPUS_BANDWIDTH_SUPERWIDEBAND;
1504 data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels);
1505 data[0] |= packet_code;
1507 ret = packet_code <= 1 ? 1 : 2;
1509 max_data_bytes = IMAX(max_data_bytes, ret);
1512 data[1] = num_multiframes;
1516 ret = opus_packet_pad(data, ret, max_data_bytes);
1518 ret = max_data_bytes;
1520 ret = OPUS_INTERNAL_ERROR;
1525 max_rate = frame_rate*max_data_bytes*8;
1527 /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */
1528 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->channels, st->Fs/frame_size,
1529 st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1531 if (st->signal_type == OPUS_SIGNAL_VOICE)
1533 else if (st->signal_type == OPUS_SIGNAL_MUSIC)
1535 else if (st->voice_ratio >= 0)
1537 voice_est = st->voice_ratio*327>>8;
1538 /* For AUDIO, never be more than 90% confident of having speech */
1539 if (st->application == OPUS_APPLICATION_AUDIO)
1540 voice_est = IMIN(voice_est, 115);
1541 } else if (st->application == OPUS_APPLICATION_VOIP)
1546 if (st->force_channels!=OPUS_AUTO && st->channels == 2)
1548 st->stream_channels = st->force_channels;
1551 /* Random mono/stereo decision */
1552 if (st->channels == 2 && (rand()&0x1F)==0)
1553 st->stream_channels = 3-st->stream_channels;
1555 /* Rate-dependent mono-stereo decision */
1556 if (st->channels == 2)
1558 opus_int32 stereo_threshold;
1559 stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14);
1560 if (st->stream_channels == 2)
1561 stereo_threshold -= 1000;
1563 stereo_threshold += 1000;
1564 st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
1566 st->stream_channels = st->channels;
1570 /* Update equivalent rate for channels decision. */
1571 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1572 st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1574 /* Mode selection depending on application and signal type */
1575 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1577 st->mode = MODE_CELT_ONLY;
1578 } else if (st->user_forced_mode == OPUS_AUTO)
1581 /* Random mode switching */
1582 if ((rand()&0xF)==0)
1584 if ((rand()&0x1)==0)
1585 st->mode = MODE_CELT_ONLY;
1587 st->mode = MODE_SILK_ONLY;
1589 if (st->prev_mode==MODE_CELT_ONLY)
1590 st->mode = MODE_CELT_ONLY;
1592 st->mode = MODE_SILK_ONLY;
1595 opus_int32 mode_voice, mode_music;
1596 opus_int32 threshold;
1598 /* Interpolate based on stereo width */
1599 mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[0][0])
1600 + MULT16_32_Q15(stereo_width,mode_thresholds[1][0]));
1601 mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[1][1])
1602 + MULT16_32_Q15(stereo_width,mode_thresholds[1][1]));
1603 /* Interpolate based on speech/music probability */
1604 threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14);
1605 /* Bias towards SILK for VoIP because of some useful features */
1606 if (st->application == OPUS_APPLICATION_VOIP)
1609 /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/
1611 if (st->prev_mode == MODE_CELT_ONLY)
1613 else if (st->prev_mode>0)
1616 st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY;
1618 /* When FEC is enabled and there's enough packet loss, use SILK */
1619 if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (128-voice_est)>>4)
1620 st->mode = MODE_SILK_ONLY;
1621 /* When encoding voice and DTX is enabled but the generalized DTX cannot be used,
1622 because of complexity and sampling frequency settings, switch to SILK DTX and
1623 set the encoder to SILK mode */
1624 #ifndef DISABLE_FLOAT_API
1625 st->silk_mode.useDTX = st->use_dtx && !(analysis_info.valid || is_silence);
1627 st->silk_mode.useDTX = st->use_dtx;
1629 if (st->silk_mode.useDTX && voice_est > 100)
1630 st->mode = MODE_SILK_ONLY;
1633 /* If max_data_bytes represents less than 6 kb/s, switch to CELT-only mode */
1634 if (max_data_bytes < (frame_rate > 50 ? 9000 : 6000)*frame_size / (st->Fs * 8))
1635 st->mode = MODE_CELT_ONLY;
1637 st->mode = st->user_forced_mode;
1640 /* Override the chosen mode to make sure we meet the requested frame size */
1641 if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100)
1642 st->mode = MODE_CELT_ONLY;
1644 st->mode = MODE_CELT_ONLY;
1646 if (st->prev_mode > 0 &&
1647 ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ||
1648 (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)))
1651 celt_to_silk = (st->mode != MODE_CELT_ONLY);
1654 /* Switch to SILK/hybrid if frame size is 10 ms or more*/
1655 if (frame_size >= st->Fs/100)
1657 st->mode = st->prev_mode;
1665 /* When encoding multiframes, we can ask for a switch to CELT only in the last frame. This switch
1666 * is processed above as the requested mode shouldn't interrupt stereo->mono transition. */
1667 if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0
1668 && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)
1670 /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */
1671 st->silk_mode.toMono = 1;
1672 st->stream_channels = 2;
1674 st->silk_mode.toMono = 0;
1677 /* Update equivalent rate with mode decision. */
1678 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1679 st->use_vbr, st->mode, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1681 /* For the first frame at a new SILK bandwidth */
1682 if (st->silk_bw_switch)
1686 st->silk_bw_switch = 0;
1692 /* Fair share of the max size allowed */
1693 redundancy_bytes = IMIN(257, max_data_bytes*(opus_int32)(st->Fs/200)/(frame_size+st->Fs/200));
1694 /* For VBR, target the actual bitrate (subject to the limit above) */
1696 redundancy_bytes = IMIN(redundancy_bytes, st->bitrate_bps/1600);
1699 if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
1701 silk_EncControlStruct dummy;
1702 silk_InitEncoder( silk_enc, st->arch, &dummy);
1706 /* Automatic (rate-dependent) bandwidth selection */
1707 if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch)
1709 const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds;
1710 opus_int32 bandwidth_thresholds[8];
1711 int bandwidth = OPUS_BANDWIDTH_FULLBAND;
1713 if (st->channels==2 && st->force_channels!=1)
1715 voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds;
1716 music_bandwidth_thresholds = stereo_music_bandwidth_thresholds;
1718 voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds;
1719 music_bandwidth_thresholds = mono_music_bandwidth_thresholds;
1721 /* Interpolate bandwidth thresholds depending on voice estimation */
1724 bandwidth_thresholds[i] = music_bandwidth_thresholds[i]
1725 + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14);
1728 int threshold, hysteresis;
1729 threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)];
1730 hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1];
1733 if (st->auto_bandwidth >= bandwidth)
1734 threshold -= hysteresis;
1736 threshold += hysteresis;
1738 if (equiv_rate >= threshold)
1740 } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND);
1741 st->bandwidth = st->auto_bandwidth = bandwidth;
1742 /* Prevents any transition to SWB/FB until the SILK layer has fully
1743 switched to WB mode and turned the variable LP filter off */
1744 if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1745 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1748 if (st->bandwidth>st->max_bandwidth)
1749 st->bandwidth = st->max_bandwidth;
1751 if (st->user_bandwidth != OPUS_AUTO)
1752 st->bandwidth = st->user_bandwidth;
1754 /* This prevents us from using hybrid at unsafe CBR/max rates */
1755 if (st->mode != MODE_CELT_ONLY && max_rate < 15000)
1757 st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND);
1760 /* Prevents Opus from wasting bits on frequencies that are above
1761 the Nyquist rate of the input signal */
1762 if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND)
1763 st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1764 if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1765 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1766 if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND)
1767 st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1768 if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
1769 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1770 #ifndef DISABLE_FLOAT_API
1771 /* Use detected bandwidth to reduce the encoded bandwidth. */
1772 if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO)
1774 int min_detected_bandwidth;
1775 /* Makes bandwidth detection more conservative just in case the detector
1776 gets it wrong when we could have coded a high bandwidth transparently.
1777 When operating in SILK/hybrid mode, we don't go below wideband to avoid
1778 more complicated switches that require redundancy. */
1779 if (equiv_rate <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1780 min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1781 else if (equiv_rate <= 24000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1782 min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1783 else if (equiv_rate <= 30000*st->stream_channels)
1784 min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1785 else if (equiv_rate <= 44000*st->stream_channels)
1786 min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1788 min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1790 st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwidth);
1791 st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth);
1794 st->silk_mode.LBRR_coded = decide_fec(st->silk_mode.useInBandFEC, st->silk_mode.packetLossPercentage,
1795 st->silk_mode.LBRR_coded, st->mode, &st->bandwidth, equiv_rate);
1796 celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth));
1798 /* CELT mode doesn't support mediumband, use wideband instead */
1799 if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1800 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1802 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1804 curr_bandwidth = st->bandwidth;
1806 /* Chooses the appropriate mode for speech
1807 *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */
1808 if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1809 st->mode = MODE_HYBRID;
1810 if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND)
1811 st->mode = MODE_SILK_ONLY;
1813 /* Can't support higher than >60 ms frames, and >20 ms when in Hybrid or CELT-only modes */
1814 if ((frame_size > st->Fs/50 && (st->mode != MODE_SILK_ONLY)) || frame_size > 3*st->Fs/50)
1819 if (st->mode == MODE_SILK_ONLY)
1821 if (frame_size == 2*st->Fs/25) /* 80 ms -> 2x 40 ms */
1822 enc_frame_size = st->Fs/25;
1823 if (frame_size == 3*st->Fs/25) /* 120 ms -> 2x 60 ms */
1824 enc_frame_size = 3*st->Fs/50;
1825 else /* 100 ms -> 5x 20 ms */
1826 enc_frame_size = st->Fs/50;
1829 enc_frame_size = st->Fs/50;
1831 nb_frames = frame_size/enc_frame_size;
1833 #ifndef DISABLE_FLOAT_API
1834 if (analysis_read_pos_bak!= -1)
1836 st->analysis.read_pos = analysis_read_pos_bak;
1837 st->analysis.read_subframe = analysis_read_subframe_bak;
1841 ret = encode_multiframe_packet(st, pcm, nb_frames, enc_frame_size, data,
1842 out_data_bytes, to_celt, lsb_depth, float_api);
1848 /* If we decided to go with CELT, make sure redundancy is off, no matter what
1849 we decided earlier. */
1850 if (st->mode == MODE_CELT_ONLY)
1853 redundancy_bytes = 0;
1856 /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */
1857 bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1;
1861 ec_enc_init(&enc, data, max_data_bytes-1);
1863 ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_val16);
1864 OPUS_COPY(pcm_buf, &st->delay_buffer[(st->encoder_buffer-total_buffer)*st->channels], total_buffer*st->channels);
1866 if (st->mode == MODE_CELT_ONLY)
1867 hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
1869 hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15;
1871 st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15,
1872 hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) );
1874 /* convert from log scale to Hertz */
1875 cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
1877 if (st->application == OPUS_APPLICATION_VOIP)
1879 hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
1881 dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
1887 sum = celt_inner_prod(&pcm_buf[total_buffer*st->channels], &pcm_buf[total_buffer*st->channels], frame_size*st->channels, st->arch);
1888 /* This should filter out both NaNs and ridiculous signals that could
1889 cause NaNs further down. */
1890 if (!(sum < 1e9f) || celt_isnan(sum))
1892 OPUS_CLEAR(&pcm_buf[total_buffer*st->channels], frame_size*st->channels);
1893 st->hp_mem[0] = st->hp_mem[1] = st->hp_mem[2] = st->hp_mem[3] = 0;
1899 /* SILK processing */
1901 if (st->mode != MODE_CELT_ONLY)
1903 opus_int32 total_bitRate, celt_rate;
1905 const opus_int16 *pcm_silk;
1907 VARDECL(opus_int16, pcm_silk);
1908 ALLOC(pcm_silk, st->channels*frame_size, opus_int16);
1911 /* Distribute bits between SILK and CELT */
1912 total_bitRate = 8 * bytes_target * frame_rate;
1913 if( st->mode == MODE_HYBRID ) {
1914 /* Base rate for SILK */
1915 st->silk_mode.bitRate = compute_silk_rate_for_hybrid(total_bitRate,
1916 curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded);
1917 if (!st->energy_masking)
1919 /* Increasingly attenuate high band when it gets allocated fewer bits */
1920 celt_rate = total_bitRate - st->silk_mode.bitRate;
1921 HB_gain = Q15ONE - SHR32(celt_exp2(-celt_rate * QCONST16(1.f/1024, 10)), 1);
1924 /* SILK gets all bits */
1925 st->silk_mode.bitRate = total_bitRate;
1928 /* Surround masking for SILK */
1929 if (st->energy_masking && st->use_vbr && !st->lfe)
1931 opus_val32 mask_sum=0;
1932 opus_val16 masking_depth;
1933 opus_int32 rate_offset;
1936 opus_int16 srate = 16000;
1937 if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
1941 } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1946 for (c=0;c<st->channels;c++)
1951 mask = MAX16(MIN16(st->energy_masking[21*c+i],
1952 QCONST16(.5f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT));
1954 mask = HALF16(mask);
1958 /* Conservative rate reduction, we cut the masking in half */
1959 masking_depth = mask_sum / end*st->channels;
1960 masking_depth += QCONST16(.2f, DB_SHIFT);
1961 rate_offset = (opus_int32)PSHR32(MULT16_16(srate, masking_depth), DB_SHIFT);
1962 rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3);
1963 /* Split the rate change between the SILK and CELT part for hybrid. */
1964 if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPUS_BANDWIDTH_FULLBAND)
1965 st->silk_mode.bitRate += 3*rate_offset/5;
1967 st->silk_mode.bitRate += rate_offset;
1968 bytes_target += rate_offset * frame_size / (8 * st->Fs);
1971 st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
1972 st->silk_mode.nChannelsAPI = st->channels;
1973 st->silk_mode.nChannelsInternal = st->stream_channels;
1974 if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
1975 st->silk_mode.desiredInternalSampleRate = 8000;
1976 } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
1977 st->silk_mode.desiredInternalSampleRate = 12000;
1979 silk_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND );
1980 st->silk_mode.desiredInternalSampleRate = 16000;
1982 if( st->mode == MODE_HYBRID ) {
1983 /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
1984 st->silk_mode.minInternalSampleRate = 16000;
1986 st->silk_mode.minInternalSampleRate = 8000;
1989 st->silk_mode.maxInternalSampleRate = 16000;
1990 if (st->mode == MODE_SILK_ONLY)
1992 opus_int32 effective_max_rate = max_rate;
1993 if (frame_rate > 50)
1994 effective_max_rate = effective_max_rate*2/3;
1995 if (effective_max_rate < 8000)
1997 st->silk_mode.maxInternalSampleRate = 12000;
1998 st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate);
2000 if (effective_max_rate < 7000)
2002 st->silk_mode.maxInternalSampleRate = 8000;
2003 st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate);
2007 st->silk_mode.useCBR = !st->use_vbr;
2009 /* Call SILK encoder for the low band */
2011 /* Max bits for SILK, counting ToC, redundancy bytes, and optionally redundancy. */
2012 st->silk_mode.maxBits = IMIN(1275, max_data_bytes-1-redundancy_bytes)*8;
2015 /* Counting 1 bit for redundancy position and 20 bits for flag+size (only for hybrid). */
2016 st->silk_mode.maxBits -= 1;
2017 if (st->mode == MODE_HYBRID)
2018 st->silk_mode.maxBits -= 20;
2020 if (st->silk_mode.useCBR)
2022 if (st->mode == MODE_HYBRID)
2024 st->silk_mode.maxBits = IMIN(st->silk_mode.maxBits, st->silk_mode.bitRate * frame_size / st->Fs);
2027 /* Constrained VBR. */
2028 if (st->mode == MODE_HYBRID)
2030 /* Compute SILK bitrate corresponding to the max total bits available */
2031 opus_int32 maxBitRate = compute_silk_rate_for_hybrid(st->silk_mode.maxBits*st->Fs / frame_size,
2032 curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded);
2033 st->silk_mode.maxBits = maxBitRate * frame_size / st->Fs;
2041 /* Use a smooth onset for the SILK prefill to avoid the encoder trying to encode
2042 a discontinuity. The exact location is what we need to avoid leaving any "gap"
2043 in the audio when mixing with the redundant CELT frame. Here we can afford to
2044 overwrite st->delay_buffer because the only thing that uses it before it gets
2045 rewritten is tmp_prefill[] and even then only the part after the ramp really
2046 gets used (rather than sent to the encoder and discarded) */
2047 prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensation-st->Fs/400);
2048 gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_offset,
2049 0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_mode->window, st->Fs);
2050 OPUS_CLEAR(st->delay_buffer, prefill_offset);
2052 pcm_silk = st->delay_buffer;
2054 for (i=0;i<st->encoder_buffer*st->channels;i++)
2055 pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]);
2057 silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, 1 );
2061 pcm_silk = pcm_buf+total_buffer*st->channels;
2063 for (i=0;i<frame_size*st->channels;i++)
2064 pcm_silk[i] = FLOAT2INT16(pcm_buf[total_buffer*st->channels + i]);
2066 ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0 );
2068 /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
2071 return OPUS_INTERNAL_ERROR;
2074 /* Extract SILK internal bandwidth for signaling in first byte */
2075 if( st->mode == MODE_SILK_ONLY ) {
2076 if( st->silk_mode.internalSampleRate == 8000 ) {
2077 curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
2078 } else if( st->silk_mode.internalSampleRate == 12000 ) {
2079 curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
2080 } else if( st->silk_mode.internalSampleRate == 16000 ) {
2081 curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
2084 silk_assert( st->silk_mode.internalSampleRate == 16000 );
2087 st->silk_mode.opusCanSwitch = st->silk_mode.switchReady && !st->nonfinal_frame;
2092 data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2097 /* FIXME: How do we allocate the redundancy for CBR? */
2098 if (st->silk_mode.opusCanSwitch)
2102 st->silk_bw_switch = 1;
2106 /* CELT processing */
2110 switch(curr_bandwidth)
2112 case OPUS_BANDWIDTH_NARROWBAND:
2115 case OPUS_BANDWIDTH_MEDIUMBAND:
2116 case OPUS_BANDWIDTH_WIDEBAND:
2119 case OPUS_BANDWIDTH_SUPERWIDEBAND:
2122 case OPUS_BANDWIDTH_FULLBAND:
2126 celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband));
2127 celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels));
2129 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2130 if (st->mode != MODE_SILK_ONLY)
2132 opus_val32 celt_pred=2;
2133 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2134 /* We may still decide to disable prediction later */
2135 if (st->silk_mode.reducedDependency)
2137 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred));
2139 if (st->mode == MODE_HYBRID)
2143 len = (ec_tell(&enc)+7)>>3;
2145 len += st->mode == MODE_HYBRID ? 3 : 1;
2147 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps-st->silk_mode.bitRate));
2148 nb_compr_bytes = max_data_bytes-1-redundancy_bytes;
2149 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0));
2151 /* check if SILK used up too much */
2152 nb_compr_bytes = len > bytes_target ? len : bytes_target;
2158 #ifndef DISABLE_FLOAT_API
2159 if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != st->Fs/50)
2161 bonus = (60*st->stream_channels+40)*(st->Fs/frame_size-50);
2162 if (analysis_info.valid)
2163 bonus = (opus_int32)(bonus*(1.f+.5f*analysis_info.tonality));
2166 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
2167 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint));
2168 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps+bonus));
2169 nb_compr_bytes = max_data_bytes-1-redundancy_bytes;
2171 nb_compr_bytes = bytes_target;
2179 ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16);
2180 if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0)
2182 OPUS_COPY(tmp_prefill, &st->delay_buffer[(st->encoder_buffer-total_buffer-st->Fs/400)*st->channels], st->channels*st->Fs/400);
2185 if (st->channels*(st->encoder_buffer-(frame_size+total_buffer)) > 0)
2187 OPUS_MOVE(st->delay_buffer, &st->delay_buffer[st->channels*frame_size], st->channels*(st->encoder_buffer-frame_size-total_buffer));
2188 OPUS_COPY(&st->delay_buffer[st->channels*(st->encoder_buffer-frame_size-total_buffer)],
2190 (frame_size+total_buffer)*st->channels);
2192 OPUS_COPY(st->delay_buffer, &pcm_buf[(frame_size+total_buffer-st->encoder_buffer)*st->channels], st->encoder_buffer*st->channels);
2194 /* gain_fade() and stereo_fade() need to be after the buffer copying
2195 because we don't want any of this to affect the SILK part */
2196 if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) {
2197 gain_fade(pcm_buf, pcm_buf,
2198 st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs);
2200 st->prev_HB_gain = HB_gain;
2201 if (st->mode != MODE_HYBRID || st->stream_channels==1)
2202 st->silk_mode.stereoWidth_Q14 = IMIN((1<<14),2*IMAX(0,equiv_rate-24000));
2203 if( !st->energy_masking && st->channels == 2 ) {
2204 /* Apply stereo width reduction (at low bitrates) */
2205 if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) {
2207 g1 = st->hybrid_stereo_width_Q14;
2208 g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
2210 g1 = g1==16384 ? Q15ONE : SHL16(g1,1);
2211 g2 = g2==16384 ? Q15ONE : SHL16(g2,1);
2216 stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
2217 frame_size, st->channels, celt_mode->window, st->Fs);
2218 st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
2222 if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) <= 8*(max_data_bytes-1))
2224 /* For SILK mode, the redundancy is inferred from the length */
2225 if (st->mode == MODE_HYBRID && (redundancy || ec_tell(&enc)+37 <= 8*nb_compr_bytes))
2226 ec_enc_bit_logp(&enc, redundancy, 12);
2230 ec_enc_bit_logp(&enc, celt_to_silk, 1);
2231 if (st->mode == MODE_HYBRID)
2232 max_redundancy = (max_data_bytes-1)-nb_compr_bytes;
2234 max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3);
2235 /* Target the same bit-rate for redundancy as for the rest,
2236 up to a max of 257 bytes */
2237 redundancy_bytes = IMIN(max_redundancy, st->bitrate_bps/1600);
2238 redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes));
2239 if (st->mode == MODE_HYBRID)
2240 ec_enc_uint(&enc, redundancy_bytes-2, 256);
2248 st->silk_bw_switch = 0;
2249 redundancy_bytes = 0;
2251 if (st->mode != MODE_CELT_ONLY)start_band=17;
2253 if (st->mode == MODE_SILK_ONLY)
2255 ret = (ec_tell(&enc)+7)>>3;
2257 nb_compr_bytes = ret;
2259 nb_compr_bytes = IMIN((max_data_bytes-1)-redundancy_bytes, nb_compr_bytes);
2260 ec_enc_shrink(&enc, nb_compr_bytes);
2263 #ifndef DISABLE_FLOAT_API
2264 if (redundancy || st->mode != MODE_SILK_ONLY)
2265 celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(&analysis_info));
2267 if (st->mode == MODE_HYBRID) {
2269 info.signalType = st->silk_mode.signalType;
2270 info.offset = st->silk_mode.offset;
2271 celt_encoder_ctl(celt_enc, CELT_SET_SILK_INFO(&info));
2273 celt_encoder_ctl(celt_enc, CELT_SET_SILK_INFO((SILKInfo*)NULL));
2276 /* 5 ms redundant frame for CELT->SILK */
2277 if (redundancy && celt_to_silk)
2280 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2281 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2282 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2283 err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL);
2287 return OPUS_INTERNAL_ERROR;
2289 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2290 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2293 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band));
2295 if (st->mode != MODE_SILK_ONLY)
2297 if (st->mode != st->prev_mode && st->prev_mode > 0)
2299 unsigned char dummy[2];
2300 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2303 celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL);
2304 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2306 /* If false, we already busted the budget and we'll end up with a "PLC packet" */
2307 if (ec_tell(&enc) <= 8*nb_compr_bytes)
2309 /* Set the bitrate again if it was overridden in the redundancy code above*/
2310 if (redundancy && celt_to_silk && st->mode==MODE_HYBRID && st->use_vbr)
2311 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps-st->silk_mode.bitRate));
2312 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(st->use_vbr));
2313 ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc);
2317 return OPUS_INTERNAL_ERROR;
2319 /* Put CELT->SILK redundancy data in the right place. */
2320 if (redundancy && celt_to_silk && st->mode==MODE_HYBRID && st->use_vbr)
2322 OPUS_MOVE(data+ret, data+nb_compr_bytes, redundancy_bytes);
2323 nb_compr_bytes = nb_compr_bytes+redundancy_bytes;
2328 /* 5 ms redundant frame for SILK->CELT */
2329 if (redundancy && !celt_to_silk)
2332 unsigned char dummy[2];
2337 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2338 celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2339 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2340 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2341 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2343 if (st->mode == MODE_HYBRID)
2345 /* Shrink packet to what the encoder actually used. */
2346 nb_compr_bytes = ret;
2347 ec_enc_shrink(&enc, nb_compr_bytes);
2349 /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */
2350 celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL);
2352 err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL);
2356 return OPUS_INTERNAL_ERROR;
2358 celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2363 /* Signalling the mode in the first byte */
2365 data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2367 st->rangeFinal = enc.rng ^ redundant_rng;
2370 st->prev_mode = MODE_CELT_ONLY;
2372 st->prev_mode = st->mode;
2373 st->prev_channels = st->stream_channels;
2374 st->prev_framesize = frame_size;
2379 #ifndef DISABLE_FLOAT_API
2380 if (st->use_dtx && (analysis_info.valid || is_silence))
2382 if (decide_dtx_mode(analysis_info.activity_probability, &st->nb_no_activity_frames,
2383 st->peak_signal_energy, pcm, frame_size, st->channels, is_silence, st->arch))
2386 data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2393 /* In the unlikely case that the SILK encoder busted its target, tell
2394 the decoder to call the PLC */
2395 if (ec_tell(&enc) > (max_data_bytes-1)*8)
2397 if (max_data_bytes < 2)
2400 return OPUS_BUFFER_TOO_SMALL;
2405 } else if (st->mode==MODE_SILK_ONLY&&!redundancy)
2407 /*When in LPC only mode it's perfectly
2408 reasonable to strip off trailing zero bytes as
2409 the required range decoder behavior is to
2410 fill these in. This can't be done when the MDCT
2411 modes are used because the decoder needs to know
2412 the actual length for allocation purposes.*/
2413 while(ret>2&&data[ret]==0)ret--;
2415 /* Count ToC and redundancy */
2416 ret += 1+redundancy_bytes;
2419 if (opus_packet_pad(data, ret, max_data_bytes) != OPUS_OK)
2423 return OPUS_INTERNAL_ERROR;
2425 ret = max_data_bytes;
2433 #ifndef DISABLE_FLOAT_API
2434 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
2435 unsigned char *data, opus_int32 max_data_bytes)
2439 int delay_compensation;
2440 VARDECL(opus_int16, in);
2443 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2444 delay_compensation = 0;
2446 delay_compensation = st->delay_compensation;
2447 frame_size = compute_frame_size(pcm, analysis_frame_size,
2448 st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
2449 delay_compensation, downmix_float, st->analysis.subframe_mem);
2451 ALLOC(in, frame_size*st->channels, opus_int16);
2453 for (i=0;i<frame_size*st->channels;i++)
2454 in[i] = FLOAT2INT16(pcm[i]);
2455 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
2456 pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1);
2462 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
2463 unsigned char *data, opus_int32 out_data_bytes)
2466 int delay_compensation;
2467 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2468 delay_compensation = 0;
2470 delay_compensation = st->delay_compensation;
2471 frame_size = compute_frame_size(pcm, analysis_frame_size,
2472 st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
2473 delay_compensation, downmix_int
2474 #ifndef DISABLE_FLOAT_API
2475 , st->analysis.subframe_mem
2478 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 16,
2479 pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 0);
2483 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
2484 unsigned char *data, opus_int32 max_data_bytes)
2488 int delay_compensation;
2492 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2493 delay_compensation = 0;
2495 delay_compensation = st->delay_compensation;
2496 frame_size = compute_frame_size(pcm, analysis_frame_size,
2497 st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
2498 delay_compensation, downmix_int, st->analysis.subframe_mem);
2500 ALLOC(in, frame_size*st->channels, float);
2502 for (i=0;i<frame_size*st->channels;i++)
2503 in[i] = (1.0f/32768)*pcm[i];
2504 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
2505 pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 0);
2509 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
2510 unsigned char *data, opus_int32 out_data_bytes)
2513 int delay_compensation;
2514 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2515 delay_compensation = 0;
2517 delay_compensation = st->delay_compensation;
2518 frame_size = compute_frame_size(pcm, analysis_frame_size,
2519 st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
2520 delay_compensation, downmix_float, st->analysis.subframe_mem);
2521 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 24,
2522 pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1);
2527 int opus_encoder_ctl(OpusEncoder *st, int request, ...)
2530 CELTEncoder *celt_enc;
2534 va_start(ap, request);
2536 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
2540 case OPUS_SET_APPLICATION_REQUEST:
2542 opus_int32 value = va_arg(ap, opus_int32);
2543 if ( (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_AUDIO
2544 && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2545 || (!st->first && st->application != value))
2550 st->application = value;
2553 case OPUS_GET_APPLICATION_REQUEST:
2555 opus_int32 *value = va_arg(ap, opus_int32*);
2560 *value = st->application;
2563 case OPUS_SET_BITRATE_REQUEST:
2565 opus_int32 value = va_arg(ap, opus_int32);
2566 if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX)
2570 else if (value <= 500)
2572 else if (value > (opus_int32)300000*st->channels)
2573 value = (opus_int32)300000*st->channels;
2575 st->user_bitrate_bps = value;
2578 case OPUS_GET_BITRATE_REQUEST:
2580 opus_int32 *value = va_arg(ap, opus_int32*);
2585 *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276);
2588 case OPUS_SET_FORCE_CHANNELS_REQUEST:
2590 opus_int32 value = va_arg(ap, opus_int32);
2591 if((value<1 || value>st->channels) && value != OPUS_AUTO)
2595 st->force_channels = value;
2598 case OPUS_GET_FORCE_CHANNELS_REQUEST:
2600 opus_int32 *value = va_arg(ap, opus_int32*);
2605 *value = st->force_channels;
2608 case OPUS_SET_MAX_BANDWIDTH_REQUEST:
2610 opus_int32 value = va_arg(ap, opus_int32);
2611 if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND)
2615 st->max_bandwidth = value;
2616 if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2617 st->silk_mode.maxInternalSampleRate = 8000;
2618 } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2619 st->silk_mode.maxInternalSampleRate = 12000;
2621 st->silk_mode.maxInternalSampleRate = 16000;
2625 case OPUS_GET_MAX_BANDWIDTH_REQUEST:
2627 opus_int32 *value = va_arg(ap, opus_int32*);
2632 *value = st->max_bandwidth;
2635 case OPUS_SET_BANDWIDTH_REQUEST:
2637 opus_int32 value = va_arg(ap, opus_int32);
2638 if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO)
2642 st->user_bandwidth = value;
2643 if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2644 st->silk_mode.maxInternalSampleRate = 8000;
2645 } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2646 st->silk_mode.maxInternalSampleRate = 12000;
2648 st->silk_mode.maxInternalSampleRate = 16000;
2652 case OPUS_GET_BANDWIDTH_REQUEST:
2654 opus_int32 *value = va_arg(ap, opus_int32*);
2659 *value = st->bandwidth;
2662 case OPUS_SET_DTX_REQUEST:
2664 opus_int32 value = va_arg(ap, opus_int32);
2665 if(value<0 || value>1)
2669 st->use_dtx = value;
2672 case OPUS_GET_DTX_REQUEST:
2674 opus_int32 *value = va_arg(ap, opus_int32*);
2679 *value = st->use_dtx;
2682 case OPUS_SET_COMPLEXITY_REQUEST:
2684 opus_int32 value = va_arg(ap, opus_int32);
2685 if(value<0 || value>10)
2689 st->silk_mode.complexity = value;
2690 celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value));
2693 case OPUS_GET_COMPLEXITY_REQUEST:
2695 opus_int32 *value = va_arg(ap, opus_int32*);
2700 *value = st->silk_mode.complexity;
2703 case OPUS_SET_INBAND_FEC_REQUEST:
2705 opus_int32 value = va_arg(ap, opus_int32);
2706 if(value<0 || value>1)
2710 st->silk_mode.useInBandFEC = value;
2713 case OPUS_GET_INBAND_FEC_REQUEST:
2715 opus_int32 *value = va_arg(ap, opus_int32*);
2720 *value = st->silk_mode.useInBandFEC;
2723 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2725 opus_int32 value = va_arg(ap, opus_int32);
2726 if (value < 0 || value > 100)
2730 st->silk_mode.packetLossPercentage = value;
2731 celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value));
2734 case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
2736 opus_int32 *value = va_arg(ap, opus_int32*);
2741 *value = st->silk_mode.packetLossPercentage;
2744 case OPUS_SET_VBR_REQUEST:
2746 opus_int32 value = va_arg(ap, opus_int32);
2747 if(value<0 || value>1)
2751 st->use_vbr = value;
2752 st->silk_mode.useCBR = 1-value;
2755 case OPUS_GET_VBR_REQUEST:
2757 opus_int32 *value = va_arg(ap, opus_int32*);
2762 *value = st->use_vbr;
2765 case OPUS_SET_VOICE_RATIO_REQUEST:
2767 opus_int32 value = va_arg(ap, opus_int32);
2768 if (value<-1 || value>100)
2772 st->voice_ratio = value;
2775 case OPUS_GET_VOICE_RATIO_REQUEST:
2777 opus_int32 *value = va_arg(ap, opus_int32*);
2782 *value = st->voice_ratio;
2785 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2787 opus_int32 value = va_arg(ap, opus_int32);
2788 if(value<0 || value>1)
2792 st->vbr_constraint = value;
2795 case OPUS_GET_VBR_CONSTRAINT_REQUEST:
2797 opus_int32 *value = va_arg(ap, opus_int32*);
2802 *value = st->vbr_constraint;
2805 case OPUS_SET_SIGNAL_REQUEST:
2807 opus_int32 value = va_arg(ap, opus_int32);
2808 if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNAL_MUSIC)
2812 st->signal_type = value;
2815 case OPUS_GET_SIGNAL_REQUEST:
2817 opus_int32 *value = va_arg(ap, opus_int32*);
2822 *value = st->signal_type;
2825 case OPUS_GET_LOOKAHEAD_REQUEST:
2827 opus_int32 *value = va_arg(ap, opus_int32*);
2832 *value = st->Fs/400;
2833 if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2834 *value += st->delay_compensation;
2837 case OPUS_GET_SAMPLE_RATE_REQUEST:
2839 opus_int32 *value = va_arg(ap, opus_int32*);
2847 case OPUS_GET_FINAL_RANGE_REQUEST:
2849 opus_uint32 *value = va_arg(ap, opus_uint32*);
2854 *value = st->rangeFinal;
2857 case OPUS_SET_LSB_DEPTH_REQUEST:
2859 opus_int32 value = va_arg(ap, opus_int32);
2860 if (value<8 || value>24)
2864 st->lsb_depth=value;
2867 case OPUS_GET_LSB_DEPTH_REQUEST:
2869 opus_int32 *value = va_arg(ap, opus_int32*);
2874 *value = st->lsb_depth;
2877 case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST:
2879 opus_int32 value = va_arg(ap, opus_int32);
2880 if (value != OPUS_FRAMESIZE_ARG && value != OPUS_FRAMESIZE_2_5_MS &&
2881 value != OPUS_FRAMESIZE_5_MS && value != OPUS_FRAMESIZE_10_MS &&
2882 value != OPUS_FRAMESIZE_20_MS && value != OPUS_FRAMESIZE_40_MS &&
2883 value != OPUS_FRAMESIZE_60_MS && value != OPUS_FRAMESIZE_80_MS &&
2884 value != OPUS_FRAMESIZE_100_MS && value != OPUS_FRAMESIZE_120_MS &&
2885 value != OPUS_FRAMESIZE_VARIABLE)
2889 st->variable_duration = value;
2890 celt_encoder_ctl(celt_enc, OPUS_SET_EXPERT_FRAME_DURATION(value));
2893 case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST:
2895 opus_int32 *value = va_arg(ap, opus_int32*);
2900 *value = st->variable_duration;
2903 case OPUS_SET_PREDICTION_DISABLED_REQUEST:
2905 opus_int32 value = va_arg(ap, opus_int32);
2906 if (value > 1 || value < 0)
2908 st->silk_mode.reducedDependency = value;
2911 case OPUS_GET_PREDICTION_DISABLED_REQUEST:
2913 opus_int32 *value = va_arg(ap, opus_int32*);
2916 *value = st->silk_mode.reducedDependency;
2919 case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
2921 opus_int32 value = va_arg(ap, opus_int32);
2922 if(value<0 || value>1)
2926 celt_encoder_ctl(celt_enc, OPUS_SET_PHASE_INVERSION_DISABLED(value));
2929 case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
2931 opus_int32 *value = va_arg(ap, opus_int32*);
2936 celt_encoder_ctl(celt_enc, OPUS_GET_PHASE_INVERSION_DISABLED(value));
2939 case OPUS_RESET_STATE:
2942 silk_EncControlStruct dummy;
2944 silk_enc = (char*)st+st->silk_enc_offset;
2945 #ifndef DISABLE_FLOAT_API
2946 tonality_analysis_reset(&st->analysis);
2949 start = (char*)&st->OPUS_ENCODER_RESET_START;
2950 OPUS_CLEAR(start, sizeof(OpusEncoder) - (start - (char*)st));
2952 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2953 silk_InitEncoder( silk_enc, st->arch, &dummy );
2954 st->stream_channels = st->channels;
2955 st->hybrid_stereo_width_Q14 = 1 << 14;
2956 st->prev_HB_gain = Q15ONE;
2958 st->mode = MODE_HYBRID;
2959 st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
2960 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
2963 case OPUS_SET_FORCE_MODE_REQUEST:
2965 opus_int32 value = va_arg(ap, opus_int32);
2966 if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO)
2970 st->user_forced_mode = value;
2973 case OPUS_SET_LFE_REQUEST:
2975 opus_int32 value = va_arg(ap, opus_int32);
2977 ret = celt_encoder_ctl(celt_enc, OPUS_SET_LFE(value));
2980 case OPUS_SET_ENERGY_MASK_REQUEST:
2982 opus_val16 *value = va_arg(ap, opus_val16*);
2983 st->energy_masking = value;
2984 ret = celt_encoder_ctl(celt_enc, OPUS_SET_ENERGY_MASK(value));
2988 case CELT_GET_MODE_REQUEST:
2990 const CELTMode ** value = va_arg(ap, const CELTMode**);
2995 ret = celt_encoder_ctl(celt_enc, CELT_GET_MODE(value));
2999 /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/
3000 ret = OPUS_UNIMPLEMENTED;
3007 return OPUS_BAD_ARG;
3010 void opus_encoder_destroy(OpusEncoder *st)