2006-08-30 04:04:47 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
* 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 <anthmct@yahoo.com>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
* Brian K. West <brian.west@mac.com>
|
|
|
|
*
|
2006-08-30 05:01:52 +00:00
|
|
|
* The amr codec itself is not distributed with this module.
|
|
|
|
*
|
2006-08-30 04:04:47 +00:00
|
|
|
* mod_amr.c -- GSM-AMR Codec Module
|
|
|
|
*
|
2007-03-29 22:31:56 +00:00
|
|
|
*/
|
2008-02-01 06:22:13 +00:00
|
|
|
|
2006-08-30 04:04:47 +00:00
|
|
|
#include "switch.h"
|
2007-06-13 17:06:10 +00:00
|
|
|
|
|
|
|
SWITCH_MODULE_LOAD_FUNCTION(mod_amr_load);
|
|
|
|
SWITCH_MODULE_DEFINITION(mod_amr, mod_amr_load, NULL, NULL);
|
2007-02-23 21:58:11 +00:00
|
|
|
|
|
|
|
#ifndef AMR_PASSTHROUGH
|
2008-01-13 18:39:51 +00:00
|
|
|
#include "interf_enc.h"
|
|
|
|
#include "interf_dec.h"
|
2006-08-30 04:04:47 +00:00
|
|
|
|
2006-08-30 05:16:12 +00:00
|
|
|
/*
|
|
|
|
* Check section 8.1 of rfc3267 for possible sdp options.
|
|
|
|
*
|
|
|
|
* SDP Example
|
|
|
|
*
|
|
|
|
* a=fmtp:97 mode-set=0,2,5,7; mode-change-period=2; mode-change-neighbor=1
|
|
|
|
*
|
|
|
|
* Class A total speech
|
|
|
|
* Index Mode bits bits
|
|
|
|
* ----------------------------------------
|
|
|
|
* 0 AMR 4.75 42 95
|
|
|
|
* 1 AMR 5.15 49 103
|
|
|
|
* 2 AMR 5.9 55 118
|
|
|
|
* 3 AMR 6.7 58 134
|
|
|
|
* 4 AMR 7.4 61 148
|
|
|
|
* 5 AMR 7.95 75 159
|
|
|
|
* 6 AMR 10.2 65 204
|
|
|
|
* 7 AMR 12.2 81 244
|
|
|
|
* 8 AMR SID 39 39
|
|
|
|
*
|
|
|
|
* Table 1. The number of class A bits for the AMR codec.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-10-09 22:46:51 +00:00
|
|
|
typedef enum {
|
|
|
|
AMR_OPT_OCTET_ALIGN = (1 << 0),
|
|
|
|
AMR_OPT_CRC = (1 << 1),
|
|
|
|
AMR_OPT_MODE_CHANGE_NEIGHBOR = (1 << 2),
|
|
|
|
AMR_OPT_ROBUST_SORTING = (1 << 3),
|
|
|
|
AMR_OPT_INTERLEAVING = (1 << 4)
|
|
|
|
} amr_flag_t;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
GENERIC_PARAMETER_AMR_MAXAL_SDUFRAMES = 0,
|
|
|
|
GENERIC_PARAMETER_AMR_BITRATE,
|
|
|
|
GENERIC_PARAMETER_AMR_GSMAMRCOMFORTNOISE,
|
|
|
|
GENERIC_PARAMETER_AMR_GSMEFRCOMFORTNOISE,
|
|
|
|
GENERIC_PARAMETER_AMR_IS_641COMFORTNOISE,
|
|
|
|
GENERIC_PARAMETER_AMR_PDCEFRCOMFORTNOISE
|
|
|
|
} amr_param_t;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
AMR_BITRATE_475 = 0,
|
|
|
|
AMR_BITRATE_515,
|
|
|
|
AMR_BITRATE_590,
|
|
|
|
AMR_BITRATE_670,
|
|
|
|
AMR_BITRATE_740,
|
|
|
|
AMR_BITRATE_795,
|
|
|
|
AMR_BITRATE_1020,
|
|
|
|
AMR_BITRATE_1220
|
|
|
|
} amr_bitrate_t;
|
|
|
|
|
2006-08-30 04:04:47 +00:00
|
|
|
struct amr_context {
|
|
|
|
void *encoder_state;
|
2006-08-31 00:15:31 +00:00
|
|
|
void *decoder_state;
|
2006-10-09 22:46:51 +00:00
|
|
|
switch_byte_t enc_modes;
|
|
|
|
switch_byte_t enc_mode;
|
|
|
|
uint32_t change_period;
|
|
|
|
switch_byte_t max_ptime;
|
|
|
|
switch_byte_t ptime;
|
|
|
|
switch_byte_t channels;
|
|
|
|
switch_byte_t flags;
|
2006-08-30 04:04:47 +00:00
|
|
|
};
|
|
|
|
|
2006-10-09 22:46:51 +00:00
|
|
|
#define AMR_DEFAULT_BITRATE AMR_BITRATE_1220
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
switch_byte_t default_bitrate;
|
|
|
|
} globals;
|
|
|
|
|
2007-02-23 21:58:11 +00:00
|
|
|
#endif
|
2006-08-30 04:04:47 +00:00
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
static switch_status_t switch_amr_init(switch_codec_t *codec, switch_codec_flag_t flags, const switch_codec_settings_t *codec_settings)
|
2006-08-30 04:04:47 +00:00
|
|
|
{
|
2007-02-23 21:58:11 +00:00
|
|
|
#ifdef AMR_PASSTHROUGH
|
|
|
|
codec->flags |= SWITCH_CODEC_FLAG_PASSTHROUGH;
|
2007-03-29 22:31:56 +00:00
|
|
|
if (codec->fmtp_in) {
|
|
|
|
codec->fmtp_out = switch_core_strdup(codec->memory_pool, codec->fmtp_in);
|
|
|
|
}
|
2007-02-23 21:58:11 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
#else
|
2006-08-30 04:04:47 +00:00
|
|
|
struct amr_context *context = NULL;
|
|
|
|
int encoding, decoding;
|
2006-10-09 22:46:51 +00:00
|
|
|
int x, i, argc;
|
2006-10-09 17:11:39 +00:00
|
|
|
char *argv[10];
|
2006-10-09 22:46:51 +00:00
|
|
|
char fmtptmp[128];
|
2006-08-30 04:04:47 +00:00
|
|
|
|
|
|
|
encoding = (flags & SWITCH_CODEC_FLAG_ENCODE);
|
|
|
|
decoding = (flags & SWITCH_CODEC_FLAG_DECODE);
|
|
|
|
|
|
|
|
if (!(encoding || decoding) || (!(context = switch_core_alloc(codec->memory_pool, sizeof(struct amr_context))))) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
} else {
|
|
|
|
|
2006-10-09 17:11:39 +00:00
|
|
|
if (codec->fmtp_in) {
|
|
|
|
argc = switch_separate_string(codec->fmtp_in, ';', argv, (sizeof(argv) / sizeof(argv[0])));
|
2007-03-29 22:31:56 +00:00
|
|
|
for (x = 0; x < argc; x++) {
|
2006-10-09 17:11:39 +00:00
|
|
|
char *data = argv[x];
|
|
|
|
char *arg;
|
2007-03-29 22:31:56 +00:00
|
|
|
while (*data && *data == ' ') {
|
2006-10-09 17:11:39 +00:00
|
|
|
data++;
|
|
|
|
}
|
|
|
|
if ((arg = strchr(data, '='))) {
|
|
|
|
*arg++ = '\0';
|
2006-10-09 22:46:51 +00:00
|
|
|
if (!strcasecmp(data, "octet-align")) {
|
|
|
|
if (atoi(arg)) {
|
|
|
|
switch_set_flag(context, AMR_OPT_OCTET_ALIGN);
|
|
|
|
}
|
|
|
|
} else if (!strcasecmp(data, "mode-change-neighbor")) {
|
|
|
|
if (atoi(arg)) {
|
|
|
|
switch_set_flag(context, AMR_OPT_MODE_CHANGE_NEIGHBOR);
|
|
|
|
}
|
|
|
|
} else if (!strcasecmp(data, "crc")) {
|
|
|
|
if (atoi(arg)) {
|
|
|
|
switch_set_flag(context, AMR_OPT_CRC);
|
|
|
|
}
|
|
|
|
} else if (!strcasecmp(data, "robust-sorting")) {
|
|
|
|
if (atoi(arg)) {
|
|
|
|
switch_set_flag(context, AMR_OPT_ROBUST_SORTING);
|
|
|
|
}
|
|
|
|
} else if (!strcasecmp(data, "interveaving")) {
|
|
|
|
if (atoi(arg)) {
|
|
|
|
switch_set_flag(context, AMR_OPT_INTERLEAVING);
|
|
|
|
}
|
|
|
|
} else if (!strcasecmp(data, "mode-change-period")) {
|
|
|
|
context->change_period = atoi(arg);
|
|
|
|
} else if (!strcasecmp(data, "ptime")) {
|
2007-03-29 22:31:56 +00:00
|
|
|
context->ptime = (switch_byte_t) atoi(arg);
|
2006-10-09 22:46:51 +00:00
|
|
|
} else if (!strcasecmp(data, "channels")) {
|
2007-03-29 22:31:56 +00:00
|
|
|
context->channels = (switch_byte_t) atoi(arg);
|
2006-10-09 22:46:51 +00:00
|
|
|
} else if (!strcasecmp(data, "maxptime")) {
|
2007-03-29 22:31:56 +00:00
|
|
|
context->max_ptime = (switch_byte_t) atoi(arg);
|
2006-10-09 22:46:51 +00:00
|
|
|
} else if (!strcasecmp(data, "mode-set")) {
|
|
|
|
int y, m_argc;
|
|
|
|
char *m_argv[7];
|
|
|
|
m_argc = switch_separate_string(arg, ',', m_argv, (sizeof(m_argv) / sizeof(m_argv[0])));
|
2007-03-29 22:31:56 +00:00
|
|
|
for (y = 0; y < m_argc; y++) {
|
2006-10-09 22:46:51 +00:00
|
|
|
context->enc_modes |= (1 << atoi(m_argv[y]));
|
|
|
|
}
|
|
|
|
}
|
2006-10-09 17:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-09 22:46:51 +00:00
|
|
|
if (context->enc_modes) {
|
|
|
|
for (i = 7; i > -1; i++) {
|
|
|
|
if (context->enc_modes & (1 << i)) {
|
2007-03-29 22:31:56 +00:00
|
|
|
context->enc_mode = (switch_byte_t) i;
|
2006-10-09 22:46:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!context->enc_mode) {
|
|
|
|
context->enc_mode = globals.default_bitrate;
|
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2007-12-12 21:53:32 +00:00
|
|
|
switch_snprintf(fmtptmp, sizeof(fmtptmp), "octet-align=%d; mode-set=%d", switch_test_flag(context, AMR_OPT_OCTET_ALIGN) ? 1 : 0, context->enc_mode);
|
2006-10-09 22:46:51 +00:00
|
|
|
codec->fmtp_out = switch_core_strdup(codec->memory_pool, fmtptmp);
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-10-09 22:46:51 +00:00
|
|
|
context->enc_mode = AMR_DEFAULT_BITRATE;
|
2006-08-31 00:15:31 +00:00
|
|
|
context->encoder_state = NULL;
|
|
|
|
context->decoder_state = NULL;
|
2006-08-30 04:04:47 +00:00
|
|
|
|
|
|
|
if (encoding) {
|
2006-10-10 02:16:29 +00:00
|
|
|
context->encoder_state = Encoder_Interface_init(1);
|
2006-08-30 04:04:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (decoding) {
|
2006-08-31 00:15:31 +00:00
|
|
|
context->decoder_state = Decoder_Interface_init();
|
2006-08-30 04:04:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
codec->private_info = context;
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
2007-02-23 21:58:11 +00:00
|
|
|
#endif
|
2006-08-30 04:04:47 +00:00
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
static switch_status_t switch_amr_destroy(switch_codec_t *codec)
|
2006-08-30 04:04:47 +00:00
|
|
|
{
|
2007-02-23 21:58:11 +00:00
|
|
|
#ifndef AMR_PASSTHROUGH
|
2006-08-30 04:04:47 +00:00
|
|
|
struct amr_context *context = codec->private_info;
|
2006-08-31 00:15:31 +00:00
|
|
|
|
|
|
|
if (context->encoder_state) {
|
|
|
|
Encoder_Interface_exit(context->encoder_state);
|
|
|
|
}
|
|
|
|
if (context->decoder_state) {
|
|
|
|
Decoder_Interface_exit(context->decoder_state);
|
|
|
|
}
|
2006-08-30 04:04:47 +00:00
|
|
|
codec->private_info = NULL;
|
2007-02-23 21:58:11 +00:00
|
|
|
#endif
|
2006-08-30 04:04:47 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
static switch_status_t switch_amr_encode(switch_codec_t *codec,
|
|
|
|
switch_codec_t *other_codec,
|
|
|
|
void *decoded_data,
|
|
|
|
uint32_t decoded_data_len,
|
2007-03-30 00:15:25 +00:00
|
|
|
uint32_t decoded_rate, void *encoded_data, uint32_t * encoded_data_len, uint32_t * encoded_rate,
|
|
|
|
unsigned int *flag)
|
2006-08-30 04:04:47 +00:00
|
|
|
{
|
2007-02-23 21:58:11 +00:00
|
|
|
#ifdef AMR_PASSTHROUGH
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "This codec is only usable in passthrough mode!\n");
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
#else
|
2006-08-30 04:04:47 +00:00
|
|
|
struct amr_context *context = codec->private_info;
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-08-30 04:04:47 +00:00
|
|
|
if (!context) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
*encoded_data_len = Encoder_Interface_Encode(context->encoder_state, context->enc_mode, (int16_t *) decoded_data, (switch_byte_t *) encoded_data, 0);
|
2006-08-30 04:04:47 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2007-02-23 21:58:11 +00:00
|
|
|
#endif
|
2006-08-30 04:04:47 +00:00
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
static switch_status_t switch_amr_decode(switch_codec_t *codec,
|
|
|
|
switch_codec_t *other_codec,
|
|
|
|
void *encoded_data,
|
|
|
|
uint32_t encoded_data_len,
|
2007-03-30 00:15:25 +00:00
|
|
|
uint32_t encoded_rate, void *decoded_data, uint32_t * decoded_data_len, uint32_t * decoded_rate,
|
|
|
|
unsigned int *flag)
|
2006-08-30 04:04:47 +00:00
|
|
|
{
|
2007-02-23 21:58:11 +00:00
|
|
|
#ifdef AMR_PASSTHROUGH
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "This codec is only usable in passthrough mode!\n");
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
#else
|
2006-08-30 04:04:47 +00:00
|
|
|
struct amr_context *context = codec->private_info;
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
Decoder_Interface_Decode(context->decoder_state, (unsigned char *) encoded_data, (int16_t *) decoded_data, 0);
|
2006-09-05 22:09:56 +00:00
|
|
|
*decoded_data_len = codec->implementation->bytes_per_frame;
|
2006-10-09 22:46:51 +00:00
|
|
|
|
2006-08-30 04:04:47 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2007-02-23 21:58:11 +00:00
|
|
|
#endif
|
2006-08-30 04:04:47 +00:00
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
/* Registration */
|
2007-06-13 17:06:10 +00:00
|
|
|
SWITCH_MODULE_LOAD_FUNCTION(mod_amr_load)
|
2006-08-30 04:04:47 +00:00
|
|
|
{
|
2007-06-20 08:41:55 +00:00
|
|
|
switch_codec_interface_t *codec_interface;
|
2007-02-23 21:58:11 +00:00
|
|
|
#ifndef AMR_PASSTHROUGH
|
2006-10-09 22:46:51 +00:00
|
|
|
char *cf = "amr.conf";
|
|
|
|
switch_xml_t cfg, xml, settings, param;
|
|
|
|
|
|
|
|
memset(&globals, 0, sizeof(globals));
|
|
|
|
globals.default_bitrate = AMR_DEFAULT_BITRATE;
|
|
|
|
|
|
|
|
if ((xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
|
|
|
|
if ((settings = switch_xml_child(cfg, "settings"))) {
|
|
|
|
for (param = switch_xml_child(settings, "param"); param; param = param->next) {
|
|
|
|
char *var = (char *) switch_xml_attr_soft(param, "name");
|
|
|
|
char *val = (char *) switch_xml_attr_soft(param, "value");
|
|
|
|
if (!strcasecmp(var, "default-bitrate")) {
|
2007-03-29 22:31:56 +00:00
|
|
|
globals.default_bitrate = (switch_byte_t) atoi(val);
|
2006-10-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-02-23 21:58:11 +00:00
|
|
|
#endif
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
/* connect my internal structure to the blank pointer passed to me */
|
2007-06-20 08:41:55 +00:00
|
|
|
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
|
2006-08-30 04:04:47 +00:00
|
|
|
|
2008-02-01 06:22:13 +00:00
|
|
|
SWITCH_ADD_CODEC(codec_interface, "GSM-AMR");
|
|
|
|
switch_core_codec_add_implementation(pool, codec_interface,
|
|
|
|
SWITCH_CODEC_TYPE_AUDIO, 96, "AMR", "octet-align=0", 8000, 8000, 12200,
|
|
|
|
20000, 160, 320, 0, 1, 1, 1,
|
|
|
|
switch_amr_init, switch_amr_encode, switch_amr_decode, switch_amr_destroy);
|
2007-03-29 22:31:56 +00:00
|
|
|
/* indicate that the module should continue to be loaded */
|
2006-08-30 04:04:47 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
2008-01-27 02:54:41 +00:00
|
|
|
|
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
2008-01-27 20:01:02 +00:00
|
|
|
* indent-tabs-mode:nil
|
2008-01-27 02:54:41 +00:00
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
|
|
|
|
*/
|