long opus_create(const char* format_parameters, const char** format_parameters_out, amci_codec_fmt_info_t** format_description) { opus_state_t* codec_inst; int error; unsigned int maxbandwidth = _OPUS_RATE; int useinbandfec = _OPUS_INBAND_FEC_; int stereo = 0; if (format_parameters) { DBG("OPUS params: >>%s<<.\n", format_parameters); decode_format_parameters(format_parameters, &maxbandwidth, &useinbandfec, &stereo); } codec_inst = (opus_state_t*)malloc(sizeof(opus_state_t)); if (!codec_inst) return -1; DBG("OPUS: creating encoder with maxbandwidth=%u, stereo=%s, useinbandfec=%s\n", maxbandwidth, stereo?"true":"false", useinbandfec?"true":"false"); codec_inst->opus_enc = opus_encoder_create(_OPUS_RATE,1,_OPUS_APPLICATION_,&error); if (error) { DBG("OPUS: error %d while creating encoder state.\n", error); return -1; } opus_encoder_ctl(codec_inst->opus_enc, OPUS_SET_FORCE_CHANNELS(stereo ? 2:1)); unsigned int opus_set_bw = _OPUS_RATE; if (maxbandwidth <= 8000) { opus_set_bw = OPUS_BANDWIDTH_NARROWBAND; } else if (maxbandwidth <= 12000) { opus_set_bw = OPUS_BANDWIDTH_MEDIUMBAND; } else if (maxbandwidth <= 16000) { opus_set_bw = OPUS_BANDWIDTH_WIDEBAND; } else if (maxbandwidth <= 24000) { opus_set_bw = OPUS_BANDWIDTH_SUPERWIDEBAND; } else { opus_set_bw = OPUS_BANDWIDTH_FULLBAND; } opus_encoder_ctl(codec_inst->opus_enc, OPUS_SET_MAX_BANDWIDTH(opus_set_bw)); opus_encoder_ctl(codec_inst->opus_enc, OPUS_SET_PACKET_LOSS_PERC(_OPUS_PKT_LOSS_PCT_)); opus_encoder_ctl(codec_inst->opus_enc, OPUS_SET_COMPLEXITY(_OPUS_COMPLEXITY_)); opus_encoder_ctl(codec_inst->opus_enc, OPUS_SET_INBAND_FEC(useinbandfec ? 1:0)); opus_encoder_ctl(codec_inst->opus_enc, OPUS_SET_DTX(_OPUS_DTX_)); codec_inst->opus_dec = opus_decoder_create(_OPUS_RATE,1,&error); if (error) { DBG("OPUS: error %d while creating decoder state.\n", error); opus_encoder_destroy(codec_inst->opus_enc); return -1; } *format_description = opus_fmt_description; return (long)codec_inst; }
JNIEXPORT jint JNICALL Java_org_jitsi_impl_neomedia_codec_audio_opus_Opus_encoder_1set_1force_1channels (JNIEnv *env, jclass clazz, jlong encoder, jint forcechannels) { opus_int32 x = forcechannels; return opus_encoder_ctl( (OpusEncoder *) (intptr_t) encoder, OPUS_SET_FORCE_CHANNELS(x)); }
static void ms_opus_enc_preprocess(MSFilter *f) { int error; OpusEncData *d = (OpusEncData *)f->data; /* create the encoder */ d->state = opus_encoder_create(d->samplerate, d->channels, d->application, &error); if (error != OPUS_OK) { ms_error("Opus encoder creation failed: %s", opus_strerror(error)); return; } /* set complexity to 0 for single processor arm devices */ #if defined(__arm__) || defined(_M_ARM) if (ms_factory_get_cpu_count(f->factory)==1){ opus_encoder_ctl(d->state, OPUS_SET_COMPLEXITY(0)); }else{ opus_encoder_ctl(d->state, OPUS_SET_COMPLEXITY(5)); } #endif error = opus_encoder_ctl(d->state, OPUS_SET_PACKET_LOSS_PERC(10)); if (error != OPUS_OK) { ms_error("Could not set default loss percentage to opus encoder: %s", opus_strerror(error)); } /* set the encoder parameters: VBR, IN_BAND_FEC, DTX and bitrate settings */ ms_opus_enc_set_vbr(f); ms_opus_enc_set_inbandfec(f); ms_opus_enc_set_dtx(f); /* if decoder prefers mono signal, force encoder to output mono signal */ if (d->stereo == 0) { error = opus_encoder_ctl(d->state, OPUS_SET_FORCE_CHANNELS(1)); if (error != OPUS_OK) { ms_error("could not force mono channel to opus encoder: %s", opus_strerror(error)); } if (d->channels == 2) ms_message("Opus encoder configured to encode mono despite it is feed with stereo."); }else if (d->channels == 2){ ms_message("Opus encoder configured to encode stereo."); } ms_filter_lock(f); // set bitrate wasn't call, compute it with the default network bitrate (36000) if (d->bitrate==-1) { compute_max_bitrate(d, 0); } apply_max_bitrate(d); ms_filter_unlock(f); }
opus_encoder::opus_encoder() { int err; _opus = opus_encoder_create(SAMPLE_RATE,CHANNELS, OPUS_APPLICATION_VOIP, &err); if(err != OPUS_OK) { LOG << "opus encoder create error: "; log_opus_error(err); } opus_encoder_ctl(_opus, OPUS_SET_BITRATE(OPUS_AUTO)); opus_encoder_ctl(_opus, OPUS_SET_VBR(1)); opus_encoder_ctl(_opus, OPUS_SET_FORCE_CHANNELS(1)); //force mono opus_encoder_ctl(_opus, OPUS_SET_PACKET_LOSS_PERC(2)); ENSURE(_opus); }
static void ms_opus_enc_preprocess(MSFilter *f) { int error; OpusEncData *d = (OpusEncData *)f->data; /* create the encoder */ d->state = opus_encoder_create(d->samplerate, d->channels, d->application, &error); if (error != OPUS_OK) { ms_error("Opus encoder creation failed: %s", opus_strerror(error)); return; } /* set complexity to 0 for arm devices */ #ifdef __arm__ opus_encoder_ctl(d->state, OPUS_SET_COMPLEXITY(0)); #endif error = opus_encoder_ctl(d->state, OPUS_SET_PACKET_LOSS_PERC(10)); if (error != OPUS_OK) { ms_error("Could not set default loss percentage to opus encoder: %s", opus_strerror(error)); } /* set the encoder parameters: VBR, IN_BAND_FEC, DTX and bitrate settings */ ms_opus_enc_set_vbr(f); ms_opus_enc_set_inbandfec(f); ms_opus_enc_set_dtx(f); /* if decoder prefers mono signal, force encoder to output mono signal */ if (d->stereo == 0) { error = opus_encoder_ctl(d->state, OPUS_SET_FORCE_CHANNELS(1)); if (error != OPUS_OK) { ms_error("could not force mono channel to opus encoder: %s", opus_strerror(error)); } } ms_filter_lock(f); if (d->ptime==-1) { // set ptime wasn't call, set default:20ms d->ptime = 20; } if (d->bitrate==-1) { // set bitrate wasn't call, compute it with the default network bitrate (36000) compute_max_bitrate(d, 0); } apply_max_bitrate(d); ms_filter_unlock(f); }
static void ms_opus_enc_preprocess(MSFilter *f) { int error; int opusComplexity = -1; const char *env = NULL; OpusEncData *d = (OpusEncData *)f->data; /* create the encoder */ d->state = opus_encoder_create(d->samplerate, d->channels, d->application, &error); if (error != OPUS_OK) { ms_error("Opus encoder creation failed: %s", opus_strerror(error)); return; } #ifndef MS2_WINDOWS_UNIVERSAL env = getenv("MS2_OPUS_COMPLEXITY"); #endif if (env != NULL) { opusComplexity = atoi(env); if (opusComplexity < -1) opusComplexity = -1; /*our default value*/ if (opusComplexity > 10) opusComplexity = 10; } if (opusComplexity == -1){ #if defined(__arm__) || defined(_M_ARM) int cpucount = ms_factory_get_cpu_count(f->factory); if (cpucount == 1){ opusComplexity = 0; /* set complexity to 0 for single processor arm devices */ }else if (cpucount == 2) { opusComplexity = 5; } #endif } if (opusComplexity != -1){ ms_message("Set Opus complexity to %d", opusComplexity); opus_encoder_ctl(d->state, OPUS_SET_COMPLEXITY(opusComplexity)); } /*otherwise we let opus with its default value, which is 9*/ error = opus_encoder_ctl(d->state, OPUS_SET_PACKET_LOSS_PERC(10)); if (error != OPUS_OK) { ms_error("Could not set default loss percentage to opus encoder: %s", opus_strerror(error)); } /* set the encoder parameters: VBR, IN_BAND_FEC, DTX and bitrate settings */ ms_opus_enc_set_vbr(f); ms_opus_enc_set_inbandfec(f); ms_opus_enc_set_dtx(f); /* if decoder prefers mono signal, force encoder to output mono signal */ if (d->stereo == 0) { error = opus_encoder_ctl(d->state, OPUS_SET_FORCE_CHANNELS(1)); if (error != OPUS_OK) { ms_error("could not force mono channel to opus encoder: %s", opus_strerror(error)); } if (d->channels == 2) ms_message("Opus encoder configured to encode mono despite it is feed with stereo."); }else if (d->channels == 2){ ms_message("Opus encoder configured to encode stereo."); } ms_filter_lock(f); // set bitrate wasn't call, compute it with the default network bitrate (36000) if (d->bitrate==-1) { compute_max_bitrate(d, 0); } apply_max_bitrate(d); ms_filter_unlock(f); }
int opus_encode_update(struct auenc_state **aesp, const struct aucodec *ac, struct auenc_param *param, const char *fmtp) { struct auenc_state *aes; struct opus_param prm, conf_prm; opus_int32 fch, vbr; const struct aucodec *auc = ac; (void)param; if (!aesp || !ac || !ac->ch) return EINVAL; debug("opus: encoder fmtp (%s)\n", fmtp); /* Save the incoming OPUS parameters from SDP offer */ if (str_isset(fmtp)) { opus_mirror_params(fmtp); } aes = *aesp; if (!aes) { const opus_int32 complex = 10; int opuserr; aes = mem_zalloc(sizeof(*aes), destructor); if (!aes) return ENOMEM; aes->ch = ac->ch; aes->enc = opus_encoder_create(ac->srate, ac->ch, /* this has big impact on cpu */ OPUS_APPLICATION_AUDIO, &opuserr); if (!aes->enc) { warning("opus: encoder create: %s\n", opus_strerror(opuserr)); mem_deref(aes); return ENOMEM; } (void)opus_encoder_ctl(aes->enc, OPUS_SET_COMPLEXITY(complex)); *aesp = aes; } prm.srate = 48000; prm.bitrate = OPUS_AUTO; prm.stereo = 1; prm.cbr = 0; prm.inband_fec = 0; prm.dtx = 0; opus_decode_fmtp(&prm, fmtp); conf_prm.bitrate = OPUS_AUTO; opus_decode_fmtp(&conf_prm, auc->fmtp); if ((prm.bitrate == OPUS_AUTO) || ((conf_prm.bitrate != OPUS_AUTO) && (conf_prm.bitrate < prm.bitrate))) prm.bitrate = conf_prm.bitrate; fch = prm.stereo ? OPUS_AUTO : 1; vbr = prm.cbr ? 0 : 1; (void)opus_encoder_ctl(aes->enc, OPUS_SET_MAX_BANDWIDTH(srate2bw(prm.srate))); (void)opus_encoder_ctl(aes->enc, OPUS_SET_BITRATE(prm.bitrate)); (void)opus_encoder_ctl(aes->enc, OPUS_SET_FORCE_CHANNELS(fch)); (void)opus_encoder_ctl(aes->enc, OPUS_SET_VBR(vbr)); (void)opus_encoder_ctl(aes->enc, OPUS_SET_INBAND_FEC(prm.inband_fec)); (void)opus_encoder_ctl(aes->enc, OPUS_SET_DTX(prm.dtx)); #if 0 { opus_int32 bw, complex; (void)opus_encoder_ctl(aes->enc, OPUS_GET_MAX_BANDWIDTH(&bw)); (void)opus_encoder_ctl(aes->enc, OPUS_GET_BITRATE(&prm.bitrate)); (void)opus_encoder_ctl(aes->enc, OPUS_GET_FORCE_CHANNELS(&fch)); (void)opus_encoder_ctl(aes->enc, OPUS_GET_VBR(&vbr)); (void)opus_encoder_ctl(aes->enc, OPUS_GET_INBAND_FEC(&prm.inband_fec)); (void)opus_encoder_ctl(aes->enc, OPUS_GET_DTX(&prm.dtx)); (void)opus_encoder_ctl(aes->enc, OPUS_GET_COMPLEXITY(&complex)); debug("opus: encode bw=%s bitrate=%i fch=%s " "vbr=%i fec=%i dtx=%i complex=%i\n", bwname(bw), prm.bitrate, chname(fch), vbr, prm.inband_fec, prm.dtx, complex); } #endif return 0; }