summaryrefslogtreecommitdiff
path: root/modules/speex_aec/speex_aec.c
blob: 15ea552a212350ea9e4c27ea30cf3d98bb983497 (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
/**
 * @file speex_aec.c  Speex Acoustic Echo Cancellation
 *
 * Copyright (C) 2010 Creytiv.com
 */
#include <stdlib.h>
#include <string.h>
#include <speex/speex.h>
#include <speex/speex_echo.h>
#include <re.h>
#include <baresip.h>


struct speex_st {
	int16_t *out;
	SpeexEchoState *state;
};

struct enc_st {
	struct aufilt_enc_st af;  /* base class */
	struct speex_st *st;
};

struct dec_st {
	struct aufilt_dec_st af;  /* base class */
	struct speex_st *st;
};


static void enc_destructor(void *arg)
{
	struct enc_st *st = arg;

	list_unlink(&st->af.le);
	mem_deref(st->st);
}


static void dec_destructor(void *arg)
{
	struct dec_st *st = arg;

	list_unlink(&st->af.le);
	mem_deref(st->st);
}


#ifdef SPEEX_SET_VBR_MAX_BITRATE
static void speex_aec_destructor(void *arg)
{
	struct speex_st *st = arg;

	if (st->state)
		speex_echo_state_destroy(st->state);

	mem_deref(st->out);
}


static int aec_alloc(struct speex_st **stp, void **ctx, struct aufilt_prm *prm)
{
	struct speex_st *st;
	uint32_t sampc;
	int err, tmp, fl;

	if (!stp || !ctx || !prm)
		return EINVAL;

	if (*ctx) {
		*stp = mem_ref(*ctx);
		return 0;
	}

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

	sampc = prm->srate * prm->ch * prm->ptime / 1000;

	st->out = mem_alloc(2 * sampc, NULL);
	if (!st->out) {
		err = ENOMEM;
		goto out;
	}

	/* Echo canceller with 200 ms tail length */
	fl = 10 * sampc;
	st->state = speex_echo_state_init(sampc, fl);
	if (!st->state) {
		err = ENOMEM;
		goto out;
	}

	tmp = prm->srate;
	err = speex_echo_ctl(st->state, SPEEX_ECHO_SET_SAMPLING_RATE, &tmp);
	if (err < 0) {
		warning("speex_aec: speex_echo_ctl: err=%d\n", err);
	}

	info("speex_aec: Speex AEC loaded: srate = %uHz\n", prm->srate);

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

	return err;
}


static int encode_update(struct aufilt_enc_st **stp, void **ctx,
			 const struct aufilt *af, struct aufilt_prm *prm)
{
	struct enc_st *st;
	int err;

	if (!stp || !ctx || !af || !prm)
		return EINVAL;

	if (*stp)
		return 0;

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

	err = aec_alloc(&st->st, ctx, prm);

	if (err)
		mem_deref(st);
	else
		*stp = (struct aufilt_enc_st *)st;

	return err;
}


static int decode_update(struct aufilt_dec_st **stp, void **ctx,
			 const struct aufilt *af, struct aufilt_prm *prm)
{
	struct dec_st *st;
	int err;

	if (!stp || !ctx || !af || !prm)
		return EINVAL;

	if (*stp)
		return 0;

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

	err = aec_alloc(&st->st, ctx, prm);

	if (err)
		mem_deref(st);
	else
		*stp = (struct aufilt_dec_st *)st;

	return err;
}


static int encode(struct aufilt_enc_st *st, int16_t *sampv, size_t *sampc)
{
	struct enc_st *est = (struct enc_st *)st;
	struct speex_st *sp = est->st;

	if (*sampc) {
		speex_echo_capture(sp->state, sampv, sp->out);
		memcpy(sampv, sp->out, *sampc * 2);
	}

	return 0;
}


static int decode(struct aufilt_dec_st *st, int16_t *sampv, size_t *sampc)
{
	struct dec_st *dst = (struct dec_st *)st;
	struct speex_st *sp = dst->st;

	if (*sampc)
		speex_echo_playback(sp->state, sampv);

	return 0;
}
#endif


static struct aufilt speex_aec = {
	LE_INIT, "speex_aec", encode_update, encode, decode_update, decode
};


static int module_init(void)
{
	/* Note: Hack to check libspeex version */
#ifdef SPEEX_SET_VBR_MAX_BITRATE
	aufilt_register(&speex_aec);
	return 0;
#else
	return ENOSYS;
#endif
}


static int module_close(void)
{
	aufilt_unregister(&speex_aec);
	return 0;
}


EXPORT_SYM const struct mod_export DECL_EXPORTS(speex_aec) = {
	"speex_aec",
	"filter",
	module_init,
	module_close
};