From 938affef32bc6a2ae7b6bdaa8b22330ebb06d1cd Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 11 Jan 2017 22:33:37 +0100 Subject: [PATCH] pcm/export: support DSD_U16 --- Makefile.am | 1 + src/pcm/Dsd16.cxx | 62 ++++++++++++++++++++++++++++++++++++++++ src/pcm/Dsd16.hxx | 36 +++++++++++++++++++++++ src/pcm/PcmExport.cxx | 25 +++++++++++++++- src/pcm/PcmExport.hxx | 6 ++++ test/test_pcm_all.hxx | 2 ++ test/test_pcm_export.cxx | 31 ++++++++++++++++++++ 7 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 src/pcm/Dsd16.cxx create mode 100644 src/pcm/Dsd16.hxx diff --git a/Makefile.am b/Makefile.am index e0202b0cc..b0ac1aa36 100644 --- a/Makefile.am +++ b/Makefile.am @@ -575,6 +575,7 @@ PCM_LIBS = \ if ENABLE_DSD libpcm_a_SOURCES += \ + src/pcm/Dsd16.cxx src/pcm/Dsd16.hxx \ src/pcm/Dsd32.cxx src/pcm/Dsd32.hxx \ src/pcm/PcmDsd.cxx src/pcm/PcmDsd.hxx \ src/pcm/dsd2pcm/dsd2pcm.c src/pcm/dsd2pcm/dsd2pcm.h diff --git a/src/pcm/Dsd16.cxx b/src/pcm/Dsd16.cxx new file mode 100644 index 000000000..774a51ecc --- /dev/null +++ b/src/pcm/Dsd16.cxx @@ -0,0 +1,62 @@ +/* + * Copyright 2003-2017 The Music Player Daemon Project + * 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 "Dsd16.hxx" +#include "PcmBuffer.hxx" +#include "util/ConstBuffer.hxx" + +/** + * Construct a 16 bit integer from two bytes. + */ +static constexpr inline uint16_t +Construct16(uint8_t a, uint8_t b) +{ + /* "a" is the oldest byte, which must be in the most + significant byte */ + + return uint16_t(b) | (uint16_t(a) << 8); +} + +static constexpr inline uint16_t +Dsd8To16Sample(const uint8_t *src, unsigned channels) +{ + return Construct16(src[0], src[channels]); +} + +ConstBuffer +Dsd8To16(PcmBuffer &buffer, unsigned channels, ConstBuffer _src) +{ + const size_t in_frames = _src.size / channels; + const size_t out_frames = in_frames / 2; + const size_t out_samples = out_frames * channels; + + const uint8_t *src = _src.data; + uint16_t *const dest0 = buffer.GetT(out_samples); + uint16_t *dest = dest0; + + for (size_t i = 0; i < out_frames; ++i) { + for (size_t c = 0; c < channels; ++c) + *dest++ = Dsd8To16Sample(src++, channels); + + src += channels; + } + + return {dest0, out_samples}; +} diff --git a/src/pcm/Dsd16.hxx b/src/pcm/Dsd16.hxx new file mode 100644 index 000000000..47341868b --- /dev/null +++ b/src/pcm/Dsd16.hxx @@ -0,0 +1,36 @@ +/* + * Copyright 2003-2017 The Music Player Daemon Project + * 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. + */ + +#ifndef MPD_PCM_DSD_16_HXX +#define MPD_PCM_DSD_16_HXX + +#include "check.h" + +#include + +template struct ConstBuffer; +class PcmBuffer; + +/** + * Convert DSD_U8 to DSD_U16 (native endian, oldest bits in MSB). + */ +ConstBuffer +Dsd8To16(PcmBuffer &buffer, unsigned channels, ConstBuffer src); + +#endif diff --git a/src/pcm/PcmExport.cxx b/src/pcm/PcmExport.cxx index 6b4f625cc..6738ebada 100644 --- a/src/pcm/PcmExport.cxx +++ b/src/pcm/PcmExport.cxx @@ -25,6 +25,7 @@ #include "util/ConstBuffer.hxx" #ifdef ENABLE_DSD +#include "Dsd16.hxx" #include "Dsd32.hxx" #include "PcmDsd.hxx" #include "PcmDop.hxx" @@ -42,9 +43,15 @@ PcmExport::Open(SampleFormat sample_format, unsigned _channels, : SampleFormat::UNDEFINED; #ifdef ENABLE_DSD - assert(!params.dsd_u32 || !params.dop); + assert((params.dsd_u16 + params.dsd_u32 + params.dop) <= 1); assert(!params.dop || audio_valid_channel_count(_channels)); + 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; + dsd_u32 = params.dsd_u32 && sample_format == SampleFormat::DSD; if (dsd_u32) /* after the conversion to DSD_U32, the DSD samples @@ -83,6 +90,9 @@ PcmExport::GetFrameSize(const AudioFormat &audio_format) const return audio_format.channels * 3; #ifdef ENABLE_DSD + if (dsd_u16) + return channels * 2; + if (dsd_u32) return channels * 4; @@ -101,6 +111,11 @@ unsigned PcmExport::Params::CalcOutputSampleRate(unsigned sample_rate) const { #ifdef ENABLE_DSD + if (dsd_u16) + /* DSD_U16 combines two 8-bit "samples" in one 16-bit + "sample" */ + sample_rate /= 2; + if (dsd_u32) /* DSD_U32 combines four 8-bit "samples" in one 32-bit "sample" */ @@ -119,6 +134,9 @@ unsigned PcmExport::Params::CalcInputSampleRate(unsigned sample_rate) const { #ifdef ENABLE_DSD + if (dsd_u16) + sample_rate *= 2; + if (dsd_u32) sample_rate *= 4; @@ -137,6 +155,11 @@ PcmExport::Export(ConstBuffer data) alsa_channel_order, channels); #ifdef ENABLE_DSD + if (dsd_u16) + data = Dsd8To16(dop_buffer, channels, + ConstBuffer::FromVoid(data)) + .ToVoid(); + if (dsd_u32) data = Dsd8To32(dop_buffer, channels, ConstBuffer::FromVoid(data)) diff --git a/src/pcm/PcmExport.hxx b/src/pcm/PcmExport.hxx index eb010bf0e..3e801a150 100644 --- a/src/pcm/PcmExport.hxx +++ b/src/pcm/PcmExport.hxx @@ -79,6 +79,11 @@ class PcmExport { SampleFormat alsa_channel_order; #ifdef ENABLE_DSD + /** + * Convert DSD (U8) to DSD_U16? + */ + bool dsd_u16; + /** * Convert DSD (U8) to DSD_U32? */ @@ -114,6 +119,7 @@ public: struct Params { bool alsa_channel_order = false; #ifdef ENABLE_DSD + bool dsd_u16 = false; bool dsd_u32 = false; bool dop = false; #endif diff --git a/test/test_pcm_all.hxx b/test/test_pcm_all.hxx index 8a5b83cb6..ace822ab8 100644 --- a/test/test_pcm_all.hxx +++ b/test/test_pcm_all.hxx @@ -128,6 +128,7 @@ class PcmExportTest : public CppUnit::TestFixture { CPPUNIT_TEST(TestPack24); CPPUNIT_TEST(TestReverseEndian); #ifdef ENABLE_DSD + CPPUNIT_TEST(TestDsdU16); CPPUNIT_TEST(TestDsdU32); CPPUNIT_TEST(TestDop); #endif @@ -139,6 +140,7 @@ public: void TestPack24(); void TestReverseEndian(); #ifdef ENABLE_DSD + void TestDsdU16(); void TestDsdU32(); void TestDop(); #endif diff --git a/test/test_pcm_export.cxx b/test/test_pcm_export.cxx index ba4ef347f..ebbeeaf57 100644 --- a/test/test_pcm_export.cxx +++ b/test/test_pcm_export.cxx @@ -126,6 +126,37 @@ PcmExportTest::TestReverseEndian() #ifdef ENABLE_DSD +void +PcmExportTest::TestDsdU16() +{ + static constexpr uint8_t src[] = { + 0x01, 0x23, 0x45, 0x67, + 0x89, 0xab, 0xcd, 0xef, + 0x11, 0x22, 0x33, 0x44, + 0x55, 0x66, 0x77, 0x88, + }; + + static constexpr uint16_t expected[] = { + 0x0145, 0x2367, + 0x89cd, 0xabef, + 0x1133, 0x2244, + 0x5577, 0x6688, + }; + + PcmExport::Params params; + params.dsd_u16 = true; + + CPPUNIT_ASSERT_EQUAL(params.CalcOutputSampleRate(705600u), 352800u); + CPPUNIT_ASSERT_EQUAL(params.CalcInputSampleRate(352800u), 705600u); + + PcmExport e; + e.Open(SampleFormat::DSD, 2, params); + + auto dest = e.Export({src, sizeof(src)}); + CPPUNIT_ASSERT_EQUAL(sizeof(expected), dest.size); + CPPUNIT_ASSERT(memcmp(dest.data, expected, dest.size) == 0); +} + void PcmExportTest::TestDsdU32() {