Thanks for review, Timo.

Please find revised patch attached.
(de)interlacing is now removed, doc entry was added as well.
________________________________
От: Timo Rothenpieler <t...@rothenpieler.org>
Отправлено: 28 сентября 2021 г. 22:58
Кому: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>; 
Roman Arzumanyan <rarzuman...@nvidia.com>
Копия: Yogender Gupta <ygu...@nvidia.com>; Ricardo Monteiro 
<rmonte...@nvidia.com>
Тема: Re: [FFmpeg-devel] [PATCH] avfilter: add sharpen_npp video filter

External email: Use caution opening links or attachments

From 422be4d348a07e1abbdae0208a20cd1f3ce70e28 Mon Sep 17 00:00:00 2001
From: Roman Arzumanyan <rarzuman...@nvidia.com>
Date: Mon, 6 Sep 2021 14:26:27 +0300
Subject: [PATCH] avcodec/sharpen_npp: adding sharpening video filter with
 borders control

---
 configure                    |   5 +-
 doc/filters.texi             |  17 +++
 libavfilter/Makefile         |   1 +
 libavfilter/allfilters.c     |   1 +
 libavfilter/version.h        |   2 +-
 libavfilter/vf_sharpen_npp.c | 284 +++++++++++++++++++++++++++++++++++
 6 files changed, 307 insertions(+), 3 deletions(-)
 create mode 100644 libavfilter/vf_sharpen_npp.c

diff --git a/configure b/configure
index 231d0398a8..5e630e6e5d 100755
--- a/configure
+++ b/configure
@@ -3094,6 +3094,7 @@ thumbnail_cuda_filter_deps_any="cuda_nvcc cuda_llvm"
 transpose_npp_filter_deps="ffnvcodec libnpp"
 overlay_cuda_filter_deps="ffnvcodec"
 overlay_cuda_filter_deps_any="cuda_nvcc cuda_llvm"
+sharpen_npp_filter_deps="ffnvcodec libnpp"
 
 amf_deps_any="libdl LoadLibrary"
 nvenc_deps="ffnvcodec"
@@ -6444,8 +6445,8 @@ enabled libmodplug        && require_pkg_config libmodplug libmodplug libmodplug
 enabled libmp3lame        && require "libmp3lame >= 3.98.3" lame/lame.h lame_set_VBR_quality -lmp3lame $libm_extralibs
 enabled libmysofa         && { check_pkg_config libmysofa libmysofa mysofa.h mysofa_neighborhood_init_withstepdefine ||
                                require libmysofa mysofa.h mysofa_neighborhood_init_withstepdefine -lmysofa $zlib_extralibs; }
