From patchwork Fri Aug 23 08:41:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Rajiv Harlalka X-Patchwork-Id: 51117 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:b3c7:0:b0:48e:c0f8:d0de with SMTP id g7csp187741vqt; Fri, 23 Aug 2024 01:42:21 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUJlQuCSr60EYu+ji4HcVxtT34bw6+KsDd1pooha0hIYD+9tDXBVWyZE64qHymzjtVUSdNZdPBnixuRaj/6XOhv@gmail.com X-Google-Smtp-Source: AGHT+IGNBFhVt8s8nZ/70j5MUjT1rSfpKpQSjSRdpjsRHwbw4caaMIcgR6ArwaZUQwB51zHLuogz X-Received: by 2002:a05:6512:ea8:b0:52f:cf7e:b1bc with SMTP id 2adb3069b0e04-5343876c2a6mr539678e87.1.1724402541506; Fri, 23 Aug 2024 01:42:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1724402541; cv=none; d=google.com; s=arc-20240605; b=e1vnjOpwwSBCTDGWO8V+lyT0Jp8sveeN93KH89NZuSRb3FNRNXhqfZXt872nGtYO5P y322E1fisGPXAbCf2PDRdwWb2UPLPWzxSJX83DgeSXO36iKc1RKqED9EFUJyQny4h0s7 3E3EEz+AQhdY2p7oUtj41y7r8x7eHkPDzjzyUPmlOxg8OWZrXLnOfAEYSy5qJzGlD3st vosrZA+jR+8DiO+gWycNGtVW0BMItWxHQkr4R/CYgfwBbZZP21a5sWN4mf7eHHjYewoy FqRrvrFnQo4p1wkW0bo4/NroVzH7yB5yKMTq1wWHQMUSNCct/CIi7Bqbl8Ix11d9Kwfx g+kA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=qHFvlLZsTINBG9gMZjro+ZT3ePdgr3i2AhEIxuDqrBw=; fh=vDofb6Cb0l/WrMMHuTNW0kjJ61Koc7sAjaBtezdxdu8=; b=MKQVmFaIY631NMmXwVCO1Y3o4M21iG/AHjkk6l6LMxOfoL44k9fTr449BJtDmY/bDw L1Vh62zqWHeKutfuVC2OEXU24b1+a/3Y66gqE8OPTtSi9H2Y36aGqFeERCfkbfhT5KlC Rzhm2ANU+5qNynbxZPbwiiWvmtVtd6QdGaAj87fLY6UNivgnPVK6gGTKEKtm1GfYxyiu RjcnEBGqD3IryP2eAmpFoVN8lnmm8NLqsbd/Sbq6go8AVe29UeyzHoPz7cdlNUC2JJxN 9+m8Omm8RJfnGPKq1yhYhWohUITZvJ8vMrJ0ezvo64EauEBcksmDbv1Lxf7KMEvvifWw Zg0Q==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=MbdW3ZX3; 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; dara=fail header.i=@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 2adb3069b0e04-5334ea95df5si1144128e87.578.2024.08.23.01.42.20; Fri, 23 Aug 2024 01:42:21 -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=20230601 header.b=MbdW3ZX3; 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; dara=fail header.i=@gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 55BA568DAFF; Fri, 23 Aug 2024 11:42:16 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oo1-f48.google.com (mail-oo1-f48.google.com [209.85.161.48]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 166D568DA99 for ; Fri, 23 Aug 2024 11:42:10 +0300 (EEST) Received: by mail-oo1-f48.google.com with SMTP id 006d021491bc7-5dca9cc71b2so1210427eaf.2 for ; Fri, 23 Aug 2024 01:42:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724402528; x=1725007328; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=abh9g8WJftzlFMb7R9dl7dl2Ft7jUIi3t0+ERKVGSko=; b=MbdW3ZX3Jvdw5WmsHliVnhf+AnCHk4D9X01Exlk4VkyGRVP/hT5U5Ya3ZnEampF97j M5ppI/rPN3ZxU2CPqS7mpHp+Q8YOFOEIEARYCHBlSWRK/6dTIS0b2C/I75+XysvskPWK 0LabLhlsU/uslx2O10WGN0ZLmzbaJAjl0i394rMMr9OwmYsZZjr6+Dot2s3kf300eR4G b62en2o6Yx+t6FxWuvQUYpQVPBX32J3ovkXUc6Os2TdDGlxAs0+xB8tHxpW5d5ZaWd1m Tx2z0fQV6tvFnOq4HsfEY/U9DhElyzH4Ue0z6ACXEmElKJyewgo67IeUd8JJ/G+Yi0LE lj9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724402528; x=1725007328; 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=abh9g8WJftzlFMb7R9dl7dl2Ft7jUIi3t0+ERKVGSko=; b=xSvGt1iYnTkS2acKqQgm+xt3/Ix6cwSsFFSmPzGYs31/Znd9eAJ1aeufTKVNR+dIRF 6rgsfoNe/vWmruYaSUGTBBIBaST3w2MpMQiCKHC4TCmaSU9KC5Zelq6unRzjPyFsI8j8 g8QYc0B//PBmr3k/oTWnCK0ToEOXX73xr8NrL2imWMMhXmKTyR38j69ibuMjh1qKTYKM GCPOdve0GYxUIt1tYJqckPuqQkScOTCB/juNX/aBxkWLiNeuRNeqIyQLLaEOlSb/JlTd df9kAzweS9x5K83cElZ/JWtqH9Riifr2HXtsGk19n33lhK3kxiV/z9NG3npe3mCrwsdO QudQ== X-Gm-Message-State: AOJu0Yxk0FovEj02dIJW+GRVKIJNIPmYvmXapif6EW7MTxYYwSe/FtnL 23rqBOV7t4qyJPRMEoEZxofjmYR81zEenezVuBJqo9XUdNa3R0/zWwt3se3SGNY= X-Received: by 2002:a05:6358:724f:b0:1b3:93b8:8c1e with SMTP id e5c5f4694b2df-1b5c20c625amr180413455d.2.1724402527762; Fri, 23 Aug 2024 01:42:07 -0700 (PDT) Received: from KOSS-server.0xgdoau1yfnebo2q4ljtgdjnrd.rx.internal.cloudapp.net ([20.197.21.123]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-714343388bcsm2557391b3a.189.2024.08.23.01.42.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Aug 2024 01:42:07 -0700 (PDT) From: Rajiv Harlalka To: ffmpeg-devel@ffmpeg.org Date: Fri, 23 Aug 2024 08:41:52 +0000 Message-Id: <20240823084152.43406-1-rajivharlalka009@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avfilter: add pu21 filter 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: Rajiv Harlalka , cosmin@cosmin.at, thilo.borgmann@googlemail.com Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: /y987cn5A1wb Adds the PU21 encoding filter for high dynamic range images and video quality assessment Mantiuk, R., & Azimi, M. PU21: A novel perceptually uniform encoding for adapting existing quality metrics for HDR https://github.com/gfxdisp/pu21 Signed-off-by: Rajiv Harlalka --- Changelog | 1 + libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/version.h | 4 +- libavfilter/vf_pu21.c | 210 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 libavfilter/vf_pu21.c diff --git a/Changelog b/Changelog index 571b27ad98..552aa6028a 100644 --- a/Changelog +++ b/Changelog @@ -18,6 +18,7 @@ version : - D3D12VA HEVC encoder - Cropping metadata parsing and writing in Matroska and MP4/MOV de/muxers - Intel QSV-accelerated VVC decoding +- Addition of vf_pu21 encoding filter version 7.0: diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 63088e9286..102315f5bf 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -121,6 +121,7 @@ OBJS-$(CONFIG_AXCORRELATE_FILTER) += af_axcorrelate.o OBJS-$(CONFIG_AZMQ_FILTER) += f_zmq.o OBJS-$(CONFIG_BANDPASS_FILTER) += af_biquads.o OBJS-$(CONFIG_BANDREJECT_FILTER) += af_biquads.o +OBJS-$(CONFIG_PU21_FILTER) += vf_pu21.o OBJS-$(CONFIG_BASS_FILTER) += af_biquads.o OBJS-$(CONFIG_BIQUAD_FILTER) += af_biquads.o OBJS-$(CONFIG_BS2B_FILTER) += af_bs2b.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 63600e9b58..ced076626c 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -231,6 +231,7 @@ extern const AVFilter ff_vf_convolution_opencl; extern const AVFilter ff_vf_convolve; extern const AVFilter ff_vf_copy; extern const AVFilter ff_vf_coreimage; +extern const AVFilter ff_vf_pu21; extern const AVFilter ff_vf_corr; extern const AVFilter ff_vf_cover_rect; extern const AVFilter ff_vf_crop; diff --git a/libavfilter/version.h b/libavfilter/version.h index d8cd8a2cfb..7e0eb9af97 100644 --- a/libavfilter/version.h +++ b/libavfilter/version.h @@ -31,8 +31,8 @@ #include "version_major.h" -#define LIBAVFILTER_VERSION_MINOR 2 -#define LIBAVFILTER_VERSION_MICRO 102 +#define LIBAVFILTER_VERSION_MINOR 3 +#define LIBAVFILTER_VERSION_MICRO 100 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ diff --git a/libavfilter/vf_pu21.c b/libavfilter/vf_pu21.c new file mode 100644 index 0000000000..c3b56f9c1b --- /dev/null +++ b/libavfilter/vf_pu21.c @@ -0,0 +1,210 @@ + +#include "libavfilter/avfilter.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" +#include "libavutil/mem.h" +#include "libavutil/imgutils.h" +#include "libavfilter/formats.h" +#include "libavutil/internal.h" +#include "video.h" + +static enum pu21_mode { + BANDING, + BANDING_GLARE, + PEAKS, + PEAKS_GLARE +}; + +typedef struct PU21Context { + const AVClass* class; + double L_min, L_max; + int multiplier; + int mode; + + int depth; + int nb_planes; +} PU21Context; + +const float pu21_params[4][7] = { + // Reference: "PU21: A novel perceptually uniform encoding for adapting existing quality metrics for HDR" + // RafaƂ K. Mantiuk and M. Azimi, Picture Coding Symposium 2021 + // https://github.com/gfxdisp/pu21 + {1.070275272f, 0.4088273932f, 0.153224308f, 0.2520326168f, 1.063512885f, 1.14115047f, 521.4527484f}, // BANDING + {0.353487901f, 0.3734658629f, 8.277049286e-05f, 0.9062562627f, 0.09150303166f, 0.9099517204f, 596.3148142f}, // BANDING_GLARE + {1.043882782f, 0.6459495343f, 0.3194584211f, 0.374025247f, 1.114783422f, 1.095360363f, 384.9217577f}, // PEAKS + {816.885024f, 1479.463946f, 0.001253215609f, 0.9329636822f, 0.06746643971f, 1.573435413f, 419.6006374f} // PEAKS_GLARE +}; + + +#define OFFSET(x) offsetof(PU21Context, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM +static const AVOption pu21_options[] = { + { "L_min", "Set the minimum luminance value", OFFSET(L_min), AV_OPT_TYPE_DOUBLE, {.dbl = 0.005}, 0.005, 10000, FLAGS }, + { "L_max", "Set the maximum luminance value", OFFSET(L_max), AV_OPT_TYPE_DOUBLE, {.dbl = 10000}, 0.005, 10000, FLAGS }, + { "multiplier", "Set the parameters for the encoding", OFFSET(multiplier), AV_OPT_TYPE_INT, {.i64 = 1}, 1, 10000, FLAGS}, + { "mode", "Set the mode of the PU21 encoding", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = BANDING_GLARE}, BANDING, PEAKS_GLARE, FLAGS}, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(pu21); + +static av_cold int pu21_init(AVFilterContext* ctx) { + PU21Context* pu21 = ctx->priv; + + pu21->L_max = 300; + pu21->L_min = 0.005; + pu21->multiplier = 1; + pu21->mode = BANDING_GLARE; + return 0; +} + +static float apply_pu21(float pixel_val, PU21Context* pu21) { + float Y, V; + Y = pixel_val * (pu21->multiplier); + V = fmax(pu21_params[pu21->mode][6] * (pow((pu21_params[pu21->mode][0] + pu21_params[pu21->mode][1] * pow(Y, pu21_params[pu21->mode][3])) / (1 + pu21_params[pu21->mode][2] * pow(Y, pu21_params[pu21->mode][3])), pu21_params[pu21->mode][4]) - pu21_params[pu21->mode][5]), 0); + return V; +} + +static void pu21_encode8(AVFilterContext* ctx, AVFrame* in, AVFrame* out) { + PU21Context* pu21 = ctx->priv; + const int height = in->height; + const int width = in->width; + + int plane; + for (plane = 0; plane < pu21->nb_planes; plane++) { + const uint8_t* src8 = in->data[plane]; + uint8_t* dst8 = out->data[plane]; + + int linesize = in->linesize[plane]; + + uint8_t pixel_val; + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + pixel_val = src8[y * linesize + x]; + dst8[y * linesize + x] = (uint8_t)apply_pu21(pixel_val, pu21); + } + } + } +} + +static void pu21_encode16(AVFilterContext* ctx, AVFrame* in, AVFrame* out) { + PU21Context* pu21 = ctx->priv; + const int height = in->height; + const int width = in->width; + + int plane; + for (plane = 0; plane < pu21->nb_planes; plane++) { + const uint16_t* src16 = (uint16_t*)in->data[plane]; + uint16_t* dst16 = (uint16_t*)out->data[plane]; + + int linesize = in->linesize[plane] / 2; + + uint16_t pixel_val; + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + pixel_val = src16[y * linesize + x]; + dst16[y * linesize + x] = (uint16_t)apply_pu21(pixel_val, pu21); + } + } + } +} + +static void pu21_encode32(AVFilterContext* ctx, AVFrame* in, AVFrame* out) { + PU21Context* pu21 = ctx->priv; + const int height = in->height; + const int width = in->width; + + int plane; + for (plane = 0; plane < pu21->nb_planes; plane++) { + const float* src32 = (float*)in->data[plane]; + float* dst32 = (float*)out->data[plane]; + + int linesize = in->linesize[plane] / 4; + + float pixel_val; + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + pixel_val = src32[y * linesize + x]; + dst32[y * linesize + x] = (float)apply_pu21(pixel_val, pu21); + } + } + } +} + +static int filter_frame(AVFilterLink* inlink, AVFrame* input) { + AVFilterContext* ctx = inlink->dst; + PU21Context* pu21 = ctx->priv; + + AVFilterLink* outlink = ctx->outputs[0]; + AVFrame* out; + + if (av_frame_is_writable(input)) { + out = input; + } + else { + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) { + av_frame_free(&input); + return AVERROR(ENOMEM); + } + av_frame_copy_props(out, input); + } + + if (pu21->depth <= 8) { + pu21_encode8(ctx, input, out); + } + else if (pu21->depth <= 16) { + pu21_encode16(ctx, input, out); + } + else { + pu21_encode32(ctx, input, out); + } + + if (out != input) + av_frame_free(&input); + return ff_filter_frame(outlink, out); +} + +static av_cold void uninit(AVFilterContext* ctx) { + PU21Context* s = ctx->priv; +} + +static int config_input(AVFilterLink* inlink) { + const AVPixFmtDescriptor* desc = av_pix_fmt_desc_get(inlink->format); + PU21Context* s = inlink->dst->priv; + + uninit(inlink->dst); + + s->depth = desc->comp[0].depth; + s->nb_planes = av_pix_fmt_count_planes(inlink->format); + + return 0; +} + +static const AVFilterPad pu21_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_input, + .filter_frame = filter_frame, + } +}; + +static const AVFilterPad pu21_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + } +}; + +const AVFilter ff_vf_pu21 = { + .name = "pu21", + .description = NULL_IF_CONFIG_SMALL("Convert HDR linear values to PU values using PU21 transform"), + .priv_size = sizeof(PU21Context), + .init = pu21_init, + .uninit = uninit, + FILTER_INPUTS(pu21_inputs), + FILTER_OUTPUTS(pu21_outputs), + .priv_class = &pu21_class, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, +};