Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2011-2013 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2011 Dan Arrhenius <[email protected]>
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 /*
21 * AMR codec implementation with OpenCORE AMR library
22 */
23 #include <pjmedia-codec/g722.h>
24 #include <pjmedia-codec/amr_sdp_match.h>
25 #include <pjmedia/codec.h>
26 #include <pjmedia/errno.h>
27 #include <pjmedia/endpoint.h>
28 #include <pjmedia/plc.h>
29 #include <pjmedia/port.h>
30 #include <pjmedia/silencedet.h>
31 #include <pj/assert.h>
32 #include <pj/log.h>
33 #include <pj/pool.h>
34 #include <pj/string.h>
35 #include <pj/os.h>
36 #include <pj/math.h>
37
38 #if defined(PJMEDIA_HAS_OPENCORE_AMRNB_CODEC) && \
39 (PJMEDIA_HAS_OPENCORE_AMRNB_CODEC != 0)
40 #define USE_AMRNB
41 #endif
42
43 #if defined(PJMEDIA_HAS_OPENCORE_AMRWB_CODEC) && \
44 (PJMEDIA_HAS_OPENCORE_AMRWB_CODEC != 0)
45 #define USE_AMRWB
46 #endif
47
48 #if defined(USE_AMRNB) || defined(USE_AMRWB)
49
50 #ifdef USE_AMRNB
51 #include <opencore-amrnb/interf_enc.h>
52 #include <opencore-amrnb/interf_dec.h>
53 #endif
54
55 #ifdef USE_AMRWB
56 #include <vo-amrwbenc/enc_if.h>
57 #include <opencore-amrwb/dec_if.h>
58 #endif
59
60 #include <pjmedia-codec/amr_helper.h>
61 #include <pjmedia-codec/opencore_amr.h>
62
63 #define THIS_FILE "opencore_amr.c"
64
65 /* Tracing */
66 #define PJ_TRACE 0
67
68 #if PJ_TRACE
69 # define TRACE_(expr) PJ_LOG(4,expr)
70 #else
71 # define TRACE_(expr)
72 #endif
73
74 /* Use PJMEDIA PLC */
75 #define USE_PJMEDIA_PLC 1
76
77 #define FRAME_LENGTH_MS 20
78
79
80 /* Prototypes for AMR factory */
81 static pj_status_t amr_test_alloc(pjmedia_codec_factory *factory,
82 const pjmedia_codec_info *id );
83 static pj_status_t amr_default_attr(pjmedia_codec_factory *factory,
84 const pjmedia_codec_info *id,
85 pjmedia_codec_param *attr );
86 static pj_status_t amr_enum_codecs(pjmedia_codec_factory *factory,
87 unsigned *count,
88 pjmedia_codec_info codecs[]);
89 static pj_status_t amr_alloc_codec(pjmedia_codec_factory *factory,
90 const pjmedia_codec_info *id,
91 pjmedia_codec **p_codec);
92 static pj_status_t amr_dealloc_codec(pjmedia_codec_factory *factory,
93 pjmedia_codec *codec );
94
95 /* Prototypes for AMR implementation. */
96 static pj_status_t amr_codec_init(pjmedia_codec *codec,
97 pj_pool_t *pool );
98 static pj_status_t amr_codec_open(pjmedia_codec *codec,
99 pjmedia_codec_param *attr );
100 static pj_status_t amr_codec_close(pjmedia_codec *codec );
101 static pj_status_t amr_codec_modify(pjmedia_codec *codec,
102 const pjmedia_codec_param *attr );
103 static pj_status_t amr_codec_parse(pjmedia_codec *codec,
104 void *pkt,
105 pj_size_t pkt_size,
106 const pj_timestamp *ts,
107 unsigned *frame_cnt,
108 pjmedia_frame frames[]);
109 static pj_status_t amr_codec_encode(pjmedia_codec *codec,
110 const struct pjmedia_frame *input,
111 unsigned output_buf_len,
112 struct pjmedia_frame *output);
113 static pj_status_t amr_codec_decode(pjmedia_codec *codec,
114 const struct pjmedia_frame *input,
115 unsigned output_buf_len,
116 struct pjmedia_frame *output);
117 static pj_status_t amr_codec_recover(pjmedia_codec *codec,
118 unsigned output_buf_len,
119 struct pjmedia_frame *output);
120
121
122
123 /* Definition for AMR codec operations. */
124 static pjmedia_codec_op amr_op =
125 {
126 &amr_codec_init,
127 &amr_codec_open,
128 &amr_codec_close,
129 &amr_codec_modify,
130 &amr_codec_parse,
131 &amr_codec_encode,
132 &amr_codec_decode,
133 &amr_codec_recover
134 };
135
136 /* Definition for AMR codec factory operations. */
137 static pjmedia_codec_factory_op amr_factory_op =
138 {
139 &amr_test_alloc,
140 &amr_default_attr,
141 &amr_enum_codecs,
142 &amr_alloc_codec,
143 &amr_dealloc_codec,
144 &pjmedia_codec_opencore_amr_deinit
145 };
146
147
148 /* AMR factory */
149 static struct amr_codec_factory
150 {
151 pjmedia_codec_factory base;
152 pjmedia_endpt *endpt;
153 pj_pool_t *pool;
154 pj_bool_t init[2];
155 } amr_codec_factory;
156
157
158 /* AMR codec private data. */
159 struct amr_data
160 {
161 pj_pool_t *pool;
162 unsigned clock_rate;
163 void *encoder;
164 void *decoder;
165 pj_bool_t plc_enabled;
166 pj_bool_t vad_enabled;
167 int enc_mode;
168 pjmedia_codec_amr_pack_setting enc_setting;
169 pjmedia_codec_amr_pack_setting dec_setting;
170 #if USE_PJMEDIA_PLC
171 pjmedia_plc *plc;
172 #endif
173 pj_timestamp last_tx;
174 };
175
176 /* Index for AMR tables. */
177 enum
178 {
179 IDX_AMR_NB, /* Index for narrowband. */
180 IDX_AMR_WB /* Index for wideband. */
181 };
182
183 static pjmedia_codec_amr_config def_config[2] =
184 {{ /* AMR-NB */
185 PJ_FALSE, /* octet align */
186 5900 /* bitrate */
187 },
188 { /* AMR-WB */
189 PJ_FALSE, /* octet align */
190 12650 /* bitrate */
191 }};
192
193 static const pj_uint16_t* amr_bitrates[2] =
194 {pjmedia_codec_amrnb_bitrates, pjmedia_codec_amrwb_bitrates};
195
196 static const unsigned amr_bitrates_size[2] =
197 {
198 PJ_ARRAY_SIZE(pjmedia_codec_amrnb_bitrates),
199 PJ_ARRAY_SIZE(pjmedia_codec_amrwb_bitrates)
200 };
201
202
203 /*
204 * Initialize and register AMR codec factory to pjmedia endpoint.
205 */
206 PJ_DEF(pj_status_t) pjmedia_codec_opencore_amr_init( pjmedia_endpt *endpt,
207 unsigned options)
208 {
209 pjmedia_codec_mgr *codec_mgr;
210 pj_str_t codec_name;
211 pj_status_t status;
212
213 if (amr_codec_factory.pool != NULL)
214 return PJ_SUCCESS;
215
216 /* Create AMR codec factory. */
217 amr_codec_factory.base.op = &amr_factory_op;
218 amr_codec_factory.base.factory_data = NULL;
219 amr_codec_factory.endpt = endpt;
220 #ifdef USE_AMRNB
221 amr_codec_factory.init[IDX_AMR_NB] = ((options & PJMEDIA_AMR_NO_NB) == 0);
222 #else
223 amr_codec_factory.init[IDX_AMR_NB] = PJ_FALSE;
224 #endif
225 #ifdef USE_AMRWB
226 amr_codec_factory.init[IDX_AMR_WB] = ((options & PJMEDIA_AMR_NO_WB) == 0);
227 #else
228 amr_codec_factory.init[IDX_AMR_WB] = PJ_FALSE;
229 #endif
230
231 amr_codec_factory.pool = pjmedia_endpt_create_pool(endpt, "amr", 1000,
232 1000);
233 if (!amr_codec_factory.pool)
234 return PJ_ENOMEM;
235
236 /* Get the codec manager. */
237 codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
238 if (!codec_mgr) {
239 status = PJ_EINVALIDOP;
240 goto on_error;
241 }
242
243 /* Register format match callback. */
244 #ifdef USE_AMRNB
245 if ((options & PJMEDIA_AMR_NO_NB) == 0) {
246 pj_cstr(&codec_name, "AMR");
247 status = pjmedia_sdp_neg_register_fmt_match_cb(
248 &codec_name,
249 &pjmedia_codec_amr_match_sdp);
250 if (status != PJ_SUCCESS)
251 goto on_error;
252 }
253 #endif
254 #ifdef USE_AMRWB
255 if ((options & PJMEDIA_AMR_NO_WB) == 0) {
256 pj_cstr(&codec_name, "AMR-WB");
257 status = pjmedia_sdp_neg_register_fmt_match_cb(
258 &codec_name,
259 &pjmedia_codec_amr_match_sdp);
260 if (status != PJ_SUCCESS)
261 goto on_error;
262 }
263 #endif
264
265 /* Register codec factory to endpoint. */
266 status = pjmedia_codec_mgr_register_factory(codec_mgr,
267 &amr_codec_factory.base);
268 if (status != PJ_SUCCESS)
269 goto on_error;
270
271 /* Done. */
272 return PJ_SUCCESS;
273
274 on_error:
275 pj_pool_release(amr_codec_factory.pool);
276 amr_codec_factory.pool = NULL;
277 return status;
278 }
279
280 PJ_DEF(pj_status_t)
281 pjmedia_codec_opencore_amr_init_default( pjmedia_endpt *endpt )
282 {
283 return pjmedia_codec_opencore_amr_init(endpt, 0);
284 }
285
286 PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_init( pjmedia_endpt *endpt )
287 {
288 return pjmedia_codec_opencore_amr_init(endpt, PJMEDIA_AMR_NO_WB);
289 }
290
291
292 /*
293 * Unregister AMR codec factory from pjmedia endpoint and deinitialize
294 * the AMR codec library.
295 */
296 PJ_DEF(pj_status_t) pjmedia_codec_opencore_amr_deinit(void)
297 {
298 pjmedia_codec_mgr *codec_mgr;
299 pj_status_t status;
300
301 amr_codec_factory.init[IDX_AMR_NB] = PJ_FALSE;
302 amr_codec_factory.init[IDX_AMR_WB] = PJ_FALSE;
303
304 if (amr_codec_factory.pool == NULL)
305 return PJ_SUCCESS;
306
307 /* Get the codec manager. */
308 codec_mgr = pjmedia_endpt_get_codec_mgr(amr_codec_factory.endpt);
309 if (!codec_mgr) {
310 pj_pool_release(amr_codec_factory.pool);
311 amr_codec_factory.pool = NULL;
312 return PJ_EINVALIDOP;
313 }
314
315 /* Unregister AMR codec factory. */
316 status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
317 &amr_codec_factory.base);
318
319 /* Destroy pool. */
320 pj_pool_release(amr_codec_factory.pool);
321 amr_codec_factory.pool = NULL;
322
323 return status;
324 }
325
326 PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_deinit(void)
327 {
328 if (amr_codec_factory.init[IDX_AMR_NB] &&
329 amr_codec_factory.init[IDX_AMR_WB])
330 {
331 PJ_LOG(4, (THIS_FILE, "Should call "
332 "pjmedia_codec_opencore_amr_deinit() instead"));
333
334 return PJ_EINVALIDOP;
335 }
336
337 return pjmedia_codec_opencore_amr_deinit();
338 }
339
340 static pj_status_t
341 amr_set_config(unsigned idx, const pjmedia_codec_amr_config *config)
342 {
343 unsigned nbitrates;
344
345 def_config[idx] = *config;
346
347 /* Normalize bitrate. */
348 nbitrates = amr_bitrates_size[idx];
349 if (def_config[idx].bitrate < amr_bitrates[idx][0]) {
350 def_config[idx].bitrate = amr_bitrates[idx][0];
351 } else if (def_config[idx].bitrate > amr_bitrates[idx][nbitrates-1]) {
352 def_config[idx].bitrate = amr_bitrates[idx][nbitrates-1];
353 } else
354 {
355 unsigned i;
356
357 for (i = 0; i < nbitrates; ++i) {
358 if (def_config[idx].bitrate <= amr_bitrates[idx][i])
359 break;
360 }
361 def_config[idx].bitrate = amr_bitrates[idx][i];
362 }
363
364 return PJ_SUCCESS;
365 }
366
367 PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrnb_set_config(
368 const pjmedia_codec_amrnb_config *config)
369 {
370 return amr_set_config(IDX_AMR_NB, (const pjmedia_codec_amr_config *)config);
371 }
372
373 PJ_DEF(pj_status_t) pjmedia_codec_opencore_amrwb_set_config(
374 const pjmedia_codec_amrwb_config *config)
375 {
376 return amr_set_config(IDX_AMR_WB, (const pjmedia_codec_amr_config *)config);
377 }
378
379 /*
380 * Check if factory can allocate the specified codec.
381 */
382 static pj_status_t amr_test_alloc( pjmedia_codec_factory *factory,
383 const pjmedia_codec_info *info )
384 {
385 const pj_str_t amr_tag = { "AMR", 3};
386 const pj_str_t amrwb_tag = { "AMR-WB", 6};
387 PJ_UNUSED_ARG(factory);
388
389 /* Type MUST be audio. */
390 if (info->type != PJMEDIA_TYPE_AUDIO)
391 return PJMEDIA_CODEC_EUNSUP;
392
393 /* Check payload type. */
394 if (info->pt != PJMEDIA_RTP_PT_AMR && info->pt != PJMEDIA_RTP_PT_AMRWB)
395 return PJMEDIA_CODEC_EUNSUP;
396
397 /* Check encoding name. */
398 if (pj_stricmp(&info->encoding_name, &amr_tag) != 0 &&
399 pj_stricmp(&info->encoding_name, &amrwb_tag) != 0)
400 {
401 return PJMEDIA_CODEC_EUNSUP;
402 }
403
404 /* Check clock-rate */
405 if ((info->clock_rate == 8000 && amr_codec_factory.init[IDX_AMR_NB]) ||
406 (info->clock_rate == 16000 && amr_codec_factory.init[IDX_AMR_WB]))
407 {
408 return PJ_SUCCESS;
409 }
410
411 /* Unsupported or disabled. */
412 return PJMEDIA_CODEC_EUNSUP;
413 }
414
415 /*
416 * Generate default attribute.
417 */
418 static pj_status_t amr_default_attr( pjmedia_codec_factory *factory,
419 const pjmedia_codec_info *id,
420 pjmedia_codec_param *attr )
421 {
422 unsigned idx;
423
424 PJ_UNUSED_ARG(factory);
425
426 idx = (id->clock_rate <= 8000? IDX_AMR_NB: IDX_AMR_WB);
427 pj_bzero(attr, sizeof(pjmedia_codec_param));
428 attr->info.clock_rate = (id->clock_rate <= 8000? 8000: 16000);
429 attr->info.channel_cnt = 1;
430 attr->info.avg_bps = def_config[idx].bitrate;
431 attr->info.max_bps = amr_bitrates[idx][amr_bitrates_size[idx]-1];
432 attr->info.pcm_bits_per_sample = 16;
433 attr->info.frm_ptime = 20;
434 attr->info.pt = (pj_uint8_t)id->pt;
435
436 attr->setting.frm_per_pkt = 1;
437 attr->setting.vad = 1;
438 attr->setting.plc = 1;
439
440 if (def_config[idx].octet_align) {
441 attr->setting.dec_fmtp.cnt = 1;
442 attr->setting.dec_fmtp.param[0].name = pj_str("octet-align");
443 attr->setting.dec_fmtp.param[0].val = pj_str("1");
444 }
445
446 /* Default all other flag bits disabled. */
447
448 return PJ_SUCCESS;
449 }
450
451
452 /*
453 * Enum codecs supported by this factory (i.e. AMR-NB and AMR-WB).
454 */
455 static pj_status_t amr_enum_codecs( pjmedia_codec_factory *factory,
456 unsigned *count,
457 pjmedia_codec_info codecs[])
458 {
459 PJ_UNUSED_ARG(factory);
460 PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL);
461
462 *count = 0;
463
464 if (amr_codec_factory.init[IDX_AMR_NB]) {
465 pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info));
466 codecs[*count].encoding_name = pj_str("AMR");
467 codecs[*count].pt = PJMEDIA_RTP_PT_AMR;
468 codecs[*count].type = PJMEDIA_TYPE_AUDIO;
469 codecs[*count].clock_rate = 8000;
470 codecs[*count].channel_cnt = 1;
471 (*count)++;
472 }
473
474 if (amr_codec_factory.init[IDX_AMR_WB]) {
475 pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info));
476 codecs[*count].encoding_name = pj_str("AMR-WB");
477 codecs[*count].pt = PJMEDIA_RTP_PT_AMRWB;
478 codecs[*count].type = PJMEDIA_TYPE_AUDIO;
479 codecs[*count].clock_rate = 16000;
480 codecs[*count].channel_cnt = 1;
481 (*count)++;
482 }
483
484 return PJ_SUCCESS;
485 }
486
487
488 /*
489 * Allocate a new AMR codec instance.
490 */
491 static pj_status_t amr_alloc_codec( pjmedia_codec_factory *factory,
492 const pjmedia_codec_info *id,
493 pjmedia_codec **p_codec)
494 {
495 pj_pool_t *pool;
496 pjmedia_codec *codec;
497 struct amr_data *amr_data;
498 pj_status_t status;
499
500 PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL);
501 PJ_ASSERT_RETURN(factory == &amr_codec_factory.base, PJ_EINVAL);
502
503 pool = pjmedia_endpt_create_pool(amr_codec_factory.endpt, "amr-inst",
504 512, 512);
505
506 codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec);
507 PJ_ASSERT_RETURN(codec != NULL, PJ_ENOMEM);
508 codec->op = &amr_op;
509 codec->factory = factory;
510
511 amr_data = PJ_POOL_ZALLOC_T(pool, struct amr_data);
512 codec->codec_data = amr_data;
513 amr_data->pool = pool;
514
515 #if USE_PJMEDIA_PLC
516 /* Create PLC */
517 status = pjmedia_plc_create(pool, id->clock_rate,
518 id->clock_rate * FRAME_LENGTH_MS / 1000, 0,
519 &amr_data->plc);
520 if (status != PJ_SUCCESS) {
521 return status;
522 }
523 #else
524 PJ_UNUSED_ARG(status);
525 #endif
526 *p_codec = codec;
527 return PJ_SUCCESS;
528 }
529
530
531 /*
532 * Free codec.
533 */
534 static pj_status_t amr_dealloc_codec( pjmedia_codec_factory *factory,
535 pjmedia_codec *codec )
536 {
537 struct amr_data *amr_data;
538
539 PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL);
540 PJ_ASSERT_RETURN(factory == &amr_codec_factory.base, PJ_EINVAL);
541
542 amr_data = (struct amr_data*) codec->codec_data;
543
544 /* Close codec, if it's not closed. */
545 amr_codec_close(codec);
546
547 pj_pool_release(amr_data->pool);
548 amr_data = NULL;
549
550 return PJ_SUCCESS;
551 }
552
553 /*
554 * Init codec.
555 */
556 static pj_status_t amr_codec_init( pjmedia_codec *codec,
557 pj_pool_t *pool )
558 {
559 PJ_UNUSED_ARG(codec);
560 PJ_UNUSED_ARG(pool);
561 return PJ_SUCCESS;
562 }
563
564
565 /*
566 * Open codec.
567 */
568 static pj_status_t amr_codec_open( pjmedia_codec *codec,
569 pjmedia_codec_param *attr )
570 {
571 struct amr_data *amr_data = (struct amr_data*) codec->codec_data;
572 pjmedia_codec_amr_pack_setting *setting;
573 unsigned i;
574 pj_uint8_t octet_align = 0;
575 pj_int8_t enc_mode;
576 const pj_str_t STR_FMTP_OCTET_ALIGN = {"octet-align", 11};
577 unsigned idx;
578
579 PJ_ASSERT_RETURN(codec && attr, PJ_EINVAL);
580 PJ_ASSERT_RETURN(amr_data != NULL, PJ_EINVALIDOP);
581
582 idx = (attr->info.clock_rate <= 8000? IDX_AMR_NB: IDX_AMR_WB);
583 enc_mode = pjmedia_codec_amr_get_mode(attr->info.avg_bps);
584 pj_assert(enc_mode >= 0 && (unsigned)enc_mode < amr_bitrates_size[idx]);
585
586 /* Check octet-align */
587 for (i = 0; i < attr->setting.dec_fmtp.cnt; ++i) {
588 if (pj_stricmp(&attr->setting.dec_fmtp.param[i].name,
589 &STR_FMTP_OCTET_ALIGN) == 0)
590 {
591 octet_align = (pj_uint8_t)
592 (pj_strtoul(&attr->setting.dec_fmtp.param[i].val));
593 break;
594 }
595 }
596
597 /* Check mode-set */
598 for (i = 0; i < attr->setting.enc_fmtp.cnt; ++i) {
599 const pj_str_t STR_FMTP_MODE_SET = {"mode-set", 8};
600
601 if (pj_stricmp(&attr->setting.enc_fmtp.param[i].name,
602 &STR_FMTP_MODE_SET) == 0)
603 {
604 const char *p;
605 pj_size_t l;
606 pj_int8_t diff = 99;
607
608 /* Encoding mode is chosen based on local default mode setting:
609 * - if local default mode is included in the mode-set, use it
610 * - otherwise, find the closest mode to local default mode;
611 * if there are two closest modes, prefer to use the higher
612 * one, e.g: local default mode is 4, the mode-set param
613 * contains '2,3,5,6', then 5 will be chosen.
614 */
615 p = pj_strbuf(&attr->setting.enc_fmtp.param[i].val);
616 l = pj_strlen(&attr->setting.enc_fmtp.param[i].val);
617 while (l--) {
618 if (*p>='0' && (unsigned)*p<=('0'+amr_bitrates_size[idx]-1)) {
619 pj_int8_t tmp = *p - '0' - enc_mode;
620
621 if (PJ_ABS(diff) > PJ_ABS(tmp) ||
622 (PJ_ABS(diff) == PJ_ABS(tmp) && tmp > diff))
623 {
624 diff = tmp;
625 if (diff == 0) break;
626 }
627 }
628 ++p;
629 }
630 PJ_ASSERT_RETURN(diff != 99, PJMEDIA_CODEC_EFAILED);
631
632 enc_mode = enc_mode + diff;
633
634 break;
635 }
636 }
637
638 amr_data->clock_rate = attr->info.clock_rate;
639 amr_data->vad_enabled = (attr->setting.vad != 0);
640 amr_data->plc_enabled = (attr->setting.plc != 0);
641 amr_data->enc_mode = enc_mode;
642
643 if (idx == IDX_AMR_NB) {
644 #ifdef USE_AMRNB
645 amr_data->encoder = Encoder_Interface_init(amr_data->vad_enabled);
646 #endif
647 } else {
648 #ifdef USE_AMRWB
649 amr_data->encoder = E_IF_init();
650 #endif
651 }
652 if (amr_data->encoder == NULL) {
653 TRACE_((THIS_FILE, "Encoder initialization failed"));
654 amr_codec_close(codec);
655 return PJMEDIA_CODEC_EFAILED;
656 }
657 setting = &amr_data->enc_setting;
658 pj_bzero(setting, sizeof(pjmedia_codec_amr_pack_setting));
659 setting->amr_nb = (idx == IDX_AMR_NB? 1: 0);
660 setting->reorder = 0;
661 setting->octet_aligned = octet_align;
662 setting->cmr = 15;
663
664 if (idx == IDX_AMR_NB) {
665 #ifdef USE_AMRNB
666 amr_data->decoder = Decoder_Interface_init();
667 #endif
668 } else {
669 #ifdef USE_AMRWB
670 amr_data->decoder = D_IF_init();
671 #endif
672 }
673 if (amr_data->decoder == NULL) {
674 TRACE_((THIS_FILE, "Decoder initialization failed"));
675 amr_codec_close(codec);
676 return PJMEDIA_CODEC_EFAILED;
677 }
678 setting = &amr_data->dec_setting;
679 pj_bzero(setting, sizeof(pjmedia_codec_amr_pack_setting));
680 setting->amr_nb = (idx == IDX_AMR_NB? 1: 0);
681 setting->reorder = 0;
682 setting->octet_aligned = octet_align;
683
684 TRACE_((THIS_FILE, "AMR codec allocated: clockrate=%d vad=%d, plc=%d,"
685 " bitrate=%d", amr_data->clock_rate,
686 amr_data->vad_enabled, amr_data->plc_enabled,
687 amr_bitrates[idx][amr_data->enc_mode]));
688 return PJ_SUCCESS;
689 }
690
691
692 /*
693 * Close codec.
694 */
695 static pj_status_t amr_codec_close( pjmedia_codec *codec )
696 {
697 struct amr_data *amr_data;
698
699 PJ_ASSERT_RETURN(codec, PJ_EINVAL);
700
701 amr_data = (struct amr_data*) codec->codec_data;
702 PJ_ASSERT_RETURN(amr_data != NULL, PJ_EINVALIDOP);
703
704 if (amr_data->encoder) {
705 if (amr_data->enc_setting.amr_nb) {
706 #ifdef USE_AMRNB
707 Encoder_Interface_exit(amr_data->encoder);
708 #endif
709 } else {
710 #ifdef USE_AMRWB
711 E_IF_exit(amr_data->encoder);
712 #endif
713 }
714 amr_data->encoder = NULL;
715 }
716
717 if (amr_data->decoder) {
718 if (amr_data->dec_setting.amr_nb) {
719 #ifdef USE_AMRNB
720 Decoder_Interface_exit(amr_data->decoder);
721 #endif
722 } else {
723 #ifdef USE_AMRWB
724 D_IF_exit(amr_data->decoder);
725 #endif
726 }
727 amr_data->decoder = NULL;
728 }
729
730 TRACE_((THIS_FILE, "AMR codec closed"));
731 return PJ_SUCCESS;
732 }
733
734
735 /*
736 * Modify codec settings.
737 */
738 static pj_status_t amr_codec_modify( pjmedia_codec *codec,
739 const pjmedia_codec_param *attr )
740 {
741 struct amr_data *amr_data = (struct amr_data*) codec->codec_data;
742 pj_bool_t prev_vad_state;
743
744 pj_assert(amr_data != NULL);
745 pj_assert(amr_data->encoder != NULL && amr_data->decoder != NULL);
746
747 prev_vad_state = amr_data->vad_enabled;
748 amr_data->vad_enabled = (attr->setting.vad != 0);
749 amr_data->plc_enabled = (attr->setting.plc != 0);
750
751 if (amr_data->enc_setting.amr_nb &&
752 prev_vad_state != amr_data->vad_enabled)
753 {
754 /* Reinit AMR encoder to update VAD setting */
755 TRACE_((THIS_FILE, "Reiniting AMR encoder to update VAD setting."));
756 #ifdef USE_AMRNB
757 Encoder_Interface_exit(amr_data->encoder);
758 amr_data->encoder = Encoder_Interface_init(amr_data->vad_enabled);
759 #endif
760 if (amr_data->encoder == NULL) {
761 TRACE_((THIS_FILE, "Encoder_Interface_init() failed"));
762 amr_codec_close(codec);
763 return PJMEDIA_CODEC_EFAILED;
764 }
765 }
766
767 TRACE_((THIS_FILE, "AMR codec modified: vad=%d, plc=%d",
768 amr_data->vad_enabled, amr_data->plc_enabled));
769 return PJ_SUCCESS;
770 }
771
772
773 /*
774 * Get frames in the packet.
775 */
776 static pj_status_t amr_codec_parse( pjmedia_codec *codec,
777 void *pkt,
778 pj_size_t pkt_size,
779 const pj_timestamp *ts,
780 unsigned *frame_cnt,
781 pjmedia_frame frames[])
782 {
783 struct amr_data *amr_data = (struct amr_data*) codec->codec_data;
784 pj_uint8_t cmr;
785 pj_status_t status;
786 unsigned idx = (amr_data->enc_setting.amr_nb? 0: 1);
787
788 status = pjmedia_codec_amr_parse(pkt, pkt_size, ts, &amr_data->dec_setting,
789 frames, frame_cnt, &cmr);
790 if (status != PJ_SUCCESS)
791 return status;
792
793 /* Check for Change Mode Request. */
794 if (cmr < amr_bitrates_size[idx] && amr_data->enc_mode != cmr) {
795 amr_data->enc_mode = cmr;
796 TRACE_((THIS_FILE, "AMR encoder switched mode to %d (%dbps)",
797 amr_data->enc_mode,
798 amr_bitrates[idx][amr_data->enc_mode]));
799 }
800
801 return PJ_SUCCESS;
802 }
803
804
805 /*
806 * Encode frame.
807 */
808 static pj_status_t amr_codec_encode( pjmedia_codec *codec,
809 const struct pjmedia_frame *input,
810 unsigned output_buf_len,
811 struct pjmedia_frame *output)
812 {
813 struct amr_data *amr_data = (struct amr_data*) codec->codec_data;
814 unsigned char *bitstream;
815 pj_int16_t *speech;
816 unsigned nsamples, samples_per_frame;
817 enum {MAX_FRAMES_PER_PACKET = 16};
818 pjmedia_frame frames[MAX_FRAMES_PER_PACKET];
819 pj_uint8_t *p;
820 unsigned i, out_size = 0, nframes = 0;
821 pj_size_t payload_len;
822 unsigned dtx_cnt, sid_cnt;
823 pj_status_t status;
824
825 pj_assert(amr_data != NULL);
826 PJ_ASSERT_RETURN(input && output, PJ_EINVAL);
827
828 nsamples = input->size >> 1;
829 samples_per_frame = amr_data->clock_rate * FRAME_LENGTH_MS / 1000;
830 PJ_ASSERT_RETURN(nsamples % samples_per_frame == 0,
831 PJMEDIA_CODEC_EPCMFRMINLEN);
832
833 nframes = nsamples / samples_per_frame;
834 PJ_ASSERT_RETURN(nframes <= MAX_FRAMES_PER_PACKET,
835 PJMEDIA_CODEC_EFRMTOOSHORT);
836
837 /* Encode the frames */
838 speech = (pj_int16_t*)input->buf;
839 bitstream = (unsigned char*)output->buf;
840 while (nsamples >= samples_per_frame) {
841 int size;
842 if (amr_data->enc_setting.amr_nb) {
843 #ifdef USE_AMRNB
844 size = Encoder_Interface_Encode (amr_data->encoder,
845 amr_data->enc_mode,
846 speech, bitstream, 0);
847 #else
848 size = 0;
849 #endif
850 } else {
851 #ifdef USE_AMRWB
852 size = E_IF_encode (amr_data->encoder, amr_data->enc_mode,
853 speech, bitstream, 0);
854 #else
855 size = 0;
856 #endif
857 }
858 if (size == 0) {
859 output->size = 0;
860 output->buf = NULL;
861 output->type = PJMEDIA_FRAME_TYPE_NONE;
862 TRACE_((THIS_FILE, "AMR encode() failed"));
863 return PJMEDIA_CODEC_EFAILED;
864 }
865 nsamples -= samples_per_frame;
866 speech += samples_per_frame;
867 bitstream += size;
868 out_size += size;
869 TRACE_((THIS_FILE, "AMR encode(): mode=%d, size=%d",
870 amr_data->enc_mode, out_size));
871 }
872
873 /* Pack payload */
874 p = (pj_uint8_t*)output->buf + output_buf_len - out_size;
875 pj_memmove(p, output->buf, out_size);
876 dtx_cnt = sid_cnt = 0;
877 for (i = 0; i < nframes; ++i) {
878 pjmedia_codec_amr_bit_info *info = (pjmedia_codec_amr_bit_info*)
879 &frames[i].bit_info;
880 info->frame_type = (pj_uint8_t)((*p >> 3) & 0x0F);
881 info->good_quality = (pj_uint8_t)((*p >> 2) & 0x01);
882 info->mode = (pj_int8_t)amr_data->enc_mode;
883 info->start_bit = 0;
884 frames[i].buf = p + 1;
885 if (amr_data->enc_setting.amr_nb) {
886 frames[i].size = (info->frame_type <= 8)?
887 pjmedia_codec_amrnb_framelen[info->frame_type] : 0;
888 } else {
889 frames[i].size = (info->frame_type <= 9)?
890 pjmedia_codec_amrwb_framelen[info->frame_type] : 0;
891 }
892 p += frames[i].size + 1;
893
894 /* Count the number of SID and DTX frames */
895 if (info->frame_type == 15) /* DTX*/
896 ++dtx_cnt;
897 else if (info->frame_type == 8 && amr_data->enc_setting.amr_nb) /* SID */
898 ++sid_cnt;
899 else if (info->frame_type == 9 && !amr_data->enc_setting.amr_nb) /* SID */
900 ++sid_cnt;
901 }
902
903 /* VA generates DTX frames as DTX+SID frames switching quickly and it
904 * seems that the SID frames occur too often (assuming the purpose is
905 * only for keeping NAT alive?). So let's modify the behavior a bit.
906 * Only an SID frame will be sent every PJMEDIA_CODEC_MAX_SILENCE_PERIOD
907 * milliseconds.
908 */
909 if (sid_cnt + dtx_cnt == nframes) {
910 pj_int32_t dtx_duration;
911
912 dtx_duration = pj_timestamp_diff32(&amr_data->last_tx,
913 &input->timestamp);
914 if (PJMEDIA_CODEC_MAX_SILENCE_PERIOD == -1 ||
915 dtx_duration < (int)(PJMEDIA_CODEC_MAX_SILENCE_PERIOD*
916 amr_data->clock_rate/1000))
917 {
918 output->size = 0;
919 output->type = PJMEDIA_FRAME_TYPE_NONE;
920 output->timestamp = input->timestamp;
921 return PJ_SUCCESS;
922 }
923 }
924
925 payload_len = output_buf_len;
926
927 status = pjmedia_codec_amr_pack(frames, nframes, &amr_data->enc_setting,
928 output->buf, &payload_len);
929 if (status != PJ_SUCCESS) {
930 output->size = 0;
931 output->buf = NULL;
932 output->type = PJMEDIA_FRAME_TYPE_NONE;
933 TRACE_((THIS_FILE, "Failed to pack AMR payload, status=%d", status));
934 return status;
935 }
936
937 output->size = payload_len;
938 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
939 output->timestamp = input->timestamp;
940
941 amr_data->last_tx = input->timestamp;
942
943 return PJ_SUCCESS;
944 }
945
946
947 /*
948 * Decode frame.
949 */
950 static pj_status_t amr_codec_decode( pjmedia_codec *codec,
951 const struct pjmedia_frame *input,
952 unsigned output_buf_len,
953 struct pjmedia_frame *output)
954 {
955 struct amr_data *amr_data = (struct amr_data*) codec->codec_data;
956 pjmedia_frame input_;
957 pjmedia_codec_amr_bit_info *info;
958 unsigned out_size;
959 /* AMR decoding buffer: AMR max frame size + 1 byte header. */
960 unsigned char bitstream[61];
961
962 pj_assert(amr_data != NULL);
963 PJ_ASSERT_RETURN(input && output, PJ_EINVAL);
964
965 out_size = amr_data->clock_rate * FRAME_LENGTH_MS / 1000 * 2;
966 if (output_buf_len < out_size)
967 return PJMEDIA_CODEC_EPCMTOOSHORT;
968
969 input_.buf = &bitstream[1];
970 /* AMR max frame size */
971 input_.size = (amr_data->dec_setting.amr_nb? 31: 60);
972 pjmedia_codec_amr_predecode(input, &amr_data->dec_setting, &input_);
973 info = (pjmedia_codec_amr_bit_info*)&input_.bit_info;
974
975 /* VA AMR decoder requires frame info in the first byte. */
976 bitstream[0] = (info->frame_type << 3) | (info->good_quality << 2);
977
978 TRACE_((THIS_FILE, "AMR decode(): mode=%d, ft=%d, size=%d",
979 info->mode, info->frame_type, input_.size));
980
981 /* Decode */
982 if (amr_data->dec_setting.amr_nb) {
983 #ifdef USE_AMRNB
984 Decoder_Interface_Decode(amr_data->decoder, bitstream,
985 (pj_int16_t*)output->buf, 0);
986 #endif
987 } else {
988 #ifdef USE_AMRWB
989 D_IF_decode(amr_data->decoder, bitstream,
990 (pj_int16_t*)output->buf, 0);
991 #endif
992 }
993
994 output->size = out_size;
995 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
996 output->timestamp = input->timestamp;
997
998 #if USE_PJMEDIA_PLC
999 if (amr_data->plc_enabled)
1000 pjmedia_plc_save(amr_data->plc, (pj_int16_t*)output->buf);
1001 #endif
1002
1003 return PJ_SUCCESS;
1004 }
1005
1006
1007 /*
1008 * Recover lost frame.
1009 */
1010 #if USE_PJMEDIA_PLC
1011 /*
1012 * Recover lost frame.
1013 */
1014 static pj_status_t amr_codec_recover( pjmedia_codec *codec,
1015 unsigned output_buf_len,
1016 struct pjmedia_frame *output)
1017 {
1018 struct amr_data *amr_data = codec->codec_data;
1019 unsigned out_size = amr_data->clock_rate * FRAME_LENGTH_MS / 1000 * 2;
1020
1021 TRACE_((THIS_FILE, "amr_codec_recover"));
1022
1023 PJ_ASSERT_RETURN(amr_data->plc_enabled, PJ_EINVALIDOP);
1024
1025 PJ_ASSERT_RETURN(output_buf_len >= out_size, PJMEDIA_CODEC_EPCMTOOSHORT);
1026
1027 pjmedia_plc_generate(amr_data->plc, (pj_int16_t*)output->buf);
1028
1029 output->size = out_size;
1030 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
1031
1032 return PJ_SUCCESS;
1033 }
1034 #endif
1035
1036 #if defined(_MSC_VER) && PJMEDIA_AUTO_LINK_OPENCORE_AMR_LIBS
1037 # if PJMEDIA_OPENCORE_AMR_BUILT_WITH_GCC
1038 # ifdef USE_AMRNB
1039 # pragma comment( lib, "libopencore-amrnb.a")
1040 # endif
1041 # ifdef USE_AMRWB
1042 # pragma comment( lib, "libopencore-amrwb.a")
1043 # pragma comment( lib, "libvo-amrwbenc.a")
1044 # endif
1045 # else
1046 # error Unsupported OpenCORE AMR library, fix here
1047 # endif
1048 #endif
1049
1050 #endif