[Common] function to get data size
authorJaeyun <jy1210.jung@samsung.com>
Thu, 9 Jul 2020 10:25:48 +0000 (19:25 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Wed, 29 Jul 2020 08:06:05 +0000 (17:06 +0900)
Add new function to get data size from tensors info.
If index is negative value, this will return total data size of tensors.

Signed-off-by: Jaeyun <jy1210.jung@samsung.com>
gst/nnstreamer/include/nnstreamer_plugin_api.h
gst/nnstreamer/tensor_common.c
tests/common/unittest_common.cc

index 56057de..7fca43f 100644 (file)
@@ -108,6 +108,15 @@ extern void
 gst_tensors_info_free (GstTensorsInfo * info);
 
 /**
+ * @brief Get data size of single tensor
+ * @param info tensors info structure
+ * @param index the index of tensor (-1 to get total size of tensors)
+ * @return data size
+ */
+gsize
+gst_tensors_info_get_size (const GstTensorsInfo * info, gint index);
+
+/**
  * @brief Parse the string of dimensions
  * @param info tensors info structure
  * @param dim_string string of dimensions
index 12c0466..36cb127 100644 (file)
@@ -280,6 +280,31 @@ gst_tensors_info_free (GstTensorsInfo * info)
 }
 
 /**
+ * @brief Get data size of single tensor
+ * @param info tensors info structure
+ * @param index the index of tensor (-1 to get total size of tensors)
+ * @return data size
+ */
+gsize
+gst_tensors_info_get_size (const GstTensorsInfo * info, gint index)
+{
+  gsize data_size = 0;
+  guint i;
+
+  g_return_val_if_fail (info != NULL, 0);
+  g_return_val_if_fail (index < (gint) info->num_tensors, 0);
+
+  if (index < 0) {
+    for (i = 0; i < info->num_tensors; ++i)
+      data_size += gst_tensor_info_get_size (&info->info[i]);
+  } else {
+    data_size = gst_tensor_info_get_size (&info->info[index]);
+  }
+
+  return data_size;
+}
+
+/**
  * @brief Check the tensors info is valid
  * @param info tensors info structure
  * @return TRUE if info is valid
index 3c6a38e..dddaab5 100644 (file)
@@ -448,6 +448,68 @@ fill_tensors_config_for_test (GstTensorsConfig * conf1, GstTensorsConfig * conf2
 }
 
 /**
+ * @brief Test for data size.
+ */
+TEST (common_tensor_info, size_01_p)
+{
+  GstTensorsInfo info1, info2;
+  gsize size1, size2;
+  guint i;
+
+  fill_tensors_info_for_test (&info1, &info2);
+
+  size1 = gst_tensor_info_get_size (&info1.info[0]);
+  size2 = gst_tensors_info_get_size (&info1, 0);
+
+  EXPECT_TRUE (size1 == size2);
+
+  size1 = 0;
+  for (i = 0; i < info2.num_tensors; i++) {
+    size1 += gst_tensor_info_get_size (&info2.info[i]);
+  }
+
+  size2 = gst_tensors_info_get_size (&info2, -1);
+
+  EXPECT_TRUE (size1 == size2);
+}
+
+/**
+ * @brief Test for data size.
+ */
+TEST (common_tensor_info, size_02_n)
+{
+  GstTensorsInfo info1, info2;
+  gsize size1;
+  gint index;
+
+  fill_tensors_info_for_test (&info1, &info2);
+
+  /* get size with null param */
+  index = (gint) info1.num_tensors - 1;
+  size1 = gst_tensors_info_get_size (NULL, index);
+
+  EXPECT_TRUE (size1 == 0);
+}
+
+/**
+ * @brief Test for data size.
+ */
+TEST (common_tensor_info, size_03_n)
+{
+  GstTensorsInfo info1, info2;
+  gsize size1;
+  gint index;
+
+  fill_tensors_info_for_test (&info1, &info2);
+
+  /* get size with invalid index */
+  index = (gint) info1.num_tensors;
+  size1 = gst_tensors_info_get_size (&info1, index);
+
+  EXPECT_TRUE (size1 == 0);
+}
+
+/**
  * @brief Test for same tensors info.
  */
 TEST (common_tensor_info, equal_01_p)