From: gichan-jang Date: Wed, 26 Feb 2020 11:30:28 +0000 (+0900) Subject: [Decoder] Add tensor decoder subplugin flatbuffers X-Git-Tag: accepted/tizen/unified/20200629.143505~10 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=58e002c4acb7e9479aeb7fb6bae7b8722bf85000;p=platform%2Fupstream%2Fnnstreamer.git [Decoder] Add tensor decoder subplugin flatbuffers Add tensor decoder subplugin for flatbuffer flatbuffers package can be found here : https://launchpad.net/~nnstreamer/+archive/ubuntu/ppa-build-test/+packages Signed-off-by: gichan-jang --- diff --git a/ext/nnstreamer/tensor_converter/tensor_converter_flatbuf.cc b/ext/nnstreamer/tensor_converter/tensor_converter_flatbuf.cc index 342b144..2883406 100644 --- a/ext/nnstreamer/tensor_converter/tensor_converter_flatbuf.cc +++ b/ext/nnstreamer/tensor_converter/tensor_converter_flatbuf.cc @@ -9,7 +9,7 @@ * @date 14 May 2020 * @brief NNStreamer tensor-converter subplugin, "flatbuffer", * which converts flatbuufers byte stream to tensors. -* @see https://github.com/nnsuite/nnstreamer +* @see https://github.com/nnstreamer/nnstreamer * @author Gichan Jang * @bug No known bugs except for NYI items * diff --git a/ext/nnstreamer/tensor_decoder/meson.build b/ext/nnstreamer/tensor_decoder/meson.build index c6ffbc1..c87b1de 100644 --- a/ext/nnstreamer/tensor_decoder/meson.build +++ b/ext/nnstreamer/tensor_decoder/meson.build @@ -131,14 +131,27 @@ if protobuf_support_is_available shared_library('nnstreamer_decoder_protobuf', nnstreamer_decoder_protobuf_sources, - dependencies: [nnstreamer_dep, glib_dep, gst_dep, protobuf_dep, thread_dep], + dependencies: [nnstreamer_dep, glib_dep, gst_dep, protobuf_dep], install: true, install_dir: decoder_subplugin_install_dir ) - static_library('nnstreamer_decoder_protobuf', - nnstreamer_decoder_protobuf_sources, - dependencies: [nnstreamer_dep, glib_dep, gst_dep, protobuf_dep, thread_dep], +endif + +# flatbuffer +if flatbuf_support_is_available + decoder_sub_flatbuf_sources = [ + 'tensordec-flatbuf.cc', + ] + + nnstreamer_decoder_flatbuf_sources = [fb_gen_src] + foreach s : decoder_sub_flatbuf_sources + nnstreamer_decoder_flatbuf_sources += join_paths(meson.current_source_dir(), s) + endforeach + + shared_library('nnstreamer_decoder_flatbuf', + nnstreamer_decoder_flatbuf_sources, + dependencies: [nnstreamer_dep, glib_dep, gst_dep, flatbuf_dep], install: true, - install_dir: nnstreamer_libdir + install_dir: decoder_subplugin_install_dir, ) endif diff --git a/ext/nnstreamer/tensor_decoder/tensordec-flatbuf.cc b/ext/nnstreamer/tensor_decoder/tensordec-flatbuf.cc new file mode 100644 index 0000000..75b2103 --- /dev/null +++ b/ext/nnstreamer/tensor_decoder/tensordec-flatbuf.cc @@ -0,0 +1,165 @@ +/* SPDX-License-Identifier: LGPL-2.1-only */ +/** + * GStreamer / NNStreamer tensor_decoder subplugin, "Flatbuffer" + * Copyright (C) 2020 Gichan Jang + */ +/** + * @file tensordec-flatbuf.cc + * @date 26 Feb 2020 + * @brief NNStreamer tensor-decoder subplugin, "flatbuffer", + * which converts tensor or tensors to flatbuffer byte stream. + * + * @see https://github.com/nnstreamer/nnstreamer + * @author Gichan Jang + * @bug No known bugs except for NYI items + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include // Generated by `flatc`. + +namespace nnstreamer { +namespace flatbuf { + +void init_fb (void) __attribute__ ((constructor)); +void fini_fb (void) __attribute__ ((destructor)); + +/** @brief tensordec-plugin's GstTensorDecoderDef callback */ +static int +fb_init (void **pdata) +{ + *pdata = NULL; + return TRUE; +} + +/** @brief tensordec-plugin's GstTensorDecoderDef callback */ +static void +fb_exit (void **pdata) +{ + return; +} + +/** @brief tensordec-plugin's GstTensorDecoderDef callback */ +static int +fb_setOption (void **pdata, int opNum, const char *param) +{ + return TRUE; +} + +/** @brief tensordec-plugin's GstTensorDecoderDef callback */ +static GstCaps * +fb_getOutCaps (void **pdata, const GstTensorsConfig * config) +{ + return gst_caps_from_string (GST_FLATBUF_TENSOR_CAP_DEFAULT); +} + +/** @brief tensordec-plugin's GstTensorDecoderDef callback */ +static GstFlowReturn +fb_decode (void **pdata, const GstTensorsConfig * config, + const GstTensorMemory * input, GstBuffer * outbuf) +{ + char *name; + Tensor_type type; + GstMapInfo out_info; + GstMemory *out_mem; + unsigned int i, num_tensors = config->info.num_tensors; + flatbuffers::uoffset_t fb_size; + flatbuffers::FlatBufferBuilder builder; + std::vector> tensor_vector; + flatbuffers::Offset> dim; + flatbuffers::Offset tensor_name; + flatbuffers::Offset> input_vector; + flatbuffers::Offset tensor; + flatbuffers::Offset tensors; + frame_rate fr = frame_rate (config->rate_n, config->rate_d); + + /* Fill the info in tensor and puth to tensor vector */ + for (i = 0; i < num_tensors; i++) { + unsigned char *tmp_buf; + + dim = builder.CreateVector (config->info.info[i].dimension, NNS_TENSOR_RANK_LIMIT); + name = config->info.info[i].name; + + if (name == NULL) + tensor_name = builder.CreateString ("Anonymous"); + else + tensor_name = builder.CreateString (name); + + type = (Tensor_type) config->info.info[i].type; + + /* Create the vector first, and fill in data later */ + /** @todo Consider to remove memcpy */ + input_vector = builder.CreateUninitializedVector (input[i].size, &tmp_buf); + memcpy (tmp_buf, input[i].data, input[i].size); + + tensor = CreateTensor (builder, tensor_name, type, dim, input_vector); + tensor_vector.push_back (tensor); + } + + tensors = CreateTensors (builder, num_tensors, &fr, builder.CreateVector (tensor_vector)); + + /* Serialize the data.*/ + builder.Finish (tensors); + fb_size = builder.GetSize (); + + g_assert (outbuf); + + if (gst_buffer_get_size (outbuf) == 0) { + out_mem = gst_allocator_alloc (NULL, fb_size, NULL); + } + else { + if (gst_buffer_get_size (outbuf) < fb_size) { + gst_buffer_set_size (outbuf, fb_size); + } + out_mem = gst_buffer_get_all_memory (outbuf); + } + + if (FALSE == gst_memory_map (out_mem, &out_info, GST_MAP_WRITE)) { + nns_loge ("Cannot map gst memory (tensor decoder flatbuf)\n"); + return GST_FLOW_ERROR; + } + + memcpy (out_info.data, builder.GetBufferPointer (), fb_size); + + gst_memory_unmap (out_mem, &out_info); + + if (gst_buffer_get_size (outbuf) == 0) + gst_buffer_append_memory (outbuf, out_mem); + + return GST_FLOW_OK; +} + +static gchar decoder_subplugin_flatbuf[] = "flatbuf"; + +/** @brief flatbuffer tensordec-plugin GstTensorDecoderDef instance */ +static GstTensorDecoderDef flatBuf = { + .modename = decoder_subplugin_flatbuf, + .init = fb_init, + .exit = fb_exit, + .setOption = fb_setOption, + .getOutCaps = fb_getOutCaps, + .decode = fb_decode +}; + +/** @brief Initialize this object for tensordec-plugin */ +void +init_fb (void) +{ + nnstreamer_decoder_probe (&flatBuf); +} + +/** @brief Destruct this object for tensordec-plugin */ +void +fini_fb (void) +{ + nnstreamer_decoder_exit (flatBuf.modename); +} + +}; /* Namespace flatbuf */ +}; /* Namespace nnstreamer */