Merging gst-docs
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst-libs / gst / d3d11 / gstd3d11bufferpool.cpp
1 /* GStreamer
2  * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
3  * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
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 "gstd3d11bufferpool.h"
26 #include "gstd3d11memory.h"
27 #include "gstd3d11device.h"
28 #include "gstd3d11utils.h"
29
30 #include <string.h>
31
32 /**
33  * SECTION:gstd3d11bufferpool
34  * @title: GstD3D11BufferPool
35  * @short_description: buffer pool for #GstD3D11Memory objects
36  * @see_also: #GstBufferPool, #GstGLMemory
37  *
38  * a #GstD3D11BufferPool is an object that allocates buffers with #GstD3D11Memory
39  *
40  * A #GstGLBufferPool is created with gst_d3d11_buffer_pool_new()
41  */
42
43 GST_DEBUG_CATEGORY_STATIC (gst_d3d11_buffer_pool_debug);
44 #define GST_CAT_DEFAULT gst_d3d11_buffer_pool_debug
45
46 struct _GstD3D11BufferPoolPrivate
47 {
48   GstD3D11Allocator *alloc[GST_VIDEO_MAX_PLANES];
49
50   GstD3D11AllocationParams *d3d11_params;
51   gboolean texture_array_pool;
52
53   gint stride[GST_VIDEO_MAX_PLANES];
54   gsize offset[GST_VIDEO_MAX_PLANES];
55 };
56
57 #define gst_d3d11_buffer_pool_parent_class parent_class
58 G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11BufferPool,
59     gst_d3d11_buffer_pool, GST_TYPE_BUFFER_POOL);
60
61 static void gst_d3d11_buffer_pool_dispose (GObject * object);
62 static const gchar **gst_d3d11_buffer_pool_get_options (GstBufferPool * pool);
63 static gboolean gst_d3d11_buffer_pool_set_config (GstBufferPool * pool,
64     GstStructure * config);
65 static GstFlowReturn gst_d3d11_buffer_pool_alloc_buffer (GstBufferPool * pool,
66     GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
67 static GstFlowReturn gst_d3d11_buffer_pool_acquire_buffer (GstBufferPool * pool,
68     GstBuffer ** buffer, GstBufferPoolAcquireParams * params);
69 static void gst_d3d11_buffer_pool_reset_buffer (GstBufferPool * pool,
70     GstBuffer * buffer);
71 static gboolean gst_d3d11_buffer_pool_start (GstBufferPool * pool);
72 static gboolean gst_d3d11_buffer_pool_stop (GstBufferPool * pool);
73
74 static void
75 gst_d3d11_buffer_pool_class_init (GstD3D11BufferPoolClass * klass)
76 {
77   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
78   GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
79
80   gobject_class->dispose = gst_d3d11_buffer_pool_dispose;
81
82   bufferpool_class->get_options = gst_d3d11_buffer_pool_get_options;
83   bufferpool_class->set_config = gst_d3d11_buffer_pool_set_config;
84   bufferpool_class->alloc_buffer = gst_d3d11_buffer_pool_alloc_buffer;
85   bufferpool_class->acquire_buffer = gst_d3d11_buffer_pool_acquire_buffer;
86   bufferpool_class->reset_buffer = gst_d3d11_buffer_pool_reset_buffer;
87   bufferpool_class->start = gst_d3d11_buffer_pool_start;
88   bufferpool_class->stop = gst_d3d11_buffer_pool_stop;
89
90   GST_DEBUG_CATEGORY_INIT (gst_d3d11_buffer_pool_debug, "d3d11bufferpool", 0,
91       "d3d11bufferpool object");
92 }
93
94 static void
95 gst_d3d11_buffer_pool_init (GstD3D11BufferPool * self)
96 {
97   self->priv = (GstD3D11BufferPoolPrivate *)
98       gst_d3d11_buffer_pool_get_instance_private (self);
99 }
100
101 static void
102 gst_d3d11_buffer_pool_clear_allocator (GstD3D11BufferPool * self)
103 {
104   GstD3D11BufferPoolPrivate *priv = self->priv;
105   guint i;
106
107   for (i = 0; i < G_N_ELEMENTS (priv->alloc); i++) {
108     if (priv->alloc[i]) {
109       gst_d3d11_allocator_set_active (priv->alloc[i], FALSE);
110       gst_clear_object (&priv->alloc[i]);
111     }
112   }
113 }
114
115 static void
116 gst_d3d11_buffer_pool_dispose (GObject * object)
117 {
118   GstD3D11BufferPool *self = GST_D3D11_BUFFER_POOL (object);
119   GstD3D11BufferPoolPrivate *priv = self->priv;
120
121   g_clear_pointer (&priv->d3d11_params, gst_d3d11_allocation_params_free);
122   gst_clear_object (&self->device);
123   gst_d3d11_buffer_pool_clear_allocator (self);
124
125   G_OBJECT_CLASS (parent_class)->dispose (object);
126 }
127
128 static const gchar **
129 gst_d3d11_buffer_pool_get_options (GstBufferPool * pool)
130 {
131   /* NOTE: d3d11 memory does not support alignment */
132   static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META, NULL };
133
134   return options;
135 }
136
137 static gboolean
138 gst_d3d11_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
139 {
140   GstD3D11BufferPool *self = GST_D3D11_BUFFER_POOL (pool);
141   GstD3D11BufferPoolPrivate *priv = self->priv;
142   GstVideoInfo info;
143   GstCaps *caps = NULL;
144   guint min_buffers, max_buffers;
145   gboolean ret = TRUE;
146   D3D11_TEXTURE2D_DESC *desc;
147   const GstD3D11Format *format;
148   gsize offset = 0;
149   gint i;
150
151   if (!gst_buffer_pool_config_get_params (config, &caps, NULL, &min_buffers,
152           &max_buffers))
153     goto wrong_config;
154
155   if (caps == NULL)
156     goto no_caps;
157
158   /* now parse the caps from the config */
159   if (!gst_video_info_from_caps (&info, caps))
160     goto wrong_caps;
161
162   GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
163       caps);
164
165   gst_d3d11_buffer_pool_clear_allocator (self);
166
167   memset (priv->stride, 0, sizeof (priv->stride));
168   memset (priv->offset, 0, sizeof (priv->offset));
169
170   if (priv->d3d11_params)
171     gst_d3d11_allocation_params_free (priv->d3d11_params);
172   priv->d3d11_params =
173       gst_buffer_pool_config_get_d3d11_allocation_params (config);
174   if (!priv->d3d11_params) {
175     /* allocate memory with resource format by default */
176     priv->d3d11_params =
177         gst_d3d11_allocation_params_new (self->device,
178         &info, (GstD3D11AllocationFlags) 0, 0);
179   }
180
181   desc = priv->d3d11_params->desc;
182
183   /* resolution of semi-planar formats must be multiple of 2 */
184   if (desc[0].Format == DXGI_FORMAT_NV12 || desc[0].Format == DXGI_FORMAT_P010
185       || desc[0].Format == DXGI_FORMAT_P016) {
186     if (desc[0].Width % 2 || desc[0].Height % 2) {
187       gint width, height;
188       GstVideoAlignment align;
189
190       GST_WARNING_OBJECT (self, "Resolution %dx%d is not mutiple of 2, fixing",
191           desc[0].Width, desc[0].Height);
192
193       width = GST_ROUND_UP_2 (desc[0].Width);
194       height = GST_ROUND_UP_2 (desc[0].Height);
195
196       gst_video_alignment_reset (&align);
197       align.padding_right = width - desc[0].Width;
198       align.padding_bottom = height - desc[0].Height;
199
200       gst_d3d11_allocation_params_alignment (priv->d3d11_params, &align);
201     }
202   }
203 #ifndef GST_DISABLE_GST_DEBUG
204   {
205     GST_LOG_OBJECT (self, "Direct3D11 Allocation params");
206     GST_LOG_OBJECT (self, "\tD3D11AllocationFlags: 0x%x",
207         priv->d3d11_params->flags);
208     for (i = 0; GST_VIDEO_MAX_PLANES; i++) {
209       if (desc[i].Format == DXGI_FORMAT_UNKNOWN)
210         break;
211       GST_LOG_OBJECT (self, "\t[plane %d] %dx%d, DXGI format %d",
212           i, desc[i].Width, desc[i].Height, desc[i].Format);
213       GST_LOG_OBJECT (self, "\t[plane %d] MipLevel %d, ArraySize %d",
214           i, desc[i].MipLevels, desc[i].ArraySize);
215       GST_LOG_OBJECT (self,
216           "\t[plane %d] SampleDesc.Count %d, SampleDesc.Quality %d",
217           i, desc[i].SampleDesc.Count, desc[i].SampleDesc.Quality);
218       GST_LOG_OBJECT (self, "\t[plane %d] Usage %d", i, desc[i].Usage);
219       GST_LOG_OBJECT (self,
220           "\t[plane %d] BindFlags 0x%x", i, desc[i].BindFlags);
221       GST_LOG_OBJECT (self,
222           "\t[plane %d] CPUAccessFlags 0x%x", i, desc[i].CPUAccessFlags);
223       GST_LOG_OBJECT (self,
224           "\t[plane %d] MiscFlags 0x%x", i, desc[i].MiscFlags);
225     }
226   }
227 #endif
228
229   if ((priv->d3d11_params->flags & GST_D3D11_ALLOCATION_FLAG_TEXTURE_ARRAY)) {
230     guint max_array_size = 0;
231
232     for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
233       if (desc[i].Format == DXGI_FORMAT_UNKNOWN)
234         break;
235
236       if (desc[i].ArraySize > max_array_size)
237         max_array_size = desc[i].ArraySize;
238     }
239
240     if (max_buffers == 0 || max_buffers > max_array_size) {
241       GST_WARNING_OBJECT (pool,
242           "Array pool is requested but allowed pool size %d > ArraySize %d",
243           max_buffers, max_array_size);
244       max_buffers = max_array_size;
245     }
246
247     priv->texture_array_pool = TRUE;
248   } else {
249     priv->texture_array_pool = FALSE;
250   }
251
252   offset = 0;
253   for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
254     GstD3D11Allocator *alloc;
255     GstD3D11PoolAllocator *pool_alloc;
256     GstFlowReturn flow_ret;
257     GstMemory *mem = NULL;
258     guint stride = 0;
259
260     if (desc[i].Format == DXGI_FORMAT_UNKNOWN)
261       break;
262
263     alloc =
264         (GstD3D11Allocator *) gst_d3d11_pool_allocator_new (self->device,
265         &desc[i]);
266     if (!gst_d3d11_allocator_set_active (alloc, TRUE)) {
267       GST_ERROR_OBJECT (self, "Failed to activate allocator");
268       gst_object_unref (alloc);
269       return FALSE;
270     }
271
272     pool_alloc = GST_D3D11_POOL_ALLOCATOR (alloc);
273     flow_ret = gst_d3d11_pool_allocator_acquire_memory (pool_alloc, &mem);
274     if (flow_ret != GST_FLOW_OK) {
275       GST_ERROR_OBJECT (self, "Failed to allocate initial memory");
276       gst_d3d11_allocator_set_active (alloc, FALSE);
277       gst_object_unref (alloc);
278       return FALSE;
279     }
280
281     if (!gst_d3d11_memory_get_texture_stride (GST_D3D11_MEMORY_CAST (mem),
282             &stride) || stride < desc[i].Width) {
283       GST_ERROR_OBJECT (self, "Failed to calculate stride");
284
285       gst_d3d11_allocator_set_active (alloc, FALSE);
286       gst_object_unref (alloc);
287       gst_memory_unref (mem);
288
289       return FALSE;
290     }
291
292     priv->stride[i] = stride;
293     priv->offset[i] = offset;
294     offset += mem->size;
295
296     priv->alloc[i] = alloc;
297
298     gst_memory_unref (mem);
299   }
300
301   g_assert (priv->d3d11_params->d3d11_format != NULL);
302   format = priv->d3d11_params->d3d11_format;
303   /* single texture semi-planar formats */
304   if (format->dxgi_format != DXGI_FORMAT_UNKNOWN &&
305       GST_VIDEO_INFO_N_PLANES (&info) == 2) {
306     priv->stride[1] = priv->stride[0];
307     priv->offset[1] = priv->stride[0] * desc[0].Height;
308   }
309
310   gst_buffer_pool_config_set_params (config,
311       caps, offset, min_buffers, max_buffers);
312
313   return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config) && ret;
314
315   /* ERRORS */
316 wrong_config:
317   {
318     GST_WARNING_OBJECT (pool, "invalid config");
319     return FALSE;
320   }
321 no_caps:
322   {
323     GST_WARNING_OBJECT (pool, "no caps in config");
324     return FALSE;
325   }
326 wrong_caps:
327   {
328     GST_WARNING_OBJECT (pool,
329         "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
330     return FALSE;
331   }
332 }
333
334 static GstFlowReturn
335 gst_d3d11_buffer_pool_fill_buffer (GstD3D11BufferPool * self, GstBuffer * buf)
336 {
337   GstD3D11BufferPoolPrivate *priv = self->priv;
338   GstFlowReturn ret = GST_FLOW_OK;
339   guint i;
340
341   for (i = 0; i < G_N_ELEMENTS (priv->alloc); i++) {
342     GstMemory *mem = NULL;
343     GstD3D11PoolAllocator *alloc = GST_D3D11_POOL_ALLOCATOR (priv->alloc[i]);
344
345     if (!alloc)
346       break;
347
348     ret = gst_d3d11_pool_allocator_acquire_memory (alloc, &mem);
349     if (ret != GST_FLOW_OK) {
350       GST_WARNING_OBJECT (self, "Failed to acquire memory, ret %s",
351           gst_flow_get_name (ret));
352       return ret;
353     }
354
355     gst_buffer_append_memory (buf, mem);
356   }
357
358   return GST_FLOW_OK;
359 }
360
361 static GstFlowReturn
362 gst_d3d11_buffer_pool_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
363     GstBufferPoolAcquireParams * params)
364 {
365   GstD3D11BufferPool *self = GST_D3D11_BUFFER_POOL (pool);
366   GstD3D11BufferPoolPrivate *priv = self->priv;
367   GstD3D11AllocationParams *d3d11_params = priv->d3d11_params;
368   GstVideoInfo *info = &d3d11_params->info;
369   GstBuffer *buf;
370   GstFlowReturn ret = GST_FLOW_OK;
371
372   buf = gst_buffer_new ();
373   /* For texture-array case, we release memory in reset_buffer() so that it can
374    * be returned to allocator. So our acquire_buffer() method is expecting
375    * empty buffer in that case. Don't fill memory here for non-texture-array */
376   if (!priv->texture_array_pool) {
377     ret = gst_d3d11_buffer_pool_fill_buffer (self, buf);
378     if (ret != GST_FLOW_OK) {
379       gst_buffer_unref (buf);
380       return ret;
381     }
382   }
383
384   gst_buffer_add_video_meta_full (buf, GST_VIDEO_FRAME_FLAG_NONE,
385       GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
386       GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
387       priv->offset, priv->stride);
388
389   *buffer = buf;
390
391   return GST_FLOW_OK;
392 }
393
394 static GstFlowReturn
395 gst_d3d11_buffer_pool_acquire_buffer (GstBufferPool * pool,
396     GstBuffer ** buffer, GstBufferPoolAcquireParams * params)
397 {
398   GstD3D11BufferPool *self = GST_D3D11_BUFFER_POOL (pool);
399   GstD3D11BufferPoolPrivate *priv = self->priv;
400   GstFlowReturn ret;
401
402   ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (pool,
403       buffer, params);
404
405   if (ret != GST_FLOW_OK)
406     return ret;
407
408   /* Don't need special handling for non-texture-array case */
409   if (!priv->texture_array_pool)
410     return ret;
411
412   /* Baseclass will hold empty buffer in this case, fill GstMemory */
413   g_assert (gst_buffer_n_memory (*buffer) == 0);
414
415   return gst_d3d11_buffer_pool_fill_buffer (self, *buffer);
416 }
417
418 static void
419 gst_d3d11_buffer_pool_reset_buffer (GstBufferPool * pool, GstBuffer * buffer)
420 {
421   GstD3D11BufferPool *self = GST_D3D11_BUFFER_POOL (pool);
422   GstD3D11BufferPoolPrivate *priv = self->priv;
423
424   /* if we are using texture array, return memory to allocator, so that
425    * memory pool allocator can wake up if it's waiting for available memory */
426   if (priv->texture_array_pool) {
427     GST_LOG_OBJECT (self, "Returning memory to allocator");
428     gst_buffer_remove_all_memory (buffer);
429   }
430
431   GST_BUFFER_POOL_CLASS (parent_class)->reset_buffer (pool, buffer);
432   GST_BUFFER_FLAGS (buffer) = 0;
433 }
434
435 static gboolean
436 gst_d3d11_buffer_pool_start (GstBufferPool * pool)
437 {
438   GstD3D11BufferPool *self = GST_D3D11_BUFFER_POOL (pool);
439   GstD3D11BufferPoolPrivate *priv = self->priv;
440   guint i;
441   gboolean ret;
442
443   GST_DEBUG_OBJECT (self, "Start");
444
445   for (i = 0; i < G_N_ELEMENTS (priv->alloc); i++) {
446     GstD3D11Allocator *alloc = priv->alloc[i];
447
448     if (!alloc)
449       break;
450
451     if (!gst_d3d11_allocator_set_active (alloc, TRUE)) {
452       GST_ERROR_OBJECT (self, "Failed to activate allocator");
453       return FALSE;
454     }
455   }
456
457   ret = GST_BUFFER_POOL_CLASS (parent_class)->start (pool);
458   if (!ret) {
459     GST_ERROR_OBJECT (self, "Failed to start");
460
461     for (i = 0; i < G_N_ELEMENTS (priv->alloc); i++) {
462       GstD3D11Allocator *alloc = priv->alloc[i];
463
464       if (!alloc)
465         break;
466
467       gst_d3d11_allocator_set_active (alloc, FALSE);
468     }
469
470     return FALSE;
471   }
472
473   return TRUE;
474 }
475
476 static gboolean
477 gst_d3d11_buffer_pool_stop (GstBufferPool * pool)
478 {
479   GstD3D11BufferPool *self = GST_D3D11_BUFFER_POOL (pool);
480   GstD3D11BufferPoolPrivate *priv = self->priv;
481   guint i;
482
483   GST_DEBUG_OBJECT (self, "Stop");
484
485   for (i = 0; i < G_N_ELEMENTS (priv->alloc); i++) {
486     GstD3D11Allocator *alloc = priv->alloc[i];
487
488     if (!alloc)
489       break;
490
491     if (!gst_d3d11_allocator_set_active (alloc, FALSE)) {
492       GST_ERROR_OBJECT (self, "Failed to deactivate allocator");
493       return FALSE;
494     }
495   }
496
497   return GST_BUFFER_POOL_CLASS (parent_class)->stop (pool);
498 }
499
500 /**
501  * gst_d3d11_buffer_pool_new:
502  * @device: a #GstD3D11Device to use
503  *
504  * Returns: a #GstBufferPool that allocates buffers with #GstD3D11Memory
505  *
506  * Since: 1.20
507  */
508 GstBufferPool *
509 gst_d3d11_buffer_pool_new (GstD3D11Device * device)
510 {
511   GstD3D11BufferPool *pool;
512
513   g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
514
515   pool = (GstD3D11BufferPool *) g_object_new (GST_TYPE_D3D11_BUFFER_POOL, NULL);
516   gst_object_ref_sink (pool);
517
518   pool->device = (GstD3D11Device *) gst_object_ref (device);
519
520   return GST_BUFFER_POOL_CAST (pool);
521 }
522
523 /**
524  * gst_buffer_pool_config_get_d3d11_allocation_params:
525  * @config: a buffer pool config
526  *
527  * Returns: (transfer full) (nullable): the currently configured
528  * #GstD3D11AllocationParams on @config or %NULL if @config doesn't contain
529  * #GstD3D11AllocationParams
530  *
531  * Since: 1.20
532  */
533 GstD3D11AllocationParams *
534 gst_buffer_pool_config_get_d3d11_allocation_params (GstStructure * config)
535 {
536   GstD3D11AllocationParams *ret;
537
538   if (!gst_structure_get (config, "d3d11-allocation-params",
539           GST_TYPE_D3D11_ALLOCATION_PARAMS, &ret, NULL))
540     ret = NULL;
541
542   return ret;
543 }
544
545 /**
546  * gst_buffer_pool_config_set_d3d11_allocation_params:
547  * @config: a buffer pool config
548  * @params: (transfer none): a #GstD3D11AllocationParams
549  *
550  * Sets @params on @config
551  *
552  * Since: 1.20
553  */
554 void
555 gst_buffer_pool_config_set_d3d11_allocation_params (GstStructure * config,
556     GstD3D11AllocationParams * params)
557 {
558   g_return_if_fail (config != NULL);
559   g_return_if_fail (params != NULL);
560
561   gst_structure_set (config, "d3d11-allocation-params",
562       GST_TYPE_D3D11_ALLOCATION_PARAMS, params, NULL);
563 }