blob: 555474ed3f4703aa99936ebcdb921496e3f65924 [file] [log] [blame]
Ronald S. Bultje868291e2004-11-07 18:30:06 +00001/* GStreamer Musepack decoder plugin
2 * Copyright (C) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +00003 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
Sebastian Dröge01bcb092008-04-24 22:19:48 +00004 * Copyright (C) 2008 Sebastian Dröge <slomo@circular-chaos.org>
Ronald S. Bultje868291e2004-11-07 18:30:06 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
Edward Herveya1b7f842012-03-06 18:11:35 +010022/* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
23 * with newer GLib versions (>= 2.31.0) */
24#define GLIB_DISABLE_DEPRECATION_WARNINGS
25
Ronald S. Bultje868291e2004-11-07 18:30:06 +000026#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif
29
30#include "gstmusepackdec.h"
31#include "gstmusepackreader.h"
32
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +000033GST_DEBUG_CATEGORY (musepackdec_debug);
34#define GST_CAT_DEFAULT musepackdec_debug
35
Ronald S. Bultje868291e2004-11-07 18:30:06 +000036static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
37 GST_PAD_SINK,
38 GST_PAD_ALWAYS,
Sebastian Dröge01bcb092008-04-24 22:19:48 +000039#ifdef MPC_IS_OLD_API
40 GST_STATIC_CAPS ("audio/x-musepack, streamversion = (int) 7")
41#else
42 GST_STATIC_CAPS ("audio/x-musepack, streamversion = (int) { 7, 8 }")
43#endif
Ronald S. Bultje868291e2004-11-07 18:30:06 +000044 );
45
Ronald S. Bultje40e9cff2004-12-03 18:13:43 +000046#ifdef MPC_FIXED_POINT
47#define BASE_CAPS \
48 "audio/x-raw-int, " \
49 "signed = (bool) TRUE, " \
50 "width = (int) 32, " \
51 "depth = (int) 32"
52#else
53#define BASE_CAPS \
54 "audio/x-raw-float, " \
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +000055 "width = (int) 32"
Ronald S. Bultje40e9cff2004-12-03 18:13:43 +000056#endif
57
Ronald S. Bultje868291e2004-11-07 18:30:06 +000058static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
59 GST_PAD_SRC,
60 GST_PAD_ALWAYS,
Ronald S. Bultje40e9cff2004-12-03 18:13:43 +000061 GST_STATIC_CAPS (BASE_CAPS ", "
Ronald S. Bultje868291e2004-11-07 18:30:06 +000062 "endianness = (int) BYTE_ORDER, "
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +000063 "rate = (int) [ 8000, 96000 ], " "channels = (int) [ 1, 2 ]")
Ronald S. Bultje868291e2004-11-07 18:30:06 +000064 );
65
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +000066static void gst_musepackdec_dispose (GObject * obj);
Ronald S. Bultje868291e2004-11-07 18:30:06 +000067
68static gboolean gst_musepackdec_src_event (GstPad * pad, GstEvent * event);
Edgard Limaa5ef8ae2005-11-22 15:09:28 +000069static const GstQueryType *gst_musepackdec_get_src_query_types (GstPad * pad);
70static gboolean gst_musepackdec_src_query (GstPad * pad, GstQuery * query);
Edgard Limaa5ef8ae2005-11-22 15:09:28 +000071static gboolean gst_musepackdec_sink_activate (GstPad * sinkpad);
72static gboolean
73gst_musepackdec_sink_activate_pull (GstPad * sinkpad, gboolean active);
74
75static void gst_musepackdec_loop (GstPad * sinkpad);
Jan Schmidt012dfb82005-09-05 17:20:29 +000076static GstStateChangeReturn
77gst_musepackdec_change_state (GstElement * element, GstStateChange transition);
Ronald S. Bultje868291e2004-11-07 18:30:06 +000078
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +000079GST_BOILERPLATE (GstMusepackDec, gst_musepackdec, GstElement, GST_TYPE_ELEMENT);
Ronald S. Bultje868291e2004-11-07 18:30:06 +000080
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +000081static void
82gst_musepackdec_base_init (gpointer klass)
Ronald S. Bultje868291e2004-11-07 18:30:06 +000083{
Ronald S. Bultje868291e2004-11-07 18:30:06 +000084 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
85
86 gst_element_class_add_pad_template (element_class,
87 gst_static_pad_template_get (&src_template));
88 gst_element_class_add_pad_template (element_class,
89 gst_static_pad_template_get (&sink_template));
90
Mark Nauwelaerts578861a2012-09-14 17:08:49 +020091 gst_element_class_set_metadata (element_class, "Musepack decoder",
Sebastian Dröge01bcb092008-04-24 22:19:48 +000092 "Codec/Decoder/Audio",
93 "Musepack decoder", "Ronald Bultje <rbultje@ronald.bitfreak.net>");
Ronald S. Bultje868291e2004-11-07 18:30:06 +000094}
95
96static void
97gst_musepackdec_class_init (GstMusepackDecClass * klass)
98{
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +000099 GST_ELEMENT_CLASS (klass)->change_state =
100 GST_DEBUG_FUNCPTR (gst_musepackdec_change_state);
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000101
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000102 G_OBJECT_CLASS (klass)->dispose = GST_DEBUG_FUNCPTR (gst_musepackdec_dispose);
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000103}
104
105static void
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000106gst_musepackdec_init (GstMusepackDec * musepackdec, GstMusepackDecClass * klass)
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000107{
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000108 musepackdec->offset = 0;
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000109 musepackdec->rate = 0;
110 musepackdec->bps = 0;
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000111
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +0000112 musepackdec->r = g_new (mpc_reader, 1);
Sebastian Dröge01bcb092008-04-24 22:19:48 +0000113#ifdef MPC_IS_OLD_API
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +0000114 musepackdec->d = g_new (mpc_decoder, 1);
Sebastian Dröge01bcb092008-04-24 22:19:48 +0000115#endif
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000116
117 musepackdec->sinkpad =
Tim-Philipp Müller8b081dd2006-04-14 13:12:58 +0000118 gst_pad_new_from_static_template (&sink_template, "sink");
119 gst_pad_set_activate_function (musepackdec->sinkpad,
120 GST_DEBUG_FUNCPTR (gst_musepackdec_sink_activate));
121 gst_pad_set_activatepull_function (musepackdec->sinkpad,
122 GST_DEBUG_FUNCPTR (gst_musepackdec_sink_activate_pull));
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000123 gst_element_add_pad (GST_ELEMENT (musepackdec), musepackdec->sinkpad);
124
Tim-Philipp Müller8b081dd2006-04-14 13:12:58 +0000125 musepackdec->srcpad = gst_pad_new_from_static_template (&src_template, "src");
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000126 gst_pad_set_event_function (musepackdec->srcpad,
127 GST_DEBUG_FUNCPTR (gst_musepackdec_src_event));
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000128 gst_pad_set_query_function (musepackdec->srcpad,
129 GST_DEBUG_FUNCPTR (gst_musepackdec_src_query));
130 gst_pad_set_query_type_function (musepackdec->srcpad,
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000131 GST_DEBUG_FUNCPTR (gst_musepackdec_get_src_query_types));
132 gst_pad_use_fixed_caps (musepackdec->srcpad);
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000133 gst_element_add_pad (GST_ELEMENT (musepackdec), musepackdec->srcpad);
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000134}
135
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +0000136static void
137gst_musepackdec_dispose (GObject * obj)
138{
139 GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (obj);
140
141 g_free (musepackdec->r);
142 musepackdec->r = NULL;
Sebastian Dröge01bcb092008-04-24 22:19:48 +0000143
144#ifdef MPC_IS_OLD_API
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +0000145 g_free (musepackdec->d);
146 musepackdec->d = NULL;
Sebastian Dröge01bcb092008-04-24 22:19:48 +0000147#else
148 if (musepackdec->d) {
149 mpc_demux_exit (musepackdec->d);
150 musepackdec->d = NULL;
151 }
152#endif
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +0000153
154 G_OBJECT_CLASS (parent_class)->dispose (obj);
155}
156
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000157static void
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000158gst_musepackdec_send_newsegment (GstMusepackDec * dec)
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000159{
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000160 GstSegment *s = &dec->segment;
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000161 gint64 stop_time = GST_CLOCK_TIME_NONE;
162 gint64 start_time = 0;
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000163
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000164 start_time = gst_util_uint64_scale_int (s->start, GST_SECOND, dec->rate);
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000165
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000166 if (s->stop != -1)
167 stop_time = gst_util_uint64_scale_int (s->stop, GST_SECOND, dec->rate);
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000168
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000169 GST_DEBUG_OBJECT (dec, "sending newsegment from %" GST_TIME_FORMAT
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000170 " to %" GST_TIME_FORMAT ", rate = %.1f", GST_TIME_ARGS (start_time),
171 GST_TIME_ARGS (stop_time), s->rate);
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000172
173 gst_pad_push_event (dec->srcpad,
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000174 gst_event_new_new_segment (FALSE, s->rate, GST_FORMAT_TIME,
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000175 start_time, stop_time, start_time));
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000176}
177
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000178static gboolean
179gst_musepackdec_handle_seek_event (GstMusepackDec * dec, GstEvent * event)
180{
181 GstSeekType start_type, stop_type;
182 GstSeekFlags flags;
183 GstSegment segment;
184 GstFormat format;
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000185 gboolean flush;
186 gdouble rate;
187 gint64 start, stop;
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000188 gint samplerate;
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000189
190 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
191 &stop_type, &stop);
192
193 if (format != GST_FORMAT_TIME && format != GST_FORMAT_DEFAULT) {
194 GST_DEBUG_OBJECT (dec, "seek failed: only TIME or DEFAULT format allowed");
195 return FALSE;
196 }
197
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000198 samplerate = g_atomic_int_get (&dec->rate);
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000199
200 if (format == GST_FORMAT_TIME) {
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000201 if (start_type != GST_SEEK_TYPE_NONE)
202 start = gst_util_uint64_scale_int (start, samplerate, GST_SECOND);
203 if (stop_type != GST_SEEK_TYPE_NONE)
204 stop = gst_util_uint64_scale_int (stop, samplerate, GST_SECOND);
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000205 }
206
207 flush = ((flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH);
208
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000209 if (flush)
210 gst_pad_push_event (dec->srcpad, gst_event_new_flush_start ());
211 else
212 gst_pad_pause_task (dec->sinkpad); /* not _stop_task()? */
213
214 GST_PAD_STREAM_LOCK (dec->sinkpad);
215
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000216 /* operate on segment copy until we know the seek worked */
217 segment = dec->segment;
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000218
219 gst_segment_set_seek (&segment, rate, GST_FORMAT_DEFAULT,
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000220 flags, start_type, start, stop_type, stop, NULL);
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000221
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000222 gst_pad_push_event (dec->sinkpad, gst_event_new_flush_stop ());
223
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000224 GST_DEBUG_OBJECT (dec, "segment: [%" G_GINT64_FORMAT "-%" G_GINT64_FORMAT
225 "] = [%" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "]",
226 segment.start, segment.stop,
227 GST_TIME_ARGS (segment.start * GST_SECOND / dec->rate),
228 GST_TIME_ARGS (segment.stop * GST_SECOND / dec->rate));
229
230 GST_DEBUG_OBJECT (dec, "performing seek to sample %" G_GINT64_FORMAT,
231 segment.start);
232
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000233 if (segment.start < 0 || segment.start >= segment.duration) {
234 GST_WARNING_OBJECT (dec, "seek out of bounds");
235 goto failed;
236 }
Sebastian Dröge01bcb092008-04-24 22:19:48 +0000237#ifdef MPC_IS_OLD_API
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000238 if (!mpc_decoder_seek_sample (dec->d, segment.start))
239 goto failed;
Sebastian Dröge01bcb092008-04-24 22:19:48 +0000240#else
241 if (mpc_demux_seek_sample (dec->d, segment.start) != MPC_STATUS_OK)
242 goto failed;
243#endif
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000244
Tim-Philipp Mülleraf701d82006-04-20 18:02:07 +0000245 if ((flags & GST_SEEK_FLAG_SEGMENT) == GST_SEEK_FLAG_SEGMENT) {
246 GST_DEBUG_OBJECT (dec, "posting SEGMENT_START message");
247
248 gst_element_post_message (GST_ELEMENT (dec),
249 gst_message_new_segment_start (GST_OBJECT (dec), GST_FORMAT_TIME,
250 gst_util_uint64_scale_int (segment.start, GST_SECOND, dec->rate)));
251 }
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000252
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000253 if (flush) {
254 gst_pad_push_event (dec->srcpad, gst_event_new_flush_stop ());
255 }
256
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000257 gst_segment_set_last_stop (&segment, GST_FORMAT_DEFAULT, segment.start);
258 dec->segment = segment;
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000259 gst_musepackdec_send_newsegment (dec);
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000260
261 GST_DEBUG_OBJECT (dec, "seek successful");
262
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000263 gst_pad_start_task (dec->sinkpad,
Wim Taymansdbed7262012-06-20 10:34:48 +0200264 (GstTaskFunction) gst_musepackdec_loop, dec->sinkpad, NULL);
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000265
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000266 GST_PAD_STREAM_UNLOCK (dec->sinkpad);
267
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000268 return TRUE;
269
270failed:
271 {
272 GST_WARNING_OBJECT (dec, "seek failed");
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000273 GST_PAD_STREAM_UNLOCK (dec->sinkpad);
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000274 return FALSE;
275 }
276}
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000277
278static gboolean
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000279gst_musepackdec_src_event (GstPad * pad, GstEvent * event)
280{
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000281 GstMusepackDec *dec;
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000282 gboolean res;
283
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000284 dec = GST_MUSEPACK_DEC (gst_pad_get_parent (pad));
285
286 GST_DEBUG_OBJECT (dec, "handling %s event", GST_EVENT_TYPE_NAME (event));
287
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000288 switch (GST_EVENT_TYPE (event)) {
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000289 case GST_EVENT_SEEK:
290 res = gst_musepackdec_handle_seek_event (dec, event);
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000291 break;
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000292 default:
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000293 res = gst_pad_event_default (pad, event);
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000294 break;
295 }
296
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000297 gst_object_unref (dec);
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000298 return res;
299}
300
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000301static const GstQueryType *
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000302gst_musepackdec_get_src_query_types (GstPad * pad)
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000303{
304 static const GstQueryType query_types[] = {
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000305 GST_QUERY_POSITION,
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000306 GST_QUERY_DURATION,
Sebastian Dröge7de65d92009-07-24 07:40:17 +0200307 GST_QUERY_SEEKING,
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000308 (GstQueryType) 0
309 };
310
311 return query_types;
312}
313
314static gboolean
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000315gst_musepackdec_src_query (GstPad * pad, GstQuery * query)
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000316{
317 GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (gst_pad_get_parent (pad));
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000318 GstFormat format;
319 gboolean res = FALSE;
320 gint samplerate;
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000321
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000322 samplerate = g_atomic_int_get (&musepackdec->rate);
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000323
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000324 if (samplerate == 0)
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000325 goto done;
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000326
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000327 switch (GST_QUERY_TYPE (query)) {
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000328 case GST_QUERY_POSITION:{
329 gint64 cur, cur_off;
330
331 gst_query_parse_position (query, &format, NULL);
332
333 GST_OBJECT_LOCK (musepackdec);
334 cur_off = musepackdec->segment.last_stop;
335 GST_OBJECT_UNLOCK (musepackdec);
336
337 if (format == GST_FORMAT_TIME) {
338 cur = gst_util_uint64_scale_int (cur_off, GST_SECOND, samplerate);
339 gst_query_set_position (query, GST_FORMAT_TIME, cur);
340 res = TRUE;
341 } else if (format == GST_FORMAT_DEFAULT) {
342 gst_query_set_position (query, GST_FORMAT_DEFAULT, cur_off);
343 res = TRUE;
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000344 }
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000345 break;
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000346 }
347 case GST_QUERY_DURATION:{
348 gint64 len, len_off;
349
350 gst_query_parse_duration (query, &format, NULL);
351
352 GST_OBJECT_LOCK (musepackdec);
353 len_off = musepackdec->segment.duration;
354 GST_OBJECT_UNLOCK (musepackdec);
355
356 if (format == GST_FORMAT_TIME) {
357 len = gst_util_uint64_scale_int (len_off, GST_SECOND, samplerate);
358 gst_query_set_duration (query, GST_FORMAT_TIME, len);
359 res = TRUE;
360 } else if (format == GST_FORMAT_DEFAULT) {
361 gst_query_set_duration (query, GST_FORMAT_DEFAULT, len_off);
362 res = TRUE;
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000363 }
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000364 break;
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000365 }
Sebastian Dröge7de65d92009-07-24 07:40:17 +0200366 case GST_QUERY_SEEKING:{
367 GstFormat fmt;
368
369 res = TRUE;
370 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
371 if (fmt == GST_FORMAT_TIME || fmt == GST_FORMAT_DEFAULT)
372 gst_query_set_seeking (query, fmt, TRUE, 0, -1);
373 else
374 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
375
376 break;
377 }
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000378 default:
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000379 res = gst_pad_query_default (pad, query);
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000380 break;
381 }
382
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000383done:
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000384 gst_object_unref (musepackdec);
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000385 return res;
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000386}
387
388static gboolean
389gst_musepack_stream_init (GstMusepackDec * musepackdec)
390{
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +0000391 mpc_streaminfo i;
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000392 GstTagList *tags;
Ronald S. Bultje40e9cff2004-12-03 18:13:43 +0000393 GstCaps *caps;
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000394
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +0000395 /* set up reading */
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000396 gst_musepack_init_reader (musepackdec->r, musepackdec);
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000397
Sebastian Dröge01bcb092008-04-24 22:19:48 +0000398#ifdef MPC_IS_OLD_API
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +0000399 /* streaminfo */
400 mpc_streaminfo_init (&i);
401 if (mpc_streaminfo_read (&i, musepackdec->r) < 0) {
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000402 GST_ELEMENT_ERROR (musepackdec, STREAM, WRONG_TYPE, (NULL), (NULL));
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000403 return FALSE;
404 }
405
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +0000406 /* decoding */
407 mpc_decoder_setup (musepackdec->d, musepackdec->r);
408 mpc_decoder_scale_output (musepackdec->d, 1.0);
409 if (!mpc_decoder_initialize (musepackdec->d, &i)) {
410 GST_ELEMENT_ERROR (musepackdec, STREAM, WRONG_TYPE, (NULL), (NULL));
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000411 return FALSE;
412 }
Sebastian Dröge01bcb092008-04-24 22:19:48 +0000413#else
414 musepackdec->d = mpc_demux_init (musepackdec->r);
415 if (!musepackdec->d) {
416 GST_ELEMENT_ERROR (musepackdec, STREAM, WRONG_TYPE, (NULL), (NULL));
417 return FALSE;
418 }
419
420 mpc_demux_get_info (musepackdec->d, &i);
421#endif
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000422
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +0000423 /* capsnego */
Ronald S. Bultje40e9cff2004-12-03 18:13:43 +0000424 caps = gst_caps_from_string (BASE_CAPS);
425 gst_caps_set_simple (caps,
426 "endianness", G_TYPE_INT, G_BYTE_ORDER,
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +0000427 "channels", G_TYPE_INT, i.channels,
428 "rate", G_TYPE_INT, i.sample_freq, NULL);
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000429 gst_pad_use_fixed_caps (musepackdec->srcpad);
430 if (!gst_pad_set_caps (musepackdec->srcpad, caps)) {
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000431 GST_ELEMENT_ERROR (musepackdec, CORE, NEGOTIATION, (NULL), (NULL));
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000432 return FALSE;
433 }
434
Sebastian Dröge01bcb092008-04-24 22:19:48 +0000435 g_atomic_int_set (&musepackdec->bps, 4 * i.channels);
436 g_atomic_int_set (&musepackdec->rate, i.sample_freq);
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000437
438 gst_segment_set_last_stop (&musepackdec->segment, GST_FORMAT_DEFAULT, 0);
439 gst_segment_set_duration (&musepackdec->segment, GST_FORMAT_DEFAULT,
440 mpc_streaminfo_get_length_samples (&i));
441
442 /* send basic tags */
443 tags = gst_tag_list_new ();
444 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
445 GST_TAG_AUDIO_CODEC, "Musepack", NULL);
446
447 if (i.encoder[0] != '\0' && i.encoder_version > 0) {
448 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
449 GST_TAG_ENCODER, i.encoder,
450 GST_TAG_ENCODER_VERSION, i.encoder_version, NULL);
451 }
452
453 if (i.bitrate > 0) {
454 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
455 GST_TAG_BITRATE, i.bitrate, NULL);
456 } else if (i.average_bitrate > 0.0) {
457 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
458 GST_TAG_BITRATE, (guint) i.average_bitrate, NULL);
459 }
460
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000461 if (i.gain_title != 0 || i.gain_album != 0) {
462 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
463 GST_TAG_TRACK_GAIN, (gdouble) i.gain_title / 100.0,
464 GST_TAG_ALBUM_GAIN, (gdouble) i.gain_album / 100.0, NULL);
465 }
466
Tim-Philipp Müller8b081dd2006-04-14 13:12:58 +0000467 if (i.peak_title != 0 && i.peak_title != 32767 &&
468 i.peak_album != 0 && i.peak_album != 32767) {
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000469 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
Tim-Philipp Müller8b081dd2006-04-14 13:12:58 +0000470 GST_TAG_TRACK_PEAK, (gdouble) i.peak_title / 32767.0,
471 GST_TAG_ALBUM_PEAK, (gdouble) i.peak_album / 32767.0, NULL);
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000472 }
473
474 GST_LOG_OBJECT (musepackdec, "Posting tags: %" GST_PTR_FORMAT, tags);
475 gst_element_found_tags (GST_ELEMENT (musepackdec), tags);
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000476
477 return TRUE;
478}
479
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000480static gboolean
481gst_musepackdec_sink_activate (GstPad * sinkpad)
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000482{
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000483 if (!gst_pad_check_pull_range (sinkpad))
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000484 return FALSE;
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000485
486 return gst_pad_activate_pull (sinkpad, TRUE);
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000487}
488
489static gboolean
490gst_musepackdec_sink_activate_pull (GstPad * sinkpad, gboolean active)
491{
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000492 gboolean result;
493
494 if (active) {
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000495 result = gst_pad_start_task (sinkpad,
Wim Taymansdbed7262012-06-20 10:34:48 +0200496 (GstTaskFunction) gst_musepackdec_loop, sinkpad, NULL);
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000497 } else {
498 result = gst_pad_stop_task (sinkpad);
499 }
500
501 return result;
502}
503
504static void
505gst_musepackdec_loop (GstPad * sinkpad)
506{
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000507 GstMusepackDec *musepackdec;
508 GstFlowReturn flow;
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000509 GstBuffer *out;
Sebastian Dröge01bcb092008-04-24 22:19:48 +0000510
511#ifdef MPC_IS_OLD_API
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +0000512 guint32 update_acc, update_bits;
Sebastian Dröge01bcb092008-04-24 22:19:48 +0000513#else
514 mpc_frame_info frame;
515 mpc_status err;
516#endif
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000517 gint num_samples, samplerate, bitspersample;
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000518
519 musepackdec = GST_MUSEPACK_DEC (GST_PAD_PARENT (sinkpad));
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000520
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000521 samplerate = g_atomic_int_get (&musepackdec->rate);
522
523 if (samplerate == 0) {
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000524 if (!gst_musepack_stream_init (musepackdec))
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000525 goto pause_task;
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000526
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000527 gst_musepackdec_send_newsegment (musepackdec);
528 samplerate = g_atomic_int_get (&musepackdec->rate);
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000529 }
530
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000531 bitspersample = g_atomic_int_get (&musepackdec->bps);
532
533 flow = gst_pad_alloc_buffer_and_set_caps (musepackdec->srcpad, -1,
534 MPC_DECODER_BUFFER_LENGTH * 4, GST_PAD_CAPS (musepackdec->srcpad), &out);
535
536 if (flow != GST_FLOW_OK) {
537 GST_DEBUG_OBJECT (musepackdec, "Flow: %s", gst_flow_get_name (flow));
538 goto pause_task;
539 }
Sebastian Dröge01bcb092008-04-24 22:19:48 +0000540#ifdef MPC_IS_OLD_API
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000541 num_samples = mpc_decoder_decode (musepackdec->d,
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +0000542 (MPC_SAMPLE_FORMAT *) GST_BUFFER_DATA (out), &update_acc, &update_bits);
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000543
544 if (num_samples < 0) {
545 GST_ERROR_OBJECT (musepackdec, "Failed to decode sample");
546 GST_ELEMENT_ERROR (musepackdec, STREAM, DECODE, (NULL), (NULL));
547 goto pause_task;
548 } else if (num_samples == 0) {
Tim-Philipp Mülleraf701d82006-04-20 18:02:07 +0000549 goto eos_and_pause;
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000550 }
Sebastian Dröge01bcb092008-04-24 22:19:48 +0000551#else
552 frame.buffer = (MPC_SAMPLE_FORMAT *) GST_BUFFER_DATA (out);
553 err = mpc_demux_decode (musepackdec->d, &frame);
554
555 if (err != MPC_STATUS_OK) {
556 GST_ERROR_OBJECT (musepackdec, "Failed to decode sample");
557 GST_ELEMENT_ERROR (musepackdec, STREAM, DECODE, (NULL), (NULL));
558 goto pause_task;
559 } else if (frame.bits == -1) {
560 goto eos_and_pause;
561 }
562
563 num_samples = frame.samples;
564#endif
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +0000565
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000566 GST_BUFFER_SIZE (out) = num_samples * bitspersample;
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000567
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000568 GST_BUFFER_OFFSET (out) = musepackdec->segment.last_stop;
569 GST_BUFFER_TIMESTAMP (out) =
570 gst_util_uint64_scale_int (musepackdec->segment.last_stop,
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000571 GST_SECOND, samplerate);
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000572 GST_BUFFER_DURATION (out) =
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000573 gst_util_uint64_scale_int (num_samples, GST_SECOND, samplerate);
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000574
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000575 musepackdec->segment.last_stop += num_samples;
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000576
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000577 GST_LOG_OBJECT (musepackdec, "Pushing buffer, timestamp %" GST_TIME_FORMAT,
Tim-Philipp Müller64350142005-12-12 10:40:42 +0000578 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (out)));
579
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000580 flow = gst_pad_push (musepackdec->srcpad, out);
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000581 if (flow != GST_FLOW_OK) {
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000582 GST_DEBUG_OBJECT (musepackdec, "Flow: %s", gst_flow_get_name (flow));
583 goto pause_task;
584 }
585
Tim-Philipp Mülleraf701d82006-04-20 18:02:07 +0000586 /* check if we're at the end of a configured segment */
587 if (musepackdec->segment.stop != -1 &&
588 musepackdec->segment.last_stop >= musepackdec->segment.stop) {
589 gint64 stop_time;
590
591 GST_DEBUG_OBJECT (musepackdec, "Reached end of configured segment");
592
593 if ((musepackdec->segment.flags & GST_SEEK_FLAG_SEGMENT) == 0)
594 goto eos_and_pause;
595
596 GST_DEBUG_OBJECT (musepackdec, "Posting SEGMENT_DONE message");
597
598 stop_time = gst_util_uint64_scale_int (musepackdec->segment.stop,
599 GST_SECOND, samplerate);
600
601 gst_element_post_message (GST_ELEMENT (musepackdec),
602 gst_message_new_segment_done (GST_OBJECT (musepackdec),
603 GST_FORMAT_TIME, stop_time));
Sebastian Dröge9c7d7492012-07-05 13:18:47 +0200604 gst_pad_push_event (musepackdec->srcpad,
605 gst_event_new_segment_done (GST_FORMAT_TIME, stop_time));
Tim-Philipp Mülleraf701d82006-04-20 18:02:07 +0000606
607 goto pause_task;
608 }
609
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000610 return;
611
Tim-Philipp Mülleraf701d82006-04-20 18:02:07 +0000612eos_and_pause:
613 {
614 GST_DEBUG_OBJECT (musepackdec, "sending EOS event");
615 gst_pad_push_event (musepackdec->srcpad, gst_event_new_eos ());
616 /* fall through to pause */
617 }
618
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000619pause_task:
620 {
621 GST_DEBUG_OBJECT (musepackdec, "Pausing task");
622 gst_pad_pause_task (sinkpad);
623 return;
624 }
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000625}
626
Jan Schmidt012dfb82005-09-05 17:20:29 +0000627static GstStateChangeReturn
628gst_musepackdec_change_state (GstElement * element, GstStateChange transition)
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000629{
630 GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (element);
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000631 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
632
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000633 switch (transition) {
634 case GST_STATE_CHANGE_READY_TO_PAUSED:
635 gst_segment_init (&musepackdec->segment, GST_FORMAT_DEFAULT);
636 gst_segment_set_last_stop (&musepackdec->segment, GST_FORMAT_DEFAULT, 0);
637 break;
638 default:
639 break;
640 }
641
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000642 if (GST_ELEMENT_CLASS (parent_class)->change_state)
643 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
644
Jan Schmidt012dfb82005-09-05 17:20:29 +0000645 switch (transition) {
Jan Schmidt012dfb82005-09-05 17:20:29 +0000646 case GST_STATE_CHANGE_PAUSED_TO_READY:
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000647 gst_segment_init (&musepackdec->segment, GST_FORMAT_UNDEFINED);
Tim-Philipp Müllerb92a9c42006-01-24 21:33:25 +0000648 musepackdec->offset = 0;
Tim-Philipp Müller13cd0b02006-03-06 13:13:44 +0000649 musepackdec->rate = 0;
650 musepackdec->bps = 0;
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000651 break;
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000652 default:
653 break;
654 }
655
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000656 return ret;
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000657
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000658}
659
660static gboolean
661plugin_init (GstPlugin * plugin)
662{
Sebastian Dröge01bcb092008-04-24 22:19:48 +0000663 GST_DEBUG_CATEGORY_INIT (musepackdec_debug, "musepackdec", 0, "mpc decoder");
664
Edgard Limaa5ef8ae2005-11-22 15:09:28 +0000665 return gst_element_register (plugin, "musepackdec",
Ronald S. Bultjef6b6f722005-01-29 01:28:34 +0000666 GST_RANK_PRIMARY, GST_TYPE_MUSEPACK_DEC);
Ronald S. Bultje868291e2004-11-07 18:30:06 +0000667}
668
669GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
670 GST_VERSION_MINOR,
Sebastian Drögecda192b2012-04-05 18:02:56 +0200671 musepack,
Thomas Vander Stichele60f80592006-04-01 10:09:11 +0000672 "Musepack decoder", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
673 GST_PACKAGE_ORIGIN)