From 868291efc42d7b66bfdafe0eded0b38ccad0b3b1 Mon Sep 17 00:00:00 2001 From: "Ronald S. Bultje" Date: Sun, 7 Nov 2004 18:30:06 +0000 Subject: Add musepack decoder. Original commit message from CVS: * configure.ac: * ext/Makefile.am: * ext/musepack/Makefile.am: * ext/musepack/gstmusepackdec.cpp: * ext/musepack/gstmusepackdec.h: * ext/musepack/gstmusepackreader.cpp: * ext/musepack/gstmusepackreader.h: Add musepack decoder. * ext/faad/gstfaad.c: (gst_faad_base_init): Make pad templates static. * gst/typefind/gsttypefindfunctions.c: (mp3_type_find), (plugin_init): Add musepack typefinder, make mp3 typefinding work halfway stream, which doesn't actually work yet because id3demux doesn't implement _get_length(). --- ChangeLog | 18 ++ configure.ac | 18 ++ ext/Makefile.am | 8 + ext/faad/gstfaad.c | 18 +- ext/musepack/Makefile.am | 12 + ext/musepack/gstmusepackdec.c | 505 +++++++++++++++++++++++++++++++++++++ ext/musepack/gstmusepackdec.cpp | 505 +++++++++++++++++++++++++++++++++++++ ext/musepack/gstmusepackdec.h | 73 ++++++ ext/musepack/gstmusepackreader.c | 145 +++++++++++ ext/musepack/gstmusepackreader.cpp | 145 +++++++++++ ext/musepack/gstmusepackreader.h | 43 ++++ 11 files changed, 1480 insertions(+), 10 deletions(-) create mode 100644 ext/musepack/Makefile.am create mode 100644 ext/musepack/gstmusepackdec.c create mode 100644 ext/musepack/gstmusepackdec.cpp create mode 100644 ext/musepack/gstmusepackdec.h create mode 100644 ext/musepack/gstmusepackreader.c create mode 100644 ext/musepack/gstmusepackreader.cpp create mode 100644 ext/musepack/gstmusepackreader.h diff --git a/ChangeLog b/ChangeLog index d2ecf69b..5d1de381 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2004-11-07 Ronald S. Bultje + + * configure.ac: + * ext/Makefile.am: + * ext/musepack/Makefile.am: + * ext/musepack/gstmusepackdec.cpp: + * ext/musepack/gstmusepackdec.h: + * ext/musepack/gstmusepackreader.cpp: + * ext/musepack/gstmusepackreader.h: + Add musepack decoder. + * ext/faad/gstfaad.c: (gst_faad_base_init): + Make pad templates static. + * gst/typefind/gsttypefindfunctions.c: (mp3_type_find), + (plugin_init): + Add musepack typefinder, make mp3 typefinding work halfway stream, + which doesn't actually work yet because id3demux doesn't implement + _get_length(). + 2004-11-07 Ronald S. Bultje * ext/ogg/gstoggmux.c: (gst_ogg_mux_next_buffer), diff --git a/configure.ac b/configure.ac index 11b17ff0..a69a2bcd 100644 --- a/configure.ac +++ b/configure.ac @@ -1359,6 +1359,23 @@ main (int argc, fi ]) +dnl *** musepack *** +translit(dnm, m, l) AM_CONDITIONAL(USE_MUSEPACK, true) +GST_CHECK_FEATURE(MUSEPACK, [musepackdec], musepack, [ + AC_LANG_CPLUSPLUS + OLD_CPPFLAGS="$CPPFLAGS" + CPPFLAGS="-I/usr/include/musepack $CPPFLAGS" + AC_CHECK_HEADER(mpc_dec.h, [ + HAVE_MUSEPACK="yes" + MUSEPACK_LIBS="-lmusepack" + MUSEPACK_CFLAGS="-I/usr/include/musepack" + AC_SUBST(MUSEPACK_CFLAGS) + AC_SUBST(MUSEPACK_LIBS) + ], [HAVE_MUSEPACK="no"]) + CPPFLAGS="$OLD_CPPFLAGS" + AC_LANG_C +]) + dnl *** musicbrainz *** dnl libmusicbrainz <= 2.0.2 has symbol clashes with ffmpeg dnl however, our ffmpeg patch was accepted upstream @@ -1948,6 +1965,7 @@ ext/mikmod/Makefile ext/mpeg2dec/Makefile ext/mpeg2enc/Makefile ext/mplex/Makefile +ext/musepack/Makefile ext/musicbrainz/Makefile ext/nas/Makefile ext/ogg/Makefile diff --git a/ext/Makefile.am b/ext/Makefile.am index cd9c5ac3..311a3209 100644 --- a/ext/Makefile.am +++ b/ext/Makefile.am @@ -238,6 +238,12 @@ endif #MAS_DIR= #endif +if USE_MUSEPACK +MUSEPACK_DIR=musepack +else +MUSEPACK_DIR= +endif + if USE_MUSICBRAINZ MUSICBRAINZ_DIR=musicbrainz else @@ -399,6 +405,7 @@ SUBDIRS=\ $(MPEG2DEC_DIR) \ $(MPEG2ENC_DIR) \ $(MPLEX_DIR) \ + $(MUSEPACK_DIR) \ $(MUSICBRAINZ_DIR) \ $(OGG_DIR) \ $(PANGO_DIR) \ @@ -458,6 +465,7 @@ DIST_SUBDIRS=\ mpeg2dec \ mpeg2enc \ mplex \ + musepack \ musicbrainz \ nas \ ogg \ diff --git a/ext/faad/gstfaad.c b/ext/faad/gstfaad.c index 04bf6e3c..883790e2 100644 --- a/ext/faad/gstfaad.c +++ b/ext/faad/gstfaad.c @@ -25,14 +25,13 @@ #include "gstfaad.h" -GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("audio/mpeg, " - "systemstream = (bool) FALSE, " "mpegversion = { (int) 2, (int) 4 }") + GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) { 2, 4 }") ); -GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", +static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-int, " @@ -92,12 +91,11 @@ gst_faad_get_type (void) static void gst_faad_base_init (GstFaadClass * klass) { - GstElementDetails gst_faad_details = { - "Free AAC Decoder (FAAD)", - "Codec/Decoder/Audio", - "Free MPEG-2/4 AAC decoder", - "Ronald Bultje ", - }; + static GstElementDetails gst_faad_details = + GST_ELEMENT_DETAILS ("Free AAC Decoder (FAAD)", + "Codec/Decoder/Audio", + "Free MPEG-2/4 AAC decoder", + "Ronald Bultje "); GstElementClass *element_class = GST_ELEMENT_CLASS (klass); gst_element_class_add_pad_template (element_class, diff --git a/ext/musepack/Makefile.am b/ext/musepack/Makefile.am new file mode 100644 index 00000000..3c3b1bfa --- /dev/null +++ b/ext/musepack/Makefile.am @@ -0,0 +1,12 @@ +plugin_LTLIBRARIES = libgstmusepack.la + +libgstmusepack_la_SOURCES = \ + gstmusepackdec.cpp \ + gstmusepackreader.cpp +libgstmusepack_la_CXXFLAGS = $(MUSEPACK_CFLAGS) $(GST_CFLAGS) +libgstmusepack_la_LIBADD = $(MUSEPACK_LIBS) +libgstmusepack_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) + +noinst_HEADERS = \ + gstmusepackdec.h \ + gstmusepackreader.h diff --git a/ext/musepack/gstmusepackdec.c b/ext/musepack/gstmusepackdec.c new file mode 100644 index 00000000..08f8d569 --- /dev/null +++ b/ext/musepack/gstmusepackdec.c @@ -0,0 +1,505 @@ +/* GStreamer Musepack decoder plugin + * Copyright (C) 2004 Ronald Bultje + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstmusepackdec.h" +#include "gstmusepackreader.h" + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/x-musepack") + ); + +static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/x-raw-int, " + "endianness = (int) BYTE_ORDER, " + "signed = (bool) TRUE, " + "width = (int) 32, " + "depth = (int) 32, " + "rate = (int) [ 8000, 96000 ], " + "channels = (int) [ 1, 2 ]") + ); + +static void gst_musepackdec_base_init (GstMusepackDecClass * klass); +static void gst_musepackdec_class_init (GstMusepackDecClass * klass); +static void gst_musepackdec_init (GstMusepackDec * musepackdec); + +static gboolean gst_musepackdec_src_event (GstPad * pad, GstEvent * event); +static const GstFormat *gst_musepackdec_get_formats (GstPad * pad); +static const GstEventMask *gst_musepackdec_get_event_masks (GstPad * pad); +static const GstQueryType *gst_musepackdec_get_query_types (GstPad * pad); +static gboolean gst_musepackdec_src_query (GstPad * pad, GstQueryType type, + GstFormat * format, gint64 * value); +static gboolean gst_musepackdec_src_convert (GstPad * pad, + GstFormat src_format, + gint64 src_value, GstFormat * dest_format, gint64 * dest_value); + +static void gst_musepackdec_loop (GstElement * element); +static GstElementStateReturn + gst_musepackdec_change_state (GstElement * element); + +static GstElementClass *parent_class = NULL; + +/* static guint gst_musepackdec_signals[LAST_SIGNAL] = { 0 }; */ + +GType +gst_musepackdec_get_type (void) +{ + static GType gst_musepackdec_type = 0; + + if (!gst_musepackdec_type) { + static const GTypeInfo gst_musepackdec_info = { + sizeof (GstMusepackDecClass), + (GBaseInitFunc) gst_musepackdec_base_init, + NULL, + (GClassInitFunc) gst_musepackdec_class_init, + NULL, + NULL, + sizeof (GstMusepackDec), + 0, + (GInstanceInitFunc) gst_musepackdec_init, + }; + + gst_musepackdec_type = g_type_register_static (GST_TYPE_ELEMENT, + "GstMusepackDec", &gst_musepackdec_info, (GTypeFlags) 0); + } + + return gst_musepackdec_type; +} + +static void +gst_musepackdec_base_init (GstMusepackDecClass * klass) +{ + static GstElementDetails gst_musepackdec_details = GST_ELEMENT_DETAILS ( + "Musepack decoder", + "Codec/Decoder/Audio", + "Musepack decoder", + "Ronald Bultje " + ); + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&src_template)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&sink_template)); + + gst_element_class_set_details (element_class, &gst_musepackdec_details); +} + +static void +gst_musepackdec_class_init (GstMusepackDecClass * klass) +{ + GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass); + + parent_class = GST_ELEMENT_CLASS (g_type_class_ref (GST_TYPE_ELEMENT)); + + gstelement_class->change_state = gst_musepackdec_change_state; +} + +static void +gst_musepackdec_init (GstMusepackDec * musepackdec) +{ + GST_FLAG_SET (musepackdec, GST_ELEMENT_EVENT_AWARE); + + musepackdec->dec = NULL; + musepackdec->seek_pending = FALSE; + + musepackdec->sinkpad = + gst_pad_new_from_template (gst_static_pad_template_get (&sink_template), + "sink"); + gst_element_add_pad (GST_ELEMENT (musepackdec), musepackdec->sinkpad); + + musepackdec->srcpad = + gst_pad_new_from_template (gst_static_pad_template_get (&src_template), + "src"); + gst_pad_set_event_function (musepackdec->srcpad, + GST_DEBUG_FUNCPTR (gst_musepackdec_src_event)); + gst_pad_set_event_mask_function (musepackdec->srcpad, + GST_DEBUG_FUNCPTR (gst_musepackdec_get_event_masks)); + gst_pad_set_query_function (musepackdec->srcpad, + GST_DEBUG_FUNCPTR (gst_musepackdec_src_query)); + gst_pad_set_query_type_function (musepackdec->srcpad, + GST_DEBUG_FUNCPTR (gst_musepackdec_get_query_types)); + gst_pad_set_convert_function (musepackdec->srcpad, + GST_DEBUG_FUNCPTR (gst_musepackdec_src_convert)); + gst_pad_set_formats_function (musepackdec->srcpad, + GST_DEBUG_FUNCPTR (gst_musepackdec_get_formats)); + gst_pad_use_explicit_caps (musepackdec->srcpad); + gst_element_add_pad (GST_ELEMENT (musepackdec), musepackdec->srcpad); + + gst_element_set_loop_function (GST_ELEMENT (musepackdec), + gst_musepackdec_loop); +} + +static gboolean +gst_musepackdec_src_event (GstPad * pad, GstEvent * event) +{ + GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (gst_pad_get_parent (pad)); + gboolean res; + + switch (GST_EVENT_TYPE (event)) { + case GST_EVENT_SEEK: { + gint64 offset, len, pos; + GstFormat fmt = GST_FORMAT_TIME; + + /* in time */ + if (!gst_pad_convert (pad, + (GstFormat) GST_EVENT_SEEK_FORMAT (event), + GST_EVENT_SEEK_OFFSET (event), + &fmt, &offset) || + !gst_pad_convert (pad, + GST_FORMAT_DEFAULT, musepackdec->len, + &fmt, &len) || + !gst_pad_convert (pad, + GST_FORMAT_DEFAULT, musepackdec->pos, + &fmt, &pos)) { + res = FALSE; + break; + } + + /* offset from start */ + switch (GST_EVENT_SEEK_METHOD (event)) { + case GST_SEEK_METHOD_SET: + break; + case GST_SEEK_METHOD_CUR: + offset += pos; + break; + case GST_SEEK_METHOD_END: + offset = len - offset; + break; + default: + res = FALSE; + goto done; + } + + /* only valid seeks */ + if (offset >= len || offset < 0) { + res = FALSE; + break; + } + + /* store */ + musepackdec->seek_pending = TRUE; + musepackdec->flush_pending = + GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH; + musepackdec->seek_time = offset; + res = TRUE; + break; + } + default: + res = FALSE; + break; + } + +done: + gst_event_unref (event); + + return res; +} + +static const GstFormat * +gst_musepackdec_get_formats (GstPad * pad) +{ + static const GstFormat formats[] = { + GST_FORMAT_BYTES, + GST_FORMAT_DEFAULT, + GST_FORMAT_TIME, + (GstFormat) 0 + }; + + return formats; +} + +static const GstEventMask * +gst_musepackdec_get_event_masks (GstPad * pad) +{ + static const GstEventMask event_masks[] = { + { GST_EVENT_SEEK, + (GstEventFlag) (GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH) }, + { (GstEventType) 0, (GstEventFlag) 0 } + }; + + return event_masks; +} + +static const GstQueryType * +gst_musepackdec_get_query_types (GstPad * pad) +{ + static const GstQueryType query_types[] = { + GST_QUERY_TOTAL, + GST_QUERY_POSITION, + (GstQueryType) 0 + }; + + return query_types; +} + +static gboolean +gst_musepackdec_src_query (GstPad * pad, GstQueryType type, + GstFormat * format, gint64 * value) +{ + GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (gst_pad_get_parent (pad)); + gboolean res; + + if (!musepackdec->dec) + return FALSE; + + switch (type) { + case GST_QUERY_TOTAL: + res = gst_pad_convert (pad, + GST_FORMAT_DEFAULT, musepackdec->len, + format, value); + break; + case GST_QUERY_POSITION: + res = gst_pad_convert (pad, + GST_FORMAT_DEFAULT, musepackdec->pos, + format, value); + break; + default: + res = FALSE; + break; + } + + return res; +} + +static gboolean +gst_musepackdec_src_convert (GstPad * pad, GstFormat src_format, + gint64 src_value, GstFormat * dest_format, gint64 * dest_value) +{ + GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (gst_pad_get_parent (pad)); + gboolean res = TRUE; + + if (!musepackdec->dec) + return FALSE; + + switch (src_format) { + case GST_FORMAT_DEFAULT: + switch (*dest_format) { + case GST_FORMAT_TIME: + *dest_value = src_value * GST_SECOND / musepackdec->rate; + break; + case GST_FORMAT_BYTES: + *dest_value = src_value * musepackdec->bps; + break; + default: + res = FALSE; + break; + } + break; + + case GST_FORMAT_TIME: + switch (*dest_format) { + case GST_FORMAT_DEFAULT: + *dest_value = src_value * musepackdec->rate / GST_SECOND; + break; + case GST_FORMAT_BYTES: + *dest_value = src_value * musepackdec->rate * + musepackdec->bps / GST_SECOND; + break; + default: + res = FALSE; + break; + } + break; + + case GST_FORMAT_BYTES: + switch (*dest_format) { + case GST_FORMAT_DEFAULT: + *dest_value = src_value / musepackdec->bps; + break; + case GST_FORMAT_TIME: + *dest_value = src_value * GST_SECOND / + (musepackdec->bps * musepackdec->rate); + break; + default: + res = FALSE; + break; + } + break; + + default: + res = FALSE; + break; + } + + return TRUE; +} + +static gboolean +gst_musepack_stream_init (GstMusepackDec * musepackdec) +{ + StreamInfo si = StreamInfo (); + + if (musepackdec->dec) + delete musepackdec->dec; + + musepackdec->reader = new GstMusepackReader (musepackdec->bs); + if (si.ReadStreamInfo (musepackdec->reader) != ERROR_CODE_OK) { + GST_ELEMENT_ERROR (musepackdec, STREAM, WRONG_TYPE, (NULL), (NULL)); + delete musepackdec->reader; + return FALSE; + } + + musepackdec->dec = new MPC_decoder (musepackdec->reader); + if (!musepackdec->dec) { + GST_ELEMENT_ERROR (musepackdec, LIBRARY, INIT, (NULL), + ("Creating decoder object failed")); + delete musepackdec->reader; + return FALSE; + } + + if (!musepackdec->dec->Initialize (&si)) { + GST_ELEMENT_ERROR (musepackdec, LIBRARY, INIT, (NULL), + ("Initializating stream failed")); + delete musepackdec->dec; + musepackdec->dec = NULL; + return FALSE; + } + + if (!gst_pad_set_explicit_caps (musepackdec->srcpad, + gst_caps_new_simple ("audio/x-raw-int", + "width", G_TYPE_INT, 32, + "depth", G_TYPE_INT, 32, + "endianness", G_TYPE_INT, G_BYTE_ORDER, + "channels", G_TYPE_INT, si.simple.Channels, + "rate", G_TYPE_INT, si.simple.SampleFreq, + "signed", G_TYPE_BOOLEAN, TRUE, + NULL))) { + GST_ELEMENT_ERROR (musepackdec, CORE, NEGOTIATION, (NULL), (NULL)); + delete musepackdec->dec; + musepackdec->dec = NULL; + return FALSE; + } + + musepackdec->bps = 4 * si.simple.Channels; + musepackdec->rate = si.simple.SampleFreq; + musepackdec->pos = 0; + musepackdec->len = si.simple.PCMSamples; + + return TRUE; +} + +static void +gst_musepackdec_loop (GstElement * element) +{ + GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (element); + GstBuffer *out; + GstFormat fmt; + + if (!musepackdec->dec) { + if (!gst_musepack_stream_init (musepackdec)) + return; + } + + if (musepackdec->seek_pending) { + gdouble seek_time = (gdouble) musepackdec->seek_time / GST_SECOND; + + musepackdec->seek_pending = FALSE; + if (musepackdec->dec->SeekSeconds (seek_time)) { + if (musepackdec->flush_pending) { + musepackdec->flush_pending = FALSE; + gst_pad_push (musepackdec->srcpad, + GST_DATA (gst_event_new (GST_EVENT_FLUSH))); + } + gst_pad_push (musepackdec->srcpad, + GST_DATA (gst_event_new_discontinuous (FALSE, + GST_FORMAT_TIME, musepackdec->seek_time, + GST_FORMAT_UNDEFINED))); + fmt = GST_FORMAT_DEFAULT; + gst_pad_convert (musepackdec->srcpad, + GST_FORMAT_TIME, musepackdec->seek_time, + &fmt, (gint64 *) &musepackdec->pos); + } + } + + out = gst_buffer_new_and_alloc (MPC_decoder::DecodeBufferLength * 4); + GST_BUFFER_SIZE (out) = + musepackdec->dec->Decode ((MPC_SAMPLE_FORMAT *) GST_BUFFER_DATA (out)); + if (GST_BUFFER_SIZE (out) == 0) { + if (musepackdec->reader->eos == true) { + gst_element_set_eos (element); + gst_pad_push (musepackdec->srcpad, + GST_DATA (gst_event_new (GST_EVENT_EOS))); + } + gst_buffer_unref (out); + return; + } + /* note that the size is still in samples */ + fmt = GST_FORMAT_TIME; + gst_pad_query (musepackdec->srcpad, + GST_QUERY_POSITION, &fmt, (gint64 *) &GST_BUFFER_TIMESTAMP (out)); + gst_pad_convert (musepackdec->srcpad, + GST_FORMAT_DEFAULT, GST_BUFFER_SIZE (out), + &fmt, (gint64 *) &GST_BUFFER_DURATION (out)); + musepackdec->pos += GST_BUFFER_SIZE (out); + /* convert to bytes */ + GST_BUFFER_SIZE (out) *= musepackdec->bps; + gst_pad_push (musepackdec->srcpad, GST_DATA (out)); +} + +static GstElementStateReturn +gst_musepackdec_change_state (GstElement * element) +{ + GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (element); + + switch (GST_STATE_TRANSITION (element)) { + case GST_STATE_NULL_TO_READY: + musepackdec->bs = gst_bytestream_new (musepackdec->sinkpad); + break; + case GST_STATE_PAUSED_TO_READY: + musepackdec->seek_pending = FALSE; + if (musepackdec->dec != NULL) { + delete musepackdec->reader; + delete musepackdec->dec; + musepackdec->dec = NULL; + } + break; + case GST_STATE_READY_TO_NULL: + gst_bytestream_destroy (musepackdec->bs); + break; + default: + break; + } + + if (GST_ELEMENT_CLASS (parent_class)->change_state) + return GST_ELEMENT_CLASS (parent_class)->change_state (element); + + return GST_STATE_SUCCESS; +} + +static gboolean +plugin_init (GstPlugin * plugin) +{ + return gst_library_load ("gstbytestream") && + gst_element_register (plugin, "musepackdec", + GST_RANK_PRIMARY, GST_TYPE_MUSEPACK_DEC); +} + +GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, + GST_VERSION_MINOR, + "musepack", + "Musepack decoder", + plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN) diff --git a/ext/musepack/gstmusepackdec.cpp b/ext/musepack/gstmusepackdec.cpp new file mode 100644 index 00000000..08f8d569 --- /dev/null +++ b/ext/musepack/gstmusepackdec.cpp @@ -0,0 +1,505 @@ +/* GStreamer Musepack decoder plugin + * Copyright (C) 2004 Ronald Bultje + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstmusepackdec.h" +#include "gstmusepackreader.h" + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/x-musepack") + ); + +static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/x-raw-int, " + "endianness = (int) BYTE_ORDER, " + "signed = (bool) TRUE, " + "width = (int) 32, " + "depth = (int) 32, " + "rate = (int) [ 8000, 96000 ], " + "channels = (int) [ 1, 2 ]") + ); + +static void gst_musepackdec_base_init (GstMusepackDecClass * klass); +static void gst_musepackdec_class_init (GstMusepackDecClass * klass); +static void gst_musepackdec_init (GstMusepackDec * musepackdec); + +static gboolean gst_musepackdec_src_event (GstPad * pad, GstEvent * event); +static const GstFormat *gst_musepackdec_get_formats (GstPad * pad); +static const GstEventMask *gst_musepackdec_get_event_masks (GstPad * pad); +static const GstQueryType *gst_musepackdec_get_query_types (GstPad * pad); +static gboolean gst_musepackdec_src_query (GstPad * pad, GstQueryType type, + GstFormat * format, gint64 * value); +static gboolean gst_musepackdec_src_convert (GstPad * pad, + GstFormat src_format, + gint64 src_value, GstFormat * dest_format, gint64 * dest_value); + +static void gst_musepackdec_loop (GstElement * element); +static GstElementStateReturn + gst_musepackdec_change_state (GstElement * element); + +static GstElementClass *parent_class = NULL; + +/* static guint gst_musepackdec_signals[LAST_SIGNAL] = { 0 }; */ + +GType +gst_musepackdec_get_type (void) +{ + static GType gst_musepackdec_type = 0; + + if (!gst_musepackdec_type) { + static const GTypeInfo gst_musepackdec_info = { + sizeof (GstMusepackDecClass), + (GBaseInitFunc) gst_musepackdec_base_init, + NULL, + (GClassInitFunc) gst_musepackdec_class_init, + NULL, + NULL, + sizeof (GstMusepackDec), + 0, + (GInstanceInitFunc) gst_musepackdec_init, + }; + + gst_musepackdec_type = g_type_register_static (GST_TYPE_ELEMENT, + "GstMusepackDec", &gst_musepackdec_info, (GTypeFlags) 0); + } + + return gst_musepackdec_type; +} + +static void +gst_musepackdec_base_init (GstMusepackDecClass * klass) +{ + static GstElementDetails gst_musepackdec_details = GST_ELEMENT_DETAILS ( + "Musepack decoder", + "Codec/Decoder/Audio", + "Musepack decoder", + "Ronald Bultje " + ); + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&src_template)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&sink_template)); + + gst_element_class_set_details (element_class, &gst_musepackdec_details); +} + +static void +gst_musepackdec_class_init (GstMusepackDecClass * klass) +{ + GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass); + + parent_class = GST_ELEMENT_CLASS (g_type_class_ref (GST_TYPE_ELEMENT)); + + gstelement_class->change_state = gst_musepackdec_change_state; +} + +static void +gst_musepackdec_init (GstMusepackDec * musepackdec) +{ + GST_FLAG_SET (musepackdec, GST_ELEMENT_EVENT_AWARE); + + musepackdec->dec = NULL; + musepackdec->seek_pending = FALSE; + + musepackdec->sinkpad = + gst_pad_new_from_template (gst_static_pad_template_get (&sink_template), + "sink"); + gst_element_add_pad (GST_ELEMENT (musepackdec), musepackdec->sinkpad); + + musepackdec->srcpad = + gst_pad_new_from_template (gst_static_pad_template_get (&src_template), + "src"); + gst_pad_set_event_function (musepackdec->srcpad, + GST_DEBUG_FUNCPTR (gst_musepackdec_src_event)); + gst_pad_set_event_mask_function (musepackdec->srcpad, + GST_DEBUG_FUNCPTR (gst_musepackdec_get_event_masks)); + gst_pad_set_query_function (musepackdec->srcpad, + GST_DEBUG_FUNCPTR (gst_musepackdec_src_query)); + gst_pad_set_query_type_function (musepackdec->srcpad, + GST_DEBUG_FUNCPTR (gst_musepackdec_get_query_types)); + gst_pad_set_convert_function (musepackdec->srcpad, + GST_DEBUG_FUNCPTR (gst_musepackdec_src_convert)); + gst_pad_set_formats_function (musepackdec->srcpad, + GST_DEBUG_FUNCPTR (gst_musepackdec_get_formats)); + gst_pad_use_explicit_caps (musepackdec->srcpad); + gst_element_add_pad (GST_ELEMENT (musepackdec), musepackdec->srcpad); + + gst_element_set_loop_function (GST_ELEMENT (musepackdec), + gst_musepackdec_loop); +} + +static gboolean +gst_musepackdec_src_event (GstPad * pad, GstEvent * event) +{ + GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (gst_pad_get_parent (pad)); + gboolean res; + + switch (GST_EVENT_TYPE (event)) { + case GST_EVENT_SEEK: { + gint64 offset, len, pos; + GstFormat fmt = GST_FORMAT_TIME; + + /* in time */ + if (!gst_pad_convert (pad, + (GstFormat) GST_EVENT_SEEK_FORMAT (event), + GST_EVENT_SEEK_OFFSET (event), + &fmt, &offset) || + !gst_pad_convert (pad, + GST_FORMAT_DEFAULT, musepackdec->len, + &fmt, &len) || + !gst_pad_convert (pad, + GST_FORMAT_DEFAULT, musepackdec->pos, + &fmt, &pos)) { + res = FALSE; + break; + } + + /* offset from start */ + switch (GST_EVENT_SEEK_METHOD (event)) { + case GST_SEEK_METHOD_SET: + break; + case GST_SEEK_METHOD_CUR: + offset += pos; + break; + case GST_SEEK_METHOD_END: + offset = len - offset; + break; + default: + res = FALSE; + goto done; + } + + /* only valid seeks */ + if (offset >= len || offset < 0) { + res = FALSE; + break; + } + + /* store */ + musepackdec->seek_pending = TRUE; + musepackdec->flush_pending = + GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH; + musepackdec->seek_time = offset; + res = TRUE; + break; + } + default: + res = FALSE; + break; + } + +done: + gst_event_unref (event); + + return res; +} + +static const GstFormat * +gst_musepackdec_get_formats (GstPad * pad) +{ + static const GstFormat formats[] = { + GST_FORMAT_BYTES, + GST_FORMAT_DEFAULT, + GST_FORMAT_TIME, + (GstFormat) 0 + }; + + return formats; +} + +static const GstEventMask * +gst_musepackdec_get_event_masks (GstPad * pad) +{ + static const GstEventMask event_masks[] = { + { GST_EVENT_SEEK, + (GstEventFlag) (GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH) }, + { (GstEventType) 0, (GstEventFlag) 0 } + }; + + return event_masks; +} + +static const GstQueryType * +gst_musepackdec_get_query_types (GstPad * pad) +{ + static const GstQueryType query_types[] = { + GST_QUERY_TOTAL, + GST_QUERY_POSITION, + (GstQueryType) 0 + }; + + return query_types; +} + +static gboolean +gst_musepackdec_src_query (GstPad * pad, GstQueryType type, + GstFormat * format, gint64 * value) +{ + GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (gst_pad_get_parent (pad)); + gboolean res; + + if (!musepackdec->dec) + return FALSE; + + switch (type) { + case GST_QUERY_TOTAL: + res = gst_pad_convert (pad, + GST_FORMAT_DEFAULT, musepackdec->len, + format, value); + break; + case GST_QUERY_POSITION: + res = gst_pad_convert (pad, + GST_FORMAT_DEFAULT, musepackdec->pos, + format, value); + break; + default: + res = FALSE; + break; + } + + return res; +} + +static gboolean +gst_musepackdec_src_convert (GstPad * pad, GstFormat src_format, + gint64 src_value, GstFormat * dest_format, gint64 * dest_value) +{ + GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (gst_pad_get_parent (pad)); + gboolean res = TRUE; + + if (!musepackdec->dec) + return FALSE; + + switch (src_format) { + case GST_FORMAT_DEFAULT: + switch (*dest_format) { + case GST_FORMAT_TIME: + *dest_value = src_value * GST_SECOND / musepackdec->rate; + break; + case GST_FORMAT_BYTES: + *dest_value = src_value * musepackdec->bps; + break; + default: + res = FALSE; + break; + } + break; + + case GST_FORMAT_TIME: + switch (*dest_format) { + case GST_FORMAT_DEFAULT: + *dest_value = src_value * musepackdec->rate / GST_SECOND; + break; + case GST_FORMAT_BYTES: + *dest_value = src_value * musepackdec->rate * + musepackdec->bps / GST_SECOND; + break; + default: + res = FALSE; + break; + } + break; + + case GST_FORMAT_BYTES: + switch (*dest_format) { + case GST_FORMAT_DEFAULT: + *dest_value = src_value / musepackdec->bps; + break; + case GST_FORMAT_TIME: + *dest_value = src_value * GST_SECOND / + (musepackdec->bps * musepackdec->rate); + break; + default: + res = FALSE; + break; + } + break; + + default: + res = FALSE; + break; + } + + return TRUE; +} + +static gboolean +gst_musepack_stream_init (GstMusepackDec * musepackdec) +{ + StreamInfo si = StreamInfo (); + + if (musepackdec->dec) + delete musepackdec->dec; + + musepackdec->reader = new GstMusepackReader (musepackdec->bs); + if (si.ReadStreamInfo (musepackdec->reader) != ERROR_CODE_OK) { + GST_ELEMENT_ERROR (musepackdec, STREAM, WRONG_TYPE, (NULL), (NULL)); + delete musepackdec->reader; + return FALSE; + } + + musepackdec->dec = new MPC_decoder (musepackdec->reader); + if (!musepackdec->dec) { + GST_ELEMENT_ERROR (musepackdec, LIBRARY, INIT, (NULL), + ("Creating decoder object failed")); + delete musepackdec->reader; + return FALSE; + } + + if (!musepackdec->dec->Initialize (&si)) { + GST_ELEMENT_ERROR (musepackdec, LIBRARY, INIT, (NULL), + ("Initializating stream failed")); + delete musepackdec->dec; + musepackdec->dec = NULL; + return FALSE; + } + + if (!gst_pad_set_explicit_caps (musepackdec->srcpad, + gst_caps_new_simple ("audio/x-raw-int", + "width", G_TYPE_INT, 32, + "depth", G_TYPE_INT, 32, + "endianness", G_TYPE_INT, G_BYTE_ORDER, + "channels", G_TYPE_INT, si.simple.Channels, + "rate", G_TYPE_INT, si.simple.SampleFreq, + "signed", G_TYPE_BOOLEAN, TRUE, + NULL))) { + GST_ELEMENT_ERROR (musepackdec, CORE, NEGOTIATION, (NULL), (NULL)); + delete musepackdec->dec; + musepackdec->dec = NULL; + return FALSE; + } + + musepackdec->bps = 4 * si.simple.Channels; + musepackdec->rate = si.simple.SampleFreq; + musepackdec->pos = 0; + musepackdec->len = si.simple.PCMSamples; + + return TRUE; +} + +static void +gst_musepackdec_loop (GstElement * element) +{ + GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (element); + GstBuffer *out; + GstFormat fmt; + + if (!musepackdec->dec) { + if (!gst_musepack_stream_init (musepackdec)) + return; + } + + if (musepackdec->seek_pending) { + gdouble seek_time = (gdouble) musepackdec->seek_time / GST_SECOND; + + musepackdec->seek_pending = FALSE; + if (musepackdec->dec->SeekSeconds (seek_time)) { + if (musepackdec->flush_pending) { + musepackdec->flush_pending = FALSE; + gst_pad_push (musepackdec->srcpad, + GST_DATA (gst_event_new (GST_EVENT_FLUSH))); + } + gst_pad_push (musepackdec->srcpad, + GST_DATA (gst_event_new_discontinuous (FALSE, + GST_FORMAT_TIME, musepackdec->seek_time, + GST_FORMAT_UNDEFINED))); + fmt = GST_FORMAT_DEFAULT; + gst_pad_convert (musepackdec->srcpad, + GST_FORMAT_TIME, musepackdec->seek_time, + &fmt, (gint64 *) &musepackdec->pos); + } + } + + out = gst_buffer_new_and_alloc (MPC_decoder::DecodeBufferLength * 4); + GST_BUFFER_SIZE (out) = + musepackdec->dec->Decode ((MPC_SAMPLE_FORMAT *) GST_BUFFER_DATA (out)); + if (GST_BUFFER_SIZE (out) == 0) { + if (musepackdec->reader->eos == true) { + gst_element_set_eos (element); + gst_pad_push (musepackdec->srcpad, + GST_DATA (gst_event_new (GST_EVENT_EOS))); + } + gst_buffer_unref (out); + return; + } + /* note that the size is still in samples */ + fmt = GST_FORMAT_TIME; + gst_pad_query (musepackdec->srcpad, + GST_QUERY_POSITION, &fmt, (gint64 *) &GST_BUFFER_TIMESTAMP (out)); + gst_pad_convert (musepackdec->srcpad, + GST_FORMAT_DEFAULT, GST_BUFFER_SIZE (out), + &fmt, (gint64 *) &GST_BUFFER_DURATION (out)); + musepackdec->pos += GST_BUFFER_SIZE (out); + /* convert to bytes */ + GST_BUFFER_SIZE (out) *= musepackdec->bps; + gst_pad_push (musepackdec->srcpad, GST_DATA (out)); +} + +static GstElementStateReturn +gst_musepackdec_change_state (GstElement * element) +{ + GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (element); + + switch (GST_STATE_TRANSITION (element)) { + case GST_STATE_NULL_TO_READY: + musepackdec->bs = gst_bytestream_new (musepackdec->sinkpad); + break; + case GST_STATE_PAUSED_TO_READY: + musepackdec->seek_pending = FALSE; + if (musepackdec->dec != NULL) { + delete musepackdec->reader; + delete musepackdec->dec; + musepackdec->dec = NULL; + } + break; + case GST_STATE_READY_TO_NULL: + gst_bytestream_destroy (musepackdec->bs); + break; + default: + break; + } + + if (GST_ELEMENT_CLASS (parent_class)->change_state) + return GST_ELEMENT_CLASS (parent_class)->change_state (element); + + return GST_STATE_SUCCESS; +} + +static gboolean +plugin_init (GstPlugin * plugin) +{ + return gst_library_load ("gstbytestream") && + gst_element_register (plugin, "musepackdec", + GST_RANK_PRIMARY, GST_TYPE_MUSEPACK_DEC); +} + +GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, + GST_VERSION_MINOR, + "musepack", + "Musepack decoder", + plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN) diff --git a/ext/musepack/gstmusepackdec.h b/ext/musepack/gstmusepackdec.h new file mode 100644 index 00000000..a5626192 --- /dev/null +++ b/ext/musepack/gstmusepackdec.h @@ -0,0 +1,73 @@ +/* GStreamer Musepack decoder plugin + * Copyright (C) 2004 Ronald Bultje + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __GST_MUSEPACK_DEC_H__ +#define __GST_MUSEPACK_DEC_H__ + +#include +#include +#include +#include "gstmusepackreader.h" + +G_BEGIN_DECLS + +#define GST_TYPE_MUSEPACK_DEC \ + (gst_musepackdec_get_type ()) +#define GST_MUSEPACK_DEC(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_MUSEPACK_DEC, \ + GstMusepackDec)) +#define GST_MUSEPACK_DEC_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_MUSEPACK_DEC, \ + GstMusepackDecClass)) +#define GST_IS_MUSEPACK_DEC(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_MUSEPACK_DEC)) +#define GST_IS_MUSEPACK_DEC_CLASS(obj) \ + (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_MUSEPACK_DEC)) + +typedef struct _GstMusepackDec { + GstElement element; + + /* pads */ + GstPad *srcpad, *sinkpad; + GstByteStream *bs; + + /* MUSEPACK_DEC object */ + MPC_decoder *dec; + GstMusepackReader *reader; + + /* bytes-per-sample */ + int bps, rate; + + /* position and length, in samples */ + guint64 pos, len; + + /* seeks */ + gdouble flush_pending, seek_pending; + guint64 seek_time; +} GstMusepackDec; + +typedef struct _GstMusepackDecClass { + GstElementClass parent_class; +} GstMusepackDecClass; + +GType gst_musepackdec_get_type (void); + +G_END_DECLS + +#endif /* __GST_MUSEPACK_DEC_H__ */ diff --git a/ext/musepack/gstmusepackreader.c b/ext/musepack/gstmusepackreader.c new file mode 100644 index 00000000..e26baabc --- /dev/null +++ b/ext/musepack/gstmusepackreader.c @@ -0,0 +1,145 @@ +/* GStreamer Musepack decoder plugin + * Copyright (C) 2004 Ronald Bultje + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include + +#include "gstmusepackreader.h" + +GstMusepackReader::GstMusepackReader (GstByteStream *bs) +{ + this->bs = bs; + this->eos = false; +} + +GstMusepackReader::~GstMusepackReader (void) +{ +} + +mpc_int32_t +GstMusepackReader::read (void * ptr, mpc_int32_t size) +{ + guint8 *data; + gint read; + + do { + read = gst_bytestream_peek_bytes (this->bs, &data, size); + + if (read != size) { + GstEvent *event; + guint32 remaining; + + gst_bytestream_get_status (this->bs, &remaining, &event); + if (!event) { + GST_ELEMENT_ERROR (gst_pad_get_parent (this->bs->pad), + RESOURCE, READ, (NULL), (NULL)); + goto done; + } + + switch (GST_EVENT_TYPE (event)) { + case GST_EVENT_INTERRUPT: + gst_event_unref (event); + goto done; + case GST_EVENT_EOS: + this->eos = true; + gst_event_unref (event); + goto done; + default: + gst_pad_event_default (this->bs->pad, event); + break; + } + } + } while (read != size); + +done: + if (read != 0) { + memcpy (ptr, data, read); + gst_bytestream_flush_fast (this->bs, read); + } + + return read; +} + +bool +GstMusepackReader::seek (mpc_int32_t offset) +{ + guint8 *dummy; + + /* hacky hack - if we're after typefind, we'll fail because + * typefind is still typefinding (heh :) ). So read first. */ + if (this->tell () == 0) { + guint8 dummy2[1]; + this->read (dummy2, 1); + } + + if (!gst_bytestream_seek (this->bs, offset, GST_SEEK_METHOD_SET)) + return FALSE; + + /* get discont */ + while (gst_bytestream_peek_bytes (this->bs, &dummy, 1) != 1) { + GstEvent *event; + guint32 remaining; + + gst_bytestream_get_status (this->bs, &remaining, &event); + if (!event) { + GST_ELEMENT_ERROR (gst_pad_get_parent (this->bs->pad), + RESOURCE, SEEK, (NULL), (NULL)); + return false; + } + switch (GST_EVENT_TYPE (event)) { + case GST_EVENT_EOS: + g_warning ("EOS!"); + gst_event_unref (event); + return false; + case GST_EVENT_DISCONTINUOUS: + gst_event_unref (event); + return true; + case GST_EVENT_INTERRUPT: + g_warning ("interrupt!"); + return false; + default: + gst_pad_event_default (this->bs->pad, event); + break; + } + } + + return false; +} + +mpc_int32_t +GstMusepackReader::tell (void) +{ + return gst_bytestream_tell (this->bs); +} + +mpc_int32_t +GstMusepackReader::get_size (void) +{ + return gst_bytestream_length (this->bs); +} + +bool +GstMusepackReader::canseek (void) +{ + return true; +} diff --git a/ext/musepack/gstmusepackreader.cpp b/ext/musepack/gstmusepackreader.cpp new file mode 100644 index 00000000..e26baabc --- /dev/null +++ b/ext/musepack/gstmusepackreader.cpp @@ -0,0 +1,145 @@ +/* GStreamer Musepack decoder plugin + * Copyright (C) 2004 Ronald Bultje + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include + +#include "gstmusepackreader.h" + +GstMusepackReader::GstMusepackReader (GstByteStream *bs) +{ + this->bs = bs; + this->eos = false; +} + +GstMusepackReader::~GstMusepackReader (void) +{ +} + +mpc_int32_t +GstMusepackReader::read (void * ptr, mpc_int32_t size) +{ + guint8 *data; + gint read; + + do { + read = gst_bytestream_peek_bytes (this->bs, &data, size); + + if (read != size) { + GstEvent *event; + guint32 remaining; + + gst_bytestream_get_status (this->bs, &remaining, &event); + if (!event) { + GST_ELEMENT_ERROR (gst_pad_get_parent (this->bs->pad), + RESOURCE, READ, (NULL), (NULL)); + goto done; + } + + switch (GST_EVENT_TYPE (event)) { + case GST_EVENT_INTERRUPT: + gst_event_unref (event); + goto done; + case GST_EVENT_EOS: + this->eos = true; + gst_event_unref (event); + goto done; + default: + gst_pad_event_default (this->bs->pad, event); + break; + } + } + } while (read != size); + +done: + if (read != 0) { + memcpy (ptr, data, read); + gst_bytestream_flush_fast (this->bs, read); + } + + return read; +} + +bool +GstMusepackReader::seek (mpc_int32_t offset) +{ + guint8 *dummy; + + /* hacky hack - if we're after typefind, we'll fail because + * typefind is still typefinding (heh :) ). So read first. */ + if (this->tell () == 0) { + guint8 dummy2[1]; + this->read (dummy2, 1); + } + + if (!gst_bytestream_seek (this->bs, offset, GST_SEEK_METHOD_SET)) + return FALSE; + + /* get discont */ + while (gst_bytestream_peek_bytes (this->bs, &dummy, 1) != 1) { + GstEvent *event; + guint32 remaining; + + gst_bytestream_get_status (this->bs, &remaining, &event); + if (!event) { + GST_ELEMENT_ERROR (gst_pad_get_parent (this->bs->pad), + RESOURCE, SEEK, (NULL), (NULL)); + return false; + } + switch (GST_EVENT_TYPE (event)) { + case GST_EVENT_EOS: + g_warning ("EOS!"); + gst_event_unref (event); + return false; + case GST_EVENT_DISCONTINUOUS: + gst_event_unref (event); + return true; + case GST_EVENT_INTERRUPT: + g_warning ("interrupt!"); + return false; + default: + gst_pad_event_default (this->bs->pad, event); + break; + } + } + + return false; +} + +mpc_int32_t +GstMusepackReader::tell (void) +{ + return gst_bytestream_tell (this->bs); +} + +mpc_int32_t +GstMusepackReader::get_size (void) +{ + return gst_bytestream_length (this->bs); +} + +bool +GstMusepackReader::canseek (void) +{ + return true; +} diff --git a/ext/musepack/gstmusepackreader.h b/ext/musepack/gstmusepackreader.h new file mode 100644 index 00000000..e2e0bf21 --- /dev/null +++ b/ext/musepack/gstmusepackreader.h @@ -0,0 +1,43 @@ +/* GStreamer Musepack decoder plugin + * Copyright (C) 2004 Ronald Bultje + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __GST_MUSEPACK_READER_H__ +#define __GST_MUSEPACK_READER_H__ + +#include +#include + +class GstMusepackReader : public MPC_reader { +public: + GstMusepackReader (GstByteStream *bs); + virtual ~GstMusepackReader (void); + + mpc_int32_t read (void * ptr, mpc_int32_t size); + bool seek (mpc_int32_t offset); + mpc_int32_t tell (void); + mpc_int32_t get_size (void); + bool canseek (void); + + bool eos; + +private: + GstByteStream *bs; +}; + +#endif /* __GST_MUSEPACK_READER_H__ */ -- cgit v1.2.1