Blame sys/xvimage/xvimagepool.c

Packit 971217
/* GStreamer
Packit 971217
 * Copyright (C) <2005> Julien Moutte <julien@moutte.net>
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
/* Object header */
Packit 971217
#include "xvimagepool.h"
Packit 971217
#include "xvimageallocator.h"
Packit 971217
Packit 971217
/* Debugging category */
Packit 971217
#include <gst/gstinfo.h>
Packit 971217
Packit 971217
/* Helper functions */
Packit 971217
#include <gst/video/video.h>
Packit 971217
#include <gst/video/gstvideometa.h>
Packit 971217
#include <gst/video/gstvideopool.h>
Packit 971217
Packit 971217
Packit 971217
GST_DEBUG_CATEGORY_EXTERN (gst_debug_xv_image_pool);
Packit 971217
#define GST_CAT_DEFAULT gst_debug_xv_image_pool
Packit 971217
Packit 971217
/* bufferpool */
Packit 971217
static void gst_xvimage_buffer_pool_finalize (GObject * object);
Packit 971217
Packit 971217
#define gst_xvimage_buffer_pool_parent_class parent_class
Packit 971217
G_DEFINE_TYPE (GstXvImageBufferPool, gst_xvimage_buffer_pool,
Packit 971217
    GST_TYPE_BUFFER_POOL);
Packit 971217
Packit 971217
static const gchar **
Packit 971217
xvimage_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
Packit 971217
  return options;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
xvimage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
Packit 971217
{
Packit 971217
  GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
Packit 971217
  GstVideoInfo info;
Packit 971217
  GstCaps *caps;
Packit 971217
  guint size, min_buffers, max_buffers;
Packit 971217
  GstXvContext *context;
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
  GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
Packit 971217
      caps);
Packit 971217
Packit 971217
  context = gst_xvimage_allocator_peek_context (xvpool->allocator);
