2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "pcm_channels.h"
22 #include "pcm_buffer.h"
27 pcm_convert_channels_16_1_to_2(int16_t *dest
, const int16_t *src
,
30 while (num_frames
-- > 0) {
31 int16_t value
= *src
++;
39 pcm_convert_channels_16_2_to_1(int16_t *dest
, const int16_t *src
,
42 while (num_frames
-- > 0) {
43 int32_t a
= *src
++, b
= *src
++;
45 *dest
++ = (a
+ b
) / 2;
50 pcm_convert_channels_16_n_to_2(int16_t *dest
,
51 unsigned src_channels
, const int16_t *src
,
56 assert(src_channels
> 0);
58 while (num_frames
-- > 0) {
62 for (c
= 0; c
< src_channels
; ++c
)
64 value
= sum
/ (int)src_channels
;
66 /* XXX this is actually only mono ... */
73 pcm_convert_channels_16(struct pcm_buffer
*buffer
,
74 uint8_t dest_channels
,
75 uint8_t src_channels
, const int16_t *src
,
76 size_t src_size
, size_t *dest_size_r
)
78 unsigned num_frames
= src_size
/ src_channels
/ sizeof(*src
);
79 unsigned dest_size
= num_frames
* dest_channels
* sizeof(*src
);
80 int16_t *dest
= pcm_buffer_get(buffer
, dest_size
);
82 *dest_size_r
= dest_size
;
84 if (src_channels
== 1 && dest_channels
== 2)
85 pcm_convert_channels_16_1_to_2(dest
, src
, num_frames
);
86 else if (src_channels
== 2 && dest_channels
== 1)
87 pcm_convert_channels_16_2_to_1(dest
, src
, num_frames
);
88 else if (dest_channels
== 2)
89 pcm_convert_channels_16_n_to_2(dest
, src_channels
, src
,
98 pcm_convert_channels_24_1_to_2(int32_t *dest
, const int32_t *src
,
101 while (num_frames
-- > 0) {
102 int32_t value
= *src
++;
110 pcm_convert_channels_24_2_to_1(int32_t *dest
, const int32_t *src
,
113 while (num_frames
-- > 0) {
114 int32_t a
= *src
++, b
= *src
++;
116 *dest
++ = (a
+ b
) / 2;
121 pcm_convert_channels_24_n_to_2(int32_t *dest
,
122 unsigned src_channels
, const int32_t *src
,
127 assert(src_channels
> 0);
129 while (num_frames
-- > 0) {
133 for (c
= 0; c
< src_channels
; ++c
)
135 value
= sum
/ (int)src_channels
;
137 /* XXX this is actually only mono ... */
144 pcm_convert_channels_24(struct pcm_buffer
*buffer
,
145 uint8_t dest_channels
,
146 uint8_t src_channels
, const int32_t *src
,
147 size_t src_size
, size_t *dest_size_r
)
149 unsigned num_frames
= src_size
/ src_channels
/ sizeof(*src
);
150 unsigned dest_size
= num_frames
* dest_channels
* sizeof(*src
);
151 int32_t *dest
= pcm_buffer_get(buffer
, dest_size
);
153 *dest_size_r
= dest_size
;
155 if (src_channels
== 1 && dest_channels
== 2)
156 pcm_convert_channels_24_1_to_2(dest
, src
, num_frames
);
157 else if (src_channels
== 2 && dest_channels
== 1)
158 pcm_convert_channels_24_2_to_1(dest
, src
, num_frames
);
159 else if (dest_channels
== 2)
160 pcm_convert_channels_24_n_to_2(dest
, src_channels
, src
,
169 pcm_convert_channels_32_1_to_2(int32_t *dest
, const int32_t *src
,
172 pcm_convert_channels_24_1_to_2(dest
, src
, num_frames
);
176 pcm_convert_channels_32_2_to_1(int32_t *dest
, const int32_t *src
,
179 while (num_frames
-- > 0) {
180 int64_t a
= *src
++, b
= *src
++;
182 *dest
++ = (a
+ b
) / 2;
187 pcm_convert_channels_32_n_to_2(int32_t *dest
,
188 unsigned src_channels
, const int32_t *src
,
193 assert(src_channels
> 0);
195 while (num_frames
-- > 0) {
199 for (c
= 0; c
< src_channels
; ++c
)
201 value
= sum
/ (int64_t)src_channels
;
203 /* XXX this is actually only mono ... */
210 pcm_convert_channels_32(struct pcm_buffer
*buffer
,
211 uint8_t dest_channels
,
212 uint8_t src_channels
, const int32_t *src
,
213 size_t src_size
, size_t *dest_size_r
)
215 unsigned num_frames
= src_size
/ src_channels
/ sizeof(*src
);
216 unsigned dest_size
= num_frames
* dest_channels
* sizeof(*src
);
217 int32_t *dest
= pcm_buffer_get(buffer
, dest_size
);
219 *dest_size_r
= dest_size
;
221 if (src_channels
== 1 && dest_channels
== 2)
222 pcm_convert_channels_32_1_to_2(dest
, src
, num_frames
);
223 else if (src_channels
== 2 && dest_channels
== 1)
224 pcm_convert_channels_32_2_to_1(dest
, src
, num_frames
);
225 else if (dest_channels
== 2)
226 pcm_convert_channels_32_n_to_2(dest
, src_channels
, src
,