Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[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 #include <pjmedia-codec/gsm.h>
20 #include <pjmedia/codec.h>
21 #include <pjmedia/errno.h>
22 #include <pjmedia/endpoint.h>
23 #include <pjmedia/plc.h>
24 #include <pjmedia/port.h>
25 #include <pjmedia/silencedet.h>
26 #include <pj/assert.h>
27 #include <pj/pool.h>
28 #include <pj/string.h>
29 #include <pj/os.h>
30
31 /*
32 * Only build this file if PJMEDIA_HAS_GSM_CODEC != 0
33 */
34 #if defined(PJMEDIA_HAS_GSM_CODEC) && PJMEDIA_HAS_GSM_CODEC != 0
35
36 #if defined(PJMEDIA_EXTERNAL_GSM_CODEC) && PJMEDIA_EXTERNAL_GSM_CODEC
37 # if PJMEDIA_EXTERNAL_GSM_GSM_H
38 # include <gsm/gsm.h>
39 # elif PJMEDIA_EXTERNAL_GSM_H
40 # include <gsm.h>
41 # else
42 # error Please set the location of gsm.h
43 # endif
44 #else
45 # include "../../third_party/gsm/inc/gsm.h"
46 #endif
47
48 /* We removed PLC in 0.6 (and re-enabled it again in 0.9!) */
49 #define PLC_DISABLED 0
50
51
52 /* Prototypes for GSM factory */
53 static pj_status_t gsm_test_alloc( pjmedia_codec_factory *factory,
54 const pjmedia_codec_info *id );
55 static pj_status_t gsm_default_attr( pjmedia_codec_factory *factory,
56 const pjmedia_codec_info *id,
57 pjmedia_codec_param *attr );
58 static pj_status_t gsm_enum_codecs( pjmedia_codec_factory *factory,
59 unsigned *count,
60 pjmedia_codec_info codecs[]);
61 static pj_status_t gsm_alloc_codec( pjmedia_codec_factory *factory,
62 const pjmedia_codec_info *id,
63 pjmedia_codec **p_codec);
64 static pj_status_t gsm_dealloc_codec( pjmedia_codec_factory *factory,
65 pjmedia_codec *codec );
66
67 /* Prototypes for GSM implementation. */
68 static pj_status_t gsm_codec_init( pjmedia_codec *codec,
69 pj_pool_t *pool );
70 static pj_status_t gsm_codec_open( pjmedia_codec *codec,
71 pjmedia_codec_param *attr );
72 static pj_status_t gsm_codec_close( pjmedia_codec *codec );
73 static pj_status_t gsm_codec_modify(pjmedia_codec *codec,
74 const pjmedia_codec_param *attr );
75 static pj_status_t gsm_codec_parse( pjmedia_codec *codec,
76 void *pkt,
77 pj_size_t pkt_size,
78 const pj_timestamp *ts,
79 unsigned *frame_cnt,
80 pjmedia_frame frames[]);
81 static pj_status_t gsm_codec_encode( pjmedia_codec *codec,
82 const struct pjmedia_frame *input,
83 unsigned output_buf_len,
84 struct pjmedia_frame *output);
85 static pj_status_t gsm_codec_decode( pjmedia_codec *codec,
86 const struct pjmedia_frame *input,
87 unsigned output_buf_len,
88 struct pjmedia_frame *output);
89 #if !PLC_DISABLED
90 static pj_status_t gsm_codec_recover(pjmedia_codec *codec,
91 unsigned output_buf_len,
92 struct pjmedia_frame *output);
93 #endif
94
95 /* Definition for GSM codec operations. */
96 static pjmedia_codec_op gsm_op =
97 {
98 &gsm_codec_init,
99 &gsm_codec_open,
100 &gsm_codec_close,
101 &gsm_codec_modify,
102 &gsm_codec_parse,
103 &gsm_codec_encode,
104 &gsm_codec_decode,
105 #if !PLC_DISABLED
106 &gsm_codec_recover
107 #else
108 NULL
109 #endif
110 };
111
112 /* Definition for GSM codec factory operations. */
113 static pjmedia_codec_factory_op gsm_factory_op =
114 {
115 &gsm_test_alloc,
116 &gsm_default_attr,
117 &gsm_enum_codecs,
118 &gsm_alloc_codec,
119 &gsm_dealloc_codec,
120 &pjmedia_codec_gsm_deinit
121 };
122
123 /* GSM factory */
124 static struct gsm_codec_factory
125 {
126 pjmedia_codec_factory base;
127 pjmedia_endpt *endpt;
128 pj_pool_t *pool;
129 pj_mutex_t *mutex;
130 pjmedia_codec codec_list;
131 } gsm_codec_factory;
132
133
134 /* GSM codec private data. */
135 struct gsm_data
136 {
137 struct gsm_state *encoder;
138 struct gsm_state *decoder;
139 pj_bool_t plc_enabled;
140 #if !PLC_DISABLED
141 pjmedia_plc *plc;
142 #endif
143 pj_bool_t vad_enabled;
144 pjmedia_silence_det *vad;
145 pj_timestamp last_tx;
146 };
147
148
149
150 /*
151 * Initialize and register GSM codec factory to pjmedia endpoint.
152 */
153 PJ_DEF(pj_status_t) pjmedia_codec_gsm_init( pjmedia_endpt *endpt )
154 {
155 pjmedia_codec_mgr *codec_mgr;
156 pj_status_t status;
157
158 if (gsm_codec_factory.pool != NULL)
159 return PJ_SUCCESS;
160
161 /* Create GSM codec factory. */
162 gsm_codec_factory.base.op = &gsm_factory_op;
163 gsm_codec_factory.base.factory_data = NULL;
164 gsm_codec_factory.endpt = endpt;
165
166 gsm_codec_factory.pool = pjmedia_endpt_create_pool(endpt, "gsm", 1000,
167 4000);
168 if (!gsm_codec_factory.pool)
169 return PJ_ENOMEM;
170
171 pj_list_init(&gsm_codec_factory.codec_list);
172
173 /* Create mutex. */
174 status = pj_mutex_create_simple(gsm_codec_factory.pool, "gsm",
175 &gsm_codec_factory.mutex);
176 if (status != PJ_SUCCESS)
177 goto on_error;
178
179 /* Get the codec manager. */
180 codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
181 if (!codec_mgr) {
182 status = PJ_EINVALIDOP;
183 goto on_error;
184 }
185
186 /* Register codec factory to endpoint. */
187 status = pjmedia_codec_mgr_register_factory(codec_mgr,
188 &gsm_codec_factory.base);
189 if (status != PJ_SUCCESS)
190 goto on_error;
191
192 /* Done. */
193 return PJ_SUCCESS;
194
195 on_error:
196 pj_pool_release(gsm_codec_factory.pool);
197 gsm_codec_factory.pool = NULL;
198 return status;
199 }
200
201
202
203 /*
204 * Unregister GSM codec factory from pjmedia endpoint and deinitialize
205 * the GSM codec library.
206 */
207 PJ_DEF(pj_status_t) pjmedia_codec_gsm_deinit(void)
208 {
209 pjmedia_codec_mgr *codec_mgr;
210 pj_status_t status;
211
212 if (gsm_codec_factory.pool == NULL)
213 return PJ_SUCCESS;
214
215 /* We don't want to deinit if there's outstanding codec. */
216 /* This is silly, as we'll always have codec in the list if
217 we ever allocate a codec! A better behavior maybe is to
218 deallocate all codecs in the list.
219 pj_mutex_lock(gsm_codec_factory.mutex);
220 if (!pj_list_empty(&gsm_codec_factory.codec_list)) {
221 pj_mutex_unlock(gsm_codec_factory.mutex);
222 return PJ_EBUSY;
223 }
224 */
225
226 /* Get the codec manager. */
227 codec_mgr = pjmedia_endpt_get_codec_mgr(gsm_codec_factory.endpt);
228 if (!codec_mgr) {
229 pj_pool_release(gsm_codec_factory.pool);
230 gsm_codec_factory.pool = NULL;
231 return PJ_EINVALIDOP;
232 }
233
234 /* Unregister GSM codec factory. */
235 status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
236 &gsm_codec_factory.base);
237
238 /* Destroy mutex. */
239 pj_mutex_destroy(gsm_codec_factory.mutex);
240
241 /* Destroy pool. */
242 pj_pool_release(gsm_codec_factory.pool);
243 gsm_codec_factory.pool = NULL;
244
245 return status;
246 }
247
248 /*
249 * Check if factory can allocate the specified codec.
250 */
251 static pj_status_t gsm_test_alloc( pjmedia_codec_factory *factory,
252 const pjmedia_codec_info *info )
253 {
254 PJ_UNUSED_ARG(factory);
255
256 /* Check payload type. */
257 if (info->pt != PJMEDIA_RTP_PT_GSM)
258 return PJMEDIA_CODEC_EUNSUP;
259
260 /* Ignore the rest, since it's static payload type. */
261
262 return PJ_SUCCESS;
263 }
264
265 /*
266 * Generate default attribute.
267 */
268 static pj_status_t gsm_default_attr (pjmedia_codec_factory *factory,
269 const pjmedia_codec_info *id,
270 pjmedia_codec_param *attr )
271 {
272 PJ_UNUSED_ARG(factory);
273 PJ_UNUSED_ARG(id);
274
275 pj_bzero(attr, sizeof(pjmedia_codec_param));
276 attr->info.clock_rate = 8000;
277 attr->info.channel_cnt = 1;
278 attr->info.avg_bps = 13200;
279 attr->info.max_bps = 13200;
280 attr->info.pcm_bits_per_sample = 16;
281 attr->info.frm_ptime = 20;
282 attr->info.pt = PJMEDIA_RTP_PT_GSM;
283
284 attr->setting.frm_per_pkt = 1;
285 attr->setting.vad = 1;
286 #if !PLC_DISABLED
287 attr->setting.plc = 1;
288 #endif
289
290 /* Default all other flag bits disabled. */
291
292 return PJ_SUCCESS;
293 }
294
295 /*
296 * Enum codecs supported by this factory (i.e. only GSM!).
297 */
298 static pj_status_t gsm_enum_codecs(pjmedia_codec_factory *factory,
299 unsigned *count,
300 pjmedia_codec_info codecs[])
301 {
302 PJ_UNUSED_ARG(factory);
303 PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL);
304
305 pj_bzero(&codecs[0], sizeof(pjmedia_codec_info));
306 codecs[0].encoding_name = pj_str("GSM");
307 codecs[0].pt = PJMEDIA_RTP_PT_GSM;
308 codecs[0].type = PJMEDIA_TYPE_AUDIO;
309 codecs[0].clock_rate = 8000;
310 codecs[0].channel_cnt = 1;
311
312 *count = 1;
313
314 return PJ_SUCCESS;
315 }
316
317 /*
318 * Allocate a new GSM codec instance.
319 */
320 static pj_status_t gsm_alloc_codec( pjmedia_codec_factory *factory,
321 const pjmedia_codec_info *id,
322 pjmedia_codec **p_codec)
323 {
324 pjmedia_codec *codec;
325 struct gsm_data *gsm_data;
326 pj_status_t status;
327
328 PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL);
329 PJ_ASSERT_RETURN(factory == &gsm_codec_factory.base, PJ_EINVAL);
330
331
332 pj_mutex_lock(gsm_codec_factory.mutex);
333
334 /* Get free nodes, if any. */
335 if (!pj_list_empty(&gsm_codec_factory.codec_list)) {
336 codec = gsm_codec_factory.codec_list.next;
337 pj_list_erase(codec);
338 } else {
339 codec = PJ_POOL_ZALLOC_T(gsm_codec_factory.pool, pjmedia_codec);
340 PJ_ASSERT_RETURN(codec != NULL, PJ_ENOMEM);
341 codec->op = &gsm_op;
342 codec->factory = factory;
343
344 gsm_data = PJ_POOL_ZALLOC_T(gsm_codec_factory.pool, struct gsm_data);
345 codec->codec_data = gsm_data;
346
347 #if !PLC_DISABLED
348 /* Create PLC */
349 status = pjmedia_plc_create(gsm_codec_factory.pool, 8000,
350 160, 0, &gsm_data->plc);
351 if (status != PJ_SUCCESS) {
352 pj_mutex_unlock(gsm_codec_factory.mutex);
353 return status;
354 }
355 #endif
356
357 /* Create silence detector */
358 status = pjmedia_silence_det_create(gsm_codec_factory.pool,
359 8000, 160,
360 &gsm_data->vad);
361 if (status != PJ_SUCCESS) {
362 pj_mutex_unlock(gsm_codec_factory.mutex);
363 return status;
364 }
365 }
366
367 pj_mutex_unlock(gsm_codec_factory.mutex);
368
369 *p_codec = codec;
370 return PJ_SUCCESS;
371 }
372
373 /*
374 * Free codec.
375 */
376 static pj_status_t gsm_dealloc_codec( pjmedia_codec_factory *factory,
377 pjmedia_codec *codec )
378 {
379 struct gsm_data *gsm_data;
380 int i;
381
382 PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL);
383 PJ_ASSERT_RETURN(factory == &gsm_codec_factory.base, PJ_EINVAL);
384
385 gsm_data = (struct gsm_data*) codec->codec_data;
386
387 /* Close codec, if it's not closed. */
388 gsm_codec_close(codec);
389
390 #if !PLC_DISABLED
391 /* Clear left samples in the PLC, since codec+plc will be reused
392 * next time.
393 */
394 for (i=0; i<2; ++i) {
395 pj_int16_t frame[160];
396 pjmedia_zero_samples(frame, PJ_ARRAY_SIZE(frame));
397 pjmedia_plc_save(gsm_data->plc, frame);
398 }
399 #else
400 PJ_UNUSED_ARG(i);
401 #endif
402
403 /* Re-init silence_period */
404 pj_set_timestamp32(&gsm_data->last_tx, 0, 0);
405
406 /* Put in the free list. */
407 pj_mutex_lock(gsm_codec_factory.mutex);
408 pj_list_push_front(&gsm_codec_factory.codec_list, codec);
409 pj_mutex_unlock(gsm_codec_factory.mutex);
410
411 return PJ_SUCCESS;
412 }
413
414 /*
415 * Init codec.
416 */
417 static pj_status_t gsm_codec_init( pjmedia_codec *codec,
418 pj_pool_t *pool )
419 {
420 PJ_UNUSED_ARG(codec);
421 PJ_UNUSED_ARG(pool);
422 return PJ_SUCCESS;
423 }
424
425 /*
426 * Open codec.
427 */
428 static pj_status_t gsm_codec_open( pjmedia_codec *codec,
429 pjmedia_codec_param *attr )
430 {
431 struct gsm_data *gsm_data = (struct gsm_data*) codec->codec_data;
432
433 pj_assert(gsm_data != NULL);
434 pj_assert(gsm_data->encoder == NULL && gsm_data->decoder == NULL);
435
436 gsm_data->encoder = gsm_create();
437 if (!gsm_data->encoder)
438 return PJMEDIA_CODEC_EFAILED;
439
440 gsm_data->decoder = gsm_create();
441 if (!gsm_data->decoder)
442 return PJMEDIA_CODEC_EFAILED;
443
444 gsm_data->vad_enabled = (attr->setting.vad != 0);
445 gsm_data->plc_enabled = (attr->setting.plc != 0);
446
447 return PJ_SUCCESS;
448 }
449
450 /*
451 * Close codec.
452 */
453 static pj_status_t gsm_codec_close( pjmedia_codec *codec )
454 {
455 struct gsm_data *gsm_data = (struct gsm_data*) codec->codec_data;
456
457 pj_assert(gsm_data != NULL);
458
459 if (gsm_data->encoder) {
460 gsm_destroy(gsm_data->encoder);
461 gsm_data->encoder = NULL;
462 }
463 if (gsm_data->decoder) {
464 gsm_destroy(gsm_data->decoder);
465 gsm_data->decoder = NULL;
466 }
467
468 return PJ_SUCCESS;
469 }
470
471
472 /*
473 * Modify codec settings.
474 */
475 static pj_status_t gsm_codec_modify(pjmedia_codec *codec,
476 const pjmedia_codec_param *attr )
477 {
478 struct gsm_data *gsm_data = (struct gsm_data*) codec->codec_data;
479
480 pj_assert(gsm_data != NULL);
481 pj_assert(gsm_data->encoder != NULL && gsm_data->decoder != NULL);
482
483 gsm_data->vad_enabled = (attr->setting.vad != 0);
484 gsm_data->plc_enabled = (attr->setting.plc != 0);
485
486 return PJ_SUCCESS;
487 }
488
489
490 /*
491 * Get frames in the packet.
492 */
493 static pj_status_t gsm_codec_parse( pjmedia_codec *codec,
494 void *pkt,
495 pj_size_t pkt_size,
496 const pj_timestamp *ts,
497 unsigned *frame_cnt,
498 pjmedia_frame frames[])
499 {
500 unsigned count = 0;
501
502 PJ_UNUSED_ARG(codec);
503
504 PJ_ASSERT_RETURN(frame_cnt, PJ_EINVAL);
505
506 while (pkt_size >= 33 && count < *frame_cnt) {
507 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
508 frames[count].buf = pkt;
509 frames[count].size = 33;
510 frames[count].timestamp.u64 = ts->u64 + count * 160;
511
512 pkt = ((char*)pkt) + 33;
513 pkt_size -= 33;
514
515 ++count;
516 }
517
518 *frame_cnt = count;
519 return PJ_SUCCESS;
520 }
521
522 /*
523 * Encode frame.
524 */
525 static pj_status_t gsm_codec_encode( pjmedia_codec *codec,
526 const struct pjmedia_frame *input,
527 unsigned output_buf_len,
528 struct pjmedia_frame *output)
529 {
530 struct gsm_data *gsm_data = (struct gsm_data*) codec->codec_data;
531 pj_int16_t *pcm_in;
532 pj_size_t in_size;
533
534 pj_assert(gsm_data && input && output);
535
536 pcm_in = (pj_int16_t*)input->buf;
537 in_size = input->size;
538
539 PJ_ASSERT_RETURN(in_size % 320 == 0, PJMEDIA_CODEC_EPCMFRMINLEN);
540 PJ_ASSERT_RETURN(output_buf_len >= 33 * in_size/320,
541 PJMEDIA_CODEC_EFRMTOOSHORT);
542
543 /* Detect silence */
544 if (gsm_data->vad_enabled) {
545 pj_bool_t is_silence;
546 pj_int32_t silence_duration;
547
548 silence_duration = pj_timestamp_diff32(&gsm_data->last_tx,
549 &input->timestamp);
550
551 is_silence = pjmedia_silence_det_detect(gsm_data->vad,
552 (const pj_int16_t*) input->buf,
553 (input->size >> 1),
554 NULL);
555 if (is_silence &&
556 (PJMEDIA_CODEC_MAX_SILENCE_PERIOD == -1 ||
557 silence_duration < PJMEDIA_CODEC_MAX_SILENCE_PERIOD*8000/1000))
558 {
559 output->type = PJMEDIA_FRAME_TYPE_NONE;
560 output->buf = NULL;
561 output->size = 0;
562 output->timestamp = input->timestamp;
563 return PJ_SUCCESS;
564 } else {
565 gsm_data->last_tx = input->timestamp;
566 }
567 }
568
569 /* Encode */
570 output->size = 0;
571 while (in_size >= 320) {
572 gsm_encode(gsm_data->encoder, pcm_in,
573 (unsigned char*)output->buf + output->size);
574 pcm_in += 160;
575 output->size += 33;
576 in_size -= 320;
577 }
578
579 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
580 output->timestamp = input->timestamp;
581
582 return PJ_SUCCESS;
583 }
584
585 /*
586 * Decode frame.
587 */
588 static pj_status_t gsm_codec_decode( pjmedia_codec *codec,
589 const struct pjmedia_frame *input,
590 unsigned output_buf_len,
591 struct pjmedia_frame *output)
592 {
593 struct gsm_data *gsm_data = (struct gsm_data*) codec->codec_data;
594
595 pj_assert(gsm_data != NULL);
596 PJ_ASSERT_RETURN(input && output, PJ_EINVAL);
597
598 if (output_buf_len < 320)
599 return PJMEDIA_CODEC_EPCMTOOSHORT;
600
601 if (input->size < 33)
602 return PJMEDIA_CODEC_EFRMTOOSHORT;
603
604 gsm_decode(gsm_data->decoder,
605 (unsigned char*)input->buf,
606 (short*)output->buf);
607
608 output->size = 320;
609 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
610 output->timestamp = input->timestamp;
611
612 #if !PLC_DISABLED
613 if (gsm_data->plc_enabled)
614 pjmedia_plc_save( gsm_data->plc, (pj_int16_t*)output->buf);
615 #endif
616
617 return PJ_SUCCESS;
618 }
619
620
621 #if !PLC_DISABLED
622 /*
623 * Recover lost frame.
624 */
625 static pj_status_t gsm_codec_recover(pjmedia_codec *codec,
626 unsigned output_buf_len,
627 struct pjmedia_frame *output)
628 {
629 struct gsm_data *gsm_data = (struct gsm_data*) codec->codec_data;
630
631 PJ_ASSERT_RETURN(gsm_data->plc_enabled, PJ_EINVALIDOP);
632
633 PJ_ASSERT_RETURN(output_buf_len >= 320, PJMEDIA_CODEC_EPCMTOOSHORT);
634
635 pjmedia_plc_generate(gsm_data->plc, (pj_int16_t*)output->buf);
636 output->size = 320;
637
638 return PJ_SUCCESS;
639 }
640 #endif
641
642
643 #endif /* PJMEDIA_HAS_GSM_CODEC */
644