~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject/pjmedia/src/pjmedia/g711.c

  • Committer: Package Import Robot
  • Author(s): Francois Marier
  • Date: 2011-11-25 13:24:12 UTC
  • mfrom: (4.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20111125132412-dc4qvhyosk74cd42
Tags: 1.0.1-4
Don't assume that arch:all packages will get built (closes: #649726)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: g711.c 3553 2011-05-05 06:14:19Z nanang $ */
 
2
/* 
 
3
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 
4
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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 
 
19
 */
 
20
/* This file contains file from Sun Microsystems, Inc, with the complete 
 
21
 * notice in the second half of this file.
 
22
 */
 
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>
 
31
#include <pj/pool.h>
 
32
#include <pj/string.h>
 
33
#include <pj/assert.h>
 
34
 
 
35
#if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0
 
36
 
 
37
/* We removed PLC in 0.6 (and re-enabled it again in 0.9!) */
 
38
#define PLC_DISABLED    0
 
39
 
 
40
 
 
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   */
 
46
 
 
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, 
 
54
                                     unsigned *count, 
 
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 );
 
61
 
 
62
/* Prototypes for G711 implementation. */
 
63
static pj_status_t  g711_init( pjmedia_codec *codec, 
 
64
                               pj_pool_t *pool );
 
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,
 
71
                               void *pkt,
 
72
                               pj_size_t pkt_size,
 
73
                               const pj_timestamp *timestamp,
 
74
                               unsigned *frame_cnt,
 
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);
 
84
#if !PLC_DISABLED
 
85
static pj_status_t  g711_recover( pjmedia_codec *codec,
 
86
                                  unsigned output_buf_len,
 
87
                                  struct pjmedia_frame *output);
 
88
#endif
 
89
 
 
90
/* Definition for G711 codec operations. */
 
91
static pjmedia_codec_op g711_op = 
 
92
{
 
93
    &g711_init,
 
94
    &g711_open,
 
95
    &g711_close,
 
96
    &g711_modify,
 
97
    &g711_parse,
 
98
    &g711_encode,
 
99
    &g711_decode,
 
100
#if !PLC_DISABLED
 
101
    &g711_recover
 
102
#else
 
103
    NULL
 
104
#endif
 
105
};
 
106
 
 
107
/* Definition for G711 codec factory operations. */
 
108
static pjmedia_codec_factory_op g711_factory_op =
 
109
{
 
110
    &g711_test_alloc,
 
111
    &g711_default_attr,
 
112
    &g711_enum_codecs,
 
113
    &g711_alloc_codec,
 
114
    &g711_dealloc_codec
 
115
};
 
116
 
 
117
/* G711 factory private data */
 
118
static struct g711_factory
 
119
{
 
120
    pjmedia_codec_factory       base;
 
121
    pjmedia_endpt              *endpt;
 
122
    pj_pool_t                  *pool;
 
123
    pj_mutex_t                 *mutex;
 
124
    pjmedia_codec               codec_list;
 
125
} g711_factory;
 
126
 
 
127
/* G711 codec private data. */
 
128
struct g711_private
 
129
{
 
130
    unsigned             pt;
 
131
#if !PLC_DISABLED
 
132
    pj_bool_t            plc_enabled;
 
133
    pjmedia_plc         *plc;
 
134
#endif
 
135
    pj_bool_t            vad_enabled;
 
136
    pjmedia_silence_det *vad;
 
137
    pj_timestamp         last_tx;
 
138
};
 
139
 
 
140
 
 
141
PJ_DEF(pj_status_t) pjmedia_codec_g711_init(pjmedia_endpt *endpt)
 
