Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gstreamer.git] / sys / xvimage / xvimagesink.c
index 8a7913b..a54e18f 100644 (file)
@@ -94,7 +94,7 @@
  * position. This also handles borders correctly, limiting coordinates to the
  * image area
  * |[
- * gst-launch -v videotestsrc ! video/x-raw-yuv, pixel-aspect-ratio=(fraction)4/3 ! xvimagesink
+ * gst-launch -v videotestsrc ! video/x-raw, pixel-aspect-ratio=(fraction)4/3 ! xvimagesink
  * ]| This is faking a 4/3 pixel aspect ratio caps on video frames produced by
  * videotestsrc, in most cases the pixel aspect ratio of the display will be
  * 1/1. This means that XvImageSink will have to do the scaling to convert
 
 /* Our interfaces */
 #include <gst/interfaces/navigation.h>
-#include <gst/interfaces/xoverlay.h>
-#include <gst/interfaces/colorbalance.h>
 #include <gst/interfaces/propertyprobe.h>
+#include <gst/video/videooverlay.h>
+#include <gst/video/colorbalance.h>
 /* Helper functions */
-#include <gst/video/video.h>
+#include <gst/video/gstvideometa.h>
 
 /* Object header */
 #include "xvimagesink.h"
 
 #include "gst/glib-compat-private.h"
 
-GST_DEBUG_CATEGORY_STATIC (gst_debug_xvimagesink);
+GST_DEBUG_CATEGORY_EXTERN (gst_debug_xvimagesink);
+GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
 #define GST_CAT_DEFAULT gst_debug_xvimagesink
