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