1 // SPDX-License-Identifier: LGPL-2.0+ 2 /* 3 * Route Plug-In 4 * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org> 5 */ 6 7 #include <linux/time.h> 8 #include <sound/core.h> 9 #include <sound/pcm.h> 10 #include "pcm_plugin.h" 11 12 static void zero_areas(struct snd_pcm_plugin_channel *dvp, int ndsts, 13 snd_pcm_uframes_t frames, snd_pcm_format_t format) 14 { 15 int dst = 0; 16 for (; dst < ndsts; ++dst) { 17 if (dvp->wanted) 18 snd_pcm_area_silence(&dvp->area, 0, frames, format); 19 dvp->enabled = 0; 20 dvp++; 21 } 22 } 23 24 static inline void copy_area(const struct snd_pcm_plugin_channel *src_channel, 25 struct snd_pcm_plugin_channel *dst_channel, 26 snd_pcm_uframes_t frames, snd_pcm_format_t format) 27 { 28 dst_channel->enabled = 1; 29 snd_pcm_area_copy(&src_channel->area, 0, &dst_channel->area, 0, frames, format); 30 } 31 32 static snd_pcm_sframes_t route_transfer(struct snd_pcm_plugin *plugin, 33 const struct snd_pcm_plugin_channel *src_channels, 34 struct snd_pcm_plugin_channel *dst_channels, 35 snd_pcm_uframes_t frames) 36 { 37 int nsrcs, ndsts, dst; 38 struct snd_pcm_plugin_channel *dvp; 39 snd_pcm_format_t format; 40 41 if (snd_BUG_ON(!plugin || !src_channels || !dst_channels)) 42 return -ENXIO; 43 if (frames == 0) 44 return 0; 45 if (frames > dst_channels[0].frames) 46 frames = dst_channels[0].frames; 47 48 nsrcs = plugin->src_format.channels; 49 ndsts = plugin->dst_format.channels; 50 51 format = plugin->dst_format.format; 52 dvp = dst_channels; 53 if (nsrcs <= 1) { 54 /* expand to all channels */ 55 for (dst = 0; dst < ndsts; ++dst) { 56 copy_area(src_channels, dvp, frames, format); 57 dvp++; 58 } 59 return frames; 60 } 61 62 for (dst = 0; dst < ndsts && dst < nsrcs; ++dst) { 63 copy_area(src_channels, dvp, frames, format); 64 dvp++; 65 src_channels++; 66 } 67 if (dst < ndsts) 68 zero_areas(dvp, ndsts - dst, frames, format); 69 return frames; 70 } 71 72 int snd_pcm_plugin_build_route(struct snd_pcm_substream *plug, 73 struct snd_pcm_plugin_format *src_format, 74 struct snd_pcm_plugin_format *dst_format, 75 struct snd_pcm_plugin **r_plugin) 76 { 77 struct snd_pcm_plugin *plugin; 78 int err; 79 80 if (snd_BUG_ON(!r_plugin)) 81 return -ENXIO; 82 *r_plugin = NULL; 83 if (snd_BUG_ON(src_format->rate != dst_format->rate)) 84 return -ENXIO; 85 if (snd_BUG_ON(src_format->format != dst_format->format)) 86 return -ENXIO; 87 88 err = snd_pcm_plugin_build(plug, "route conversion", 89 src_format, dst_format, 0, &plugin); 90 if (err < 0) 91 return err; 92 93 plugin->transfer = route_transfer; 94 *r_plugin = plugin; 95 return 0; 96 } 97