From patchwork Mon Jan 8 08:35:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Zhao X-Patchwork-Id: 7200 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.78.2 with SMTP id r2csp2386198jaa; Mon, 8 Jan 2018 00:36:03 -0800 (PST) X-Google-Smtp-Source: ACJfBov/5sXusGGqdmbTj+5qlxLUgJQosQiCln54okH2yisQOw6AlBpxboDljDIDCS6yAz36271p X-Received: by 10.223.208.206 with SMTP id z14mr9873082wrh.109.1515400563885; Mon, 08 Jan 2018 00:36:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515400563; cv=none; d=google.com; s=arc-20160816; b=KTXgibDzqPRk8Pl4l5mnrfJhTB6VKAgvSpaYKsS1NVdbfteeCqezw8vNVZHfZhfEqH MHi3UYW0ISf5OEbNSSpwSSwUWnVxyhF9bFLHPGdlJXMlGg7kC8K3WSwli2rGlbxQrDqO BYBzxX4G4E9ibWzOlvb/wXK9jyFGMpDzdMUYrbZ3qBjAjNGdqARlNWzWqidqF8MuUybm sR4RR2hbdslQOSF5W1NPgQZm7bg2xYL2U3ksQDHssq/SMaMcr+fendBxTs6i6b9APzdV n1ATg3lJVJ7ClSQrXhk6C4rn31x8okax0oR4rI05fxcl+O73kiQ1FYHtpXrdlQ3hsUZs 3azw== 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=DEij0KV7gc237JKAU1DAvsZEwbIDJHNVkgKiLEWQS7s=; b=hxXeUzIASBElAkKqWvOZmeIecM8+2/CSYr6Ii29RSMDlbw9WkcsD1hhSZGvMH3oUML JhXhfu6s9rEiGLE5ahA2J5ftBTf5PpDER0+Pdso30qPz1As4U+U+Tbh3txpwpADsqljM RxX/uHvmhI/wHhWhJRCfq7A7Aoc1rjxDjwKjJ4hDiSGdWXbt8inPJCrKazqjMuES92LY aNXgGhtNU668sJB/bUjKjlPCYueayuDARl4dbKqUJUrOuudk12j4jqGr/efRSPae0a5l MfWpIBLvJQX/G6UOvL/WHHmdP1H4TuvaNXpysVGFpZJhxwdTRZWmduiUUGmYrZovbkaa cOHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=m+C7nUEX; 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 h88si8891094wrh.277.2018.01.08.00.36.03; Mon, 08 Jan 2018 00:36:03 -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=m+C7nUEX; 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 38377689FD6; Mon, 8 Jan 2018 10:35:57 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf0-f178.google.com (mail-pf0-f178.google.com [209.85.192.178]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 26474689BD0 for ; Mon, 8 Jan 2018 10:35:55 +0200 (EET) Received: by mail-pf0-f178.google.com with SMTP id y5so1581015pff.13 for ; Mon, 08 Jan 2018 00:35:56 -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=RHYfQLUPpCSo7sMFQ76ks9+3OtTuO7ZSMWgvXXkVi60=; b=m+C7nUEXOl3ecQgiq2qOrdD2Jr/4ASfB8X9dm3KjGGQUvZHVY0xNGsp7Qxnk7wJODF YJesbhobx/xtImTygdCCi38aNw4GXwPrT63XtZ+Oi92jCfP6/0xXgwayuNaZ+v10tPzY G2e7Bq08ilS5Q/kkyEz5SYXDtt7dbY7ScfIHYztwcgyf42fhS00mb9RiWjSHkId0YfRT apE+uq5Ok6JnDuL1ERiZeU3J8/XxGgC6+v4oh5VACmmya4qQu1pYWTW5pRSEL0cpFyWe FKBtzKfzJLx0rK3NrZrKBra7Lckxju4Dw0z2Qnc/D4Khj2CqG3dw5hqC2Cpl/H/FI8yh BF7w== 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=RHYfQLUPpCSo7sMFQ76ks9+3OtTuO7ZSMWgvXXkVi60=; b=cJCGtL4h58HRgQO9uwq0cSEVsrrZmQEcKja8oXZ/+60bi+YNThnKygr16sk6G5ymRx kmvTY6EJl2vUf1TP/4eYZPhKHiW42uMxaOFe0Ur2VadP0n3rkAtJalgAURQTuqt2ieVm Ofh0zmGM6Rzq7BnoADY7YZ6nYXd8DfmS5jBYAuEC8UVeycTFZd6fVaYI82cnpT/UfxLm tTVrzTH5syWmT783LK4So7HO9fpsm5qPPasYPUkDZlDn0fhmJhirYLjj+LDKj4WKs9EF ywPPVKr/3Yij2mY3jnWZE6PT29+B1dBU6eZLZFtA/Mu6x91eGhIsajJLL5KNvdo9ed5e Spdg== X-Gm-Message-State: AKGB3mIS3w6BK5GN4XBlNNRfsmBWqtRTqbFDUoJp7rLfYyuVQwwejrPU 8iWvCKK085qcMFfeHEX8MwltybQQ X-Received: by 10.84.216.26 with SMTP id m26mr3164639pli.158.1515400555585; Mon, 08 Jan 2018 00:35:55 -0800 (PST) Received: from [10.67.68.34] ([192.55.54.42]) by smtp.gmail.com with ESMTPSA id h69sm26482726pfk.166.2018.01.08.00.35.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Jan 2018 00:35:55 -0800 (PST) To: FFmpeg development discussions and patches , Mark Thompson From: Jun Zhao Message-ID: <70483faa-5c07-b7f6-82f1-4e1e61aad500@gmail.com> Date: Mon, 8 Jan 2018 16:35:52 +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 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 5be7c9c55e1ad092223a13f465da3b1a2f1d01b3 Mon Sep 17 00:00:00 2001 From: Jun Zhao 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 Signed-off-by: Jun Zhao --- 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 + +#include +#include + +#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, +}; +