2 * gstvaapipostproc.c - VA-API video postprocessing
4 * Copyright (C) 2012 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
32 #include <gst/video/video.h>
33 #include <gst/video/videocontext.h>
34 #include <gst/vaapi/gstvaapivideosink.h>
35 #include <gst/vaapi/gstvaapivideobuffer.h>
37 #include "gstvaapipluginutil.h"
38 #include "gstvaapipostproc.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 /* ElementFactory information */
47 static const GstElementDetails gst_vaapipostproc_details =
49 "VA-API video postprocessing",
50 "Filter/Converter/Video",
52 "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");
54 /* Default templates */
55 static const char gst_vaapipostproc_sink_caps_str[] =
56 GST_VAAPI_SURFACE_CAPS ", "
57 "interlaced = (boolean) { true, false }";
59 static const char gst_vaapipostproc_src_caps_str[] =
60 GST_VAAPI_SURFACE_CAPS ", "
61 "interlaced = (boolean) false";
63 static GstStaticPadTemplate gst_vaapipostproc_sink_factory =
64 GST_STATIC_PAD_TEMPLATE(
68 GST_STATIC_CAPS(gst_vaapipostproc_sink_caps_str));
70 static GstStaticPadTemplate gst_vaapipostproc_src_factory =
71 GST_STATIC_PAD_TEMPLATE(
75 GST_STATIC_CAPS(gst_vaapipostproc_src_caps_str));
78 gst_vaapipostproc_implements_iface_init(GstImplementsInterfaceClass *iface);
81 gst_video_context_interface_init(GstVideoContextInterface *iface);
83 #define GstVideoContextClass GstVideoContextInterface
84 G_DEFINE_TYPE_WITH_CODE(
88 G_IMPLEMENT_INTERFACE(GST_TYPE_IMPLEMENTS_INTERFACE,
89 gst_vaapipostproc_implements_iface_init);
90 G_IMPLEMENT_INTERFACE(GST_TYPE_VIDEO_CONTEXT,
91 gst_video_context_interface_init));
96 PROP_DEINTERLACE_MODE,
97 PROP_DEINTERLACE_METHOD,
100 #define DEFAULT_DEINTERLACE_MODE GST_VAAPI_DEINTERLACE_MODE_AUTO
101 #define DEFAULT_DEINTERLACE_METHOD GST_VAAPI_DEINTERLACE_METHOD_BOB
103 #define GST_TYPE_VAAPI_DEINTERLACE_MODES \
104 gst_vaapi_deinterlace_modes_get_type()
107 gst_vaapi_deinterlace_modes_get_type(void)
109 static GType deinterlace_modes_type = 0;
111 static const GEnumValue modes_types[] = {
112 { GST_VAAPI_DEINTERLACE_MODE_AUTO,
113 "Auto detection", "auto" },
114 { GST_VAAPI_DEINTERLACE_MODE_INTERLACED,
115 "Force deinterlacing", "interlaced" },
116 { GST_VAAPI_DEINTERLACE_MODE_DISABLED,
117 "Never deinterlace", "disabled" },
121 if (!deinterlace_modes_type) {
122 deinterlace_modes_type =
123 g_enum_register_static("GstVaapiDeinterlaceModes", modes_types);
125 return deinterlace_modes_type;
128 #define GST_TYPE_VAAPI_DEINTERLACE_METHODS \
129 gst_vaapi_deinterlace_methods_get_type()
132 gst_vaapi_deinterlace_methods_get_type(void)
134 static GType deinterlace_methods_type = 0;
136 static const GEnumValue methods_types[] = {
137 { GST_VAAPI_DEINTERLACE_METHOD_BOB,
138 "Bob deinterlacing", "bob" },
141 { GST_VAAPI_DEINTERLACE_METHOD_WEAVE,
142 "Weave deinterlacing", "weave" },
143 { GST_VAAPI_DEINTERLACE_METHOD_MOTION_ADAPTIVE,
144 "Motion adaptive deinterlacing", "motion-adaptive" },
145 { GST_VAAPI_DEINTERLACE_METHOD_MOTION_COMPENSATED,
146 "Motion compensated deinterlacing", "motion-compensated" },
151 if (!deinterlace_methods_type) {
152 deinterlace_methods_type =
153 g_enum_register_static("GstVaapiDeinterlaceMethods", methods_types);
155 return deinterlace_methods_type;
158 static inline GstVaapiPostproc *
159 get_vaapipostproc_from_pad(GstPad *pad)
161 return GST_VAAPIPOSTPROC(gst_pad_get_parent_element(pad));
164 /* GstImplementsInterface interface */
167 gst_vaapipostproc_implements_interface_supported(
168 GstImplementsInterface *iface,
172 return (type == GST_TYPE_VIDEO_CONTEXT);
176 gst_vaapipostproc_implements_iface_init(GstImplementsInterfaceClass *iface)
178 iface->supported = gst_vaapipostproc_implements_interface_supported;
181 /* GstVideoContext interface */
184 gst_vaapipostproc_set_video_context(
185 GstVideoContext *context,
190 GstVaapiPostproc * const postproc = GST_VAAPIPOSTPROC(context);
192 gst_vaapi_set_display(type, value, &postproc->display);
196 gst_video_context_interface_init(GstVideoContextInterface *iface)
198 iface->set_context = gst_vaapipostproc_set_video_context;
202 gst_vaapipostproc_create(GstVaapiPostproc *postproc, GstCaps *caps)
204 if (!gst_vaapi_ensure_display(postproc, &postproc->display, NULL))
207 gst_caps_replace(&postproc->postproc_caps, caps);
212 gst_vaapipostproc_destroy(GstVaapiPostproc *postproc)
214 gst_caps_replace(&postproc->postproc_caps, NULL);
216 g_clear_object(&postproc->display);
220 gst_vaapipostproc_reset(GstVaapiPostproc *postproc, GstCaps *caps)
222 if (postproc->postproc_caps &&
223 gst_caps_is_always_compatible(caps, postproc->postproc_caps))
226 gst_vaapipostproc_destroy(postproc);
227 return gst_vaapipostproc_create(postproc, caps);
231 gst_vaapipostproc_start(GstVaapiPostproc *postproc)
233 if (!gst_vaapi_ensure_display(postproc, &postproc->display, NULL))
239 gst_vaapipostproc_stop(GstVaapiPostproc *postproc)
241 if (postproc->display) {
242 g_object_unref(postproc->display);
243 postproc->display = NULL;
249 gst_vaapipostproc_process(GstVaapiPostproc *postproc, GstBuffer *buf)
251 GstVaapiVideoBuffer *vbuf = GST_VAAPI_VIDEO_BUFFER(buf);
252 GstVaapiSurfaceProxy *proxy;
253 GstClockTime timestamp;
255 GstBuffer *outbuf = NULL;
256 guint outbuf_flags, flags;
257 gboolean interlaced, tff;
259 flags = gst_vaapi_video_buffer_get_render_flags(vbuf);
261 /* Deinterlacing disabled, push frame */
262 if (!postproc->deinterlace) {
263 gst_vaapi_video_buffer_set_render_flags(vbuf, flags);
264 ret = gst_pad_push(postproc->srcpad, buf);
265 if (ret != GST_FLOW_OK)
266 goto error_push_buffer;
270 timestamp = GST_BUFFER_TIMESTAMP(buf);
271 proxy = gst_vaapi_video_buffer_get_surface_proxy(vbuf);
272 interlaced = gst_vaapi_surface_proxy_get_interlaced(proxy);
273 tff = gst_vaapi_surface_proxy_get_tff(proxy);
275 flags &= ~(GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD|
276 GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD);
279 outbuf = gst_vaapi_video_buffer_new_with_surface_proxy(proxy);
281 goto error_create_buffer;
283 vbuf = GST_VAAPI_VIDEO_BUFFER(outbuf);
284 outbuf_flags = flags;
285 outbuf_flags |= interlaced ? (
287 GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD :
288 GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD) :
289 GST_VAAPI_PICTURE_STRUCTURE_FRAME;
290 gst_vaapi_video_buffer_set_render_flags(vbuf, outbuf_flags);
292 GST_BUFFER_TIMESTAMP(outbuf) = timestamp;
293 GST_BUFFER_DURATION(outbuf) = postproc->field_duration;
294 gst_buffer_set_caps(outbuf, postproc->srcpad_caps);
295 ret = gst_pad_push(postproc->srcpad, outbuf);
296 if (ret != GST_FLOW_OK)
297 goto error_push_buffer;
300 outbuf = gst_vaapi_video_buffer_new_with_surface_proxy(proxy);
302 goto error_create_buffer;
304 vbuf = GST_VAAPI_VIDEO_BUFFER(outbuf);
305 outbuf_flags = flags;
306 outbuf_flags |= interlaced ? (
308 GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD :
309 GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD) :
310 GST_VAAPI_PICTURE_STRUCTURE_FRAME;
311 gst_vaapi_video_buffer_set_render_flags(vbuf, outbuf_flags);
313 GST_BUFFER_TIMESTAMP(outbuf) = timestamp + postproc->field_duration;
314 GST_BUFFER_DURATION(outbuf) = postproc->field_duration;
315 gst_buffer_set_caps(outbuf, postproc->srcpad_caps);
316 ret = gst_pad_push(postproc->srcpad, outbuf);
317 if (ret != GST_FLOW_OK)
318 goto error_push_buffer;
320 gst_buffer_unref(buf);
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_details_simple(
648 gst_vaapipostproc_details.longname,
649 gst_vaapipostproc_details.klass,
650 gst_vaapipostproc_details.description,
651 gst_vaapipostproc_details.author
655 pad_template = gst_static_pad_template_get(&gst_vaapipostproc_sink_factory);
656 gst_element_class_add_pad_template(element_class, pad_template);
657 gst_object_unref(pad_template);
660 pad_template = gst_static_pad_template_get(&gst_vaapipostproc_src_factory);
661 gst_element_class_add_pad_template(element_class, pad_template);
662 gst_object_unref(pad_template);
665 * GstVaapiPostproc:deinterlace-mode:
667 * This selects whether the deinterlacing should always be applied or if
668 * they should only be applied on content that has the "interlaced" flag
671 g_object_class_install_property
673 PROP_DEINTERLACE_MODE,
674 g_param_spec_enum("deinterlace",
676 "Deinterlace mode to use",
677 GST_TYPE_VAAPI_DEINTERLACE_MODES,
678 DEFAULT_DEINTERLACE_MODE,
679 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
682 * GstVaapiPostproc:deinterlace-method:
684 * This selects the deinterlacing method to apply.
686 g_object_class_install_property
688 PROP_DEINTERLACE_METHOD,
689 g_param_spec_enum("deinterlace-method",
690 "Deinterlace method",
691 "Deinterlace method to use",
692 GST_TYPE_VAAPI_DEINTERLACE_METHODS,
693 DEFAULT_DEINTERLACE_METHOD,
694 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
698 gst_vaapipostproc_init(GstVaapiPostproc *postproc)
700 GstVaapiPostprocClass *klass = GST_VAAPIPOSTPROC_GET_CLASS(postproc);
701 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
703 postproc->allowed_caps = NULL;
704 postproc->postproc_caps = NULL;
705 postproc->display = NULL;
706 postproc->surface_width = 0;
707 postproc->surface_height = 0;
708 postproc->deinterlace = FALSE;
709 postproc->deinterlace_mode = DEFAULT_DEINTERLACE_MODE;
710 postproc->deinterlace_method = DEFAULT_DEINTERLACE_METHOD;
711 postproc->field_duration = GST_CLOCK_TIME_NONE;
715 /* Pad through which data comes in to the element */
716 postproc->sinkpad = gst_pad_new_from_template(
717 gst_element_class_get_pad_template(element_class, "sink"),
720 postproc->sinkpad_caps = NULL;
722 gst_pad_set_getcaps_function(postproc->sinkpad, gst_vaapipostproc_get_caps);
723 gst_pad_set_setcaps_function(postproc->sinkpad, gst_vaapipostproc_set_caps);
724 gst_pad_set_chain_function(postproc->sinkpad, gst_vaapipostproc_chain);
725 gst_pad_set_event_function(postproc->sinkpad, gst_vaapipostproc_sink_event);
726 gst_pad_set_query_function(postproc->sinkpad, gst_vaapipostproc_query);
727 gst_element_add_pad(GST_ELEMENT(postproc), postproc->sinkpad);
729 /* Pad through which data goes out of the element */
730 postproc->srcpad = gst_pad_new_from_template(
731 gst_element_class_get_pad_template(element_class, "src"),
734 postproc->srcpad_caps = NULL;
736 gst_pad_set_event_function(postproc->srcpad, gst_vaapipostproc_src_event);
737 gst_pad_set_query_function(postproc->srcpad, gst_vaapipostproc_query);
738 gst_element_add_pad(GST_ELEMENT(postproc), postproc->srcpad);