From patchwork Mon Sep 5 15:03:23 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 433 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.140.134 with SMTP id o128csp143875vsd; Mon, 5 Sep 2016 08:03:47 -0700 (PDT) X-Received: by 10.194.102.227 with SMTP id fr3mr6738926wjb.147.1473087827045; Mon, 05 Sep 2016 08:03:47 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id w10si17856971wmg.106.2016.09.05.08.03.46; Mon, 05 Sep 2016 08:03:46 -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; 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 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 45634689FEB; Mon, 5 Sep 2016 18:03:35 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f66.google.com (mail-wm0-f66.google.com [74.125.82.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9B25A689FA4 for ; Mon, 5 Sep 2016 18:03:29 +0300 (EEST) Received: by mail-wm0-f66.google.com with SMTP id w207so13831393wmw.0 for ; Mon, 05 Sep 2016 08:03:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id; bh=AeKohxALMstP2xZxEgeeIcTiNrGcXCP7teskIjYR9vw=; b=Xi1ZPV92FOP6Hc7LxlrUXHe33vx3nL42hU+kdNQ9UOW7guMGACJXhmSHldM915BMXx kjbsi707MBR+dyU8WCu+ABrjXqAP22AfgEbPXl8DQUx8I54PAAX3kW6DaxbfaxShZDb2 Z8ncxR+BMSkgm20p1QS4XRDjIU2vq2BPKRA0lhzRUoNEhfZtRglqPyJAm/xsAo+G4OD6 H/k35K3fsKCWntFmm0LvvFps9Hyk1QhMNH09M1hPLYMEgpo0DBtuqoyVciwKG9LYA/1n lVWFIGlpMmoUS/l9ZLQOuDfndi9pXMGEAlsYbJflPVrrtLxztJ1mUgsCt5JsE6nO7Qt1 hgSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id; bh=AeKohxALMstP2xZxEgeeIcTiNrGcXCP7teskIjYR9vw=; b=FJ5PghCL48K1Wr10ZT9JxBeNxosvv5EPRfl/kc/Z9n8ljSsZIQeZPwcucI6nOHrd/X 8CBojRudfNGFjpSFXsB9ZcceGwqKkAo44bZbLbsv5PTY1JlXTSSad2LvR8/2mz6In2LS uM5RqsX++gTz65TRe2Tpdsyts6QlnkrzB7g/R6MZY1Vlx048eoTge427oVpNYnN47PZ9 fYuQ/nUIMQuk64c5jA/FfwgL2QzcBpYo9qyj9XbPqjA7PJlg5UTg8Rk/O5cksAbJ4X/a EbTO4G/KwRqfXNBNzhddPdswCryK6DGMynmhZThk3zOFFiSvWQvabreshPSv50B03g0r uzlg== X-Gm-Message-State: AE9vXwM+v7BAAIiNvsE44HuQ/L1CB2VEQY+C9VQ6bzhuK7ebOHMhDv17QMXkauIiO6HTpA== X-Received: by 10.194.2.47 with SMTP id 15mr35316544wjr.133.1473087817816; Mon, 05 Sep 2016 08:03:37 -0700 (PDT) Received: from computer.gigaset.lan (233-149.dsl.iskon.hr. [89.164.233.149]) by smtp.gmail.com with ESMTPSA id e5sm21296005wma.13.2016.09.05.08.03.36 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 05 Sep 2016 08:03:37 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Mon, 5 Sep 2016 17:03:23 +0200 Message-Id: <1473087803-19712-1-git-send-email-onemda@gmail.com> X-Mailer: git-send-email 2.5.0 Subject: [FFmpeg-devel] [PATCH] avfilter: add ablur 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 | 18 +++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_ablur.c | 341 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 361 insertions(+) create mode 100644 libavfilter/vf_ablur.c diff --git a/doc/filters.texi b/doc/filters.texi index 99f07a6..9277841 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -4310,6 +4310,24 @@ build. Below is a description of the currently available video filters. +@section ablur + +Apply average blur filter. + +The filter accepts the following options: + +@table @option +@item sizeX +Set horizontal kernel size. + +@item planes +Set which planes to filter. By default all planes are filtered. + +@item sizeY +Set vertical kernel size, if zero it will be same as @code{sizeX}. +Default is @code{0}. +@end table + @section alphaextract Extract the alpha component from the input as a grayscale video. This diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 52b148a..08e4159 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -120,6 +120,7 @@ OBJS-$(CONFIG_SINE_FILTER) += asrc_sine.o OBJS-$(CONFIG_ANULLSINK_FILTER) += asink_anullsink.o # video filters +OBJS-$(CONFIG_ABLUR_FILTER) += vf_ablur.o OBJS-$(CONFIG_ALPHAEXTRACT_FILTER) += vf_extractplanes.o OBJS-$(CONFIG_ALPHAMERGE_FILTER) += vf_alphamerge.o OBJS-$(CONFIG_ASS_FILTER) += vf_subtitles.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index ac8b41d..7dd306a 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -137,6 +137,7 @@ void avfilter_register_all(void) REGISTER_FILTER(ANULLSINK, anullsink, asink); + REGISTER_FILTER(ABLUR, ablur, vf); REGISTER_FILTER(ALPHAEXTRACT, alphaextract, vf); REGISTER_FILTER(ALPHAMERGE, alphamerge, vf); REGISTER_FILTER(ASS, ass, vf); diff --git a/libavfilter/vf_ablur.c b/libavfilter/vf_ablur.c new file mode 100644 index 0000000..d678f4a --- /dev/null +++ b/libavfilter/vf_ablur.c @@ -0,0 +1,341 @@ +/* + * Copyright (c) 2016 Paul B Mahol + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "libavutil/imgutils.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +typedef struct ABlurContext { + const AVClass *class; + + int radius; + int radiusV; + int planes; + + int depth; + int planewidth[4]; + int planeheight[4]; + float *buffer; + int nb_planes; +} ABlurContext; + +#define OFFSET(x) offsetof(ABlurContext, x) +#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM + +static const AVOption ablur_options[] = { + { "sizeX", "set horizontal size", OFFSET(radius), AV_OPT_TYPE_INT, {.i64=1}, 1, 1024, FLAGS }, + { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS }, + { "sizeY", "set vertical size", OFFSET(radiusV), AV_OPT_TYPE_INT, {.i64=0}, 0, 1024, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(ablur); + +typedef struct ThreadData { + int height; + int width; +} ThreadData; + +static int filter_horizontally(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ABlurContext *s = ctx->priv; + ThreadData *td = arg; + const int height = td->height; + const int width = td->width; + const int slice_start = (height * jobnr ) / nb_jobs; + const int slice_end = (height * (jobnr+1)) / nb_jobs; + const int radius = FFMIN(s->radius, width / 2); + float *buffer = s->buffer; + int y, x; + float *ptr; + + /* Filter horizontally along each row */ + for (y = slice_start; y < slice_end; y++) { + float acc = 0; + int count = 0; + + ptr = buffer + width * y; + + for (x = 0; x < radius; x++) { + acc += ptr[x]; + } + count += radius; + + for (x = 0; x <= radius; x++) { + acc += ptr[x + radius]; + count++; + ptr[x] = acc / count; + } + + for (; x < width - radius; x++) { + acc += ptr[x + radius] - ptr[x - radius]; + ptr[x] = acc / count; + } + + for (; x < width; x++) { + acc -= ptr[x - radius]; + count--; + ptr[x] = acc / count; + } + } + + return 0; +} + +static int filter_vertically(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ABlurContext *s = ctx->priv; + ThreadData *td = arg; + const int height = td->height; + const int width = td->width; + const int slice_start = (width * jobnr ) / nb_jobs; + const int slice_end = (width * (jobnr+1)) / nb_jobs; + const int radius = FFMIN(s->radiusV, height / 2); + float *buffer = s->buffer; + int i, x; + float *ptr; + + /* Filter vertically along each column */ + for (x = slice_start; x < slice_end; x++) { + float acc = 0; + int count = 0; + + ptr = buffer + x; + + for (i = 0; i < radius; i++) { + acc += ptr[0]; + ptr += width; + } + count += radius; + + ptr = buffer + x; + for (i = 0; i <= radius; i++) { + acc += ptr[(i + radius) * width]; + count++; + ptr[i * width] = acc / count; + } + + for (; i < height - radius; i++) { + acc += ptr[(i + radius) * width] - ptr[(i - radius) * width]; + ptr[i * width] = acc / count; + } + + for (; i < height; i++) { + acc -= ptr[(i - radius) * width]; + count--; + ptr[i * width] = acc / count; + } + } + + return 0; +} + +static void averageiir2d(AVFilterContext *ctx, int plane) +{ + ABlurContext *s = ctx->priv; + const int width = s->planewidth[plane]; + const int height = s->planeheight[plane]; + const int nb_threads = ff_filter_get_nb_threads(ctx); + ThreadData td; + + td.width = width; + td.height = height; + ctx->internal->execute(ctx, filter_horizontally, &td, NULL, FFMIN(height, nb_threads)); + ctx->internal->execute(ctx, filter_vertically, &td, NULL, FFMIN(width, nb_threads)); +} + +static int query_formats(AVFilterContext *ctx) +{ + static const enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, + AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, + AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, + AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, + AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, + AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, + AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, + AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12, + AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, + AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, + AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9, + AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10, + AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16, + AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, + AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, + AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, + AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16, + AV_PIX_FMT_NONE + }; + + return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); +} + +static int config_input(AVFilterLink *inlink) +{ + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + ABlurContext *s = inlink->dst->priv; + + s->depth = desc->comp[0].depth; + 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; + + s->nb_planes = av_pix_fmt_count_planes(inlink->format); + + s->buffer = av_malloc_array(inlink->w, inlink->h * sizeof(*s->buffer)); + if (!s->buffer) + return AVERROR(ENOMEM); + + if (s->radiusV <= 0) { + s->radiusV = s->radius; + } + + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + ABlurContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + AVFrame *out; + int plane; + + if (av_frame_is_writable(in)) { + out = in; + } else { + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) { + av_frame_free(&in); + return AVERROR(ENOMEM); + } + av_frame_copy_props(out, in); + } + + for (plane = 0; plane < s->nb_planes; plane++) { + const int height = s->planeheight[plane]; + const int width = s->planewidth[plane]; + float *bptr = s->buffer; + const uint8_t *src = in->data[plane]; + const uint16_t *src16 = (const uint16_t *)in->data[plane]; + uint8_t *dst = out->data[plane]; + uint16_t *dst16 = (uint16_t *)out->data[plane]; + int y, x; + + if (!(s->planes & (1 << plane))) { + if (out != in) + av_image_copy_plane(out->data[plane], out->linesize[plane], + in->data[plane], in->linesize[plane], + width * ((s->depth + 7) / 8), height); + continue; + } + + if (s->depth == 8) { + for (y = 0; y < height; y++) { + for (x = 0; x < width; x++) { + bptr[x] = src[x]; + } + bptr += width; + src += in->linesize[plane]; + } + } else { + for (y = 0; y < height; y++) { + for (x = 0; x < width; x++) { + bptr[x] = src16[x]; + } + bptr += width; + src16 += in->linesize[plane] / 2; + } + } + + averageiir2d(ctx, plane); + + bptr = s->buffer; + if (s->depth == 8) { + for (y = 0; y < height; y++) { + for (x = 0; x < width; x++) { + dst[x] = bptr[x]; + } + bptr += width; + dst += out->linesize[plane]; + } + } else { + for (y = 0; y < height; y++) { + for (x = 0; x < width; x++) { + dst16[x] = bptr[x]; + } + bptr += width; + dst16 += out->linesize[plane] / 2; + } + } + } + + if (out != in) + av_frame_free(&in); + return ff_filter_frame(outlink, out); +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + ABlurContext *s = ctx->priv; + + av_freep(&s->buffer); +} + +static const AVFilterPad ablur_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_input, + .filter_frame = filter_frame, + }, + { NULL } +}; + +static const AVFilterPad ablur_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +AVFilter ff_vf_ablur = { + .name = "ablur", + .description = NULL_IF_CONFIG_SMALL("Apply Average Blur filter."), + .priv_size = sizeof(ABlurContext), + .priv_class = &ablur_class, + .uninit = uninit, + .query_formats = query_formats, + .inputs = ablur_inputs, + .outputs = ablur_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, +};