diff options
author | Nanang Izzuddin <nanang@teluu.com> | 2012-09-24 06:58:16 +0000 |
---|---|---|
committer | Nanang Izzuddin <nanang@teluu.com> | 2012-09-24 06:58:16 +0000 |
commit | 31425448324e10b5fb02557a9f1d5cc86df7bed3 (patch) | |
tree | 93edaaebaffa076a067eb38e65358e6a5158b3f6 /pjmedia/src/pjmedia-codec/silk.c | |
parent | ec7c46ffe515eb8613470c9956158ade6829b941 (diff) |
Re #1586: Initial version of SILK wrapper.
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@4264 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjmedia/src/pjmedia-codec/silk.c')
-rw-r--r-- | pjmedia/src/pjmedia-codec/silk.c | 805 |
1 files changed, 805 insertions, 0 deletions
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 <pjmedia-codec/silk.h> +#include <pjmedia/codec.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" + +#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; 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_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 */ |