2 * gstvaapisink.c - VA-API video sink
4 * gstreamer-vaapi (C) 2010-2011 Splitted-Desktop Systems
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1
9 * of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
23 * SECTION:gstvaapisink
24 * @short_description: A VA-API based videosink
26 * vaapisink renders video frames to a drawable (X #Window) on a local
27 * display using the Video Acceleration (VA) API. The element will
28 * create its own internal window and render into it.
33 #include <gst/video/video.h>
34 #include <gst/vaapi/gstvaapivideobuffer.h>
35 #include <gst/vaapi/gstvaapivideosink.h>
36 #include <gst/vaapi/gstvaapidisplay_x11.h>
37 #include <gst/vaapi/gstvaapiwindow_x11.h>
39 #include <gst/vaapi/gstvaapidisplay_glx.h>
40 #include <gst/vaapi/gstvaapiwindow_glx.h>
42 #include "gstvaapisink.h"
44 /* Supported interfaces */
45 #include <gst/interfaces/xoverlay.h>
47 #define GST_PLUGIN_NAME "vaapisink"
48 #define GST_PLUGIN_DESC "A VA-API based videosink"
50 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapisink);
51 #define GST_CAT_DEFAULT gst_debug_vaapisink
53 /* ElementFactory information */
54 static const GstElementDetails gst_vaapisink_details =
59 "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");
61 /* Default template */
62 static GstStaticPadTemplate gst_vaapisink_sink_factory =
63 GST_STATIC_PAD_TEMPLATE(
67 GST_STATIC_CAPS(GST_VAAPI_SURFACE_CAPS));
69 static void gst_vaapisink_iface_init(GType type);
76 gst_vaapisink_iface_init);
88 /* GstImplementsInterface interface */
91 gst_vaapisink_implements_interface_supported(
92 GstImplementsInterface *iface,
96 return (type == GST_VAAPI_TYPE_VIDEO_SINK ||
97 type == GST_TYPE_X_OVERLAY);
101 gst_vaapisink_implements_iface_init(GstImplementsInterfaceClass *iface)
103 iface->supported = gst_vaapisink_implements_interface_supported;
106 /* GstVaapiVideoSink interface */
108 static GstVaapiDisplay *
109 gst_vaapisink_get_display(GstVaapiSink *sink);
111 static GstVaapiDisplay *
112 gst_vaapi_video_sink_do_get_display(GstVaapiVideoSink *sink)
114 return gst_vaapisink_get_display(GST_VAAPISINK(sink));
118 gst_vaapi_video_sink_iface_init(GstVaapiVideoSinkInterface *iface)
120 iface->get_display = gst_vaapi_video_sink_do_get_display;
123 /* GstXOverlay interface */
126 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id);
129 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *buffer);
132 gst_vaapisink_xoverlay_set_window_handle(GstXOverlay *overlay, guintptr window_id)
134 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
136 /* Disable GLX rendering when vaapisink is using a foreign X
137 window. It's pretty much useless */
138 sink->use_glx = FALSE;
140 sink->foreign_window = TRUE;
141 gst_vaapisink_ensure_window_xid(sink, window_id);
145 gst_vaapisink_xoverlay_expose(GstXOverlay *overlay)
147 GstBaseSink * const base_sink = GST_BASE_SINK(overlay);
150 buffer = gst_base_sink_get_last_buffer(base_sink);
152 gst_vaapisink_show_frame(base_sink, buffer);
153 gst_buffer_unref(buffer);
158 gst_vaapisink_xoverlay_iface_init(GstXOverlayClass *iface)
160 iface->set_window_handle = gst_vaapisink_xoverlay_set_window_handle;
161 iface->expose = gst_vaapisink_xoverlay_expose;
165 gst_vaapisink_iface_init(GType type)
167 const GType g_define_type_id = type;
169 G_IMPLEMENT_INTERFACE(GST_TYPE_IMPLEMENTS_INTERFACE,
170 gst_vaapisink_implements_iface_init);
171 G_IMPLEMENT_INTERFACE(GST_VAAPI_TYPE_VIDEO_SINK,
172 gst_vaapi_video_sink_iface_init);
173 G_IMPLEMENT_INTERFACE(GST_TYPE_X_OVERLAY,
174 gst_vaapisink_xoverlay_iface_init);
178 gst_vaapisink_destroy(GstVaapiSink *sink)
181 g_object_unref(sink->display);
182 sink->display = NULL;
185 if (sink->display_name) {
186 g_free(sink->display_name);
187 sink->display_name = NULL;
191 /* Checks whether a ConfigureNotify event is in the queue */
192 typedef struct _ConfigureNotifyEventPendingArgs ConfigureNotifyEventPendingArgs;
193 struct _ConfigureNotifyEventPendingArgs {
201 configure_notify_event_pending_cb(Display *dpy, XEvent *xev, XPointer arg)
203 ConfigureNotifyEventPendingArgs * const args =
204 (ConfigureNotifyEventPendingArgs *)arg;
206 if (xev->type == ConfigureNotify &&
207 xev->xconfigure.window == args->window &&
208 xev->xconfigure.width == args->width &&
209 xev->xconfigure.height == args->height)
212 /* XXX: this is a hack to traverse the whole queue because we
213 can't use XPeekIfEvent() since it could block */
218 configure_notify_event_pending(
225 ConfigureNotifyEventPendingArgs args;
228 args.window = window;
230 args.height = height;
233 /* XXX: don't use XPeekIfEvent() because it might block */
235 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
237 configure_notify_event_pending_cb, (XPointer)&args
242 static inline gboolean
243 gst_vaapisink_ensure_display(GstVaapiSink *sink)
245 if (!sink->display) {
246 #if USE_VAAPISINK_GLX
248 sink->display = gst_vaapi_display_glx_new(sink->display_name);
251 sink->display = gst_vaapi_display_x11_new(sink->display_name);
252 if (!sink->display || !gst_vaapi_display_get_display(sink->display))
254 g_object_set(sink, "synchronous", sink->synchronous, NULL);
256 return sink->display != NULL;
260 gst_vaapisink_ensure_render_rect(GstVaapiSink *sink, guint width, guint height)
262 GstVaapiRectangle * const display_rect = &sink->display_rect;
263 guint num, den, display_par_n, display_par_d;
264 double display_ratio;
267 GST_DEBUG("ensure render rect within %ux%u bounds", width, height);
269 gst_vaapi_display_get_pixel_aspect_ratio(
271 &display_par_n, &display_par_d
273 GST_DEBUG("display pixel-aspect-ratio %d/%d",
274 display_par_n, display_par_d);
276 success = gst_video_calculate_display_ratio(
278 sink->video_width, sink->video_height,
279 sink->video_par_n, sink->video_par_d,
280 display_par_n, display_par_d
284 GST_DEBUG("video size %dx%d, calculated ratio %d/%d",
285 sink->video_width, sink->video_height, num, den);
287 if ((sink->video_height % den) == 0) {
288 GST_DEBUG("keeping video height");
289 display_rect->width =
290 gst_util_uint64_scale_int(sink->video_height, num, den);
291 display_rect->height = sink->video_height;
293 else if ((sink->video_width % num) == 0) {
294 GST_DEBUG("keeping video width");
295 display_rect->width = sink->video_width;
296 display_rect->height =
297 gst_util_uint64_scale_int(sink->video_width, den, num);
300 GST_DEBUG("approximating while keeping video height");
301 display_rect->width =
302 gst_util_uint64_scale_int(sink->video_height, num, den);
303 display_rect->height = sink->video_height;
305 display_ratio = (gdouble)display_rect->width / display_rect->height;
306 GST_DEBUG("scaling to %ux%u", display_rect->width, display_rect->height);
308 if (sink->fullscreen || sink->foreign_window ||
309 display_rect->width > width || display_rect->height > height) {
310 if (sink->video_width > sink->video_height) {
311 display_rect->width = width;
312 display_rect->height = width / display_ratio;
315 display_rect->width = height * display_ratio;
316 display_rect->height = height;
320 if (sink->fullscreen) {
321 display_rect->x = (width - display_rect->width) / 2;
322 display_rect->y = (height - display_rect->height) / 2;
329 GST_DEBUG("render rect (%d,%d):%ux%u",
330 display_rect->x, display_rect->y,
331 display_rect->width, display_rect->height);
335 static inline gboolean
336 gst_vaapisink_ensure_window(GstVaapiSink *sink, guint width, guint height)
338 GstVaapiDisplay * const display = sink->display;
341 #if USE_VAAPISINK_GLX
343 sink->window = gst_vaapi_window_glx_new(display, width, height);
346 sink->window = gst_vaapi_window_x11_new(display, width, height);
348 gst_x_overlay_got_xwindow_id(
350 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window))
353 return sink->window != NULL;
357 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id)
360 unsigned int width, height, border_width, depth;
364 if (!gst_vaapisink_ensure_display(sink))
367 gst_vaapi_display_lock(sink->display);
369 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
372 &x, &y, &width, &height, &border_width, &depth
374 gst_vaapi_display_unlock(sink->display);
376 if ((width != sink->window_width || height != sink->window_height) &&
377 !configure_notify_event_pending(sink, xid, width, height)) {
378 if (!gst_vaapisink_ensure_render_rect(sink, width, height))
380 sink->window_width = width;
381 sink->window_height = height;
385 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window)) == xid)
389 g_object_unref(sink->window);
393 #if USE_VAAPISINK_GLX
395 sink->window = gst_vaapi_window_glx_new_with_xid(sink->display, xid);
398 sink->window = gst_vaapi_window_x11_new_with_xid(sink->display, xid);
399 return sink->window != NULL;
403 gst_vaapisink_start(GstBaseSink *base_sink)
405 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
407 if (!gst_vaapisink_ensure_display(sink))
413 gst_vaapisink_stop(GstBaseSink *base_sink)
415 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
418 g_object_unref(sink->window);
423 g_object_unref(sink->display);
424 sink->display = NULL;
430 gst_vaapisink_set_caps(GstBaseSink *base_sink, GstCaps *caps)
432 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
433 GstStructure * const structure = gst_caps_get_structure(caps, 0);
434 guint display_width, display_height, win_width, win_height;
435 gint video_width, video_height, video_par_n = 1, video_par_d = 1;
439 if (!gst_structure_get_int(structure, "width", &video_width))
441 if (!gst_structure_get_int(structure, "height", &video_height))
443 sink->video_width = video_width;
444 sink->video_height = video_height;
446 gst_video_parse_caps_pixel_aspect_ratio(caps, &video_par_n, &video_par_d);
447 sink->video_par_n = video_par_n;
448 sink->video_par_d = video_par_d;
449 GST_DEBUG("video pixel-aspect-ratio %d/%d", video_par_n, video_par_d);
451 gst_vaapi_display_get_size(sink->display, &display_width, &display_height);
452 if (!gst_vaapisink_ensure_render_rect(sink, display_width, display_height))
455 if (sink->fullscreen) {
456 win_width = display_width;
457 win_height = display_height;
460 win_width = sink->display_rect.width;
461 win_height = sink->display_rect.height;
465 gst_vaapi_window_set_size(sink->window, win_width, win_height);
467 gst_vaapi_display_lock(sink->display);
468 gst_x_overlay_prepare_xwindow_id(GST_X_OVERLAY(sink));
469 gst_vaapi_display_unlock(sink->display);
472 if (!gst_vaapisink_ensure_window(sink, win_width, win_height))
474 gst_vaapi_window_set_fullscreen(sink->window, sink->fullscreen);
475 gst_vaapi_window_show(sink->window);
477 sink->window_width = win_width;
478 sink->window_height = win_height;
483 gst_vaapisink_buffer_alloc(
484 GstBaseSink *base_sink,
488 GstBuffer **pout_buffer
491 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
492 GstStructure *structure;
495 structure = gst_caps_get_structure(caps, 0);
496 if (!gst_structure_has_name(structure, "video/x-vaapi-surface"))
497 goto error_invalid_caps;
499 buffer = gst_vaapi_video_buffer_new(sink->display);
501 goto error_create_buffer;
503 gst_buffer_set_caps(buffer, caps);
504 *pout_buffer = buffer;
510 GST_DEBUG("failed to validate input caps");
511 return GST_FLOW_UNEXPECTED;
515 GST_DEBUG("failed to create video buffer");
516 return GST_FLOW_UNEXPECTED;
520 #if USE_VAAPISINK_GLX
522 render_background(GstVaapiSink *sink)
524 /* Original code from Mirco Muller (MacSlow):
525 <http://cgit.freedesktop.org/~macslow/gl-gst-player/> */
526 GLfloat fStartX = 0.0f;
527 GLfloat fStartY = 0.0f;
528 GLfloat fWidth = (GLfloat)sink->window_width;
529 GLfloat fHeight = (GLfloat)sink->window_height;
531 glClear(GL_COLOR_BUFFER_BIT);
534 /* top third, darker grey to white */
535 glColor3f(0.85f, 0.85f, 0.85f);
536 glVertex3f(fStartX, fStartY, 0.0f);
537 glColor3f(0.85f, 0.85f, 0.85f);
538 glVertex3f(fStartX + fWidth, fStartY, 0.0f);
539 glColor3f(1.0f, 1.0f, 1.0f);
540 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
541 glColor3f(1.0f, 1.0f, 1.0f);
542 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
544 /* middle third, just plain white */
545 glColor3f(1.0f, 1.0f, 1.0f);
546 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
547 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
548 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
549 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
551 /* bottom third, white to lighter grey */
552 glColor3f(1.0f, 1.0f, 1.0f);
553 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
554 glColor3f(1.0f, 1.0f, 1.0f);
555 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
556 glColor3f(0.62f, 0.66f, 0.69f);
557 glVertex3f(fStartX + fWidth, fStartY + fHeight, 0.0f);
558 glColor3f(0.62f, 0.66f, 0.69f);
559 glVertex3f(fStartX, fStartY + fHeight, 0.0f);
565 render_frame(GstVaapiSink *sink)
567 const guint x1 = sink->display_rect.x;
568 const guint x2 = sink->display_rect.x + sink->display_rect.width;
569 const guint y1 = sink->display_rect.y;
570 const guint y2 = sink->display_rect.y + sink->display_rect.height;
572 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
575 glTexCoord2f(0.0f, 0.0f); glVertex2i(x1, y1);
576 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y2);
577 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y2);
578 glTexCoord2f(1.0f, 0.0f); glVertex2i(x2, y1);
584 render_reflection(GstVaapiSink *sink)
586 const guint x1 = sink->display_rect.x;
587 const guint x2 = sink->display_rect.x + sink->display_rect.width;
588 const guint y1 = sink->display_rect.y;
589 const guint rh = sink->display_rect.height / 5;
590 GLfloat ry = 1.0f - (GLfloat)rh / (GLfloat)sink->display_rect.height;
594 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
595 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y1);
596 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y1);
598 glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
599 glTexCoord2f(1.0f, ry); glVertex2i(x2, y1 + rh);
600 glTexCoord2f(0.0f, ry); glVertex2i(x1, y1 + rh);
606 gst_vaapisink_show_frame_glx(
608 GstVaapiSurface *surface,
612 GstVaapiWindowGLX * const window = GST_VAAPI_WINDOW_GLX(sink->window);
616 gst_vaapi_window_glx_make_current(window);
617 if (!sink->texture) {
618 sink->texture = gst_vaapi_texture_new(
626 goto error_create_texture;
628 if (!gst_vaapi_texture_put_surface(sink->texture, surface, flags))
629 goto error_transfer_surface;
631 target = gst_vaapi_texture_get_target(sink->texture);
632 texture = gst_vaapi_texture_get_id(sink->texture);
633 if (target != GL_TEXTURE_2D || !texture)
636 if (sink->use_reflection)
637 render_background(sink);
640 glBindTexture(target, texture);
642 if (sink->use_reflection) {
644 glRotatef(20.0f, 0.0f, 1.0f, 0.0f);
645 glTranslatef(50.0f, 0.0f, 0.0f);
648 if (sink->use_reflection) {
650 glTranslatef(0.0, (GLfloat)sink->display_rect.height + 5.0f, 0.0f);
651 render_reflection(sink);
656 glBindTexture(target, 0);
658 gst_vaapi_window_glx_swap_buffers(window);
662 error_create_texture:
664 GST_DEBUG("could not create VA/GLX texture");
667 error_transfer_surface:
669 GST_DEBUG("could not transfer VA surface to texture");
675 static inline gboolean
676 gst_vaapisink_show_frame_x11(
678 GstVaapiSurface *surface,
682 if (!gst_vaapi_window_put_surface(sink->window, surface,
683 NULL, &sink->display_rect, flags)) {
684 GST_DEBUG("could not render VA surface");
691 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *buffer)
693 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
694 GstVaapiVideoBuffer * const vbuffer = GST_VAAPI_VIDEO_BUFFER(buffer);
695 GstVaapiSurface *surface;
700 return GST_FLOW_UNEXPECTED;
702 surface = gst_vaapi_video_buffer_get_surface(vbuffer);
704 return GST_FLOW_UNEXPECTED;
706 GST_DEBUG("render surface %" GST_VAAPI_ID_FORMAT,
707 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surface)));
709 flags = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
711 #if USE_VAAPISINK_GLX
713 success = gst_vaapisink_show_frame_glx(sink, surface, flags);
716 success = gst_vaapisink_show_frame_x11(sink, surface, flags);
717 return success ? GST_FLOW_OK : GST_FLOW_UNEXPECTED;
721 gst_vaapisink_finalize(GObject *object)
723 gst_vaapisink_destroy(GST_VAAPISINK(object));
725 G_OBJECT_CLASS(parent_class)->finalize(object);
729 gst_vaapisink_set_property(
736 GstVaapiSink * const sink = GST_VAAPISINK(object);
740 sink->use_glx = g_value_get_boolean(value);
743 g_free(sink->display_name);
744 sink->display_name = g_strdup(g_value_get_string(value));
746 case PROP_FULLSCREEN:
747 sink->fullscreen = g_value_get_boolean(value);
749 case PROP_SYNCHRONOUS:
750 sink->synchronous = g_value_get_boolean(value);
752 case PROP_USE_REFLECTION:
753 sink->use_reflection = g_value_get_boolean(value);
756 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
762 gst_vaapisink_get_property(
769 GstVaapiSink * const sink = GST_VAAPISINK(object);
773 g_value_set_boolean(value, sink->use_glx);
776 g_value_set_string(value, sink->display_name);
778 case PROP_FULLSCREEN:
779 g_value_set_boolean(value, sink->fullscreen);
781 case PROP_SYNCHRONOUS:
782 g_value_set_boolean(value, sink->synchronous);
784 case PROP_USE_REFLECTION:
785 g_value_set_boolean(value, sink->use_reflection);
788 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
794 gst_vaapisink_base_init(gpointer klass)
796 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
798 gst_element_class_set_details(element_class, &gst_vaapisink_details);
800 gst_element_class_add_pad_template(
802 gst_static_pad_template_get(&gst_vaapisink_sink_factory)
807 gst_vaapisink_class_init(GstVaapiSinkClass *klass)
809 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
810 GstBaseSinkClass * const basesink_class = GST_BASE_SINK_CLASS(klass);
812 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapisink,
813 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
815 object_class->finalize = gst_vaapisink_finalize;
816 object_class->set_property = gst_vaapisink_set_property;
817 object_class->get_property = gst_vaapisink_get_property;
819 basesink_class->start = gst_vaapisink_start;
820 basesink_class->stop = gst_vaapisink_stop;
821 basesink_class->set_caps = gst_vaapisink_set_caps;
822 basesink_class->buffer_alloc = gst_vaapisink_buffer_alloc;
823 basesink_class->preroll = gst_vaapisink_show_frame;
824 basesink_class->render = gst_vaapisink_show_frame;
826 #if USE_VAAPISINK_GLX
827 g_object_class_install_property
830 g_param_spec_boolean("use-glx",
832 "Enables GLX rendering",
836 g_object_class_install_property
839 g_param_spec_boolean("use-reflection",
841 "Enables OpenGL reflection effect",
846 g_object_class_install_property
849 g_param_spec_string("display",
855 g_object_class_install_property
858 g_param_spec_boolean("fullscreen",
860 "Requests window in fullscreen state",
865 * GstVaapiSink:synchronous:
867 * When enabled, runs the X display in synchronous mode. Note that
868 * this is used only for debugging.
870 g_object_class_install_property
873 g_param_spec_boolean("synchronous",
875 "Toggles X display synchronous mode",
881 gst_vaapisink_init(GstVaapiSink *sink, GstVaapiSinkClass *klass)
883 sink->display_name = NULL;
884 sink->display = NULL;
886 sink->window_width = 0;
887 sink->window_height = 0;
888 sink->texture = NULL;
889 sink->video_width = 0;
890 sink->video_height = 0;
891 sink->video_par_n = 1;
892 sink->video_par_d = 1;
893 sink->foreign_window = FALSE;
894 sink->fullscreen = FALSE;
895 sink->synchronous = FALSE;
896 sink->use_glx = USE_VAAPISINK_GLX;
897 sink->use_reflection = FALSE;
901 gst_vaapisink_get_display(GstVaapiSink *sink)
903 if (!gst_vaapisink_ensure_display(sink))
905 return sink->display;