FFmpeg
Loading...
Searching...
No Matches
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
35typedef enum {
36 DNN_TF = 1,
37 DNN_OV = 1 << 1,
38 DNN_TH = 1 << 2,
39 DNN_ONNX = 1 << 3
41
42typedef enum {DNN_FLOAT = 1, DNN_UINT8 = 4} DNNDataType;
43
49
50typedef 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
57typedef 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
69
70typedef 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
88
93
95typedef int (*DetectPostProc)(AVFrame *frame, DNNData *output, uint32_t nb, AVFilterContext *filter_ctx);
96typedef int (*ClassifyPostProc)(AVFrame *frame, DNNData *output, uint32_t bbox_index, AVFilterContext *filter_ctx);
97
98typedef 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
121typedef struct TFOptions{
123
125} TFOptions;
126
127typedef struct OVOptions {
129
132 float scale;
133 float mean;
134} OVOptions;
135
136typedef struct THOptions {
139} THOptions;
140
141#if CONFIG_LIBONNXRUNTIME
142typedef struct ONNXOptions {
143 const AVClass *clazz;
144 int num_threads;
145} ONNXOptions;
146#endif
147
148typedef struct DNNModule DNNModule;
149
150typedef struct DnnContext {
152
154
160 int async;
161
163 uint32_t nb_outputs;
165
166 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.
187struct DNNModule {
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.
203const DNNModule *ff_get_dnn_module(DNNBackendType backend_type, void *log_ctx);
204
206void *ff_dnn_child_next(DnnContext *obj, void *prev);
207const 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
static FILE * out
static AVFormatContext * ctx
Main libavfilter public API header.
static AVFrame * frame
static int dnn_get_height_idx_by_layout(DNNLayout layout)
int(* FramePrePostProc)(AVFrame *frame, DNNData *model, AVFilterContext *filter_ctx)
void * ff_dnn_child_next(DnnContext *obj, void *prev)
const DNNModule * ff_get_dnn_module(DNNBackendType backend_type, void *log_ctx)
DNNAsyncStatusType
@ DAST_NOT_READY
@ DAST_EMPTY_QUEUE
@ DAST_SUCCESS
@ DAST_FAIL
int(* DetectPostProc)(AVFrame *frame, DNNData *output, uint32_t nb, AVFilterContext *filter_ctx)
DNNLayout
@ DL_NCHW
@ DL_NHWC
@ DL_NONE
const AVClass * ff_dnn_child_class_iterate_with_mask(void **iter, uint32_t backend_mask)
void ff_dnn_init_child_class(DnnContext *ctx)
DNNBackendType
@ DNN_OV
@ DNN_ONNX
@ DNN_TH
@ DNN_TF
DNNFunctionType
@ DFT_ANALYTICS_CLASSIFY
@ DFT_NONE
@ DFT_PROCESS_FRAME
@ DFT_ANALYTICS_DETECT
static int dnn_get_width_idx_by_layout(DNNLayout layout)
DNNDataType
@ DNN_UINT8
@ DNN_FLOAT
DNNColorOrder
@ DCO_RGB
@ DCO_BGR
@ DCO_NONE
int(* ClassifyPostProc)(AVFrame *frame, DNNData *output, uint32_t bbox_index, AVFilterContext *filter_ctx)
static int dnn_get_channel_idx_by_layout(DNNLayout layout)
reference-counted frame API
uint64_t layout
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
float scale
DNNDataType dt
int dims[4]
DNNColorOrder order
void * data
DNNLayout layout
float mean
const char ** output_names
const char * input_name
int(* get_input)(struct DNNModel *model, DNNData *input, const char *input_name)
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)
FramePrePostProc frame_pre_proc
ClassifyPostProc classify_post_proc
FramePrePostProc frame_post_proc
DetectPostProc detect_post_proc
AVFilterContext * filter_ctx
DNNFunctionType func_type
int(* execute_model)(const DNNModel *model, DNNExecBaseParams *exec_params)
DNNBackendType type
void(* free_model)(DNNModel **model)
DNNAsyncStatusType(* get_result)(const DNNModel *model, AVFrame **in, AVFrame **out)
int(* flush)(const DNNModel *model)
const AVClass clazz
const AVClass * clazz
char ** model_outputnames
const DNNModule * dnn_module
char * backend_options
char * model_inputname
char * model_outputnames_string
DNNBackendType backend_type
DNNModel * model
char * model_filename
uint32_t nb_outputs
int input_resizable
DNNLayout layout
const AVClass * clazz
char * sess_config
const AVClass * clazz
const AVClass * clazz
static FilteringContext * filter_ctx
Definition transcode.c:52