2011-04-06 10:26:47 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) <2005> Julien Moutte <julien@moutte.net>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 23:05:09 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2011-04-06 10:26:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Object header */
|
2013-03-13 10:13:58 +00:00
|
|
|
#include "xvimagepool.h"
|
|
|
|
#include "xvimageallocator.h"
|
2011-04-06 10:26:47 +00:00
|
|
|
|
|
|
|
/* Debugging category */
|
|
|
|
#include <gst/gstinfo.h>
|
|
|
|
|
2011-06-17 15:54:52 +00:00
|
|
|
/* Helper functions */
|
|
|
|
#include <gst/video/video.h>
|
2011-10-31 01:23:21 +00:00
|
|
|
#include <gst/video/gstvideometa.h>
|
2011-07-29 15:15:39 +00:00
|
|
|
#include <gst/video/gstvideopool.h>
|
|
|
|
|
2011-06-17 15:54:52 +00:00
|
|
|
|
2021-03-29 12:10:12 +00:00
|
|
|
GST_DEBUG_CATEGORY (gst_debug_xv_image_pool);
|
2015-07-03 19:48:52 +00:00
|
|
|
#define GST_CAT_DEFAULT gst_debug_xv_image_pool
|
2011-04-06 10:26:47 +00:00
|
|
|
|
|
|
|
/* bufferpool */
|
|
|
|
static void gst_xvimage_buffer_pool_finalize (GObject * object);
|
|
|
|
|
2011-07-18 16:46:01 +00:00
|
|
|
#define gst_xvimage_buffer_pool_parent_class parent_class
|
2011-04-06 10:26:47 +00:00
|
|
|
G_DEFINE_TYPE (GstXvImageBufferPool, gst_xvimage_buffer_pool,
|
|
|
|
GST_TYPE_BUFFER_POOL);
|
|
|
|
|
2011-06-20 11:28:12 +00:00
|
|
|
static const gchar **
|
2011-07-29 15:15:39 +00:00
|
|
|
xvimage_buffer_pool_get_options (GstBufferPool * pool)
|
2011-06-20 11:28:12 +00:00
|
|
|
{
|
2011-10-31 01:23:21 +00:00
|
|
|
static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
|
2011-08-01 15:10:10 +00:00
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT, NULL
|
|
|
|
};
|
2011-06-20 11:28:12 +00:00
|
|
|
|
2011-07-29 15:15:39 +00:00
|
|
|
return options;
|
2011-06-20 11:28:12 +00:00
|
|
|
}
|
|
|
|
|
2011-04-06 10:26:47 +00:00
|
|
|
static gboolean
|
|
|
|
xvimage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
|
|
|
|
{
|
|
|
|
GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
|
2011-06-17 15:54:52 +00:00
|
|
|
GstVideoInfo info;
|
2012-03-27 10:44:02 +00:00
|
|
|
GstCaps *caps;
|
2014-12-16 11:10:53 +00:00
|
|
|
guint size, min_buffers, max_buffers;
|
2013-03-13 10:13:58 +00:00
|
|
|
GstXvContext *context;
|
2011-04-06 10:26:47 +00:00
|
|
|
|
2014-12-16 11:10:53 +00:00
|
|
|
if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
|
|
|
|
&max_buffers))
|
2011-04-06 10:26:47 +00:00
|
|
|
goto wrong_config;
|
|
|
|
|
|
|
|
if (caps == NULL)
|
|
|
|
goto no_caps;
|
|
|
|
|
|
|
|
/* now parse the caps from the config */
|
2011-06-17 15:54:52 +00:00
|
|
|
if (!gst_video_info_from_caps (&info, caps))
|
2011-04-06 10:26:47 +00:00
|
|
|
goto wrong_caps;
|
|
|
|
|
2011-06-17 15:54:52 +00:00
|
|
|
GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
|
|
|
|
caps);
|
2011-04-06 10:26:47 +00:00
|
|
|
|
2014-09-27 14:58:51 +00:00
|
|
|
context = gst_xvimage_allocator_peek_context (xvpool->allocator);
|
2013-03-13 10:13:58 +00:00
|
|
|
|
2014-09-27 14:58:51 +00:00
|
|
|
xvpool->im_format = gst_xvcontext_get_format_from_info (context, &info);
|
|
|
|
if (xvpool->im_format == -1)
|
2011-08-05 10:48:07 +00:00
|
|
|
goto unknown_format;
|
|
|
|
|
2014-09-27 14:58:51 +00:00
|
|
|
if (xvpool->caps)
|
|
|
|
gst_caps_unref (xvpool->caps);
|
|
|
|
xvpool->caps = gst_caps_ref (caps);
|
2011-06-20 11:28:12 +00:00
|
|
|
|
|
|
|
/* enable metadata based on config of the pool */
|
2014-09-27 14:58:51 +00:00
|
|
|
xvpool->add_metavideo =
|
2011-07-29 15:15:39 +00:00
|
|
|
gst_buffer_pool_config_has_option (config,
|
2011-10-31 01:23:21 +00:00
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_META);
|
2011-06-20 11:28:12 +00:00
|
|
|
|
2011-08-01 15:10:10 +00:00
|
|
|
/* parse extra alignment info */
|
2014-09-27 14:58:51 +00:00
|
|
|
xvpool->need_alignment = gst_buffer_pool_config_has_option (config,
|
2011-08-01 15:10:10 +00:00
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
|
|
|
|
|
2014-09-27 14:58:51 +00:00
|
|
|
if (xvpool->need_alignment) {
|
|
|
|
gst_buffer_pool_config_get_video_alignment (config, &xvpool->align);
|
2011-08-01 15:10:10 +00:00
|
|
|
|
2014-09-27 14:58:51 +00:00
|
|
|
GST_LOG_OBJECT (pool, "padding %u-%ux%u-%u", xvpool->align.padding_top,
|
2019-11-08 08:33:17 +00:00
|
|
|
xvpool->align.padding_left, xvpool->align.padding_right,
|
2014-09-27 14:58:51 +00:00
|
|
|
xvpool->align.padding_bottom);
|
2011-08-01 15:10:10 +00:00
|
|
|
|
2012-08-20 09:19:37 +00:00
|
|
|
/* do padding and alignment */
|
2014-09-27 14:58:51 +00:00
|
|
|
gst_video_info_align (&info, &xvpool->align);
|
2012-08-20 09:19:37 +00:00
|
|
|
|
2014-12-22 14:25:04 +00:00
|
|
|
gst_buffer_pool_config_set_video_alignment (config, &xvpool->align);
|
|
|
|
|
2011-08-01 15:10:10 +00:00
|
|
|
/* we need the video metadata too now */
|
2014-09-27 14:58:51 +00:00
|
|
|
xvpool->add_metavideo = TRUE;
|
2011-12-21 17:14:45 +00:00
|
|
|
} else {
|
2014-09-27 14:58:51 +00:00
|
|
|
gst_video_alignment_reset (&xvpool->align);
|
2011-08-01 15:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add the padding */
|
2014-09-27 14:58:51 +00:00
|
|
|
xvpool->padded_width =
|
|
|
|
GST_VIDEO_INFO_WIDTH (&info) + xvpool->align.padding_left +
|
|
|
|
xvpool->align.padding_right;
|
|
|
|
xvpool->padded_height =
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&info) + xvpool->align.padding_top +
|
|
|
|
xvpool->align.padding_bottom;
|
|
|
|
|
|
|
|
xvpool->info = info;
|
|
|
|
xvpool->crop.x = xvpool->align.padding_left;
|
|
|
|
xvpool->crop.y = xvpool->align.padding_top;
|
|
|
|
xvpool->crop.w = xvpool->info.width;
|
|
|
|
xvpool->crop.h = xvpool->info.height;
|
2012-08-20 09:19:37 +00:00
|
|
|
|
2019-11-08 08:28:44 +00:00
|
|
|
/* update offset, stride and size with actual xvimage buffer */
|
|
|
|
if (xvpool->pre_alloc_mem)
|
|
|
|
gst_memory_unref (xvpool->pre_alloc_mem);
|
|
|
|
|
|
|
|
xvpool->pre_alloc_mem = gst_xvimage_allocator_alloc (xvpool->allocator,
|
|
|
|
xvpool->im_format, &info, xvpool->padded_width,
|
|
|
|
xvpool->padded_height, &xvpool->crop, NULL);
|
|
|
|
|
|
|
|
if (!xvpool->pre_alloc_mem) {
|
|
|
|
GST_ERROR_OBJECT (pool, "couldn't allocate image");
|
|
|
|
gst_structure_free (config);
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
gint i;
|
|
|
|
XvImage *img;
|
|
|
|
|
|
|
|
img = gst_xvimage_memory_get_xvimage ((GstXvImageMemory *)
|
|
|
|
xvpool->pre_alloc_mem);
|
|
|
|
|
|
|
|
info.size = img->data_size;
|
|
|
|
|
|
|
|
for (i = 0; i < img->num_planes; i++) {
|
|
|
|
info.stride[i] = img->pitches[i];
|
|
|
|
info.offset[i] = img->offsets[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_video_info_is_equal (&xvpool->info, &info) ||
|
|
|
|
xvpool->info.size != info.size) {
|
|
|
|
GST_WARNING_OBJECT (pool, "different size, stride and/or offset, update");
|
|
|
|
xvpool->info = info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 11:10:53 +00:00
|
|
|
gst_buffer_pool_config_set_params (config, caps, info.size, min_buffers,
|
|
|
|
max_buffers);
|
|
|
|
|
2011-07-18 16:46:01 +00:00
|
|
|
return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
|
2011-04-06 10:26:47 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
wrong_config:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (pool, "invalid config");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
no_caps:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (pool, "no caps in config");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
wrong_caps:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (pool,
|
|
|
|
"failed getting geometry from caps %" GST_PTR_FORMAT, caps);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-06-17 15:54:52 +00:00
|
|
|
unknown_format:
|
|
|
|
{
|
2013-03-13 10:13:58 +00:00
|
|
|
GST_WARNING_OBJECT (pool, "failed to get format from caps %"
|
2011-06-17 15:54:52 +00:00
|
|
|
GST_PTR_FORMAT, caps);
|
2015-03-10 09:27:08 +00:00
|
|
|
return FALSE;
|
2011-06-17 15:54:52 +00:00
|
|
|
}
|
2011-04-06 10:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function handles GstXImageBuffer creation depending on XShm availability */
|
|
|
|
static GstFlowReturn
|
|
|
|
xvimage_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
|
2012-03-15 13:06:24 +00:00
|
|
|
GstBufferPoolAcquireParams * params)
|
2011-04-06 10:26:47 +00:00
|
|
|
{
|
|
|
|
GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
|
2011-07-04 08:19:13 +00:00
|
|
|
GstVideoInfo *info;
|
2011-04-06 10:26:47 +00:00
|
|
|
GstBuffer *xvimage;
|
2013-02-23 08:52:57 +00:00
|
|
|
GstMemory *mem;
|
2015-10-04 17:36:00 +00:00
|
|
|
GError *err = NULL;
|
2011-04-06 10:26:47 +00:00
|
|
|
|
2014-09-27 14:58:51 +00:00
|
|
|
info = &xvpool->info;
|
2011-07-04 08:19:13 +00:00
|
|
|
|
2011-06-17 15:27:22 +00:00
|
|
|
xvimage = gst_buffer_new ();
|
2013-03-13 10:13:58 +00:00
|
|
|
|
2019-11-08 08:28:44 +00:00
|
|
|
if (xvpool->pre_alloc_mem) {
|
|
|
|
mem = xvpool->pre_alloc_mem;
|
|
|
|
xvpool->pre_alloc_mem = NULL;
|
|
|
|
} else {
|
|
|
|
mem = gst_xvimage_allocator_alloc (xvpool->allocator, xvpool->im_format,
|
|
|
|
info, xvpool->padded_width, xvpool->padded_height, &xvpool->crop, &err);
|
|
|
|
}
|
2013-03-13 10:13:58 +00:00
|
|
|
|
2013-02-23 08:52:57 +00:00
|
|
|
if (mem == NULL) {
|
2011-06-17 15:27:22 +00:00
|
|
|
gst_buffer_unref (xvimage);
|
2011-04-06 10:26:47 +00:00
|
|
|
goto no_buffer;
|
2011-06-17 15:27:22 +00:00
|
|
|
}
|
2013-02-23 08:52:57 +00:00
|
|
|
gst_buffer_append_memory (xvimage, mem);
|
2011-06-17 15:54:52 +00:00
|
|
|
|
2014-09-27 14:58:51 +00:00
|
|
|
if (xvpool->add_metavideo) {
|
2019-07-22 11:25:04 +00:00
|
|
|
GstVideoMeta *meta;
|
|
|
|
|
2011-10-31 01:23:21 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "adding GstVideoMeta");
|
2019-07-22 11:25:04 +00:00
|
|
|
meta = gst_buffer_add_video_meta_full (xvimage, GST_VIDEO_FRAME_FLAG_NONE,
|
2012-08-20 14:13:00 +00:00
|
|
|
GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
|
|
|
|
info->offset, info->stride);
|
2019-07-22 11:25:04 +00:00
|
|
|
|
|
|
|
gst_video_meta_set_alignment (meta, xvpool->align);
|
2011-06-17 15:54:52 +00:00
|
|
|
}
|
|
|
|
|
2011-04-06 10:26:47 +00:00
|
|
|
*buffer = xvimage;
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
/* ERROR */
|
|
|
|
no_buffer:
|
|
|
|
{
|
2015-10-04 17:36:00 +00:00
|
|
|
GST_WARNING_OBJECT (pool, "can't create image: %s", err->message);
|
|
|
|
g_clear_error (&err);
|
2011-04-06 10:26:47 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GstBufferPool *
|
2013-03-13 10:13:58 +00:00
|
|
|
gst_xvimage_buffer_pool_new (GstXvImageAllocator * allocator)
|
2011-04-06 10:26:47 +00:00
|
|
|
{
|
|
|
|
GstXvImageBufferPool *pool;
|
|
|
|
|
|
|
|
pool = g_object_new (GST_TYPE_XVIMAGE_BUFFER_POOL, NULL);
|
2017-05-15 16:47:22 +00:00
|
|
|
gst_object_ref_sink (pool);
|
2014-09-27 14:58:51 +00:00
|
|
|
pool->allocator = gst_object_ref (allocator);
|
2011-04-06 10:26:47 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (pool, "new XvImage buffer pool %p", pool);
|
|
|
|
|
|
|
|
return GST_BUFFER_POOL_CAST (pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_xvimage_buffer_pool_class_init (GstXvImageBufferPoolClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = (GObjectClass *) klass;
|
|
|
|
GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
|
|
|
|
|
|
|
|
gobject_class->finalize = gst_xvimage_buffer_pool_finalize;
|
|
|
|
|
2011-07-29 15:15:39 +00:00
|
|
|
gstbufferpool_class->get_options = xvimage_buffer_pool_get_options;
|
2011-04-06 10:26:47 +00:00
|
|
|
gstbufferpool_class->set_config = xvimage_buffer_pool_set_config;
|
|
|
|
gstbufferpool_class->alloc_buffer = xvimage_buffer_pool_alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_xvimage_buffer_pool_init (GstXvImageBufferPool * pool)
|
|
|
|
{
|
2014-09-27 14:58:51 +00:00
|
|
|
/* nothing to do here */
|
2011-04-06 10:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_xvimage_buffer_pool_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstXvImageBufferPool *pool = GST_XVIMAGE_BUFFER_POOL_CAST (object);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (pool, "finalize XvImage buffer pool %p", pool);
|
|
|
|
|
2019-11-08 08:28:44 +00:00
|
|
|
if (pool->pre_alloc_mem)
|
|
|
|
gst_memory_unref (pool->pre_alloc_mem);
|
2014-09-27 14:58:51 +00:00
|
|
|
if (pool->caps)
|
|
|
|
gst_caps_unref (pool->caps);
|
|
|
|
if (pool->allocator)
|
|
|
|
gst_object_unref (pool->allocator);
|
2011-04-06 10:26:47 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_xvimage_buffer_pool_parent_class)->finalize (object);
|
|
|
|
}
|