Attached is version 3 of the patch which adds support for
DSF files to the DSDIFF decoder. It can be used in
conjunction with DSD2PCM and DSD-over-USB. 
This version should address the concerns with v2.

Changes from v2 patch:
- Adhered to max 80 colom width
- Fixed indents (hopefully correctly)
- Removed usage of splitting 32-bit values in two 16-bit values
- bitreverse is now a bool
- Renamed make_dff_format function, added more comments explaining it

I took a stab a changing the loop part. Please check, it probably needs
some more work.

>       while (true) {
[...]
> +             if (!metadata.fileisdff) {

The current code allows for more then one DSD chunk in a DFF file. From
reading the DSDIFF standard I cannot determine if this is even
supported. I think the loop could be removed without affecting
the ability to play DSDIFF files.

Jurgen

diff --git a/doc/user.xml b/doc/user.xml
index cd36528..46d9c11 100644
--- a/doc/user.xml
+++ b/doc/user.xml
@@ -782,7 +782,7 @@ systemctl start mpd.socket</programlisting>
         <title><varname>dsdiff</varname></title>
 
         <para>
-          Decodes DFF files containing DSDIFF data (e.g. SACD rips).
+          Decodes DFF and DSF files containing DSDIFF data (e.g. SACD rips).
         </para>
 
         <informaltable>
diff --git a/src/decoder/dsdiff_decoder_plugin.c b/src/decoder/dsdiff_decoder_plugin.c
index ae42002..4fb9b23 100644
--- a/src/decoder/dsdiff_decoder_plugin.c
+++ b/src/decoder/dsdiff_decoder_plugin.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2011 The Music Player Daemon Project
+ * Copyright (C) 2003-2012 The Music Player Daemon Project
  * http://www.musicpd.org
  *
  * This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,12 @@
 
 /* \file
  *
- * This plugin decodes DSDIFF data (SACD) embedded in DFF files.  It
- * was modeled after the specification found here:
+ * This plugin decodes DSDIFF data (SACD) embedded in DFF and DSF files.
+ * The DFF code was modeled after the specification found here:
  * http://www.sonicstudio.com/pdf/dsd/DSDIFF_1.5_Spec.pdf
+ *
+ * The DSF code was created using the specification found here:
+ * http://dsd-guide.com/sonys-dsf-file-format-spec
  */
 
 #include "config.h"
@@ -53,10 +56,46 @@ struct dsdiff_chunk_header {
 
 struct dsdiff_metadata {
 	unsigned sample_rate, channels;
+	bool fileisdff;
+	bool bitreverse;
+	uint64_t chunk_size;
 };
 
 static bool lsbitfirst;
 
+struct dsdiff_header_dsf {
+	struct dsdiff_id id;		/* "DSD " */
+	uint32_t size_low, size_high;	/* DSD chunk size, including id = 28 */
+	uint32_t fsize_low, fsize_high;	/* Total file size */
+	uint32_t pmeta_low, pmeta_high;	/* Pointer to id3v2 metadata, should be
+					   at the end of the file */
+};
+
+struct dsdiff_dsf_fmt_chunk {		/* -- DSF file fmt chunk -- */			
+
+	struct dsdiff_id id;		/* "fmt " */
+	uint32_t size_low, size_high;	/* fmt chunk size, including id, 
+					   normally 52 */
+
+	uint32_t version;		/* Version of this format = 1 */
+	uint32_t formatid;		/* 0: DSD raw */
+	uint32_t channeltype;		/* Channel Type, 1 = mono, 2 = stereo, 
+					   3 = 3 channels, etc */
+	uint32_t channelnum;		/* Channel number, 1 = mono, 2 = stereo,
+					   ... 6 = 6 channels */
+	uint32_t sample_freq;		/* Sample frequency: 2822400, 5644800 */
+	uint32_t bitssample;		/* Bits per sample 1 or 8 */
+	uint32_t scnt_low, scnt_high;	/* Sample count per channel in bytes */
+	uint32_t block_size;		/* Block size per channel = 4096 */
+	uint32_t reserved;		/* Reserved, should be all zero */
+};
+
+struct dsdiff_dsf_data_chunk {
+	struct dsdiff_id id;
+	uint32_t size_low, size_high;	/* 'data' chunk size, includes header
+					   (id+size) */
+};
+
 static bool
 dsdiff_init(const struct config_param *param)
 {
@@ -224,7 +263,7 @@ dsdiff_read_prop_snd(struct decoder *decoder, struct input_stream *is,
 				return false;
 
 			if (!dsdiff_id_equals(&type, "DSD "))
-				/* only uincompressed DSD audio data
+				/* only uncompressed DSD audio data
 				   is implemented */
 				return false;
 		} else {
@@ -274,28 +313,111 @@ dsdiff_read_metadata(struct decoder *decoder, struct input_stream *is,
 	struct dsdiff_header header;
 	if (!dsdiff_read(decoder, is, &header, sizeof(header)) ||
 	    !dsdiff_id_equals(&header.id, "FRM8") ||
-	    !dsdiff_id_equals(&header.format, "DSD "))
+	    !dsdiff_id_equals(&header.format, "DSD ")) {
+
+	/* It's not a DFF file, check if it is a DSF file */
+	if (!dsdiff_skip_to(decoder, is, 0))	/* Reset to beginning 
+						   of the stream */
+		return false;
+
+	uint64_t chunk_size;
+	struct dsdiff_header_dsf dsfheader;
+	if (!dsdiff_read(decoder, is, &dsfheader, sizeof(dsfheader)) ||
+			 !dsdiff_id_equals(&header.id, "DSD "))
 		return false;
 
-	while (true) {
-		if (!dsdiff_read_chunk_header(decoder, is, chunk_header))
+	chunk_size =
+		(((uint64_t)GUINT32_FROM_LE(dsfheader.size_high)) << 32) |
+		 ((uint64_t)GUINT32_FROM_LE(dsfheader.size_low));
+
+	if (sizeof(dsfheader) != chunk_size)
+		return false;
+
+	/* Read the 'fmt ' chunk of the DSF file */
+	struct dsdiff_dsf_fmt_chunk dsf_fmt_chunk;
+	if (!dsdiff_read(decoder, is, &dsf_fmt_chunk, sizeof(dsf_fmt_chunk)) ||
+		    	 !dsdiff_id_equals(&dsf_fmt_chunk.id, "fmt "))
 			return false;
 
-		if (dsdiff_id_equals(&chunk_header->id, "PROP")) {
-			if (!dsdiff_read_prop(decoder, is, metadata,
-					      chunk_header))
-				return false;
-		} else if (dsdiff_id_equals(&chunk_header->id, "DSD ")) {
-			/* done with metadata */
-			return true;
-		} else {
-			/* ignore unknown chunk */
+	uint64_t fmt_chunk_size =
+		(((uint64_t)GUINT32_FROM_LE(dsf_fmt_chunk.size_high)) << 32) |
+		 ((uint64_t)GUINT32_FROM_LE(dsf_fmt_chunk.size_low));
 
-			uint64_t chunk_size = dsdiff_chunk_size(chunk_header);
-			goffset chunk_end_offset = is->offset + chunk_size;
+	if (fmt_chunk_size != sizeof(dsf_fmt_chunk))
+		return false;
 
-			if (!dsdiff_skip_to(decoder, is, chunk_end_offset))
+	uint32_t samplefreq =
+		(uint32_t)GUINT32_FROM_LE(dsf_fmt_chunk.sample_freq);
+
+	/* For now, only support version 1 of the standard, DSD raw stereo 
+	   files with a sample freq of 2822400 Hz */
+
+	if (dsf_fmt_chunk.version != 1 || dsf_fmt_chunk.formatid != 0 		
+	    || dsf_fmt_chunk.channeltype != 2
+	    || dsf_fmt_chunk.channelnum != 2
+	    || samplefreq != 2822400 )
+		return false;
+
+	uint32_t chblksize =
+		(uint32_t)GUINT32_FROM_LE(dsf_fmt_chunk.block_size);
+
+	/* According to Sony spec block size should always be 4096 */
+	if (chblksize != 4096)
+		return false;
+
+	/* Read the 'data' chunk of the DSF file */
+	struct dsdiff_dsf_data_chunk data_chunk;
+	if (!dsdiff_read(decoder, is, &data_chunk, sizeof(data_chunk)) ||
+			 !dsdiff_id_equals(&data_chunk.id, "data"))
+		return false;
+
+	/* Data size of DSF files are padded to multiple of 4096,
+	   we use the actual data size as chunk size */
+
+	uint64_t data_size =
+		(((uint64_t)GUINT32_FROM_LE(data_chunk.size_high)) << 32) |
+		 ((uint64_t)GUINT32_FROM_LE(data_chunk.size_low));
+	data_size -= sizeof(data_chunk);
+
+	metadata->chunk_size = data_size;
+
+	metadata->channels = (unsigned) dsf_fmt_chunk.channelnum;
+	metadata->sample_rate = samplefreq;
+
+	/* Check bits per sample format, determine if bitreverse is needed */
+	metadata->bitreverse = dsf_fmt_chunk.bitssample == 1 ?  true : false;
+	metadata->fileisdff = false;
+	return true;
+
+	} else {
+
+		/* Continue processing of a DFF format file */
+		while (true) {
+			if (!dsdiff_read_chunk_header(decoder, is,
+						      chunk_header))
 				return false;
+	
+			if (dsdiff_id_equals(&chunk_header->id, "PROP")) {
+				if (!dsdiff_read_prop(decoder, is, metadata,
+						      chunk_header))
+					return false;
+			} else if (dsdiff_id_equals(&chunk_header->id,
+						    "DSD ")) {
+				/* done with metadata */
+				metadata->fileisdff = true;	/* mark as DFF
+								   file */
+				return true;
+			} else {
+				/* ignore unknown chunk */
+				uint64_t chunk_size =
+					dsdiff_chunk_size(chunk_header);
+				goffset chunk_end_offset = is->offset
+							   + chunk_size;
+	
+				if (!dsdiff_skip_to(decoder, is,
+						    chunk_end_offset))
+					return false;
+			}
 		}
 	}
 }
@@ -308,14 +430,50 @@ bit_reverse_buffer(uint8_t *p, uint8_t *end)
 }
 
 /**
+ * DSF data is build up of alternating 4096 blocks of DSD samples for left and
+ * right data. 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
+dsf_to_pcm_order(uint8_t *p, uint8_t *q, size_t nrbytes)
+{
+	unsigned j=0, i=0;
+
+	for (i=0 ; i < (unsigned) nrbytes; i += 2) {
+		q[i] = *(p+j);
+		j++;
+	}
+
+	j=0;
+	for (i=1; i < (unsigned) nrbytes ; i += 2) {
+		q[i] = *(p+4096+j);
+		j++;
+	}
+
+	for (i=0 ; i < (unsigned) nrbytes; i++) {
+		*p = q[i];
+		p++;
+	}
+
+}
+
+/**
  * Decode one "DSD" chunk.
  */
 static bool
 dsdiff_decode_chunk(struct decoder *decoder, struct input_stream *is,
 		    unsigned channels,
-		    uint64_t chunk_size)
+		    uint64_t chunk_size,
+		    bool fileisdff,
+		    bool bitreverse)
 {
 	uint8_t buffer[8192];
+
+	/* Scratch buffer for DSF samples to convert to the needed 
+	   normal Left/Right regime of samples */
+	uint8_t dsf_scratch_buffer[8192];
+
 	const size_t sample_size = sizeof(buffer[0]);
 	const size_t frame_size = channels * sample_size;
 	const unsigned buffer_frames = sizeof(buffer) / frame_size;
@@ -338,9 +496,12 @@ dsdiff_decode_chunk(struct decoder *decoder, struct input_stream *is,
 
 		chunk_size -= nbytes;
 
-		if (lsbitfirst)
+		if (lsbitfirst || bitreverse)
 			bit_reverse_buffer(buffer, buffer + nbytes);
 
+		if (!fileisdff)
+			dsf_to_pcm_order(buffer, dsf_scratch_buffer, nbytes);
+
 		enum decoder_command cmd =
 			decoder_data(decoder, is, buffer, nbytes, 0);
 		switch (cmd) {
@@ -384,32 +545,49 @@ dsdiff_stream_decode(struct decoder *decoder, struct input_stream *is)
 	}
 
 	/* success: file was recognized */
-
 	decoder_initialized(decoder, &audio_format, false, -1);
 
-	/* every iteration of the following loop decodes one "DSD"
-	   chunk */
-
-	while (true) {
-		uint64_t chunk_size = dsdiff_chunk_size(&chunk_header);
-
-		if (dsdiff_id_equals(&chunk_header.id, "DSD ")) {
-			if (!dsdiff_decode_chunk(decoder, is,
-						 metadata.channels,
-						 chunk_size))
-				break;
-		} else {
-			/* ignore other chunks */
-
-			if (!dsdiff_skip(decoder, is, chunk_size))
+	if (!metadata.fileisdff) {
+		uint64_t chunk_size = metadata.chunk_size;
+		if (!dsdiff_decode_chunk(decoder, is, 
+					 metadata.channels,
+					 chunk_size, 
+					 metadata.fileisdff,
+					 metadata.bitreverse))
+			return;
+			
+	} else {
+
+		/* every iteration of the following loop decodes one "DSD"
+		   chunk from a DFF file */
+
+		while (true) {
+
+			uint64_t chunk_size = dsdiff_chunk_size(&chunk_header);
+
+			if (dsdiff_id_equals(&chunk_header.id, "DSD ")) {
+				if (!dsdiff_decode_chunk(decoder, is,
+							 metadata.channels,
+							 chunk_size,
+							 metadata.fileisdff,
+						/* Set bitreverse to 
+						   false for DFF files */
+							false))
+					break;
+			} else {
+				/* ignore other chunks */
+
+				if (!dsdiff_skip(decoder, is, chunk_size))
+					break;
+			}
+	
+			/* read next chunk header; the first one was read by
+			   dsdiff_read_metadata() */
+	
+			if (!dsdiff_read_chunk_header(decoder,
+						      is, &chunk_header))
 				break;
 		}
-
-		/* read next chunk header; the first one was read by
-		   dsdiff_read_metadata() */
-
-		if (!dsdiff_read_chunk_header(decoder, is, &chunk_header))
-			break;
 	}
 }
 
@@ -440,11 +618,13 @@ dsdiff_scan_stream(struct input_stream *is,
 
 static const char *const dsdiff_suffixes[] = {
 	"dff",
+	"dsf",
 	NULL
 };
 
 static const char *const dsdiff_mime_types[] = {
 	"application/x-dff",
+	"application/x-dsf",
 	NULL
 };
 
-- 
1.7.6.5

------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
Musicpd-dev-team mailing list
Musicpd-dev-team@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/musicpd-dev-team

Reply via email to