[Subplugin/Flatbuf] util to get nth info
authorJaeyun Jung <jy1210.jung@samsung.com>
Tue, 5 Sep 2023 04:40:13 +0000 (13:40 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Tue, 12 Sep 2023 02:42:53 +0000 (11:42 +0900)
Code clean, remove unnecessary copy of tensors-info and use util to get nth info.

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
ext/nnstreamer/tensor_converter/tensor_converter_flatbuf.cc
ext/nnstreamer/tensor_converter/tensor_converter_flexbuf.cc
ext/nnstreamer/tensor_decoder/tensordec-flatbuf.cc
ext/nnstreamer/tensor_decoder/tensordec-flexbuf.cc

index 83f7eed..60a004e 100644 (file)
@@ -74,6 +74,8 @@ fbc_convert (GstBuffer *in_buf, GstTensorsConfig *config, void *priv_data)
   GstMemory *in_mem, *out_mem;
   GstMapInfo in_info;
   guint mem_size;
+  GstTensorInfo *_info;
+
   UNUSED (priv_data);
 
   if (!in_buf || !config) {
@@ -108,12 +110,15 @@ fbc_convert (GstBuffer *in_buf, GstTensorsConfig *config, void *priv_data)
     std::string _name = tensor->Get (i)->name ()->str ();
     const gchar *name = _name.c_str ();
 
-    config->info.info[i].name = (name && strlen (name) > 0) ? g_strdup (name) : NULL;
-    config->info.info[i].type = (tensor_type) tensor->Get (i)->type ();
+    _info = gst_tensors_info_get_nth_info (&config->info, i);
+
+    g_free (_info->name);
+    _info->name = (name && strlen (name) > 0) ? g_strdup (name) : NULL;
+    _info->type = (tensor_type) tensor->Get (i)->type ();
     tensor_data = tensor->Get (i)->data ();
 
     for (guint j = 0; j < NNS_TENSOR_RANK_LIMIT; j++) {
-      config->info.info[i].dimension[j] = tensor->Get (i)->dimension ()->Get (j);
+      _info->dimension[j] = tensor->Get (i)->dimension ()->Get (j);
     }
     mem_size = VectorLength (tensor_data);
 
index 69a2367..f32c068 100644 (file)
@@ -91,6 +91,8 @@ flxc_convert (GstBuffer *in_buf, GstTensorsConfig *config, void *priv_data)
   GstMemory *in_mem, *out_mem;
   GstMapInfo in_info;
   gsize mem_size;
+  GstTensorInfo *_info;
+
   UNUSED (priv_data);
 
   if (!in_buf || !config) {
@@ -124,12 +126,15 @@ flxc_convert (GstBuffer *in_buf, GstTensorsConfig *config, void *priv_data)
     flexbuffers::String _name = tensor[0].AsString ();
     const gchar *name = _name.c_str ();
 
-    config->info.info[i].name = (name && strlen (name) > 0) ? g_strdup (name) : NULL;
-    config->info.info[i].type = (tensor_type) tensor[1].AsInt32 ();
+    _info = gst_tensors_info_get_nth_info (&config->info, i);
+
+    g_free (_info->name);
+    _info->name = (name && strlen (name) > 0) ? g_strdup (name) : NULL;
+    _info->type = (tensor_type) tensor[1].AsInt32 ();
 
     flexbuffers::TypedVector dim = tensor[2].AsTypedVector ();
     for (guint j = 0; j < NNS_TENSOR_RANK_LIMIT; j++) {
-      config->info.info[i].dimension[j] = dim[j].AsInt32 ();
+      _info->dimension[j] = dim[j].AsInt32 ();
     }
     flexbuffers::Blob tensor_data = tensor[3].AsBlob ();
     mem_size = gst_tensor_info_get_size (&config->info.info[i]);
index 57bea13..22874f3 100644 (file)
@@ -84,7 +84,6 @@ static GstFlowReturn
 fbd_decode (void **pdata, const GstTensorsConfig *config,
     const GstTensorMemory *input, GstBuffer *outbuf)
 {
-  char *name;
   Tensor_type type;
   Tensor_format format;
   GstMapInfo out_info;
@@ -101,7 +100,7 @@ fbd_decode (void **pdata, const GstTensorsConfig *config,
   frame_rate fr;
   gboolean is_flexible;
   GstTensorMetaInfo meta;
-  GstTensorsConfig fbd_config;
+  GstTensorInfo *_info;
 
   UNUSED (pdata);
 
@@ -109,30 +108,31 @@ fbd_decode (void **pdata, const GstTensorsConfig *config,
     ml_loge ("NULL parameter is passed to tensor_decoder::flatbuf");
     return GST_FLOW_ERROR;
   }
-  gst_tensors_config_copy (&fbd_config, config);
 
-  is_flexible = gst_tensors_config_is_flexible (&fbd_config);
+  is_flexible = gst_tensors_config_is_flexible (config);
 
-  num_tensors = fbd_config.info.num_tensors;
-  fr = frame_rate (fbd_config.rate_n, fbd_config.rate_d);
-  format = (Tensor_format) fbd_config.info.format;
+  num_tensors = config->info.num_tensors;
+  fr = frame_rate (config->rate_n, config->rate_d);
+  format = (Tensor_format) config->info.format;
   /* Fill the info in tensor and puth to tensor vector */
   for (i = 0; i < num_tensors; i++) {
     unsigned char *tmp_buf;
 
+    _info = gst_tensors_info_get_nth_info ((GstTensorsInfo *) &config->info, i);
+
     if (is_flexible) {
       gst_tensor_meta_info_parse_header (&meta, input[i].data);
-      gst_tensor_meta_info_convert (&meta, &fbd_config.info.info[i]);
+      gst_tensor_meta_info_convert (&meta, _info);
     }
-    dim = builder.CreateVector (fbd_config.info.info[i].dimension, NNS_TENSOR_RANK_LIMIT);
-    name = fbd_config.info.info[i].name;
 
-    if (name == NULL)
+    dim = builder.CreateVector (_info->dimension, NNS_TENSOR_RANK_LIMIT);
+
+    if (_info->name == NULL)
       tensor_name = builder.CreateString ("");
     else
-      tensor_name = builder.CreateString (name);
+      tensor_name = builder.CreateString (_info->name);
 
-    type = (Tensor_type) fbd_config.info.info[i].type;
+    type = (Tensor_type) _info->type;
 
     /* Create the vector first, and fill in data later */
     /** @todo Consider to remove memcpy */
index d591ec6..7629329 100644 (file)
@@ -125,41 +125,45 @@ flxd_decode (void **pdata, const GstTensorsConfig *config,
   flexbuffers::Builder fbb;
   gboolean is_flexible;
   GstTensorMetaInfo meta;
-  GstTensorsConfig flxd_config;
+  GstTensorInfo *_info;
+
   UNUSED (pdata);
 
   if (!config || !input || !outbuf) {
     ml_loge ("NULL parameter is passed to tensor_decoder::flexbuf");
     return GST_FLOW_ERROR;
   }
-  gst_tensors_config_copy (&flxd_config, config);
-  is_flexible = gst_tensors_config_is_flexible (&flxd_config);
 
-  num_tensors = flxd_config.info.num_tensors;
+  is_flexible = gst_tensors_config_is_flexible (config);
+
+  num_tensors = config->info.num_tensors;
   fbb.Map ([&] () {
     fbb.UInt ("num_tensors", num_tensors);
-    fbb.Int ("rate_n", flxd_config.rate_n);
-    fbb.Int ("rate_d", flxd_config.rate_d);
-    fbb.Int ("format", flxd_config.info.format);
+    fbb.Int ("rate_n", config->rate_n);
+    fbb.Int ("rate_d", config->rate_d);
+    fbb.Int ("format", config->info.format);
     for (i = 0; i < num_tensors; i++) {
       gchar *tensor_key = g_strdup_printf ("tensor_%d", i);
       gchar *tensor_name = NULL;
+
+      _info = gst_tensors_info_get_nth_info ((GstTensorsInfo *) &config->info, i);
+
       if (is_flexible) {
         gst_tensor_meta_info_parse_header (&meta, input[i].data);
-        gst_tensor_meta_info_convert (&meta, &flxd_config.info.info[i]);
+        gst_tensor_meta_info_convert (&meta, _info);
       }
-      tensor_name = flxd_config.info.info[i].name;
-      if (flxd_config.info.info[i].name == NULL) {
+
+      if (_info->name == NULL) {
         tensor_name = g_strdup ("");
       } else {
-        tensor_name = g_strdup (flxd_config.info.info[i].name);
+        tensor_name = g_strdup (_info->name);
       }
-      tensor_type type = flxd_config.info.info[i].type;
+      tensor_type type = _info->type;
 
       fbb.Vector (tensor_key, [&] () {
         fbb += tensor_name;
         fbb += type;
-        fbb.Vector (flxd_config.info.info[i].dimension, NNS_TENSOR_RANK_LIMIT);
+        fbb.Vector (_info->dimension, NNS_TENSOR_RANK_LIMIT);
         fbb.Blob (input[i].data, input[i].size);
       });
       g_free (tensor_key);