1 /* GStreamer Intel MSDK plugin
2 * Copyright (c) 2018, Intel Corporation
3 * Copyright (c) 2018, Igalia S.L.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
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.
15 *
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.
19 *
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.
31 */
32
33 #include "gstmsdkbufferpool.h"
34 #include "gstmsdksystemmemory.h"
35 #include "gstmsdkvideomemory.h"
36 #ifndef _WIN32
37 #include "gstmsdkallocator_libva.h"
38 #endif
39
40 GST_DEBUG_CATEGORY_STATIC (gst_debug_msdkbufferpool);
41 #define GST_CAT_DEFAULT gst_debug_msdkbufferpool
42
43 typedef enum _GstMsdkMemoryType
44 {
45 GST_MSDK_MEMORY_TYPE_SYSTEM,
46 GST_MSDK_MEMORY_TYPE_VIDEO,
47 GST_MSDK_MEMORY_TYPE_DMABUF,
48 } GstMsdkMemoryType;
49
50 struct _GstMsdkBufferPoolPrivate
51 {
52 GstMsdkContext *context;
53 GstAllocator *allocator;
54 mfxFrameAllocResponse *alloc_response;
55 GstMsdkMemoryType memory_type;
56 gboolean add_videometa;
57 gboolean need_alignment;
58 GstVideoAlignment alignment;
59
60 };
61
62 #define gst_msdk_buffer_pool_parent_class parent_class
63 G_DEFINE_TYPE_WITH_CODE (GstMsdkBufferPool, gst_msdk_buffer_pool,
64 GST_TYPE_VIDEO_BUFFER_POOL, G_ADD_PRIVATE (GstMsdkBufferPool)
65 GST_DEBUG_CATEGORY_INIT (gst_debug_msdkbufferpool, "msdkbufferpool", 0,
66 "MSDK Buffer Pool"));
67
68 static const gchar **
gst_msdk_buffer_pool_get_options(GstBufferPool * pool)69 gst_msdk_buffer_pool_get_options (GstBufferPool * pool)
70 {
71 static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
72 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT,
73 GST_BUFFER_POOL_OPTION_MSDK_USE_VIDEO_MEMORY,
74 GST_BUFFER_POOL_OPTION_MSDK_USE_DMABUF,
75 NULL
76 };
77
78 return options;
79 }
80
81 static inline GstMsdkMemoryType
_msdk_get_memory_type(GstStructure * config)82 _msdk_get_memory_type (GstStructure * config)
83 {
84 gboolean video, dmabuf;
85
86 video = gst_buffer_pool_config_has_option (config,
87 GST_BUFFER_POOL_OPTION_MSDK_USE_VIDEO_MEMORY);
88 dmabuf = gst_buffer_pool_config_has_option (config,
89 GST_BUFFER_POOL_OPTION_MSDK_USE_DMABUF);
90
91 if (video && !dmabuf)
92 return GST_MSDK_MEMORY_TYPE_VIDEO;
93 if (video && dmabuf)
94 return GST_MSDK_MEMORY_TYPE_DMABUF;
95 if (!video && dmabuf)
96 GST_WARNING ("Can't use DMAbuf since it's system msdk bufferpool");
97 return GST_MSDK_MEMORY_TYPE_SYSTEM;
98 }
99
100 static gboolean
gst_msdk_buffer_pool_set_config(GstBufferPool * pool,GstStructure * config)101 gst_msdk_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
102 {
103 GstMsdkBufferPool *msdk_pool = GST_MSDK_BUFFER_POOL_CAST (pool);
104 GstMsdkBufferPoolPrivate *priv = msdk_pool->priv;
105 GstCaps *caps = NULL;
106 GstAllocator *allocator = NULL;
107 GstVideoInfo video_info;
108 guint size, min_buffers, max_buffers;
109
110 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
111 &max_buffers))
112 goto error_invalid_config;
113
114 if (!caps)
115 goto error_no_caps;
116
117 if (!gst_video_info_from_caps (&video_info, caps))
118 goto error_invalid_caps;
119
120 if (!gst_buffer_pool_config_get_allocator (config, &allocator, NULL))
121 goto error_invalid_allocator;
122
123 if (allocator
124 && (g_strcmp0 (allocator->mem_type, GST_MSDK_SYSTEM_MEMORY_NAME) != 0
125 && g_strcmp0 (allocator->mem_type, GST_MSDK_VIDEO_MEMORY_NAME) != 0
126 && g_strcmp0 (allocator->mem_type,
127 GST_MSDK_DMABUF_MEMORY_NAME) != 0)) {
128 GST_INFO_OBJECT (pool,
129 "This is not MSDK allocator. So this will be ignored");
130 gst_object_unref (allocator);
131 allocator = NULL;
132 }
133
134 priv->add_videometa = gst_buffer_pool_config_has_option (config,
135 GST_BUFFER_POOL_OPTION_VIDEO_META);
136
137 priv->need_alignment = gst_buffer_pool_config_has_option (config,
138 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
139
140 if (priv->add_videometa && priv->need_alignment) {
141 gst_msdk_set_video_alignment (&video_info, 0, 0, &priv->alignment);
142 gst_video_info_align (&video_info, &priv->alignment);
143 gst_buffer_pool_config_set_video_alignment (config, &priv->alignment);
144 }
145
146 priv->memory_type = _msdk_get_memory_type (config);
147 if (((priv->memory_type == GST_MSDK_MEMORY_TYPE_VIDEO)
148 || (priv->memory_type == GST_MSDK_MEMORY_TYPE_DMABUF))
149 && (!priv->context || !priv->alloc_response)) {
150 GST_ERROR_OBJECT (pool,
151 "No MSDK context or Allocation response for using video memory");
152 goto error_invalid_config;
153 }
154
155 /* create a new allocator if needed */
156 if (!allocator) {
157 GstAllocationParams params = { 0, 31, 0, 0, };
158
159 if (priv->memory_type == GST_MSDK_MEMORY_TYPE_DMABUF)
160 allocator =
161 gst_msdk_dmabuf_allocator_new (priv->context, &video_info,
162 priv->alloc_response);
163 else if (priv->memory_type == GST_MSDK_MEMORY_TYPE_VIDEO)
164 allocator =
165 gst_msdk_video_allocator_new (priv->context, &video_info,
166 priv->alloc_response);
167 else
168 allocator = gst_msdk_system_allocator_new (&video_info);
169
170 if (!allocator)
171 goto error_no_allocator;
172
173 GST_INFO_OBJECT (pool, "created new allocator %" GST_PTR_FORMAT, allocator);
174
175 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
176 gst_object_unref (allocator);
177 }
178
179 if (priv->allocator)
180 gst_object_unref (priv->allocator);
181 priv->allocator = gst_object_ref (allocator);
182
183 return GST_BUFFER_POOL_CLASS
184 (gst_msdk_buffer_pool_parent_class)->set_config (pool, config);
185
186 error_invalid_config:
187 {
188 GST_ERROR_OBJECT (pool, "invalid config");
189 return FALSE;
190 }
191 error_no_caps:
192 {
193 GST_ERROR_OBJECT (pool, "no caps in config");
194 return FALSE;
195 }
196 error_invalid_caps:
197 {
198 GST_ERROR_OBJECT (pool, "invalid caps %" GST_PTR_FORMAT, caps);
199 return FALSE;
200 }
201 error_invalid_allocator:
202 {
203 GST_ERROR_OBJECT (pool, "no allocator in config");
204 return FALSE;
205 }
206 error_no_allocator:
207 {
208 GST_ERROR_OBJECT (pool, "no allocator defined");
209 return FALSE;
210 }
211 }
212
213 static GstFlowReturn
gst_msdk_buffer_pool_alloc_buffer(GstBufferPool * pool,GstBuffer ** out_buffer_ptr,GstBufferPoolAcquireParams * params)214 gst_msdk_buffer_pool_alloc_buffer (GstBufferPool * pool,
215 GstBuffer ** out_buffer_ptr, GstBufferPoolAcquireParams * params)
216 {
217 GstMsdkBufferPool *msdk_pool = GST_MSDK_BUFFER_POOL_CAST (pool);
218 GstMsdkBufferPoolPrivate *priv = msdk_pool->priv;
219 GstMemory *mem;
220 GstBuffer *buf;
221
222 buf = gst_buffer_new ();
223
224 if (priv->memory_type == GST_MSDK_MEMORY_TYPE_DMABUF)
225 mem = gst_msdk_dmabuf_memory_new (priv->allocator);
226 else if (priv->memory_type == GST_MSDK_MEMORY_TYPE_VIDEO)
227 mem = gst_msdk_video_memory_new (priv->allocator);
228 else
229 mem = gst_msdk_system_memory_new (priv->allocator);
230
231 if (!mem)
232 goto no_memory;
233
234 gst_buffer_append_memory (buf, mem);
235
236 if (priv->add_videometa) {
237 GstVideoMeta *vmeta;
238 GstVideoInfo *info;
239
240 if (priv->memory_type == GST_MSDK_MEMORY_TYPE_DMABUF)
241 info = &GST_MSDK_DMABUF_ALLOCATOR_CAST (priv->allocator)->image_info;
242 else if (priv->memory_type == GST_MSDK_MEMORY_TYPE_VIDEO)
243 info = &GST_MSDK_VIDEO_ALLOCATOR_CAST (priv->allocator)->image_info;
244 else
245 info = &GST_MSDK_SYSTEM_ALLOCATOR_CAST (priv->allocator)->image_info;
246
247 vmeta = gst_buffer_add_video_meta_full (buf, GST_VIDEO_FRAME_FLAG_NONE,
248 GST_VIDEO_INFO_FORMAT (info),
249 GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info),
250 GST_VIDEO_INFO_N_PLANES (info), info->offset, info->stride);
251
252 if (priv->need_alignment) {
253 if (!gst_video_meta_set_alignment (vmeta, priv->alignment)) {
254 GST_ERROR_OBJECT (pool, "failed to set alignment");
255 return GST_FLOW_ERROR;
256 }
257 }
258
259 if (priv->memory_type == GST_MSDK_MEMORY_TYPE_VIDEO) {
260 vmeta->map = gst_video_meta_map_msdk_memory;
261 vmeta->unmap = gst_video_meta_unmap_msdk_memory;
262 }
263 }
264
265 *out_buffer_ptr = buf;
266 return GST_FLOW_OK;
267
268 no_memory:
269 {
270 GST_ERROR_OBJECT (pool, "failed to create new MSDK memory");
271 return GST_FLOW_ERROR;
272 }
273 }
274
275 static GstFlowReturn
gst_msdk_buffer_pool_acquire_buffer(GstBufferPool * pool,GstBuffer ** out_buffer_ptr,GstBufferPoolAcquireParams * params)276 gst_msdk_buffer_pool_acquire_buffer (GstBufferPool * pool,
277 GstBuffer ** out_buffer_ptr, GstBufferPoolAcquireParams * params)
278 {
279 GstMsdkBufferPool *msdk_pool = GST_MSDK_BUFFER_POOL_CAST (pool);
280 GstMsdkBufferPoolPrivate *priv = msdk_pool->priv;
281 GstBuffer *buf = NULL;
282 GstFlowReturn ret;
283 mfxFrameSurface1 *surface;
284
285 ret =
286 GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (pool, &buf, params);
287
288 if (ret != GST_FLOW_OK || priv->memory_type == GST_MSDK_MEMORY_TYPE_SYSTEM) {
289 if (buf)
290 *out_buffer_ptr = buf;
291 return ret;
292 }
293
294 surface = gst_msdk_get_surface_from_buffer (buf);
295
296 /* When using video memory, mfx surface is still locked even though
297 * it's finished by SyncOperation. There's no way to get notified when it gets unlocked.
298 * So we need to confirm if it's unlocked every time a gst buffer is acquired.
299 * If it's still locked, we can replace it with new unlocked/unused surface.
300 */
301 if (!surface || surface->Data.Locked > 0) {
302 if (!gst_msdk_video_memory_get_surface_available (gst_buffer_peek_memory
303 (buf, 0))) {
304 GST_WARNING_OBJECT (pool, "failed to get new surface available");
305 return GST_FLOW_ERROR;
306 }
307 }
308 #ifndef _WIN32
309 /* When using dmabuf, we should confirm that the fd of memory and
310 * the fd of surface match, since there is no guarantee that fd matches
311 * between surface and memory.
312 */
313 if (priv->memory_type == GST_MSDK_MEMORY_TYPE_DMABUF) {
314 gint fd;
315 surface = gst_msdk_get_surface_from_buffer (buf);
316 gst_msdk_get_dmabuf_info_from_surface (surface, &fd, NULL);
317
318 if (gst_dmabuf_memory_get_fd (gst_buffer_peek_memory (buf, 0)) != fd) {
319 GstMemory *mem;
320 mem = gst_msdk_dmabuf_memory_new_with_surface (priv->allocator, surface);
321 gst_buffer_replace_memory (buf, 0, mem);
322 gst_buffer_unset_flags (buf, GST_BUFFER_FLAG_TAG_MEMORY);
323 }
324 }
325 #endif
326
327 *out_buffer_ptr = buf;
328 return GST_FLOW_OK;
329 }
330
331 static void
gst_msdk_buffer_pool_release_buffer(GstBufferPool * pool,GstBuffer * buf)332 gst_msdk_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buf)
333 {
334 mfxFrameSurface1 *surface;
335 GstMsdkBufferPool *msdk_pool = GST_MSDK_BUFFER_POOL_CAST (pool);
336 GstMsdkBufferPoolPrivate *priv = msdk_pool->priv;
337
338 if (priv->memory_type == GST_MSDK_MEMORY_TYPE_SYSTEM)
339 goto done;
340
341 surface = gst_msdk_get_surface_from_buffer (buf);
342 if (!surface)
343 goto done;
344
345 gst_msdk_video_memory_release_surface (gst_buffer_peek_memory (buf, 0));
346
347 done:
348 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (pool, buf);
349 }
350
351 static void
gst_msdk_buffer_pool_finalize(GObject * object)352 gst_msdk_buffer_pool_finalize (GObject * object)
353 {
354 GstMsdkBufferPool *pool = GST_MSDK_BUFFER_POOL_CAST (object);
355 GstMsdkBufferPoolPrivate *priv = pool->priv;
356
357 if (priv->allocator)
358 gst_object_unref (priv->allocator);
359
360 G_OBJECT_CLASS (parent_class)->finalize (object);
361 }
362
363 static void
gst_msdk_buffer_pool_init(GstMsdkBufferPool * pool)364 gst_msdk_buffer_pool_init (GstMsdkBufferPool * pool)
365 {
366 pool->priv = gst_msdk_buffer_pool_get_instance_private (pool);
367 }
368
369 static void
gst_msdk_buffer_pool_class_init(GstMsdkBufferPoolClass * klass)370 gst_msdk_buffer_pool_class_init (GstMsdkBufferPoolClass * klass)
371 {
372 GObjectClass *object_class = G_OBJECT_CLASS (klass);
373 GstBufferPoolClass *pool_class = GST_BUFFER_POOL_CLASS (klass);
374
375 object_class->finalize = gst_msdk_buffer_pool_finalize;
376
377 pool_class->get_options = gst_msdk_buffer_pool_get_options;
378 pool_class->set_config = gst_msdk_buffer_pool_set_config;
379 pool_class->alloc_buffer = gst_msdk_buffer_pool_alloc_buffer;
380 pool_class->acquire_buffer = gst_msdk_buffer_pool_acquire_buffer;
381 pool_class->release_buffer = gst_msdk_buffer_pool_release_buffer;
382 }
383
384 GstBufferPool *
gst_msdk_buffer_pool_new(GstMsdkContext * context,mfxFrameAllocResponse * alloc_resp)385 gst_msdk_buffer_pool_new (GstMsdkContext * context,
386 mfxFrameAllocResponse * alloc_resp)
387 {
388 GstMsdkBufferPool *pool = g_object_new (GST_TYPE_MSDK_BUFFER_POOL, NULL);
389
390 /* Doesn't need to count reference of the context */
391 pool->priv->context = context;
392 pool->priv->alloc_response = alloc_resp;
393
394 return GST_BUFFER_POOL_CAST (pool);
395 }
396