PcmExport.cxx 4.2 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "Order.hxx"
23
#include "PcmPack.hxx"
24
#include "util/ByteReverse.hxx"
25
#include "util/ConstBuffer.hxx"
26

27
#ifdef ENABLE_DSD
28
#include "PcmDsd.hxx"
29 30 31
#include "PcmDop.hxx"
#endif

32
void
33
PcmExport::Open(SampleFormat sample_format, unsigned _channels,
34
		Params params)
35 36 37 38
{
	assert(audio_valid_sample_format(sample_format));

	channels = _channels;
39
	alsa_channel_order = params.alsa_channel_order
40 41
		? sample_format
		: SampleFormat::UNDEFINED;
42 43

#ifdef ENABLE_DSD
44
	assert(!params.dsd_u32 || !params.dop);
45
	assert(!params.dop || audio_valid_channel_count(_channels));
46 47 48 49 50 51 52

	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;

53
	dop = params.dop && sample_format == SampleFormat::DSD;
54 55
	if (dop)
		/* after the conversion to DoP, the DSD
56
		   samples are stuffed inside fake 24 bit samples */
57
		sample_format = SampleFormat::S24_P32;
58
#else
59
	(void)_channels;
60
#endif
61

62 63
	shift8 = params.shift8 && sample_format == SampleFormat::S24_P32;
	pack24 = params.pack24 && sample_format == SampleFormat::S24_P32;
64 65 66 67

	assert(!shift8 || !pack24);

	reverse_endian = 0;
68
	if (params.reverse_endian) {
69 70 71 72 73 74 75 76 77 78 79
		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
80
PcmExport::GetFrameSize(const AudioFormat &audio_format) const
81 82 83 84 85
{
	if (pack24)
		/* packed 24 bit samples (3 bytes per sample) */
		return audio_format.channels * 3;

86
#ifdef ENABLE_DSD
87 88 89
	if (dsd_u32)
		return channels * 4;

90
	if (dop)
91 92 93 94 95
		/* 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;
96
#endif
97

98
	return audio_format.GetFrameSize();
99 100
}

101 102
ConstBuffer<void>
PcmExport::Export(ConstBuffer<void> data)
103
{
104 105 106 107
	if (alsa_channel_order != SampleFormat::UNDEFINED)
		data = ToAlsaChannelOrder(order_buffer, data,
					  alsa_channel_order, channels);

108
#ifdef ENABLE_DSD
109 110 111 112 113
	if (dsd_u32)
		data = Dsd8To32(dop_buffer, channels,
				ConstBuffer<uint8_t>::FromVoid(data))
			.ToVoid();

114 115
	if (dop)
		data = pcm_dsd_to_dop(dop_buffer, channels,
116 117
				      ConstBuffer<uint8_t>::FromVoid(data))
			.ToVoid();
118
#endif
119 120

	if (pack24) {
121 122
		const auto src = ConstBuffer<int32_t>::FromVoid(data);
		const size_t num_samples = src.size;
123
		const size_t dest_size = num_samples * 3;
124
		uint8_t *dest = (uint8_t *)pack_buffer.Get(dest_size);
125
		assert(dest != nullptr);
126

127
		pcm_pack_24(dest, src.begin(), src.end());
128

129 130
		data.data = dest;
		data.size = dest_size;
131
	} else if (shift8) {
132
		const auto src = ConstBuffer<int32_t>::FromVoid(data);
133

134 135
		uint32_t *dest = (uint32_t *)pack_buffer.Get(data.size);
		data.data = dest;
136

137 138
		for (auto i : src)
			*dest++ = i << 8;
139 140 141 142 143
	}

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

144
		const auto src = ConstBuffer<uint8_t>::FromVoid(data);
145

146 147 148
		uint8_t *dest = (uint8_t *)reverse_buffer.Get(data.size);
		assert(dest != nullptr);
		data.data = dest;
149

150
		reverse_bytes(dest, src.begin(), src.end(), reverse_endian);
151 152 153 154 155 156 157 158 159 160 161 162
	}

	return data;
}

size_t
PcmExport::CalcSourceSize(size_t size) const
{
	if (pack24)
		/* 32 bit to 24 bit conversion (4 to 3 bytes) */
		size = (size / 3) * 4;

163
#ifdef ENABLE_DSD
164 165
	if (dop)
		/* DoP doubles the transport size */
166
		size /= 2;
167
#endif
168 169 170

	return size;
}