如何使用ffmpeg解码具有硬件加速的文件?
我写过一个使用ffmpeg的工作视频播放器。
我使用"av_hwaccel_next"
检查了支持情况,发现mpeg2_dxva
。
然而,当我加载mpeg2文件(像往常一样)时,我没有得到任何硬件加速。 AVCodecContext->hwaccel
和AVCodecContext->hwaccelcontext
均为空。
我是否必须在某处传递一些标志以启用hw-acceleration?
我无法找到任何关于此的信息,任何人都知道一个好的来源?
答案 0 :(得分:23)
从阅读ffmpeg文档开始:https://trac.ffmpeg.org/wiki/HWAccelIntro并更好地回答How to use hardware acceleration with ffmpeg(对于linux检查页面https://wiki.archlinux.org/index.php/Hardware_video_acceleration)
当使用FFmpeg工具时,通过
-hwaccel
选项启用HW辅助解码,该选项启用特定解码器。每个解码器可能具有特定限制(例如,H.264解码器可能仅支持基线简档)。通过使用特定编码器(例如h264_nvenc)启用HW辅助编码。仅在几个过滤器中支持过滤硬件辅助处理。 有一些硬件加速标准API,FFmpeg在某种程度上支持其中一些。
hwaccel激活由代码控制(在2013 https://github.com/FFmpeg/FFmpeg/commit/08303d774132775d49d4ba767092de5d426f089d之后重新格式化)
avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
例如,在libavcodec / mpeg12dec.c https://github.com/FFmpeg/FFmpeg/blob/6c7254722ad43712db5686feee8bf75c74d8635b/libavcodec/mpeg12dec.c
中avctx->pix_fmt = mpeg_get_pixelformat(avctx);
avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
ff_find_hwaccel
检查编解码器和pixelformat图像对以及所有可用的hwaccelerators。
AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
{
AVHWAccel *hwaccel=NULL;
while((hwaccel= av_hwaccel_next(hwaccel))){
if ( hwaccel->id == codec_id
&& hwaccel->pix_fmt == pix_fmt)
return hwaccel;
}
return NULL;
}
例如,dxva2(https://en.wikipedia.org/wiki/DirectX_Video_Acceleration)具有:
AVHWAccel mpeg2_dxva2_hwaccel = {
.name = "mpeg2_dxva2",
.type = AVMEDIA_TYPE_VIDEO,
.id = CODEC_ID_MPEG2VIDEO,
.pix_fmt = PIX_FMT_DXVA2_VLD,
.capabilities = 0,
.start_frame = start_frame,
.decode_slice = decode_slice,
.end_frame = end_frame,
.priv_data_size = sizeof(struct dxva2_picture_context),
};
libavutil/pixfmt.h
按像素格式列出所有支持的hw解码器/加速器https://ffmpeg.org/doxygen/3.2/pixfmt_8h.html
AV_PIX_FMT_XVMC_MPEG2_MC - XVideo Motion Acceleration via common packet passing.
AV_PIX_FMT_XVMC_MPEG2_IDCT - undocumented
AV_PIX_FMT_XVMC - undocumented
AV_PIX_FMT_VDPAU_H264 - H.264 HW decoding with VDPAU, data[0] contains a vdpau_render_state struct which contains the bitstream of the slices as well as various fields extracted from headers.
AV_PIX_FMT_VDPAU_MPEG1 - MPEG-1 HW decoding with VDPAU, data[0] contains a vdpau_render_state struct which contains the bitstream of the slices as well as various fields extracted from headers.
AV_PIX_FMT_VDPAU_MPEG2 - MPEG-2 HW decoding with VDPAU, data[0] contains a vdpau_render_state struct which contains the bitstream of the slices as well as various fields extracted from headers.
AV_PIX_FMT_VDPAU_WMV3 - WMV3 HW decoding with VDPAU, data[0] contains a vdpau_render_state struct which contains the bitstream of the slices as well as various fields extracted from headers.
AV_PIX_FMT_VDPAU_VC1 - VC-1 HW decoding with VDPAU, data[0] contains a vdpau_render_state struct which contains the bitstream of the slices as well as various fields extracted from headers.
AV_PIX_FMT_VAAPI_MOCO - HW acceleration through VA API at motion compensation entry-point, Picture.data[3] contains a vaapi_render_state struct which contains macroblocks as well as various fields extracted from headers.
AV_PIX_FMT_VAAPI_IDCT - HW acceleration through VA API at IDCT entry-point, Picture.data[3] contains a vaapi_render_state struct which contains fields extracted from headers.
AV_PIX_FMT_VAAPI_VLD - HW decoding through VA API, Picture.data[3] contains a VASurfaceID.
AV_PIX_FMT_VDPAU_MPEG4 - MPEG-4 HW decoding with VDPAU, data[0] contains a vdpau_render_state struct which contains the bitstream of the slices as well as various fields extracted from headers.
AV_PIX_FMT_DXVA2_VLD - HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer.
AV_PIX_FMT_VDPAU - HW acceleration through VDPAU, Picture.data[3] contains a VdpVideoSurface.
AV_PIX_FMT_VDA - HW acceleration through VDA, data[3] contains a CVPixelBufferRef.
AV_PIX_FMT_QSV - HW acceleration through QSV, data[3] contains a pointer to the mfxFrameSurface1 structure.
AV_PIX_FMT_MMAL - HW acceleration though MMAL, data[3] contains a pointer to the MMAL_BUFFER_HEADER_T structure.
AV_PIX_FMT_D3D11VA_VLD - HW decoding through Direct3D11, Picture.data[3] contains a ID3D11VideoDecoderOutputView pointer.
AV_PIX_FMT_CUDA - HW acceleration through CUDA. data[i] contain CUdeviceptr pointers exactly as for system memory frames.
实际选择的像素格式是在ff_find_hwaccel
之前调用的函数,static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
为mpeg1 / 2的libavcodec/mpeg12dec.c
。在早期版本的ffmpeg / libavcodec中,它检查avctx->xvmc_acceleration
和/或avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU
或调用avctx->get_format(avctx,ff_hwaccel_pixfmt_list_420);
以在某些情况下启用hw解码。
在最近的版本(2017)中,它和附近的几个函数选择了hw编码器https://github.com/FFmpeg/FFmpeg/blob/aff8cf18cb0b1fa4f2e3d163c3da2f25aa6d1906/libavcodec/mpeg12dec.c#L1189。
基本上:应该启用硬件解码器及其api(过时的XVMC,VDPAU,VA API,MS DXVA或MS Direct3D11或videotoolbox)在你的ffmpeg和supported by your hardware及其驱动程序/库的构建中(许多是专有的,应该单独下载)。有时应该为ffmpeg或插件加载-hwaccel
选项。在linux中,您可以使用vainfo
和vdpauinfo
命令来测试最流行的标准视频hw解码API的可用性和支持的配置文件。
输入文件(对于mpeg1 / 2)应该不是灰度,它应该有s->chroma_format
少于2(4:2:0 Chroma subsampling,这通常用于ISO / IEC MPEG和ITU- T VCEG H.26x;但不适用于某些MPEG-4第2部分,而不适用于H.264 / MPEG-4 AVC的高4:4:4变体。
static const enum AVPixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
#if CONFIG_MPEG2_XVMC_HWACCEL
AV_PIX_FMT_XVMC,
#endif
#if CONFIG_MPEG_VDPAU_DECODER && FF_API_VDPAU
AV_PIX_FMT_VDPAU_MPEG2,
#endif
#if CONFIG_MPEG2_VDPAU_HWACCEL
AV_PIX_FMT_VDPAU,
#endif
#if CONFIG_MPEG2_DXVA2_HWACCEL
AV_PIX_FMT_DXVA2_VLD,
#endif
#if CONFIG_MPEG2_D3D11VA_HWACCEL
AV_PIX_FMT_D3D11VA_VLD,
#endif
#if CONFIG_MPEG2_VAAPI_HWACCEL
AV_PIX_FMT_VAAPI,
#endif
#if CONFIG_MPEG2_VIDEOTOOLBOX_HWACCEL
AV_PIX_FMT_VIDEOTOOLBOX,
#endif
AV_PIX_FMT_YUV420P,
AV_PIX_FMT_NONE
};
static const enum AVPixelFormat mpeg12_pixfmt_list_422[] = {
AV_PIX_FMT_YUV422P,
AV_PIX_FMT_NONE
};
static const enum AVPixelFormat mpeg12_pixfmt_list_444[] = {
AV_PIX_FMT_YUV444P,
AV_PIX_FMT_NONE
};
#if FF_API_VDPAU
static inline int uses_vdpau(AVCodecContext *avctx) {
return avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG2;
}
#endif
static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
{
Mpeg1Context *s1 = avctx->priv_data;
MpegEncContext *s = &s1->mpeg_enc_ctx;
const enum AVPixelFormat *pix_fmts;
if (CONFIG_GRAY && (avctx->flags & AV_CODEC_FLAG_GRAY))
return AV_PIX_FMT_GRAY8;
if (s->chroma_format < 2)
pix_fmts = avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO ?
mpeg1_hwaccel_pixfmt_list_420 :
mpeg2_hwaccel_pixfmt_list_420;
else if (s->chroma_format == 2)
pix_fmts = mpeg12_pixfmt_list_422;
else
pix_fmts = mpeg12_pixfmt_list_444;
return ff_thread_get_format(avctx, pix_fmts);
}
static void setup_hwaccel_for_pixfmt(AVCodecContext *avctx)
{
// until then pix_fmt may be changed right after codec init
if (avctx->hwaccel
#if FF_API_VDPAU
|| uses_vdpau(avctx)
#endif
)
if (avctx->idct_algo == FF_IDCT_AUTO)
avctx->idct_algo = FF_IDCT_SIMPLE;
if (avctx->hwaccel && avctx->pix_fmt == AV_PIX_FMT_XVMC) {
Mpeg1Context *s1 = avctx->priv_data;
MpegEncContext *s = &s1->mpeg_enc_ctx;
s->pack_pblocks = 1;
#if FF_API_XVMC
FF_DISABLE_DEPRECATION_WARNINGS
avctx->xvmc_acceleration = 2;
FF_ENABLE_DEPRECATION_WARNINGS
#endif /* FF_API_XVMC */
}
}