[FFmpeg-cvslog] avfilter/dnn: define each backend as a DNNModule

Zhao Zhili git at videolan.org
Thu Jun 8 06:14:10 EEST 2023


ffmpeg | branch: master | Zhao Zhili <zhilizhao at tencent.com> | Sun Apr 30 23:38:53 2023 +0800| [3f52b7eedc9f131c10cfc2cdf2d493f04db05fd0] | committer: Zhao Zhili

avfilter/dnn: define each backend as a DNNModule

To avoid export multiple functions for each backend
implementation.

Signed-off-by: Zhao Zhili <zhilizhao at tencent.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=3f52b7eedc9f131c10cfc2cdf2d493f04db05fd0
---

 libavfilter/dnn/dnn_backend_openvino.c |  98 +++++++++++++++++---------------
 libavfilter/dnn/dnn_backend_openvino.h |  40 -------------
 libavfilter/dnn/dnn_backend_tf.c       | 101 ++++++++++++++++++---------------
 libavfilter/dnn/dnn_backend_tf.h       |  40 -------------
 libavfilter/dnn/dnn_interface.c        |  17 ++----
 5 files changed, 112 insertions(+), 184 deletions(-)

diff --git a/libavfilter/dnn/dnn_backend_openvino.c b/libavfilter/dnn/dnn_backend_openvino.c
index b67f288336..7db2e7a10f 100644
--- a/libavfilter/dnn/dnn_backend_openvino.c
+++ b/libavfilter/dnn/dnn_backend_openvino.c
@@ -23,7 +23,6 @@
  * DNN OpenVINO backend implementation.
  */
 
-#include "dnn_backend_openvino.h"
 #include "dnn_io_proc.h"
 #include "libavformat/avio.h"
 #include "libavutil/avassert.h"
@@ -293,6 +292,46 @@ static void infer_completion_callback(void *args)
     }
 }
 
+static void dnn_free_model_ov(DNNModel **model)
+{
+    if (*model){
+        OVModel *ov_model = (*model)->model;
+        while (ff_safe_queue_size(ov_model->request_queue) != 0) {
+            OVRequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue);
+            if (item && item->infer_request) {
+                ie_infer_request_free(&item->infer_request);
+            }
+            av_freep(&item->lltasks);
+            av_freep(&item);
+        }
+        ff_safe_queue_destroy(ov_model->request_queue);
+
+        while (ff_queue_size(ov_model->lltask_queue) != 0) {
+            LastLevelTaskItem *item = ff_queue_pop_front(ov_model->lltask_queue);
+            av_freep(&item);
+        }
+        ff_queue_destroy(ov_model->lltask_queue);
+
+        while (ff_queue_size(ov_model->task_queue) != 0) {
+            TaskItem *item = ff_queue_pop_front(ov_model->task_queue);
+            av_frame_free(&item->in_frame);
+            av_frame_free(&item->out_frame);
+            av_freep(&item);
+        }
+        ff_queue_destroy(ov_model->task_queue);
+
+        if (ov_model->exe_network)
+            ie_exec_network_free(&ov_model->exe_network);
+        if (ov_model->network)
+            ie_network_free(&ov_model->network);
+        if (ov_model->core)
+            ie_core_free(&ov_model->core);
+        av_freep(&ov_model);
+        av_freep(model);
+    }
+}
+
+
 static int init_model_ov(OVModel *ov_model, const char *input_name, const char *output_name)
 {
     int ret = 0;
@@ -438,7 +477,7 @@ static int init_model_ov(OVModel *ov_model, const char *input_name, const char *
     return 0;
 
 err:
-    ff_dnn_free_model_ov(&ov_model->model);
+    dnn_free_model_ov(&ov_model->model);
     return ret;
 }
 
@@ -721,7 +760,7 @@ err:
     return ret;
 }
 
-DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
+static DNNModel *dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
 {
     DNNModel *model = NULL;
     OVModel *ov_model = NULL;
@@ -806,11 +845,11 @@ DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_
     return model;
 
 err:
-    ff_dnn_free_model_ov(&model);
+    dnn_free_model_ov(&model);
     return NULL;
 }
 
