FFmpeg
dnn_interface.h
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  * DNN inference engine interface.
24  */
25 
26 #ifndef AVFILTER_DNN_INTERFACE_H
27 #define AVFILTER_DNN_INTERFACE_H
28 
29 #include <stdint.h>
30 #include "libavutil/frame.h"
31 #include "avfilter.h"
32 
33 #define DNN_GENERIC_ERROR FFERRTAG('D','N','N','!')
34 
35 typedef enum {
36  DNN_TF = 1,
37  DNN_OV = 1 << 1,
38  DNN_TH = 1 << 2,
39  DNN_ONNX = 1 << 3
41 
42 typedef enum {DNN_FLOAT = 1, DNN_UINT8 = 4} DNNDataType;
43 
44 typedef enum {
49 
50 typedef enum {
51  DAST_FAIL, // something wrong
52  DAST_EMPTY_QUEUE, // no more inference result to get
53  DAST_NOT_READY, // all queued inferences are not finished
54  DAST_SUCCESS // got a result frame successfully
56 
57 typedef enum {
59  DFT_PROCESS_FRAME, // process the whole frame
60  DFT_ANALYTICS_DETECT, // detect from the whole frame
61  DFT_ANALYTICS_CLASSIFY, // classify for each bounding box
63 
64 typedef enum {
68 } DNNLayout;
69 
70 typedef struct DNNData{
71  void *data;
72  int dims[4];
73  // dt and order together decide the color format
77  float scale;
78  float mean;
79 } DNNData;
80 
81 typedef struct DNNExecBaseParams {
82  const char *input_name;
83  const char **output_names;
84  uint32_t nb_output;
88 
91  const char *target;
93 
96 typedef int (*ClassifyPostProc)(AVFrame *frame, DNNData *output, uint32_t bbox_index, AVFilterContext *filter_ctx);
97 
98 typedef struct DNNModel{
99  // Stores FilterContext used for the interaction between AVFrame and DNNData
101  // Stores function type of the model
103  // Gets model input information
104  // Just reuse struct DNNData here, actually the DNNData.data field is not needed.
105  int (*get_input)(struct DNNModel *model, DNNData *input, const char *input_name);
106  // Gets model output width/height with given input w/h
107  int (*get_output)(struct DNNModel *model, const char *input_name, int input_width, int input_height,
108  const char *output_name, int *output_width, int *output_height);
109  // set the pre process to transfer data from AVFrame to DNNData
110  // the default implementation within DNN is used if it is not provided by the filter
112  // set the post process to transfer data from DNNData to AVFrame
113  // the default implementation within DNN is used if it is not provided by the filter
115  // set the post process to interpret detect result from DNNData
117  // set the post process to interpret classify result from DNNData
119 } DNNModel;
120 
121 typedef struct TFOptions{
122  const AVClass *clazz;
123 
124  char *sess_config;
125 } TFOptions;
126 
127 typedef struct OVOptions {
128  const AVClass *clazz;
129 
133  float scale;
134  float mean;
135 } OVOptions;
136 
137 typedef struct THOptions {
138  const AVClass *clazz;
139  int optimize;
140 } THOptions;
141 
142 #if CONFIG_LIBONNXRUNTIME
143 typedef struct ONNXOptions {
144  const AVClass *clazz;
145  int num_threads;
146 } ONNXOptions;
147 #endif
148 
149 typedef struct DNNModule DNNModule;
150 
151 typedef struct DnnContext {
152  const AVClass *clazz;
153 
155 
161  int async;
162 
164  uint32_t nb_outputs;
166 
167  int nireq;
168  char *device;
170 
171 #if CONFIG_LIBTENSORFLOW
172  TFOptions tf_option;
173 #endif
174 
175 #if CONFIG_LIBOPENVINO
176  OVOptions ov_option;
177 #endif
178 #if CONFIG_LIBTORCH
179  THOptions torch_option;
180 #endif
181 #if CONFIG_LIBONNXRUNTIME
182  ONNXOptions onnx_option;
183 #endif
184 } DnnContext;
185 
186 // Stores pointers to functions for loading, executing, freeing DNN models for one of the backends.
187 struct DNNModule {
188  const AVClass clazz;
190  // Loads model and parameters from given file. Returns NULL if it is not possible.
192  // Executes model with specified input and output. Returns the error code otherwise.
193  int (*execute_model)(const DNNModel *model, DNNExecBaseParams *exec_params);
194  // Retrieve inference result.
196  // Flush all the pending tasks.
197  int (*flush)(const DNNModel *model);
198  // Frees memory allocated for model.
199  void (*free_model)(DNNModel **model);
200 };
201 
202 // Initializes DNNModule depending on chosen backend.
203 const DNNModule *ff_get_dnn_module(DNNBackendType backend_type, void *log_ctx);
204 
206 void *ff_dnn_child_next(DnnContext *obj, void *prev);
207 const AVClass *ff_dnn_child_class_iterate_with_mask(void **iter, uint32_t backend_mask);
208 
210 {
211  return layout == DL_NHWC ? 2 : 3;
212 }
213 
215 {
216  return layout == DL_NHWC ? 1 : 2;
217 }
218 
220 {
221  return layout == DL_NHWC ? 3 : 1;
222 }
223 
224 #endif
DNNModule::type
DNNBackendType type
Definition: dnn_interface.h:189
TFOptions::sess_config
char * sess_config
Definition: dnn_interface.h:124
DNNColorOrder
DNNColorOrder
Definition: dnn_interface.h:44
out
static FILE * out
Definition: movenc.c:55
DNNModule::get_result
DNNAsyncStatusType(* get_result)(const DNNModel *model, AVFrame **in, AVFrame **out)
Definition: dnn_interface.h:195
DNNFunctionType
DNNFunctionType
Definition: dnn_interface.h:57
DnnContext::model
DNNModel * model
Definition: dnn_interface.h:154
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:226
DNNData::data
void * data
Definition: dnn_interface.h:71
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:466
OVOptions::mean
float mean
Definition: dnn_interface.h:134
DNNModel::frame_pre_proc
FramePrePostProc frame_pre_proc
Definition: dnn_interface.h:111
DetectPostProc
int(* DetectPostProc)(AVFrame *frame, DNNData *output, uint32_t nb, AVFilterContext *filter_ctx)
Definition: dnn_interface.h:95
DNNExecBaseParams::input_name
const char * input_name
Definition: dnn_interface.h:82
DnnContext::clazz
const AVClass * clazz
Definition: dnn_interface.h:152
DNNExecBaseParams::in_frame
AVFrame * in_frame
Definition: dnn_interface.h:85
THOptions
Definition: dnn_interface.h:137
DFT_NONE
@ DFT_NONE
Definition: dnn_interface.h:58
OVOptions::batch_size
int batch_size
Definition: dnn_interface.h:130
DnnContext::dnn_module
const DNNModule * dnn_module
Definition: dnn_interface.h:165
DnnContext::device_id
int device_id
Definition: dnn_interface.h:169
DNNModel::filter_ctx
AVFilterContext * filter_ctx
Definition: dnn_interface.h:100
dnn_get_width_idx_by_layout
static int dnn_get_width_idx_by_layout(DNNLayout layout)
Definition: dnn_interface.h:209
DnnContext
Definition: dnn_interface.h:151
filter_ctx
static FilteringContext * filter_ctx
Definition: transcode.c:52
DAST_FAIL
@ DAST_FAIL
Definition: dnn_interface.h:51
DL_NHWC
@ DL_NHWC
Definition: dnn_interface.h:67
OVOptions::clazz
const AVClass * clazz
Definition: dnn_interface.h:128
DNN_TF
@ DNN_TF
Definition: dnn_interface.h:36
DnnContext::async
int async
Definition: dnn_interface.h:161
DnnContext::model_filename
char * model_filename
Definition: dnn_interface.h:156
DCO_NONE
@ DCO_NONE
Definition: dnn_interface.h:45
DNNExecClassificationParams
Definition: dnn_interface.h:89
DnnContext::model_outputnames
char ** model_outputnames
Definition: dnn_interface.h:163
OVOptions::layout
DNNLayout layout
Definition: dnn_interface.h:132
DNNData::order
DNNColorOrder order
Definition: dnn_interface.h:75
DNNData
Definition: dnn_interface.h:70
DNNModule::clazz
const AVClass clazz
Definition: dnn_interface.h:188
DNNModel::get_output
int(* get_output)(struct DNNModel *model, const char *input_name, int input_width, int input_height, const char *output_name, int *output_width, int *output_height)
Definition: dnn_interface.h:107
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
DL_NCHW
@ DL_NCHW
Definition: dnn_interface.h:66
DNN_OV
@ DNN_OV
Definition: dnn_interface.h:37
DNNExecClassificationParams::target
const char * target
Definition: dnn_interface.h:91
DnnContext::backend_options
char * backend_options
Definition: dnn_interface.h:160
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
DNNExecClassificationParams::base
DNNExecBaseParams base
Definition: dnn_interface.h:90
DNNModel::frame_post_proc
FramePrePostProc frame_post_proc
Definition: dnn_interface.h:114
TFOptions::clazz
const AVClass * clazz
Definition: dnn_interface.h:122
ff_get_dnn_module
const DNNModule * ff_get_dnn_module(DNNBackendType backend_type, void *log_ctx)
Definition: dnn_interface.c:91
DnnContext::nireq
int nireq
Definition: dnn_interface.h:167
DnnContext::backend_type
DNNBackendType backend_type
Definition: dnn_interface.h:157
ClassifyPostProc
int(* ClassifyPostProc)(AVFrame *frame, DNNData *output, uint32_t bbox_index, AVFilterContext *filter_ctx)
Definition: dnn_interface.h:96
DAST_SUCCESS
@ DAST_SUCCESS
Definition: dnn_interface.h:54
DnnContext::model_inputname
char * model_inputname
Definition: dnn_interface.h:158
DNNBackendType
DNNBackendType
Definition: dnn_interface.h:35
DAST_EMPTY_QUEUE
@ DAST_EMPTY_QUEUE
Definition: dnn_interface.h:52
DnnContext::nb_outputs
uint32_t nb_outputs
Definition: dnn_interface.h:164
DNNLayout
DNNLayout
Definition: dnn_interface.h:64
DNNModel::detect_post_proc
DetectPostProc detect_post_proc
Definition: dnn_interface.h:116
DNNModel::func_type
DNNFunctionType func_type
Definition: dnn_interface.h:102
DNNDataType
DNNDataType
Definition: dnn_interface.h:42
DNNData::dt
DNNDataType dt
Definition: dnn_interface.h:74
frame.h
DNNData::scale
float scale
Definition: dnn_interface.h:77
DNN_ONNX
@ DNN_ONNX
Definition: dnn_interface.h:39
DNNData::layout
DNNLayout layout
Definition: dnn_interface.h:76
DNN_FLOAT
@ DNN_FLOAT
Definition: dnn_interface.h:42
DNNExecBaseParams::out_frame
AVFrame * out_frame
Definition: dnn_interface.h:86
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
OVOptions::input_resizable
int input_resizable
Definition: dnn_interface.h:131
THOptions::optimize
int optimize
Definition: dnn_interface.h:139
layout
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel layout
Definition: filter_design.txt:18
DFT_ANALYTICS_DETECT
@ DFT_ANALYTICS_DETECT
Definition: dnn_interface.h:60
ff_dnn_child_next
void * ff_dnn_child_next(DnnContext *obj, void *prev)
Definition: dnn_interface.c:114
DnnContext::device
char * device
Definition: dnn_interface.h:168
DNNModel::classify_post_proc
ClassifyPostProc classify_post_proc
Definition: dnn_interface.h:118
OVOptions::scale
float scale
Definition: dnn_interface.h:133
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
DNNData::mean
float mean
Definition: dnn_interface.h:78
DNN_UINT8
@ DNN_UINT8
Definition: dnn_interface.h:42
DFT_ANALYTICS_CLASSIFY
@ DFT_ANALYTICS_CLASSIFY
Definition: dnn_interface.h:61
DNNModule::free_model
void(* free_model)(DNNModel **model)
Definition: dnn_interface.h:199
avfilter.h
THOptions::clazz
const AVClass * clazz
Definition: dnn_interface.h:138
DCO_RGB
@ DCO_RGB
Definition: dnn_interface.h:47
DNNExecBaseParams::output_names
const char ** output_names
Definition: dnn_interface.h:83
FramePrePostProc
int(* FramePrePostProc)(AVFrame *frame, DNNData *model, AVFilterContext *filter_ctx)
Definition: dnn_interface.h:94
DL_NONE
@ DL_NONE
Definition: dnn_interface.h:65
AVFilterContext
An instance of a filter.
Definition: avfilter.h:273
DNNModel
Definition: dnn_interface.h:98
DNNData::dims
int dims[4]
Definition: dnn_interface.h:72
DNN_TH
@ DNN_TH
Definition: dnn_interface.h:38
dnn_get_height_idx_by_layout
static int dnn_get_height_idx_by_layout(DNNLayout layout)
Definition: dnn_interface.h:214
ff_dnn_init_child_class
void ff_dnn_init_child_class(DnnContext *ctx)
Definition: dnn_interface.c:104
dnn_get_channel_idx_by_layout
static int dnn_get_channel_idx_by_layout(DNNLayout layout)
Definition: dnn_interface.h:219
ff_dnn_child_class_iterate_with_mask
const AVClass * ff_dnn_child_class_iterate_with_mask(void **iter, uint32_t backend_mask)
Definition: dnn_interface.c:134
TFOptions
Definition: dnn_interface.h:121
OVOptions
Definition: dnn_interface.h:127
DNNExecBaseParams
Definition: dnn_interface.h:81
DNNModel::get_input
int(* get_input)(struct DNNModel *model, DNNData *input, const char *input_name)
Definition: dnn_interface.h:105
DCO_BGR
@ DCO_BGR
Definition: dnn_interface.h:46
DAST_NOT_READY
@ DAST_NOT_READY
Definition: dnn_interface.h:53
DNNAsyncStatusType
DNNAsyncStatusType
Definition: dnn_interface.h:50
DFT_PROCESS_FRAME
@ DFT_PROCESS_FRAME
Definition: dnn_interface.h:59
DNNModule
Definition: dnn_interface.h:187
DNNModule::flush
int(* flush)(const DNNModel *model)
Definition: dnn_interface.h:197
DNNExecBaseParams::nb_output
uint32_t nb_output
Definition: dnn_interface.h:84
DnnContext::model_outputnames_string
char * model_outputnames_string
Definition: dnn_interface.h:159
DNNModule::execute_model
int(* execute_model)(const DNNModel *model, DNNExecBaseParams *exec_params)
Definition: dnn_interface.h:193