2011-10-03 12:52:15 +02:00
|
|
|
/*
|
2013-07-28 12:20:50 +02:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2011-10-03 12:52:15 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-10-07 06:38:23 +02:00
|
|
|
/* \file
|
|
|
|
*
|
2012-06-16 13:46:42 +02:00
|
|
|
* This plugin decodes DSDIFF data (SACD) embedded in DFF files.
|
2012-04-28 15:40:38 +02:00
|
|
|
* The DFF code was modeled after the specification found here:
|
2011-10-07 06:38:23 +02:00
|
|
|
* http://www.sonicstudio.com/pdf/dsd/DSDIFF_1.5_Spec.pdf
|
2012-04-28 15:40:38 +02:00
|
|
|
*
|
2012-06-16 13:46:42 +02:00
|
|
|
* All functions common to both DSD decoders have been moved to dsdlib
|
2011-10-07 06:38:23 +02:00
|
|
|
*/
|
|
|
|
|
2011-10-03 12:52:15 +02:00
|
|
|
#include "config.h"
|
2013-07-28 12:20:50 +02:00
|
|
|
#include "DsdiffDecoderPlugin.hxx"
|
2013-07-28 13:18:48 +02:00
|
|
|
#include "DecoderAPI.hxx"
|
2013-09-05 00:06:31 +02:00
|
|
|
#include "InputStream.hxx"
|
2013-07-29 07:50:08 +02:00
|
|
|
#include "CheckAudioFormat.hxx"
|
2012-03-21 09:01:56 +01:00
|
|
|
#include "util/bit_reverse.h"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-10-16 21:09:19 +02:00
|
|
|
#include "system/ByteOrder.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/TagHandler.hxx"
|
2013-07-28 12:20:50 +02:00
|
|
|
#include "DsdLib.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2011-10-03 12:52:15 +02:00
|
|
|
|
|
|
|
#include <unistd.h>
|
2011-10-09 17:39:11 +02:00
|
|
|
#include <stdio.h> /* for SEEK_SET, SEEK_CUR */
|
2011-10-03 12:52:15 +02:00
|
|
|
|
2013-07-28 12:20:50 +02:00
|
|
|
struct DsdiffHeader {
|
2013-10-28 23:12:48 +01:00
|
|
|
DsdId id;
|
2013-11-09 17:02:49 +01:00
|
|
|
DffDsdUint64 size;
|
2013-10-28 23:12:48 +01:00
|
|
|
DsdId format;
|
2011-10-03 12:52:15 +02:00
|
|
|
};
|
|
|
|
|
2013-07-28 12:20:50 +02:00
|
|
|
struct DsdiffChunkHeader {
|
2013-10-28 23:12:48 +01:00
|
|
|
DsdId id;
|
2013-11-09 17:02:49 +01:00
|
|
|
DffDsdUint64 size;
|
2013-07-28 12:20:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the "size" attribute from the specified header, converting it
|
|
|
|
* to the host byte order if needed.
|
|
|
|
*/
|
2013-10-28 23:29:23 +01:00
|
|
|
constexpr
|
2013-07-28 12:20:50 +02:00
|
|
|
uint64_t GetSize() const {
|
2013-10-28 23:29:23 +01:00
|
|
|
return size.Read();
|
2013-07-28 12:20:50 +02:00
|
|
|
}
|
2011-10-03 12:52:15 +02:00
|
|
|
};
|
|
|
|
|
2012-10-27 11:42:34 +02:00
|
|
|
/** struct for DSDIFF native Artist and Title tags */
|
|
|
|
struct dsdiff_native_tag {
|
|
|
|
uint32_t size;
|
|
|
|
};
|
|
|
|
|
2013-07-28 12:20:50 +02:00
|
|
|
struct DsdiffMetaData {
|
2011-10-03 12:52:15 +02:00
|
|
|
unsigned sample_rate, channels;
|
2012-04-28 15:40:38 +02:00
|
|
|
bool bitreverse;
|
|
|
|
uint64_t chunk_size;
|
2012-10-27 11:42:34 +02:00
|
|
|
#ifdef HAVE_ID3TAG
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream::offset_type id3_offset;
|
2012-10-27 11:42:34 +02:00
|
|
|
uint64_t id3_size;
|
|
|
|
#endif
|
|
|
|
/** offset for artist tag */
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream::offset_type diar_offset;
|
2012-10-27 11:42:34 +02:00
|
|
|
/** offset for title tag */
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream::offset_type diti_offset;
|
2011-10-03 12:52:15 +02:00
|
|
|
};
|
|
|
|
|
2012-03-21 09:01:56 +01:00
|
|
|
static bool lsbitfirst;
|
2011-10-07 10:10:43 +02:00
|
|
|
|
|
|
|
static bool
|
2013-08-04 11:30:26 +02:00
|
|
|
dsdiff_init(const config_param ¶m)
|
2011-10-07 10:10:43 +02:00
|
|
|
{
|
2013-08-04 11:30:26 +02:00
|
|
|
lsbitfirst = param.GetBlockValue("lsbitfirst", false);
|
2011-10-07 10:10:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-03 12:52:15 +02:00
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
dsdiff_read_id(Decoder *decoder, InputStream &is,
|
2013-10-28 23:12:48 +01:00
|
|
|
DsdId *id)
|
2011-10-03 12:52:15 +02:00
|
|
|
{
|
2014-07-09 19:18:36 +02:00
|
|
|
return decoder_read_full(decoder, is, id, sizeof(*id));
|
2011-10-03 12:52:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
dsdiff_read_chunk_header(Decoder *decoder, InputStream &is,
|
2013-07-28 12:20:50 +02:00
|
|
|
DsdiffChunkHeader *header)
|
2011-10-03 12:52:15 +02:00
|
|
|
{
|
2014-07-09 19:18:36 +02:00
|
|
|
return decoder_read_full(decoder, is, header, sizeof(*header));
|
2011-10-03 12:52:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
dsdiff_read_payload(Decoder *decoder, InputStream &is,
|
2013-07-28 12:20:50 +02:00
|
|
|
const DsdiffChunkHeader *header,
|
2011-10-03 12:52:15 +02:00
|
|
|
void *data, size_t length)
|
|
|
|
{
|
2013-07-28 12:20:50 +02:00
|
|
|
uint64_t size = header->GetSize();
|
2011-10-03 12:52:15 +02:00
|
|
|
if (size != (uint64_t)length)
|
|
|
|
return false;
|
|
|
|
|
2014-07-09 19:18:36 +02:00
|
|
|
return decoder_read_full(decoder, is, data, length);
|
2011-10-03 12:52:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read and parse a "SND" chunk inside "PROP".
|
|
|
|
*/
|
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
dsdiff_read_prop_snd(Decoder *decoder, InputStream &is,
|
2013-07-28 12:20:50 +02:00
|
|
|
DsdiffMetaData *metadata,
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream::offset_type end_offset)
|
2011-10-03 12:52:15 +02:00
|
|
|
{
|
2013-07-28 12:20:50 +02:00
|
|
|
DsdiffChunkHeader header;
|
2013-10-23 22:08:59 +02:00
|
|
|
while ((InputStream::offset_type)(is.GetOffset() + sizeof(header)) <= end_offset) {
|
2011-10-03 12:52:15 +02:00
|
|
|
if (!dsdiff_read_chunk_header(decoder, is, &header))
|
|
|
|
return false;
|
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream::offset_type chunk_end_offset = is.GetOffset()
|
2013-07-28 12:20:50 +02:00
|
|
|
+ header.GetSize();
|
2011-10-03 12:52:15 +02:00
|
|
|
if (chunk_end_offset > end_offset)
|
|
|
|
return false;
|
|
|
|
|
2013-10-28 23:12:48 +01:00
|
|
|
if (header.id.Equals("FS ")) {
|
2011-10-03 12:52:15 +02:00
|
|
|
uint32_t sample_rate;
|
|
|
|
if (!dsdiff_read_payload(decoder, is, &header,
|
|
|
|
&sample_rate,
|
|
|
|
sizeof(sample_rate)))
|
|
|
|
return false;
|
|
|
|
|
2013-10-16 21:09:19 +02:00
|
|
|
metadata->sample_rate = FromBE32(sample_rate);
|
2013-10-28 23:12:48 +01:00
|
|
|
} else if (header.id.Equals("CHNL")) {
|
2011-10-03 12:52:15 +02:00
|
|
|
uint16_t channels;
|
2013-07-28 12:20:50 +02:00
|
|
|
if (header.GetSize() < sizeof(channels) ||
|
2014-07-09 19:18:36 +02:00
|
|
|
!decoder_read_full(decoder, is,
|
|
|
|
&channels, sizeof(channels)) ||
|
2012-06-16 13:46:42 +02:00
|
|
|
!dsdlib_skip_to(decoder, is, chunk_end_offset))
|
2011-10-03 12:52:15 +02:00
|
|
|
return false;
|
|
|
|
|
2013-10-16 21:09:19 +02:00
|
|
|
metadata->channels = FromBE16(channels);
|
2013-10-28 23:12:48 +01:00
|
|
|
} else if (header.id.Equals("CMPR")) {
|
|
|
|
DsdId type;
|
2013-07-28 12:20:50 +02:00
|
|
|
if (header.GetSize() < sizeof(type) ||
|
2014-07-09 19:18:36 +02:00
|
|
|
!decoder_read_full(decoder, is,
|
|
|
|
&type, sizeof(type)) ||
|
2012-06-16 13:46:42 +02:00
|
|
|
!dsdlib_skip_to(decoder, is, chunk_end_offset))
|
2011-10-03 12:52:15 +02:00
|
|
|
return false;
|
|
|
|
|
2013-10-28 23:12:48 +01:00
|
|
|
if (!type.Equals("DSD "))
|
2012-04-28 15:40:38 +02:00
|
|
|
/* only uncompressed DSD audio data
|
2011-10-03 12:52:15 +02:00
|
|
|
is implemented */
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
/* ignore unknown chunk */
|
|
|
|
|
2012-06-16 13:46:42 +02:00
|
|
|
if (!dsdlib_skip_to(decoder, is, chunk_end_offset))
|
2011-10-03 12:52:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
return is.GetOffset() == end_offset;
|
2011-10-03 12:52:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read and parse a "PROP" chunk.
|
|
|
|
*/
|
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
dsdiff_read_prop(Decoder *decoder, InputStream &is,
|
2013-07-28 12:20:50 +02:00
|
|
|
DsdiffMetaData *metadata,
|
|
|
|
const DsdiffChunkHeader *prop_header)
|
2011-10-03 12:52:15 +02:00
|
|
|
{
|
2013-07-28 12:20:50 +02:00
|
|
|
uint64_t prop_size = prop_header->GetSize();
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream::offset_type end_offset = is.GetOffset() + prop_size;
|
2011-10-03 12:52:15 +02:00
|
|
|
|
2013-10-28 23:12:48 +01:00
|
|
|
DsdId prop_id;
|
2011-10-03 12:52:15 +02:00
|
|
|
if (prop_size < sizeof(prop_id) ||
|
|
|
|
!dsdiff_read_id(decoder, is, &prop_id))
|
|
|
|
return false;
|
|
|
|
|
2013-10-28 23:12:48 +01:00
|
|
|
if (prop_id.Equals("SND "))
|
2011-10-03 12:52:15 +02:00
|
|
|
return dsdiff_read_prop_snd(decoder, is, metadata, end_offset);
|
|
|
|
else
|
|
|
|
/* ignore unknown PROP chunk */
|
2012-06-16 13:46:42 +02:00
|
|
|
return dsdlib_skip_to(decoder, is, end_offset);
|
2011-10-03 12:52:15 +02:00
|
|
|
}
|
|
|
|
|
2012-10-27 11:42:34 +02:00
|
|
|
static void
|
2013-10-23 22:08:59 +02:00
|
|
|
dsdiff_handle_native_tag(InputStream &is,
|
2012-10-27 11:42:34 +02:00
|
|
|
const struct tag_handler *handler,
|
2013-10-23 22:08:59 +02:00
|
|
|
void *handler_ctx, InputStream::offset_type tagoffset,
|
2013-10-20 13:32:59 +02:00
|
|
|
TagType type)
|
2012-10-27 11:42:34 +02:00
|
|
|
{
|
2013-07-28 12:20:50 +02:00
|
|
|
if (!dsdlib_skip_to(nullptr, is, tagoffset))
|
2012-10-27 11:42:34 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
struct dsdiff_native_tag metatag;
|
|
|
|
|
2014-07-09 19:18:36 +02:00
|
|
|
if (!decoder_read_full(nullptr, is, &metatag, sizeof(metatag)))
|
2012-10-27 11:42:34 +02:00
|
|
|
return;
|
|
|
|
|
2013-10-16 21:09:19 +02:00
|
|
|
uint32_t length = FromBE32(metatag.size);
|
2012-10-27 11:42:34 +02:00
|
|
|
|
|
|
|
/* Check and limit size of the tag to prevent a stack overflow */
|
|
|
|
if (length == 0 || length > 60)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char string[length];
|
|
|
|
char *label;
|
|
|
|
label = string;
|
|
|
|
|
2014-07-09 19:18:36 +02:00
|
|
|
if (!decoder_read_full(nullptr, is, label, (size_t)length))
|
2012-10-27 11:42:34 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
string[length] = '\0';
|
|
|
|
tag_handler_invoke_tag(handler, handler_ctx, type, label);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read and parse additional metadata chunks for tagging purposes. By default
|
|
|
|
* dsdiff files only support equivalents for artist and title but some of the
|
|
|
|
* extract tools add an id3 tag to provide more tags. If such id3 is found
|
|
|
|
* this will be used for tagging otherwise the native tags (if any) will be
|
|
|
|
* used
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
dsdiff_read_metadata_extra(Decoder *decoder, InputStream &is,
|
2013-07-28 12:20:50 +02:00
|
|
|
DsdiffMetaData *metadata,
|
|
|
|
DsdiffChunkHeader *chunk_header,
|
2012-10-27 11:42:34 +02:00
|
|
|
const struct tag_handler *handler,
|
|
|
|
void *handler_ctx)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* skip from DSD data to next chunk header */
|
|
|
|
if (!dsdlib_skip(decoder, is, metadata->chunk_size))
|
|
|
|
return false;
|
|
|
|
if (!dsdiff_read_chunk_header(decoder, is, chunk_header))
|
|
|
|
return false;
|
|
|
|
|
2014-07-12 20:41:21 +02:00
|
|
|
metadata->diar_offset = 0;
|
|
|
|
metadata->diti_offset = 0;
|
|
|
|
|
2012-10-27 11:42:34 +02:00
|
|
|
#ifdef HAVE_ID3TAG
|
2014-07-12 20:41:21 +02:00
|
|
|
metadata->id3_offset = 0;
|
2012-10-27 11:42:34 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Now process all the remaining chunk headers in the stream
|
|
|
|
and record their position and size */
|
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
const auto size = is.GetSize();
|
|
|
|
while (is.GetOffset() < size) {
|
2013-07-28 12:20:50 +02:00
|
|
|
uint64_t chunk_size = chunk_header->GetSize();
|
2012-10-27 11:42:34 +02:00
|
|
|
|
|
|
|
/* DIIN chunk, is directly followed by other chunks */
|
2013-10-28 23:12:48 +01:00
|
|
|
if (chunk_header->id.Equals("DIIN"))
|
2012-10-27 11:42:34 +02:00
|
|
|
chunk_size = 0;
|
|
|
|
|
|
|
|
/* DIAR chunk - DSDIFF native tag for Artist */
|
2013-10-28 23:12:48 +01:00
|
|
|
if (chunk_header->id.Equals("DIAR")) {
|
2013-07-28 12:20:50 +02:00
|
|
|
chunk_size = chunk_header->GetSize();
|
2013-10-23 22:08:59 +02:00
|
|
|
metadata->diar_offset = is.GetOffset();
|
2012-10-27 11:42:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* DITI chunk - DSDIFF native tag for Title */
|
2013-10-28 23:12:48 +01:00
|
|
|
if (chunk_header->id.Equals("DITI")) {
|
2013-07-28 12:20:50 +02:00
|
|
|
chunk_size = chunk_header->GetSize();
|
2013-10-23 22:08:59 +02:00
|
|
|
metadata->diti_offset = is.GetOffset();
|
2012-10-27 11:42:34 +02:00
|
|
|
}
|
|
|
|
#ifdef HAVE_ID3TAG
|
|
|
|
/* 'ID3 ' chunk, offspec. Used by sacdextract */
|
2013-10-28 23:12:48 +01:00
|
|
|
if (chunk_header->id.Equals("ID3 ")) {
|
2013-07-28 12:20:50 +02:00
|
|
|
chunk_size = chunk_header->GetSize();
|
2013-10-23 22:08:59 +02:00
|
|
|
metadata->id3_offset = is.GetOffset();
|
2012-10-27 11:42:34 +02:00
|
|
|
metadata->id3_size = chunk_size;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (chunk_size != 0) {
|
|
|
|
if (!dsdlib_skip(decoder, is, chunk_size))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
if (is.GetOffset() < size) {
|
2012-10-27 11:42:34 +02:00
|
|
|
if (!dsdiff_read_chunk_header(decoder, is, chunk_header))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* done processing chunk headers, process tags if any */
|
|
|
|
|
|
|
|
#ifdef HAVE_ID3TAG
|
|
|
|
if (metadata->id3_offset != 0)
|
|
|
|
{
|
|
|
|
/* a ID3 tag has preference over the other tags, do not process
|
|
|
|
other tags if we have one */
|
|
|
|
dsdlib_tag_id3(is, handler, handler_ctx, metadata->id3_offset);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (metadata->diar_offset != 0)
|
|
|
|
dsdiff_handle_native_tag(is, handler, handler_ctx,
|
|
|
|
metadata->diar_offset, TAG_ARTIST);
|
|
|
|
|
|
|
|
if (metadata->diti_offset != 0)
|
|
|
|
dsdiff_handle_native_tag(is, handler, handler_ctx,
|
|
|
|
metadata->diti_offset, TAG_TITLE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-03 12:52:15 +02:00
|
|
|
/**
|
|
|
|
* Read and parse all metadata chunks at the beginning. Stop when the
|
|
|
|
* first "DSD" chunk is seen, and return its header in the
|
|
|
|
* "chunk_header" parameter.
|
|
|
|
*/
|
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
dsdiff_read_metadata(Decoder *decoder, InputStream &is,
|
2013-07-28 12:20:50 +02:00
|
|
|
DsdiffMetaData *metadata,
|
|
|
|
DsdiffChunkHeader *chunk_header)
|
2011-10-03 12:52:15 +02:00
|
|
|
{
|
2013-07-28 12:20:50 +02:00
|
|
|
DsdiffHeader header;
|
2014-07-09 19:18:36 +02:00
|
|
|
if (!decoder_read_full(decoder, is, &header, sizeof(header)) ||
|
2013-10-28 23:12:48 +01:00
|
|
|
!header.id.Equals("FRM8") ||
|
|
|
|
!header.format.Equals("DSD "))
|
2011-10-03 12:52:15 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
while (true) {
|
2012-04-28 15:40:38 +02:00
|
|
|
if (!dsdiff_read_chunk_header(decoder, is,
|
|
|
|
chunk_header))
|
2011-10-03 12:52:15 +02:00
|
|
|
return false;
|
|
|
|
|
2013-10-28 23:12:48 +01:00
|
|
|
if (chunk_header->id.Equals("PROP")) {
|
2011-10-03 12:52:15 +02:00
|
|
|
if (!dsdiff_read_prop(decoder, is, metadata,
|
|
|
|
chunk_header))
|
2012-04-28 15:40:38 +02:00
|
|
|
return false;
|
2013-10-28 23:12:48 +01:00
|
|
|
} else if (chunk_header->id.Equals("DSD ")) {
|
2013-07-28 12:20:50 +02:00
|
|
|
const uint64_t chunk_size = chunk_header->GetSize();
|
2012-06-16 13:46:42 +02:00
|
|
|
metadata->chunk_size = chunk_size;
|
2011-10-03 12:52:15 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
/* ignore unknown chunk */
|
2013-07-28 12:20:50 +02:00
|
|
|
const uint64_t chunk_size = chunk_header->GetSize();
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream::offset_type chunk_end_offset =
|
|
|
|
is.GetOffset() + chunk_size;
|
2011-10-03 12:52:15 +02:00
|
|
|
|
2012-06-16 13:46:42 +02:00
|
|
|
if (!dsdlib_skip_to(decoder, is, chunk_end_offset))
|
2011-10-03 12:52:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-21 09:01:56 +01:00
|
|
|
static void
|
|
|
|
bit_reverse_buffer(uint8_t *p, uint8_t *end)
|
|
|
|
{
|
|
|
|
for (; p < end; ++p)
|
|
|
|
*p = bit_reverse(*p);
|
|
|
|
}
|
|
|
|
|
2011-10-03 12:52:15 +02:00
|
|
|
/**
|
|
|
|
* Decode one "DSD" chunk.
|
|
|
|
*/
|
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
dsdiff_decode_chunk(Decoder &decoder, InputStream &is,
|
2011-10-03 12:52:15 +02:00
|
|
|
unsigned channels,
|
2012-06-16 13:46:42 +02:00
|
|
|
uint64_t chunk_size)
|
2011-10-03 12:52:15 +02:00
|
|
|
{
|
|
|
|
uint8_t buffer[8192];
|
2012-04-28 15:40:38 +02:00
|
|
|
|
2011-10-03 12:52:15 +02:00
|
|
|
const size_t sample_size = sizeof(buffer[0]);
|
|
|
|
const size_t frame_size = channels * sample_size;
|
|
|
|
const unsigned buffer_frames = sizeof(buffer) / frame_size;
|
|
|
|
const unsigned buffer_samples = buffer_frames * frame_size;
|
|
|
|
const size_t buffer_size = buffer_samples * sample_size;
|
|
|
|
|
|
|
|
while (chunk_size > 0) {
|
|
|
|
/* see how much aligned data from the remaining chunk
|
|
|
|
fits into the local buffer */
|
|
|
|
size_t now_size = buffer_size;
|
|
|
|
if (chunk_size < (uint64_t)now_size) {
|
2013-11-23 18:30:06 +01:00
|
|
|
unsigned now_frames =
|
|
|
|
(unsigned)chunk_size / frame_size;
|
2011-10-03 12:52:15 +02:00
|
|
|
now_size = now_frames * frame_size;
|
|
|
|
}
|
|
|
|
|
2014-07-09 19:18:36 +02:00
|
|
|
if (!decoder_read_full(&decoder, is, buffer, now_size))
|
2011-10-03 12:52:15 +02:00
|
|
|
return false;
|
|
|
|
|
2014-07-09 19:18:36 +02:00
|
|
|
const size_t nbytes = now_size;
|
2011-10-03 12:52:15 +02:00
|
|
|
chunk_size -= nbytes;
|
|
|
|
|
2012-06-16 13:46:42 +02:00
|
|
|
if (lsbitfirst)
|
2012-03-21 09:01:56 +01:00
|
|
|
bit_reverse_buffer(buffer, buffer + nbytes);
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
const auto cmd = decoder_data(decoder, is, buffer, nbytes, 0);
|
2011-10-03 12:52:15 +02:00
|
|
|
switch (cmd) {
|
2013-09-27 12:11:37 +02:00
|
|
|
case DecoderCommand::NONE:
|
2011-10-03 12:52:15 +02:00
|
|
|
break;
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
case DecoderCommand::START:
|
|
|
|
case DecoderCommand::STOP:
|
2011-10-03 12:52:15 +02:00
|
|
|
return false;
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
case DecoderCommand::SEEK:
|
2012-06-16 13:46:42 +02:00
|
|
|
|
|
|
|
/* Not implemented yet */
|
2011-10-03 12:52:15 +02:00
|
|
|
decoder_seek_error(decoder);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-10-21 21:12:37 +02:00
|
|
|
return dsdlib_skip(&decoder, is, chunk_size);
|
2011-10-03 12:52:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-10-23 22:08:59 +02:00
|
|
|
dsdiff_stream_decode(Decoder &decoder, InputStream &is)
|
2011-10-03 12:52:15 +02:00
|
|
|
{
|
2013-07-28 12:20:50 +02:00
|
|
|
DsdiffMetaData metadata;
|
2011-10-03 12:52:15 +02:00
|
|
|
|
2013-07-28 12:20:50 +02:00
|
|
|
DsdiffChunkHeader chunk_header;
|
2012-06-16 13:46:42 +02:00
|
|
|
/* check if it is is a proper DFF file */
|
2013-10-21 21:12:37 +02:00
|
|
|
if (!dsdiff_read_metadata(&decoder, is, &metadata, &chunk_header))
|
2012-06-16 13:46:42 +02:00
|
|
|
return;
|
2011-10-03 12:52:15 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2013-08-03 21:00:50 +02:00
|
|
|
AudioFormat audio_format;
|
|
|
|
if (!audio_format_init_checked(audio_format, metadata.sample_rate / 8,
|
|
|
|
SampleFormat::DSD,
|
2013-08-10 18:02:44 +02:00
|
|
|
metadata.channels, error)) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(error);
|
2011-10-03 12:52:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-12 19:40:56 +02:00
|
|
|
/* calculate song time from DSD chunk size and sample frequency */
|
|
|
|
uint64_t chunk_size = metadata.chunk_size;
|
|
|
|
float songtime = ((chunk_size / metadata.channels) * 8) /
|
|
|
|
(float) metadata.sample_rate;
|
|
|
|
|
2011-10-03 12:52:15 +02:00
|
|
|
/* success: file was recognized */
|
2013-08-03 21:00:50 +02:00
|
|
|
decoder_initialized(decoder, audio_format, false, songtime);
|
2011-10-03 12:52:15 +02:00
|
|
|
|
2012-06-16 13:46:42 +02:00
|
|
|
/* every iteration of the following loop decodes one "DSD"
|
|
|
|
chunk from a DFF file */
|
2012-04-28 15:40:38 +02:00
|
|
|
|
2012-06-16 13:46:42 +02:00
|
|
|
while (true) {
|
2013-07-28 12:20:50 +02:00
|
|
|
chunk_size = chunk_header.GetSize();
|
2012-04-28 15:40:38 +02:00
|
|
|
|
2013-10-28 23:12:48 +01:00
|
|
|
if (chunk_header.id.Equals("DSD ")) {
|
2012-06-16 13:46:42 +02:00
|
|
|
if (!dsdiff_decode_chunk(decoder, is,
|
|
|
|
metadata.channels,
|
|
|
|
chunk_size))
|
2012-04-28 15:40:38 +02:00
|
|
|
break;
|
2012-06-16 13:46:42 +02:00
|
|
|
} else {
|
|
|
|
/* ignore other chunks */
|
2013-10-21 21:12:37 +02:00
|
|
|
if (!dsdlib_skip(&decoder, is, chunk_size))
|
2011-10-03 12:52:15 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-06-16 13:46:42 +02:00
|
|
|
|
|
|
|
/* read next chunk header; the first one was read by
|
|
|
|
dsdiff_read_metadata() */
|
2013-10-21 21:12:37 +02:00
|
|
|
if (!dsdiff_read_chunk_header(&decoder,
|
2012-06-16 13:46:42 +02:00
|
|
|
is, &chunk_header))
|
|
|
|
break;
|
2011-10-03 12:52:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
dsdiff_scan_stream(InputStream &is,
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_unused const struct tag_handler *handler,
|
|
|
|
gcc_unused void *handler_ctx)
|
2011-10-03 12:52:15 +02:00
|
|
|
{
|
2013-07-28 12:20:50 +02:00
|
|
|
DsdiffMetaData metadata;
|
|
|
|
DsdiffChunkHeader chunk_header;
|
2011-10-03 12:52:15 +02:00
|
|
|
|
2012-04-28 15:40:38 +02:00
|
|
|
/* First check for DFF metadata */
|
2013-07-28 12:20:50 +02:00
|
|
|
if (!dsdiff_read_metadata(nullptr, is, &metadata, &chunk_header))
|
2012-06-16 13:46:42 +02:00
|
|
|
return false;
|
2011-10-03 12:52:15 +02:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
AudioFormat audio_format;
|
|
|
|
if (!audio_format_init_checked(audio_format, metadata.sample_rate / 8,
|
|
|
|
SampleFormat::DSD,
|
2013-08-10 18:02:44 +02:00
|
|
|
metadata.channels, IgnoreError()))
|
2011-10-03 12:52:15 +02:00
|
|
|
/* refuse to parse files which we cannot play anyway */
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2011-10-03 12:52:15 +02:00
|
|
|
|
2012-07-12 19:40:56 +02:00
|
|
|
/* calculate song time and add as tag */
|
|
|
|
unsigned songtime = ((metadata.chunk_size / metadata.channels) * 8) /
|
|
|
|
metadata.sample_rate;
|
|
|
|
tag_handler_invoke_duration(handler, handler_ctx, songtime);
|
|
|
|
|
2012-10-27 11:42:34 +02:00
|
|
|
/* Read additional metadata and created tags if available */
|
2013-07-28 12:20:50 +02:00
|
|
|
dsdiff_read_metadata_extra(nullptr, is, &metadata, &chunk_header,
|
2012-10-27 11:42:34 +02:00
|
|
|
handler, handler_ctx);
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
return true;
|
2011-10-03 12:52:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *const dsdiff_suffixes[] = {
|
|
|
|
"dff",
|
2013-07-28 12:20:50 +02:00
|
|
|
nullptr
|
2011-10-03 12:52:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char *const dsdiff_mime_types[] = {
|
|
|
|
"application/x-dff",
|
2013-07-28 12:20:50 +02:00
|
|
|
nullptr
|
2011-10-03 12:52:15 +02:00
|
|
|
};
|
|
|
|
|
2013-10-21 20:31:34 +02:00
|
|
|
const struct DecoderPlugin dsdiff_decoder_plugin = {
|
2013-07-28 12:20:50 +02:00
|
|
|
"dsdiff",
|
|
|
|
dsdiff_init,
|
|
|
|
nullptr,
|
|
|
|
dsdiff_stream_decode,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
dsdiff_scan_stream,
|
|
|
|
nullptr,
|
|
|
|
dsdiff_suffixes,
|
|
|
|
dsdiff_mime_types,
|
2011-10-03 12:52:15 +02:00
|
|
|
};
|