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