DsdiffDecoderPlugin.cxx 12.9 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20 21
/* \file
 *
22
 * This plugin decodes DSDIFF data (SACD) embedded in DFF files.
23
 * The DFF code was modeled after the specification found here:
24
 * http://www.sonicstudio.com/pdf/dsd/DSDIFF_1.5_Spec.pdf
25
 *
26
 * All functions common to both DSD decoders have been moved to dsdlib
27 28
 */

29
#include "config.h"
30
#include "DsdiffDecoderPlugin.hxx"
31
#include "../DecoderAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
32
#include "input/InputStream.hxx"
33
#include "pcm/CheckAudioFormat.hxx"
34
#include "util/BitReverse.hxx"
35
#include "util/ByteOrder.hxx"
36
#include "util/StringView.hxx"
37
#include "tag/Handler.hxx"
38
#include "DsdLib.hxx"
39

40
struct DsdiffHeader {
41
	DsdId id;
42
	DffDsdUint64 size;
43
	DsdId format;
44 45
};

46
struct DsdiffChunkHeader {
47
	DsdId id;
48
	DffDsdUint64 size;
49 50 51 52 53

	/**
	 * Read the "size" attribute from the specified header, converting it
	 * to the host byte order if needed.
	 */
54
	[[nodiscard]] constexpr
55
	uint64_t GetSize() const {
56
		return size.Read();
57
	}
58 59 60 61 62 63 64 65 66 67 68

	/**
	 * Applies padding to GetSize(), according to the DSDIFF
	 * specification
	 * (http://www.sonicstudio.com/pdf/dsd/DSDIFF_1.5_Spec.pdf)
	 * section 2.3.
	 */
	[[nodiscard]] constexpr
	uint64_t GetPaddedSize() const noexcept {
		return (GetSize() + 1) & ~uint64_t(1);
	}
69 70
};

71 72 73 74 75
/** struct for DSDIFF native Artist and Title tags */
struct dsdiff_native_tag {
	uint32_t size;
};

76
struct DsdiffMetaData {
77
	unsigned sample_rate, channels;
78
	bool bitreverse;
79
	offset_type chunk_size;
80 81
};

82
static bool lsbitfirst;
83 84

static bool
85
dsdiff_init(const ConfigBlock &block)
86
{
87
	lsbitfirst = block.GetBlockValue("lsbitfirst", false);
88 89 90
	return true;
}

91
static bool
92
dsdiff_read_id(DecoderClient *client, InputStream &is,
93
	       DsdId *id)
94
{
95
	return decoder_read_full(client, is, id, sizeof(*id));
96 97 98
}

static bool
99
dsdiff_read_chunk_header(DecoderClient *client, InputStream &is,
100
			 DsdiffChunkHeader *header)
101
{
102
	return decoder_read_full(client, is, header, sizeof(*header));
103 104 105
}

static bool
106
dsdiff_read_payload(DecoderClient *client, InputStream &is,
107
		    const DsdiffChunkHeader *header,
108 109
		    void *data, size_t length)
{
110
	uint64_t size = header->GetSize();
111 112 113
	if (size != (uint64_t)length)
		return false;

114
	return decoder_read_full(client, is, data, length);
115 116 117 118 119 120
}

/**
 * Read and parse a "SND" chunk inside "PROP".
 */
static bool
121
dsdiff_read_prop_snd(DecoderClient *client, InputStream &is,
122
		     DsdiffMetaData *metadata,
123
		     offset_type end_offset)
