PcmChannels.cxx 6.89 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "PcmChannels.hxx"
22
#include "PcmBuffer.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "PcmUtils.hxx"
Max Kellermann's avatar
Max Kellermann committed
24

25 26
#include <assert.h>

27
template<typename D, typename S>
28
static void
29
MonoToStereo(D dest, S src, S end)
30
{
31 32
	while (src != end) {
		const auto value = *src++;
33 34 35 36

		*dest++ = value;
		*dest++ = value;
	}
37

38 39 40
}

static void
Max Kellermann's avatar
Max Kellermann committed
41 42 43
pcm_convert_channels_16_2_to_1(int16_t *gcc_restrict dest,
			       const int16_t *gcc_restrict src,
			       const int16_t *gcc_restrict src_end)
44
{
45
	while (src < src_end) {
46 47 48 49 50 51 52
		int32_t a = *src++, b = *src++;

		*dest++ = (a + b) / 2;
	}
}

static void
Max Kellermann's avatar
Max Kellermann committed
53
pcm_convert_channels_16_n_to_2(int16_t *gcc_restrict dest,
54
			       unsigned src_channels,
Max Kellermann's avatar
Max Kellermann committed
55 56
			       const int16_t *gcc_restrict src,
			       const int16_t *gcc_restrict src_end)
57 58 59 60 61
{
	unsigned c;

	assert(src_channels > 0);

62
	while (src < src_end) {
63 64 65 66 67 68 69 70 71 72 73 74 75 76
		int32_t sum = 0;
		int16_t value;

		for (c = 0; c < src_channels; ++c)
			sum += *src++;
		value = sum / (int)src_channels;

		/* XXX this is actually only mono ... */
		*dest++ = value;
		*dest++ = value;
	}
}

const int16_t *
77
pcm_convert_channels_16(PcmBuffer &buffer,
78 79
			unsigned dest_channels,
			unsigned src_channels, const int16_t *src,
80 81
			size_t src_size, size_t *dest_size_r)
{
82 83
	assert(src_size % (sizeof(*src) * src_channels) == 0);

84
	size_t dest_size = src_size / src_channels * dest_channels;
85 86
	*dest_size_r = dest_size;

87
	int16_t *dest = (int16_t *)buffer.Get(dest_size);
88 89
	const int16_t *src_end = pcm_end_pointer(src, src_size);

90
	if (src_channels == 1 && dest_channels == 2)
91
		MonoToStereo(dest, src, src_end);
92
	else if (src_channels == 2 && dest_channels == 1)
93
		pcm_convert_channels_16_2_to_1(dest, src, src_end);
94
	else if (dest_channels == 2)
95
		pcm_convert_channels_16_n_to_2(dest, src_channels, src,
96
					       src_end);
97
	else
98
		return nullptr;
99

100
	return dest;
101
}
102 103

static void
Max Kellermann's avatar
Max Kellermann committed
104 105 106
pcm_convert_channels_24_2_to_1(int32_t *gcc_restrict dest,
			       const int32_t *gcc_restrict src,
			       const int32_t *gcc_restrict src_end)
107
{
108
	while (src < src_end) {
109 110 111 112 113 114 115
		int32_t a = *src++, b = *src++;

		*dest++ = (a + b) / 2;
	}
}

static void
Max Kellermann's avatar
Max Kellermann committed
116
pcm_convert_channels_24_n_to_2(int32_t *gcc_restrict dest,
117
			       unsigned src_channels,
Max Kellermann's avatar
Max Kellermann committed
118 119
			       const int32_t *gcc_restrict src,
			       const int32_t *gcc_restrict src_end)
120 121 122 123 124
{
	unsigned c;

	assert(src_channels > 0);

125
	while (src < src_end) {
126 127 128 129 130 131 132 133 134 135 136 137 138 139
		int32_t sum = 0;
		int32_t value;

		for (c = 0; c < src_channels; ++c)
			sum += *src++;
		value = sum / (int)src_channels;

		/* XXX this is actually only mono ... */
		*dest++ = value;
		*dest++ = value;
	}
}

const int32_t *
140
pcm_convert_channels_24(PcmBuffer &buffer,
141 142
			unsigned dest_channels,
			unsigned src_channels, const int32_t *src,
143 144
			size_t src_size, size_t *dest_size_r)
{
145 146
	assert(src_size % (sizeof(*src) * src_channels) == 0);

147
	size_t dest_size = src_size / src_channels * dest_channels;
148 149
	*dest_size_r = dest_size;

150
	int32_t *dest = (int32_t *)buffer.Get(dest_size);
Max Kellermann's avatar
Max Kellermann committed
151 152
	const int32_t *src_end = (const int32_t *)
		pcm_end_pointer(src, src_size);
153

154
	if (src_channels == 1 && dest_channels == 2)
155
		MonoToStereo(dest, src, src_end);
156
	else if (src_channels == 2 && dest_channels == 1)
157
		pcm_convert_channels_24_2_to_1(dest, src, src_end);
158
	else if (dest_channels == 2)
159
		pcm_convert_channels_24_n_to_2(dest, src_channels, src,
160
					       src_end);
161
	else
162
		return nullptr;
163

164
	return dest;
165
}
166 167

