Home | History | Annotate | Download | only in src
      1 /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited
      2    Written by Jean-Marc Valin and Koen Vos */
      3 /*
      4    Redistribution and use in source and binary forms, with or without
      5    modification, are permitted provided that the following conditions
      6    are met:
      7 
      8    - Redistributions of source code must retain the above copyright
      9    notice, this list of conditions and the following disclaimer.
     10 
     11    - Redistributions in binary form must reproduce the above copyright
     12    notice, this list of conditions and the following disclaimer in the
     13    documentation and/or other materials provided with the distribution.
     14 
     15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     16    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     17    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     18    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
     19    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     20    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     22    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     23    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     24    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     25    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26 */
     27 
     28 #ifdef HAVE_CONFIG_H
     29 #include "config.h"
     30 #endif
     31 
     32 #include <stdarg.h>
     33 #include "celt.h"
     34 #include "entenc.h"
     35 #include "modes.h"
     36 #include "API.h"
     37 #include "stack_alloc.h"
     38 #include "float_cast.h"
     39 #include "opus.h"
     40 #include "arch.h"
     41 #include "opus_private.h"
     42 #include "os_support.h"
     43 
     44 #include "tuning_parameters.h"
     45 #ifdef FIXED_POINT
     46 #include "fixed/structs_FIX.h"
     47 #else
     48 #include "float/structs_FLP.h"
     49 #endif
     50 
     51 #define MAX_ENCODER_BUFFER 480
     52 
     53 struct OpusEncoder {
     54     int          celt_enc_offset;
     55     int          silk_enc_offset;
     56     silk_EncControlStruct silk_mode;
     57     int          application;
     58     int          channels;
     59     int          delay_compensation;
     60     int          force_channels;
     61     int          signal_type;
     62     int          user_bandwidth;
     63     int          max_bandwidth;
     64     int          user_forced_mode;
     65     int          voice_ratio;
     66     opus_int32   Fs;
     67     int          use_vbr;
     68     int          vbr_constraint;
     69     opus_int32   bitrate_bps;
     70     opus_int32   user_bitrate_bps;
     71     int          encoder_buffer;
     72 
     73 #define OPUS_ENCODER_RESET_START stream_channels
     74     int          stream_channels;
     75     opus_int16   hybrid_stereo_width_Q14;
     76     opus_int32   variable_HP_smth2_Q15;
     77     opus_val32   hp_mem[4];
     78     int          mode;
     79     int          prev_mode;
     80     int          prev_channels;
     81     int          prev_framesize;
     82     int          bandwidth;
     83     int          silk_bw_switch;
     84     /* Sampling rate (at the API level) */
     85     int          first;
     86     opus_val16   delay_buffer[MAX_ENCODER_BUFFER*2];
     87 
     88     opus_uint32  rangeFinal;
     89 };
     90 
     91 /* Transition tables for the voice and music. First column is the
     92    middle (memoriless) threshold. The second column is the hysteresis
     93    (difference with the middle) */
     94 static const opus_int32 mono_voice_bandwidth_thresholds[8] = {
     95         11000, 1000, /* NB<->MB */
     96         14000, 1000, /* MB<->WB */
     97         21000, 2000, /* WB<->SWB */
     98         29000, 2000, /* SWB<->FB */
     99 };
    100 static const opus_int32 mono_music_bandwidth_thresholds[8] = {
    101         14000, 1000, /* MB not allowed */
    102         18000, 2000, /* MB<->WB */
    103         24000, 2000, /* WB<->SWB */
    104         33000, 2000, /* SWB<->FB */
    105 };
    106 static const opus_int32 stereo_voice_bandwidth_thresholds[8] = {
    107         11000, 1000, /* NB<->MB */
    108         14000, 1000, /* MB<->WB */
    109         21000, 2000, /* WB<->SWB */
    110         32000, 2000, /* SWB<->FB */
    111 };
    112 static const opus_int32 stereo_music_bandwidth_thresholds[8] = {
    113         14000, 1000, /* MB not allowed */
    114         18000, 2000, /* MB<->WB */
    115         24000, 2000, /* WB<->SWB */
    116         48000, 2000, /* SWB<->FB */
    117 };
    118 /* Threshold bit-rates for switching between mono and stereo */
    119 static const opus_int32 stereo_voice_threshold = 26000;
    120 static const opus_int32 stereo_music_threshold = 36000;
    121 
    122 /* Threshold bit-rate for switching between SILK/hybrid and CELT-only */
    123 static const opus_int32 mode_thresholds[2][2] = {
    124       /* voice */ /* music */
    125       {  48000,      24000}, /* mono */
    126       {  48000,      24000}, /* stereo */
    127 };
    128 
    129 int opus_encoder_get_size(int channels)
    130 {
    131     int silkEncSizeBytes, celtEncSizeBytes;
    132     int ret;
    133     if (channels<1 || channels > 2)
    134         return 0;
    135     ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
    136     if (ret)
    137         return 0;
    138     silkEncSizeBytes = align(silkEncSizeBytes);
    139     celtEncSizeBytes = celt_encoder_get_size(channels);
    140     return align(sizeof(OpusEncoder))+silkEncSizeBytes+celtEncSizeBytes;
    141 }
    142 
    143 int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int application)
    144 {
    145     void *silk_enc;
    146     CELTEncoder *celt_enc;
    147     int err;
    148     int ret, silkEncSizeBytes;
    149 
    150    if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2)||
    151         (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO
    152         && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY))
    153         return OPUS_BAD_ARG;
    154 
    155     OPUS_CLEAR((char*)st, opus_encoder_get_size(channels));
    156     /* Create SILK encoder */
    157     ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
    158     if (ret)
    159         return OPUS_BAD_ARG;
    160     silkEncSizeBytes = align(silkEncSizeBytes);
    161     st->silk_enc_offset = align(sizeof(OpusEncoder));
    162     st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes;
    163     silk_enc = (char*)st+st->silk_enc_offset;
    164     celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
    165 
    166     st->stream_channels = st->channels = channels;
    167 
    168     st->Fs = Fs;
    169 
    170     ret = silk_InitEncoder( silk_enc, &st->silk_mode );
    171     if(ret)return OPUS_INTERNAL_ERROR;
    172 
    173     /* default SILK parameters */
    174     st->silk_mode.nChannelsAPI              = channels;
    175     st->silk_mode.nChannelsInternal         = channels;
    176     st->silk_mode.API_sampleRate            = st->Fs;
    177     st->silk_mode.maxInternalSampleRate     = 16000;
    178     st->silk_mode.minInternalSampleRate     = 8000;
    179     st->silk_mode.desiredInternalSampleRate = 16000;
    180     st->silk_mode.payloadSize_ms            = 20;
    181     st->silk_mode.bitRate                   = 25000;
    182     st->silk_mode.packetLossPercentage      = 0;
    183     st->silk_mode.complexity                = 10;
    184     st->silk_mode.useInBandFEC              = 0;
    185     st->silk_mode.useDTX                    = 0;
    186     st->silk_mode.useCBR                    = 0;
    187 
    188     /* Create CELT encoder */
    189     /* Initialize CELT encoder */
    190     err = celt_encoder_init(celt_enc, Fs, channels);
    191     if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR;
    192 
    193     celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0));
    194     celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(10));
    195 
    196     st->use_vbr = 1;
    197     /* Makes constrained VBR the default (safer for real-time use) */
    198     st->vbr_constraint = 1;
    199     st->user_bitrate_bps = OPUS_AUTO;
    200     st->bitrate_bps = 3000+Fs*channels;
    201     st->application = application;
    202     st->signal_type = OPUS_AUTO;
    203     st->user_bandwidth = OPUS_AUTO;
    204     st->max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
    205     st->force_channels = OPUS_AUTO;
    206     st->user_forced_mode = OPUS_AUTO;
    207     st->voice_ratio = -1;
    208     st->encoder_buffer = st->Fs/100;
    209 
    210     /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead
    211        + 1.5 ms for SILK resamplers and stereo prediction) */
    212     st->delay_compensation = st->Fs/250;
    213 
    214     st->hybrid_stereo_width_Q14 = 1 << 14;
    215     st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
    216     st->first = 1;
    217     st->mode = MODE_HYBRID;
    218     st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
    219 
    220     return OPUS_OK;
    221 }
    222 
    223 static int pad_frame(unsigned char *data, opus_int32 len, opus_int32 new_len)
    224 {
    225    if (len == new_len)
    226       return 0;
    227    if (len > new_len)
    228       return 1;
    229 
    230    if ((data[0]&0x3)==0)
    231    {
    232       int i;
    233       int padding, nb_255s;
    234 
    235       padding = new_len - len;
    236       if (padding >= 2)
    237       {
    238          nb_255s = (padding-2)/255;
    239 
    240          for (i=len-1;i>=1;i--)
    241             data[i+nb_255s+2] = data[i];
    242          data[0] |= 0x3;
    243          data[1] = 0x41;
    244          for (i=0;i<nb_255s;i++)
    245             data[i+2] = 255;
    246          data[nb_255s+2] = padding-255*nb_255s-2;
    247          for (i=len+3+nb_255s;i<new_len;i++)
    248             data[i] = 0;
    249       } else {
    250          for (i=len-1;i>=1;i--)
    251             data[i+1] = data[i];
    252          data[0] |= 0x3;
    253          data[1] = 1;
    254       }
    255       return 0;
    256    } else {
    257       return 1;
    258    }
    259 }
    260 
    261 static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channels)
    262 {
    263    int period;
    264    unsigned char toc;
    265    period = 0;
    266    while (framerate < 400)
    267    {
    268        framerate <<= 1;
    269        period++;
    270    }
    271    if (mode == MODE_SILK_ONLY)
    272    {
    273        toc = (bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5;
    274        toc |= (period-2)<<3;
    275    } else if (mode == MODE_CELT_ONLY)
    276    {
    277        int tmp = bandwidth-OPUS_BANDWIDTH_MEDIUMBAND;
    278        if (tmp < 0)
    279            tmp = 0;
    280        toc = 0x80;
    281        toc |= tmp << 5;
    282        toc |= period<<3;
    283    } else /* Hybrid */
    284    {
    285        toc = 0x60;
    286        toc |= (bandwidth-OPUS_BANDWIDTH_SUPERWIDEBAND)<<4;
    287        toc |= (period-2)<<3;
    288    }
    289    toc |= (channels==2)<<2;
    290    return toc;
    291 }
    292 
    293 #ifndef FIXED_POINT
    294 static void silk_biquad_float(
    295     const opus_val16      *in,            /* I:    Input signal                   */
    296     const opus_int32      *B_Q28,         /* I:    MA coefficients [3]            */
    297     const opus_int32      *A_Q28,         /* I:    AR coefficients [2]            */
    298     opus_val32            *S,             /* I/O:  State vector [2]               */
    299     opus_val16            *out,           /* O:    Output signal                  */
    300     const opus_int32      len,            /* I:    Signal length (must be even)   */
    301     int stride
    302 )
    303 {
    304     /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */
    305     opus_int   k;
    306     opus_val32 vout;
    307     opus_val32 inval;
    308     opus_val32 A[2], B[3];
    309 
    310     A[0] = (opus_val32)(A_Q28[0] * (1.f/((opus_int32)1<<28)));
    311     A[1] = (opus_val32)(A_Q28[1] * (1.f/((opus_int32)1<<28)));
    312     B[0] = (opus_val32)(B_Q28[0] * (1.f/((opus_int32)1<<28)));
    313     B[1] = (opus_val32)(B_Q28[1] * (1.f/((opus_int32)1<<28)));
    314     B[2] = (opus_val32)(B_Q28[2] * (1.f/((opus_int32)1<<28)));
    315 
    316     /* Negate A_Q28 values and split in two parts */
    317 
    318     for( k = 0; k < len; k++ ) {
    319         /* S[ 0 ], S[ 1 ]: Q12 */
    320         inval = in[ k*stride ];
    321         vout = S[ 0 ] + B[0]*inval;
    322 
    323         S[ 0 ] = S[1] - vout*A[0] + B[1]*inval;
    324 
    325         S[ 1 ] = - vout*A[1] + B[2]*inval;
    326 
    327         /* Scale back to Q0 and saturate */
    328         out[ k*stride ] = vout;
    329     }
    330 }
    331 #endif
    332 
    333 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)
    334 {
    335    opus_int32 B_Q28[ 3 ], A_Q28[ 2 ];
    336    opus_int32 Fc_Q19, r_Q28, r_Q22;
    337 
    338    silk_assert( cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ) );
    339    Fc_Q19 = silk_DIV32_16( silk_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 );
    340    silk_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 );
    341 
    342    r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - silk_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q19 );
    343 
    344    /* b = r * [ 1; -2; 1 ]; */
    345    /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */
    346    B_Q28[ 0 ] = r_Q28;
    347    B_Q28[ 1 ] = silk_LSHIFT( -r_Q28, 1 );
    348    B_Q28[ 2 ] = r_Q28;
    349 
    350    /* -r * ( 2 - Fc * Fc ); */
    351    r_Q22  = silk_RSHIFT( r_Q28, 6 );
    352    A_Q28[ 0 ] = silk_SMULWW( r_Q22, silk_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CONST( 2.0,  22 ) );
    353    A_Q28[ 1 ] = silk_SMULWW( r_Q22, r_Q22 );
    354 
    355 #ifdef FIXED_POINT
    356    silk_biquad_alt( in, B_Q28, A_Q28, hp_mem, out, len, channels );
    357    if( channels == 2 ) {
    358        silk_biquad_alt( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels );
    359    }
    360 #else
    361    silk_biquad_float( in, B_Q28, A_Q28, hp_mem, out, len, channels );
    362    if( channels == 2 ) {
    363        silk_biquad_float( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels );
    364    }
    365 #endif
    366 }
    367 
    368 static void stereo_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, opus_val16 g2,
    369         int overlap48, int frame_size, int channels, const opus_val16 *window, opus_int32 Fs)
    370 {
    371     int i;
    372     int overlap;
    373     int inc;
    374     inc = 48000/Fs;
    375     overlap=overlap48/inc;
    376     g1 = Q15ONE-g1;
    377     g2 = Q15ONE-g2;
    378     for (i=0;i<overlap;i++)
    379     {
    380        opus_val32 diff;
    381        opus_val16 g, w;
    382        w = MULT16_16_Q15(window[i*inc], window[i*inc]);
    383        g = SHR32(MAC16_16(MULT16_16(w,g2),
    384              Q15ONE-w, g1), 15);
    385        diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]));
    386        diff = MULT16_16_Q15(g, diff);
    387        out[i*channels] = out[i*channels] - diff;
    388        out[i*channels+1] = out[i*channels+1] + diff;
    389     }
    390     for (;i<frame_size;i++)
    391     {
    392        opus_val32 diff;
    393        diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]));
    394        diff = MULT16_16_Q15(g2, diff);
    395        out[i*channels] = out[i*channels] - diff;
    396        out[i*channels+1] = out[i*channels+1] + diff;
    397     }
    398 }
    399 
    400 OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error)
    401 {
    402    int ret;
    403    OpusEncoder *st;
    404    if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2)||
    405        (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO
    406        && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY))
    407    {
    408       if (error)
    409          *error = OPUS_BAD_ARG;
    410       return NULL;
    411    }
    412    st = (OpusEncoder *)opus_alloc(opus_encoder_get_size(channels));
    413    if (st == NULL)
    414    {
    415       if (error)
    416          *error = OPUS_ALLOC_FAIL;
    417       return NULL;
    418    }
    419    ret = opus_encoder_init(st, Fs, channels, application);
    420    if (error)
    421       *error = ret;
    422    if (ret != OPUS_OK)
    423    {
    424       opus_free(st);
    425       st = NULL;
    426    }
    427    return st;
    428 }
    429 
    430 static opus_int32 user_bitrate_to_bitrate(OpusEncoder *st, int frame_size, int max_data_bytes)
    431 {
    432   if(!frame_size)frame_size=st->Fs/400;
    433   if (st->user_bitrate_bps==OPUS_AUTO)
    434     return 60*st->Fs/frame_size + st->Fs*st->channels;
    435   else if (st->user_bitrate_bps==OPUS_BITRATE_MAX)
    436     return max_data_bytes*8*st->Fs/frame_size;
    437   else
    438     return st->user_bitrate_bps;
    439 }
    440 
    441 #ifdef FIXED_POINT
    442 #define opus_encode_native opus_encode
    443 opus_int32 opus_encode(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
    444                 unsigned char *data, opus_int32 out_data_bytes)
    445 #else
    446 #define opus_encode_native opus_encode_float
    447 opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
    448                       unsigned char *data, opus_int32 out_data_bytes)
    449 #endif
    450 {
    451     void *silk_enc;
    452     CELTEncoder *celt_enc;
    453     int i;
    454     int ret=0;
    455     opus_int32 nBytes;
    456     ec_enc enc;
    457     int bytes_target;
    458     int prefill=0;
    459     int start_band = 0;
    460     int redundancy = 0;
    461     int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */
    462     int celt_to_silk = 0;
    463     VARDECL(opus_val16, pcm_buf);
    464     int nb_compr_bytes;
    465     int to_celt = 0;
    466     opus_uint32 redundant_rng = 0;
    467     int cutoff_Hz, hp_freq_smth1;
    468     int voice_est; /* Probability of voice in Q7 */
    469     opus_int32 equiv_rate;
    470     int delay_compensation;
    471     int frame_rate;
    472     opus_int32 max_rate; /* Max bitrate we're allowed to use */
    473     int curr_bandwidth;
    474     opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */
    475     VARDECL(opus_val16, tmp_prefill);
    476 
    477     ALLOC_STACK;
    478 
    479     max_data_bytes = IMIN(1276, out_data_bytes);
    480 
    481     st->rangeFinal = 0;
    482     if (400*frame_size != st->Fs && 200*frame_size != st->Fs && 100*frame_size != st->Fs &&
    483          50*frame_size != st->Fs &&  25*frame_size != st->Fs &&  50*frame_size != 3*st->Fs)
    484     {
    485        RESTORE_STACK;
    486        return OPUS_BAD_ARG;
    487     }
    488     if (max_data_bytes<=0)
    489     {
    490        RESTORE_STACK;
    491        return OPUS_BAD_ARG;
    492     }
    493     silk_enc = (char*)st+st->silk_enc_offset;
    494     celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
    495 
    496     if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
    497        delay_compensation = 0;
    498     else
    499        delay_compensation = st->delay_compensation;
    500 
    501     st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
    502 
    503     frame_rate = st->Fs/frame_size;
    504     if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8
    505        || (frame_rate<50 && (max_data_bytes*frame_rate<300 || st->bitrate_bps < 2400)))
    506     {
    507        /*If the space is too low to do something useful, emit 'PLC' frames.*/
    508        int tocmode = st->mode;
    509        int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth;
    510        if (tocmode==0)
    511           tocmode = MODE_SILK_ONLY;
    512        if (frame_rate>100)
    513           tocmode = MODE_CELT_ONLY;
    514        if (frame_rate < 50)
    515           tocmode = MODE_SILK_ONLY;
    516        if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND)
    517           bw=OPUS_BANDWIDTH_WIDEBAND;
    518        else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND)
    519           bw=OPUS_BANDWIDTH_NARROWBAND;
    520        else if (bw<=OPUS_BANDWIDTH_SUPERWIDEBAND)
    521           bw=OPUS_BANDWIDTH_SUPERWIDEBAND;
    522        data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels);
    523        RESTORE_STACK;
    524        return 1;
    525     }
    526     if (!st->use_vbr)
    527     {
    528        int cbrBytes;
    529        cbrBytes = IMIN( (st->bitrate_bps + 4*frame_rate)/(8*frame_rate) , max_data_bytes);
    530        st->bitrate_bps = cbrBytes * (8*frame_rate);
    531        max_data_bytes = cbrBytes;
    532     }
    533     max_rate = frame_rate*max_data_bytes*8;
    534 
    535     /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */
    536     equiv_rate = st->bitrate_bps - 60*(st->Fs/frame_size - 50);
    537 
    538     if (st->signal_type == OPUS_SIGNAL_VOICE)
    539        voice_est = 127;
    540     else if (st->signal_type == OPUS_SIGNAL_MUSIC)
    541        voice_est = 0;
    542     else if (st->voice_ratio >= 0)
    543        voice_est = st->voice_ratio*327>>8;
    544     else if (st->application == OPUS_APPLICATION_VOIP)
    545        voice_est = 115;
    546     else
    547        voice_est = 48;
    548 
    549     if (st->force_channels!=OPUS_AUTO && st->channels == 2)
    550     {
    551         st->stream_channels = st->force_channels;
    552     } else {
    553 #ifdef FUZZING
    554        /* Random mono/stereo decision */
    555        if (st->channels == 2 && (rand()&0x1F)==0)
    556           st->stream_channels = 3-st->stream_channels;
    557 #else
    558        /* Rate-dependent mono-stereo decision */
    559        if (st->channels == 2)
    560        {
    561           opus_int32 stereo_threshold;
    562           stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14);
    563           if (st->stream_channels == 2)
    564              stereo_threshold -= 4000;
    565           else
    566              stereo_threshold += 4000;
    567           st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
    568        } else {
    569           st->stream_channels = st->channels;
    570        }
    571 #endif
    572     }
    573 
    574     /* Mode selection depending on application and signal type */
    575     if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
    576     {
    577        st->mode = MODE_CELT_ONLY;
    578     } else if (st->user_forced_mode == OPUS_AUTO)
    579     {
    580 #ifdef FUZZING
    581        /* Random mode switching */
    582        if ((rand()&0xF)==0)
    583        {
    584           if ((rand()&0x1)==0)
    585              st->mode = MODE_CELT_ONLY;
    586           else
    587              st->mode = MODE_SILK_ONLY;
    588        } else {
    589           if (st->prev_mode==MODE_CELT_ONLY)
    590              st->mode = MODE_CELT_ONLY;
    591           else
    592              st->mode = MODE_SILK_ONLY;
    593        }
    594 #else
    595        int chan;
    596        opus_int32 mode_voice, mode_music;
    597        opus_int32 threshold;
    598 
    599        chan = (st->channels==2) && st->force_channels!=1;
    600        mode_voice = mode_thresholds[chan][0];
    601        mode_music = mode_thresholds[chan][1];
    602        threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14);
    603 
    604        /* Hysteresis */
    605        if (st->prev_mode == MODE_CELT_ONLY)
    606            threshold -= 4000;
    607        else if (st->prev_mode>0)
    608            threshold += 4000;
    609 
    610        st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY;
    611 
    612        /* When FEC is enabled and there's enough packet loss, use SILK */
    613        if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (128-voice_est)>>4)
    614           st->mode = MODE_SILK_ONLY;
    615        /* When encoding voice and DTX is enabled, set the encoder to SILK mode (at least for now) */
    616        if (st->silk_mode.useDTX && voice_est > 100)
    617           st->mode = MODE_SILK_ONLY;
    618 #endif
    619     } else {
    620        st->mode = st->user_forced_mode;
    621     }
    622 
    623     /* Override the chosen mode to make sure we meet the requested frame size */
    624     if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100)
    625        st->mode = MODE_CELT_ONLY;
    626 
    627     if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0
    628           && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)
    629     {
    630        /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */
    631        st->silk_mode.toMono = 1;
    632        st->stream_channels = 2;
    633     } else {
    634        st->silk_mode.toMono = 0;
    635     }
    636 
    637     if (st->prev_mode > 0 &&
    638         ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ||
    639     (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)))
    640     {
    641         redundancy = 1;
    642         celt_to_silk = (st->mode != MODE_CELT_ONLY);
    643         if (!celt_to_silk)
    644         {
    645             /* Switch to SILK/hybrid if frame size is 10 ms or more*/
    646             if (frame_size >= st->Fs/100)
    647             {
    648                 st->mode = st->prev_mode;
    649                 to_celt = 1;
    650             } else {
    651                 redundancy=0;
    652             }
    653         }
    654     }
    655     /* For the first frame at a new SILK bandwidth */
    656     if (st->silk_bw_switch)
    657     {
    658        redundancy = 1;
    659        celt_to_silk = 1;
    660        st->silk_bw_switch = 0;
    661     }
    662 
    663     if (redundancy)
    664     {
    665        /* Fair share of the max size allowed */
    666        redundancy_bytes = IMIN(257, max_data_bytes*(opus_int32)(st->Fs/200)/(frame_size+st->Fs/200));
    667        /* For VBR, target the actual bitrate (subject to the limit above) */
    668        if (st->use_vbr)
    669           redundancy_bytes = IMIN(redundancy_bytes, st->bitrate_bps/1600);
    670     }
    671 
    672     if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
    673     {
    674         silk_EncControlStruct dummy;
    675         silk_InitEncoder( silk_enc, &dummy);
    676         prefill=1;
    677     }
    678 
    679     /* Automatic (rate-dependent) bandwidth selection */
    680     if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch)
    681     {
    682         const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds;
    683         opus_int32 bandwidth_thresholds[8];
    684         int bandwidth = OPUS_BANDWIDTH_FULLBAND;
    685         opus_int32 equiv_rate2;
    686 
    687         equiv_rate2 = equiv_rate;
    688         if (st->mode != MODE_CELT_ONLY)
    689         {
    690            /* Adjust the threshold +/- 10% depending on complexity */
    691            equiv_rate2 = equiv_rate2 * (45+st->silk_mode.complexity)/50;
    692            /* CBR is less efficient by ~1 kb/s */
    693            if (!st->use_vbr)
    694               equiv_rate2 -= 1000;
    695         }
    696         if (st->channels==2 && st->force_channels!=1)
    697         {
    698            voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds;
    699            music_bandwidth_thresholds = stereo_music_bandwidth_thresholds;
    700         } else {
    701            voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds;
    702            music_bandwidth_thresholds = mono_music_bandwidth_thresholds;
    703         }
    704         /* Interpolate bandwidth thresholds depending on voice estimation */
    705         for (i=0;i<8;i++)
    706         {
    707            bandwidth_thresholds[i] = music_bandwidth_thresholds[i]
    708                     + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14);
    709         }
    710         do {
    711             int threshold, hysteresis;
    712             threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)];
    713             hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1];
    714             if (!st->first)
    715             {
    716                 if (st->bandwidth >= bandwidth)
    717                     threshold -= hysteresis;
    718                 else
    719                     threshold += hysteresis;
    720             }
    721             if (equiv_rate2 >= threshold)
    722                 break;
    723         } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND);
    724         st->bandwidth = bandwidth;
    725         /* Prevents any transition to SWB/FB until the SILK layer has fully
    726            switched to WB mode and turned the variable LP filter off */
    727         if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
    728             st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
    729     }
    730 
    731     if (st->bandwidth>st->max_bandwidth)
    732        st->bandwidth = st->max_bandwidth;
    733 
    734     if (st->user_bandwidth != OPUS_AUTO)
    735         st->bandwidth = st->user_bandwidth;
    736 
    737     /* This prevents us from using hybrid at unsafe CBR/max rates */
    738     if (st->mode != MODE_CELT_ONLY && max_rate < 15000)
    739     {
    740        st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND);
    741     }
    742 
    743     /* Prevents Opus from wasting bits on frequencies that are above
    744        the Nyquist rate of the input signal */
    745     if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND)
    746         st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
    747     if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
    748         st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
    749     if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND)
    750         st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
    751     if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
    752         st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
    753 
    754     /* If max_data_bytes represents less than 8 kb/s, switch to CELT-only mode */
    755     if (max_data_bytes < (frame_rate > 50 ? 12000 : 8000)*frame_size / (st->Fs * 8))
    756        st->mode = MODE_CELT_ONLY;
    757 
    758     /* CELT mode doesn't support mediumband, use wideband instead */
    759     if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
    760         st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
    761 
    762     /* Can't support higher than wideband for >20 ms frames */
    763     if (frame_size > st->Fs/50 && (st->mode == MODE_CELT_ONLY || st->bandwidth > OPUS_BANDWIDTH_WIDEBAND))
    764     {
    765        VARDECL(unsigned char, tmp_data);
    766        int nb_frames;
    767        int bak_mode, bak_bandwidth, bak_channels, bak_to_mono;
    768        OpusRepacketizer rp;
    769        opus_int32 bytes_per_frame;
    770 
    771 
    772        nb_frames = frame_size > st->Fs/25 ? 3 : 2;
    773        bytes_per_frame = IMIN(1276,(out_data_bytes-3)/nb_frames);
    774 
    775        ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char);
    776 
    777        opus_repacketizer_init(&rp);
    778 
    779        bak_mode = st->user_forced_mode;
    780        bak_bandwidth = st->user_bandwidth;
    781        bak_channels = st->force_channels;
    782 
    783        st->user_forced_mode = st->mode;
    784        st->user_bandwidth = st->bandwidth;
    785        st->force_channels = st->stream_channels;
    786        bak_to_mono = st->silk_mode.toMono;
    787 
    788        if (bak_to_mono)
    789           st->force_channels = 1;
    790        else
    791           st->prev_channels = st->stream_channels;
    792        for (i=0;i<nb_frames;i++)
    793        {
    794           int tmp_len;
    795           st->silk_mode.toMono = 0;
    796           /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */
    797           if (to_celt && i==nb_frames-1)
    798              st->user_forced_mode = MODE_CELT_ONLY;
    799           tmp_len = opus_encode_native(st, pcm+i*(st->channels*st->Fs/50), st->Fs/50, tmp_data+i*bytes_per_frame, bytes_per_frame);
    800           if (tmp_len<0)
    801           {
    802              RESTORE_STACK;
    803              return OPUS_INTERNAL_ERROR;
    804           }
    805           ret = opus_repacketizer_cat(&rp, tmp_data+i*bytes_per_frame, tmp_len);
    806           if (ret<0)
    807           {
    808              RESTORE_STACK;
    809              return OPUS_INTERNAL_ERROR;
    810           }
    811        }
    812        ret = opus_repacketizer_out(&rp, data, out_data_bytes);
    813        if (ret<0)
    814        {
    815           RESTORE_STACK;
    816           return OPUS_INTERNAL_ERROR;
    817        }
    818        st->user_forced_mode = bak_mode;
    819        st->user_bandwidth = bak_bandwidth;
    820        st->force_channels = bak_channels;
    821        st->silk_mode.toMono = bak_to_mono;
    822        RESTORE_STACK;
    823        return ret;
    824     }
    825 
    826     curr_bandwidth = st->bandwidth;
    827 
    828     /* Chooses the appropriate mode for speech
    829        *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */
    830     if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
    831         st->mode = MODE_HYBRID;
    832     if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND)
    833         st->mode = MODE_SILK_ONLY;
    834 
    835     /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */
    836     bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1;
    837 
    838     data += 1;
    839 
    840     ec_enc_init(&enc, data, max_data_bytes-1);
    841 
    842     ALLOC(pcm_buf, (delay_compensation+frame_size)*st->channels, opus_val16);
    843     for (i=0;i<delay_compensation*st->channels;i++)
    844        pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-delay_compensation)*st->channels+i];
    845 
    846     if (st->mode == MODE_CELT_ONLY)
    847        hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
    848     else
    849        hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15;
    850 
    851     st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15,
    852           hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) );
    853 
    854     /* convert from log scale to Hertz */
    855     cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
    856 
    857     if (st->application == OPUS_APPLICATION_VOIP)
    858     {
    859        hp_cutoff(pcm, cutoff_Hz, &pcm_buf[delay_compensation*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
    860     } else {
    861        for (i=0;i<frame_size*st->channels;i++)
    862           pcm_buf[delay_compensation*st->channels + i] = pcm[i];
    863     }
    864 
    865     /* SILK processing */
    866     if (st->mode != MODE_CELT_ONLY)
    867     {
    868 #ifdef FIXED_POINT
    869        const opus_int16 *pcm_silk;
    870 #else
    871        VARDECL(opus_int16, pcm_silk);
    872        ALLOC(pcm_silk, st->channels*frame_size, opus_int16);
    873 #endif
    874         st->silk_mode.bitRate = 8*bytes_target*frame_rate;
    875         if( st->mode == MODE_HYBRID ) {
    876             st->silk_mode.bitRate /= st->stream_channels;
    877             if( curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND ) {
    878                 if( st->Fs == 100 * frame_size ) {
    879                     /* 24 kHz, 10 ms */
    880                     st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 2000 + st->use_vbr * 1000 ) * 2 ) / 3;
    881                 } else {
    882                     /* 24 kHz, 20 ms */
    883                     st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 1000 + st->use_vbr * 1000 ) * 2 ) / 3;
    884                 }
    885             } else {
    886                 if( st->Fs == 100 * frame_size ) {
    887                     /* 48 kHz, 10 ms */
    888                     st->silk_mode.bitRate = ( st->silk_mode.bitRate + 8000 + st->use_vbr * 3000 ) / 2;
    889                 } else {
    890                     /* 48 kHz, 20 ms */
    891                     st->silk_mode.bitRate = ( st->silk_mode.bitRate + 9000 + st->use_vbr * 1000 ) / 2;
    892                 }
    893             }
    894             st->silk_mode.bitRate *= st->stream_channels;
    895             /* don't let SILK use more than 80% */
    896             if( st->silk_mode.bitRate > ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5 ) {
    897                 st->silk_mode.bitRate = ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5;
    898             }
    899         }
    900 
    901         st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
    902         st->silk_mode.nChannelsAPI = st->channels;
    903         st->silk_mode.nChannelsInternal = st->stream_channels;
    904         if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
    905             st->silk_mode.desiredInternalSampleRate = 8000;
    906         } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
    907             st->silk_mode.desiredInternalSampleRate = 12000;
    908         } else {
    909             silk_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND );
    910             st->silk_mode.desiredInternalSampleRate = 16000;
    911         }
    912         if( st->mode == MODE_HYBRID ) {
    913             /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
    914             st->silk_mode.minInternalSampleRate = 16000;
    915         } else {
    916             st->silk_mode.minInternalSampleRate = 8000;
    917         }
    918 
    919         if (st->mode == MODE_SILK_ONLY)
    920         {
    921            opus_int32 effective_max_rate = max_rate;
    922            st->silk_mode.maxInternalSampleRate = 16000;
    923            if (frame_rate > 50)
    924               effective_max_rate = effective_max_rate*2/3;
    925            if (effective_max_rate < 13000)
    926            {
    927               st->silk_mode.maxInternalSampleRate = 12000;
    928               st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate);
    929            }
    930            if (effective_max_rate < 9600)
    931            {
    932               st->silk_mode.maxInternalSampleRate = 8000;
    933               st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate);
    934            }
    935         } else {
    936            st->silk_mode.maxInternalSampleRate = 16000;
    937         }
    938 
    939         st->silk_mode.useCBR = !st->use_vbr;
    940 
    941         /* Call SILK encoder for the low band */
    942         nBytes = IMIN(1275, max_data_bytes-1-redundancy_bytes);
    943 
    944         st->silk_mode.maxBits = nBytes*8;
    945         /* Only allow up to 90% of the bits for hybrid mode*/
    946         if (st->mode == MODE_HYBRID)
    947            st->silk_mode.maxBits = (opus_int32)st->silk_mode.maxBits*9/10;
    948         if (st->silk_mode.useCBR)
    949         {
    950            st->silk_mode.maxBits = (st->silk_mode.bitRate * frame_size / (st->Fs * 8))*8;
    951            /* Reduce the initial target to make it easier to reach the CBR rate */
    952            st->silk_mode.bitRate = IMAX(1, st->silk_mode.bitRate-2000);
    953         }
    954 
    955         if (prefill)
    956         {
    957             opus_int32 zero=0;
    958 #ifdef FIXED_POINT
    959             pcm_silk = st->delay_buffer;
    960 #else
    961             for (i=0;i<st->encoder_buffer*st->channels;i++)
    962                 pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]);
    963 #endif
    964             silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, 1 );
    965         }
    966 
    967 #ifdef FIXED_POINT
    968         pcm_silk = pcm_buf+delay_compensation*st->channels;
    969 #else
    970         for (i=0;i<frame_size*st->channels;i++)
    971             pcm_silk[i] = FLOAT2INT16(pcm_buf[delay_compensation*st->channels + i]);
    972 #endif
    973         ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0 );
    974         if( ret ) {
    975             /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
    976             /* Handle error */
    977            RESTORE_STACK;
    978            return OPUS_INTERNAL_ERROR;
    979         }
    980         if (nBytes==0)
    981         {
    982            st->rangeFinal = 0;
    983            data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
    984            RESTORE_STACK;
    985            return 1;
    986         }
    987         /* Extract SILK internal bandwidth for signaling in first byte */
    988         if( st->mode == MODE_SILK_ONLY ) {
    989             if( st->silk_mode.internalSampleRate == 8000 ) {
    990                curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
    991             } else if( st->silk_mode.internalSampleRate == 12000 ) {
    992                curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
    993             } else if( st->silk_mode.internalSampleRate == 16000 ) {
    994                curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
    995             }
    996         } else {
    997             silk_assert( st->silk_mode.internalSampleRate == 16000 );
    998         }
    999 
   1000         st->silk_mode.opusCanSwitch = st->silk_mode.switchReady;
   1001         /* FIXME: How do we allocate the redundancy for CBR? */
   1002         if (st->silk_mode.opusCanSwitch)
   1003         {
   1004            redundancy = 1;
   1005            celt_to_silk = 0;
   1006            st->silk_bw_switch = 1;
   1007         }
   1008     }
   1009 
   1010     /* CELT processing */
   1011     {
   1012         int endband=21;
   1013 
   1014         switch(curr_bandwidth)
   1015         {
   1016             case OPUS_BANDWIDTH_NARROWBAND:
   1017                 endband = 13;
   1018                 break;
   1019             case OPUS_BANDWIDTH_MEDIUMBAND:
   1020             case OPUS_BANDWIDTH_WIDEBAND:
   1021                 endband = 17;
   1022                 break;
   1023             case OPUS_BANDWIDTH_SUPERWIDEBAND:
   1024                 endband = 19;
   1025                 break;
   1026             case OPUS_BANDWIDTH_FULLBAND:
   1027                 endband = 21;
   1028                 break;
   1029         }
   1030         celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband));
   1031         celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels));
   1032     }
   1033     celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
   1034     if (st->mode != MODE_SILK_ONLY)
   1035     {
   1036         celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
   1037         /* Allow prediction unless we decide to disable it later */
   1038         celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(2));
   1039 
   1040         if (st->mode == MODE_HYBRID)
   1041         {
   1042             int len;
   1043 
   1044             len = (ec_tell(&enc)+7)>>3;
   1045             if (redundancy)
   1046                len += st->mode == MODE_HYBRID ? 3 : 1;
   1047             if( st->use_vbr ) {
   1048                 nb_compr_bytes = len + bytes_target - (st->silk_mode.bitRate * frame_size) / (8 * st->Fs);
   1049             } else {
   1050                 /* check if SILK used up too much */
   1051                 nb_compr_bytes = len > bytes_target ? len : bytes_target;
   1052             }
   1053         } else {
   1054             if (st->use_vbr)
   1055             {
   1056                 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
   1057                 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint));
   1058                 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps));
   1059                 nb_compr_bytes = max_data_bytes-1-redundancy_bytes;
   1060             } else {
   1061                 nb_compr_bytes = bytes_target;
   1062             }
   1063         }
   1064 
   1065     } else {
   1066         nb_compr_bytes = 0;
   1067     }
   1068 
   1069     ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16);
   1070     if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0)
   1071     {
   1072        for (i=0;i<st->channels*st->Fs/400;i++)
   1073           tmp_prefill[i] = st->delay_buffer[(st->encoder_buffer-st->delay_compensation-st->Fs/400)*st->channels + i];
   1074     }
   1075 
   1076     for (i=0;i<st->channels*(st->encoder_buffer-(frame_size+delay_compensation));i++)
   1077         st->delay_buffer[i] = st->delay_buffer[i+st->channels*frame_size];
   1078     for (;i<st->encoder_buffer*st->channels;i++)
   1079         st->delay_buffer[i] = pcm_buf[(frame_size+delay_compensation-st->encoder_buffer)*st->channels+i];
   1080 
   1081 
   1082     if (st->mode != MODE_HYBRID || st->stream_channels==1)
   1083        st->silk_mode.stereoWidth_Q14 = 1<<14;
   1084     if( st->channels == 2 ) {
   1085         /* Apply stereo width reduction (at low bitrates) */
   1086         if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) {
   1087             opus_val16 g1, g2;
   1088             const CELTMode *celt_mode;
   1089 
   1090             celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
   1091             g1 = st->hybrid_stereo_width_Q14;
   1092             g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
   1093 #ifdef FIXED_POINT
   1094             g1 = g1==16384 ? Q15ONE : SHL16(g1,1);
   1095             g2 = g2==16384 ? Q15ONE : SHL16(g2,1);
   1096 #else
   1097             g1 *= (1.f/16384);
   1098             g2 *= (1.f/16384);
   1099 #endif
   1100             stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
   1101                   frame_size, st->channels, celt_mode->window, st->Fs);
   1102             st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
   1103         }
   1104     }
   1105 
   1106     if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) <= 8*(max_data_bytes-1))
   1107     {
   1108         /* For SILK mode, the redundancy is inferred from the length */
   1109         if (st->mode == MODE_HYBRID && (redundancy || ec_tell(&enc)+37 <= 8*nb_compr_bytes))
   1110            ec_enc_bit_logp(&enc, redundancy, 12);
   1111         if (redundancy)
   1112         {
   1113             int max_redundancy;
   1114             ec_enc_bit_logp(&enc, celt_to_silk, 1);
   1115             if (st->mode == MODE_HYBRID)
   1116                max_redundancy = (max_data_bytes-1)-nb_compr_bytes-1;
   1117             else
   1118                max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3);
   1119             /* Target the same bit-rate for redundancy as for the rest,
   1120                up to a max of 257 bytes */
   1121             redundancy_bytes = IMIN(max_redundancy, st->bitrate_bps/1600);
   1122             redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes));
   1123             if (st->mode == MODE_HYBRID)
   1124                 ec_enc_uint(&enc, redundancy_bytes-2, 256);
   1125         }
   1126     } else {
   1127         redundancy = 0;
   1128     }
   1129 
   1130     if (!redundancy)
   1131     {
   1132        st->silk_bw_switch = 0;
   1133        redundancy_bytes = 0;
   1134     }
   1135     if (st->mode != MODE_CELT_ONLY)start_band=17;
   1136 
   1137     if (st->mode == MODE_SILK_ONLY)
   1138     {
   1139         ret = (ec_tell(&enc)+7)>>3;
   1140         ec_enc_done(&enc);
   1141         nb_compr_bytes = ret;
   1142     } else {
   1143        nb_compr_bytes = IMIN((max_data_bytes-1)-redundancy_bytes, nb_compr_bytes);
   1144        ec_enc_shrink(&enc, nb_compr_bytes);
   1145     }
   1146 
   1147 
   1148     /* 5 ms redundant frame for CELT->SILK */
   1149     if (redundancy && celt_to_silk)
   1150     {
   1151         int err;
   1152         celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
   1153         celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
   1154         err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL);
   1155         if (err < 0)
   1156         {
   1157            RESTORE_STACK;
   1158            return OPUS_INTERNAL_ERROR;
   1159         }
   1160         celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
   1161         celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
   1162     }
   1163 
   1164     celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band));
   1165 
   1166     if (st->mode != MODE_SILK_ONLY)
   1167     {
   1168         if (st->mode != st->prev_mode && st->prev_mode > 0)
   1169         {
   1170            unsigned char dummy[2];
   1171            celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
   1172 
   1173            /* Prefilling */
   1174            celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL);
   1175            celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
   1176         }
   1177         /* If false, we already busted the budget and we'll end up with a "PLC packet" */
   1178         if (ec_tell(&enc) <= 8*nb_compr_bytes)
   1179         {
   1180            ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc);
   1181            if (ret < 0)
   1182            {
   1183               RESTORE_STACK;
   1184               return OPUS_INTERNAL_ERROR;
   1185            }
   1186         }
   1187     }
   1188 
   1189     /* 5 ms redundant frame for SILK->CELT */
   1190     if (redundancy && !celt_to_silk)
   1191     {
   1192         int err;
   1193         unsigned char dummy[2];
   1194         int N2, N4;
   1195         N2 = st->Fs/200;
   1196         N4 = st->Fs/400;
   1197 
   1198         celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
   1199         celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
   1200         celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
   1201 
   1202         /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */
   1203         celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL);
   1204 
   1205         err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL);
   1206         if (err < 0)
   1207         {
   1208            RESTORE_STACK;
   1209            return OPUS_INTERNAL_ERROR;
   1210         }
   1211         celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
   1212     }
   1213 
   1214 
   1215 
   1216     /* Signalling the mode in the first byte */
   1217     data--;
   1218     data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
   1219 
   1220     st->rangeFinal = enc.rng ^ redundant_rng;
   1221 
   1222     if (to_celt)
   1223         st->prev_mode = MODE_CELT_ONLY;
   1224     else
   1225         st->prev_mode = st->mode;
   1226     st->prev_channels = st->stream_channels;
   1227     st->prev_framesize = frame_size;
   1228 
   1229     st->first = 0;
   1230 
   1231     /* In the unlikely case that the SILK encoder busted its target, tell
   1232        the decoder to call the PLC */
   1233     if (ec_tell(&enc) > (max_data_bytes-1)*8)
   1234     {
   1235        if (max_data_bytes < 2)
   1236        {
   1237           RESTORE_STACK;
   1238           return OPUS_BUFFER_TOO_SMALL;
   1239        }
   1240        data[1] = 0;
   1241        ret = 1;
   1242        st->rangeFinal = 0;
   1243     } else if (st->mode==MODE_SILK_ONLY&&!redundancy)
   1244     {
   1245        /*When in LPC only mode it's perfectly
   1246          reasonable to strip off trailing zero bytes as
   1247          the required range decoder behavior is to
   1248          fill these in. This can't be done when the MDCT
   1249          modes are used because the decoder needs to know
   1250          the actual length for allocation purposes.*/
   1251        while(ret>2&&data[ret]==0)ret--;
   1252     }
   1253     /* Count ToC and redundancy */
   1254     ret += 1+redundancy_bytes;
   1255     if (!st->use_vbr && ret >= 3)
   1256     {
   1257        if (pad_frame(data, ret, max_data_bytes))
   1258        {
   1259           RESTORE_STACK;
   1260           return OPUS_INTERNAL_ERROR;
   1261        }
   1262        ret = max_data_bytes;
   1263     }
   1264     RESTORE_STACK;
   1265     return ret;
   1266 }
   1267 
   1268 #ifdef FIXED_POINT
   1269 
   1270 #ifndef DISABLE_FLOAT_API
   1271 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int frame_size,
   1272       unsigned char *data, opus_int32 max_data_bytes)
   1273 {
   1274    int i, ret;
   1275    VARDECL(opus_int16, in);
   1276    ALLOC_STACK;
   1277 
   1278    if(frame_size<0)
   1279    {
   1280       RESTORE_STACK;
   1281       return OPUS_BAD_ARG;
   1282    }
   1283 
   1284    ALLOC(in, frame_size*st->channels, opus_int16);
   1285 
   1286    for (i=0;i<frame_size*st->channels;i++)
   1287       in[i] = FLOAT2INT16(pcm[i]);
   1288    ret = opus_encode(st, in, frame_size, data, max_data_bytes);
   1289    RESTORE_STACK;
   1290    return ret;
   1291 }
   1292 #endif
   1293 
   1294 #else
   1295 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int frame_size,
   1296       unsigned char *data, opus_int32 max_data_bytes)
   1297 {
   1298    int i, ret;
   1299    VARDECL(float, in);
   1300    ALLOC_STACK;
   1301 
   1302    ALLOC(in, frame_size*st->channels, float);
   1303 
   1304    for (i=0;i<frame_size*st->channels;i++)
   1305       in[i] = (1.0f/32768)*pcm[i];
   1306    ret = opus_encode_float(st, in, frame_size, data, max_data_bytes);
   1307    RESTORE_STACK;
   1308    return ret;
   1309 }
   1310 #endif
   1311 
   1312 
   1313 int opus_encoder_ctl(OpusEncoder *st, int request, ...)
   1314 {
   1315     int ret;
   1316     CELTEncoder *celt_enc;
   1317     va_list ap;
   1318 
   1319     ret = OPUS_OK;
   1320     va_start(ap, request);
   1321 
   1322     celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
   1323 
   1324     switch (request)
   1325     {
   1326         case OPUS_SET_APPLICATION_REQUEST:
   1327         {
   1328             opus_int32 value = va_arg(ap, opus_int32);
   1329             if (   (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_AUDIO
   1330                  && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
   1331                || (!st->first && st->application != value))
   1332             {
   1333                ret = OPUS_BAD_ARG;
   1334                break;
   1335             }
   1336             st->application = value;
   1337         }
   1338         break;
   1339         case OPUS_GET_APPLICATION_REQUEST:
   1340         {
   1341             opus_int32 *value = va_arg(ap, opus_int32*);
   1342             *value = st->application;
   1343         }
   1344         break;
   1345         case OPUS_SET_BITRATE_REQUEST:
   1346         {
   1347             opus_int32 value = va_arg(ap, opus_int32);
   1348             if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX)
   1349             {
   1350                 if (value <= 0)
   1351                     goto bad_arg;
   1352                 else if (value <= 500)
   1353                     value = 500;
   1354                 else if (value > (opus_int32)300000*st->channels)
   1355                     value = (opus_int32)300000*st->channels;
   1356             }
   1357             st->user_bitrate_bps = value;
   1358         }
   1359         break;
   1360         case OPUS_GET_BITRATE_REQUEST:
   1361         {
   1362             opus_int32 *value = va_arg(ap, opus_int32*);
   1363             *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276);
   1364         }
   1365         break;
   1366         case OPUS_SET_FORCE_CHANNELS_REQUEST:
   1367         {
   1368             opus_int32 value = va_arg(ap, opus_int32);
   1369             if((value<1 || value>st->channels) && value != OPUS_AUTO)
   1370                 return OPUS_BAD_ARG;
   1371             st->force_channels = value;
   1372         }
   1373         break;
   1374         case OPUS_GET_FORCE_CHANNELS_REQUEST:
   1375         {
   1376             opus_int32 *value = va_arg(ap, opus_int32*);
   1377             *value = st->force_channels;
   1378         }
   1379         break;
   1380         case OPUS_SET_MAX_BANDWIDTH_REQUEST:
   1381         {
   1382             opus_int32 value = va_arg(ap, opus_int32);
   1383             if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND)
   1384                 return OPUS_BAD_ARG;
   1385             st->max_bandwidth = value;
   1386             if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
   1387                 st->silk_mode.maxInternalSampleRate = 8000;
   1388             } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
   1389                 st->silk_mode.maxInternalSampleRate = 12000;
   1390             } else {
   1391                 st->silk_mode.maxInternalSampleRate = 16000;
   1392             }
   1393         }
   1394         break;
   1395         case OPUS_GET_MAX_BANDWIDTH_REQUEST:
   1396         {
   1397             opus_int32 *value = va_arg(ap, opus_int32*);
   1398             *value = st->max_bandwidth;
   1399         }
   1400         break;
   1401         case OPUS_SET_BANDWIDTH_REQUEST:
   1402         {
   1403             opus_int32 value = va_arg(ap, opus_int32);
   1404             if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO)
   1405                 return OPUS_BAD_ARG;
   1406             st->user_bandwidth = value;
   1407             if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
   1408                 st->silk_mode.maxInternalSampleRate = 8000;
   1409             } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
   1410                 st->silk_mode.maxInternalSampleRate = 12000;
   1411             } else {
   1412                 st->silk_mode.maxInternalSampleRate = 16000;
   1413             }
   1414         }
   1415         break;
   1416         case OPUS_GET_BANDWIDTH_REQUEST:
   1417         {
   1418             opus_int32 *value = va_arg(ap, opus_int32*);
   1419             *value = st->bandwidth;
   1420         }
   1421         break;
   1422         case OPUS_SET_DTX_REQUEST:
   1423         {
   1424             opus_int32 value = va_arg(ap, opus_int32);
   1425             if(value<0 || value>1)
   1426                 return OPUS_BAD_ARG;
   1427             st->silk_mode.useDTX = value;
   1428         }
   1429         break;
   1430         case OPUS_GET_DTX_REQUEST:
   1431         {
   1432             opus_int32 *value = va_arg(ap, opus_int32*);
   1433             *value = st->silk_mode.useDTX;
   1434         }
   1435         break;
   1436         case OPUS_SET_COMPLEXITY_REQUEST:
   1437         {
   1438             opus_int32 value = va_arg(ap, opus_int32);
   1439             if(value<0 || value>10)
   1440                 return OPUS_BAD_ARG;
   1441             st->silk_mode.complexity = value;
   1442             celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value));
   1443         }
   1444         break;
   1445         case OPUS_GET_COMPLEXITY_REQUEST:
   1446         {
   1447             opus_int32 *value = va_arg(ap, opus_int32*);
   1448             *value = st->silk_mode.complexity;
   1449         }
   1450         break;
   1451         case OPUS_SET_INBAND_FEC_REQUEST:
   1452         {
   1453             opus_int32 value = va_arg(ap, opus_int32);
   1454             if(value<0 || value>1)
   1455                 return OPUS_BAD_ARG;
   1456             st->silk_mode.useInBandFEC = value;
   1457         }
   1458         break;
   1459         case OPUS_GET_INBAND_FEC_REQUEST:
   1460         {
   1461             opus_int32 *value = va_arg(ap, opus_int32*);
   1462             *value = st->silk_mode.useInBandFEC;
   1463         }
   1464         break;
   1465         case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
   1466         {
   1467             opus_int32 value = va_arg(ap, opus_int32);
   1468             if (value < 0 || value > 100)
   1469                 return OPUS_BAD_ARG;
   1470             st->silk_mode.packetLossPercentage = value;
   1471             celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value));
   1472         }
   1473         break;
   1474         case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
   1475         {
   1476             opus_int32 *value = va_arg(ap, opus_int32*);
   1477             *value = st->silk_mode.packetLossPercentage;
   1478         }
   1479         break;
   1480         case OPUS_SET_VBR_REQUEST:
   1481         {
   1482             opus_int32 value = va_arg(ap, opus_int32);
   1483             if(value<0 || value>1)
   1484                 return OPUS_BAD_ARG;
   1485             st->use_vbr = value;
   1486             st->silk_mode.useCBR = 1-value;
   1487         }
   1488         break;
   1489         case OPUS_GET_VBR_REQUEST:
   1490         {
   1491             opus_int32 *value = va_arg(ap, opus_int32*);
   1492             *value = st->use_vbr;
   1493         }
   1494         break;
   1495         case OPUS_SET_VOICE_RATIO_REQUEST:
   1496         {
   1497             opus_int32 value = va_arg(ap, opus_int32);
   1498             if (value>100 || value<-1)
   1499                 goto bad_arg;
   1500             st->voice_ratio = value;
   1501         }
   1502         break;
   1503         case OPUS_GET_VOICE_RATIO_REQUEST:
   1504         {
   1505             opus_int32 *value = va_arg(ap, opus_int32*);
   1506             *value = st->voice_ratio;
   1507         }
   1508         break;
   1509         case OPUS_SET_VBR_CONSTRAINT_REQUEST:
   1510         {
   1511             opus_int32 value = va_arg(ap, opus_int32);
   1512             if(value<0 || value>1)
   1513                 return OPUS_BAD_ARG;
   1514             st->vbr_constraint = value;
   1515         }
   1516         break;
   1517         case OPUS_GET_VBR_CONSTRAINT_REQUEST:
   1518         {
   1519             opus_int32 *value = va_arg(ap, opus_int32*);
   1520             *value = st->vbr_constraint;
   1521         }
   1522         break;
   1523         case OPUS_SET_SIGNAL_REQUEST:
   1524         {
   1525             opus_int32 value = va_arg(ap, opus_int32);
   1526             if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNAL_MUSIC)
   1527                 return OPUS_BAD_ARG;
   1528             st->signal_type = value;
   1529         }
   1530         break;
   1531         case OPUS_GET_SIGNAL_REQUEST:
   1532         {
   1533             opus_int32 *value = va_arg(ap, opus_int32*);
   1534             *value = st->signal_type;
   1535         }
   1536         break;
   1537         case OPUS_GET_LOOKAHEAD_REQUEST:
   1538         {
   1539             opus_int32 *value = va_arg(ap, opus_int32*);
   1540             *value = st->Fs/400;
   1541             if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
   1542                 *value += st->delay_compensation;
   1543         }
   1544         break;
   1545         case OPUS_GET_SAMPLE_RATE_REQUEST:
   1546         {
   1547             opus_int32 *value = va_arg(ap, opus_int32*);
   1548             if (value==NULL)
   1549             {
   1550                 ret = OPUS_BAD_ARG;
   1551                 break;
   1552             }
   1553             *value = st->Fs;
   1554         }
   1555         break;
   1556         case OPUS_GET_FINAL_RANGE_REQUEST:
   1557         {
   1558             opus_uint32 *value = va_arg(ap, opus_uint32*);
   1559             *value = st->rangeFinal;
   1560         }
   1561         break;
   1562         case OPUS_SET_LSB_DEPTH_REQUEST:
   1563         {
   1564             opus_int32 value = va_arg(ap, opus_int32);
   1565             ret = celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(value));
   1566         }
   1567         break;
   1568         case OPUS_GET_LSB_DEPTH_REQUEST:
   1569         {
   1570             opus_int32 *value = va_arg(ap, opus_int32*);
   1571             celt_encoder_ctl(celt_enc, OPUS_GET_LSB_DEPTH(value));
   1572         }
   1573         break;
   1574         case OPUS_RESET_STATE:
   1575         {
   1576            void *silk_enc;
   1577            silk_EncControlStruct dummy;
   1578            silk_enc = (char*)st+st->silk_enc_offset;
   1579 
   1580            OPUS_CLEAR((char*)&st->OPUS_ENCODER_RESET_START,
   1581                  sizeof(OpusEncoder)-
   1582                  ((char*)&st->OPUS_ENCODER_RESET_START - (char*)st));
   1583 
   1584            celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
   1585            silk_InitEncoder( silk_enc, &dummy );
   1586            st->stream_channels = st->channels;
   1587            st->hybrid_stereo_width_Q14 = 1 << 14;
   1588            st->first = 1;
   1589            st->mode = MODE_HYBRID;
   1590            st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
   1591            st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
   1592         }
   1593         break;
   1594         case OPUS_SET_FORCE_MODE_REQUEST:
   1595         {
   1596             opus_int32 value = va_arg(ap, opus_int32);
   1597             if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO)
   1598                goto bad_arg;
   1599             st->user_forced_mode = value;
   1600         }
   1601         break;
   1602         default:
   1603             /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/
   1604             ret = OPUS_UNIMPLEMENTED;
   1605             break;
   1606     }
   1607     va_end(ap);
   1608     return ret;
   1609 bad_arg:
   1610     va_end(ap);
   1611     return OPUS_BAD_ARG;
   1612 }
   1613 
   1614 void opus_encoder_destroy(OpusEncoder *st)
   1615 {
   1616     opus_free(st);
   1617 }
   1618