-enabled libnpp            && { check_lib libnpp npp.h nppGetLibVersion -lnppig -lnppicc -lnppc -lnppidei ||
-                               check_lib libnpp npp.h nppGetLibVersion -lnppi -lnppc -lnppidei ||
+enabled libnpp            && { check_lib libnpp npp.h nppGetLibVersion -lnppig -lnppicc -lnppc -lnppidei -lnppif ||
+                               check_lib libnpp npp.h nppGetLibVersion -lnppi -lnppif -lnppc -lnppidei ||
                                die "ERROR: libnpp not found"; }
 enabled libopencore_amrnb && require libopencore_amrnb opencore-amrnb/interf_dec.h Decoder_Interface_init -lopencore-amrnb
 enabled libopencore_amrwb && require libopencore_amrwb opencore-amrwb/dec_if.h D_IF_init -lopencore-amrwb
diff --git a/doc/filters.texi b/doc/filters.texi
index 568c2995bc..c9bd4ac87d 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -18888,6 +18888,23 @@ Keep the same colorspace property (default).
 @end table
 @end table
 
+@section sharpen_npp
+Use the NVIDIA Performance Primitives (libnpp) to perform image sharpening with
+border control.
+
+The following additional options are accepted:
+@table @option
+
+@item border_type
+Type of sampling to be used ad frame borders. One of the following:
+@table @option
+
+@item replicate
+Replicate pixel values.
+
+@end table
+@end table
+
 @section shear
 Apply shear transform to input video.
 
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index fa2d18e45f..ab5e0ce075 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -426,6 +426,7 @@ OBJS-$(CONFIG_SETRANGE_FILTER)               += vf_setparams.o
 OBJS-$(CONFIG_SETSAR_FILTER)                 += vf_aspect.o
 OBJS-$(CONFIG_SETTB_FILTER)                  += settb.o
 OBJS-$(CONFIG_SHARPNESS_VAAPI_FILTER)        += vf_misc_vaapi.o vaapi_vpp.o
+OBJS-$(CONFIG_SHARPEN_NPP_FILTER)            += vf_sharpen_npp.o
 OBJS-$(CONFIG_SHEAR_FILTER)                  += vf_shear.o
 OBJS-$(CONFIG_SHOWINFO_FILTER)               += vf_showinfo.o
 OBJS-$(CONFIG_SHOWPALETTE_FILTER)            += vf_showpalette.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 338561a446..002626cef7 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -407,6 +407,7 @@ extern const AVFilter ff_vf_setrange;
 extern const AVFilter ff_vf_setsar;
 extern const AVFilter ff_vf_settb;
 extern const AVFilter ff_vf_sharpness_vaapi;
+extern const AVFilter ff_vf_sharpen_npp;
 extern const AVFilter ff_vf_shear;
 extern const AVFilter ff_vf_showinfo;
 extern const AVFilter ff_vf_showpalette;
diff --git a/libavfilter/version.h b/libavfilter/version.h
index ab5eb14634..dfbc1bd7c2 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -30,7 +30,7 @@
 #include "libavutil/version.h"
 
 #define LIBAVFILTER_VERSION_MAJOR   8
-#define LIBAVFILTER_VERSION_MINOR  10
+#define LIBAVFILTER_VERSION_MINOR  11
 #define LIBAVFILTER_VERSION_MICRO 100
 
 
diff --git a/libavfilter/vf_sharpen_npp.c b/libavfilter/vf_sharpen_npp.c
new file mode 100644
index 0000000000..f34c6706a5
--- /dev/null
+++ b/libavfilter/vf_sharpen_npp.c
@@ -0,0 +1,284 @@
+/*
+ * 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
+ */
+
+/**
+ * @file
+ * NPP sharpen video filter
+ */
+
+#include <nppi.h>
+#include <nppi_filtering_functions.h>
+
+#include "internal.h"
+#include "libavutil/cuda_check.h"
+#include "libavutil/hwcontext.h"
+#include "libavutil/hwcontext_cuda_internal.h"
+#include "libavutil/opt.h"
+
+#define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, device_hwctx->internal->cuda_dl, x)
+
+static const enum AVPixelFormat supported_formats[] = {
+    AV_PIX_FMT_YUV420P,
+    AV_PIX_FMT_YUV444P,
+};
+
+typedef struct NPPSharpenContext {
+    const AVClass* class;
+
+    AVBufferRef* frames_ctx;
+    AVFrame* own_frame;
+    AVFrame* tmp_frame;
+
+    NppiBorderType border_type;
+} NPPSharpenContext;
+
+static int nppsharpen_init(AVFilterContext* ctx)
+{
+    NPPSharpenContext* s = ctx->priv;
+
+    s->own_frame = av_frame_alloc();
+    if (!s->own_frame)
+        goto fail;
+
+    s->tmp_frame = av_frame_alloc();
+    if (!s->tmp_frame)
+        goto fail;
+
+    return 0;
+
+fail:
+    av_frame_free(&s->own_frame);
+    av_frame_free(&s->tmp_frame);
+    return AVERROR(ENOMEM);
+}
+
+static int nppsharpen_config(AVFilterContext* ctx, int width, int height)
+{
+    NPPSharpenContext* s = ctx->priv;
+    AVHWFramesContext *out_ctx, *in_ctx;
+    int i, ret, supported_format = 0;
+
+    if (!ctx->inputs[0]->hw_frames_ctx) {
+        av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
+        goto fail;
+    }
+
+    in_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data;
+
+    s->frames_ctx = av_hwframe_ctx_alloc(in_ctx->device_ref);
+    if (!s->frames_ctx)
+        goto fail;
+
+    out_ctx = (AVHWFramesContext*)s->frames_ctx->data;
+    out_ctx->format = AV_PIX_FMT_CUDA;
+    out_ctx->sw_format = in_ctx->sw_format;
+    out_ctx->width = FFALIGN(width, 32);
+    out_ctx->height = FFALIGN(height, 32);
+
+    for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
+        if (in_ctx->sw_format == supported_formats[i]) {
+            supported_format = 1;
+            break;
+        }
+    }
+
+    if (!supported_format) {
+        av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format\n");
+        goto fail;
+    }
+
+    ret = av_hwframe_ctx_init(s->frames_ctx);
+    if (ret < 0)
+        goto fail;
+
+    ret = av_hwframe_get_buffer(s->frames_ctx, s->own_frame, 0);
+    if (ret < 0)
+        goto fail;
+
+    ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->frames_ctx);
+    if (!ctx->outputs[0]->hw_frames_ctx)
+        goto fail;
+
+    return 0;
+
+fail:
+    av_buffer_unref(&s->frames_ctx);
+    return AVERROR(ENOMEM);
+}
+
+static void nppsharpen_uninit(AVFilterContext* ctx)
+{
+    NPPSharpenContext* s = ctx->priv;
+
+    av_buffer_unref(&s->frames_ctx);
+    av_frame_free(&s->own_frame);
+    av_frame_free(&s->tmp_frame);
+}
+
+static int nppsharpen_query_formats(AVFilterContext* ctx)
+{
+    static const enum AVPixelFormat pixel_formats[] = {
+        AV_PIX_FMT_CUDA,
+        AV_PIX_FMT_NONE,
+    };
+    AVFilterFormats* pix_fmts = ff_make_format_list(pixel_formats);
+
+    return ff_set_common_formats(ctx, pix_fmts);
+}
+
+static int nppsharpen_config_props(AVFilterLink* outlink)
+{
+    AVFilterLink* inlink = outlink->src->inputs[0];
+
+    outlink->w = inlink->w;
+    outlink->h = inlink->h;
+
+    if (inlink->sample_aspect_ratio.num)
+        outlink->sample_aspect_ratio = av_mul_q(
+            (AVRational){outlink->h * inlink->w, outlink->w * inlink->h},
+            inlink->sample_aspect_ratio);
+    else
+        outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
+
+    nppsharpen_config(outlink->src, inlink->w, inlink->h);
+
+    return 0;
+}
+
+static int nppsharpen_sharpen(AVFilterContext* ctx, AVFrame* out, AVFrame* in)
+{
+    AVHWFramesContext* in_ctx =
+        (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data;
+    NPPSharpenContext* s = ctx->priv;
+
+    for (int i = 0; i < FF_ARRAY_ELEMS(in->data) && in->data[i]; i++) {
+        int chroma_shift = (AV_PIX_FMT_YUV420P == in_ctx->sw_format) && (i > 0);
+        int ow = in->width >> chroma_shift;
+        int oh = in->height >> chroma_shift;
+
+        NppStatus err = nppiFilterSharpenBorder_8u_C1R(
+            in->data[i], in->linesize[i], (NppiSize){ow, oh}, (NppiPoint){0, 0},
+            out->data[i], out->linesize[i], (NppiSize){ow, oh}, s->border_type);
+        if (err != NPP_SUCCESS) {
+            av_log(ctx, AV_LOG_ERROR, "NPP sharpen error: %d\n", err);
+            return AVERROR_UNKNOWN;
+        }
+    }
+
+    return 0;
+}
+
+static int nppsharpen_filter_frame(AVFilterLink* link, AVFrame* in)
+{
+    AVFilterContext* ctx = link->dst;
+    NPPSharpenContext* s = ctx->priv;
+    AVFilterLink* outlink = ctx->outputs[0];
+    AVHWFramesContext* frames_ctx =
+        (AVHWFramesContext*)outlink->hw_frames_ctx->data;
+    AVCUDADeviceContext* device_hwctx = frames_ctx->device_ctx->hwctx;
+
+    AVFrame* out = NULL;
+    CUcontext dummy;
+    int ret = 0;
+
+    out = av_frame_alloc();
+    if (!out) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    ret = CHECK_CU(device_hwctx->internal->cuda_dl->cuCtxPushCurrent(
+        device_hwctx->cuda_ctx));
+    if (ret < 0)
+        goto fail;
+
+    ret = nppsharpen_sharpen(ctx, s->own_frame, in);
+    if (ret < 0)
+        goto pop_ctx;
+
+    ret = av_hwframe_get_buffer(s->own_frame->hw_frames_ctx, s->tmp_frame, 0);
+    if (ret < 0)
+        goto pop_ctx;
+
+    av_frame_move_ref(out, s->own_frame);
+    av_frame_move_ref(s->own_frame, s->tmp_frame);
+
+    ret = av_frame_copy_props(out, in);
+    if (ret < 0)
+        goto pop_ctx;
+
+    av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
+              (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
+              (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
+              INT_MAX);
+
+    av_frame_free(&in);
+
+pop_ctx:
+    CHECK_CU(device_hwctx->internal->cuda_dl->cuCtxPopCurrent(&dummy));
+    return ff_filter_frame(outlink, out);
+fail:
+    av_frame_free(&in);
+    av_frame_free(&out);
+    return ret;
+}
+
+#define OFFSET(x) offsetof(NPPSharpenContext, x)
+#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
+static const AVOption options[] = {
+    { "border_type", "Type of operation to be performed on image border", OFFSET(border_type), AV_OPT_TYPE_INT, { .i64 = NPP_BORDER_REPLICATE }, NPP_BORDER_REPLICATE, NPP_BORDER_REPLICATE, FLAGS, "border_type" },
+    { "replicate", "replicate pixels", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_BORDER_REPLICATE }, 0, 0, FLAGS, "border_type" },
+    {NULL},
+};
+
+static const AVClass nppsharpen_class = {
+    .class_name = "nppsharpen",
+    .item_name = av_default_item_name,
+    .option = options,
+    .version = LIBAVUTIL_VERSION_INT,
+};
+
+static const AVFilterPad nppsharpen_inputs[] = {{
+    .name = "default",
+    .type = AVMEDIA_TYPE_VIDEO,
+    .filter_frame = nppsharpen_filter_frame,
+}};
+
+static const AVFilterPad nppsharpen_outputs[] = {{
+    .name = "default",
+    .type = AVMEDIA_TYPE_VIDEO,
+    .config_props = nppsharpen_config_props,
+}};
+
+const AVFilter ff_vf_sharpen_npp = {
+    .name = "sharpen_npp",
+    .description = NULL_IF_CONFIG_SMALL("NVIDIA Performance Primitives video "
+                                        "sharpening filter."),
+
+    .init = nppsharpen_init,
+    .uninit = nppsharpen_uninit,
+    .query_formats = nppsharpen_query_formats,
+
+    .priv_size = sizeof(NPPSharpenContext),
+    .priv_class = &nppsharpen_class,
+
+    FILTER_INPUTS(nppsharpen_inputs),
+    FILTER_OUTPUTS(nppsharpen_outputs),
+
+    .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
+};
-- 
2.25.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to