diff options
Diffstat (limited to 'pjmedia/src/pjmedia-codec')
-rw-r--r-- | pjmedia/src/pjmedia-codec/audio_codecs.c | 15 | ||||
-rw-r--r-- | pjmedia/src/pjmedia-codec/ffmpeg_vid_codecs.c | 17 | ||||
-rw-r--r-- | pjmedia/src/pjmedia-codec/opencore_amr.c (renamed from pjmedia/src/pjmedia-codec/opencore_amrnb.c) | 402 | ||||
-rw-r--r-- | pjmedia/src/pjmedia-codec/silk.c | 953 |
4 files changed, 1276 insertions, 111 deletions
diff --git a/pjmedia/src/pjmedia-codec/audio_codecs.c b/pjmedia/src/pjmedia-codec/audio_codecs.c index caf63e5..0c21821 100644 --- a/pjmedia/src/pjmedia-codec/audio_codecs.c +++ b/pjmedia/src/pjmedia-codec/audio_codecs.c @@ -1,4 +1,4 @@ -/* $Id: audio_codecs.c 3910 2011-12-15 06:34:25Z nanang $ */ +/* $Id: audio_codecs.c 4335 2013-01-29 08:09:15Z ming $ */ /* * Copyright (C) 2011-2011 Teluu Inc. (http://www.teluu.com) * @@ -107,9 +107,16 @@ pjmedia_codec_register_audio_codecs(pjmedia_endpt *endpt, return status; #endif /* PJMEDIA_HAS_L16_CODEC */ -#if PJMEDIA_HAS_OPENCORE_AMRNB_CODEC - /* Register OpenCORE AMR-NB */ - status = pjmedia_codec_opencore_amrnb_init(endpt); +#if PJMEDIA_HAS_OPENCORE_AMRNB_CODEC || PJMEDIA_HAS_OPENCORE_AMRWB_CODEC + /* Register OpenCORE AMR */ + status = pjmedia_codec_opencore_amr_init(endpt, 0); + if (status != PJ_SUCCESS) + return status; +#endif + +#if PJMEDIA_HAS_SILK_CODEC + /* Register SILK */ + status = pjmedia_codec_silk_init(endpt); if (status != PJ_SUCCESS) return status; #endif diff --git a/pjmedia/src/pjmedia-codec/ffmpeg_vid_codecs.c b/pjmedia/src/pjmedia-codec/ffmpeg_vid_codecs.c index d0e87ef..a71a2cc 100644 --- a/pjmedia/src/pjmedia-codec/ffmpeg_vid_codecs.c +++ b/pjmedia/src/pjmedia-codec/ffmpeg_vid_codecs.c @@ -1,4 +1,4 @@ -/* $Id: ffmpeg_vid_codecs.c 4089 2012-04-26 07:27:06Z nanang $ */ +/* $Id: ffmpeg_vid_codecs.c 4311 2012-12-20 06:45:09Z nanang $ */ /* * Copyright (C) 2010-2011 Teluu Inc. (http://www.teluu.com) * @@ -62,11 +62,16 @@ #endif #if LIBAVCODEC_VER_AT_LEAST(53,61) -/* Not sure when AVCodec::encode2 is introduced. It appears in - * libavcodec 53.61 where some codecs actually still use AVCodec::encode - * (e.g: H263, H264). - */ -# define AVCODEC_HAS_ENCODE(c) (c->encode || c->encode2) +# if LIBAVCODEC_VER_AT_LEAST(54,59) + /* Not sure when AVCodec::encode is obsoleted/removed. */ +# define AVCODEC_HAS_ENCODE(c) (c->encode2) +# else + /* Not sure when AVCodec::encode2 is introduced. It appears in + * libavcodec 53.61 where some codecs actually still use AVCodec::encode + * (e.g: H263, H264). + */ +# define AVCODEC_HAS_ENCODE(c) (c->encode || c->encode2) +# endif # define AV_OPT_SET(obj,name,val,opt) (av_opt_set(obj,name,val,opt)==0) # define AV_OPT_SET_INT(obj,name,val) (av_opt_set_int(obj,name,val,0)==0) #else diff --git a/pjmedia/src/pjmedia-codec/opencore_amrnb.c b/pjmedia/src/pjmedia-codec/opencore_amr.c index 9b6daa9..ccfcc2e 100644 --- a/pjmedia/src/pjmedia-codec/opencore_amrnb.c +++ b/pjmedia/src/pjmedia-codec/opencore_amr.c @@ -1,6 +1,6 @@ -/* $Id: opencore_amrnb.c 3939 2012-01-10 05:38:40Z nanang $ */ +/* $Id: opencore_amr.c 4348 2013-02-14 02:00:13Z ming $ */ /* - * Copyright (C) 2011 Teluu Inc. (http://www.teluu.com) + * Copyright (C) 2011-2013 Teluu Inc. (http://www.teluu.com) * Copyright (C) 2011 Dan Arrhenius <dan@keystream.se> * * This program is free software; you can redistribute it and/or modify @@ -19,7 +19,7 @@ */ /* - * AMR-NB codec implementation with OpenCORE AMRNB library + * AMR codec implementation with OpenCORE AMR library */ #include <pjmedia-codec/g722.h> #include <pjmedia-codec/amr_sdp_match.h> @@ -38,13 +38,30 @@ #if defined(PJMEDIA_HAS_OPENCORE_AMRNB_CODEC) && \ (PJMEDIA_HAS_OPENCORE_AMRNB_CODEC != 0) +#define USE_AMRNB +#endif + +#if defined(PJMEDIA_HAS_OPENCORE_AMRWB_CODEC) && \ + (PJMEDIA_HAS_OPENCORE_AMRWB_CODEC != 0) +#define USE_AMRWB +#endif +#if defined(USE_AMRNB) || defined(USE_AMRWB) + +#ifdef USE_AMRNB #include <opencore-amrnb/interf_enc.h> #include <opencore-amrnb/interf_dec.h> +#endif + +#ifdef USE_AMRWB +#include <vo-amrwbenc/enc_if.h> +#include <opencore-amrwb/dec_if.h> +#endif + #include <pjmedia-codec/amr_helper.h> -#include <pjmedia-codec/opencore_amrnb.h> +#include <pjmedia-codec/opencore_amr.h> -#define THIS_FILE "opencore_amrnb.c" +#define THIS_FILE "opencore_amr.c" /* Tracing */ #define PJ_TRACE 0 @@ -58,9 +75,10 @@ /* Use PJMEDIA PLC */ #define USE_PJMEDIA_PLC 1 +#define FRAME_LENGTH_MS 20 -/* Prototypes for AMR-NB factory */ +/* Prototypes for AMR factory */ static pj_status_t amr_test_alloc(pjmedia_codec_factory *factory, const pjmedia_codec_info *id ); static pj_status_t amr_default_attr(pjmedia_codec_factory *factory, @@ -75,7 +93,7 @@ static pj_status_t amr_alloc_codec(pjmedia_codec_factory *factory, static pj_status_t amr_dealloc_codec(pjmedia_codec_factory *factory, pjmedia_codec *codec ); -/* Prototypes for AMR-NB implementation. */ +/* Prototypes for AMR implementation. */ static pj_status_t amr_codec_init(pjmedia_codec *codec, pj_pool_t *pool ); static pj_status_t amr_codec_open(pjmedia_codec *codec, @@ -103,7 +121,7 @@ static pj_status_t amr_codec_recover(pjmedia_codec *codec, -/* Definition for AMR-NB codec operations. */ +/* Definition for AMR codec operations. */ static pjmedia_codec_op amr_op = { &amr_codec_init, @@ -116,7 +134,7 @@ static pjmedia_codec_op amr_op = &amr_codec_recover }; -/* Definition for AMR-NB codec factory operations. */ +/* Definition for AMR codec factory operations. */ static pjmedia_codec_factory_op amr_factory_op = { &amr_test_alloc, @@ -124,23 +142,25 @@ static pjmedia_codec_factory_op amr_factory_op = &amr_enum_codecs, &amr_alloc_codec, &amr_dealloc_codec, - &pjmedia_codec_opencore_amrnb_deinit + &pjmedia_codec_opencore_amr_deinit }; -/* AMR-NB factory */ +/* AMR factory */ static struct amr_codec_factory { pjmedia_codec_factory base; pjmedia_endpt *endpt; pj_pool_t *pool; + pj_bool_t init[2]; } amr_codec_factory; -/* AMR-NB codec private data. */ +/* AMR codec private data. */ struct amr_data { pj_pool_t *pool; + unsigned clock_rate; void *encoder; void *decoder; pj_bool_t plc_enabled; @@ -154,18 +174,38 @@ struct amr_data pj_timestamp last_tx; }; -static pjmedia_codec_amrnb_config def_config = +/* Index for AMR tables. */ +enum { + IDX_AMR_NB, /* Index for narrowband. */ + IDX_AMR_WB /* Index for wideband. */ +}; + +static pjmedia_codec_amr_config def_config[2] = +{{ /* AMR-NB */ PJ_FALSE, /* octet align */ 5900 /* bitrate */ -}; + }, + { /* AMR-WB */ + PJ_FALSE, /* octet align */ + 12650 /* bitrate */ + }}; +static const pj_uint16_t* amr_bitrates[2] = + {pjmedia_codec_amrnb_bitrates, pjmedia_codec_amrwb_bitrates}; + +static const unsigned amr_bitrates_size[2] = +{ + PJ_ARRAY_SIZE(pjmedia_codec_amrnb_bitrates), + PJ_ARRAY_SIZE(pjmedia_codec_amrwb_bitrates) +}; /* - * Initialize and register AMR-NB codec factory to pjmedia endpoint. + * Initialize and register AMR codec factory to pjmedia endpoint. */ -PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_init( pjmedia_endpt *endpt ) +PJ_DEF(pj_status_t) pjmedia_codec_opencore_amr_init( pjmedia_endpt *endpt, + unsigned options) { pjmedia_codec_mgr *codec_mgr; pj_str_t codec_name; @@ -174,12 +214,22 @@ PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_init( pjmedia_endpt *endpt ) if (amr_codec_factory.pool != NULL) return PJ_SUCCESS; - /* Create AMR-NB codec factory. */ + /* Create AMR codec factory. */ amr_codec_factory.base.op = &amr_factory_op; amr_codec_factory.base.factory_data = NULL; amr_codec_factory.endpt = endpt; +#ifdef USE_AMRNB + amr_codec_factory.init[IDX_AMR_NB] = ((options & PJMEDIA_AMR_NO_NB) == 0); +#else + amr_codec_factory.init[IDX_AMR_NB] = PJ_FALSE; +#endif +#ifdef USE_AMRWB + amr_codec_factory.init[IDX_AMR_WB] = ((options & PJMEDIA_AMR_NO_WB) == 0); +#else + amr_codec_factory.init[IDX_AMR_WB] = PJ_FALSE; +#endif - amr_codec_factory.pool = pjmedia_endpt_create_pool(endpt, "amrnb", 1000, + amr_codec_factory.pool = pjmedia_endpt_create_pool(endpt, "amr", 1000, 1000); if (!amr_codec_factory.pool) return PJ_ENOMEM; @@ -214,16 +264,30 @@ on_error: return status; } +PJ_DEF(pj_status_t) +pjmedia_codec_opencore_amr_init_default( pjmedia_endpt *endpt ) +{ + return pjmedia_codec_opencore_amr_init(endpt, 0); +} + +PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_init( pjmedia_endpt *endpt ) +{ + return pjmedia_codec_opencore_amr_init(endpt, PJMEDIA_AMR_NO_WB); +} + /* - * Unregister AMR-NB codec factory from pjmedia endpoint and deinitialize - * the AMR-NB codec library. + * Unregister AMR codec factory from pjmedia endpoint and deinitialize + * the AMR codec library. */ -PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_deinit(void) +PJ_DEF(pj_status_t) pjmedia_codec_opencore_amr_deinit(void) { pjmedia_codec_mgr *codec_mgr; pj_status_t status; + amr_codec_factory.init[IDX_AMR_NB] = PJ_FALSE; + amr_codec_factory.init[IDX_AMR_WB] = PJ_FALSE; + if (amr_codec_factory.pool == NULL) return PJ_SUCCESS; @@ -235,7 +299,7 @@ PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_deinit(void) return PJ_EINVALIDOP; } - /* Unregister AMR-NB codec factory. */ + /* Unregister AMR codec factory. */ status = pjmedia_codec_mgr_unregister_factory(codec_mgr, &amr_codec_factory.base); @@ -246,50 +310,93 @@ PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_deinit(void) return status; } +PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_deinit(void) +{ + if (amr_codec_factory.init[IDX_AMR_NB] && + amr_codec_factory.init[IDX_AMR_WB]) + { + PJ_LOG(4, (THIS_FILE, "Should call " + "pjmedia_codec_opencore_amr_deinit() instead")); + + return PJ_EINVALIDOP; + } + + return pjmedia_codec_opencore_amr_deinit(); +} -PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_set_config( - const pjmedia_codec_amrnb_config *config) +static pj_status_t +amr_set_config(unsigned idx, const pjmedia_codec_amr_config *config) { unsigned nbitrates; - - def_config = *config; + def_config[idx] = *config; /* Normalize bitrate. */ - nbitrates = PJ_ARRAY_SIZE(pjmedia_codec_amrnb_bitrates); - if (def_config.bitrate < pjmedia_codec_amrnb_bitrates[0]) - def_config.bitrate = pjmedia_codec_amrnb_bitrates[0]; - else if (def_config.bitrate > pjmedia_codec_amrnb_bitrates[nbitrates-1]) - def_config.bitrate = pjmedia_codec_amrnb_bitrates[nbitrates-1]; - else + nbitrates = amr_bitrates_size[idx]; + if (def_config[idx].bitrate < amr_bitrates[idx][0]) { + def_config[idx].bitrate = amr_bitrates[idx][0]; + } else if (def_config[idx].bitrate > amr_bitrates[idx][nbitrates-1]) { + def_config[idx].bitrate = amr_bitrates[idx][nbitrates-1]; + } else { unsigned i; for (i = 0; i < nbitrates; ++i) { - if (def_config.bitrate <= pjmedia_codec_amrnb_bitrates[i]) + if (def_config[idx].bitrate <= amr_bitrates[idx][i]) break; } - def_config.bitrate = pjmedia_codec_amrnb_bitrates[i]; + def_config[idx].bitrate = amr_bitrates[idx][i]; } return PJ_SUCCESS; } -/* - * Check if factory can allocate the specified codec. +PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_set_config( + const pjmedia_codec_amrnb_config *config) +{ + return amr_set_config(IDX_AMR_NB, (const pjmedia_codec_amr_config *)config); +} + +PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrwb_set_config( + const pjmedia_codec_amrwb_config *config) +{ + return amr_set_config(IDX_AMR_WB, (const pjmedia_codec_amr_config *)config); +} + +/* + * Check if factory can allocate the specified codec. */ static pj_status_t amr_test_alloc( pjmedia_codec_factory *factory, const pjmedia_codec_info *info ) { + const pj_str_t amr_tag = { "AMR", 3}; + const pj_str_t amrwb_tag = { "AMR-WB", 6}; PJ_UNUSED_ARG(factory); + /* Type MUST be audio. */ + if (info->type != PJMEDIA_TYPE_AUDIO) + return PJMEDIA_CODEC_EUNSUP; + /* Check payload type. */ - if (info->pt != PJMEDIA_RTP_PT_AMR) + if (info->pt != PJMEDIA_RTP_PT_AMR && info->pt != PJMEDIA_RTP_PT_AMRWB) return PJMEDIA_CODEC_EUNSUP; + + /* Check encoding name. */ + if (pj_stricmp(&info->encoding_name, &amr_tag) != 0 && + pj_stricmp(&info->encoding_name, &amrwb_tag) != 0) + { + return PJMEDIA_CODEC_EUNSUP; + } + + /* Check clock-rate */ + if ((info->clock_rate == 8000 && amr_codec_factory.init[IDX_AMR_NB]) || + (info->clock_rate == 16000 && amr_codec_factory.init[IDX_AMR_WB])) + { + return PJ_SUCCESS; + } - /* Ignore the rest, since it's static payload type. */ - - return PJ_SUCCESS; + /* Unsupported or disabled. */ + return PJMEDIA_CODEC_EUNSUP; } /* @@ -299,23 +406,25 @@ static pj_status_t amr_default_attr( pjmedia_codec_factory *factory, const pjmedia_codec_info *id, pjmedia_codec_param *attr ) { + unsigned idx; + PJ_UNUSED_ARG(factory); - PJ_UNUSED_ARG(id); + idx = (id->clock_rate <= 8000? IDX_AMR_NB: IDX_AMR_WB); pj_bzero(attr, sizeof(pjmedia_codec_param)); - attr->info.clock_rate = 8000; + attr->info.clock_rate = (id->clock_rate <= 8000? 8000: 16000); attr->info.channel_cnt = 1; - attr->info.avg_bps = def_config.bitrate; - attr->info.max_bps = pjmedia_codec_amrnb_bitrates[7]; + attr->info.avg_bps = def_config[idx].bitrate; + attr->info.max_bps = amr_bitrates[idx][amr_bitrates_size[idx]-1]; attr->info.pcm_bits_per_sample = 16; attr->info.frm_ptime = 20; - attr->info.pt = PJMEDIA_RTP_PT_AMR; + attr->info.pt = (pj_uint8_t)id->pt; attr->setting.frm_per_pkt = 2; attr->setting.vad = 1; attr->setting.plc = 1; - if (def_config.octet_align) { + if (def_config[idx].octet_align) { attr->setting.dec_fmtp.cnt = 1; attr->setting.dec_fmtp.param[0].name = pj_str("octet-align"); attr->setting.dec_fmtp.param[0].val = pj_str("1"); @@ -328,7 +437,7 @@ static pj_status_t amr_default_attr( pjmedia_codec_factory *factory, /* - * Enum codecs supported by this factory (i.e. only AMR-NB!). + * Enum codecs supported by this factory (i.e. AMR-NB and AMR-WB). */ static pj_status_t amr_enum_codecs( pjmedia_codec_factory *factory, unsigned *count, @@ -337,21 +446,34 @@ static pj_status_t amr_enum_codecs( pjmedia_codec_factory *factory, PJ_UNUSED_ARG(factory); PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL); - pj_bzero(&codecs[0], sizeof(pjmedia_codec_info)); - codecs[0].encoding_name = pj_str("AMR"); - codecs[0].pt = PJMEDIA_RTP_PT_AMR; - codecs[0].type = PJMEDIA_TYPE_AUDIO; - codecs[0].clock_rate = 8000; - codecs[0].channel_cnt = 1; + *count = 0; - *count = 1; + if (amr_codec_factory.init[IDX_AMR_NB]) { + pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info)); + codecs[*count].encoding_name = pj_str("AMR"); + codecs[*count].pt = PJMEDIA_RTP_PT_AMR; + codecs[*count].type = PJMEDIA_TYPE_AUDIO; + codecs[*count].clock_rate = 8000; + codecs[*count].channel_cnt = 1; + (*count)++; + } + + if (amr_codec_factory.init[IDX_AMR_WB]) { + pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info)); + codecs[*count].encoding_name = pj_str("AMR-WB"); + codecs[*count].pt = PJMEDIA_RTP_PT_AMRWB; + codecs[*count].type = PJMEDIA_TYPE_AUDIO; + codecs[*count].clock_rate = 16000; + codecs[*count].channel_cnt = 1; + (*count)++; + } return PJ_SUCCESS; } /* - * Allocate a new AMR-NB codec instance. + * Allocate a new AMR codec instance. */ static pj_status_t amr_alloc_codec( pjmedia_codec_factory *factory, const pjmedia_codec_info *id, @@ -365,7 +487,7 @@ static pj_status_t amr_alloc_codec( pjmedia_codec_factory *factory, PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL); PJ_ASSERT_RETURN(factory == &amr_codec_factory.base, PJ_EINVAL); - pool = pjmedia_endpt_create_pool(amr_codec_factory.endpt, "amrnb-inst", + pool = pjmedia_endpt_create_pool(amr_codec_factory.endpt, "amr-inst", 512, 512); codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec); @@ -379,7 +501,9 @@ static pj_status_t amr_alloc_codec( pjmedia_codec_factory *factory, #if USE_PJMEDIA_PLC /* Create PLC */ - status = pjmedia_plc_create(pool, 8000, 160, 0, &amr_data->plc); + status = pjmedia_plc_create(pool, id->clock_rate, + id->clock_rate * FRAME_LENGTH_MS / 1000, 0, + &amr_data->plc); if (status != PJ_SUCCESS) { return status; } @@ -437,12 +561,14 @@ static pj_status_t amr_codec_open( pjmedia_codec *codec, pj_uint8_t octet_align = 0; pj_int8_t enc_mode; const pj_str_t STR_FMTP_OCTET_ALIGN = {"octet-align", 11}; + unsigned idx; PJ_ASSERT_RETURN(codec && attr, PJ_EINVAL); PJ_ASSERT_RETURN(amr_data != NULL, PJ_EINVALIDOP); + idx = (attr->info.clock_rate <= 8000? IDX_AMR_NB: IDX_AMR_WB); enc_mode = pjmedia_codec_amr_get_mode(attr->info.avg_bps); - pj_assert(enc_mode >= 0 && enc_mode <= 7); + pj_assert(enc_mode >= 0 && enc_mode < amr_bitrates_size[idx]); /* Check octet-align */ for (i = 0; i < attr->setting.dec_fmtp.cnt; ++i) { @@ -476,7 +602,7 @@ static pj_status_t amr_codec_open( pjmedia_codec *codec, p = pj_strbuf(&attr->setting.enc_fmtp.param[i].val); l = pj_strlen(&attr->setting.enc_fmtp.param[i].val); while (l--) { - if (*p>='0' && *p<='7') { + if (*p>='0' && *p<=('0'+amr_bitrates_size[idx]-1)) { pj_int8_t tmp = *p - '0' - enc_mode; if (PJ_ABS(diff) > PJ_ABS(tmp) || @@ -496,38 +622,56 @@ static pj_status_t amr_codec_open( pjmedia_codec *codec, } } + amr_data->clock_rate = attr->info.clock_rate; amr_data->vad_enabled = (attr->setting.vad != 0); amr_data->plc_enabled = (attr->setting.plc != 0); amr_data->enc_mode = enc_mode; - amr_data->encoder = Encoder_Interface_init(amr_data->vad_enabled); + if (idx == IDX_AMR_NB) { +#ifdef USE_AMRNB + amr_data->encoder = Encoder_Interface_init(amr_data->vad_enabled); +#endif + } else { +#ifdef USE_AMRWB + amr_data->encoder = E_IF_init(); +#endif + } if (amr_data->encoder == NULL) { - TRACE_((THIS_FILE, "Encoder_Interface_init() failed")); + TRACE_((THIS_FILE, "Encoder initialization failed")); amr_codec_close(codec); return PJMEDIA_CODEC_EFAILED; } setting = &amr_data->enc_setting; pj_bzero(setting, sizeof(pjmedia_codec_amr_pack_setting)); - setting->amr_nb = 1; + setting->amr_nb = (idx == IDX_AMR_NB? 1: 0); setting->reorder = 0; setting->octet_aligned = octet_align; setting->cmr = 15; - amr_data->decoder = Decoder_Interface_init(); + if (idx == IDX_AMR_NB) { +#ifdef USE_AMRNB + amr_data->decoder = Decoder_Interface_init(); +#endif + } else { +#ifdef USE_AMRWB + amr_data->decoder = D_IF_init(); +#endif + } if (amr_data->decoder == NULL) { - TRACE_((THIS_FILE, "Decoder_Interface_init() failed")); + TRACE_((THIS_FILE, "Decoder initialization failed")); amr_codec_close(codec); return PJMEDIA_CODEC_EFAILED; } setting = &amr_data->dec_setting; pj_bzero(setting, sizeof(pjmedia_codec_amr_pack_setting)); - setting->amr_nb = 1; + setting->amr_nb = (idx == IDX_AMR_NB? 1: 0); setting->reorder = 0; setting->octet_aligned = octet_align; - TRACE_((THIS_FILE, "AMR-NB codec allocated: vad=%d, plc=%d, bitrate=%d", + TRACE_((THIS_FILE, "AMR codec allocated: clockrate=%d vad=%d, plc=%d," + " bitrate=%d", amr_data->clock_rate, amr_data->vad_enabled, amr_data->plc_enabled, - pjmedia_codec_amrnb_bitrates[amr_data->enc_mode])); + amr_bitrates[idx][amr_data->enc_mode])); return PJ_SUCCESS; } @@ -545,16 +689,32 @@ static pj_status_t amr_codec_close( pjmedia_codec *codec ) PJ_ASSERT_RETURN(amr_data != NULL, PJ_EINVALIDOP); if (amr_data->encoder) { - Encoder_Interface_exit(amr_data->encoder); + if (amr_data->enc_setting.amr_nb) { +#ifdef USE_AMRNB + Encoder_Interface_exit(amr_data->encoder); +#endif + } else { +#ifdef USE_AMRWB + E_IF_exit(amr_data->encoder); +#endif + } amr_data->encoder = NULL; } if (amr_data->decoder) { - Decoder_Interface_exit(amr_data->decoder); + if (amr_data->dec_setting.amr_nb) { +#ifdef USE_AMRNB + Decoder_Interface_exit(amr_data->decoder); +#endif + } else { +#ifdef USE_AMRWB + D_IF_exit(amr_data->decoder); +#endif + } amr_data->decoder = NULL; } - TRACE_((THIS_FILE, "AMR-NB codec closed")); + TRACE_((THIS_FILE, "AMR codec closed")); return PJ_SUCCESS; } @@ -575,11 +735,15 @@ static pj_status_t amr_codec_modify( pjmedia_codec *codec, amr_data->vad_enabled = (attr->setting.vad != 0); amr_data->plc_enabled = (attr->setting.plc != 0); - if (prev_vad_state != amr_data->vad_enabled) { + if (amr_data->enc_setting.amr_nb && + prev_vad_state != amr_data->vad_enabled) + { /* Reinit AMR encoder to update VAD setting */ TRACE_((THIS_FILE, "Reiniting AMR encoder to update VAD setting.")); +#ifdef USE_AMRNB Encoder_Interface_exit(amr_data->encoder); amr_data->encoder = Encoder_Interface_init(amr_data->vad_enabled); +#endif if (amr_data->encoder == NULL) { TRACE_((THIS_FILE, "Encoder_Interface_init() failed")); amr_codec_close(codec); @@ -587,7 +751,7 @@ static pj_status_t amr_codec_modify( pjmedia_codec *codec, } } - TRACE_((THIS_FILE, "AMR-NB codec modified: vad=%d, plc=%d", + TRACE_((THIS_FILE, "AMR codec modified: vad=%d, plc=%d", amr_data->vad_enabled, amr_data->plc_enabled)); return PJ_SUCCESS; } @@ -606,6 +770,7 @@ static pj_status_t amr_codec_parse( pjmedia_codec *codec, struct amr_data *amr_data = (struct amr_data*) codec->codec_data; pj_uint8_t cmr; pj_status_t status; + unsigned idx = (amr_data->enc_setting.amr_nb? 0: 1); status = pjmedia_codec_amr_parse(pkt, pkt_size, ts, &amr_data->dec_setting, frames, frame_cnt, &cmr); @@ -613,11 +778,11 @@ static pj_status_t amr_codec_parse( pjmedia_codec *codec, return status; /* Check for Change Mode Request. */ - if (cmr <= 7 && amr_data->enc_mode != cmr) { + if (cmr < amr_bitrates_size[idx] && amr_data->enc_mode != cmr) { amr_data->enc_mode = cmr; - TRACE_((THIS_FILE, "AMR-NB encoder switched mode to %d (%dbps)", - amr_data->enc_mode, - pjmedia_codec_amrnb_bitrates[amr_data->enc_mode])); + TRACE_((THIS_FILE, "AMR encoder switched mode to %d (%dbps)", + amr_data->enc_mode, + amr_bitrates[idx][amr_data->enc_mode])); } return PJ_SUCCESS; @@ -649,7 +814,7 @@ static pj_status_t amr_codec_encode( pjmedia_codec *codec, PJ_ASSERT_RETURN(input && output, PJ_EINVAL); nsamples = input->size >> 1; - samples_per_frame = 160; + samples_per_frame = amr_data->clock_rate * FRAME_LENGTH_MS / 1000; PJ_ASSERT_RETURN(nsamples % samples_per_frame == 0, PJMEDIA_CODEC_EPCMFRMINLEN); @@ -661,20 +826,30 @@ static pj_status_t amr_codec_encode( pjmedia_codec *codec, speech = (pj_int16_t*)input->buf; bitstream = (unsigned char*)output->buf; while (nsamples >= samples_per_frame) { - size = Encoder_Interface_Encode (amr_data->encoder, amr_data->enc_mode, - speech, bitstream, 0); + if (amr_data->enc_setting.amr_nb) { +#ifdef USE_AMRNB + size = Encoder_Interface_Encode (amr_data->encoder, + amr_data->enc_mode, + speech, bitstream, 0); +#endif + } else { +#ifdef USE_AMRWB + size = E_IF_encode (amr_data->encoder, amr_data->enc_mode, + speech, bitstream, 0); +#endif + } if (size == 0) { output->size = 0; output->buf = NULL; output->type = PJMEDIA_FRAME_TYPE_NONE; - TRACE_((THIS_FILE, "AMR-NB encode() failed")); + TRACE_((THIS_FILE, "AMR encode() failed")); return PJMEDIA_CODEC_EFAILED; } - nsamples -= 160; + nsamples -= samples_per_frame; speech += samples_per_frame; bitstream += size; out_size += size; - TRACE_((THIS_FILE, "AMR-NB encode(): mode=%d, size=%d", + TRACE_((THIS_FILE, "AMR encode(): mode=%d, size=%d", amr_data->enc_mode, out_size)); } @@ -690,8 +865,13 @@ static pj_status_t amr_codec_encode( pjmedia_codec *codec, info->mode = (pj_int8_t)amr_data->enc_mode; info->start_bit = 0; frames[i].buf = p + 1; - frames[i].size = (info->frame_type <= 8)? - pjmedia_codec_amrnb_framelen[info->frame_type] : 0; + if (amr_data->enc_setting.amr_nb) { + frames[i].size = (info->frame_type <= 8)? + pjmedia_codec_amrnb_framelen[info->frame_type] : 0; + } else { + frames[i].size = (info->frame_type <= 9)? + pjmedia_codec_amrwb_framelen[info->frame_type] : 0; + } p += frames[i].size + 1; /* Count the number of SID and DTX frames */ @@ -713,7 +893,8 @@ static pj_status_t amr_codec_encode( pjmedia_codec *codec, dtx_duration = pj_timestamp_diff32(&amr_data->last_tx, &input->timestamp); if (PJMEDIA_CODEC_MAX_SILENCE_PERIOD == -1 || - dtx_duration < PJMEDIA_CODEC_MAX_SILENCE_PERIOD*8000/1000) + dtx_duration < PJMEDIA_CODEC_MAX_SILENCE_PERIOD* + amr_data->clock_rate/1000) { output->size = 0; output->type = PJMEDIA_FRAME_TYPE_NONE; @@ -755,31 +936,43 @@ static pj_status_t amr_codec_decode( pjmedia_codec *codec, struct amr_data *amr_data = (struct amr_data*) codec->codec_data; pjmedia_frame input_; pjmedia_codec_amr_bit_info *info; - /* VA AMR-NB decoding buffer: AMR-NB max frame size + 1 byte header. */ - unsigned char bitstream[32]; + unsigned out_size; + /* AMR decoding buffer: AMR max frame size + 1 byte header. */ + unsigned char bitstream[61]; pj_assert(amr_data != NULL); PJ_ASSERT_RETURN(input && output, PJ_EINVAL); - if (output_buf_len < 320) + out_size = amr_data->clock_rate * FRAME_LENGTH_MS / 1000 * 2; + if (output_buf_len < out_size) return PJMEDIA_CODEC_EPCMTOOSHORT; input_.buf = &bitstream[1]; - input_.size = 31; /* AMR-NB max frame size */ + /* AMR max frame size */ + input_.size = (amr_data->dec_setting.amr_nb? 31: 60); pjmedia_codec_amr_predecode(input, &amr_data->dec_setting, &input_); info = (pjmedia_codec_amr_bit_info*)&input_.bit_info; - /* VA AMRNB decoder requires frame info in the first byte. */ + /* VA AMR decoder requires frame info in the first byte. */ bitstream[0] = (info->frame_type << 3) | (info->good_quality << 2); - TRACE_((THIS_FILE, "AMR-NB decode(): mode=%d, ft=%d, size=%d", + TRACE_((THIS_FILE, "AMR decode(): mode=%d, ft=%d, size=%d", info->mode, info->frame_type, input_.size)); /* Decode */ - Decoder_Interface_Decode(amr_data->decoder, bitstream, - (pj_int16_t*)output->buf, 0); + if (amr_data->dec_setting.amr_nb) { +#ifdef USE_AMRNB + Decoder_Interface_Decode(amr_data->decoder, bitstream, + (pj_int16_t*)output->buf, 0); +#endif + } else { +#ifdef USE_AMRWB + D_IF_decode(amr_data->decoder, bitstream, + (pj_int16_t*)output->buf, 0); +#endif + } - output->size = 320; + output->size = out_size; output->type = PJMEDIA_FRAME_TYPE_AUDIO; output->timestamp = input->timestamp; @@ -804,16 +997,17 @@ static pj_status_t amr_codec_recover( pjmedia_codec *codec, struct pjmedia_frame *output) { struct amr_data *amr_data = codec->codec_data; + unsigned out_size = amr_data->clock_rate * FRAME_LENGTH_MS / 1000 * 2; TRACE_((THIS_FILE, "amr_codec_recover")); PJ_ASSERT_RETURN(amr_data->plc_enabled, PJ_EINVALIDOP); - PJ_ASSERT_RETURN(output_buf_len >= 320, PJMEDIA_CODEC_EPCMTOOSHORT); + PJ_ASSERT_RETURN(output_buf_len >= out_size, PJMEDIA_CODEC_EPCMTOOSHORT); pjmedia_plc_generate(amr_data->plc, (pj_int16_t*)output->buf); - output->size = 320; + output->size = out_size; output->type = PJMEDIA_FRAME_TYPE_AUDIO; return PJ_SUCCESS; @@ -821,11 +1015,17 @@ static pj_status_t amr_codec_recover( pjmedia_codec *codec, #endif #if defined(_MSC_VER) && PJMEDIA_AUTO_LINK_OPENCORE_AMR_LIBS -# if PJMEDIA_OPENCORE_AMR_BUILT_WITH_GCC -# pragma comment( lib, "libopencore-amrnb.a") -# else -# error Unsupported OpenCORE AMR library, fix here -# endif +# if PJMEDIA_OPENCORE_AMR_BUILT_WITH_GCC +# ifdef USE_AMRNB +# pragma comment( lib, "libopencore-amrnb.a") +# endif +# ifdef USE_AMRWB +# pragma comment( lib, "libopencore-amrwb.a") +# pragma comment( lib, "libvo-amrwbenc.a") +# endif +# else +# error Unsupported OpenCORE AMR library, fix here +# endif #endif #endif diff --git a/pjmedia/src/pjmedia-codec/silk.c b/pjmedia/src/pjmedia-codec/silk.c new file mode 100644 index 0000000..75ee7ca --- /dev/null +++ b/pjmedia/src/pjmedia-codec/silk.c @@ -0,0 +1,953 @@ +/* $Id: silk.c 4339 2013-01-31 05:23:46Z ming $ */ +/* + * Copyright (C) 2012-2012 Teluu Inc. (http://www.teluu.com) + * Contributed by Regis Montoya (aka r3gis - www.r3gis.fr) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <pjmedia-codec/silk.h> +#include <pjmedia/codec.h> +#include <pjmedia/delaybuf.h> +#include <pjmedia/endpoint.h> +#include <pjmedia/errno.h> +#include <pjmedia/port.h> +#include <pj/pool.h> +#include <pj/string.h> +#include <pj/assert.h> +#include <pj/log.h> + +#if defined(PJMEDIA_HAS_SILK_CODEC) && (PJMEDIA_HAS_SILK_CODEC!=0) + +#include "SKP_Silk_SDK_API.h" + +#define THIS_FILE "silk.c" + +#ifndef PJMEDIA_SILK_DELAY_BUF_OPTIONS + #define PJMEDIA_SILK_DELAY_BUF_OPTIONS PJMEDIA_DELAY_BUF_SIMPLE_FIFO +#endif + +#define FRAME_LENGTH_MS 20 +#define SILK_ENC_CTL_PACKET_LOSS_PCT 10 +#define SILK_MIN_BITRATE 5000 +#define CALC_BITRATE_QUALITY(quality, max_br) \ + (quality * max_br / 10) +#define CALC_BITRATE(max_br) \ + CALC_BITRATE_QUALITY(PJMEDIA_CODEC_SILK_DEFAULT_QUALITY, \ + max_br); + + +/* Prototypes for SILK factory */ +static pj_status_t silk_test_alloc( pjmedia_codec_factory *factory, + const pjmedia_codec_info *id ); +static pj_status_t silk_default_attr( pjmedia_codec_factory *factory, + const pjmedia_codec_info *id, + pjmedia_codec_param *attr ); +static pj_status_t silk_enum_codecs ( pjmedia_codec_factory *factory, + unsigned *count, + pjmedia_codec_info codecs[]); +static pj_status_t silk_alloc_codec( pjmedia_codec_factory *factory, + const pjmedia_codec_info *id, + pjmedia_codec **p_codec); +static pj_status_t silk_dealloc_codec( pjmedia_codec_factory *factory, + pjmedia_codec *codec ); + +/* Prototypes for SILK implementation. */ +static pj_status_t silk_codec_init( pjmedia_codec *codec, + pj_pool_t *pool ); +static pj_status_t silk_codec_open( pjmedia_codec *codec, + pjmedia_codec_param *attr ); +static pj_status_t silk_codec_close( pjmedia_codec *codec ); +static pj_status_t silk_codec_modify( pjmedia_codec *codec, + const pjmedia_codec_param *attr ); +static pj_status_t silk_codec_parse( pjmedia_codec *codec, + void *pkt, + pj_size_t pkt_size, + const pj_timestamp *timestamp, + unsigned *frame_cnt, + pjmedia_frame frames[]); +static pj_status_t silk_codec_encode( pjmedia_codec *codec, + const struct pjmedia_frame *input, + unsigned output_buf_len, + struct pjmedia_frame *output); +static pj_status_t silk_codec_decode( pjmedia_codec *codec, + const struct pjmedia_frame *input, + unsigned output_buf_len, + struct pjmedia_frame *output); +static pj_status_t silk_codec_recover( pjmedia_codec *codec, + unsigned output_buf_len, + struct pjmedia_frame *output); + + +typedef enum +{ + PARAM_NB, /* Index for narrowband parameter. */ + PARAM_MB, /* Index for medium parameter. */ + PARAM_WB, /* Index for wideband parameter. */ + PARAM_SWB, /* Index for super-wideband parameter */ +} silk_mode; + + +/* Silk default parameter */ +typedef struct silk_param +{ + int enabled; /* Is this mode enabled? */ + int pt; /* Payload type. */ + unsigned clock_rate; /* Default sampling rate to be used. */ + pj_uint16_t ptime; /* packet length (in ms). */ + pj_uint32_t bitrate; /* Bit rate for current mode. */ + pj_uint32_t max_bitrate; /* Max bit rate for current mode. */ + int complexity; /* Complexity mode: 0/lowest to 2. */ +} silk_param; + + +/* Definition for SILK codec operations. */ +static pjmedia_codec_op silk_op = +{ + &silk_codec_init, + &silk_codec_open, + &silk_codec_close, + &silk_codec_modify, + &silk_codec_parse, + &silk_codec_encode, + &silk_codec_decode, + &silk_codec_recover +}; + +/* Definition for SILK codec factory operations. */ +static pjmedia_codec_factory_op silk_factory_op = +{ + &silk_test_alloc, + &silk_default_attr, + &silk_enum_codecs, + &silk_alloc_codec, + &silk_dealloc_codec, + &pjmedia_codec_silk_deinit +}; + + +/* SILK factory private data */ +static struct silk_factory +{ + pjmedia_codec_factory base; + pjmedia_endpt *endpt; + pj_pool_t *pool; + pj_mutex_t *mutex; + struct silk_param silk_param[4]; +} silk_factory; + + +/* SILK codec private data. */ +typedef struct silk_private +{ + silk_mode mode; /**< Silk mode. */ + pj_pool_t *pool; /**< Pool for each instance. */ + unsigned samples_per_frame; + pj_uint8_t pcm_bytes_per_sample; + + pj_bool_t enc_ready; + SKP_SILK_SDK_EncControlStruct enc_ctl; + void *enc_st; + + pj_bool_t dec_ready; + SKP_SILK_SDK_DecControlStruct dec_ctl; + void *dec_st; + + /* Buffer to hold decoded frames. */ + void *dec_buf[SILK_MAX_FRAMES_PER_PACKET-1]; + SKP_int16 dec_buf_size[SILK_MAX_FRAMES_PER_PACKET-1]; + pj_size_t dec_buf_sz; + unsigned dec_buf_cnt; + pj_uint32_t pkt_info; /**< Packet info for buffered frames. */ +} silk_private; + + +silk_mode silk_get_mode_from_clock_rate(unsigned clock_rate) { + if (clock_rate <= silk_factory.silk_param[PARAM_NB].clock_rate) { + return PARAM_NB; + } else if (clock_rate <= silk_factory.silk_param[PARAM_MB].clock_rate) { + return PARAM_MB; + } else if (clock_rate <= silk_factory.silk_param[PARAM_WB].clock_rate) { + return PARAM_WB; + } + return PARAM_SWB; +} + + +PJ_DEF(pj_status_t) pjmedia_codec_silk_init(pjmedia_endpt *endpt) +{ + pjmedia_codec_mgr *codec_mgr; + silk_param *sp; + pj_status_t status; + + if (silk_factory.endpt != NULL) { + /* Already initialized. */ + return PJ_SUCCESS; + } + + /* Init factory */ + pj_bzero(&silk_factory, sizeof(silk_factory)); + silk_factory.base.op = &silk_factory_op; + silk_factory.base.factory_data = NULL; + silk_factory.endpt = endpt; + + /* Create pool */ + silk_factory.pool = pjmedia_endpt_create_pool(endpt, "silk", 4000, 4000); + if (!silk_factory.pool) + return PJ_ENOMEM; + + /* Create mutex. */ + status = pj_mutex_create_simple(silk_factory.pool, "silk", + &silk_factory.mutex); + if (status != PJ_SUCCESS) + goto on_error; + + /* Initialize default codec params */ + + /* From SILK docs: + - SILK bitrate tables: + +----------------+---------+-----------+ + | | fs (Hz) | BR (kbps) | + +----------------+---------+-----------+ + | Narrowband | 8000 | 6 - 20 | + | Mediumband | 12000 | 7 - 25 | + | Wideband | 16000 | 8 - 30 | + | Super Wideband | 24000 | 12 - 40 | + +----------------+---------+-----------+ + - The upper limits of the bit rate ranges in this table are + recommended values. + */ + + sp = &silk_factory.silk_param[PARAM_NB]; + sp->pt = PJMEDIA_RTP_PT_SILK_NB; + sp->clock_rate = 8000; + sp->max_bitrate = 22000; + sp->bitrate = CALC_BITRATE(sp->max_bitrate); + sp->ptime = FRAME_LENGTH_MS; + sp->complexity = PJMEDIA_CODEC_SILK_DEFAULT_COMPLEXITY; + sp->enabled = 1; + + sp = &silk_factory.silk_param[PARAM_MB]; + sp->pt = PJMEDIA_RTP_PT_SILK_MB; + sp->clock_rate = 12000; + sp->max_bitrate = 28000; + sp->bitrate = CALC_BITRATE(sp->max_bitrate); + sp->ptime = FRAME_LENGTH_MS; + sp->complexity = PJMEDIA_CODEC_SILK_DEFAULT_COMPLEXITY; + sp->enabled = 0; + + sp = &silk_factory.silk_param[PARAM_WB]; + sp->pt = PJMEDIA_RTP_PT_SILK_WB; + sp->clock_rate = 16000; + sp->max_bitrate = 36000; + sp->bitrate = CALC_BITRATE(sp->max_bitrate); + sp->ptime = FRAME_LENGTH_MS; + sp->complexity = PJMEDIA_CODEC_SILK_DEFAULT_COMPLEXITY; + sp->enabled = 1; + + sp = &silk_factory.silk_param[PARAM_SWB]; + sp->pt = PJMEDIA_RTP_PT_SILK_SWB; + sp->clock_rate = 24000; + sp->max_bitrate = 46000; + sp->bitrate = CALC_BITRATE(sp->max_bitrate); + sp->ptime = FRAME_LENGTH_MS; + sp->complexity = PJMEDIA_CODEC_SILK_DEFAULT_COMPLEXITY; + sp->enabled = 0; + + + /* Get the codec manager. */ + codec_mgr = pjmedia_endpt_get_codec_mgr(endpt); + if (!codec_mgr) { + return PJ_EINVALIDOP; + } + + /* Register codec factory to endpoint. */ + status = pjmedia_codec_mgr_register_factory(codec_mgr, + &silk_factory.base); + if (status != PJ_SUCCESS) + return status; + + PJ_LOG(4,(THIS_FILE, "SILK codec version %s initialized", + SKP_Silk_SDK_get_version())); + return PJ_SUCCESS; + +on_error: + if (silk_factory.mutex) { + pj_mutex_destroy(silk_factory.mutex); + silk_factory.mutex = NULL; + } + if (silk_factory.pool) { + pj_pool_release(silk_factory.pool); + silk_factory.pool = NULL; + } + + return status; +} + + +/* + * Change the configuration setting of the SILK codec for the specified + * clock rate. + */ +PJ_DEF(pj_status_t) pjmedia_codec_silk_set_config( + unsigned clock_rate, + const pjmedia_codec_silk_setting *opt) +{ + unsigned i; + + /* Look up in factory modes table */ + for (i = 0; i < sizeof(silk_factory.silk_param)/ + sizeof(silk_factory.silk_param[0]); ++i) + { + if (silk_factory.silk_param[i].clock_rate == clock_rate) { + int quality = PJMEDIA_CODEC_SILK_DEFAULT_QUALITY; + int complexity = PJMEDIA_CODEC_SILK_DEFAULT_COMPLEXITY; + + silk_factory.silk_param[i].enabled = opt->enabled; + if (opt->complexity >= 0) + complexity = opt->complexity; + silk_factory.silk_param[i].complexity = complexity; + if (opt->quality >= 0) + quality = opt->quality; + silk_factory.silk_param[i].bitrate = + CALC_BITRATE_QUALITY(quality, + silk_factory.silk_param[i].max_bitrate); + if (silk_factory.silk_param[i].bitrate < SILK_MIN_BITRATE) + silk_factory.silk_param[i].bitrate = SILK_MIN_BITRATE; + + return PJ_SUCCESS; + } + } + + return PJ_ENOTFOUND; +} + + +/* + * Unregister SILK codec factory from pjmedia endpoint and deinitialize + * the SILK codec library. + */ +PJ_DEF(pj_status_t) pjmedia_codec_silk_deinit(void) +{ + pjmedia_codec_mgr *codec_mgr; + pj_status_t status; + + if (silk_factory.endpt == NULL) { + /* Not registered. */ + return PJ_SUCCESS; + } + + /* Lock mutex. */ + pj_mutex_lock(silk_factory.mutex); + + /* Get the codec manager. */ + codec_mgr = pjmedia_endpt_get_codec_mgr(silk_factory.endpt); + if (!codec_mgr) { + silk_factory.endpt = NULL; + pj_mutex_unlock(silk_factory.mutex); + return PJ_EINVALIDOP; + } + + /* Unregister silk codec factory. */ + status = pjmedia_codec_mgr_unregister_factory(codec_mgr, + &silk_factory.base); + silk_factory.endpt = NULL; + + /* Destroy mutex. */ + pj_mutex_destroy(silk_factory.mutex); + silk_factory.mutex = NULL; + + + /* Release pool. */ + pj_pool_release(silk_factory.pool); + silk_factory.pool = NULL; + + return status; +} + + +/* + * Check if factory can allocate the specified codec. + */ +static pj_status_t silk_test_alloc(pjmedia_codec_factory *factory, + const pjmedia_codec_info *info ) +{ + const pj_str_t silk_tag = {"SILK", 4}; + unsigned i; + + PJ_UNUSED_ARG(factory); + PJ_ASSERT_RETURN(factory==&silk_factory.base, PJ_EINVAL); + + /* Type MUST be audio. */ + if (info->type != PJMEDIA_TYPE_AUDIO) + return PJMEDIA_CODEC_EUNSUP; + + /* Check encoding name. */ + if (pj_stricmp(&info->encoding_name, &silk_tag) != 0) + return PJMEDIA_CODEC_EUNSUP; + + /* Channel count must be one */ + if (info->channel_cnt != 1) + return PJMEDIA_CODEC_EUNSUP; + + /* Check clock-rate */ + for (i=0; i<PJ_ARRAY_SIZE(silk_factory.silk_param); ++i) { + silk_param *sp = &silk_factory.silk_param[i]; + if (sp->enabled && info->clock_rate == sp->clock_rate) + { + return PJ_SUCCESS; + } + } + /* Clock rate not supported */ + return PJMEDIA_CODEC_EUNSUP; +} + + +/* + * Generate default attribute. + */ +static pj_status_t silk_default_attr( pjmedia_codec_factory *factory, + const pjmedia_codec_info *id, + pjmedia_codec_param *attr ) +{ + silk_param *sp; + int i; + + PJ_ASSERT_RETURN(factory==&silk_factory.base, PJ_EINVAL); + + i = silk_get_mode_from_clock_rate(id->clock_rate); + pj_assert(i >= PARAM_NB && i <= PARAM_SWB); + + sp = &silk_factory.silk_param[i]; + + pj_bzero(attr, sizeof(pjmedia_codec_param)); + attr->info.channel_cnt = 1; + attr->info.clock_rate = sp->clock_rate; + attr->info.avg_bps = sp->bitrate; + attr->info.max_bps = sp->max_bitrate; + attr->info.frm_ptime = sp->ptime; + attr->info.pcm_bits_per_sample = 16; + attr->info.pt = (pj_uint8_t) sp->pt; + attr->setting.frm_per_pkt = 1; + attr->setting.vad = 0; /* DTX is not recommended for quality reason */ + attr->setting.plc = 1; + + i = 0; + attr->setting.dec_fmtp.param[i].name = pj_str("useinbandfec"); + attr->setting.dec_fmtp.param[i++].val = pj_str("0"); + /* + attr->setting.dec_fmtp.param[i].name = pj_str("maxaveragebitrate"); + attr->setting.dec_fmtp.param[i++].val = pj_str(mode->bitrate_str); + */ + attr->setting.dec_fmtp.cnt = (pj_uint8_t)i; + + return PJ_SUCCESS; +} + + +/* + * Enum codecs supported by this factory. + */ +static pj_status_t silk_enum_codecs(pjmedia_codec_factory *factory, + unsigned *count, + pjmedia_codec_info codecs[]) +{ + unsigned max; + int i; + + PJ_ASSERT_RETURN(factory==&silk_factory.base, PJ_EINVAL); + PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL); + + max = *count; + *count = 0; + + for (i = 0; i<PJ_ARRAY_SIZE(silk_factory.silk_param) && *count<max; ++i) + { + silk_param *sp = &silk_factory.silk_param[i]; + + if (!sp->enabled) + continue; + + pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info)); + codecs[*count].encoding_name = pj_str("SILK"); + codecs[*count].pt = sp->pt; + codecs[*count].type = PJMEDIA_TYPE_AUDIO; + codecs[*count].clock_rate = sp->clock_rate; + codecs[*count].channel_cnt = 1; + + ++*count; + } + + return PJ_SUCCESS; +} + + +/* + * Allocate a new SILK codec instance. + */ +static pj_status_t silk_alloc_codec(pjmedia_codec_factory *factory, + const pjmedia_codec_info *id, + pjmedia_codec **p_codec) +{ + pj_pool_t *pool; + pjmedia_codec *codec; + silk_private *silk; + + PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL); + PJ_ASSERT_RETURN(factory == &silk_factory.base, PJ_EINVAL); + + /* Create pool for codec instance */ + pool = pjmedia_endpt_create_pool(silk_factory.endpt, "silk", 512, 512); + + /* Allocate codec */ + codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec); + codec->op = &silk_op; + codec->factory = factory; + codec->codec_data = PJ_POOL_ZALLOC_T(pool, silk_private); + silk = (silk_private*) codec->codec_data; + silk->pool = pool; + silk->enc_ready = PJ_FALSE; + silk->dec_ready = PJ_FALSE; + silk->mode = silk_get_mode_from_clock_rate(id->clock_rate); + + *p_codec = codec; + return PJ_SUCCESS; +} + + +/* + * Free codec. + */ +static pj_status_t silk_dealloc_codec( pjmedia_codec_factory *factory, + pjmedia_codec *codec ) +{ + silk_private *silk; + + PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL); + PJ_ASSERT_RETURN(factory == &silk_factory.base, PJ_EINVAL); + + silk = (silk_private*)codec->codec_data; + + /* Close codec, if it's not closed. */ + if (silk->enc_ready == PJ_TRUE || silk->dec_ready == PJ_TRUE) { + silk_codec_close(codec); + } + + pj_pool_release(silk->pool); + + return PJ_SUCCESS; +} + + +/* + * Init codec. + */ +static pj_status_t silk_codec_init(pjmedia_codec *codec, + pj_pool_t *pool ) +{ + PJ_UNUSED_ARG(codec); + PJ_UNUSED_ARG(pool); + return PJ_SUCCESS; +} + + +/* + * Open codec. + */ +static pj_status_t silk_codec_open(pjmedia_codec *codec, + pjmedia_codec_param *attr ) +{ + + silk_private *silk; + silk_param *sp; + SKP_int st_size, err; + pj_bool_t enc_use_fec; + unsigned enc_bitrate, i; + + PJ_ASSERT_RETURN(codec && attr, PJ_EINVAL); + + silk = (silk_private*)codec->codec_data; + sp = &silk_factory.silk_param[silk->mode]; + + /* Already opened? */ + if (silk->enc_ready || silk->dec_ready) + return PJ_SUCCESS; + + /* Allocate and initialize encoder */ + err = SKP_Silk_SDK_Get_Encoder_Size(&st_size); + if (err) { + PJ_LOG(3,(THIS_FILE, "Failed to get encoder state size (err=%d)", + err)); + return PJMEDIA_CODEC_EFAILED; + } + silk->enc_st = pj_pool_zalloc(silk->pool, st_size); + err = SKP_Silk_SDK_InitEncoder(silk->enc_st, &silk->enc_ctl); + if (err) { + PJ_LOG(3,(THIS_FILE, "Failed to init encoder (err=%d)", err)); + return PJMEDIA_CODEC_EFAILED; + } + + /* Check fmtp params */ + enc_use_fec = PJ_TRUE; + enc_bitrate = sp->bitrate; + for (i = 0; i < attr->setting.enc_fmtp.cnt; ++i) { + pjmedia_codec_fmtp *fmtp = &attr->setting.enc_fmtp; + const pj_str_t STR_USEINBANDFEC = {"useinbandfec", 12}; + const pj_str_t STR_MAXAVERAGEBITRATE = {"maxaveragebitrate", 17}; + + if (!pj_stricmp(&fmtp->param[i].name, &STR_USEINBANDFEC)) { + enc_use_fec = pj_strtoul(&fmtp->param[i].val) != 0; + } else if (!pj_stricmp(&fmtp->param[i].name, &STR_MAXAVERAGEBITRATE)) { + enc_bitrate = pj_strtoul(&fmtp->param[i].val); + if (enc_bitrate > sp->max_bitrate) { + enc_bitrate = sp->max_bitrate; + } + } + } + + /* Setup encoder control for encoding process */ + silk->enc_ready = PJ_TRUE; + silk->samples_per_frame = FRAME_LENGTH_MS * + attr->info.clock_rate / 1000; + silk->pcm_bytes_per_sample = attr->info.pcm_bits_per_sample / 8; + + silk->enc_ctl.API_sampleRate = attr->info.clock_rate; + silk->enc_ctl.maxInternalSampleRate = attr->info.clock_rate; + silk->enc_ctl.packetSize = attr->setting.frm_per_pkt * + silk->samples_per_frame; + /* For useInBandFEC setting to be useful, we need to set + * packetLossPercentage greater than LBRR_LOSS_THRES (1) + */ + silk->enc_ctl.packetLossPercentage = SILK_ENC_CTL_PACKET_LOSS_PCT; + silk->enc_ctl.useInBandFEC = enc_use_fec; + silk->enc_ctl.useDTX = attr->setting.vad; + silk->enc_ctl.complexity = sp->complexity; + silk->enc_ctl.bitRate = enc_bitrate; + + + /* Allocate and initialize decoder */ + err = SKP_Silk_SDK_Get_Decoder_Size(&st_size); + if (err) { + PJ_LOG(3,(THIS_FILE, "Failed to get decoder state size (err=%d)", + err)); + return PJMEDIA_CODEC_EFAILED; + } + silk->dec_st = pj_pool_zalloc(silk->pool, st_size); + err = SKP_Silk_SDK_InitDecoder(silk->dec_st); + if (err) { + PJ_LOG(3,(THIS_FILE, "Failed to init decoder (err=%d)", err)); + return PJMEDIA_CODEC_EFAILED; + } + + /* Setup decoder control for decoding process */ + silk->dec_ctl.API_sampleRate = attr->info.clock_rate; + silk->dec_ctl.framesPerPacket = 1; /* for proper PLC at start */ + silk->dec_ready = PJ_TRUE; + silk->dec_buf_sz = attr->info.clock_rate * attr->info.channel_cnt * + attr->info.frm_ptime / 1000 * + silk->pcm_bytes_per_sample; + + /* Inform the stream to prepare a larger buffer since we cannot parse + * SILK packets and split it into individual frames. + */ + attr->info.max_rx_frame_size = attr->info.max_bps * + attr->info.frm_ptime / 8 / 1000; + if ((attr->info.max_bps * attr->info.frm_ptime) % 8000 != 0) + { + ++attr->info.max_rx_frame_size; + } + attr->info.max_rx_frame_size *= SILK_MAX_FRAMES_PER_PACKET; + + return PJ_SUCCESS; +} + + +/* + * Close codec. + */ +static pj_status_t silk_codec_close( pjmedia_codec *codec ) +{ + silk_private *silk; + silk = (silk_private*)codec->codec_data; + + silk->enc_ready = PJ_FALSE; + silk->dec_ready = PJ_FALSE; + + return PJ_SUCCESS; +} + + +/* + * Modify codec settings. + */ +static pj_status_t silk_codec_modify(pjmedia_codec *codec, + const pjmedia_codec_param *attr ) +{ + PJ_TODO(implement_silk_codec_modify); + + PJ_UNUSED_ARG(codec); + PJ_UNUSED_ARG(attr); + + return PJ_SUCCESS; +} + + +/* + * Encode frame. + */ +static pj_status_t silk_codec_encode(pjmedia_codec *codec, + const struct pjmedia_frame *input, + unsigned output_buf_len, + struct pjmedia_frame *output) +{ + silk_private *silk; + SKP_int err; + unsigned nsamples; + SKP_int16 out_size; + + PJ_ASSERT_RETURN(codec && input && output_buf_len && output, PJ_EINVAL); + silk = (silk_private*)codec->codec_data; + + /* Check frame in size */ + nsamples = input->size >> 1; + PJ_ASSERT_RETURN(nsamples % silk->samples_per_frame == 0, + PJMEDIA_CODEC_EPCMFRMINLEN); + + /* Encode */ + output->size = 0; + out_size = (SKP_int16)output_buf_len; + err = SKP_Silk_SDK_Encode(silk->enc_st, &silk->enc_ctl, + (SKP_int16*)input->buf, nsamples, + (SKP_uint8*)output->buf, &out_size); + if (err) { + PJ_LOG(3, (THIS_FILE, "Failed to encode frame (err=%d)", err)); + output->type = PJMEDIA_FRAME_TYPE_NONE; + if (err == SKP_SILK_ENC_PAYLOAD_BUF_TOO_SHORT) + return PJMEDIA_CODEC_EFRMTOOSHORT; + return PJMEDIA_CODEC_EFAILED; + } + + output->size = out_size; + output->type = PJMEDIA_FRAME_TYPE_AUDIO; + output->timestamp = input->timestamp; + + return PJ_SUCCESS; +} + + +/* + * Get frames in the packet. + */ +static pj_status_t silk_codec_parse( pjmedia_codec *codec, + void *pkt, + pj_size_t pkt_size, + const pj_timestamp *ts, + unsigned *frame_cnt, + pjmedia_frame frames[]) +{ + silk_private *silk; + SKP_Silk_TOC_struct toc; + unsigned i, count; + + PJ_ASSERT_RETURN(codec && ts && frames && frame_cnt, PJ_EINVAL); + silk = (silk_private*)codec->codec_data; + + SKP_Silk_SDK_get_TOC(pkt, pkt_size, &toc); + count = toc.framesInPacket; + pj_assert(count <= SILK_MAX_FRAMES_PER_PACKET); + + for (i = 0; i < count; i++) { + frames[i].type = PJMEDIA_FRAME_TYPE_AUDIO; + frames[i].bit_info = (((unsigned)ts->u64 & 0xFFFF) << 16) | + (((unsigned)pkt & 0xFF) << 8) | + (toc.framesInPacket << 4) | i; + frames[i].buf = pkt; + frames[i].size = pkt_size; + frames[i].timestamp.u64 = ts->u64 + i * silk->samples_per_frame; + } + + *frame_cnt = count; + return PJ_SUCCESS; +} + +static pj_status_t silk_codec_decode(pjmedia_codec *codec, + const struct pjmedia_frame *input, + unsigned output_buf_len, + struct pjmedia_frame *output) +{ + silk_private *silk; + SKP_int16 out_size; + SKP_Silk_TOC_struct toc; + SKP_int err = 0; + unsigned pkt_info, frm_info; + + PJ_ASSERT_RETURN(codec && input && output_buf_len && output, PJ_EINVAL); + silk = (silk_private*)codec->codec_data; + PJ_ASSERT_RETURN(output_buf_len >= silk->dec_buf_sz, PJ_ETOOSMALL); + + SKP_Silk_SDK_get_TOC(input->buf, input->size, &toc); + pkt_info = input->bit_info & 0xFFFFFF00; + frm_info = input->bit_info & 0xF; + + if (toc.framesInPacket == 0) { + /* In SILK ARM version, the table of content can indicate + * that the number of frames in the packet is 0. + * Try to get the number of frames in packet that we save + * in the frame instead. + */ + toc.framesInPacket = (input->bit_info & 0xF0) >> 4; + } + + if (toc.framesInPacket == 0) { + output->size = 0; + } else if (silk->pkt_info != pkt_info || input->bit_info == 0) { + unsigned i; + SKP_int16 nsamples; + + silk->pkt_info = pkt_info; + nsamples = (SKP_int16)silk->dec_buf_sz / silk->pcm_bytes_per_sample; + + if (toc.framesInPacket-1 > (SKP_int)silk->dec_buf_cnt) { + /* Grow the buffer */ + for (i = silk->dec_buf_cnt+1; i < (unsigned)toc.framesInPacket; + i++) + { + silk->dec_buf[i-1] = pj_pool_alloc(silk->pool, + silk->dec_buf_sz); + } + silk->dec_buf_cnt = toc.framesInPacket-1; + } + + /* We need to decode all the frames in the packet. */ + for (i = 0; i < (unsigned)toc.framesInPacket;) { + void *buf; + SKP_int16 *size; + + if (i == 0 || i == frm_info) { + buf = output->buf; + size = &out_size; + } else { + buf = silk->dec_buf[i-1]; + size = &silk->dec_buf_size[i-1]; + } + + *size = nsamples; + err = SKP_Silk_SDK_Decode(silk->dec_st, &silk->dec_ctl, + 0, /* Normal frame flag */ + input->buf, input->size, + buf, size); + if (err) { + PJ_LOG(3, (THIS_FILE, "Failed to decode frame (err=%d)", + err)); + *size = 0; + } else { + *size = *size * silk->pcm_bytes_per_sample; + } + + if (i == frm_info) { + output->size = *size; + } + + i++; + if (!silk->dec_ctl.moreInternalDecoderFrames && + i < (unsigned)toc.framesInPacket) + { + /* It turns out that the packet does not have + * the number of frames as mentioned in the TOC. + */ + for (; i < (unsigned)toc.framesInPacket; i++) { + silk->dec_buf_size[i-1] = 0; + if (i == frm_info) { + output->size = 0; + } + } + } + } + } else { + /* We have already decoded this packet. */ + if (frm_info == 0 || silk->dec_buf_size[frm_info-1] == 0) { + /* The decoding was a failure. */ + output->size = 0; + } else { + /* Copy the decoded frame from the buffer. */ + pj_assert(frm_info-1 < silk->dec_buf_cnt); + if (frm_info-1 >= silk->dec_buf_cnt) { + output->size = 0; + } else { + pj_memcpy(output->buf, silk->dec_buf[frm_info-1], + silk->dec_buf_size[frm_info-1]); + output->size = silk->dec_buf_size[frm_info-1]; + } + } + } + + if (output->size == 0) { + output->type = PJMEDIA_TYPE_NONE; + output->buf = NULL; + return PJMEDIA_CODEC_EFAILED; + } + + output->type = PJMEDIA_TYPE_AUDIO; + output->timestamp = input->timestamp; + + return PJ_SUCCESS; +} + + +/* + * Recover lost frame. + */ +static pj_status_t silk_codec_recover(pjmedia_codec *codec, + unsigned output_buf_len, + struct pjmedia_frame *output) +{ + silk_private *silk; + SKP_int16 out_size; + SKP_int err; + + PJ_ASSERT_RETURN(codec && output_buf_len && output, PJ_EINVAL); + silk = (silk_private*)codec->codec_data; + + out_size = (SKP_int16)output_buf_len / silk->pcm_bytes_per_sample; + err = SKP_Silk_SDK_Decode(silk->dec_st, &silk->dec_ctl, + 1, /* Lost frame flag */ + NULL, + 0, + output->buf, + &out_size); + if (err) { + PJ_LOG(3, (THIS_FILE, "Failed to conceal lost frame (err=%d)", err)); + output->type = PJMEDIA_FRAME_TYPE_NONE; + output->buf = NULL; + output->size = 0; + return PJMEDIA_CODEC_EFAILED; + } + + output->size = out_size * silk->pcm_bytes_per_sample; + output->type = PJMEDIA_FRAME_TYPE_AUDIO; + + return PJ_SUCCESS; +} + +#if defined(_MSC_VER) +# if PJ_DEBUG +# pragma comment(lib, "SKP_Silk_FLP_Win32_debug.lib") +# else +# pragma comment(lib, "SKP_Silk_FLP_Win32_mt.lib") +# endif +#endif + + +#endif /* PJMEDIA_HAS_SILK_CODEC */ |