2 * Copyright (C) <2005> Julien Moutte <julien@moutte.net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
25 #include "xvimagesink.h"
27 /* Debugging category */
28 #include <gst/gstinfo.h>
30 /* Helper functions */
31 #include <gst/video/video.h>
32 #include <gst/video/gstvideometa.h>
33 #include <gst/video/gstvideopool.h>
36 GST_DEBUG_CATEGORY_EXTERN (gst_debug_xvimagepool);
37 #define GST_CAT_DEFAULT gst_debug_xvimagepool
40 struct _GstXvImageBufferPoolPrivate
45 GstVideoAlignment align;
48 gboolean add_metavideo;
49 gboolean need_alignment;
52 static void gst_xvimage_meta_free (GstXvImageMeta * meta, GstBuffer * buffer);
54 /* xvimage metadata */
56 gst_xvimage_meta_api_get_type (void)
58 static volatile GType type;
59 static const gchar *tags[] =
60 { "memory", "size", "colorspace", "orientation", NULL };
62 if (g_once_init_enter (&type)) {
63 GType _type = gst_meta_api_type_register ("GstXvImageMetaAPI", tags);
64 g_once_init_leave (&type, _type);
70 gst_xvimage_meta_get_info (void)
72 static const GstMetaInfo *xvimage_meta_info = NULL;
74 if (xvimage_meta_info == NULL) {
76 gst_meta_register (GST_XVIMAGE_META_API_TYPE, "GstXvImageMeta",
77 sizeof (GstXvImageMeta), (GstMetaInitFunction) NULL,
78 (GstMetaFreeFunction) gst_xvimage_meta_free,
79 (GstMetaTransformFunction) NULL);
81 return xvimage_meta_info;
85 static gboolean error_caught = FALSE;
88 gst_xvimagesink_handle_xerror (Display * display, XErrorEvent * xevent)
92 XGetErrorText (display, xevent->error_code, error_msg, 1024);
93 GST_DEBUG ("xvimagesink triggered an XError. error: %s", error_msg);
98 static GstXvImageMeta *
99 gst_buffer_add_xvimage_meta (GstBuffer * buffer, GstXvImageBufferPool * xvpool)
101 GstXvImageSink *xvimagesink;
102 int (*handler) (Display *, XErrorEvent *);
103 gboolean success = FALSE;
104 GstXContext *xcontext;
105 GstXvImageMeta *meta;
106 gint width, height, im_format;
107 GstXvImageBufferPoolPrivate *priv;
110 xvimagesink = xvpool->sink;
111 xcontext = xvimagesink->xcontext;
113 width = priv->padded_width;
114 height = priv->padded_height;
115 im_format = priv->im_format;
118 (GstXvImageMeta *) gst_buffer_add_meta (buffer, GST_XVIMAGE_META_INFO,
121 meta->SHMInfo.shmaddr = ((void *) -1);
122 meta->SHMInfo.shmid = -1;
124 meta->x = priv->align.padding_left;
125 meta->y = priv->align.padding_top;
126 meta->width = priv->info.width;
127 meta->height = priv->info.height;
128 meta->sink = gst_object_ref (xvimagesink);
129 meta->im_format = im_format;
131 GST_DEBUG_OBJECT (xvimagesink, "creating image %p (%dx%d)", buffer,
134 g_mutex_lock (xvimagesink->x_lock);
136 /* Setting an error handler to catch failure */
137 error_caught = FALSE;
138 handler = XSetErrorHandler (gst_xvimagesink_handle_xerror);
141 if (xcontext->use_xshm) {
144 meta->xvimage = XvShmCreateImage (xcontext->disp,
145 xcontext->xv_port_id, im_format, NULL, width, height, &meta->SHMInfo);
146 if (!meta->xvimage || error_caught) {
147 g_mutex_unlock (xvimagesink->x_lock);
149 /* Reset error flag */
150 error_caught = FALSE;
153 GST_ELEMENT_WARNING (xvimagesink, RESOURCE, WRITE,
154 ("Failed to create output image buffer of %dx%d pixels",
156 ("could not XShmCreateImage a %dx%d image", width, height));
158 /* Retry without XShm */
159 xvimagesink->xcontext->use_xshm = FALSE;
161 /* Hold X mutex again to try without XShm */
162 g_mutex_lock (xvimagesink->x_lock);
166 /* we have to use the returned data_size for our shm size */
167 meta->size = meta->xvimage->data_size;
168 GST_LOG_OBJECT (xvimagesink, "XShm image size is %" G_GSIZE_FORMAT,
171 /* calculate the expected size. This is only for sanity checking the
172 * number we get from X. */
174 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
175 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
182 pitches[0] = GST_ROUND_UP_4 (width);
183 offsets[1] = offsets[0] + pitches[0] * GST_ROUND_UP_2 (height);
184 pitches[1] = GST_ROUND_UP_8 (width) / 2;
185 offsets[2] = offsets[1] + pitches[1] * GST_ROUND_UP_2 (height) / 2;
186 pitches[2] = GST_ROUND_UP_8 (pitches[0]) / 2;
188 expected_size = offsets[2] + pitches[2] * GST_ROUND_UP_2 (height) / 2;
190 for (plane = 0; plane < meta->xvimage->num_planes; plane++) {
191 GST_DEBUG_OBJECT (xvimagesink,
192 "Plane %u has a expected pitch of %d bytes, " "offset of %d",
193 plane, pitches[plane], offsets[plane]);
197 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
198 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
199 expected_size = height * GST_ROUND_UP_4 (width * 2);
205 if (expected_size != 0 && meta->size != expected_size) {
206 GST_WARNING_OBJECT (xvimagesink,
207 "unexpected XShm image size (got %" G_GSIZE_FORMAT ", expected %d)",
208 meta->size, expected_size);
211 /* Be verbose about our XvImage stride */
215 for (plane = 0; plane < meta->xvimage->num_planes; plane++) {
216 GST_DEBUG_OBJECT (xvimagesink, "Plane %u has a pitch of %d bytes, "
217 "offset of %d", plane, meta->xvimage->pitches[plane],
218 meta->xvimage->offsets[plane]);
222 /* get shared memory */
223 meta->SHMInfo.shmid = shmget (IPC_PRIVATE, meta->size, IPC_CREAT | 0777);
224 if (meta->SHMInfo.shmid == -1)
228 meta->SHMInfo.shmaddr = shmat (meta->SHMInfo.shmid, NULL, 0);
229 if (meta->SHMInfo.shmaddr == ((void *) -1))
232 /* now we can set up the image data */
233 meta->xvimage->data = meta->SHMInfo.shmaddr;
234 meta->SHMInfo.readOnly = FALSE;
236 if (XShmAttach (xcontext->disp, &meta->SHMInfo) == 0)
239 XSync (xcontext->disp, FALSE);
241 /* Delete the shared memory segment as soon as we everyone is attached.
242 * This way, it will be deleted as soon as we detach later, and not
243 * leaked if we crash. */
244 shmctl (meta->SHMInfo.shmid, IPC_RMID, NULL);
246 GST_DEBUG_OBJECT (xvimagesink, "XServer ShmAttached to 0x%x, id 0x%lx",
247 meta->SHMInfo.shmid, meta->SHMInfo.shmseg);
250 #endif /* HAVE_XSHM */
252 meta->xvimage = XvCreateImage (xcontext->disp,
253 xcontext->xv_port_id, im_format, NULL, width, height);
254 if (!meta->xvimage || error_caught)
257 /* we have to use the returned data_size for our image size */
258 meta->size = meta->xvimage->data_size;
259 meta->xvimage->data = g_malloc (meta->size);
261 XSync (xcontext->disp, FALSE);
264 /* Reset error handler */
265 error_caught = FALSE;
266 XSetErrorHandler (handler);
268 gst_buffer_take_memory (buffer, -1,
269 gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE, meta->xvimage->data,
270 meta->size, 0, meta->size, NULL, NULL));
272 g_mutex_unlock (xvimagesink->x_lock);
285 g_mutex_unlock (xvimagesink->x_lock);
286 /* Reset error handler */
287 error_caught = FALSE;
288 XSetErrorHandler (handler);
290 GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
291 ("Failed to create output image buffer of %dx%d pixels",
293 ("could not XvShmCreateImage a %dx%d image", width, height));
298 g_mutex_unlock (xvimagesink->x_lock);
299 GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
300 ("Failed to create output image buffer of %dx%d pixels",
302 ("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
308 g_mutex_unlock (xvimagesink->x_lock);
309 GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
310 ("Failed to create output image buffer of %dx%d pixels",
311 width, height), ("Failed to shmat: %s", g_strerror (errno)));
312 /* Clean up the shared memory segment */
313 shmctl (meta->SHMInfo.shmid, IPC_RMID, NULL);
318 /* Clean up the shared memory segment */
319 shmctl (meta->SHMInfo.shmid, IPC_RMID, NULL);
320 g_mutex_unlock (xvimagesink->x_lock);
322 GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
323 ("Failed to create output image buffer of %dx%d pixels",
324 width, height), ("Failed to XShmAttach"));
330 gst_xvimage_meta_free (GstXvImageMeta * meta, GstBuffer * buffer)
332 GstXvImageSink *xvimagesink;
334 xvimagesink = meta->sink;
336 GST_DEBUG_OBJECT (xvimagesink, "free meta on buffer %p", buffer);
338 /* Hold the object lock to ensure the XContext doesn't disappear */
339 GST_OBJECT_LOCK (xvimagesink);
340 /* We might have some buffers destroyed after changing state to NULL */
341 if (xvimagesink->xcontext == NULL) {
342 GST_DEBUG_OBJECT (xvimagesink, "Destroying XvImage after Xcontext");
344 /* Need to free the shared memory segment even if the x context
345 * was already cleaned up */
346 if (meta->SHMInfo.shmaddr != ((void *) -1)) {
347 shmdt (meta->SHMInfo.shmaddr);
351 XFree (meta->xvimage);
355 g_mutex_lock (xvimagesink->x_lock);
358 if (xvimagesink->xcontext->use_xshm) {
359 if (meta->SHMInfo.shmaddr != ((void *) -1)) {
360 GST_DEBUG_OBJECT (xvimagesink, "XServer ShmDetaching from 0x%x id 0x%lx",
361 meta->SHMInfo.shmid, meta->SHMInfo.shmseg);
362 XShmDetach (xvimagesink->xcontext->disp, &meta->SHMInfo);
363 XSync (xvimagesink->xcontext->disp, FALSE);
364 shmdt (meta->SHMInfo.shmaddr);
365 meta->SHMInfo.shmaddr = (void *) -1;
368 XFree (meta->xvimage);
370 #endif /* HAVE_XSHM */
373 g_free (meta->xvimage->data);
374 XFree (meta->xvimage);
378 XSync (xvimagesink->xcontext->disp, FALSE);
380 g_mutex_unlock (xvimagesink->x_lock);
383 GST_OBJECT_UNLOCK (xvimagesink);
385 gst_object_unref (meta->sink);
389 /* This function checks that it is actually really possible to create an image
392 gst_xvimagesink_check_xshm_calls (GstXvImageSink * xvimagesink,
393 GstXContext * xcontext)
396 XShmSegmentInfo SHMInfo;
398 int (*handler) (Display *, XErrorEvent *);
399 gboolean result = FALSE;
400 gboolean did_attach = FALSE;
402 g_return_val_if_fail (xcontext != NULL, FALSE);
404 /* Sync to ensure any older errors are already processed */
405 XSync (xcontext->disp, FALSE);
407 /* Set defaults so we don't free these later unnecessarily */
408 SHMInfo.shmaddr = ((void *) -1);
411 /* Setting an error handler to catch failure */
412 error_caught = FALSE;
413 handler = XSetErrorHandler (gst_xvimagesink_handle_xerror);
415 /* Trying to create a 1x1 picture */
416 GST_DEBUG ("XvShmCreateImage of 1x1");
417 xvimage = XvShmCreateImage (xcontext->disp, xcontext->xv_port_id,
418 xcontext->im_format, NULL, 1, 1, &SHMInfo);
420 /* Might cause an error, sync to ensure it is noticed */
421 XSync (xcontext->disp, FALSE);
422 if (!xvimage || error_caught) {
423 GST_WARNING ("could not XvShmCreateImage a 1x1 image");
426 size = xvimage->data_size;
428 SHMInfo.shmid = shmget (IPC_PRIVATE, size, IPC_CREAT | 0777);
429 if (SHMInfo.shmid == -1) {
430 GST_WARNING ("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
435 SHMInfo.shmaddr = shmat (SHMInfo.shmid, NULL, 0);
436 if (SHMInfo.shmaddr == ((void *) -1)) {
437 GST_WARNING ("Failed to shmat: %s", g_strerror (errno));
438 /* Clean up the shared memory segment */
439 shmctl (SHMInfo.shmid, IPC_RMID, NULL);
443 xvimage->data = SHMInfo.shmaddr;
444 SHMInfo.readOnly = FALSE;
446 if (XShmAttach (xcontext->disp, &SHMInfo) == 0) {
447 GST_WARNING ("Failed to XShmAttach");
448 /* Clean up the shared memory segment */
449 shmctl (SHMInfo.shmid, IPC_RMID, NULL);
453 /* Sync to ensure we see any errors we caused */
454 XSync (xcontext->disp, FALSE);
456 /* Delete the shared memory segment as soon as everyone is attached.
457 * This way, it will be deleted as soon as we detach later, and not
458 * leaked if we crash. */
459 shmctl (SHMInfo.shmid, IPC_RMID, NULL);
462 GST_DEBUG ("XServer ShmAttached to 0x%x, id 0x%lx", SHMInfo.shmid,
466 /* store whether we succeeded in result */
469 GST_WARNING ("MIT-SHM extension check failed at XShmAttach. "
470 "Not using shared memory.");
474 /* Sync to ensure we swallow any errors we caused and reset error_caught */
475 XSync (xcontext->disp, FALSE);
477 error_caught = FALSE;
478 XSetErrorHandler (handler);
481 GST_DEBUG ("XServer ShmDetaching from 0x%x id 0x%lx",
482 SHMInfo.shmid, SHMInfo.shmseg);
483 XShmDetach (xcontext->disp, &SHMInfo);
484 XSync (xcontext->disp, FALSE);
486 if (SHMInfo.shmaddr != ((void *) -1))
487 shmdt (SHMInfo.shmaddr);
492 #endif /* HAVE_XSHM */
495 static void gst_xvimage_buffer_pool_finalize (GObject * object);
497 #define GST_XVIMAGE_BUFFER_POOL_GET_PRIVATE(obj) \
498 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_XVIMAGE_BUFFER_POOL, GstXvImageBufferPoolPrivate))
500 #define gst_xvimage_buffer_pool_parent_class parent_class
501 G_DEFINE_TYPE (GstXvImageBufferPool, gst_xvimage_buffer_pool,
502 GST_TYPE_BUFFER_POOL);
504 static const gchar **
505 xvimage_buffer_pool_get_options (GstBufferPool * pool)
507 static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
508 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT, NULL
515 xvimage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
517 GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
518 GstXvImageBufferPoolPrivate *priv = xvpool->priv;
522 if (!gst_buffer_pool_config_get (config, &caps, NULL, NULL, NULL, NULL, NULL,
529 /* now parse the caps from the config */
530 if (!gst_video_info_from_caps (&info, caps))
533 GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
536 priv->im_format = gst_xvimagesink_get_format_from_info (xvpool->sink, &info);
537 if (priv->im_format == -1)
541 gst_caps_unref (priv->caps);
542 priv->caps = gst_caps_copy (caps);
545 /* enable metadata based on config of the pool */
546 priv->add_metavideo =
547 gst_buffer_pool_config_has_option (config,
548 GST_BUFFER_POOL_OPTION_VIDEO_META);
550 /* parse extra alignment info */
551 priv->need_alignment = gst_buffer_pool_config_has_option (config,
552 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
554 if (priv->need_alignment) {
555 gst_buffer_pool_config_get_video_alignment (config, &priv->align);
557 GST_LOG_OBJECT (pool, "padding %u-%ux%u-%u", priv->align.padding_top,
558 priv->align.padding_left, priv->align.padding_left,
559 priv->align.padding_bottom);
561 /* we need the video metadata too now */
562 priv->add_metavideo = TRUE;
564 gst_video_alignment_reset (&priv->align);
567 /* add the padding */
569 GST_VIDEO_INFO_WIDTH (&info) + priv->align.padding_left +
570 priv->align.padding_right;
571 priv->padded_height =
572 GST_VIDEO_INFO_HEIGHT (&info) + priv->align.padding_top +
573 priv->align.padding_bottom;
575 return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
580 GST_WARNING_OBJECT (pool, "invalid config");
585 GST_WARNING_OBJECT (pool, "no caps in config");
590 GST_WARNING_OBJECT (pool,
591 "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
596 GST_WARNING_OBJECT (xvpool->sink, "failed to get format from caps %"
597 GST_PTR_FORMAT, caps);
598 GST_ELEMENT_ERROR (xvpool->sink, RESOURCE, WRITE,
599 ("Failed to create output image buffer of %dx%d pixels",
600 priv->info.width, priv->info.height),
601 ("Invalid input caps %" GST_PTR_FORMAT, caps));
606 /* This function handles GstXImageBuffer creation depending on XShm availability */
608 xvimage_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
609 GstBufferPoolAcquireParams * params)
611 GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
612 GstXvImageBufferPoolPrivate *priv = xvpool->priv;
615 GstXvImageMeta *meta;
619 xvimage = gst_buffer_new ();
620 meta = gst_buffer_add_xvimage_meta (xvimage, xvpool);
622 gst_buffer_unref (xvimage);
626 if (priv->add_metavideo) {
628 const GstVideoFormatInfo *vinfo = info->finfo;
631 GST_DEBUG_OBJECT (pool, "adding GstVideoMeta");
632 /* these are just the defaults for now */
633 meta = gst_buffer_add_video_meta (xvimage, 0, GST_VIDEO_INFO_FORMAT (info),
634 priv->padded_width, priv->padded_height);
636 if (priv->need_alignment) {
637 meta->width = GST_VIDEO_INFO_WIDTH (&priv->info);
638 meta->height = GST_VIDEO_INFO_HEIGHT (&priv->info);
640 /* FIXME, not quite correct, NV12 would apply the vedge twice on the second
642 for (i = 0; i < GST_VIDEO_INFO_N_COMPONENTS (info); i++) {
643 gint vedge, hedge, plane;
646 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (vinfo, i,
647 priv->align.padding_left);
649 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vinfo, i,
650 priv->align.padding_top);
651 plane = GST_VIDEO_FORMAT_INFO_PLANE (vinfo, i);
653 GST_LOG_OBJECT (pool, "comp %d, plane %d: hedge %d, vedge %d", i,
654 plane, hedge, vedge);
656 meta->offset[plane] += (vedge * meta->stride[plane]) + hedge;
668 GST_WARNING_OBJECT (pool, "can't create image");
669 return GST_FLOW_ERROR;
674 gst_xvimage_buffer_pool_new (GstXvImageSink * xvimagesink)
676 GstXvImageBufferPool *pool;
678 g_return_val_if_fail (GST_IS_XVIMAGESINK (xvimagesink), NULL);
680 pool = g_object_new (GST_TYPE_XVIMAGE_BUFFER_POOL, NULL);
681 pool->sink = gst_object_ref (xvimagesink);
683 GST_LOG_OBJECT (pool, "new XvImage buffer pool %p", pool);
685 return GST_BUFFER_POOL_CAST (pool);
689 gst_xvimage_buffer_pool_class_init (GstXvImageBufferPoolClass * klass)
691 GObjectClass *gobject_class = (GObjectClass *) klass;
692 GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
694 g_type_class_add_private (klass, sizeof (GstXvImageBufferPoolPrivate));
696 gobject_class->finalize = gst_xvimage_buffer_pool_finalize;
698 gstbufferpool_class->get_options = xvimage_buffer_pool_get_options;
699 gstbufferpool_class->set_config = xvimage_buffer_pool_set_config;
700 gstbufferpool_class->alloc_buffer = xvimage_buffer_pool_alloc;
704 gst_xvimage_buffer_pool_init (GstXvImageBufferPool * pool)
706 pool->priv = GST_XVIMAGE_BUFFER_POOL_GET_PRIVATE (pool);
710 gst_xvimage_buffer_pool_finalize (GObject * object)
712 GstXvImageBufferPool *pool = GST_XVIMAGE_BUFFER_POOL_CAST (object);
713 GstXvImageBufferPoolPrivate *priv = pool->priv;
715 GST_LOG_OBJECT (pool, "finalize XvImage buffer pool %p", pool);
718 gst_caps_unref (priv->caps);
719 gst_object_unref (pool->sink);
721 G_OBJECT_CLASS (gst_xvimage_buffer_pool_parent_class)->finalize (object);
724 /* This function tries to get a format matching with a given caps in the
725 supported list of formats we generated in gst_xvimagesink_get_xv_support */
727 gst_xvimagesink_get_format_from_info (GstXvImageSink * xvimagesink,
732 g_return_val_if_fail (GST_IS_XVIMAGESINK (xvimagesink), 0);
734 list = xvimagesink->xcontext->formats_list;
737 GstXvImageFormat *format = list->data;
739 if (format && format->vformat == GST_VIDEO_INFO_FORMAT (info))
740 return format->format;
742 list = g_list_next (list);