aboutsummaryrefslogtreecommitdiffstats
path: root/libavfilter/dnn/dnn_interface.c
blob: e7453f1bb16ea555dab6600c998dcd3fbc347da5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (c) 2018 Sergey Lavrushkin
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * Implements DNN module initialization with specified backend.
 */

#include "../dnn_interface.h"
#include "libavutil/avassert.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavfilter/internal.h"

extern const DNNModule ff_dnn_backend_openvino;
extern const DNNModule ff_dnn_backend_tf;
extern const DNNModule ff_dnn_backend_torch;

#define OFFSET(x) offsetof(DnnContext, x)
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM
static const AVOption dnn_base_options[] = {
        {"model", "path to model file",
                OFFSET(model_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS},
        {"input", "input name of the model",
                OFFSET(model_inputname), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS},
        {"output", "output name of the model",
                OFFSET(model_outputnames_string), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS},
        {"backend_configs", "backend configs (deprecated)",
                OFFSET(backend_options), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS | AV_OPT_FLAG_DEPRECATED},
        {"options", "backend configs (deprecated)",
                OFFSET(backend_options), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS | AV_OPT_FLAG_DEPRECATED},
        {"nireq", "number of request",
                OFFSET(nireq), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS},
        {"async", "use DNN async inference",
                OFFSET(async), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS},
        {"device", "device to run model",
                OFFSET(device), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS},
        {NULL}
};

AVFILTER_DEFINE_CLASS(dnn_base);

typedef struct DnnBackendInfo {
    const size_t offset;
    union {
        const AVClass *class;
        const DNNModule *module;
    };
} DnnBackendInfo;

static const DnnBackendInfo dnn_backend_info_list[] = {
        {0, .class = &dnn_base_class},
        // Must keep the same order as in DNNOptions, so offset value in incremental order
#if CONFIG_LIBTENSORFLOW
        {offsetof(DnnContext, tf_option), .module = &ff_dnn_backend_tf},
#endif
#if CONFIG_LIBOPENVINO
        {offsetof(DnnContext, ov_option), .module = &ff_dnn_backend_openvino},
#endif
#if CONFIG_LIBTORCH
        {offsetof(DnnContext, torch_option), .module = &ff_dnn_backend_torch},
#endif
};

const DNNModule *ff_get_dnn_module(DNNBackendType backend_type, void *log_ctx)
{
    switch(backend_type){
    #if (CONFIG_LIBTENSORFLOW == 1)
    case DNN_TF:
        return &ff_dnn_backend_tf;
    #endif
    #if (CONFIG_LIBOPENVINO == 1)
    case DNN_OV:
        return &ff_dnn_backend_openvino;
    #endif
    #if (CONFIG_LIBTORCH == 1)
    case DNN_TH:
        return &ff_dnn_backend_torch;
    #endif
    default:
        av_log(log_ctx, AV_LOG_ERROR,
                "Module backend_type %d is not supported or enabled.\n",
                backend_type);
        return NULL;
    }
}

void ff_dnn_init_child_class(DnnContext *ctx)
{
    for (int i = 0; i < FF_ARRAY_ELEMS(dnn_backend_info_list); i++) {
        const AVClass **ptr = (const AVClass **) ((char *) ctx + dnn_backend_info_list[i].offset);
        *ptr = dnn_backend_info_list[i].class;
    }
}

void *ff_dnn_child_next(DnnContext *obj, void *prev) {
    size_t pre_offset;

    if (!prev) {
        av_assert0(obj->clazz);
        return obj;
    }

    pre_offset = (char *)prev - (char *)obj;
    for (int i = 0; i < FF_ARRAY_ELEMS(dnn_backend_info_list) - 1; i++) {
        if (dnn_backend_info_list[i].offset == pre_offset) {
            const AVClass **ptr = (const AVClass **) ((char *) obj + dnn_backend_info_list[i + 1].offset);
            av_assert0(*ptr);
            return ptr;
        }
    }

    return NULL;
}

const AVClass *ff_dnn_child_class_iterate(void **iter)
{
    uintptr_t i = (uintptr_t) *iter;

    if (i < FF_ARRAY_ELEMS(dnn_backend_info_list)) {
        *iter = (void *)(i + 1);
        return dnn_backend_info_list[i].class;
    }

    return NULL;
}