PcmExport.cxx 5.33 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
 * 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 "PcmExport.hxx"
21
#include "AudioFormat.hxx"
22
#include "Order.hxx"
23
#include "PcmPack.hxx"
24
#include "util/ByteReverse.hxx"
25
#include "util/ConstBuffer.hxx"
26

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

34 35
#include <assert.h>

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

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

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

51 52 53 54 55 56
	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;

57 58 59 60 61 62
	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;

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

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

	assert(!shift8 || !pack24);

	reverse_endian = 0;
76
	if (params.reverse_endian) {
77 78 79 80 81 82 83 84 85 86 87
		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
88
PcmExport::GetFrameSize(const AudioFormat &audio_format) const noexcept
89 90 91 92 93
{
	if (pack24)
		/* packed 24 bit samples (3 bytes per sample) */
		return audio_format.channels * 3;

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

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

101
	if (dop)
102 103 104 105 106
		/* 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;
107
#endif
108

109
	return audio_format.GetFrameSize();
110 111
}

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

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

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

132 133 134 135
	return sample_rate;
}

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

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

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

149 150 151
	return sample_rate;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

	return data;
}

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

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

	return size;
}