aboutsummaryrefslogtreecommitdiffstats
path: root/codecs/codec_g722.c
blob: 38014dabe7394061b4e255778857629686b40c9b (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
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 1999 - 2006, Digium, Inc.
 *
 * Matthew Fredrickson <creslin@digium.com>
 *
 * Special thanks to Steve Underwood for the implementation
 * and for doing the 8khz<->g.722 direct translation code.
 *
 * 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 codec_g722.c - translate between signed linear and ITU G.722-64kbps
 *
 * \ingroup codecs
 */

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include <fcntl.h>
#include <netinet/in.h>

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

#define BUFFER_SAMPLES   8096	/* size for the translation buffers */
#define BUF_SHIFT	5

/* Sample frame data */

#include "g722/g722.h"
#include "slin_g722_ex.h"
#include "g722_slin_ex.h"

struct g722_encoder_pvt {
	g722_encode_state_t g722;
};

struct g722_decoder_pvt {
	g722_decode_state_t g722;
};

/*! \brief init a new instance of g722_encoder_pvt. */
static int lintog722_new(struct ast_trans_pvt *pvt)
{
	struct g722_encoder_pvt *tmp = pvt->pvt;

	g722_encode_init(&tmp->g722, 64000, G722_SAMPLE_RATE_8000);

	return 0;
}

static int lin16tog722_new(struct ast_trans_pvt *pvt)
{
	struct g722_encoder_pvt *tmp = pvt->pvt;

	g722_encode_init(&tmp->g722, 64000, 0);

	return 0;
}

/*! \brief init a new instance of g722_encoder_pvt. */
static int g722tolin_new(struct ast_trans_pvt *pvt)
{
	struct g722_decoder_pvt *tmp = pvt->pvt;

	g722_decode_init(&tmp->g722, 64000, G722_SAMPLE_RATE_8000);

	return 0;
}

static int g722tolin16_new(struct ast_trans_pvt *pvt)
{
	struct g722_decoder_pvt *tmp = pvt->pvt;

	g722_decode_init(&tmp->g722, 64000, 0);

	return 0;
}

static int g722tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
{
	struct g722_decoder_pvt *tmp = pvt->pvt;
	unsigned char *src = f->data;
	int16_t *dst = (int16_t *) pvt->outbuf + pvt->samples;

	g722_decode(&tmp->g722, dst, src, f->samples);
	pvt->samples += f->samples;
	pvt->datalen += 2 * f->samples;

	return 0;
}

static int lintog722_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
{
	struct g722_encoder_pvt *tmp = pvt->pvt;
	int16_t *src = f->data;

	g722_encode(&tmp->g722, (uint8_t*)(&pvt->outbuf[pvt->datalen]), src, f->samples);
	/* Since G.722 64kbps per second is one bye per sample, all of these
	   calculations are easy */
	pvt->samples += f->samples;
	pvt->datalen += f->samples;

	return 0;
}

static struct ast_frame *g722tolin_sample(void)
{
	static struct ast_frame f = {
		.frametype = AST_FRAME_VOICE,
		.subclass = AST_FORMAT_G722,
		.datalen = sizeof(g722_slin_ex),
		.samples = sizeof(g722_slin_ex) / sizeof(g722_slin_ex[0]),
		.src = __PRETTY_FUNCTION__,
		.data = g722_slin_ex,
	};

	return &f;
}

static struct ast_frame *g722tolin16_sample(void)
{
	static struct ast_frame f = {
		.frametype = AST_FRAME_VOICE,
		.subclass = AST_FORMAT_G722,
		.datalen = sizeof(slin_g722_ex),
		.samples = sizeof(slin_g722_ex) / sizeof(slin_g722_ex[0]),
		.src = __PRETTY_FUNCTION__,
		.data = slin_g722_ex,
	};

	return &f;
}

static struct ast_frame *lintog722_sample (void)
{
	static struct ast_frame f = {
		.frametype = AST_FRAME_VOICE,
		.subclass = AST_FORMAT_SLINEAR,
		.datalen = sizeof(slin_g722_ex),
		.samples = sizeof(slin_g722_ex) / sizeof(slin_g722_ex[0]),
		.src = __PRETTY_FUNCTION__,
		.data = slin_g722_ex,
	};

	return &f;
}

static struct ast_frame *lin16tog722_sample (void)
{
	static struct ast_frame f = {
		.frametype = AST_FRAME_VOICE,
		.subclass = AST_FORMAT_SLINEAR16,
		.datalen = sizeof(slin_g722_ex),
		.samples = sizeof(slin_g722_ex) / sizeof(slin_g722_ex[0]),
		.src = __PRETTY_FUNCTION__,
		.data = slin_g722_ex,
	};

	return &f;
}

static struct ast_translator g722tolin = {
	.name = "g722tolin",
	.srcfmt = AST_FORMAT_G722,
	.dstfmt = AST_FORMAT_SLINEAR,
	.newpvt = g722tolin_new,	/* same for both directions */
	.framein = g722tolin_framein,
	.sample = g722tolin_sample,
	.desc_size = sizeof(struct g722_decoder_pvt),
	.buffer_samples = BUFFER_SAMPLES,
	.buf_size = BUFFER_SAMPLES,
	.plc_samples = 160,
};

static struct ast_translator lintog722 = {
	.name = "lintog722",
	.srcfmt = AST_FORMAT_SLINEAR,
	.dstfmt = AST_FORMAT_G722,
	.newpvt = lintog722_new,	/* same for both directions */
	.framein = lintog722_framein,
	.sample = lintog722_sample,
	.desc_size = sizeof(struct g722_encoder_pvt),
	.buffer_samples = BUFFER_SAMPLES,
	.buf_size = BUFFER_SAMPLES,
};

static struct ast_translator g722tolin16 = {
	.name = "g722tolin16",
	.srcfmt = AST_FORMAT_G722,
	.dstfmt = AST_FORMAT_SLINEAR16,
	.newpvt = g722tolin16_new,	/* same for both directions */
	.framein = g722tolin_framein,
	.sample = g722tolin16_sample,
	.desc_size = sizeof(struct g722_decoder_pvt),
	.buffer_samples = BUFFER_SAMPLES,
	.buf_size = BUFFER_SAMPLES,
	.plc_samples = 160,
};

static struct ast_translator lin16tog722 = {
	.name = "lin16tog722",
	.srcfmt = AST_FORMAT_SLINEAR16,
	.dstfmt = AST_FORMAT_G722,
	.newpvt = lin16tog722_new,	/* same for both directions */
	.framein = lintog722_framein,
	.sample = lin16tog722_sample,
	.desc_size = sizeof(struct g722_encoder_pvt),
	.buffer_samples = BUFFER_SAMPLES,
	.buf_size = BUFFER_SAMPLES,
};

static int parse_config(int reload)
{
	struct ast_variable *var;
	struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
	struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);

	if (cfg == NULL)
		return -1;
	if (cfg == CONFIG_STATUS_FILEUNCHANGED)
		return 0;
	for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
		if (!strcasecmp(var->name, "genericplc")) {
			g722tolin.useplc = ast_true(var->value) ? 1 : 0;
			ast_verb(3, "codec_g722: %susing generic PLC\n",
					g722tolin.useplc ? "" : "not ");
		}
	}
	ast_config_destroy(cfg);
	return 0;
}

static int reload(void)
{
	if (parse_config(1))
		return AST_MODULE_LOAD_DECLINE;
	return AST_MODULE_LOAD_SUCCESS;
}

static int unload_module(void)
{
	int res = 0;

	res |= ast_unregister_translator(&g722tolin);
	res |= ast_unregister_translator(&lintog722);
	res |= ast_unregister_translator(&g722tolin16);
	res |= ast_unregister_translator(&lin16tog722);

	return res;
}

static int load_module(void)
{
	int res = 0;

	if (parse_config(0))
		return AST_MODULE_LOAD_DECLINE;

	res |= ast_register_translator(&g722tolin);
	res |= ast_register_translator(&lintog722);
	res |= ast_register_translator(&g722tolin16);
	res |= ast_register_translator(&lin16tog722);

	if (res) {
		unload_module();
		return AST_MODULE_LOAD_FAILURE;
	}	

	return AST_MODULE_LOAD_SUCCESS;
}

AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ITU G.722-64kbps G722 Transcoder",
		.load = load_module,
		.unload = unload_module,
		.reload = reload,
	       );