[FFmpeg-devel] [PATCH] lavfi: VAAPI video processing filter

Jun Zhao mypopydev at gmail.com
Sun Sep 18 03:26:19 EEST 2016


Can't find SKL available now, just run the test in IVY(Ivybridge)/Debian 8.5/
Kernel 3.16.0/Libva master/intel-driver master/ffmpeg master

- Build config:

    ./configure --enable-libx264 --enable-gpl --enable-vaapi --prefix=/opt/ffmpeg

- Libva and intel-driver

barry at barry:~/Source/video/ffmpeg$ vainfo 
libva info: VA-API version 0.39.3
libva info: va_getDriverName() returns 0
libva info: Trying to open /opt/yami/vaapi/lib/dri/i965_drv_video.so
libva info: Found init function __vaDriverInit_0_39
libva info: va_openDriver() returns 0
vainfo: VA-API version: 0.39 (libva 1.7.3.pre1)
vainfo: Driver version: Intel i965 driver for Intel(R) Ivybridge Mobile - 1.7.3.pre1 (1.7.0-118-gb5cd299)
vainfo: Supported profile and entrypoints
      VAProfileMPEG2Simple            :	VAEntrypointVLD
      VAProfileMPEG2Simple            :	VAEntrypointEncSlice
      VAProfileMPEG2Main              :	VAEntrypointVLD
      VAProfileMPEG2Main              :	VAEntrypointEncSlice
      VAProfileH264ConstrainedBaseline:	VAEntrypointVLD
      VAProfileH264ConstrainedBaseline:	VAEntrypointEncSlice
      VAProfileH264Main               :	VAEntrypointVLD
      VAProfileH264Main               :	VAEntrypointEncSlice
      VAProfileH264High               :	VAEntrypointVLD
      VAProfileH264High               :	VAEntrypointEncSlice
      VAProfileH264StereoHigh         :	VAEntrypointVLD
      VAProfileVC1Simple              :	VAEntrypointVLD
      VAProfileVC1Main                :	VAEntrypointVLD
      VAProfileVC1Advanced            :	VAEntrypointVLD
      VAProfileNone                   :	VAEntrypointVideoProc
      VAProfileJPEGBaseline           :	VAEntrypointVLD

- Kernel and distribution

barry at barry:~/Source/video/ffmpeg$ uname -a
Linux barry 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-2+deb8u3 (2016-07-02) x86_64 GNU/Linux

barry at barry:~/Source/video/ffmpeg$ lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 8.5 (jessie)
Release:	8.5
Codename:	jessie

- Test result

a). denoise -> scale [With the patch adding vf_process_vaapi]

./ffmpeg_g -y -vaapi_device /dev/dri/card0 -hwaccel vaapi -hwaccel_output_format vaapi -i ../yami/ffmpeg_yami_testcase/skyfall2-trailer.mp4 -an -vf 'format=vaapi|nv12,hwupload,process_vaapi=denoise=50,scale_vaapi=w=1280:h=720' -c:v h264_vaapi -qp 20 out_denoise_scale.mp4

121 fps

b). scale -> denosie [With the patch adding vf_process_vaapi]

./ffmpeg_g -y -vaapi_device /dev/dri/card0 -hwaccel vaapi -hwaccel_output_format vaapi -i ../yami/ffmpeg_yami_testcase/skyfall2-trailer.mp4 -an -vf 'format=vaapi|nv12,hwupload,scale_vaapi=w=1280:h=720,process_vaapi=denoise=50' -c:v h264_vaapi -qp 20 out_scale_denoise.mp4

169 fps

c). scale + denosie in all [patch to vf_scale_vaapi]

 ./ffmpeg_g -y -vaapi_device /dev/dri/card0 -hwaccel vaapi -hwaccel_output_format vaapi -i ../yami/ffmpeg_yami_testcase/skyfall2-trailer.mp4 -an -vf 'format=vaapi|nv12,hwupload,scale_vaapi=w=1280:h=720:denoise=50' -c:v h264_vaapi -qp 20 out_all.mp4

139 fps

d). scale without denoise

./ffmpeg_g -y -vaapi_device /dev/dri/card0 -hwaccel vaapi -hwaccel_output_format vaapi -i ../yami/ffmpeg_yami_testcase/skyfall2-trailer.mp4 -an -vf 'format=vaapi|nv12,hwupload,scale_vaapi=w=1280:h=720' -c:v h264_vaapi -qp 20 out_scale.mp4

254 fps

I will try this after find the SKL. :)

On 2016/9/14 6:06, Mark Thompson wrote:
> 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 <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"
> +
> +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, &params,
> +                             &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, &params,
> +                             &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, &params,
> +                             &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(&params, 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, &params, &params_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,
> +                          &params_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, &params_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,
> +};
> 


More information about the ffmpeg-devel mailing list