18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *  Route Plug-In
38c2ecf20Sopenharmony_ci *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *   This library is free software; you can redistribute it and/or modify
78c2ecf20Sopenharmony_ci *   it under the terms of the GNU Library General Public License as
88c2ecf20Sopenharmony_ci *   published by the Free Software Foundation; either version 2 of
98c2ecf20Sopenharmony_ci *   the License, or (at your option) any later version.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *   This program is distributed in the hope that it will be useful,
128c2ecf20Sopenharmony_ci *   but WITHOUT ANY WARRANTY; without even the implied warranty of
138c2ecf20Sopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
148c2ecf20Sopenharmony_ci *   GNU Library General Public License for more details.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *   You should have received a copy of the GNU Library General Public
178c2ecf20Sopenharmony_ci *   License along with this library; if not, write to the Free Software
188c2ecf20Sopenharmony_ci *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <linux/time.h>
238c2ecf20Sopenharmony_ci#include <sound/core.h>
248c2ecf20Sopenharmony_ci#include <sound/pcm.h>
258c2ecf20Sopenharmony_ci#include "pcm_plugin.h"
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic void zero_areas(struct snd_pcm_plugin_channel *dvp, int ndsts,
288c2ecf20Sopenharmony_ci		       snd_pcm_uframes_t frames, snd_pcm_format_t format)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	int dst = 0;
318c2ecf20Sopenharmony_ci	for (; dst < ndsts; ++dst) {
328c2ecf20Sopenharmony_ci		if (dvp->wanted)
338c2ecf20Sopenharmony_ci			snd_pcm_area_silence(&dvp->area, 0, frames, format);
348c2ecf20Sopenharmony_ci		dvp->enabled = 0;
358c2ecf20Sopenharmony_ci		dvp++;
368c2ecf20Sopenharmony_ci	}
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic inline void copy_area(const struct snd_pcm_plugin_channel *src_channel,
408c2ecf20Sopenharmony_ci			     struct snd_pcm_plugin_channel *dst_channel,
418c2ecf20Sopenharmony_ci			     snd_pcm_uframes_t frames, snd_pcm_format_t format)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	dst_channel->enabled = 1;
448c2ecf20Sopenharmony_ci	snd_pcm_area_copy(&src_channel->area, 0, &dst_channel->area, 0, frames, format);
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic snd_pcm_sframes_t route_transfer(struct snd_pcm_plugin *plugin,
488c2ecf20Sopenharmony_ci					const struct snd_pcm_plugin_channel *src_channels,
498c2ecf20Sopenharmony_ci					struct snd_pcm_plugin_channel *dst_channels,
508c2ecf20Sopenharmony_ci					snd_pcm_uframes_t frames)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	int nsrcs, ndsts, dst;
538c2ecf20Sopenharmony_ci	struct snd_pcm_plugin_channel *dvp;
548c2ecf20Sopenharmony_ci	snd_pcm_format_t format;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!plugin || !src_channels || !dst_channels))
578c2ecf20Sopenharmony_ci		return -ENXIO;
588c2ecf20Sopenharmony_ci	if (frames == 0)
598c2ecf20Sopenharmony_ci		return 0;
608c2ecf20Sopenharmony_ci	if (frames > dst_channels[0].frames)
618c2ecf20Sopenharmony_ci		frames = dst_channels[0].frames;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	nsrcs = plugin->src_format.channels;
648c2ecf20Sopenharmony_ci	ndsts = plugin->dst_format.channels;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	format = plugin->dst_format.format;
678c2ecf20Sopenharmony_ci	dvp = dst_channels;
688c2ecf20Sopenharmony_ci	if (nsrcs <= 1) {
698c2ecf20Sopenharmony_ci		/* expand to all channels */
708c2ecf20Sopenharmony_ci		for (dst = 0; dst < ndsts; ++dst) {
718c2ecf20Sopenharmony_ci			copy_area(src_channels, dvp, frames, format);
728c2ecf20Sopenharmony_ci			dvp++;
738c2ecf20Sopenharmony_ci		}
748c2ecf20Sopenharmony_ci		return frames;
758c2ecf20Sopenharmony_ci	}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	for (dst = 0; dst < ndsts && dst < nsrcs; ++dst) {
788c2ecf20Sopenharmony_ci		copy_area(src_channels, dvp, frames, format);
798c2ecf20Sopenharmony_ci		dvp++;
808c2ecf20Sopenharmony_ci		src_channels++;
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci	if (dst < ndsts)
838c2ecf20Sopenharmony_ci		zero_areas(dvp, ndsts - dst, frames, format);
848c2ecf20Sopenharmony_ci	return frames;
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ciint snd_pcm_plugin_build_route(struct snd_pcm_substream *plug,
888c2ecf20Sopenharmony_ci			       struct snd_pcm_plugin_format *src_format,
898c2ecf20Sopenharmony_ci			       struct snd_pcm_plugin_format *dst_format,
908c2ecf20Sopenharmony_ci			       struct snd_pcm_plugin **r_plugin)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	struct snd_pcm_plugin *plugin;
938c2ecf20Sopenharmony_ci	int err;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!r_plugin))
968c2ecf20Sopenharmony_ci		return -ENXIO;
978c2ecf20Sopenharmony_ci	*r_plugin = NULL;
988c2ecf20Sopenharmony_ci	if (snd_BUG_ON(src_format->rate != dst_format->rate))
998c2ecf20Sopenharmony_ci		return -ENXIO;
1008c2ecf20Sopenharmony_ci	if (snd_BUG_ON(src_format->format != dst_format->format))
1018c2ecf20Sopenharmony_ci		return -ENXIO;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	err = snd_pcm_plugin_build(plug, "route conversion",
1048c2ecf20Sopenharmony_ci				   src_format, dst_format, 0, &plugin);
1058c2ecf20Sopenharmony_ci	if (err < 0)
1068c2ecf20Sopenharmony_ci		return err;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	plugin->transfer = route_transfer;
1098c2ecf20Sopenharmony_ci	*r_plugin = plugin;
1108c2ecf20Sopenharmony_ci	return 0;
1118c2ecf20Sopenharmony_ci}
112