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.

419 lines
14KB

  1. /*
  2. * Copyright (c) 2017 Paul B Mahol
  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. #include "libavutil/imgutils.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "libavcodec/avfft.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "framesync.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. typedef struct ConvolveContext {
  30. const AVClass *class;
  31. FFFrameSync fs;
  32. FFTContext *fft[4];
  33. FFTContext *ifft[4];
  34. int fft_bits[4];
  35. int fft_len[4];
  36. int planewidth[4];
  37. int planeheight[4];
  38. FFTComplex *fft_hdata[4];
  39. FFTComplex *fft_vdata[4];
  40. FFTComplex *fft_hdata_impulse[4];
  41. FFTComplex *fft_vdata_impulse[4];
  42. int depth;
  43. int planes;
  44. int impulse;
  45. int nb_planes;
  46. int got_impulse[4];
  47. } ConvolveContext;
  48. #define OFFSET(x) offsetof(ConvolveContext, x)
  49. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  50. static const AVOption convolve_options[] = {
  51. { "planes", "set planes to convolve", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=7}, 0, 15, FLAGS },
  52. { "impulse", "when to process impulses", OFFSET(impulse), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "impulse" },
  53. { "first", "process only first impulse, ignore rest", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "impulse" },
  54. { "all", "process all impulses", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "impulse" },
  55. { NULL },
  56. };
  57. FRAMESYNC_DEFINE_CLASS(convolve, ConvolveContext, fs);
  58. static int query_formats(AVFilterContext *ctx)
  59. {
  60. static const enum AVPixelFormat pixel_fmts_fftfilt[] = {
  61. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  62. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  63. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  64. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  65. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  66. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  67. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  68. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  69. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  70. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  71. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  72. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  73. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  74. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  75. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  76. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  77. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
  78. AV_PIX_FMT_NONE
  79. };
  80. AVFilterFormats *fmts_list = ff_make_format_list(pixel_fmts_fftfilt);
  81. if (!fmts_list)
  82. return AVERROR(ENOMEM);
  83. return ff_set_common_formats(ctx, fmts_list);
  84. }
  85. static int config_input_main(AVFilterLink *inlink)
  86. {
  87. ConvolveContext *s = inlink->dst->priv;
  88. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  89. int fft_bits, i;
  90. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  91. s->planewidth[0] = s->planewidth[3] = inlink->w;
  92. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  93. s->planeheight[0] = s->planeheight[3] = inlink->h;
  94. s->nb_planes = desc->nb_components;
  95. s->depth = desc->comp[0].depth;
  96. for (i = 0; i < s->nb_planes; i++) {
  97. int w = s->planewidth[i];
  98. int h = s->planeheight[i];
  99. int n = FFMAX(w, h) * 10/9;
  100. for (fft_bits = 1; 1 << fft_bits < n; fft_bits++);
  101. s->fft_bits[i] = fft_bits;
  102. s->fft_len[i] = 1 << s->fft_bits[i];
  103. if (!(s->fft_hdata[i] = av_calloc(s->fft_len[i], s->fft_len[i] * sizeof(FFTComplex))))
  104. return AVERROR(ENOMEM);
  105. if (!(s->fft_vdata[i] = av_calloc(s->fft_len[i], s->fft_len[i] * sizeof(FFTComplex))))
  106. return AVERROR(ENOMEM);
  107. if (!(s->fft_hdata_impulse[i] = av_calloc(s->fft_len[i], s->fft_len[i] * sizeof(FFTComplex))))
  108. return AVERROR(ENOMEM);
  109. if (!(s->fft_vdata_impulse[i] = av_calloc(s->fft_len[i], s->fft_len[i] * sizeof(FFTComplex))))
  110. return AVERROR(ENOMEM);
  111. }
  112. return 0;
  113. }
  114. static int config_input_impulse(AVFilterLink *inlink)
  115. {
  116. AVFilterContext *ctx = inlink->dst;
  117. if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
  118. ctx->inputs[0]->h != ctx->inputs[1]->h) {
  119. av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
  120. return AVERROR(EINVAL);
  121. }
  122. if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
  123. av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
  124. return AVERROR(EINVAL);
  125. }
  126. return 0;
  127. }
  128. static void fft_horizontal(ConvolveContext *s, FFTComplex *fft_hdata,
  129. AVFrame *in, int w, int h, int n, int plane, float scale)
  130. {
  131. int y, x;
  132. for (y = 0; y < h; y++) {
  133. if (s->depth == 8) {
  134. const uint8_t *src = in->data[plane] + in->linesize[plane] * y;
  135. for (x = 0; x < w; x++) {
  136. fft_hdata[y * n + x].re = src[x] * scale;
  137. fft_hdata[y * n + x].im = 0;
  138. }
  139. } else {
  140. const uint16_t *src = (const uint16_t *)(in->data[plane] + in->linesize[plane] * y);
  141. for (x = 0; x < w; x++) {
  142. fft_hdata[y * n + x].re = src[x] * scale;
  143. fft_hdata[y * n + x].im = 0;
  144. }
  145. }
  146. for (; x < n; x++) {
  147. fft_hdata[y * n + x].re = 0;
  148. fft_hdata[y * n + x].im = 0;
  149. }
  150. }
  151. for (; y < n; y++) {
  152. for (x = 0; x < n; x++) {
  153. fft_hdata[y * n + x].re = 0;
  154. fft_hdata[y * n + x].im = 0;
  155. }
  156. }
  157. for (y = 0; y < n; y++) {
  158. av_fft_permute(s->fft[plane], fft_hdata + y * n);
  159. av_fft_calc(s->fft[plane], fft_hdata + y * n);
  160. }
  161. }
  162. static void fft_vertical(ConvolveContext *s, FFTComplex *fft_hdata, FFTComplex *fft_vdata,
  163. int n, int plane)
  164. {
  165. int y, x;
  166. for (y = 0; y < n; y++) {
  167. for (x = 0; x < n; x++) {
  168. fft_vdata[y * n + x].re = fft_hdata[x * n + y].re;
  169. fft_vdata[y * n + x].im = fft_hdata[x * n + y].im;
  170. }
  171. for (; x < n; x++) {
  172. fft_vdata[y * n + x].re = 0;
  173. fft_vdata[y * n + x].im = 0;
  174. }
  175. av_fft_permute(s->fft[plane], fft_vdata + y * n);
  176. av_fft_calc(s->fft[plane], fft_vdata + y * n);
  177. }
  178. }
  179. static void ifft_vertical(ConvolveContext *s, int n, int plane)
  180. {
  181. int y, x;
  182. for (y = 0; y < n; y++) {
  183. av_fft_permute(s->ifft[plane], s->fft_vdata[plane] + y * n);
  184. av_fft_calc(s->ifft[plane], s->fft_vdata[plane] + y * n);
  185. for (x = 0; x < n; x++) {
  186. s->fft_hdata[plane][x * n + y].re = s->fft_vdata[plane][y * n + x].re;
  187. s->fft_hdata[plane][x * n + y].im = s->fft_vdata[plane][y * n + x].im;
  188. }
  189. }
  190. }
  191. static void ifft_horizontal(ConvolveContext *s, AVFrame *out,
  192. int w, int h, int n, int plane)
  193. {
  194. const float scale = 1.f / (n * n);
  195. const int max = (1 << s->depth) - 1;
  196. const int oh = h / 2;
  197. const int ow = w / 2;
  198. int y, x;
  199. for (y = 0; y < n; y++) {
  200. av_fft_permute(s->ifft[plane], s->fft_hdata[plane] + y * n);
  201. av_fft_calc(s->ifft[plane], s->fft_hdata[plane] + y * n);
  202. }
  203. if (s->depth == 8) {
  204. for (y = 0; y < h; y++) {
  205. uint8_t *dst = out->data[plane] + y * out->linesize[plane];
  206. for (x = 0; x < w; x++)
  207. dst[x] = av_clip_uint8(s->fft_hdata[plane][(y+oh) * n + x+ow].re * scale);
  208. }
  209. } else {
  210. for (y = 0; y < h; y++) {
  211. uint16_t *dst = (uint16_t *)(out->data[plane] + y * out->linesize[plane]);
  212. for (x = 0; x < w; x++)
  213. dst[x] = av_clip(s->fft_hdata[plane][(y+oh) * n + x+ow].re * scale, 0, max);
  214. }
  215. }
  216. }
  217. static int do_convolve(FFFrameSync *fs)
  218. {
  219. AVFilterContext *ctx = fs->parent;
  220. AVFilterLink *outlink = ctx->outputs[0];
  221. ConvolveContext *s = ctx->priv;
  222. AVFrame *mainpic = NULL, *impulsepic = NULL;
  223. int ret, y, x, plane;
  224. ret = ff_framesync_dualinput_get(fs, &mainpic, &impulsepic);
  225. if (ret < 0)
  226. return ret;
  227. if (!impulsepic)
  228. return ff_filter_frame(outlink, mainpic);
  229. for (plane = 0; plane < s->nb_planes; plane++) {
  230. const int n = s->fft_len[plane];
  231. const int w = s->planewidth[plane];
  232. const int h = s->planeheight[plane];
  233. float total = 0;
  234. if (!(s->planes & (1 << plane))) {
  235. continue;
  236. }
  237. fft_horizontal(s, s->fft_hdata[plane], mainpic, w, h, n, plane, 1.f);
  238. fft_vertical(s, s->fft_hdata[plane], s->fft_vdata[plane],
  239. n, plane);
  240. if ((!s->impulse && !s->got_impulse[plane]) || s->impulse) {
  241. if (s->depth == 8) {
  242. for (y = 0; y < h; y++) {
  243. const uint8_t *src = (const uint8_t *)(impulsepic->data[plane] + y * impulsepic->linesize[plane]) ;
  244. for (x = 0; x < w; x++) {
  245. total += src[x];
  246. }
  247. }
  248. } else {
  249. for (y = 0; y < h; y++) {
  250. const uint16_t *src = (const uint16_t *)(impulsepic->data[plane] + y * impulsepic->linesize[plane]) ;
  251. for (x = 0; x < w; x++) {
  252. total += src[x];
  253. }
  254. }
  255. }
  256. total = FFMAX(1, total);
  257. fft_horizontal(s, s->fft_hdata_impulse[plane], impulsepic, w, h, n, plane, 1 / total);
  258. fft_vertical(s, s->fft_hdata_impulse[plane], s->fft_vdata_impulse[plane],
  259. n, plane);
  260. s->got_impulse[plane] = 1;
  261. }
  262. for (y = 0; y < n; y++) {
  263. for (x = 0; x < n; x++) {
  264. FFTSample re, im, ire, iim;
  265. re = s->fft_vdata[plane][y*n + x].re;
  266. im = s->fft_vdata[plane][y*n + x].im;
  267. ire = s->fft_vdata_impulse[plane][y*n + x].re;
  268. iim = s->fft_vdata_impulse[plane][y*n + x].im;
  269. s->fft_vdata[plane][y*n + x].re = ire * re - iim * im;
  270. s->fft_vdata[plane][y*n + x].im = iim * re + ire * im;
  271. }
  272. }
  273. ifft_vertical(s, n, plane);
  274. ifft_horizontal(s, mainpic, w, h, n, plane);
  275. }
  276. return ff_filter_frame(outlink, mainpic);
  277. }
  278. static int config_output(AVFilterLink *outlink)
  279. {
  280. AVFilterContext *ctx = outlink->src;
  281. ConvolveContext *s = ctx->priv;
  282. AVFilterLink *mainlink = ctx->inputs[0];
  283. int ret, i;
  284. s->fs.on_event = do_convolve;
  285. ret = ff_framesync_init_dualinput(&s->fs, ctx);
  286. if (ret < 0)
  287. return ret;
  288. outlink->w = mainlink->w;
  289. outlink->h = mainlink->h;
  290. outlink->time_base = mainlink->time_base;
  291. outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
  292. outlink->frame_rate = mainlink->frame_rate;
  293. if ((ret = ff_framesync_configure(&s->fs)) < 0)
  294. return ret;
  295. for (i = 0; i < s->nb_planes; i++) {
  296. s->fft[i] = av_fft_init(s->fft_bits[i], 0);
  297. s->ifft[i] = av_fft_init(s->fft_bits[i], 1);
  298. if (!s->fft[i] || !s->ifft[i])
  299. return AVERROR(ENOMEM);
  300. }
  301. return 0;
  302. }
  303. static int activate(AVFilterContext *ctx)
  304. {
  305. ConvolveContext *s = ctx->priv;
  306. return ff_framesync_activate(&s->fs);
  307. }
  308. static av_cold void uninit(AVFilterContext *ctx)
  309. {
  310. ConvolveContext *s = ctx->priv;
  311. int i;
  312. for (i = 0; i < 4; i++) {
  313. av_freep(&s->fft_hdata[i]);
  314. av_freep(&s->fft_vdata[i]);
  315. av_freep(&s->fft_hdata_impulse[i]);
  316. av_freep(&s->fft_vdata_impulse[i]);
  317. av_fft_end(s->fft[i]);
  318. av_fft_end(s->ifft[i]);
  319. }
  320. ff_framesync_uninit(&s->fs);
  321. }
  322. static const AVFilterPad convolve_inputs[] = {
  323. {
  324. .name = "main",
  325. .type = AVMEDIA_TYPE_VIDEO,
  326. .config_props = config_input_main,
  327. },{
  328. .name = "impulse",
  329. .type = AVMEDIA_TYPE_VIDEO,
  330. .config_props = config_input_impulse,
  331. },
  332. { NULL }
  333. };
  334. static const AVFilterPad convolve_outputs[] = {
  335. {
  336. .name = "default",
  337. .type = AVMEDIA_TYPE_VIDEO,
  338. .config_props = config_output,
  339. },
  340. { NULL }
  341. };
  342. AVFilter ff_vf_convolve = {
  343. .name = "convolve",
  344. .description = NULL_IF_CONFIG_SMALL("Convolve first video stream with second video stream."),
  345. .preinit = convolve_framesync_preinit,
  346. .uninit = uninit,
  347. .query_formats = query_formats,
  348. .activate = activate,
  349. .priv_size = sizeof(ConvolveContext),
  350. .priv_class = &convolve_class,
  351. .inputs = convolve_inputs,
  352. .outputs = convolve_outputs,
  353. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  354. };