2 * gstvaapisink.c - VA-API video sink
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
5 * Copyright (C) 2011-2013 Intel Corporation
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
24 * SECTION:gstvaapisink
25 * @short_description: A VA-API based videosink
27 * vaapisink renders video frames to a drawable (X #Window) on a local
28 * display using the Video Acceleration (VA) API. The element will
29 * create its own internal window and render into it.
32 #include "gst/vaapi/sysdeps.h"
34 #include <gst/video/video.h>
35 #include <gst/video/videocontext.h>
36 #include <gst/vaapi/gstvaapivalue.h>
38 # include <gst/vaapi/gstvaapidisplay_drm.h>
41 # include <gst/vaapi/gstvaapidisplay_x11.h>
42 # include <gst/vaapi/gstvaapiwindow_x11.h>
45 # include <gst/vaapi/gstvaapidisplay_glx.h>
46 # include <gst/vaapi/gstvaapiwindow_glx.h>
49 # include <gst/vaapi/gstvaapidisplay_wayland.h>
50 # include <gst/vaapi/gstvaapiwindow_wayland.h>
53 /* Supported interfaces */
54 #if GST_CHECK_VERSION(1,0,0)
55 # include <gst/video/videooverlay.h>
57 # include <gst/interfaces/xoverlay.h>
59 # define GST_TYPE_VIDEO_OVERLAY GST_TYPE_X_OVERLAY
60 # define GST_VIDEO_OVERLAY GST_X_OVERLAY
61 # define GstVideoOverlay GstXOverlay
62 # define GstVideoOverlayInterface GstXOverlayClass
64 # define gst_video_overlay_prepare_window_handle(sink) \
65 gst_x_overlay_prepare_xwindow_id(sink)
66 # define gst_video_overlay_got_window_handle(sink, window_handle) \
67 gst_x_overlay_got_window_handle(sink, window_handle)
70 #include "gstvaapisink.h"
71 #include "gstvaapipluginutil.h"
72 #include "gstvaapivideometa.h"
73 #if GST_CHECK_VERSION(1,0,0)
74 #include "gstvaapivideobufferpool.h"
75 #include "gstvaapivideomemory.h"
78 #define GST_PLUGIN_NAME "vaapisink"
79 #define GST_PLUGIN_DESC "A VA-API based videosink"
81 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapisink);
82 #define GST_CAT_DEFAULT gst_debug_vaapisink
84 /* Default template */
85 static const char gst_vaapisink_sink_caps_str[] =
86 #if !GST_CHECK_VERSION(1,0,0)
88 "width = (int) [ 1, MAX ], "
89 "height = (int) [ 1, MAX ]; "
91 GST_VAAPI_SURFACE_CAPS;
93 static GstStaticPadTemplate gst_vaapisink_sink_factory =
94 GST_STATIC_PAD_TEMPLATE(
98 GST_STATIC_CAPS(gst_vaapisink_sink_caps_str));
100 /* GstImplementsInterface interface */
101 #if !GST_CHECK_VERSION(1,0,0)
103 gst_vaapisink_implements_interface_supported(
104 GstImplementsInterface *iface,
108 return (type == GST_TYPE_VIDEO_CONTEXT ||
109 type == GST_TYPE_VIDEO_OVERLAY);
113 gst_vaapisink_implements_iface_init(GstImplementsInterfaceClass *iface)
115 iface->supported = gst_vaapisink_implements_interface_supported;
119 /* GstVideoContext interface */
121 gst_vaapisink_set_video_context(GstVideoContext *context, const gchar *type,
124 GstVaapiSink *sink = GST_VAAPISINK (context);
125 gst_vaapi_set_display (type, value, &sink->display);
129 gst_vaapisink_video_context_iface_init(GstVideoContextInterface *iface)
131 iface->set_context = gst_vaapisink_set_video_context;
135 gst_vaapisink_video_overlay_iface_init(GstVideoOverlayInterface *iface);
137 G_DEFINE_TYPE_WITH_CODE(
141 #if !GST_CHECK_VERSION(1,0,0)
142 G_IMPLEMENT_INTERFACE(GST_TYPE_IMPLEMENTS_INTERFACE,
143 gst_vaapisink_implements_iface_init);
145 G_IMPLEMENT_INTERFACE(GST_TYPE_VIDEO_CONTEXT,
146 gst_vaapisink_video_context_iface_init);
147 G_IMPLEMENT_INTERFACE(GST_TYPE_VIDEO_OVERLAY,
148 gst_vaapisink_video_overlay_iface_init))
160 #define DEFAULT_DISPLAY_TYPE GST_VAAPI_DISPLAY_TYPE_ANY
161 #define DEFAULT_ROTATION GST_VAAPI_ROTATION_0
163 /* GstVideoOverlay interface */
167 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id);
171 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *buffer);
174 gst_vaapisink_video_overlay_set_window_handle(GstVideoOverlay *overlay, guintptr window)
176 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
178 /* Disable GLX rendering when vaapisink is using a foreign X
179 window. It's pretty much useless */
180 if (sink->display_type == GST_VAAPI_DISPLAY_TYPE_GLX)
181 sink->display_type = GST_VAAPI_DISPLAY_TYPE_X11;
183 sink->foreign_window = TRUE;
185 switch (sink->display_type) {
187 case GST_VAAPI_DISPLAY_TYPE_X11:
188 gst_vaapisink_ensure_window_xid(sink, window);
197 gst_vaapisink_video_overlay_set_render_rectangle(
198 GstVideoOverlay *overlay,
205 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
206 GstVaapiRectangle * const display_rect = &sink->display_rect;
210 display_rect->width = width;
211 display_rect->height = height;
213 GST_DEBUG("render rect (%d,%d):%ux%u",
214 display_rect->x, display_rect->y,
215 display_rect->width, display_rect->height);
219 gst_vaapisink_video_overlay_expose(GstVideoOverlay *overlay)
221 GstBaseSink * const base_sink = GST_BASE_SINK(overlay);
224 #if GST_CHECK_VERSION(1,0,0)
225 GstSample * const sample = gst_base_sink_get_last_sample(base_sink);
228 buffer = gst_buffer_ref(gst_sample_get_buffer(sample));
229 gst_sample_unref(sample);
231 buffer = gst_base_sink_get_last_buffer(base_sink);
234 gst_vaapisink_show_frame(base_sink, buffer);
235 gst_buffer_unref(buffer);
240 gst_vaapisink_video_overlay_iface_init(GstVideoOverlayInterface *iface)
242 iface->set_window_handle = gst_vaapisink_video_overlay_set_window_handle;
243 iface->set_render_rectangle = gst_vaapisink_video_overlay_set_render_rectangle;
244 iface->expose = gst_vaapisink_video_overlay_expose;
248 gst_vaapisink_destroy(GstVaapiSink *sink)
250 gst_buffer_replace(&sink->video_buffer, NULL);
251 g_clear_object(&sink->texture);
252 g_clear_object(&sink->display);
253 g_clear_object(&sink->uploader);
255 gst_caps_replace(&sink->caps, NULL);
259 /* Checks whether a ConfigureNotify event is in the queue */
260 typedef struct _ConfigureNotifyEventPendingArgs ConfigureNotifyEventPendingArgs;
261 struct _ConfigureNotifyEventPendingArgs {
269 configure_notify_event_pending_cb(Display *dpy, XEvent *xev, XPointer arg)
271 ConfigureNotifyEventPendingArgs * const args =
272 (ConfigureNotifyEventPendingArgs *)arg;
274 if (xev->type == ConfigureNotify &&
275 xev->xconfigure.window == args->window &&
276 xev->xconfigure.width == args->width &&
277 xev->xconfigure.height == args->height)
280 /* XXX: this is a hack to traverse the whole queue because we
281 can't use XPeekIfEvent() since it could block */
286 configure_notify_event_pending(
293 ConfigureNotifyEventPendingArgs args;
296 args.window = window;
298 args.height = height;
301 /* XXX: don't use XPeekIfEvent() because it might block */
303 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
305 configure_notify_event_pending_cb, (XPointer)&args
312 get_display_type_name(GstVaapiDisplayType display_type)
314 gpointer const klass = g_type_class_peek(GST_VAAPI_TYPE_DISPLAY_TYPE);
315 GEnumValue * const e = g_enum_get_value(klass, display_type);
318 return e->value_name;
319 return "<unknown-type>";
322 static inline gboolean
323 gst_vaapisink_ensure_display(GstVaapiSink *sink)
325 GstVaapiDisplayType display_type;
326 GstVaapiRenderMode render_mode;
328 if (!gst_vaapi_ensure_display(sink, sink->display_type, &sink->display))
331 display_type = gst_vaapi_display_get_display_type(sink->display);
332 if (display_type != sink->display_type) {
333 GST_INFO("created %s %p", get_display_type_name(display_type),
335 sink->display_type = display_type;
338 gst_vaapi_display_get_render_mode(sink->display, &render_mode) &&
339 render_mode == GST_VAAPI_RENDER_MODE_OVERLAY;
340 GST_DEBUG("use %s rendering mode", sink->use_overlay ? "overlay" : "texture");
342 sink->use_rotation = gst_vaapi_display_has_property(
343 sink->display, GST_VAAPI_DISPLAY_PROP_ROTATION);
349 gst_vaapisink_ensure_uploader(GstVaapiSink *sink)
351 if (!gst_vaapisink_ensure_display(sink))
354 if (!sink->uploader) {
355 sink->uploader = gst_vaapi_uploader_new(sink->display);
363 gst_vaapisink_ensure_render_rect(GstVaapiSink *sink, guint width, guint height)
365 GstVaapiRectangle * const display_rect = &sink->display_rect;
366 guint num, den, display_par_n, display_par_d;
369 /* Return success if caps are not set yet */
373 GST_DEBUG("ensure render rect within %ux%u bounds", width, height);
375 gst_vaapi_display_get_pixel_aspect_ratio(
377 &display_par_n, &display_par_d
379 GST_DEBUG("display pixel-aspect-ratio %d/%d",
380 display_par_n, display_par_d);
382 success = gst_video_calculate_display_ratio(
384 sink->video_width, sink->video_height,
385 sink->video_par_n, sink->video_par_d,
386 display_par_n, display_par_d
390 GST_DEBUG("video size %dx%d, calculated ratio %d/%d",
391 sink->video_width, sink->video_height, num, den);
393 display_rect->width = gst_util_uint64_scale_int(height, num, den);
394 if (display_rect->width <= width) {
395 GST_DEBUG("keeping window height");
396 display_rect->height = height;
399 GST_DEBUG("keeping window width");
400 display_rect->width = width;
401 display_rect->height =
402 gst_util_uint64_scale_int(width, den, num);
404 GST_DEBUG("scaling video to %ux%u", display_rect->width, display_rect->height);
406 g_assert(display_rect->width <= width);
407 g_assert(display_rect->height <= height);
409 display_rect->x = (width - display_rect->width) / 2;
410 display_rect->y = (height - display_rect->height) / 2;
412 GST_DEBUG("render rect (%d,%d):%ux%u",
413 display_rect->x, display_rect->y,
414 display_rect->width, display_rect->height);
419 gst_vaapisink_ensure_window_size(GstVaapiSink *sink, guint *pwidth, guint *pheight)
421 GstVideoRectangle src_rect, dst_rect, out_rect;
422 guint num, den, display_width, display_height, display_par_n, display_par_d;
423 gboolean success, scale;
425 if (sink->foreign_window) {
426 *pwidth = sink->window_width;
427 *pheight = sink->window_height;
431 gst_vaapi_display_get_size(sink->display, &display_width, &display_height);
432 if (sink->fullscreen) {
433 *pwidth = display_width;
434 *pheight = display_height;
438 gst_vaapi_display_get_pixel_aspect_ratio(
440 &display_par_n, &display_par_d
443 success = gst_video_calculate_display_ratio(
445 sink->video_width, sink->video_height,
446 sink->video_par_n, sink->video_par_d,
447 display_par_n, display_par_d
450 num = sink->video_par_n;
451 den = sink->video_par_d;
456 src_rect.w = gst_util_uint64_scale_int(sink->video_height, num, den);
457 src_rect.h = sink->video_height;
460 dst_rect.w = display_width;
461 dst_rect.h = display_height;
462 scale = (src_rect.w > dst_rect.w || src_rect.h > dst_rect.h);
463 gst_video_sink_center_rect(src_rect, dst_rect, &out_rect, scale);
464 *pwidth = out_rect.w;
465 *pheight = out_rect.h;
468 static inline gboolean
469 gst_vaapisink_ensure_window(GstVaapiSink *sink, guint width, guint height)
471 GstVaapiDisplay * const display = sink->display;
474 switch (sink->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", sink->display_type);
502 return sink->window != NULL;
507 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id)
510 unsigned int width, height, border_width, depth;
514 if (!gst_vaapisink_ensure_display(sink))
517 gst_vaapi_display_lock(sink->display);
519 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
522 &x, &y, &width, &height, &border_width, &depth
524 gst_vaapi_display_unlock(sink->display);
526 if ((width != sink->window_width || height != sink->window_height) &&
527 !configure_notify_event_pending(sink, xid, width, height)) {
528 if (!gst_vaapisink_ensure_render_rect(sink, width, height))
530 sink->window_width = width;
531 sink->window_height = height;
535 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window)) == xid)
538 g_clear_object(&sink->window);
540 switch (sink->display_type) {
542 case GST_VAAPI_DISPLAY_TYPE_GLX:
543 sink->window = gst_vaapi_window_glx_new_with_xid(sink->display, xid);
546 case GST_VAAPI_DISPLAY_TYPE_X11:
547 sink->window = gst_vaapi_window_x11_new_with_xid(sink->display, xid);
550 GST_ERROR("unsupported display type %d", sink->display_type);
553 return sink->window != NULL;
558 gst_vaapisink_ensure_rotation(GstVaapiSink *sink, gboolean recalc_display_rect)
560 gboolean success = FALSE;
562 g_return_val_if_fail(sink->display, FALSE);
564 if (sink->rotation == sink->rotation_req)
567 if (!sink->use_rotation) {
568 GST_WARNING("VA display does not support rotation");
572 gst_vaapi_display_lock(sink->display);
573 success = gst_vaapi_display_set_rotation(sink->display, sink->rotation_req);
574 gst_vaapi_display_unlock(sink->display);
576 GST_ERROR("failed to change VA display rotation mode");
580 if (((sink->rotation + sink->rotation_req) % 180) == 90) {
581 /* Orientation changed */
582 G_PRIMITIVE_SWAP(guint, sink->video_width, sink->video_height);
583 G_PRIMITIVE_SWAP(gint, sink->video_par_n, sink->video_par_d);
586 if (recalc_display_rect && !sink->foreign_window)
587 gst_vaapisink_ensure_render_rect(sink, sink->window_width,
588 sink->window_height);
592 sink->rotation = sink->rotation_req;
597 gst_vaapisink_ensure_video_buffer_pool(GstVaapiSink *sink, GstCaps *caps)
599 #if GST_CHECK_VERSION(1,0,0)
602 GstStructure *config;
606 if (!gst_vaapisink_ensure_display(sink))
609 if (sink->video_buffer_pool) {
610 config = gst_buffer_pool_get_config(sink->video_buffer_pool);
611 gst_buffer_pool_config_get_params(config, &pool_caps, NULL, NULL, NULL);
612 need_pool = !gst_caps_is_equal(caps, pool_caps);
613 gst_structure_free(config);
616 g_clear_object(&sink->video_buffer_pool);
617 sink->video_buffer_size = 0;
620 pool = gst_vaapi_video_buffer_pool_new(sink->display);
622 goto error_create_pool;
624 gst_video_info_init(&vi);
625 gst_video_info_from_caps(&vi, caps);
626 if (GST_VIDEO_INFO_FORMAT(&vi) == GST_VIDEO_FORMAT_ENCODED) {
627 GST_DEBUG("assume video buffer pool format is NV12");
628 gst_video_info_set_format(&vi, GST_VIDEO_FORMAT_NV12,
629 GST_VIDEO_INFO_WIDTH(&vi), GST_VIDEO_INFO_HEIGHT(&vi));
631 sink->video_buffer_size = vi.size;
633 config = gst_buffer_pool_get_config(pool);
634 gst_buffer_pool_config_set_params(config, caps, sink->video_buffer_size,
636 gst_buffer_pool_config_add_option(config,
637 GST_BUFFER_POOL_OPTION_VAAPI_VIDEO_META);
638 if (!gst_buffer_pool_set_config(pool, config))
639 goto error_pool_config;
640 sink->video_buffer_pool = pool;
646 GST_ERROR("failed to create buffer pool");
651 GST_ERROR("failed to reset buffer pool config");
652 g_object_unref(pool);
661 gst_vaapisink_start(GstBaseSink *base_sink)
663 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
665 if (!gst_vaapisink_ensure_display(sink))
668 sink->uploader = gst_vaapi_uploader_new(sink->display);
675 gst_vaapisink_stop(GstBaseSink *base_sink)
677 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
679 gst_buffer_replace(&sink->video_buffer, NULL);
680 #if GST_CHECK_VERSION(1,0,0)
681 g_clear_object(&sink->video_buffer_pool);
683 g_clear_object(&sink->window);
684 g_clear_object(&sink->display);
685 g_clear_object(&sink->uploader);
691 gst_vaapisink_get_caps_impl(GstBaseSink *base_sink)
693 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
694 GstCaps *out_caps, *yuv_caps;
696 out_caps = gst_caps_from_string(GST_VAAPI_SURFACE_CAPS);
700 if (gst_vaapisink_ensure_uploader(sink)) {
701 yuv_caps = gst_vaapi_uploader_get_caps(sink->uploader);
703 gst_caps_append(out_caps, gst_caps_copy(yuv_caps));
708 #if GST_CHECK_VERSION(1,0,0)
709 static inline GstCaps *
710 gst_vaapisink_get_caps(GstBaseSink *base_sink, GstCaps *filter)
712 return gst_vaapisink_get_caps_impl(base_sink);
715 #define gst_vaapisink_get_caps gst_vaapisink_get_caps_impl
719 gst_vaapisink_set_caps(GstBaseSink *base_sink, GstCaps *caps)
721 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
723 guint win_width, win_height;
726 if (sink->display_type == GST_VAAPI_DISPLAY_TYPE_DRM)
730 if (!gst_vaapisink_ensure_video_buffer_pool(sink, caps))
733 if (!gst_video_info_from_caps(&vi, caps))
735 sink->use_video_raw = GST_VIDEO_INFO_IS_YUV(&vi);
736 sink->video_width = GST_VIDEO_INFO_WIDTH(&vi);
737 sink->video_height = GST_VIDEO_INFO_HEIGHT(&vi);
738 sink->video_par_n = GST_VIDEO_INFO_PAR_N(&vi);
739 sink->video_par_d = GST_VIDEO_INFO_PAR_D(&vi);
740 GST_DEBUG("video pixel-aspect-ratio %d/%d",
741 sink->video_par_n, sink->video_par_d);
743 gst_caps_replace(&sink->caps, caps);
745 if (!gst_vaapisink_ensure_display(sink))
748 gst_vaapisink_ensure_rotation(sink, FALSE);
750 gst_vaapisink_ensure_window_size(sink, &win_width, &win_height);
752 if (!sink->foreign_window || sink->fullscreen)
753 gst_vaapi_window_set_size(sink->window, win_width, win_height);
756 gst_vaapi_display_lock(sink->display);
757 gst_video_overlay_prepare_window_handle(GST_VIDEO_OVERLAY(sink));
758 gst_vaapi_display_unlock(sink->display);
761 if (!gst_vaapisink_ensure_window(sink, win_width, win_height))
763 gst_vaapi_window_set_fullscreen(sink->window, sink->fullscreen);
764 gst_vaapi_window_show(sink->window);
765 gst_vaapi_window_get_size(sink->window, &win_width, &win_height);
767 sink->window_width = win_width;
768 sink->window_height = win_height;
769 GST_DEBUG("window size %ux%u", win_width, win_height);
771 return gst_vaapisink_ensure_render_rect(sink, win_width, win_height);
776 render_background(GstVaapiSink *sink)
778 /* Original code from Mirco Muller (MacSlow):
779 <http://cgit.freedesktop.org/~macslow/gl-gst-player/> */
780 GLfloat fStartX = 0.0f;
781 GLfloat fStartY = 0.0f;
782 GLfloat fWidth = (GLfloat)sink->window_width;
783 GLfloat fHeight = (GLfloat)sink->window_height;
785 glClear(GL_COLOR_BUFFER_BIT);
788 /* top third, darker grey to white */
789 glColor3f(0.85f, 0.85f, 0.85f);
790 glVertex3f(fStartX, fStartY, 0.0f);
791 glColor3f(0.85f, 0.85f, 0.85f);
792 glVertex3f(fStartX + fWidth, fStartY, 0.0f);
793 glColor3f(1.0f, 1.0f, 1.0f);
794 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
795 glColor3f(1.0f, 1.0f, 1.0f);
796 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
798 /* middle third, just plain white */
799 glColor3f(1.0f, 1.0f, 1.0f);
800 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
801 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
802 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
803 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
805 /* bottom third, white to lighter grey */
806 glColor3f(1.0f, 1.0f, 1.0f);
807 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
808 glColor3f(1.0f, 1.0f, 1.0f);
809 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
810 glColor3f(0.62f, 0.66f, 0.69f);
811 glVertex3f(fStartX + fWidth, fStartY + fHeight, 0.0f);
812 glColor3f(0.62f, 0.66f, 0.69f);
813 glVertex3f(fStartX, fStartY + fHeight, 0.0f);
819 render_frame(GstVaapiSink *sink)
821 const guint x1 = sink->display_rect.x;
822 const guint x2 = sink->display_rect.x + sink->display_rect.width;
823 const guint y1 = sink->display_rect.y;
824 const guint y2 = sink->display_rect.y + sink->display_rect.height;
826 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
829 glTexCoord2f(0.0f, 0.0f); glVertex2i(x1, y1);
830 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y2);
831 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y2);
832 glTexCoord2f(1.0f, 0.0f); glVertex2i(x2, y1);
838 render_reflection(GstVaapiSink *sink)
840 const guint x1 = sink->display_rect.x;
841 const guint x2 = sink->display_rect.x + sink->display_rect.width;
842 const guint y1 = sink->display_rect.y;
843 const guint rh = sink->display_rect.height / 5;
844 GLfloat ry = 1.0f - (GLfloat)rh / (GLfloat)sink->display_rect.height;
848 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
849 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y1);
850 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y1);
852 glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
853 glTexCoord2f(1.0f, ry); glVertex2i(x2, y1 + rh);
854 glTexCoord2f(0.0f, ry); glVertex2i(x1, y1 + rh);
860 gst_vaapisink_show_frame_glx(
862 GstVaapiSurface *surface,
866 GstVaapiWindowGLX * const window = GST_VAAPI_WINDOW_GLX(sink->window);
870 gst_vaapi_window_glx_make_current(window);
871 if (!sink->texture) {
872 sink->texture = gst_vaapi_texture_new(
880 goto error_create_texture;
882 if (!gst_vaapi_texture_put_surface(sink->texture, surface, flags))
883 goto error_transfer_surface;
885 target = gst_vaapi_texture_get_target(sink->texture);
886 texture = gst_vaapi_texture_get_id(sink->texture);
887 if (target != GL_TEXTURE_2D || !texture)
890 if (sink->use_reflection)
891 render_background(sink);
894 glBindTexture(target, texture);
896 if (sink->use_reflection) {
898 glRotatef(20.0f, 0.0f, 1.0f, 0.0f);
899 glTranslatef(50.0f, 0.0f, 0.0f);
902 if (sink->use_reflection) {
904 glTranslatef(0.0, (GLfloat)sink->display_rect.height + 5.0f, 0.0f);
905 render_reflection(sink);
910 glBindTexture(target, 0);
912 gst_vaapi_window_glx_swap_buffers(window);
916 error_create_texture:
918 GST_DEBUG("could not create VA/GLX texture");
921 error_transfer_surface:
923 GST_DEBUG("could not transfer VA surface to texture");
929 static inline gboolean
930 gst_vaapisink_put_surface(
932 GstVaapiSurface *surface,
936 if (!gst_vaapi_window_put_surface(sink->window, surface,
937 NULL, &sink->display_rect, flags)) {
938 GST_DEBUG("could not render VA surface");
945 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *src_buffer)
947 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
948 GstVaapiVideoMeta *meta;
949 GstVaapiSurface *surface;
954 meta = gst_buffer_get_vaapi_video_meta(src_buffer);
955 #if GST_CHECK_VERSION(1,0,0)
958 buffer = gst_buffer_ref(src_buffer);
961 buffer = gst_buffer_ref(src_buffer);
962 else if (sink->use_video_raw) {
963 buffer = gst_vaapi_uploader_get_buffer(sink->uploader);
966 meta = gst_buffer_get_vaapi_video_meta(buffer);
973 if (sink->use_video_raw &&
974 !gst_vaapi_uploader_process(sink->uploader, src_buffer, buffer)) {
975 GST_WARNING("failed to process raw YUV buffer");
980 if (sink->display != gst_vaapi_video_meta_get_display(meta)) {
981 g_clear_object(&sink->display);
982 sink->display = g_object_ref(gst_vaapi_video_meta_get_display(meta));
988 gst_vaapisink_ensure_rotation(sink, TRUE);
990 surface = gst_vaapi_video_meta_get_surface(meta);
994 GST_DEBUG("render surface %" GST_VAAPI_ID_FORMAT,
995 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surface)));
997 flags = gst_vaapi_video_meta_get_render_flags(meta);
999 if (!gst_vaapi_apply_composition(surface, src_buffer))
1000 GST_WARNING("could not update subtitles");
1002 switch (sink->display_type) {
1004 case GST_VAAPI_DISPLAY_TYPE_GLX:
1005 success = gst_vaapisink_show_frame_glx(sink, surface, flags);
1009 case GST_VAAPI_DISPLAY_TYPE_DRM:
1014 case GST_VAAPI_DISPLAY_TYPE_X11:
1015 success = gst_vaapisink_put_surface(sink, surface, flags);
1019 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
1020 success = gst_vaapisink_put_surface(sink, surface, flags);
1024 GST_ERROR("unsupported display type %d", sink->display_type);
1031 /* Retain VA surface until the next one is displayed */
1032 if (sink->use_overlay)
1033 gst_buffer_replace(&sink->video_buffer, buffer);
1034 gst_buffer_unref(buffer);
1038 gst_buffer_unref(buffer);
1039 return GST_FLOW_EOS;
1042 #if GST_CHECK_VERSION(1,0,0)
1044 gst_vaapisink_propose_allocation(GstBaseSink *base_sink, GstQuery *query)
1046 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
1047 GstCaps *caps = NULL;
1050 gst_query_parse_allocation(query, &caps, &need_pool);
1055 if (!gst_vaapisink_ensure_video_buffer_pool(sink, caps))
1057 gst_query_add_allocation_pool(query, sink->video_buffer_pool,
1058 sink->video_buffer_size, 0, 0);
1061 gst_query_add_allocation_meta(query,
1062 GST_VAAPI_VIDEO_META_API_TYPE, NULL);
1063 gst_query_add_allocation_meta(query,
1064 GST_VIDEO_META_API_TYPE, NULL);
1065 gst_query_add_allocation_meta(query,
1066 GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, NULL);
1072 GST_ERROR("no caps specified");
1077 static GstFlowReturn
1078 gst_vaapisink_buffer_alloc(
1079 GstBaseSink *base_sink,
1086 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
1092 if (!sink->use_video_raw) {
1093 /* Note: this code path is rarely used but for raw YUV formats
1094 from custom pipeline. Otherwise, GstBaseSink::set_caps() is
1095 called first, and GstBaseSink::buffer_alloc() is not called
1096 in VA surface format mode */
1097 if (!gst_video_info_from_caps(&vi, caps))
1098 return GST_FLOW_NOT_SUPPORTED;
1099 if (!GST_VIDEO_INFO_IS_YUV(&vi))
1103 if (!gst_vaapi_uploader_ensure_display(sink->uploader, sink->display))
1104 return GST_FLOW_NOT_SUPPORTED;
1105 if (!gst_vaapi_uploader_ensure_caps(sink->uploader, caps, NULL))
1106 return GST_FLOW_NOT_SUPPORTED;
1108 buf = gst_vaapi_uploader_get_buffer(sink->uploader);
1110 GST_WARNING("failed to allocate resources for raw YUV buffer");
1111 return GST_FLOW_NOT_SUPPORTED;
1120 gst_vaapisink_query(GstBaseSink *base_sink, GstQuery *query)
1122 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
1124 if (gst_vaapi_reply_to_query(query, sink->display)) {
1125 GST_DEBUG("sharing display %p", sink->display);
1128 return GST_BASE_SINK_CLASS(gst_vaapisink_parent_class)->query(base_sink,
1133 gst_vaapisink_finalize(GObject *object)
1135 gst_vaapisink_destroy(GST_VAAPISINK(object));
1137 G_OBJECT_CLASS(gst_vaapisink_parent_class)->finalize(object);
1141 gst_vaapisink_set_property(
1144 const GValue *value,
1148 GstVaapiSink * const sink = GST_VAAPISINK(object);
1151 case PROP_DISPLAY_TYPE:
1152 sink->display_type = g_value_get_enum(value);
1154 case PROP_FULLSCREEN:
1155 sink->fullscreen = g_value_get_boolean(value);
1157 case PROP_SYNCHRONOUS:
1158 sink->synchronous = g_value_get_boolean(value);
1160 case PROP_USE_REFLECTION:
1161 sink->use_reflection = g_value_get_boolean(value);
1164 sink->rotation_req = g_value_get_enum(value);
1167 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1173 gst_vaapisink_get_property(
1180 GstVaapiSink * const sink = GST_VAAPISINK(object);
1183 case PROP_DISPLAY_TYPE:
1184 g_value_set_enum(value, sink->display_type);
1186 case PROP_FULLSCREEN:
1187 g_value_set_boolean(value, sink->fullscreen);
1189 case PROP_SYNCHRONOUS:
1190 g_value_set_boolean(value, sink->synchronous);
1192 case PROP_USE_REFLECTION:
1193 g_value_set_boolean(value, sink->use_reflection);
1196 g_value_set_enum(value, sink->rotation);
1199 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1205 gst_vaapisink_class_init(GstVaapiSinkClass *klass)
1207 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
1208 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
1209 GstBaseSinkClass * const basesink_class = GST_BASE_SINK_CLASS(klass);
1210 GstPadTemplate *pad_template;
1212 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapisink,
1213 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
1215 object_class->finalize = gst_vaapisink_finalize;
1216 object_class->set_property = gst_vaapisink_set_property;
1217 object_class->get_property = gst_vaapisink_get_property;
1219 basesink_class->start = gst_vaapisink_start;
1220 basesink_class->stop = gst_vaapisink_stop;
1221 basesink_class->get_caps = gst_vaapisink_get_caps;
1222 basesink_class->set_caps = gst_vaapisink_set_caps;
1223 basesink_class->preroll = gst_vaapisink_show_frame;
1224 basesink_class->render = gst_vaapisink_show_frame;
1225 basesink_class->query = gst_vaapisink_query;
1226 #if GST_CHECK_VERSION(1,0,0)
1227 basesink_class->propose_allocation = gst_vaapisink_propose_allocation;
1229 basesink_class->buffer_alloc = gst_vaapisink_buffer_alloc;
1232 gst_element_class_set_static_metadata(element_class,
1236 "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");
1238 pad_template = gst_static_pad_template_get(&gst_vaapisink_sink_factory);
1239 gst_element_class_add_pad_template(element_class, pad_template);
1241 g_object_class_install_property
1244 g_param_spec_enum("display",
1246 "display type to use",
1247 GST_VAAPI_TYPE_DISPLAY_TYPE,
1248 GST_VAAPI_DISPLAY_TYPE_ANY,
1249 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1252 g_object_class_install_property
1254 PROP_USE_REFLECTION,
1255 g_param_spec_boolean("use-reflection",
1256 "Reflection effect",
1257 "Enables OpenGL reflection effect",
1259 G_PARAM_READWRITE));
1262 g_object_class_install_property
1265 g_param_spec_boolean("fullscreen",
1267 "Requests window in fullscreen state",
1269 G_PARAM_READWRITE));
1272 * GstVaapiSink:synchronous:
1274 * When enabled, runs the X display in synchronous mode. Note that
1275 * this is used only for debugging.
1277 g_object_class_install_property
1280 g_param_spec_boolean("synchronous",
1282 "Toggles X display synchronous mode",
1284 G_PARAM_READWRITE));
1287 * GstVaapiSink:rotation:
1289 * The VA display rotation mode, expressed as a #GstVaapiRotation.
1291 g_object_class_install_property
1294 g_param_spec_enum(GST_VAAPI_DISPLAY_PROP_ROTATION,
1296 "The display rotation mode",
1297 GST_VAAPI_TYPE_ROTATION,
1299 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1303 gst_vaapisink_init(GstVaapiSink *sink)
1306 sink->display = NULL;
1307 sink->window = NULL;
1308 sink->window_width = 0;
1309 sink->window_height = 0;
1310 sink->texture = NULL;
1311 sink->video_buffer = NULL;
1312 sink->video_width = 0;
1313 sink->video_height = 0;
1314 sink->video_par_n = 1;
1315 sink->video_par_d = 1;
1316 sink->foreign_window = FALSE;
1317 sink->fullscreen = FALSE;
1318 sink->synchronous = FALSE;
1319 sink->display_type = DEFAULT_DISPLAY_TYPE;
1320 sink->rotation = DEFAULT_ROTATION;
1321 sink->rotation_req = DEFAULT_ROTATION;
1322 sink->use_reflection = FALSE;
1323 sink->use_overlay = FALSE;
1324 sink->use_rotation = FALSE;