<div dir="ltr">Hi, <div>I've rebuilt ffmpeg using these instructions <a href="https://trac.ffmpeg.org/wiki/CompilationGuide/MSVC">https://trac.ffmpeg.org/wiki/CompilationGuide/MSVC</a> from the Visual Studio Console, installing MinGW directly from the link with the flags <span style="font-family:arial,sans-serif;font-size:13px">--enable-dxva2 and </span><span style="font-family:arial,sans-serif;font-size:13px">--enable-hwaccel=h264_dxva2 and is almost working but I get a lot of </span><font face="arial, sans-serif">errors: -1094995529 "Invalid data found when processing input" from avcodec_decode_video2 a lot of artifacts and a very poor and inconsistent image quality.</font></div>
<div><font face="arial, sans-serif">This is my dxva initialization code:</font></div><div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif"><div>_pCodecCtx->flags|= CODEC_CAP_DR1;</div>
<div>_pCodecCtx->flags|= CODEC_CAP_HWACCEL;</div><div><br></div><div>InputStream *ist = new InputStream ();</div><div>ist->hwaccel_id = HWACCEL_AUTO; </div><div>ist->hwaccel_device = "dxva2";</div><div>
ist->dec = _pCodec;</div><div>ist->dec_ctx = _pCodecCtx;</div><div>_pCodecCtx->coded_width  = 1280;<span class="" style="white-space:pre">  </span></div><div>_pCodecCtx->coded_height = 720;</div><div>_pCodecCtx->opaque = ist;</div>
<div><span class="" style="white-space:pre">            </span></div><div>dxva2_init(_pCodecCtx);</div><div>_pCodecCtx->opaque  = ist;</div><div>_pCodecCtx->get_buffer2 = ist->hwaccel_get_buffer;</div><div>_pCodecCtx->get_format<span style="white-space:pre"> </span>= get_format;</div>
<div>_pCodecCtx->thread_safe_callbacks = 0;</div><div><br></div><div>before  calling</div><div><br></div><div> if (avcodec_open2(_pCodecCtx , _pCodec, 0) < 0)  ...</div><div><br></div><div>Where get_format is:</div>
<div><div><br></div><div>static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)</div><div>{</div><div><span class="" style="white-space:pre">      </span>InputStream* ist = (InputStream*)s->opaque;</div>
<div><span class="" style="white-space:pre">    </span>ist->active_hwaccel_id = HWACCEL_DXVA2;</div><div><span class="" style="white-space:pre"> </span>ist->hwaccel_pix_fmt   = AV_PIX_FMT_DXVA2_VLD;</div><div><span class="" style="white-space:pre"> </span>return ist->hwaccel_pix_fmt;</div>
<div>}</div></div><div><br></div><div>Find attached my modified ffmpeg_dxva2.cpp and ffmpeg_dxva2.h files from the original ffmpeg_dxva2.c and the InputStream declaration from ffmpeg.h</div><div><br></div><div>For getting the decoded frame I call: dxva2_retrieve_data_call, and then I convert it to YUV420p (I make the final conversion via pixel shader with OpenGL texture).</div>
<div><br></div><div>Also, with my old CPU decoder I had 3-6% CPU usage and 5-10% GPU usage (as I said, I'm converting and painting with OpenGL) and now with the "GPU Accelerated" one I have 5-13% CPU and 6-12% GPU.</div>
<div><br></div><div>Any thoughts?</div><div><br></div><div>Thank you!</div></font></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-08-19 11:25 GMT+02:00 Hector Alonso <span dir="ltr"><<a href="mailto:hector.alonso.aparicio@gmail.com" target="_blank">hector.alonso.aparicio@gmail.com</a>></span>:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><span style="font-family:arial,sans-serif;font-size:13px">Hi,</span><div style="font-family:arial,sans-serif;font-size:13px">
I'm developing a H264 decoder in Windows and I want to use the hardware acceleration capabilities: DXVA2</div>
<div style="font-family:arial,sans-serif;font-size:13px">I've already built FFMPEG from git (lastest version: N-65404-gd34ec64; libavcodec 55.73.101 ...) using Mingw64 for 32bits this way:</div><div style="font-family:arial,sans-serif;font-size:13px">

<div><pre style="white-space:pre-wrap;border:none!important;margin-top:0px!important;margin-bottom:0px!important;width:auto!important;clear:none!important;overflow:visible!important;line-height:16px!important;padding:0px 4px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:initial!important;background-color:rgb(249,249,249);background-repeat:initial!important">
<font face="arial, helvetica, sans-serif" color="#000000"><br></font></pre><pre style="white-space:pre-wrap;border:none!important;margin-top:0px!important;margin-bottom:0px!important;width:auto!important;clear:none!important;overflow:visible!important;line-height:16px!important;padding:0px 4px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:initial!important;background-color:rgb(249,249,249);background-repeat:initial!important">
<font face="arial, helvetica, sans-serif" color="#000000">echo 'export PATH=.:/local/bin:/bin:/mingw64/bin' > .profile
source .profile</font></pre><pre style="white-space:pre-wrap;border:none!important;margin-top:0px!important;margin-bottom:0px!important;width:auto!important;clear:none!important;overflow:visible!important;line-height:16px!important;padding:0px 4px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:initial!important;background-color:rgb(249,249,249);background-repeat:initial!important">
<font face="arial, helvetica, sans-serif" color="#000000"><br></font></pre></div><div><font face="arial, helvetica, sans-serif" color="#000000"><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">git config</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">--global</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> core.autocrlf </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">false</span><br>

</font></div><div><font face="arial, helvetica, sans-serif" color="#000000"><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> 
</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">git clone</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> git:</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">//</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"><a href="http://git.videolan.org/" target="_blank">git.videolan.org</a></span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">/</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">x264.git x264
</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">cd</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> x264
.</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">/</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">configure </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">--host</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">=x86_64-w64-mingw32 </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">--enable-static</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">--enable-shared</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">&&</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">make</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">&&</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">make</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">install</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">
</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">cd</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> ..
 
</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">git clone</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> git:</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">//</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"><a href="http://github.com/" target="_blank">github.com</a></span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">/</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">mstorsjo</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">/</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">fdk-aac.git fdk-aac
</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">cd</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> fdk-aac
.</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">/</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">autogen.sh
.</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">/</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">configure </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">--host</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">=x86_64-w64-mingw32 </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">--enable-static</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">--enable-shared</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">&&</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">make</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">&&</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">make</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">install</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">
</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">cd</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> ..</span></font></div></div><div style="font-family:arial,sans-serif;font-size:13px">

<span style="color:rgb(0,0,0);font-family:monospace;font-size:12px;line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"><br></span></div><div style="font-family:arial,sans-serif;font-size:13px"><div><font color="#000000" face="arial, helvetica, sans-serif"><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">git clone</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> git:</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">//</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"><a href="http://source.ffmpeg.org/" target="_blank">source.ffmpeg.org</a></span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">/</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">ffmpeg.git </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">ffmpeg</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">
</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">cd</span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)"> </span><span style="line-height:16px;white-space:pre-wrap;background-color:rgb(249,249,249)">ffmpeg</span></font></div>

<div><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">./configure --enable-gpl --enable-nonfree --enable-libx264 --enable-libfdk_aac --enable-memalign-hack --enable-runtime-cpudetect --enable-dxva2 --enable-decoder?h264_dxva2 --disable-hwaccels --enable-hwaccel=h264_dxva2 --enable-static --enable-shared --extra-cflags=-I/local/</span><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">include --extra-ldflags='-L/local/lib -static' && make && make install</span><br>

</div></div><div style="font-family:arial,sans-serif;font-size:13px"><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif"><br></span></div><div style="font-family:arial,sans-serif;font-size:13px"><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">I've adapted the newest version of ffmpeg_dxva2.c to C++ (with a header file including InputStream, </span><font color="#000000" face="arial, helvetica, sans-serif">HWAccelID and dxva2_init</font><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif"> declarations), now it builds with VisualStudio 2012 and everything is running correctly with my current decoder (not HW accelerated).</span></div>

<div style="font-family:arial,sans-serif;font-size:13px"><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif"><br></span></div><div style="font-family:arial,sans-serif;font-size:13px"><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">I found an old post of this list (</span><font color="#000000" face="arial, helvetica, sans-serif"><a href="https://lists.ffmpeg.org/pipermail/ffmpeg-user/2012-May/006600.html" target="_blank">https://lists.ffmpeg.org/pipermail/ffmpeg-user/2012-May/006600.html</a></font><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">) describing the steps for running it, but now te API has changed...</span></div>

<div style="font-family:arial,sans-serif;font-size:13px"><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">I've also studied the VLC method, but it is also working with the older API (get_buffer instead of get_buffer2, and so).</span></div>

<div style="font-family:arial,sans-serif;font-size:13px"><font color="#000000" face="arial, helvetica, sans-serif">In the newest ffmpeg.c they actually use the hwaccel including DXVA2, but in quite a complicated way... could you please give an example or a piece of advice  on how to do it? </font></div>

<div style="font-family:arial,sans-serif;font-size:13px"><font color="#000000" face="arial, helvetica, sans-serif"><br></font></div><div style="font-family:arial,sans-serif;font-size:13px"><font color="#000000" face="arial, helvetica, sans-serif">1.- Create a InputStream instance, populate it and address it to AVCodecContext->opaque. Which are the needed attributes? It requires width and height of the input before opening it!</font></div>

<div style="font-family:arial,sans-serif;font-size:13px"><font color="#000000" face="arial, helvetica, sans-serif"><br></font></div><div style="font-family:arial,sans-serif;font-size:13px"><font color="#000000" face="arial, helvetica, sans-serif">2.- call dxva_init() with the decoder AVCodecContext allocated using avcodec_alloc_context3 from CODEC_ID_H264 codec before calling avcodec_open2 ?</font></div>

<div style="font-family:arial,sans-serif;font-size:13px"><font color="#000000" face="arial, helvetica, sans-serif"><br></font></div><div style="font-family:arial,sans-serif;font-size:13px"><font color="#000000" face="arial, helvetica, sans-serif">3.- Call dxva2_retrieve_data() with the decoder </font><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">AVCodecContext</span><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">  and a </span><font color="#000000" face="arial, helvetica, sans-serif">AV_PIX_FMT_NV12 frame when avcodec_decode_video2 returns a complete frame (got_output</font><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">) ?</span></div>

<div style="font-family:arial,sans-serif;font-size:13px"><font color="#000000" face="arial, helvetica, sans-serif"><br></font></div><div style="font-family:arial,sans-serif;font-size:13px"><font color="#000000" face="arial, helvetica, sans-serif">4.- Copy (convert) the resulting frame to a YUV420P OpenGL texture, apply a pixelformat conversion shader and show it! <- This step is working fine (I did it for a separated decoder using </font><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">Intel Media SDK</span><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif"> </span><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">last week).</span></div>

<div style="font-family:arial,sans-serif;font-size:13px"><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif"><br></span></div><div style="font-family:arial,sans-serif;font-size:13px"><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">(Future step. - use directly the DX surface using OpenGL extensions.)</span></div>

<div style="font-family:arial,sans-serif;font-size:13px"><font color="#000000" face="arial, helvetica, sans-serif"><br></font></div><div style="font-family:arial,sans-serif;font-size:13px"><font color="#000000" face="arial, helvetica, sans-serif">Now I'm stuck in the first and second steps, when </font><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">avcodec_decode_video2</span><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">  is called it crashes. I've debugged step by step the dxva initialization and the DX surfaces are being created correctly. What am I missing?</span></div>

<div style="font-family:arial,sans-serif;font-size:13px"><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif"><br></span></div><div style="font-family:arial,sans-serif;font-size:13px"><span style="color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">Thanks!</span></div>

</div>
</blockquote></div><br></div>