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 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
222 GstBaseSink * const base_sink = GST_BASE_SINK(overlay);
225 if (sink->use_overlay)
226 buffer = sink->video_buffer ? gst_buffer_ref(sink->video_buffer) : NULL;
228 #if GST_CHECK_VERSION(1,0,0)
229 GstSample * const sample = gst_base_sink_get_last_sample(base_sink);
232 buffer = gst_buffer_ref(gst_sample_get_buffer(sample));
233 gst_sample_unref(sample);
235 buffer = gst_base_sink_get_last_buffer(base_sink);
239 gst_vaapisink_show_frame(base_sink, buffer);
240 gst_buffer_unref(buffer);
245 gst_vaapisink_video_overlay_iface_init(GstVideoOverlayInterface *iface)
247 iface->set_window_handle = gst_vaapisink_video_overlay_set_window_handle;
248 iface->set_render_rectangle = gst_vaapisink_video_overlay_set_render_rectangle;
249 iface->expose = gst_vaapisink_video_overlay_expose;
253 gst_vaapisink_destroy(GstVaapiSink *sink)
255 gst_buffer_replace(&sink->video_buffer, NULL);
256 gst_vaapi_texture_replace(&sink->texture, NULL);
257 gst_vaapi_display_replace(&sink->display, NULL);
258 g_clear_object(&sink->uploader);
260 gst_caps_replace(&sink->caps, NULL);
264 /* Checks whether a ConfigureNotify event is in the queue */
265 typedef struct _ConfigureNotifyEventPendingArgs ConfigureNotifyEventPendingArgs;
266 struct _ConfigureNotifyEventPendingArgs {
274 configure_notify_event_pending_cb(Display *dpy, XEvent *xev, XPointer arg)
276 ConfigureNotifyEventPendingArgs * const args =
277 (ConfigureNotifyEventPendingArgs *)arg;
279 if (xev->type == ConfigureNotify &&
280 xev->xconfigure.window == args->window &&
281 xev->xconfigure.width == args->width &&
282 xev->xconfigure.height == args->height)
285 /* XXX: this is a hack to traverse the whole queue because we
286 can't use XPeekIfEvent() since it could block */
291 configure_notify_event_pending(
298 ConfigureNotifyEventPendingArgs args;
301 args.window = window;
303 args.height = height;
306 /* XXX: don't use XPeekIfEvent() because it might block */
308 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
310 configure_notify_event_pending_cb, (XPointer)&args
317 get_display_type_name(GstVaapiDisplayType display_type)
319 gpointer const klass = g_type_class_peek(GST_VAAPI_TYPE_DISPLAY_TYPE);
320 GEnumValue * const e = g_enum_get_value(klass, display_type);
323 return e->value_name;
324 return "<unknown-type>";
327 static inline gboolean
328 gst_vaapisink_ensure_display(GstVaapiSink *sink)
330 GstVaapiDisplayType display_type;
331 GstVaapiRenderMode render_mode;
332 const gboolean had_display = sink->display != NULL;
334 if (!gst_vaapi_ensure_display(sink, sink->display_type, &sink->display))
337 display_type = gst_vaapi_display_get_display_type(sink->display);
338 if (display_type != sink->display_type || (!had_display && sink->display)) {
339 GST_INFO("created %s %p", get_display_type_name(display_type),
341 sink->display_type = display_type;
344 gst_vaapi_display_get_render_mode(sink->display, &render_mode) &&
345 render_mode == GST_VAAPI_RENDER_MODE_OVERLAY;
346 GST_DEBUG("use %s rendering mode", sink->use_overlay ? "overlay" : "texture");
348 sink->use_rotation = gst_vaapi_display_has_property(
349 sink->display, GST_VAAPI_DISPLAY_PROP_ROTATION);
355 gst_vaapisink_ensure_uploader(GstVaapiSink *sink)
357 if (!gst_vaapisink_ensure_display(sink))
360 if (!sink->uploader) {
361 sink->uploader = gst_vaapi_uploader_new(sink->display);
369 gst_vaapisink_ensure_render_rect(GstVaapiSink *sink, guint width, guint height)
371 GstVaapiRectangle * const display_rect = &sink->display_rect;
372 guint num, den, display_par_n, display_par_d;
375 /* Return success if caps are not set yet */
379 GST_DEBUG("ensure render rect within %ux%u bounds", width, height);
381 gst_vaapi_display_get_pixel_aspect_ratio(
383 &display_par_n, &display_par_d
385 GST_DEBUG("display pixel-aspect-ratio %d/%d",
386 display_par_n, display_par_d);
388 success = gst_video_calculate_display_ratio(
390 sink->video_width, sink->video_height,
391 sink->video_par_n, sink->video_par_d,
392 display_par_n, display_par_d
396 GST_DEBUG("video size %dx%d, calculated ratio %d/%d",
397 sink->video_width, sink->video_height, num, den);
399 display_rect->width = gst_util_uint64_scale_int(height, num, den);
400 if (display_rect->width <= width) {
401 GST_DEBUG("keeping window height");
402 display_rect->height = height;
405 GST_DEBUG("keeping window width");
406 display_rect->width = width;
407 display_rect->height =
408 gst_util_uint64_scale_int(width, den, num);
410 GST_DEBUG("scaling video to %ux%u", display_rect->width, display_rect->height);
412 g_assert(display_rect->width <= width);
413 g_assert(display_rect->height <= height);
415 display_rect->x = (width - display_rect->width) / 2;
416 display_rect->y = (height - display_rect->height) / 2;
418 GST_DEBUG("render rect (%d,%d):%ux%u",
419 display_rect->x, display_rect->y,
420 display_rect->width, display_rect->height);
425 gst_vaapisink_ensure_window_size(GstVaapiSink *sink, guint *pwidth, guint *pheight)
427 GstVideoRectangle src_rect, dst_rect, out_rect;
428 guint num, den, display_width, display_height, display_par_n, display_par_d;
429 gboolean success, scale;
431 if (sink->foreign_window) {
432 *pwidth = sink->window_width;
433 *pheight = sink->window_height;
437 gst_vaapi_display_get_size(sink->display, &display_width, &display_height);
438 if (sink->fullscreen) {
439 *pwidth = display_width;
440 *pheight = display_height;
444 gst_vaapi_display_get_pixel_aspect_ratio(
446 &display_par_n, &display_par_d
449 success = gst_video_calculate_display_ratio(
451 sink->video_width, sink->video_height,
452 sink->video_par_n, sink->video_par_d,
453 display_par_n, display_par_d
456 num = sink->video_par_n;
457 den = sink->video_par_d;
462 src_rect.w = gst_util_uint64_scale_int(sink->video_height, num, den);
463 src_rect.h = sink->video_height;
466 dst_rect.w = display_width;
467 dst_rect.h = display_height;
468 scale = (src_rect.w > dst_rect.w || src_rect.h > dst_rect.h);
469 gst_video_sink_center_rect(src_rect, dst_rect, &out_rect, scale);
470 *pwidth = out_rect.w;
471 *pheight = out_rect.h;
474 static inline gboolean
475 gst_vaapisink_ensure_window(GstVaapiSink *sink, guint width, guint height)
477 GstVaapiDisplay * const display = sink->display;
480 switch (sink->display_type) {
482 case GST_VAAPI_DISPLAY_TYPE_GLX:
483 sink->window = gst_vaapi_window_glx_new(display, width, height);
484 goto notify_video_overlay_interface;
487 case GST_VAAPI_DISPLAY_TYPE_X11:
488 sink->window = gst_vaapi_window_x11_new(display, width, height);
489 notify_video_overlay_interface:
492 gst_video_overlay_got_window_handle(
493 GST_VIDEO_OVERLAY(sink),
494 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window))
499 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
500 sink->window = gst_vaapi_window_wayland_new(display, width, height);
504 GST_ERROR("unsupported display type %d", sink->display_type);
508 return sink->window != NULL;
513 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id)
516 unsigned int width, height, border_width, depth;
520 if (!gst_vaapisink_ensure_display(sink))
523 gst_vaapi_display_lock(sink->display);
525 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
528 &x, &y, &width, &height, &border_width, &depth
530 gst_vaapi_display_unlock(sink->display);
532 if ((width != sink->window_width || height != sink->window_height) &&
533 !configure_notify_event_pending(sink, xid, width, height)) {
534 if (!gst_vaapisink_ensure_render_rect(sink, width, height))
536 sink->window_width = width;
537 sink->window_height = height;
541 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window)) == xid)
544 gst_vaapi_window_replace(&sink->window, NULL);
546 switch (sink->display_type) {
548 case GST_VAAPI_DISPLAY_TYPE_GLX:
549 sink->window = gst_vaapi_window_glx_new_with_xid(sink->display, xid);
552 case GST_VAAPI_DISPLAY_TYPE_X11:
553 sink->window = gst_vaapi_window_x11_new_with_xid(sink->display, xid);
556 GST_ERROR("unsupported display type %d", sink->display_type);
559 return sink->window != NULL;
564 gst_vaapisink_ensure_rotation(GstVaapiSink *sink, gboolean recalc_display_rect)
566 gboolean success = FALSE;
568 g_return_val_if_fail(sink->display, FALSE);
570 if (sink->rotation == sink->rotation_req)
573 if (!sink->use_rotation) {
574 GST_WARNING("VA display does not support rotation");
578 gst_vaapi_display_lock(sink->display);
579 success = gst_vaapi_display_set_rotation(sink->display, sink->rotation_req);
580 gst_vaapi_display_unlock(sink->display);
582 GST_ERROR("failed to change VA display rotation mode");
586 if (((sink->rotation + sink->rotation_req) % 180) == 90) {
587 /* Orientation changed */
588 G_PRIMITIVE_SWAP(guint, sink->video_width, sink->video_height);
589 G_PRIMITIVE_SWAP(gint, sink->video_par_n, sink->video_par_d);
592 if (recalc_display_rect && !sink->foreign_window)
593 gst_vaapisink_ensure_render_rect(sink, sink->window_width,
594 sink->window_height);
598 sink->rotation = sink->rotation_req;
603 gst_vaapisink_ensure_video_buffer_pool(GstVaapiSink *sink, GstCaps *caps)
605 #if GST_CHECK_VERSION(1,0,0)
608 GstStructure *config;
612 if (!gst_vaapisink_ensure_display(sink))
615 if (sink->video_buffer_pool) {
616 config = gst_buffer_pool_get_config(sink->video_buffer_pool);
617 gst_buffer_pool_config_get_params(config, &pool_caps, NULL, NULL, NULL);
618 need_pool = !gst_caps_is_equal(caps, pool_caps);
619 gst_structure_free(config);
622 g_clear_object(&sink->video_buffer_pool);
623 sink->video_buffer_size = 0;
626 pool = gst_vaapi_video_buffer_pool_new(sink->display);
628 goto error_create_pool;
630 gst_video_info_init(&vi);
631 gst_video_info_from_caps(&vi, caps);
632 if (GST_VIDEO_INFO_FORMAT(&vi) == GST_VIDEO_FORMAT_ENCODED) {
633 GST_DEBUG("assume video buffer pool format is NV12");
634 gst_video_info_set_format(&vi, GST_VIDEO_FORMAT_NV12,
635 GST_VIDEO_INFO_WIDTH(&vi), GST_VIDEO_INFO_HEIGHT(&vi));
637 sink->video_buffer_size = vi.size;
639 config = gst_buffer_pool_get_config(pool);
640 gst_buffer_pool_config_set_params(config, caps, sink->video_buffer_size,
642 gst_buffer_pool_config_add_option(config,
643 GST_BUFFER_POOL_OPTION_VAAPI_VIDEO_META);
644 if (!gst_buffer_pool_set_config(pool, config))
645 goto error_pool_config;
646 sink->video_buffer_pool = pool;
652 GST_ERROR("failed to create buffer pool");
657 GST_ERROR("failed to reset buffer pool config");
658 gst_object_unref(pool);
667 gst_vaapisink_start(GstBaseSink *base_sink)
669 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
671 if (!gst_vaapisink_ensure_display(sink))
674 sink->uploader = gst_vaapi_uploader_new(sink->display);
681 gst_vaapisink_stop(GstBaseSink *base_sink)
683 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
685 gst_buffer_replace(&sink->video_buffer, NULL);
686 #if GST_CHECK_VERSION(1,0,0)
687 g_clear_object(&sink->video_buffer_pool);
689 gst_vaapi_window_replace(&sink->window, NULL);
690 gst_vaapi_display_replace(&sink->display, NULL);
691 g_clear_object(&sink->uploader);
697 gst_vaapisink_get_caps_impl(GstBaseSink *base_sink)
699 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
700 GstCaps *out_caps, *yuv_caps;
702 out_caps = gst_caps_from_string(GST_VAAPI_SURFACE_CAPS);
706 if (gst_vaapisink_ensure_uploader(sink)) {
707 yuv_caps = gst_vaapi_uploader_get_caps(sink->uploader);
709 gst_caps_append(out_caps, gst_caps_copy(yuv_caps));
714 #if GST_CHECK_VERSION(1,0,0)
715 static inline GstCaps *
716 gst_vaapisink_get_caps(GstBaseSink *base_sink, GstCaps *filter)
718 return gst_vaapisink_get_caps_impl(base_sink);
721 #define gst_vaapisink_get_caps gst_vaapisink_get_caps_impl
725 gst_vaapisink_set_caps(GstBaseSink *base_sink, GstCaps *caps)
727 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
729 guint win_width, win_height;
732 if (sink->display_type == GST_VAAPI_DISPLAY_TYPE_DRM)
736 if (!gst_vaapisink_ensure_video_buffer_pool(sink, caps))
739 if (!gst_video_info_from_caps(&vi, caps))
741 sink->use_video_raw = GST_VIDEO_INFO_IS_YUV(&vi);
742 sink->video_width = GST_VIDEO_INFO_WIDTH(&vi);
743 sink->video_height = GST_VIDEO_INFO_HEIGHT(&vi);
744 sink->video_par_n = GST_VIDEO_INFO_PAR_N(&vi);
745 sink->video_par_d = GST_VIDEO_INFO_PAR_D(&vi);
746 GST_DEBUG("video pixel-aspect-ratio %d/%d",
747 sink->video_par_n, sink->video_par_d);
749 gst_caps_replace(&sink->caps, caps);
751 if (!gst_vaapisink_ensure_display(sink))
754 gst_vaapisink_ensure_rotation(sink, FALSE);
756 gst_vaapisink_ensure_window_size(sink, &win_width, &win_height);
758 if (!sink->foreign_window || sink->fullscreen)
759 gst_vaapi_window_set_size(sink->window, win_width, win_height);
762 gst_vaapi_display_lock(sink->display);
763 gst_video_overlay_prepare_window_handle(GST_VIDEO_OVERLAY(sink));
764 gst_vaapi_display_unlock(sink->display);
767 if (!gst_vaapisink_ensure_window(sink, win_width, win_height))
769 gst_vaapi_window_set_fullscreen(sink->window, sink->fullscreen);
770 gst_vaapi_window_show(sink->window);
771 gst_vaapi_window_get_size(sink->window, &win_width, &win_height);
773 sink->window_width = win_width;
774 sink->window_height = win_height;
775 GST_DEBUG("window size %ux%u", win_width, win_height);
777 return gst_vaapisink_ensure_render_rect(sink, win_width, win_height);
782 render_background(GstVaapiSink *sink)
784 /* Original code from Mirco Muller (MacSlow):
785 <http://cgit.freedesktop.org/~macslow/gl-gst-player/> */
786 GLfloat fStartX = 0.0f;
787 GLfloat fStartY = 0.0f;
788 GLfloat fWidth = (GLfloat)sink->window_width;
789 GLfloat fHeight = (GLfloat)sink->window_height;
791 glClear(GL_COLOR_BUFFER_BIT);
794 /* top third, darker grey to white */
795 glColor3f(0.85f, 0.85f, 0.85f);
796 glVertex3f(fStartX, fStartY, 0.0f);
797 glColor3f(0.85f, 0.85f, 0.85f);
798 glVertex3f(fStartX + fWidth, fStartY, 0.0f);
799 glColor3f(1.0f, 1.0f, 1.0f);
800 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
801 glColor3f(1.0f, 1.0f, 1.0f);
802 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
804 /* middle third, just plain white */
805 glColor3f(1.0f, 1.0f, 1.0f);
806 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
807 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
808 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
809 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
811 /* bottom third, white to lighter grey */
812 glColor3f(1.0f, 1.0f, 1.0f);
813 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
814 glColor3f(1.0f, 1.0f, 1.0f);
815 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
816 glColor3f(0.62f, 0.66f, 0.69f);
817 glVertex3f(fStartX + fWidth, fStartY + fHeight, 0.0f);
818 glColor3f(0.62f, 0.66f, 0.69f);
819 glVertex3f(fStartX, fStartY + fHeight, 0.0f);
825 render_frame(GstVaapiSink *sink)
827 const guint x1 = sink->display_rect.x;
828 const guint x2 = sink->display_rect.x + sink->display_rect.width;
829 const guint y1 = sink->display_rect.y;
830 const guint y2 = sink->display_rect.y + sink->display_rect.height;
832 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
835 glTexCoord2f(0.0f, 0.0f); glVertex2i(x1, y1);
836 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y2);
837 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y2);
838 glTexCoord2f(1.0f, 0.0f); glVertex2i(x2, y1);
844 render_reflection(GstVaapiSink *sink)
846 const guint x1 = sink->display_rect.x;
847 const guint x2 = sink->display_rect.x + sink->display_rect.width;
848 const guint y1 = sink->display_rect.y;
849 const guint rh = sink->display_rect.height / 5;
850 GLfloat ry = 1.0f - (GLfloat)rh / (GLfloat)sink->display_rect.height;
854 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
855 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y1);
856 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y1);
858 glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
859 glTexCoord2f(1.0f, ry); glVertex2i(x2, y1 + rh);
860 glTexCoord2f(0.0f, ry); glVertex2i(x1, y1 + rh);
866 gst_vaapisink_show_frame_glx(
868 GstVaapiSurface *surface,
872 GstVaapiWindowGLX * const window = GST_VAAPI_WINDOW_GLX(sink->window);
876 gst_vaapi_window_glx_make_current(window);
877 if (!sink->texture) {
878 sink->texture = gst_vaapi_texture_new(
886 goto error_create_texture;
888 if (!gst_vaapi_texture_put_surface(sink->texture, surface, flags))
889 goto error_transfer_surface;
891 target = gst_vaapi_texture_get_target(sink->texture);
892 texture = gst_vaapi_texture_get_id(sink->texture);
893 if (target != GL_TEXTURE_2D || !texture)
896 if (sink->use_reflection)
897 render_background(sink);
900 glBindTexture(target, texture);
902 if (sink->use_reflection) {
904 glRotatef(20.0f, 0.0f, 1.0f, 0.0f);
905 glTranslatef(50.0f, 0.0f, 0.0f);
908 if (sink->use_reflection) {
910 glTranslatef(0.0, (GLfloat)sink->display_rect.height + 5.0f, 0.0f);
911 render_reflection(sink);
916 glBindTexture(target, 0);
918 gst_vaapi_window_glx_swap_buffers(window);
922 error_create_texture:
924 GST_DEBUG("could not create VA/GLX texture");
927 error_transfer_surface:
929 GST_DEBUG("could not transfer VA surface to texture");
935 static inline gboolean
936 gst_vaapisink_put_surface(
938 GstVaapiSurface *surface,
942 if (!gst_vaapi_window_put_surface(sink->window, surface,
943 NULL, &sink->display_rect, flags)) {
944 GST_DEBUG("could not render VA surface");
951 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *src_buffer)
953 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
954 GstVaapiVideoMeta *meta;
955 GstVaapiSurface *surface;
960 meta = gst_buffer_get_vaapi_video_meta(src_buffer);
961 #if GST_CHECK_VERSION(1,0,0)
964 buffer = gst_buffer_ref(src_buffer);
967 buffer = gst_buffer_ref(src_buffer);
968 else if (sink->use_video_raw) {
969 buffer = gst_vaapi_uploader_get_buffer(sink->uploader);
972 meta = gst_buffer_get_vaapi_video_meta(buffer);
979 if (sink->use_video_raw &&
980 !gst_vaapi_uploader_process(sink->uploader, src_buffer, buffer)) {
981 GST_WARNING("failed to process raw YUV buffer");
986 if (sink->display != gst_vaapi_video_meta_get_display(meta))
987 gst_vaapi_display_replace(&sink->display,
988 gst_vaapi_video_meta_get_display(meta));
993 gst_vaapisink_ensure_rotation(sink, TRUE);
995 surface = gst_vaapi_video_meta_get_surface(meta);
999 GST_DEBUG("render surface %" GST_VAAPI_ID_FORMAT,
1000 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surface)));
1002 flags = gst_vaapi_video_meta_get_render_flags(meta);
1004 if (!gst_vaapi_apply_composition(surface, src_buffer))
1005 GST_WARNING("could not update subtitles");
1007 switch (sink->display_type) {
1009 case GST_VAAPI_DISPLAY_TYPE_GLX:
1010 success = gst_vaapisink_show_frame_glx(sink, surface, flags);
1014 case GST_VAAPI_DISPLAY_TYPE_DRM:
1019 case GST_VAAPI_DISPLAY_TYPE_X11:
1020 success = gst_vaapisink_put_surface(sink, surface, flags);
1024 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
1025 success = gst_vaapisink_put_surface(sink, surface, flags);
1029 GST_ERROR("unsupported display type %d", sink->display_type);
1036 /* Retain VA surface until the next one is displayed */
1037 if (sink->use_overlay)
1038 gst_buffer_replace(&sink->video_buffer, buffer);
1039 gst_buffer_unref(buffer);
1043 gst_buffer_unref(buffer);
1044 return GST_FLOW_EOS;
1047 #if GST_CHECK_VERSION(1,0,0)
1049 gst_vaapisink_propose_allocation(GstBaseSink *base_sink, GstQuery *query)
1051 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
1052 GstCaps *caps = NULL;
1055 gst_query_parse_allocation(query, &caps, &need_pool);
1060 if (!gst_vaapisink_ensure_video_buffer_pool(sink, caps))
1062 gst_query_add_allocation_pool(query, sink->video_buffer_pool,
1063 sink->video_buffer_size, 0, 0);
1066 gst_query_add_allocation_meta(query,
1067 GST_VAAPI_VIDEO_META_API_TYPE, NULL);
1068 gst_query_add_allocation_meta(query,
1069 GST_VIDEO_META_API_TYPE, NULL);
1070 gst_query_add_allocation_meta(query,
1071 GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, NULL);
1077 GST_ERROR("no caps specified");
1082 static GstFlowReturn
1083 gst_vaapisink_buffer_alloc(
1084 GstBaseSink *base_sink,
1091 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
1097 if (!sink->use_video_raw) {
1098 /* Note: this code path is rarely used but for raw YUV formats
1099 from custom pipeline. Otherwise, GstBaseSink::set_caps() is
1100 called first, and GstBaseSink::buffer_alloc() is not called
1101 in VA surface format mode */
1102 if (!gst_video_info_from_caps(&vi, caps))
1103 return GST_FLOW_NOT_SUPPORTED;
1104 if (!GST_VIDEO_INFO_IS_YUV(&vi))
1108 if (!gst_vaapi_uploader_ensure_display(sink->uploader, sink->display))
1109 return GST_FLOW_NOT_SUPPORTED;
1110 if (!gst_vaapi_uploader_ensure_caps(sink->uploader, caps, NULL))
1111 return GST_FLOW_NOT_SUPPORTED;
1113 buf = gst_vaapi_uploader_get_buffer(sink->uploader);
1115 GST_WARNING("failed to allocate resources for raw YUV buffer");
1116 return GST_FLOW_NOT_SUPPORTED;
1125 gst_vaapisink_query(GstBaseSink *base_sink, GstQuery *query)
1127 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
1129 if (gst_vaapi_reply_to_query(query, sink->display)) {
1130 GST_DEBUG("sharing display %p", sink->display);
1133 return GST_BASE_SINK_CLASS(gst_vaapisink_parent_class)->query(base_sink,
1138 gst_vaapisink_finalize(GObject *object)
1140 gst_vaapisink_destroy(GST_VAAPISINK(object));
1142 G_OBJECT_CLASS(gst_vaapisink_parent_class)->finalize(object);
1146 gst_vaapisink_set_property(
1149 const GValue *value,
1153 GstVaapiSink * const sink = GST_VAAPISINK(object);
1156 case PROP_DISPLAY_TYPE:
1157 sink->display_type = g_value_get_enum(value);
1159 case PROP_FULLSCREEN:
1160 sink->fullscreen = g_value_get_boolean(value);
1162 case PROP_SYNCHRONOUS:
1163 sink->synchronous = g_value_get_boolean(value);
1165 case PROP_USE_REFLECTION:
1166 sink->use_reflection = g_value_get_boolean(value);
1169 sink->rotation_req = g_value_get_enum(value);
1172 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1178 gst_vaapisink_get_property(
1185 GstVaapiSink * const sink = GST_VAAPISINK(object);
1188 case PROP_DISPLAY_TYPE:
1189 g_value_set_enum(value, sink->display_type);
1191 case PROP_FULLSCREEN:
1192 g_value_set_boolean(value, sink->fullscreen);
1194 case PROP_SYNCHRONOUS:
1195 g_value_set_boolean(value, sink->synchronous);
1197 case PROP_USE_REFLECTION:
1198 g_value_set_boolean(value, sink->use_reflection);
1201 g_value_set_enum(value, sink->rotation);
1204 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1210 gst_vaapisink_class_init(GstVaapiSinkClass *klass)
1212 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
1213 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
1214 GstBaseSinkClass * const basesink_class = GST_BASE_SINK_CLASS(klass);
1215 GstPadTemplate *pad_template;
1217 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapisink,
1218 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
1220 object_class->finalize = gst_vaapisink_finalize;
1221 object_class->set_property = gst_vaapisink_set_property;
1222 object_class->get_property = gst_vaapisink_get_property;
1224 basesink_class->start = gst_vaapisink_start;
1225 basesink_class->stop = gst_vaapisink_stop;
1226 basesink_class->get_caps = gst_vaapisink_get_caps;
1227 basesink_class->set_caps = gst_vaapisink_set_caps;
1228 basesink_class->preroll = gst_vaapisink_show_frame;
1229 basesink_class->render = gst_vaapisink_show_frame;
1230 basesink_class->query = gst_vaapisink_query;
1231 #if GST_CHECK_VERSION(1,0,0)
1232 basesink_class->propose_allocation = gst_vaapisink_propose_allocation;
1234 basesink_class->buffer_alloc = gst_vaapisink_buffer_alloc;
1237 gst_element_class_set_static_metadata(element_class,
1241 "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");
1243 pad_template = gst_static_pad_template_get(&gst_vaapisink_sink_factory);
1244 gst_element_class_add_pad_template(element_class, pad_template);
1246 g_object_class_install_property
1249 g_param_spec_enum("display",
1251 "display type to use",
1252 GST_VAAPI_TYPE_DISPLAY_TYPE,
1253 GST_VAAPI_DISPLAY_TYPE_ANY,
1254 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1257 g_object_class_install_property
1259 PROP_USE_REFLECTION,
1260 g_param_spec_boolean("use-reflection",
1261 "Reflection effect",
1262 "Enables OpenGL reflection effect",
1264 G_PARAM_READWRITE));
1267 g_object_class_install_property
1270 g_param_spec_boolean("fullscreen",
1272 "Requests window in fullscreen state",
1274 G_PARAM_READWRITE));
1277 * GstVaapiSink:synchronous:
1279 * When enabled, runs the X display in synchronous mode. Note that
1280 * this is used only for debugging.
1282 g_object_class_install_property
1285 g_param_spec_boolean("synchronous",
1287 "Toggles X display synchronous mode",
1289 G_PARAM_READWRITE));
1292 * GstVaapiSink:rotation:
1294 * The VA display rotation mode, expressed as a #GstVaapiRotation.
1296 g_object_class_install_property
1299 g_param_spec_enum(GST_VAAPI_DISPLAY_PROP_ROTATION,
1301 "The display rotation mode",
1302 GST_VAAPI_TYPE_ROTATION,
1304 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1308 gst_vaapisink_init(GstVaapiSink *sink)
1311 sink->display = NULL;
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->display_type = DEFAULT_DISPLAY_TYPE;
1325 sink->rotation = DEFAULT_ROTATION;
1326 sink->rotation_req = DEFAULT_ROTATION;
1327 sink->use_reflection = FALSE;
1328 sink->use_overlay = FALSE;
1329 sink->use_rotation = FALSE;