1 /*
2 * GStreamer
3 * Copyright (C) 2012 Matthew Waters <>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gstglbufferpool.h"
26
27 #include "gstglmemory.h"
28 #include "gstglsyncmeta.h"
29 #include "gstglutils.h"
30
31 /**
32 * SECTION:gstglbufferpool
33 * @title: GstGLBufferPool
34 * @short_description: buffer pool for #GstGLBaseMemory objects
35 * @see_also: #GstBufferPool, #GstGLBaseMemory, #GstGLMemory
36 *
37 * a #GstGLBufferPool is an object that allocates buffers with #GstGLBaseMemory
38 *
39 * A #GstGLBufferPool is created with gst_gl_buffer_pool_new()
40 *
41 * #GstGLBufferPool implements the VideoMeta buffer pool option
42 * %GST_BUFFER_POOL_OPTION_VIDEO_META, the VideoAligment buffer pool option
43 * %GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT as well as the OpenGL specific
44 * %GST_BUFFER_POOL_OPTION_GL_SYNC_META buffer pool option.
45 */
46
47 /* bufferpool */
48 struct _GstGLBufferPoolPrivate
49 {
50 GstAllocator *allocator;
51 GstGLVideoAllocationParams *gl_params;
52 GstCaps *caps;
53 gboolean add_videometa;
54 gboolean add_glsyncmeta;
55 };
56
57 static void gst_gl_buffer_pool_finalize (GObject * object);
58
59 GST_DEBUG_CATEGORY_STATIC (GST_CAT_GL_BUFFER_POOL);
60 #define GST_CAT_DEFAULT GST_CAT_GL_BUFFER_POOL
61
62 #define _init \
63 GST_DEBUG_CATEGORY_INIT (GST_CAT_GL_BUFFER_POOL, "glbufferpool", 0, \
64 "GL Buffer Pool");
65
66 #define gst_gl_buffer_pool_parent_class parent_class
67 G_DEFINE_TYPE_WITH_CODE (GstGLBufferPool, gst_gl_buffer_pool,
68 GST_TYPE_BUFFER_POOL, G_ADD_PRIVATE (GstGLBufferPool)
69 _init);
70
71 static const gchar **
gst_gl_buffer_pool_get_options(GstBufferPool * pool)72 gst_gl_buffer_pool_get_options (GstBufferPool * pool)
73 {
74 static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
75 GST_BUFFER_POOL_OPTION_GL_SYNC_META,
76 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT,
77 GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_2D,
78 GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_RECTANGLE,
79 NULL
80 };
81
82 return options;
83 }
84
85 static gboolean
gst_gl_buffer_pool_set_config(GstBufferPool * pool,GstStructure * config)86 gst_gl_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
87 {
88 GstGLBufferPool *glpool = GST_GL_BUFFER_POOL_CAST (pool);
89 GstGLBufferPoolPrivate *priv = glpool->priv;
90 GstVideoInfo info;
91 GstCaps *caps = NULL;
92 guint min_buffers, max_buffers;
93 guint max_align, n;
94 GstAllocator *allocator = NULL;
95 GstAllocationParams alloc_params;
96 GstGLTextureTarget tex_target;
97 gboolean ret = TRUE;
98 gint p;
99
100 if (!gst_buffer_pool_config_get_params (config, &caps, NULL, &min_buffers,
101 &max_buffers))
102 goto wrong_config;
103
104 if (caps == NULL)
105 goto no_caps;
106
107 /* now parse the caps from the config */
108 if (!gst_video_info_from_caps (&info, caps))
109 goto wrong_caps;
110
111 GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
112 caps);
113
114 if (!gst_buffer_pool_config_get_allocator (config, &allocator, &alloc_params))
115 goto wrong_config;
116
117 gst_caps_replace (&priv->caps, caps);
118
119 if (priv->allocator)
120 gst_object_unref (priv->allocator);
121
122 if (allocator) {
123 if (!GST_IS_GL_MEMORY_ALLOCATOR (allocator)) {
124 gst_object_unref (allocator);
125 goto wrong_allocator;
126 } else {
127 priv->allocator = gst_object_ref (allocator);
128 }
129 } else {
130 priv->allocator =
131 GST_ALLOCATOR (gst_gl_memory_allocator_get_default (glpool->context));
132 g_assert (priv->allocator);
133 }
134
135 priv->add_videometa = gst_buffer_pool_config_has_option (config,
136 GST_BUFFER_POOL_OPTION_VIDEO_META);
137 priv->add_glsyncmeta = gst_buffer_pool_config_has_option (config,
138 GST_BUFFER_POOL_OPTION_GL_SYNC_META);
139
140 if (priv->gl_params)
141 gst_gl_allocation_params_free ((GstGLAllocationParams *) priv->gl_params);
142 priv->gl_params = (GstGLVideoAllocationParams *)
143 gst_buffer_pool_config_get_gl_allocation_params (config);
144 if (!priv->gl_params)
145 priv->gl_params = gst_gl_video_allocation_params_new (glpool->context,
146 &alloc_params, &info, -1, NULL, 0, 0);
147
148 max_align = alloc_params.align;
149
150 if (gst_buffer_pool_config_has_option (config,
151 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT)) {
152 priv->add_videometa = TRUE;
153
154 gst_buffer_pool_config_get_video_alignment (config,
155 priv->gl_params->valign);
156
157 for (n = 0; n < GST_VIDEO_MAX_PLANES; ++n)
158 max_align |= priv->gl_params->valign->stride_align[n];
159
160 for (n = 0; n < GST_VIDEO_MAX_PLANES; ++n)
161 priv->gl_params->valign->stride_align[n] = max_align;
162
163 gst_video_info_align (priv->gl_params->v_info, priv->gl_params->valign);
164
165 gst_buffer_pool_config_set_video_alignment (config,
166 priv->gl_params->valign);
167 }
168
169 if (alloc_params.align < max_align) {
170 GST_WARNING_OBJECT (pool, "allocation params alignment %u is smaller "
171 "than the max specified video stride alignment %u, fixing",
172 (guint) alloc_params.align, max_align);
173
174 alloc_params.align = max_align;
175 gst_buffer_pool_config_set_allocator (config, allocator, &alloc_params);
176 if (priv->gl_params->parent.alloc_params)
177 gst_allocation_params_free (priv->gl_params->parent.alloc_params);
178 priv->gl_params->parent.alloc_params =
179 gst_allocation_params_copy (&alloc_params);
180 }
181
182 {
183 GstStructure *s = gst_caps_get_structure (caps, 0);
184 const gchar *target_str = gst_structure_get_string (s, "texture-target");
185 gboolean multiple_texture_targets = FALSE;
186
187 tex_target = priv->gl_params->target;
188 if (target_str)
189 tex_target = gst_gl_texture_target_from_string (target_str);
190
191 if (gst_buffer_pool_config_has_option (config,
192 GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_2D)) {
193 if (tex_target && tex_target != GST_GL_TEXTURE_TARGET_2D)
194 multiple_texture_targets = TRUE;
195 tex_target = GST_GL_TEXTURE_TARGET_2D;
196 }
197 if (gst_buffer_pool_config_has_option (config,
198 GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_RECTANGLE)) {
199 if (tex_target && tex_target != GST_GL_TEXTURE_TARGET_RECTANGLE)
200 multiple_texture_targets = TRUE;
201 tex_target = GST_GL_TEXTURE_TARGET_RECTANGLE;
202 }
203 if (gst_buffer_pool_config_has_option (config,
204 GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_EXTERNAL_OES)) {
205 if (tex_target && tex_target != GST_GL_TEXTURE_TARGET_EXTERNAL_OES)
206 multiple_texture_targets = TRUE;
207 tex_target = GST_GL_TEXTURE_TARGET_EXTERNAL_OES;
208 }
209
210 if (!tex_target)
211 tex_target = GST_GL_TEXTURE_TARGET_2D;
212
213 if (multiple_texture_targets) {
214 GST_WARNING_OBJECT (pool, "Multiple texture targets configured either "
215 "through caps or buffer pool options");
216 ret = FALSE;
217 }
218
219 priv->gl_params->target = tex_target;
220 }
221
222 /* Recalculate the size and offset as we don't add padding between planes. */
223 priv->gl_params->v_info->size = 0;
224 for (p = 0; p < GST_VIDEO_INFO_N_PLANES (priv->gl_params->v_info); p++) {
225 priv->gl_params->v_info->offset[p] = priv->gl_params->v_info->size;
226 priv->gl_params->v_info->size +=
227 gst_gl_get_plane_data_size (priv->gl_params->v_info,
228 priv->gl_params->valign, p);
229 }
230
231 gst_buffer_pool_config_set_params (config, caps,
232 priv->gl_params->v_info->size, min_buffers, max_buffers);
233
234 return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config) && ret;
235
236 /* ERRORS */
237 wrong_config:
238 {
239 GST_WARNING_OBJECT (pool, "invalid config");
240 return FALSE;
241 }
242 no_caps:
243 {
244 GST_WARNING_OBJECT (pool, "no caps in config");
245 return FALSE;
246 }
247 wrong_caps:
248 {
249 GST_WARNING_OBJECT (pool,
250 "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
251 return FALSE;
252 }
253 wrong_allocator:
254 {
255 GST_WARNING_OBJECT (pool, "Incorrect allocator type for this pool");
256 return FALSE;
257 }
258 }
259
260 static gboolean
gst_gl_buffer_pool_start(GstBufferPool * pool)261 gst_gl_buffer_pool_start (GstBufferPool * pool)
262 {
263 return GST_BUFFER_POOL_CLASS (parent_class)->start (pool);
264 }
265
266 /* This function handles GstBuffer creation */
267 static GstFlowReturn
gst_gl_buffer_pool_alloc(GstBufferPool * pool,GstBuffer ** buffer,GstBufferPoolAcquireParams * params)268 gst_gl_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
269 GstBufferPoolAcquireParams * params)
270 {
271 GstGLMemoryAllocator *alloc;
272 GstGLBufferPool *glpool = GST_GL_BUFFER_POOL_CAST (pool);
273 GstGLBufferPoolPrivate *priv = glpool->priv;
274 GstBuffer *buf;
275
276 if (!(buf = gst_buffer_new ())) {
277 goto no_buffer;
278 }
279
280 alloc = GST_GL_MEMORY_ALLOCATOR (priv->allocator);
281 if (!gst_gl_memory_setup_buffer (alloc, buf, priv->gl_params, NULL, NULL, 0))
282 goto mem_create_failed;
283
284 if (priv->add_glsyncmeta)
285 gst_buffer_add_gl_sync_meta (glpool->context, buf);
286
287 *buffer = buf;
288
289 return GST_FLOW_OK;
290
291 /* ERROR */
292 no_buffer:
293 {
294 GST_WARNING_OBJECT (pool, "can't create image");
295 return GST_FLOW_ERROR;
296 }
297 mem_create_failed:
298 {
299 GST_WARNING_OBJECT (pool, "Could not create GL Memory");
300 return GST_FLOW_ERROR;
301 }
302 }
303
304 /**
305 * gst_gl_buffer_pool_new:
306 * @context: the #GstGLContext to use
307 *
308 * Returns: a #GstBufferPool that allocates buffers with #GstGLMemory
309 */
310 GstBufferPool *
gst_gl_buffer_pool_new(GstGLContext * context)311 gst_gl_buffer_pool_new (GstGLContext * context)
312 {
313 GstGLBufferPool *pool;
314
315 pool = g_object_new (GST_TYPE_GL_BUFFER_POOL, NULL);
316 gst_object_ref_sink (pool);
317 pool->context = gst_object_ref (context);
318
319 GST_LOG_OBJECT (pool, "new GL buffer pool for context %" GST_PTR_FORMAT,
320 context);
321
322 return GST_BUFFER_POOL_CAST (pool);
323 }
324
325 static void
gst_gl_buffer_pool_class_init(GstGLBufferPoolClass * klass)326 gst_gl_buffer_pool_class_init (GstGLBufferPoolClass * klass)
327 {
328 GObjectClass *gobject_class = (GObjectClass *) klass;
329 GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
330
331 gobject_class->finalize = gst_gl_buffer_pool_finalize;
332
333 gstbufferpool_class->get_options = gst_gl_buffer_pool_get_options;
334 gstbufferpool_class->set_config = gst_gl_buffer_pool_set_config;
335 gstbufferpool_class->alloc_buffer = gst_gl_buffer_pool_alloc;
336 gstbufferpool_class->start = gst_gl_buffer_pool_start;
337 }
338
339 static void
gst_gl_buffer_pool_init(GstGLBufferPool * pool)340 gst_gl_buffer_pool_init (GstGLBufferPool * pool)
341 {
342 GstGLBufferPoolPrivate *priv = NULL;
343
344 pool->priv = gst_gl_buffer_pool_get_instance_private (pool);
345 priv = pool->priv;
346
347 priv->allocator = NULL;
348 priv->caps = NULL;
349 priv->add_videometa = TRUE;
350 priv->add_glsyncmeta = FALSE;
351 }
352
353 static void
gst_gl_buffer_pool_finalize(GObject * object)354 gst_gl_buffer_pool_finalize (GObject * object)
355 {
356 GstGLBufferPool *pool = GST_GL_BUFFER_POOL_CAST (object);
357 GstGLBufferPoolPrivate *priv = pool->priv;
358
359 GST_LOG_OBJECT (pool, "finalize GL buffer pool %p", pool);
360
361 if (priv->caps)
362 gst_caps_unref (priv->caps);
363
364 G_OBJECT_CLASS (gst_gl_buffer_pool_parent_class)->finalize (object);
365
366 /* only release the context once all our memory have been deleted */
367 if (pool->context) {
368 gst_object_unref (pool->context);
369 pool->context = NULL;
370 }
371
372 if (priv->allocator) {
373 gst_object_unref (priv->allocator);
374 priv->allocator = NULL;
375 }
376
377 if (priv->gl_params)
378 gst_gl_allocation_params_free ((GstGLAllocationParams *) priv->gl_params);
379 priv->gl_params = NULL;
380 }
381
382 /**
383 * gst_gl_buffer_pool_get_gl_allocation_params:
384 * @pool: the #GstGLBufferPool
385 *
386 * The returned #GstGLAllocationParams will by %NULL before the first successful
387 * call to gst_buffer_pool_set_config(). Subsequent successful calls to
388 * gst_buffer_pool_set_config() will cause this function to return a new
389 * #GstGLAllocationParams which may or may not contain the same information.
390 *
391 * Returns: (transfer full): a copy of the #GstGLAllocationParams being used by the @pool
392 *
393 * Since: 1.20
394 */
395 GstGLAllocationParams *
gst_gl_buffer_pool_get_gl_allocation_params(GstGLBufferPool * pool)396 gst_gl_buffer_pool_get_gl_allocation_params (GstGLBufferPool * pool)
397 {
398 g_return_val_if_fail (GST_IS_GL_BUFFER_POOL (pool), NULL);
399
400 if (pool->priv->gl_params)
401 return gst_gl_allocation_params_copy ((GstGLAllocationParams *) pool->
402 priv->gl_params);
403 else
404 return NULL;
405 }
406
407 /**
408 * gst_buffer_pool_config_get_gl_allocation_params:
409 * @config: a buffer pool config
410 *
411 * Returns: (transfer full): the currently set #GstGLAllocationParams or %NULL
412 */
413 GstGLAllocationParams *
gst_buffer_pool_config_get_gl_allocation_params(GstStructure * config)414 gst_buffer_pool_config_get_gl_allocation_params (GstStructure * config)
415 {
416 GstGLAllocationParams *ret;
417
418 if (!gst_structure_get (config, "gl-allocation-params",
419 GST_TYPE_GL_ALLOCATION_PARAMS, &ret, NULL))
420 ret = NULL;
421
422 return ret;
423 }
424
425 /**
426 * gst_buffer_pool_config_set_gl_allocation_params:
427 * @config: a buffer pool config
428 * @params: (transfer none): a #GstGLAllocationParams
429 *
430 * Sets @params on @config
431 */
432 void
gst_buffer_pool_config_set_gl_allocation_params(GstStructure * config,const GstGLAllocationParams * params)433 gst_buffer_pool_config_set_gl_allocation_params (GstStructure * config,
434 const GstGLAllocationParams * params)
435 {
436 g_return_if_fail (config != NULL);
437 g_return_if_fail (params != NULL);
438
439 gst_structure_set (config, "gl-allocation-params",
440 GST_TYPE_GL_ALLOCATION_PARAMS, params, NULL);
441 }
442