sox-devel@lists.sourceforge.net unofficial mirror
 help / color / mirror / code / Atom feed
From: Mans Rullgard <mans@mansr.com>
To: sox-devel@lists.sourceforge.net
Subject: [PATCH 3/6] Add support for reading DSDIFF files
Date: Wed, 16 Sep 2015 15:16:27 +0100	[thread overview]
Message-ID: <1442412990-20764-3-git-send-email-mans@mansr.com> (raw)
In-Reply-To: <1442412990-20764-1-git-send-email-mans@mansr.com>

This adds support for reading DSDIFF files containing uncompressed
DSD data.  1-bit samples are unpacked to sox_sample_t values of
maximum amplitude.  Optional file elements are ignored.
---
 msvc10/LibSoX.vcxproj         |   1 +
 msvc10/LibSoX.vcxproj.filters |   3 +
 soxformat.7                   |   5 +
 src/Makefile.am               |   3 +-
 src/dsdiff.c                  | 209 ++++++++++++++++++++++++++++++++++++++++++
 src/formats.h                 |   1 +
 6 files changed, 221 insertions(+), 1 deletion(-)
 create mode 100644 src/dsdiff.c

diff --git a/msvc10/LibSoX.vcxproj b/msvc10/LibSoX.vcxproj
index e257831..c38a764 100644
--- a/msvc10/LibSoX.vcxproj
+++ b/msvc10/LibSoX.vcxproj
@@ -144,6 +144,7 @@
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
     </ClCompile>
+    <ClCompile Include="..\src\dsdiff.c" />
     <ClCompile Include="..\src\dsf.c" />
     <ClCompile Include="..\src\example0.c">
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
diff --git a/msvc10/LibSoX.vcxproj.filters b/msvc10/LibSoX.vcxproj.filters
index 6cc951c..bed7cb2 100644
--- a/msvc10/LibSoX.vcxproj.filters
+++ b/msvc10/LibSoX.vcxproj.filters
@@ -597,6 +597,9 @@
     <ClCompile Include="..\src\downsample.c">
       <Filter>Effect Sources</Filter>
     </ClCompile>
+    <ClCompile Include="..\src\dsdiff.c">
+      <Filter>Format Sources</Filter>
+    </ClCompile>
     <ClCompile Include="..\src\dsf.c">
       <Filter>Format Sources</Filter>
     </ClCompile>
diff --git a/soxformat.7 b/soxformat.7
index 685b27e..645910e 100644
--- a/soxformat.7
+++ b/soxformat.7
@@ -304,6 +304,11 @@ Example containing only 2 stereo samples of silence:
     0.00012481278	0	0
 .EE
 .TP
+.B .dff
+Direct Stream Digital Interchange File Format (DSDIFF). Format defined
+by Philips for storing 1-bit DSD data.  Used in SACD mastering and
+occasionally for online distribution.
+.TP
 .B .dsf
 DSD Stream File.  Format defined by Sony for storing 1-bit DSD data.
 Commonly used for online distribution of audiophile recordings.
diff --git a/src/Makefile.am b/src/Makefile.am
index 462d46d..e047580 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -121,7 +121,8 @@ libsox_la_SOURCES += raw-fmt.c s1-fmt.c s2-fmt.c s3-fmt.c \
   lu-fmt.c 8svx.c aiff-fmt.c aifc-fmt.c au.c avr.c cdr.c cvsd-fmt.c \
   dvms-fmt.c dat.c hcom.c htk.c maud.c prc.c sf.c smp.c \
   sounder.c soundtool.c sphere.c tx16w.c voc.c vox-fmt.c ima-fmt.c adpcm.c adpcm.h \
-  ima_rw.c ima_rw.h wav.c wve.c xa.c nulfile.c f4-fmt.c f8-fmt.c gsrt.c dsf.c
+  ima_rw.c ima_rw.h wav.c wve.c xa.c nulfile.c f4-fmt.c f8-fmt.c gsrt.c dsf.c \
+  dsdiff.c
 
 libsox_la_LIBADD += @GSM_LIBS@ @LIBGSM_LIBADD@
 libsox_la_LIBADD += @LPC10_LIBS@ @LIBLPC10_LIBADD@