142
{
 
143
    pjmedia_codec_mgr *codec_mgr;
 
144
    pj_status_t status;
 
145
 
 
146
    if (g711_factory.endpt != NULL) {
 
147
        /* Already initialized. */
 
148
        return PJ_SUCCESS;
 
149
    }
 
150
 
 
151
    /* Init factory */
 
152
    g711_factory.base.op = &g711_factory_op;
 
153
    g711_factory.base.factory_data = NULL;
 
154
    g711_factory.endpt = endpt;
 
155
 
 
156
    pj_list_init(&g711_factory.codec_list);
 
157
 
 
158
    /* Create pool */
 
159
    g711_factory.pool = pjmedia_endpt_create_pool(endpt, "g711", 4000, 4000);
 
160
    if (!g711_factory.pool)
 
161
        return PJ_ENOMEM;
 
162
 
 
163
    /* Create mutex. */
 
164
    status = pj_mutex_create_simple(g711_factory.pool, "g611", 
 
165
                                    &g711_factory.mutex);
 
166
    if (status != PJ_SUCCESS)
 
167
        goto on_error;
 
168
 
 
169
    /* Get the codec manager. */
 
170
    codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
 
171
    if (!codec_mgr) {
 
172
        return PJ_EINVALIDOP;
 
173
    }
 
174
 
 
175
    /* Register codec factory to endpoint. */
 
176
    status = pjmedia_codec_mgr_register_factory(codec_mgr, 
 
177
                                                &g711_factory.base);
 
178
    if (status != PJ_SUCCESS)
 
179
        return status;
 
180
 
 
181
 
 
182
    return PJ_SUCCESS;
 
183
 
 
184
on_error:
 
185
    if (g711_factory.mutex) {
 
186
        pj_mutex_destroy(g711_factory.mutex);
 
187
        g711_factory.mutex = NULL;
 
188
    }
 
189
    if (g711_factory.pool) {
 
190
        pj_pool_release(g711_factory.pool);
 
191
        g711_factory.pool = NULL;
 
192
    }
 
193
    return status;
 
194
}
 
195
 
 
196
PJ_DEF(pj_status_t) pjmedia_codec_g711_deinit(void)
 
197
{
 
198
    pjmedia_codec_mgr *codec_mgr;
 
199
    pj_status_t status;
 
200
 
 
201
    if (g711_factory.endpt == NULL) {
 
202
        /* Not registered. */
 
203
        return PJ_SUCCESS;
 
204
    }
 
205
 
 
206
    /* Lock mutex. */
 
207
    pj_mutex_lock(g711_factory.mutex);
 
208
 
 
209
    /* Get the codec manager. */
 
210
    codec_mgr = pjmedia_endpt_get_codec_mgr(g711_factory.endpt);
 
211
    if (!codec_mgr) {
 
212
        g711_factory.endpt = NULL;
 
213
        pj_mutex_unlock(g711_factory.mutex);
 
214
        return PJ_EINVALIDOP;
 
215
    }
 
216
 
 
217
    /* Unregister G711 codec factory. */
 
218
    status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
 
219
                                                  &g711_factory.base);
 
220
    g711_factory.endpt = NULL;
 
221
 
 
222
    /* Destroy mutex. */
 
223
    pj_mutex_destroy(g711_factory.mutex);
 
224
    g711_factory.mutex = NULL;
 
225
 
 
226
 
 
227
    /* Release pool. */
 
228
    pj_pool_release(g711_factory.pool);
 
229
    g711_factory.pool = NULL;
 
230
 
 
231
 
 
232
    return status;
 
233
}
 
234
 
 
235
static pj_status_t g711_test_alloc(pjmedia_codec_factory *factory, 
 
236
                                   const pjmedia_codec_info *id )
 
237
{
 
238
    PJ_UNUSED_ARG(factory);
 
239
 
 
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;
 
242
}
 
243
 
 
244
static pj_status_t g711_default_attr (pjmedia_codec_factory *factory, 
 
245
                                      const pjmedia_codec_info *id, 
 
246
                                      pjmedia_codec_param *attr )
 
247
{
 
248
    PJ_UNUSED_ARG(factory);
 
249
 
 
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;
 
258
 
 
259
    /* Set default frames per packet to 2 (or 20ms) */
 
260
    attr->setting.frm_per_pkt = 2;
 
261
 
 
262
#if !PLC_DISABLED
 
263
    /* Enable plc by default. */
 
264
    attr->setting.plc = 1;
 
265
#endif
 
266
 
 
267
    /* Enable VAD by default. */
 
268
    attr->setting.vad = 1;
 
269
 
 
270
    /* Default all other flag bits disabled. */
 
271
 
 
272
    return PJ_SUCCESS;
 
273
}
 
274
 
 
275
static pj_status_t g711_enum_codecs(pjmedia_codec_factory *factory, 
 
276
                                    unsigned *max_count, 
 
277
                                    pjmedia_codec_info codecs[])
 
