From b8fdb452be9d79492b4bff84d2990cfcb1da282b Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Thu, 4 Oct 2012 07:09:31 +0200
Subject: [PATCH] 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.
---
 Makefile.am                       |   1 +
 NEWS                              |   1 +
 src/decoder/FLACDecoderPlugin.cxx |  34 ++++++++-
 src/decoder/FLACIOHandle.cxx      | 114 ++++++++++++++++++++++++++++++
 src/decoder/FLACIOHandle.hxx      |  48 +++++++++++++
 src/decoder/FLACMetaData.hxx      |  21 ++++++
 6 files changed, 217 insertions(+), 2 deletions(-)
 create mode 100644 src/decoder/FLACIOHandle.cxx
 create mode 100644 src/decoder/FLACIOHandle.hxx

diff --git a/Makefile.am b/Makefile.am
index 92ee671e4..3f462db45 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -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 \
diff --git a/NEWS b/NEWS
index e49343321..af9f5181e 100644
--- a/NEWS
+++ b/NEWS
@@ -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:
diff --git a/src/decoder/FLACDecoderPlugin.cxx b/src/decoder/FLACDecoderPlugin.cxx
index b8d5173b5..dbe7f207f 100644
--- a/src/decoder/FLACDecoderPlugin.cxx
+++ b/src/decoder/FLACDecoderPlugin.cxx
@@ -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,
diff --git a/src/decoder/FLACIOHandle.cxx b/src/decoder/FLACIOHandle.cxx
new file mode 100644
index 000000000..08ec36e48
--- /dev/null
+++ b/src/decoder/FLACIOHandle.cxx
@@ -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,
+};
diff --git a/src/decoder/FLACIOHandle.hxx b/src/decoder/FLACIOHandle.hxx
new file mode 100644
index 000000000..193a15ef5
--- /dev/null
+++ b/src/decoder/FLACIOHandle.hxx
@@ -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
diff --git a/src/decoder/FLACMetaData.hxx b/src/decoder/FLACMetaData.hxx
index 9808852f3..7b5eb8b0c 100644
--- a/src/decoder/FLACMetaData.hxx
+++ b/src/decoder/FLACMetaData.hxx
@@ -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);