You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
4.1KB

  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. * @file
  22. * DNN inference engine interface.
  23. */
  24. #ifndef AVFILTER_DNN_INTERFACE_H
  25. #define AVFILTER_DNN_INTERFACE_H
  26. #include <stdint.h>
  27. #include "libavutil/frame.h"
  28. #include "avfilter.h"
  29. typedef enum {DNN_SUCCESS, DNN_ERROR} DNNReturnType;
  30. typedef enum {DNN_NATIVE, DNN_TF, DNN_OV} DNNBackendType;
  31. typedef enum {DNN_FLOAT = 1, DNN_UINT8 = 4} DNNDataType;
  32. typedef enum {
  33. DAST_FAIL, // something wrong
  34. DAST_EMPTY_QUEUE, // no more inference result to get
  35. DAST_NOT_READY, // all queued inferences are not finished
  36. DAST_SUCCESS // got a result frame successfully
  37. } DNNAsyncStatusType;
  38. typedef struct DNNData{
  39. void *data;
  40. DNNDataType dt;
  41. int width, height, channels;
  42. } DNNData;
  43. typedef struct DNNModel{
  44. // Stores model that can be different for different backends.
  45. void *model;
  46. // Stores options when the model is executed by the backend
  47. const char *options;
  48. // Stores FilterContext used for the interaction between AVFrame and DNNData
  49. AVFilterContext *filter_ctx;
  50. // Gets model input information
  51. // Just reuse struct DNNData here, actually the DNNData.data field is not needed.
  52. DNNReturnType (*get_input)(void *model, DNNData *input, const char *input_name);
  53. // Gets model output width/height with given input w/h
  54. DNNReturnType (*get_output)(void *model, const char *input_name, int input_width, int input_height,
  55. const char *output_name, int *output_width, int *output_height);
  56. // set the pre process to transfer data from AVFrame to DNNData
  57. // the default implementation within DNN is used if it is not provided by the filter
  58. int (*pre_proc)(AVFrame *frame_in, DNNData *model_input, AVFilterContext *filter_ctx);
  59. // set the post process to transfer data from DNNData to AVFrame
  60. // the default implementation within DNN is used if it is not provided by the filter
  61. int (*post_proc)(AVFrame *frame_out, DNNData *model_output, AVFilterContext *filter_ctx);
  62. } DNNModel;
  63. // Stores pointers to functions for loading, executing, freeing DNN models for one of the backends.
  64. typedef struct DNNModule{
  65. // Loads model and parameters from given file. Returns NULL if it is not possible.
  66. DNNModel *(*load_model)(const char *model_filename, const char *options, AVFilterContext *filter_ctx);
  67. // Executes model with specified input and output. Returns DNN_ERROR otherwise.
  68. DNNReturnType (*execute_model)(const DNNModel *model, const char *input_name, AVFrame *in_frame,
  69. const char **output_names, uint32_t nb_output, AVFrame *out_frame);
  70. // Executes model with specified input and output asynchronously. Returns DNN_ERROR otherwise.
  71. DNNReturnType (*execute_model_async)(const DNNModel *model, const char *input_name, AVFrame *in_frame,
  72. const char **output_names, uint32_t nb_output, AVFrame *out_frame);
  73. // Retrieve inference result.
  74. DNNAsyncStatusType (*get_async_result)(const DNNModel *model, AVFrame **in, AVFrame **out);
  75. // Flush all the pending tasks.
  76. DNNReturnType (*flush)(const DNNModel *model);
  77. // Frees memory allocated for model.
  78. void (*free_model)(DNNModel **model);
  79. } DNNModule;
  80. // Initializes DNNModule depending on chosen backend.
  81. DNNModule *ff_get_dnn_module(DNNBackendType backend_type);
  82. #endif