Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / ext / gme / gstgme.c
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>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gstgme.h"
26
27 #include <string.h>
28 #include <glib/gprintf.h>
29 #include <glib.h>
30
31 static GstStaticPadTemplate sink_factory =
32     GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
33     GST_STATIC_CAPS ("audio/x-ay; "
34         "audio/x-gbs; "
35         "audio/x-gym; "
36         "audio/x-hes; "
37         "audio/x-kss; "
38         "audio/x-nsf; " "audio/x-sap; " "audio/x-spc; " "audio/x-vgm"));
39
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, "
45         "width = (int) 16, "
46         "depth = (int) 16, " "rate = (int) 32000, " "channels = (int) 2"));
47
48 #define gst_gme_dec_parent_class parent_class
49 G_DEFINE_TYPE (GstGmeDec, gst_gme_dec, GST_TYPE_ELEMENT);
50
51 static GstFlowReturn gst_gme_dec_chain (GstPad * pad, GstObject * parent,
52     GstBuffer * buffer);
53 static gboolean gst_gme_dec_sink_event (GstPad * pad, GstObject * parent,
54     GstEvent * event);
55 static gboolean gst_gme_dec_src_event (GstPad * pad, GstObject * parent,
56     GstEvent * event);
57 static gboolean gst_gme_dec_src_query (GstPad * pad, GstObject * parent,
58     GstQuery * query);
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);
64
65 static gboolean
66 gme_negotiate (GstGmeDec * gme)
67 {
68   GstCaps *allowed, *caps;
69   GstStructure *structure;
70   gint width = 16, depth = 16;
71   gboolean sign;
72   int rate = 32000;
73   int channels = 2;
74
75   allowed = gst_pad_get_allowed_caps (gme->srcpad);
76   if (!allowed) {
77     GST_DEBUG_OBJECT (gme, "couldn't get allowed caps");
78     return FALSE;
79   }
80
81   GST_DEBUG_OBJECT (gme, "allowed caps: %" GST_PTR_FORMAT, allowed);
82
83   structure = gst_caps_get_structure (allowed, 0);
84   gst_structure_get_int (structure, "width", &width);
85   gst_structure_get_int (structure, "depth", &depth);
86
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);
90     return FALSE;
91   }
92
93   gst_structure_get_boolean (structure, "signed", &sign);
94   gst_structure_get_int (structure, "rate", &rate);
95   gst_structure_get_int (structure, "channels", &channels);
96
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);
104
105   gst_caps_unref (caps);
106   gst_caps_unref (allowed);
107
108   return TRUE;
109 }
110
111 static void
112 gst_gme_dec_class_init (GstGmeDecClass * klass)
113 {
114   GObjectClass *gobject_class = (GObjectClass *) klass;
115   GstElementClass *element_class = (GstElementClass *) klass;
116
117   gobject_class->dispose = gst_gme_dec_dispose;
118
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>");
124
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));
129
130   element_class->change_state = GST_DEBUG_FUNCPTR (gst_gme_dec_change_state);
131 }
132
133
134 static void
135 gst_gme_dec_init (GstGmeDec * gme)
136 {
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);
142
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);
148
149   gme->adapter = gst_adapter_new ();
150   gme->player = NULL;
151   gme->total_duration = GST_CLOCK_TIME_NONE;
152   gme->initialized = FALSE;
153 }
154
155 static void
156 gst_gme_dec_dispose (GObject * object)
157 {
158   GstGmeDec *gme = GST_GME_DEC (object);
159
160   if (gme->adapter) {
161     g_object_unref (gme->adapter);
162     gme->adapter = NULL;
163   }
164
165   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
166 }
167
168 static GstFlowReturn
169 gst_gme_dec_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
170 {
171   GstGmeDec *gme = GST_GME_DEC (parent);
172
173   /* Accumulate GME data until end-of-stream, then commence playback. */
174   gst_adapter_push (gme->adapter, buffer);
175
176   return GST_FLOW_OK;
177 }
178
179 static gboolean
180 gst_gme_dec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
181 {
182   GstGmeDec *gme = GST_GME_DEC (parent);
183   gboolean result = TRUE;
184   gboolean forward = FALSE;
185
186   switch (GST_EVENT_TYPE (event)) {
187     case GST_EVENT_EOS:
188       /* we get EOS when we loaded the complete file, now try to initialize the
189        * decoding */
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"));
194         forward = TRUE;
195       }
196       break;
197     default:
198       break;
199   }
200   if (forward)
201     result = gst_pad_push_event (gme->srcpad, event);
202   else
203     gst_event_unref (event);
204
205   return result;
206 }
207
208 static gboolean
209 gst_gme_dec_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
210 {
211   GstGmeDec *gme = GST_GME_DEC (parent);
212   gboolean result = FALSE;
213
214   switch (GST_EVENT_TYPE (event)) {
215     case GST_EVENT_SEEK:
216     {
217       gdouble rate;
218       GstFormat format;
219       GstSeekFlags flags;
220       GstSeekType start_type, stop_type;
221       gint64 start, stop;
222       gboolean flush;
223
224       gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
225           &stop_type, &stop);
226
227       gst_event_unref (event);
228
229       if (format != GST_FORMAT_TIME) {
230         GST_DEBUG_OBJECT (gme, "seeking is only supported in TIME format");
231         break;
232       }
233
234       if (start_type != GST_SEEK_TYPE_SET || stop_type != GST_SEEK_TYPE_NONE) {
235         GST_DEBUG_OBJECT (gme, "unsupported seek type");
236         break;
237       }
238
239       if (stop_type == GST_SEEK_TYPE_NONE)
240         stop = GST_CLOCK_TIME_NONE;
241
242       if (start_type == GST_SEEK_TYPE_SET) {
243         GstSegment seg;
244         guint64 cur = gme_tell (gme->player) * GST_MSECOND;
245         guint64 dest = (guint64) start;
246
247         if (gme->total_duration != GST_CLOCK_TIME_NONE)
248           dest = CLAMP (dest, 0, gme->total_duration);
249         else
250           dest = MAX (0, dest);
251
252         if (dest == cur)
253           break;
254
255         flush = (flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH;
256
257         if (flush) {
258           gst_pad_push_event (gme->srcpad, gst_event_new_flush_start ());
259         } else {
260           gst_pad_stop_task (gme->srcpad);
261         }
262
263         GST_PAD_STREAM_LOCK (gme->srcpad);
264
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));
268         }
269
270         if (flush) {
271           gst_pad_push_event (gme->srcpad, gst_event_new_flush_stop (TRUE));
272         }
273
274         if (stop == GST_CLOCK_TIME_NONE
275             && gme->total_duration != GST_CLOCK_TIME_NONE)
276           stop = gme->total_duration;
277
278         gst_segment_init (&seg, GST_FORMAT_TIME);
279         seg.rate = rate;
280         seg.start = dest;
281         seg.stop = stop;
282         seg.time = dest;
283         gst_pad_push_event (gme->srcpad, gst_event_new_segment (&seg));
284
285         gme->seekpoint = dest / GST_MSECOND;    /* nsecs to msecs */
286         gme->seeking = TRUE;
287
288         gst_pad_start_task (gme->srcpad, (GstTaskFunction) gst_gme_play,
289             gme->srcpad);
290
291         GST_PAD_STREAM_UNLOCK (gme->srcpad);
292         result = TRUE;
293       }
294       break;
295     }
296     default:
297       result = gst_pad_push_event (gme->sinkpad, event);
298       break;
299   }
300
301   return result;
302 }
303
304 static gboolean
305 gst_gme_dec_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
306 {
307   GstGmeDec *gme = GST_GME_DEC (parent);
308   gboolean result = TRUE;
309
310   switch (GST_QUERY_TYPE (query)) {
311     case GST_QUERY_DURATION:
312     {
313       GstFormat format;
314
315       gst_query_parse_duration (query, &format, NULL);
316       if (!gme->initialized || format != GST_FORMAT_TIME
317           || gme->total_duration == GST_CLOCK_TIME_NONE) {
318         result = FALSE;
319         break;
320       }
321       gst_query_set_duration (query, GST_FORMAT_TIME, gme->total_duration);
322       break;
323     }
324     case GST_QUERY_POSITION:
325     {
326       GstFormat format;
327
328       gst_query_parse_position (query, &format, NULL);
329       if (!gme->initialized || format != GST_FORMAT_TIME) {
330         result = FALSE;
331         break;
332       }
333       gst_query_set_position (query, GST_FORMAT_TIME,
334           (gint64) gme_tell (gme->player) * GST_MSECOND);
335       break;
336     }
337     default:
338       result = gst_pad_query_default (pad, parent, query);
339       break;
340   }
341
342   return result;
343 }
344
345 static void
346 gst_gme_play (GstPad * pad)
347 {
348   GstGmeDec *gme = GST_GME_DEC (gst_pad_get_parent (pad));
349   GstFlowReturn flow_return;
350   GstBuffer *out;
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 */
354
355   if (!seeking) {
356     GstMapInfo map;
357
358     out = gst_buffer_new_and_alloc (NUM_SAMPLES * 4);
359     GST_BUFFER_TIMESTAMP (out) = gme_tell (gme->player) * GST_MSECOND;
360
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);
364
365     if (gme_err) {
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);
370       return;
371     }
372   } else {
373     gme_seek (gme->player, gme->seekpoint);
374     gme->seeking = FALSE;
375
376     out = gst_buffer_new ();
377   }
378
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));
382
383     gst_pad_pause_task (pad);
384
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)));
390
391       gst_pad_push_event (pad, gst_event_new_eos ());
392     }
393   }
394
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 ());
398   }
399
400   gst_object_unref (gme);
401
402   return;
403 }
404
405 static gboolean
406 gme_setup (GstGmeDec * gme)
407 {
408   gme_info_t *info;
409   gme_err_t gme_err = NULL;
410   GstTagList *taglist;
411   guint64 total_duration;
412   guint64 fade_time;
413   GstBuffer *buffer;
414   GstSegment seg;
415   GstMapInfo map;
416
417   if (!gst_adapter_available (gme->adapter) || !gme_negotiate (gme)) {
418     return FALSE;
419   }
420
421   buffer =
422       gst_adapter_take_buffer (gme->adapter,
423       gst_adapter_available (gme->adapter));
424
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);
429
430   if (gme_err || !gme->player) {
431     if (gme->player) {
432       gme_delete (gme->player);
433       gme->player = NULL;
434     }
435
436     GST_ELEMENT_ERROR (gme, STREAM, DEMUX, (NULL), ("%s", gme_err));
437
438     return FALSE;
439   }
440
441   gme_err = gme_track_info (gme->player, &info, 0);
442
443   taglist = gst_tag_list_new_empty ();
444
445   if (info->song && *info->song)
446     gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_TITLE,
447         info->song, NULL);
448   if (info->author && *info->author)
449     gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_ARTIST,
450         info->author, NULL);
451   /* Prefer the name of the official soundtrack over the name of the game (since this is
452    * how track numbers are derived)
453    */
454   if (info->game && *info->game)
455     gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_ALBUM, info->game,
456         NULL);
457
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,
463         info->dumper, NULL);
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,
469         info->system, NULL);
470
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;
475
476   gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
477       GST_TAG_DURATION, total_duration, NULL);
478
479   gst_pad_push_event (gme->srcpad, gst_event_new_tag (taglist));
480
481   g_free (info);
482
483 #ifdef HAVE_LIBGME_ACCURACY
484   /* TODO: Is it worth it to make this optional? */
485   gme_enable_accuracy (gme->player, 1);
486 #endif
487   gme_start_track (gme->player, 0);
488   if (fade_time)
489     gme_set_fade (gme->player, fade_time);
490
491   gst_segment_init (&seg, GST_FORMAT_TIME);
492   gst_pad_push_event (gme->srcpad, gst_event_new_segment (&seg));
493
494   gst_pad_start_task (gme->srcpad, (GstTaskFunction) gst_gme_play, gme->srcpad);
495
496   gme->initialized = TRUE;
497   gme->seeking = FALSE;
498   gme->seekpoint = 0;
499   return gme->initialized;
500 }
501
502 static GstStateChangeReturn
503 gst_gme_dec_change_state (GstElement * element, GstStateChange transition)
504 {
505   GstStateChangeReturn result;
506   GstGmeDec *dec;
507
508   dec = GST_GME_DEC (element);
509
510   switch (transition) {
511     case GST_STATE_CHANGE_READY_TO_PAUSED:
512       dec->total_duration = GST_CLOCK_TIME_NONE;
513       break;
514     default:
515       break;
516   }
517
518   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
519   if (result == GST_STATE_CHANGE_FAILURE)
520     return result;
521
522   switch (transition) {
523     case GST_STATE_CHANGE_PAUSED_TO_READY:
524       gst_adapter_clear (dec->adapter);
525       break;
526     default:
527       break;
528   }
529
530   return result;
531 }
532
533 static gboolean
534 plugin_init (GstPlugin * plugin)
535 {
536   return gst_element_register (plugin, "gmedec", GST_RANK_PRIMARY,
537       GST_TYPE_GME_DEC);
538 }
539
540 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
541     GST_VERSION_MINOR,
542     "gmedec",
543     "GME Audio Decoder",
544     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);