From 4ae76f8bf496dd283d21b65af0904642bfaeabf1 Mon Sep 17 00:00:00 2001 From: Jaeyun Date: Fri, 28 Sep 2018 13:22:11 +0900 Subject: [PATCH] [Common] add function to get tensor size add function to get date size with GstTensorInfo and refactor elements and examples Signed-off-by: Jaeyun Jung --- common/tensor_common.c | 20 +++++++++++++- gst/tensor_filter/tensor_filter.c | 8 ++---- include/tensor_common.h | 8 ++++++ .../nnstreamer_customfilter_example_passthrough.c | 31 +++++++--------------- ...mer_customfilter_example_passthrough_variable.c | 3 +-- ...treamer_customfilter_example_scaler_allocator.c | 4 +-- .../tensors_test/gsttensorscheck.c | 3 +-- 7 files changed, 41 insertions(+), 36 deletions(-) diff --git a/common/tensor_common.c b/common/tensor_common.c index fcc8ceb..a078ea7 100644 --- a/common/tensor_common.c +++ b/common/tensor_common.c @@ -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; diff --git a/gst/tensor_filter/tensor_filter.c b/gst/tensor_filter/tensor_filter.c index 890f722..bfe146f 100644 --- a/gst/tensor_filter/tensor_filter.c +++ b/gst/tensor_filter/tensor_filter.c @@ -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; diff --git a/include/tensor_common.h b/include/tensor_common.h index 9433a16..f3f169a 100644 --- a/include/tensor_common.h +++ b/include/tensor_common.h @@ -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 */ diff --git a/nnstreamer_example/custom_example_passthrough/nnstreamer_customfilter_example_passthrough.c b/nnstreamer_example/custom_example_passthrough/nnstreamer_customfilter_example_passthrough.c index 60d9091..9ba4819 100644 --- a/nnstreamer_example/custom_example_passthrough/nnstreamer_customfilter_example_passthrough.c +++ b/nnstreamer_example/custom_example_passthrough/nnstreamer_customfilter_example_passthrough.c @@ -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); diff --git a/nnstreamer_example/custom_example_passthrough/nnstreamer_customfilter_example_passthrough_variable.c b/nnstreamer_example/custom_example_passthrough/nnstreamer_customfilter_example_passthrough_variable.c index febb352..1ad1840 100644 --- a/nnstreamer_example/custom_example_passthrough/nnstreamer_customfilter_example_passthrough_variable.c +++ b/nnstreamer_example/custom_example_passthrough/nnstreamer_customfilter_example_passthrough_variable.c @@ -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); diff --git a/nnstreamer_example/custom_example_scaler/nnstreamer_customfilter_example_scaler_allocator.c b/nnstreamer_example/custom_example_scaler/nnstreamer_customfilter_example_scaler_allocator.c index 974bbb4..449e75b 100644 --- a/nnstreamer_example/custom_example_scaler/nnstreamer_customfilter_example_scaler_allocator.c +++ b/nnstreamer_example/custom_example_scaler/nnstreamer_customfilter_example_scaler_allocator.c @@ -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 */ diff --git a/tests/nnstreamer_tensors/tensors_test/gsttensorscheck.c b/tests/nnstreamer_tensors/tensors_test/gsttensorscheck.c index aad5695..79b4d24 100644 --- a/tests/nnstreamer_tensors/tensors_test/gsttensorscheck.c +++ b/tests/nnstreamer_tensors/tensors_test/gsttensorscheck.c @@ -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); -- 2.7.4