]> code.delx.au - pulseaudio/blob - src/pulsecore/remap.c
Whitespace cleanup: Remove all multiple newlines
[pulseaudio] / src / pulsecore / remap.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2009 Wim Taymans <wim.taymans@collabora.co.uk.com>
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28
29 #include <pulse/sample.h>
30 #include <pulse/volume.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/macro.h>
33
34 #include "remap.h"
35
36 static void remap_mono_to_stereo_c(pa_remap_t *m, void *dst, const void *src, unsigned n) {
37 unsigned i;
38
39 switch (*m->format) {
40 case PA_SAMPLE_FLOAT32NE:
41 {
42 float *d, *s;
43
44 d = (float *) dst;
45 s = (float *) src;
46
47 for (i = n >> 2; i; i--) {
48 d[0] = d[1] = s[0];
49 d[2] = d[3] = s[1];
50 d[4] = d[5] = s[2];
51 d[6] = d[7] = s[3];
52 s += 4;
53 d += 8;
54 }
55 for (i = n & 3; i; i--) {
56 d[0] = d[1] = s[0];
57 s++;
58 d += 2;
59 }
60 break;
61 }
62 case PA_SAMPLE_S16NE:
63 {
64 int16_t *d, *s;
65
66 d = (int16_t *) dst;
67 s = (int16_t *) src;
68
69 for (i = n >> 2; i; i--) {
70 d[0] = d[1] = s[0];
71 d[2] = d[3] = s[1];
72 d[4] = d[5] = s[2];
73 d[6] = d[7] = s[3];
74 s += 4;
75 d += 8;
76 }
77 for (i = n & 3; i; i--) {
78 d[0] = d[1] = s[0];
79 s++;
80 d += 2;
81 }
82 break;
83 }
84 default:
85 pa_assert_not_reached();
86 }
87 }
88
89 static void remap_channels_matrix_c(pa_remap_t *m, void *dst, const void *src, unsigned n) {
90 unsigned oc, ic, i;
91 unsigned n_ic, n_oc;
92
93 n_ic = m->i_ss->channels;
94 n_oc = m->o_ss->channels;
95
96 switch (*m->format) {
97 case PA_SAMPLE_FLOAT32NE:
98 {
99 float *d, *s;
100
101 memset(dst, 0, n * sizeof(float) * n_oc);
102
103 for (oc = 0; oc < n_oc; oc++) {
104
105 for (ic = 0; ic < n_ic; ic++) {
106 float vol;
107
108 vol = m->map_table_f[oc][ic];
109
110 if (vol <= 0.0)
111 continue;
112
113 d = (float *)dst + oc;
114 s = (float *)src + ic;
115
116 if (vol >= 1.0) {
117 for (i = n; i > 0; i--, s += n_ic, d += n_oc)
118 *d += *s;
119 } else {
120 for (i = n; i > 0; i--, s += n_ic, d += n_oc)
121 *d += *s * vol;
122 }
123 }
124 }
125
126 break;
127 }
128 case PA_SAMPLE_S16NE:
129 {
130 int16_t *d, *s;
131
132 memset(dst, 0, n * sizeof(int16_t) * n_oc);
133
134 for (oc = 0; oc < n_oc; oc++) {
135
136 for (ic = 0; ic < n_ic; ic++) {
137 int32_t vol;
138
139 vol = m->map_table_i[oc][ic];
140
141 if (vol <= 0)
142 continue;
143
144 d = (int16_t *)dst + oc;
145 s = (int16_t *)src + ic;
146
147 if (vol >= 0x10000) {
148 for (i = n; i > 0; i--, s += n_ic, d += n_oc)
149 *d += *s;
150 } else {
151 for (i = n; i > 0; i--, s += n_ic, d += n_oc)
152 *d += (int16_t) (((int32_t)*s * vol) >> 16);
153 }
154 }
155 }
156 break;
157 }
158 default:
159 pa_assert_not_reached();
160 }
161 }
162
163 /* set the function that will execute the remapping based on the matrices */
164 static void init_remap_c(pa_remap_t *m) {
165 unsigned n_oc, n_ic;
166
167 n_oc = m->o_ss->channels;
168 n_ic = m->i_ss->channels;
169
170 /* find some common channel remappings, fall back to full matrix operation. */
171 if (n_ic == 1 && n_oc == 2 &&
172 m->map_table_i[0][0] == PA_VOLUME_NORM && m->map_table_i[1][0] == PA_VOLUME_NORM) {
173 m->do_remap = (pa_do_remap_func_t) remap_mono_to_stereo_c;
174 pa_log_info("Using mono to stereo remapping");
175 } else {
176 m->do_remap = (pa_do_remap_func_t) remap_channels_matrix_c;
177 pa_log_info("Using generic matrix remapping");
178 }
179 }
180
181 /* default C implementation */
182 static pa_init_remap_func_t remap_func = init_remap_c;
183
184 void pa_init_remap(pa_remap_t *m) {
185 pa_assert(remap_func);
186
187 m->do_remap = NULL;
188
189 /* call the installed remap init function */
190 remap_func(m);
191
192 if (m->do_remap == NULL) {
193 /* nothing was installed, fallback to C version */
194 init_remap_c(m);
195 }
196 }
197
198 pa_init_remap_func_t pa_get_init_remap_func(void) {
199 return remap_func;
200 }
201
202 void pa_set_init_remap_func(pa_init_remap_func_t func) {
203 remap_func = func;
204 }