aboutsummaryrefslogtreecommitdiffstats
path: root/codecs/codec_g723_1.c
blob: 5da9492a74137be7059da539f0f6b5aa95c02160 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * The G.723.1 code is not included in the Asterisk distribution because
 * it is covered with patents, and in spite of statements to the contrary,
 * the "technology" is extremely expensive to license.
 * 
 * Copyright (C) 1999 - 2005, Digium, Inc.
 *
 * Mark Spencer <markster@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Translate between signed linear and G.723.1
 *
 * \ingroup codecs
 */

/*** MODULEINFO
	<defaultenabled>no</defaultenabled>
 ***/

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>

#include "asterisk/lock.h"
#include "asterisk/translate.h"
#include "asterisk/module.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/utils.h"

#ifdef ANNEX_B
#include "g723.1b/typedef2.h"
#include "g723.1b/cst2.h"
#include "g723.1b/coder2.h"
#include "g723.1b/decod2.h"
#include "g723.1b/deccng2.h"
#include "g723.1b/codcng2.h"
#include "g723.1b/vad2.h"
#else
#include "g723.1/typedef.h"
#include "g723.1/cst_lbc.h"
#include "g723.1/coder.h"
#include "g723.1/decod.h"
#include "g723.1/dec_cng.h"
#include "g723.1/cod_cng.h"
#include "g723.1/vad.h"
#endif

/* Sample frame data */
#include "slin_g723_ex.h"
#include "g723_slin_ex.h"

#define TYPE_HIGH	 0x0
#define TYPE_LOW	 0x1
#define TYPE_SILENCE	 0x2
#define TYPE_DONTSEND	 0x3
#define TYPE_MASK	 0x3

/* g723_1 has 240 samples per buffer.
 * We want a buffer which is a multiple...
 */
#define	G723_SAMPLES	240
#define	BUFFER_SAMPLES	8160	/* 240 * 34 */

/* Globals */
Flag UsePf = True;
Flag UseHp = True;
Flag UseVx = True;

enum Crate WrkRate = Rate63;

struct g723_encoder_pvt {
	struct cod_state cod;
	int16_t buf[BUFFER_SAMPLES];	/* input buffer */
};

struct g723_decoder_pvt {
	struct dec_state dec;
};

static struct ast_trans_pvt *g723tolin_new(struct ast *pvt)
{
	struct g723_decoder_pvt *tmp = pvt;

	Init_Decod(&tmp->dec);
	Init_Dec_Cng(&tmp->dec);
	return tmp;
}

static struct ast_frame *lintog723_sample(void)
{
	static struct ast_frame f;
	f.frametype = AST_FRAME_VOICE;
	f.subclass = AST_FORMAT_SLINEAR;
	f.datalen = sizeof(slin_g723_ex);
	f.samples = sizeof(slin_g723_ex)/2;
	f.mallocd = 0;
	f.offset = 0;
	f.src = __PRETTY_FUNCTION__;
	f.data = slin_g723_ex;
	return &f;
}

static struct ast_frame *g723tolin_sample(void)
{
	static struct ast_frame f;
	f.frametype = AST_FRAME_VOICE;
	f.subclass = AST_FORMAT_G723_1;
	f.datalen = sizeof(g723_slin_ex);
	/* All frames are 30 ms long */
	f.samples = 240;
	f.mallocd = 0;
	f.offset = 0;
	f.src = __PRETTY_FUNCTION__;
	f.data = g723_slin_ex;
	return &f;
}

static void *lintog723_new(void *pvt)
{
	struct g723_encoder_pvt *tmp = pvt;
	Init_Coder(&tmp->cod);
	/* Init Comfort Noise Functions */
	if( UseVx ) {
		Init_Vad(&tmp->cod);
		Init_Cod_Cng(&tmp->cod);
	}
	return tmp;
}

static int g723_len(unsigned char buf)
{
	switch(buf & TYPE_MASK) {
	case TYPE_DONTSEND:
		return 0;
		break;
	case TYPE_SILENCE:
		return 4;
		break;
	case TYPE_HIGH:
		return 24;
		break;
	case TYPE_LOW:
		return 20;
		break;
	default:
		ast_log(LOG_WARNING, "Badly encoded frame (%d)\n", buf & TYPE_MASK);
	}
	return -1;
}

