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