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 "dnn_io_proc.h"
28 #include "libavformat/avio.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/avstring.h"
32 #include "../internal.h"
34 #include "safe_queue.h"
35 #include <c_api/ie_c_api.h>
37 typedef struct OVOptions{
44 typedef struct OVContext {
49 typedef struct OVModel{
53 ie_network_t *network;
54 ie_executable_network_t *exe_network;
55 ie_infer_request_t *infer_request;
57 /* for async execution */
58 SafeQueue *request_queue; // holds RequestItem
59 Queue *task_queue; // holds TaskItem
62 typedef struct TaskItem {
64 const char *input_name;
66 const char *output_name;
73 typedef struct RequestItem {
74 ie_infer_request_t *infer_request;
77 ie_complete_call_back_t callback;
80 #define APPEND_STRING(generated_string, iterate_string) \
81 generated_string = generated_string ? av_asprintf("%s %s", generated_string, iterate_string) : \
82 av_asprintf("%s", iterate_string);
84 #define OFFSET(x) offsetof(OVContext, x)
85 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
86 static const AVOption dnn_openvino_options[] = {
87 { "device", "device to run model", OFFSET(options.device_type), AV_OPT_TYPE_STRING, { .str = "CPU" }, 0, 0, FLAGS },
88 { "nireq", "number of request", OFFSET(options.nireq), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
89 { "batch_size", "batch size per request", OFFSET(options.batch_size), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 1000, FLAGS},
90 { "input_resizable", "can input be resizable or not", OFFSET(options.input_resizable), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
94 AVFILTER_DEFINE_CLASS(dnn_openvino);
96 static DNNDataType precision_to_datatype(precision_e precision)
103 av_assert0(!"not supported yet.");
108 static int get_datatype_size(DNNDataType dt)
113 return sizeof(float);
115 av_assert0(!"not supported yet.");
120 static DNNReturnType fill_model_input_ov(OVModel *ov_model, RequestItem *request)
123 precision_e precision;
124 ie_blob_buffer_t blob_buffer;
125 OVContext *ctx = &ov_model->ctx;
128 ie_blob_t *input_blob = NULL;
129 TaskItem *task = request->tasks[0];
131 status = ie_infer_request_get_blob(request->infer_request, task->input_name, &input_blob);
133 av_log(ctx, AV_LOG_ERROR, "Failed to get input blob with name %s\n", task->input_name);
137 status |= ie_blob_get_dims(input_blob, &dims);
138 status |= ie_blob_get_precision(input_blob, &precision);
140 av_log(ctx, AV_LOG_ERROR, "Failed to get input blob dims/precision\n");
144 status = ie_blob_get_buffer(input_blob, &blob_buffer);
146 av_log(ctx, AV_LOG_ERROR, "Failed to get input blob buffer\n");
150 input.height = dims.dims[2];
151 input.width = dims.dims[3];
152 input.channels = dims.dims[1];
153 input.data = blob_buffer.buffer;
154 input.dt = precision_to_datatype(precision);
156 av_assert0(request->task_count <= dims.dims[0]);
157 for (int i = 0; i < request->task_count; ++i) {
158 task = request->tasks[i];
159 if (task->do_ioproc) {
160 if (ov_model->model->pre_proc != NULL) {
161 ov_model->model->pre_proc(task->in_frame, &input, ov_model->model->filter_ctx);
163 ff_proc_from_frame_to_dnn(task->in_frame, &input, ctx);
166 input.data = (uint8_t *)input.data
167 + input.width * input.height * input.channels * get_datatype_size(input.dt);
169 ie_blob_free(&input_blob);
174 static void infer_completion_callback(void *args)
177 precision_e precision;
179 RequestItem *request = args;
180 TaskItem *task = request->tasks[0];
181 SafeQueue *requestq = task->ov_model->request_queue;
182 ie_blob_t *output_blob = NULL;
183 ie_blob_buffer_t blob_buffer;
185 OVContext *ctx = &task->ov_model->ctx;
187 status = ie_infer_request_get_blob(request->infer_request, task->output_name, &output_blob);
189 //incorrect output name
190 char *model_output_name = NULL;
191 char *all_output_names = NULL;
192 size_t model_output_count = 0;
193 av_log(ctx, AV_LOG_ERROR, "Failed to get model output data\n");
194 status = ie_network_get_outputs_number(task->ov_model->network, &model_output_count);
195 for (size_t i = 0; i < model_output_count; i++) {
196 status = ie_network_get_output_name(task->ov_model->network, i, &model_output_name);
197 APPEND_STRING(all_output_names, model_output_name)
199 av_log(ctx, AV_LOG_ERROR,
200 "output \"%s\" may not correct, all output(s) are: \"%s\"\n",
201 task->output_name, all_output_names);
205 status = ie_blob_get_buffer(output_blob, &blob_buffer);
207 av_log(ctx, AV_LOG_ERROR, "Failed to access output memory\n");
211 status |= ie_blob_get_dims(output_blob, &dims);
212 status |= ie_blob_get_precision(output_blob, &precision);
214 av_log(ctx, AV_LOG_ERROR, "Failed to get dims or precision of output\n");
218 output.channels = dims.dims[1];
219 output.height = dims.dims[2];
220 output.width = dims.dims[3];
221 output.dt = precision_to_datatype(precision);
222 output.data = blob_buffer.buffer;
224 av_assert0(request->task_count <= dims.dims[0]);
225 av_assert0(request->task_count >= 1);
226 for (int i = 0; i < request->task_count; ++i) {
227 task = request->tasks[i];
228 if (task->do_ioproc) {
229 if (task->ov_model->model->post_proc != NULL) {
230 task->ov_model->model->post_proc(task->out_frame, &output, task->ov_model->model->filter_ctx);
232 ff_proc_from_dnn_to_frame(task->out_frame, &output, ctx);
235 task->out_frame->width = output.width;
236 task->out_frame->height = output.height;
239 output.data = (uint8_t *)output.data
240 + output.width * output.height * output.channels * get_datatype_size(output.dt);
242 ie_blob_free(&output_blob);
244 request->task_count = 0;
247 if (ff_safe_queue_push_back(requestq, request) < 0) {
248 av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
254 static DNNReturnType init_model_ov(OVModel *ov_model, const char *input_name, const char *output_name)
256 OVContext *ctx = &ov_model->ctx;
258 ie_available_devices_t a_dev;
259 ie_config_t config = {NULL, NULL, NULL};
260 char *all_dev_names = NULL;
263 if (ctx->options.batch_size <= 0) {
264 ctx->options.batch_size = 1;
267 if (ctx->options.batch_size > 1) {
268 input_shapes_t input_shapes;
269 status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
272 for (int i = 0; i < input_shapes.shape_num; i++)
273 input_shapes.shapes[i].shape.dims[0] = ctx->options.batch_size;
274 status = ie_network_reshape(ov_model->network, input_shapes);
275 ie_network_input_shapes_free(&input_shapes);
280 // The order of dims in the openvino is fixed and it is always NCHW for 4-D data.
281 // while we pass NHWC data from FFmpeg to openvino
282 status = ie_network_set_input_layout(ov_model->network, input_name, NHWC);
284 av_log(ctx, AV_LOG_ERROR, "Failed to set layout as NHWC for input %s\n", input_name);
287 status = ie_network_set_output_layout(ov_model->network, output_name, NHWC);
289 av_log(ctx, AV_LOG_ERROR, "Failed to set layout as NHWC for output %s\n", output_name);
293 status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
295 av_log(ctx, AV_LOG_ERROR, "Failed to load OpenVINO model network\n");
296 status = ie_core_get_available_devices(ov_model->core, &a_dev);
298 av_log(ctx, AV_LOG_ERROR, "Failed to get available devices\n");
301 for (int i = 0; i < a_dev.num_devices; i++) {
302 APPEND_STRING(all_dev_names, a_dev.devices[i])
304 av_log(ctx, AV_LOG_ERROR,"device %s may not be supported, all available devices are: \"%s\"\n",
305 ctx->options.device_type, all_dev_names);
309 // create infer_request for sync execution
310 status = ie_exec_network_create_infer_request(ov_model->exe_network, &ov_model->infer_request);
314 // create infer_requests for async execution
315 if (ctx->options.nireq <= 0) {
316 // the default value is a rough estimation
317 ctx->options.nireq = av_cpu_count() / 2 + 1;
320 ov_model->request_queue = ff_safe_queue_create();
321 if (!ov_model->request_queue) {
325 for (int i = 0; i < ctx->options.nireq; i++) {
326 RequestItem *item = av_mallocz(sizeof(*item));
331 status = ie_exec_network_create_infer_request(ov_model->exe_network, &item->infer_request);
337 item->tasks = av_malloc_array(ctx->options.batch_size, sizeof(*item->tasks));
342 item->task_count = 0;
344 item->callback.completeCallBackFunc = infer_completion_callback;
345 item->callback.args = item;
346 if (ff_safe_queue_push_back(ov_model->request_queue, item) < 0) {
352 ov_model->task_queue = ff_queue_create();
353 if (!ov_model->task_queue) {
360 ff_dnn_free_model_ov(&ov_model->model);
364 static DNNReturnType execute_model_ov(RequestItem *request)
368 TaskItem *task = request->tasks[0];
369 OVContext *ctx = &task->ov_model->ctx;
372 if (request->task_count < ctx->options.batch_size) {
373 if (ff_safe_queue_push_front(task->ov_model->request_queue, request) < 0) {
374 av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
379 ret = fill_model_input_ov(task->ov_model, request);
380 if (ret != DNN_SUCCESS) {
383 status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
385 av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
388 status = ie_infer_request_infer_async(request->infer_request);
390 av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
395 ret = fill_model_input_ov(task->ov_model, request);
396 if (ret != DNN_SUCCESS) {
399 status = ie_infer_request_infer(request->infer_request);
401 av_log(ctx, AV_LOG_ERROR, "Failed to start synchronous model inference\n");
404 infer_completion_callback(request);
405 return task->done ? DNN_SUCCESS : DNN_ERROR;
409 static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input_name)
411 OVModel *ov_model = model;
412 OVContext *ctx = &ov_model->ctx;
413 char *model_input_name = NULL;
414 char *all_input_names = NULL;
416 size_t model_input_count = 0;
418 precision_e precision;
419 int input_resizable = ctx->options.input_resizable;
421 status = ie_network_get_inputs_number(ov_model->network, &model_input_count);
423 av_log(ctx, AV_LOG_ERROR, "Failed to get input count\n");
427 for (size_t i = 0; i < model_input_count; i++) {
428 status = ie_network_get_input_name(ov_model->network, i, &model_input_name);
430 av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's name\n", (int)i);
433 if (strcmp(model_input_name, input_name) == 0) {
434 ie_network_name_free(&model_input_name);
435 status |= ie_network_get_input_dims(ov_model->network, input_name, &dims);
436 status |= ie_network_get_input_precision(ov_model->network, input_name, &precision);
438 av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's dims or precision\n", (int)i);
442 input->channels = dims.dims[1];
443 input->height = input_resizable ? -1 : dims.dims[2];
444 input->width = input_resizable ? -1 : dims.dims[3];
445 input->dt = precision_to_datatype(precision);
448 //incorrect input name
449 APPEND_STRING(all_input_names, model_input_name)
452 ie_network_name_free(&model_input_name);
455 av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model, all input(s) are: \"%s\"\n", input_name, all_input_names);
459 static DNNReturnType get_output_ov(void *model, const char *input_name, int input_width, int input_height,
460 const char *output_name, int *output_width, int *output_height)
463 OVModel *ov_model = model;
464 OVContext *ctx = &ov_model->ctx;
467 AVFrame *in_frame = av_frame_alloc();
468 AVFrame *out_frame = NULL;
469 TaskItem *ptask = &task;
471 input_shapes_t input_shapes;
474 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
477 out_frame = av_frame_alloc();
479 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
480 av_frame_free(&in_frame);
483 in_frame->width = input_width;
484 in_frame->height = input_height;
486 if (ctx->options.input_resizable) {
487 status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
488 input_shapes.shapes->shape.dims[2] = input_height;
489 input_shapes.shapes->shape.dims[3] = input_width;
490 status |= ie_network_reshape(ov_model->network, input_shapes);
491 ie_network_input_shapes_free(&input_shapes);
493 av_log(ctx, AV_LOG_ERROR, "Failed to reshape input size for %s\n", input_name);
498 if (!ov_model->exe_network) {
499 if (init_model_ov(ov_model, input_name, output_name) != DNN_SUCCESS) {
500 av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
508 task.input_name = input_name;
509 task.in_frame = in_frame;
510 task.output_name = output_name;
511 task.out_frame = out_frame;
512 task.ov_model = ov_model;
514 request.infer_request = ov_model->infer_request;
515 request.task_count = 1;
516 request.tasks = &ptask;
518 ret = execute_model_ov(&request);
519 *output_width = out_frame->width;
520 *output_height = out_frame->height;
522 av_frame_free(&out_frame);
523 av_frame_free(&in_frame);
527 DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
529 DNNModel *model = NULL;
530 OVModel *ov_model = NULL;
531 OVContext *ctx = NULL;
534 model = av_mallocz(sizeof(DNNModel));
539 ov_model = av_mallocz(sizeof(OVModel));
544 model->model = ov_model;
545 ov_model->model = model;
546 ov_model->ctx.class = &dnn_openvino_class;
547 ctx = &ov_model->ctx;
550 av_opt_set_defaults(ctx);
551 if (av_opt_set_from_string(ctx, options, NULL, "=", "&") < 0) {
552 av_log(ctx, AV_LOG_ERROR, "Failed to parse options \"%s\"\n", options);
556 status = ie_core_create("", &ov_model->core);
560 status = ie_core_read_network(ov_model->core, model_filename, NULL, &ov_model->network);
563 ver = ie_c_api_version();
564 av_log(ctx, AV_LOG_ERROR, "Failed to read the network from model file %s,\n"
565 "Please check if the model version matches the runtime OpenVINO %s\n",
566 model_filename, ver.api_version);
567 ie_version_free(&ver);
571 model->get_input = &get_input_ov;
572 model->get_output = &get_output_ov;
573 model->options = options;
574 model->filter_ctx = filter_ctx;
575 model->func_type = func_type;
580 ff_dnn_free_model_ov(&model);
584 DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
585 const char **output_names, uint32_t nb_output, AVFrame *out_frame)
587 OVModel *ov_model = model->model;
588 OVContext *ctx = &ov_model->ctx;
591 TaskItem *ptask = &task;
594 av_log(ctx, AV_LOG_ERROR, "in frame is NULL when execute model.\n");
599 av_log(ctx, AV_LOG_ERROR, "out frame is NULL when execute model.\n");
603 if (nb_output != 1) {
604 // currently, the filter does not need multiple outputs,
605 // so we just pending the support until we really need it.
606 avpriv_report_missing_feature(ctx, "multiple outputs");
610 if (ctx->options.batch_size > 1) {
611 avpriv_report_missing_feature(ctx, "batch mode for sync execution");
615 if (!ov_model->exe_network) {
616 if (init_model_ov(ov_model, input_name, output_names[0]) != DNN_SUCCESS) {
617 av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
625 task.input_name = input_name;
626 task.in_frame = in_frame;
627 task.output_name = output_names[0];
628 task.out_frame = out_frame;
629 task.ov_model = ov_model;
631 request.infer_request = ov_model->infer_request;
632 request.task_count = 1;
633 request.tasks = &ptask;
635 return execute_model_ov(&request);
638 DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
639 const char **output_names, uint32_t nb_output, AVFrame *out_frame)
641 OVModel *ov_model = model->model;
642 OVContext *ctx = &ov_model->ctx;
643 RequestItem *request;
647 av_log(ctx, AV_LOG_ERROR, "in frame is NULL when async execute model.\n");
652 av_log(ctx, AV_LOG_ERROR, "out frame is NULL when async execute model.\n");
656 task = av_malloc(sizeof(*task));
658 av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");
662 if (!ov_model->exe_network) {
663 if (init_model_ov(ov_model, input_name, output_names[0]) != DNN_SUCCESS) {
664 av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
672 task->input_name = input_name;
673 task->in_frame = in_frame;
674 task->output_name = output_names[0];
675 task->out_frame = out_frame;
676 task->ov_model = ov_model;
677 if (ff_queue_push_back(ov_model->task_queue, task) < 0) {
679 av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n");
683 request = ff_safe_queue_pop_front(ov_model->request_queue);
685 av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
689 request->tasks[request->task_count++] = task;
690 return execute_model_ov(request);
693 DNNAsyncStatusType ff_dnn_get_async_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
695 OVModel *ov_model = model->model;
696 TaskItem *task = ff_queue_peek_front(ov_model->task_queue);
699 return DAST_EMPTY_QUEUE;
703 return DAST_NOT_READY;
706 *in = task->in_frame;
707 *out = task->out_frame;
708 ff_queue_pop_front(ov_model->task_queue);
714 DNNReturnType ff_dnn_flush_ov(const DNNModel *model)
716 OVModel *ov_model = model->model;
717 OVContext *ctx = &ov_model->ctx;
718 RequestItem *request;
722 request = ff_safe_queue_pop_front(ov_model->request_queue);
724 av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
728 if (request->task_count == 0) {
729 // no pending task need to flush
730 if (ff_safe_queue_push_back(ov_model->request_queue, request) < 0) {
731 av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
737 ret = fill_model_input_ov(ov_model, request);
738 if (ret != DNN_SUCCESS) {
739 av_log(ctx, AV_LOG_ERROR, "Failed to fill model input.\n");
742 status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
744 av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
747 status = ie_infer_request_infer_async(request->infer_request);
749 av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
756 void ff_dnn_free_model_ov(DNNModel **model)
759 OVModel *ov_model = (*model)->model;
760 while (ff_safe_queue_size(ov_model->request_queue) != 0) {
761 RequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue);
762 if (item && item->infer_request) {
763 ie_infer_request_free(&item->infer_request);
765 av_freep(&item->tasks);
768 ff_safe_queue_destroy(ov_model->request_queue);
770 while (ff_queue_size(ov_model->task_queue) != 0) {
771 TaskItem *item = ff_queue_pop_front(ov_model->task_queue);
772 av_frame_free(&item->in_frame);
773 av_frame_free(&item->out_frame);
776 ff_queue_destroy(ov_model->task_queue);
778 if (ov_model->infer_request)
779 ie_infer_request_free(&ov_model->infer_request);
780 if (ov_model->exe_network)
781 ie_exec_network_free(&ov_model->exe_network);
782 if (ov_model->network)
783 ie_network_free(&ov_model->network);
785 ie_core_free(&ov_model->core);