Blame gst-libs/gst/gl/gstglbufferpool.c

Packit 971217
/*
Packit 971217
 * GStreamer
Packit 971217
 * Copyright (C) 2012 Matthew Waters <>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include "gstglbufferpool.h"
Packit 971217
Packit 971217
#include "gstglmemory.h"
Packit 971217
#include "gstglsyncmeta.h"
Packit 971217
#include "gstglutils.h"
Packit 971217
Packit 971217
/**
Packit 971217
 * SECTION:gstglbufferpool
Packit 971217
 * @title: GstGLBufferPool
Packit 971217
 * @short_description: buffer pool for #GstGLBaseMemory objects
Packit 971217
 * @see_also: #GstBufferPool, #GstGLBaseMemory, #GstGLMemory
Packit 971217
 *
Packit 971217
 * a #GstGLBufferPool is an object that allocates buffers with #GstGLBaseMemory
Packit 971217
 *
Packit 971217
 * A #GstGLBufferPool is created with gst_gl_buffer_pool_new()
Packit 971217
 *
Packit 971217
 * #GstGLBufferPool implements the VideoMeta buffer pool option
Packit 971217
 * %GST_BUFFER_POOL_OPTION_VIDEO_META, the VideoAligment buffer pool option
Packit 971217
 * %GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT as well as the OpenGL specific
Packit 971217
 * %GST_BUFFER_POOL_OPTION_GL_SYNC_META buffer pool option.
Packit 971217
 */
Packit 971217
Packit 971217
/* bufferpool */
Packit 971217
struct _GstGLBufferPoolPrivate
Packit 971217
{
Packit 971217
  GstAllocator *allocator;
Packit 971217
  GstGLVideoAllocationParams *gl_params;
Packit 971217
  GstCaps *caps;
Packit 971217
  gboolean add_videometa;
Packit 971217
  gboolean add_glsyncmeta;
Packit 971217
};
Packit 971217
Packit 971217
static void gst_gl_buffer_pool_finalize (GObject * object);
Packit 971217
Packit 971217
GST_DEBUG_CATEGORY_STATIC (GST_CAT_GL_BUFFER_POOL);
Packit 971217
#define GST_CAT_DEFAULT GST_CAT_GL_BUFFER_POOL
Packit 971217
Packit 971217
#define GST_GL_BUFFER_POOL_GET_PRIVATE(obj)  \
Packit 971217
   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_GL_BUFFER_POOL, GstGLBufferPoolPrivate))
Packit 971217
Packit 971217
#define gst_gl_buffer_pool_parent_class parent_class
Packit 971217
G_DEFINE_TYPE_WITH_CODE (GstGLBufferPool, gst_gl_buffer_pool,
Packit 971217
    GST_TYPE_BUFFER_POOL, GST_DEBUG_CATEGORY_INIT (GST_CAT_GL_BUFFER_POOL,
Packit 971217
        "glbufferpool", 0, "GL Buffer Pool"));
Packit 971217
Packit 971217
static const gchar **
Packit 971217
gst_gl_buffer_pool_get_options (GstBufferPool * pool)
Packit 971217
{
Packit 971217
  static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
Packit 971217
    GST_BUFFER_POOL_OPTION_GL_SYNC_META,
Packit 971217
    GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT,
Packit 971217
    GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_2D,
Packit 971217
    GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_RECTANGLE,
Packit 971217
    NULL
Packit 971217
  };
Packit 971217
Packit 971217
  return options;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_gl_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
