1
/* $Id: g711.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
/* This file contains file from Sun Microsystems, Inc, with the complete
21
* notice in the second half of this file.
23
#include <pjmedia/g711.h>
24
#include <pjmedia/codec.h>
25
#include <pjmedia/alaw_ulaw.h>
26
#include <pjmedia/endpoint.h>
27
#include <pjmedia/errno.h>
28
#include <pjmedia/port.h>
29
#include <pjmedia/plc.h>
30
#include <pjmedia/silencedet.h>
32
#include <pj/string.h>
33
#include <pj/assert.h>
35
#if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0
37
/* We removed PLC in 0.6 (and re-enabled it again in 0.9!) */
38
#define PLC_DISABLED 0
41
#define G711_BPS 64000
42
#define G711_CODEC_CNT 0 /* number of codec to preallocate in memory */
43
#define PTIME 10 /* basic frame size is 10 msec */
44
#define FRAME_SIZE (8000 * PTIME / 1000) /* 80 bytes */
45
#define SAMPLES_PER_FRAME (8000 * PTIME / 1000) /* 80 samples */
47
/* Prototypes for G711 factory */
48
static pj_status_t g711_test_alloc( pjmedia_codec_factory *factory,
49
const pjmedia_codec_info *id );
50
static pj_status_t g711_default_attr( pjmedia_codec_factory *factory,
51
const pjmedia_codec_info *id,
52
pjmedia_codec_param *attr );
53
static pj_status_t g711_enum_codecs (pjmedia_codec_factory *factory,
55
pjmedia_codec_info codecs[]);
56
static pj_status_t g711_alloc_codec( pjmedia_codec_factory *factory,
57
const pjmedia_codec_info *id,
58
pjmedia_codec **p_codec);
59
static pj_status_t g711_dealloc_codec( pjmedia_codec_factory *factory,
60
pjmedia_codec *codec );
62
/* Prototypes for G711 implementation. */
63
static pj_status_t g711_init( pjmedia_codec *codec,
65
static pj_status_t g711_open( pjmedia_codec *codec,
66
pjmedia_codec_param *attr );
67
static pj_status_t g711_close( pjmedia_codec *codec );
68
static pj_status_t g711_modify(pjmedia_codec *codec,
69
const pjmedia_codec_param *attr );
70
static pj_status_t g711_parse(pjmedia_codec *codec,
73
const pj_timestamp *timestamp,
75
pjmedia_frame frames[]);
76
static pj_status_t g711_encode( pjmedia_codec *codec,
77
const struct pjmedia_frame *input,
78
unsigned output_buf_len,
79
struct pjmedia_frame *output);
80
static pj_status_t g711_decode( pjmedia_codec *codec,
81
const struct pjmedia_frame *input,
82
unsigned output_buf_len,
83
struct pjmedia_frame *output);
85
static pj_status_t g711_recover( pjmedia_codec *codec,
86
unsigned output_buf_len,
87
struct pjmedia_frame *output);
90
/* Definition for G711 codec operations. */
91
static pjmedia_codec_op g711_op =
107
/* Definition for G711 codec factory operations. */
108
static pjmedia_codec_factory_op g711_factory_op =
117
/* G711 factory private data */
118
static struct g711_factory
120
pjmedia_codec_factory base;
121
pjmedia_endpt *endpt;
124
pjmedia_codec codec_list;
127
/* G711 codec private data. */
132
pj_bool_t plc_enabled;
135
pj_bool_t vad_enabled;
136
pjmedia_silence_det *vad;
137
pj_timestamp last_tx;
141
PJ_DEF(pj_status_t) pjmedia_codec_g711_init(pjmedia_endpt *endpt)
143
pjmedia_codec_mgr *codec_mgr;
146
if (g711_factory.endpt != NULL) {
147
/* Already initialized. */
152
g711_factory.base.op = &g711_factory_op;
153
g711_factory.base.factory_data = NULL;
154
g711_factory.endpt = endpt;
156
pj_list_init(&g711_factory.codec_list);
159
g711_factory.pool = pjmedia_endpt_create_pool(endpt, "g711", 4000, 4000);
160
if (!g711_factory.pool)
164
status = pj_mutex_create_simple(g711_factory.pool, "g611",
165
&g711_factory.mutex);
166
if (status != PJ_SUCCESS)
169
/* Get the codec manager. */
170
codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
172
return PJ_EINVALIDOP;
175
/* Register codec factory to endpoint. */
176
status = pjmedia_codec_mgr_register_factory(codec_mgr,
178
if (status != PJ_SUCCESS)
185
if (g711_factory.mutex) {
186
pj_mutex_destroy(g711_factory.mutex);
187
g711_factory.mutex = NULL;
189
if (g711_factory.pool) {
190
pj_pool_release(g711_factory.pool);
191
g711_factory.pool = NULL;
196
PJ_DEF(pj_status_t) pjmedia_codec_g711_deinit(void)
198
pjmedia_codec_mgr *codec_mgr;
201
if (g711_factory.endpt == NULL) {
202
/* Not registered. */
207
pj_mutex_lock(g711_factory.mutex);
209
/* Get the codec manager. */
210
codec_mgr = pjmedia_endpt_get_codec_mgr(g711_factory.endpt);
212
g711_factory.endpt = NULL;
213
pj_mutex_unlock(g711_factory.mutex);
214
return PJ_EINVALIDOP;
217
/* Unregister G711 codec factory. */
218
status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
220
g711_factory.endpt = NULL;
223
pj_mutex_destroy(g711_factory.mutex);
224
g711_factory.mutex = NULL;
228
pj_pool_release(g711_factory.pool);
229
g711_factory.pool = NULL;
235
static pj_status_t g711_test_alloc(pjmedia_codec_factory *factory,
236
const pjmedia_codec_info *id )
238
PJ_UNUSED_ARG(factory);
240
/* It's sufficient to check payload type only. */
241
return (id->pt==PJMEDIA_RTP_PT_PCMU || id->pt==PJMEDIA_RTP_PT_PCMA)? 0:-1;
244
static pj_status_t g711_default_attr (pjmedia_codec_factory *factory,
245
const pjmedia_codec_info *id,
246
pjmedia_codec_param *attr )
248
PJ_UNUSED_ARG(factory);
250
pj_bzero(attr, sizeof(pjmedia_codec_param));
251
attr->info.clock_rate = 8000;
252
attr->info.channel_cnt = 1;
253
attr->info.avg_bps = G711_BPS;
254
attr->info.max_bps = G711_BPS;
255
attr->info.pcm_bits_per_sample = 16;
256
attr->info.frm_ptime = PTIME;
257
attr->info.pt = (pj_uint8_t)id->pt;
259
/* Set default frames per packet to 2 (or 20ms) */
260
attr->setting.frm_per_pkt = 2;
263
/* Enable plc by default. */
264
attr->setting.plc = 1;
267
/* Enable VAD by default. */
268
attr->setting.vad = 1;
270
/* Default all other flag bits disabled. */
275
static pj_status_t g711_enum_codecs(pjmedia_codec_factory *factory,
277
pjmedia_codec_info codecs[])
281
PJ_UNUSED_ARG(factory);
283
if (count < *max_count) {
284
codecs[count].type = PJMEDIA_TYPE_AUDIO;
285
codecs[count].pt = PJMEDIA_RTP_PT_PCMU;
286
codecs[count].encoding_name = pj_str("PCMU");
287
codecs[count].clock_rate = 8000;
288
codecs[count].channel_cnt = 1;
291
if (count < *max_count) {
292
codecs[count].type = PJMEDIA_TYPE_AUDIO;
293
codecs[count].pt = PJMEDIA_RTP_PT_PCMA;
294
codecs[count].encoding_name = pj_str("PCMA");
295
codecs[count].clock_rate = 8000;
296
codecs[count].channel_cnt = 1;
305
static pj_status_t g711_alloc_codec( pjmedia_codec_factory *factory,
306
const pjmedia_codec_info *id,
307
pjmedia_codec **p_codec)
309
pjmedia_codec *codec = NULL;
312
PJ_ASSERT_RETURN(factory==&g711_factory.base, PJ_EINVAL);
315
pj_mutex_lock(g711_factory.mutex);
317
/* Allocate new codec if no more is available */
318
if (pj_list_empty(&g711_factory.codec_list)) {
319
struct g711_private *codec_priv;
321
codec = PJ_POOL_ALLOC_T(g711_factory.pool, pjmedia_codec);
322
codec_priv = PJ_POOL_ZALLOC_T(g711_factory.pool, struct g711_private);
323
if (!codec || !codec_priv) {
324
pj_mutex_unlock(g711_factory.mutex);
328
/* Set the payload type */
329
codec_priv->pt = id->pt;
332
/* Create PLC, always with 10ms ptime */
333
status = pjmedia_plc_create(g711_factory.pool, 8000, 80,
334
0, &codec_priv->plc);
335
if (status != PJ_SUCCESS) {
336
pj_mutex_unlock(g711_factory.mutex);
342
status = pjmedia_silence_det_create(g711_factory.pool,
345
if (status != PJ_SUCCESS) {
346
pj_mutex_unlock(g711_factory.mutex);
350
codec->factory = factory;
351
codec->op = &g711_op;
352
codec->codec_data = codec_priv;
354
codec = g711_factory.codec_list.next;
355
pj_list_erase(codec);
358
/* Zero the list, for error detection in g711_dealloc_codec */
359
codec->next = codec->prev = NULL;
364
pj_mutex_unlock(g711_factory.mutex);
369
static pj_status_t g711_dealloc_codec(pjmedia_codec_factory *factory,
370
pjmedia_codec *codec )
372
struct g711_private *priv = (struct g711_private*) codec->codec_data;
375
PJ_ASSERT_RETURN(factory==&g711_factory.base, PJ_EINVAL);
377
/* Check that this node has not been deallocated before */
378
pj_assert (codec->next==NULL && codec->prev==NULL);
379
if (codec->next!=NULL || codec->prev!=NULL) {
380
return PJ_EINVALIDOP;
384
/* Clear left samples in the PLC, since codec+plc will be reused
387
for (i=0; i<2; ++i) {
388
pj_int16_t frame[SAMPLES_PER_FRAME];
389
pjmedia_zero_samples(frame, PJ_ARRAY_SIZE(frame));
390
pjmedia_plc_save(priv->plc, frame);
398
pj_mutex_lock(g711_factory.mutex);
400
/* Insert at the back of the list */
401
pj_list_insert_before(&g711_factory.codec_list, codec);
404
pj_mutex_unlock(g711_factory.mutex);
409
static pj_status_t g711_init( pjmedia_codec *codec, pj_pool_t *pool )
411
/* There's nothing to do here really */
412
PJ_UNUSED_ARG(codec);
418
static pj_status_t g711_open(pjmedia_codec *codec,
419
pjmedia_codec_param *attr )
421
struct g711_private *priv = (struct g711_private*) codec->codec_data;
422
priv->pt = attr->info.pt;
424
priv->plc_enabled = (attr->setting.plc != 0);
426
priv->vad_enabled = (attr->setting.vad != 0);
430
static pj_status_t g711_close( pjmedia_codec *codec )
432
PJ_UNUSED_ARG(codec);
437
static pj_status_t g711_modify(pjmedia_codec *codec,
438
const pjmedia_codec_param *attr )
440
struct g711_private *priv = (struct g711_private*) codec->codec_data;
442
if (attr->info.pt != priv->pt)
443
return PJMEDIA_EINVALIDPT;
446
priv->plc_enabled = (attr->setting.plc != 0);
448
priv->vad_enabled = (attr->setting.vad != 0);
453
static pj_status_t g711_parse( pjmedia_codec *codec,
456
const pj_timestamp *ts,
458
pjmedia_frame frames[])
462
PJ_UNUSED_ARG(codec);
464
PJ_ASSERT_RETURN(ts && frame_cnt && frames, PJ_EINVAL);
466
while (pkt_size >= FRAME_SIZE && count < *frame_cnt) {
467
frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
468
frames[count].buf = pkt;
469
frames[count].size = FRAME_SIZE;
470
frames[count].timestamp.u64 = ts->u64 + SAMPLES_PER_FRAME * count;
472
pkt = ((char*)pkt) + FRAME_SIZE;
473
pkt_size -= FRAME_SIZE;
482
static pj_status_t g711_encode(pjmedia_codec *codec,
483
const struct pjmedia_frame *input,
484
unsigned output_buf_len,
485
struct pjmedia_frame *output)
487
pj_int16_t *samples = (pj_int16_t*) input->buf;
488
struct g711_private *priv = (struct g711_private*) codec->codec_data;
490
/* Check output buffer length */
491
if (output_buf_len < (input->size >> 1))
492
return PJMEDIA_CODEC_EFRMTOOSHORT;
494
/* Detect silence if VAD is enabled */
495
if (priv->vad_enabled) {
496
pj_bool_t is_silence;
497
pj_int32_t silence_period;
499
silence_period = pj_timestamp_diff32(&priv->last_tx,
502
is_silence = pjmedia_silence_det_detect(priv->vad,
503
(const pj_int16_t*) input->buf,
504
(input->size >> 1), NULL);
506
(PJMEDIA_CODEC_MAX_SILENCE_PERIOD == -1 ||
507
silence_period < PJMEDIA_CODEC_MAX_SILENCE_PERIOD*8000/1000))
509
output->type = PJMEDIA_FRAME_TYPE_NONE;
512
output->timestamp = input->timestamp;
515
priv->last_tx = input->timestamp;
520
if (priv->pt == PJMEDIA_RTP_PT_PCMA) {
522
pj_uint8_t *dst = (pj_uint8_t*) output->buf;
524
n = (input->size >> 1);
525
for (i=0; i!=n; ++i, ++dst) {
526
*dst = pjmedia_linear2alaw(samples[i]);
528
} else if (priv->pt == PJMEDIA_RTP_PT_PCMU) {
530
pj_uint8_t *dst = (pj_uint8_t*) output->buf;
532
n = (input->size >> 1);
533
for (i=0; i!=n; ++i, ++dst) {
534
*dst = pjmedia_linear2ulaw(samples[i]);
538
return PJMEDIA_EINVALIDPT;
541
output->type = PJMEDIA_FRAME_TYPE_AUDIO;
542
output->size = (input->size >> 1);
543
output->timestamp = input->timestamp;
548
static pj_status_t g711_decode(pjmedia_codec *codec,
549
const struct pjmedia_frame *input,
550
unsigned output_buf_len,
551
struct pjmedia_frame *output)
553
struct g711_private *priv = (struct g711_private*) codec->codec_data;
555
/* Check output buffer length */
556
PJ_ASSERT_RETURN(output_buf_len >= (input->size << 1),
557
PJMEDIA_CODEC_EPCMTOOSHORT);
559
/* Input buffer MUST have exactly 80 bytes long */
560
PJ_ASSERT_RETURN(input->size == FRAME_SIZE,
561
PJMEDIA_CODEC_EFRMINLEN);
564
if (priv->pt == PJMEDIA_RTP_PT_PCMA) {
566
pj_uint8_t *src = (pj_uint8_t*) input->buf;
567
pj_uint16_t *dst = (pj_uint16_t*) output->buf;
569
for (i=0; i!=input->size; ++i) {
570
*dst++ = (pj_uint16_t) pjmedia_alaw2linear(*src++);
572
} else if (priv->pt == PJMEDIA_RTP_PT_PCMU) {
574
pj_uint8_t *src = (pj_uint8_t*) input->buf;
575
pj_uint16_t *dst = (pj_uint16_t*) output->buf;
577
for (i=0; i!=input->size; ++i) {
578
*dst++ = (pj_uint16_t) pjmedia_ulaw2linear(*src++);
582
return PJMEDIA_EINVALIDPT;
585
output->type = PJMEDIA_FRAME_TYPE_AUDIO;
586
output->size = (input->size << 1);
587
output->timestamp = input->timestamp;
590
if (priv->plc_enabled)
591
pjmedia_plc_save( priv->plc, (pj_int16_t*)output->buf);
598
static pj_status_t g711_recover( pjmedia_codec *codec,
599
unsigned output_buf_len,
600
struct pjmedia_frame *output)
602
struct g711_private *priv = (struct g711_private*) codec->codec_data;
604
if (!priv->plc_enabled)
605
return PJ_EINVALIDOP;
607
PJ_ASSERT_RETURN(output_buf_len >= SAMPLES_PER_FRAME * 2,
608
PJMEDIA_CODEC_EPCMTOOSHORT);
610
pjmedia_plc_generate(priv->plc, (pj_int16_t*)output->buf);
611
output->size = SAMPLES_PER_FRAME * 2;
617
#endif /* PJMEDIA_HAS_G711_CODEC */