tensormeta: Add APIs to create and access GstTensorMeta contents

Also document those APIs better.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/6000>
This commit is contained in:
Olivier Crête 2024-11-04 19:30:02 -05:00 committed by GStreamer Marge Bot
parent 5e73e8e1b3
commit d934ea3f7a
4 changed files with 89 additions and 11 deletions

View file

@ -174,8 +174,6 @@ GstTensorDim * gst_tensor_get_dims (GstTensor * tensor, gsize * num_dims);
GST_ANALYTICS_META_API
GType gst_tensor_get_type (void);
#define GST_TENSOR_MISSING_ID -1
G_END_DECLS
#endif /* __GST_TENSOR_H__ */

View file

@ -44,6 +44,11 @@ gst_tensor_meta_free (GstMeta * meta, GstBuffer * buffer)
g_free (tmeta->tensors);
}
/**
* gst_tensor_meta_api_get_type: (skip)
*
* Since: 1.26
*/
GType
gst_tensor_meta_api_get_type (void)
{
@ -123,6 +128,64 @@ gst_buffer_get_tensor_meta (GstBuffer * buffer)
GST_TENSOR_META_API_TYPE);
}
/**
* gst_tensor_meta_set:
* @tmeta: a #GstTensorMeta
* @num_tensors: The number of tensors in the @tensors array
* @tensors: (in) (array length=num_tensors) (transfer full): An array of poiners to #GstTensor
*
* Sets tensors into the #GstTensorMeta
*
* Since: 1.26
*/
void
gst_tensor_meta_set (GstTensorMeta * tmeta, guint num_tensors,
GstTensor ** tensors)
{
guint i;
for (i = 0; i < tmeta->num_tensors; i++) {
gst_tensor_free (tmeta->tensors[i]);
}
g_free (tmeta->tensors);
tmeta->num_tensors = num_tensors;
tmeta->tensors = tensors;
}
/**
* gst_tensor_meta_get:
* @tmeta: A #GstTensorMeta
* @index: The number of the tensor to get
*
* Retrieves a tensor from the #GstTensorMeta, the index must be
* smaller than #GstTensorMeta.num_tensors
*
* Return: (transfer none): a GstTensor
*
* Since: 1.26
*/
const GstTensor *
gst_tensor_meta_get (GstTensorMeta * tmeta, gsize index)
{
g_return_val_if_fail (tmeta->tensors, NULL);
g_return_val_if_fail (index < tmeta->num_tensors, NULL);
return tmeta->tensors[index];
}
/**
* gst_tensor_meta_get_index_from_id:
* @meta: a #GstTensorMeta
* @id: The tensor id to look for
*
* Finds the first tensor with the requsted ID in the meta
*
* Return: The index of the tensor inthe meta, or -1 if
* its not found.
*
* Since: 1.26
*/
gint
gst_tensor_meta_get_index_from_id (GstTensorMeta * meta, GQuark id)
{
@ -131,5 +194,5 @@ gst_tensor_meta_get_index_from_id (GstTensorMeta * meta, GQuark id)
return i;
}
return GST_TENSOR_MISSING_ID;
return -1;
}

View file

@ -49,9 +49,23 @@ typedef struct _GstTensorMeta
G_BEGIN_DECLS
/**
* GST_TENSOR_META_API_TYPE:
*
* The Tensor Meta API type
*
* Since: 1.26
*/
#define GST_TENSOR_META_API_TYPE \
(gst_tensor_meta_api_get_type())
/**
* GST_TENSOR_META_INFO: (skip)
*
* The Tensor Meta API Info
*
* Since: 1.26
*/
#define GST_TENSOR_META_INFO \
(gst_tensor_meta_get_info())
@ -61,6 +75,13 @@ GType gst_tensor_meta_api_get_type (void);
GST_ANALYTICS_META_API
const GstMetaInfo *gst_tensor_meta_get_info (void);
GST_ANALYTICS_META_API
void gst_tensor_meta_set (GstTensorMeta *tmeta, guint num_tensors,
GstTensor **tensors);
GST_ANALYTICS_META_API
const GstTensor *gst_tensor_meta_get (GstTensorMeta *tmeta, gsize index);
GST_ANALYTICS_META_API
gint gst_tensor_meta_get_index_from_id(GstTensorMeta *meta, GQuark id);

View file

@ -338,12 +338,10 @@ gst_ssd_object_detector_get_tensor_meta (GstSsdObjectDetector * object_detector,
gint clasesIndex = gst_tensor_meta_get_index_from_id (tensor_meta,
g_quark_from_static_string (GST_MODEL_OBJECT_DETECTOR_CLASSES));
if (boxesIndex == GST_TENSOR_MISSING_ID
|| scoresIndex == GST_TENSOR_MISSING_ID
|| numDetectionsIndex == GST_TENSOR_MISSING_ID)
if (boxesIndex == -1 || scoresIndex == -1 || numDetectionsIndex == -1)
continue;
if (tensor_meta->num_tensors == 4 && clasesIndex == GST_TENSOR_MISSING_ID)
if (tensor_meta->num_tensors == 4 && clasesIndex == -1)
continue;
return tensor_meta;
@ -435,9 +433,7 @@ DEFINE_GET_FUNC (guint32, UINT32_MAX)
boxes_index = gst_tensor_meta_get_index_from_id (tmeta,
g_quark_from_static_string (GST_MODEL_OBJECT_DETECTOR_BOXES));
if (numdetect_index == GST_TENSOR_MISSING_ID
|| scores_index == GST_TENSOR_MISSING_ID
|| numdetect_index == GST_TENSOR_MISSING_ID) {
if (numdetect_index == -1 || scores_index == -1 || numdetect_index == -1) {
GST_WARNING ("Missing tensor data expected for SSD model");
return;
}
@ -463,7 +459,7 @@ DEFINE_GET_FUNC (guint32, UINT32_MAX)
goto cleanup;
}
if (classes_index != GST_TENSOR_MISSING_ID &&
if (classes_index != -1 &&
!gst_buffer_map (tmeta->tensors[classes_index]->data, &classes_map,
GST_MAP_READ)) {
GST_DEBUG_OBJECT (self, "Failed to map tensor memory for index %d",