Packit 971217
{
Packit 971217
  GstGLBufferPool *glpool = GST_GL_BUFFER_POOL_CAST (pool);
Packit 971217
  GstGLBufferPoolPrivate *priv = glpool->priv;
Packit 971217
  GstVideoInfo info;
Packit 971217
  GstCaps *caps = NULL;
Packit 971217
  guint min_buffers, max_buffers;
Packit 971217
  guint max_align, n;
Packit 971217
  GstAllocator *allocator = NULL;
Packit 971217
  GstAllocationParams alloc_params;
Packit 971217
  GstGLTextureTarget tex_target;
Packit 971217
  gboolean ret = TRUE;
Packit 971217
  gint p;
Packit 971217
Packit 971217
  if (!gst_buffer_pool_config_get_params (config, &caps, NULL, &min_buffers,
Packit 971217
          &max_buffers))
Packit 971217
    goto wrong_config;
Packit 971217
Packit 971217
  if (caps == NULL)
Packit 971217
    goto no_caps;
Packit 971217
Packit 971217
  /* now parse the caps from the config */
Packit 971217
  if (!gst_video_info_from_caps (&info, caps))
Packit 971217
    goto wrong_caps;
Packit 971217
Packit 971217
  GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
Packit 971217
      caps);
Packit 971217
Packit 971217
  if (!gst_buffer_pool_config_get_allocator (config, &allocator, &alloc_params))
Packit 971217
    goto wrong_config;
Packit 971217
Packit 971217
  gst_caps_replace (&priv->caps, caps);
Packit 971217
Packit 971217
  if (priv->allocator)
Packit 971217
    gst_object_unref (priv->allocator);
Packit 971217
Packit 971217
  if (allocator) {
Packit 971217
    if (!GST_IS_GL_MEMORY_ALLOCATOR (allocator)) {
Packit 971217
      gst_object_unref (allocator);
Packit 971217
      goto wrong_allocator;
Packit 971217
    } else {
Packit 971217
      priv->allocator = gst_object_ref (allocator);
Packit 971217
    }
Packit 971217
  } else {
Packit 971217
    priv->allocator =
Packit 971217
        GST_ALLOCATOR (gst_gl_memory_allocator_get_default (glpool->context));
Packit 971217
    g_assert (priv->allocator);
Packit 971217
  }
Packit 971217
Packit 971217
  priv->add_videometa = gst_buffer_pool_config_has_option (config,
Packit 971217
      GST_BUFFER_POOL_OPTION_VIDEO_META);
Packit 971217
  priv->add_glsyncmeta = gst_buffer_pool_config_has_option (config,
Packit 971217
      GST_BUFFER_POOL_OPTION_GL_SYNC_META);
Packit 971217
Packit 971217
  if (priv->gl_params)
Packit 971217
    gst_gl_allocation_params_free ((GstGLAllocationParams *) priv->gl_params);
Packit 971217
  priv->gl_params = (GstGLVideoAllocationParams *)
Packit 971217
      gst_buffer_pool_config_get_gl_allocation_params (config);
Packit 971217
  if (!priv->gl_params)
Packit 971217
    priv->gl_params = gst_gl_video_allocation_params_new (glpool->context,
Packit 971217
        &alloc_params, &info, -1, NULL, 0, 0);
Packit 971217
Packit 971217
  max_align = alloc_params.align;
Packit 971217
Packit 971217
  if (gst_buffer_pool_config_has_option (config,
Packit 971217
          GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT)) {
Packit 971217
    priv->add_videometa = TRUE;
Packit 971217
Packit 971217
    gst_buffer_pool_config_get_video_alignment (config,
Packit 971217
        priv->gl_params->valign);
Packit 971217
Packit 971217
    for (n = 0; n < GST_VIDEO_MAX_PLANES; ++n)
Packit 971217
      max_align |= priv->gl_params->valign->stride_align[n];
Packit 971217
Packit 971217
    for (n = 0; n < GST_VIDEO_MAX_PLANES; ++n)
Packit 971217
      priv->gl_params->valign->stride_align[n] = max_align;
Packit 971217
Packit 971217
    gst_video_info_align (priv->gl_params->v_info, priv->gl_params->valign);
Packit 971217
Packit 971217
    gst_buffer_pool_config_set_video_alignment (config,
Packit 971217
        priv->gl_params->valign);
Packit 971217
  }
