From patchwork Tue Sep 13 22:06:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 565 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.140.134 with SMTP id o128csp2360780vsd; Tue, 13 Sep 2016 15:13:01 -0700 (PDT) X-Received: by 10.28.210.5 with SMTP id j5mr2681092wmg.67.1473804781843; Tue, 13 Sep 2016 15:13:01 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id hf6si856463wjb.113.2016.09.13.15.12.59; Tue, 13 Sep 2016 15:13:01 -0700 (PDT) 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=@jkqxz-net.20150623.gappssmtp.com; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CFE56689FF5; Wed, 14 Sep 2016 01:12:44 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f48.google.com (mail-wm0-f48.google.com [74.125.82.48]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8764E689C44 for ; Wed, 14 Sep 2016 01:12:37 +0300 (EEST) Received: by mail-wm0-f48.google.com with SMTP id b187so17952731wme.1 for ; Tue, 13 Sep 2016 15:12:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding; bh=CD/VFXMC/+nAvB8clK+J2czwyA1o8aLaHDOB3fzBdKc=; b=V9NEQ3Kr80WfQUYzndgx8t51OCNyz4aEoKzT9janCVjJbPgt9yaX/EDteYw6Nfcfzx H4wdbBterlLqosCGaJgI3E9z32otrmjreU4Q/AK4y/KqZUUeqay0eFhTabia2WEOr6U6 PbTcUQGO94uJMMW0pI/bcs/JOWSlEC9A36wcPR2ZTE9KEie//AhWJyP62kPYEEbPcbTA jiye8tqsMTKO6HrgP3A6lHqso3vP2bYm++zhfLSr1zDYpqe9g2NsMUi/1M2ajtTDtH1e Lb6EyfK+ZPIL5pr79J8+CK1qVANCIQ4xwizNBVy9PWEsI4Idvh3OcdcOnpPXE6jkJ3qE 8Npg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=CD/VFXMC/+nAvB8clK+J2czwyA1o8aLaHDOB3fzBdKc=; b=OEhjLXcgrSfShU6vuTk7uep6rLvRBl1ZzbjdJjuPdouYQrNxlyJ87JA+xyFrEb/i0i fTz8/9/TYg2d1Bf65mbdJZvPVeFDPU4/VXBVoAguI8HqtcZKet+sKyAeALrZMD/Xgsps EusxrWsjqK0nwBxEHp/p3MvOX5tAzxsVtY9xf/1yCESv/p/4F25jTyPTjCIoHmeoRP6I YoEZGCNZawyjgVcDzZQyIo0PYPnQQ9ceI7rVy+gK+6EGY0+UVe4qmZdKZvpr/qLz5j7n +5zb5CWlOM173y9OS1y6+yYTG1FuhFqLEP+tl9I+GzMspTLOOS/3GYZLYjzzyGOhLMSO 0wOg== X-Gm-Message-State: AE9vXwMy1P6pGrxawz2ToH4+cNHxhKVzqDYIDnRFKXsDfD/OaudzhldHLZxgFJhspVwDAw== X-Received: by 10.28.211.10 with SMTP id k10mr2687442wmg.16.1473804367904; Tue, 13 Sep 2016 15:06:07 -0700 (PDT) Received: from [192.168.0.7] (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id o2sm871007wjo.3.2016.09.13.15.06.06 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Sep 2016 15:06:06 -0700 (PDT) To: FFmpeg development discussions and patches References: <7f2e007d-d1d5-f8de-252e-40b2a32822ad@gmail.com> From: Mark Thompson Message-ID: Date: Tue, 13 Sep 2016 23:06:05 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.2.0 MIME-Version: 1.0 In-Reply-To: Subject: [FFmpeg-devel] [PATCH] lavfi: VAAPI video processing 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" WIP. --- On 05/09/16 02:52, Jun Zhao wrote: > On 2016/8/31 6:48, Mark Thompson wrote: >> On 30/08/16 09:00, Jun Zhao wrote: >>> v3 : fix sharpless mapping issue >>> v2 : fix filter support flag check logic issue >> >> Hi, >> >> A general remark to start: vf_scale_vaapi is named to be a scaling filter (i.e. it replaces vf_scale/swscale for AV_PIX_FMT_VAAPI) - is this therefore really the right place to be adding other operations unrelated to scaling? >> >> Do use-cases for these operations actually make sense to add here rather than in a separate filter? (I'm not sure what the answer to this should be - I would definitely argue that the deinterlacer should be a separate filter, but these other operations are unclear.) >> >> > > As you know, VPP use the pipeline mode, split the scale/denoise/sharpness/... in > different filter maybe is not good idea, I guess nobody want to call vaRenderPicture()/ > vaEndpicture/... again and again in vf_scale_vaapi.c/vf_denosie_vaapi.c/vf_sharpness_vaapi.c/... How about something like this, then? It adds a new filter to do the video processing, while leaving the scale filter as-is. Implements denoise, sharpen and all of the colour balance controls; lightly tested but seems working on i965/Skylake. Outstanding issues: * The name is not very good, but I can't think of anything better. * Needs more testing. * Some error recovery is missing. * Documentation. * Reuses the surface pool from the input hw_frames_ctx - is anything going to object to that? * Can't order the filters applied - does that matter? * Sharpness + anything else aborts inside the i965 driver, other combinations work - should vaQueryVideoProcPipelineCaps() detect that, or is there some other way to get it? Thanks, - Mark libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_process_vaapi.c | 597 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 599 insertions(+) create mode 100644 libavfilter/vf_process_vaapi.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 5cd10fa..10ffa78 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -239,6 +239,7 @@ OBJS-$(CONFIG_PIXDESCTEST_FILTER) += vf_pixdesctest.o OBJS-$(CONFIG_PP_FILTER) += vf_pp.o OBJS-$(CONFIG_PP7_FILTER) += vf_pp7.o OBJS-$(CONFIG_PREWITT_FILTER) += vf_convolution.o +OBJS-$(CONFIG_PROCESS_VAAPI_FILTER) += vf_process_vaapi.o OBJS-$(CONFIG_PSNR_FILTER) += vf_psnr.o dualinput.o framesync.o OBJS-$(CONFIG_PULLUP_FILTER) += vf_pullup.o OBJS-$(CONFIG_QP_FILTER) += vf_qp.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 47d95f5..0684aef 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -255,6 +255,7 @@ void avfilter_register_all(void) REGISTER_FILTER(PP, pp, vf); REGISTER_FILTER(PP7, pp7, vf); REGISTER_FILTER(PREWITT, prewitt, vf); + REGISTER_FILTER(PROCESS_VAAPI, process_vaapi, vf); REGISTER_FILTER(PSNR, psnr, vf); REGISTER_FILTER(PULLUP, pullup, vf); REGISTER_FILTER(QP, qp, vf); diff --git a/libavfilter/vf_process_vaapi.c b/libavfilter/vf_process_vaapi.c new file mode 100644 index 0000000..25701a0 --- /dev/null +++ b/libavfilter/vf_process_vaapi.c @@ -0,0 +1,597 @@ +/* + * 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" + +typedef struct ProcessVAAPIContext { + const AVClass *class; + + AVVAAPIDeviceContext *hwctx; + AVBufferRef *device_ref; + + int va_ids_set; + VAConfigID va_config; + VAContextID va_context; + + AVBufferRef *frames_ref; + AVHWFramesContext *frames; + + int denoise; + VAProcFilterCap denoise_caps; + + int sharpness; + VAProcFilterCap sharpness_caps; + + int colour[VAProcColorBalanceCount]; + unsigned int nb_colour_caps; + VAProcFilterCapColorBalance colour_caps[VAProcColorBalanceCount]; + + unsigned int nb_filter_buffers; + VABufferID filter_buffers[VAProcFilterCount + + VAProcColorBalanceCount]; + + VAProcPipelineCaps pipeline_caps; +} ProcessVAAPIContext; + + +static const struct ProcessVAAPIColourBalance { + const char *name; + VAProcColorBalanceType type; + int min; + int max; +} process_vaapi_colour_balance[] = { + { "hue", VAProcColorBalanceHue, 0, 360 }, + { "saturation", VAProcColorBalanceSaturation, 0, 100 }, + { "brightness", VAProcColorBalanceBrightness, 0, 100 }, + { "contrast", VAProcColorBalanceContrast, 0, 100 }, + + { "auto-saturation", VAProcColorBalanceAutoSaturation, 0, 1 }, + { "auto-brightness", VAProcColorBalanceAutoBrightness, 0, 1 }, + { "auto-contrast", VAProcColorBalanceAutoContrast, 0, 1 }, +}; + +static int process_vaapi_query_formats(AVFilterContext *avctx) +{ + enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_VAAPI, AV_PIX_FMT_NONE, + }; + + ff_formats_ref(ff_make_format_list(pix_fmts), + &avctx->inputs[0]->out_formats); + ff_formats_ref(ff_make_format_list(pix_fmts), + &avctx->outputs[0]->in_formats); + + return 0; +} + +static int process_vaapi_pipeline_uninit(AVFilterContext *avctx) +{ + ProcessVAAPIContext *ctx = avctx->priv; + + if (ctx->va_context != VA_INVALID_ID) { + vaDestroyContext(ctx->hwctx->display, ctx->va_context); + ctx->va_context = VA_INVALID_ID; + } + + if (ctx->va_config != VA_INVALID_ID) { + vaDestroyConfig(ctx->hwctx->display, ctx->va_config); + ctx->va_config = VA_INVALID_ID; + } + + av_buffer_unref(&ctx->device_ref); + ctx->hwctx = NULL; + + return 0; +} + +static int process_vaapi_config_input(AVFilterLink *inlink) +{ + AVFilterContext *avctx = inlink->dst; + ProcessVAAPIContext *ctx = avctx->priv; + + process_vaapi_pipeline_uninit(avctx); + + if (!inlink->hw_frames_ctx) { + av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is " + "required to associate the processing device.\n"); + return AVERROR(EINVAL); + } + + ctx->frames_ref = av_buffer_ref(inlink->hw_frames_ctx); + ctx->frames = (AVHWFramesContext*)ctx->frames_ref->data; + + return 0; +} + +static float process_vaapi_map_range(int input, int min, int max, + const VAProcFilterValueRange *range) +{ + return ((input - min) * (range->max_value - range->min_value) / + (max - min) + range->min_value); +} + +static int process_vaapi_build_parameters(AVFilterContext *avctx) +{ + ProcessVAAPIContext *ctx = avctx->priv; + VAStatus vas; + unsigned int count; + VAProcFilterParameterBuffer params; + VABufferID filter_buffer; + int i, j, use_colour_filter; + + if (ctx->denoise != -1) { + count = 1; + vas = vaQueryVideoProcFilterCaps(ctx->hwctx->display, + ctx->va_context, + VAProcFilterNoiseReduction, + &ctx->denoise_caps, &count); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to query denoise " + "caps: %d (%s).\n", vas, vaErrorStr(vas)); + return AVERROR(EIO); + } + + params.type = VAProcFilterNoiseReduction; + params.value = process_vaapi_map_range(ctx->denoise, 0, 100, + &ctx->denoise_caps.range); + av_log(avctx, AV_LOG_DEBUG, "Setting denoise " + "parameter to %f.\n", params.value); + + vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context, + VAProcFilterParameterBufferType, + sizeof(params), 1, ¶ms, + &filter_buffer); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to create denoise " + "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas)); + return AVERROR(EIO); + } + + ctx->filter_buffers[ctx->nb_filter_buffers++] = filter_buffer; + } + + if (ctx->sharpness != -1) { + count = 1; + vas = vaQueryVideoProcFilterCaps(ctx->hwctx->display, + ctx->va_context, + VAProcFilterSharpening, + &ctx->sharpness_caps, &count); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to query sharpness " + "caps: %d (%s).\n", vas, vaErrorStr(vas)); + return AVERROR(EIO); + } + + params.type = VAProcFilterSharpening; + params.value = process_vaapi_map_range(ctx->sharpness, 0, 100, + &ctx->sharpness_caps.range); + av_log(avctx, AV_LOG_DEBUG, "Setting sharpness " + "parameter to %f.\n", params.value); + + vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context, + VAProcFilterParameterBufferType, + sizeof(params), 1, ¶ms, + &filter_buffer); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to create sharpness " + "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas)); + return AVERROR(EIO); + } + + ctx->filter_buffers[ctx->nb_filter_buffers++] = filter_buffer; + } + + use_colour_filter = 0; + for (i = 0; i < FF_ARRAY_ELEMS(process_vaapi_colour_balance); i++) { + if (ctx->colour[process_vaapi_colour_balance[i].type] != -1) + use_colour_filter = 1; + } + if (use_colour_filter) { + count = FF_ARRAY_ELEMS(ctx->colour_caps); + vas = vaQueryVideoProcFilterCaps(ctx->hwctx->display, + ctx->va_context, + VAProcFilterColorBalance, + ctx->colour_caps, &count); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to query colour " + "balance caps: %d (%s).\n", vas, vaErrorStr(vas)); + return AVERROR(EIO); + } + + ctx->nb_colour_caps = count; + } + + for (i = 0; i < FF_ARRAY_ELEMS(process_vaapi_colour_balance); i++) { + const struct ProcessVAAPIColourBalance *adj = + &process_vaapi_colour_balance[i]; + VAProcFilterCapColorBalance *caps = NULL; + VAProcFilterParameterBufferColorBalance params; + + if (ctx->colour[adj->type] == -1) + continue; + + for (j = 0; j < ctx->nb_colour_caps; j++) { + if (ctx->colour_caps[j].type == adj->type) { + caps = &ctx->colour_caps[j]; + break; + } + } + if (!caps) { + av_log(avctx, AV_LOG_ERROR, "Colour balance type %s " + "is not supported.\n", adj->name); + return AVERROR(EIO); + } + + params.type = VAProcFilterColorBalance; + params.attrib = adj->type; + + if (adj->max == 1) { + params.value = (float)ctx->colour[adj->type]; + } else if (adj->max == 360) { + // Remap to the range which is actually provided. + if (ctx->colour[adj->type] <= caps->range.max_value) + params.value = (float)ctx->colour[adj->type]; + else + params.value = (float)(ctx->colour[adj->type] - 360); + } else { + params.value = process_vaapi_map_range(ctx->colour[adj->type], + adj->min, adj->max, + &caps->range); + } + av_log(avctx, AV_LOG_DEBUG, "Setting colour balance " + "parameter %s to %f.\n", adj->name, params.value); + + vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context, + VAProcFilterParameterBufferType, + sizeof(params), 1, ¶ms, + &filter_buffer); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to create colour " + "parameter buffer for %s: %d (%s).\n", + adj->name, vas, vaErrorStr(vas)); + return AVERROR(EIO); + } + + ctx->filter_buffers[ctx->nb_filter_buffers++] = filter_buffer; + } + + vas = vaQueryVideoProcPipelineCaps(ctx->hwctx->display, + ctx->va_context, + ctx->filter_buffers, + ctx->nb_filter_buffers, + &ctx->pipeline_caps); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to query pipeline " + "caps: %d (%s).\n", vas, vaErrorStr(vas)); + return AVERROR(EIO); + } + // That should succeed iff the pipeline was usable. Since we + // don't support any temporal filters here, we don't need to do + // anything else with the result. + + return 0; +} + +static int process_vaapi_config_output(AVFilterLink *outlink) +{ + AVFilterContext *avctx = outlink->src; + AVFilterLink *inlink = avctx->inputs[0]; + ProcessVAAPIContext *ctx = avctx->priv; + AVVAAPIFramesContext *va_frames; + VAStatus vas; + int err; + + process_vaapi_pipeline_uninit(avctx); + + av_assert0(ctx->frames); + ctx->device_ref = av_buffer_ref(ctx->frames->device_ref); + ctx->hwctx = ((AVHWDeviceContext*)ctx->device_ref->data)->hwctx; + + av_assert0(ctx->va_config == VA_INVALID_ID); + vas = vaCreateConfig(ctx->hwctx->display, + VAProfileNone, VAEntrypointVideoProc, + NULL, 0, &ctx->va_config); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to create processing " + "pipeline config: %d (%s).\n", vas, vaErrorStr(vas)); + return AVERROR(EIO); + } + + va_frames = ctx->frames->hwctx; + + av_assert0(ctx->va_context == VA_INVALID_ID); + vas = vaCreateContext(ctx->hwctx->display, ctx->va_config, + ctx->frames->width, ctx->frames->height, + VA_PROGRESSIVE, + va_frames->surface_ids, + va_frames->nb_surfaces, + &ctx->va_context); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to create processing " + "pipeline context: %d (%s).\n", vas, vaErrorStr(vas)); + return AVERROR(EIO); + } + + err = process_vaapi_build_parameters(avctx); + if (err) + return err; + + outlink->w = inlink->w; + outlink->h = inlink->h; + + outlink->hw_frames_ctx = av_buffer_ref(ctx->frames_ref); + if (!outlink->hw_frames_ctx) + return AVERROR(ENOMEM); + + return 0; +} + +static int vaapi_proc_colour_standard(enum AVColorSpace av_cs) +{ + switch(av_cs) { +#define CS(av, va) case AVCOL_SPC_ ## av: return VAProcColorStandard ## va; + CS(BT709, BT709); + CS(BT470BG, BT470BG); + CS(SMPTE170M, SMPTE170M); + CS(SMPTE240M, SMPTE240M); +#undef CS + default: + return VAProcColorStandardNone; + } +} + +static int process_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame) +{ + AVFilterContext *avctx = inlink->dst; + AVFilterLink *outlink = avctx->outputs[0]; + ProcessVAAPIContext *ctx = avctx->priv; + AVFrame *output_frame = NULL; + VASurfaceID input_surface, output_surface; + VAProcPipelineParameterBuffer params; + VABufferID params_id; + VAStatus vas; + int err; + + av_log(avctx, 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 (ctx->va_context == VA_INVALID_ID) + return AVERROR(EINVAL); + + input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3]; + av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for process input.\n", + input_surface); + + output_frame = av_frame_alloc(); + if (!output_frame) { + av_log(avctx, AV_LOG_ERROR, "Failed to allocate output frame."); + err = AVERROR(ENOMEM); + goto fail; + } + + err = av_hwframe_get_buffer(ctx->frames_ref, output_frame, 0); + if (err < 0) { + av_log(avctx, AV_LOG_ERROR, "Failed to get surface for " + "output: %d\n.", err); + } + + output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3]; + av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for process output.\n", + output_surface); + + memset(¶ms, 0, sizeof(params)); + + params.surface = input_surface; + params.surface_region = NULL; + params.surface_color_standard = + vaapi_proc_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 = 0; + + params.filters = ctx->filter_buffers; + params.num_filters = ctx->nb_filter_buffers; + + vas = vaBeginPicture(ctx->hwctx->display, + ctx->va_context, output_surface); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to attach new picture: " + "%d (%s).\n", vas, vaErrorStr(vas)); + err = AVERROR(EIO); + goto fail; + } + + vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context, + VAProcPipelineParameterBufferType, + sizeof(params), 1, ¶ms, ¶ms_id); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer: " + "%d (%s).\n", vas, vaErrorStr(vas)); + err = AVERROR(EIO); + goto fail_after_begin; + } + av_log(avctx, AV_LOG_DEBUG, "Pipeline parameter buffer is %#x.\n", + params_id); + + vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context, + ¶ms_id, 1); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to render parameter buffer: " + "%d (%s).\n", vas, vaErrorStr(vas)); + err = AVERROR(EIO); + goto fail_after_begin; + } + + vas = vaEndPicture(ctx->hwctx->display, ctx->va_context); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to start picture processing: " + "%d (%s).\n", vas, vaErrorStr(vas)); + err = AVERROR(EIO); + goto fail_after_render; + } + + if (/* ctx->hwctx->driver_quirks & + AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS */ 0) { + vas = vaDestroyBuffer(ctx->hwctx->display, params_id); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to free parameter buffer: " + "%d (%s).\n", vas, vaErrorStr(vas)); + // And ignore. + } + } + + av_frame_copy_props(output_frame, input_frame); + av_frame_free(&input_frame); + + av_log(avctx, 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); + + // We want to make sure that if vaBeginPicture has been called, we also + // call vaRenderPicture and vaEndPicture. These calls may well fail or + // do something else nasty, but once we're in this failure case there + // isn't much else we can do. +fail_after_begin: + vaRenderPicture(ctx->hwctx->display, ctx->va_context, ¶ms_id, 1); +fail_after_render: + vaEndPicture(ctx->hwctx->display, ctx->va_context); +fail: + av_frame_free(&input_frame); + av_frame_free(&output_frame); + return err; +} + +static av_cold int process_vaapi_init(AVFilterContext *avctx) +{ + ProcessVAAPIContext *ctx = avctx->priv; + + ctx->va_config = VA_INVALID_ID; + ctx->va_context = VA_INVALID_ID; + ctx->va_ids_set = 1; + + return 0; +} + +static av_cold void process_vaapi_uninit(AVFilterContext *avctx) +{ + ProcessVAAPIContext *ctx = avctx->priv; + int i; + + for (i = 0; i < ctx->nb_filter_buffers; i++) + vaDestroyBuffer(ctx->hwctx->display, + ctx->filter_buffers[i]); + + if (ctx->va_ids_set) + process_vaapi_pipeline_uninit(avctx); + + av_buffer_unref(&ctx->frames_ref); + av_buffer_unref(&ctx->device_ref); +} + +#define OFFSET(x) offsetof(ProcessVAAPIContext, x) +#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM) +static const AVOption process_vaapi_options[] = { + { "denoise", "Apply noise reduction filter (0 - 100)", + OFFSET(denoise), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 100, .flags = FLAGS }, + { "sharpness", "Apply sharpening filter (0 - 100)", + OFFSET(sharpness), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 100, .flags = FLAGS }, + + { "hue", "Adjust hue in (degrees, 0 - 360)", + OFFSET(colour[VAProcColorBalanceHue]), + AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 360, .flags = FLAGS }, + { "saturation", "Adjust saturation (0 - 100)", + OFFSET(colour[VAProcColorBalanceSaturation]), + AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 100, .flags = FLAGS }, + { "brightness", "Adjust brightness (0 - 100)", + OFFSET(colour[VAProcColorBalanceBrightness]), + AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 100, .flags = FLAGS }, + { "contrast", "Adjust contrast (0 - 100)", + OFFSET(colour[VAProcColorBalanceContrast]), + AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 100, .flags = FLAGS }, + + { "auto_saturation", "Automatically adjust saturation", + OFFSET(colour[VAProcColorBalanceAutoSaturation]), + AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, .flags = FLAGS }, + { "auto_brightness", "Automatically adjust brightness", + OFFSET(colour[VAProcColorBalanceAutoBrightness]), + AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, .flags = FLAGS }, + { "auto_contrast", "Automatically adjust contrast", + OFFSET(colour[VAProcColorBalanceAutoContrast]), + AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, .flags = FLAGS }, + + { NULL }, +}; + +static const AVClass process_vaapi_class = { + .class_name = "process_vaapi", + .item_name = av_default_item_name, + .option = process_vaapi_options, + .version = LIBAVUTIL_VERSION_INT, +}; + +static const AVFilterPad process_vaapi_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = &process_vaapi_filter_frame, + .config_props = &process_vaapi_config_input, + }, + { NULL } +}; + +static const AVFilterPad process_vaapi_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = &process_vaapi_config_output, + }, + { NULL } +}; + +AVFilter ff_vf_process_vaapi = { + .name = "process_vaapi", + .description = NULL_IF_CONFIG_SMALL("Video processing on VAAPI surfaces."), + .priv_size = sizeof(ProcessVAAPIContext), + .init = &process_vaapi_init, + .uninit = &process_vaapi_uninit, + .query_formats = &process_vaapi_query_formats, + .inputs = process_vaapi_inputs, + .outputs = process_vaapi_outputs, + .priv_class = &process_vaapi_class, +};