Remove memory copy in converter.
Memory allocation occurs two times on g_memdup and gst_memory_new_wrapped.
Changed to use gst_memory_share.
No memory copy is performed and the memory region is simply shared.
Signed-off-by: Gichan Jang <gichan2.jang@samsung.com>
GstMemory *in_mem, *out_mem;
GstMapInfo in_info;
guint mem_size;
- gpointer mem_data;
in_mem = gst_buffer_peek_memory (in_buf, 0);
- g_assert (gst_memory_map (in_mem, &in_info, GST_MAP_READ));
+ if (FALSE == gst_memory_map (in_mem, &in_info, GST_MAP_READ)) {
+ nns_loge ("Cannot map input memory / tensor_converter::flatbuf");
+ return NULL;
+ }
tensors = GetTensors (in_info.data);
g_assert (tensors);
*frame_size = 0;
*frames_in = 1;
-
for (guint i = 0; i < config->info.num_tensors; i++) {
+ gsize offset;
+
config->info.info[i].name = g_strdup (tensor->Get (i)->name ()->str ().c_str ());
config->info.info[i].type = (tensor_type)tensor->Get (i)->type ();
tensor_data = tensor->Get (i)->data ();
mem_size = VectorLength (tensor_data);
*frame_size += mem_size;
- mem_data = g_memdup (tensor_data->data (), mem_size);
+ offset = tensor_data->data () - in_info.data;
- out_mem = gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, mem_data,
- mem_size, 0, mem_size, mem_data, g_free);
+ out_mem = gst_memory_share (in_mem, offset, mem_size);
gst_buffer_append_memory (out_buf, out_mem);
}
GstMemory *in_mem, *out_mem;
GstMapInfo in_info;
guint mem_size;
- gpointer mem_data;
in_mem = gst_buffer_peek_memory (in_buf, 0);
+
if (gst_memory_map (in_mem, &in_info, GST_MAP_READ) == FALSE) {
ml_loge ("Cannot map input memory / tensor_converter::flexbuf.\n");
return NULL;
for (guint i = 0; i < config->info.num_tensors; i++) {
gchar * tensor_key = g_strdup_printf ("tensor_%d", i);
+ gsize offset;
flexbuffers::Vector tensor = tensors[tensor_key].AsVector ();
config->info.info[i].name = g_strdup (tensor[0].AsString ().c_str ());
config->info.info[i].type = (tensor_type) tensor[1].AsInt32 ();
mem_size = tensor_data.size ();
*frame_size += mem_size;
- /** @todo Consider removing memory copies for better performance. */
- mem_data = g_memdup (tensor_data.data (), mem_size);
+ offset = tensor_data.data () - in_info.data;
- out_mem = gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, mem_data,
- mem_size, 0, mem_size, mem_data, g_free);
+ out_mem = gst_memory_share (in_mem, offset, mem_size);
gst_buffer_append_memory (out_buf, out_mem);
g_free (tensor_key);
if (self->tensors_info.num_tensors > 1) {
GstMemory *mem;
GstMapInfo info;
- gpointer data;
gsize idx, size;
guint i;
for (i = 0; i < self->tensors_info.num_tensors; ++i) {
size = gst_tensors_info_get_size (&self->tensors_info, i);
- data = g_memdup (info.data + idx, size);
+ gst_buffer_append_memory (buffer, gst_memory_share (mem, idx, size));
idx += size;
-
- gst_buffer_append_memory (buffer,
- gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
}
gst_memory_unmap (mem, &info);