static int g723tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
{
	struct g723_decoder_pvt *tmp = pvt->pvt;
	int len = 0;
	int res;
	int16_t *dst = pvt->outbuf;
#ifdef  ANNEX_B
	FLOAT tmpdata[Frame];
	int x;
#endif
	unsigned char *src = f->data;

	while(len < f->datalen) {
		/* Assuming there's space left, decode into the current buffer at
		   the tail location */
		res = g723_len(src[len]);
		if (res < 0) {
			ast_log(LOG_WARNING, "Invalid data\n");
			return -1;
		}
		if (res + len > f->datalen) {
			ast_log(LOG_WARNING, "Measured length exceeds frame length\n");
			return -1;
		}
		if (pvt->samples + Frame > BUFFER_SAMPLES) {	
			ast_log(LOG_WARNING, "Out of buffer space\n");
			return -1;
		}
#ifdef ANNEX_B
		Decod(&tmp->dec, tmpdata, f->data + len, 0);
		for (x=0;x<Frame;x++)
			dst[pvt->samples + x] = (int16_t)(tmpdata[x]); 
#else
		Decod(&tmp->dec, dst + pvt->samples, f->data + len, 0);
#endif
		pvt->samples += Frame;
		pvt->datalen += 2*Frame; /* 2 bytes/sample */
		len += res;
	}
	return 0;
}

static int lintog723_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
{
	/* Just add the frames to our stream */
	/* XXX We should look at how old the rest of our stream is, and if it
	   is too old, then we should overwrite it entirely, otherwise we can
	   get artifacts of earlier talk that do not belong */
	struct g723_encoder_pvt *tmp = pvt->pvt;

	if (tmp->samples + f->samples > BUFFER_SAMPLES) {
		ast_log(LOG_WARNING, "Out of buffer space\n");
		return -1;
	}
	memcpy(&tmp->buf[pvt->samples], f->data, f->datalen);
	pvt->samples += f->samples;
	return 0;
}

static struct ast_frame *lintog723_frameout(void *pvt)
{
	struct g723_encoder_pvt *tmp = (struct g723_encoder_pvt *)pvt;
	int samples = 0;	/* how many samples in buffer */
#ifdef ANNEX_B
	int x;
	FLOAT tmpdata[Frame];
#endif
	int cnt = 0;	/* how many bytes so far */

	/* We can't work on anything less than a frame in size */
	if (pvt->samples < Frame)
		return NULL;
	while (pvt->samples >= Frame) {
		/* Encode a frame of data */
		/* at most 24 bytes/frame... */
		if (cnt + 24 > pvt->buf_size) {
			ast_log(LOG_WARNING, "Out of buffer space\n");
			return NULL;
		}
#ifdef ANNEX_B
		for ( x = 0; x < Frame ; x++)
			tmpdata[x] = tmp->buf[x];
		Coder(&tmp->cod, tmpdata, pvt->outbuf + cnt);
#else
		Coder(&tmp->cod, tmp->buf, pvt->outbuf + cnt);
#endif
		/* Assume 8000 Hz */
		samples += G723_SAMPLES;
		cnt += g723_len(tmp->outbuf[cnt]);
		pvt->samples -= Frame;
		/* Move the data at the end of the buffer to the front */
		/* XXX inefficient... */
		if (pvt->samples)
			memmove(tmp->buf, tmp->buf + Frame, pvt->samples * 2);
	}
	return ast_trans_frameout(pvt, cnt, samples);
}

static struct ast_translator g723tolin = {
	.name =
#ifdef ANNEX_B
	"g723btolin", 
#else
	"g723tolin", 
#endif
	.srcfmt = AST_FORMAT_G723_1,
	.dstfmt =  AST_FORMAT_SLINEAR,
	.newpvt = g723tolin_new,
	.framein = g723tolin_framein,
	.sample = g723tolin_sample,
	.desc_size = sizeof(struct ...),
};

static struct ast_translator lintog723 = {
	.name =
#ifdef ANNEX_B
	"lintog723b", 
#else
	"lintog723", 
#endif
	.srcfmt = AST_FORMAT_SLINEAR,
	.dstfmt =  AST_FORMAT_G723_1,
	.new = lintog723_new,
	.framein = lintog723_framein,
	.frameout = lintog723_frameout,
	.destroy = g723_destroy,
	.sample = lintog723_sample,
	.desc_size = sizeof(struct ...),
};

/*! \brief standard module glue */

static int unload_module(void *mod)
{
	int res;
	res = ast_unregister_translator(&lintog723);
	res |= ast_unregister_translator(&g723tolin);
	return res;
}

static int load_module(void *mod)
{
	int res;
	res=ast_register_translator(&g723tolin, mod);
	if (!res) 
		res=ast_register_translator(&lintog723, mod);
	else
		ast_unregister_translator(&g723tolin);
	return res;
}

static const char *description(void)
{
#ifdef ANNEX_B
	return "Annex B (floating point) G.723.1/PCM16 Codec Translator";
#else
	return "Annex A (fixed point) G.723.1/PCM16 Codec Translator";
#endif

}

static const char *key(void)
{
	return ASTERISK_GPL_KEY;
}

STD_MOD(MOD_1, reload, NULL, NULL);