1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libopus/src/opus_encoder.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,2488 @@ 1.4 +/* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited 1.5 + Written by Jean-Marc Valin and Koen Vos */ 1.6 +/* 1.7 + Redistribution and use in source and binary forms, with or without 1.8 + modification, are permitted provided that the following conditions 1.9 + are met: 1.10 + 1.11 + - Redistributions of source code must retain the above copyright 1.12 + notice, this list of conditions and the following disclaimer. 1.13 + 1.14 + - Redistributions in binary form must reproduce the above copyright 1.15 + notice, this list of conditions and the following disclaimer in the 1.16 + documentation and/or other materials provided with the distribution. 1.17 + 1.18 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.19 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.20 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.21 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 1.22 + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.23 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.24 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.25 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.26 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.27 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.28 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.29 +*/ 1.30 + 1.31 +#ifdef HAVE_CONFIG_H 1.32 +#include "config.h" 1.33 +#endif 1.34 + 1.35 +#include <stdarg.h> 1.36 +#include "celt.h" 1.37 +#include "entenc.h" 1.38 +#include "modes.h" 1.39 +#include "API.h" 1.40 +#include "stack_alloc.h" 1.41 +#include "float_cast.h" 1.42 +#include "opus.h" 1.43 +#include "arch.h" 1.44 +#include "opus_private.h" 1.45 +#include "os_support.h" 1.46 +#include "cpu_support.h" 1.47 +#include "analysis.h" 1.48 +#include "mathops.h" 1.49 +#include "tuning_parameters.h" 1.50 +#ifdef FIXED_POINT 1.51 +#include "fixed/structs_FIX.h" 1.52 +#else 1.53 +#include "float/structs_FLP.h" 1.54 +#endif 1.55 + 1.56 +#define MAX_ENCODER_BUFFER 480 1.57 + 1.58 +typedef struct { 1.59 + opus_val32 XX, XY, YY; 1.60 + opus_val16 smoothed_width; 1.61 + opus_val16 max_follower; 1.62 +} StereoWidthState; 1.63 + 1.64 +struct OpusEncoder { 1.65 + int celt_enc_offset; 1.66 + int silk_enc_offset; 1.67 + silk_EncControlStruct silk_mode; 1.68 + int application; 1.69 + int channels; 1.70 + int delay_compensation; 1.71 + int force_channels; 1.72 + int signal_type; 1.73 + int user_bandwidth; 1.74 + int max_bandwidth; 1.75 + int user_forced_mode; 1.76 + int voice_ratio; 1.77 + opus_int32 Fs; 1.78 + int use_vbr; 1.79 + int vbr_constraint; 1.80 + int variable_duration; 1.81 + opus_int32 bitrate_bps; 1.82 + opus_int32 user_bitrate_bps; 1.83 + int lsb_depth; 1.84 + int encoder_buffer; 1.85 + int lfe; 1.86 + 1.87 +#define OPUS_ENCODER_RESET_START stream_channels 1.88 + int stream_channels; 1.89 + opus_int16 hybrid_stereo_width_Q14; 1.90 + opus_int32 variable_HP_smth2_Q15; 1.91 + opus_val16 prev_HB_gain; 1.92 + opus_val32 hp_mem[4]; 1.93 + int mode; 1.94 + int prev_mode; 1.95 + int prev_channels; 1.96 + int prev_framesize; 1.97 + int bandwidth; 1.98 + int silk_bw_switch; 1.99 + /* Sampling rate (at the API level) */ 1.100 + int first; 1.101 + opus_val16 * energy_masking; 1.102 + StereoWidthState width_mem; 1.103 + opus_val16 delay_buffer[MAX_ENCODER_BUFFER*2]; 1.104 +#ifndef DISABLE_FLOAT_API 1.105 + TonalityAnalysisState analysis; 1.106 + int detected_bandwidth; 1.107 + int analysis_offset; 1.108 +#endif 1.109 + opus_uint32 rangeFinal; 1.110 + int arch; 1.111 +}; 1.112 + 1.113 +/* Transition tables for the voice and music. First column is the 1.114 + middle (memoriless) threshold. The second column is the hysteresis 1.115 + (difference with the middle) */ 1.116 +static const opus_int32 mono_voice_bandwidth_thresholds[8] = { 1.117 + 11000, 1000, /* NB<->MB */ 1.118 + 14000, 1000, /* MB<->WB */ 1.119 + 17000, 1000, /* WB<->SWB */ 1.120 + 21000, 2000, /* SWB<->FB */ 1.121 +}; 1.122 +static const opus_int32 mono_music_bandwidth_thresholds[8] = { 1.123 + 12000, 1000, /* NB<->MB */ 1.124 + 15000, 1000, /* MB<->WB */ 1.125 + 18000, 2000, /* WB<->SWB */ 1.126 + 22000, 2000, /* SWB<->FB */ 1.127 +}; 1.128 +static const opus_int32 stereo_voice_bandwidth_thresholds[8] = { 1.129 + 11000, 1000, /* NB<->MB */ 1.130 + 14000, 1000, /* MB<->WB */ 1.131 + 21000, 2000, /* WB<->SWB */ 1.132 + 28000, 2000, /* SWB<->FB */ 1.133 +}; 1.134 +static const opus_int32 stereo_music_bandwidth_thresholds[8] = { 1.135 + 12000, 1000, /* NB<->MB */ 1.136 + 18000, 2000, /* MB<->WB */ 1.137 + 21000, 2000, /* WB<->SWB */ 1.138 + 30000, 2000, /* SWB<->FB */ 1.139 +}; 1.140 +/* Threshold bit-rates for switching between mono and stereo */ 1.141 +static const opus_int32 stereo_voice_threshold = 30000; 1.142 +static const opus_int32 stereo_music_threshold = 30000; 1.143 + 1.144 +/* Threshold bit-rate for switching between SILK/hybrid and CELT-only */ 1.145 +static const opus_int32 mode_thresholds[2][2] = { 1.146 + /* voice */ /* music */ 1.147 + { 64000, 16000}, /* mono */ 1.148 + { 36000, 16000}, /* stereo */ 1.149 +}; 1.150 + 1.151 +int opus_encoder_get_size(int channels) 1.152 +{ 1.153 + int silkEncSizeBytes, celtEncSizeBytes; 1.154 + int ret; 1.155 + if (channels<1 || channels > 2) 1.156 + return 0; 1.157 + ret = silk_Get_Encoder_Size( &silkEncSizeBytes ); 1.158 + if (ret) 1.159 + return 0; 1.160 + silkEncSizeBytes = align(silkEncSizeBytes); 1.161 + celtEncSizeBytes = celt_encoder_get_size(channels); 1.162 + return align(sizeof(OpusEncoder))+silkEncSizeBytes+celtEncSizeBytes; 1.163 +} 1.164 + 1.165 +int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int application) 1.166 +{ 1.167 + void *silk_enc; 1.168 + CELTEncoder *celt_enc; 1.169 + int err; 1.170 + int ret, silkEncSizeBytes; 1.171 + 1.172 + if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2)|| 1.173 + (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO 1.174 + && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)) 1.175 + return OPUS_BAD_ARG; 1.176 + 1.177 + OPUS_CLEAR((char*)st, opus_encoder_get_size(channels)); 1.178 + /* Create SILK encoder */ 1.179 + ret = silk_Get_Encoder_Size( &silkEncSizeBytes ); 1.180 + if (ret) 1.181 + return OPUS_BAD_ARG; 1.182 + silkEncSizeBytes = align(silkEncSizeBytes); 1.183 + st->silk_enc_offset = align(sizeof(OpusEncoder)); 1.184 + st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes; 1.185 + silk_enc = (char*)st+st->silk_enc_offset; 1.186 + celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); 1.187 + 1.188 + st->stream_channels = st->channels = channels; 1.189 + 1.190 + st->Fs = Fs; 1.191 + 1.192 + st->arch = opus_select_arch(); 1.193 + 1.194 + ret = silk_InitEncoder( silk_enc, st->arch, &st->silk_mode ); 1.195 + if(ret)return OPUS_INTERNAL_ERROR; 1.196 + 1.197 + /* default SILK parameters */ 1.198 + st->silk_mode.nChannelsAPI = channels; 1.199 + st->silk_mode.nChannelsInternal = channels; 1.200 + st->silk_mode.API_sampleRate = st->Fs; 1.201 + st->silk_mode.maxInternalSampleRate = 16000; 1.202 + st->silk_mode.minInternalSampleRate = 8000; 1.203 + st->silk_mode.desiredInternalSampleRate = 16000; 1.204 + st->silk_mode.payloadSize_ms = 20; 1.205 + st->silk_mode.bitRate = 25000; 1.206 + st->silk_mode.packetLossPercentage = 0; 1.207 + st->silk_mode.complexity = 9; 1.208 + st->silk_mode.useInBandFEC = 0; 1.209 + st->silk_mode.useDTX = 0; 1.210 + st->silk_mode.useCBR = 0; 1.211 + st->silk_mode.reducedDependency = 0; 1.212 + 1.213 + /* Create CELT encoder */ 1.214 + /* Initialize CELT encoder */ 1.215 + err = celt_encoder_init(celt_enc, Fs, channels, st->arch); 1.216 + if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR; 1.217 + 1.218 + celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0)); 1.219 + celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(st->silk_mode.complexity)); 1.220 + 1.221 + st->use_vbr = 1; 1.222 + /* Makes constrained VBR the default (safer for real-time use) */ 1.223 + st->vbr_constraint = 1; 1.224 + st->user_bitrate_bps = OPUS_AUTO; 1.225 + st->bitrate_bps = 3000+Fs*channels; 1.226 + st->application = application; 1.227 + st->signal_type = OPUS_AUTO; 1.228 + st->user_bandwidth = OPUS_AUTO; 1.229 + st->max_bandwidth = OPUS_BANDWIDTH_FULLBAND; 1.230 + st->force_channels = OPUS_AUTO; 1.231 + st->user_forced_mode = OPUS_AUTO; 1.232 + st->voice_ratio = -1; 1.233 + st->encoder_buffer = st->Fs/100; 1.234 + st->lsb_depth = 24; 1.235 + st->variable_duration = OPUS_FRAMESIZE_ARG; 1.236 + 1.237 + /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead 1.238 + + 1.5 ms for SILK resamplers and stereo prediction) */ 1.239 + st->delay_compensation = st->Fs/250; 1.240 + 1.241 + st->hybrid_stereo_width_Q14 = 1 << 14; 1.242 + st->prev_HB_gain = Q15ONE; 1.243 + st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); 1.244 + st->first = 1; 1.245 + st->mode = MODE_HYBRID; 1.246 + st->bandwidth = OPUS_BANDWIDTH_FULLBAND; 1.247 + 1.248 + return OPUS_OK; 1.249 +} 1.250 + 1.251 +static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channels) 1.252 +{ 1.253 + int period; 1.254 + unsigned char toc; 1.255 + period = 0; 1.256 + while (framerate < 400) 1.257 + { 1.258 + framerate <<= 1; 1.259 + period++; 1.260 + } 1.261 + if (mode == MODE_SILK_ONLY) 1.262 + { 1.263 + toc = (bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5; 1.264 + toc |= (period-2)<<3; 1.265 + } else if (mode == MODE_CELT_ONLY) 1.266 + { 1.267 + int tmp = bandwidth-OPUS_BANDWIDTH_MEDIUMBAND; 1.268 + if (tmp < 0) 1.269 + tmp = 0; 1.270 + toc = 0x80; 1.271 + toc |= tmp << 5; 1.272 + toc |= period<<3; 1.273 + } else /* Hybrid */ 1.274 + { 1.275 + toc = 0x60; 1.276 + toc |= (bandwidth-OPUS_BANDWIDTH_SUPERWIDEBAND)<<4; 1.277 + toc |= (period-2)<<3; 1.278 + } 1.279 + toc |= (channels==2)<<2; 1.280 + return toc; 1.281 +} 1.282 + 1.283 +#ifndef FIXED_POINT 1.284 +static void silk_biquad_float( 1.285 + const opus_val16 *in, /* I: Input signal */ 1.286 + const opus_int32 *B_Q28, /* I: MA coefficients [3] */ 1.287 + const opus_int32 *A_Q28, /* I: AR coefficients [2] */ 1.288 + opus_val32 *S, /* I/O: State vector [2] */ 1.289 + opus_val16 *out, /* O: Output signal */ 1.290 + const opus_int32 len, /* I: Signal length (must be even) */ 1.291 + int stride 1.292 +) 1.293 +{ 1.294 + /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */ 1.295 + opus_int k; 1.296 + opus_val32 vout; 1.297 + opus_val32 inval; 1.298 + opus_val32 A[2], B[3]; 1.299 + 1.300 + A[0] = (opus_val32)(A_Q28[0] * (1.f/((opus_int32)1<<28))); 1.301 + A[1] = (opus_val32)(A_Q28[1] * (1.f/((opus_int32)1<<28))); 1.302 + B[0] = (opus_val32)(B_Q28[0] * (1.f/((opus_int32)1<<28))); 1.303 + B[1] = (opus_val32)(B_Q28[1] * (1.f/((opus_int32)1<<28))); 1.304 + B[2] = (opus_val32)(B_Q28[2] * (1.f/((opus_int32)1<<28))); 1.305 + 1.306 + /* Negate A_Q28 values and split in two parts */ 1.307 + 1.308 + for( k = 0; k < len; k++ ) { 1.309 + /* S[ 0 ], S[ 1 ]: Q12 */ 1.310 + inval = in[ k*stride ]; 1.311 + vout = S[ 0 ] + B[0]*inval; 1.312 + 1.313 + S[ 0 ] = S[1] - vout*A[0] + B[1]*inval; 1.314 + 1.315 + S[ 1 ] = - vout*A[1] + B[2]*inval + VERY_SMALL; 1.316 + 1.317 + /* Scale back to Q0 and saturate */ 1.318 + out[ k*stride ] = vout; 1.319 + } 1.320 +} 1.321 +#endif 1.322 + 1.323 +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) 1.324 +{ 1.325 + opus_int32 B_Q28[ 3 ], A_Q28[ 2 ]; 1.326 + opus_int32 Fc_Q19, r_Q28, r_Q22; 1.327 + 1.328 + silk_assert( cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ) ); 1.329 + Fc_Q19 = silk_DIV32_16( silk_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 ); 1.330 + silk_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 ); 1.331 + 1.332 + r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - silk_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q19 ); 1.333 + 1.334 + /* b = r * [ 1; -2; 1 ]; */ 1.335 + /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */ 1.336 + B_Q28[ 0 ] = r_Q28; 1.337 + B_Q28[ 1 ] = silk_LSHIFT( -r_Q28, 1 ); 1.338 + B_Q28[ 2 ] = r_Q28; 1.339 + 1.340 + /* -r * ( 2 - Fc * Fc ); */ 1.341 + r_Q22 = silk_RSHIFT( r_Q28, 6 ); 1.342 + A_Q28[ 0 ] = silk_SMULWW( r_Q22, silk_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CONST( 2.0, 22 ) ); 1.343 + A_Q28[ 1 ] = silk_SMULWW( r_Q22, r_Q22 ); 1.344 + 1.345 +#ifdef FIXED_POINT 1.346 + silk_biquad_alt( in, B_Q28, A_Q28, hp_mem, out, len, channels ); 1.347 + if( channels == 2 ) { 1.348 + silk_biquad_alt( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels ); 1.349 + } 1.350 +#else 1.351 + silk_biquad_float( in, B_Q28, A_Q28, hp_mem, out, len, channels ); 1.352 + if( channels == 2 ) { 1.353 + silk_biquad_float( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels ); 1.354 + } 1.355 +#endif 1.356 +} 1.357 + 1.358 +#ifdef FIXED_POINT 1.359 +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) 1.360 +{ 1.361 + int c, i; 1.362 + int shift; 1.363 + 1.364 + /* Approximates -round(log2(4.*cutoff_Hz/Fs)) */ 1.365 + shift=celt_ilog2(Fs/(cutoff_Hz*3)); 1.366 + for (c=0;c<channels;c++) 1.367 + { 1.368 + for (i=0;i<len;i++) 1.369 + { 1.370 + opus_val32 x, tmp, y; 1.371 + x = SHL32(EXTEND32(in[channels*i+c]), 15); 1.372 + /* First stage */ 1.373 + tmp = x-hp_mem[2*c]; 1.374 + hp_mem[2*c] = hp_mem[2*c] + PSHR32(x - hp_mem[2*c], shift); 1.375 + /* Second stage */ 1.376 + y = tmp - hp_mem[2*c+1]; 1.377 + hp_mem[2*c+1] = hp_mem[2*c+1] + PSHR32(tmp - hp_mem[2*c+1], shift); 1.378 + out[channels*i+c] = EXTRACT16(SATURATE(PSHR32(y, 15), 32767)); 1.379 + } 1.380 + } 1.381 +} 1.382 + 1.383 +#else 1.384 +static void dc_reject(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs) 1.385 +{ 1.386 + int c, i; 1.387 + float coef; 1.388 + 1.389 + coef = 4.0f*cutoff_Hz/Fs; 1.390 + for (c=0;c<channels;c++) 1.391 + { 1.392 + for (i=0;i<len;i++) 1.393 + { 1.394 + opus_val32 x, tmp, y; 1.395 + x = in[channels*i+c]; 1.396 + /* First stage */ 1.397 + tmp = x-hp_mem[2*c]; 1.398 + hp_mem[2*c] = hp_mem[2*c] + coef*(x - hp_mem[2*c]) + VERY_SMALL; 1.399 + /* Second stage */ 1.400 + y = tmp - hp_mem[2*c+1]; 1.401 + hp_mem[2*c+1] = hp_mem[2*c+1] + coef*(tmp - hp_mem[2*c+1]) + VERY_SMALL; 1.402 + out[channels*i+c] = y; 1.403 + } 1.404 + } 1.405 +} 1.406 +#endif 1.407 + 1.408 +static void stereo_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, opus_val16 g2, 1.409 + int overlap48, int frame_size, int channels, const opus_val16 *window, opus_int32 Fs) 1.410 +{ 1.411 + int i; 1.412 + int overlap; 1.413 + int inc; 1.414 + inc = 48000/Fs; 1.415 + overlap=overlap48/inc; 1.416 + g1 = Q15ONE-g1; 1.417 + g2 = Q15ONE-g2; 1.418 + for (i=0;i<overlap;i++) 1.419 + { 1.420 + opus_val32 diff; 1.421 + opus_val16 g, w; 1.422 + w = MULT16_16_Q15(window[i*inc], window[i*inc]); 1.423 + g = SHR32(MAC16_16(MULT16_16(w,g2), 1.424 + Q15ONE-w, g1), 15); 1.425 + diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1])); 1.426 + diff = MULT16_16_Q15(g, diff); 1.427 + out[i*channels] = out[i*channels] - diff; 1.428 + out[i*channels+1] = out[i*channels+1] + diff; 1.429 + } 1.430 + for (;i<frame_size;i++) 1.431 + { 1.432 + opus_val32 diff; 1.433 + diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1])); 1.434 + diff = MULT16_16_Q15(g2, diff); 1.435 + out[i*channels] = out[i*channels] - diff; 1.436 + out[i*channels+1] = out[i*channels+1] + diff; 1.437 + } 1.438 +} 1.439 + 1.440 +static void gain_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, opus_val16 g2, 1.441 + int overlap48, int frame_size, int channels, const opus_val16 *window, opus_int32 Fs) 1.442 +{ 1.443 + int i; 1.444 + int inc; 1.445 + int overlap; 1.446 + int c; 1.447 + inc = 48000/Fs; 1.448 + overlap=overlap48/inc; 1.449 + if (channels==1) 1.450 + { 1.451 + for (i=0;i<overlap;i++) 1.452 + { 1.453 + opus_val16 g, w; 1.454 + w = MULT16_16_Q15(window[i*inc], window[i*inc]); 1.455 + g = SHR32(MAC16_16(MULT16_16(w,g2), 1.456 + Q15ONE-w, g1), 15); 1.457 + out[i] = MULT16_16_Q15(g, in[i]); 1.458 + } 1.459 + } else { 1.460 + for (i=0;i<overlap;i++) 1.461 + { 1.462 + opus_val16 g, w; 1.463 + w = MULT16_16_Q15(window[i*inc], window[i*inc]); 1.464 + g = SHR32(MAC16_16(MULT16_16(w,g2), 1.465 + Q15ONE-w, g1), 15); 1.466 + out[i*2] = MULT16_16_Q15(g, in[i*2]); 1.467 + out[i*2+1] = MULT16_16_Q15(g, in[i*2+1]); 1.468 + } 1.469 + } 1.470 + c=0;do { 1.471 + for (i=overlap;i<frame_size;i++) 1.472 + { 1.473 + out[i*channels+c] = MULT16_16_Q15(g2, in[i*channels+c]); 1.474 + } 1.475 + } 1.476 + while (++c<channels); 1.477 +} 1.478 + 1.479 +OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error) 1.480 +{ 1.481 + int ret; 1.482 + OpusEncoder *st; 1.483 + if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2)|| 1.484 + (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO 1.485 + && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)) 1.486 + { 1.487 + if (error) 1.488 + *error = OPUS_BAD_ARG; 1.489 + return NULL; 1.490 + } 1.491 + st = (OpusEncoder *)opus_alloc(opus_encoder_get_size(channels)); 1.492 + if (st == NULL) 1.493 + { 1.494 + if (error) 1.495 + *error = OPUS_ALLOC_FAIL; 1.496 + return NULL; 1.497 + } 1.498 + ret = opus_encoder_init(st, Fs, channels, application); 1.499 + if (error) 1.500 + *error = ret; 1.501 + if (ret != OPUS_OK) 1.502 + { 1.503 + opus_free(st); 1.504 + st = NULL; 1.505 + } 1.506 + return st; 1.507 +} 1.508 + 1.509 +static opus_int32 user_bitrate_to_bitrate(OpusEncoder *st, int frame_size, int max_data_bytes) 1.510 +{ 1.511 + if(!frame_size)frame_size=st->Fs/400; 1.512 + if (st->user_bitrate_bps==OPUS_AUTO) 1.513 + return 60*st->Fs/frame_size + st->Fs*st->channels; 1.514 + else if (st->user_bitrate_bps==OPUS_BITRATE_MAX) 1.515 + return max_data_bytes*8*st->Fs/frame_size; 1.516 + else 1.517 + return st->user_bitrate_bps; 1.518 +} 1.519 + 1.520 +#ifndef DISABLE_FLOAT_API 1.521 +/* Don't use more than 60 ms for the frame size analysis */ 1.522 +#define MAX_DYNAMIC_FRAMESIZE 24 1.523 +/* Estimates how much the bitrate will be boosted based on the sub-frame energy */ 1.524 +static float transient_boost(const float *E, const float *E_1, int LM, int maxM) 1.525 +{ 1.526 + int i; 1.527 + int M; 1.528 + float sumE=0, sumE_1=0; 1.529 + float metric; 1.530 + 1.531 + M = IMIN(maxM, (1<<LM)+1); 1.532 + for (i=0;i<M;i++) 1.533 + { 1.534 + sumE += E[i]; 1.535 + sumE_1 += E_1[i]; 1.536 + } 1.537 + metric = sumE*sumE_1/(M*M); 1.538 + /*if (LM==3) 1.539 + printf("%f\n", metric);*/ 1.540 + /*return metric>10 ? 1 : 0;*/ 1.541 + /*return MAX16(0,1-exp(-.25*(metric-2.)));*/ 1.542 + return MIN16(1,(float)sqrt(MAX16(0,.05f*(metric-2)))); 1.543 +} 1.544 + 1.545 +/* Viterbi decoding trying to find the best frame size combination using look-ahead 1.546 + 1.547 + State numbering: 1.548 + 0: unused 1.549 + 1: 2.5 ms 1.550 + 2: 5 ms (#1) 1.551 + 3: 5 ms (#2) 1.552 + 4: 10 ms (#1) 1.553 + 5: 10 ms (#2) 1.554 + 6: 10 ms (#3) 1.555 + 7: 10 ms (#4) 1.556 + 8: 20 ms (#1) 1.557 + 9: 20 ms (#2) 1.558 + 10: 20 ms (#3) 1.559 + 11: 20 ms (#4) 1.560 + 12: 20 ms (#5) 1.561 + 13: 20 ms (#6) 1.562 + 14: 20 ms (#7) 1.563 + 15: 20 ms (#8) 1.564 +*/ 1.565 +static int transient_viterbi(const float *E, const float *E_1, int N, int frame_cost, int rate) 1.566 +{ 1.567 + int i; 1.568 + float cost[MAX_DYNAMIC_FRAMESIZE][16]; 1.569 + int states[MAX_DYNAMIC_FRAMESIZE][16]; 1.570 + float best_cost; 1.571 + int best_state; 1.572 + float factor; 1.573 + /* Take into account that we damp VBR in the 32 kb/s to 64 kb/s range. */ 1.574 + if (rate<80) 1.575 + factor=0; 1.576 + else if (rate>160) 1.577 + factor=1; 1.578 + else 1.579 + factor = (rate-80.f)/80.f; 1.580 + /* Makes variable framesize less aggressive at lower bitrates, but I can't 1.581 + find any valid theoretical justification for this (other than it seems 1.582 + to help) */ 1.583 + for (i=0;i<16;i++) 1.584 + { 1.585 + /* Impossible state */ 1.586 + states[0][i] = -1; 1.587 + cost[0][i] = 1e10; 1.588 + } 1.589 + for (i=0;i<4;i++) 1.590 + { 1.591 + cost[0][1<<i] = (frame_cost + rate*(1<<i))*(1+factor*transient_boost(E, E_1, i, N+1)); 1.592 + states[0][1<<i] = i; 1.593 + } 1.594 + for (i=1;i<N;i++) 1.595 + { 1.596 + int j; 1.597 + 1.598 + /* Follow continuations */ 1.599 + for (j=2;j<16;j++) 1.600 + { 1.601 + cost[i][j] = cost[i-1][j-1]; 1.602 + states[i][j] = j-1; 1.603 + } 1.604 + 1.605 + /* New frames */ 1.606 + for(j=0;j<4;j++) 1.607 + { 1.608 + int k; 1.609 + float min_cost; 1.610 + float curr_cost; 1.611 + states[i][1<<j] = 1; 1.612 + min_cost = cost[i-1][1]; 1.613 + for(k=1;k<4;k++) 1.614 + { 1.615 + float tmp = cost[i-1][(1<<(k+1))-1]; 1.616 + if (tmp < min_cost) 1.617 + { 1.618 + states[i][1<<j] = (1<<(k+1))-1; 1.619 + min_cost = tmp; 1.620 + } 1.621 + } 1.622 + curr_cost = (frame_cost + rate*(1<<j))*(1+factor*transient_boost(E+i, E_1+i, j, N-i+1)); 1.623 + cost[i][1<<j] = min_cost; 1.624 + /* If part of the frame is outside the analysis window, only count part of the cost */ 1.625 + if (N-i < (1<<j)) 1.626 + cost[i][1<<j] += curr_cost*(float)(N-i)/(1<<j); 1.627 + else 1.628 + cost[i][1<<j] += curr_cost; 1.629 + } 1.630 + } 1.631 + 1.632 + best_state=1; 1.633 + best_cost = cost[N-1][1]; 1.634 + /* Find best end state (doesn't force a frame to end at N-1) */ 1.635 + for (i=2;i<16;i++) 1.636 + { 1.637 + if (cost[N-1][i]<best_cost) 1.638 + { 1.639 + best_cost = cost[N-1][i]; 1.640 + best_state = i; 1.641 + } 1.642 + } 1.643 + 1.644 + /* Follow transitions back */ 1.645 + for (i=N-1;i>=0;i--) 1.646 + { 1.647 + /*printf("%d ", best_state);*/ 1.648 + best_state = states[i][best_state]; 1.649 + } 1.650 + /*printf("%d\n", best_state);*/ 1.651 + return best_state; 1.652 +} 1.653 + 1.654 +int optimize_framesize(const opus_val16 *x, int len, int C, opus_int32 Fs, 1.655 + int bitrate, opus_val16 tonality, float *mem, int buffering, 1.656 + downmix_func downmix) 1.657 +{ 1.658 + int N; 1.659 + int i; 1.660 + float e[MAX_DYNAMIC_FRAMESIZE+4]; 1.661 + float e_1[MAX_DYNAMIC_FRAMESIZE+3]; 1.662 + opus_val32 memx; 1.663 + int bestLM=0; 1.664 + int subframe; 1.665 + int pos; 1.666 + VARDECL(opus_val32, sub); 1.667 + 1.668 + subframe = Fs/400; 1.669 + ALLOC(sub, subframe, opus_val32); 1.670 + e[0]=mem[0]; 1.671 + e_1[0]=1.f/(EPSILON+mem[0]); 1.672 + if (buffering) 1.673 + { 1.674 + /* Consider the CELT delay when not in restricted-lowdelay */ 1.675 + /* We assume the buffering is between 2.5 and 5 ms */ 1.676 + int offset = 2*subframe - buffering; 1.677 + celt_assert(offset>=0 && offset <= subframe); 1.678 + x += C*offset; 1.679 + len -= offset; 1.680 + e[1]=mem[1]; 1.681 + e_1[1]=1.f/(EPSILON+mem[1]); 1.682 + e[2]=mem[2]; 1.683 + e_1[2]=1.f/(EPSILON+mem[2]); 1.684 + pos = 3; 1.685 + } else { 1.686 + pos=1; 1.687 + } 1.688 + N=IMIN(len/subframe, MAX_DYNAMIC_FRAMESIZE); 1.689 + /* Just silencing a warning, it's really initialized later */ 1.690 + memx = 0; 1.691 + for (i=0;i<N;i++) 1.692 + { 1.693 + float tmp; 1.694 + opus_val32 tmpx; 1.695 + int j; 1.696 + tmp=EPSILON; 1.697 + 1.698 + downmix(x, sub, subframe, i*subframe, 0, -2, C); 1.699 + if (i==0) 1.700 + memx = sub[0]; 1.701 + for (j=0;j<subframe;j++) 1.702 + { 1.703 + tmpx = sub[j]; 1.704 + tmp += (tmpx-memx)*(float)(tmpx-memx); 1.705 + memx = tmpx; 1.706 + } 1.707 + e[i+pos] = tmp; 1.708 + e_1[i+pos] = 1.f/tmp; 1.709 + } 1.710 + /* Hack to get 20 ms working with APPLICATION_AUDIO 1.711 + The real problem is that the corresponding memory needs to use 1.5 ms 1.712 + from this frame and 1 ms from the next frame */ 1.713 + e[i+pos] = e[i+pos-1]; 1.714 + if (buffering) 1.715 + N=IMIN(MAX_DYNAMIC_FRAMESIZE, N+2); 1.716 + bestLM = transient_viterbi(e, e_1, N, (int)((1.f+.5f*tonality)*(60*C+40)), bitrate/400); 1.717 + mem[0] = e[1<<bestLM]; 1.718 + if (buffering) 1.719 + { 1.720 + mem[1] = e[(1<<bestLM)+1]; 1.721 + mem[2] = e[(1<<bestLM)+2]; 1.722 + } 1.723 + return bestLM; 1.724 +} 1.725 + 1.726 +#endif 1.727 + 1.728 +#ifndef DISABLE_FLOAT_API 1.729 +#ifdef FIXED_POINT 1.730 +#define PCM2VAL(x) FLOAT2INT16(x) 1.731 +#else 1.732 +#define PCM2VAL(x) SCALEIN(x) 1.733 +#endif 1.734 +void downmix_float(const void *_x, opus_val32 *sub, int subframe, int offset, int c1, int c2, int C) 1.735 +{ 1.736 + const float *x; 1.737 + opus_val32 scale; 1.738 + int j; 1.739 + x = (const float *)_x; 1.740 + for (j=0;j<subframe;j++) 1.741 + sub[j] = PCM2VAL(x[(j+offset)*C+c1]); 1.742 + if (c2>-1) 1.743 + { 1.744 + for (j=0;j<subframe;j++) 1.745 + sub[j] += PCM2VAL(x[(j+offset)*C+c2]); 1.746 + } else if (c2==-2) 1.747 + { 1.748 + int c; 1.749 + for (c=1;c<C;c++) 1.750 + { 1.751 + for (j=0;j<subframe;j++) 1.752 + sub[j] += PCM2VAL(x[(j+offset)*C+c]); 1.753 + } 1.754 + } 1.755 +#ifdef FIXED_POINT 1.756 + scale = (1<<SIG_SHIFT); 1.757 +#else 1.758 + scale = 1.f; 1.759 +#endif 1.760 + if (C==-2) 1.761 + scale /= C; 1.762 + else 1.763 + scale /= 2; 1.764 + for (j=0;j<subframe;j++) 1.765 + sub[j] *= scale; 1.766 +} 1.767 +#endif 1.768 + 1.769 +void downmix_int(const void *_x, opus_val32 *sub, int subframe, int offset, int c1, int c2, int C) 1.770 +{ 1.771 + const opus_int16 *x; 1.772 + opus_val32 scale; 1.773 + int j; 1.774 + x = (const opus_int16 *)_x; 1.775 + for (j=0;j<subframe;j++) 1.776 + sub[j] = x[(j+offset)*C+c1]; 1.777 + if (c2>-1) 1.778 + { 1.779 + for (j=0;j<subframe;j++) 1.780 + sub[j] += x[(j+offset)*C+c2]; 1.781 + } else if (c2==-2) 1.782 + { 1.783 + int c; 1.784 + for (c=1;c<C;c++) 1.785 + { 1.786 + for (j=0;j<subframe;j++) 1.787 + sub[j] += x[(j+offset)*C+c]; 1.788 + } 1.789 + } 1.790 +#ifdef FIXED_POINT 1.791 + scale = (1<<SIG_SHIFT); 1.792 +#else 1.793 + scale = 1.f/32768; 1.794 +#endif 1.795 + if (C==-2) 1.796 + scale /= C; 1.797 + else 1.798 + scale /= 2; 1.799 + for (j=0;j<subframe;j++) 1.800 + sub[j] *= scale; 1.801 +} 1.802 + 1.803 +opus_int32 frame_size_select(opus_int32 frame_size, int variable_duration, opus_int32 Fs) 1.804 +{ 1.805 + int new_size; 1.806 + if (frame_size<Fs/400) 1.807 + return -1; 1.808 + if (variable_duration == OPUS_FRAMESIZE_ARG) 1.809 + new_size = frame_size; 1.810 + else if (variable_duration == OPUS_FRAMESIZE_VARIABLE) 1.811 + new_size = Fs/50; 1.812 + else if (variable_duration >= OPUS_FRAMESIZE_2_5_MS && variable_duration <= OPUS_FRAMESIZE_60_MS) 1.813 + new_size = IMIN(3*Fs/50, (Fs/400)<<(variable_duration-OPUS_FRAMESIZE_2_5_MS)); 1.814 + else 1.815 + return -1; 1.816 + if (new_size>frame_size) 1.817 + return -1; 1.818 + if (400*new_size!=Fs && 200*new_size!=Fs && 100*new_size!=Fs && 1.819 + 50*new_size!=Fs && 25*new_size!=Fs && 50*new_size!=3*Fs) 1.820 + return -1; 1.821 + return new_size; 1.822 +} 1.823 + 1.824 +opus_int32 compute_frame_size(const void *analysis_pcm, int frame_size, 1.825 + int variable_duration, int C, opus_int32 Fs, int bitrate_bps, 1.826 + int delay_compensation, downmix_func downmix 1.827 +#ifndef DISABLE_FLOAT_API 1.828 + , float *subframe_mem 1.829 +#endif 1.830 + ) 1.831 +{ 1.832 +#ifndef DISABLE_FLOAT_API 1.833 + if (variable_duration == OPUS_FRAMESIZE_VARIABLE && frame_size >= Fs/200) 1.834 + { 1.835 + int LM = 3; 1.836 + LM = optimize_framesize(analysis_pcm, frame_size, C, Fs, bitrate_bps, 1.837 + 0, subframe_mem, delay_compensation, downmix); 1.838 + while ((Fs/400<<LM)>frame_size) 1.839 + LM--; 1.840 + frame_size = (Fs/400<<LM); 1.841 + } else 1.842 +#endif 1.843 + { 1.844 + frame_size = frame_size_select(frame_size, variable_duration, Fs); 1.845 + } 1.846 + if (frame_size<0) 1.847 + return -1; 1.848 + return frame_size; 1.849 +} 1.850 + 1.851 +opus_val16 compute_stereo_width(const opus_val16 *pcm, int frame_size, opus_int32 Fs, StereoWidthState *mem) 1.852 +{ 1.853 + opus_val16 corr; 1.854 + opus_val16 ldiff; 1.855 + opus_val16 width; 1.856 + opus_val32 xx, xy, yy; 1.857 + opus_val16 sqrt_xx, sqrt_yy; 1.858 + opus_val16 qrrt_xx, qrrt_yy; 1.859 + int frame_rate; 1.860 + int i; 1.861 + opus_val16 short_alpha; 1.862 + 1.863 + frame_rate = Fs/frame_size; 1.864 + short_alpha = Q15ONE - 25*Q15ONE/IMAX(50,frame_rate); 1.865 + xx=xy=yy=0; 1.866 + for (i=0;i<frame_size;i+=4) 1.867 + { 1.868 + opus_val32 pxx=0; 1.869 + opus_val32 pxy=0; 1.870 + opus_val32 pyy=0; 1.871 + opus_val16 x, y; 1.872 + x = pcm[2*i]; 1.873 + y = pcm[2*i+1]; 1.874 + pxx = SHR32(MULT16_16(x,x),2); 1.875 + pxy = SHR32(MULT16_16(x,y),2); 1.876 + pyy = SHR32(MULT16_16(y,y),2); 1.877 + x = pcm[2*i+2]; 1.878 + y = pcm[2*i+3]; 1.879 + pxx += SHR32(MULT16_16(x,x),2); 1.880 + pxy += SHR32(MULT16_16(x,y),2); 1.881 + pyy += SHR32(MULT16_16(y,y),2); 1.882 + x = pcm[2*i+4]; 1.883 + y = pcm[2*i+5]; 1.884 + pxx += SHR32(MULT16_16(x,x),2); 1.885 + pxy += SHR32(MULT16_16(x,y),2); 1.886 + pyy += SHR32(MULT16_16(y,y),2); 1.887 + x = pcm[2*i+6]; 1.888 + y = pcm[2*i+7]; 1.889 + pxx += SHR32(MULT16_16(x,x),2); 1.890 + pxy += SHR32(MULT16_16(x,y),2); 1.891 + pyy += SHR32(MULT16_16(y,y),2); 1.892 + 1.893 + xx += SHR32(pxx, 10); 1.894 + xy += SHR32(pxy, 10); 1.895 + yy += SHR32(pyy, 10); 1.896 + } 1.897 + mem->XX += MULT16_32_Q15(short_alpha, xx-mem->XX); 1.898 + mem->XY += MULT16_32_Q15(short_alpha, xy-mem->XY); 1.899 + mem->YY += MULT16_32_Q15(short_alpha, yy-mem->YY); 1.900 + mem->XX = MAX32(0, mem->XX); 1.901 + mem->XY = MAX32(0, mem->XY); 1.902 + mem->YY = MAX32(0, mem->YY); 1.903 + if (MAX32(mem->XX, mem->YY)>QCONST16(8e-4f, 18)) 1.904 + { 1.905 + sqrt_xx = celt_sqrt(mem->XX); 1.906 + sqrt_yy = celt_sqrt(mem->YY); 1.907 + qrrt_xx = celt_sqrt(sqrt_xx); 1.908 + qrrt_yy = celt_sqrt(sqrt_yy); 1.909 + /* Inter-channel correlation */ 1.910 + mem->XY = MIN32(mem->XY, sqrt_xx*sqrt_yy); 1.911 + corr = SHR32(frac_div32(mem->XY,EPSILON+MULT16_16(sqrt_xx,sqrt_yy)),16); 1.912 + /* Approximate loudness difference */ 1.913 + ldiff = Q15ONE*ABS16(qrrt_xx-qrrt_yy)/(EPSILON+qrrt_xx+qrrt_yy); 1.914 + width = MULT16_16_Q15(celt_sqrt(QCONST32(1.f,30)-MULT16_16(corr,corr)), ldiff); 1.915 + /* Smoothing over one second */ 1.916 + mem->smoothed_width += (width-mem->smoothed_width)/frame_rate; 1.917 + /* Peak follower */ 1.918 + mem->max_follower = MAX16(mem->max_follower-QCONST16(.02f,15)/frame_rate, mem->smoothed_width); 1.919 + } else { 1.920 + width = 0; 1.921 + corr=Q15ONE; 1.922 + ldiff=0; 1.923 + } 1.924 + /*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);*/ 1.925 + return EXTRACT16(MIN32(Q15ONE,20*mem->max_follower)); 1.926 +} 1.927 + 1.928 +opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_size, 1.929 + unsigned char *data, opus_int32 out_data_bytes, int lsb_depth, 1.930 + const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2, int analysis_channels, downmix_func downmix) 1.931 +{ 1.932 + void *silk_enc; 1.933 + CELTEncoder *celt_enc; 1.934 + int i; 1.935 + int ret=0; 1.936 + opus_int32 nBytes; 1.937 + ec_enc enc; 1.938 + int bytes_target; 1.939 + int prefill=0; 1.940 + int start_band = 0; 1.941 + int redundancy = 0; 1.942 + int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */ 1.943 + int celt_to_silk = 0; 1.944 + VARDECL(opus_val16, pcm_buf); 1.945 + int nb_compr_bytes; 1.946 + int to_celt = 0; 1.947 + opus_uint32 redundant_rng = 0; 1.948 + int cutoff_Hz, hp_freq_smth1; 1.949 + int voice_est; /* Probability of voice in Q7 */ 1.950 + opus_int32 equiv_rate; 1.951 + int delay_compensation; 1.952 + int frame_rate; 1.953 + opus_int32 max_rate; /* Max bitrate we're allowed to use */ 1.954 + int curr_bandwidth; 1.955 + opus_val16 HB_gain; 1.956 + opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */ 1.957 + int total_buffer; 1.958 + opus_val16 stereo_width; 1.959 + const CELTMode *celt_mode; 1.960 + AnalysisInfo analysis_info; 1.961 + int analysis_read_pos_bak=-1; 1.962 + int analysis_read_subframe_bak=-1; 1.963 + VARDECL(opus_val16, tmp_prefill); 1.964 + 1.965 + ALLOC_STACK; 1.966 + 1.967 + max_data_bytes = IMIN(1276, out_data_bytes); 1.968 + 1.969 + st->rangeFinal = 0; 1.970 + if ((!st->variable_duration && 400*frame_size != st->Fs && 200*frame_size != st->Fs && 100*frame_size != st->Fs && 1.971 + 50*frame_size != st->Fs && 25*frame_size != st->Fs && 50*frame_size != 3*st->Fs) 1.972 + || (400*frame_size < st->Fs) 1.973 + || max_data_bytes<=0 1.974 + ) 1.975 + { 1.976 + RESTORE_STACK; 1.977 + return OPUS_BAD_ARG; 1.978 + } 1.979 + silk_enc = (char*)st+st->silk_enc_offset; 1.980 + celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); 1.981 + if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1.982 + delay_compensation = 0; 1.983 + else 1.984 + delay_compensation = st->delay_compensation; 1.985 + 1.986 + lsb_depth = IMIN(lsb_depth, st->lsb_depth); 1.987 + 1.988 + analysis_info.valid = 0; 1.989 + celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode)); 1.990 +#ifndef DISABLE_FLOAT_API 1.991 +#ifdef FIXED_POINT 1.992 + if (st->silk_mode.complexity >= 10 && st->Fs==48000) 1.993 +#else 1.994 + if (st->silk_mode.complexity >= 7 && st->Fs==48000) 1.995 +#endif 1.996 + { 1.997 + analysis_read_pos_bak = st->analysis.read_pos; 1.998 + analysis_read_subframe_bak = st->analysis.read_subframe; 1.999 + run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame_size, 1.1000 + c1, c2, analysis_channels, st->Fs, 1.1001 + lsb_depth, downmix, &analysis_info); 1.1002 + } 1.1003 +#endif 1.1004 + 1.1005 + st->voice_ratio = -1; 1.1006 + 1.1007 +#ifndef DISABLE_FLOAT_API 1.1008 + st->detected_bandwidth = 0; 1.1009 + if (analysis_info.valid) 1.1010 + { 1.1011 + int analysis_bandwidth; 1.1012 + if (st->signal_type == OPUS_AUTO) 1.1013 + st->voice_ratio = (int)floor(.5+100*(1-analysis_info.music_prob)); 1.1014 + 1.1015 + analysis_bandwidth = analysis_info.bandwidth; 1.1016 + if (analysis_bandwidth<=12) 1.1017 + st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1.1018 + else if (analysis_bandwidth<=14) 1.1019 + st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; 1.1020 + else if (analysis_bandwidth<=16) 1.1021 + st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1.1022 + else if (analysis_bandwidth<=18) 1.1023 + st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; 1.1024 + else 1.1025 + st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND; 1.1026 + } 1.1027 +#endif 1.1028 + 1.1029 + if (st->channels==2 && st->force_channels!=1) 1.1030 + stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_mem); 1.1031 + else 1.1032 + stereo_width = 0; 1.1033 + total_buffer = delay_compensation; 1.1034 + st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes); 1.1035 + 1.1036 + frame_rate = st->Fs/frame_size; 1.1037 + if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8 1.1038 + || (frame_rate<50 && (max_data_bytes*frame_rate<300 || st->bitrate_bps < 2400))) 1.1039 + { 1.1040 + /*If the space is too low to do something useful, emit 'PLC' frames.*/ 1.1041 + int tocmode = st->mode; 1.1042 + int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth; 1.1043 + if (tocmode==0) 1.1044 + tocmode = MODE_SILK_ONLY; 1.1045 + if (frame_rate>100) 1.1046 + tocmode = MODE_CELT_ONLY; 1.1047 + if (frame_rate < 50) 1.1048 + tocmode = MODE_SILK_ONLY; 1.1049 + if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND) 1.1050 + bw=OPUS_BANDWIDTH_WIDEBAND; 1.1051 + else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND) 1.1052 + bw=OPUS_BANDWIDTH_NARROWBAND; 1.1053 + else if (bw<=OPUS_BANDWIDTH_SUPERWIDEBAND) 1.1054 + bw=OPUS_BANDWIDTH_SUPERWIDEBAND; 1.1055 + data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels); 1.1056 + RESTORE_STACK; 1.1057 + return 1; 1.1058 + } 1.1059 + if (!st->use_vbr) 1.1060 + { 1.1061 + int cbrBytes; 1.1062 + cbrBytes = IMIN( (st->bitrate_bps + 4*frame_rate)/(8*frame_rate) , max_data_bytes); 1.1063 + st->bitrate_bps = cbrBytes * (8*frame_rate); 1.1064 + max_data_bytes = cbrBytes; 1.1065 + } 1.1066 + max_rate = frame_rate*max_data_bytes*8; 1.1067 + 1.1068 + /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */ 1.1069 + equiv_rate = st->bitrate_bps - (40*st->channels+20)*(st->Fs/frame_size - 50); 1.1070 + 1.1071 + if (st->signal_type == OPUS_SIGNAL_VOICE) 1.1072 + voice_est = 127; 1.1073 + else if (st->signal_type == OPUS_SIGNAL_MUSIC) 1.1074 + voice_est = 0; 1.1075 + else if (st->voice_ratio >= 0) 1.1076 + { 1.1077 + voice_est = st->voice_ratio*327>>8; 1.1078 + /* For AUDIO, never be more than 90% confident of having speech */ 1.1079 + if (st->application == OPUS_APPLICATION_AUDIO) 1.1080 + voice_est = IMIN(voice_est, 115); 1.1081 + } else if (st->application == OPUS_APPLICATION_VOIP) 1.1082 + voice_est = 115; 1.1083 + else 1.1084 + voice_est = 48; 1.1085 + 1.1086 + if (st->force_channels!=OPUS_AUTO && st->channels == 2) 1.1087 + { 1.1088 + st->stream_channels = st->force_channels; 1.1089 + } else { 1.1090 +#ifdef FUZZING 1.1091 + /* Random mono/stereo decision */ 1.1092 + if (st->channels == 2 && (rand()&0x1F)==0) 1.1093 + st->stream_channels = 3-st->stream_channels; 1.1094 +#else 1.1095 + /* Rate-dependent mono-stereo decision */ 1.1096 + if (st->channels == 2) 1.1097 + { 1.1098 + opus_int32 stereo_threshold; 1.1099 + stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14); 1.1100 + if (st->stream_channels == 2) 1.1101 + stereo_threshold -= 1000; 1.1102 + else 1.1103 + stereo_threshold += 1000; 1.1104 + st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1; 1.1105 + } else { 1.1106 + st->stream_channels = st->channels; 1.1107 + } 1.1108 +#endif 1.1109 + } 1.1110 + equiv_rate = st->bitrate_bps - (40*st->stream_channels+20)*(st->Fs/frame_size - 50); 1.1111 + 1.1112 + /* Mode selection depending on application and signal type */ 1.1113 + if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1.1114 + { 1.1115 + st->mode = MODE_CELT_ONLY; 1.1116 + } else if (st->user_forced_mode == OPUS_AUTO) 1.1117 + { 1.1118 +#ifdef FUZZING 1.1119 + /* Random mode switching */ 1.1120 + if ((rand()&0xF)==0) 1.1121 + { 1.1122 + if ((rand()&0x1)==0) 1.1123 + st->mode = MODE_CELT_ONLY; 1.1124 + else 1.1125 + st->mode = MODE_SILK_ONLY; 1.1126 + } else { 1.1127 + if (st->prev_mode==MODE_CELT_ONLY) 1.1128 + st->mode = MODE_CELT_ONLY; 1.1129 + else 1.1130 + st->mode = MODE_SILK_ONLY; 1.1131 + } 1.1132 +#else 1.1133 + opus_int32 mode_voice, mode_music; 1.1134 + opus_int32 threshold; 1.1135 + 1.1136 + /* Interpolate based on stereo width */ 1.1137 + mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[0][0]) 1.1138 + + MULT16_32_Q15(stereo_width,mode_thresholds[1][0])); 1.1139 + mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[1][1]) 1.1140 + + MULT16_32_Q15(stereo_width,mode_thresholds[1][1])); 1.1141 + /* Interpolate based on speech/music probability */ 1.1142 + threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14); 1.1143 + /* Bias towards SILK for VoIP because of some useful features */ 1.1144 + if (st->application == OPUS_APPLICATION_VOIP) 1.1145 + threshold += 8000; 1.1146 + 1.1147 + /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/ 1.1148 + /* Hysteresis */ 1.1149 + if (st->prev_mode == MODE_CELT_ONLY) 1.1150 + threshold -= 4000; 1.1151 + else if (st->prev_mode>0) 1.1152 + threshold += 4000; 1.1153 + 1.1154 + st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY; 1.1155 + 1.1156 + /* When FEC is enabled and there's enough packet loss, use SILK */ 1.1157 + if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (128-voice_est)>>4) 1.1158 + st->mode = MODE_SILK_ONLY; 1.1159 + /* When encoding voice and DTX is enabled, set the encoder to SILK mode (at least for now) */ 1.1160 + if (st->silk_mode.useDTX && voice_est > 100) 1.1161 + st->mode = MODE_SILK_ONLY; 1.1162 +#endif 1.1163 + } else { 1.1164 + st->mode = st->user_forced_mode; 1.1165 + } 1.1166 + 1.1167 + /* Override the chosen mode to make sure we meet the requested frame size */ 1.1168 + if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100) 1.1169 + st->mode = MODE_CELT_ONLY; 1.1170 + if (st->lfe) 1.1171 + st->mode = MODE_CELT_ONLY; 1.1172 + /* If max_data_bytes represents less than 8 kb/s, switch to CELT-only mode */ 1.1173 + if (max_data_bytes < (frame_rate > 50 ? 12000 : 8000)*frame_size / (st->Fs * 8)) 1.1174 + st->mode = MODE_CELT_ONLY; 1.1175 + 1.1176 + if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0 1.1177 + && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY) 1.1178 + { 1.1179 + /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */ 1.1180 + st->silk_mode.toMono = 1; 1.1181 + st->stream_channels = 2; 1.1182 + } else { 1.1183 + st->silk_mode.toMono = 0; 1.1184 + } 1.1185 + 1.1186 + if (st->prev_mode > 0 && 1.1187 + ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) || 1.1188 + (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY))) 1.1189 + { 1.1190 + redundancy = 1; 1.1191 + celt_to_silk = (st->mode != MODE_CELT_ONLY); 1.1192 + if (!celt_to_silk) 1.1193 + { 1.1194 + /* Switch to SILK/hybrid if frame size is 10 ms or more*/ 1.1195 + if (frame_size >= st->Fs/100) 1.1196 + { 1.1197 + st->mode = st->prev_mode; 1.1198 + to_celt = 1; 1.1199 + } else { 1.1200 + redundancy=0; 1.1201 + } 1.1202 + } 1.1203 + } 1.1204 + /* For the first frame at a new SILK bandwidth */ 1.1205 + if (st->silk_bw_switch) 1.1206 + { 1.1207 + redundancy = 1; 1.1208 + celt_to_silk = 1; 1.1209 + st->silk_bw_switch = 0; 1.1210 + prefill=1; 1.1211 + } 1.1212 + 1.1213 + if (redundancy) 1.1214 + { 1.1215 + /* Fair share of the max size allowed */ 1.1216 + redundancy_bytes = IMIN(257, max_data_bytes*(opus_int32)(st->Fs/200)/(frame_size+st->Fs/200)); 1.1217 + /* For VBR, target the actual bitrate (subject to the limit above) */ 1.1218 + if (st->use_vbr) 1.1219 + redundancy_bytes = IMIN(redundancy_bytes, st->bitrate_bps/1600); 1.1220 + } 1.1221 + 1.1222 + if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) 1.1223 + { 1.1224 + silk_EncControlStruct dummy; 1.1225 + silk_InitEncoder( silk_enc, st->arch, &dummy); 1.1226 + prefill=1; 1.1227 + } 1.1228 + 1.1229 + /* Automatic (rate-dependent) bandwidth selection */ 1.1230 + if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch) 1.1231 + { 1.1232 + const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds; 1.1233 + opus_int32 bandwidth_thresholds[8]; 1.1234 + int bandwidth = OPUS_BANDWIDTH_FULLBAND; 1.1235 + opus_int32 equiv_rate2; 1.1236 + 1.1237 + equiv_rate2 = equiv_rate; 1.1238 + if (st->mode != MODE_CELT_ONLY) 1.1239 + { 1.1240 + /* Adjust the threshold +/- 10% depending on complexity */ 1.1241 + equiv_rate2 = equiv_rate2 * (45+st->silk_mode.complexity)/50; 1.1242 + /* CBR is less efficient by ~1 kb/s */ 1.1243 + if (!st->use_vbr) 1.1244 + equiv_rate2 -= 1000; 1.1245 + } 1.1246 + if (st->channels==2 && st->force_channels!=1) 1.1247 + { 1.1248 + voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds; 1.1249 + music_bandwidth_thresholds = stereo_music_bandwidth_thresholds; 1.1250 + } else { 1.1251 + voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds; 1.1252 + music_bandwidth_thresholds = mono_music_bandwidth_thresholds; 1.1253 + } 1.1254 + /* Interpolate bandwidth thresholds depending on voice estimation */ 1.1255 + for (i=0;i<8;i++) 1.1256 + { 1.1257 + bandwidth_thresholds[i] = music_bandwidth_thresholds[i] 1.1258 + + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14); 1.1259 + } 1.1260 + do { 1.1261 + int threshold, hysteresis; 1.1262 + threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)]; 1.1263 + hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1]; 1.1264 + if (!st->first) 1.1265 + { 1.1266 + if (st->bandwidth >= bandwidth) 1.1267 + threshold -= hysteresis; 1.1268 + else 1.1269 + threshold += hysteresis; 1.1270 + } 1.1271 + if (equiv_rate2 >= threshold) 1.1272 + break; 1.1273 + } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND); 1.1274 + st->bandwidth = bandwidth; 1.1275 + /* Prevents any transition to SWB/FB until the SILK layer has fully 1.1276 + switched to WB mode and turned the variable LP filter off */ 1.1277 + if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND) 1.1278 + st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1.1279 + } 1.1280 + 1.1281 + if (st->bandwidth>st->max_bandwidth) 1.1282 + st->bandwidth = st->max_bandwidth; 1.1283 + 1.1284 + if (st->user_bandwidth != OPUS_AUTO) 1.1285 + st->bandwidth = st->user_bandwidth; 1.1286 + 1.1287 + /* This prevents us from using hybrid at unsafe CBR/max rates */ 1.1288 + if (st->mode != MODE_CELT_ONLY && max_rate < 15000) 1.1289 + { 1.1290 + st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND); 1.1291 + } 1.1292 + 1.1293 + /* Prevents Opus from wasting bits on frequencies that are above 1.1294 + the Nyquist rate of the input signal */ 1.1295 + if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND) 1.1296 + st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; 1.1297 + if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND) 1.1298 + st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1.1299 + if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND) 1.1300 + st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; 1.1301 + if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND) 1.1302 + st->bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1.1303 +#ifndef DISABLE_FLOAT_API 1.1304 + /* Use detected bandwidth to reduce the encoded bandwidth. */ 1.1305 + if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO) 1.1306 + { 1.1307 + int min_detected_bandwidth; 1.1308 + /* Makes bandwidth detection more conservative just in case the detector 1.1309 + gets it wrong when we could have coded a high bandwidth transparently. 1.1310 + When operating in SILK/hybrid mode, we don't go below wideband to avoid 1.1311 + more complicated switches that require redundancy. */ 1.1312 + if (equiv_rate <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY) 1.1313 + min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1.1314 + else if (equiv_rate <= 24000*st->stream_channels && st->mode == MODE_CELT_ONLY) 1.1315 + min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; 1.1316 + else if (equiv_rate <= 30000*st->stream_channels) 1.1317 + min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1.1318 + else if (equiv_rate <= 44000*st->stream_channels) 1.1319 + min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; 1.1320 + else 1.1321 + min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND; 1.1322 + 1.1323 + st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwidth); 1.1324 + st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth); 1.1325 + } 1.1326 +#endif 1.1327 + celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth)); 1.1328 + 1.1329 + /* CELT mode doesn't support mediumband, use wideband instead */ 1.1330 + if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) 1.1331 + st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1.1332 + if (st->lfe) 1.1333 + st->bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1.1334 + 1.1335 + /* Can't support higher than wideband for >20 ms frames */ 1.1336 + if (frame_size > st->Fs/50 && (st->mode == MODE_CELT_ONLY || st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)) 1.1337 + { 1.1338 + VARDECL(unsigned char, tmp_data); 1.1339 + int nb_frames; 1.1340 + int bak_mode, bak_bandwidth, bak_channels, bak_to_mono; 1.1341 + VARDECL(OpusRepacketizer, rp); 1.1342 + opus_int32 bytes_per_frame; 1.1343 + opus_int32 repacketize_len; 1.1344 + 1.1345 +#ifndef DISABLE_FLOAT_API 1.1346 + if (analysis_read_pos_bak!= -1) 1.1347 + { 1.1348 + st->analysis.read_pos = analysis_read_pos_bak; 1.1349 + st->analysis.read_subframe = analysis_read_subframe_bak; 1.1350 + } 1.1351 +#endif 1.1352 + 1.1353 + nb_frames = frame_size > st->Fs/25 ? 3 : 2; 1.1354 + bytes_per_frame = IMIN(1276,(out_data_bytes-3)/nb_frames); 1.1355 + 1.1356 + ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char); 1.1357 + 1.1358 + ALLOC(rp, 1, OpusRepacketizer); 1.1359 + opus_repacketizer_init(rp); 1.1360 + 1.1361 + bak_mode = st->user_forced_mode; 1.1362 + bak_bandwidth = st->user_bandwidth; 1.1363 + bak_channels = st->force_channels; 1.1364 + 1.1365 + st->user_forced_mode = st->mode; 1.1366 + st->user_bandwidth = st->bandwidth; 1.1367 + st->force_channels = st->stream_channels; 1.1368 + bak_to_mono = st->silk_mode.toMono; 1.1369 + 1.1370 + if (bak_to_mono) 1.1371 + st->force_channels = 1; 1.1372 + else 1.1373 + st->prev_channels = st->stream_channels; 1.1374 + for (i=0;i<nb_frames;i++) 1.1375 + { 1.1376 + int tmp_len; 1.1377 + st->silk_mode.toMono = 0; 1.1378 + /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */ 1.1379 + if (to_celt && i==nb_frames-1) 1.1380 + st->user_forced_mode = MODE_CELT_ONLY; 1.1381 + tmp_len = opus_encode_native(st, pcm+i*(st->channels*st->Fs/50), st->Fs/50, 1.1382 + tmp_data+i*bytes_per_frame, bytes_per_frame, lsb_depth, 1.1383 + NULL, 0, c1, c2, analysis_channels, downmix); 1.1384 + if (tmp_len<0) 1.1385 + { 1.1386 + RESTORE_STACK; 1.1387 + return OPUS_INTERNAL_ERROR; 1.1388 + } 1.1389 + ret = opus_repacketizer_cat(rp, tmp_data+i*bytes_per_frame, tmp_len); 1.1390 + if (ret<0) 1.1391 + { 1.1392 + RESTORE_STACK; 1.1393 + return OPUS_INTERNAL_ERROR; 1.1394 + } 1.1395 + } 1.1396 + if (st->use_vbr) 1.1397 + repacketize_len = out_data_bytes; 1.1398 + else 1.1399 + repacketize_len = IMIN(3*st->bitrate_bps/(3*8*50/nb_frames), out_data_bytes); 1.1400 + ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketize_len, 0, !st->use_vbr); 1.1401 + if (ret<0) 1.1402 + { 1.1403 + RESTORE_STACK; 1.1404 + return OPUS_INTERNAL_ERROR; 1.1405 + } 1.1406 + st->user_forced_mode = bak_mode; 1.1407 + st->user_bandwidth = bak_bandwidth; 1.1408 + st->force_channels = bak_channels; 1.1409 + st->silk_mode.toMono = bak_to_mono; 1.1410 + RESTORE_STACK; 1.1411 + return ret; 1.1412 + } 1.1413 + curr_bandwidth = st->bandwidth; 1.1414 + 1.1415 + /* Chooses the appropriate mode for speech 1.1416 + *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */ 1.1417 + if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND) 1.1418 + st->mode = MODE_HYBRID; 1.1419 + if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND) 1.1420 + st->mode = MODE_SILK_ONLY; 1.1421 + 1.1422 + /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */ 1.1423 + bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1; 1.1424 + 1.1425 + data += 1; 1.1426 + 1.1427 + ec_enc_init(&enc, data, max_data_bytes-1); 1.1428 + 1.1429 + ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_val16); 1.1430 + for (i=0;i<total_buffer*st->channels;i++) 1.1431 + pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-total_buffer)*st->channels+i]; 1.1432 + 1.1433 + if (st->mode == MODE_CELT_ONLY) 1.1434 + hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); 1.1435 + else 1.1436 + hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15; 1.1437 + 1.1438 + st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15, 1.1439 + hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) ); 1.1440 + 1.1441 + /* convert from log scale to Hertz */ 1.1442 + cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) ); 1.1443 + 1.1444 + if (st->application == OPUS_APPLICATION_VOIP) 1.1445 + { 1.1446 + hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs); 1.1447 + } else { 1.1448 + dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs); 1.1449 + } 1.1450 + 1.1451 + 1.1452 + 1.1453 + /* SILK processing */ 1.1454 + HB_gain = Q15ONE; 1.1455 + if (st->mode != MODE_CELT_ONLY) 1.1456 + { 1.1457 + opus_int32 total_bitRate, celt_rate; 1.1458 +#ifdef FIXED_POINT 1.1459 + const opus_int16 *pcm_silk; 1.1460 +#else 1.1461 + VARDECL(opus_int16, pcm_silk); 1.1462 + ALLOC(pcm_silk, st->channels*frame_size, opus_int16); 1.1463 +#endif 1.1464 + 1.1465 + /* Distribute bits between SILK and CELT */ 1.1466 + total_bitRate = 8 * bytes_target * frame_rate; 1.1467 + if( st->mode == MODE_HYBRID ) { 1.1468 + int HB_gain_ref; 1.1469 + /* Base rate for SILK */ 1.1470 + st->silk_mode.bitRate = st->stream_channels * ( 5000 + 1000 * ( st->Fs == 100 * frame_size ) ); 1.1471 + if( curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND ) { 1.1472 + /* SILK gets 2/3 of the remaining bits */ 1.1473 + st->silk_mode.bitRate += ( total_bitRate - st->silk_mode.bitRate ) * 2 / 3; 1.1474 + } else { /* FULLBAND */ 1.1475 + /* SILK gets 3/5 of the remaining bits */ 1.1476 + st->silk_mode.bitRate += ( total_bitRate - st->silk_mode.bitRate ) * 3 / 5; 1.1477 + } 1.1478 + /* Don't let SILK use more than 80% */ 1.1479 + if( st->silk_mode.bitRate > total_bitRate * 4/5 ) { 1.1480 + st->silk_mode.bitRate = total_bitRate * 4/5; 1.1481 + } 1.1482 + if (!st->energy_masking) 1.1483 + { 1.1484 + /* Increasingly attenuate high band when it gets allocated fewer bits */ 1.1485 + celt_rate = total_bitRate - st->silk_mode.bitRate; 1.1486 + HB_gain_ref = (curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND) ? 3000 : 3600; 1.1487 + HB_gain = SHL32((opus_val32)celt_rate, 9) / SHR32((opus_val32)celt_rate + st->stream_channels * HB_gain_ref, 6); 1.1488 + HB_gain = HB_gain < Q15ONE*6/7 ? HB_gain + Q15ONE/7 : Q15ONE; 1.1489 + } 1.1490 + } else { 1.1491 + /* SILK gets all bits */ 1.1492 + st->silk_mode.bitRate = total_bitRate; 1.1493 + } 1.1494 + 1.1495 + /* Surround masking for SILK */ 1.1496 + if (st->energy_masking && st->use_vbr && !st->lfe) 1.1497 + { 1.1498 + opus_val32 mask_sum=0; 1.1499 + opus_val16 masking_depth; 1.1500 + opus_int32 rate_offset; 1.1501 + int c; 1.1502 + int end = 17; 1.1503 + opus_int16 srate = 16000; 1.1504 + if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND) 1.1505 + { 1.1506 + end = 13; 1.1507 + srate = 8000; 1.1508 + } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) 1.1509 + { 1.1510 + end = 15; 1.1511 + srate = 12000; 1.1512 + } 1.1513 + for (c=0;c<st->channels;c++) 1.1514 + { 1.1515 + for(i=0;i<end;i++) 1.1516 + { 1.1517 + opus_val16 mask; 1.1518 + mask = MAX16(MIN16(st->energy_masking[21*c+i], 1.1519 + QCONST16(.5f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT)); 1.1520 + if (mask > 0) 1.1521 + mask = HALF16(mask); 1.1522 + mask_sum += mask; 1.1523 + } 1.1524 + } 1.1525 + /* Conservative rate reduction, we cut the masking in half */ 1.1526 + masking_depth = mask_sum / end*st->channels; 1.1527 + masking_depth += QCONST16(.2f, DB_SHIFT); 1.1528 + rate_offset = (opus_int32)PSHR32(MULT16_16(srate, masking_depth), DB_SHIFT); 1.1529 + rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3); 1.1530 + /* Split the rate change between the SILK and CELT part for hybrid. */ 1.1531 + if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPUS_BANDWIDTH_FULLBAND) 1.1532 + st->silk_mode.bitRate += 3*rate_offset/5; 1.1533 + else 1.1534 + st->silk_mode.bitRate += rate_offset; 1.1535 + bytes_target += rate_offset * frame_size / (8 * st->Fs); 1.1536 + } 1.1537 + 1.1538 + st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs; 1.1539 + st->silk_mode.nChannelsAPI = st->channels; 1.1540 + st->silk_mode.nChannelsInternal = st->stream_channels; 1.1541 + if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { 1.1542 + st->silk_mode.desiredInternalSampleRate = 8000; 1.1543 + } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { 1.1544 + st->silk_mode.desiredInternalSampleRate = 12000; 1.1545 + } else { 1.1546 + silk_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND ); 1.1547 + st->silk_mode.desiredInternalSampleRate = 16000; 1.1548 + } 1.1549 + if( st->mode == MODE_HYBRID ) { 1.1550 + /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */ 1.1551 + st->silk_mode.minInternalSampleRate = 16000; 1.1552 + } else { 1.1553 + st->silk_mode.minInternalSampleRate = 8000; 1.1554 + } 1.1555 + 1.1556 + if (st->mode == MODE_SILK_ONLY) 1.1557 + { 1.1558 + opus_int32 effective_max_rate = max_rate; 1.1559 + st->silk_mode.maxInternalSampleRate = 16000; 1.1560 + if (frame_rate > 50) 1.1561 + effective_max_rate = effective_max_rate*2/3; 1.1562 + if (effective_max_rate < 13000) 1.1563 + { 1.1564 + st->silk_mode.maxInternalSampleRate = 12000; 1.1565 + st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate); 1.1566 + } 1.1567 + if (effective_max_rate < 9600) 1.1568 + { 1.1569 + st->silk_mode.maxInternalSampleRate = 8000; 1.1570 + st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate); 1.1571 + } 1.1572 + } else { 1.1573 + st->silk_mode.maxInternalSampleRate = 16000; 1.1574 + } 1.1575 + 1.1576 + st->silk_mode.useCBR = !st->use_vbr; 1.1577 + 1.1578 + /* Call SILK encoder for the low band */ 1.1579 + nBytes = IMIN(1275, max_data_bytes-1-redundancy_bytes); 1.1580 + 1.1581 + st->silk_mode.maxBits = nBytes*8; 1.1582 + /* Only allow up to 90% of the bits for hybrid mode*/ 1.1583 + if (st->mode == MODE_HYBRID) 1.1584 + st->silk_mode.maxBits = (opus_int32)st->silk_mode.maxBits*9/10; 1.1585 + if (st->silk_mode.useCBR) 1.1586 + { 1.1587 + st->silk_mode.maxBits = (st->silk_mode.bitRate * frame_size / (st->Fs * 8))*8; 1.1588 + /* Reduce the initial target to make it easier to reach the CBR rate */ 1.1589 + st->silk_mode.bitRate = IMAX(1, st->silk_mode.bitRate-2000); 1.1590 + } 1.1591 + 1.1592 + if (prefill) 1.1593 + { 1.1594 + opus_int32 zero=0; 1.1595 + int prefill_offset; 1.1596 + /* Use a smooth onset for the SILK prefill to avoid the encoder trying to encode 1.1597 + a discontinuity. The exact location is what we need to avoid leaving any "gap" 1.1598 + in the audio when mixing with the redundant CELT frame. Here we can afford to 1.1599 + overwrite st->delay_buffer because the only thing that uses it before it gets 1.1600 + rewritten is tmp_prefill[] and even then only the part after the ramp really 1.1601 + gets used (rather than sent to the encoder and discarded) */ 1.1602 + prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensation-st->Fs/400); 1.1603 + gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_offset, 1.1604 + 0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_mode->window, st->Fs); 1.1605 + for(i=0;i<prefill_offset;i++) 1.1606 + st->delay_buffer[i]=0; 1.1607 +#ifdef FIXED_POINT 1.1608 + pcm_silk = st->delay_buffer; 1.1609 +#else 1.1610 + for (i=0;i<st->encoder_buffer*st->channels;i++) 1.1611 + pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]); 1.1612 +#endif 1.1613 + silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, 1 ); 1.1614 + } 1.1615 + 1.1616 +#ifdef FIXED_POINT 1.1617 + pcm_silk = pcm_buf+total_buffer*st->channels; 1.1618 +#else 1.1619 + for (i=0;i<frame_size*st->channels;i++) 1.1620 + pcm_silk[i] = FLOAT2INT16(pcm_buf[total_buffer*st->channels + i]); 1.1621 +#endif 1.1622 + ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0 ); 1.1623 + if( ret ) { 1.1624 + /*fprintf (stderr, "SILK encode error: %d\n", ret);*/ 1.1625 + /* Handle error */ 1.1626 + RESTORE_STACK; 1.1627 + return OPUS_INTERNAL_ERROR; 1.1628 + } 1.1629 + if (nBytes==0) 1.1630 + { 1.1631 + st->rangeFinal = 0; 1.1632 + data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels); 1.1633 + RESTORE_STACK; 1.1634 + return 1; 1.1635 + } 1.1636 + /* Extract SILK internal bandwidth for signaling in first byte */ 1.1637 + if( st->mode == MODE_SILK_ONLY ) { 1.1638 + if( st->silk_mode.internalSampleRate == 8000 ) { 1.1639 + curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1.1640 + } else if( st->silk_mode.internalSampleRate == 12000 ) { 1.1641 + curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; 1.1642 + } else if( st->silk_mode.internalSampleRate == 16000 ) { 1.1643 + curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1.1644 + } 1.1645 + } else { 1.1646 + silk_assert( st->silk_mode.internalSampleRate == 16000 ); 1.1647 + } 1.1648 + 1.1649 + st->silk_mode.opusCanSwitch = st->silk_mode.switchReady; 1.1650 + /* FIXME: How do we allocate the redundancy for CBR? */ 1.1651 + if (st->silk_mode.opusCanSwitch) 1.1652 + { 1.1653 + redundancy = 1; 1.1654 + celt_to_silk = 0; 1.1655 + st->silk_bw_switch = 1; 1.1656 + } 1.1657 + } 1.1658 + 1.1659 + /* CELT processing */ 1.1660 + { 1.1661 + int endband=21; 1.1662 + 1.1663 + switch(curr_bandwidth) 1.1664 + { 1.1665 + case OPUS_BANDWIDTH_NARROWBAND: 1.1666 + endband = 13; 1.1667 + break; 1.1668 + case OPUS_BANDWIDTH_MEDIUMBAND: 1.1669 + case OPUS_BANDWIDTH_WIDEBAND: 1.1670 + endband = 17; 1.1671 + break; 1.1672 + case OPUS_BANDWIDTH_SUPERWIDEBAND: 1.1673 + endband = 19; 1.1674 + break; 1.1675 + case OPUS_BANDWIDTH_FULLBAND: 1.1676 + endband = 21; 1.1677 + break; 1.1678 + } 1.1679 + celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband)); 1.1680 + celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels)); 1.1681 + } 1.1682 + celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX)); 1.1683 + if (st->mode != MODE_SILK_ONLY) 1.1684 + { 1.1685 + opus_val32 celt_pred=2; 1.1686 + celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0)); 1.1687 + /* We may still decide to disable prediction later */ 1.1688 + if (st->silk_mode.reducedDependency) 1.1689 + celt_pred = 0; 1.1690 + celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred)); 1.1691 + 1.1692 + if (st->mode == MODE_HYBRID) 1.1693 + { 1.1694 + int len; 1.1695 + 1.1696 + len = (ec_tell(&enc)+7)>>3; 1.1697 + if (redundancy) 1.1698 + len += st->mode == MODE_HYBRID ? 3 : 1; 1.1699 + if( st->use_vbr ) { 1.1700 + nb_compr_bytes = len + bytes_target - (st->silk_mode.bitRate * frame_size) / (8 * st->Fs); 1.1701 + } else { 1.1702 + /* check if SILK used up too much */ 1.1703 + nb_compr_bytes = len > bytes_target ? len : bytes_target; 1.1704 + } 1.1705 + } else { 1.1706 + if (st->use_vbr) 1.1707 + { 1.1708 + opus_int32 bonus=0; 1.1709 +#ifndef DISABLE_FLOAT_API 1.1710 + if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != st->Fs/50) 1.1711 + { 1.1712 + bonus = (60*st->stream_channels+40)*(st->Fs/frame_size-50); 1.1713 + if (analysis_info.valid) 1.1714 + bonus = (opus_int32)(bonus*(1.f+.5f*analysis_info.tonality)); 1.1715 + } 1.1716 +#endif 1.1717 + celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1)); 1.1718 + celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint)); 1.1719 + celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps+bonus)); 1.1720 + nb_compr_bytes = max_data_bytes-1-redundancy_bytes; 1.1721 + } else { 1.1722 + nb_compr_bytes = bytes_target; 1.1723 + } 1.1724 + } 1.1725 + 1.1726 + } else { 1.1727 + nb_compr_bytes = 0; 1.1728 + } 1.1729 + 1.1730 + ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16); 1.1731 + if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0) 1.1732 + { 1.1733 + for (i=0;i<st->channels*st->Fs/400;i++) 1.1734 + tmp_prefill[i] = st->delay_buffer[(st->encoder_buffer-total_buffer-st->Fs/400)*st->channels + i]; 1.1735 + } 1.1736 + 1.1737 + for (i=0;i<st->channels*(st->encoder_buffer-(frame_size+total_buffer));i++) 1.1738 + st->delay_buffer[i] = st->delay_buffer[i+st->channels*frame_size]; 1.1739 + for (;i<st->encoder_buffer*st->channels;i++) 1.1740 + st->delay_buffer[i] = pcm_buf[(frame_size+total_buffer-st->encoder_buffer)*st->channels+i]; 1.1741 + 1.1742 + /* gain_fade() and stereo_fade() need to be after the buffer copying 1.1743 + because we don't want any of this to affect the SILK part */ 1.1744 + if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) { 1.1745 + gain_fade(pcm_buf, pcm_buf, 1.1746 + st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs); 1.1747 + } 1.1748 + st->prev_HB_gain = HB_gain; 1.1749 + if (st->mode != MODE_HYBRID || st->stream_channels==1) 1.1750 + st->silk_mode.stereoWidth_Q14 = IMIN((1<<14),2*IMAX(0,equiv_rate-30000)); 1.1751 + if( !st->energy_masking && st->channels == 2 ) { 1.1752 + /* Apply stereo width reduction (at low bitrates) */ 1.1753 + if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) { 1.1754 + opus_val16 g1, g2; 1.1755 + g1 = st->hybrid_stereo_width_Q14; 1.1756 + g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14); 1.1757 +#ifdef FIXED_POINT 1.1758 + g1 = g1==16384 ? Q15ONE : SHL16(g1,1); 1.1759 + g2 = g2==16384 ? Q15ONE : SHL16(g2,1); 1.1760 +#else 1.1761 + g1 *= (1.f/16384); 1.1762 + g2 *= (1.f/16384); 1.1763 +#endif 1.1764 + stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap, 1.1765 + frame_size, st->channels, celt_mode->window, st->Fs); 1.1766 + st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14; 1.1767 + } 1.1768 + } 1.1769 + 1.1770 + if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) <= 8*(max_data_bytes-1)) 1.1771 + { 1.1772 + /* For SILK mode, the redundancy is inferred from the length */ 1.1773 + if (st->mode == MODE_HYBRID && (redundancy || ec_tell(&enc)+37 <= 8*nb_compr_bytes)) 1.1774 + ec_enc_bit_logp(&enc, redundancy, 12); 1.1775 + if (redundancy) 1.1776 + { 1.1777 + int max_redundancy; 1.1778 + ec_enc_bit_logp(&enc, celt_to_silk, 1); 1.1779 + if (st->mode == MODE_HYBRID) 1.1780 + max_redundancy = (max_data_bytes-1)-nb_compr_bytes; 1.1781 + else 1.1782 + max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3); 1.1783 + /* Target the same bit-rate for redundancy as for the rest, 1.1784 + up to a max of 257 bytes */ 1.1785 + redundancy_bytes = IMIN(max_redundancy, st->bitrate_bps/1600); 1.1786 + redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes)); 1.1787 + if (st->mode == MODE_HYBRID) 1.1788 + ec_enc_uint(&enc, redundancy_bytes-2, 256); 1.1789 + } 1.1790 + } else { 1.1791 + redundancy = 0; 1.1792 + } 1.1793 + 1.1794 + if (!redundancy) 1.1795 + { 1.1796 + st->silk_bw_switch = 0; 1.1797 + redundancy_bytes = 0; 1.1798 + } 1.1799 + if (st->mode != MODE_CELT_ONLY)start_band=17; 1.1800 + 1.1801 + if (st->mode == MODE_SILK_ONLY) 1.1802 + { 1.1803 + ret = (ec_tell(&enc)+7)>>3; 1.1804 + ec_enc_done(&enc); 1.1805 + nb_compr_bytes = ret; 1.1806 + } else { 1.1807 + nb_compr_bytes = IMIN((max_data_bytes-1)-redundancy_bytes, nb_compr_bytes); 1.1808 + ec_enc_shrink(&enc, nb_compr_bytes); 1.1809 + } 1.1810 + 1.1811 +#ifndef DISABLE_FLOAT_API 1.1812 + if (redundancy || st->mode != MODE_SILK_ONLY) 1.1813 + celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(&analysis_info)); 1.1814 +#endif 1.1815 + 1.1816 + /* 5 ms redundant frame for CELT->SILK */ 1.1817 + if (redundancy && celt_to_silk) 1.1818 + { 1.1819 + int err; 1.1820 + celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)); 1.1821 + celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0)); 1.1822 + err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL); 1.1823 + if (err < 0) 1.1824 + { 1.1825 + RESTORE_STACK; 1.1826 + return OPUS_INTERNAL_ERROR; 1.1827 + } 1.1828 + celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng)); 1.1829 + celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); 1.1830 + } 1.1831 + 1.1832 + celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band)); 1.1833 + 1.1834 + if (st->mode != MODE_SILK_ONLY) 1.1835 + { 1.1836 + if (st->mode != st->prev_mode && st->prev_mode > 0) 1.1837 + { 1.1838 + unsigned char dummy[2]; 1.1839 + celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); 1.1840 + 1.1841 + /* Prefilling */ 1.1842 + celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL); 1.1843 + celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0)); 1.1844 + } 1.1845 + /* If false, we already busted the budget and we'll end up with a "PLC packet" */ 1.1846 + if (ec_tell(&enc) <= 8*nb_compr_bytes) 1.1847 + { 1.1848 + ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc); 1.1849 + if (ret < 0) 1.1850 + { 1.1851 + RESTORE_STACK; 1.1852 + return OPUS_INTERNAL_ERROR; 1.1853 + } 1.1854 + } 1.1855 + } 1.1856 + 1.1857 + /* 5 ms redundant frame for SILK->CELT */ 1.1858 + if (redundancy && !celt_to_silk) 1.1859 + { 1.1860 + int err; 1.1861 + unsigned char dummy[2]; 1.1862 + int N2, N4; 1.1863 + N2 = st->Fs/200; 1.1864 + N4 = st->Fs/400; 1.1865 + 1.1866 + celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); 1.1867 + celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)); 1.1868 + celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0)); 1.1869 + 1.1870 + /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */ 1.1871 + celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL); 1.1872 + 1.1873 + err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL); 1.1874 + if (err < 0) 1.1875 + { 1.1876 + RESTORE_STACK; 1.1877 + return OPUS_INTERNAL_ERROR; 1.1878 + } 1.1879 + celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng)); 1.1880 + } 1.1881 + 1.1882 + 1.1883 + 1.1884 + /* Signalling the mode in the first byte */ 1.1885 + data--; 1.1886 + data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels); 1.1887 + 1.1888 + st->rangeFinal = enc.rng ^ redundant_rng; 1.1889 + 1.1890 + if (to_celt) 1.1891 + st->prev_mode = MODE_CELT_ONLY; 1.1892 + else 1.1893 + st->prev_mode = st->mode; 1.1894 + st->prev_channels = st->stream_channels; 1.1895 + st->prev_framesize = frame_size; 1.1896 + 1.1897 + st->first = 0; 1.1898 + 1.1899 + /* In the unlikely case that the SILK encoder busted its target, tell 1.1900 + the decoder to call the PLC */ 1.1901 + if (ec_tell(&enc) > (max_data_bytes-1)*8) 1.1902 + { 1.1903 + if (max_data_bytes < 2) 1.1904 + { 1.1905 + RESTORE_STACK; 1.1906 + return OPUS_BUFFER_TOO_SMALL; 1.1907 + } 1.1908 + data[1] = 0; 1.1909 + ret = 1; 1.1910 + st->rangeFinal = 0; 1.1911 + } else if (st->mode==MODE_SILK_ONLY&&!redundancy) 1.1912 + { 1.1913 + /*When in LPC only mode it's perfectly 1.1914 + reasonable to strip off trailing zero bytes as 1.1915 + the required range decoder behavior is to 1.1916 + fill these in. This can't be done when the MDCT 1.1917 + modes are used because the decoder needs to know 1.1918 + the actual length for allocation purposes.*/ 1.1919 + while(ret>2&&data[ret]==0)ret--; 1.1920 + } 1.1921 + /* Count ToC and redundancy */ 1.1922 + ret += 1+redundancy_bytes; 1.1923 + if (!st->use_vbr) 1.1924 + { 1.1925 + if (opus_packet_pad(data, ret, max_data_bytes) != OPUS_OK) 1.1926 + 1.1927 + { 1.1928 + RESTORE_STACK; 1.1929 + return OPUS_INTERNAL_ERROR; 1.1930 + } 1.1931 + ret = max_data_bytes; 1.1932 + } 1.1933 + RESTORE_STACK; 1.1934 + return ret; 1.1935 +} 1.1936 + 1.1937 +#ifdef FIXED_POINT 1.1938 + 1.1939 +#ifndef DISABLE_FLOAT_API 1.1940 +opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size, 1.1941 + unsigned char *data, opus_int32 max_data_bytes) 1.1942 +{ 1.1943 + int i, ret; 1.1944 + int frame_size; 1.1945 + int delay_compensation; 1.1946 + VARDECL(opus_int16, in); 1.1947 + ALLOC_STACK; 1.1948 + 1.1949 + if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1.1950 + delay_compensation = 0; 1.1951 + else 1.1952 + delay_compensation = st->delay_compensation; 1.1953 + frame_size = compute_frame_size(pcm, analysis_frame_size, 1.1954 + st->variable_duration, st->channels, st->Fs, st->bitrate_bps, 1.1955 + delay_compensation, downmix_float, st->analysis.subframe_mem); 1.1956 + 1.1957 + ALLOC(in, frame_size*st->channels, opus_int16); 1.1958 + 1.1959 + for (i=0;i<frame_size*st->channels;i++) 1.1960 + in[i] = FLOAT2INT16(pcm[i]); 1.1961 + ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, pcm, analysis_frame_size, 0, -2, st->channels, downmix_float); 1.1962 + RESTORE_STACK; 1.1963 + return ret; 1.1964 +} 1.1965 +#endif 1.1966 + 1.1967 +opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size, 1.1968 + unsigned char *data, opus_int32 out_data_bytes) 1.1969 +{ 1.1970 + int frame_size; 1.1971 + int delay_compensation; 1.1972 + if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1.1973 + delay_compensation = 0; 1.1974 + else 1.1975 + delay_compensation = st->delay_compensation; 1.1976 + frame_size = compute_frame_size(pcm, analysis_frame_size, 1.1977 + st->variable_duration, st->channels, st->Fs, st->bitrate_bps, 1.1978 + delay_compensation, downmix_int 1.1979 +#ifndef DISABLE_FLOAT_API 1.1980 + , st->analysis.subframe_mem 1.1981 +#endif 1.1982 + ); 1.1983 + return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 16, pcm, analysis_frame_size, 0, -2, st->channels, downmix_int); 1.1984 +} 1.1985 + 1.1986 +#else 1.1987 +opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size, 1.1988 + unsigned char *data, opus_int32 max_data_bytes) 1.1989 +{ 1.1990 + int i, ret; 1.1991 + int frame_size; 1.1992 + int delay_compensation; 1.1993 + VARDECL(float, in); 1.1994 + ALLOC_STACK; 1.1995 + 1.1996 + if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1.1997 + delay_compensation = 0; 1.1998 + else 1.1999 + delay_compensation = st->delay_compensation; 1.2000 + frame_size = compute_frame_size(pcm, analysis_frame_size, 1.2001 + st->variable_duration, st->channels, st->Fs, st->bitrate_bps, 1.2002 + delay_compensation, downmix_int, st->analysis.subframe_mem); 1.2003 + 1.2004 + ALLOC(in, frame_size*st->channels, float); 1.2005 + 1.2006 + for (i=0;i<frame_size*st->channels;i++) 1.2007 + in[i] = (1.0f/32768)*pcm[i]; 1.2008 + ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, pcm, analysis_frame_size, 0, -2, st->channels, downmix_int); 1.2009 + RESTORE_STACK; 1.2010 + return ret; 1.2011 +} 1.2012 +opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size, 1.2013 + unsigned char *data, opus_int32 out_data_bytes) 1.2014 +{ 1.2015 + int frame_size; 1.2016 + int delay_compensation; 1.2017 + if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1.2018 + delay_compensation = 0; 1.2019 + else 1.2020 + delay_compensation = st->delay_compensation; 1.2021 + frame_size = compute_frame_size(pcm, analysis_frame_size, 1.2022 + st->variable_duration, st->channels, st->Fs, st->bitrate_bps, 1.2023 + delay_compensation, downmix_float, st->analysis.subframe_mem); 1.2024 + return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 24, 1.2025 + pcm, analysis_frame_size, 0, -2, st->channels, downmix_float); 1.2026 +} 1.2027 +#endif 1.2028 + 1.2029 + 1.2030 +int opus_encoder_ctl(OpusEncoder *st, int request, ...) 1.2031 +{ 1.2032 + int ret; 1.2033 + CELTEncoder *celt_enc; 1.2034 + va_list ap; 1.2035 + 1.2036 + ret = OPUS_OK; 1.2037 + va_start(ap, request); 1.2038 + 1.2039 + celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); 1.2040 + 1.2041 + switch (request) 1.2042 + { 1.2043 + case OPUS_SET_APPLICATION_REQUEST: 1.2044 + { 1.2045 + opus_int32 value = va_arg(ap, opus_int32); 1.2046 + if ( (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_AUDIO 1.2047 + && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1.2048 + || (!st->first && st->application != value)) 1.2049 + { 1.2050 + ret = OPUS_BAD_ARG; 1.2051 + break; 1.2052 + } 1.2053 + st->application = value; 1.2054 + } 1.2055 + break; 1.2056 + case OPUS_GET_APPLICATION_REQUEST: 1.2057 + { 1.2058 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2059 + if (!value) 1.2060 + { 1.2061 + goto bad_arg; 1.2062 + } 1.2063 + *value = st->application; 1.2064 + } 1.2065 + break; 1.2066 + case OPUS_SET_BITRATE_REQUEST: 1.2067 + { 1.2068 + opus_int32 value = va_arg(ap, opus_int32); 1.2069 + if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX) 1.2070 + { 1.2071 + if (value <= 0) 1.2072 + goto bad_arg; 1.2073 + else if (value <= 500) 1.2074 + value = 500; 1.2075 + else if (value > (opus_int32)300000*st->channels) 1.2076 + value = (opus_int32)300000*st->channels; 1.2077 + } 1.2078 + st->user_bitrate_bps = value; 1.2079 + } 1.2080 + break; 1.2081 + case OPUS_GET_BITRATE_REQUEST: 1.2082 + { 1.2083 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2084 + if (!value) 1.2085 + { 1.2086 + goto bad_arg; 1.2087 + } 1.2088 + *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276); 1.2089 + } 1.2090 + break; 1.2091 + case OPUS_SET_FORCE_CHANNELS_REQUEST: 1.2092 + { 1.2093 + opus_int32 value = va_arg(ap, opus_int32); 1.2094 + if((value<1 || value>st->channels) && value != OPUS_AUTO) 1.2095 + { 1.2096 + goto bad_arg; 1.2097 + } 1.2098 + st->force_channels = value; 1.2099 + } 1.2100 + break; 1.2101 + case OPUS_GET_FORCE_CHANNELS_REQUEST: 1.2102 + { 1.2103 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2104 + if (!value) 1.2105 + { 1.2106 + goto bad_arg; 1.2107 + } 1.2108 + *value = st->force_channels; 1.2109 + } 1.2110 + break; 1.2111 + case OPUS_SET_MAX_BANDWIDTH_REQUEST: 1.2112 + { 1.2113 + opus_int32 value = va_arg(ap, opus_int32); 1.2114 + if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) 1.2115 + { 1.2116 + goto bad_arg; 1.2117 + } 1.2118 + st->max_bandwidth = value; 1.2119 + if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { 1.2120 + st->silk_mode.maxInternalSampleRate = 8000; 1.2121 + } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { 1.2122 + st->silk_mode.maxInternalSampleRate = 12000; 1.2123 + } else { 1.2124 + st->silk_mode.maxInternalSampleRate = 16000; 1.2125 + } 1.2126 + } 1.2127 + break; 1.2128 + case OPUS_GET_MAX_BANDWIDTH_REQUEST: 1.2129 + { 1.2130 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2131 + if (!value) 1.2132 + { 1.2133 + goto bad_arg; 1.2134 + } 1.2135 + *value = st->max_bandwidth; 1.2136 + } 1.2137 + break; 1.2138 + case OPUS_SET_BANDWIDTH_REQUEST: 1.2139 + { 1.2140 + opus_int32 value = va_arg(ap, opus_int32); 1.2141 + if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO) 1.2142 + { 1.2143 + goto bad_arg; 1.2144 + } 1.2145 + st->user_bandwidth = value; 1.2146 + if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { 1.2147 + st->silk_mode.maxInternalSampleRate = 8000; 1.2148 + } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { 1.2149 + st->silk_mode.maxInternalSampleRate = 12000; 1.2150 + } else { 1.2151 + st->silk_mode.maxInternalSampleRate = 16000; 1.2152 + } 1.2153 + } 1.2154 + break; 1.2155 + case OPUS_GET_BANDWIDTH_REQUEST: 1.2156 + { 1.2157 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2158 + if (!value) 1.2159 + { 1.2160 + goto bad_arg; 1.2161 + } 1.2162 + *value = st->bandwidth; 1.2163 + } 1.2164 + break; 1.2165 + case OPUS_SET_DTX_REQUEST: 1.2166 + { 1.2167 + opus_int32 value = va_arg(ap, opus_int32); 1.2168 + if(value<0 || value>1) 1.2169 + { 1.2170 + goto bad_arg; 1.2171 + } 1.2172 + st->silk_mode.useDTX = value; 1.2173 + } 1.2174 + break; 1.2175 + case OPUS_GET_DTX_REQUEST: 1.2176 + { 1.2177 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2178 + if (!value) 1.2179 + { 1.2180 + goto bad_arg; 1.2181 + } 1.2182 + *value = st->silk_mode.useDTX; 1.2183 + } 1.2184 + break; 1.2185 + case OPUS_SET_COMPLEXITY_REQUEST: 1.2186 + { 1.2187 + opus_int32 value = va_arg(ap, opus_int32); 1.2188 + if(value<0 || value>10) 1.2189 + { 1.2190 + goto bad_arg; 1.2191 + } 1.2192 + st->silk_mode.complexity = value; 1.2193 + celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value)); 1.2194 + } 1.2195 + break; 1.2196 + case OPUS_GET_COMPLEXITY_REQUEST: 1.2197 + { 1.2198 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2199 + if (!value) 1.2200 + { 1.2201 + goto bad_arg; 1.2202 + } 1.2203 + *value = st->silk_mode.complexity; 1.2204 + } 1.2205 + break; 1.2206 + case OPUS_SET_INBAND_FEC_REQUEST: 1.2207 + { 1.2208 + opus_int32 value = va_arg(ap, opus_int32); 1.2209 + if(value<0 || value>1) 1.2210 + { 1.2211 + goto bad_arg; 1.2212 + } 1.2213 + st->silk_mode.useInBandFEC = value; 1.2214 + } 1.2215 + break; 1.2216 + case OPUS_GET_INBAND_FEC_REQUEST: 1.2217 + { 1.2218 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2219 + if (!value) 1.2220 + { 1.2221 + goto bad_arg; 1.2222 + } 1.2223 + *value = st->silk_mode.useInBandFEC; 1.2224 + } 1.2225 + break; 1.2226 + case OPUS_SET_PACKET_LOSS_PERC_REQUEST: 1.2227 + { 1.2228 + opus_int32 value = va_arg(ap, opus_int32); 1.2229 + if (value < 0 || value > 100) 1.2230 + { 1.2231 + goto bad_arg; 1.2232 + } 1.2233 + st->silk_mode.packetLossPercentage = value; 1.2234 + celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value)); 1.2235 + } 1.2236 + break; 1.2237 + case OPUS_GET_PACKET_LOSS_PERC_REQUEST: 1.2238 + { 1.2239 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2240 + if (!value) 1.2241 + { 1.2242 + goto bad_arg; 1.2243 + } 1.2244 + *value = st->silk_mode.packetLossPercentage; 1.2245 + } 1.2246 + break; 1.2247 + case OPUS_SET_VBR_REQUEST: 1.2248 + { 1.2249 + opus_int32 value = va_arg(ap, opus_int32); 1.2250 + if(value<0 || value>1) 1.2251 + { 1.2252 + goto bad_arg; 1.2253 + } 1.2254 + st->use_vbr = value; 1.2255 + st->silk_mode.useCBR = 1-value; 1.2256 + } 1.2257 + break; 1.2258 + case OPUS_GET_VBR_REQUEST: 1.2259 + { 1.2260 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2261 + if (!value) 1.2262 + { 1.2263 + goto bad_arg; 1.2264 + } 1.2265 + *value = st->use_vbr; 1.2266 + } 1.2267 + break; 1.2268 + case OPUS_SET_VOICE_RATIO_REQUEST: 1.2269 + { 1.2270 + opus_int32 value = va_arg(ap, opus_int32); 1.2271 + if (value<-1 || value>100) 1.2272 + { 1.2273 + goto bad_arg; 1.2274 + } 1.2275 + st->voice_ratio = value; 1.2276 + } 1.2277 + break; 1.2278 + case OPUS_GET_VOICE_RATIO_REQUEST: 1.2279 + { 1.2280 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2281 + if (!value) 1.2282 + { 1.2283 + goto bad_arg; 1.2284 + } 1.2285 + *value = st->voice_ratio; 1.2286 + } 1.2287 + break; 1.2288 + case OPUS_SET_VBR_CONSTRAINT_REQUEST: 1.2289 + { 1.2290 + opus_int32 value = va_arg(ap, opus_int32); 1.2291 + if(value<0 || value>1) 1.2292 + { 1.2293 + goto bad_arg; 1.2294 + } 1.2295 + st->vbr_constraint = value; 1.2296 + } 1.2297 + break; 1.2298 + case OPUS_GET_VBR_CONSTRAINT_REQUEST: 1.2299 + { 1.2300 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2301 + if (!value) 1.2302 + { 1.2303 + goto bad_arg; 1.2304 + } 1.2305 + *value = st->vbr_constraint; 1.2306 + } 1.2307 + break; 1.2308 + case OPUS_SET_SIGNAL_REQUEST: 1.2309 + { 1.2310 + opus_int32 value = va_arg(ap, opus_int32); 1.2311 + if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNAL_MUSIC) 1.2312 + { 1.2313 + goto bad_arg; 1.2314 + } 1.2315 + st->signal_type = value; 1.2316 + } 1.2317 + break; 1.2318 + case OPUS_GET_SIGNAL_REQUEST: 1.2319 + { 1.2320 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2321 + if (!value) 1.2322 + { 1.2323 + goto bad_arg; 1.2324 + } 1.2325 + *value = st->signal_type; 1.2326 + } 1.2327 + break; 1.2328 + case OPUS_GET_LOOKAHEAD_REQUEST: 1.2329 + { 1.2330 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2331 + if (!value) 1.2332 + { 1.2333 + goto bad_arg; 1.2334 + } 1.2335 + *value = st->Fs/400; 1.2336 + if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1.2337 + *value += st->delay_compensation; 1.2338 + } 1.2339 + break; 1.2340 + case OPUS_GET_SAMPLE_RATE_REQUEST: 1.2341 + { 1.2342 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2343 + if (!value) 1.2344 + { 1.2345 + goto bad_arg; 1.2346 + } 1.2347 + *value = st->Fs; 1.2348 + } 1.2349 + break; 1.2350 + case OPUS_GET_FINAL_RANGE_REQUEST: 1.2351 + { 1.2352 + opus_uint32 *value = va_arg(ap, opus_uint32*); 1.2353 + if (!value) 1.2354 + { 1.2355 + goto bad_arg; 1.2356 + } 1.2357 + *value = st->rangeFinal; 1.2358 + } 1.2359 + break; 1.2360 + case OPUS_SET_LSB_DEPTH_REQUEST: 1.2361 + { 1.2362 + opus_int32 value = va_arg(ap, opus_int32); 1.2363 + if (value<8 || value>24) 1.2364 + { 1.2365 + goto bad_arg; 1.2366 + } 1.2367 + st->lsb_depth=value; 1.2368 + } 1.2369 + break; 1.2370 + case OPUS_GET_LSB_DEPTH_REQUEST: 1.2371 + { 1.2372 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2373 + if (!value) 1.2374 + { 1.2375 + goto bad_arg; 1.2376 + } 1.2377 + *value = st->lsb_depth; 1.2378 + } 1.2379 + break; 1.2380 + case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST: 1.2381 + { 1.2382 + opus_int32 value = va_arg(ap, opus_int32); 1.2383 + if (value != OPUS_FRAMESIZE_ARG && value != OPUS_FRAMESIZE_2_5_MS && 1.2384 + value != OPUS_FRAMESIZE_5_MS && value != OPUS_FRAMESIZE_10_MS && 1.2385 + value != OPUS_FRAMESIZE_20_MS && value != OPUS_FRAMESIZE_40_MS && 1.2386 + value != OPUS_FRAMESIZE_60_MS && value != OPUS_FRAMESIZE_VARIABLE) 1.2387 + { 1.2388 + goto bad_arg; 1.2389 + } 1.2390 + st->variable_duration = value; 1.2391 + celt_encoder_ctl(celt_enc, OPUS_SET_EXPERT_FRAME_DURATION(value)); 1.2392 + } 1.2393 + break; 1.2394 + case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST: 1.2395 + { 1.2396 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2397 + if (!value) 1.2398 + { 1.2399 + goto bad_arg; 1.2400 + } 1.2401 + *value = st->variable_duration; 1.2402 + } 1.2403 + break; 1.2404 + case OPUS_SET_PREDICTION_DISABLED_REQUEST: 1.2405 + { 1.2406 + opus_int32 value = va_arg(ap, opus_int32); 1.2407 + if (value > 1 || value < 0) 1.2408 + goto bad_arg; 1.2409 + st->silk_mode.reducedDependency = value; 1.2410 + } 1.2411 + break; 1.2412 + case OPUS_GET_PREDICTION_DISABLED_REQUEST: 1.2413 + { 1.2414 + opus_int32 *value = va_arg(ap, opus_int32*); 1.2415 + if (!value) 1.2416 + goto bad_arg; 1.2417 + *value = st->silk_mode.reducedDependency; 1.2418 + } 1.2419 + break; 1.2420 + case OPUS_RESET_STATE: 1.2421 + { 1.2422 + void *silk_enc; 1.2423 + silk_EncControlStruct dummy; 1.2424 + silk_enc = (char*)st+st->silk_enc_offset; 1.2425 + 1.2426 + OPUS_CLEAR((char*)&st->OPUS_ENCODER_RESET_START, 1.2427 + sizeof(OpusEncoder)- 1.2428 + ((char*)&st->OPUS_ENCODER_RESET_START - (char*)st)); 1.2429 + 1.2430 + celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); 1.2431 + silk_InitEncoder( silk_enc, st->arch, &dummy ); 1.2432 + st->stream_channels = st->channels; 1.2433 + st->hybrid_stereo_width_Q14 = 1 << 14; 1.2434 + st->prev_HB_gain = Q15ONE; 1.2435 + st->first = 1; 1.2436 + st->mode = MODE_HYBRID; 1.2437 + st->bandwidth = OPUS_BANDWIDTH_FULLBAND; 1.2438 + st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); 1.2439 + } 1.2440 + break; 1.2441 + case OPUS_SET_FORCE_MODE_REQUEST: 1.2442 + { 1.2443 + opus_int32 value = va_arg(ap, opus_int32); 1.2444 + if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO) 1.2445 + { 1.2446 + goto bad_arg; 1.2447 + } 1.2448 + st->user_forced_mode = value; 1.2449 + } 1.2450 + break; 1.2451 + case OPUS_SET_LFE_REQUEST: 1.2452 + { 1.2453 + opus_int32 value = va_arg(ap, opus_int32); 1.2454 + st->lfe = value; 1.2455 + ret = celt_encoder_ctl(celt_enc, OPUS_SET_LFE(value)); 1.2456 + } 1.2457 + break; 1.2458 + case OPUS_SET_ENERGY_MASK_REQUEST: 1.2459 + { 1.2460 + opus_val16 *value = va_arg(ap, opus_val16*); 1.2461 + st->energy_masking = value; 1.2462 + ret = celt_encoder_ctl(celt_enc, OPUS_SET_ENERGY_MASK(value)); 1.2463 + } 1.2464 + break; 1.2465 + 1.2466 + case CELT_GET_MODE_REQUEST: 1.2467 + { 1.2468 + const CELTMode ** value = va_arg(ap, const CELTMode**); 1.2469 + if (!value) 1.2470 + { 1.2471 + goto bad_arg; 1.2472 + } 1.2473 + ret = celt_encoder_ctl(celt_enc, CELT_GET_MODE(value)); 1.2474 + } 1.2475 + break; 1.2476 + default: 1.2477 + /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/ 1.2478 + ret = OPUS_UNIMPLEMENTED; 1.2479 + break; 1.2480 + } 1.2481 + va_end(ap); 1.2482 + return ret; 1.2483 +bad_arg: 1.2484 + va_end(ap); 1.2485 + return OPUS_BAD_ARG; 1.2486 +} 1.2487 + 1.2488 +void opus_encoder_destroy(OpusEncoder *st) 1.2489 +{ 1.2490 + opus_free(st); 1.2491 +}