Packit 971217
Packit 971217
  if (alloc_params.align < max_align) {
Packit 971217
    GST_WARNING_OBJECT (pool, "allocation params alignment %u is smaller "
Packit 971217
        "than the max specified video stride alignment %u, fixing",
Packit 971217
        (guint) alloc_params.align, max_align);
Packit 971217
Packit 971217
    alloc_params.align = max_align;
Packit 971217
    gst_buffer_pool_config_set_allocator (config, allocator, &alloc_params);
Packit 971217
    if (priv->gl_params->parent.alloc_params)
Packit 971217
      gst_allocation_params_free (priv->gl_params->parent.alloc_params);
Packit 971217
    priv->gl_params->parent.alloc_params =
Packit 971217
        gst_allocation_params_copy (&alloc_params);
Packit 971217
  }
Packit 971217
Packit 971217
  {
Packit 971217
    GstStructure *s = gst_caps_get_structure (caps, 0);
Packit 971217
    const gchar *target_str = gst_structure_get_string (s, "texture-target");
Packit 971217
    gboolean multiple_texture_targets = FALSE;
Packit 971217
Packit 971217
    tex_target = priv->gl_params->target;
Packit 971217
    if (target_str)
Packit 971217
      tex_target = gst_gl_texture_target_from_string (target_str);
Packit 971217
Packit 971217
    if (gst_buffer_pool_config_has_option (config,
Packit 971217
            GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_2D)) {
Packit 971217
      if (tex_target && tex_target != GST_GL_TEXTURE_TARGET_2D)
Packit 971217
        multiple_texture_targets = TRUE;
Packit 971217
      tex_target = GST_GL_TEXTURE_TARGET_2D;
Packit 971217
    }
Packit 971217
    if (gst_buffer_pool_config_has_option (config,
Packit 971217
            GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_RECTANGLE)) {
Packit 971217
      if (tex_target && tex_target != GST_GL_TEXTURE_TARGET_RECTANGLE)
Packit 971217
        multiple_texture_targets = TRUE;
Packit 971217
      tex_target = GST_GL_TEXTURE_TARGET_RECTANGLE;
Packit 971217
    }
Packit 971217
    if (gst_buffer_pool_config_has_option (config,
Packit 971217
            GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_EXTERNAL_OES)) {
Packit 971217
      if (tex_target && tex_target != GST_GL_TEXTURE_TARGET_EXTERNAL_OES)
Packit 971217
        multiple_texture_targets = TRUE;
Packit 971217
      tex_target = GST_GL_TEXTURE_TARGET_EXTERNAL_OES;
Packit 971217
    }
Packit 971217
Packit 971217
    if (!tex_target)
Packit 971217
      tex_target = GST_GL_TEXTURE_TARGET_2D;
Packit 971217
Packit 971217
    if (multiple_texture_targets) {
Packit 971217
      GST_WARNING_OBJECT (pool, "Multiple texture targets configured either "
Packit 971217
          "through caps or buffer pool options");
Packit 971217
      ret = FALSE;
Packit 971217
    }
Packit 971217
Packit 971217
    priv->gl_params->target = tex_target;
Packit 971217
  }
Packit 971217
Packit 971217
  /* Recalulate the size and offset as we don't add padding between planes. */
Packit 971217
  priv->gl_params->v_info->size = 0;
Packit 971217
  for (p = 0; p < GST_VIDEO_INFO_N_PLANES (priv->gl_params->v_info); p++) {
Packit 971217
    priv->gl_params->v_info->offset[p] = priv->gl_params->v_info->size;
Packit 971217
    priv->gl_params->v_info->size +=
Packit 971217
        gst_gl_get_plane_data_size (priv->gl_params->v_info,
Packit 971217
        priv->gl_params->valign, p);
Packit 971217
  }
Packit 971217
Packit 971217
  gst_buffer_pool_config_set_params (config, caps,
Packit 971217
      priv->gl_params->v_info->size, min_buffers, max_buffers);
Packit 971217
Packit 971217
  return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config) && ret;