diff --git a/src/dsdiff.c b/src/dsdiff.c
new file mode 100644
index 0000000..8d3ccc3
--- /dev/null
+++ b/src/dsdiff.c
@@ -0,0 +1,209 @@
+/* DSDIFF file support
+ *
+ * Copyright (c) 2015 Mans Rullgard <mans@mansr.com>
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
+ *
+ * This library 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 Lesser
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/* File format specification available at
+ * http://dsd-guide.com/sites/default/files/white-papers/DSDIFF_1.5_Spec.pdf
+ */
+
+#include <stdint.h>
+#include "sox_i.h"
+
+struct dsdiff {
+	uint32_t sample_rate;
+	uint16_t num_channels;
+	uint64_t data_size;
+	uint8_t *buf;
+};
+
+#define ID(a, b, c, d) ((a) << 24 | (b) << 16 | (c) << 8 | (d))
+
+static int dff_startread(sox_format_t *ft)
+{
+	struct dsdiff *dff = ft->priv;
+	uint32_t ckid;
+	uint32_t cktype;
+	uint64_t cksize;
+	uint64_t f8size;
+	uint32_t fver;
+	uint64_t spos, epos;
+
+	if (lsx_readdw(ft, &ckid) || ckid != ID('F', 'R', 'M', '8')) {
+		lsx_fail_errno(ft, SOX_EHDR, "FRM8 tag not found");
+		return SOX_EHDR;
+	}
+
+	if (lsx_readqw(ft, &f8size)) {
+		lsx_fail_errno(ft, SOX_EHDR, "error reading chunk size");
+		return SOX_EHDR;
+	}
+
+	if (lsx_readdw(ft, &cktype) || cktype != ID('D', 'S', 'D', ' ')) {
+		lsx_fail_errno(ft, SOX_EHDR, "DSD tag not found");
+		return SOX_EHDR;
+	}
+
+	do {
+		if (lsx_readdw(ft, &ckid) || lsx_readqw(ft, &cksize)) {
+			lsx_fail_errno(ft, SOX_EHDR, "read error");
+			return SOX_EHDR;
+		}
+
+		spos = lsx_tell(ft);
+
+		switch (ckid) {
+		case ID('F', 'V', 'E', 'R'):
+			if (cksize != 4)
+				return SOX_EHDR;
+			if (lsx_readdw(ft, &fver))
+				return SOX_EHDR;
+			if (fver >> 24 != 1) {
+				lsx_fail_errno(ft, SOX_EHDR, "unknown version");
+				return SOX_EHDR;
+			}
+			break;
+
+		case ID('P', 'R', 'O', 'P'):
+			if (cksize < 4)
+				return SOX_EHDR;
+			if (lsx_readdw(ft, &cktype))
+				return SOX_EHDR;
+			if (cktype == ID('S', 'N', 'D', ' '))
+				cksize = 4;
+			break;
+
+		case ID('F', 'S', ' ', ' '):
+			if (cksize < 4)
+				return SOX_EHDR;
+			if (lsx_readdw(ft, &dff->sample_rate))
+				return SOX_EHDR;
+			break;
+
+		case ID('C', 'H', 'N', 'L'):
+			if (cksize < 4)
+				return SOX_EHDR;
+			if (lsx_readw(ft, &dff->num_channels))
+				return SOX_EHDR;
+			break;
+
+		case ID('C', 'M', 'P', 'R'):
+			if (cksize < 4)
+				return SOX_EHDR;
+			if (lsx_readdw(ft, &cktype))
+				return SOX_EHDR;
+			if (cktype != ID('D', 'S', 'D', ' ')) {
+				lsx_fail_errno(ft, SOX_EHDR,
+					       "unsupported compression");
+				return SOX_EHDR;
+			}
+			break;
+
+		case ID('D', 'S', 'D', ' '):
+			if (cksize < 8)
+				return SOX_EHDR;
+			dff->data_size = cksize;
+			cksize = 0;
+			break;
+		}
+
+		cksize += cksize & 1;
+		epos = lsx_tell(ft);
+		if (epos != spos + cksize)
+			lsx_seeki(ft, (off_t)(spos + cksize - epos), SEEK_CUR);
+	} while (cksize && epos < f8size);
+
+	if (!dff->sample_rate || !dff->num_channels || !dff->data_size) {
+		lsx_fail_errno(ft, SOX_EHDR, "invalid file header");
+		return SOX_EHDR;
+	}
+
+	if (ckid != ID('D', 'S', 'D', ' ')) {
+		lsx_fail_errno(ft, SOX_EHDR, "unsupported data type");
+		return SOX_EHDR;
+	}
+
+	dff->buf = lsx_malloc((size_t)dff->num_channels);
+	if (!dff->buf)
+		return SOX_ENOMEM;
+
+	ft->signal.rate = dff->sample_rate;
+	ft->signal.channels = dff->num_channels;
+	ft->signal.precision = 1;
+	ft->signal.length = dff->data_size * 8;
+
+	ft->encoding.encoding = SOX_ENCODING_DSD;
+	ft->encoding.bits_per_sample = 1;
+
+	return SOX_SUCCESS;
+}
+
+static size_t dff_read(sox_format_t *ft, sox_sample_t *buf, size_t len)
+{
+	struct dsdiff *dff = ft->priv;
+	size_t nc = dff->num_channels;
+	size_t rsamp = 0;
+	unsigned i, j;
+
+	len /= nc;
+
+	while (len >= 8) {
+		if (lsx_read_b_buf(ft, dff->buf, nc) < nc)
+			return rsamp * nc;
+
+		for (i = 0; i < nc; i++) {
+			unsigned d = dff->buf[i];
+
+			for (j = 0; j < 8; j++) {
+				buf[i + j * nc] = d & 128 ?
+					SOX_SAMPLE_MAX : -SOX_SAMPLE_MAX;
+				d <<= 1;
+			}
+		}
+
+		buf += 8 * nc;
+		rsamp += 8;
+		len -= 8;
+	}
+
+	return rsamp * nc;
+}
+
+static int dff_stopread(sox_format_t *ft)
+{
+	struct dsdiff *dff = ft->priv;
+
+	free(dff->buf);
+
+	return SOX_SUCCESS;
+}
+
+LSX_FORMAT_HANDLER(dsdiff)
+{
+	static char const * const names[] = { "dff", NULL };
+	static unsigned const write_encodings[] = { 0 };
+	static sox_format_handler_t const handler = {
+		SOX_LIB_VERSION_CODE,
+		"Direct Stream Digital Interchange File Format (DSDIFF)",
+		names, SOX_FILE_BIG_END,
+		dff_startread, dff_read, dff_stopread,
+		NULL, NULL, NULL,
+		NULL, write_encodings, NULL,
+		sizeof(struct dsdiff)
+	};
+	return &handler;
+}
diff --git a/src/formats.h b/src/formats.h
index 4701efd..eb33577 100644
--- a/src/formats.h
+++ b/src/formats.h
@@ -26,6 +26,7 @@
   FORMAT(cvsd)
   FORMAT(cvu)
   FORMAT(dat)
