From 31425448324e10b5fb02557a9f1d5cc86df7bed3 Mon Sep 17 00:00:00 2001 From: Nanang Izzuddin Date: Mon, 24 Sep 2012 06:58:16 +0000 Subject: Re #1586: Initial version of SILK wrapper. git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@4264 74dad513-b988-da41-8d7b-12977e46ad98 --- pjmedia/build/pjmedia_codec.vcproj | 354 +++++++------- pjmedia/include/pjmedia-codec.h | 3 +- pjmedia/include/pjmedia-codec/config.h | 32 ++ pjmedia/include/pjmedia-codec/silk.h | 133 +++++ pjmedia/include/pjmedia-codec/types.h | 28 +- pjmedia/src/pjmedia-codec/audio_codecs.c | 7 + pjmedia/src/pjmedia-codec/silk.c | 805 +++++++++++++++++++++++++++++++ 7 files changed, 1176 insertions(+), 186 deletions(-) create mode 100644 pjmedia/include/pjmedia-codec/silk.h create mode 100644 pjmedia/src/pjmedia-codec/silk.c diff --git a/pjmedia/build/pjmedia_codec.vcproj b/pjmedia/build/pjmedia_codec.vcproj index 158d43de..305c2e22 100644 --- a/pjmedia/build/pjmedia_codec.vcproj +++ b/pjmedia/build/pjmedia_codec.vcproj @@ -223,12 +223,11 @@ /> + + @@ -320,7 +328,7 @@ /> @@ -386,7 +394,7 @@ /> + + - - @@ -576,7 +584,7 @@ /> + + @@ -700,7 +716,7 @@ /> @@ -766,7 +782,7 @@ /> + + @@ -890,7 +914,7 @@ /> - - + + @@ -1080,7 +1104,7 @@ /> @@ -1146,7 +1170,7 @@ /> @@ -1278,7 +1302,7 @@ /> - - @@ -1476,7 +1492,7 @@ /> @@ -1542,7 +1558,7 @@ /> @@ -1608,7 +1624,7 @@ /> @@ -1674,7 +1690,7 @@ /> @@ -1740,7 +1756,7 @@ /> @@ -1806,7 +1822,7 @@ /> - - @@ -1938,7 +1946,7 @@ /> @@ -2004,7 +2012,7 @@ /> @@ -2070,7 +2078,7 @@ /> @@ -2136,7 +2144,7 @@ /> @@ -2202,7 +2210,7 @@ /> @@ -2268,7 +2276,7 @@ /> - - @@ -2400,7 +2400,7 @@ /> @@ -2466,7 +2466,7 @@ /> + + @@ -3133,6 +3137,10 @@ RelativePath="..\include\pjmedia-codec.h" > + + diff --git a/pjmedia/include/pjmedia-codec.h b/pjmedia/include/pjmedia-codec.h index 48f7c8db..26389f8b 100644 --- a/pjmedia/include/pjmedia-codec.h +++ b/pjmedia/include/pjmedia-codec.h @@ -34,8 +34,9 @@ #include #include #include -#include #include +#include +#include #endif /* __PJMEDIA_CODEC_PJMEDIA_CODEC_H__ */ diff --git a/pjmedia/include/pjmedia-codec/config.h b/pjmedia/include/pjmedia-codec/config.h index 99b1d910..a6aa2d66 100644 --- a/pjmedia/include/pjmedia-codec/config.h +++ b/pjmedia/include/pjmedia-codec/config.h @@ -377,6 +377,38 @@ #endif +/** + * Enable SILK codec. + * + * Default: 0 + */ +#ifndef PJMEDIA_HAS_SILK_CODEC +# define PJMEDIA_HAS_SILK_CODEC 0 +#endif + + +/** + * SILK codec default complexity setting, valid values are 0 (lowest), 1, + * and 2. + * + * Default: 2 + */ +#ifndef PJMEDIA_CODEC_SILK_DEFAULT_COMPLEXITY +# define PJMEDIA_CODEC_SILK_DEFAULT_COMPLEXITY 2 +#endif + +/** + * SILK codec default quality setting, valid values are ranging from + * 0 (lowest) to 10. Please note that pjsua-lib may override this setting + * via its codec quality setting (i.e PJSUA_DEFAULT_CODEC_QUALITY). + * + * Default: 10 + */ +#ifndef PJMEDIA_CODEC_SILK_DEFAULT_QUALITY +# define PJMEDIA_CODEC_SILK_DEFAULT_QUALITY 10 +#endif + + /** * Specify if FFMPEG codecs are available. * diff --git a/pjmedia/include/pjmedia-codec/silk.h b/pjmedia/include/pjmedia-codec/silk.h new file mode 100644 index 00000000..ebb364e9 --- /dev/null +++ b/pjmedia/include/pjmedia-codec/silk.h @@ -0,0 +1,133 @@ +/* $Id$ */ +/* + * 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 + */ +#ifndef __PJMEDIA_CODEC_SILK_H__ +#define __PJMEDIA_CODEC_SILK_H__ + +/** + * @file silk.h + * @brief SILK codec. + */ + +#include + +/** + * @defgroup PJMED_SILK SILK Codec Family + * @ingroup PJMEDIA_CODEC_CODECS + * @brief Implementation of SILK codecs (narrow/medium/wide/superwide-band). + * @{ + * + * This section describes functions to initialize and register SILK codec + * factory to the codec manager. After the codec factory has been registered, + * application can use @ref PJMEDIA_CODEC API to manipulate the codec. + * + * The SILK codec uses multiple bit rates, and supports super wideband + * (24 kHz sampling rate), wideband (16 kHz sampling rate), medium (12kHz + * sampling rate), and narrowband (telephone quality, 8 kHz sampling rate). + * + * By default, the SILK codec factory registers two SILK codecs: + * "SILK/8000" narrowband codec and "SILK/16000" wideband codec. This behavior + * can be changed by specifying #pjmedia_codec_silk_options flags during + * initialization. + * + * + * \section codec_setting Codec Settings + * + * \subsection general_setting General Settings + * + * General codec settings for this codec such as VAD and PLC can be + * manipulated through the setting field in #pjmedia_codec_param. + * Please see the documentation of #pjmedia_codec_param for more info. + * + * \subsection specific_setting Codec Specific Settings + * + * The following settings are applicable for this codec. + * + * \subsubsection quality_vs_complexity Quality vs Complexity + * + * The SILK codec quality versus computational complexity and bandwidth + * requirement can be adjusted by modifying the quality and complexity + * setting, by calling #pjmedia_codec_silk_set_config(). + * + * The default setting of quality is specified in + * #PJMEDIA_CODEC_SILK_DEFAULT_QUALITY. And the default setting of + * complexity is specified in #PJMEDIA_CODEC_SILK_DEFAULT_COMPLEXITY. + */ + +PJ_BEGIN_DECL + +typedef struct pjmedia_codec_silk_setting +{ + pj_bool_t enabled; /**< Enable/disable. */ + int quality; /**< Encoding quality, or use -1 for default + (@see PJMEDIA_CODEC_SILK_DEFAULT_QUALITY). */ + int complexity; /**< Encoding complexity, or use -1 for default + (@see PJMEDIA_CODEC_SILK_DEFAULT_COMPLEXITY)*/ +} pjmedia_codec_silk_setting; + + +/** + * Initialize and register SILK codec factory to pjmedia endpoint. By default, + * only narrowband (8kHz sampling rate) and wideband (16kHz sampling rate) + * will be enabled. Quality and complexity for those sampling rate modes + * will be set to the default values (see #PJMEDIA_CODEC_SILK_DEFAULT_QUALITY + * and #PJMEDIA_CODEC_SILK_DEFAULT_COMPLEXITY), application may modify these + * settings via #pjmedia_codec_silk_set_config(). + * + * @param endpt The pjmedia endpoint. + * + * @return PJ_SUCCESS on success. + */ +PJ_DECL(pj_status_t) pjmedia_codec_silk_init(pjmedia_endpt *endpt); + + +/** + * Change the configuration setting of the SILK codec for the specified + * clock rate. + * + * @param clock_rate PCM sampling rate, in Hz, valid values are 8000, + * 12000, 16000 and 24000. + * @param opt The setting to be applied for the specified + * clock rate. + * + * @return PJ_SUCCESS on success. + */ +PJ_DECL(pj_status_t) pjmedia_codec_silk_set_config( + unsigned clock_rate, + const pjmedia_codec_silk_setting *opt); + + +/** + * Unregister SILK codec factory from pjmedia endpoint and deinitialize + * the SILK codec library. + * + * @return PJ_SUCCESS on success. + */ +PJ_DECL(pj_status_t) pjmedia_codec_silk_deinit(void); + + +PJ_END_DECL + + +/** + * @} + */ + +#endif /* __PJMEDIA_CODEC_SILK_H__ */ + diff --git a/pjmedia/include/pjmedia-codec/types.h b/pjmedia/include/pjmedia-codec/types.h index 752a1fcb..d0549f5e 100644 --- a/pjmedia/include/pjmedia-codec/types.h +++ b/pjmedia/include/pjmedia-codec/types.h @@ -63,18 +63,10 @@ enum pjmedia_audio_pt PJMEDIA_RTP_PT_SPEEX_NB, /**< Speex narrowband/8KHz */ PJMEDIA_RTP_PT_SPEEX_WB, /**< Speex wideband/16KHz */ PJMEDIA_RTP_PT_SPEEX_UWB, /**< Speex 32KHz */ - PJMEDIA_RTP_PT_L16_8KHZ_MONO, /**< L16 @ 8KHz, mono */ - PJMEDIA_RTP_PT_L16_8KHZ_STEREO, /**< L16 @ 8KHz, stereo */ - //PJMEDIA_RTP_PT_L16_11KHZ_MONO, /**< L16 @ 11KHz, mono */ - //PJMEDIA_RTP_PT_L16_11KHZ_STEREO, /**< L16 @ 11KHz, stereo */ - PJMEDIA_RTP_PT_L16_16KHZ_MONO, /**< L16 @ 16KHz, mono */ - PJMEDIA_RTP_PT_L16_16KHZ_STEREO, /**< L16 @ 16KHz, stereo */ - //PJMEDIA_RTP_PT_L16_22KHZ_MONO, /**< L16 @ 22KHz, mono */ - //PJMEDIA_RTP_PT_L16_22KHZ_STEREO, /**< L16 @ 22KHz, stereo */ - //PJMEDIA_RTP_PT_L16_32KHZ_MONO, /**< L16 @ 32KHz, mono */ - //PJMEDIA_RTP_PT_L16_32KHZ_STEREO, /**< L16 @ 32KHz, stereo */ - //PJMEDIA_RTP_PT_L16_48KHZ_MONO, /**< L16 @ 48KHz, mono */ - //PJMEDIA_RTP_PT_L16_48KHZ_STEREO, /**< L16 @ 48KHz, stereo */ + PJMEDIA_RTP_PT_SILK_NB, /**< SILK narrowband/8KHz */ + PJMEDIA_RTP_PT_SILK_MB, /**< SILK mediumband/12KHz */ + PJMEDIA_RTP_PT_SILK_WB, /**< SILK wideband/16KHz */ + PJMEDIA_RTP_PT_SILK_SWB, /**< SILK 24KHz */ PJMEDIA_RTP_PT_ILBC, /**< iLBC (13.3/15.2Kbps) */ PJMEDIA_RTP_PT_AMR, /**< AMR (4.75 - 12.2Kbps) */ PJMEDIA_RTP_PT_AMRWB, /**< AMRWB (6.6 - 23.85Kbps)*/ @@ -91,6 +83,18 @@ enum pjmedia_audio_pt PJMEDIA_RTP_PT_G7221C_48, /**< G722.1 Annex C (48Kbps)*/ PJMEDIA_RTP_PT_G7221_RSV1, /**< G722.1 reserve */ PJMEDIA_RTP_PT_G7221_RSV2, /**< G722.1 reserve */ + PJMEDIA_RTP_PT_L16_8KHZ_MONO, /**< L16 @ 8KHz, mono */ + PJMEDIA_RTP_PT_L16_8KHZ_STEREO, /**< L16 @ 8KHz, stereo */ + //PJMEDIA_RTP_PT_L16_11KHZ_MONO, /**< L16 @ 11KHz, mono */ + //PJMEDIA_RTP_PT_L16_11KHZ_STEREO, /**< L16 @ 11KHz, stereo */ + PJMEDIA_RTP_PT_L16_16KHZ_MONO, /**< L16 @ 16KHz, mono */ + PJMEDIA_RTP_PT_L16_16KHZ_STEREO, /**< L16 @ 16KHz, stereo */ + //PJMEDIA_RTP_PT_L16_22KHZ_MONO, /**< L16 @ 22KHz, mono */ + //PJMEDIA_RTP_PT_L16_22KHZ_STEREO, /**< L16 @ 22KHz, stereo */ + //PJMEDIA_RTP_PT_L16_32KHZ_MONO, /**< L16 @ 32KHz, mono */ + //PJMEDIA_RTP_PT_L16_32KHZ_STEREO, /**< L16 @ 32KHz, stereo */ + //PJMEDIA_RTP_PT_L16_48KHZ_MONO, /**< L16 @ 48KHz, mono */ + //PJMEDIA_RTP_PT_L16_48KHZ_STEREO, /**< L16 @ 48KHz, stereo */ /* Caution! * Ensure the value of the last pt above is <= 127. diff --git a/pjmedia/src/pjmedia-codec/audio_codecs.c b/pjmedia/src/pjmedia-codec/audio_codecs.c index 1fbd038a..f338db80 100644 --- a/pjmedia/src/pjmedia-codec/audio_codecs.c +++ b/pjmedia/src/pjmedia-codec/audio_codecs.c @@ -114,6 +114,13 @@ pjmedia_codec_register_audio_codecs(pjmedia_endpt *endpt, return status; #endif +#if PJMEDIA_HAS_SILK_CODEC + /* Register SILK */ + status = pjmedia_codec_silk_init(endpt); + if (status != PJ_SUCCESS) + return status; +#endif + return PJ_SUCCESS; } diff --git a/pjmedia/src/pjmedia-codec/silk.c b/pjmedia/src/pjmedia-codec/silk.c new file mode 100644 index 00000000..7b72bd5c --- /dev/null +++ b/pjmedia/src/pjmedia-codec/silk.c @@ -0,0 +1,805 @@ +/* $Id$ */ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(PJMEDIA_HAS_SILK_CODEC) && (PJMEDIA_HAS_SILK_CODEC!=0) + +#include "SKP_Silk_SDK_API.h" + +#define THIS_FILE "silk.c" + +#define FRAME_LENGTH_MS 20 +#define CALC_BITRATE(max_br) \ + (PJMEDIA_CODEC_SILK_DEFAULT_QUALITY * max_br / 10) + + +/* 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_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; +} 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 = 20000; + 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 = 25000; + 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 = 30000; + 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 = 40000; + 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) +{ + PJ_UNUSED_ARG(clock_rate); + PJ_UNUSED_ARG(opt); + return PJ_ENOTSUP; +} + + +/* + * 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; ienabled && 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; ienabled) + 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_FALSE; + 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 = attr->setting.frm_per_pkt * FRAME_LENGTH_MS * + attr->info.clock_rate / 1000; + + silk->enc_ctl.API_sampleRate = attr->info.clock_rate; + silk->enc_ctl.maxInternalSampleRate = attr->info.clock_rate; + silk->enc_ctl.packetSize = silk->samples_per_frame; + silk->enc_ctl.packetLossPercentage = 0; + 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; + + /* Increase max_bps (as the bitrate values in the table seems to be + * rounded down, and without increasing this, frame size of the jitter + * buffer has been found lower than the actual frame size, which in turn + * it causes noise as the payloads get trimmed). + */ + attr->info.max_bps *= 2; + + 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, + 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; + unsigned count; + + PJ_ASSERT_RETURN(codec && ts && frames && frame_cnt, PJ_EINVAL); + silk = (silk_private*)codec->codec_data; + + PJ_TODO(support_parsing_multiple_frames_per_packet); + + count = 0; + frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO; + frames[count].buf = pkt; + frames[count].size = pkt_size; + frames[count].timestamp.u64 = ts->u64; + ++count; + + *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_int err; + + PJ_ASSERT_RETURN(codec && input && output_buf_len && output, PJ_EINVAL); + silk = (silk_private*)codec->codec_data; + + out_size = (SKP_int16)output_buf_len; + err = SKP_Silk_SDK_Decode(silk->dec_st, &silk->dec_ctl, + 0, /* Normal frame flag */ + input->buf, + input->size, + output->buf, + &out_size); + if (err) { + PJ_LOG(3, (THIS_FILE, "Failed to decode frame (err=%d)", err)); + output->type = PJMEDIA_FRAME_TYPE_NONE; + output->buf = NULL; + output->size = 0; + return PJMEDIA_CODEC_EFAILED; + } + + output->size = out_size; + output->type = PJMEDIA_FRAME_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; + 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; + 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 */ -- cgit v1.2.3