278
{
 
279
    unsigned count = 0;
 
280
 
 
281
    PJ_UNUSED_ARG(factory);
 
282
 
 
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;
 
289
        ++count;
 
290
    }
 
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;
 
297
        ++count;
 
298
    }
 
299
 
 
300
    *max_count = count;
 
301
 
 
302
    return PJ_SUCCESS;
 
303
}
 
304
 
 
305
static pj_status_t g711_alloc_codec( pjmedia_codec_factory *factory, 
 
306
                                     const pjmedia_codec_info *id,
 
307
                                     pjmedia_codec **p_codec)
 
308
{
 
309
    pjmedia_codec *codec = NULL;
 
310
    pj_status_t status;
 
311
 
 
312
    PJ_ASSERT_RETURN(factory==&g711_factory.base, PJ_EINVAL);
 
313
 
 
314
    /* Lock mutex. */
 
315
    pj_mutex_lock(g711_factory.mutex);
 
316
 
 
317
    /* Allocate new codec if no more is available */
 
318
    if (pj_list_empty(&g711_factory.codec_list)) {
 
319
        struct g711_private *codec_priv;
 
320
 
 
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);
 
325
            return PJ_ENOMEM;
 
326
        }
 
327
 
 
328
        /* Set the payload type */
 
329
        codec_priv->pt = id->pt;
 
330
 
 
331
#if !PLC_DISABLED
 
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);
 
337
            return status;
 
338
        }
 
339
#endif
 
340
 
 
341
        /* Create VAD */
 
342
        status = pjmedia_silence_det_create(g711_factory.pool,
 
343
                                            8000, 80,
 
344
                                            &codec_priv->vad);
 
345
        if (status != PJ_SUCCESS) {
 
346
            pj_mutex_unlock(g711_factory.mutex);
 
347
            return status;
 
348
        }
 
349
 
 
350
        codec->factory = factory;
 
351
        codec->op = &g711_op;
 
352
        codec->codec_data = codec_priv;
 
353
    } else {
 
354
        codec = g711_factory.codec_list.next;
 
355
        pj_list_erase(codec);
 
356
    }
 
357
 
 
358
    /* Zero the list, for error detection in g711_dealloc_codec */
 
359
    codec->next = codec->prev = NULL;
 
360
 
 
361
    *p_codec = codec;
 
362
 
 
363
    /* Unlock mutex. */
 
364
    pj_mutex_unlock(g711_factory.mutex);
 
365
 
 
366
    return PJ_SUCCESS;
 
367
}
 
368
 
 
369
static pj_status_t g711_dealloc_codec(pjmedia_codec_factory *factory, 
 
370
                                      pjmedia_codec *codec )
 
371
{
 
372
    struct g711_private *priv = (struct g711_private*) codec->codec_data;
 
373
    int i = 0;
 
374
 
 
375
    PJ_ASSERT_RETURN(factory==&g711_factory.base, PJ_EINVAL);
 
376
 
 
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;
 
381
    }
 
382
 
 
383
#if !PLC_DISABLED
 
384
    /* Clear left samples in the PLC, since codec+plc will be reused
 
385
     * next time.
 
386
     */
 
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);
 
391
    }
 
392
#else
 
393
    PJ_UNUSED_ARG(i);
 
394
    PJ_UNUSED_ARG(priv);
 
395
#endif
 
396
 
 
397
    /* Lock mutex. */
 
398
    pj_mutex_lock(g711_factory.mutex);
 
399
 
 
400
    /* Insert at the back of the list */
 
401
    pj_list_insert_before(&g711_factory.codec_list, codec);
 
402
 
 
403
    /* Unlock mutex. */
 
404
    pj_mutex_unlock(g711_factory.mutex);
 
405
 
 
406
    return PJ_SUCCESS;
 
407
}
 
408
 
 
409
static pj_status_t g711_init( pjmedia_codec *codec, pj_pool_t *pool )
 
410
{
 
411
    /* There's nothing to do here really */
 
412
    PJ_UNUSED_ARG(codec);
 
413
    PJ_UNUSED_ARG(pool);
 
414
 
 
415
    return PJ_SUCCESS;
 
416
}
 
