/* Copyright (C) 2004-2005,2009 Michael Pyne * Copyright (C) 2004-2006 Chris Lee * Copyright (C) 2007 Brian Koropoff * * 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 "gstgme.h" #include #include #include static const GstElementDetails gst_spc_dec_details = GST_ELEMENT_DETAILS ("SNES SPC 700 decoder", "Codec/Audio/Decoder", "Uses Blargg's libgme to emulate an SPC processor", "Chris Lee , Brian Koropoff , Michael Pyne "); static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-spc")); static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-int, " "endianness = (int) BYTE_ORDER, " "signed = (boolean) TRUE, " "width = (int) 16, " "depth = (int) 16, " "rate = (int) 32000, " "channels = (int) 2")); GST_BOILERPLATE (GstSpcDec, gst_spc_dec, GstElement, GST_TYPE_ELEMENT); static GstFlowReturn gst_spc_dec_chain (GstPad * pad, GstBuffer * buffer); static gboolean gst_spc_dec_sink_event (GstPad * pad, GstEvent * event); static gboolean gst_spc_dec_src_event (GstPad * pad, GstEvent * event); static gboolean gst_spc_dec_src_query (GstPad * pad, GstQuery * query); static GstStateChangeReturn gst_spc_dec_change_state (GstElement * element, GstStateChange transition); static void gst_spc_play (GstPad * pad); static void gst_spc_dec_dispose (GObject * object); static gboolean spc_setup (GstSpcDec * spc); static gboolean spc_negotiate (GstSpcDec * spc) { GstCaps *allowed, *caps; GstStructure *structure; gint width = 16, depth = 16; gboolean sign; int rate = 32000; int channels = 2; allowed = gst_pad_get_allowed_caps (spc->srcpad); if (!allowed) { GST_DEBUG_OBJECT (spc, "couldn't get allowed caps"); return FALSE; } GST_DEBUG_OBJECT (spc, "allowed caps: %" GST_PTR_FORMAT, allowed); structure = gst_caps_get_structure (allowed, 0); gst_structure_get_int (structure, "width", &width); gst_structure_get_int (structure, "depth", &depth); if (width && depth && width != depth) { GST_DEBUG_OBJECT (spc, "width %d and depth %d are different", width, depth); gst_caps_unref (allowed); return FALSE; } gst_structure_get_boolean (structure, "signed", &sign); gst_structure_get_int (structure, "rate", &rate); gst_structure_get_int (structure, "channels", &channels); caps = gst_caps_new_simple ("audio/x-raw-int", "endianness", G_TYPE_INT, G_BYTE_ORDER, "signed", G_TYPE_BOOLEAN, TRUE, "width", G_TYPE_INT, width, "depth", G_TYPE_INT, depth, "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, channels, NULL); gst_pad_set_caps (spc->srcpad, caps); gst_caps_unref (caps); gst_caps_unref (allowed); return TRUE; } static void gst_spc_dec_base_init (gpointer g_class) { GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); gst_element_class_set_details (element_class, &gst_spc_dec_details); gst_element_class_add_pad_template (element_class, gst_static_pad_template_get (&sink_factory)); gst_element_class_add_pad_template (element_class, gst_static_pad_template_get (&src_factory)); } static void gst_spc_dec_class_init (GstSpcDecClass * klass) { GstElementClass *element_class = (GstElementClass *) klass; GObjectClass *gobject_class = (GObjectClass *) klass; element_class->change_state = GST_DEBUG_FUNCPTR (gst_spc_dec_change_state); gobject_class->dispose = gst_spc_dec_dispose; } static const GstQueryType * gst_spc_dec_src_query_type (GstPad * pad) { static const GstQueryType query_types[] = { GST_QUERY_DURATION, GST_QUERY_POSITION, (GstQueryType) 0 }; return query_types; } static void gst_spc_dec_init (GstSpcDec * spc, GstSpcDecClass * klass) { spc->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink"); /* gst_pad_set_query_function (spc->sinkpad, NULL); */ gst_pad_set_event_function (spc->sinkpad, gst_spc_dec_sink_event); gst_pad_set_chain_function (spc->sinkpad, gst_spc_dec_chain); gst_element_add_pad (GST_ELEMENT (spc), spc->sinkpad); spc->srcpad = gst_pad_new_from_static_template (&src_factory, "src"); gst_pad_set_event_function (spc->srcpad, gst_spc_dec_src_event); gst_pad_set_query_function (spc->srcpad, gst_spc_dec_src_query); gst_pad_set_query_type_function (spc->srcpad, gst_spc_dec_src_query_type); gst_pad_use_fixed_caps (spc->srcpad); gst_element_add_pad (GST_ELEMENT (spc), spc->srcpad); spc->buf = NULL; spc->player = NULL; spc->total_duration = GST_CLOCK_TIME_NONE; spc->initialized = FALSE; } static void gst_spc_dec_dispose (GObject * object) { GstSpcDec *spc = GST_SPC_DEC (object); if (spc->buf) { gst_buffer_unref (spc->buf); spc->buf = NULL; } } static GstFlowReturn gst_spc_dec_chain (GstPad * pad, GstBuffer * buffer) { GstSpcDec *spc = GST_SPC_DEC (gst_pad_get_parent (pad)); /* Accumulate SPC data until end-of-stream, then commence playback. */ if (spc->buf) { spc->buf = gst_buffer_join (spc->buf, buffer); } else { spc->buf = buffer; } gst_object_unref (spc); return GST_FLOW_OK; } static gboolean gst_spc_dec_sink_event (GstPad * pad, GstEvent * event) { GstSpcDec *spc = GST_SPC_DEC (gst_pad_get_parent (pad)); gboolean result = TRUE; gboolean forward = FALSE; switch (GST_EVENT_TYPE (event)) { case GST_EVENT_EOS: /* we get EOS when we loaded the complete file, now try to initialize the * decoding */ if (!(result = spc_setup (spc))) { /* can't start, post an ERROR and push EOS downstream */ GST_ELEMENT_ERROR (spc, STREAM, DEMUX, (NULL), ("can't start playback")); forward = TRUE; } break; default: break; } if (forward) result = gst_pad_push_event (spc->srcpad, event); else gst_event_unref (event); gst_object_unref (spc); return result; } static gboolean gst_spc_dec_src_event (GstPad * pad, GstEvent * event) { GstSpcDec *spc = GST_SPC_DEC (gst_pad_get_parent (pad)); gboolean result = FALSE; switch (GST_EVENT_TYPE (event)) { case GST_EVENT_SEEK: { gdouble rate; GstFormat format; GstSeekFlags flags; GstSeekType start_type, stop_type; gint64 start, stop; gboolean flush; gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start, &stop_type, &stop); gst_event_unref (event); if (format != GST_FORMAT_TIME) { GST_DEBUG_OBJECT (spc, "seeking is only supported in TIME format"); break; } if (start_type != GST_SEEK_TYPE_SET || stop_type != GST_SEEK_TYPE_NONE) { GST_DEBUG_OBJECT (spc, "unsupported seek type"); break; } if (stop_type == GST_SEEK_TYPE_NONE) stop = GST_CLOCK_TIME_NONE; if (start_type == GST_SEEK_TYPE_SET) { guint64 cur = gme_tell (spc->player) * GST_MSECOND; guint64 dest = (guint64) start; if (spc->total_duration != GST_CLOCK_TIME_NONE) dest = CLAMP (dest, 0, spc->total_duration); else dest = MAX (0, dest); if (dest == cur) break; flush = (flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH; if (flush) { gst_pad_push_event (spc->srcpad, gst_event_new_flush_start ()); } else { gst_pad_stop_task (spc->srcpad); } GST_PAD_STREAM_LOCK (spc->srcpad); if (flags & GST_SEEK_FLAG_SEGMENT) { gst_element_post_message (GST_ELEMENT (spc), gst_message_new_segment_start (GST_OBJECT (spc), format, cur)); } if (flush) { gst_pad_push_event (spc->srcpad, gst_event_new_flush_stop ()); } if (stop == GST_CLOCK_TIME_NONE && spc->total_duration != GST_CLOCK_TIME_NONE) stop = spc->total_duration; gst_pad_push_event (spc->srcpad, gst_event_new_new_segment (FALSE, rate, GST_FORMAT_TIME, dest, stop, dest)); spc->seekpoint = dest / GST_MSECOND; /* nsecs to msecs */ spc->seeking = TRUE; gst_pad_start_task (spc->srcpad, (GstTaskFunction) gst_spc_play, spc->srcpad); GST_PAD_STREAM_UNLOCK (spc->srcpad); result = TRUE; } break; } default: result = gst_pad_push_event (spc->sinkpad, event); break; } gst_object_unref (spc); return result; } static gboolean gst_spc_dec_src_query (GstPad * pad, GstQuery * query) { GstSpcDec *spc = GST_SPC_DEC (gst_pad_get_parent (pad)); gboolean result = TRUE; switch (GST_QUERY_TYPE (query)) { case GST_QUERY_DURATION: { GstFormat format; gst_query_parse_duration (query, &format, NULL); if (!spc->initialized || format != GST_FORMAT_TIME || spc->total_duration == GST_CLOCK_TIME_NONE) { result = FALSE; break; } gst_query_set_duration (query, GST_FORMAT_TIME, spc->total_duration); break; } case GST_QUERY_POSITION: { GstFormat format; gst_query_parse_position (query, &format, NULL); if (!spc->initialized || format != GST_FORMAT_TIME) { result = FALSE; break; } gst_query_set_position (query, GST_FORMAT_TIME, (gint64) gme_tell (spc->player) * GST_MSECOND); break; } default: result = gst_pad_query_default (pad, query); break; } gst_object_unref (spc); return result; } static void gst_spc_play (GstPad * pad) { GstSpcDec *spc = GST_SPC_DEC (gst_pad_get_parent (pad)); GstFlowReturn flow_return; GstBuffer *out; gboolean seeking = spc->seeking; gme_err_t gme_err = NULL; const int NUM_SAMPLES = 1600; /* 4 bytes (stereo 16-bit) per sample */ if (!seeking) { out = gst_buffer_new_and_alloc (NUM_SAMPLES * 4); gst_buffer_set_caps (out, GST_PAD_CAPS (pad)); GST_BUFFER_TIMESTAMP (out) = gme_tell (spc->player) * GST_MSECOND; gme_err = gme_play (spc->player, NUM_SAMPLES * 2, (short *) GST_BUFFER_DATA (out)); if (gme_err) { GST_ELEMENT_ERROR (spc, STREAM, DEMUX, (NULL), (gme_err)); gst_pad_pause_task (pad); gst_pad_push_event (pad, gst_event_new_eos ()); gst_object_unref (spc); return; } } else { gme_seek (spc->player, spc->seekpoint); spc->seeking = FALSE; out = gst_buffer_new (); gst_buffer_set_caps (out, GST_PAD_CAPS (pad)); } if ((flow_return = gst_pad_push (spc->srcpad, out)) != GST_FLOW_OK) { GST_DEBUG_OBJECT (spc, "pausing task, reason %s", gst_flow_get_name (flow_return)); gst_pad_pause_task (pad); if (GST_FLOW_IS_FATAL (flow_return) || flow_return == GST_FLOW_NOT_LINKED) { gst_pad_push_event (pad, gst_event_new_eos ()); } } if (gme_track_ended (spc->player)) { gst_pad_pause_task (pad); gst_pad_push_event (pad, gst_event_new_eos ()); } gst_object_unref (spc); return; } static gboolean spc_setup (GstSpcDec * spc) { gme_info_t *info; gme_err_t gme_err = NULL; GstTagList *taglist; guint64 total_duration; if (!spc->buf || !spc_negotiate (spc)) { return FALSE; } gme_err = gme_open_data (GST_BUFFER_DATA (spc->buf), GST_BUFFER_SIZE (spc->buf), &spc->player, 32000); if (gme_err || !spc->player) { if (spc->player) { gme_delete (spc->player); spc->player = NULL; } GST_ELEMENT_ERROR (spc, STREAM, DEMUX, (NULL), (gme_err)); return FALSE; } gme_err = gme_track_info (spc->player, &info, 0); taglist = gst_tag_list_new (); if (info->song) gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_TITLE, info->song, NULL); if (info->author) gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_ARTIST, info->author, NULL); /* Prefer the name of the official soundtrack over the name of the game (since this is * how track numbers are derived) */ if (info->game) gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_ALBUM, info->game, NULL); if (info->comment) gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_COMMENT, info->comment, NULL); if (info->dumper) gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_CONTACT, info->dumper, NULL); if (info->copyright) gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_COPYRIGHT, info->copyright, NULL); if (info->system) gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_ENCODER, info->system, NULL); spc->total_duration = total_duration = gst_util_uint64_scale_int (info->play_length, GST_MSECOND, 1); gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_DURATION, total_duration, NULL); gst_element_found_tags_for_pad (GST_ELEMENT (spc), spc->srcpad, taglist); g_free (info); #ifdef HAVE_LIBGME_ACCURACY /* TODO: Is it worth it to make this optional? */ gme_enable_accuracy (spc->player, 1); #endif gme_start_track (spc->player, 0); gst_pad_push_event (spc->srcpad, gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0)); gst_pad_start_task (spc->srcpad, (GstTaskFunction) gst_spc_play, spc->srcpad); /* We can't unreference this buffer because we might need to re-initialize * the emulator with the original data during a reverse seek * gst_buffer_unref (spc->buf); * spc->buf = NULL; */ spc->initialized = TRUE; spc->seeking = FALSE; spc->seekpoint = 0; return spc->initialized; } static GstStateChangeReturn gst_spc_dec_change_state (GstElement * element, GstStateChange transition) { GstStateChangeReturn result; GstSpcDec *dec; dec = GST_SPC_DEC (element); switch (transition) { case GST_STATE_CHANGE_READY_TO_PAUSED: dec->total_duration = GST_CLOCK_TIME_NONE; break; default: break; } result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); if (result == GST_STATE_CHANGE_FAILURE) return result; switch (transition) { case GST_STATE_CHANGE_PAUSED_TO_READY: if (dec->buf) { gst_buffer_unref (dec->buf); dec->buf = NULL; } break; default: break; } return result; } static gboolean plugin_init (GstPlugin * plugin) { return gst_element_register (plugin, "spcdec", GST_RANK_PRIMARY, GST_TYPE_SPC_DEC); } GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR, "spcdec", "OpenSPC Audio Decoder", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);