1
/* $Id: g7221.c 4001 2012-03-30 07:53:36Z bennylp $ */
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
#include <pjmedia-codec/g7221.h>
21
#include <pjmedia-codec/g7221_sdp_match.h>
22
#include <pjmedia/codec.h>
23
#include <pjmedia/errno.h>
24
#include <pjmedia/endpoint.h>
25
#include <pjmedia/port.h>
26
#include <pjmedia/silencedet.h>
27
#include <pj/assert.h>
31
#include <pj/string.h>
35
* Only build this file if PJMEDIA_HAS_G7221_CODEC != 0
37
#if defined(PJMEDIA_HAS_G7221_CODEC) && PJMEDIA_HAS_G7221_CODEC!=0
39
#include "../../../third_party/g7221/common/defs.h"
41
#define THIS_FILE "g7221.c"
43
/* Codec tag, it is the SDP encoding name and also MIME subtype name */
44
#define CODEC_TAG "G7221"
46
/* Sampling rates definition */
47
#define WB_SAMPLE_RATE 16000
48
#define UWB_SAMPLE_RATE 32000
50
/* Maximum number of samples per frame. */
51
#define MAX_SAMPLES_PER_FRAME (UWB_SAMPLE_RATE * 20 / 1000)
53
/* Maximum number of codec params. */
54
#define MAX_CODEC_MODES 8
55
#define START_RSV_MODES_IDX 6
58
/* Prototypes for G722.1 codec factory */
59
static pj_status_t test_alloc( pjmedia_codec_factory *factory,
60
const pjmedia_codec_info *id );
61
static pj_status_t default_attr( pjmedia_codec_factory *factory,
62
const pjmedia_codec_info *id,
63
pjmedia_codec_param *attr );
64
static pj_status_t enum_codecs( pjmedia_codec_factory *factory,
66
pjmedia_codec_info codecs[]);
67
static pj_status_t alloc_codec( pjmedia_codec_factory *factory,
68
const pjmedia_codec_info *id,
69
pjmedia_codec **p_codec);
70
static pj_status_t dealloc_codec( pjmedia_codec_factory *factory,
71
pjmedia_codec *codec );
73
/* Prototypes for G722.1 codec implementation. */
74
static pj_status_t codec_init( pjmedia_codec *codec,
76
static pj_status_t codec_open( pjmedia_codec *codec,
77
pjmedia_codec_param *attr );
78
static pj_status_t codec_close( pjmedia_codec *codec );
79
static pj_status_t codec_modify(pjmedia_codec *codec,
80
const pjmedia_codec_param *attr );
81
static pj_status_t codec_parse( pjmedia_codec *codec,
84
const pj_timestamp *ts,
86
pjmedia_frame frames[]);
87
static pj_status_t codec_encode( pjmedia_codec *codec,
88
const struct pjmedia_frame *input,
89
unsigned output_buf_len,
90
struct pjmedia_frame *output);
91
static pj_status_t codec_decode( pjmedia_codec *codec,
92
const struct pjmedia_frame *input,
93
unsigned output_buf_len,
94
struct pjmedia_frame *output);
95
static pj_status_t codec_recover( pjmedia_codec *codec,
96
unsigned output_buf_len,
97
struct pjmedia_frame *output);
99
/* Definition for G722.1 codec operations. */
100
static pjmedia_codec_op codec_op =
112
/* Definition for G722.1 codec factory operations. */
113
static pjmedia_codec_factory_op codec_factory_op =
120
&pjmedia_codec_g7221_deinit
124
/* Structure of G722.1 mode */
125
typedef struct codec_mode
127
pj_bool_t enabled; /* Is this mode enabled? */
128
pj_uint8_t pt; /* Payload type. */
129
unsigned sample_rate; /* Default sampling rate to be used.*/
130
unsigned bitrate; /* Bitrate. */
131
char bitrate_str[8]; /* Bitrate in string. */
135
/* G722.1 codec factory */
136
static struct codec_factory {
137
pjmedia_codec_factory base; /**< Base class. */
138
pjmedia_endpt *endpt; /**< PJMEDIA endpoint instance. */
139
pj_pool_t *pool; /**< Codec factory pool. */
140
pj_mutex_t *mutex; /**< Codec factory mutex. */
142
int pcm_shift; /**< Level adjustment */
143
unsigned mode_count; /**< Number of G722.1 modes. */
144
codec_mode modes[MAX_CODEC_MODES]; /**< The G722.1 modes. */
145
unsigned mode_rsv_start;/**< Start index of G722.1 non-
146
standard modes, currently
147
there can only be up to two
148
non-standard modes enabled
152
/* G722.1 codec private data. */
153
typedef struct codec_private {
154
pj_pool_t *pool; /**< Pool for each instance. */
155
pj_bool_t plc_enabled; /**< PLC enabled? */
156
pj_bool_t vad_enabled; /**< VAD enabled? */
157
pjmedia_silence_det *vad; /**< PJMEDIA VAD instance. */
158
pj_timestamp last_tx; /**< Timestamp of last transmit.*/
160
/* ITU ref implementation seems to leave the codec engine states to be
161
* managed by the application, so here we go.
164
/* Common engine state */
165
pj_uint16_t samples_per_frame; /**< Samples per frame. */
166
pj_uint16_t bitrate; /**< Coded stream bitrate. */
167
pj_uint16_t frame_size; /**< Coded frame size. */
168
pj_uint16_t frame_size_bits; /**< Coded frame size in bits. */
169
pj_uint16_t number_of_regions; /**< Number of regions. */
170
int pcm_shift; /**< Adjustment for PCM in/out */
172
/* Encoder specific state */
173
Word16 *enc_frame; /**< 16bit to 14bit buffer */
174
Word16 *enc_old_frame;
176
/* Decoder specific state */
177
Word16 *dec_old_frame;
178
Rand_Obj dec_randobj;
179
Word16 dec_old_mag_shift;
180
Word16 *dec_old_mlt_coefs;
184
* Helper function for looking up mode based on payload type.
186
static codec_mode* lookup_mode(unsigned pt)
188
codec_mode* mode = NULL;
191
for (i = 0; i < codec_factory.mode_count; ++i) {
192
mode = &codec_factory.modes[i];
201
* Helper function to validate G722.1 mode. Valid modes are defined as:
202
* 1. sample rate must be 16kHz or 32kHz,
204
* - for sampling rate 16kHz: 16000 to 32000 bps, it must be a multiple
205
* of 400 (to keep RTP payload octed-aligned)
206
* - for sampling rate 32kHz: 24000 to 48000 bps, it must be a multiple
207
* of 400 (to keep RTP payload octed-aligned)
209
static pj_bool_t validate_mode(unsigned sample_rate, unsigned bitrate)
211
if (sample_rate == WB_SAMPLE_RATE) {
212
if (bitrate < 16000 || bitrate > 32000 ||
213
((bitrate-16000) % 400 != 0))
217
} else if (sample_rate == UWB_SAMPLE_RATE) {
218
if (bitrate < 24000 || bitrate > 48000 ||
219
((bitrate-24000) % 400 != 0))
230
#if defined(PJ_IS_LITTLE_ENDIAN) && PJ_IS_LITTLE_ENDIAN!=0
231
PJ_INLINE(void) swap_bytes(pj_uint16_t *buf, unsigned count)
233
pj_uint16_t *end = buf + count;
235
*buf = (pj_uint16_t)((*buf << 8) | (*buf >> 8));
240
#define swap_bytes(buf, count)
244
* Initialize and register G722.1 codec factory to pjmedia endpoint.
246
PJ_DEF(pj_status_t) pjmedia_codec_g7221_init( pjmedia_endpt *endpt )
248
pjmedia_codec_mgr *codec_mgr;
253
if (codec_factory.pool != NULL) {
254
/* Already initialized. */
258
/* Initialize codec modes, by default all standard bitrates are enabled */
259
codec_factory.mode_count = 0;
260
codec_factory.pcm_shift = PJMEDIA_G7221_DEFAULT_PCM_SHIFT;
262
mode = &codec_factory.modes[codec_factory.mode_count++];
263
mode->enabled = PJ_TRUE;
264
mode->pt = PJMEDIA_RTP_PT_G722_1_24;
265
mode->sample_rate = WB_SAMPLE_RATE;
266
mode->bitrate = 24000;
267
pj_utoa(mode->bitrate, mode->bitrate_str);
269
mode = &codec_factory.modes[codec_factory.mode_count++];
270
mode->enabled = PJ_TRUE;
271
mode->pt = PJMEDIA_RTP_PT_G722_1_32;
272
mode->sample_rate = WB_SAMPLE_RATE;
273
mode->bitrate = 32000;
274
pj_utoa(mode->bitrate, mode->bitrate_str);
276
mode = &codec_factory.modes[codec_factory.mode_count++];
277
mode->enabled = PJ_TRUE;
278
mode->pt = PJMEDIA_RTP_PT_G7221C_24;
279
mode->sample_rate = UWB_SAMPLE_RATE;
280
mode->bitrate = 24000;
281
pj_utoa(mode->bitrate, mode->bitrate_str);
283
mode = &codec_factory.modes[codec_factory.mode_count++];
284
mode->enabled = PJ_TRUE;
285
mode->pt = PJMEDIA_RTP_PT_G7221C_32;
286
mode->sample_rate = UWB_SAMPLE_RATE;
287
mode->bitrate = 32000;
288
pj_utoa(mode->bitrate, mode->bitrate_str);
290
mode = &codec_factory.modes[codec_factory.mode_count++];
291
mode->enabled = PJ_TRUE;
292
mode->pt = PJMEDIA_RTP_PT_G7221C_48;
293
mode->sample_rate = UWB_SAMPLE_RATE;
294
mode->bitrate = 48000;
295
pj_utoa(mode->bitrate, mode->bitrate_str);
297
/* Non-standard bitrates */
299
/* Bitrate 16kbps is non-standard but rather commonly used. */
300
mode = &codec_factory.modes[codec_factory.mode_count++];
301
mode->enabled = PJ_FALSE;
302
mode->pt = PJMEDIA_RTP_PT_G722_1_16;
303
mode->sample_rate = WB_SAMPLE_RATE;
304
mode->bitrate = 16000;
305
pj_utoa(mode->bitrate, mode->bitrate_str);
307
/* Reserved two modes for non-standard bitrates */
308
codec_factory.mode_rsv_start = codec_factory.mode_count;
309
mode = &codec_factory.modes[codec_factory.mode_count++];
310
mode->enabled = PJ_FALSE;
311
mode->pt = PJMEDIA_RTP_PT_G7221_RSV1;
313
mode = &codec_factory.modes[codec_factory.mode_count++];
314
mode->enabled = PJ_FALSE;
315
mode->pt = PJMEDIA_RTP_PT_G7221_RSV2;
317
pj_assert(codec_factory.mode_count <= MAX_CODEC_MODES);
319
/* Create G722.1 codec factory. */
320
codec_factory.base.op = &codec_factory_op;
321
codec_factory.base.factory_data = NULL;
322
codec_factory.endpt = endpt;
324
codec_factory.pool = pjmedia_endpt_create_pool(endpt, "G722.1 codec",
326
if (!codec_factory.pool)
330
status = pj_mutex_create_simple(codec_factory.pool, "G722.1 codec",
331
&codec_factory.mutex);
332
if (status != PJ_SUCCESS)
335
/* Get the codec manager. */
336
codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
338
status = PJ_EINVALIDOP;
342
/* Register format match callback. */
343
pj_cstr(&codec_name, CODEC_TAG);
344
status = pjmedia_sdp_neg_register_fmt_match_cb(
346
&pjmedia_codec_g7221_match_sdp);
347
if (status != PJ_SUCCESS)
350
/* Register codec factory to endpoint. */
351
status = pjmedia_codec_mgr_register_factory(codec_mgr,
352
&codec_factory.base);
353
if (status != PJ_SUCCESS)
360
if (codec_factory.mutex) {
361
pj_mutex_destroy(codec_factory.mutex);
362
codec_factory.mutex = NULL;
365
pj_pool_release(codec_factory.pool);
366
codec_factory.pool = NULL;
372
* Enable and disable G722.1 modes, including non-standard modes.
374
PJ_DEF(pj_status_t) pjmedia_codec_g7221_set_mode(unsigned sample_rate,
381
if (!validate_mode(sample_rate, bitrate))
382
return PJMEDIA_CODEC_EINMODE;
384
/* Look up in factory modes table */
385
for (i = 0; i < codec_factory.mode_count; ++i) {
386
if (codec_factory.modes[i].sample_rate == sample_rate &&
387
codec_factory.modes[i].bitrate == bitrate)
389
codec_factory.modes[i].enabled = enabled;
394
/* Mode not found in modes table, this may be a request to enable
395
* a non-standard G722.1 mode.
398
/* Non-standard mode need to be initialized first before user
404
/* Initialize a non-standard mode, look for available space. */
405
for (i = codec_factory.mode_rsv_start;
406
i < codec_factory.mode_count; ++i)
408
if (!codec_factory.modes[i].enabled)
410
codec_mode *mode = &codec_factory.modes[i];
411
mode->enabled = PJ_TRUE;
412
mode->sample_rate = sample_rate;
413
mode->bitrate = bitrate;
414
pj_utoa(mode->bitrate, mode->bitrate_str);
420
/* No space for non-standard mode. */
426
* Set level adjustment.
428
PJ_DEF(pj_status_t) pjmedia_codec_g7221_set_pcm_shift(int val)
430
codec_factory.pcm_shift = val;
435
* Unregister G722.1 codec factory from pjmedia endpoint.
437
PJ_DEF(pj_status_t) pjmedia_codec_g7221_deinit(void)
439
pjmedia_codec_mgr *codec_mgr;
442
if (codec_factory.pool == NULL) {
443
/* Already deinitialized */
447
pj_mutex_lock(codec_factory.mutex);
449
/* Get the codec manager. */
450
codec_mgr = pjmedia_endpt_get_codec_mgr(codec_factory.endpt);
452
pj_pool_release(codec_factory.pool);
453
codec_factory.pool = NULL;
454
return PJ_EINVALIDOP;
457
/* Unregister G722.1 codec factory. */
458
status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
459
&codec_factory.base);
462
pj_mutex_destroy(codec_factory.mutex);
465
pj_pool_release(codec_factory.pool);
466
codec_factory.pool = NULL;
472
* Check if factory can allocate the specified codec.
474
static pj_status_t test_alloc( pjmedia_codec_factory *factory,
475
const pjmedia_codec_info *info )
477
PJ_UNUSED_ARG(factory);
479
/* Type MUST be audio. */
480
if (info->type != PJMEDIA_TYPE_AUDIO)
481
return PJMEDIA_CODEC_EUNSUP;
483
/* Check encoding name. */
484
if (pj_stricmp2(&info->encoding_name, CODEC_TAG) != 0)
485
return PJMEDIA_CODEC_EUNSUP;
487
/* Check clock-rate */
488
if (info->clock_rate != WB_SAMPLE_RATE &&
489
info->clock_rate != UWB_SAMPLE_RATE)
491
return PJMEDIA_CODEC_EUNSUP;
498
* Generate default attribute.
500
static pj_status_t default_attr ( pjmedia_codec_factory *factory,
501
const pjmedia_codec_info *id,
502
pjmedia_codec_param *attr )
506
PJ_ASSERT_RETURN(factory==&codec_factory.base, PJ_EINVAL);
508
pj_bzero(attr, sizeof(pjmedia_codec_param));
510
mode = lookup_mode(id->pt);
511
if (mode == NULL || !mode->enabled)
512
return PJMEDIA_CODEC_EUNSUP;
514
attr->info.pt = (pj_uint8_t)id->pt;
515
attr->info.channel_cnt = 1;
516
attr->info.clock_rate = mode->sample_rate;
517
attr->info.max_bps = mode->bitrate;
518
attr->info.avg_bps = mode->bitrate;
519
attr->info.pcm_bits_per_sample = 16;
520
attr->info.frm_ptime = 20;
523
attr->setting.plc = 1;
524
attr->setting.vad = 0;
525
attr->setting.frm_per_pkt = 1;
527
/* Default FMTP setting */
528
attr->setting.dec_fmtp.cnt = 1;
529
attr->setting.dec_fmtp.param[0].name = pj_str("bitrate");
530
attr->setting.dec_fmtp.param[0].val = pj_str(mode->bitrate_str);
536
* Enum codecs supported by this factory.
538
static pj_status_t enum_codecs( pjmedia_codec_factory *factory,
540
pjmedia_codec_info codecs[])
544
PJ_ASSERT_RETURN(factory==&codec_factory.base, PJ_EINVAL);
545
PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL);
550
for (i=0; (i < codec_factory.mode_count) && (*count < max_cnt); ++i)
552
if (!codec_factory.modes[i].enabled)
555
pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info));
556
codecs[*count].encoding_name = pj_str((char*)CODEC_TAG);
557
codecs[*count].pt = codec_factory.modes[i].pt;
558
codecs[*count].type = PJMEDIA_TYPE_AUDIO;
559
codecs[*count].clock_rate = codec_factory.modes[i].sample_rate;
560
codecs[*count].channel_cnt = 1;
569
* Allocate a new codec instance.
571
static pj_status_t alloc_codec( pjmedia_codec_factory *factory,
572
const pjmedia_codec_info *id,
573
pjmedia_codec **p_codec)
575
codec_private_t *codec_data;
576
pjmedia_codec *codec;
580
PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL);
581
PJ_ASSERT_RETURN(factory == &codec_factory.base, PJ_EINVAL);
583
pj_mutex_lock(codec_factory.mutex);
585
/* Create pool for codec instance */
586
pool = pjmedia_endpt_create_pool(codec_factory.endpt, "G7221", 512, 512);
587
codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec);
588
codec->op = &codec_op;
589
codec->factory = factory;
590
codec->codec_data = PJ_POOL_ZALLOC_T(pool, codec_private_t);
591
codec_data = (codec_private_t*) codec->codec_data;
592
codec_data->pool = pool;
594
/* Create silence detector */
595
status = pjmedia_silence_det_create(pool, id->clock_rate,
596
id->clock_rate * 20 / 1000,
598
if (status != PJ_SUCCESS) {
599
pj_mutex_unlock(codec_factory.mutex);
603
pj_mutex_unlock(codec_factory.mutex);
612
static pj_status_t dealloc_codec( pjmedia_codec_factory *factory,
613
pjmedia_codec *codec )
615
codec_private_t *codec_data;
618
PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL);
619
PJ_ASSERT_RETURN(factory == &codec_factory.base, PJ_EINVAL);
621
/* Close codec, if it's not closed. */
622
codec_data = (codec_private_t*) codec->codec_data;
623
pool = codec_data->pool;
626
/* Release codec pool */
627
pj_pool_release(pool);
635
static pj_status_t codec_init( pjmedia_codec *codec,
638
PJ_UNUSED_ARG(codec);
646
static pj_status_t codec_open( pjmedia_codec *codec,
647
pjmedia_codec_param *attr )
649
codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
653
/* Validation mode first! */
654
if (!validate_mode(attr->info.clock_rate, attr->info.avg_bps))
655
return PJMEDIA_CODEC_EINMODE;
657
pool = codec_data->pool;
659
/* Initialize common state */
660
codec_data->vad_enabled = (attr->setting.vad != 0);
661
codec_data->plc_enabled = (attr->setting.plc != 0);
663
codec_data->bitrate = (pj_uint16_t)attr->info.avg_bps;
664
codec_data->frame_size_bits = (pj_uint16_t)(attr->info.avg_bps*20/1000);
665
codec_data->frame_size = (pj_uint16_t)(codec_data->frame_size_bits>>3);
666
codec_data->samples_per_frame = (pj_uint16_t)
667
(attr->info.clock_rate*20/1000);
668
codec_data->number_of_regions = (pj_uint16_t)
669
(attr->info.clock_rate <= WB_SAMPLE_RATE?
670
NUMBER_OF_REGIONS:MAX_NUMBER_OF_REGIONS);
671
codec_data->pcm_shift = codec_factory.pcm_shift;
673
/* Initialize encoder state */
674
tmp = codec_data->samples_per_frame << 1;
675
codec_data->enc_old_frame = (Word16*)pj_pool_zalloc(pool, tmp);
676
codec_data->enc_frame = (Word16*)pj_pool_alloc(pool, tmp);
678
/* Initialize decoder state */
679
tmp = codec_data->samples_per_frame;
680
codec_data->dec_old_frame = (Word16*)pj_pool_zalloc(pool, tmp);
682
tmp = codec_data->samples_per_frame << 1;
683
codec_data->dec_old_mlt_coefs = (Word16*)pj_pool_zalloc(pool, tmp);
685
codec_data->dec_randobj.seed0 = 1;
686
codec_data->dec_randobj.seed1 = 1;
687
codec_data->dec_randobj.seed2 = 1;
688
codec_data->dec_randobj.seed3 = 1;
696
static pj_status_t codec_close( pjmedia_codec *codec )
698
PJ_UNUSED_ARG(codec);
705
* Modify codec settings.
707
static pj_status_t codec_modify( pjmedia_codec *codec,
708
const pjmedia_codec_param *attr )
710
codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
712
codec_data->vad_enabled = (attr->setting.vad != 0);
713
codec_data->plc_enabled = (attr->setting.plc != 0);
719
* Get frames in the packet.
721
static pj_status_t codec_parse( pjmedia_codec *codec,
724
const pj_timestamp *ts,
726
pjmedia_frame frames[])
728
codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
731
PJ_ASSERT_RETURN(frame_cnt, PJ_EINVAL);
733
/* Parse based on fixed frame size. */
734
while (pkt_size >= codec_data->frame_size && count < *frame_cnt) {
735
frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
736
frames[count].buf = pkt;
737
frames[count].size = codec_data->frame_size;
738
frames[count].timestamp.u64 = ts->u64 +
739
count * codec_data->samples_per_frame;
741
pkt = (pj_uint8_t*)pkt + codec_data->frame_size;
742
pkt_size -= codec_data->frame_size;
747
pj_assert(pkt_size == 0);
756
static pj_status_t codec_encode( pjmedia_codec *codec,
757
const struct pjmedia_frame *input,
758
unsigned output_buf_len,
759
struct pjmedia_frame *output)
761
codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
762
unsigned nsamples, processed;
764
/* Check frame in & out size */
765
nsamples = input->size >> 1;
766
PJ_ASSERT_RETURN(nsamples % codec_data->samples_per_frame == 0,
767
PJMEDIA_CODEC_EPCMFRMINLEN);
768
PJ_ASSERT_RETURN(output_buf_len >= codec_data->frame_size * nsamples /
769
codec_data->samples_per_frame,
770
PJMEDIA_CODEC_EFRMTOOSHORT);
772
/* Apply silence detection if VAD is enabled */
773
if (codec_data->vad_enabled) {
774
pj_bool_t is_silence;
775
pj_int32_t silence_duration;
777
pj_assert(codec_data->vad);
779
silence_duration = pj_timestamp_diff32(&codec_data->last_tx,
782
is_silence = pjmedia_silence_det_detect(codec_data->vad,
783
(const pj_int16_t*) input->buf,
787
(PJMEDIA_CODEC_MAX_SILENCE_PERIOD == -1 ||
788
silence_duration < (PJMEDIA_CODEC_MAX_SILENCE_PERIOD *
789
(int)codec_data->samples_per_frame / 20)))
791
output->type = PJMEDIA_FRAME_TYPE_NONE;
794
output->timestamp = input->timestamp;
797
codec_data->last_tx = input->timestamp;
803
while (processed < nsamples) {
804
Word16 mlt_coefs[MAX_SAMPLES_PER_FRAME];
806
const Word16 *pcm_input;
809
pcm_input = (const Word16*)input->buf + processed;
810
out_bits = (pj_int8_t*)output->buf + output->size;
812
/* Encoder adjust the input signal level */
813
if (codec_data->pcm_shift) {
815
for (i=0; i<codec_data->samples_per_frame; ++i) {
816
codec_data->enc_frame[i] =
817
(Word16)(pcm_input[i] >> codec_data->pcm_shift);
819
pcm_input = codec_data->enc_frame;
822
/* Convert input samples to rmlt coefs */
823
mag_shift = samples_to_rmlt_coefs(pcm_input,
824
codec_data->enc_old_frame,
826
codec_data->samples_per_frame);
828
/* Encode the mlt coefs. Note that encoder output stream is
829
* 16 bit array, so we need to take care about endianness.
831
encoder(codec_data->frame_size_bits,
832
codec_data->number_of_regions,
837
/* Encoder output are in native host byte order, while ITU says
838
* it must be in network byte order (MSB first).
840
swap_bytes((pj_uint16_t*)out_bits, codec_data->frame_size/2);
842
processed += codec_data->samples_per_frame;
843
output->size += codec_data->frame_size;
846
output->type = PJMEDIA_FRAME_TYPE_AUDIO;
847
output->timestamp = input->timestamp;
855
static pj_status_t codec_decode( pjmedia_codec *codec,
856
const struct pjmedia_frame *input,
857
unsigned output_buf_len,
858
struct pjmedia_frame *output)
860
codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
861
Word16 mlt_coefs[MAX_SAMPLES_PER_FRAME];
864
Word16 frame_error_flag = 0;
866
/* Check frame out length size */
867
PJ_ASSERT_RETURN(output_buf_len >=
868
(unsigned)(codec_data->samples_per_frame<<1),
869
PJMEDIA_CODEC_EPCMTOOSHORT);
871
/* If input is NULL, perform PLC by settting frame_error_flag to 1 */
873
/* Check frame in length size */
874
PJ_ASSERT_RETURN((pj_uint16_t)input->size == codec_data->frame_size,
875
PJMEDIA_CODEC_EFRMINLEN);
877
/* Decoder requires input of 16-bits array in native host byte
878
* order, while the frame received from the network are in
879
* network byte order (MSB first).
881
swap_bytes((pj_uint16_t*)input->buf, codec_data->frame_size/2);
883
bitobj.code_word_ptr = (Word16*)input->buf;
884
bitobj.current_word = *bitobj.code_word_ptr;
885
bitobj.code_bit_count = 0;
886
bitobj.number_of_bits_left = codec_data->frame_size_bits;
888
output->timestamp = input->timestamp;
890
pj_bzero(&bitobj, sizeof(bitobj));
891
frame_error_flag = 1;
894
/* Process the input frame to get mlt coefs */
896
&codec_data->dec_randobj,
897
codec_data->number_of_regions,
900
&codec_data->dec_old_mag_shift,
901
codec_data->dec_old_mlt_coefs,
904
/* Convert the mlt_coefs to PCM samples */
905
rmlt_coefs_to_samples(mlt_coefs,
906
codec_data->dec_old_frame,
907
(Word16*)output->buf,
908
codec_data->samples_per_frame,
911
/* Decoder adjust PCM signal */
912
if (codec_data->pcm_shift) {
914
pj_int16_t *buf = (Word16*)output->buf;
916
for (i=0; i<codec_data->samples_per_frame; ++i) {
917
buf[i] <<= codec_data->pcm_shift;
921
output->type = PJMEDIA_FRAME_TYPE_AUDIO;
922
output->size = codec_data->samples_per_frame << 1;
928
* Recover lost frame.
930
static pj_status_t codec_recover( pjmedia_codec *codec,
931
unsigned output_buf_len,
932
struct pjmedia_frame *output)
934
codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
936
/* Use native PLC when PLC is enabled. */
937
if (codec_data->plc_enabled)
938
return codec_decode(codec, NULL, output_buf_len, output);
940
/* Otherwise just return zero-fill frame. */
941
output->type = PJMEDIA_FRAME_TYPE_AUDIO;
942
output->size = codec_data->samples_per_frame << 1;
944
pjmedia_zero_samples((pj_int16_t*)output->buf,
945
codec_data->samples_per_frame);
950
#endif /* PJMEDIA_HAS_G7221_CODEC */