2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2009 Wim Taymans <wim.taymans@collabora.co.uk.com>
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.
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.
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
29 #include <pulse/sample.h>
30 #include <pulse/volume.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/macro.h>
36 static void remap_mono_to_stereo_s16ne_c(pa_remap_t
*m
, int16_t *dst
, const int16_t *src
, unsigned n
) {
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];
47 for (i
= n
& 3; i
; i
--) {
48 dst
[0] = dst
[1] = src
[0];
54 static void remap_mono_to_stereo_float32ne_c(pa_remap_t
*m
, float *dst
, const float *src
, unsigned n
) {
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];
65 for (i
= n
& 3; i
; i
--) {
66 dst
[0] = dst
[1] = src
[0];
72 static void remap_channels_matrix_s16ne_c(pa_remap_t
*m
, int16_t *dst
, const int16_t *src
, unsigned n
) {
76 n_ic
= m
->i_ss
.channels
;
77 n_oc
= m
->o_ss
.channels
;
79 memset(dst
, 0, n
* sizeof(int16_t) * n_oc
);
81 for (oc
= 0; oc
< n_oc
; oc
++) {
83 for (ic
= 0; ic
< n_ic
; ic
++) {
84 int16_t *d
= dst
+ oc
;
85 const int16_t *s
= src
+ ic
;
86 int32_t vol
= m
->map_table_i
[oc
][ic
];
92 for (i
= n
; i
> 0; i
--, s
+= n_ic
, d
+= n_oc
)
95 for (i
= n
; i
> 0; i
--, s
+= n_ic
, d
+= n_oc
)
96 *d
+= (int16_t) (((int32_t)*s
* vol
) >> 16);
102 static void remap_channels_matrix_float32ne_c(pa_remap_t
*m
, float *dst
, const float *src
, unsigned n
) {
106 n_ic
= m
->i_ss
.channels
;
107 n_oc
= m
->o_ss
.channels
;
109 memset(dst
, 0, n
* sizeof(float) * n_oc
);
111 for (oc
= 0; oc
< n_oc
; oc
++) {
113 for (ic
= 0; ic
< n_ic
; ic
++) {
115 const float *s
= src
+ ic
;
116 float vol
= m
->map_table_f
[oc
][ic
];
122 for (i
= n
; i
> 0; i
--, s
+= n_ic
, d
+= n_oc
)
125 for (i
= n
; i
> 0; i
--, s
+= n_ic
, d
+= n_oc
)
132 bool pa_setup_remap_arrange(const pa_remap_t
*m
, int8_t arrange
[PA_CHANNELS_MAX
]) {
138 n_ic
= m
->i_ss
.channels
;
139 n_oc
= m
->o_ss
.channels
;
141 for (oc
= 0; oc
< n_oc
; oc
++) {
143 for (ic
= 0; ic
< n_ic
; ic
++) {
144 int32_t vol
= m
->map_table_i
[oc
][ic
];
146 /* input channel is not used */
150 /* if mixing this channel, we cannot just rearrange */
151 if (vol
!= 0x10000 || arrange
[oc
] >= 0)
161 void pa_set_remap_func(pa_remap_t
*m
, pa_do_remap_func_t func_s16
,
162 pa_do_remap_func_t func_float
) {
166 if (m
->format
== PA_SAMPLE_S16NE
)
167 m
->do_remap
= func_s16
;
168 else if (m
->format
== PA_SAMPLE_FLOAT32NE
)
169 m
->do_remap
= func_float
;
171 pa_assert_not_reached();
174 /* set the function that will execute the remapping based on the matrices */
175 static void init_remap_c(pa_remap_t
*m
) {
178 n_oc
= m
->o_ss
.channels
;
179 n_ic
= m
->i_ss
.channels
;
181 /* find some common channel remappings, fall back to full matrix operation. */
182 if (n_ic
== 1 && n_oc
== 2 &&
183 m
->map_table_i
[0][0] == 0x10000 && m
->map_table_i
[1][0] == 0x10000) {
185 pa_log_info("Using mono to stereo remapping");
186 pa_set_remap_func(m
, (pa_do_remap_func_t
) remap_mono_to_stereo_s16ne_c
,
187 (pa_do_remap_func_t
) remap_mono_to_stereo_float32ne_c
);
189 pa_log_info("Using generic matrix remapping");
191 pa_set_remap_func(m
, (pa_do_remap_func_t
) remap_channels_matrix_s16ne_c
,
192 (pa_do_remap_func_t
) remap_channels_matrix_float32ne_c
);
196 /* default C implementation */
197 static pa_init_remap_func_t init_remap_func
= init_remap_c
;
199 void pa_init_remap_func(pa_remap_t
*m
) {
200 pa_assert(init_remap_func
);
204 /* call the installed remap init function */
207 if (m
->do_remap
== NULL
) {
208 /* nothing was installed, fallback to C version */
213 pa_init_remap_func_t
pa_get_init_remap_func(void) {
214 return init_remap_func
;
217 void pa_set_init_remap_func(pa_init_remap_func_t func
) {
218 init_remap_func
= func
;