2 * gstvaapipostproc.c - VA-API video postprocessing
4 * Copyright (C) 2012-2013 Intel Corporation
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1
9 * of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
23 * SECTION:gstvaapipostproc
24 * @short_description: A video postprocessing filter
26 * vaapipostproc consists in various postprocessing algorithms to be
27 * applied to VA surfaces. So far, only basic bob deinterlacing is
31 #include "gst/vaapi/sysdeps.h"
32 #include <gst/video/video.h>
33 #include <gst/video/videocontext.h>
34 #include <gst/vaapi/gstvaapivideometa.h>
36 #include "gstvaapipostproc.h"
37 #include "gstvaapipluginutil.h"
38 #include "gstvaapipluginbuffer.h"
40 #define GST_PLUGIN_NAME "vaapipostproc"
41 #define GST_PLUGIN_DESC "A video postprocessing filter"
43 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapipostproc);
44 #define GST_CAT_DEFAULT gst_debug_vaapipostproc
46 /* Default templates */
47 static const char gst_vaapipostproc_sink_caps_str[] =
48 GST_VAAPI_SURFACE_CAPS ", "
49 "interlaced = (boolean) { true, false }";
51 static const char gst_vaapipostproc_src_caps_str[] =
52 GST_VAAPI_SURFACE_CAPS ", "
53 "interlaced = (boolean) false";
55 static GstStaticPadTemplate gst_vaapipostproc_sink_factory =
56 GST_STATIC_PAD_TEMPLATE(
60 GST_STATIC_CAPS(gst_vaapipostproc_sink_caps_str));
62 static GstStaticPadTemplate gst_vaapipostproc_src_factory =
63 GST_STATIC_PAD_TEMPLATE(
67 GST_STATIC_CAPS(gst_vaapipostproc_src_caps_str));
69 /* GstImplementsInterface interface */
71 gst_vaapipostproc_implements_interface_supported(
72 GstImplementsInterface *iface,
76 return (type == GST_TYPE_VIDEO_CONTEXT);
80 gst_vaapipostproc_implements_iface_init(GstImplementsInterfaceClass *iface)
82 iface->supported = gst_vaapipostproc_implements_interface_supported;
85 /* GstVideoContext interface */
87 gst_vaapipostproc_set_video_context(
88 GstVideoContext *context,
93 GstVaapiPostproc * const postproc = GST_VAAPIPOSTPROC(context);
95 gst_vaapi_set_display(type, value, &postproc->display);
99 gst_video_context_interface_init(GstVideoContextInterface *iface)
101 iface->set_context = gst_vaapipostproc_set_video_context;
104 #define GstVideoContextClass GstVideoContextInterface
105 G_DEFINE_TYPE_WITH_CODE(
109 G_IMPLEMENT_INTERFACE(GST_TYPE_IMPLEMENTS_INTERFACE,
110 gst_vaapipostproc_implements_iface_init);
111 G_IMPLEMENT_INTERFACE(GST_TYPE_VIDEO_CONTEXT,
112 gst_video_context_interface_init))
117 PROP_DEINTERLACE_MODE,
118 PROP_DEINTERLACE_METHOD,
121 #define DEFAULT_DEINTERLACE_MODE GST_VAAPI_DEINTERLACE_MODE_AUTO
122 #define DEFAULT_DEINTERLACE_METHOD GST_VAAPI_DEINTERLACE_METHOD_BOB
124 #define GST_VAAPI_TYPE_DEINTERLACE_MODE \
125 gst_vaapi_deinterlace_mode_get_type()
128 gst_vaapi_deinterlace_mode_get_type(void)
130 static GType deinterlace_mode_type = 0;
132 static const GEnumValue mode_types[] = {
133 { GST_VAAPI_DEINTERLACE_MODE_AUTO,
134 "Auto detection", "auto" },
135 { GST_VAAPI_DEINTERLACE_MODE_INTERLACED,
136 "Force deinterlacing", "interlaced" },
137 { GST_VAAPI_DEINTERLACE_MODE_DISABLED,
138 "Never deinterlace", "disabled" },
142 if (!deinterlace_mode_type) {
143 deinterlace_mode_type =
144 g_enum_register_static("GstVaapiDeinterlaceMode", mode_types);
146 return deinterlace_mode_type;
149 #define GST_VAAPI_TYPE_DEINTERLACE_METHOD \
150 gst_vaapi_deinterlace_method_get_type()
153 gst_vaapi_deinterlace_method_get_type(void)
155 static GType deinterlace_method_type = 0;
157 static const GEnumValue method_types[] = {
158 { GST_VAAPI_DEINTERLACE_METHOD_BOB,
159 "Bob deinterlacing", "bob" },
162 { GST_VAAPI_DEINTERLACE_METHOD_WEAVE,
163 "Weave deinterlacing", "weave" },
164 { GST_VAAPI_DEINTERLACE_METHOD_MOTION_ADAPTIVE,
165 "Motion adaptive deinterlacing", "motion-adaptive" },
166 { GST_VAAPI_DEINTERLACE_METHOD_MOTION_COMPENSATED,
167 "Motion compensated deinterlacing", "motion-compensated" },
172 if (!deinterlace_method_type) {
173 deinterlace_method_type =
174 g_enum_register_static("GstVaapiDeinterlaceMethod", method_types);
176 return deinterlace_method_type;
179 static inline GstVaapiPostproc *
180 get_vaapipostproc_from_pad(GstPad *pad)
182 return GST_VAAPIPOSTPROC(gst_pad_get_parent_element(pad));
185 static inline gboolean
186 gst_vaapipostproc_ensure_display(GstVaapiPostproc *postproc)
188 return gst_vaapi_ensure_display(postproc, GST_VAAPI_DISPLAY_TYPE_ANY,
193 gst_vaapipostproc_create(GstVaapiPostproc *postproc, GstCaps *caps)
195 if (!gst_vaapipostproc_ensure_display(postproc))
198 gst_caps_replace(&postproc->postproc_caps, caps);
203 gst_vaapipostproc_destroy(GstVaapiPostproc *postproc)
205 gst_caps_replace(&postproc->postproc_caps, NULL);
207 g_clear_object(&postproc->display);
211 gst_vaapipostproc_reset(GstVaapiPostproc *postproc, GstCaps *caps)
213 if (postproc->postproc_caps &&
214 gst_caps_is_always_compatible(caps, postproc->postproc_caps))
217 gst_vaapipostproc_destroy(postproc);
218 return gst_vaapipostproc_create(postproc, caps);
222 gst_vaapipostproc_start(GstVaapiPostproc *postproc)
224 if (!gst_vaapipostproc_ensure_display(postproc))
230 gst_vaapipostproc_stop(GstVaapiPostproc *postproc)
232 if (postproc->display) {
233 g_object_unref(postproc->display);
234 postproc->display = NULL;
240 gst_vaapipostproc_process(GstVaapiPostproc *postproc, GstBuffer *buf)
242 GstVaapiVideoMeta *meta;
243 GstVaapiSurfaceProxy *proxy;
244 GstClockTime timestamp;
246 GstBuffer *outbuf = NULL;
247 guint outbuf_flags, flags;
250 meta = gst_buffer_get_vaapi_video_meta(buf);
252 goto error_invalid_buffer;
254 flags = gst_vaapi_video_meta_get_render_flags(meta);
256 /* Deinterlacing disabled, push frame */
257 if (!postproc->deinterlace) {
258 gst_vaapi_video_meta_set_render_flags(meta, flags);
259 ret = gst_pad_push(postproc->srcpad, buf);
260 if (ret != GST_FLOW_OK)
261 goto error_push_buffer;
265 timestamp = GST_BUFFER_TIMESTAMP(buf);
266 proxy = gst_vaapi_video_meta_get_surface_proxy(meta);
267 tff = GST_BUFFER_FLAG_IS_SET(buf, GST_VIDEO_BUFFER_TFF);
269 flags &= ~(GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD|
270 GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD);
273 outbuf = gst_vaapi_video_buffer_new_with_surface_proxy(proxy);
275 goto error_create_buffer;
277 meta = gst_buffer_get_vaapi_video_meta(outbuf);
278 outbuf_flags = flags;
279 outbuf_flags |= postproc->deinterlace ? (
281 GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD :
282 GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD) :
283 GST_VAAPI_PICTURE_STRUCTURE_FRAME;
284 gst_vaapi_video_meta_set_render_flags(meta, outbuf_flags);
286 GST_BUFFER_TIMESTAMP(outbuf) = timestamp;
287 GST_BUFFER_DURATION(outbuf) = postproc->field_duration;
288 gst_buffer_set_caps(outbuf, postproc->srcpad_caps);
289 ret = gst_pad_push(postproc->srcpad, outbuf);
290 if (ret != GST_FLOW_OK)
291 goto error_push_buffer;
294 outbuf = gst_vaapi_video_buffer_new_with_surface_proxy(proxy);
296 goto error_create_buffer;
298 meta = gst_buffer_get_vaapi_video_meta(outbuf);
299 outbuf_flags = flags;
300 outbuf_flags |= postproc->deinterlace ? (
302 GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD :
303 GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD) :
304 GST_VAAPI_PICTURE_STRUCTURE_FRAME;
305 gst_vaapi_video_meta_set_render_flags(meta, outbuf_flags);
307 GST_BUFFER_TIMESTAMP(outbuf) = timestamp + postproc->field_duration;
308 GST_BUFFER_DURATION(outbuf) = postproc->field_duration;
309 gst_buffer_set_caps(outbuf, postproc->srcpad_caps);
310 ret = gst_pad_push(postproc->srcpad, outbuf);
311 if (ret != GST_FLOW_OK)
312 goto error_push_buffer;
314 gst_buffer_unref(buf);
318 error_invalid_buffer:
320 GST_ERROR("failed to receive a valid video buffer");
321 gst_buffer_unref(buf);
322 return GST_FLOW_UNEXPECTED;
326 GST_ERROR("failed to create output buffer");
327 gst_buffer_unref(buf);
328 return GST_FLOW_UNEXPECTED;
332 if (ret != GST_FLOW_WRONG_STATE)
333 GST_ERROR("failed to push output buffer to video sink");
334 gst_buffer_unref(buf);
335 return GST_FLOW_UNEXPECTED;
340 gst_vaapipostproc_update_sink_caps(GstVaapiPostproc *postproc, GstCaps *caps)
345 if (!gst_video_parse_caps_framerate(caps, &fps_n, &fps_d))
347 postproc->fps_n = fps_n;
348 postproc->fps_d = fps_d;
350 switch (postproc->deinterlace_mode) {
351 case GST_VAAPI_DEINTERLACE_MODE_AUTO:
352 if (!gst_video_format_parse_caps_interlaced(caps, &interlaced))
354 postproc->deinterlace = interlaced;
356 case GST_VAAPI_DEINTERLACE_MODE_INTERLACED:
357 postproc->deinterlace = TRUE;
359 case GST_VAAPI_DEINTERLACE_MODE_DISABLED:
360 postproc->deinterlace = FALSE;
364 postproc->field_duration = gst_util_uint64_scale(
367 (1 + postproc->deinterlace) * postproc->fps_n
370 gst_caps_replace(&postproc->sinkpad_caps, caps);
375 gst_vaapipostproc_update_src_caps(GstVaapiPostproc *postproc, GstCaps *caps)
378 GstStructure *structure;
379 const GValue *v_width, *v_height, *v_par;
382 if (postproc->srcpad_caps)
383 src_caps = gst_caps_make_writable(postproc->srcpad_caps);
385 src_caps = gst_caps_from_string(GST_VAAPI_SURFACE_CAPS_NAME);
388 postproc->srcpad_caps = src_caps;
390 structure = gst_caps_get_structure(caps, 0);
391 v_width = gst_structure_get_value(structure, "width");
392 v_height = gst_structure_get_value(structure, "height");
393 v_par = gst_structure_get_value(structure, "pixel-aspect-ratio");
395 structure = gst_caps_get_structure(src_caps, 0);
396 if (v_width && v_height) {
397 gst_structure_set_value(structure, "width", v_width);
398 gst_structure_set_value(structure, "height", v_height);
401 gst_structure_set_value(structure, "pixel-aspect-ratio", v_par);
403 gst_structure_set(structure, "type", G_TYPE_STRING, "vaapi", NULL);
404 gst_structure_set(structure, "opengl", G_TYPE_BOOLEAN, USE_GLX, NULL);
406 if (!postproc->deinterlace)
407 gst_structure_remove_field(structure, "interlaced");
409 /* Set double framerate in interlaced mode */
410 if (!gst_util_fraction_multiply(postproc->fps_n, postproc->fps_d,
417 "interlaced", G_TYPE_BOOLEAN, FALSE,
418 "framerate", GST_TYPE_FRACTION, fps_n, fps_d,
422 return gst_pad_set_caps(postproc->srcpad, src_caps);
426 gst_vaapipostproc_ensure_allowed_caps(GstVaapiPostproc *postproc)
428 if (postproc->allowed_caps)
431 postproc->allowed_caps =
432 gst_caps_from_string(gst_vaapipostproc_sink_caps_str);
433 if (!postproc->allowed_caps)
436 /* XXX: append VA/VPP filters */
441 gst_vaapipostproc_get_caps(GstPad *pad)
443 GstVaapiPostproc * const postproc = get_vaapipostproc_from_pad(pad);
446 if (gst_vaapipostproc_ensure_allowed_caps(postproc))
447 out_caps = gst_caps_ref(postproc->allowed_caps);
449 out_caps = gst_caps_new_empty();
451 gst_object_unref(postproc);
456 gst_vaapipostproc_set_caps(GstPad *pad, GstCaps *caps)
458 GstVaapiPostproc * const postproc = get_vaapipostproc_from_pad(pad);
459 gboolean success = FALSE;
461 g_return_val_if_fail(pad == postproc->sinkpad, FALSE);
464 if (!gst_vaapipostproc_update_sink_caps(postproc, caps))
466 if (!gst_vaapipostproc_update_src_caps(postproc, caps))
468 if (!gst_vaapipostproc_reset(postproc, postproc->sinkpad_caps))
472 gst_object_unref(postproc);
477 gst_vaapipostproc_chain(GstPad *pad, GstBuffer *buf)
479 GstVaapiPostproc * const postproc = get_vaapipostproc_from_pad(pad);
482 ret = gst_vaapipostproc_process(postproc, buf);
483 gst_object_unref(postproc);
488 gst_vaapipostproc_sink_event(GstPad *pad, GstEvent *event)
490 GstVaapiPostproc * const postproc = get_vaapipostproc_from_pad(pad);
493 GST_DEBUG("handle sink event '%s'", GST_EVENT_TYPE_NAME(event));
495 /* Propagate event downstream */
496 success = gst_pad_push_event(postproc->srcpad, event);
497 gst_object_unref(postproc);
502 gst_vaapipostproc_src_event(GstPad *pad, GstEvent *event)
504 GstVaapiPostproc * const postproc = get_vaapipostproc_from_pad(pad);
507 GST_DEBUG("handle src event '%s'", GST_EVENT_TYPE_NAME(event));
509 /* Propagate event upstream */
510 success = gst_pad_push_event(postproc->sinkpad, event);
511 gst_object_unref(postproc);
516 gst_vaapipostproc_query(GstPad *pad, GstQuery *query)
518 GstVaapiPostproc * const postproc = get_vaapipostproc_from_pad(pad);
521 GST_DEBUG("sharing display %p", postproc->display);
523 if (gst_vaapi_reply_to_query(query, postproc->display))
526 success = gst_pad_query_default(pad, query);
528 gst_object_unref(postproc);
533 gst_vaapipostproc_finalize(GObject *object)
535 GstVaapiPostproc * const postproc = GST_VAAPIPOSTPROC(object);
537 gst_vaapipostproc_destroy(postproc);
539 gst_caps_replace(&postproc->sinkpad_caps, NULL);
540 gst_caps_replace(&postproc->srcpad_caps, NULL);
541 gst_caps_replace(&postproc->allowed_caps, NULL);
543 G_OBJECT_CLASS(gst_vaapipostproc_parent_class)->finalize(object);
547 gst_vaapipostproc_set_property(
554 GstVaapiPostproc * const postproc = GST_VAAPIPOSTPROC(object);
557 case PROP_DEINTERLACE_MODE:
558 postproc->deinterlace_mode = g_value_get_enum(value);
560 case PROP_DEINTERLACE_METHOD:
561 postproc->deinterlace_method = g_value_get_enum(value);
564 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
570 gst_vaapipostproc_get_property(
577 GstVaapiPostproc * const postproc = GST_VAAPIPOSTPROC(object);
580 case PROP_DEINTERLACE_MODE:
581 g_value_set_enum(value, postproc->deinterlace_mode);
583 case PROP_DEINTERLACE_METHOD:
584 g_value_set_enum(value, postproc->deinterlace_method);
587 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
592 static GstStateChangeReturn
593 gst_vaapipostproc_change_state(GstElement *element, GstStateChange transition)
595 GstVaapiPostproc * const postproc = GST_VAAPIPOSTPROC(element);
596 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
598 switch (transition) {
599 case GST_STATE_CHANGE_NULL_TO_READY:
600 if (!gst_vaapipostproc_start(postproc))
601 return GST_STATE_CHANGE_FAILURE;
603 case GST_STATE_CHANGE_READY_TO_PAUSED:
605 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
611 ret = GST_ELEMENT_CLASS(gst_vaapipostproc_parent_class)->change_state(element, transition);
612 if (ret != GST_STATE_CHANGE_SUCCESS)
615 switch (transition) {
616 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
618 case GST_STATE_CHANGE_PAUSED_TO_READY:
620 case GST_STATE_CHANGE_READY_TO_NULL:
621 if (!gst_vaapipostproc_stop(postproc))
622 return GST_STATE_CHANGE_FAILURE;
627 return GST_STATE_CHANGE_SUCCESS;
631 gst_vaapipostproc_class_init(GstVaapiPostprocClass *klass)
633 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
634 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
635 GstPadTemplate *pad_template;
637 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapipostproc,
638 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
640 object_class->finalize = gst_vaapipostproc_finalize;
641 object_class->set_property = gst_vaapipostproc_set_property;
642 object_class->get_property = gst_vaapipostproc_get_property;
644 element_class->change_state = gst_vaapipostproc_change_state;
646 gst_element_class_set_static_metadata(element_class,
647 "VA-API video postprocessing",
648 "Filter/Converter/Video",
650 "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");
653 pad_template = gst_static_pad_template_get(&gst_vaapipostproc_sink_factory);
654 gst_element_class_add_pad_template(element_class, pad_template);
655 gst_object_unref(pad_template);
658 pad_template = gst_static_pad_template_get(&gst_vaapipostproc_src_factory);
659 gst_element_class_add_pad_template(element_class, pad_template);
660 gst_object_unref(pad_template);
663 * GstVaapiPostproc:deinterlace-mode:
665 * This selects whether the deinterlacing should always be applied or if
666 * they should only be applied on content that has the "interlaced" flag
669 g_object_class_install_property
671 PROP_DEINTERLACE_MODE,
672 g_param_spec_enum("deinterlace",
674 "Deinterlace mode to use",
675 GST_VAAPI_TYPE_DEINTERLACE_MODE,
676 DEFAULT_DEINTERLACE_MODE,
677 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
680 * GstVaapiPostproc:deinterlace-method:
682 * This selects the deinterlacing method to apply.
684 g_object_class_install_property
686 PROP_DEINTERLACE_METHOD,
687 g_param_spec_enum("deinterlace-method",
688 "Deinterlace method",
689 "Deinterlace method to use",
690 GST_VAAPI_TYPE_DEINTERLACE_METHOD,
691 DEFAULT_DEINTERLACE_METHOD,
692 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
696 gst_vaapipostproc_init(GstVaapiPostproc *postproc)
698 GstVaapiPostprocClass *klass = GST_VAAPIPOSTPROC_GET_CLASS(postproc);
699 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
701 postproc->allowed_caps = NULL;
702 postproc->postproc_caps = NULL;
703 postproc->display = NULL;
704 postproc->surface_width = 0;
705 postproc->surface_height = 0;
706 postproc->deinterlace = FALSE;
707 postproc->deinterlace_mode = DEFAULT_DEINTERLACE_MODE;
708 postproc->deinterlace_method = DEFAULT_DEINTERLACE_METHOD;
709 postproc->field_duration = GST_CLOCK_TIME_NONE;
713 /* Pad through which data comes in to the element */
714 postproc->sinkpad = gst_pad_new_from_template(
715 gst_element_class_get_pad_template(element_class, "sink"),
718 postproc->sinkpad_caps = NULL;
720 gst_pad_set_getcaps_function(postproc->sinkpad, gst_vaapipostproc_get_caps);
721 gst_pad_set_setcaps_function(postproc->sinkpad, gst_vaapipostproc_set_caps);
722 gst_pad_set_chain_function(postproc->sinkpad, gst_vaapipostproc_chain);
723 gst_pad_set_event_function(postproc->sinkpad, gst_vaapipostproc_sink_event);
724 gst_pad_set_query_function(postproc->sinkpad, gst_vaapipostproc_query);
725 gst_element_add_pad(GST_ELEMENT(postproc), postproc->sinkpad);
727 /* Pad through which data goes out of the element */
728 postproc->srcpad = gst_pad_new_from_template(
729 gst_element_class_get_pad_template(element_class, "src"),
732 postproc->srcpad_caps = NULL;
734 gst_pad_set_event_function(postproc->srcpad, gst_vaapipostproc_src_event);
735 gst_pad_set_query_function(postproc->srcpad, gst_vaapipostproc_query);
736 gst_element_add_pad(GST_ELEMENT(postproc), postproc->srcpad);