我正在尝试使用 FFmpeg 中的硬件加速来解码 h264 视频,特别是针对 iOS 设备并利用 VideoToolbox 硬件设备。虽然我已经成功生成了解码器上下文,但在创建硬件设备上下文和设置缓冲区引用时遇到了困难。我是 ffmpeg 新手,正在寻求解决问题的帮助。相关代码片段在这里:
class Decoder_Setup {
private:
AVCodecContext *decoder_ctx = NULL;
const AVCodec *decoder = NULL;
enum AVHWDeviceType type;
AVBufferRef *buffer_ref = NULL;
AVPixelFormat pix_fmt;
AVCodecHWConfig *hwConfig = NULL;
public:
int set();
};
int Decoder_Setup::set() {
type = AV_HWDEVICE_TYPE_VIDEOTOOLBOX;
pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX;
decoder = avcodec_find_decoder_by_name("h264");
decoder_ctx = avcodec_alloc_context3(decoder);
int res = av_hwdevice_ctx_create(&buffer_ref, type, NULL, NULL, 0);
decoder_ctx->hw_device_ctx = buffer_ref;
// decoder_ctx->get_format = (AVPixelFormat (*)(AVCodecContext *, const AVPixelFormat *)) AV_PIX_FMT_VIDEOTOOLBOX;
avcodec_open2(decoder_ctx, decoder, NULL);
return res;
}
预先感谢您的协助!
你的代码看起来不错。尝试使用
avcodec_find_decoder_by_name("h264_videotoolbox");
查找硬件解码器。还要确保您的 ffmpeg 库使用相应的标志构建以支持硬件加速,例如 --enable-decoder=h264_videotoolbox
和 --enable-hwaccel=h264_videotoolbox
。
这里是为Android构建ffmpeg的脚本示例。我认为您可以通过在 scripts/ffmpeg/build.sh 文件中设置标志来自定义它,以便它针对 iOS 进行构建。