]> git.sesse.net Git - vlc/blobdiff - modules/codec/avcodec/dxva2.c
decoder: remove decoder_LinkPicture()
[vlc] / modules / codec / avcodec / dxva2.c
index b0e27f4e9ee8c9ead4dfae5f686418b74d090752..710230055bc12c319a5f7c47069b41ad6b83a86f 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * va.c: Video Acceleration helpers
+ * dxva2.c: Video Acceleration helpers
  *****************************************************************************
  * Copyright (C) 2009 Geoffroy Couprie
  * Copyright (C) 2009 Laurent Aimar
@@ -8,46 +8,59 @@
  * Authors: Geoffroy Couprie <geal@videolan.org>
  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
+
+# if _WIN32_WINNT < 0x600
+/* dxva2 needs Vista support */
+#  undef _WIN32_WINNT
+#  define _WIN32_WINNT 0x600
+# endif
+
+#include <assert.h>
+
 #include <vlc_common.h>
 #include <vlc_picture.h>
 #include <vlc_fourcc.h>
 #include <vlc_cpu.h>
-#include <assert.h>
+#include <vlc_plugin.h>
 
-#ifdef HAVE_LIBAVCODEC_AVCODEC_H
-#   include <libavcodec/avcodec.h>
-#   ifdef HAVE_AVCODEC_DXVA2
-#       define DXVA2API_USE_BITFIELDS
-#       include <libavcodec/dxva2.h>
-#   endif
-#else
-#   include <avcodec.h>
-#endif
+#include <libavcodec/avcodec.h>
+#    define DXVA2API_USE_BITFIELDS
+#    define COBJMACROS
+#    include <libavcodec/dxva2.h>
 
 #include "avcodec.h"
 #include "va.h"
-#include "copy.h"
+#include "../../video_chroma/copy.h"
+
+static int Open(vlc_va_t *, AVCodecContext *, const es_format_t *);
+static void Close(vlc_va_t *, AVCodecContext *);
 
-#ifdef HAVE_AVCODEC_DXVA2
+vlc_module_begin()
+    set_description(N_("DirectX Video Acceleration (DXVA) 2.0"))
+    set_capability("hw decoder", 0)
+    set_category(CAT_INPUT)
+    set_subcategory(SUBCAT_INPUT_VCODEC)
+    set_callbacks(Open, Close)
+vlc_module_end()
 
 #include <windows.h>
 #include <windowsx.h>
 #include <commctrl.h>
 #include <shlwapi.h>
 #include <d3d9.h>
