From patchwork Wed Dec 27 18:21:29 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: 6994 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.79.195 with SMTP id r64csp8338544jad; Wed, 27 Dec 2017 10:22:04 -0800 (PST) X-Google-Smtp-Source: ACJfBotx0AXRClxq91XovG90CPwJmMKyycijy0CpD1ctznfmVzpC3xQs25ohfwLAL2VYdcWFcBi5 X-Received: by 10.28.22.67 with SMTP id 64mr25051299wmw.131.1514398924274; Wed, 27 Dec 2017 10:22:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514398924; cv=none; d=google.com; s=arc-20160816; b=ZXtsh41GqKsiGTNbUBtq2ZDqwPeZLdaC55iaKMbzMHQCVU790At7Ec2qpyLJ+1oShF P5dR2iu83WYSp8761tblJKvSo+oX1OXLTK2O03aZbbEJ4NrBj/8JUGoHetSsyslWWGkD j6aAqd3O4y45nxxSNH9MGY2BTrY8nBzySWf2/UPAlIXhGQ687pp0j9oD2423mrGHzMcF RkuFN6KSJpHgQHumSOtdFAGMGqPj2qbvvK21UzT/o+vb+Ub2cnBr4a2dH5rb5IcFFnAB Mwesh4B//WiF9o25nYvfFRF8odsVrH4NYiTVhKfxeD7PMe8fIIjpxn4aNj/T0wlQvVCP lQHA== 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=PWlDZZdUONVBqWl2mst0f8ZmOdptMFdaZeGPCVEs1Jk=; b=bsoBzO5tocPN3TqNy+bwSTQrd0vlWQtNu2GxPS/SFQivgGWZ2Ot0Wv7gvALQmNwU2g OCEGSACIY/4tDXDFp3WERYc9LGYXTTb1R9xunsoX7LfB1rGxynpKj3hX4aoqRa00AQXl HN/gVUIiQUfqdDN6tUuxWWHenRF6+VoZ9d49eyJLY5rcENz8MsU86EsEFZbbPNcEqYXE pl7dZBnjEd3PwMMj+dyFMCQ3OAs+wkFlgmoX2yTwnq9/a+KghNAjThCqJ9jl32JyY6Hi qJXTHktA/+8fKpgCBTK0vYVrlS5MVVu9wj1sHj/9yoHvLbww1exU1hFyksnzz1INYAPB /12A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=XEYus9xp; 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 11si9511231wru.19.2017.12.27.10.22.03; Wed, 27 Dec 2017 10:22:04 -0800 (PST) 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=XEYus9xp; 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 3DEF46883CF; Wed, 27 Dec 2017 20:21:48 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f171.google.com (mail-wr0-f171.google.com [209.85.128.171]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 49D866883A8 for ; Wed, 27 Dec 2017 20:21:42 +0200 (EET) Received: by mail-wr0-f171.google.com with SMTP id o15so5192887wrf.12 for ; Wed, 27 Dec 2017 10:21:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=oLjb2ATcrYqJVO/n/WIP65azuoCBHX6pd334FWnduWw=; b=XEYus9xpqApqDn556g9rXJB34koHx4oCkfp6fk10VMefI6eYkRp6Kq+MsV5iG3ROaM LhodPYURbXAWhCwIy2NyOEKIciciDRSx/UhP4n1N61oFVg6z+vLieLJEYuDgUkeqYsuo 73Ska2hB4xiTwiur5tvDfY4nfCo7BOXUyBCcSFYWwKkPt0l3F4bkdtBC58NKUln6q1wk cc6s28GmdXTxKrVfOi2qBpF6D5T15l5Ss8spHP2suoV07JXnpqpHVx8H1Kt/LvNZ8KEm ONCM+n/bHE4LZv4LlAoI1s0VpDG7wNFxiaE/OgZh6OGtQwxxONRHjO1qd7OZ9hP/tlIu 0WMQ== 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=oLjb2ATcrYqJVO/n/WIP65azuoCBHX6pd334FWnduWw=; b=mgulUQ2pFdHA/1u2fsM4pZhmgCYwD0PzHc+B24dw7Yr3beg8LHjTyU0q6U5mvkm96A DeFg70BGkE797ZHVwzYvD99mcce9vHaN3JjYnQapHSQ3beYo0aRq+EEAO+ugyxiPowpj 8tyBZ0/Oed+XcPMRGJ/kUbqcSVu1NxLrJ+LdufBPqF1DcrkvEwlmjMEQmrWmLlt3OQxg yxtUQszF+sHpt5fA74oPtLzTDfQEc+3GUN16j020h2b8/xQIfT2arRDocS80diXwSdCK VtTg7hxBbXoTl88pToYoOWc+fro4BQe5GPyEal3bYjf0upmcQ9lVwjsPR1ON0DjhqTzZ aY3A== X-Gm-Message-State: AKGB3mL5y8Onvlu0MzQeh2tfWg8U1fVrCSHFGDFq7fuk/hT9GxSmIDLp jEUaBvlkd0wjc6xwljIiMT4s6g== X-Received: by 10.223.152.118 with SMTP id v109mr28213583wrb.277.1514398915417; Wed, 27 Dec 2017 10:21:55 -0800 (PST) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id p128sm17169829wmb.23.2017.12.27.10.21.54 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Dec 2017 10:21:54 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Wed, 27 Dec 2017 19:21:29 +0100 Message-Id: <20171227182129.14605-1-onemda@gmail.com> X-Mailer: git-send-email 2.11.0 Subject: [FFmpeg-devel] [PATCH] avfilter: add entropy 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 | 4 ++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_entropy.c | 174 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 libavfilter/vf_entropy.c diff --git a/doc/filters.texi b/doc/filters.texi index f651f1234d..0e3f4cd78d 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -8220,6 +8220,10 @@ Set pal8 output pixel format. This option does not work with codebook length greater than 256. @end table +@section entropy + +Measure graylevel entropy in histogram of color channels of video frames. + @section fade Apply a fade-in/out effect to the input video. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 8bde542163..7d9f2330ca 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -184,6 +184,7 @@ OBJS-$(CONFIG_DRAWGRID_FILTER) += vf_drawbox.o OBJS-$(CONFIG_DRAWTEXT_FILTER) += vf_drawtext.o OBJS-$(CONFIG_EDGEDETECT_FILTER) += vf_edgedetect.o OBJS-$(CONFIG_ELBG_FILTER) += vf_elbg.o +OBJS-$(CONFIG_ENTROPY_FILTER) += vf_entropy.o OBJS-$(CONFIG_EQ_FILTER) += vf_eq.o OBJS-$(CONFIG_EROSION_FILTER) += vf_neighbor.o OBJS-$(CONFIG_EXTRACTPLANES_FILTER) += vf_extractplanes.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 67c073091f..7635a8a583 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -194,6 +194,7 @@ static void register_all(void) REGISTER_FILTER(DRAWTEXT, drawtext, vf); REGISTER_FILTER(EDGEDETECT, edgedetect, vf); REGISTER_FILTER(ELBG, elbg, vf); + REGISTER_FILTER(ENTROPY, entropy, vf); REGISTER_FILTER(EQ, eq, vf); REGISTER_FILTER(EROSION, erosion, vf); REGISTER_FILTER(EXTRACTPLANES, extractplanes, vf); diff --git a/libavfilter/vf_entropy.c b/libavfilter/vf_entropy.c new file mode 100644 index 0000000000..4c622127f5 --- /dev/null +++ b/libavfilter/vf_entropy.c @@ -0,0 +1,174 @@ +/* + * 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" +#include "drawutils.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +typedef struct EntropyContext { + const AVClass *class; + + int nb_planes; + int planeheight[4]; + int planewidth[4]; + int depth; + int is_rgb; + uint8_t rgba_map[4]; + char planenames[4]; + uint64_t *histogram; +} EntropyContext; + +static int query_formats(AVFilterContext *ctx) +{ + static const enum AVPixelFormat pixfmts[] = { + AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, + AV_PIX_FMT_YUV440P, + AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P, + AV_PIX_FMT_YUVJ440P, + AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9, + AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10, + 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_YUV444P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV420P16, + 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_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16, + AV_PIX_FMT_NONE + }; + + AVFilterFormats *formats = ff_make_format_list(pixfmts); + if (!formats) + return AVERROR(ENOMEM); + return ff_set_common_formats(ctx, formats); +} + +static int config_input(AVFilterLink *inlink) +{ + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + AVFilterContext *ctx = inlink->dst; + EntropyContext *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; + s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0; + + s->planenames[0] = s->is_rgb ? 'R' : 'Y'; + s->planenames[1] = s->is_rgb ? 'G' : 'U'; + s->planenames[2] = s->is_rgb ? 'B' : 'V'; + s->planenames[3] = 'A'; + + s->histogram = av_malloc_array(1 << s->depth, sizeof(*s->histogram)); + if (!s->histogram) + return AVERROR(ENOMEM); + + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + AVFilterLink *outlink = ctx->outputs[0]; + EntropyContext *s = ctx->priv; + int plane, y, x; + + for (plane = 0; plane < s->nb_planes; plane++) { + int cidx = s->is_rgb ? s->rgba_map[plane] : plane; + uint8_t *src = in->data[plane]; + float total = s->planewidth[plane] * s->planeheight[plane]; + float entropy = 0; + char metabuf[128]; + char key[128]; + + memset(s->histogram, 0, (1 << s->depth) * sizeof(*s->histogram)); + + for (y = 0; y < s->planeheight[plane]; y++) { + for (x = 0; x < s->planewidth[plane]; x++) { + s->histogram[src[x]]++; + } + + src += in->linesize[plane]; + } + + for (y = 0; y < 1 << s->depth; y++) { + if (s->histogram[y]) { + float p = s->histogram[y] / total; + entropy += -log2(p) * p; + } + } + + snprintf(key, sizeof(key), "lavfi.entropy.entropy.%c", s->planenames[cidx]); + snprintf(metabuf, sizeof(metabuf), "%f", entropy); + av_dict_set(&in->metadata, key, metabuf, 0); + snprintf(key, sizeof(key), "lavfi.entropy.normalized_entropy.%c", s->planenames[cidx]); + snprintf(metabuf, sizeof(metabuf), "%f", entropy / log2(1 << s->depth)); + av_dict_set(&in->metadata, key, metabuf, 0); + } + + return ff_filter_frame(outlink, in); +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + EntropyContext *s = ctx->priv; + + 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, + }, + { NULL } +}; + +AVFilter ff_vf_entropy = { + .name = "entropy", + .description = NULL_IF_CONFIG_SMALL("Measure video frames entropy."), + .priv_size = sizeof(EntropyContext), + .uninit = uninit, + .query_formats = query_formats, + .inputs = inputs, + .outputs = outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, +};