Doxygen Book
gsttensor_repo.c
Go to the documentation of this file.
1 
26 #include "gsttensor_repo.h"
27 
28 #ifndef DBG
29 #define DBG FALSE
30 #endif
31 
35 static GstTensorRepo _repo = {.num_data = 0,.initialized = FALSE };
36 
40 #define GST_REPO_LOCK() (g_mutex_lock(&_repo.repo_lock))
41 #define GST_REPO_UNLOCK() (g_mutex_unlock(&_repo.repo_lock))
42 #define GST_REPO_WAIT() (g_cond_wait(&_repo.repo_cond, &_repo.repo_lock))
43 #define GST_REPO_BROADCAST() (g_cond_broadcast (&_repo.repo_cond))
44 
48 static void
50 {
52  g_return_if_fail (_data != NULL);
53 
54  g_mutex_lock (&_data->lock);
55  g_cond_signal (&_data->cond_pull);
56  g_cond_signal (&_data->cond_push);
57  if (_data->buffer)
58  gst_buffer_unref (_data->buffer);
59  if (_data->caps)
60  gst_caps_unref (_data->caps);
61  g_mutex_unlock (&_data->lock);
62 
63  g_mutex_clear (&_data->lock);
64  g_cond_clear (&_data->cond_pull);
65  g_cond_clear (&_data->cond_push);
66 
67  g_free (_data);
68 }
69 
75 {
76  gpointer p;
77 
78  g_return_val_if_fail (_repo.initialized, NULL);
79 
80  GST_REPO_LOCK ();
81  p = g_hash_table_lookup (_repo.hash, GINT_TO_POINTER (nth));
82  GST_REPO_UNLOCK ();
83 
84  return (GstTensorRepoData *) p;
85 }
86 
90 gboolean
91 gst_tensor_repo_set_changed (guint o_nth, guint nth, gboolean is_sink)
92 {
94 
96 
97  if (data) {
98  g_mutex_lock (&data->lock);
99 
100  if (is_sink) {
101  data->sink_changed = TRUE;
102  data->sink_id = nth;
103  if (DBG)
104  GST_DEBUG ("SET sink_changed! @id %d \n", o_nth);
105 
106  /* signal pull */
107  g_cond_signal (&data->cond_pull);
108  } else {
109  data->src_changed = TRUE;
110  data->src_id = nth;
111  if (DBG)
112  GST_DEBUG ("SET src_changed! @id %d\n", o_nth);
113 
114  /* signal push */
115  g_cond_signal (&data->cond_push);
116  }
117 
118  g_mutex_unlock (&data->lock);
119  return TRUE;
120  }
121 
122  return FALSE;
123 }
124 
128 gboolean
129 gst_tensor_repo_add_repodata (guint nth, gboolean is_sink)
130 {
131  gboolean ret = FALSE;
133 
135 
136  if (data != NULL) {
137  g_mutex_lock (&data->lock);
138 
139  if (is_sink)
140  data->sink_changed = FALSE;
141  else
142  data->src_changed = FALSE;
143 
144  data->pushed = FALSE;
145 
146  g_mutex_unlock (&data->lock);
147 
148  if (DBG)
149  GST_DEBUG ("SET SINK & SRC Changed FALSE!! @%d\n", nth);
150  return TRUE;
151  }
152 
153  data = g_new0 (GstTensorRepoData, 1);
154  if (data == NULL) {
155  GST_ERROR ("Failed to allocate memory for repo data.");
156  return FALSE;
157  }
158 
159  g_cond_init (&data->cond_push);
160  g_cond_init (&data->cond_pull);
161  g_mutex_init (&data->lock);
162 
163  g_mutex_lock (&data->lock);
164  data->eos = FALSE;
165  data->buffer = NULL;
166  data->caps = NULL;
167  data->sink_changed = FALSE;
168  data->src_changed = FALSE;
169  data->pushed = FALSE;
170  g_mutex_unlock (&data->lock);
171 
172  GST_REPO_LOCK ();
173  ret = g_hash_table_insert (_repo.hash, GINT_TO_POINTER (nth), data);
174 
175  if (ret) {
176  _repo.num_data++;
177 
178  if (DBG)
179  GST_DEBUG ("Successfully added in hash table with key[%d]", nth);
180  } else {
182  ml_logf ("The key[%d] is duplicated. Cannot proceed.\n", nth);
183  }
184 
185  GST_REPO_UNLOCK ();
186  return ret;
187 }
188 
192 gboolean
193 gst_tensor_repo_set_buffer (guint nth, GstBuffer * buffer, GstCaps * caps)
194 {
196 
198 
199  g_return_val_if_fail (data != NULL, FALSE);
200 
201  g_mutex_lock (&data->lock);
202 
203  while (data->buffer != NULL && !data->eos) {
204  /* wait pull */
205  g_cond_wait (&data->cond_pull, &data->lock);
206  }
207 
208  if (data->eos) {
209  g_mutex_unlock (&data->lock);
210  return FALSE;
211  }
212 
213  data->buffer = gst_buffer_copy_deep (buffer);
214  if (!data->caps || !gst_caps_is_equal (data->caps, caps)) {
215  if (data->caps)
216  gst_caps_unref (data->caps);
217  data->caps = gst_caps_copy (caps);
218  }
219 
220  if (DBG) {
221  unsigned long size = gst_buffer_get_size (data->buffer);
222  GST_DEBUG ("Pushed [%d] (size : %lu)\n", nth, size);
223  }
224 
225  /* signal push */
226  g_cond_signal (&data->cond_push);
227 
228  g_mutex_unlock (&data->lock);
229  return TRUE;
230 }
231 
235 gboolean
237 {
239 
241 
242  if (data) {
243  if (DBG)
244  GST_DEBUG ("check eos done [%s]\n", data->eos ? "TRUE" : "FALSE");
245  return data->eos;
246  }
247 
248  return FALSE;
249 }
250 
254 gboolean
255 gst_tensor_repo_check_changed (guint nth, guint * newid, gboolean is_sink)
256 {
257  gboolean ret = FALSE;
259 
261 
262  g_return_val_if_fail (data != NULL, FALSE);
263 
264  if (DBG)
265  GST_DEBUG ("%dth RepoData : sink_changed %d, src_changed %d\n", nth,
266  data->sink_changed, data->src_changed);
267 
268  if (is_sink) {
269  if (data->sink_changed) {
270  *newid = data->sink_id;
271  ret = TRUE;
272  }
273  } else {
274  if (data->src_changed) {
275  *newid = data->src_id;
276  ret = TRUE;
277  }
278  }
279 
280  return ret;
281 }
282 
286 gboolean
288 {
290 
292 
293  g_return_val_if_fail (data != NULL, FALSE);
294 
295  g_mutex_lock (&data->lock);
296 
297  data->eos = TRUE;
298  g_cond_signal (&data->cond_push);
299  g_cond_signal (&data->cond_pull);
300 
301  g_mutex_unlock (&data->lock);
302  return TRUE;
303 }
304 
308 GstBuffer *
309 gst_tensor_repo_get_buffer (guint nth, gboolean * eos, guint * newid,
310  GstCaps ** caps)
311 {
313  GstBuffer *buf = NULL;
314 
316 
317  g_return_val_if_fail (data != NULL, NULL);
318 
319  g_mutex_lock (&data->lock);
320 
321  while (!data->buffer) {
322  if (gst_tensor_repo_check_changed (nth, newid, FALSE)) {
323  buf = NULL;
324  goto done;
325  }
326 
327  if (gst_tensor_repo_check_eos (nth)) {
328  *eos = TRUE;
329  buf = NULL;
330  goto done;
331  }
332 
333  /* wait push */
334  g_cond_wait (&data->cond_push, &data->lock);
335  }
336 
337  /* Current buffer will be wasted. */
338  buf = data->buffer;
339  *caps = gst_caps_ref (data->caps);
340  if (DBG) {
341  unsigned long size = gst_buffer_get_size (buf);
342  GST_DEBUG ("Popped [ %d ] (size: %lu)\n", nth, size);
343  }
344 
345 done:
346  data->buffer = NULL;
347  /* signal pull */
348  g_cond_signal (&data->cond_pull);
349  g_mutex_unlock (&data->lock);
350  return buf;
351 }
352 
356 gboolean
358 {
359  gboolean ret = FALSE;
361 
362  g_return_val_if_fail (_repo.initialized, FALSE);
363 
365 
366  if (data) {
367  GST_REPO_LOCK ();
368  ret = g_hash_table_remove (_repo.hash, GINT_TO_POINTER (nth));
369 
370  if (ret) {
371  _repo.num_data--;
372  if (DBG)
373  GST_DEBUG ("key[%d] is removed\n", nth);
374  }
375 
376  GST_REPO_UNLOCK ();
377  }
378 
379  return ret;
380 }
381 
385 void
387 {
388  if (_repo.initialized)
389  return;
390 
391  g_mutex_init (&_repo.repo_lock);
392  g_cond_init (&_repo.repo_cond);
393  GST_REPO_LOCK ();
394  _repo.num_data = 0;
395  _repo.hash = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
399  GST_REPO_UNLOCK ();
400 }
401 
405 gboolean
407 {
408  GST_REPO_LOCK ();
409  while (!_repo.initialized)
410  GST_REPO_WAIT ();
411  GST_REPO_UNLOCK ();
412  return TRUE;
413 }
data
svtc_1 data
Definition: gsttensor_if.c:844
FALSE
return FALSE
Definition: gsttensor_transform.c:590
ml_logf
#define ml_logf
Definition: nnstreamer_log.h:80
gst_tensor_repo_set_changed
gboolean gst_tensor_repo_set_changed(guint o_nth, guint nth, gboolean is_sink)
Set the changing status of repo.
Definition: gsttensor_repo.c:91
GST_REPO_LOCK
#define GST_REPO_LOCK()
Macro for Lock & Cond.
Definition: gsttensor_repo.c:40
GstTensorRepo::repo_lock
GMutex repo_lock
Definition: gsttensor_repo.h:61
GstTensorRepoData::caps
GstCaps * caps
Definition: gsttensor_repo.h:43
GstTensorRepoData::cond_pull
GCond cond_pull
Definition: gsttensor_repo.h:45
GstTensorRepo
GstTensorRepo data structure.
Definition: gsttensor_repo.h:58
DBG
#define DBG
Definition: gsttensor_repo.c:29
gst_tensor_repo_remove_repodata
gboolean gst_tensor_repo_remove_repodata(guint nth)
Remove nth GstTensorRepoData from GstTensorRepo.
Definition: gsttensor_repo.c:357
g_free
g_free(self->option[(opnum) - 1])
opnum: \
gst_tensor_repo_wait
gboolean gst_tensor_repo_wait(void)
Wait for finish of initialization.
Definition: gsttensor_repo.c:406
GST_REPO_UNLOCK
#define GST_REPO_UNLOCK()
Definition: gsttensor_repo.c:41
gst_tensor_repo_set_eos
gboolean gst_tensor_repo_set_eos(guint nth)
Set EOS (End-of-Stream) of slot.
Definition: gsttensor_repo.c:287
GstTensorRepo::num_data
guint num_data
Definition: gsttensor_repo.h:60
TRUE
return TRUE
Definition: gsttensor_if.c:897
gst_tensor_repo_release_repodata
static void gst_tensor_repo_release_repodata(gpointer data)
Internal function to release repo data.
Definition: gsttensor_repo.c:49
GstTensorRepoData
GstTensorRepo internal data structure.
Definition: gsttensor_repo.h:40
GST_REPO_BROADCAST
#define GST_REPO_BROADCAST()
Definition: gsttensor_repo.c:43
gst_tensor_repo_get_repodata
GstTensorRepoData * gst_tensor_repo_get_repodata(guint nth)
Getter to get nth GstTensorRepoData.
Definition: gsttensor_repo.c:74
GstTensorRepo::hash
GHashTable * hash
Definition: gsttensor_repo.h:63
_repo
static GstTensorRepo _repo
tensor repo global variable with init.
Definition: gsttensor_repo.c:35
gsttensor_repo.h
tensor repo header file for NNStreamer, the GStreamer plugin for neural networks
GST_REPO_WAIT
#define GST_REPO_WAIT()
Definition: gsttensor_repo.c:42
GstTensorRepo::initialized
gboolean initialized
Definition: gsttensor_repo.h:64
gst_tensor_repo_set_buffer
gboolean gst_tensor_repo_set_buffer(guint nth, GstBuffer *buffer, GstCaps *caps)
Push GstBuffer into repo.
Definition: gsttensor_repo.c:193
GstTensorRepoData::buffer
GstBuffer * buffer
Definition: gsttensor_repo.h:42
gst_tensor_repo_check_changed
gboolean gst_tensor_repo_check_changed(guint nth, guint *newid, gboolean is_sink)
Check repo data is changed.
Definition: gsttensor_repo.c:255
GstTensorRepoData::lock
GMutex lock
Definition: gsttensor_repo.h:46
gst_tensor_repo_check_eos
gboolean gst_tensor_repo_check_eos(guint nth)
Check EOS (End-of-Stream) of slot.
Definition: gsttensor_repo.c:236
GstTensorRepoData::cond_push
GCond cond_push
Definition: gsttensor_repo.h:44
gst_tensor_repo_add_repodata
gboolean gst_tensor_repo_add_repodata(guint nth, gboolean is_sink)
Add GstTensorRepoData into repo.
Definition: gsttensor_repo.c:129
GstTensorRepo::repo_cond
GCond repo_cond
Definition: gsttensor_repo.h:62
gst_tensor_repo_get_buffer
GstBuffer * gst_tensor_repo_get_buffer(guint nth, gboolean *eos, guint *newid, GstCaps **caps)
Get GstTensorRepoData from repo.
Definition: gsttensor_repo.c:309
GST_ERROR
GST_ERROR("Failed to register nnstreamer plugin : tensor_" # name)
type)) { \
gst_tensor_repo_init
void gst_tensor_repo_init(void)
GstTensorRepo initialization.
Definition: gsttensor_repo.c:386