Packit 971217
Packit 971217
  xvpool->im_format = gst_xvcontext_get_format_from_info (context, &info;;
Packit 971217
  if (xvpool->im_format == -1)
Packit 971217
    goto unknown_format;
Packit 971217
Packit 971217
  if (xvpool->caps)
Packit 971217
    gst_caps_unref (xvpool->caps);
Packit 971217
  xvpool->caps = gst_caps_ref (caps);
Packit 971217
Packit 971217
  /* enable metadata based on config of the pool */
Packit 971217
  xvpool->add_metavideo =
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
  xvpool->need_alignment = gst_buffer_pool_config_has_option (config,
Packit 971217
      GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
Packit 971217
Packit 971217
  if (xvpool->need_alignment) {
Packit 971217
    gst_buffer_pool_config_get_video_alignment (config, &xvpool->align);
Packit 971217
Packit 971217
    GST_LOG_OBJECT (pool, "padding %u-%ux%u-%u", xvpool->align.padding_top,
Packit 971217
        xvpool->align.padding_left, xvpool->align.padding_left,
Packit 971217
        xvpool->align.padding_bottom);
Packit 971217
Packit 971217
    /* do padding and alignment */
Packit 971217
    gst_video_info_align (&info, &xvpool->align);
Packit 971217
Packit 971217
    gst_buffer_pool_config_set_video_alignment (config, &xvpool->align);
Packit 971217
Packit 971217
    /* we need the video metadata too now */
Packit 971217
    xvpool->add_metavideo = TRUE;
Packit 971217
  } else {
Packit 971217
    gst_video_alignment_reset (&xvpool->align);
Packit 971217
  }
Packit 971217
Packit 971217
  /* add the padding */
Packit 971217
  xvpool->padded_width =
Packit 971217
      GST_VIDEO_INFO_WIDTH (&info) + xvpool->align.padding_left +
Packit 971217
      xvpool->align.padding_right;
Packit 971217
  xvpool->padded_height =
Packit 971217
      GST_VIDEO_INFO_HEIGHT (&info) + xvpool->align.padding_top +
Packit 971217
      xvpool->align.padding_bottom;
Packit 971217
Packit 971217
  xvpool->info = info;
Packit 971217
  xvpool->crop.x = xvpool->align.padding_left;
Packit 971217
  xvpool->crop.y = xvpool->align.padding_top;
Packit 971217
  xvpool->crop.w = xvpool->info.width;
Packit 971217
  xvpool->crop.h = xvpool->info.height;
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
unknown_format:
Packit 971217
  {
Packit 971217
    GST_WARNING_OBJECT (pool, "failed to get format from caps %"
Packit 971217
        GST_PTR_FORMAT, caps);
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
/* This function handles GstXImageBuffer creation depending on XShm availability */
Packit 971217
static GstFlowReturn
Packit 971217
xvimage_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
Packit 971217
    GstBufferPoolAcquireParams * params)
Packit 971217
{
Packit 971217
  GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
Packit 971217
  GstVideoInfo *info;
Packit 971217
  GstBuffer *xvimage;
Packit 971217
  GstMemory *mem;
Packit 971217
  GError *err = NULL;
Packit 971217
Packit 971217
  info = &xvpool->info;
Packit 971217
Packit 971217
  xvimage = gst_buffer_new ();
Packit 971217
Packit 971217
  mem = gst_xvimage_allocator_alloc (xvpool->allocator, xvpool->im_format,
Packit 971217
      info, xvpool->padded_width, xvpool->padded_height, &xvpool->crop, &err;;
Packit 971217
Packit 971217
  if (mem == NULL) {
Packit 971217
    gst_buffer_unref (xvimage);
Packit 971217
    goto no_buffer;
Packit 971217
  }
Packit 971217
  gst_buffer_append_memory (xvimage, mem);
Packit 971217
Packit 971217
  if (xvpool->add_metavideo) {
Packit 971217
    GST_DEBUG_OBJECT (pool, "adding GstVideoMeta");
Packit 971217
    gst_buffer_add_video_meta_full (xvimage, GST_VIDEO_FRAME_FLAG_NONE,
Packit 971217
        GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
Packit 971217
        GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
Packit 971217
        info->offset, info->stride);
Packit 971217
  }
Packit 971217
Packit 971217
  *buffer = xvimage;
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: %s", err->message);
Packit 971217
    g_clear_error (&err;;
Packit 971217
    return GST_FLOW_ERROR;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
GstBufferPool *
Packit 971217
gst_xvimage_buffer_pool_new (GstXvImageAllocator * allocator)
Packit 971217
{
Packit 971217
  GstXvImageBufferPool *pool;
Packit 971217
Packit 971217
  pool = g_object_new (GST_TYPE_XVIMAGE_BUFFER_POOL, NULL);
Packit 971217
  gst_object_ref_sink (pool);
Packit 971217
  pool->allocator = gst_object_ref (allocator);
Packit 971217
Packit 971217
  GST_LOG_OBJECT (pool, "new XvImage 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_xvimage_buffer_pool_class_init (GstXvImageBufferPoolClass * klass)
Packit 971217
{
Packit 971217
  GObjectClass *gobject_class = (GObjectClass *) klass;
Packit 971217
  GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
Packit 971217
Packit 971217
  gobject_class->finalize = gst_xvimage_buffer_pool_finalize;
Packit 971217
Packit 971217
  gstbufferpool_class->get_options = xvimage_buffer_pool_get_options;
Packit 971217
  gstbufferpool_class->set_config = xvimage_buffer_pool_set_config;
Packit 971217
  gstbufferpool_class->alloc_buffer = xvimage_buffer_pool_alloc;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_xvimage_buffer_pool_init (GstXvImageBufferPool * pool)
Packit 971217
{
Packit 971217
  /* nothing to do here */
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_xvimage_buffer_pool_finalize (GObject * object)
Packit 971217
{
Packit 971217
  GstXvImageBufferPool *pool = GST_XVIMAGE_BUFFER_POOL_CAST (object);
Packit 971217
Packit 971217
  GST_LOG_OBJECT (pool, "finalize XvImage buffer pool %p", pool);
Packit 971217
Packit 971217
  if (pool->caps)
Packit 971217
    gst_caps_unref (pool->caps);
Packit 971217
  if (pool->allocator)
Packit 971217
    gst_object_unref (pool->allocator);
Packit 971217
Packit 971217
  G_OBJECT_CLASS (gst_xvimage_buffer_pool_parent_class)->finalize (object);
Packit 971217
}