转载:https://www.pudn.com/news/62bc096d405aad31f717648e.html
ref:
该库希望支持能在一份代码中支持CUDA GPU和CPU模式的切换,也可以选择是否只解码关键帧。主要设计思想如下:
bool support_hwdevice()
{AVHWDeviceType type;type = av_hwdevice_find_type_by_name(s_hwdevice_name);if (type == AV_HWDEVICE_TYPE_NONE){fprintf(stderr, "Device type %s is not supported.\n", s_hwdevice_name);fprintf(stderr, "Available device types:");while ((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)fprintf(stderr, " %s", av_hwdevice_get_type_name(type));fprintf(stderr, "\n");return false;}return true;
}
该方法对有显卡,但不支持硬解加速的机器不适用,比如部分笔记本。
此时该函数也会返回true,但是解码时候会报 Hardware is lacking required capabilities这样的错误。
init_ctx初始化函数主要是对输入的input_ctx和用于解码的decoder_ctx初始化。

说明:
深色框为硬件解码与软解解码不一样的地方。
av_hwdevice_find_type_by_name()的功能是根据名称查找对应的AVHWDeviceType。
AVHWDeviceType表示硬件加速API的类型,比如AV_HWDEVICE_TYPE_CUDA是nvidia提供的加速API.
av_hwdevice_find_type_by_name支持的名称如下所示。
static const char *const hw_type_names[] = {[AV_HWDEVICE_TYPE_CUDA] = "cuda",[AV_HWDEVICE_TYPE_DRM] = "drm",[AV_HWDEVICE_TYPE_DXVA2] = "dxva2",[AV_HWDEVICE_TYPE_D3D11VA] = "d3d11va",[AV_HWDEVICE_TYPE_OPENCL] = "opencl",[AV_HWDEVICE_TYPE_QSV] = "qsv",[AV_HWDEVICE_TYPE_VAAPI] = "vaapi",[AV_HWDEVICE_TYPE_VDPAU] = "vdpau",[AV_HWDEVICE_TYPE_VIDEOTOOLBOX] = "videotoolbox",[AV_HWDEVICE_TYPE_MEDIACODEC] = "mediacodec",
};
decoder_ctx->get_format = get_hw_format ,get_hw_format是向AVCodecContext注册的一个函数,用于协商支持的像素格式。
// ref:https://github.com/FFmpeg/FFmpeg/blob/master/doc/examples/hw_decode.c
// ref: https://github.com/chinahbcq/ffmpeg_hw_decode
// ref: https://www.jianshu.com/p/3ea9ef713211