DsfDecoderPlugin.cxx 9.91 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 23 24 25 26 27 28 29 30
 * 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.
 */

/* \file
 *
 * This plugin decodes DSDIFF data (SACD) embedded in DSF files.
 *
 * The DSF code was created using the specification found here:
 * http://dsd-guide.com/sonys-dsf-file-format-spec
 *
 * All functions common to both DSD decoders have been moved to dsdlib
 */

#include "config.h"
31
#include "DsfDecoderPlugin.hxx"
32
#include "../DecoderAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
33
#include "input/InputStream.hxx"
34
#include "CheckAudioFormat.hxx"
35
#include "util/bit_reverse.h"
36
#include "system/ByteOrder.hxx"
37
#include "DsdLib.hxx"
38
#include "tag/Handler.hxx"
39
#include "Log.hxx"
40

41 42
#include <string.h>

43 44
static constexpr unsigned DSF_BLOCK_SIZE = 4096;

45
struct DsfMetaData {
46 47
	unsigned sample_rate, channels;
	bool bitreverse;
48
	offset_type n_blocks;
49
#ifdef ENABLE_ID3TAG
50
	offset_type id3_offset;
51
#endif
52 53
};

54
struct DsfHeader {
55
	/** DSF header id: "DSD " */
56
	DsdId id;
57
	/** DSD chunk size, including id = 28 */
58
	DsdUint64 size;
59
	/** total file size */
60
	DsdUint64 fsize;
61
	/** pointer to id3v2 metadata, should be at the end of the file */
62
	DsdUint64 pmeta;
63
};
64

65
/** DSF file fmt chunk */
66
struct DsfFmtChunk {
67
	/** id: "fmt " */
68
	DsdId id;
69
	/** fmt chunk size, including id, normally 52 */
70
	DsdUint64 size;
71 72 73 74 75 76 77 78 79 80 81 82 83
	/** version of this format = 1 */
	uint32_t version;
	/** 0: DSD raw */
	uint32_t formatid;
	/** channel type, 1 = mono, 2 = stereo, 3 = 3 channels, etc */
	uint32_t channeltype;
	/** Channel number, 1 = mono, 2 = stereo, ... 6 = 6 channels */
	uint32_t channelnum;
	/** sample frequency: 2822400, 5644800 */
	uint32_t sample_freq;
	/** bits per sample 1 or 8 */
	uint32_t bitssample;
	/** Sample count per channel in bytes */
84
	DsdUint64 scnt;
85 86 87 88 89 90
	/** block size per channel = 4096 */
	uint32_t block_size;
	/** reserved, should be all zero */
	uint32_t reserved;
};

91
struct DsfDataChunk {
92
	DsdId id;
93
	/** "data" chunk size, includes header (id+size) */
94
	DsdUint64 size;
95 96 97 98 99 100
};

/**
 * Read and parse all needed metadata chunks for DSF files.
 */
static bool
101
dsf_read_metadata(DecoderClient *client, InputStream &is,
102
		  DsfMetaData *metadata)
