diff mbox

[FFmpeg-devel,3/5] lavfi: use common VPP infrastructure for vf_deinterlace_vaapi.

Message ID 302762a6-3c35-cc8a-a3b6-7e47fcb855fd@gmail.com
State New
Headers show

Commit Message

Jun Zhao Jan. 8, 2018, 8:35 a.m. UTC
From 0274a13bc933e4d82a937afb6886b4132834ecff Mon Sep 17 00:00:00 2001
From: Jun Zhao <jun.zhao@intel.com>
Date: Mon, 8 Jan 2018 16:07:38 +0800
Subject: [PATCH 3/5] lavfi: use common VPP infrastructure for
 vf_deinterlace_vaapi.

Use the common VPP infrastructure re-work vf_deinterlace_vaapi.

Signed-off-by: Jun Zhao <jun.zhao@intel.com>
---
 libavfilter/Makefile               |   2 +-
 libavfilter/vf_deinterlace_vaapi.c | 318 ++++++-------------------------------
 2 files changed, 51 insertions(+), 269 deletions(-)

Comments

Mark Thompson Jan. 14, 2018, 10:30 p.m. UTC | #1
On 08/01/18 08:35, Jun Zhao wrote:
> 
> From 0274a13bc933e4d82a937afb6886b4132834ecff Mon Sep 17 00:00:00 2001
> From: Jun Zhao <jun.zhao@intel.com>
> Date: Mon, 8 Jan 2018 16:07:38 +0800
> Subject: [PATCH 3/5] lavfi: use common VPP infrastructure for
>  vf_deinterlace_vaapi.
> 
> Use the common VPP infrastructure re-work vf_deinterlace_vaapi.
> 
> Signed-off-by: Jun Zhao <jun.zhao@intel.com>
> ---
>  libavfilter/Makefile               |   2 +-
>  libavfilter/vf_deinterlace_vaapi.c | 318 ++++++-------------------------------
>  2 files changed, 51 insertions(+), 269 deletions(-)
> 
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 3d8dd2c890..bbc97a0831 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -171,7 +171,7 @@ OBJS-$(CONFIG_DECONVOLVE_FILTER)             += vf_convolve.o framesync.o
>  OBJS-$(CONFIG_DEFLATE_FILTER)                += vf_neighbor.o
>  OBJS-$(CONFIG_DEFLICKER_FILTER)              += vf_deflicker.o
>  OBJS-$(CONFIG_DEINTERLACE_QSV_FILTER)        += vf_deinterlace_qsv.o
> -OBJS-$(CONFIG_DEINTERLACE_VAAPI_FILTER)      += vf_deinterlace_vaapi.o
> +OBJS-$(CONFIG_DEINTERLACE_VAAPI_FILTER)      += vf_deinterlace_vaapi.o vaapi_vpp.o
>  OBJS-$(CONFIG_DEJUDDER_FILTER)               += vf_dejudder.o
>  OBJS-$(CONFIG_DELOGO_FILTER)                 += vf_delogo.o
>  OBJS-$(CONFIG_DESHAKE_FILTER)                += vf_deshake.o
> diff --git a/libavfilter/vf_deinterlace_vaapi.c b/libavfilter/vf_deinterlace_vaapi.c
> index 44c5ae7642..f9f5fcd096 100644
> --- a/libavfilter/vf_deinterlace_vaapi.c
> +++ b/libavfilter/vf_deinterlace_vaapi.c
> @@ -33,31 +33,19 @@
>  #include "formats.h"
>  #include "internal.h"
>  #include "video.h"
> +#include "vaapi_vpp.h"
>  
>  #define MAX_REFERENCES 8
>  
>  typedef struct DeintVAAPIContext {
>      const AVClass     *class;
>  
> -    AVVAAPIDeviceContext *hwctx;
> -    AVBufferRef       *device_ref;
> +    VAAPIVPPContext   *vpp_ctx;
>  
>      int                mode;
>      int                field_rate;
>      int                auto_enable;
>  
> -    int                valid_ids;
> -    VAConfigID         va_config;
> -    VAContextID        va_context;
> -
> -    AVBufferRef       *input_frames_ref;
> -    AVHWFramesContext *input_frames;
> -
> -    AVBufferRef       *output_frames_ref;
> -    AVHWFramesContext *output_frames;
> -    int                output_height;
> -    int                output_width;
> -
>      VAProcFilterCapDeinterlacing
>                         deint_caps[VAProcDeinterlacingCount];
>      int             nb_deint_caps;
> @@ -67,8 +55,6 @@ typedef struct DeintVAAPIContext {
>      int                queue_count;
>      AVFrame           *frame_queue[MAX_REFERENCES];
>      int                extra_delay_for_timestamps;
> -
> -    VABufferID         filter_buffer;
>  } DeintVAAPIContext;
>  
>  static const char *deint_vaapi_mode_name(int mode)
> @@ -87,80 +73,43 @@ static const char *deint_vaapi_mode_name(int mode)
>  
>  static int deint_vaapi_query_formats(AVFilterContext *avctx)
>  {
> -    enum AVPixelFormat pix_fmts[] = {
> -        AV_PIX_FMT_VAAPI, AV_PIX_FMT_NONE,
> -    };
> -    int err;
> -
> -    if ((err = ff_formats_ref(ff_make_format_list(pix_fmts),
> -                              &avctx->inputs[0]->out_formats)) < 0)
> -        return err;
> -    if ((err = ff_formats_ref(ff_make_format_list(pix_fmts),
> -                              &avctx->outputs[0]->in_formats)) < 0)
> -        return err;
> -
> -    return 0;
> +    return vaapi_vpp_query_formats(avctx);
>  }
>  
>  static int deint_vaapi_pipeline_uninit(AVFilterContext *avctx)
>  {
>      DeintVAAPIContext *ctx = avctx->priv;
> +    VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
>      int i;
>  
>      for (i = 0; i < ctx->queue_count; i++)
>          av_frame_free(&ctx->frame_queue[i]);
>      ctx->queue_count = 0;
>  
> -    if (ctx->filter_buffer != VA_INVALID_ID) {
> -        vaDestroyBuffer(ctx->hwctx->display, ctx->filter_buffer);
> -        ctx->filter_buffer = VA_INVALID_ID;
> -    }
> -
> -    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;
> +    return vaapi_vpp_pipeline_uninit(vpp_ctx);
>  }
>  
>  static int deint_vaapi_config_input(AVFilterLink *inlink)
>  {
>      AVFilterContext   *avctx = inlink->dst;
> -    DeintVAAPIContext *ctx = avctx->priv;
> -
> -    deint_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);
> -    }
> +    DeintVAAPIContext *ctx   = avctx->priv;
>  
> -    ctx->input_frames_ref = av_buffer_ref(inlink->hw_frames_ctx);
> -    ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
> +    VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
>  
> -    return 0;
> +    return vaapi_vpp_config_input(inlink, vpp_ctx);
>  }
>  
>  static int deint_vaapi_build_filter_params(AVFilterContext *avctx)
>  {
> -    DeintVAAPIContext *ctx = avctx->priv;
> +    DeintVAAPIContext *ctx   = avctx->priv;
> +    VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
>      VAStatus vas;
>      VAProcFilterParameterBufferDeinterlacing params;
>      int i;
>  
>      ctx->nb_deint_caps = VAProcDeinterlacingCount;
> -    vas = vaQueryVideoProcFilterCaps(ctx->hwctx->display,
> -                                     ctx->va_context,
> +    vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display,
> +                                     vpp_ctx->va_context,
>                                       VAProcFilterDeinterlacing,
>                                       &ctx->deint_caps,
>                                       &ctx->nb_deint_caps);
> @@ -194,20 +143,15 @@ static int deint_vaapi_build_filter_params(AVFilterContext *avctx)
>      params.algorithm = ctx->mode;
>      params.flags     = 0;
>  
> -    av_assert0(ctx->filter_buffer == VA_INVALID_ID);
> -    vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
> -                         VAProcFilterParameterBufferType,
> -                         sizeof(params), 1, &params,
> -                         &ctx->filter_buffer);
> -    if (vas != VA_STATUS_SUCCESS) {
> -        av_log(avctx, AV_LOG_ERROR, "Failed to create deinterlace "
> -               "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas));
> -        return AVERROR(EIO);
> -    }
> +    vaapi_vpp_make_param_buffers(vpp_ctx,
> +                                 VAProcFilterParameterBufferType,
> +                                 &params,
> +                                 sizeof(params),
> +                                 1);
>  
> -    vas = vaQueryVideoProcPipelineCaps(ctx->hwctx->display,
> -                                       ctx->va_context,
> -                                       &ctx->filter_buffer, 1,
> +    vas = vaQueryVideoProcPipelineCaps(vpp_ctx->hwctx->display,
> +                                       vpp_ctx->va_context,
> +                                       &vpp_ctx->filter_buffers[0], 1,
>                                         &ctx->pipeline_caps);
>      if (vas != VA_STATUS_SUCCESS) {
>          av_log(avctx, AV_LOG_ERROR, "Failed to query pipeline "
> @@ -234,151 +178,35 @@ static int deint_vaapi_build_filter_params(AVFilterContext *avctx)
>  
>  static int deint_vaapi_config_output(AVFilterLink *outlink)
>  {
> -    AVFilterContext    *avctx = outlink->src;
> -    AVFilterLink      *inlink = avctx->inputs[0];
> -    DeintVAAPIContext    *ctx = avctx->priv;
> -    AVVAAPIHWConfig *hwconfig = NULL;
> -    AVHWFramesConstraints *constraints = NULL;
> -    AVVAAPIFramesContext *va_frames;
> -    VAStatus vas;
> +    AVFilterLink *inlink = outlink->src->inputs[0];
> +    AVFilterContext *avctx = outlink->src;
> +    DeintVAAPIContext *ctx = avctx->priv;
> +    VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
>      int err;
>  
> -    deint_vaapi_pipeline_uninit(avctx);
> -
> -    av_assert0(ctx->input_frames);
> -    ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
> -    ctx->hwctx = ((AVHWDeviceContext*)ctx->device_ref->data)->hwctx;
> -
> -    ctx->output_width  = ctx->input_frames->width;
> -    ctx->output_height = ctx->input_frames->height;
> -
> -    av_assert0(ctx->va_config == VA_INVALID_ID);
> -    vas = vaCreateConfig(ctx->hwctx->display, VAProfileNone,
> -                         VAEntrypointVideoProc, 0, 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));
> -        err = AVERROR(EIO);
> -        goto fail;
> -    }
> -
> -    hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
> -    if (!hwconfig) {
> -        err = AVERROR(ENOMEM);
> -        goto fail;
> -    }
> -    hwconfig->config_id = ctx->va_config;
> -
> -    constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
> -                                                      hwconfig);
> -    if (!constraints) {
> -        err = AVERROR(ENOMEM);
> -        goto fail;
> -    }
> -
> -    if (ctx->output_width  < constraints->min_width  ||
> -        ctx->output_height < constraints->min_height ||
> -        ctx->output_width  > constraints->max_width  ||
> -        ctx->output_height > constraints->max_height) {
> -        av_log(avctx, AV_LOG_ERROR, "Hardware does not support "
> -               "deinterlacing to size %dx%d "
> -               "(constraints: width %d-%d height %d-%d).\n",
> -               ctx->output_width, ctx->output_height,
> -               constraints->min_width,  constraints->max_width,
> -               constraints->min_height, constraints->max_height);
> -        err = AVERROR(EINVAL);
> -        goto fail;
> -    }
> -
> -    ctx->output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
> -    if (!ctx->output_frames_ref) {
> -        av_log(avctx, AV_LOG_ERROR, "Failed to create HW frame context "
> -               "for output.\n");
> -        err = AVERROR(ENOMEM);
> -        goto fail;
> -    }
> -
> -    ctx->output_frames = (AVHWFramesContext*)ctx->output_frames_ref->data;
> -
> -    ctx->output_frames->format    = AV_PIX_FMT_VAAPI;
> -    ctx->output_frames->sw_format = ctx->input_frames->sw_format;
> -    ctx->output_frames->width     = ctx->output_width;
> -    ctx->output_frames->height    = ctx->output_height;
> -
> -    // The number of output frames we need is determined by what follows
> -    // the filter.  If it's an encoder with complex frame reference
> -    // structures then this could be very high.
> -    ctx->output_frames->initial_pool_size = 10;
> -
> -    err = av_hwframe_ctx_init(ctx->output_frames_ref);
> -    if (err < 0) {
> -        av_log(avctx, AV_LOG_ERROR, "Failed to initialise VAAPI frame "
> -               "context for output: %d\n", err);
> -        goto fail;
> -    }
> -
> -    va_frames = ctx->output_frames->hwctx;
> -
> -    av_assert0(ctx->va_context == VA_INVALID_ID);
> -    vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
> -                          ctx->output_width, ctx->output_height, 0,
> -                          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));
> -        err = AVERROR(EIO);
> -        goto fail;
> -    }
> +    ctx->vpp_ctx->output_width = avctx->inputs[0]->w;
> +    ctx->vpp_ctx->output_height = avctx->inputs[0]->h;

