From 352d7f477eaf8a6a8536feb059a32a8b61ccb9ca Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 28 Jul 2013 12:20:50 +0200 Subject: decoder/{dsf,dsdiff}: convert to C++ --- Makefile.am | 12 +- src/DecoderList.cxx | 4 +- src/decoder/DsdLib.cxx | 171 ++++++++++++ src/decoder/DsdLib.hxx | 51 ++++ src/decoder/DsdiffDecoderPlugin.cxx | 530 +++++++++++++++++++++++++++++++++++ src/decoder/DsdiffDecoderPlugin.hxx | 25 ++ src/decoder/DsfDecoderPlugin.cxx | 360 ++++++++++++++++++++++++ src/decoder/DsfDecoderPlugin.hxx | 25 ++ src/decoder/dsdiff_decoder_plugin.c | 536 ------------------------------------ src/decoder/dsdiff_decoder_plugin.h | 25 -- src/decoder/dsdlib.c | 170 ------------ src/decoder/dsdlib.h | 47 ---- src/decoder/dsf_decoder_plugin.c | 364 ------------------------ src/decoder/dsf_decoder_plugin.h | 25 -- 14 files changed, 1170 insertions(+), 1175 deletions(-) create mode 100644 src/decoder/DsdLib.cxx create mode 100644 src/decoder/DsdLib.hxx create mode 100644 src/decoder/DsdiffDecoderPlugin.cxx create mode 100644 src/decoder/DsdiffDecoderPlugin.hxx create mode 100644 src/decoder/DsfDecoderPlugin.cxx create mode 100644 src/decoder/DsfDecoderPlugin.hxx delete mode 100644 src/decoder/dsdiff_decoder_plugin.c delete mode 100644 src/decoder/dsdiff_decoder_plugin.h delete mode 100644 src/decoder/dsdlib.c delete mode 100644 src/decoder/dsdlib.h delete mode 100644 src/decoder/dsf_decoder_plugin.c delete mode 100644 src/decoder/dsf_decoder_plugin.h diff --git a/Makefile.am b/Makefile.am index 563c5f6eb..67c159cb3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -448,12 +448,12 @@ endif libdecoder_plugins_a_SOURCES = \ src/decoder/pcm_decoder_plugin.c \ - src/decoder/dsdiff_decoder_plugin.c \ - src/decoder/dsdiff_decoder_plugin.h \ - src/decoder/dsf_decoder_plugin.c \ - src/decoder/dsf_decoder_plugin.h \ - src/decoder/dsdlib.c \ - src/decoder/dsdlib.h \ + src/decoder/DsdiffDecoderPlugin.cxx \ + src/decoder/DsdiffDecoderPlugin.hxx \ + src/decoder/DsfDecoderPlugin.cxx \ + src/decoder/DsfDecoderPlugin.hxx \ + src/decoder/DsdLib.cxx \ + src/decoder/DsdLib.hxx \ src/DecoderBuffer.cxx src/DecoderBuffer.hxx \ src/DecoderPlugin.cxx \ src/DecoderList.cxx src/DecoderList.hxx diff --git a/src/DecoderList.cxx b/src/DecoderList.cxx index 3cde7db72..d8035a737 100644 --- a/src/DecoderList.cxx +++ b/src/DecoderList.cxx @@ -23,8 +23,8 @@ #include "conf.h" #include "mpd_error.h" #include "decoder/pcm_decoder_plugin.h" -#include "decoder/dsdiff_decoder_plugin.h" -#include "decoder/dsf_decoder_plugin.h" +#include "decoder/DsdiffDecoderPlugin.hxx" +#include "decoder/DsfDecoderPlugin.hxx" #include "decoder/FlacDecoderPlugin.h" #include "decoder/OpusDecoderPlugin.h" #include "decoder/VorbisDecoderPlugin.h" diff --git a/src/decoder/DsdLib.cxx b/src/decoder/DsdLib.cxx new file mode 100644 index 000000000..dd7ea70c9 --- /dev/null +++ b/src/decoder/DsdLib.cxx @@ -0,0 +1,171 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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. + */ + +/* \file + * + * This file contains functions used by the DSF and DSDIFF decoders. + * + */ + +#include "config.h" +#include "DsdLib.hxx" +#include "decoder_api.h" +#include "util/bit_reverse.h" +#include "tag_handler.h" + +extern "C" { +#include "tag_id3.h" +} + +#include +#include /* for SEEK_SET, SEEK_CUR */ + +#ifdef HAVE_ID3TAG +#include +#endif + +bool +dsdlib_id_equals(const struct dsdlib_id *id, const char *s) +{ + assert(id != nullptr); + assert(s != nullptr); + assert(strlen(s) == sizeof(id->value)); + + return memcmp(id->value, s, sizeof(id->value)) == 0; +} + +bool +dsdlib_read(struct decoder *decoder, struct input_stream *is, + void *data, size_t length) +{ + size_t nbytes = decoder_read(decoder, is, data, length); + return nbytes == length; +} + +/** + * Skip the #input_stream to the specified offset. + */ +bool +dsdlib_skip_to(struct decoder *decoder, struct input_stream *is, + goffset offset) +{ + if (input_stream_is_seekable(is)) + return input_stream_seek(is, offset, SEEK_SET, nullptr); + + if (input_stream_get_offset(is) > offset) + return false; + + char buffer[8192]; + while (input_stream_get_offset(is) < offset) { + size_t length = sizeof(buffer); + if (offset - input_stream_get_offset(is) < (goffset)length) + length = offset - input_stream_get_offset(is); + + size_t nbytes = decoder_read(decoder, is, buffer, length); + if (nbytes == 0) + return false; + } + + assert(input_stream_get_offset(is) == offset); + return true; +} + +/** + * Skip some bytes from the #input_stream. + */ +bool +dsdlib_skip(struct decoder *decoder, struct input_stream *is, + goffset delta) +{ + assert(delta >= 0); + + if (delta == 0) + return true; + + if (input_stream_is_seekable(is)) + return input_stream_seek(is, delta, SEEK_CUR, nullptr); + + char buffer[8192]; + while (delta > 0) { + size_t length = sizeof(buffer); + if ((goffset)length > delta) + length = delta; + + size_t nbytes = decoder_read(decoder, is, buffer, length); + if (nbytes == 0) + return false; + + delta -= nbytes; + } + + return true; +} + +/** + * Add tags from ID3 tag. All tags commonly found in the ID3 tags of + * DSF and DSDIFF files are imported + */ + +#ifdef HAVE_ID3TAG +void +dsdlib_tag_id3(struct input_stream *is, + const struct tag_handler *handler, + void *handler_ctx, goffset tagoffset) +{ + assert(tagoffset >= 0); + + if (tagoffset == 0) + return; + + if (!dsdlib_skip_to(nullptr, is, tagoffset)) + return; + + struct id3_tag *id3_tag = nullptr; + id3_length_t count; + + /* Prevent broken files causing problems */ + const goffset size = input_stream_get_size(is); + const goffset offset = input_stream_get_offset(is); + if (offset >= size) + return; + + count = size - offset; + + /* Check and limit id3 tag size to prevent a stack overflow */ + if (count == 0 || count > 4096) + return; + + id3_byte_t dsdid3[count]; + id3_byte_t *dsdid3data; + dsdid3data = dsdid3; + + if (!dsdlib_read(nullptr, is, dsdid3data, count)) + return; + + id3_tag = id3_tag_parse(dsdid3data, count); + if (id3_tag == nullptr) + return; + + scan_id3_tag(id3_tag, handler, handler_ctx); + + id3_tag_delete(id3_tag); + + return; +} +#endif diff --git a/src/decoder/DsdLib.hxx b/src/decoder/DsdLib.hxx new file mode 100644 index 000000000..2a8e15190 --- /dev/null +++ b/src/decoder/DsdLib.hxx @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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. + */ + +#ifndef MPD_DECODER_DSDLIB_HXX +#define MPD_DECODER_DSDLIB_HXX + +#include + +#include + +struct dsdlib_id { + char value[4]; +}; + +bool +dsdlib_id_equals(const struct dsdlib_id *id, const char *s); + +bool +dsdlib_read(struct decoder *decoder, struct input_stream *is, + void *data, size_t length); + +bool +dsdlib_skip_to(struct decoder *decoder, struct input_stream *is, + goffset offset); + +bool +dsdlib_skip(struct decoder *decoder, struct input_stream *is, + goffset delta); + +void +dsdlib_tag_id3(struct input_stream *is, + const struct tag_handler *handler, + void *handler_ctx, goffset tagoffset); + +#endif diff --git a/src/decoder/DsdiffDecoderPlugin.cxx b/src/decoder/DsdiffDecoderPlugin.cxx new file mode 100644 index 000000000..ed2a533b0 --- /dev/null +++ b/src/decoder/DsdiffDecoderPlugin.cxx @@ -0,0 +1,530 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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. + */ + +/* \file + * + * This plugin decodes DSDIFF data (SACD) embedded in DFF files. + * The DFF code was modeled after the specification found here: + * http://www.sonicstudio.com/pdf/dsd/DSDIFF_1.5_Spec.pdf + * + * All functions common to both DSD decoders have been moved to dsdlib + */ + +#include "config.h" +#include "DsdiffDecoderPlugin.hxx" +#include "decoder_api.h" +#include "audio_check.h" +#include "util/bit_reverse.h" +#include "tag_handler.h" +#include "DsdLib.hxx" +#include "tag_handler.h" + +#include +#include /* for SEEK_SET, SEEK_CUR */ + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "dsdiff" + +struct DsdiffHeader { + struct dsdlib_id id; + uint32_t size_high, size_low; + struct dsdlib_id format; +}; + +struct DsdiffChunkHeader { + struct dsdlib_id id; + uint32_t size_high, size_low; + + /** + * 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)); + } +}; + +/** struct for DSDIFF native Artist and Title tags */ +struct dsdiff_native_tag { + uint32_t size; +}; + +struct DsdiffMetaData { + unsigned sample_rate, channels; + bool bitreverse; + uint64_t chunk_size; +#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; +}; + +static bool lsbitfirst; + +static bool +dsdiff_init(const struct config_param *param) +{ + lsbitfirst = config_get_block_bool(param, "lsbitfirst", false); + return true; +} + +static bool +dsdiff_read_id(struct decoder *decoder, struct input_stream *is, + struct dsdlib_id *id) +{ + return dsdlib_read(decoder, is, id, sizeof(*id)); +} + +static bool +dsdiff_read_chunk_header(struct decoder *decoder, struct input_stream *is, + DsdiffChunkHeader *header) +{ + return dsdlib_read(decoder, is, header, sizeof(*header)); +} + +static bool +dsdiff_read_payload(struct decoder *decoder, struct input_stream *is, + const DsdiffChunkHeader *header, + void *data, size_t length) +{ + uint64_t size = header->GetSize(); + 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, + DsdiffMetaData *metadata, + goffset end_offset) +{ + DsdiffChunkHeader header; + while ((goffset)(input_stream_get_offset(is) + sizeof(header)) <= end_offset) { + if (!dsdiff_read_chunk_header(decoder, is, &header)) + return false; + + goffset chunk_end_offset = input_stream_get_offset(is) + + header.GetSize(); + if (chunk_end_offset > end_offset) + return false; + + if (dsdlib_id_equals(&header.id, "FS ")) { + 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); + } else if (dsdlib_id_equals(&header.id, "CHNL")) { + uint16_t channels; + if (header.GetSize() < sizeof(channels) || + !dsdlib_read(decoder, is, + &channels, sizeof(channels)) || + !dsdlib_skip_to(decoder, is, chunk_end_offset)) + return false; + + metadata->channels = GUINT16_FROM_BE(channels); + } else if (dsdlib_id_equals(&header.id, "CMPR")) { + struct dsdlib_id type; + if (header.GetSize() < sizeof(type) || + !dsdlib_read(decoder, is, + &type, sizeof(type)) || + !dsdlib_skip_to(decoder, is, chunk_end_offset)) + return false; + + if (!dsdlib_id_equals(&type, "DSD ")) + /* only uncompressed DSD audio data + is implemented */ + return false; + } else { + /* ignore unknown chunk */ + + if (!dsdlib_skip_to(decoder, is, chunk_end_offset)) + return false; + } + } + + return input_stream_get_offset(is) == end_offset; +} + +/** + * Read and parse a "PROP" chunk. + */ +static bool +dsdiff_read_prop(struct decoder *decoder, struct input_stream *is, + DsdiffMetaData *metadata, + const DsdiffChunkHeader *prop_header) +{ + uint64_t prop_size = prop_header->GetSize(); + goffset end_offset = input_stream_get_offset(is) + prop_size; + + struct dsdlib_id prop_id; + if (prop_size < sizeof(prop_id) || + !dsdiff_read_id(decoder, is, &prop_id)) + return false; + + if (dsdlib_id_equals(&prop_id, "SND ")) + return dsdiff_read_prop_snd(decoder, is, metadata, end_offset); + else + /* ignore unknown PROP chunk */ + return dsdlib_skip_to(decoder, is, end_offset); +} + +static void +dsdiff_handle_native_tag(struct input_stream *is, + const struct tag_handler *handler, + void *handler_ctx, goffset tagoffset, + enum tag_type type) +{ + if (!dsdlib_skip_to(nullptr, is, tagoffset)) + return; + + struct dsdiff_native_tag metatag; + + if (!dsdlib_read(nullptr, is, &metatag, sizeof(metatag))) + 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; + + if (!dsdlib_read(nullptr, is, label, (size_t)length)) + 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, + DsdiffMetaData *metadata, + DsdiffChunkHeader *chunk_header, + 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 */ + + const goffset size = input_stream_get_size(is); + while (input_stream_get_offset(is) < size) { + uint64_t chunk_size = chunk_header->GetSize(); + + /* 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")) { + chunk_size = chunk_header->GetSize(); + metadata->diar_offset = input_stream_get_offset(is); + } + + /* DITI chunk - DSDIFF native tag for Title */ + if (dsdlib_id_equals(&chunk_header->id, "DITI")) { + chunk_size = chunk_header->GetSize(); + metadata->diti_offset = input_stream_get_offset(is); + } +#ifdef HAVE_ID3TAG + /* 'ID3 ' chunk, offspec. Used by sacdextract */ + if (dsdlib_id_equals(&chunk_header->id, "ID3 ")) { + chunk_size = chunk_header->GetSize(); + metadata->id3_offset = input_stream_get_offset(is); + metadata->id3_size = chunk_size; + } +#endif + if (chunk_size != 0) { + if (!dsdlib_skip(decoder, is, chunk_size)) + break; + } + + if (input_stream_get_offset(is) < size) { + 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; +} + +/** + * 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, + DsdiffMetaData *metadata, + DsdiffChunkHeader *chunk_header) +{ + DsdiffHeader header; + if (!dsdlib_read(decoder, is, &header, sizeof(header)) || + !dsdlib_id_equals(&header.id, "FRM8") || + !dsdlib_id_equals(&header.format, "DSD ")) + return false; + + while (true) { + if (!dsdiff_read_chunk_header(decoder, is, + chunk_header)) + return false; + + if (dsdlib_id_equals(&chunk_header->id, "PROP")) { + if (!dsdiff_read_prop(decoder, is, metadata, + chunk_header)) + return false; + } else if (dsdlib_id_equals(&chunk_header->id, "DSD ")) { + const uint64_t chunk_size = chunk_header->GetSize(); + metadata->chunk_size = chunk_size; + return true; + } else { + /* ignore unknown chunk */ + const uint64_t chunk_size = chunk_header->GetSize(); + goffset chunk_end_offset = input_stream_get_offset(is) + + chunk_size; + + if (!dsdlib_skip_to(decoder, is, chunk_end_offset)) + return false; + } + } +} + +static void +bit_reverse_buffer(uint8_t *p, uint8_t *end) +{ + for (; p < end; ++p) + *p = bit_reverse(*p); +} + +/** + * Decode one "DSD" chunk. + */ +static bool +dsdiff_decode_chunk(struct decoder *decoder, struct input_stream *is, + unsigned channels, + uint64_t chunk_size) +{ + uint8_t buffer[8192]; + + 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; + + if (lsbitfirst) + bit_reverse_buffer(buffer, buffer + nbytes); + + enum decoder_command cmd = + decoder_data(decoder, is, buffer, nbytes, 0); + switch (cmd) { + case DECODE_COMMAND_NONE: + break; + + case DECODE_COMMAND_START: + case DECODE_COMMAND_STOP: + return false; + + case DECODE_COMMAND_SEEK: + + /* Not implemented yet */ + decoder_seek_error(decoder); + break; + } + } + return dsdlib_skip(decoder, is, chunk_size); +} + +static void +dsdiff_stream_decode(struct decoder *decoder, struct input_stream *is) +{ + DsdiffMetaData metadata; + + DsdiffChunkHeader chunk_header; + /* check if it is is a proper DFF file */ + if (!dsdiff_read_metadata(decoder, is, &metadata, &chunk_header)) + return; + + GError *error = nullptr; + struct audio_format audio_format; + if (!audio_format_init_checked(&audio_format, metadata.sample_rate / 8, + SAMPLE_FORMAT_DSD, + metadata.channels, &error)) { + g_warning("%s", error->message); + g_error_free(error); + return; + } + + /* 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; + + /* success: file was recognized */ + decoder_initialized(decoder, &audio_format, false, songtime); + + /* every iteration of the following loop decodes one "DSD" + chunk from a DFF file */ + + while (true) { + chunk_size = chunk_header.GetSize(); + + if (dsdlib_id_equals(&chunk_header.id, "DSD ")) { + if (!dsdiff_decode_chunk(decoder, is, + metadata.channels, + chunk_size)) + break; + } else { + /* ignore other chunks */ + if (!dsdlib_skip(decoder, is, chunk_size)) + break; + } + + /* read next chunk header; the first one was read by + dsdiff_read_metadata() */ + if (!dsdiff_read_chunk_header(decoder, + is, &chunk_header)) + break; + } +} + +static bool +dsdiff_scan_stream(struct input_stream *is, + G_GNUC_UNUSED const struct tag_handler *handler, + G_GNUC_UNUSED void *handler_ctx) +{ + DsdiffMetaData metadata; + DsdiffChunkHeader chunk_header; + + /* First check for DFF metadata */ + if (!dsdiff_read_metadata(nullptr, is, &metadata, &chunk_header)) + return false; + + struct audio_format audio_format; + if (!audio_format_init_checked(&audio_format, metadata.sample_rate / 8, + SAMPLE_FORMAT_DSD, + metadata.channels, nullptr)) + /* refuse to parse files which we cannot play anyway */ + return false; + + /* 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); + + /* Read additional metadata and created tags if available */ + dsdiff_read_metadata_extra(nullptr, is, &metadata, &chunk_header, + handler, handler_ctx); + + return true; +} + +static const char *const dsdiff_suffixes[] = { + "dff", + nullptr +}; + +static const char *const dsdiff_mime_types[] = { + "application/x-dff", + nullptr +}; + +const struct decoder_plugin dsdiff_decoder_plugin = { + "dsdiff", + dsdiff_init, + nullptr, + dsdiff_stream_decode, + nullptr, + nullptr, + dsdiff_scan_stream, + nullptr, + dsdiff_suffixes, + dsdiff_mime_types, +}; diff --git a/src/decoder/DsdiffDecoderPlugin.hxx b/src/decoder/DsdiffDecoderPlugin.hxx new file mode 100644 index 000000000..c50605457 --- /dev/null +++ b/src/decoder/DsdiffDecoderPlugin.hxx @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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. + */ + +#ifndef MPD_DECODER_DSDIFF_H +#define MPD_DECODER_DSDIFF_H + +extern const struct decoder_plugin dsdiff_decoder_plugin; + +#endif diff --git a/src/decoder/DsfDecoderPlugin.cxx b/src/decoder/DsfDecoderPlugin.cxx new file mode 100644 index 000000000..a6f575d97 --- /dev/null +++ b/src/decoder/DsfDecoderPlugin.cxx @@ -0,0 +1,360 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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. + */ + +/* \file + * + * This plugin decodes DSDIFF data (SACD) embedded in DSF files. + * + * The DSF code was created using the specification found here: + * http://dsd-guide.com/sonys-dsf-file-format-spec + * + * All functions common to both DSD decoders have been moved to dsdlib + */ + +#include "config.h" +#include "DsfDecoderPlugin.hxx" +#include "decoder_api.h" +#include "audio_check.h" +#include "util/bit_reverse.h" +#include "DsdLib.hxx" +#include "tag_handler.h" + +#include +#include /* for SEEK_SET, SEEK_CUR */ + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "dsf" + +struct DsfMetaData { + unsigned sample_rate, channels; + bool bitreverse; + uint64_t chunk_size; +#ifdef HAVE_ID3TAG + goffset id3_offset; + uint64_t id3_size; +#endif +}; + +struct DsfHeader { + /** DSF header id: "DSD " */ + struct dsdlib_id id; + /** DSD chunk size, including id = 28 */ + uint32_t size_low, size_high; + /** total file size */ + uint32_t fsize_low, fsize_high; + /** pointer to id3v2 metadata, should be at the end of the file */ + uint32_t pmeta_low, pmeta_high; +}; + +/** DSF file fmt chunk */ +struct DsfFmtChunk { + /** id: "fmt " */ + struct dsdlib_id id; + /** fmt chunk size, including id, normally 52 */ + uint32_t size_low, size_high; + /** version of this format = 1 */ + uint32_t version; + /** 0: DSD raw */ + uint32_t formatid; + /** channel type, 1 = mono, 2 = stereo, 3 = 3 channels, etc */ + uint32_t channeltype; + /** Channel number, 1 = mono, 2 = stereo, ... 6 = 6 channels */ + uint32_t channelnum; + /** sample frequency: 2822400, 5644800 */ + uint32_t sample_freq; + /** bits per sample 1 or 8 */ + uint32_t bitssample; + /** Sample count per channel in bytes */ + uint32_t scnt_low, scnt_high; + /** block size per channel = 4096 */ + uint32_t block_size; + /** reserved, should be all zero */ + uint32_t reserved; +}; + +struct DsfDataChunk { + struct dsdlib_id id; + /** "data" chunk size, includes header (id+size) */ + uint32_t size_low, size_high; +}; + +/** + * Read and parse all needed metadata chunks for DSF files. + */ +static bool +dsf_read_metadata(struct decoder *decoder, struct input_stream *is, + DsfMetaData *metadata) +{ + uint64_t chunk_size; + DsfHeader dsf_header; + if (!dsdlib_read(decoder, is, &dsf_header, sizeof(dsf_header)) || + !dsdlib_id_equals(&dsf_header.id, "DSD ")) + return false; + + chunk_size = (((uint64_t)GUINT32_FROM_LE(dsf_header.size_high)) << 32) | + ((uint64_t)GUINT32_FROM_LE(dsf_header.size_low)); + + if (sizeof(dsf_header) != chunk_size) + return false; + +#ifdef HAVE_ID3TAG + uint64_t metadata_offset; + metadata_offset = (((uint64_t)GUINT32_FROM_LE(dsf_header.pmeta_high)) << 32) | + ((uint64_t)GUINT32_FROM_LE(dsf_header.pmeta_low)); +#endif + + /* read the 'fmt ' chunk of the DSF file */ + DsfFmtChunk dsf_fmt_chunk; + if (!dsdlib_read(decoder, is, &dsf_fmt_chunk, sizeof(dsf_fmt_chunk)) || + !dsdlib_id_equals(&dsf_fmt_chunk.id, "fmt ")) + return false; + + uint64_t fmt_chunk_size; + fmt_chunk_size = (((uint64_t)GUINT32_FROM_LE(dsf_fmt_chunk.size_high)) << 32) | + ((uint64_t)GUINT32_FROM_LE(dsf_fmt_chunk.size_low)); + + if (fmt_chunk_size != sizeof(dsf_fmt_chunk)) + return false; + + uint32_t samplefreq = (uint32_t)GUINT32_FROM_LE(dsf_fmt_chunk.sample_freq); + + /* for now, only support version 1 of the standard, DSD raw stereo + files with a sample freq of 2822400 Hz */ + + if (dsf_fmt_chunk.version != 1 || dsf_fmt_chunk.formatid != 0 + || dsf_fmt_chunk.channeltype != 2 + || dsf_fmt_chunk.channelnum != 2 + || samplefreq != 2822400) + return false; + + uint32_t chblksize = (uint32_t)GUINT32_FROM_LE(dsf_fmt_chunk.block_size); + /* according to the spec block size should always be 4096 */ + if (chblksize != 4096) + return false; + + /* read the 'data' chunk of the DSF file */ + DsfDataChunk data_chunk; + if (!dsdlib_read(decoder, is, &data_chunk, sizeof(data_chunk)) || + !dsdlib_id_equals(&data_chunk.id, "data")) + return false; + + /* data size of DSF files are padded to multiple of 4096, + we use the actual data size as chunk size */ + + uint64_t data_size; + data_size = (((uint64_t)GUINT32_FROM_LE(data_chunk.size_high)) << 32) | + ((uint64_t)GUINT32_FROM_LE(data_chunk.size_low)); + data_size -= sizeof(data_chunk); + + metadata->chunk_size = data_size; + /* data_size cannot be bigger or equal to total file size */ + const uint64_t size = (uint64_t)input_stream_get_size(is); + if (data_size >= size) + return false; + + metadata->channels = (unsigned) dsf_fmt_chunk.channelnum; + metadata->sample_rate = samplefreq; +#ifdef HAVE_ID3TAG + /* metada_offset cannot be bigger then or equal to total file size */ + if (metadata_offset >= size) + metadata->id3_offset = 0; + else + metadata->id3_offset = (goffset) metadata_offset; +#endif + /* check bits per sample format, determine if bitreverse is needed */ + metadata->bitreverse = dsf_fmt_chunk.bitssample == 1; + return true; +} + +static void +bit_reverse_buffer(uint8_t *p, uint8_t *end) +{ + for (; p < end; ++p) + *p = bit_reverse(*p); +} + +/** + * DSF data is build up of alternating 4096 blocks of DSD samples for left and + * right. Convert the buffer holding 1 block of 4096 DSD left samples and 1 + * block of 4096 DSD right samples to 8k of samples in normal PCM left/right + * order. + */ +static void +dsf_to_pcm_order(uint8_t *dest, uint8_t *scratch, size_t nrbytes) +{ + for (unsigned i = 0, j = 0; i < (unsigned)nrbytes; i += 2) { + scratch[i] = *(dest+j); + j++; + } + + for (unsigned i = 1, j = 0; i < (unsigned) nrbytes; i += 2) { + scratch[i] = *(dest+4096+j); + j++; + } + + for (unsigned i = 0; i < (unsigned)nrbytes; i++) { + *dest = scratch[i]; + dest++; + } +} + +/** + * Decode one complete DSF 'data' chunk i.e. a complete song + */ +static bool +dsf_decode_chunk(struct decoder *decoder, struct input_stream *is, + unsigned channels, + uint64_t chunk_size, + bool bitreverse) +{ + uint8_t buffer[8192]; + + /* scratch buffer for DSF samples to convert to the needed + normal left/right regime of samples */ + uint8_t dsf_scratch_buffer[8192]; + + 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; + + if (bitreverse) + bit_reverse_buffer(buffer, buffer + nbytes); + + dsf_to_pcm_order(buffer, dsf_scratch_buffer, nbytes); + + enum decoder_command cmd = + decoder_data(decoder, is, buffer, nbytes, 0); + switch (cmd) { + case DECODE_COMMAND_NONE: + break; + + case DECODE_COMMAND_START: + case DECODE_COMMAND_STOP: + return false; + + case DECODE_COMMAND_SEEK: + + /* not implemented yet */ + decoder_seek_error(decoder); + break; + } + } + return dsdlib_skip(decoder, is, chunk_size); +} + +static void +dsf_stream_decode(struct decoder *decoder, struct input_stream *is) +{ + /* check if it is a proper DSF file */ + DsfMetaData metadata; + if (!dsf_read_metadata(decoder, is, &metadata)) + return; + + GError *error = NULL; + struct audio_format audio_format; + if (!audio_format_init_checked(&audio_format, metadata.sample_rate / 8, + SAMPLE_FORMAT_DSD, + metadata.channels, &error)) { + g_warning("%s", error->message); + g_error_free(error); + return; + } + /* 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; + + /* success: file was recognized */ + decoder_initialized(decoder, &audio_format, false, songtime); + + if (!dsf_decode_chunk(decoder, is, metadata.channels, + chunk_size, + metadata.bitreverse)) + return; +} + +static bool +dsf_scan_stream(struct input_stream *is, + G_GNUC_UNUSED const struct tag_handler *handler, + G_GNUC_UNUSED void *handler_ctx) +{ + /* check DSF metadata */ + DsfMetaData metadata; + if (!dsf_read_metadata(NULL, is, &metadata)) + return false; + + struct audio_format audio_format; + if (!audio_format_init_checked(&audio_format, metadata.sample_rate / 8, + SAMPLE_FORMAT_DSD, + metadata.channels, NULL)) + /* refuse to parse files which we cannot play anyway */ + return false; + + /* 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); + +#ifdef HAVE_ID3TAG + /* Add available tags from the ID3 tag */ + dsdlib_tag_id3(is, handler, handler_ctx, metadata.id3_offset); +#endif + return true; +} + +static const char *const dsf_suffixes[] = { + "dsf", + NULL +}; + +static const char *const dsf_mime_types[] = { + "application/x-dsf", + NULL +}; + +const struct decoder_plugin dsf_decoder_plugin = { + "dsf", + nullptr, + nullptr, + dsf_stream_decode, + nullptr, + nullptr, + dsf_scan_stream, + nullptr, + dsf_suffixes, + dsf_mime_types, +}; diff --git a/src/decoder/DsfDecoderPlugin.hxx b/src/decoder/DsfDecoderPlugin.hxx new file mode 100644 index 000000000..749032d1f --- /dev/null +++ b/src/decoder/DsfDecoderPlugin.hxx @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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. + */ + +#ifndef MPD_DECODER_DSF_H +#define MPD_DECODER_DSF_H + +extern const struct decoder_plugin dsf_decoder_plugin; + +#endif diff --git a/src/decoder/dsdiff_decoder_plugin.c b/src/decoder/dsdiff_decoder_plugin.c deleted file mode 100644 index 44d12d899..000000000 --- a/src/decoder/dsdiff_decoder_plugin.c +++ /dev/null @@ -1,536 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * 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. - */ - -/* \file - * - * This plugin decodes DSDIFF data (SACD) embedded in DFF files. - * The DFF code was modeled after the specification found here: - * http://www.sonicstudio.com/pdf/dsd/DSDIFF_1.5_Spec.pdf - * - * All functions common to both DSD decoders have been moved to dsdlib - */ - -#include "config.h" -#include "dsdiff_decoder_plugin.h" -#include "decoder_api.h" -#include "audio_check.h" -#include "util/bit_reverse.h" -#include "tag_handler.h" -#include "dsdlib.h" -#include "tag_handler.h" - -#include -#include /* for SEEK_SET, SEEK_CUR */ - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "dsdiff" - -struct dsdiff_header { - struct dsdlib_id id; - uint32_t size_high, size_low; - struct dsdlib_id format; -}; - -struct dsdiff_chunk_header { - struct dsdlib_id id; - uint32_t size_high, size_low; -}; - -/** struct for DSDIFF native Artist and Title tags */ -struct dsdiff_native_tag { - uint32_t size; -}; - -struct dsdiff_metadata { - unsigned sample_rate, channels; - bool bitreverse; - uint64_t chunk_size; -#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; -}; - -static bool lsbitfirst; - -static bool -dsdiff_init(const struct config_param *param) -{ - lsbitfirst = config_get_block_bool(param, "lsbitfirst", false); - return true; -} - -/** - * Read the "size" attribute from the specified header, converting it - * to the host byte order if needed. - */ -G_GNUC_CONST -static uint64_t -dsdiff_chunk_size(const struct dsdiff_chunk_header *header) -{ - return (((uint64_t)GUINT32_FROM_BE(header->size_high)) << 32) | - ((uint64_t)GUINT32_FROM_BE(header->size_low)); -} - -static bool -dsdiff_read_id(struct decoder *decoder, struct input_stream *is, - struct dsdlib_id *id) -{ - return dsdlib_read(decoder, is, id, sizeof(*id)); -} - -static bool -dsdiff_read_chunk_header(struct decoder *decoder, struct input_stream *is, - struct dsdiff_chunk_header *header) -{ - return dsdlib_read(decoder, is, header, sizeof(*header)); -} - -static bool -dsdiff_read_payload(struct decoder *decoder, struct input_stream *is, - const struct dsdiff_chunk_header *header, - void *data, size_t length) -{ - uint64_t size = dsdiff_chunk_size(header); - 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, - struct dsdiff_metadata *metadata, - goffset end_offset) -{ - struct dsdiff_chunk_header header; - while ((goffset)(input_stream_get_offset(is) + sizeof(header)) <= end_offset) { - if (!dsdiff_read_chunk_header(decoder, is, &header)) - return false; - - goffset chunk_end_offset = input_stream_get_offset(is) - + dsdiff_chunk_size(&header); - if (chunk_end_offset > end_offset) - return false; - - if (dsdlib_id_equals(&header.id, "FS ")) { - 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); - } else if (dsdlib_id_equals(&header.id, "CHNL")) { - uint16_t channels; - if (dsdiff_chunk_size(&header) < sizeof(channels) || - !dsdlib_read(decoder, is, - &channels, sizeof(channels)) || - !dsdlib_skip_to(decoder, is, chunk_end_offset)) - return false; - - metadata->channels = GUINT16_FROM_BE(channels); - } else if (dsdlib_id_equals(&header.id, "CMPR")) { - struct dsdlib_id type; - if (dsdiff_chunk_size(&header) < sizeof(type) || - !dsdlib_read(decoder, is, - &type, sizeof(type)) || - !dsdlib_skip_to(decoder, is, chunk_end_offset)) - return false; - - if (!dsdlib_id_equals(&type, "DSD ")) - /* only uncompressed DSD audio data - is implemented */ - return false; - } else { - /* ignore unknown chunk */ - - if (!dsdlib_skip_to(decoder, is, chunk_end_offset)) - return false; - } - } - - return input_stream_get_offset(is) == end_offset; -} - -/** - * Read and parse a "PROP" chunk. - */ -static bool -dsdiff_read_prop(struct decoder *decoder, struct input_stream *is, - struct dsdiff_metadata *metadata, - const struct dsdiff_chunk_header *prop_header) -{ - uint64_t prop_size = dsdiff_chunk_size(prop_header); - goffset end_offset = input_stream_get_offset(is) + prop_size; - - struct dsdlib_id prop_id; - if (prop_size < sizeof(prop_id) || - !dsdiff_read_id(decoder, is, &prop_id)) - return false; - - if (dsdlib_id_equals(&prop_id, "SND ")) - return dsdiff_read_prop_snd(decoder, is, metadata, end_offset); - else - /* ignore unknown PROP chunk */ - return dsdlib_skip_to(decoder, is, end_offset); -} - -static void -dsdiff_handle_native_tag(struct input_stream *is, - const struct tag_handler *handler, - void *handler_ctx, goffset tagoffset, - enum tag_type type) -{ - if (!dsdlib_skip_to(NULL, is, tagoffset)) - return; - - struct dsdiff_native_tag metatag; - - if (!dsdlib_read(NULL, is, &metatag, sizeof(metatag))) - 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; - - if (!dsdlib_read(NULL, is, label, (size_t)length)) - 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, - struct dsdiff_metadata *metadata, - struct dsdiff_chunk_header *chunk_header, - 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 */ - - const goffset size = input_stream_get_size(is); - while (input_stream_get_offset(is) < size) { - uint64_t chunk_size = dsdiff_chunk_size(chunk_header); - - /* 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")) { - chunk_size = dsdiff_chunk_size(chunk_header); - metadata->diar_offset = input_stream_get_offset(is); - } - - /* DITI chunk - DSDIFF native tag for Title */ - if (dsdlib_id_equals(&chunk_header->id, "DITI")) { - chunk_size = dsdiff_chunk_size(chunk_header); - metadata->diti_offset = input_stream_get_offset(is); - } -#ifdef HAVE_ID3TAG - /* 'ID3 ' chunk, offspec. Used by sacdextract */ - if (dsdlib_id_equals(&chunk_header->id, "ID3 ")) { - chunk_size = dsdiff_chunk_size(chunk_header); - metadata->id3_offset = input_stream_get_offset(is); - metadata->id3_size = chunk_size; - } -#endif - if (chunk_size != 0) { - if (!dsdlib_skip(decoder, is, chunk_size)) - break; - } - - if (input_stream_get_offset(is) < size) { - 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; -} - -/** - * 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, - struct dsdiff_metadata *metadata, - struct dsdiff_chunk_header *chunk_header) -{ - struct dsdiff_header header; - if (!dsdlib_read(decoder, is, &header, sizeof(header)) || - !dsdlib_id_equals(&header.id, "FRM8") || - !dsdlib_id_equals(&header.format, "DSD ")) - return false; - - while (true) { - if (!dsdiff_read_chunk_header(decoder, is, - chunk_header)) - return false; - - if (dsdlib_id_equals(&chunk_header->id, "PROP")) { - if (!dsdiff_read_prop(decoder, is, metadata, - chunk_header)) - return false; - } else if (dsdlib_id_equals(&chunk_header->id, "DSD ")) { - uint64_t chunk_size; - chunk_size = dsdiff_chunk_size(chunk_header); - metadata->chunk_size = chunk_size; - return true; - } else { - /* ignore unknown chunk */ - uint64_t chunk_size; - chunk_size = dsdiff_chunk_size(chunk_header); - goffset chunk_end_offset = input_stream_get_offset(is) - + chunk_size; - - if (!dsdlib_skip_to(decoder, is, chunk_end_offset)) - return false; - } - } -} - -static void -bit_reverse_buffer(uint8_t *p, uint8_t *end) -{ - for (; p < end; ++p) - *p = bit_reverse(*p); -} - -/** - * Decode one "DSD" chunk. - */ -static bool -dsdiff_decode_chunk(struct decoder *decoder, struct input_stream *is, - unsigned channels, - uint64_t chunk_size) -{ - uint8_t buffer[8192]; - - 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; - - if (lsbitfirst) - bit_reverse_buffer(buffer, buffer + nbytes); - - enum decoder_command cmd = - decoder_data(decoder, is, buffer, nbytes, 0); - switch (cmd) { - case DECODE_COMMAND_NONE: - break; - - case DECODE_COMMAND_START: - case DECODE_COMMAND_STOP: - return false; - - case DECODE_COMMAND_SEEK: - - /* Not implemented yet */ - decoder_seek_error(decoder); - break; - } - } - return dsdlib_skip(decoder, is, chunk_size); -} - -static void -dsdiff_stream_decode(struct decoder *decoder, struct input_stream *is) -{ - struct dsdiff_metadata metadata = { - .sample_rate = 0, - .channels = 0, - }; - - struct dsdiff_chunk_header chunk_header; - /* check if it is is a proper DFF file */ - if (!dsdiff_read_metadata(decoder, is, &metadata, &chunk_header)) - return; - - GError *error = NULL; - struct audio_format audio_format; - if (!audio_format_init_checked(&audio_format, metadata.sample_rate / 8, - SAMPLE_FORMAT_DSD, - metadata.channels, &error)) { - g_warning("%s", error->message); - g_error_free(error); - return; - } - - /* 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; - - /* success: file was recognized */ - decoder_initialized(decoder, &audio_format, false, songtime); - - /* every iteration of the following loop decodes one "DSD" - chunk from a DFF file */ - - while (true) { - chunk_size = dsdiff_chunk_size(&chunk_header); - - if (dsdlib_id_equals(&chunk_header.id, "DSD ")) { - if (!dsdiff_decode_chunk(decoder, is, - metadata.channels, - chunk_size)) - break; - } else { - /* ignore other chunks */ - if (!dsdlib_skip(decoder, is, chunk_size)) - break; - } - - /* read next chunk header; the first one was read by - dsdiff_read_metadata() */ - if (!dsdiff_read_chunk_header(decoder, - is, &chunk_header)) - break; - } -} - -static bool -dsdiff_scan_stream(struct input_stream *is, - G_GNUC_UNUSED const struct tag_handler *handler, - G_GNUC_UNUSED void *handler_ctx) -{ - struct dsdiff_metadata metadata = { - .sample_rate = 0, - .channels = 0, - }; - - struct dsdiff_chunk_header chunk_header; - /* First check for DFF metadata */ - if (!dsdiff_read_metadata(NULL, is, &metadata, &chunk_header)) - return false; - - struct audio_format audio_format; - if (!audio_format_init_checked(&audio_format, metadata.sample_rate / 8, - SAMPLE_FORMAT_DSD, - metadata.channels, NULL)) - /* refuse to parse files which we cannot play anyway */ - return false; - - /* 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); - - /* Read additional metadata and created tags if available */ - dsdiff_read_metadata_extra(NULL, is, &metadata, &chunk_header, - handler, handler_ctx); - - return true; -} - -static const char *const dsdiff_suffixes[] = { - "dff", - NULL -}; - -static const char *const dsdiff_mime_types[] = { - "application/x-dff", - NULL -}; - -const struct decoder_plugin dsdiff_decoder_plugin = { - .name = "dsdiff", - .init = dsdiff_init, - .stream_decode = dsdiff_stream_decode, - .scan_stream = dsdiff_scan_stream, - .suffixes = dsdiff_suffixes, - .mime_types = dsdiff_mime_types, -}; diff --git a/src/decoder/dsdiff_decoder_plugin.h b/src/decoder/dsdiff_decoder_plugin.h deleted file mode 100644 index 452f9050b..000000000 --- a/src/decoder/dsdiff_decoder_plugin.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * 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. - */ - -#ifndef MPD_DECODER_DSDIFF_H -#define MPD_DECODER_DSDIFF_H - -extern const struct decoder_plugin dsdiff_decoder_plugin; - -#endif diff --git a/src/decoder/dsdlib.c b/src/decoder/dsdlib.c deleted file mode 100644 index d3043fb05..000000000 --- a/src/decoder/dsdlib.c +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright (C) 2012 The Music Player Daemon Project - * 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. - */ - -/* \file - * - * This file contains functions used by the DSF and DSDIFF decoders. - * - */ - -#include "config.h" -#include "dsf_decoder_plugin.h" -#include "decoder_api.h" -#include "util/bit_reverse.h" -#include "tag_handler.h" -#include "tag_id3.h" -#include "dsdlib.h" -#include "dsdiff_decoder_plugin.h" - -#include -#include /* for SEEK_SET, SEEK_CUR */ - -#ifdef HAVE_ID3TAG -#include -#endif - -bool -dsdlib_id_equals(const struct dsdlib_id *id, const char *s) -{ - assert(id != NULL); - assert(s != NULL); - assert(strlen(s) == sizeof(id->value)); - - return memcmp(id->value, s, sizeof(id->value)) == 0; -} - -bool -dsdlib_read(struct decoder *decoder, struct input_stream *is, - void *data, size_t length) -{ - size_t nbytes = decoder_read(decoder, is, data, length); - return nbytes == length; -} - -/** - * Skip the #input_stream to the specified offset. - */ -bool -dsdlib_skip_to(struct decoder *decoder, struct input_stream *is, - goffset offset) -{ - if (input_stream_is_seekable(is)) - return input_stream_seek(is, offset, SEEK_SET, NULL); - - if (input_stream_get_offset(is) > offset) - return false; - - char buffer[8192]; - while (input_stream_get_offset(is) < offset) { - size_t length = sizeof(buffer); - if (offset - input_stream_get_offset(is) < (goffset)length) - length = offset - input_stream_get_offset(is); - - size_t nbytes = decoder_read(decoder, is, buffer, length); - if (nbytes == 0) - return false; - } - - assert(input_stream_get_offset(is) == offset); - return true; -} - -/** - * Skip some bytes from the #input_stream. - */ -bool -dsdlib_skip(struct decoder *decoder, struct input_stream *is, - goffset delta) -{ - assert(delta >= 0); - - if (delta == 0) - return true; - - if (input_stream_is_seekable(is)) - return input_stream_seek(is, delta, SEEK_CUR, NULL); - - char buffer[8192]; - while (delta > 0) { - size_t length = sizeof(buffer); - if ((goffset)length > delta) - length = delta; - - size_t nbytes = decoder_read(decoder, is, buffer, length); - if (nbytes == 0) - return false; - - delta -= nbytes; - } - - return true; -} - -/** - * Add tags from ID3 tag. All tags commonly found in the ID3 tags of - * DSF and DSDIFF files are imported - */ - -#ifdef HAVE_ID3TAG -void -dsdlib_tag_id3(struct input_stream *is, - const struct tag_handler *handler, - void *handler_ctx, goffset tagoffset) -{ - assert(tagoffset >= 0); - - if (tagoffset == 0) - return; - - if (!dsdlib_skip_to(NULL, is, tagoffset)) - return; - - struct id3_tag *id3_tag = NULL; - id3_length_t count; - - /* Prevent broken files causing problems */ - const goffset size = input_stream_get_size(is); - const goffset offset = input_stream_get_offset(is); - if (offset >= size) - return; - - count = size - offset; - - /* Check and limit id3 tag size to prevent a stack overflow */ - if (count == 0 || count > 4096) - return; - - id3_byte_t dsdid3[count]; - id3_byte_t *dsdid3data; - dsdid3data = dsdid3; - - if (!dsdlib_read(NULL, is, dsdid3data, count)) - return; - - id3_tag = id3_tag_parse(dsdid3data, count); - if (id3_tag == NULL) - return; - - scan_id3_tag(id3_tag, handler, handler_ctx); - - id3_tag_delete(id3_tag); - - return; -} -#endif diff --git a/src/decoder/dsdlib.h b/src/decoder/dsdlib.h deleted file mode 100644 index 0912740c3..000000000 --- a/src/decoder/dsdlib.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2012 The Music Player Daemon Project - * 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. - */ - -#ifndef MPD_DECODER_DSDLIB_H -#define MPD_DECODER_DSDLIB_H - -struct dsdlib_id { - char value[4]; -}; - -bool -dsdlib_id_equals(const struct dsdlib_id *id, const char *s); - -bool -dsdlib_read(struct decoder *decoder, struct input_stream *is, - void *data, size_t length); - -bool -dsdlib_skip_to(struct decoder *decoder, struct input_stream *is, - goffset offset); - -bool -dsdlib_skip(struct decoder *decoder, struct input_stream *is, - goffset delta); - -void -dsdlib_tag_id3(struct input_stream *is, - const struct tag_handler *handler, - void *handler_ctx, goffset tagoffset); - -#endif diff --git a/src/decoder/dsf_decoder_plugin.c b/src/decoder/dsf_decoder_plugin.c deleted file mode 100644 index 23576a629..000000000 --- a/src/decoder/dsf_decoder_plugin.c +++ /dev/null @@ -1,364 +0,0 @@ -/* - * Copyright (C) 2012 The Music Player Daemon Project - * 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. - */ - -/* \file - * - * This plugin decodes DSDIFF data (SACD) embedded in DSF files. - * - * The DSF code was created using the specification found here: - * http://dsd-guide.com/sonys-dsf-file-format-spec - * - * All functions common to both DSD decoders have been moved to dsdlib - */ - -#include "config.h" -#include "dsf_decoder_plugin.h" -#include "decoder_api.h" -#include "audio_check.h" -#include "util/bit_reverse.h" -#include "dsdlib.h" -#include "tag_handler.h" - -#include -#include /* for SEEK_SET, SEEK_CUR */ - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "dsf" - -struct dsf_metadata { - unsigned sample_rate, channels; - bool bitreverse; - uint64_t chunk_size; -#ifdef HAVE_ID3TAG - goffset id3_offset; - uint64_t id3_size; -#endif -}; - -struct dsf_header { - /** DSF header id: "DSD " */ - struct dsdlib_id id; - /** DSD chunk size, including id = 28 */ - uint32_t size_low, size_high; - /** total file size */ - uint32_t fsize_low, fsize_high; - /** pointer to id3v2 metadata, should be at the end of the file */ - uint32_t pmeta_low, pmeta_high; -}; - -/** DSF file fmt chunk */ -struct dsf_fmt_chunk { - - /** id: "fmt " */ - struct dsdlib_id id; - /** fmt chunk size, including id, normally 52 */ - uint32_t size_low, size_high; - /** version of this format = 1 */ - uint32_t version; - /** 0: DSD raw */ - uint32_t formatid; - /** channel type, 1 = mono, 2 = stereo, 3 = 3 channels, etc */ - uint32_t channeltype; - /** Channel number, 1 = mono, 2 = stereo, ... 6 = 6 channels */ - uint32_t channelnum; - /** sample frequency: 2822400, 5644800 */ - uint32_t sample_freq; - /** bits per sample 1 or 8 */ - uint32_t bitssample; - /** Sample count per channel in bytes */ - uint32_t scnt_low, scnt_high; - /** block size per channel = 4096 */ - uint32_t block_size; - /** reserved, should be all zero */ - uint32_t reserved; -}; - -struct dsf_data_chunk { - struct dsdlib_id id; - /** "data" chunk size, includes header (id+size) */ - uint32_t size_low, size_high; -}; - -/** - * Read and parse all needed metadata chunks for DSF files. - */ -static bool -dsf_read_metadata(struct decoder *decoder, struct input_stream *is, - struct dsf_metadata *metadata) -{ - uint64_t chunk_size; - struct dsf_header dsf_header; - if (!dsdlib_read(decoder, is, &dsf_header, sizeof(dsf_header)) || - !dsdlib_id_equals(&dsf_header.id, "DSD ")) - return false; - - chunk_size = (((uint64_t)GUINT32_FROM_LE(dsf_header.size_high)) << 32) | - ((uint64_t)GUINT32_FROM_LE(dsf_header.size_low)); - - if (sizeof(dsf_header) != chunk_size) - return false; - -#ifdef HAVE_ID3TAG - uint64_t metadata_offset; - metadata_offset = (((uint64_t)GUINT32_FROM_LE(dsf_header.pmeta_high)) << 32) | - ((uint64_t)GUINT32_FROM_LE(dsf_header.pmeta_low)); -#endif - - /* read the 'fmt ' chunk of the DSF file */ - struct dsf_fmt_chunk dsf_fmt_chunk; - if (!dsdlib_read(decoder, is, &dsf_fmt_chunk, sizeof(dsf_fmt_chunk)) || - !dsdlib_id_equals(&dsf_fmt_chunk.id, "fmt ")) - return false; - - uint64_t fmt_chunk_size; - fmt_chunk_size = (((uint64_t)GUINT32_FROM_LE(dsf_fmt_chunk.size_high)) << 32) | - ((uint64_t)GUINT32_FROM_LE(dsf_fmt_chunk.size_low)); - - if (fmt_chunk_size != sizeof(dsf_fmt_chunk)) - return false; - - uint32_t samplefreq = (uint32_t)GUINT32_FROM_LE(dsf_fmt_chunk.sample_freq); - - /* for now, only support version 1 of the standard, DSD raw stereo - files with a sample freq of 2822400 Hz */ - - if (dsf_fmt_chunk.version != 1 || dsf_fmt_chunk.formatid != 0 - || dsf_fmt_chunk.channeltype != 2 - || dsf_fmt_chunk.channelnum != 2 - || samplefreq != 2822400) - return false; - - uint32_t chblksize = (uint32_t)GUINT32_FROM_LE(dsf_fmt_chunk.block_size); - /* according to the spec block size should always be 4096 */ - if (chblksize != 4096) - return false; - - /* read the 'data' chunk of the DSF file */ - struct dsf_data_chunk data_chunk; - if (!dsdlib_read(decoder, is, &data_chunk, sizeof(data_chunk)) || - !dsdlib_id_equals(&data_chunk.id, "data")) - return false; - - /* data size of DSF files are padded to multiple of 4096, - we use the actual data size as chunk size */ - - uint64_t data_size; - data_size = (((uint64_t)GUINT32_FROM_LE(data_chunk.size_high)) << 32) | - ((uint64_t)GUINT32_FROM_LE(data_chunk.size_low)); - data_size -= sizeof(data_chunk); - - metadata->chunk_size = data_size; - /* data_size cannot be bigger or equal to total file size */ - const uint64_t size = (uint64_t)input_stream_get_size(is); - if (data_size >= size) - return false; - - metadata->channels = (unsigned) dsf_fmt_chunk.channelnum; - metadata->sample_rate = samplefreq; -#ifdef HAVE_ID3TAG - /* metada_offset cannot be bigger then or equal to total file size */ - if (metadata_offset >= size) - metadata->id3_offset = 0; - else - metadata->id3_offset = (goffset) metadata_offset; -#endif - /* check bits per sample format, determine if bitreverse is needed */ - metadata->bitreverse = dsf_fmt_chunk.bitssample == 1; - return true; -} - -static void -bit_reverse_buffer(uint8_t *p, uint8_t *end) -{ - for (; p < end; ++p) - *p = bit_reverse(*p); -} - -/** - * DSF data is build up of alternating 4096 blocks of DSD samples for left and - * right. Convert the buffer holding 1 block of 4096 DSD left samples and 1 - * block of 4096 DSD right samples to 8k of samples in normal PCM left/right - * order. - */ -static void -dsf_to_pcm_order(uint8_t *dest, uint8_t *scratch, size_t nrbytes) -{ - for (unsigned i = 0, j = 0; i < (unsigned)nrbytes; i += 2) { - scratch[i] = *(dest+j); - j++; - } - - for (unsigned i = 1, j = 0; i < (unsigned) nrbytes; i += 2) { - scratch[i] = *(dest+4096+j); - j++; - } - - for (unsigned i = 0; i < (unsigned)nrbytes; i++) { - *dest = scratch[i]; - dest++; - } -} - -/** - * Decode one complete DSF 'data' chunk i.e. a complete song - */ -static bool -dsf_decode_chunk(struct decoder *decoder, struct input_stream *is, - unsigned channels, - uint64_t chunk_size, - bool bitreverse) -{ - uint8_t buffer[8192]; - - /* scratch buffer for DSF samples to convert to the needed - normal left/right regime of samples */ - uint8_t dsf_scratch_buffer[8192]; - - 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; - - if (bitreverse) - bit_reverse_buffer(buffer, buffer + nbytes); - - dsf_to_pcm_order(buffer, dsf_scratch_buffer, nbytes); - - enum decoder_command cmd = - decoder_data(decoder, is, buffer, nbytes, 0); - switch (cmd) { - case DECODE_COMMAND_NONE: - break; - - case DECODE_COMMAND_START: - case DECODE_COMMAND_STOP: - return false; - - case DECODE_COMMAND_SEEK: - - /* not implemented yet */ - decoder_seek_error(decoder); - break; - } - } - return dsdlib_skip(decoder, is, chunk_size); -} - -static void -dsf_stream_decode(struct decoder *decoder, struct input_stream *is) -{ - struct dsf_metadata metadata = { - .sample_rate = 0, - .channels = 0, - }; - - /* check if it is a proper DSF file */ - if (!dsf_read_metadata(decoder, is, &metadata)) - return; - - GError *error = NULL; - struct audio_format audio_format; - if (!audio_format_init_checked(&audio_format, metadata.sample_rate / 8, - SAMPLE_FORMAT_DSD, - metadata.channels, &error)) { - g_warning("%s", error->message); - g_error_free(error); - return; - } - /* 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; - - /* success: file was recognized */ - decoder_initialized(decoder, &audio_format, false, songtime); - - if (!dsf_decode_chunk(decoder, is, metadata.channels, - chunk_size, - metadata.bitreverse)) - return; -} - -static bool -dsf_scan_stream(struct input_stream *is, - G_GNUC_UNUSED const struct tag_handler *handler, - G_GNUC_UNUSED void *handler_ctx) -{ - struct dsf_metadata metadata = { - .sample_rate = 0, - .channels = 0, - }; - - /* check DSF metadata */ - if (!dsf_read_metadata(NULL, is, &metadata)) - return false; - - struct audio_format audio_format; - if (!audio_format_init_checked(&audio_format, metadata.sample_rate / 8, - SAMPLE_FORMAT_DSD, - metadata.channels, NULL)) - /* refuse to parse files which we cannot play anyway */ - return false; - - /* 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); - -#ifdef HAVE_ID3TAG - /* Add available tags from the ID3 tag */ - dsdlib_tag_id3(is, handler, handler_ctx, metadata.id3_offset); -#endif - return true; -} - -static const char *const dsf_suffixes[] = { - "dsf", - NULL -}; - -static const char *const dsf_mime_types[] = { - "application/x-dsf", - NULL -}; - -const struct decoder_plugin dsf_decoder_plugin = { - .name = "dsf", - .stream_decode = dsf_stream_decode, - .scan_stream = dsf_scan_stream, - .suffixes = dsf_suffixes, - .mime_types = dsf_mime_types, -}; diff --git a/src/decoder/dsf_decoder_plugin.h b/src/decoder/dsf_decoder_plugin.h deleted file mode 100644 index 401d3fed7..000000000 --- a/src/decoder/dsf_decoder_plugin.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2012 The Music Player Daemon Project - * 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. - */ - -#ifndef MPD_DECODER_DSF_H -#define MPD_DECODER_DSF_H - -extern const struct decoder_plugin dsf_decoder_plugin; - -#endif -- cgit v1.2.3