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.

169 lines
5.6KB

  1. /*
  2. * Copyright (c) 2019 Xuewei Meng
  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. * Filter implementing image derain filter using deep convolutional networks.
  23. * http://openaccess.thecvf.com/content_ECCV_2018/html/Xia_Li_Recurrent_Squeeze-and-Excitation_Context_ECCV_2018_paper.html
  24. */
  25. #include "libavformat/avio.h"
  26. #include "libavutil/opt.h"
  27. #include "avfilter.h"
  28. #include "dnn_interface.h"
  29. #include "formats.h"
  30. #include "internal.h"
  31. typedef struct DRContext {
  32. const AVClass *class;
  33. int filter_type;
  34. char *model_filename;
  35. DNNBackendType backend_type;
  36. DNNModule *dnn_module;
  37. DNNModel *model;
  38. } DRContext;
  39. #define OFFSET(x) offsetof(DRContext, x)
  40. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
  41. static const AVOption derain_options[] = {
  42. { "filter_type", "filter type(derain/dehaze)", OFFSET(filter_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS, "type" },
  43. { "derain", "derain filter flag", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "type" },
  44. { "dehaze", "dehaze filter flag", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "type" },
  45. { "dnn_backend", "DNN backend", OFFSET(backend_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS, "backend" },
  46. { "native", "native backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "backend" },
  47. #if (CONFIG_LIBTENSORFLOW == 1)
  48. { "tensorflow", "tensorflow backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "backend" },
  49. #endif
  50. { "model", "path to model file", OFFSET(model_filename), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
  51. { NULL }
  52. };
  53. AVFILTER_DEFINE_CLASS(derain);
  54. static int query_formats(AVFilterContext *ctx)
  55. {
  56. AVFilterFormats *formats;
  57. const enum AVPixelFormat pixel_fmts[] = {
  58. AV_PIX_FMT_RGB24,
  59. AV_PIX_FMT_NONE
  60. };
  61. formats = ff_make_format_list(pixel_fmts);
  62. return ff_set_common_formats(ctx, formats);
  63. }
  64. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  65. {
  66. AVFilterContext *ctx = inlink->dst;
  67. AVFilterLink *outlink = ctx->outputs[0];
  68. DRContext *dr_context = ctx->priv;
  69. DNNReturnType dnn_result;
  70. const char *model_output_name = "y";
  71. AVFrame *out;
  72. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  73. if (!out) {
  74. av_log(ctx, AV_LOG_ERROR, "could not allocate memory for output frame\n");
  75. av_frame_free(&in);
  76. return AVERROR(ENOMEM);
  77. }
  78. av_frame_copy_props(out, in);
  79. dnn_result = (dr_context->dnn_module->execute_model)(dr_context->model, "x", in, &model_output_name, 1, out);
  80. if (dnn_result != DNN_SUCCESS){
  81. av_log(ctx, AV_LOG_ERROR, "failed to execute model\n");
  82. av_frame_free(&in);
  83. return AVERROR(EIO);
  84. }
  85. av_frame_free(&in);
  86. return ff_filter_frame(outlink, out);
  87. }
  88. static av_cold int init(AVFilterContext *ctx)
  89. {
  90. DRContext *dr_context = ctx->priv;
  91. dr_context->dnn_module = ff_get_dnn_module(dr_context->backend_type);
  92. if (!dr_context->dnn_module) {
  93. av_log(ctx, AV_LOG_ERROR, "could not create DNN module for requested backend\n");
  94. return AVERROR(ENOMEM);
  95. }
  96. if (!dr_context->model_filename) {
  97. av_log(ctx, AV_LOG_ERROR, "model file for network is not specified\n");
  98. return AVERROR(EINVAL);
  99. }
  100. if (!dr_context->dnn_module->load_model) {
  101. av_log(ctx, AV_LOG_ERROR, "load_model for network is not specified\n");
  102. return AVERROR(EINVAL);
  103. }
  104. dr_context->model = (dr_context->dnn_module->load_model)(dr_context->model_filename, NULL, NULL);
  105. if (!dr_context->model) {
  106. av_log(ctx, AV_LOG_ERROR, "could not load DNN model\n");
  107. return AVERROR(EINVAL);
  108. }
  109. return 0;
  110. }
  111. static av_cold void uninit(AVFilterContext *ctx)
  112. {
  113. DRContext *dr_context = ctx->priv;
  114. if (dr_context->dnn_module) {
  115. (dr_context->dnn_module->free_model)(&dr_context->model);
  116. av_freep(&dr_context->dnn_module);
  117. }
  118. }
  119. static const AVFilterPad derain_inputs[] = {
  120. {
  121. .name = "default",
  122. .type = AVMEDIA_TYPE_VIDEO,
  123. .filter_frame = filter_frame,
  124. },
  125. { NULL }
  126. };
  127. static const AVFilterPad derain_outputs[] = {
  128. {
  129. .name = "default",
  130. .type = AVMEDIA_TYPE_VIDEO,
  131. },
  132. { NULL }
  133. };
  134. AVFilter ff_vf_derain = {
  135. .name = "derain",
  136. .description = NULL_IF_CONFIG_SMALL("Apply derain filter to the input."),
  137. .priv_size = sizeof(DRContext),
  138. .init = init,
  139. .uninit = uninit,
  140. .query_formats = query_formats,
  141. .inputs = derain_inputs,
  142. .outputs = derain_outputs,
  143. .priv_class = &derain_class,
  144. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  145. };