summaryrefslogtreecommitdiff
path: root/modules/amr/amr.c
blob: 52c20b370439321c71770194cc7cc41c0655452d (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
341
342
343
344
345
/**
 * @file amr.c Adaptive Multi-Rate (AMR) audio codec
 *
 * Copyright (C) 2010 Creytiv.com
 */
#include <stdlib.h>
#ifdef AMR_NB
#include <interf_enc.h>
#include <interf_dec.h>
#endif
#ifdef AMR_WB
#ifdef _TYPEDEF_H
#define typedef_h
#endif
#include <enc_if.h>
#include <dec_if.h>
#endif
#include <re.h>
#include <baresip.h>
#include "amr.h"


#ifdef VO_AMRWBENC_ENC_IF_H
#define IF2E_IF_encode E_IF_encode
#define IF2D_IF_decode D_IF_decode
#endif


/**
 * @defgroup amr amr
 *
 * This module supports both AMR Narrowband (8000 Hz) and
 * AMR Wideband (16000 Hz) audio codecs.
 *
 * NOTE: only octet-align mode is supported.
 *
 *
 * Reference:
 *
 *     http://tools.ietf.org/html/rfc4867
 *
 *     http://www.penguin.cz/~utx/amr
 */


#ifndef L_FRAME16k
#define L_FRAME16k 320
#endif

#ifndef NB_SERIAL_MAX
#define NB_SERIAL_MAX 61
#endif

enum {
	FRAMESIZE_NB = 160
};


struct auenc_state {
	const struct aucodec *ac;
	void *enc;                  /**< Encoder state            */
};

struct audec_state {
	const struct aucodec *ac;
	void *dec;                  /**< Decoder state            */
};


static void encode_destructor(void *arg)
{
	struct auenc_state *st = arg;

	switch (st->ac->srate) {

#ifdef AMR_NB
	case 8000:
		Encoder_Interface_exit(st->enc);
		break;
#endif

#ifdef AMR_WB
	case 16000:
		E_IF_exit(st->enc);
		break;
#endif
	}
}


static void decode_destructor(void *arg)
{
	struct audec_state *st = arg;

	switch (st->ac->srate) {

#ifdef AMR_NB
	case 8000:
		Decoder_Interface_exit(st->dec);
		break;
#endif

#ifdef AMR_WB
	case 16000:
		D_IF_exit(st->dec);
		break;
#endif
	}
}


static int encode_update(struct auenc_state **aesp,
			 const struct aucodec *ac,
			 struct auenc_param *prm, const char *fmtp)
{
	struct auenc_state *st;
	int err = 0;
	(void)prm;
	(void)fmtp;

	if (!aesp || !ac)
		return EINVAL;

	if (*aesp)
		return 0;

	st = mem_zalloc(sizeof(*st), encode_destructor);
	if (!st)
		return ENOMEM;

	st->ac = ac;

	switch (ac->srate) {

#ifdef AMR_NB
	case 8000:
		st->enc = Encoder_Interface_init(0);
		break;
#endif

#ifdef AMR_WB
	case 16000:
		st->enc = E_IF_init();
		break;
#endif
	}

	if (!st->enc)
		err = ENOMEM;

	if (err)
		mem_deref(st);
	else
		*aesp = st;

	return err;
}


static int decode_update(struct audec_state **adsp,
			 const struct aucodec *ac, const char *fmtp)
{
	struct audec_state *st;
	int err = 0;
	(void)fmtp;

	if (!adsp || !ac)
		return EINVAL;

	if (*adsp)
		return 0;

	st = mem_zalloc(sizeof(*st), decode_destructor);
	if (!st)
		return ENOMEM;

	st->ac = ac;

	switch (ac->srate) {

#ifdef AMR_NB
	case 8000:
		st->dec = Decoder_Interface_init();
		break;
#endif

#ifdef AMR_WB
	case 16000:
		st->dec = D_IF_init();
		break;
#endif
	}

	if (!st->dec)
		err = ENOMEM;

	if (err)
		mem_deref(st);
	else
		*adsp = st;

	return err;
}


#ifdef AMR_WB
static int encode_wb(struct auenc_state *st, uint8_t *buf, size_t *len,
		     const int16_t *sampv, size_t sampc)
{
	int n;

	if (sampc != L_FRAME16k)
		return EINVAL;

	if (*len < NB_SERIAL_MAX)
		return ENOMEM;

	/* CMR value 15 indicates that no mode request is present */
	buf[0] = 15 << 4;

	n = IF2E_IF_encode(st->enc, 8, sampv, &buf[1], 0);
	if (n <= 0)
		return EPROTO;

	*len = (1 + n);

	return 0;
}


static int decode_wb(struct audec_state *st, int16_t *sampv, size_t *sampc,
		     const uint8_t *buf, size_t len)
{
	if (*sampc < L_FRAME16k)
		return ENOMEM;
	if (len > NB_SERIAL_MAX)
		return EINVAL;

	IF2D_IF_decode(st->dec, &buf[1], sampv, 0);

	*sampc = L_FRAME16k;

	return 0;
}
#endif


#ifdef AMR_NB
static int encode_nb(struct auenc_state *st, uint8_t *buf,
		     size_t *len, const int16_t *sampv, size_t sampc)
{
	int r;

	if (!st || !buf || !len || !sampv || sampc != FRAMESIZE_NB)
		return EINVAL;
	if (*len < NB_SERIAL_MAX)
		return ENOMEM;

	/* CMR value 15 indicates that no mode request is present */
	buf[0] = 15 << 4;

	r = Encoder_Interface_Encode(st->enc, MR122, sampv, &buf[1], 0);
	if (r <= 0)
		return EPROTO;

	*len = (1 + r);

	return 0;
}


static int decode_nb(struct audec_state *st, int16_t *sampv,
		     size_t *sampc, const uint8_t *buf, size_t len)
{
	if (!st || !sampv || !sampc || !buf)
		return EINVAL;

	if (len > NB_SERIAL_MAX)
		return EPROTO;

	if (*sampc < L_FRAME16k)
		return ENOMEM;

	Decoder_Interface_Decode(st->dec, &buf[1], sampv, 0);

	*sampc = FRAMESIZE_NB;

	return 0;
}
#endif


#ifdef AMR_WB
static struct aucodec amr_wb = {
	LE_INIT, NULL, "AMR-WB", 16000, 16000, 1, NULL,
	encode_update, encode_wb,
	decode_update, decode_wb,
	NULL, amr_fmtp_enc, amr_fmtp_cmp
};
#endif
#ifdef AMR_NB
static struct aucodec amr_nb = {
	LE_INIT, NULL, "AMR", 8000, 8000, 1, NULL,
	encode_update, encode_nb,
	decode_update, decode_nb,
	NULL, amr_fmtp_enc, amr_fmtp_cmp
};
#endif


static int module_init(void)
{
	int err = 0;

#ifdef AMR_WB
	aucodec_register(baresip_aucodecl(), &amr_wb);
#endif
#ifdef AMR_NB
	aucodec_register(baresip_aucodecl(), &amr_nb);
#endif

	return err;
}


static int module_close(void)
{
#ifdef AMR_WB
	aucodec_unregister(&amr_wb);
#endif
#ifdef AMR_NB
	aucodec_unregister(&amr_nb);
#endif

	return 0;
}


/** Module exports */
EXPORT_SYM const struct mod_export DECL_EXPORTS(amr) = {
	"amr",
	"codec",
	module_init,
	module_close
};