[Util] util function to compare tensor-config
authorJaeyun <jy1210.jung@samsung.com>
Wed, 2 Mar 2022 06:09:15 +0000 (15:09 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Wed, 2 Mar 2022 07:36:56 +0000 (16:36 +0900)
remove gst-dependency to compare tensor config.
add return val of invalid condition.

Signed-off-by: Jaeyun <jy1210.jung@samsung.com>
gst/nnstreamer/include/nnstreamer_plugin_api.h
gst/nnstreamer/include/nnstreamer_plugin_api_util.h
gst/nnstreamer/nnstreamer_plugin_api_impl.c
gst/nnstreamer/nnstreamer_plugin_api_util_impl.c
gst/nnstreamer/tensor_filter/tensor_filter_common.c

index 9834716..06b2689 100644 (file)
@@ -51,14 +51,6 @@ extern media_type
 gst_structure_get_media_type (const GstStructure * structure);
 
 /**
- * @brief Compare tensor config info (for other/tensors)
- * @param TRUE if equal
- */
-extern gboolean
-gst_tensors_config_is_equal (const GstTensorsConfig * c1,
-    const GstTensorsConfig * c2);
-
-/**
  * @brief Parse structure and set tensors config (for other/tensors)
  * @param config tensors config structure to be filled
  * @param structure structure to be interpreted
index 9ca26fa..8a0323f 100644 (file)
@@ -220,6 +220,14 @@ extern gboolean
 gst_tensors_config_validate (const GstTensorsConfig * config);
 
 /**
+ * @brief Compare tensor config info (for other/tensors)
+ * @param TRUE if equal
+ */
+extern gboolean
+gst_tensors_config_is_equal (const GstTensorsConfig * c1,
+    const GstTensorsConfig * c2);
+
+/**
  * @brief Copy tensors config
  */
 extern void
index 24fd4a7..6b939c1 100644 (file)
@@ -847,43 +847,6 @@ gst_structure_get_media_type (const GstStructure * structure)
 }
 
 /**
- * @brief Compare tensor config info
- * @param TRUE if equal
- */
-gboolean
-gst_tensors_config_is_equal (const GstTensorsConfig * c1,
-    const GstTensorsConfig * c2)
-{
-  g_return_val_if_fail (c1 != NULL, FALSE);
-  g_return_val_if_fail (c2 != NULL, FALSE);
-
-  if (!gst_tensors_config_validate (c1) || !gst_tensors_config_validate (c2)) {
-    return FALSE;
-  }
-
-  if (gst_util_fraction_compare (c1->rate_n, c1->rate_d, c2->rate_n,
-          c2->rate_d)) {
-    nns_logd ("Tensors config is not equal. framerate: %d/%d vs %d/%d.",
-        c1->rate_n, c1->rate_d, c2->rate_n, c2->rate_d);
-    return FALSE;
-  }
-
-  if (c1->format != c2->format || c1->format == _NNS_TENSOR_FORMAT_END) {
-    nns_logd ("Tensors config is not equal. format: %s vs %s ",
-        _STR_NULL (gst_tensor_get_format_string (c1->format)),
-        _STR_NULL (gst_tensor_get_format_string (c2->format)));
-    return FALSE;
-  }
-
-  /* cannot compare tensor info when tensor is not static */
-  if (!gst_tensors_config_is_static (c1)) {
-    return TRUE;
-  }
-
-  return gst_tensors_info_is_equal (&c1->info, &c2->info);
-}
-
-/**
  * @brief Parse caps from peer pad and set tensors config.
  * @param pad GstPad to get the capabilities
  * @param config tensors config structure to be filled
index b6ac5ed..f2f6f1d 100644 (file)
@@ -59,6 +59,59 @@ static const gchar *tensor_format_name[] = {
 };
 
 /**
+ * @brief Internal function, copied from gst_util_greatest_common_divisor() to remove dependency of gstreamer.
+ */
+static gint
+_gcd (gint a, gint b)
+{
+  while (b != 0) {
+    int temp = a;
+
+    a = b;
+    b = temp % b;
+  }
+
+  return ABS (a);
+}
+
+/**
+ * @brief Internal function, copied from gst_util_fraction_compare() to remove dependency of gstreamer.
+ */
+static gint
+_compare_rate (gint a_n, gint a_d, gint b_n, gint b_d)
+{
+  gint64 new_num_1;
+  gint64 new_num_2;
+  gint gcd;
+
+  g_return_val_if_fail (a_d != 0 && b_d != 0, 0);
+
+  /* Simplify */
+  gcd = _gcd (a_n, a_d);
+  a_n /= gcd;
+  a_d /= gcd;
+
+  gcd = _gcd (b_n, b_d);
+  b_n /= gcd;
+  b_d /= gcd;
+
+  /* fractions are reduced when set, so we can quickly see if they're equal */
+  if (a_n == b_n && a_d == b_d)
+    return 0;
+
+  /* extend to 64 bits */
+  new_num_1 = ((gint64) a_n) * b_d;
+  new_num_2 = ((gint64) b_n) * a_d;
+  if (new_num_1 < new_num_2)
+    return -1;
+  if (new_num_1 > new_num_2)
+    return 1;
+
+  /* Should not happen because a_d and b_d are not 0 */
+  g_return_val_if_reached (0);
+}
+
+/**
  * @brief Initialize the tensor info structure
  * @param info tensor info structure to be initialized
  */
@@ -672,6 +725,42 @@ gst_tensors_config_validate (const GstTensorsConfig * config)
 }
 
 /**
+ * @brief Compare tensor config info
+ * @param TRUE if equal
+ */
+gboolean
+gst_tensors_config_is_equal (const GstTensorsConfig * c1,
+    const GstTensorsConfig * c2)
+{
+  g_return_val_if_fail (c1 != NULL, FALSE);
+  g_return_val_if_fail (c2 != NULL, FALSE);
+
+  if (!gst_tensors_config_validate (c1) || !gst_tensors_config_validate (c2)) {
+    return FALSE;
+  }
+
+  if (_compare_rate (c1->rate_n, c1->rate_d, c2->rate_n, c2->rate_d)) {
+    nns_logd ("Tensors config is not equal. framerate: %d/%d vs %d/%d.",
+        c1->rate_n, c1->rate_d, c2->rate_n, c2->rate_d);
+    return FALSE;
+  }
+
+  if (c1->format != c2->format || c1->format == _NNS_TENSOR_FORMAT_END) {
+    nns_logd ("Tensors config is not equal. format: %s vs %s ",
+        _STR_NULL (gst_tensor_get_format_string (c1->format)),
+        _STR_NULL (gst_tensor_get_format_string (c2->format)));
+    return FALSE;
+  }
+
+  /* cannot compare tensor info when tensor is not static */
+  if (!gst_tensors_config_is_static (c1)) {
+    return TRUE;
+  }
+
+  return gst_tensors_info_is_equal (&c1->info, &c2->info);
+}
+
+/**
  * @brief Copy tensors config
  */
 void
index 4e9be60..bd078da 100644 (file)
@@ -2815,7 +2815,7 @@ gst_tensor_filter_check_hw_availability (const gchar * name, const accl_hw hw,
 
   if (!name) {
     nns_logw ("Cannot check hw availability, given framwork name is NULL.");
-
+    return FALSE;
   }
   if ((fw = nnstreamer_filter_find (name)) == NULL) {
     nns_logw ("Cannot find sub-plugin for %s.", name);