From d934ea3f7ad31e300d78ba57177a8c7a22174520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Cr=C3=AAte?= Date: Mon, 4 Nov 2024 19:30:02 -0500 Subject: [PATCH] tensormeta: Add APIs to create and access GstTensorMeta contents Also document those APIs better. Part-of: --- .../gst-libs/gst/analytics/gsttensor.h | 2 - .../gst-libs/gst/analytics/gsttensormeta.c | 65 ++++++++++++++++++- .../gst-libs/gst/analytics/gsttensormeta.h | 21 ++++++ .../gst/tensordecoders/gstssdobjectdetector.c | 12 ++-- 4 files changed, 89 insertions(+), 11 deletions(-) diff --git a/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensor.h b/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensor.h index 2585fbcd8c..ea686c2462 100644 --- a/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensor.h +++ b/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensor.h @@ -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__ */ diff --git a/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensormeta.c b/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensormeta.c index 26243a31ab..0bd705c074 100644 --- a/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensormeta.c +++ b/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensormeta.c @@ -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; } diff --git a/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensormeta.h b/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensormeta.h index f997a18fc5..4c64a219d8 100644 --- a/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensormeta.h +++ b/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensormeta.h @@ -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); diff --git a/subprojects/gst-plugins-bad/gst/tensordecoders/gstssdobjectdetector.c b/subprojects/gst-plugins-bad/gst/tensordecoders/gstssdobjectdetector.c index 1f69fea77a..6a23bb399e 100644 --- a/subprojects/gst-plugins-bad/gst/tensordecoders/gstssdobjectdetector.c +++ b/subprojects/gst-plugins-bad/gst/tensordecoders/gstssdobjectdetector.c @@ -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",