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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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 (g_once_init_enter (&xvimage_meta_info)) {
75 const GstMetaInfo *meta =
76 gst_meta_register (GST_XVIMAGE_META_API_TYPE, "GstXvImageMeta",
77 sizeof (GstXvImageMeta), (GstMetaInitFunction) NULL,
78 (GstMetaFreeFunction) gst_xvimage_meta_free,
79 (GstMetaTransformFunction) NULL);
80 g_once_init_leave (&xvimage_meta_info, meta);
82 return xvimage_meta_info;
86 static gboolean error_caught = FALSE;
89 gst_xvimagesink_handle_xerror (Display * display, XErrorEvent * xevent)
93 XGetErrorText (display, xevent->error_code, error_msg, 1024);
94 GST_DEBUG ("xvimagesink triggered an XError. error: %s", error_msg);
99 static GstXvImageMeta *
100 gst_buffer_add_xvimage_meta (GstBuffer * buffer, GstXvImageBufferPool * xvpool)
102 GstXvImageSink *xvimagesink;
103 int (*handler) (Display *, XErrorEvent *);
104 gboolean success = FALSE;
105 GstXContext *xcontext;
106 GstXvImageMeta *meta;
107 gint width, height, im_format, align = 15, offset;
108 GstXvImageBufferPoolPrivate *priv;
111 xvimagesink = xvpool->sink;
112 xcontext = xvimagesink->xcontext;
114 width = priv->padded_width;
115 height = priv->padded_height;
116 im_format = priv->im_format;
119 (GstXvImageMeta *) gst_buffer_add_meta (buffer, GST_XVIMAGE_META_INFO,
122 meta->SHMInfo.shmaddr = ((void *) -1);
123 meta->SHMInfo.shmid = -1;
125 meta->x = priv->align.padding_left;
126 meta->y = priv->align.padding_top;
127 meta->width = priv->info.width;
128 meta->height = priv->info.height;
129 meta->sink = gst_object_ref (xvimagesink);
130 meta->im_format = im_format;
132 GST_DEBUG_OBJECT (xvimagesink, "creating image %p (%dx%d)", buffer,
135 g_mutex_lock (&xvimagesink->x_lock);
137 /* Setting an error handler to catch failure */
138 error_caught = FALSE;
139 handler = XSetErrorHandler (gst_xvimagesink_handle_xerror);
142 if (xcontext->use_xshm) {
145 meta->xvimage = XvShmCreateImage (xcontext->disp,
146 xcontext->xv_port_id, im_format, NULL, width, height, &meta->SHMInfo);
147 if (!meta->xvimage || error_caught) {
148 g_mutex_unlock (&xvimagesink->x_lock);
150 /* Reset error flag */
151 error_caught = FALSE;
154 GST_ELEMENT_WARNING (xvimagesink, RESOURCE, WRITE,
155 ("Failed to create output image buffer of %dx%d pixels",
157 ("could not XShmCreateImage a %dx%d image", width, height));
159 /* Retry without XShm */
160 xvimagesink->xcontext->use_xshm = FALSE;
162 /* Hold X mutex again to try without XShm */
163 g_mutex_lock (&xvimagesink->x_lock);
167 /* we have to use the returned data_size for our shm size */
168 meta->size = meta->xvimage->data_size;
169 GST_LOG_OBJECT (xvimagesink, "XShm image size is %" G_GSIZE_FORMAT,
172 /* calculate the expected size. This is only for sanity checking the
173 * number we get from X. */
175 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
176 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
183 pitches[0] = GST_ROUND_UP_4 (width);
184 offsets[1] = offsets[0] + pitches[0] * GST_ROUND_UP_2 (height);
185 pitches[1] = GST_ROUND_UP_8 (width) / 2;
186 offsets[2] = offsets[1] + pitches[1] * GST_ROUND_UP_2 (height) / 2;
187 pitches[2] = GST_ROUND_UP_8 (pitches[0]) / 2;
189 expected_size = offsets[2] + pitches[2] * GST_ROUND_UP_2 (height) / 2;
191 for (plane = 0; plane < meta->xvimage->num_planes; plane++) {
192 GST_DEBUG_OBJECT (xvimagesink,
193 "Plane %u has a expected pitch of %d bytes, " "offset of %d",
194 plane, pitches[plane], offsets[plane]);
198 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
199 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
200 expected_size = height * GST_ROUND_UP_4 (width * 2);
206 if (expected_size != 0 && meta->size != expected_size) {
207 GST_WARNING_OBJECT (xvimagesink,
208 "unexpected XShm image size (got %" G_GSIZE_FORMAT ", expected %d)",
209 meta->size, expected_size);
212 /* Be verbose about our XvImage stride */
216 for (plane = 0; plane < meta->xvimage->num_planes; plane++) {
217 GST_DEBUG_OBJECT (xvimagesink, "Plane %u has a pitch of %d bytes, "
218 "offset of %d", plane, meta->xvimage->pitches[plane],
219 meta->xvimage->offsets[plane]);
223 /* get shared memory */
224 meta->SHMInfo.shmid =
225 shmget (IPC_PRIVATE, meta->size + align, IPC_CREAT | 0777);
226 if (meta->SHMInfo.shmid == -1)
230 meta->SHMInfo.shmaddr = shmat (meta->SHMInfo.shmid, NULL, 0);
231 if (meta->SHMInfo.shmaddr == ((void *) -1))
234 /* now we can set up the image data */
235 meta->xvimage->data = meta->SHMInfo.shmaddr;
236 meta->SHMInfo.readOnly = FALSE;
238 if (XShmAttach (xcontext->disp, &meta->SHMInfo) == 0)
241 XSync (xcontext->disp, FALSE);
243 /* Delete the shared memory segment as soon as we everyone is attached.
244 * This way, it will be deleted as soon as we detach later, and not
245 * leaked if we crash. */
246 shmctl (meta->SHMInfo.shmid, IPC_RMID, NULL);
248 GST_DEBUG_OBJECT (xvimagesink, "XServer ShmAttached to 0x%x, id 0x%lx",
249 meta->SHMInfo.shmid, meta->SHMInfo.shmseg);
252 #endif /* HAVE_XSHM */
254 meta->xvimage = XvCreateImage (xcontext->disp,
255 xcontext->xv_port_id, im_format, NULL, width, height);
256 if (!meta->xvimage || error_caught)
259 /* we have to use the returned data_size for our image size */
260 meta->size = meta->xvimage->data_size;
261 meta->xvimage->data = g_malloc (meta->size + align);
263 XSync (xcontext->disp, FALSE);
266 if ((offset = ((guintptr) meta->xvimage->data & align)))
267 offset = (align + 1) - offset;
269 GST_DEBUG_OBJECT (xvimagesink, "memory %p, align %d, offset %d",
270 meta->xvimage->data, align, offset);
272 /* Reset error handler */
273 error_caught = FALSE;
274 XSetErrorHandler (handler);
276 gst_buffer_append_memory (buffer,
277 gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE, meta->xvimage->data,
278 meta->size + align, offset, meta->size, NULL, NULL));
280 g_mutex_unlock (&xvimagesink->x_lock);
293 g_mutex_unlock (&xvimagesink->x_lock);
294 /* Reset error handler */
295 error_caught = FALSE;
296 XSetErrorHandler (handler);
298 GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
299 ("Failed to create output image buffer of %dx%d pixels",
301 ("could not XvShmCreateImage a %dx%d image", width, height));
307 g_mutex_unlock (&xvimagesink->x_lock);
308 GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
309 ("Failed to create output image buffer of %dx%d pixels",
311 ("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
317 g_mutex_unlock (&xvimagesink->x_lock);
318 GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
319 ("Failed to create output image buffer of %dx%d pixels",
320 width, height), ("Failed to shmat: %s", g_strerror (errno)));
321 /* Clean up the shared memory segment */
322 shmctl (meta->SHMInfo.shmid, IPC_RMID, NULL);
327 /* Clean up the shared memory segment */
328 shmctl (meta->SHMInfo.shmid, IPC_RMID, NULL);
329 g_mutex_unlock (&xvimagesink->x_lock);
331 GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
332 ("Failed to create output image buffer of %dx%d pixels",
333 width, height), ("Failed to XShmAttach"));
340 gst_xvimage_meta_free (GstXvImageMeta * meta, GstBuffer * buffer)
342 GstXvImageSink *xvimagesink;
344 xvimagesink = meta->sink;
346 GST_DEBUG_OBJECT (xvimagesink, "free meta on buffer %p", buffer);
348 /* Hold the object lock to ensure the XContext doesn't disappear */
349 GST_OBJECT_LOCK (xvimagesink);
350 /* We might have some buffers destroyed after changing state to NULL */
351 if (xvimagesink->xcontext == NULL) {
352 GST_DEBUG_OBJECT (xvimagesink, "Destroying XvImage after Xcontext");
354 /* Need to free the shared memory segment even if the x context
355 * was already cleaned up */
356 if (meta->SHMInfo.shmaddr != ((void *) -1)) {
357 shmdt (meta->SHMInfo.shmaddr);
361 XFree (meta->xvimage);
365 g_mutex_lock (&xvimagesink->x_lock);
368 if (xvimagesink->xcontext->use_xshm) {
369 if (meta->SHMInfo.shmaddr != ((void *) -1)) {
370 GST_DEBUG_OBJECT (xvimagesink, "XServer ShmDetaching from 0x%x id 0x%lx",
371 meta->SHMInfo.shmid, meta->SHMInfo.shmseg);
372 XShmDetach (xvimagesink->xcontext->disp, &meta->SHMInfo);
373 XSync (xvimagesink->xcontext->disp, FALSE);
374 shmdt (meta->SHMInfo.shmaddr);
375 meta->SHMInfo.shmaddr = (void *) -1;
378 XFree (meta->xvimage);
380 #endif /* HAVE_XSHM */
383 g_free (meta->xvimage->data);
384 XFree (meta->xvimage);
388 XSync (xvimagesink->xcontext->disp, FALSE);
390 g_mutex_unlock (&xvimagesink->x_lock);
393 GST_OBJECT_UNLOCK (xvimagesink);
395 gst_object_unref (meta->sink);
399 /* This function checks that it is actually really possible to create an image
402 gst_xvimagesink_check_xshm_calls (GstXvImageSink * xvimagesink,
403 GstXContext * xcontext)
406 XShmSegmentInfo SHMInfo;
408 int (*handler) (Display *, XErrorEvent *);
409 gboolean result = FALSE;
410 gboolean did_attach = FALSE;
412 g_return_val_if_fail (xcontext != NULL, FALSE);
414 /* Sync to ensure any older errors are already processed */
415 XSync (xcontext->disp, FALSE);
417 /* Set defaults so we don't free these later unnecessarily */
418 SHMInfo.shmaddr = ((void *) -1);
421 /* Setting an error handler to catch failure */
422 error_caught = FALSE;
423 handler = XSetErrorHandler (gst_xvimagesink_handle_xerror);
425 /* Trying to create a 1x1 picture */
426 GST_DEBUG ("XvShmCreateImage of 1x1");
427 xvimage = XvShmCreateImage (xcontext->disp, xcontext->xv_port_id,
428 xcontext->im_format, NULL, 1, 1, &SHMInfo);
430 /* Might cause an error, sync to ensure it is noticed */
431 XSync (xcontext->disp, FALSE);
432 if (!xvimage || error_caught) {
433 GST_WARNING ("could not XvShmCreateImage a 1x1 image");
436 size = xvimage->data_size;
438 SHMInfo.shmid = shmget (IPC_PRIVATE, size, IPC_CREAT | 0777);
439 if (SHMInfo.shmid == -1) {
440 GST_WARNING ("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
445 SHMInfo.shmaddr = shmat (SHMInfo.shmid, NULL, 0);
446 if (SHMInfo.shmaddr == ((void *) -1)) {
447 GST_WARNING ("Failed to shmat: %s", g_strerror (errno));
448 /* Clean up the shared memory segment */
449 shmctl (SHMInfo.shmid, IPC_RMID, NULL);
453 xvimage->data = SHMInfo.shmaddr;
454 SHMInfo.readOnly = FALSE;
456 if (XShmAttach (xcontext->disp, &SHMInfo) == 0) {
457 GST_WARNING ("Failed to XShmAttach");
458 /* Clean up the shared memory segment */
459 shmctl (SHMInfo.shmid, IPC_RMID, NULL);
463 /* Sync to ensure we see any errors we caused */
464 XSync (xcontext->disp, FALSE);
466 /* Delete the shared memory segment as soon as everyone is attached.
467 * This way, it will be deleted as soon as we detach later, and not
468 * leaked if we crash. */
469 shmctl (SHMInfo.shmid, IPC_RMID, NULL);
472 GST_DEBUG ("XServer ShmAttached to 0x%x, id 0x%lx", SHMInfo.shmid,
476 /* store whether we succeeded in result */
479 GST_WARNING ("MIT-SHM extension check failed at XShmAttach. "
480 "Not using shared memory.");
484 /* Sync to ensure we swallow any errors we caused and reset error_caught */
485 XSync (xcontext->disp, FALSE);
487 error_caught = FALSE;
488 XSetErrorHandler (handler);
491 GST_DEBUG ("XServer ShmDetaching from 0x%x id 0x%lx",
492 SHMInfo.shmid, SHMInfo.shmseg);
493 XShmDetach (xcontext->disp, &SHMInfo);
494 XSync (xcontext->disp, FALSE);
496 if (SHMInfo.shmaddr != ((void *) -1))
497 shmdt (SHMInfo.shmaddr);
502 #endif /* HAVE_XSHM */
505 static void gst_xvimage_buffer_pool_finalize (GObject * object);
507 #define GST_XVIMAGE_BUFFER_POOL_GET_PRIVATE(obj) \
508 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_XVIMAGE_BUFFER_POOL, GstXvImageBufferPoolPrivate))
510 #define gst_xvimage_buffer_pool_parent_class parent_class
511 G_DEFINE_TYPE (GstXvImageBufferPool, gst_xvimage_buffer_pool,
512 GST_TYPE_BUFFER_POOL);
514 static const gchar **
515 xvimage_buffer_pool_get_options (GstBufferPool * pool)
517 static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
518 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT, NULL
525 xvimage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
527 GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
528 GstXvImageBufferPoolPrivate *priv = xvpool->priv;
532 if (!gst_buffer_pool_config_get_params (config, &caps, NULL, NULL, NULL))
538 /* now parse the caps from the config */
539 if (!gst_video_info_from_caps (&info, caps))
542 GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
545 priv->im_format = gst_xvimagesink_get_format_from_info (xvpool->sink, &info);
546 if (priv->im_format == -1)
550 gst_caps_unref (priv->caps);
551 priv->caps = gst_caps_ref (caps);
553 /* enable metadata based on config of the pool */
554 priv->add_metavideo =
555 gst_buffer_pool_config_has_option (config,
556 GST_BUFFER_POOL_OPTION_VIDEO_META);
558 /* parse extra alignment info */
559 priv->need_alignment = gst_buffer_pool_config_has_option (config,
560 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
562 if (priv->need_alignment) {
563 gst_buffer_pool_config_get_video_alignment (config, &priv->align);
565 GST_LOG_OBJECT (pool, "padding %u-%ux%u-%u", priv->align.padding_top,
566 priv->align.padding_left, priv->align.padding_left,
567 priv->align.padding_bottom);
569 /* do padding and alignment */
570 gst_video_info_align (&info, &priv->align);
572 /* we need the video metadata too now */
573 priv->add_metavideo = TRUE;
575 gst_video_alignment_reset (&priv->align);
578 /* add the padding */
580 GST_VIDEO_INFO_WIDTH (&info) + priv->align.padding_left +
581 priv->align.padding_right;
582 priv->padded_height =
583 GST_VIDEO_INFO_HEIGHT (&info) + priv->align.padding_top +
584 priv->align.padding_bottom;
588 return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
593 GST_WARNING_OBJECT (pool, "invalid config");
598 GST_WARNING_OBJECT (pool, "no caps in config");
603 GST_WARNING_OBJECT (pool,
604 "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
609 GST_WARNING_OBJECT (xvpool->sink, "failed to get format from caps %"
610 GST_PTR_FORMAT, caps);
611 GST_ELEMENT_ERROR (xvpool->sink, RESOURCE, WRITE,
612 ("Failed to create output image buffer of %dx%d pixels",
613 priv->info.width, priv->info.height),
614 ("Invalid input caps %" GST_PTR_FORMAT, caps));
619 /* This function handles GstXImageBuffer creation depending on XShm availability */
621 xvimage_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
622 GstBufferPoolAcquireParams * params)
624 GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
625 GstXvImageBufferPoolPrivate *priv = xvpool->priv;
628 GstXvImageMeta *meta;
632 xvimage = gst_buffer_new ();
633 meta = gst_buffer_add_xvimage_meta (xvimage, xvpool);
635 gst_buffer_unref (xvimage);
639 if (priv->add_metavideo) {
640 GST_DEBUG_OBJECT (pool, "adding GstVideoMeta");
641 gst_buffer_add_video_meta_full (xvimage, GST_VIDEO_FRAME_FLAG_NONE,
642 GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
643 GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
644 info->offset, info->stride);
654 GST_WARNING_OBJECT (pool, "can't create image");
655 return GST_FLOW_ERROR;
660 gst_xvimage_buffer_pool_new (GstXvImageSink * xvimagesink)
662 GstXvImageBufferPool *pool;
664 g_return_val_if_fail (GST_IS_XVIMAGESINK (xvimagesink), NULL);
666 pool = g_object_new (GST_TYPE_XVIMAGE_BUFFER_POOL, NULL);
667 pool->sink = gst_object_ref (xvimagesink);
669 GST_LOG_OBJECT (pool, "new XvImage buffer pool %p", pool);
671 return GST_BUFFER_POOL_CAST (pool);
675 gst_xvimage_buffer_pool_class_init (GstXvImageBufferPoolClass * klass)
677 GObjectClass *gobject_class = (GObjectClass *) klass;
678 GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
680 g_type_class_add_private (klass, sizeof (GstXvImageBufferPoolPrivate));
682 gobject_class->finalize = gst_xvimage_buffer_pool_finalize;
684 gstbufferpool_class->get_options = xvimage_buffer_pool_get_options;
685 gstbufferpool_class->set_config = xvimage_buffer_pool_set_config;
686 gstbufferpool_class->alloc_buffer = xvimage_buffer_pool_alloc;
690 gst_xvimage_buffer_pool_init (GstXvImageBufferPool * pool)
692 pool->priv = GST_XVIMAGE_BUFFER_POOL_GET_PRIVATE (pool);
696 gst_xvimage_buffer_pool_finalize (GObject * object)
698 GstXvImageBufferPool *pool = GST_XVIMAGE_BUFFER_POOL_CAST (object);
699 GstXvImageBufferPoolPrivate *priv = pool->priv;
701 GST_LOG_OBJECT (pool, "finalize XvImage buffer pool %p", pool);
704 gst_caps_unref (priv->caps);
705 gst_object_unref (pool->sink);
707 G_OBJECT_CLASS (gst_xvimage_buffer_pool_parent_class)->finalize (object);
710 /* This function tries to get a format matching with a given caps in the
711 supported list of formats we generated in gst_xvimagesink_get_xv_support */
713 gst_xvimagesink_get_format_from_info (GstXvImageSink * xvimagesink,
718 g_return_val_if_fail (GST_IS_XVIMAGESINK (xvimagesink), 0);
720 list = xvimagesink->xcontext->formats_list;
723 GstXvImageFormat *format = list->data;
725 if (format && format->vformat == GST_VIDEO_INFO_FORMAT (info))
726 return format->format;
728 list = g_list_next (list);