From 154aa496e8c18bba3dc10c607987c187f4686ae4 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Tue, 26 Aug 2008 08:27:04 +0200
Subject: added struct decoder

The decoder struct should later be made opaque to the decoder plugin,
because maintaining a stable struct ABI is quite difficult.  The ABI
should only consist of a small number of stable functions.
---
 src/Makefile.am                     |  2 ++
 src/decode.c                        | 18 +++++++++++++-----
 src/decoder_api.h                   | 37 +++++++++++++++++++++++++++++++++++++
 src/decoder_internal.h              | 28 ++++++++++++++++++++++++++++
 src/inputPlugin.h                   |  8 ++++++--
 src/inputPlugins/_flac_common.c     |  4 +++-
 src/inputPlugins/_flac_common.h     |  4 +++-
 src/inputPlugins/aac_plugin.c       |  2 +-
 src/inputPlugins/audiofile_plugin.c |  2 +-
 src/inputPlugins/flac_plugin.c      | 13 +++++++------
 src/inputPlugins/mod_plugin.c       |  2 +-
 src/inputPlugins/mp3_plugin.c       |  3 ++-
 src/inputPlugins/mp4_plugin.c       |  3 ++-
 src/inputPlugins/mpc_plugin.c       |  3 ++-
 src/inputPlugins/oggflac_plugin.c   |  8 +++++---
 src/inputPlugins/oggvorbis_plugin.c |  3 ++-
 src/inputPlugins/wavpack_plugin.c   | 11 ++++++-----
 17 files changed, 121 insertions(+), 30 deletions(-)
 create mode 100644 src/decoder_api.h
 create mode 100644 src/decoder_internal.h

diff --git a/src/Makefile.am b/src/Makefile.am
index d79bda29f..4a4196499 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -40,6 +40,8 @@ mpd_headers = \
 	conf.h \
 	dbUtils.h \
 	decode.h \
+	decoder_api.h \
+	decoder_internal.h \
 	directory.h \
 	gcc.h \
 	inputPlugin.h \
diff --git a/src/decode.c b/src/decode.c
index 628dca14f..4df410e50 100644
--- a/src/decode.c
+++ b/src/decode.c
@@ -17,6 +17,7 @@
  */
 
 #include "decode.h"
+#include "decoder_internal.h"
 
 #include "player.h"
 #include "playerData.h"
@@ -205,6 +206,7 @@ static void processDecodeInput(int *pause_r, unsigned int *bbp_r,
 
 static void decodeStart(void)
 {
+	struct decoder decoder;
 	int ret;
 	int close_instream = 1;
 	InputStream inStream;
@@ -250,7 +252,7 @@ static void decodeStart(void)
 			if (plugin->tryDecodeFunc
 			    && !plugin->tryDecodeFunc(&inStream))
 				continue;
-			ret = plugin->streamDecodeFunc(&inStream);
+			ret = plugin->streamDecodeFunc(&decoder, &inStream);
 			break;
 		}
 
@@ -267,7 +269,8 @@ static void decodeStart(void)
 				if (plugin->tryDecodeFunc &&
 				    !plugin->tryDecodeFunc(&inStream))
 					continue;
-				ret = plugin->streamDecodeFunc(&inStream);
+				decoder.plugin = plugin;
+				ret = plugin->streamDecodeFunc(&decoder, &inStream);
 				break;
 			}
 		}
@@ -278,7 +281,9 @@ static void decodeStart(void)
 			/* we already know our mp3Plugin supports streams, no
 			 * need to check for stream{Types,DecodeFunc} */
 			if ((plugin = getInputPluginFromName("mp3"))) {
-				ret = plugin->streamDecodeFunc(&inStream);
+				decoder.plugin = plugin;
+				ret = plugin->streamDecodeFunc(&decoder,
+				                               &inStream);
 			}
 		}
 	} else {
@@ -295,10 +300,13 @@ static void decodeStart(void)
 			if (plugin->fileDecodeFunc) {
 				closeInputStream(&inStream);
 				close_instream = 0;
-				ret = plugin->fileDecodeFunc(path_max_fs);
+				decoder.plugin = plugin;
+				ret = plugin->fileDecodeFunc(&decoder,
+				                             path_max_fs);
 				break;
 			} else if (plugin->streamDecodeFunc) {
-				ret = plugin->streamDecodeFunc(&inStream);
+				decoder.plugin = plugin;
+				ret = plugin->streamDecodeFunc(&decoder, &inStream);
 				break;
 			}
 		}