-int ff_dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_params)
+static int dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_params)
 {
     OVModel *ov_model = model->model;
     OVContext *ctx = &ov_model->ctx;
@@ -893,13 +932,13 @@ int ff_dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_param
     }
 }
 
-DNNAsyncStatusType ff_dnn_get_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
+static DNNAsyncStatusType dnn_get_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
 {
     OVModel *ov_model = model->model;
     return ff_dnn_get_result_common(ov_model->task_queue, in, out);
 }
 
-int ff_dnn_flush_ov(const DNNModel *model)
+static int dnn_flush_ov(const DNNModel *model)
 {
     OVModel *ov_model = model->model;
     OVContext *ctx = &ov_model->ctx;
@@ -937,41 +976,10 @@ int ff_dnn_flush_ov(const DNNModel *model)
     return 0;
 }
 
-void ff_dnn_free_model_ov(DNNModel **model)
-{
-    if (*model){
-        OVModel *ov_model = (*model)->model;
-        while (ff_safe_queue_size(ov_model->request_queue) != 0) {
-            OVRequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue);
-            if (item && item->infer_request) {
-                ie_infer_request_free(&item->infer_request);
-            }
-            av_freep(&item->lltasks);
-            av_freep(&item);
-        }
-        ff_safe_queue_destroy(ov_model->request_queue);
-
-        while (ff_queue_size(ov_model->lltask_queue) != 0) {
-            LastLevelTaskItem *item = ff_queue_pop_front(ov_model->lltask_queue);
-            av_freep(&item);
-        }
-        ff_queue_destroy(ov_model->lltask_queue);
-
-        while (ff_queue_size(ov_model->task_queue) != 0) {
-            TaskItem *item = ff_queue_pop_front(ov_model->task_queue);
-            av_frame_free(&item->in_frame);
-            av_frame_free(&item->out_frame);
-            av_freep(&item);
-        }
-        ff_queue_destroy(ov_model->task_queue);
-
-        if (ov_model->exe_network)
-            ie_exec_network_free(&ov_model->exe_network);
-        if (ov_model->network)
-            ie_network_free(&ov_model->network);
-        if (ov_model->core)
-            ie_core_free(&ov_model->core);
-        av_freep(&ov_model);
-        av_freep(model);
-    }
-}
+const DNNModule ff_dnn_backend_openvino = {
+    .load_model     = dnn_load_model_ov,
+    .execute_model  = dnn_execute_model_ov,
+    .get_result     = dnn_get_result_ov,
+    .flush          = dnn_flush_ov,
+    .free_model     = dnn_free_model_ov,
+};
diff --git a/libavfilter/dnn/dnn_backend_openvino.h b/libavfilter/dnn/dnn_backend_openvino.h
deleted file mode 100644
index 304bc96b99..0000000000
--- a/libavfilter/dnn/dnn_backend_openvino.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (c) 2020
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/**
- * @file
- * DNN inference functions interface for OpenVINO backend.
- */
-
-
-#ifndef AVFILTER_DNN_DNN_BACKEND_OPENVINO_H
-#define AVFILTER_DNN_DNN_BACKEND_OPENVINO_H
-
-#include "../dnn_interface.h"
-
-DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx);
-
-int ff_dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_params);
-DNNAsyncStatusType ff_dnn_get_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out);
-int ff_dnn_flush_ov(const DNNModel *model);
-
-void ff_dnn_free_model_ov(DNNModel **model);
-
-#endif
diff --git a/libavfilter/dnn/dnn_backend_tf.c b/libavfilter/dnn/dnn_backend_tf.c
index 486d2405b8..e6ebd17595 100644
--- a/libavfilter/dnn/dnn_backend_tf.c
+++ b/libavfilter/dnn/dnn_backend_tf.c
@@ -23,7 +23,6 @@
  * DNN tensorflow backend implementation.
  */
 
-#include "dnn_backend_tf.h"
 #include "libavformat/avio.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
