[FFmpeg-devel] [PATCH 5/5] lavfi: add misc(denoise/sharpness) VPP video filter.
Jun Zhao
mypopydev at gmail.com
Mon Jan 8 10:35:52 EET 2018
-------------- next part --------------
From 5be7c9c55e1ad092223a13f465da3b1a2f1d01b3 Mon Sep 17 00:00:00 2001
From: Jun Zhao <jun.zhao at intel.com>
Date: Mon, 8 Jan 2018 16:19:17 +0800
Subject: [PATCH 5/5] lavfi: add misc(denoise/sharpness) VPP video filter.
add misc(denoise/sharpness) VPP video filter.
Signed-off-by: Yun Zhou <yunx.z.zhou at intel.com>
Signed-off-by: Jun Zhao <jun.zhao at intel.com>
---
configure | 1 +
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/vf_misc_vaapi.c | 328 ++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 331 insertions(+)
create mode 100644 libavfilter/vf_misc_vaapi.c
diff --git a/configure b/configure
index 366a67d327..3b8743db4d 100755
--- a/configure
+++ b/configure
@@ -3227,6 +3227,7 @@ kerndeint_filter_deps="gpl"
ladspa_filter_deps="ladspa libdl"
lv2_filter_deps="lv2"
mcdeint_filter_deps="avcodec gpl"
+misc_vaapi_filter_deps="vaapi VAProcPipelineParameterBuffer"
movie_filter_deps="avcodec avformat"
mpdecimate_filter_deps="gpl"
mpdecimate_filter_select="pixelutils"
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 43d0dd36e6..970597b706 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -247,6 +247,7 @@ OBJS-$(CONFIG_METADATA_FILTER) += f_metadata.o
OBJS-$(CONFIG_MIDEQUALIZER_FILTER) += vf_midequalizer.o framesync.o
OBJS-$(CONFIG_MINTERPOLATE_FILTER) += vf_minterpolate.o motion_estimation.o
OBJS-$(CONFIG_MIX_FILTER) += vf_mix.o
+OBJS-$(CONFIG_MISC_VAAPI_FILTER) += vf_misc_vaapi.o vaapi_vpp.o
OBJS-$(CONFIG_MPDECIMATE_FILTER) += vf_mpdecimate.o
OBJS-$(CONFIG_NEGATE_FILTER) += vf_lut.o
OBJS-$(CONFIG_NLMEANS_FILTER) += vf_nlmeans.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 63550628e5..e5b713ea08 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -257,6 +257,7 @@ static void register_all(void)
REGISTER_FILTER(MIDEQUALIZER, midequalizer, vf);
REGISTER_FILTER(MINTERPOLATE, minterpolate, vf);
REGISTER_FILTER(MIX, mix, vf);
+ REGISTER_FILTER(MISC_VAAPI, misc_vaapi, vf);
REGISTER_FILTER(MPDECIMATE, mpdecimate, vf);
REGISTER_FILTER(NEGATE, negate, vf);
REGISTER_FILTER(NLMEANS, nlmeans, vf);
diff --git a/libavfilter/vf_misc_vaapi.c b/libavfilter/vf_misc_vaapi.c
new file mode 100644
index 0000000000..f6ef764b09
--- /dev/null
+++ b/libavfilter/vf_misc_vaapi.c
@@ -0,0 +1,328 @@
+/*
+ * 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
+ */
+#include <string.h>
+
+#include <va/va.h>
+#include <va/va_vpp.h>
+
+#include "libavutil/avassert.h"
+#include "libavutil/hwcontext.h"
+#include "libavutil/hwcontext_vaapi.h"
+#include "libavutil/mem.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "vaapi_vpp.h"
+
+typedef struct MiscVAAPIContext {
+ const AVClass *class;
+
+ VAAPIVPPContext *vpp_ctx;
+
+ VAProcFilterCap denoise_caps;
+ int denoise; // enable denoise algo. level is the optional
+ // value from the interval [-1, 100], -1 means disabled
+
+ VAProcFilterCap sharpness_caps;
+ int sharpness; // enable sharpness. level is the optional value
+ // from the interval [-1, 100], -1 means disabled
+ int num_filter_bufs;
+ VABufferID filter_bufs[VAProcFilterCount];
+} MiscVAAPIContext;
+
+static int misc_vaapi_query_formats(AVFilterContext *avctx)
+{
+ return vaapi_vpp_query_formats(avctx);
+}
+
+static int misc_vaapi_pipeline_uninit(AVFilterContext *avctx)
+{
+ MiscVAAPIContext *ctx = avctx->priv;
+ VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
+
+ return vaapi_vpp_pipeline_uninit(vpp_ctx);
+}
+
+static int misc_vaapi_config_input(AVFilterLink *inlink)
+{
+ AVFilterContext *avctx = inlink->dst;
+ MiscVAAPIContext *ctx = avctx->priv;
+ VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
+
+ return vaapi_vpp_config_input(inlink, vpp_ctx);
+}
+
+static float map_to_range(
+ int input, int in_min, int in_max,
+ float out_min, float out_max)
+{
+ return (input - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
+}
+
+static int misc_vaapi_build_filter_params(AVFilterContext *avctx)
+{
+ MiscVAAPIContext *ctx = avctx->priv;
+ VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
+ VAStatus vas;
+ VAProcFilterParameterBufferColorBalance misc_params[4];
+ VAProcFilterCapColorBalance misc_caps[VAProcColorBalanceCount];
+ uint32_t num_denoise_caps = 1;
+ uint32_t num_sharpness_caps = 1;
+
+ VAProcFilterParameterBuffer denoise;
+ VAProcFilterParameterBuffer sharpness;
+
+ memset(&misc_params, 0, sizeof(misc_params));
+ memset(&misc_caps, 0, sizeof(misc_caps));
+
+ if (ctx->denoise != -1) {
+ vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display, vpp_ctx->va_context,
+ VAProcFilterNoiseReduction,
+ &ctx->denoise_caps, &num_denoise_caps);
+ if (vas != VA_STATUS_SUCCESS) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to query denoise caps "
+ "context: %d (%s).\n", vas, vaErrorStr(vas));
+ return AVERROR(EIO);
+ }
+
+ denoise.type = VAProcFilterNoiseReduction;
+ denoise.value = map_to_range(ctx->denoise, 0, 100,
+ ctx->denoise_caps.range.min_value,
+ ctx->denoise_caps.range.max_value);
+ vaapi_vpp_make_param_buffers(vpp_ctx, VAProcFilterParameterBufferType,
+ &denoise, sizeof(denoise), 1);
+ }
+
+ if (ctx->sharpness != -1) {
+ vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display, vpp_ctx->va_context,
+ VAProcFilterSharpening,
+ &ctx->sharpness_caps, &num_sharpness_caps);
+ if (vas != VA_STATUS_SUCCESS) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to query sharpness caps "
+ "context: %d (%s).\n", vas, vaErrorStr(vas));
+ return AVERROR(EIO);
+ }
+
+ sharpness.type = VAProcFilterSharpening;
+ sharpness.value = map_to_range(ctx->sharpness,
+ 0, 100,
+ ctx->sharpness_caps.range.min_value,
+ ctx->sharpness_caps.range.max_value);
+ vaapi_vpp_make_param_buffers(vpp_ctx,
+ VAProcFilterParameterBufferType,
+ &sharpness, sizeof(sharpness), 1);
+ }
+
+ return 0;
+}
+
+static int misc_vaapi_config_output(AVFilterLink *outlink)
+{
+ AVFilterContext *avctx = outlink->src;
+ MiscVAAPIContext *ctx = avctx->priv;
+ VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
+ int err;
+
+ ctx->vpp_ctx->output_width = avctx->inputs[0]->w;
+ ctx->vpp_ctx->output_height = avctx->inputs[0]->h;
+
+ // multiple filters aren't supported in the driver:
+ // sharpness can't work with noise reduction(de-noise), deinterlacing
+ // color balance, skin tone enhancement...
+ if (ctx->denoise != -1 && ctx->sharpness != -1) {
+ av_log(ctx, AV_LOG_ERROR, "Do not support multiply filters (sharpness "
+ "can't work with the other filters).\n");
+ err = AVERROR(EINVAL);
+ goto fail;
+ }
+
+ err = vaapi_vpp_config_output(outlink, vpp_ctx);
+ if (err < 0)
+ goto fail;
+
+ return 0;
+
+fail:
+ return err;
+}
+
+static int misc_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
+{
+ AVFilterContext *avctx = inlink->dst;
+ AVFilterLink *outlink = avctx->outputs[0];
+ MiscVAAPIContext *ctx = avctx->priv;
+ VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
+ AVFrame *output_frame = NULL;
+ VASurfaceID input_surface, output_surface;
+ VARectangle input_region;
+
+ VAProcPipelineParameterBuffer params;
+ int err;
+
+ av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
+ av_get_pix_fmt_name(input_frame->format),
+ input_frame->width, input_frame->height, input_frame->pts);
+
+ if (vpp_ctx->va_context == VA_INVALID_ID)
+ return AVERROR(EINVAL);
+
+ input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
+ av_log(ctx, AV_LOG_DEBUG, "Using surface %#x for misc vpp input.\n",
+ input_surface);
+
+ output_frame = av_frame_alloc();
+ if (!output_frame) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to allocate output frame.");
+ err = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ err = av_hwframe_get_buffer(vpp_ctx->output_frames_ref, output_frame, 0);
+ if (err < 0) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to get surface for "
+ "output: %d\n.", err);
+ }
+
+ output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
+ av_log(ctx, AV_LOG_DEBUG, "Using surface %#x for misc vpp output.\n",
+ output_surface);
+ memset(¶ms, 0, sizeof(params));
+ input_region = (VARectangle) {
+ .x = 0,
+ .y = 0,
+ .width = input_frame->width,
+ .height = input_frame->height,
+ };
+
+ if (vpp_ctx->nb_filter_buffers) {
+ params.filters = &vpp_ctx->filter_buffers[0];
+ params.num_filters = vpp_ctx->nb_filter_buffers;
+ }
+ params.surface = input_surface;
+ params.surface_region = &input_region;
+ params.surface_color_standard =
+ vaapi_vpp_colour_standard(input_frame->colorspace);
+
+ params.output_region = NULL;
+ params.output_background_color = 0xff000000;
+ params.output_color_standard = params.surface_color_standard;
+
+ params.pipeline_flags = 0;
+ params.filter_flags = VA_FRAME_PICTURE;
+
+ err = vaapi_vpp_render_picture(vpp_ctx, ¶ms, output_surface);
+ if (err < 0)
+ goto fail;
+
+ err = av_frame_copy_props(output_frame, input_frame);
+ if (err < 0)
+ goto fail;
+ av_frame_free(&input_frame);
+
+ av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
+ av_get_pix_fmt_name(output_frame->format),
+ output_frame->width, output_frame->height, output_frame->pts);
+
+ return ff_filter_frame(outlink, output_frame);
+
+fail:
+ av_frame_free(&output_frame);
+ return err;
+}
+
+static av_cold int misc_vaapi_init(AVFilterContext *avctx)
+{
+ MiscVAAPIContext *ctx = avctx->priv;
+ VAAPIVPPContext *vpp_ctx;
+
+ ctx->vpp_ctx = av_mallocz(sizeof(VAAPIVPPContext));
+ if (!ctx->vpp_ctx)
+ return AVERROR(ENOMEM);
+
+ vpp_ctx = ctx->vpp_ctx;
+
+ vaapi_vpp_ctx_init(vpp_ctx);
+ vpp_ctx->pipeline_uninit = misc_vaapi_pipeline_uninit;
+ vpp_ctx->build_filter_params = misc_vaapi_build_filter_params;
+ vpp_ctx->output_format = AV_PIX_FMT_NONE;
+
+ return 0;
+}
+
+static av_cold void misc_vaapi_uninit(AVFilterContext *avctx)
+{
+ MiscVAAPIContext *ctx = avctx->priv;
+ VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
+ for (int i = 0; i < ctx->num_filter_bufs; i++)
+ vaDestroyBuffer(vpp_ctx->hwctx->display, ctx->filter_bufs[i]);
+
+ vaapi_vpp_ctx_uninit(avctx, vpp_ctx);
+}
+
+#define OFFSET(x) offsetof(MiscVAAPIContext, x)
+#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM)
+static const AVOption misc_vaapi_options[] = {
+ { "denoise", "denoise level [-1, 100], -1 means disabled",
+ OFFSET(denoise), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 100, .flags = FLAGS },
+ { "sharpness", "sharpness level [-1, 100], -1 means disabled",
+ OFFSET(sharpness), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 100, .flags = FLAGS },
+ { NULL },
+};
+
+static const AVClass misc_vaapi_class = {
+ .class_name = "misc_vaapi",
+ .item_name = av_default_item_name,
+ .option = misc_vaapi_options,
+ .version = LIBAVUTIL_VERSION_INT,
+};
+
+static const AVFilterPad misc_vaapi_inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .filter_frame = &misc_vaapi_filter_frame,
+ .config_props = &misc_vaapi_config_input,
+ },
+ { NULL }
+};
+
+static const AVFilterPad misc_vaapi_outputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .config_props = &misc_vaapi_config_output,
+ },
+ { NULL }
+};
+
+AVFilter ff_vf_misc_vaapi = {
+ .name = "misc_vaapi",
+ .description = NULL_IF_CONFIG_SMALL("Misc VAAPI VPP for de-noise, sharpness"),
+ .priv_size = sizeof(MiscVAAPIContext),
+ .init = &misc_vaapi_init,
+ .uninit = &misc_vaapi_uninit,
+ .query_formats = &misc_vaapi_query_formats,
+ .inputs = misc_vaapi_inputs,
+ .outputs = misc_vaapi_outputs,
+ .priv_class = &misc_vaapi_class,
+ .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
+};
+
--
2.14.1
More information about the ffmpeg-devel
mailing list