2 * Copyright (C) <2009> Jan Schmidt <thaytan@noraisin.net>
3 * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
26 #include <gst/video/video.h>
27 #include <gst/audio/audio.h>
31 GST_DEBUG_CATEGORY_STATIC (rsn_dec_debug);
32 #define GST_CAT_DEFAULT rsn_dec_debug
34 static GstStateChangeReturn rsn_dec_change_state (GstElement * element,
35 GstStateChange transition);
37 static void rsn_dec_dispose (GObject * gobj);
38 static void cleanup_child (RsnDec * self);
40 static GstBinClass *rsn_dec_parent_class = NULL;
43 rsn_dec_class_init (RsnDecClass * klass)
45 GObjectClass *object_class = G_OBJECT_CLASS (klass);
46 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
48 GST_DEBUG_CATEGORY_INIT (rsn_dec_debug, "rsndec",
49 0, "Resin DVD stream decoder");
51 rsn_dec_parent_class = (GstBinClass *) g_type_class_peek_parent (klass);
52 object_class->dispose = rsn_dec_dispose;
54 element_class->change_state = GST_DEBUG_FUNCPTR (rsn_dec_change_state);
58 rsn_dec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
60 RsnDec *self = RSN_DEC (parent);
62 const GstStructure *s = gst_event_get_structure (event);
63 const gchar *name = (s ? gst_structure_get_name (s) : NULL);
65 if (name && g_str_equal (name, "application/x-gst-dvd"))
66 ret = gst_pad_push_event (GST_PAD_CAST (self->srcpad), event);
68 ret = self->sink_event_func (pad, parent, event);
74 rsn_dec_init (RsnDec * self, RsnDecClass * klass)
76 GstPadTemplate *templ;
79 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
80 g_assert (templ != NULL);
82 GST_GHOST_PAD_CAST (gst_ghost_pad_new_no_target_from_template ("sink",
84 self->sink_event_func = GST_PAD_EVENTFUNC (self->sinkpad);
85 gst_pad_set_event_function (GST_PAD_CAST (self->sinkpad),
86 GST_DEBUG_FUNCPTR (rsn_dec_sink_event));
88 templ = gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
89 g_assert (templ != NULL);
91 GST_GHOST_PAD_CAST (gst_ghost_pad_new_no_target_from_template ("src",
93 gst_element_add_pad (GST_ELEMENT (self), GST_PAD_CAST (self->sinkpad));
94 gst_element_add_pad (GST_ELEMENT (self), GST_PAD_CAST (self->srcpad));
98 rsn_dec_dispose (GObject * object)
100 RsnDec *self = (RsnDec *) object;
101 cleanup_child (self);
103 G_OBJECT_CLASS (rsn_dec_parent_class)->dispose (object);
107 rsn_dec_set_child (RsnDec * self, GstElement * new_child)
110 if (self->current_decoder) {
111 gst_ghost_pad_set_target (self->srcpad, NULL);
112 gst_ghost_pad_set_target (self->sinkpad, NULL);
113 gst_bin_remove ((GstBin *) self, self->current_decoder);
114 self->current_decoder = NULL;
117 if (new_child == NULL)
120 if (!gst_bin_add ((GstBin *) self, new_child))
123 child_pad = gst_element_get_static_pad (new_child, "sink");
124 if (child_pad == NULL) {
127 gst_ghost_pad_set_target (self->sinkpad, child_pad);
128 gst_object_unref (child_pad);
130 child_pad = gst_element_get_static_pad (new_child, "src");
131 if (child_pad == NULL) {
134 gst_ghost_pad_set_target (self->srcpad, child_pad);
135 gst_object_unref (child_pad);
137 GST_DEBUG_OBJECT (self, "Add child %" GST_PTR_FORMAT, new_child);
138 self->current_decoder = new_child;
140 gst_element_sync_state_with_parent (new_child);
146 cleanup_child (RsnDec * self)
148 GST_DEBUG_OBJECT (self, "Removing child element");
149 (void) rsn_dec_set_child (self, NULL);
154 GstCaps *desired_caps;
155 GstCaps *decoder_caps;
156 } RsnDecFactoryFilterCtx;
159 rsndec_factory_filter (GstPluginFeature * feature, RsnDecFactoryFilterCtx * ctx)
161 GstElementFactory *factory;
164 const GList *templates;
166 gboolean can_sink = FALSE;
168 /* we only care about element factories */
169 if (!GST_IS_ELEMENT_FACTORY (feature))
172 factory = GST_ELEMENT_FACTORY (feature);
175 gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
176 /* only decoders can play */
177 if (strstr (klass, "Decoder") == NULL)
180 /* only select elements with autoplugging rank */
181 rank = gst_plugin_feature_get_rank (feature);
182 if (rank < GST_RANK_MARGINAL)
185 /* See if the element has a sink pad that can possibly sink this caps */
187 /* get the templates from the element factory */
188 templates = gst_element_factory_get_static_pad_templates (factory);
189 for (walk = (GList *) templates; walk && !can_sink; walk = g_list_next (walk)) {
190 GstStaticPadTemplate *templ = walk->data;
192 /* we only care about the sink templates */
193 if (templ->direction == GST_PAD_SINK) {
197 /* try to intersect the caps with the caps of the template */
198 tmpl_caps = gst_static_caps_get (&templ->static_caps);
200 intersect = gst_caps_intersect (ctx->desired_caps, tmpl_caps);
201 gst_caps_unref (tmpl_caps);
203 /* check if the intersection is empty */
204 if (!gst_caps_is_empty (intersect)) {
205 /* non empty intersection, we can use this element */
207 ctx->decoder_caps = gst_caps_merge (ctx->decoder_caps, intersect);
209 gst_caps_unref (intersect);
214 GST_DEBUG ("Found decoder element %s (%s)",
215 gst_element_factory_get_metadata (factory,
216 GST_ELEMENT_METADATA_LONGNAME),
217 gst_plugin_feature_get_name (feature));
224 sort_by_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
227 const gchar *rname1, *rname2;
229 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
233 rname1 = gst_plugin_feature_get_name (f1);
234 rname2 = gst_plugin_feature_get_name (f2);
236 diff = strcmp (rname2, rname1);
242 _get_decoder_factories (gpointer arg)
244 GstElementClass *klass = arg;
246 GstPadTemplate *templ = gst_element_class_get_pad_template (klass,
248 RsnDecFactoryFilterCtx ctx = { NULL, };
251 GstRegistry *registry = gst_registry_get ();
253 ctx.desired_caps = gst_pad_template_get_caps (templ);
257 ("audio/x-raw,format=(string){ F32LE, F32BE, F64LE, F64BE }");
258 raw_audio = gst_caps_can_intersect (raw, ctx.desired_caps);
260 GstCaps *sub = gst_caps_subtract (ctx.desired_caps, raw);
261 ctx.desired_caps = sub;
263 gst_caps_ref (ctx.desired_caps);
265 gst_caps_unref (raw);
267 /* Set decoder caps to empty. Will be filled by the factory_filter */
268 ctx.decoder_caps = gst_caps_new_empty ();
269 GST_DEBUG ("Finding factories for caps: %" GST_PTR_FORMAT, ctx.desired_caps);
271 factories = gst_registry_feature_filter (registry,
272 (GstPluginFeatureFilter) rsndec_factory_filter, FALSE, &ctx);
274 /* If these are audio caps, we add audioconvert, which is not a decoder,
275 but allows raw audio to go through relatively unmolested - this will
276 come handy when we have to send placeholder silence to allow preroll
277 for those DVDs which have titles with no audio track. */
279 GstPluginFeature *feature;
280 GST_DEBUG ("These are audio caps, adding audioconvert");
282 gst_registry_find_feature (registry, "audioconvert",
283 GST_TYPE_ELEMENT_FACTORY);
285 factories = g_list_append (factories, feature);
287 GST_WARNING ("Could not find feature audioconvert");
291 factories = g_list_sort (factories, (GCompareFunc) sort_by_ranks);
293 GST_DEBUG ("Available decoder caps %" GST_PTR_FORMAT, ctx.decoder_caps);
294 gst_caps_unref (ctx.decoder_caps);
295 gst_caps_unref (ctx.desired_caps);
300 static GstStateChangeReturn
301 rsn_dec_change_state (GstElement * element, GstStateChange transition)
303 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
304 RsnDec *self = RSN_DEC (element);
305 RsnDecClass *klass = RSN_DEC_GET_CLASS (element);
307 switch (transition) {
308 case GST_STATE_CHANGE_NULL_TO_READY:{
309 GstElement *new_child;
310 const GList *decoder_factories;
312 new_child = gst_element_factory_make ("autoconvert", NULL);
313 decoder_factories = klass->get_decoder_factories (klass);
314 g_object_set (G_OBJECT (new_child), "factories", decoder_factories, NULL);
315 if (new_child == NULL || !rsn_dec_set_child (self, new_child))
316 ret = GST_STATE_CHANGE_FAILURE;
319 case GST_STATE_CHANGE_READY_TO_PAUSED:
325 if (ret == GST_STATE_CHANGE_FAILURE)
328 GST_ELEMENT_CLASS (rsn_dec_parent_class)->change_state (element,
330 if (ret == GST_STATE_CHANGE_FAILURE)
333 switch (transition) {
334 case GST_STATE_CHANGE_PAUSED_TO_READY:
336 case GST_STATE_CHANGE_READY_TO_NULL:
337 cleanup_child (self);
347 rsn_dec_get_type (void)
349 static gsize type = 0;
351 if (g_once_init_enter (&type)) {
353 static const GTypeInfo type_info = {
354 sizeof (RsnDecClass),
357 (GClassInitFunc) rsn_dec_class_init,
362 (GInstanceInitFunc) rsn_dec_init,
365 _type = g_type_register_static (GST_TYPE_BIN,
366 "RsnDec", &type_info, G_TYPE_FLAG_ABSTRACT);
367 g_once_init_leave (&type, _type);
372 /* Audio decoder subclass */
373 static GstStaticPadTemplate audio_sink_template =
374 GST_STATIC_PAD_TEMPLATE ("sink",
377 GST_STATIC_CAPS ("audio/mpeg,mpegversion=(int)1;"
378 "audio/x-private1-lpcm;"
379 "audio/x-private1-ac3;" "audio/ac3;" "audio/x-ac3;"
380 "audio/x-private1-dts; audio/x-raw,format=(string)"
381 GST_AUDIO_FORMATS_ALL)
384 static GstStaticPadTemplate audio_src_template = GST_STATIC_PAD_TEMPLATE ("src",
387 GST_STATIC_CAPS (GST_AUDIO_CAPS_MAKE (GST_AUDIO_FORMATS_ALL))
390 G_DEFINE_TYPE (RsnAudioDec, rsn_audiodec, RSN_TYPE_DEC);
393 rsn_audiodec_get_decoder_factories (RsnDecClass * klass)
395 static GOnce gonce = G_ONCE_INIT;
397 g_once (&gonce, _get_decoder_factories, klass);
399 return (const GList *) gonce.retval;
403 rsn_audiodec_class_init (RsnAudioDecClass * klass)
405 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
406 RsnDecClass *dec_class = RSN_DEC_CLASS (klass);
408 gst_element_class_add_static_pad_template (element_class,
409 &audio_src_template);
410 gst_element_class_add_static_pad_template (element_class,
411 &audio_sink_template);
413 gst_element_class_set_static_metadata (element_class, "RsnAudioDec",
415 "Resin DVD audio stream decoder", "Jan Schmidt <thaytan@noraisin.net>");
417 dec_class->get_decoder_factories = rsn_audiodec_get_decoder_factories;
421 rsn_audiodec_init (RsnAudioDec * self)
425 /* Video decoder subclass */
426 static GstStaticPadTemplate video_sink_template =
427 GST_STATIC_PAD_TEMPLATE ("sink",
430 GST_STATIC_CAPS ("video/mpeg, "
431 "mpegversion = (int) [ 1, 2 ], " "systemstream = (bool) FALSE")
434 static GstStaticPadTemplate video_src_template = GST_STATIC_PAD_TEMPLATE ("src",
437 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL))
440 G_DEFINE_TYPE (RsnVideoDec, rsn_videodec, RSN_TYPE_DEC);
443 rsn_videodec_get_decoder_factories (RsnDecClass * klass)
445 static GOnce gonce = G_ONCE_INIT;
447 g_once (&gonce, _get_decoder_factories, klass);
449 return (const GList *) gonce.retval;
453 rsn_videodec_class_init (RsnAudioDecClass * klass)
455 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
456 RsnDecClass *dec_class = RSN_DEC_CLASS (klass);
458 gst_element_class_add_static_pad_template (element_class,
459 &video_src_template);
460 gst_element_class_add_static_pad_template (element_class,
461 &video_sink_template);
463 gst_element_class_set_static_metadata (element_class, "RsnVideoDec",
465 "Resin DVD video stream decoder", "Jan Schmidt <thaytan@noraisin.net>");
467 dec_class->get_decoder_factories = rsn_videodec_get_decoder_factories;
471 rsn_videodec_init (RsnVideoDec * self)