2 * gstvaapidecode.c - VA-API video decoder
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
5 * Copyright (C) 2011-2012 Intel Corporation
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.
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.
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
24 * SECTION:gstvaapidecode
25 * @short_description: A VA-API based video decoder
27 * vaapidecode decodes from raw bitstreams to surfaces suitable for
28 * the vaapisink element.
33 #include <gst/vaapi/gstvaapidisplay.h>
34 #include <gst/vaapi/gstvaapivideosink.h>
35 #include <gst/vaapi/gstvaapivideobuffer.h>
36 #include <gst/video/videocontext.h>
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))
44 #include "gstvaapidecode.h"
45 #include "gstvaapipluginutil.h"
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>
53 #define GST_PLUGIN_NAME "vaapidecode"
54 #define GST_PLUGIN_DESC "A VA-API based video decoder"
56 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapidecode);
57 #define GST_CAT_DEFAULT gst_debug_vaapidecode
59 /* ElementFactory information */
60 static const GstElementDetails gst_vaapidecode_details =
63 "Codec/Decoder/Video",
65 "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");
67 /* Default templates */
68 #define GST_CAPS_CODEC(CODEC) CODEC "; "
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")
81 static const char gst_vaapidecode_src_caps_str[] =
82 GST_VAAPI_SURFACE_CAPS;
84 static GstStaticPadTemplate gst_vaapidecode_sink_factory =
85 GST_STATIC_PAD_TEMPLATE(
89 GST_STATIC_CAPS(gst_vaapidecode_sink_caps_str));
91 static GstStaticPadTemplate gst_vaapidecode_src_factory =
92 GST_STATIC_PAD_TEMPLATE(
96 GST_STATIC_CAPS(gst_vaapidecode_src_caps_str));
99 gst_vaapidecode_implements_iface_init(GstImplementsInterfaceClass *iface);
102 gst_video_context_interface_init(GstVideoContextInterface *iface);
104 #define GstVideoContextClass GstVideoContextInterface
105 G_DEFINE_TYPE_WITH_CODE(
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));
115 gst_vaapidecode_update_src_caps(GstVaapiDecode *decode, GstCaps *caps);
118 gst_vaapi_decoder_notify_caps(GObject *obj, GParamSpec *pspec, void *user_data)
120 GstVaapiDecode * const decode = GST_VAAPIDECODE(user_data);
123 g_assert(decode->decoder == GST_VAAPI_DECODER(obj));
125 caps = gst_vaapi_decoder_get_caps(decode->decoder);
126 gst_vaapidecode_update_src_caps(decode, caps);
129 static inline gboolean
130 gst_vaapidecode_update_sink_caps(GstVaapiDecode *decode, GstCaps *caps)
132 if (decode->sinkpad_caps)
133 gst_caps_unref(decode->sinkpad_caps);
134 decode->sinkpad_caps = gst_caps_ref(caps);
139 gst_vaapidecode_update_src_caps(GstVaapiDecode *decode, GstCaps *caps)
142 GstStructure *structure;
143 const GValue *v_width, *v_height, *v_framerate, *v_par, *v_interlaced;
146 if (!decode->srcpad_caps) {
147 decode->srcpad_caps = gst_caps_from_string(GST_VAAPI_SURFACE_CAPS_NAME);
148 if (!decode->srcpad_caps)
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");
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);
165 gst_structure_set_value(structure, "framerate", v_framerate);
167 gst_structure_set_value(structure, "pixel-aspect-ratio", v_par);
169 gst_structure_set_value(structure, "interlaced", v_interlaced);
171 gst_structure_set(structure, "type", G_TYPE_STRING, "vaapi", NULL);
172 gst_structure_set(structure, "opengl", G_TYPE_BOOLEAN, USE_GLX, NULL);
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);
181 gst_vaapidecode_release(GstVaapiDecode *decode, GObject *dead_object)
183 g_mutex_lock(decode->decoder_mutex);
184 g_cond_signal(decode->decoder_ready);
185 g_mutex_unlock(decode->decoder_mutex);
189 gst_vaapidecode_step(GstVaapiDecode *decode)
191 GstVaapiSurfaceProxy *proxy;
192 GstVaapiDecoderStatus status;
200 proxy = gst_vaapi_decoder_get_surface(decode->decoder, &status);
202 if (status == GST_VAAPI_DECODER_STATUS_ERROR_NO_SURFACE) {
203 /* Wait for a VA surface to be displayed and free'd */
205 goto error_decode_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);
211 decode->decoder_ready,
212 decode->decoder_mutex,
215 g_mutex_unlock(decode->decoder_mutex);
218 if (status != GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA)
220 /* More data is needed */
226 (GWeakNotify)gst_vaapidecode_release,
230 buffer = gst_vaapi_video_buffer_new(decode->display);
232 goto error_create_buffer;
234 GST_BUFFER_TIMESTAMP(buffer) = GST_VAAPI_SURFACE_PROXY_TIMESTAMP(proxy);
235 gst_buffer_set_caps(buffer, GST_PAD_CAPS(decode->srcpad));
237 if (GST_VAAPI_SURFACE_PROXY_TFF(proxy))
238 GST_BUFFER_FLAG_SET(buffer, GST_VIDEO_BUFFER_TFF);
240 gst_vaapi_video_buffer_set_surface_proxy(
241 GST_VAAPI_VIDEO_BUFFER(buffer),
245 ret = gst_pad_push(decode->srcpad, buffer);
246 if (ret != GST_FLOW_OK)
247 goto error_commit_buffer;
249 g_object_unref(proxy);
254 error_decode_timeout:
256 GST_DEBUG("decode timeout. Decoder required a VA surface but none "
257 "got available within one second");
258 return GST_FLOW_UNEXPECTED;
262 GST_DEBUG("decode error %d", 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;
270 ret = GST_FLOW_UNEXPECTED;
277 const GstVaapiID surface_id =
278 gst_vaapi_surface_get_id(GST_VAAPI_SURFACE_PROXY_SURFACE(proxy));
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;
288 GST_DEBUG("video sink rejected the video buffer (error %d)", ret);
289 g_object_unref(proxy);
290 return GST_FLOW_UNEXPECTED;
295 gst_vaapidecode_create(GstVaapiDecode *decode, GstCaps *caps)
297 GstVaapiDisplay *dpy;
298 GstStructure *structure;
301 if (!gst_vaapi_ensure_display(decode, &decode->display))
303 dpy = decode->display;
305 decode->decoder_mutex = g_mutex_new();
306 if (!decode->decoder_mutex)
309 decode->decoder_ready = g_cond_new();
310 if (!decode->decoder_ready)
313 structure = gst_caps_get_structure(caps, 0);
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))
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);
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);
334 else if (gst_structure_has_name(structure, "image/jpeg"))
335 decode->decoder = gst_vaapi_decoder_jpeg_new(dpy, caps);
337 if (!decode->decoder)
341 G_OBJECT(decode->decoder),
343 G_CALLBACK(gst_vaapi_decoder_notify_caps),
347 decode->decoder_caps = gst_caps_ref(caps);
352 gst_vaapidecode_destroy(GstVaapiDecode *decode)
354 if (decode->decoder) {
355 gst_vaapi_decoder_put_buffer(decode->decoder, NULL);
356 g_object_unref(decode->decoder);
357 decode->decoder = NULL;
360 if (decode->decoder_caps) {
361 gst_caps_unref(decode->decoder_caps);
362 decode->decoder_caps = NULL;
365 if (decode->decoder_ready) {
366 gst_vaapidecode_release(decode, NULL);
367 g_cond_free(decode->decoder_ready);
368 decode->decoder_ready = NULL;
371 if (decode->decoder_mutex) {
372 g_mutex_free(decode->decoder_mutex);
373 decode->decoder_mutex = NULL;
378 gst_vaapidecode_reset(GstVaapiDecode *decode, GstCaps *caps)
380 if (decode->decoder &&
381 decode->decoder_caps &&
382 gst_caps_is_always_compatible(caps, decode->decoder_caps))
385 gst_vaapidecode_destroy(decode);
386 return gst_vaapidecode_create(decode, caps);
389 /* GstImplementsInterface interface */
392 gst_vaapidecode_implements_interface_supported(
393 GstImplementsInterface *iface,
397 return (type == GST_TYPE_VIDEO_CONTEXT);
401 gst_vaapidecode_implements_iface_init(GstImplementsInterfaceClass *iface)
403 iface->supported = gst_vaapidecode_implements_interface_supported;
406 /* GstVideoContext interface */
409 gst_vaapidecode_set_video_context(GstVideoContext *context, const gchar *type,
412 GstVaapiDecode *decode = GST_VAAPIDECODE (context);
413 gst_vaapi_set_display (type, value, &decode->display);
417 gst_video_context_interface_init(GstVideoContextInterface *iface)
419 iface->set_context = gst_vaapidecode_set_video_context;
423 gst_vaapidecode_finalize(GObject *object)
425 GstVaapiDecode * const decode = GST_VAAPIDECODE(object);
427 gst_vaapidecode_destroy(decode);
429 if (decode->sinkpad_caps) {
430 gst_caps_unref(decode->sinkpad_caps);
431 decode->sinkpad_caps = NULL;
434 if (decode->srcpad_caps) {
435 gst_caps_unref(decode->srcpad_caps);
436 decode->srcpad_caps = NULL;
439 g_clear_object(&decode->display);
441 if (decode->allowed_caps) {
442 gst_caps_unref(decode->allowed_caps);
443 decode->allowed_caps = NULL;
446 if (decode->delayed_new_seg) {
447 gst_event_unref(decode->delayed_new_seg);
448 decode->delayed_new_seg = NULL;
451 G_OBJECT_CLASS(gst_vaapidecode_parent_class)->finalize(object);
454 static GstStateChangeReturn
455 gst_vaapidecode_change_state(GstElement *element, GstStateChange transition)
457 GstVaapiDecode * const decode = GST_VAAPIDECODE(element);
458 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
460 switch (transition) {
461 case GST_STATE_CHANGE_NULL_TO_READY:
462 decode->is_ready = TRUE;
464 case GST_STATE_CHANGE_READY_TO_PAUSED:
466 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
472 ret = GST_ELEMENT_CLASS(gst_vaapidecode_parent_class)->change_state(element, transition);
473 if (ret != GST_STATE_CHANGE_SUCCESS)
476 switch (transition) {
477 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
479 case GST_STATE_CHANGE_PAUSED_TO_READY:
481 case GST_STATE_CHANGE_READY_TO_NULL:
482 gst_vaapidecode_destroy(decode);
483 g_clear_object(&decode->display);
484 decode->is_ready = FALSE;
489 return GST_STATE_CHANGE_SUCCESS;
493 gst_vaapidecode_class_init(GstVaapiDecodeClass *klass)
495 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
496 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
497 GstPadTemplate *pad_template;
499 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapidecode,
500 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
502 object_class->finalize = gst_vaapidecode_finalize;
504 element_class->change_state = gst_vaapidecode_change_state;
506 gst_element_class_set_details_simple(
508 gst_vaapidecode_details.longname,
509 gst_vaapidecode_details.klass,
510 gst_vaapidecode_details.description,
511 gst_vaapidecode_details.author
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);
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);
526 gst_vaapidecode_ensure_allowed_caps(GstVaapiDecode *decode)
528 GstCaps *decode_caps;
529 guint i, n_decode_caps;
531 if (decode->allowed_caps)
534 if (!gst_vaapi_ensure_display(decode, &decode->display))
535 goto error_no_display;
537 decode_caps = gst_vaapi_display_get_decode_caps(decode->display);
539 goto error_no_decode_caps;
540 n_decode_caps = gst_caps_get_size(decode_caps);
542 decode->allowed_caps = gst_caps_new_empty();
543 if (!decode->allowed_caps)
544 goto error_no_memory;
546 for (i = 0; i < n_decode_caps; i++) {
547 GstStructure *structure;
548 structure = gst_caps_get_structure(decode_caps, i);
551 structure = gst_structure_copy(structure);
554 gst_structure_remove_field(structure, "profile");
557 "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
558 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
561 gst_caps_merge_structure(decode->allowed_caps, structure);
564 gst_caps_unref(decode_caps);
570 GST_DEBUG("failed to retrieve VA display");
573 error_no_decode_caps:
575 GST_DEBUG("failed to retrieve VA decode caps");
580 GST_DEBUG("failed to allocate allowed-caps set");
581 gst_caps_unref(decode_caps);
587 gst_vaapidecode_get_caps(GstPad *pad)
589 GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
591 if (!decode->is_ready)
592 return gst_static_pad_template_get_caps(&gst_vaapidecode_sink_factory);
594 if (!gst_vaapidecode_ensure_allowed_caps(decode))
595 return gst_caps_new_empty();
597 return gst_caps_ref(decode->allowed_caps);
601 gst_vaapidecode_set_caps(GstPad *pad, GstCaps *caps)
603 GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
605 g_return_val_if_fail(pad == decode->sinkpad, FALSE);
607 if (!gst_vaapidecode_update_sink_caps(decode, caps))
609 if (!gst_vaapidecode_update_src_caps(decode, caps))
611 if (!gst_vaapidecode_reset(decode, decode->sinkpad_caps))
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;
624 gst_vaapidecode_chain(GstPad *pad, GstBuffer *buf)
626 GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
628 if (!gst_vaapi_decoder_put_buffer(decode->decoder, buf))
629 goto error_push_buffer;
631 gst_buffer_unref(buf);
632 return gst_vaapidecode_step(decode);
637 GST_DEBUG("failed to push input buffer to decoder");
638 gst_buffer_unref(buf);
639 return GST_FLOW_UNEXPECTED;
644 gst_vaapidecode_sink_event(GstPad *pad, GstEvent *event)
646 GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
648 GST_DEBUG("handle sink event '%s'", GST_EVENT_TYPE_NAME(event));
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;
657 if (!GST_PAD_PEER(decode->srcpad)) {
658 decode->delayed_new_seg = gst_event_ref(event);
665 return gst_pad_push_event(decode->srcpad, event);
669 gst_vaapidecode_src_event(GstPad *pad, GstEvent *event)
671 GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
673 GST_DEBUG("handle src event '%s'", GST_EVENT_TYPE_NAME(event));
675 /* Propagate event upstream */
676 return gst_pad_push_event(decode->sinkpad, event);
680 gst_vaapidecode_query (GstPad *pad, GstQuery *query) {
681 GstVaapiDecode *decode = GST_VAAPIDECODE (gst_pad_get_parent_element (pad));
684 GST_DEBUG ("sharing display %p", decode->display);
686 if (gst_vaapi_reply_to_query (query, decode->display))
689 res = gst_pad_query_default (pad, query);
691 g_object_unref (decode);
696 gst_vaapidecode_init(GstVaapiDecode *decode)
698 GstVaapiDecodeClass *klass = GST_VAAPIDECODE_GET_CLASS(decode);
699 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
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;
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"),
715 decode->sinkpad_caps = NULL;
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);
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"),
729 decode->srcpad_caps = NULL;
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);