static void
Max Kellermann's avatar
Max Kellermann committed
168 169 170
pcm_convert_channels_32_2_to_1(int32_t *gcc_restrict dest,
			       const int32_t *gcc_restrict src,
			       const int32_t *gcc_restrict src_end)
171
{
172
	while (src < src_end) {
173 174 175 176 177 178 179 180 181
		int64_t a = *src++, b = *src++;

		*dest++ = (a + b) / 2;
	}
}

static void
pcm_convert_channels_32_n_to_2(int32_t *dest,
			       unsigned src_channels, const int32_t *src,
182
			       const int32_t *src_end)
183 184 185 186 187
{
	unsigned c;

	assert(src_channels > 0);

188
	while (src < src_end) {
189 190 191 192 193 194 195 196 197 198 199 200 201 202
		int64_t sum = 0;
		int32_t value;

		for (c = 0; c < src_channels; ++c)
			sum += *src++;
		value = sum / (int64_t)src_channels;

		/* XXX this is actually only mono ... */
		*dest++ = value;
		*dest++ = value;
	}
}

const int32_t *
203
pcm_convert_channels_32(PcmBuffer &buffer,
204 205
			unsigned dest_channels,
			unsigned src_channels, const int32_t *src,
206 207
			size_t src_size, size_t *dest_size_r)
{
208 209
	assert(src_size % (sizeof(*src) * src_channels) == 0);

210
	size_t dest_size = src_size / src_channels * dest_channels;
211 212
	*dest_size_r = dest_size;

213
	int32_t *dest = (int32_t *)buffer.Get(dest_size);
Max Kellermann's avatar
Max Kellermann committed
214 215
	const int32_t *src_end = (const int32_t *)
		pcm_end_pointer(src, src_size);
216

217
	if (src_channels == 1 && dest_channels == 2)
218
		MonoToStereo(dest, src, src_end);
219
	else if (src_channels == 2 && dest_channels == 1)
220
		pcm_convert_channels_32_2_to_1(dest, src, src_end);
221 222
	else if (dest_channels == 2)
		pcm_convert_channels_32_n_to_2(dest, src_channels, src,
223
					       src_end);
224
	else
225
		return nullptr;
226 227 228

	return dest;
}
229 230

static void
Max Kellermann's avatar
Max Kellermann committed
231 232 233
pcm_convert_channels_float_2_to_1(float *gcc_restrict dest,
				  const float *gcc_restrict src,
				  const float *gcc_restrict src_end)
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
{
	while (src < src_end) {
		double a = *src++, b = *src++;

		*dest++ = (a + b) / 2;
	}
}

static void
pcm_convert_channels_float_n_to_2(float *dest,
				  unsigned src_channels, const float *src,
				  const float *src_end)
{
	unsigned c;

	assert(src_channels > 0);

	while (src < src_end) {
		double sum = 0;
		float value;

		for (c = 0; c < src_channels; ++c)
			sum += *src++;
		value = sum / (double)src_channels;

		/* XXX this is actually only mono ... */
		*dest++ = value;
		*dest++ = value;
	}
}

const float *
266
pcm_convert_channels_float(PcmBuffer &buffer,
267 268 269 270 271 272 273 274 275
			   unsigned dest_channels,
			   unsigned src_channels, const float *src,
			   size_t src_size, size_t *dest_size_r)
{
	assert(src_size % (sizeof(*src) * src_channels) == 0);

	size_t dest_size = src_size / src_channels * dest_channels;
	*dest_size_r = dest_size;

276
	float *dest = (float *)buffer.Get(dest_size);
Max Kellermann's avatar
Max Kellermann committed
277
	const float *src_end = (const float *)pcm_end_pointer(src, src_size);
278 279

	if (src_channels == 1 && dest_channels == 2)
280
		MonoToStereo(dest, src, src_end);
281 282 283 284 285 286
	else if (src_channels == 2 && dest_channels == 1)
		pcm_convert_channels_float_2_to_1(dest, src, src_end);
	else if (dest_channels == 2)
		pcm_convert_channels_float_n_to_2(dest, src_channels, src,
						  src_end);
	else
287
		return nullptr;
288 289 290

	return dest;
}