Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / sys / ximage / ximagepool.c
index 2752831..7ad88e9 100644 (file)
 
 /* Helper functions */
 #include <gst/video/video.h>
-#include <gst/video/gstmetavideo.h>
+#include <gst/video/gstvideometa.h>
+#include <gst/video/gstvideopool.h>
 
 GST_DEBUG_CATEGORY_EXTERN (gst_debug_ximagepool);
 #define GST_CAT_DEFAULT gst_debug_ximagepool
 
-static void gst_meta_ximage_free (GstMetaXImage * meta, GstBuffer * buffer);
+struct _GstXImageBufferPoolPrivate
+{
+  GstCaps *caps;
+  GstVideoInfo info;
+  GstVideoAlignment align;
+  guint padded_width;
+  guint padded_height;
+  gboolean add_metavideo;
+  gboolean need_alignment;
+};
+
+static void gst_ximage_meta_free (GstXImageMeta * meta, GstBuffer * buffer);
 
 /* ximage metadata */
 const GstMetaInfo *
-gst_meta_ximage_get_info (void)
+gst_ximage_meta_get_info (void)
 {
-  static const GstMetaInfo *meta_ximage_info = NULL;
+  static const GstMetaInfo *ximage_meta_info = NULL;
 
-  if (meta_ximage_info == NULL) {
-    meta_ximage_info = gst_meta_register ("GstMetaXImage", "GstMetaXImage",
-        sizeof (GstMetaXImage),
+  if (ximage_meta_info == NULL) {
+    ximage_meta_info = gst_meta_register ("GstXImageMeta", "GstXImageMeta",
+        sizeof (GstXImageMeta),
         (GstMetaInitFunction) NULL,
-        (GstMetaFreeFunction) gst_meta_ximage_free,
+        (GstMetaFreeFunction) gst_ximage_meta_free,
         (GstMetaCopyFunction) NULL, (GstMetaTransformFunction) NULL);
   }
-  return meta_ximage_info;
+  return ximage_meta_info;
 }
 
 /* X11 stuff */
@@ -66,30 +78,39 @@ gst_ximagesink_handle_xerror (Display * display, XErrorEvent * xevent)
   return 0;
 }
 
-GstMetaXImage *
-gst_buffer_add_meta_ximage (GstBuffer * buffer, GstXImageSink * ximagesink,
-    gint width, gint height)
+static GstXImageMeta *
+gst_buffer_add_ximage_meta (GstBuffer * buffer, GstXImageBufferPool * xpool)
 {
+  GstXImageSink *ximagesink;
   int (*handler) (Display *, XErrorEvent *);
   gboolean success = FALSE;
   GstXContext *xcontext;
-  GstMetaXImage *meta;
+  GstXImageMeta *meta;
+  gint width, height;
+  GstXImageBufferPoolPrivate *priv;
 
+  priv = xpool->priv;
+  ximagesink = xpool->sink;
   xcontext = ximagesink->xcontext;
 
+  width = priv->padded_width;
+  height = priv->padded_height;
+
   meta =
-      (GstMetaXImage *) gst_buffer_add_meta (buffer, GST_META_INFO_XIMAGE,
+      (GstXImageMeta *) gst_buffer_add_meta (buffer, GST_XIMAGE_META_INFO,
       NULL);
 #ifdef HAVE_XSHM
   meta->SHMInfo.shmaddr = ((void *) -1);
   meta->SHMInfo.shmid = -1;
 #endif
-  meta->width = width;
-  meta->height = height;
+  meta->x = priv->align.padding_left;
+  meta->y = priv->align.padding_top;
+  meta->width = GST_VIDEO_INFO_WIDTH (&priv->info);
+  meta->height = GST_VIDEO_INFO_HEIGHT (&priv->info);
   meta->sink = gst_object_ref (ximagesink);
 
   GST_DEBUG_OBJECT (ximagesink, "creating image %p (%dx%d)", buffer,
-      meta->width, meta->height);
+      width, height);
 
   g_mutex_lock (ximagesink->x_lock);
 
@@ -101,8 +122,7 @@ gst_buffer_add_meta_ximage (GstBuffer * buffer, GstXImageSink * ximagesink,
   if (xcontext->use_xshm) {
     meta->ximage = XShmCreateImage (xcontext->disp,
         xcontext->visual,
-        xcontext->depth,
-        ZPixmap, NULL, &meta->SHMInfo, meta->width, meta->height);
+        xcontext->depth, ZPixmap, NULL, &meta->SHMInfo, width, height);
     if (!meta->ximage || error_caught) {
       g_mutex_unlock (ximagesink->x_lock);
 
@@ -112,9 +132,8 @@ gst_buffer_add_meta_ximage (GstBuffer * buffer, GstXImageSink * ximagesink,
       /* Push a warning */
       GST_ELEMENT_WARNING (ximagesink, RESOURCE, WRITE,
           ("Failed to create output image buffer of %dx%d pixels",
-              meta->width, meta->height),
-          ("could not XShmCreateImage a %dx%d image",
-              meta->width, meta->height));
+              width, height),
+          ("could not XShmCreateImage a %dx%d image", width, height));
 
       /* Retry without XShm */
       ximagesink->xcontext->use_xshm = FALSE;
@@ -129,7 +148,7 @@ gst_buffer_add_meta_ximage (GstBuffer * buffer, GstXImageSink * ximagesink,
     meta->size = meta->ximage->bytes_per_line * meta->ximage->height;
     GST_LOG_OBJECT (ximagesink,
         "XShm image size is %" G_GSIZE_FORMAT ", width %d, stride %d",
-        meta->size, meta->width, meta->ximage->bytes_per_line);
+        meta->size, width, meta->ximage->bytes_per_line);
 
     /* get shared memory */
     meta->SHMInfo.shmid = shmget (IPC_PRIVATE, meta->size, IPC_CREAT | 0777);
@@ -165,8 +184,7 @@ gst_buffer_add_meta_ximage (GstBuffer * buffer, GstXImageSink * ximagesink,
 
     meta->ximage = XCreateImage (xcontext->disp,
         xcontext->visual,
-        xcontext->depth,
-        ZPixmap, 0, NULL, meta->width, meta->height, xcontext->bpp, 0);
+        xcontext->depth, ZPixmap, 0, NULL, width, height, xcontext->bpp, 0);
     if (!meta->ximage || error_caught)
       goto create_failed;
 
@@ -189,7 +207,7 @@ gst_buffer_add_meta_ximage (GstBuffer * buffer, GstXImageSink * ximagesink,
     meta->ximage->data = g_malloc (allocsize);
     GST_LOG_OBJECT (ximagesink,
         "non-XShm image size is %" G_GSIZE_FORMAT " (alloced: %u), width %d, "
-        "stride %d", meta->size, allocsize, meta->width,
+        "stride %d", meta->size, allocsize, width,
         meta->ximage->bytes_per_line);
 
     XSync (xcontext->disp, FALSE);
@@ -223,8 +241,8 @@ create_failed:
     /* Push an error */
     GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
         ("Failed to create output image buffer of %dx%d pixels",
-            meta->width, meta->height),
-        ("could not XShmCreateImage a %dx%d image", meta->width, meta->height));
+            width, height),
+        ("could not XShmCreateImage a %dx%d image", width, height));
     goto beach;
   }
 shmget_failed:
