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.

484 lines
16KB

  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);
  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] + 1, s->fft_len[i] * sizeof(FFTComplex))))
  104. return AVERROR(ENOMEM);
  105. if (!(s->fft_vdata[i] = av_calloc(s->fft_len[i] + 1, s->fft_len[i] * sizeof(FFTComplex))))
  106. return AVERROR(ENOMEM);
  107. if (!(s->fft_hdata_impulse[i] = av_calloc(s->fft_len[i] + 1, s->fft_len[i] * sizeof(FFTComplex))))
  108. return AVERROR(ENOMEM);
  109. if (!(s->fft_vdata_impulse[i] = av_calloc(s->fft_len[i] + 1, 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. const int iw = (n - w) / 2, ih = (n - h) / 2;
  132. int y, x;
  133. for (y = 0; y < n; y++) {
  134. for (x = 0; x < n; x++) {
  135. fft_hdata[y * n + x].re = 0;
  136. fft_hdata[y * n + x].im = 0;
  137. }
  138. }
  139. if (s->depth == 8) {
  140. for (y = 0; y < h; y++) {
  141. const uint8_t *src = in->data[plane] + in->linesize[plane] * y;
  142. for (x = 0; x < w; x++) {
  143. fft_hdata[(y + ih) * n + iw + x].re = src[x] * scale;
  144. }
  145. for (x = 0; x < iw; x++) {
  146. fft_hdata[(y + ih) * n + x].re = fft_hdata[(y + ih) * n + iw].re;
  147. }
  148. for (x = n - iw; x < n; x++) {
  149. fft_hdata[(y + ih) * n + x].re = fft_hdata[(y + ih) * n + n - iw - 1].re;
  150. }
  151. }
  152. for (y = 0; y < ih; y++) {
  153. for (x = 0; x < n; x++) {
  154. fft_hdata[y * n + x].re = fft_hdata[ih * n + x].re;
  155. }
  156. }
  157. for (y = n - ih; y < n; y++) {
  158. for (x = 0; x < n; x++) {
  159. fft_hdata[y * n + x].re = fft_hdata[(n - ih - 1) * n + x].re;
  160. }
  161. }
  162. } else {
  163. for (y = 0; y < h; y++) {
  164. const uint16_t *src = (const uint16_t *)(in->data[plane] + in->linesize[plane] * y);
  165. for (x = 0; x < w; x++) {
  166. fft_hdata[(y + ih) * n + iw + x].re = src[x] * scale;
  167. }
  168. for (x = 0; x < iw; x++) {
  169. fft_hdata[(y + ih) * n + x].re = fft_hdata[(y + ih) * n + iw].re;
  170. }
  171. for (x = n - iw; x < n; x++) {
  172. fft_hdata[(y + ih) * n + x].re = fft_hdata[(y + ih) * n + n - iw - 1].re;
  173. }
  174. }
  175. for (y = 0; y < ih; y++) {
  176. for (x = 0; x < n; x++) {
  177. fft_hdata[y * n + x].re = fft_hdata[ih * n + x].re;
  178. }
  179. }
  180. for (y = n - ih; y < n; y++) {
  181. for (x = 0; x < n; x++) {
  182. fft_hdata[y * n + x].re = fft_hdata[(n - ih - 1) * n + x].re;
  183. }
  184. }
  185. }
  186. for (y = 0; y < n; y++) {
  187. av_fft_permute(s->fft[plane], fft_hdata + y * n);
  188. av_fft_calc(s->fft[plane], fft_hdata + y * n);
  189. }
  190. }
  191. static void fft_vertical(ConvolveContext *s, FFTComplex *fft_hdata, FFTComplex *fft_vdata,
  192. int n, int plane)
  193. {
  194. int y, x;
  195. for (y = 0; y < n; y++) {
  196. for (x = 0; x < n; x++) {
  197. fft_vdata[y * n + x].re = fft_hdata[x * n + y].re;
  198. fft_vdata[y * n + x].im = fft_hdata[x * n + y].im;
  199. }
  200. av_fft_permute(s->fft[plane], fft_vdata + y * n);
  201. av_fft_calc(s->fft[plane], fft_vdata + y * n);
  202. }
  203. }
  204. static void ifft_vertical(ConvolveContext *s, int n, int plane)
  205. {
  206. int y, x;
  207. for (y = 0; y < n; y++) {
  208. av_fft_permute(s->ifft[plane], s->fft_vdata[plane] + y * n);
  209. av_fft_calc(s->ifft[plane], s->fft_vdata[plane] + y * n);
  210. for (x = 0; x < n; x++) {
  211. s->fft_hdata[plane][x * n + y].re = s->fft_vdata[plane][y * n + x].re;
  212. s->fft_hdata[plane][x * n + y].im = s->fft_vdata[plane][y * n + x].im;
  213. }
  214. }
  215. }
  216. static void ifft_horizontal(ConvolveContext *s, AVFrame *out,
  217. int w, int h, int n, int plane)
  218. {
  219. const float scale = 1.f / (n * n);
  220. const int max = (1 << s->depth) - 1;
  221. const int hh = h / 2;
  222. const int hw = w / 2;
  223. int y, x;
  224. for (y = 0; y < n; y++) {
  225. av_fft_permute(s->ifft[plane], s->fft_hdata[plane] + y * n);
  226. av_fft_calc(s->ifft[plane], s->fft_hdata[plane] + y * n);
  227. }
  228. if (s->depth == 8) {
  229. for (y = 0; y < hh; y++) {
  230. uint8_t *dst = out->data[plane] + (y + hh) * out->linesize[plane] + hw;
  231. for (x = 0; x < hw; x++)
  232. dst[x] = av_clip_uint8(s->fft_hdata[plane][y * n + x].re * scale);
  233. }
  234. for (y = 0; y < hh; y++) {
  235. uint8_t *dst = out->data[plane] + (y + hh) * out->linesize[plane];
  236. for (x = 0; x < hw; x++)
  237. dst[x] = av_clip_uint8(s->fft_hdata[plane][y * n + n - hw + x].re * scale);
  238. }
  239. for (y = 0; y < hh; y++) {
  240. uint8_t *dst = out->data[plane] + y * out->linesize[plane] + hw;
  241. for (x = 0; x < hw; x++)
  242. dst[x] = av_clip_uint8(s->fft_hdata[plane][(n - hh + y) * n + x].re * scale);
  243. }
  244. for (y = 0; y < hh; y++) {
  245. uint8_t *dst = out->data[plane] + y * out->linesize[plane];
  246. for (x = 0; x < hw; x++)
  247. dst[x] = av_clip_uint8(s->fft_hdata[plane][(n - hh + y) * n + n - hw + x].re * scale);
  248. }
  249. } else {
  250. for (y = 0; y < hh; y++) {
  251. uint16_t *dst = (uint16_t *)(out->data[plane] + (y + hh) * out->linesize[plane] + hw * 2);
  252. for (x = 0; x < hw; x++)
  253. dst[x] = av_clip(s->fft_hdata[plane][y * n + x].re * scale, 0, max);
  254. }
  255. for (y = 0; y < hh; y++) {
  256. uint16_t *dst = (uint16_t *)(out->data[plane] + (y + hh) * out->linesize[plane]);
  257. for (x = 0; x < hw; x++)
  258. dst[x] = av_clip(s->fft_hdata[plane][y * n + n - hw + x].re * scale, 0, max);
  259. }
  260. for (y = 0; y < hh; y++) {
  261. uint16_t *dst = (uint16_t *)(out->data[plane] + y * out->linesize[plane] + hw * 2);
  262. for (x = 0; x < hw; x++)
  263. dst[x] = av_clip(s->fft_hdata[plane][(n - hh + y) * n + x].re * scale, 0, max);
  264. }
  265. for (y = 0; y < hh; y++) {
  266. uint16_t *dst = (uint16_t *)(out->data[plane] + y * out->linesize[plane]);
  267. for (x = 0; x < hw; x++)
  268. dst[x] = av_clip(s->fft_hdata[plane][(n - hh + y) * n + n - hw + x].re * scale, 0, max);
  269. }
  270. }
  271. }
  272. static int do_convolve(FFFrameSync *fs)
  273. {
  274. AVFilterContext *ctx = fs->parent;
  275. AVFilterLink *outlink = ctx->outputs[0];
  276. ConvolveContext *s = ctx->priv;
  277. AVFrame *mainpic = NULL, *impulsepic = NULL;
  278. int ret, y, x, plane;
  279. ret = ff_framesync_dualinput_get(fs, &mainpic, &impulsepic);
  280. if (ret < 0)
  281. return ret;
  282. if (!impulsepic)
  283. return ff_filter_frame(outlink, mainpic);
  284. for (plane = 0; plane < s->nb_planes; plane++) {
  285. const int n = s->fft_len[plane];
  286. const int w = s->planewidth[plane];
  287. const int h = s->planeheight[plane];
  288. float total = 0;
  289. if (!(s->planes & (1 << plane))) {
  290. continue;
  291. }
  292. fft_horizontal(s, s->fft_hdata[plane], mainpic, w, h, n, plane, 1.f);
  293. fft_vertical(s, s->fft_hdata[plane], s->fft_vdata[plane],
  294. n, plane);
  295. if ((!s->impulse && !s->got_impulse[plane]) || s->impulse) {
  296. if (s->depth == 8) {
  297. for (y = 0; y < h; y++) {
  298. const uint8_t *src = (const uint8_t *)(impulsepic->data[plane] + y * impulsepic->linesize[plane]) ;
  299. for (x = 0; x < w; x++) {
  300. total += src[x];
  301. }
  302. }
  303. } else {
  304. for (y = 0; y < h; y++) {
  305. const uint16_t *src = (const uint16_t *)(impulsepic->data[plane] + y * impulsepic->linesize[plane]) ;
  306. for (x = 0; x < w; x++) {
  307. total += src[x];
  308. }
  309. }
  310. }
  311. total = FFMAX(1, total);
  312. fft_horizontal(s, s->fft_hdata_impulse[plane], impulsepic, w, h, n, plane, 1 / total);
  313. fft_vertical(s, s->fft_hdata_impulse[plane], s->fft_vdata_impulse[plane],
  314. n, plane);
  315. s->got_impulse[plane] = 1;
  316. }
  317. for (y = 0; y < n; y++) {
  318. for (x = 0; x < n; x++) {
  319. FFTSample re, im, ire, iim;
  320. re = s->fft_vdata[plane][y*n + x].re;
  321. im = s->fft_vdata[plane][y*n + x].im;
  322. ire = s->fft_vdata_impulse[plane][y*n + x].re;
  323. iim = s->fft_vdata_impulse[plane][y*n + x].im;
  324. s->fft_vdata[plane][y*n + x].re = ire * re - iim * im;
  325. s->fft_vdata[plane][y*n + x].im = iim * re + ire * im;
  326. }
  327. }
  328. ifft_vertical(s, n, plane);
  329. ifft_horizontal(s, mainpic, w, h, n, plane);
  330. }
  331. return ff_filter_frame(outlink, mainpic);
  332. }
  333. static int config_output(AVFilterLink *outlink)
  334. {
  335. AVFilterContext *ctx = outlink->src;
  336. ConvolveContext *s = ctx->priv;
  337. AVFilterLink *mainlink = ctx->inputs[0];
  338. int ret, i;
  339. s->fs.on_event = do_convolve;
  340. ret = ff_framesync_init_dualinput(&s->fs, ctx);
  341. if (ret < 0)
  342. return ret;
  343. outlink->w = mainlink->w;
  344. outlink->h = mainlink->h;
  345. outlink->time_base = mainlink->time_base;
  346. outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
  347. outlink->frame_rate = mainlink->frame_rate;
  348. if ((ret = ff_framesync_configure(&s->fs)) < 0)
  349. return ret;
  350. for (i = 0; i < s->nb_planes; i++) {
  351. s->fft[i] = av_fft_init(s->fft_bits[i], 0);
  352. s->ifft[i] = av_fft_init(s->fft_bits[i], 1);
  353. if (!s->fft[i] || !s->ifft[i])
  354. return AVERROR(ENOMEM);
  355. }
  356. return 0;
  357. }
  358. static int activate(AVFilterContext *ctx)
  359. {
  360. ConvolveContext *s = ctx->priv;
  361. return ff_framesync_activate(&s->fs);
  362. }
  363. static av_cold void uninit(AVFilterContext *ctx)
  364. {
  365. ConvolveContext *s = ctx->priv;
  366. int i;
  367. for (i = 0; i < 4; i++) {
  368. av_freep(&s->fft_hdata[i]);
  369. av_freep(&s->fft_vdata[i]);
  370. av_freep(&s->fft_hdata_impulse[i]);
  371. av_freep(&s->fft_vdata_impulse[i]);
  372. av_fft_end(s->fft[i]);
  373. av_fft_end(s->ifft[i]);
  374. }
  375. ff_framesync_uninit(&s->fs);
  376. }
  377. static const AVFilterPad convolve_inputs[] = {
  378. {
  379. .name = "main",
  380. .type = AVMEDIA_TYPE_VIDEO,
  381. .config_props = config_input_main,
  382. },{
  383. .name = "impulse",
  384. .type = AVMEDIA_TYPE_VIDEO,
  385. .config_props = config_input_impulse,
  386. },
  387. { NULL }
  388. };
  389. static const AVFilterPad convolve_outputs[] = {
  390. {
  391. .name = "default",
  392. .type = AVMEDIA_TYPE_VIDEO,
  393. .config_props = config_output,
  394. },
  395. { NULL }
  396. };
  397. AVFilter ff_vf_convolve = {
  398. .name = "convolve",
  399. .description = NULL_IF_CONFIG_SMALL("Convolve first video stream with second video stream."),
  400. .preinit = convolve_framesync_preinit,
  401. .uninit = uninit,
  402. .query_formats = query_formats,
  403. .activate = activate,
  404. .priv_size = sizeof(ConvolveContext),
  405. .priv_class = &convolve_class,
  406. .inputs = convolve_inputs,
  407. .outputs = convolve_outputs,
  408. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  409. };