Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1-only */
2 : /**
3 : * GStreamer/NNStreamer Tensor-Decoder
4 : * Copyright (C) 2021 Gichan Jang <gichan2.jang@samsung.com>
5 : */
6 : /**
7 : * @file tensordec-octetstream.c
8 : * @date 04 Nov 2021
9 : * @brief NNStreamer tensor-decoder subplugin, "octet stream",
10 : * which converts tensors to octet stream.
11 : * @see https://github.com/nnstreamer/nnstreamer
12 : * @author Gichan Jang <gichan2.jang@samsung.com>
13 : * @bug No known bugs except for NYI items
14 : *
15 : */
16 :
17 : #include <string.h>
18 : #include <glib.h>
19 : #include <nnstreamer_plugin_api_decoder.h>
20 : #include <nnstreamer_plugin_api.h>
21 : #include <nnstreamer_log.h>
22 : #include <nnstreamer_util.h>
23 : #include "tensordecutil.h"
24 :
25 : void init_os (void) __attribute__ ((constructor));
26 : void fini_os (void) __attribute__ ((destructor));
27 :
28 : #define OCTET_CAPS_STR "application/octet-stream"
29 :
30 : /** @brief tensordec-plugin's GstTensorDecoderDef callback */
31 : static int
32 4 : os_init (void **pdata)
33 : {
34 4 : *pdata = NULL;
35 4 : return TRUE;
36 : }
37 :
38 : /** @brief tensordec-plugin's GstTensorDecoderDef callback */
39 : static void
40 4 : os_exit (void **pdata)
41 : {
42 : UNUSED (pdata);
43 4 : return;
44 : }
45 :
46 : /** @brief tensordec-plugin's GstTensorDecoderDef callback */
47 : static int
48 0 : os_setOption (void **pdata, int opNum, const char *param)
49 : {
50 : UNUSED (pdata);
51 : UNUSED (opNum);
52 : UNUSED (param);
53 0 : return TRUE;
54 : }
55 :
56 : /** @brief tensordec-plugin's GstTensorDecoderDef callback */
57 : static GstCaps *
58 33 : os_getOutCaps (void **pdata, const GstTensorsConfig * config)
59 : {
60 : GstCaps *caps;
61 : UNUSED (pdata);
62 :
63 33 : caps = gst_caps_from_string (OCTET_CAPS_STR);
64 33 : setFramerateFromConfig (caps, config);
65 33 : return caps;
66 : }
67 :
68 : /** @brief tensordec-plugin's GstTensorDecoderDef callback */
69 : static GstFlowReturn
70 12 : os_decode (void **pdata, const GstTensorsConfig * config,
71 : const GstTensorMemory * input, GstBuffer * outbuf)
72 : {
73 : guint i;
74 : gboolean is_flexible;
75 : GstTensorMetaInfo meta;
76 : gpointer mem_data;
77 : UNUSED (pdata);
78 :
79 12 : if (!config || !input || !outbuf) {
80 0 : ml_loge ("NULL parameter is passed to tensor_decoder::octet_stream");
81 12 : return GST_FLOW_ERROR;
82 : }
83 12 : is_flexible = gst_tensors_config_is_flexible (config);
84 :
85 30 : for (i = 0; i < config->info.num_tensors; i++) {
86 18 : gsize offset = 0, data_size = 0;
87 18 : GstMemory *mem = NULL;
88 :
89 18 : if (is_flexible) {
90 9 : gst_tensor_meta_info_parse_header (&meta, input[i].data);
91 9 : offset = gst_tensor_meta_info_get_header_size (&meta);
92 9 : data_size = gst_tensor_meta_info_get_data_size (&meta);
93 : } else {
94 9 : data_size = gst_tensors_info_get_size (&config->info, i);
95 : }
96 18 : mem_data = _g_memdup ((guint8 *) input[i].data + offset, data_size);
97 18 : mem = gst_memory_new_wrapped ((GstMemoryFlags) 0, mem_data, data_size,
98 : 0, data_size, NULL, g_free);
99 18 : gst_buffer_append_memory (outbuf, mem);
100 : }
101 :
102 12 : return GST_FLOW_OK;
103 : }
104 :
105 : static gchar decoder_subplugin_octet_stream[] = "octet_stream";
106 :
107 : /** @brief octet stream tensordec-plugin GstTensorDecoderDef instance */
108 : static GstTensorDecoderDef octetSTream = {
109 : .modename = decoder_subplugin_octet_stream,
110 : .init = os_init,
111 : .exit = os_exit,
112 : .setOption = os_setOption,
113 : .getOutCaps = os_getOutCaps,
114 : .getTransformSize = NULL,
115 : .decode = os_decode
116 : };
117 :
118 : /** @brief Initialize this object for tensordec-plugin */
119 : void
120 35 : init_os (void)
121 : {
122 35 : nnstreamer_decoder_probe (&octetSTream);
123 35 : }
124 :
125 : /** @brief Destruct this object for tensordec-plugin */
126 : void
127 35 : fini_os (void)
128 : {
129 35 : nnstreamer_decoder_exit (octetSTream.modename);
130 35 : }
|