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 <c_api/ie_c_api.h>
31 typedef struct OVModel{
33 ie_network_t *network;
34 ie_executable_network_t *exe_network;
35 ie_infer_request_t *infer_request;
36 ie_blob_t *input_blob;
39 static DNNDataType precision_to_datatype(precision_e precision)
46 av_assert0(!"not supported yet.");
51 static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input_name)
53 OVModel *ov_model = (OVModel *)model;
54 char *model_input_name = NULL;
56 size_t model_input_count = 0;
58 precision_e precision;
60 status = ie_network_get_inputs_number(ov_model->network, &model_input_count);
64 for (size_t i = 0; i < model_input_count; i++) {
65 status = ie_network_get_input_name(ov_model->network, i, &model_input_name);
68 if (strcmp(model_input_name, input_name) == 0) {
69 ie_network_name_free(&model_input_name);
70 status |= ie_network_get_input_dims(ov_model->network, input_name, &dims);
71 status |= ie_network_get_input_precision(ov_model->network, input_name, &precision);
75 // The order of dims in the openvino is fixed and it is always NCHW for 4-D data.
76 // while we pass NHWC data from FFmpeg to openvino
77 status = ie_network_set_input_layout(ov_model->network, input_name, NHWC);
81 input->channels = dims.dims[1];
82 input->height = dims.dims[2];
83 input->width = dims.dims[3];
84 input->dt = precision_to_datatype(precision);
88 ie_network_name_free(&model_input_name);
94 static DNNReturnType set_input_ov(void *model, DNNData *input, const char *input_name)
96 OVModel *ov_model = (OVModel *)model;
99 precision_e precision;
100 ie_blob_buffer_t blob_buffer;
102 status = ie_exec_network_create_infer_request(ov_model->exe_network, &ov_model->infer_request);
106 status = ie_infer_request_get_blob(ov_model->infer_request, input_name, &ov_model->input_blob);
110 status |= ie_blob_get_dims(ov_model->input_blob, &dims);
111 status |= ie_blob_get_precision(ov_model->input_blob, &precision);
115 av_assert0(input->channels == dims.dims[1]);
116 av_assert0(input->height == dims.dims[2]);
117 av_assert0(input->width == dims.dims[3]);
118 av_assert0(input->dt == precision_to_datatype(precision));
120 status = ie_blob_get_buffer(ov_model->input_blob, &blob_buffer);
123 input->data = blob_buffer.buffer;
128 if (ov_model->input_blob)
129 ie_blob_free(&ov_model->input_blob);
130 if (ov_model->infer_request)
131 ie_infer_request_free(&ov_model->infer_request);
135 DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options)
137 DNNModel *model = NULL;
138 OVModel *ov_model = NULL;
140 ie_config_t config = {NULL, NULL, NULL};
142 model = av_malloc(sizeof(DNNModel));
147 ov_model = av_mallocz(sizeof(OVModel));
151 status = ie_core_create("", &ov_model->core);
155 status = ie_core_read_network(ov_model->core, model_filename, NULL, &ov_model->network);
159 status = ie_core_load_network(ov_model->core, ov_model->network, "CPU", &config, &ov_model->exe_network);
163 model->model = (void *)ov_model;
164 model->set_input = &set_input_ov;
165 model->get_input = &get_input_ov;
166 model->options = options;
174 if (ov_model->exe_network)
175 ie_exec_network_free(&ov_model->exe_network);
176 if (ov_model->network)
177 ie_network_free(&ov_model->network);
179 ie_core_free(&ov_model->core);
185 DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, DNNData *outputs, const char **output_names, uint32_t nb_output)
188 precision_e precision;
189 ie_blob_buffer_t blob_buffer;
190 OVModel *ov_model = (OVModel *)model->model;
191 IEStatusCode status = ie_infer_request_infer(ov_model->infer_request);
195 for (uint32_t i = 0; i < nb_output; ++i) {
196 const char *output_name = output_names[i];
197 ie_blob_t *output_blob = NULL;
198 status = ie_infer_request_get_blob(ov_model->infer_request, output_name, &output_blob);
202 status = ie_blob_get_buffer(output_blob, &blob_buffer);
206 status |= ie_blob_get_dims(output_blob, &dims);
207 status |= ie_blob_get_precision(output_blob, &precision);
211 outputs[i].channels = dims.dims[1];
212 outputs[i].height = dims.dims[2];
213 outputs[i].width = dims.dims[3];
214 outputs[i].dt = precision_to_datatype(precision);
215 outputs[i].data = blob_buffer.buffer;
221 void ff_dnn_free_model_ov(DNNModel **model)
224 OVModel *ov_model = (OVModel *)(*model)->model;
225 if (ov_model->input_blob)
226 ie_blob_free(&ov_model->input_blob);
227 if (ov_model->infer_request)
228 ie_infer_request_free(&ov_model->infer_request);
229 if (ov_model->exe_network)
230 ie_exec_network_free(&ov_model->exe_network);
231 if (ov_model->network)
232 ie_network_free(&ov_model->network);
234 ie_core_free(&ov_model->core);