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.

492 lines
17KB

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