test_pcm_export.cxx 13.1 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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"
21
#include "pcm/Export.hxx"
22
#include "pcm/Traits.hxx"
23
#include "util/ByteOrder.hxx"
24
#include "util/ConstBuffer.hxx"
25

26 27
#include <gtest/gtest.h>

28 29
#include <string.h>

30
TEST(PcmTest, ExportShift8)
31 32 33 34
{
	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
	EXPECT_EQ(params.CalcOutputSampleRate(42u), 42u);
	EXPECT_EQ(params.CalcInputSampleRate(42u), 42u);
40

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

44 45 46 47 48
	EXPECT_EQ(e.GetInputFrameSize(), 8u);
	EXPECT_EQ(e.GetOutputFrameSize(), 8u);
	EXPECT_EQ(e.GetInputBlockSize(), 8u);
	EXPECT_EQ(e.GetOutputBlockSize(), 8u);

49
	auto dest = e.Export({src, sizeof(src)});
50 51
	EXPECT_EQ(sizeof(expected), dest.size);
	EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0);
52 53 54 55 56 57

	const auto silence = e.GetSilence();
	constexpr uint8_t expected_silence[8]{};
	EXPECT_EQ(silence.size, sizeof(expected_silence));
	EXPECT_EQ(memcmp(silence.data, expected_silence,
			 sizeof(expected_silence)), 0);
58 59
}

60
TEST(PcmTest, ExportPack24)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
{
	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;

84 85 86
	PcmExport::Params params;
	params.pack24 = true;

87 88
	EXPECT_EQ(params.CalcOutputSampleRate(42u), 42u);
	EXPECT_EQ(params.CalcInputSampleRate(42u), 42u);
89

90
	PcmExport e;
91
	e.Open(SampleFormat::S24_P32, 2, params);
92

93 94 95 96 97
	EXPECT_EQ(e.GetInputFrameSize(), 8u);
	EXPECT_EQ(e.GetOutputFrameSize(), 6u);
	EXPECT_EQ(e.GetInputBlockSize(), 8u);
	EXPECT_EQ(e.GetOutputBlockSize(), 6u);

98
	auto dest = e.Export({src, sizeof(src)});
99 100
	EXPECT_EQ(expected_size, dest.size);
	EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0);
101 102 103 104 105 106

	const auto silence = e.GetSilence();
	constexpr uint8_t expected_silence[6]{};
	EXPECT_EQ(silence.size, sizeof(expected_silence));
	EXPECT_EQ(memcmp(silence.data, expected_silence,
			 sizeof(expected_silence)), 0);
107 108
}

109
TEST(PcmTest, ExportReverseEndian)
110 111 112 113 114 115 116 117 118 119 120 121 122
{
	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,
	};

123 124 125
	PcmExport::Params params;
	params.reverse_endian = true;

126 127
	EXPECT_EQ(params.CalcOutputSampleRate(42u), 42u);
	EXPECT_EQ(params.CalcInputSampleRate(42u), 42u);
128

129
	PcmExport e;
130
	e.Open(SampleFormat::S8, 2, params);
131

132 133 134 135 136
	EXPECT_EQ(e.GetInputFrameSize(), 2u);
	EXPECT_EQ(e.GetOutputFrameSize(), 2u);
	EXPECT_EQ(e.GetInputBlockSize(), 2u);
	EXPECT_EQ(e.GetOutputBlockSize(), 2u);

137
	auto dest = e.Export({src, sizeof(src)});
138 139
	EXPECT_EQ(sizeof(src), dest.size);
	EXPECT_TRUE(memcmp(dest.data, src, dest.size) == 0);
140

141
	e.Open(SampleFormat::S16, 2, params);
142 143 144 145 146 147

	EXPECT_EQ(e.GetInputFrameSize(), 4u);
	EXPECT_EQ(e.GetOutputFrameSize(), 4u);
	EXPECT_EQ(e.GetInputBlockSize(), 4u);
	EXPECT_EQ(e.GetOutputBlockSize(), 4u);

148
	dest = e.Export({src, sizeof(src)});
149 150
	EXPECT_EQ(sizeof(expected2), dest.size);
	EXPECT_TRUE(memcmp(dest.data, expected2, dest.size) == 0);
151

152
	e.Open(SampleFormat::S32, 2, params);
153 154 155 156 157 158

	EXPECT_EQ(e.GetInputFrameSize(), 8u);
	EXPECT_EQ(e.GetOutputFrameSize(), 8u);
	EXPECT_EQ(e.GetInputBlockSize(), 8u);
	EXPECT_EQ(e.GetOutputBlockSize(), 8u);

