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 typedef enum {DNN_SUCCESS, DNN_ERROR} DNNReturnType; 34 35 typedef enum {DNN_NATIVE, DNN_TF, DNN_OV} DNNBackendType; 36 37 typedef enum {DNN_FLOAT = 1, DNN_UINT8 = 4} DNNDataType; 38 39 typedef enum { 40 DCO_NONE, 41 DCO_BGR, 42 } DNNColorOrder; 43 44 typedef enum { 45 DAST_FAIL, // something wrong 46 DAST_EMPTY_QUEUE, // no more inference result to get 47 DAST_NOT_READY, // all queued inferences are not finished 48 DAST_SUCCESS // got a result frame successfully 49 } DNNAsyncStatusType; 50 51 typedef enum { 52 DFT_NONE, 53 DFT_PROCESS_FRAME, // process the whole frame 54 DFT_ANALYTICS_DETECT, // detect from the whole frame 55 // we can add more such as detect_from_crop, classify_from_bbox, etc. 56 }DNNFunctionType; 57 58 typedef struct DNNData{ 59 void *data; 60 int width, height, channels; 61 // dt and order together decide the color format 62 DNNDataType dt; 63 DNNColorOrder order; 64 } DNNData; 65 66 typedef struct DNNModel{ 67 // Stores model that can be different for different backends. 68 void *model; 69 // Stores options when the model is executed by the backend 70 const char *options; 71 // Stores FilterContext used for the interaction between AVFrame and DNNData 72 AVFilterContext *filter_ctx; 73 // Stores function type of the model 74 DNNFunctionType func_type; 75 // Gets model input information 76 // Just reuse struct DNNData here, actually the DNNData.data field is not needed. 77 DNNReturnType (*get_input)(void *model, DNNData *input, const char *input_name); 78 // Gets model output width/height with given input w/h 79 DNNReturnType (*get_output)(void *model, const char *input_name, int input_width, int input_height, 80 const char *output_name, int *output_width, int *output_height); 81 // set the pre process to transfer data from AVFrame to DNNData 82 // the default implementation within DNN is used if it is not provided by the filter 83 int (*pre_proc)(AVFrame *frame_in, DNNData *model_input, AVFilterContext *filter_ctx); 84 // set the post process to transfer data from DNNData to AVFrame 85 // the default implementation within DNN is used if it is not provided by the filter 86 int (*post_proc)(AVFrame *frame_out, DNNData *model_output, AVFilterContext *filter_ctx); 87 } DNNModel; 88 89 // Stores pointers to functions for loading, executing, freeing DNN models for one of the backends. 90 typedef struct DNNModule{ 91 // Loads model and parameters from given file. Returns NULL if it is not possible. 92 DNNModel *(*load_model)(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx); 93 // Executes model with specified input and output. Returns DNN_ERROR otherwise. 94 DNNReturnType (*execute_model)(const DNNModel *model, const char *input_name, AVFrame *in_frame, 95 const char **output_names, uint32_t nb_output, AVFrame *out_frame); 96 // Executes model with specified input and output asynchronously. Returns DNN_ERROR otherwise. 97 DNNReturnType (*execute_model_async)(const DNNModel *model, const char *input_name, AVFrame *in_frame, 98 const char **output_names, uint32_t nb_output, AVFrame *out_frame); 99 // Retrieve inference result. 100 DNNAsyncStatusType (*get_async_result)(const DNNModel *model, AVFrame **in, AVFrame **out); 101 // Flush all the pending tasks. 102 DNNReturnType (*flush)(const DNNModel *model); 103 // Frees memory allocated for model. 104 void (*free_model)(DNNModel **model); 105 } DNNModule; 106 107 // Initializes DNNModule depending on chosen backend. 108 DNNModule *ff_get_dnn_module(DNNBackendType backend_type); 109 110 #endif 111