History

avfilter/dnn: define each backend as a DNNModule

To avoid export multiple functions for each backend implementation.

Zhao Zhili committed 3 years ago

Commit 3f52b7e1 parent 2c4b6975 files · +112 −184Browse files

Showing the changes to libavfilter (5 of 5 files). Show all

Unified · Split

5 files
  1. libavfilter/dnn/dnn_backend_openvino.c +53 −45
  2. libavfilter/dnn/dnn_backend_openvino.h +0 −40
  3. libavfilter/dnn/dnn_backend_tf.c +54 −47
  4. libavfilter/dnn/dnn_backend_tf.h +0 −40
  5. libavfilter/dnn/dnn_interface.c +5 −12
libavfilter/dnn/dnn_backend_openvino.c+53−45
@@ -23,7 +23,6 @@ · 22 unmodified lines
2323 * DNN OpenVINO backend implementation.
2424 */
2525
26−#include "dnn_backend_openvino.h"
2726 #include "dnn_io_proc.h"
2827 #include "libavformat/avio.h"
2928 #include "libavutil/avassert.h"
@@ -293,6 +292,46 @@ static void infer_completion_callback(void *args) · 263 unmodified lines
293292 }
294293 }
295294
295+static void dnn_free_model_ov(DNNModel **model)
296+{
297+ if (*model){
298+ OVModel *ov_model = (*model)->model;
299+ while (ff_safe_queue_size(ov_model->request_queue) != 0) {
300+ OVRequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue);
301+ if (item && item->infer_request) {
302+ ie_infer_request_free(&item->infer_request);
303+ }
304+ av_freep(&item->lltasks);
305+ av_freep(&item);
306+ }
307+ ff_safe_queue_destroy(ov_model->request_queue);
308+
309+ while (ff_queue_size(ov_model->lltask_queue) != 0) {
310+ LastLevelTaskItem *item = ff_queue_pop_front(ov_model->lltask_queue);
311+ av_freep(&item);
312+ }
313+ ff_queue_destroy(ov_model->lltask_queue);
314+
315+ while (ff_queue_size(ov_model->task_queue) != 0) {
316+ TaskItem *item = ff_queue_pop_front(ov_model->task_queue);
317+ av_frame_free(&item->in_frame);
318+ av_frame_free(&item->out_frame);
319+ av_freep(&item);
320+ }
321+ ff_queue_destroy(ov_model->task_queue);
322+
323+ if (ov_model->exe_network)
324+ ie_exec_network_free(&ov_model->exe_network);
325+ if (ov_model->network)
326+ ie_network_free(&ov_model->network);
327+ if (ov_model->core)
328+ ie_core_free(&ov_model->core);
329+ av_freep(&ov_model);
330+ av_freep(model);
331+ }
332+}
333+
334+
296335 static int init_model_ov(OVModel *ov_model, const char *input_name, const char *output_name)
297336 {
298337 int ret = 0;
@@ -438,7 +477,7 @@ static int init_model_ov(OVModel *ov_model, const char *input_name, const char * · 139 unmodified lines
438477 return 0;
439478
440479 err:
441− ff_dnn_free_model_ov(&ov_model->model);
480+ dnn_free_model_ov(&ov_model->model);
442481 return ret;
443482 }
444483
@@ -721,7 +760,7 @@ err: · 276 unmodified lines
721760 return ret;
722761 }
723762
724−DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
763+static DNNModel *dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
725764 {
726765 DNNModel *model = NULL;
727766 OVModel *ov_model = NULL;
@@ -806,11 +845,11 @@ DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_ · 78 unmodified lines
806845 return model;
807846
808847 err:
809− ff_dnn_free_model_ov(&model);
848+ dnn_free_model_ov(&model);
810849 return NULL;
811850 }
812851
813−int ff_dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_params)
852+static int dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_params)
814853 {
815854 OVModel *ov_model = model->model;
816855 OVContext *ctx = &ov_model->ctx;
@@ -893,13 +932,13 @@ int ff_dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_param · 76 unmodified lines
893932 }
894933 }
895934
896−DNNAsyncStatusType ff_dnn_get_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
935+static DNNAsyncStatusType dnn_get_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
897936 {
898937 OVModel *ov_model = model->model;
899938 return ff_dnn_get_result_common(ov_model->task_queue, in, out);
900939 }
901940
902−int ff_dnn_flush_ov(const DNNModel *model)
941+static int dnn_flush_ov(const DNNModel *model)
903942 {
904943 OVModel *ov_model = model->model;
905944 OVContext *ctx = &ov_model->ctx;
@@ -937,41 +976,10 @@ int ff_dnn_flush_ov(const DNNModel *model) · 31 unmodified lines
937976 return 0;
938977 }
939978
940−void ff_dnn_free_model_ov(DNNModel **model)
941−{
942− if (*model){
943− OVModel *ov_model = (*model)->model;
944− while (ff_safe_queue_size(ov_model->request_queue) != 0) {
945− OVRequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue);
946− if (item && item->infer_request) {
947− ie_infer_request_free(&item->infer_request);
948− }
949− av_freep(&item->lltasks);
950− av_freep(&item);
951− }
952− ff_safe_queue_destroy(ov_model->request_queue);
953−
954− while (ff_queue_size(ov_model->lltask_queue) != 0) {
955− LastLevelTaskItem *item = ff_queue_pop_front(ov_model->lltask_queue);
956− av_freep(&item);
957− }
958− ff_queue_destroy(ov_model->lltask_queue);
959−
960− while (ff_queue_size(ov_model->task_queue) != 0) {
961− TaskItem *item = ff_queue_pop_front(ov_model->task_queue);
962− av_frame_free(&item->in_frame);
963− av_frame_free(&item->out_frame);
964− av_freep(&item);
965− }
966− ff_queue_destroy(ov_model->task_queue);
967−
968− if (ov_model->exe_network)
969− ie_exec_network_free(&ov_model->exe_network);
970− if (ov_model->network)
971− ie_network_free(&ov_model->network);
972− if (ov_model->core)
973− ie_core_free(&ov_model->core);
974− av_freep(&ov_model);
975− av_freep(model);
976− }
977−}
979+const DNNModule ff_dnn_backend_openvino = {
980+ .load_model = dnn_load_model_ov,
981+ .execute_model = dnn_execute_model_ov,
982+ .get_result = dnn_get_result_ov,
983+ .flush = dnn_flush_ov,
984+ .free_model = dnn_free_model_ov,
985+};
libavfilter/dnn/dnn_backend_openvino.h+0−40
@@ -1,40 +0,0 @@
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 inference functions interface for OpenVINO backend.
24− */
25−
26−
27−#ifndef AVFILTER_DNN_DNN_BACKEND_OPENVINO_H
28−#define AVFILTER_DNN_DNN_BACKEND_OPENVINO_H
29−
30−#include "../dnn_interface.h"
31−
32−DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx);
33−
34−int ff_dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_params);
35−DNNAsyncStatusType ff_dnn_get_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out);
36−int ff_dnn_flush_ov(const DNNModel *model);
37−
38−void ff_dnn_free_model_ov(DNNModel **model);
39−
40−#endif
libavfilter/dnn/dnn_backend_tf.c+54−47
@@ -23,7 +23,6 @@ · 22 unmodified lines
2323 * DNN tensorflow backend implementation.
2424 */
2525
26−#include "dnn_backend_tf.h"
2726 #include "libavformat/avio.h"
2827 #include "libavutil/avassert.h"
2928 #include "libavutil/avstring.h"
@@ -477,7 +476,48 @@ static int load_tf_model(TFModel *tf_model, const char *model_filename) · 447 unmodified lines
477476
478477 #define NAME_BUFFER_SIZE 256
479478
480−DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
479+static void dnn_free_model_tf(DNNModel **model)
480+{
481+ TFModel *tf_model;
482+
483+ if (*model){
484+ tf_model = (*model)->model;
485+ while (ff_safe_queue_size(tf_model->request_queue) != 0) {
486+ TFRequestItem *item = ff_safe_queue_pop_front(tf_model->request_queue);
487+ destroy_request_item(&item);
488+ }
489+ ff_safe_queue_destroy(tf_model->request_queue);
490+
491+ while (ff_queue_size(tf_model->lltask_queue) != 0) {
492+ LastLevelTaskItem *item = ff_queue_pop_front(tf_model->lltask_queue);
493+ av_freep(&item);
494+ }
495+ ff_queue_destroy(tf_model->lltask_queue);
496+
497+ while (ff_queue_size(tf_model->task_queue) != 0) {
498+ TaskItem *item = ff_queue_pop_front(tf_model->task_queue);
499+ av_frame_free(&item->in_frame);
500+ av_frame_free(&item->out_frame);
501+ av_freep(&item);
502+ }
503+ ff_queue_destroy(tf_model->task_queue);
504+
505+ if (tf_model->graph){
506+ TF_DeleteGraph(tf_model->graph);
507+ }
508+ if (tf_model->session){
509+ TF_CloseSession(tf_model->session, tf_model->status);
510+ TF_DeleteSession(tf_model->session, tf_model->status);
511+ }
512+ if (tf_model->status){
513+ TF_DeleteStatus(tf_model->status);
514+ }
515+ av_freep(&tf_model);
516+ av_freep(model);
517+ }
518+}
519+
520+static DNNModel *dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
481521 {
482522 DNNModel *model = NULL;
483523 TFModel *tf_model = NULL;
@@ -567,7 +607,7 @@ DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_ · 83 unmodified lines
567607
568608 return model;
569609 err:
570− ff_dnn_free_model_tf(&model);
610+ dnn_free_model_tf(&model);
571611 return NULL;
572612 }
573613
@@ -765,11 +805,11 @@ err: · 191 unmodified lines
765805 if (ff_safe_queue_push_back(tf_model->request_queue, request) < 0) {
766806 destroy_request_item(&request);
767807 }
768− ff_dnn_free_model_tf(&tf_model->model);
808+ dnn_free_model_tf(&tf_model->model);
769809 return ret;
770810 }
771811
772−int ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params)
812+static int dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params)
773813 {
774814 TFModel *tf_model = model->model;
775815 TFContext *ctx = &tf_model->ctx;
@@ -817,13 +857,13 @@ int ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_param · 41 unmodified lines
817857 return execute_model_tf(request, tf_model->lltask_queue);
818858 }
819859
820−DNNAsyncStatusType ff_dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVFrame **out)
860+static DNNAsyncStatusType dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVFrame **out)
821861 {
822862 TFModel *tf_model = model->model;
823863 return ff_dnn_get_result_common(tf_model->task_queue, in, out);
824864 }
825865
826−int ff_dnn_flush_tf(const DNNModel *model)
866+static int dnn_flush_tf(const DNNModel *model)
827867 {
828868 TFModel *tf_model = model->model;
829869 TFContext *ctx = &tf_model->ctx;
@@ -853,43 +893,10 @@ int ff_dnn_flush_tf(const DNNModel *model) · 23 unmodified lines
853893 return ff_dnn_start_inference_async(ctx, &request->exec_module);
854894 }
855895
856−void ff_dnn_free_model_tf(DNNModel **model)
857−{
858− TFModel *tf_model;
859−
860− if (*model){
861− tf_model = (*model)->model;
862− while (ff_safe_queue_size(tf_model->request_queue) != 0) {
863− TFRequestItem *item = ff_safe_queue_pop_front(tf_model->request_queue);
864− destroy_request_item(&item);
865− }
866− ff_safe_queue_destroy(tf_model->request_queue);
867−
868− while (ff_queue_size(tf_model->lltask_queue) != 0) {
869− LastLevelTaskItem *item = ff_queue_pop_front(tf_model->lltask_queue);
870− av_freep(&item);
871− }
872− ff_queue_destroy(tf_model->lltask_queue);
873−
874− while (ff_queue_size(tf_model->task_queue) != 0) {
875− TaskItem *item = ff_queue_pop_front(tf_model->task_queue);
876− av_frame_free(&item->in_frame);
877− av_frame_free(&item->out_frame);
878− av_freep(&item);
879− }
880− ff_queue_destroy(tf_model->task_queue);
881−
882− if (tf_model->graph){
883− TF_DeleteGraph(tf_model->graph);
884− }
885− if (tf_model->session){
886− TF_CloseSession(tf_model->session, tf_model->status);
887− TF_DeleteSession(tf_model->session, tf_model->status);
888− }
889− if (tf_model->status){
890− TF_DeleteStatus(tf_model->status);
891− }
892− av_freep(&tf_model);
893− av_freep(model);
894− }
895−}
896+const DNNModule ff_dnn_backend_tf = {
897+ .load_model = dnn_load_model_tf,
898+ .execute_model = dnn_execute_model_tf,
899+ .get_result = dnn_get_result_tf,
900+ .flush = dnn_flush_tf,
901+ .free_model = dnn_free_model_tf,
902+};
libavfilter/dnn/dnn_backend_tf.h+0−40
@@ -1,40 +0,0 @@
1−/*
2− * Copyright (c) 2018 Sergey Lavrushkin
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 inference functions interface for TensorFlow backend.
24− */
25−
26−
27−#ifndef AVFILTER_DNN_DNN_BACKEND_TF_H
28−#define AVFILTER_DNN_DNN_BACKEND_TF_H
29−
30−#include "../dnn_interface.h"
31−
32−DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx);
33−
34−int ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params);
35−DNNAsyncStatusType ff_dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVFrame **out);
36−int ff_dnn_flush_tf(const DNNModel *model);
37−
38−void ff_dnn_free_model_tf(DNNModel **model);
39−
40−#endif
libavfilter/dnn/dnn_interface.c+5−12
@@ -24,10 +24,11 @@ · 23 unmodified lines
2424 */
2525
2626 #include "../dnn_interface.h"
27−#include "dnn_backend_tf.h"
28−#include "dnn_backend_openvino.h"
2927 #include "libavutil/mem.h"
3028
29+extern const DNNModule ff_dnn_backend_openvino;
30+extern const DNNModule ff_dnn_backend_tf;
31+
3132 DNNModule *ff_get_dnn_module(DNNBackendType backend_type)
3233 {
3334 DNNModule *dnn_module;
@@ -40,11 +41,7 @@ DNNModule *ff_get_dnn_module(DNNBackendType backend_type) · 6 unmodified lines
4041 switch(backend_type){
4142 case DNN_TF:
4243 #if (CONFIG_LIBTENSORFLOW == 1)
43− dnn_module->load_model = &ff_dnn_load_model_tf;
44− dnn_module->execute_model = &ff_dnn_execute_model_tf;
45− dnn_module->get_result = &ff_dnn_get_result_tf;
46− dnn_module->flush = &ff_dnn_flush_tf;
47− dnn_module->free_model = &ff_dnn_free_model_tf;
44+ *dnn_module = ff_dnn_backend_tf;
4845 #else
4946 av_freep(&dnn_module);
5047 return NULL;
@@ -52,11 +49,7 @@ DNNModule *ff_get_dnn_module(DNNBackendType backend_type) · 1 unmodified line
5249 break;
5350 case DNN_OV:
5451 #if (CONFIG_LIBOPENVINO == 1)
55− dnn_module->load_model = &ff_dnn_load_model_ov;
56− dnn_module->execute_model = &ff_dnn_execute_model_ov;
57− dnn_module->get_result = &ff_dnn_get_result_ov;
58− dnn_module->flush = &ff_dnn_flush_ov;
59− dnn_module->free_model = &ff_dnn_free_model_ov;
52+ *dnn_module = ff_dnn_backend_openvino;
6053 #else
6154 av_freep(&dnn_module);
6255 return NULL;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t