Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2017 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #include <pjmedia-codec/bcg729.h>
20 #include <pjmedia/codec.h>
21 #include <pjmedia/endpoint.h>
22 #include <pjmedia/errno.h>
23 #include <pjmedia/port.h>
24 #include <pj/pool.h>
25 #include <pj/string.h>
26 #include <pj/assert.h>
27 #include <pj/log.h>
28
29 #if defined(PJMEDIA_HAS_BCG729) && (PJMEDIA_HAS_BCG729!=0)
30
31 #include <bcg729/encoder.h>
32 #include <bcg729/decoder.h>
33
34 #define THIS_FILE "bcg729.c"
35
36 /* Prototypes for BCG729 factory */
37 static pj_status_t bcg729_test_alloc(pjmedia_codec_factory *factory,
38 const pjmedia_codec_info *id);
39 static pj_status_t bcg729_default_attr(pjmedia_codec_factory *factory,
40 const pjmedia_codec_info *id,
41 pjmedia_codec_param *attr);
42 static pj_status_t bcg729_enum_codecs (pjmedia_codec_factory *factory,
43 unsigned *count,
44 pjmedia_codec_info codecs[]);
45 static pj_status_t bcg729_alloc_codec(pjmedia_codec_factory *factory,
46 const pjmedia_codec_info *id,
47 pjmedia_codec **p_codec);
48 static pj_status_t bcg729_dealloc_codec(pjmedia_codec_factory *factory,
49 pjmedia_codec *codec);
50
51 /* Prototypes for BCG729 implementation. */
52 static pj_status_t bcg729_codec_init(pjmedia_codec *codec,
53 pj_pool_t *pool );
54 static pj_status_t bcg729_codec_open(pjmedia_codec *codec,
55 pjmedia_codec_param *attr);
56 static pj_status_t bcg729_codec_close(pjmedia_codec *codec);
57 static pj_status_t bcg729_codec_modify(pjmedia_codec *codec,
58 const pjmedia_codec_param *attr);
59 static pj_status_t bcg729_codec_parse(pjmedia_codec *codec,
60 void *pkt,
61 pj_size_t pkt_size,
62 const pj_timestamp *timestamp,
63 unsigned *frame_cnt,
64 pjmedia_frame frames[]);
65 static pj_status_t bcg729_codec_encode(pjmedia_codec *codec,
66 const struct pjmedia_frame *input,
67 unsigned output_buf_len,
68 struct pjmedia_frame *output);
69 static pj_status_t bcg729_codec_decode(pjmedia_codec *codec,
70 const struct pjmedia_frame *input,
71 unsigned output_buf_len,
72 struct pjmedia_frame *output);
73 static pj_status_t bcg729_codec_recover(pjmedia_codec *codec,
74 unsigned output_buf_len,
75 struct pjmedia_frame *output);
76
77 /* Codec const. */
78 #define G729_TAG "G729"
79 #define G729_CLOCK_RATE 8000
80 #define G729_CHANNEL_COUNT 1
81 #define G729_SAMPLES_PER_FRAME 80
82 #define G729_DEFAULT_BIT_RATE 8000
83 #define G729_MAX_BIT_RATE 11800
84 #define G729_FRAME_PER_PACKET 2
85 #define G729_FRAME_SIZE 10
86
87 /* Definition for BCG729 codec operations. */
88 static pjmedia_codec_op bcg729_op =
89 {
90 &bcg729_codec_init,
91 &bcg729_codec_open,
92 &bcg729_codec_close,
93 &bcg729_codec_modify,
94 &bcg729_codec_parse,
95 &bcg729_codec_encode,
96 &bcg729_codec_decode,
97 &bcg729_codec_recover
98 };
99
100 /* Definition for BCG729 codec factory operations. */
101 static pjmedia_codec_factory_op bcg729_factory_op =
102 {
103 &bcg729_test_alloc,
104 &bcg729_default_attr,
105 &bcg729_enum_codecs,
106 &bcg729_alloc_codec,
107 &bcg729_dealloc_codec,
108 &pjmedia_codec_bcg729_deinit
109 };
110
111
112 /* BCG729 factory private data */
113 static struct bcg729_factory
114 {
115 pjmedia_codec_factory base;
116 pjmedia_endpt *endpt;
117 pj_pool_t *pool;
118 pj_mutex_t *mutex;
119 } bcg729_factory;
120
121
122 /* BCG729 codec private data. */
123 typedef struct bcg729_private
124 {
125 pj_pool_t *pool; /**< Pool for each instance. */
126
127 bcg729EncoderChannelContextStruct *encoder;
128 bcg729DecoderChannelContextStruct *decoder;
129
130 pj_bool_t vad_enabled;
131 pj_bool_t plc_enabled;
132 } bcg729_private;
133
134
135 PJ_DEF(pj_status_t) pjmedia_codec_bcg729_init(pjmedia_endpt *endpt)
136 {
137 pjmedia_codec_mgr *codec_mgr;
138 pj_status_t status;
139
140 if (bcg729_factory.endpt != NULL) {
141 /* Already initialized. */
142 return PJ_SUCCESS;
143 }
144
145 /* Init factory */
146 pj_bzero(&bcg729_factory, sizeof(bcg729_factory));
147 bcg729_factory.base.op = &bcg729_factory_op;
148 bcg729_factory.base.factory_data = NULL;
149 bcg729_factory.endpt = endpt;
150
151 /* Create pool */
152 bcg729_factory.pool = pjmedia_endpt_create_pool(endpt, "bcg729", 4000,
153 4000);
154 if (!bcg729_factory.pool)
155 return PJ_ENOMEM;
156
157 /* Create mutex. */
158 status = pj_mutex_create_simple(bcg729_factory.pool, "bcg729",
159 &bcg729_factory.mutex);
160 if (status != PJ_SUCCESS)
161 goto on_error;
162
163 /* Get the codec manager. */
164 codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
165 if (!codec_mgr) {
166 status = PJ_EINVALIDOP;
167 goto on_error;
168 }
169
170 /* Register codec factory to endpoint. */
171 status = pjmedia_codec_mgr_register_factory(codec_mgr,
172 &bcg729_factory.base);
173 if (status != PJ_SUCCESS)
174 goto on_error;
175
176 PJ_LOG(4,(THIS_FILE, "BCG729 codec initialized"));
177 return PJ_SUCCESS;
178
179 on_error:
180 if (bcg729_factory.mutex) {
181 pj_mutex_destroy(bcg729_factory.mutex);
182 bcg729_factory.mutex = NULL;
183 }
184 if (bcg729_factory.pool) {
185 pj_pool_release(bcg729_factory.pool);
186 bcg729_factory.pool = NULL;
187 }
188
189 return status;
190 }
191
192 /*
193 * Unregister BCG729 codec factory from pjmedia endpoint and deinitialize
194 * the BCG729 codec library.
195 */
196 PJ_DEF(pj_status_t) pjmedia_codec_bcg729_deinit(void)
197 {
198 pjmedia_codec_mgr *codec_mgr;
199 pj_status_t status;
200
201 if (bcg729_factory.endpt == NULL) {
202 /* Not registered. */
203 return PJ_SUCCESS;
204 }
205
206 /* Lock mutex. */
207 pj_mutex_lock(bcg729_factory.mutex);
208
209 /* Get the codec manager. */
210 codec_mgr = pjmedia_endpt_get_codec_mgr(bcg729_factory.endpt);
211 if (!codec_mgr) {
212 bcg729_factory.endpt = NULL;
213 pj_mutex_unlock(bcg729_factory.mutex);
214 return PJ_EINVALIDOP;
215 }
216
217 /* Release the factory mutex before calling unregister to avoid lock
218 * order inversion with codec manager mutex.
219 */
220 pj_mutex_unlock(bcg729_factory.mutex);
221
222 /* Unregister bcg729 codec factory. */
223 status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
224 &bcg729_factory.base);
225 bcg729_factory.endpt = NULL;
226
227 /* Destroy mutex. */
228 pj_mutex_destroy(bcg729_factory.mutex);
229 bcg729_factory.mutex = NULL;
230
231 /* Release pool. */
232 pj_pool_release(bcg729_factory.pool);
233 bcg729_factory.pool = NULL;
234
235 return status;
236 }
237
238
239 /*
240 * Check if factory can allocate the specified codec.
241 */
242 static pj_status_t bcg729_test_alloc(pjmedia_codec_factory *factory,
243 const pjmedia_codec_info *info )
244 {
245 pj_str_t g729_tag = pj_str(G729_TAG);
246 PJ_UNUSED_ARG(factory);
247 PJ_ASSERT_RETURN(factory==&bcg729_factory.base, PJ_EINVAL);
248
249 /* Type MUST be audio. */
250 if (info->type != PJMEDIA_TYPE_AUDIO)
251 return PJMEDIA_CODEC_EUNSUP;
252
253 /* Check encoding name. */
254 if (pj_stricmp(&info->encoding_name, &g729_tag) != 0)
255 return PJMEDIA_CODEC_EUNSUP;
256
257 /* Channel count must be one */
258 if (info->channel_cnt != G729_CHANNEL_COUNT)
259 return PJMEDIA_CODEC_EUNSUP;
260
261 /* Check clock-rate */
262 if (info->clock_rate != G729_CLOCK_RATE)
263 return PJMEDIA_CODEC_EUNSUP;
264
265 return PJ_SUCCESS;
266 }
267
268
269 /*
270 * Generate default attribute.
271 */
272 static pj_status_t bcg729_default_attr(pjmedia_codec_factory *factory,
273 const pjmedia_codec_info *id,
274 pjmedia_codec_param *attr )
275 {
276 PJ_ASSERT_RETURN(factory==&bcg729_factory.base, PJ_EINVAL);
277
278 if (id->pt != PJMEDIA_RTP_PT_G729)
279 return PJMEDIA_CODEC_EUNSUP;
280
281 pj_bzero(attr, sizeof(pjmedia_codec_param));
282 attr->info.pt = PJMEDIA_RTP_PT_G729;
283 attr->info.channel_cnt = G729_CHANNEL_COUNT;
284 attr->info.clock_rate = G729_CLOCK_RATE;
285 attr->info.avg_bps = G729_DEFAULT_BIT_RATE;
286 attr->info.max_bps = G729_MAX_BIT_RATE;
287
288 attr->info.pcm_bits_per_sample = G729_FRAME_PER_PACKET * 8;
289 attr->info.frm_ptime = (pj_uint16_t)
290 (G729_SAMPLES_PER_FRAME * 1000 /
291 G729_CHANNEL_COUNT /
292 G729_CLOCK_RATE);
293 attr->setting.frm_per_pkt = G729_FRAME_PER_PACKET;
294
295 /* Default flags. */
296 attr->setting.plc = 1;
297 attr->setting.penh= 0;
298 attr->setting.vad = 1;
299 attr->setting.cng = attr->setting.vad;
300
301 if (attr->setting.vad == 0) {
302 attr->setting.dec_fmtp.cnt = 1;
303 pj_strset2(&attr->setting.dec_fmtp.param[0].name, "annexb");
304 pj_strset2(&attr->setting.dec_fmtp.param[0].val, "no");
305 }
306 return PJ_SUCCESS;
307 }
308
309
310 /*
311 * Enum codecs supported by this factory.
312 */
313 static pj_status_t bcg729_enum_codecs(pjmedia_codec_factory *factory,
314 unsigned *count,
315 pjmedia_codec_info codecs[])
316 {
317 PJ_ASSERT_RETURN(factory==&bcg729_factory.base, PJ_EINVAL);
318 PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL);
319
320 pj_bzero(&codecs[0], sizeof(pjmedia_codec_info));
321 codecs[0].encoding_name = pj_str(G729_TAG);
322 codecs[0].pt = PJMEDIA_RTP_PT_G729;
323 codecs[0].type = PJMEDIA_TYPE_AUDIO;
324 codecs[0].clock_rate = G729_CLOCK_RATE;
325 codecs[0].channel_cnt = G729_CHANNEL_COUNT;
326
327 *count = 1;
328
329 return PJ_SUCCESS;
330 }
331
332
333 /*
334 * Allocate a new BCG729 codec instance.
335 */
336 static pj_status_t bcg729_alloc_codec(pjmedia_codec_factory *factory,
337 const pjmedia_codec_info *id,
338 pjmedia_codec **p_codec)
339 {
340 pj_pool_t *pool;
341 pjmedia_codec *codec;
342 bcg729_private *bcg729_data;
343
344 PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL);
345 PJ_ASSERT_RETURN(factory == &bcg729_factory.base, PJ_EINVAL);
346
347 pj_mutex_lock(bcg729_factory.mutex);
348
349 /* Create pool for codec instance */
350 pool = pjmedia_endpt_create_pool(bcg729_factory.endpt, "bcg729", 512, 512);
351
352 /* Allocate codec */
353 codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec);
354 codec->op = &bcg729_op;
355 codec->factory = factory;
356 codec->codec_data = PJ_POOL_ZALLOC_T(pool, bcg729_private);
357 bcg729_data = (bcg729_private*) codec->codec_data;
358 bcg729_data->pool = pool;
359
360 pj_mutex_unlock(bcg729_factory.mutex);
361
362 *p_codec = codec;
363 return PJ_SUCCESS;
364 }
365
366
367 /*
368 * Free codec.
369 */
370 static pj_status_t bcg729_dealloc_codec(pjmedia_codec_factory *factory,
371 pjmedia_codec *codec )
372 {
373 bcg729_private *bcg729_data;
374
375 PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL);
376 PJ_ASSERT_RETURN(factory == &bcg729_factory.base, PJ_EINVAL);
377
378 bcg729_data = (bcg729_private*)codec->codec_data;
379
380 /* Close codec, if it's not closed. */
381 if (bcg729_data->encoder || bcg729_data->decoder) {
382 bcg729_codec_close(codec);
383 }
384
385 pj_pool_release(bcg729_data->pool);
386
387 return PJ_SUCCESS;
388 }
389
390
391 /*
392 * Init codec.
393 */
394 static pj_status_t bcg729_codec_init(pjmedia_codec *codec,
395 pj_pool_t *pool )
396 {
397 PJ_UNUSED_ARG(codec);
398 PJ_UNUSED_ARG(pool);
399 return PJ_SUCCESS;
400 }
401
402
403 /*
404 * Open codec.
405 */
406 static pj_status_t bcg729_codec_open(pjmedia_codec *codec,
407 pjmedia_codec_param *attr )
408 {
409 bcg729_private *bcg729_data;
410 unsigned i;
411
412 PJ_ASSERT_RETURN(codec && attr, PJ_EINVAL);
413
414 bcg729_data = (bcg729_private*) codec->codec_data;
415
416 /* Already open? */
417 if (bcg729_data->encoder && bcg729_data->decoder)
418 return PJ_SUCCESS;
419
420 bcg729_data->vad_enabled = (attr->setting.vad != 0);
421 bcg729_data->plc_enabled = (attr->setting.plc != 0);
422
423 /* Check if G729 Annex B is signaled to be disabled */
424 for (i = 0; i < attr->setting.enc_fmtp.cnt; ++i) {
425 if (pj_stricmp2(&attr->setting.enc_fmtp.param[i].name, "annexb")==0)
426 {
427 if (pj_stricmp2(&attr->setting.enc_fmtp.param[i].val, "no")==0)
428 {
429 attr->setting.vad = 0;
430 bcg729_data->vad_enabled = PJ_FALSE;
431 }
432 break;
433 }
434 }
435
436 bcg729_data->encoder = initBcg729EncoderChannel(
437 bcg729_data->vad_enabled?1:0);
438 if (!bcg729_data->encoder)
439 return PJMEDIA_CODEC_EFAILED;
440
441 bcg729_data->decoder = initBcg729DecoderChannel();
442 if (!bcg729_data->decoder)
443 return PJMEDIA_CODEC_EFAILED;
444
445 return PJ_SUCCESS;
446 }
447
448
449 /*
450 * Close codec.
451 */
452 static pj_status_t bcg729_codec_close( pjmedia_codec *codec )
453 {
454 bcg729_private *bcg729_data;
455
456 PJ_ASSERT_RETURN(codec, PJ_EINVAL);
457
458 bcg729_data = (bcg729_private *)codec->codec_data;
459
460 if (bcg729_data->encoder) {
461 closeBcg729EncoderChannel(bcg729_data->encoder);
462 bcg729_data->encoder = NULL;
463 }
464 if (bcg729_data->decoder) {
465 closeBcg729DecoderChannel(bcg729_data->decoder);
466 bcg729_data->decoder = NULL;
467 }
468
469 return PJ_SUCCESS;
470 }
471
472
473 /*
474 * Get frames in the packet.
475 */
476 static pj_status_t bcg729_codec_parse(pjmedia_codec *codec,
477 void *pkt,
478 pj_size_t pkt_size,
479 const pj_timestamp *ts,
480 unsigned *frame_cnt,
481 pjmedia_frame frames[])
482 {
483 unsigned count = 0;
484 PJ_UNUSED_ARG(codec);
485
486 PJ_ASSERT_RETURN(codec && ts && frames && frame_cnt, PJ_EINVAL);
487
488 while (pkt_size >= G729_FRAME_SIZE && count < *frame_cnt) {
489 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
490 frames[count].buf = pkt;
491 frames[count].size = G729_FRAME_SIZE;
492 frames[count].timestamp.u64 = ts->u64 + count * G729_SAMPLES_PER_FRAME;
493
494 pkt = ((char*)pkt) + 10;
495 pkt_size -= 10;
496
497 ++count;
498 }
499
500 *frame_cnt = count;
501 return PJ_SUCCESS;
502 }
503
504
505 /*
506 * Modify codec settings.
507 */
508 static pj_status_t bcg729_codec_modify(pjmedia_codec *codec,
509 const pjmedia_codec_param *attr )
510 {
511 PJ_UNUSED_ARG(codec);
512 PJ_UNUSED_ARG(attr);
513
514 return PJ_SUCCESS;
515 }
516
517
518 /*
519 * Encode frame.
520 */
521 static pj_status_t bcg729_codec_encode(pjmedia_codec *codec,
522 const struct pjmedia_frame *input,
523 unsigned output_buf_len,
524 struct pjmedia_frame *output)
525 {
526 bcg729_private *bcg729_data;
527 pj_int16_t *pcm_in;
528 unsigned nsamples;
529 pj_uint8_t stream_len;
530
531 PJ_ASSERT_RETURN(codec && input && output_buf_len && output, PJ_EINVAL);
532
533 bcg729_data = (bcg729_private*)codec->codec_data;
534 pcm_in = (pj_int16_t*)input->buf;
535
536 /* Check frame in size */
537 nsamples = input->size >> 1;
538 output->size = 0;
539
540 /* Encode */
541 while (nsamples >= G729_SAMPLES_PER_FRAME) {
542 bcg729Encoder(bcg729_data->encoder, pcm_in,
543 (unsigned char*)output->buf + output->size, &stream_len);
544
545 pcm_in += G729_SAMPLES_PER_FRAME;
546 nsamples -= G729_SAMPLES_PER_FRAME;
547
548 if (stream_len == 0) {
549 /* Untransmitted */
550 break;
551 } else {
552 output->size += stream_len;
553 if (stream_len == 2) {
554 /* SID */
555 break;
556 }
557 }
558 }
559
560 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
561 output->timestamp = input->timestamp;
562
563 return PJ_SUCCESS;
564 }
565
566
567 static pj_status_t bcg729_codec_decode(pjmedia_codec *codec,
568 const struct pjmedia_frame *input,
569 unsigned output_buf_len,
570 struct pjmedia_frame *output)
571 {
572 bcg729_private *bcg729_data;
573
574 PJ_ASSERT_RETURN(codec && input && output_buf_len && output, PJ_EINVAL);
575
576 bcg729_data = (struct bcg729_private*) codec->codec_data;
577
578 bcg729Decoder(bcg729_data->decoder,
579 (unsigned char*)input->buf,
580 G729_FRAME_SIZE,
581 0,
582 0,
583 0,
584 (short*)output->buf);
585
586 output->size = G729_SAMPLES_PER_FRAME * G729_FRAME_PER_PACKET;
587 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
588 output->timestamp = input->timestamp;
589
590 return PJ_SUCCESS;
591 }
592
593
594 /*
595 * Recover lost frame.
596 */
597 static pj_status_t bcg729_codec_recover(pjmedia_codec *codec,
598 unsigned output_buf_len,
599 struct pjmedia_frame *output)
600 {
601 bcg729_private *bcg729_data;
602
603 PJ_ASSERT_RETURN(codec && output_buf_len && output, PJ_EINVAL);
604
605 bcg729_data = (struct bcg729_private*) codec->codec_data;
606
607 output->size = G729_SAMPLES_PER_FRAME * G729_FRAME_PER_PACKET;
608 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
609
610 if (bcg729_data->plc_enabled) {
611 bcg729Decoder(bcg729_data->decoder,
612 NULL,
613 G729_FRAME_SIZE,
614 1,
615 0,
616 0,
617 (short*)output->buf);
618 } else {
619 pjmedia_zero_samples((pj_int16_t*)output->buf, G729_SAMPLES_PER_FRAME);
620 }
621
622 return PJ_SUCCESS;
623 }
624
625 #endif /* PJMEDIA_HAS_BCG729 */