]> git.sesse.net Git - ffmpeg/blob - libavfilter/dnn/dnn_backend_openvino.c
lavfi/dnn_backend_openvino.c: fix typo upon error
[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     case U8:
103         return DNN_UINT8;
104     default:
105         av_assert0(!"not supported yet.");
106         return DNN_FLOAT;
107     }
108 }
109
110 static int get_datatype_size(DNNDataType dt)
111 {
112     switch (dt)
113     {
114     case DNN_FLOAT:
115         return sizeof(float);
116     case DNN_UINT8:
117         return sizeof(uint8_t);
118     default:
119         av_assert0(!"not supported yet.");
120         return 1;
121     }
122 }
123
124 static DNNReturnType fill_model_input_ov(OVModel *ov_model, RequestItem *request)
125 {
126     dimensions_t dims;
127     precision_e precision;
128     ie_blob_buffer_t blob_buffer;
129     OVContext *ctx = &ov_model->ctx;
130     IEStatusCode status;
131     DNNData input;
132     ie_blob_t *input_blob = NULL;
133     TaskItem *task = request->tasks[0];
134
135     status = ie_infer_request_get_blob(request->infer_request, task->input_name, &input_blob);
136     if (status != OK) {
137         av_log(ctx, AV_LOG_ERROR, "Failed to get input blob with name %s\n", task->input_name);
138         return DNN_ERROR;
139     }
140
141     status |= ie_blob_get_dims(input_blob, &dims);
142     status |= ie_blob_get_precision(input_blob, &precision);
143     if (status != OK) {
144         ie_blob_free(&input_blob);
145         av_log(ctx, AV_LOG_ERROR, "Failed to get input blob dims/precision\n");
146         return DNN_ERROR;
147     }
148
149     status = ie_blob_get_buffer(input_blob, &blob_buffer);
150     if (status != OK) {
151         ie_blob_free(&input_blob);
152         av_log(ctx, AV_LOG_ERROR, "Failed to get input blob buffer\n");
153         return DNN_ERROR;
154     }
155
156     input.height = dims.dims[2];
157     input.width = dims.dims[3];
158     input.channels = dims.dims[1];
159     input.data = blob_buffer.buffer;
160     input.dt = precision_to_datatype(precision);
161     // all models in openvino open model zoo use BGR as input,
162     // change to be an option when necessary.
163     input.order = DCO_BGR;
164
165     av_assert0(request->task_count <= dims.dims[0]);
166     for (int i = 0; i < request->task_count; ++i) {
167         task = request->tasks[i];
168         if (task->do_ioproc) {
169             if (ov_model->model->pre_proc != NULL) {
170                 ov_model->model->pre_proc(task->in_frame, &input, ov_model->model->filter_ctx);
171             } else {
172                 ff_proc_from_frame_to_dnn(task->in_frame, &input, ov_model->model->func_type, ctx);
173             }
174         }
175         input.data = (uint8_t *)input.data
176                      + input.width * input.height * input.channels * get_datatype_size(input.dt);
177     }
178     ie_blob_free(&input_blob);
179
180     return DNN_SUCCESS;
181 }
182
183 static void infer_completion_callback(void *args)
184 {
185     dimensions_t dims;
186     precision_e precision;
187     IEStatusCode status;
188     RequestItem *request = args;
189     TaskItem *task = request->tasks[0];
190     SafeQueue *requestq = task->ov_model->request_queue;
191     ie_blob_t *output_blob = NULL;
192     ie_blob_buffer_t blob_buffer;
193     DNNData output;
194     OVContext *ctx = &task->ov_model->ctx;
195
196     status = ie_infer_request_get_blob(request->infer_request, task->output_name, &output_blob);
197     if (status != OK) {
198         //incorrect output name
199         char *model_output_name = NULL;
200         char *all_output_names = NULL;
201         size_t model_output_count = 0;
202         av_log(ctx, AV_LOG_ERROR, "Failed to get model output data\n");
203         status = ie_network_get_outputs_number(task->ov_model->network, &model_output_count);
204         for (size_t i = 0; i < model_output_count; i++) {
205             status = ie_network_get_output_name(task->ov_model->network, i, &model_output_name);
206             APPEND_STRING(all_output_names, model_output_name)
207         }
208         av_log(ctx, AV_LOG_ERROR,
209                "output \"%s\" may not correct, all output(s) are: \"%s\"\n",
210                task->output_name, all_output_names);
211         return;
212     }
213
214     status = ie_blob_get_buffer(output_blob, &blob_buffer);
215     if (status != OK) {
216         ie_blob_free(&output_blob);
217         av_log(ctx, AV_LOG_ERROR, "Failed to access output memory\n");
218         return;
219     }
220
221     status |= ie_blob_get_dims(output_blob, &dims);
222     status |= ie_blob_get_precision(output_blob, &precision);
223     if (status != OK) {
224         ie_blob_free(&output_blob);
225         av_log(ctx, AV_LOG_ERROR, "Failed to get dims or precision of output\n");
226         return;
227     }
228
229     output.channels = dims.dims[1];
230     output.height   = dims.dims[2];
231     output.width    = dims.dims[3];
232     output.dt       = precision_to_datatype(precision);
233     output.data     = blob_buffer.buffer;
234
235     av_assert0(request->task_count <= dims.dims[0]);
236     av_assert0(request->task_count >= 1);
237     for (int i = 0; i < request->task_count; ++i) {
238         task = request->tasks[i];
239         if (task->do_ioproc) {
240             if (task->ov_model->model->post_proc != NULL) {
241                 task->ov_model->model->post_proc(task->out_frame, &output, task->ov_model->model->filter_ctx);
242             } else {
243                 ff_proc_from_dnn_to_frame(task->out_frame, &output, ctx);
244             }
245         } else {
246             task->out_frame->width = output.width;
247             task->out_frame->height = output.height;
248         }
249         task->done = 1;
250         output.data = (uint8_t *)output.data
251                       + output.width * output.height * output.channels * get_datatype_size(output.dt);
252     }
253     ie_blob_free(&output_blob);
254
255     request->task_count = 0;
256
257     if (task->async) {
258         if (ff_safe_queue_push_back(requestq, request) < 0) {
259             av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
260             return;
261         }
262     }
263 }
264
265 static DNNReturnType init_model_ov(OVModel *ov_model, const char *input_name, const char *output_name)
266 {
267     OVContext *ctx = &ov_model->ctx;
268     IEStatusCode status;
269     ie_available_devices_t a_dev;
270     ie_config_t config = {NULL, NULL, NULL};
271     char *all_dev_names = NULL;
272
273     // batch size
274     if (ctx->options.batch_size <= 0) {
275         ctx->options.batch_size = 1;
276     }
277
278     if (ctx->options.batch_size > 1) {
279         input_shapes_t input_shapes;
280         status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
281         if (status != OK)
282             goto err;
283         for (int i = 0; i < input_shapes.shape_num; i++)
284             input_shapes.shapes[i].shape.dims[0] = ctx->options.batch_size;
285         status = ie_network_reshape(ov_model->network, input_shapes);
286         ie_network_input_shapes_free(&input_shapes);
287         if (status != OK)
288             goto err;
289     }
290
291     // The order of dims in the openvino is fixed and it is always NCHW for 4-D data.
292     // while we pass NHWC data from FFmpeg to openvino
293     status = ie_network_set_input_layout(ov_model->network, input_name, NHWC);
294     if (status != OK) {
295         av_log(ctx, AV_LOG_ERROR, "Failed to set layout as NHWC for input %s\n", input_name);
296         goto err;
297     }
298     status = ie_network_set_output_layout(ov_model->network, output_name, NHWC);
299     if (status != OK) {
300         av_log(ctx, AV_LOG_ERROR, "Failed to set layout as NHWC for output %s\n", output_name);
301         goto err;
302     }
303
304     // all models in openvino open model zoo use BGR with range [0.0f, 255.0f] as input,
305     // we don't have a AVPixelFormat to descibe it, so we'll use AV_PIX_FMT_BGR24 and
306     // ask openvino to do the conversion internally.
307     // the current supported SR model (frame processing) is generated from tensorflow model,
308     // and its input is Y channel as float with range [0.0f, 1.0f], so do not set for this case.
309     // TODO: we need to get a final clear&general solution with all backends/formats considered.
310     if (ov_model->model->func_type != DFT_PROCESS_FRAME) {
311         status = ie_network_set_input_precision(ov_model->network, input_name, U8);
312         if (status != OK) {
313             av_log(ctx, AV_LOG_ERROR, "Failed to set input precision as U8 for %s\n", input_name);
314             goto err;
315         }
316     }
317
318     status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
319     if (status != OK) {
320         av_log(ctx, AV_LOG_ERROR, "Failed to load OpenVINO model network\n");
321         status = ie_core_get_available_devices(ov_model->core, &a_dev);
322         if (status != OK) {
323             av_log(ctx, AV_LOG_ERROR, "Failed to get available devices\n");
324             goto err;
325         }
326         for (int i = 0; i < a_dev.num_devices; i++) {
327             APPEND_STRING(all_dev_names, a_dev.devices[i])
328         }
329         av_log(ctx, AV_LOG_ERROR,"device %s may not be supported, all available devices are: \"%s\"\n",
330                ctx->options.device_type, all_dev_names);
331         goto err;
332     }
333
334     // create infer_request for sync execution
335     status = ie_exec_network_create_infer_request(ov_model->exe_network, &ov_model->infer_request);
336     if (status != OK)
337         goto err;
338
339     // create infer_requests for async execution
340     if (ctx->options.nireq <= 0) {
341         // the default value is a rough estimation
342         ctx->options.nireq = av_cpu_count() / 2 + 1;
343     }
344
345     ov_model->request_queue = ff_safe_queue_create();
346     if (!ov_model->request_queue) {
347         goto err;
348     }
349
350     for (int i = 0; i < ctx->options.nireq; i++) {
351         RequestItem *item = av_mallocz(sizeof(*item));
352         if (!item) {
353             goto err;
354         }
355
356         status = ie_exec_network_create_infer_request(ov_model->exe_network, &item->infer_request);
357         if (status != OK) {
358             av_freep(&item);
359             goto err;
360         }
361
362         item->tasks = av_malloc_array(ctx->options.batch_size, sizeof(*item->tasks));
363         if (!item->tasks) {
364             av_freep(&item);
365             goto err;
366         }
367         item->task_count = 0;
368
369         item->callback.completeCallBackFunc = infer_completion_callback;
370         item->callback.args = item;
371         if (ff_safe_queue_push_back(ov_model->request_queue, item) < 0) {
372             av_freep(&item);
373             goto err;
374         }
375     }
376
377     ov_model->task_queue = ff_queue_create();
378     if (!ov_model->task_queue) {
379         goto err;
380     }
381
382     return DNN_SUCCESS;
383
384 err:
385     ff_dnn_free_model_ov(&ov_model->model);
386     return DNN_ERROR;
387 }
388
389 static DNNReturnType execute_model_ov(RequestItem *request)
390 {
391     IEStatusCode status;
392     DNNReturnType ret;
393     TaskItem *task = request->tasks[0];
394     OVContext *ctx = &task->ov_model->ctx;
395
396     if (task->async) {
397         if (request->task_count < ctx->options.batch_size) {
398             if (ff_safe_queue_push_front(task->ov_model->request_queue, request) < 0) {
399                 av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
400                 return DNN_ERROR;
401             }
402             return DNN_SUCCESS;
403         }
404         ret = fill_model_input_ov(task->ov_model, request);
405         if (ret != DNN_SUCCESS) {
406             return ret;
407         }
408         status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
409         if (status != OK) {
410             av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
411             return DNN_ERROR;
412         }
413         status = ie_infer_request_infer_async(request->infer_request);
414         if (status != OK) {
415             av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
416             return DNN_ERROR;
417         }
418         return DNN_SUCCESS;
419     } else {
420         ret = fill_model_input_ov(task->ov_model, request);
421         if (ret != DNN_SUCCESS) {
422             return ret;
423         }
424         status = ie_infer_request_infer(request->infer_request);
425         if (status != OK) {
426             av_log(ctx, AV_LOG_ERROR, "Failed to start synchronous model inference\n");
427             return DNN_ERROR;
428         }
429         infer_completion_callback(request);
430         return task->done ? DNN_SUCCESS : DNN_ERROR;
431     }
432 }
433
434 static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input_name)
435 {
436     OVModel *ov_model = model;
437     OVContext *ctx = &ov_model->ctx;
438     char *model_input_name = NULL;
439     char *all_input_names = NULL;
440     IEStatusCode status;
441     size_t model_input_count = 0;
442     dimensions_t dims;
443     precision_e precision;
444     int input_resizable = ctx->options.input_resizable;
445
446     status = ie_network_get_inputs_number(ov_model->network, &model_input_count);
447     if (status != OK) {
448         av_log(ctx, AV_LOG_ERROR, "Failed to get input count\n");
449         return DNN_ERROR;
450     }
451
452     for (size_t i = 0; i < model_input_count; i++) {
453         status = ie_network_get_input_name(ov_model->network, i, &model_input_name);
454         if (status != OK) {
455             av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's name\n", (int)i);
456             return DNN_ERROR;
457         }
458         if (strcmp(model_input_name, input_name) == 0) {
459             ie_network_name_free(&model_input_name);
460             status |= ie_network_get_input_dims(ov_model->network, input_name, &dims);
461             status |= ie_network_get_input_precision(ov_model->network, input_name, &precision);
462             if (status != OK) {
463                 av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's dims or precision\n", (int)i);
464                 return DNN_ERROR;
465             }
466
467             input->channels = dims.dims[1];
468             input->height   = input_resizable ? -1 : dims.dims[2];
469             input->width    = input_resizable ? -1 : dims.dims[3];
470             input->dt       = precision_to_datatype(precision);
471             return DNN_SUCCESS;
472         } else {
473             //incorrect input name
474             APPEND_STRING(all_input_names, model_input_name)
475         }
476
477         ie_network_name_free(&model_input_name);
478     }
479
480     av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model, all input(s) are: \"%s\"\n", input_name, all_input_names);
481     return DNN_ERROR;
482 }
483
484 static DNNReturnType get_output_ov(void *model, const char *input_name, int input_width, int input_height,
485                                    const char *output_name, int *output_width, int *output_height)
486 {
487     DNNReturnType ret;
488     OVModel *ov_model = model;
489     OVContext *ctx = &ov_model->ctx;
490     TaskItem task;
491     RequestItem request;
492     AVFrame *in_frame = NULL;
493     AVFrame *out_frame = NULL;
494     TaskItem *ptask = &task;
495     IEStatusCode status;
496     input_shapes_t input_shapes;
497
498     if (ctx->options.input_resizable) {
499         status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
500         input_shapes.shapes->shape.dims[2] = input_height;
501         input_shapes.shapes->shape.dims[3] = input_width;
502         status |= ie_network_reshape(ov_model->network, input_shapes);
503         ie_network_input_shapes_free(&input_shapes);
504         if (status != OK) {
505             av_log(ctx, AV_LOG_ERROR, "Failed to reshape input size for %s\n", input_name);
506             return DNN_ERROR;
507         }
508     }
509
510     if (!ov_model->exe_network) {
511         if (init_model_ov(ov_model, input_name, output_name) != DNN_SUCCESS) {
512             av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
513             return DNN_ERROR;
514         }
515     }
516
517     in_frame = av_frame_alloc();
518     if (!in_frame) {
519         av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
520         return DNN_ERROR;
521     }
522     in_frame->width = input_width;
523     in_frame->height = input_height;
524
525     out_frame = av_frame_alloc();
526     if (!out_frame) {
527         av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
528         av_frame_free(&in_frame);
529         return DNN_ERROR;
530     }
531
532     task.done = 0;
533     task.do_ioproc = 0;
534     task.async = 0;
535     task.input_name = input_name;
536     task.in_frame = in_frame;
537     task.output_name = output_name;
538     task.out_frame = out_frame;
539     task.ov_model = ov_model;
540
541     request.infer_request = ov_model->infer_request;
542     request.task_count = 1;
543     request.tasks = &ptask;
544
545     ret = execute_model_ov(&request);
546     *output_width = out_frame->width;
547     *output_height = out_frame->height;
548
549     av_frame_free(&out_frame);
550     av_frame_free(&in_frame);
551     return ret;
552 }
553
554 DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
555 {
556     DNNModel *model = NULL;
557     OVModel *ov_model = NULL;
558     OVContext *ctx = NULL;
559     IEStatusCode status;
560
561     model = av_mallocz(sizeof(DNNModel));
562     if (!model){
563         return NULL;
564     }
565
566     ov_model = av_mallocz(sizeof(OVModel));
567     if (!ov_model) {
568         av_freep(&model);
569         return NULL;
570     }
571     model->model = ov_model;
572     ov_model->model = model;
573     ov_model->ctx.class = &dnn_openvino_class;
574     ctx = &ov_model->ctx;
575
576     //parse options
577     av_opt_set_defaults(ctx);
578     if (av_opt_set_from_string(ctx, options, NULL, "=", "&") < 0) {
579         av_log(ctx, AV_LOG_ERROR, "Failed to parse options \"%s\"\n", options);
580         goto err;
581     }
582
583     status = ie_core_create("", &ov_model->core);
584     if (status != OK)
585         goto err;
586
587     status = ie_core_read_network(ov_model->core, model_filename, NULL, &ov_model->network);
588     if (status != OK) {
589         ie_version_t ver;
590         ver = ie_c_api_version();
591         av_log(ctx, AV_LOG_ERROR, "Failed to read the network from model file %s,\n"
592                                   "Please check if the model version matches the runtime OpenVINO %s\n",
593                                    model_filename, ver.api_version);
594         ie_version_free(&ver);
595         goto err;
596     }
597
598     model->get_input = &get_input_ov;
599     model->get_output = &get_output_ov;
600     model->options = options;
601     model->filter_ctx = filter_ctx;
602     model->func_type = func_type;
603
604     return model;
605
606 err:
607     ff_dnn_free_model_ov(&model);
608     return NULL;
609 }
610
611 DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
612                                       const char **output_names, uint32_t nb_output, AVFrame *out_frame)
613 {
614     OVModel *ov_model = model->model;
615     OVContext *ctx = &ov_model->ctx;
616     TaskItem task;
617     RequestItem request;
618     TaskItem *ptask = &task;
619
620     if (!in_frame) {
621         av_log(ctx, AV_LOG_ERROR, "in frame is NULL when execute model.\n");
622         return DNN_ERROR;
623     }
624
625     if (!out_frame && model->func_type == DFT_PROCESS_FRAME) {
626         av_log(ctx, AV_LOG_ERROR, "out frame is NULL when execute model.\n");
627         return DNN_ERROR;
628     }
629
630     if (nb_output != 1) {
631         // currently, the filter does not need multiple outputs,
632         // so we just pending the support until we really need it.
633         avpriv_report_missing_feature(ctx, "multiple outputs");
634         return DNN_ERROR;
635     }
636
637     if (ctx->options.batch_size > 1) {
638         avpriv_report_missing_feature(ctx, "batch mode for sync execution");
639         return DNN_ERROR;
640     }
641
642     if (!ov_model->exe_network) {
643         if (init_model_ov(ov_model, input_name, output_names[0]) != DNN_SUCCESS) {
644             av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
645             return DNN_ERROR;
646         }
647     }
648
649     task.done = 0;
650     task.do_ioproc = 1;
651     task.async = 0;
652     task.input_name = input_name;
653     task.in_frame = in_frame;
654     task.output_name = output_names[0];
655     task.out_frame = out_frame;
656     task.ov_model = ov_model;
657
658     request.infer_request = ov_model->infer_request;
659     request.task_count = 1;
660     request.tasks = &ptask;
661
662     return execute_model_ov(&request);
663 }
664
665 DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
666                                             const char **output_names, uint32_t nb_output, AVFrame *out_frame)
667 {
668     OVModel *ov_model = model->model;
669     OVContext *ctx = &ov_model->ctx;
670     RequestItem *request;
671     TaskItem *task;
672
673     if (!in_frame) {
674         av_log(ctx, AV_LOG_ERROR, "in frame is NULL when async execute model.\n");
675         return DNN_ERROR;
676     }
677
678     if (!out_frame && model->func_type == DFT_PROCESS_FRAME) {
679         av_log(ctx, AV_LOG_ERROR, "out frame is NULL when async execute model.\n");
680         return DNN_ERROR;
681     }
682
683     task = av_malloc(sizeof(*task));
684     if (!task) {
685         av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");
686         return DNN_ERROR;
687     }
688
689     if (!ov_model->exe_network) {
690         if (init_model_ov(ov_model, input_name, output_names[0]) != DNN_SUCCESS) {
691             av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
692             return DNN_ERROR;
693         }
694     }
695
696     task->done = 0;
697     task->do_ioproc = 1;
698     task->async = 1;
699     task->input_name = input_name;
700     task->in_frame = in_frame;
701     task->output_name = output_names[0];
702     task->out_frame = out_frame;
703     task->ov_model = ov_model;
704     if (ff_queue_push_back(ov_model->task_queue, task) < 0) {
705         av_freep(&task);
706         av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n");
707         return DNN_ERROR;
708     }
709
710     request = ff_safe_queue_pop_front(ov_model->request_queue);
711     if (!request) {
712         av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
713         return DNN_ERROR;
714     }
715
716     request->tasks[request->task_count++] = task;
717     return execute_model_ov(request);
718 }
719
720 DNNAsyncStatusType ff_dnn_get_async_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
721 {
722     OVModel *ov_model = model->model;
723     TaskItem *task = ff_queue_peek_front(ov_model->task_queue);
724
725     if (!task) {
726         return DAST_EMPTY_QUEUE;
727     }
728
729     if (!task->done) {
730         return DAST_NOT_READY;
731     }
732
733     *in = task->in_frame;
734     *out = task->out_frame;
735     ff_queue_pop_front(ov_model->task_queue);
736     av_freep(&task);
737
738     return DAST_SUCCESS;
739 }
740
741 DNNReturnType ff_dnn_flush_ov(const DNNModel *model)
742 {
743     OVModel *ov_model = model->model;
744     OVContext *ctx = &ov_model->ctx;
745     RequestItem *request;
746     IEStatusCode status;
747     DNNReturnType ret;
748
749     request = ff_safe_queue_pop_front(ov_model->request_queue);
750     if (!request) {
751         av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
752         return DNN_ERROR;
753     }
754
755     if (request->task_count == 0) {
756         // no pending task need to flush
757         if (ff_safe_queue_push_back(ov_model->request_queue, request) < 0) {
758             av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
759             return DNN_ERROR;
760         }
761         return DNN_SUCCESS;
762     }
763
764     ret = fill_model_input_ov(ov_model, request);
765     if (ret != DNN_SUCCESS) {
766         av_log(ctx, AV_LOG_ERROR, "Failed to fill model input.\n");
767         return ret;
768     }
769     status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
770     if (status != OK) {
771         av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
772         return DNN_ERROR;
773     }
774     status = ie_infer_request_infer_async(request->infer_request);
775     if (status != OK) {
776         av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
777         return DNN_ERROR;
778     }
779
780     return DNN_SUCCESS;
781 }
782
783 void ff_dnn_free_model_ov(DNNModel **model)
784 {
785     if (*model){
786         OVModel *ov_model = (*model)->model;
787         while (ff_safe_queue_size(ov_model->request_queue) != 0) {
788             RequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue);
789             if (item && item->infer_request) {
790                 ie_infer_request_free(&item->infer_request);
791             }
792             av_freep(&item->tasks);
793             av_freep(&item);
794         }
795         ff_safe_queue_destroy(ov_model->request_queue);
796
797         while (ff_queue_size(ov_model->task_queue) != 0) {
798             TaskItem *item = ff_queue_pop_front(ov_model->task_queue);
799             av_frame_free(&item->in_frame);
800             av_frame_free(&item->out_frame);
801             av_freep(&item);
802         }
803         ff_queue_destroy(ov_model->task_queue);
804
805         if (ov_model->infer_request)
806             ie_infer_request_free(&ov_model->infer_request);
807         if (ov_model->exe_network)
808             ie_exec_network_free(&ov_model->exe_network);
809         if (ov_model->network)
810             ie_network_free(&ov_model->network);
811         if (ov_model->core)
812             ie_core_free(&ov_model->core);
813         av_freep(&ov_model);
814         av_freep(model);
815     }
816 }