michael@0: /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited michael@0: Written by Jean-Marc Valin and Koen Vos */ michael@0: /* michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions michael@0: are met: michael@0: michael@0: - Redistributions of source code must retain the above copyright michael@0: notice, this list of conditions and the following disclaimer. michael@0: michael@0: - Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER michael@0: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifdef HAVE_CONFIG_H michael@0: #include "config.h" michael@0: #endif michael@0: michael@0: #include michael@0: #include "celt.h" michael@0: #include "entenc.h" michael@0: #include "modes.h" michael@0: #include "API.h" michael@0: #include "stack_alloc.h" michael@0: #include "float_cast.h" michael@0: #include "opus.h" michael@0: #include "arch.h" michael@0: #include "opus_private.h" michael@0: #include "os_support.h" michael@0: #include "cpu_support.h" michael@0: #include "analysis.h" michael@0: #include "mathops.h" michael@0: #include "tuning_parameters.h" michael@0: #ifdef FIXED_POINT michael@0: #include "fixed/structs_FIX.h" michael@0: #else michael@0: #include "float/structs_FLP.h" michael@0: #endif michael@0: michael@0: #define MAX_ENCODER_BUFFER 480 michael@0: michael@0: typedef struct { michael@0: opus_val32 XX, XY, YY; michael@0: opus_val16 smoothed_width; michael@0: opus_val16 max_follower; michael@0: } StereoWidthState; michael@0: michael@0: struct OpusEncoder { michael@0: int celt_enc_offset; michael@0: int silk_enc_offset; michael@0: silk_EncControlStruct silk_mode; michael@0: int application; michael@0: int channels; michael@0: int delay_compensation; michael@0: int force_channels; michael@0: int signal_type; michael@0: int user_bandwidth; michael@0: int max_bandwidth; michael@0: int user_forced_mode; michael@0: int voice_ratio; michael@0: opus_int32 Fs; michael@0: int use_vbr; michael@0: int vbr_constraint; michael@0: int variable_duration; michael@0: opus_int32 bitrate_bps; michael@0: opus_int32 user_bitrate_bps; michael@0: int lsb_depth; michael@0: int encoder_buffer; michael@0: int lfe; michael@0: michael@0: #define OPUS_ENCODER_RESET_START stream_channels michael@0: int stream_channels; michael@0: opus_int16 hybrid_stereo_width_Q14; michael@0: opus_int32 variable_HP_smth2_Q15; michael@0: opus_val16 prev_HB_gain; michael@0: opus_val32 hp_mem[4]; michael@0: int mode; michael@0: int prev_mode; michael@0: int prev_channels; michael@0: int prev_framesize; michael@0: int bandwidth; michael@0: int silk_bw_switch; michael@0: /* Sampling rate (at the API level) */ michael@0: int first; michael@0: opus_val16 * energy_masking; michael@0: StereoWidthState width_mem; michael@0: opus_val16 delay_buffer[MAX_ENCODER_BUFFER*2]; michael@0: #ifndef DISABLE_FLOAT_API michael@0: TonalityAnalysisState analysis; michael@0: int detected_bandwidth; michael@0: int analysis_offset; michael@0: #endif michael@0: opus_uint32 rangeFinal; michael@0: int arch; michael@0: }; michael@0: michael@0: /* Transition tables for the voice and music. First column is the michael@0: middle (memoriless) threshold. The second column is the hysteresis michael@0: (difference with the middle) */ michael@0: static const opus_int32 mono_voice_bandwidth_thresholds[8] = { michael@0: 11000, 1000, /* NB<->MB */ michael@0: 14000, 1000, /* MB<->WB */ michael@0: 17000, 1000, /* WB<->SWB */ michael@0: 21000, 2000, /* SWB<->FB */ michael@0: }; michael@0: static const opus_int32 mono_music_bandwidth_thresholds[8] = { michael@0: 12000, 1000, /* NB<->MB */ michael@0: 15000, 1000, /* MB<->WB */ michael@0: 18000, 2000, /* WB<->SWB */ michael@0: 22000, 2000, /* SWB<->FB */ michael@0: }; michael@0: static const opus_int32 stereo_voice_bandwidth_thresholds[8] = { michael@0: 11000, 1000, /* NB<->MB */ michael@0: 14000, 1000, /* MB<->WB */ michael@0: 21000, 2000, /* WB<->SWB */ michael@0: 28000, 2000, /* SWB<->FB */ michael@0: }; michael@0: static const opus_int32 stereo_music_bandwidth_thresholds[8] = { michael@0: 12000, 1000, /* NB<->MB */ michael@0: 18000, 2000, /* MB<->WB */ michael@0: 21000, 2000, /* WB<->SWB */ michael@0: 30000, 2000, /* SWB<->FB */ michael@0: }; michael@0: /* Threshold bit-rates for switching between mono and stereo */ michael@0: static const opus_int32 stereo_voice_threshold = 30000; michael@0: static const opus_int32 stereo_music_threshold = 30000; michael@0: michael@0: /* Threshold bit-rate for switching between SILK/hybrid and CELT-only */ michael@0: static const opus_int32 mode_thresholds[2][2] = { michael@0: /* voice */ /* music */ michael@0: { 64000, 16000}, /* mono */ michael@0: { 36000, 16000}, /* stereo */ michael@0: }; michael@0: michael@0: int opus_encoder_get_size(int channels) michael@0: { michael@0: int silkEncSizeBytes, celtEncSizeBytes; michael@0: int ret; michael@0: if (channels<1 || channels > 2) michael@0: return 0; michael@0: ret = silk_Get_Encoder_Size( &silkEncSizeBytes ); michael@0: if (ret) michael@0: return 0; michael@0: silkEncSizeBytes = align(silkEncSizeBytes); michael@0: celtEncSizeBytes = celt_encoder_get_size(channels); michael@0: return align(sizeof(OpusEncoder))+silkEncSizeBytes+celtEncSizeBytes; michael@0: } michael@0: michael@0: int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int application) michael@0: { michael@0: void *silk_enc; michael@0: CELTEncoder *celt_enc; michael@0: int err; michael@0: int ret, silkEncSizeBytes; michael@0: michael@0: if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2)|| michael@0: (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO michael@0: && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)) michael@0: return OPUS_BAD_ARG; michael@0: michael@0: OPUS_CLEAR((char*)st, opus_encoder_get_size(channels)); michael@0: /* Create SILK encoder */ michael@0: ret = silk_Get_Encoder_Size( &silkEncSizeBytes ); michael@0: if (ret) michael@0: return OPUS_BAD_ARG; michael@0: silkEncSizeBytes = align(silkEncSizeBytes); michael@0: st->silk_enc_offset = align(sizeof(OpusEncoder)); michael@0: st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes; michael@0: silk_enc = (char*)st+st->silk_enc_offset; michael@0: celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); michael@0: michael@0: st->stream_channels = st->channels = channels; michael@0: michael@0: st->Fs = Fs; michael@0: michael@0: st->arch = opus_select_arch(); michael@0: michael@0: ret = silk_InitEncoder( silk_enc, st->arch, &st->silk_mode ); michael@0: if(ret)return OPUS_INTERNAL_ERROR; michael@0: michael@0: /* default SILK parameters */ michael@0: st->silk_mode.nChannelsAPI = channels; michael@0: st->silk_mode.nChannelsInternal = channels; michael@0: st->silk_mode.API_sampleRate = st->Fs; michael@0: st->silk_mode.maxInternalSampleRate = 16000; michael@0: st->silk_mode.minInternalSampleRate = 8000; michael@0: st->silk_mode.desiredInternalSampleRate = 16000; michael@0: st->silk_mode.payloadSize_ms = 20; michael@0: st->silk_mode.bitRate = 25000; michael@0: st->silk_mode.packetLossPercentage = 0; michael@0: st->silk_mode.complexity = 9; michael@0: st->silk_mode.useInBandFEC = 0; michael@0: st->silk_mode.useDTX = 0; michael@0: st->silk_mode.useCBR = 0; michael@0: st->silk_mode.reducedDependency = 0; michael@0: michael@0: /* Create CELT encoder */ michael@0: /* Initialize CELT encoder */ michael@0: err = celt_encoder_init(celt_enc, Fs, channels, st->arch); michael@0: if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR; michael@0: michael@0: celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0)); michael@0: celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(st->silk_mode.complexity)); michael@0: michael@0: st->use_vbr = 1; michael@0: /* Makes constrained VBR the default (safer for real-time use) */ michael@0: st->vbr_constraint = 1; michael@0: st->user_bitrate_bps = OPUS_AUTO; michael@0: st->bitrate_bps = 3000+Fs*channels; michael@0: st->application = application; michael@0: st->signal_type = OPUS_AUTO; michael@0: st->user_bandwidth = OPUS_AUTO; michael@0: st->max_bandwidth = OPUS_BANDWIDTH_FULLBAND; michael@0: st->force_channels = OPUS_AUTO; michael@0: st->user_forced_mode = OPUS_AUTO; michael@0: st->voice_ratio = -1; michael@0: st->encoder_buffer = st->Fs/100; michael@0: st->lsb_depth = 24; michael@0: st->variable_duration = OPUS_FRAMESIZE_ARG; michael@0: michael@0: /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead michael@0: + 1.5 ms for SILK resamplers and stereo prediction) */ michael@0: st->delay_compensation = st->Fs/250; michael@0: michael@0: st->hybrid_stereo_width_Q14 = 1 << 14; michael@0: st->prev_HB_gain = Q15ONE; michael@0: st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); michael@0: st->first = 1; michael@0: st->mode = MODE_HYBRID; michael@0: st->bandwidth = OPUS_BANDWIDTH_FULLBAND; michael@0: michael@0: return OPUS_OK; michael@0: } michael@0: michael@0: static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channels) michael@0: { michael@0: int period; michael@0: unsigned char toc; michael@0: period = 0; michael@0: while (framerate < 400) michael@0: { michael@0: framerate <<= 1; michael@0: period++; michael@0: } michael@0: if (mode == MODE_SILK_ONLY) michael@0: { michael@0: toc = (bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5; michael@0: toc |= (period-2)<<3; michael@0: } else if (mode == MODE_CELT_ONLY) michael@0: { michael@0: int tmp = bandwidth-OPUS_BANDWIDTH_MEDIUMBAND; michael@0: if (tmp < 0) michael@0: tmp = 0; michael@0: toc = 0x80; michael@0: toc |= tmp << 5; michael@0: toc |= period<<3; michael@0: } else /* Hybrid */ michael@0: { michael@0: toc = 0x60; michael@0: toc |= (bandwidth-OPUS_BANDWIDTH_SUPERWIDEBAND)<<4; michael@0: toc |= (period-2)<<3; michael@0: } michael@0: toc |= (channels==2)<<2; michael@0: return toc; michael@0: } michael@0: michael@0: #ifndef FIXED_POINT michael@0: static void silk_biquad_float( michael@0: const opus_val16 *in, /* I: Input signal */ michael@0: const opus_int32 *B_Q28, /* I: MA coefficients [3] */ michael@0: const opus_int32 *A_Q28, /* I: AR coefficients [2] */ michael@0: opus_val32 *S, /* I/O: State vector [2] */ michael@0: opus_val16 *out, /* O: Output signal */ michael@0: const opus_int32 len, /* I: Signal length (must be even) */ michael@0: int stride michael@0: ) michael@0: { michael@0: /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */ michael@0: opus_int k; michael@0: opus_val32 vout; michael@0: opus_val32 inval; michael@0: opus_val32 A[2], B[3]; michael@0: michael@0: A[0] = (opus_val32)(A_Q28[0] * (1.f/((opus_int32)1<<28))); michael@0: A[1] = (opus_val32)(A_Q28[1] * (1.f/((opus_int32)1<<28))); michael@0: B[0] = (opus_val32)(B_Q28[0] * (1.f/((opus_int32)1<<28))); michael@0: B[1] = (opus_val32)(B_Q28[1] * (1.f/((opus_int32)1<<28))); michael@0: B[2] = (opus_val32)(B_Q28[2] * (1.f/((opus_int32)1<<28))); michael@0: michael@0: /* Negate A_Q28 values and split in two parts */ michael@0: michael@0: for( k = 0; k < len; k++ ) { michael@0: /* S[ 0 ], S[ 1 ]: Q12 */ michael@0: inval = in[ k*stride ]; michael@0: vout = S[ 0 ] + B[0]*inval; michael@0: michael@0: S[ 0 ] = S[1] - vout*A[0] + B[1]*inval; michael@0: michael@0: S[ 1 ] = - vout*A[1] + B[2]*inval + VERY_SMALL; michael@0: michael@0: /* Scale back to Q0 and saturate */ michael@0: out[ k*stride ] = vout; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: 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) michael@0: { michael@0: opus_int32 B_Q28[ 3 ], A_Q28[ 2 ]; michael@0: opus_int32 Fc_Q19, r_Q28, r_Q22; michael@0: michael@0: silk_assert( cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ) ); michael@0: Fc_Q19 = silk_DIV32_16( silk_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 ); michael@0: silk_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 ); michael@0: michael@0: r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - silk_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q19 ); michael@0: michael@0: /* b = r * [ 1; -2; 1 ]; */ michael@0: /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */ michael@0: B_Q28[ 0 ] = r_Q28; michael@0: B_Q28[ 1 ] = silk_LSHIFT( -r_Q28, 1 ); michael@0: B_Q28[ 2 ] = r_Q28; michael@0: michael@0: /* -r * ( 2 - Fc * Fc ); */ michael@0: r_Q22 = silk_RSHIFT( r_Q28, 6 ); michael@0: A_Q28[ 0 ] = silk_SMULWW( r_Q22, silk_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CONST( 2.0, 22 ) ); michael@0: A_Q28[ 1 ] = silk_SMULWW( r_Q22, r_Q22 ); michael@0: michael@0: #ifdef FIXED_POINT michael@0: silk_biquad_alt( in, B_Q28, A_Q28, hp_mem, out, len, channels ); michael@0: if( channels == 2 ) { michael@0: silk_biquad_alt( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels ); michael@0: } michael@0: #else michael@0: silk_biquad_float( in, B_Q28, A_Q28, hp_mem, out, len, channels ); michael@0: if( channels == 2 ) { michael@0: silk_biquad_float( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels ); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: #ifdef FIXED_POINT michael@0: 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) michael@0: { michael@0: int c, i; michael@0: int shift; michael@0: michael@0: /* Approximates -round(log2(4.*cutoff_Hz/Fs)) */ michael@0: shift=celt_ilog2(Fs/(cutoff_Hz*3)); michael@0: for (c=0;cFs/400; michael@0: if (st->user_bitrate_bps==OPUS_AUTO) michael@0: return 60*st->Fs/frame_size + st->Fs*st->channels; michael@0: else if (st->user_bitrate_bps==OPUS_BITRATE_MAX) michael@0: return max_data_bytes*8*st->Fs/frame_size; michael@0: else michael@0: return st->user_bitrate_bps; michael@0: } michael@0: michael@0: #ifndef DISABLE_FLOAT_API michael@0: /* Don't use more than 60 ms for the frame size analysis */ michael@0: #define MAX_DYNAMIC_FRAMESIZE 24 michael@0: /* Estimates how much the bitrate will be boosted based on the sub-frame energy */ michael@0: static float transient_boost(const float *E, const float *E_1, int LM, int maxM) michael@0: { michael@0: int i; michael@0: int M; michael@0: float sumE=0, sumE_1=0; michael@0: float metric; michael@0: michael@0: M = IMIN(maxM, (1<10 ? 1 : 0;*/ michael@0: /*return MAX16(0,1-exp(-.25*(metric-2.)));*/ michael@0: return MIN16(1,(float)sqrt(MAX16(0,.05f*(metric-2)))); michael@0: } michael@0: michael@0: /* Viterbi decoding trying to find the best frame size combination using look-ahead michael@0: michael@0: State numbering: michael@0: 0: unused michael@0: 1: 2.5 ms michael@0: 2: 5 ms (#1) michael@0: 3: 5 ms (#2) michael@0: 4: 10 ms (#1) michael@0: 5: 10 ms (#2) michael@0: 6: 10 ms (#3) michael@0: 7: 10 ms (#4) michael@0: 8: 20 ms (#1) michael@0: 9: 20 ms (#2) michael@0: 10: 20 ms (#3) michael@0: 11: 20 ms (#4) michael@0: 12: 20 ms (#5) michael@0: 13: 20 ms (#6) michael@0: 14: 20 ms (#7) michael@0: 15: 20 ms (#8) michael@0: */ michael@0: static int transient_viterbi(const float *E, const float *E_1, int N, int frame_cost, int rate) michael@0: { michael@0: int i; michael@0: float cost[MAX_DYNAMIC_FRAMESIZE][16]; michael@0: int states[MAX_DYNAMIC_FRAMESIZE][16]; michael@0: float best_cost; michael@0: int best_state; michael@0: float factor; michael@0: /* Take into account that we damp VBR in the 32 kb/s to 64 kb/s range. */ michael@0: if (rate<80) michael@0: factor=0; michael@0: else if (rate>160) michael@0: factor=1; michael@0: else michael@0: factor = (rate-80.f)/80.f; michael@0: /* Makes variable framesize less aggressive at lower bitrates, but I can't michael@0: find any valid theoretical justification for this (other than it seems michael@0: to help) */ michael@0: for (i=0;i<16;i++) michael@0: { michael@0: /* Impossible state */ michael@0: states[0][i] = -1; michael@0: cost[0][i] = 1e10; michael@0: } michael@0: for (i=0;i<4;i++) michael@0: { michael@0: cost[0][1<=0;i--) michael@0: { michael@0: /*printf("%d ", best_state);*/ michael@0: best_state = states[i][best_state]; michael@0: } michael@0: /*printf("%d\n", best_state);*/ michael@0: return best_state; michael@0: } michael@0: michael@0: int optimize_framesize(const opus_val16 *x, int len, int C, opus_int32 Fs, michael@0: int bitrate, opus_val16 tonality, float *mem, int buffering, michael@0: downmix_func downmix) michael@0: { michael@0: int N; michael@0: int i; michael@0: float e[MAX_DYNAMIC_FRAMESIZE+4]; michael@0: float e_1[MAX_DYNAMIC_FRAMESIZE+3]; michael@0: opus_val32 memx; michael@0: int bestLM=0; michael@0: int subframe; michael@0: int pos; michael@0: VARDECL(opus_val32, sub); michael@0: michael@0: subframe = Fs/400; michael@0: ALLOC(sub, subframe, opus_val32); michael@0: e[0]=mem[0]; michael@0: e_1[0]=1.f/(EPSILON+mem[0]); michael@0: if (buffering) michael@0: { michael@0: /* Consider the CELT delay when not in restricted-lowdelay */ michael@0: /* We assume the buffering is between 2.5 and 5 ms */ michael@0: int offset = 2*subframe - buffering; michael@0: celt_assert(offset>=0 && offset <= subframe); michael@0: x += C*offset; michael@0: len -= offset; michael@0: e[1]=mem[1]; michael@0: e_1[1]=1.f/(EPSILON+mem[1]); michael@0: e[2]=mem[2]; michael@0: e_1[2]=1.f/(EPSILON+mem[2]); michael@0: pos = 3; michael@0: } else { michael@0: pos=1; michael@0: } michael@0: N=IMIN(len/subframe, MAX_DYNAMIC_FRAMESIZE); michael@0: /* Just silencing a warning, it's really initialized later */ michael@0: memx = 0; michael@0: for (i=0;i-1) michael@0: { michael@0: for (j=0;j-1) michael@0: { michael@0: for (j=0;j= OPUS_FRAMESIZE_2_5_MS && variable_duration <= OPUS_FRAMESIZE_60_MS) michael@0: new_size = IMIN(3*Fs/50, (Fs/400)<<(variable_duration-OPUS_FRAMESIZE_2_5_MS)); michael@0: else michael@0: return -1; michael@0: if (new_size>frame_size) michael@0: return -1; michael@0: if (400*new_size!=Fs && 200*new_size!=Fs && 100*new_size!=Fs && michael@0: 50*new_size!=Fs && 25*new_size!=Fs && 50*new_size!=3*Fs) michael@0: return -1; michael@0: return new_size; michael@0: } michael@0: michael@0: opus_int32 compute_frame_size(const void *analysis_pcm, int frame_size, michael@0: int variable_duration, int C, opus_int32 Fs, int bitrate_bps, michael@0: int delay_compensation, downmix_func downmix michael@0: #ifndef DISABLE_FLOAT_API michael@0: , float *subframe_mem michael@0: #endif michael@0: ) michael@0: { michael@0: #ifndef DISABLE_FLOAT_API michael@0: if (variable_duration == OPUS_FRAMESIZE_VARIABLE && frame_size >= Fs/200) michael@0: { michael@0: int LM = 3; michael@0: LM = optimize_framesize(analysis_pcm, frame_size, C, Fs, bitrate_bps, michael@0: 0, subframe_mem, delay_compensation, downmix); michael@0: while ((Fs/400<frame_size) michael@0: LM--; michael@0: frame_size = (Fs/400<XX += MULT16_32_Q15(short_alpha, xx-mem->XX); michael@0: mem->XY += MULT16_32_Q15(short_alpha, xy-mem->XY); michael@0: mem->YY += MULT16_32_Q15(short_alpha, yy-mem->YY); michael@0: mem->XX = MAX32(0, mem->XX); michael@0: mem->XY = MAX32(0, mem->XY); michael@0: mem->YY = MAX32(0, mem->YY); michael@0: if (MAX32(mem->XX, mem->YY)>QCONST16(8e-4f, 18)) michael@0: { michael@0: sqrt_xx = celt_sqrt(mem->XX); michael@0: sqrt_yy = celt_sqrt(mem->YY); michael@0: qrrt_xx = celt_sqrt(sqrt_xx); michael@0: qrrt_yy = celt_sqrt(sqrt_yy); michael@0: /* Inter-channel correlation */ michael@0: mem->XY = MIN32(mem->XY, sqrt_xx*sqrt_yy); michael@0: corr = SHR32(frac_div32(mem->XY,EPSILON+MULT16_16(sqrt_xx,sqrt_yy)),16); michael@0: /* Approximate loudness difference */ michael@0: ldiff = Q15ONE*ABS16(qrrt_xx-qrrt_yy)/(EPSILON+qrrt_xx+qrrt_yy); michael@0: width = MULT16_16_Q15(celt_sqrt(QCONST32(1.f,30)-MULT16_16(corr,corr)), ldiff); michael@0: /* Smoothing over one second */ michael@0: mem->smoothed_width += (width-mem->smoothed_width)/frame_rate; michael@0: /* Peak follower */ michael@0: mem->max_follower = MAX16(mem->max_follower-QCONST16(.02f,15)/frame_rate, mem->smoothed_width); michael@0: } else { michael@0: width = 0; michael@0: corr=Q15ONE; michael@0: ldiff=0; michael@0: } michael@0: /*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);*/ michael@0: return EXTRACT16(MIN32(Q15ONE,20*mem->max_follower)); michael@0: } michael@0: michael@0: opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_size, michael@0: unsigned char *data, opus_int32 out_data_bytes, int lsb_depth, michael@0: const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2, int analysis_channels, downmix_func downmix) michael@0: { michael@0: void *silk_enc; michael@0: CELTEncoder *celt_enc; michael@0: int i; michael@0: int ret=0; michael@0: opus_int32 nBytes; michael@0: ec_enc enc; michael@0: int bytes_target; michael@0: int prefill=0; michael@0: int start_band = 0; michael@0: int redundancy = 0; michael@0: int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */ michael@0: int celt_to_silk = 0; michael@0: VARDECL(opus_val16, pcm_buf); michael@0: int nb_compr_bytes; michael@0: int to_celt = 0; michael@0: opus_uint32 redundant_rng = 0; michael@0: int cutoff_Hz, hp_freq_smth1; michael@0: int voice_est; /* Probability of voice in Q7 */ michael@0: opus_int32 equiv_rate; michael@0: int delay_compensation; michael@0: int frame_rate; michael@0: opus_int32 max_rate; /* Max bitrate we're allowed to use */ michael@0: int curr_bandwidth; michael@0: opus_val16 HB_gain; michael@0: opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */ michael@0: int total_buffer; michael@0: opus_val16 stereo_width; michael@0: const CELTMode *celt_mode; michael@0: AnalysisInfo analysis_info; michael@0: int analysis_read_pos_bak=-1; michael@0: int analysis_read_subframe_bak=-1; michael@0: VARDECL(opus_val16, tmp_prefill); michael@0: michael@0: ALLOC_STACK; michael@0: michael@0: max_data_bytes = IMIN(1276, out_data_bytes); michael@0: michael@0: st->rangeFinal = 0; michael@0: if ((!st->variable_duration && 400*frame_size != st->Fs && 200*frame_size != st->Fs && 100*frame_size != st->Fs && michael@0: 50*frame_size != st->Fs && 25*frame_size != st->Fs && 50*frame_size != 3*st->Fs) michael@0: || (400*frame_size < st->Fs) michael@0: || max_data_bytes<=0 michael@0: ) michael@0: { michael@0: RESTORE_STACK; michael@0: return OPUS_BAD_ARG; michael@0: } michael@0: silk_enc = (char*)st+st->silk_enc_offset; michael@0: celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); michael@0: if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) michael@0: delay_compensation = 0; michael@0: else michael@0: delay_compensation = st->delay_compensation; michael@0: michael@0: lsb_depth = IMIN(lsb_depth, st->lsb_depth); michael@0: michael@0: analysis_info.valid = 0; michael@0: celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode)); michael@0: #ifndef DISABLE_FLOAT_API michael@0: #ifdef FIXED_POINT michael@0: if (st->silk_mode.complexity >= 10 && st->Fs==48000) michael@0: #else michael@0: if (st->silk_mode.complexity >= 7 && st->Fs==48000) michael@0: #endif michael@0: { michael@0: analysis_read_pos_bak = st->analysis.read_pos; michael@0: analysis_read_subframe_bak = st->analysis.read_subframe; michael@0: run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame_size, michael@0: c1, c2, analysis_channels, st->Fs, michael@0: lsb_depth, downmix, &analysis_info); michael@0: } michael@0: #endif michael@0: michael@0: st->voice_ratio = -1; michael@0: michael@0: #ifndef DISABLE_FLOAT_API michael@0: st->detected_bandwidth = 0; michael@0: if (analysis_info.valid) michael@0: { michael@0: int analysis_bandwidth; michael@0: if (st->signal_type == OPUS_AUTO) michael@0: st->voice_ratio = (int)floor(.5+100*(1-analysis_info.music_prob)); michael@0: michael@0: analysis_bandwidth = analysis_info.bandwidth; michael@0: if (analysis_bandwidth<=12) michael@0: st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND; michael@0: else if (analysis_bandwidth<=14) michael@0: st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; michael@0: else if (analysis_bandwidth<=16) michael@0: st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND; michael@0: else if (analysis_bandwidth<=18) michael@0: st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; michael@0: else michael@0: st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND; michael@0: } michael@0: #endif michael@0: michael@0: if (st->channels==2 && st->force_channels!=1) michael@0: stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_mem); michael@0: else michael@0: stereo_width = 0; michael@0: total_buffer = delay_compensation; michael@0: st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes); michael@0: michael@0: frame_rate = st->Fs/frame_size; michael@0: if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8 michael@0: || (frame_rate<50 && (max_data_bytes*frame_rate<300 || st->bitrate_bps < 2400))) michael@0: { michael@0: /*If the space is too low to do something useful, emit 'PLC' frames.*/ michael@0: int tocmode = st->mode; michael@0: int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth; michael@0: if (tocmode==0) michael@0: tocmode = MODE_SILK_ONLY; michael@0: if (frame_rate>100) michael@0: tocmode = MODE_CELT_ONLY; michael@0: if (frame_rate < 50) michael@0: tocmode = MODE_SILK_ONLY; michael@0: if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND) michael@0: bw=OPUS_BANDWIDTH_WIDEBAND; michael@0: else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND) michael@0: bw=OPUS_BANDWIDTH_NARROWBAND; michael@0: else if (bw<=OPUS_BANDWIDTH_SUPERWIDEBAND) michael@0: bw=OPUS_BANDWIDTH_SUPERWIDEBAND; michael@0: data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels); michael@0: RESTORE_STACK; michael@0: return 1; michael@0: } michael@0: if (!st->use_vbr) michael@0: { michael@0: int cbrBytes; michael@0: cbrBytes = IMIN( (st->bitrate_bps + 4*frame_rate)/(8*frame_rate) , max_data_bytes); michael@0: st->bitrate_bps = cbrBytes * (8*frame_rate); michael@0: max_data_bytes = cbrBytes; michael@0: } michael@0: max_rate = frame_rate*max_data_bytes*8; michael@0: michael@0: /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */ michael@0: equiv_rate = st->bitrate_bps - (40*st->channels+20)*(st->Fs/frame_size - 50); michael@0: michael@0: if (st->signal_type == OPUS_SIGNAL_VOICE) michael@0: voice_est = 127; michael@0: else if (st->signal_type == OPUS_SIGNAL_MUSIC) michael@0: voice_est = 0; michael@0: else if (st->voice_ratio >= 0) michael@0: { michael@0: voice_est = st->voice_ratio*327>>8; michael@0: /* For AUDIO, never be more than 90% confident of having speech */ michael@0: if (st->application == OPUS_APPLICATION_AUDIO) michael@0: voice_est = IMIN(voice_est, 115); michael@0: } else if (st->application == OPUS_APPLICATION_VOIP) michael@0: voice_est = 115; michael@0: else michael@0: voice_est = 48; michael@0: michael@0: if (st->force_channels!=OPUS_AUTO && st->channels == 2) michael@0: { michael@0: st->stream_channels = st->force_channels; michael@0: } else { michael@0: #ifdef FUZZING michael@0: /* Random mono/stereo decision */ michael@0: if (st->channels == 2 && (rand()&0x1F)==0) michael@0: st->stream_channels = 3-st->stream_channels; michael@0: #else michael@0: /* Rate-dependent mono-stereo decision */ michael@0: if (st->channels == 2) michael@0: { michael@0: opus_int32 stereo_threshold; michael@0: stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14); michael@0: if (st->stream_channels == 2) michael@0: stereo_threshold -= 1000; michael@0: else michael@0: stereo_threshold += 1000; michael@0: st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1; michael@0: } else { michael@0: st->stream_channels = st->channels; michael@0: } michael@0: #endif michael@0: } michael@0: equiv_rate = st->bitrate_bps - (40*st->stream_channels+20)*(st->Fs/frame_size - 50); michael@0: michael@0: /* Mode selection depending on application and signal type */ michael@0: if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) michael@0: { michael@0: st->mode = MODE_CELT_ONLY; michael@0: } else if (st->user_forced_mode == OPUS_AUTO) michael@0: { michael@0: #ifdef FUZZING michael@0: /* Random mode switching */ michael@0: if ((rand()&0xF)==0) michael@0: { michael@0: if ((rand()&0x1)==0) michael@0: st->mode = MODE_CELT_ONLY; michael@0: else michael@0: st->mode = MODE_SILK_ONLY; michael@0: } else { michael@0: if (st->prev_mode==MODE_CELT_ONLY) michael@0: st->mode = MODE_CELT_ONLY; michael@0: else michael@0: st->mode = MODE_SILK_ONLY; michael@0: } michael@0: #else michael@0: opus_int32 mode_voice, mode_music; michael@0: opus_int32 threshold; michael@0: michael@0: /* Interpolate based on stereo width */ michael@0: mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[0][0]) michael@0: + MULT16_32_Q15(stereo_width,mode_thresholds[1][0])); michael@0: mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[1][1]) michael@0: + MULT16_32_Q15(stereo_width,mode_thresholds[1][1])); michael@0: /* Interpolate based on speech/music probability */ michael@0: threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14); michael@0: /* Bias towards SILK for VoIP because of some useful features */ michael@0: if (st->application == OPUS_APPLICATION_VOIP) michael@0: threshold += 8000; michael@0: michael@0: /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/ michael@0: /* Hysteresis */ michael@0: if (st->prev_mode == MODE_CELT_ONLY) michael@0: threshold -= 4000; michael@0: else if (st->prev_mode>0) michael@0: threshold += 4000; michael@0: michael@0: st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY; michael@0: michael@0: /* When FEC is enabled and there's enough packet loss, use SILK */ michael@0: if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (128-voice_est)>>4) michael@0: st->mode = MODE_SILK_ONLY; michael@0: /* When encoding voice and DTX is enabled, set the encoder to SILK mode (at least for now) */ michael@0: if (st->silk_mode.useDTX && voice_est > 100) michael@0: st->mode = MODE_SILK_ONLY; michael@0: #endif michael@0: } else { michael@0: st->mode = st->user_forced_mode; michael@0: } michael@0: michael@0: /* Override the chosen mode to make sure we meet the requested frame size */ michael@0: if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100) michael@0: st->mode = MODE_CELT_ONLY; michael@0: if (st->lfe) michael@0: st->mode = MODE_CELT_ONLY; michael@0: /* If max_data_bytes represents less than 8 kb/s, switch to CELT-only mode */ michael@0: if (max_data_bytes < (frame_rate > 50 ? 12000 : 8000)*frame_size / (st->Fs * 8)) michael@0: st->mode = MODE_CELT_ONLY; michael@0: michael@0: if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0 michael@0: && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY) michael@0: { michael@0: /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */ michael@0: st->silk_mode.toMono = 1; michael@0: st->stream_channels = 2; michael@0: } else { michael@0: st->silk_mode.toMono = 0; michael@0: } michael@0: michael@0: if (st->prev_mode > 0 && michael@0: ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) || michael@0: (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY))) michael@0: { michael@0: redundancy = 1; michael@0: celt_to_silk = (st->mode != MODE_CELT_ONLY); michael@0: if (!celt_to_silk) michael@0: { michael@0: /* Switch to SILK/hybrid if frame size is 10 ms or more*/ michael@0: if (frame_size >= st->Fs/100) michael@0: { michael@0: st->mode = st->prev_mode; michael@0: to_celt = 1; michael@0: } else { michael@0: redundancy=0; michael@0: } michael@0: } michael@0: } michael@0: /* For the first frame at a new SILK bandwidth */ michael@0: if (st->silk_bw_switch) michael@0: { michael@0: redundancy = 1; michael@0: celt_to_silk = 1; michael@0: st->silk_bw_switch = 0; michael@0: prefill=1; michael@0: } michael@0: michael@0: if (redundancy) michael@0: { michael@0: /* Fair share of the max size allowed */ michael@0: redundancy_bytes = IMIN(257, max_data_bytes*(opus_int32)(st->Fs/200)/(frame_size+st->Fs/200)); michael@0: /* For VBR, target the actual bitrate (subject to the limit above) */ michael@0: if (st->use_vbr) michael@0: redundancy_bytes = IMIN(redundancy_bytes, st->bitrate_bps/1600); michael@0: } michael@0: michael@0: if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) michael@0: { michael@0: silk_EncControlStruct dummy; michael@0: silk_InitEncoder( silk_enc, st->arch, &dummy); michael@0: prefill=1; michael@0: } michael@0: michael@0: /* Automatic (rate-dependent) bandwidth selection */ michael@0: if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch) michael@0: { michael@0: const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds; michael@0: opus_int32 bandwidth_thresholds[8]; michael@0: int bandwidth = OPUS_BANDWIDTH_FULLBAND; michael@0: opus_int32 equiv_rate2; michael@0: michael@0: equiv_rate2 = equiv_rate; michael@0: if (st->mode != MODE_CELT_ONLY) michael@0: { michael@0: /* Adjust the threshold +/- 10% depending on complexity */ michael@0: equiv_rate2 = equiv_rate2 * (45+st->silk_mode.complexity)/50; michael@0: /* CBR is less efficient by ~1 kb/s */ michael@0: if (!st->use_vbr) michael@0: equiv_rate2 -= 1000; michael@0: } michael@0: if (st->channels==2 && st->force_channels!=1) michael@0: { michael@0: voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds; michael@0: music_bandwidth_thresholds = stereo_music_bandwidth_thresholds; michael@0: } else { michael@0: voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds; michael@0: music_bandwidth_thresholds = mono_music_bandwidth_thresholds; michael@0: } michael@0: /* Interpolate bandwidth thresholds depending on voice estimation */ michael@0: for (i=0;i<8;i++) michael@0: { michael@0: bandwidth_thresholds[i] = music_bandwidth_thresholds[i] michael@0: + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14); michael@0: } michael@0: do { michael@0: int threshold, hysteresis; michael@0: threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)]; michael@0: hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1]; michael@0: if (!st->first) michael@0: { michael@0: if (st->bandwidth >= bandwidth) michael@0: threshold -= hysteresis; michael@0: else michael@0: threshold += hysteresis; michael@0: } michael@0: if (equiv_rate2 >= threshold) michael@0: break; michael@0: } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND); michael@0: st->bandwidth = bandwidth; michael@0: /* Prevents any transition to SWB/FB until the SILK layer has fully michael@0: switched to WB mode and turned the variable LP filter off */ michael@0: if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND) michael@0: st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; michael@0: } michael@0: michael@0: if (st->bandwidth>st->max_bandwidth) michael@0: st->bandwidth = st->max_bandwidth; michael@0: michael@0: if (st->user_bandwidth != OPUS_AUTO) michael@0: st->bandwidth = st->user_bandwidth; michael@0: michael@0: /* This prevents us from using hybrid at unsafe CBR/max rates */ michael@0: if (st->mode != MODE_CELT_ONLY && max_rate < 15000) michael@0: { michael@0: st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND); michael@0: } michael@0: michael@0: /* Prevents Opus from wasting bits on frequencies that are above michael@0: the Nyquist rate of the input signal */ michael@0: if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND) michael@0: st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; michael@0: if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND) michael@0: st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; michael@0: if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND) michael@0: st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; michael@0: if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND) michael@0: st->bandwidth = OPUS_BANDWIDTH_NARROWBAND; michael@0: #ifndef DISABLE_FLOAT_API michael@0: /* Use detected bandwidth to reduce the encoded bandwidth. */ michael@0: if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO) michael@0: { michael@0: int min_detected_bandwidth; michael@0: /* Makes bandwidth detection more conservative just in case the detector michael@0: gets it wrong when we could have coded a high bandwidth transparently. michael@0: When operating in SILK/hybrid mode, we don't go below wideband to avoid michael@0: more complicated switches that require redundancy. */ michael@0: if (equiv_rate <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY) michael@0: min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND; michael@0: else if (equiv_rate <= 24000*st->stream_channels && st->mode == MODE_CELT_ONLY) michael@0: min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; michael@0: else if (equiv_rate <= 30000*st->stream_channels) michael@0: min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND; michael@0: else if (equiv_rate <= 44000*st->stream_channels) michael@0: min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; michael@0: else michael@0: min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND; michael@0: michael@0: st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwidth); michael@0: st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth); michael@0: } michael@0: #endif michael@0: celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth)); michael@0: michael@0: /* CELT mode doesn't support mediumband, use wideband instead */ michael@0: if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) michael@0: st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; michael@0: if (st->lfe) michael@0: st->bandwidth = OPUS_BANDWIDTH_NARROWBAND; michael@0: michael@0: /* Can't support higher than wideband for >20 ms frames */ michael@0: if (frame_size > st->Fs/50 && (st->mode == MODE_CELT_ONLY || st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)) michael@0: { michael@0: VARDECL(unsigned char, tmp_data); michael@0: int nb_frames; michael@0: int bak_mode, bak_bandwidth, bak_channels, bak_to_mono; michael@0: VARDECL(OpusRepacketizer, rp); michael@0: opus_int32 bytes_per_frame; michael@0: opus_int32 repacketize_len; michael@0: michael@0: #ifndef DISABLE_FLOAT_API michael@0: if (analysis_read_pos_bak!= -1) michael@0: { michael@0: st->analysis.read_pos = analysis_read_pos_bak; michael@0: st->analysis.read_subframe = analysis_read_subframe_bak; michael@0: } michael@0: #endif michael@0: michael@0: nb_frames = frame_size > st->Fs/25 ? 3 : 2; michael@0: bytes_per_frame = IMIN(1276,(out_data_bytes-3)/nb_frames); michael@0: michael@0: ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char); michael@0: michael@0: ALLOC(rp, 1, OpusRepacketizer); michael@0: opus_repacketizer_init(rp); michael@0: michael@0: bak_mode = st->user_forced_mode; michael@0: bak_bandwidth = st->user_bandwidth; michael@0: bak_channels = st->force_channels; michael@0: michael@0: st->user_forced_mode = st->mode; michael@0: st->user_bandwidth = st->bandwidth; michael@0: st->force_channels = st->stream_channels; michael@0: bak_to_mono = st->silk_mode.toMono; michael@0: michael@0: if (bak_to_mono) michael@0: st->force_channels = 1; michael@0: else michael@0: st->prev_channels = st->stream_channels; michael@0: for (i=0;isilk_mode.toMono = 0; michael@0: /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */ michael@0: if (to_celt && i==nb_frames-1) michael@0: st->user_forced_mode = MODE_CELT_ONLY; michael@0: tmp_len = opus_encode_native(st, pcm+i*(st->channels*st->Fs/50), st->Fs/50, michael@0: tmp_data+i*bytes_per_frame, bytes_per_frame, lsb_depth, michael@0: NULL, 0, c1, c2, analysis_channels, downmix); michael@0: if (tmp_len<0) michael@0: { michael@0: RESTORE_STACK; michael@0: return OPUS_INTERNAL_ERROR; michael@0: } michael@0: ret = opus_repacketizer_cat(rp, tmp_data+i*bytes_per_frame, tmp_len); michael@0: if (ret<0) michael@0: { michael@0: RESTORE_STACK; michael@0: return OPUS_INTERNAL_ERROR; michael@0: } michael@0: } michael@0: if (st->use_vbr) michael@0: repacketize_len = out_data_bytes; michael@0: else michael@0: repacketize_len = IMIN(3*st->bitrate_bps/(3*8*50/nb_frames), out_data_bytes); michael@0: ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketize_len, 0, !st->use_vbr); michael@0: if (ret<0) michael@0: { michael@0: RESTORE_STACK; michael@0: return OPUS_INTERNAL_ERROR; michael@0: } michael@0: st->user_forced_mode = bak_mode; michael@0: st->user_bandwidth = bak_bandwidth; michael@0: st->force_channels = bak_channels; michael@0: st->silk_mode.toMono = bak_to_mono; michael@0: RESTORE_STACK; michael@0: return ret; michael@0: } michael@0: curr_bandwidth = st->bandwidth; michael@0: michael@0: /* Chooses the appropriate mode for speech michael@0: *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */ michael@0: if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND) michael@0: st->mode = MODE_HYBRID; michael@0: if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND) michael@0: st->mode = MODE_SILK_ONLY; michael@0: michael@0: /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */ michael@0: bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1; michael@0: michael@0: data += 1; michael@0: michael@0: ec_enc_init(&enc, data, max_data_bytes-1); michael@0: michael@0: ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_val16); michael@0: for (i=0;ichannels;i++) michael@0: pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-total_buffer)*st->channels+i]; michael@0: michael@0: if (st->mode == MODE_CELT_ONLY) michael@0: hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); michael@0: else michael@0: hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15; michael@0: michael@0: st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15, michael@0: hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) ); michael@0: michael@0: /* convert from log scale to Hertz */ michael@0: cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) ); michael@0: michael@0: if (st->application == OPUS_APPLICATION_VOIP) michael@0: { michael@0: hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs); michael@0: } else { michael@0: dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs); michael@0: } michael@0: michael@0: michael@0: michael@0: /* SILK processing */ michael@0: HB_gain = Q15ONE; michael@0: if (st->mode != MODE_CELT_ONLY) michael@0: { michael@0: opus_int32 total_bitRate, celt_rate; michael@0: #ifdef FIXED_POINT michael@0: const opus_int16 *pcm_silk; michael@0: #else michael@0: VARDECL(opus_int16, pcm_silk); michael@0: ALLOC(pcm_silk, st->channels*frame_size, opus_int16); michael@0: #endif michael@0: michael@0: /* Distribute bits between SILK and CELT */ michael@0: total_bitRate = 8 * bytes_target * frame_rate; michael@0: if( st->mode == MODE_HYBRID ) { michael@0: int HB_gain_ref; michael@0: /* Base rate for SILK */ michael@0: st->silk_mode.bitRate = st->stream_channels * ( 5000 + 1000 * ( st->Fs == 100 * frame_size ) ); michael@0: if( curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND ) { michael@0: /* SILK gets 2/3 of the remaining bits */ michael@0: st->silk_mode.bitRate += ( total_bitRate - st->silk_mode.bitRate ) * 2 / 3; michael@0: } else { /* FULLBAND */ michael@0: /* SILK gets 3/5 of the remaining bits */ michael@0: st->silk_mode.bitRate += ( total_bitRate - st->silk_mode.bitRate ) * 3 / 5; michael@0: } michael@0: /* Don't let SILK use more than 80% */ michael@0: if( st->silk_mode.bitRate > total_bitRate * 4/5 ) { michael@0: st->silk_mode.bitRate = total_bitRate * 4/5; michael@0: } michael@0: if (!st->energy_masking) michael@0: { michael@0: /* Increasingly attenuate high band when it gets allocated fewer bits */ michael@0: celt_rate = total_bitRate - st->silk_mode.bitRate; michael@0: HB_gain_ref = (curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND) ? 3000 : 3600; michael@0: HB_gain = SHL32((opus_val32)celt_rate, 9) / SHR32((opus_val32)celt_rate + st->stream_channels * HB_gain_ref, 6); michael@0: HB_gain = HB_gain < Q15ONE*6/7 ? HB_gain + Q15ONE/7 : Q15ONE; michael@0: } michael@0: } else { michael@0: /* SILK gets all bits */ michael@0: st->silk_mode.bitRate = total_bitRate; michael@0: } michael@0: michael@0: /* Surround masking for SILK */ michael@0: if (st->energy_masking && st->use_vbr && !st->lfe) michael@0: { michael@0: opus_val32 mask_sum=0; michael@0: opus_val16 masking_depth; michael@0: opus_int32 rate_offset; michael@0: int c; michael@0: int end = 17; michael@0: opus_int16 srate = 16000; michael@0: if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND) michael@0: { michael@0: end = 13; michael@0: srate = 8000; michael@0: } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) michael@0: { michael@0: end = 15; michael@0: srate = 12000; michael@0: } michael@0: for (c=0;cchannels;c++) michael@0: { michael@0: for(i=0;ienergy_masking[21*c+i], michael@0: QCONST16(.5f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT)); michael@0: if (mask > 0) michael@0: mask = HALF16(mask); michael@0: mask_sum += mask; michael@0: } michael@0: } michael@0: /* Conservative rate reduction, we cut the masking in half */ michael@0: masking_depth = mask_sum / end*st->channels; michael@0: masking_depth += QCONST16(.2f, DB_SHIFT); michael@0: rate_offset = (opus_int32)PSHR32(MULT16_16(srate, masking_depth), DB_SHIFT); michael@0: rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3); michael@0: /* Split the rate change between the SILK and CELT part for hybrid. */ michael@0: if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPUS_BANDWIDTH_FULLBAND) michael@0: st->silk_mode.bitRate += 3*rate_offset/5; michael@0: else michael@0: st->silk_mode.bitRate += rate_offset; michael@0: bytes_target += rate_offset * frame_size / (8 * st->Fs); michael@0: } michael@0: michael@0: st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs; michael@0: st->silk_mode.nChannelsAPI = st->channels; michael@0: st->silk_mode.nChannelsInternal = st->stream_channels; michael@0: if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { michael@0: st->silk_mode.desiredInternalSampleRate = 8000; michael@0: } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { michael@0: st->silk_mode.desiredInternalSampleRate = 12000; michael@0: } else { michael@0: silk_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND ); michael@0: st->silk_mode.desiredInternalSampleRate = 16000; michael@0: } michael@0: if( st->mode == MODE_HYBRID ) { michael@0: /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */ michael@0: st->silk_mode.minInternalSampleRate = 16000; michael@0: } else { michael@0: st->silk_mode.minInternalSampleRate = 8000; michael@0: } michael@0: michael@0: if (st->mode == MODE_SILK_ONLY) michael@0: { michael@0: opus_int32 effective_max_rate = max_rate; michael@0: st->silk_mode.maxInternalSampleRate = 16000; michael@0: if (frame_rate > 50) michael@0: effective_max_rate = effective_max_rate*2/3; michael@0: if (effective_max_rate < 13000) michael@0: { michael@0: st->silk_mode.maxInternalSampleRate = 12000; michael@0: st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate); michael@0: } michael@0: if (effective_max_rate < 9600) michael@0: { michael@0: st->silk_mode.maxInternalSampleRate = 8000; michael@0: st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate); michael@0: } michael@0: } else { michael@0: st->silk_mode.maxInternalSampleRate = 16000; michael@0: } michael@0: michael@0: st->silk_mode.useCBR = !st->use_vbr; michael@0: michael@0: /* Call SILK encoder for the low band */ michael@0: nBytes = IMIN(1275, max_data_bytes-1-redundancy_bytes); michael@0: michael@0: st->silk_mode.maxBits = nBytes*8; michael@0: /* Only allow up to 90% of the bits for hybrid mode*/ michael@0: if (st->mode == MODE_HYBRID) michael@0: st->silk_mode.maxBits = (opus_int32)st->silk_mode.maxBits*9/10; michael@0: if (st->silk_mode.useCBR) michael@0: { michael@0: st->silk_mode.maxBits = (st->silk_mode.bitRate * frame_size / (st->Fs * 8))*8; michael@0: /* Reduce the initial target to make it easier to reach the CBR rate */ michael@0: st->silk_mode.bitRate = IMAX(1, st->silk_mode.bitRate-2000); michael@0: } michael@0: michael@0: if (prefill) michael@0: { michael@0: opus_int32 zero=0; michael@0: int prefill_offset; michael@0: /* Use a smooth onset for the SILK prefill to avoid the encoder trying to encode michael@0: a discontinuity. The exact location is what we need to avoid leaving any "gap" michael@0: in the audio when mixing with the redundant CELT frame. Here we can afford to michael@0: overwrite st->delay_buffer because the only thing that uses it before it gets michael@0: rewritten is tmp_prefill[] and even then only the part after the ramp really michael@0: gets used (rather than sent to the encoder and discarded) */ michael@0: prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensation-st->Fs/400); michael@0: gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_offset, michael@0: 0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_mode->window, st->Fs); michael@0: for(i=0;idelay_buffer[i]=0; michael@0: #ifdef FIXED_POINT michael@0: pcm_silk = st->delay_buffer; michael@0: #else michael@0: for (i=0;iencoder_buffer*st->channels;i++) michael@0: pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]); michael@0: #endif michael@0: silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, 1 ); michael@0: } michael@0: michael@0: #ifdef FIXED_POINT michael@0: pcm_silk = pcm_buf+total_buffer*st->channels; michael@0: #else michael@0: for (i=0;ichannels;i++) michael@0: pcm_silk[i] = FLOAT2INT16(pcm_buf[total_buffer*st->channels + i]); michael@0: #endif michael@0: ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0 ); michael@0: if( ret ) { michael@0: /*fprintf (stderr, "SILK encode error: %d\n", ret);*/ michael@0: /* Handle error */ michael@0: RESTORE_STACK; michael@0: return OPUS_INTERNAL_ERROR; michael@0: } michael@0: if (nBytes==0) michael@0: { michael@0: st->rangeFinal = 0; michael@0: data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels); michael@0: RESTORE_STACK; michael@0: return 1; michael@0: } michael@0: /* Extract SILK internal bandwidth for signaling in first byte */ michael@0: if( st->mode == MODE_SILK_ONLY ) { michael@0: if( st->silk_mode.internalSampleRate == 8000 ) { michael@0: curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND; michael@0: } else if( st->silk_mode.internalSampleRate == 12000 ) { michael@0: curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; michael@0: } else if( st->silk_mode.internalSampleRate == 16000 ) { michael@0: curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND; michael@0: } michael@0: } else { michael@0: silk_assert( st->silk_mode.internalSampleRate == 16000 ); michael@0: } michael@0: michael@0: st->silk_mode.opusCanSwitch = st->silk_mode.switchReady; michael@0: /* FIXME: How do we allocate the redundancy for CBR? */ michael@0: if (st->silk_mode.opusCanSwitch) michael@0: { michael@0: redundancy = 1; michael@0: celt_to_silk = 0; michael@0: st->silk_bw_switch = 1; michael@0: } michael@0: } michael@0: michael@0: /* CELT processing */ michael@0: { michael@0: int endband=21; michael@0: michael@0: switch(curr_bandwidth) michael@0: { michael@0: case OPUS_BANDWIDTH_NARROWBAND: michael@0: endband = 13; michael@0: break; michael@0: case OPUS_BANDWIDTH_MEDIUMBAND: michael@0: case OPUS_BANDWIDTH_WIDEBAND: michael@0: endband = 17; michael@0: break; michael@0: case OPUS_BANDWIDTH_SUPERWIDEBAND: michael@0: endband = 19; michael@0: break; michael@0: case OPUS_BANDWIDTH_FULLBAND: michael@0: endband = 21; michael@0: break; michael@0: } michael@0: celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband)); michael@0: celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels)); michael@0: } michael@0: celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX)); michael@0: if (st->mode != MODE_SILK_ONLY) michael@0: { michael@0: opus_val32 celt_pred=2; michael@0: celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0)); michael@0: /* We may still decide to disable prediction later */ michael@0: if (st->silk_mode.reducedDependency) michael@0: celt_pred = 0; michael@0: celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred)); michael@0: michael@0: if (st->mode == MODE_HYBRID) michael@0: { michael@0: int len; michael@0: michael@0: len = (ec_tell(&enc)+7)>>3; michael@0: if (redundancy) michael@0: len += st->mode == MODE_HYBRID ? 3 : 1; michael@0: if( st->use_vbr ) { michael@0: nb_compr_bytes = len + bytes_target - (st->silk_mode.bitRate * frame_size) / (8 * st->Fs); michael@0: } else { michael@0: /* check if SILK used up too much */ michael@0: nb_compr_bytes = len > bytes_target ? len : bytes_target; michael@0: } michael@0: } else { michael@0: if (st->use_vbr) michael@0: { michael@0: opus_int32 bonus=0; michael@0: #ifndef DISABLE_FLOAT_API michael@0: if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != st->Fs/50) michael@0: { michael@0: bonus = (60*st->stream_channels+40)*(st->Fs/frame_size-50); michael@0: if (analysis_info.valid) michael@0: bonus = (opus_int32)(bonus*(1.f+.5f*analysis_info.tonality)); michael@0: } michael@0: #endif michael@0: celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1)); michael@0: celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint)); michael@0: celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps+bonus)); michael@0: nb_compr_bytes = max_data_bytes-1-redundancy_bytes; michael@0: } else { michael@0: nb_compr_bytes = bytes_target; michael@0: } michael@0: } michael@0: michael@0: } else { michael@0: nb_compr_bytes = 0; michael@0: } michael@0: michael@0: ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16); michael@0: if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0) michael@0: { michael@0: for (i=0;ichannels*st->Fs/400;i++) michael@0: tmp_prefill[i] = st->delay_buffer[(st->encoder_buffer-total_buffer-st->Fs/400)*st->channels + i]; michael@0: } michael@0: michael@0: for (i=0;ichannels*(st->encoder_buffer-(frame_size+total_buffer));i++) michael@0: st->delay_buffer[i] = st->delay_buffer[i+st->channels*frame_size]; michael@0: for (;iencoder_buffer*st->channels;i++) michael@0: st->delay_buffer[i] = pcm_buf[(frame_size+total_buffer-st->encoder_buffer)*st->channels+i]; michael@0: michael@0: /* gain_fade() and stereo_fade() need to be after the buffer copying michael@0: because we don't want any of this to affect the SILK part */ michael@0: if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) { michael@0: gain_fade(pcm_buf, pcm_buf, michael@0: st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs); michael@0: } michael@0: st->prev_HB_gain = HB_gain; michael@0: if (st->mode != MODE_HYBRID || st->stream_channels==1) michael@0: st->silk_mode.stereoWidth_Q14 = IMIN((1<<14),2*IMAX(0,equiv_rate-30000)); michael@0: if( !st->energy_masking && st->channels == 2 ) { michael@0: /* Apply stereo width reduction (at low bitrates) */ michael@0: if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) { michael@0: opus_val16 g1, g2; michael@0: g1 = st->hybrid_stereo_width_Q14; michael@0: g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14); michael@0: #ifdef FIXED_POINT michael@0: g1 = g1==16384 ? Q15ONE : SHL16(g1,1); michael@0: g2 = g2==16384 ? Q15ONE : SHL16(g2,1); michael@0: #else michael@0: g1 *= (1.f/16384); michael@0: g2 *= (1.f/16384); michael@0: #endif michael@0: stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap, michael@0: frame_size, st->channels, celt_mode->window, st->Fs); michael@0: st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14; michael@0: } michael@0: } michael@0: michael@0: if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) <= 8*(max_data_bytes-1)) michael@0: { michael@0: /* For SILK mode, the redundancy is inferred from the length */ michael@0: if (st->mode == MODE_HYBRID && (redundancy || ec_tell(&enc)+37 <= 8*nb_compr_bytes)) michael@0: ec_enc_bit_logp(&enc, redundancy, 12); michael@0: if (redundancy) michael@0: { michael@0: int max_redundancy; michael@0: ec_enc_bit_logp(&enc, celt_to_silk, 1); michael@0: if (st->mode == MODE_HYBRID) michael@0: max_redundancy = (max_data_bytes-1)-nb_compr_bytes; michael@0: else michael@0: max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3); michael@0: /* Target the same bit-rate for redundancy as for the rest, michael@0: up to a max of 257 bytes */ michael@0: redundancy_bytes = IMIN(max_redundancy, st->bitrate_bps/1600); michael@0: redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes)); michael@0: if (st->mode == MODE_HYBRID) michael@0: ec_enc_uint(&enc, redundancy_bytes-2, 256); michael@0: } michael@0: } else { michael@0: redundancy = 0; michael@0: } michael@0: michael@0: if (!redundancy) michael@0: { michael@0: st->silk_bw_switch = 0; michael@0: redundancy_bytes = 0; michael@0: } michael@0: if (st->mode != MODE_CELT_ONLY)start_band=17; michael@0: michael@0: if (st->mode == MODE_SILK_ONLY) michael@0: { michael@0: ret = (ec_tell(&enc)+7)>>3; michael@0: ec_enc_done(&enc); michael@0: nb_compr_bytes = ret; michael@0: } else { michael@0: nb_compr_bytes = IMIN((max_data_bytes-1)-redundancy_bytes, nb_compr_bytes); michael@0: ec_enc_shrink(&enc, nb_compr_bytes); michael@0: } michael@0: michael@0: #ifndef DISABLE_FLOAT_API michael@0: if (redundancy || st->mode != MODE_SILK_ONLY) michael@0: celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(&analysis_info)); michael@0: #endif michael@0: michael@0: /* 5 ms redundant frame for CELT->SILK */ michael@0: if (redundancy && celt_to_silk) michael@0: { michael@0: int err; michael@0: celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)); michael@0: celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0)); michael@0: err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL); michael@0: if (err < 0) michael@0: { michael@0: RESTORE_STACK; michael@0: return OPUS_INTERNAL_ERROR; michael@0: } michael@0: celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng)); michael@0: celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); michael@0: } michael@0: michael@0: celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band)); michael@0: michael@0: if (st->mode != MODE_SILK_ONLY) michael@0: { michael@0: if (st->mode != st->prev_mode && st->prev_mode > 0) michael@0: { michael@0: unsigned char dummy[2]; michael@0: celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); michael@0: michael@0: /* Prefilling */ michael@0: celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL); michael@0: celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0)); michael@0: } michael@0: /* If false, we already busted the budget and we'll end up with a "PLC packet" */ michael@0: if (ec_tell(&enc) <= 8*nb_compr_bytes) michael@0: { michael@0: ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc); michael@0: if (ret < 0) michael@0: { michael@0: RESTORE_STACK; michael@0: return OPUS_INTERNAL_ERROR; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* 5 ms redundant frame for SILK->CELT */ michael@0: if (redundancy && !celt_to_silk) michael@0: { michael@0: int err; michael@0: unsigned char dummy[2]; michael@0: int N2, N4; michael@0: N2 = st->Fs/200; michael@0: N4 = st->Fs/400; michael@0: michael@0: celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); michael@0: celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)); michael@0: celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0)); michael@0: michael@0: /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */ michael@0: celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL); michael@0: michael@0: err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL); michael@0: if (err < 0) michael@0: { michael@0: RESTORE_STACK; michael@0: return OPUS_INTERNAL_ERROR; michael@0: } michael@0: celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng)); michael@0: } michael@0: michael@0: michael@0: michael@0: /* Signalling the mode in the first byte */ michael@0: data--; michael@0: data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels); michael@0: michael@0: st->rangeFinal = enc.rng ^ redundant_rng; michael@0: michael@0: if (to_celt) michael@0: st->prev_mode = MODE_CELT_ONLY; michael@0: else michael@0: st->prev_mode = st->mode; michael@0: st->prev_channels = st->stream_channels; michael@0: st->prev_framesize = frame_size; michael@0: michael@0: st->first = 0; michael@0: michael@0: /* In the unlikely case that the SILK encoder busted its target, tell michael@0: the decoder to call the PLC */ michael@0: if (ec_tell(&enc) > (max_data_bytes-1)*8) michael@0: { michael@0: if (max_data_bytes < 2) michael@0: { michael@0: RESTORE_STACK; michael@0: return OPUS_BUFFER_TOO_SMALL; michael@0: } michael@0: data[1] = 0; michael@0: ret = 1; michael@0: st->rangeFinal = 0; michael@0: } else if (st->mode==MODE_SILK_ONLY&&!redundancy) michael@0: { michael@0: /*When in LPC only mode it's perfectly michael@0: reasonable to strip off trailing zero bytes as michael@0: the required range decoder behavior is to michael@0: fill these in. This can't be done when the MDCT michael@0: modes are used because the decoder needs to know michael@0: the actual length for allocation purposes.*/ michael@0: while(ret>2&&data[ret]==0)ret--; michael@0: } michael@0: /* Count ToC and redundancy */ michael@0: ret += 1+redundancy_bytes; michael@0: if (!st->use_vbr) michael@0: { michael@0: if (opus_packet_pad(data, ret, max_data_bytes) != OPUS_OK) michael@0: michael@0: { michael@0: RESTORE_STACK; michael@0: return OPUS_INTERNAL_ERROR; michael@0: } michael@0: ret = max_data_bytes; michael@0: } michael@0: RESTORE_STACK; michael@0: return ret; michael@0: } michael@0: michael@0: #ifdef FIXED_POINT michael@0: michael@0: #ifndef DISABLE_FLOAT_API michael@0: opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size, michael@0: unsigned char *data, opus_int32 max_data_bytes) michael@0: { michael@0: int i, ret; michael@0: int frame_size; michael@0: int delay_compensation; michael@0: VARDECL(opus_int16, in); michael@0: ALLOC_STACK; michael@0: michael@0: if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) michael@0: delay_compensation = 0; michael@0: else michael@0: delay_compensation = st->delay_compensation; michael@0: frame_size = compute_frame_size(pcm, analysis_frame_size, michael@0: st->variable_duration, st->channels, st->Fs, st->bitrate_bps, michael@0: delay_compensation, downmix_float, st->analysis.subframe_mem); michael@0: michael@0: ALLOC(in, frame_size*st->channels, opus_int16); michael@0: michael@0: for (i=0;ichannels;i++) michael@0: in[i] = FLOAT2INT16(pcm[i]); michael@0: ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, pcm, analysis_frame_size, 0, -2, st->channels, downmix_float); michael@0: RESTORE_STACK; michael@0: return ret; michael@0: } michael@0: #endif michael@0: michael@0: opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size, michael@0: unsigned char *data, opus_int32 out_data_bytes) michael@0: { michael@0: int frame_size; michael@0: int delay_compensation; michael@0: if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) michael@0: delay_compensation = 0; michael@0: else michael@0: delay_compensation = st->delay_compensation; michael@0: frame_size = compute_frame_size(pcm, analysis_frame_size, michael@0: st->variable_duration, st->channels, st->Fs, st->bitrate_bps, michael@0: delay_compensation, downmix_int michael@0: #ifndef DISABLE_FLOAT_API michael@0: , st->analysis.subframe_mem michael@0: #endif michael@0: ); michael@0: return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 16, pcm, analysis_frame_size, 0, -2, st->channels, downmix_int); michael@0: } michael@0: michael@0: #else michael@0: opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size, michael@0: unsigned char *data, opus_int32 max_data_bytes) michael@0: { michael@0: int i, ret; michael@0: int frame_size; michael@0: int delay_compensation; michael@0: VARDECL(float, in); michael@0: ALLOC_STACK; michael@0: michael@0: if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) michael@0: delay_compensation = 0; michael@0: else michael@0: delay_compensation = st->delay_compensation; michael@0: frame_size = compute_frame_size(pcm, analysis_frame_size, michael@0: st->variable_duration, st->channels, st->Fs, st->bitrate_bps, michael@0: delay_compensation, downmix_int, st->analysis.subframe_mem); michael@0: michael@0: ALLOC(in, frame_size*st->channels, float); michael@0: michael@0: for (i=0;ichannels;i++) michael@0: in[i] = (1.0f/32768)*pcm[i]; michael@0: ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, pcm, analysis_frame_size, 0, -2, st->channels, downmix_int); michael@0: RESTORE_STACK; michael@0: return ret; michael@0: } michael@0: opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size, michael@0: unsigned char *data, opus_int32 out_data_bytes) michael@0: { michael@0: int frame_size; michael@0: int delay_compensation; michael@0: if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) michael@0: delay_compensation = 0; michael@0: else michael@0: delay_compensation = st->delay_compensation; michael@0: frame_size = compute_frame_size(pcm, analysis_frame_size, michael@0: st->variable_duration, st->channels, st->Fs, st->bitrate_bps, michael@0: delay_compensation, downmix_float, st->analysis.subframe_mem); michael@0: return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 24, michael@0: pcm, analysis_frame_size, 0, -2, st->channels, downmix_float); michael@0: } michael@0: #endif michael@0: michael@0: michael@0: int opus_encoder_ctl(OpusEncoder *st, int request, ...) michael@0: { michael@0: int ret; michael@0: CELTEncoder *celt_enc; michael@0: va_list ap; michael@0: michael@0: ret = OPUS_OK; michael@0: va_start(ap, request); michael@0: michael@0: celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); michael@0: michael@0: switch (request) michael@0: { michael@0: case OPUS_SET_APPLICATION_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if ( (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_AUDIO michael@0: && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY) michael@0: || (!st->first && st->application != value)) michael@0: { michael@0: ret = OPUS_BAD_ARG; michael@0: break; michael@0: } michael@0: st->application = value; michael@0: } michael@0: break; michael@0: case OPUS_GET_APPLICATION_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->application; michael@0: } michael@0: break; michael@0: case OPUS_SET_BITRATE_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX) michael@0: { michael@0: if (value <= 0) michael@0: goto bad_arg; michael@0: else if (value <= 500) michael@0: value = 500; michael@0: else if (value > (opus_int32)300000*st->channels) michael@0: value = (opus_int32)300000*st->channels; michael@0: } michael@0: st->user_bitrate_bps = value; michael@0: } michael@0: break; michael@0: case OPUS_GET_BITRATE_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276); michael@0: } michael@0: break; michael@0: case OPUS_SET_FORCE_CHANNELS_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if((value<1 || value>st->channels) && value != OPUS_AUTO) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: st->force_channels = value; michael@0: } michael@0: break; michael@0: case OPUS_GET_FORCE_CHANNELS_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->force_channels; michael@0: } michael@0: break; michael@0: case OPUS_SET_MAX_BANDWIDTH_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: st->max_bandwidth = value; michael@0: if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { michael@0: st->silk_mode.maxInternalSampleRate = 8000; michael@0: } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { michael@0: st->silk_mode.maxInternalSampleRate = 12000; michael@0: } else { michael@0: st->silk_mode.maxInternalSampleRate = 16000; michael@0: } michael@0: } michael@0: break; michael@0: case OPUS_GET_MAX_BANDWIDTH_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->max_bandwidth; michael@0: } michael@0: break; michael@0: case OPUS_SET_BANDWIDTH_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: st->user_bandwidth = value; michael@0: if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { michael@0: st->silk_mode.maxInternalSampleRate = 8000; michael@0: } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { michael@0: st->silk_mode.maxInternalSampleRate = 12000; michael@0: } else { michael@0: st->silk_mode.maxInternalSampleRate = 16000; michael@0: } michael@0: } michael@0: break; michael@0: case OPUS_GET_BANDWIDTH_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->bandwidth; michael@0: } michael@0: break; michael@0: case OPUS_SET_DTX_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if(value<0 || value>1) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: st->silk_mode.useDTX = value; michael@0: } michael@0: break; michael@0: case OPUS_GET_DTX_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->silk_mode.useDTX; michael@0: } michael@0: break; michael@0: case OPUS_SET_COMPLEXITY_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if(value<0 || value>10) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: st->silk_mode.complexity = value; michael@0: celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value)); michael@0: } michael@0: break; michael@0: case OPUS_GET_COMPLEXITY_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->silk_mode.complexity; michael@0: } michael@0: break; michael@0: case OPUS_SET_INBAND_FEC_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if(value<0 || value>1) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: st->silk_mode.useInBandFEC = value; michael@0: } michael@0: break; michael@0: case OPUS_GET_INBAND_FEC_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->silk_mode.useInBandFEC; michael@0: } michael@0: break; michael@0: case OPUS_SET_PACKET_LOSS_PERC_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if (value < 0 || value > 100) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: st->silk_mode.packetLossPercentage = value; michael@0: celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value)); michael@0: } michael@0: break; michael@0: case OPUS_GET_PACKET_LOSS_PERC_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->silk_mode.packetLossPercentage; michael@0: } michael@0: break; michael@0: case OPUS_SET_VBR_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if(value<0 || value>1) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: st->use_vbr = value; michael@0: st->silk_mode.useCBR = 1-value; michael@0: } michael@0: break; michael@0: case OPUS_GET_VBR_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->use_vbr; michael@0: } michael@0: break; michael@0: case OPUS_SET_VOICE_RATIO_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if (value<-1 || value>100) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: st->voice_ratio = value; michael@0: } michael@0: break; michael@0: case OPUS_GET_VOICE_RATIO_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->voice_ratio; michael@0: } michael@0: break; michael@0: case OPUS_SET_VBR_CONSTRAINT_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if(value<0 || value>1) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: st->vbr_constraint = value; michael@0: } michael@0: break; michael@0: case OPUS_GET_VBR_CONSTRAINT_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->vbr_constraint; michael@0: } michael@0: break; michael@0: case OPUS_SET_SIGNAL_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNAL_MUSIC) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: st->signal_type = value; michael@0: } michael@0: break; michael@0: case OPUS_GET_SIGNAL_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->signal_type; michael@0: } michael@0: break; michael@0: case OPUS_GET_LOOKAHEAD_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->Fs/400; michael@0: if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY) michael@0: *value += st->delay_compensation; michael@0: } michael@0: break; michael@0: case OPUS_GET_SAMPLE_RATE_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->Fs; michael@0: } michael@0: break; michael@0: case OPUS_GET_FINAL_RANGE_REQUEST: michael@0: { michael@0: opus_uint32 *value = va_arg(ap, opus_uint32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->rangeFinal; michael@0: } michael@0: break; michael@0: case OPUS_SET_LSB_DEPTH_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if (value<8 || value>24) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: st->lsb_depth=value; michael@0: } michael@0: break; michael@0: case OPUS_GET_LSB_DEPTH_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->lsb_depth; michael@0: } michael@0: break; michael@0: case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if (value != OPUS_FRAMESIZE_ARG && value != OPUS_FRAMESIZE_2_5_MS && michael@0: value != OPUS_FRAMESIZE_5_MS && value != OPUS_FRAMESIZE_10_MS && michael@0: value != OPUS_FRAMESIZE_20_MS && value != OPUS_FRAMESIZE_40_MS && michael@0: value != OPUS_FRAMESIZE_60_MS && value != OPUS_FRAMESIZE_VARIABLE) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: st->variable_duration = value; michael@0: celt_encoder_ctl(celt_enc, OPUS_SET_EXPERT_FRAME_DURATION(value)); michael@0: } michael@0: break; michael@0: case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->variable_duration; michael@0: } michael@0: break; michael@0: case OPUS_SET_PREDICTION_DISABLED_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if (value > 1 || value < 0) michael@0: goto bad_arg; michael@0: st->silk_mode.reducedDependency = value; michael@0: } michael@0: break; michael@0: case OPUS_GET_PREDICTION_DISABLED_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: goto bad_arg; michael@0: *value = st->silk_mode.reducedDependency; michael@0: } michael@0: break; michael@0: case OPUS_RESET_STATE: michael@0: { michael@0: void *silk_enc; michael@0: silk_EncControlStruct dummy; michael@0: silk_enc = (char*)st+st->silk_enc_offset; michael@0: michael@0: OPUS_CLEAR((char*)&st->OPUS_ENCODER_RESET_START, michael@0: sizeof(OpusEncoder)- michael@0: ((char*)&st->OPUS_ENCODER_RESET_START - (char*)st)); michael@0: michael@0: celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); michael@0: silk_InitEncoder( silk_enc, st->arch, &dummy ); michael@0: st->stream_channels = st->channels; michael@0: st->hybrid_stereo_width_Q14 = 1 << 14; michael@0: st->prev_HB_gain = Q15ONE; michael@0: st->first = 1; michael@0: st->mode = MODE_HYBRID; michael@0: st->bandwidth = OPUS_BANDWIDTH_FULLBAND; michael@0: st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); michael@0: } michael@0: break; michael@0: case OPUS_SET_FORCE_MODE_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: st->user_forced_mode = value; michael@0: } michael@0: break; michael@0: case OPUS_SET_LFE_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: st->lfe = value; michael@0: ret = celt_encoder_ctl(celt_enc, OPUS_SET_LFE(value)); michael@0: } michael@0: break; michael@0: case OPUS_SET_ENERGY_MASK_REQUEST: michael@0: { michael@0: opus_val16 *value = va_arg(ap, opus_val16*); michael@0: st->energy_masking = value; michael@0: ret = celt_encoder_ctl(celt_enc, OPUS_SET_ENERGY_MASK(value)); michael@0: } michael@0: break; michael@0: michael@0: case CELT_GET_MODE_REQUEST: michael@0: { michael@0: const CELTMode ** value = va_arg(ap, const CELTMode**); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: ret = celt_encoder_ctl(celt_enc, CELT_GET_MODE(value)); michael@0: } michael@0: break; michael@0: default: michael@0: /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/ michael@0: ret = OPUS_UNIMPLEMENTED; michael@0: break; michael@0: } michael@0: va_end(ap); michael@0: return ret; michael@0: bad_arg: michael@0: va_end(ap); michael@0: return OPUS_BAD_ARG; michael@0: } michael@0: michael@0: void opus_encoder_destroy(OpusEncoder *st) michael@0: { michael@0: opus_free(st); michael@0: }