4 * Copyright (C) 2015 Intel Corporation
5 * Author: Sreerenj Balachandran <sreerenj.balachandran@intel.com>
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
23 #include "gstcompat.h"
27 #include <gst/pbutils/pbutils.h>
28 #include "gstvaapipluginutil.h"
29 #include "gstvaapidecodebin.h"
30 #include "gstvaapivideocontext.h"
32 #define GST_PLUGIN_NAME "vaapidecodebin"
33 #define GST_PLUGIN_DESC "A Bin of VA-API elements: vaapidecode ! queue ! vaapipostproc"
35 GST_DEBUG_CATEGORY_STATIC (gst_debug_vaapi_decode_bin);
36 #define GST_CAT_DEFAULT gst_debug_vaapi_decode_bin
38 #define DEFAULT_QUEUE_MAX_SIZE_BUFFERS 0
39 #define DEFAULT_QUEUE_MAX_SIZE_BYTES 0
40 #define DEFAULT_QUEUE_MAX_SIZE_TIME 0
41 #define DEFAULT_DEINTERLACE_METHOD GST_VAAPI_DEINTERLACE_METHOD_BOB
46 PROP_MAX_SIZE_BUFFERS,
49 PROP_DEINTERLACE_METHOD,
61 static GParamSpec *properties[PROP_LAST];
63 /* Default templates */
64 #define GST_CAPS_CODEC(CODEC) CODEC "; "
66 static const char gst_vaapi_decode_bin_sink_caps_str[] =
67 GST_CAPS_CODEC("video/mpeg, mpegversion=2, systemstream=(boolean)false")
68 GST_CAPS_CODEC("video/mpeg, mpegversion=4")
69 GST_CAPS_CODEC("video/x-divx")
70 GST_CAPS_CODEC("video/x-xvid")
71 GST_CAPS_CODEC("video/x-h263")
72 GST_CAPS_CODEC("video/x-h264")
74 GST_CAPS_CODEC("video/x-h265")
76 GST_CAPS_CODEC("video/x-wmv")
78 GST_CAPS_CODEC("video/x-vp8")
81 GST_CAPS_CODEC("image/jpeg")
87 static const char gst_vaapi_decode_bin_src_caps_str[] =
88 GST_VAAPI_MAKE_SURFACE_CAPS ", "
89 GST_CAPS_INTERLACED_FALSE "; "
90 GST_VAAPI_MAKE_GLTEXUPLOAD_CAPS ", "
91 GST_CAPS_INTERLACED_FALSE "; "
92 GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL) ", "
93 GST_CAPS_INTERLACED_FALSE;
96 static GstStaticPadTemplate gst_vaapi_decode_bin_sink_factory =
97 GST_STATIC_PAD_TEMPLATE ("sink",
100 GST_STATIC_CAPS (gst_vaapi_decode_bin_sink_caps_str));
102 static GstStaticPadTemplate gst_vaapi_decode_bin_src_factory =
103 GST_STATIC_PAD_TEMPLATE ("src",
106 GST_STATIC_CAPS (gst_vaapi_decode_bin_src_caps_str));
108 G_DEFINE_TYPE (GstVaapiDecodeBin, gst_vaapi_decode_bin, GST_TYPE_BIN);
111 post_missing_element_message (GstVaapiDecodeBin * vaapidecbin,
112 const gchar * missing_factory)
116 GST_ERROR_OBJECT (vaapidecbin, "Failed to create %s element",
119 gst_missing_element_message_new (GST_ELEMENT_CAST (vaapidecbin),
121 gst_element_post_message (GST_ELEMENT_CAST (vaapidecbin), msg);
125 activate_vpp (GstVaapiDecodeBin * vaapidecbin)
129 if (vaapidecbin->ghost_pad_src || vaapidecbin->postproc)
132 if (vaapidecbin->has_vpp != HAS_VPP_YES || vaapidecbin->disable_vpp) {
133 src = vaapidecbin->queue;
134 goto connect_src_ghost_pad;
137 /* create the postproc */
138 vaapidecbin->postproc =
139 gst_element_factory_make ("vaapipostproc", "vaapipostproc");
140 if (!vaapidecbin->postproc)
141 goto error_element_missing;
143 g_object_set (G_OBJECT (vaapidecbin->postproc), "deinterlace-method",
144 vaapidecbin->deinterlace_method, NULL);
146 gst_bin_add (GST_BIN (vaapidecbin), vaapidecbin->postproc);
147 if (!gst_element_link_pads_full (vaapidecbin->queue, "src",
148 vaapidecbin->postproc, "sink", GST_PAD_LINK_CHECK_NOTHING))
151 GST_DEBUG_OBJECT (vaapidecbin, "Enabling VPP");
152 src = vaapidecbin->postproc;
154 goto connect_src_ghost_pad;
156 error_element_missing:
158 post_missing_element_message (vaapidecbin, "vaapipostproc");
163 GST_ERROR_OBJECT (vaapidecbin, "Failed to link the child elements");
166 connect_src_ghost_pad:
168 GstPad *srcpad, *ghostpad;
170 srcpad = gst_element_get_static_pad (src, "src");
171 ghostpad = gst_ghost_pad_new ("src", srcpad);
172 vaapidecbin->ghost_pad_src = ghostpad;
173 gst_object_unref (srcpad);
174 gst_element_add_pad (GST_ELEMENT (vaapidecbin), ghostpad);
180 ensure_vpp (GstVaapiDecodeBin * vaapidecbin)
182 GstVaapiDisplay *display;
184 if (vaapidecbin->has_vpp != HAS_VPP_UNKNOWN)
187 GST_DEBUG_OBJECT (vaapidecbin, "Creating a dummy display to test for vpp");
188 display = gst_vaapi_create_test_display ();
192 vaapidecbin->has_vpp = gst_vaapi_display_has_video_processing (display) ?
193 HAS_VPP_YES : HAS_VPP_NO;
195 gst_vaapi_display_unref (display);
197 if (!activate_vpp (vaapidecbin))
204 gst_vaapi_decode_bin_set_property (GObject * object,
205 guint prop_id, const GValue * value, GParamSpec * pspec)
207 GstVaapiDecodeBin *vaapidecbin = GST_VAAPI_DECODE_BIN (object);
210 case PROP_MAX_SIZE_BYTES:
211 vaapidecbin->max_size_bytes = g_value_get_uint (value);
212 g_object_set (G_OBJECT (vaapidecbin->queue), "max-size-bytes",
213 vaapidecbin->max_size_bytes, NULL);
215 case PROP_MAX_SIZE_BUFFERS:
216 vaapidecbin->max_size_buffers = g_value_get_uint (value);
217 g_object_set (G_OBJECT (vaapidecbin->queue), "max-size-buffers",
218 vaapidecbin->max_size_buffers, NULL);
220 case PROP_MAX_SIZE_TIME:
221 vaapidecbin->max_size_time = g_value_get_uint64 (value);
222 g_object_set (G_OBJECT (vaapidecbin->queue), "max-size-time",
223 vaapidecbin->max_size_time, NULL);
225 case PROP_DEINTERLACE_METHOD:
226 vaapidecbin->deinterlace_method = g_value_get_enum (value);
227 g_object_set (G_OBJECT (vaapidecbin->postproc), "deinterlace-method",
228 vaapidecbin->deinterlace_method, NULL);
230 case PROP_DISABLE_VPP:
232 gboolean disable_vpp;
234 disable_vpp = g_value_get_boolean (value);
235 if (!disable_vpp && !vaapidecbin->has_vpp)
236 GST_WARNING_OBJECT (vaapidecbin,
237 "Cannot enable VPP since the VA driver does not support it");
239 vaapidecbin->disable_vpp = disable_vpp;
241 /* @TODO: Add run-time disabling support */
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251 gst_vaapi_decode_bin_get_property (GObject * object,
252 guint prop_id, GValue * value, GParamSpec * pspec)
254 GstVaapiDecodeBin *vaapidecbin = GST_VAAPI_DECODE_BIN (object);
257 case PROP_MAX_SIZE_BYTES:
258 g_value_set_uint (value, vaapidecbin->max_size_bytes);
260 case PROP_MAX_SIZE_BUFFERS:
261 g_value_set_uint (value, vaapidecbin->max_size_buffers);
263 case PROP_MAX_SIZE_TIME:
264 g_value_set_uint64 (value, vaapidecbin->max_size_time);
266 case PROP_DEINTERLACE_METHOD:
267 g_value_set_enum (value, vaapidecbin->deinterlace_method);
269 case PROP_DISABLE_VPP:
270 g_value_set_boolean (value, vaapidecbin->disable_vpp);
273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
279 gst_vaapi_decode_bin_handle_message (GstBin * bin, GstMessage * message)
281 GstVaapiDecodeBin *vaapidecbin = GST_VAAPI_DECODE_BIN (bin);
283 GstContext *context = NULL;
284 const gchar *context_type;
285 GstVaapiDisplay *display = NULL;
287 type = GST_MESSAGE_TYPE (message);
288 if (type != GST_MESSAGE_HAVE_CONTEXT)
290 gst_message_parse_have_context (message, &context);
293 context_type = gst_context_get_context_type (context);
294 if (g_strcmp0 (context_type, GST_VAAPI_DISPLAY_CONTEXT_TYPE_NAME) != 0)
296 if (!gst_vaapi_video_context_get_display (context, &display))
299 vaapidecbin->has_vpp = gst_vaapi_display_has_video_processing (display) ?
300 HAS_VPP_YES : HAS_VPP_NO;
302 /* the underlying VA driver implementation doesn't support video
303 * post-processing, hence we have to disable it */
304 if (vaapidecbin->has_vpp != HAS_VPP_YES) {
305 GST_WARNING_OBJECT (vaapidecbin, "VA driver doesn't support VPP");
306 if (!vaapidecbin->disable_vpp) {
307 vaapidecbin->disable_vpp = TRUE;
308 g_object_notify_by_pspec (G_OBJECT (vaapidecbin),
309 properties[PROP_DISABLE_VPP]);
313 activate_vpp (vaapidecbin);
316 GST_BIN_CLASS (gst_vaapi_decode_bin_parent_class)->handle_message (bin,
320 static GstStateChangeReturn
321 gst_vaapi_decode_bin_change_state (GstElement * element,
322 GstStateChange transition)
324 GstVaapiDecodeBin *vaapidecbin = GST_VAAPI_DECODE_BIN (element);
325 GstStateChangeReturn ret;
327 switch (transition) {
332 ret = GST_ELEMENT_CLASS (gst_vaapi_decode_bin_parent_class)->change_state
333 (element, transition);
334 if (ret == GST_STATE_CHANGE_FAILURE)
337 switch (transition) {
338 case GST_STATE_CHANGE_NULL_TO_READY:
339 if (!ensure_vpp (vaapidecbin))
340 return GST_STATE_CHANGE_FAILURE;
350 gst_vaapi_decode_bin_class_init (GstVaapiDecodeBinClass * klass)
352 GObjectClass *gobject_class;
353 GstElementClass *element_class;
354 GstBinClass *bin_class;
356 gobject_class = G_OBJECT_CLASS (klass);
357 element_class = GST_ELEMENT_CLASS (klass);
358 bin_class = GST_BIN_CLASS (klass);
360 gobject_class->set_property = gst_vaapi_decode_bin_set_property;
361 gobject_class->get_property = gst_vaapi_decode_bin_get_property;
363 element_class->change_state =
364 GST_DEBUG_FUNCPTR (gst_vaapi_decode_bin_change_state);
366 bin_class->handle_message =
367 GST_DEBUG_FUNCPTR (gst_vaapi_decode_bin_handle_message);
369 gst_element_class_set_static_metadata (element_class,
371 "Codec/Decoder/Video",
373 "Sreerenj Balachandran <sreerenj.balachandran@intel.com>");
375 properties[PROP_MAX_SIZE_BYTES] = g_param_spec_uint ("max-size-bytes",
376 "Max. size (kB)", "Max. amount of data in the queue (bytes, 0=disable)",
377 0, G_MAXUINT, DEFAULT_QUEUE_MAX_SIZE_BYTES,
378 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
379 properties[PROP_MAX_SIZE_BUFFERS] = g_param_spec_uint ("max-size-buffers",
380 "Max. size (buffers)", "Max. number of buffers in the queue (0=disable)",
381 0, G_MAXUINT, DEFAULT_QUEUE_MAX_SIZE_BUFFERS,
382 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
383 properties[PROP_MAX_SIZE_TIME] = g_param_spec_uint64 ("max-size-time",
384 "Max. size (ns)", "Max. amount of data in the queue (in ns, 0=disable)",
385 0, G_MAXUINT64, DEFAULT_QUEUE_MAX_SIZE_TIME,
386 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
387 properties[PROP_DEINTERLACE_METHOD] = g_param_spec_enum ("deinterlace-method",
388 "Deinterlace method", "Deinterlace method to use",
389 GST_VAAPI_TYPE_DEINTERLACE_METHOD, DEFAULT_DEINTERLACE_METHOD,
390 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
391 properties[PROP_DISABLE_VPP] = g_param_spec_boolean ("disable-vpp",
393 "Disable Video Post Processing (No support for run time disabling)",
394 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
396 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
398 gst_element_class_add_pad_template (element_class,
399 gst_static_pad_template_get (&gst_vaapi_decode_bin_sink_factory));
401 gst_element_class_add_pad_template (element_class,
402 gst_static_pad_template_get (&gst_vaapi_decode_bin_src_factory));
404 GST_DEBUG_CATEGORY_INIT (gst_debug_vaapi_decode_bin,
405 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
409 gst_vaapi_decode_bin_configure (GstVaapiDecodeBin * vaapidecbin)
411 gchar *missing_factory = NULL;
413 /* create the decoder */
414 vaapidecbin->decoder =
415 gst_element_factory_make ("vaapidecode", "vaapidecode");
416 if (!vaapidecbin->decoder) {
417 missing_factory = "vaapidecode";
418 goto error_element_missing;
420 /* create the queue */
421 vaapidecbin->queue = gst_element_factory_make ("queue", "queue");
422 if (!vaapidecbin->queue) {
423 missing_factory = "queue";
424 goto error_element_missing;
427 g_object_set (G_OBJECT (vaapidecbin->queue),
428 "max-size-bytes", vaapidecbin->max_size_bytes,
429 "max-size-buffers", vaapidecbin->max_size_buffers,
430 "max-size-time", vaapidecbin->max_size_time, NULL);
432 gst_bin_add_many (GST_BIN (vaapidecbin),
433 vaapidecbin->decoder, vaapidecbin->queue, NULL);
435 if (!gst_element_link_pads_full (vaapidecbin->decoder, "src",
436 vaapidecbin->queue, "sink", GST_PAD_LINK_CHECK_NOTHING))
441 error_element_missing:
443 post_missing_element_message (vaapidecbin, missing_factory);
448 GST_ELEMENT_ERROR (vaapidecbin, CORE, PAD,
449 (NULL), ("Failed to configure the vaapidecodebin."));
455 gst_vaapi_decode_bin_init (GstVaapiDecodeBin * vaapidecbin)
457 GstPad *element_pad, *ghost_pad;
459 vaapidecbin->has_vpp = HAS_VPP_UNKNOWN;
461 if (!gst_vaapi_decode_bin_configure (vaapidecbin))
464 /* create ghost pad sink */
466 gst_element_get_static_pad (GST_ELEMENT (vaapidecbin->decoder), "sink");
468 gst_ghost_pad_new_from_template ("sink", element_pad,
469 GST_PAD_PAD_TEMPLATE (element_pad));
470 gst_object_unref (element_pad);
471 gst_element_add_pad (GST_ELEMENT (vaapidecbin), ghost_pad);