2 * gstvaapisink.c - VA-API video sink
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
5 * Author: Gwenole Beauchesne <gwenole.beauchesne@splitted-desktop.com>
6 * Copyright (C) 2011-2013 Intel Corporation
7 * Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA
26 * SECTION:gstvaapisink
27 * @short_description: A VA-API based videosink
29 * vaapisink renders video frames to a drawable (X #Window) on a local
30 * display using the Video Acceleration (VA) API. The element will
31 * create its own internal window and render into it.
34 #include "gst/vaapi/sysdeps.h"
36 #include <gst/video/video.h>
38 #include <gst/vaapi/gstvaapivalue.h>
40 # include <gst/vaapi/gstvaapidisplay_drm.h>
43 # include <gst/vaapi/gstvaapidisplay_x11.h>
44 # include <gst/vaapi/gstvaapiwindow_x11.h>
47 # include <gst/vaapi/gstvaapidisplay_glx.h>
48 # include <gst/vaapi/gstvaapiwindow_glx.h>
51 # include <gst/vaapi/gstvaapidisplay_wayland.h>
52 # include <gst/vaapi/gstvaapiwindow_wayland.h>
55 /* Supported interfaces */
56 #if GST_CHECK_VERSION(1,0,0)
57 # include <gst/video/videooverlay.h>
59 # include <gst/interfaces/xoverlay.h>
61 # define GST_TYPE_VIDEO_OVERLAY GST_TYPE_X_OVERLAY
62 # define GST_VIDEO_OVERLAY GST_X_OVERLAY
63 # define GstVideoOverlay GstXOverlay
64 # define GstVideoOverlayInterface GstXOverlayClass
66 # define gst_video_overlay_prepare_window_handle(sink) \
67 gst_x_overlay_prepare_xwindow_id(sink)
68 # define gst_video_overlay_got_window_handle(sink, window_handle) \
69 gst_x_overlay_got_window_handle(sink, window_handle)
72 #include "gstvaapisink.h"
73 #include "gstvaapipluginutil.h"
74 #include "gstvaapivideometa.h"
75 #if GST_CHECK_VERSION(1,0,0)
76 #include "gstvaapivideobufferpool.h"
77 #include "gstvaapivideomemory.h"
80 #define GST_PLUGIN_NAME "vaapisink"
81 #define GST_PLUGIN_DESC "A VA-API based videosink"
83 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapisink);
84 #define GST_CAT_DEFAULT gst_debug_vaapisink
86 /* Default template */
87 static const char gst_vaapisink_sink_caps_str[] =
88 #if GST_CHECK_VERSION(1,1,0)
89 GST_VIDEO_CAPS_MAKE_WITH_FEATURES(
90 GST_CAPS_FEATURE_MEMORY_VAAPI_SURFACE, "{ ENCODED, NV12, I420, YV12 }");
92 #if GST_CHECK_VERSION(1,0,0)
93 GST_VIDEO_CAPS_MAKE(GST_VIDEO_FORMATS_ALL) "; "
96 "width = (int) [ 1, MAX ], "
97 "height = (int) [ 1, MAX ]; "
99 GST_VAAPI_SURFACE_CAPS;
102 static GstStaticPadTemplate gst_vaapisink_sink_factory =
103 GST_STATIC_PAD_TEMPLATE(
107 GST_STATIC_CAPS(gst_vaapisink_sink_caps_str));
110 gst_vaapisink_has_interface(GstVaapiPluginBase *plugin, GType type)
112 return type == GST_TYPE_VIDEO_OVERLAY;
116 gst_vaapisink_video_overlay_iface_init(GstVideoOverlayInterface *iface);
118 G_DEFINE_TYPE_WITH_CODE(
122 GST_VAAPI_PLUGIN_BASE_INIT_INTERFACES
123 G_IMPLEMENT_INTERFACE(GST_TYPE_VIDEO_OVERLAY,
124 gst_vaapisink_video_overlay_iface_init))
135 PROP_FORCE_ASPECT_RATIO,
138 #define DEFAULT_DISPLAY_TYPE GST_VAAPI_DISPLAY_TYPE_ANY
139 #define DEFAULT_ROTATION GST_VAAPI_ROTATION_0
141 /* GstVideoOverlay interface */
145 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id);
149 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *buffer);
152 gst_vaapisink_video_overlay_set_window_handle(GstVideoOverlay *overlay,
155 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
156 GstVaapiDisplayType display_type = GST_VAAPI_PLUGIN_BASE_DISPLAY_TYPE(sink);
158 /* Disable GLX rendering when vaapisink is using a foreign X
159 window. It's pretty much useless */
160 if (display_type == GST_VAAPI_DISPLAY_TYPE_GLX) {
161 display_type = GST_VAAPI_DISPLAY_TYPE_X11;
162 gst_vaapi_plugin_base_set_display_type(GST_VAAPI_PLUGIN_BASE(sink),
166 sink->foreign_window = TRUE;
168 switch (display_type) {
170 case GST_VAAPI_DISPLAY_TYPE_X11:
171 gst_vaapisink_ensure_window_xid(sink, window);
180 gst_vaapisink_video_overlay_set_render_rectangle(
181 GstVideoOverlay *overlay,
188 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
189 GstVaapiRectangle * const display_rect = &sink->display_rect;
193 display_rect->width = width;
194 display_rect->height = height;
196 GST_DEBUG("render rect (%d,%d):%ux%u",
197 display_rect->x, display_rect->y,
198 display_rect->width, display_rect->height);
202 gst_vaapisink_video_overlay_expose(GstVideoOverlay *overlay)
204 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
205 GstBaseSink * const base_sink = GST_BASE_SINK(overlay);
208 if (sink->use_overlay)
209 buffer = sink->video_buffer ? gst_buffer_ref(sink->video_buffer) : NULL;
211 #if GST_CHECK_VERSION(1,0,0)
212 GstSample * const sample = gst_base_sink_get_last_sample(base_sink);
215 buffer = gst_buffer_ref(gst_sample_get_buffer(sample));
216 gst_sample_unref(sample);
218 buffer = gst_base_sink_get_last_buffer(base_sink);
222 gst_vaapisink_show_frame(base_sink, buffer);
223 gst_buffer_unref(buffer);
228 gst_vaapisink_video_overlay_iface_init(GstVideoOverlayInterface *iface)
230 iface->set_window_handle = gst_vaapisink_video_overlay_set_window_handle;
231 iface->set_render_rectangle = gst_vaapisink_video_overlay_set_render_rectangle;
232 iface->expose = gst_vaapisink_video_overlay_expose;
236 gst_vaapisink_destroy(GstVaapiSink *sink)
238 gst_buffer_replace(&sink->video_buffer, NULL);
240 gst_vaapi_texture_replace(&sink->texture, NULL);
242 gst_caps_replace(&sink->caps, NULL);
246 /* Checks whether a ConfigureNotify event is in the queue */
247 typedef struct _ConfigureNotifyEventPendingArgs ConfigureNotifyEventPendingArgs;
248 struct _ConfigureNotifyEventPendingArgs {
256 configure_notify_event_pending_cb(Display *dpy, XEvent *xev, XPointer arg)
258 ConfigureNotifyEventPendingArgs * const args =
259 (ConfigureNotifyEventPendingArgs *)arg;
261 if (xev->type == ConfigureNotify &&
262 xev->xconfigure.window == args->window &&
263 xev->xconfigure.width == args->width &&
264 xev->xconfigure.height == args->height)
267 /* XXX: this is a hack to traverse the whole queue because we
268 can't use XPeekIfEvent() since it could block */
273 configure_notify_event_pending(
280 GstVaapiDisplayX11 * const display =
281 GST_VAAPI_DISPLAY_X11(GST_VAAPI_PLUGIN_BASE_DISPLAY(sink));
282 ConfigureNotifyEventPendingArgs args;
285 args.window = window;
287 args.height = height;
290 /* XXX: don't use XPeekIfEvent() because it might block */
292 gst_vaapi_display_x11_get_display(display),
294 configure_notify_event_pending_cb, (XPointer)&args
301 get_display_type_name(GstVaapiDisplayType display_type)
303 gpointer const klass = g_type_class_peek(GST_VAAPI_TYPE_DISPLAY_TYPE);
304 GEnumValue * const e = g_enum_get_value(klass, display_type);
307 return e->value_name;
308 return "<unknown-type>";
311 static inline gboolean
312 gst_vaapisink_ensure_display(GstVaapiSink *sink)
314 return gst_vaapi_plugin_base_ensure_display(GST_VAAPI_PLUGIN_BASE(sink));
318 gst_vaapisink_display_changed(GstVaapiPluginBase *plugin)
320 GstVaapiSink * const sink = GST_VAAPISINK(plugin);
321 GstVaapiRenderMode render_mode;
323 GST_INFO("created %s %p", get_display_type_name(plugin->display_type),
327 gst_vaapi_display_get_render_mode(plugin->display, &render_mode) &&
328 render_mode == GST_VAAPI_RENDER_MODE_OVERLAY;
329 GST_DEBUG("use %s rendering mode",
330 sink->use_overlay ? "overlay" : "texture");
332 sink->use_rotation = gst_vaapi_display_has_property(plugin->display,
333 GST_VAAPI_DISPLAY_PROP_ROTATION);
337 gst_vaapisink_ensure_uploader(GstVaapiSink *sink)
339 if (!gst_vaapisink_ensure_display(sink))
341 if (!gst_vaapi_plugin_base_ensure_uploader(GST_VAAPI_PLUGIN_BASE(sink)))
347 gst_vaapisink_ensure_render_rect(GstVaapiSink *sink, guint width, guint height)
349 GstVaapiRectangle * const display_rect = &sink->display_rect;
350 guint num, den, display_par_n, display_par_d;
353 /* Return success if caps are not set yet */
357 if (!sink->keep_aspect) {
358 display_rect->width = width;
359 display_rect->height = height;
363 GST_DEBUG("force-aspect-ratio is false; distorting while scaling video");
364 GST_DEBUG("render rect (%d,%d):%ux%u",
365 display_rect->x, display_rect->y,
366 display_rect->width, display_rect->height);
370 GST_DEBUG("ensure render rect within %ux%u bounds", width, height);
372 gst_vaapi_display_get_pixel_aspect_ratio(
373 GST_VAAPI_PLUGIN_BASE_DISPLAY(sink),
374 &display_par_n, &display_par_d
376 GST_DEBUG("display pixel-aspect-ratio %d/%d",
377 display_par_n, display_par_d);
379 success = gst_video_calculate_display_ratio(
381 sink->video_width, sink->video_height,
382 sink->video_par_n, sink->video_par_d,
383 display_par_n, display_par_d
387 GST_DEBUG("video size %dx%d, calculated ratio %d/%d",
388 sink->video_width, sink->video_height, num, den);
390 display_rect->width = gst_util_uint64_scale_int(height, num, den);
391 if (display_rect->width <= width) {
392 GST_DEBUG("keeping window height");
393 display_rect->height = height;
396 GST_DEBUG("keeping window width");
397 display_rect->width = width;
398 display_rect->height =
399 gst_util_uint64_scale_int(width, den, num);
401 GST_DEBUG("scaling video to %ux%u", display_rect->width, display_rect->height);
403 g_assert(display_rect->width <= width);
404 g_assert(display_rect->height <= height);
406 display_rect->x = (width - display_rect->width) / 2;
407 display_rect->y = (height - display_rect->height) / 2;
409 GST_DEBUG("render rect (%d,%d):%ux%u",
410 display_rect->x, display_rect->y,
411 display_rect->width, display_rect->height);
416 gst_vaapisink_ensure_window_size(GstVaapiSink *sink, guint *pwidth, guint *pheight)
418 GstVaapiDisplay * const display = GST_VAAPI_PLUGIN_BASE_DISPLAY(sink);
419 GstVideoRectangle src_rect, dst_rect, out_rect;
420 guint num, den, display_width, display_height, display_par_n, display_par_d;
421 gboolean success, scale;
423 if (sink->foreign_window) {
424 *pwidth = sink->window_width;
425 *pheight = sink->window_height;
429 gst_vaapi_display_get_size(display, &display_width, &display_height);
430 if (sink->fullscreen) {
431 *pwidth = display_width;
432 *pheight = display_height;
436 gst_vaapi_display_get_pixel_aspect_ratio(
438 &display_par_n, &display_par_d
441 success = gst_video_calculate_display_ratio(
443 sink->video_width, sink->video_height,
444 sink->video_par_n, sink->video_par_d,
445 display_par_n, display_par_d
448 num = sink->video_par_n;
449 den = sink->video_par_d;
454 src_rect.w = gst_util_uint64_scale_int(sink->video_height, num, den);
455 src_rect.h = sink->video_height;
458 dst_rect.w = display_width;
459 dst_rect.h = display_height;
460 scale = (src_rect.w > dst_rect.w || src_rect.h > dst_rect.h);
461 gst_video_sink_center_rect(src_rect, dst_rect, &out_rect, scale);
462 *pwidth = out_rect.w;
463 *pheight = out_rect.h;
466 static inline gboolean
467 gst_vaapisink_ensure_window(GstVaapiSink *sink, guint width, guint height)
469 GstVaapiDisplay * const display = GST_VAAPI_PLUGIN_BASE_DISPLAY(sink);
472 const GstVaapiDisplayType display_type =
473 GST_VAAPI_PLUGIN_BASE_DISPLAY_TYPE(sink);
474 switch (display_type) {
476 case GST_VAAPI_DISPLAY_TYPE_GLX:
477 sink->window = gst_vaapi_window_glx_new(display, width, height);
478 goto notify_video_overlay_interface;
481 case GST_VAAPI_DISPLAY_TYPE_X11:
482 sink->window = gst_vaapi_window_x11_new(display, width, height);
483 notify_video_overlay_interface:
486 gst_video_overlay_got_window_handle(
487 GST_VIDEO_OVERLAY(sink),
488 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window))
493 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
494 sink->window = gst_vaapi_window_wayland_new(display, width, height);
498 GST_ERROR("unsupported display type %d", display_type);
502 return sink->window != NULL;
507 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id)
509 GstVaapiDisplay *display;
511 unsigned int width, height, border_width, depth;
515 if (!gst_vaapisink_ensure_display(sink))
517 display = GST_VAAPI_PLUGIN_BASE_DISPLAY(sink);
519 gst_vaapi_display_lock(display);
521 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(display)),
524 &x, &y, &width, &height, &border_width, &depth
526 gst_vaapi_display_unlock(display);
528 if ((width != sink->window_width || height != sink->window_height) &&
529 !configure_notify_event_pending(sink, xid, width, height)) {
530 if (!gst_vaapisink_ensure_render_rect(sink, width, height))
532 sink->window_width = width;
533 sink->window_height = height;
537 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window)) == xid)
540 gst_vaapi_window_replace(&sink->window, NULL);
542 switch (GST_VAAPI_PLUGIN_BASE_DISPLAY_TYPE(sink)) {
544 case GST_VAAPI_DISPLAY_TYPE_GLX:
545 sink->window = gst_vaapi_window_glx_new_with_xid(display, xid);
548 case GST_VAAPI_DISPLAY_TYPE_X11:
549 sink->window = gst_vaapi_window_x11_new_with_xid(display, xid);
552 GST_ERROR("unsupported display type %d",
553 GST_VAAPI_PLUGIN_BASE_DISPLAY_TYPE(sink));
556 return sink->window != NULL;
561 gst_vaapisink_ensure_rotation(GstVaapiSink *sink, gboolean recalc_display_rect)
563 GstVaapiDisplay * const display = GST_VAAPI_PLUGIN_BASE_DISPLAY(sink);
564 gboolean success = FALSE;
566 g_return_val_if_fail(display, FALSE);
568 if (sink->rotation == sink->rotation_req)
571 if (!sink->use_rotation) {
572 GST_WARNING("VA display does not support rotation");
576 gst_vaapi_display_lock(display);
577 success = gst_vaapi_display_set_rotation(display, sink->rotation_req);
578 gst_vaapi_display_unlock(display);
580 GST_ERROR("failed to change VA display rotation mode");
584 if (((sink->rotation + sink->rotation_req) % 180) == 90) {
585 /* Orientation changed */
586 G_PRIMITIVE_SWAP(guint, sink->video_width, sink->video_height);
587 G_PRIMITIVE_SWAP(gint, sink->video_par_n, sink->video_par_d);
590 if (recalc_display_rect && !sink->foreign_window)
591 gst_vaapisink_ensure_render_rect(sink, sink->window_width,
592 sink->window_height);
596 sink->rotation = sink->rotation_req;
601 gst_vaapisink_start(GstBaseSink *base_sink)
603 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
605 if (!gst_vaapi_plugin_base_open(GST_VAAPI_PLUGIN_BASE(sink)))
607 return gst_vaapisink_ensure_uploader(sink);
611 gst_vaapisink_stop(GstBaseSink *base_sink)
613 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
615 gst_buffer_replace(&sink->video_buffer, NULL);
616 #if GST_CHECK_VERSION(1,0,0)
617 g_clear_object(&sink->video_buffer_pool);
619 gst_vaapi_window_replace(&sink->window, NULL);
621 gst_vaapi_plugin_base_close(GST_VAAPI_PLUGIN_BASE(sink));
626 gst_vaapisink_get_caps_impl(GstBaseSink *base_sink)
628 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
629 GstCaps *out_caps, *yuv_caps;
631 #if GST_CHECK_VERSION(1,1,0)
632 out_caps = gst_static_pad_template_get_caps(&gst_vaapisink_sink_factory);
634 out_caps = gst_caps_from_string(GST_VAAPI_SURFACE_CAPS);
639 if (gst_vaapisink_ensure_uploader(sink)) {
640 yuv_caps = GST_VAAPI_PLUGIN_BASE_UPLOADER_CAPS(sink);
642 out_caps = gst_caps_make_writable(out_caps);
643 gst_caps_append(out_caps, gst_caps_copy(yuv_caps));
649 #if GST_CHECK_VERSION(1,0,0)
650 static inline GstCaps *
651 gst_vaapisink_get_caps(GstBaseSink *base_sink, GstCaps *filter)
653 GstCaps *caps, *out_caps;
655 caps = gst_vaapisink_get_caps_impl(base_sink);
656 if (caps && filter) {
657 out_caps = gst_caps_intersect_full(caps, filter,
658 GST_CAPS_INTERSECT_FIRST);
659 gst_caps_unref(caps);
666 #define gst_vaapisink_get_caps gst_vaapisink_get_caps_impl
670 gst_vaapisink_set_caps(GstBaseSink *base_sink, GstCaps *caps)
672 GstVaapiPluginBase * const plugin = GST_VAAPI_PLUGIN_BASE(base_sink);
673 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
674 GstVideoInfo * const vip = GST_VAAPI_PLUGIN_BASE_SINK_PAD_INFO(sink);
675 GstVaapiDisplay *display;
676 guint win_width, win_height;
678 if (!gst_vaapisink_ensure_display(sink))
680 display = GST_VAAPI_PLUGIN_BASE_DISPLAY(sink);
683 if (GST_VAAPI_PLUGIN_BASE_DISPLAY_TYPE(sink) == GST_VAAPI_DISPLAY_TYPE_DRM)
687 if (!gst_vaapi_plugin_base_set_caps(plugin, caps, NULL))
690 sink->video_width = GST_VIDEO_INFO_WIDTH(vip);
691 sink->video_height = GST_VIDEO_INFO_HEIGHT(vip);
692 sink->video_par_n = GST_VIDEO_INFO_PAR_N(vip);
693 sink->video_par_d = GST_VIDEO_INFO_PAR_D(vip);
694 GST_DEBUG("video pixel-aspect-ratio %d/%d",
695 sink->video_par_n, sink->video_par_d);
697 gst_caps_replace(&sink->caps, caps);
699 gst_vaapisink_ensure_rotation(sink, FALSE);
701 gst_vaapisink_ensure_window_size(sink, &win_width, &win_height);
703 if (!sink->foreign_window || sink->fullscreen)
704 gst_vaapi_window_set_size(sink->window, win_width, win_height);
707 gst_vaapi_display_lock(display);
708 gst_video_overlay_prepare_window_handle(GST_VIDEO_OVERLAY(sink));
709 gst_vaapi_display_unlock(display);
712 if (!gst_vaapisink_ensure_window(sink, win_width, win_height))
714 gst_vaapi_window_set_fullscreen(sink->window, sink->fullscreen);
715 gst_vaapi_window_show(sink->window);
716 gst_vaapi_window_get_size(sink->window, &win_width, &win_height);
718 sink->window_width = win_width;
719 sink->window_height = win_height;
720 GST_DEBUG("window size %ux%u", win_width, win_height);
722 return gst_vaapisink_ensure_render_rect(sink, win_width, win_height);
727 render_background(GstVaapiSink *sink)
729 /* Original code from Mirco Muller (MacSlow):
730 <http://cgit.freedesktop.org/~macslow/gl-gst-player/> */
731 GLfloat fStartX = 0.0f;
732 GLfloat fStartY = 0.0f;
733 GLfloat fWidth = (GLfloat)sink->window_width;
734 GLfloat fHeight = (GLfloat)sink->window_height;
736 glClear(GL_COLOR_BUFFER_BIT);
739 /* top third, darker grey to white */
740 glColor3f(0.85f, 0.85f, 0.85f);
741 glVertex3f(fStartX, fStartY, 0.0f);
742 glColor3f(0.85f, 0.85f, 0.85f);
743 glVertex3f(fStartX + fWidth, fStartY, 0.0f);
744 glColor3f(1.0f, 1.0f, 1.0f);
745 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
746 glColor3f(1.0f, 1.0f, 1.0f);
747 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
749 /* middle third, just plain white */
750 glColor3f(1.0f, 1.0f, 1.0f);
751 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
752 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
753 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
754 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
756 /* bottom third, white to lighter grey */
757 glColor3f(1.0f, 1.0f, 1.0f);
758 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
759 glColor3f(1.0f, 1.0f, 1.0f);
760 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
761 glColor3f(0.62f, 0.66f, 0.69f);
762 glVertex3f(fStartX + fWidth, fStartY + fHeight, 0.0f);
763 glColor3f(0.62f, 0.66f, 0.69f);
764 glVertex3f(fStartX, fStartY + fHeight, 0.0f);
770 render_frame(GstVaapiSink *sink, GstVaapiSurface *surface,
771 const GstVaapiRectangle *surface_rect)
773 const guint x1 = sink->display_rect.x;
774 const guint x2 = sink->display_rect.x + sink->display_rect.width;
775 const guint y1 = sink->display_rect.y;
776 const guint y2 = sink->display_rect.y + sink->display_rect.height;
777 gfloat tx1, tx2, ty1, ty2;
781 gst_vaapi_surface_get_size(surface, &width, &height);
782 tx1 = (gfloat)surface_rect->x / width;
783 ty1 = (gfloat)surface_rect->y / height;
784 tx2 = (gfloat)(surface_rect->x + surface_rect->width) / width;
785 ty2 = (gfloat)(surface_rect->y + surface_rect->height) / height;
794 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
797 glTexCoord2f(tx1, ty1); glVertex2i(x1, y1);
798 glTexCoord2f(tx1, ty2); glVertex2i(x1, y2);
799 glTexCoord2f(tx2, ty2); glVertex2i(x2, y2);
800 glTexCoord2f(tx2, ty1); glVertex2i(x2, y1);
806 render_reflection(GstVaapiSink *sink)
808 const guint x1 = sink->display_rect.x;
809 const guint x2 = sink->display_rect.x + sink->display_rect.width;
810 const guint y1 = sink->display_rect.y;
811 const guint rh = sink->display_rect.height / 5;
812 GLfloat ry = 1.0f - (GLfloat)rh / (GLfloat)sink->display_rect.height;
816 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
817 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y1);
818 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y1);
820 glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
821 glTexCoord2f(1.0f, ry); glVertex2i(x2, y1 + rh);
822 glTexCoord2f(0.0f, ry); glVertex2i(x1, y1 + rh);
828 gst_vaapisink_ensure_texture(GstVaapiSink *sink, GstVaapiSurface *surface)
830 GstVaapiDisplay * display = GST_VAAPI_PLUGIN_BASE_DISPLAY(sink);
831 GstVideoRectangle tex_rect, dis_rect, out_rect;
837 gst_vaapi_surface_get_size(surface, &width, &height);
843 gst_vaapi_display_get_size(display, &width, &height);
849 gst_video_sink_center_rect(tex_rect, dis_rect, &out_rect, TRUE);
851 /* XXX: use surface size for now since some VA drivers have issues
852 with downscaling to the provided texture size. i.e. we should be
853 using the resulting out_rect size, which preserves the aspect
854 ratio of the surface */
857 GST_INFO("texture size %ux%u", width, height);
859 sink->texture = gst_vaapi_texture_new(display,
860 GL_TEXTURE_2D, GL_BGRA, width, height);
861 return sink->texture != NULL;
865 gst_vaapisink_show_frame_glx(
867 GstVaapiSurface *surface,
868 const GstVaapiRectangle *surface_rect,
872 GstVaapiWindowGLX * const window = GST_VAAPI_WINDOW_GLX(sink->window);
876 gst_vaapi_window_glx_make_current(window);
877 if (!gst_vaapisink_ensure_texture(sink, surface))
878 goto error_create_texture;
879 if (!gst_vaapi_texture_put_surface(sink->texture, surface, flags))
880 goto error_transfer_surface;
882 target = gst_vaapi_texture_get_target(sink->texture);
883 texture = gst_vaapi_texture_get_id(sink->texture);
884 if (target != GL_TEXTURE_2D || !texture)
887 if (sink->use_reflection)
888 render_background(sink);
891 glBindTexture(target, texture);
893 if (sink->use_reflection) {
895 glRotatef(20.0f, 0.0f, 1.0f, 0.0f);
896 glTranslatef(50.0f, 0.0f, 0.0f);
898 render_frame(sink, surface, surface_rect);
899 if (sink->use_reflection) {
901 glTranslatef(0.0, (GLfloat)sink->display_rect.height + 5.0f, 0.0f);
902 render_reflection(sink);
907 glBindTexture(target, 0);
909 gst_vaapi_window_glx_swap_buffers(window);
913 error_create_texture:
915 GST_DEBUG("could not create VA/GLX texture");
918 error_transfer_surface:
920 GST_DEBUG("could not transfer VA surface to texture");
926 static inline gboolean
927 gst_vaapisink_put_surface(
929 GstVaapiSurface *surface,
930 const GstVaapiRectangle *surface_rect,
934 if (!gst_vaapi_window_put_surface(sink->window, surface,
935 surface_rect, &sink->display_rect, flags)) {
936 GST_DEBUG("could not render VA surface");
943 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *src_buffer)
945 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
946 GstVaapiVideoMeta *meta;
947 GstVaapiSurface *surface;
951 GstVaapiRectangle *surface_rect = NULL;
952 #if GST_CHECK_VERSION(1,0,0)
953 GstVaapiRectangle tmp_rect;
957 #if GST_CHECK_VERSION(1,0,0)
958 GstVideoCropMeta * const crop_meta =
959 gst_buffer_get_video_crop_meta(src_buffer);
961 surface_rect = &tmp_rect;
962 surface_rect->x = crop_meta->x;
963 surface_rect->y = crop_meta->y;
964 surface_rect->width = crop_meta->width;
965 surface_rect->height = crop_meta->height;
969 ret = gst_vaapi_plugin_base_get_input_buffer(GST_VAAPI_PLUGIN_BASE(sink),
970 src_buffer, &buffer);
971 if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_SUPPORTED)
974 meta = gst_buffer_get_vaapi_video_meta(buffer);
975 GST_VAAPI_PLUGIN_BASE_DISPLAY_REPLACE(sink,
976 gst_vaapi_video_meta_get_display(meta));
981 gst_vaapisink_ensure_rotation(sink, TRUE);
983 surface = gst_vaapi_video_meta_get_surface(meta);
987 GST_DEBUG("render surface %" GST_VAAPI_ID_FORMAT,
988 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surface)));
991 surface_rect = (GstVaapiRectangle *)
992 gst_vaapi_video_meta_get_render_rect(meta);
995 GST_DEBUG("render rect (%d,%d), size %ux%u",
996 surface_rect->x, surface_rect->y,
997 surface_rect->width, surface_rect->height);
999 flags = gst_vaapi_video_meta_get_render_flags(meta);
1001 if (!gst_vaapi_apply_composition(surface, src_buffer))
1002 GST_WARNING("could not update subtitles");
1004 switch (GST_VAAPI_PLUGIN_BASE_DISPLAY_TYPE(sink)) {
1006 case GST_VAAPI_DISPLAY_TYPE_DRM:
1011 case GST_VAAPI_DISPLAY_TYPE_GLX:
1013 goto put_surface_x11;
1014 success = gst_vaapisink_show_frame_glx(sink, surface, surface_rect,
1019 case GST_VAAPI_DISPLAY_TYPE_X11:
1021 success = gst_vaapisink_put_surface(sink, surface, surface_rect, flags);
1025 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
1026 success = gst_vaapisink_put_surface(sink, surface, surface_rect, flags);
1030 GST_ERROR("unsupported display type %d",
1031 GST_VAAPI_PLUGIN_BASE_DISPLAY_TYPE(sink));
1038 /* Retain VA surface until the next one is displayed */
1039 if (sink->use_overlay)
1040 gst_buffer_replace(&sink->video_buffer, buffer);
1041 gst_buffer_unref(buffer);
1045 gst_buffer_unref(buffer);
1046 return GST_FLOW_EOS;
1049 #if GST_CHECK_VERSION(1,0,0)
1051 gst_vaapisink_propose_allocation(GstBaseSink *base_sink, GstQuery *query)
1053 GstVaapiPluginBase * const plugin = GST_VAAPI_PLUGIN_BASE(base_sink);
1055 if (!gst_vaapi_plugin_base_propose_allocation(plugin, query))
1058 gst_query_add_allocation_meta(query, GST_VIDEO_CROP_META_API_TYPE, NULL);
1059 gst_query_add_allocation_meta(query,
1060 GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, NULL);
1064 static GstFlowReturn
1065 gst_vaapisink_buffer_alloc(GstBaseSink *base_sink, guint64 offset, guint size,
1066 GstCaps *caps, GstBuffer **outbuf_ptr)
1068 return gst_vaapi_plugin_base_allocate_input_buffer(
1069 GST_VAAPI_PLUGIN_BASE(base_sink), caps, outbuf_ptr);
1074 gst_vaapisink_query(GstBaseSink *base_sink, GstQuery *query)
1076 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
1078 GST_INFO_OBJECT(sink, "query type %s", GST_QUERY_TYPE_NAME(query));
1080 if (gst_vaapi_reply_to_query(query, GST_VAAPI_PLUGIN_BASE_DISPLAY(sink))) {
1081 GST_DEBUG("sharing display %p", GST_VAAPI_PLUGIN_BASE_DISPLAY(sink));
1085 return GST_BASE_SINK_CLASS(gst_vaapisink_parent_class)->query(base_sink,
1090 gst_vaapisink_finalize(GObject *object)
1092 gst_vaapisink_destroy(GST_VAAPISINK(object));
1094 gst_vaapi_plugin_base_finalize(GST_VAAPI_PLUGIN_BASE(object));
1095 G_OBJECT_CLASS(gst_vaapisink_parent_class)->finalize(object);
1099 gst_vaapisink_set_property(
1102 const GValue *value,
1106 GstVaapiSink * const sink = GST_VAAPISINK(object);
1109 case PROP_DISPLAY_TYPE:
1110 gst_vaapi_plugin_base_set_display_type(GST_VAAPI_PLUGIN_BASE(sink),
1111 g_value_get_enum(value));
1113 case PROP_FULLSCREEN:
1114 sink->fullscreen = g_value_get_boolean(value);
1116 case PROP_SYNCHRONOUS:
1117 sink->synchronous = g_value_get_boolean(value);
1120 sink->use_glx = g_value_get_boolean(value);
1122 case PROP_USE_REFLECTION:
1123 sink->use_reflection = g_value_get_boolean(value);
1126 sink->rotation_req = g_value_get_enum(value);
1128 case PROP_FORCE_ASPECT_RATIO:
1129 sink->keep_aspect = g_value_get_boolean(value);
1132 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1138 gst_vaapisink_get_property(
1145 GstVaapiSink * const sink = GST_VAAPISINK(object);
1148 case PROP_DISPLAY_TYPE:
1149 g_value_set_enum(value, GST_VAAPI_PLUGIN_BASE_DISPLAY_TYPE(sink));
1151 case PROP_FULLSCREEN:
1152 g_value_set_boolean(value, sink->fullscreen);
1154 case PROP_SYNCHRONOUS:
1155 g_value_set_boolean(value, sink->synchronous);
1158 g_value_set_boolean(value, sink->use_glx);
1160 case PROP_USE_REFLECTION:
1161 g_value_set_boolean(value, sink->use_reflection);
1164 g_value_set_enum(value, sink->rotation);
1166 case PROP_FORCE_ASPECT_RATIO:
1167 g_value_set_boolean(value, sink->keep_aspect);
1170 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1176 gst_vaapisink_class_init(GstVaapiSinkClass *klass)
1178 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
1179 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
1180 GstBaseSinkClass * const basesink_class = GST_BASE_SINK_CLASS(klass);
1181 GstVaapiPluginBaseClass * const base_plugin_class =
1182 GST_VAAPI_PLUGIN_BASE_CLASS(klass);
1183 GstPadTemplate *pad_template;
1185 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapisink,
1186 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
1188 gst_vaapi_plugin_base_class_init(base_plugin_class);
1189 base_plugin_class->has_interface = gst_vaapisink_has_interface;
1190 base_plugin_class->display_changed = gst_vaapisink_display_changed;
1192 object_class->finalize = gst_vaapisink_finalize;
1193 object_class->set_property = gst_vaapisink_set_property;
1194 object_class->get_property = gst_vaapisink_get_property;
1196 basesink_class->start = gst_vaapisink_start;
1197 basesink_class->stop = gst_vaapisink_stop;
1198 basesink_class->get_caps = gst_vaapisink_get_caps;
1199 basesink_class->set_caps = gst_vaapisink_set_caps;
1200 basesink_class->preroll = gst_vaapisink_show_frame;
1201 basesink_class->render = gst_vaapisink_show_frame;
1202 basesink_class->query = gst_vaapisink_query;
1203 #if GST_CHECK_VERSION(1,0,0)
1204 basesink_class->propose_allocation = gst_vaapisink_propose_allocation;
1206 basesink_class->buffer_alloc = gst_vaapisink_buffer_alloc;
1209 gst_element_class_set_static_metadata(element_class,
1213 "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");
1215 pad_template = gst_static_pad_template_get(&gst_vaapisink_sink_factory);
1216 gst_element_class_add_pad_template(element_class, pad_template);
1218 g_object_class_install_property
1221 g_param_spec_enum("display",
1223 "display type to use",
1224 GST_VAAPI_TYPE_DISPLAY_TYPE,
1225 GST_VAAPI_DISPLAY_TYPE_ANY,
1226 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1229 g_object_class_install_property
1232 g_param_spec_boolean("use-glx",
1234 "Enables OpenGL rendering",
1236 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1238 g_object_class_install_property
1240 PROP_USE_REFLECTION,
1241 g_param_spec_boolean("use-reflection",
1242 "Reflection effect",
1243 "Enables OpenGL reflection effect",
1245 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1248 g_object_class_install_property
1251 g_param_spec_boolean("fullscreen",
1253 "Requests window in fullscreen state",
1255 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1258 * GstVaapiSink:synchronous:
1260 * When enabled, runs the X display in synchronous mode. Note that
1261 * this is used only for debugging.
1263 g_object_class_install_property
1266 g_param_spec_boolean("synchronous",
1268 "Toggles X display synchronous mode",
1270 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1273 * GstVaapiSink:rotation:
1275 * The VA display rotation mode, expressed as a #GstVaapiRotation.
1277 g_object_class_install_property
1280 g_param_spec_enum(GST_VAAPI_DISPLAY_PROP_ROTATION,
1282 "The display rotation mode",
1283 GST_VAAPI_TYPE_ROTATION,
1285 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1288 * GstVaapiSink:force-aspect-ratio:
1290 * When enabled, scaling respects video aspect ratio; when disabled, the
1291 * video is distorted to fit the window.
1293 g_object_class_install_property
1295 PROP_FORCE_ASPECT_RATIO,
1296 g_param_spec_boolean("force-aspect-ratio",
1297 "Force aspect ratio",
1298 "When enabled, scaling will respect original aspect ratio",
1300 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1304 gst_vaapisink_init(GstVaapiSink *sink)
1306 GstVaapiPluginBase * const plugin = GST_VAAPI_PLUGIN_BASE(sink);
1308 gst_vaapi_plugin_base_init(plugin, GST_CAT_DEFAULT);
1309 gst_vaapi_plugin_base_set_display_type(plugin, DEFAULT_DISPLAY_TYPE);
1312 sink->window = NULL;
1313 sink->window_width = 0;
1314 sink->window_height = 0;
1315 sink->texture = NULL;
1316 sink->video_buffer = NULL;
1317 sink->video_width = 0;
1318 sink->video_height = 0;
1319 sink->video_par_n = 1;
1320 sink->video_par_d = 1;
1321 sink->foreign_window = FALSE;
1322 sink->fullscreen = FALSE;
1323 sink->synchronous = FALSE;
1324 sink->rotation = DEFAULT_ROTATION;
1325 sink->rotation_req = DEFAULT_ROTATION;
1326 sink->use_reflection = FALSE;
1327 sink->use_overlay = FALSE;
1328 sink->use_rotation = FALSE;
1329 sink->keep_aspect = TRUE;
1330 gst_video_info_init(&sink->video_info);