-GST_DEBUG_CATEGORY_STATIC (GST_CAT_PERFORMANCE);
 
 typedef struct
 {
@@ -146,27 +146,17 @@ MotifWmHints, MwmHints;
 #define MWM_HINTS_DECORATIONS   (1L << 1)
 
 static void gst_xvimagesink_reset (GstXvImageSink * xvimagesink);
-
-static GstBufferClass *xvimage_buffer_parent_class = NULL;
-static void gst_xvimage_buffer_finalize (GstXvImageBuffer * xvimage);
-
 static void gst_xvimagesink_xwindow_update_geometry (GstXvImageSink *
     xvimagesink);
-static gint gst_xvimagesink_get_format_from_caps (GstXvImageSink * xvimagesink,
-    GstCaps * caps);
-static void gst_xvimagesink_expose (GstXOverlay * overlay);
+static void gst_xvimagesink_expose (GstVideoOverlay * overlay);
 
 /* Default template - initiated with class struct to allow gst-register to work
    without X running */
 static GstStaticPadTemplate gst_xvimagesink_sink_template_factory =
-    GST_STATIC_PAD_TEMPLATE ("sink",
+GST_STATIC_PAD_TEMPLATE ("sink",
     GST_PAD_SINK,
     GST_PAD_ALWAYS,
-    GST_STATIC_CAPS ("video/x-raw-rgb, "
-        "framerate = (fraction) [ 0, MAX ], "
-        "width = (int) [ 1, MAX ], "
-        "height = (int) [ 1, MAX ]; "
-        "video/x-raw-yuv, "
+    GST_STATIC_CAPS ("video/x-raw, "
         "framerate = (fraction) [ 0, MAX ], "
         "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ]")
     );
@@ -194,548 +184,43 @@ enum
   PROP_WINDOW_HEIGHT
 };
 
-static void gst_xvimagesink_init_interfaces (GType type);
-
-GST_BOILERPLATE_FULL (GstXvImageSink, gst_xvimagesink, GstVideoSink,
-    GST_TYPE_VIDEO_SINK, gst_xvimagesink_init_interfaces);
-
-
 /* ============================================================= */
 /*                                                               */
-/*                       Private Methods                         */
+/*                       Public Methods                          */
 /*                                                               */
 /* ============================================================= */
 
-/* xvimage buffers */
-
-#define GST_TYPE_XVIMAGE_BUFFER (gst_xvimage_buffer_get_type())
-
-#define GST_IS_XVIMAGE_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_XVIMAGE_BUFFER))
-#define GST_XVIMAGE_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_XVIMAGE_BUFFER, GstXvImageBuffer))
-#define GST_XVIMAGE_BUFFER_CAST(obj) ((GstXvImageBuffer *)(obj))
-
-/* This function destroys a GstXvImage handling XShm availability */
-static void
-gst_xvimage_buffer_destroy (GstXvImageBuffer * xvimage)
-{
-  GstXvImageSink *xvimagesink;
-
-  GST_DEBUG_OBJECT (xvimage, "Destroying buffer");
-
-  xvimagesink = xvimage->xvimagesink;
-  if (G_UNLIKELY (xvimagesink == NULL))
-    goto no_sink;
-
-  g_return_if_fail (GST_IS_XVIMAGESINK (xvimagesink));
-
-  GST_OBJECT_LOCK (xvimagesink);
-
-  /* If the destroyed image is the current one we destroy our reference too */
-  if (xvimagesink->cur_image == xvimage)
-    xvimagesink->cur_image = NULL;
-
-  /* We might have some buffers destroyed after changing state to NULL */
-  if (xvimagesink->xcontext == NULL) {
-    GST_DEBUG_OBJECT (xvimagesink, "Destroying XvImage after Xcontext");
-#ifdef HAVE_XSHM
-    /* Need to free the shared memory segment even if the x context
-     * was already cleaned up */
-    if (xvimage->SHMInfo.shmaddr != ((void *) -1)) {
-      shmdt (xvimage->SHMInfo.shmaddr);
-    }
-#endif
-    goto beach;
-  }
-
-  g_mutex_lock (xvimagesink->x_lock);
-
-#ifdef HAVE_XSHM
-  if (xvimagesink->xcontext->use_xshm) {
-    if (xvimage->SHMInfo.shmaddr != ((void *) -1)) {
-      GST_DEBUG_OBJECT (xvimagesink, "XServer ShmDetaching from 0x%x id 0x%lx",
-          xvimage->SHMInfo.shmid, xvimage->SHMInfo.shmseg);
-      XShmDetach (xvimagesink->xcontext->disp, &xvimage->SHMInfo);
-      XSync (xvimagesink->xcontext->disp, FALSE);
-
-      shmdt (xvimage->SHMInfo.shmaddr);
-    }
-    if (xvimage->xvimage)
-      XFree (xvimage->xvimage);
-  } else
-#endif /* HAVE_XSHM */
-  {
-    if (xvimage->xvimage) {
-      if (xvimage->xvimage->data) {
-        g_free (xvimage->xvimage->data);
-      }
-      XFree (xvimage->xvimage);
-    }
-  }
-
-  XSync (xvimagesink->xcontext->disp, FALSE);
-
-  g_mutex_unlock (xvimagesink->x_lock);
-
-beach:
-  GST_OBJECT_UNLOCK (xvimagesink);
-  xvimage->xvimagesink = NULL;
-  gst_object_unref (xvimagesink);
-
-  GST_MINI_OBJECT_CLASS (xvimage_buffer_parent_class)->finalize (GST_MINI_OBJECT
-      (xvimage));
-
-  return;
-
-no_sink:
-  {
-    GST_WARNING ("no sink found");
-    return;
-  }
-}
-
-static void
-gst_xvimage_buffer_finalize (GstXvImageBuffer * xvimage)
-{
-  GstXvImageSink *xvimagesink;
-  gboolean running;
-
-  xvimagesink = xvimage->xvimagesink;
-  if (G_UNLIKELY (xvimagesink == NULL))
-    goto no_sink;
-
-  g_return_if_fail (GST_IS_XVIMAGESINK (xvimagesink));
-
-  GST_OBJECT_LOCK (xvimagesink);
-  running = xvimagesink->running;
-  GST_OBJECT_UNLOCK (xvimagesink);
-
-  /* If our geometry changed we can't reuse that image. */
-  if (running == FALSE) {
-    GST_LOG_OBJECT (xvimage, "destroy image as sink is shutting down");
-    gst_xvimage_buffer_destroy (xvimage);
-  } else if ((xvimage->width != xvimagesink->video_width) ||
-      (xvimage->height != xvimagesink->video_height)) {
-    GST_LOG_OBJECT (xvimage,
-        "destroy image as its size changed %dx%d vs current %dx%d",
-        xvimage->width, xvimage->height,
-        xvimagesink->video_width, xvimagesink->video_height);
-    gst_xvimage_buffer_destroy (xvimage);
-  } else {
-    /* In that case we can reuse the image and add it to our image pool. */
-    GST_LOG_OBJECT (xvimage, "recycling image in pool");
-    /* need to increment the refcount again to recycle */
-    gst_buffer_ref (GST_BUFFER_CAST (xvimage));
-    g_mutex_lock (xvimagesink->pool_lock);
-    xvimagesink->image_pool = g_slist_prepend (xvimagesink->image_pool,
-        xvimage);
-    g_mutex_unlock (xvimagesink->pool_lock);
-  }
-  return;
-
-no_sink:
-  {
-    GST_WARNING ("no sink found");
-    return;
-  }
-}
-
-static void
-gst_xvimage_buffer_free (GstXvImageBuffer * xvimage)
-{
-  /* make sure it is not recycled */
-  xvimage->width = -1;
-  xvimage->height = -1;
-  gst_buffer_unref (GST_BUFFER (xvimage));
-}
-
-static void
-gst_xvimage_buffer_init (GstXvImageBuffer * xvimage, gpointer g_class)
-{
-#ifdef HAVE_XSHM
-  xvimage->SHMInfo.shmaddr = ((void *) -1);
-  xvimage->SHMInfo.shmid = -1;
-#endif
-}
-
+/* =========================================== */
+/*                                             */
+/*          Object typing & Creation           */
+/*                                             */
+/* =========================================== */
+static void gst_xvimagesink_navigation_init (GstNavigationInterface * iface);
+static void gst_xvimagesink_video_overlay_init (GstVideoOverlayInterface *
+    iface);
+static void gst_xvimagesink_colorbalance_init (GstColorBalanceInterface *
+    iface);
 static void
-gst_xvimage_buffer_class_init (gpointer g_class, gpointer class_data)
-{
-  GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
-
-  xvimage_buffer_parent_class = g_type_class_peek_parent (g_class);
-
-  mini_object_class->finalize = (GstMiniObjectFinalizeFunction)
-      gst_xvimage_buffer_finalize;
-}
-
-static GType
-gst_xvimage_buffer_get_type (void)
-{
-  static GType _gst_xvimage_buffer_type;
-
-  if (G_UNLIKELY (_gst_xvimage_buffer_type == 0)) {
-    static const GTypeInfo xvimage_buffer_info = {
-      sizeof (GstBufferClass),
-      NULL,
-      NULL,
-      gst_xvimage_buffer_class_init,
-      NULL,
-      NULL,
-      sizeof (GstXvImageBuffer),
-      0,
-      (GInstanceInitFunc) gst_xvimage_buffer_init,
-      NULL
-    };
-    _gst_xvimage_buffer_type = g_type_register_static (GST_TYPE_BUFFER,
-        "GstXvImageBuffer", &xvimage_buffer_info, 0);
-  }
-  return _gst_xvimage_buffer_type;
-}
-
-/* X11 stuff */
-
-static gboolean error_caught = FALSE;
-
-static int
-gst_xvimagesink_handle_xerror (Display * display, XErrorEvent * xevent)
-{
-  char error_msg[1024];
-
-  XGetErrorText (display, xevent->error_code, error_msg, 1024);
-  GST_DEBUG ("xvimagesink triggered an XError. error: %s", error_msg);
-  error_caught = TRUE;
-  return 0;
-}
-
-#ifdef HAVE_XSHM
-/* This function checks that it is actually really possible to create an image
-   using XShm */
-static gboolean
-gst_xvimagesink_check_xshm_calls (GstXContext * xcontext)
-{
-  XvImage *xvimage;
-  XShmSegmentInfo SHMInfo;
-  gint size;
-  int (*handler) (Display *, XErrorEvent *);
-  gboolean result = FALSE;
-  gboolean did_attach = FALSE;
-
-  g_return_val_if_fail (xcontext != NULL, FALSE);
-
-  /* Sync to ensure any older errors are already processed */
-  XSync (xcontext->disp, FALSE);
-
-  /* Set defaults so we don't free these later unnecessarily */
-  SHMInfo.shmaddr = ((void *) -1);
-  SHMInfo.shmid = -1;
-
-  /* Setting an error handler to catch failure */
-  error_caught = FALSE;
-  handler = XSetErrorHandler (gst_xvimagesink_handle_xerror);
-
-  /* Trying to create a 1x1 picture */
-  GST_DEBUG ("XvShmCreateImage of 1x1");
-  xvimage = XvShmCreateImage (xcontext->disp, xcontext->xv_port_id,
-      xcontext->im_format, NULL, 1, 1, &SHMInfo);
-
-  /* Might cause an error, sync to ensure it is noticed */
-  XSync (xcontext->disp, FALSE);
-  if (!xvimage || error_caught) {
-    GST_WARNING ("could not XvShmCreateImage a 1x1 image");
-    goto beach;
-  }
-  size = xvimage->data_size;
-
-  SHMInfo.shmid = shmget (IPC_PRIVATE, size, IPC_CREAT | 0777);
-  if (SHMInfo.shmid == -1) {
-    GST_WARNING ("could not get shared memory of %d bytes", size);
-    goto beach;
-  }
-
-  SHMInfo.shmaddr = shmat (SHMInfo.shmid, NULL, 0);
-  if (SHMInfo.shmaddr == ((void *) -1)) {
-    GST_WARNING ("Failed to shmat: %s", g_strerror (errno));
-    /* Clean up the shared memory segment */
-    shmctl (SHMInfo.shmid, IPC_RMID, NULL);
-    goto beach;
-  }
-
-  xvimage->data = SHMInfo.shmaddr;
-  SHMInfo.readOnly = FALSE;
-
-  if (XShmAttach (xcontext->disp, &SHMInfo) == 0) {
-    GST_WARNING ("Failed to XShmAttach");
-    /* Clean up the shared memory segment */
-    shmctl (SHMInfo.shmid, IPC_RMID, NULL);
-    goto beach;
-  }
-
-  /* Sync to ensure we see any errors we caused */
-  XSync (xcontext->disp, FALSE);
-
-  /* Delete the shared memory segment as soon as everyone is attached.
-   * This way, it will be deleted as soon as we detach later, and not
-   * leaked if we crash. */
-  shmctl (SHMInfo.shmid, IPC_RMID, NULL);
-
-  if (!error_caught) {
-    GST_DEBUG ("XServer ShmAttached to 0x%x, id 0x%lx", SHMInfo.shmid,
-        SHMInfo.shmseg);
-
-    did_attach = TRUE;
-    /* store whether we succeeded in result */
-    result = TRUE;
-  } else {
-    GST_WARNING ("MIT-SHM extension check failed at XShmAttach. "
-        "Not using shared memory.");
-  }
-
-beach:
-  /* Sync to ensure we swallow any errors we caused and reset error_caught */
-  XSync (xcontext->disp, FALSE);
-
-  error_caught = FALSE;
-  XSetErrorHandler (handler);
-
-  if (did_attach) {
-    GST_DEBUG ("XServer ShmDetaching from 0x%x id 0x%lx",
-        SHMInfo.shmid, SHMInfo.shmseg);
-    XShmDetach (xcontext->disp, &SHMInfo);
-    XSync (xcontext->disp, FALSE);
-  }
-  if (SHMInfo.shmaddr != ((void *) -1))
-    shmdt (SHMInfo.shmaddr);
-  if (xvimage)
-    XFree (xvimage);
-  return result;
-}
-#endif /* HAVE_XSHM */
-
-/* This function handles GstXvImage creation depending on XShm availability */
-static GstXvImageBuffer *
-gst_xvimagesink_xvimage_new (GstXvImageSink * xvimagesink, GstCaps * caps)
-{
-  GstXvImageBuffer *xvimage = NULL;
-  GstStructure *structure = NULL;
-  gboolean succeeded = FALSE;
-  int (*handler) (Display *, XErrorEvent *);
-
-  g_return_val_if_fail (GST_IS_XVIMAGESINK (xvimagesink), NULL);
-
-  if (caps == NULL)
-    return NULL;
-
-  xvimage = (GstXvImageBuffer *) gst_mini_object_new (GST_TYPE_XVIMAGE_BUFFER);
-  GST_DEBUG_OBJECT (xvimage, "Creating new XvImageBuffer");
-
-  structure = gst_caps_get_structure (caps, 0);
-
-  if (!gst_structure_get_int (structure, "width", &xvimage->width) ||
-      !gst_structure_get_int (structure, "height", &xvimage->height)) {
-    GST_WARNING ("failed getting geometry from caps %" GST_PTR_FORMAT, caps);
-  }
-
-  GST_LOG_OBJECT (xvimagesink, "creating %dx%d", xvimage->width,
-      xvimage->height);
-
-  xvimage->im_format = gst_xvimagesink_get_format_from_caps (xvimagesink, caps);
-  if (xvimage->im_format == -1) {
-    GST_WARNING_OBJECT (xvimagesink, "failed to get format from caps %"
-        GST_PTR_FORMAT, caps);
-    GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
-        ("Failed to create output image buffer of %dx%d pixels",
-            xvimage->width, xvimage->height), ("Invalid input caps"));
-    goto beach_unlocked;
-  }
-  xvimage->xvimagesink = gst_object_ref (xvimagesink);
-
-  g_mutex_lock (xvimagesink->x_lock);
-
-  /* Setting an error handler to catch failure */
-  error_caught = FALSE;
-  handler = XSetErrorHandler (gst_xvimagesink_handle_xerror);
-
-#ifdef HAVE_XSHM
-  if (xvimagesink->xcontext->use_xshm) {
-    int expected_size;
-
-    xvimage->xvimage = XvShmCreateImage (xvimagesink->xcontext->disp,
-        xvimagesink->xcontext->xv_port_id,
-        xvimage->im_format, NULL,
-        xvimage->width, xvimage->height, &xvimage->SHMInfo);
-    if (!xvimage->xvimage || error_caught) {
-      g_mutex_unlock (xvimagesink->x_lock);
-
-      /* Reset error flag */
-      error_caught = FALSE;
-
-      /* Push a warning */
-      GST_ELEMENT_WARNING (xvimagesink, RESOURCE, WRITE,
-          ("Failed to create output image buffer of %dx%d pixels",
-              xvimage->width, xvimage->height),
-          ("could not XvShmCreateImage a %dx%d image",
-              xvimage->width, xvimage->height));
-
-      /* Retry without XShm */
-      xvimagesink->xcontext->use_xshm = FALSE;
-
-      /* Hold X mutex again to try without XShm */
-      g_mutex_lock (xvimagesink->x_lock);
-      goto no_xshm;
-    }
-
-    /* we have to use the returned data_size for our shm size */
-    xvimage->size = xvimage->xvimage->data_size;
-    GST_LOG_OBJECT (xvimagesink, "XShm image size is %" G_GSIZE_FORMAT,
-        xvimage->size);
-
-    /* calculate the expected size.  This is only for sanity checking the
-     * number we get from X. */
-    switch (xvimage->im_format) {
-      case GST_MAKE_FOURCC ('I', '4', '2', '0'):
-      case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
-      {
-        gint pitches[3];
-        gint offsets[3];
-        guint plane;
-
-        offsets[0] = 0;
-        pitches[0] = GST_ROUND_UP_4 (xvimage->width);
-        offsets[1] = offsets[0] + pitches[0] * GST_ROUND_UP_2 (xvimage->height);
-        pitches[1] = GST_ROUND_UP_8 (xvimage->width) / 2;
-        offsets[2] =
-            offsets[1] + pitches[1] * GST_ROUND_UP_2 (xvimage->height) / 2;
-        pitches[2] = GST_ROUND_UP_8 (pitches[0]) / 2;
-
-        expected_size =
-            offsets[2] + pitches[2] * GST_ROUND_UP_2 (xvimage->height) / 2;
-
-        for (plane = 0; plane < xvimage->xvimage->num_planes; plane++) {
-          GST_DEBUG_OBJECT (xvimagesink,
-              "Plane %u has a expected pitch of %d bytes, " "offset of %d",
-              plane, pitches[plane], offsets[plane]);
-        }
-        break;
-      }
-      case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
-      case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
-        expected_size = xvimage->height * GST_ROUND_UP_4 (xvimage->width * 2);
-        break;
-      default:
-        expected_size = 0;
-        break;
-    }
-    if (expected_size != 0 && xvimage->size != expected_size) {
-      GST_WARNING_OBJECT (xvimagesink,
-          "unexpected XShm image size (got %" G_GSIZE_FORMAT ", expected %d)",
-          xvimage->size, expected_size);
-    }
-
-    /* Be verbose about our XvImage stride */
-    {
-      guint plane;
-
-      for (plane = 0; plane < xvimage->xvimage->num_planes; plane++) {
-        GST_DEBUG_OBJECT (xvimagesink, "Plane %u has a pitch of %d bytes, "
-            "offset of %d", plane, xvimage->xvimage->pitches[plane],
-            xvimage->xvimage->offsets[plane]);
-      }
-    }
-
-    xvimage->SHMInfo.shmid = shmget (IPC_PRIVATE, xvimage->size,
-        IPC_CREAT | 0777);
-    if (xvimage->SHMInfo.shmid == -1) {
-      g_mutex_unlock (xvimagesink->x_lock);
-      GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
-          ("Failed to create output image buffer of %dx%d pixels",
-              xvimage->width, xvimage->height),
-          ("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
-              xvimage->size));
-      goto beach_unlocked;
-    }
-
-    xvimage->SHMInfo.shmaddr = shmat (xvimage->SHMInfo.shmid, NULL, 0);
-    if (xvimage->SHMInfo.shmaddr == ((void *) -1)) {
-      g_mutex_unlock (xvimagesink->x_lock);
-      GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
-          ("Failed to create output image buffer of %dx%d pixels",
-              xvimage->width, xvimage->height),
-          ("Failed to shmat: %s", g_strerror (errno)));
-      /* Clean up the shared memory segment */
-      shmctl (xvimage->SHMInfo.shmid, IPC_RMID, NULL);
-      goto beach_unlocked;
-    }
-
-    xvimage->xvimage->data = xvimage->SHMInfo.shmaddr;
-    xvimage->SHMInfo.readOnly = FALSE;
-
-    if (XShmAttach (xvimagesink->xcontext->disp, &xvimage->SHMInfo) == 0) {
-      /* Clean up the shared memory segment */
-      shmctl (xvimage->SHMInfo.shmid, IPC_RMID, NULL);
-
-      g_mutex_unlock (xvimagesink->x_lock);
-      GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
-          ("Failed to create output image buffer of %dx%d pixels",
-              xvimage->width, xvimage->height), ("Failed to XShmAttach"));
-      goto beach_unlocked;
-    }
-
-    XSync (xvimagesink->xcontext->disp, FALSE);
-
-    /* Delete the shared memory segment as soon as we everyone is attached.
-     * This way, it will be deleted as soon as we detach later, and not
-     * leaked if we crash. */
-    shmctl (xvimage->SHMInfo.shmid, IPC_RMID, NULL);
-
-    GST_DEBUG_OBJECT (xvimagesink, "XServer ShmAttached to 0x%x, id 0x%lx",
-        xvimage->SHMInfo.shmid, xvimage->SHMInfo.shmseg);
-  } else
-  no_xshm:
-#endif /* HAVE_XSHM */
-  {
-    xvimage->xvimage = XvCreateImage (xvimagesink->xcontext->disp,
-        xvimagesink->xcontext->xv_port_id,
-        xvimage->im_format, NULL, xvimage->width, xvimage->height);
-    if (!xvimage->xvimage || error_caught) {
-      g_mutex_unlock (xvimagesink->x_lock);
-      /* Reset error handler */
-      error_caught = FALSE;
-      XSetErrorHandler (handler);
-      /* Push an error */
-      GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
-          ("Failed to create outputimage buffer of %dx%d pixels",
-              xvimage->width, xvimage->height),
-          ("could not XvCreateImage a %dx%d image",
-              xvimage->width, xvimage->height));
-      goto beach_unlocked;
-    }
-
-    /* we have to use the returned data_size for our image size */
-    xvimage->size = xvimage->xvimage->data_size;
-    xvimage->xvimage->data = g_malloc (xvimage->size);
-
-    XSync (xvimagesink->xcontext->disp, FALSE);
-  }
-
-  /* Reset error handler */
-  error_caught = FALSE;
-  XSetErrorHandler (handler);
-
-  succeeded = TRUE;
-
-  GST_BUFFER_DATA (xvimage) = (guchar *) xvimage->xvimage->data;
-  GST_BUFFER_SIZE (xvimage) = xvimage->size;
+gst_xvimagesink_property_probe_interface_init (GstPropertyProbeInterface *
+    iface);
+#define gst_xvimagesink_parent_class parent_class
+G_DEFINE_TYPE_WITH_CODE (GstXvImageSink, gst_xvimagesink, GST_TYPE_VIDEO_SINK,
+    G_IMPLEMENT_INTERFACE (GST_TYPE_NAVIGATION,
+        gst_xvimagesink_navigation_init);
+    G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
+        gst_xvimagesink_video_overlay_init);
+    G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
+        gst_xvimagesink_colorbalance_init);
+    G_IMPLEMENT_INTERFACE (GST_TYPE_PROPERTY_PROBE,
+        gst_xvimagesink_property_probe_interface_init));
 