diff --git a/src/decoder_api.h b/src/decoder_api.h
new file mode 100644
index 000000000..f4e0c13b9
--- /dev/null
+++ b/src/decoder_api.h
@@ -0,0 +1,37 @@
+/* the Music Player Daemon (MPD)
+ * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
+ * Copyright (C) 2008 Max Kellermann <max@duempel.org>
+ * This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef DECODER_API_H
+#define DECODER_API_H
+
+/*
+ * This is the public API which is used by decoder plugins to
+ * communicate with the mpd core.
+ *
+ */
+
+#include "inputPlugin.h"
+
+/**
+ * Opaque handle which the decoder plugin passes to the functions in
+ * this header.
+ */
+struct decoder;
+
+#endif
diff --git a/src/decoder_internal.h b/src/decoder_internal.h
new file mode 100644
index 000000000..7f1095db9
--- /dev/null
+++ b/src/decoder_internal.h
@@ -0,0 +1,28 @@
+/* the Music Player Daemon (MPD)
+ * Copyright (C) 2008 Max Kellermann <max@duempel.org>
+ * This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef DECODER_INTERNAL_H
+#define DECODER_INTERNAL_H
+
+#include "decoder_api.h"
+
+struct decoder {
+	InputPlugin *plugin;
+};
+
+#endif
diff --git a/src/inputPlugin.h b/src/inputPlugin.h
index 169781931..2b0e504ba 100644
--- a/src/inputPlugin.h
+++ b/src/inputPlugin.h
@@ -27,6 +27,8 @@
 #define INPUT_PLUGIN_STREAM_FILE	0x01
 #define INPUT_PLUGIN_STREAM_URL		0x02
 
+struct decoder;
+
 /* optional, set this to NULL if the InputPlugin doesn't have/need one
  * this must return < 0 if there is an error and >= 0 otherwise */
 typedef int (*InputPlugin_initFunc) (void);
@@ -42,14 +44,16 @@ typedef unsigned int (*InputPlugin_tryDecodeFunc) (InputStream *);
  * and networked (HTTP) connections.
  *
  * returns -1 on error, 0 on success */
-typedef int (*InputPlugin_streamDecodeFunc) (InputStream *);
+typedef int (*InputPlugin_streamDecodeFunc) (struct decoder *,
+					     InputStream *);
 
 /* use this if and only if your InputPlugin can only be passed a filename or
  * handle as input, and will not allow callbacks to be set (like Ogg-Vorbis
  * and FLAC libraries allow)
  *
  * returns -1 on error, 0 on success */
-typedef int (*InputPlugin_fileDecodeFunc) (char *path);
+typedef int (*InputPlugin_fileDecodeFunc) (struct decoder *,
+					   char *path);
 
 /* file should be the full path!  Returns NULL if a tag cannot be found
  * or read */
diff --git a/src/inputPlugins/_flac_common.c b/src/inputPlugins/_flac_common.c
index 9e70662be..e1017e596 100644
--- a/src/inputPlugins/_flac_common.c
+++ b/src/inputPlugins/_flac_common.c
@@ -30,12 +30,14 @@
 #include <FLAC/format.h>
 #include <FLAC/metadata.h>
 
-void init_FlacData(FlacData * data, InputStream * inStream)
+void init_FlacData(FlacData * data, struct decoder * decoder,
+		   InputStream * inStream)
 {
 	data->chunk_length = 0;
 	data->time = 0;
 	data->position = 0;
 	data->bitRate = 0;
+	data->decoder = decoder;
 	data->inStream = inStream;
 	data->replayGainInfo = NULL;
 	data->tag = NULL;
diff --git a/src/inputPlugins/_flac_common.h b/src/inputPlugins/_flac_common.h
index fb69277ff..0437a416d 100644
--- a/src/inputPlugins/_flac_common.h
+++ b/src/inputPlugins/_flac_common.h
@@ -144,13 +144,15 @@ typedef struct {
 	float time;
 	unsigned int bitRate;
 	FLAC__uint64 position;
+	struct decoder *decoder;
 	InputStream *inStream;
 	ReplayGainInfo *replayGainInfo;
 	MpdTag *tag;
 } FlacData;
 
 /* initializes a given FlacData struct */
-void init_FlacData(FlacData * data, InputStream * inStream);
+void init_FlacData(FlacData * data, struct decoder * decoder,
+		   InputStream * inStream);
 void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
 			     FlacData * data);
 void flac_error_common_cb(const char *plugin,
diff --git a/src/inputPlugins/aac_plugin.c b/src/inputPlugins/aac_plugin.c
index 2ca757392..4f1818478 100644
--- a/src/inputPlugins/aac_plugin.c
+++ b/src/inputPlugins/aac_plugin.c
@@ -278,7 +278,7 @@ static int getAacTotalTime(char *file)
 	return file_time;
 }
 
