[FFmpeg-devel] 971108 - ffmpeg doesn't use GPU

hamidi hamidi at gmail.com
Mon Jan 28 15:13:49 EET 2019


I got the latest version binaries of ffmpeg from here
<https://ffmpeg.zeranoe.com/builds/>. When I examine CPU and GPU usages
when I play a video by its ffplay, I see that GPU is used during play. Not
much using of CPU also indicates it. But when I get the latest version
sources from the original site, I can't use GPU. To clarify, I include a
player test program I wrote until now. When I uncomment the line which
includes avcodec_find_decoder_by_name("h264_cuvid"), I get error -1. The
error happens in avcodec_open2 with the description of Operation not
permitted.

CString format(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    char buf[512];
    vsnprintf(buf, sizeof(buf), fmt, ap);
    va_end(ap);
    return buf;
}

int CplayerDlg::play()
{
    FILE *fp = fopen("video_files/1010.brf", "rb");
    if (!fp)
    {
        AfxMessageBox("can't open video file");
        return -1;
    }
    RecordFrame frame;
    RecordHeader hdr;
    fread(&frame, sizeof(frame), 1, fp);
    if (frame.frameType != FRAME_TYPE_HEADER)
    {
        AfxMessageBox("record file doesn't begin with header");
        return -1;
    }
    fread(&hdr, sizeof(hdr), 1, fp);
    GetDlgItem(IDC_DIM)->SetWindowText(format("%dx%d", hdr.width,
hdr.height));
    GetDlgItem(IDC_CODEC_ID)->SetWindowText(format("%d", hdr.codecId));
    GetDlgItem(IDC_PIXEL_FORMAT)->SetWindowText(format("%d",
hdr.pixelFormat));
    GetDlgItem(IDC_TIMEBASE)->SetWindowText(format("%d/%d",
hdr.timebaseNum, hdr.timebaseDen));
    AVCodec *pCodec;

#if 0
#define CHECK(decoder)\
    pCodec = avcodec_find_decoder_by_name(#decoder);\
    AfxMessageBox(pCodec ? #decoder " found" : "can't find " #decoder);

    CHECK(h264_cuvid);

#undef CHECK
#endif

    pCodec = avcodec_find_decoder(AV_CODEC_ID_H264);
    //pCodec = avcodec_find_decoder_by_name("h264_cuvid");
    if (!pCodec)
    {
        AfxMessageBox("can't find h264 decoder");
        return -1;
    }

    AVCodecContext *pCodecContext = avcodec_alloc_context3(pCodec);
    if (!pCodecContext)
    {
        AfxMessageBox("can't allocate codec context");
        return -1;
    }

#if 0
// enumerating available codecs
    //av_register_all();
    avcodec_register_all();

    AVCodec *current_codec = av_codec_next(NULL);
    while (current_codec != NULL)
    {
        TRACE("%s\n", current_codec->name);
        current_codec = av_codec_next(current_codec);
    }
#endif

    int err = avcodec_open2(pCodecContext, pCodec, NULL);
    if (err != 0)
    {
        char buf[AV_ERROR_MAX_STRING_SIZE];
        av_make_error_string(buf, AV_ERROR_MAX_STRING_SIZE, err);
        char buf2[AV_ERROR_MAX_STRING_SIZE];
        sprintf(buf2, "%d (%x): %s\n", err, err, buf);
        AfxMessageBox(buf2);
        return -1;
    }
    AfxMessageBox("operation completed successfully");
    fclose(fp);
    return 0;
}


More information about the ffmpeg-devel mailing list