+  FORMAT(dsdiff)
   FORMAT(dsf)
   FORMAT(dvms)
   FORMAT(f4)
-- 
2.5.2


------------------------------------------------------------------------------
Monitor Your Dynamic Infrastructure at Any Scale With Datadog!
Get real-time metrics from all of your servers, apps and tools
in one place.
SourceForge users - Click here to start your Free Trial of Datadog now!
http://pubads.g.doubleclick.net/gampad/clk?id=241902991&iu=/4140

  parent reply	other threads:[~2015-09-16 14:17 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-16 14:16 [PATCH 1/6] Add SOX_ENCODING_DSD Mans Rullgard
2015-09-16 14:16 ` [PATCH 2/6] Add DSF file support Mans Rullgard
2015-09-16 14:16 ` Mans Rullgard [this message]
2015-09-16 14:16 ` [PATCH 4/6] Add macros for increasing data alignment Mans Rullgard
2015-12-20 12:41   ` Eric Wong
2015-12-20 13:54     ` Måns Rullgård
2015-12-21 10:55       ` Eric Wong
2015-12-21 11:37         ` Måns Rullgård
2015-12-21 18:33           ` Eric Wong
2015-12-21 18:37             ` Måns Rullgård
2015-09-16 14:16 ` [PATCH 5/6] Add a sigma-delta modulator for DSD encoding Mans Rullgard
2015-10-03 22:31   ` Eric Wong
2015-10-03 22:39     ` Måns Rullgård
2015-09-16 14:16 ` [PATCH 6/6] Add DSD over PCM (dop) effect Mans Rullgard
2015-12-19 12:09   ` Eric Wong
2015-12-19 12:14     ` Måns Rullgård
2015-12-20  5:04       ` Eric Wong
2015-12-20 13:44         ` Måns Rullgård

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.sourceforge.net/lists/listinfo/sox-devel

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1442412990-20764-3-git-send-email-mans@mansr.com \
    --to=sox-devel@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/sox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).