decoder/flac: support FLAC files inside archives

Implement the "scan_stream" method that can read tags from any
input_stream object.  This requires a FLAC__IOCallbacks implementation
based on the input_stream API.
This commit is contained in:
Max Kellermann 2012-10-04 07:09:31 +02:00
parent 6b416ce6be
commit b8fdb452be
6 changed files with 217 additions and 2 deletions

View File

@ -620,6 +620,7 @@ endif
if HAVE_FLAC
libdecoder_plugins_a_SOURCES += \
src/decoder/FLACInput.cxx src/decoder/FLACInput.hxx \
src/decoder/FLACIOHandle.cxx src/decoder/FLACIOHandle.hxx \
src/decoder/FLACMetaData.cxx src/decoder/FLACMetaData.hxx \
src/decoder/FLAC_PCM.cxx src/decoder/FLAC_PCM.hxx \
src/decoder/FLACCommon.cxx src/decoder/FLACCommon.hxx \

1
NEWS
View File

@ -2,6 +2,7 @@ ver 0.18 (2012/??/??)
* decoder:
- adplug: new decoder plugin using libadplug
- flac: require libFLAC 1.2 or newer
- flac: support FLAC files inside archives
- opus: new decoder plugin for the Opus codec
- vorbis: skip 16 bit quantisation, provide float samples
* encoder:

View File

@ -101,6 +101,21 @@ flac_scan_file(const char *file,
return true;
}
static bool
flac_scan_stream(struct input_stream *is,
const struct tag_handler *handler, void *handler_ctx)
{
FLACMetadataChain chain;
if (!chain.Read(is)) {
g_debug("Failed to read FLAC tags: %s",
chain.GetStatusString());
return false;
}
chain.Scan(handler, handler_ctx);
return true;
}
/**
* Some glue code around FLAC__stream_decoder_new().
*/
@ -297,6 +312,21 @@ oggflac_scan_file(const char *file,
return true;
}
static bool
oggflac_scan_stream(struct input_stream *is,
const struct tag_handler *handler, void *handler_ctx)
{
FLACMetadataChain chain;
if (!chain.ReadOgg(is)) {
g_debug("Failed to read OggFLAC tags: %s",
chain.GetStatusString());
return false;
}
chain.Scan(handler, handler_ctx);
return true;
}
static void
oggflac_decode(struct decoder *decoder, struct input_stream *input_stream)
{
@ -327,7 +357,7 @@ const struct decoder_plugin oggflac_decoder_plugin = {
oggflac_decode,
nullptr,
oggflac_scan_file,
nullptr,
oggflac_scan_stream,
nullptr,
oggflac_suffixes,
oggflac_mime_types,
@ -349,7 +379,7 @@ const struct decoder_plugin flac_decoder_plugin = {
flac_decode,
nullptr,
flac_scan_file,
nullptr,
flac_scan_stream,
nullptr,
flac_suffixes,
flac_mime_types,

View File

@ -0,0 +1,114 @@
/*
* 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.
*/
#include "config.h"
#include "FLACIOHandle.hxx"
#include "io_error.h"
#include "gcc.h"
#include <errno.h>
static size_t
FLACIORead(void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle)
{
input_stream *is = (input_stream *)handle;
uint8_t *const p0 = (uint8_t *)ptr, *p = p0,
*const end = p0 + size * nmemb;
/* libFLAC is very picky about short reads, and expects the IO
callback to fill the whole buffer (undocumented!) */
GError *error = nullptr;
while (p < end) {
size_t nbytes = input_stream_lock_read(is, p, end - p, &error);
if (nbytes == 0) {
if (error == nullptr)
/* end of file */
break;
if (error->domain == errno_quark())
errno = error->code;
else
/* just some random non-zero
errno value */
errno = EINVAL;
g_error_free(error);
return 0;
}
p += nbytes;
}
/* libFLAC expects a clean errno after returning from the IO
callbacks (undocumented!) */
errno = 0;
return (p - p0) / size;
}
static int
FLACIOSeek(FLAC__IOHandle handle, FLAC__int64 offset, int whence)
{
input_stream *is = (input_stream *)handle;
return input_stream_lock_seek(is, offset, whence, nullptr) ? 0 : -1;
}
static FLAC__int64
FLACIOTell(FLAC__IOHandle handle)
{
input_stream *is = (input_stream *)handle;
return is->offset;
}
static int
FLACIOEof(FLAC__IOHandle handle)
{
input_stream *is = (input_stream *)handle;
return input_stream_lock_eof(is);
}
static int
FLACIOClose(gcc_unused FLAC__IOHandle handle)
{
/* no-op because the libFLAC caller is repsonsible for closing
the #input_stream */
return 0;
}
const FLAC__IOCallbacks flac_io_callbacks = {
FLACIORead,
nullptr,
nullptr,
nullptr,
FLACIOEof,
FLACIOClose,
};
const FLAC__IOCallbacks flac_io_callbacks_seekable = {
FLACIORead,
nullptr,
FLACIOSeek,
FLACIOTell,
FLACIOEof,
FLACIOClose,
};

View File

@ -0,0 +1,48 @@
/*
* 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_FLAC_IO_HANDLE_HXX
#define MPD_FLAC_IO_HANDLE_HXX
#include "gcc.h"
extern "C" {
#include "input_stream.h"
}
#include <FLAC/callback.h>
extern const FLAC__IOCallbacks flac_io_callbacks;
extern const FLAC__IOCallbacks flac_io_callbacks_seekable;
static inline FLAC__IOHandle
ToFLACIOHandle(input_stream *is)
{
return (FLAC__IOHandle)is;
}
static inline const FLAC__IOCallbacks &
GetFLACIOCallbacks(const input_stream *is)
{
return is->seekable
? flac_io_callbacks_seekable
: flac_io_callbacks;
}
#endif

View File

@ -21,6 +21,7 @@
#define MPD_FLAC_METADATA_H
#include "gcc.h"
#include "FLACIOHandle.hxx"
#include <FLAC/metadata.h>
@ -45,10 +46,30 @@ public:
return ::FLAC__metadata_chain_read(chain, path);
}
bool Read(FLAC__IOHandle handle, FLAC__IOCallbacks callbacks) {
return ::FLAC__metadata_chain_read_with_callbacks(chain,
handle,
callbacks);
}
bool Read(input_stream *is) {
return Read(::ToFLACIOHandle(is), ::GetFLACIOCallbacks(is));
}
bool ReadOgg(const char *path) {
return ::FLAC__metadata_chain_read_ogg(chain, path);
}
bool ReadOgg(FLAC__IOHandle handle, FLAC__IOCallbacks callbacks) {
return ::FLAC__metadata_chain_read_ogg_with_callbacks(chain,
handle,
callbacks);
}
bool ReadOgg(input_stream *is) {
return ReadOgg(::ToFLACIOHandle(is), ::GetFLACIOCallbacks(is));
}
gcc_pure
FLAC__Metadata_ChainStatus GetStatus() const {
return ::FLAC__metadata_chain_status(chain);