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

Packit 0652a1
/* GStreamer
Packit 0652a1
 * Copyright (C) <2011> Wim Taymans <wim.taymans@gmail.com>
Packit 0652a1
 *
Packit 0652a1
 * This library is free software; you can redistribute it and/or
Packit 0652a1
 * modify it under the terms of the GNU Library General Public
Packit 0652a1
 * License as published by the Free Software Foundation; either
Packit 0652a1
 * version 2 of the License, or (at your option) any later version.
Packit 0652a1
 *
Packit 0652a1
 * This library is distributed in the hope that it will be useful,
Packit 0652a1
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0652a1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0652a1
 * Library General Public License for more details.
Packit 0652a1
 *
Packit 0652a1
 * You should have received a copy of the GNU Library General Public
Packit 0652a1
 * License along with this library; if not, write to the
Packit 0652a1
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 0652a1
 * Boston, MA 02110-1301, USA.
Packit 0652a1
 */
Packit 0652a1
#ifdef HAVE_CONFIG_H
Packit 0652a1
#include "config.h"
Packit 0652a1
#endif
Packit 0652a1
Packit 0652a1
#include "gst/video/gstvideometa.h"
Packit 0652a1
#include "gst/video/gstvideopool.h"
Packit 0652a1
Packit 0652a1
Packit 0652a1
GST_DEBUG_CATEGORY_STATIC (gst_video_pool_debug);
Packit 0652a1
#define GST_CAT_DEFAULT gst_video_pool_debug
Packit 0652a1
Packit 0652a1
/**
Packit 0652a1
 * SECTION:gstvideopool
Packit 0652a1
 * @title: GstVideoBufferPool
Packit 0652a1
 * @short_description: GstBufferPool for raw video buffers
Packit 0652a1
 * @see_also: #GstBufferPool
Packit 0652a1
 *
Packit 0652a1
 * Special GstBufferPool subclass for raw video buffers.
Packit 0652a1
 *
Packit 0652a1
 * Allows configuration of video-specific requirements such as
Packit 0652a1
 * stride alignments or pixel padding, and can also be configured
Packit 0652a1
 * to automatically add #GstVideoMeta to the buffers.
Packit 0652a1
 */
Packit 0652a1
Packit 0652a1
/**
Packit 0652a1
 * gst_buffer_pool_config_set_video_alignment:
Packit 0652a1
 * @config: a #GstStructure
Packit 0652a1
 * @align: a #GstVideoAlignment
Packit 0652a1
 *
Packit 0652a1
 * Set the video alignment in @align to the bufferpool configuration
Packit 0652a1
 * @config
Packit 0652a1
 */
Packit 0652a1
void
Packit 0652a1
gst_buffer_pool_config_set_video_alignment (GstStructure * config,
Packit 0652a1
    GstVideoAlignment * align)
