_peer_get_caps() -> _peer_query_caps()
[platform/upstream/gstreamer.git] / gst / encoding / gstsmartencoder.c
1 /* GStreamer Smart Video Encoder element
2  * Copyright (C) <2010> Edward Hervey <bilboed@gmail.com>
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /* TODO:
21  * * Implement get_caps/set_caps (store/forward caps)
22  * * Adjust template caps to the formats we can support
23  **/
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <string.h>
30 #include "gstsmartencoder.h"
31
32 GST_DEBUG_CATEGORY_STATIC (smart_encoder_debug);
33 #define GST_CAT_DEFAULT smart_encoder_debug
34
35 /* FIXME : Update this with new caps */
36 /* WARNING : We can only allow formats with closed-GOP */
37 #define ALLOWED_CAPS "video/x-h263;video/x-intel-h263;"\
38   "video/mpeg,mpegversion=(int)1,systemstream=(boolean)false;"\
39   "video/mpeg,mpegversion=(int)2,systemstream=(boolean)false;"
40
41 static GstStaticPadTemplate src_template =
42 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS (ALLOWED_CAPS)
45     );
46
47 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
48     GST_PAD_SINK,
49     GST_PAD_ALWAYS,
50     GST_STATIC_CAPS (ALLOWED_CAPS)
51     );
52
53 static GQuark INTERNAL_ELEMENT;
54
55 /* GstSmartEncoder signals and args */
56 enum
57 {
58   /* FILL ME */
59   LAST_SIGNAL
60 };
61
62 enum
63 {
64   ARG_0
65       /* FILL ME */
66 };
67
68 static void
69 _do_init (void)
70 {
71   INTERNAL_ELEMENT = g_quark_from_string ("internal-element");
72 };
73
74 G_DEFINE_TYPE_EXTENDED (GstSmartEncoder, gst_smart_encoder, GST_TYPE_ELEMENT, 0,
75     _do_init ());
76
77 static void gst_smart_encoder_dispose (GObject * object);
78
79 static gboolean setup_recoder_pipeline (GstSmartEncoder * smart_encoder);
80
81 static GstFlowReturn gst_smart_encoder_chain (GstPad * pad, GstBuffer * buf);
82 static gboolean smart_encoder_sink_event (GstPad * pad, GstEvent * event);
83 static gboolean smart_encoder_sink_query (GstPad * pad, GstQuery * query);
84 static GstCaps *smart_encoder_sink_getcaps (GstPad * pad, GstCaps * filter);
85 static GstStateChangeReturn
86 gst_smart_encoder_change_state (GstElement * element,
87     GstStateChange transition);
88
89 static void
90 gst_smart_encoder_class_init (GstSmartEncoderClass * klass)
91 {
92   GObjectClass *gobject_class;
93   GstElementClass *element_class;
94
95   element_class = (GstElementClass *) klass;
96   gobject_class = G_OBJECT_CLASS (klass);
97
98   gst_smart_encoder_parent_class = g_type_class_peek_parent (klass);
99
100   gst_element_class_add_pad_template (element_class,
101       gst_static_pad_template_get (&src_template));
102   gst_element_class_add_pad_template (element_class,
103       gst_static_pad_template_get (&sink_template));
104
105   gst_element_class_set_details_simple (element_class, "Smart Video Encoder",
106       "Codec/Recoder/Video",
107       "Re-encodes portions of Video that lay on segment boundaries",
108       "Edward Hervey <bilboed@gmail.com>");
109
110   gobject_class->dispose = (GObjectFinalizeFunc) (gst_smart_encoder_dispose);
111   element_class->change_state = gst_smart_encoder_change_state;
112
113   GST_DEBUG_CATEGORY_INIT (smart_encoder_debug, "smartencoder", 0,
114       "Smart Encoder");
115 }
116
117 static void
118 smart_encoder_reset (GstSmartEncoder * smart_encoder)
119 {
120   gst_segment_init (smart_encoder->segment, GST_FORMAT_UNDEFINED);
121
122   if (smart_encoder->encoder) {
123     /* Clean up/remove elements */
124     gst_element_set_state (smart_encoder->encoder, GST_STATE_NULL);
125     gst_element_set_state (smart_encoder->decoder, GST_STATE_NULL);
126     gst_element_set_bus (smart_encoder->encoder, NULL);
127     gst_element_set_bus (smart_encoder->decoder, NULL);
128     gst_pad_set_active (smart_encoder->internal_srcpad, FALSE);
129     gst_pad_set_active (smart_encoder->internal_sinkpad, FALSE);
130     gst_object_unref (smart_encoder->encoder);
131     gst_object_unref (smart_encoder->decoder);
132     gst_object_unref (smart_encoder->internal_srcpad);
133     gst_object_unref (smart_encoder->internal_sinkpad);
134
135     smart_encoder->encoder = NULL;
136     smart_encoder->decoder = NULL;
137     smart_encoder->internal_sinkpad = NULL;
138     smart_encoder->internal_srcpad = NULL;
139   }
140
141   if (smart_encoder->newsegment) {
142     gst_event_unref (smart_encoder->newsegment);
143     smart_encoder->newsegment = NULL;
144   }
145 }
146
147
148 static void
149 gst_smart_encoder_init (GstSmartEncoder * smart_encoder)
150 {
151   smart_encoder->sinkpad =
152       gst_pad_new_from_static_template (&sink_template, "sink");
153   gst_pad_set_chain_function (smart_encoder->sinkpad, gst_smart_encoder_chain);
154   gst_pad_set_event_function (smart_encoder->sinkpad, smart_encoder_sink_event);
155   gst_pad_set_query_function (smart_encoder->sinkpad, smart_encoder_sink_query);
156   gst_element_add_pad (GST_ELEMENT (smart_encoder), smart_encoder->sinkpad);
157
158   smart_encoder->srcpad =
159       gst_pad_new_from_static_template (&src_template, "src");
160   gst_pad_use_fixed_caps (smart_encoder->srcpad);
161   gst_element_add_pad (GST_ELEMENT (smart_encoder), smart_encoder->srcpad);
162
163   smart_encoder->segment = gst_segment_new ();
164
165   smart_encoder_reset (smart_encoder);
166 }
167
168 void
169 gst_smart_encoder_dispose (GObject * object)
170 {
171   GstSmartEncoder *smart_encoder = (GstSmartEncoder *) object;
172
173   if (smart_encoder->segment)
174     gst_segment_free (smart_encoder->segment);
175   smart_encoder->segment = NULL;
176   if (smart_encoder->available_caps)
177     gst_caps_unref (smart_encoder->available_caps);
178   smart_encoder->available_caps = NULL;
179   G_OBJECT_CLASS (gst_smart_encoder_parent_class)->dispose (object);
180 }
181
182 static GstFlowReturn
183 gst_smart_encoder_reencode_gop (GstSmartEncoder * smart_encoder)
184 {
185   GstFlowReturn res = GST_FLOW_OK;
186   GList *tmp;
187
188   if (smart_encoder->encoder == NULL) {
189     if (!setup_recoder_pipeline (smart_encoder))
190       return GST_FLOW_ERROR;
191   }
192
193   /* Activate elements */
194   /* Set elements to PAUSED */
195   gst_element_set_state (smart_encoder->encoder, GST_STATE_PAUSED);
196   gst_element_set_state (smart_encoder->decoder, GST_STATE_PAUSED);
197
198   GST_INFO ("Pushing Flush start/stop to clean decoder/encoder");
199   gst_pad_push_event (smart_encoder->internal_srcpad,
200       gst_event_new_flush_start ());
201   gst_pad_push_event (smart_encoder->internal_srcpad,
202       gst_event_new_flush_stop (TRUE));
203
204   /* push newsegment */
205   GST_INFO ("Pushing newsegment %" GST_PTR_FORMAT, smart_encoder->newsegment);
206   gst_pad_push_event (smart_encoder->internal_srcpad,
207       gst_event_ref (smart_encoder->newsegment));
208
209   /* Push buffers through our pads */
210   GST_DEBUG ("Pushing pending buffers");
211
212   for (tmp = smart_encoder->pending_gop; tmp; tmp = tmp->next) {
213     GstBuffer *buf = (GstBuffer *) tmp->data;
214
215     res = gst_pad_push (smart_encoder->internal_srcpad, buf);
216     if (G_UNLIKELY (res != GST_FLOW_OK))
217       break;
218   }
219
220   if (G_UNLIKELY (res != GST_FLOW_OK)) {
221     GST_WARNING ("Error pushing pending buffers : %s", gst_flow_get_name (res));
222     /* Remove pending bfufers */
223     for (tmp = smart_encoder->pending_gop; tmp; tmp = tmp->next) {
224       gst_buffer_unref ((GstBuffer *) tmp->data);
225     }
226   } else {
227     GST_INFO ("Pushing out EOS to flush out decoder/encoder");
228     gst_pad_push_event (smart_encoder->internal_srcpad, gst_event_new_eos ());
229   }
230
231   /* Activate elements */
232   /* Set elements to PAUSED */
233   gst_element_set_state (smart_encoder->encoder, GST_STATE_NULL);
234   gst_element_set_state (smart_encoder->decoder, GST_STATE_NULL);
235
236   g_list_free (smart_encoder->pending_gop);
237   smart_encoder->pending_gop = NULL;
238
239   return res;
240 }
241
242 static GstFlowReturn
243 gst_smart_encoder_push_pending_gop (GstSmartEncoder * smart_encoder)
244 {
245   guint64 cstart, cstop;
246   GList *tmp;
247   GstFlowReturn res = GST_FLOW_OK;
248
249   GST_DEBUG ("Pushing pending GOP (%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
250       ")", GST_TIME_ARGS (smart_encoder->gop_start),
251       GST_TIME_ARGS (smart_encoder->gop_stop));
252
253   /* If GOP is entirely within segment, just push downstream */
254   if (gst_segment_clip (smart_encoder->segment, GST_FORMAT_TIME,
255           smart_encoder->gop_start, smart_encoder->gop_stop, &cstart, &cstop)) {
256     if ((cstart != smart_encoder->gop_start)
257         || (cstop != smart_encoder->gop_stop)) {
258       GST_DEBUG ("GOP needs to be re-encoded from %" GST_TIME_FORMAT " to %"
259           GST_TIME_FORMAT, GST_TIME_ARGS (cstart), GST_TIME_ARGS (cstop));
260       res = gst_smart_encoder_reencode_gop (smart_encoder);
261     } else {
262       /* The whole GOP is within the segment, push all pending buffers downstream */
263       GST_DEBUG ("GOP doesn't need to be modified, pushing downstream");
264       for (tmp = smart_encoder->pending_gop; tmp; tmp = tmp->next) {
265         GstBuffer *buf = (GstBuffer *) tmp->data;
266         res = gst_pad_push (smart_encoder->srcpad, buf);
267         if (G_UNLIKELY (res != GST_FLOW_OK))
268           break;
269       }
270     }
271   } else {
272     /* The whole GOP is outside the segment, there's most likely
273      * a bug somewhere. */
274     GST_WARNING
275         ("GOP is entirely outside of the segment, upstream gave us too much data");
276     for (tmp = smart_encoder->pending_gop; tmp; tmp = tmp->next) {
277       gst_buffer_unref ((GstBuffer *) tmp->data);
278     }
279   }
280
281   if (smart_encoder->pending_gop) {
282     g_list_free (smart_encoder->pending_gop);
283     smart_encoder->pending_gop = NULL;
284   }
285   smart_encoder->gop_start = GST_CLOCK_TIME_NONE;
286   smart_encoder->gop_stop = GST_CLOCK_TIME_NONE;
287
288   return res;
289 }
290
291 static GstFlowReturn
292 gst_smart_encoder_chain (GstPad * pad, GstBuffer * buf)
293 {
294   GstSmartEncoder *smart_encoder;
295   GstFlowReturn res = GST_FLOW_OK;
296   gboolean discont, keyframe;
297
298   smart_encoder = GST_SMART_ENCODER (gst_object_get_parent (GST_OBJECT (pad)));
299
300   discont = GST_BUFFER_IS_DISCONT (buf);
301   keyframe = !GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
302
303   GST_DEBUG ("New buffer %s %s %" GST_TIME_FORMAT,
304       discont ? "discont" : "",
305       keyframe ? "keyframe" : "", GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
306
307   if (keyframe) {
308     GST_DEBUG ("Got a keyframe");
309
310     /* If there's a pending GOP, flush it out */
311     if (smart_encoder->pending_gop) {
312       /* Mark gop_stop */
313       smart_encoder->gop_stop = GST_BUFFER_TIMESTAMP (buf);
314
315       /* flush pending */
316       res = gst_smart_encoder_push_pending_gop (smart_encoder);
317       if (G_UNLIKELY (res != GST_FLOW_OK))
318         goto beach;
319     }
320
321     /* Mark gop_start for new gop */
322     smart_encoder->gop_start = GST_BUFFER_TIMESTAMP (buf);
323   }
324
325   /* Store buffer */
326   smart_encoder->pending_gop = g_list_append (smart_encoder->pending_gop, buf);
327   /* Update GOP stop position */
328   if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
329     smart_encoder->gop_stop = GST_BUFFER_TIMESTAMP (buf);
330     if (GST_BUFFER_DURATION_IS_VALID (buf))
331       smart_encoder->gop_stop += GST_BUFFER_DURATION (buf);
332   }
333
334   GST_DEBUG ("Buffer stored , Current GOP : %" GST_TIME_FORMAT " -- %"
335       GST_TIME_FORMAT, GST_TIME_ARGS (smart_encoder->gop_start),
336       GST_TIME_ARGS (smart_encoder->gop_stop));
337
338 beach:
339   gst_object_unref (smart_encoder);
340   return res;
341 }
342
343 static gboolean
344 smart_encoder_sink_event (GstPad * pad, GstEvent * event)
345 {
346   gboolean res = TRUE;
347   GstSmartEncoder *smart_encoder = GST_SMART_ENCODER (gst_pad_get_parent (pad));
348
349   switch (GST_EVENT_TYPE (event)) {
350     case GST_EVENT_FLUSH_STOP:
351       smart_encoder_reset (smart_encoder);
352       break;
353     case GST_EVENT_SEGMENT:
354     {
355       gst_event_copy_segment (event, smart_encoder->segment);
356
357       GST_DEBUG_OBJECT (smart_encoder, "segment: %" GST_SEGMENT_FORMAT,
358           smart_encoder->segment);
359       if (smart_encoder->segment->format != GST_FORMAT_TIME)
360         GST_ERROR
361             ("smart_encoder can not handle streams not specified in GST_FORMAT_TIME");
362
363       /* And keep a copy for further usage */
364       if (smart_encoder->newsegment)
365         gst_event_unref (smart_encoder->newsegment);
366       smart_encoder->newsegment = gst_event_ref (event);
367     }
368       break;
369     case GST_EVENT_EOS:
370       GST_DEBUG ("Eos, flushing remaining data");
371       gst_smart_encoder_push_pending_gop (smart_encoder);
372       break;
373     default:
374       break;
375   }
376
377   res = gst_pad_push_event (smart_encoder->srcpad, event);
378
379   gst_object_unref (smart_encoder);
380   return res;
381 }
382
383 static GstCaps *
384 smart_encoder_sink_getcaps (GstPad * pad, GstCaps * filter)
385 {
386   GstCaps *peer, *tmpl, *res;
387   GstSmartEncoder *smart_encoder = GST_SMART_ENCODER (gst_pad_get_parent (pad));
388
389   /* Use computed caps */
390   if (smart_encoder->available_caps)
391     tmpl = gst_caps_ref (smart_encoder->available_caps);
392   else
393     tmpl = gst_static_pad_template_get_caps (&src_template);
394
395   /* Try getting it from downstream */
396   peer = gst_pad_peer_query_caps (smart_encoder->srcpad, tmpl);
397
398   if (peer == NULL) {
399     res = tmpl;
400   } else {
401     res = peer;
402     gst_caps_unref (tmpl);
403   }
404
405   gst_object_unref (smart_encoder);
406   return res;
407 }
408
409 static gboolean
410 smart_encoder_sink_query (GstPad * pad, GstQuery * query)
411 {
412   gboolean res;
413
414   switch (GST_QUERY_TYPE (query)) {
415     case GST_QUERY_CAPS:
416     {
417       GstCaps *filter, *caps;
418
419       gst_query_parse_caps (query, &filter);
420       caps = smart_encoder_sink_getcaps (pad, filter);
421       gst_query_set_caps_result (query, caps);
422       gst_caps_unref (caps);
423       res = TRUE;
424       break;
425     }
426     default:
427       res = gst_pad_query_default (pad, query);
428       break;
429   }
430   return res;
431 }
432
433 /*****************************************
434  *    Internal encoder/decoder pipeline  *
435  ******************************************/
436
437 static GstElementFactory *
438 get_decoder_factory (GstCaps * caps)
439 {
440   GstElementFactory *fact = NULL;
441   GList *decoders, *tmp;
442
443   tmp =
444       gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECODER,
445       GST_RANK_MARGINAL);
446   decoders = gst_element_factory_list_filter (tmp, caps, GST_PAD_SINK, FALSE);
447   gst_plugin_feature_list_free (tmp);
448
449   for (tmp = decoders; tmp; tmp = tmp->next) {
450     /* We just pick the first one */
451     fact = (GstElementFactory *) tmp->data;
452     gst_object_ref (fact);
453     break;
454   }
455
456   gst_plugin_feature_list_free (decoders);
457
458   return fact;
459 }
460
461 static GstElementFactory *
462 get_encoder_factory (GstCaps * caps)
463 {
464   GstElementFactory *fact = NULL;
465   GList *encoders, *tmp;
466
467   tmp =
468       gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_ENCODER,
469       GST_RANK_MARGINAL);
470   encoders = gst_element_factory_list_filter (tmp, caps, GST_PAD_SRC, FALSE);
471   gst_plugin_feature_list_free (tmp);
472
473   for (tmp = encoders; tmp; tmp = tmp->next) {
474     /* We just pick the first one */
475     fact = (GstElementFactory *) tmp->data;
476     gst_object_ref (fact);
477     break;
478   }
479
480   gst_plugin_feature_list_free (encoders);
481
482   return fact;
483 }
484
485 static GstElement *
486 get_decoder (GstCaps * caps)
487 {
488   GstElementFactory *fact = get_decoder_factory (caps);
489   GstElement *res = NULL;
490
491   if (fact) {
492     res = gst_element_factory_create (fact, "internal-decoder");
493     gst_object_unref (fact);
494   }
495   return res;
496 }
497
498 static GstElement *
499 get_encoder (GstCaps * caps)
500 {
501   GstElementFactory *fact = get_encoder_factory (caps);
502   GstElement *res = NULL;
503
504   if (fact) {
505     res = gst_element_factory_create (fact, "internal-encoder");
506     gst_object_unref (fact);
507   }
508   return res;
509 }
510
511 static GstFlowReturn
512 internal_chain (GstPad * pad, GstBuffer * buf)
513 {
514   GstSmartEncoder *smart_encoder =
515       g_object_get_qdata ((GObject *) pad, INTERNAL_ELEMENT);
516
517   return gst_pad_push (smart_encoder->srcpad, buf);
518 }
519
520 static gboolean
521 setup_recoder_pipeline (GstSmartEncoder * smart_encoder)
522 {
523   GstPad *tmppad;
524   GstCaps *caps;
525
526   /* Fast path */
527   if (G_UNLIKELY (smart_encoder->encoder))
528     return TRUE;
529
530   GST_DEBUG ("Creating internal decoder and encoder");
531
532   /* Create decoder/encoder */
533   caps = gst_pad_get_current_caps (smart_encoder->sinkpad);
534   smart_encoder->decoder = get_decoder (caps);
535   if (G_UNLIKELY (smart_encoder->decoder == NULL))
536     goto no_decoder;
537   gst_caps_unref (caps);
538   gst_element_set_bus (smart_encoder->decoder, GST_ELEMENT_BUS (smart_encoder));
539
540   caps = gst_pad_get_current_caps (smart_encoder->sinkpad);
541   smart_encoder->encoder = get_encoder (caps);
542   if (G_UNLIKELY (smart_encoder->encoder == NULL))
543     goto no_encoder;
544   gst_caps_unref (caps);
545   gst_element_set_bus (smart_encoder->encoder, GST_ELEMENT_BUS (smart_encoder));
546
547   GST_DEBUG ("Creating internal pads");
548
549   /* Create internal pads */
550
551   /* Source pad which we'll use to feed data to decoders */
552   smart_encoder->internal_srcpad = gst_pad_new ("internal_src", GST_PAD_SRC);
553   g_object_set_qdata ((GObject *) smart_encoder->internal_srcpad,
554       INTERNAL_ELEMENT, smart_encoder);
555   gst_pad_set_active (smart_encoder->internal_srcpad, TRUE);
556
557   /* Sink pad which will get the buffers from the encoder.
558    * Note: We don't need an event function since we'll be discarding all
559    * of them. */
560   smart_encoder->internal_sinkpad = gst_pad_new ("internal_sink", GST_PAD_SINK);
561   g_object_set_qdata ((GObject *) smart_encoder->internal_sinkpad,
562       INTERNAL_ELEMENT, smart_encoder);
563   gst_pad_set_chain_function (smart_encoder->internal_sinkpad, internal_chain);
564   gst_pad_set_active (smart_encoder->internal_sinkpad, TRUE);
565
566   GST_DEBUG ("Linking pads to elements");
567
568   /* Link everything */
569   tmppad = gst_element_get_static_pad (smart_encoder->encoder, "src");
570   if (GST_PAD_LINK_FAILED (gst_pad_link (tmppad,
571               smart_encoder->internal_sinkpad)))
572     goto sinkpad_link_fail;
573   gst_object_unref (tmppad);
574
575   if (!gst_element_link (smart_encoder->decoder, smart_encoder->encoder))
576     goto encoder_decoder_link_fail;
577
578   tmppad = gst_element_get_static_pad (smart_encoder->decoder, "sink");
579   if (GST_PAD_LINK_FAILED (gst_pad_link (smart_encoder->internal_srcpad,
580               tmppad)))
581     goto srcpad_link_fail;
582   gst_object_unref (tmppad);
583
584   GST_DEBUG ("Done creating internal elements/pads");
585
586   return TRUE;
587
588 no_decoder:
589   {
590     GST_WARNING ("Couldn't find a decoder for %" GST_PTR_FORMAT, caps);
591     gst_caps_unref (caps);
592     return FALSE;
593   }
594
595 no_encoder:
596   {
597     GST_WARNING ("Couldn't find an encoder for %" GST_PTR_FORMAT, caps);
598     gst_caps_unref (caps);
599     return FALSE;
600   }
601
602 srcpad_link_fail:
603   {
604     gst_object_unref (tmppad);
605     GST_WARNING ("Couldn't link internal srcpad to decoder");
606     return FALSE;
607   }
608
609 sinkpad_link_fail:
610   {
611     gst_object_unref (tmppad);
612     GST_WARNING ("Couldn't link encoder to internal sinkpad");
613     return FALSE;
614   }
615
616 encoder_decoder_link_fail:
617   {
618     GST_WARNING ("Couldn't link decoder to encoder");
619     return FALSE;
620   }
621 }
622
623 static GstStateChangeReturn
624 gst_smart_encoder_find_elements (GstSmartEncoder * smart_encoder)
625 {
626   guint i, n;
627   GstCaps *tmpl, *st, *res;
628   GstElementFactory *dec, *enc;
629   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
630
631   if (G_UNLIKELY (smart_encoder->available_caps))
632     goto beach;
633
634   /* Iterate over all pad template caps and see if we have both an
635    * encoder and a decoder for those media types */
636   tmpl = gst_static_pad_template_get_caps (&src_template);
637   res = gst_caps_new_empty ();
638   n = gst_caps_get_size (tmpl);
639
640   for (i = 0; i < n; i++) {
641     st = gst_caps_copy_nth (tmpl, i);
642     GST_DEBUG_OBJECT (smart_encoder,
643         "Checking for available decoder and encoder for %" GST_PTR_FORMAT, st);
644     if (!(dec = get_decoder_factory (st))) {
645       gst_caps_unref (st);
646       continue;
647     }
648     gst_object_unref (dec);
649     if (!(enc = get_encoder_factory (st))) {
650       gst_caps_unref (st);
651       continue;
652     }
653     gst_object_unref (enc);
654     GST_DEBUG_OBJECT (smart_encoder, "OK");
655     gst_caps_append (res, st);
656   }
657
658   gst_caps_unref (tmpl);
659
660   if (gst_caps_is_empty (res))
661     ret = GST_STATE_CHANGE_FAILURE;
662   else
663     smart_encoder->available_caps = res;
664
665   GST_DEBUG_OBJECT (smart_encoder, "Done, available_caps:%" GST_PTR_FORMAT,
666       smart_encoder->available_caps);
667
668 beach:
669   return ret;
670 }
671
672 /******************************************
673  *    GstElement vmethod implementations  *
674  ******************************************/
675
676 static GstStateChangeReturn
677 gst_smart_encoder_change_state (GstElement * element, GstStateChange transition)
678 {
679   GstSmartEncoder *smart_encoder;
680   GstStateChangeReturn ret;
681
682   g_return_val_if_fail (GST_IS_SMART_ENCODER (element),
683       GST_STATE_CHANGE_FAILURE);
684
685   smart_encoder = GST_SMART_ENCODER (element);
686
687   switch (transition) {
688     case GST_STATE_CHANGE_NULL_TO_READY:
689       /* Figure out which elements are available  */
690       if ((ret =
691               gst_smart_encoder_find_elements (smart_encoder)) ==
692           GST_STATE_CHANGE_FAILURE)
693         goto beach;
694       break;
695     default:
696       break;
697   }
698
699   ret =
700       GST_ELEMENT_CLASS (gst_smart_encoder_parent_class)->change_state (element,
701       transition);
702
703   switch (transition) {
704     case GST_STATE_CHANGE_PAUSED_TO_READY:
705       smart_encoder_reset (smart_encoder);
706       break;
707     default:
708       break;
709   }
710
711 beach:
712   return ret;
713 }