]> git.sesse.net Git - ffmpeg/blob - libavfilter/dnn/dnn_backend_openvino.c
dnn: add function type for model
[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, DNNFunctionType func_type, 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     model->func_type = func_type;
576
577     return model;
578
579 err:
580     ff_dnn_free_model_ov(&model);
581     return NULL;
582 }
583
584 DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
585                                       const char **output_names, uint32_t nb_output, AVFrame *out_frame)
586 {
587     OVModel *ov_model = model->model;
588     OVContext *ctx = &ov_model->ctx;
589     TaskItem task;
590     RequestItem request;
591     TaskItem *ptask = &task;
592
593     if (!in_frame) {
594         av_log(ctx, AV_LOG_ERROR, "in frame is NULL when execute model.\n");
595         return DNN_ERROR;
596     }
597
598     if (!out_frame) {
599         av_log(ctx, AV_LOG_ERROR, "out frame is NULL when execute model.\n");
600         return DNN_ERROR;
601     }
602
603     if (nb_output != 1) {
604         // currently, the filter does not need multiple outputs,
605         // so we just pending the support until we really need it.
606         avpriv_report_missing_feature(ctx, "multiple outputs");
607         return DNN_ERROR;
608     }
609
610     if (ctx->options.batch_size > 1) {
611         avpriv_report_missing_feature(ctx, "batch mode for sync execution");
612         return DNN_ERROR;
613     }
614
615     if (!ov_model->exe_network) {
616         if (init_model_ov(ov_model, input_name, output_names[0]) != DNN_SUCCESS) {
617             av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
618             return DNN_ERROR;
619         }
620     }
621
622     task.done = 0;
623     task.do_ioproc = 1;
624     task.async = 0;
625     task.input_name = input_name;
626     task.in_frame = in_frame;
627     task.output_name = output_names[0];
628     task.out_frame = out_frame;
629     task.ov_model = ov_model;
630
631     request.infer_request = ov_model->infer_request;
632     request.task_count = 1;
633     request.tasks = &ptask;
634
635     return execute_model_ov(&request);
636 }
637
638 DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
639                                             const char **output_names, uint32_t nb_output, AVFrame *out_frame)
640 {
641     OVModel *ov_model = model->model;
642     OVContext *ctx = &ov_model->ctx;
643     RequestItem *request;
644     TaskItem *task;
645
646     if (!in_frame) {
647         av_log(ctx, AV_LOG_ERROR, "in frame is NULL when async execute model.\n");
648         return DNN_ERROR;
649     }
650
651     if (!out_frame) {
652         av_log(ctx, AV_LOG_ERROR, "out frame is NULL when async execute model.\n");
653         return DNN_ERROR;
654     }
655
656     task = av_malloc(sizeof(*task));
657     if (!task) {
658         av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");
659         return DNN_ERROR;
660     }
661
662     if (!ov_model->exe_network) {
663         if (init_model_ov(ov_model, input_name, output_names[0]) != DNN_SUCCESS) {
664             av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
665             return DNN_ERROR;
666         }
667     }
668
669     task->done = 0;
670     task->do_ioproc = 1;
671     task->async = 1;
672     task->input_name = input_name;
673     task->in_frame = in_frame;
674     task->output_name = output_names[0];
675     task->out_frame = out_frame;
676     task->ov_model = ov_model;
677     if (ff_queue_push_back(ov_model->task_queue, task) < 0) {
678         av_freep(&task);
679         av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n");
680         return DNN_ERROR;
681     }
682
683     request = ff_safe_queue_pop_front(ov_model->request_queue);
684     if (!request) {
685         av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
686         return DNN_ERROR;
687     }
688
689     request->tasks[request->task_count++] = task;
690     return execute_model_ov(request);
691 }
692
693 DNNAsyncStatusType ff_dnn_get_async_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
694 {
695     OVModel *ov_model = model->model;
696     TaskItem *task = ff_queue_peek_front(ov_model->task_queue);
697
698     if (!task) {
699         return DAST_EMPTY_QUEUE;
700     }
701
702     if (!task->done) {
703         return DAST_NOT_READY;
704     }
705
706     *in = task->in_frame;
707     *out = task->out_frame;
708     ff_queue_pop_front(ov_model->task_queue);
709     av_freep(&task);
710
711     return DAST_SUCCESS;
712 }
713
714 DNNReturnType ff_dnn_flush_ov(const DNNModel *model)
715 {
716     OVModel *ov_model = model->model;
717     OVContext *ctx = &ov_model->ctx;
718     RequestItem *request;
719     IEStatusCode status;
720     DNNReturnType ret;
721
722     request = ff_safe_queue_pop_front(ov_model->request_queue);
723     if (!request) {
724         av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
725         return DNN_ERROR;
726     }
727
728     if (request->task_count == 0) {
729         // no pending task need to flush
730         if (ff_safe_queue_push_back(ov_model->request_queue, request) < 0) {
731             av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
732             return DNN_ERROR;
733         }
734         return DNN_SUCCESS;
735     }
736
737     ret = fill_model_input_ov(ov_model, request);
738     if (ret != DNN_SUCCESS) {
739         av_log(ctx, AV_LOG_ERROR, "Failed to fill model input.\n");
740         return ret;
741     }
742     status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
743     if (status != OK) {
744         av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
745         return DNN_ERROR;
746     }
747     status = ie_infer_request_infer_async(request->infer_request);
748     if (status != OK) {
749         av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
750         return DNN_ERROR;
751     }
752
753     return DNN_SUCCESS;
754 }
755
756 void ff_dnn_free_model_ov(DNNModel **model)
757 {
758     if (*model){
759         OVModel *ov_model = (*model)->model;
760         while (ff_safe_queue_size(ov_model->request_queue) != 0) {
761             RequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue);
762             if (item && item->infer_request) {
763                 ie_infer_request_free(&item->infer_request);
764             }
765             av_freep(&item->tasks);
766             av_freep(&item);
767         }
768         ff_safe_queue_destroy(ov_model->request_queue);
769
770         while (ff_queue_size(ov_model->task_queue) != 0) {
771             TaskItem *item = ff_queue_pop_front(ov_model->task_queue);
772             av_frame_free(&item->in_frame);
773             av_frame_free(&item->out_frame);
774             av_freep(&item);
775         }
776         ff_queue_destroy(ov_model->task_queue);
777
778         if (ov_model->infer_request)
779             ie_infer_request_free(&ov_model->infer_request);
780         if (ov_model->exe_network)
781             ie_exec_network_free(&ov_model->exe_network);
782         if (ov_model->network)
783             ie_network_free(&ov_model->network);
784         if (ov_model->core)
785             ie_core_free(&ov_model->core);
786         av_freep(&ov_model);
787         av_freep(model);
788     }
789 }