summaryrefslogtreecommitdiff
path: root/modules/isac/isac.c
blob: 868ef0e6dc679a6387ca591db79df1d104ddee31 (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
/**
 * @file isac.c iSAC audio codec
 *
 * Copyright (C) 2010 Creytiv.com
 */
#include <re.h>
#include <baresip.h>
#include "isac.h"


/**
 * @defgroup isac isac
 *
 * iSAC audio codec
 *
 * draft-ietf-avt-rtp-isac-04
 */


struct auenc_state {
	ISACStruct *inst;
};

struct audec_state {
	ISACStruct *inst;
};


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

	if (st->inst)
		WebRtcIsac_Free(st->inst);
}


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

	if (st->inst)
		WebRtcIsac_Free(st->inst);
}


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_alloc(sizeof(*st), encode_destructor);
	if (!st)
		return ENOMEM;

	if (WebRtcIsac_Create(&st->inst) < 0) {
		err = ENOMEM;
		goto out;
	}

	WebRtcIsac_EncoderInit(st->inst, 0);

	if (ac->srate == 32000)
		WebRtcIsac_SetEncSampRate(st->inst, kIsacSuperWideband);

 out:
	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_alloc(sizeof(*st), decode_destructor);
	if (!st)
		return ENOMEM;

	if (WebRtcIsac_Create(&st->inst) < 0) {
		err = ENOMEM;
		goto out;
	}

	WebRtcIsac_DecoderInit(st->inst);

	if (ac->srate == 32000)
		WebRtcIsac_SetDecSampRate(st->inst, kIsacSuperWideband);

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

	return err;
}


static int encode(struct auenc_state *st, uint8_t *buf, size_t *len,
		  const int16_t *sampv, size_t sampc)
{
	WebRtc_Word16 len1, len2;
	size_t l;

	if (!st || !buf || !len || !sampv || !sampc)
		return EINVAL;

	/* 10 ms audio blocks */
	len1 = WebRtcIsac_Encode(st->inst, sampv,           (void *)buf);
	len2 = WebRtcIsac_Encode(st->inst, &sampv[sampc/2], (void *)buf);

	l = len1 ? len1 : len2;

	if (l > *len)
		return ENOMEM;

	*len = l;

	return 0;
}


static int decode(struct audec_state *st, int16_t *sampv,
		  size_t *sampc, const uint8_t *buf, size_t len)
{
	WebRtc_Word16 speechType;
	int n;

	if (!st || !sampv || !sampc || !buf || !len)
		return EINVAL;

	n = WebRtcIsac_Decode(st->inst, (void *)buf, len,
			      (void *)sampv, &speechType);
	if (n < 0)
		return EPROTO;

	if ((size_t)n > *sampc)
		return ENOMEM;

	*sampc = n;

	return 0;
}


static int plc(struct audec_state *st, int16_t *sampv, size_t *sampc)
{
	int n;

	if (!st || !sampv || !sampc)
		return EINVAL;

	n = WebRtcIsac_DecodePlc(st->inst, (void *)sampv, 1);
	if (n < 0)
		return EPROTO;

	*sampc = n;

	return 0;
}


static struct aucodec isacv[] = {
	{
	LE_INIT, 0, "isac", 32000, 32000, 1, NULL,
	encode_update, encode, decode_update, decode, plc, NULL, NULL
	},
	{
	LE_INIT, 0, "isac", 16000, 16000, 1, NULL,
	encode_update, encode, decode_update, decode, plc, NULL, NULL
	}
};


static int module_init(void)
{
	unsigned i;

	for (i=0; i<ARRAY_SIZE(isacv); i++)
		aucodec_register(baresip_aucodecl(), &isacv[i]);

	return 0;
}


static int module_close(void)
{
	int i = ARRAY_SIZE(isacv);

	while (i--)
		aucodec_unregister(&isacv[i]);

	return 0;
}


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