• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2020 Igalia, S.L.
3  *     Author: Víctor Jáquez <vjaquez@igalia.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 "gstvapool.h"
26 
27 #include "gstvaallocator.h"
28 #include "gstvacaps.h"
29 
30 GST_DEBUG_CATEGORY_STATIC (gst_va_pool_debug);
31 #define GST_CAT_DEFAULT gst_va_pool_debug
32 
33 struct _GstVaPool
34 {
35   GstBufferPool parent;
36 
37   GstVideoInfo alloc_info;
38   GstVideoInfo caps_info;
39   GstAllocator *allocator;
40   gboolean force_videometa;
41   gboolean add_videometa;
42   gint crop_left;
43   gint crop_top;
44 
45   gboolean starting;
46 };
47 
48 #define gst_va_pool_parent_class parent_class
49 G_DEFINE_TYPE_WITH_CODE (GstVaPool, gst_va_pool, GST_TYPE_BUFFER_POOL,
50     GST_DEBUG_CATEGORY_INIT (gst_va_pool_debug, "vapool", 0, "VA Pool"));
51 
52 static const gchar **
gst_va_pool_get_options(GstBufferPool * pool)53 gst_va_pool_get_options (GstBufferPool * pool)
54 {
55   static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META, NULL };
56   return options;
57 }
58 
59 static inline gboolean
gst_buffer_pool_config_get_va_allocation_params(GstStructure * config,guint32 * usage_hint)60 gst_buffer_pool_config_get_va_allocation_params (GstStructure * config,
61     guint32 * usage_hint)
62 {
63   if (!gst_structure_get (config, "usage-hint", G_TYPE_UINT, usage_hint, NULL))
64     *usage_hint = VA_SURFACE_ATTRIB_USAGE_HINT_GENERIC;
65 
66   return TRUE;
67 }
68 
69 static inline gboolean
gst_buffer_pool_config_get_va_alignment(GstStructure * config,GstVideoAlignment * align)70 gst_buffer_pool_config_get_va_alignment (GstStructure * config,
71     GstVideoAlignment * align)
72 {
73   return gst_structure_get (config,
74       "va-padding-top", G_TYPE_UINT, &align->padding_top,
75       "va-padding-bottom", G_TYPE_UINT, &align->padding_bottom,
76       "va-padding-left", G_TYPE_UINT, &align->padding_left,
77       "va-padding-right", G_TYPE_UINT, &align->padding_right, NULL);
78 }
79 
80 static gboolean
gst_va_pool_set_config(GstBufferPool * pool,GstStructure * config)81 gst_va_pool_set_config (GstBufferPool * pool, GstStructure * config)
82 {
83   GstAllocator *allocator;
84   GstCaps *caps;
85   GstVaPool *vpool = GST_VA_POOL (pool);
86   GstVideoAlignment video_align = { 0, };
87   GstVideoInfo caps_info, alloc_info;
88   gint width, height;
89   guint i, min_buffers, max_buffers;
90   guint32 usage_hint;
91   gboolean has_alignment;
92 
93   if (!gst_buffer_pool_config_get_params (config, &caps, NULL, &min_buffers,
94           &max_buffers))
95     goto wrong_config;
96 
97   if (!caps)
98     goto no_caps;
99 
100   if (!gst_video_info_from_caps (&caps_info, caps))
101     goto wrong_caps;
102 
103   if (!gst_buffer_pool_config_get_allocator (config, &allocator, NULL))
104     goto wrong_config;
105 
106   if (!(allocator && (GST_IS_VA_DMABUF_ALLOCATOR (allocator)
107               || GST_IS_VA_ALLOCATOR (allocator))))
108     goto wrong_config;
109 
110   if (!gst_buffer_pool_config_get_va_allocation_params (config, &usage_hint))
111     goto wrong_config;
112 
113   width = GST_VIDEO_INFO_WIDTH (&caps_info);
114   height = GST_VIDEO_INFO_HEIGHT (&caps_info);
115 
116   GST_LOG_OBJECT (vpool, "%dx%d | %" GST_PTR_FORMAT, width, height, caps);
117 
118   /* enable metadata based on config of the pool */
119   vpool->add_videometa = gst_buffer_pool_config_has_option (config,
120       GST_BUFFER_POOL_OPTION_VIDEO_META);
121 
122   /* parse extra alignment info */
123   has_alignment = gst_buffer_pool_config_get_va_alignment (config,
124       &video_align);
125 
126   if (has_alignment) {
127     width += video_align.padding_left + video_align.padding_right;
128     height += video_align.padding_bottom + video_align.padding_top;
129 
130     if (video_align.padding_left > 0)
131       vpool->crop_left = video_align.padding_left;
132     if (video_align.padding_top > 0)
133       vpool->crop_top = video_align.padding_top;
134   }
135 
136   /* update allocation info with aligned size */
137   alloc_info = caps_info;
138   GST_VIDEO_INFO_WIDTH (&alloc_info) = width;
139   GST_VIDEO_INFO_HEIGHT (&alloc_info) = height;
140 
141   if (GST_IS_VA_DMABUF_ALLOCATOR (allocator)) {
142     if (!gst_va_dmabuf_allocator_set_format (allocator, &alloc_info,
143             usage_hint))
144       goto failed_allocator;
145   } else if (GST_IS_VA_ALLOCATOR (allocator)) {
146     if (!gst_va_allocator_set_format (allocator, &alloc_info, usage_hint))
147       goto failed_allocator;
148   }
149 
150   gst_object_replace ((GstObject **) & vpool->allocator,
151       GST_OBJECT (allocator));
152 
153   vpool->caps_info = caps_info;
154   vpool->alloc_info = alloc_info;
155 
156   for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&caps_info); i++) {
157     if (GST_VIDEO_INFO_PLANE_STRIDE (&caps_info, i) !=
158         GST_VIDEO_INFO_PLANE_STRIDE (&alloc_info, i) ||
159         GST_VIDEO_INFO_PLANE_OFFSET (&caps_info, i) !=
160         GST_VIDEO_INFO_PLANE_OFFSET (&alloc_info, i)) {
161       GST_INFO_OBJECT (vpool, "Video meta is required in buffer.");
162       vpool->force_videometa = TRUE;
163       break;
164     }
165   }
166 
167   gst_buffer_pool_config_set_params (config, caps,
168       GST_VIDEO_INFO_SIZE (&alloc_info), min_buffers, max_buffers);
169 
170   return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
171 
172   /* ERRORS */
173 wrong_config:
174   {
175     GST_WARNING_OBJECT (vpool, "invalid config");
176     return FALSE;
177   }
178 no_caps:
179   {
180     GST_WARNING_OBJECT (vpool, "no caps in config");
181     return FALSE;
182   }
183 wrong_caps:
184   {
185     GST_WARNING_OBJECT (vpool,
186         "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
187     return FALSE;
188   }
189 failed_allocator:
190   {
191     GST_WARNING_OBJECT (vpool, "Failed to set format to allocator");
192     return FALSE;
193   }
194 }
195 
196 static GstFlowReturn
gst_va_pool_alloc(GstBufferPool * pool,GstBuffer ** buffer,GstBufferPoolAcquireParams * params)197 gst_va_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
198     GstBufferPoolAcquireParams * params)
199 {
200   GstBuffer *buf;
201   GstVaPool *vpool = GST_VA_POOL (pool);
202 
203   buf = gst_buffer_new ();
204 
205   if (GST_IS_VA_DMABUF_ALLOCATOR (vpool->allocator)) {
206     if (vpool->starting) {
207       if (!gst_va_dmabuf_allocator_setup_buffer (vpool->allocator, buf))
208         goto no_memory;
209     } else if (!gst_va_dmabuf_allocator_prepare_buffer (vpool->allocator, buf)) {
210       if (!gst_va_dmabuf_allocator_setup_buffer (vpool->allocator, buf))
211         goto no_memory;
212     }
213   } else if (GST_IS_VA_ALLOCATOR (vpool->allocator)) {
214     if (vpool->starting) {
215       if (!gst_va_allocator_setup_buffer (vpool->allocator, buf))
216         goto no_memory;
217     } else if (!gst_va_allocator_prepare_buffer (vpool->allocator, buf)) {
218       if (!gst_va_allocator_setup_buffer (vpool->allocator, buf))
219         goto no_memory;
220     }
221   } else
222     goto no_memory;
223 
224   if (vpool->add_videometa) {
225     if (vpool->crop_left > 0 || vpool->crop_top > 0) {
226       GstVideoCropMeta *crop_meta;
227 
228       /* For video crop, its video meta's width and height should be
229          the full size of uncropped resolution. */
230       gst_buffer_add_video_meta_full (buf, GST_VIDEO_FRAME_FLAG_NONE,
231           GST_VIDEO_INFO_FORMAT (&vpool->alloc_info),
232           GST_VIDEO_INFO_WIDTH (&vpool->alloc_info),
233           GST_VIDEO_INFO_HEIGHT (&vpool->alloc_info),
234           GST_VIDEO_INFO_N_PLANES (&vpool->alloc_info),
235           vpool->alloc_info.offset, vpool->alloc_info.stride);
236 
237       crop_meta = gst_buffer_add_video_crop_meta (buf);
238       crop_meta->x = vpool->crop_left;
239       crop_meta->y = vpool->crop_top;
240       crop_meta->width = GST_VIDEO_INFO_WIDTH (&vpool->caps_info);
241       crop_meta->height = GST_VIDEO_INFO_HEIGHT (&vpool->caps_info);
242     } else {
243       /* GstVaAllocator may update offset/stride given the physical
244        * memory */
245       gst_buffer_add_video_meta_full (buf, GST_VIDEO_FRAME_FLAG_NONE,
246           GST_VIDEO_INFO_FORMAT (&vpool->caps_info),
247           GST_VIDEO_INFO_WIDTH (&vpool->caps_info),
248           GST_VIDEO_INFO_HEIGHT (&vpool->caps_info),
249           GST_VIDEO_INFO_N_PLANES (&vpool->caps_info),
250           vpool->alloc_info.offset, vpool->alloc_info.stride);
251     }
252   }
253 
254   *buffer = buf;
255 
256   return GST_FLOW_OK;
257 
258   /* ERROR */
259 no_memory:
260   {
261     gst_buffer_unref (buf);
262     GST_WARNING_OBJECT (vpool, "can't create memory");
263     return GST_FLOW_ERROR;
264   }
265 }
266 
267 static gboolean
gst_va_pool_start(GstBufferPool * pool)268 gst_va_pool_start (GstBufferPool * pool)
269 {
270   GstVaPool *vpool = GST_VA_POOL (pool);
271   gboolean ret;
272 
273   vpool->starting = TRUE;
274   ret = GST_BUFFER_POOL_CLASS (parent_class)->start (pool);
275   vpool->starting = FALSE;
276 
277   return ret;
278 }
279 
280 static gboolean
gst_va_pool_stop(GstBufferPool * pool)281 gst_va_pool_stop (GstBufferPool * pool)
282 {
283   GstVaPool *vpool = GST_VA_POOL (pool);
284   gboolean ret;
285 
286   ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (pool);
287 
288   if (GST_IS_VA_DMABUF_ALLOCATOR (vpool->allocator))
289     gst_va_dmabuf_allocator_flush (vpool->allocator);
290   else if (GST_IS_VA_ALLOCATOR (vpool->allocator))
291     gst_va_allocator_flush (vpool->allocator);
292 
293   return ret;
294 }
295 
296 static void
gst_va_pool_dispose(GObject * object)297 gst_va_pool_dispose (GObject * object)
298 {
299   GstVaPool *pool = GST_VA_POOL (object);
300 
301   GST_LOG_OBJECT (pool, "finalize video buffer pool %p", pool);
302 
303   gst_clear_object (&pool->allocator);
304 
305   G_OBJECT_CLASS (parent_class)->dispose (object);
306 }
307 
308 static void
gst_va_pool_class_init(GstVaPoolClass * klass)309 gst_va_pool_class_init (GstVaPoolClass * klass)
310 {
311   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
312   GstBufferPoolClass *gstbufferpool_class = GST_BUFFER_POOL_CLASS (klass);
313 
314   gobject_class->dispose = gst_va_pool_dispose;
315 
316   gstbufferpool_class->get_options = gst_va_pool_get_options;
317   gstbufferpool_class->set_config = gst_va_pool_set_config;
318   gstbufferpool_class->alloc_buffer = gst_va_pool_alloc;
319   gstbufferpool_class->start = gst_va_pool_start;
320   gstbufferpool_class->stop = gst_va_pool_stop;
321 }
322 
323 static void
gst_va_pool_init(GstVaPool * self)324 gst_va_pool_init (GstVaPool * self)
325 {
326 }
327 
328 GstBufferPool *
gst_va_pool_new(void)329 gst_va_pool_new (void)
330 {
331   GstVaPool *pool;
332 
333   pool = g_object_new (GST_TYPE_VA_POOL, NULL);
334   gst_object_ref_sink (pool);
335 
336   GST_LOG_OBJECT (pool, "new va video buffer pool %p", pool);
337 
338   return GST_BUFFER_POOL_CAST (pool);
339 }
340 
341 void
gst_buffer_pool_config_set_va_allocation_params(GstStructure * config,guint usage_hint)342 gst_buffer_pool_config_set_va_allocation_params (GstStructure * config,
343     guint usage_hint)
344 {
345   gst_structure_set (config, "usage-hint", G_TYPE_UINT, usage_hint, NULL);
346 }
347 
348 void
gst_buffer_pool_config_set_va_alignment(GstStructure * config,const GstVideoAlignment * align)349 gst_buffer_pool_config_set_va_alignment (GstStructure * config,
350     const GstVideoAlignment * align)
351 {
352   gst_structure_set (config,
353       "va-padding-top", G_TYPE_UINT, align->padding_top,
354       "va-padding-bottom", G_TYPE_UINT, align->padding_bottom,
355       "va-padding-left", G_TYPE_UINT, align->padding_left,
356       "va-padding-right", G_TYPE_UINT, align->padding_right, NULL);
357 }
358 
359 gboolean
gst_va_pool_requires_video_meta(GstBufferPool * pool)360 gst_va_pool_requires_video_meta (GstBufferPool * pool)
361 {
362   return GST_VA_POOL (pool)->force_videometa;
363 }
364 
365 GstBufferPool *
gst_va_pool_new_with_config(GstCaps * caps,guint size,guint min_buffers,guint max_buffers,guint usage_hint,GstAllocator * allocator,GstAllocationParams * alloc_params)366 gst_va_pool_new_with_config (GstCaps * caps, guint size, guint min_buffers,
367     guint max_buffers, guint usage_hint, GstAllocator * allocator,
368     GstAllocationParams * alloc_params)
369 {
370   GstBufferPool *pool;
371   GstStructure *config;
372 
373   pool = gst_va_pool_new ();
374 
375   config = gst_buffer_pool_get_config (pool);
376   gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
377       max_buffers);
378   gst_buffer_pool_config_set_va_allocation_params (config, usage_hint);
379   gst_buffer_pool_config_set_allocator (config, allocator, alloc_params);
380   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
381 
382   if (!gst_buffer_pool_set_config (pool, config))
383     gst_clear_object (&pool);
384 
385   return pool;
386 }
387