Skip to content

Commit

Permalink
Add support for reading DSDIFF files
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mansr committed Aug 25, 2015
1 parent 393f021 commit bf94320
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 1 deletion.
1 change: 1 addition & 0 deletions msvc10/LibSoX.vcxproj
Expand Up @@ -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>
Expand Down
3 changes: 3 additions & 0 deletions msvc10/LibSoX.vcxproj.filters
Expand Up @@ -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>
Expand Down
5 changes: 5 additions & 0 deletions soxformat.7
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/Makefile.am
Expand Up @@ -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@
Expand Down
209 changes: 209 additions & 0 deletions 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;
}
1 change: 1 addition & 0 deletions src/formats.h
Expand Up @@ -26,6 +26,7 @@
FORMAT(cvsd)
FORMAT(cvu)
FORMAT(dat)
FORMAT(dsdiff)
FORMAT(dsf)
FORMAT(dvms)
FORMAT(f4)
Expand Down

0 comments on commit bf94320

Please sign in to comment.