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.

217 lines
7.2KB

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