-static int aac_decode(char *path)
+static int aac_decode(mpd_unused struct decoder * mpd_decoder, char *path)
 {
 	float file_time;
 	float totalTime;
diff --git a/src/inputPlugins/audiofile_plugin.c b/src/inputPlugins/audiofile_plugin.c
index 08c5e6e16..0cc357135 100644
--- a/src/inputPlugins/audiofile_plugin.c
+++ b/src/inputPlugins/audiofile_plugin.c
@@ -40,7 +40,7 @@ static int getAudiofileTotalTime(char *file)
 	return total_time;
 }
 
-static int audiofile_decode(char *path)
+static int audiofile_decode(mpd_unused struct decoder * decoder, char *path)
 {
 	int fs, frame_count;
 	AFfilehandle af_fp;
diff --git a/src/inputPlugins/flac_plugin.c b/src/inputPlugins/flac_plugin.c
index a5b44c7f4..7c4b8e356 100644
--- a/src/inputPlugins/flac_plugin.c
+++ b/src/inputPlugins/flac_plugin.c
@@ -376,7 +376,8 @@ static MpdTag *flacTagDup(char *file)
 	return ret;
 }
 
-static int flac_decode_internal(InputStream * inStream, int is_ogg)
+static int flac_decode_internal(struct decoder * decoder,
+				InputStream * inStream, int is_ogg)
 {
 	flac_decoder *flacDec;
 	FlacData data;
@@ -384,7 +385,7 @@ static int flac_decode_internal(InputStream * inStream, int is_ogg)
 
 	if (!(flacDec = flac_new()))
 		return -1;
-	init_FlacData(&data, inStream);
+	init_FlacData(&data, decoder, inStream);
 
 #if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
         if(!FLAC__stream_decoder_set_metadata_respond(flacDec, FLAC__METADATA_TYPE_VORBIS_COMMENT))
@@ -458,9 +459,9 @@ fail:
 	return 0;
 }
 
-static int flac_decode(InputStream * inStream)
+static int flac_decode(struct decoder * decoder, InputStream * inStream)
 {
-	return flac_decode_internal(inStream, 0);
+	return flac_decode_internal(decoder, inStream, 0);
 }
 
 #if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
@@ -499,9 +500,9 @@ out:
 	return ret;
 }
 
-static int oggflac_decode(InputStream * inStream)
+static int oggflac_decode(struct decoder *decoder, InputStream * inStream)
 {
-	return flac_decode_internal(inStream, 1);
+	return flac_decode_internal(decoder, inStream, 1);
 }
 
 static unsigned int oggflac_try_decode(InputStream * inStream)
diff --git a/src/inputPlugins/mod_plugin.c b/src/inputPlugins/mod_plugin.c
index 40e2ef841..a041f1e34 100644
--- a/src/inputPlugins/mod_plugin.c
+++ b/src/inputPlugins/mod_plugin.c
@@ -159,7 +159,7 @@ static void mod_close(mod_Data * data)
 	free(data);
 }
 
-static int mod_decode(char *path)
+static int mod_decode(mpd_unused struct decoder * decoder, char *path)
 {
 	mod_Data *data;
 	float total_time = 0.0;
diff --git a/src/inputPlugins/mp3_plugin.c b/src/inputPlugins/mp3_plugin.c
index 4288f85de..e68452470 100644
--- a/src/inputPlugins/mp3_plugin.c
+++ b/src/inputPlugins/mp3_plugin.c
@@ -1015,7 +1015,8 @@ static void initAudioFormatFromMp3DecodeData(mp3DecodeData * data,
 	af->channels = MAD_NCHANNELS(&(data->frame).header);
 }
 
-static int mp3_decode(InputStream * inStream)
+static int mp3_decode(mpd_unused struct decoder * decoder,
+		      InputStream * inStream)
 {
 	mp3DecodeData data;
 	MpdTag *tag = NULL;
diff --git a/src/inputPlugins/mp4_plugin.c b/src/inputPlugins/mp4_plugin.c
index 0bdd3e75b..e715b3983 100644
--- a/src/inputPlugins/mp4_plugin.c
+++ b/src/inputPlugins/mp4_plugin.c
@@ -78,7 +78,8 @@ static uint32_t mp4_inputStreamSeekCallback(void *inStream, uint64_t position)
 	return seekInputStream((InputStream *) inStream, position, SEEK_SET);
 }
 
-static int mp4_decode(InputStream * inStream)
+static int mp4_decode(mpd_unused struct decoder * mpd_decoder,
+		      InputStream * inStream)
 {
 	mp4ff_t *mp4fh;
 	mp4ff_callback_t *mp4cb;
diff --git a/src/inputPlugins/mpc_plugin.c b/src/inputPlugins/mpc_plugin.c
index c6b13cdff..f69e9eddb 100644
--- a/src/inputPlugins/mpc_plugin.c
+++ b/src/inputPlugins/mpc_plugin.c
@@ -106,7 +106,8 @@ static inline mpd_sint16 convertSample(MPC_SAMPLE_FORMAT sample)
 	return val;
 }
 
-static int mpc_decode(InputStream * inStream)
+static int mpc_decode(mpd_unused struct decoder * mpd_decoder,
+		      InputStream * inStream)
 {
 	mpc_decoder decoder;
 	mpc_reader reader;
diff --git a/src/inputPlugins/oggflac_plugin.c b/src/inputPlugins/oggflac_plugin.c
index 552b550be..a365f73cc 100644
--- a/src/inputPlugins/oggflac_plugin.c
+++ b/src/inputPlugins/oggflac_plugin.c
@@ -314,7 +314,7 @@ static MpdTag *oggflac_TagDup(char *file)
 		return NULL;
 	}
 
-	init_FlacData(&data, &inStream);
+	init_FlacData(&data, NULL, &inStream);
 
 	/* errors here won't matter,
 	 * data.tag will be set or unset, that's all we care about */
@@ -331,13 +331,15 @@ static unsigned int oggflac_try_decode(InputStream * inStream)
 	return (ogg_stream_type_detect(inStream) == FLAC) ? 1 : 0;
 }
 
