FFmpeg
dnn_backend_openvino.c
Go to the documentation of this file.
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;
41 } OVContext;
42 
43 typedef struct OVModel{
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  char *all_input_names = NULL;
83  IEStatusCode status;
84  size_t model_input_count = 0;
85  dimensions_t dims;
86  precision_e precision;
87 
88  status = ie_network_get_inputs_number(ov_model->network, &model_input_count);
89  if (status != OK) {
90  av_log(ctx, AV_LOG_ERROR, "Failed to get input count\n");
91  return DNN_ERROR;
92  }
93 
94  for (size_t i = 0; i < model_input_count; i++) {
95  status = ie_network_get_input_name(ov_model->network, i, &model_input_name);
96  if (status != OK) {
97  av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's name\n", (int)i);
98  return DNN_ERROR;
99  }
100  if (strcmp(model_input_name, input_name) == 0) {
101  ie_network_name_free(&model_input_name);
102  status |= ie_network_get_input_dims(ov_model->network, input_name, &dims);
103  status |= ie_network_get_input_precision(ov_model->network, input_name, &precision);
104  if (status != OK) {
105  av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's dims or precision\n", (int)i);
106  return DNN_ERROR;
107  }
108 
109  // The order of dims in the openvino is fixed and it is always NCHW for 4-D data.
110  // while we pass NHWC data from FFmpeg to openvino
111  status = ie_network_set_input_layout(ov_model->network, input_name, NHWC);
112  if (status != OK) {
113  av_log(ctx, AV_LOG_ERROR, "Input \"%s\" does not match layout NHWC\n", input_name);
114  return DNN_ERROR;
115  }
116 
117  input->channels = dims.dims[1];
118  input->height = dims.dims[2];
119  input->width = dims.dims[3];
120  input->dt = precision_to_datatype(precision);
121  return DNN_SUCCESS;
122  } else {
123  //incorrect input name
124  APPEND_STRING(all_input_names, model_input_name)
125  }
126 
127  ie_network_name_free(&model_input_name);
128  }
129 
130  av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model, all input(s) are: \"%s\"\n", input_name, all_input_names);
131  return DNN_ERROR;
132 }
133 
134 static DNNReturnType set_input_ov(void *model, DNNData *input, const char *input_name)
135 {
136  OVModel *ov_model = (OVModel *)model;
137  OVContext *ctx = &ov_model->ctx;
138  IEStatusCode status;
139  dimensions_t dims;
140  precision_e precision;
141  ie_blob_buffer_t blob_buffer;
142 
143  status = ie_exec_network_create_infer_request(ov_model->exe_network, &ov_model->infer_request);
144  if (status != OK)
145  goto err;
146 
147  status = ie_infer_request_get_blob(ov_model->infer_request, input_name, &ov_model->input_blob);
148  if (status != OK)
149  goto err;
150 
151  status |= ie_blob_get_dims(ov_model->input_blob, &dims);
152  status |= ie_blob_get_precision(ov_model->input_blob, &precision);
153  if (status != OK)
154  goto err;
155 
156  av_assert0(input->channels == dims.dims[1]);
157  av_assert0(input->height == dims.dims[2]);
158  av_assert0(input->width == dims.dims[3]);
159  av_assert0(input->dt == precision_to_datatype(precision));
160 
161  status = ie_blob_get_buffer(ov_model->input_blob, &blob_buffer);
162  if (status != OK)
163  goto err;
164  input->data = blob_buffer.buffer;
165 
166  return DNN_SUCCESS;
167 
168 err:
169  if (ov_model->input_blob)
170  ie_blob_free(&ov_model->input_blob);
171  if (ov_model->infer_request)
172  ie_infer_request_free(&ov_model->infer_request);
173  av_log(ctx, AV_LOG_ERROR, "Failed to create inference instance or get input data/dims/precision/memory\n");
174  return DNN_ERROR;
175 }
176 
177 DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options)
178 {
179  char *all_dev_names = NULL;
180  DNNModel *model = NULL;
181  OVModel *ov_model = NULL;
182  OVContext *ctx = NULL;
183  IEStatusCode status;
184  ie_config_t config = {NULL, NULL, NULL};
185  ie_available_devices_t a_dev;
186 
187  model = av_malloc(sizeof(DNNModel));
188  if (!model){
189  return NULL;
190  }
191 
192  ov_model = av_mallocz(sizeof(OVModel));
193  if (!ov_model)
194  goto err;
195  ov_model->ctx.class = &dnn_openvino_class;
196  ctx = &ov_model->ctx;
197 
198  //parse options
199  av_opt_set_defaults(ctx);
200  if (av_opt_set_from_string(ctx, options, NULL, "=", "&") < 0) {
201  av_log(ctx, AV_LOG_ERROR, "Failed to parse options \"%s\"\n", options);
202  goto err;
203  }
204 
205  status = ie_core_create("", &ov_model->core);
206  if (status != OK)
207  goto err;
208 
209  status = ie_core_read_network(ov_model->core, model_filename, NULL, &ov_model->network);
210  if (status != OK)
211  goto err;
212 
213  status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
214  if (status != OK) {
215  av_log(ctx, AV_LOG_ERROR, "Failed to init OpenVINO model\n");
216  status = ie_core_get_available_devices(ov_model->core, &a_dev);
217  if (status != OK) {
218  av_log(ctx, AV_LOG_ERROR, "Failed to get available devices\n");
219  goto err;
220  }
221  for (int i = 0; i < a_dev.num_devices; i++) {
222  APPEND_STRING(all_dev_names, a_dev.devices[i])
223  }
224  av_log(ctx, AV_LOG_ERROR,"device %s may not be supported, all available devices are: \"%s\"\n",
225  ctx->options.device_type, all_dev_names);
226  goto err;
227  }
228 
229  model->model = (void *)ov_model;
230  model->set_input = &set_input_ov;
231  model->get_input = &get_input_ov;
232  model->options = options;
233 
234  return model;
235 
236 err:
237  if (model)
238  av_freep(&model);
239  if (ov_model) {
240  if (ov_model->exe_network)
241  ie_exec_network_free(&ov_model->exe_network);
242  if (ov_model->network)
243  ie_network_free(&ov_model->network);
244  if (ov_model->core)
245  ie_core_free(&ov_model->core);
246  av_freep(&ov_model);
247  }
248  return NULL;
249 }
250 
251 DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, DNNData *outputs, const char **output_names, uint32_t nb_output)
252 {
253  char *model_output_name = NULL;
254  char *all_output_names = NULL;
255  dimensions_t dims;
256  precision_e precision;
257  ie_blob_buffer_t blob_buffer;
258  OVModel *ov_model = (OVModel *)model->model;
259  OVContext *ctx = &ov_model->ctx;
260  IEStatusCode status = ie_infer_request_infer(ov_model->infer_request);
261  size_t model_output_count = 0;
262  if (status != OK) {
263  av_log(ctx, AV_LOG_ERROR, "Failed to start synchronous model inference\n");
264  return DNN_ERROR;
265  }
266 
267  for (uint32_t i = 0; i < nb_output; ++i) {
268  const char *output_name = output_names[i];
269  ie_blob_t *output_blob = NULL;
270  status = ie_infer_request_get_blob(ov_model->infer_request, output_name, &output_blob);
271  if (status != OK) {
272  //incorrect output name
273  av_log(ctx, AV_LOG_ERROR, "Failed to get model output data\n");
274  status = ie_network_get_outputs_number(ov_model->network, &model_output_count);
275  for (size_t i = 0; i < model_output_count; i++) {
276  status = ie_network_get_output_name(ov_model->network, i, &model_output_name);
277  APPEND_STRING(all_output_names, model_output_name)
278  }
280  "output \"%s\" may not correct, all output(s) are: \"%s\"\n",
281  output_name, all_output_names);
282  return DNN_ERROR;
283  }
284 
285  status = ie_blob_get_buffer(output_blob, &blob_buffer);
286  if (status != OK) {
287  av_log(ctx, AV_LOG_ERROR, "Failed to access output memory\n");
288  return DNN_ERROR;
289  }
290 
291  status |= ie_blob_get_dims(output_blob, &dims);
292  status |= ie_blob_get_precision(output_blob, &precision);
293  if (status != OK) {
294  av_log(ctx, AV_LOG_ERROR, "Failed to get dims or precision of output\n");
295  return DNN_ERROR;
296  }
297 
298  outputs[i].channels = dims.dims[1];
299  outputs[i].height = dims.dims[2];
300  outputs[i].width = dims.dims[3];
301  outputs[i].dt = precision_to_datatype(precision);
302  outputs[i].data = blob_buffer.buffer;
303  }
304 
305  return DNN_SUCCESS;
306 }
307 
309 {
310  if (*model){
311  OVModel *ov_model = (OVModel *)(*model)->model;
312  if (ov_model->input_blob)
313  ie_blob_free(&ov_model->input_blob);
314  if (ov_model->infer_request)
315  ie_infer_request_free(&ov_model->infer_request);
316  if (ov_model->exe_network)
317  ie_exec_network_free(&ov_model->exe_network);
318  if (ov_model->network)
319  ie_network_free(&ov_model->network);
320  if (ov_model->core)
321  ie_core_free(&ov_model->core);
322  av_freep(&ov_model);
323  av_freep(model);
324  }
325 }
void * model
Definition: dnn_interface.h:45
#define NULL
Definition: coverity.c:32
Buffered I/O operations.
AVOption.
Definition: opt.h:248
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:1357
int channels
Definition: dnn_interface.h:40
static DNNReturnType set_input_ov(void *model, DNNData *input, const char *input_name)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
const char * options
Definition: dnn_interface.h:47
int av_opt_set_from_string(void *ctx, const char *opts, const char *const *shorthand, const char *key_val_sep, const char *pairs_sep)
Parse the key-value pairs list in opts.
Definition: opt.c:1558
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define av_malloc(s)
AVOptions.
static const AVOption dnn_openvino_options[]
static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input_name)
ie_infer_request_t * infer_request
#define av_log(a,...)
DNNDataType
Definition: dnn_interface.h:35
DNNReturnType(* get_input)(void *model, DNNData *input, const char *input_name)
Definition: dnn_interface.h:50
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int height
Definition: dnn_interface.h:40
void * data
Definition: dnn_interface.h:38
simple assert() macros that are a bit more flexible than ISO C assert().
ie_core_t * core
#define FLAGS
OVOptions options
static DNNDataType precision_to_datatype(precision_e precision)
const AVClass * class
DNNModel * ff_dnn_load_model_ov(const char *model_filename, const char *options)
OVContext ctx
AVFormatContext * ctx
Definition: movenc.c:48
#define OFFSET(x)
DNNReturnType
Definition: dnn_interface.h:31
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
AVFILTER_DEFINE_CLASS(dnn_openvino)
if(ret)
ie_network_t * network
Describe the class of an AVClass context structure.
Definition: log.h:67
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
ie_blob_t * input_blob
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
void ff_dnn_free_model_ov(DNNModel **model)
const OptionDef options[]
Definition: ffmpeg_opt.c:3393
DNN inference functions interface for OpenVINO backend.
DNNReturnType(* set_input)(void *model, DNNData *input, const char *input_name)
Definition: dnn_interface.h:53
#define APPEND_STRING(generated_string, iterate_string)
#define av_freep(p)
DNNDataType dt
Definition: dnn_interface.h:39
ie_executable_network_t * exe_network
DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, DNNData *outputs, const char **output_names, uint32_t nb_output)
int i
Definition: input.c:407