Packit 0652a1
{
Packit 0652a1
  g_return_if_fail (config != NULL);
Packit 0652a1
  g_return_if_fail (align != NULL);
Packit 0652a1
Packit 0652a1
  gst_structure_set (config,
Packit 0652a1
      "padding-top", G_TYPE_UINT, align->padding_top,
Packit 0652a1
      "padding-bottom", G_TYPE_UINT, align->padding_bottom,
Packit 0652a1
      "padding-left", G_TYPE_UINT, align->padding_left,
Packit 0652a1
      "padding-right", G_TYPE_UINT, align->padding_right,
Packit 0652a1
      "stride-align0", G_TYPE_UINT, align->stride_align[0],
Packit 0652a1
      "stride-align1", G_TYPE_UINT, align->stride_align[1],
Packit 0652a1
      "stride-align2", G_TYPE_UINT, align->stride_align[2],
Packit 0652a1
      "stride-align3", G_TYPE_UINT, align->stride_align[3], NULL);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
/**
Packit 0652a1
 * gst_buffer_pool_config_get_video_alignment:
Packit 0652a1
 * @config: a #GstStructure
Packit 0652a1
 * @align: a #GstVideoAlignment
Packit 0652a1
 *
Packit 0652a1
 * Get the video alignment from the bufferpool configuration @config in
Packit 0652a1
 * in @align
Packit 0652a1
 *
Packit 0652a1
 * Returns: %TRUE if @config could be parsed correctly.
Packit 0652a1
 */
Packit 0652a1
gboolean
Packit 0652a1
gst_buffer_pool_config_get_video_alignment (GstStructure * config,
Packit 0652a1
    GstVideoAlignment * align)
Packit 0652a1
{
Packit 0652a1
  g_return_val_if_fail (config != NULL, FALSE);
Packit 0652a1
  g_return_val_if_fail (align != NULL, FALSE);
Packit 0652a1
Packit 0652a1
  return gst_structure_get (config,
Packit 0652a1
      "padding-top", G_TYPE_UINT, &align->padding_top,
Packit 0652a1
      "padding-bottom", G_TYPE_UINT, &align->padding_bottom,
Packit 0652a1
      "padding-left", G_TYPE_UINT, &align->padding_left,
Packit 0652a1
      "padding-right", G_TYPE_UINT, &align->padding_right,
Packit 0652a1
      "stride-align0", G_TYPE_UINT, &align->stride_align[0],
Packit 0652a1
      "stride-align1", G_TYPE_UINT, &align->stride_align[1],
Packit 0652a1
      "stride-align2", G_TYPE_UINT, &align->stride_align[2],
Packit 0652a1
      "stride-align3", G_TYPE_UINT, &align->stride_align[3], NULL);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
/* bufferpool */
Packit 0652a1
struct _GstVideoBufferPoolPrivate
Packit 0652a1
{
Packit 0652a1
  GstVideoInfo info;
Packit 0652a1
  GstVideoAlignment video_align;
Packit 0652a1
  gboolean add_videometa;
Packit 0652a1
  gboolean need_alignment;
Packit 0652a1
  GstAllocator *allocator;
Packit 0652a1
  GstAllocationParams params;
Packit 0652a1
};
Packit 0652a1
Packit 0652a1
static void gst_video_buffer_pool_finalize (GObject * object);
Packit 0652a1
Packit 0652a1
#define gst_video_buffer_pool_parent_class parent_class
Packit 0652a1
G_DEFINE_TYPE_WITH_PRIVATE (GstVideoBufferPool, gst_video_buffer_pool,
Packit 0652a1
    GST_TYPE_BUFFER_POOL);
Packit 0652a1
Packit 0652a1
static const gchar **
Packit 0652a1
video_buffer_pool_get_options (GstBufferPool * pool)
Packit 0652a1
{
Packit 0652a1
  static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
Packit 0652a1
    GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT, NULL
Packit 0652a1
  };
Packit 0652a1
  return options;
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static gboolean
Packit 0652a1
video_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
Packit 0652a1
{
Packit 0652a1
  GstVideoBufferPool *vpool = GST_VIDEO_BUFFER_POOL_CAST (pool);
Packit 0652a1
  GstVideoBufferPoolPrivate *priv = vpool->priv;
Packit 0652a1
  GstVideoInfo info;
Packit 0652a1
  GstCaps *caps;
Packit 0652a1
  guint size, min_buffers, max_buffers;
Packit 0652a1
  gint width, height;
Packit 0652a1
  GstAllocator *allocator;
Packit 0652a1
  GstAllocationParams params;
Packit 0652a1
Packit 0652a1
  if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
Packit 0652a1
          &max_buffers))
Packit 0652a1
    goto wrong_config;
Packit 0652a1
Packit 0652a1
  if (caps == NULL)
Packit 0652a1
    goto no_caps;
Packit 0652a1
Packit 0652a1
  /* now parse the caps from the config */
Packit 0652a1
  if (!gst_video_info_from_caps (&info, caps))
Packit 0652a1
    goto wrong_caps;
Packit 0652a1
Packit 0652a1
  if (size < info.size)
Packit 0652a1
    goto wrong_size;
Packit 0652a1
Packit 0652a1
  if (!gst_buffer_pool_config_get_allocator (config, &allocator, &params))
Packit 0652a1
    goto wrong_config;
Packit 0652a1
Packit 0652a1
  width = info.width;
Packit 0652a1
  height = info.height;
Packit 0652a1
Packit 0652a1
  GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, width, height, caps);
Packit 0652a1
Packit 0652a1
  priv->params = params;
Packit 0652a1
  if (priv->allocator)
Packit 0652a1
    gst_object_unref (priv->allocator);
Packit 0652a1
  if ((priv->allocator = allocator))
Packit 0652a1
    gst_object_ref (allocator);