-static int oggflac_decode(InputStream * inStream)
+static int oggflac_decode(struct decoder * mpd_decoder, InputStream * inStream)
 {
+	DecoderControl *dc = mpd_decoder->dc;
+	OutputBuffer *ob = mpd_decoder->ob;
 	OggFLAC__SeekableStreamDecoder *decoder = NULL;
 	FlacData data;
 	int ret = 0;
 
-	init_FlacData(&data, inStream);
+	init_FlacData(&data, mpd_decoder, inStream);
 
 	if (!(decoder = full_decoder_init_and_read_metadata(&data, 0))) {
 		ret = -1;
diff --git a/src/inputPlugins/oggvorbis_plugin.c b/src/inputPlugins/oggvorbis_plugin.c
index 993035f37..cb0c5a948 100644
--- a/src/inputPlugins/oggvorbis_plugin.c
+++ b/src/inputPlugins/oggvorbis_plugin.c
@@ -210,7 +210,8 @@ static void putOggCommentsIntoOutputBuffer(char *streamName,
 }
 
 /* public */
-static int oggvorbis_decode(InputStream * inStream)
+static int oggvorbis_decode(mpd_unused struct decoder * decoder,
+			    InputStream * inStream)
 {
 	OggVorbis_File vf;
 	ov_callbacks callbacks;
diff --git a/src/inputPlugins/wavpack_plugin.c b/src/inputPlugins/wavpack_plugin.c
index ae5f50826..99491a232 100644
--- a/src/inputPlugins/wavpack_plugin.c
+++ b/src/inputPlugins/wavpack_plugin.c
@@ -124,7 +124,8 @@ static void format_samples_float(mpd_unused int Bps, void *buffer,
  * This does the main decoding thing.
  * Requires an already opened WavpackContext.
  */
-static void wavpack_decode(WavpackContext *wpc, int canseek,
+static void wavpack_decode(mpd_unused struct decoder * decoder,
+                           WavpackContext *wpc, int canseek,
                            ReplayGainInfo *replayGainInfo)
 {
 	void (*format_samples)(int Bps, void *buffer, uint32_t samcnt);
@@ -436,7 +437,7 @@ static unsigned int wavpack_trydecode(InputStream *is)
 /*
  * Decodes a stream.
  */
-static int wavpack_streamdecode(InputStream *is)
+static int wavpack_streamdecode(struct decoder * decoder, InputStream *is)
 {
 	char error[ERRORLEN];
 	WavpackContext *wpc;
@@ -535,7 +536,7 @@ static int wavpack_streamdecode(InputStream *is)
 		return -1;
 	}
 
-	wavpack_decode(wpc, canseek, NULL);
+	wavpack_decode(decoder, wpc, canseek, NULL);
 
 	WavpackCloseFile(wpc);
 	if (wvc_url != NULL) {
@@ -550,7 +551,7 @@ static int wavpack_streamdecode(InputStream *is)
 /*
  * Decodes a file.
  */
-static int wavpack_filedecode(char *fname)
+static int wavpack_filedecode(struct decoder * decoder, char *fname)
 {
 	char error[ERRORLEN];
 	WavpackContext *wpc;
@@ -566,7 +567,7 @@ static int wavpack_filedecode(char *fname)
 
 	replayGainInfo = wavpack_replaygain(wpc);
 
-	wavpack_decode(wpc, 1, replayGainInfo);
+	wavpack_decode(decoder, wpc, 1, replayGainInfo);
 
 	if (replayGainInfo)
 		freeReplayGainInfo(replayGainInfo);
-- 
cgit v1.2.3