From patchwork Sun Jan 12 19:48:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 17305 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id ABA8D44B0B1 for ; Sun, 12 Jan 2020 21:49:03 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9100168B034; Sun, 12 Jan 2020 21:49:03 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0CE3A6881F2 for ; Sun, 12 Jan 2020 21:48:57 +0200 (EET) Received: by mail-wr1-f65.google.com with SMTP id d16so6528165wre.10 for ; Sun, 12 Jan 2020 11:48:57 -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=mK9UKNP+/ahZxNUYQEvKYl0KcaAm0yxutXVu80ubR1Y=; b=mebTSiR2fodu+qjqc8D0w6qepOGW5Xced2CPPFuqckdgAFkUpVXDccxPrAKIQ70KUf pgiu2Dg+6VLyRZjpazNJX08hxLsX7+dP+9jbHf8lA8K2kdl8l5JV6df3fmW0zP7BpGi6 FD7Ifogh6BZIOz9s4JGgobsPBf4t6ywkj8vY0awZpOMJJQUbqGNiNuHJ3VjIfl5aYW/J O1Q+tA6b8QsAM2fbIR881e67gIB0UYFmP0yRoTpcXL8cMLkH6kaJQsxM1P7WwtChVamE cgDzZDn9XVC35SJk6+ptYv6zzp0/TOEXXCIrS5y/wNM8dBNXkx7DptsWuj/x/ZjjdKJN 2+WQ== 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=mK9UKNP+/ahZxNUYQEvKYl0KcaAm0yxutXVu80ubR1Y=; b=sodfni4xlNE27SDC5RhLg5zKEswpnF/teUWT6DDGgkLTZACF9UR5zsI+jV7hu97QMX dyp2EMILTDMCujDELSBYSwYMRqhqLjxgU00oSs+t8+dWkLhnsObh5t2MYoVgzKk6agHM YrlRbDwrtaqydxduGxnRBI8YbCHpAUBjleKzZMqW7xKGqEyLzuUcse3K5a8KpjGVfWhB fA7qHI0Xj+/xWSNsIbi3MOO9CHH680pECXSDDxX5fyMJq3P92hdzg9aL28dfz3iWlmaE qMtU98vktBrreRqeX1B4p3K0BWQxMfvrcTcCwOIfKs8Ky+u1xsumBqAAnlg2Lo3R8b2v wD3w== X-Gm-Message-State: APjAAAWBsJrsT90pj88erpRsZFL4uzFtXJVm/KdWjX5YVeyzUw2+zpdk Cx8D8sBTQLFIHTmtaeW4nmFRzaSuA2U= X-Google-Smtp-Source: APXvYqwbNG7q+CV77jD0ay782Gb1/vsb94ZSP29sC3gm321sZ1x3vdXtKFFalNxvvzCUqYJJuJ6OVw== X-Received: by 2002:a5d:62c8:: with SMTP id o8mr14606792wrv.316.1578858536165; Sun, 12 Jan 2020 11:48:56 -0800 (PST) Received: from localhost.localdomain ([109.227.51.81]) by smtp.gmail.com with ESMTPSA id r62sm11930484wma.32.2020.01.12.11.48.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2020 11:48:55 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 12 Jan 2020 20:48:44 +0100 Message-Id: <20200112194847.15750-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 1/4] avfilter/vf_normalize: move luts into private context 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/vf_normalize.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libavfilter/vf_normalize.c b/libavfilter/vf_normalize.c index 2e1b274f8c..ef97aecc0e 100644 --- a/libavfilter/vf_normalize.c +++ b/libavfilter/vf_normalize.c @@ -112,6 +112,7 @@ typedef struct NormalizeContext { NormalizeHistory min[3], max[3]; // Min and max for each channel in {R,G,B}. uint8_t *history_mem; // Single allocation for above history entries + uint8_t lut[3][256]; // Lookup table } NormalizeContext; #define OFFSET(x) offsetof(NormalizeContext, x) @@ -139,7 +140,6 @@ static void normalize(NormalizeContext *s, AVFrame *in, AVFrame *out) float rgb_min_smoothed; // Min input range for linked normalization float rgb_max_smoothed; // Max input range for linked normalization - uint8_t lut[3][256]; // Lookup table int x, y, c; // First, scan the input frame to find, for each channel, the minimum @@ -216,7 +216,7 @@ static void normalize(NormalizeContext *s, AVFrame *in, AVFrame *out) if (min[c].smoothed == max[c].smoothed) { // There is no dynamic range to expand. No mapping for this channel. for (in_val = min[c].in; in_val <= max[c].in; in_val++) - lut[c][in_val] = min[c].out; + s->lut[c][in_val] = min[c].out; } else { // We must set lookup values for all values in the original input // range [min.in,max.in]. Since the original input range may be @@ -227,7 +227,7 @@ static void normalize(NormalizeContext *s, AVFrame *in, AVFrame *out) int out_val = (in_val - min[c].smoothed) * scale + min[c].out + 0.5f; out_val = FFMAX(out_val, 0); out_val = FFMIN(out_val, 255); - lut[c][in_val] = out_val; + s->lut[c][in_val] = out_val; } } } @@ -238,7 +238,7 @@ static void normalize(NormalizeContext *s, AVFrame *in, AVFrame *out) uint8_t *outp = out->data[0] + y * out->linesize[0]; for (x = 0; x < in->width; x++) { for (c = 0; c < 3; c++) - outp[s->co[c]] = lut[c][inp[s->co[c]]]; + outp[s->co[c]] = s->lut[c][inp[s->co[c]]]; if (s->num_components == 4) // Copy alpha as-is. outp[s->co[3]] = inp[s->co[3]]; From patchwork Sun Jan 12 19:48:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 17306 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id BBD8C44B0B1 for ; Sun, 12 Jan 2020 21:49:06 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9FC0C68B080; Sun, 12 Jan 2020 21:49:06 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 134E26881F2 for ; Sun, 12 Jan 2020 21:48:58 +0200 (EET) Received: by mail-wr1-f66.google.com with SMTP id d16so6528198wre.10 for ; Sun, 12 Jan 2020 11:48:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=de4BVx9tbmdeEUCOgN/DUzy1ivYg/BO01Ur2y747kbA=; b=OCg9QYnmPmPodPNItgDm3MI1koEw+yjlGhHzzQKFTNo4Ut81DAI9AAvS1nqebz6yta dfxnJLx5ZWP7ymqbOy3U+fuZ990sjRtKfPVDGvixy8rR3HUgqqcTbtk8tqKyXsePW3sB nOPl2hNJ2jysTtmsMUwliDhsc/WWxKJa0Ajw+9+TQIZt1LmVH17yg0u7VMUwEBCiJ+G6 ZhtQ+wMHcO6d36VsCDeHEO4iOF6RmOuFKX8XAGiGpsT0TNrayqzs44fL4Fz4cR3mSypY GCit96VSr8Qkh/qho4+edkUA6kgnKrWI4+UpEMMb4tLSSHMp4f+WZK1MZFR5FpELPjA2 IGXA== 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:in-reply-to :references; bh=de4BVx9tbmdeEUCOgN/DUzy1ivYg/BO01Ur2y747kbA=; b=A4quDhsPb+zKJjNgDV0o2qWTpoOqZuZBol1T8nHF/ddE2OfNVVqpc9FPrsyiBkI9Sm xw+8Ko9117/JnT8jrpifFgpNFxljXMEbo5K0fwscuKV4lTT0WNMlPANWLVQ6q41Ehmsl 4/jxH7LSDDr0SRinzbCd/FkQhSqGUN/om1BofTD91+Yro7uA69aogusIbxiauE3YdRL/ ghpaWY/sIkgXgwvcdm9pbKm+Jhpt6kjPqwezdSBbAUtHQCs+JKxHI7VWODaEOGnVrPev tiU1Z7KgOrpYuIBem8oGskCKKpAiaPOPwtts8T2R89jeVpBZ3v61U7dsjhlCzfyYSFBe OBNg== X-Gm-Message-State: APjAAAX6b5UdJbab69B3sjWr1+sSw3dsHfwzb6KBDKijOLBAoYp5nGap Bk7vi9AflnPAeZXDvFrqG3/x6KTMq78= X-Google-Smtp-Source: APXvYqzkjhRsPY0hc2BPbBUjD/A2pNyXLUe6qUPeiJirdpaFuQ0/S2h7cNCDsRAiu3VMm0jrS5/Ovw== X-Received: by 2002:a5d:5706:: with SMTP id a6mr2160678wrv.108.1578858537131; Sun, 12 Jan 2020 11:48:57 -0800 (PST) Received: from localhost.localdomain ([109.227.51.81]) by smtp.gmail.com with ESMTPSA id r62sm11930484wma.32.2020.01.12.11.48.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2020 11:48:56 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 12 Jan 2020 20:48:45 +0100 Message-Id: <20200112194847.15750-2-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200112194847.15750-1-onemda@gmail.com> References: <20200112194847.15750-1-onemda@gmail.com> Subject: [FFmpeg-devel] [PATCH 2/4] avfilter/vf_normalize: factor code dealing with AVFrame pixels out 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/vf_normalize.c | 69 ++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/libavfilter/vf_normalize.c b/libavfilter/vf_normalize.c index ef97aecc0e..d353d8b75a 100644 --- a/libavfilter/vf_normalize.c +++ b/libavfilter/vf_normalize.c @@ -113,6 +113,9 @@ typedef struct NormalizeContext { uint8_t *history_mem; // Single allocation for above history entries uint8_t lut[3][256]; // Lookup table + + void (*find_min_max)(struct NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]); + void (*process)(struct NormalizeContext *s, AVFrame *in, AVFrame *out); } NormalizeContext; #define OFFSET(x) offsetof(NormalizeContext, x) @@ -130,6 +133,39 @@ static const AVOption normalize_options[] = { AVFILTER_DEFINE_CLASS(normalize); +static void find_min_max(NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]) +{ + for (int c = 0; c < 3; c++) + min[c].in = max[c].in = in->data[0][s->co[c]]; + for (int y = 0; y < in->height; y++) { + uint8_t *inp = in->data[0] + y * in->linesize[0]; + for (int x = 0; x < in->width; x++) { + for (int c = 0; c < 3; c++) { + min[c].in = FFMIN(min[c].in, inp[s->co[c]]); + max[c].in = FFMAX(max[c].in, inp[s->co[c]]); + } + inp += s->step; + } + } +} + +static void process(NormalizeContext *s, AVFrame *in, AVFrame *out) +{ + for (int y = 0; y < in->height; y++) { + uint8_t *inp = in->data[0] + y * in->linesize[0]; + uint8_t *outp = out->data[0] + y * out->linesize[0]; + for (int x = 0; x < in->width; x++) { + for (int c = 0; c < 3; c++) + outp[s->co[c]] = s->lut[c][inp[s->co[c]]]; + if (s->num_components == 4) + // Copy alpha as-is. + outp[s->co[3]] = inp[s->co[3]]; + inp += s->step; + outp += s->step; + } + } +} + // This function is the main guts of the filter. Normalizes the input frame // into the output frame. The frames are known to have the same dimensions // and pixel format. @@ -140,22 +176,11 @@ static void normalize(NormalizeContext *s, AVFrame *in, AVFrame *out) float rgb_min_smoothed; // Min input range for linked normalization float rgb_max_smoothed; // Max input range for linked normalization - int x, y, c; + int c; // First, scan the input frame to find, for each channel, the minimum // (min.in) and maximum (max.in) values present in the channel. - for (c = 0; c < 3; c++) - min[c].in = max[c].in = in->data[0][s->co[c]]; - for (y = 0; y < in->height; y++) { - uint8_t *inp = in->data[0] + y * in->linesize[0]; - for (x = 0; x < in->width; x++) { - for (c = 0; c < 3; c++) { - min[c].in = FFMIN(min[c].in, inp[s->co[c]]); - max[c].in = FFMAX(max[c].in, inp[s->co[c]]); - } - inp += s->step; - } - } + s->find_min_max(s, in, min, max); // Next, for each channel, push min.in and max.in into their respective // histories, to determine the min.smoothed and max.smoothed for this frame. @@ -233,19 +258,7 @@ static void normalize(NormalizeContext *s, AVFrame *in, AVFrame *out) } // Finally, process the pixels of the input frame using the lookup tables. - for (y = 0; y < in->height; y++) { - uint8_t *inp = in->data[0] + y * in->linesize[0]; - uint8_t *outp = out->data[0] + y * out->linesize[0]; - for (x = 0; x < in->width; x++) { - for (c = 0; c < 3; c++) - outp[s->co[c]] = s->lut[c][inp[s->co[c]]]; - if (s->num_components == 4) - // Copy alpha as-is. - outp[s->co[3]] = inp[s->co[3]]; - inp += s->step; - outp += s->step; - } - } + s->process(s, in, out); s->frame_num++; } @@ -311,6 +324,10 @@ static int config_input(AVFilterLink *inlink) s->min[c].history = s->history_mem + (c*2) * s->history_len; s->max[c].history = s->history_mem + (c*2+1) * s->history_len; } + + s->find_min_max = find_min_max; + s->process = process; + return 0; } From patchwork Sun Jan 12 19:48:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 17307 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id E37CC44B0B1 for ; Sun, 12 Jan 2020 21:49:07 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CBF5068B081; Sun, 12 Jan 2020 21:49:07 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 018B868B06B for ; Sun, 12 Jan 2020 21:48:58 +0200 (EET) Received: by mail-wr1-f66.google.com with SMTP id q6so6513530wro.9 for ; Sun, 12 Jan 2020 11:48:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=hicFCjS9oAWXyLos3ylEjidZ/9Kc69svecGtuB/DrF4=; b=CMCwugvtBXC829PZYCNggH7cS3d4wjqtZ6D7DLDim5/Id0lNzxo8KjWXrAVoJOjhZm Y0AmwgjEcsnifxADaMsYM+wCeJVq0Lfl8cmNnTAymDDINPmIb2IgeT1CXBDjiWbz9k57 Ti3hvTkf0zgZBL9QmFTLnOYqkMYkLafv6IQN9c1iBUJOekl6RwB73XM0JnPw+mAradZ3 CDd0BK3CKMuIiJaoah39nLXifISfpqUxP/qh/ImB7Q3bFmCtR7mOd2mbRa22BRX7lRUA S4SSwyKrscZYwyFu8BWGZNXUHB0I9/dWRtgk2kln+t5cQbz0VwrmTVUANc/B/gybaBcr KrAg== 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:in-reply-to :references; bh=hicFCjS9oAWXyLos3ylEjidZ/9Kc69svecGtuB/DrF4=; b=NtQAxEUJWP7XhFDwkybZychCz37PPL/lRkGK28aTqsf2MmeFnrn4n6YwBJ5Bw0yzIn u7mIlryS3ZKQnpCCWVR/FO0Ft3fQzPCtNv+y89OIaRkHO42hpbTE/eGJsSC/HjN04Mav eTIuc4W+HWcN0OQcnEZmqOeAtgcb7j16BAxJTmz5D6PrDsscvQ5YZk4xmDqp8Lc7jfgk pOT+ckl6wkWD0i6th4bGAqMziKwA0mK8+Xj0tyM6g3Ga3xO9T+bGlT2RtG+cap5CwdLo 2CQ14hzRFI7FgcqkHQiVoVzvPUnhLazZ+luotrEJVgTbKftYcpehDansFV/jHx7GXmQU bZdg== X-Gm-Message-State: APjAAAXAZK78Ad42A6/HI3THXMZ/plbaC1GOT8rx3GAHqf3L1kZt5KA4 kG5MF3BoVwcI8gRp93xjNUIuSfRkVzU= X-Google-Smtp-Source: APXvYqxX1d1ajtey90422EbrmDIsVBSAmKHuHKhHSO23w5QbdN0D9H3ZAcV1xatAfhPlvgLeq64CtQ== X-Received: by 2002:adf:cf0a:: with SMTP id o10mr14074461wrj.325.1578858538184; Sun, 12 Jan 2020 11:48:58 -0800 (PST) Received: from localhost.localdomain ([109.227.51.81]) by smtp.gmail.com with ESMTPSA id r62sm11930484wma.32.2020.01.12.11.48.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2020 11:48:57 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 12 Jan 2020 20:48:46 +0100 Message-Id: <20200112194847.15750-3-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200112194847.15750-1-onemda@gmail.com> References: <20200112194847.15750-1-onemda@gmail.com> Subject: [FFmpeg-devel] [PATCH 3/4] avfilter/vf_normalize: add support for planar rgb 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/vf_normalize.c | 51 +++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/libavfilter/vf_normalize.c b/libavfilter/vf_normalize.c index d353d8b75a..6a8a7631bc 100644 --- a/libavfilter/vf_normalize.c +++ b/libavfilter/vf_normalize.c @@ -166,6 +166,47 @@ static void process(NormalizeContext *s, AVFrame *in, AVFrame *out) } } +static void find_min_max_planar(NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]) +{ + min[0].in = max[0].in = in->data[2][0]; + min[1].in = max[1].in = in->data[0][0]; + min[2].in = max[2].in = in->data[1][0]; + for (int y = 0; y < in->height; y++) { + uint8_t *inrp = in->data[2] + y * in->linesize[2]; + uint8_t *ingp = in->data[0] + y * in->linesize[0]; + uint8_t *inbp = in->data[1] + y * in->linesize[1]; + for (int x = 0; x < in->width; x++) { + min[0].in = FFMIN(min[0].in, inrp[x]); + max[0].in = FFMAX(max[0].in, inrp[x]); + min[1].in = FFMIN(min[1].in, ingp[x]); + max[1].in = FFMAX(max[1].in, ingp[x]); + min[2].in = FFMIN(min[2].in, inbp[x]); + max[2].in = FFMAX(max[2].in, inbp[x]); + } + } +} + +static void process_planar(NormalizeContext *s, AVFrame *in, AVFrame *out) +{ + for (int y = 0; y < in->height; y++) { + uint8_t *inrp = in->data[2] + y * in->linesize[2]; + uint8_t *ingp = in->data[0] + y * in->linesize[0]; + uint8_t *inbp = in->data[1] + y * in->linesize[1]; + uint8_t *inap = in->data[3] + y * in->linesize[3]; + uint8_t *outrp = out->data[2] + y * out->linesize[2]; + uint8_t *outgp = out->data[0] + y * out->linesize[0]; + uint8_t *outbp = out->data[1] + y * out->linesize[1]; + uint8_t *outap = out->data[3] + y * out->linesize[3]; + for (int x = 0; x < in->width; x++) { + outrp[x] = s->lut[0][inrp[x]]; + outgp[x] = s->lut[1][ingp[x]]; + outbp[x] = s->lut[2][inbp[x]]; + if (s->num_components == 4) + outap[x] = inap[x]; + } + } +} + // This function is the main guts of the filter. Normalizes the input frame // into the output frame. The frames are known to have the same dimensions // and pixel format. @@ -283,6 +324,8 @@ static int query_formats(AVFilterContext *ctx) AV_PIX_FMT_RGB0, AV_PIX_FMT_0BGR, AV_PIX_FMT_BGR0, + AV_PIX_FMT_GBRAP, + AV_PIX_FMT_GBRP, AV_PIX_FMT_NONE }; // According to filter_design.txt, using ff_set_common_formats() this way @@ -302,7 +345,7 @@ static int config_input(AVFilterLink *inlink) NormalizeContext *s = inlink->dst->priv; // Store offsets to R,G,B,A bytes respectively in each pixel const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); - int c; + int c, planar; ff_fill_rgba_map(s->co, inlink->format); s->num_components = desc->nb_components; @@ -325,8 +368,10 @@ static int config_input(AVFilterLink *inlink) s->max[c].history = s->history_mem + (c*2+1) * s->history_len; } - s->find_min_max = find_min_max; - s->process = process; + planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR; + + s->find_min_max = planar ? find_min_max_planar : find_min_max; + s->process = planar? process_planar : process; return 0; } From patchwork Sun Jan 12 19:48:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 17308 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 7928044943D for ; Sun, 12 Jan 2020 21:54:16 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4F62768037C; Sun, 12 Jan 2020 21:54:16 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A04DD68037C for ; Sun, 12 Jan 2020 21:54:09 +0200 (EET) Received: by mail-wm1-f66.google.com with SMTP id 20so7403763wmj.4 for ; Sun, 12 Jan 2020 11:54:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=LFa4pfYb4aAjv8Jv5ecPnRGR+z220O199makLAdc1c4=; b=Z2/V+6+AxBG+vA6MCJYjoqvzNJAJx3mE50t8ngnD6EQTT1oOJ5XXrZ/T1Dtu0C0Cnt LjzNK5wzKuD+ocN6Hlj1uFbu2sj8zaQt52pgdjl5ji8yocSoQUwm+7Udwo1qium0p3+r 5+Oxi1V2OtaJlq/rIFF5bhQomTrw9RMJctBDJma2fO+jGRdo1KMS9F7AYLmHTVPj+K2f gVBoaIFis4YrOFAtyzzdCskho1DAY8yICr9CyImMW2rO4TEssCrIrEq66FJtVR477wVf BTa5jtIr6bvj5FEH1rnrgo9NoEaIewPBKEZqnRne0kasEEgVkqq/tuZO8NeZZE1epJxs xHlw== 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:in-reply-to :references; bh=LFa4pfYb4aAjv8Jv5ecPnRGR+z220O199makLAdc1c4=; b=I5XGB1zfXAruR4/Qv4dCLcPgjwaZ+VWdLbV19/vJjDQZG4i3TQBTzcJWh707o2k8/c spH9mcHpA1TJNvXeB0v3ovyoyp+fvVv5DFdJvrb6Wzpf63Mj0NA3XRVmgnNY1gwSolkY O62/xcg+H5LEm1ajuQ0ySMf3cq8guUfWz2JaTsREyZ3HPsCDIOby1aEiY/1IpPSMkhRO IyyD3jl4Pr1IIA0dz6kUdpH00Se2YqPKcYUgfDlGsLBNgoH85NmviInNlgjzMFZi7R9c 3YhNqBwAu61sp/RRwNkpk09eJJAAdkqnIcFJLicswSukKIzI2NXZ9yzHlavsnXkodNdt JhVg== X-Gm-Message-State: APjAAAXZw/nkiWfDB5kU7/Nh3chF/iTigHA4Lg4t/1qXz5Qd7uNsfBRh ZMLVgCKI6KuDR0gr5zgv9TVhRcg0/s8= X-Google-Smtp-Source: APXvYqzdcu8sOHkI9MExJmpKdP59gPJP0rakXfQJGo+w+YIa0C2YeYdiOVICaSxe2PaOtagNJzmo0w== X-Received: by 2002:a7b:c084:: with SMTP id r4mr15309998wmh.99.1578858539200; Sun, 12 Jan 2020 11:48:59 -0800 (PST) Received: from localhost.localdomain ([109.227.51.81]) by smtp.gmail.com with ESMTPSA id r62sm11930484wma.32.2020.01.12.11.48.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2020 11:48:58 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 12 Jan 2020 20:48:47 +0100 Message-Id: <20200112194847.15750-4-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200112194847.15750-1-onemda@gmail.com> References: <20200112194847.15750-1-onemda@gmail.com> Subject: [FFmpeg-devel] [PATCH 4/4] avfilter/vf_normalize: add support for >8 depth 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/vf_normalize.c | 121 ++++++++++++++++++++++++++++++++----- 1 file changed, 105 insertions(+), 16 deletions(-) diff --git a/libavfilter/vf_normalize.c b/libavfilter/vf_normalize.c index 6a8a7631bc..a008c14f3c 100644 --- a/libavfilter/vf_normalize.c +++ b/libavfilter/vf_normalize.c @@ -73,6 +73,7 @@ */ #include "libavutil/imgutils.h" +#include "libavutil/intreadwrite.h" #include "libavutil/opt.h" #include "libavutil/pixdesc.h" #include "avfilter.h" @@ -82,12 +83,12 @@ #include "video.h" typedef struct NormalizeHistory { - uint8_t *history; // History entries. - uint32_t history_sum; // Sum of history entries. + uint16_t *history; // History entries. + uint64_t history_sum; // Sum of history entries. } NormalizeHistory; typedef struct NormalizeLocal { - uint8_t in; // Original input byte value for this frame. + uint16_t in; // Original input byte value for this frame. float smoothed; // Smoothed input value [0,255]. float out; // Output value [0,255] } NormalizeLocal; @@ -103,6 +104,9 @@ typedef struct NormalizeContext { float strength; uint8_t co[4]; // Offsets to R,G,B,A bytes respectively in each pixel + int depth; + int sblackpt[4]; + int swhitept[4]; int num_components; // Number of components in the pixel format int step; int history_len; // Number of frames to average; based on smoothing factor @@ -110,9 +114,9 @@ typedef struct NormalizeContext { // Per-extremum, per-channel history, for temporal smoothing. NormalizeHistory min[3], max[3]; // Min and max for each channel in {R,G,B}. - uint8_t *history_mem; // Single allocation for above history entries + uint16_t *history_mem; // Single allocation for above history entries - uint8_t lut[3][256]; // Lookup table + uint16_t lut[3][65536]; // Lookup table void (*find_min_max)(struct NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]); void (*process)(struct NormalizeContext *s, AVFrame *in, AVFrame *out); @@ -207,6 +211,80 @@ static void process_planar(NormalizeContext *s, AVFrame *in, AVFrame *out) } } +static void find_min_max_16(NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]) +{ + for (int c = 0; c < 3; c++) + min[c].in = max[c].in = AV_RN16(in->data[0] + 2 * s->co[c]); + for (int y = 0; y < in->height; y++) { + uint16_t *inp = (uint16_t *)(in->data[0] + y * in->linesize[0]); + for (int x = 0; x < in->width; x++) { + for (int c = 0; c < 3; c++) { + min[c].in = FFMIN(min[c].in, inp[s->co[c]]); + max[c].in = FFMAX(max[c].in, inp[s->co[c]]); + } + inp += s->step; + } + } +} + +static void process_16(NormalizeContext *s, AVFrame *in, AVFrame *out) +{ + for (int y = 0; y < in->height; y++) { + uint16_t *inp = (uint16_t *)(in->data[0] + y * in->linesize[0]); + uint16_t *outp = (uint16_t *)(out->data[0] + y * out->linesize[0]); + for (int x = 0; x < in->width; x++) { + for (int c = 0; c < 3; c++) + outp[s->co[c]] = s->lut[c][inp[s->co[c]]]; + if (s->num_components == 4) + // Copy alpha as-is. + outp[s->co[3]] = inp[s->co[3]]; + inp += s->step; + outp += s->step; + } + } +} + +static void find_min_max_planar_16(NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]) +{ + min[0].in = max[0].in = AV_RN16(in->data[2]); + min[1].in = max[1].in = AV_RN16(in->data[0]); + min[2].in = max[2].in = AV_RN16(in->data[1]); + for (int y = 0; y < in->height; y++) { + uint16_t *inrp = (uint16_t *)(in->data[2] + y * in->linesize[2]); + uint16_t *ingp = (uint16_t *)(in->data[0] + y * in->linesize[0]); + uint16_t *inbp = (uint16_t *)(in->data[1] + y * in->linesize[1]); + for (int x = 0; x < in->width; x++) { + min[0].in = FFMIN(min[0].in, inrp[x]); + max[0].in = FFMAX(max[0].in, inrp[x]); + min[1].in = FFMIN(min[1].in, ingp[x]); + max[1].in = FFMAX(max[1].in, ingp[x]); + min[2].in = FFMIN(min[2].in, inbp[x]); + max[2].in = FFMAX(max[2].in, inbp[x]); + } + } +} + +static void process_planar_16(NormalizeContext *s, AVFrame *in, AVFrame *out) +{ + for (int y = 0; y < in->height; y++) { + uint16_t *inrp = (uint16_t *)(in->data[2] + y * in->linesize[2]); + uint16_t *ingp = (uint16_t *)(in->data[0] + y * in->linesize[0]); + uint16_t *inbp = (uint16_t *)(in->data[1] + y * in->linesize[1]); + uint16_t *inap = (uint16_t *)(in->data[3] + y * in->linesize[3]); + uint16_t *outrp = (uint16_t *)(out->data[2] + y * out->linesize[2]); + uint16_t *outgp = (uint16_t *)(out->data[0] + y * out->linesize[0]); + uint16_t *outbp = (uint16_t *)(out->data[1] + y * out->linesize[1]); + uint16_t *outap = (uint16_t *)(out->data[3] + y * out->linesize[3]); + for (int x = 0; x < in->width; x++) { + outrp[x] = s->lut[0][inrp[x]]; + outgp[x] = s->lut[1][ingp[x]]; + outbp[x] = s->lut[2][inbp[x]]; + if (s->num_components == 4) + outap[x] = inap[x]; + } + } +} + // This function is the main guts of the filter. Normalizes the input frame // into the output frame. The frames are known to have the same dimensions // and pixel format. @@ -269,9 +347,9 @@ static void normalize(NormalizeContext *s, AVFrame *in, AVFrame *out) // Calculate the output range [min.out,max.out] as a ratio of the full- // strength output range [blackpt,whitept] and the original input range // [min.in,max.in], based on the user-specified filter strength. - min[c].out = (s->blackpt[c] * s->strength) + min[c].out = (s->sblackpt[c] * s->strength) + (min[c].in * (1.0f - s->strength)); - max[c].out = (s->whitept[c] * s->strength) + max[c].out = (s->swhitept[c] * s->strength) + (max[c].in * (1.0f - s->strength)); // Now, build a lookup table which linearly maps the adjusted input range @@ -291,8 +369,7 @@ static void normalize(NormalizeContext *s, AVFrame *in, AVFrame *out) float scale = (max[c].out - min[c].out) / (max[c].smoothed - min[c].smoothed); for (in_val = min[c].in; in_val <= max[c].in; in_val++) { int out_val = (in_val - min[c].smoothed) * scale + min[c].out + 0.5f; - out_val = FFMAX(out_val, 0); - out_val = FFMIN(out_val, 255); + out_val = av_clip_uintp2_c(out_val, s->depth); s->lut[c][in_val] = out_val; } } @@ -324,8 +401,11 @@ static int query_formats(AVFilterContext *ctx) AV_PIX_FMT_RGB0, AV_PIX_FMT_0BGR, AV_PIX_FMT_BGR0, - AV_PIX_FMT_GBRAP, - AV_PIX_FMT_GBRP, + AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48, + AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64, + 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_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_NONE }; // According to filter_design.txt, using ff_set_common_formats() this way @@ -345,11 +425,13 @@ static int config_input(AVFilterLink *inlink) NormalizeContext *s = inlink->dst->priv; // Store offsets to R,G,B,A bytes respectively in each pixel const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); - int c, planar; + int c, planar, scale; ff_fill_rgba_map(s->co, inlink->format); + s->depth = desc->comp[0].depth; + scale = 1 << (s->depth - 8); s->num_components = desc->nb_components; - s->step = av_get_padded_bits_per_pixel(desc) >> 3; + s->step = av_get_padded_bits_per_pixel(desc) >> (3 + (s->depth > 8)); // Convert smoothing value to history_len (a count of frames to average, // must be at least 1). Currently this is a direct assignment, but the // smoothing value was originally envisaged as a number of seconds. In @@ -359,19 +441,26 @@ static int config_input(AVFilterLink *inlink) // Allocate the history buffers -- there are 6 -- one for each extrema. // s->smoothing is limited to INT_MAX/8, so that (s->history_len * 6) // can't overflow on 32bit causing a too-small allocation. - s->history_mem = av_malloc(s->history_len * 6); + s->history_mem = av_malloc(s->history_len * 6 * sizeof(*s->history_mem)); if (s->history_mem == NULL) return AVERROR(ENOMEM); for (c = 0; c < 3; c++) { s->min[c].history = s->history_mem + (c*2) * s->history_len; s->max[c].history = s->history_mem + (c*2+1) * s->history_len; + s->sblackpt[c] = scale * s->blackpt[c] + (s->blackpt[c] >> (s->depth - 8)); + s->swhitept[c] = scale * s->whitept[c] + (s->whitept[c] >> (s->depth - 8)); } planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR; - s->find_min_max = planar ? find_min_max_planar : find_min_max; - s->process = planar? process_planar : process; + if (s->depth <= 8) { + s->find_min_max = planar ? find_min_max_planar : find_min_max; + s->process = planar? process_planar : process; + } else { + s->find_min_max = planar ? find_min_max_planar_16 : find_min_max_16; + s->process = planar? process_planar_16 : process_16; + } return 0; }