Blame gst-libs/gst/video/gstvideopool.c

Packit 971217
/* GStreamer
Packit 971217
 * Copyright (C) <2011> Wim Taymans <wim.taymans@gmail.com>
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
#include "gst/video/gstvideometa.h"
Packit 971217
#include "gst/video/gstvideopool.h"
Packit 971217
Packit 971217
Packit 971217
GST_DEBUG_CATEGORY_STATIC (gst_video_pool_debug);
Packit 971217
#define GST_CAT_DEFAULT gst_video_pool_debug
Packit 971217
Packit 971217
/**
Packit 971217
 * SECTION:gstvideopool
Packit 971217
 * @title: GstVideoBufferPool
Packit 971217
 * @short_description: GstBufferPool for raw video buffers
Packit 971217
 * @see_also: #GstBufferPool
Packit 971217
 *
Packit 971217
 * Special GstBufferPool subclass for raw video buffers.
Packit 971217
 *
Packit 971217
 * Allows configuration of video-specific requirements such as
Packit 971217
 * stride alignments or pixel padding, and can also be configured
Packit 971217
 * to automatically add #GstVideoMeta to the buffers.
Packit 971217
 */
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_buffer_pool_config_set_video_alignment:
Packit 971217
 * @config: a #GstStructure
Packit 971217
 * @align: a #GstVideoAlignment
Packit 971217
 *
Packit 971217
 * Set the video alignment in @align to the bufferpool configuration
Packit 971217
 * @config
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_buffer_pool_config_set_video_alignment (GstStructure * config,
Packit 971217
    GstVideoAlignment * align)
Packit 971217
{
Packit 971217
  g_return_if_fail (config != NULL);
Packit 971217
  g_return_if_fail (align != NULL);
Packit 971217
Packit 971217
  gst_structure_set (config,
Packit 971217
      "padding-top", G_TYPE_UINT, align->padding_top,
Packit 971217
      "padding-bottom", G_TYPE_UINT, align->padding_bottom,
Packit 971217
      "padding-left", G_TYPE_UINT, align->padding_left,
Packit 971217
      "padding-right", G_TYPE_UINT, align->padding_right,
Packit 971217
      "stride-align0", G_TYPE_UINT, align->stride_align[0],
Packit 971217
      "stride-align1", G_TYPE_UINT, align->stride_align[1],
Packit 971217
      "stride-align2", G_TYPE_UINT, align->stride_align[2],
Packit 971217
      "stride-align3", G_TYPE_UINT, align->stride_align[3], NULL);
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_buffer_pool_config_get_video_alignment:
Packit 971217
 * @config: a #GstStructure
Packit 971217
 * @align: a #GstVideoAlignment
Packit 971217
 *
Packit 971217
 * Get the video alignment from the bufferpool configuration @config in
Packit 971217
 * in @align
Packit 971217
 *
Packit 971217
 * Returns: %TRUE if @config could be parsed correctly.
Packit 971217
 */
Packit 971217
gboolean
Packit 971217
gst_buffer_pool_config_get_video_alignment (GstStructure * config,
Packit 971217
    GstVideoAlignment * align)
Packit 971217
{
Packit 971217
  g_return_val_if_fail (config != NULL, FALSE);
Packit 971217
  g_return_val_if_fail (align != NULL, FALSE);
Packit 971217
Packit 971217
  return gst_structure_get (config,
Packit 971217
      "padding-top", G_TYPE_UINT, &align->padding_top,
Packit 971217
      "padding-bottom", G_TYPE_UINT, &align->padding_bottom,
Packit 971217
      "padding-left", G_TYPE_UINT, &align->padding_left,
Packit 971217
      "padding-right", G_TYPE_UINT, &align->padding_right,
Packit 971217
      "stride-align0", G_TYPE_UINT, &align->stride_align[0],
Packit 971217
      "stride-align1", G_TYPE_UINT, &align->stride_align[1],
Packit 971217
      "stride-align2", G_TYPE_UINT, &align->stride_align[2],
Packit 971217
      "stride-align3", G_TYPE_UINT, &align->stride_align[3], NULL);
Packit 971217
}
Packit 971217
Packit 971217
/* bufferpool */
Packit 971217
struct _GstVideoBufferPoolPrivate
Packit 971217
{
Packit 971217
  GstCaps *caps;
Packit 971217
  GstVideoInfo info;
Packit 971217
  GstVideoAlignment video_align;
Packit 971217
  gboolean add_videometa;
Packit 971217
  gboolean need_alignment;
Packit 971217
  GstAllocator *allocator;
Packit 971217
  GstAllocationParams params;
Packit 971217
};
Packit 971217
Packit 971217
static void gst_video_buffer_pool_finalize (GObject * object);
Packit 971217
Packit 971217
#define GST_VIDEO_BUFFER_POOL_GET_PRIVATE(obj)  \
Packit 971217
   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_VIDEO_BUFFER_POOL, GstVideoBufferPoolPrivate))
