test_pcm_export.cxx 7.12 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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 "test_pcm_all.hxx"
#include "pcm/PcmExport.hxx"
23
#include "pcm/Traits.hxx"
24
#include "system/ByteOrder.hxx"
25
#include "util/ConstBuffer.hxx"
26 27 28 29 30 31 32 33 34

#include <string.h>

void
PcmExportTest::TestShift8()
{
	static constexpr int32_t src[] = { 0x0, 0x1, 0x100, 0x10000, 0xffffff };
	static constexpr uint32_t expected[] = { 0x0, 0x100, 0x10000, 0x1000000, 0xffffff00 };

35 36 37
	PcmExport::Params params;
	params.shift8 = true;

38 39 40
	CPPUNIT_ASSERT_EQUAL(params.CalcOutputSampleRate(42u), 42u);
	CPPUNIT_ASSERT_EQUAL(params.CalcInputSampleRate(42u), 42u);

41
	PcmExport e;
42
	e.Open(SampleFormat::S24_P32, 2, params);
43

44 45 46
	auto dest = e.Export({src, sizeof(src)});
	CPPUNIT_ASSERT_EQUAL(sizeof(expected), dest.size);
	CPPUNIT_ASSERT(memcmp(dest.data, expected, dest.size) == 0);
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
}

void
PcmExportTest::TestPack24()
{
	static constexpr int32_t src[] = { 0x0, 0x1, 0x100, 0x10000, 0xffffff };

	static constexpr uint8_t expected_be[] = {
		0, 0, 0x0,
		0, 0, 0x1,
		0, 0x1, 0x00,
		0x1, 0x00, 0x00,
		0xff, 0xff, 0xff,
	};

	static constexpr uint8_t expected_le[] = {
		0, 0, 0x0,
		0x1, 0, 0,
		0x00, 0x1, 0,
		0, 0x00, 0x01,
		0xff, 0xff, 0xff,
	};

	static constexpr size_t expected_size = sizeof(expected_be);
	static const uint8_t *const expected = IsBigEndian()
		? expected_be : expected_le;

74 75 76
	PcmExport::Params params;
	params.pack24 = true;

77 78 79
	CPPUNIT_ASSERT_EQUAL(params.CalcOutputSampleRate(42u), 42u);
	CPPUNIT_ASSERT_EQUAL(params.CalcInputSampleRate(42u), 42u);

80
	PcmExport e;
81
	e.Open(SampleFormat::S24_P32, 2, params);
82

83 84 85
	auto dest = e.Export({src, sizeof(src)});
	CPPUNIT_ASSERT_EQUAL(expected_size, dest.size);
	CPPUNIT_ASSERT(memcmp(dest.data, expected, dest.size) == 0);
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
}

void
PcmExportTest::TestReverseEndian()
{
	static constexpr uint8_t src[] = {
		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
	};

	static constexpr uint8_t expected2[] = {
		2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11
	};

	static constexpr uint8_t expected4[] = {
		4, 3, 2, 1, 8, 7, 6, 5, 12, 11, 10, 9,
	};

103 104 105
	PcmExport::Params params;
	params.reverse_endian = true;

106 107 108
	CPPUNIT_ASSERT_EQUAL(params.CalcOutputSampleRate(42u), 42u);
	CPPUNIT_ASSERT_EQUAL(params.CalcInputSampleRate(42u), 42u);

109
	PcmExport e;
110
	e.Open(SampleFormat::S8, 2, params);
111

112 113 114
	auto dest = e.Export({src, sizeof(src)});
	CPPUNIT_ASSERT_EQUAL(sizeof(src), dest.size);
	CPPUNIT_ASSERT(memcmp(dest.data, src, dest.size) == 0);
115

116
	e.Open(SampleFormat::S16, 2, params);
117 118 119
	dest = e.Export({src, sizeof(src)});
	CPPUNIT_ASSERT_EQUAL(sizeof(expected2), dest.size);
	CPPUNIT_ASSERT(memcmp(dest.data, expected2, dest.size) == 0);
120

121
	e.Open(SampleFormat::S32, 2, params);
122 123 124
	dest = e.Export({src, sizeof(src)});
	CPPUNIT_ASSERT_EQUAL(sizeof(expected4), dest.size);
	CPPUNIT_ASSERT(memcmp(dest.data, expected4, dest.size) == 0);
125 126
}

127 128
#ifdef ENABLE_DSD

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
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);
}

160 161 162 163 164 165
void
PcmExportTest::TestDsdU32()
{
	static constexpr uint8_t src[] = {
		0x01, 0x23, 0x45, 0x67,
		0x89, 0xab, 0xcd, 0xef,
166 167
		0x11, 0x22, 0x33, 0x44,
		0x55, 0x66, 0x77, 0x88,
168 169 170
	};

	static constexpr uint32_t expected[] = {
171 172 173 174
		0x014589cd,
		0x2367abef,
		0x11335577,
		0x22446688,
175 176 177 178 179
	};

	PcmExport::Params params;
	params.dsd_u32 = true;

180 181 182
	CPPUNIT_ASSERT_EQUAL(params.CalcOutputSampleRate(705600u), 176400u);
	CPPUNIT_ASSERT_EQUAL(params.CalcInputSampleRate(176400u), 705600u);

183 184 185 186 187 188 189 190
	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);
}

191
void
192
PcmExportTest::TestDop()
193 194 195 196 197 198 199 200 201 202 203 204 205
{
	static constexpr uint8_t src[] = {
		0x01, 0x23, 0x45, 0x67,
		0x89, 0xab, 0xcd, 0xef,
	};

	static constexpr uint32_t expected[] = {
		0xff050145,
		0xff052367,
		0xfffa89cd,
		0xfffaabef,
	};

206 207 208
	PcmExport::Params params;
	params.dop = true;

209 210
	CPPUNIT_ASSERT_EQUAL(params.CalcOutputSampleRate(705600u), 352800u);
	CPPUNIT_ASSERT_EQUAL(params.CalcInputSampleRate(352800u), 705600u);
211

212
	PcmExport e;
213
	e.Open(SampleFormat::DSD, 2, params);
214

215 216 217
	auto dest = e.Export({src, sizeof(src)});
	CPPUNIT_ASSERT_EQUAL(sizeof(expected), dest.size);
	CPPUNIT_ASSERT(memcmp(dest.data, expected, dest.size) == 0);
218
}
219

220 221
#endif

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
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,
	};

238 239 240
	PcmExport::Params params;
	params.alsa_channel_order = true;

241 242 243
	CPPUNIT_ASSERT_EQUAL(params.CalcOutputSampleRate(42u), 42u);
	CPPUNIT_ASSERT_EQUAL(params.CalcInputSampleRate(42u), 42u);

244
	PcmExport e;
245
	e.Open(F, 6, params);
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

	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,
	};

268 269 270
	PcmExport::Params params;
	params.alsa_channel_order = true;

271 272 273
	CPPUNIT_ASSERT_EQUAL(params.CalcOutputSampleRate(42u), 42u);
	CPPUNIT_ASSERT_EQUAL(params.CalcInputSampleRate(42u), 42u);

274
	PcmExport e;
275
	e.Open(F, 8, params);
276 277 278 279 280 281 282 283 284 285 286 287 288 289

	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>();
}