@@ -477,7 +476,48 @@ static int load_tf_model(TFModel *tf_model, const char *model_filename)
 
 #define NAME_BUFFER_SIZE 256
 
-DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
+static void dnn_free_model_tf(DNNModel **model)
+{
+    TFModel *tf_model;
+
+    if (*model){
+        tf_model = (*model)->model;
+        while (ff_safe_queue_size(tf_model->request_queue) != 0) {
+            TFRequestItem *item = ff_safe_queue_pop_front(tf_model->request_queue);
+            destroy_request_item(&item);
+        }
+        ff_safe_queue_destroy(tf_model->request_queue);
+
+        while (ff_queue_size(tf_model->lltask_queue) != 0) {
+            LastLevelTaskItem *item = ff_queue_pop_front(tf_model->lltask_queue);
+            av_freep(&item);
+        }
+        ff_queue_destroy(tf_model->lltask_queue);
+
+        while (ff_queue_size(tf_model->task_queue) != 0) {
+            TaskItem *item = ff_queue_pop_front(tf_model->task_queue);
+            av_frame_free(&item->in_frame);
+            av_frame_free(&item->out_frame);
+            av_freep(&item);
+        }
+        ff_queue_destroy(tf_model->task_queue);
+
+        if (tf_model->graph){
+            TF_DeleteGraph(tf_model->graph);
+        }
+        if (tf_model->session){
+            TF_CloseSession(tf_model->session, tf_model->status);
+            TF_DeleteSession(tf_model->session, tf_model->status);
+        }
+        if (tf_model->status){
+            TF_DeleteStatus(tf_model->status);
+        }
+        av_freep(&tf_model);
+        av_freep(model);
+    }
+}
+
+static DNNModel *dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
 {
     DNNModel *model = NULL;
     TFModel *tf_model = NULL;
@@ -567,7 +607,7 @@ DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_
 
     return model;
 err:
-    ff_dnn_free_model_tf(&model);
+    dnn_free_model_tf(&model);
     return NULL;
 }
 
@@ -765,11 +805,11 @@ err:
     if (ff_safe_queue_push_back(tf_model->request_queue, request) < 0) {
         destroy_request_item(&request);
     }
-    ff_dnn_free_model_tf(&tf_model->model);
+    dnn_free_model_tf(&tf_model->model);
     return ret;
 }
 
-int ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params)
+static int dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params)
 {
     TFModel *tf_model = model->model;
     TFContext *ctx = &tf_model->ctx;
@@ -817,13 +857,13 @@ int ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_param
     return execute_model_tf(request, tf_model->lltask_queue);
 }
 
-DNNAsyncStatusType ff_dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVFrame **out)
+static DNNAsyncStatusType dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVFrame **out)
 {
     TFModel *tf_model = model->model;
     return ff_dnn_get_result_common(tf_model->task_queue, in, out);
 }
 
-int ff_dnn_flush_tf(const DNNModel *model)
+static int dnn_flush_tf(const DNNModel *model)
 {
     TFModel *tf_model = model->model;
     TFContext *ctx = &tf_model->ctx;
@@ -853,43 +893,10 @@ int ff_dnn_flush_tf(const DNNModel *model)
     return ff_dnn_start_inference_async(ctx, &request->exec_module);
 }
 
