]> git.sesse.net Git - ffmpeg/blob - libavfilter/dnn/dnn_backend_openvino.c
dnn_backend_openvino.c: add version mismatch reminder
[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     FFSafeQueue *request_queue;   // holds RequestItem
59     FFQueue *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                 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     ie_blob_t *output_blob = NULL;
182     ie_blob_buffer_t blob_buffer;
183     DNNData output;
184     OVContext *ctx = &task->ov_model->ctx;
185
186     status = ie_infer_request_get_blob(request->infer_request, task->output_name, &output_blob);
187     if (status != OK) {
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)
197         }
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);
201         return;
202     }
203
204     status = ie_blob_get_buffer(output_blob, &blob_buffer);
205     if (status != OK) {
206         av_log(ctx, AV_LOG_ERROR, "Failed to access output memory\n");
207         return;
208     }
209
210     status |= ie_blob_get_dims(output_blob, &dims);
211     status |= ie_blob_get_precision(output_blob, &precision);
212     if (status != OK) {
213         av_log(ctx, AV_LOG_ERROR, "Failed to get dims or precision of output\n");
214         return;
215     }
216
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;
222
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);
230             } else {
231                 proc_from_dnn_to_frame(task->out_frame, &output, ctx);
232             }
233         } else {
234             task->out_frame->width = output.width;
235             task->out_frame->height = output.height;
236         }
237         task->done = 1;
238         output.data = (uint8_t *)output.data
239                       + output.width * output.height * output.channels * get_datatype_size(output.dt);
240     }
241     ie_blob_free(&output_blob);
242
243     request->task_count = 0;
244
245     if (task->async) {
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");
248             return;
249         }
250     }
251 }
252
253 static DNNReturnType init_model_ov(OVModel *ov_model)
254 {
255     OVContext *ctx = &ov_model->ctx;
256     IEStatusCode status;
257     ie_available_devices_t a_dev;
258     ie_config_t config = {NULL, NULL, NULL};
259     char *all_dev_names = NULL;
260
261     // batch size
262     if (ctx->options.batch_size <= 0) {
263         ctx->options.batch_size = 1;
264     }
265
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);
269         if (status != OK)
270             goto err;
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);
275         if (status != OK)
276             goto err;
277     }
278
279     status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
280     if (status != OK) {
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);
283         if (status != OK) {
284             av_log(ctx, AV_LOG_ERROR, "Failed to get available devices\n");
285             goto err;
286         }
287         for (int i = 0; i < a_dev.num_devices; i++) {
288             APPEND_STRING(all_dev_names, a_dev.devices[i])
289         }
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);
292         goto err;
293     }
294
295     // create infer_request for sync execution
296     status = ie_exec_network_create_infer_request(ov_model->exe_network, &ov_model->infer_request);
297     if (status != OK)
298         goto err;
299
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;
304     }
305
306     ov_model->request_queue = ff_safe_queue_create();
307     if (!ov_model->request_queue) {
308         goto err;
309     }
310
311     for (int i = 0; i < ctx->options.nireq; i++) {
312         RequestItem *item = av_mallocz(sizeof(*item));
313         if (!item) {
314             goto err;
315         }
316
317         status = ie_exec_network_create_infer_request(ov_model->exe_network, &item->infer_request);
318         if (status != OK) {
319             av_freep(&item);
320             goto err;
321         }
322
323         item->tasks = av_malloc_array(ctx->options.batch_size, sizeof(*item->tasks));
324         if (!item->tasks) {
325             av_freep(&item);
326             goto err;
327         }
328         item->task_count = 0;
329
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) {
333             av_freep(&item);
334             goto err;
335         }
336     }
337
338     ov_model->task_queue = ff_queue_create();
339     if (!ov_model->task_queue) {
340         goto err;
341     }
342
343     return DNN_SUCCESS;
344
345 err:
346     ff_dnn_free_model_ov(&ov_model->model);
347     return DNN_ERROR;
348 }
349
350 static DNNReturnType execute_model_ov(RequestItem *request)
351 {
352     IEStatusCode status;
353     DNNReturnType ret;
354     TaskItem *task = request->tasks[0];
355     OVContext *ctx = &task->ov_model->ctx;
356
357     if (task->async) {
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");
361                 return DNN_ERROR;
362             }
363             return DNN_SUCCESS;
364         }
365         ret = fill_model_input_ov(task->ov_model, request);
366         if (ret != DNN_SUCCESS) {
367             return ret;
368         }
369         status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
370         if (status != OK) {
371             av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
372             return DNN_ERROR;
373         }
374         status = ie_infer_request_infer_async(request->infer_request);
375         if (status != OK) {
376             av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
377             return DNN_ERROR;
378         }
379         return DNN_SUCCESS;
380     } else {
381         ret = fill_model_input_ov(task->ov_model, request);
382         if (ret != DNN_SUCCESS) {
383             return ret;
384         }
385         status = ie_infer_request_infer(request->infer_request);
386         if (status != OK) {
387             av_log(ctx, AV_LOG_ERROR, "Failed to start synchronous model inference\n");
388             return DNN_ERROR;
389         }
390         infer_completion_callback(request);
391         return task->done ? DNN_SUCCESS : DNN_ERROR;
392     }
393 }
394
395 static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input_name)
396 {
397     OVModel *ov_model = (OVModel *)model;
398     OVContext *ctx = &ov_model->ctx;
399     char *model_input_name = NULL;
400     char *all_input_names = NULL;
401     IEStatusCode status;
402     size_t model_input_count = 0;
403     dimensions_t dims;
404     precision_e precision;
405     int input_resizable = ctx->options.input_resizable;
406
407     status = ie_network_get_inputs_number(ov_model->network, &model_input_count);
408     if (status != OK) {
409         av_log(ctx, AV_LOG_ERROR, "Failed to get input count\n");
410         return DNN_ERROR;
411     }
412
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);
415         if (status != OK) {
416             av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's name\n", (int)i);
417             return DNN_ERROR;
418         }
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);
423             if (status != OK) {
424                 av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's dims or precision\n", (int)i);
425                 return DNN_ERROR;
426             }
427
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);
432             return DNN_SUCCESS;
433         } else {
434             //incorrect input name
435             APPEND_STRING(all_input_names, model_input_name)
436         }
437
438         ie_network_name_free(&model_input_name);
439     }
440
441     av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model, all input(s) are: \"%s\"\n", input_name, all_input_names);
442     return DNN_ERROR;
443 }
444
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)
447 {
448     DNNReturnType ret;
449     OVModel *ov_model = (OVModel *)model;
450     OVContext *ctx = &ov_model->ctx;
451     TaskItem task;
452     RequestItem request;
453     AVFrame *in_frame = av_frame_alloc();
454     AVFrame *out_frame = NULL;
455     TaskItem *ptask = &task;
456     IEStatusCode status;
457     input_shapes_t input_shapes;
458
459     if (!in_frame) {
460         av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
461         return DNN_ERROR;
462     }
463     out_frame = av_frame_alloc();
464     if (!out_frame) {
465         av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
466         av_frame_free(&in_frame);
467         return DNN_ERROR;
468     }
469     in_frame->width = input_width;
470     in_frame->height = input_height;
471
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);
478         if (status != OK) {
479             av_log(ctx, AV_LOG_ERROR, "Failed to reshape input size for %s\n", input_name);
480             return DNN_ERROR;
481         }
482     }
483
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");
487             return DNN_ERROR;
488         };
489     }
490
491     task.done = 0;
492     task.do_ioproc = 0;
493     task.async = 0;
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;
499
500     request.infer_request = ov_model->infer_request;
501     request.task_count = 1;
502     request.tasks = &ptask;
503
504     ret = execute_model_ov(&request);
505     *output_width = out_frame->width;
506     *output_height = out_frame->height;
507
508     av_frame_free(&out_frame);
509     av_frame_free(&in_frame);
510     return ret;
511 }
512
513 DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options, AVFilterContext *filter_ctx)
514 {
515     DNNModel *model = NULL;
516     OVModel *ov_model = NULL;
517     OVContext *ctx = NULL;
518     IEStatusCode status;
519
520     model = av_mallocz(sizeof(DNNModel));
521     if (!model){
522         return NULL;
523     }
524
525     ov_model = av_mallocz(sizeof(OVModel));
526     if (!ov_model) {
527         av_freep(&model);
528         return NULL;
529     }
530     model->model = (void *)ov_model;
531     ov_model->model = model;
532     ov_model->ctx.class = &dnn_openvino_class;
533     ctx = &ov_model->ctx;
534
535     //parse options
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);
539         goto err;
540     }
541
542     status = ie_core_create("", &ov_model->core);
543     if (status != OK)
544         goto err;
545
546     status = ie_core_read_network(ov_model->core, model_filename, NULL, &ov_model->network);
547     if (status != OK) {
548         ie_version_t ver;
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);
554         goto err;
555     }
556
557     model->get_input = &get_input_ov;
558     model->get_output = &get_output_ov;
559     model->options = options;
560     model->filter_ctx = filter_ctx;
561
562     return model;
563
564 err:
565     ff_dnn_free_model_ov(&model);
566     return NULL;
567 }
568
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)
571 {
572     OVModel *ov_model = (OVModel *)model->model;
573     OVContext *ctx = &ov_model->ctx;
574     TaskItem task;
575     RequestItem request;
576     TaskItem *ptask = &task;
577
578     if (!in_frame) {
579         av_log(ctx, AV_LOG_ERROR, "in frame is NULL when execute model.\n");
580         return DNN_ERROR;
581     }
582
583     if (!out_frame) {
584         av_log(ctx, AV_LOG_ERROR, "out frame is NULL when execute model.\n");
585         return DNN_ERROR;
586     }
587
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");
592         return DNN_ERROR;
593     }
594
595     if (ctx->options.batch_size > 1) {
596         av_log(ctx, AV_LOG_ERROR, "do not support batch mode for sync execution.\n");
597         return DNN_ERROR;
598     }
599
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");
603             return DNN_ERROR;
604         };
605     }
606
607     task.done = 0;
608     task.do_ioproc = 1;
609     task.async = 0;
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;
615
616     request.infer_request = ov_model->infer_request;
617     request.task_count = 1;
618     request.tasks = &ptask;
619
620     return execute_model_ov(&request);
621 }
622
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)
625 {
626     OVModel *ov_model = (OVModel *)model->model;
627     OVContext *ctx = &ov_model->ctx;
628     RequestItem *request;
629     TaskItem *task;
630
631     if (!in_frame) {
632         av_log(ctx, AV_LOG_ERROR, "in frame is NULL when async execute model.\n");
633         return DNN_ERROR;
634     }
635
636     if (!out_frame) {
637         av_log(ctx, AV_LOG_ERROR, "out frame is NULL when async execute model.\n");
638         return DNN_ERROR;
639     }
640
641     task = av_malloc(sizeof(*task));
642     if (!task) {
643         av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");
644         return DNN_ERROR;
645     }
646
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");
650             return DNN_ERROR;
651         };
652     }
653
654     task->done = 0;
655     task->do_ioproc = 1;
656     task->async = 1;
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) {
663         av_freep(&task);
664         av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n");
665         return DNN_ERROR;
666     }
667
668     request = ff_safe_queue_pop_front(ov_model->request_queue);
669     if (!request) {
670         av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
671         return DNN_ERROR;
672     }
673
674     request->tasks[request->task_count++] = task;
675     return execute_model_ov(request);
676 }
677
678 DNNAsyncStatusType ff_dnn_get_async_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
679 {
680     OVModel *ov_model = (OVModel *)model->model;
681     TaskItem *task = ff_queue_peek_front(ov_model->task_queue);
682
683     if (!task) {
684         return DAST_EMPTY_QUEUE;
685     }
686
687     if (!task->done) {
688         return DAST_NOT_READY;
689     }
690
691     *in = task->in_frame;
692     *out = task->out_frame;
693     ff_queue_pop_front(ov_model->task_queue);
694     av_freep(&task);
695
696     return DAST_SUCCESS;
697 }
698
699 DNNReturnType ff_dnn_flush_ov(const DNNModel *model)
700 {
701     OVModel *ov_model = (OVModel *)model->model;
702     OVContext *ctx = &ov_model->ctx;
703     RequestItem *request;
704     IEStatusCode status;
705     DNNReturnType ret;
706
707     request = ff_safe_queue_pop_front(ov_model->request_queue);
708     if (!request) {
709         av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
710         return DNN_ERROR;
711     }
712
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");
717             return DNN_ERROR;
718         }
719         return DNN_SUCCESS;
720     }
721
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");
725         return ret;
726     }
727     status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
728     if (status != OK) {
729         av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
730         return DNN_ERROR;
731     }
732     status = ie_infer_request_infer_async(request->infer_request);
733     if (status != OK) {
734         av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
735         return DNN_ERROR;
736     }
737
738     return DNN_SUCCESS;
739 }
740
741 void ff_dnn_free_model_ov(DNNModel **model)
742 {
743     if (*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);
749             }
750             av_freep(&item->tasks);
751             av_freep(&item);
752         }
753         ff_safe_queue_destroy(ov_model->request_queue);
754
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);
759             av_freep(&item);
760         }
761         ff_queue_destroy(ov_model->task_queue);
762
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);
769         if (ov_model->core)
770             ie_core_free(&ov_model->core);
771         av_freep(&ov_model);
772         av_freep(model);
773     }
774 }