]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/dnn/dnn_backend_openvino.c
lavfi/dnn_backend_openvino.c: Spelling Correction in OpenVino Backend
[ffmpeg] / libavfilter / dnn / dnn_backend_openvino.c
index 9c803c8188f477eeecab4f094e6a95b045197c0b..a8032fe56bbb210981bf77c6c09e3b2dd6b3f2b0 100644 (file)
@@ -55,8 +55,8 @@ typedef struct OVModel{
     ie_infer_request_t *infer_request;
 
     /* for async execution */
-    FFSafeQueue *request_queue;   // holds RequestItem
-    FFQueue *task_queue;          // holds TaskItem
+    SafeQueue *request_queue;   // holds RequestItem
+    Queue *task_queue;          // holds TaskItem
 } OVModel;
 
 typedef struct TaskItem {
@@ -99,6 +99,8 @@ static DNNDataType precision_to_datatype(precision_e precision)
     {
     case FP32:
         return DNN_FLOAT;
+    case U8:
+        return DNN_UINT8;
     default:
         av_assert0(!"not supported yet.");
         return DNN_FLOAT;
@@ -111,6 +113,8 @@ static int get_datatype_size(DNNDataType dt)
     {
     case DNN_FLOAT:
         return sizeof(float);
+    case DNN_UINT8:
+        return sizeof(uint8_t);
     default:
         av_assert0(!"not supported yet.");
         return 1;
@@ -137,12 +141,14 @@ static DNNReturnType fill_model_input_ov(OVModel *ov_model, RequestItem *request
     status |= ie_blob_get_dims(input_blob, &dims);
     status |= ie_blob_get_precision(input_blob, &precision);
     if (status != OK) {
+        ie_blob_free(&input_blob);
         av_log(ctx, AV_LOG_ERROR, "Failed to get input blob dims/precision\n");
         return DNN_ERROR;
     }
 
     status = ie_blob_get_buffer(input_blob, &blob_buffer);
     if (status != OK) {
+        ie_blob_free(&input_blob);
         av_log(ctx, AV_LOG_ERROR, "Failed to get input blob buffer\n");
         return DNN_ERROR;
     }
@@ -152,15 +158,18 @@ static DNNReturnType fill_model_input_ov(OVModel *ov_model, RequestItem *request
     input.channels = dims.dims[1];
     input.data = blob_buffer.buffer;
     input.dt = precision_to_datatype(precision);
+    // all models in openvino open model zoo use BGR as input,
+    // change to be an option when necessary.
+    input.order = DCO_BGR;
 
     av_assert0(request->task_count <= dims.dims[0]);
     for (int i = 0; i < request->task_count; ++i) {
         task = request->tasks[i];
         if (task->do_ioproc) {
-            if (ov_model->model->pre_proc != NULL) {
-                ov_model->model->pre_proc(task->in_frame, &input, ov_model->model->filter_ctx);
+            if (ov_model->model->frame_pre_proc != NULL) {
+                ov_model->model->frame_pre_proc(task->in_frame, &input, ov_model->model->filter_ctx);
             } else {
-                ff_proc_from_frame_to_dnn(task->in_frame, &input, ctx);
+                ff_proc_from_frame_to_dnn(task->in_frame, &input, ov_model->model->func_type, ctx);
             }
         }
         input.data = (uint8_t *)input.data
@@ -178,6 +187,7 @@ static void infer_completion_callback(void *args)
     IEStatusCode status;
     RequestItem *request = args;
     TaskItem *task = request->tasks[0];
+    SafeQueue *requestq = task->ov_model->request_queue;
     ie_blob_t *output_blob = NULL;
     ie_blob_buffer_t blob_buffer;
     DNNData output;
@@ -203,6 +213,7 @@ static void infer_completion_callback(void *args)
 
     status = ie_blob_get_buffer(output_blob, &blob_buffer);
     if (status != OK) {
+        ie_blob_free(&output_blob);
         av_log(ctx, AV_LOG_ERROR, "Failed to access output memory\n");
         return;
     }
@@ -210,6 +221,7 @@ static void infer_completion_callback(void *args)
     status |= ie_blob_get_dims(output_blob, &dims);
     status |= ie_blob_get_precision(output_blob, &precision);
     if (status != OK) {
+        ie_blob_free(&output_blob);
         av_log(ctx, AV_LOG_ERROR, "Failed to get dims or precision of output\n");
         return;
     }
@@ -224,16 +236,32 @@ static void infer_completion_callback(void *args)
     av_assert0(request->task_count >= 1);
     for (int i = 0; i < request->task_count; ++i) {
         task = request->tasks[i];
-        if (task->do_ioproc) {
-            if (task->ov_model->model->post_proc != NULL) {
-                task->ov_model->model->post_proc(task->out_frame, &output, task->ov_model->model->filter_ctx);
+
+        switch (task->ov_model->model->func_type) {
+        case DFT_PROCESS_FRAME:
+            if (task->do_ioproc) {
+                if (task->ov_model->model->frame_post_proc != NULL) {
+                    task->ov_model->model->frame_post_proc(task->out_frame, &output, task->ov_model->model->filter_ctx);
+                } else {
+                    ff_proc_from_dnn_to_frame(task->out_frame, &output, ctx);
+                }
             } else {
-                ff_proc_from_dnn_to_frame(task->out_frame, &output, ctx);
+                task->out_frame->width = output.width;
+                task->out_frame->height = output.height;
             }
-        } else {
-            task->out_frame->width = output.width;
-            task->out_frame->height = output.height;
+            break;
+        case DFT_ANALYTICS_DETECT:
+            if (!task->ov_model->model->detect_post_proc) {
+                av_log(ctx, AV_LOG_ERROR, "detect filter needs to provide post proc\n");
+                return;
+            }
+            task->ov_model->model->detect_post_proc(task->out_frame, &output, 1, task->ov_model->model->filter_ctx);
+            break;
+        default:
+            av_assert0(!"should not reach here");
+            break;
         }
+
         task->done = 1;
         output.data = (uint8_t *)output.data
                       + output.width * output.height * output.channels * get_datatype_size(output.dt);
@@ -243,14 +271,14 @@ static void infer_completion_callback(void *args)
     request->task_count = 0;
 
     if (task->async) {
-        if (ff_safe_queue_push_back(task->ov_model->request_queue, request) < 0) {
+        if (ff_safe_queue_push_back(requestq, request) < 0) {
             av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
             return;
         }
     }
 }
 
-static DNNReturnType init_model_ov(OVModel *ov_model)
+static DNNReturnType init_model_ov(OVModel *ov_model, const char *input_name, const char *output_name)
 {
     OVContext *ctx = &ov_model->ctx;
     IEStatusCode status;
@@ -276,6 +304,33 @@ static DNNReturnType init_model_ov(OVModel *ov_model)
             goto err;
     }
 
+    // The order of dims in the openvino is fixed and it is always NCHW for 4-D data.
+    // while we pass NHWC data from FFmpeg to openvino
+    status = ie_network_set_input_layout(ov_model->network, input_name, NHWC);
+    if (status != OK) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to set layout as NHWC for input %s\n", input_name);
+        goto err;
+    }
+    status = ie_network_set_output_layout(ov_model->network, output_name, NHWC);
+    if (status != OK) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to set layout as NHWC for output %s\n", output_name);
+        goto err;
+    }
+
+    // all models in openvino open model zoo use BGR with range [0.0f, 255.0f] as input,
+    // we don't have a AVPixelFormat to describe it, so we'll use AV_PIX_FMT_BGR24 and
+    // ask openvino to do the conversion internally.
+    // the current supported SR model (frame processing) is generated from tensorflow model,
+    // and its input is Y channel as float with range [0.0f, 1.0f], so do not set for this case.
+    // TODO: we need to get a final clear&general solution with all backends/formats considered.
+    if (ov_model->model->func_type != DFT_PROCESS_FRAME) {
+        status = ie_network_set_input_precision(ov_model->network, input_name, U8);
+        if (status != OK) {
+            av_log(ctx, AV_LOG_ERROR, "Failed to set input precision as U8 for %s\n", input_name);
+            goto err;
+        }
+    }
+
     status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
     if (status != OK) {
         av_log(ctx, AV_LOG_ERROR, "Failed to load OpenVINO model network\n");
@@ -314,25 +369,23 @@ static DNNReturnType init_model_ov(OVModel *ov_model)
             goto err;
         }
 
+        item->callback.completeCallBackFunc = infer_completion_callback;
+        item->callback.args = item;
+        if (ff_safe_queue_push_back(ov_model->request_queue, item) < 0) {
+            av_freep(&item);
+            goto err;
+        }
+
         status = ie_exec_network_create_infer_request(ov_model->exe_network, &item->infer_request);
         if (status != OK) {
-            av_freep(&item);
             goto err;
         }
 
         item->tasks = av_malloc_array(ctx->options.batch_size, sizeof(*item->tasks));
         if (!item->tasks) {
-            av_freep(&item);
             goto err;
         }
         item->task_count = 0;
-
-        item->callback.completeCallBackFunc = infer_completion_callback;
-        item->callback.args = item;
-        if (ff_safe_queue_push_back(ov_model->request_queue, item) < 0) {
-            av_freep(&item);
-            goto err;
-        }
     }
 
     ov_model->task_queue = ff_queue_create();
@@ -394,7 +447,7 @@ static DNNReturnType execute_model_ov(RequestItem *request)
 
 static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input_name)
 {
-    OVModel *ov_model = (OVModel *)model;
+    OVModel *ov_model = model;
     OVContext *ctx = &ov_model->ctx;
     char *model_input_name = NULL;
     char *all_input_names = NULL;
@@ -446,28 +499,20 @@ static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu
                                    const char *output_name, int *output_width, int *output_height)
 {
     DNNReturnType ret;
-    OVModel *ov_model = (OVModel *)model;
+    OVModel *ov_model = model;
     OVContext *ctx = &ov_model->ctx;
     TaskItem task;
     RequestItem request;
-    AVFrame *in_frame = av_frame_alloc();
+    AVFrame *in_frame = NULL;
     AVFrame *out_frame = NULL;
     TaskItem *ptask = &task;
     IEStatusCode status;
     input_shapes_t input_shapes;
 
-    if (!in_frame) {
-        av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
-        return DNN_ERROR;
-    }
-    out_frame = av_frame_alloc();
-    if (!out_frame) {
-        av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
-        av_frame_free(&in_frame);
+    if (ov_model->model->func_type != DFT_PROCESS_FRAME) {
+        av_log(ctx, AV_LOG_ERROR, "Get output dim only when processing frame.\n");
         return DNN_ERROR;
     }
-    in_frame->width = input_width;
-    in_frame->height = input_height;
 
     if (ctx->options.input_resizable) {
         status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
@@ -482,10 +527,25 @@ static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu
     }
 
     if (!ov_model->exe_network) {
-        if (init_model_ov(ov_model) != DNN_SUCCESS) {
+        if (init_model_ov(ov_model, input_name, output_name) != DNN_SUCCESS) {
             av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
             return DNN_ERROR;
-        };
+        }
+    }
+
+    in_frame = av_frame_alloc();
+    if (!in_frame) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
+        return DNN_ERROR;
+    }
+    in_frame->width = input_width;
+    in_frame->height = input_height;
+
+    out_frame = av_frame_alloc();
+    if (!out_frame) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
+        av_frame_free(&in_frame);
+        return DNN_ERROR;
     }
 
     task.done = 0;
@@ -510,7 +570,7 @@ static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu
     return ret;
 }
 
-DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options, AVFilterContext *filter_ctx)
+DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
 {
     DNNModel *model = NULL;
     OVModel *ov_model = NULL;
@@ -527,7 +587,7 @@ DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options,
         av_freep(&model);
         return NULL;
     }
-    model->model = (void *)ov_model;
+    model->model = ov_model;
     ov_model->model = model;
     ov_model->ctx.class = &dnn_openvino_class;
     ctx = &ov_model->ctx;
@@ -558,6 +618,7 @@ DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options,
     model->get_output = &get_output_ov;
     model->options = options;
     model->filter_ctx = filter_ctx;
+    model->func_type = func_type;
 
     return model;
 
@@ -569,7 +630,7 @@ err:
 DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
                                       const char **output_names, uint32_t nb_output, AVFrame *out_frame)
 {
-    OVModel *ov_model = (OVModel *)model->model;
+    OVModel *ov_model = model->model;
     OVContext *ctx = &ov_model->ctx;
     TaskItem task;
     RequestItem request;
@@ -580,7 +641,7 @@ DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_n
         return DNN_ERROR;
     }
 
-    if (!out_frame) {
+    if (!out_frame && model->func_type == DFT_PROCESS_FRAME) {
         av_log(ctx, AV_LOG_ERROR, "out frame is NULL when execute model.\n");
         return DNN_ERROR;
     }
@@ -598,10 +659,10 @@ DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_n
     }
 
     if (!ov_model->exe_network) {
-        if (init_model_ov(ov_model) != DNN_SUCCESS) {
+        if (init_model_ov(ov_model, input_name, output_names[0]) != DNN_SUCCESS) {
             av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
             return DNN_ERROR;
-        };
+        }
     }
 
     task.done = 0;
@@ -623,7 +684,7 @@ DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_n
 DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
                                             const char **output_names, uint32_t nb_output, AVFrame *out_frame)
 {
-    OVModel *ov_model = (OVModel *)model->model;
+    OVModel *ov_model = model->model;
     OVContext *ctx = &ov_model->ctx;
     RequestItem *request;
     TaskItem *task;
@@ -633,24 +694,24 @@ DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *i
         return DNN_ERROR;
     }
 
-    if (!out_frame) {
+    if (!out_frame && model->func_type == DFT_PROCESS_FRAME) {
         av_log(ctx, AV_LOG_ERROR, "out frame is NULL when async execute model.\n");
         return DNN_ERROR;
     }
 
+    if (!ov_model->exe_network) {
+        if (init_model_ov(ov_model, input_name, output_names[0]) != DNN_SUCCESS) {
+            av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
+            return DNN_ERROR;
+        }
+    }
+
     task = av_malloc(sizeof(*task));
     if (!task) {
         av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");
         return DNN_ERROR;
     }
 
-    if (!ov_model->exe_network) {
-        if (init_model_ov(ov_model) != DNN_SUCCESS) {
-            av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
-            return DNN_ERROR;
-        };
-    }
-
     task->done = 0;
     task->do_ioproc = 1;
     task->async = 1;
@@ -677,7 +738,7 @@ DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *i
 
 DNNAsyncStatusType ff_dnn_get_async_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
 {
-    OVModel *ov_model = (OVModel *)model->model;
+    OVModel *ov_model = model->model;
     TaskItem *task = ff_queue_peek_front(ov_model->task_queue);
 
     if (!task) {
@@ -698,7 +759,7 @@ DNNAsyncStatusType ff_dnn_get_async_result_ov(const DNNModel *model, AVFrame **i
 
 DNNReturnType ff_dnn_flush_ov(const DNNModel *model)
 {
-    OVModel *ov_model = (OVModel *)model->model;
+    OVModel *ov_model = model->model;
     OVContext *ctx = &ov_model->ctx;
     RequestItem *request;
     IEStatusCode status;
@@ -741,7 +802,7 @@ DNNReturnType ff_dnn_flush_ov(const DNNModel *model)
 void ff_dnn_free_model_ov(DNNModel **model)
 {
     if (*model){
-        OVModel *ov_model = (OVModel *)(*model)->model;
+        OVModel *ov_model = (*model)->model;
         while (ff_safe_queue_size(ov_model->request_queue) != 0) {
             RequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue);
             if (item && item->infer_request) {