From patchwork Sun Feb 26 15:02:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: yizhuo liu X-Patchwork-Id: 40528 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:5494:b0:bf:7b3a:fd32 with SMTP id i20csp2731503pzk; Sun, 26 Feb 2023 07:03:22 -0800 (PST) X-Google-Smtp-Source: AK7set8wqUVbjb9qrvCzwgwWyvBdT3y35y33zo0TFyf1Rat8Vm1WtlSNLeeJoORmdZlscfTOs9wJ X-Received: by 2002:aa7:c404:0:b0:4ac:b4f3:b8a7 with SMTP id j4-20020aa7c404000000b004acb4f3b8a7mr20311083edq.7.1677423802262; Sun, 26 Feb 2023 07:03:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677423802; cv=none; d=google.com; s=arc-20160816; b=YXNt+HzrEkk6AdDGAb0PeaCGfWWMFGXzjuJNXSZgx/QN0LRmJUma2PgAFeSDbSZJqK V4A0iXCW6BtL+Ra9R9bMlaM+YZ+MLZBDVeNQRIYVNFSPtcw6fjDSVq4oS1iFBYh+MiGP PHj7UA+uUwdXu7bUs+D7p7PNdYZcFIrriVznSpJKWZqzar/Wqa5useY4cJWp5nlTmkbV Ym4aYhM8WwLMq/FKuIm+UMgC3KV1Rt/J8n2DEIcQb6VnGJQpwvJGCBXIqhFz0ODFOEQP e7/LxQicRYdC6Y2FeemQBwMksCuPE9aimd/KtjyhHi1IenmrqvD0n/WfEsQTQccUOrPk 6u5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=31g9CqN+t+EYTYnBmnaGBgs9ctMh3VsPPkJzl2bIWPU=; b=FXKk8KHr4u7+R2lKXEw02NIMlqSD3E5d6HFNhuMK89ZjKYaSI+TI877+dWUHrEP9yj SQDGdVf/WQFWSVtlbLvh1kjHIzDG569B22M6wgek+8En1fcSxfp+CSdWmtwqfNk1T0mR sCpdUULjnTcutWiQrVQNFlrB0NPz9fzSnxxmOP/jYK4fPk7tX0iAi9nE50IwE56koZ3p MrzJStbXSOswUB76imBQWt+r3ERVO9wfSizkw9hOIMuCSt6FhDwz/+UjwZA2z6pZ0Rx1 jCVmWc3Ha2N9FZnUHGWAaRqoSmUomImMm7Q3CyacpEyA0p+qiwml7ait2LHC16lziU1n n8yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=PGpRvB9u; 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=QUARANTINE 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 a2-20020aa7d742000000b004acb305fe3bsi5670914eds.605.2023.02.26.07.03.21; Sun, 26 Feb 2023 07:03:22 -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=20210112 header.b=PGpRvB9u; 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=QUARANTINE 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 AE55368BD33; Sun, 26 Feb 2023 17:03:17 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3D5D868BA90 for ; Sun, 26 Feb 2023 17:03:11 +0200 (EET) Received: by mail-pj1-f42.google.com with SMTP id kb15so3642661pjb.1 for ; Sun, 26 Feb 2023 07:03:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=AdEqNDgu2t0oSK77KSjiRVby/YKs3BTXDoaxN5NSff8=; b=PGpRvB9umXvJF0nqzR0s79eCAGOCyG2Rd2NbFmsg0MIgO04CxaV8CsitVmuMXMgCbo rhDrTKOg8nZGTyBf5a10ZL3htjGNnULHF8S1+at9rXffPcZUB5i55iGEzXcVioMyBWV0 hIrK+j2hcCkA8d6oF/8vA4bVy+oWYmVPcYllHY8b9v+9dPbOfPHH1gUc4hLI+R7I6aje aGjdvmEIrmmn4K4opQ934uVJKZVo4Z2VeWqwzYQKBkhIUkltuA8c8nwzwlNrcepEMl2g G+FGcwZIZKkfUcBCcp8I7ONIJotTESvbh0I2EjCODLcArzlNGhG78f15oEuZEvZ9XA1S ci5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=AdEqNDgu2t0oSK77KSjiRVby/YKs3BTXDoaxN5NSff8=; b=hlOESY1mx8JoYYaGcGTC3EYMkZD4A++p58qAujhSSWEqjDhZOMS2w2rN5Ei9izzpM6 8NYTIcejnx77Y08H0ecbKs9XVEj3TRBQVT4aCfhzyeWnFXsSWMLLyaKoD8Dg7l2P3in7 5BZV8Y7K6bzlpygk4WQ+waVgUDuUtZbAttH7VA1+gWl/tTPBjBzfKb4Hyj5B1aYdYykM 78uOsmcwdY/QNmYEpFFN626U6JCJauzTEC0YWXnMK4TojaBNazZ3J9MtWFHD/aRxZAtW U5yFatJ0rxeAnXJGTM7dT7arocuqq813ioDg9FLuttCx7VVKd5XavCWhIg/3JLFCB086 WUVg== X-Gm-Message-State: AO0yUKVJchA5jTMsqLel0EZb+eqLuhVf2cKHaytUVGBWIyh5xtabSaul DNr7eFQL+y4zVxixMq/pHD3rixI60eRcGw== X-Received: by 2002:a17:902:d2cd:b0:19a:98fd:9c54 with SMTP id n13-20020a170902d2cd00b0019a98fd9c54mr27224282plc.35.1677423788576; Sun, 26 Feb 2023 07:03:08 -0800 (PST) Received: from 192.168.0.104 ([120.245.114.205]) by smtp.gmail.com with ESMTPSA id d19-20020a170902b71300b0019a74841c9bsm2778086pls.192.2023.02.26.07.03.03 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 26 Feb 2023 07:03:08 -0800 (PST) From: liuyizhuo To: ffmpeg-devel@ffmpeg.org Date: Sun, 26 Feb 2023 23:02:56 +0800 Message-Id: <20230226150256.7940-1-yizhuo.liu753@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] libavfilter/vf_signalstats.c: add new hsl(Hue, Saturation, Lightness) for filter signalstats X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Cc: wangwei1237@gmail.com, "yizhuo.liu753@gmail.com" Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 8l2ZuHOz1XCu From: "yizhuo.liu753@gmail.com" --- libavfilter/vf_signalstats.c | 244 +++++++++++++++++++++++++++++++---- 1 file changed, 219 insertions(+), 25 deletions(-) diff --git a/libavfilter/vf_signalstats.c b/libavfilter/vf_signalstats.c index b4d1029296..90dbe853fa 100644 --- a/libavfilter/vf_signalstats.c +++ b/libavfilter/vf_signalstats.c @@ -1,7 +1,9 @@ /* * Copyright (c) 2010 Mark Heath mjpeg0 @ silicontrip dot org - * Copyright (c) 2014 Clément Bœsch + * Copyright (c) 2014 Clément Bœsch * Copyright (c) 2014 Dave Rice @dericed + * Copyright (c) 2022 Wang Wei + * Copyright (c) 2022 Liu yizhuo * * This file is part of FFmpeg. * @@ -23,8 +25,8 @@ #include "libavutil/intreadwrite.h" #include "libavutil/opt.h" #include "libavutil/pixdesc.h" -#include "filters.h" #include "internal.h" +#include "libswscale/swscale.h" enum FilterMode { FILTER_NONE = -1, @@ -36,6 +38,8 @@ enum FilterMode { typedef struct SignalstatsContext { const AVClass *class; + int lumah; // height of luma plane + int lumaw; // width of luma plane int chromah; // height of chroma plane int chromaw; // width of chroma plane int hsub; // horizontal subsampling @@ -56,6 +60,11 @@ typedef struct SignalstatsContext { AVFrame *frame_sat; AVFrame *frame_hue; + AVFrame *frame_rgb; + + int *hsl_h; + int *hsl_s; + int *hsl_l; } SignalstatsContext; typedef struct ThreadData { @@ -65,9 +74,21 @@ typedef struct ThreadData { typedef struct ThreadDataHueSatMetrics { const AVFrame *src; - AVFrame *dst_sat, *dst_hue; + AVFrame *dst_sat, *dst_hue, *dst_h, *dst_s, *dst_l; } ThreadDataHueSatMetrics; +typedef struct ThreadDataHSLMetrics { + const AVFrame *src; + int *dst_h, *dst_s, *dst_l; +} ThreadDataHSLMetrics; + +typedef float num; + +static const float EPSILON = 1e-9; + +/** @brief Equal of A and B */ +#define EQ(A,B) ((fabs((A) - (B)) < EPSILON) ? 1 : 0) + #define OFFSET(x) offsetof(SignalstatsContext, x) #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM @@ -101,6 +122,7 @@ static av_cold int init(AVFilterContext *ctx) s->yuv_color[0] = (( 66*r + 129*g + 25*b + (1<<7)) >> 8) + 16; s->yuv_color[1] = ((-38*r + -74*g + 112*b + (1<<7)) >> 8) + 128; s->yuv_color[2] = ((112*r + -94*g + -18*b + (1<<7)) >> 8) + 128; + return 0; } @@ -110,11 +132,15 @@ static av_cold void uninit(AVFilterContext *ctx) av_frame_free(&s->frame_prev); av_frame_free(&s->frame_sat); av_frame_free(&s->frame_hue); + av_frame_free(&s->frame_rgb); av_freep(&s->jobs_rets); av_freep(&s->histy); av_freep(&s->histu); av_freep(&s->histv); av_freep(&s->histsat); + av_freep(&s->hsl_h); + av_freep(&s->hsl_s); + av_freep(&s->hsl_l); } // TODO: add more @@ -151,6 +177,23 @@ static AVFrame *alloc_frame(enum AVPixelFormat pixfmt, int w, int h) return frame; } +static int config_input(AVFilterLink *inlink) +{ + // Video input data avilable + AVFilterContext *ctx = inlink->dst; + SignalstatsContext *s = ctx->priv; + + // free previous buffers in case they are allocated already + av_frame_free(&s->frame_rgb); + s->frame_rgb = alloc_frame(AV_PIX_FMT_RGB24, inlink->w, inlink->h); + + if (!s->frame_rgb) { + return AVERROR(ENOMEM); + } + + return 0; +} + static int config_output(AVFilterLink *outlink) { AVFilterContext *ctx = outlink->src; @@ -172,12 +215,22 @@ static int config_output(AVFilterLink *outlink) outlink->w = inlink->w; outlink->h = inlink->h; + s->lumaw = inlink->w; + s->lumah = inlink->h; + s->chromaw = AV_CEIL_RSHIFT(inlink->w, s->hsub); s->chromah = AV_CEIL_RSHIFT(inlink->h, s->vsub); s->fs = inlink->w * inlink->h; s->cfs = s->chromaw * s->chromah; + s->hsl_h = av_malloc_array(s->lumah, sizeof(*s->hsl_h)); + s->hsl_s = av_malloc_array(s->lumah, sizeof(*s->hsl_s)); + s->hsl_l = av_malloc_array(s->lumah, sizeof(*s->hsl_l)); + if (!s->hsl_h || !s->hsl_s || !s->hsl_l) { + return AVERROR(ENOMEM); + } + s->nb_jobs = FFMAX(1, FFMIN(inlink->h, ff_filter_get_nb_threads(ctx))); s->jobs_rets = av_malloc_array(s->nb_jobs, sizeof(*s->jobs_rets)); if (!s->jobs_rets) @@ -455,6 +508,110 @@ static const struct { {NULL} }; +static void YUV2RGB(const AVFrame* src, enum AVPixelFormat dstFormat, AVFrame* dst) +{ + int width = src->width; + int height = src->height; + + struct SwsContext* conversion = NULL; + conversion = sws_getContext(width, + height, + (enum AVPixelFormat)src->format, + width, + height, + dstFormat, + SWS_FAST_BILINEAR, + NULL, + NULL, + NULL); + sws_scale(conversion, (const uint8_t * const *)src->data, src->linesize, 0, height, dst->data, dst->linesize); + sws_freeContext(conversion); +} + +/** + * @brief Convert an sRGB color to Hue-Saturation-Lightness (HSL) + * + * @param H, S, L pointers to hold the result + * @param R, G, B the input sRGB values scaled in [0,1] + * + * This routine transforms from sRGB to the double hexcone HSL color space + * The sRGB values are assumed to be between 0 and 1. The outputs are + * H = hexagonal hue angle (0 <= H < 360), + * S = { C/(2L) if L <= 1/2 (0 <= S <= 1), + * { C/(2 - 2L) if L > 1/2 + * L = (max(R',G',B') + min(R',G',B'))/2 (0 <= L <= 1), + * where C = max(R',G',B') - min(R',G',B'). + * + * Wikipedia: http://en.wikipedia.org/wiki/HSL_and_HSV + */ +static void RGB2HSL(num *H, num *S, num *L, num R, num G, num B) +{ + num Max = FFMAX3(R, G, B); + num Min = FFMIN3(R, G, B); + num C = Max - Min; + + *L = (Max + Min) / 2; + + if (C > 0) { + if (EQ(Max, R)) { + *H = (G - B) / C; + + if (G < B) { + *H += 6; + } + } else if (EQ(Max, G)) { + *H = 2 + (B - R) / C; + } else { + *H = 4 + (R - G) / C; + } + + *H *= 60; + *S = (*L <= 0.5) ? (C/(2*(*L))) : (C/(2 - 2*(*L))); + } else { + *H = *S = 0; + } +} + +static int compute_hsl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + int i, j; + num H, S, L; + ThreadDataHSLMetrics *td = arg; + const SignalstatsContext *s = ctx->priv; + const AVFrame *src = td->src; + int *dst_h = td->dst_h; + int *dst_s = td->dst_s; + int *dst_l = td->dst_l; + + const int slice_start = (s->lumah * jobnr ) / nb_jobs; + const int slice_end = (s->lumah * (jobnr+1)) / nb_jobs; + + const int lsz_src = src->linesize[0]; + const uint8_t *p_src = src->data[0] + slice_start * lsz_src; + + for (j = slice_start; j < slice_end; j++) { + int line_h = 0, line_s = 0, line_l = 0; + for (i = 0; i < s->lumaw; i++) { + const uint8_t rgbr = p_src[3 * i]; + const uint8_t rgbg = p_src[3 * i + 1]; + const uint8_t rgbb = p_src[3 * i + 2]; + + RGB2HSL(&H, &S, &L, 1.0 * rgbr / 255, 1.0 * rgbg / 255, 1.0 * rgbb / 255); + line_h += (uint16_t)(H + 0.5); + line_s += (uint8_t)(S * 100 + 0.5); + line_l += (uint8_t)(L * 100 + 0.5); + } + + dst_h[j] = line_h / s->lumaw; + dst_s[j] = line_s / s->lumaw; + dst_l[j] = line_l / s->lumaw; + + p_src += lsz_src; + } + + return 0; +} + static int compute_sat_hue_metrics8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) { int i, j; @@ -562,11 +719,12 @@ static int filter_frame8(AVFilterLink *link, AVFrame *in) int accy, accu, accv; int accsat, acchue = 0; int medhue, maxhue; + int avgh = 0, avgs = 0, avgl = 0; int toty = 0, totu = 0, totv = 0, totsat=0; int tothue = 0; int dify = 0, difu = 0, difv = 0; uint16_t masky = 0, masku = 0, maskv = 0; - int ret; + int filtot[FILT_NUMB] = {0}; AVFrame *prev; @@ -589,21 +747,34 @@ static int filter_frame8(AVFilterLink *link, AVFrame *in) if (s->outfilter != FILTER_NONE) { out = av_frame_clone(in); - if (!out) { - av_frame_free(&in); - return AVERROR(ENOMEM); - } - ret = ff_inlink_make_frame_writable(link, &out); - if (ret < 0) { - av_frame_free(&out); - av_frame_free(&in); - return ret; - } + av_frame_make_writable(out); } ff_filter_execute(ctx, compute_sat_hue_metrics8, &td_huesat, NULL, FFMIN(s->chromah, ff_filter_get_nb_threads(ctx))); + // Calculate HSL information. + YUV2RGB(in, AV_PIX_FMT_RGB24, s->frame_rgb); + ThreadDataHSLMetrics td_hsl = { + .src = s->frame_rgb, + .dst_h = s->hsl_h, + .dst_s = s->hsl_s, + .dst_l = s->hsl_l, + }; + ff_filter_execute(ctx, compute_hsl, &td_hsl, + NULL, FFMIN(link->h, ff_filter_get_nb_threads(ctx))); + + int sumh = 0, sums = 0, suml = 0; + for (j = 0; j < s->lumah; j++) { + sumh += s->hsl_h[j]; + sums += s->hsl_s[j]; + suml += s->hsl_l[j]; + } + + avgh = sumh / s->lumah; + avgs = sums / s->lumah; + avgl = suml / s->lumah; + // Calculate luma histogram and difference with previous frame or field. memset(s->histy, 0, s->maxsize * sizeof(*s->histy)); for (j = 0; j < link->h; j++) { @@ -746,6 +917,10 @@ static int filter_frame8(AVFilterLink *link, AVFrame *in) SET_META("HUEMED", "%d", medhue); SET_META("HUEAVG", "%g", 1.0 * tothue / s->cfs); + SET_META("HAVG", "%d", avgh); + SET_META("SAVG", "%d", avgs); + SET_META("LAVG", "%d", avgl); + SET_META("YDIF", "%g", 1.0 * dify / s->fs); SET_META("UDIF", "%g", 1.0 * difu / s->cfs); SET_META("VDIF", "%g", 1.0 * difv / s->cfs); @@ -793,6 +968,7 @@ static int filter_frame16(AVFilterLink *link, AVFrame *in) int accy, accu, accv; int accsat, acchue = 0; int medhue, maxhue; + int avgh = 0, avgs = 0, avgl = 0; int64_t toty = 0, totu = 0, totv = 0, totsat=0; int64_t tothue = 0; int64_t dify = 0, difu = 0, difv = 0; @@ -800,7 +976,7 @@ static int filter_frame16(AVFilterLink *link, AVFrame *in) int filtot[FILT_NUMB] = {0}; AVFrame *prev; - int ret; + AVFrame *sat = s->frame_sat; AVFrame *hue = s->frame_hue; const uint16_t *p_sat = (uint16_t *)sat->data[0]; @@ -820,21 +996,34 @@ static int filter_frame16(AVFilterLink *link, AVFrame *in) if (s->outfilter != FILTER_NONE) { out = av_frame_clone(in); - if (!out) { - av_frame_free(&in); - return AVERROR(ENOMEM); - } - ret = ff_inlink_make_frame_writable(link, &out); - if (ret < 0) { - av_frame_free(&out); - av_frame_free(&in); - return ret; - } + av_frame_make_writable(out); } ff_filter_execute(ctx, compute_sat_hue_metrics16, &td_huesat, NULL, FFMIN(s->chromah, ff_filter_get_nb_threads(ctx))); + // Calculate HSL information. + YUV2RGB(in, AV_PIX_FMT_RGB24, s->frame_rgb); + ThreadDataHSLMetrics td_hsl = { + .src = s->frame_rgb, + .dst_h = s->hsl_h, + .dst_s = s->hsl_s, + .dst_l = s->hsl_l, + }; + ff_filter_execute(ctx, compute_hsl, &td_hsl, + NULL, FFMIN(link->h, ff_filter_get_nb_threads(ctx))); + + int sumh = 0, sums = 0, suml = 0; + for (j = 0; j < s->lumah; j++) { + sumh += s->hsl_h[j]; + sums += s->hsl_s[j]; + suml += s->hsl_l[j]; + } + + avgh = sumh / s->lumah; + avgs = sums / s->lumah; + avgl = suml / s->lumah; + // Calculate luma histogram and difference with previous frame or field. memset(s->histy, 0, s->maxsize * sizeof(*s->histy)); for (j = 0; j < link->h; j++) { @@ -972,6 +1161,10 @@ static int filter_frame16(AVFilterLink *link, AVFrame *in) SET_META("HUEMED", "%d", medhue); SET_META("HUEAVG", "%g", 1.0 * tothue / s->cfs); + SET_META("HAVG", "%d", avgh); + SET_META("SAVG", "%d", avgs); + SET_META("LAVG", "%d", avgl); + SET_META("YDIF", "%g", 1.0 * dify / s->fs); SET_META("UDIF", "%g", 1.0 * difu / s->cfs); SET_META("VDIF", "%g", 1.0 * difv / s->cfs); @@ -1009,6 +1202,7 @@ static const AVFilterPad signalstats_inputs[] = { { .name = "default", .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_input, .filter_frame = filter_frame, }, };