159
	dest = e.Export({src, sizeof(src)});
160 161
	EXPECT_EQ(sizeof(expected4), dest.size);
	EXPECT_TRUE(memcmp(dest.data, expected4, dest.size) == 0);
162 163 164 165 166 167

	const auto silence = e.GetSilence();
	constexpr uint8_t expected_silence[8]{};
	EXPECT_EQ(silence.size, sizeof(expected_silence));
	EXPECT_EQ(memcmp(silence.data, expected_silence,
			 sizeof(expected_silence)), 0);
168 169
}

170 171
#ifdef ENABLE_DSD

172
TEST(PcmTest, ExportDsdU16)
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
{
	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;
189
	params.dsd_mode = PcmExport::DsdMode::U16;
190

191 192
	EXPECT_EQ(params.CalcOutputSampleRate(705600u), 352800u);
	EXPECT_EQ(params.CalcInputSampleRate(352800u), 705600u);
193 194 195 196

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

197 198 199 200 201
	EXPECT_EQ(e.GetInputFrameSize(), 2u);
	EXPECT_EQ(e.GetOutputFrameSize(), 4u);
	EXPECT_EQ(e.GetInputBlockSize(), 4u);
	EXPECT_EQ(e.GetOutputBlockSize(), 4u);

202
	auto dest = e.Export({src, sizeof(src)});
203 204
	EXPECT_EQ(sizeof(expected), dest.size);
	EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0);
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

	/* no output, 2/4 remains */
	static constexpr uint8_t src2[] = { 0x11, 0x22 };
	static constexpr uint16_t expected2[] = {};
	dest = e.Export({src2, sizeof(src2)});
	EXPECT_EQ(sizeof(expected2), dest.size);
	EXPECT_TRUE(memcmp(dest.data, expected2, dest.size) == 0);

	/* one full frame and 2/4 remains */
	static constexpr uint8_t src3[] = { 0x33, 0x44, 0x55, 0x66 };
	static constexpr uint16_t expected3[] = { 0x1133, 0x2244 };
	dest = e.Export({src3, sizeof(src3)});
	EXPECT_EQ(sizeof(expected3), dest.size);
	EXPECT_TRUE(memcmp(dest.data, expected3, dest.size) == 0);

	/* two full frames and 2/4 remains again */
	static constexpr uint8_t src4[] = { 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee };
	static constexpr uint16_t expected4[] = { 0x5577, 0x6688, 0x99bb, 0xaacc };
	dest = e.Export({src4, sizeof(src4)});
	EXPECT_EQ(sizeof(expected4), dest.size);
	EXPECT_TRUE(memcmp(dest.data, expected4, dest.size) == 0);
226 227 228 229 230 231

	const auto silence = e.GetSilence();
	constexpr uint8_t expected_silence[]{0x69, 0x69, 0x69, 0x69};
	EXPECT_EQ(silence.size, sizeof(expected_silence));
	EXPECT_EQ(memcmp(silence.data, expected_silence,
			 sizeof(expected_silence)), 0);
232 233
}

234
TEST(PcmTest, ExportDsdU32)
235 236 237 238
{
	static constexpr uint8_t src[] = {
		0x01, 0x23, 0x45, 0x67,
		0x89, 0xab, 0xcd, 0xef,
239 240
		0x11, 0x22, 0x33, 0x44,
		0x55, 0x66, 0x77, 0x88,
241 242 243
	};

	static constexpr uint32_t expected[] = {
244 245 246 247
		0x014589cd,
		0x2367abef,
		0x11335577,
		0x22446688,
248 249 250
	};

	PcmExport::Params params;
251
	params.dsd_mode = PcmExport::DsdMode::U32;
252

253 254
	EXPECT_EQ(params.CalcOutputSampleRate(705600u), 176400u);
	EXPECT_EQ(params.CalcInputSampleRate(176400u), 705600u);
255

256 257 258
	PcmExport e;
	e.Open(SampleFormat::DSD, 2, params);

259 260 261 262 263
	EXPECT_EQ(e.GetInputFrameSize(), 2u);
	EXPECT_EQ(e.GetOutputFrameSize(), 8u);
	EXPECT_EQ(e.GetInputBlockSize(), 8u);
	EXPECT_EQ(e.GetOutputBlockSize(), 8u);

264
	auto dest = e.Export({src, sizeof(src)});
265 266
	EXPECT_EQ(sizeof(expected), dest.size);
	EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0);
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

	/* no output, 4/8 remains */
	static constexpr uint8_t src2[] = { 0x11, 0x22, 0x33, 0x44 };
	static constexpr uint32_t expected2[] = {};
	dest = e.Export({src2, sizeof(src2)});
	EXPECT_EQ(sizeof(expected2), dest.size);
	EXPECT_TRUE(memcmp(dest.data, expected2, dest.size) == 0);

	/* one full frame and 4/8 remains */
	static constexpr uint8_t src3[] = { 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc };
	static constexpr uint32_t expected3[] = { 0x11335577, 0x22446688 };
	dest = e.Export({src3, sizeof(src3)});
	EXPECT_EQ(sizeof(expected3), dest.size);
	EXPECT_TRUE(memcmp(dest.data, expected3, dest.size) == 0);

	/* two full frames and 2/4 remains again */
	static constexpr uint8_t src4[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
	static constexpr uint32_t expected4[] = { 0x99bb0103, 0xaacc0204 };
	dest = e.Export({src4, sizeof(src4)});
	EXPECT_EQ(sizeof(expected4), dest.size);
	EXPECT_TRUE(memcmp(dest.data, expected4, dest.size) == 0);
288 289 290 291 292 293

	const auto silence = e.GetSilence();
	constexpr uint8_t expected_silence[]{0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69};
	EXPECT_EQ(silence.size, sizeof(expected_silence));
	EXPECT_EQ(memcmp(silence.data, expected_silence,
			 sizeof(expected_silence)), 0);
294 295
}