124
{
125
	DsdiffChunkHeader header;
126
	while (is.GetOffset() + sizeof(header) <= end_offset) {
127
		if (!dsdiff_read_chunk_header(client, is, &header))
128 129
			return false;

130
		offset_type chunk_end_offset = is.GetOffset()
131
			+ header.GetPaddedSize();
132 133 134
		if (chunk_end_offset > end_offset)
			return false;

135
		if (header.id.Equals("FS  ")) {
136
			uint32_t sample_rate;
137
			if (!dsdiff_read_payload(client, is, &header,
138 139 140 141
						 &sample_rate,
						 sizeof(sample_rate)))
				return false;

142
			metadata->sample_rate = FromBE32(sample_rate);
143
		} else if (header.id.Equals("CHNL")) {
144
			uint16_t channels;
145
			if (header.GetSize() < sizeof(channels) ||
146
			    !decoder_read_full(client, is,
147
					       &channels, sizeof(channels)) ||
148
			    !dsdlib_skip_to(client, is, chunk_end_offset))
149 150
				return false;

151
			metadata->channels = FromBE16(channels);
152 153
		} else if (header.id.Equals("CMPR")) {
			DsdId type;
154
			if (header.GetSize() < sizeof(type) ||
155
			    !decoder_read_full(client, is,
156
					       &type, sizeof(type)) ||
157
			    !dsdlib_skip_to(client, is, chunk_end_offset))
158 159
				return false;

160
			if (!type.Equals("DSD "))
161
				/* only uncompressed DSD audio data
162 163 164 165 166
				   is implemented */
				return false;
		} else {
			/* ignore unknown chunk */

167
			if (!dsdlib_skip_to(client, is, chunk_end_offset))
168 169 170 171
				return false;
		}
	}

172
	return is.GetOffset() == end_offset;
173 174 175 176 177 178
}

/**
 * Read and parse a "PROP" chunk.
 */
static bool
179
dsdiff_read_prop(DecoderClient *client, InputStream &is,
180 181
		 DsdiffMetaData *metadata,
		 const DsdiffChunkHeader *prop_header)
182
{
183
	uint64_t prop_size = prop_header->GetSize();
184
	const offset_type end_offset = is.GetOffset() + prop_size;
185

186
	DsdId prop_id;
187
	if (prop_size < sizeof(prop_id) ||
188
	    !dsdiff_read_id(client, is, &prop_id))
189 190
		return false;

191
	if (prop_id.Equals("SND "))
192
		return dsdiff_read_prop_snd(client, is, metadata, end_offset);
193 194
	else
		/* ignore unknown PROP chunk */
195
		return dsdlib_skip_to(client, is, end_offset);
196 197
}

198
static void
199
dsdiff_handle_native_tag(DecoderClient *client, InputStream &is,
200 201
			 TagHandler &handler,
			 offset_type tagoffset,
202
			 TagType type)
203
{
204
	if (!dsdlib_skip_to(client, is, tagoffset))
205 206 207 208
		return;

	struct dsdiff_native_tag metatag;

209
	if (!decoder_read_full(client, is, &metatag, sizeof(metatag)))
210 211
		return;

212
	uint32_t length = FromBE32(metatag.size);
213 214

	/* Check and limit size of the tag to prevent a stack overflow */
215
	constexpr size_t MAX_LENGTH = 1024;
216
	if (length == 0 || length > MAX_LENGTH)
217 218
		return;

219
	char string[MAX_LENGTH];
220 221 222
	char *label;
	label = string;

223
	if (!decoder_read_full(client, is, label, (size_t)length))
224 225
		return;

226
	handler.OnTag(type, {label, length});
227 228 229 230 231 232 233 234 235 236 237
}

/**
 * Read and parse additional metadata chunks for tagging purposes. By default
 * dsdiff files only support equivalents for artist and title but some of the
 * extract tools add an id3 tag to provide more tags. If such id3 is found
 * this will be used for tagging otherwise the native tags (if any) will be
 * used
 */

static bool
238
dsdiff_read_metadata_extra(DecoderClient *client, InputStream &is,
239 240
			   DsdiffMetaData *metadata,
			   DsdiffChunkHeader *chunk_header,
241
			   TagHandler &handler)
