[FFmpeg-user] How do I use the HW accerlator?
rogerdpack2 at gmail.com
Mon Jul 30 21:04:35 CEST 2012
> I've similarly been battling to get DXVA2 to work for H264 decoding. The following is what I've learned:
> libavcodec does include some support for DXVA2, but it doesn't do all the work necessary to be able to automatically use DXVA2.
> The "Player" (i.e. the program that you write to use libavcodec) needs to do half of the work. ffmpeg.exe and ffplay.exe have not been written to use DXVA2.
> Everyone points to the VLC open source player as the best (only) example of how to use DXVA2 with libavcodec. Unfortunately VLC is very complex
> and hard to even recompile on Windows. It would be great to have a simple/minimal player example that could demonstrate how to do it.
> From what I understand the basic steps are to:
> 1) get ffmpeg libraries to compile with DXVA2 support enabled.
> 2) Create a DirectXVideoDecoderService (http://msdn.microsoft.com/en-us/library/windows/desktop/aa965245(v=vs.85).aspx#Decoding)
> 3) Choose a Decoder configuration and create an array of Direct3DSurfaces
> 3) Create a dxva_context structure and initialize it's fields with the above objects
> 4) Get the AVCodecContext for your video stream, e.g: pCodecCtx = pFormatCtx->streams[videoStream]->codec
> 5) Before you open the decoder, override the get_format, get_buffer, release_buffer functions of this context, eg: pCodecCtx->get_format = MyGetFormat
> 6) Set the hwaccel_context of this context to the dxva_context structure created earlier.
> 7) Your get_format function needs to select the PIX_FMT_DXVA2_VLD pixel format.
> 8) Your get_buffer function needs to return one of your unused Direct3DSurfaces
> 9) Your release_buffer function needs to note that your Direct3DSurface is now available to be reused.
> I've followed these steps and got it to run. It definitely does use the DXVA accelerator and it doesn't crash, but the output frames are garbled rubbish, which by the was is the same output that I get when I turn on DXVA2 decoding in VLC. I suspect the problem is that I haven't set all of the corrrect parameter values when configuring my DirectX objects to match the video format that I'm trying to play. I know that DVXA2 does definitely work on my machine because I can see Windows Media Player using it successfully.
Perhaps you could publish it as an example app somewhere? (I
mean...ideally all this code would be contained with ffmpeg, yes? but
still for example use.)
More information about the ffmpeg-user