summaryrefslogtreecommitdiff
path: root/modules/silk/silk.c
blob: dee296c29b6becf511ca841bcb45497879c60a62 (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
/**
 * @file silk.c  Skype SILK audio codec
 *
 * Copyright (C) 2010 - 2015 Creytiv.com
 */
#include <re.h>
#include <baresip.h>
#include <silk/SKP_Silk_SDK_API.h>


/**
 * @defgroup silk silk
 *
 * The Skype SILK audio codec
 *
 * References:  https://developer.skype.com/silk
 */


enum {
	MAX_BYTES_PER_FRAME = 250,
	MAX_FRAME_SIZE      = 2*480,
};


struct auenc_state {
	void *enc;
	SKP_SILK_SDK_EncControlStruct encControl;
};

struct audec_state {
	void *dec;
	SKP_SILK_SDK_DecControlStruct decControl;
};


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

	mem_deref(st->enc);
}


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

	mem_deref(st->dec);
}


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

	if (!aesp || !ac || !prm)
		return EINVAL;
	if (*aesp)
		return 0;

	ret = SKP_Silk_SDK_Get_Encoder_Size(&enc_size);
	if (ret || enc_size <= 0)
		return EINVAL;

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

	st->enc = mem_alloc(enc_size, NULL);
	if (!st->enc) {
		err = ENOMEM;
		goto out;
	}

	ret = SKP_Silk_SDK_InitEncoder(st->enc, &st->encControl);
	if (ret) {
		err = EPROTO;
		goto out;
	}

	st->encControl.API_sampleRate = ac->srate;
	st->encControl.maxInternalSampleRate = ac->srate;
	st->encControl.packetSize = prm->ptime * ac->srate / 1000;
	st->encControl.bitRate = 64000;
	st->encControl.complexity = 2;
	st->encControl.useInBandFEC = 0;
	st->encControl.useDTX = 0;

	info("silk: encoder: %dHz, psize=%d, bitrate=%d, complex=%d,"
	     " fec=%d, dtx=%d\n",
	     st->encControl.API_sampleRate,
	     st->encControl.packetSize,
	     st->encControl.bitRate,
	     st->encControl.complexity,
	     st->encControl.useInBandFEC,
	     st->encControl.useDTX);

 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 ret, err = 0;
	int32_t dec_size;
	(void)fmtp;

	if (*adsp)
		return 0;

	ret = SKP_Silk_SDK_Get_Decoder_Size(&dec_size);
	if (ret || dec_size <= 0)
		return EINVAL;

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

	st->dec = mem_alloc(dec_size, NULL);
	if (!st->dec) {
		err = ENOMEM;
		goto out;
	}

	ret = SKP_Silk_SDK_InitDecoder(st->dec);
	if (ret) {
		err = EPROTO;
		goto out;
	}

	st->decControl.API_sampleRate = ac->srate;

 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)
{
	int ret;
	int16_t nBytesOut;

	if (*len < MAX_BYTES_PER_FRAME)
		return ENOMEM;

	nBytesOut = *len;
	ret = SKP_Silk_SDK_Encode(st->enc,
				  &st->encControl,
				  sampv,
				  (int)sampc,
				  buf,
				  &nBytesOut);
	if (ret) {
		warning("silk: SKP_Silk_SDK_Encode: ret=%d\n", ret);
	}

	*len = nBytesOut;

	return 0;
}


static int decode(struct audec_state *st, int16_t *sampv,
		  size_t *sampc, const uint8_t *buf, size_t len)
{
	int16_t nsamp = *sampc;
	int ret;

	ret = SKP_Silk_SDK_Decode(st->dec,
				  &st->decControl,
				  0,
				  buf,
				  (int)len,
				  sampv,
				  &nsamp);
	if (ret) {
		warning("silk: SKP_Silk_SDK_Decode: ret=%d\n", ret);
	}

	*sampc = nsamp;

	return 0;
}


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

	ret = SKP_Silk_SDK_Decode(st->dec,
				  &st->decControl,
				  1,
				  NULL,
				  0,
				  sampv,
				  &nsamp);
	if (ret)
		return EPROTO;

	*sampc = nsamp;

	return 0;
}


static struct aucodec silk[] = {
	{
		LE_INIT, 0, "SILK", 24000, 24000, 1, NULL,
		encode_update, encode, decode_update, decode, plc, 0, 0
	},

};


static int module_init(void)
{
	debug("silk: SILK %s\n", SKP_Silk_SDK_get_version());

	aucodec_register(baresip_aucodecl(), &silk[0]);

	return 0;
}


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

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

	return 0;
}


EXPORT_SYM const struct mod_export DECL_EXPORTS(silk) = {
	"silk",
	"codec",
	module_init,
	module_close
};