-  g_mutex_unlock (xvimagesink->x_lock);
 
-beach_unlocked:
-  if (!succeeded) {
-    gst_xvimage_buffer_free (xvimage);
-    xvimage = NULL;
-  }
+/* ============================================================= */
+/*                                                               */
+/*                       Private Methods                         */
+/*                                                               */
+/* ============================================================= */
 
-  return xvimage;
-}
 
 /* We are called with the x_lock taken */
 static void
@@ -784,11 +269,13 @@ gst_xvimagesink_xwindow_draw_borders (GstXvImageSink * xvimagesink,
 /* This function puts a GstXvImage on a GstXvImageSink's window. Returns FALSE
  * if no window was available  */
 static gboolean
-gst_xvimagesink_xvimage_put (GstXvImageSink * xvimagesink,
-    GstXvImageBuffer * xvimage)
+gst_xvimagesink_xvimage_put (GstXvImageSink * xvimagesink, GstBuffer * xvimage)
 {
+  GstXvImageMeta *meta;
+  GstVideoCropMeta *crop;
   GstVideoRectangle result;
   gboolean draw_border = FALSE;
+  GstVideoRectangle src, dst;
 
   /* We take the flow_lock. If expose is in there we don't want to run
      concurrently from the data flow thread */
@@ -809,11 +296,10 @@ gst_xvimagesink_xvimage_put (GstXvImageSink * xvimagesink,
   if (xvimage && xvimagesink->cur_image != xvimage) {
     if (xvimagesink->cur_image) {
       GST_LOG_OBJECT (xvimagesink, "unreffing %p", xvimagesink->cur_image);
-      gst_buffer_unref (GST_BUFFER_CAST (xvimagesink->cur_image));
+      gst_buffer_unref (xvimagesink->cur_image);
     }
     GST_LOG_OBJECT (xvimagesink, "reffing %p as our current image", xvimage);
-    xvimagesink->cur_image =
-        GST_XVIMAGE_BUFFER_CAST (gst_buffer_ref (GST_BUFFER_CAST (xvimage)));
+    xvimagesink->cur_image = gst_buffer_ref (xvimage);
   }
 
   /* Expose sends a NULL image, we take the latest frame */
@@ -827,13 +313,23 @@ gst_xvimagesink_xvimage_put (GstXvImageSink * xvimagesink,
     }
   }
 
-  if (xvimagesink->keep_aspect) {
-    GstVideoRectangle src, dst;
+  meta = gst_buffer_get_xvimage_meta (xvimage);
 
-    /* We use the calculated geometry from _setcaps as a source to respect
-       source and screen pixel aspect ratios. */
-    src.w = GST_VIDEO_SINK_WIDTH (xvimagesink);
-    src.h = GST_VIDEO_SINK_HEIGHT (xvimagesink);
+  crop = gst_buffer_get_video_crop_meta (xvimage);
+
+  if (crop) {
+    src.x = crop->x + meta->x;
+    src.y = crop->y + meta->y;
+    src.w = crop->width;
+    src.h = crop->height;
+  } else {
+    src.x = meta->x;
+    src.y = meta->y;
+    src.w = meta->width;
+    src.h = meta->height;
+  }
+
+  if (xvimagesink->keep_aspect) {
     dst.w = xvimagesink->render_rect.w;
     dst.h = xvimagesink->render_rect.h;
 
@@ -851,22 +347,18 @@ gst_xvimagesink_xvimage_put (GstXvImageSink * xvimagesink,
         result);
     xvimagesink->redraw_border = FALSE;
   }
-
-  /* We scale to the window's geometry */
 #ifdef HAVE_XSHM
   if (xvimagesink->xcontext->use_xshm) {
     GST_LOG_OBJECT (xvimagesink,
         "XvShmPutImage with image %dx%d and window %dx%d, from xvimage %"
-        GST_PTR_FORMAT,
-        xvimage->width, xvimage->height,
+        GST_PTR_FORMAT, meta->width, meta->height,
         xvimagesink->render_rect.w, xvimagesink->render_rect.h, xvimage);
 
     XvShmPutImage (xvimagesink->xcontext->disp,
         xvimagesink->xcontext->xv_port_id,
         xvimagesink->xwindow->win,
-        xvimagesink->xwindow->gc, xvimage->xvimage,
-        xvimagesink->disp_x, xvimagesink->disp_y,
-        xvimagesink->disp_width, xvimagesink->disp_height,
+        xvimagesink->xwindow->gc, meta->xvimage,
+        src.x, src.y, src.w, src.h,
         result.x, result.y, result.w, result.h, FALSE);
   } else
 #endif /* HAVE_XSHM */
@@ -874,10 +366,8 @@ gst_xvimagesink_xvimage_put (GstXvImageSink * xvimagesink,
     XvPutImage (xvimagesink->xcontext->disp,
         xvimagesink->xcontext->xv_port_id,
         xvimagesink->xwindow->win,
-        xvimagesink->xwindow->gc, xvimage->xvimage,
-        xvimagesink->disp_x, xvimagesink->disp_y,
-        xvimagesink->disp_width, xvimagesink->disp_height,
-        result.x, result.y, result.w, result.h);
+        xvimagesink->xwindow->gc, meta->xvimage,
+        src.x, src.y, src.w, src.h, result.x, result.y, result.w, result.h);
   }
 
   XSync (xvimagesink->xcontext->disp, FALSE);
@@ -1029,7 +519,8 @@ gst_xvimagesink_xwindow_new (GstXvImageSink * xvimagesink,
 
   gst_xvimagesink_xwindow_decorate (xvimagesink, xwindow);
 
-  gst_x_overlay_got_window_handle (GST_X_OVERLAY (xvimagesink), xwindow->win);
+  gst_video_overlay_got_window_handle (GST_VIDEO_OVERLAY (xvimagesink),
+      xwindow->win);
 
   return xwindow;
 }
@@ -1211,6 +702,7 @@ gst_xvimagesink_handle_xevents (GstXvImageSink * xvimagesink)
     g_mutex_lock (xvimagesink->flow_lock);
     g_mutex_lock (xvimagesink->x_lock);
   }
+
   if (pointer_moved) {
     g_mutex_unlock (xvimagesink->x_lock);
     g_mutex_unlock (xvimagesink->flow_lock);
@@ -1276,7 +768,8 @@ gst_xvimagesink_handle_xevents (GstXvImageSink * xvimagesink)
         }
         break;
       default:
-        GST_DEBUG ("xvimagesink unhandled X event (%d)", e.type);
+        GST_DEBUG_OBJECT (xvimagesink, "xvimagesink unhandled X event (%d)",
+            e.type);
     }
     g_mutex_lock (xvimagesink->flow_lock);
     g_mutex_lock (xvimagesink->x_lock);
