[MachineLearning.Inference] Pipeline class to execute neural network stream (#1404)
[platform/core/csapi/tizenfx.git] / src / Tizen.MachineLearning.Inference / Interop / Interop.Nnstreamer.cs
1 /*
2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 using System;
18 using System.Runtime.InteropServices;
19 using Tizen.MachineLearning.Inference;
20
21 internal static partial class Interop
22 {
23     internal static partial class Libraries
24     {
25         public const string Nnstreamer = "libcapi-nnstreamer.so.0";
26     }
27
28     internal static partial class Pipeline
29     {
30         /* typedef void (*ml_pipeline_state_cb) (ml_pipeline_state_e state, void *user_data); */
31         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
32         internal delegate void StateChangedCallback(PipelineState state, IntPtr user_data);
33
34         /* typedef void (*ml_pipeline_sink_cb) (const ml_tensors_data_h data, const ml_tensors_info_h info, void *user_data); */
35         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
36         internal delegate void NewDataCallback(IntPtr data, IntPtr info, IntPtr user_data);
37
38         /* int ml_pipeline_construct (const char *pipeline_description, ml_pipeline_state_cb cb, void *user_data, ml_pipeline_h *pipe); */
39         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_pipeline_construct", CallingConvention = CallingConvention.Cdecl)]
40         internal static extern NNStreamerError Construct(string pipeline_description, StateChangedCallback callback, IntPtr user_data, out IntPtr pipeline_handle);
41
42         /* int ml_pipeline_destroy (ml_pipeline_h pipe); */
43         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_pipeline_destroy", CallingConvention = CallingConvention.Cdecl)]
44         internal static extern NNStreamerError Destroy(IntPtr pipeline_handle);
45
46         /* int ml_pipeline_get_state (ml_pipeline_h pipe, ml_pipeline_state_e *state) */
47         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_pipeline_get_state", CallingConvention = CallingConvention.Cdecl)]
48         internal static extern NNStreamerError GetState(IntPtr pipeline_handle, out int state);
49
50         /* int ml_pipeline_start (ml_pipeline_h pipe); */
51         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_pipeline_start", CallingConvention = CallingConvention.Cdecl)]
52         internal static extern NNStreamerError Start(IntPtr pipeline_handle);
53
54         /* int ml_pipeline_stop (ml_pipeline_h pipe); */
55         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_pipeline_stop", CallingConvention = CallingConvention.Cdecl)]
56         internal static extern NNStreamerError Stop(IntPtr pipeline_handle);
57
58         /* int ml_pipeline_sink_register (ml_pipeline_h pipe, const char *sink_name, ml_pipeline_sink_cb cb, void *user_data, ml_pipeline_sink_h *sink_handle); */
59         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_pipeline_sink_register", CallingConvention = CallingConvention.Cdecl)]
60         internal static extern NNStreamerError RegisterSinkCallback(IntPtr pipeline_handle, string sink_name, NewDataCallback callback, IntPtr user_data, out IntPtr sink_handle);
61
62         /* int ml_pipeline_sink_unregister (ml_pipeline_sink_h sink_handle); */
63         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_pipeline_sink_unregister", CallingConvention = CallingConvention.Cdecl)]
64         internal static extern NNStreamerError UnregisterSinkCallback(IntPtr sink_handle);
65
66         /* int ml_pipeline_src_get_handle (ml_pipeline_h pipe, const char *src_name, ml_pipeline_src_h *src_handle); */
67         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_pipeline_src_get_handle", CallingConvention = CallingConvention.Cdecl)]
68         internal static extern NNStreamerError GetSrcHandle(IntPtr pipeline_handle, string src_name, out IntPtr src_handle);
69
70         /* int ml_pipeline_src_release_handle (ml_pipeline_src_h src_handle); */
71         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_pipeline_src_release_handle", CallingConvention = CallingConvention.Cdecl)]
72         internal static extern NNStreamerError ReleaseSrcHandle(IntPtr src_handle);
73
74         /* int ml_pipeline_src_input_data (ml_pipeline_src_h src_handle, ml_tensors_data_h data, ml_pipeline_buf_policy_e policy); */
75         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_pipeline_src_input_data", CallingConvention = CallingConvention.Cdecl)]
76         internal static extern NNStreamerError InputSrcData(IntPtr src_handle, IntPtr data_handle, PipelineBufferPolicy policy);
77
78         /* int ml_pipeline_valve_get_handle (ml_pipeline_h pipe, const char *valve_name, ml_pipeline_valve_h *valve_handle); */
79         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_pipeline_valve_get_handle", CallingConvention = CallingConvention.Cdecl)]
80         internal static extern NNStreamerError GetValveHandle(IntPtr pipeline_handle, string valve_name, out IntPtr valve_handle);
81
82         /* int ml_pipeline_valve_release_handle (ml_pipeline_valve_h valve_handle); */
83         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_pipeline_valve_release_handle", CallingConvention = CallingConvention.Cdecl)]
84         internal static extern NNStreamerError ReleaseValveHandle(IntPtr valve_handle);
85
86         /* int ml_pipeline_valve_set_open (ml_pipeline_valve_h valve_handle, bool open); */
87         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_pipeline_valve_set_open", CallingConvention = CallingConvention.Cdecl)]
88         internal static extern NNStreamerError OpenValve(IntPtr valve_handle, bool open);
89
90         /* int ml_pipeline_switch_get_handle (ml_pipeline_h pipe, const char *switch_name, ml_pipeline_switch_e *switch_type, ml_pipeline_switch_h *switch_handle); */
91         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_pipeline_switch_get_handle", CallingConvention = CallingConvention.Cdecl)]
92         internal static extern NNStreamerError GetSwitchHandle(IntPtr pipeline_handle, string switch_name, out SwitchType switch_type, out IntPtr switch_handle);
93
94         /* int ml_pipeline_switch_release_handle (ml_pipeline_switch_h switch_handle); */
95         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_pipeline_switch_release_handle", CallingConvention = CallingConvention.Cdecl)]
96         internal static extern NNStreamerError ReleaseSwitchHandle(IntPtr switch_handle);
97
98         /* int ml_pipeline_switch_select (ml_pipeline_switch_h switch_handle, const char *pad_name); */
99         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_pipeline_switch_select", CallingConvention = CallingConvention.Cdecl)]
100         internal static extern NNStreamerError SelectSwitchPad(IntPtr switch_handle, string pad_name);
101     }
102
103     internal static partial class SingleShot
104     {
105         /* int ml_single_open (ml_single_h *single, const char *model, const ml_tensors_info_h input_info, const ml_tensors_info_h output_info, ml_nnfw_type_e nnfw, ml_nnfw_hw_e hw) */
106         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_open", CallingConvention = CallingConvention.Cdecl)]
107         internal static extern NNStreamerError OpenSingle(out IntPtr single_handle, string model_path, IntPtr input_info, IntPtr output_info, NNFWType nn_type, HWType hw_type);
108
109         /* int ml_single_close (ml_single_h single) */
110         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_close", CallingConvention = CallingConvention.Cdecl)]
111         internal static extern NNStreamerError CloseSingle(IntPtr single_handle);
112
113         /* int ml_single_invoke (ml_single_h single, const ml_tensors_data_h input, ml_tensors_data_h *output) */
114         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_invoke", CallingConvention = CallingConvention.Cdecl)]
115         internal static extern NNStreamerError InvokeSingle(IntPtr single_handle, IntPtr input_data, out IntPtr output_data);
116
117         /* int ml_single_invoke_dynamic (ml_single_h single, const ml_tensors_data_h input, const ml_tensors_info_h in_info, ml_tensors_data_h * output, ml_tensors_info_h * out_info) */
118         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_invoke_dynamic", CallingConvention = CallingConvention.Cdecl)]
119         internal static extern NNStreamerError InvokeSingleDynamic(IntPtr single_handle, IntPtr input_data, IntPtr input_info, out IntPtr output_data, out IntPtr output_info);
120
121         /* int ml_single_get_input_info (ml_single_h single, ml_tensors_info_h *info) */
122         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_get_input_info", CallingConvention = CallingConvention.Cdecl)]
123         internal static extern NNStreamerError GetInputTensorsInfo(IntPtr single_handle, out IntPtr input_info);
124
125         /* int ml_single_get_output_info (ml_single_h single, ml_tensors_info_h *info) */
126         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_get_output_info", CallingConvention = CallingConvention.Cdecl)]
127         internal static extern NNStreamerError GetOutputTensorsInfo(IntPtr single_handle, out IntPtr output_info);
128
129         /* int ml_single_set_input_info (ml_single_h single, const ml_tensors_info_h info) */
130         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_set_input_info", CallingConvention = CallingConvention.Cdecl)]
131         internal static extern NNStreamerError SetInputInfo(IntPtr single_handle, IntPtr in_handle);
132
133         /* int ml_single_set_timeout (ml_single_h single, unsigned int timeout)*/
134         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_set_timeout", CallingConvention = CallingConvention.Cdecl)]
135         internal static extern NNStreamerError SetTimeout(IntPtr single_handle, int time_ms);
136
137         /* int ml_single_set_property (ml_single_h single, const char *name, const char *value) */
138         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_set_property", CallingConvention = CallingConvention.Cdecl)]
139         internal static extern NNStreamerError SetValue(IntPtr single_handle, string name, string value);
140
141         /* int ml_single_get_property (ml_single_h single, const char *name, char **value) */
142         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_get_property", CallingConvention = CallingConvention.Cdecl)]
143         internal static extern NNStreamerError GetValue(IntPtr single_handle, string name, out IntPtr value);
144     }
145
146     internal static partial class Util
147     {
148         /* int ml_tensors_info_create (ml_tensors_info_h *info) */
149         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_create", CallingConvention = CallingConvention.Cdecl)]
150         internal static extern NNStreamerError CreateTensorsInfo(out IntPtr info);
151
152         /* int ml_tensors_info_destroy (ml_tensors_info_h info) */
153         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_destroy", CallingConvention = CallingConvention.Cdecl)]
154         internal static extern NNStreamerError DestroyTensorsInfo(IntPtr info);
155
156         /* int ml_tensors_info_set_count (ml_tensors_info_h info, unsigned int count) */
157         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_set_count", CallingConvention = CallingConvention.Cdecl)]
158         internal static extern NNStreamerError SetTensorsCount(IntPtr info, int count);
159
160         /* int ml_tensors_info_get_count (ml_tensors_info_h info, unsigned int *count) */
161         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_get_count", CallingConvention = CallingConvention.Cdecl)]
162         internal static extern NNStreamerError GetTensorsCount(IntPtr info, out int count);
163
164         /* int ml_tensors_info_set_tensor_name (ml_tensors_info_h info, unsigned int index, const char *name) */
165         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_set_tensor_name", CallingConvention = CallingConvention.Cdecl)]
166         internal static extern NNStreamerError SetTensorName(IntPtr info, int index, string name);
167
168         /* int ml_tensors_info_get_tensor_name (ml_tensors_info_h info, unsigned int index, char **name) */
169         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_get_tensor_name", CallingConvention = CallingConvention.Cdecl)]
170         internal static extern NNStreamerError GetTensorName(IntPtr info, int index, out string name);
171
172         /* int ml_tensors_info_set_tensor_type (ml_tensors_info_h info, unsigned int index, const ml_tensor_type_e type) */
173         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_set_tensor_type", CallingConvention = CallingConvention.Cdecl)]
174         internal static extern NNStreamerError SetTensorType(IntPtr info, int index, TensorType type);
175
176         /* int ml_tensors_info_get_tensor_type (ml_tensors_info_h info, unsigned int index, ml_tensor_type_e *type) */
177         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_get_tensor_type", CallingConvention = CallingConvention.Cdecl)]
178         internal static extern NNStreamerError GetTensorType(IntPtr info, int index, out TensorType type);
179
180         /* int ml_tensors_info_set_tensor_dimension (ml_tensors_info_h info, unsigned int index, const ml_tensor_dimension dimension) */
181         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_set_tensor_dimension", CallingConvention = CallingConvention.Cdecl)]
182         internal static extern NNStreamerError SetTensorDimension(IntPtr info, int index, int[] dimension);
183
184         /* int ml_tensors_info_get_tensor_dimension (ml_tensors_info_h info, unsigned int index, ml_tensor_dimension dimension) */
185         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_get_tensor_dimension", CallingConvention = CallingConvention.Cdecl)]
186         internal static extern NNStreamerError GetTensorDimension(IntPtr info, int index, [In, Out] uint[] dimension);
187
188         /* int ml_tensors_data_create (const ml_tensors_info_h info, ml_tensors_data_h *data) */
189         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_data_create", CallingConvention = CallingConvention.Cdecl)]
190         internal static extern NNStreamerError CreateTensorsData(IntPtr info, out IntPtr data);
191
192         /* int ml_tensors_data_destroy (ml_tensors_data_h data) */
193         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_data_destroy", CallingConvention = CallingConvention.Cdecl)]
194         internal static extern NNStreamerError DestroyTensorsData(IntPtr data);
195
196         /* int ml_tensors_data_get_tensor_data (ml_tensors_data_h data, unsigned int index, void **raw_data, size_t *data_size) */
197         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_data_get_tensor_data", CallingConvention = CallingConvention.Cdecl)]
198         internal static extern NNStreamerError GetTensorData(IntPtr data, int index, out IntPtr raw_data, out int data_size);
199
200         /* int ml_tensors_data_set_tensor_data (ml_tensors_data_h data, unsigned int index, const void *raw_data, const size_t data_size) */
201         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_data_set_tensor_data", CallingConvention = CallingConvention.Cdecl)]
202         internal static extern NNStreamerError SetTensorData(IntPtr data, int index, byte[] raw_data, int data_size);
203
204         /* int ml_check_nnfw_availability (ml_nnfw_type_e nnfw, ml_nnfw_hw_e hw, bool *available); */
205         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_check_nnfw_availability", CallingConvention = CallingConvention.Cdecl)]
206         internal static extern NNStreamerError CheckNNFWAvailability(NNFWType nnfw, HWType hw, out bool available);
207
208         internal static byte[] IntPtrToByteArray(IntPtr unmanagedByteArray, int size)
209         {
210             byte[] retByte = new byte[size];
211             Marshal.Copy(unmanagedByteArray, retByte, 0, size);
212             return retByte;
213         }
214
215         internal static string IntPtrToString(IntPtr val)
216         {
217             return (val != IntPtr.Zero) ? Marshal.PtrToStringAnsi(val) : string.Empty;
218         }
219     }
220 }