1 /* Copyright (C) 2004-2005,2009 Michael Pyne <mpyne at kde org>
2 * Copyright (C) 2004-2006 Chris Lee <clee at kde org>
3 * Copyright (C) 2007 Brian Koropoff <bkoropoff at gmail com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
28 #include <glib/gprintf.h>
31 static GstStaticPadTemplate sink_factory =
32 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
33 GST_STATIC_CAPS ("audio/x-ay; "
38 "audio/x-nsf; " "audio/x-sap; " "audio/x-spc; " "audio/x-vgm"));
40 static GstStaticPadTemplate src_factory =
41 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
42 GST_STATIC_CAPS ("audio/x-raw-int, "
43 "endianness = (int) BYTE_ORDER, "
44 "signed = (boolean) TRUE, "
46 "depth = (int) 16, " "rate = (int) 32000, " "channels = (int) 2"));
48 #define gst_gme_dec_parent_class parent_class
49 G_DEFINE_TYPE (GstGmeDec, gst_gme_dec, GST_TYPE_ELEMENT);
51 static GstFlowReturn gst_gme_dec_chain (GstPad * pad, GstObject * parent,
53 static gboolean gst_gme_dec_sink_event (GstPad * pad, GstObject * parent,
55 static gboolean gst_gme_dec_src_event (GstPad * pad, GstObject * parent,
57 static gboolean gst_gme_dec_src_query (GstPad * pad, GstObject * parent,
59 static GstStateChangeReturn gst_gme_dec_change_state (GstElement * element,
60 GstStateChange transition);
61 static void gst_gme_play (GstPad * pad);
62 static void gst_gme_dec_dispose (GObject * object);
63 static gboolean gme_setup (GstGmeDec * gme);
66 gme_negotiate (GstGmeDec * gme)
68 GstCaps *allowed, *caps;
69 GstStructure *structure;
70 gint width = 16, depth = 16;
75 allowed = gst_pad_get_allowed_caps (gme->srcpad);
77 GST_DEBUG_OBJECT (gme, "couldn't get allowed caps");
81 GST_DEBUG_OBJECT (gme, "allowed caps: %" GST_PTR_FORMAT, allowed);
83 structure = gst_caps_get_structure (allowed, 0);
84 gst_structure_get_int (structure, "width", &width);
85 gst_structure_get_int (structure, "depth", &depth);
87 if (width && depth && width != depth) {
88 GST_DEBUG_OBJECT (gme, "width %d and depth %d are different", width, depth);
89 gst_caps_unref (allowed);
93 gst_structure_get_boolean (structure, "signed", &sign);
94 gst_structure_get_int (structure, "rate", &rate);
95 gst_structure_get_int (structure, "channels", &channels);
97 caps = gst_caps_new_simple ("audio/x-raw-int",
98 "endianness", G_TYPE_INT, G_BYTE_ORDER,
99 "signed", G_TYPE_BOOLEAN, TRUE,
100 "width", G_TYPE_INT, width,
101 "depth", G_TYPE_INT, depth,
102 "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, channels, NULL);
103 gst_pad_set_caps (gme->srcpad, caps);
105 gst_caps_unref (caps);
106 gst_caps_unref (allowed);
112 gst_gme_dec_class_init (GstGmeDecClass * klass)
114 GObjectClass *gobject_class = (GObjectClass *) klass;
115 GstElementClass *element_class = (GstElementClass *) klass;
117 gobject_class->dispose = gst_gme_dec_dispose;
119 gst_element_class_set_details_simple (element_class,
120 "Gaming console music file decoder", "Codec/Audio/Decoder",
121 "Uses libgme to emulate a gaming console sound processors",
122 "Chris Lee <clee@kde.org>, Brian Koropoff <bkoropoff@gmail.com>, "
123 "Michael Pyne <mpyne@kde.org>, Sebastian Dröge <sebastian.droege@collabora.co.uk>");
125 gst_element_class_add_pad_template (element_class,
126 gst_static_pad_template_get (&sink_factory));
127 gst_element_class_add_pad_template (element_class,
128 gst_static_pad_template_get (&src_factory));
130 element_class->change_state = GST_DEBUG_FUNCPTR (gst_gme_dec_change_state);
135 gst_gme_dec_init (GstGmeDec * gme)
137 gme->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
138 /* gst_pad_set_query_function (gme->sinkpad, NULL); */
139 gst_pad_set_event_function (gme->sinkpad, gst_gme_dec_sink_event);
140 gst_pad_set_chain_function (gme->sinkpad, gst_gme_dec_chain);
141 gst_element_add_pad (GST_ELEMENT (gme), gme->sinkpad);
143 gme->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
144 gst_pad_set_event_function (gme->srcpad, gst_gme_dec_src_event);
145 gst_pad_set_query_function (gme->srcpad, gst_gme_dec_src_query);
146 gst_pad_use_fixed_caps (gme->srcpad);
147 gst_element_add_pad (GST_ELEMENT (gme), gme->srcpad);
149 gme->adapter = gst_adapter_new ();
151 gme->total_duration = GST_CLOCK_TIME_NONE;
152 gme->initialized = FALSE;
156 gst_gme_dec_dispose (GObject * object)
158 GstGmeDec *gme = GST_GME_DEC (object);
161 g_object_unref (gme->adapter);
165 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
169 gst_gme_dec_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
171 GstGmeDec *gme = GST_GME_DEC (parent);
173 /* Accumulate GME data until end-of-stream, then commence playback. */
174 gst_adapter_push (gme->adapter, buffer);
180 gst_gme_dec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
182 GstGmeDec *gme = GST_GME_DEC (parent);
183 gboolean result = TRUE;
184 gboolean forward = FALSE;
186 switch (GST_EVENT_TYPE (event)) {
188 /* we get EOS when we loaded the complete file, now try to initialize the
190 if (!(result = gme_setup (gme))) {
191 /* can't start, post an ERROR and push EOS downstream */
192 GST_ELEMENT_ERROR (gme, STREAM, DEMUX, (NULL),
193 ("can't start playback"));
201 result = gst_pad_push_event (gme->srcpad, event);
203 gst_event_unref (event);
209 gst_gme_dec_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
211 GstGmeDec *gme = GST_GME_DEC (parent);
212 gboolean result = FALSE;
214 switch (GST_EVENT_TYPE (event)) {
220 GstSeekType start_type, stop_type;
224 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
227 gst_event_unref (event);
229 if (format != GST_FORMAT_TIME) {
230 GST_DEBUG_OBJECT (gme, "seeking is only supported in TIME format");
234 if (start_type != GST_SEEK_TYPE_SET || stop_type != GST_SEEK_TYPE_NONE) {
235 GST_DEBUG_OBJECT (gme, "unsupported seek type");
239 if (stop_type == GST_SEEK_TYPE_NONE)
240 stop = GST_CLOCK_TIME_NONE;
242 if (start_type == GST_SEEK_TYPE_SET) {
244 guint64 cur = gme_tell (gme->player) * GST_MSECOND;
245 guint64 dest = (guint64) start;
247 if (gme->total_duration != GST_CLOCK_TIME_NONE)
248 dest = CLAMP (dest, 0, gme->total_duration);
250 dest = MAX (0, dest);
255 flush = (flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH;
258 gst_pad_push_event (gme->srcpad, gst_event_new_flush_start ());
260 gst_pad_stop_task (gme->srcpad);
263 GST_PAD_STREAM_LOCK (gme->srcpad);
265 if (flags & GST_SEEK_FLAG_SEGMENT) {
266 gst_element_post_message (GST_ELEMENT (gme),
267 gst_message_new_segment_start (GST_OBJECT (gme), format, cur));
271 gst_pad_push_event (gme->srcpad, gst_event_new_flush_stop (TRUE));
274 if (stop == GST_CLOCK_TIME_NONE
275 && gme->total_duration != GST_CLOCK_TIME_NONE)
276 stop = gme->total_duration;
278 gst_segment_init (&seg, GST_FORMAT_TIME);
283 gst_pad_push_event (gme->srcpad, gst_event_new_segment (&seg));
285 gme->seekpoint = dest / GST_MSECOND; /* nsecs to msecs */
288 gst_pad_start_task (gme->srcpad, (GstTaskFunction) gst_gme_play,
291 GST_PAD_STREAM_UNLOCK (gme->srcpad);
297 result = gst_pad_push_event (gme->sinkpad, event);
305 gst_gme_dec_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
307 GstGmeDec *gme = GST_GME_DEC (parent);
308 gboolean result = TRUE;
310 switch (GST_QUERY_TYPE (query)) {
311 case GST_QUERY_DURATION:
315 gst_query_parse_duration (query, &format, NULL);
316 if (!gme->initialized || format != GST_FORMAT_TIME
317 || gme->total_duration == GST_CLOCK_TIME_NONE) {
321 gst_query_set_duration (query, GST_FORMAT_TIME, gme->total_duration);
324 case GST_QUERY_POSITION:
328 gst_query_parse_position (query, &format, NULL);
329 if (!gme->initialized || format != GST_FORMAT_TIME) {
333 gst_query_set_position (query, GST_FORMAT_TIME,
334 (gint64) gme_tell (gme->player) * GST_MSECOND);
338 result = gst_pad_query_default (pad, parent, query);
346 gst_gme_play (GstPad * pad)
348 GstGmeDec *gme = GST_GME_DEC (gst_pad_get_parent (pad));
349 GstFlowReturn flow_return;
351 gboolean seeking = gme->seeking;
352 gme_err_t gme_err = NULL;
353 const int NUM_SAMPLES = 1600; /* 4 bytes (stereo 16-bit) per sample */
358 out = gst_buffer_new_and_alloc (NUM_SAMPLES * 4);
359 GST_BUFFER_TIMESTAMP (out) = gme_tell (gme->player) * GST_MSECOND;
361 gst_buffer_map (out, &map, GST_MAP_WRITE);
362 gme_err = gme_play (gme->player, NUM_SAMPLES * 2, (short *) map.data);
363 gst_buffer_unmap (out, &map);
366 GST_ELEMENT_ERROR (gme, STREAM, DEMUX, (NULL), ("%s", gme_err));
367 gst_pad_pause_task (pad);
368 gst_pad_push_event (pad, gst_event_new_eos ());
369 gst_object_unref (gme);
373 gme_seek (gme->player, gme->seekpoint);
374 gme->seeking = FALSE;
376 out = gst_buffer_new ();
379 if ((flow_return = gst_pad_push (gme->srcpad, out)) != GST_FLOW_OK) {
380 GST_DEBUG_OBJECT (gme, "pausing task, reason %s",
381 gst_flow_get_name (flow_return));
383 gst_pad_pause_task (pad);
385 if (flow_return == GST_FLOW_EOS) {
386 gst_pad_push_event (pad, gst_event_new_eos ());
387 } else if (flow_return < GST_FLOW_EOS || flow_return == GST_FLOW_NOT_LINKED) {
388 GST_ELEMENT_ERROR (gme, STREAM, FAILED, ("Internal data stream error."),
389 ("stream stopped, reason %s", gst_flow_get_name (flow_return)));
391 gst_pad_push_event (pad, gst_event_new_eos ());
395 if (gme_tell (gme->player) * GST_MSECOND > gme->total_duration) {
396 gst_pad_pause_task (pad);
397 gst_pad_push_event (pad, gst_event_new_eos ());
400 gst_object_unref (gme);
406 gme_setup (GstGmeDec * gme)
409 gme_err_t gme_err = NULL;
411 guint64 total_duration;
417 if (!gst_adapter_available (gme->adapter) || !gme_negotiate (gme)) {
422 gst_adapter_take_buffer (gme->adapter,
423 gst_adapter_available (gme->adapter));
425 gst_buffer_map (buffer, &map, GST_MAP_READ);
426 gme_err = gme_open_data (map.data, map.size, &gme->player, 32000);
427 gst_buffer_unmap (buffer, &map);
428 gst_buffer_unref (buffer);
430 if (gme_err || !gme->player) {
432 gme_delete (gme->player);
436 GST_ELEMENT_ERROR (gme, STREAM, DEMUX, (NULL), ("%s", gme_err));
441 gme_err = gme_track_info (gme->player, &info, 0);
443 taglist = gst_tag_list_new_empty ();
445 if (info->song && *info->song)
446 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_TITLE,
448 if (info->author && *info->author)
449 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_ARTIST,
451 /* Prefer the name of the official soundtrack over the name of the game (since this is
452 * how track numbers are derived)
454 if (info->game && *info->game)
455 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_ALBUM, info->game,
458 if (info->comment && *info->comment)
459 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_COMMENT,
460 info->comment, NULL);
461 if (info->dumper && *info->dumper)
462 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_CONTACT,
464 if (info->copyright && *info->copyright)
465 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_COPYRIGHT,
466 info->copyright, NULL);
467 if (info->system && *info->system)
468 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_ENCODER,
471 gme->total_duration = total_duration =
472 gst_util_uint64_scale_int (info->play_length + (info->loop_length >
473 0 ? 8000 : 0), GST_MSECOND, 1);
474 fade_time = info->loop_length > 0 ? info->play_length : 0;
476 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
477 GST_TAG_DURATION, total_duration, NULL);
479 gst_pad_push_event (gme->srcpad, gst_event_new_tag (taglist));
483 #ifdef HAVE_LIBGME_ACCURACY
484 /* TODO: Is it worth it to make this optional? */
485 gme_enable_accuracy (gme->player, 1);
487 gme_start_track (gme->player, 0);
489 gme_set_fade (gme->player, fade_time);
491 gst_segment_init (&seg, GST_FORMAT_TIME);
492 gst_pad_push_event (gme->srcpad, gst_event_new_segment (&seg));
494 gst_pad_start_task (gme->srcpad, (GstTaskFunction) gst_gme_play, gme->srcpad);
496 gme->initialized = TRUE;
497 gme->seeking = FALSE;
499 return gme->initialized;
502 static GstStateChangeReturn
503 gst_gme_dec_change_state (GstElement * element, GstStateChange transition)
505 GstStateChangeReturn result;
508 dec = GST_GME_DEC (element);
510 switch (transition) {
511 case GST_STATE_CHANGE_READY_TO_PAUSED:
512 dec->total_duration = GST_CLOCK_TIME_NONE;
518 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
519 if (result == GST_STATE_CHANGE_FAILURE)
522 switch (transition) {
523 case GST_STATE_CHANGE_PAUSED_TO_READY:
524 gst_adapter_clear (dec->adapter);
534 plugin_init (GstPlugin * plugin)
536 return gst_element_register (plugin, "gmedec", GST_RANK_PRIMARY,
540 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
544 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);