Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-codec/silk.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2012-2012 Teluu Inc. (http://www.teluu.com)
3 * Contributed by Regis Montoya (aka r3gis - www.r3gis.fr)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include <pjmedia-codec/silk.h>
21 #include <pjmedia/codec.h>
22 #include <pjmedia/delaybuf.h>
23 #include <pjmedia/endpoint.h>
24 #include <pjmedia/errno.h>
25 #include <pjmedia/port.h>
26 #include <pj/pool.h>
27 #include <pj/string.h>
28 #include <pj/assert.h>
29 #include <pj/log.h>
30
31 #if defined(PJMEDIA_HAS_SILK_CODEC) && (PJMEDIA_HAS_SILK_CODEC!=0)
32
33 #include "SKP_Silk_SDK_API.h"
34
35 #define THIS_FILE "silk.c"
36
37 #ifndef PJMEDIA_SILK_DELAY_BUF_OPTIONS
38 #define PJMEDIA_SILK_DELAY_BUF_OPTIONS PJMEDIA_DELAY_BUF_SIMPLE_FIFO
39 #endif
40
41 #define FRAME_LENGTH_MS 20
42 #define SILK_ENC_CTL_PACKET_LOSS_PCT 10
43 #define SILK_MIN_BITRATE 5000
44 #define CALC_BITRATE_QUALITY(quality, max_br) \
45 (quality * max_br / 10)
46 #define CALC_BITRATE(max_br) \
47 CALC_BITRATE_QUALITY(PJMEDIA_CODEC_SILK_DEFAULT_QUALITY, \
48 max_br);
49
50
51 /* Prototypes for SILK factory */
52 static pj_status_t silk_test_alloc( pjmedia_codec_factory *factory,
53 const pjmedia_codec_info *id );
54 static pj_status_t silk_default_attr( pjmedia_codec_factory *factory,
55 const pjmedia_codec_info *id,
56 pjmedia_codec_param *attr );
57 static pj_status_t silk_enum_codecs ( pjmedia_codec_factory *factory,
58 unsigned *count,
59 pjmedia_codec_info codecs[]);
60 static pj_status_t silk_alloc_codec( pjmedia_codec_factory *factory,
61 const pjmedia_codec_info *id,
62 pjmedia_codec **p_codec);
63 static pj_status_t silk_dealloc_codec( pjmedia_codec_factory *factory,
64 pjmedia_codec *codec );
65
66 /* Prototypes for SILK implementation. */
67 static pj_status_t silk_codec_init( pjmedia_codec *codec,
68 pj_pool_t *pool );
69 static pj_status_t silk_codec_open( pjmedia_codec *codec,
70 pjmedia_codec_param *attr );
71 static pj_status_t silk_codec_close( pjmedia_codec *codec );
72 static pj_status_t silk_codec_modify( pjmedia_codec *codec,
73 const pjmedia_codec_param *attr );
74 static pj_status_t silk_codec_parse( pjmedia_codec *codec,
75 void *pkt,
76 pj_size_t pkt_size,
77 const pj_timestamp *timestamp,
78 unsigned *frame_cnt,
79 pjmedia_frame frames[]);
80 static pj_status_t silk_codec_encode( pjmedia_codec *codec,
81 const struct pjmedia_frame *input,
82 unsigned output_buf_len,
83 struct pjmedia_frame *output);
84 static pj_status_t silk_codec_decode( pjmedia_codec *codec,
85 const struct pjmedia_frame *input,
86 unsigned output_buf_len,
87 struct pjmedia_frame *output);
88 static pj_status_t silk_codec_recover( pjmedia_codec *codec,
89 unsigned output_buf_len,
90 struct pjmedia_frame *output);
91
92
93 typedef enum
94 {
95 PARAM_NB, /* Index for narrowband parameter. */
96 PARAM_MB, /* Index for medium parameter. */
97 PARAM_WB, /* Index for wideband parameter. */
98 PARAM_SWB, /* Index for super-wideband parameter */
99 } silk_mode;
100
101
102 /* Silk default parameter */
103 typedef struct silk_param
104 {
105 int enabled; /* Is this mode enabled? */
106 int pt; /* Payload type. */
107 unsigned clock_rate; /* Default sampling rate to be used. */
108 pj_uint16_t ptime; /* packet length (in ms). */
109 pj_uint32_t bitrate; /* Bit rate for current mode. */
110 pj_uint32_t max_bitrate; /* Max bit rate for current mode. */
111 int complexity; /* Complexity mode: 0/lowest to 2. */
112 } silk_param;
113
114
115 /* Definition for SILK codec operations. */
116 static pjmedia_codec_op silk_op =
117 {
118 &silk_codec_init,
119 &silk_codec_open,
120 &silk_codec_close,
121 &silk_codec_modify,
122 &silk_codec_parse,
123 &silk_codec_encode,
124 &silk_codec_decode,
125 &silk_codec_recover
126 };
127
128 /* Definition for SILK codec factory operations. */
129 static pjmedia_codec_factory_op silk_factory_op =
130 {
131 &silk_test_alloc,
132 &silk_default_attr,
133 &silk_enum_codecs,
134 &silk_alloc_codec,
135 &silk_dealloc_codec,
136 &pjmedia_codec_silk_deinit
137 };
138
139
140 /* SILK factory private data */
141 static struct silk_factory
142 {
143 pjmedia_codec_factory base;
144 pjmedia_endpt *endpt;
145 pj_pool_t *pool;
146 pj_mutex_t *mutex;
147 struct silk_param silk_param[4];
148 } silk_factory;
149
150
151 /* SILK codec private data. */
152 typedef struct silk_private
153 {
154 silk_mode mode; /**< Silk mode. */
155 pj_pool_t *pool; /**< Pool for each instance. */
156 unsigned samples_per_frame;
157 pj_uint8_t pcm_bytes_per_sample;
158
159 pj_bool_t enc_ready;
160 SKP_SILK_SDK_EncControlStruct enc_ctl;
161 void *enc_st;
162
163 pj_bool_t dec_ready;
164 SKP_SILK_SDK_DecControlStruct dec_ctl;
165 void *dec_st;
166
167 /* Buffer to hold decoded frames. */
168 void *dec_buf[SILK_MAX_FRAMES_PER_PACKET-1];
169 SKP_int16 dec_buf_size[SILK_MAX_FRAMES_PER_PACKET-1];
170 pj_size_t dec_buf_sz;
171 unsigned dec_buf_cnt;
172 pj_uint32_t pkt_info; /**< Packet info for buffered frames. */
173 } silk_private;
174
175
176 silk_mode silk_get_mode_from_clock_rate(unsigned clock_rate) {
177 if (clock_rate <= silk_factory.silk_param[PARAM_NB].clock_rate) {
178 return PARAM_NB;
179 } else if (clock_rate <= silk_factory.silk_param[PARAM_MB].clock_rate) {
180 return PARAM_MB;
181 } else if (clock_rate <= silk_factory.silk_param[PARAM_WB].clock_rate) {
182 return PARAM_WB;
183 }
184 return PARAM_SWB;
185 }
186
187
188 PJ_DEF(pj_status_t) pjmedia_codec_silk_init(pjmedia_endpt *endpt)
189 {
190 pjmedia_codec_mgr *codec_mgr;
191 silk_param *sp;
192 pj_status_t status;
193
194 if (silk_factory.endpt != NULL) {
195 /* Already initialized. */
196 return PJ_SUCCESS;
197 }
198
199 /* Init factory */
200 pj_bzero(&silk_factory, sizeof(silk_factory));
201 silk_factory.base.op = &silk_factory_op;
202 silk_factory.base.factory_data = NULL;
203 silk_factory.endpt = endpt;
204
205 /* Create pool */
206 silk_factory.pool = pjmedia_endpt_create_pool(endpt, "silk", 4000, 4000);
207 if (!silk_factory.pool)
208 return PJ_ENOMEM;
209
210 /* Create mutex. */
211 status = pj_mutex_create_simple(silk_factory.pool, "silk",
212 &silk_factory.mutex);
213 if (status != PJ_SUCCESS)
214 goto on_error;
215
216 /* Initialize default codec params */
217
218 /* From SILK docs:
219 - SILK bitrate tables:
220 +----------------+---------+-----------+
221 | | fs (Hz) | BR (kbps) |
222 +----------------+---------+-----------+
223 | Narrowband | 8000 | 6 - 20 |
224 | Mediumband | 12000 | 7 - 25 |
225 | Wideband | 16000 | 8 - 30 |
226 | Super Wideband | 24000 | 12 - 40 |
227 +----------------+---------+-----------+
228 - The upper limits of the bit rate ranges in this table are
229 recommended values.
230 */
231
232 sp = &silk_factory.silk_param[PARAM_NB];
233 sp->pt = PJMEDIA_RTP_PT_SILK_NB;
234 sp->clock_rate = 8000;
235 sp->max_bitrate = 22000;
236 sp->bitrate = CALC_BITRATE(sp->max_bitrate);
237 sp->ptime = FRAME_LENGTH_MS;
238 sp->complexity = PJMEDIA_CODEC_SILK_DEFAULT_COMPLEXITY;
239 sp->enabled = 1;
240
241 sp = &silk_factory.silk_param[PARAM_MB];
242 sp->pt = PJMEDIA_RTP_PT_SILK_MB;
243 sp->clock_rate = 12000;
244 sp->max_bitrate = 28000;
245 sp->bitrate = CALC_BITRATE(sp->max_bitrate);
246 sp->ptime = FRAME_LENGTH_MS;
247 sp->complexity = PJMEDIA_CODEC_SILK_DEFAULT_COMPLEXITY;
248 sp->enabled = 0;
249
250 sp = &silk_factory.silk_param[PARAM_WB];
251 sp->pt = PJMEDIA_RTP_PT_SILK_WB;
252 sp->clock_rate = 16000;
253 sp->max_bitrate = 36000;
254 sp->bitrate = CALC_BITRATE(sp->max_bitrate);
255 sp->ptime = FRAME_LENGTH_MS;
256 sp->complexity = PJMEDIA_CODEC_SILK_DEFAULT_COMPLEXITY;
257 sp->enabled = 1;
258
259 sp = &silk_factory.silk_param[PARAM_SWB];
260 sp->pt = PJMEDIA_RTP_PT_SILK_SWB;
261 sp->clock_rate = 24000;
262 sp->max_bitrate = 46000;
263 sp->bitrate = CALC_BITRATE(sp->max_bitrate);
264 sp->ptime = FRAME_LENGTH_MS;
265 sp->complexity = PJMEDIA_CODEC_SILK_DEFAULT_COMPLEXITY;
266 sp->enabled = 0;
267
268
269 /* Get the codec manager. */
270 codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
271 if (!codec_mgr) {
272 return PJ_EINVALIDOP;
273 }
274
275 /* Register codec factory to endpoint. */
276 status = pjmedia_codec_mgr_register_factory(codec_mgr,
277 &silk_factory.base);
278 if (status != PJ_SUCCESS)
279 return status;
280
281 PJ_LOG(4,(THIS_FILE, "SILK codec version %s initialized",
282 SKP_Silk_SDK_get_version()));
283 return PJ_SUCCESS;
284
285 on_error:
286 if (silk_factory.mutex) {
287 pj_mutex_destroy(silk_factory.mutex);
288 silk_factory.mutex = NULL;
289 }
290 if (silk_factory.pool) {
291 pj_pool_release(silk_factory.pool);
292 silk_factory.pool = NULL;
293 }
294
295 return status;
296 }
297
298
299 /*
300 * Change the configuration setting of the SILK codec for the specified
301 * clock rate.
302 */
303 PJ_DEF(pj_status_t) pjmedia_codec_silk_set_config(
304 unsigned clock_rate,
305 const pjmedia_codec_silk_setting *opt)
306 {
307 unsigned i;
308
309 /* Look up in factory modes table */
310 for (i = 0; i < PJ_ARRAY_SIZE(silk_factory.silk_param); ++i)
311 {
312 if (silk_factory.silk_param[i].clock_rate == clock_rate) {
313 int quality = PJMEDIA_CODEC_SILK_DEFAULT_QUALITY;
314 int complexity = PJMEDIA_CODEC_SILK_DEFAULT_COMPLEXITY;
315
316 silk_factory.silk_param[i].enabled = opt->enabled;
317 if (opt->complexity >= 0)
318 complexity = opt->complexity;
319 silk_factory.silk_param[i].complexity = complexity;
320 if (opt->quality >= 0)
321 quality = opt->quality;
322 silk_factory.silk_param[i].bitrate =
323 CALC_BITRATE_QUALITY(quality,
324 silk_factory.silk_param[i].max_bitrate);
325 if (silk_factory.silk_param[i].bitrate < SILK_MIN_BITRATE)
326 silk_factory.silk_param[i].bitrate = SILK_MIN_BITRATE;
327
328 return PJ_SUCCESS;
329 }
330 }
331
332 return PJ_ENOTFOUND;
333 }
334
335
336 /*
337 * Unregister SILK codec factory from pjmedia endpoint and deinitialize
338 * the SILK codec library.
339 */
340 PJ_DEF(pj_status_t) pjmedia_codec_silk_deinit(void)
341 {
342 pjmedia_codec_mgr *codec_mgr;
343 pj_status_t status;
344
345 if (silk_factory.endpt == NULL) {
346 /* Not registered. */
347 return PJ_SUCCESS;
348 }
349
350 /* Lock mutex. */
351 pj_mutex_lock(silk_factory.mutex);
352
353 /* Get the codec manager. */
354 codec_mgr = pjmedia_endpt_get_codec_mgr(silk_factory.endpt);
355 if (!codec_mgr) {
356 silk_factory.endpt = NULL;
357 pj_mutex_unlock(silk_factory.mutex);
358 return PJ_EINVALIDOP;
359 }
360
361 /* Release the factory mutex before calling unregister to avoid lock
362 * order inversion with codec manager mutex.
363 */
364 pj_mutex_unlock(silk_factory.mutex);
365
366 /* Unregister silk codec factory. */
367 status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
368 &silk_factory.base);
369 silk_factory.endpt = NULL;
370
371 /* Destroy mutex. */
372 pj_mutex_destroy(silk_factory.mutex);
373 silk_factory.mutex = NULL;
374
375
376 /* Release pool. */
377 pj_pool_release(silk_factory.pool);
378 silk_factory.pool = NULL;
379
380 return status;
381 }
382
383
384 /*
385 * Check if factory can allocate the specified codec.
386 */
387 static pj_status_t silk_test_alloc(pjmedia_codec_factory *factory,
388 const pjmedia_codec_info *info )
389 {
390 const pj_str_t silk_tag = {"SILK", 4};
391 unsigned i;
392
393 PJ_UNUSED_ARG(factory);
394 PJ_ASSERT_RETURN(factory==&silk_factory.base, PJ_EINVAL);
395
396 /* Type MUST be audio. */
397 if (info->type != PJMEDIA_TYPE_AUDIO)
398 return PJMEDIA_CODEC_EUNSUP;
399
400 /* Check encoding name. */
401 if (pj_stricmp(&info->encoding_name, &silk_tag) != 0)
402 return PJMEDIA_CODEC_EUNSUP;
403
404 /* Channel count must be one */
405 if (info->channel_cnt != 1)
406 return PJMEDIA_CODEC_EUNSUP;
407
408 /* Check clock-rate */
409 for (i=0; i<PJ_ARRAY_SIZE(silk_factory.silk_param); ++i) {
410 silk_param *sp = &silk_factory.silk_param[i];
411 if (sp->enabled && info->clock_rate == sp->clock_rate)
412 {
413 return PJ_SUCCESS;
414 }
415 }
416 /* Clock rate not supported */
417 return PJMEDIA_CODEC_EUNSUP;
418 }
419
420
421 /*
422 * Generate default attribute.
423 */
424 static pj_status_t silk_default_attr( pjmedia_codec_factory *factory,
425 const pjmedia_codec_info *id,
426 pjmedia_codec_param *attr )
427 {
428 silk_param *sp;
429 int i;
430
431 PJ_ASSERT_RETURN(factory==&silk_factory.base, PJ_EINVAL);
432
433 i = silk_get_mode_from_clock_rate(id->clock_rate);
434 pj_assert(i >= PARAM_NB && i <= PARAM_SWB);
435
436 sp = &silk_factory.silk_param[i];
437
438 pj_bzero(attr, sizeof(pjmedia_codec_param));
439 attr->info.channel_cnt = 1;
440 attr->info.clock_rate = sp->clock_rate;
441 attr->info.avg_bps = sp->bitrate;
442 attr->info.max_bps = sp->max_bitrate;
443 attr->info.frm_ptime = sp->ptime;
444 attr->info.pcm_bits_per_sample = 16;
445 attr->info.pt = (pj_uint8_t) sp->pt;
446 attr->setting.frm_per_pkt = 1;
447 attr->setting.vad = 0; /* DTX is not recommended for quality reason */
448 attr->setting.plc = 1;
449
450 i = 0;
451 attr->setting.dec_fmtp.param[i].name = pj_str("useinbandfec");
452 attr->setting.dec_fmtp.param[i++].val = pj_str("0");
453 /*
454 attr->setting.dec_fmtp.param[i].name = pj_str("maxaveragebitrate");
455 attr->setting.dec_fmtp.param[i++].val = pj_str(mode->bitrate_str);
456 */
457 attr->setting.dec_fmtp.cnt = (pj_uint8_t)i;
458
459 return PJ_SUCCESS;
460 }
461
462
463 /*
464 * Enum codecs supported by this factory.
465 */
466 static pj_status_t silk_enum_codecs(pjmedia_codec_factory *factory,
467 unsigned *count,
468 pjmedia_codec_info codecs[])
469 {
470 unsigned max;
471 int i;
472
473 PJ_ASSERT_RETURN(factory==&silk_factory.base, PJ_EINVAL);
474 PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL);
475
476 max = *count;
477 *count = 0;
478
479 for (i = 0; i<(int)PJ_ARRAY_SIZE(silk_factory.silk_param) && *count<max;
480 ++i)
481 {
482 silk_param *sp = &silk_factory.silk_param[i];
483
484 if (!sp->enabled)
485 continue;
486
487 pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info));
488 codecs[*count].encoding_name = pj_str("SILK");
489 codecs[*count].pt = sp->pt;
490 codecs[*count].type = PJMEDIA_TYPE_AUDIO;
491 codecs[*count].clock_rate = sp->clock_rate;
492 codecs[*count].channel_cnt = 1;
493
494 ++*count;
495 }
496
497 return PJ_SUCCESS;
498 }
499
500
501 /*
502 * Allocate a new SILK codec instance.
503 */
504 static pj_status_t silk_alloc_codec(pjmedia_codec_factory *factory,
505 const pjmedia_codec_info *id,
506 pjmedia_codec **p_codec)
507 {
508 pj_pool_t *pool;
509 pjmedia_codec *codec;
510 silk_private *silk;
511
512 PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL);
513 PJ_ASSERT_RETURN(factory == &silk_factory.base, PJ_EINVAL);
514
515 /* Create pool for codec instance */
516 pool = pjmedia_endpt_create_pool(silk_factory.endpt, "silk", 512, 512);
517
518 /* Allocate codec */
519 codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec);
520 codec->op = &silk_op;
521 codec->factory = factory;
522 codec->codec_data = PJ_POOL_ZALLOC_T(pool, silk_private);
523 silk = (silk_private*) codec->codec_data;
524 silk->pool = pool;
525 silk->enc_ready = PJ_FALSE;
526 silk->dec_ready = PJ_FALSE;
527 silk->mode = silk_get_mode_from_clock_rate(id->clock_rate);
528
529 *p_codec = codec;
530 return PJ_SUCCESS;
531 }
532
533
534 /*
535 * Free codec.
536 */
537 static pj_status_t silk_dealloc_codec( pjmedia_codec_factory *factory,
538 pjmedia_codec *codec )
539 {
540 silk_private *silk;
541
542 PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL);
543 PJ_ASSERT_RETURN(factory == &silk_factory.base, PJ_EINVAL);
544
545 silk = (silk_private*)codec->codec_data;
546
547 /* Close codec, if it's not closed. */
548 if (silk->enc_ready == PJ_TRUE || silk->dec_ready == PJ_TRUE) {
549 silk_codec_close(codec);
550 }
551
552 pj_pool_release(silk->pool);
553
554 return PJ_SUCCESS;
555 }
556
557
558 /*
559 * Init codec.
560 */
561 static pj_status_t silk_codec_init(pjmedia_codec *codec,
562 pj_pool_t *pool )
563 {
564 PJ_UNUSED_ARG(codec);
565 PJ_UNUSED_ARG(pool);
566 return PJ_SUCCESS;
567 }
568
569
570 /*
571 * Open codec.
572 */
573 static pj_status_t silk_codec_open(pjmedia_codec *codec,
574 pjmedia_codec_param *attr )
575 {
576
577 silk_private *silk;
578 silk_param *sp;
579 SKP_int st_size, err;
580 pj_bool_t enc_use_fec;
581 unsigned enc_bitrate, i;
582
583 PJ_ASSERT_RETURN(codec && attr, PJ_EINVAL);
584
585 silk = (silk_private*)codec->codec_data;
586 sp = &silk_factory.silk_param[silk->mode];
587
588 /* Already opened? */
589 if (silk->enc_ready || silk->dec_ready)
590 return PJ_SUCCESS;
591
592 /* Allocate and initialize encoder */
593 err = SKP_Silk_SDK_Get_Encoder_Size(&st_size);
594 if (err) {
595 PJ_LOG(3,(THIS_FILE, "Failed to get encoder state size (err=%d)",
596 err));
597 return PJMEDIA_CODEC_EFAILED;
598 }
599 silk->enc_st = pj_pool_zalloc(silk->pool, st_size);
600 err = SKP_Silk_SDK_InitEncoder(silk->enc_st, &silk->enc_ctl);
601 if (err) {
602 PJ_LOG(3,(THIS_FILE, "Failed to init encoder (err=%d)", err));
603 return PJMEDIA_CODEC_EFAILED;
604 }
605
606 /* Check fmtp params */
607 enc_use_fec = PJ_TRUE;
608 enc_bitrate = sp->bitrate;
609 for (i = 0; i < attr->setting.enc_fmtp.cnt; ++i) {
610 pjmedia_codec_fmtp *fmtp = &attr->setting.enc_fmtp;
611 const pj_str_t STR_USEINBANDFEC = {"useinbandfec", 12};
612 const pj_str_t STR_MAXAVERAGEBITRATE = {"maxaveragebitrate", 17};
613
614 if (!pj_stricmp(&fmtp->param[i].name, &STR_USEINBANDFEC)) {
615 enc_use_fec = pj_strtoul(&fmtp->param[i].val) != 0;
616 } else if (!pj_stricmp(&fmtp->param[i].name, &STR_MAXAVERAGEBITRATE)) {
617 enc_bitrate = pj_strtoul(&fmtp->param[i].val);
618 if (enc_bitrate > sp->max_bitrate) {
619 enc_bitrate = sp->max_bitrate;
620 }
621 }
622 }
623
624 /* Setup encoder control for encoding process */
625 silk->enc_ready = PJ_TRUE;
626 silk->samples_per_frame = FRAME_LENGTH_MS *
627 attr->info.clock_rate / 1000;
628 silk->pcm_bytes_per_sample = attr->info.pcm_bits_per_sample / 8;
629
630 silk->enc_ctl.API_sampleRate = attr->info.clock_rate;
631 silk->enc_ctl.maxInternalSampleRate = attr->info.clock_rate;
632 silk->enc_ctl.packetSize = attr->setting.frm_per_pkt *
633 silk->samples_per_frame;
634 /* For useInBandFEC setting to be useful, we need to set
635 * packetLossPercentage greater than LBRR_LOSS_THRES (1)
636 */
637 silk->enc_ctl.packetLossPercentage = SILK_ENC_CTL_PACKET_LOSS_PCT;
638 silk->enc_ctl.useInBandFEC = enc_use_fec;
639 silk->enc_ctl.useDTX = attr->setting.vad;
640 silk->enc_ctl.complexity = sp->complexity;
641 silk->enc_ctl.bitRate = enc_bitrate;
642
643
644 /* Allocate and initialize decoder */
645 err = SKP_Silk_SDK_Get_Decoder_Size(&st_size);
646 if (err) {
647 PJ_LOG(3,(THIS_FILE, "Failed to get decoder state size (err=%d)",
648 err));
649 return PJMEDIA_CODEC_EFAILED;
650 }
651 silk->dec_st = pj_pool_zalloc(silk->pool, st_size);
652 err = SKP_Silk_SDK_InitDecoder(silk->dec_st);
653 if (err) {
654 PJ_LOG(3,(THIS_FILE, "Failed to init decoder (err=%d)", err));
655 return PJMEDIA_CODEC_EFAILED;
656 }
657
658 /* Setup decoder control for decoding process */
659 silk->dec_ctl.API_sampleRate = attr->info.clock_rate;
660 silk->dec_ctl.framesPerPacket = 1; /* for proper PLC at start */
661 silk->dec_ready = PJ_TRUE;
662 silk->dec_buf_sz = attr->info.clock_rate * attr->info.channel_cnt *
663 attr->info.frm_ptime / 1000 *
664 silk->pcm_bytes_per_sample;
665
666 /* Inform the stream to prepare a larger buffer since we cannot parse
667 * SILK packets and split it into individual frames.
668 */
669 attr->info.max_rx_frame_size = attr->info.max_bps *
670 attr->info.frm_ptime / 8 / 1000;
671 if ((attr->info.max_bps * attr->info.frm_ptime) % 8000 != 0)
672 {
673 ++attr->info.max_rx_frame_size;
674 }
675 attr->info.max_rx_frame_size *= SILK_MAX_FRAMES_PER_PACKET;
676
677 return PJ_SUCCESS;
678 }
679
680
681 /*
682 * Close codec.
683 */
684 static pj_status_t silk_codec_close( pjmedia_codec *codec )
685 {
686 silk_private *silk;
687 silk = (silk_private*)codec->codec_data;
688
689 silk->enc_ready = PJ_FALSE;
690 silk->dec_ready = PJ_FALSE;
691
692 return PJ_SUCCESS;
693 }
694
695
696 /*
697 * Modify codec settings.
698 */
699 static pj_status_t silk_codec_modify(pjmedia_codec *codec,
700 const pjmedia_codec_param *attr )
701 {
702 PJ_TODO(implement_silk_codec_modify);
703
704 PJ_UNUSED_ARG(codec);
705 PJ_UNUSED_ARG(attr);
706
707 return PJ_ENOTSUP;
708 }
709
710
711 /*
712 * Encode frame.
713 */
714 static pj_status_t silk_codec_encode(pjmedia_codec *codec,
715 const struct pjmedia_frame *input,
716 unsigned output_buf_len,
717 struct pjmedia_frame *output)
718 {
719 silk_private *silk;
720 SKP_int err;
721 unsigned nsamples;
722 SKP_int16 out_size;
723
724 PJ_ASSERT_RETURN(codec && input && output_buf_len && output, PJ_EINVAL);
725 silk = (silk_private*)codec->codec_data;
726
727 /* Check frame in size */
728 nsamples = input->size >> 1;
729 PJ_ASSERT_RETURN(nsamples % silk->samples_per_frame == 0,
730 PJMEDIA_CODEC_EPCMFRMINLEN);
731
732 /* Encode */
733 output->size = 0;
734 out_size = (SKP_int16)output_buf_len;
735 err = SKP_Silk_SDK_Encode(silk->enc_st, &silk->enc_ctl,
736 (SKP_int16*)input->buf, nsamples,
737 (SKP_uint8*)output->buf, &out_size);
738 if (err) {
739 PJ_LOG(3, (THIS_FILE, "Failed to encode frame (err=%d)", err));
740 output->type = PJMEDIA_FRAME_TYPE_NONE;
741 if (err == SKP_SILK_ENC_PAYLOAD_BUF_TOO_SHORT)
742 return PJMEDIA_CODEC_EFRMTOOSHORT;
743 return PJMEDIA_CODEC_EFAILED;
744 }
745
746 output->size = out_size;
747 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
748 output->timestamp = input->timestamp;
749
750 return PJ_SUCCESS;
751 }
752
753
754 /*
755 * Get frames in the packet.
756 */
757 static pj_status_t silk_codec_parse( pjmedia_codec *codec,
758 void *pkt,
759 pj_size_t pkt_size,
760 const pj_timestamp *ts,
761 unsigned *frame_cnt,
762 pjmedia_frame frames[])
763 {
764 silk_private *silk;
765 SKP_Silk_TOC_struct toc;
766 unsigned i, count;
767
768 PJ_ASSERT_RETURN(codec && ts && frames && frame_cnt, PJ_EINVAL);
769 silk = (silk_private*)codec->codec_data;
770
771 SKP_Silk_SDK_get_TOC(pkt, pkt_size, &toc);
772 count = toc.framesInPacket;
773 pj_assert(count <= SILK_MAX_FRAMES_PER_PACKET);
774 if (count > *frame_cnt)
775 count = *frame_cnt;
776
777 for (i = 0; i < count; i++) {
778 frames[i].type = PJMEDIA_FRAME_TYPE_AUDIO;
779 frames[i].bit_info = (((unsigned)ts->u64 & 0xFFFF) << 16) |
780 (((pj_size_t)pkt & 0xFF) << 8) |
781 (toc.framesInPacket << 4) | i;
782 frames[i].buf = pkt;
783 frames[i].size = pkt_size;
784 frames[i].timestamp.u64 = ts->u64 + i * silk->samples_per_frame;
785 }
786
787 *frame_cnt = count;
788 return PJ_SUCCESS;
789 }
790
791 static pj_status_t silk_codec_decode(pjmedia_codec *codec,
792 const struct pjmedia_frame *input,
793 unsigned output_buf_len,
794 struct pjmedia_frame *output)
795 {
796 silk_private *silk;
797 SKP_int16 out_size;
798 SKP_Silk_TOC_struct toc;
799 SKP_int err = 0;
800 unsigned pkt_info, frm_info;
801
802 PJ_ASSERT_RETURN(codec && input && output_buf_len && output, PJ_EINVAL);
803 silk = (silk_private*)codec->codec_data;
804 PJ_ASSERT_RETURN(output_buf_len >= silk->dec_buf_sz, PJ_ETOOSMALL);
805
806 SKP_Silk_SDK_get_TOC(input->buf, input->size, &toc);
807 pkt_info = input->bit_info & 0xFFFFFF00;
808 frm_info = input->bit_info & 0xF;
809
810 if (toc.framesInPacket == 0) {
811 /* In SILK ARM version, the table of content can indicate
812 * that the number of frames in the packet is 0.
813 * Try to get the number of frames in packet that we save
814 * in the frame instead.
815 */
816 toc.framesInPacket = (input->bit_info & 0xF0) >> 4;
817 }
818
819 if (toc.framesInPacket == 0) {
820 output->size = 0;
821 } else if (silk->pkt_info != pkt_info || input->bit_info == 0) {
822 unsigned i;
823 SKP_int16 nsamples;
824
825 silk->pkt_info = pkt_info;
826 nsamples = (SKP_int16)silk->dec_buf_sz / silk->pcm_bytes_per_sample;
827
828 if (toc.framesInPacket-1 > (SKP_int)silk->dec_buf_cnt) {
829 /* Grow the buffer */
830 for (i = silk->dec_buf_cnt+1; i < (unsigned)toc.framesInPacket;
831 i++)
832 {
833 silk->dec_buf[i-1] = pj_pool_alloc(silk->pool,
834 silk->dec_buf_sz);
835 }
836 silk->dec_buf_cnt = toc.framesInPacket-1;
837 }
838
839 /* We need to decode all the frames in the packet. */
840 for (i = 0; i < (unsigned)toc.framesInPacket;) {
841 void *buf;
842 SKP_int16 *size;
843
844 if (i == 0 || i == frm_info) {
845 buf = output->buf;
846 size = &out_size;
847 } else {
848 buf = silk->dec_buf[i-1];
849 size = &silk->dec_buf_size[i-1];
850 }
851
852 *size = nsamples;
853 err = SKP_Silk_SDK_Decode(silk->dec_st, &silk->dec_ctl,
854 0, /* Normal frame flag */
855 input->buf, input->size,
856 buf, size);
857 if (err) {
858 PJ_LOG(3, (THIS_FILE, "Failed to decode frame (err=%d)",
859 err));
860 *size = 0;
861 } else {
862 *size = *size * silk->pcm_bytes_per_sample;
863 }
864
865 if (i == frm_info) {
866 output->size = *size;
867 }
868
869 i++;
870 if (!silk->dec_ctl.moreInternalDecoderFrames &&
871 i < (unsigned)toc.framesInPacket)
872 {
873 /* It turns out that the packet does not have
874 * the number of frames as mentioned in the TOC.
875 */
876 for (; i < (unsigned)toc.framesInPacket; i++) {
877 silk->dec_buf_size[i-1] = 0;
878 if (i == frm_info) {
879 output->size = 0;
880 }
881 }
882 }
883 }
884 } else {
885 /* We have already decoded this packet. */
886 if (frm_info == 0 || silk->dec_buf_size[frm_info-1] == 0) {
887 /* The decoding was a failure. */
888 output->size = 0;
889 } else {
890 /* Copy the decoded frame from the buffer. */
891 pj_assert(frm_info-1 < silk->dec_buf_cnt);
892 if (frm_info-1 >= silk->dec_buf_cnt) {
893 output->size = 0;
894 } else {
895 pj_memcpy(output->buf, silk->dec_buf[frm_info-1],
896 silk->dec_buf_size[frm_info-1]);
897 output->size = silk->dec_buf_size[frm_info-1];
898 }
899 }
900 }
901
902 if (output->size == 0) {
903 output->type = PJMEDIA_FRAME_TYPE_NONE;
904 output->buf = NULL;
905 return PJMEDIA_CODEC_EFAILED;
906 }
907
908 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
909 output->timestamp = input->timestamp;
910
911 return PJ_SUCCESS;
912 }
913
914
915 /*
916 * Recover lost frame.
917 */
918 static pj_status_t silk_codec_recover(pjmedia_codec *codec,
919 unsigned output_buf_len,
920 struct pjmedia_frame *output)
921 {
922 silk_private *silk;
923 SKP_int16 out_size;
924 SKP_int err;
925
926 PJ_ASSERT_RETURN(codec && output_buf_len && output, PJ_EINVAL);
927 silk = (silk_private*)codec->codec_data;
928
929 out_size = (SKP_int16)output_buf_len / silk->pcm_bytes_per_sample;
930 err = SKP_Silk_SDK_Decode(silk->dec_st, &silk->dec_ctl,
931 1, /* Lost frame flag */
932 NULL,
933 0,
934 output->buf,
935 &out_size);
936 if (err) {
937 PJ_LOG(3, (THIS_FILE, "Failed to conceal lost frame (err=%d)", err));
938 output->type = PJMEDIA_FRAME_TYPE_NONE;
939 output->buf = NULL;
940 output->size = 0;
941 return PJMEDIA_CODEC_EFAILED;
942 }
943
944 output->size = out_size * silk->pcm_bytes_per_sample;
945 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
946
947 return PJ_SUCCESS;
948 }
949
950 #if defined(_MSC_VER)
951 # if PJ_DEBUG
952 # pragma comment(lib, "SKP_Silk_FLP_Win32_debug.lib")
953 # else
954 # pragma comment(lib, "SKP_Silk_FLP_Win32_mt.lib")
955 # endif
956 #endif
957
958
959 #endif /* PJMEDIA_HAS_SILK_CODEC */