How do I decode a file with hardware acceleration with ffmpeg?
I have written a working video player that uses ffmpeg.
I have checked for support using "av_hwaccel_next" and found mpeg2_dxva.
However, when I load an mpeg2 file (as usual) I do not get any hardware acceleration. AVCodecContext->hwaccel and AVCodecContext->hwaccelcontext are both null.
Do I have to pass some flag somewhere in order to enable hw-acceleration?
I haven’t been able to find any information regarding this, anyone know of a good source?
Start from reading ffmpeg documentation: https://trac.ffmpeg.org/wiki/HWAccelIntro and better answer How to use hardware acceleration with ffmpeg (and for linux check page https://wiki.archlinux.org/index.php/Hardware_video_acceleration)
hwaccel activation was controlled by code like (bit reformatted after 2013 https://github.com/FFmpeg/FFmpeg/commit/08303d774132775d49d4ba767092de5d426f089d)
For example, in libavcodec/mpeg12dec.c https://github.com/FFmpeg/FFmpeg/blob/6c7254722ad43712db5686feee8bf75c74d8635b/libavcodec/mpeg12dec.c
The
ff_find_hwaccelchecks codec and pixelformat pair of image and of all available hwaccelerators.For example, dxva2 (https://en.wikipedia.org/wiki/DirectX_Video_Acceleration) has:
And
libavutil/pixfmt.hlists all supported hw decoders / accelerators by their pixel formats https://ffmpeg.org/doxygen/3.2/pixfmt_8h.htmlActual selection of pixel formats is in function called before
ff_find_hwaccel, thestatic enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)oflibavcodec/mpeg12dec.cfor mpeg1/2. In earlier versions of ffmpeg/libavcodec it checksavctx->xvmc_accelerationand/oravctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAUor callsavctx->get_format(avctx,ff_hwaccel_pixfmt_list_420);to enable hw decoding in some cases.In recent version (2017) it and several nearby function do the selection of hw coder https://github.com/FFmpeg/FFmpeg/blob/aff8cf18cb0b1fa4f2e3d163c3da2f25aa6d1906/libavcodec/mpeg12dec.c#L1189.
Basically: hardware decoder and its api (obsolete XVMC, VDPAU, VA API, MS DXVA or MS Direct3D11, or videotoolbox) should be enabled in your build of ffmpeg and supported by your hardware and its driver/libraries (many are proprietary and should be downloaded separately). Sometimes
-hwacceloption should be given to ffmpeg, or plugin loaded. In linux you may usevainfoandvdpauinfocommands to test availability and supported profiles with most popular standard video hw decoding APIs.Input file (for mpeg1/2) should be not Grayscale, it should have
s->chroma_formatless of 2 (4:2:0 Chroma subsampling, which is usual for ISO/IEC MPEG and ITU-T VCEG H.26x; but not for some MPEG-4 Part 2 and not for high 4:4:4 variants of H.264/MPEG-4 AVC).