]> code.delx.au - pulseaudio/blob - src/pulsecore/remap.c
a550b569bf74bb844901e859ce10a9f6c19acc1a
[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_s16ne_c(pa_remap_t *m, int16_t *dst, const int16_t *src, unsigned n) {
37 unsigned i;
38
39 for (i = n >> 2; i; i--) {
40 dst[0] = dst[1] = src[0];
41 dst[2] = dst[3] = src[1];
42 dst[4] = dst[5] = src[2];
43 dst[6] = dst[7] = src[3];
44 src += 4;
45 dst += 8;
46 }
47 for (i = n & 3; i; i--) {
48 dst[0] = dst[1] = src[0];
49 src++;
50 dst += 2;
51 }
52 }
53
54 static void remap_mono_to_stereo_float32ne_c(pa_remap_t *m, float *dst, const float *src, unsigned n) {
55 unsigned i;
56
57 for (i = n >> 2; i; i--) {
58 dst[0] = dst[1] = src[0];
59 dst[2] = dst[3] = src[1];
60 dst[4] = dst[5] = src[2];
61 dst[6] = dst[7] = src[3];
62 src += 4;
63 dst += 8;
64 }
65 for (i = n & 3; i; i--) {
66 dst[0] = dst[1] = src[0];
67 src++;
68 dst += 2;
69 }
70 }
71
72 static void remap_channels_matrix_s16ne_c(pa_remap_t *m, void *dst, const void *src, unsigned n) {
73 unsigned oc, ic, i;
74 unsigned n_ic, n_oc;
75
76 n_ic = m->i_ss.channels;
77 n_oc = m->o_ss.channels;
78
79 memset(dst, 0, n * sizeof(int16_t) * n_oc);
80
81 for (oc = 0; oc < n_oc; oc++) {
82
83 for (ic = 0; ic < n_ic; ic++) {
84 int16_t *d, *s;
85 int32_t vol;
86
87 vol = m->map_table_i[oc][ic];
88
89 if (vol <= 0)
90 continue;
91
92 d = (int16_t *)dst + oc;
93 s = (int16_t *)src + ic;
94
95 if (vol >= 0x10000) {
96 for (i = n; i > 0; i--, s += n_ic, d += n_oc)
97 *d += *s;
98 } else {
99 for (i = n; i > 0; i--, s += n_ic, d += n_oc)
100 *d += (int16_t) (((int32_t)*s * vol) >> 16);
101 }
102 }
103 }
104 }
105
106 static void remap_channels_matrix_float32ne_c(pa_remap_t *m, void *dst, const void *src, unsigned n) {
107 unsigned oc, ic, i;
108 unsigned n_ic, n_oc;
109
110 n_ic = m->i_ss.channels;
111 n_oc = m->o_ss.channels;
112
113 memset(dst, 0, n * sizeof(float) * n_oc);
114
115 for (oc = 0; oc < n_oc; oc++) {
116
117 for (ic = 0; ic < n_ic; ic++) {
118 float *d, *s;
119 float vol;
120
121 vol = m->map_table_f[oc][ic];
122
123 if (vol <= 0.0f)
124 continue;
125
126 d = (float *)dst + oc;
127 s = (float *)src + ic;
128
129 if (vol >= 1.0f) {
130 for (i = n; i > 0; i--, s += n_ic, d += n_oc)
131 *d += *s;
132 } else {
133 for (i = n; i > 0; i--, s += n_ic, d += n_oc)
134 *d += *s * vol;
135 }
136 }
137 }
138 }
139
140 bool pa_setup_remap_arrange(const pa_remap_t *m, int8_t arrange[PA_CHANNELS_MAX]) {
141 unsigned ic, oc;
142 unsigned n_ic, n_oc;
143
144 pa_assert(m);
145
146 n_ic = m->i_ss.channels;
147 n_oc = m->o_ss.channels;
148
149 for (oc = 0; oc < n_oc; oc++) {
150 arrange[oc] = -1;
151 for (ic = 0; ic < n_ic; ic++) {
152 int32_t vol = m->map_table_i[oc][ic];
153
154 /* input channel is not used */
155 if (vol == 0)
156 continue;
157
158 /* if mixing this channel, we cannot just rearrange */
159 if (vol != 0x10000 || arrange[oc] >= 0)
160 return false;
161
162 arrange[oc] = ic;
163 }
164 }
165
166 return true;
167 }
168
169 /* set the function that will execute the remapping based on the matrices */
170 static void init_remap_c(pa_remap_t *m) {
171 unsigned n_oc, n_ic;
172
173 n_oc = m->o_ss.channels;
174 n_ic = m->i_ss.channels;
175
176 /* find some common channel remappings, fall back to full matrix operation. */
177 if (n_ic == 1 && n_oc == 2 &&
178 m->map_table_i[0][0] == 0x10000 && m->map_table_i[1][0] == 0x10000) {
179
180 pa_log_info("Using mono to stereo remapping");
181 switch (m->format) {
182 case PA_SAMPLE_S16NE:
183 m->do_remap = (pa_do_remap_func_t) remap_mono_to_stereo_s16ne_c;
184 break;
185 case PA_SAMPLE_FLOAT32NE:
186 m->do_remap = (pa_do_remap_func_t) remap_mono_to_stereo_float32ne_c;
187 break;
188 default:
189 pa_assert_not_reached();
190 }
191 } else {
192 pa_log_info("Using generic matrix remapping");
193 switch (m->format) {
194 case PA_SAMPLE_S16NE:
195 m->do_remap = (pa_do_remap_func_t) remap_channels_matrix_s16ne_c;
196 break;
197 case PA_SAMPLE_FLOAT32NE:
198 m->do_remap = (pa_do_remap_func_t) remap_channels_matrix_float32ne_c;
199 break;
200 default:
201 pa_assert_not_reached();
202 }
203 }
204 }
205
206 /* default C implementation */
207 static pa_init_remap_func_t init_remap_func = init_remap_c;
208
209 void pa_init_remap_func(pa_remap_t *m) {
210 pa_assert(init_remap_func);
211
212 m->do_remap = NULL;
213
214 /* call the installed remap init function */
215 init_remap_func(m);
216
217 if (m->do_remap == NULL) {
218 /* nothing was installed, fallback to C version */
219 init_remap_c(m);
220 }
221 }
222
223 pa_init_remap_func_t pa_get_init_remap_func(void) {
224 return init_remap_func;
225 }
226
227 void pa_set_init_remap_func(pa_init_remap_func_t func) {
228 init_remap_func = func;
229 }