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