103
{
104
	DsfHeader dsf_header;
105
	if (!decoder_read_full(client, is, &dsf_header, sizeof(dsf_header)) ||
106
	    !dsf_header.id.Equals("DSD "))
107 108
		return false;

109
	const offset_type chunk_size = dsf_header.size.Read();
110 111 112
	if (sizeof(dsf_header) != chunk_size)
		return false;

113
#ifdef ENABLE_ID3TAG
114
	const offset_type metadata_offset = dsf_header.pmeta.Read();
115 116
#endif

117
	/* read the 'fmt ' chunk of the DSF file */
118
	DsfFmtChunk dsf_fmt_chunk;
119
	if (!decoder_read_full(client, is,
120
			       &dsf_fmt_chunk, sizeof(dsf_fmt_chunk)) ||
121
	    !dsf_fmt_chunk.id.Equals("fmt "))
122 123
		return false;

124
	const uint64_t fmt_chunk_size = dsf_fmt_chunk.size.Read();
125 126 127
	if (fmt_chunk_size != sizeof(dsf_fmt_chunk))
		return false;

128
	uint32_t samplefreq = FromLE32(dsf_fmt_chunk.sample_freq);
129
	const unsigned channels = FromLE32(dsf_fmt_chunk.channelnum);
130 131

	/* for now, only support version 1 of the standard, DSD raw stereo
132
	   files with a sample freq of 2822400 or 5644800 Hz */
133

134 135
	if (FromLE32(dsf_fmt_chunk.version) != 1 ||
	    FromLE32(dsf_fmt_chunk.formatid) != 0 ||
136
	    !audio_valid_channel_count(channels) ||
137
	    !dsdlib_valid_freq(samplefreq))
138 139
		return false;

140
	uint32_t chblksize = FromLE32(dsf_fmt_chunk.block_size);
141
	/* according to the spec block size should always be 4096 */
142
	if (chblksize != DSF_BLOCK_SIZE)
143 144 145
		return false;

	/* read the 'data' chunk of the DSF file */
146
	DsfDataChunk data_chunk;
147
	if (!decoder_read_full(client, is, &data_chunk, sizeof(data_chunk)) ||
148
	    !data_chunk.id.Equals("data"))
149 150 151 152 153
		return false;

	/* data size of DSF files are padded to multiple of 4096,
	   we use the actual data size as chunk size */

154
	offset_type data_size = data_chunk.size.Read();
155 156 157
	if (data_size < sizeof(data_chunk))
		return false;

158 159
	data_size -= sizeof(data_chunk);

160
	/* data_size cannot be bigger or equal to total file size */
161 162
	if (is.KnownSize() && data_size > is.GetRest())
		return false;
163

164 165 166 167
	/* use the sample count from the DSF header as the upper
	   bound, because some DSF files contain junk at the end of
	   the "data" chunk */
	const uint64_t samplecnt = dsf_fmt_chunk.scnt.Read();
168
	const offset_type playable_size = samplecnt * channels / 8;
169 170 171
	if (data_size > playable_size)
		data_size = playable_size;

172 173
	const size_t block_size = channels * DSF_BLOCK_SIZE;
	metadata->n_blocks = data_size / block_size;
174
	metadata->channels = channels;
175
	metadata->sample_rate = samplefreq;
176
#ifdef ENABLE_ID3TAG
177
	metadata->id3_offset = metadata_offset;
178
#endif
179
	/* check bits per sample format, determine if bitreverse is needed */
180
	metadata->bitreverse = FromLE32(dsf_fmt_chunk.bitssample) == 1;
181 182 183 184 185 186 187 188 189 190
	return true;
}

static void
bit_reverse_buffer(uint8_t *p, uint8_t *end)
{
	for (; p < end; ++p)
		*p = bit_reverse(*p);
}

191 192 193 194 195 196 197
static void
InterleaveDsfBlockMono(uint8_t *gcc_restrict dest,
		       const uint8_t *gcc_restrict src)
{
	memcpy(dest, src, DSF_BLOCK_SIZE);
}

198 199 200 201 202 203 204
/**
 * DSF data is build up of alternating 4096 blocks of DSD samples for left and
 * right. Convert the buffer holding 1 block of 4096 DSD left samples and 1
 * block of 4096 DSD right samples to 8k of samples in normal PCM left/right
 * order.
 */
static void
205 206
InterleaveDsfBlockStereo(uint8_t *gcc_restrict dest,
			 const uint8_t *gcc_restrict src)
207
{
208 209 210
	for (size_t i = 0; i < DSF_BLOCK_SIZE; ++i) {
		dest[2 * i] = src[i];
		dest[2 * i + 1] = src[DSF_BLOCK_SIZE + i];
211 212 213
	}
}

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
static void
InterleaveDsfBlockChannel(uint8_t *gcc_restrict dest,
			  const uint8_t *gcc_restrict src,
			  unsigned channels)
{
	for (size_t i = 0; i < DSF_BLOCK_SIZE; ++i, dest += channels, ++src)
		*dest = *src;
}

static void
InterleaveDsfBlockGeneric(uint8_t *gcc_restrict dest,
			  const uint8_t *gcc_restrict src,
			  unsigned channels)
{
	for (unsigned c = 0; c < channels; ++c, ++dest, src += DSF_BLOCK_SIZE)
		InterleaveDsfBlockChannel(dest, src, channels);
}

static void
InterleaveDsfBlock(uint8_t *gcc_restrict dest, const uint8_t *gcc_restrict src,
		   unsigned channels)
{
	if (channels == 1)
		InterleaveDsfBlockMono(dest, src);
	else if (channels == 2)
		InterleaveDsfBlockStereo(dest, src);
	else
		InterleaveDsfBlockGeneric(dest, src, channels);
}

244
static offset_type
245
FrameToBlock(uint64_t frame)
246
{
247
	return frame / DSF_BLOCK_SIZE;
248 249
}

250 251 252 253
/**
 * Decode one complete DSF 'data' chunk i.e. a complete song
 */
