pcm/Order: new library to convert from FLAC to ALSA channel order

This new library is integrated in the PcmExport class and (if enabled)
converts MPD's channel order (= FLAC channel order) to ALSA channel
order.

This fixes:
 http://bugs.musicpd.org/view.php?id=3147
and
 http://bugs.musicpd.org/view.php?id=3255
This commit is contained in:
Max Kellermann 2015-10-27 00:22:22 +01:00
parent 4b1630e1ec
commit 15e432204e
10 changed files with 269 additions and 8 deletions

View File

@ -537,6 +537,7 @@ libpcm_a_SOURCES = \
src/pcm/Neon.hxx \
src/pcm/FormatConverter.cxx src/pcm/FormatConverter.hxx \
src/pcm/ChannelsConverter.cxx src/pcm/ChannelsConverter.hxx \
src/pcm/Order.cxx src/pcm/Order.hxx \
src/pcm/Resampler.hxx \
src/pcm/GlueResampler.cxx src/pcm/GlueResampler.hxx \
src/pcm/FallbackResampler.cxx src/pcm/FallbackResampler.hxx \

1
NEWS
View File

@ -22,6 +22,7 @@ ver 0.20 (not yet released)
- embcue: fix last track
- flac: new plugin which reads the "CUESHEET" metadata block
* output
- alsa: fix multi-channel order
- jack: reduce CPU usage
- pulse: set channel map to WAVE-EX
- recorder: record tags

View File

@ -711,7 +711,7 @@ AlsaOutput::SetupOrDop(AudioFormat &audio_format, Error &error)
pcm_export->Open(audio_format.format,
audio_format.channels,
dop2, shift8, packed, reverse_endian);
true, dop2, shift8, packed, reverse_endian);
return true;
}

View File

