2011-10-03 12:52:15 +02:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 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"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "../DecoderAPI.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "input/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-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
|
|
|
|
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;
|
2014-08-19 22:39:44 +02:00
|
|
|
offset_type chunk_size;
|
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
|
2015-01-21 22:13:44 +01:00
|
|
|
dsdiff_init(const ConfigBlock &block)
|
2011-10-07 10:10:43 +02:00
|
|
|
{
|
2015-01-21 22:13:44 +01:00
|
|
|
lsbitfirst = block.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,
|
2014-08-19 22:29:52 +02:00
|
|
|
offset_type end_offset)
|
2011-10-03 12:52:15 +02:00
|
|
|
{
|
2013-07-28 12:20:50 +02:00
|
|
|
DsdiffChunkHeader header;
|
2014-08-19 22:39:44 +02:00
|
|
|
while (is.GetOffset() + sizeof(header) <= end_offset) {
|
2011-10-03 12:52:15 +02:00
|
|
|
if (!dsdiff_read_chunk_header(decoder, is, &header))
|
|
|
|
return false;
|
|
|
|
|
2014-08-19 22:29:52 +02:00
|
|
|
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();
|
2014-08-19 22:29:52 +02:00
|
|
|
const 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,
|
2016-02-23 10:10:13 +01:00
|
|
|
const TagHandler &handler,
|
2014-08-19 22:29:52 +02:00
|
|
|
void *handler_ctx, 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;
|
|
|
|
|
2016-03-06 23:28:29 +01:00
|
|
|
char string[length + 1];
|
2012-10-27 11:42:34 +02:00
|
|
|
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,
|
2016-02-23 10:10:13 +01:00
|
|
|
const TagHandler &handler,
|
2012-10-27 11:42:34 +02:00
|
|
|
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-11 17:53:35 +02:00
|
|
|
/** offset for artist tag */
|
2014-08-19 22:29:52 +02:00
|
|
|
offset_type artist_offset = 0;
|
2014-07-11 17:53:35 +02:00
|
|
|
/** offset for title tag */
|
2014-08-19 22:29:52 +02:00
|
|
|
offset_type title_offset = 0;
|
2014-07-12 20:41:21 +02:00
|
|
|
|
2014-11-22 23:18:07 +01:00
|
|
|
#ifdef ENABLE_ID3TAG
|
2014-08-19 22:29:52 +02:00
|
|
|
offset_type 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 */
|
|
|
|
|
2014-07-12 20:46:24 +02:00
|
|
|
do {
|
2014-08-19 22:39:44 +02:00
|
|
|
offset_type 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();
|
2014-07-11 17:53:35 +02:00
|
|
|
artist_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();
|
2014-07-11 17:53:35 +02:00
|
|
|
title_offset = is.GetOffset();
|
2012-10-27 11:42:34 +02:00
|
|
|
}
|
2014-11-22 23:18:07 +01:00
|
|
|
#ifdef ENABLE_ID3TAG
|
2012-10-27 11:42:34 +02:00
|
|
|
/* '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();
|
2014-07-11 17:53:35 +02:00
|
|
|
id3_offset = is.GetOffset();
|
2012-10-27 11:42:34 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-07-12 20:51:00 +02:00
|
|
|
if (!dsdlib_skip(decoder, is, chunk_size))
|
|
|
|
break;
|
2014-07-12 20:46:24 +02:00
|
|
|
} while (dsdiff_read_chunk_header(decoder, is, chunk_header));
|
2012-10-27 11:42:34 +02:00
|
|
|
|
|
|
|
/* done processing chunk headers, process tags if any */
|
|
|
|
|
2014-11-22 23:18:07 +01:00
|
|
|
#ifdef ENABLE_ID3TAG
|
2014-07-11 17:53:35 +02:00
|
|
|
if (id3_offset != 0) {
|
2012-10-27 11:42:34 +02:00
|
|
|
/* a ID3 tag has preference over the other tags, do not process
|
|
|
|
other tags if we have one */
|
2014-07-11 17:53:35 +02:00
|
|
|
dsdlib_tag_id3(is, handler, handler_ctx, id3_offset);
|
2012-10-27 11:42:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-07-11 17:53:35 +02:00
|
|
|
if (artist_offset != 0)
|
2012-10-27 11:42:34 +02:00
|
|
|
dsdiff_handle_native_tag(is, handler, handler_ctx,
|
2014-07-11 17:53:35 +02:00
|
|
|
artist_offset, TAG_ARTIST);
|
2012-10-27 11:42:34 +02:00
|
|
|
|
2014-07-11 17:53:35 +02:00
|
|
|
if (title_offset != 0)
|
2012-10-27 11:42:34 +02:00
|
|
|
dsdiff_handle_native_tag(is, handler, handler_ctx,
|
2014-07-11 17:53:35 +02:00
|
|
|
title_offset, TAG_TITLE);
|
2012-10-27 11:42:34 +02:00
|
|
|
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 ")) {
|
2014-08-19 22:39:44 +02:00
|
|
|
const offset_type 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 */
|
2014-08-19 22:39:44 +02:00
|
|
|
const offset_type chunk_size = chunk_header->GetSize();
|
2014-08-19 22:29:52 +02:00
|
|
|
const offset_type chunk_end_offset =
|
2013-10-23 22:08:59 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-08-23 15:27:21 +02:00
|
|
|
static offset_type
|
2014-08-26 21:53:50 +02:00
|
|
|
FrameToOffset(uint64_t frame, unsigned channels)
|
2014-08-23 15:27:21 +02:00
|
|
|
{
|
2014-08-26 21:53:50 +02:00
|
|
|
return frame * channels;
|
2014-08-23 15:27:21 +02:00
|
|
|
}
|
|
|
|
|
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,
|
2014-04-04 12:43:54 +02:00
|
|
|
unsigned channels, unsigned sample_rate,
|
2014-08-23 15:21:08 +02:00
|
|
|
const offset_type total_bytes)
|
2011-10-03 12:52:15 +02:00
|
|
|
{
|
2014-08-23 15:27:21 +02:00
|
|
|
const offset_type start_offset = is.GetOffset();
|
|
|
|
|
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;
|
2014-08-23 15:17:31 +02:00
|
|
|
const size_t buffer_size = buffer_frames * frame_size;
|
2011-10-03 12:52:15 +02:00
|
|
|
|
2014-08-23 15:25:40 +02:00
|
|
|
auto cmd = decoder_get_command(decoder);
|
2014-08-23 15:21:08 +02:00
|
|
|
for (offset_type remaining_bytes = total_bytes;
|
2014-08-23 15:25:40 +02:00
|
|
|
remaining_bytes >= frame_size && cmd != DecoderCommand::STOP;) {
|
2014-08-23 15:27:21 +02:00
|
|
|
if (cmd == DecoderCommand::SEEK) {
|
2014-08-26 21:53:50 +02:00
|
|
|
uint64_t frame = decoder_seek_where_frame(decoder);
|
|
|
|
offset_type offset = FrameToOffset(frame, channels);
|
2014-08-23 15:27:21 +02:00
|
|
|
if (offset >= total_bytes) {
|
|
|
|
decoder_command_finished(decoder);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dsdlib_skip_to(&decoder, is,
|
|
|
|
start_offset + offset)) {
|
|
|
|
decoder_command_finished(decoder);
|
|
|
|
remaining_bytes = total_bytes - offset;
|
|
|
|
} else
|
|
|
|
decoder_seek_error(decoder);
|
|
|
|
}
|
|
|
|
|
2011-10-03 12:52:15 +02:00
|
|
|
/* see how much aligned data from the remaining chunk
|
|
|
|
fits into the local buffer */
|
|
|
|
size_t now_size = buffer_size;
|
2014-08-23 15:21:08 +02:00
|
|
|
if (remaining_bytes < (offset_type)now_size) {
|
|
|
|
unsigned now_frames = remaining_bytes / 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;
|
2014-08-23 15:21:08 +02:00
|
|
|
remaining_bytes -= nbytes;
|
2011-10-03 12:52:15 +02:00
|
|
|
|
2012-06-16 13:46:42 +02:00
|
|
|
if (lsbitfirst)
|
2012-03-21 09:01:56 +01:00
|
|
|
bit_reverse_buffer(buffer, buffer + nbytes);
|
|
|
|
|
2014-08-23 15:25:40 +02:00
|
|
|
cmd = decoder_data(decoder, is, buffer, nbytes,
|
|
|
|
sample_rate / 1000);
|
2011-10-03 12:52:15 +02:00
|
|
|
}
|
2014-08-23 15:19:34 +02:00
|
|
|
|
|
|
|
return true;
|
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
|
|
|
|
2016-11-10 11:45:17 +01:00
|
|
|
auto audio_format = CheckAudioFormat(metadata.sample_rate / 8,
|
|
|
|
SampleFormat::DSD,
|
|
|
|
metadata.channels);
|
2011-10-03 12:52:15 +02:00
|
|
|
|
2012-07-12 19:40:56 +02:00
|
|
|
/* calculate song time from DSD chunk size and sample frequency */
|
2014-08-19 22:39:44 +02:00
|
|
|
offset_type chunk_size = metadata.chunk_size;
|
2014-08-29 20:52:39 +02:00
|
|
|
|
|
|
|
uint64_t n_frames = chunk_size / audio_format.channels;
|
|
|
|
auto songtime = SongTime::FromScale<uint64_t>(n_frames,
|
|
|
|
audio_format.sample_rate);
|
2012-07-12 19:40:56 +02:00
|
|
|
|
2011-10-03 12:52:15 +02:00
|
|
|
/* success: file was recognized */
|
2014-08-23 15:27:21 +02:00
|
|
|
decoder_initialized(decoder, audio_format, is.IsSeekable(), 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
|
|
|
|
2014-08-23 15:14:16 +02:00
|
|
|
dsdiff_decode_chunk(decoder, is,
|
|
|
|
metadata.channels,
|
|
|
|
metadata.sample_rate,
|
|
|
|
chunk_size);
|
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,
|
2016-02-23 10:10:13 +01:00
|
|
|
gcc_unused const TagHandler &handler,
|
2013-08-04 23:48:01 +02:00
|
|
|
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
|
|
|
|
2016-11-10 11:45:17 +01:00
|
|
|
auto audio_format = CheckAudioFormat(metadata.sample_rate / 8,
|
|
|
|
SampleFormat::DSD,
|
|
|
|
metadata.channels);
|
2011-10-03 12:52:15 +02:00
|
|
|
|
2012-07-12 19:40:56 +02:00
|
|
|
/* calculate song time and add as tag */
|
2014-08-29 22:43:36 +02:00
|
|
|
uint64_t n_frames = metadata.chunk_size / audio_format.channels;
|
|
|
|
auto songtime = SongTime::FromScale<uint64_t>(n_frames,
|
|
|
|
audio_format.sample_rate);
|
2012-07-12 19:40:56 +02:00
|
|
|
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
|
|
|
};
|