2 * gstvaapidecode.c - VA-API video decoder
4 * gstreamer-vaapi (C) 2010-2011 Splitted-Desktop Systems
5 * Copyright (C) 2011 Intel Corporation
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program 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
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 * SECTION:gstvaapidecode
24 * @short_description: A VA-API based video decoder
26 * vaapidecode decodes from raw bitstreams to surfaces suitable for
27 * the vaapisink element.
31 #include "gstvaapidecode.h"
32 #include <gst/vaapi/gstvaapidisplay_x11.h>
33 #include <gst/vaapi/gstvaapivideosink.h>
34 #include <gst/vaapi/gstvaapivideobuffer.h>
35 #include <gst/vaapi/gstvaapidecoder_ffmpeg.h>
36 #include <gst/vaapi/gstvaapiutils_gst.h>
38 #define GST_PLUGIN_NAME "vaapidecode"
39 #define GST_PLUGIN_DESC "A VA-API based video decoder"
41 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapidecode);
42 #define GST_CAT_DEFAULT gst_debug_vaapidecode
44 /* ElementFactory information */
45 static const GstElementDetails gst_vaapidecode_details =
48 "Codec/Decoder/Video",
50 "Gwenole Beauchesne <gbeauchesne@splitted-desktop.com>");
52 /* Default templates */
53 #define GST_CAPS_CODEC(CODEC) CODEC "; "
55 static const char gst_vaapidecode_sink_caps_str[] =
56 GST_CAPS_CODEC("video/mpeg, mpegversion=2, systemstream=(boolean)false")
57 GST_CAPS_CODEC("video/mpeg, mpegversion=4")
58 GST_CAPS_CODEC("video/x-divx")
59 GST_CAPS_CODEC("video/x-xvid")
60 GST_CAPS_CODEC("video/x-h263")
61 GST_CAPS_CODEC("video/x-h264")
62 GST_CAPS_CODEC("video/x-wmv")
65 static const char gst_vaapidecode_src_caps_str[] =
66 GST_VAAPI_SURFACE_CAPS;
68 static GstStaticPadTemplate gst_vaapidecode_sink_factory =
69 GST_STATIC_PAD_TEMPLATE(
73 GST_STATIC_CAPS(gst_vaapidecode_sink_caps_str));
75 static GstStaticPadTemplate gst_vaapidecode_src_factory =
76 GST_STATIC_PAD_TEMPLATE(
80 GST_STATIC_CAPS(gst_vaapidecode_src_caps_str));
95 gst_vaapidecode_update_src_caps(GstVaapiDecode *decode, GstCaps *caps);
98 gst_vaapi_decoder_notify_caps(GObject *obj, GParamSpec *pspec, void *user_data)
100 GstVaapiDecode * const decode = GST_VAAPIDECODE(user_data);
103 g_assert(decode->decoder == GST_VAAPI_DECODER(obj));
105 caps = gst_vaapi_decoder_get_caps(decode->decoder);
106 gst_vaapidecode_update_src_caps(decode, caps);
109 static inline gboolean
110 gst_vaapidecode_update_sink_caps(GstVaapiDecode *decode, GstCaps *caps)
112 if (decode->sinkpad_caps)
113 gst_caps_unref(decode->sinkpad_caps);
114 decode->sinkpad_caps = gst_caps_ref(caps);
119 gst_vaapidecode_update_src_caps(GstVaapiDecode *decode, GstCaps *caps)
122 GstStructure *structure;
123 const GValue *v_width, *v_height, *v_framerate, *v_par;
126 if (!decode->srcpad_caps) {
127 decode->srcpad_caps = gst_caps_from_string(GST_VAAPI_SURFACE_CAPS_NAME);
128 if (!decode->srcpad_caps)
132 structure = gst_caps_get_structure(caps, 0);
133 v_width = gst_structure_get_value(structure, "width");
134 v_height = gst_structure_get_value(structure, "height");
135 v_framerate = gst_structure_get_value(structure, "framerate");
136 v_par = gst_structure_get_value(structure, "pixel-aspect-ratio");
138 structure = gst_caps_get_structure(decode->srcpad_caps, 0);
139 if (v_width && v_height) {
140 gst_structure_set_value(structure, "width", v_width);
141 gst_structure_set_value(structure, "height", v_height);
144 gst_structure_set_value(structure, "framerate", v_framerate);
146 gst_structure_set_value(structure, "pixel-aspect-ratio", v_par);
148 other_caps = gst_caps_copy(decode->srcpad_caps);
149 success = gst_pad_set_caps(decode->srcpad, other_caps);
150 gst_caps_unref(other_caps);
155 gst_vaapidecode_release(GstVaapiDecode *decode, GObject *dead_object)
157 g_mutex_lock(decode->decoder_mutex);
158 g_cond_signal(decode->decoder_ready);
159 g_mutex_unlock(decode->decoder_mutex);
163 gst_vaapidecode_step(GstVaapiDecode *decode)
165 GstVaapiSurfaceProxy *proxy;
166 GstVaapiDecoderStatus status;
174 proxy = gst_vaapi_decoder_get_surface(decode->decoder, &status);
176 if (status == GST_VAAPI_DECODER_STATUS_ERROR_NO_SURFACE) {
177 /* Wait for a VA surface to be displayed and free'd */
179 goto error_decode_timeout;
181 g_get_current_time(&timeout);
182 g_time_val_add(&timeout, 10000); /* 10 ms each step */
183 g_mutex_lock(decode->decoder_mutex);
185 decode->decoder_ready,
186 decode->decoder_mutex,
189 g_mutex_unlock(decode->decoder_mutex);
192 if (status != GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA)
194 /* More data is needed */
200 (GWeakNotify)gst_vaapidecode_release,
205 ret = gst_pad_alloc_buffer(
208 GST_PAD_CAPS(decode->srcpad),
211 if (ret != GST_FLOW_OK || !buffer)
212 goto error_create_buffer;
214 GST_BUFFER_TIMESTAMP(buffer) = GST_VAAPI_SURFACE_PROXY_TIMESTAMP(proxy);
215 gst_vaapi_video_buffer_set_surface_proxy(
216 GST_VAAPI_VIDEO_BUFFER(buffer),
220 ret = gst_pad_push(decode->srcpad, buffer);
221 if (ret != GST_FLOW_OK)
222 goto error_commit_buffer;
224 g_object_unref(proxy);
229 error_decode_timeout:
231 GST_DEBUG("decode timeout. Decoder required a VA surface but none "
232 "got available within one second");
233 return GST_FLOW_UNEXPECTED;
237 GST_DEBUG("decode error %d", status);
238 return GST_FLOW_UNEXPECTED;
242 const GstVaapiID surface_id =
243 gst_vaapi_surface_get_id(GST_VAAPI_SURFACE_PROXY_SURFACE(proxy));
245 GST_DEBUG("video sink failed to create video buffer for proxy'ed "
246 "surface %" GST_VAAPI_ID_FORMAT " (error %d)",
247 GST_VAAPI_ID_ARGS(surface_id), ret);
248 g_object_unref(proxy);
249 return GST_FLOW_UNEXPECTED;
253 GST_DEBUG("video sink rejected the video buffer (error %d)", ret);
254 g_object_unref(proxy);
255 return GST_FLOW_UNEXPECTED;
259 static inline gboolean
260 gst_vaapidecode_ensure_display(GstVaapiDecode *decode)
262 GstVaapiDisplay *display;
264 if (!decode->display) {
265 display = gst_vaapi_display_lookup_downstream(GST_ELEMENT(decode));
268 decode->display = g_object_ref(display);
274 gst_vaapidecode_create(GstVaapiDecode *decode, GstCaps *caps)
276 if (!gst_vaapidecode_ensure_display(decode))
279 decode->decoder_mutex = g_mutex_new();
280 if (!decode->decoder_mutex)
283 decode->decoder_ready = g_cond_new();
284 if (!decode->decoder_ready)
287 if (decode->use_ffmpeg)
288 decode->decoder = gst_vaapi_decoder_ffmpeg_new(decode->display, caps);
289 if (!decode->decoder)
293 G_OBJECT(decode->decoder),
295 G_CALLBACK(gst_vaapi_decoder_notify_caps),
299 decode->decoder_caps = gst_caps_ref(caps);
304 gst_vaapidecode_destroy(GstVaapiDecode *decode)
306 if (decode->decoder_ready) {
307 gst_vaapidecode_release(decode, NULL);
308 g_cond_free(decode->decoder_ready);
309 decode->decoder_ready = NULL;
312 if (decode->decoder_mutex) {
313 g_mutex_free(decode->decoder_mutex);
314 decode->decoder_mutex = NULL;
317 if (decode->decoder) {
318 gst_vaapi_decoder_put_buffer(decode->decoder, NULL);
319 g_object_unref(decode->decoder);
320 decode->decoder = NULL;
323 if (decode->decoder_caps) {
324 gst_caps_unref(decode->decoder_caps);
325 decode->decoder_caps = NULL;
330 gst_vaapidecode_reset(GstVaapiDecode *decode, GstCaps *caps)
332 if (decode->decoder &&
333 decode->decoder_caps &&
334 gst_caps_is_always_compatible(caps, decode->decoder_caps))
337 gst_vaapidecode_destroy(decode);
338 return gst_vaapidecode_create(decode, caps);
342 gst_vaapidecode_base_init(gpointer klass)
344 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
346 gst_element_class_set_details(element_class, &gst_vaapidecode_details);
349 gst_element_class_add_pad_template(
351 gst_static_pad_template_get(&gst_vaapidecode_sink_factory)
355 gst_element_class_add_pad_template(
357 gst_static_pad_template_get(&gst_vaapidecode_src_factory)
362 gst_vaapidecode_finalize(GObject *object)
364 GstVaapiDecode * const decode = GST_VAAPIDECODE(object);
366 gst_vaapidecode_destroy(decode);
368 if (decode->sinkpad_caps) {
369 gst_caps_unref(decode->sinkpad_caps);
370 decode->sinkpad_caps = NULL;
373 if (decode->srcpad_caps) {
374 gst_caps_unref(decode->srcpad_caps);
375 decode->srcpad_caps = NULL;
378 if (decode->display) {
379 g_object_unref(decode->display);
380 decode->display = NULL;
383 if (decode->allowed_caps) {
384 gst_caps_unref(decode->allowed_caps);
385 decode->allowed_caps = NULL;
388 G_OBJECT_CLASS(parent_class)->finalize(object);
392 gst_vaapidecode_set_property(
399 GstVaapiDecode * const decode = GST_VAAPIDECODE(object);
402 case PROP_USE_FFMPEG:
403 decode->use_ffmpeg = g_value_get_boolean(value);
406 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
412 gst_vaapidecode_get_property(
419 GstVaapiDecode * const decode = GST_VAAPIDECODE(object);
422 case PROP_USE_FFMPEG:
423 g_value_set_boolean(value, decode->use_ffmpeg);
426 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
431 static GstStateChangeReturn
432 gst_vaapidecode_change_state(GstElement *element, GstStateChange transition)
434 GstVaapiDecode * const decode = GST_VAAPIDECODE(element);
435 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
437 switch (transition) {
438 case GST_STATE_CHANGE_READY_TO_PAUSED:
440 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
446 ret = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
447 if (ret != GST_STATE_CHANGE_SUCCESS)
450 switch (transition) {
451 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
453 case GST_STATE_CHANGE_PAUSED_TO_READY:
458 return GST_STATE_CHANGE_SUCCESS;
462 gst_vaapidecode_class_init(GstVaapiDecodeClass *klass)
464 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
465 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
467 object_class->finalize = gst_vaapidecode_finalize;
468 object_class->set_property = gst_vaapidecode_set_property;
469 object_class->get_property = gst_vaapidecode_get_property;
471 element_class->change_state = gst_vaapidecode_change_state;
473 g_object_class_install_property
476 g_param_spec_boolean("use-ffmpeg",
477 "Use FFmpeg/VAAPI for decoding",
478 "Uses FFmpeg/VAAPI for decoding",
484 gst_vaapidecode_ensure_allowed_caps(GstVaapiDecode *decode)
486 GstVaapiDisplay *display;
487 GstCaps *decode_caps;
488 guint i, n_decode_caps;
490 if (decode->allowed_caps)
493 if (gst_vaapidecode_ensure_display(decode))
494 display = g_object_ref(decode->display);
496 display = gst_vaapi_display_x11_new(NULL);
498 goto error_no_display;
501 decode_caps = gst_vaapi_display_get_decode_caps(display);
503 goto error_no_decode_caps;
504 n_decode_caps = gst_caps_get_size(decode_caps);
506 decode->allowed_caps = gst_caps_new_empty();
507 if (!decode->allowed_caps)
508 goto error_no_memory;
510 for (i = 0; i < n_decode_caps; i++) {
511 GstStructure *structure;
512 structure = gst_caps_get_structure(decode_caps, i);
515 structure = gst_structure_copy(structure);
518 gst_structure_remove_field(structure, "profile");
521 "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
522 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
525 gst_caps_merge_structure(decode->allowed_caps, structure);
528 gst_caps_unref(decode_caps);
529 g_object_unref(display);
535 GST_DEBUG("failed to retrieve VA display");
538 error_no_decode_caps:
540 GST_DEBUG("failed to retrieve VA decode caps");
541 g_object_unref(display);
546 GST_DEBUG("failed to allocate allowed-caps set");
547 gst_caps_unref(decode_caps);
548 g_object_unref(display);
554 gst_vaapidecode_get_caps(GstPad *pad)
556 GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
558 if (!gst_vaapidecode_ensure_allowed_caps(decode))
559 return gst_caps_new_empty();
561 return gst_caps_ref(decode->allowed_caps);
565 gst_vaapidecode_set_caps(GstPad *pad, GstCaps *caps)
567 GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
569 g_return_val_if_fail(pad == decode->sinkpad, FALSE);
571 if (!gst_vaapidecode_update_sink_caps(decode, caps))
573 if (!gst_vaapidecode_update_src_caps(decode, caps))
575 return gst_vaapidecode_reset(decode, decode->sinkpad_caps);
579 gst_vaapidecode_chain(GstPad *pad, GstBuffer *buf)
581 GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
583 if (!gst_vaapi_decoder_put_buffer(decode->decoder, buf))
584 goto error_push_buffer;
586 gst_buffer_unref(buf);
587 return gst_vaapidecode_step(decode);
592 GST_DEBUG("failed to push input buffer to decoder");
593 gst_buffer_unref(buf);
594 return GST_FLOW_UNEXPECTED;
599 gst_vaapidecode_sink_event(GstPad *pad, GstEvent *event)
601 GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
603 GST_DEBUG("handle sink event '%s'", GST_EVENT_TYPE_NAME(event));
605 /* Propagate event downstream */
606 return gst_pad_push_event(decode->srcpad, event);
610 gst_vaapidecode_src_event(GstPad *pad, GstEvent *event)
612 GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
614 GST_DEBUG("handle src event '%s'", GST_EVENT_TYPE_NAME(event));
616 /* Propagate event upstream */
617 return gst_pad_push_event(decode->sinkpad, event);
621 gst_vaapidecode_init(GstVaapiDecode *decode, GstVaapiDecodeClass *klass)
623 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
625 decode->display = NULL;
626 decode->decoder = NULL;
627 decode->decoder_mutex = NULL;
628 decode->decoder_ready = NULL;
629 decode->decoder_caps = NULL;
630 decode->allowed_caps = NULL;
631 decode->use_ffmpeg = TRUE;
633 /* Pad through which data comes in to the element */
634 decode->sinkpad = gst_pad_new_from_template(
635 gst_element_class_get_pad_template(element_class, "sink"),
638 decode->sinkpad_caps = NULL;
640 gst_pad_set_getcaps_function(decode->sinkpad, gst_vaapidecode_get_caps);
641 gst_pad_set_setcaps_function(decode->sinkpad, gst_vaapidecode_set_caps);
642 gst_pad_set_chain_function(decode->sinkpad, gst_vaapidecode_chain);
643 gst_pad_set_event_function(decode->sinkpad, gst_vaapidecode_sink_event);
644 gst_element_add_pad(GST_ELEMENT(decode), decode->sinkpad);
646 /* Pad through which data goes out of the element */
647 decode->srcpad = gst_pad_new_from_template(
648 gst_element_class_get_pad_template(element_class, "src"),
651 decode->srcpad_caps = NULL;
653 gst_pad_use_fixed_caps(decode->srcpad);
654 gst_pad_set_event_function(decode->srcpad, gst_vaapidecode_src_event);
655 gst_element_add_pad(GST_ELEMENT(decode), decode->srcpad);
659 plugin_init(GstPlugin *plugin)
661 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapidecode,
662 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
664 return gst_element_register(plugin,
667 GST_TYPE_VAAPIDECODE);
671 GST_VERSION_MAJOR, GST_VERSION_MINOR,