242 243 244
{

	/* skip from DSD data to next chunk header */
245
	if (!dsdlib_skip(client, is, metadata->chunk_size))
246
		return false;
247
	if (!dsdiff_read_chunk_header(client, is, chunk_header))
248 249
		return false;

250
	/** offset for artist tag */
251
	offset_type artist_offset = 0;
252
	/** offset for title tag */
253
	offset_type title_offset = 0;
254

255
#ifdef ENABLE_ID3TAG
256
	offset_type id3_offset = 0;
257 258 259 260 261
#endif

	/* Now process all the remaining chunk headers in the stream
	   and record their position and size */

262
	do {
263
		offset_type chunk_size = chunk_header->GetSize();
264 265

		/* DIIN chunk, is directly followed by other chunks  */
266
		if (chunk_header->id.Equals("DIIN"))
267 268 269
			chunk_size = 0;

		/* DIAR chunk - DSDIFF native tag for Artist */
270
		if (chunk_header->id.Equals("DIAR")) {
271
			chunk_size = chunk_header->GetSize();
272
			artist_offset = is.GetOffset();
273 274 275
		}

		/* DITI chunk - DSDIFF native tag for Title */
276
		if (chunk_header->id.Equals("DITI")) {
277
			chunk_size = chunk_header->GetSize();
278
			title_offset = is.GetOffset();
279
		}
280
#ifdef ENABLE_ID3TAG
281
		/* 'ID3 ' chunk, offspec. Used by sacdextract */
282
		if (chunk_header->id.Equals("ID3 ")) {
283
			chunk_size = chunk_header->GetSize();
284
			id3_offset = is.GetOffset();
285 286 287
		}
#endif

288
		if (!dsdlib_skip(client, is, chunk_size))
289
			break;
290
	} while (dsdiff_read_chunk_header(client, is, chunk_header));
291 292 293

	/* done processing chunk headers, process tags if any */

294
#ifdef ENABLE_ID3TAG
295
	if (id3_offset != 0) {
296 297
		/* a ID3 tag has preference over the other tags, do not process
		   other tags if we have one */
298
		dsdlib_tag_id3(is, handler, id3_offset);
299 300 301 302
		return true;
	}
#endif

303
	if (artist_offset != 0)
304
		dsdiff_handle_native_tag(client, is, handler,
305
					 artist_offset, TAG_ARTIST);
306

307
	if (title_offset != 0)
308
		dsdiff_handle_native_tag(client, is, handler,
309
					 title_offset, TAG_TITLE);
310 311 312
	return true;
}

313 314 315 316 317 318
/**
 * Read and parse all metadata chunks at the beginning.  Stop when the
 * first "DSD" chunk is seen, and return its header in the
 * "chunk_header" parameter.
 */
static bool
319
dsdiff_read_metadata(DecoderClient *client, InputStream &is,
320 321
		     DsdiffMetaData *metadata,
		     DsdiffChunkHeader *chunk_header)
322
{
323
	DsdiffHeader header;
324
	if (!decoder_read_full(client, is, &header, sizeof(header)) ||
325 326
	    !header.id.Equals("FRM8") ||
	    !header.format.Equals("DSD "))
327 328 329
		return false;

	while (true) {
330
		if (!dsdiff_read_chunk_header(client, is,
331
					      chunk_header))
332 333
			return false;

334
		if (chunk_header->id.Equals("PROP")) {
335
			if (!dsdiff_read_prop(client, is, metadata,
336
					      chunk_header))
337
					return false;
338
		} else if (chunk_header->id.Equals("DSD ")) {
339
			const offset_type chunk_size = chunk_header->GetSize();
340
			metadata->chunk_size = chunk_size;
341 342 343
			return true;
		} else {
			/* ignore unknown chunk */
344
			const offset_type chunk_size = chunk_header->GetSize();
345
			const offset_type chunk_end_offset =
346
				is.GetOffset() + chunk_size;
347

348
			if (!dsdlib_skip_to(client, is, chunk_end_offset))
349 350 351 352 353
				return false;
		}
	}
}

354 355 356 357 358 359 360
static void
bit_reverse_buffer(uint8_t *p, uint8_t *end)
{
	for (; p < end; ++p)
		*p = bit_reverse(*p);
}

361
static offset_type
362
FrameToOffset(uint64_t frame, unsigned channels)
363
{
364
	return frame * channels;
365 366
}

367 368 369 370
/**
 * Decode one "DSD" chunk.
 */
static bool
371
dsdiff_decode_chunk(DecoderClient &client, InputStream &is,
372
		    unsigned channels, unsigned sample_rate,
373
		    const offset_type total_bytes)