@@ -1304,7 +797,7 @@ gst_xvimagesink_handle_xevents (GstXvImageSink * xvimagesink)
     g_mutex_unlock (xvimagesink->x_lock);
     g_mutex_unlock (xvimagesink->flow_lock);
 
-    gst_xvimagesink_expose (GST_X_OVERLAY (xvimagesink));
+    gst_xvimagesink_expose (GST_VIDEO_OVERLAY (xvimagesink));
 
     g_mutex_lock (xvimagesink->flow_lock);
     g_mutex_lock (xvimagesink->x_lock);
@@ -1554,6 +1047,7 @@ gst_xvimagesink_get_xv_support (GstXvImageSink * xvimagesink,
   for (i = 0; i < nb_formats; i++) {
     GstCaps *format_caps = NULL;
     gboolean is_rgb_format = FALSE;
+    GstVideoFormat vformat;
 
     /* We set the image format of the xcontext to an existing one. This
        is just some valid image format for making our xshm calls check before
@@ -1564,31 +1058,18 @@ gst_xvimagesink_get_xv_support (GstXvImageSink * xvimagesink,
       case XvRGB:
       {
         XvImageFormatValues *fmt = &(formats[i]);
-        gint endianness = G_BIG_ENDIAN;
-
-        if (fmt->byte_order == LSBFirst) {
-          /* our caps system handles 24/32bpp RGB as big-endian. */
-          if (fmt->bits_per_pixel == 24 || fmt->bits_per_pixel == 32) {
-            fmt->red_mask = GUINT32_TO_BE (fmt->red_mask);
-            fmt->green_mask = GUINT32_TO_BE (fmt->green_mask);
-            fmt->blue_mask = GUINT32_TO_BE (fmt->blue_mask);
-
-            if (fmt->bits_per_pixel == 24) {
-              fmt->red_mask >>= 8;
-              fmt->green_mask >>= 8;
-              fmt->blue_mask >>= 8;
-            }
-          } else
-            endianness = G_LITTLE_ENDIAN;
-        }
+        gint endianness;
+
+        endianness =
+            (fmt->byte_order == LSBFirst ? G_LITTLE_ENDIAN : G_BIG_ENDIAN);
 
-        format_caps = gst_caps_new_simple ("video/x-raw-rgb",
-            "endianness", G_TYPE_INT, endianness,
-            "depth", G_TYPE_INT, fmt->depth,
-            "bpp", G_TYPE_INT, fmt->bits_per_pixel,
-            "red_mask", G_TYPE_INT, fmt->red_mask,
-            "green_mask", G_TYPE_INT, fmt->green_mask,
-            "blue_mask", G_TYPE_INT, fmt->blue_mask,
+        vformat = gst_video_format_from_masks (fmt->depth, fmt->bits_per_pixel,
+            endianness, fmt->red_mask, fmt->green_mask, fmt->blue_mask, 0);
+        if (vformat == GST_VIDEO_FORMAT_UNKNOWN)
+          break;
+
+        format_caps = gst_caps_new_simple ("video/x-raw",
+            "format", G_TYPE_STRING, gst_video_format_to_string (vformat),
             "width", GST_TYPE_INT_RANGE, 1, max_w,
             "height", GST_TYPE_INT_RANGE, 1, max_h,
             "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
@@ -1597,13 +1078,20 @@ gst_xvimagesink_get_xv_support (GstXvImageSink * xvimagesink,
         break;
       }
       case XvYUV:
-        format_caps = gst_caps_new_simple ("video/x-raw-yuv",
-            "format", GST_TYPE_FOURCC, formats[i].id,
+      {
+        vformat = gst_video_format_from_fourcc (formats[i].id);
+        if (vformat == GST_VIDEO_FORMAT_UNKNOWN)
+          break;
+
+        format_caps = gst_caps_new_simple ("video/x-raw",
+            "format", G_TYPE_STRING, gst_video_format_to_string (vformat),
             "width", GST_TYPE_INT_RANGE, 1, max_w,
             "height", GST_TYPE_INT_RANGE, 1, max_h,
             "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
         break;
+      }
       default:
+        vformat = GST_VIDEO_FORMAT_UNKNOWN;
         g_assert_not_reached ();
         break;
     }
@@ -1614,6 +1102,7 @@ gst_xvimagesink_get_xv_support (GstXvImageSink * xvimagesink,
       format = g_new0 (GstXvImageFormat, 1);
       if (format) {
         format->format = formats[i].id;
+        format->vformat = vformat;
         format->caps = gst_caps_copy (format_caps);
         xcontext->formats_list = g_list_append (xcontext->formats_list, format);
       }
@@ -1747,6 +1236,7 @@ gst_xvimagesink_calculate_pixel_aspect_ratio (GstXContext * xcontext)
     ratio = 4.0 * 576 / (3.0 * 720);
   }
   GST_DEBUG ("calculated pixel aspect ratio: %f", ratio);
+
   /* now find the one from par[][2] with the lowest delta to the real one */
   delta = DELTA (0);
   index = 0;
@@ -1863,18 +1353,10 @@ gst_xvimagesink_xcontext_get (GstXvImageSink * xvimagesink)
 
   xcontext->caps = gst_xvimagesink_get_xv_support (xvimagesink, xcontext);
 
-  if (!xcontext->caps) {
-    XCloseDisplay (xcontext->disp);
-    g_mutex_unlock (xvimagesink->x_lock);
-    g_free (xcontext->par);
-    g_free (xcontext);
-    /* GST_ELEMENT_ERROR is thrown by gst_xvimagesink_get_xv_support */
-    return NULL;
-  }
-#ifdef HAVE_XSHM
   /* Search for XShm extension support */
+#ifdef HAVE_XSHM
   if (XShmQueryExtension (xcontext->disp) &&
-      gst_xvimagesink_check_xshm_calls (xcontext)) {
+      gst_xvimagesink_check_xshm_calls (xvimagesink, xcontext)) {
     xcontext->use_xshm = TRUE;
     GST_DEBUG ("xvimagesink is using XShm extension");
   } else
@@ -1884,6 +1366,15 @@ gst_xvimagesink_xcontext_get (GstXvImageSink * xvimagesink)
     GST_DEBUG ("xvimagesink is not using XShm extension");
   }
 
+  if (!xcontext->caps) {
+    XCloseDisplay (xcontext->disp);
+    g_mutex_unlock (xvimagesink->x_lock);
+    g_free (xcontext->par);
+    g_free (xcontext);
+    /* GST_ELEMENT_ERROR is thrown by gst_xvimagesink_get_xv_support */
+    return NULL;
+  }
+
   xv_attr = XvQueryPortAttributes (xcontext->disp,
       xcontext->xv_port_id, &N_attr);
 
@@ -1909,8 +1400,8 @@ gst_xvimagesink_xcontext_get (GstXvImageSink * xvimagesink)
 
       channel = g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL, NULL);
       channel->label = g_strdup (channels[i]);
-      channel->min_value = matching_attr->min_value;
-      channel->max_value = matching_attr->max_value;
+      channel->min_value = matching_attr ? matching_attr->min_value : -1000;
+      channel->max_value = matching_attr ? matching_attr->max_value : 1000;
 
       xcontext->channels_list = g_list_append (xcontext->channels_list,
           channel);
@@ -2020,63 +1511,34 @@ gst_xvimagesink_xcontext_clear (GstXvImageSink * xvimagesink)
   g_free (xcontext);
 }
 
-static void
-gst_xvimagesink_imagepool_clear (GstXvImageSink * xvimagesink)
-{
-  g_mutex_lock (xvimagesink->pool_lock);
-
-  while (xvimagesink->image_pool) {
-    GstXvImageBuffer *xvimage = xvimagesink->image_pool->data;
-
-    xvimagesink->image_pool = g_slist_delete_link (xvimagesink->image_pool,
-        xvimagesink->image_pool);
-    gst_xvimage_buffer_free (xvimage);
-  }
-
-  g_mutex_unlock (xvimagesink->pool_lock);
-}
-
 /* Element stuff */
 
-/* This function tries to get a format matching with a given caps in the
-   supported list of formats we generated in gst_xvimagesink_get_xv_support */
-static gint
-gst_xvimagesink_get_format_from_caps (GstXvImageSink * xvimagesink,
-    GstCaps * caps)
-{
-  GList *list = NULL;
-
-  g_return_val_if_fail (GST_IS_XVIMAGESINK (xvimagesink), 0);
-
-  list = xvimagesink->xcontext->formats_list;
-
-  while (list) {
-    GstXvImageFormat *format = list->data;
-
-    if (format) {
-      if (gst_caps_can_intersect (caps, format->caps)) {
-        return format->format;
-      }
-    }
-    list = g_list_next (list);
-  }
-
-  return -1;
-}
-
 static GstCaps *
-gst_xvimagesink_getcaps (GstBaseSink * bsink)
+gst_xvimagesink_getcaps (GstBaseSink * bsink, GstCaps * filter)
 {
   GstXvImageSink *xvimagesink;
+  GstCaps *caps;
 
   xvimagesink = GST_XVIMAGESINK (bsink);
 
-  if (xvimagesink->xcontext)
-    return gst_caps_ref (xvimagesink->xcontext->caps);
+  if (xvimagesink->xcontext) {
+    if (filter)
+      return gst_caps_intersect_full (filter, xvimagesink->xcontext->caps,
+          GST_CAPS_INTERSECT_FIRST);
+    else
+      return gst_caps_ref (xvimagesink->xcontext->caps);
+  }
 
-  return
-      gst_caps_copy (gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD
-          (xvimagesink)));
+  caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (xvimagesink));
+  if (filter) {
+    GstCaps *intersection;
+
+    intersection =
+        gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
+    gst_caps_unref (caps);
+    caps = intersection;
+  }
+  return caps;
 }
 
 static gboolean
