FFmpeg
dnn_backend_native.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 functions interface for native backend.
24  */
25 
26 
27 #ifndef AVFILTER_DNN_DNN_BACKEND_NATIVE_H
28 #define AVFILTER_DNN_DNN_BACKEND_NATIVE_H
29 
30 #include "../dnn_interface.h"
31 #include "libavformat/avio.h"
32 
33 /**
34  * the enum value of DNNLayerType should not be changed,
35  * the same values are used in convert_from_tensorflow.py
36  * and, it is used to index the layer execution/load function pointer.
37  */
38 typedef enum {
39  DLT_INPUT = 0,
47 } DNNLayerType;
48 
50 
51 typedef struct Layer{
53  /**
54  * a layer can have multiple inputs and one output.
55  * 4 is just a big enough number for input operands (increase it if necessary),
56  * do not use 'int32_t *input_operand_indexes', so we don't worry about mem leaks.
57  */
60  void *params;
61 } Layer;
62 
63 typedef struct DnnOperand{
64  /**
65  * there are two memory layouts, NHWC or NCHW, so we use dims,
66  * dims[0] is Number.
67  */
69 
70  /**
71  * input/output/intermediate operand of the network
72  */
74 
75  /**
76  * support different kinds of data type such as float, half float, int8 etc,
77  * first support float now.
78  */
80 
81  /**
82  * NHWC if 1, otherwise NCHW.
83  * let's first support NHWC only, this flag is for extensive usage.
84  */
85  int8_t isNHWC;
86 
87  /**
88  * to avoid possible memory leak, do not use char *name
89  */
90  char name[128];
91 
92  /**
93  * data pointer with data length in bytes.
94  * usedNumbersLeft is only valid for intermediate operand,
95  * it means how many layers still depend on this operand,
96  * todo: the memory can be reused when usedNumbersLeft is zero.
97  */
98  void *data;
101 }DnnOperand;
102 
103 typedef struct InputParams{
105 } InputParams;
106 
107 // Represents simple feed-forward convolutional network.
108 typedef struct ConvolutionalNetwork{
114  uint32_t nb_output;
116 
117 DNNModel *ff_dnn_load_model_native(const char *model_filename);
118 
119 DNNReturnType ff_dnn_execute_model_native(const DNNModel *model, DNNData *outputs, uint32_t nb_output);
120 
121 void ff_dnn_free_model_native(DNNModel **model);
122 
123 // NOTE: User must check for error (return value <= 0) to handle
124 // case like integer overflow.
127 #endif
DLT_COUNT
@ DLT_COUNT
Definition: dnn_backend_native.h:46
InputParams
Definition: dnn_backend_native.h:103
DnnOperand::isNHWC
int8_t isNHWC
NHWC if 1, otherwise NCHW.
Definition: dnn_backend_native.h:85
ConvolutionalNetwork::output_indexes
int32_t * output_indexes
Definition: dnn_backend_native.h:113
ff_dnn_load_model_native
DNNModel * ff_dnn_load_model_native(const char *model_filename)
Definition: dnn_backend_native.c:118
DLT_INPUT
@ DLT_INPUT
Definition: dnn_backend_native.h:39
ConvolutionalNetwork::layers_num
int32_t layers_num
Definition: dnn_backend_native.h:110
DLT_MATH_BINARY
@ DLT_MATH_BINARY
Definition: dnn_backend_native.h:44
ConvolutionalNetwork::operands_num
int32_t operands_num
Definition: dnn_backend_native.h:112
ConvolutionalNetwork
Definition: dnn_backend_native.h:108
DNNLayerType
DNNLayerType
the enum value of DNNLayerType should not be changed, the same values are used in convert_from_tensor...
Definition: dnn_backend_native.h:38
DnnOperand::type
DNNOperandType type
input/output/intermediate operand of the network
Definition: dnn_backend_native.h:73
DLT_MAXIMUM
@ DLT_MAXIMUM
Definition: dnn_backend_native.h:43
Layer::type
DNNLayerType type
Definition: dnn_backend_native.h:52
DLT_CONV2D
@ DLT_CONV2D
Definition: dnn_backend_native.h:40
DnnOperand::name
char name[128]
to avoid possible memory leak, do not use char *name
Definition: dnn_backend_native.h:90
DnnOperand::data
void * data
data pointer with data length in bytes.
Definition: dnn_backend_native.h:98
DNNReturnType
DNNReturnType
Definition: dnn_interface.h:31
DnnOperand::data_type
DNNDataType data_type
support different kinds of data type such as float, half float, int8 etc, first support float now.
Definition: dnn_backend_native.h:79
DNNData
Definition: dnn_interface.h:37
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
InputParams::height
int height
Definition: dnn_backend_native.h:104
int32_t
int32_t
Definition: audio_convert.c:194
DOT_OUTPUT
@ DOT_OUTPUT
Definition: dnn_backend_native.h:49
Layer::params
void * params
Definition: dnn_backend_native.h:60
DnnOperand::dims
int32_t dims[4]
there are two memory layouts, NHWC or NCHW, so we use dims, dims[0] is Number.
Definition: dnn_backend_native.h:68
ConvolutionalNetwork::operands
DnnOperand * operands
Definition: dnn_backend_native.h:111
DOT_INTERMEDIATE
@ DOT_INTERMEDIATE
Definition: dnn_backend_native.h:49
ConvolutionalNetwork::layers
Layer * layers
Definition: dnn_backend_native.h:109
DNNOperandType
DNNOperandType
Definition: dnn_backend_native.h:49
DnnOperand::length
int32_t length
Definition: dnn_backend_native.h:99
DOT_INPUT
@ DOT_INPUT
Definition: dnn_backend_native.h:49
Layer::output_operand_index
int32_t output_operand_index
Definition: dnn_backend_native.h:59
DLT_MIRROR_PAD
@ DLT_MIRROR_PAD
Definition: dnn_backend_native.h:42
Layer
Definition: dnn_backend_native.h:51
Layer::input_operand_indexes
int32_t input_operand_indexes[4]
a layer can have multiple inputs and one output.
Definition: dnn_backend_native.h:58
DnnOperand::usedNumbersLeft
int32_t usedNumbersLeft
Definition: dnn_backend_native.h:100
avio.h
calculate_operand_data_length
int32_t calculate_operand_data_length(const DnnOperand *oprd)
Definition: dnn_backend_native.c:297
DNNDataType
DNNDataType
Definition: dnn_interface.h:35
ConvolutionalNetwork::nb_output
uint32_t nb_output
Definition: dnn_backend_native.h:114
DnnOperand
Definition: dnn_backend_native.h:63
DLT_MATH_UNARY
@ DLT_MATH_UNARY
Definition: dnn_backend_native.h:45
ff_dnn_free_model_native
void ff_dnn_free_model_native(DNNModel **model)
Definition: dnn_backend_native.c:309
DNNModel
Definition: dnn_interface.h:43
DLT_DEPTH_TO_SPACE
@ DLT_DEPTH_TO_SPACE
Definition: dnn_backend_native.h:41
InputParams::channels
int channels
Definition: dnn_backend_native.h:104
InputParams::width
int width
Definition: dnn_backend_native.h:104
ff_dnn_execute_model_native
DNNReturnType ff_dnn_execute_model_native(const DNNModel *model, DNNData *outputs, uint32_t nb_output)
Definition: dnn_backend_native.c:257
calculate_operand_dims_count
int32_t calculate_operand_dims_count(const DnnOperand *oprd)
Definition: dnn_backend_native.c:288