Quoting Mark Thompson (2016-11-25 00:27:11)
> ---
> Tested somewhat on Skylake GT2 and Polaris 11; on both it comes up with 
> something which looks pretty plausible.
> 
> I'm not exactly a connoisseur of interlaced video, though, so it might be 
> helpful if someone with that particular affectation and a healthy selection 
> of evil sample cases could have a go with it.
> 
> ./avconv -y -vaapi_device /dev/dri/renderD128 -hwaccel vaapi 
> -hwaccel_output_format vaapi -i in.mp4 -an -vf 
> 'deinterlace_vaapi=motion_adaptive' -c:v h264_vaapi -qp 12 out.mp4
> 
> 
>  configure                          |   1 +
>  libavfilter/Makefile               |   1 +
>  libavfilter/allfilters.c           |   1 +
>  libavfilter/version.h              |   2 +-
>  libavfilter/vf_deinterlace_vaapi.c | 602 
> +++++++++++++++++++++++++++++++++++++
>  5 files changed, 606 insertions(+), 1 deletion(-)
>  create mode 100644 libavfilter/vf_deinterlace_vaapi.c
> 
> +static int deint_vaapi_build_filter_params(AVFilterContext *avctx)
> +{
> +    DeintVAAPIContext *ctx = avctx->priv;
> +    VAStatus vas;
> +    VAProcFilterParameterBufferDeinterlacing params;
> +    int i;
> +
> +    ctx->nb_deint_caps = VAProcDeinterlacingCount;
> +    vas = vaQueryVideoProcFilterCaps(ctx->hwctx->display,
> +                                     ctx->va_context,
> +                                     VAProcFilterDeinterlacing,
> +                                     &ctx->deint_caps,
> +                                     &ctx->nb_deint_caps);
> +    if (vas != VA_STATUS_SUCCESS) {
> +        av_log(avctx, AV_LOG_ERROR, "Failed to query deinterlacing "
> +               "caps: %d (%s).\n", vas, vaErrorStr(vas));
> +        return AVERROR(EIO);
> +    }
> +
> +    for (i = 0; i < ctx->nb_deint_caps; i++) {
> +        if (ctx->deint_caps[i].type == ctx->mode)
> +            break;
> +    }
> +    if (i >= ctx->nb_deint_caps) {
> +        av_log(avctx, AV_LOG_ERROR, "Deinterlacing mode %d (%s) is "
> +               "not supported.\n", ctx->mode,
> +               deint_vaapi_mode_name(ctx->mode));
> +    }
> +
> +    params.type      = VAProcFilterDeinterlacing;
> +    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);
> +    }
> +
> +    vas = vaQueryVideoProcPipelineCaps(ctx->hwctx->display,
> +                                       ctx->va_context,
> +                                       &ctx->filter_buffer, 1,
> +                                       &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);
> +    }
> +
> +    ctx->queue_depth = ctx->pipeline_caps.num_backward_references +
> +                       ctx->pipeline_caps.num_forward_references + 1;

I don't see anything that guarantees this is less than MAX_REFERENCES,
and it looks like stuff will break if that's not true.

