1 /* GStreamer
2 * Copyright (C) <2005> Julien Moutte <julien@moutte.net>
3 *
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.
8 *
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.
13 *
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.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 /* Object header */
25 #include "xvimagepool.h"
26 #include "xvimageallocator.h"
27
28 /* Debugging category */
29 #include <gst/gstinfo.h>
30
31 /* Helper functions */
32 #include <gst/video/video.h>
33 #include <gst/video/gstvideometa.h>
34 #include <gst/video/gstvideopool.h>
35
36
37 GST_DEBUG_CATEGORY (gst_debug_xv_image_pool);
38 #define GST_CAT_DEFAULT gst_debug_xv_image_pool
39
40 /* bufferpool */
41 static void gst_xvimage_buffer_pool_finalize (GObject * object);
42
43 #define gst_xvimage_buffer_pool_parent_class parent_class
44 G_DEFINE_TYPE (GstXvImageBufferPool, gst_xvimage_buffer_pool,
45 GST_TYPE_BUFFER_POOL);
46
47 static const gchar **
xvimage_buffer_pool_get_options(GstBufferPool * pool)48 xvimage_buffer_pool_get_options (GstBufferPool * pool)
49 {
50 static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
51 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT, NULL
52 };
53
54 return options;
55 }
56
57 static gboolean
xvimage_buffer_pool_set_config(GstBufferPool * pool,GstStructure * config)58 xvimage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
59 {
60 GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
61 GstVideoInfo info;
62 GstCaps *caps;
63 guint size, min_buffers, max_buffers;
64 GstXvContext *context;
65
66 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
67 &max_buffers))
68 goto wrong_config;
69
70 if (caps == NULL)
71 goto no_caps;
72
73 /* now parse the caps from the config */
74 if (!gst_video_info_from_caps (&info, caps))
75 goto wrong_caps;
76
77 GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
78 caps);
79
80 context = gst_xvimage_allocator_peek_context (xvpool->allocator);
81
82 xvpool->im_format = gst_xvcontext_get_format_from_info (context, &info);
83 if (xvpool->im_format == -1)
84 goto unknown_format;
85
86 if (xvpool->caps)
87 gst_caps_unref (xvpool->caps);
88 xvpool->caps = gst_caps_ref (caps);
89
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);
94
95 /* parse extra alignment info */
96 xvpool->need_alignment = gst_buffer_pool_config_has_option (config,
97 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
98
99 if (xvpool->need_alignment) {
100 gst_buffer_pool_config_get_video_alignment (config, &xvpool->align);
101
102 GST_LOG_OBJECT (pool, "padding %u-%ux%u-%u", xvpool->align.padding_top,
103 xvpool->align.padding_left, xvpool->align.padding_right,
104 xvpool->align.padding_bottom);
105
106 /* do padding and alignment */
107 gst_video_info_align (&info, &xvpool->align);
108
109 gst_buffer_pool_config_set_video_alignment (config, &xvpool->align);
110
111 /* we need the video metadata too now */
112 xvpool->add_metavideo = TRUE;
113 } else {
114 gst_video_alignment_reset (&xvpool->align);
115 }
116
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;
124
125 xvpool->info = info;
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;
130
131 /* update offset, stride and size with actual xvimage buffer */
132 if (xvpool->pre_alloc_mem)
133 gst_memory_unref (xvpool->pre_alloc_mem);
134
135 xvpool->pre_alloc_mem = gst_xvimage_allocator_alloc (xvpool->allocator,
136 xvpool->im_format, &info, xvpool->padded_width,
137 xvpool->padded_height, &xvpool->crop, NULL);
138
139 if (!xvpool->pre_alloc_mem) {
140 GST_ERROR_OBJECT (pool, "couldn't allocate image");
141 gst_structure_free (config);
142 return FALSE;
143 } else {
144 gint i;
145 XvImage *img;
146
147 img = gst_xvimage_memory_get_xvimage ((GstXvImageMemory *)
148 xvpool->pre_alloc_mem);
149
150 info.size = img->data_size;
151
152 for (i = 0; i < img->num_planes; i++) {
153 info.stride[i] = img->pitches[i];
154 info.offset[i] = img->offsets[i];
155 }
156
157 if (!gst_video_info_is_equal (&xvpool->info, &info) ||
158 xvpool->info.size != info.size) {
159 GST_WARNING_OBJECT (pool, "different size, stride and/or offset, update");
160 xvpool->info = info;
161 }
162 }
163
164 gst_buffer_pool_config_set_params (config, caps, info.size, min_buffers,
165 max_buffers);
166
167 return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
168
169 /* ERRORS */
170 wrong_config:
171 {
172 GST_WARNING_OBJECT (pool, "invalid config");
173 return FALSE;
174 }
175 no_caps:
176 {
177 GST_WARNING_OBJECT (pool, "no caps in config");
178 return FALSE;
179 }
180 wrong_caps:
181 {
182 GST_WARNING_OBJECT (pool,
183 "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
184 return FALSE;
185 }
186 unknown_format:
187 {
188 GST_WARNING_OBJECT (pool, "failed to get format from caps %"
189 GST_PTR_FORMAT, caps);
190 return FALSE;
191 }
192 }
193
194 /* This function handles GstXImageBuffer creation depending on XShm availability */
195 static GstFlowReturn
xvimage_buffer_pool_alloc(GstBufferPool * pool,GstBuffer ** buffer,GstBufferPoolAcquireParams * params)196 xvimage_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
197 GstBufferPoolAcquireParams * params)
198 {
199 GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
200 GstVideoInfo *info;
201 GstBuffer *xvimage;
202 GstMemory *mem;
203 GError *err = NULL;
204
205 info = &xvpool->info;
206
207 xvimage = gst_buffer_new ();
208
209 if (xvpool->pre_alloc_mem) {
210 mem = xvpool->pre_alloc_mem;
211 xvpool->pre_alloc_mem = NULL;
212 } else {
213 mem = gst_xvimage_allocator_alloc (xvpool->allocator, xvpool->im_format,
214 info, xvpool->padded_width, xvpool->padded_height, &xvpool->crop, &err);
215 }
216
217 if (mem == NULL) {
218 gst_buffer_unref (xvimage);
219 goto no_buffer;
220 }
221 gst_buffer_append_memory (xvimage, mem);
222
223 if (xvpool->add_metavideo) {
224 GstVideoMeta *meta;
225
226 GST_DEBUG_OBJECT (pool, "adding GstVideoMeta");
227 meta = gst_buffer_add_video_meta_full (xvimage, GST_VIDEO_FRAME_FLAG_NONE,
228 GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
229 GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
230 info->offset, info->stride);
231
232 gst_video_meta_set_alignment (meta, xvpool->align);
233 }
234
235 *buffer = xvimage;
236
237 return GST_FLOW_OK;
238
239 /* ERROR */
240 no_buffer:
241 {
242 GST_WARNING_OBJECT (pool, "can't create image: %s", err->message);
243 g_clear_error (&err);
244 return GST_FLOW_ERROR;
245 }
246 }
247
248 GstBufferPool *
gst_xvimage_buffer_pool_new(GstXvImageAllocator * allocator)249 gst_xvimage_buffer_pool_new (GstXvImageAllocator * allocator)
250 {
251 GstXvImageBufferPool *pool;
252
253 pool = g_object_new (GST_TYPE_XVIMAGE_BUFFER_POOL, NULL);
254 gst_object_ref_sink (pool);
255 pool->allocator = gst_object_ref (allocator);
256
257 GST_LOG_OBJECT (pool, "new XvImage buffer pool %p", pool);
258
259 return GST_BUFFER_POOL_CAST (pool);
260 }
261
262 static void
gst_xvimage_buffer_pool_class_init(GstXvImageBufferPoolClass * klass)263 gst_xvimage_buffer_pool_class_init (GstXvImageBufferPoolClass * klass)
264 {
265 GObjectClass *gobject_class = (GObjectClass *) klass;
266 GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
267
268 gobject_class->finalize = gst_xvimage_buffer_pool_finalize;
269
270 gstbufferpool_class->get_options = xvimage_buffer_pool_get_options;
271 gstbufferpool_class->set_config = xvimage_buffer_pool_set_config;
272 gstbufferpool_class->alloc_buffer = xvimage_buffer_pool_alloc;
273 }
274
275 static void
gst_xvimage_buffer_pool_init(GstXvImageBufferPool * pool)276 gst_xvimage_buffer_pool_init (GstXvImageBufferPool * pool)
277 {
278 /* nothing to do here */
279 }
280
281 static void
gst_xvimage_buffer_pool_finalize(GObject * object)282 gst_xvimage_buffer_pool_finalize (GObject * object)
283 {
284 GstXvImageBufferPool *pool = GST_XVIMAGE_BUFFER_POOL_CAST (object);
285
286 GST_LOG_OBJECT (pool, "finalize XvImage buffer pool %p", pool);
287
288 if (pool->pre_alloc_mem)
289 gst_memory_unref (pool->pre_alloc_mem);
290 if (pool->caps)
291 gst_caps_unref (pool->caps);
292 if (pool->allocator)
293 gst_object_unref (pool->allocator);
294
295 G_OBJECT_CLASS (gst_xvimage_buffer_pool_parent_class)->finalize (object);
296 }
297