This commit is contained in:
Akkariin Meiko
2022-03-12 03:16:09 +08:00
Unverified
parent 12b76e0c7a
commit 27c4ec74a1
10075 changed files with 5122287 additions and 1 deletions
@@ -0,0 +1,9 @@
if x11_dep.found()
# XShm is only used by ximage and xvimage
# FIXME: Need to check for XShmAttach inside libXext
xshm_dep = dependency('xext', required : get_option('xshm'))
core_conf.set('HAVE_XSHM', xshm_dep.found())
subdir('ximage')
subdir('xvimage')
endif
@@ -0,0 +1,16 @@
no_warn_args = []
# XKeycodeToKeysym is deprecated, but we use it when Xkb is unavailable
if cc.has_argument ('-Wno-deprecated-declarations')
no_warn_args += '-Wno-deprecated-declarations'
endif
gstximage = library('gstximagesink',
'ximagesink.c', 'ximage.c', 'ximagepool.c',
c_args : gst_plugins_base_args + no_warn_args,
include_directories: [configinc, libsinc],
dependencies : glib_deps + [video_dep, gst_base_dep, gst_dep, x11_dep, xshm_dep],
install : true,
install_dir : plugins_install_dir,
)
pkgconfig.generate(gstximage, install_dir : plugins_pkgconfig_install_dir)
plugins += [gstximage]
@@ -0,0 +1,37 @@
/* GStreamer
* Copyright (C) <2003> 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ximagesink.h"
static gboolean
plugin_init (GstPlugin * plugin)
{
return GST_ELEMENT_REGISTER (ximagesink, plugin);
}
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
ximagesink,
"X11 video output element based on standard Xlib calls",
plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
@@ -0,0 +1,724 @@
/* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* Object header */
#include "ximagesink.h"
/* Debugging category */
#include <gst/gstinfo.h>
/* Helper functions */
#include <gst/video/video.h>
#include <gst/video/gstvideometa.h>
#include <gst/video/gstvideopool.h>
GST_DEBUG_CATEGORY (gst_debug_x_image_pool);
#define GST_CAT_DEFAULT gst_debug_x_image_pool
/* X11 stuff */
static gboolean error_caught = FALSE;
static int
gst_ximagesink_handle_xerror (Display * display, XErrorEvent * xevent)
{
char error_msg[1024];
XGetErrorText (display, xevent->error_code, error_msg, 1024);
GST_DEBUG ("ximagesink triggered an XError. error: %s", error_msg);
error_caught = TRUE;
return 0;
}
static GstMemory *
gst_ximage_memory_alloc (GstAllocator * allocator, gsize size,
GstAllocationParams * params)
{
return NULL;
}
static void
gst_ximage_memory_free (GstAllocator * allocator, GstMemory * gmem)
{
GstXImageMemory *mem = (GstXImageMemory *) gmem;
GstXImageSink *ximagesink;
if (gmem->parent)
goto sub_mem;
ximagesink = mem->sink;
GST_DEBUG_OBJECT (ximagesink, "free memory %p", mem);
/* Hold the object lock to ensure the XContext doesn't disappear */
GST_OBJECT_LOCK (ximagesink);
/* We might have some buffers destroyed after changing state to NULL */
if (ximagesink->xcontext == NULL) {
GST_DEBUG_OBJECT (ximagesink, "Destroying XImage after XContext");
#ifdef HAVE_XSHM
/* Need to free the shared memory segment even if the x context
* was already cleaned up */
if (mem->SHMInfo.shmaddr != ((void *) -1)) {
shmdt (mem->SHMInfo.shmaddr);
}
#endif
goto beach;
}
g_mutex_lock (&ximagesink->x_lock);
#ifdef HAVE_XSHM
if (ximagesink->xcontext->use_xshm) {
if (mem->SHMInfo.shmaddr != ((void *) -1)) {
GST_DEBUG_OBJECT (ximagesink, "XServer ShmDetaching from 0x%x id 0x%lx",
mem->SHMInfo.shmid, mem->SHMInfo.shmseg);
XShmDetach (ximagesink->xcontext->disp, &mem->SHMInfo);
XSync (ximagesink->xcontext->disp, FALSE);
shmdt (mem->SHMInfo.shmaddr);
mem->SHMInfo.shmaddr = (void *) -1;
}
if (mem->ximage)
XDestroyImage (mem->ximage);
} else
#endif /* HAVE_XSHM */
{
if (mem->ximage) {
XDestroyImage (mem->ximage);
}
}
XSync (ximagesink->xcontext->disp, FALSE);
g_mutex_unlock (&ximagesink->x_lock);
beach:
GST_OBJECT_UNLOCK (ximagesink);
gst_object_unref (mem->sink);
sub_mem:
g_slice_free (GstXImageMemory, mem);
}
static gpointer
ximage_memory_map (GstXImageMemory * mem, gsize maxsize, GstMapFlags flags)
{
return mem->ximage->data + mem->parent.offset;
}
static gboolean
ximage_memory_unmap (GstXImageMemory * mem)
{
return TRUE;
}
static GstXImageMemory *
ximage_memory_share (GstXImageMemory * mem, gssize offset, gsize size)
{
GstXImageMemory *sub;
GstMemory *parent;
/* We can only share the complete memory */
if (offset != 0)
return NULL;
if (size != -1 && size != mem->size)
return NULL;
/* find the real parent */
if ((parent = mem->parent.parent) == NULL)
parent = (GstMemory *) mem;
if (size == -1)
size = mem->parent.size - offset;
/* the shared memory is always readonly */
sub = g_slice_new (GstXImageMemory);
gst_memory_init (GST_MEMORY_CAST (sub), GST_MINI_OBJECT_FLAGS (parent) |
GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->parent.allocator,
&mem->parent, mem->parent.maxsize, mem->parent.align,
mem->parent.offset + offset, size);
sub->sink = mem->sink;
sub->ximage = mem->ximage;
#ifdef HAVE_XSHM
sub->SHMInfo = mem->SHMInfo;
#endif
sub->x = mem->x;
sub->y = mem->y;
sub->width = mem->width;
sub->height = mem->height;
return sub;
}
typedef GstAllocator GstXImageMemoryAllocator;
typedef GstAllocatorClass GstXImageMemoryAllocatorClass;
GType ximage_memory_allocator_get_type (void);
G_DEFINE_TYPE (GstXImageMemoryAllocator, ximage_memory_allocator,
GST_TYPE_ALLOCATOR);
#define GST_XIMAGE_ALLOCATOR_NAME "ximage"
#define GST_TYPE_XIMAGE_MEMORY_ALLOCATOR (ximage_memory_allocator_get_type())
#define GST_IS_XIMAGE_MEMORY_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_XIMAGE_MEMORY_ALLOCATOR))
static void
ximage_memory_allocator_class_init (GstXImageMemoryAllocatorClass * klass)
{
GstAllocatorClass *allocator_class;
allocator_class = (GstAllocatorClass *) klass;
allocator_class->alloc = gst_ximage_memory_alloc;
allocator_class->free = gst_ximage_memory_free;
}
static void
ximage_memory_allocator_init (GstXImageMemoryAllocator * allocator)
{
GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
alloc->mem_type = GST_XIMAGE_ALLOCATOR_NAME;
alloc->mem_map = (GstMemoryMapFunction) ximage_memory_map;
alloc->mem_unmap = (GstMemoryUnmapFunction) ximage_memory_unmap;
alloc->mem_share = (GstMemoryShareFunction) ximage_memory_share;
/* fallback copy and is_span */
GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
}
static GstMemory *
ximage_memory_alloc (GstXImageBufferPool * xpool)
{
GstXImageSink *ximagesink;
int (*handler) (Display *, XErrorEvent *);
gboolean success = FALSE;
GstXContext *xcontext;
gint width, height, align, offset;
GstXImageMemory *mem;
ximagesink = xpool->sink;
xcontext = ximagesink->xcontext;
width = xpool->padded_width;
height = xpool->padded_height;
mem = g_slice_new (GstXImageMemory);
#ifdef HAVE_XSHM
mem->SHMInfo.shmaddr = ((void *) -1);
mem->SHMInfo.shmid = -1;
#endif
mem->x = xpool->align.padding_left;
mem->y = xpool->align.padding_top;
mem->width = GST_VIDEO_INFO_WIDTH (&xpool->info);
mem->height = GST_VIDEO_INFO_HEIGHT (&xpool->info);
mem->sink = gst_object_ref (ximagesink);
GST_DEBUG_OBJECT (ximagesink, "creating image %p (%dx%d)", mem,
width, height);
g_mutex_lock (&ximagesink->x_lock);
/* Setting an error handler to catch failure */
error_caught = FALSE;
handler = XSetErrorHandler (gst_ximagesink_handle_xerror);
#ifdef HAVE_XSHM
if (xcontext->use_xshm) {
mem->ximage = XShmCreateImage (xcontext->disp,
xcontext->visual,
xcontext->depth, ZPixmap, NULL, &mem->SHMInfo, width, height);
if (!mem->ximage || error_caught) {
g_mutex_unlock (&ximagesink->x_lock);
/* Reset error flag */
error_caught = FALSE;
/* Push a warning */
GST_ELEMENT_WARNING (ximagesink, RESOURCE, WRITE,
("Failed to create output image buffer of %dx%d pixels",
width, height),
("could not XShmCreateImage a %dx%d image", width, height));
/* Retry without XShm */
ximagesink->xcontext->use_xshm = FALSE;
/* Hold X mutex again to try without XShm */
g_mutex_lock (&ximagesink->x_lock);
goto no_xshm;
}
/* we have to use the returned bytes_per_line for our shm size */
mem->size = mem->ximage->bytes_per_line * mem->ximage->height;
GST_LOG_OBJECT (ximagesink,
"XShm image size is %" G_GSIZE_FORMAT ", width %d, stride %d",
mem->size, width, mem->ximage->bytes_per_line);
/* get shared memory */
align = 0;
mem->SHMInfo.shmid =
shmget (IPC_PRIVATE, mem->size + align, IPC_CREAT | 0777);
if (mem->SHMInfo.shmid == -1)
goto shmget_failed;
/* attach */
mem->SHMInfo.shmaddr = shmat (mem->SHMInfo.shmid, NULL, 0);
if (mem->SHMInfo.shmaddr == ((void *) -1))
goto shmat_failed;
/* now we can set up the image data */
mem->ximage->data = mem->SHMInfo.shmaddr;
mem->SHMInfo.readOnly = FALSE;
if (XShmAttach (xcontext->disp, &mem->SHMInfo) == 0)
goto xattach_failed;
XSync (xcontext->disp, FALSE);
/* Now that everyone has attached, we can delete the shared memory segment.
* This way, it will be deleted as soon as we detach later, and not
* leaked if we crash. */
shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
GST_DEBUG_OBJECT (ximagesink, "XServer ShmAttached to 0x%x, id 0x%lx",
mem->SHMInfo.shmid, mem->SHMInfo.shmseg);
} else
no_xshm:
#endif /* HAVE_XSHM */
{
guint allocsize;
mem->ximage = XCreateImage (xcontext->disp,
xcontext->visual,
xcontext->depth, ZPixmap, 0, NULL, width, height, xcontext->bpp, 0);
if (!mem->ximage || error_caught)
goto create_failed;
/* upstream will assume that rowstrides are multiples of 4, but this
* doesn't always seem to be the case with XCreateImage() */
if ((mem->ximage->bytes_per_line % 4) != 0) {
GST_WARNING_OBJECT (ximagesink, "returned stride not a multiple of 4 as "
"usually assumed");
}
/* we have to use the returned bytes_per_line for our image size */
mem->size = mem->ximage->bytes_per_line * mem->ximage->height;
/* alloc a bit more for unexpected strides to avoid crashes upstream.
* FIXME: if we get an unrounded stride, the image will be displayed
* distorted, since all upstream elements assume a rounded stride */
allocsize =
GST_ROUND_UP_4 (mem->ximage->bytes_per_line) * mem->ximage->height;
/* we want 16 byte aligned memory, g_malloc may only give 8 */
align = 15;
mem->ximage->data = g_malloc (allocsize + align);
GST_LOG_OBJECT (ximagesink,
"non-XShm image size is %" G_GSIZE_FORMAT " (allocated: %u), width %d, "
"stride %d", mem->size, allocsize, width, mem->ximage->bytes_per_line);
XSync (xcontext->disp, FALSE);
}
if ((offset = ((guintptr) mem->ximage->data & align)))
offset = (align + 1) - offset;
GST_DEBUG_OBJECT (ximagesink, "memory %p, align %d, offset %d",
mem->ximage->data, align, offset);
/* Reset error handler */
error_caught = FALSE;
XSetErrorHandler (handler);
gst_memory_init (GST_MEMORY_CAST (mem), GST_MEMORY_FLAG_NO_SHARE,
xpool->allocator, NULL, mem->size + align, align, offset, mem->size);
g_mutex_unlock (&ximagesink->x_lock);
success = TRUE;
beach:
if (!success) {
g_slice_free (GstXImageMemory, mem);
mem = NULL;
}
return GST_MEMORY_CAST (mem);
/* ERRORS */
create_failed:
{
g_mutex_unlock (&ximagesink->x_lock);
/* Reset error handler */
error_caught = FALSE;
XSetErrorHandler (handler);
/* Push an error */
GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
("Failed to create output image buffer of %dx%d pixels",
width, height),
("could not XShmCreateImage a %dx%d image", width, height));
goto beach;
}
#ifdef HAVE_XSHM
shmget_failed:
{
g_mutex_unlock (&ximagesink->x_lock);
GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
("Failed to create output image buffer of %dx%d pixels",
width, height),
("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
mem->size));
goto beach;
}
shmat_failed:
{
g_mutex_unlock (&ximagesink->x_lock);
GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
("Failed to create output image buffer of %dx%d pixels",
width, height), ("Failed to shmat: %s", g_strerror (errno)));
/* Clean up the shared memory segment */
shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
goto beach;
}
xattach_failed:
{
/* Clean up the shared memory segment */
shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
g_mutex_unlock (&ximagesink->x_lock);
GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
("Failed to create output image buffer of %dx%d pixels",
width, height), ("Failed to XShmAttach"));
goto beach;
}
#endif
}
#ifdef HAVE_XSHM
/* This function checks that it is actually really possible to create an image
using XShm */
gboolean
gst_x_image_sink_check_xshm_calls (GstXImageSink * ximagesink,
GstXContext * xcontext)
{
XImage *ximage;
XShmSegmentInfo SHMInfo;
size_t size;
int (*handler) (Display *, XErrorEvent *);
gboolean result = FALSE;
gboolean did_attach = FALSE;
g_return_val_if_fail (xcontext != NULL, FALSE);
/* Sync to ensure any older errors are already processed */
XSync (xcontext->disp, FALSE);
/* Set defaults so we don't free these later unnecessarily */
SHMInfo.shmaddr = ((void *) -1);
SHMInfo.shmid = -1;
/* Setting an error handler to catch failure */
error_caught = FALSE;
handler = XSetErrorHandler (gst_ximagesink_handle_xerror);
/* Trying to create a 1x1 ximage */
GST_DEBUG ("XShmCreateImage of 1x1");
ximage = XShmCreateImage (xcontext->disp, xcontext->visual,
xcontext->depth, ZPixmap, NULL, &SHMInfo, 1, 1);
/* Might cause an error, sync to ensure it is noticed */
XSync (xcontext->disp, FALSE);
if (!ximage || error_caught) {
GST_WARNING ("could not XShmCreateImage a 1x1 image");
goto beach;
}
size = ximage->height * ximage->bytes_per_line;
SHMInfo.shmid = shmget (IPC_PRIVATE, size, IPC_CREAT | 0777);
if (SHMInfo.shmid == -1) {
GST_WARNING ("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
size);
goto beach;
}
SHMInfo.shmaddr = shmat (SHMInfo.shmid, NULL, 0);
if (SHMInfo.shmaddr == ((void *) -1)) {
GST_WARNING ("Failed to shmat: %s", g_strerror (errno));
/* Clean up the shared memory segment */
shmctl (SHMInfo.shmid, IPC_RMID, NULL);
goto beach;
}
ximage->data = SHMInfo.shmaddr;
SHMInfo.readOnly = FALSE;
if (XShmAttach (xcontext->disp, &SHMInfo) == 0) {
GST_WARNING ("Failed to XShmAttach");
/* Clean up the shared memory segment */
shmctl (SHMInfo.shmid, IPC_RMID, NULL);
goto beach;
}
/* Sync to ensure we see any errors we caused */
XSync (xcontext->disp, FALSE);
/* Delete the shared memory segment as soon as everyone is attached.
* This way, it will be deleted as soon as we detach later, and not
* leaked if we crash. */
shmctl (SHMInfo.shmid, IPC_RMID, NULL);
if (!error_caught) {
GST_DEBUG ("XServer ShmAttached to 0x%x, id 0x%lx", SHMInfo.shmid,
SHMInfo.shmseg);
did_attach = TRUE;
/* store whether we succeeded in result */
result = TRUE;
} else {
GST_WARNING ("MIT-SHM extension check failed at XShmAttach. "
"Not using shared memory.");
}
beach:
/* Sync to ensure we swallow any errors we caused and reset error_caught */
XSync (xcontext->disp, FALSE);
error_caught = FALSE;
XSetErrorHandler (handler);
if (did_attach) {
GST_DEBUG ("XServer ShmDetaching from 0x%x id 0x%lx",
SHMInfo.shmid, SHMInfo.shmseg);
XShmDetach (xcontext->disp, &SHMInfo);
XSync (xcontext->disp, FALSE);
}
if (SHMInfo.shmaddr != ((void *) -1))
shmdt (SHMInfo.shmaddr);
if (ximage)
XDestroyImage (ximage);
return result;
}
#endif /* HAVE_XSHM */
/* bufferpool */
static void gst_ximage_buffer_pool_finalize (GObject * object);
#define gst_ximage_buffer_pool_parent_class parent_class
G_DEFINE_TYPE (GstXImageBufferPool, gst_ximage_buffer_pool,
GST_TYPE_BUFFER_POOL);
static const gchar **
ximage_buffer_pool_get_options (GstBufferPool * pool)
{
static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT, NULL
};
return options;
}
static gboolean
ximage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
{
GstXImageBufferPool *xpool = GST_XIMAGE_BUFFER_POOL_CAST (pool);
GstVideoInfo info;
GstCaps *caps;
guint size, min_buffers, max_buffers;
if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
&max_buffers))
goto wrong_config;
if (caps == NULL)
goto no_caps;
/* now parse the caps from the config */
if (!gst_video_info_from_caps (&info, caps))
goto wrong_caps;
GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
caps);
/* keep track of the width and height and caps */
if (xpool->caps)
gst_caps_unref (xpool->caps);
xpool->caps = gst_caps_ref (caps);
/* check for the configured metadata */
xpool->add_metavideo =
gst_buffer_pool_config_has_option (config,
GST_BUFFER_POOL_OPTION_VIDEO_META);
/* parse extra alignment info */
xpool->need_alignment = gst_buffer_pool_config_has_option (config,
GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
if (xpool->need_alignment) {
gst_buffer_pool_config_get_video_alignment (config, &xpool->align);
GST_LOG_OBJECT (pool, "padding %u-%ux%u-%u", xpool->align.padding_top,
xpool->align.padding_left, xpool->align.padding_left,
xpool->align.padding_bottom);
/* do padding and alignment */
gst_video_info_align (&info, &xpool->align);
gst_buffer_pool_config_set_video_alignment (config, &xpool->align);
/* we need the video metadata too now */
xpool->add_metavideo = TRUE;
} else {
gst_video_alignment_reset (&xpool->align);
}
/* add the padding */
xpool->padded_width =
GST_VIDEO_INFO_WIDTH (&info) + xpool->align.padding_left +
xpool->align.padding_right;
xpool->padded_height =
GST_VIDEO_INFO_HEIGHT (&info) + xpool->align.padding_top +
xpool->align.padding_bottom;
xpool->info = info;
gst_buffer_pool_config_set_params (config, caps, info.size, min_buffers,
max_buffers);
return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
/* 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;
}
}
/* This function handles GstXImageBuffer creation depending on XShm availability */
static GstFlowReturn
ximage_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
GstBufferPoolAcquireParams * params)
{
GstXImageBufferPool *xpool = GST_XIMAGE_BUFFER_POOL_CAST (pool);
GstVideoInfo *info;
GstBuffer *ximage;
GstMemory *mem;
info = &xpool->info;
ximage = gst_buffer_new ();
mem = ximage_memory_alloc (xpool);
if (mem == NULL) {
gst_buffer_unref (ximage);
goto no_buffer;
}
gst_buffer_append_memory (ximage, mem);
if (xpool->add_metavideo) {
GstVideoMeta *meta;
GST_DEBUG_OBJECT (pool, "adding GstVideoMeta");
/* these are just the defaults for now */
meta = gst_buffer_add_video_meta_full (ximage, GST_VIDEO_FRAME_FLAG_NONE,
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);
gst_video_meta_set_alignment (meta, xpool->align);
}
*buffer = ximage;
return GST_FLOW_OK;
/* ERROR */
no_buffer:
{
GST_WARNING_OBJECT (pool, "can't create image");
return GST_FLOW_ERROR;
}
}
GstBufferPool *
gst_ximage_buffer_pool_new (GstXImageSink * ximagesink)
{
GstXImageBufferPool *pool;
g_return_val_if_fail (GST_IS_X_IMAGE_SINK (ximagesink), NULL);
pool = g_object_new (GST_TYPE_XIMAGE_BUFFER_POOL, NULL);
gst_object_ref_sink (pool);
pool->sink = gst_object_ref (ximagesink);
pool->allocator = g_object_new (GST_TYPE_XIMAGE_MEMORY_ALLOCATOR, NULL);
gst_object_ref_sink (pool->allocator);
GST_LOG_OBJECT (pool, "new XImage buffer pool %p", pool);
return GST_BUFFER_POOL_CAST (pool);
}
static void
gst_ximage_buffer_pool_class_init (GstXImageBufferPoolClass * klass)
{
GObjectClass *gobject_class = (GObjectClass *) klass;
GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
gobject_class->finalize = gst_ximage_buffer_pool_finalize;
gstbufferpool_class->get_options = ximage_buffer_pool_get_options;
gstbufferpool_class->set_config = ximage_buffer_pool_set_config;
gstbufferpool_class->alloc_buffer = ximage_buffer_pool_alloc;
}
static void
gst_ximage_buffer_pool_init (GstXImageBufferPool * pool)
{
/* nothing to do here */
}
static void
gst_ximage_buffer_pool_finalize (GObject * object)
{
GstXImageBufferPool *pool = GST_XIMAGE_BUFFER_POOL_CAST (object);
GST_LOG_OBJECT (pool, "finalize XImage buffer pool %p", pool);
if (pool->caps)
gst_caps_unref (pool->caps);
gst_object_unref (pool->sink);
gst_object_unref (pool->allocator);
G_OBJECT_CLASS (gst_ximage_buffer_pool_parent_class)->finalize (object);
}
@@ -0,0 +1,113 @@
/* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_XIMAGEPOOL_H__
#define __GST_XIMAGEPOOL_H__
#ifdef HAVE_XSHM
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#endif /* HAVE_XSHM */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef HAVE_XSHM
#include <X11/extensions/XShm.h>
#endif /* HAVE_XSHM */
#include <string.h>
#include <math.h>
G_BEGIN_DECLS
typedef struct _GstXImageMemory GstXImageMemory;
typedef struct _GstXImageBufferPool GstXImageBufferPool;
typedef struct _GstXImageBufferPoolClass GstXImageBufferPoolClass;
#include "ximagesink.h"
/**
* GstXImageMemory:
* @sink: a reference to the our #GstXImageSink
* @ximage: the XImage of this buffer
* @width: the width in pixels of XImage @ximage
* @height: the height in pixels of XImage @ximage
* @size: the size in bytes of XImage @ximage
*
* Subclass of #GstMemory containing additional information about an XImage.
*/
struct _GstXImageMemory
{
GstMemory parent;
/* Reference to the ximagesink we belong to */
GstXImageSink *sink;
XImage *ximage;
#ifdef HAVE_XSHM
XShmSegmentInfo SHMInfo;
#endif /* HAVE_XSHM */
gint x, y;
gint width, height;
size_t size;
};
/* buffer pool functions */
#define GST_TYPE_XIMAGE_BUFFER_POOL (gst_ximage_buffer_pool_get_type())
#define GST_IS_XIMAGE_BUFFER_POOL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_XIMAGE_BUFFER_POOL))
#define GST_XIMAGE_BUFFER_POOL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_XIMAGE_BUFFER_POOL, GstXImageBufferPool))
#define GST_XIMAGE_BUFFER_POOL_CAST(obj) ((GstXImageBufferPool*)(obj))
struct _GstXImageBufferPool
{
GstBufferPool bufferpool;
GstXImageSink *sink;
GstAllocator *allocator;
GstCaps *caps;
GstVideoInfo info;
GstVideoAlignment align;
guint padded_width;
guint padded_height;
gboolean add_metavideo;
gboolean need_alignment;
};
struct _GstXImageBufferPoolClass
{
GstBufferPoolClass parent_class;
};
GType gst_ximage_buffer_pool_get_type (void);
GstBufferPool * gst_ximage_buffer_pool_new (GstXImageSink * ximagesink);
gboolean gst_x_image_sink_check_xshm_calls (GstXImageSink * ximagesink,
GstXContext * xcontext);
G_END_DECLS
#endif /* __GST_XIMAGEPOOL_H__ */
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,214 @@
/* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_X_IMAGE_SINK_H__
#define __GST_X_IMAGE_SINK_H__
#include <gst/video/gstvideosink.h>
#ifdef HAVE_XSHM
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#endif /* HAVE_XSHM */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef HAVE_XSHM
#include <X11/extensions/XShm.h>
#endif /* HAVE_XSHM */
#include <string.h>
#include <math.h>
/* Helper functions */
#include <gst/video/video.h>
G_BEGIN_DECLS
#define GST_TYPE_X_IMAGE_SINK \
(gst_x_image_sink_get_type())
#define GST_X_IMAGE_SINK(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_X_IMAGE_SINK, GstXImageSink))
#define GST_X_IMAGE_SINK_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_X_IMAGE_SINK, GstXImageSinkClass))
#define GST_IS_X_IMAGE_SINK(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_X_IMAGE_SINK))
#define GST_IS_X_IMAGE_SINK_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_X_IMAGE_SINK))
typedef struct _GstXContext GstXContext;
typedef struct _GstXWindow GstXWindow;
typedef struct _GstXImageSink GstXImageSink;
typedef struct _GstXImageSinkClass GstXImageSinkClass;
#include "ximagepool.h"
/*
* GstXContext:
* @disp: the X11 Display of this context
* @screen: the default Screen of Display @disp
* @screen_num: the Screen number of @screen
* @visual: the default Visual of Screen @screen
* @root: the root Window of Display @disp
* @white: the value of a white pixel on Screen @screen
* @black: the value of a black pixel on Screen @screen
* @depth: the color depth of Display @disp
* @bpp: the number of bits per pixel on Display @disp
* @endianness: the endianness of image bytes on Display @disp
* @width: the width in pixels of Display @disp
* @height: the height in pixels of Display @disp
* @widthmm: the width in millimeters of Display @disp
* @heightmm: the height in millimeters of Display @disp
* @par: the pixel aspect ratio calculated from @width, @widthmm and @height,
* @heightmm ratio
* @use_xshm: used to known whether of not XShm extension is usable or not even
* if the Extension is present
* @use_xkb: used to known wether of not Xkb extension is usable or not even
* if the Extension is present
* @caps: the #GstCaps that Display @disp can accept
*
* Structure used to store various information collected/calculated for a
* Display.
*/
struct _GstXContext
{
Display *disp;
Screen *screen;
gint screen_num;
Visual *visual;
Window root;
gulong white, black;
gint depth;
gint bpp;
gint width, height;
gint widthmm, heightmm;
GValue *par; /* calculated pixel aspect ratio */
gboolean use_xshm;
gboolean use_xkb;
GstCaps *caps;
GstCaps *last_caps;
};
/*
* GstXWindow:
* @win: the Window ID of this X11 window
* @width: the width in pixels of Window @win
* @height: the height in pixels of Window @win
* @internal: used to remember if Window @win was created internally or passed
* through the #GstVideoOverlay interface
* @gc: the Graphical Context of Window @win
*
* Structure used to store information about a Window.
*/
struct _GstXWindow
{
Window win;
gint width, height;
gboolean internal;
GC gc;
};
/**
* GstXImageSink:
* @display_name: the name of the Display we want to render to
* @xcontext: our instance's #GstXContext
* @xwindow: the #GstXWindow we are rendering to
* @ximage: internal #GstXImage used to store incoming buffers and render when
* not using the buffer_alloc optimization mechanism
* @cur_image: a reference to the last #GstXImage that was put to @xwindow. It
* is used when Expose events are received to redraw the latest video frame
* @event_thread: a thread listening for events on @xwindow and handling them
* @running: used to inform @event_thread if it should run/shutdown
* @fps_n: the framerate fraction numerator
* @fps_d: the framerate fraction denominator
* @x_lock: used to protect X calls as we are not using the XLib in threaded
* mode
* @flow_lock: used to protect data flow routines from external calls such as
* events from @event_thread or methods from the #GstVideoOverlay interface
* @par: used to override calculated pixel aspect ratio from @xcontext
* @pool_lock: used to protect the buffer pool
* @buffer_pool: a list of #GstXImageBuffer that could be reused at next buffer
* allocation call
* @synchronous: used to store if XSynchronous should be used or not (for
* debugging purpose only)
* @keep_aspect: used to remember if reverse negotiation scaling should respect
* aspect ratio
* @handle_events: used to know if we should handle select XEvents or not
*
* The #GstXImageSink data structure.
*/
struct _GstXImageSink
{
/* Our element stuff */
GstVideoSink videosink;
char *display_name;
GstXContext *xcontext;
GstXWindow *xwindow;
GstBuffer *cur_image;
GThread *event_thread;
gboolean running;
GstVideoInfo info;
/* Framerate numerator and denominator */
gint fps_n;
gint fps_d;
GMutex x_lock;
GMutex flow_lock;
/* object-set pixel aspect ratio */
GValue *par;
/* the buffer pool */
GstBufferPool *pool;
gboolean synchronous;
gboolean keep_aspect;
gboolean handle_events;
gboolean handle_expose;
gboolean draw_border;
/* stream metadata */
gchar *media_title;
};
struct _GstXImageSinkClass
{
GstVideoSinkClass parent_class;
};
GType gst_x_image_sink_get_type (void);
GST_ELEMENT_REGISTER_DECLARE (ximagesink);
G_END_DECLS
#endif /* __GST_X_IMAGE_SINK_H__ */
@@ -0,0 +1,28 @@
xvimage_sources = [
'xvcontext.c',
'xvimage.c',
'xvimageallocator.c',
'xvimagepool.c',
'xvimagesink.c',
]
no_warn_args = []
# XKeycodeToKeysym is deprecated, but we use it when Xkb is unavailable
if cc.has_argument ('-Wno-deprecated-declarations')
no_warn_args += '-Wno-deprecated-declarations'
endif
xvideo_dep = dependency('xv', required : get_option('xvideo'))
if xvideo_dep.found()
gstxvimage = library('gstxvimagesink',
xvimage_sources,
c_args : gst_plugins_base_args + no_warn_args,
include_directories: [configinc, libsinc],
dependencies : glib_deps + [video_dep, gst_base_dep, gst_dep, x11_dep, xshm_dep, xvideo_dep, libm],
install : true,
install_dir : plugins_install_dir,
)
pkgconfig.generate(gstxvimage, install_dir : plugins_pkgconfig_install_dir)
plugins += [gstxvimage]
endif
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,254 @@
/* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_XVCONTEXT_H__
#define __GST_XVCONTEXT_H__
#ifdef HAVE_XSHM
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#endif /* HAVE_XSHM */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef HAVE_XSHM
#include <X11/extensions/XShm.h>
#endif /* HAVE_XSHM */
#include <X11/extensions/Xv.h>
#include <X11/extensions/Xvlib.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <gst/video/video.h>
G_BEGIN_DECLS
typedef struct _GstXvContextConfig GstXvContextConfig;
typedef struct _GstXvImageFormat GstXvImageFormat;
typedef struct _GstXvContext GstXvContext;
/**
* GstXvContextConfig:
*
* current configuration of the context
*/
struct _GstXvContextConfig
{
gchar *display_name;
guint adaptor_nr;
/* port attributes */
gboolean autopaint_colorkey;
gint colorkey;
gboolean double_buffer;
gint brightness;
gint contrast;
gint hue;
gint saturation;
gboolean cb_changed;
};
/**
* GstXvImageFormat:
* @format: the image format
* @caps: generated #GstCaps for this image format
*
* Structure storing image format to #GstCaps association.
*/
struct _GstXvImageFormat
{
gint format;
GstVideoFormat vformat;
GstCaps *caps;
};
#define GST_TYPE_XVCONTEXT (gst_xvcontext_get_type())
#define GST_IS_XVCONTEXT(obj) (GST_IS_MINI_OBJECT_TYPE(obj, GST_TYPE_XVCONTEXT))
#define GST_XVCONTEXT_CAST(obj) ((GstXvContext *)obj)
#define GST_XVCONTEXT(obj) (GST_XVCONTEXT_CAST(obj))
/*
* GstXvContext:
* @disp: the X11 Display of this context
* @screen: the default Screen of Display @disp
* @screen_num: the Screen number of @screen
* @visual: the default Visual of Screen @screen
* @root: the root Window of Display @disp
* @white: the value of a white pixel on Screen @screen
* @black: the value of a black pixel on Screen @screen
* @depth: the color depth of Display @disp
* @bpp: the number of bits per pixel on Display @disp
* @endianness: the endianness of image bytes on Display @disp
* @width: the width in pixels of Display @disp
* @height: the height in pixels of Display @disp
* @widthmm: the width in millimeters of Display @disp
* @heightmm: the height in millimeters of Display @disp
* @par: the pixel aspect ratio calculated from @width, @widthmm and @height,
* @heightmm ratio
* @use_xshm: used to known whether of not XShm extension is usable or not even
* if the Extension is present
* @use_xkb: used to known wether of not Xkb extension is usable or not even
* if the Extension is present
* @xv_port_id: the XVideo port ID
* @im_format: used to store at least a valid format for XShm calls checks
* @formats_list: list of supported image formats on @xv_port_id
* @channels_list: list of #GstColorBalanceChannels
* @caps: the #GstCaps that Display @disp can accept
*
* Structure used to store various information collected/calculated for a
* Display.
*/
struct _GstXvContext
{
GstMiniObject parent;
GMutex lock;
Display *disp;
Screen *screen;
gint screen_num;
Visual *visual;
Window root;
gulong white, black;
gint depth;
gint bpp;
gint endianness;
gint width, height;
gint widthmm, heightmm;
GValue *par; /* calculated pixel aspect ratio */
gboolean use_xshm;
gboolean use_xkb;
XvPortID xv_port_id;
guint nb_adaptors;
gchar **adaptors;
guint adaptor_nr;
gint im_format;
/* port features */
gboolean have_autopaint_colorkey;
gboolean have_colorkey;
gboolean have_double_buffer;
gboolean have_iturbt709;
gboolean have_xvcolorspace;
GList *formats_list;
GList *channels_list;
GstCaps *caps;
/* Optimisation storage for buffer_alloc return */
GstCaps *last_caps;
gint last_format;
gint last_width;
gint last_height;
};
GType gst_xvcontext_get_type (void);
void gst_xvcontext_config_clear (GstXvContextConfig *config);
GstXvContext * gst_xvcontext_new (GstXvContextConfig *config, GError **error);
/* refcounting */
static inline GstXvContext *
gst_xvcontext_ref (GstXvContext * xvcontext)
{
return GST_XVCONTEXT_CAST (gst_mini_object_ref (GST_MINI_OBJECT_CAST (
xvcontext)));
}
static inline void
gst_xvcontext_unref (GstXvContext * xvcontext)
{
gst_mini_object_unref (GST_MINI_OBJECT_CAST (xvcontext));
}
gint gst_xvcontext_get_format_from_info (GstXvContext * xvcontext,
const GstVideoInfo * info);
void gst_xvcontext_set_synchronous (GstXvContext * xvcontext,
gboolean synchronous);
void gst_xvcontext_update_colorbalance (GstXvContext * xvcontext,
GstXvContextConfig * config);
void gst_xvcontext_set_colorimetry (GstXvContext * xvcontext,
GstVideoColorimetry *colorimetry);
typedef struct _GstXWindow GstXWindow;
/*
* GstXWindow:
* @win: the Window ID of this X11 window
* @width: the width in pixels of Window @win
* @height: the height in pixels of Window @win
* @internal: used to remember if Window @win was created internally or passed
* through the #GstVideoOverlay interface
* @gc: the Graphical Context of Window @win
*
* Structure used to store information about a Window.
*/
struct _GstXWindow
{
GstXvContext *context;
Window win;
gint width, height;
gboolean have_render_rect;
GstVideoRectangle render_rect;
gboolean internal;
GC gc;
};
G_END_DECLS
GstXWindow * gst_xvcontext_create_xwindow (GstXvContext * context,
gint width, gint height);
GstXWindow * gst_xvcontext_create_xwindow_from_xid (GstXvContext * context, XID xid);
void gst_xwindow_destroy (GstXWindow * window);
void gst_xwindow_set_event_handling (GstXWindow * window, gboolean handle_events);
void gst_xwindow_set_title (GstXWindow * window, const gchar * title);
void gst_xwindow_update_geometry (GstXWindow * window);
void gst_xwindow_clear (GstXWindow * window);
void gst_xwindow_set_render_rectangle (GstXWindow * window,
gint x, gint y, gint width, gint height);
#endif /* __GST_XVCONTEXT_H__ */
@@ -0,0 +1,37 @@
/* GStreamer
* Copyright (C) <2003> 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "xvimagesink.h"
static gboolean
plugin_init (GstPlugin * plugin)
{
return GST_ELEMENT_REGISTER (xvimagesink, plugin);;
}
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
xvimagesink,
"XFree86 video output plugin using Xv extension",
plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
@@ -0,0 +1,669 @@
/* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_XSHM
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#endif /* HAVE_XSHM */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef HAVE_XSHM
#include <X11/extensions/XShm.h>
#endif /* HAVE_XSHM */
#include <string.h>
#include <math.h>
/* Object header */
#include "xvimageallocator.h"
/* Debugging category */
#include <gst/gstinfo.h>
/* Helper functions */
#include <gst/video/video.h>
GST_DEBUG_CATEGORY_STATIC (gst_debug_xvimageallocator);
#define GST_CAT_DEFAULT gst_debug_xvimageallocator
struct _GstXvImageMemory
{
GstMemory parent;
gint im_format;
GstVideoInfo info;
GstVideoRectangle crop;
XvImage *xvimage;
#ifdef HAVE_XSHM
XShmSegmentInfo SHMInfo;
#endif /* HAVE_XSHM */
};
struct _GstXvImageAllocator
{
GstAllocator parent;
GstXvContext *context;
};
struct _GstXvImageAllocatorClass
{
GstAllocatorClass parent_class;
};
gboolean
gst_xvimage_memory_is_from_context (GstMemory * mem, GstXvContext * context)
{
GstXvImageAllocator *alloc;
if (!GST_IS_XVIMAGE_ALLOCATOR (mem->allocator))
return FALSE;
alloc = GST_XVIMAGE_ALLOCATOR_CAST (mem->allocator);
if (alloc->context != context)
return FALSE;
return TRUE;
}
gint
gst_xvimage_memory_get_format (GstXvImageMemory * xvmem)
{
g_return_val_if_fail (xvmem != NULL, FALSE);
return xvmem->im_format;
}
XvImage *
gst_xvimage_memory_get_xvimage (GstXvImageMemory * xvmem)
{
g_return_val_if_fail (xvmem != NULL, FALSE);
return xvmem->xvimage;
}
gboolean
gst_xvimage_memory_get_crop (GstXvImageMemory * xvmem, GstVideoRectangle * crop)
{
g_return_val_if_fail (xvmem != NULL, FALSE);
if (crop)
*crop = xvmem->crop;
return TRUE;
}
/* X11 stuff */
static gboolean error_caught = FALSE;
static int
gst_xvimage_handle_xerror (Display * display, XErrorEvent * xevent)
{
char error_msg[1024];
XGetErrorText (display, xevent->error_code, error_msg, 1024);
GST_DEBUG ("xvimage triggered an XError. error: %s", error_msg);
error_caught = TRUE;
return 0;
}
static GstMemory *
gst_xvimage_allocator_dummy_alloc (GstAllocator * allocator, gsize size,
GstAllocationParams * params)
{
return NULL;
}
static void
gst_xvimage_allocator_free (GstAllocator * allocator, GstMemory * gmem)
{
GstXvImageMemory *mem = (GstXvImageMemory *) gmem;
GstXvImageAllocator *alloc = (GstXvImageAllocator *) allocator;
GstXvContext *context;
if (gmem->parent)
goto sub_mem;
context = alloc->context;
GST_DEBUG_OBJECT (allocator, "free memory %p", mem);
g_mutex_lock (&context->lock);
#ifdef HAVE_XSHM
if (context->use_xshm) {
if (mem->SHMInfo.shmaddr != ((void *) -1)) {
GST_DEBUG_OBJECT (allocator, "XServer ShmDetaching from 0x%x id 0x%lx",
mem->SHMInfo.shmid, mem->SHMInfo.shmseg);
XShmDetach (context->disp, &mem->SHMInfo);
XSync (context->disp, FALSE);
shmdt (mem->SHMInfo.shmaddr);
mem->SHMInfo.shmaddr = (void *) -1;
}
if (mem->xvimage)
XFree (mem->xvimage);
} else
#endif /* HAVE_XSHM */
{
if (mem->xvimage) {
g_free (mem->xvimage->data);
XFree (mem->xvimage);
}
}
XSync (context->disp, FALSE);
g_mutex_unlock (&context->lock);
sub_mem:
g_slice_free (GstXvImageMemory, mem);
}
static gpointer
gst_xvimage_memory_map (GstXvImageMemory * mem, gsize maxsize,
GstMapFlags flags)
{
return mem->xvimage->data + mem->parent.offset;
}
static gboolean
gst_xvimage_memory_unmap (GstXvImageMemory * mem)
{
return TRUE;
}
static GstXvImageMemory *
gst_xvimage_memory_share (GstXvImageMemory * mem, gssize offset, gsize size)
{
GstXvImageMemory *sub;
GstMemory *parent;
/* We can only share the complete memory */
if (offset != 0)
return NULL;
if (size != -1 && size != mem->xvimage->data_size)
return NULL;
GST_DEBUG ("share memory %p", mem);
/* find the real parent */
if ((parent = mem->parent.parent) == NULL)
parent = (GstMemory *) mem;
if (size == -1)
size = mem->parent.size - offset;
/* the shared memory is always readonly */
sub = g_slice_new (GstXvImageMemory);
gst_memory_init (GST_MEMORY_CAST (sub), GST_MINI_OBJECT_FLAGS (parent) |
GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->parent.allocator,
&mem->parent, mem->parent.maxsize, mem->parent.align,
mem->parent.offset + offset, size);
sub->info = mem->info;
sub->im_format = mem->im_format;
sub->crop = mem->crop;
sub->xvimage = mem->xvimage;
#ifdef HAVE_XSHM
sub->SHMInfo = mem->SHMInfo;
#endif
return sub;
}
static GstXvImageMemory *
gst_xvimage_memory_copy (GstMemory * gmem, gssize offset, gsize size)
{
GstXvImageMemory *mem, *copy;
mem = (GstXvImageMemory *) gmem;
/* We can only copy the complete memory */
if (offset != 0)
return NULL;
if (size != -1 && size != mem->xvimage->data_size)
return NULL;
GST_DEBUG ("copy memory %p", mem);
copy = (GstXvImageMemory *)
gst_xvimage_allocator_alloc (GST_XVIMAGE_ALLOCATOR_CAST (gmem->allocator),
mem->im_format, &mem->info, mem->xvimage->width,
mem->xvimage->height, &mem->crop, NULL);
memcpy (copy->xvimage->data + copy->parent.offset,
mem->xvimage->data + mem->parent.offset, mem->xvimage->data_size);
return copy;
}
#define gst_xvimage_allocator_parent_class parent_class
G_DEFINE_TYPE (GstXvImageAllocator, gst_xvimage_allocator, GST_TYPE_ALLOCATOR);
static void gst_xvimage_allocator_finalize (GObject * object);
#define GST_XVIMAGE_ALLOCATOR_NAME "xvimage"
static void
gst_xvimage_allocator_class_init (GstXvImageAllocatorClass * klass)
{
GObjectClass *gobject_class;
GstAllocatorClass *allocator_class;
gobject_class = (GObjectClass *) klass;
allocator_class = (GstAllocatorClass *) klass;
gobject_class->finalize = gst_xvimage_allocator_finalize;
allocator_class->alloc = gst_xvimage_allocator_dummy_alloc;
allocator_class->free = gst_xvimage_allocator_free;
GST_DEBUG_CATEGORY_INIT (gst_debug_xvimageallocator, "xvimageallocator", 0,
"xvimageallocator object");
}
static void
gst_xvimage_allocator_init (GstXvImageAllocator * allocator)
{
GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
alloc->mem_type = GST_XVIMAGE_ALLOCATOR_NAME;
alloc->mem_map = (GstMemoryMapFunction) gst_xvimage_memory_map;
alloc->mem_unmap = (GstMemoryUnmapFunction) gst_xvimage_memory_unmap;
alloc->mem_share = (GstMemoryShareFunction) gst_xvimage_memory_share;
alloc->mem_copy = (GstMemoryShareFunction) gst_xvimage_memory_copy;
/* fallback is_span */
GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
}
static void
gst_xvimage_allocator_finalize (GObject * object)
{
GstXvImageAllocator *alloc = GST_XVIMAGE_ALLOCATOR (object);
GST_DEBUG_OBJECT (object, "finalize");
gst_xvcontext_unref (alloc->context);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
GstXvImageAllocator *
gst_xvimage_allocator_new (GstXvContext * context)
{
GstXvImageAllocator *alloc;
g_return_val_if_fail (GST_IS_XVCONTEXT (context), NULL);
alloc = g_object_new (GST_TYPE_XVIMAGE_ALLOCATOR, NULL);
alloc->context = gst_xvcontext_ref (context);
gst_object_ref_sink (alloc);
return alloc;
}
GstXvContext *
gst_xvimage_allocator_peek_context (GstXvImageAllocator * allocator)
{
g_return_val_if_fail (GST_IS_XVIMAGE_ALLOCATOR (allocator), NULL);
return allocator->context;
}
GstMemory *
gst_xvimage_allocator_alloc (GstXvImageAllocator * allocator, gint im_format,
const GstVideoInfo * info, gint padded_width, gint padded_height,
const GstVideoRectangle * crop, GError ** error)
{
int (*handler) (Display *, XErrorEvent *);
gboolean success = FALSE;
GstXvContext *context;
gint align, offset;
GstXvImageMemory *mem;
#ifdef HAVE_XSHM
gint expected_size = 0;
#endif
context = allocator->context;
mem = g_slice_new (GstXvImageMemory);
mem->im_format = im_format;
mem->info = *info;
#ifdef HAVE_XSHM
mem->SHMInfo.shmaddr = ((void *) -1);
mem->SHMInfo.shmid = -1;
#endif
mem->crop = *crop;
GST_DEBUG_OBJECT (allocator, "creating image %p (%dx%d) cropped %dx%d-%dx%d",
mem, padded_width, padded_height, crop->x, crop->y, crop->w, crop->h);
g_mutex_lock (&context->lock);
/* Setting an error handler to catch failure */
error_caught = FALSE;
handler = XSetErrorHandler (gst_xvimage_handle_xerror);
#ifdef HAVE_XSHM
if (context->use_xshm) {
mem->xvimage = XvShmCreateImage (context->disp,
context->xv_port_id, im_format, NULL, padded_width, padded_height,
&mem->SHMInfo);
if (!mem->xvimage || error_caught) {
g_mutex_unlock (&context->lock);
/* Reset error flag */
error_caught = FALSE;
/* Push a warning */
GST_WARNING_OBJECT (allocator,
"could not XShmCreateImage a %dx%d image", padded_width,
padded_height);
/* Retry without XShm */
context->use_xshm = FALSE;
/* Hold X mutex again to try without XShm */
g_mutex_lock (&context->lock);
goto no_xshm;
}
/* we have to use the returned data_size for our shm size */
GST_LOG_OBJECT (allocator, "XShm image size is %d",
mem->xvimage->data_size);
/* calculate the expected size. This is only for sanity checking the
* number we get from X. */
if (GST_VIDEO_FORMAT_INFO_IS_YUV (info->finfo)) {
switch (GST_VIDEO_FORMAT_INFO_FORMAT (info->finfo)) {
case GST_VIDEO_FORMAT_I420:
case GST_VIDEO_FORMAT_YV12:
{
gint pitches[3];
gint offsets[3];
guint plane;
offsets[0] = 0;
pitches[0] = GST_ROUND_UP_4 (padded_width);
offsets[1] = offsets[0] + pitches[0] * GST_ROUND_UP_2 (padded_height);
pitches[1] = GST_ROUND_UP_8 (padded_width) / 2;
offsets[2] =
offsets[1] + pitches[1] * GST_ROUND_UP_2 (padded_height) / 2;
pitches[2] = GST_ROUND_UP_8 (pitches[0]) / 2;
expected_size =
offsets[2] + pitches[2] * GST_ROUND_UP_2 (padded_height) / 2;
for (plane = 0; plane < mem->xvimage->num_planes; plane++) {
GST_DEBUG_OBJECT (allocator,
"Plane %u has a expected pitch of %d bytes, " "offset of %d",
plane, pitches[plane], offsets[plane]);
}
break;
}
case GST_VIDEO_FORMAT_YUY2:
case GST_VIDEO_FORMAT_UYVY:
expected_size = padded_height * GST_ROUND_UP_4 (padded_width * 2);
break;
default:
break;
}
} else if (GST_VIDEO_FORMAT_INFO_IS_RGB (info->finfo) &&
GST_VIDEO_FORMAT_INFO_N_PLANES (info->finfo) == 1) {
expected_size = padded_height * GST_ROUND_UP_4 (padded_width *
GST_VIDEO_FORMAT_INFO_PSTRIDE (info->finfo, 0));
}
if (expected_size != 0 && mem->xvimage->data_size < expected_size)
goto unexpected_size;
/* Be verbose about our XvImage stride */
{
guint plane;
for (plane = 0; plane < mem->xvimage->num_planes; plane++) {
GST_DEBUG_OBJECT (allocator, "Plane %u has a pitch of %d bytes, "
"offset of %d", plane, mem->xvimage->pitches[plane],
mem->xvimage->offsets[plane]);
}
}
/* get shared memory */
align = 0;
mem->SHMInfo.shmid =
shmget (IPC_PRIVATE, mem->xvimage->data_size, IPC_CREAT | 0777);
if (mem->SHMInfo.shmid == -1)
goto shmget_failed;
/* attach */
mem->SHMInfo.shmaddr = shmat (mem->SHMInfo.shmid, NULL, 0);
if (mem->SHMInfo.shmaddr == ((void *) -1))
goto shmat_failed;
/* now we can set up the image data */
mem->xvimage->data = mem->SHMInfo.shmaddr;
mem->SHMInfo.readOnly = FALSE;
if (XShmAttach (context->disp, &mem->SHMInfo) == 0)
goto xattach_failed;
XSync (context->disp, FALSE);
/* Delete the shared memory segment as soon as we everyone is attached.
* This way, it will be deleted as soon as we detach later, and not
* leaked if we crash. */
shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
GST_DEBUG_OBJECT (allocator, "XServer ShmAttached to 0x%x, id 0x%lx",
mem->SHMInfo.shmid, mem->SHMInfo.shmseg);
} else
no_xshm:
#endif /* HAVE_XSHM */
{
mem->xvimage = XvCreateImage (context->disp,
context->xv_port_id, im_format, NULL, padded_width, padded_height);
if (!mem->xvimage || error_caught)
goto create_failed;
/* we have to use the returned data_size for our image size */
align = 15; /* g_malloc aligns to 8, we need 16 */
mem->xvimage->data = g_malloc (mem->xvimage->data_size + align);
XSync (context->disp, FALSE);
}
if ((offset = ((guintptr) mem->xvimage->data & align)))
offset = (align + 1) - offset;
GST_DEBUG_OBJECT (allocator, "memory %p, align %d, offset %d",
mem->xvimage->data, align, offset);
/* Reset error handler */
error_caught = FALSE;
XSetErrorHandler (handler);
gst_memory_init (GST_MEMORY_CAST (mem), 0,
GST_ALLOCATOR_CAST (allocator), NULL, mem->xvimage->data_size + align,
align, offset, mem->xvimage->data_size);
g_mutex_unlock (&context->lock);
success = TRUE;
beach:
if (!success) {
g_slice_free (GstXvImageMemory, mem);
mem = NULL;
}
return GST_MEMORY_CAST (mem);
/* ERRORS */
create_failed:
{
g_mutex_unlock (&context->lock);
/* Reset error handler */
error_caught = FALSE;
XSetErrorHandler (handler);
/* Push an error */
g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
"could not XvShmCreateImage a %dx%d image", padded_width,
padded_height);
goto beach;
}
#ifdef HAVE_XSHM
unexpected_size:
{
g_mutex_unlock (&context->lock);
g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
"unexpected XShm image size (got %d, expected %d)",
mem->xvimage->data_size, expected_size);
goto beach;
}
shmget_failed:
{
g_mutex_unlock (&context->lock);
g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
"could not get shared memory of %d bytes", mem->xvimage->data_size);
goto beach;
}
shmat_failed:
{
g_mutex_unlock (&context->lock);
g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
"Failed to shmat: %s", g_strerror (errno));
/* Clean up the shared memory segment */
shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
goto beach;
}
xattach_failed:
{
/* Clean up the shared memory segment */
shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
g_mutex_unlock (&context->lock);
g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
"Failed to XShmAttach");
goto beach;
}
#endif
}
/* We are called with the x_lock taken */
static void
gst_xwindow_draw_borders (GstXWindow * window, GstVideoRectangle * rect)
{
gint t1, t2;
GstXvContext *context;
g_return_if_fail (window != NULL);
g_return_if_fail (rect != NULL);
context = window->context;
XSetForeground (context->disp, window->gc, context->black);
/* Left border */
if (rect->x > window->render_rect.x) {
XFillRectangle (context->disp, window->win, window->gc,
window->render_rect.x, window->render_rect.y,
rect->x - window->render_rect.x, window->render_rect.h);
}
/* Right border */
t1 = rect->x + rect->w;
t2 = window->render_rect.x + window->render_rect.w;
if (t1 < t2) {
XFillRectangle (context->disp, window->win, window->gc,
t1, window->render_rect.y, t2 - t1, window->render_rect.h);
}
/* Top border */
if (rect->y > window->render_rect.y) {
XFillRectangle (context->disp, window->win, window->gc,
window->render_rect.x, window->render_rect.y,
window->render_rect.w, rect->y - window->render_rect.y);
}
/* Bottom border */
t1 = rect->y + rect->h;
t2 = window->render_rect.y + window->render_rect.h;
if (t1 < t2) {
XFillRectangle (context->disp, window->win, window->gc,
window->render_rect.x, t1, window->render_rect.w, t2 - t1);
}
}
void
gst_xvimage_memory_render (GstXvImageMemory * mem, GstVideoRectangle * src_crop,
GstXWindow * window, GstVideoRectangle * dst_crop, gboolean draw_border)
{
GstXvContext *context;
XvImage *xvimage;
context = window->context;
g_mutex_lock (&context->lock);
xvimage = gst_xvimage_memory_get_xvimage (mem);
if (draw_border) {
gst_xwindow_draw_borders (window, dst_crop);
}
#ifdef HAVE_XSHM
if (context->use_xshm) {
GST_LOG ("XvShmPutImage with image %dx%d and window %dx%d, from xvimage %p",
src_crop->w, src_crop->h, window->render_rect.w, window->render_rect.h,
mem);
XvShmPutImage (context->disp,
context->xv_port_id,
window->win,
window->gc, xvimage,
src_crop->x, src_crop->y, src_crop->w, src_crop->h,
dst_crop->x, dst_crop->y, dst_crop->w, dst_crop->h, FALSE);
} else
#endif /* HAVE_XSHM */
{
XvPutImage (context->disp,
context->xv_port_id,
window->win,
window->gc, xvimage,
src_crop->x, src_crop->y, src_crop->w, src_crop->h,
dst_crop->x, dst_crop->y, dst_crop->w, dst_crop->h);
}
XSync (context->disp, FALSE);
g_mutex_unlock (&context->lock);
}
@@ -0,0 +1,70 @@
/* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_XVIMAGEALLOCATOR_H__
#define __GST_XVIMAGEALLOCATOR_H__
typedef struct _GstXvImageMemory GstXvImageMemory;
typedef struct _GstXvImageAllocator GstXvImageAllocator;
typedef struct _GstXvImageAllocatorClass GstXvImageAllocatorClass;
#include "xvcontext.h"
G_BEGIN_DECLS
/* allocator functions */
#define GST_TYPE_XVIMAGE_ALLOCATOR (gst_xvimage_allocator_get_type())
#define GST_IS_XVIMAGE_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_XVIMAGE_ALLOCATOR))
#define GST_XVIMAGE_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_XVIMAGE_ALLOCATOR, GstXvImageAllocator))
#define GST_XVIMAGE_ALLOCATOR_CAST(obj) ((GstXvImageAllocator*)(obj))
GType gst_xvimage_allocator_get_type (void);
/* the allocator */
GstXvImageAllocator * gst_xvimage_allocator_new (GstXvContext * context);
GstXvContext * gst_xvimage_allocator_peek_context (GstXvImageAllocator * allocator);
GstMemory * gst_xvimage_allocator_alloc (GstXvImageAllocator * allocator,
gint im_format,
const GstVideoInfo * info,
gint padded_width,
gint padded_height,
const GstVideoRectangle *crop,
GError ** error);
/* memory from the allocator */
gboolean gst_xvimage_memory_is_from_context (GstMemory *mem,
GstXvContext * context);
gint gst_xvimage_memory_get_format (GstXvImageMemory *mem);
XvImage * gst_xvimage_memory_get_xvimage (GstXvImageMemory *mem);
gboolean gst_xvimage_memory_get_crop (GstXvImageMemory *mem,
GstVideoRectangle *crop);
void gst_xvimage_memory_render (GstXvImageMemory *mem,
GstVideoRectangle *src_crop,
GstXWindow *window,
GstVideoRectangle *dst_crop,
gboolean draw_border);
G_END_DECLS
#endif /*__GST_XVIMAGEALLOCATOR_H__*/
@@ -0,0 +1,296 @@
/* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* Object header */
#include "xvimagepool.h"
#include "xvimageallocator.h"
/* Debugging category */
#include <gst/gstinfo.h>
/* Helper functions */
#include <gst/video/video.h>
#include <gst/video/gstvideometa.h>
#include <gst/video/gstvideopool.h>
GST_DEBUG_CATEGORY (gst_debug_xv_image_pool);
#define GST_CAT_DEFAULT gst_debug_xv_image_pool
/* bufferpool */
static void gst_xvimage_buffer_pool_finalize (GObject * object);
#define gst_xvimage_buffer_pool_parent_class parent_class
G_DEFINE_TYPE (GstXvImageBufferPool, gst_xvimage_buffer_pool,
GST_TYPE_BUFFER_POOL);
static const gchar **
xvimage_buffer_pool_get_options (GstBufferPool * pool)
{
static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT, NULL
};
return options;
}
static gboolean
xvimage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
{
GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
GstVideoInfo info;
GstCaps *caps;
guint size, min_buffers, max_buffers;
GstXvContext *context;
if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
&max_buffers))
goto wrong_config;
if (caps == NULL)
goto no_caps;
/* now parse the caps from the config */
if (!gst_video_info_from_caps (&info, caps))
goto wrong_caps;
GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
caps);
context = gst_xvimage_allocator_peek_context (xvpool->allocator);
xvpool->im_format = gst_xvcontext_get_format_from_info (context, &info);
if (xvpool->im_format == -1)
goto unknown_format;
if (xvpool->caps)
gst_caps_unref (xvpool->caps);
xvpool->caps = gst_caps_ref (caps);
/* enable metadata based on config of the pool */
xvpool->add_metavideo =
gst_buffer_pool_config_has_option (config,
GST_BUFFER_POOL_OPTION_VIDEO_META);
/* parse extra alignment info */
xvpool->need_alignment = gst_buffer_pool_config_has_option (config,
GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
if (xvpool->need_alignment) {
gst_buffer_pool_config_get_video_alignment (config, &xvpool->align);
GST_LOG_OBJECT (pool, "padding %u-%ux%u-%u", xvpool->align.padding_top,
xvpool->align.padding_left, xvpool->align.padding_right,
xvpool->align.padding_bottom);
/* do padding and alignment */
gst_video_info_align (&info, &xvpool->align);
gst_buffer_pool_config_set_video_alignment (config, &xvpool->align);
/* we need the video metadata too now */
xvpool->add_metavideo = TRUE;
} else {
gst_video_alignment_reset (&xvpool->align);
}
/* add the padding */
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;
/* 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;
}
}
gst_buffer_pool_config_set_params (config, caps, info.size, min_buffers,
max_buffers);
return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
/* 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;
}
unknown_format:
{
GST_WARNING_OBJECT (pool, "failed to get format from caps %"
GST_PTR_FORMAT, caps);
return FALSE;
}
}
/* This function handles GstXImageBuffer creation depending on XShm availability */
static GstFlowReturn
xvimage_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
GstBufferPoolAcquireParams * params)
{
GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
GstVideoInfo *info;
GstBuffer *xvimage;
GstMemory *mem;
GError *err = NULL;
info = &xvpool->info;
xvimage = gst_buffer_new ();
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);
}
if (mem == NULL) {
gst_buffer_unref (xvimage);
goto no_buffer;
}
gst_buffer_append_memory (xvimage, mem);
if (xvpool->add_metavideo) {
GstVideoMeta *meta;
GST_DEBUG_OBJECT (pool, "adding GstVideoMeta");
meta = gst_buffer_add_video_meta_full (xvimage, GST_VIDEO_FRAME_FLAG_NONE,
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);
gst_video_meta_set_alignment (meta, xvpool->align);
}
*buffer = xvimage;
return GST_FLOW_OK;
/* ERROR */
no_buffer:
{
GST_WARNING_OBJECT (pool, "can't create image: %s", err->message);
g_clear_error (&err);
return GST_FLOW_ERROR;
}
}
GstBufferPool *
gst_xvimage_buffer_pool_new (GstXvImageAllocator * allocator)
{
GstXvImageBufferPool *pool;
pool = g_object_new (GST_TYPE_XVIMAGE_BUFFER_POOL, NULL);
gst_object_ref_sink (pool);
pool->allocator = gst_object_ref (allocator);
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;
gstbufferpool_class->get_options = xvimage_buffer_pool_get_options;
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)
{
/* nothing to do here */
}
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);
if (pool->pre_alloc_mem)
gst_memory_unref (pool->pre_alloc_mem);
if (pool->caps)
gst_caps_unref (pool->caps);
if (pool->allocator)
gst_object_unref (pool->allocator);
G_OBJECT_CLASS (gst_xvimage_buffer_pool_parent_class)->finalize (object);
}
@@ -0,0 +1,69 @@
/* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_XVIMAGEPOOL_H__
#define __GST_XVIMAGEPOOL_H__
#include <gst/gst.h>
#include "xvimageallocator.h"
G_BEGIN_DECLS
typedef struct _GstXvImageBufferPool GstXvImageBufferPool;
typedef struct _GstXvImageBufferPoolClass GstXvImageBufferPoolClass;
/* buffer pool functions */
#define GST_TYPE_XVIMAGE_BUFFER_POOL (gst_xvimage_buffer_pool_get_type())
#define GST_IS_XVIMAGE_BUFFER_POOL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_XVIMAGE_BUFFER_POOL))
#define GST_XVIMAGE_BUFFER_POOL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_XVIMAGE_BUFFER_POOL, GstXvImageBufferPool))
#define GST_XVIMAGE_BUFFER_POOL_CAST(obj) ((GstXvImageBufferPool*)(obj))
struct _GstXvImageBufferPool
{
GstBufferPool bufferpool;
GstXvImageAllocator *allocator;
GstCaps *caps;
gint im_format;
GstVideoRectangle crop;
GstVideoInfo info;
GstVideoAlignment align;
guint padded_width;
guint padded_height;
gboolean add_metavideo;
gboolean need_alignment;
/* used for calculating actual size, stride, and offset */
GstMemory *pre_alloc_mem;
};
struct _GstXvImageBufferPoolClass
{
GstBufferPoolClass parent_class;
};
GType gst_xvimage_buffer_pool_get_type (void);
GstBufferPool * gst_xvimage_buffer_pool_new (GstXvImageAllocator *allocator);
G_END_DECLS
#endif /*__GST_XVIMAGEPOOL_H__*/
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,138 @@
/* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_XV_IMAGE_SINK_H__
#define __GST_XV_IMAGE_SINK_H__
#include <gst/video/gstvideosink.h>
/* Helper functions */
#include <gst/video/video.h>
G_BEGIN_DECLS
#define GST_TYPE_XV_IMAGE_SINK \
(gst_xv_image_sink_get_type())
#define GST_XV_IMAGE_SINK(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_XV_IMAGE_SINK, GstXvImageSink))
#define GST_XV_IMAGE_SINK_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_XV_IMAGE_SINK, GstXvImageSinkClass))
#define GST_IS_XV_IMAGE_SINK(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_XV_IMAGE_SINK))
#define GST_IS_XV_IMAGE_SINK_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_XV_IMAGE_SINK))
typedef struct _GstXvImageSink GstXvImageSink;
typedef struct _GstXvImageSinkClass GstXvImageSinkClass;
#include "xvimageallocator.h"
#include "xvimagepool.h"
#include "xvcontext.h"
/**
* GstXvImageSink:
* @display_name: the name of the Display we want to render to
* @xvcontext: our instance's #GstXvContext
* @xwindow: the #GstXWindow we are rendering to
* @cur_image: a reference to the last #GstXvImage that was put to @xwindow. It
* is used when Expose events are received to redraw the latest video frame
* @event_thread: a thread listening for events on @xwindow and handling them
* @running: used to inform @event_thread if it should run/shutdown
* @fps_n: the framerate fraction numerator
* @fps_d: the framerate fraction denominator
* @x_lock: used to protect X calls as we are not using the XLib in threaded
* mode
* @flow_lock: used to protect data flow routines from external calls such as
* events from @event_thread or methods from the #GstVideoOverlay interface
* @par: used to override calculated pixel aspect ratio from @xvcontext
* @pool_lock: used to protect the buffer pool
* @image_pool: a list of #GstXvImageBuffer that could be reused at next buffer
* allocation call
* @synchronous: used to store if XSynchronous should be used or not (for
* debugging purpose only)
* @keep_aspect: used to remember if reverse negotiation scaling should respect
* aspect ratio
* @handle_events: used to know if we should handle select XEvents or not
* @brightness: used to store the user settings for color balance brightness
* @contrast: used to store the user settings for color balance contrast
* @hue: used to store the user settings for color balance hue
* @saturation: used to store the user settings for color balance saturation
* @cb_changed: used to store if the color balance settings where changed
* @video_width: the width of incoming video frames in pixels
* @video_height: the height of incoming video frames in pixels
*
* The #GstXvImageSink data structure.
*/
struct _GstXvImageSink
{
/* Our element stuff */
GstVideoSink videosink;
GstXvContextConfig config;
GstXvContext *context;
GstXvImageAllocator *allocator;
GstXWindow *xwindow;
GstBuffer *cur_image;
GThread *event_thread;
gboolean running;
GstVideoInfo info;
/* Framerate numerator and denominator */
gint fps_n;
gint fps_d;
GMutex flow_lock;
/* object-set pixel aspect ratio */
GValue *par;
/* the buffer pool */
GstBufferPool *pool;
gboolean synchronous;
gboolean double_buffer;
gboolean keep_aspect;
gboolean redraw_border;
gboolean handle_events;
gboolean handle_expose;
/* size of incoming video, used as the size for XvImage */
guint video_width, video_height;
gboolean draw_borders;
/* stream metadata */
gchar *media_title;
/* saved render rectangle until we have a window */
gboolean pending_render_rect;
GstVideoRectangle render_rect;
};
struct _GstXvImageSinkClass
{
GstVideoSinkClass parent_class;
};
GType gst_xv_image_sink_get_type (void);
GST_ELEMENT_REGISTER_DECLARE (xvimagesink);
G_END_DECLS
#endif /* __GST_XV_IMAGE_SINK_H__ */