]> git.sesse.net Git - ffmpeg/blob - libavfilter/dnn/dnn_backend_openvino.c
ed41b721fc54fd3be0d2d18218da695843a584ad
[ffmpeg] / libavfilter / dnn / dnn_backend_openvino.c
1 /*
2  * Copyright (c) 2020
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
23  * DNN OpenVINO backend implementation.
24  */
25
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"
33 #include "queue.h"
34 #include "safe_queue.h"
35 #include <c_api/ie_c_api.h>
36
37 typedef struct OVOptions{
38     char *device_type;
39     int nireq;
40     int batch_size;
41     int input_resizable;
42 } OVOptions;
43
44 typedef struct OVContext {
45     const AVClass *class;
46     OVOptions options;
47 } OVContext;
48
49 typedef struct OVModel{
50     OVContext ctx;
51     DNNModel *model;
52     ie_core_t *core;
53     ie_network_t *network;
54     ie_executable_network_t *exe_network;
55     ie_infer_request_t *infer_request;
56
57     /* for async execution */
58     SafeQueue *request_queue;   // holds RequestItem
59     Queue *task_queue;          // holds TaskItem
60 } OVModel;
61
62 typedef struct TaskItem {
63     OVModel *ov_model;
64     const char *input_name;
65     AVFrame *in_frame;
66     const char *output_name;
67     AVFrame *out_frame;
68     int do_ioproc;
69     int async;
70     int done;
71 } TaskItem;
72
73 typedef struct RequestItem {
74     ie_infer_request_t *infer_request;
75     TaskItem **tasks;
76     int task_count;
77     ie_complete_call_back_t callback;
78 } RequestItem;
79
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);
83
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 },
91     { NULL }
92 };
93
94 AVFILTER_DEFINE_CLASS(dnn_openvino);
95
96 static DNNDataType precision_to_datatype(precision_e precision)
97 {
98     switch (precision)
99     {
100     case FP32:
101         return DNN_FLOAT;
102     default:
103         av_assert0(!"not supported yet.");
104         return DNN_FLOAT;
105     }
106 }
107
108 static int get_datatype_size(DNNDataType dt)
109 {
110     switch (dt)
111     {
112     case DNN_FLOAT:
113         return sizeof(float);
114     default:
115         av_assert0(!"not supported yet.");
116         return 1;
117     }
118 }
119
120 static DNNReturnType fill_model_input_ov(OVModel *ov_model, RequestItem *request)
121 {
122     dimensions_t dims;
123     precision_e precision;
124     ie_blob_buffer_t blob_buffer;
125     OVContext *ctx = &ov_model->ctx;
126     IEStatusCode status;
127     DNNData input;
128     ie_blob_t *input_blob = NULL;
129     TaskItem *task = request->tasks[0];
130
131     status = ie_infer_request_get_blob(request->infer_request, task->input_name, &input_blob);
132     if (status != OK) {
133         av_log(ctx, AV_LOG_ERROR, "Failed to get input blob with name %s\n", task->input_name);
134         return DNN_ERROR;
135     }
136
137     status |= ie_blob_get_dims(input_blob, &dims);
138     status |= ie_blob_get_precision(input_blob, &precision);
139     if (status != OK) {
140         av_log(ctx, AV_LOG_ERROR, "Failed to get input blob dims/precision\n");
141         return DNN_ERROR;
142     }
143
144     status = ie_blob_get_buffer(input_blob, &blob_buffer);
145     if (status != OK) {
146         av_log(ctx, AV_LOG_ERROR, "Failed to get input blob buffer\n");
147         return DNN_ERROR;
148     }
149
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);
155
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);
162             } else {
163                 ff_proc_from_frame_to_dnn(task->in_frame, &input, ctx);
164             }
165         }
166         input.data = (uint8_t *)input.data
167                      + input.width * input.height * input.channels * get_datatype_size(input.dt);
168     }
169     ie_blob_free(&input_blob);
170
171     return DNN_SUCCESS;
172 }
173
174 static void infer_completion_callback(void *args)
175 {
176     dimensions_t dims;
177     precision_e precision;
178     IEStatusCode status;
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;
184     DNNData output;
185     OVContext *ctx = &task->ov_model->ctx;
186
187     status = ie_infer_request_get_blob(request->infer_request, task->output_name, &output_blob);
188     if (status != OK) {
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)
198         }
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);
202         return;
203     }
204
205     status = ie_blob_get_buffer(output_blob, &blob_buffer);
206     if (status != OK) {
207         av_log(ctx, AV_LOG_ERROR, "Failed to access output memory\n");
208         return;
209     }
210
211     status |= ie_blob_get_dims(output_blob, &dims);
212     status |= ie_blob_get_precision(output_blob, &precision);
213     if (status != OK) {
214         av_log(ctx, AV_LOG_ERROR, "Failed to get dims or precision of output\n");
215         return;
216     }
217
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;
223
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);
231             } else {
232                 ff_proc_from_dnn_to_frame(task->out_frame, &output, ctx);
233             }
234         } else {
235             task->out_frame->width = output.width;
236             task->out_frame->height = output.height;
237         }
238         task->done = 1;
239         output.data = (uint8_t *)output.data
240                       + output.width * output.height * output.channels * get_datatype_size(output.dt);
241     }
242     ie_blob_free(&output_blob);
243
244     request->task_count = 0;
245
246     if (task->async) {
247         if (ff_safe_queue_push_back(requestq, request) < 0) {
248             av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
249             return;
250         }
251     }
252 }
253
254 static DNNReturnType init_model_ov(OVModel *ov_model, const char *input_name, const char *output_name)
255 {
256     OVContext *ctx = &ov_model->ctx;
257     IEStatusCode status;
258     ie_available_devices_t a_dev;
259     ie_config_t config = {NULL, NULL, NULL};
260     char *all_dev_names = NULL;
261
262     // batch size
263     if (ctx->options.batch_size <= 0) {
264         ctx->options.batch_size = 1;
265     }
266
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);
270         if (status != OK)
271             goto err;
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);
276         if (status != OK)
277             goto err;
278     }
279
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);
283     if (status != OK) {
284         av_log(ctx, AV_LOG_ERROR, "Failed to set layout as NHWC for input %s\n", input_name);
285         goto err;
286     }
287     status = ie_network_set_output_layout(ov_model->network, output_name, NHWC);
288     if (status != OK) {
289         av_log(ctx, AV_LOG_ERROR, "Failed to set layout as NHWC for output %s\n", output_name);
290         goto err;
291     }
292
293     status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
294     if (status != OK) {
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);
297         if (status != OK) {
298             av_log(ctx, AV_LOG_ERROR, "Failed to get available devices\n");
299             goto err;
300         }
301         for (int i = 0; i < a_dev.num_devices; i++) {
302             APPEND_STRING(all_dev_names, a_dev.devices[i])
303         }
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);
306         goto err;
307     }
308
309     // create infer_request for sync execution
310     status = ie_exec_network_create_infer_request(ov_model->exe_network, &ov_model->infer_request);
311     if (status != OK)
312         goto err;
313
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;
318     }
319
320     ov_model->request_queue = ff_safe_queue_create();
321     if (!ov_model->request_queue) {
322         goto err;
323     }
324
325     for (int i = 0; i < ctx->options.nireq; i++) {
326         RequestItem *item = av_mallocz(sizeof(*item));
327         if (!item) {
328             goto err;
329         }
330
331         status = ie_exec_network_create_infer_request(ov_model->exe_network, &item->infer_request);
332         if (status != OK) {
333             av_freep(&item);
334             goto err;
335         }
336
337         item->tasks = av_malloc_array(ctx->options.batch_size, sizeof(*item->tasks));
338         if (!item->tasks) {
339             av_freep(&item);
340             goto err;
341         }
342         item->task_count = 0;
343
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) {
347             av_freep(&item);
348             goto err;
349         }
350     }
351
352     ov_model->task_queue = ff_queue_create();
353     if (!ov_model->task_queue) {
354         goto err;
355     }
356
357     return DNN_SUCCESS;
358
359 err:
360     ff_dnn_free_model_ov(&ov_model->model);
361     return DNN_ERROR;
362 }
363
364 static DNNReturnType execute_model_ov(RequestItem *request)
365 {
366     IEStatusCode status;
367     DNNReturnType ret;
368     TaskItem *task = request->tasks[0];
369     OVContext *ctx = &task->ov_model->ctx;
370
371     if (task->async) {
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");
375                 return DNN_ERROR;
376             }
377             return DNN_SUCCESS;
378         }
379         ret = fill_model_input_ov(task->ov_model, request);
380         if (ret != DNN_SUCCESS) {
381             return ret;
382         }
383         status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
384         if (status != OK) {
385             av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
386             return DNN_ERROR;
387         }
388         status = ie_infer_request_infer_async(request->infer_request);
389         if (status != OK) {
390             av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
391             return DNN_ERROR;
392         }
393         return DNN_SUCCESS;
394     } else {
395         ret = fill_model_input_ov(task->ov_model, request);
396         if (ret != DNN_SUCCESS) {
397             return ret;
398         }
399         status = ie_infer_request_infer(request->infer_request);
400         if (status != OK) {
401             av_log(ctx, AV_LOG_ERROR, "Failed to start synchronous model inference\n");
402             return DNN_ERROR;
403         }
404         infer_completion_callback(request);
405         return task->done ? DNN_SUCCESS : DNN_ERROR;
406     }
407 }
408
409 static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input_name)
410 {
411     OVModel *ov_model = model;
412     OVContext *ctx = &ov_model->ctx;
413     char *model_input_name = NULL;
414     char *all_input_names = NULL;
415     IEStatusCode status;
416     size_t model_input_count = 0;
417     dimensions_t dims;
418     precision_e precision;
419     int input_resizable = ctx->options.input_resizable;
420
421     status = ie_network_get_inputs_number(ov_model->network, &model_input_count);
422     if (status != OK) {
423         av_log(ctx, AV_LOG_ERROR, "Failed to get input count\n");
424         return DNN_ERROR;
425     }
426
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);
429         if (status != OK) {
430             av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's name\n", (int)i);
431             return DNN_ERROR;
432         }
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);
437             if (status != OK) {
438                 av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's dims or precision\n", (int)i);
439                 return DNN_ERROR;
440             }
441
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);
446             return DNN_SUCCESS;
447         } else {
448             //incorrect input name
449             APPEND_STRING(all_input_names, model_input_name)
450         }
451
452         ie_network_name_free(&model_input_name);
453     }
454
455     av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model, all input(s) are: \"%s\"\n", input_name, all_input_names);
456     return DNN_ERROR;
457 }
458
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)
461 {
462     DNNReturnType ret;
463     OVModel *ov_model = model;
464     OVContext *ctx = &ov_model->ctx;
465     TaskItem task;
466     RequestItem request;
467     AVFrame *in_frame = av_frame_alloc();
468     AVFrame *out_frame = NULL;
469     TaskItem *ptask = &task;
470     IEStatusCode status;
471     input_shapes_t input_shapes;
472
473     if (!in_frame) {
474         av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
475         return DNN_ERROR;
476     }
477     out_frame = av_frame_alloc();
478     if (!out_frame) {
479         av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
480         av_frame_free(&in_frame);
481         return DNN_ERROR;
482     }
483     in_frame->width = input_width;
484     in_frame->height = input_height;
485
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);
492         if (status != OK) {
493             av_log(ctx, AV_LOG_ERROR, "Failed to reshape input size for %s\n", input_name);
494             return DNN_ERROR;
495         }
496     }
497
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");
501             return DNN_ERROR;
502         }
503     }
504
505     task.done = 0;
506     task.do_ioproc = 0;
507     task.async = 0;
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;
513
514     request.infer_request = ov_model->infer_request;
515     request.task_count = 1;
516     request.tasks = &ptask;
517
518     ret = execute_model_ov(&request);
519     *output_width = out_frame->width;
520     *output_height = out_frame->height;
521
522     av_frame_free(&out_frame);
523     av_frame_free(&in_frame);
524     return ret;
525 }
526
527 DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options, AVFilterContext *filter_ctx)
528 {
529     DNNModel *model = NULL;
530     OVModel *ov_model = NULL;
531     OVContext *ctx = NULL;
532     IEStatusCode status;
533
534     model = av_mallocz(sizeof(DNNModel));
535     if (!model){
536         return NULL;
537     }
538
539     ov_model = av_mallocz(sizeof(OVModel));
540     if (!ov_model) {
541         av_freep(&model);
542         return NULL;
543     }
544     model->model = ov_model;
545     ov_model->model = model;
546     ov_model->ctx.class = &dnn_openvino_class;
547     ctx = &ov_model->ctx;
548
549     //parse options
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);
553         goto err;
554     }
555
556     status = ie_core_create("", &ov_model->core);
557     if (status != OK)
558         goto err;
559
560     status = ie_core_read_network(ov_model->core, model_filename, NULL, &ov_model->network);
561     if (status != OK) {
562         ie_version_t ver;
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);
568         goto err;
569     }
570
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
576     return model;
577
578 err:
579     ff_dnn_free_model_ov(&model);
580     return NULL;
581 }
582
583 DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
584                                       const char **output_names, uint32_t nb_output, AVFrame *out_frame)
585 {
586     OVModel *ov_model = model->model;
587     OVContext *ctx = &ov_model->ctx;
588     TaskItem task;
589     RequestItem request;
590     TaskItem *ptask = &task;
591
592     if (!in_frame) {
593         av_log(ctx, AV_LOG_ERROR, "in frame is NULL when execute model.\n");
594         return DNN_ERROR;
595     }
596
597     if (!out_frame) {
598         av_log(ctx, AV_LOG_ERROR, "out frame is NULL when execute model.\n");
599         return DNN_ERROR;
600     }
601
602     if (nb_output != 1) {
603         // currently, the filter does not need multiple outputs,
604         // so we just pending the support until we really need it.
605         avpriv_report_missing_feature(ctx, "multiple outputs");
606         return DNN_ERROR;
607     }
608
609     if (ctx->options.batch_size > 1) {
610         avpriv_report_missing_feature(ctx, "batch mode for sync execution");
611         return DNN_ERROR;
612     }
613
614     if (!ov_model->exe_network) {
615         if (init_model_ov(ov_model, input_name, output_names[0]) != DNN_SUCCESS) {
616             av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
617             return DNN_ERROR;
618         }
619     }
620
621     task.done = 0;
622     task.do_ioproc = 1;
623     task.async = 0;
624     task.input_name = input_name;
625     task.in_frame = in_frame;
626     task.output_name = output_names[0];
627     task.out_frame = out_frame;
628     task.ov_model = ov_model;
629
630     request.infer_request = ov_model->infer_request;
631     request.task_count = 1;
632     request.tasks = &ptask;
633
634     return execute_model_ov(&request);
635 }
636
637 DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
638                                             const char **output_names, uint32_t nb_output, AVFrame *out_frame)
639 {
640     OVModel *ov_model = model->model;
641     OVContext *ctx = &ov_model->ctx;
642     RequestItem *request;
643     TaskItem *task;
644
645     if (!in_frame) {
646         av_log(ctx, AV_LOG_ERROR, "in frame is NULL when async execute model.\n");
647         return DNN_ERROR;
648     }
649
650     if (!out_frame) {
651         av_log(ctx, AV_LOG_ERROR, "out frame is NULL when async execute model.\n");
652         return DNN_ERROR;
653     }
654
655     task = av_malloc(sizeof(*task));
656     if (!task) {
657         av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");
658         return DNN_ERROR;
659     }
660
661     if (!ov_model->exe_network) {
662         if (init_model_ov(ov_model, input_name, output_names[0]) != DNN_SUCCESS) {
663             av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
664             return DNN_ERROR;
665         }
666     }
667
668     task->done = 0;
669     task->do_ioproc = 1;
670     task->async = 1;
671     task->input_name = input_name;
672     task->in_frame = in_frame;
673     task->output_name = output_names[0];
674     task->out_frame = out_frame;
675     task->ov_model = ov_model;
676     if (ff_queue_push_back(ov_model->task_queue, task) < 0) {
677         av_freep(&task);
678         av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n");
679         return DNN_ERROR;
680     }
681
682     request = ff_safe_queue_pop_front(ov_model->request_queue);
683     if (!request) {
684         av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
685         return DNN_ERROR;
686     }
687
688     request->tasks[request->task_count++] = task;
689     return execute_model_ov(request);
690 }
691
692 DNNAsyncStatusType ff_dnn_get_async_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
693 {
694     OVModel *ov_model = model->model;
695     TaskItem *task = ff_queue_peek_front(ov_model->task_queue);
696
697     if (!task) {
698         return DAST_EMPTY_QUEUE;
699     }
700
701     if (!task->done) {
702         return DAST_NOT_READY;
703     }
704
705     *in = task->in_frame;
706     *out = task->out_frame;
707     ff_queue_pop_front(ov_model->task_queue);
708     av_freep(&task);
709
710     return DAST_SUCCESS;
711 }
712
713 DNNReturnType ff_dnn_flush_ov(const DNNModel *model)
714 {
715     OVModel *ov_model = model->model;
716     OVContext *ctx = &ov_model->ctx;
717     RequestItem *request;
718     IEStatusCode status;
719     DNNReturnType ret;
720
721     request = ff_safe_queue_pop_front(ov_model->request_queue);
722     if (!request) {
723         av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
724         return DNN_ERROR;
725     }
726
727     if (request->task_count == 0) {
728         // no pending task need to flush
729         if (ff_safe_queue_push_back(ov_model->request_queue, request) < 0) {
730             av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
731             return DNN_ERROR;
732         }
733         return DNN_SUCCESS;
734     }
735
736     ret = fill_model_input_ov(ov_model, request);
737     if (ret != DNN_SUCCESS) {
738         av_log(ctx, AV_LOG_ERROR, "Failed to fill model input.\n");
739         return ret;
740     }
741     status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
742     if (status != OK) {
743         av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
744         return DNN_ERROR;
745     }
746     status = ie_infer_request_infer_async(request->infer_request);
747     if (status != OK) {
748         av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
749         return DNN_ERROR;
750     }
751
752     return DNN_SUCCESS;
753 }
754
755 void ff_dnn_free_model_ov(DNNModel **model)
756 {
757     if (*model){
758         OVModel *ov_model = (*model)->model;
759         while (ff_safe_queue_size(ov_model->request_queue) != 0) {
760             RequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue);
761             if (item && item->infer_request) {
762                 ie_infer_request_free(&item->infer_request);
763             }
764             av_freep(&item->tasks);
765             av_freep(&item);
766         }
767         ff_safe_queue_destroy(ov_model->request_queue);
768
769         while (ff_queue_size(ov_model->task_queue) != 0) {
770             TaskItem *item = ff_queue_pop_front(ov_model->task_queue);
771             av_frame_free(&item->in_frame);
772             av_frame_free(&item->out_frame);
773             av_freep(&item);
774         }
775         ff_queue_destroy(ov_model->task_queue);
776
777         if (ov_model->infer_request)
778             ie_infer_request_free(&ov_model->infer_request);
779         if (ov_model->exe_network)
780             ie_exec_network_free(&ov_model->exe_network);
781         if (ov_model->network)
782             ie_network_free(&ov_model->network);
783         if (ov_model->core)
784             ie_core_free(&ov_model->core);
785         av_freep(&ov_model);
786         av_freep(model);
787     }
788 }