FFmpeg
Loading...
Searching...
No Matches
dnn_interface.c
Go to the documentation of this file.
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
31#include "libavfilter/filters.h"
32
34extern const DNNModule ff_dnn_backend_tf;
36#if CONFIG_LIBONNXRUNTIME
38#endif
39
40#define OFFSET(x) offsetof(DnnContext, x)
41#define FLAGS AV_OPT_FLAG_FILTERING_PARAM
42static const AVOption dnn_base_options[] = {
43 {"model", "path to model file",
44 OFFSET(model_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS},
45 {"input", "input name of the model",
46 OFFSET(model_inputname), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS},
47 {"output", "output name of the model",
48 OFFSET(model_outputnames_string), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS},
49 {"backend_configs", "backend configs (deprecated)",
50 OFFSET(backend_options), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS | AV_OPT_FLAG_DEPRECATED},
51 {"options", "backend configs (deprecated)",
52 OFFSET(backend_options), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS | AV_OPT_FLAG_DEPRECATED},
53 {"nireq", "number of request",
54 OFFSET(nireq), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS},
55 {"batch_size", "batch size per request",
56 OFFSET(batch_size), AV_OPT_TYPE_INT, {.i64 = 1}, 1, 1000, FLAGS},
57 {"async", "use DNN async inference",
58 OFFSET(async), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS},
59 {"device", "device to run model",
60 OFFSET(device), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS},
61 {"device_id", "device ID to run model",
62 OFFSET(device_id), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS},
63 {"input_dtype", "input tensor data type",
64 OFFSET(input_dtype), AV_OPT_TYPE_INT, {.i64 = DNN_AUTO}, DNN_AUTO, DNN_UINT8, FLAGS, "input_dtype"},
65 {"auto", "auto-detect from model graph, fall back to float", 0, AV_OPT_TYPE_CONST, {.i64 = DNN_AUTO}, 0, 0, FLAGS, "input_dtype"},
66 {"float", "float32 input tensor", 0, AV_OPT_TYPE_CONST, {.i64 = DNN_FLOAT}, 0, 0, FLAGS, "input_dtype"},
67 {"uint8", "uint8 input tensor", 0, AV_OPT_TYPE_CONST, {.i64 = DNN_UINT8}, 0, 0, FLAGS, "input_dtype"},
68 {NULL}
69};
70
72
73typedef struct DnnBackendInfo {
74 const size_t offset;
75 union {
76 const AVClass *class;
78 };
80
82 {0, .class = &dnn_base_class},
83 // Must keep the same order as in DNNOptions, so offset value in incremental order
84#if CONFIG_LIBTENSORFLOW
85 {offsetof(DnnContext, tf_option), .module = &ff_dnn_backend_tf},
86#endif
87#if CONFIG_LIBOPENVINO
88 {offsetof(DnnContext, ov_option), .module = &ff_dnn_backend_openvino},
89#endif
90#if CONFIG_LIBTORCH
91 {offsetof(DnnContext, torch_option), .module = &ff_dnn_backend_torch},
92#endif
93#if CONFIG_LIBONNXRUNTIME
94 {offsetof(DnnContext, onnx_option), .module = &ff_dnn_backend_onnx},
95#endif
96};
97
98const DNNModule *ff_get_dnn_module(DNNBackendType backend_type, void *log_ctx)
99{
100 for (int i = 1; i < FF_ARRAY_ELEMS(dnn_backend_info_list); i++) {
101 if (dnn_backend_info_list[i].module->type == backend_type)
102 return dnn_backend_info_list[i].module;
103 }
104
105 av_log(log_ctx, AV_LOG_ERROR,
106 "Module backend_type %d is not supported or enabled.\n",
107 backend_type);
108 return NULL;
109}
110
112{
113 for (int i = 0; i < FF_ARRAY_ELEMS(dnn_backend_info_list); i++) {
114 const AVClass **ptr = (const AVClass **) ((char *) ctx + dnn_backend_info_list[i].offset);
115 *ptr = dnn_backend_info_list[i].class;
116 // Set default values after the class pointer is set
118 }
119}
120
121void *ff_dnn_child_next(DnnContext *obj, void *prev) {
122 size_t pre_offset;
123
124 if (!prev) {
125 av_assert0(obj->clazz);
126 return obj;
127 }
128
129 pre_offset = (char *)prev - (char *)obj;
130 for (int i = 0; i < FF_ARRAY_ELEMS(dnn_backend_info_list) - 1; i++) {
131 if (dnn_backend_info_list[i].offset == pre_offset) {
132 const AVClass **ptr = (const AVClass **) ((char *) obj + dnn_backend_info_list[i + 1].offset);
133 av_assert0(*ptr);
134 return ptr;
135 }
136 }
137
138 return NULL;
139}
140
141const AVClass *ff_dnn_child_class_iterate_with_mask(void **iter, uint32_t backend_mask)
142{
143 for (uintptr_t i = (uintptr_t)*iter; i < FF_ARRAY_ELEMS(dnn_backend_info_list); i++) {
144 if (i > 0) {
145 const DNNModule *module = dnn_backend_info_list[i].module;
146
147 if (!(module->type & backend_mask))
148 continue;
149 }
150
151 *iter = (void *)(i + 1);
152 return dnn_backend_info_list[i].class;
153 }
154
155 return NULL;
156}
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FLAGS
Definition cmdutils.c:596
#define NULL
Definition coverity.c:32
const DNNModule ff_dnn_backend_onnx
const DNNModule ff_dnn_backend_openvino
const DNNModule ff_dnn_backend_tf
const DNNModule ff_dnn_backend_torch
void * ff_dnn_child_next(DnnContext *obj, void *prev)
const DNNModule * ff_get_dnn_module(DNNBackendType backend_type, void *log_ctx)
static const AVOption dnn_base_options[]
static const DnnBackendInfo dnn_backend_info_list[]
const AVClass * ff_dnn_child_class_iterate_with_mask(void **iter, uint32_t backend_mask)
void ff_dnn_init_child_class(DnnContext *ctx)
#define OFFSET(x)
DNN inference engine interface.
DNNBackendType
@ DNN_UINT8
@ DNN_FLOAT
@ DNN_AUTO
#define AV_OPT_FLAG_DEPRECATED
Set if option is deprecated, users should refer to AVOption.help text for more information.
Definition opt.h:385
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition opt.c:1771
unsigned offset
Definition libaomenc.c:763
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:493
Memory handling functions.
static const char * obj
Definition mscl.c:57
AVOptions.
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
Definition log.h:76
AVOption.
Definition opt.h:428
DNNBackendType type
const DNNModule * module
const size_t offset
#define av_log(a,...)