FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/dnn/dnn_interface.c
Date: 2024-05-26 22:00:00
Exec Total Coverage
Lines: 0 21 0.0%
Functions: 0 4 0.0%
Branches: 0 8 0.0%

Line Branch Exec Source
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 * Implements DNN module initialization with specified backend.
24 */
25
26 #include "../dnn_interface.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/opt.h"
30 #include "libavfilter/internal.h"
31
32 extern const DNNModule ff_dnn_backend_openvino;
33 extern const DNNModule ff_dnn_backend_tf;
34 extern const DNNModule ff_dnn_backend_torch;
35
36 #define OFFSET(x) offsetof(DnnContext, x)
37 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
38 static const AVOption dnn_base_options[] = {
39 {"model", "path to model file",
40 OFFSET(model_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS},
41 {"input", "input name of the model",
42 OFFSET(model_inputname), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS},
43 {"output", "output name of the model",
44 OFFSET(model_outputnames_string), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS},
45 {"backend_configs", "backend configs (deprecated)",
46 OFFSET(backend_options), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS | AV_OPT_FLAG_DEPRECATED},
47 {"options", "backend configs (deprecated)",
48 OFFSET(backend_options), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS | AV_OPT_FLAG_DEPRECATED},
49 {"nireq", "number of request",
50 OFFSET(nireq), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS},
51 {"async", "use DNN async inference",
52 OFFSET(async), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS},
53 {"device", "device to run model",
54 OFFSET(device), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS},
55 {NULL}
56 };
57
58 AVFILTER_DEFINE_CLASS(dnn_base);
59
60 typedef struct DnnBackendInfo {
61 const size_t offset;
62 union {
63 const AVClass *class;
64 const DNNModule *module;
65 };
66 } DnnBackendInfo;
67
68 static const DnnBackendInfo dnn_backend_info_list[] = {
69 {0, .class = &dnn_base_class},
70 // Must keep the same order as in DNNOptions, so offset value in incremental order
71 #if CONFIG_LIBTENSORFLOW
72 {offsetof(DnnContext, tf_option), .module = &ff_dnn_backend_tf},
73 #endif
74 #if CONFIG_LIBOPENVINO
75 {offsetof(DnnContext, ov_option), .module = &ff_dnn_backend_openvino},
76 #endif
77 #if CONFIG_LIBTORCH
78 {offsetof(DnnContext, torch_option), .module = &ff_dnn_backend_torch},
79 #endif
80 };
81
82 const DNNModule *ff_get_dnn_module(DNNBackendType backend_type, void *log_ctx)
83 {
84 switch(backend_type){
85 #if (CONFIG_LIBTENSORFLOW == 1)
86 case DNN_TF:
87 return &ff_dnn_backend_tf;
88 #endif
89 #if (CONFIG_LIBOPENVINO == 1)
90 case DNN_OV:
91 return &ff_dnn_backend_openvino;
92 #endif
93 #if (CONFIG_LIBTORCH == 1)
94 case DNN_TH:
95 return &ff_dnn_backend_torch;
96 #endif
97 default:
98 av_log(log_ctx, AV_LOG_ERROR,
99 "Module backend_type %d is not supported or enabled.\n",
100 backend_type);
101 return NULL;
102 }
103 }
104
105 void ff_dnn_init_child_class(DnnContext *ctx)
106 {
107 for (int i = 0; i < FF_ARRAY_ELEMS(dnn_backend_info_list); i++) {
108 const AVClass **ptr = (const AVClass **) ((char *) ctx + dnn_backend_info_list[i].offset);
109 *ptr = dnn_backend_info_list[i].class;
110 }
111 }
112
113 void *ff_dnn_child_next(DnnContext *obj, void *prev) {
114 size_t pre_offset;
115
116 if (!prev) {
117 av_assert0(obj->clazz);
118 return obj;
119 }
120
121 pre_offset = (char *)prev - (char *)obj;
122 for (int i = 0; i < FF_ARRAY_ELEMS(dnn_backend_info_list) - 1; i++) {
123 if (dnn_backend_info_list[i].offset == pre_offset) {
124 const AVClass **ptr = (const AVClass **) ((char *) obj + dnn_backend_info_list[i + 1].offset);
125 av_assert0(*ptr);
126 return ptr;
127 }
128 }
129
130 return NULL;
131 }
132
133 const AVClass *ff_dnn_child_class_iterate(void **iter)
134 {
135 uintptr_t i = (uintptr_t) *iter;
136
137 if (i < FF_ARRAY_ELEMS(dnn_backend_info_list)) {
138 *iter = (void *)(i + 1);
139 return dnn_backend_info_list[i].class;
140 }
141
142 return NULL;
143 }
144
145