@@ -232,7 +250,7 @@ shmget_failed:
     g_mutex_unlock (ximagesink->x_lock);
     GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
         ("Failed to create output image buffer of %dx%d pixels",
-            meta->width, meta->height),
+            width, height),
         ("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
             meta->size));
     goto beach;
@@ -242,8 +260,7 @@ shmat_failed:
     g_mutex_unlock (ximagesink->x_lock);
     GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
         ("Failed to create output image buffer of %dx%d pixels",
-            meta->width, meta->height),
-        ("Failed to shmat: %s", g_strerror (errno)));
+            width, height), ("Failed to shmat: %s", g_strerror (errno)));
     /* Clean up the shared memory segment */
     shmctl (meta->SHMInfo.shmid, IPC_RMID, NULL);
     goto beach;
@@ -256,13 +273,13 @@ xattach_failed:
 
     GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
         ("Failed to create output image buffer of %dx%d pixels",
-            meta->width, meta->height), ("Failed to XShmAttach"));
+            width, height), ("Failed to XShmAttach"));
     goto beach;
   }
 }
 
 static void
-gst_meta_ximage_free (GstMetaXImage * meta, GstBuffer * buffer)
+gst_ximage_meta_free (GstXImageMeta * meta, GstBuffer * buffer)
 {
   GstXImageSink *ximagesink;
 
@@ -430,22 +447,18 @@ static void gst_ximage_buffer_pool_finalize (GObject * object);
 #define GST_XIMAGE_BUFFER_POOL_GET_PRIVATE(obj)  \
    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_XIMAGE_BUFFER_POOL, GstXImageBufferPoolPrivate))
 
-struct _GstXImageBufferPoolPrivate
-{
-  GstCaps *caps;
-  GstVideoInfo info;
-  gboolean add_metavideo;
-};
-
+#define gst_ximage_buffer_pool_parent_class parent_class
 G_DEFINE_TYPE (GstXImageBufferPool, gst_ximage_buffer_pool,
     GST_TYPE_BUFFER_POOL);
 
 static const gchar **
