1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /*
21 Code based on modplugxmms
22 XMMS plugin:
23 Kenton Varda <temporal@gauge3d.org>
24 Sound Engine:
25 Olivier Lapicque <olivierl@jps.net>
26 */
27
28 /**
29 * SECTION:element-modplug
30 *
31 * Modplug uses the <ulink url="http://modplug-xmms.sourceforge.net/">modplug</ulink>
32 * library to decode tracked music in the MOD/S3M/XM/IT and related formats.
33 *
34 * <refsect2>
35 * <title>Example pipeline</title>
36 * |[
37 * gst-launch-1.0 -v filesrc location=1990s-nostalgia.xm ! modplug ! audioconvert ! alsasink
38 * ]| Play a FastTracker xm file.
39 * </refsect2>
40 */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 /* Required to not get an undefined warning
47 * https://bugzilla.gnome.org/show_bug.cgi?id=613795
48 */
49 #ifndef WORDS_BIGENDIAN
50 #define WORDS_BIGENDIAN 0
51 #endif
52
53 #include <libmodplug/stdafx.h>
54 #include <libmodplug/sndfile.h>
55
56 #include "gstmodplug.h"
57
58 #include <gst/gst.h>
59 #include <stdlib.h>
60 #include <gst/audio/audio.h>
61
62 GST_DEBUG_CATEGORY_STATIC (modplug_debug);
63 #define GST_CAT_DEFAULT modplug_debug
64
65 enum
66 {
67 ARG_0,
68 ARG_SONGNAME,
69 ARG_REVERB,
70 ARG_REVERB_DEPTH,
71 ARG_REVERB_DELAY,
72 ARG_MEGABASS,
73 ARG_MEGABASS_AMOUNT,
74 ARG_MEGABASS_RANGE,
75 ARG_NOISE_REDUCTION,
76 ARG_SURROUND,
77 ARG_SURROUND_DEPTH,
78 ARG_SURROUND_DELAY,
79 ARG_OVERSAMP
80 };
81
82 #define DEFAULT_REVERB FALSE
83 #define DEFAULT_REVERB_DEPTH 30
84 #define DEFAULT_REVERB_DELAY 100
85 #define DEFAULT_MEGABASS FALSE
86 #define DEFAULT_MEGABASS_AMOUNT 40
87 #define DEFAULT_MEGABASS_RANGE 30
88 #define DEFAULT_SURROUND TRUE
89 #define DEFAULT_SURROUND_DEPTH 20
90 #define DEFAULT_SURROUND_DELAY 20
91 #define DEFAULT_OVERSAMP TRUE
92 #define DEFAULT_NOISE_REDUCTION TRUE
93
94 #define FORMATS "{ " GST_AUDIO_NE (S32) ", " GST_AUDIO_NE (S16) ", U8 }"
95
96 static GstStaticPadTemplate modplug_src_template_factory =
97 GST_STATIC_PAD_TEMPLATE ("src",
98 GST_PAD_SRC,
99 GST_PAD_ALWAYS,
100 GST_STATIC_CAPS ("audio/x-raw,"
101 " format = (string) " FORMATS ", "
102 " layout = (string) interleaved, "
103 " rate = (int) { 8000, 11025, 22050, 44100 },"
104 " channels = (int) [ 1, 2 ]"));
105
106 static GstStaticPadTemplate modplug_sink_template_factory =
107 GST_STATIC_PAD_TEMPLATE ("sink",
108 GST_PAD_SINK,
109 GST_PAD_ALWAYS,
110 GST_STATIC_CAPS ("audio/x-mod; audio/x-xm; audio/x-it; audio/x-s3m; "
111 "audio/x-stm"));
112
113 static void gst_modplug_dispose (GObject * object);
114 static void gst_modplug_set_property (GObject * object,
115 guint id, const GValue * value, GParamSpec * pspec);
116 static void gst_modplug_get_property (GObject * object,
117 guint id, GValue * value, GParamSpec * pspec);
118
119 static gboolean gst_modplug_src_event (GstPad * pad, GstObject * parent,
120 GstEvent * event);
121 static gboolean gst_modplug_src_query (GstPad * pad, GstObject * parent,
122 GstQuery * query);
123 static GstStateChangeReturn gst_modplug_change_state (GstElement * element,
124 GstStateChange transition);
125
126 static gboolean gst_modplug_sinkpad_activate (GstPad * pad, GstObject * parent);
127 static gboolean gst_modplug_sinkpad_activate_mode (GstPad * pad,
128 GstObject * parent, GstPadMode mode, gboolean active);
129 static void gst_modplug_loop (GstModPlug * element);
130
131 #define parent_class gst_modplug_parent_class
132 G_DEFINE_TYPE (GstModPlug, gst_modplug, GST_TYPE_ELEMENT);
133
134 static void
gst_modplug_class_init(GstModPlugClass * klass)135 gst_modplug_class_init (GstModPlugClass * klass)
136 {
137 GObjectClass *gobject_class;
138 GstElementClass *gstelement_class;
139
140 gobject_class = (GObjectClass *) klass;
141 gstelement_class = (GstElementClass *) klass;
142
143 gobject_class->set_property = gst_modplug_set_property;
144 gobject_class->get_property = gst_modplug_get_property;
145 gobject_class->dispose = gst_modplug_dispose;
146
147 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SONGNAME,
148 g_param_spec_string ("songname", "Songname", "The song name",
149 NULL, (GParamFlags) (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
150
151 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_REVERB,
152 g_param_spec_boolean ("reverb", "reverb", "Reverb",
153 DEFAULT_REVERB,
154 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
155
156 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_REVERB_DEPTH,
157 g_param_spec_int ("reverb-depth", "reverb depth", "Reverb depth",
158 0, 100, DEFAULT_REVERB_DEPTH,
159 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
160
161 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_REVERB_DELAY,
162 g_param_spec_int ("reverb-delay", "reverb delay", "Reverb delay",
163 0, 200, DEFAULT_REVERB_DELAY,
164 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
165
166 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MEGABASS,
167 g_param_spec_boolean ("megabass", "megabass", "Megabass",
168 DEFAULT_MEGABASS,
169 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
170
171 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MEGABASS_AMOUNT,
172 g_param_spec_int ("megabass-amount", "megabass amount", "Megabass amount",
173 0, 100, DEFAULT_MEGABASS_AMOUNT,
174 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
175
176 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MEGABASS_RANGE,
177 g_param_spec_int ("megabass-range", "megabass range", "Megabass range",
178 0, 100, DEFAULT_MEGABASS_RANGE,
179 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
180
181 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SURROUND,
182 g_param_spec_boolean ("surround", "surround", "Surround",
183 DEFAULT_SURROUND,
184 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
185
186 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SURROUND_DEPTH,
187 g_param_spec_int ("surround-depth", "surround depth", "Surround depth",
188 0, 100, DEFAULT_SURROUND_DEPTH,
189 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
190
191 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SURROUND_DELAY,
192 g_param_spec_int ("surround-delay", "surround delay", "Surround delay",
193 0, 40, DEFAULT_SURROUND_DELAY,
194 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
195
196 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_OVERSAMP,
197 g_param_spec_boolean ("oversamp", "oversamp", "oversamp",
198 DEFAULT_OVERSAMP,
199 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
200
201 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NOISE_REDUCTION,
202 g_param_spec_boolean ("noise-reduction", "noise reduction",
203 "noise reduction", DEFAULT_NOISE_REDUCTION,
204 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
205
206 gstelement_class->change_state = gst_modplug_change_state;
207
208 gst_element_class_add_static_pad_template (gstelement_class, &modplug_sink_template_factory);
209 gst_element_class_add_static_pad_template (gstelement_class, &modplug_src_template_factory);
210
211 gst_element_class_set_static_metadata (gstelement_class, "ModPlug",
212 "Codec/Decoder/Audio", "Module decoder based on modplug engine",
213 "Jeremy SIMON <jsimon13@yahoo.fr>");
214
215 GST_DEBUG_CATEGORY_INIT (modplug_debug, "modplug", 0, "ModPlug element");
216 }
217
218 static void
gst_modplug_init(GstModPlug * modplug)219 gst_modplug_init (GstModPlug * modplug)
220 {
221 /* create the sink and src pads */
222 modplug->sinkpad =
223 gst_pad_new_from_static_template (&modplug_sink_template_factory, "sink");
224 gst_pad_set_activate_function (modplug->sinkpad,
225 GST_DEBUG_FUNCPTR (gst_modplug_sinkpad_activate));
226 gst_pad_set_activatemode_function (modplug->sinkpad,
227 GST_DEBUG_FUNCPTR (gst_modplug_sinkpad_activate_mode));
228 gst_element_add_pad (GST_ELEMENT (modplug), modplug->sinkpad);
229
230 modplug->srcpad =
231 gst_pad_new_from_static_template (&modplug_src_template_factory, "src");
232 gst_pad_set_event_function (modplug->srcpad,
233 GST_DEBUG_FUNCPTR (gst_modplug_src_event));
234 gst_pad_set_query_function (modplug->srcpad,
235 GST_DEBUG_FUNCPTR (gst_modplug_src_query));
236 gst_element_add_pad (GST_ELEMENT (modplug), modplug->srcpad);
237
238 modplug->reverb = DEFAULT_REVERB;
239 modplug->reverb_depth = DEFAULT_REVERB_DEPTH;
240 modplug->reverb_delay = DEFAULT_REVERB_DELAY;
241 modplug->megabass = DEFAULT_MEGABASS;
242 modplug->megabass_amount = DEFAULT_MEGABASS_AMOUNT;
243 modplug->megabass_range = DEFAULT_MEGABASS_RANGE;
244 modplug->surround = DEFAULT_SURROUND;
245 modplug->surround_depth = DEFAULT_SURROUND_DEPTH;
246 modplug->surround_delay = DEFAULT_SURROUND_DELAY;
247 modplug->oversamp = DEFAULT_OVERSAMP;
248 modplug->noise_reduction = DEFAULT_NOISE_REDUCTION;
249
250 modplug->bits = 16;
251 modplug->channel = 2;
252 modplug->frequency = 44100;
253 }
254
255
256 static void
gst_modplug_dispose(GObject * object)257 gst_modplug_dispose (GObject * object)
258 {
259 GstModPlug *modplug = GST_MODPLUG (object);
260
261 G_OBJECT_CLASS (parent_class)->dispose (object);
262
263 if (modplug->buffer) {
264 gst_buffer_unref (modplug->buffer);
265 modplug->buffer = NULL;
266 }
267 }
268
269 static gboolean
gst_modplug_src_query(GstPad * pad,GstObject * parent,GstQuery * query)270 gst_modplug_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
271 {
272 GstModPlug *modplug;
273 gboolean res = FALSE;
274
275 modplug = GST_MODPLUG (parent);
276
277 switch (GST_QUERY_TYPE (query)) {
278 case GST_QUERY_DURATION:
279 {
280 GstFormat format;
281
282 if (!modplug->mSoundFile)
283 goto done;
284
285 gst_query_parse_duration (query, &format, NULL);
286 if (format == GST_FORMAT_TIME) {
287 gst_query_set_duration (query, format, modplug->song_length);
288 res = TRUE;
289 }
290 }
291 break;
292 case GST_QUERY_POSITION:
293 {
294 GstFormat format;
295
296 if (!modplug->mSoundFile)
297 goto done;
298
299 gst_query_parse_position (query, &format, NULL);
300 if (format == GST_FORMAT_TIME) {
301 gint64 pos;
302
303 pos = (modplug->song_length * modplug->mSoundFile->GetCurrentPos ());
304 pos /= modplug->mSoundFile->GetMaxPosition ();
305 gst_query_set_position (query, format, pos);
306 res = TRUE;
307 }
308 }
309 break;
310 default:
311 res = gst_pad_query_default (pad, parent, query);
312 break;
313 }
314
315 done:
316 return res;
317 }
318
319 static gboolean
gst_modplug_do_seek(GstModPlug * modplug,GstEvent * event)320 gst_modplug_do_seek (GstModPlug * modplug, GstEvent * event)
321 {
322 gdouble rate;
323 GstFormat format;
324 GstSeekFlags flags;
325 GstSeekType cur_type, stop_type;
326 gboolean flush;
327 gint64 cur, stop;
328 GstSegment seg;
329
330 if (modplug->frequency == 0)
331 goto no_song;
332
333 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
334 &stop_type, &stop);
335
336 if (format != GST_FORMAT_TIME)
337 goto no_time;
338
339 /* FIXME: we should be using GstSegment for all this */
340 if (cur_type != GST_SEEK_TYPE_SET || stop_type != GST_SEEK_TYPE_NONE)
341 goto not_supported;
342
343 if (stop_type == GST_SEEK_TYPE_NONE)
344 stop = GST_CLOCK_TIME_NONE;
345 if (!GST_CLOCK_TIME_IS_VALID (stop) && modplug->song_length > 0)
346 stop = modplug->song_length;
347
348 cur = CLAMP (cur, -1, modplug->song_length);
349
350 GST_DEBUG_OBJECT (modplug, "seek to %" GST_TIME_FORMAT,
351 GST_TIME_ARGS ((guint64) cur));
352
353 modplug->seek_at = cur;
354
355 flush = ((flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH);
356
357 if (flush) {
358 gst_pad_push_event (modplug->srcpad, gst_event_new_flush_start ());
359 } else {
360 gst_pad_stop_task (modplug->sinkpad);
361 }
362
363 GST_PAD_STREAM_LOCK (modplug->sinkpad);
364
365 if (flags & GST_SEEK_FLAG_SEGMENT) {
366 gst_element_post_message (GST_ELEMENT (modplug),
367 gst_message_new_segment_start (GST_OBJECT (modplug), format, cur));
368 }
369
370 if (flush) {
371 gst_pad_push_event (modplug->srcpad, gst_event_new_flush_stop (TRUE));
372 }
373
374 GST_LOG_OBJECT (modplug, "sending newsegment from %" GST_TIME_FORMAT "-%"
375 GST_TIME_FORMAT ", pos=%" GST_TIME_FORMAT,
376 GST_TIME_ARGS ((guint64) cur), GST_TIME_ARGS ((guint64) stop),
377 GST_TIME_ARGS ((guint64) cur));
378
379 gst_segment_init (&seg, GST_FORMAT_TIME);
380 seg.rate = rate;
381 seg.start = cur;
382 seg.stop = stop;
383 seg.time = cur;
384 gst_pad_push_event (modplug->srcpad, gst_event_new_segment (&seg));
385
386 modplug->offset =
387 gst_util_uint64_scale_int (cur, modplug->frequency, GST_SECOND);
388
389 gst_pad_start_task (modplug->sinkpad,
390 (GstTaskFunction) gst_modplug_loop, modplug, NULL);
391
392 GST_PAD_STREAM_UNLOCK (modplug->sinkpad);
393
394 return TRUE;
395
396 /* ERROR */
397 no_song:
398 {
399 GST_DEBUG_OBJECT (modplug, "no song loaded yet");
400 return FALSE;
401 }
402 no_time:
403 {
404 GST_DEBUG_OBJECT (modplug, "seeking is only supported in TIME format");
405 return FALSE;
406 }
407 not_supported:
408 {
409 GST_DEBUG_OBJECT (modplug, "unsupported seek type");
410 return FALSE;
411 }
412 }
413
414 static gboolean
gst_modplug_src_event(GstPad * pad,GstObject * parent,GstEvent * event)415 gst_modplug_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
416 {
417 GstModPlug *modplug;
418 gboolean res = FALSE;
419
420 modplug = GST_MODPLUG (parent);
421
422 switch (GST_EVENT_TYPE (event)) {
423 case GST_EVENT_SEEK:
424 res = gst_modplug_do_seek (modplug, event);
425 break;
426 default:
427 res = gst_pad_event_default (pad, parent, event);
428 break;
429 }
430 return res;
431 }
432
433 static gboolean
gst_modplug_load_song(GstModPlug * modplug)434 gst_modplug_load_song (GstModPlug * modplug)
435 {
436 GstCaps *newcaps;
437 GstStructure *structure;
438 GstMapInfo map;
439 const gchar *format;
440
441 GST_DEBUG_OBJECT (modplug, "Setting caps");
442
443 /* negotiate srcpad caps */
444 if ((newcaps = gst_pad_get_allowed_caps (modplug->srcpad)) == NULL) {
445 newcaps = gst_pad_get_pad_template_caps (modplug->srcpad);
446 }
447 newcaps = gst_caps_make_writable (newcaps);
448
449 GST_DEBUG_OBJECT (modplug, "allowed caps %" GST_PTR_FORMAT, newcaps);
450
451 structure = gst_caps_get_structure (newcaps, 0);
452
453 if (!gst_structure_fixate_field_string (structure, "format",
454 GST_AUDIO_NE (S16)))
455 GST_WARNING_OBJECT (modplug, "Failed to fixate format to S16NE");
456 if (!gst_structure_fixate_field_nearest_int (structure, "rate", 44100))
457 GST_WARNING_OBJECT (modplug, "Failed to fixate rate to 44100");
458 if (!gst_structure_fixate_field_nearest_int (structure, "channels", 2))
459 GST_WARNING_OBJECT (modplug,
460 "Failed to fixate number of channels to stereo");
461
462 GST_DEBUG_OBJECT (modplug, "normalized caps %" GST_PTR_FORMAT, newcaps);
463
464 newcaps = gst_caps_fixate (newcaps);
465
466 GST_DEBUG_OBJECT (modplug, "fixated caps %" GST_PTR_FORMAT, newcaps);
467
468 /* set up modplug to output the negotiated format */
469 structure = gst_caps_get_structure (newcaps, 0);
470 format = gst_structure_get_string (structure, "format");
471
472 if (g_str_equal (format, GST_AUDIO_NE (S32)))
473 modplug->bits = 32;
474 else if (g_str_equal (format, GST_AUDIO_NE (S16)))
475 modplug->bits = 16;
476 else
477 modplug->bits = 8;
478
479 gst_structure_get_int (structure, "channels", &modplug->channel);
480 gst_structure_get_int (structure, "rate", &modplug->frequency);
481
482 GST_DEBUG_OBJECT (modplug,
483 "Audio settings: %d bits, %d channel(s), %d Hz sampling rate",
484 modplug->bits, modplug->channel, modplug->frequency);
485
486 gst_pad_set_caps (modplug->srcpad, newcaps);
487 gst_caps_unref (newcaps);
488
489 modplug->read_samples = 1152;
490 modplug->read_bytes =
491 modplug->read_samples * modplug->channel * modplug->bits / 8;
492
493 GST_DEBUG_OBJECT (modplug, "Loading song");
494
495 modplug->mSoundFile = new CSoundFile;
496
497 modplug->mSoundFile->SetWaveConfig (modplug->frequency, modplug->bits,
498 modplug->channel);
499
500 modplug->mSoundFile->SetWaveConfigEx (modplug->surround, !modplug->oversamp,
501 modplug->reverb, true, modplug->megabass, modplug->noise_reduction, true);
502 modplug->mSoundFile->SetResamplingMode (SRCMODE_POLYPHASE);
503
504 if (modplug->surround)
505 modplug->mSoundFile->SetSurroundParameters (modplug->surround_depth,
506 modplug->surround_delay);
507
508 if (modplug->megabass)
509 modplug->mSoundFile->SetXBassParameters (modplug->megabass_amount,
510 modplug->megabass_range);
511
512 if (modplug->reverb)
513 modplug->mSoundFile->SetReverbParameters (modplug->reverb_depth,
514 modplug->reverb_delay);
515
516
517 gst_buffer_map (modplug->buffer, &map, GST_MAP_READ);
518 if (!modplug->mSoundFile->Create (map.data, modplug->song_size))
519 goto load_error;
520 gst_buffer_unmap (modplug->buffer, &map);
521
522 modplug->song_length = modplug->mSoundFile->GetSongTime () * GST_SECOND;
523 modplug->seek_at = -1;
524
525 GST_INFO_OBJECT (modplug, "Song length: %" GST_TIME_FORMAT,
526 GST_TIME_ARGS ((guint64) modplug->song_length));
527
528 return TRUE;
529
530 /* ERRORS */
531 load_error:
532 {
533 gst_buffer_unmap (modplug->buffer, &map);
534 GST_ELEMENT_ERROR (modplug, STREAM, DECODE, (NULL),
535 ("Unable to load song"));
536 return FALSE;
537 }
538 }
539
540 static gboolean
gst_modplug_sinkpad_activate(GstPad * sinkpad,GstObject * parent)541 gst_modplug_sinkpad_activate (GstPad * sinkpad, GstObject * parent)
542 {
543 GstQuery *query;
544 gboolean pull_mode;
545
546 query = gst_query_new_scheduling ();
547
548 if (!gst_pad_peer_query (sinkpad, query)) {
549 gst_query_unref (query);
550 goto activate_push;
551 }
552
553 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
554 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
555 gst_query_unref (query);
556
557 if (!pull_mode)
558 goto activate_push;
559
560 GST_DEBUG_OBJECT (sinkpad, "activating pull");
561 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
562
563 activate_push:
564 {
565 GST_DEBUG_OBJECT (sinkpad, "activating push");
566 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
567 }
568 }
569
570 static gboolean
gst_modplug_sinkpad_activate_mode(GstPad * pad,GstObject * parent,GstPadMode mode,gboolean active)571 gst_modplug_sinkpad_activate_mode (GstPad * pad, GstObject * parent,
572 GstPadMode mode, gboolean active)
573 {
574 GstModPlug *modplug = GST_MODPLUG (parent);
575 gboolean res;
576
577 switch (mode) {
578 case GST_PAD_MODE_PUSH:
579 res = TRUE;
580 break;
581 case GST_PAD_MODE_PULL:
582 if (active) {
583 res = gst_pad_start_task (pad, (GstTaskFunction) gst_modplug_loop,
584 modplug, NULL);
585 } else {
586 res = gst_pad_stop_task (pad);
587 }
588 break;
589 default:
590 res = FALSE;
591 break;
592 }
593 return res;
594 }
595
596 static gboolean
gst_modplug_get_upstream_size(GstModPlug * modplug,gint64 * length)597 gst_modplug_get_upstream_size (GstModPlug * modplug, gint64 * length)
598 {
599 gboolean res = FALSE;
600 GstPad *peer;
601
602 peer = gst_pad_get_peer (modplug->sinkpad);
603 if (peer == NULL)
604 return FALSE;
605
606 if (gst_pad_query_duration (peer, GST_FORMAT_BYTES, length) && *length >= 0) {
607 res = TRUE;
608 }
609
610 gst_object_unref (peer);
611 return res;
612 }
613
614 static void
gst_modplug_loop(GstModPlug * modplug)615 gst_modplug_loop (GstModPlug * modplug)
616 {
617 GstFlowReturn flow;
618 GstBuffer *out = NULL;
619 GstMapInfo map;
620
621 g_assert (GST_IS_MODPLUG (modplug));
622
623 /* first, get the size of the song */
624 if (!modplug->song_size) {
625 if (!gst_modplug_get_upstream_size (modplug, &modplug->song_size)) {
626 GST_ELEMENT_ERROR (modplug, STREAM, DECODE, (NULL),
627 ("Unable to load song"));
628 goto pause;
629 }
630
631 if (modplug->buffer) {
632 gst_buffer_unref (modplug->buffer);
633 }
634 modplug->buffer = gst_buffer_new_and_alloc (modplug->song_size);
635 modplug->offset = 0;
636 }
637
638 /* read in the song data */
639 if (!modplug->mSoundFile) {
640 GstBuffer *buffer = NULL;
641 guint64 read_size = modplug->song_size - modplug->offset;
642
643 if (read_size > 4096)
644 read_size = 4096;
645
646 flow =
647 gst_pad_pull_range (modplug->sinkpad, modplug->offset, read_size,
648 &buffer);
649 if (flow != GST_FLOW_OK) {
650 GST_ELEMENT_ERROR (modplug, STREAM, DECODE, (NULL),
651 ("Unable to load song"));
652 goto pause;
653 }
654
655 /* GST_LOG_OBJECT (modplug, "Read %u bytes", GST_BUFFER_SIZE (buffer)); */
656 gst_buffer_map (buffer, &map, GST_MAP_READ);
657 gst_buffer_fill (modplug->buffer, modplug->offset, map.data, map.size);
658 gst_buffer_unmap (buffer, &map);
659 gst_buffer_unref (buffer);
660
661 modplug->offset += read_size;
662
663 /* actually load it */
664 if (modplug->offset == modplug->song_size) {
665 GstTagList *tags;
666 gboolean ok;
667 #define COMMENT_SIZE 16384
668 gchar comment[COMMENT_SIZE];
669 GstSegment seg;
670
671 ok = gst_modplug_load_song (modplug);
672 gst_buffer_unref (modplug->buffer);
673 modplug->buffer = NULL;
674 modplug->offset = 0;
675
676 if (!ok) {
677 goto pause;
678 }
679
680 gst_segment_init (&seg, GST_FORMAT_TIME);
681 seg.stop = modplug->song_length;
682 gst_pad_push_event (modplug->srcpad, gst_event_new_segment (&seg));
683
684 /* get and send metadata */
685 tags = gst_tag_list_new_empty ();
686 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND,
687 GST_TAG_TITLE, modplug->mSoundFile->GetTitle (),
688 GST_TAG_BEATS_PER_MINUTE,
689 (gdouble) modplug->mSoundFile->GetMusicTempo (), NULL);
690
691 if (modplug->mSoundFile->GetSongComments ((gchar *) & comment,
692 COMMENT_SIZE, 32)) {
693 comment[COMMENT_SIZE - 1] = '\0';
694 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND,
695 GST_TAG_COMMENT, comment, NULL);
696 }
697 gst_pad_push_event (modplug->srcpad, gst_event_new_tag (tags));
698 } else {
699 /* not fully loaded yet */
700 return;
701 }
702 }
703
704 /* could move this to gst_modplug_src_event
705 * if libmodplug was definitely thread safe.. */
706 if (modplug->seek_at != -1) {
707 gint seek_to_pos;
708 gfloat temp;
709
710 temp = (gfloat) modplug->song_length / modplug->seek_at;
711 seek_to_pos = (gint) (modplug->mSoundFile->GetMaxPosition () / temp);
712
713 GST_DEBUG_OBJECT (modplug, "Seeking to row %d", seek_to_pos);
714
715 modplug->mSoundFile->SetCurrentPos (seek_to_pos);
716 modplug->seek_at = -1;
717 }
718
719 /* read and output a buffer */
720 GST_LOG_OBJECT (modplug, "Read %d bytes", (gint) modplug->read_bytes);
721 /* libmodplug 0.8.7 trashes memory */
722 out = gst_buffer_new_allocate (NULL, modplug->read_bytes * 2, NULL);
723
724 gst_buffer_map (out, &map, GST_MAP_WRITE);
725 if (!modplug->mSoundFile->Read (map.data, modplug->read_bytes)) {
726 gst_buffer_unmap (out, &map);
727 goto eos;
728 }
729 gst_buffer_unmap (out, &map);
730 gst_buffer_resize (out, 0, modplug->read_bytes);
731
732 GST_BUFFER_DURATION (out) =
733 gst_util_uint64_scale_int (modplug->read_samples, GST_SECOND,
734 modplug->frequency);
735 GST_BUFFER_OFFSET (out) = modplug->offset;
736 GST_BUFFER_TIMESTAMP (out) =
737 gst_util_uint64_scale_int (modplug->offset, GST_SECOND,
738 modplug->frequency);
739
740 modplug->offset += modplug->read_samples;
741
742 flow = gst_pad_push (modplug->srcpad, out);
743
744 if (flow != GST_FLOW_OK) {
745 GST_LOG_OBJECT (modplug, "pad push flow: %s", gst_flow_get_name (flow));
746 goto pause;
747 }
748
749 return;
750
751 eos:
752 {
753 gst_buffer_unref (out);
754 GST_INFO_OBJECT (modplug, "EOS");
755 gst_pad_push_event (modplug->srcpad, gst_event_new_eos ());
756 goto pause;
757 }
758
759 pause:
760 {
761 GST_INFO_OBJECT (modplug, "Pausing");
762 gst_pad_pause_task (modplug->sinkpad);
763 }
764 }
765
766
767 static GstStateChangeReturn
gst_modplug_change_state(GstElement * element,GstStateChange transition)768 gst_modplug_change_state (GstElement * element, GstStateChange transition)
769 {
770 GstModPlug *modplug;
771 GstStateChangeReturn ret;
772
773 modplug = GST_MODPLUG (element);
774
775 switch (transition) {
776 case GST_STATE_CHANGE_READY_TO_PAUSED:
777 modplug->buffer = NULL;
778 modplug->offset = 0;
779 modplug->song_size = 0;
780 break;
781 default:
782 break;
783 }
784
785 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
786 if (ret == GST_STATE_CHANGE_FAILURE)
787 return ret;
788
789 switch (transition) {
790 case GST_STATE_CHANGE_PAUSED_TO_READY:
791 if (modplug->buffer) {
792 gst_buffer_unref (modplug->buffer);
793 modplug->buffer = NULL;
794 }
795 if (modplug->mSoundFile) {
796 modplug->mSoundFile->Destroy ();
797 delete modplug->mSoundFile;
798 modplug->mSoundFile = NULL;
799 }
800 break;
801 default:
802 break;
803 }
804
805 return GST_STATE_CHANGE_SUCCESS;
806 }
807
808
809 static void
gst_modplug_set_property(GObject * object,guint id,const GValue * value,GParamSpec * pspec)810 gst_modplug_set_property (GObject * object, guint id, const GValue * value,
811 GParamSpec * pspec)
812 {
813 GstModPlug *modplug;
814
815 g_return_if_fail (GST_IS_MODPLUG (object));
816 modplug = GST_MODPLUG (object);
817
818 switch (id) {
819 case ARG_REVERB:
820 modplug->reverb = g_value_get_boolean (value);
821 break;
822 case ARG_REVERB_DEPTH:
823 modplug->reverb_depth = g_value_get_int (value);
824 break;
825 case ARG_REVERB_DELAY:
826 modplug->reverb_delay = g_value_get_int (value);
827 break;
828 case ARG_MEGABASS:
829 modplug->megabass = g_value_get_boolean (value);
830 break;
831 case ARG_MEGABASS_AMOUNT:
832 modplug->megabass_amount = g_value_get_int (value);
833 break;
834 case ARG_MEGABASS_RANGE:
835 modplug->megabass_range = g_value_get_int (value);
836 break;
837 case ARG_NOISE_REDUCTION:
838 modplug->noise_reduction = g_value_get_boolean (value);
839 break;
840 case ARG_SURROUND:
841 modplug->surround = g_value_get_boolean (value);
842 break;
843 case ARG_SURROUND_DEPTH:
844 modplug->surround_depth = g_value_get_int (value);
845 break;
846 case ARG_SURROUND_DELAY:
847 modplug->surround_delay = g_value_get_int (value);
848 break;
849 default:
850 break;
851 }
852 }
853
854 static void
gst_modplug_get_property(GObject * object,guint id,GValue * value,GParamSpec * pspec)855 gst_modplug_get_property (GObject * object, guint id, GValue * value,
856 GParamSpec * pspec)
857 {
858 GstModPlug *modplug;
859
860 g_return_if_fail (GST_IS_MODPLUG (object));
861 modplug = GST_MODPLUG (object);
862
863 switch (id) {
864 case ARG_REVERB:
865 g_value_set_boolean (value, modplug->reverb);
866 break;
867 case ARG_REVERB_DEPTH:
868 g_value_set_int (value, modplug->reverb_depth);
869 break;
870 case ARG_REVERB_DELAY:
871 g_value_set_int (value, modplug->reverb_delay);
872 break;
873 case ARG_MEGABASS:
874 g_value_set_boolean (value, modplug->megabass);
875 break;
876 case ARG_MEGABASS_AMOUNT:
877 g_value_set_int (value, modplug->megabass_amount);
878 break;
879 case ARG_MEGABASS_RANGE:
880 g_value_set_int (value, modplug->megabass_range);
881 break;
882 case ARG_SURROUND:
883 g_value_set_boolean (value, modplug->surround);
884 break;
885 case ARG_SURROUND_DEPTH:
886 g_value_set_int (value, modplug->surround_depth);
887 break;
888 case ARG_SURROUND_DELAY:
889 g_value_set_int (value, modplug->surround_delay);
890 break;
891 case ARG_NOISE_REDUCTION:
892 g_value_set_boolean (value, modplug->noise_reduction);
893 break;
894 default:
895 break;
896 }
897 }
898
899 static gboolean
plugin_init(GstPlugin * plugin)900 plugin_init (GstPlugin * plugin)
901 {
902 return gst_element_register (plugin, "modplug",
903 GST_RANK_PRIMARY, GST_TYPE_MODPLUG);
904 }
905
906 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
907 GST_VERSION_MINOR,
908 modplug,
909 ".MOD audio decoding",
910 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
911