pcm/export: support DSD_U16

This commit is contained in:
Max Kellermann 2017-01-11 22:33:37 +01:00
parent a3c33000ee
commit 938affef32
7 changed files with 162 additions and 1 deletions

View File

@ -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

62
src/pcm/Dsd16.cxx Normal file
View File

@ -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<uint16_t>
Dsd8To16(PcmBuffer &buffer, unsigned channels, ConstBuffer<uint8_t> _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<uint16_t>(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};
}

36
src/pcm/Dsd16.hxx Normal file
View File

@ -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 <stdint.h>
template<typename T> struct ConstBuffer;
class PcmBuffer;
/**
* Convert DSD_U8 to DSD_U16 (native endian, oldest bits in MSB).
*/
ConstBuffer<uint16_t>
Dsd8To16(PcmBuffer &buffer, unsigned channels, ConstBuffer<uint8_t> src);
#endif

View File

@ -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<void> data)
alsa_channel_order, channels);
#ifdef ENABLE_DSD
if (dsd_u16)
data = Dsd8To16(dop_buffer, channels,
ConstBuffer<uint8_t>::FromVoid(data))
.ToVoid();
if (dsd_u32)
data = Dsd8To32(dop_buffer, channels,
ConstBuffer<uint8_t>::FromVoid(data))

View File

@ -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

View File

@ -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

View File

@ -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()
{