-ximage_buffer_pool_get_metas (GstBufferPool * pool)
+ximage_buffer_pool_get_options (GstBufferPool * pool)
 {
-  static const gchar *metas[] = { "GstMetaVideo", NULL };
+  static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
+    GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT, NULL
+  };
 
-  return metas;
+  return options;
 }
 
 static gboolean
@@ -478,9 +491,35 @@ ximage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
 
   /* check for the configured metadata */
   priv->add_metavideo =
-      gst_buffer_pool_config_has_meta (config, GST_META_API_VIDEO);
+      gst_buffer_pool_config_has_option (config,
+      GST_BUFFER_POOL_OPTION_VIDEO_META);
+
+  /* parse extra alignment info */
+  priv->need_alignment = gst_buffer_pool_config_has_option (config,
+      GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
 
-  return TRUE;
+  if (priv->need_alignment) {
+    gst_buffer_pool_config_get_video_alignment (config, &priv->align);
+
+    GST_LOG_OBJECT (pool, "padding %u-%ux%u-%u", priv->align.padding_top,
+        priv->align.padding_left, priv->align.padding_left,
+        priv->align.padding_bottom);
+
+    /* we need the video metadata too now */
+    priv->add_metavideo = TRUE;
+  } else {
+    gst_video_alignment_reset (&priv->align);
+  }
+
+  /* add the padding */
+  priv->padded_width =
+      GST_VIDEO_INFO_WIDTH (&info) + priv->align.padding_left +
+      priv->align.padding_right;
+  priv->padded_height =
+      GST_VIDEO_INFO_HEIGHT (&info) + priv->align.padding_top +
+      priv->align.padding_bottom;
+
+  return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
 
   /* ERRORS */
 wrong_config:
@@ -510,23 +549,36 @@ ximage_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
   GstXImageBufferPoolPrivate *priv = xpool->priv;
   GstVideoInfo *info;
   GstBuffer *ximage;
-  GstMetaXImage *meta;
+  GstXImageMeta *meta;
 
   info = &priv->info;
 
   ximage = gst_buffer_new ();
-  meta =
-      gst_buffer_add_meta_ximage (ximage, xpool->sink,
-      GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info));
+  meta = gst_buffer_add_ximage_meta (ximage, xpool);
   if (meta == NULL) {
     gst_buffer_unref (ximage);
     goto no_buffer;
   }
   if (priv->add_metavideo) {
-    GST_DEBUG_OBJECT (pool, "adding GstMetaVideo");
+    GstVideoMeta *meta;
+
+    GST_DEBUG_OBJECT (pool, "adding GstVideoMeta");
     /* these are just the defaults for now */
-    gst_buffer_add_meta_video (ximage, 0, GST_VIDEO_INFO_FORMAT (info),
-        GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info));
+    meta = gst_buffer_add_video_meta (ximage, 0, GST_VIDEO_INFO_FORMAT (info),
+        priv->padded_width, priv->padded_height);
+
+    if (priv->need_alignment) {
+      gint vpad, hpad, pstride;
+
+      vpad = priv->align.padding_left;
+      hpad = priv->align.padding_top;
+
+      meta->width = GST_VIDEO_INFO_WIDTH (&priv->info);
+      meta->height = GST_VIDEO_INFO_HEIGHT (&priv->info);
+      pstride = GST_VIDEO_INFO_COMP_PSTRIDE (&priv->info, 0);
+
+      meta->offset[0] += (vpad * meta->stride[0]) + (hpad * pstride);
+    }
   }
   *buffer = ximage;
 
@@ -540,12 +592,6 @@ no_buffer:
   }
 }
 
-static void
-ximage_buffer_pool_free (GstBufferPool * pool, GstBuffer * buffer)
-{
-  gst_buffer_unref (buffer);
-}
-
 GstBufferPool *
 gst_ximage_buffer_pool_new (GstXImageSink * ximagesink)
 {
@@ -571,10 +617,9 @@ gst_ximage_buffer_pool_class_init (GstXImageBufferPoolClass * klass)
 
   gobject_class->finalize = gst_ximage_buffer_pool_finalize;
 
-  gstbufferpool_class->get_metas = ximage_buffer_pool_get_metas;
+  gstbufferpool_class->get_options = ximage_buffer_pool_get_options;
   gstbufferpool_class->set_config = ximage_buffer_pool_set_config;
   gstbufferpool_class->alloc_buffer = ximage_buffer_pool_alloc;
-  gstbufferpool_class->free_buffer = ximage_buffer_pool_free;
 }
 
 static void