417
 
 
418
static pj_status_t g711_open(pjmedia_codec *codec, 
 
419
                             pjmedia_codec_param *attr )
 
420
{
 
421
    struct g711_private *priv = (struct g711_private*) codec->codec_data;
 
422
    priv->pt = attr->info.pt;
 
423
#if !PLC_DISABLED
 
424
    priv->plc_enabled = (attr->setting.plc != 0);
 
425
#endif
 
426
    priv->vad_enabled = (attr->setting.vad != 0);
 
427
    return PJ_SUCCESS;
 
428
}
 
429
 
 
430
static pj_status_t g711_close( pjmedia_codec *codec )
 
431
{
 
432
    PJ_UNUSED_ARG(codec);
 
433
    /* Nothing to do */
 
434
    return PJ_SUCCESS;
 
435
}
 
436
 
 
437
static pj_status_t  g711_modify(pjmedia_codec *codec, 
 
438
                                const pjmedia_codec_param *attr )
 
439
{
 
440
    struct g711_private *priv = (struct g711_private*) codec->codec_data;
 
441
 
 
442
    if (attr->info.pt != priv->pt)
 
443
        return PJMEDIA_EINVALIDPT;
 
444
 
 
445
#if !PLC_DISABLED
 
446
    priv->plc_enabled = (attr->setting.plc != 0);
 
447
#endif
 
448
    priv->vad_enabled = (attr->setting.vad != 0);
 
449
 
 
450
    return PJ_SUCCESS;
 
451
}
 
452
 
 
453
static pj_status_t  g711_parse( pjmedia_codec *codec,
 
454
                                void *pkt,
 
455
                                pj_size_t pkt_size,
 
456
                                const pj_timestamp *ts,
 
457
                                unsigned *frame_cnt,
 
458
                                pjmedia_frame frames[])
 
459
{
 
460
    unsigned count = 0;
 
461
 
 
462
    PJ_UNUSED_ARG(codec);
 
463
 
 
464
    PJ_ASSERT_RETURN(ts && frame_cnt && frames, PJ_EINVAL);
 
465
 
 
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;
 
471
 
 
472
        pkt = ((char*)pkt) + FRAME_SIZE;
 
473
        pkt_size -= FRAME_SIZE;
 
474
 
 
475
        ++count;
 
476
    }
 
477
 
 
478
    *frame_cnt = count;
 
479
    return PJ_SUCCESS;
 
480
}
 
481
 
 
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)
 
486
{
 
487
    pj_int16_t *samples = (pj_int16_t*) input->buf;
 
488
    struct g711_private *priv = (struct g711_private*) codec->codec_data;
 
489
 
 
490
    /* Check output buffer length */
 
491
    if (output_buf_len < (input->size >> 1))
 
492
        return PJMEDIA_CODEC_EFRMTOOSHORT;
 
493
 
 
494
    /* Detect silence if VAD is enabled */
 
495
    if (priv->vad_enabled) {
 
496
        pj_bool_t is_silence;
 
497
        pj_int32_t silence_period;
 
498
 
 
499
        silence_period = pj_timestamp_diff32(&priv->last_tx,
 
500
                                             &input->timestamp);
 
501
 
 
502
        is_silence = pjmedia_silence_det_detect(priv->vad, 
 
503
                                                (const pj_int16_t*) input->buf, 
 
504
                                                (input->size >> 1), NULL);
 
505
        if (is_silence && 
 
506
            (PJMEDIA_CODEC_MAX_SILENCE_PERIOD == -1 ||
 
507
             silence_period < PJMEDIA_CODEC_MAX_SILENCE_PERIOD*8000/1000))
 
508
        {
 
509
            output->type = PJMEDIA_FRAME_TYPE_NONE;
 
510
            output->buf = NULL;
 
511
            output->size = 0;
 
512
            output->timestamp = input->timestamp;
 
513
            return PJ_SUCCESS;
 
514
        } else {
 
515
            priv->last_tx = input->timestamp;
 
516
        }
 
517
    }
 
518
 
 
519
    /* Encode */
 
520
    if (priv->pt == PJMEDIA_RTP_PT_PCMA) {
 
521
        unsigned i, n;
 
522
        pj_uint8_t *dst = (pj_uint8_t*) output->buf;
 
523
 
 
524
        n = (input->size >> 1);
 
525
        for (i=0; i!=n; ++i, ++dst) {
 
526
            *dst = pjmedia_linear2alaw(samples[i]);
 
527
        }
 
528
    } else if (priv->pt == PJMEDIA_RTP_PT_PCMU) {
 
529
        unsigned i, n;
 
530
        pj_uint8_t *dst = (pj_uint8_t*) output->buf;
 
531
 
 
532
        n = (input->size >> 1);
 
533
        for (i=0; i!=n; ++i, ++dst) {
 
534
            *dst = pjmedia_linear2ulaw(samples[i]);
 
535
        }
 
536
 
 
537
    } else {
 
538
        return PJMEDIA_EINVALIDPT;
 
539
    }
 
540
 
 
541
    output->type = PJMEDIA_FRAME_TYPE_AUDIO;
 
542
    output->size = (input->size >> 1);
 
543
    output->timestamp = input->timestamp;
 
544
 
 
545
    return PJ_SUCCESS;
 
546
}
 
