From patchwork Tue Apr 18 16:28:11 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: 3446 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.3.129 with SMTP id 123csp1796744vsd; Tue, 18 Apr 2017 09:37:01 -0700 (PDT) X-Received: by 10.28.154.198 with SMTP id c189mr13762612wme.45.1492533421146; Tue, 18 Apr 2017 09:37:01 -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 z137si23675wmc.145.2017.04.18.09.37.00; Tue, 18 Apr 2017 09:37:01 -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 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 A1F11689230; Tue, 18 Apr 2017 19:36:49 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f68.google.com (mail-wm0-f68.google.com [74.125.82.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1945D6882C9 for ; Tue, 18 Apr 2017 19:36:43 +0300 (EEST) Received: by mail-wm0-f68.google.com with SMTP id z129so413290wmb.1 for ; Tue, 18 Apr 2017 09:36:51 -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=HbpDQD4OA9rT94v8ugE41kD5Ale2zNtWJjRkJh3KEiU=; b=KqioyOFAqzpASzhE6EqaNmmzbkjiIof6aWMfWCiCdG0RnDwASg1fHaz6Ld6MZmyJC9 mq9lMmjZhyeJggsHFj8xeevm14PhWgQhe5bnL1wRf4UELWS7v2jXtDkGALrN2d4rF6Js oF7rIhXk2AuKkYN9nK4aayeWFNiDxZ7YX/XTJW8xc3833dACbDMbNUGkTcbfPfkCmKK5 lyHj156B+m7oGB2WYGma6/mhgL0a9vnpI3FoJaCXjQg844Tk0zKGi584MD3JSAsteLdh rJdZmWw68QyUoja5FrVPyW68aFLbpKckhKSGVYRq+Gm/9DFWxJ+qCiM6RYF5VYI/zPab P+fg== 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=HbpDQD4OA9rT94v8ugE41kD5Ale2zNtWJjRkJh3KEiU=; b=iGsIxwnmAUTNcNknjWA0XgCV2ThtzPSnrTN4clvHeVa/8JsVTzJU8T5i542ZRjAJdv EPh4ah9ObtBBiKjSqsDtl97hqfs3l1GW78a6UNZXwOEniZbfTKI7GDkVrv1iBHUnxuL1 4A/AGXB5KXwpvpFnnWnpORE0U5uhUcE6shzyMHZbVDJ5KxPENxXCHH2E4IsrnicynWQX hy8MAy4mZ6iIIOp8I3K7qKiTrN2H4/gdLPHe0eSWqQod2zi2C6ntW9L74NcK0Uhtde+b Jh5YHNqFFqWw8f5qZ8dlG7Yrh7StwXNmNp6X44pxouNZgKgKzmNxjXfYGP0pcZ/0WgkV JQAQ== X-Gm-Message-State: AN3rC/4SR6il7ouyORCnfWPKI8hP6kb6eSfhqMsM8svhtN5ess9PWVfU IAFmp1acI2gu+yKT X-Received: by 10.28.16.65 with SMTP id 62mr8368193wmq.75.1492532911879; Tue, 18 Apr 2017 09:28:31 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id w126sm15082318wmb.25.2017.04.18.09.28.30 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Apr 2017 09:28:30 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Tue, 18 Apr 2017 18:28:11 +0200 Message-Id: <20170418162811.18614-1-onemda@gmail.com> X-Mailer: git-send-email 2.9.3 Subject: [FFmpeg-devel] [PATCH] avfilter: add deflicker 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 --- libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_deflicker.c | 327 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 329 insertions(+) create mode 100644 libavfilter/vf_deflicker.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index d6daa7a..6de7cc0 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -155,6 +155,7 @@ OBJS-$(CONFIG_DCTDNOIZ_FILTER) += vf_dctdnoiz.o OBJS-$(CONFIG_DEBAND_FILTER) += vf_deband.o OBJS-$(CONFIG_DECIMATE_FILTER) += vf_decimate.o OBJS-$(CONFIG_DEFLATE_FILTER) += vf_neighbor.o +OBJS-$(CONFIG_DEFLICKER_FILTER) += vf_deflicker.o OBJS-$(CONFIG_DEINTERLACE_QSV_FILTER) += vf_deinterlace_qsv.o OBJS-$(CONFIG_DEINTERLACE_VAAPI_FILTER) += vf_deinterlace_vaapi.o OBJS-$(CONFIG_DEJUDDER_FILTER) += vf_dejudder.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index ec6ec04..0e69f99 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -166,6 +166,7 @@ static void register_all(void) REGISTER_FILTER(DEBAND, deband, vf); REGISTER_FILTER(DECIMATE, decimate, vf); REGISTER_FILTER(DEFLATE, deflate, vf); + REGISTER_FILTER(DEFLICKER, deflicker, vf); REGISTER_FILTER(DEINTERLACE_QSV,deinterlace_qsv,vf); REGISTER_FILTER(DEINTERLACE_VAAPI, deinterlace_vaapi, vf); REGISTER_FILTER(DEJUDDER, dejudder, vf); diff --git a/libavfilter/vf_deflicker.c b/libavfilter/vf_deflicker.c new file mode 100644 index 0000000..5e9d957 --- /dev/null +++ b/libavfilter/vf_deflicker.c @@ -0,0 +1,327 @@ +/* + * 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 "avfilter.h" + +#define FF_BUFQUEUE_SIZE 129 +#include "bufferqueue.h" + +#include "formats.h" +#include "internal.h" +#include "video.h" + +#define SIZE FF_BUFQUEUE_SIZE + +typedef struct DeflickerContext { + const AVClass *class; + + int eof; + int depth; + int nb_planes; + int planewidth[4]; + int planeheight[4]; + + uint64_t *histogram; + float luminance[SIZE]; + + struct FFBufQueue q; + int size; + int available; + + float (*calc_avgy)(AVFilterContext *ctx, AVFrame *in); + int (*deflicker)(AVFilterContext *ctx, AVFrame *in, AVFrame *out); +} DeflickerContext; + +#define OFFSET(x) offsetof(DeflickerContext, x) +#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM + +static const AVOption deflicker_options[] = { + { "s", "set how many frames to use", OFFSET(size), AV_OPT_TYPE_INT, {.i64=5}, 2, SIZE, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(deflicker); + +static int query_formats(AVFilterContext *ctx) +{ + static const enum AVPixelFormat pixel_fmts[] = { + AV_PIX_FMT_GRAY8, + AV_PIX_FMT_GRAY10, + AV_PIX_FMT_GRAY12, + AV_PIX_FMT_GRAY16, + AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, + AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, + AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, + AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, + AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, + AV_PIX_FMT_YUVJ411P, + 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_YUV440P10, + AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, + AV_PIX_FMT_YUV440P12, + AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14, + AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, + AV_PIX_FMT_NONE + }; + AVFilterFormats *formats = ff_make_format_list(pixel_fmts); + if (!formats) + return AVERROR(ENOMEM); + return ff_set_common_formats(ctx, formats); +} + +static int deflicker8(AVFilterContext *ctx, AVFrame *in, AVFrame *out) +{ + DeflickerContext *s = ctx->priv; + const int h = s->planeheight[0]; + const int w = s->planewidth[0]; + const uint8_t *src = in->data[0]; + uint8_t *dst = out->data[0]; + float f = 0; + int x, y; + + for (y = 0; y < s->size; y++) { + f += s->luminance[y]; + } + f /= s->size; + f /= s->luminance[0]; + + for (y = 0; y < h; y++) { + for (x = 0; x < w; x++) { + dst[x] = av_clip(src[x] * f, 0, (1 << s->depth) - 1); + } + + dst += out->linesize[0]; + src += in->linesize[0]; + } + + for (y = 1; y < s->nb_planes; y++) { + av_image_copy_plane(out->data[y], out->linesize[y], + in->data[y], in->linesize[y], + s->planewidth[y], s->planeheight[y]); + } + + return 0; +} + +static int deflicker16(AVFilterContext *ctx, AVFrame *in, AVFrame *out) +{ + DeflickerContext *s = ctx->priv; + const int h = s->planeheight[0]; + const int w = s->planewidth[0]; + const uint16_t *src = (const uint16_t *)in->data[0]; + uint16_t *dst = (uint16_t *)out->data[0]; + float f = 0; + int x, y; + + for (y = 0; y < s->size; y++) { + f += s->luminance[y]; + } + f /= s->size; + f /= s->luminance[0]; + + for (y = 0; y < h; y++) { + for (x = 0; x < w; x++) { + dst[x] = av_clip(src[x] * f, 0, (1 << s->depth) - 1); + } + + dst += out->linesize[0] / 2; + src += in->linesize[0] / 2; + } + + for (y = 1; y < s->nb_planes; y++) { + av_image_copy_plane(out->data[y], out->linesize[y], + in->data[y], in->linesize[y], + s->planewidth[y] * 2, s->planeheight[y]); + } + + return 0; +} + +static float calc_avgy8(AVFilterContext *ctx, AVFrame *in) +{ + DeflickerContext *s = ctx->priv; + const uint8_t *src = in->data[0]; + int64_t sum = 0; + int y, x; + + memset(s->histogram, 0, (1 << s->depth) * sizeof(*s->histogram)); + + for (y = 0; y < s->planeheight[0]; y++) { + for (x = 0; x < s->planewidth[0]; x++) { + s->histogram[src[x]]++; + } + src += in->linesize[0]; + } + + for (y = 0; y < 1 << s->depth; y++) { + sum += s->histogram[y] * y; + } + + return 1.0 * sum / (s->planeheight[0] * s->planewidth[0]); +} + +static float calc_avgy16(AVFilterContext *ctx, AVFrame *in) +{ + DeflickerContext *s = ctx->priv; + const uint16_t *src = (const uint16_t *)in->data[0]; + int64_t sum = 0; + int y, x; + + memset(s->histogram, 0, (1 << s->depth) * sizeof(*s->histogram)); + + for (y = 0; y < s->planeheight[0]; y++) { + for (x = 0; x < s->planewidth[0]; x++) { + s->histogram[src[x]]++; + } + src += in->linesize[0] / 2; + } + + for (y = 0; y < 1 << s->depth; y++) { + sum += s->histogram[y] * y; + } + + return 1.0 * sum / (s->planeheight[0] * s->planewidth[0]); +} + +static int config_input(AVFilterLink *inlink) +{ + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + AVFilterContext *ctx = inlink->dst; + DeflickerContext *s = ctx->priv; + + s->nb_planes = desc->nb_components; + + s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); + s->planeheight[0] = s->planeheight[3] = inlink->h; + s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); + s->planewidth[0] = s->planewidth[3] = inlink->w; + + s->depth = desc->comp[0].depth; + if (s->depth == 8) { + s->deflicker = deflicker8; + s->calc_avgy = calc_avgy8; + } else { + s->deflicker = deflicker16; + s->calc_avgy = calc_avgy16; + } + + s->histogram = av_calloc(1 << s->depth, sizeof(*s->histogram)); + if (!s->histogram) + return AVERROR(ENOMEM); + + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *buf) +{ + AVFilterContext *ctx = inlink->dst; + AVFilterLink *outlink = ctx->outputs[0]; + DeflickerContext *s = ctx->priv; + AVFrame *out, *in; + + if (s->q.available < s->size && !s->eof) { + s->luminance[s->available] = s->calc_avgy(ctx, buf); + ff_bufqueue_add(ctx, &s->q, buf); + s->available++; + return 0; + } + + in = ff_bufqueue_peek(&s->q, 0); + + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) { + av_frame_free(&buf); + return AVERROR(ENOMEM); + } + + s->deflicker(ctx, in, out); + av_frame_copy_props(out, in); + + in = ff_bufqueue_get(&s->q); + av_frame_free(&in); + memmove(&s->luminance[0], &s->luminance[1], sizeof(*s->luminance) * (s->size - 1)); + s->luminance[s->available - 1] = s->calc_avgy(ctx, buf); + ff_bufqueue_add(ctx, &s->q, buf); + + return ff_filter_frame(outlink, out); +} + +static int request_frame(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + DeflickerContext *s = ctx->priv; + int ret = 0; + + ret = ff_request_frame(ctx->inputs[0]); + if (ret == AVERROR_EOF && s->available > 0) { + AVFrame *buf = av_frame_clone(ff_bufqueue_peek(&s->q, s->size - 1)); + if (!buf) + return AVERROR(ENOMEM); + + s->eof = 1; + ret = filter_frame(ctx->inputs[0], buf); + s->available--; + } + + + return ret; +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + DeflickerContext *s = ctx->priv; + + ff_bufqueue_discard_all(&s->q); + av_freep(&s->histogram); +} + +static const AVFilterPad inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = filter_frame, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .request_frame = request_frame, + }, + { NULL } +}; + +AVFilter ff_vf_deflicker = { + .name = "deflicker", + .description = NULL_IF_CONFIG_SMALL("Deflicker input video."), + .priv_size = sizeof(DeflickerContext), + .priv_class = &deflicker_class, + .uninit = uninit, + .query_formats = query_formats, + .inputs = inputs, + .outputs = outputs, +};