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 "xvimagepool.h"
26 #include "xvimageallocator.h"
28 /* Debugging category */
29 #include <gst/gstinfo.h>
31 /* Helper functions */
32 #include <gst/video/video.h>
33 #include <gst/video/gstvideometa.h>
34 #include <gst/video/gstvideopool.h>
37 GST_DEBUG_CATEGORY_EXTERN (gst_debug_xv_image_pool);
38 #define GST_CAT_DEFAULT gst_debug_xv_image_pool
41 static void gst_xvimage_buffer_pool_finalize (GObject * object);
43 #define gst_xvimage_buffer_pool_parent_class parent_class
44 G_DEFINE_TYPE (GstXvImageBufferPool, gst_xvimage_buffer_pool,
45 GST_TYPE_BUFFER_POOL);
48 xvimage_buffer_pool_get_options (GstBufferPool * pool)
50 static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
51 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT, NULL
58 xvimage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
60 GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
63 guint size, min_buffers, max_buffers;
64 GstXvContext *context;
66 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
73 /* now parse the caps from the config */
74 if (!gst_video_info_from_caps (&info, caps))
77 GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
80 context = gst_xvimage_allocator_peek_context (xvpool->allocator);
82 xvpool->im_format = gst_xvcontext_get_format_from_info (context, &info);
83 if (xvpool->im_format == -1)
87 gst_caps_unref (xvpool->caps);
88 xvpool->caps = gst_caps_ref (caps);
90 /* enable metadata based on config of the pool */
91 xvpool->add_metavideo =
92 gst_buffer_pool_config_has_option (config,
93 GST_BUFFER_POOL_OPTION_VIDEO_META);
95 /* parse extra alignment info */
96 xvpool->need_alignment = gst_buffer_pool_config_has_option (config,
97 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
99 if (xvpool->need_alignment) {
100 gst_buffer_pool_config_get_video_alignment (config, &xvpool->align);
102 GST_LOG_OBJECT (pool, "padding %u-%ux%u-%u", xvpool->align.padding_top,
103 xvpool->align.padding_left, xvpool->align.padding_left,
104 xvpool->align.padding_bottom);
106 /* do padding and alignment */
107 gst_video_info_align (&info, &xvpool->align);
109 gst_buffer_pool_config_set_video_alignment (config, &xvpool->align);
111 /* we need the video metadata too now */
112 xvpool->add_metavideo = TRUE;
114 gst_video_alignment_reset (&xvpool->align);
117 /* add the padding */
118 xvpool->padded_width =
119 GST_VIDEO_INFO_WIDTH (&info) + xvpool->align.padding_left +
120 xvpool->align.padding_right;
121 xvpool->padded_height =
122 GST_VIDEO_INFO_HEIGHT (&info) + xvpool->align.padding_top +
123 xvpool->align.padding_bottom;
126 xvpool->crop.x = xvpool->align.padding_left;
127 xvpool->crop.y = xvpool->align.padding_top;
128 xvpool->crop.w = xvpool->info.width;
129 xvpool->crop.h = xvpool->info.height;
131 gst_buffer_pool_config_set_params (config, caps, info.size, min_buffers,
134 return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
139 GST_WARNING_OBJECT (pool, "invalid config");
144 GST_WARNING_OBJECT (pool, "no caps in config");
149 GST_WARNING_OBJECT (pool,
150 "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
155 GST_WARNING_OBJECT (pool, "failed to get format from caps %"
156 GST_PTR_FORMAT, caps);
161 /* This function handles GstXImageBuffer creation depending on XShm availability */
163 xvimage_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
164 GstBufferPoolAcquireParams * params)
166 GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
171 info = &xvpool->info;
173 xvimage = gst_buffer_new ();
175 mem = gst_xvimage_allocator_alloc (xvpool->allocator, xvpool->im_format,
176 xvpool->padded_width, xvpool->padded_height, &xvpool->crop, NULL);
179 gst_buffer_unref (xvimage);
182 gst_buffer_append_memory (xvimage, mem);
184 if (xvpool->add_metavideo) {
185 GST_DEBUG_OBJECT (pool, "adding GstVideoMeta");
186 gst_buffer_add_video_meta_full (xvimage, GST_VIDEO_FRAME_FLAG_NONE,
187 GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
188 GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
189 info->offset, info->stride);
199 GST_WARNING_OBJECT (pool, "can't create image");
200 return GST_FLOW_ERROR;
205 gst_xvimage_buffer_pool_new (GstXvImageAllocator * allocator)
207 GstXvImageBufferPool *pool;
209 pool = g_object_new (GST_TYPE_XVIMAGE_BUFFER_POOL, NULL);
210 pool->allocator = gst_object_ref (allocator);
212 GST_LOG_OBJECT (pool, "new XvImage buffer pool %p", pool);
214 return GST_BUFFER_POOL_CAST (pool);
218 gst_xvimage_buffer_pool_class_init (GstXvImageBufferPoolClass * klass)
220 GObjectClass *gobject_class = (GObjectClass *) klass;
221 GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
223 gobject_class->finalize = gst_xvimage_buffer_pool_finalize;
225 gstbufferpool_class->get_options = xvimage_buffer_pool_get_options;
226 gstbufferpool_class->set_config = xvimage_buffer_pool_set_config;
227 gstbufferpool_class->alloc_buffer = xvimage_buffer_pool_alloc;
231 gst_xvimage_buffer_pool_init (GstXvImageBufferPool * pool)
233 /* nothing to do here */
237 gst_xvimage_buffer_pool_finalize (GObject * object)
239 GstXvImageBufferPool *pool = GST_XVIMAGE_BUFFER_POOL_CAST (object);
241 GST_LOG_OBJECT (pool, "finalize XvImage buffer pool %p", pool);
244 gst_caps_unref (pool->caps);
246 gst_object_unref (pool->allocator);
248 G_OBJECT_CLASS (gst_xvimage_buffer_pool_parent_class)->finalize (object);