Commit 32380d1d authored by Max Kellermann's avatar Max Kellermann

pcm/Dop: stash odd frames away for the next call

First part of the "real" fix for https://github.com/MusicPlayerDaemon/MPD/issues/469
parent c9f1354e
......@@ -21,6 +21,8 @@
#include "ChannelDefs.hxx"
#include "util/ConstBuffer.hxx"
#include <functional>
#include <assert.h>
static constexpr uint32_t
......@@ -83,24 +85,14 @@ DsdToDopConverter::Open(unsigned _channels) noexcept
assert(audio_valid_channel_count(_channels));
channels = _channels;
rest_buffer.Open(channels);
}
ConstBuffer<uint32_t>
DsdToDopConverter::Convert(ConstBuffer<uint8_t> src) noexcept
{
assert(audio_valid_channel_count(channels));
assert(src.size % channels == 0);
const size_t num_src_samples = src.size;
const size_t num_src_frames = num_src_samples / channels;
/* this rounds down and discards up to 3 odd frames; not
elegant, but good enough for now */
const size_t num_dop_quads = num_src_frames / 4;
const size_t num_frames = num_dop_quads * 2;
const size_t num_samples = num_frames * channels;
uint32_t *const dest = (uint32_t *)buffer.GetT<uint32_t>(num_samples);
DsdToDop(dest, src.data, num_dop_quads, channels);
return { dest, num_samples };
using namespace std::placeholders;
return rest_buffer.Process<uint32_t>(buffer, src, 2 * channels,
std::bind(DsdToDop, _1, _2, _3, channels));
}
......@@ -21,6 +21,7 @@
#define MPD_PCM_DOP_HXX
#include "Buffer.hxx"
#include "RestBuffer.hxx"
#include <stdint.h>
......@@ -36,10 +37,13 @@ class DsdToDopConverter {
PcmBuffer buffer;
PcmRestBuffer<uint8_t, 4> rest_buffer;
public:
void Open(unsigned _channels) noexcept;
void Reset() noexcept {
rest_buffer.Reset();
}
ConstBuffer<uint32_t> Convert(ConstBuffer<uint8_t> src) noexcept;
......
......@@ -210,6 +210,41 @@ TEST(PcmTest, ExportDop)
auto dest = e.Export({src, sizeof(src)});
EXPECT_EQ(sizeof(expected), dest.size);
EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0);
/* not enough data: 2/8 */
static constexpr uint8_t src2[] = { 0x12, 0x34 };
static constexpr uint32_t expected2[] = {};
dest = e.Export({src2, sizeof(src2)});
ASSERT_EQ(sizeof(expected2), dest.size);
ASSERT_TRUE(memcmp(dest.data, expected2, dest.size) == 0);
/* not enough data: 6/8 */
static constexpr uint8_t src3[] = { 0x56, 0x78, 0x9a, 0xbc };
static constexpr uint32_t expected3[] = {};
dest = e.Export({src3, sizeof(src3)});
ASSERT_EQ(sizeof(expected3), dest.size);
ASSERT_TRUE(memcmp(dest.data, expected3, dest.size) == 0);
/* just enough data: 8/8 */
static constexpr uint8_t src4[] = { 0xde, 0xf0 };
static constexpr uint32_t expected4[] = { 0xff051256, 0xff053478, 0xfffa9ade, 0xfffabcf0 };
dest = e.Export({src4, sizeof(src4)});
ASSERT_EQ(sizeof(expected4), dest.size);
ASSERT_TRUE(memcmp(dest.data, expected4, dest.size) == 0);
/* not enough data: 6/8 */
static constexpr uint8_t src5[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
static constexpr uint32_t expected5[] = {};
dest = e.Export({src5, sizeof(src5)});
ASSERT_EQ(sizeof(expected5), dest.size);
ASSERT_TRUE(memcmp(dest.data, expected5, dest.size) == 0);
/* two quads returned, not enough data for more: 2/8 */
static constexpr uint8_t src6[] = { 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x10, 0x20 };
static constexpr uint32_t expected6[] = { 0xff051133, 0xff052244, 0xfffa5577, 0xfffa6688, 0xff0599bb, 0xff05aacc, 0xfffaddff, 0xfffaee00 };
dest = e.Export({src6, sizeof(src6)});
ASSERT_EQ(sizeof(expected6), dest.size);
ASSERT_TRUE(memcmp(dest.data, expected6, dest.size) == 0);
}
#endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment