• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
3  * Copyright (C) 2016 Igalia
4  *
5  * Authors:
6  *  Víctor Manuel Jáquez Leal <vjaquez@igalia.com>
7  *  Javier Martin <javiermartin@by.com.es>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  *
24  */
25 
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 
30 #include <gst/video/gstvideometa.h>
31 
32 #include "gstkmsbufferpool.h"
33 #include "gstkmsallocator.h"
34 
35 GST_DEBUG_CATEGORY_STATIC (gst_kms_buffer_pool_debug);
36 #define GST_CAT_DEFAULT gst_kms_buffer_pool_debug
37 
38 struct _GstKMSBufferPoolPrivate
39 {
40   GstVideoInfo vinfo;
41   GstAllocator *allocator;
42   gboolean add_videometa;
43   gboolean has_prime_export;
44 };
45 
46 #define parent_class gst_kms_buffer_pool_parent_class
47 G_DEFINE_TYPE_WITH_CODE (GstKMSBufferPool, gst_kms_buffer_pool,
48     GST_TYPE_VIDEO_BUFFER_POOL, G_ADD_PRIVATE (GstKMSBufferPool);
49     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "kmsbufferpool", 0,
50         "KMS buffer pool"));
51 
52 static const gchar **
gst_kms_buffer_pool_get_options(GstBufferPool * pool)53 gst_kms_buffer_pool_get_options (GstBufferPool * pool)
54 {
55   static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
56     GST_BUFFER_POOL_OPTION_KMS_BUFFER,
57     GST_BUFFER_POOL_OPTION_KMS_PRIME_EXPORT,
58     NULL
59   };
60   return options;
61 }
62 
63 static gboolean
gst_kms_buffer_pool_set_config(GstBufferPool * pool,GstStructure * config)64 gst_kms_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
65 {
66   GstKMSBufferPool *vpool;
67   GstKMSBufferPoolPrivate *priv;
68   GstCaps *caps;
69   GstVideoInfo vinfo;
70   GstAllocator *allocator;
71   GstAllocationParams params;
72 
73   vpool = GST_KMS_BUFFER_POOL_CAST (pool);
74   priv = vpool->priv;
75 
76   if (!gst_buffer_pool_config_get_params (config, &caps, NULL, NULL, NULL))
77     goto wrong_config;
78 
79   if (!caps)
80     goto no_caps;
81 
82   /* now parse the caps from the config */
83   if (!gst_video_info_from_caps (&vinfo, caps))
84     goto wrong_caps;
85 
86   allocator = NULL;
87   gst_buffer_pool_config_get_allocator (config, &allocator, &params);
88 
89   /* not our allocator, not our buffers */
90   if (allocator && GST_IS_KMS_ALLOCATOR (allocator)) {
91     if (priv->allocator)
92       gst_object_unref (priv->allocator);
93     priv->allocator = gst_object_ref (allocator);
94   }
95   if (!priv->allocator)
96     goto no_allocator;
97 
98   priv->vinfo = vinfo;
99 
100   /* enable metadata based on config of the pool */
101   priv->add_videometa = gst_buffer_pool_config_has_option (config,
102       GST_BUFFER_POOL_OPTION_VIDEO_META);
103   priv->has_prime_export = gst_buffer_pool_config_has_option (config,
104       GST_BUFFER_POOL_OPTION_KMS_PRIME_EXPORT);
105 
106   return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
107 
108   /* ERRORS */
109 wrong_config:
110   {
111     GST_WARNING_OBJECT (pool, "invalid config");
112     return FALSE;
113   }
114 no_caps:
115   {
116     GST_WARNING_OBJECT (pool, "no caps in config");
117     return FALSE;
118   }
119 wrong_caps:
120   {
121     GST_WARNING_OBJECT (pool,
122         "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
123     return FALSE;
124   }
125 no_allocator:
126   {
127     GST_WARNING_OBJECT (pool, "no valid allocator in pool");
128     return FALSE;
129   }
130 }
131 
132 static GstFlowReturn
gst_kms_buffer_pool_alloc_buffer(GstBufferPool * pool,GstBuffer ** buffer,GstBufferPoolAcquireParams * params)133 gst_kms_buffer_pool_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
134     GstBufferPoolAcquireParams * params)
135 {
136   GstKMSBufferPool *vpool;
137   GstKMSBufferPoolPrivate *priv;
138   GstVideoInfo *info;
139   GstMemory *mem;
140 
141   vpool = GST_KMS_BUFFER_POOL_CAST (pool);
142   priv = vpool->priv;
143   info = &priv->vinfo;
144 
145   mem = gst_kms_allocator_bo_alloc (priv->allocator, info);
146   if (!mem)
147     goto no_memory;
148 
149   if (vpool->priv->has_prime_export) {
150     GstMemory *dmabufmem;
151 
152     dmabufmem = gst_kms_allocator_dmabuf_export (priv->allocator, mem);
153     if (dmabufmem)
154       mem = dmabufmem;
155     else
156       GST_WARNING_OBJECT (pool, "Failed to export DMABuf from Dumb buffer.");
157   }
158 
159   *buffer = gst_buffer_new ();
160   gst_buffer_append_memory (*buffer, mem);
161 
162   if (priv->add_videometa) {
163     GST_DEBUG_OBJECT (pool, "adding GstVideoMeta");
164 
165     gst_buffer_add_video_meta_full (*buffer, GST_VIDEO_FRAME_FLAG_NONE,
166         GST_VIDEO_INFO_FORMAT (info),
167         GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info),
168         GST_VIDEO_INFO_N_PLANES (info), info->offset, info->stride);
169   }
170 
171   return GST_FLOW_OK;
172 
173   /* ERROR */
174 no_memory:
175   {
176     GST_WARNING_OBJECT (pool, "can't create memory");
177     return GST_FLOW_ERROR;
178   }
179 }
180 
181 static void
gst_kms_buffer_pool_finalize(GObject * object)182 gst_kms_buffer_pool_finalize (GObject * object)
183 {
184   GstKMSBufferPool *pool;
185   GstKMSBufferPoolPrivate *priv;
186 
187   pool = GST_KMS_BUFFER_POOL (object);
188   priv = pool->priv;
189 
190   if (priv->allocator)
191     gst_object_unref (priv->allocator);
192 
193   G_OBJECT_CLASS (parent_class)->finalize (object);
194 }
195 
196 static void
gst_kms_buffer_pool_init(GstKMSBufferPool * pool)197 gst_kms_buffer_pool_init (GstKMSBufferPool * pool)
198 {
199   pool->priv = gst_kms_buffer_pool_get_instance_private (pool);
200 }
201 
202 static void
gst_kms_buffer_pool_class_init(GstKMSBufferPoolClass * klass)203 gst_kms_buffer_pool_class_init (GstKMSBufferPoolClass * klass)
204 {
205   GObjectClass *gobject_class;
206   GstBufferPoolClass *gstbufferpool_class;
207 
208   gobject_class = (GObjectClass *) klass;
209   gstbufferpool_class = (GstBufferPoolClass *) klass;
210 
211   gobject_class->finalize = gst_kms_buffer_pool_finalize;
212 
213   gstbufferpool_class->get_options = gst_kms_buffer_pool_get_options;
214   gstbufferpool_class->set_config = gst_kms_buffer_pool_set_config;
215   gstbufferpool_class->alloc_buffer = gst_kms_buffer_pool_alloc_buffer;
216 }
217 
218 GstBufferPool *
gst_kms_buffer_pool_new(void)219 gst_kms_buffer_pool_new (void)
220 {
221   GstBufferPool *pool;
222 
223   pool = g_object_new (GST_TYPE_KMS_BUFFER_POOL, NULL);
224   gst_object_ref_sink (pool);
225 
226   return pool;
227 }
228