You already have inlink here to use instead of avctx->inputs[0].

Could this be the default inside vaapi_vpp_config_output() if output_width or output_height aren't set?  It's repeated in all the following filters, only scale sets a different value.

>  
> -    err = deint_vaapi_build_filter_params(avctx);
> +    err = vaapi_vpp_config_output(outlink, vpp_ctx);
>      if (err < 0)
>          goto fail;
> -
> -    outlink->w = inlink->w;
> -    outlink->h = inlink->h;
> -
>      outlink->time_base  = av_mul_q(inlink->time_base,
>                                     (AVRational) { 1, ctx->field_rate });
>      outlink->frame_rate = av_mul_q(inlink->frame_rate,
>                                     (AVRational) { ctx->field_rate, 1 });
>  
> -    outlink->hw_frames_ctx = av_buffer_ref(ctx->output_frames_ref);
> -    if (!outlink->hw_frames_ctx) {
> -        err = AVERROR(ENOMEM);
> -        goto fail;
> -    }
> -
> -    av_freep(&hwconfig);
> -    av_hwframe_constraints_free(&constraints);
>      return 0;
>  
>  fail:
> -    av_buffer_unref(&ctx->output_frames_ref);
> -    av_freep(&hwconfig);
> -    av_hwframe_constraints_free(&constraints);
>      return err;