@ -537,7 +537,7 @@ oss_probe_sample_format(int fd, SampleFormat sample_format,
*oss_format_r = oss_format;
#ifdef AFMT_S24_PACKED
pcm_export.Open(sample_format, 0, false, false,
pcm_export.Open(sample_format, 0, true, false, false,
oss_format == AFMT_S24_PACKED,
oss_format == AFMT_S24_PACKED &&
!IsLittleEndian());

135
src/pcm/Order.cxx Normal file
View File

@ -0,0 +1,135 @@
/*
* Copyright (C) 2003-2015 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 "Order.hxx"
#include "PcmBuffer.hxx"
#include "util/ConstBuffer.hxx"
template<typename V>
struct TwoPointers {
V *dest;
const V *src;
TwoPointers<V> &CopyOne() {
*dest++ = *src++;
return *this;
}
TwoPointers<V> &CopyTwo() {
return CopyOne().CopyOne();
}
TwoPointers<V> &SwapTwoPairs() {
*dest++ = src[2];
*dest++ = src[3];
*dest++ = src[0];
*dest++ = src[1];
src += 4;
return *this;
}
TwoPointers<V> &ToAlsa51() {
return CopyTwo() // left+right
.SwapTwoPairs(); // center, LFE, surround left+right
}
TwoPointers<V> &ToAlsa71() {
return ToAlsa51()
.CopyTwo(); // side left+right
}
};
template<typename V>
static void
ToAlsaChannelOrder51(V *dest, const V *src, size_t n)
{
TwoPointers<V> p{dest, src};
for (size_t i = 0; i != n; ++i)
p.ToAlsa51();
}
template<typename V>
static inline ConstBuffer<V>
ToAlsaChannelOrder51(PcmBuffer &buffer, ConstBuffer<V> src)
{
auto dest = buffer.GetT<V>(src.size);
ToAlsaChannelOrder51(dest, src.data, src.size / 6);
return { dest, src.size };
}
template<typename V>
static void
ToAlsaChannelOrder71(V *dest, const V *src, size_t n)
{
TwoPointers<V> p{dest, src};
for (size_t i = 0; i != n; ++i)
p.ToAlsa71();
}
template<typename V>
static inline ConstBuffer<V>
ToAlsaChannelOrder71(PcmBuffer &buffer, ConstBuffer<V> src)
{
auto dest = buffer.GetT<V>(src.size);
ToAlsaChannelOrder71(dest, src.data, src.size / 6);
return { dest, src.size };
}
template<typename V>
static ConstBuffer<V>
ToAlsaChannelOrderT(PcmBuffer &buffer, ConstBuffer<V> src, unsigned channels)
{
switch (channels) {
case 6: // 5.1
return ToAlsaChannelOrder51(buffer, src);
case 8: // 7.1
return ToAlsaChannelOrder71(buffer, src);
default:
return src;
}
}
ConstBuffer<void>
ToAlsaChannelOrder(PcmBuffer &buffer, ConstBuffer<void> src,
SampleFormat sample_format, unsigned channels)
{
switch (sample_format) {
case SampleFormat::UNDEFINED:
case SampleFormat::S8:
case SampleFormat::DSD:
return src;
case SampleFormat::S16:
return ToAlsaChannelOrderT(buffer,
ConstBuffer<int16_t>::FromVoid(src),
channels).ToVoid();
case SampleFormat::S24_P32:
case SampleFormat::S32:
case SampleFormat::FLOAT:
return ToAlsaChannelOrderT(buffer,
ConstBuffer<int32_t>::FromVoid(src),
channels).ToVoid();
}
gcc_unreachable();
}

37
src/pcm/Order.hxx Normal file
View File

@ -0,0 +1,37 @@
/*
* Copyright (C) 2003-2015 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_ORDER_HXX
#define MPD_PCM_ORDER_HXX
#include "check.h"
#include "AudioFormat.hxx"
class PcmBuffer;
template<typename T> struct ConstBuffer;
/**
* Convert the given buffer from FLAC channel order
* (https://xiph.org/flac/format.html) to ALSA channel order.
*/
ConstBuffer<void>
ToAlsaChannelOrder(PcmBuffer &buffer, ConstBuffer<void> src,
SampleFormat sample_format, unsigned channels);
#endif

View File

@ -19,6 +19,7 @@
#include "config.h"
#include "PcmExport.hxx"
#include "Order.hxx"
#include "PcmDop.hxx"
#include "PcmPack.hxx"
#include "util/ByteReverse.hxx"
@ -28,12 +29,16 @@
void
PcmExport::Open(SampleFormat sample_format, unsigned _channels,
bool _alsa_channel_order,
bool _dop, bool _shift8, bool _pack, bool _reverse_endian)
{
assert(audio_valid_sample_format(sample_format));
assert(!_dop || audio_valid_channel_count(_channels));
channels = _channels;
alsa_channel_order = _alsa_channel_order
? sample_format
: SampleFormat::UNDEFINED;
dop = _dop && sample_format == SampleFormat::DSD;
if (dop)
/* after the conversion to DoP, the DSD
@ -77,6 +82,10 @@ PcmExport::GetFrameSize(const AudioFormat &audio_format) const
ConstBuffer<void>
PcmExport::Export(ConstBuffer<void> data)
{
if (alsa_channel_order != SampleFormat::UNDEFINED)
data = ToAlsaChannelOrder(order_buffer, data,
alsa_channel_order, channels);
if (dop)
data = pcm_dsd_to_dop(dop_buffer, channels,
ConstBuffer<uint8_t>::FromVoid(data))

View File

@ -33,6 +33,13 @@ template<typename T> struct ConstBuffer;
* representation which are not supported by the pcm_convert library.
*/
struct PcmExport {
/**
* This buffer is used to reorder channels.
*
* @see #alsa_channel_order
*/
PcmBuffer order_buffer;
/**
* The buffer is used to convert DSD samples to the
* DoP format.
@ -60,6 +67,16 @@ struct PcmExport {
*/
uint8_t channels;
/**
* Convert the given buffer from FLAC channel order to ALSA
* channel order using ToAlsaChannelOrder()?
*
* If this value is SampleFormat::UNDEFINED, then no channel
* reordering is applied, otherwise this is the input sample
* format.
*/
SampleFormat alsa_channel_order;
/**
* Convert DSD to DSD-over-PCM (DoP)? Input format must be
* SampleFormat::DSD and output format must be
@ -96,6 +113,7 @@ struct PcmExport {
* @param channels the number of channels; ignored unless dop is set
*/
void Open(SampleFormat sample_format, unsigned channels,
bool _alsa_channel_order,
bool dop, bool shift8, bool pack, bool reverse_endian);
/**

View File

@ -126,6 +126,7 @@ class PcmExportTest : public CppUnit::TestFixture {
CPPUNIT_TEST(TestPack24);
CPPUNIT_TEST(TestReverseEndian);
CPPUNIT_TEST(TestDop);
CPPUNIT_TEST(TestAlsaChannelOrder);
CPPUNIT_TEST_SUITE_END();
public:
@ -133,6 +134,7 @@ public:
void TestPack24();
void TestReverseEndian();
void TestDop();
void TestAlsaChannelOrder();
};
#endif

View File

@ -20,6 +20,7 @@
#include "config.h"
#include "test_pcm_all.hxx"
#include "pcm/PcmExport.hxx"
#include "pcm/Traits.hxx"
#include "system/ByteOrder.hxx"
#include "util/ConstBuffer.hxx"
@ -32,7 +33,7 @@ PcmExportTest::TestShift8()
static constexpr uint32_t expected[] = { 0x0, 0x100, 0x10000, 0x1000000, 0xffffff00 };
PcmExport e;
e.Open(SampleFormat::S24_P32, 2, false, true, false, false);
e.Open(SampleFormat::S24_P32, 2, false, false, true, false, false);
auto dest = e.Export({src, sizeof(src)});
CPPUNIT_ASSERT_EQUAL(sizeof(expected), dest.size);
@ -65,7 +66,7 @@ PcmExportTest::TestPack24()
? expected_be : expected_le;
PcmExport e;
e.Open(SampleFormat::S24_P32, 2, false, false, true, false);
e.Open(SampleFormat::S24_P32, 2, false, false, false, true, false);
auto dest = e.Export({src, sizeof(src)});
CPPUNIT_ASSERT_EQUAL(expected_size, dest.size);
@ -88,18 +89,18 @@ PcmExportTest::TestReverseEndian()
};
PcmExport e;
e.Open(SampleFormat::S8, 2, false, false, false, true);
e.Open(SampleFormat::S8, 2, false, false, false, false, true);
auto dest = e.Export({src, sizeof(src)});
CPPUNIT_ASSERT_EQUAL(sizeof(src), dest.size);
CPPUNIT_ASSERT(memcmp(dest.data, src, dest.size) == 0);
e.Open(SampleFormat::S16, 2, false, false, false, true);
e.Open(SampleFormat::S16, 2, false, false, false, false, true);
dest = e.Export({src, sizeof(src)});
CPPUNIT_ASSERT_EQUAL(sizeof(expected2), dest.size);
CPPUNIT_ASSERT(memcmp(dest.data, expected2, dest.size) == 0);
e.Open(SampleFormat::S32, 2, false, false, false, true);
e.Open(SampleFormat::S32, 2, false, false, false, false, true);
dest = e.Export({src, sizeof(src)});
CPPUNIT_ASSERT_EQUAL(sizeof(expected4), dest.size);
CPPUNIT_ASSERT(memcmp(dest.data, expected4, dest.size) == 0);
@ -121,9 +122,66 @@ PcmExportTest::TestDop()
};
PcmExport e;
e.Open(SampleFormat::DSD, 2, true, false, false, false);
e.Open(SampleFormat::DSD, 2, false, true, false, false, false);
auto dest = e.Export({src, sizeof(src)});
CPPUNIT_ASSERT_EQUAL(sizeof(expected), dest.size);
CPPUNIT_ASSERT(memcmp(dest.data, expected, dest.size) == 0);
}
template<SampleFormat F, class Traits=SampleTraits<F>>
static void
TestAlsaChannelOrder51()
{
typedef typename Traits::value_type value_type;
static constexpr value_type src[] = {
0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11,
};
static constexpr value_type expected[] = {
0, 1, 4, 5, 2, 3,
6, 7, 10, 11, 8, 9,
};
PcmExport e;
e.Open(F, 6, true, false, false, false, false);
auto dest = e.Export({src, sizeof(src)});
CPPUNIT_ASSERT_EQUAL(sizeof(expected), dest.size);
CPPUNIT_ASSERT(memcmp(dest.data, expected, dest.size) == 0);
}
template<SampleFormat F, class Traits=SampleTraits<F>>
static void
TestAlsaChannelOrder71()
{
typedef typename Traits::value_type value_type;
static constexpr value_type src[] = {
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
};
static constexpr value_type expected[] = {
0, 1, 4, 5, 2, 3, 6, 7,
8, 9, 12, 13, 10, 11, 14, 15,
};
PcmExport e;
e.Open(F, 8, true, false, false, false, false);
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::TestAlsaChannelOrder()
{
TestAlsaChannelOrder51<SampleFormat::S16>();
TestAlsaChannelOrder71<SampleFormat::S16>();
TestAlsaChannelOrder51<SampleFormat::S32>();
TestAlsaChannelOrder71<SampleFormat::S32>();
}