-void ff_dnn_free_model_tf(DNNModel **model)
-{
-    TFModel *tf_model;
-
-    if (*model){
-        tf_model = (*model)->model;
-        while (ff_safe_queue_size(tf_model->request_queue) != 0) {
-            TFRequestItem *item = ff_safe_queue_pop_front(tf_model->request_queue);
-            destroy_request_item(&item);
-        }
-        ff_safe_queue_destroy(tf_model->request_queue);
-
-        while (ff_queue_size(tf_model->lltask_queue) != 0) {
-            LastLevelTaskItem *item = ff_queue_pop_front(tf_model->lltask_queue);
-            av_freep(&item);
-        }
-        ff_queue_destroy(tf_model->lltask_queue);
-
-        while (ff_queue_size(tf_model->task_queue) != 0) {
-            TaskItem *item = ff_queue_pop_front(tf_model->task_queue);
-            av_frame_free(&item->in_frame);
-            av_frame_free(&item->out_frame);
-            av_freep(&item);
-        }
-        ff_queue_destroy(tf_model->task_queue);
-
-        if (tf_model->graph){
-            TF_DeleteGraph(tf_model->graph);
-        }
-        if (tf_model->session){
-            TF_CloseSession(tf_model->session, tf_model->status);
-            TF_DeleteSession(tf_model->session, tf_model->status);
-        }
-        if (tf_model->status){
-            TF_DeleteStatus(tf_model->status);
-        }
-        av_freep(&tf_model);
-        av_freep(model);
-    }
-}
+const DNNModule ff_dnn_backend_tf = {
+    .load_model     = dnn_load_model_tf,
+    .execute_model  = dnn_execute_model_tf,
+    .get_result     = dnn_get_result_tf,
+    .flush          = dnn_flush_tf,
+    .free_model     = dnn_free_model_tf,
+};
diff --git a/libavfilter/dnn/dnn_backend_tf.h b/libavfilter/dnn/dnn_backend_tf.h
deleted file mode 100644
index 0b63a4b6d2..0000000000
--- a/libavfilter/dnn/dnn_backend_tf.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (c) 2018 Sergey Lavrushkin
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/**
- * @file
- * DNN inference functions interface for TensorFlow backend.
- */
-
-
-#ifndef AVFILTER_DNN_DNN_BACKEND_TF_H
-#define AVFILTER_DNN_DNN_BACKEND_TF_H
-
-#include "../dnn_interface.h"
-
-DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx);
-
-int ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params);
-DNNAsyncStatusType ff_dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVFrame **out);
-int ff_dnn_flush_tf(const DNNModel *model);
-
-void ff_dnn_free_model_tf(DNNModel **model);
-
-#endif
diff --git a/libavfilter/dnn/dnn_interface.c b/libavfilter/dnn/dnn_interface.c
index 5b1695a1dd..4f78f35474 100644
--- a/libavfilter/dnn/dnn_interface.c
+++ b/libavfilter/dnn/dnn_interface.c
@@ -24,10 +24,11 @@
  */
 
 #include "../dnn_interface.h"
-#include "dnn_backend_tf.h"
-#include "dnn_backend_openvino.h"
 #include "libavutil/mem.h"
 
+extern const DNNModule ff_dnn_backend_openvino;
+extern const DNNModule ff_dnn_backend_tf;
+
 DNNModule *ff_get_dnn_module(DNNBackendType backend_type)
 {
     DNNModule *dnn_module;
@@ -40,11 +41,7 @@ DNNModule *ff_get_dnn_module(DNNBackendType backend_type)
     switch(backend_type){
     case DNN_TF:
     #if (CONFIG_LIBTENSORFLOW == 1)
-        dnn_module->load_model = &ff_dnn_load_model_tf;
-        dnn_module->execute_model = &ff_dnn_execute_model_tf;
-        dnn_module->get_result = &ff_dnn_get_result_tf;
-        dnn_module->flush = &ff_dnn_flush_tf;
-        dnn_module->free_model = &ff_dnn_free_model_tf;
+        *dnn_module = ff_dnn_backend_tf;
     #else
         av_freep(&dnn_module);
         return NULL;
@@ -52,11 +49,7 @@ DNNModule *ff_get_dnn_module(DNNBackendType backend_type)
         break;
     case DNN_OV:
     #if (CONFIG_LIBOPENVINO == 1)
-        dnn_module->load_model = &ff_dnn_load_model_ov;
-        dnn_module->execute_model = &ff_dnn_execute_model_ov;
-        dnn_module->get_result = &ff_dnn_get_result_ov;
-        dnn_module->flush = &ff_dnn_flush_ov;
-        dnn_module->free_model = &ff_dnn_free_model_ov;
+        *dnn_module = ff_dnn_backend_openvino;
     #else
         av_freep(&dnn_module);
         return NULL;



More information about the ffmpeg-cvslog mailing list