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)
87 GST_VIDEO_CAPS_MAKE(GST_VIDEO_FORMATS_ALL) "; "
90 "width = (int) [ 1, MAX ], "
91 "height = (int) [ 1, MAX ]; "
93 GST_VAAPI_SURFACE_CAPS;
95 static GstStaticPadTemplate gst_vaapisink_sink_factory =
96 GST_STATIC_PAD_TEMPLATE(
100 GST_STATIC_CAPS(gst_vaapisink_sink_caps_str));
102 /* GstImplementsInterface interface */
103 #if !GST_CHECK_VERSION(1,0,0)
105 gst_vaapisink_implements_interface_supported(
106 GstImplementsInterface *iface,
110 return (type == GST_TYPE_VIDEO_CONTEXT ||
111 type == GST_TYPE_VIDEO_OVERLAY);
115 gst_vaapisink_implements_iface_init(GstImplementsInterfaceClass *iface)
117 iface->supported = gst_vaapisink_implements_interface_supported;
121 /* GstVideoContext interface */
123 gst_vaapisink_set_video_context(GstVideoContext *context, const gchar *type,
126 GstVaapiSink *sink = GST_VAAPISINK (context);
127 gst_vaapi_set_display (type, value, &sink->display);
131 gst_vaapisink_video_context_iface_init(GstVideoContextInterface *iface)
133 iface->set_context = gst_vaapisink_set_video_context;
137 gst_vaapisink_video_overlay_iface_init(GstVideoOverlayInterface *iface);
139 G_DEFINE_TYPE_WITH_CODE(
143 #if !GST_CHECK_VERSION(1,0,0)
144 G_IMPLEMENT_INTERFACE(GST_TYPE_IMPLEMENTS_INTERFACE,
145 gst_vaapisink_implements_iface_init);
147 G_IMPLEMENT_INTERFACE(GST_TYPE_VIDEO_CONTEXT,
148 gst_vaapisink_video_context_iface_init);
149 G_IMPLEMENT_INTERFACE(GST_TYPE_VIDEO_OVERLAY,
150 gst_vaapisink_video_overlay_iface_init))
163 #define DEFAULT_DISPLAY_TYPE GST_VAAPI_DISPLAY_TYPE_ANY
164 #define DEFAULT_ROTATION GST_VAAPI_ROTATION_0
166 /* GstVideoOverlay interface */
170 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id);
174 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *buffer);
177 gst_vaapisink_video_overlay_set_window_handle(GstVideoOverlay *overlay, guintptr window)
179 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
181 /* Disable GLX rendering when vaapisink is using a foreign X
182 window. It's pretty much useless */
183 if (sink->display_type == GST_VAAPI_DISPLAY_TYPE_GLX)
184 sink->display_type = GST_VAAPI_DISPLAY_TYPE_X11;
186 sink->foreign_window = TRUE;
188 switch (sink->display_type) {
190 case GST_VAAPI_DISPLAY_TYPE_X11:
191 gst_vaapisink_ensure_window_xid(sink, window);
200 gst_vaapisink_video_overlay_set_render_rectangle(
201 GstVideoOverlay *overlay,
208 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
209 GstVaapiRectangle * const display_rect = &sink->display_rect;
213 display_rect->width = width;
214 display_rect->height = height;
216 GST_DEBUG("render rect (%d,%d):%ux%u",
217 display_rect->x, display_rect->y,
218 display_rect->width, display_rect->height);
222 gst_vaapisink_video_overlay_expose(GstVideoOverlay *overlay)
224 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
225 GstBaseSink * const base_sink = GST_BASE_SINK(overlay);
228 if (sink->use_overlay)
229 buffer = sink->video_buffer ? gst_buffer_ref(sink->video_buffer) : NULL;
231 #if GST_CHECK_VERSION(1,0,0)
232 GstSample * const sample = gst_base_sink_get_last_sample(base_sink);
235 buffer = gst_buffer_ref(gst_sample_get_buffer(sample));
236 gst_sample_unref(sample);
238 buffer = gst_base_sink_get_last_buffer(base_sink);
242 gst_vaapisink_show_frame(base_sink, buffer);
243 gst_buffer_unref(buffer);
248 gst_vaapisink_video_overlay_iface_init(GstVideoOverlayInterface *iface)
250 iface->set_window_handle = gst_vaapisink_video_overlay_set_window_handle;
251 iface->set_render_rectangle = gst_vaapisink_video_overlay_set_render_rectangle;
252 iface->expose = gst_vaapisink_video_overlay_expose;
256 gst_vaapisink_destroy(GstVaapiSink *sink)
258 gst_buffer_replace(&sink->video_buffer, NULL);
260 gst_vaapi_texture_replace(&sink->texture, NULL);
262 gst_vaapi_display_replace(&sink->display, NULL);
263 g_clear_object(&sink->uploader);
265 gst_caps_replace(&sink->caps, NULL);
269 /* Checks whether a ConfigureNotify event is in the queue */
270 typedef struct _ConfigureNotifyEventPendingArgs ConfigureNotifyEventPendingArgs;
271 struct _ConfigureNotifyEventPendingArgs {
279 configure_notify_event_pending_cb(Display *dpy, XEvent *xev, XPointer arg)
281 ConfigureNotifyEventPendingArgs * const args =
282 (ConfigureNotifyEventPendingArgs *)arg;
284 if (xev->type == ConfigureNotify &&
285 xev->xconfigure.window == args->window &&
286 xev->xconfigure.width == args->width &&
287 xev->xconfigure.height == args->height)
290 /* XXX: this is a hack to traverse the whole queue because we
291 can't use XPeekIfEvent() since it could block */
296 configure_notify_event_pending(
303 ConfigureNotifyEventPendingArgs args;
306 args.window = window;
308 args.height = height;
311 /* XXX: don't use XPeekIfEvent() because it might block */
313 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
315 configure_notify_event_pending_cb, (XPointer)&args
322 get_display_type_name(GstVaapiDisplayType display_type)
324 gpointer const klass = g_type_class_peek(GST_VAAPI_TYPE_DISPLAY_TYPE);
325 GEnumValue * const e = g_enum_get_value(klass, display_type);
328 return e->value_name;
329 return "<unknown-type>";
332 static inline gboolean
333 gst_vaapisink_ensure_display(GstVaapiSink *sink)
335 GstVaapiDisplayType display_type;
336 GstVaapiRenderMode render_mode;
337 const gboolean had_display = sink->display != NULL;
339 if (!gst_vaapi_ensure_display(sink, sink->display_type, &sink->display))
342 display_type = gst_vaapi_display_get_display_type(sink->display);
343 if (display_type != sink->display_type || (!had_display && sink->display)) {
344 GST_INFO("created %s %p", get_display_type_name(display_type),
346 sink->display_type = display_type;
349 gst_vaapi_display_get_render_mode(sink->display, &render_mode) &&
350 render_mode == GST_VAAPI_RENDER_MODE_OVERLAY;
351 GST_DEBUG("use %s rendering mode", sink->use_overlay ? "overlay" : "texture");
353 sink->use_rotation = gst_vaapi_display_has_property(
354 sink->display, GST_VAAPI_DISPLAY_PROP_ROTATION);
360 gst_vaapisink_ensure_uploader(GstVaapiSink *sink)
362 if (!gst_vaapisink_ensure_display(sink))
365 if (!sink->uploader) {
366 sink->uploader = gst_vaapi_uploader_new(sink->display);
374 gst_vaapisink_ensure_render_rect(GstVaapiSink *sink, guint width, guint height)
376 GstVaapiRectangle * const display_rect = &sink->display_rect;
377 guint num, den, display_par_n, display_par_d;
380 /* Return success if caps are not set yet */
384 GST_DEBUG("ensure render rect within %ux%u bounds", width, height);
386 gst_vaapi_display_get_pixel_aspect_ratio(
388 &display_par_n, &display_par_d
390 GST_DEBUG("display pixel-aspect-ratio %d/%d",
391 display_par_n, display_par_d);
393 success = gst_video_calculate_display_ratio(
395 sink->video_width, sink->video_height,
396 sink->video_par_n, sink->video_par_d,
397 display_par_n, display_par_d
401 GST_DEBUG("video size %dx%d, calculated ratio %d/%d",
402 sink->video_width, sink->video_height, num, den);
404 display_rect->width = gst_util_uint64_scale_int(height, num, den);
405 if (display_rect->width <= width) {
406 GST_DEBUG("keeping window height");
407 display_rect->height = height;
410 GST_DEBUG("keeping window width");
411 display_rect->width = width;
412 display_rect->height =
413 gst_util_uint64_scale_int(width, den, num);
415 GST_DEBUG("scaling video to %ux%u", display_rect->width, display_rect->height);
417 g_assert(display_rect->width <= width);
418 g_assert(display_rect->height <= height);
420 display_rect->x = (width - display_rect->width) / 2;
421 display_rect->y = (height - display_rect->height) / 2;
423 GST_DEBUG("render rect (%d,%d):%ux%u",
424 display_rect->x, display_rect->y,
425 display_rect->width, display_rect->height);
430 gst_vaapisink_ensure_window_size(GstVaapiSink *sink, guint *pwidth, guint *pheight)
432 GstVideoRectangle src_rect, dst_rect, out_rect;
433 guint num, den, display_width, display_height, display_par_n, display_par_d;
434 gboolean success, scale;
436 if (sink->foreign_window) {
437 *pwidth = sink->window_width;
438 *pheight = sink->window_height;
442 gst_vaapi_display_get_size(sink->display, &display_width, &display_height);
443 if (sink->fullscreen) {
444 *pwidth = display_width;
445 *pheight = display_height;
449 gst_vaapi_display_get_pixel_aspect_ratio(
451 &display_par_n, &display_par_d
454 success = gst_video_calculate_display_ratio(
456 sink->video_width, sink->video_height,
457 sink->video_par_n, sink->video_par_d,
458 display_par_n, display_par_d
461 num = sink->video_par_n;
462 den = sink->video_par_d;
467 src_rect.w = gst_util_uint64_scale_int(sink->video_height, num, den);
468 src_rect.h = sink->video_height;
471 dst_rect.w = display_width;
472 dst_rect.h = display_height;
473 scale = (src_rect.w > dst_rect.w || src_rect.h > dst_rect.h);
474 gst_video_sink_center_rect(src_rect, dst_rect, &out_rect, scale);
475 *pwidth = out_rect.w;
476 *pheight = out_rect.h;
479 static inline gboolean
480 gst_vaapisink_ensure_window(GstVaapiSink *sink, guint width, guint height)
482 GstVaapiDisplay * const display = sink->display;
485 switch (sink->display_type) {
487 case GST_VAAPI_DISPLAY_TYPE_GLX:
488 sink->window = gst_vaapi_window_glx_new(display, width, height);
489 goto notify_video_overlay_interface;
492 case GST_VAAPI_DISPLAY_TYPE_X11:
493 sink->window = gst_vaapi_window_x11_new(display, width, height);
494 notify_video_overlay_interface:
497 gst_video_overlay_got_window_handle(
498 GST_VIDEO_OVERLAY(sink),
499 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window))
504 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
505 sink->window = gst_vaapi_window_wayland_new(display, width, height);
509 GST_ERROR("unsupported display type %d", sink->display_type);
513 return sink->window != NULL;
518 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id)
521 unsigned int width, height, border_width, depth;
525 if (!gst_vaapisink_ensure_display(sink))
528 gst_vaapi_display_lock(sink->display);
530 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
533 &x, &y, &width, &height, &border_width, &depth
535 gst_vaapi_display_unlock(sink->display);
537 if ((width != sink->window_width || height != sink->window_height) &&
538 !configure_notify_event_pending(sink, xid, width, height)) {
539 if (!gst_vaapisink_ensure_render_rect(sink, width, height))
541 sink->window_width = width;
542 sink->window_height = height;
546 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window)) == xid)
549 gst_vaapi_window_replace(&sink->window, NULL);
551 switch (sink->display_type) {
553 case GST_VAAPI_DISPLAY_TYPE_GLX:
554 sink->window = gst_vaapi_window_glx_new_with_xid(sink->display, xid);
557 case GST_VAAPI_DISPLAY_TYPE_X11:
558 sink->window = gst_vaapi_window_x11_new_with_xid(sink->display, xid);
561 GST_ERROR("unsupported display type %d", sink->display_type);
564 return sink->window != NULL;
569 gst_vaapisink_ensure_rotation(GstVaapiSink *sink, gboolean recalc_display_rect)
571 gboolean success = FALSE;
573 g_return_val_if_fail(sink->display, FALSE);
575 if (sink->rotation == sink->rotation_req)
578 if (!sink->use_rotation) {
579 GST_WARNING("VA display does not support rotation");
583 gst_vaapi_display_lock(sink->display);
584 success = gst_vaapi_display_set_rotation(sink->display, sink->rotation_req);
585 gst_vaapi_display_unlock(sink->display);
587 GST_ERROR("failed to change VA display rotation mode");
591 if (((sink->rotation + sink->rotation_req) % 180) == 90) {
592 /* Orientation changed */
593 G_PRIMITIVE_SWAP(guint, sink->video_width, sink->video_height);
594 G_PRIMITIVE_SWAP(gint, sink->video_par_n, sink->video_par_d);
597 if (recalc_display_rect && !sink->foreign_window)
598 gst_vaapisink_ensure_render_rect(sink, sink->window_width,
599 sink->window_height);
603 sink->rotation = sink->rotation_req;
608 gst_vaapisink_ensure_video_buffer_pool(GstVaapiSink *sink, GstCaps *caps)
610 #if GST_CHECK_VERSION(1,0,0)
613 GstStructure *config;
617 if (!gst_vaapisink_ensure_display(sink))
620 if (sink->video_buffer_pool) {
621 config = gst_buffer_pool_get_config(sink->video_buffer_pool);
622 gst_buffer_pool_config_get_params(config, &pool_caps, NULL, NULL, NULL);
623 need_pool = !gst_caps_is_equal(caps, pool_caps);
624 gst_structure_free(config);
627 g_clear_object(&sink->video_buffer_pool);
628 sink->video_buffer_size = 0;
631 pool = gst_vaapi_video_buffer_pool_new(sink->display);
633 goto error_create_pool;
635 gst_video_info_init(&vi);
636 gst_video_info_from_caps(&vi, caps);
637 if (GST_VIDEO_INFO_FORMAT(&vi) == GST_VIDEO_FORMAT_ENCODED) {
638 GST_DEBUG("assume video buffer pool format is NV12");
639 gst_video_info_set_format(&vi, GST_VIDEO_FORMAT_NV12,
640 GST_VIDEO_INFO_WIDTH(&vi), GST_VIDEO_INFO_HEIGHT(&vi));
642 sink->video_buffer_size = vi.size;
644 config = gst_buffer_pool_get_config(pool);
645 gst_buffer_pool_config_set_params(config, caps, sink->video_buffer_size,
647 gst_buffer_pool_config_add_option(config,
648 GST_BUFFER_POOL_OPTION_VAAPI_VIDEO_META);
649 if (!gst_buffer_pool_set_config(pool, config))
650 goto error_pool_config;
651 sink->video_buffer_pool = pool;
657 GST_ERROR("failed to create buffer pool");
662 GST_ERROR("failed to reset buffer pool config");
663 gst_object_unref(pool);
672 gst_vaapisink_start(GstBaseSink *base_sink)
674 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
676 if (!gst_vaapisink_ensure_display(sink))
679 sink->uploader = gst_vaapi_uploader_new(sink->display);
686 gst_vaapisink_stop(GstBaseSink *base_sink)
688 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
690 gst_buffer_replace(&sink->video_buffer, NULL);
691 #if GST_CHECK_VERSION(1,0,0)
692 g_clear_object(&sink->video_buffer_pool);
694 gst_vaapi_window_replace(&sink->window, NULL);
695 gst_vaapi_display_replace(&sink->display, NULL);
696 g_clear_object(&sink->uploader);
702 gst_vaapisink_get_caps_impl(GstBaseSink *base_sink)
704 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
705 GstCaps *out_caps, *yuv_caps;
707 out_caps = gst_caps_from_string(GST_VAAPI_SURFACE_CAPS);
711 if (gst_vaapisink_ensure_uploader(sink)) {
712 yuv_caps = gst_vaapi_uploader_get_caps(sink->uploader);
714 gst_caps_append(out_caps, gst_caps_copy(yuv_caps));
719 #if GST_CHECK_VERSION(1,0,0)
720 static inline GstCaps *
721 gst_vaapisink_get_caps(GstBaseSink *base_sink, GstCaps *filter)
723 GstCaps *caps, *out_caps;
725 caps = gst_vaapisink_get_caps_impl(base_sink);
726 if (caps && filter) {
727 out_caps = gst_caps_intersect_full(caps, filter,
728 GST_CAPS_INTERSECT_FIRST);
729 gst_caps_unref(caps);
736 #define gst_vaapisink_get_caps gst_vaapisink_get_caps_impl
740 gst_vaapisink_set_caps(GstBaseSink *base_sink, GstCaps *caps)
742 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
744 guint win_width, win_height;
747 if (sink->display_type == GST_VAAPI_DISPLAY_TYPE_DRM)
751 if (!gst_vaapisink_ensure_video_buffer_pool(sink, caps))
754 if (!gst_video_info_from_caps(&vi, caps))
756 sink->use_video_raw = GST_VIDEO_INFO_IS_YUV(&vi);
757 sink->video_width = GST_VIDEO_INFO_WIDTH(&vi);
758 sink->video_height = GST_VIDEO_INFO_HEIGHT(&vi);
759 sink->video_par_n = GST_VIDEO_INFO_PAR_N(&vi);
760 sink->video_par_d = GST_VIDEO_INFO_PAR_D(&vi);
761 GST_DEBUG("video pixel-aspect-ratio %d/%d",
762 sink->video_par_n, sink->video_par_d);
764 gst_caps_replace(&sink->caps, caps);
766 if (!gst_vaapisink_ensure_display(sink))
769 gst_vaapisink_ensure_rotation(sink, FALSE);
771 gst_vaapisink_ensure_window_size(sink, &win_width, &win_height);
773 if (!sink->foreign_window || sink->fullscreen)
774 gst_vaapi_window_set_size(sink->window, win_width, win_height);
777 gst_vaapi_display_lock(sink->display);
778 gst_video_overlay_prepare_window_handle(GST_VIDEO_OVERLAY(sink));
779 gst_vaapi_display_unlock(sink->display);
782 if (!gst_vaapisink_ensure_window(sink, win_width, win_height))
784 gst_vaapi_window_set_fullscreen(sink->window, sink->fullscreen);
785 gst_vaapi_window_show(sink->window);
786 gst_vaapi_window_get_size(sink->window, &win_width, &win_height);
788 sink->window_width = win_width;
789 sink->window_height = win_height;
790 GST_DEBUG("window size %ux%u", win_width, win_height);
792 return gst_vaapisink_ensure_render_rect(sink, win_width, win_height);
797 render_background(GstVaapiSink *sink)
799 /* Original code from Mirco Muller (MacSlow):
800 <http://cgit.freedesktop.org/~macslow/gl-gst-player/> */
801 GLfloat fStartX = 0.0f;
802 GLfloat fStartY = 0.0f;
803 GLfloat fWidth = (GLfloat)sink->window_width;
804 GLfloat fHeight = (GLfloat)sink->window_height;
806 glClear(GL_COLOR_BUFFER_BIT);
809 /* top third, darker grey to white */
810 glColor3f(0.85f, 0.85f, 0.85f);
811 glVertex3f(fStartX, fStartY, 0.0f);
812 glColor3f(0.85f, 0.85f, 0.85f);
813 glVertex3f(fStartX + fWidth, fStartY, 0.0f);
814 glColor3f(1.0f, 1.0f, 1.0f);
815 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
816 glColor3f(1.0f, 1.0f, 1.0f);
817 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
819 /* middle third, just plain white */
820 glColor3f(1.0f, 1.0f, 1.0f);
821 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
822 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
823 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
824 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
826 /* bottom third, white to lighter grey */
827 glColor3f(1.0f, 1.0f, 1.0f);
828 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
829 glColor3f(1.0f, 1.0f, 1.0f);
830 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
831 glColor3f(0.62f, 0.66f, 0.69f);
832 glVertex3f(fStartX + fWidth, fStartY + fHeight, 0.0f);
833 glColor3f(0.62f, 0.66f, 0.69f);
834 glVertex3f(fStartX, fStartY + fHeight, 0.0f);
840 render_frame(GstVaapiSink *sink, GstVaapiSurface *surface,
841 const GstVaapiRectangle *surface_rect)
843 const guint x1 = sink->display_rect.x;
844 const guint x2 = sink->display_rect.x + sink->display_rect.width;
845 const guint y1 = sink->display_rect.y;
846 const guint y2 = sink->display_rect.y + sink->display_rect.height;
847 gfloat tx1, tx2, ty1, ty2;
851 gst_vaapi_surface_get_size(surface, &width, &height);
852 tx1 = (gfloat)surface_rect->x / width;
853 ty1 = (gfloat)surface_rect->y / height;
854 tx2 = (gfloat)(surface_rect->x + surface_rect->width) / width;
855 ty2 = (gfloat)(surface_rect->y + surface_rect->height) / height;
864 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
867 glTexCoord2f(tx1, ty1); glVertex2i(x1, y1);
868 glTexCoord2f(tx1, ty2); glVertex2i(x1, y2);
869 glTexCoord2f(tx2, ty2); glVertex2i(x2, y2);
870 glTexCoord2f(tx2, ty1); glVertex2i(x2, y1);
876 render_reflection(GstVaapiSink *sink)
878 const guint x1 = sink->display_rect.x;
879 const guint x2 = sink->display_rect.x + sink->display_rect.width;
880 const guint y1 = sink->display_rect.y;
881 const guint rh = sink->display_rect.height / 5;
882 GLfloat ry = 1.0f - (GLfloat)rh / (GLfloat)sink->display_rect.height;
886 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
887 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y1);
888 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y1);
890 glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
891 glTexCoord2f(1.0f, ry); glVertex2i(x2, y1 + rh);
892 glTexCoord2f(0.0f, ry); glVertex2i(x1, y1 + rh);
898 gst_vaapisink_ensure_texture(GstVaapiSink *sink, GstVaapiSurface *surface)
900 GstVideoRectangle tex_rect, dis_rect, out_rect;
906 gst_vaapi_surface_get_size(surface, &width, &height);
912 gst_vaapi_display_get_size(sink->display, &width, &height);
918 gst_video_sink_center_rect(tex_rect, dis_rect, &out_rect, TRUE);
920 /* XXX: use surface size for now since some VA drivers have issues
921 with downscaling to the provided texture size. i.e. we should be
922 using the resulting out_rect size, which preserves the aspect
923 ratio of the surface */
926 GST_INFO("texture size %ux%u", width, height);
928 sink->texture = gst_vaapi_texture_new(sink->display,
929 GL_TEXTURE_2D, GL_BGRA, width, height);
930 return sink->texture != NULL;
934 gst_vaapisink_show_frame_glx(
936 GstVaapiSurface *surface,
937 const GstVaapiRectangle *surface_rect,
941 GstVaapiWindowGLX * const window = GST_VAAPI_WINDOW_GLX(sink->window);
945 gst_vaapi_window_glx_make_current(window);
946 if (!gst_vaapisink_ensure_texture(sink, surface))
947 goto error_create_texture;
948 if (!gst_vaapi_texture_put_surface(sink->texture, surface, flags))
949 goto error_transfer_surface;
951 target = gst_vaapi_texture_get_target(sink->texture);
952 texture = gst_vaapi_texture_get_id(sink->texture);
953 if (target != GL_TEXTURE_2D || !texture)
956 if (sink->use_reflection)
957 render_background(sink);
960 glBindTexture(target, texture);
962 if (sink->use_reflection) {
964 glRotatef(20.0f, 0.0f, 1.0f, 0.0f);
965 glTranslatef(50.0f, 0.0f, 0.0f);
967 render_frame(sink, surface, surface_rect);
968 if (sink->use_reflection) {
970 glTranslatef(0.0, (GLfloat)sink->display_rect.height + 5.0f, 0.0f);
971 render_reflection(sink);
976 glBindTexture(target, 0);
978 gst_vaapi_window_glx_swap_buffers(window);
982 error_create_texture:
984 GST_DEBUG("could not create VA/GLX texture");
987 error_transfer_surface:
989 GST_DEBUG("could not transfer VA surface to texture");
995 static inline gboolean
996 gst_vaapisink_put_surface(
998 GstVaapiSurface *surface,
999 const GstVaapiRectangle *surface_rect,
1003 if (!gst_vaapi_window_put_surface(sink->window, surface,
1004 surface_rect, &sink->display_rect, flags)) {
1005 GST_DEBUG("could not render VA surface");
1011 static GstFlowReturn
1012 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *src_buffer)
1014 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
1015 GstVaapiVideoMeta *meta;
1016 GstVaapiSurface *surface;
1020 GstVaapiRectangle *surface_rect = NULL;
1021 #if GST_CHECK_VERSION(1,0,0)
1022 GstVaapiRectangle tmp_rect;
1025 meta = gst_buffer_get_vaapi_video_meta(src_buffer);
1026 #if GST_CHECK_VERSION(1,0,0)
1028 return GST_FLOW_EOS;
1029 buffer = gst_buffer_ref(src_buffer);
1031 GstVideoCropMeta * const crop_meta =
1032 gst_buffer_get_video_crop_meta(buffer);
1034 surface_rect = &tmp_rect;
1035 surface_rect->x = crop_meta->x;
1036 surface_rect->y = crop_meta->y;
1037 surface_rect->width = crop_meta->width;
1038 surface_rect->height = crop_meta->height;
1042 buffer = gst_buffer_ref(src_buffer);
1043 else if (sink->use_video_raw) {
1044 buffer = gst_vaapi_uploader_get_buffer(sink->uploader);
1046 return GST_FLOW_EOS;
1047 meta = gst_buffer_get_vaapi_video_meta(buffer);
1052 return GST_FLOW_EOS;
1054 if (sink->use_video_raw &&
1055 !gst_vaapi_uploader_process(sink->uploader, src_buffer, buffer)) {
1056 GST_WARNING("failed to process raw YUV buffer");
1061 if (sink->display != gst_vaapi_video_meta_get_display(meta))
1062 gst_vaapi_display_replace(&sink->display,
1063 gst_vaapi_video_meta_get_display(meta));
1068 gst_vaapisink_ensure_rotation(sink, TRUE);
1070 surface = gst_vaapi_video_meta_get_surface(meta);
1074 GST_DEBUG("render surface %" GST_VAAPI_ID_FORMAT,
1075 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surface)));
1078 surface_rect = (GstVaapiRectangle *)
1079 gst_vaapi_video_meta_get_render_rect(meta);
1082 GST_DEBUG("render rect (%d,%d), size %ux%u",
1083 surface_rect->x, surface_rect->y,
1084 surface_rect->width, surface_rect->height);
1086 flags = gst_vaapi_video_meta_get_render_flags(meta);
1088 if (!gst_vaapi_apply_composition(surface, src_buffer))
1089 GST_WARNING("could not update subtitles");
1091 switch (sink->display_type) {
1093 case GST_VAAPI_DISPLAY_TYPE_DRM:
1098 case GST_VAAPI_DISPLAY_TYPE_GLX:
1100 goto put_surface_x11;
1101 success = gst_vaapisink_show_frame_glx(sink, surface, surface_rect,
1106 case GST_VAAPI_DISPLAY_TYPE_X11:
1108 success = gst_vaapisink_put_surface(sink, surface, surface_rect, flags);
1112 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
1113 success = gst_vaapisink_put_surface(sink, surface, surface_rect, flags);
1117 GST_ERROR("unsupported display type %d", sink->display_type);
1124 /* Retain VA surface until the next one is displayed */
1125 if (sink->use_overlay)
1126 gst_buffer_replace(&sink->video_buffer, buffer);
1127 gst_buffer_unref(buffer);
1131 gst_buffer_unref(buffer);
1132 return GST_FLOW_EOS;
1135 #if GST_CHECK_VERSION(1,0,0)
1137 gst_vaapisink_propose_allocation(GstBaseSink *base_sink, GstQuery *query)
1139 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
1140 GstCaps *caps = NULL;
1143 gst_query_parse_allocation(query, &caps, &need_pool);
1148 if (!gst_vaapisink_ensure_video_buffer_pool(sink, caps))
1150 gst_query_add_allocation_pool(query, sink->video_buffer_pool,
1151 sink->video_buffer_size, 0, 0);
1154 gst_query_add_allocation_meta(query,
1155 GST_VAAPI_VIDEO_META_API_TYPE, NULL);
1156 gst_query_add_allocation_meta(query,
1157 GST_VIDEO_META_API_TYPE, NULL);
1158 gst_query_add_allocation_meta(query,
1159 GST_VIDEO_CROP_META_API_TYPE, NULL);
1160 gst_query_add_allocation_meta(query,
1161 GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, NULL);
1167 GST_ERROR("no caps specified");
1172 static GstFlowReturn
1173 gst_vaapisink_buffer_alloc(
1174 GstBaseSink *base_sink,
1181 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
1187 if (!sink->use_video_raw) {
1188 /* Note: this code path is rarely used but for raw YUV formats
1189 from custom pipeline. Otherwise, GstBaseSink::set_caps() is
1190 called first, and GstBaseSink::buffer_alloc() is not called
1191 in VA surface format mode */
1192 if (!gst_video_info_from_caps(&vi, caps))
1193 return GST_FLOW_NOT_SUPPORTED;
1194 if (!GST_VIDEO_INFO_IS_YUV(&vi))
1198 if (!gst_vaapi_uploader_ensure_display(sink->uploader, sink->display))
1199 return GST_FLOW_NOT_SUPPORTED;
1200 if (!gst_vaapi_uploader_ensure_caps(sink->uploader, caps, NULL))
1201 return GST_FLOW_NOT_SUPPORTED;
1203 buf = gst_vaapi_uploader_get_buffer(sink->uploader);
1205 GST_WARNING("failed to allocate resources for raw YUV buffer");
1206 return GST_FLOW_NOT_SUPPORTED;
1215 gst_vaapisink_query(GstBaseSink *base_sink, GstQuery *query)
1217 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
1219 if (gst_vaapi_reply_to_query(query, sink->display)) {
1220 GST_DEBUG("sharing display %p", sink->display);
1223 return GST_BASE_SINK_CLASS(gst_vaapisink_parent_class)->query(base_sink,
1228 gst_vaapisink_finalize(GObject *object)
1230 gst_vaapisink_destroy(GST_VAAPISINK(object));
1232 G_OBJECT_CLASS(gst_vaapisink_parent_class)->finalize(object);
1236 gst_vaapisink_set_property(
1239 const GValue *value,
1243 GstVaapiSink * const sink = GST_VAAPISINK(object);
1246 case PROP_DISPLAY_TYPE:
1247 sink->display_type = g_value_get_enum(value);
1249 case PROP_FULLSCREEN:
1250 sink->fullscreen = g_value_get_boolean(value);
1252 case PROP_SYNCHRONOUS:
1253 sink->synchronous = g_value_get_boolean(value);
1256 sink->use_glx = g_value_get_boolean(value);
1258 case PROP_USE_REFLECTION:
1259 sink->use_reflection = g_value_get_boolean(value);
1262 sink->rotation_req = g_value_get_enum(value);
1265 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1271 gst_vaapisink_get_property(
1278 GstVaapiSink * const sink = GST_VAAPISINK(object);
1281 case PROP_DISPLAY_TYPE:
1282 g_value_set_enum(value, sink->display_type);
1284 case PROP_FULLSCREEN:
1285 g_value_set_boolean(value, sink->fullscreen);
1287 case PROP_SYNCHRONOUS:
1288 g_value_set_boolean(value, sink->synchronous);
1291 g_value_set_boolean(value, sink->use_glx);
1293 case PROP_USE_REFLECTION:
1294 g_value_set_boolean(value, sink->use_reflection);
1297 g_value_set_enum(value, sink->rotation);
1300 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1306 gst_vaapisink_class_init(GstVaapiSinkClass *klass)
1308 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
1309 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
1310 GstBaseSinkClass * const basesink_class = GST_BASE_SINK_CLASS(klass);
1311 GstPadTemplate *pad_template;
1313 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapisink,
1314 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
1316 object_class->finalize = gst_vaapisink_finalize;
1317 object_class->set_property = gst_vaapisink_set_property;
1318 object_class->get_property = gst_vaapisink_get_property;
1320 basesink_class->start = gst_vaapisink_start;
1321 basesink_class->stop = gst_vaapisink_stop;
1322 basesink_class->get_caps = gst_vaapisink_get_caps;
1323 basesink_class->set_caps = gst_vaapisink_set_caps;
1324 basesink_class->preroll = gst_vaapisink_show_frame;
1325 basesink_class->render = gst_vaapisink_show_frame;
1326 basesink_class->query = gst_vaapisink_query;
1327 #if GST_CHECK_VERSION(1,0,0)
1328 basesink_class->propose_allocation = gst_vaapisink_propose_allocation;
1330 basesink_class->buffer_alloc = gst_vaapisink_buffer_alloc;
1333 gst_element_class_set_static_metadata(element_class,
1337 "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");
1339 pad_template = gst_static_pad_template_get(&gst_vaapisink_sink_factory);
1340 gst_element_class_add_pad_template(element_class, pad_template);
1342 g_object_class_install_property
1345 g_param_spec_enum("display",
1347 "display type to use",
1348 GST_VAAPI_TYPE_DISPLAY_TYPE,
1349 GST_VAAPI_DISPLAY_TYPE_ANY,
1350 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1353 g_object_class_install_property
1356 g_param_spec_boolean("use-glx",
1358 "Enables OpenGL rendering",
1360 G_PARAM_READWRITE));
1362 g_object_class_install_property
1364 PROP_USE_REFLECTION,
1365 g_param_spec_boolean("use-reflection",
1366 "Reflection effect",
1367 "Enables OpenGL reflection effect",
1369 G_PARAM_READWRITE));
1372 g_object_class_install_property
1375 g_param_spec_boolean("fullscreen",
1377 "Requests window in fullscreen state",
1379 G_PARAM_READWRITE));
1382 * GstVaapiSink:synchronous:
1384 * When enabled, runs the X display in synchronous mode. Note that
1385 * this is used only for debugging.
1387 g_object_class_install_property
1390 g_param_spec_boolean("synchronous",
1392 "Toggles X display synchronous mode",
1394 G_PARAM_READWRITE));
1397 * GstVaapiSink:rotation:
1399 * The VA display rotation mode, expressed as a #GstVaapiRotation.
1401 g_object_class_install_property
1404 g_param_spec_enum(GST_VAAPI_DISPLAY_PROP_ROTATION,
1406 "The display rotation mode",
1407 GST_VAAPI_TYPE_ROTATION,
1409 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1413 gst_vaapisink_init(GstVaapiSink *sink)
1416 sink->display = NULL;
1417 sink->window = NULL;
1418 sink->window_width = 0;
1419 sink->window_height = 0;
1420 sink->texture = NULL;
1421 sink->video_buffer = NULL;
1422 sink->video_width = 0;
1423 sink->video_height = 0;
1424 sink->video_par_n = 1;
1425 sink->video_par_d = 1;
1426 sink->foreign_window = FALSE;
1427 sink->fullscreen = FALSE;
1428 sink->synchronous = FALSE;
1429 sink->display_type = DEFAULT_DISPLAY_TYPE;
1430 sink->rotation = DEFAULT_ROTATION;
1431 sink->rotation_req = DEFAULT_ROTATION;
1432 sink->use_reflection = FALSE;
1433 sink->use_overlay = FALSE;
1434 sink->use_rotation = FALSE;