nvenc - 未找到支持 NVENC 的设备

标签 nvenc

我遇到了一个奇怪的问题。我一直使用FFmpeg的NVENC来编码视频。奇怪的是,我可以顺利地使用h264_nvenc,没有问题,但是当我用hevc_nvenc替换h264_nvenc时,我得到了问题“No NVENCcapable devicesfound”我使用的FFmpeg版本是3.2,我使用命令行使用hevc_nvenc进行编码,工作正常。我的代码在这里:

#include "stdafx.h"


int flush_encoder(AVFormatContext *fmt_ctx, unsigned int stream_index)
{
int ret;
int got_frame;
AVPacket enc_pkt;
if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &
    CODEC_CAP_DELAY))
    return 0;
while (1) {
    printf("Flushing stream #%u encoder\n", stream_index);
    //ret = encode_write_frame(NULL, stream_index, &got_frame);
    enc_pkt.data = NULL;
    enc_pkt.size = 0;
    av_init_packet(&enc_pkt);
    ret = avcodec_encode_video2(fmt_ctx->streams[stream_index]->codec, &enc_pkt,
        NULL, &got_frame);
    av_frame_free(NULL);
    if (ret < 0)
        break;
    if (!got_frame){
        ret = 0;
        break;
    }
    printf("Succeed to encode 1 frame! 编码成功1帧!\n");
    /* mux encoded frame */
    ret = av_write_frame(fmt_ctx, &enc_pkt);
    if (ret < 0)
        break;
}
return ret;
}

int main(int argc, char* argv[])
{
AVFormatContext* pFormatCtx;
AVOutputFormat* fmt;
AVStream* video_st;
AVCodecContext* pCodecCtx;
AVCodec* pCodec;

uint8_t* picture_buf;
AVFrame* picture;
int size;

FILE *in_file = fopen("test_yuv420p_320x180.yuv", "rb");    //Input YUV data 视频YUV源文件 
int in_w = 320, in_h = 180;//宽高 
int framenum = 100;
const char* out_file = "ds.hevc";

av_register_all();
//Method1 方法1.组合使用几个函数
pFormatCtx = avformat_alloc_context();
//Guess Format 猜格式
fmt = av_guess_format(NULL, out_file, NULL);
pFormatCtx->oformat = fmt;

//Method 2 方法2.更加自动化一些
//avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);
//fmt = pFormatCtx->oformat;


//Output Format 注意输出路径
if (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0)
{
    printf("Failed to open output file! 输出文件打开失败");
    return -1;
}

video_st = avformat_new_stream(pFormatCtx, 0);
video_st->time_base.num = 1;
video_st->time_base.den = 25;

if (video_st == NULL)
{
    return -1;
}
//Param that must set
pCodecCtx = video_st->codec;
pCodecCtx->codec_id =AV_CODEC_ID_HEVC;
//pCodecCtx->codec_id = fmt->video_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
pCodecCtx->width = in_w;
pCodecCtx->height = in_h;
pCodecCtx->time_base.num = 1;
pCodecCtx->time_base.den = 25;
pCodecCtx->bit_rate = 400000;
pCodecCtx->gop_size = 12;
//H264
//pCodecCtx->me_range = 16;
//pCodecCtx->max_qdiff = 4;
//pCodecCtx->qcompress = 0.6;
pCodecCtx->qmin = 10;
pCodecCtx->qmax = 51;

//Optional Param
pCodecCtx->max_b_frames = 3;

// Set Option
AVDictionary *param = 0;
//H.264
if (pCodecCtx->codec_id == AV_CODEC_ID_H264) {
    av_dict_set(&param, "preset", "slow", 0);
    av_dict_set(&param, "tune", "zerolatency", 0);
}
//H.265
if (pCodecCtx->codec_id == AV_CODEC_ID_H265){
    av_dict_set(&param, "x265-params", "qp=20", 0);
    av_dict_set(&param, "preset", "default", 0);
    av_dict_set(&param, "tune", "zero-latency", 0);
}

//Dump Information 输出格式信息
av_dump_format(pFormatCtx, 0, out_file, 1);

//pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
pCodec = avcodec_find_encoder_by_name("hevc_nvenc");
if (!pCodec){
    printf("Can not find encoder! 没有找到合适的编码器!\n");
    return -1;
}
if (avcodec_open2(pCodecCtx, pCodec, &param) < 0){
    printf("Failed to open encoder! 编码器打开失败!\n");
    return -1;
}

picture = av_frame_alloc();
size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
picture_buf = (uint8_t *)av_malloc(size);
avpicture_fill((AVPicture *)picture, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);

//Write File Header 写文件头
avformat_write_header(pFormatCtx, NULL);

AVPacket pkt;
int y_size = pCodecCtx->width * pCodecCtx->height;
av_new_packet(&pkt, y_size * 3);

for (int i = 0; i<framenum; i++){
    //Read YUV 读入YUV
    if (fread(picture_buf, 1, y_size * 3 / 2, in_file) < 0){
        printf("Failed to read YUV data! 文件读取错误\n");
        return -1;
    }
    else if (feof(in_file)){
        break;
    }
    picture->data[0] = picture_buf;  // 亮度Y
    picture->data[1] = picture_buf + y_size;  // U 
    picture->data[2] = picture_buf + y_size * 5 / 4; // V
    //PTS
    picture->pts = i;
    picture->format = pCodecCtx->pix_fmt;
    picture->width = in_w;
    picture->height = in_h;
    int got_picture = 0;
    //Encode 编码
    int ret = avcodec_encode_video2(pCodecCtx, &pkt, picture, &got_picture);
    if (ret < 0){
        printf("Failed to encode! 编码错误!\n");
        return -1;
    }
    if (got_picture == 1){
        printf("Succeed to encode 1 frame! 编码成功1帧!\n");
        pkt.stream_index = video_st->index;
        ret = av_write_frame(pFormatCtx, &pkt);
        av_free_packet(&pkt);
    }
}
//Flush Encoder
int ret = flush_encoder(pFormatCtx, 0);
if (ret < 0) {
    printf("Flushing encoder failed\n");
    return -1;
}

//Write file trailer 写文件尾
av_write_trailer(pFormatCtx);

//Clean 清理
if (video_st){
    avcodec_close(video_st->codec);
    av_free(picture);
    av_free(picture_buf);
}
avio_close(pFormatCtx->pb);
avformat_free_context(pFormatCtx);

fclose(in_file);

system("pause");
return 0;
}

救命!!!

最佳答案

经过几天的挣扎,我再次尝试自己回答这个问题。关键是,当使用hevc_nvenc编码时,必须设置pCodecCtx->max_b_frames = 0;(至少对于ffmpeg 3.2 版本)。

关于nvenc - 未找到支持 NVENC 的设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43244894/

相关文章:

某些三星机型不支持 ffmpeg 1080p50 50fps,导致错误

video - 如何使用带有 Nvidia Acceleration (Nvenc) 的 FFMPEG 对字幕进行硬编码/刻录

ffmpeg - 手动生成 "empty"h264 p-frame

ffmpeg - 从 mxf 到 mp4 的 Nvidia Nvenc 视频转换在内部的多个流中出现错误

与 libx264 相比,gpu 上的 Ffmpeg nvenc 编码器不会像 libx264 那样压缩文件

FFMPEG hevc_nvenc "No NVENC capable devices found"与 NVidia GTX950M

使用 NVENC 时 FFMpeg 质量下降

c++ - FFmpeg + cpp,如何获取处理 h264_nvenc 的 GPU 列表?

c++ - 使用随机访问解码 H264