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.

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