xref: /OK3568_Linux_fs/kernel/sound/core/oss/copy.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *  Linear conversion 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 
copy_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)27*4882a593Smuzhiyun static snd_pcm_sframes_t copy_transfer(struct snd_pcm_plugin *plugin,
28*4882a593Smuzhiyun 			     const struct snd_pcm_plugin_channel *src_channels,
29*4882a593Smuzhiyun 			     struct snd_pcm_plugin_channel *dst_channels,
30*4882a593Smuzhiyun 			     snd_pcm_uframes_t frames)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun 	unsigned int channel;
33*4882a593Smuzhiyun 	unsigned int nchannels;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	if (snd_BUG_ON(!plugin || !src_channels || !dst_channels))
36*4882a593Smuzhiyun 		return -ENXIO;
37*4882a593Smuzhiyun 	if (frames == 0)
38*4882a593Smuzhiyun 		return 0;
39*4882a593Smuzhiyun 	nchannels = plugin->src_format.channels;
40*4882a593Smuzhiyun 	for (channel = 0; channel < nchannels; channel++) {
41*4882a593Smuzhiyun 		if (snd_BUG_ON(src_channels->area.first % 8 ||
42*4882a593Smuzhiyun 			       src_channels->area.step % 8))
43*4882a593Smuzhiyun 			return -ENXIO;
44*4882a593Smuzhiyun 		if (snd_BUG_ON(dst_channels->area.first % 8 ||
45*4882a593Smuzhiyun 			       dst_channels->area.step % 8))
46*4882a593Smuzhiyun 			return -ENXIO;
47*4882a593Smuzhiyun 		if (!src_channels->enabled) {
48*4882a593Smuzhiyun 			if (dst_channels->wanted)
49*4882a593Smuzhiyun 				snd_pcm_area_silence(&dst_channels->area, 0, frames, plugin->dst_format.format);
50*4882a593Smuzhiyun 			dst_channels->enabled = 0;
51*4882a593Smuzhiyun 			continue;
52*4882a593Smuzhiyun 		}
53*4882a593Smuzhiyun 		dst_channels->enabled = 1;
54*4882a593Smuzhiyun 		snd_pcm_area_copy(&src_channels->area, 0, &dst_channels->area, 0, frames, plugin->src_format.format);
55*4882a593Smuzhiyun 		src_channels++;
56*4882a593Smuzhiyun 		dst_channels++;
57*4882a593Smuzhiyun 	}
58*4882a593Smuzhiyun 	return frames;
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun 
snd_pcm_plugin_build_copy(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)61*4882a593Smuzhiyun int snd_pcm_plugin_build_copy(struct snd_pcm_substream *plug,
62*4882a593Smuzhiyun 			      struct snd_pcm_plugin_format *src_format,
63*4882a593Smuzhiyun 			      struct snd_pcm_plugin_format *dst_format,
64*4882a593Smuzhiyun 			      struct snd_pcm_plugin **r_plugin)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	int err;
67*4882a593Smuzhiyun 	struct snd_pcm_plugin *plugin;
68*4882a593Smuzhiyun 	int width;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	if (snd_BUG_ON(!r_plugin))
71*4882a593Smuzhiyun 		return -ENXIO;
72*4882a593Smuzhiyun 	*r_plugin = NULL;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	if (snd_BUG_ON(src_format->format != dst_format->format))
75*4882a593Smuzhiyun 		return -ENXIO;
76*4882a593Smuzhiyun 	if (snd_BUG_ON(src_format->rate != dst_format->rate))
77*4882a593Smuzhiyun 		return -ENXIO;
78*4882a593Smuzhiyun 	if (snd_BUG_ON(src_format->channels != dst_format->channels))
79*4882a593Smuzhiyun 		return -ENXIO;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	width = snd_pcm_format_physical_width(src_format->format);
82*4882a593Smuzhiyun 	if (snd_BUG_ON(width <= 0))
83*4882a593Smuzhiyun 		return -ENXIO;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	err = snd_pcm_plugin_build(plug, "copy", src_format, dst_format,
86*4882a593Smuzhiyun 				   0, &plugin);
87*4882a593Smuzhiyun 	if (err < 0)
88*4882a593Smuzhiyun 		return err;
89*4882a593Smuzhiyun 	plugin->transfer = copy_transfer;
90*4882a593Smuzhiyun 	*r_plugin = plugin;
91*4882a593Smuzhiyun 	return 0;
92*4882a593Smuzhiyun }
93