[Converter/Flatbuf,Flexbuf,Protobuf] Remove duplicated code
authorMyungJoo Ham <myungjoo.ham@samsung.com>
Wed, 26 May 2021 02:52:01 +0000 (11:52 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Sat, 29 May 2021 23:20:29 +0000 (08:20 +0900)
Remove the duplicated function definition.
Create tensor-converter extra subplugin's common util.

Signed-off-by: MyungJoo Ham <myungjoo.ham@samsung.com>
ext/nnstreamer/tensor_converter/meson.build
ext/nnstreamer/tensor_converter/tensor_converter_flatbuf.cc
ext/nnstreamer/tensor_converter/tensor_converter_flexbuf.cc
ext/nnstreamer/tensor_converter/tensor_converter_protobuf.cc
ext/nnstreamer/tensor_converter/tensor_converter_util.c [new file with mode: 0644]
ext/nnstreamer/tensor_converter/tensor_converter_util.h [new file with mode: 0644]
jni/nnstreamer.mk

index 7f505d2..f641186 100644 (file)
@@ -1,7 +1,8 @@
 # flatbuffer
 if flatbuf_support_is_available
   converter_sub_flatbuf_sources = [
-    'tensor_converter_flatbuf.cc'
+    'tensor_converter_flatbuf.cc',
+    'tensor_converter_util.c'
   ]
 
   nnstreamer_converter_flatbuf_sources = [fb_gen_src]
@@ -17,7 +18,8 @@ if flatbuf_support_is_available
   )
 
   converter_sub_flexbuf_sources = [
-    'tensor_converter_flexbuf.cc'
+    'tensor_converter_flexbuf.cc',
+    'tensor_converter_util.c'
   ]
 
   nnstreamer_converter_flexbuf_sources = []
@@ -36,7 +38,8 @@ endif
 # protocol buffer
 if protobuf_support_is_available
   converter_sub_protobuf_sources = [
-    'tensor_converter_protobuf.cc'
+    'tensor_converter_protobuf.cc',
+    'tensor_converter_util.c'
   ]
 
   nnstreamer_converter_protobuf_sources = []
index b01f668..1887b77 100644 (file)
@@ -34,6 +34,7 @@
 #include <nnstreamer_plugin_api.h>
 #include <typeinfo>
 #include "nnstreamer_plugin_api_converter.h"
+#include "tensor_converter_util.h"
 
 namespace nnstreamer
 {
@@ -55,36 +56,6 @@ fbc_query_caps (const GstTensorsConfig *config)
   return gst_caps_from_string (GST_FLATBUF_TENSOR_CAP_DEFAULT);
 }
 
