[Common] add function to get tensor size
authorJaeyun <jy1210.jung@samsung.com>
Fri, 28 Sep 2018 04:22:11 +0000 (13:22 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Fri, 28 Sep 2018 05:10:28 +0000 (14:10 +0900)
add function to get date size with GstTensorInfo
and refactor elements and examples

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
common/tensor_common.c
gst/tensor_filter/tensor_filter.c
include/tensor_common.h
nnstreamer_example/custom_example_passthrough/nnstreamer_customfilter_example_passthrough.c
nnstreamer_example/custom_example_passthrough/nnstreamer_customfilter_example_passthrough_variable.c
nnstreamer_example/custom_example_scaler/nnstreamer_customfilter_example_scaler_allocator.c
tests/nnstreamer_tensors/tensors_test/gsttensorscheck.c

index fcc8ceb..a078ea7 100644 (file)
@@ -156,6 +156,24 @@ gst_tensor_info_is_equal (const GstTensorInfo * i1, const GstTensorInfo * i2)
 }
 
 /**
+ * @brief Get data size of single tensor
+ * @param info tensor info structure
+ * @return data size
+ */
+gsize
+gst_tensor_info_get_size (const GstTensorInfo * info)
+{
+  gsize data_size;
+
+  g_return_val_if_fail (info != NULL, 0);
+
+  data_size = get_tensor_element_count (info->dimension) *
+      tensor_element_size[info->type];
+
+  return data_size;
+}
+
+/**
  * @brief Initialize the tensors info structure
  * @param info tensors info structure to be initialized
  */
@@ -972,7 +990,7 @@ get_tensor_dimension_string (const tensor_dim dim)
  * @param dim The tensor dimension
  */
 size_t