374
{
375
	const unsigned kbit_rate = channels * sample_rate / 1000;
376 377
	const offset_type start_offset = is.GetOffset();

378
	uint8_t buffer[8192];
379

380 381 382
	const size_t sample_size = sizeof(buffer[0]);
	const size_t frame_size = channels * sample_size;
	const unsigned buffer_frames = sizeof(buffer) / frame_size;
383
	const size_t buffer_size = buffer_frames * frame_size;
384

385
	auto cmd = client.GetCommand();
386
	for (offset_type remaining_bytes = total_bytes;
387
	     remaining_bytes >= frame_size && cmd != DecoderCommand::STOP;) {
388
		if (cmd == DecoderCommand::SEEK) {
389
			uint64_t frame = client.GetSeekFrame();
390
			offset_type offset = FrameToOffset(frame, channels);
391
			if (offset >= total_bytes) {
392
				client.CommandFinished();
393 394 395
				break;
			}

396
			if (dsdlib_skip_to(&client, is,
397
					   start_offset + offset)) {
398
				client.CommandFinished();
399 400
				remaining_bytes = total_bytes - offset;
			} else
401
				client.SeekError();
402 403
		}

404 405 406
		/* see how much aligned data from the remaining chunk
		   fits into the local buffer */
		size_t now_size = buffer_size;
407 408
		if (remaining_bytes < (offset_type)now_size) {
			unsigned now_frames = remaining_bytes / frame_size;
409 410 411
			now_size = now_frames * frame_size;
		}

412
		if (!decoder_read_full(&client, is, buffer, now_size))
413 414
			return false;

415
		const size_t nbytes = now_size;
416
		remaining_bytes -= nbytes;
417

418
		if (lsbitfirst)
419 420
			bit_reverse_buffer(buffer, buffer + nbytes);

421
		cmd = client.SubmitData(is, buffer, nbytes,
422
					kbit_rate);
423
	}
424 425

	return true;
426 427 428
}

static void
429
dsdiff_stream_decode(DecoderClient &client, InputStream &is)
430
{
431
	DsdiffMetaData metadata;
432

433
	DsdiffChunkHeader chunk_header;
434
	/* check if it is is a proper DFF file */
435
	if (!dsdiff_read_metadata(&client, is, &metadata, &chunk_header))
436
		return;
437

438 439 440
	auto audio_format = CheckAudioFormat(metadata.sample_rate / 8,
					     SampleFormat::DSD,
					     metadata.channels);
441

442
	/* calculate song time from DSD chunk size and sample frequency */
443
	offset_type chunk_size = metadata.chunk_size;
444 445 446 447

	uint64_t n_frames = chunk_size / audio_format.channels;
	auto songtime = SongTime::FromScale<uint64_t>(n_frames,
						      audio_format.sample_rate);
448

449
	/* success: file was recognized */
450
	client.Ready(audio_format, is.IsSeekable(), songtime);
451

452 453
	/* every iteration of the following loop decodes one "DSD"
	   chunk from a DFF file */
454

455
	dsdiff_decode_chunk(client, is,
456 457 458
			    metadata.channels,
			    metadata.sample_rate,
			    chunk_size);
459 460
}

461
static bool
462
dsdiff_scan_stream(InputStream &is, TagHandler &handler)
463
{
464 465
	DsdiffMetaData metadata;
	DsdiffChunkHeader chunk_header;
466

467
	/* First check for DFF metadata */
468
	if (!dsdiff_read_metadata(nullptr, is, &metadata, &chunk_header))
469
		return false;
470

471 472 473 474
	const auto sample_rate = metadata.sample_rate / 8;
	if (!audio_valid_sample_rate(sample_rate) ||
	    !audio_valid_channel_count(metadata.channels))
		return false;
475

476
	/* calculate song time and add as tag */
477
	uint64_t n_frames = metadata.chunk_size / metadata.channels;
478
	auto songtime = SongTime::FromScale<uint64_t>(n_frames,
479
						      sample_rate);
480
	handler.OnDuration(songtime);
481

482
	/* Read additional metadata and created tags if available */
483
	dsdiff_read_metadata_extra(nullptr, is, &metadata, &chunk_header,
484
				   handler);
485

486
	return true;
487 488 489 490
}

static const char *const dsdiff_suffixes[] = {
	"dff",
491
	nullptr
492 493 494 495
};

static const char *const dsdiff_mime_types[] = {
	"application/x-dff",
496 497
	"audio/x-dff",
	"audio/x-dsd",
498
	nullptr
499 500
};

501 502 503 504 505
constexpr DecoderPlugin dsdiff_decoder_plugin =
	DecoderPlugin("dsdiff", dsdiff_stream_decode, dsdiff_scan_stream)
	.WithInit(dsdiff_init)
	.WithSuffixes(dsdiff_suffixes)
	.WithMimeTypes(dsdiff_mime_types);