2013-04-09 01:24:32 +02:00
|
|
|
/*
|
2019-06-17 11:17:30 +02:00
|
|
|
* Copyright 2003-2019 The Music Player Daemon Project
|
2013-04-09 01:24:32 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-06-16 12:11:44 +02:00
|
|
|
#include "Export.hxx"
|
2017-01-17 11:02:11 +01:00
|
|
|
#include "AudioFormat.hxx"
|
2015-10-27 00:22:22 +01:00
|
|
|
#include "Order.hxx"
|
2019-06-17 11:10:33 +02:00
|
|
|
#include "Pack.hxx"
|
2013-10-16 21:04:52 +02:00
|
|
|
#include "util/ByteReverse.hxx"
|
2014-08-12 21:40:06 +02:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2013-04-09 01:24:32 +02:00
|
|
|
|
2016-02-26 18:44:23 +01:00
|
|
|
#ifdef ENABLE_DSD
|
2017-01-11 22:33:37 +01:00
|
|
|
#include "Dsd16.hxx"
|
2017-01-11 15:18:57 +01:00
|
|
|
#include "Dsd32.hxx"
|
2016-02-26 18:17:11 +01:00
|
|
|
#include "PcmDsd.hxx"
|
2019-06-16 12:11:44 +02:00
|
|
|
#include "Dop.hxx"
|
2016-02-26 18:44:23 +01:00
|
|
|
#endif
|
|
|
|
|
2018-07-06 17:10:11 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2013-04-09 01:24:32 +02:00
|
|
|
void
|
2013-08-03 21:00:50 +02:00
|
|
|
PcmExport::Open(SampleFormat sample_format, unsigned _channels,
|
2017-10-26 12:24:47 +02:00
|
|
|
Params params) noexcept
|
2013-04-09 01:24:32 +02:00
|
|
|
{
|
|
|
|
assert(audio_valid_sample_format(sample_format));
|
|
|
|
|
|
|
|
channels = _channels;
|
2016-02-26 18:54:13 +01:00
|
|
|
alsa_channel_order = params.alsa_channel_order
|
2015-10-27 00:22:22 +01:00
|
|
|
? sample_format
|
|
|
|
: SampleFormat::UNDEFINED;
|
2016-02-26 18:44:23 +01:00
|
|
|
|
|
|
|
#ifdef ENABLE_DSD
|
2017-01-11 22:33:37 +01:00
|
|
|
assert((params.dsd_u16 + params.dsd_u32 + params.dop) <= 1);
|
2016-02-26 18:54:13 +01:00
|
|
|
assert(!params.dop || audio_valid_channel_count(_channels));
|
2016-02-26 18:17:11 +01:00
|
|
|
|
2017-01-11 22:33:37 +01:00
|
|
|
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;
|
|
|
|
|
2016-02-26 18:17:11 +01:00
|
|
|
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;
|
|
|
|
|
2016-02-26 18:54:13 +01:00
|
|
|
dop = params.dop && sample_format == SampleFormat::DSD;
|
2019-06-17 12:25:24 +02:00
|
|
|
if (dop) {
|
|
|
|
dop_converter.Open(_channels);
|
|
|
|
|
2014-08-31 16:12:26 +02:00
|
|
|
/* after the conversion to DoP, the DSD
|
2013-04-09 01:24:32 +02:00
|
|
|
samples are stuffed inside fake 24 bit samples */
|
2013-08-03 21:00:50 +02:00
|
|
|
sample_format = SampleFormat::S24_P32;
|
2019-06-17 12:25:24 +02:00
|
|
|
}
|
2016-02-26 18:44:23 +01:00
|
|
|
#endif
|
2013-04-09 01:24:32 +02:00
|
|
|
|
2016-02-26 18:54:13 +01:00
|
|
|
shift8 = params.shift8 && sample_format == SampleFormat::S24_P32;
|
|
|
|
pack24 = params.pack24 && sample_format == SampleFormat::S24_P32;
|
2013-04-09 01:24:32 +02:00
|
|
|
|
|
|
|
assert(!shift8 || !pack24);
|
|
|
|
|
|
|
|
reverse_endian = 0;
|
2016-02-26 18:54:13 +01:00
|
|
|
if (params.reverse_endian) {
|
2013-04-09 01:24:32 +02:00
|
|
|
size_t sample_size = pack24
|
|
|
|
? 3
|
|
|
|
: sample_format_size(sample_format);
|
|
|
|
assert(sample_size <= 0xff);
|
|
|
|
|
|
|
|
if (sample_size > 1)
|
|
|
|
reverse_endian = sample_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 12:25:24 +02:00
|
|
|
void
|
|
|
|
PcmExport::Reset() noexcept
|
|
|
|
{
|
|
|
|
#ifdef ENABLE_DSD
|
|
|
|
if (dop)
|
|
|
|
dop_converter.Reset();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-04-09 01:24:32 +02:00
|
|
|
size_t
|
2017-05-08 14:44:49 +02:00
|
|
|
PcmExport::GetFrameSize(const AudioFormat &audio_format) const noexcept
|
2013-04-09 01:24:32 +02:00
|
|
|
{
|
|
|
|
if (pack24)
|
|
|
|
/* packed 24 bit samples (3 bytes per sample) */
|
|
|
|
return audio_format.channels * 3;
|
|
|
|
|
2016-02-26 18:44:23 +01:00
|
|
|
#ifdef ENABLE_DSD
|
2017-01-11 22:33:37 +01:00
|
|
|
if (dsd_u16)
|
|
|
|
return channels * 2;
|
|
|
|
|
2016-02-26 18:17:11 +01:00
|
|
|
if (dsd_u32)
|
|
|
|
return channels * 4;
|
|
|
|
|
2014-08-31 16:12:26 +02:00
|
|
|
if (dop)
|
2013-04-09 01:24:32 +02:00
|
|
|
/* 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;
|
2016-02-26 18:44:23 +01:00
|
|
|
#endif
|
2013-04-09 01:24:32 +02:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
return audio_format.GetFrameSize();
|
2013-04-09 01:24:32 +02:00
|
|
|
}
|
|
|
|
|
2017-01-10 23:48:26 +01:00
|
|
|
unsigned
|
2017-05-08 14:44:49 +02:00
|
|
|
PcmExport::Params::CalcOutputSampleRate(unsigned sample_rate) const noexcept
|
2017-01-10 23:48:26 +01:00
|
|
|
{
|
2017-01-11 10:06:27 +01:00
|
|
|
#ifdef ENABLE_DSD
|
2017-01-11 22:33:37 +01:00
|
|
|
if (dsd_u16)
|
|
|
|
/* DSD_U16 combines two 8-bit "samples" in one 16-bit
|
|
|
|
"sample" */
|
|
|
|
sample_rate /= 2;
|
|
|
|
|
2017-01-11 10:06:27 +01:00
|
|
|
if (dsd_u32)
|
|
|
|
/* DSD_U32 combines four 8-bit "samples" in one 32-bit
|
|
|
|
"sample" */
|
|
|
|
sample_rate /= 4;
|
2017-01-11 00:11:43 +01:00
|
|
|
|
|
|
|
if (dop)
|
|
|
|
/* DoP packs two 8-bit "samples" in one 24-bit
|
|
|
|
"sample" */
|
|
|
|
sample_rate /= 2;
|
2017-01-11 10:06:27 +01:00
|
|
|
#endif
|
|
|
|
|
2017-01-10 23:48:26 +01:00
|
|
|
return sample_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
2017-05-08 14:44:49 +02:00
|
|
|
PcmExport::Params::CalcInputSampleRate(unsigned sample_rate) const noexcept
|
2017-01-10 23:48:26 +01:00
|
|
|
{
|
2017-01-11 10:06:27 +01:00
|
|
|
#ifdef ENABLE_DSD
|
2017-01-11 22:33:37 +01:00
|
|
|
if (dsd_u16)
|
|
|
|
sample_rate *= 2;
|
|
|
|
|
2017-01-11 10:06:27 +01:00
|
|
|
if (dsd_u32)
|
|
|
|
sample_rate *= 4;
|
2017-01-11 00:11:43 +01:00
|
|
|
|
|
|
|
if (dop)
|
|
|
|
sample_rate *= 2;
|
2017-01-11 10:06:27 +01:00
|
|
|
#endif
|
|
|
|
|
2017-01-10 23:48:26 +01:00
|
|
|
return sample_rate;
|
|
|
|
}
|
|
|
|
|
2014-08-12 21:40:06 +02:00
|
|
|
ConstBuffer<void>
|
2017-10-26 12:24:47 +02:00
|
|
|
PcmExport::Export(ConstBuffer<void> data) noexcept
|
2013-04-09 01:24:32 +02:00
|
|
|
{
|
2015-10-27 00:22:22 +01:00
|
|
|
if (alsa_channel_order != SampleFormat::UNDEFINED)
|
|
|
|
data = ToAlsaChannelOrder(order_buffer, data,
|
|
|
|
alsa_channel_order, channels);
|
|
|
|
|
2016-02-26 18:44:23 +01:00
|
|
|
#ifdef ENABLE_DSD
|
2017-01-11 22:33:37 +01:00
|
|
|
if (dsd_u16)
|
2019-06-17 12:28:48 +02:00
|
|
|
data = Dsd8To16(dsd_buffer, channels,
|
2017-01-11 22:33:37 +01:00
|
|
|
ConstBuffer<uint8_t>::FromVoid(data))
|
|
|
|
.ToVoid();
|
|
|
|
|
2016-02-26 18:17:11 +01:00
|
|
|
if (dsd_u32)
|
2019-06-17 12:28:48 +02:00
|
|
|
data = Dsd8To32(dsd_buffer, channels,
|
2016-02-26 18:17:11 +01:00
|
|
|
ConstBuffer<uint8_t>::FromVoid(data))
|
|
|
|
.ToVoid();
|
|
|
|
|
2014-08-31 16:12:26 +02:00
|
|
|
if (dop)
|
2019-06-17 12:25:24 +02:00
|
|
|
data = dop_converter.Convert(ConstBuffer<uint8_t>::FromVoid(data))
|
2014-08-12 21:38:36 +02:00
|
|
|
.ToVoid();
|
2016-02-26 18:44:23 +01:00
|
|
|
#endif
|
2013-04-09 01:24:32 +02:00
|
|
|
|
|
|
|
if (pack24) {
|
2014-08-12 21:40:06 +02:00
|
|
|
const auto src = ConstBuffer<int32_t>::FromVoid(data);
|
|
|
|
const size_t num_samples = src.size;
|
2013-04-09 01:24:32 +02:00
|
|
|
const size_t dest_size = num_samples * 3;
|
2013-07-29 08:10:10 +02:00
|
|
|
uint8_t *dest = (uint8_t *)pack_buffer.Get(dest_size);
|
2013-10-28 23:58:17 +01:00
|
|
|
assert(dest != nullptr);
|
2013-04-09 01:24:32 +02:00
|
|
|
|
2014-08-12 21:40:06 +02:00
|
|
|
pcm_pack_24(dest, src.begin(), src.end());
|
2013-04-09 01:24:32 +02:00
|
|
|
|
2014-08-12 21:40:06 +02:00
|
|
|
data.data = dest;
|
|
|
|
data.size = dest_size;
|
2013-04-09 01:24:32 +02:00
|
|
|
} else if (shift8) {
|
2014-08-12 21:40:06 +02:00
|
|
|
const auto src = ConstBuffer<int32_t>::FromVoid(data);
|
2013-04-09 01:24:32 +02:00
|
|
|
|
2014-08-12 21:40:06 +02:00
|
|
|
uint32_t *dest = (uint32_t *)pack_buffer.Get(data.size);
|
|
|
|
data.data = dest;
|
2013-04-09 01:24:32 +02:00
|
|
|
|
2014-08-12 21:40:06 +02:00
|
|
|
for (auto i : src)
|
|
|
|
*dest++ = i << 8;
|
2013-04-09 01:24:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (reverse_endian > 0) {
|
|
|
|
assert(reverse_endian >= 2);
|
|
|
|
|
2014-08-12 21:40:06 +02:00
|
|
|
const auto src = ConstBuffer<uint8_t>::FromVoid(data);
|
2013-04-09 01:24:32 +02:00
|
|
|
|
2014-08-12 21:40:06 +02:00
|
|
|
uint8_t *dest = (uint8_t *)reverse_buffer.Get(data.size);
|
|
|
|
assert(dest != nullptr);
|
|
|
|
data.data = dest;
|
2013-04-09 01:24:32 +02:00
|
|
|
|
2014-08-12 21:40:06 +02:00
|
|
|
reverse_bytes(dest, src.begin(), src.end(), reverse_endian);
|
2013-04-09 01:24:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2017-05-08 14:44:49 +02:00
|
|
|
PcmExport::CalcSourceSize(size_t size) const noexcept
|
2013-04-09 01:24:32 +02:00
|
|
|
{
|
|
|
|
if (pack24)
|
|
|
|
/* 32 bit to 24 bit conversion (4 to 3 bytes) */
|
|
|
|
size = (size / 3) * 4;
|
|
|
|
|
2016-02-26 18:44:23 +01:00
|
|
|
#ifdef ENABLE_DSD
|
2014-08-31 16:12:26 +02:00
|
|
|
if (dop)
|
|
|
|
/* DoP doubles the transport size */
|
2013-04-09 01:24:32 +02:00
|
|
|
size /= 2;
|
2016-02-26 18:44:23 +01:00
|
|
|
#endif
|
2013-04-09 01:24:32 +02:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|