ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / core / oss / route.c
1 /*
2  *  Attenuated route Plug-In
3  *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
4  *
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
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"
28
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
34 #else
35 #error "Add some code here"
36 #endif
37
38 typedef struct ttable_dst ttable_dst_t;
39 typedef struct route_private_data route_t;
40
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);
45
46 typedef struct {
47         int channel;
48         int as_int;
49 #if ROUTE_PLUGIN_USE_FLOAT
50         float as_float;
51 #endif
52 } ttable_src_t;
53
54 struct ttable_dst {
55         int att;        /* Attenuated */
56         unsigned int nsrcs;
57         ttable_src_t* srcs;
58         route_channel_f func;
59 };
60
61 struct route_private_data {
62         enum {R_UINT32=0, R_UINT64=1, R_FLOAT=2} sum_type;
63         int get, put;
64         int conv;
65         int src_sample_size;
66         ttable_dst_t ttable[0];
67 };
68
69 typedef union {
70         u_int32_t as_uint32;
71         u_int64_t as_uint64;
72 #if ROUTE_PLUGIN_USE_FLOAT
73         float as_float;
74 #endif
75 } sum_t;
76
77
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)
82 {
83         if (dst_channel->wanted)
84                 snd_pcm_area_silence(&dst_channel->area, 0, frames, plugin->dst_format.format);
85         dst_channel->enabled = 0;
86 }
87
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)
92 {
93 #define CONV_LABELS
94 #include "plugin_ops.h"
95 #undef CONV_LABELS
96         route_t *data = (route_t *)plugin->extra_data;
97         void *conv;
98         const snd_pcm_plugin_channel_t *src_channel = 0;
99         unsigned int srcidx;
100         char *src, *dst;
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)
105                         break;
106         }
107         if (srcidx == ttable->nsrcs) {
108                 route_to_channel_from_zero(plugin, src_channels, dst_channel, ttable, frames);
109                 return;
110         }
111
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) {
119                 goto *conv;
120 #define CONV_END after
121 #include "plugin_ops.h"
122 #undef CONV_END
123         after:
124                 src += src_step;
125                 dst += dst_step;
126         }
127 }
128
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)
133 {
134 #define GET_U_LABELS
135 #define PUT_U32_LABELS
136 #include "plugin_ops.h"
137 #undef GET_U_LABELS
138 #undef PUT_U32_LABELS
139         static void *zero_labels[3] = { &&zero_int32, &&zero_int64,
140 #if ROUTE_PLUGIN_USE_FLOAT
141                                  &&zero_float
142 #endif
143         };
144         /* sum_type att */
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
149 #endif
150         };
151         /* sum_type att shift */
152         static void *norm_labels[3 * 2 * 4] = { 0,
153                                          &&norm_int32_8_noatt,
154                                          &&norm_int32_16_noatt,
155                                          &&norm_int32_24_noatt,
156                                          0,
157                                          &&norm_int32_8_att,
158                                          &&norm_int32_16_att,
159                                          &&norm_int32_24_att,
160                                          &&norm_int64_0_noatt,
161                                          &&norm_int64_8_noatt,
162                                          &&norm_int64_16_noatt,
163                                          &&norm_int64_24_noatt,
164                                          &&norm_int64_0_att,
165                                          &&norm_int64_8_att,
166                                          &&norm_int64_16_att,
167                                          &&norm_int64_24_att,
168 #if ROUTE_PLUGIN_USE_FLOAT
169                                          &&norm_float_0,
170                                          &&norm_float_8,
171                                          &&norm_float_16,
172                                          &&norm_float_24,
173                                          &&norm_float_0,
174                                          &&norm_float_8,
175                                          &&norm_float_16,
176                                          &&norm_float_24,
177 #endif
178         };
179         route_t *data = (route_t *)plugin->extra_data;
180         void *zero, *get, *add, *norm, *put_u32;
181         int nsrcs = ttable->nsrcs;
182         char *dst;
183         int dst_step;
184         char *srcs[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)
192                         continue;
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];
196                 srcidx1++;
197         }
198         nsrcs = srcidx1;
199         if (nsrcs == 0) {
200                 route_to_channel_from_zero(plugin, src_channels, dst_channel, ttable, frames);
201                 return;
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);
204                 return;
205         }
206
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;
215
216         while (frames-- > 0) {
217                 ttable_src_t *ttp = src_tt;
218                 sum_t sum;
219
220                 /* Zero sum */
221                 goto *zero;
222         zero_int32:
223                 sum.as_uint32 = 0;
224                 goto zero_end;
225         zero_int64: 
226                 sum.as_uint64 = 0;
227                 goto zero_end;
228 #if ROUTE_PLUGIN_USE_FLOAT
229         zero_float:
230                 sum.as_float = 0.0;
231                 goto zero_end;
232 #endif
233         zero_end:
234                 for (srcidx = 0; srcidx < nsrcs; ++srcidx) {
235                         char *src = srcs[srcidx];
236                         
237                         /* Get sample */
238                         goto *get;
239 #define GET_U_END after_get
240 #include "plugin_ops.h"
241 #undef GET_U_END
242                 after_get:
243
244                         /* Sum */
245                         goto *add;
246                 add_int32_att:
247                         sum.as_uint32 += sample * ttp->as_int;
248                         goto after_sum;
249                 add_int32_noatt:
250                         if (ttp->as_int)
251                                 sum.as_uint32 += sample;
252                         goto after_sum;
253                 add_int64_att:
254                         sum.as_uint64 += (u_int64_t) sample * ttp->as_int;
255                         goto after_sum;
256                 add_int64_noatt:
257                         if (ttp->as_int)
258                                 sum.as_uint64 += sample;
259                         goto after_sum;
260 #if ROUTE_PLUGIN_USE_FLOAT
261                 add_float_att:
262                         sum.as_float += sample * ttp->as_float;
263                         goto after_sum;
264                 add_float_noatt:
265                         if (ttp->as_int)
266                                 sum.as_float += sample;
267                         goto after_sum;
268 #endif
269                 after_sum:
270                         srcs[srcidx] += src_steps[srcidx];
271                         ttp++;
272                 }
273                 
274                 /* Normalization */
275                 goto *norm;
276         norm_int32_8_att:
277                 sum.as_uint64 = sum.as_uint32;
278         norm_int64_8_att:
279                 sum.as_uint64 <<= 8;
280         norm_int64_0_att:
281                 div(sum.as_uint64);
282                 goto norm_int;
283
284         norm_int32_16_att:
285                 sum.as_uint64 = sum.as_uint32;
286         norm_int64_16_att:
287                 sum.as_uint64 <<= 16;
288                 div(sum.as_uint64);
289                 goto norm_int;
290
291         norm_int32_24_att:
292                 sum.as_uint64 = sum.as_uint32;
293         norm_int64_24_att:
294                 sum.as_uint64 <<= 24;
295                 div(sum.as_uint64);
296                 goto norm_int;
297
298         norm_int32_8_noatt:
299                 sum.as_uint64 = sum.as_uint32;
300         norm_int64_8_noatt:
301                 sum.as_uint64 <<= 8;
302                 goto norm_int;
303
304         norm_int32_16_noatt:
305                 sum.as_uint64 = sum.as_uint32;
306         norm_int64_16_noatt:
307                 sum.as_uint64 <<= 16;
308                 goto norm_int;
309
310         norm_int32_24_noatt:
311                 sum.as_uint64 = sum.as_uint32;
312         norm_int64_24_noatt:
313                 sum.as_uint64 <<= 24;
314                 goto norm_int;
315
316         norm_int64_0_noatt:
317         norm_int:
318                 if (sum.as_uint64 > (u_int32_t)0xffffffff)
319                         sample = (u_int32_t)0xffffffff;
320                 else
321                         sample = sum.as_uint64;
322                 goto after_norm;
323
324 #if ROUTE_PLUGIN_USE_FLOAT
325         norm_float_8:
326                 sum.as_float *= 1 << 8;
327                 goto norm_float;
328         norm_float_16:
329                 sum.as_float *= 1 << 16;
330                 goto norm_float;
331         norm_float_24:
332                 sum.as_float *= 1 << 24;
333                 goto norm_float;
334         norm_float_0:
335         norm_float:
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;
339                 else
340                         sample = sum.as_float;
341                 goto after_norm;
342 #endif
343         after_norm:
344                 
345                 /* Put sample */
346                 goto *put_u32;
347 #define PUT_U32_END after_put_u32
348 #include "plugin_ops.h"
349 #undef PUT_U32_END
350         after_put_u32:
351                 
352                 dst += dst_step;
353         }
354 }
355
356 int route_src_channels_mask(snd_pcm_plugin_t *plugin,
357                           bitset_t *dst_vmask,
358                           bitset_t **src_vmask)
359 {
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;
364         int channel;
365         ttable_dst_t *dp = data->ttable;
366         bitset_zero(vmask, schannels);
367         for (channel = 0; channel < dchannels; channel++, dp++) {
368                 unsigned int src;
369                 ttable_src_t *sp;
370                 if (!bitset_get(dst_vmask, channel))
371                         continue;
372                 sp = dp->srcs;
373                 for (src = 0; src < dp->nsrcs; src++, sp++)
374                         bitset_set(vmask, sp->channel);
375         }
376         *src_vmask = vmask;
377         return 0;
378 }
379
380 int route_dst_channels_mask(snd_pcm_plugin_t *plugin,
381                           bitset_t *src_vmask,
382                           bitset_t **dst_vmask)
383 {
384         route_t *data = (route_t *)plugin->extra_data;
385         int dchannels = plugin->dst_format.channels;
386         bitset_t *vmask = plugin->dst_vmask;
387         int channel;
388         ttable_dst_t *dp = data->ttable;
389         bitset_zero(vmask, dchannels);
390         for (channel = 0; channel < dchannels; channel++, dp++) {
391                 unsigned int src;
392                 ttable_src_t *sp;
393                 sp = dp->srcs;
394                 for (src = 0; src < dp->nsrcs; src++, sp++) {
395                         if (bitset_get(src_vmask, sp->channel)) {
396                                 bitset_set(vmask, channel);
397                                 break;
398                         }
399                 }
400         }
401         *dst_vmask = vmask;
402         return 0;
403 }
404
405 static void route_free(snd_pcm_plugin_t *plugin)
406 {
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);
412         }
413 }
414
415 static int route_load_ttable(snd_pcm_plugin_t *plugin, 
416                              const route_ttable_entry_t* src_ttable)
417 {
418         route_t *data;
419         unsigned int src_channel, dst_channel;
420         const route_ttable_entry_t *sptr;
421         ttable_dst_t *dptr;
422         if (src_ttable == NULL)
423                 return 0;
424         data = (route_t *)plugin->extra_data;
425         dptr = data->ttable;
426         sptr = src_ttable;
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;
430                 int att = 0;
431                 int nsrcs = 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);
435                         if (*sptr != 0) {
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;
441 #else
442                                 srcs[nsrcs].as_int = *sptr;
443 #endif
444                                 if (*sptr != FULL)
445                                         att = 1;
446                                 t += *sptr;
447                                 nsrcs++;
448                         }
449                         sptr++;
450                 }
451                 dptr->att = att;
452                 dptr->nsrcs = nsrcs;
453                 if (nsrcs == 0)
454                         dptr->func = route_to_channel_from_zero;
455                 else if (nsrcs == 1 && !att)
456                         dptr->func = route_to_channel_from_one;
457                 else
458                         dptr->func = route_to_channel;
459                 if (nsrcs > 0) {
460                         int srcidx;
461                         dptr->srcs = snd_kcalloc(nsrcs * sizeof(*srcs), GFP_KERNEL);
462                         for(srcidx = 0; srcidx < nsrcs; srcidx++)
463                                 dptr->srcs[srcidx] = srcs[srcidx];
464                 } else
465                         dptr->srcs = 0;
466                 dptr++;
467         }
468         return 0;
469 }
470
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)
475 {
476         route_t *data;
477         int src_nchannels, dst_nchannels;
478         int dst_channel;
479         ttable_dst_t *ttp;
480         snd_pcm_plugin_channel_t *dvp;
481
482         snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO);
483         if (frames == 0)
484                 return 0;
485         data = (route_t *)plugin->extra_data;
486
487         src_nchannels = plugin->src_format.channels;
488         dst_nchannels = plugin->dst_format.channels;
489
490 #ifdef CONFIG_SND_DEBUG
491         {
492                 int src_channel;
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,
496                                    return -ENXIO);
497                 }
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,
501                                    return -ENXIO);
502                 }
503         }
504 #endif
505
506         ttp = data->ttable;
507         dvp = dst_channels;
508         for (dst_channel = 0; dst_channel < dst_nchannels; ++dst_channel) {
509                 ttp->func(plugin, src_channels, dvp, ttp, frames);
510                 dvp++;
511                 ttp++;
512         }
513         return frames;
514 }
515
516 int getput_index(int format)
517 {
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);
523                 width = 0;
524         }
525 #ifdef SNDRV_LITTLE_ENDIAN
526         endian = snd_pcm_format_big_endian(format);
527 #else
528         endian = snd_pcm_format_little_endian(format);
529 #endif
530         if (endian < 0)
531                 endian = 0;
532         return width * 4 + endian * 2 + sign;
533 }
534
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)
540 {
541         route_t *data;
542         snd_pcm_plugin_t *plugin;
543         int err;
544
545         snd_assert(r_plugin != NULL, return -ENXIO);
546         *r_plugin = NULL;
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,
550                    return -ENXIO);
551
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,
555                                    &plugin);
556         if (err < 0)
557                 return err;
558
559         data = (route_t *) plugin->extra_data;
560
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);
564
565 #if ROUTE_PLUGIN_USE_FLOAT
566         data->sum_type = R_FLOAT;
567 #else
568         if (snd_pcm_format_width(src_format->format) == 32)
569                 data->sum_type = R_UINT64;
570         else
571                 data->sum_type = R_UINT32;
572 #endif
573         data->src_sample_size = snd_pcm_format_width(src_format->format) / 8;
574
575         if ((err = route_load_ttable(plugin, ttable)) < 0) {
576                 snd_pcm_plugin_free(plugin);
577                 return err;
578         }
579         plugin->transfer = route_transfer;
580         plugin->src_channels_mask = route_src_channels_mask;
581         plugin->dst_channels_mask = route_dst_channels_mask;
582         *r_plugin = plugin;
583         return 0;
584 }