]> git.sesse.net Git - ffmpeg/blob - libavfilter/dnn/dnn_backend_openvino.c
dnn/openvino: refine code for better model initialization
[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 } OVOptions;
42
43 typedef struct OVContext {
44     const AVClass *class;
45     OVOptions options;
46 } OVContext;
47
48 typedef struct OVModel{
49     OVContext ctx;
50     DNNModel *model;
51     ie_core_t *core;
52     ie_network_t *network;
53     ie_executable_network_t *exe_network;
54     ie_infer_request_t *infer_request;
55
56     /* for async execution */
57     FFSafeQueue *request_queue;   // holds RequestItem
58     FFQueue *task_queue;          // holds TaskItem
59 } OVModel;
60
61 typedef struct TaskItem {
62     OVModel *ov_model;
63     const char *input_name;
64     AVFrame *in_frame;
65     const char *output_name;
66     AVFrame *out_frame;
67     int do_ioproc;
68     int async;
69     int done;
70 } TaskItem;
71
72 typedef struct RequestItem {
73     ie_infer_request_t *infer_request;
74     TaskItem **tasks;
75     int task_count;
76     ie_complete_call_back_t callback;
77 } RequestItem;
78
79 #define APPEND_STRING(generated_string, iterate_string)                                            \
80     generated_string = generated_string ? av_asprintf("%s %s", generated_string, iterate_string) : \
81                                           av_asprintf("%s", iterate_string);
82
83 #define OFFSET(x) offsetof(OVContext, x)
84 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
85 static const AVOption dnn_openvino_options[] = {
86     { "device", "device to run model", OFFSET(options.device_type), AV_OPT_TYPE_STRING, { .str = "CPU" }, 0, 0, FLAGS },
87     { "nireq",  "number of request",   OFFSET(options.nireq),       AV_OPT_TYPE_INT,    { .i64 = 0 },     0, INT_MAX, FLAGS },
88     { "batch_size",  "batch size per request", OFFSET(options.batch_size),  AV_OPT_TYPE_INT,    { .i64 = 1 },     1, 1000, FLAGS},
89     { NULL }
90 };
91
92 AVFILTER_DEFINE_CLASS(dnn_openvino);
93
94 static DNNDataType precision_to_datatype(precision_e precision)
95 {
96     switch (precision)
97     {
98     case FP32:
99         return DNN_FLOAT;
100     default:
101         av_assert0(!"not supported yet.");
102         return DNN_FLOAT;
103     }
104 }
105
106 static int get_datatype_size(DNNDataType dt)
107 {
108     switch (dt)
109     {
110     case DNN_FLOAT:
111         return sizeof(float);
112     default:
113         av_assert0(!"not supported yet.");
114         return 1;
115     }
116 }
117
118 static DNNReturnType fill_model_input_ov(OVModel *ov_model, RequestItem *request)
119 {
120     dimensions_t dims;
121     precision_e precision;
122     ie_blob_buffer_t blob_buffer;
123     OVContext *ctx = &ov_model->ctx;
124     IEStatusCode status;
125     DNNData input;
126     ie_blob_t *input_blob = NULL;
127     TaskItem *task = request->tasks[0];
128
129     status = ie_infer_request_get_blob(request->infer_request, task->input_name, &input_blob);
130     if (status != OK) {
131         av_log(ctx, AV_LOG_ERROR, "Failed to get input blob with name %s\n", task->input_name);
132         return DNN_ERROR;
133     }
134
135     status |= ie_blob_get_dims(input_blob, &dims);
136     status |= ie_blob_get_precision(input_blob, &precision);
137     if (status != OK) {
138         av_log(ctx, AV_LOG_ERROR, "Failed to get input blob dims/precision\n");
139         return DNN_ERROR;
140     }
141
142     status = ie_blob_get_buffer(input_blob, &blob_buffer);
143     if (status != OK) {
144         av_log(ctx, AV_LOG_ERROR, "Failed to get input blob buffer\n");
145         return DNN_ERROR;
146     }
147
148     input.height = dims.dims[2];
149     input.width = dims.dims[3];
150     input.channels = dims.dims[1];
151     input.data = blob_buffer.buffer;
152     input.dt = precision_to_datatype(precision);
153
154     av_assert0(request->task_count <= dims.dims[0]);
155     for (int i = 0; i < request->task_count; ++i) {
156         task = request->tasks[i];
157         if (task->do_ioproc) {
158             if (ov_model->model->pre_proc != NULL) {
159                 ov_model->model->pre_proc(task->in_frame, &input, ov_model->model->filter_ctx);
160             } else {
161                 proc_from_frame_to_dnn(task->in_frame, &input, ctx);
162             }
163         }
164         input.data = (uint8_t *)input.data
165                      + input.width * input.height * input.channels * get_datatype_size(input.dt);
166     }
167     ie_blob_free(&input_blob);
168
169     return DNN_SUCCESS;
170 }
171
172 static void infer_completion_callback(void *args)
173 {
174     dimensions_t dims;
175     precision_e precision;
176     IEStatusCode status;
177     RequestItem *request = args;
178     TaskItem *task = request->tasks[0];
179     ie_blob_t *output_blob = NULL;
180     ie_blob_buffer_t blob_buffer;
181     DNNData output;
182     OVContext *ctx = &task->ov_model->ctx;
183
184     status = ie_infer_request_get_blob(request->infer_request, task->output_name, &output_blob);
185     if (status != OK) {
186         //incorrect output name
187         char *model_output_name = NULL;
188         char *all_output_names = NULL;
189         size_t model_output_count = 0;
190         av_log(ctx, AV_LOG_ERROR, "Failed to get model output data\n");
191         status = ie_network_get_outputs_number(task->ov_model->network, &model_output_count);
192         for (size_t i = 0; i < model_output_count; i++) {
193             status = ie_network_get_output_name(task->ov_model->network, i, &model_output_name);
194             APPEND_STRING(all_output_names, model_output_name)
195         }
196         av_log(ctx, AV_LOG_ERROR,
197                "output \"%s\" may not correct, all output(s) are: \"%s\"\n",
198                task->output_name, all_output_names);
199         return;
200     }
201
202     status = ie_blob_get_buffer(output_blob, &blob_buffer);
203     if (status != OK) {
204         av_log(ctx, AV_LOG_ERROR, "Failed to access output memory\n");
205         return;
206     }
207
208     status |= ie_blob_get_dims(output_blob, &dims);
209     status |= ie_blob_get_precision(output_blob, &precision);
210     if (status != OK) {
211         av_log(ctx, AV_LOG_ERROR, "Failed to get dims or precision of output\n");
212         return;
213     }
214
215     output.channels = dims.dims[1];
216     output.height   = dims.dims[2];
217     output.width    = dims.dims[3];
218     output.dt       = precision_to_datatype(precision);
219     output.data     = blob_buffer.buffer;
220
221     av_assert0(request->task_count <= dims.dims[0]);
222     av_assert0(request->task_count >= 1);
223     for (int i = 0; i < request->task_count; ++i) {
224         task = request->tasks[i];
225         if (task->do_ioproc) {
226             if (task->ov_model->model->post_proc != NULL) {
227                 task->ov_model->model->post_proc(task->out_frame, &output, task->ov_model->model->filter_ctx);
228             } else {
229                 proc_from_dnn_to_frame(task->out_frame, &output, ctx);
230             }
231         } else {
232             task->out_frame->width = output.width;
233             task->out_frame->height = output.height;
234         }
235         task->done = 1;
236         output.data = (uint8_t *)output.data
237                       + output.width * output.height * output.channels * get_datatype_size(output.dt);
238     }
239     ie_blob_free(&output_blob);
240
241     request->task_count = 0;
242
243     if (task->async) {
244         if (ff_safe_queue_push_back(task->ov_model->request_queue, request) < 0) {
245             av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
246             return;
247         }
248     }
249 }
250
251 static DNNReturnType init_model_ov(OVModel *ov_model)
252 {
253     OVContext *ctx = &ov_model->ctx;
254     IEStatusCode status;
255     ie_available_devices_t a_dev;
256     ie_config_t config = {NULL, NULL, NULL};
257     char *all_dev_names = NULL;
258
259     // batch size
260     if (ctx->options.batch_size <= 0) {
261         ctx->options.batch_size = 1;
262     }
263
264     if (ctx->options.batch_size > 1) {
265         input_shapes_t input_shapes;
266         status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
267         if (status != OK)
268             goto err;
269         for (int i = 0; i < input_shapes.shape_num; i++)
270             input_shapes.shapes[i].shape.dims[0] = ctx->options.batch_size;
271         status = ie_network_reshape(ov_model->network, input_shapes);
272         ie_network_input_shapes_free(&input_shapes);
273         if (status != OK)
274             goto err;
275     }
276
277     status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
278     if (status != OK) {
279         av_log(ctx, AV_LOG_ERROR, "Failed to load OpenVINO model network\n");
280         status = ie_core_get_available_devices(ov_model->core, &a_dev);
281         if (status != OK) {
282             av_log(ctx, AV_LOG_ERROR, "Failed to get available devices\n");
283             goto err;
284         }
285         for (int i = 0; i < a_dev.num_devices; i++) {
286             APPEND_STRING(all_dev_names, a_dev.devices[i])
287         }
288         av_log(ctx, AV_LOG_ERROR,"device %s may not be supported, all available devices are: \"%s\"\n",
289                ctx->options.device_type, all_dev_names);
290         goto err;
291     }
292
293     // create infer_request for sync execution
294     status = ie_exec_network_create_infer_request(ov_model->exe_network, &ov_model->infer_request);
295     if (status != OK)
296         goto err;
297
298     // create infer_requests for async execution
299     if (ctx->options.nireq <= 0) {
300         // the default value is a rough estimation
301         ctx->options.nireq = av_cpu_count() / 2 + 1;
302     }
303
304     ov_model->request_queue = ff_safe_queue_create();
305     if (!ov_model->request_queue) {
306         goto err;
307     }
308
309     for (int i = 0; i < ctx->options.nireq; i++) {
310         RequestItem *item = av_mallocz(sizeof(*item));
311         if (!item) {
312             goto err;
313         }
314
315         status = ie_exec_network_create_infer_request(ov_model->exe_network, &item->infer_request);
316         if (status != OK) {
317             av_freep(&item);
318             goto err;
319         }
320
321         item->tasks = av_malloc_array(ctx->options.batch_size, sizeof(*item->tasks));
322         if (!item->tasks) {
323             av_freep(&item);
324             goto err;
325         }
326         item->task_count = 0;
327
328         item->callback.completeCallBackFunc = infer_completion_callback;
329         item->callback.args = item;
330         if (ff_safe_queue_push_back(ov_model->request_queue, item) < 0) {
331             av_freep(&item);
332             goto err;
333         }
334     }
335
336     ov_model->task_queue = ff_queue_create();
337     if (!ov_model->task_queue) {
338         goto err;
339     }
340
341     return DNN_SUCCESS;
342
343 err:
344     ff_dnn_free_model_ov(&ov_model->model);
345     return DNN_ERROR;
346 }
347
348 static DNNReturnType execute_model_ov(RequestItem *request)
349 {
350     IEStatusCode status;
351     DNNReturnType ret;
352     TaskItem *task = request->tasks[0];
353     OVContext *ctx = &task->ov_model->ctx;
354
355     if (task->async) {
356         if (request->task_count < ctx->options.batch_size) {
357             if (ff_safe_queue_push_front(task->ov_model->request_queue, request) < 0) {
358                 av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
359                 return DNN_ERROR;
360             }
361             return DNN_SUCCESS;
362         }
363         ret = fill_model_input_ov(task->ov_model, request);
364         if (ret != DNN_SUCCESS) {
365             return ret;
366         }
367         status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
368         if (status != OK) {
369             av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
370             return DNN_ERROR;
371         }
372         status = ie_infer_request_infer_async(request->infer_request);
373         if (status != OK) {
374             av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
375             return DNN_ERROR;
376         }
377         return DNN_SUCCESS;
378     } else {
379         ret = fill_model_input_ov(task->ov_model, request);
380         if (ret != DNN_SUCCESS) {
381             return ret;
382         }
383         status = ie_infer_request_infer(request->infer_request);
384         if (status != OK) {
385             av_log(ctx, AV_LOG_ERROR, "Failed to start synchronous model inference\n");
386             return DNN_ERROR;
387         }
388         infer_completion_callback(request);
389         return task->done ? DNN_SUCCESS : DNN_ERROR;
390     }
391 }
392
393 static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input_name)
394 {
395     OVModel *ov_model = (OVModel *)model;
396     OVContext *ctx = &ov_model->ctx;
397     char *model_input_name = NULL;
398     char *all_input_names = NULL;
399     IEStatusCode status;
400     size_t model_input_count = 0;
401     dimensions_t dims;
402     precision_e precision;
403
404     status = ie_network_get_inputs_number(ov_model->network, &model_input_count);
405     if (status != OK) {
406         av_log(ctx, AV_LOG_ERROR, "Failed to get input count\n");
407         return DNN_ERROR;
408     }
409
410     for (size_t i = 0; i < model_input_count; i++) {
411         status = ie_network_get_input_name(ov_model->network, i, &model_input_name);
412         if (status != OK) {
413             av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's name\n", (int)i);
414             return DNN_ERROR;
415         }
416         if (strcmp(model_input_name, input_name) == 0) {
417             ie_network_name_free(&model_input_name);
418             status |= ie_network_get_input_dims(ov_model->network, input_name, &dims);
419             status |= ie_network_get_input_precision(ov_model->network, input_name, &precision);
420             if (status != OK) {
421                 av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's dims or precision\n", (int)i);
422                 return DNN_ERROR;
423             }
424
425             input->channels = dims.dims[1];
426             input->height   = dims.dims[2];
427             input->width    = dims.dims[3];
428             input->dt       = precision_to_datatype(precision);
429             return DNN_SUCCESS;
430         } else {
431             //incorrect input name
432             APPEND_STRING(all_input_names, model_input_name)
433         }
434
435         ie_network_name_free(&model_input_name);
436     }
437
438     av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model, all input(s) are: \"%s\"\n", input_name, all_input_names);
439     return DNN_ERROR;
440 }
441
442 static DNNReturnType get_output_ov(void *model, const char *input_name, int input_width, int input_height,
443                                    const char *output_name, int *output_width, int *output_height)
444 {
445     DNNReturnType ret;
446     OVModel *ov_model = (OVModel *)model;
447     OVContext *ctx = &ov_model->ctx;
448     TaskItem task;
449     RequestItem request;
450     AVFrame *in_frame = av_frame_alloc();
451     AVFrame *out_frame = NULL;
452     TaskItem *ptask = &task;
453
454     if (!in_frame) {
455         av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
456         return DNN_ERROR;
457     }
458     out_frame = av_frame_alloc();
459     if (!out_frame) {
460         av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
461         av_frame_free(&in_frame);
462         return DNN_ERROR;
463     }
464     in_frame->width = input_width;
465     in_frame->height = input_height;
466
467     if (!ov_model->exe_network) {
468         if (init_model_ov(ov_model) != DNN_SUCCESS) {
469             av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
470             return DNN_ERROR;
471         };
472     }
473
474     task.done = 0;
475     task.do_ioproc = 0;
476     task.async = 0;
477     task.input_name = input_name;
478     task.in_frame = in_frame;
479     task.output_name = output_name;
480     task.out_frame = out_frame;
481     task.ov_model = ov_model;
482
483     request.infer_request = ov_model->infer_request;
484     request.task_count = 1;
485     request.tasks = &ptask;
486
487     ret = execute_model_ov(&request);
488     *output_width = out_frame->width;
489     *output_height = out_frame->height;
490
491     av_frame_free(&out_frame);
492     av_frame_free(&in_frame);
493     return ret;
494 }
495
496 DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options, AVFilterContext *filter_ctx)
497 {
498     DNNModel *model = NULL;
499     OVModel *ov_model = NULL;
500     OVContext *ctx = NULL;
501     IEStatusCode status;
502
503     model = av_mallocz(sizeof(DNNModel));
504     if (!model){
505         return NULL;
506     }
507
508     ov_model = av_mallocz(sizeof(OVModel));
509     if (!ov_model) {
510         av_freep(&model);
511         return NULL;
512     }
513     model->model = (void *)ov_model;
514     ov_model->model = model;
515     ov_model->ctx.class = &dnn_openvino_class;
516     ctx = &ov_model->ctx;
517
518     //parse options
519     av_opt_set_defaults(ctx);
520     if (av_opt_set_from_string(ctx, options, NULL, "=", "&") < 0) {
521         av_log(ctx, AV_LOG_ERROR, "Failed to parse options \"%s\"\n", options);
522         goto err;
523     }
524
525     status = ie_core_create("", &ov_model->core);
526     if (status != OK)
527         goto err;
528
529     status = ie_core_read_network(ov_model->core, model_filename, NULL, &ov_model->network);
530     if (status != OK)
531         goto err;
532
533     model->get_input = &get_input_ov;
534     model->get_output = &get_output_ov;
535     model->options = options;
536     model->filter_ctx = filter_ctx;
537
538     return model;
539
540 err:
541     ff_dnn_free_model_ov(&model);
542     return NULL;
543 }
544
545 DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
546                                       const char **output_names, uint32_t nb_output, AVFrame *out_frame)
547 {
548     OVModel *ov_model = (OVModel *)model->model;
549     OVContext *ctx = &ov_model->ctx;
550     TaskItem task;
551     RequestItem request;
552     TaskItem *ptask = &task;
553
554     if (!in_frame) {
555         av_log(ctx, AV_LOG_ERROR, "in frame is NULL when execute model.\n");
556         return DNN_ERROR;
557     }
558
559     if (!out_frame) {
560         av_log(ctx, AV_LOG_ERROR, "out frame is NULL when execute model.\n");
561         return DNN_ERROR;
562     }
563
564     if (nb_output != 1) {
565         // currently, the filter does not need multiple outputs,
566         // so we just pending the support until we really need it.
567         av_log(ctx, AV_LOG_ERROR, "do not support multiple outputs\n");
568         return DNN_ERROR;
569     }
570
571     if (ctx->options.batch_size > 1) {
572         av_log(ctx, AV_LOG_ERROR, "do not support batch mode for sync execution.\n");
573         return DNN_ERROR;
574     }
575
576     if (!ov_model->exe_network) {
577         if (init_model_ov(ov_model) != DNN_SUCCESS) {
578             av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
579             return DNN_ERROR;
580         };
581     }
582
583     task.done = 0;
584     task.do_ioproc = 1;
585     task.async = 0;
586     task.input_name = input_name;
587     task.in_frame = in_frame;
588     task.output_name = output_names[0];
589     task.out_frame = out_frame;
590     task.ov_model = ov_model;
591
592     request.infer_request = ov_model->infer_request;
593     request.task_count = 1;
594     request.tasks = &ptask;
595
596     return execute_model_ov(&request);
597 }
598
599 DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
600                                             const char **output_names, uint32_t nb_output, AVFrame *out_frame)
601 {
602     OVModel *ov_model = (OVModel *)model->model;
603     OVContext *ctx = &ov_model->ctx;
604     RequestItem *request;
605     TaskItem *task;
606
607     if (!in_frame) {
608         av_log(ctx, AV_LOG_ERROR, "in frame is NULL when async execute model.\n");
609         return DNN_ERROR;
610     }
611
612     if (!out_frame) {
613         av_log(ctx, AV_LOG_ERROR, "out frame is NULL when async execute model.\n");
614         return DNN_ERROR;
615     }
616
617     task = av_malloc(sizeof(*task));
618     if (!task) {
619         av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");
620         return DNN_ERROR;
621     }
622
623     if (!ov_model->exe_network) {
624         if (init_model_ov(ov_model) != DNN_SUCCESS) {
625             av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
626             return DNN_ERROR;
627         };
628     }
629
630     task->done = 0;
631     task->do_ioproc = 1;
632     task->async = 1;
633     task->input_name = input_name;
634     task->in_frame = in_frame;
635     task->output_name = output_names[0];
636     task->out_frame = out_frame;
637     task->ov_model = ov_model;
638     if (ff_queue_push_back(ov_model->task_queue, task) < 0) {
639         av_freep(&task);
640         av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n");
641         return DNN_ERROR;
642     }
643
644     request = ff_safe_queue_pop_front(ov_model->request_queue);
645     if (!request) {
646         av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
647         return DNN_ERROR;
648     }
649
650     request->tasks[request->task_count++] = task;
651     return execute_model_ov(request);
652 }
653
654 DNNAsyncStatusType ff_dnn_get_async_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
655 {
656     OVModel *ov_model = (OVModel *)model->model;
657     TaskItem *task = ff_queue_peek_front(ov_model->task_queue);
658
659     if (!task) {
660         return DAST_EMPTY_QUEUE;
661     }
662
663     if (!task->done) {
664         return DAST_NOT_READY;
665     }
666
667     *in = task->in_frame;
668     *out = task->out_frame;
669     ff_queue_pop_front(ov_model->task_queue);
670     av_freep(&task);
671
672     return DAST_SUCCESS;
673 }
674
675 DNNReturnType ff_dnn_flush_ov(const DNNModel *model)
676 {
677     OVModel *ov_model = (OVModel *)model->model;
678     OVContext *ctx = &ov_model->ctx;
679     RequestItem *request;
680     IEStatusCode status;
681     DNNReturnType ret;
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     if (request->task_count == 0) {
690         // no pending task need to flush
691         if (ff_safe_queue_push_back(ov_model->request_queue, request) < 0) {
692             av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
693             return DNN_ERROR;
694         }
695         return DNN_SUCCESS;
696     }
697
698     ret = fill_model_input_ov(ov_model, request);
699     if (ret != DNN_SUCCESS) {
700         av_log(ctx, AV_LOG_ERROR, "Failed to fill model input.\n");
701         return ret;
702     }
703     status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
704     if (status != OK) {
705         av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
706         return DNN_ERROR;
707     }
708     status = ie_infer_request_infer_async(request->infer_request);
709     if (status != OK) {
710         av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
711         return DNN_ERROR;
712     }
713
714     return DNN_SUCCESS;
715 }
716
717 void ff_dnn_free_model_ov(DNNModel **model)
718 {
719     if (*model){
720         OVModel *ov_model = (OVModel *)(*model)->model;
721         while (ff_safe_queue_size(ov_model->request_queue) != 0) {
722             RequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue);
723             if (item && item->infer_request) {
724                 ie_infer_request_free(&item->infer_request);
725             }
726             av_freep(&item->tasks);
727             av_freep(&item);
728         }
729         ff_safe_queue_destroy(ov_model->request_queue);
730
731         while (ff_queue_size(ov_model->task_queue) != 0) {
732             TaskItem *item = ff_queue_pop_front(ov_model->task_queue);
733             av_frame_free(&item->in_frame);
734             av_frame_free(&item->out_frame);
735             av_freep(&item);
736         }
737         ff_queue_destroy(ov_model->task_queue);
738
739         if (ov_model->infer_request)
740             ie_infer_request_free(&ov_model->infer_request);
741         if (ov_model->exe_network)
742             ie_exec_network_free(&ov_model->exe_network);
743         if (ov_model->network)
744             ie_network_free(&ov_model->network);
745         if (ov_model->core)
746             ie_core_free(&ov_model->core);
747         av_freep(&ov_model);
748         av_freep(model);
749     }
750 }