[Libav-user] Nvenc_H264 very slow compared to Libx264

Strahinja Radman dr.strashni at gmail.com
Sat Jan 18 12:30:27 EET 2020

>  Thanks! I read through the nvidia docs. It seems that I need to pass in
the "hwaccel: cuvid" option in order to keep the raw frame in GPU memory.
Do you have any idea how to do this in Libav (C)? I tried following this
Stackoverflow post,
but av_hwaccel_next() kept >   returning NULL for a pixel format of
AV_PIX_FMT_YUV420P and a codec of "h264_nvenc." I also see that
av_hwaccel_next is deprecated. Do you have any suggestions?

You first need to create hardware device context, this example will get you
https://github.com/FFmpeg/FFmpeg/blob/master/doc/examples/hw_decode.c .
After this you need to create hardware frames context which you will use to
pass frames inside of the GPU. For decoder get_hw_format should look
something like this

enum AVPixelFormat get_hw_format(AVCodecContext* ctx, const enum
AVPixelFormat* pix_fmts)
    const enum AVPixelFormat* p;
    for (p = pix_fmts; *p != -1; p++) {
     if (*p == this->hw_pix_fmt)

    ctx->hw_frames_ctx = av_buffer_ref(this->hw_frames_ctx);  //This is the
important line
    if (!p) return AV_PIX_FMT_NONE;
     return *p;

While opening the encoder you simply reference the same hw_frames_ctx

     enc_ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ctx);

And that should be it.

Strahinja Radman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20200118/8939e8f9/attachment.html>

More information about the Libav-user mailing list