Debian Codegraph @

gst-libs/gst/analytics/gsttensormeta.c

raw symbols SCIP
1 /*
2 * GStreamer gstreamer-tensormeta
3 * Copyright (C) 2023 Collabora Ltd
4 *
5 * gsttensormeta.c
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #include "gsttensormeta.h"
24
25 static gboolean
26 gst_tensor_meta_transform (GstBuffer * dest, GstMeta * meta,
27 GstBuffer * buffer, GQuark type, gpointer data)
28 {
29 GstTensorMeta *dmeta, *smeta;
30
31 smeta = (GstTensorMeta *) meta;
32
33 if (GST_META_TRANSFORM_IS_COPY (type)) {
34 smeta = (GstTensorMeta *) meta;
35 dmeta = gst_buffer_add_tensor_meta (dest);
36 if (!dmeta)
37 return FALSE;
38 GST_TRACE ("copy tensor metadata");
39 dmeta->num_tensors = smeta->num_tensors;
40 dmeta->tensors = g_new (GstTensor *, smeta->num_tensors);
41 for (int i = 0; i < smeta->num_tensors; i++) {
42 dmeta->tensors[i] = gst_tensor_copy (smeta->tensors[i]);
43 }
44 } else {
45 GST_WARNING ("gst_tensor_meta_transform: transform type %u not supported",
46 type);
47 return FALSE;
48 }
49 return TRUE;
50 }
51
52 static gboolean
53 gst_tensor_meta_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
54 {
55 GstTensorMeta *tmeta = (GstTensorMeta *) meta;
56
57 tmeta->num_tensors = 0;
58 tmeta->tensors = NULL;
59
60 return TRUE;
61 }
62
63 static void
64 gst_tensor_meta_free (GstMeta * meta, GstBuffer * buffer)
65 {
66 GstTensorMeta *tmeta = (GstTensorMeta *) meta;
67
68 for (int i = 0; i < tmeta->num_tensors; i++) {
69 gst_tensor_free (tmeta->tensors[i]);
70 }
71 g_free (tmeta->tensors);
72 }
73
74 /**
75 * gst_tensor_meta_api_get_type: (skip)
76 *
77 * Since: 1.26
78 */
79 GType
80 gst_tensor_meta_api_get_type (void)
81 {
82 static GType type = 0;
83 static const gchar *tags[] = { NULL };
84
85 if (g_once_init_enter (&type)) {
86 GType _type = gst_meta_api_type_register ("GstTensorMetaAPI", tags);
87 g_once_init_leave (&type, _type);
88 }
89 return type;
90 }
91
92
93 /**
94 * gst_tensor_meta_get_info: (skip)
95 *
96 * Since: 1.26
97 */
98 const GstMetaInfo *
99 gst_tensor_meta_get_info (void)
100 {
101 static const GstMetaInfo *tmeta_info = NULL;
102
103 if (g_once_init_enter (&tmeta_info)) {
104 const GstMetaInfo *meta =
105 gst_meta_register (gst_tensor_meta_api_get_type (),
106 "GstTensorMeta",
107 sizeof (GstTensorMeta),
108 gst_tensor_meta_init,
109 gst_tensor_meta_free,
110 gst_tensor_meta_transform);
111 g_once_init_leave (&tmeta_info, meta);
112 }
113 return tmeta_info;
114 }
115
116 /**
117 * gst_buffer_add_tensor_meta:
118 * @buffer: A writable #GstBuffer
119 *
120 * Adds a #GstTensorMeta to a buffer
121 *
122 * Returns: (transfer none): The new #GstTensorMeta
123 *
124 * Since: 1.26
125 */
126
127 GstTensorMeta *
128 gst_buffer_add_tensor_meta (GstBuffer * buffer)
129 {
130 return (GstTensorMeta *) gst_buffer_add_meta (buffer,
131 gst_tensor_meta_get_info (), NULL);
132 }
133
134 /**
135 * gst_buffer_get_tensor_meta:
136 * @buffer: A #GstBuffer
137 *
138 * Gets the #GstTensorMeta from a buffer
139 *
140 * Returns: (nullable)(transfer none): The #GstTensorMeta if there is wone
141 *
142 * Since: 1.26
143 */
144
145 GstTensorMeta *
146 gst_buffer_get_tensor_meta (GstBuffer * buffer)
147 {
148 return (GstTensorMeta *) gst_buffer_get_meta (buffer,
149 GST_TENSOR_META_API_TYPE);
150 }
151
152 /**
153 * gst_tensor_meta_set:
154 * @tmeta: a #GstTensorMeta
155 * @num_tensors: The number of tensors in the @tensors array
156 * @tensors: (in) (array length=num_tensors) (transfer full): An array of poiners to #GstTensor
157 *
158 * Sets tensors into the #GstTensorMeta
159 *
160 * Since: 1.26
161 */
162 void
163 gst_tensor_meta_set (GstTensorMeta * tmeta, guint num_tensors,
164 GstTensor ** tensors)
165 {
166 guint i;
167
168 for (i = 0; i < tmeta->num_tensors; i++) {
169 gst_tensor_free (tmeta->tensors[i]);
170 }
171 g_free (tmeta->tensors);
172
173 tmeta->num_tensors = num_tensors;
174 tmeta->tensors = tensors;
175 }
176
177 /**
178 * gst_tensor_meta_get_by_id:
179 * @tmeta: A #GstTensorMeta
180 * @id: A #GQuark identifying tensor-encoding
181 *
182 * Get the first tensor from the #GstTensorMeta identified by @id.
183 *
184 * Return: (nullable)(transfer none): a GstTensor with id matching @id.
185 * Otherwise NULL will be returned.
186 *
187 * Since: 1.28
188 */
189 const GstTensor *
190 gst_tensor_meta_get_by_id (GstTensorMeta * tmeta, GQuark id)
191 {
192 g_return_val_if_fail (tmeta != NULL, NULL);
193 g_return_val_if_fail (tmeta->tensors, NULL);
194
195 for (int i = 0; i < tmeta->num_tensors; ++i) {
196 if (tmeta->tensors[i]->id == id)
197 return tmeta->tensors[i];
198 }
199
200 return NULL;
201 }
202
203 /**
204 * gst_tensor_meta_get_typed_tensor:
205 * @tmeta: A #GstTensorMeta
206 * @tensor_id: A #GQuark identifying the tensor-encoding
207 * @data_type: The data type of the tensor
208 * @order: The order of the tensor to read from the memory
209 * @num_dims: The number of dimensions that the tensor can have
210 * @dims: (array length=num_dims)(nullable): An optional array of dimensions, where G_MAXSIZE means ANY.
211 *
212 * Get the first tensor from the #GstTensorMeta identified by
213 * @tensor_id, matching the reading order, dimensions and the data
214 * type and optionally the dimensions. Validate whether the
215 * #GstBuffer has enough size to hold the tensor data.
216 *
217 * Return: (nullable) (transfer none): a matching #GstTensor,
218 * otherwise NULL
219 *
220 * Since: 1.28
221 */
222 const GstTensor *
223 gst_tensor_meta_get_typed_tensor (GstTensorMeta * tmeta,
224 GQuark tensor_id, GstTensorDataType data_type, GstTensorDimOrder order,
225 gsize num_dims, const gsize * dims)
226 {
227 const GstTensor *tensor;
228
229 tensor = gst_tensor_meta_get_by_id (tmeta, tensor_id);
230
231 if (tensor == NULL)
232 return NULL;
233
234 if (!gst_tensor_check_type (tensor, data_type, order, num_dims, dims))
235 return NULL;
236
237 return tensor;
238 }
239
240 /**
241 * gst_tensor_meta_get:
242 * @tmeta: A #GstTensorMeta
243 * @index: The number of the tensor to get
244 *
245 * Retrieves a tensor from the #GstTensorMeta, the index must be
246 * smaller than #GstTensorMeta.num_tensors
247 *
248 * Return: (transfer none): a GstTensor
249 *
250 * Since: 1.26
251 */
252 const GstTensor *
253 gst_tensor_meta_get (GstTensorMeta * tmeta, gsize index)
254 {
255 g_return_val_if_fail (tmeta->tensors, NULL);
256 g_return_val_if_fail (index < tmeta->num_tensors, NULL);
257
258 return tmeta->tensors[index];
259 }
260
261 /**
262 * gst_tensor_meta_get_index_from_id:
263 * @meta: a #GstTensorMeta
264 * @id: The tensor id to look for
265 *
266 * Finds the first tensor with the requsted ID in the meta
267 *
268 * Return: The index of the tensor inthe meta, or -1 if
269 * its not found.
270 *
271 * Since: 1.26
272 */
273 gint
274 gst_tensor_meta_get_index_from_id (GstTensorMeta * meta, GQuark id)
275 {
276 for (int i = 0; i < meta->num_tensors; ++i) {
277 if (meta->tensors[i]->id == id)
278 return i;
279 }
280
281 return -1;
282 }