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) {
60 av_log(ctx,
AV_LOG_ERROR,
"Found \"%s\" in model, but it is not input node\n", input_name);
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;
118 char header_expected[] =
"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 size =
sizeof(header_expected);
150 if (strncmp(buf, header_expected, size) != 0) {
158 if (version != major_version_expected) {
165 header_size = dnn_size;
176 model->
model = (
void *)native_model;
177 native_model->
model = model;
179 #if !HAVE_PTHREAD_CANCEL 182 "on this build (pthread support is required)\n");
186 avio_seek(model_file_context, file_size - 8, SEEK_SET);
190 avio_seek(model_file_context, header_size, SEEK_SET);
193 if (!native_model->
layers){
202 for (layer = 0; layer < native_model->
layers_num; ++layer){
210 native_model->
layers[layer].
type = layer_type;
215 dnn_size += parsed_size;
228 oprd = &native_model->
operands[operand_index];
233 dnn_size += name_len;
251 if (dnn_size != file_size){
269 const char **output_names, uint32_t nb_output,
AVFrame *out_frame,
285 if (strcmp(oprd->name, input_name) == 0) {
299 oprd->dims[1] = in_frame->
height;
300 oprd->dims[2] = in_frame->
width;
304 if (oprd->length <= 0) {
314 input.height = oprd->dims[1];
315 input.width = oprd->dims[2];
316 input.channels = oprd->dims[3];
317 input.data = oprd->data;
318 input.dt = oprd->data_type;
327 if (nb_output != 1) {
334 for (layer = 0; layer < native_model->
layers_num; ++layer){
346 for (uint32_t
i = 0;
i < nb_output; ++
i) {
348 const char *output_name = output_names[
i];
350 if (strcmp(native_model->
operands[j].
name, output_name) == 0) {
383 const char **output_names, uint32_t nb_output,
AVFrame *out_frame)
398 return execute_model_native(model, input_name, in_frame, output_names, nb_output, out_frame, 1);
404 for (
int i = 0;
i < 4; ++
i)
405 result *= oprd->
dims[
i];
413 uint64_t
len =
sizeof(float);
414 for (
int i = 0;
i < 4;
i++) {
415 len *= oprd->
dims[
i];
430 if ((*model)->model) {
433 for (layer = 0; layer < native_model->
layers_num; ++layer){
445 for (uint32_t operand = 0; operand < native_model->
operands_num; ++operand)
int avio_open(AVIOContext **s, const char *url, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
int8_t isNHWC
NHWC if 1, otherwise NCHW.
int64_t avio_size(AVIOContext *s)
Get the filesize.
This structure describes decoded (raw) audio or video data.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
DNN inference functions interface for native backend.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
#define AVIO_FLAG_READ
read-only
const char * av_default_item_name(void *ptr)
Return the context name.
int32_t calculate_operand_dims_count(const DnnOperand *oprd)
int(* post_proc)(AVFrame *frame_out, DNNData *model_output, AVFilterContext *filter_ctx)
DNNModel * ff_dnn_load_model_native(const char *model_filename, const char *options, AVFilterContext *filter_ctx)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
DNNOperandType type
input/output/intermediate operand of the network
DNNReturnType proc_from_dnn_to_frame(AVFrame *frame, DNNData *output, void *log_ctx)
int32_t input_operand_indexes[4]
a layer can have multiple inputs and one output.
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.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
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
int(* pre_proc)(AVFrame *frame_in, DNNData *model_input, AVFilterContext *filter_ctx)
DNNDataType data_type
support different kinds of data type such as float, half float, int8 etc, first support float now...
DNNReturnType(* get_input)(void *model, DNNData *input, const char *input_name)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
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)
unsigned int avio_rl32(AVIOContext *s)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
void * data
data pointer with data length in bytes.
simple assert() macros that are a bit more flexible than ISO C assert().
const LayerFunc layer_funcs[DLT_COUNT]
static FilteringContext * filter_ctx
int32_t dims[4]
there are two memory layouts, NHWC or NCHW, so we use dims, dims[0] is Number.
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)
char name[128]
to avoid possible memory leak, do not use char *name
void ff_dnn_free_model_native(DNNModel **model)
const AVClass dnn_native_class
static const AVOption dnn_native_options[]
static DNNReturnType get_input_native(void *model, DNNData *input, const char *input_name)
DNN input&output process between AVFrame and DNNData.
Describe the class of an AVClass context structure.
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
DNNLayerType
the enum value of DNNLayerType should not be changed, the same values are used in convert_from_tensor...
const OptionDef options[]
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 proc_from_frame_to_dnn(AVFrame *frame, DNNData *input, void *log_ctx)
int32_t calculate_operand_data_length(const DnnOperand *oprd)
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)
and forward the result(frame or status change) to the corresponding input.If nothing is possible
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
AVFilterContext * filter_ctx
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
int32_t output_operand_index