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.

213 lines
6.7KB

  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. char *model_filename;
  34. DNNBackendType backend_type;
  35. DNNModule *dnn_module;
  36. DNNModel *model;
  37. DNNInputData input;
  38. DNNData output;
  39. } DRContext;
  40. #define CLIP(x, min, max) (x < min ? min : (x > max ? max : x))
  41. #define OFFSET(x) offsetof(DRContext, x)
  42. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
  43. static const AVOption derain_options[] = {
  44. { "dnn_backend", "DNN backend", OFFSET(backend_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS, "backend" },
  45. { "native", "native backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "backend" },
  46. #if (CONFIG_LIBTENSORFLOW == 1)
  47. { "tensorflow", "tensorflow backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "backend" },
  48. #endif
  49. { "model", "path to model file", OFFSET(model_filename), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
  50. { NULL }
  51. };
  52. AVFILTER_DEFINE_CLASS(derain);
  53. static int query_formats(AVFilterContext *ctx)
  54. {
  55. AVFilterFormats *formats;
  56. const enum AVPixelFormat pixel_fmts[] = {
  57. AV_PIX_FMT_RGB24,
  58. AV_PIX_FMT_NONE
  59. };
  60. formats = ff_make_format_list(pixel_fmts);
  61. return ff_set_common_formats(ctx, formats);
  62. }
  63. static int config_inputs(AVFilterLink *inlink)
  64. {
  65. AVFilterContext *ctx = inlink->dst;
  66. DRContext *dr_context = ctx->priv;
  67. const char *model_output_name = "y";
  68. DNNReturnType result;
  69. dr_context->input.width = inlink->w;
  70. dr_context->input.height = inlink->h;
  71. dr_context->input.channels = 3;
  72. result = (dr_context->model->set_input_output)(dr_context->model->model, &dr_context->input, "x", &model_output_name, 1);
  73. if (result != DNN_SUCCESS) {
  74. av_log(ctx, AV_LOG_ERROR, "could not set input and output for the model\n");
  75. return AVERROR(EIO);
  76. }
  77. return 0;
  78. }
  79. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  80. {
  81. AVFilterContext *ctx = inlink->dst;
  82. AVFilterLink *outlink = ctx->outputs[0];
  83. DRContext *dr_context = ctx->priv;
  84. DNNReturnType dnn_result;
  85. int pad_size;
  86. AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  87. if (!out) {
  88. av_log(ctx, AV_LOG_ERROR, "could not allocate memory for output frame\n");
  89. av_frame_free(&in);
  90. return AVERROR(ENOMEM);
  91. }
  92. av_frame_copy_props(out, in);
  93. for (int i = 0; i < in->height; i++){
  94. for(int j = 0; j < in->width * 3; j++){
  95. int k = i * in->linesize[0] + j;
  96. int t = i * in->width * 3 + j;
  97. ((float *)dr_context->input.data)[t] = in->data[0][k] / 255.0;
  98. }
  99. }
  100. dnn_result = (dr_context->dnn_module->execute_model)(dr_context->model, &dr_context->output, 1);
  101. if (dnn_result != DNN_SUCCESS){
  102. av_log(ctx, AV_LOG_ERROR, "failed to execute model\n");
  103. return AVERROR(EIO);
  104. }
  105. out->height = dr_context->output.height;
  106. out->width = dr_context->output.width;
  107. outlink->h = dr_context->output.height;
  108. outlink->w = dr_context->output.width;
  109. pad_size = (in->height - out->height) >> 1;
  110. for (int i = 0; i < out->height; i++){
  111. for(int j = 0; j < out->width * 3; j++){
  112. int k = i * out->linesize[0] + j;
  113. int t = i * out->width * 3 + j;
  114. int t_in = (i + pad_size) * in->width * 3 + j + pad_size * 3;
  115. out->data[0][k] = CLIP((int)((((float *)dr_context->input.data)[t_in] - dr_context->output.data[t]) * 255), 0, 255);
  116. }
  117. }
  118. av_frame_free(&in);
  119. return ff_filter_frame(outlink, out);
  120. }
  121. static av_cold int init(AVFilterContext *ctx)
  122. {
  123. DRContext *dr_context = ctx->priv;
  124. dr_context->input.dt = DNN_FLOAT;
  125. dr_context->dnn_module = ff_get_dnn_module(dr_context->backend_type);
  126. if (!dr_context->dnn_module) {
  127. av_log(ctx, AV_LOG_ERROR, "could not create DNN module for requested backend\n");
  128. return AVERROR(ENOMEM);
  129. }
  130. if (!dr_context->model_filename) {
  131. av_log(ctx, AV_LOG_ERROR, "model file for network is not specified\n");
  132. return AVERROR(EINVAL);
  133. }
  134. if (!dr_context->dnn_module->load_model) {
  135. av_log(ctx, AV_LOG_ERROR, "load_model for network is not specified\n");
  136. return AVERROR(EINVAL);
  137. }
  138. dr_context->model = (dr_context->dnn_module->load_model)(dr_context->model_filename);
  139. if (!dr_context->model) {
  140. av_log(ctx, AV_LOG_ERROR, "could not load DNN model\n");
  141. return AVERROR(EINVAL);
  142. }
  143. return 0;
  144. }
  145. static av_cold void uninit(AVFilterContext *ctx)
  146. {
  147. DRContext *dr_context = ctx->priv;
  148. if (dr_context->dnn_module) {
  149. (dr_context->dnn_module->free_model)(&dr_context->model);
  150. av_freep(&dr_context->dnn_module);
  151. }
  152. }
  153. static const AVFilterPad derain_inputs[] = {
  154. {
  155. .name = "default",
  156. .type = AVMEDIA_TYPE_VIDEO,
  157. .config_props = config_inputs,
  158. .filter_frame = filter_frame,
  159. },
  160. { NULL }
  161. };
  162. static const AVFilterPad derain_outputs[] = {
  163. {
  164. .name = "default",
  165. .type = AVMEDIA_TYPE_VIDEO,
  166. },
  167. { NULL }
  168. };
  169. AVFilter ff_vf_derain = {
  170. .name = "derain",
  171. .description = NULL_IF_CONFIG_SMALL("Apply derain filter to the input."),
  172. .priv_size = sizeof(DRContext),
  173. .init = init,
  174. .uninit = uninit,
  175. .query_formats = query_formats,
  176. .inputs = derain_inputs,
  177. .outputs = derain_outputs,
  178. .priv_class = &derain_class,
  179. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  180. };