4 * This file is part of FFmpeg.
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.
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.
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
23 * DNN OpenVINO backend implementation.
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>
34 typedef struct OVOptions{
38 typedef struct OVContext {
43 typedef struct OVModel{
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;
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);
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 },
63 AVFILTER_DEFINE_CLASS(dnn_openvino);
65 static DNNDataType precision_to_datatype(precision_e precision)
72 av_assert0(!"not supported yet.");
77 static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input_name)
79 OVModel *ov_model = (OVModel *)model;
80 OVContext *ctx = &ov_model->ctx;
81 char *model_input_name = NULL;
83 size_t model_input_count = 0;
85 precision_e precision;
87 status = ie_network_get_inputs_number(ov_model->network, &model_input_count);
89 av_log(ctx, AV_LOG_ERROR, "Failed to get input count\n");
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);
96 av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's name\n", (int)i);
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);
104 av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's dims or precision\n", (int)i);
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);
112 av_log(ctx, AV_LOG_ERROR, "Input \"%s\" does not match layout NHWC\n", input_name);
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);
123 ie_network_name_free(&model_input_name);
126 av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model\n", model_input_name);
130 static DNNReturnType set_input_ov(void *model, DNNData *input, const char *input_name)
132 OVModel *ov_model = (OVModel *)model;
133 OVContext *ctx = &ov_model->ctx;
136 precision_e precision;
137 ie_blob_buffer_t blob_buffer;
139 status = ie_exec_network_create_infer_request(ov_model->exe_network, &ov_model->infer_request);
143 status = ie_infer_request_get_blob(ov_model->infer_request, input_name, &ov_model->input_blob);
147 status |= ie_blob_get_dims(ov_model->input_blob, &dims);
148 status |= ie_blob_get_precision(ov_model->input_blob, &precision);
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));
157 status = ie_blob_get_buffer(ov_model->input_blob, &blob_buffer);
160 input->data = blob_buffer.buffer;
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");
173 DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options)
175 char *all_dev_names = NULL;
176 DNNModel *model = NULL;
177 OVModel *ov_model = NULL;
178 OVContext *ctx = NULL;
180 ie_config_t config = {NULL, NULL, NULL};
181 ie_available_devices_t a_dev;
183 model = av_malloc(sizeof(DNNModel));
188 ov_model = av_mallocz(sizeof(OVModel));
191 ov_model->ctx.class = &dnn_openvino_class;
192 ctx = &ov_model->ctx;
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);
201 status = ie_core_create("", &ov_model->core);
205 status = ie_core_read_network(ov_model->core, model_filename, NULL, &ov_model->network);
209 status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
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);
214 av_log(ctx, AV_LOG_ERROR, "Failed to get available devices\n");
217 for (int i = 0; i < a_dev.num_devices; i++) {
218 APPEND_STRING(all_dev_names, a_dev.devices[i])
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);
225 model->model = (void *)ov_model;
226 model->set_input = &set_input_ov;
227 model->get_input = &get_input_ov;
228 model->options = options;
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);
241 ie_core_free(&ov_model->core);
247 DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, DNNData *outputs, const char **output_names, uint32_t nb_output)
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);
256 av_log(ctx, AV_LOG_ERROR, "Failed to start synchronous model inference\n");
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);
265 av_log(ctx, AV_LOG_ERROR, "Failed to get model output data\n");
269 status = ie_blob_get_buffer(output_blob, &blob_buffer);
271 av_log(ctx, AV_LOG_ERROR, "Failed to access output memory\n");
275 status |= ie_blob_get_dims(output_blob, &dims);
276 status |= ie_blob_get_precision(output_blob, &precision);
278 av_log(ctx, AV_LOG_ERROR, "Failed to get dims or precision of output\n");
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;
292 void ff_dnn_free_model_ov(DNNModel **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);
305 ie_core_free(&ov_model->core);