summaryrefslogtreecommitdiffstats
path: root/mix/pcm_vdownmix.c
blob: 85eafad11cc87881d5df9dbd662b12a73935b0fc (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
/*
 * 4/5/6 -> 2 downmix with a simple spacialization
 *
 * Copyright (c) 2006 by Takashi Iwai <tiwai@suse.de>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <alsa/asoundlib.h>
#include <alsa/pcm_external.h>

/* #define I_AM_POWERFUL */

#ifdef I_AM_POWERFUL
#define RINGBUF_SIZE	(1 << 9)
#else
#define RINGBUF_SIZE	(1 << 7)
#endif
#define RINGBUF_MASK	(RINGBUF_SIZE - 1)

struct vdownmix_tap {
	int delay;
	int weight;
};

#define MAX_TAPS	30

struct vdownmix_filter {
	int taps;
	struct vdownmix_tap tap[MAX_TAPS];
};

typedef struct {
	snd_pcm_extplug_t ext;
	int channels;
	unsigned int curpos;
	short rbuf[RINGBUF_SIZE][5];
} snd_pcm_vdownmix_t;

static const struct vdownmix_filter tap_filters[5] = {
	{
#ifdef I_AM_POWERFUL
		18,
#else
		14,
#endif
		{{ 0, 0xfffffd0a },
		 { 1, 0x41d },
		 { 2, 0xffffe657 },
		 { 3, 0x6eb5 },
		 { 4, 0xffffe657 },
		 { 5, 0x41d },
		 { 6, 0xfffffd0a },
		 { 71, 0xffffff1c },
		 { 72, 0x12e },
		 { 73, 0xfffff81a },
		 { 74, 0x24de },
		 { 75, 0xfffff81a },
		 { 76, 0x12e },
		 { 77, 0xffffff1c },
		 { 265, 0xfffffc65 },
		 { 266, 0xee1 },
		 { 267, 0xfffffc65 },
		 { 395, 0x46a }},
	},

	{
#ifdef I_AM_POWERFUL
		17,
#else
		10,
#endif
		{{ 8, 0xcf },
		 { 9, 0xa7b },
		 { 10, 0xcd7 },
		 { 11, 0x5b3 },
		 { 12, 0x859 },
		 { 13, 0xaf },
		 { 80, 0x38b },
		 { 81, 0x454 },
		 { 82, 0x218 },
		 { 83, 0x2c1 },
		 { 268, 0x58b },
		 { 275, 0xc2 },
		 { 397, 0xbd },
		 { 398, 0x1e8 },
		 { 506, 0xfffffeac },
		 { 507, 0x636 },
		 { 508, 0xfffffeac }},
	},

	{
#ifdef I_AM_POWERFUL
		11,
#else
		1,
#endif
		{{ 3, 0x4000 },
		 { 125, 0x12a },
		 { 126, 0xda1 },
		 { 127, 0x12a },
		 { 193, 0xfffffed3 },
		 { 194, 0xdb9 },
		 { 195, 0xfffffed3 },
		 { 454, 0x10a },
		 { 483, 0xfffffe97 },
		 { 484, 0x698 },
		 { 485, 0xfffffe97 }},
	},

	{
#ifdef I_AM_POWERFUL
		25,
#else
		10,
#endif
		{{ 5, 0x1cb },
		 { 6, 0x9c5 },
		 { 7, 0x117e },
		 { 8, 0x200 },
		 { 9, 0x533 },
		 { 10, 0x1c6 },
		 { 11, 0x167 },
		 { 12, 0x5ff },
		 { 13, 0x425 },
		 { 14, 0xd9 },
		 { 128, 0x247 },
		 { 129, 0x5e1 },
		 { 130, 0xb7 },
		 { 131, 0x122 },
		 { 135, 0x10a },
		 { 200, 0x1b6 },
		 { 201, 0xa7 },
		 { 202, 0x188 },
		 { 203, 0x1d9 },
		 { 445, 0xffffff44 },
		 { 446, 0x5e2 },
		 { 447, 0xffffff44 },
		 { 484, 0xffffff51 },
		 { 485, 0x449 },
		 { 486, 0xffffff51 }},
	},

	{
#ifdef I_AM_POWERFUL
		21,
#else
		7,
#endif
		{{ 0, 0xfffffdee },
		 { 1, 0x28b },
		 { 2, 0xffffed1e },
		 { 3, 0x6336 },
		 { 4, 0xffffed1e },
		 { 5, 0x28b },
		 { 6, 0xfffffdee },
		 { 51, 0xffffff2c },
		 { 52, 0x105 },
		 { 53, 0xfffff86b },
		 { 54, 0x27d9 },
		 { 55, 0xfffff86b },
		 { 56, 0x105 },
		 { 57, 0xffffff2c },
		 { 333, 0xfffffd69 },
		 { 334, 0xb2f },
		 { 335, 0xfffffd69 },
		 { 339, 0xdf },
		 { 340, 0x168 },
		 { 342, 0xa6 },
		 { 343, 0xba }},
	},
};

static const int tap_index[5][2] = {
	/* left */
	{ 0, 1 },
	/* right */
	{ 1, 0 },
	/* rear left */
	{ 2, 3 },
	/* rear right */
	{ 3, 2 },
	/* center */
	{ 4, 4 },
};

static inline void *area_addr(const snd_pcm_channel_area_t *area, snd_pcm_uframes_t offset)
{
	unsigned int bitofs = area->first + area->step * offset;
	return (char *) area->addr + bitofs / 8;
}

static inline unsigned int area_step(const snd_pcm_channel_area_t *area)
{
	return area->step / 8;
}

static snd_pcm_sframes_t
vdownmix_transfer(snd_pcm_extplug_t *ext,
		  const snd_pcm_channel_area_t *dst_areas,
		  snd_pcm_uframes_t dst_offset,
		  const snd_pcm_channel_area_t *src_areas,
		  snd_pcm_uframes_t src_offset,
		  snd_pcm_uframes_t size)
{
	snd_pcm_vdownmix_t *mix = (snd_pcm_vdownmix_t *)ext;
	short *src[mix->channels], *ptr[2];
	unsigned int src_step[mix->channels], step[2];
	int i, ch, curpos, p, idx;
	int acc[2];
	int fr;

	ptr[0] = area_addr(dst_areas, dst_offset);
	step[0] = area_step(dst_areas) / 2;
	ptr[1] = area_addr(dst_areas + 1, dst_offset);
	step[1] = area_step(dst_areas + 1) / 2;
	for (ch = 0; ch < mix->channels; ch++) {
		const snd_pcm_channel_area_t *src_area = &src_areas[ch];
		src[ch] = area_addr(src_area, src_offset);
		src_step[ch] = area_step(src_area) / 2;
	}
	curpos = mix->curpos;
	fr = size;
	while (fr--) {
		acc[0] = acc[1] = 0;
		for (ch = 0; ch < mix->channels; ch++) {
			mix->rbuf[curpos][ch] = *src[ch];
			for (idx = 0; idx < 2; idx++) {
				int f = tap_index[ch][idx];
				const struct vdownmix_filter *filter;
				filter = &tap_filters[f];
				for (i = 0; i < filter->taps; i++) {
					p = (curpos + RINGBUF_SIZE - filter->tap[i].delay)
						& RINGBUF_MASK;
					acc[idx] += mix->rbuf[p][ch] * filter->tap[i].weight;
				}
			}
			src[ch] += src_step[ch];
		}
		for (idx = 0; idx < 2; idx++) {
			acc[idx] >>= 14;
			if (acc[idx] < -32768)
				*ptr[idx] = -32768;
			else if (acc[idx] > 32767)
				*ptr[idx] = 32767;
			else
				*ptr[idx] = acc[idx];
			ptr[idx] += step[idx];
		}
		curpos = (curpos + 1) & RINGBUF_MASK;
	}
	mix->curpos = curpos;
	return size;
}


static int vdownmix_init(snd_pcm_extplug_t *ext)
{
	snd_pcm_vdownmix_t *mix = (snd_pcm_vdownmix_t *)ext;
	mix->channels = ext->channels;
	if (mix->channels > 5) /* ignore LFE */
		mix->channels = 5;
	mix->curpos = 0;
	memset(mix->rbuf, 0, sizeof(mix->rbuf));
	return 0;
}

static const snd_pcm_extplug_callback_t vdownmix_callback = {
	.transfer = vdownmix_transfer,
	.init = vdownmix_init,
	/* .dump = filr_dump, */
};

SND_PCM_PLUGIN_DEFINE_FUNC(vdownmix)
{
	snd_config_iterator_t i, next;
	snd_pcm_vdownmix_t *mix;
	snd_config_t *sconf = NULL;
	int err;

	snd_config_for_each(i, next, conf) {
		snd_config_t *n = snd_config_iterator_entry(i);
		const char *id;
		if (snd_config_get_id(n, &id) < 0)
			continue;
		if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 || strcmp(id, "hint") == 0)
			continue;
		if (strcmp(id, "slave") == 0) {
			sconf = n;
			continue;
		}
		SNDERR("Unknown field %s", id);
		return -EINVAL;
	}

	if (! sconf) {
		SNDERR("No slave configuration for vdownmix pcm");
		return -EINVAL;
	}

	mix = calloc(1, sizeof(*mix));
	if (mix == NULL)
		return -ENOMEM;

	mix->ext.version = SND_PCM_EXTPLUG_VERSION;
	mix->ext.name = "Vdownmix Plugin";
	mix->ext.callback = &vdownmix_callback;
	mix->ext.private_data = mix;

	err = snd_pcm_extplug_create(&mix->ext, name, root, sconf, stream, mode);
	if (err < 0) {
		free(mix);
		return err;
	}

	/* 4/5/6 -> 2 downmix */
	snd_pcm_extplug_set_param_minmax(&mix->ext, SND_PCM_EXTPLUG_HW_CHANNELS,
					 4, 6);
	snd_pcm_extplug_set_slave_param(&mix->ext, SND_PCM_EXTPLUG_HW_CHANNELS, 2);
	snd_pcm_extplug_set_param(&mix->ext, SND_PCM_EXTPLUG_HW_FORMAT,
				  SND_PCM_FORMAT_S16);
	snd_pcm_extplug_set_slave_param(&mix->ext, SND_PCM_EXTPLUG_HW_FORMAT,
					SND_PCM_FORMAT_S16);

	*pcmp = mix->ext.pcm;
	return 0;
}

SND_PCM_PLUGIN_SYMBOL(vdownmix);