This no longer has any cleanup to do, you could just return the error code directly where the goto currently is.

>  }
>  
> -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 deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
>  {
>      AVFilterContext   *avctx = inlink->dst;
>      AVFilterLink    *outlink = avctx->outputs[0];
>      DeintVAAPIContext *ctx = avctx->priv;
> +    VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
>      AVFrame *output_frame = NULL;
>      VASurfaceID input_surface, output_surface;
>      VASurfaceID backward_references[MAX_REFERENCES];
> @@ -386,7 +214,6 @@ static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
>      VAProcPipelineParameterBuffer params;
>      VAProcFilterParameterBufferDeinterlacing *filter_params;
>      VARectangle input_region;
> -    VABufferID params_id;
>      VAStatus vas;
>      void *filter_params_addr = NULL;
>      int err, i, field, current_frame_index;
> @@ -431,8 +258,8 @@ static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
>      av_log(avctx, AV_LOG_DEBUG, "\n");
>  
>      for (field = 0; field < ctx->field_rate; field++) {
> -        output_frame = ff_get_video_buffer(outlink, ctx->output_width,
> -                                           ctx->output_height);
> +        output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
> +                                           vpp_ctx->output_height);
>          if (!output_frame) {
>              err = AVERROR(ENOMEM);
>              goto fail;
> @@ -454,7 +281,7 @@ static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
>          params.surface = input_surface;
>          params.surface_region = &input_region;
>          params.surface_color_standard =
> -            vaapi_proc_colour_standard(input_frame->colorspace);
> +            vaapi_vpp_colour_standard(input_frame->colorspace);
>  
>          params.output_region = NULL;
>          params.output_background_color = 0xff000000;
> @@ -464,7 +291,7 @@ static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
>          params.filter_flags   = VA_FRAME_PICTURE;
>  
>          if (!ctx->auto_enable || input_frame->interlaced_frame) {
> -            vas = vaMapBuffer(ctx->hwctx->display, ctx->filter_buffer,
> +            vas = vaMapBuffer(vpp_ctx->hwctx->display, vpp_ctx->filter_buffers[0],
>                                &filter_params_addr);
>              if (vas != VA_STATUS_SUCCESS) {
>                  av_log(avctx, AV_LOG_ERROR, "Failed to map filter parameter "
> @@ -481,12 +308,12 @@ static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
>                  filter_params->flags |= field ? 0 : VA_DEINTERLACING_BOTTOM_FIELD;
>              }
>              filter_params_addr = NULL;
> -            vas = vaUnmapBuffer(ctx->hwctx->display, ctx->filter_buffer);
> +            vas = vaUnmapBuffer(vpp_ctx->hwctx->display, vpp_ctx->filter_buffers[0]);
>              if (vas != VA_STATUS_SUCCESS)
>                  av_log(avctx, AV_LOG_ERROR, "Failed to unmap filter parameter "
>                         "buffer: %d (%s).\n", vas, vaErrorStr(vas));
>  
> -            params.filters     = &ctx->filter_buffer;
> +            params.filters     = &vpp_ctx->filter_buffers[0];
>              params.num_filters = 1;
>  
>              params.forward_references = forward_references;
> @@ -501,53 +328,9 @@ static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
>              params.num_filters = 0;
>          }
>  
> -        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);
> +        err = vaapi_vpp_render_picture(vpp_ctx, &params, output_surface);
> +        if (err < 0)
>              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 (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
> -            AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) {
> -            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.
> -            }
> -        }
>  
>          err = av_frame_copy_props(output_frame, input_frame);
>          if (err < 0)
> @@ -573,13 +356,9 @@ static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
>  
>      return err;
>  
> -fail_after_begin:
> -    vaRenderPicture(ctx->hwctx->display, ctx->va_context, &params_id, 1);
> -fail_after_render:
> -    vaEndPicture(ctx->hwctx->display, ctx->va_context);
>  fail:
>      if (filter_params_addr)
> -        vaUnmapBuffer(ctx->hwctx->display, ctx->filter_buffer);
> +        vaUnmapBuffer(vpp_ctx->hwctx->display, vpp_ctx->filter_buffers[0]);
>      av_frame_free(&output_frame);
>      return err;
>  }
> @@ -587,11 +366,18 @@ fail:
>  static av_cold int deint_vaapi_init(AVFilterContext *avctx)
>  {
>      DeintVAAPIContext *ctx = avctx->priv;
> +    VAAPIVPPContext *vpp_ctx;
> +
> +    ctx->vpp_ctx = av_mallocz(sizeof(VAAPIVPPContext));
> +    if (!ctx->vpp_ctx)
> +        return AVERROR(ENOMEM);
>  
> -    ctx->va_config     = VA_INVALID_ID;
> -    ctx->va_context    = VA_INVALID_ID;
> -    ctx->filter_buffer = VA_INVALID_ID;
> -    ctx->valid_ids = 1;
> +    vpp_ctx = ctx->vpp_ctx;
> +
> +    vaapi_vpp_ctx_init(vpp_ctx);
> +    vpp_ctx->pipeline_uninit     = deint_vaapi_pipeline_uninit;
> +    vpp_ctx->build_filter_params = deint_vaapi_build_filter_params;
> +    vpp_ctx->output_format = AV_PIX_FMT_NONE;
>  
>      return 0;
>  }
> @@ -599,13 +385,9 @@ static av_cold int deint_vaapi_init(AVFilterContext *avctx)
>  static av_cold void deint_vaapi_uninit(AVFilterContext *avctx)
>  {
>      DeintVAAPIContext *ctx = avctx->priv;
> +    VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
>  
> -    if (ctx->valid_ids)
> -        deint_vaapi_pipeline_uninit(avctx);
> -
> -    av_buffer_unref(&ctx->input_frames_ref);
> -    av_buffer_unref(&ctx->output_frames_ref);
> -    av_buffer_unref(&ctx->device_ref);
> +    vaapi_vpp_ctx_uninit(avctx, vpp_ctx);
>  }
>  
>  #define OFFSET(x) offsetof(DeintVAAPIContext, x)
> -- 
> 2.14.1
>
diff mbox

Patch

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 3d8dd2c890..bbc97a0831 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -171,7 +171,7 @@  OBJS-$(CONFIG_DECONVOLVE_FILTER)             += vf_convolve.o framesync.o
 OBJS-$(CONFIG_DEFLATE_FILTER)                += vf_neighbor.o
 OBJS-$(CONFIG_DEFLICKER_FILTER)              += vf_deflicker.o
 OBJS-$(CONFIG_DEINTERLACE_QSV_FILTER)        += vf_deinterlace_qsv.o
-OBJS-$(CONFIG_DEINTERLACE_VAAPI_FILTER)      += vf_deinterlace_vaapi.o
+OBJS-$(CONFIG_DEINTERLACE_VAAPI_FILTER)      += vf_deinterlace_vaapi.o vaapi_vpp.o
 OBJS-$(CONFIG_DEJUDDER_FILTER)               += vf_dejudder.o
 OBJS-$(CONFIG_DELOGO_FILTER)                 += vf_delogo.o
 OBJS-$(CONFIG_DESHAKE_FILTER)                += vf_deshake.o
diff --git a/libavfilter/vf_deinterlace_vaapi.c b/libavfilter/vf_deinterlace_vaapi.c
index 44c5ae7642..f9f5fcd096 100644
--- a/libavfilter/vf_deinterlace_vaapi.c
+++ b/libavfilter/vf_deinterlace_vaapi.c
@@ -33,31 +33,19 @@ 
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
+#include "vaapi_vpp.h"
 
 #define MAX_REFERENCES 8
 
 typedef struct DeintVAAPIContext {
     const AVClass     *class;
 
-    AVVAAPIDeviceContext *hwctx;
-    AVBufferRef       *device_ref;
+    VAAPIVPPContext   *vpp_ctx;
 
     int                mode;
     int                field_rate;
     int                auto_enable;
 
-    int                valid_ids;
-    VAConfigID         va_config;
-    VAContextID        va_context;
-
-    AVBufferRef       *input_frames_ref;
-    AVHWFramesContext *input_frames;
-
-    AVBufferRef       *output_frames_ref;
-    AVHWFramesContext *output_frames;
-    int                output_height;
-    int                output_width;
-
     VAProcFilterCapDeinterlacing
                        deint_caps[VAProcDeinterlacingCount];
     int             nb_deint_caps;
@@ -67,8 +55,6 @@  typedef struct DeintVAAPIContext {
     int                queue_count;
     AVFrame           *frame_queue[MAX_REFERENCES];
     int                extra_delay_for_timestamps;
-
-    VABufferID         filter_buffer;
 } DeintVAAPIContext;
 
 static const char *deint_vaapi_mode_name(int mode)
@@ -87,80 +73,43 @@  static const char *deint_vaapi_mode_name(int mode)
 
 static int deint_vaapi_query_formats(AVFilterContext *avctx)
 {
-    enum AVPixelFormat pix_fmts[] = {
-        AV_PIX_FMT_VAAPI, AV_PIX_FMT_NONE,
-    };
-    int err;
-
-    if ((err = ff_formats_ref(ff_make_format_list(pix_fmts),
-                              &avctx->inputs[0]->out_formats)) < 0)
-        return err;
-    if ((err = ff_formats_ref(ff_make_format_list(pix_fmts),
-                              &avctx->outputs[0]->in_formats)) < 0)
-        return err;
-
-    return 0;
+    return vaapi_vpp_query_formats(avctx);
 }
 
 static int deint_vaapi_pipeline_uninit(AVFilterContext *avctx)
 {
     DeintVAAPIContext *ctx = avctx->priv;
+    VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
     int i;
 
     for (i = 0; i < ctx->queue_count; i++)
         av_frame_free(&ctx->frame_queue[i]);
     ctx->queue_count = 0;
 
-    if (ctx->filter_buffer != VA_INVALID_ID) {
-        vaDestroyBuffer(ctx->hwctx->display, ctx->filter_buffer);
-        ctx->filter_buffer = VA_INVALID_ID;
-    }
-
-    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;
+    return vaapi_vpp_pipeline_uninit(vpp_ctx);
 }
 
 static int deint_vaapi_config_input(AVFilterLink *inlink)
 {
     AVFilterContext   *avctx = inlink->dst;
-    DeintVAAPIContext *ctx = avctx->priv;
-
-    deint_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);
-    }
+    DeintVAAPIContext *ctx   = avctx->priv;
 
-    ctx->input_frames_ref = av_buffer_ref(inlink->hw_frames_ctx);
-    ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
+    VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
 
-    return 0;
+    return vaapi_vpp_config_input(inlink, vpp_ctx);
 }
 
 static int deint_vaapi_build_filter_params(AVFilterContext *avctx)
 {
-    DeintVAAPIContext *ctx = avctx->priv;
+    DeintVAAPIContext *ctx   = avctx->priv;
+    VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
     VAStatus vas;
     VAProcFilterParameterBufferDeinterlacing params;
     int i;
 
     ctx->nb_deint_caps = VAProcDeinterlacingCount;
-    vas = vaQueryVideoProcFilterCaps(ctx->hwctx->display,
-                                     ctx->va_context,
+    vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display,
+                                     vpp_ctx->va_context,
                                      VAProcFilterDeinterlacing,
                                      &ctx->deint_caps,
                                      &ctx->nb_deint_caps);
@@ -194,20 +143,15 @@  static int deint_vaapi_build_filter_params(AVFilterContext *avctx)
     params.algorithm = ctx->mode;
     params.flags     = 0;
 
-    av_assert0(ctx->filter_buffer == VA_INVALID_ID);
-    vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
-                         VAProcFilterParameterBufferType,
-                         sizeof(params), 1, &params,
-                         &ctx->filter_buffer);
-    if (vas != VA_STATUS_SUCCESS) {
-        av_log(avctx, AV_LOG_ERROR, "Failed to create deinterlace "
-               "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas));
-        return AVERROR(EIO);
-    }
+    vaapi_vpp_make_param_buffers(vpp_ctx,
+                                 VAProcFilterParameterBufferType,
+                                 &params,
+                                 sizeof(params),
+                                 1);
 
-    vas = vaQueryVideoProcPipelineCaps(ctx->hwctx->display,
-                                       ctx->va_context,
-                                       &ctx->filter_buffer, 1,
+    vas = vaQueryVideoProcPipelineCaps(vpp_ctx->hwctx->display,
+                                       vpp_ctx->va_context,
+                                       &vpp_ctx->filter_buffers[0], 1,
                                        &ctx->pipeline_caps);
     if (vas != VA_STATUS_SUCCESS) {
         av_log(avctx, AV_LOG_ERROR, "Failed to query pipeline "
@@ -234,151 +178,35 @@  static int deint_vaapi_build_filter_params(AVFilterContext *avctx)
 
 static int deint_vaapi_config_output(AVFilterLink *outlink)
 {
-    AVFilterContext    *avctx = outlink->src;
-    AVFilterLink      *inlink = avctx->inputs[0];
-    DeintVAAPIContext    *ctx = avctx->priv;
-    AVVAAPIHWConfig *hwconfig = NULL;
-    AVHWFramesConstraints *constraints = NULL;
-    AVVAAPIFramesContext *va_frames;
-    VAStatus vas;
+    AVFilterLink *inlink = outlink->src->inputs[0];
+    AVFilterContext *avctx = outlink->src;
+    DeintVAAPIContext *ctx = avctx->priv;
+    VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
     int err;
 
-    deint_vaapi_pipeline_uninit(avctx);
-
-    av_assert0(ctx->input_frames);
-    ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
-    ctx->hwctx = ((AVHWDeviceContext*)ctx->device_ref->data)->hwctx;
-
-    ctx->output_width  = ctx->input_frames->width;
-    ctx->output_height = ctx->input_frames->height;
-
-    av_assert0(ctx->va_config == VA_INVALID_ID);
-    vas = vaCreateConfig(ctx->hwctx->display, VAProfileNone,
-                         VAEntrypointVideoProc, 0, 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));
-        err = AVERROR(EIO);
-        goto fail;
-    }
-
-    hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
-    if (!hwconfig) {
-        err = AVERROR(ENOMEM);
-        goto fail;
-    }
-    hwconfig->config_id = ctx->va_config;
-
-    constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
-                                                      hwconfig);
-    if (!constraints) {
-        err = AVERROR(ENOMEM);
-        goto fail;
-    }
-
-    if (ctx->output_width  < constraints->min_width  ||
-        ctx->output_height < constraints->min_height ||
-        ctx->output_width  > constraints->max_width  ||
-        ctx->output_height > constraints->max_height) {
-        av_log(avctx, AV_LOG_ERROR, "Hardware does not support "
-               "deinterlacing to size %dx%d "
-               "(constraints: width %d-%d height %d-%d).\n",
-               ctx->output_width, ctx->output_height,
-               constraints->min_width,  constraints->max_width,
-               constraints->min_height, constraints->max_height);
-        err = AVERROR(EINVAL);
-        goto fail;
-    }
-
-    ctx->output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
-    if (!ctx->output_frames_ref) {
-        av_log(avctx, AV_LOG_ERROR, "Failed to create HW frame context "
-               "for output.\n");
-        err = AVERROR(ENOMEM);
-        goto fail;
-    }
-
-    ctx->output_frames = (AVHWFramesContext*)ctx->output_frames_ref->data;
-
-    ctx->output_frames->format    = AV_PIX_FMT_VAAPI;
-    ctx->output_frames->sw_format = ctx->input_frames->sw_format;
-    ctx->output_frames->width     = ctx->output_width;
-    ctx->output_frames->height    = ctx->output_height;
-
-    // The number of output frames we need is determined by what follows
-    // the filter.  If it's an encoder with complex frame reference
-    // structures then this could be very high.
-    ctx->output_frames->initial_pool_size = 10;
-
-    err = av_hwframe_ctx_init(ctx->output_frames_ref);
-    if (err < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Failed to initialise VAAPI frame "
-               "context for output: %d\n", err);
-        goto fail;
-    }
-
-    va_frames = ctx->output_frames->hwctx;
-
-    av_assert0(ctx->va_context == VA_INVALID_ID);
-    vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
-                          ctx->output_width, ctx->output_height, 0,
-                          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));
-        err = AVERROR(EIO);
-        goto fail;
-    }
+    ctx->vpp_ctx->output_width = avctx->inputs[0]->w;
+    ctx->vpp_ctx->output_height = avctx->inputs[0]->h;
 
