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 };
58
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,
63 "MSDK Buffer Pool"));
64
65 static const gchar **
gst_msdk_buffer_pool_get_options(GstBufferPool * pool)66 gst_msdk_buffer_pool_get_options (GstBufferPool * pool)
67 {
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,
72 NULL
73 };
74
75 return options;
76 }
77
78 static inline GstMsdkMemoryType
_msdk_get_memory_type(GstStructure * config)79 _msdk_get_memory_type (GstStructure * config)
80 {
81 gboolean video, dmabuf;
82
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);
87
88 if (video && !dmabuf)
89 return GST_MSDK_MEMORY_TYPE_VIDEO;
90 if (video && dmabuf)
91 return GST_MSDK_MEMORY_TYPE_DMABUF;
92 if (!video && dmabuf)
93 GST_WARNING ("Can't use DMAbuf since it's system msdk bufferpool");
94 return GST_MSDK_MEMORY_TYPE_SYSTEM;
95 }
96
97 static gboolean
gst_msdk_buffer_pool_set_config(GstBufferPool * pool,GstStructure * config)98 gst_msdk_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
99 {
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;
106
107 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
108 &max_buffers))
109 goto error_invalid_config;
110
111 if (!caps)
112 goto error_no_caps;
113
114 if (!gst_video_info_from_caps (&video_info, caps))
115 goto error_invalid_caps;
116
117 if (!gst_buffer_pool_config_get_allocator (config, &allocator, NULL))
118 goto error_invalid_allocator;
119
120 if (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);
128 allocator = NULL;
129 }
130
131 priv->add_videometa = gst_buffer_pool_config_has_option (config,
132 GST_BUFFER_POOL_OPTION_VIDEO_META);
133
134 if (priv->add_videometa && gst_buffer_pool_config_has_option (config,
135 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT)) {
136 GstVideoAlignment alignment;
137
138 gst_msdk_set_video_alignment (&video_info, &alignment);
139 gst_video_info_align (&video_info, &alignment);
140 gst_buffer_pool_config_set_video_alignment (config, &alignment);
141 }
142
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;
150 }
151
152 /* create a new allocator if needed */
153 if (!allocator) {
154 GstAllocationParams params = { 0, 31, 0, 0, };
155
156 if (priv->memory_type == GST_MSDK_MEMORY_TYPE_DMABUF)
157 allocator =
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)
161 allocator =
162 gst_msdk_video_allocator_new (priv->context, &video_info,
163 priv->alloc_response);
164 else
165 allocator = gst_msdk_system_allocator_new (&video_info);
166
167 if (!allocator)
168 goto error_no_allocator;
169
170 GST_INFO_OBJECT (pool, "created new allocator %" GST_PTR_FORMAT, allocator);
171
172 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
173 gst_object_unref (allocator);
174 }
175
176 if (priv->allocator)
177 gst_object_unref (priv->allocator);
178 priv->allocator = gst_object_ref (allocator);
179
180 return GST_BUFFER_POOL_CLASS
181 (gst_msdk_buffer_pool_parent_class)->set_config (pool, config);
182
183 error_invalid_config:
184 {
185 GST_ERROR_OBJECT (pool, "invalid config");
186 return FALSE;
187 }
188 error_no_caps:
189 {
190 GST_ERROR_OBJECT (pool, "no caps in config");
191 return FALSE;
192 }
193 error_invalid_caps:
194 {
195 GST_ERROR_OBJECT (pool, "invalid caps %" GST_PTR_FORMAT, caps);
196 return FALSE;
197 }
198 error_invalid_allocator:
199 {
200 GST_ERROR_OBJECT (pool, "no allocator in config");
201 return FALSE;
202 }
203 error_no_allocator:
204 {
205 GST_ERROR_OBJECT (pool, "no allocator defined");
206 return FALSE;
207 }
208 }
209
210 static GstFlowReturn
gst_msdk_buffer_pool_alloc_buffer(GstBufferPool * pool,GstBuffer ** out_buffer_ptr,GstBufferPoolAcquireParams * params)211 gst_msdk_buffer_pool_alloc_buffer (GstBufferPool * pool,
212 GstBuffer ** out_buffer_ptr, GstBufferPoolAcquireParams * params)
213 {
214 GstMsdkBufferPool *msdk_pool = GST_MSDK_BUFFER_POOL_CAST (pool);
215 GstMsdkBufferPoolPrivate *priv = msdk_pool->priv;
216 GstMemory *mem;
217 GstBuffer *buf;
218
219 buf = gst_buffer_new ();
220
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);
225 else
226 mem = gst_msdk_system_memory_new (priv->allocator);
227
228 if (!mem)
229 goto no_memory;
230
231 gst_buffer_append_memory (buf, mem);
232
233 if (priv->add_videometa) {
234 GstVideoMeta *vmeta;
235 GstVideoInfo *info;
236
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;
241 else
242 info = &GST_MSDK_SYSTEM_ALLOCATOR_CAST (priv->allocator)->image_info;
243
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);
248
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;
252 }
253 }
254
255 *out_buffer_ptr = buf;
256 return GST_FLOW_OK;
257
258 no_memory:
259 {
260 GST_ERROR_OBJECT (pool, "failed to create new MSDK memory");
261 return GST_FLOW_ERROR;
262 }
263 }
264
265 static GstFlowReturn
gst_msdk_buffer_pool_acquire_buffer(GstBufferPool * pool,GstBuffer ** out_buffer_ptr,GstBufferPoolAcquireParams * params)266 gst_msdk_buffer_pool_acquire_buffer (GstBufferPool * pool,
267 GstBuffer ** out_buffer_ptr, GstBufferPoolAcquireParams * params)
268 {
269 GstMsdkBufferPool *msdk_pool = GST_MSDK_BUFFER_POOL_CAST (pool);
270 GstMsdkBufferPoolPrivate *priv = msdk_pool->priv;
271 GstBuffer *buf = NULL;
272 GstFlowReturn ret;
273 mfxFrameSurface1 *surface;
274 gint fd;
275
276 ret =
277 GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (pool, &buf, params);
278
279 if (ret != GST_FLOW_OK || priv->memory_type == GST_MSDK_MEMORY_TYPE_SYSTEM) {
280 if (buf)
281 *out_buffer_ptr = buf;
282 return ret;
283 }
284
285 surface = gst_msdk_get_surface_from_buffer (buf);
286
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.
291 */
292 if (!surface || surface->Data.Locked > 0) {
293 if (!gst_msdk_video_memory_get_surface_available (gst_buffer_peek_memory
294 (buf, 0))) {
295 GST_WARNING_OBJECT (pool, "failed to get new surface available");
296 return GST_FLOW_ERROR;
297 }
298 }
299 #ifndef _WIN32
300 /* When using dmabuf, we should confirm that the fd of memeory and
301 * the fd of surface match, since there is no guarantee that fd matches
302 * between surface and memory.
303 */
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);
307
308 if (gst_dmabuf_memory_get_fd (gst_buffer_peek_memory (buf, 0)) != fd) {
309 GstMemory *mem;
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);
313 }
314 }
315 #endif
316
317 *out_buffer_ptr = buf;
318 return GST_FLOW_OK;
319 }
320
321 static void
gst_msdk_buffer_pool_release_buffer(GstBufferPool * pool,GstBuffer * buf)322 gst_msdk_buffer_pool_release_buffer (GstBufferPool * pool, GstBuffer * buf)
323 {
324 mfxFrameSurface1 *surface;
325 GstMsdkBufferPool *msdk_pool = GST_MSDK_BUFFER_POOL_CAST (pool);
326 GstMsdkBufferPoolPrivate *priv = msdk_pool->priv;
327
328 if (priv->memory_type == GST_MSDK_MEMORY_TYPE_SYSTEM)
329 goto done;
330
331 surface = gst_msdk_get_surface_from_buffer (buf);
332 if (!surface)
333 goto done;
334
335 gst_msdk_video_memory_release_surface (gst_buffer_peek_memory (buf, 0));
336
337 done:
338 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (pool, buf);
339 }
340
341 static void
gst_msdk_buffer_pool_finalize(GObject * object)342 gst_msdk_buffer_pool_finalize (GObject * object)
343 {
344 GstMsdkBufferPool *pool = GST_MSDK_BUFFER_POOL_CAST (object);
345 GstMsdkBufferPoolPrivate *priv = pool->priv;
346
347 if (priv->allocator)
348 gst_object_unref (priv->allocator);
349
350 G_OBJECT_CLASS (parent_class)->finalize (object);
351 }
352
353 static void
gst_msdk_buffer_pool_init(GstMsdkBufferPool * pool)354 gst_msdk_buffer_pool_init (GstMsdkBufferPool * pool)
355 {
356 pool->priv = gst_msdk_buffer_pool_get_instance_private (pool);
357 }
358
359 static void
gst_msdk_buffer_pool_class_init(GstMsdkBufferPoolClass * klass)360 gst_msdk_buffer_pool_class_init (GstMsdkBufferPoolClass * klass)
361 {
362 GObjectClass *object_class = G_OBJECT_CLASS (klass);
363 GstBufferPoolClass *pool_class = GST_BUFFER_POOL_CLASS (klass);
364
365 object_class->finalize = gst_msdk_buffer_pool_finalize;
366
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;
372 }
373
374 GstBufferPool *
gst_msdk_buffer_pool_new(GstMsdkContext * context,mfxFrameAllocResponse * alloc_resp)375 gst_msdk_buffer_pool_new (GstMsdkContext * context,
376 mfxFrameAllocResponse * alloc_resp)
377 {
378 GstMsdkBufferPool *pool = g_object_new (GST_TYPE_MSDK_BUFFER_POOL, NULL);
379
380 /* Doesn't need to count reference of the context */
381 pool->priv->context = context;
382 pool->priv->alloc_response = alloc_resp;
383
384 return GST_BUFFER_POOL_CAST (pool);
385 }
386