ffmpeg代码分析--h264的GPU加速

硬件选择

-hwaccel_device 0

{ "hwaccel_device",   OPT_VIDEO | OPT_STRING | HAS_ARG |OPT_EXPERT |

                          OPT_SPEC |OPT_INPUT,                                 { .off = OFFSET(hwaccel_devices) },

       "select a device for HW acceleration", "devicename"}

libavutil/hwcontex_cuda.c

cuda_device_create

可以看到使用的CUDA的函数,如cuda_device_init,cuDeviceGet等。

static intcuda_device_create(AVHWDeviceContext *ctx, const char *device,

                              AVDictionary*opts, int flags)

{

   AVCUDADeviceContext *hwctx = ctx->hwctx;

    CudaFunctions*cu;

   CUdevice cu_device;

   CUcontext dummy;

   CUresult err;

   int device_idx = 0;

   if (device)

       device_idx = strtol(device, NULL, 0);

   if (cuda_device_init(ctx) < 0)

       goto error;

   cu = hwctx->internal->cuda_dl;

   err = cu->cuInit(0);

   if (err != CUDA_SUCCESS) {

       av_log(ctx, AV_LOG_ERROR, "Could not initialize the CUDA driverAPI\n");

       goto error;

    }

    err = cu->cuDeviceGet(&cu_device, device_idx);

    if(err != CUDA_SUCCESS) {

       av_log(ctx, AV_LOG_ERROR, "Could not get the device number%d\n", device_idx);

       goto error;

    }

    err = cu->cuCtxCreate(&hwctx->cuda_ctx,CU_CTX_SCHED_BLOCKING_SYNC, cu_device);

   if (err != CUDA_SUCCESS) {

        av_log(ctx, AV_LOG_ERROR, "Errorcreating a CUDA context\n");

       goto error;

    }

    cu->cuCtxPopCurrent(&dummy);

   hwctx->internal->is_allocated = 1;

   return 0;

 error:

   cuda_device_uninit(ctx);

   return AVERROR_UNKNOWN;

}

加速解码

-hwaccel cuvid  -c:v h264_cuvid

    {"hwaccel",          OPT_VIDEO |OPT_STRING | HAS_ARG | OPT_EXPERT |

                          OPT_SPEC |OPT_INPUT,                                  { .off = OFFSET(hwaccels) },

       "use HW accelerated decoding", "hwaccel name" },

const HWAccel hwaccels[] = {

 ...

#if CONFIG_CUVID

    {"cuvid", cuvid_init, HWACCEL_CUVID, AV_PIX_FMT_CUDA,

     AV_HWDEVICE_TYPE_NONE },

#endif

    {0 },

};

 

加速编码

-c:v  h264_nvenc

libavcodec/nvenc_h264.c

AVCodec ff_h264_nvenc_encoder = {

   .name           ="h264_nvenc",

   .long_name      =NULL_IF_CONFIG_SMALL("NVIDIA NVENC H.264 encoder"),

   .type           =AVMEDIA_TYPE_VIDEO,

   .id             =AV_CODEC_ID_H264,

   .init           =ff_nvenc_encode_init,

   .send_frame     =ff_nvenc_send_frame,

   .receive_packet = ff_nvenc_receive_packet,

   .encode2        = ff_nvenc_encode_frame,

   .close          =ff_nvenc_encode_close,

   .priv_data_size = sizeof(NvencContext),

   .priv_class     =&h264_nvenc_class,

   .defaults       = defaults,

   .capabilities   =AV_CODEC_CAP_DELAY,

   .caps_internal  =FF_CODEC_CAP_INIT_CLEANUP,

   .pix_fmts       =ff_nvenc_pix_fmts,

};

libavcodec/nvenc.c

例:ff_nvenc_encode_frame

可以看到库函数nvEncEncodePicture。
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山西茄子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值