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, XID xid);
129 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *buffer);
132 gst_vaapisink_xoverlay_set_xid(GstXOverlay *overlay, XID xid)
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, xid);
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_xwindow_id = gst_vaapisink_xoverlay_set_xid;
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, XID xid)
360 unsigned int width, height, border_width, depth;
363 if (!gst_vaapisink_ensure_display(sink))
366 gst_vaapi_display_lock(sink->display);
368 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
371 &x, &y, &width, &height, &border_width, &depth
373 gst_vaapi_display_unlock(sink->display);
375 if ((width != sink->window_width || height != sink->window_height) &&
376 !configure_notify_event_pending(sink, xid, width, height)) {
377 if (!gst_vaapisink_ensure_render_rect(sink, width, height))
379 sink->window_width = width;
380 sink->window_height = height;
384 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window)) == xid)
388 g_object_unref(sink->window);
392 #if USE_VAAPISINK_GLX
394 sink->window = gst_vaapi_window_glx_new_with_xid(sink->display, xid);
397 sink->window = gst_vaapi_window_x11_new_with_xid(sink->display, xid);
398 return sink->window != NULL;
402 gst_vaapisink_start(GstBaseSink *base_sink)
404 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
406 if (!gst_vaapisink_ensure_display(sink))
412 gst_vaapisink_stop(GstBaseSink *base_sink)
414 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
417 g_object_unref(sink->window);
422 g_object_unref(sink->display);
423 sink->display = NULL;
429 gst_vaapisink_set_caps(GstBaseSink *base_sink, GstCaps *caps)
431 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
432 GstStructure * const structure = gst_caps_get_structure(caps, 0);
433 guint display_width, display_height, win_width, win_height;
434 gint video_width, video_height, video_par_n = 1, video_par_d = 1;
438 if (!gst_structure_get_int(structure, "width", &video_width))
440 if (!gst_structure_get_int(structure, "height", &video_height))
442 sink->video_width = video_width;
443 sink->video_height = video_height;
445 gst_video_parse_caps_pixel_aspect_ratio(caps, &video_par_n, &video_par_d);
446 sink->video_par_n = video_par_n;
447 sink->video_par_d = video_par_d;
448 GST_DEBUG("video pixel-aspect-ratio %d/%d", video_par_n, video_par_d);
450 gst_vaapi_display_get_size(sink->display, &display_width, &display_height);
451 if (!gst_vaapisink_ensure_render_rect(sink, display_width, display_height))
454 if (sink->fullscreen) {
455 win_width = display_width;
456 win_height = display_height;
459 win_width = sink->display_rect.width;
460 win_height = sink->display_rect.height;
464 gst_vaapi_window_set_size(sink->window, win_width, win_height);
466 gst_vaapi_display_lock(sink->display);
467 gst_x_overlay_prepare_xwindow_id(GST_X_OVERLAY(sink));
468 gst_vaapi_display_unlock(sink->display);
471 if (!gst_vaapisink_ensure_window(sink, win_width, win_height))
473 gst_vaapi_window_set_fullscreen(sink->window, sink->fullscreen);
474 gst_vaapi_window_show(sink->window);
476 sink->window_width = win_width;
477 sink->window_height = win_height;
482 gst_vaapisink_buffer_alloc(
483 GstBaseSink *base_sink,
487 GstBuffer **pout_buffer
490 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
491 GstStructure *structure;
494 structure = gst_caps_get_structure(caps, 0);
495 if (!gst_structure_has_name(structure, "video/x-vaapi-surface"))
496 goto error_invalid_caps;
498 buffer = gst_vaapi_video_buffer_new(sink->display);
500 goto error_create_buffer;
502 gst_buffer_set_caps(buffer, caps);
503 *pout_buffer = buffer;
509 GST_DEBUG("failed to validate input caps");
510 return GST_FLOW_UNEXPECTED;
514 GST_DEBUG("failed to create video buffer");
515 return GST_FLOW_UNEXPECTED;
519 #if USE_VAAPISINK_GLX
521 render_background(GstVaapiSink *sink)
523 /* Original code from Mirco Muller (MacSlow):
524 <http://cgit.freedesktop.org/~macslow/gl-gst-player/> */
525 GLfloat fStartX = 0.0f;
526 GLfloat fStartY = 0.0f;
527 GLfloat fWidth = (GLfloat)sink->window_width;
528 GLfloat fHeight = (GLfloat)sink->window_height;
530 glClear(GL_COLOR_BUFFER_BIT);
533 /* top third, darker grey to white */
534 glColor3f(0.85f, 0.85f, 0.85f);
535 glVertex3f(fStartX, fStartY, 0.0f);
536 glColor3f(0.85f, 0.85f, 0.85f);
537 glVertex3f(fStartX + fWidth, fStartY, 0.0f);
538 glColor3f(1.0f, 1.0f, 1.0f);
539 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
540 glColor3f(1.0f, 1.0f, 1.0f);
541 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
543 /* middle third, just plain white */
544 glColor3f(1.0f, 1.0f, 1.0f);
545 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
546 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
547 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
548 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
550 /* bottom third, white to lighter grey */
551 glColor3f(1.0f, 1.0f, 1.0f);
552 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
553 glColor3f(1.0f, 1.0f, 1.0f);
554 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
555 glColor3f(0.62f, 0.66f, 0.69f);
556 glVertex3f(fStartX + fWidth, fStartY + fHeight, 0.0f);
557 glColor3f(0.62f, 0.66f, 0.69f);
558 glVertex3f(fStartX, fStartY + fHeight, 0.0f);
564 render_frame(GstVaapiSink *sink)
566 const guint x1 = sink->display_rect.x;
567 const guint x2 = sink->display_rect.x + sink->display_rect.width;
568 const guint y1 = sink->display_rect.y;
569 const guint y2 = sink->display_rect.y + sink->display_rect.height;
571 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
574 glTexCoord2f(0.0f, 0.0f); glVertex2i(x1, y1);
575 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y2);
576 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y2);
577 glTexCoord2f(1.0f, 0.0f); glVertex2i(x2, y1);
583 render_reflection(GstVaapiSink *sink)
585 const guint x1 = sink->display_rect.x;
586 const guint x2 = sink->display_rect.x + sink->display_rect.width;
587 const guint y1 = sink->display_rect.y;
588 const guint rh = sink->display_rect.height / 5;
589 GLfloat ry = 1.0f - (GLfloat)rh / (GLfloat)sink->display_rect.height;
593 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
594 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y1);
595 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y1);
597 glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
598 glTexCoord2f(1.0f, ry); glVertex2i(x2, y1 + rh);
599 glTexCoord2f(0.0f, ry); glVertex2i(x1, y1 + rh);
605 gst_vaapisink_show_frame_glx(
607 GstVaapiSurface *surface,
611 GstVaapiWindowGLX * const window = GST_VAAPI_WINDOW_GLX(sink->window);
615 gst_vaapi_window_glx_make_current(window);
616 if (!sink->texture) {
617 sink->texture = gst_vaapi_texture_new(
625 goto error_create_texture;
627 if (!gst_vaapi_texture_put_surface(sink->texture, surface, flags))
628 goto error_transfer_surface;
630 target = gst_vaapi_texture_get_target(sink->texture);
631 texture = gst_vaapi_texture_get_id(sink->texture);
632 if (target != GL_TEXTURE_2D || !texture)
635 if (sink->use_reflection)
636 render_background(sink);
639 glBindTexture(target, texture);
641 if (sink->use_reflection) {
643 glRotatef(20.0f, 0.0f, 1.0f, 0.0f);
644 glTranslatef(50.0f, 0.0f, 0.0f);
647 if (sink->use_reflection) {
649 glTranslatef(0.0, (GLfloat)sink->display_rect.height + 5.0f, 0.0f);
650 render_reflection(sink);
655 glBindTexture(target, 0);
657 gst_vaapi_window_glx_swap_buffers(window);
661 error_create_texture:
663 GST_DEBUG("could not create VA/GLX texture");
666 error_transfer_surface:
668 GST_DEBUG("could not transfer VA surface to texture");
674 static inline gboolean
675 gst_vaapisink_show_frame_x11(
677 GstVaapiSurface *surface,
681 if (!gst_vaapi_window_put_surface(sink->window, surface,
682 NULL, &sink->display_rect, flags)) {
683 GST_DEBUG("could not render VA surface");
690 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *buffer)
692 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
693 GstVaapiVideoBuffer * const vbuffer = GST_VAAPI_VIDEO_BUFFER(buffer);
694 GstVaapiSurface *surface;
699 return GST_FLOW_UNEXPECTED;
701 surface = gst_vaapi_video_buffer_get_surface(vbuffer);
703 return GST_FLOW_UNEXPECTED;
705 GST_DEBUG("render surface %" GST_VAAPI_ID_FORMAT,
706 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surface)));
708 flags = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
710 #if USE_VAAPISINK_GLX
712 success = gst_vaapisink_show_frame_glx(sink, surface, flags);
715 success = gst_vaapisink_show_frame_x11(sink, surface, flags);
716 return success ? GST_FLOW_OK : GST_FLOW_UNEXPECTED;
720 gst_vaapisink_finalize(GObject *object)
722 gst_vaapisink_destroy(GST_VAAPISINK(object));
724 G_OBJECT_CLASS(parent_class)->finalize(object);
728 gst_vaapisink_set_property(
735 GstVaapiSink * const sink = GST_VAAPISINK(object);
739 sink->use_glx = g_value_get_boolean(value);
742 g_free(sink->display_name);
743 sink->display_name = g_strdup(g_value_get_string(value));
745 case PROP_FULLSCREEN:
746 sink->fullscreen = g_value_get_boolean(value);
748 case PROP_SYNCHRONOUS:
749 sink->synchronous = g_value_get_boolean(value);
751 case PROP_USE_REFLECTION:
752 sink->use_reflection = g_value_get_boolean(value);
755 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
761 gst_vaapisink_get_property(
768 GstVaapiSink * const sink = GST_VAAPISINK(object);
772 g_value_set_boolean(value, sink->use_glx);
775 g_value_set_string(value, sink->display_name);
777 case PROP_FULLSCREEN:
778 g_value_set_boolean(value, sink->fullscreen);
780 case PROP_SYNCHRONOUS:
781 g_value_set_boolean(value, sink->synchronous);
783 case PROP_USE_REFLECTION:
784 g_value_set_boolean(value, sink->use_reflection);
787 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
793 gst_vaapisink_base_init(gpointer klass)
795 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
797 gst_element_class_set_details(element_class, &gst_vaapisink_details);
799 gst_element_class_add_pad_template(
801 gst_static_pad_template_get(&gst_vaapisink_sink_factory)
806 gst_vaapisink_class_init(GstVaapiSinkClass *klass)
808 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
809 GstBaseSinkClass * const basesink_class = GST_BASE_SINK_CLASS(klass);
811 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapisink,
812 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
814 object_class->finalize = gst_vaapisink_finalize;
815 object_class->set_property = gst_vaapisink_set_property;
816 object_class->get_property = gst_vaapisink_get_property;
818 basesink_class->start = gst_vaapisink_start;
819 basesink_class->stop = gst_vaapisink_stop;
820 basesink_class->set_caps = gst_vaapisink_set_caps;
821 basesink_class->buffer_alloc = gst_vaapisink_buffer_alloc;
822 basesink_class->preroll = gst_vaapisink_show_frame;
823 basesink_class->render = gst_vaapisink_show_frame;
825 #if USE_VAAPISINK_GLX
826 g_object_class_install_property
829 g_param_spec_boolean("use-glx",
831 "Enables GLX rendering",
835 g_object_class_install_property
838 g_param_spec_boolean("use-reflection",
840 "Enables OpenGL reflection effect",
845 g_object_class_install_property
848 g_param_spec_string("display",
854 g_object_class_install_property
857 g_param_spec_boolean("fullscreen",
859 "Requests window in fullscreen state",
864 * GstVaapiSink:synchronous:
866 * When enabled, runs the X display in synchronous mode. Note that
867 * this is used only for debugging.
869 g_object_class_install_property
872 g_param_spec_boolean("synchronous",
874 "Toggles X display synchronous mode",
880 gst_vaapisink_init(GstVaapiSink *sink, GstVaapiSinkClass *klass)
882 sink->display_name = NULL;
883 sink->display = NULL;
885 sink->window_width = 0;
886 sink->window_height = 0;
887 sink->texture = NULL;
888 sink->video_width = 0;
889 sink->video_height = 0;
890 sink->video_par_n = 1;
891 sink->video_par_d = 1;
892 sink->foreign_window = FALSE;
893 sink->fullscreen = FALSE;
894 sink->synchronous = FALSE;
895 sink->use_glx = USE_VAAPISINK_GLX;
896 sink->use_reflection = FALSE;
900 gst_vaapisink_get_display(GstVaapiSink *sink)
902 if (!gst_vaapisink_ensure_display(sink))
904 return sink->display;