296
TEST(PcmTest, ExportDop)
297 298 299 300 301 302 303 304 305 306 307 308 309
{
	static constexpr uint8_t src[] = {
		0x01, 0x23, 0x45, 0x67,
		0x89, 0xab, 0xcd, 0xef,
	};

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

310
	PcmExport::Params params;
311
	params.dsd_mode = PcmExport::DsdMode::DOP;
312

313 314
	EXPECT_EQ(params.CalcOutputSampleRate(705600u), 352800u);
	EXPECT_EQ(params.CalcInputSampleRate(352800u), 705600u);
315

316
	PcmExport e;
317
	e.Open(SampleFormat::DSD, 2, params);
318

319 320 321 322 323
	EXPECT_EQ(e.GetInputFrameSize(), 2u);
	EXPECT_EQ(e.GetOutputFrameSize(), 8u);
	EXPECT_EQ(e.GetInputBlockSize(), 8u);
	EXPECT_EQ(e.GetOutputBlockSize(), 16u);

324
	auto dest = e.Export({src, sizeof(src)});
325 326
	EXPECT_EQ(sizeof(expected), dest.size);
	EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0);
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

	/* 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);
362 363 364 365 366 367

	const auto silence = e.GetSilence();
	constexpr uint32_t expected_silence[]{0xff056969, 0xff056969, 0xfffa6969, 0xfffa6969};
	EXPECT_EQ(silence.size, sizeof(expected_silence));
	EXPECT_EQ(memcmp(silence.data, expected_silence,
			 sizeof(expected_silence)), 0);
368
}
369

370 371
#endif

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
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,
	};

388 389 390
	PcmExport::Params params;
	params.alsa_channel_order = true;

391 392
	EXPECT_EQ(params.CalcOutputSampleRate(42u), 42u);
	EXPECT_EQ(params.CalcInputSampleRate(42u), 42u);
393

394
	PcmExport e;
395
	e.Open(F, 6, params);
396 397

	auto dest = e.Export({src, sizeof(src)});
398 399
	EXPECT_EQ(sizeof(expected), dest.size);
	EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0);
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
}

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

418 419 420
	PcmExport::Params params;
	params.alsa_channel_order = true;

421 422
	EXPECT_EQ(params.CalcOutputSampleRate(42u), 42u);
	EXPECT_EQ(params.CalcInputSampleRate(42u), 42u);
423

424
	PcmExport e;
425
	e.Open(F, 8, params);
426 427

	auto dest = e.Export({src, sizeof(src)});
428 429
	EXPECT_EQ(sizeof(expected), dest.size);
	EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0);
430 431 432 433 434 435

	const auto silence = e.GetSilence();
	constexpr value_type expected_silence[8]{};
	EXPECT_EQ(silence.size, sizeof(expected_silence));
	EXPECT_EQ(memcmp(silence.data, expected_silence,
			 sizeof(expected_silence)), 0);
436 437
}

438
TEST(PcmTest, ExportAlsaChannelOrder)
439 440 441 442 443 444
{
	TestAlsaChannelOrder51<SampleFormat::S16>();
	TestAlsaChannelOrder71<SampleFormat::S16>();
	TestAlsaChannelOrder51<SampleFormat::S32>();
	TestAlsaChannelOrder71<SampleFormat::S32>();
}