From patchwork Sat Sep 2 19:32:17 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 4955 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.15.201 with SMTP id 70csp1737223jao; Sat, 2 Sep 2017 12:39:00 -0700 (PDT) X-Received: by 10.223.156.25 with SMTP id f25mr2941406wrc.332.1504381140308; Sat, 02 Sep 2017 12:39:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1504381140; cv=none; d=google.com; s=arc-20160816; b=tUEfiLaRajq8242aFXWdw9s76FG5bFFLGetFhhRTollhZlTz5WthfSmrHDV/NdOXss Yns+8RZXXt4S4BGw8Fd/msRpE5EaPS8a+UZjKQZ9vcHgIdRmkrt2r6W9l1uhNEtYTYKZ /1rEsxF6PlI9jlNcktNeBnZmzVh8KBZx4fISPxC4swwgI5dr1EXRu/Si3hO2sLluKLPM MkbwwNv74fCBU1d+o4csOuj2XapPCM1jnrQFFQ6SkxkfkU51Ny1rygYrzx+5pIwksWrf wRduq2R2bI42tT04Y7vAgRMVOEY6o+HHCTCoTdWN+CkV1SCmHGYx4nyanvd3uJ+hwn4Q 9niQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=i0WKgn98ngMbihseTIkES4otyOkCO/QpYXMJ/3C6Fuk=; b=b8Wpkx1Pjdu3ItLnNmXtIVlheaaMt3vnN9JKNC7UCNij01cMcH/q1kyiA3935KuXL5 vHrwo0ra3b7EB+6Pk3Z2/59rgd89zecxmCFbYHDli8S2elkuMzAxk4bFTXxV2cDjmppl KpUgudMBTwmg0ZyN6H62eNLyioNEN7t1ojMYw5zaaS21uXWXTAVIlPMH4QdTne0BdMzQ JZZDLniSqU5JAwPyx+Rv6NeCiKs5vVbn50EYY1p7wr3Fdw8hFaTV+gCRMdlt3uymCGvX 0QnDDQkeV9pi5YCBUw/zGmZwL40JH/4n1VbpmsSSRC1q4bGV3uHoqSqG+LKBFf7ZC2YS 1F/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=rO8DZJ+L; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 193si2183842wmp.6.2017.09.02.12.38.59; Sat, 02 Sep 2017 12:39:00 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=rO8DZJ+L; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 51436689E34; Sat, 2 Sep 2017 22:38:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f196.google.com (mail-wr0-f196.google.com [209.85.128.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 64EAC689A6C for ; Sat, 2 Sep 2017 22:38:50 +0300 (EEST) Received: by mail-wr0-f196.google.com with SMTP id k94so1539340wrc.0 for ; Sat, 02 Sep 2017 12:38:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=VjEKGBOu2MiZfQpdfH5Kb1kucFh/8MXUhAtjvBiufik=; b=rO8DZJ+Lmh13bH09iTcKhnFAsAd4KjNZjXVZAhexF0ZlOUK5EPuhLmabeUmWEfeiEw mXffYSMQTqIfxj194sFK4cYOLwbykB3IiTaDZHkPAx21chHkaeUVVBKdWVIH+/8liiOF W64eeMmYTeCQRHg5u7X8DOAWkKinx7HItQQfIShR+OSBopC0pHP7WHQlLH8u+0IcPwYV Rfv7tsQpEBICM1De5tgWrqXnpmi72+ZN4MP9NbXxnI4I95LvDWZgqOz1sJoeUAbinIQP f3kZFREYwjrozoMd/Y9NOAXBTq4vrP1opfS4XfOcXJo6+Q5/Lp8CX/IcXfaHJInZvwPc +FtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=VjEKGBOu2MiZfQpdfH5Kb1kucFh/8MXUhAtjvBiufik=; b=FSa+Cx8qrXonwdlED8ECcskAxtomePuuybbzbeER3Y5AeWfHLnsIzh7E6y1zIArqgh o9pIOQ7bwhLl6R7sZtujL2GZ6gopgij893vlT56tZUb3/bFFdVk1BBiajsToU2t4TSzi qUvQlTYlhyLhZmsZ1T7tUmkyjrJR/VGi9+JWz7mR0iqMfWJG288jIfHdtPEbzqJo0zwB 4HE/KRskKc3bYcU2Iwj0TTmcgKTr1BvI5+s4DMWIPSqplN374Q0ZWMwQpobWG+bXIgRi LzP6opGJyEjUMtsbgdf0pCpzvmyK4EWXV7ikQstTRrj0j8+R6z/kL5sFZDOBByvPMNSq Gawg== X-Gm-Message-State: AHPjjUih8JhaZTRyaeLkWhJ6H9qP2F6K3JPmUj64z/2FVpocGznPr5ZD 2ebgYKeQba7VFIXL X-Google-Smtp-Source: ADKCNb6KeAsSIl1wkcwxuY16cDGd9ATT7zz5IDkkrS+DklWYndIp84Zf0mYbK7TLgMQPz0+BW6AIAw== X-Received: by 10.223.165.5 with SMTP id i5mr1926412wrb.204.1504380748210; Sat, 02 Sep 2017 12:32:28 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id m9sm5796533wrf.49.2017.09.02.12.32.26 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 02 Sep 2017 12:32:27 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sat, 2 Sep 2017 21:32:17 +0200 Message-Id: <20170902193217.27002-1-onemda@gmail.com> X-Mailer: git-send-email 2.9.3 Subject: [FFmpeg-devel] [PATCH] avfilter: add generic FFT video convolve filter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- doc/filters.texi | 11 ++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_convolve.c | 360 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 373 insertions(+) create mode 100644 libavfilter/vf_convolve.c diff --git a/doc/filters.texi b/doc/filters.texi index afcb99d..078fd32 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -5867,6 +5867,17 @@ For example to convert the input to SMPTE-240M, use the command: colorspace=smpte240m @end example +@section convolve + +Apply 2D convolution of video stream in frequency domain using second stream as impulse. + +The filter accepts the following options: + +@table @option +@item planes +Set which planes to process. +@end table + @section convolution Apply convolution 3x3 or 5x5 filter. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index ee840b0..d76d57c 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -149,6 +149,7 @@ OBJS-$(CONFIG_COLORLEVELS_FILTER) += vf_colorlevels.o OBJS-$(CONFIG_COLORMATRIX_FILTER) += vf_colormatrix.o OBJS-$(CONFIG_COLORSPACE_FILTER) += vf_colorspace.o colorspacedsp.o OBJS-$(CONFIG_CONVOLUTION_FILTER) += vf_convolution.o +OBJS-$(CONFIG_CONVOLVE_FILTER) += vf_convolve.o OBJS-$(CONFIG_COPY_FILTER) += vf_copy.o OBJS-$(CONFIG_COREIMAGE_FILTER) += vf_coreimage.o OBJS-$(CONFIG_COVER_RECT_FILTER) += vf_cover_rect.o lavfutils.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 8b9b9a4..890a7b8 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -161,6 +161,7 @@ static void register_all(void) REGISTER_FILTER(COLORMATRIX, colormatrix, vf); REGISTER_FILTER(COLORSPACE, colorspace, vf); REGISTER_FILTER(CONVOLUTION, convolution, vf); + REGISTER_FILTER(CONVOLVE, convolve, vf); REGISTER_FILTER(COPY, copy, vf); REGISTER_FILTER(COREIMAGE, coreimage, vf); REGISTER_FILTER(COVER_RECT, cover_rect, vf); diff --git a/libavfilter/vf_convolve.c b/libavfilter/vf_convolve.c new file mode 100644 index 0000000..925d03e --- /dev/null +++ b/libavfilter/vf_convolve.c @@ -0,0 +1,360 @@ +/* + * Copyright (c) 2017 Paul B Mahol + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/imgutils.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" +#include "libavcodec/avfft.h" + +#include "avfilter.h" +#include "formats.h" +#include "framesync2.h" +#include "internal.h" +#include "video.h" + +typedef struct ConvolveContext { + const AVClass *class; + FFFrameSync fs; + + FFTContext *fft[4]; + FFTContext *ifft[4]; + + int n[4]; + int fft_bits[4]; + int fft_len[4]; + int planewidth[4]; + int planeheight[4]; + + FFTComplex *fft_hdata[4]; + FFTComplex *fft_vdata[4]; + FFTComplex *fft_hdata_impulse[4]; + FFTComplex *fft_vdata_impulse[4]; + + int planes; +} ConvolveContext; + +#define OFFSET(x) offsetof(ConvolveContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM + +static const AVOption convolve_options[] = { + { "planes", "set planes to convolve", OFFSET(planes), AV_OPT_TYPE_INT, {.i64 = 15}, 0, 15, FLAGS }, + { NULL }, +}; + +FRAMESYNC_DEFINE_CLASS(convolve, ConvolveContext, fs); + +static int query_formats(AVFilterContext *ctx) +{ + static const enum AVPixelFormat pixel_fmts_fftfilt[] = { + AV_PIX_FMT_GRAY8, + AV_PIX_FMT_GBRP, + AV_PIX_FMT_YUV420P, + AV_PIX_FMT_YUV444P, + AV_PIX_FMT_NONE + }; + + AVFilterFormats *fmts_list = ff_make_format_list(pixel_fmts_fftfilt); + if (!fmts_list) + return AVERROR(ENOMEM); + return ff_set_common_formats(ctx, fmts_list); +} + +static int config_input_main(AVFilterLink *inlink) +{ + ConvolveContext *s = inlink->dst->priv; + int fft_bits, i; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + + s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); + s->planewidth[0] = s->planewidth[3] = inlink->w; + s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); + s->planeheight[0] = s->planeheight[3] = inlink->h; + + for (i = 0; i < desc->nb_components; i++) { + int w = s->planewidth[i]; + int h = s->planeheight[i]; + int n = FFMAX(w, h) + FFMIN(w, h) / 2; + + for (fft_bits = 1; 1 << fft_bits < n; fft_bits++); + + s->fft_bits[i] = fft_bits; + s->n[i] = n; + + s->fft_len[i] = 1 << s->fft_bits[i]; + + if (!(s->fft_hdata[i] = av_calloc(n, s->fft_len[i] * sizeof(FFTComplex)))) + return AVERROR(ENOMEM); + + if (!(s->fft_vdata[i] = av_calloc(s->fft_len[i], s->fft_len[i] * sizeof(FFTComplex)))) + return AVERROR(ENOMEM); + + if (!(s->fft_hdata_impulse[i] = av_calloc(n, s->fft_len[i] * sizeof(FFTComplex)))) + return AVERROR(ENOMEM); + + if (!(s->fft_vdata_impulse[i] = av_calloc(s->fft_len[i], s->fft_len[i] * sizeof(FFTComplex)))) + return AVERROR(ENOMEM); + } + + return 0; +} + +static int config_input_impulse(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + + if (ctx->inputs[0]->w != ctx->inputs[1]->w || + ctx->inputs[0]->h != ctx->inputs[1]->h) { + av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n"); + return AVERROR(EINVAL); + } + if (ctx->inputs[0]->format != ctx->inputs[1]->format) { + av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n"); + return AVERROR(EINVAL); + } + + return 0; +} + +static void fft_horizontal(ConvolveContext *s, FFTComplex *fft_hdata, int fft_len, + AVFrame *in, int w, int h, int n, int plane, float scale) +{ + int y, x; + + for (y = 0; y < h; y++) { + for (x = 0; x < w; x++) { + fft_hdata[y * fft_len + x].re = in->data[plane][in->linesize[plane]*y+x] * scale; + fft_hdata[y * fft_len + x].im = 0; + } + for (; x < fft_len; x++) { + fft_hdata[y * fft_len + x].re = 0; + fft_hdata[y * fft_len + x].im = 0; + } + + av_fft_permute(s->fft[plane], fft_hdata + y * fft_len); + av_fft_calc(s->fft[plane], fft_hdata + y * fft_len); + } + + for (; y < n; y++) { + for (x = 0; x < fft_len; x++) { + fft_hdata[y * fft_len + x].re = 0; + fft_hdata[y * fft_len + x].im = 0; + } + } +} + +static void fft_vertical(ConvolveContext *s, FFTComplex *fft_hdata, FFTComplex *fft_vdata, + int fft_len, int h, int plane) +{ + int y, x; + + for (y = 0; y < fft_len; y++) { + for (x = 0; x < h; x++) { + fft_vdata[y * fft_len + x].re = fft_hdata[x * fft_len + y].re; + fft_vdata[y * fft_len + x].im = fft_hdata[x * fft_len + y].im; + } + for (; x < fft_len; x++) { + fft_vdata[y * fft_len + x].re = 0; + fft_vdata[y * fft_len + x].im = 0; + } + av_fft_permute(s->fft[plane], fft_vdata + y * fft_len); + av_fft_calc(s->fft[plane], fft_vdata + y * fft_len); + } +} + +static void ifft_vertical(ConvolveContext *s, int fft_len, int h, int plane) +{ + int y, x; + + for (y = 0; y < fft_len; y++) { + av_fft_permute(s->ifft[plane], s->fft_vdata[plane] + y * fft_len); + av_fft_calc(s->ifft[plane], s->fft_vdata[plane] + y * fft_len); + for (x = 0; x < h; x++) { + s->fft_hdata[plane][x * fft_len + y].re = s->fft_vdata[plane][y * fft_len + x].re; + s->fft_hdata[plane][x * fft_len + y].im = s->fft_vdata[plane][y * fft_len + x].im; + } + } +} + +static void ifft_horizontal(ConvolveContext *s, AVFrame *out, int fft_len, + int w, int h, int n, int plane) +{ + const float scale = 1.f / (fft_len * fft_len); + int y, x; + + for (y = 0; y < n; y++) { + av_fft_permute(s->ifft[plane], s->fft_hdata[plane] + y * fft_len); + av_fft_calc(s->ifft[plane], s->fft_hdata[plane] + y * fft_len); + } + + for (y = 0; y < h; y++) { + for (x = 0; x < w; x++) + out->data[plane][out->linesize[plane] * y + x] = + av_clip(s->fft_hdata[plane][(y+h/2)* fft_len + x + w/2].re * scale, 0, 255); + } +} + +static int do_convolve(FFFrameSync *fs) +{ + AVFilterContext *ctx = fs->parent; + AVFilterLink *outlink = ctx->outputs[0]; + const AVPixFmtDescriptor *desc; + ConvolveContext *s = ctx->priv; + AVFrame *mainpic = NULL, *impulsepic = NULL; + int ret, y, x, plane; + + ret = ff_framesync2_dualinput_get(fs, &mainpic, &impulsepic); + if (ret < 0) + return ret; + if (!impulsepic) + return ff_filter_frame(ctx->outputs[0], mainpic); + + desc = av_pix_fmt_desc_get(outlink->format); + for (plane = 0; plane < desc->nb_components; plane++) { + const int fft_len = s->fft_len[plane]; + int w = s->planewidth[plane]; + int h = s->planeheight[plane]; + int n = s->n[plane]; + float total = 0; + + if (!(s->planes & (1 << plane))) + continue; + + fft_horizontal(s, s->fft_hdata[plane], fft_len, mainpic, w, h, n, plane, 1.f); + fft_vertical(s, s->fft_hdata[plane], s->fft_vdata[plane], + fft_len, n, plane); + + for (y = 0; y < h; y++) { + for (x = 0; x < w; x++) { + total += impulsepic->data[plane][y * impulsepic->linesize[plane] + x]; + } + } + total = FFMAX(1, total); + + fft_horizontal(s, s->fft_hdata_impulse[plane], fft_len, impulsepic, w, h, n, plane, 1 / total); + fft_vertical(s, s->fft_hdata_impulse[plane], s->fft_vdata_impulse[plane], + fft_len, n, plane); + + for (y = 0; y < fft_len; y++) { + for (x = 0; x < fft_len; x++) { + FFTSample re, im, ire, iim; + + re = s->fft_vdata[plane][y*fft_len + x].re; + im = s->fft_vdata[plane][y*fft_len + x].im; + ire = s->fft_vdata_impulse[plane][y*fft_len + x].re; + iim = s->fft_vdata_impulse[plane][y*fft_len + x].im; + + s->fft_vdata[plane][y*fft_len + x].re = ire * re - iim * im; + s->fft_vdata[plane][y*fft_len + x].im = iim * re + ire * im; + } + } + + ifft_vertical(s, fft_len, n, plane); + ifft_horizontal(s, mainpic, fft_len, w, h, n, plane); + } + + return ff_filter_frame(outlink, mainpic); +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + ConvolveContext *s = ctx->priv; + AVFilterLink *mainlink = ctx->inputs[0]; + int ret, i; + + s->fs.on_event = do_convolve; + ret = ff_framesync2_init_dualinput(&s->fs, ctx); + if (ret < 0) + return ret; + outlink->w = mainlink->w; + outlink->h = mainlink->h; + outlink->time_base = mainlink->time_base; + outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio; + outlink->frame_rate = mainlink->frame_rate; + + if ((ret = ff_framesync2_configure(&s->fs)) < 0) + return ret; + + for (i = 0; i < 4; i++) { + s->fft[i] = av_fft_init(s->fft_bits[i], 0); + s->ifft[i] = av_fft_init(s->fft_bits[i], 1); + } + + return 0; +} + +static int activate(AVFilterContext *ctx) +{ + ConvolveContext *s = ctx->priv; + return ff_framesync2_activate(&s->fs); +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + ConvolveContext *s = ctx->priv; + int i; + + for (i = 0; i < 4; i++) { + av_freep(&s->fft_hdata[i]); + av_freep(&s->fft_vdata[i]); + av_freep(&s->fft_hdata_impulse[i]); + av_freep(&s->fft_vdata_impulse[i]); + av_fft_end(s->fft[i]); + av_fft_end(s->ifft[i]); + } + + ff_framesync2_uninit(&s->fs); +} + +static const AVFilterPad convolve_inputs[] = { + { + .name = "main", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_input_main, + },{ + .name = "impulse", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_input_impulse, + }, + { NULL } +}; + +static const AVFilterPad convolve_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_output, + }, + { NULL } +}; + +AVFilter ff_vf_convolve = { + .name = "convolve", + .description = NULL_IF_CONFIG_SMALL("Convolve first video stream with second video stream."), + .preinit = convolve_framesync_preinit, + .uninit = uninit, + .query_formats = query_formats, + .activate = activate, + .priv_size = sizeof(ConvolveContext), + .priv_class = &convolve_class, + .inputs = convolve_inputs, + .outputs = convolve_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, +};