[Tensors] Support heterogeneous types for other/tensors
authorjijoong.moon <jijoong.moon@samsung.com>
Tue, 3 Jul 2018 00:59:06 +0000 (09:59 +0900)
committer함명주/동작제어Lab(SR)/Principal Engineer/삼성전자 <myungjoo.ham@samsung.com>
Tue, 3 Jul 2018 02:47:53 +0000 (11:47 +0900)
By taking the input string for types in capability, we can supprot the
heterogeneous types for other/tensors.

- Added parse string for types
- Added string for types of tensors in capability.
- Added test for type string

Signed-off-by: jijoong.moon <jijoong.moon@samsung.com>
common/tensor_meta.c
include/tensor_common.h
include/tensor_meta.h
tests/nnstreamer_tensors/tensors_test/gsttensorscheck.c
tests/nnstreamer_tensors/tensors_test/gsttensorscheck.h
tests/nnstreamer_tensors/tensors_test/gsttesttensors.c

index 8c042a5..da7463a 100644 (file)
@@ -262,39 +262,55 @@ gst_get_num_tensors (GstBuffer * buffer)
   return meta->num_tensors;
 }
 
+/**
+ * @brief Utility function to parse string
+ * @param str input string to parse
+ * @param delim delimitor
+ * @param arr output array to store parsed string
+ * @return gint number of parsed string
+ */
+static gint
+parse_string (const gchar * str, const char *delim, char ***arr)
+{
+  char *pch, *buf;
+  int count = 0;
+  buf = strdup (str);
+  pch = strtok (buf, delim);
+  while (pch != NULL) {
+    pch = strtok (NULL, delim);
+    count++;
+  }
+  free (buf);
+
+  (*arr) = (char **) malloc (sizeof (char *) * count);
+
+  count = 0;
+  pch = NULL;
+  buf = strdup (str);
+  pch = strtok (buf, delim);
+  while (pch != NULL) {
+    (*arr)[count] = strdup (pch);
+    pch = strtok (NULL, delim);
+    count++;
+  }
+  free (buf);
+  return count;
+}
+
 GArray *
 parse_dimensions (const gchar * dim_string)
 {
   GArray *dimensions;
   gint num_tensors;
-  gchar *tempbuf;
   char **arr;
+  unsigned int i;
 
-  char *pch;
-  num_tensors = 0;
-  tempbuf = strdup (dim_string);
-  pch = strtok (tempbuf, " ,");
-  while (pch != NULL) {
-    pch = strtok (NULL, " ,");
-    num_tensors++;
-  }
+  num_tensors = parse_string (dim_string, " ,.;/", &arr);
 
   dimensions =
       g_array_sized_new (FALSE, FALSE, sizeof (tensor_dim *), num_tensors);
-  arr = (char **) malloc (sizeof (char *) * num_tensors);
-
-  num_tensors = 0;
-  pch = NULL;
-  tempbuf = strdup (dim_string);
-
-  pch = strtok (tempbuf, " ,");
-  while (pch != NULL) {
-    arr[num_tensors] = strdup (pch);
-    pch = strtok (NULL, " ,");
-    num_tensors++;
-  }
 
-  for (int i = 0; i < num_tensors; i++) {
+  for (i = 0; i < num_tensors; i++) {
     char *p = strtok (arr[i], " :");
     int c = 0;
     tensor_dim *d = g_new0 (tensor_dim, 1);
@@ -306,8 +322,31 @@ parse_dimensions (const gchar * dim_string)
     g_array_append_val (dimensions, d);
   }
 
+  for (i = 0; i < num_tensors; i++)
+    free (arr[i]);
   free (arr);
 
   return dimensions;
+}
+
+GArray *
+parse_types (const gchar * types_string)
+{
+  char **charbuf;
+  int num_type;
+  unsigned int i;
+  num_type = parse_string (types_string, " ,.:/", &charbuf);
+  GArray *types =
+      g_array_sized_new (FALSE, FALSE, sizeof (tensor_type *), num_type);
+
+  for (i = 0; i < num_type; i++) {
+    tensor_type *t = g_new0 (tensor_type, 1);
+    (*t) = get_tensor_type (charbuf[i]);
+    g_array_append_val (types, t);
+  }
 
+  for (i = 0; i < num_type; i++)
+    free (charbuf[i]);
+  free (charbuf);
+  return types;
 }
index d1be346..76c1d67 100644 (file)
@@ -81,8 +81,9 @@ G_BEGIN_DECLS
     "other/tensors, " \
     "rank = (int) [ 1, 4 ], " \
     "num_tensors = (int) [1, 65535], "\
-    "type = (string) { float32, float64, int32, uint32, int16, uint16, int8, uint8 }, " \
     "framerate = (fraction) [ 0/1, 2147483647/1 ]"
+    /* type should be one of { float32, float64, int32, uint32, int16, uint16, int8, uint8 } */
+    /* "types = (string) uint8, uint8, uint8" \ */
     /* Dimensions of Tensors for negotiation. It's comment out here,
        but when we call gst_structure_get_string, it actually is working well.*/
     /* "dimensions = (string) dim1:dim2:dim3:dim4, dim1:dim2:dim3:dim4" \ */
index d56fc93..447b167 100644 (file)
@@ -150,8 +150,20 @@ GstFlowReturn gst_remove_tensor (GstBuffer *buffer, gint nth);
  */
 gint gst_get_num_tensors (GstBuffer *buffer);
 
+/**
+ * @brief Utility function to get parse the dimension of tensors
+ * @param dim_string Input String to parse
+ * @return GArray Array which includes tensor_dim for each tensor
+ */
 GArray * parse_dimensions (const gchar* dim_string);
 
+/**
+ * @brief Utility function to get parse the type of tensors
+ * @param type_string Input String to parse
+ * @return GArray Array which includes tensor_type for each tensor
+ */
+GArray * parse_types (const gchar* type_string);
+
 G_END_DECLS
 
 #endif /* __GST_TENSOR_META_H__ */
index 3b8ede5..b3c5cdf 100644 (file)
@@ -253,6 +253,8 @@ GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
   gint dim;
   GstCaps *othercaps;
   const gchar *dim_string;
+  const gchar *types;
+  int i;
 
   GstStructure *s = gst_caps_get_structure (caps, 0);
   gst_structure_get_int (s, "rank", &filter->rank);
@@ -263,7 +265,7 @@ GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
   if (dim_string) {
     debug_print (!filter->silent, "dimension sting : %s\n", dim_string);
     filter->dimensions = parse_dimensions (dim_string);
-    for (int i = 0; i < filter->num_tensors; i++) {
+    for (i = 0; i < filter->num_tensors; i++) {
       tensor_dim *d = g_array_index (filter->dimensions, tensor_dim *, i);
       debug_print (!filter->silent, "dimensions[%d] %d %d %d %d\n", i, (*d)[0],
           (*d)[1], (*d)[2], (*d)[3]);
@@ -273,16 +275,28 @@ GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
   }
   gst_structure_get_fraction (s, "framerate", &filter->framerate_numerator,
       &filter->framerate_denominator);
-  filter->type = _NNS_UINT8;
+  types = gst_structure_get_string (s, "types");
+  if (types) {
+    debug_print (!filter->silent, "types string : %s\n", types);
+    filter->types = parse_types (types);
+    for (i = 0; i < filter->num_tensors; i++) {
+      tensor_type *t = g_array_index (filter->types, tensor_type *, i);
+      debug_print (!filter->silent, "types[%d] %s\n", i,
+          tensor_element_typename[(*t)]);
+    }
+  } else {
+    err_print ("Cannot get types for negotiation!\n");
+  }
 
   tensor_dim *d = g_array_index (filter->dimensions, tensor_dim *, 0);
+  tensor_type *t = g_array_index (filter->types, tensor_type *, 0);
   othercaps = gst_caps_new_simple ("other/tensor",
       "rank", G_TYPE_INT, filter->rank,
       "dim1", G_TYPE_INT, (*d)[0],
       "dim2", G_TYPE_INT, (*d)[1],
       "dim3", G_TYPE_INT, (*d)[2],
       "dim4", G_TYPE_INT, (*d)[3],
-      "type", G_TYPE_STRING, tensor_element_typename[filter->type],
+      "type", G_TYPE_STRING, tensor_element_typename[*t],
       "framerate", GST_TYPE_FRACTION, filter->framerate_numerator,
       filter->framerate_denominator, NULL);
   ret = gst_pad_set_caps (filter->srcpad, othercaps);
@@ -304,7 +318,7 @@ gst_tensors_check (Gsttensorscheck * filter, GstBuffer * inbuf)
   GstMapInfo info, src_info, dest_info;
   GstMemory *buffer_mem;
   tensor_dim *dim;
-  unsigned int d0, d1, d2;
+  unsigned int d0, d1, d2, i;
   gboolean ret;
 
   /* Mapping input buffer (tensors) into src_info */
@@ -325,7 +339,7 @@ gst_tensors_check (Gsttensorscheck * filter, GstBuffer * inbuf)
   /* Get number of tensors */
   num_tensors = gst_get_num_tensors (inbuf);
   debug_print (!filter->silent, "Number of Tensors : %d\n", num_tensors);
-  for (int i = 0; i < num_tensors; i++) {
+  for (i = 0; i < num_tensors; i++) {
     GstMemory *mem = gst_get_tensor (inbuf, i);
     if (!mem)
       debug_print (!filter->silent, "Cannot get memory\n");
index af07ed9..a6d1c04 100644 (file)
@@ -92,7 +92,7 @@ struct _Gsttensorscheck
   GArray *dimensions;
   gint num_tensors;
   gint rank;
-  tensor_type type;
+  GArray *types;
   gint framerate_numerator;
   gint framerate_denominator;
   gboolean passthrough;
index f13fbdf..3703382 100644 (file)
@@ -71,6 +71,7 @@
 #include <gst/gst.h>
 
 #include "gsttesttensors.h"
+#include <string.h>
 
 GST_DEBUG_CATEGORY_STATIC (gst_testtensors_debug);
 #define GST_CAT_DEFAULT gst_testtensors_debug
@@ -252,6 +253,7 @@ GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
   gint dim;
   GstCaps *othercaps;
   gboolean ret;
+  unsigned int i;
 
   GstStructure *s = gst_caps_get_structure (caps, 0);
 
@@ -269,10 +271,17 @@ GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
   filter->type = _NNS_UINT8;
   filter->rank = 3;
 
+  char str[256];
+  strcpy (str, tensor_element_typename[filter->type]);
+  for (i = 1; i < filter->num_tensors; i++) {
+    strcat (str, ",");
+    strcat (str, tensor_element_typename[filter->type]);
+  }
+
   othercaps = gst_caps_new_simple ("other/tensors",
       "rank", G_TYPE_INT, filter->rank,
       "num_tensors", G_TYPE_INT, filter->num_tensors,
-      "type", G_TYPE_STRING, tensor_element_typename[filter->type],
+      "types", G_TYPE_STRING, str,
       "framerate", GST_TYPE_FRACTION, filter->framerate_numerator,
       filter->framerate_denominator, "dimensions", G_TYPE_STRING,
       "1:640:480:1 ,1:640:480:3 ,1:640:480:1", NULL);