32 #define OFFSET(x) offsetof(NativeContext, x)
33 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
48 const char **output_names, uint32_t nb_output,
AVFrame *out_frame,
58 if (strcmp(oprd->
name, input_name) == 0) {
78 const char *output_name,
int *output_width,
int *output_height)
99 in_frame->
width = input_width;
100 in_frame->
height = input_height;
103 *output_width = out_frame->
width;
104 *output_height = out_frame->
height;
117 #define DNN_NATIVE_MAGIC "FFMPEGDNNNATIVE"
121 int version, header_size, major_version_expected = 1;
124 int file_size, dnn_size, parsed_size;
131 file_size =
avio_size(model_file_context);
141 if (
avio_read(model_file_context, buf,
sizeof(buf)) !=
sizeof(buf) ||
144 dnn_size =
sizeof(buf);
148 if (
version != major_version_expected) {
155 header_size = dnn_size;
161 model->
model = native_model;
167 native_model->
model = model;
169 #if !HAVE_PTHREAD_CANCEL
172 "on this build (pthread support is required)\n");
176 avio_seek(model_file_context, file_size - 8, SEEK_SET);
180 avio_seek(model_file_context, header_size, SEEK_SET);
183 if (!native_model->
layers){
192 for (layer = 0; layer < native_model->
layers_num; ++layer){
200 native_model->
layers[layer].
type = layer_type;
205 dnn_size += parsed_size;
218 oprd = &native_model->
operands[operand_index];
223 dnn_size += name_len;
243 if (dnn_size != file_size){
262 const char **output_names, uint32_t nb_output,
AVFrame *out_frame,
278 if (strcmp(oprd->
name, input_name) == 0) {
320 if (nb_output != 1) {
327 for (layer = 0; layer < native_model->
layers_num; ++layer){
339 for (uint32_t
i = 0;
i < nb_output; ++
i) {
341 const char *output_name = output_names[
i];
343 if (strcmp(native_model->
operands[j].
name, output_name) == 0) {
376 const char **output_names, uint32_t nb_output,
AVFrame *out_frame)
391 return execute_model_native(model, input_name, in_frame, output_names, nb_output, out_frame, 1);
397 for (
int i = 0;
i < 4; ++
i)
398 result *= oprd->
dims[
i];
406 uint64_t
len =
sizeof(float);
407 for (
int i = 0;
i < 4;
i++) {
423 if ((*model)->model) {
424 native_model = (*model)->
model;
425 if (native_model->
layers) {
426 for (layer = 0; layer < native_model->
layers_num; ++layer){
438 for (uint32_t operand = 0; operand < native_model->
operands_num; ++operand)
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
#define AVIO_FLAG_READ
read-only
int avio_open(AVIOContext **s, const char *url, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
int64_t avio_size(AVIOContext *s)
Get the filesize.
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
unsigned int avio_rl32(AVIOContext *s)
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
void ff_dnn_free_model_native(DNNModel **model)
static const AVOption dnn_native_options[]
static DNNReturnType get_output_native(void *model, const char *input_name, int input_width, int input_height, const char *output_name, int *output_width, int *output_height)
int32_t ff_calculate_operand_dims_count(const DnnOperand *oprd)
static DNNReturnType execute_model_native(const DNNModel *model, const char *input_name, AVFrame *in_frame, const char **output_names, uint32_t nb_output, AVFrame *out_frame, int do_ioproc)
static const AVClass dnn_native_class
static DNNReturnType get_input_native(void *model, DNNData *input, const char *input_name)
DNNReturnType ff_dnn_execute_model_native(const DNNModel *model, const char *input_name, AVFrame *in_frame, const char **output_names, uint32_t nb_output, AVFrame *out_frame)
DNNModel * ff_dnn_load_model_native(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
int32_t ff_calculate_operand_data_length(const DnnOperand *oprd)
DNN inference functions interface for native backend.
DNNLayerType
the enum value of DNNLayerType should not be changed, the same values are used in convert_from_tensor...
const LayerFunc ff_layer_funcs[DLT_COUNT]
DNNReturnType ff_proc_from_dnn_to_frame(AVFrame *frame, DNNData *output, void *log_ctx)
DNNReturnType ff_proc_from_frame_to_dnn(AVFrame *frame, DNNData *input, DNNFunctionType func_type, void *log_ctx)
DNN input&output process between AVFrame and DNNData.
const OptionDef options[]
int av_opt_set_from_string(void *ctx, const char *opts, const char *const *shorthand, const char *key_val_sep, const char *pairs_sep)
Parse the key-value pairs list in opts.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
#define LIBAVUTIL_VERSION_INT
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
@ AV_CLASS_CATEGORY_FILTER
Describe the class of an AVClass context structure.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
This structure describes decoded (raw) audio or video data.
int(* pre_proc)(AVFrame *frame_in, DNNData *model_input, AVFilterContext *filter_ctx)
int(* post_proc)(AVFrame *frame_out, DNNData *model_output, AVFilterContext *filter_ctx)
DNNReturnType(* get_output)(void *model, const char *input_name, int input_width, int input_height, const char *output_name, int *output_width, int *output_height)
DNNReturnType(* get_input)(void *model, DNNData *input, const char *input_name)
AVFilterContext * filter_ctx
DNNFunctionType func_type
void * data
data pointer with data length in bytes.
DNNDataType data_type
support different kinds of data type such as float, half float, int8 etc, first support float now.
char name[128]
to avoid possible memory leak, do not use char *name
DNNOperandType type
input/output/intermediate operand of the network
int8_t isNHWC
NHWC if 1, otherwise NCHW.
int32_t dims[4]
there are two memory layouts, NHWC or NCHW, so we use dims, dims[0] is Number.
int32_t input_operand_indexes[4]
a layer can have multiple inputs and one output.
int32_t output_operand_index
static FilteringContext * filter_ctx