pluginutils: improve automatic display type selection.
[profile/ivi/gstreamer-vaapi.git] / gst / vaapi / gstvaapidecode.c
1 /*
2  *  gstvaapidecode.c - VA-API video decoder
3  *
4  *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5  *  Copyright (C) 2011-2012 Intel Corporation
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public License
9  *  as published by the Free Software Foundation; either version 2.1
10  *  of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free
19  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  *  Boston, MA 02110-1301 USA
21 */
22
23 /**
24  * SECTION:gstvaapidecode
25  * @short_description: A VA-API based video decoder
26  *
27  * vaapidecode decodes from raw bitstreams to surfaces suitable for
28  * the vaapisink element.
29  */
30
31 #include "config.h"
32
33 #include <gst/vaapi/gstvaapidisplay.h>
34 #include <gst/vaapi/gstvaapivideosink.h>
35 #include <gst/vaapi/gstvaapivideobuffer.h>
36 #include <gst/video/videocontext.h>
37
38 #if USE_GLX
39 #include <gst/vaapi/gstvaapivideobuffer_glx.h>
40 #define gst_vaapi_video_buffer_new(display) \
41     gst_vaapi_video_buffer_glx_new(GST_VAAPI_DISPLAY_GLX(display))
42 #endif
43
44 #include "gstvaapidecode.h"
45 #include "gstvaapipluginutil.h"
46
47 #include <gst/vaapi/gstvaapidecoder_h264.h>
48 #include <gst/vaapi/gstvaapidecoder_jpeg.h>
49 #include <gst/vaapi/gstvaapidecoder_mpeg2.h>
50 #include <gst/vaapi/gstvaapidecoder_mpeg4.h>
51 #include <gst/vaapi/gstvaapidecoder_vc1.h>
52
53 #define GST_PLUGIN_NAME "vaapidecode"
54 #define GST_PLUGIN_DESC "A VA-API based video decoder"
55
56 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapidecode);
57 #define GST_CAT_DEFAULT gst_debug_vaapidecode
58
59 /* ElementFactory information */
60 static const GstElementDetails gst_vaapidecode_details =
61     GST_ELEMENT_DETAILS(
62         "VA-API decoder",
63         "Codec/Decoder/Video",
64         GST_PLUGIN_DESC,
65         "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");
66
67 /* Default templates */
68 #define GST_CAPS_CODEC(CODEC) CODEC "; "
69
70 static const char gst_vaapidecode_sink_caps_str[] =
71     GST_CAPS_CODEC("video/mpeg, mpegversion=2, systemstream=(boolean)false")
72     GST_CAPS_CODEC("video/mpeg, mpegversion=4")
73     GST_CAPS_CODEC("video/x-divx")
74     GST_CAPS_CODEC("video/x-xvid")
75     GST_CAPS_CODEC("video/x-h263")
76     GST_CAPS_CODEC("video/x-h264")
77     GST_CAPS_CODEC("video/x-wmv")
78     GST_CAPS_CODEC("image/jpeg")
79     ;
80
81 static const char gst_vaapidecode_src_caps_str[] =
82     GST_VAAPI_SURFACE_CAPS;
83
84 static GstStaticPadTemplate gst_vaapidecode_sink_factory =
85     GST_STATIC_PAD_TEMPLATE(
86         "sink",
87         GST_PAD_SINK,
88         GST_PAD_ALWAYS,
89         GST_STATIC_CAPS(gst_vaapidecode_sink_caps_str));
90
91 static GstStaticPadTemplate gst_vaapidecode_src_factory =
92     GST_STATIC_PAD_TEMPLATE(
93         "src",
94         GST_PAD_SRC,
95         GST_PAD_ALWAYS,
96         GST_STATIC_CAPS(gst_vaapidecode_src_caps_str));
97
98 static void
99 gst_vaapidecode_implements_iface_init(GstImplementsInterfaceClass *iface);
100
101 static void
102 gst_video_context_interface_init(GstVideoContextInterface *iface);
103
104 #define GstVideoContextClass GstVideoContextInterface
105 G_DEFINE_TYPE_WITH_CODE(
106     GstVaapiDecode,
107     gst_vaapidecode,
108     GST_TYPE_ELEMENT,
109     G_IMPLEMENT_INTERFACE(GST_TYPE_IMPLEMENTS_INTERFACE,
110                           gst_vaapidecode_implements_iface_init);
111     G_IMPLEMENT_INTERFACE(GST_TYPE_VIDEO_CONTEXT,
112                           gst_video_context_interface_init));
113
114 static gboolean
115 gst_vaapidecode_update_src_caps(GstVaapiDecode *decode, GstCaps *caps);
116
117 static void
118 gst_vaapi_decoder_notify_caps(GObject *obj, GParamSpec *pspec, void *user_data)
119 {
120     GstVaapiDecode * const decode = GST_VAAPIDECODE(user_data);
121     GstCaps *caps;
122
123     g_assert(decode->decoder == GST_VAAPI_DECODER(obj));
124
125     caps = gst_vaapi_decoder_get_caps(decode->decoder);
126     gst_vaapidecode_update_src_caps(decode, caps);
127 }
128
129 static inline gboolean
130 gst_vaapidecode_update_sink_caps(GstVaapiDecode *decode, GstCaps *caps)
131 {
132     if (decode->sinkpad_caps)
133         gst_caps_unref(decode->sinkpad_caps);
134     decode->sinkpad_caps = gst_caps_ref(caps);
135     return TRUE;
136 }
137
138 static gboolean
139 gst_vaapidecode_update_src_caps(GstVaapiDecode *decode, GstCaps *caps)
140 {
141     GstCaps *other_caps;
142     GstStructure *structure;
143     const GValue *v_width, *v_height, *v_framerate, *v_par, *v_interlaced;
144     gboolean success;
145
146     if (!decode->srcpad_caps) {
147         decode->srcpad_caps = gst_caps_from_string(GST_VAAPI_SURFACE_CAPS_NAME);
148         if (!decode->srcpad_caps)
149             return FALSE;
150     }
151
152     structure    = gst_caps_get_structure(caps, 0);
153     v_width      = gst_structure_get_value(structure, "width");
154     v_height     = gst_structure_get_value(structure, "height");
155     v_framerate  = gst_structure_get_value(structure, "framerate");
156     v_par        = gst_structure_get_value(structure, "pixel-aspect-ratio");
157     v_interlaced = gst_structure_get_value(structure, "interlaced");
158
159     structure = gst_caps_get_structure(decode->srcpad_caps, 0);
160     if (v_width && v_height) {
161         gst_structure_set_value(structure, "width", v_width);
162         gst_structure_set_value(structure, "height", v_height);
163     }
164     if (v_framerate)
165         gst_structure_set_value(structure, "framerate", v_framerate);
166     if (v_par)
167         gst_structure_set_value(structure, "pixel-aspect-ratio", v_par);
168     if (v_interlaced)
169         gst_structure_set_value(structure, "interlaced", v_interlaced);
170
171     gst_structure_set(structure, "type", G_TYPE_STRING, "vaapi", NULL);
172     gst_structure_set(structure, "opengl", G_TYPE_BOOLEAN, USE_GLX, NULL);
173
174     other_caps = gst_caps_copy(decode->srcpad_caps);
175     success = gst_pad_set_caps(decode->srcpad, other_caps);
176     gst_caps_unref(other_caps);
177     return success;
178 }
179
180 static void
181 gst_vaapidecode_release(GstVaapiDecode *decode, GObject *dead_object)
182 {
183     g_mutex_lock(decode->decoder_mutex);
184     g_cond_signal(decode->decoder_ready);
185     g_mutex_unlock(decode->decoder_mutex);
186 }
187
188 static GstFlowReturn
189 gst_vaapidecode_step(GstVaapiDecode *decode)
190 {
191     GstVaapiSurfaceProxy *proxy;
192     GstVaapiDecoderStatus status;
193     GstBuffer *buffer;
194     GstFlowReturn ret;
195     guint tries;
196
197     for (;;) {
198         tries = 0;
199     again:
200         proxy = gst_vaapi_decoder_get_surface(decode->decoder, &status);
201         if (!proxy) {
202             if (status == GST_VAAPI_DECODER_STATUS_ERROR_NO_SURFACE) {
203                 /* Wait for a VA surface to be displayed and free'd */
204                 if (++tries > 100)
205                     goto error_decode_timeout;
206                 GTimeVal timeout;
207                 g_get_current_time(&timeout);
208                 g_time_val_add(&timeout, 10000); /* 10 ms each step */
209                 g_mutex_lock(decode->decoder_mutex);
210                 g_cond_timed_wait(
211                     decode->decoder_ready,
212                     decode->decoder_mutex,
213                     &timeout
214                 );
215                 g_mutex_unlock(decode->decoder_mutex);
216                 goto again;
217             }
218             if (status != GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA)
219                 goto error_decode;
220             /* More data is needed */
221             break;
222         }
223
224         g_object_weak_ref(
225             G_OBJECT(proxy),
226             (GWeakNotify)gst_vaapidecode_release,
227             decode
228         );
229
230         buffer = gst_vaapi_video_buffer_new(decode->display);
231         if (!buffer)
232             goto error_create_buffer;
233
234         GST_BUFFER_TIMESTAMP(buffer) = GST_VAAPI_SURFACE_PROXY_TIMESTAMP(proxy);
235         gst_buffer_set_caps(buffer, GST_PAD_CAPS(decode->srcpad));
236
237         if (GST_VAAPI_SURFACE_PROXY_TFF(proxy))
238             GST_BUFFER_FLAG_SET(buffer, GST_VIDEO_BUFFER_TFF);
239
240         gst_vaapi_video_buffer_set_surface_proxy(
241             GST_VAAPI_VIDEO_BUFFER(buffer),
242             proxy
243         );
244
245         ret = gst_pad_push(decode->srcpad, buffer);
246         if (ret != GST_FLOW_OK)
247             goto error_commit_buffer;
248
249         g_object_unref(proxy);
250     }
251     return GST_FLOW_OK;
252
253     /* ERRORS */
254 error_decode_timeout:
255     {
256         GST_DEBUG("decode timeout. Decoder required a VA surface but none "
257                   "got available within one second");
258         return GST_FLOW_UNEXPECTED;
259     }
260 error_decode:
261     {
262         GST_DEBUG("decode error %d", status);
263         switch (status) {
264         case GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CODEC:
265         case GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE:
266         case GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CHROMA_FORMAT:
267             ret = GST_FLOW_NOT_SUPPORTED;
268             break;
269         default:
270             ret = GST_FLOW_UNEXPECTED;
271             break;
272         }
273         return ret;
274     }
275 error_create_buffer:
276     {
277         const GstVaapiID surface_id =
278             gst_vaapi_surface_get_id(GST_VAAPI_SURFACE_PROXY_SURFACE(proxy));
279
280         GST_DEBUG("video sink failed to create video buffer for proxy'ed "
281                   "surface %" GST_VAAPI_ID_FORMAT " (error %d)",
282                   GST_VAAPI_ID_ARGS(surface_id), ret);
283         g_object_unref(proxy);
284         return GST_FLOW_UNEXPECTED;
285     }
286 error_commit_buffer:
287     {
288         GST_DEBUG("video sink rejected the video buffer (error %d)", ret);
289         g_object_unref(proxy);
290         return GST_FLOW_UNEXPECTED;
291     }
292 }
293
294 static gboolean
295 gst_vaapidecode_create(GstVaapiDecode *decode, GstCaps *caps)
296 {
297     GstVaapiDisplay *dpy;
298     GstStructure *structure;
299     int version;
300
301     if (!gst_vaapi_ensure_display(decode, &decode->display, NULL))
302         return FALSE;
303     dpy = decode->display;
304
305     decode->decoder_mutex = g_mutex_new();
306     if (!decode->decoder_mutex)
307         return FALSE;
308
309     decode->decoder_ready = g_cond_new();
310     if (!decode->decoder_ready)
311         return FALSE;
312
313     structure = gst_caps_get_structure(caps, 0);
314     if (!structure)
315         return FALSE;
316
317     if (gst_structure_has_name(structure, "video/x-h264"))
318         decode->decoder = gst_vaapi_decoder_h264_new(dpy, caps);
319     else if (gst_structure_has_name(structure, "video/mpeg")) {
320         if (!gst_structure_get_int(structure, "mpegversion", &version))
321             return FALSE;
322         if (version == 2)
323             decode->decoder = gst_vaapi_decoder_mpeg2_new(dpy, caps);
324         else if (version == 4)
325             decode->decoder = gst_vaapi_decoder_mpeg4_new(dpy, caps);
326     }
327     else if (gst_structure_has_name(structure, "video/x-wmv"))
328         decode->decoder = gst_vaapi_decoder_vc1_new(dpy, caps);
329     else if (gst_structure_has_name(structure, "video/x-h263") ||
330              gst_structure_has_name(structure, "video/x-divx") ||
331              gst_structure_has_name(structure, "video/x-xvid"))
332         decode->decoder = gst_vaapi_decoder_mpeg4_new(dpy, caps);
333 #if USE_JPEG_DECODER
334     else if (gst_structure_has_name(structure, "image/jpeg"))
335         decode->decoder = gst_vaapi_decoder_jpeg_new(dpy, caps);
336 #endif
337     if (!decode->decoder)
338         return FALSE;
339
340     g_signal_connect(
341         G_OBJECT(decode->decoder),
342         "notify::caps",
343         G_CALLBACK(gst_vaapi_decoder_notify_caps),
344         decode
345     );
346
347     decode->decoder_caps = gst_caps_ref(caps);
348     return TRUE;
349 }
350
351 static void
352 gst_vaapidecode_destroy(GstVaapiDecode *decode)
353 {
354     if (decode->decoder) {
355         gst_vaapi_decoder_put_buffer(decode->decoder, NULL);
356         g_object_unref(decode->decoder);
357         decode->decoder = NULL;
358     }
359
360     if (decode->decoder_caps) {
361         gst_caps_unref(decode->decoder_caps);
362         decode->decoder_caps = NULL;
363     }
364
365     if (decode->decoder_ready) {
366         gst_vaapidecode_release(decode, NULL);
367         g_cond_free(decode->decoder_ready);
368         decode->decoder_ready = NULL;
369     }
370
371     if (decode->decoder_mutex) {
372         g_mutex_free(decode->decoder_mutex);
373         decode->decoder_mutex = NULL;
374     }
375 }
376
377 static gboolean
378 gst_vaapidecode_reset(GstVaapiDecode *decode, GstCaps *caps)
379 {
380     if (decode->decoder &&
381         decode->decoder_caps &&
382         gst_caps_is_always_compatible(caps, decode->decoder_caps))
383         return TRUE;
384
385     gst_vaapidecode_destroy(decode);
386     return gst_vaapidecode_create(decode, caps);
387 }
388
389 /* GstImplementsInterface interface */
390
391 static gboolean
392 gst_vaapidecode_implements_interface_supported(
393     GstImplementsInterface *iface,
394     GType                   type
395 )
396 {
397     return (type == GST_TYPE_VIDEO_CONTEXT);
398 }
399
400 static void
401 gst_vaapidecode_implements_iface_init(GstImplementsInterfaceClass *iface)
402 {
403     iface->supported = gst_vaapidecode_implements_interface_supported;
404 }
405
406 /* GstVideoContext interface */
407
408 static void
409 gst_vaapidecode_set_video_context(GstVideoContext *context, const gchar *type,
410     const GValue *value)
411 {
412     GstVaapiDecode *decode = GST_VAAPIDECODE (context);
413     gst_vaapi_set_display (type, value, &decode->display);
414 }
415
416 static void
417 gst_video_context_interface_init(GstVideoContextInterface *iface)
418 {
419     iface->set_context = gst_vaapidecode_set_video_context;
420 }
421
422 static void
423 gst_vaapidecode_finalize(GObject *object)
424 {
425     GstVaapiDecode * const decode = GST_VAAPIDECODE(object);
426
427     gst_vaapidecode_destroy(decode);
428
429     if (decode->sinkpad_caps) {
430         gst_caps_unref(decode->sinkpad_caps);
431         decode->sinkpad_caps = NULL;
432     }
433
434     if (decode->srcpad_caps) {
435         gst_caps_unref(decode->srcpad_caps);
436         decode->srcpad_caps = NULL;
437     }
438
439     g_clear_object(&decode->display);
440
441     if (decode->allowed_caps) {
442         gst_caps_unref(decode->allowed_caps);
443         decode->allowed_caps = NULL;
444     }
445
446     if (decode->delayed_new_seg) {
447         gst_event_unref(decode->delayed_new_seg);
448         decode->delayed_new_seg = NULL;
449     }
450
451     G_OBJECT_CLASS(gst_vaapidecode_parent_class)->finalize(object);
452 }
453
454 static GstStateChangeReturn
455 gst_vaapidecode_change_state(GstElement *element, GstStateChange transition)
456 {
457     GstVaapiDecode * const decode = GST_VAAPIDECODE(element);
458     GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
459
460     switch (transition) {
461     case GST_STATE_CHANGE_NULL_TO_READY:
462         decode->is_ready = TRUE;
463         break;
464     case GST_STATE_CHANGE_READY_TO_PAUSED:
465         break;
466     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
467         break;
468     default:
469         break;
470     }
471
472     ret = GST_ELEMENT_CLASS(gst_vaapidecode_parent_class)->change_state(element, transition);
473     if (ret != GST_STATE_CHANGE_SUCCESS)
474         return ret;
475
476     switch (transition) {
477     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
478         break;
479     case GST_STATE_CHANGE_PAUSED_TO_READY:
480         break;
481     case GST_STATE_CHANGE_READY_TO_NULL:
482         gst_vaapidecode_destroy(decode);
483         g_clear_object(&decode->display);
484         decode->is_ready = FALSE;
485         break;
486     default:
487         break;
488     }
489     return GST_STATE_CHANGE_SUCCESS;
490 }
491
492 static void
493 gst_vaapidecode_class_init(GstVaapiDecodeClass *klass)
494 {
495     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
496     GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
497     GstPadTemplate *pad_template;
498
499     GST_DEBUG_CATEGORY_INIT(gst_debug_vaapidecode,
500                             GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
501
502     object_class->finalize      = gst_vaapidecode_finalize;
503
504     element_class->change_state = gst_vaapidecode_change_state;
505
506     gst_element_class_set_details_simple(
507         element_class,
508         gst_vaapidecode_details.longname,
509         gst_vaapidecode_details.klass,
510         gst_vaapidecode_details.description,
511         gst_vaapidecode_details.author
512     );
513
514     /* sink pad */
515     pad_template = gst_static_pad_template_get(&gst_vaapidecode_sink_factory);
516     gst_element_class_add_pad_template(element_class, pad_template);
517     gst_object_unref(pad_template);
518
519     /* src pad */
520     pad_template = gst_static_pad_template_get(&gst_vaapidecode_src_factory);
521     gst_element_class_add_pad_template(element_class, pad_template);
522     gst_object_unref(pad_template);
523 }
524
525 static gboolean
526 gst_vaapidecode_ensure_allowed_caps(GstVaapiDecode *decode)
527 {
528     GstCaps *decode_caps;
529     guint i, n_decode_caps;
530
531     if (decode->allowed_caps)
532         return TRUE;
533
534     if (!gst_vaapi_ensure_display(decode, &decode->display, NULL))
535         goto error_no_display;
536
537     decode_caps = gst_vaapi_display_get_decode_caps(decode->display);
538     if (!decode_caps)
539         goto error_no_decode_caps;
540     n_decode_caps = gst_caps_get_size(decode_caps);
541
542     decode->allowed_caps = gst_caps_new_empty();
543     if (!decode->allowed_caps)
544         goto error_no_memory;
545
546     for (i = 0; i < n_decode_caps; i++) {
547         GstStructure *structure;
548         structure = gst_caps_get_structure(decode_caps, i);
549         if (!structure)
550             continue;
551         structure = gst_structure_copy(structure);
552         if (!structure)
553             continue;
554         gst_structure_remove_field(structure, "profile");
555         gst_structure_set(
556             structure,
557             "width",  GST_TYPE_INT_RANGE, 1, G_MAXINT,
558             "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
559             NULL
560         );
561         gst_caps_merge_structure(decode->allowed_caps, structure);
562     }
563
564     gst_caps_unref(decode_caps);
565     return TRUE;
566
567     /* ERRORS */
568 error_no_display:
569     {
570         GST_DEBUG("failed to retrieve VA display");
571         return FALSE;
572     }
573 error_no_decode_caps:
574     {
575         GST_DEBUG("failed to retrieve VA decode caps");
576         return FALSE;
577     }
578 error_no_memory:
579     {
580         GST_DEBUG("failed to allocate allowed-caps set");
581         gst_caps_unref(decode_caps);
582         return FALSE;
583     }
584 }
585
586 static GstCaps *
587 gst_vaapidecode_get_caps(GstPad *pad)
588 {
589     GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
590
591     if (!decode->is_ready)
592         return gst_static_pad_template_get_caps(&gst_vaapidecode_sink_factory);
593
594     if (!gst_vaapidecode_ensure_allowed_caps(decode))
595         return gst_caps_new_empty();
596
597     return gst_caps_ref(decode->allowed_caps);
598 }
599
600 static gboolean
601 gst_vaapidecode_set_caps(GstPad *pad, GstCaps *caps)
602 {
603     GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
604
605     g_return_val_if_fail(pad == decode->sinkpad, FALSE);
606
607     if (!gst_vaapidecode_update_sink_caps(decode, caps))
608         return FALSE;
609     if (!gst_vaapidecode_update_src_caps(decode, caps))
610         return FALSE;
611     if (!gst_vaapidecode_reset(decode, decode->sinkpad_caps))
612         return FALSE;
613
614     /* Propagate NEWSEGMENT event downstream, now that pads are linked */
615     if (decode->delayed_new_seg) {
616         if (gst_pad_push_event(decode->srcpad, decode->delayed_new_seg))
617             gst_event_unref(decode->delayed_new_seg);
618         decode->delayed_new_seg = NULL;
619     }
620     return TRUE;
621 }
622
623 static GstFlowReturn
624 gst_vaapidecode_chain(GstPad *pad, GstBuffer *buf)
625 {
626     GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
627
628     if (!gst_vaapi_decoder_put_buffer(decode->decoder, buf))
629         goto error_push_buffer;
630
631     gst_buffer_unref(buf);
632     return gst_vaapidecode_step(decode);
633
634     /* ERRORS */
635 error_push_buffer:
636     {
637         GST_DEBUG("failed to push input buffer to decoder");
638         gst_buffer_unref(buf);
639         return GST_FLOW_UNEXPECTED;
640     }
641 }
642
643 static gboolean
644 gst_vaapidecode_sink_event(GstPad *pad, GstEvent *event)
645 {
646     GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
647
648     GST_DEBUG("handle sink event '%s'", GST_EVENT_TYPE_NAME(event));
649
650     /* Propagate event downstream */
651     switch (GST_EVENT_TYPE(event)) {
652     case GST_EVENT_NEWSEGMENT:
653         if (decode->delayed_new_seg) {
654             gst_event_unref(decode->delayed_new_seg);
655             decode->delayed_new_seg = NULL;
656         }
657         if (!GST_PAD_PEER(decode->srcpad)) {
658             decode->delayed_new_seg = gst_event_ref(event);
659             return TRUE;
660         }
661         break;
662     default:
663         break;
664     }
665     return gst_pad_push_event(decode->srcpad, event);
666 }
667
668 static gboolean
669 gst_vaapidecode_src_event(GstPad *pad, GstEvent *event)
670 {
671     GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
672
673     GST_DEBUG("handle src event '%s'", GST_EVENT_TYPE_NAME(event));
674
675     /* Propagate event upstream */
676     return gst_pad_push_event(decode->sinkpad, event);
677 }
678
679 static gboolean
680 gst_vaapidecode_query (GstPad *pad, GstQuery *query) {
681     GstVaapiDecode *decode = GST_VAAPIDECODE (gst_pad_get_parent_element (pad));
682     gboolean res;
683
684     GST_DEBUG ("sharing display %p", decode->display);
685
686     if (gst_vaapi_reply_to_query (query, decode->display))
687       res = TRUE;
688     else
689       res = gst_pad_query_default (pad, query);
690
691     g_object_unref (decode);
692     return res;
693 }
694
695 static void
696 gst_vaapidecode_init(GstVaapiDecode *decode)
697 {
698     GstVaapiDecodeClass *klass = GST_VAAPIDECODE_GET_CLASS(decode);
699     GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
700
701     decode->display             = NULL;
702     decode->decoder             = NULL;
703     decode->decoder_mutex       = NULL;
704     decode->decoder_ready       = NULL;
705     decode->decoder_caps        = NULL;
706     decode->allowed_caps        = NULL;
707     decode->delayed_new_seg     = NULL;
708     decode->is_ready            = FALSE;
709
710     /* Pad through which data comes in to the element */
711     decode->sinkpad = gst_pad_new_from_template(
712         gst_element_class_get_pad_template(element_class, "sink"),
713         "sink"
714     );
715     decode->sinkpad_caps = NULL;
716
717     gst_pad_set_getcaps_function(decode->sinkpad, gst_vaapidecode_get_caps);
718     gst_pad_set_setcaps_function(decode->sinkpad, gst_vaapidecode_set_caps);
719     gst_pad_set_chain_function(decode->sinkpad, gst_vaapidecode_chain);
720     gst_pad_set_event_function(decode->sinkpad, gst_vaapidecode_sink_event);
721     gst_pad_set_query_function(decode->sinkpad, gst_vaapidecode_query);
722     gst_element_add_pad(GST_ELEMENT(decode), decode->sinkpad);
723
724     /* Pad through which data goes out of the element */
725     decode->srcpad = gst_pad_new_from_template(
726         gst_element_class_get_pad_template(element_class, "src"),
727         "src"
728     );
729     decode->srcpad_caps = NULL;
730
731     gst_pad_use_fixed_caps(decode->srcpad);
732     gst_pad_set_event_function(decode->srcpad, gst_vaapidecode_src_event);
733     gst_pad_set_query_function(decode->srcpad, gst_vaapidecode_query);
734     gst_element_add_pad(GST_ELEMENT(decode), decode->srcpad);
735 }