Packit 0652a1
Packit 0652a1
  /* enable metadata based on config of the pool */
Packit 0652a1
  priv->add_videometa =
Packit 0652a1
      gst_buffer_pool_config_has_option (config,
Packit 0652a1
      GST_BUFFER_POOL_OPTION_VIDEO_META);
Packit 0652a1
Packit 0652a1
  /* parse extra alignment info */
Packit 0652a1
  priv->need_alignment = gst_buffer_pool_config_has_option (config,
Packit 0652a1
      GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
Packit 0652a1
Packit 0652a1
  if (priv->need_alignment && priv->add_videometa) {
Packit 0652a1
    guint max_align, n;
Packit 0652a1
Packit 0652a1
    gst_buffer_pool_config_get_video_alignment (config, &priv->video_align);
Packit 0652a1
Packit 0652a1
    /* ensure GstAllocationParams alignment is compatible with video alignment */
Packit 0652a1
    max_align = priv->params.align;
Packit 0652a1
    for (n = 0; n < GST_VIDEO_MAX_PLANES; ++n)
Packit 0652a1
      max_align |= priv->video_align.stride_align[n];
Packit 0652a1
Packit 0652a1
    for (n = 0; n < GST_VIDEO_MAX_PLANES; ++n)
Packit 0652a1
      priv->video_align.stride_align[n] = max_align;
Packit 0652a1
Packit 0652a1
    /* apply the alignment to the info */
Packit 0652a1
    if (!gst_video_info_align (&info, &priv->video_align))
Packit 0652a1
      goto failed_to_align;
Packit 0652a1
Packit 0652a1
    gst_buffer_pool_config_set_video_alignment (config, &priv->video_align);
Packit 0652a1
Packit 0652a1
    if (priv->params.align < max_align) {
Packit 0652a1
      GST_WARNING_OBJECT (pool, "allocation params alignment %u is smaller "
Packit 0652a1
          "than the max specified video stride alignment %u, fixing",
Packit 0652a1
          (guint) priv->params.align, max_align);
Packit 0652a1
      priv->params.align = max_align;
Packit 0652a1
      gst_buffer_pool_config_set_allocator (config, allocator, &priv->params);
Packit 0652a1
    }
Packit 0652a1
  }
Packit 0652a1
  info.size = MAX (size, info.size);
Packit 0652a1
  priv->info = info;
Packit 0652a1
Packit 0652a1
  gst_buffer_pool_config_set_params (config, caps, info.size, min_buffers,
Packit 0652a1
      max_buffers);
Packit 0652a1
Packit 0652a1
  return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
Packit 0652a1
Packit 0652a1
  /* ERRORS */
Packit 0652a1
wrong_config:
Packit 0652a1
  {
Packit 0652a1
    GST_WARNING_OBJECT (pool, "invalid config");
Packit 0652a1
    return FALSE;
Packit 0652a1
  }
Packit 0652a1
no_caps:
Packit 0652a1
  {
Packit 0652a1
    GST_WARNING_OBJECT (pool, "no caps in config");
Packit 0652a1
    return FALSE;
Packit 0652a1
  }
Packit 0652a1
wrong_caps:
Packit 0652a1
  {
Packit 0652a1
    GST_WARNING_OBJECT (pool,
Packit 0652a1
        "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
Packit 0652a1
    return FALSE;
Packit 0652a1
  }
Packit 0652a1
wrong_size:
Packit 0652a1
  {
Packit 0652a1
    GST_WARNING_OBJECT (pool,
Packit 0652a1
        "Provided size is to small for the caps: %u < %" G_GSIZE_FORMAT, size,
Packit 0652a1
        info.size);
Packit 0652a1
    return FALSE;
Packit 0652a1
  }
Packit 0652a1
failed_to_align:
Packit 0652a1
  {
Packit 0652a1
    GST_WARNING_OBJECT (pool, "Failed to align");
Packit 0652a1
    return FALSE;
Packit 0652a1
  }
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static GstFlowReturn
Packit 0652a1
video_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
Packit 0652a1
    GstBufferPoolAcquireParams * params)
Packit 0652a1
{
Packit 0652a1
  GstVideoBufferPool *vpool = GST_VIDEO_BUFFER_POOL_CAST (pool);
Packit 0652a1
  GstVideoBufferPoolPrivate *priv = vpool->priv;
Packit 0652a1
  GstVideoInfo *info;
Packit 0652a1
Packit 0652a1
  info = &priv->info;
Packit 0652a1
Packit 0652a1
  GST_DEBUG_OBJECT (pool, "alloc %" G_GSIZE_FORMAT, info->size);
Packit 0652a1
Packit 0652a1
  *buffer =
Packit 0652a1
      gst_buffer_new_allocate (priv->allocator, info->size, &priv->params);
Packit 0652a1
  if (*buffer == NULL)
Packit 0652a1
    goto no_memory;
Packit 0652a1
Packit 0652a1
  if (priv->add_videometa) {
Packit 0652a1
    GST_DEBUG_OBJECT (pool, "adding GstVideoMeta");
Packit 0652a1
Packit 0652a1
    gst_buffer_add_video_meta_full (*buffer, GST_VIDEO_FRAME_FLAG_NONE,
Packit 0652a1
        GST_VIDEO_INFO_FORMAT (info),
Packit 0652a1
        GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info),
Packit 0652a1
        GST_VIDEO_INFO_N_PLANES (info), info->offset, info->stride);
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  return GST_FLOW_OK;
Packit 0652a1
Packit 0652a1
  /* ERROR */
Packit 0652a1
no_memory:
Packit 0652a1
  {
Packit 0652a1
    GST_WARNING_OBJECT (pool, "can't create memory");
Packit 0652a1
    return GST_FLOW_ERROR;
Packit 0652a1
  }
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
/**
Packit 0652a1
 * gst_video_buffer_pool_new:
Packit 0652a1
 *
Packit 0652a1
 * Create a new bufferpool that can allocate video frames. This bufferpool
Packit 0652a1
 * supports all the video bufferpool options.
Packit 0652a1
 *
Packit 0652a1
 * Returns: (transfer full): a new #GstBufferPool to allocate video frames
Packit 0652a1
 */
Packit 0652a1
GstBufferPool *
Packit 0652a1
gst_video_buffer_pool_new ()
Packit 0652a1
{
Packit 0652a1
  GstVideoBufferPool *pool;
Packit 0652a1
Packit 0652a1
  pool = g_object_new (GST_TYPE_VIDEO_BUFFER_POOL, NULL);
Packit 0652a1
  gst_object_ref_sink (pool);
Packit 0652a1
Packit 0652a1
  GST_LOG_OBJECT (pool, "new video buffer pool %p", pool);
Packit 0652a1
Packit 0652a1
  return GST_BUFFER_POOL_CAST (pool);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_video_buffer_pool_class_init (GstVideoBufferPoolClass * klass)
Packit 0652a1
{
Packit 0652a1
  GObjectClass *gobject_class = (GObjectClass *) klass;
Packit 0652a1
  GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
Packit 0652a1
Packit 0652a1
  gobject_class->finalize = gst_video_buffer_pool_finalize;
Packit 0652a1
Packit 0652a1
  gstbufferpool_class->get_options = video_buffer_pool_get_options;
Packit 0652a1
  gstbufferpool_class->set_config = video_buffer_pool_set_config;
Packit 0652a1
  gstbufferpool_class->alloc_buffer = video_buffer_pool_alloc;
Packit 0652a1
Packit 0652a1
  GST_DEBUG_CATEGORY_INIT (gst_video_pool_debug, "videopool", 0,
Packit 0652a1
      "videopool object");
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_video_buffer_pool_init (GstVideoBufferPool * pool)
Packit 0652a1
{
Packit 0652a1
  pool->priv = gst_video_buffer_pool_get_instance_private (pool);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_video_buffer_pool_finalize (GObject * object)
Packit 0652a1
{
Packit 0652a1
  GstVideoBufferPool *pool = GST_VIDEO_BUFFER_POOL_CAST (object);
Packit 0652a1
  GstVideoBufferPoolPrivate *priv = pool->priv;
Packit 0652a1
Packit 0652a1
  GST_LOG_OBJECT (pool, "finalize video buffer pool %p", pool);
Packit 0652a1
Packit 0652a1
  if (priv->allocator)
Packit 0652a1
    gst_object_unref (priv->allocator);
Packit 0652a1
Packit 0652a1
  G_OBJECT_CLASS (gst_video_buffer_pool_parent_class)->finalize (object);
Packit 0652a1
}