@@ -2084,17 +1546,13 @@ gst_xvimagesink_setcaps (GstBaseSink * bsink, GstCaps * caps)
 {
   GstXvImageSink *xvimagesink;
   GstStructure *structure;
+  GstBufferPool *newpool, *oldpool;
+  GstVideoInfo info;
   guint32 im_format = 0;
-  gboolean ret;
-  gint video_width, video_height;
-  gint disp_x, disp_y;
-  gint disp_width, disp_height;
   gint video_par_n, video_par_d;        /* video's PAR */
   gint display_par_n, display_par_d;    /* display's PAR */
-  const GValue *caps_par;
-  const GValue *caps_disp_reg;
-  const GValue *fps;
   guint num, den;
+  gint size;
 
   xvimagesink = GST_XVIMAGESINK (bsink);
 
@@ -2105,38 +1563,31 @@ gst_xvimagesink_setcaps (GstBaseSink * bsink, GstCaps * caps)
   if (!gst_caps_can_intersect (xvimagesink->xcontext->caps, caps))
     goto incompatible_caps;
 
-  structure = gst_caps_get_structure (caps, 0);
-  ret = gst_structure_get_int (structure, "width", &video_width);
-  ret &= gst_structure_get_int (structure, "height", &video_height);
-  fps = gst_structure_get_value (structure, "framerate");
-  ret &= (fps != NULL);
+  if (!gst_video_info_from_caps (&info, caps))
+    goto invalid_format;
 
-  if (!ret)
-    goto incomplete_caps;
+  structure = gst_caps_get_structure (caps, 0);
 
-  xvimagesink->fps_n = gst_value_get_fraction_numerator (fps);
-  xvimagesink->fps_d = gst_value_get_fraction_denominator (fps);
+  xvimagesink->fps_n = info.fps_n;
+  xvimagesink->fps_d = info.fps_d;
 
-  xvimagesink->video_width = video_width;
-  xvimagesink->video_height = video_height;
+  xvimagesink->video_width = info.width;
+  xvimagesink->video_height = info.height;
 
-  im_format = gst_xvimagesink_get_format_from_caps (xvimagesink, caps);
+  im_format = gst_xvimagesink_get_format_from_info (xvimagesink, &info);
   if (im_format == -1)
     goto invalid_format;
 
+  size = info.size;
+
   /* get aspect ratio from caps if it's present, and
    * convert video width and height to a display width and height
    * using wd / hd = wv / hv * PARv / PARd */
 
   /* get video's PAR */
-  caps_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
-  if (caps_par) {
-    video_par_n = gst_value_get_fraction_numerator (caps_par);
-    video_par_d = gst_value_get_fraction_denominator (caps_par);
-  } else {
-    video_par_n = 1;
-    video_par_d = 1;
-  }
+  video_par_n = info.par_n;
+  video_par_d = info.par_d;
+
   /* get display's PAR */
   if (xvimagesink->par) {
     display_par_n = gst_value_get_fraction_numerator (xvimagesink->par);
@@ -2146,32 +1597,13 @@ gst_xvimagesink_setcaps (GstBaseSink * bsink, GstCaps * caps)
     display_par_d = 1;
   }
 
-  /* get the display region */
-  caps_disp_reg = gst_structure_get_value (structure, "display-region");
-  if (caps_disp_reg) {
-    disp_x = g_value_get_int (gst_value_array_get_value (caps_disp_reg, 0));
-    disp_y = g_value_get_int (gst_value_array_get_value (caps_disp_reg, 1));
-    disp_width = g_value_get_int (gst_value_array_get_value (caps_disp_reg, 2));
-    disp_height =
-        g_value_get_int (gst_value_array_get_value (caps_disp_reg, 3));
-  } else {
-    disp_x = disp_y = 0;
-    disp_width = video_width;
-    disp_height = video_height;
-  }
-
-  if (!gst_video_calculate_display_ratio (&num, &den, video_width,
-          video_height, video_par_n, video_par_d, display_par_n, display_par_d))
+  if (!gst_video_calculate_display_ratio (&num, &den, info.width,
+          info.height, video_par_n, video_par_d, display_par_n, display_par_d))
     goto no_disp_ratio;
 
-  xvimagesink->disp_x = disp_x;
-  xvimagesink->disp_y = disp_y;
-  xvimagesink->disp_width = disp_width;
-  xvimagesink->disp_height = disp_height;
-
   GST_DEBUG_OBJECT (xvimagesink,
       "video width/height: %dx%d, calculated display ratio: %d/%d",
-      video_width, video_height, num, den);
+      info.width, info.height, num, den);
 
   /* now find a width x height that respects this display ratio.
    * prefer those that have one of w/h the same as the incoming video
@@ -2179,21 +1611,21 @@ gst_xvimagesink_setcaps (GstBaseSink * bsink, GstCaps * caps)
 
   /* start with same height, because of interlaced video */
   /* check hd / den is an integer scale factor, and scale wd with the PAR */
-  if (video_height % den == 0) {
+  if (info.height % den == 0) {
     GST_DEBUG_OBJECT (xvimagesink, "keeping video height");
     GST_VIDEO_SINK_WIDTH (xvimagesink) = (guint)
-        gst_util_uint64_scale_int (video_height, num, den);
-    GST_VIDEO_SINK_HEIGHT (xvimagesink) = video_height;
-  } else if (video_width % num == 0) {
+        gst_util_uint64_scale_int (info.height, num, den);
+    GST_VIDEO_SINK_HEIGHT (xvimagesink) = info.height;
+  } else if (info.width % num == 0) {
     GST_DEBUG_OBJECT (xvimagesink, "keeping video width");
-    GST_VIDEO_SINK_WIDTH (xvimagesink) = video_width;
+    GST_VIDEO_SINK_WIDTH (xvimagesink) = info.width;
     GST_VIDEO_SINK_HEIGHT (xvimagesink) = (guint)
-        gst_util_uint64_scale_int (video_width, den, num);
+        gst_util_uint64_scale_int (info.width, den, num);
   } else {
     GST_DEBUG_OBJECT (xvimagesink, "approximating while keeping video height");
     GST_VIDEO_SINK_WIDTH (xvimagesink) = (guint)
-        gst_util_uint64_scale_int (video_height, num, den);
-    GST_VIDEO_SINK_HEIGHT (xvimagesink) = video_height;
+        gst_util_uint64_scale_int (info.height, num, den);
+    GST_VIDEO_SINK_HEIGHT (xvimagesink) = info.height;
   }
   GST_DEBUG_OBJECT (xvimagesink, "scaling to %dx%d",
       GST_VIDEO_SINK_WIDTH (xvimagesink), GST_VIDEO_SINK_HEIGHT (xvimagesink));
@@ -2202,7 +1634,7 @@ gst_xvimagesink_setcaps (GstBaseSink * bsink, GstCaps * caps)
   g_mutex_lock (xvimagesink->flow_lock);
   if (!xvimagesink->xwindow) {
     g_mutex_unlock (xvimagesink->flow_lock);
-    gst_x_overlay_prepare_xwindow_id (GST_X_OVERLAY (xvimagesink));
+    gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (xvimagesink));
   } else {
     g_mutex_unlock (xvimagesink->flow_lock);
   }
@@ -2219,27 +1651,31 @@ gst_xvimagesink_setcaps (GstBaseSink * bsink, GstCaps * caps)
         GST_VIDEO_SINK_HEIGHT (xvimagesink));
   }
 
+  xvimagesink->info = info;
+
   /* After a resize, we want to redraw the borders in case the new frame size
    * doesn't cover the same area */
   xvimagesink->redraw_border = TRUE;
 
-  /* We renew our xvimage only if size or format changed;
-   * the xvimage is the same size as the video pixel size */
-  if ((xvimagesink->xvimage) &&
-      ((im_format != xvimagesink->xvimage->im_format) ||
-          (video_width != xvimagesink->xvimage->width) ||
-          (video_height != xvimagesink->xvimage->height))) {
-    GST_DEBUG_OBJECT (xvimagesink,
-        "old format %" GST_FOURCC_FORMAT ", new format %" GST_FOURCC_FORMAT,
-        GST_FOURCC_ARGS (xvimagesink->xvimage->im_format),
-        GST_FOURCC_ARGS (im_format));
-    GST_DEBUG_OBJECT (xvimagesink, "renewing xvimage");
-    gst_buffer_unref (GST_BUFFER (xvimagesink->xvimage));
-    xvimagesink->xvimage = NULL;
-  }
+  /* create a new pool for the new configuration */
+  newpool = gst_xvimage_buffer_pool_new (xvimagesink);
+
+  structure = gst_buffer_pool_get_config (newpool);
+  gst_buffer_pool_config_set (structure, caps, size, 2, 0, 0, 15);
+  if (!gst_buffer_pool_set_config (newpool, structure))
+    goto config_failed;
 
+  oldpool = xvimagesink->pool;
+  xvimagesink->pool = newpool;
   g_mutex_unlock (xvimagesink->flow_lock);
 
+  /* unref the old sink */
+  if (oldpool) {
+    /* we don't deactivate, some elements might still be using it, it will
+     * be deactivated when the last ref is gone */
+    gst_object_unref (oldpool);
+  }
+
   return TRUE;
 
   /* ERRORS */
@@ -2248,12 +1684,6 @@ incompatible_caps:
     GST_ERROR_OBJECT (xvimagesink, "caps incompatible");
     return FALSE;
   }
-incomplete_caps:
-  {
-    GST_DEBUG_OBJECT (xvimagesink, "Failed to retrieve either width, "
-        "height or framerate from intersected caps");
-    return FALSE;
-  }
 invalid_format:
   {
     GST_DEBUG_OBJECT (xvimagesink,
@@ -2272,6 +1702,12 @@ no_display_size:
         ("Error calculating the output display ratio of the video."));
     return FALSE;
   }
+config_failed:
+  {
+    GST_ERROR_OBJECT (xvimagesink, "failed to set config.");
+    g_mutex_unlock (xvimagesink->flow_lock);
+    return FALSE;
+  }
 }
 
 static GstStateChangeReturn
@@ -2288,8 +1724,10 @@ gst_xvimagesink_change_state (GstElement * element, GstStateChange transition)
       /* Initializing the XContext */
       if (xvimagesink->xcontext == NULL) {
         xcontext = gst_xvimagesink_xcontext_get (xvimagesink);
-        if (xcontext == NULL)
-          return GST_STATE_CHANGE_FAILURE;
+        if (xcontext == NULL) {
+          ret = GST_STATE_CHANGE_FAILURE;
+          goto beach;
+        }
         GST_OBJECT_LOCK (xvimagesink);
         if (xcontext)
           xvimagesink->xcontext = xcontext;
@@ -2310,16 +1748,10 @@ gst_xvimagesink_change_state (GstElement * element, GstStateChange transition)
       gst_xvimagesink_manage_event_thread (xvimagesink);
       break;
     case GST_STATE_CHANGE_READY_TO_PAUSED:
-      g_mutex_lock (xvimagesink->pool_lock);
-      xvimagesink->pool_invalid = FALSE;
-      g_mutex_unlock (xvimagesink->pool_lock);
       break;
     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
       break;
     case GST_STATE_CHANGE_PAUSED_TO_READY:
-      g_mutex_lock (xvimagesink->pool_lock);
-      xvimagesink->pool_invalid = TRUE;
-      g_mutex_unlock (xvimagesink->pool_lock);
       break;
     default:
       break;
@@ -2335,6 +1767,10 @@ gst_xvimagesink_change_state (GstElement * element, GstStateChange transition)
       xvimagesink->fps_d = 1;
       GST_VIDEO_SINK_WIDTH (xvimagesink) = 0;
       GST_VIDEO_SINK_HEIGHT (xvimagesink) = 0;
+      g_mutex_lock (xvimagesink->flow_lock);
+      if (xvimagesink->pool)
+        gst_buffer_pool_set_active (xvimagesink->pool, FALSE);
+      g_mutex_unlock (xvimagesink->flow_lock);
       break;
     case GST_STATE_CHANGE_READY_TO_NULL:
       gst_xvimagesink_reset (xvimagesink);
@@ -2343,6 +1779,7 @@ gst_xvimagesink_change_state (GstElement * element, GstStateChange transition)
       break;
   }
 
