PcmExport.cxx 5.33 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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 "config.h"
#include "PcmExport.hxx"
22
#include "AudioFormat.hxx"
23
#include "Order.hxx"
24
#include "PcmPack.hxx"
25
#include "util/ByteReverse.hxx"
26
#include "util/ConstBuffer.hxx"
27

28
#ifdef ENABLE_DSD
29
#include "Dsd16.hxx"
30
#include "Dsd32.hxx"
31
#include "PcmDsd.hxx"
32 33 34
#include "PcmDop.hxx"
#endif

35
void
36
PcmExport::Open(SampleFormat sample_format, unsigned _channels,
37
		Params params) noexcept
38 39 40 41
{
	assert(audio_valid_sample_format(sample_format));

	channels = _channels;
42
	alsa_channel_order = params.alsa_channel_order
43 44
		? sample_format
		: SampleFormat::UNDEFINED;
45 46

#ifdef ENABLE_DSD
47
	assert((params.dsd_u16 + params.dsd_u32 + params.dop) <= 1);
48
	assert(!params.dop || audio_valid_channel_count(_channels));
49

50 51 52 53 54 55
	dsd_u16 = params.dsd_u16 && sample_format == SampleFormat::DSD;
	if (dsd_u16)
		/* after the conversion to DSD_U16, the DSD samples
		   are stuffed inside fake 16 bit samples */
		sample_format = SampleFormat::S16;

56 57 58 59 60 61
	dsd_u32 = params.dsd_u32 && sample_format == SampleFormat::DSD;
	if (dsd_u32)
		/* after the conversion to DSD_U32, the DSD samples
		   are stuffed inside fake 32 bit samples */
		sample_format = SampleFormat::S32;

62
	dop = params.dop && sample_format == SampleFormat::DSD;
63 64
	if (dop)
		/* after the conversion to DoP, the DSD
65
		   samples are stuffed inside fake 24 bit samples */
66
		sample_format = SampleFormat::S24_P32;
67
#endif
68

69 70
	shift8 = params.shift8 && sample_format == SampleFormat::S24_P32;
	pack24 = params.pack24 && sample_format == SampleFormat::S24_P32;
71 72 73 74

	assert(!shift8 || !pack24);

	reverse_endian = 0;
75
	if (params.reverse_endian) {
76 77 78 79 80 81 82 83 84 85 86
		size_t sample_size = pack24
			? 3
			: sample_format_size(sample_format);
		assert(sample_size <= 0xff);

		if (sample_size > 1)
			reverse_endian = sample_size;
	}
}

size_t
87
PcmExport::GetFrameSize(const AudioFormat &audio_format) const noexcept
88 89 90 91 92
{
	if (pack24)
		/* packed 24 bit samples (3 bytes per sample) */
		return audio_format.channels * 3;

93
#ifdef ENABLE_DSD
94 95 96
	if (dsd_u16)
		return channels * 2;

97 98 99
	if (dsd_u32)
		return channels * 4;

100
	if (dop)
101 102 103 104 105
		/* the DSD-over-USB draft says that DSD 1-bit samples
		   are enclosed within 24 bit samples, and MPD's
		   representation of 24 bit is padded to 32 bit (4
		   bytes per sample) */
		return channels * 4;
106
#endif
107

108
	return audio_format.GetFrameSize();
109 110
}

111
unsigned
112
PcmExport::Params::CalcOutputSampleRate(unsigned sample_rate) const noexcept
113
{
114
#ifdef ENABLE_DSD
115 116 117 118 119
	if (dsd_u16)
		/* DSD_U16 combines two 8-bit "samples" in one 16-bit
		   "sample" */
		sample_rate /= 2;

120 121 122 123
	if (dsd_u32)
		/* DSD_U32 combines four 8-bit "samples" in one 32-bit
		   "sample" */
		sample_rate /= 4;
124 125 126 127 128

	if (dop)
		/* DoP packs two 8-bit "samples" in one 24-bit
		   "sample" */
		sample_rate /= 2;
129 130
#endif

131 132 133 134
	return sample_rate;
}

unsigned
135
PcmExport::Params::CalcInputSampleRate(unsigned sample_rate) const noexcept
136
{
137
#ifdef ENABLE_DSD
138 139 140
	if (dsd_u16)
		sample_rate *= 2;

141 142
	if (dsd_u32)
		sample_rate *= 4;
143 144 145

	if (dop)
		sample_rate *= 2;
146 147
#endif

148 149 150
	return sample_rate;
}

151
ConstBuffer<void>
152
PcmExport::Export(ConstBuffer<void> data) noexcept
153
{
154 155 156 157
	if (alsa_channel_order != SampleFormat::UNDEFINED)
		data = ToAlsaChannelOrder(order_buffer, data,
					  alsa_channel_order, channels);

158
#ifdef ENABLE_DSD
159 160 161 162 163
	if (dsd_u16)
		data = Dsd8To16(dop_buffer, channels,
				ConstBuffer<uint8_t>::FromVoid(data))
			.ToVoid();

164 165 166 167 168
	if (dsd_u32)
		data = Dsd8To32(dop_buffer, channels,
				ConstBuffer<uint8_t>::FromVoid(data))
			.ToVoid();

169 170
	if (dop)
		data = pcm_dsd_to_dop(dop_buffer, channels,
171 172
				      ConstBuffer<uint8_t>::FromVoid(data))
			.ToVoid();
173
#endif
174 175

	if (pack24) {
176 177
		const auto src = ConstBuffer<int32_t>::FromVoid(data);
		const size_t num_samples = src.size;
178
		const size_t dest_size = num_samples * 3;
179
		uint8_t *dest = (uint8_t *)pack_buffer.Get(dest_size);
180
		assert(dest != nullptr);
181

182
		pcm_pack_24(dest, src.begin(), src.end());
183

184 185
		data.data = dest;
		data.size = dest_size;
186
	} else if (shift8) {
187
		const auto src = ConstBuffer<int32_t>::FromVoid(data);
188

189 190
		uint32_t *dest = (uint32_t *)pack_buffer.Get(data.size);
		data.data = dest;
191

192 193
		for (auto i : src)
			*dest++ = i << 8;
194 195 196 197 198
	}

	if (reverse_endian > 0) {
		assert(reverse_endian >= 2);

199
		const auto src = ConstBuffer<uint8_t>::FromVoid(data);
200

201 202 203
		uint8_t *dest = (uint8_t *)reverse_buffer.Get(data.size);
		assert(dest != nullptr);
		data.data = dest;
204

205
		reverse_bytes(dest, src.begin(), src.end(), reverse_endian);
206 207 208 209 210 211
	}

	return data;
}

size_t
212
PcmExport::CalcSourceSize(size_t size) const noexcept
213 214 215 216 217
{
	if (pack24)
		/* 32 bit to 24 bit conversion (4 to 3 bytes) */
		size = (size / 3) * 4;

218
#ifdef ENABLE_DSD
219 220
	if (dop)
		/* DoP doubles the transport size */
221
		size /= 2;
222
#endif
223 224 225

	return size;
}