547
 
 
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)
 
552
{
 
553
    struct g711_private *priv = (struct g711_private*) codec->codec_data;
 
554
 
 
555
    /* Check output buffer length */
 
556
    PJ_ASSERT_RETURN(output_buf_len >= (input->size << 1),
 
557
                     PJMEDIA_CODEC_EPCMTOOSHORT);
 
558
 
 
559
    /* Input buffer MUST have exactly 80 bytes long */
 
560
    PJ_ASSERT_RETURN(input->size == FRAME_SIZE, 
 
561
                     PJMEDIA_CODEC_EFRMINLEN);
 
562
 
 
563
    /* Decode */
 
564
    if (priv->pt == PJMEDIA_RTP_PT_PCMA) {
 
565
        unsigned i;
 
566
        pj_uint8_t *src = (pj_uint8_t*) input->buf;
 
567
        pj_uint16_t *dst = (pj_uint16_t*) output->buf;
 
568
 
 
569
        for (i=0; i!=input->size; ++i) {
 
570
            *dst++ = (pj_uint16_t) pjmedia_alaw2linear(*src++);
 
571
        }
 
572
    } else if (priv->pt == PJMEDIA_RTP_PT_PCMU) {
 
573
        unsigned i;
 
574
        pj_uint8_t *src = (pj_uint8_t*) input->buf;
 
575
        pj_uint16_t *dst = (pj_uint16_t*) output->buf;
 
576
 
 
577
        for (i=0; i!=input->size; ++i) {
 
578
            *dst++ = (pj_uint16_t) pjmedia_ulaw2linear(*src++);
 
579
        }
 
580
 
 
581
    } else {
 
582
        return PJMEDIA_EINVALIDPT;
 
583
    }
 
584
 
 
585
    output->type = PJMEDIA_FRAME_TYPE_AUDIO;
 
586
    output->size = (input->size << 1);
 
587
    output->timestamp = input->timestamp;
 
588
 
 
589
#if !PLC_DISABLED
 
590
    if (priv->plc_enabled)
 
591
        pjmedia_plc_save( priv->plc, (pj_int16_t*)output->buf);
 
592
#endif
 
593
 
 
594
    return PJ_SUCCESS;
 
595
}
 
596
 
 
597
#if !PLC_DISABLED
 
598
static pj_status_t  g711_recover( pjmedia_codec *codec,
 
599
                                  unsigned output_buf_len,
 
600
                                  struct pjmedia_frame *output)
 
601
{
 
602
    struct g711_private *priv = (struct g711_private*) codec->codec_data;
 
603
 
 
604
    if (!priv->plc_enabled)
 
605
        return PJ_EINVALIDOP;
 
606
 
 
607
    PJ_ASSERT_RETURN(output_buf_len >= SAMPLES_PER_FRAME * 2, 
 
608
                     PJMEDIA_CODEC_EPCMTOOSHORT);
 
609
 
 
610
    pjmedia_plc_generate(priv->plc, (pj_int16_t*)output->buf);
 
611
    output->size = SAMPLES_PER_FRAME * 2;
 
612
 
 
613
    return PJ_SUCCESS;
 
614
}
 
615
#endif
 
616
 
 
617
#endif  /* PJMEDIA_HAS_G711_CODEC */
 
618
 
 
619
 
 
620