1 /* GStreamer Intel MSDK plugin
2 * Copyright (c) 2018, Intel Corporation
3 * Copyright (c) 2018, Igalia S.L.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * 3. Neither the name of the copyright holder nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGDECE
29 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "gstmsdkbufferpool.h"
34 #include "gstmsdksystemmemory.h"
35 #include "gstmsdkvideomemory.h"
37 #include "gstmsdkallocator_libva.h"
40 GST_DEBUG_CATEGORY_STATIC (gst_debug_msdkbufferpool);
41 #define GST_CAT_DEFAULT gst_debug_msdkbufferpool
43 typedef enum _GstMsdkMemoryType
45 GST_MSDK_MEMORY_TYPE_SYSTEM,
46 GST_MSDK_MEMORY_TYPE_VIDEO,
47 GST_MSDK_MEMORY_TYPE_DMABUF,
50 struct _GstMsdkBufferPoolPrivate
52 GstMsdkContext *context;
53 GstAllocator *allocator;
54 mfxFrameAllocResponse *alloc_response;
55 GstMsdkMemoryType memory_type;
56 gboolean add_videometa;
59 #define gst_msdk_buffer_pool_parent_class parent_class
60 G_DEFINE_TYPE_WITH_CODE (GstMsdkBufferPool, gst_msdk_buffer_pool,
61 GST_TYPE_VIDEO_BUFFER_POOL, G_ADD_PRIVATE (GstMsdkBufferPool)
62 GST_DEBUG_CATEGORY_INIT (gst_debug_msdkbufferpool, "msdkbufferpool", 0,
66 gst_msdk_buffer_pool_get_options (GstBufferPool * pool)
68 static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
69 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT,
70 GST_BUFFER_POOL_OPTION_MSDK_USE_VIDEO_MEMORY,
71 GST_BUFFER_POOL_OPTION_MSDK_USE_DMABUF,
78 static inline GstMsdkMemoryType
79 _msdk_get_memory_type (GstStructure * config)
81 gboolean video, dmabuf;
83 video = gst_buffer_pool_config_has_option (config,
84 GST_BUFFER_POOL_OPTION_MSDK_USE_VIDEO_MEMORY);
85 dmabuf = gst_buffer_pool_config_has_option (config,
86 GST_BUFFER_POOL_OPTION_MSDK_USE_DMABUF);
89 return GST_MSDK_MEMORY_TYPE_VIDEO;
91 return GST_MSDK_MEMORY_TYPE_DMABUF;
93 GST_WARNING ("Can't use DMAbuf since it's system msdk bufferpool");
94 return GST_MSDK_MEMORY_TYPE_SYSTEM;
98 gst_msdk_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
100 GstMsdkBufferPool *msdk_pool = GST_MSDK_BUFFER_POOL_CAST (pool);
101 GstMsdkBufferPoolPrivate *priv = msdk_pool->priv;
102 GstCaps *caps = NULL;
103 GstAllocator *allocator = NULL;
104 GstVideoInfo video_info;
105 guint size, min_buffers, max_buffers;
107 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
109 goto error_invalid_config;
114 if (!gst_video_info_from_caps (&video_info, caps))
115 goto error_invalid_caps;
117 if (!gst_buffer_pool_config_get_allocator (config, &allocator, NULL))
118 goto error_invalid_allocator;
121 && (g_strcmp0 (allocator->mem_type, GST_MSDK_SYSTEM_MEMORY_NAME) != 0
122 && g_strcmp0 (allocator->mem_type, GST_MSDK_VIDEO_MEMORY_NAME) != 0
123 && g_strcmp0 (allocator->mem_type,
124 GST_MSDK_DMABUF_MEMORY_NAME) != 0)) {
125 GST_INFO_OBJECT (pool,
126 "This is not MSDK allocator. So this will be ignored");
127 gst_object_unref (allocator);
131 priv->add_videometa = gst_buffer_pool_config_has_option (config,
132 GST_BUFFER_POOL_OPTION_VIDEO_META);
134 if (priv->add_videometa && gst_buffer_pool_config_has_option (config,
135 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT)) {
136 GstVideoAlignment alignment;
138 gst_msdk_set_video_alignment (&video_info, 0, 0, &alignment);
139 gst_video_info_align (&video_info, &alignment);
140 gst_buffer_pool_config_set_video_alignment (config, &alignment);
143 priv->memory_type = _msdk_get_memory_type (config);
144 if (((priv->memory_type == GST_MSDK_MEMORY_TYPE_VIDEO)
145 || (priv->memory_type == GST_MSDK_MEMORY_TYPE_DMABUF))
146 && (!priv->context || !priv->alloc_response)) {
147 GST_ERROR_OBJECT (pool,
148 "No MSDK context or Allocation response for using video memory");
149 goto error_invalid_config;
152 /* create a new allocator if needed */
154 GstAllocationParams params = { 0, 31, 0, 0, };
156 if (priv->memory_type == GST_MSDK_MEMORY_TYPE_DMABUF)
158 gst_msdk_dmabuf_allocator_new (priv->context, &video_info,
159 priv->alloc_response);
160 else if (priv->memory_type == GST_MSDK_MEMORY_TYPE_VIDEO)
162 gst_msdk_video_allocator_new (priv->context, &video_info,
163 priv->alloc_response);
165 allocator = gst_msdk_system_allocator_new (&video_info);
168 goto error_no_allocator;
170 GST_INFO_OBJECT (pool, "created new allocator %" GST_PTR_FORMAT, allocator);
172 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
173 gst_object_unref (allocator);
177 gst_object_unref (priv->allocator);
178 priv->allocator = gst_object_ref (allocator);
180 return GST_BUFFER_POOL_CLASS
181 (gst_msdk_buffer_pool_parent_class)->set_config (pool, config);
183 error_invalid_config:
185 GST_ERROR_OBJECT (pool, "invalid config");
190 GST_ERROR_OBJECT (pool, "no caps in config");
195 GST_ERROR_OBJECT (pool, "invalid caps %" GST_PTR_FORMAT, caps);
198 error_invalid_allocator:
200 GST_ERROR_OBJECT (pool, "no allocator in config");
205 GST_ERROR_OBJECT (pool, "no allocator defined");
211 gst_msdk_buffer_pool_alloc_buffer (GstBufferPool * pool,
212 GstBuffer ** out_buffer_ptr, GstBufferPoolAcquireParams * params)
214 GstMsdkBufferPool *msdk_pool = GST_MSDK_BUFFER_POOL_CAST (pool);
215 GstMsdkBufferPoolPrivate *priv = msdk_pool->priv;
219 buf = gst_buffer_new ();
221 if (priv->memory_type == GST_MSDK_MEMORY_TYPE_DMABUF)
222 mem = gst_msdk_dmabuf_memory_new (priv->allocator);
223 else if (priv->memory_type == GST_MSDK_MEMORY_TYPE_VIDEO)
224 mem = gst_msdk_video_memory_new (priv->allocator);
226 mem = gst_msdk_system_memory_new (priv->allocator);
231 gst_buffer_append_memory (buf, mem);
233 if (priv->add_videometa) {
237 if (priv->memory_type == GST_MSDK_MEMORY_TYPE_DMABUF)
238 info = &GST_MSDK_DMABUF_ALLOCATOR_CAST (priv->allocator)->image_info;
239 else if (priv->memory_type == GST_MSDK_MEMORY_TYPE_VIDEO)
240 info = &GST_MSDK_VIDEO_ALLOCATOR_CAST (priv->allocator)->image_info;
242 info = &GST_MSDK_SYSTEM_ALLOCATOR_CAST (priv->allocator)->image_info;
244 vmeta = gst_buffer_add_video_meta_full (buf, GST_VIDEO_FRAME_FLAG_NONE,
245 GST_VIDEO_INFO_FORMAT (info),
246 GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info),
247 GST_VIDEO_INFO_N_PLANES (info), info->offset, info->stride);
249 if (priv->memory_type == GST_MSDK_MEMORY_TYPE_VIDEO) {
250 vmeta->map = gst_video_meta_map_msdk_memory;
251 vmeta->unmap = gst_video_meta_unmap_msdk_memory;
255 *out_buffer_ptr = buf;
260 GST_ERROR_OBJECT (pool, "failed to create new MSDK memory");
261 return GST_FLOW_ERROR;
266 gst_msdk_buffer_pool_acquire_buffer (GstBufferPool * pool,
267 GstBuffer ** out_buffer_ptr, GstBufferPoolAcquireParams * params)
269 GstMsdkBufferPool *msdk_pool = GST_MSDK_BUFFER_POOL_CAST (pool);
270 GstMsdkBufferPoolPrivate *priv = msdk_pool->priv;
271 GstBuffer *buf = NULL;
273 mfxFrameSurface1 *surface;
277 GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (pool, &buf, params);
279 if (ret != GST_FLOW_OK || priv->memory_type == GST_MSDK_MEMORY_TYPE_SYSTEM) {
281 *out_buffer_ptr = buf;
285 surface = gst_msdk_get_surface_from_buffer (buf);
287 /* When using video memory, mfx surface is still locked even though
288 * it's finished by SyncOperation. There's no way to get notified when it gets unlocked.
289 * So we need to confirm if it's unlocked every time a gst buffer is acquired.
290 * If it's still locked, we can replace it with new unlocked/unused surface.
292 if (!surface || surface->Data.Locked > 0) {
293 if (!gst_msdk_video_memory_get_surface_available (gst_buffer_peek_memory
295 GST_WARNING_OBJECT (pool, "failed to get new surface available");
296 return GST_FLOW_ERROR;
300 /* When using dmabuf, we should confirm that the fd of memory and
301 * the fd of surface match, since there is no guarantee that fd matches
302 * between surface and memory.
304 if (priv->memory_type == GST_MSDK_MEMORY_TYPE_DMABUF) {
305 surface = gst_msdk_get_surface_from_buffer (buf);
306 gst_msdk_get_dmabuf_info_from_surface (surface, &fd, NULL);
308 if (gst_dmabuf_memory_get_fd (gst_buffer_peek_memory (buf, 0)) != fd) {
310 mem = gst_msdk_dmabuf_memory_new_with_surface (priv->allocator, surface);
311 gst_buffer_replace_memory (buf, 0, mem);
312 gst_buffer_unset_flags (buf, GST_BUFFER_FLAG_TAG_MEMORY);
317 *out_buffer_ptr = buf;
322 gst_msdk_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buf)
324 mfxFrameSurface1 *surface;
325 GstMsdkBufferPool *msdk_pool = GST_MSDK_BUFFER_POOL_CAST (pool);
326 GstMsdkBufferPoolPrivate *priv = msdk_pool->priv;
328 if (priv->memory_type == GST_MSDK_MEMORY_TYPE_SYSTEM)
331 surface = gst_msdk_get_surface_from_buffer (buf);
335 gst_msdk_video_memory_release_surface (gst_buffer_peek_memory (buf, 0));
338 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (pool, buf);
342 gst_msdk_buffer_pool_finalize (GObject * object)
344 GstMsdkBufferPool *pool = GST_MSDK_BUFFER_POOL_CAST (object);
345 GstMsdkBufferPoolPrivate *priv = pool->priv;
348 gst_object_unref (priv->allocator);
350 G_OBJECT_CLASS (parent_class)->finalize (object);
354 gst_msdk_buffer_pool_init (GstMsdkBufferPool * pool)
356 pool->priv = gst_msdk_buffer_pool_get_instance_private (pool);
360 gst_msdk_buffer_pool_class_init (GstMsdkBufferPoolClass * klass)
362 GObjectClass *object_class = G_OBJECT_CLASS (klass);
363 GstBufferPoolClass *pool_class = GST_BUFFER_POOL_CLASS (klass);
365 object_class->finalize = gst_msdk_buffer_pool_finalize;
367 pool_class->get_options = gst_msdk_buffer_pool_get_options;
368 pool_class->set_config = gst_msdk_buffer_pool_set_config;
369 pool_class->alloc_buffer = gst_msdk_buffer_pool_alloc_buffer;
370 pool_class->acquire_buffer = gst_msdk_buffer_pool_acquire_buffer;
371 pool_class->release_buffer = gst_msdk_buffer_pool_release_buffer;
375 gst_msdk_buffer_pool_new (GstMsdkContext * context,
376 mfxFrameAllocResponse * alloc_resp)
378 GstMsdkBufferPool *pool = g_object_new (GST_TYPE_MSDK_BUFFER_POOL, NULL);
380 /* Doesn't need to count reference of the context */
381 pool->priv->context = context;
382 pool->priv->alloc_response = alloc_resp;
384 return GST_BUFFER_POOL_CAST (pool);