Debian Codegraph @

gst-libs/gst/analytics/gstanalyticstensormtd.c

raw symbols SCIP
1 /* GStreamer
2 * Copyright (C) 2024 Collabora Ltd
3 * @author: Daniel Morin <[email protected]>
4 *
5 * gstanalyticssegmentmeta.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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "gstanalyticstensormtd.h"
27 #include <gst/video/video.h>
28
29 /**
30 * SECTION: gstanalyticstensormtd
31 * @title: GstAnalyticsTensorMtd
32 * @short_description: Analytics metadata to store tensor inside a
33 * #GstAnalyticsRelationMeta
34 * @symbols:
35 * - GstAnalyticsTensorMtd
36 * @see_also: #GstAnalyticsMtd, #GstAnalyticsRelationMeta
37 *
38 * This type of metadata holds a tensor. It can be used to store tensor as
39 * analytics-meta for their ability to relate to each others. For example
40 * in a multi-model analytics pipeline, we sometime have one model input match
41 * the output of the other model. In this context it can be useful to keep the
42 * ancestry relation between first tensor, output of first inference, and the
43 * second tensor, output from second inference. Another use-case for
44 * #GstAnalyticsTensorMtd is to transport tensors from inference element to a
45 * post-processing element using a computing graph framework, like ONNX.
46 * Essentially #GstAnalyticsTensorMtd is a GstBuffer encapsulated by a
47 * analytics-meta with additional parameters describing the tensor.
48 *
49 * Since 1.28
50 */
51
52 static void gst_analytics_tensor_mtd_clear (GstBuffer * buffer,
53 GstAnalyticsMtd * mtd);
54
55 static gboolean
56 gst_analytics_tensor_mtd_transform (GstBuffer * transbuf,
57 GstAnalyticsMtd * transmtd, GstBuffer * buffer, GQuark type, gpointer data);
58
59 static const GstAnalyticsMtdImpl tensor_impl = {
60 "tensor",
61 gst_analytics_tensor_mtd_transform,
62 gst_analytics_tensor_mtd_clear
63 };
64
65
66 typedef GstTensor GstAnalyticsTensorMtdData;
67
68 /**
69 * gst_analytics_tensor_mtd_get_mtd_type:
70 *
71 * Get an id that represent tensor metadata type
72 *
73 * Returns: Opaque id of the #GstAnalyticsMtd type
74 *
75 * Since: 1.28
76 */
77 GstAnalyticsMtdType
78 gst_analytics_tensor_mtd_get_mtd_type (void)
79 {
80 return (GstAnalyticsMtdType) & tensor_impl;
81 }
82
83 /**
84 * gst_analytics_tensor_mtd_get_tensor:
85 * @instance: Instance of #GstAnalyticsTensorMtd
86 *
87 * Get tensor
88 *
89 * Returns: (transfer none): a #GstTensor
90 *
91 * Since: 1.28
92 */
93 GstTensor *
94 gst_analytics_tensor_mtd_get_tensor (const GstAnalyticsTensorMtd * instance)
95 {
96 GstAnalyticsTensorMtdData *mtddata;
97
98 g_return_val_if_fail (instance, NULL);
99
100 mtddata =
101 gst_analytics_relation_meta_get_mtd_data (instance->meta, instance->id);
102 g_return_val_if_fail (mtddata != NULL, NULL);
103
104 return mtddata;
105 }
106
107 /**
108 * gst_analytics_relation_meta_add_tensor_mtd:
109 * @instance: Instance
110 * @num_dims: The number of dimensions in the tensor
111 * @tensor_mtd: (out)(nullable): Handle update with newly added tensor mtd.
112 * Add tensor mtd to @instance.
113 *
114 * Add a new #GstAnalyticsTensorMtd holding a #GstTensor to @instance. The
115 * #GstTensor needs to be filled.
116 *
117 * Returns: Added successfully
118 *
119 * Since: 1.28
120 */
121 gboolean
122 gst_analytics_relation_meta_add_tensor_mtd (GstAnalyticsRelationMeta
123 * meta, gsize num_dims, GstAnalyticsTensorMtd * tensor_mtd)
124 {
125 GstTensor *tensor;
126
127 tensor = gst_analytics_relation_meta_add_mtd (meta, &tensor_impl,
128 sizeof (GstAnalyticsTensorMtdData) + (num_dims * sizeof (gsize)),
129 tensor_mtd);
130
131 if (tensor == NULL)
132 return FALSE;
133
134 memset (tensor, 0, sizeof (GstTensor));
135 tensor->num_dims = num_dims;
136
137 return TRUE;
138 }
139
140 /**
141 * gst_analytics_relation_meta_add_tensor_mtd_simple:
142 * @instance: Instance
143 * @id: semantically identify the contents of the tensor
144 * @data_type: #GstTensorDataType of tensor data
145 * @data: (transfer full): #GstBuffer holding tensor data
146 * @dims_order: Indicate tensor dimension indexing order
147 * @num_dims: number of tensor dimensions
148 * @dims: (array length=num_dims): size of tensor in each dimension.
149 * A value of 0 means the dimension is dynamic.
150 * @tensor_mtd: (out)(nullable): Handle update with newly added tensor mtd.
151 * Add tensor mtd to @instance.
152 *
153 * Add a new #GstAnalyticsTensorMtd holding a #GstTensor to @instance.
154 *
155 * Returns: Added successfully
156 *
157 * Since: 1.28
158 */
159 gboolean
160 gst_analytics_relation_meta_add_tensor_mtd_simple (GstAnalyticsRelationMeta
161 * meta, GQuark id, GstTensorDataType data_type, GstBuffer * data,
162 GstTensorDimOrder dims_order, gsize num_dims, gsize * dims,
163 GstAnalyticsTensorMtd * tensor_mtd)
164 {
165 GstTensor *tensor;
166 GstAnalyticsTensorMtd mtd;
167
168 if (!gst_analytics_relation_meta_add_tensor_mtd (meta, num_dims, &mtd))
169 return FALSE;
170
171 tensor = gst_analytics_relation_meta_get_mtd_data (meta, mtd.id);
172
173 if (!gst_tensor_set_simple (tensor, id, data_type, data,
174 dims_order, num_dims, dims))
175 return FALSE;
176
177 if (tensor_mtd)
178 *tensor_mtd = mtd;
179
180 return TRUE;
181 }
182
183 static void
184 gst_analytics_tensor_mtd_clear (GstBuffer * buffer, GstAnalyticsMtd * mtd)
185 {
186 GstAnalyticsTensorMtdData *tensor;
187 gsize num_dims;
188
189 tensor = gst_analytics_relation_meta_get_mtd_data (mtd->meta, mtd->id);
190 g_assert (tensor);
191
192 num_dims = tensor->num_dims;
193 if (tensor->data)
194 gst_buffer_unref (tensor->data);
195
196 memset (tensor, 0, sizeof (GstTensor));
197 tensor->num_dims = num_dims;
198 }
199
200 static gboolean
201 gst_analytics_tensor_mtd_transform (GstBuffer * transbuf,
202 GstAnalyticsMtd * transmtd, GstBuffer * buffer, GQuark type, gpointer data)
203 {
204 GstTensor *tensor;
205
206 tensor = gst_analytics_relation_meta_get_mtd_data (transmtd->meta,
207 transmtd->id);
208
209 if (tensor->data)
210 tensor->data = gst_buffer_ref (tensor->data);
211
212 return TRUE;
213 }
214
215 /**
216 * gst_analytics_relation_meta_get_tensor_mtd:
217 * @meta: Instance of #GstAnalyticsRelationMeta
218 * @an_meta_id: Id of #GstAnalyticsTensorMtd instance to retrieve
219 * @rlt: (out caller-allocates)(not nullable): Will be filled with relatable
220 * meta
221 *
222 * Fill @rlt if a analytics-meta with id == @an_meta_id exist in @meta instance,
223 * otherwise this method return FALSE and @rlt is invalid.
224 *
225 * Returns: TRUE if successful.
226 *
227 * Since: 1.28
228 */
229 gboolean
230 gst_analytics_relation_meta_get_tensor_mtd (GstAnalyticsRelationMeta * meta,
231 guint an_meta_id, GstAnalyticsTensorMtd * rlt)
232 {
233 return gst_analytics_relation_meta_get_mtd (meta, an_meta_id,
234 gst_analytics_tensor_mtd_get_mtd_type (), (GstAnalyticsTensorMtd *) rlt);
235 }