From patchwork Mon Nov 2 02:07:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Wang X-Patchwork-Id: 23320 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 971044493C6 for ; Mon, 2 Nov 2020 04:37:04 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7231068AF26; Mon, 2 Nov 2020 04:37:04 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f195.google.com (mail-pf1-f195.google.com [209.85.210.195]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C192368A994 for ; Mon, 2 Nov 2020 04:36:57 +0200 (EET) Received: by mail-pf1-f195.google.com with SMTP id c20so9779390pfr.8 for ; Sun, 01 Nov 2020 18:36:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=0s0YMoloccGrtayUiXWzO6Tdt2qBcBxLcNJHdv2ADBQ=; b=susn1QzzSTsUVSi3rYoNVeRr+pohdQA/P3PnKz2v/GwzsYBLFEe7h+BkRZJcZaSwHH U4soV0OMpT7+ggEsx7thXfgK8MDqHwWU3XMSrmJ6uKeV/TC2TCx50sr5Jbm67UbdxeZt VtnMHmVM5P40qr0aEjYySbdJmTClBi8Ul5z+vb3DVIvQatkRS/uscyTZbFx/wTt5Eij+ /q5PAtSug8+j+CCI86y0X8HcIkEvvDwP8jbUAviVR2OItWn1lfpG1wQteFhxRQxRYQjW NETN8uRnQx7R3+NX9vIXPoK9iXYMMIr0q0h3nyuTt/h5bDz/iaBpnwv6nILSlLKOUVc/ E73Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=0s0YMoloccGrtayUiXWzO6Tdt2qBcBxLcNJHdv2ADBQ=; b=h5u6tgN09k9E4lF/sisFZqlhTgE9yJrmacU3gvqFQfgehf53NF0dSnY/3/fTawT4FP NpEjIPMsk2YUDyYZWQSVL6kL+zH9Qr6dSY+Aus0Ew6HlAYqW5P3sacVloq0NoiEax4FC HBLWlOipQJT4a+nKJm7OZwZ4AH/2E8n4AZltyMGUxXCvJyGNw1JtDfdTh9iHwKLJ2H3R GgTvpg8ntAoMgCuSTq0PaVmaNLARZDq4gmKPnKTKv/fxI/v8pIJhtjr7ecbCrQxOLPE7 BOOA2RyhJiZ+GwwJiNI2ymdlWkWUfoa0TGNX+iwBY2TTUSx0eBm7mEIEIkS4n7qewC5W hq8g== X-Gm-Message-State: AOAM533z7C2KIuT3QVlsWK19z2UuxB5UfG85L80ZhvCHEeJKQW8b5TEs AgtQ59UlvRmEIkwDH6QwdTiCCF5bw+c= X-Google-Smtp-Source: ABdhPJz1ssHwqbbycUm5sjD7ovlAvJRtYFWnwf4VSZoVCZPz5ZgLd+DiYdvkcjB6mTxFGMBblUF/vA== X-Received: by 2002:a62:1c53:0:b029:155:e9e:d1da with SMTP id c80-20020a621c530000b02901550e9ed1damr19303959pfc.14.1604282867846; Sun, 01 Nov 2020 18:07:47 -0800 (PST) Received: from vpn2.localdomain ([161.117.202.209]) by smtp.gmail.com with ESMTPSA id w4sm5449878pjh.14.2020.11.01.18.07.46 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 01 Nov 2020 18:07:47 -0800 (PST) From: lance.lmwang@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Mon, 2 Nov 2020 10:07:40 +0800 Message-Id: <1604282862-26534-1-git-send-email-lance.lmwang@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1603967178-3765-1-git-send-email-lance.lmwang@gmail.com> References: <1603967178-3765-1-git-send-email-lance.lmwang@gmail.com> Subject: [FFmpeg-devel] [PATCH v5 1/3] avfilter/vf_unsharp: add 10bit support 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 Cc: Limin Wang MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Limin Wang Signed-off-by: Limin Wang --- libavfilter/unsharp.h | 3 + libavfilter/vf_unsharp.c | 162 +++++++++++++++++++++++++---------------------- 2 files changed, 90 insertions(+), 75 deletions(-) diff --git a/libavfilter/unsharp.h b/libavfilter/unsharp.h index a60b30f..253e32d 100644 --- a/libavfilter/unsharp.h +++ b/libavfilter/unsharp.h @@ -48,9 +48,12 @@ typedef struct UnsharpContext { UnsharpFilterParam luma; ///< luma parameters (width, height, amount) UnsharpFilterParam chroma; ///< chroma parameters (width, height, amount) int hsub, vsub; + int bitdepth; + int bps; int nb_threads; int opencl; int (* apply_unsharp)(AVFilterContext *ctx, AVFrame *in, AVFrame *out); + int (* unsharp_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); } UnsharpContext; #endif /* AVFILTER_UNSHARP_H */ diff --git a/libavfilter/vf_unsharp.c b/libavfilter/vf_unsharp.c index 7b430b6..416bf1c 100644 --- a/libavfilter/vf_unsharp.c +++ b/libavfilter/vf_unsharp.c @@ -57,81 +57,90 @@ typedef struct TheadData { int height; } ThreadData; -static int unsharp_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) -{ - ThreadData *td = arg; - UnsharpFilterParam *fp = td->fp; - uint32_t **sc = fp->sc; - uint32_t *sr = fp->sr; - const uint8_t *src2 = NULL; //silence a warning - const int amount = fp->amount; - const int steps_x = fp->steps_x; - const int steps_y = fp->steps_y; - const int scalebits = fp->scalebits; - const int32_t halfscale = fp->halfscale; - - uint8_t *dst = td->dst; - const uint8_t *src = td->src; - const int dst_stride = td->dst_stride; - const int src_stride = td->src_stride; - const int width = td->width; - const int height = td->height; - const int sc_offset = jobnr * 2 * steps_y; - const int sr_offset = jobnr * (MAX_MATRIX_SIZE - 1); - const int slice_start = (height * jobnr) / nb_jobs; - const int slice_end = (height * (jobnr+1)) / nb_jobs; - - int32_t res; - int x, y, z; - uint32_t tmp1, tmp2; - - if (!amount) { - av_image_copy_plane(dst + slice_start * dst_stride, dst_stride, - src + slice_start * src_stride, src_stride, - width, slice_end - slice_start); - return 0; - } - - for (y = 0; y < 2 * steps_y; y++) - memset(sc[sc_offset + y], 0, sizeof(sc[y][0]) * (width + 2 * steps_x)); - - // if this is not the first tile, we start from (slice_start - steps_y), - // so we can get smooth result at slice boundary - if (slice_start > steps_y) { - src += (slice_start - steps_y) * src_stride; - dst += (slice_start - steps_y) * dst_stride; - } - - for (y = -steps_y + slice_start; y < steps_y + slice_end; y++) { - if (y < height) - src2 = src; - - memset(sr + sr_offset, 0, sizeof(sr[0]) * (2 * steps_x - 1)); - for (x = -steps_x; x < width + steps_x; x++) { - tmp1 = x <= 0 ? src2[0] : x >= width ? src2[width-1] : src2[x]; - for (z = 0; z < steps_x * 2; z += 2) { - tmp2 = sr[sr_offset + z + 0] + tmp1; sr[sr_offset + z + 0] = tmp1; - tmp1 = sr[sr_offset + z + 1] + tmp2; sr[sr_offset + z + 1] = tmp2; - } - for (z = 0; z < steps_y * 2; z += 2) { - tmp2 = sc[sc_offset + z + 0][x + steps_x] + tmp1; sc[sc_offset + z + 0][x + steps_x] = tmp1; - tmp1 = sc[sc_offset + z + 1][x + steps_x] + tmp2; sc[sc_offset + z + 1][x + steps_x] = tmp2; - } - if (x >= steps_x && y >= (steps_y + slice_start)) { - const uint8_t *srx = src - steps_y * src_stride + x - steps_x; - uint8_t *dsx = dst - steps_y * dst_stride + x - steps_x; - - res = (int32_t)*srx + ((((int32_t) * srx - (int32_t)((tmp1 + halfscale) >> scalebits)) * amount) >> 16); - *dsx = av_clip_uint8(res); - } - } - if (y >= 0) { - dst += dst_stride; - src += src_stride; - } - } - return 0; +#define DEF_UNSHARP_SLICE_FUNC(name, nbits) \ +static int name##_##nbits(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \ +{ \ + ThreadData *td = arg; \ + UnsharpFilterParam *fp = td->fp; \ + UnsharpContext *s = ctx->priv; \ + uint32_t **sc = fp->sc; \ + uint32_t *sr = fp->sr; \ + const uint##nbits##_t *src2 = NULL; \ + const int amount = fp->amount; \ + const int steps_x = fp->steps_x; \ + const int steps_y = fp->steps_y; \ + const int scalebits = fp->scalebits; \ + const int32_t halfscale = fp->halfscale; \ + \ + uint##nbits##_t *dst = (uint##nbits##_t*)td->dst; \ + const uint##nbits##_t *src = (const uint##nbits##_t *)td->src; \ + int dst_stride = td->dst_stride; \ + int src_stride = td->src_stride; \ + const int width = td->width; \ + const int height = td->height; \ + const int sc_offset = jobnr * 2 * steps_y; \ + const int sr_offset = jobnr * (MAX_MATRIX_SIZE - 1); \ + const int slice_start = (height * jobnr) / nb_jobs; \ + const int slice_end = (height * (jobnr+1)) / nb_jobs; \ + \ + int32_t res; \ + int x, y, z; \ + uint32_t tmp1, tmp2; \ + \ + if (!amount) { \ + av_image_copy_plane(td->dst + slice_start * dst_stride, dst_stride, \ + td->src + slice_start * src_stride, src_stride, \ + width * s->bps, slice_end - slice_start); \ + return 0; \ + } \ + \ + for (y = 0; y < 2 * steps_y; y++) \ + memset(sc[sc_offset + y], 0, sizeof(sc[y][0]) * (width + 2 * steps_x)); \ + \ + dst_stride = dst_stride / s->bps; \ + src_stride = src_stride / s->bps; \ + /* if this is not the first tile, we start from (slice_start - steps_y) */ \ + /* so we can get smooth result at slice boundary */ \ + if (slice_start > steps_y) { \ + src += (slice_start - steps_y) * src_stride; \ + dst += (slice_start - steps_y) * dst_stride; \ + } \ + \ + for (y = -steps_y + slice_start; y < steps_y + slice_end; y++) { \ + if (y < height) \ + src2 = src; \ + \ + memset(sr + sr_offset, 0, sizeof(sr[0]) * (2 * steps_x - 1)); \ + for (x = -steps_x; x < width + steps_x; x++) { \ + tmp1 = x <= 0 ? src2[0] : x >= width ? src2[width-1] : src2[x]; \ + for (z = 0; z < steps_x * 2; z += 2) { \ + tmp2 = sr[sr_offset + z + 0] + tmp1; sr[sr_offset + z + 0] = tmp1; \ + tmp1 = sr[sr_offset + z + 1] + tmp2; sr[sr_offset + z + 1] = tmp2; \ + } \ + for (z = 0; z < steps_y * 2; z += 2) { \ + tmp2 = sc[sc_offset + z + 0][x + steps_x] + tmp1; \ + sc[sc_offset + z + 0][x + steps_x] = tmp1; \ + tmp1 = sc[sc_offset + z + 1][x + steps_x] + tmp2; \ + sc[sc_offset + z + 1][x + steps_x] = tmp2; \ + } \ + if (x >= steps_x && y >= (steps_y + slice_start)) { \ + const uint##nbits##_t *srx = src - steps_y * src_stride + x - steps_x; \ + uint##nbits##_t *dsx = dst - steps_y * dst_stride + x - steps_x; \ + \ + res = (int32_t)*srx + ((((int32_t) * srx - \ + (int32_t)((tmp1 + halfscale) >> scalebits)) * amount) >> (8+nbits)); \ + *dsx = av_clip_uint##nbits(res); \ + } \ + } \ + if (y >= 0) { \ + dst += dst_stride; \ + src += src_stride; \ + } \ + } \ + return 0; \ } +DEF_UNSHARP_SLICE_FUNC(unsharp_slice, 16); +DEF_UNSHARP_SLICE_FUNC(unsharp_slice, 8); static int apply_unsharp_c(AVFilterContext *ctx, AVFrame *in, AVFrame *out) { @@ -155,7 +164,7 @@ static int apply_unsharp_c(AVFilterContext *ctx, AVFrame *in, AVFrame *out) td.height = plane_h[i]; td.dst_stride = out->linesize[i]; td.src_stride = in->linesize[i]; - ctx->internal->execute(ctx, unsharp_slice, &td, NULL, FFMIN(plane_h[i], s->nb_threads)); + ctx->internal->execute(ctx, s->unsharp_slice, &td, NULL, FFMIN(plane_h[i], s->nb_threads)); } return 0; } @@ -238,6 +247,9 @@ static int config_input(AVFilterLink *inlink) s->hsub = desc->log2_chroma_w; s->vsub = desc->log2_chroma_h; + s->bitdepth = desc->comp[0].depth; + s->bps = s->bitdepth > 8 ? 2 : 1; + s->unsharp_slice = s->bitdepth > 8 ? unsharp_slice_16 : unsharp_slice_8; // ensure (height / nb_threads) > 4 * steps_y, // so that we don't have too much overlap between two threads