xref: /OK3568_Linux_fs/kernel/sound/core/oss/route.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *  Route Plug-In
3*4882a593Smuzhiyun  *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *   This library is free software; you can redistribute it and/or modify
7*4882a593Smuzhiyun  *   it under the terms of the GNU Library General Public License as
8*4882a593Smuzhiyun  *   published by the Free Software Foundation; either version 2 of
9*4882a593Smuzhiyun  *   the License, or (at your option) any later version.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  *   This program is distributed in the hope that it will be useful,
12*4882a593Smuzhiyun  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13*4882a593Smuzhiyun  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*4882a593Smuzhiyun  *   GNU Library General Public License for more details.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  *   You should have received a copy of the GNU Library General Public
17*4882a593Smuzhiyun  *   License along with this library; if not, write to the Free Software
18*4882a593Smuzhiyun  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include <linux/time.h>
23*4882a593Smuzhiyun #include <sound/core.h>
24*4882a593Smuzhiyun #include <sound/pcm.h>
25*4882a593Smuzhiyun #include "pcm_plugin.h"
26*4882a593Smuzhiyun 
zero_areas(struct snd_pcm_plugin_channel * dvp,int ndsts,snd_pcm_uframes_t frames,snd_pcm_format_t format)27*4882a593Smuzhiyun static void zero_areas(struct snd_pcm_plugin_channel *dvp, int ndsts,
28*4882a593Smuzhiyun 		       snd_pcm_uframes_t frames, snd_pcm_format_t format)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	int dst = 0;
31*4882a593Smuzhiyun 	for (; dst < ndsts; ++dst) {
32*4882a593Smuzhiyun 		if (dvp->wanted)
33*4882a593Smuzhiyun 			snd_pcm_area_silence(&dvp->area, 0, frames, format);
34*4882a593Smuzhiyun 		dvp->enabled = 0;
35*4882a593Smuzhiyun 		dvp++;
36*4882a593Smuzhiyun 	}
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun 
copy_area(const struct snd_pcm_plugin_channel * src_channel,struct snd_pcm_plugin_channel * dst_channel,snd_pcm_uframes_t frames,snd_pcm_format_t format)39*4882a593Smuzhiyun static inline void copy_area(const struct snd_pcm_plugin_channel *src_channel,
40*4882a593Smuzhiyun 			     struct snd_pcm_plugin_channel *dst_channel,
41*4882a593Smuzhiyun 			     snd_pcm_uframes_t frames, snd_pcm_format_t format)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	dst_channel->enabled = 1;
44*4882a593Smuzhiyun 	snd_pcm_area_copy(&src_channel->area, 0, &dst_channel->area, 0, frames, format);
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun 
route_transfer(struct snd_pcm_plugin * plugin,const struct snd_pcm_plugin_channel * src_channels,struct snd_pcm_plugin_channel * dst_channels,snd_pcm_uframes_t frames)47*4882a593Smuzhiyun static snd_pcm_sframes_t route_transfer(struct snd_pcm_plugin *plugin,
48*4882a593Smuzhiyun 					const struct snd_pcm_plugin_channel *src_channels,
49*4882a593Smuzhiyun 					struct snd_pcm_plugin_channel *dst_channels,
50*4882a593Smuzhiyun 					snd_pcm_uframes_t frames)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	int nsrcs, ndsts, dst;
53*4882a593Smuzhiyun 	struct snd_pcm_plugin_channel *dvp;
54*4882a593Smuzhiyun 	snd_pcm_format_t format;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	if (snd_BUG_ON(!plugin || !src_channels || !dst_channels))
57*4882a593Smuzhiyun 		return -ENXIO;
58*4882a593Smuzhiyun 	if (frames == 0)
59*4882a593Smuzhiyun 		return 0;
60*4882a593Smuzhiyun 	if (frames > dst_channels[0].frames)
61*4882a593Smuzhiyun 		frames = dst_channels[0].frames;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	nsrcs = plugin->src_format.channels;
64*4882a593Smuzhiyun 	ndsts = plugin->dst_format.channels;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	format = plugin->dst_format.format;
67*4882a593Smuzhiyun 	dvp = dst_channels;
68*4882a593Smuzhiyun 	if (nsrcs <= 1) {
69*4882a593Smuzhiyun 		/* expand to all channels */
70*4882a593Smuzhiyun 		for (dst = 0; dst < ndsts; ++dst) {
71*4882a593Smuzhiyun 			copy_area(src_channels, dvp, frames, format);
72*4882a593Smuzhiyun 			dvp++;
73*4882a593Smuzhiyun 		}
74*4882a593Smuzhiyun 		return frames;
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	for (dst = 0; dst < ndsts && dst < nsrcs; ++dst) {
78*4882a593Smuzhiyun 		copy_area(src_channels, dvp, frames, format);
79*4882a593Smuzhiyun 		dvp++;
80*4882a593Smuzhiyun 		src_channels++;
81*4882a593Smuzhiyun 	}
82*4882a593Smuzhiyun 	if (dst < ndsts)
83*4882a593Smuzhiyun 		zero_areas(dvp, ndsts - dst, frames, format);
84*4882a593Smuzhiyun 	return frames;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun 
snd_pcm_plugin_build_route(struct snd_pcm_substream * plug,struct snd_pcm_plugin_format * src_format,struct snd_pcm_plugin_format * dst_format,struct snd_pcm_plugin ** r_plugin)87*4882a593Smuzhiyun int snd_pcm_plugin_build_route(struct snd_pcm_substream *plug,
88*4882a593Smuzhiyun 			       struct snd_pcm_plugin_format *src_format,
89*4882a593Smuzhiyun 			       struct snd_pcm_plugin_format *dst_format,
90*4882a593Smuzhiyun 			       struct snd_pcm_plugin **r_plugin)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	struct snd_pcm_plugin *plugin;
93*4882a593Smuzhiyun 	int err;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	if (snd_BUG_ON(!r_plugin))
96*4882a593Smuzhiyun 		return -ENXIO;
97*4882a593Smuzhiyun 	*r_plugin = NULL;
98*4882a593Smuzhiyun 	if (snd_BUG_ON(src_format->rate != dst_format->rate))
99*4882a593Smuzhiyun 		return -ENXIO;
100*4882a593Smuzhiyun 	if (snd_BUG_ON(src_format->format != dst_format->format))
101*4882a593Smuzhiyun 		return -ENXIO;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	err = snd_pcm_plugin_build(plug, "route conversion",
104*4882a593Smuzhiyun 				   src_format, dst_format, 0, &plugin);
105*4882a593Smuzhiyun 	if (err < 0)
106*4882a593Smuzhiyun 		return err;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	plugin->transfer = route_transfer;
109*4882a593Smuzhiyun 	*r_plugin = plugin;
110*4882a593Smuzhiyun 	return 0;
111*4882a593Smuzhiyun }
112