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 FFSafeQueue *request_queue; // holds RequestItem
59 FFQueue *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 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 ie_blob_t *output_blob = NULL;
182 ie_blob_buffer_t blob_buffer;
184 OVContext *ctx = &task->ov_model->ctx;
186 status = ie_infer_request_get_blob(request->infer_request, task->output_name, &output_blob);
188 //incorrect output name
189 char *model_output_name = NULL;
190 char *all_output_names = NULL;
191 size_t model_output_count = 0;
192 av_log(ctx, AV_LOG_ERROR, "Failed to get model output data\n");
193 status = ie_network_get_outputs_number(task->ov_model->network, &model_output_count);
194 for (size_t i = 0; i < model_output_count; i++) {
195 status = ie_network_get_output_name(task->ov_model->network, i, &model_output_name);
196 APPEND_STRING(all_output_names, model_output_name)
198 av_log(ctx, AV_LOG_ERROR,
199 "output \"%s\" may not correct, all output(s) are: \"%s\"\n",
200 task->output_name, all_output_names);
204 status = ie_blob_get_buffer(output_blob, &blob_buffer);
206 av_log(ctx, AV_LOG_ERROR, "Failed to access output memory\n");
210 status |= ie_blob_get_dims(output_blob, &dims);
211 status |= ie_blob_get_precision(output_blob, &precision);
213 av_log(ctx, AV_LOG_ERROR, "Failed to get dims or precision of output\n");
217 output.channels = dims.dims[1];
218 output.height = dims.dims[2];
219 output.width = dims.dims[3];
220 output.dt = precision_to_datatype(precision);
221 output.data = blob_buffer.buffer;
223 av_assert0(request->task_count <= dims.dims[0]);
224 av_assert0(request->task_count >= 1);
225 for (int i = 0; i < request->task_count; ++i) {
226 task = request->tasks[i];
227 if (task->do_ioproc) {
228 if (task->ov_model->model->post_proc != NULL) {
229 task->ov_model->model->post_proc(task->out_frame, &output, task->ov_model->model->filter_ctx);
231 proc_from_dnn_to_frame(task->out_frame, &output, ctx);
234 task->out_frame->width = output.width;
235 task->out_frame->height = output.height;
238 output.data = (uint8_t *)output.data
239 + output.width * output.height * output.channels * get_datatype_size(output.dt);
241 ie_blob_free(&output_blob);
243 request->task_count = 0;
246 if (ff_safe_queue_push_back(task->ov_model->request_queue, request) < 0) {
247 av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
253 static DNNReturnType init_model_ov(OVModel *ov_model)
255 OVContext *ctx = &ov_model->ctx;
257 ie_available_devices_t a_dev;
258 ie_config_t config = {NULL, NULL, NULL};
259 char *all_dev_names = NULL;
262 if (ctx->options.batch_size <= 0) {
263 ctx->options.batch_size = 1;
266 if (ctx->options.batch_size > 1) {
267 input_shapes_t input_shapes;
268 status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
271 for (int i = 0; i < input_shapes.shape_num; i++)
272 input_shapes.shapes[i].shape.dims[0] = ctx->options.batch_size;
273 status = ie_network_reshape(ov_model->network, input_shapes);
274 ie_network_input_shapes_free(&input_shapes);
279 status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
281 av_log(ctx, AV_LOG_ERROR, "Failed to load OpenVINO model network\n");
282 status = ie_core_get_available_devices(ov_model->core, &a_dev);
284 av_log(ctx, AV_LOG_ERROR, "Failed to get available devices\n");
287 for (int i = 0; i < a_dev.num_devices; i++) {
288 APPEND_STRING(all_dev_names, a_dev.devices[i])
290 av_log(ctx, AV_LOG_ERROR,"device %s may not be supported, all available devices are: \"%s\"\n",
291 ctx->options.device_type, all_dev_names);
295 // create infer_request for sync execution
296 status = ie_exec_network_create_infer_request(ov_model->exe_network, &ov_model->infer_request);
300 // create infer_requests for async execution
301 if (ctx->options.nireq <= 0) {
302 // the default value is a rough estimation
303 ctx->options.nireq = av_cpu_count() / 2 + 1;
306 ov_model->request_queue = ff_safe_queue_create();
307 if (!ov_model->request_queue) {
311 for (int i = 0; i < ctx->options.nireq; i++) {
312 RequestItem *item = av_mallocz(sizeof(*item));
317 status = ie_exec_network_create_infer_request(ov_model->exe_network, &item->infer_request);
323 item->tasks = av_malloc_array(ctx->options.batch_size, sizeof(*item->tasks));
328 item->task_count = 0;
330 item->callback.completeCallBackFunc = infer_completion_callback;
331 item->callback.args = item;
332 if (ff_safe_queue_push_back(ov_model->request_queue, item) < 0) {
338 ov_model->task_queue = ff_queue_create();
339 if (!ov_model->task_queue) {
346 ff_dnn_free_model_ov(&ov_model->model);
350 static DNNReturnType execute_model_ov(RequestItem *request)
354 TaskItem *task = request->tasks[0];
355 OVContext *ctx = &task->ov_model->ctx;
358 if (request->task_count < ctx->options.batch_size) {
359 if (ff_safe_queue_push_front(task->ov_model->request_queue, request) < 0) {
360 av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
365 ret = fill_model_input_ov(task->ov_model, request);
366 if (ret != DNN_SUCCESS) {
369 status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
371 av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
374 status = ie_infer_request_infer_async(request->infer_request);
376 av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
381 ret = fill_model_input_ov(task->ov_model, request);
382 if (ret != DNN_SUCCESS) {
385 status = ie_infer_request_infer(request->infer_request);
387 av_log(ctx, AV_LOG_ERROR, "Failed to start synchronous model inference\n");
390 infer_completion_callback(request);
391 return task->done ? DNN_SUCCESS : DNN_ERROR;
395 static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input_name)
397 OVModel *ov_model = (OVModel *)model;
398 OVContext *ctx = &ov_model->ctx;
399 char *model_input_name = NULL;
400 char *all_input_names = NULL;
402 size_t model_input_count = 0;
404 precision_e precision;
405 int input_resizable = ctx->options.input_resizable;
407 status = ie_network_get_inputs_number(ov_model->network, &model_input_count);
409 av_log(ctx, AV_LOG_ERROR, "Failed to get input count\n");
413 for (size_t i = 0; i < model_input_count; i++) {
414 status = ie_network_get_input_name(ov_model->network, i, &model_input_name);
416 av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's name\n", (int)i);
419 if (strcmp(model_input_name, input_name) == 0) {
420 ie_network_name_free(&model_input_name);
421 status |= ie_network_get_input_dims(ov_model->network, input_name, &dims);
422 status |= ie_network_get_input_precision(ov_model->network, input_name, &precision);
424 av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's dims or precision\n", (int)i);
428 input->channels = dims.dims[1];
429 input->height = input_resizable ? -1 : dims.dims[2];
430 input->width = input_resizable ? -1 : dims.dims[3];
431 input->dt = precision_to_datatype(precision);
434 //incorrect input name
435 APPEND_STRING(all_input_names, model_input_name)
438 ie_network_name_free(&model_input_name);
441 av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model, all input(s) are: \"%s\"\n", input_name, all_input_names);
445 static DNNReturnType get_output_ov(void *model, const char *input_name, int input_width, int input_height,
446 const char *output_name, int *output_width, int *output_height)
449 OVModel *ov_model = (OVModel *)model;
450 OVContext *ctx = &ov_model->ctx;
453 AVFrame *in_frame = av_frame_alloc();
454 AVFrame *out_frame = NULL;
455 TaskItem *ptask = &task;
457 input_shapes_t input_shapes;
460 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
463 out_frame = av_frame_alloc();
465 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
466 av_frame_free(&in_frame);
469 in_frame->width = input_width;
470 in_frame->height = input_height;
472 if (ctx->options.input_resizable) {
473 status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
474 input_shapes.shapes->shape.dims[2] = input_height;
475 input_shapes.shapes->shape.dims[3] = input_width;
476 status |= ie_network_reshape(ov_model->network, input_shapes);
477 ie_network_input_shapes_free(&input_shapes);
479 av_log(ctx, AV_LOG_ERROR, "Failed to reshape input size for %s\n", input_name);
484 if (!ov_model->exe_network) {
485 if (init_model_ov(ov_model) != DNN_SUCCESS) {
486 av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
494 task.input_name = input_name;
495 task.in_frame = in_frame;
496 task.output_name = output_name;
497 task.out_frame = out_frame;
498 task.ov_model = ov_model;
500 request.infer_request = ov_model->infer_request;
501 request.task_count = 1;
502 request.tasks = &ptask;
504 ret = execute_model_ov(&request);
505 *output_width = out_frame->width;
506 *output_height = out_frame->height;
508 av_frame_free(&out_frame);
509 av_frame_free(&in_frame);
513 DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options, AVFilterContext *filter_ctx)
515 DNNModel *model = NULL;
516 OVModel *ov_model = NULL;
517 OVContext *ctx = NULL;
520 model = av_mallocz(sizeof(DNNModel));
525 ov_model = av_mallocz(sizeof(OVModel));
530 model->model = (void *)ov_model;
531 ov_model->model = model;
532 ov_model->ctx.class = &dnn_openvino_class;
533 ctx = &ov_model->ctx;
536 av_opt_set_defaults(ctx);
537 if (av_opt_set_from_string(ctx, options, NULL, "=", "&") < 0) {
538 av_log(ctx, AV_LOG_ERROR, "Failed to parse options \"%s\"\n", options);
542 status = ie_core_create("", &ov_model->core);
546 status = ie_core_read_network(ov_model->core, model_filename, NULL, &ov_model->network);
549 ver = ie_c_api_version();
550 av_log(ctx, AV_LOG_ERROR, "Failed to read the network from model file %s,\n"
551 "Please check if the model version matches the runtime OpenVINO %s\n",
552 model_filename, ver.api_version);
553 ie_version_free(&ver);
557 model->get_input = &get_input_ov;
558 model->get_output = &get_output_ov;
559 model->options = options;
560 model->filter_ctx = filter_ctx;
565 ff_dnn_free_model_ov(&model);
569 DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
570 const char **output_names, uint32_t nb_output, AVFrame *out_frame)
572 OVModel *ov_model = (OVModel *)model->model;
573 OVContext *ctx = &ov_model->ctx;
576 TaskItem *ptask = &task;
579 av_log(ctx, AV_LOG_ERROR, "in frame is NULL when execute model.\n");
584 av_log(ctx, AV_LOG_ERROR, "out frame is NULL when execute model.\n");
588 if (nb_output != 1) {
589 // currently, the filter does not need multiple outputs,
590 // so we just pending the support until we really need it.
591 av_log(ctx, AV_LOG_ERROR, "do not support multiple outputs\n");
595 if (ctx->options.batch_size > 1) {
596 av_log(ctx, AV_LOG_ERROR, "do not support batch mode for sync execution.\n");
600 if (!ov_model->exe_network) {
601 if (init_model_ov(ov_model) != DNN_SUCCESS) {
602 av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
610 task.input_name = input_name;
611 task.in_frame = in_frame;
612 task.output_name = output_names[0];
613 task.out_frame = out_frame;
614 task.ov_model = ov_model;
616 request.infer_request = ov_model->infer_request;
617 request.task_count = 1;
618 request.tasks = &ptask;
620 return execute_model_ov(&request);
623 DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
624 const char **output_names, uint32_t nb_output, AVFrame *out_frame)
626 OVModel *ov_model = (OVModel *)model->model;
627 OVContext *ctx = &ov_model->ctx;
628 RequestItem *request;
632 av_log(ctx, AV_LOG_ERROR, "in frame is NULL when async execute model.\n");
637 av_log(ctx, AV_LOG_ERROR, "out frame is NULL when async execute model.\n");
641 task = av_malloc(sizeof(*task));
643 av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");
647 if (!ov_model->exe_network) {
648 if (init_model_ov(ov_model) != DNN_SUCCESS) {
649 av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
657 task->input_name = input_name;
658 task->in_frame = in_frame;
659 task->output_name = output_names[0];
660 task->out_frame = out_frame;
661 task->ov_model = ov_model;
662 if (ff_queue_push_back(ov_model->task_queue, task) < 0) {
664 av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n");
668 request = ff_safe_queue_pop_front(ov_model->request_queue);
670 av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
674 request->tasks[request->task_count++] = task;
675 return execute_model_ov(request);
678 DNNAsyncStatusType ff_dnn_get_async_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
680 OVModel *ov_model = (OVModel *)model->model;
681 TaskItem *task = ff_queue_peek_front(ov_model->task_queue);
684 return DAST_EMPTY_QUEUE;
688 return DAST_NOT_READY;
691 *in = task->in_frame;
692 *out = task->out_frame;
693 ff_queue_pop_front(ov_model->task_queue);
699 DNNReturnType ff_dnn_flush_ov(const DNNModel *model)
701 OVModel *ov_model = (OVModel *)model->model;
702 OVContext *ctx = &ov_model->ctx;
703 RequestItem *request;
707 request = ff_safe_queue_pop_front(ov_model->request_queue);
709 av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
713 if (request->task_count == 0) {
714 // no pending task need to flush
715 if (ff_safe_queue_push_back(ov_model->request_queue, request) < 0) {
716 av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
722 ret = fill_model_input_ov(ov_model, request);
723 if (ret != DNN_SUCCESS) {
724 av_log(ctx, AV_LOG_ERROR, "Failed to fill model input.\n");
727 status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
729 av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
732 status = ie_infer_request_infer_async(request->infer_request);
734 av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
741 void ff_dnn_free_model_ov(DNNModel **model)
744 OVModel *ov_model = (OVModel *)(*model)->model;
745 while (ff_safe_queue_size(ov_model->request_queue) != 0) {
746 RequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue);
747 if (item && item->infer_request) {
748 ie_infer_request_free(&item->infer_request);
750 av_freep(&item->tasks);
753 ff_safe_queue_destroy(ov_model->request_queue);
755 while (ff_queue_size(ov_model->task_queue) != 0) {
756 TaskItem *item = ff_queue_pop_front(ov_model->task_queue);
757 av_frame_free(&item->in_frame);
758 av_frame_free(&item->out_frame);
761 ff_queue_destroy(ov_model->task_queue);
763 if (ov_model->infer_request)
764 ie_infer_request_free(&ov_model->infer_request);
765 if (ov_model->exe_network)
766 ie_exec_network_free(&ov_model->exe_network);
767 if (ov_model->network)
768 ie_network_free(&ov_model->network);
770 ie_core_free(&ov_model->core);