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-07-29 07:50:08 +02:00
|
|
|
#include "CheckAudioFormat.hxx"
|
2012-03-21 09:01:56 +01:00
|
|
|
#include "util/bit_reverse.h"
|
2013-07-29 07:32:36 +02:00
|
|
|
#include "TagHandler.hxx"
|
2013-07-28 12:20:50 +02:00
|
|
|
#include "DsdLib.hxx"
|
2013-07-29 07:32:36 +02:00
|
|
|
#include "TagHandler.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
|
|
|
|
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "dsdiff"
|
|
|
|
|
2013-07-28 12:20:50 +02:00
|
|
|
struct DsdiffHeader {
|
2012-06-16 13:46:42 +02:00
|
|
|
struct dsdlib_id id;
|
2011-10-03 12:52:15 +02:00
|
|
|
uint32_t size_high, size_low;
|
2012-06-16 13:46:42 +02:00
|
|
|
struct dsdlib_id format;
|
2011-10-03 12:52:15 +02:00
|
|
|
};
|
|
|
|
|
2013-07-28 12:20:50 +02:00
|
|
|
struct DsdiffChunkHeader {
|
2012-06-16 13:46:42 +02:00
|
|
|
struct dsdlib_id id;
|
2011-10-03 12:52:15 +02:00
|
|
|
uint32_t size_high, size_low;
|
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.
|
|
|
|
*/
|
|
|
|
gcc_const
|
|
|
|
uint64_t GetSize() const {
|
|
|
|
return (((uint64_t)GUINT32_FROM_BE(size_high)) << 32) |
|
|
|
|
((uint64_t)GUINT32_FROM_BE(size_low));
|
|
|
|
}
|
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
|
|
|
|
goffset id3_offset;
|
|
|
|
uint64_t id3_size;
|
|
|
|
#endif
|
|
|
|
/** offset for artist tag */
|
|
|
|
goffset diar_offset;
|
|
|
|
/** offset for title tag */
|
|
|
|
goffset 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
|
|
|
|
dsdiff_init(const struct config_param *param)
|
|
|
|
{
|
2012-03-21 09:01:56 +01:00
|
|
|
lsbitfirst = config_get_block_bool(param, "lsbitfirst", false);
|
2011-10-07 10:10:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-03 12:52:15 +02:00
|
|
|
static bool
|
|
|
|
dsdiff_read_id(struct decoder *decoder, struct input_stream *is,
|
2012-06-16 13:46:42 +02:00
|
|
|
struct dsdlib_id *id)
|
2011-10-03 12:52:15 +02:00
|
|
|
{
|
2012-06-16 13:46:42 +02:00
|
|
|
return dsdlib_read(decoder, is, id, sizeof(*id));
|
2011-10-03 12:52:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
dsdiff_read_chunk_header(struct decoder *decoder, struct input_stream *is,
|
2013-07-28 12:20:50 +02:00
|
|
|
DsdiffChunkHeader *header)
|
2011-10-03 12:52:15 +02:00
|
|
|
{
|
2012-06-16 13:46:42 +02:00
|
|
|
return dsdlib_read(decoder, is, header, sizeof(*header));
|
2011-10-03 12:52:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
dsdiff_read_payload(struct decoder *decoder, struct input_stream *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;
|
|
|
|
|
|
|
|
size_t nbytes = decoder_read(decoder, is, data, length);
|
|
|
|
return nbytes == length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read and parse a "SND" chunk inside "PROP".
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
dsdiff_read_prop_snd(struct decoder *decoder, struct input_stream *is,
|
2013-07-28 12:20:50 +02:00
|
|
|
DsdiffMetaData *metadata,
|
2011-10-03 12:52:15 +02:00
|
|
|
goffset end_offset)
|
|
|
|
{
|
2013-07-28 12:20:50 +02:00
|
|
|
DsdiffChunkHeader header;
|
2013-01-24 19:14:40 +01:00
|
|
|
while ((goffset)(input_stream_get_offset(is) + sizeof(header)) <= end_offset) {
|
2011-10-03 12:52:15 +02:00
|
|
|
if (!dsdiff_read_chunk_header(decoder, is, &header))
|
|
|
|
return false;
|
|
|
|
|
2013-01-24 19:14:40 +01:00
|
|
|
goffset chunk_end_offset = input_stream_get_offset(is)
|
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;
|
|
|
|
|
2012-06-16 13:46:42 +02:00
|
|
|
if (dsdlib_id_equals(&header.id, "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;
|
|
|
|
|
|
|
|
metadata->sample_rate = GUINT32_FROM_BE(sample_rate);
|
2012-06-16 13:46:42 +02:00
|
|
|
} else if (dsdlib_id_equals(&header.id, "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) ||
|
2012-06-16 13:46:42 +02:00
|
|
|
!dsdlib_read(decoder, is,
|
2011-10-03 12:52:15 +02:00
|
|
|
&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;
|
|
|
|
|
|
|
|
metadata->channels = GUINT16_FROM_BE(channels);
|
2012-06-16 13:46:42 +02:00
|
|
|
} else if (dsdlib_id_equals(&header.id, "CMPR")) {
|
|
|
|
struct dsdlib_id type;
|
2013-07-28 12:20:50 +02:00
|
|
|
if (header.GetSize() < sizeof(type) ||
|
2012-06-16 13:46:42 +02:00
|
|
|
!dsdlib_read(decoder, is,
|
2011-10-03 12:52:15 +02:00
|
|
|
&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;
|
|
|
|
|
2012-06-16 13:46:42 +02:00
|
|
|
if (!dsdlib_id_equals(&type, "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-01-24 19:14:40 +01:00
|
|
|
return input_stream_get_offset(is) == end_offset;
|
2011-10-03 12:52:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read and parse a "PROP" chunk.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
dsdiff_read_prop(struct decoder *decoder, struct input_stream *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-01-24 19:14:40 +01:00
|
|
|
goffset end_offset = input_stream_get_offset(is) + prop_size;
|
2011-10-03 12:52:15 +02:00
|
|
|
|
2012-06-16 13:46:42 +02:00
|
|
|
struct dsdlib_id 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;
|
|
|
|
|
2012-06-16 13:46:42 +02:00
|
|
|
if (dsdlib_id_equals(&prop_id, "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
|
|
|
|
dsdiff_handle_native_tag(struct input_stream *is,
|
|
|
|
const struct tag_handler *handler,
|
|
|
|
void *handler_ctx, goffset tagoffset,
|
|
|
|
enum tag_type type)
|
|
|
|
{
|
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;
|
|
|
|
|
2013-07-28 12:20:50 +02:00
|
|
|
if (!dsdlib_read(nullptr, is, &metatag, sizeof(metatag)))
|
2012-10-27 11:42:34 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
uint32_t length = GUINT32_FROM_BE(metatag.size);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2013-07-28 12:20:50 +02:00
|
|
|
if (!dsdlib_read(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
|
|
|
|
dsdiff_read_metadata_extra(struct decoder *decoder, struct input_stream *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;
|
|
|
|
|
|
|
|
#ifdef HAVE_ID3TAG
|
|
|
|
metadata->id3_size = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Now process all the remaining chunk headers in the stream
|
|
|
|
and record their position and size */
|
|
|
|
|
2013-01-24 19:14:40 +01:00
|
|
|
const goffset size = input_stream_get_size(is);
|
|
|
|
while (input_stream_get_offset(is) < 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 */
|
|
|
|
if (dsdlib_id_equals(&chunk_header->id, "DIIN"))
|
|
|
|
chunk_size = 0;
|
|
|
|
|
|
|
|
/* DIAR chunk - DSDIFF native tag for Artist */
|
|
|
|
if (dsdlib_id_equals(&chunk_header->id, "DIAR")) {
|
2013-07-28 12:20:50 +02:00
|
|
|
chunk_size = chunk_header->GetSize();
|
2013-01-24 19:14:40 +01:00
|
|
|
metadata->diar_offset = input_stream_get_offset(is);
|
2012-10-27 11:42:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* DITI chunk - DSDIFF native tag for Title */
|
|
|
|
if (dsdlib_id_equals(&chunk_header->id, "DITI")) {
|
2013-07-28 12:20:50 +02:00
|
|
|
chunk_size = chunk_header->GetSize();
|
2013-01-24 19:14:40 +01:00
|
|
|
metadata->diti_offset = input_stream_get_offset(is);
|
2012-10-27 11:42:34 +02:00
|
|
|
}
|
|
|
|
#ifdef HAVE_ID3TAG
|
|
|
|
/* 'ID3 ' chunk, offspec. Used by sacdextract */
|
|
|
|
if (dsdlib_id_equals(&chunk_header->id, "ID3 ")) {
|
2013-07-28 12:20:50 +02:00
|
|
|
chunk_size = chunk_header->GetSize();
|
2013-01-24 19:14:40 +01:00
|
|
|
metadata->id3_offset = input_stream_get_offset(is);
|
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-01-24 19:14:40 +01:00
|
|
|
if (input_stream_get_offset(is) < size) {
|
2012-10-27 11:42:34 +02:00
|
|
|
if (!dsdiff_read_chunk_header(decoder, is, chunk_header))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
chunk_size = 0;
|
|
|
|
}
|
|
|
|
/* 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
|
|
|
|
dsdiff_read_metadata(struct decoder *decoder, struct input_stream *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;
|
2012-06-16 13:46:42 +02:00
|
|
|
if (!dsdlib_read(decoder, is, &header, sizeof(header)) ||
|
|
|
|
!dsdlib_id_equals(&header.id, "FRM8") ||
|
|
|
|
!dsdlib_id_equals(&header.format, "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;
|
|
|
|
|
2012-06-16 13:46:42 +02:00
|
|
|
if (dsdlib_id_equals(&chunk_header->id, "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;
|
2012-06-16 13:46:42 +02:00
|
|
|
} else if (dsdlib_id_equals(&chunk_header->id, "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-01-24 19:14:40 +01:00
|
|
|
goffset chunk_end_offset = input_stream_get_offset(is)
|
|
|
|
+ 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
|
|
|
|
dsdiff_decode_chunk(struct decoder *decoder, struct input_stream *is,
|
|
|
|
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 */
|
|
|
|
unsigned now_frames = buffer_frames;
|
|
|
|
size_t now_size = buffer_size;
|
|
|
|
if (chunk_size < (uint64_t)now_size) {
|
|
|
|
now_frames = (unsigned)chunk_size / frame_size;
|
|
|
|
now_size = now_frames * frame_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t nbytes = decoder_read(decoder, is, buffer, now_size);
|
|
|
|
if (nbytes != now_size)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2011-10-03 12:52:15 +02:00
|
|
|
enum decoder_command cmd =
|
2012-03-01 01:56:23 +01:00
|
|
|
decoder_data(decoder, is, buffer, nbytes, 0);
|
2011-10-03 12:52:15 +02:00
|
|
|
switch (cmd) {
|
|
|
|
case DECODE_COMMAND_NONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DECODE_COMMAND_START:
|
|
|
|
case DECODE_COMMAND_STOP:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case DECODE_COMMAND_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;
|
|
|
|
}
|
|
|
|
}
|
2012-06-16 13:46:42 +02:00
|
|
|
return dsdlib_skip(decoder, is, chunk_size);
|
2011-10-03 12:52:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dsdiff_stream_decode(struct decoder *decoder, struct input_stream *is)
|
|
|
|
{
|
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 */
|
2011-10-03 12:52:15 +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-07-28 12:20:50 +02:00
|
|
|
GError *error = nullptr;
|
2011-10-03 12:52:15 +02:00
|
|
|
struct audio_format audio_format;
|
|
|
|
if (!audio_format_init_checked(&audio_format, metadata.sample_rate / 8,
|
2012-03-21 09:01:56 +01:00
|
|
|
SAMPLE_FORMAT_DSD,
|
2011-10-03 12:52:15 +02:00
|
|
|
metadata.channels, &error)) {
|
|
|
|
g_warning("%s", error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
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 */
|
2012-07-12 19:40:56 +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
|
|
|
|
2012-06-16 13:46:42 +02:00
|
|
|
if (dsdlib_id_equals(&chunk_header.id, "DSD ")) {
|
|
|
|
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 */
|
|
|
|
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() */
|
|
|
|
if (!dsdiff_read_chunk_header(decoder,
|
|
|
|
is, &chunk_header))
|
|
|
|
break;
|
2011-10-03 12:52:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
|
|
|
dsdiff_scan_stream(struct input_stream *is,
|
|
|
|
G_GNUC_UNUSED const struct tag_handler *handler,
|
|
|
|
G_GNUC_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
|
|
|
|
|
|
|
struct audio_format audio_format;
|
|
|
|
if (!audio_format_init_checked(&audio_format, metadata.sample_rate / 8,
|
2012-03-21 09:01:56 +01:00
|
|
|
SAMPLE_FORMAT_DSD,
|
2013-07-28 12:20:50 +02:00
|
|
|
metadata.channels, nullptr))
|
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
|
|
|
};
|
|
|
|
|
|
|
|
const struct decoder_plugin 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
|
|
|
};
|