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.

355 lines
13KB

  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 "libavformat/avio.h"
  31. #include "libswscale/swscale.h"
  32. #include "dnn_interface.h"
  33. typedef enum {SRCNN, ESPCN} SRModel;
  34. typedef struct SRContext {
  35. const AVClass *class;
  36. SRModel model_type;
  37. char* model_filename;
  38. DNNBackendType backend_type;
  39. DNNModule* dnn_module;
  40. DNNModel* model;
  41. DNNData input, output;
  42. int scale_factor;
  43. struct SwsContext* sws_context;
  44. int sws_slice_h;
  45. } SRContext;
  46. #define OFFSET(x) offsetof(SRContext, x)
  47. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
  48. static const AVOption sr_options[] = {
  49. { "model", "specifies what DNN model to use", OFFSET(model_type), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, 1, FLAGS, "model_type" },
  50. { "srcnn", "Super-Resolution Convolutional Neural Network model (scale factor should be specified for custom SRCNN model)", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "model_type" },
  51. { "espcn", "Efficient Sub-Pixel Convolutional Neural Network model", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "model_type" },
  52. { "dnn_backend", "DNN backend used for model execution", OFFSET(backend_type), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, 1, FLAGS, "backend" },
  53. { "native", "native backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "backend" },
  54. #if (CONFIG_LIBTENSORFLOW == 1)
  55. { "tensorflow", "tensorflow backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "backend" },
  56. #endif
  57. {"scale_factor", "scale factor for SRCNN model", OFFSET(scale_factor), AV_OPT_TYPE_INT, { .i64 = 2 }, 2, 4, FLAGS},
  58. { "model_filename", "path to model file specifying network architecture and its parameters", OFFSET(model_filename), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  59. { NULL }
  60. };
  61. AVFILTER_DEFINE_CLASS(sr);
  62. static av_cold int init(AVFilterContext* context)
  63. {
  64. SRContext* sr_context = context->priv;
  65. sr_context->dnn_module = ff_get_dnn_module(sr_context->backend_type);
  66. if (!sr_context->dnn_module){
  67. av_log(context, AV_LOG_ERROR, "could not create DNN module for requested backend\n");
  68. return AVERROR(ENOMEM);
  69. }
  70. if (!sr_context->model_filename){
  71. av_log(context, AV_LOG_VERBOSE, "model file for network was not specified, using default network for x2 upsampling\n");
  72. sr_context->scale_factor = 2;
  73. switch (sr_context->model_type){
  74. case SRCNN:
  75. sr_context->model = (sr_context->dnn_module->load_default_model)(DNN_SRCNN);
  76. break;
  77. case ESPCN:
  78. sr_context->model = (sr_context->dnn_module->load_default_model)(DNN_ESPCN);
  79. }
  80. }
  81. else{
  82. sr_context->model = (sr_context->dnn_module->load_model)(sr_context->model_filename);
  83. }
  84. if (!sr_context->model){
  85. av_log(context, AV_LOG_ERROR, "could not load DNN model\n");
  86. return AVERROR(EIO);
  87. }
  88. return 0;
  89. }
  90. static int query_formats(AVFilterContext* context)
  91. {
  92. const enum AVPixelFormat pixel_formats[] = {AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  93. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
  94. AV_PIX_FMT_NONE};
  95. AVFilterFormats* formats_list;
  96. formats_list = ff_make_format_list(pixel_formats);
  97. if (!formats_list){
  98. av_log(context, AV_LOG_ERROR, "could not create formats list\n");
  99. return AVERROR(ENOMEM);
  100. }
  101. return ff_set_common_formats(context, formats_list);
  102. }
  103. static int config_props(AVFilterLink* inlink)
  104. {
  105. AVFilterContext* context = inlink->dst;
  106. SRContext* sr_context = context->priv;
  107. AVFilterLink* outlink = context->outputs[0];
  108. DNNReturnType result;
  109. int sws_src_h, sws_src_w, sws_dst_h, sws_dst_w;
  110. switch (sr_context->model_type){
  111. case SRCNN:
  112. sr_context->input.width = inlink->w * sr_context->scale_factor;
  113. sr_context->input.height = inlink->h * sr_context->scale_factor;
  114. break;
  115. case ESPCN:
  116. sr_context->input.width = inlink->w;
  117. sr_context->input.height = inlink->h;
  118. }
  119. sr_context->input.channels = 1;
  120. result = (sr_context->model->set_input_output)(sr_context->model->model, &sr_context->input, &sr_context->output);
  121. if (result != DNN_SUCCESS){
  122. av_log(context, AV_LOG_ERROR, "could not set input and output for the model\n");
  123. return AVERROR(EIO);
  124. }
  125. else{
  126. outlink->h = sr_context->output.height;
  127. outlink->w = sr_context->output.width;
  128. switch (sr_context->model_type){
  129. case SRCNN:
  130. sr_context->sws_context = sws_getContext(inlink->w, inlink->h, inlink->format,
  131. outlink->w, outlink->h, outlink->format, SWS_BICUBIC, NULL, NULL, NULL);
  132. if (!sr_context->sws_context){
  133. av_log(context, AV_LOG_ERROR, "could not create SwsContext\n");
  134. return AVERROR(ENOMEM);
  135. }
  136. sr_context->sws_slice_h = inlink->h;
  137. break;
  138. case ESPCN:
  139. if (inlink->format == AV_PIX_FMT_GRAY8){
  140. sr_context->sws_context = NULL;
  141. }
  142. else{
  143. sws_src_h = sr_context->input.height;
  144. sws_src_w = sr_context->input.width;
  145. sws_dst_h = sr_context->output.height;
  146. sws_dst_w = sr_context->output.width;
  147. switch (inlink->format){
  148. case AV_PIX_FMT_YUV420P:
  149. sws_src_h = (sws_src_h >> 1) + (sws_src_h % 2 != 0 ? 1 : 0);
  150. sws_src_w = (sws_src_w >> 1) + (sws_src_w % 2 != 0 ? 1 : 0);
  151. sws_dst_h = (sws_dst_h >> 1) + (sws_dst_h % 2 != 0 ? 1 : 0);
  152. sws_dst_w = (sws_dst_w >> 1) + (sws_dst_w % 2 != 0 ? 1 : 0);
  153. break;
  154. case AV_PIX_FMT_YUV422P:
  155. sws_src_w = (sws_src_w >> 1) + (sws_src_w % 2 != 0 ? 1 : 0);
  156. sws_dst_w = (sws_dst_w >> 1) + (sws_dst_w % 2 != 0 ? 1 : 0);
  157. break;
  158. case AV_PIX_FMT_YUV444P:
  159. break;
  160. case AV_PIX_FMT_YUV410P:
  161. sws_src_h = (sws_src_h >> 2) + (sws_src_h % 4 != 0 ? 1 : 0);
  162. sws_src_w = (sws_src_w >> 2) + (sws_src_w % 4 != 0 ? 1 : 0);
  163. sws_dst_h = (sws_dst_h >> 2) + (sws_dst_h % 4 != 0 ? 1 : 0);
  164. sws_dst_w = (sws_dst_w >> 2) + (sws_dst_w % 4 != 0 ? 1 : 0);
  165. break;
  166. case AV_PIX_FMT_YUV411P:
  167. sws_src_w = (sws_src_w >> 2) + (sws_src_w % 4 != 0 ? 1 : 0);
  168. sws_dst_w = (sws_dst_w >> 2) + (sws_dst_w % 4 != 0 ? 1 : 0);
  169. break;
  170. default:
  171. av_log(context, AV_LOG_ERROR, "could not create SwsContext for input pixel format");
  172. return AVERROR(EIO);
  173. }
  174. sr_context->sws_context = sws_getContext(sws_src_w, sws_src_h, AV_PIX_FMT_GRAY8,
  175. sws_dst_w, sws_dst_h, AV_PIX_FMT_GRAY8, SWS_BICUBIC, NULL, NULL, NULL);
  176. if (!sr_context->sws_context){
  177. av_log(context, AV_LOG_ERROR, "could not create SwsContext\n");
  178. return AVERROR(ENOMEM);
  179. }
  180. sr_context->sws_slice_h = sws_src_h;
  181. }
  182. }
  183. return 0;
  184. }
  185. }
  186. typedef struct ThreadData{
  187. uint8_t* data;
  188. int data_linesize, height, width;
  189. } ThreadData;
  190. static int uint8_to_float(AVFilterContext* context, void* arg, int jobnr, int nb_jobs)
  191. {
  192. SRContext* sr_context = context->priv;
  193. const ThreadData* td = arg;
  194. const int slice_start = (td->height * jobnr ) / nb_jobs;
  195. const int slice_end = (td->height * (jobnr + 1)) / nb_jobs;
  196. const uint8_t* src = td->data + slice_start * td->data_linesize;
  197. float* dst = sr_context->input.data + slice_start * td->width;
  198. int y, x;
  199. for (y = slice_start; y < slice_end; ++y){
  200. for (x = 0; x < td->width; ++x){
  201. dst[x] = (float)src[x] / 255.0f;
  202. }
  203. src += td->data_linesize;
  204. dst += td->width;
  205. }
  206. return 0;
  207. }
  208. static int float_to_uint8(AVFilterContext* context, void* arg, int jobnr, int nb_jobs)
  209. {
  210. SRContext* sr_context = context->priv;
  211. const ThreadData* td = arg;
  212. const int slice_start = (td->height * jobnr ) / nb_jobs;
  213. const int slice_end = (td->height * (jobnr + 1)) / nb_jobs;
  214. const float* src = sr_context->output.data + slice_start * td->width;
  215. uint8_t* dst = td->data + slice_start * td->data_linesize;
  216. int y, x;
  217. for (y = slice_start; y < slice_end; ++y){
  218. for (x = 0; x < td->width; ++x){
  219. dst[x] = (uint8_t)(255.0f * FFMIN(src[x], 1.0f));
  220. }
  221. src += td->width;
  222. dst += td->data_linesize;
  223. }
  224. return 0;
  225. }
  226. static int filter_frame(AVFilterLink* inlink, AVFrame* in)
  227. {
  228. AVFilterContext* context = inlink->dst;
  229. SRContext* sr_context = context->priv;
  230. AVFilterLink* outlink = context->outputs[0];
  231. AVFrame* out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  232. ThreadData td;
  233. int nb_threads;
  234. DNNReturnType dnn_result;
  235. if (!out){
  236. av_log(context, AV_LOG_ERROR, "could not allocate memory for output frame\n");
  237. av_frame_free(&in);
  238. return AVERROR(ENOMEM);
  239. }
  240. av_frame_copy_props(out, in);
  241. out->height = sr_context->output.height;
  242. out->width = sr_context->output.width;
  243. switch (sr_context->model_type){
  244. case SRCNN:
  245. sws_scale(sr_context->sws_context, (const uint8_t **)in->data, in->linesize,
  246. 0, sr_context->sws_slice_h, out->data, out->linesize);
  247. td.data = out->data[0];
  248. td.data_linesize = out->linesize[0];
  249. td.height = out->height;
  250. td.width = out->width;
  251. break;
  252. case ESPCN:
  253. if (sr_context->sws_context){
  254. sws_scale(sr_context->sws_context, (const uint8_t **)(in->data + 1), in->linesize + 1,
  255. 0, sr_context->sws_slice_h, out->data + 1, out->linesize + 1);
  256. sws_scale(sr_context->sws_context, (const uint8_t **)(in->data + 2), in->linesize + 2,
  257. 0, sr_context->sws_slice_h, out->data + 2, out->linesize + 2);
  258. }
  259. td.data = in->data[0];
  260. td.data_linesize = in->linesize[0];
  261. td.height = in->height;
  262. td.width = in->width;
  263. }
  264. nb_threads = ff_filter_get_nb_threads(context);
  265. context->internal->execute(context, uint8_to_float, &td, NULL, FFMIN(td.height, nb_threads));
  266. av_frame_free(&in);
  267. dnn_result = (sr_context->dnn_module->execute_model)(sr_context->model);
  268. if (dnn_result != DNN_SUCCESS){
  269. av_log(context, AV_LOG_ERROR, "failed to execute loaded model\n");
  270. return AVERROR(EIO);
  271. }
  272. td.data = out->data[0];
  273. td.data_linesize = out->linesize[0];
  274. td.height = out->height;
  275. td.width = out->width;
  276. context->internal->execute(context, float_to_uint8, &td, NULL, FFMIN(td.height, nb_threads));
  277. return ff_filter_frame(outlink, out);
  278. }
  279. static av_cold void uninit(AVFilterContext* context)
  280. {
  281. SRContext* sr_context = context->priv;
  282. if (sr_context->dnn_module){
  283. (sr_context->dnn_module->free_model)(&sr_context->model);
  284. av_freep(&sr_context->dnn_module);
  285. }
  286. if (sr_context->sws_context){
  287. sws_freeContext(sr_context->sws_context);
  288. }
  289. }
  290. static const AVFilterPad sr_inputs[] = {
  291. {
  292. .name = "default",
  293. .type = AVMEDIA_TYPE_VIDEO,
  294. .config_props = config_props,
  295. .filter_frame = filter_frame,
  296. },
  297. { NULL }
  298. };
  299. static const AVFilterPad sr_outputs[] = {
  300. {
  301. .name = "default",
  302. .type = AVMEDIA_TYPE_VIDEO,
  303. },
  304. { NULL }
  305. };
  306. AVFilter ff_vf_sr = {
  307. .name = "sr",
  308. .description = NULL_IF_CONFIG_SMALL("Apply DNN-based image super resolution to the input."),
  309. .priv_size = sizeof(SRContext),
  310. .init = init,
  311. .uninit = uninit,
  312. .query_formats = query_formats,
  313. .inputs = sr_inputs,
  314. .outputs = sr_outputs,
  315. .priv_class = &sr_class,
  316. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  317. };