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 {NULL}
64};
65
67
68typedef struct DnnBackendInfo {
69 const size_t offset;
70 union {
71 const AVClass *class;
73 };
75
77 {0, .class = &dnn_base_class},
78 // Must keep the same order as in DNNOptions, so offset value in incremental order
79#if CONFIG_LIBTENSORFLOW
80 {offsetof(DnnContext, tf_option), .module = &ff_dnn_backend_tf},
81#endif
82#if CONFIG_LIBOPENVINO
83 {offsetof(DnnContext, ov_option), .module = &ff_dnn_backend_openvino},
84#endif
85#if CONFIG_LIBTORCH
86 {offsetof(DnnContext, torch_option), .module = &ff_dnn_backend_torch},
87#endif
88#if CONFIG_LIBONNXRUNTIME
89 {offsetof(DnnContext, onnx_option), .module = &ff_dnn_backend_onnx},
90#endif
91};
92
93const DNNModule *ff_get_dnn_module(DNNBackendType backend_type, void *log_ctx)
94{
95 for (int i = 1; i < FF_ARRAY_ELEMS(dnn_backend_info_list); i++) {
96 if (dnn_backend_info_list[i].module->type == backend_type)
97 return dnn_backend_info_list[i].module;
98 }
99
100 av_log(log_ctx, AV_LOG_ERROR,
101 "Module backend_type %d is not supported or enabled.\n",
102 backend_type);
103 return NULL;
104}
105
107{
108 for (int i = 0; i < FF_ARRAY_ELEMS(dnn_backend_info_list); i++) {
109 const AVClass **ptr = (const AVClass **) ((char *) ctx + dnn_backend_info_list[i].offset);
110 *ptr = dnn_backend_info_list[i].class;
111 // Set default values after the class pointer is set
113 }
114}
115
116void *ff_dnn_child_next(DnnContext *obj, void *prev) {
117 size_t pre_offset;
118
119 if (!prev) {
120 av_assert0(obj->clazz);
121 return obj;
122 }
123
124 pre_offset = (char *)prev - (char *)obj;
125 for (int i = 0; i < FF_ARRAY_ELEMS(dnn_backend_info_list) - 1; i++) {
126 if (dnn_backend_info_list[i].offset == pre_offset) {
127 const AVClass **ptr = (const AVClass **) ((char *) obj + dnn_backend_info_list[i + 1].offset);
128 av_assert0(*ptr);
129 return ptr;
130 }
131 }
132
133 return NULL;
134}
135
136const AVClass *ff_dnn_child_class_iterate_with_mask(void **iter, uint32_t backend_mask)
137{
138 for (uintptr_t i = (uintptr_t)*iter; i < FF_ARRAY_ELEMS(dnn_backend_info_list); i++) {
139 if (i > 0) {
140 const DNNModule *module = dnn_backend_info_list[i].module;
141
142 if (!(module->type & backend_mask))
143 continue;
144 }
145
146 *iter = (void *)(i + 1);
147 return dnn_backend_info_list[i].class;
148 }
149
150 return NULL;
151}
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:598
#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
#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_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:1756
unsigned offset
Definition libaomenc.c:763
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
Memory handling functions.
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
const AVClass * clazz
#define av_log(a,...)