2 * Attenuated route Plug-In
3 * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/driver.h>
23 #include <linux/slab.h>
24 #include <linux/time.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include "pcm_plugin.h"
29 /* The best possible hack to support missing optimization in gcc 2.7.2.3 */
30 #if ROUTE_PLUGIN_RESOLUTION & (ROUTE_PLUGIN_RESOLUTION - 1) != 0
31 #define div(a) a /= ROUTE_PLUGIN_RESOLUTION
32 #elif ROUTE_PLUGIN_RESOLUTION == 16
33 #define div(a) a >>= 4
35 #error "Add some code here"
38 typedef struct ttable_dst ttable_dst_t;
39 typedef struct route_private_data route_t;
41 typedef void (*route_channel_f)(snd_pcm_plugin_t *plugin,
42 const snd_pcm_plugin_channel_t *src_channels,
43 snd_pcm_plugin_channel_t *dst_channel,
44 ttable_dst_t* ttable, snd_pcm_uframes_t frames);
49 #if ROUTE_PLUGIN_USE_FLOAT
55 int att; /* Attenuated */
61 struct route_private_data {
62 enum {R_UINT32=0, R_UINT64=1, R_FLOAT=2} sum_type;
66 ttable_dst_t ttable[0];
72 #if ROUTE_PLUGIN_USE_FLOAT
78 static void route_to_channel_from_zero(snd_pcm_plugin_t *plugin,
79 const snd_pcm_plugin_channel_t *src_channels ATTRIBUTE_UNUSED,
80 snd_pcm_plugin_channel_t *dst_channel,
81 ttable_dst_t* ttable ATTRIBUTE_UNUSED, snd_pcm_uframes_t frames)
83 if (dst_channel->wanted)
84 snd_pcm_area_silence(&dst_channel->area, 0, frames, plugin->dst_format.format);
85 dst_channel->enabled = 0;
88 static void route_to_channel_from_one(snd_pcm_plugin_t *plugin,
89 const snd_pcm_plugin_channel_t *src_channels,
90 snd_pcm_plugin_channel_t *dst_channel,
91 ttable_dst_t* ttable, snd_pcm_uframes_t frames)
94 #include "plugin_ops.h"
96 route_t *data = (route_t *)plugin->extra_data;
98 const snd_pcm_plugin_channel_t *src_channel = NULL;
101 int src_step, dst_step;
102 for (srcidx = 0; srcidx < ttable->nsrcs; ++srcidx) {
103 src_channel = &src_channels[ttable->srcs[srcidx].channel];
104 if (src_channel->area.addr != NULL)
107 if (srcidx == ttable->nsrcs) {
108 route_to_channel_from_zero(plugin, src_channels, dst_channel, ttable, frames);
112 dst_channel->enabled = 1;
113 conv = conv_labels[data->conv];
114 src = src_channel->area.addr + src_channel->area.first / 8;
115 src_step = src_channel->area.step / 8;
116 dst = dst_channel->area.addr + dst_channel->area.first / 8;
117 dst_step = dst_channel->area.step / 8;
118 while (frames-- > 0) {
120 #define CONV_END after
121 #include "plugin_ops.h"
129 static void route_to_channel(snd_pcm_plugin_t *plugin,
130 const snd_pcm_plugin_channel_t *src_channels,
131 snd_pcm_plugin_channel_t *dst_channel,
132 ttable_dst_t* ttable, snd_pcm_uframes_t frames)
135 #define PUT_U32_LABELS
136 #include "plugin_ops.h"
138 #undef PUT_U32_LABELS
139 static void *zero_labels[3] = { &&zero_int32, &&zero_int64,
140 #if ROUTE_PLUGIN_USE_FLOAT
145 static void *add_labels[3 * 2] = { &&add_int32_noatt, &&add_int32_att,
146 &&add_int64_noatt, &&add_int64_att,
147 #if ROUTE_PLUGIN_USE_FLOAT
148 &&add_float_noatt, &&add_float_att
151 /* sum_type att shift */
152 static void *norm_labels[3 * 2 * 4] = { NULL,
153 &&norm_int32_8_noatt,
154 &&norm_int32_16_noatt,
155 &&norm_int32_24_noatt,
160 &&norm_int64_0_noatt,
161 &&norm_int64_8_noatt,
162 &&norm_int64_16_noatt,
163 &&norm_int64_24_noatt,
168 #if ROUTE_PLUGIN_USE_FLOAT
179 route_t *data = (route_t *)plugin->extra_data;
180 void *zero, *get, *add, *norm, *put_u32;
181 int nsrcs = ttable->nsrcs;
185 int src_steps[nsrcs];
186 ttable_src_t src_tt[nsrcs];
187 u_int32_t sample = 0;
188 int srcidx, srcidx1 = 0;
189 for (srcidx = 0; srcidx < nsrcs; ++srcidx) {
190 const snd_pcm_plugin_channel_t *src_channel = &src_channels[ttable->srcs[srcidx].channel];
191 if (!src_channel->enabled)
193 srcs[srcidx1] = src_channel->area.addr + src_channel->area.first / 8;
194 src_steps[srcidx1] = src_channel->area.step / 8;
195 src_tt[srcidx1] = ttable->srcs[srcidx];
200 route_to_channel_from_zero(plugin, src_channels, dst_channel, ttable, frames);
202 } else if (nsrcs == 1 && src_tt[0].as_int == ROUTE_PLUGIN_RESOLUTION) {
203 route_to_channel_from_one(plugin, src_channels, dst_channel, ttable, frames);
207 dst_channel->enabled = 1;
208 zero = zero_labels[data->sum_type];
209 get = get_u_labels[data->get];
210 add = add_labels[data->sum_type * 2 + ttable->att];
211 norm = norm_labels[data->sum_type * 8 + ttable->att * 4 + 4 - data->src_sample_size];
212 put_u32 = put_u32_labels[data->put];
213 dst = dst_channel->area.addr + dst_channel->area.first / 8;
214 dst_step = dst_channel->area.step / 8;
216 while (frames-- > 0) {
217 ttable_src_t *ttp = src_tt;
228 #if ROUTE_PLUGIN_USE_FLOAT
234 for (srcidx = 0; srcidx < nsrcs; ++srcidx) {
235 char *src = srcs[srcidx];
239 #define GET_U_END after_get
240 #include "plugin_ops.h"
247 sum.as_uint32 += sample * ttp->as_int;
251 sum.as_uint32 += sample;
254 sum.as_uint64 += (u_int64_t) sample * ttp->as_int;
258 sum.as_uint64 += sample;
260 #if ROUTE_PLUGIN_USE_FLOAT
262 sum.as_float += sample * ttp->as_float;
266 sum.as_float += sample;
270 srcs[srcidx] += src_steps[srcidx];
277 sum.as_uint64 = sum.as_uint32;
285 sum.as_uint64 = sum.as_uint32;
287 sum.as_uint64 <<= 16;
292 sum.as_uint64 = sum.as_uint32;
294 sum.as_uint64 <<= 24;
299 sum.as_uint64 = sum.as_uint32;
305 sum.as_uint64 = sum.as_uint32;
307 sum.as_uint64 <<= 16;
311 sum.as_uint64 = sum.as_uint32;
313 sum.as_uint64 <<= 24;
318 if (sum.as_uint64 > (u_int32_t)0xffffffff)
319 sample = (u_int32_t)0xffffffff;
321 sample = sum.as_uint64;
324 #if ROUTE_PLUGIN_USE_FLOAT
326 sum.as_float *= 1 << 8;
329 sum.as_float *= 1 << 16;
332 sum.as_float *= 1 << 24;
336 sum.as_float = floor(sum.as_float + 0.5);
337 if (sum.as_float > (u_int32_t)0xffffffff)
338 sample = (u_int32_t)0xffffffff;
340 sample = sum.as_float;
347 #define PUT_U32_END after_put_u32
348 #include "plugin_ops.h"
356 int route_src_channels_mask(snd_pcm_plugin_t *plugin,
358 bitset_t **src_vmask)
360 route_t *data = (route_t *)plugin->extra_data;
361 int schannels = plugin->src_format.channels;
362 int dchannels = plugin->dst_format.channels;
363 bitset_t *vmask = plugin->src_vmask;
365 ttable_dst_t *dp = data->ttable;
366 bitset_zero(vmask, schannels);
367 for (channel = 0; channel < dchannels; channel++, dp++) {
370 if (!bitset_get(dst_vmask, channel))
373 for (src = 0; src < dp->nsrcs; src++, sp++)
374 bitset_set(vmask, sp->channel);
380 int route_dst_channels_mask(snd_pcm_plugin_t *plugin,
382 bitset_t **dst_vmask)
384 route_t *data = (route_t *)plugin->extra_data;
385 int dchannels = plugin->dst_format.channels;
386 bitset_t *vmask = plugin->dst_vmask;
388 ttable_dst_t *dp = data->ttable;
389 bitset_zero(vmask, dchannels);
390 for (channel = 0; channel < dchannels; channel++, dp++) {
394 for (src = 0; src < dp->nsrcs; src++, sp++) {
395 if (bitset_get(src_vmask, sp->channel)) {
396 bitset_set(vmask, channel);
405 static void route_free(snd_pcm_plugin_t *plugin)
407 route_t *data = (route_t *)plugin->extra_data;
408 unsigned int dst_channel;
409 for (dst_channel = 0; dst_channel < plugin->dst_format.channels; ++dst_channel) {
410 if (data->ttable[dst_channel].srcs != NULL)
411 kfree(data->ttable[dst_channel].srcs);
415 static int route_load_ttable(snd_pcm_plugin_t *plugin,
416 const route_ttable_entry_t* src_ttable)
419 unsigned int src_channel, dst_channel;
420 const route_ttable_entry_t *sptr;
422 if (src_ttable == NULL)
424 data = (route_t *)plugin->extra_data;
427 plugin->private_free = route_free;
428 for (dst_channel = 0; dst_channel < plugin->dst_format.channels; ++dst_channel) {
429 route_ttable_entry_t t = 0;
432 ttable_src_t srcs[plugin->src_format.channels];
433 for (src_channel = 0; src_channel < plugin->src_format.channels; ++src_channel) {
434 snd_assert(*sptr >= 0 || *sptr <= FULL, return -ENXIO);
436 srcs[nsrcs].channel = src_channel;
437 #if ROUTE_PLUGIN_USE_FLOAT
438 /* Also in user space for non attenuated */
439 srcs[nsrcs].as_int = (*sptr == FULL ? ROUTE_PLUGIN_RESOLUTION : 0);
440 srcs[nsrcs].as_float = *sptr;
442 srcs[nsrcs].as_int = *sptr;
454 dptr->func = route_to_channel_from_zero;
455 else if (nsrcs == 1 && !att)
456 dptr->func = route_to_channel_from_one;
458 dptr->func = route_to_channel;
461 dptr->srcs = kcalloc(nsrcs, sizeof(*srcs), GFP_KERNEL);
462 for(srcidx = 0; srcidx < nsrcs; srcidx++)
463 dptr->srcs[srcidx] = srcs[srcidx];
471 static snd_pcm_sframes_t route_transfer(snd_pcm_plugin_t *plugin,
472 const snd_pcm_plugin_channel_t *src_channels,
473 snd_pcm_plugin_channel_t *dst_channels,
474 snd_pcm_uframes_t frames)
477 int src_nchannels, dst_nchannels;
480 snd_pcm_plugin_channel_t *dvp;
482 snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO);
485 data = (route_t *)plugin->extra_data;
487 src_nchannels = plugin->src_format.channels;
488 dst_nchannels = plugin->dst_format.channels;
490 #ifdef CONFIG_SND_DEBUG
493 for (src_channel = 0; src_channel < src_nchannels; ++src_channel) {
494 snd_assert(src_channels[src_channel].area.first % 8 == 0 ||
495 src_channels[src_channel].area.step % 8 == 0,
498 for (dst_channel = 0; dst_channel < dst_nchannels; ++dst_channel) {
499 snd_assert(dst_channels[dst_channel].area.first % 8 == 0 ||
500 dst_channels[dst_channel].area.step % 8 == 0,
508 for (dst_channel = 0; dst_channel < dst_nchannels; ++dst_channel) {
509 ttp->func(plugin, src_channels, dvp, ttp, frames);
516 int getput_index(int format)
518 int sign, width, endian;
519 sign = !snd_pcm_format_signed(format);
520 width = snd_pcm_format_width(format) / 8 - 1;
521 if (width < 0 || width > 3) {
522 snd_printk(KERN_ERR "snd-pcm-oss: invalid format %d\n", format);
525 #ifdef SNDRV_LITTLE_ENDIAN
526 endian = snd_pcm_format_big_endian(format);
528 endian = snd_pcm_format_little_endian(format);
532 return width * 4 + endian * 2 + sign;
535 int snd_pcm_plugin_build_route(snd_pcm_plug_t *plug,
536 snd_pcm_plugin_format_t *src_format,
537 snd_pcm_plugin_format_t *dst_format,
538 route_ttable_entry_t *ttable,
539 snd_pcm_plugin_t **r_plugin)
542 snd_pcm_plugin_t *plugin;
545 snd_assert(r_plugin != NULL, return -ENXIO);
547 snd_assert(src_format->rate == dst_format->rate, return -ENXIO);
548 snd_assert(snd_pcm_format_linear(src_format->format) != 0 &&
549 snd_pcm_format_linear(dst_format->format) != 0,
552 err = snd_pcm_plugin_build(plug, "attenuated route conversion",
553 src_format, dst_format,
554 sizeof(route_t) + sizeof(data->ttable[0]) * dst_format->channels,
559 data = (route_t *) plugin->extra_data;
561 data->get = getput_index(src_format->format);
562 data->put = getput_index(dst_format->format);
563 data->conv = conv_index(src_format->format, dst_format->format);
565 #if ROUTE_PLUGIN_USE_FLOAT
566 data->sum_type = R_FLOAT;
568 if (snd_pcm_format_width(src_format->format) == 32)
569 data->sum_type = R_UINT64;
571 data->sum_type = R_UINT32;
573 data->src_sample_size = snd_pcm_format_width(src_format->format) / 8;
575 if ((err = route_load_ttable(plugin, ttable)) < 0) {
576 snd_pcm_plugin_free(plugin);
579 plugin->transfer = route_transfer;
580 plugin->src_channels_mask = route_src_channels_mask;
581 plugin->dst_channels_mask = route_dst_channels_mask;