Doxygen Book
tensor_filter_single.c
Go to the documentation of this file.
1 
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42 
43 #include <glib.h>
44 #include <string.h>
45 
46 #include "nnstreamer_log.h"
47 #include "tensor_filter_common.h"
48 #include "tensor_filter_single.h"
49 
54 {
56  gboolean allocate_in_invoke;
58 
59 #define G_TENSOR_FILTER_SINGLE_PRIV(obj) ((GTensorFilterSinglePrivate *) (obj)->priv)
60 
61 #define g_tensor_filter_single_parent_class parent_class
62 G_DEFINE_TYPE_WITH_PRIVATE (GTensorFilterSingle, g_tensor_filter_single,
63  G_TYPE_OBJECT);
64 
65 /* GObject vmethod implementations */
66 static void g_tensor_filter_single_finalize (GObject * object);
67 static void g_tensor_filter_single_set_property (GObject * object,
68  guint prop_id, const GValue * value, GParamSpec * pspec);
69 static void g_tensor_filter_single_get_property (GObject * object,
70  guint prop_id, GValue * value, GParamSpec * pspec);
71 
72 /* GTensorFilterSingle method implementations */
74  const GstTensorMemory * input, GstTensorMemory * output, gboolean allocate);
78  const GstTensorsInfo * in_info, GstTensorsInfo * out_info);
80  GstTensorMemory * mem);
82 static gboolean g_tensor_filter_single_start (GTensorFilterSingle * self);
83 static gboolean g_tensor_filter_single_stop (GTensorFilterSingle * self);
84 
88 static void
90 {
91  GObjectClass *gobject_class;
92 
93  gobject_class = (GObjectClass *) klass;
94 
95  gobject_class->set_property = g_tensor_filter_single_set_property;
96  gobject_class->get_property = g_tensor_filter_single_get_property;
97  gobject_class->finalize = g_tensor_filter_single_finalize;
98 
100 
109 }
110 
114 static void
116 {
119 
120  self->priv = g_type_instance_get_private ((GTypeInstance *) self,
122  spriv = G_TENSOR_FILTER_SINGLE_PRIV (self);
123  priv = &spriv->filter_priv;
124 
126  spriv->allocate_in_invoke = FALSE;
127 }
128 
132 static void
134 {
135  GTensorFilterSingle *self;
138 
139  self = G_TENSOR_FILTER_SINGLE (object);
140  spriv = G_TENSOR_FILTER_SINGLE_PRIV (self);
141  priv = &spriv->filter_priv;
142 
144  if (priv->configured) {
146  }
147 
149 
150  G_OBJECT_CLASS (parent_class)->finalize (object);
151 }
152 
156 static void
157 g_tensor_filter_single_set_property (GObject * object, guint prop_id,
158  const GValue * value, GParamSpec * pspec)
159 {
160  GTensorFilterSingle *self;
163 
164  self = G_TENSOR_FILTER_SINGLE (object);
165  spriv = G_TENSOR_FILTER_SINGLE_PRIV (self);
166  priv = &spriv->filter_priv;
167 
168  ml_logd ("Setting property for prop %d.\n", prop_id);
169 
170  if (!gst_tensor_filter_common_set_property (priv, prop_id, value, pspec))
171  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172 }
173 
177 static void
178 g_tensor_filter_single_get_property (GObject * object, guint prop_id,
179  GValue * value, GParamSpec * pspec)
180 {
181  GTensorFilterSingle *self;
184 
185  self = G_TENSOR_FILTER_SINGLE (object);
186  spriv = G_TENSOR_FILTER_SINGLE_PRIV (self);
187  priv = &spriv->filter_priv;
188 
189  ml_logd ("Getting property for prop %d.\n", prop_id);
190 
191  if (!gst_tensor_filter_common_get_property (priv, prop_id, value, pspec))
192  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193 }
194 
199 static gboolean
201 {
204 
205  spriv = G_TENSOR_FILTER_SINGLE_PRIV (self);
206  priv = &spriv->filter_priv;
207 
208  return priv->prop.input_configured;
209 }
210 
215 static gboolean
217 {
220 
221  spriv = G_TENSOR_FILTER_SINGLE_PRIV (self);
222  priv = &spriv->filter_priv;
223 
224  return priv->prop.output_configured;
225 }
226 
230 static inline gboolean
232 {
234 
235  spriv = G_TENSOR_FILTER_SINGLE_PRIV (self);
236 
237  return spriv->allocate_in_invoke;
238 }
239 
245 static gboolean
247 {
250 
251  spriv = G_TENSOR_FILTER_SINGLE_PRIV (self);
252  priv = &spriv->filter_priv;
253 
255  if (G_UNLIKELY (priv->fw == NULL))
256  return FALSE;
257 
259 
260  if (G_UNLIKELY (!priv->prop.fw_opened))
261  return FALSE;
262 
265 
266  priv->configured = TRUE;
267 
268  return TRUE;
269 }
270 
276 static gboolean
278 {
281 
282  spriv = G_TENSOR_FILTER_SINGLE_PRIV (self);
283  priv = &spriv->filter_priv;
284 
287  return TRUE;
288 }
289 
295 static void
297  GstTensorMemory * mem)
298 {
299  guint i;
302 
303  spriv = G_TENSOR_FILTER_SINGLE_PRIV (self);
304  priv = &spriv->filter_priv;
305 
306  for (i = 0; i < priv->prop.output_meta.num_tensors; i++) {
308  mem[i].data = NULL;
309  }
310 }
311 
320 static gboolean
322  const GstTensorMemory * input, GstTensorMemory * output, gboolean allocate)
323 {
326  GstTensorMemory *_out;
327  GstTensorMemory out_tensors[NNS_TENSOR_SIZE_LIMIT] = { {0} };
328  guint i;
329  gint status;
330 
331  spriv = G_TENSOR_FILTER_SINGLE_PRIV (self);
332  priv = &spriv->filter_priv;
333 
335  if (!priv->configured) {
336  if (!g_tensor_filter_single_start (self)) {
337  return FALSE;
338  }
339  }
340 
341  /* set output tensors for given params */
342  _out = output;
343 
344  if (spriv->allocate_in_invoke) {
345  if (!allocate) {
350  _out = out_tensors;
351 
352  for (i = 0; i < priv->prop.output_meta.num_tensors; i++)
353  out_tensors[i].size = output[i].size;
354  }
355  } else {
356  /* allocate memory if allocate_in_invoke is FALSE */
357  if (allocate) {
358  for (i = 0; i < priv->prop.output_meta.num_tensors; i++) {
359  output[i].data = g_try_malloc (output[i].size);
360  if (!output[i].data) {
361  ml_loge ("Failed to allocate the output tensor.");
362  goto error;
363  }
364  }
365  }
366  }
367 
368  GST_TF_FW_INVOKE_COMPAT (priv, status, input, _out);
369 
370  if (status == 0) {
371  if (_out != output) {
372  for (i = 0; i < priv->prop.output_meta.num_tensors; i++)
373  memcpy (output[i].data, _out[i].data, output[i].size);
374 
375  g_tensor_filter_destroy_notify (self, _out);
376  }
377 
378  return TRUE;
379  }
380 
381 error:
382  /* if failed to invoke the model, release allocated memory. */
383  if (!spriv->allocate_in_invoke && allocate) {
384  for (i = 0; i < priv->prop.output_meta.num_tensors; i++) {
385  g_free (output[i].data);
386  output[i].data = NULL;
387  }
388  }
389  return FALSE;
390 }
391 
399 static gint
401  const GstTensorsInfo * in_info, GstTensorsInfo * out_info)
402 {
405  gint status = -EINVAL;
406 
407  spriv = G_TENSOR_FILTER_SINGLE_PRIV (self);
408  priv = &spriv->filter_priv;
409 
410  if (G_UNLIKELY (!priv->fw) || G_UNLIKELY (!priv->prop.fw_opened))
411  return -EINVAL;
412 
413  gst_tensors_info_init (out_info);
414  if (GST_TF_FW_V0 (priv->fw)) {
415  if (G_LIKELY (priv->fw->setInputDimension)) {
416  status = priv->fw->setInputDimension (&priv->prop, &priv->privateData,
417  in_info, out_info);
418  } else {
419  status = -ENOENT;
420  }
421  } else {
422  status = priv->fw->getModelInfo (priv->fw, &priv->prop, &priv->privateData,
423  SET_INPUT_INFO, (GstTensorsInfo *) in_info, out_info);
424  }
425 
426  if (status == 0) {
429 
430  gst_tensors_info_copy (&priv->prop.input_meta, in_info);
431  gst_tensors_info_copy (&priv->prop.output_meta, out_info);
432  }
433 
434  return status;
435 }
data
svtc_1 data
Definition: gsttensor_if.c:844
gst_tensor_filter_common_open_fw
void gst_tensor_filter_common_open_fw(GstTensorFilterPrivate *priv)
Open NN framework.
Definition: tensor_filter_common.c:2491
NNS_TENSOR_SIZE_LIMIT
#define NNS_TENSOR_SIZE_LIMIT
The number of tensors NNStreamer supports is 256. The max memories of gst-buffer is 16 (See NNS_TENSO...
Definition: tensor_typedef.h:42
FALSE
return FALSE
Definition: gsttensor_transform.c:590
GstTensorMemory::data
void * data
Definition: tensor_typedef.h:254
gst_tensor_filter_common_set_property
gboolean gst_tensor_filter_common_set_property(GstTensorFilterPrivate *priv, guint prop_id, const GValue *value, GParamSpec *pspec)
Set the properties for tensor_filter.
Definition: tensor_filter_common.c:1967
GST_TF_FW_V0
#define GST_TF_FW_V0(fw)
Definition: tensor_filter_common.h:47
GstTensorsInfo
Internal meta data exchange format for a other/tensors instance.
Definition: tensor_typedef.h:273
GST_TF_FW_INVOKE_COMPAT
#define GST_TF_FW_INVOKE_COMPAT(priv, ret, in, out)
Definition: tensor_filter_common.h:69
_GstTensorFilterProperties::fw_opened
int fw_opened
Definition: nnstreamer_plugin_api_filter.h:115
g_tensor_filter_single_invoke
static gboolean g_tensor_filter_single_invoke(GTensorFilterSingle *self, const GstTensorMemory *input, GstTensorMemory *output, gboolean allocate)
Called when an input supposed to be invoked.
Definition: tensor_filter_single.c:321
_GstTensorFilterProperties::output_configured
int output_configured
Definition: nnstreamer_plugin_api_filter.h:124
nnstreamer_log.h
Internal log util for NNStreamer plugins and native APIs.
g_tensor_filter_single_class_init
static void g_tensor_filter_single_class_init(GTensorFilterSingleClass *klass)
initialize the tensor_filter's class
Definition: tensor_filter_single.c:89
gst_tensors_info_init
void gst_tensors_info_init(GstTensorsInfo *info)
Initialize the tensors info structure.
Definition: nnstreamer_plugin_api_util_impl.c:325
GstTensorMemory
The unit of each data tensors. It will be used as an input/output tensor of other/tensors.
Definition: tensor_typedef.h:252
gst_tensors_info_free
void gst_tensors_info_free(GstTensorsInfo *info)
Free allocated data in tensors info structure.
Definition: nnstreamer_plugin_api_util_impl.c:347
g_tensor_filter_single_start
static gboolean g_tensor_filter_single_start(GTensorFilterSingle *self)
Called when the element starts processing, if fw not loaded.
Definition: tensor_filter_single.c:246
g_tensor_filter_single_get_property
static void g_tensor_filter_single_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
Getter for tensor_filter_single properties.
Definition: tensor_filter_single.c:178
_GTensorFilterSingleClass::set_input_info
gint(* set_input_info)(GTensorFilterSingle *self, const GstTensorsInfo *in_info, GstTensorsInfo *out_info)
Definition: tensor_filter_single.h:78
g_tensor_filter_allocate_in_invoke
static gboolean g_tensor_filter_allocate_in_invoke(GTensorFilterSingle *self)
Determine if this filter framework supports allocation in invoke.
Definition: tensor_filter_single.c:231
g_free
g_free(self->option[(opnum) - 1])
opnum: \
gst_tensor_filter_common_free_property
void gst_tensor_filter_common_free_property(GstTensorFilterPrivate *priv)
Free the properties for tensor-filter.
Definition: tensor_filter_common.c:1065
GTensorFilterSinglePrivate
struct _GTensorFilterSinglePrivate GTensorFilterSinglePrivate
Private data struct for tensor-filter single class.
g_tensor_filter_single_init
static void g_tensor_filter_single_init(GTensorFilterSingle *self)
initialize the new element
Definition: tensor_filter_single.c:115
G_DEFINE_TYPE_WITH_PRIVATE
G_DEFINE_TYPE_WITH_PRIVATE(GTensorFilterSingle, g_tensor_filter_single, G_TYPE_OBJECT)
_GTensorFilterSingleClass::destroy_notify
void(* destroy_notify)(GTensorFilterSingle *self, GstTensorMemory *mem)
Definition: tensor_filter_single.h:83
_GTensorFilterSingleClass::start
gboolean(* start)(GTensorFilterSingle *self)
Definition: tensor_filter_single.h:70
g_tensor_filter_single_set_property
static void g_tensor_filter_single_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
Setter for tensor_filter_single properties.
Definition: tensor_filter_single.c:157
_GTensorFilterSingleClass::output_configured
gboolean(* output_configured)(GTensorFilterSingle *self)
Definition: tensor_filter_single.h:76
_GTensorFilterSingle
Internal data structure for tensor_filter_single instances.
Definition: tensor_filter_single.h:51
gst_tensor_filter_destroy_notify_util
void gst_tensor_filter_destroy_notify_util(GstTensorFilterPrivate *priv, void *data)
Free the data allocated for tensor filter output.
Definition: tensor_filter_common.c:786
gst_tensor_filter_install_properties
void gst_tensor_filter_install_properties(GObjectClass *gobject_class)
Installs all the properties for tensor_filter.
Definition: tensor_filter_common.c:888
ml_loge
#define ml_loge
Definition: nnstreamer_log.h:78
TRUE
return TRUE
Definition: gsttensor_if.c:897
_GstTensorFilterPrivate
Structure definition for common tensor-filter properties.
Definition: tensor_filter_common.h:152
_GstTensorFilterProperties::input_configured
int input_configured
Definition: nnstreamer_plugin_api_filter.h:119
G_TENSOR_FILTER_SINGLE
#define G_TENSOR_FILTER_SINGLE(obj)
Definition: tensor_filter_single.h:35
_GTensorFilterSinglePrivate
Private data struct for tensor-filter single class.
Definition: tensor_filter_single.c:53
tensor_filter_single.h
Element to use general neural network framework individually without gstreamer pipeline.
_GstTensorFilterPrivate::fw
const GstTensorFilterFramework * fw
Definition: tensor_filter_common.h:158
_GTensorFilterSingleClass::input_configured
gboolean(* input_configured)(GTensorFilterSingle *self)
Definition: tensor_filter_single.h:74
_GstTensorFilterProperties::output_meta
GstTensorsInfo output_meta
Definition: nnstreamer_plugin_api_filter.h:125
g_tensor_filter_single_stop
static gboolean g_tensor_filter_single_stop(GTensorFilterSingle *self)
Called when the element stops processing, if fw loaded.
Definition: tensor_filter_single.c:277
_GTensorFilterSingleClass::stop
gboolean(* stop)(GTensorFilterSingle *self)
Definition: tensor_filter_single.h:72
g_tensor_filter_output_configured
static gboolean g_tensor_filter_output_configured(GTensorFilterSingle *self)
Determine if output is configured (both input and output tensor)
Definition: tensor_filter_single.c:216
_GstTensorFilterProperties::input_meta
GstTensorsInfo input_meta
Definition: nnstreamer_plugin_api_filter.h:120
_GstTensorFilterFramework::setInputDimension
int(* setInputDimension)(const GstTensorFilterProperties *prop, void **private_data, const GstTensorsInfo *in_info, GstTensorsInfo *out_info)
Definition: nnstreamer_plugin_api_filter.h:327
_GstTensorFilterFramework::getModelInfo
int(* getModelInfo)(const GstTensorFilterFramework *self, const GstTensorFilterProperties *prop, void *private_data, model_info_ops ops, GstTensorsInfo *in_info, GstTensorsInfo *out_info)
Definition: nnstreamer_plugin_api_filter.h:420
_GstTensorFilterPrivate::configured
gboolean configured
Definition: tensor_filter_common.h:162
g_tensor_filter_set_input_info
static gint g_tensor_filter_set_input_info(GTensorFilterSingle *self, const GstTensorsInfo *in_info, GstTensorsInfo *out_info)
Set input tensor information in the framework.
Definition: tensor_filter_single.c:400
g_tensor_filter_single_finalize
static void g_tensor_filter_single_finalize(GObject *object)
Function to finalize instance.
Definition: tensor_filter_single.c:133
_GTensorFilterSinglePrivate::filter_priv
GstTensorFilterPrivate filter_priv
Definition: tensor_filter_single.c:55
G_TYPE_TENSOR_FILTER_SINGLE
#define G_TYPE_TENSOR_FILTER_SINGLE
Definition: tensor_filter_single.h:33
gst_tensor_filter_common_get_property
gboolean gst_tensor_filter_common_get_property(GstTensorFilterPrivate *priv, guint prop_id, GValue *value, GParamSpec *pspec)
Get the properties for tensor_filter.
Definition: tensor_filter_common.c:2102
gst_tensor_filter_load_tensor_info
void gst_tensor_filter_load_tensor_info(GstTensorFilterPrivate *priv)
Load tensor info from NN model. (both input and output tensor)
Definition: tensor_filter_common.c:2409
_GTensorFilterSingleClass::invoke
gboolean(* invoke)(GTensorFilterSingle *self, const GstTensorMemory *input, GstTensorMemory *output, gboolean allocate)
Definition: tensor_filter_single.h:67
SET_INPUT_INFO
@ SET_INPUT_INFO
Definition: nnstreamer_plugin_api_filter.h:194
gst_tensor_filter_allocate_in_invoke
gboolean gst_tensor_filter_allocate_in_invoke(GstTensorFilterPrivate *priv)
check if the allocate_in_invoke is valid for the framework
Definition: tensor_filter_common.c:757
GstTensorsInfo::num_tensors
unsigned int num_tensors
Definition: tensor_typedef.h:275
G_TENSOR_FILTER_SINGLE_PRIV
#define G_TENSOR_FILTER_SINGLE_PRIV(obj)
Definition: tensor_filter_single.c:59
tensor_filter_common.h
Common functions for various tensor_filters.
ml_logd
#define ml_logd
Definition: nnstreamer_log.h:79
gst_tensor_filter_common_init_property
void gst_tensor_filter_common_init_property(GstTensorFilterPrivate *priv)
Initialize the properties for tensor-filter.
Definition: tensor_filter_common.c:1041
_GstTensorFilterPrivate::prop
GstTensorFilterProperties prop
Definition: tensor_filter_common.h:155
g_tensor_filter_destroy_notify
static void g_tensor_filter_destroy_notify(GTensorFilterSingle *self, GstTensorMemory *mem)
Called to notify the framework to destroy the allocated memory.
Definition: tensor_filter_single.c:296
gst_tensors_info_copy
void gst_tensors_info_copy(GstTensorsInfo *dest, const GstTensorsInfo *src)
Copy tensor info.
Definition: nnstreamer_plugin_api_util_impl.c:502
gst_tensor_filter_common_close_fw
void gst_tensor_filter_common_close_fw(GstTensorFilterPrivate *priv)
Close NN framework.
Definition: tensor_filter_common.c:2560
_GTensorFilterSingleClass::allocate_in_invoke
gboolean(* allocate_in_invoke)(GTensorFilterSingle *self)
Definition: tensor_filter_single.h:81
_GTensorFilterSingleClass
GTensorFilterSingleClass inherits GObjectClass.
Definition: tensor_filter_single.h:62
_GstTensorFilterPrivate::privateData
void * privateData
Definition: tensor_filter_common.h:154
_GTensorFilterSinglePrivate::allocate_in_invoke
gboolean allocate_in_invoke
Definition: tensor_filter_single.c:56
g_tensor_filter_input_configured
static gboolean g_tensor_filter_input_configured(GTensorFilterSingle *self)
Determine if input is configured (both input and output tensor)
Definition: tensor_filter_single.c:200