]> git.sesse.net Git - ffmpeg/blob - libavfilter/dnn/dnn_backend_openvino.c
dnn/openvino: support run inference via GPU
[ffmpeg] / libavfilter / dnn / dnn_backend_openvino.c
1 /*
2  * Copyright (c) 2020
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * DNN OpenVINO backend implementation.
24  */
25
26 #include "dnn_backend_openvino.h"
27 #include "libavformat/avio.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/avstring.h"
31 #include "../internal.h"
32 #include <c_api/ie_c_api.h>
33
34 typedef struct OVOptions{
35     char *device_type;
36 } OVOptions;
37
38 typedef struct OVContext {
39     const AVClass *class;
40     OVOptions options;
41 } OVContext;
42
43 typedef struct OVModel{
44     OVContext ctx;
45     ie_core_t *core;
46     ie_network_t *network;
47     ie_executable_network_t *exe_network;
48     ie_infer_request_t *infer_request;
49     ie_blob_t *input_blob;
50 } OVModel;
51
52 #define APPEND_STRING(generated_string, iterate_string)                                            \
53     generated_string = generated_string ? av_asprintf("%s %s", generated_string, iterate_string) : \
54                                           av_asprintf("%s", iterate_string);
55
56 #define OFFSET(x) offsetof(OVContext, x)
57 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
58 static const AVOption dnn_openvino_options[] = {
59     { "device", "device to run model", OFFSET(options.device_type), AV_OPT_TYPE_STRING, { .str = "CPU" }, 0, 0, FLAGS },
60     { NULL }
61 };
62
63 AVFILTER_DEFINE_CLASS(dnn_openvino);
64
65 static DNNDataType precision_to_datatype(precision_e precision)
66 {
67     switch (precision)
68     {
69     case FP32:
70         return DNN_FLOAT;
71     default:
72         av_assert0(!"not supported yet.");
73         return DNN_FLOAT;
74     }
75 }
76
77 static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input_name)
78 {
79     OVModel *ov_model = (OVModel *)model;
80     OVContext *ctx = &ov_model->ctx;
81     char *model_input_name = NULL;
82     IEStatusCode status;
83     size_t model_input_count = 0;
84     dimensions_t dims;
85     precision_e precision;
86
87     status = ie_network_get_inputs_number(ov_model->network, &model_input_count);
88     if (status != OK) {
89         av_log(ctx, AV_LOG_ERROR, "Failed to get input count\n");
90         return DNN_ERROR;
91     }
92
93     for (size_t i = 0; i < model_input_count; i++) {
94         status = ie_network_get_input_name(ov_model->network, i, &model_input_name);
95         if (status != OK) {
96             av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's name\n", (int)i);
97             return DNN_ERROR;
98         }
99         if (strcmp(model_input_name, input_name) == 0) {
100             ie_network_name_free(&model_input_name);
101             status |= ie_network_get_input_dims(ov_model->network, input_name, &dims);
102             status |= ie_network_get_input_precision(ov_model->network, input_name, &precision);
103             if (status != OK) {
104                 av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's dims or precision\n", (int)i);
105                 return DNN_ERROR;
106             }
107
108             // The order of dims in the openvino is fixed and it is always NCHW for 4-D data.
109             // while we pass NHWC data from FFmpeg to openvino
110             status = ie_network_set_input_layout(ov_model->network, input_name, NHWC);
111             if (status != OK) {
112                 av_log(ctx, AV_LOG_ERROR, "Input \"%s\" does not match layout NHWC\n", input_name);
113                 return DNN_ERROR;
114             }
115
116             input->channels = dims.dims[1];
117             input->height   = dims.dims[2];
118             input->width    = dims.dims[3];
119             input->dt       = precision_to_datatype(precision);
120             return DNN_SUCCESS;
121         }
122
123         ie_network_name_free(&model_input_name);
124     }
125
126     av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model\n", model_input_name);
127     return DNN_ERROR;
128 }
129
130 static DNNReturnType set_input_ov(void *model, DNNData *input, const char *input_name)
131 {
132     OVModel *ov_model = (OVModel *)model;
133     OVContext *ctx = &ov_model->ctx;
134     IEStatusCode status;
135     dimensions_t dims;
136     precision_e precision;
137     ie_blob_buffer_t blob_buffer;
138
139     status = ie_exec_network_create_infer_request(ov_model->exe_network, &ov_model->infer_request);
140     if (status != OK)
141         goto err;
142
143     status = ie_infer_request_get_blob(ov_model->infer_request, input_name, &ov_model->input_blob);
144     if (status != OK)
145         goto err;
146
147     status |= ie_blob_get_dims(ov_model->input_blob, &dims);
148     status |= ie_blob_get_precision(ov_model->input_blob, &precision);
149     if (status != OK)
150         goto err;
151
152     av_assert0(input->channels == dims.dims[1]);
153     av_assert0(input->height   == dims.dims[2]);
154     av_assert0(input->width    == dims.dims[3]);
155     av_assert0(input->dt       == precision_to_datatype(precision));
156
157     status = ie_blob_get_buffer(ov_model->input_blob, &blob_buffer);
158     if (status != OK)
159         goto err;
160     input->data = blob_buffer.buffer;
161
162     return DNN_SUCCESS;
163
164 err:
165     if (ov_model->input_blob)
166         ie_blob_free(&ov_model->input_blob);
167     if (ov_model->infer_request)
168         ie_infer_request_free(&ov_model->infer_request);
169     av_log(ctx, AV_LOG_ERROR, "Failed to create inference instance or get input data/dims/precision/memory\n");
170     return DNN_ERROR;
171 }
172
173 DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options)
174 {
175     char *all_dev_names = NULL;
176     DNNModel *model = NULL;
177     OVModel *ov_model = NULL;
178     OVContext *ctx = NULL;
179     IEStatusCode status;
180     ie_config_t config = {NULL, NULL, NULL};
181     ie_available_devices_t a_dev;
182
183     model = av_malloc(sizeof(DNNModel));
184     if (!model){
185         return NULL;
186     }
187
188     ov_model = av_mallocz(sizeof(OVModel));
189     if (!ov_model)
190         goto err;
191     ov_model->ctx.class = &dnn_openvino_class;
192     ctx = &ov_model->ctx;
193
194     //parse options
195     av_opt_set_defaults(ctx);
196     if (av_opt_set_from_string(ctx, options, NULL, "=", "&") < 0) {
197         av_log(ctx, AV_LOG_ERROR, "Failed to parse options \"%s\"\n", options);
198         goto err;
199     }
200
201     status = ie_core_create("", &ov_model->core);
202     if (status != OK)
203         goto err;
204
205     status = ie_core_read_network(ov_model->core, model_filename, NULL, &ov_model->network);
206     if (status != OK)
207         goto err;
208
209     status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
210     if (status != OK) {
211         av_log(ctx, AV_LOG_ERROR, "Failed to init OpenVINO model\n");
212         status = ie_core_get_available_devices(ov_model->core, &a_dev);
213         if (status != OK) {
214             av_log(ctx, AV_LOG_ERROR, "Failed to get available devices\n");
215             goto err;
216         }
217         for (int i = 0; i < a_dev.num_devices; i++) {
218             APPEND_STRING(all_dev_names, a_dev.devices[i])
219         }
220         av_log(ctx, AV_LOG_ERROR,"device %s may not be supported, all available devices are: \"%s\"\n",
221                ctx->options.device_type, all_dev_names);
222         goto err;
223     }
224
225     model->model = (void *)ov_model;
226     model->set_input = &set_input_ov;
227     model->get_input = &get_input_ov;
228     model->options = options;
229
230     return model;
231
232 err:
233     if (model)
234         av_freep(&model);
235     if (ov_model) {
236         if (ov_model->exe_network)
237             ie_exec_network_free(&ov_model->exe_network);
238         if (ov_model->network)
239             ie_network_free(&ov_model->network);
240         if (ov_model->core)
241             ie_core_free(&ov_model->core);
242         av_freep(&ov_model);
243     }
244     return NULL;
245 }
246
247 DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, DNNData *outputs, const char **output_names, uint32_t nb_output)
248 {
249     dimensions_t dims;
250     precision_e precision;
251     ie_blob_buffer_t blob_buffer;
252     OVModel *ov_model = (OVModel *)model->model;
253     OVContext *ctx = &ov_model->ctx;
254     IEStatusCode status = ie_infer_request_infer(ov_model->infer_request);
255     if (status != OK) {
256         av_log(ctx, AV_LOG_ERROR, "Failed to start synchronous model inference\n");
257         return DNN_ERROR;
258     }
259
260     for (uint32_t i = 0; i < nb_output; ++i) {
261         const char *output_name = output_names[i];
262         ie_blob_t *output_blob = NULL;
263         status = ie_infer_request_get_blob(ov_model->infer_request, output_name, &output_blob);
264         if (status != OK) {
265             av_log(ctx, AV_LOG_ERROR, "Failed to get model output data\n");
266             return DNN_ERROR;
267         }
268
269         status = ie_blob_get_buffer(output_blob, &blob_buffer);
270         if (status != OK) {
271             av_log(ctx, AV_LOG_ERROR, "Failed to access output memory\n");
272             return DNN_ERROR;
273         }
274
275         status |= ie_blob_get_dims(output_blob, &dims);
276         status |= ie_blob_get_precision(output_blob, &precision);
277         if (status != OK) {
278             av_log(ctx, AV_LOG_ERROR, "Failed to get dims or precision of output\n");
279             return DNN_ERROR;
280         }
281
282         outputs[i].channels = dims.dims[1];
283         outputs[i].height   = dims.dims[2];
284         outputs[i].width    = dims.dims[3];
285         outputs[i].dt       = precision_to_datatype(precision);
286         outputs[i].data     = blob_buffer.buffer;
287     }
288
289     return DNN_SUCCESS;
290 }
291
292 void ff_dnn_free_model_ov(DNNModel **model)
293 {
294     if (*model){
295         OVModel *ov_model = (OVModel *)(*model)->model;
296         if (ov_model->input_blob)
297             ie_blob_free(&ov_model->input_blob);
298         if (ov_model->infer_request)
299             ie_infer_request_free(&ov_model->infer_request);
300         if (ov_model->exe_network)
301             ie_exec_network_free(&ov_model->exe_network);
302         if (ov_model->network)
303             ie_network_free(&ov_model->network);
304         if (ov_model->core)
305             ie_core_free(&ov_model->core);
306         av_freep(&ov_model);
307         av_freep(model);
308     }
309 }