2 * Copyright (c) 2017 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/imgutils.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavcodec/avfft.h"
28 #include "framesync.h"
32 #define MAX_THREADS 16
34 typedef struct ConvolveContext {
38 FFTContext *fft[4][MAX_THREADS];
39 FFTContext *ifft[4][MAX_THREADS];
46 FFTComplex *fft_hdata[4];
47 FFTComplex *fft_vdata[4];
48 FFTComplex *fft_hdata_impulse[4];
49 FFTComplex *fft_vdata_impulse[4];
58 #define OFFSET(x) offsetof(ConvolveContext, x)
59 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
61 static const AVOption convolve_options[] = {
62 { "planes", "set planes to convolve", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=7}, 0, 15, FLAGS },
63 { "impulse", "when to process impulses", OFFSET(impulse), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "impulse" },
64 { "first", "process only first impulse, ignore rest", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "impulse" },
65 { "all", "process all impulses", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "impulse" },
69 FRAMESYNC_DEFINE_CLASS(convolve, ConvolveContext, fs);
71 static int query_formats(AVFilterContext *ctx)
73 static const enum AVPixelFormat pixel_fmts_fftfilt[] = {
74 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
75 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
76 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
77 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
78 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
79 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
80 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
81 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
82 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
83 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
84 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
85 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
86 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
87 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
88 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
89 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
90 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
94 AVFilterFormats *fmts_list = ff_make_format_list(pixel_fmts_fftfilt);
96 return AVERROR(ENOMEM);
97 return ff_set_common_formats(ctx, fmts_list);
100 static int config_input_main(AVFilterLink *inlink)
102 ConvolveContext *s = inlink->dst->priv;
103 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
106 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
107 s->planewidth[0] = s->planewidth[3] = inlink->w;
108 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
109 s->planeheight[0] = s->planeheight[3] = inlink->h;
111 s->nb_planes = desc->nb_components;
112 s->depth = desc->comp[0].depth;
114 for (i = 0; i < s->nb_planes; i++) {
115 int w = s->planewidth[i];
116 int h = s->planeheight[i];
119 for (fft_bits = 1; 1 << fft_bits < n; fft_bits++);
121 s->fft_bits[i] = fft_bits;
122 s->fft_len[i] = 1 << s->fft_bits[i];
124 if (!(s->fft_hdata[i] = av_calloc(s->fft_len[i] + 1, s->fft_len[i] * sizeof(FFTComplex))))
125 return AVERROR(ENOMEM);
127 if (!(s->fft_vdata[i] = av_calloc(s->fft_len[i] + 1, s->fft_len[i] * sizeof(FFTComplex))))
128 return AVERROR(ENOMEM);
130 if (!(s->fft_hdata_impulse[i] = av_calloc(s->fft_len[i] + 1, s->fft_len[i] * sizeof(FFTComplex))))
131 return AVERROR(ENOMEM);
133 if (!(s->fft_vdata_impulse[i] = av_calloc(s->fft_len[i] + 1, s->fft_len[i] * sizeof(FFTComplex))))
134 return AVERROR(ENOMEM);
140 static int config_input_impulse(AVFilterLink *inlink)
142 AVFilterContext *ctx = inlink->dst;
144 if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
145 ctx->inputs[0]->h != ctx->inputs[1]->h) {
146 av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
147 return AVERROR(EINVAL);
149 if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
150 av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
151 return AVERROR(EINVAL);
157 typedef struct ThreadData {
158 FFTComplex *hdata, *vdata;
162 static int fft_horizontal(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
164 ConvolveContext *s = ctx->priv;
165 ThreadData *td = arg;
166 FFTComplex *hdata = td->hdata;
167 const int plane = td->plane;
169 int start = (n * jobnr ) / nb_jobs;
170 int end = (n * (jobnr+1)) / nb_jobs;
173 for (y = start; y < end; y++) {
174 av_fft_permute(s->fft[plane][jobnr], hdata + y * n);
175 av_fft_calc(s->fft[plane][jobnr], hdata + y * n);
181 static void get_input(ConvolveContext *s, FFTComplex *fft_hdata,
182 AVFrame *in, int w, int h, int n, int plane, float scale)
184 const int iw = (n - w) / 2, ih = (n - h) / 2;
188 for (y = 0; y < h; y++) {
189 const uint8_t *src = in->data[plane] + in->linesize[plane] * y;
191 for (x = 0; x < w; x++) {
192 fft_hdata[(y + ih) * n + iw + x].re = src[x] * scale;
193 fft_hdata[(y + ih) * n + iw + x].im = 0;
196 for (x = 0; x < iw; x++) {
197 fft_hdata[(y + ih) * n + x].re = fft_hdata[(y + ih) * n + iw].re;
198 fft_hdata[(y + ih) * n + x].im = 0;
201 for (x = n - iw; x < n; x++) {
202 fft_hdata[(y + ih) * n + x].re = fft_hdata[(y + ih) * n + n - iw - 1].re;
203 fft_hdata[(y + ih) * n + x].im = 0;
207 for (y = 0; y < ih; y++) {
208 for (x = 0; x < n; x++) {
209 fft_hdata[y * n + x].re = fft_hdata[ih * n + x].re;
210 fft_hdata[y * n + x].im = 0;
214 for (y = n - ih; y < n; y++) {
215 for (x = 0; x < n; x++) {
216 fft_hdata[y * n + x].re = fft_hdata[(n - ih - 1) * n + x].re;
217 fft_hdata[y * n + x].im = 0;
221 for (y = 0; y < h; y++) {
222 const uint16_t *src = (const uint16_t *)(in->data[plane] + in->linesize[plane] * y);
224 for (x = 0; x < w; x++) {
225 fft_hdata[(y + ih) * n + iw + x].re = src[x] * scale;
226 fft_hdata[(y + ih) * n + iw + x].im = 0;
229 for (x = 0; x < iw; x++) {
230 fft_hdata[(y + ih) * n + x].re = fft_hdata[(y + ih) * n + iw].re;
231 fft_hdata[(y + ih) * n + x].im = 0;
234 for (x = n - iw; x < n; x++) {
235 fft_hdata[(y + ih) * n + x].re = fft_hdata[(y + ih) * n + n - iw - 1].re;
236 fft_hdata[(y + ih) * n + x].im = 0;
240 for (y = 0; y < ih; y++) {
241 for (x = 0; x < n; x++) {
242 fft_hdata[y * n + x].re = fft_hdata[ih * n + x].re;
243 fft_hdata[y * n + x].im = 0;
247 for (y = n - ih; y < n; y++) {
248 for (x = 0; x < n; x++) {
249 fft_hdata[y * n + x].re = fft_hdata[(n - ih - 1) * n + x].re;
250 fft_hdata[y * n + x].im = 0;
256 static int fft_vertical(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
258 ConvolveContext *s = ctx->priv;
259 ThreadData *td = arg;
260 FFTComplex *hdata = td->hdata;
261 FFTComplex *vdata = td->vdata;
262 const int plane = td->plane;
264 int start = (n * jobnr ) / nb_jobs;
265 int end = (n * (jobnr+1)) / nb_jobs;
268 for (y = start; y < end; y++) {
269 for (x = 0; x < n; x++) {
270 vdata[y * n + x].re = hdata[x * n + y].re;
271 vdata[y * n + x].im = hdata[x * n + y].im;
274 av_fft_permute(s->fft[plane][jobnr], vdata + y * n);
275 av_fft_calc(s->fft[plane][jobnr], vdata + y * n);
281 static int ifft_vertical(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
283 ConvolveContext *s = ctx->priv;
284 ThreadData *td = arg;
285 FFTComplex *hdata = td->hdata;
286 FFTComplex *vdata = td->vdata;
287 const int plane = td->plane;
289 int start = (n * jobnr ) / nb_jobs;
290 int end = (n * (jobnr+1)) / nb_jobs;
293 for (y = start; y < end; y++) {
294 av_fft_permute(s->ifft[plane][jobnr], vdata + y * n);
295 av_fft_calc(s->ifft[plane][jobnr], vdata + y * n);
297 for (x = 0; x < n; x++) {
298 hdata[x * n + y].re = vdata[y * n + x].re;
299 hdata[x * n + y].im = vdata[y * n + x].im;
306 static int ifft_horizontal(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
308 ConvolveContext *s = ctx->priv;
309 ThreadData *td = arg;
310 FFTComplex *hdata = td->hdata;
311 const int plane = td->plane;
313 int start = (n * jobnr ) / nb_jobs;
314 int end = (n * (jobnr+1)) / nb_jobs;
317 for (y = start; y < end; y++) {
318 av_fft_permute(s->ifft[plane][jobnr], hdata + y * n);
319 av_fft_calc(s->ifft[plane][jobnr], hdata + y * n);
325 static void get_output(ConvolveContext *s, AVFrame *out,
326 int w, int h, int n, int plane)
328 FFTComplex *input = s->fft_hdata[plane];
329 const float scale = 1.f / (n * n);
330 const int max = (1 << s->depth) - 1;
331 const int hh = h / 2;
332 const int hw = w / 2;
336 for (y = 0; y < hh; y++) {
337 uint8_t *dst = out->data[plane] + (y + hh) * out->linesize[plane] + hw;
338 for (x = 0; x < hw; x++)
339 dst[x] = av_clip_uint8(input[y * n + x].re * scale);
341 for (y = 0; y < hh; y++) {
342 uint8_t *dst = out->data[plane] + (y + hh) * out->linesize[plane];
343 for (x = 0; x < hw; x++)
344 dst[x] = av_clip_uint8(input[y * n + n - hw + x].re * scale);
346 for (y = 0; y < hh; y++) {
347 uint8_t *dst = out->data[plane] + y * out->linesize[plane] + hw;
348 for (x = 0; x < hw; x++)
349 dst[x] = av_clip_uint8(input[(n - hh + y) * n + x].re * scale);
351 for (y = 0; y < hh; y++) {
352 uint8_t *dst = out->data[plane] + y * out->linesize[plane];
353 for (x = 0; x < hw; x++)
354 dst[x] = av_clip_uint8(input[(n - hh + y) * n + n - hw + x].re * scale);
357 for (y = 0; y < hh; y++) {
358 uint16_t *dst = (uint16_t *)(out->data[plane] + (y + hh) * out->linesize[plane] + hw * 2);
359 for (x = 0; x < hw; x++)
360 dst[x] = av_clip(input[y * n + x].re * scale, 0, max);
362 for (y = 0; y < hh; y++) {
363 uint16_t *dst = (uint16_t *)(out->data[plane] + (y + hh) * out->linesize[plane]);
364 for (x = 0; x < hw; x++)
365 dst[x] = av_clip(input[y * n + n - hw + x].re * scale, 0, max);
367 for (y = 0; y < hh; y++) {
368 uint16_t *dst = (uint16_t *)(out->data[plane] + y * out->linesize[plane] + hw * 2);
369 for (x = 0; x < hw; x++)
370 dst[x] = av_clip(input[(n - hh + y) * n + x].re * scale, 0, max);
372 for (y = 0; y < hh; y++) {
373 uint16_t *dst = (uint16_t *)(out->data[plane] + y * out->linesize[plane]);
374 for (x = 0; x < hw; x++)
375 dst[x] = av_clip(input[(n - hh + y) * n + n - hw + x].re * scale, 0, max);
380 static int complex_multiply(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
382 ThreadData *td = arg;
383 FFTComplex *input = td->hdata;
384 FFTComplex *filter = td->vdata;
386 int start = (n * jobnr ) / nb_jobs;
387 int end = (n * (jobnr+1)) / nb_jobs;
390 for (y = start; y < end; y++) {
393 for (x = 0; x < n; x++) {
394 FFTSample re, im, ire, iim;
396 re = input[yn + x].re;
397 im = input[yn + x].im;
398 ire = filter[yn + x].re;
399 iim = filter[yn + x].im;
401 input[yn + x].re = ire * re - iim * im;
402 input[yn + x].im = iim * re + ire * im;
409 static int do_convolve(FFFrameSync *fs)
411 AVFilterContext *ctx = fs->parent;
412 AVFilterLink *outlink = ctx->outputs[0];
413 ConvolveContext *s = ctx->priv;
414 AVFrame *mainpic = NULL, *impulsepic = NULL;
415 int ret, y, x, plane;
417 ret = ff_framesync_dualinput_get(fs, &mainpic, &impulsepic);
421 return ff_filter_frame(outlink, mainpic);
423 for (plane = 0; plane < s->nb_planes; plane++) {
424 FFTComplex *filter = s->fft_vdata_impulse[plane];
425 FFTComplex *input = s->fft_vdata[plane];
426 const int n = s->fft_len[plane];
427 const int w = s->planewidth[plane];
428 const int h = s->planeheight[plane];
432 if (!(s->planes & (1 << plane))) {
436 td.plane = plane, td.n = n;
437 get_input(s, s->fft_hdata[plane], mainpic, w, h, n, plane, 1.f);
439 td.hdata = s->fft_hdata[plane];
440 td.vdata = s->fft_vdata[plane];
442 ctx->internal->execute(ctx, fft_horizontal, &td, NULL, FFMIN3(MAX_THREADS, n, ff_filter_get_nb_threads(ctx)));
443 ctx->internal->execute(ctx, fft_vertical, &td, NULL, FFMIN3(MAX_THREADS, n, ff_filter_get_nb_threads(ctx)));
445 if ((!s->impulse && !s->got_impulse[plane]) || s->impulse) {
447 for (y = 0; y < h; y++) {
448 const uint8_t *src = (const uint8_t *)(impulsepic->data[plane] + y * impulsepic->linesize[plane]) ;
449 for (x = 0; x < w; x++) {
454 for (y = 0; y < h; y++) {
455 const uint16_t *src = (const uint16_t *)(impulsepic->data[plane] + y * impulsepic->linesize[plane]) ;
456 for (x = 0; x < w; x++) {
461 total = FFMAX(1, total);
463 get_input(s, s->fft_hdata_impulse[plane], impulsepic, w, h, n, plane, 1 / total);
465 td.hdata = s->fft_hdata_impulse[plane];
466 td.vdata = s->fft_vdata_impulse[plane];
468 ctx->internal->execute(ctx, fft_horizontal, &td, NULL, FFMIN3(MAX_THREADS, n, ff_filter_get_nb_threads(ctx)));
469 ctx->internal->execute(ctx, fft_vertical, &td, NULL, FFMIN3(MAX_THREADS, n, ff_filter_get_nb_threads(ctx)));
471 s->got_impulse[plane] = 1;
477 ctx->internal->execute(ctx, complex_multiply, &td, NULL, FFMIN3(MAX_THREADS, n, ff_filter_get_nb_threads(ctx)));
479 td.hdata = s->fft_hdata[plane];
480 td.vdata = s->fft_vdata[plane];
482 ctx->internal->execute(ctx, ifft_vertical, &td, NULL, FFMIN3(MAX_THREADS, n, ff_filter_get_nb_threads(ctx)));
483 ctx->internal->execute(ctx, ifft_horizontal, &td, NULL, FFMIN3(MAX_THREADS, n, ff_filter_get_nb_threads(ctx)));
484 get_output(s, mainpic, w, h, n, plane);
487 return ff_filter_frame(outlink, mainpic);
490 static int config_output(AVFilterLink *outlink)
492 AVFilterContext *ctx = outlink->src;
493 ConvolveContext *s = ctx->priv;
494 AVFilterLink *mainlink = ctx->inputs[0];
497 s->fs.on_event = do_convolve;
498 ret = ff_framesync_init_dualinput(&s->fs, ctx);
501 outlink->w = mainlink->w;
502 outlink->h = mainlink->h;
503 outlink->time_base = mainlink->time_base;
504 outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
505 outlink->frame_rate = mainlink->frame_rate;
507 if ((ret = ff_framesync_configure(&s->fs)) < 0)
510 for (i = 0; i < s->nb_planes; i++) {
511 for (j = 0; j < MAX_THREADS; j++) {
512 s->fft[i][j] = av_fft_init(s->fft_bits[i], 0);
513 s->ifft[i][j] = av_fft_init(s->fft_bits[i], 1);
514 if (!s->fft[i][j] || !s->ifft[i][j])
515 return AVERROR(ENOMEM);
522 static int activate(AVFilterContext *ctx)
524 ConvolveContext *s = ctx->priv;
525 return ff_framesync_activate(&s->fs);
528 static av_cold void uninit(AVFilterContext *ctx)
530 ConvolveContext *s = ctx->priv;
533 for (i = 0; i < 4; i++) {
534 av_freep(&s->fft_hdata[i]);
535 av_freep(&s->fft_vdata[i]);
536 av_freep(&s->fft_hdata_impulse[i]);
537 av_freep(&s->fft_vdata_impulse[i]);
539 for (j = 0; j < MAX_THREADS; j++) {
540 av_fft_end(s->fft[i][j]);
541 av_fft_end(s->ifft[i][j]);
545 ff_framesync_uninit(&s->fs);
548 static const AVFilterPad convolve_inputs[] = {
551 .type = AVMEDIA_TYPE_VIDEO,
552 .config_props = config_input_main,
555 .type = AVMEDIA_TYPE_VIDEO,
556 .config_props = config_input_impulse,
561 static const AVFilterPad convolve_outputs[] = {
564 .type = AVMEDIA_TYPE_VIDEO,
565 .config_props = config_output,
570 AVFilter ff_vf_convolve = {
572 .description = NULL_IF_CONFIG_SMALL("Convolve first video stream with second video stream."),
573 .preinit = convolve_framesync_preinit,
575 .query_formats = query_formats,
576 .activate = activate,
577 .priv_size = sizeof(ConvolveContext),
578 .priv_class = &convolve_class,
579 .inputs = convolve_inputs,
580 .outputs = convolve_outputs,
581 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,