+beach:
   return ret;
 }
 
@@ -2371,66 +1808,113 @@ gst_xvimagesink_get_times (GstBaseSink * bsink, GstBuffer * buf,
 static GstFlowReturn
 gst_xvimagesink_show_frame (GstVideoSink * vsink, GstBuffer * buf)
 {
+  GstFlowReturn res;
   GstXvImageSink *xvimagesink;
+  GstXvImageMeta *meta;
+  GstBuffer *to_put;
 
   xvimagesink = GST_XVIMAGESINK (vsink);
 
-  /* If this buffer has been allocated using our buffer management we simply
-     put the ximage which is in the PRIVATE pointer */
-  if (GST_IS_XVIMAGE_BUFFER (buf)) {
-    GST_LOG_OBJECT (xvimagesink, "fast put of bufferpool buffer %p", buf);
-    if (!gst_xvimagesink_xvimage_put (xvimagesink,
-            GST_XVIMAGE_BUFFER_CAST (buf)))
-      goto no_window;
+  meta = gst_buffer_get_xvimage_meta (buf);
+
+  if (meta && meta->sink == xvimagesink) {
+    /* If this buffer has been allocated using our buffer management we simply
+       put the ximage which is in the PRIVATE pointer */
+    GST_LOG_OBJECT (xvimagesink, "buffer %p from our pool, writing directly",
+        buf);
+    to_put = buf;
+    res = GST_FLOW_OK;
   } else {
-    GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, xvimagesink,
-        "slow copy into bufferpool buffer %p", buf);
+    GstVideoFrame src, dest;
+
     /* Else we have to copy the data into our private image, */
     /* if we have one... */
-    if (!xvimagesink->xvimage) {
-      GST_DEBUG_OBJECT (xvimagesink, "creating our xvimage");
+    GST_LOG_OBJECT (xvimagesink, "buffer %p not from our pool, copying", buf);
 
-      xvimagesink->xvimage = gst_xvimagesink_xvimage_new (xvimagesink,
-          GST_BUFFER_CAPS (buf));
+    /* we should have a pool, configured in setcaps */
+    if (xvimagesink->pool == NULL)
+      goto no_pool;
 
-      if (!xvimagesink->xvimage)
-        /* The create method should have posted an informative error */
-        goto no_image;
+    if (!gst_buffer_pool_set_active (xvimagesink->pool, TRUE))
+      goto activate_failed;
 
-      if (xvimagesink->xvimage->size < GST_BUFFER_SIZE (buf)) {
-        GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
-            ("Failed to create output image buffer of %dx%d pixels",
-                xvimagesink->xvimage->width, xvimagesink->xvimage->height),
-            ("XServer allocated buffer size did not match input buffer"));
+    /* take a buffer form our pool */
+    res = gst_buffer_pool_acquire_buffer (xvimagesink->pool, &to_put, NULL);
+    if (res != GST_FLOW_OK)
+      goto no_buffer;
 
-        gst_xvimage_buffer_destroy (xvimagesink->xvimage);
-        xvimagesink->xvimage = NULL;
-        goto no_image;
-      }
+    if (gst_buffer_get_size (to_put) < gst_buffer_get_size (buf))
+      goto wrong_size;
+
+    GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, xvimagesink,
+        "slow copy into bufferpool buffer %p", to_put);
+
+    if (!gst_video_frame_map (&src, &xvimagesink->info, buf, GST_MAP_READ))
+      goto invalid_buffer;
+
+    if (!gst_video_frame_map (&dest, &xvimagesink->info, to_put, GST_MAP_WRITE)) {
+      gst_video_frame_unmap (&src);
+      goto invalid_buffer;
     }
 
-    memcpy (xvimagesink->xvimage->xvimage->data,
-        GST_BUFFER_DATA (buf),
-        MIN (GST_BUFFER_SIZE (buf), xvimagesink->xvimage->size));
+    gst_video_frame_copy (&dest, &src);
 
-    if (!gst_xvimagesink_xvimage_put (xvimagesink, xvimagesink->xvimage))
-      goto no_window;
+    gst_video_frame_unmap (&dest);
+    gst_video_frame_unmap (&src);
   }
 
-  return GST_FLOW_OK;
+  if (!gst_xvimagesink_xvimage_put (xvimagesink, to_put))
+    goto no_window;
+
+done:
+  if (to_put != buf)
+    gst_buffer_unref (to_put);
+
+  return res;
 
   /* ERRORS */
-no_image:
+no_pool:
+  {
+    GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
+        ("Internal error: can't allocate images"),
+        ("We don't have a bufferpool negotiated"));
+    return GST_FLOW_ERROR;
+  }
+no_buffer:
   {
     /* No image available. That's very bad ! */
     GST_WARNING_OBJECT (xvimagesink, "could not create image");
-    return GST_FLOW_ERROR;
+    return res;
+  }
+wrong_size:
+  {
+    GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
+        ("Failed to create output image buffer"),
+        ("XServer allocated buffer size did not match input buffer %"
+            G_GSIZE_FORMAT " - %" G_GSIZE_FORMAT, gst_buffer_get_size (to_put),
+            gst_buffer_get_size (buf)));
+    res = GST_FLOW_ERROR;
+    goto done;
+  }
+invalid_buffer:
+  {
+    /* No Window available to put our image into */
+    GST_WARNING_OBJECT (xvimagesink, "could map image");
+    res = GST_FLOW_OK;
+    goto done;
   }
 no_window:
   {
     /* No Window available to put our image into */
     GST_WARNING_OBJECT (xvimagesink, "could not output image - no window");
-    return GST_FLOW_ERROR;
+    res = GST_FLOW_ERROR;
+    goto done;
+  }
+activate_failed:
+  {
+    GST_ERROR_OBJECT (xvimagesink, "failed to activate bufferpool.");
+    res = GST_FLOW_ERROR;
+    goto done;
   }
 }
 
@@ -2459,298 +1943,91 @@ gst_xvimagesink_event (GstBaseSink * sink, GstEvent * event)
     default:
       break;
   }
-  if (GST_BASE_SINK_CLASS (parent_class)->event)
-    return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
-  else
-    return TRUE;
+  return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
 }
 
