From 8f565277e3a9e814c12779f3d51f2527a081ef99 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Fri, 4 Feb 2011 15:33:28 -0600 Subject: [PATCH] add mod_opus --- build/modules.conf.in | 1 + conf/autoload_configs/modules.conf.xml | 1 + src/mod/codecs/mod_opus/Makefile | 28 ++++ src/mod/codecs/mod_opus/mod_opus.c | 209 +++++++++++++++++++++++++ 4 files changed, 239 insertions(+) create mode 100644 src/mod/codecs/mod_opus/Makefile create mode 100644 src/mod/codecs/mod_opus/mod_opus.c diff --git a/build/modules.conf.in b/build/modules.conf.in index 84a9e9a74d..8d3dfaa85a 100644 --- a/build/modules.conf.in +++ b/build/modules.conf.in @@ -48,6 +48,7 @@ codecs/mod_ilbc codecs/mod_speex codecs/mod_siren #codecs/mod_celt +#codecs/mod_opus #codecs/mod_sangoma_codec #codecs/mod_dahdi_codec #dialplans/mod_dialplan_directory diff --git a/conf/autoload_configs/modules.conf.xml b/conf/autoload_configs/modules.conf.xml index 2e7ebfabe5..8b4a4e7ea4 100644 --- a/conf/autoload_configs/modules.conf.xml +++ b/conf/autoload_configs/modules.conf.xml @@ -78,6 +78,7 @@ + diff --git a/src/mod/codecs/mod_opus/Makefile b/src/mod/codecs/mod_opus/Makefile new file mode 100644 index 0000000000..a34695678b --- /dev/null +++ b/src/mod/codecs/mod_opus/Makefile @@ -0,0 +1,28 @@ +BASE=../../../.. + +OPUS=opus-0.9.0 + +OPUS_DIR=$(switch_srcdir)/libs/$(OPUS) +OPUS_BUILDDIR=$(switch_builddir)/libs/$(OPUS) +LOCAL_CFLAGS=-I$(OPUS_DIR)/src -g -O2 + +IETF_LA=$(OPUS_BUILDDIR)/src/.libs/libietfcodec.a +CELT_LA=$(OPUS_BUILDDIR)/celt/libcelt/.libs/libcelt0.a +SILK_LA=$(OPUS_BUILDDIR)/silk/.libs/libSKP_SILK_SDK.a + +LOCAL_LIBADD=$(IETF_LA) $(CELT_LA) $(SILK_LA) -lm -lz + +include $(BASE)/build/modmake.rules + +$(OPUS_DIR): + $(GETLIB) $(OPUS).tar.gz + +$(OPUS_BUILDDIR)/Makefile: $(OPUS_DIR) + mkdir -p $(OPUS_BUILDDIR) + cd $(OPUS_BUILDDIR) && $(DEFAULT_VARS) $(OPUS_DIR)/configure --disable-shared --with-pic --srcdir=$(OPUS_DIR) + $(TOUCH_TARGET) + +$(IETF_LA): $(OPUS_BUILDDIR)/Makefile + cd $(OPUS_BUILDDIR) && $(MAKE) + $(TOUCH_TARGET) + diff --git a/src/mod/codecs/mod_opus/mod_opus.c b/src/mod/codecs/mod_opus/mod_opus.c new file mode 100644 index 0000000000..341254fe0c --- /dev/null +++ b/src/mod/codecs/mod_opus/mod_opus.c @@ -0,0 +1,209 @@ +/* + * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2005-2011, Anthony Minessale II + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Anthony Minessale II + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Brian K. West + * + * mod_opus.c -- The OPUS ultra-low delay audio codec (http://www.opus-codec.org/) + * + */ + +#include "switch.h" +#include "opus.h" + + +SWITCH_MODULE_LOAD_FUNCTION(mod_opus_load); +SWITCH_MODULE_DEFINITION(mod_opus, mod_opus_load, NULL, NULL); + +struct opus_context { + OpusEncoder *encoder_object; + OpusDecoder *decoder_object; + int frame_size; +}; + +static switch_status_t switch_opus_init(switch_codec_t *codec, switch_codec_flag_t flags, const switch_codec_settings_t *codec_settings) +{ + struct opus_context *context = NULL; + int encoding = (flags & SWITCH_CODEC_FLAG_ENCODE); + int decoding = (flags & SWITCH_CODEC_FLAG_DECODE); + + if (!(encoding || decoding) || (!(context = switch_core_alloc(codec->memory_pool, sizeof(*context))))) { + return SWITCH_STATUS_FALSE; + } + + context->frame_size = codec->implementation->samples_per_packet; + + if (encoding) { + /* come up with a way to specify these */ + int bitrate_bps = codec->implementation->bits_per_second; + int mode = MODE_HYBRID; + int use_vbr = 1; + int complexity = 10; + int use_inbandfec = 1; + int use_dtx = 1; + int bandwidth = BANDWIDTH_FULLBAND; + + context->encoder_object = opus_encoder_create(codec->implementation->actual_samples_per_second, codec->implementation->number_of_channels); + + opus_encoder_ctl(context->encoder_object, OPUS_SET_MODE(mode)); + opus_encoder_ctl(context->encoder_object, OPUS_SET_BITRATE(bitrate_bps)); + opus_encoder_ctl(context->encoder_object, OPUS_SET_BANDWIDTH(bandwidth)); + opus_encoder_ctl(context->encoder_object, OPUS_SET_VBR_FLAG(use_vbr)); + opus_encoder_ctl(context->encoder_object, OPUS_SET_COMPLEXITY(complexity)); + opus_encoder_ctl(context->encoder_object, OPUS_SET_INBAND_FEC_FLAG(use_inbandfec)); + opus_encoder_ctl(context->encoder_object, OPUS_SET_DTX_FLAG(use_dtx)); + + } + + if (decoding) { + context->decoder_object = opus_decoder_create(codec->implementation->actual_samples_per_second, codec->implementation->number_of_channels); + } + + codec->private_info = context; + + return SWITCH_STATUS_SUCCESS; +} + +static switch_status_t switch_opus_destroy(switch_codec_t *codec) +{ + codec->private_info = NULL; + return SWITCH_STATUS_SUCCESS; +} + +static switch_status_t switch_opus_encode(switch_codec_t *codec, + switch_codec_t *other_codec, + void *decoded_data, + uint32_t decoded_data_len, + uint32_t decoded_rate, void *encoded_data, uint32_t *encoded_data_len, uint32_t *encoded_rate, + unsigned int *flag) +{ + struct opus_context *context = codec->private_info; + int bytes = 0; + int len = (int) *encoded_data_len; + + if (!context) { + return SWITCH_STATUS_FALSE; + } + + if (len > 1275) len = 1275; + + bytes = opus_encode(context->encoder_object, (void *) decoded_data, decoded_data_len / 2, (unsigned char *) encoded_data, len); + + if (bytes > 0) { + *encoded_data_len = (uint32_t) bytes; + return SWITCH_STATUS_SUCCESS; + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Encoder Error!\n"); + return SWITCH_STATUS_GENERR; +} + +static switch_status_t switch_opus_decode(switch_codec_t *codec, + switch_codec_t *other_codec, + void *encoded_data, + uint32_t encoded_data_len, + uint32_t encoded_rate, void *decoded_data, uint32_t *decoded_data_len, uint32_t *decoded_rate, + unsigned int *flag) +{ + struct opus_context *context = codec->private_info; + int samples = 0; + + if (!context) { + return SWITCH_STATUS_FALSE; + } + + samples = opus_decode(context->decoder_object, encoded_data, encoded_data_len, decoded_data, *decoded_data_len); + + if (samples < 0) { + return SWITCH_STATUS_GENERR; + } + + *decoded_data_len = samples * 2; + + return SWITCH_STATUS_SUCCESS; +} + +SWITCH_MODULE_LOAD_FUNCTION(mod_opus_load) +{ + switch_codec_interface_t *codec_interface; + + /* connect my internal structure to the blank pointer passed to me */ + *module_interface = switch_loadable_module_create_module_interface(pool, modname); + + SWITCH_ADD_CODEC(codec_interface, "OPUS (BETA)"); + + switch_core_codec_add_implementation(pool, codec_interface, SWITCH_CODEC_TYPE_AUDIO, /* enumeration defining the type of the codec */ + 115, /* the IANA code number */ + "OPUS", /* the IANA code name */ + NULL, /* default fmtp to send (can be overridden by the init function) */ + 48000, /* samples transferred per second */ + 48000, /* actual samples transferred per second */ + 32000, /* bits transferred per second */ + 10000, /* number of microseconds per frame */ + 80, /* number of samples per frame */ + 960, /* number of bytes per frame decompressed */ + 0, /* number of bytes per frame compressed */ + 1, /* number of channels represented */ + 1, /* number of frames per network packet */ + switch_opus_init, /* function to initialize a codec handle using this implementation */ + switch_opus_encode, /* function to encode raw data into encoded data */ + switch_opus_decode, /* function to decode encoded data into raw data */ + switch_opus_destroy); /* deinitalize a codec handle using this implementation */ + + + switch_core_codec_add_implementation(pool, codec_interface, SWITCH_CODEC_TYPE_AUDIO, /* enumeration defining the type of the codec */ + 115, /* the IANA code number */ + "OPUS", /* the IANA code name */ + NULL, /* default fmtp to send (can be overridden by the init function) */ + 48000, /* samples transferred per second */ + 48000, /* actual samples transferred per second */ + 32000, /* bits transferred per second */ + 20000, /* number of microseconds per frame */ + 160, /* number of samples per frame */ + 1920, /* number of bytes per frame decompressed */ + 0, /* number of bytes per frame compressed */ + 1, /* number of channels represented */ + 1, /* number of frames per network packet */ + switch_opus_init, /* function to initialize a codec handle using this implementation */ + switch_opus_encode, /* function to encode raw data into encoded data */ + switch_opus_decode, /* function to decode encoded data into raw data */ + switch_opus_destroy); /* deinitalize a codec handle using this implementation */ + + + + + /* indicate that the module should continue to be loaded */ + return SWITCH_STATUS_SUCCESS; +} + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4: + */