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.

207 lines
7.5KB

  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. * Filter implementing image super-resolution using deep convolutional networks.
  23. * https://arxiv.org/abs/1501.00092
  24. * https://arxiv.org/abs/1609.05158
  25. */
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavformat/avio.h"
  32. #include "libswscale/swscale.h"
  33. #include "dnn_filter_common.h"
  34. typedef struct SRContext {
  35. const AVClass *class;
  36. DnnContext dnnctx;
  37. int scale_factor;
  38. struct SwsContext *sws_uv_scale;
  39. int sws_uv_height;
  40. struct SwsContext *sws_pre_scale;
  41. } SRContext;
  42. #define OFFSET(x) offsetof(SRContext, x)
  43. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
  44. static const AVOption sr_options[] = {
  45. { "dnn_backend", "DNN backend used for model execution", OFFSET(dnnctx.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. { "scale_factor", "scale factor for SRCNN model", OFFSET(scale_factor), AV_OPT_TYPE_INT, { .i64 = 2 }, 2, 4, FLAGS },
  51. { "model", "path to model file specifying network architecture and its parameters", OFFSET(dnnctx.model_filename), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  52. { "input", "input name of the model", OFFSET(dnnctx.model_inputname), AV_OPT_TYPE_STRING, { .str = "x" }, 0, 0, FLAGS },
  53. { "output", "output name of the model", OFFSET(dnnctx.model_outputname), AV_OPT_TYPE_STRING, { .str = "y" }, 0, 0, FLAGS },
  54. { NULL }
  55. };
  56. AVFILTER_DEFINE_CLASS(sr);
  57. static av_cold int init(AVFilterContext *context)
  58. {
  59. SRContext *sr_context = context->priv;
  60. return ff_dnn_init(&sr_context->dnnctx, DFT_PROCESS_FRAME, context);
  61. }
  62. static int query_formats(AVFilterContext *context)
  63. {
  64. const enum AVPixelFormat pixel_formats[] = {AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  65. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
  66. AV_PIX_FMT_NONE};
  67. AVFilterFormats *formats_list;
  68. formats_list = ff_make_format_list(pixel_formats);
  69. if (!formats_list){
  70. av_log(context, AV_LOG_ERROR, "could not create formats list\n");
  71. return AVERROR(ENOMEM);
  72. }
  73. return ff_set_common_formats(context, formats_list);
  74. }
  75. static int config_output(AVFilterLink *outlink)
  76. {
  77. AVFilterContext *context = outlink->src;
  78. SRContext *ctx = context->priv;
  79. DNNReturnType result;
  80. AVFilterLink *inlink = context->inputs[0];
  81. int out_width, out_height;
  82. // have a try run in case that the dnn model resize the frame
  83. result = ff_dnn_get_output(&ctx->dnnctx, inlink->w, inlink->h, &out_width, &out_height);
  84. if (result != DNN_SUCCESS) {
  85. av_log(ctx, AV_LOG_ERROR, "could not get output from the model\n");
  86. return AVERROR(EIO);
  87. }
  88. if (inlink->w != out_width || inlink->h != out_height) {
  89. //espcn
  90. outlink->w = out_width;
  91. outlink->h = out_height;
  92. if (inlink->format != AV_PIX_FMT_GRAY8){
  93. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  94. int sws_src_h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  95. int sws_src_w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  96. int sws_dst_h = AV_CEIL_RSHIFT(outlink->h, desc->log2_chroma_h);
  97. int sws_dst_w = AV_CEIL_RSHIFT(outlink->w, desc->log2_chroma_w);
  98. ctx->sws_uv_scale = sws_getContext(sws_src_w, sws_src_h, AV_PIX_FMT_GRAY8,
  99. sws_dst_w, sws_dst_h, AV_PIX_FMT_GRAY8,
  100. SWS_BICUBIC, NULL, NULL, NULL);
  101. ctx->sws_uv_height = sws_src_h;
  102. }
  103. } else {
  104. //srcnn
  105. outlink->w = out_width * ctx->scale_factor;
  106. outlink->h = out_height * ctx->scale_factor;
  107. ctx->sws_pre_scale = sws_getContext(inlink->w, inlink->h, inlink->format,
  108. outlink->w, outlink->h, outlink->format,
  109. SWS_BICUBIC, NULL, NULL, NULL);
  110. }
  111. return 0;
  112. }
  113. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  114. {
  115. AVFilterContext *context = inlink->dst;
  116. SRContext *ctx = context->priv;
  117. AVFilterLink *outlink = context->outputs[0];
  118. AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  119. DNNReturnType dnn_result;
  120. if (!out){
  121. av_log(context, AV_LOG_ERROR, "could not allocate memory for output frame\n");
  122. av_frame_free(&in);
  123. return AVERROR(ENOMEM);
  124. }
  125. av_frame_copy_props(out, in);
  126. if (ctx->sws_pre_scale) {
  127. sws_scale(ctx->sws_pre_scale,
  128. (const uint8_t **)in->data, in->linesize, 0, in->height,
  129. out->data, out->linesize);
  130. dnn_result = ff_dnn_execute_model(&ctx->dnnctx, out, out);
  131. } else {
  132. dnn_result = ff_dnn_execute_model(&ctx->dnnctx, in, out);
  133. }
  134. if (dnn_result != DNN_SUCCESS){
  135. av_log(ctx, AV_LOG_ERROR, "failed to execute loaded model\n");
  136. av_frame_free(&in);
  137. av_frame_free(&out);
  138. return AVERROR(EIO);
  139. }
  140. if (ctx->sws_uv_scale) {
  141. sws_scale(ctx->sws_uv_scale, (const uint8_t **)(in->data + 1), in->linesize + 1,
  142. 0, ctx->sws_uv_height, out->data + 1, out->linesize + 1);
  143. sws_scale(ctx->sws_uv_scale, (const uint8_t **)(in->data + 2), in->linesize + 2,
  144. 0, ctx->sws_uv_height, out->data + 2, out->linesize + 2);
  145. }
  146. av_frame_free(&in);
  147. return ff_filter_frame(outlink, out);
  148. }
  149. static av_cold void uninit(AVFilterContext *context)
  150. {
  151. SRContext *sr_context = context->priv;
  152. ff_dnn_uninit(&sr_context->dnnctx);
  153. sws_freeContext(sr_context->sws_uv_scale);
  154. sws_freeContext(sr_context->sws_pre_scale);
  155. }
  156. static const AVFilterPad sr_inputs[] = {
  157. {
  158. .name = "default",
  159. .type = AVMEDIA_TYPE_VIDEO,
  160. .filter_frame = filter_frame,
  161. },
  162. { NULL }
  163. };
  164. static const AVFilterPad sr_outputs[] = {
  165. {
  166. .name = "default",
  167. .config_props = config_output,
  168. .type = AVMEDIA_TYPE_VIDEO,
  169. },
  170. { NULL }
  171. };
  172. AVFilter ff_vf_sr = {
  173. .name = "sr",
  174. .description = NULL_IF_CONFIG_SMALL("Apply DNN-based image super resolution to the input."),
  175. .priv_size = sizeof(SRContext),
  176. .init = init,
  177. .uninit = uninit,
  178. .query_formats = query_formats,
  179. .inputs = sr_inputs,
  180. .outputs = sr_outputs,
  181. .priv_class = &sr_class,
  182. };