Packit 971217
Packit 971217
  /* ERRORS */
Packit 971217
wrong_config:
Packit 971217
  {
Packit 971217
    GST_WARNING_OBJECT (pool, "invalid config");
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
no_caps:
Packit 971217
  {
Packit 971217
    GST_WARNING_OBJECT (pool, "no caps in config");
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
wrong_caps:
Packit 971217
  {
Packit 971217
    GST_WARNING_OBJECT (pool,
Packit 971217
        "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
wrong_allocator:
Packit 971217
  {
Packit 971217
    GST_WARNING_OBJECT (pool, "Incorrect allocator type for this pool");
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_gl_buffer_pool_start (GstBufferPool * pool)
Packit 971217
{
Packit 971217
  return GST_BUFFER_POOL_CLASS (parent_class)->start (pool);
Packit 971217
}
Packit 971217
Packit 971217
/* This function handles GstBuffer creation */
Packit 971217
static GstFlowReturn
Packit 971217
gst_gl_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
Packit 971217
    GstBufferPoolAcquireParams * params)
Packit 971217
{
Packit 971217
  GstGLMemoryAllocator *alloc;
Packit 971217
  GstGLBufferPool *glpool = GST_GL_BUFFER_POOL_CAST (pool);
Packit 971217
  GstGLBufferPoolPrivate *priv = glpool->priv;
Packit 971217
  GstBuffer *buf;
Packit 971217
Packit 971217
  if (!(buf = gst_buffer_new ())) {
Packit 971217
    goto no_buffer;
Packit 971217
  }
Packit 971217
Packit 971217
  alloc = GST_GL_MEMORY_ALLOCATOR (priv->allocator);
Packit 971217
  if (!gst_gl_memory_setup_buffer (alloc, buf, priv->gl_params, NULL, NULL, 0))
Packit 971217
    goto mem_create_failed;
Packit 971217
Packit 971217
  if (priv->add_glsyncmeta)
Packit 971217
    gst_buffer_add_gl_sync_meta (glpool->context, buf);
Packit 971217
Packit 971217
  *buffer = buf;
Packit 971217
Packit 971217
  return GST_FLOW_OK;
Packit 971217
Packit 971217
  /* ERROR */
Packit 971217
no_buffer:
Packit 971217
  {
Packit 971217
    GST_WARNING_OBJECT (pool, "can't create image");
Packit 971217
    return GST_FLOW_ERROR;
Packit 971217
  }
Packit 971217
mem_create_failed:
Packit 971217
  {
Packit 971217
    GST_WARNING_OBJECT (pool, "Could not create GL Memory");
Packit 971217
    return GST_FLOW_ERROR;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_gl_buffer_pool_new:
Packit 971217
 * @context: the #GstGLContext to use
Packit 971217
 *
Packit 971217
 * Returns: a #GstBufferPool that allocates buffers with #GstGLMemory
Packit 971217
 */
Packit 971217
GstBufferPool *
Packit 971217
gst_gl_buffer_pool_new (GstGLContext * context)
Packit 971217
{
Packit 971217
  GstGLBufferPool *pool;
Packit 971217
Packit 971217
  pool = g_object_new (GST_TYPE_GL_BUFFER_POOL, NULL);
Packit 971217
  gst_object_ref_sink (pool);
Packit 971217
  pool->context = gst_object_ref (context);
Packit 971217
Packit 971217
  GST_LOG_OBJECT (pool, "new GL buffer pool for context %" GST_PTR_FORMAT,
Packit 971217
      context);
Packit 971217
Packit 971217
  return GST_BUFFER_POOL_CAST (pool);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_buffer_pool_class_init (GstGLBufferPoolClass * klass)
Packit 971217
{
Packit 971217
  GObjectClass *gobject_class = (GObjectClass *) klass;
Packit 971217
  GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
Packit 971217
Packit 971217
  g_type_class_add_private (klass, sizeof (GstGLBufferPoolPrivate));
Packit 971217
Packit 971217
  gobject_class->finalize = gst_gl_buffer_pool_finalize;
Packit 971217
Packit 971217
  gstbufferpool_class->get_options = gst_gl_buffer_pool_get_options;
Packit 971217
  gstbufferpool_class->set_config = gst_gl_buffer_pool_set_config;
Packit 971217
  gstbufferpool_class->alloc_buffer = gst_gl_buffer_pool_alloc;
Packit 971217
  gstbufferpool_class->start = gst_gl_buffer_pool_start;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_buffer_pool_init (GstGLBufferPool * pool)
Packit 971217
{
Packit 971217
  GstGLBufferPoolPrivate *priv = NULL;
Packit 971217
Packit 971217
  pool->priv = GST_GL_BUFFER_POOL_GET_PRIVATE (pool);
Packit 971217
  priv = pool->priv;
Packit 971217
Packit 971217
  priv->allocator = NULL;
Packit 971217
  priv->caps = NULL;
Packit 971217
  priv->add_videometa = TRUE;
Packit 971217
  priv->add_glsyncmeta = FALSE;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_buffer_pool_finalize (GObject * object)
Packit 971217
{
Packit 971217
  GstGLBufferPool *pool = GST_GL_BUFFER_POOL_CAST (object);
Packit 971217
  GstGLBufferPoolPrivate *priv = pool->priv;
Packit 971217
Packit 971217
  GST_LOG_OBJECT (pool, "finalize GL buffer pool %p", pool);
Packit 971217
Packit 971217
  if (priv->caps)
Packit 971217
    gst_caps_unref (priv->caps);
Packit 971217
Packit 971217
  G_OBJECT_CLASS (gst_gl_buffer_pool_parent_class)->finalize (object);
Packit 971217
Packit 971217
  /* only release the context once all our memory have been deleted */
Packit 971217
  if (pool->context) {
Packit 971217
    gst_object_unref (pool->context);
Packit 971217
    pool->context = NULL;
Packit 971217
  }
Packit 971217
Packit 971217
  if (priv->allocator) {
Packit 971217
    gst_object_unref (priv->allocator);
Packit 971217
    priv->allocator = NULL;
Packit 971217
  }
Packit 971217
Packit 971217
  if (priv->gl_params)
Packit 971217
    gst_gl_allocation_params_free ((GstGLAllocationParams *) priv->gl_params);
Packit 971217
  priv->gl_params = NULL;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_buffer_pool_config_get_gl_allocation_params:
Packit 971217
 * @config: a buffer pool config
Packit 971217
 *
Packit 971217
 * Returns: (transfer full): the currently set #GstGLAllocationParams or %NULL
Packit 971217
 */
Packit 971217
GstGLAllocationParams *
Packit 971217
gst_buffer_pool_config_get_gl_allocation_params (GstStructure * config)
Packit 971217
{
Packit 971217
  GstGLAllocationParams *ret;
Packit 971217
Packit 971217
  if (!gst_structure_get (config, "gl-allocation-params",
Packit 971217
          GST_TYPE_GL_ALLOCATION_PARAMS, &ret, NULL))
Packit 971217
    ret = NULL;
Packit 971217
Packit 971217
  return ret;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_buffer_pool_config_set_gl_allocation_params:
Packit 971217
 * @config: a buffer pool config
Packit 971217
 * @params: (transfer none): a #GstGLAllocationParams
Packit 971217
 *
Packit 971217
 * Sets @params on @config
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_buffer_pool_config_set_gl_allocation_params (GstStructure * config,
Packit 971217
    GstGLAllocationParams * params)
Packit 971217
{
Packit 971217
  g_return_if_fail (config != NULL);
Packit 971217
  g_return_if_fail (params != NULL);
Packit 971217
Packit 971217
  gst_structure_set (config, "gl-allocation-params",
Packit 971217
      GST_TYPE_GL_ALLOCATION_PARAMS, params, NULL);
Packit 971217
}