[Libav-user] vf_scale_npp filter api proper usage and hw init.

Paulo Narváez pnarvaez at 3se.cl
Tue Aug 25 21:12:24 EEST 2020


Hello everyone,
I'm currently on a quest to use hardware acceleration (specifically CUDA)
every where I can on my player.
So far, I'm able to decode using cuvid decoders and it's working great, but
it fails when trying scale_npp filter (scale filter works fine).
So for I narrowed down to nppscale_filter_frame processing an empty
hw_frame_ctx form the passed AVFilterLink, so I know I'm lacking a proper
hardware initialization for this filter, my problem is that I don't know
where and how.
I'm copying my code below (filter initialization and usage) in case anyone
is able to give me a hand.
Thanks.

##filter initialization.

char args [512];

    int ret = 0;

    const AVFilter *bufferSource = avfilter_get_by_name("buffer");

    const AVFilter *bufferSink = avfilter_get_by_name("buffersink");

    AVFilterInOut *outputs=avfilter_inout_alloc();

    AVFilterInOut *inputs=avfilter_inout_alloc();

    AVRational time_base = m_ffmpeg->videoStream->time_base;

    enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };


    avFilterGraph = avfilter_graph_alloc();


    snprintf(args,

             sizeof (args),

             "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",

             m_CodecCtx->width,

             m_CodecCtx->height,

             AV_PIX_FMT_NV12,

             time_base.num, time_base.den,

             m_CodecCtx->sample_aspect_ratio.num,

             m_CodecCtx->sample_aspect_ratio.den);

    ret = avfilter_graph_create_filter(&sourceContext, bufferSource,

                                   "in", args, NULL, avFilterGraph);

    if(ret < 0){

        Error("avfilter:\tCannot create buffer source");

        avfilter_inout_free(&outputs);

        avfilter_inout_free(&inputs);

        return false;

    }

    ret = avfilter_graph_create_filter(&sinkContext, bufferSink,

                                       "out", NULL, NULL, avFilterGraph);

    if(ret < 0){

        Error("avfilter:\tCannot create buffer sink");

        avfilter_inout_free(&outputs);

        avfilter_inout_free(&inputs);

        return false;

    }


    ret = av_opt_set_int_list(sinkContext, "pix_fmts", pix_fmts,

                              AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);

    if(ret < 0){

        Error("avfilter:\tCannot set output pixel format");

        avfilter_inout_free(&outputs);

        avfilter_inout_free(&inputs);

        return false;

    }


    outputs->name = av_strdup("in");

    outputs->filter_ctx = sourceContext;

    outputs->pad_idx = 0;

    outputs->next = NULL;

    inputs->name = av_strdup("out");

    inputs->filter_ctx = sinkContext;

    inputs->pad_idx = 0;

    inputs->next = NULL;


    ret = avfilter_graph_parse_ptr(avFilterGraph, filters,

                                   &inputs, &outputs, NULL);

    if(ret < 0){

        Error("avfilter:\tCannot parse filter graph");

        avfilter_inout_free(&outputs);

        avfilter_inout_free(&inputs);

        return false;

    }


    ret = avfilter_graph_config(avFilterGraph, NULL) < 0;

    if(ret < 0){

        Error("avfilter:\tCannot config filter graph");

        avfilter_inout_free(&outputs);

        avfilter_inout_free(&inputs);

        return false;

    }


    avfilter_inout_free(&outputs);

    avfilter_inout_free(&inputs);

    return true;


##apply filter to frame.

        if(m_frameBack->format==AV_PIX_FMT_CUDA){

            if(av_hwframe_transfer_data(m_frameSW,m_frameBack,0)<0){

                Error("Error downloading frame from gpu to ram");

            }

        }

        m_frameSW->pts = m_frameSW->best_effort_timestamp;

        if(av_buffersrc_add_frame_flags(sourceContext,m_frameSW,AV_BUFFERSRC_FLAG_KEEP_REF)
< 0){

            Error("avfilter:\tError feeding filtergraph");

        }

        if(av_buffersink_get_frame(sinkContext, m_frameFront) < 0){

            Error("avfilter:\tError getting frame from filtergraph");

        }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20200825/934feefb/attachment.html>


More information about the Libav-user mailing list