Packit 971217
Packit 971217
#define gst_video_buffer_pool_parent_class parent_class
Packit 971217
G_DEFINE_TYPE (GstVideoBufferPool, gst_video_buffer_pool, GST_TYPE_BUFFER_POOL);
Packit 971217
Packit 971217
static const gchar **
Packit 971217
video_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_VIDEO_ALIGNMENT, NULL
Packit 971217
  };
Packit 971217
  return options;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
video_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
Packit 971217
{
Packit 971217
  GstVideoBufferPool *vpool = GST_VIDEO_BUFFER_POOL_CAST (pool);
Packit 971217
  GstVideoBufferPoolPrivate *priv = vpool->priv;
Packit 971217
  GstVideoInfo info;
Packit 971217
  GstCaps *caps;
Packit 971217
  guint size, min_buffers, max_buffers;
Packit 971217
  gint width, height;
Packit 971217
  GstAllocator *allocator;
Packit 971217
  GstAllocationParams params;
Packit 971217
Packit 971217
  if (!gst_buffer_pool_config_get_params (config, &caps, &size, &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
  if (size < info.size)
Packit 971217
    goto wrong_size;
Packit 971217
Packit 971217
  if (!gst_buffer_pool_config_get_allocator (config, &allocator, &params))
Packit 971217
    goto wrong_config;
Packit 971217
Packit 971217
  width = info.width;
Packit 971217
  height = info.height;
Packit 971217
Packit 971217
  GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, width, height, caps);
Packit 971217
Packit 971217
  if (priv->caps)
Packit 971217
    gst_caps_unref (priv->caps);
Packit 971217
  priv->caps = gst_caps_ref (caps);
Packit 971217
Packit 971217
  priv->params = params;
Packit 971217
  if (priv->allocator)
Packit 971217
    gst_object_unref (priv->allocator);
Packit 971217
  if ((priv->allocator = allocator))
Packit 971217
    gst_object_ref (allocator);
Packit 971217
Packit 971217
  /* enable metadata based on config of the pool */
Packit 971217
  priv->add_videometa =
Packit 971217
      gst_buffer_pool_config_has_option (config,
Packit 971217
      GST_BUFFER_POOL_OPTION_VIDEO_META);
Packit 971217
Packit 971217
  /* parse extra alignment info */
Packit 971217
  priv->need_alignment = gst_buffer_pool_config_has_option (config,
Packit 971217
      GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
Packit 971217
Packit 971217
  if (priv->need_alignment && priv->add_videometa) {
Packit 971217
    guint max_align, n;
Packit 971217
Packit 971217
    gst_buffer_pool_config_get_video_alignment (config, &priv->video_align);
Packit 971217
Packit 971217
    /* ensure GstAllocationParams alignment is compatible with video alignment */
Packit 971217
    max_align = priv->params.align;
Packit 971217
    for (n = 0; n < GST_VIDEO_MAX_PLANES; ++n)
Packit 971217
      max_align |= priv->video_align.stride_align[n];
Packit 971217
Packit 971217
    for (n = 0; n < GST_VIDEO_MAX_PLANES; ++n)
Packit 971217
      priv->video_align.stride_align[n] = max_align;
Packit 971217
Packit 971217
    /* apply the alignment to the info */
Packit 971217
    if (!gst_video_info_align (&info, &priv->video_align))
Packit 971217
      goto failed_to_align;
Packit 971217
Packit 971217
    gst_buffer_pool_config_set_video_alignment (config, &priv->video_align);
Packit 971217
Packit 971217
    if (priv->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) priv->params.align, max_align);
Packit 971217
      priv->params.align = max_align;
Packit 971217
      gst_buffer_pool_config_set_allocator (config, allocator, &priv->params);
Packit 971217
    }
Packit 971217
  }
Packit 971217
  info.size = MAX (size, info.size);
Packit 971217
  priv->info = info;
Packit 971217
Packit 971217
  gst_buffer_pool_config_set_params (config, caps, info.size, min_buffers,
Packit 971217
      max_buffers);
Packit 971217
Packit 971217
  return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
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_size:
Packit 971217
  {
Packit 971217
    GST_WARNING_OBJECT (pool,
Packit 971217
        "Provided size is to small for the caps: %u", size);
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
failed_to_align:
Packit 971217
  {
Packit 971217
    GST_WARNING_OBJECT (pool, "Failed to align");
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static GstFlowReturn
Packit 971217
video_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
Packit 971217
    GstBufferPoolAcquireParams * params)
Packit 971217
{
Packit 971217
  GstVideoBufferPool *vpool = GST_VIDEO_BUFFER_POOL_CAST (pool);
Packit 971217
  GstVideoBufferPoolPrivate *priv = vpool->priv;
Packit 971217
  GstVideoInfo *info;
Packit 971217
Packit 971217
  info = &priv->info;
Packit 971217
Packit 971217
  GST_DEBUG_OBJECT (pool, "alloc %" G_GSIZE_FORMAT, info->size);
Packit 971217
Packit 971217
  *buffer =
Packit 971217
      gst_buffer_new_allocate (priv->allocator, info->size, &priv->params);
Packit 971217
  if (*buffer == NULL)
Packit 971217
    goto no_memory;
Packit 971217
Packit 971217
  if (priv->add_videometa) {
Packit 971217
    GST_DEBUG_OBJECT (pool, "adding GstVideoMeta");
Packit 971217
Packit 971217
    gst_buffer_add_video_meta_full (*buffer, GST_VIDEO_FRAME_FLAG_NONE,
Packit 971217
        GST_VIDEO_INFO_FORMAT (info),
Packit 971217
        GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info),
Packit 971217
        GST_VIDEO_INFO_N_PLANES (info), info->offset, info->stride);
Packit 971217
  }
Packit 971217
Packit 971217
  return GST_FLOW_OK;
Packit 971217
Packit 971217
  /* ERROR */
Packit 971217
no_memory:
Packit 971217
  {
Packit 971217
    GST_WARNING_OBJECT (pool, "can't create memory");
Packit 971217
    return GST_FLOW_ERROR;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_buffer_pool_new:
Packit 971217
 *
Packit 971217
 * Create a new bufferpool that can allocate video frames. This bufferpool
Packit 971217
 * supports all the video bufferpool options.
Packit 971217
 *
Packit 971217
 * Returns: (transfer full): a new #GstBufferPool to allocate video frames
Packit 971217
 */
Packit 971217
GstBufferPool *
Packit 971217
gst_video_buffer_pool_new ()
Packit 971217
{
Packit 971217
  GstVideoBufferPool *pool;
Packit 971217
Packit 971217
  pool = g_object_new (GST_TYPE_VIDEO_BUFFER_POOL, NULL);
Packit 971217
  gst_object_ref_sink (pool);
Packit 971217
Packit 971217
  GST_LOG_OBJECT (pool, "new video buffer pool %p", pool);
Packit 971217
Packit 971217
  return GST_BUFFER_POOL_CAST (pool);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_video_buffer_pool_class_init (GstVideoBufferPoolClass * 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 (GstVideoBufferPoolPrivate));
Packit 971217
Packit 971217
  gobject_class->finalize = gst_video_buffer_pool_finalize;
Packit 971217
Packit 971217
  gstbufferpool_class->get_options = video_buffer_pool_get_options;
Packit 971217
  gstbufferpool_class->set_config = video_buffer_pool_set_config;
Packit 971217
  gstbufferpool_class->alloc_buffer = video_buffer_pool_alloc;
Packit 971217
Packit 971217
  GST_DEBUG_CATEGORY_INIT (gst_video_pool_debug, "videopool", 0,
Packit 971217
      "videopool object");
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_video_buffer_pool_init (GstVideoBufferPool * pool)
Packit 971217
{
Packit 971217
  pool->priv = GST_VIDEO_BUFFER_POOL_GET_PRIVATE (pool);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_video_buffer_pool_finalize (GObject * object)
Packit 971217
{
Packit 971217
  GstVideoBufferPool *pool = GST_VIDEO_BUFFER_POOL_CAST (object);
Packit 971217
  GstVideoBufferPoolPrivate *priv = pool->priv;
Packit 971217
Packit 971217
  GST_LOG_OBJECT (pool, "finalize video buffer pool %p", pool);
Packit 971217
Packit 971217
  if (priv->caps)
Packit 971217
    gst_caps_unref (priv->caps);
Packit 971217
Packit 971217
  if (priv->allocator)
Packit 971217
    gst_object_unref (priv->allocator);
Packit 971217
Packit 971217
  G_OBJECT_CLASS (gst_video_buffer_pool_parent_class)->finalize (object);
Packit 971217
}