-/** @brief tensor converter plugin's NNStreamerExternalConverter callback */
-static gboolean
-fbc_get_out_config (const GstCaps *in_cap, GstTensorsConfig *config)
-{
-  GstStructure *structure;
-  g_return_val_if_fail (config != NULL, FALSE);
-  gst_tensors_config_init (config);
-  g_return_val_if_fail (in_cap != NULL, FALSE);
-
-  structure = gst_caps_get_structure (in_cap, 0);
-  g_return_val_if_fail (structure != NULL, FALSE);
-
-  /* All tensor info should be updated later in chain function. */
-  config->info.info[0].type = _NNS_UINT8;
-  config->info.num_tensors = 1;
-  if (gst_tensor_parse_dimension ("1:1:1:1", config->info.info[0].dimension) == 0) {
-    ml_loge ("Failed to set initial dimension for subplugin");
-    return FALSE;
-  }
-
-  if (gst_structure_has_field (structure, "framerate")) {
-    gst_structure_get_fraction (structure, "framerate", &config->rate_n, &config->rate_d);
-  } else {
-    /* cannot get the framerate */
-    config->rate_n = 0;
-    config->rate_d = 1;
-  }
-  return TRUE;
-}
-
 /** @brief tensor converter plugin's NNStreamerExternalConverter callback
  *  @todo : Consider multi frames, return Bufferlist and
  *          remove frame size and the number of frames
@@ -162,7 +133,7 @@ static const gchar converter_subplugin_flatbuf[] = "flatbuf";
 static NNStreamerExternalConverter flatBuf = {
   .name = converter_subplugin_flatbuf,
   .convert = fbc_convert,
-  .get_out_config = fbc_get_out_config,
+  .get_out_config = tcu_get_out_config,
   .query_caps = fbc_query_caps
 };
 
index 180c486..57ec87e 100644 (file)
@@ -61,6 +61,7 @@
 #include <nnstreamer_plugin_api.h>
 #include <nnstreamer_plugin_api_converter.h>
 #include <flatbuffers/flexbuffers.h>
+#include "tensor_converter_util.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -78,36 +79,6 @@ flxc_query_caps (const GstTensorsConfig *config)
   return gst_caps_from_string (GST_FLEXBUF_CAP_DEFAULT);
 }
 
-/** @brief tensor converter plugin's NNStreamerExternalConverter callback */
-static gboolean
-flxc_get_out_config (const GstCaps *in_cap, GstTensorsConfig *config)
-{
-  GstStructure *structure;
-  g_return_val_if_fail (config != NULL, FALSE);
-  gst_tensors_config_init (config);
-  g_return_val_if_fail (in_cap != NULL, FALSE);
-
-  structure = gst_caps_get_structure (in_cap, 0);
-  g_return_val_if_fail (structure != NULL, FALSE);
-
-  /* All tensor info should be updated later in chain function. */
-  config->info.info[0].type = _NNS_UINT8;
-  config->info.num_tensors = 1;
-  if (gst_tensor_parse_dimension ("1:1:1:1", config->info.info[0].dimension) == 0) {
-    ml_loge ("Failed to set initial dimension for subplugin");
-    return FALSE;
-  }
-
-  if (gst_structure_has_field (structure, "framerate")) {
-    gst_structure_get_fraction (structure, "framerate", &config->rate_n, &config->rate_d);
-  } else {
-    /* cannot get the framerate */
-    config->rate_n = 0;
-    config->rate_d = 1;
-  }
-  return TRUE;
-}
-
 /** @brief tensor converter plugin's NNStreamerExternalConverter callback
  */
 static GstBuffer *
@@ -180,7 +151,7 @@ static const gchar converter_subplugin_flexbuf[] = "flexbuf";
 static NNStreamerExternalConverter flexBuf = {
   .name = converter_subplugin_flexbuf,
   .convert = flxc_convert,
-  .get_out_config = flxc_get_out_config,
+  .get_out_config = tcu_get_out_config,
   .query_caps = flxc_query_caps
 };
 
index 004490d..613717c 100644 (file)
@@ -35,6 +35,7 @@
 #include <typeinfo>
 #include "nnstreamer_plugin_api_converter.h"
 #include "nnstreamer_protobuf.h"
+#include "tensor_converter_util.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -52,37 +53,6 @@ pbc_query_caps (const GstTensorsConfig *config)
 }
 
 /** @brief tensor converter plugin's NNStreamerExternalConverter callback */