-/* Buffer management */
-
-static GstCaps *
-gst_xvimage_sink_different_size_suggestion (GstXvImageSink * xvimagesink,
-    GstCaps * caps)
-{
-  GstCaps *intersection;
-  GstCaps *new_caps;
-  GstStructure *s;
-  gint width, height;
-  gint par_n = 1, par_d = 1;
-  gint dar_n, dar_d;
-  gint w, h;
-
-  new_caps = gst_caps_copy (caps);
-
-  s = gst_caps_get_structure (new_caps, 0);
-
-  gst_structure_get_int (s, "width", &width);
-  gst_structure_get_int (s, "height", &height);
-  gst_structure_get_fraction (s, "pixel-aspect-ratio", &par_n, &par_d);
-
-  gst_structure_remove_field (s, "width");
-  gst_structure_remove_field (s, "height");
-  gst_structure_remove_field (s, "pixel-aspect-ratio");
-
-  intersection = gst_caps_intersect (xvimagesink->xcontext->caps, new_caps);
-  gst_caps_unref (new_caps);
-
-  if (gst_caps_is_empty (intersection))
-    return intersection;
-
-  s = gst_caps_get_structure (intersection, 0);
-
-  gst_util_fraction_multiply (width, height, par_n, par_d, &dar_n, &dar_d);
-
-  /* xvimagesink supports all PARs */
-
-  gst_structure_fixate_field_nearest_int (s, "width", width);
-  gst_structure_fixate_field_nearest_int (s, "height", height);
-  gst_structure_get_int (s, "width", &w);
-  gst_structure_get_int (s, "height", &h);
-
-  gst_util_fraction_multiply (h, w, dar_n, dar_d, &par_n, &par_d);
-  gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, par_n, par_d,
-      NULL);
-
-  return intersection;
-}
-
-static GstFlowReturn
-gst_xvimagesink_buffer_alloc (GstBaseSink * bsink, guint64 offset, guint size,
-    GstCaps * caps, GstBuffer ** buf)
+static gboolean
+gst_xvimagesink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
 {
-  GstFlowReturn ret = GST_FLOW_OK;
-  GstXvImageSink *xvimagesink;
-  GstXvImageBuffer *xvimage = NULL;
-  GstCaps *intersection = NULL;
-  GstStructure *structure = NULL;
-  gint width, height, image_format;
+  GstXvImageSink *xvimagesink = GST_XVIMAGESINK (bsink);
+  GstBufferPool *pool;
+  GstStructure *config;
+  GstCaps *caps;
+  guint size;
+  gboolean need_pool;
 
-  xvimagesink = GST_XVIMAGESINK (bsink);
+  gst_query_parse_allocation (query, &caps, &need_pool);
 
-  if (G_UNLIKELY (!caps))
+  if (caps == NULL)
     goto no_caps;
 
-  g_mutex_lock (xvimagesink->pool_lock);
-  if (G_UNLIKELY (xvimagesink->pool_invalid))
-    goto invalid;
-
-  if (G_LIKELY (xvimagesink->xcontext->last_caps &&
-          gst_caps_is_equal (caps, xvimagesink->xcontext->last_caps))) {
-    GST_LOG_OBJECT (xvimagesink,
-        "buffer alloc for same last_caps, reusing caps");
-    intersection = gst_caps_ref (caps);
-    image_format = xvimagesink->xcontext->last_format;
-    width = xvimagesink->xcontext->last_width;
-    height = xvimagesink->xcontext->last_height;
-
-    goto reuse_last_caps;
-  }
-
-  GST_DEBUG_OBJECT (xvimagesink, "buffer alloc requested size %d with caps %"
-      GST_PTR_FORMAT ", intersecting with our caps %" GST_PTR_FORMAT, size,
-      caps, xvimagesink->xcontext->caps);
-
-  /* Check the caps against our xcontext */
-  intersection = gst_caps_intersect (xvimagesink->xcontext->caps, caps);
-
-  GST_DEBUG_OBJECT (xvimagesink, "intersection in buffer alloc returned %"
-      GST_PTR_FORMAT, intersection);
-
-  if (gst_caps_is_empty (intersection)) {
-    GstCaps *new_caps;
-
-    gst_caps_unref (intersection);
-
-    /* So we don't support this kind of buffer, let's define one we'd like */
-    new_caps = gst_caps_copy (caps);
-
-    structure = gst_caps_get_structure (new_caps, 0);
-    if (!gst_structure_has_field (structure, "width") ||
-        !gst_structure_has_field (structure, "height")) {
-      gst_caps_unref (new_caps);
-      goto invalid;
-    }
-
-    /* Try different dimensions */
-    intersection =
-        gst_xvimage_sink_different_size_suggestion (xvimagesink, new_caps);
-
-    if (gst_caps_is_empty (intersection)) {
-      /* Try with different YUV formats first */
-      gst_structure_set_name (structure, "video/x-raw-yuv");
-
-      /* Remove format specific fields */
-      gst_structure_remove_field (structure, "format");
-      gst_structure_remove_field (structure, "endianness");
-      gst_structure_remove_field (structure, "depth");
-      gst_structure_remove_field (structure, "bpp");
-      gst_structure_remove_field (structure, "red_mask");
-      gst_structure_remove_field (structure, "green_mask");
-      gst_structure_remove_field (structure, "blue_mask");
-      gst_structure_remove_field (structure, "alpha_mask");
-
-      /* Reuse intersection with Xcontext */
-      intersection = gst_caps_intersect (xvimagesink->xcontext->caps, new_caps);
-    }
+  g_mutex_lock (xvimagesink->flow_lock);
+  if ((pool = xvimagesink->pool))
+    gst_object_ref (pool);
+  g_mutex_unlock (xvimagesink->flow_lock);
 
-    if (gst_caps_is_empty (intersection)) {
-      /* Try with different dimensions and YUV formats */
-      intersection =
-          gst_xvimage_sink_different_size_suggestion (xvimagesink, new_caps);
-    }
+  if (pool != NULL) {
+    const GstCaps *pcaps;
 
-    if (gst_caps_is_empty (intersection)) {
-      /* Now try with RGB */
-      gst_structure_set_name (structure, "video/x-raw-rgb");
-      /* And interset again */
-      gst_caps_unref (intersection);
-      intersection = gst_caps_intersect (xvimagesink->xcontext->caps, new_caps);
-    }
+    /* we had a pool, check caps */
+    GST_DEBUG_OBJECT (xvimagesink, "check existing pool caps");
+    config = gst_buffer_pool_get_config (pool);
+    gst_buffer_pool_config_get (config, &pcaps, &size, NULL, NULL, NULL, NULL);
 
-    if (gst_caps_is_empty (intersection)) {
-      /* Try with different dimensions and RGB formats */
-      intersection =
-          gst_xvimage_sink_different_size_suggestion (xvimagesink, new_caps);
+    if (!gst_caps_is_equal (caps, pcaps)) {
+      GST_DEBUG_OBJECT (xvimagesink, "pool has different caps");
+      /* different caps, we can't use this pool */
+      gst_object_unref (pool);
+      pool = NULL;
     }
-
-    /* Clean this copy */
-    gst_caps_unref (new_caps);
-
-    if (gst_caps_is_empty (intersection))
-      goto incompatible;
   }
+  if (pool == NULL && need_pool) {
+    GstVideoInfo info;
 
-  /* Ensure the returned caps are fixed */
-  gst_caps_truncate (intersection);
+    GST_DEBUG_OBJECT (xvimagesink, "create new pool");
+    pool = gst_xvimage_buffer_pool_new (xvimagesink);
 
-  GST_DEBUG_OBJECT (xvimagesink, "allocating a buffer with caps %"
-      GST_PTR_FORMAT, intersection);
-  if (gst_caps_is_equal (intersection, caps)) {
-    /* Things work better if we return a buffer with the same caps ptr
-     * as was asked for when we can */
-    gst_caps_replace (&intersection, caps);
-  }
-
-  /* Get image format from caps */
-  image_format = gst_xvimagesink_get_format_from_caps (xvimagesink,
-      intersection);
-
-  /* Get geometry from caps */
-  structure = gst_caps_get_structure (intersection, 0);
-  if (!gst_structure_get_int (structure, "width", &width) ||
-      !gst_structure_get_int (structure, "height", &height) ||
-      image_format == -1)
-    goto invalid_caps;
-
-  /* Store our caps and format as the last_caps to avoid expensive
-   * caps intersection next time */
-  gst_caps_replace (&xvimagesink->xcontext->last_caps, intersection);
-  xvimagesink->xcontext->last_format = image_format;
-  xvimagesink->xcontext->last_width = width;
-  xvimagesink->xcontext->last_height = height;
+    if (!gst_video_info_from_caps (&info, caps))
+      goto invalid_caps;
 
-reuse_last_caps:
+    /* the normal size of a frame */
+    size = info.size;
 
-  /* Walking through the pool cleaning unusable images and searching for a
-     suitable one */
-  while (xvimagesink->image_pool) {
-    xvimage = xvimagesink->image_pool->data;
-
-    if (xvimage) {
-      /* Removing from the pool */
-      xvimagesink->image_pool = g_slist_delete_link (xvimagesink->image_pool,
-          xvimagesink->image_pool);
-
-      /* We check for geometry or image format changes */
-      if ((xvimage->width != width) ||
-          (xvimage->height != height) || (xvimage->im_format != image_format)) {
-        /* This image is unusable. Destroying... */
-        gst_xvimage_buffer_free (xvimage);
-        xvimage = NULL;
-      } else {
-        /* We found a suitable image */
-        GST_LOG_OBJECT (xvimagesink, "found usable image in pool");
-        break;
-      }
-    }
-  }
-
-  if (!xvimage) {
-    /* We found no suitable image in the pool. Creating... */
-    GST_DEBUG_OBJECT (xvimagesink, "no usable image in pool, creating xvimage");
-    xvimage = gst_xvimagesink_xvimage_new (xvimagesink, intersection);
+    config = gst_buffer_pool_get_config (pool);
+    gst_buffer_pool_config_set (config, caps, size, 0, 0, 0, 0);
+    if (!gst_buffer_pool_set_config (pool, config))
+      goto config_failed;
   }
-  g_mutex_unlock (xvimagesink->pool_lock);
+  /* we need at least 2 buffer because we hold on to the last one */
+  gst_query_set_allocation_params (query, size, 2, 0, 0, 0, pool);
 
-  if (xvimage) {
-    /* Make sure the buffer is cleared of any previously used flags */
-    GST_MINI_OBJECT_CAST (xvimage)->flags = 0;
-    gst_buffer_set_caps (GST_BUFFER_CAST (xvimage), intersection);
-  }
+  /* we also support various metadata */
+  gst_query_add_allocation_meta (query, GST_VIDEO_META_API);
+  gst_query_add_allocation_meta (query, GST_VIDEO_CROP_META_API);
 
-  *buf = GST_BUFFER_CAST (xvimage);
+  gst_object_unref (pool);
 
-beach:
-  if (intersection) {
-    gst_caps_unref (intersection);
-  }
-
-  return ret;
+  return TRUE;
 
   /* ERRORS */
-invalid:
-  {
-    GST_DEBUG_OBJECT (xvimagesink, "the pool is flushing");
-    ret = GST_FLOW_WRONG_STATE;
-    g_mutex_unlock (xvimagesink->pool_lock);
-    goto beach;
-  }
-incompatible:
+no_caps:
   {
-    GST_WARNING_OBJECT (xvimagesink, "we were requested a buffer with "
-        "caps %" GST_PTR_FORMAT ", but our xcontext caps %" GST_PTR_FORMAT
-        " are completely incompatible with those caps", caps,
-        xvimagesink->xcontext->caps);
-    ret = GST_FLOW_NOT_NEGOTIATED;
-    g_mutex_unlock (xvimagesink->pool_lock);
-    goto beach;
+    GST_DEBUG_OBJECT (bsink, "no caps specified");
+    return FALSE;
   }
 invalid_caps:
   {
-    GST_WARNING_OBJECT (xvimagesink, "invalid caps for buffer allocation %"
-        GST_PTR_FORMAT, intersection);
-    ret = GST_FLOW_NOT_NEGOTIATED;
-    g_mutex_unlock (xvimagesink->pool_lock);
-    goto beach;
+    GST_DEBUG_OBJECT (bsink, "invalid caps specified");
+    return FALSE;
   }
-no_caps:
+config_failed:
   {
-    GST_WARNING_OBJECT (xvimagesink, "have no caps, doing fallback allocation");
-    *buf = NULL;
-    ret = GST_FLOW_OK;
-    goto beach;
+    GST_DEBUG_OBJECT (bsink, "failed setting config");
+    return FALSE;
   }
 }
 
 /* Interfaces stuff */
-
-static gboolean
-gst_xvimagesink_interface_supported (GstImplementsInterface * iface, GType type)
-{
-  if (type == GST_TYPE_NAVIGATION || type == GST_TYPE_X_OVERLAY ||
-      type == GST_TYPE_COLOR_BALANCE || type == GST_TYPE_PROPERTY_PROBE)
-    return TRUE;
-  else
-    return FALSE;
-}
-
-static void
-gst_xvimagesink_interface_init (GstImplementsInterfaceClass * klass)
-{
-  klass->supported = gst_xvimagesink_interface_supported;
-}
-
 static void
 gst_xvimagesink_navigation_send_event (GstNavigation * navigation,
     GstStructure * structure)
@@ -2821,7 +2098,7 @@ gst_xvimagesink_navigation_init (GstNavigationInterface * iface)
 }
 
 static void
-gst_xvimagesink_set_window_handle (GstXOverlay * overlay, guintptr id)
+gst_xvimagesink_set_window_handle (GstVideoOverlay * overlay, guintptr id)
 {
   XID xwindow_id = id;
   GstXvImageSink *xvimagesink = GST_XVIMAGESINK (overlay);
@@ -2847,15 +2124,6 @@ gst_xvimagesink_set_window_handle (GstXOverlay * overlay, guintptr id)
 
   gst_xvimagesink_update_colorbalance (xvimagesink);
 
-  /* Clear image pool as the images are unusable anyway */
-  gst_xvimagesink_imagepool_clear (xvimagesink);
-
-  /* Clear the xvimage */
-  if (xvimagesink->xvimage) {
-    gst_xvimage_buffer_free (xvimagesink->xvimage);
-    xvimagesink->xvimage = NULL;
-  }
-
   /* If a window is there already we destroy it */
   if (xvimagesink->xwindow) {
     gst_xvimagesink_xwindow_destroy (xvimagesink, xvimagesink->xwindow);
@@ -2910,16 +2178,17 @@ gst_xvimagesink_set_window_handle (GstXOverlay * overlay, guintptr id)
 }
 
 static void
-gst_xvimagesink_expose (GstXOverlay * overlay)
+gst_xvimagesink_expose (GstVideoOverlay * overlay)
 {
   GstXvImageSink *xvimagesink = GST_XVIMAGESINK (overlay);
 
+  GST_DEBUG ("doing expose");
   gst_xvimagesink_xwindow_update_geometry (xvimagesink);
   gst_xvimagesink_xvimage_put (xvimagesink, NULL);
 }
 
 static void
-gst_xvimagesink_set_event_handling (GstXOverlay * overlay,
+gst_xvimagesink_set_event_handling (GstVideoOverlay * overlay,
     gboolean handle_events)
 {
   GstXvImageSink *xvimagesink = GST_XVIMAGESINK (overlay);
@@ -2955,7 +2224,7 @@ gst_xvimagesink_set_event_handling (GstXOverlay * overlay,
 }
 
 static void
-gst_xvimagesink_set_render_rectangle (GstXOverlay * overlay, gint x, gint y,
+gst_xvimagesink_set_render_rectangle (GstVideoOverlay * overlay, gint x, gint y,
     gint width, gint height)
 {
   GstXvImageSink *xvimagesink = GST_XVIMAGESINK (overlay);
@@ -2977,7 +2246,7 @@ gst_xvimagesink_set_render_rectangle (GstXOverlay * overlay, gint x, gint y,
 }
 
 static void
-gst_xvimagesink_xoverlay_init (GstXOverlayClass * iface)
+gst_xvimagesink_video_overlay_init (GstVideoOverlayInterface * iface)
 {
   iface->set_window_handle = gst_xvimagesink_set_window_handle;
   iface->expose = gst_xvimagesink_expose;
@@ -3059,7 +2328,7 @@ gst_xvimagesink_colorbalance_get_value (GstColorBalance * balance,
 }
 
 static void
-gst_xvimagesink_colorbalance_init (GstColorBalanceClass * iface)
+gst_xvimagesink_colorbalance_init (GstColorBalanceInterface * iface)
 {
   GST_COLOR_BALANCE_TYPE (iface) = GST_COLOR_BALANCE_HARDWARE;
   iface->list_channels = gst_xvimagesink_colorbalance_list_channels;
@@ -3290,7 +2559,7 @@ gst_xvimagesink_set_property (GObject * object, guint prop_id,
       xvimagesink->keep_aspect = g_value_get_boolean (value);
       break;
     case PROP_HANDLE_EVENTS:
-      gst_xvimagesink_set_event_handling (GST_X_OVERLAY (xvimagesink),
+      gst_xvimagesink_set_event_handling (GST_VIDEO_OVERLAY (xvimagesink),
           g_value_get_boolean (value));
       gst_xvimagesink_manage_event_thread (xvimagesink);
       break;
@@ -3419,32 +2688,28 @@ gst_xvimagesink_reset (GstXvImageSink * xvimagesink)
   xvimagesink->event_thread = NULL;
   GST_OBJECT_UNLOCK (xvimagesink);
 
-  /* invalidate the pool, current allocations continue, new buffer_alloc fails
-   * with wrong_state */
-  g_mutex_lock (xvimagesink->pool_lock);
-  xvimagesink->pool_invalid = TRUE;
-  g_mutex_unlock (xvimagesink->pool_lock);
-
   /* Wait for our event thread to finish before we clean up our stuff. */
   if (thread)
     g_thread_join (thread);
 
   if (xvimagesink->cur_image) {
-    gst_buffer_unref (GST_BUFFER_CAST (xvimagesink->cur_image));
+    gst_buffer_unref (xvimagesink->cur_image);
     xvimagesink->cur_image = NULL;
   }
-  if (xvimagesink->xvimage) {
-    gst_buffer_unref (GST_BUFFER_CAST (xvimagesink->xvimage));
-    xvimagesink->xvimage = NULL;
-  }
 
-  gst_xvimagesink_imagepool_clear (xvimagesink);
+  g_mutex_lock (xvimagesink->flow_lock);
+
+  if (xvimagesink->pool) {
+    gst_object_unref (xvimagesink->pool);
+    xvimagesink->pool = NULL;
+  }
 
   if (xvimagesink->xwindow) {
     gst_xvimagesink_xwindow_clear (xvimagesink, xvimagesink->xwindow);
     gst_xvimagesink_xwindow_destroy (xvimagesink, xvimagesink->xwindow);
     xvimagesink->xwindow = NULL;
   }
+  g_mutex_unlock (xvimagesink->flow_lock);
 
   xvimagesink->render_rect.x = xvimagesink->render_rect.y =
       xvimagesink->render_rect.w = xvimagesink->render_rect.h = 0;
@@ -3482,10 +2747,6 @@ gst_xvimagesink_finalize (GObject * object)
     g_mutex_free (xvimagesink->flow_lock);
     xvimagesink->flow_lock = NULL;
   }
-  if (xvimagesink->pool_lock) {
-    g_mutex_free (xvimagesink->pool_lock);
-    xvimagesink->pool_lock = NULL;
-  }
 
   g_free (xvimagesink->media_title);
 
@@ -3493,14 +2754,12 @@ gst_xvimagesink_finalize (GObject * object)
 }
 
 static void
-gst_xvimagesink_init (GstXvImageSink * xvimagesink,
-    GstXvImageSinkClass * xvimagesinkclass)
+gst_xvimagesink_init (GstXvImageSink * xvimagesink)
 {
   xvimagesink->display_name = NULL;
   xvimagesink->adaptor_no = 0;
   xvimagesink->xcontext = NULL;
   xvimagesink->xwindow = NULL;
-  xvimagesink->xvimage = NULL;
   xvimagesink->cur_image = NULL;
 
   xvimagesink->hue = xvimagesink->saturation = 0;
@@ -3515,8 +2774,7 @@ gst_xvimagesink_init (GstXvImageSink * xvimagesink,
   xvimagesink->x_lock = g_mutex_new ();
   xvimagesink->flow_lock = g_mutex_new ();
 
-  xvimagesink->image_pool = NULL;
-  xvimagesink->pool_lock = g_mutex_new ();
+  xvimagesink->pool = NULL;
 
   xvimagesink->synchronous = FALSE;
   xvimagesink->double_buffer = TRUE;
@@ -3536,19 +2794,6 @@ gst_xvimagesink_init (GstXvImageSink * xvimagesink,
 }
 
 static void
-gst_xvimagesink_base_init (gpointer g_class)
-{
-  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
-
-  gst_element_class_set_details_simple (element_class,
-      "Video sink", "Sink/Video",
-      "A Xv based videosink", "Julien Moutte <julien@moutte.net>");
-
-  gst_element_class_add_static_pad_template (element_class,
-      &gst_xvimagesink_sink_template_factory);
-}
-
-static void
 gst_xvimagesink_class_init (GstXvImageSinkClass * klass)
 {
   GObjectClass *gobject_class;
@@ -3561,6 +2806,8 @@ gst_xvimagesink_class_init (GstXvImageSinkClass * klass)
   gstbasesink_class = (GstBaseSinkClass *) klass;
   videosink_class = (GstVideoSinkClass *) klass;
 
+  parent_class = g_type_class_peek_parent (klass);
+
   gobject_class->set_property = gst_xvimagesink_set_property;
   gobject_class->get_property = gst_xvimagesink_get_property;
 
@@ -3693,90 +2940,22 @@ gst_xvimagesink_class_init (GstXvImageSinkClass * klass)
 
   gobject_class->finalize = gst_xvimagesink_finalize;
 
+  gst_element_class_set_details_simple (gstelement_class,
+      "Video sink", "Sink/Video",
+      "A Xv based videosink", "Julien Moutte <julien@moutte.net>");
+
+  gst_element_class_add_pad_template (gstelement_class,
+      gst_static_pad_template_get (&gst_xvimagesink_sink_template_factory));
+
   gstelement_class->change_state =
       GST_DEBUG_FUNCPTR (gst_xvimagesink_change_state);
 
   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_xvimagesink_getcaps);
   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_xvimagesink_setcaps);
-  gstbasesink_class->buffer_alloc =
-      GST_DEBUG_FUNCPTR (gst_xvimagesink_buffer_alloc);
   gstbasesink_class->get_times = GST_DEBUG_FUNCPTR (gst_xvimagesink_get_times);
+  gstbasesink_class->propose_allocation =
+      GST_DEBUG_FUNCPTR (gst_xvimagesink_propose_allocation);
   gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_xvimagesink_event);
 
   videosink_class->show_frame = GST_DEBUG_FUNCPTR (gst_xvimagesink_show_frame);
 }
-
-/* ============================================================= */
-/*                                                               */
-/*                       Public Methods                          */
-/*                                                               */
-/* ============================================================= */
-
-/* =========================================== */
-/*                                             */
-/*          Object typing & Creation           */
-/*                                             */
-/* =========================================== */
-static void
-gst_xvimagesink_init_interfaces (GType type)
-{
-  static const GInterfaceInfo iface_info = {
-    (GInterfaceInitFunc) gst_xvimagesink_interface_init,
-    NULL,
-    NULL,
-  };
-  static const GInterfaceInfo navigation_info = {
-    (GInterfaceInitFunc) gst_xvimagesink_navigation_init,
-    NULL,
-    NULL,
-  };
-  static const GInterfaceInfo overlay_info = {
-    (GInterfaceInitFunc) gst_xvimagesink_xoverlay_init,
-    NULL,
-    NULL,
-  };
-  static const GInterfaceInfo colorbalance_info = {
-    (GInterfaceInitFunc) gst_xvimagesink_colorbalance_init,
-    NULL,
-    NULL,
-  };
-  static const GInterfaceInfo propertyprobe_info = {
-    (GInterfaceInitFunc) gst_xvimagesink_property_probe_interface_init,
-    NULL,
-    NULL,
-  };
-
-  g_type_add_interface_static (type,
-      GST_TYPE_IMPLEMENTS_INTERFACE, &iface_info);
-  g_type_add_interface_static (type, GST_TYPE_NAVIGATION, &navigation_info);
-  g_type_add_interface_static (type, GST_TYPE_X_OVERLAY, &overlay_info);
-  g_type_add_interface_static (type, GST_TYPE_COLOR_BALANCE,
-      &colorbalance_info);
-  g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
-      &propertyprobe_info);
-
-  /* register type and create class in a more safe place instead of at
-   * runtime since the type registration and class creation is not
-   * threadsafe. */
-  g_type_class_ref (gst_xvimage_buffer_get_type ());
-}
-
-static gboolean
-plugin_init (GstPlugin * plugin)
-{
-  if (!gst_element_register (plugin, "xvimagesink",
-          GST_RANK_PRIMARY, GST_TYPE_XVIMAGESINK))
-    return FALSE;
-
-  GST_DEBUG_CATEGORY_INIT (gst_debug_xvimagesink, "xvimagesink", 0,
-      "xvimagesink element");
-  GST_DEBUG_CATEGORY_GET (GST_CAT_PERFORMANCE, "GST_PERFORMANCE");
-
-  return TRUE;
-}
-
-GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
-    GST_VERSION_MINOR,
-    "xvimagesink",
-    "XFree86 video output plugin using Xv extension",
-    plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)