static bool
254
dsf_decode_chunk(DecoderClient &client, InputStream &is,
255
		 unsigned channels, unsigned sample_rate,
256
		 offset_type n_blocks,
Max Kellermann's avatar
Max Kellermann committed
257
		 bool bitreverse)
258
{
259
	const size_t block_size = channels * DSF_BLOCK_SIZE;
260
	const offset_type start_offset = is.GetOffset();
261

262
	auto cmd = client.GetCommand();
263
	for (offset_type i = 0; i < n_blocks && cmd != DecoderCommand::STOP;) {
264
		if (cmd == DecoderCommand::SEEK) {
265
			uint64_t frame = client.GetSeekFrame();
266
			offset_type block = FrameToBlock(frame);
267
			if (block >= n_blocks) {
268
				client.CommandFinished();
269 270 271 272 273
				break;
			}

			offset_type offset =
				start_offset + block * block_size;
274
			if (dsdlib_skip_to(&client, is, offset)) {
275
				client.CommandFinished();
276 277
				i = block;
			} else
278
				client.SeekError();
279 280
		}

281 282
		/* worst-case buffer size */
		uint8_t buffer[MAX_CHANNELS * DSF_BLOCK_SIZE];
283
		if (!decoder_read_full(&client, is, buffer, block_size))
284 285 286
			return false;

		if (bitreverse)
287
			bit_reverse_buffer(buffer, buffer + block_size);
288

289 290
		uint8_t interleaved_buffer[MAX_CHANNELS * DSF_BLOCK_SIZE];
		InterleaveDsfBlock(interleaved_buffer, buffer, channels);
291

292 293 294
		cmd = client.SubmitData(is,
					interleaved_buffer, block_size,
					sample_rate / 1000);
295
		++i;
296
	}
297

298
	return true;
299 300 301
}

static void
302
dsf_stream_decode(DecoderClient &client, InputStream &is)
303 304
{
	/* check if it is a proper DSF file */
305
	DsfMetaData metadata;
306
	if (!dsf_read_metadata(&client, is, &metadata))
307 308
		return;

309 310 311 312
	auto audio_format = CheckAudioFormat(metadata.sample_rate / 8,
					     SampleFormat::DSD,
					     metadata.channels);

313
	/* Calculate song time from DSD chunk size and sample frequency */
314
	const auto n_blocks = metadata.n_blocks;
315 316
	auto songtime = SongTime::FromScale<uint64_t>(n_blocks * DSF_BLOCK_SIZE,
						      audio_format.sample_rate);
317 318

	/* success: file was recognized */
319
	client.Ready(audio_format, is.IsSeekable(), songtime);
320

321
	dsf_decode_chunk(client, is, metadata.channels,
322 323 324
			 metadata.sample_rate,
			 n_blocks,
			 metadata.bitreverse);
325 326 327
}

static bool
328
dsf_scan_stream(InputStream &is,
329
		gcc_unused const TagHandler &handler,
330
		gcc_unused void *handler_ctx)
331 332
{
	/* check DSF metadata */
333
	DsfMetaData metadata;
334
	if (!dsf_read_metadata(nullptr, is, &metadata))
335 336
		return false;

337 338 339
	auto audio_format = CheckAudioFormat(metadata.sample_rate / 8,
					     SampleFormat::DSD,
					     metadata.channels);
340

341
	/* calculate song time and add as tag */
342 343 344
	const auto n_blocks = metadata.n_blocks;
	auto songtime = SongTime::FromScale<uint64_t>(n_blocks * DSF_BLOCK_SIZE,
						      audio_format.sample_rate);
345 346
	tag_handler_invoke_duration(handler, handler_ctx, songtime);

347
#ifdef ENABLE_ID3TAG
348 349 350
	/* Add available tags from the ID3 tag */
	dsdlib_tag_id3(is, handler, handler_ctx, metadata.id3_offset);
#endif
351 352 353 354 355
	return true;
}

static const char *const dsf_suffixes[] = {
	"dsf",
356
	nullptr
357 358 359 360
};

static const char *const dsf_mime_types[] = {
	"application/x-dsf",
361
	nullptr
362 363
};

364
const struct DecoderPlugin dsf_decoder_plugin = {
365 366 367 368 369 370 371 372 373 374
	"dsf",
	nullptr,
	nullptr,
	dsf_stream_decode,
	nullptr,
	nullptr,
	dsf_scan_stream,
	nullptr,
	dsf_suffixes,
	dsf_mime_types,
375
};