> +
> +    return 0;
> +}
> +
> +static int deint_vaapi_config_output(AVFilterLink *outlink)
> +{
> +    AVFilterContext    *avctx = outlink->src;
> +    DeintVAAPIContext    *ctx = avctx->priv;
> +    AVVAAPIHWConfig *hwconfig = NULL;
> +    AVHWFramesConstraints *constraints = NULL;
> +    AVVAAPIFramesContext *va_frames;
> +    VAStatus vas;
> +    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;
> +    }
> +
> +    err = deint_vaapi_build_filter_params(avctx);
> +    if (err < 0)
> +        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));
> +        return AVERROR(EIO);
> +    }
> +
> +    outlink->w = ctx->output_width;
> +    outlink->h = ctx->output_height;
> +
> +    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;
> +    AVFrame *output_frame = NULL;
> +    VASurfaceID input_surface, output_surface;
> +    VASurfaceID backward_references[MAX_REFERENCES];
> +    VASurfaceID forward_references[MAX_REFERENCES];
> +    VAProcPipelineParameterBuffer params;
> +    VAProcFilterParameterBufferDeinterlacing *filter_params;
> +    VARectangle input_region;
> +    VABufferID params_id;
> +    VAStatus vas;
> +    void *filter_params_addr = NULL;
> +    int err, i;
> +
> +    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->queue_count < ctx->queue_depth) {
> +        ctx->frame_queue[ctx->queue_count++] = input_frame;
> +        if (ctx->queue_count < ctx->queue_depth) {
> +            // Need more reference surfaces before we can continue.
> +            return 0;
> +        }
> +    } else {
> +        av_frame_free(&ctx->frame_queue[0]);
> +        for (i = 0; i + 1 < ctx->queue_count; i++)
> +            ctx->frame_queue[i] = ctx->frame_queue[i + 1];
> +        ctx->frame_queue[i] = input_frame;
> +    }
> +
> +    input_frame =
> +        ctx->frame_queue[ctx->pipeline_caps.num_backward_references];
> +    input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
> +    for (i = 0; i < ctx->pipeline_caps.num_backward_references; i++)
> +        backward_references[i] = (VASurfaceID)(uintptr_t)
> +            ctx->frame_queue[ctx->pipeline_caps.num_backward_references -
> +                             i - 1]->data[3];
> +    for (i = 0; i < ctx->pipeline_caps.num_forward_references; i++)
> +        forward_references[i] = (VASurfaceID)(uintptr_t)
> +            ctx->frame_queue[ctx->pipeline_caps.num_backward_references +
> +                             i + 1]->data[3];
> +
> +    av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for "
> +           "deinterlace input.\n", input_surface);
> +    av_log(avctx, AV_LOG_DEBUG, "Backward references:");
> +    for (i = 0; i < ctx->pipeline_caps.num_backward_references; i++)
> +        av_log(avctx, AV_LOG_DEBUG, " %#x", backward_references[i]);
> +    av_log(avctx, AV_LOG_DEBUG, "\n");
> +    av_log(avctx, AV_LOG_DEBUG, "Forward  references:");
> +    for (i = 0; i < ctx->pipeline_caps.num_forward_references; i++)
> +        av_log(avctx, AV_LOG_DEBUG, " %#x", forward_references[i]);
> +    av_log(avctx, AV_LOG_DEBUG, "\n");
> +
> +    output_frame = ff_get_video_buffer(outlink, ctx->output_width,
> +                                       ctx->output_height);
> +    if (!output_frame) {
> +        err = AVERROR(ENOMEM);
> +        goto fail;
> +    }
> +
> +    output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
> +    av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for "
> +           "deinterlace output.\n", output_surface);
> +
> +    memset(&params, 0, sizeof(params));
> +
> +    input_region = (VARectangle) {
> +        .x      = 0,
> +        .y      = 0,
> +        .width  = input_frame->width,
> +        .height = input_frame->height,
> +    };
> +
> +    params.surface = input_surface;
> +    params.surface_region = &input_region;
> +    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   = VA_FRAME_PICTURE;
> +
> +    vas = vaMapBuffer(ctx->hwctx->display, ctx->filter_buffer,
> +                      &filter_params_addr);
> +    if (vas != VA_STATUS_SUCCESS) {
> +        av_log(avctx, AV_LOG_ERROR, "Failed to map filter parameter "
> +               "buffer: %d (%s).\n", vas, vaErrorStr(vas));
> +        err = AVERROR(EIO);
> +        goto fail;
> +    }
> +    filter_params = filter_params_addr;
> +    filter_params->flags = 0;
> +    if (input_frame->interlaced_frame && !input_frame->top_field_first)
> +        filter_params->flags |= VA_DEINTERLACING_BOTTOM_FIELD_FIRST;
> +    filter_params_addr = NULL;
> +    vas = vaUnmapBuffer(ctx->hwctx->display, ctx->filter_buffer);
> +    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.num_filters = 1;
> +
> +    params.forward_references = forward_references;
> +    params.num_forward_references =
> +        ctx->pipeline_caps.num_forward_references;
> +    params.backward_references = backward_references;
> +    params.num_backward_references =
> +        ctx->pipeline_caps.num_backward_references;
> +
> +    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) {
> +        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);

This needs an error check.

> +
> +    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);
> +
> +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);
> +    av_frame_free(&output_frame);
> +    return err;
> +}
> +
> +static av_cold int deint_vaapi_init(AVFilterContext *avctx)
> +{
> +    DeintVAAPIContext *ctx = avctx->priv;
> +
> +    ctx->va_config     = VA_INVALID_ID;
> +    ctx->va_context    = VA_INVALID_ID;
> +    ctx->filter_buffer = VA_INVALID_ID;
> +
> +    return 0;
> +}
> +
> +static av_cold void deint_vaapi_uninit(AVFilterContext *avctx)
> +{
> +    DeintVAAPIContext *ctx = avctx->priv;
> +
> +    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);
> +}
> +
> +#define OFFSET(x) offsetof(DeintVAAPIContext, x)
> +#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM)
> +static const AVOption deint_vaapi_options[] = {
> +    { "mode", "Deinterlacing mode",
> +      OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = VAProcDeinterlacingBob },

We might want to use one of the fancier algorithms by default. Also,
docs would be nice.

-- 
Anton Khirnov
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to