2018-05-25 17:31:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Sergey Lavrushkin
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* DNN inference engine interface.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef AVFILTER_DNN_INTERFACE_H
|
|
|
|
#define AVFILTER_DNN_INTERFACE_H
|
|
|
|
|
2019-04-25 02:14:33 +00:00
|
|
|
#include <stdint.h>
|
2020-08-28 04:51:44 +00:00
|
|
|
#include "libavutil/frame.h"
|
2020-12-31 01:19:39 +00:00
|
|
|
#include "avfilter.h"
|
2019-04-25 02:14:33 +00:00
|
|
|
|
2018-05-25 17:31:04 +00:00
|
|
|
typedef enum {DNN_SUCCESS, DNN_ERROR} DNNReturnType;
|
|
|
|
|
2020-05-25 07:38:09 +00:00
|
|
|
typedef enum {DNN_NATIVE, DNN_TF, DNN_OV} DNNBackendType;
|
2018-05-25 17:31:04 +00:00
|
|
|
|
2019-08-20 08:50:34 +00:00
|
|
|
typedef enum {DNN_FLOAT = 1, DNN_UINT8 = 4} DNNDataType;
|
2019-04-25 02:14:42 +00:00
|
|
|
|
2021-02-08 01:53:39 +00:00
|
|
|
typedef enum {
|
|
|
|
DCO_NONE,
|
|
|
|
DCO_BGR,
|
2021-05-06 08:46:07 +00:00
|
|
|
DCO_RGB,
|
2021-02-08 01:53:39 +00:00
|
|
|
} DNNColorOrder;
|
|
|
|
|
2020-11-18 06:28:06 +00:00
|
|
|
typedef enum {
|
|
|
|
DAST_FAIL, // something wrong
|
|
|
|
DAST_EMPTY_QUEUE, // no more inference result to get
|
|
|
|
DAST_NOT_READY, // all queued inferences are not finished
|
|
|
|
DAST_SUCCESS // got a result frame successfully
|
|
|
|
} DNNAsyncStatusType;
|
|
|
|
|
2021-02-07 06:35:22 +00:00
|
|
|
typedef enum {
|
|
|
|
DFT_NONE,
|
|
|
|
DFT_PROCESS_FRAME, // process the whole frame
|
|
|
|
DFT_ANALYTICS_DETECT, // detect from the whole frame
|
2021-03-16 05:02:56 +00:00
|
|
|
DFT_ANALYTICS_CLASSIFY, // classify for each bounding box
|
2021-02-07 06:35:22 +00:00
|
|
|
}DNNFunctionType;
|
|
|
|
|
2019-10-21 12:38:10 +00:00
|
|
|
typedef struct DNNData{
|
2019-04-25 02:14:42 +00:00
|
|
|
void *data;
|
|
|
|
int width, height, channels;
|
2021-02-08 01:53:39 +00:00
|
|
|
// dt and order together decide the color format
|
|
|
|
DNNDataType dt;
|
|
|
|
DNNColorOrder order;
|
2018-05-25 17:31:04 +00:00
|
|
|
} DNNData;
|
|
|
|
|
2021-04-01 02:06:06 +00:00
|
|
|
typedef struct DNNExecBaseParams {
|
|
|
|
const char *input_name;
|
|
|
|
const char **output_names;
|
|
|
|
uint32_t nb_output;
|
|
|
|
AVFrame *in_frame;
|
|
|
|
AVFrame *out_frame;
|
|
|
|
} DNNExecBaseParams;
|
|
|
|
|
2021-03-16 05:02:56 +00:00
|
|
|
typedef struct DNNExecClassificationParams {
|
|
|
|
DNNExecBaseParams base;
|
|
|
|
const char *target;
|
|
|
|
} DNNExecClassificationParams;
|
|
|
|
|
2021-03-01 11:23:20 +00:00
|
|
|
typedef int (*FramePrePostProc)(AVFrame *frame, DNNData *model, AVFilterContext *filter_ctx);
|
2021-03-09 06:51:42 +00:00
|
|
|
typedef int (*DetectPostProc)(AVFrame *frame, DNNData *output, uint32_t nb, AVFilterContext *filter_ctx);
|
2021-03-16 05:02:56 +00:00
|
|
|
typedef int (*ClassifyPostProc)(AVFrame *frame, DNNData *output, uint32_t bbox_index, AVFilterContext *filter_ctx);
|
2021-03-01 11:23:20 +00:00
|
|
|
|
2018-05-25 17:31:04 +00:00
|
|
|
typedef struct DNNModel{
|
|
|
|
// Stores model that can be different for different backends.
|
2018-07-27 16:34:02 +00:00
|
|
|
void *model;
|
2020-08-07 06:32:55 +00:00
|
|
|
// Stores options when the model is executed by the backend
|
|
|
|
const char *options;
|
2020-11-18 06:54:10 +00:00
|
|
|
// Stores FilterContext used for the interaction between AVFrame and DNNData
|
|
|
|
AVFilterContext *filter_ctx;
|
2021-02-07 06:35:22 +00:00
|
|
|
// Stores function type of the model
|
|
|
|
DNNFunctionType func_type;
|
2019-10-21 12:38:17 +00:00
|
|
|
// Gets model input information
|
|
|
|
// Just reuse struct DNNData here, actually the DNNData.data field is not needed.
|
|
|
|
DNNReturnType (*get_input)(void *model, DNNData *input, const char *input_name);
|
2020-09-11 14:15:04 +00:00
|
|
|
// Gets model output width/height with given input w/h
|
|
|
|
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);
|
2020-08-28 04:51:44 +00:00
|
|
|
// set the pre process to transfer data from AVFrame to DNNData
|
|
|
|
// the default implementation within DNN is used if it is not provided by the filter
|
2021-03-01 11:23:20 +00:00
|
|
|
FramePrePostProc frame_pre_proc;
|
2020-08-28 04:51:44 +00:00
|
|
|
// set the post process to transfer data from DNNData to AVFrame
|
|
|
|
// the default implementation within DNN is used if it is not provided by the filter
|
2021-03-01 11:23:20 +00:00
|
|
|
FramePrePostProc frame_post_proc;
|
2021-03-09 06:51:42 +00:00
|
|
|
// set the post process to interpret detect result from DNNData
|
|
|
|
DetectPostProc detect_post_proc;
|
2021-03-16 05:02:56 +00:00
|
|
|
// set the post process to interpret classify result from DNNData
|
|
|
|
ClassifyPostProc classify_post_proc;
|
2018-05-25 17:31:04 +00:00
|
|
|
} DNNModel;
|
|
|
|
|
|
|
|
// Stores pointers to functions for loading, executing, freeing DNN models for one of the backends.
|
|
|
|
typedef struct DNNModule{
|
|
|
|
// Loads model and parameters from given file. Returns NULL if it is not possible.
|
2021-02-07 06:35:22 +00:00
|
|
|
DNNModel *(*load_model)(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx);
|
2020-09-10 14:29:57 +00:00
|
|
|
// Executes model with specified input and output. Returns DNN_ERROR otherwise.
|
2021-04-01 02:06:06 +00:00
|
|
|
DNNReturnType (*execute_model)(const DNNModel *model, DNNExecBaseParams *exec_params);
|
2020-11-18 06:28:06 +00:00
|
|
|
// Executes model with specified input and output asynchronously. Returns DNN_ERROR otherwise.
|
2021-04-01 02:06:06 +00:00
|
|
|
DNNReturnType (*execute_model_async)(const DNNModel *model, DNNExecBaseParams *exec_params);
|
2020-11-18 06:28:06 +00:00
|
|
|
// Retrieve inference result.
|
2020-11-18 06:30:11 +00:00
|
|
|
DNNAsyncStatusType (*get_async_result)(const DNNModel *model, AVFrame **in, AVFrame **out);
|
2021-01-07 03:14:10 +00:00
|
|
|
// Flush all the pending tasks.
|
|
|
|
DNNReturnType (*flush)(const DNNModel *model);
|
2018-05-25 17:31:04 +00:00
|
|
|
// Frees memory allocated for model.
|
2018-07-27 16:34:02 +00:00
|
|
|
void (*free_model)(DNNModel **model);
|
2018-05-25 17:31:04 +00:00
|
|
|
} DNNModule;
|
|
|
|
|
|
|
|
// Initializes DNNModule depending on chosen backend.
|
2018-07-27 16:34:02 +00:00
|
|
|
DNNModule *ff_get_dnn_module(DNNBackendType backend_type);
|
2018-05-25 17:31:04 +00:00
|
|
|
|
|
|
|
#endif
|