+#include <dxva2api.h>
+
+#include <initguid.h> /* must be last included to not redefine existing GUIDs */
+
+/* dxva2api.h GUIDs: http://msdn.microsoft.com/en-us/library/windows/desktop/ms697067(v=vs100).aspx
+ * assume that they are declared in dxva2api.h */
+#define MS_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)
+
+#ifdef __MINGW32__
+# include <_mingw.h>
+
+# if !defined(__MINGW64_VERSION_MAJOR)
+#  undef MS_GUID
+#  define MS_GUID DEFINE_GUID /* dxva2api.h fails to declare those, redefine as static */
+#  define DXVA2_E_NEW_VIDEO_DEVICE MAKE_HRESULT(1, 4, 4097)
+# else
+#  include <dxva.h>
+# endif
+
+#endif /* __MINGW32__ */
+
+MS_GUID(IID_IDirectXVideoDecoderService, 0xfc51a551, 0xd5e7, 0x11d9, 0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
+MS_GUID(IID_IDirectXVideoAccelerationService, 0xfc51a550, 0xd5e7, 0x11d9, 0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
+
+MS_GUID    (DXVA_NoEncrypt,                         0x1b81bed0, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
+
+/* Codec capabilities GUID, sorted by codec */
+MS_GUID    (DXVA2_ModeMPEG2_MoComp,                 0xe6a9f44b, 0x61b0, 0x4563, 0x9e, 0xa4, 0x63, 0xd2, 0xa3, 0xc6, 0xfe, 0x66);
+MS_GUID    (DXVA2_ModeMPEG2_IDCT,                   0xbf22ad00, 0x03ea, 0x4690, 0x80, 0x77, 0x47, 0x33, 0x46, 0x20, 0x9b, 0x7e);
+MS_GUID    (DXVA2_ModeMPEG2_VLD,                    0xee27417f, 0x5e28, 0x4e65, 0xbe, 0xea, 0x1d, 0x26, 0xb5, 0x08, 0xad, 0xc9);
+DEFINE_GUID(DXVA2_ModeMPEG2and1_VLD,                0x86695f12, 0x340e, 0x4f04, 0x9f, 0xd3, 0x92, 0x53, 0xdd, 0x32, 0x74, 0x60);
+DEFINE_GUID(DXVA2_ModeMPEG1_VLD,                    0x6f3ec719, 0x3735, 0x42cc, 0x80, 0x63, 0x65, 0xcc, 0x3c, 0xb3, 0x66, 0x16);
+
+MS_GUID    (DXVA2_ModeH264_A,                       0x1b81be64, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
+MS_GUID    (DXVA2_ModeH264_B,                       0x1b81be65, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
+MS_GUID    (DXVA2_ModeH264_C,                       0x1b81be66, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
+MS_GUID    (DXVA2_ModeH264_D,                       0x1b81be67, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
+MS_GUID    (DXVA2_ModeH264_E,                       0x1b81be68, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
+MS_GUID    (DXVA2_ModeH264_F,                       0x1b81be69, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
+DEFINE_GUID(DXVA_ModeH264_VLD_Multiview,            0x9901CCD3, 0xca12, 0x4b7e, 0x86, 0x7a, 0xe2, 0x22, 0x3d, 0x92, 0x55, 0xc3); // MVC
+DEFINE_GUID(DXVA_ModeH264_VLD_WithFMOASO_NoFGT,     0xd5f04ff9, 0x3418, 0x45d8, 0x95, 0x61, 0x32, 0xa7, 0x6a, 0xae, 0x2d, 0xdd);
+DEFINE_GUID(DXVADDI_Intel_ModeH264_A,               0x604F8E64, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);
+DEFINE_GUID(DXVADDI_Intel_ModeH264_C,               0x604F8E66, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);
+DEFINE_GUID(DXVADDI_Intel_ModeH264_E,               0x604F8E68, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6); // DXVA_Intel_H264_NoFGT_ClearVideo
+DEFINE_GUID(DXVA_ModeH264_VLD_NoFGT_Flash,          0x4245F676, 0x2BBC, 0x4166, 0xa0, 0xBB, 0x54, 0xE7, 0xB8, 0x49, 0xC3, 0x80);
+
+MS_GUID    (DXVA2_ModeWMV8_A,                       0x1b81be80, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
+MS_GUID    (DXVA2_ModeWMV8_B,                       0x1b81be81, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
+
+MS_GUID    (DXVA2_ModeWMV9_A,                       0x1b81be90, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
+MS_GUID    (DXVA2_ModeWMV9_B,                       0x1b81be91, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
+MS_GUID    (DXVA2_ModeWMV9_C,                       0x1b81be94, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
+
+MS_GUID    (DXVA2_ModeVC1_A,                        0x1b81beA0, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
+MS_GUID    (DXVA2_ModeVC1_B,                        0x1b81beA1, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
+MS_GUID    (DXVA2_ModeVC1_C,                        0x1b81beA2, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
+MS_GUID    (DXVA2_ModeVC1_D,                        0x1b81beA3, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
+DEFINE_GUID(DXVA2_ModeVC1_D2010,                    0x1b81beA4, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5); // August 2010 update
+DEFINE_GUID(DXVA_Intel_VC1_ClearVideo,              0xBCC5DB6D, 0xA2B6, 0x4AF0, 0xAC, 0xE4, 0xAD, 0xB1, 0xF7, 0x87, 0xBC, 0x89);
+DEFINE_GUID(DXVA_Intel_VC1_ClearVideo_2,            0xE07EC519, 0xE651, 0x4CD6, 0xAC, 0x84, 0x13, 0x70, 0xCC, 0xEE, 0xC8, 0x51);
+
+DEFINE_GUID(DXVA_nVidia_MPEG4_ASP,                  0x9947EC6F, 0x689B, 0x11DC, 0xA3, 0x20, 0x00, 0x19, 0xDB, 0xBC, 0x41, 0x84);
+DEFINE_GUID(DXVA_ModeMPEG4pt2_VLD_Simple,           0xefd64d74, 0xc9e8, 0x41d7, 0xa5, 0xe9, 0xe9, 0xb0, 0xe3, 0x9f, 0xa3, 0x19);
+DEFINE_GUID(DXVA_ModeMPEG4pt2_VLD_AdvSimple_NoGMC,  0xed418a9f, 0x010d, 0x4eda, 0x9a, 0xe3, 0x9a, 0x65, 0x35, 0x8d, 0x8d, 0x2e);
+DEFINE_GUID(DXVA_ModeMPEG4pt2_VLD_AdvSimple_GMC,    0xab998b5b, 0x4258, 0x44a9, 0x9f, 0xeb, 0x94, 0xe5, 0x97, 0xa6, 0xba, 0xae);
+DEFINE_GUID(DXVA_ModeMPEG4pt2_VLD_AdvSimple_Avivo,  0x7C74ADC6, 0xe2ba, 0x4ade, 0x86, 0xde, 0x30, 0xbe, 0xab, 0xb4, 0x0c, 0xc1);
 
-/* */
-#define DXVA2_E_NOT_INITIALIZED     MAKE_HRESULT(1, 4, 4096)
-#define DXVA2_E_NEW_VIDEO_DEVICE    MAKE_HRESULT(1, 4, 4097)
-#define DXVA2_E_VIDEO_DEVICE_LOCKED MAKE_HRESULT(1, 4, 4098)
-#define DXVA2_E_NOT_AVAILABLE       MAKE_HRESULT(1, 4, 4099)
-
-static const GUID DXVA2_ModeMPEG2_MoComp = {
-    0xe6a9f44b, 0x61b0,0x4563, {0x9e,0xa4,0x63,0xd2,0xa3,0xc6,0xfe,0x66}
-};
-static const GUID DXVA2_ModeMPEG2_IDCT = {
-    0xbf22ad00, 0x03ea,0x4690, {0x80,0x77,0x47,0x33,0x46,0x20,0x9b,0x7e}
-};
-static const GUID DXVA2_ModeMPEG2_VLD = {
-    0xee27417f, 0x5e28,0x4e65, {0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9}
-};
-static const GUID DXVA2_ModeMPEG2and1_VLD = {
-    0x86695f12, 0x340e,0x4f04, {0x9f,0xd3,0x92,0x53,0xdd,0x32,0x74,0x60}
-};
-static const GUID DXVA2_ModeMPEG1_VLD = {
-    0x6f3ec719, 0x3735,0x42cc, {0x80,0x63,0x65,0xcc,0x3c,0xb3,0x66,0x16}
-};
-
-static const GUID DXVA2_ModeH264_A = {
-    0x1b81be64, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-static const GUID DXVA2_ModeH264_B = {
-    0x1b81be65, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-static const GUID DXVA2_ModeH264_C = {
-    0x1b81be66, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-static const GUID DXVA2_ModeH264_D = {
-    0x1b81be67, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-static const GUID DXVA2_ModeH264_E = {
-    0x1b81be68, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-static const GUID DXVA2_ModeH264_F = {
-    0x1b81be69, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-static const GUID DXVADDI_Intel_ModeH264_A = {
-    0x604F8E64, 0x4951,0x4c54, {0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6}
-};
-static const GUID DXVADDI_Intel_ModeH264_C = {
-    0x604F8E66, 0x4951,0x4c54, {0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6}
-};
-static const GUID DXVADDI_Intel_ModeH264_E = { // DXVA_Intel_H264_ClearVideo
-    0x604F8E68, 0x4951,0x4c54, {0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6}
-};
-static const GUID DXVA2_ModeWMV8_A = {
-    0x1b81be80, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-static const GUID DXVA2_ModeWMV8_B = {
-    0x1b81be81, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-static const GUID DXVA2_ModeWMV9_A = {
-    0x1b81be90, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-static const GUID DXVA2_ModeWMV9_B = {
-    0x1b81be91, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-static const GUID DXVA2_ModeWMV9_C = {
-    0x1b81be94, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-
-static const GUID DXVA2_ModeVC1_A = {
-    0x1b81beA0, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-static const GUID DXVA2_ModeVC1_B = {
-    0x1b81beA1, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-static const GUID DXVA2_ModeVC1_C = {
-    0x1b81beA2, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-static const GUID DXVA2_ModeVC1_D = {
-    0x1b81beA3, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-/* Conformity to the August 2010 update of the specification, ModeVC1_VLD2010 */
-static const GUID DXVA2_ModeVC1_D2010 = {
-    0x1b81beA4, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-
-static const GUID DXVA_NoEncrypt = {
-    0x1b81bed0, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
-};
-
-static const GUID DXVA_Intel_VC1_ClearVideo = {
-    0xBCC5DB6D, 0xA2B6,0x4AF0, {0xAC,0xE4,0xAD,0xB1,0xF7,0x87,0xBC,0x89}
-};
-
-static const GUID DXVA_nVidia_MPEG4_ASP = {
-    0x9947EC6F, 0x689B,0x11DC, {0xA3,0x20,0x00,0x19,0xDB,0xBC,0x41,0x84}
-};
 
 /* */
 typedef struct {
@@ -158,41 +144,55 @@ typedef struct {
 } dxva2_mode_t;
 /* XXX Prefered modes must come first */
 static const dxva2_mode_t dxva2_modes[] = {
-    { "MPEG-2 variable-length decoder",            &DXVA2_ModeMPEG2_VLD,     CODEC_ID_MPEG2VIDEO },
-    { "MPEG-2 & MPEG-1 variable-length decoder",   &DXVA2_ModeMPEG2and1_VLD, CODEC_ID_MPEG2VIDEO },
-    { "MPEG-2 motion compensation",                &DXVA2_ModeMPEG2_MoComp,  0 },
-    { "MPEG-2 inverse discrete cosine transform",  &DXVA2_ModeMPEG2_IDCT,    0 },
-
-    { "MPEG-1 variable-length decoder",            &DXVA2_ModeMPEG1_VLD,     0 },
-
-    { "H.264 variable-length decoder, film grain technology",                      &DXVA2_ModeH264_F,         CODEC_ID_H264 },
-    { "H.264 variable-length decoder, no film grain technology",                   &DXVA2_ModeH264_E,         CODEC_ID_H264 },
-    { "H.264 variable-length decoder, no film grain technology (Intel ClearVideo)",&DXVADDI_Intel_ModeH264_E, CODEC_ID_H264 },
-    { "H.264 inverse discrete cosine transform, film grain technology",            &DXVA2_ModeH264_D,         0             },
-    { "H.264 inverse discrete cosine transform, no film grain technology",         &DXVA2_ModeH264_C,         0             },
-    { "H.264 inverse discrete cosine transform, no film grain technology (Intel)", &DXVADDI_Intel_ModeH264_C, 0             },
-    { "H.264 motion compensation, film grain technology",                          &DXVA2_ModeH264_B,         0             },
-    { "H.264 motion compensation, no film grain technology",                       &DXVA2_ModeH264_A,         0             },
-    { "H.264 motion compensation, no film grain technology (Intel)",               &DXVADDI_Intel_ModeH264_A, 0             },
-
-    { "Windows Media Video 8 motion compensation", &DXVA2_ModeWMV8_B, 0 },
-    { "Windows Media Video 8 post processing",     &DXVA2_ModeWMV8_A, 0 },
-
-    { "Windows Media Video 9 IDCT",                &DXVA2_ModeWMV9_C, 0 },
-    { "Windows Media Video 9 motion compensation", &DXVA2_ModeWMV9_B, 0 },
-    { "Windows Media Video 9 post processing",     &DXVA2_ModeWMV9_A, 0 },
-
-    { "VC-1 variable-length decoder",              &DXVA2_ModeVC1_D, CODEC_ID_VC1 },
-    { "VC-1 variable-length decoder",              &DXVA2_ModeVC1_D, CODEC_ID_WMV3 },
-    { "VC-1 variable-length decoder",              &DXVA2_ModeVC1_D2010, CODEC_ID_VC1 },
-    { "VC-1 variable-length decoder",              &DXVA2_ModeVC1_D2010, CODEC_ID_WMV3 },
-    { "VC-1 inverse discrete cosine transform",    &DXVA2_ModeVC1_C, 0 },
-    { "VC-1 motion compensation",                  &DXVA2_ModeVC1_B, 0 },
-    { "VC-1 post processing",                      &DXVA2_ModeVC1_A, 0 },
-
-    { "VC-1 variable-length decoder (Intel)",      &DXVA_Intel_VC1_ClearVideo, 0 },
-
-    { "MPEG-4 ASP nVidia bitstream decoder",       &DXVA_nVidia_MPEG4_ASP, 0 },
+    /* MPEG-1/2 */
+    { "MPEG-2 variable-length decoder",                                               &DXVA2_ModeMPEG2_VLD,                   AV_CODEC_ID_MPEG2VIDEO },
+    { "MPEG-2 & MPEG-1 variable-length decoder",                                      &DXVA2_ModeMPEG2and1_VLD,               AV_CODEC_ID_MPEG2VIDEO },
+    { "MPEG-2 motion compensation",                                                   &DXVA2_ModeMPEG2_MoComp,                0 },
+    { "MPEG-2 inverse discrete cosine transform",                                     &DXVA2_ModeMPEG2_IDCT,                  0 },
+
+    { "MPEG-1 variable-length decoder",                                               &DXVA2_ModeMPEG1_VLD,                   0 },
+
+    /* H.264 */
+    { "H.264 variable-length decoder, film grain technology",                         &DXVA2_ModeH264_F,                      AV_CODEC_ID_H264 },
+    { "H.264 variable-length decoder, no film grain technology (Intel ClearVideo)",   &DXVADDI_Intel_ModeH264_E,              AV_CODEC_ID_H264 },
+    { "H.264 variable-length decoder, no film grain technology",                      &DXVA2_ModeH264_E,                      AV_CODEC_ID_H264 },
+    { "H.264 variable-length decoder, no film grain technology, FMO/ASO",             &DXVA_ModeH264_VLD_WithFMOASO_NoFGT,    AV_CODEC_ID_H264 },
+    { "H.264 variable-length decoder, no film grain technology, Flash",               &DXVA_ModeH264_VLD_NoFGT_Flash,         AV_CODEC_ID_H264 },
+
+    { "H.264 inverse discrete cosine transform, film grain technology",               &DXVA2_ModeH264_D,                      0 },
+    { "H.264 inverse discrete cosine transform, no film grain technology",            &DXVA2_ModeH264_C,                      0 },
+    { "H.264 inverse discrete cosine transform, no film grain technology (Intel)",    &DXVADDI_Intel_ModeH264_C,              0 },
+
+    { "H.264 motion compensation, film grain technology",                             &DXVA2_ModeH264_B,                      0 },
+    { "H.264 motion compensation, no film grain technology",                          &DXVA2_ModeH264_A,                      0 },
+    { "H.264 motion compensation, no film grain technology (Intel)",                  &DXVADDI_Intel_ModeH264_A,              0 },
+
+    /* WMV */
+    { "Windows Media Video 8 motion compensation",                                    &DXVA2_ModeWMV8_B,                      0 },
+    { "Windows Media Video 8 post processing",                                        &DXVA2_ModeWMV8_A,                      0 },
+
+    { "Windows Media Video 9 IDCT",                                                   &DXVA2_ModeWMV9_C,                      0 },
+    { "Windows Media Video 9 motion compensation",                                    &DXVA2_ModeWMV9_B,                      0 },
+    { "Windows Media Video 9 post processing",                                        &DXVA2_ModeWMV9_A,                      0 },
+
+    /* VC-1 */
+    { "VC-1 variable-length decoder",                                                 &DXVA2_ModeVC1_D,                       AV_CODEC_ID_VC1 },
+    { "VC-1 variable-length decoder",                                                 &DXVA2_ModeVC1_D,                       AV_CODEC_ID_WMV3 },
+    { "VC-1 variable-length decoder",                                                 &DXVA2_ModeVC1_D2010,                   AV_CODEC_ID_VC1 },
+    { "VC-1 variable-length decoder",                                                 &DXVA2_ModeVC1_D2010,                   AV_CODEC_ID_WMV3 },
+    { "VC-1 variable-length decoder 2 (Intel)",                                       &DXVA_Intel_VC1_ClearVideo_2,           0 },
+    { "VC-1 variable-length decoder (Intel)",                                         &DXVA_Intel_VC1_ClearVideo,             0 },
+
+    { "VC-1 inverse discrete cosine transform",                                       &DXVA2_ModeVC1_C,                       0 },
+    { "VC-1 motion compensation",                                                     &DXVA2_ModeVC1_B,                       0 },
+    { "VC-1 post processing",                                                         &DXVA2_ModeVC1_A,                       0 },
+
+    /* Xvid/Divx: TODO */
+    { "MPEG-4 Part 2 nVidia bitstream decoder",                                       &DXVA_nVidia_MPEG4_ASP,                 0 },
+    { "MPEG-4 Part 2 variable-length decoder, Simple Profile",                        &DXVA_ModeMPEG4pt2_VLD_Simple,          0 },
+    { "MPEG-4 Part 2 variable-length decoder, Simple&Advanced Profile, no GMC",       &DXVA_ModeMPEG4pt2_VLD_AdvSimple_NoGMC, 0 },
+    { "MPEG-4 Part 2 variable-length decoder, Simple&Advanced Profile, GMC",          &DXVA_ModeMPEG4pt2_VLD_AdvSimple_GMC,   0 },
+    { "MPEG-4 Part 2 variable-length decoder, Simple&Advanced Profile, Avivo",        &DXVA_ModeMPEG4pt2_VLD_AdvSimple_Avivo, 0 },
 
     { NULL, NULL, 0 }
 };
@@ -216,6 +216,7 @@ typedef struct {
 static const d3d_format_t d3d_formats[] = {
     { "YV12",   MAKEFOURCC('Y','V','1','2'),    VLC_CODEC_YV12 },
     { "NV12",   MAKEFOURCC('N','V','1','2'),    VLC_CODEC_NV12 },
+    { "IMC3",   MAKEFOURCC('I','M','C','3'),    VLC_CODEC_YV12 },
 
     { NULL, 0, 0 }
 };
@@ -229,13 +230,6 @@ static const d3d_format_t *D3dFindFormat(D3DFORMAT format)
     return NULL;
 }
 
-static const GUID IID_IDirectXVideoDecoderService = {
-    0xfc51a551, 0xd5e7, 0x11d9, {0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02}
-};
-static const GUID IID_IDirectXVideoAccelerationService = {
-    0xfc51a550, 0xd5e7, 0x11d9, {0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02}
-};
-
 /* */
 typedef struct {
     LPDIRECT3DSURFACE9 d3d;
@@ -244,13 +238,8 @@ typedef struct {
 } vlc_va_surface_t;
 
 #define VA_DXVA2_MAX_SURFACE_COUNT (64)
-typedef struct
+struct vlc_va_sys_t
 {
-    /* */
-    vlc_va_t va;
-
-    /* */
-    vlc_object_t *log;
     int          codec_id;
     int          width;
     int          height;
@@ -293,283 +282,282 @@ typedef struct
     int          surface_height;
     vlc_fourcc_t surface_chroma;
 
+    int          thread_count;
+
     vlc_va_surface_t surface[VA_DXVA2_MAX_SURFACE_COUNT];
     LPDIRECT3DSURFACE9 hw_surface[VA_DXVA2_MAX_SURFACE_COUNT];
-} vlc_va_dxva2_t;
-
-/* */
-static vlc_va_dxva2_t *vlc_va_dxva2_Get(void *external)
-{
-    assert(external == (void*)(&((vlc_va_dxva2_t*)external)->va));
-    return external;
-}
+};
 
 /* */
-static int D3dCreateDevice(vlc_va_dxva2_t *);
-static void D3dDestroyDevice(vlc_va_dxva2_t *);
-static char *DxDescribe(vlc_va_dxva2_t *);
+static int D3dCreateDevice(vlc_va_t *);
+static void D3dDestroyDevice(vlc_va_sys_t *);
+static char *DxDescribe(vlc_va_sys_t *);
 
-static int D3dCreateDeviceManager(vlc_va_dxva2_t *);
-static void D3dDestroyDeviceManager(vlc_va_dxva2_t *);
+static int D3dCreateDeviceManager(vlc_va_t *);
+static void D3dDestroyDeviceManager(vlc_va_sys_t *);
 
-static int DxCreateVideoService(vlc_va_dxva2_t *);
-static void DxDestroyVideoService(vlc_va_dxva2_t *);
-static int DxFindVideoServiceConversion(vlc_va_dxva2_t *, GUID *input, D3DFORMAT *output);
+static int DxCreateVideoService(vlc_va_t *);
+static void DxDestroyVideoService(vlc_va_sys_t *);
+static int DxFindVideoServiceConversion(vlc_va_t *, GUID *input, D3DFORMAT *output);
 
-static int DxCreateVideoDecoder(vlc_va_dxva2_t *,
+static int DxCreateVideoDecoder(vlc_va_t *,
                                 int codec_id, const video_format_t *);
-static void DxDestroyVideoDecoder(vlc_va_dxva2_t *);
-static int DxResetVideoDecoder(vlc_va_dxva2_t *);
+static void DxDestroyVideoDecoder(vlc_va_sys_t *);
+static int DxResetVideoDecoder(vlc_va_t *);
 
-static void DxCreateVideoConversion(vlc_va_dxva2_t *);
-static void DxDestroyVideoConversion(vlc_va_dxva2_t *);
+static void DxCreateVideoConversion(vlc_va_sys_t *);
+static void DxDestroyVideoConversion(vlc_va_sys_t *);
 
 /* */
-static int Setup(vlc_va_t *external, void **hw, vlc_fourcc_t *chroma,
-                 int width, int height)
+static int Setup(vlc_va_t *va, AVCodecContext *avctx, vlc_fourcc_t *chroma)
 {
-    vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
+    vlc_va_sys_t *sys = va->sys;
 
-    if (va->width == width && va->height == height && va->decoder)
+    if (sys->width == avctx->coded_width && sys->height == avctx->coded_height
+     && sys->decoder != NULL)
         goto ok;
 
     /* */
-    DxDestroyVideoConversion(va);
-    DxDestroyVideoDecoder(va);
+    DxDestroyVideoConversion(sys);
+    DxDestroyVideoDecoder(sys);
 
-    *hw = NULL;
+    avctx->hwaccel_context = NULL;
     *chroma = 0;
-    if (width <= 0 || height <= 0)
+    if (avctx->coded_width <= 0 || avctx->coded_height <= 0)
         return VLC_EGENERIC;
 
     /* FIXME transmit a video_format_t by VaSetup directly */
     video_format_t fmt;
     memset(&fmt, 0, sizeof(fmt));
-    fmt.i_width = width;
-    fmt.i_height = height;
+    fmt.i_width = avctx->coded_width;
+    fmt.i_height = avctx->coded_height;
 
-    if (DxCreateVideoDecoder(va, va->codec_id, &fmt))
+    if (DxCreateVideoDecoder(va, sys->codec_id, &fmt))
         return VLC_EGENERIC;
     /* */
-    va->hw.decoder = va->decoder;
-    va->hw.cfg = &va->cfg;
-    va->hw.surface_count = va->surface_count;
-    va->hw.surface = va->hw_surface;
-    for (unsigned i = 0; i < va->surface_count; i++)
-        va->hw.surface[i] = va->surface[i].d3d;
+    sys->hw.decoder = sys->decoder;
+    sys->hw.cfg = &sys->cfg;
+    sys->hw.surface_count = sys->surface_count;
+    sys->hw.surface = sys->hw_surface;
 
     /* */
-    DxCreateVideoConversion(va);
+    DxCreateVideoConversion(sys);
 
     /* */
 ok:
-    *hw = &va->hw;
-    const d3d_format_t *output = D3dFindFormat(va->output);
+    avctx->hwaccel_context = &sys->hw;
+    const d3d_format_t *output = D3dFindFormat(sys->output);
     *chroma = output->codec;
 
     return VLC_SUCCESS;
 }
 
-static int Extract(vlc_va_t *external, picture_t *picture, AVFrame *ff)
+static int Extract(vlc_va_t *va, picture_t *picture, void *opaque,
+                   uint8_t *data)
 {
-    vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
-    LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)ff->data[3];
+    vlc_va_sys_t *sys = va->sys;
+    LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)data;
 
-    if (!va->surface_cache.buffer)
+    if (!sys->surface_cache.buffer)
         return VLC_EGENERIC;
 
     /* */
-    assert(va->output == MAKEFOURCC('Y','V','1','2'));
+    assert(sys->output == MAKEFOURCC('Y','V','1','2'));
 
     /* */
     D3DLOCKED_RECT lock;
     if (FAILED(IDirect3DSurface9_LockRect(d3d, &lock, NULL, D3DLOCK_READONLY))) {
-        msg_Err(va->log, "Failed to lock surface");
+        msg_Err(va, "Failed to lock surface");
         return VLC_EGENERIC;
     }
 
-    if (va->render == MAKEFOURCC('Y','V','1','2')) {
-        uint8_t *plane[3] = {
-            lock.pBits,
-            (uint8_t*)lock.pBits + lock.Pitch * va->surface_height,
-            (uint8_t*)lock.pBits + lock.Pitch * va->surface_height
-                                 + (lock.Pitch/2) * (va->surface_height/2)
-        };
-        size_t  pitch[3] = {
+    if (sys->render == MAKEFOURCC('Y','V','1','2') ||
+        sys->render == MAKEFOURCC('I','M','C','3')) {
+        bool imc3 = sys->render == MAKEFOURCC('I','M','C','3');
+        size_t chroma_pitch = imc3 ? lock.Pitch : (lock.Pitch / 2);
+
+        size_t pitch[3] = {
             lock.Pitch,
-            lock.Pitch / 2,
-            lock.Pitch / 2,
+            chroma_pitch,
+            chroma_pitch,
+        };
+
+        uint8_t *plane[3] = {
+            (uint8_t*)lock.pBits,
+            (uint8_t*)lock.pBits + pitch[0] * sys->surface_height,
+            (uint8_t*)lock.pBits + pitch[0] * sys->surface_height
+                                 + pitch[1] * sys->surface_height / 2,
         };
-        CopyFromYv12(picture, plane, pitch,
-                     va->width, va->height,
-                     &va->surface_cache);
+
+        if (imc3) {
+            uint8_t *V = plane[1];
+            plane[1] = plane[2];
+            plane[2] = V;
+        }
+        CopyFromYv12(picture, plane, pitch, sys->width, sys->height,
+                     &sys->surface_cache);
     } else {
-        assert(va->render == MAKEFOURCC('N','V','1','2'));
+        assert(sys->render == MAKEFOURCC('N','V','1','2'));
         uint8_t *plane[2] = {
             lock.pBits,
-            (uint8_t*)lock.pBits + lock.Pitch * va->surface_height
+            (uint8_t*)lock.pBits + lock.Pitch * sys->surface_height
         };
         size_t  pitch[2] = {
             lock.Pitch,
             lock.Pitch,
         };
-        CopyFromNv12(picture, plane, pitch,
-                     va->width, va->height,
-                     &va->surface_cache);
+        CopyFromNv12(picture, plane, pitch, sys->width, sys->height,
+                     &sys->surface_cache);
     }
 
     /* */
     IDirect3DSurface9_UnlockRect(d3d);
+    (void) opaque;
     return VLC_SUCCESS;
 }
+
 /* FIXME it is nearly common with VAAPI */
-static int Get(vlc_va_t *external, AVFrame *ff)
+static int Get(vlc_va_t *va, void **opaque, uint8_t **data)
 {
-    vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
+    vlc_va_sys_t *sys = va->sys;
 
     /* Check the device */
-    HRESULT hr = IDirect3DDeviceManager9_TestDevice(va->devmng, va->device);
+    HRESULT hr = IDirect3DDeviceManager9_TestDevice(sys->devmng, sys->device);
     if (hr == DXVA2_E_NEW_VIDEO_DEVICE) {
         if (DxResetVideoDecoder(va))
             return VLC_EGENERIC;
     } else if (FAILED(hr)) {
-        msg_Err(va->log, "IDirect3DDeviceManager9_TestDevice %u", (unsigned)hr);
+        msg_Err(va, "IDirect3DDeviceManager9_TestDevice %u", (unsigned)hr);
         return VLC_EGENERIC;
     }
 
     /* Grab an unused surface, in case none are, try the oldest
-     * XXX using the oldest is a workaround in case a problem happens with ffmpeg */
+     * XXX using the oldest is a workaround in case a problem happens with libavcodec */
     unsigned i, old;
-    for (i = 0, old = 0; i < va->surface_count; i++) {
-        vlc_va_surface_t *surface = &va->surface[i];
+    for (i = 0, old = 0; i < sys->surface_count; i++) {
+        vlc_va_surface_t *surface = &sys->surface[i];
 
         if (!surface->refcount)
             break;
 
-        if (surface->order < va->surface[old].order)
+        if (surface->order < sys->surface[old].order)
             old = i;
     }
-    if (i >= va->surface_count)
+    if (i >= sys->surface_count)
         i = old;
 
-    vlc_va_surface_t *surface = &va->surface[i];
+    vlc_va_surface_t *surface = &sys->surface[i];
 
     surface->refcount = 1;
-    surface->order = va->surface_order++;
-
-    /* */
-    for (int i = 0; i < 4; i++) {
-        ff->data[i] = NULL;
-        ff->linesize[i] = 0;
-
-        if (i == 0 || i == 3)
-            ff->data[i] = (void*)surface->d3d;/* Yummie */
-    }
+    surface->order = sys->surface_order++;
+    *data = (void *)surface->d3d;
+    *opaque = surface;
     return VLC_SUCCESS;
 }
-static void Release(vlc_va_t *external, AVFrame *ff)
-{
-    vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
-    LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)ff->data[3];
-
-    for (unsigned i = 0; i < va->surface_count; i++) {
-        vlc_va_surface_t *surface = &va->surface[i];
 
-        if (surface->d3d == d3d)
-            surface->refcount--;
-    }
-}
-static void Close(vlc_va_t *external)
+static void Release(void *opaque, uint8_t *data)
 {
-    vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
-
-    DxDestroyVideoConversion(va);
-    DxDestroyVideoDecoder(va);
-    DxDestroyVideoService(va);
-    D3dDestroyDeviceManager(va);
-    D3dDestroyDevice(va);
+    vlc_va_surface_t *surface = opaque;
 
-    if (va->hdxva2_dll)
-        FreeLibrary(va->hdxva2_dll);
-    if (va->hd3d9_dll)
-        FreeLibrary(va->hd3d9_dll);
+    surface->refcount--;
+    (void) data;
+}
 
-    free(va->va.description);
-    free(va);
+static void Close(vlc_va_t *va, AVCodecContext *ctx)
+{
+    vlc_va_sys_t *sys = va->sys;
+
+    (void) ctx;
+    DxDestroyVideoConversion(sys);
+    DxDestroyVideoDecoder(sys);
+    DxDestroyVideoService(sys);
+    D3dDestroyDeviceManager(sys);
+    D3dDestroyDevice(sys);
+
+    if (sys->hdxva2_dll)
+        FreeLibrary(sys->hdxva2_dll);
+    if (sys->hd3d9_dll)
+        FreeLibrary(sys->hd3d9_dll);
+
+    free((char *)va->description);
+    free(sys);
 }
 
-vlc_va_t *vlc_va_NewDxva2(vlc_object_t *log, int codec_id)
+static int Open(vlc_va_t *va, AVCodecContext *ctx, const es_format_t *fmt)
 {
-    vlc_va_dxva2_t *va = calloc(1, sizeof(*va));
-    if (!va)
-        return NULL;
+    vlc_va_sys_t *sys = calloc(1, sizeof (*sys));
+    if (unlikely(sys == NULL))
+        return VLC_ENOMEM;
 
-    /* */
-    va->log = log;
-    va->codec_id = codec_id;
+    va->sys = sys;
+    sys->codec_id = ctx->codec_id;
+    (void) fmt;
 
     /* Load dll*/
-    va->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
-    if (!va->hd3d9_dll) {
-        msg_Warn(va->log, "cannot load d3d9.dll");
+    sys->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
+    if (!sys->hd3d9_dll) {
+        msg_Warn(va, "cannot load d3d9.dll");
         goto error;
     }
-    va->hdxva2_dll = LoadLibrary(TEXT("DXVA2.DLL"));
-    if (!va->hdxva2_dll) {
-        msg_Warn(va->log, "cannot load dxva2.dll");
+    sys->hdxva2_dll = LoadLibrary(TEXT("DXVA2.DLL"));
+    if (!sys->hdxva2_dll) {
+        msg_Warn(va, "cannot load dxva2.dll");
         goto error;
     }
-    msg_Dbg(va->log, "DLLs loaded");
+    msg_Dbg(va, "DLLs loaded");
 
     /* */
     if (D3dCreateDevice(va)) {
-        msg_Err(va->log, "Failed to create Direct3D device");
+        msg_Err(va, "Failed to create Direct3D device");
         goto error;
     }
-    msg_Dbg(va->log, "D3dCreateDevice succeed");
+    msg_Dbg(va, "D3dCreateDevice succeed");
 
     if (D3dCreateDeviceManager(va)) {
-        msg_Err(va->log, "D3dCreateDeviceManager failed");
+        msg_Err(va, "D3dCreateDeviceManager failed");
         goto error;
     }
 
     if (DxCreateVideoService(va)) {
-        msg_Err(va->log, "DxCreateVideoService failed");
+        msg_Err(va, "DxCreateVideoService failed");
         goto error;
     }
 
     /* */
-    if (DxFindVideoServiceConversion(va, &va->input, &va->render)) {
-        msg_Err(va->log, "DxFindVideoServiceConversion failed");
+    if (DxFindVideoServiceConversion(va, &sys->input, &sys->render)) {
+        msg_Err(va, "DxFindVideoServiceConversion failed");
         goto error;
     }
 
+    sys->thread_count = ctx->thread_count;
+
     /* TODO print the hardware name/vendor for debugging purposes */
-    va->va.description = DxDescribe(va);
-    va->va.setup   = Setup;
-    va->va.get     = Get;
-    va->va.release = Release;
-    va->va.extract = Extract;
-    va->va.close   = Close;
-    return &va->va;
+    va->description = DxDescribe(sys);
+    va->pix_fmt = PIX_FMT_DXVA2_VLD;
+    va->setup   = Setup;
+    va->get     = Get;
+    va->release = Release;
+    va->extract = Extract;
+    return VLC_SUCCESS;
 
 error:
-    Close(&va->va);
-    return NULL;
+    Close(va, ctx);
+    return VLC_EGENERIC;
 }
 /* */
 
 /**
  * It creates a Direct3D device usable for DXVA 2
  */
-static int D3dCreateDevice(vlc_va_dxva2_t *va)
+static int D3dCreateDevice(vlc_va_t *va)
 {
+    vlc_va_sys_t *sys = va->sys;
+
     /* */
     LPDIRECT3D9 (WINAPI *Create9)(UINT SDKVersion);
-    Create9 = (void *)GetProcAddress(va->hd3d9_dll,
-                                     TEXT("Direct3DCreate9"));
+    Create9 = (void *)GetProcAddress(sys->hd3d9_dll, "Direct3DCreate9");
     if (!Create9) {
-        msg_Err(va->log, "Cannot locate reference to Direct3DCreate9 ABI in DLL");
+        msg_Err(va, "Cannot locate reference to Direct3DCreate9 ABI in DLL");
         return VLC_EGENERIC;
     }
 
@@ -577,21 +565,21 @@ static int D3dCreateDevice(vlc_va_dxva2_t *va)
     LPDIRECT3D9 d3dobj;
     d3dobj = Create9(D3D_SDK_VERSION);
     if (!d3dobj) {
-        msg_Err(va->log, "Direct3DCreate9 failed");
+        msg_Err(va, "Direct3DCreate9 failed");
         return VLC_EGENERIC;
     }
-    va->d3dobj = d3dobj;
+    sys->d3dobj = d3dobj;
 
     /* */
-    D3DADAPTER_IDENTIFIER9 *d3dai = &va->d3dai;
-    if (FAILED(IDirect3D9_GetAdapterIdentifier(va->d3dobj,
+    D3DADAPTER_IDENTIFIER9 *d3dai = &sys->d3dai;
+    if (FAILED(IDirect3D9_GetAdapterIdentifier(sys->d3dobj,
                                                D3DADAPTER_DEFAULT, 0, d3dai))) {
-        msg_Warn(va->log, "IDirect3D9_GetAdapterIdentifier failed");
+        msg_Warn(va, "IDirect3D9_GetAdapterIdentifier failed");
         ZeroMemory(d3dai, sizeof(*d3dai));
     }
 
     /* */
-    D3DPRESENT_PARAMETERS *d3dpp = &va->d3dpp;
+    D3DPRESENT_PARAMETERS *d3dpp = &sys->d3dpp;
     ZeroMemory(d3dpp, sizeof(*d3dpp));
     d3dpp->Flags                  = D3DPRESENTFLAG_VIDEO;
     d3dpp->Windowed               = TRUE;
@@ -607,24 +595,25 @@ static int D3dCreateDevice(vlc_va_dxva2_t *va)
 
     /* Direct3D needs a HWND to create a device, even without using ::Present
     this HWND is used to alert Direct3D when there's a change of focus window.
-    For now, use GetShellWindow, as it looks harmless */
+    For now, use GetDesktopWindow, as it looks harmless */
     LPDIRECT3DDEVICE9 d3ddev;
     if (FAILED(IDirect3D9_CreateDevice(d3dobj, D3DADAPTER_DEFAULT,
-                                       D3DDEVTYPE_HAL, GetShellWindow(),
+                                       D3DDEVTYPE_HAL, GetDesktopWindow(),
                                        D3DCREATE_SOFTWARE_VERTEXPROCESSING |
                                        D3DCREATE_MULTITHREADED,
                                        d3dpp, &d3ddev))) {
-        msg_Err(va->log, "IDirect3D9_CreateDevice failed");
+        msg_Err(va, "IDirect3D9_CreateDevice failed");
         return VLC_EGENERIC;
     }
-    va->d3ddev = d3ddev;
+    sys->d3ddev = d3ddev;
 
     return VLC_SUCCESS;
 }
+
 /**
  * It releases a Direct3D device and its resources.
  */
-static void D3dDestroyDevice(vlc_va_dxva2_t *va)
+static void D3dDestroyDevice(vlc_va_sys_t *va)
 {
     if (va->d3ddev)
         IDirect3DDevice9_Release(va->d3ddev);
@@ -634,7 +623,7 @@ static void D3dDestroyDevice(vlc_va_dxva2_t *va)
 /**
  * It describes our Direct3D object
  */
-static char *DxDescribe(vlc_va_dxva2_t *va)
+static char *DxDescribe(vlc_va_sys_t *va)
 {
     static const struct {
         unsigned id;
@@ -642,6 +631,7 @@ static char *DxDescribe(vlc_va_dxva2_t *va)
     } vendors [] = {
         { 0x1002, "ATI" },
         { 0x10DE, "NVIDIA" },
+        { 0x1106, "VIA" },
         { 0x8086, "Intel" },
         { 0x5333, "S3 Graphics" },
         { 0, "" }
@@ -657,7 +647,7 @@ static char *DxDescribe(vlc_va_dxva2_t *va)
     }
 
     char *description;
-    if (asprintf(&description, "DXVA2 (%.*s, vendor %d(%s), device %d, revision %d)",
+    if (asprintf(&description, "DXVA2 (%.*s, vendor %lu(%s), device %lu, revision %lu)",
                  sizeof(id->Description), id->Description,
                  id->VendorId, vendor, id->DeviceId, id->Revision) < 0)
         return NULL;
@@ -667,33 +657,35 @@ static char *DxDescribe(vlc_va_dxva2_t *va)
 /**
  * It creates a Direct3D device manager
  */
-static int D3dCreateDeviceManager(vlc_va_dxva2_t *va)
+static int D3dCreateDeviceManager(vlc_va_t *va)
 {
+    vlc_va_sys_t *sys = va->sys;
+
     HRESULT (WINAPI *CreateDeviceManager9)(UINT *pResetToken,
                                            IDirect3DDeviceManager9 **);
     CreateDeviceManager9 =
-      (void *)GetProcAddress(va->hdxva2_dll,
-                             TEXT("DXVA2CreateDirect3DDeviceManager9"));
+      (void *)GetProcAddress(sys->hdxva2_dll,
+                             "DXVA2CreateDirect3DDeviceManager9");
 
     if (!CreateDeviceManager9) {
-        msg_Err(va->log, "cannot load function");
+        msg_Err(va, "cannot load function");
         return VLC_EGENERIC;
     }
-    msg_Dbg(va->log, "OurDirect3DCreateDeviceManager9 Success!");
+    msg_Dbg(va, "OurDirect3DCreateDeviceManager9 Success!");
 
     UINT token;
     IDirect3DDeviceManager9 *devmng;
     if (FAILED(CreateDeviceManager9(&token, &devmng))) {
-        msg_Err(va->log, " OurDirect3DCreateDeviceManager9 failed");
+        msg_Err(va, " OurDirect3DCreateDeviceManager9 failed");
         return VLC_EGENERIC;
     }
-    va->token  = token;
-    va->devmng = devmng;
-    msg_Info(va->log, "obtained IDirect3DDeviceManager9");
+    sys->token  = token;
+    sys->devmng = devmng;
+    msg_Info(va, "obtained IDirect3DDeviceManager9");
 
-    HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, va->d3ddev, token);
+    HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, sys->d3ddev, token);
     if (FAILED(hr)) {
-        msg_Err(va->log, "IDirect3DDeviceManager9_ResetDevice failed: %08x", (unsigned)hr);
+        msg_Err(va, "IDirect3DDeviceManager9_ResetDevice failed: %08x", (unsigned)hr);
         return VLC_EGENERIC;
     }
     return VLC_SUCCESS;
@@ -701,7 +693,7 @@ static int D3dCreateDeviceManager(vlc_va_dxva2_t *va)
 /**
  * It destroys a Direct3D device manager
  */
-static void D3dDestroyDeviceManager(vlc_va_dxva2_t *va)
+static void D3dDestroyDeviceManager(vlc_va_sys_t *va)
 {
     if (va->devmng)
         IDirect3DDeviceManager9_Release(va->devmng);
@@ -710,47 +702,48 @@ static void D3dDestroyDeviceManager(vlc_va_dxva2_t *va)
 /**
  * It creates a DirectX video service
  */
-static int DxCreateVideoService(vlc_va_dxva2_t *va)
+static int DxCreateVideoService(vlc_va_t *va)
 {
+    vlc_va_sys_t *sys = va->sys;
+
     HRESULT (WINAPI *CreateVideoService)(IDirect3DDevice9 *,
                                          REFIID riid,
                                          void **ppService);
     CreateVideoService =
-      (void *)GetProcAddress(va->hdxva2_dll,
-                             TEXT("DXVA2CreateVideoService"));
+      (void *)GetProcAddress(sys->hdxva2_dll, "DXVA2CreateVideoService");
 
     if (!CreateVideoService) {
-        msg_Err(va->log, "cannot load function");
+        msg_Err(va, "cannot load function");
         return 4;
     }
-    msg_Info(va->log, "DXVA2CreateVideoService Success!");
+    msg_Info(va, "DXVA2CreateVideoService Success!");
 
     HRESULT hr;
 
     HANDLE device;
-    hr = IDirect3DDeviceManager9_OpenDeviceHandle(va->devmng, &device);
+    hr = IDirect3DDeviceManager9_OpenDeviceHandle(sys->devmng, &device);
     if (FAILED(hr)) {
-        msg_Err(va->log, "OpenDeviceHandle failed");
+        msg_Err(va, "OpenDeviceHandle failed");
         return VLC_EGENERIC;
     }
-    va->device = device;
+    sys->device = device;
 
-    IDirectXVideoDecoderService *vs;
-    hr = IDirect3DDeviceManager9_GetVideoService(va->devmng, device,
-                                                 &IID_IDirectXVideoDecoderService,
-                                                 &vs);
+    void *pv;
+    hr = IDirect3DDeviceManager9_GetVideoService(sys->devmng, device,
+                                        &IID_IDirectXVideoDecoderService, &pv);
     if (FAILED(hr)) {
-        msg_Err(va->log, "GetVideoService failed");
+        msg_Err(va, "GetVideoService failed");
         return VLC_EGENERIC;
     }
-    va->vs = vs;
+    sys->vs = pv;
 
     return VLC_SUCCESS;
 }
+
 /**
  * It destroys a DirectX video service
  */
-static void DxDestroyVideoService(vlc_va_dxva2_t *va)
+static void DxDestroyVideoService(vlc_va_sys_t *va)
 {
     if (va->device)
         IDirect3DDeviceManager9_CloseDeviceHandle(va->devmng, va->device);
@@ -760,24 +753,26 @@ static void DxDestroyVideoService(vlc_va_dxva2_t *va)
 /**
  * Find the best suited decoder mode GUID and render format.
  */
-static int DxFindVideoServiceConversion(vlc_va_dxva2_t *va, GUID *input, D3DFORMAT *output)
+static int DxFindVideoServiceConversion(vlc_va_t *va, GUID *input, D3DFORMAT *output)
 {
+    vlc_va_sys_t *sys = va->sys;
+
     /* Retreive supported modes from the decoder service */
     UINT input_count = 0;
     GUID *input_list = NULL;
-    if (FAILED(IDirectXVideoDecoderService_GetDecoderDeviceGuids(va->vs,
+    if (FAILED(IDirectXVideoDecoderService_GetDecoderDeviceGuids(sys->vs,
                                                                  &input_count,
                                                                  &input_list))) {
-        msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderDeviceGuids failed");
+        msg_Err(va, "IDirectXVideoDecoderService_GetDecoderDeviceGuids failed");
         return VLC_EGENERIC;
     }
     for (unsigned i = 0; i < input_count; i++) {
         const GUID *g = &input_list[i];
         const dxva2_mode_t *mode = Dxva2FindMode(g);
         if (mode) {
-            msg_Dbg(va->log, "- '%s' is supported by hardware", mode->name);
+            msg_Dbg(va, "- '%s' is supported by hardware", mode->name);
         } else {
-            msg_Warn(va->log, "- Unknown GUID = %08X-%04x-%04x-XXXX",
+            msg_Warn(va, "- Unknown GUID = %08X-%04x-%04x-XXXX",
                      (unsigned)g->Data1, g->Data2, g->Data3);
         }
     }
@@ -785,34 +780,34 @@ static int DxFindVideoServiceConversion(vlc_va_dxva2_t *va, GUID *input, D3DFORM
     /* Try all supported mode by our priority */
     for (unsigned i = 0; dxva2_modes[i].name; i++) {
         const dxva2_mode_t *mode = &dxva2_modes[i];
-        if (!mode->codec || mode->codec != va->codec_id)
+        if (!mode->codec || mode->codec != sys->codec_id)
             continue;
 
         /* */
-        bool is_suported = false;
-        for (const GUID *g = &input_list[0]; !is_suported && g < &input_list[input_count]; g++) {
-            is_suported = IsEqualGUID(mode->guid, g);
+        bool is_supported = false;
+        for (const GUID *g = &input_list[0]; !is_supported && g < &input_list[input_count]; g++) {
+            is_supported = IsEqualGUID(mode->guid, g);
         }
-        if (!is_suported)
+        if (!is_supported)
             continue;
 
         /* */
-        msg_Dbg(va->log, "Trying to use '%s' as input", mode->name);
+        msg_Dbg(va, "Trying to use '%s' as input", mode->name);
         UINT      output_count = 0;
         D3DFORMAT *output_list = NULL;
-        if (FAILED(IDirectXVideoDecoderService_GetDecoderRenderTargets(va->vs, mode->guid,
+        if (FAILED(IDirectXVideoDecoderService_GetDecoderRenderTargets(sys->vs, mode->guid,
                                                                        &output_count,
                                                                        &output_list))) {
-            msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderRenderTargets failed");
+            msg_Err(va, "IDirectXVideoDecoderService_GetDecoderRenderTargets failed");
             continue;
         }
         for (unsigned j = 0; j < output_count; j++) {
             const D3DFORMAT f = output_list[j];
             const d3d_format_t *format = D3dFindFormat(f);
             if (format) {
-                msg_Dbg(va->log, "%s is supported for output", format->name);
+                msg_Dbg(va, "%s is supported for output", format->name);
             } else {
-                msg_Dbg(va->log, "%d is supported for output (%4.4s)", f, (const char*)&f);
+                msg_Dbg(va, "%d is supported for output (%4.4s)", f, (const char*)&f);
             }
         }
 
@@ -821,15 +816,15 @@ static int DxFindVideoServiceConversion(vlc_va_dxva2_t *va, GUID *input, D3DFORM
             const d3d_format_t *format = &d3d_formats[j];
 
             /* */
-            bool is_suported = false;
-            for (unsigned k = 0; !is_suported && k < output_count; k++) {
-                is_suported = format->format == output_list[k];
+            bool is_supported = false;
+            for (unsigned k = 0; !is_supported && k < output_count; k++) {
+                is_supported = format->format == output_list[k];
             }
-            if (!is_suported)
+            if (!is_supported)
                 continue;
 
             /* We have our solution */
-            msg_Dbg(va->log, "Using '%s' to decode to '%s'", mode->name, format->name);
+            msg_Dbg(va, "Using '%s' to decode to '%s'", mode->name, format->name);
             *input  = *mode->guid;
             *output = format->format;
             CoTaskMemFree(output_list);
@@ -845,57 +840,66 @@ static int DxFindVideoServiceConversion(vlc_va_dxva2_t *va, GUID *input, D3DFORM
 /**
  * It creates a DXVA2 decoder using the given video format
  */
-static int DxCreateVideoDecoder(vlc_va_dxva2_t *va,
+static int DxCreateVideoDecoder(vlc_va_t *va,
                                 int codec_id, const video_format_t *fmt)
 {
+    vlc_va_sys_t *sys = va->sys;
+
     /* */
-    msg_Dbg(va->log, "DxCreateVideoDecoder id %d %dx%d",
+    msg_Dbg(va, "DxCreateVideoDecoder id %d %dx%d",
             codec_id, fmt->i_width, fmt->i_height);
 
-    va->width  = fmt->i_width;
-    va->height = fmt->i_height;
+    sys->width  = fmt->i_width;
+    sys->height = fmt->i_height;
 
     /* Allocates all surfaces needed for the decoder */
-    va->surface_width  = (fmt->i_width  + 15) & ~15;
-    va->surface_height = (fmt->i_height + 15) & ~15;
+    sys->surface_width  = (fmt->i_width  + 15) & ~15;
+    sys->surface_height = (fmt->i_height + 15) & ~15;
+    int surface_count;
     switch (codec_id) {
-    case CODEC_ID_H264:
-        va->surface_count = 16 + 1;
+    case AV_CODEC_ID_H264:
+        surface_count = 16 + sys->thread_count + 2;
+        break;
+    case AV_CODEC_ID_MPEG1VIDEO:
+    case AV_CODEC_ID_MPEG2VIDEO:
+        surface_count = 2 + 2;
         break;
     default:
-        va->surface_count = 2 + 1;
+        surface_count = 2 + 1;
         break;
     }
-    LPDIRECT3DSURFACE9 surface_list[VA_DXVA2_MAX_SURFACE_COUNT];
-    if (FAILED(IDirectXVideoDecoderService_CreateSurface(va->vs,
-                                                         va->surface_width,
-                                                         va->surface_height,
-                                                         va->surface_count - 1,
-                                                         va->render,
+    if (surface_count > VA_DXVA2_MAX_SURFACE_COUNT)
+        return VLC_EGENERIC;
+    sys->surface_count = surface_count;
+    if (FAILED(IDirectXVideoDecoderService_CreateSurface(sys->vs,
+                                                         sys->surface_width,
+                                                         sys->surface_height,
+                                                         sys->surface_count - 1,
+                                                         sys->render,
                                                          D3DPOOL_DEFAULT,
                                                          0,
                                                          DXVA2_VideoDecoderRenderTarget,
-                                                         surface_list,
+                                                         sys->hw_surface,
                                                          NULL))) {
-        msg_Err(va->log, "IDirectXVideoAccelerationService_CreateSurface failed");
-        va->surface_count = 0;
+        msg_Err(va, "IDirectXVideoAccelerationService_CreateSurface failed");
+        sys->surface_count = 0;
         return VLC_EGENERIC;
     }
-    for (unsigned i = 0; i < va->surface_count; i++) {
-        vlc_va_surface_t *surface = &va->surface[i];
-        surface->d3d = surface_list[i];
+    for (unsigned i = 0; i < sys->surface_count; i++) {
+        vlc_va_surface_t *surface = &sys->surface[i];
+        surface->d3d = sys->hw_surface[i];
         surface->refcount = 0;
         surface->order = 0;
     }
-    msg_Dbg(va->log, "IDirectXVideoAccelerationService_CreateSurface succeed with %d surfaces (%dx%d)",
-            va->surface_count, fmt->i_width, fmt->i_height);
+    msg_Dbg(va, "IDirectXVideoAccelerationService_CreateSurface succeed with %d surfaces (%dx%d)",
+            sys->surface_count, fmt->i_width, fmt->i_height);
 
     /* */
     DXVA2_VideoDesc dsc;
     ZeroMemory(&dsc, sizeof(dsc));
     dsc.SampleWidth     = fmt->i_width;
     dsc.SampleHeight    = fmt->i_height;
-    dsc.Format          = va->render;
+    dsc.Format          = sys->render;
     if (fmt->i_frame_rate > 0 && fmt->i_frame_rate_base > 0) {
         dsc.InputSampleFreq.Numerator   = fmt->i_frame_rate;
         dsc.InputSampleFreq.Denominator = fmt->i_frame_rate_base;
@@ -920,16 +924,16 @@ static int DxCreateVideoDecoder(vlc_va_dxva2_t *va,
     /* List all configurations available for the decoder */
     UINT                      cfg_count = 0;
     DXVA2_ConfigPictureDecode *cfg_list = NULL;
-    if (FAILED(IDirectXVideoDecoderService_GetDecoderConfigurations(va->vs,
-                                                                    &va->input,
+    if (FAILED(IDirectXVideoDecoderService_GetDecoderConfigurations(sys->vs,
+                                                                    &sys->input,
                                                                     &dsc,
                                                                     NULL,
                                                                     &cfg_count,
                                                                     &cfg_list))) {
-        msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderConfigurations failed");
+        msg_Err(va, "IDirectXVideoDecoderService_GetDecoderConfigurations failed");
         return VLC_EGENERIC;
     }
-    msg_Dbg(va->log, "we got %d decoder configurations", cfg_count);
+    msg_Dbg(va, "we got %d decoder configurations", cfg_count);
 
     /* Select the best decoder configuration */
     int cfg_score = 0;
@@ -937,14 +941,14 @@ static int DxCreateVideoDecoder(vlc_va_dxva2_t *va,
         const DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
 
         /* */
-        msg_Dbg(va->log, "configuration[%d] ConfigBitstreamRaw %d",
+        msg_Dbg(va, "configuration[%d] ConfigBitstreamRaw %d",
                 i, cfg->ConfigBitstreamRaw);
 
         /* */
         int score;
         if (cfg->ConfigBitstreamRaw == 1)
             score = 1;
-        else if (codec_id == CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
+        else if (codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
             score = 2;
         else
             continue;
@@ -952,33 +956,34 @@ static int DxCreateVideoDecoder(vlc_va_dxva2_t *va,
             score += 16;
 
         if (cfg_score < score) {
-            va->cfg = *cfg;
+            sys->cfg = *cfg;
             cfg_score = score;
         }
     }
     CoTaskMemFree(cfg_list);
     if (cfg_score <= 0) {
-        msg_Err(va->log, "Failed to find a supported decoder configuration");
+        msg_Err(va, "Failed to find a supported decoder configuration");
         return VLC_EGENERIC;
     }
 
     /* Create the decoder */
     IDirectXVideoDecoder *decoder;
-    if (FAILED(IDirectXVideoDecoderService_CreateVideoDecoder(va->vs,
-                                                              &va->input,
+    if (FAILED(IDirectXVideoDecoderService_CreateVideoDecoder(sys->vs,
+                                                              &sys->input,
                                                               &dsc,
-                                                              &va->cfg,
-                                                              surface_list,
-                                                              va->surface_count,
+                                                              &sys->cfg,
+                                                              sys->hw_surface,
+                                                              sys->surface_count,
                                                               &decoder))) {
-        msg_Err(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder failed");
+        msg_Err(va, "IDirectXVideoDecoderService_CreateVideoDecoder failed");
         return VLC_EGENERIC;
     }
-    va->decoder = decoder;
-    msg_Dbg(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder succeed");
+    sys->decoder = decoder;
+    msg_Dbg(va, "IDirectXVideoDecoderService_CreateVideoDecoder succeed");
     return VLC_SUCCESS;
 }
-static void DxDestroyVideoDecoder(vlc_va_dxva2_t *va)
+
+static void DxDestroyVideoDecoder(vlc_va_sys_t *va)
 {
     if (va->decoder)
         IDirectXVideoDecoder_Release(va->decoder);
@@ -988,16 +993,18 @@ static void DxDestroyVideoDecoder(vlc_va_dxva2_t *va)
         IDirect3DSurface9_Release(va->surface[i].d3d);
     va->surface_count = 0;
 }
-static int DxResetVideoDecoder(vlc_va_dxva2_t *va)
+
+static int DxResetVideoDecoder(vlc_va_t *va)
 {
-    msg_Err(va->log, "DxResetVideoDecoder unimplemented");
+    msg_Err(va, "DxResetVideoDecoder unimplemented");
     return VLC_EGENERIC;
 }
 
-static void DxCreateVideoConversion(vlc_va_dxva2_t *va)
+static void DxCreateVideoConversion(vlc_va_sys_t *va)
 {
     switch (va->render) {
     case MAKEFOURCC('N','V','1','2'):
+    case MAKEFOURCC('I','M','C','3'):
         va->output = MAKEFOURCC('Y','V','1','2');
         break;
     default:
@@ -1006,15 +1013,8 @@ static void DxCreateVideoConversion(vlc_va_dxva2_t *va)
     }
     CopyInitCache(&va->surface_cache, va->surface_width);
 }
-static void DxDestroyVideoConversion(vlc_va_dxva2_t *va)
+
+static void DxDestroyVideoConversion(vlc_va_sys_t *va)
 {
     CopyCleanCache(&va->surface_cache);
 }
-#else
-vlc_va_t *vlc_va_NewDxva2(vlc_object_t *log, int codec_id)
-{
-    (void)log;
-    (void)codec_id;
-    return NULL;
-}
-#endif