From patchwork Wed Jan 17 02:49:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Zhao X-Patchwork-Id: 7302 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.78.2 with SMTP id r2csp4993105jaa; Tue, 16 Jan 2018 18:49:46 -0800 (PST) X-Google-Smtp-Source: ACJfBouNG+j07/J34mlZ9ryrxXlVmkR/nkBaWBwL277zaDRMQlFQx30a1gx8mt80fsXL9znYAUOM X-Received: by 10.223.187.13 with SMTP id r13mr984494wrg.62.1516157386468; Tue, 16 Jan 2018 18:49:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516157386; cv=none; d=google.com; s=arc-20160816; b=aPLb6zFv32DOCLng9k3ZIDFoIIPXlcv+UZjpYjtDOjKS0zSuhxZhrBiE/stCFttFOM XfrWqGbpvUvhuuzwOmnTMs7V3kBK9HKhHdwrdg62HkaIgE7BPPSPqGKz8WHJnLQB/4Ti 4/qOS/gBKjp9/iqPjBLdvMqT083JCvDjk0kmTEnBVNj6X3O+L/p8ihnle22iw/WhDr7e Px/jySHm8xHOmzc9E+/gnxbw8qN8Ruz2nn3DBLjsbNRzTFNIQeOALwILilKBhhIP6iPi CS/wYZQlUSIdK2AgbnDZpiKPO40DhGpwexKhCrawTJn0j8gntRLsqSc24Hk3ipn+z+rO g2Sw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :content-language:mime-version:user-agent:date:message-id:from:to :dkim-signature:delivered-to:arc-authentication-results; bh=6Mgj7EsTi/jJtK4ZqTvSIPGhmtgkrQRFPy5aQUb5HHg=; b=SK44GTAFFjfo+bISMKoBNjPi6vwTDH3tIx9vR+Y+cz2VoJptAHKeyH8/tkcSHgLOcY a7SnOptnjjVltBKMAIpmBfECKnMEjPitCpfQEgq4/krSfNs4JYDdvx+GmhhmlNTfuHIH a6+HWGcTDkllJI6WeqXz0vXNFF790A5wn1Ierwdx+nqsUnGGSi0lwSqTqhz1/nXNgKDS Q8MlrbNTPHu2aHvhF2vWWYYaJ3msQ8xv13G76ViCSjYOSFQcM6CluP8syXs62Q9UdfNm Kj6FIwRTthZYchaIrdAiUQEDpDocWA2Shy0u0nySygkkZzAchFI7uLZ5QQAmauzNAdLc KAxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=aoftcrj4; 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=NONE dis=NONE) header.from=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 k13si2610779wrd.218.2018.01.16.18.49.46; Tue, 16 Jan 2018 18:49:46 -0800 (PST) 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=20161025 header.b=aoftcrj4; 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=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2FEA8689DCE; Wed, 17 Jan 2018 04:49:43 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl0-f44.google.com (mail-pl0-f44.google.com [209.85.160.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A421F6899D2 for ; Wed, 17 Jan 2018 04:49:36 +0200 (EET) Received: by mail-pl0-f44.google.com with SMTP id f8so5673154plk.11 for ; Tue, 16 Jan 2018 18:49:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=lHgfbCy/CBNKJ8vm9MnLr7KdUlH1K6GON3zw6pTCKmk=; b=aoftcrj4qEvqkXwSBrLeo7+FrhA6MBwEa+uSGuhhzuKeBwBfkif/Jv5Q5AUMPvOOsP UxaW5h852+ru3v8OdLgQVgFKtl7H7RCPpCkabei/lXbxbET1C8qn231UIdNgj/83yRuz WNIZSfaHFJjXM3z9deK5NSov+r8Y0HfN9LXadXE4uBQD1C2hQ4mIOdHEOt6K+3HoUY8o j4tmlaeMs3OQtbUAY0ztvhC0QcbOglRmmTCXuT55L+S1jVMCYoH1CSgPgJeBv53Xoq4a ipQmScf8pDbz5myqMTwhnFPAB/8wV8K15aQGmNaYdi77s+WOHxcBxKVfCMn660vOUCaE b73Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=lHgfbCy/CBNKJ8vm9MnLr7KdUlH1K6GON3zw6pTCKmk=; b=MPpUTVIR1lTacaLjWqwKcrRN2xPD1HFpZUq9lM2gGxzVjWwOHlh7/lvXg9+KaRHMVD Isx9yePI7W8QuMzyYv3t1Rq4ZPMkJzf4SEke0ACZSHygh5iKlryvSo6Lydl4ILAr6FDa 5ZrCpns0JAMm7nmNrgJr4OxVqIC6X+Yi/pxdk/bLpI8TX7eLweV5EWhifsKtEi+HvRXb fGqsT/nAtBmdM9cj0Uuf4qIis+If3IFljPX2FQ76pjc5vgmqD2wpQx+IPU7NvmF/LEM5 WXuRDDGgYxWwUh6exGGjnVh2XiAGYXKHWRnUDZ7njAJ+vwHjteV5TLJxnsCjUdo8CWU5 v8yg== X-Gm-Message-State: AKwxyteWOH5AW9a4BNE9rGgdGVMqG/BBn8qt23UzhcYaPeMALU80/Sd2 6+Byimfe+w8Ki5m5h30Q12c= X-Received: by 10.84.230.230 with SMTP id e93mr7875116plk.173.1516157377693; Tue, 16 Jan 2018 18:49:37 -0800 (PST) Received: from [10.67.68.25] ([192.55.54.41]) by smtp.gmail.com with ESMTPSA id s14sm4704926pgf.16.2018.01.16.18.49.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Jan 2018 18:49:36 -0800 (PST) To: FFmpeg development discussions and patches , Mark Thompson From: Jun Zhao Message-ID: <4091cbf8-d662-9718-56a1-a2a06012a261@gmail.com> Date: Wed, 17 Jan 2018 10:49:34 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 Content-Language: en-US Subject: [FFmpeg-devel] [PATCH V2 5/5] lavfi: add misc(denoise/sharpness) VPP video filter. 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From 0a7f0aa4a108faad5ed84c285d5431ca6c9d9fdd Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Mon, 8 Jan 2018 16:19:17 +0800 Subject: [PATCH V2 5/5] lavfi: add misc(denoise/sharpness) VPP video filter. add misc(denoise/sharpness) VPP video filter. Signed-off-by: Yun Zhou Signed-off-by: Jun Zhao --- configure | 1 + libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_misc_vaapi.c | 278 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 281 insertions(+) create mode 100644 libavfilter/vf_misc_vaapi.c diff --git a/configure b/configure index 12fb34a202..218d9ccecf 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..db9049614b 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -246,6 +246,7 @@ OBJS-$(CONFIG_MESTIMATE_FILTER) += vf_mestimate.o motion_estimation 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_MISC_VAAPI_FILTER) += vf_misc_vaapi.o vaapi_vpp.o OBJS-$(CONFIG_MIX_FILTER) += vf_mix.o OBJS-$(CONFIG_MPDECIMATE_FILTER) += vf_mpdecimate.o OBJS-$(CONFIG_NEGATE_FILTER) += vf_lut.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 63550628e5..6183be9a73 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -256,6 +256,7 @@ static void register_all(void) REGISTER_FILTER(METADATA, metadata, vf); REGISTER_FILTER(MIDEQUALIZER, midequalizer, vf); REGISTER_FILTER(MINTERPOLATE, minterpolate, vf); + REGISTER_FILTER(MISC_VAAPI, misc_vaapi, vf); REGISTER_FILTER(MIX, mix, vf); REGISTER_FILTER(MPDECIMATE, mpdecimate, vf); REGISTER_FILTER(NEGATE, negate, vf); diff --git a/libavfilter/vf_misc_vaapi.c b/libavfilter/vf_misc_vaapi.c new file mode 100644 index 0000000000..69f4c82002 --- /dev/null +++ b/libavfilter/vf_misc_vaapi.c @@ -0,0 +1,278 @@ +/* + * 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 + +#include +#include + +#include "libavutil/avassert.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 { + 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 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) +{ + VAAPIVPPContext *vpp_ctx = avctx->priv; + MiscVAAPIContext *ctx = vpp_ctx->priv; + + 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; + VAAPIVPPContext *vpp_ctx = avctx->priv; + MiscVAAPIContext *ctx = vpp_ctx->priv; + int err; + + // 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"); + return AVERROR(EINVAL); + } + + err = vaapi_vpp_config_output(outlink); + if (err < 0) + return err; + + return 0; +} + +static int misc_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame) +{ + AVFilterContext *avctx = inlink->dst; + AVFilterLink *outlink = avctx->outputs[0]; + VAAPIVPPContext *vpp_ctx = avctx->priv; + MiscVAAPIContext *ctx = vpp_ctx->priv; + 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 = ff_get_video_buffer(outlink, vpp_ctx->output_width, + vpp_ctx->output_height); + if (!output_frame) { + err = AVERROR(ENOMEM); + goto fail; + } + + 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(&input_frame); + av_frame_free(&output_frame); + return err; +} + +static av_cold int misc_vaapi_init(AVFilterContext *avctx) +{ + VAAPIVPPContext *vpp_ctx = avctx->priv; + + vaapi_vpp_ctx_init(vpp_ctx); + vpp_ctx->pipeline_uninit = vaapi_vpp_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) +{ + VAAPIVPPContext *vpp_ctx = avctx->priv; + MiscVAAPIContext *ctx = vpp_ctx->priv; + for (int i = 0; i < ctx->num_filter_bufs; i++) + vaDestroyBuffer(vpp_ctx->hwctx->display, ctx->filter_bufs[i]); + + vaapi_vpp_ctx_uninit(avctx); +} + +#define OFFSET(x) (offsetof(VAAPIVPPContext, priv_data) + \ + 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 }, +}; + +AVFILTER_DEFINE_CLASS(misc_vaapi); + +static const AVFilterPad misc_vaapi_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = &misc_vaapi_filter_frame, + .config_props = &vaapi_vpp_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(VAAPIVPPContext) + + sizeof(MiscVAAPIContext)), + .init = &misc_vaapi_init, + .uninit = &misc_vaapi_uninit, + .query_formats = &vaapi_vpp_query_formats, + .inputs = misc_vaapi_inputs, + .outputs = misc_vaapi_outputs, + .priv_class = &misc_vaapi_class, + .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE, +};