-static gboolean
-pbc_get_out_config (const GstCaps *in_cap, GstTensorsConfig *config)
-{
-  GstStructure *structure;
-
-  g_return_val_if_fail (config != NULL, FALSE);
-  gst_tensors_config_init (config);
-  g_return_val_if_fail (in_cap != NULL, FALSE);
-
-  structure = gst_caps_get_structure (in_cap, 0);
-  g_return_val_if_fail (structure != NULL, FALSE);
-
-  /* All tensor info should be updated later in chain function. */
-  config->info.info[0].type = _NNS_UINT8;
-  config->info.num_tensors = 1;
-  if (gst_tensor_parse_dimension ("1:1:1:1", config->info.info[0].dimension) == 0) {
-    ml_loge ("Failed to set initial dimension for subplugin");
-    return FALSE;
-  }
-
-  if (gst_structure_has_field (structure, "framerate")) {
-    gst_structure_get_fraction (structure, "framerate", &config->rate_n, &config->rate_d);
-  } else {
-    /* cannot get the framerate */
-    config->rate_n = 0;
-    config->rate_d = 1;
-  }
-  return TRUE;
-}
-
-/** @brief tensor converter plugin's NNStreamerExternalConverter callback */
 static GstBuffer *
 pbc_convert (GstBuffer *in_buf, GstTensorsConfig *config, void *priv_data)
 {
@@ -95,7 +65,7 @@ static gchar converter_subplugin_protobuf[] = "protobuf";
 static NNStreamerExternalConverter protobuf = {
   .name = converter_subplugin_protobuf,
   .convert = pbc_convert,
-  .get_out_config = pbc_get_out_config,
+  .get_out_config = tcu_get_out_config,
   .query_caps = pbc_query_caps
 };
 
diff --git a/ext/nnstreamer/tensor_converter/tensor_converter_util.c b/ext/nnstreamer/tensor_converter/tensor_converter_util.c
new file mode 100644 (file)
index 0000000..250cb1f
--- /dev/null
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: LGPL-2.1-only */
+/**
+* @file        tensor_converter_util.h
+* @date        26 May 2021
+* @brief       Utility functions for NNStreamer tensor-converter subplugins.
+* @see         https://github.com/nnstreamer/nnstreamer
+* @author      MyungJoo Ham <myungjoo.ham@samsung.com>
+* @bug         No known bugs except for NYI items
+*/
+
+#include <glib.h>
+#include <gst/gst.h>
+#include <nnstreamer_log.h>
+#include <nnstreamer_plugin_api.h>
+#include "tensor_converter_util.h"
+
+/** @brief tensor converter plugin's NNStreamerExternalConverter callback */
+gboolean
+tcu_get_out_config (const GstCaps * in_cap, GstTensorsConfig * config)
+{
+  GstStructure *structure;
+  g_return_val_if_fail (config != NULL, FALSE);
+  gst_tensors_config_init (config);
+  g_return_val_if_fail (in_cap != NULL, FALSE);
+
+  structure = gst_caps_get_structure (in_cap, 0);
+  g_return_val_if_fail (structure != NULL, FALSE);
+
+  /* All tensor info should be updated later in chain function. */
+  config->info.info[0].type = _NNS_UINT8;
+  config->info.num_tensors = 1;
+  if (gst_tensor_parse_dimension ("1:1:1:1",
+          config->info.info[0].dimension) == 0) {
+    ml_loge ("Failed to set initial dimension for subplugin");
+    return FALSE;
+  }
+
+  if (gst_structure_has_field (structure, "framerate")) {
+    gst_structure_get_fraction (structure, "framerate", &config->rate_n,
+        &config->rate_d);
+  } else {
+    /* cannot get the framerate */
+    config->rate_n = 0;
+    config->rate_d = 1;
+  }
+  return TRUE;
+}
diff --git a/ext/nnstreamer/tensor_converter/tensor_converter_util.h b/ext/nnstreamer/tensor_converter/tensor_converter_util.h
new file mode 100644 (file)
index 0000000..8452a09
--- /dev/null
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: LGPL-2.1-only */
+/**
+* @file        tensor_converter_util.h
+* @date        26 May 2021
+* @brief       Utility functions for NNStreamer tensor-converter subplugins.
+* @see         https://github.com/nnstreamer/nnstreamer
+* @author      MyungJoo Ham <myungjoo.hamt@samsung.com>
+* @bug         No known bugs except for NYI items
+*/
+#ifndef _TENSOR_CONVERTER_UTIL_H_
+#define _TENSOR_CONVERTER_UTIL_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <gst/gst.h>
+#include <nnstreamer_plugin_api.h>
+
+/** @brief tensor converter plugin's NNStreamerExternalConverter callback */
+gboolean tcu_get_out_config (const GstCaps *in_cap, GstTensorsConfig *config);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _TENSOR_CONVERTER_UTIL_H_ */
index 8f66f3d..6040254 100644 (file)
@@ -99,10 +99,12 @@ NNSTREAMER_FILTER_SNAP_SRCS := \
 
 # converter flatbuffers
 NNSTREAMER_CONVERTER_FLATBUF_SRCS := \
+    $(NNSTREAMER_EXT_HOME)/tensor_converter/tensor_converter_util.c \
     $(NNSTREAMER_EXT_HOME)/tensor_converter/tensor_converter_flatbuf.cc
 
 # converter flexbuffers
 NNSTREAMER_CONVERTER_FLEXBUF_SRCS := \
+    $(NNSTREAMER_EXT_HOME)/tensor_converter/tensor_converter_util.c \
     $(NNSTREAMER_EXT_HOME)/tensor_converter/tensor_converter_flexbuf.cc
 
 # decoder flatbuffers