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

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
23 * DNN OpenVINO backend implementation.23 * DNN OpenVINO backend implementation.
24 */24 */
25 25
26#include "dnn_backend_openvino.h"
27#include "dnn_io_proc.h"26#include "dnn_io_proc.h"
28#include "libavformat/avio.h"27#include "libavformat/avio.h"
29#include "libavutil/avassert.h"28#include "libavutil/avassert.h"
@@ -293,6 +292,46 @@ static void infer_completion_callback(void *args) · 263 unmodified lines
293 }292 }
294}293}
295 294
295static 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
296static int init_model_ov(OVModel *ov_model, const char *input_name, const char *output_name)335static int init_model_ov(OVModel *ov_model, const char *input_name, const char *output_name)
297{336{
298 int ret = 0;337 int ret = 0;
@@ -438,7 +477,7 @@ static int init_model_ov(OVModel *ov_model, const char *input_name, const char * · 139 unmodified lines
438 return 0;477 return 0;
439 478
440err:479err:
441 ff_dnn_free_model_ov(&ov_model->model);480 dnn_free_model_ov(&ov_model->model);
442 return ret;481 return ret;
443}482}
444 483
@@ -721,7 +760,7 @@ err: · 276 unmodified lines
721 return ret;760 return ret;
722}761}
723 762
724DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)763static DNNModel *dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
725{764{
726 DNNModel *model = NULL;765 DNNModel *model = NULL;
727 OVModel *ov_model = NULL;766 OVModel *ov_model = NULL;
@@ -806,11 +845,11 @@ DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_ · 78 unmodified lines
806 return model;845 return model;
807 846
808err:847err:
809 ff_dnn_free_model_ov(&model);848 dnn_free_model_ov(&model);
810 return NULL;849 return NULL;
811}850}
812 851
813int ff_dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_params)852static int dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_params)
814{853{
815 OVModel *ov_model = model->model;854 OVModel *ov_model = model->model;
816 OVContext *ctx = &ov_model->ctx;855 OVContext *ctx = &ov_model->ctx;
@@ -893,13 +932,13 @@ int ff_dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_param · 76 unmodified lines
893 }932 }
894}933}
895 934
896DNNAsyncStatusType ff_dnn_get_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)935static DNNAsyncStatusType dnn_get_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
897{936{
898 OVModel *ov_model = model->model;937 OVModel *ov_model = model->model;
899 return ff_dnn_get_result_common(ov_model->task_queue, in, out);938 return ff_dnn_get_result_common(ov_model->task_queue, in, out);
900}939}
901 940
902int ff_dnn_flush_ov(const DNNModel *model)941static int dnn_flush_ov(const DNNModel *model)
903{942{
904 OVModel *ov_model = model->model;943 OVModel *ov_model = model->model;
905 OVContext *ctx = &ov_model->ctx;944 OVContext *ctx = &ov_model->ctx;
@@ -937,41 +976,10 @@ int ff_dnn_flush_ov(const DNNModel *model) · 31 unmodified lines
937 return 0;976 return 0;
938}977}
939 978
940void ff_dnn_free_model_ov(DNNModel **model)979const DNNModule ff_dnn_backend_openvino = {
941{980 .load_model = dnn_load_model_ov,
942 if (*model){981 .execute_model = dnn_execute_model_ov,
943 OVModel *ov_model = (*model)->model;982 .get_result = dnn_get_result_ov,
944 while (ff_safe_queue_size(ov_model->request_queue) != 0) {983 .flush = dnn_flush_ov,
945 OVRequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue);984 .free_model = dnn_free_model_ov,
946 if (item && item->infer_request) {985};
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}
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
32DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx);
33
34int ff_dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_params);
35DNNAsyncStatusType ff_dnn_get_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out);
36int ff_dnn_flush_ov(const DNNModel *model);
37
38void 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
23 * DNN tensorflow backend implementation.23 * DNN tensorflow backend implementation.
24 */24 */
25 25
26#include "dnn_backend_tf.h"
27#include "libavformat/avio.h"26#include "libavformat/avio.h"
28#include "libavutil/avassert.h"27#include "libavutil/avassert.h"
29#include "libavutil/avstring.h"28#include "libavutil/avstring.h"
@@ -477,7 +476,48 @@ static int load_tf_model(TFModel *tf_model, const char *model_filename) · 447 unmodified lines
477 476
478#define NAME_BUFFER_SIZE 256477#define NAME_BUFFER_SIZE 256
479 478
480DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)479static 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
520static DNNModel *dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
481{521{
482 DNNModel *model = NULL;522 DNNModel *model = NULL;
483 TFModel *tf_model = NULL;523 TFModel *tf_model = NULL;
@@ -567,7 +607,7 @@ DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_ · 83 unmodified lines
567 607
568 return model;608 return model;
569err:609err:
570 ff_dnn_free_model_tf(&model);610 dnn_free_model_tf(&model);
571 return NULL;611 return NULL;
572}612}
573 613
@@ -765,11 +805,11 @@ err: · 191 unmodified lines
765 if (ff_safe_queue_push_back(tf_model->request_queue, request) < 0) {805 if (ff_safe_queue_push_back(tf_model->request_queue, request) < 0) {
766 destroy_request_item(&request);806 destroy_request_item(&request);
767 }807 }
768 ff_dnn_free_model_tf(&tf_model->model);808 dnn_free_model_tf(&tf_model->model);
769 return ret;809 return ret;
770}810}
771 811
772int ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params)812static int dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params)
773{813{
774 TFModel *tf_model = model->model;814 TFModel *tf_model = model->model;
775 TFContext *ctx = &tf_model->ctx;815 TFContext *ctx = &tf_model->ctx;
@@ -817,13 +857,13 @@ int ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_param · 41 unmodified lines
817 return execute_model_tf(request, tf_model->lltask_queue);857 return execute_model_tf(request, tf_model->lltask_queue);
818}858}
819 859
820DNNAsyncStatusType ff_dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVFrame **out)860static DNNAsyncStatusType dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVFrame **out)
821{861{
822 TFModel *tf_model = model->model;862 TFModel *tf_model = model->model;
823 return ff_dnn_get_result_common(tf_model->task_queue, in, out);863 return ff_dnn_get_result_common(tf_model->task_queue, in, out);
824}864}
825 865
826int ff_dnn_flush_tf(const DNNModel *model)866static int dnn_flush_tf(const DNNModel *model)
827{867{
828 TFModel *tf_model = model->model;868 TFModel *tf_model = model->model;
829 TFContext *ctx = &tf_model->ctx;869 TFContext *ctx = &tf_model->ctx;
@@ -853,43 +893,10 @@ int ff_dnn_flush_tf(const DNNModel *model) · 23 unmodified lines
853 return ff_dnn_start_inference_async(ctx, &request->exec_module);893 return ff_dnn_start_inference_async(ctx, &request->exec_module);
854}894}
855 895
856void ff_dnn_free_model_tf(DNNModel **model)896const DNNModule ff_dnn_backend_tf = {
857{897 .load_model = dnn_load_model_tf,
858 TFModel *tf_model;898 .execute_model = dnn_execute_model_tf,
859 899 .get_result = dnn_get_result_tf,
860 if (*model){900 .flush = dnn_flush_tf,
861 tf_model = (*model)->model;901 .free_model = dnn_free_model_tf,
862 while (ff_safe_queue_size(tf_model->request_queue) != 0) {902};
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}
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
32DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx);
33
34int ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params);
35DNNAsyncStatusType ff_dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVFrame **out);
36int ff_dnn_flush_tf(const DNNModel *model);
37
38void ff_dnn_free_model_tf(DNNModel **model);
39
40#endif
libavfilter/dnn/dnn_interface.c+5−12
@@ -24,10 +24,11 @@ · 23 unmodified lines
24 */24 */
25 25
26#include "../dnn_interface.h"26#include "../dnn_interface.h"
27#include "dnn_backend_tf.h"
28#include "dnn_backend_openvino.h"
29#include "libavutil/mem.h"27#include "libavutil/mem.h"
30 28
29extern const DNNModule ff_dnn_backend_openvino;
30extern const DNNModule ff_dnn_backend_tf;
31
31DNNModule *ff_get_dnn_module(DNNBackendType backend_type)32DNNModule *ff_get_dnn_module(DNNBackendType backend_type)
32{33{
33 DNNModule *dnn_module;34 DNNModule *dnn_module;
@@ -40,11 +41,7 @@ DNNModule *ff_get_dnn_module(DNNBackendType backend_type) · 6 unmodified lines
40 switch(backend_type){41 switch(backend_type){
41 case DNN_TF:42 case DNN_TF:
42 #if (CONFIG_LIBTENSORFLOW == 1)43 #if (CONFIG_LIBTENSORFLOW == 1)
43 dnn_module->load_model = &ff_dnn_load_model_tf;44 *dnn_module = ff_dnn_backend_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;
48 #else45 #else
49 av_freep(&dnn_module);46 av_freep(&dnn_module);
50 return NULL;47 return NULL;
@@ -52,11 +49,7 @@ DNNModule *ff_get_dnn_module(DNNBackendType backend_type) · 1 unmodified line
52 break;49 break;
53 case DNN_OV:50 case DNN_OV:
54 #if (CONFIG_LIBOPENVINO == 1)51 #if (CONFIG_LIBOPENVINO == 1)
55 dnn_module->load_model = &ff_dnn_load_model_ov;52 *dnn_module = ff_dnn_backend_openvino;
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;
60 #else53 #else
61 av_freep(&dnn_module);54 av_freep(&dnn_module);
62 return NULL;55 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