-get_tensor_element_count (const uint32_t dim[NNS_TENSOR_RANK_LIMIT])
+get_tensor_element_count (const tensor_dim dim)
 {
   size_t count = 1;
   int i;
index 890f722..bfe146f 100644 (file)
@@ -356,16 +356,12 @@ gst_tensor_filter_out_size (GstTensorFilter * self, gint index)
   if (index < 0) {
     /** calculate all output tensors */
     for (i = 0; i < info->num_tensors; i++) {
-      out_size +=
-          get_tensor_element_count (info->info[i].dimension) *
-          tensor_element_size[info->info[i].type];
+      out_size += gst_tensor_info_get_size (&info->info[i]);
     }
   } else {
     g_assert (index < info->num_tensors);
 
-    out_size =
-        get_tensor_element_count (info->info[index].dimension) *
-        tensor_element_size[info->info[index].type];
+    out_size = gst_tensor_info_get_size (&info->info[index]);
   }
 
   return out_size;
index 9433a16..f3f169a 100644 (file)
@@ -184,6 +184,14 @@ extern gboolean
 gst_tensor_info_is_equal (const GstTensorInfo * i1, const GstTensorInfo * i2);
 
 /**
+ * @brief Get data size of single tensor
+ * @param info tensor info structure
+ * @return data size
+ */
+extern gsize
+gst_tensor_info_get_size (const GstTensorInfo * info);
+
+/**
  * @brief Initialize the tensors info structure
  * @param info tensors info structure to be initialized
  */
index 60d9091..9ba4819 100644 (file)
@@ -29,8 +29,7 @@
 typedef struct _pt_data
 {
   uint32_t id; /***< Just for testing */
-  tensor_dim dim;
-  tensor_type type;
+  GstTensorInfo info; /**< tensor info */
 } pt_data;
 
 /**
@@ -43,12 +42,12 @@ pt_init (const GstTensorFilterProperties * prop)
   int i;
 
   data->id = 0;
-  data->dim[0] = D1;
-  data->dim[1] = D2;
-  data->dim[2] = D3;
+  data->info.dimension[0] = D1;
+  data->info.dimension[1] = D2;
+  data->info.dimension[2] = D3;
   for (i = 3; i < NNS_TENSOR_RANK_LIMIT; i++)
-    data->dim[i] = 1;
-  data->type = _NNS_UINT8;
+    data->info.dimension[i] = 1;
+  data->info.type = _NNS_UINT8;
 
   return data;
 }
@@ -72,18 +71,12 @@ get_inputDim (void *private_data, const GstTensorFilterProperties * prop,
     GstTensorsInfo * info)
 {
   pt_data *data = private_data;
-  int i;
 
   g_assert (data);
   g_assert (NNS_TENSOR_RANK_LIMIT >= 3);
 
-  info->info[0].dimension[0] = D1;
-  info->info[0].dimension[1] = D2;
-  info->info[0].dimension[2] = D3;
-  for (i = 3; i < NNS_TENSOR_RANK_LIMIT; i++)
-    info->info[0].dimension[i] = 1;
-  info->info[0].type = _NNS_UINT8;
   info->num_tensors = 1;
+  info->info[0] = data->info;
   return 0;
 }
 
@@ -95,18 +88,12 @@ get_outputDim (void *private_data, const GstTensorFilterProperties * prop,
     GstTensorsInfo * info)
 {
   pt_data *data = private_data;
-  int i;
 
   g_assert (data);
   g_assert (NNS_TENSOR_RANK_LIMIT >= 3);
 
-  info->info[0].dimension[0] = D1;
-  info->info[0].dimension[1] = D2;
-  info->info[0].dimension[2] = D3;
-  for (i = 3; i < NNS_TENSOR_RANK_LIMIT; i++)
-    info->info[0].dimension[i] = 1;
-  info->info[0].type = _NNS_UINT8;
   info->num_tensors = 1;
+  info->info[0] = data->info;
   return 0;
 }
 
@@ -124,7 +111,7 @@ pt_invoke (void *private_data, const GstTensorFilterProperties * prop,
   g_assert (input);
   g_assert (output);
 
-  size = get_tensor_element_count (data->dim) * tensor_element_size[data->type];
+  size = gst_tensor_info_get_size (&data->info);
 
   g_assert (input[0].data != output[0].data);
   memcpy (output[0].data, input[0].data, size);
index febb352..1ad1840 100644 (file)
@@ -90,8 +90,7 @@ pt_invoke (void *private_data, const GstTensorFilterProperties * prop,
   g_assert (output);
 
   for (t = 0; t < prop->output_meta.num_tensors; t++) {
-    size = get_tensor_element_count (prop->output_meta.info[t].dimension) *
-        tensor_element_size[prop->output_meta.info[t].type];
+    size = gst_tensor_info_get_size (&prop->output_meta.info[t]);
 
     g_assert (input[t].data != output[t].data);
     memcpy (output[t].data, input[t].data, size);
index 974bbb4..449e75b 100644 (file)
@@ -152,10 +152,8 @@ pt_allocate_invoke (void *private_data,
 
   /* allocate output data */
   elementsize = tensor_element_size[prop->output_meta.info[0].type];
+  size = gst_tensor_info_get_size (&prop->output_meta.info[0]);
 
-  size =
-      elementsize *
-      get_tensor_element_count (prop->output_meta.info[0].dimension);
   output[0].data = malloc (size);
 
   /* This assumes the limit is 4 */
index aad5695..79b4d24 100644 (file)
@@ -249,8 +249,7 @@ gst_tensors_check (Gsttensorscheck * filter, GstBuffer * inbuf)
   gst_buffer_map (inbuf, &src_info, GST_MAP_READ);
 
   /** Making output buffer (one big buffer for check tensors) */
-  out_size = get_tensor_element_count (filter->out_config.info.dimension) *
-      tensor_element_size[filter->out_config.info.type];
+  out_size = gst_tensor_info_get_size (&filter->out_config.info);
   outbuf = gst_buffer_new_allocate (NULL, out_size, NULL);
   gst_buffer_map (outbuf, &dest_info, GST_MAP_WRITE);