test: add test for tensor-meta

- Verify we can add a tensor-meta to a buffer
- Verify we can get a tensor from a tensor-meta

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/8848>
This commit is contained in:
Daniel Morin 2025-04-15 16:28:38 -04:00
parent 93af941ce7
commit 185e96aeec

View File

@ -1798,6 +1798,60 @@ GST_START_TEST (test_iou_float)
GST_END_TEST; GST_END_TEST;
GST_START_TEST (test_get_tensor)
{
GstBuffer *buf, *tensor_data;
GstTensorMeta *tmeta;
GstTensor *tensor;
const GstTensor *tensor2;
GstTensor **tensors;
GQuark tensor_id = g_quark_from_string ("tensor-encoding-1");
gsize dims[] = { 1 };
gsize index;
/* Verify we can add a tensor-meta to a buffer */
/* Create tensor data */
guint8 *data = g_malloc0 (1);
*data = 28;
/* Wrap tensor data into a GstBuffer */
tensor_data = gst_buffer_new_wrapped_full (0, data, 1, 0, 1, data, g_free);
/* Create a new buffer where we attach tensor-meta */
buf = gst_buffer_new ();
tmeta = gst_buffer_add_tensor_meta (buf);
/* Create a tensor */
tensor = gst_tensor_new_simple (tensor_id, GST_TENSOR_DATA_TYPE_UINT8,
tensor_data, GST_TENSOR_DIM_ORDER_COL_MAJOR, 1, dims);
/* Create an array of tensor to fullfil GstTensor API */
tensors = g_new (GstTensor *, 1);
tensors[0] = tensor;
/* Set tensor-meta's tensors */
gst_tensor_meta_set (tmeta, 1, tensors);
/* Retieve tensor using index interface */
index = gst_tensor_meta_get_index_from_id (tmeta, tensor_id);
fail_unless (index == 0);
tensor2 = gst_tensor_meta_get (tmeta, index);
/* Verify tensor retrieved */
fail_unless (tensor == tensor2);
/* Retrieve tensor using tensor-id directly */
tensor2 = gst_tensor_meta_get_by_id (tmeta, tensor_id);
fail_unless (tensor == tensor2);
gst_buffer_unref (buf);
}
GST_END_TEST;
static Suite * static Suite *
analyticmeta_suite (void) analyticmeta_suite (void)
@ -1811,6 +1865,7 @@ analyticmeta_suite (void)
TCase *tc_chain_tracking; TCase *tc_chain_tracking;
TCase *tc_chain_segmentation; TCase *tc_chain_segmentation;
TCase *tc_chain_util; TCase *tc_chain_util;
TCase *tc_chain_tensors;
s = suite_create ("Analytic Meta Library"); s = suite_create ("Analytic Meta Library");
@ -1861,6 +1916,10 @@ analyticmeta_suite (void)
tcase_add_test (tc_chain_util, test_iou_int); tcase_add_test (tc_chain_util, test_iou_int);
tcase_add_test (tc_chain_util, test_iou_float); tcase_add_test (tc_chain_util, test_iou_float);
tc_chain_tensors = tcase_create ("TensorMeta");
suite_add_tcase (s, tc_chain_tensors);
tcase_add_test (tc_chain_tensors, test_get_tensor);
return s; return s;
} }