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{
43 typedef struct OVContext {
48 typedef struct OVModel{
52 ie_network_t *network;
53 ie_executable_network_t *exe_network;
54 ie_infer_request_t *infer_request;
56 /* for async execution */
57 FFSafeQueue *request_queue; // holds RequestItem
58 FFQueue *task_queue; // holds TaskItem
61 typedef struct TaskItem {
63 const char *input_name;
65 const char *output_name;
72 typedef struct RequestItem {
73 ie_infer_request_t *infer_request;
76 ie_complete_call_back_t callback;
79 #define APPEND_STRING(generated_string, iterate_string) \
80 generated_string = generated_string ? av_asprintf("%s %s", generated_string, iterate_string) : \
81 av_asprintf("%s", iterate_string);
83 #define OFFSET(x) offsetof(OVContext, x)
84 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
85 static const AVOption dnn_openvino_options[] = {
86 { "device", "device to run model", OFFSET(options.device_type), AV_OPT_TYPE_STRING, { .str = "CPU" }, 0, 0, FLAGS },
87 { "nireq", "number of request", OFFSET(options.nireq), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
88 { "batch_size", "batch size per request", OFFSET(options.batch_size), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 1000, FLAGS},
92 AVFILTER_DEFINE_CLASS(dnn_openvino);
94 static DNNDataType precision_to_datatype(precision_e precision)
101 av_assert0(!"not supported yet.");
106 static int get_datatype_size(DNNDataType dt)
111 return sizeof(float);
113 av_assert0(!"not supported yet.");
118 static DNNReturnType fill_model_input_ov(OVModel *ov_model, RequestItem *request)
121 precision_e precision;
122 ie_blob_buffer_t blob_buffer;
123 OVContext *ctx = &ov_model->ctx;
126 ie_blob_t *input_blob = NULL;
127 TaskItem *task = request->tasks[0];
129 status = ie_infer_request_get_blob(request->infer_request, task->input_name, &input_blob);
131 av_log(ctx, AV_LOG_ERROR, "Failed to get input blob with name %s\n", task->input_name);
135 status |= ie_blob_get_dims(input_blob, &dims);
136 status |= ie_blob_get_precision(input_blob, &precision);
138 av_log(ctx, AV_LOG_ERROR, "Failed to get input blob dims/precision\n");
142 status = ie_blob_get_buffer(input_blob, &blob_buffer);
144 av_log(ctx, AV_LOG_ERROR, "Failed to get input blob buffer\n");
148 input.height = dims.dims[2];
149 input.width = dims.dims[3];
150 input.channels = dims.dims[1];
151 input.data = blob_buffer.buffer;
152 input.dt = precision_to_datatype(precision);
154 av_assert0(request->task_count <= dims.dims[0]);
155 for (int i = 0; i < request->task_count; ++i) {
156 task = request->tasks[i];
157 if (task->do_ioproc) {
158 if (ov_model->model->pre_proc != NULL) {
159 ov_model->model->pre_proc(task->in_frame, &input, ov_model->model->filter_ctx);
161 proc_from_frame_to_dnn(task->in_frame, &input, ctx);
164 input.data = (uint8_t *)input.data
165 + input.width * input.height * input.channels * get_datatype_size(input.dt);
167 ie_blob_free(&input_blob);
172 static void infer_completion_callback(void *args)
175 precision_e precision;
177 RequestItem *request = args;
178 TaskItem *task = request->tasks[0];
179 ie_blob_t *output_blob = NULL;
180 ie_blob_buffer_t blob_buffer;
182 OVContext *ctx = &task->ov_model->ctx;
184 status = ie_infer_request_get_blob(request->infer_request, task->output_name, &output_blob);
186 //incorrect output name
187 char *model_output_name = NULL;
188 char *all_output_names = NULL;
189 size_t model_output_count = 0;
190 av_log(ctx, AV_LOG_ERROR, "Failed to get model output data\n");
191 status = ie_network_get_outputs_number(task->ov_model->network, &model_output_count);
192 for (size_t i = 0; i < model_output_count; i++) {
193 status = ie_network_get_output_name(task->ov_model->network, i, &model_output_name);
194 APPEND_STRING(all_output_names, model_output_name)
196 av_log(ctx, AV_LOG_ERROR,
197 "output \"%s\" may not correct, all output(s) are: \"%s\"\n",
198 task->output_name, all_output_names);
202 status = ie_blob_get_buffer(output_blob, &blob_buffer);
204 av_log(ctx, AV_LOG_ERROR, "Failed to access output memory\n");
208 status |= ie_blob_get_dims(output_blob, &dims);
209 status |= ie_blob_get_precision(output_blob, &precision);
211 av_log(ctx, AV_LOG_ERROR, "Failed to get dims or precision of output\n");
215 output.channels = dims.dims[1];
216 output.height = dims.dims[2];
217 output.width = dims.dims[3];
218 output.dt = precision_to_datatype(precision);
219 output.data = blob_buffer.buffer;
221 av_assert0(request->task_count <= dims.dims[0]);
222 av_assert0(request->task_count >= 1);
223 for (int i = 0; i < request->task_count; ++i) {
224 task = request->tasks[i];
225 if (task->do_ioproc) {
226 if (task->ov_model->model->post_proc != NULL) {
227 task->ov_model->model->post_proc(task->out_frame, &output, task->ov_model->model->filter_ctx);
229 proc_from_dnn_to_frame(task->out_frame, &output, ctx);
232 task->out_frame->width = output.width;
233 task->out_frame->height = output.height;
236 output.data = (uint8_t *)output.data
237 + output.width * output.height * output.channels * get_datatype_size(output.dt);
239 ie_blob_free(&output_blob);
241 request->task_count = 0;
244 if (ff_safe_queue_push_back(task->ov_model->request_queue, request) < 0) {
245 av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
251 static DNNReturnType init_model_ov(OVModel *ov_model)
253 OVContext *ctx = &ov_model->ctx;
255 ie_available_devices_t a_dev;
256 ie_config_t config = {NULL, NULL, NULL};
257 char *all_dev_names = NULL;
260 if (ctx->options.batch_size <= 0) {
261 ctx->options.batch_size = 1;
264 if (ctx->options.batch_size > 1) {
265 input_shapes_t input_shapes;
266 status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
269 for (int i = 0; i < input_shapes.shape_num; i++)
270 input_shapes.shapes[i].shape.dims[0] = ctx->options.batch_size;
271 status = ie_network_reshape(ov_model->network, input_shapes);
272 ie_network_input_shapes_free(&input_shapes);
277 status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
279 av_log(ctx, AV_LOG_ERROR, "Failed to load OpenVINO model network\n");
280 status = ie_core_get_available_devices(ov_model->core, &a_dev);
282 av_log(ctx, AV_LOG_ERROR, "Failed to get available devices\n");
285 for (int i = 0; i < a_dev.num_devices; i++) {
286 APPEND_STRING(all_dev_names, a_dev.devices[i])
288 av_log(ctx, AV_LOG_ERROR,"device %s may not be supported, all available devices are: \"%s\"\n",
289 ctx->options.device_type, all_dev_names);
293 // create infer_request for sync execution
294 status = ie_exec_network_create_infer_request(ov_model->exe_network, &ov_model->infer_request);
298 // create infer_requests for async execution
299 if (ctx->options.nireq <= 0) {
300 // the default value is a rough estimation
301 ctx->options.nireq = av_cpu_count() / 2 + 1;
304 ov_model->request_queue = ff_safe_queue_create();
305 if (!ov_model->request_queue) {
309 for (int i = 0; i < ctx->options.nireq; i++) {
310 RequestItem *item = av_mallocz(sizeof(*item));
315 status = ie_exec_network_create_infer_request(ov_model->exe_network, &item->infer_request);
321 item->tasks = av_malloc_array(ctx->options.batch_size, sizeof(*item->tasks));
326 item->task_count = 0;
328 item->callback.completeCallBackFunc = infer_completion_callback;
329 item->callback.args = item;
330 if (ff_safe_queue_push_back(ov_model->request_queue, item) < 0) {
336 ov_model->task_queue = ff_queue_create();
337 if (!ov_model->task_queue) {
344 ff_dnn_free_model_ov(&ov_model->model);
348 static DNNReturnType execute_model_ov(RequestItem *request)
352 TaskItem *task = request->tasks[0];
353 OVContext *ctx = &task->ov_model->ctx;
356 if (request->task_count < ctx->options.batch_size) {
357 if (ff_safe_queue_push_front(task->ov_model->request_queue, request) < 0) {
358 av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
363 ret = fill_model_input_ov(task->ov_model, request);
364 if (ret != DNN_SUCCESS) {
367 status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
369 av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
372 status = ie_infer_request_infer_async(request->infer_request);
374 av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
379 ret = fill_model_input_ov(task->ov_model, request);
380 if (ret != DNN_SUCCESS) {
383 status = ie_infer_request_infer(request->infer_request);
385 av_log(ctx, AV_LOG_ERROR, "Failed to start synchronous model inference\n");
388 infer_completion_callback(request);
389 return task->done ? DNN_SUCCESS : DNN_ERROR;
393 static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input_name)
395 OVModel *ov_model = (OVModel *)model;
396 OVContext *ctx = &ov_model->ctx;
397 char *model_input_name = NULL;
398 char *all_input_names = NULL;
400 size_t model_input_count = 0;
402 precision_e precision;
404 status = ie_network_get_inputs_number(ov_model->network, &model_input_count);
406 av_log(ctx, AV_LOG_ERROR, "Failed to get input count\n");
410 for (size_t i = 0; i < model_input_count; i++) {
411 status = ie_network_get_input_name(ov_model->network, i, &model_input_name);
413 av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's name\n", (int)i);
416 if (strcmp(model_input_name, input_name) == 0) {
417 ie_network_name_free(&model_input_name);
418 status |= ie_network_get_input_dims(ov_model->network, input_name, &dims);
419 status |= ie_network_get_input_precision(ov_model->network, input_name, &precision);
421 av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's dims or precision\n", (int)i);
425 input->channels = dims.dims[1];
426 input->height = dims.dims[2];
427 input->width = dims.dims[3];
428 input->dt = precision_to_datatype(precision);
431 //incorrect input name
432 APPEND_STRING(all_input_names, model_input_name)
435 ie_network_name_free(&model_input_name);
438 av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model, all input(s) are: \"%s\"\n", input_name, all_input_names);
442 static DNNReturnType get_output_ov(void *model, const char *input_name, int input_width, int input_height,
443 const char *output_name, int *output_width, int *output_height)
446 OVModel *ov_model = (OVModel *)model;
447 OVContext *ctx = &ov_model->ctx;
450 AVFrame *in_frame = av_frame_alloc();
451 AVFrame *out_frame = NULL;
452 TaskItem *ptask = &task;
455 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
458 out_frame = av_frame_alloc();
460 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
461 av_frame_free(&in_frame);
464 in_frame->width = input_width;
465 in_frame->height = input_height;
467 if (!ov_model->exe_network) {
468 if (init_model_ov(ov_model) != DNN_SUCCESS) {
469 av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
477 task.input_name = input_name;
478 task.in_frame = in_frame;
479 task.output_name = output_name;
480 task.out_frame = out_frame;
481 task.ov_model = ov_model;
483 request.infer_request = ov_model->infer_request;
484 request.task_count = 1;
485 request.tasks = &ptask;
487 ret = execute_model_ov(&request);
488 *output_width = out_frame->width;
489 *output_height = out_frame->height;
491 av_frame_free(&out_frame);
492 av_frame_free(&in_frame);
496 DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options, AVFilterContext *filter_ctx)
498 DNNModel *model = NULL;
499 OVModel *ov_model = NULL;
500 OVContext *ctx = NULL;
503 model = av_mallocz(sizeof(DNNModel));
508 ov_model = av_mallocz(sizeof(OVModel));
513 model->model = (void *)ov_model;
514 ov_model->model = model;
515 ov_model->ctx.class = &dnn_openvino_class;
516 ctx = &ov_model->ctx;
519 av_opt_set_defaults(ctx);
520 if (av_opt_set_from_string(ctx, options, NULL, "=", "&") < 0) {
521 av_log(ctx, AV_LOG_ERROR, "Failed to parse options \"%s\"\n", options);
525 status = ie_core_create("", &ov_model->core);
529 status = ie_core_read_network(ov_model->core, model_filename, NULL, &ov_model->network);
533 model->get_input = &get_input_ov;
534 model->get_output = &get_output_ov;
535 model->options = options;
536 model->filter_ctx = filter_ctx;
541 ff_dnn_free_model_ov(&model);
545 DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
546 const char **output_names, uint32_t nb_output, AVFrame *out_frame)
548 OVModel *ov_model = (OVModel *)model->model;
549 OVContext *ctx = &ov_model->ctx;
552 TaskItem *ptask = &task;
555 av_log(ctx, AV_LOG_ERROR, "in frame is NULL when execute model.\n");
560 av_log(ctx, AV_LOG_ERROR, "out frame is NULL when execute model.\n");
564 if (nb_output != 1) {
565 // currently, the filter does not need multiple outputs,
566 // so we just pending the support until we really need it.
567 av_log(ctx, AV_LOG_ERROR, "do not support multiple outputs\n");
571 if (ctx->options.batch_size > 1) {
572 av_log(ctx, AV_LOG_ERROR, "do not support batch mode for sync execution.\n");
576 if (!ov_model->exe_network) {
577 if (init_model_ov(ov_model) != DNN_SUCCESS) {
578 av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
586 task.input_name = input_name;
587 task.in_frame = in_frame;
588 task.output_name = output_names[0];
589 task.out_frame = out_frame;
590 task.ov_model = ov_model;
592 request.infer_request = ov_model->infer_request;
593 request.task_count = 1;
594 request.tasks = &ptask;
596 return execute_model_ov(&request);
599 DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
600 const char **output_names, uint32_t nb_output, AVFrame *out_frame)
602 OVModel *ov_model = (OVModel *)model->model;
603 OVContext *ctx = &ov_model->ctx;
604 RequestItem *request;
608 av_log(ctx, AV_LOG_ERROR, "in frame is NULL when async execute model.\n");
613 av_log(ctx, AV_LOG_ERROR, "out frame is NULL when async execute model.\n");
617 task = av_malloc(sizeof(*task));
619 av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");
623 if (!ov_model->exe_network) {
624 if (init_model_ov(ov_model) != DNN_SUCCESS) {
625 av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
633 task->input_name = input_name;
634 task->in_frame = in_frame;
635 task->output_name = output_names[0];
636 task->out_frame = out_frame;
637 task->ov_model = ov_model;
638 if (ff_queue_push_back(ov_model->task_queue, task) < 0) {
640 av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n");
644 request = ff_safe_queue_pop_front(ov_model->request_queue);
646 av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
650 request->tasks[request->task_count++] = task;
651 return execute_model_ov(request);
654 DNNAsyncStatusType ff_dnn_get_async_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
656 OVModel *ov_model = (OVModel *)model->model;
657 TaskItem *task = ff_queue_peek_front(ov_model->task_queue);
660 return DAST_EMPTY_QUEUE;
664 return DAST_NOT_READY;
667 *in = task->in_frame;
668 *out = task->out_frame;
669 ff_queue_pop_front(ov_model->task_queue);
675 DNNReturnType ff_dnn_flush_ov(const DNNModel *model)
677 OVModel *ov_model = (OVModel *)model->model;
678 OVContext *ctx = &ov_model->ctx;
679 RequestItem *request;
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 if (request->task_count == 0) {
690 // no pending task need to flush
691 if (ff_safe_queue_push_back(ov_model->request_queue, request) < 0) {
692 av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
698 ret = fill_model_input_ov(ov_model, request);
699 if (ret != DNN_SUCCESS) {
700 av_log(ctx, AV_LOG_ERROR, "Failed to fill model input.\n");
703 status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
705 av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
708 status = ie_infer_request_infer_async(request->infer_request);
710 av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
717 void ff_dnn_free_model_ov(DNNModel **model)
720 OVModel *ov_model = (OVModel *)(*model)->model;
721 while (ff_safe_queue_size(ov_model->request_queue) != 0) {
722 RequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue);
723 if (item && item->infer_request) {
724 ie_infer_request_free(&item->infer_request);
726 av_freep(&item->tasks);
729 ff_safe_queue_destroy(ov_model->request_queue);
731 while (ff_queue_size(ov_model->task_queue) != 0) {
732 TaskItem *item = ff_queue_pop_front(ov_model->task_queue);
733 av_frame_free(&item->in_frame);
734 av_frame_free(&item->out_frame);
737 ff_queue_destroy(ov_model->task_queue);
739 if (ov_model->infer_request)
740 ie_infer_request_free(&ov_model->infer_request);
741 if (ov_model->exe_network)
742 ie_exec_network_free(&ov_model->exe_network);
743 if (ov_model->network)
744 ie_network_free(&ov_model->network);
746 ie_core_free(&ov_model->core);