-    err = deint_vaapi_build_filter_params(avctx);
+    err = vaapi_vpp_config_output(outlink, vpp_ctx);
     if (err < 0)
         goto fail;
-
-    outlink->w = inlink->w;
-    outlink->h = inlink->h;
-
     outlink->time_base  = av_mul_q(inlink->time_base,
                                    (AVRational) { 1, ctx->field_rate });
     outlink->frame_rate = av_mul_q(inlink->frame_rate,
                                    (AVRational) { ctx->field_rate, 1 });
 
-    outlink->hw_frames_ctx = av_buffer_ref(ctx->output_frames_ref);
-    if (!outlink->hw_frames_ctx) {
-        err = AVERROR(ENOMEM);
-        goto fail;
-    }
-
-    av_freep(&hwconfig);
-    av_hwframe_constraints_free(&constraints);
     return 0;
 
 fail:
-    av_buffer_unref(&ctx->output_frames_ref);
-    av_freep(&hwconfig);
-    av_hwframe_constraints_free(&constraints);
     return err;
 }
 
-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 deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
 {
     AVFilterContext   *avctx = inlink->dst;
     AVFilterLink    *outlink = avctx->outputs[0];
     DeintVAAPIContext *ctx = avctx->priv;
+    VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
     AVFrame *output_frame = NULL;
     VASurfaceID input_surface, output_surface;
     VASurfaceID backward_references[MAX_REFERENCES];
@@ -386,7 +214,6 @@  static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
     VAProcPipelineParameterBuffer params;
     VAProcFilterParameterBufferDeinterlacing *filter_params;
     VARectangle input_region;
-    VABufferID params_id;
     VAStatus vas;
     void *filter_params_addr = NULL;
     int err, i, field, current_frame_index;
@@ -431,8 +258,8 @@  static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
     av_log(avctx, AV_LOG_DEBUG, "\n");
 
     for (field = 0; field < ctx->field_rate; field++) {
-        output_frame = ff_get_video_buffer(outlink, ctx->output_width,
-                                           ctx->output_height);
+        output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
+                                           vpp_ctx->output_height);
         if (!output_frame) {
             err = AVERROR(ENOMEM);
             goto fail;
@@ -454,7 +281,7 @@  static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
         params.surface = input_surface;
         params.surface_region = &input_region;
         params.surface_color_standard =
-            vaapi_proc_colour_standard(input_frame->colorspace);
+            vaapi_vpp_colour_standard(input_frame->colorspace);
 
         params.output_region = NULL;
         params.output_background_color = 0xff000000;
@@ -464,7 +291,7 @@  static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
         params.filter_flags   = VA_FRAME_PICTURE;
 
         if (!ctx->auto_enable || input_frame->interlaced_frame) {
-            vas = vaMapBuffer(ctx->hwctx->display, ctx->filter_buffer,
+            vas = vaMapBuffer(vpp_ctx->hwctx->display, vpp_ctx->filter_buffers[0],
                               &filter_params_addr);
             if (vas != VA_STATUS_SUCCESS) {
                 av_log(avctx, AV_LOG_ERROR, "Failed to map filter parameter "
@@ -481,12 +308,12 @@  static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
                 filter_params->flags |= field ? 0 : VA_DEINTERLACING_BOTTOM_FIELD;
             }
             filter_params_addr = NULL;
-            vas = vaUnmapBuffer(ctx->hwctx->display, ctx->filter_buffer);
+            vas = vaUnmapBuffer(vpp_ctx->hwctx->display, vpp_ctx->filter_buffers[0]);
             if (vas != VA_STATUS_SUCCESS)
                 av_log(avctx, AV_LOG_ERROR, "Failed to unmap filter parameter "
                        "buffer: %d (%s).\n", vas, vaErrorStr(vas));
 
-            params.filters     = &ctx->filter_buffer;
+            params.filters     = &vpp_ctx->filter_buffers[0];
             params.num_filters = 1;
 
             params.forward_references = forward_references;
@@ -501,53 +328,9 @@  static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
             params.num_filters = 0;
         }
 
-        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);
+        err = vaapi_vpp_render_picture(vpp_ctx, &params, output_surface);
+        if (err < 0)
             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 (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
-            AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) {
-            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.
-            }
-        }
 
         err = av_frame_copy_props(output_frame, input_frame);
         if (err < 0)
@@ -573,13 +356,9 @@  static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
 
     return err;
 
-fail_after_begin:
-    vaRenderPicture(ctx->hwctx->display, ctx->va_context, &params_id, 1);
-fail_after_render:
-    vaEndPicture(ctx->hwctx->display, ctx->va_context);
 fail:
     if (filter_params_addr)
-        vaUnmapBuffer(ctx->hwctx->display, ctx->filter_buffer);
+        vaUnmapBuffer(vpp_ctx->hwctx->display, vpp_ctx->filter_buffers[0]);
     av_frame_free(&output_frame);
     return err;
 }
@@ -587,11 +366,18 @@  fail:
 static av_cold int deint_vaapi_init(AVFilterContext *avctx)
 {
     DeintVAAPIContext *ctx = avctx->priv;
+    VAAPIVPPContext *vpp_ctx;
+
+    ctx->vpp_ctx = av_mallocz(sizeof(VAAPIVPPContext));
+    if (!ctx->vpp_ctx)
+        return AVERROR(ENOMEM);
 
-    ctx->va_config     = VA_INVALID_ID;
-    ctx->va_context    = VA_INVALID_ID;
-    ctx->filter_buffer = VA_INVALID_ID;
-    ctx->valid_ids = 1;
+    vpp_ctx = ctx->vpp_ctx;
+
+    vaapi_vpp_ctx_init(vpp_ctx);
+    vpp_ctx->pipeline_uninit     = deint_vaapi_pipeline_uninit;
+    vpp_ctx->build_filter_params = deint_vaapi_build_filter_params;
+    vpp_ctx->output_format = AV_PIX_FMT_NONE;
 
     return 0;
 }
@@ -599,13 +385,9 @@  static av_cold int deint_vaapi_init(AVFilterContext *avctx)
 static av_cold void deint_vaapi_uninit(AVFilterContext *avctx)
 {
     DeintVAAPIContext *ctx = avctx->priv;
+    VAAPIVPPContext *vpp_ctx = ctx->vpp_ctx;
 
-    if (ctx->valid_ids)
-        deint_vaapi_pipeline_uninit(avctx);
-
-    av_buffer_unref(&ctx->input_frames_ref);
-    av_buffer_unref(&ctx->output_frames_ref);
-    av_buffer_unref(&ctx->device_ref);
+    vaapi_vpp_ctx_uninit(avctx, vpp_ctx);
 }
 
 #define OFFSET(x) offsetof(DeintVAAPIContext, x)