int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels) { void *silk_dec; CELTDecoder *celt_dec; int ret, silkDecSizeBytes; if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000) || (channels!=1&&channels!=2)) return OPUS_BAD_ARG; OPUS_CLEAR((char*)st, opus_decoder_get_size(channels)); /* Initialize SILK encoder */ ret = silk_Get_Decoder_Size(&silkDecSizeBytes); if (ret) return OPUS_INTERNAL_ERROR; silkDecSizeBytes = align(silkDecSizeBytes); st->silk_dec_offset = align(sizeof(OpusDecoder)); st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes; silk_dec = (char*)st+st->silk_dec_offset; celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); st->stream_channels = st->channels = channels; st->Fs = Fs; st->DecControl.API_sampleRate = st->Fs; st->DecControl.nChannelsAPI = st->channels; st->arch = opus_select_arch(); /* Reset decoder */ ret = silk_InitDecoder( silk_dec ); if(ret)return OPUS_INTERNAL_ERROR; /* Initialize CELT decoder */ ret = celt_decoder_init(celt_dec, Fs, channels); if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR; celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0)); st->prev_mode = 0; st->frame_size = Fs/400; return OPUS_OK; }
OpusEncoder *opus_encoder_init(OpusEncoder* st, int Fs, int channels) { void *silk_enc; CELTEncoder *celt_enc; int err; int ret, silkEncSizeBytes, celtEncSizeBytes; memset(st, 0, sizeof(OpusEncoder)); /* Create SILK encoder */ ret = silk_Get_Encoder_Size( &silkEncSizeBytes ); if( ret ) return NULL; silkEncSizeBytes = align(silkEncSizeBytes); celtEncSizeBytes = celt_encoder_get_size(channels); st->silk_enc_offset = align(sizeof(OpusEncoder)); st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes; silk_enc = (char*)st+st->silk_enc_offset; celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); st->stream_channels = st->channels = channels; st->Fs = Fs; ret = silk_InitEncoder( silk_enc, &st->silk_mode ); if( ret ) goto failure; /* default SILK parameters */ st->silk_mode.nChannelsAPI = channels; st->silk_mode.nChannelsInternal = channels; st->silk_mode.API_sampleRate = st->Fs; st->silk_mode.maxInternalSampleRate = 16000; st->silk_mode.minInternalSampleRate = 8000; st->silk_mode.desiredInternalSampleRate = 16000; st->silk_mode.payloadSize_ms = 20; st->silk_mode.bitRate = 25000; st->silk_mode.packetLossPercentage = 0; st->silk_mode.complexity = 10; st->silk_mode.useInBandFEC = 0; st->silk_mode.useDTX = 0; st->silk_mode.useCBR = 0; /* Create CELT encoder */ /* Initialize CELT encoder */ celt_encoder_init(celt_enc, Fs, channels, &err); if (err != CELT_OK) goto failure; celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0)); st->mode = MODE_HYBRID; st->bandwidth = BANDWIDTH_FULLBAND; st->use_vbr = 0; st->user_bitrate_bps = OPUS_BITRATE_AUTO; st->bitrate_bps = 3000+Fs*channels; st->user_mode = OPUS_MODE_AUTO; st->user_bandwidth = BANDWIDTH_AUTO; st->voice_ratio = 90; st->first = 1; st->encoder_buffer = st->Fs/100; st->delay_compensation = st->Fs/400; if (st->Fs > 16000) st->delay_compensation += 10; return st; failure: free(st); return NULL; }