DsfDecoderPlugin.cxx 10.2 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "util/Error.hxx"
37
#include "system/ByteOrder.hxx"
38
#include "DsdLib.hxx"
39
#include "tag/TagHandler.hxx"
40
#include "Log.hxx"
41

42 43
#include <string.h>

44
static constexpr unsigned DSF_BLOCK_SIZE = 4096;
45
static constexpr unsigned DSF_BLOCK_BITS = DSF_BLOCK_SIZE * 8;
46

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

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

67
/** DSF file fmt chunk */
68
struct DsfFmtChunk {
69
	/** id: "fmt " */
70
	DsdId id;
71
	/** fmt chunk size, including id, normally 52 */
72
	DsdUint64 size;
73 74 75 76 77 78 79 80 81 82 83 84 85
	/** 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 */
86
	DsdUint64 scnt;
87 88 89 90 91 92
	/** block size per channel = 4096 */
	uint32_t block_size;
	/** reserved, should be all zero */
	uint32_t reserved;
};

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

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

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

115
#ifdef HAVE_ID3TAG
116
	const offset_type metadata_offset = dsf_header.pmeta.Read();
117 118
#endif

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

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

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

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

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

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

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

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

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

160 161
	data_size -= sizeof(data_chunk);

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

166 167 168 169
	/* 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();
170
	const offset_type playable_size = samplecnt * channels / 8;
171 172 173
	if (data_size > playable_size)
		data_size = playable_size;

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

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

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

200 201 202 203 204 205 206
/**
 * 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
207 208
InterleaveDsfBlockStereo(uint8_t *gcc_restrict dest,
			 const uint8_t *gcc_restrict src)
209
{
210 211 212
	for (size_t i = 0; i < DSF_BLOCK_SIZE; ++i) {
		dest[2 * i] = src[i];
		dest[2 * i + 1] = src[DSF_BLOCK_SIZE + i];
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 244 245
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);
}

246 247 248 249 250 251
static offset_type
TimeToBlock(double t, unsigned sample_rate)
{
	return offset_type(t * sample_rate / DSF_BLOCK_BITS);
}

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

264 265
	auto cmd = decoder_get_command(decoder);
	for (offset_type i = 0; i < n_blocks && cmd != DecoderCommand::STOP;) {
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
		if (cmd == DecoderCommand::SEEK) {
			double t = decoder_seek_where(decoder);
			offset_type block = TimeToBlock(t, sample_rate);
			if (block >= n_blocks) {
				decoder_command_finished(decoder);
				break;
			}

			offset_type offset =
				start_offset + block * block_size;
			if (dsdlib_skip_to(&decoder, is, offset)) {
				decoder_command_finished(decoder);
				i = block;
			} else
				decoder_seek_error(decoder);
		}

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

		if (bitreverse)
289
			bit_reverse_buffer(buffer, buffer + block_size);
290

291 292
		uint8_t interleaved_buffer[MAX_CHANNELS * DSF_BLOCK_SIZE];
		InterleaveDsfBlock(interleaved_buffer, buffer, channels);
293

294 295 296 297
		cmd = decoder_data(decoder, is,
				   interleaved_buffer, block_size,
				   sample_rate / 1000);
		++i;
298
	}
299

300
	return true;
301 302 303
}

static void
304
dsf_stream_decode(Decoder &decoder, InputStream &is)
305 306
{
	/* check if it is a proper DSF file */
307
	DsfMetaData metadata;
308
	if (!dsf_read_metadata(&decoder, is, &metadata))
309 310
		return;

311
	Error error;
312 313 314
	AudioFormat audio_format;
	if (!audio_format_init_checked(audio_format, metadata.sample_rate / 8,
				       SampleFormat::DSD,
315
				       metadata.channels, error)) {
316
		LogError(error);
317 318
		return;
	}
319
	/* Calculate song time from DSD chunk size and sample frequency */
320 321
	const auto n_blocks = metadata.n_blocks;
	float songtime = float(n_blocks * DSF_BLOCK_BITS) /
322
			 (float) metadata.sample_rate;
323 324

	/* success: file was recognized */
325
	decoder_initialized(decoder, audio_format, is.IsSeekable(), songtime);
326

327 328 329 330
	dsf_decode_chunk(decoder, is, metadata.channels,
			 metadata.sample_rate,
			 n_blocks,
			 metadata.bitreverse);
331 332 333
}

static bool
334
dsf_scan_stream(InputStream &is,
335 336
		gcc_unused const struct tag_handler *handler,
		gcc_unused void *handler_ctx)
337 338
{
	/* check DSF metadata */
339
	DsfMetaData metadata;
340
	if (!dsf_read_metadata(nullptr, is, &metadata))
341 342
		return false;

343 344 345
	AudioFormat audio_format;
	if (!audio_format_init_checked(audio_format, metadata.sample_rate / 8,
				       SampleFormat::DSD,
346
				       metadata.channels, IgnoreError()))
347 348 349
		/* refuse to parse files which we cannot play anyway */
		return false;

350
	/* calculate song time and add as tag */
351 352
	unsigned songtime = (metadata.n_blocks * DSF_BLOCK_BITS) /
		metadata.sample_rate;
353 354
	tag_handler_invoke_duration(handler, handler_ctx, songtime);

355 356 357 358
#ifdef HAVE_ID3TAG
	/* Add available tags from the ID3 tag */
	dsdlib_tag_id3(is, handler, handler_ctx, metadata.id3_offset);
#endif
359 360 361 362 363
	return true;
}

static const char *const dsf_suffixes[] = {
	"dsf",
364
	nullptr
365 366 367 368
};

static const char *const dsf_mime_types[] = {
	"application/x-dsf",
369
	nullptr
370 371
};

372
const struct DecoderPlugin dsf_decoder_plugin = {
373 374 375 376 377 378 379 380 381 382
	"dsf",
	nullptr,
	nullptr,
	dsf_stream_decode,
	nullptr,
	nullptr,
	dsf_scan_stream,
	nullptr,
	dsf_suffixes,
	dsf_mime_types,
383
};