Interleave.cxx 2.83 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * http://www.musicpd.org
 *
 * 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.
 *
 * 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.
 */

#include "Interleave.hxx"

#include <string.h>

static void
25 26
GenericPcmInterleave(uint8_t *gcc_restrict dest,
		     ConstBuffer<const uint8_t *> src,
Max Kellermann's avatar
Max Kellermann committed
27
		     size_t n_frames, size_t sample_size) noexcept
28 29 30 31 32 33 34 35 36 37
{
	for (size_t frame = 0; frame < n_frames; ++frame) {
		for (size_t channel = 0; channel < src.size; ++channel) {
			memcpy(dest, src[channel] + frame * sample_size,
			       sample_size);
			dest += sample_size;
		}
	}
}

38 39 40 41 42
template<typename T>
static void
PcmInterleaveStereo(T *gcc_restrict dest,
		    const T *gcc_restrict src1,
		    const T *gcc_restrict src2,
Max Kellermann's avatar
Max Kellermann committed
43
		    size_t n_frames) noexcept
44 45 46 47 48 49 50
{
	for (size_t i = 0; i != n_frames; ++i) {
		*dest++ = *src1++;
		*dest++ = *src2++;
	}
}

51 52 53 54
template<typename T>
static void
PcmInterleaveT(T *gcc_restrict dest,
	       const ConstBuffer<const T *> src,
Max Kellermann's avatar
Max Kellermann committed
55
	       size_t n_frames) noexcept
56
{
57 58 59 60 61 62
	switch (src.size) {
	case 2:
		PcmInterleaveStereo(dest, src[0], src[1], n_frames);
		return;
	}

63 64 65 66 67 68 69 70 71
	for (const auto *s : src) {
		auto *d = dest++;

		for (const auto *const s_end = s + n_frames;
		     s != s_end; ++s, d += src.size)
			*d = *s;
	}
}

72 73 74
static void
PcmInterleave16(int16_t *gcc_restrict dest,
		const ConstBuffer<const int16_t *> src,
Max Kellermann's avatar
Max Kellermann committed
75
		size_t n_frames) noexcept
76 77 78 79
{
	PcmInterleaveT(dest, src, n_frames);
}

80 81 82
void
PcmInterleave32(int32_t *gcc_restrict dest,
		const ConstBuffer<const int32_t *> src,
Max Kellermann's avatar
Max Kellermann committed
83
		size_t n_frames) noexcept
84 85 86 87
{
	PcmInterleaveT(dest, src, n_frames);
}

88
void
89 90
PcmInterleave(void *gcc_restrict dest,
	      ConstBuffer<const void *> src,
Max Kellermann's avatar
Max Kellermann committed
91
	      size_t n_frames, size_t sample_size) noexcept
92
{
93
	switch (sample_size) {
94 95 96 97 98 99 100
	case 2:
		PcmInterleave16((int16_t *)dest,
				ConstBuffer<const int16_t *>((const int16_t *const*)src.data,
							     src.size),
				n_frames);
		break;

101 102 103 104 105 106 107 108 109 110 111 112 113
	case 4:
		PcmInterleave32((int32_t *)dest,
				ConstBuffer<const int32_t *>((const int32_t *const*)src.data,
							     src.size),
				n_frames);
		break;

	default:
		GenericPcmInterleave((uint8_t *)dest,
				     ConstBuffer<const uint8_t *>((const uint8_t *const*)src.data,
								  src.size),
				     n_frames, sample_size);
	}
114
}