codec: dxva: Pretend we're always building on desktop
[vlc.git] / modules / codec / avcodec / directx_va.c
1 /*****************************************************************************
2  * directx_va.c: DirectX Generic Video Acceleration helpers
3  *****************************************************************************
4  * Copyright (C) 2009 Geoffroy Couprie
5  * Copyright (C) 2009 Laurent Aimar
6  * Copyright (C) 2015 Steve Lhomme
7  * $Id$
8  *
9  * Authors: Geoffroy Couprie <geal@videolan.org>
10  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
11  *          Steve Lhomme <robux4@gmail.com>
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation; either version 2.1 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <assert.h>
32
33 #include <vlc_common.h>
34 #include <vlc_codecs.h>
35 #include <vlc_codec.h>
36
37 #define COBJMACROS
38
39 #include "directx_va.h"
40
41 #include "avcodec.h"
42 #include "../../packetizer/h264_nal.h"
43 #include "../../packetizer/hevc_nal.h"
44
45 static const int PROF_MPEG2_SIMPLE[] = { FF_PROFILE_MPEG2_SIMPLE, 0 };
46 static const int PROF_MPEG2_MAIN[]   = { FF_PROFILE_MPEG2_SIMPLE,
47                                          FF_PROFILE_MPEG2_MAIN, 0 };
48 static const int PROF_H264_HIGH[]    = { FF_PROFILE_H264_CONSTRAINED_BASELINE,
49                                          FF_PROFILE_H264_MAIN,
50                                          FF_PROFILE_H264_HIGH, 0 };
51 static const int PROF_HEVC_MAIN[]    = { FF_PROFILE_HEVC_MAIN, 0 };
52 static const int PROF_HEVC_MAIN10[]  = { FF_PROFILE_HEVC_MAIN,
53                                          FF_PROFILE_HEVC_MAIN_10, 0 };
54
55 #include <winapifamily.h>
56 #if defined(WINAPI_FAMILY)
57 # undef WINAPI_FAMILY
58 #endif
59 #define WINAPI_FAMILY WINAPI_PARTITION_DESKTOP
60
61 #include <d3d9.h>
62 #include <dxva2api.h>
63
64 #include <initguid.h> /* must be last included to not redefine existing GUIDs */
65
66 /* dxva2api.h GUIDs: http://msdn.microsoft.com/en-us/library/windows/desktop/ms697067(v=vs100).aspx
67  * assume that they are declared in dxva2api.h */
68 #define MS_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)
69
70 #ifdef __MINGW32__
71 # include <_mingw.h>
72
73 # if !defined(__MINGW64_VERSION_MAJOR)
74 #  undef MS_GUID
75 #  define MS_GUID DEFINE_GUID /* dxva2api.h fails to declare those, redefine as static */
76 #  define DXVA2_E_NEW_VIDEO_DEVICE MAKE_HRESULT(1, 4, 4097)
77 # else
78 #  include <dxva.h>
79 # endif
80
81 #endif /* __MINGW32__ */
82
83 /* Codec capabilities GUID, sorted by codec */
84 MS_GUID    (DXVA2_ModeMPEG2_MoComp,                 0xe6a9f44b, 0x61b0, 0x4563, 0x9e, 0xa4, 0x63, 0xd2, 0xa3, 0xc6, 0xfe, 0x66);
85 MS_GUID    (DXVA2_ModeMPEG2_IDCT,                   0xbf22ad00, 0x03ea, 0x4690, 0x80, 0x77, 0x47, 0x33, 0x46, 0x20, 0x9b, 0x7e);
86 MS_GUID    (DXVA2_ModeMPEG2_VLD,                    0xee27417f, 0x5e28, 0x4e65, 0xbe, 0xea, 0x1d, 0x26, 0xb5, 0x08, 0xad, 0xc9);
87 DEFINE_GUID(DXVA_ModeMPEG1_A,                       0x1b81be09, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
88 DEFINE_GUID(DXVA_ModeMPEG2_A,                       0x1b81be0A, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
89 DEFINE_GUID(DXVA_ModeMPEG2_B,                       0x1b81be0B, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
90 DEFINE_GUID(DXVA_ModeMPEG2_C,                       0x1b81be0C, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
91 DEFINE_GUID(DXVA_ModeMPEG2_D,                       0x1b81be0D, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
92 DEFINE_GUID(DXVA2_ModeMPEG2and1_VLD,                0x86695f12, 0x340e, 0x4f04, 0x9f, 0xd3, 0x92, 0x53, 0xdd, 0x32, 0x74, 0x60);
93 DEFINE_GUID(DXVA2_ModeMPEG1_VLD,                    0x6f3ec719, 0x3735, 0x42cc, 0x80, 0x63, 0x65, 0xcc, 0x3c, 0xb3, 0x66, 0x16);
94
95 MS_GUID    (DXVA2_ModeH264_A,                       0x1b81be64, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
96 MS_GUID    (DXVA2_ModeH264_B,                       0x1b81be65, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
97 MS_GUID    (DXVA2_ModeH264_C,                       0x1b81be66, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
98 MS_GUID    (DXVA2_ModeH264_D,                       0x1b81be67, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
99 MS_GUID    (DXVA2_ModeH264_E,                       0x1b81be68, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
100 MS_GUID    (DXVA2_ModeH264_F,                       0x1b81be69, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
101 DEFINE_GUID(DXVA_ModeH264_VLD_Multiview,            0x9901CCD3, 0xca12, 0x4b7e, 0x86, 0x7a, 0xe2, 0x22, 0x3d, 0x92, 0x55, 0xc3); // MVC
102 DEFINE_GUID(DXVA_ModeH264_VLD_WithFMOASO_NoFGT,     0xd5f04ff9, 0x3418, 0x45d8, 0x95, 0x61, 0x32, 0xa7, 0x6a, 0xae, 0x2d, 0xdd);
103 DEFINE_GUID(DXVADDI_Intel_ModeH264_A,               0x604F8E64, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);
104 DEFINE_GUID(DXVADDI_Intel_ModeH264_C,               0x604F8E66, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);
105 DEFINE_GUID(DXVA_Intel_H264_NoFGT_ClearVideo,       0x604F8E68, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);
106 DEFINE_GUID(DXVA_ModeH264_VLD_NoFGT_Flash,          0x4245F676, 0x2BBC, 0x4166, 0xa0, 0xBB, 0x54, 0xE7, 0xB8, 0x49, 0xC3, 0x80);
107
108 MS_GUID    (DXVA2_ModeWMV8_A,                       0x1b81be80, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
109 MS_GUID    (DXVA2_ModeWMV8_B,                       0x1b81be81, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
110
111 MS_GUID    (DXVA2_ModeWMV9_A,                       0x1b81be90, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
112 MS_GUID    (DXVA2_ModeWMV9_B,                       0x1b81be91, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
113 MS_GUID    (DXVA2_ModeWMV9_C,                       0x1b81be94, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
114
115 MS_GUID    (DXVA2_ModeVC1_A,                        0x1b81beA0, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
116 MS_GUID    (DXVA2_ModeVC1_B,                        0x1b81beA1, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
117 MS_GUID    (DXVA2_ModeVC1_C,                        0x1b81beA2, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
118 MS_GUID    (DXVA2_ModeVC1_D,                        0x1b81beA3, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
119 DEFINE_GUID(DXVA2_ModeVC1_D2010,                    0x1b81beA4, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5); // August 2010 update
120 DEFINE_GUID(DXVA_Intel_VC1_ClearVideo,              0xBCC5DB6D, 0xA2B6, 0x4AF0, 0xAC, 0xE4, 0xAD, 0xB1, 0xF7, 0x87, 0xBC, 0x89);
121 DEFINE_GUID(DXVA_Intel_VC1_ClearVideo_2,            0xE07EC519, 0xE651, 0x4CD6, 0xAC, 0x84, 0x13, 0x70, 0xCC, 0xEE, 0xC8, 0x51);
122
123 DEFINE_GUID(DXVA_nVidia_MPEG4_ASP,                  0x9947EC6F, 0x689B, 0x11DC, 0xA3, 0x20, 0x00, 0x19, 0xDB, 0xBC, 0x41, 0x84);
124 DEFINE_GUID(DXVA_ModeMPEG4pt2_VLD_Simple,           0xefd64d74, 0xc9e8, 0x41d7, 0xa5, 0xe9, 0xe9, 0xb0, 0xe3, 0x9f, 0xa3, 0x19);
125 DEFINE_GUID(DXVA_ModeMPEG4pt2_VLD_AdvSimple_NoGMC,  0xed418a9f, 0x010d, 0x4eda, 0x9a, 0xe3, 0x9a, 0x65, 0x35, 0x8d, 0x8d, 0x2e);
126 DEFINE_GUID(DXVA_ModeMPEG4pt2_VLD_AdvSimple_GMC,    0xab998b5b, 0x4258, 0x44a9, 0x9f, 0xeb, 0x94, 0xe5, 0x97, 0xa6, 0xba, 0xae);
127 DEFINE_GUID(DXVA_ModeMPEG4pt2_VLD_AdvSimple_Avivo,  0x7C74ADC6, 0xe2ba, 0x4ade, 0x86, 0xde, 0x30, 0xbe, 0xab, 0xb4, 0x0c, 0xc1);
128
129 DEFINE_GUID(DXVA_ModeHEVC_VLD_Main,                 0x5b11d51b, 0x2f4c, 0x4452,0xbc,0xc3,0x09,0xf2,0xa1,0x16,0x0c,0xc0);
130 DEFINE_GUID(DXVA_ModeHEVC_VLD_Main10,               0x107af0e0, 0xef1a, 0x4d19,0xab,0xa8,0x67,0xa1,0x63,0x07,0x3d,0x13);
131
132 DEFINE_GUID(DXVA_ModeH264_VLD_Stereo_Progressive_NoFGT,     0xd79be8da, 0x0cf1, 0x4c81,0xb8,0x2a,0x69,0xa4,0xe2,0x36,0xf4,0x3d);
133 DEFINE_GUID(DXVA_ModeH264_VLD_Stereo_NoFGT,                 0xf9aaccbb, 0xc2b6, 0x4cfc,0x87,0x79,0x57,0x07,0xb1,0x76,0x05,0x52);
134 DEFINE_GUID(DXVA_ModeH264_VLD_Multiview_NoFGT,              0x705b9d82, 0x76cf, 0x49d6,0xb7,0xe6,0xac,0x88,0x72,0xdb,0x01,0x3c);
135
136 DEFINE_GUID(DXVA_ModeH264_VLD_SVC_Scalable_Baseline,                    0xc30700c4, 0xe384, 0x43e0, 0xb9, 0x82, 0x2d, 0x89, 0xee, 0x7f, 0x77, 0xc4);
137 DEFINE_GUID(DXVA_ModeH264_VLD_SVC_Restricted_Scalable_Baseline,         0x9b8175d4, 0xd670, 0x4cf2, 0xa9, 0xf0, 0xfa, 0x56, 0xdf, 0x71, 0xa1, 0xae);
138 DEFINE_GUID(DXVA_ModeH264_VLD_SVC_Scalable_High,                        0x728012c9, 0x66a8, 0x422f, 0x97, 0xe9, 0xb5, 0xe3, 0x9b, 0x51, 0xc0, 0x53);
139 DEFINE_GUID(DXVA_ModeH264_VLD_SVC_Restricted_Scalable_High_Progressive, 0x8efa5926, 0xbd9e, 0x4b04, 0x8b, 0x72, 0x8f, 0x97, 0x7d, 0xc4, 0x4c, 0x36);
140
141 DEFINE_GUID(DXVA_ModeH261_A,                        0x1b81be01, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
142 DEFINE_GUID(DXVA_ModeH261_B,                        0x1b81be02, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
143
144 DEFINE_GUID(DXVA_ModeH263_A,                        0x1b81be03, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
145 DEFINE_GUID(DXVA_ModeH263_B,                        0x1b81be04, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
146 DEFINE_GUID(DXVA_ModeH263_C,                        0x1b81be05, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
147 DEFINE_GUID(DXVA_ModeH263_D,                        0x1b81be06, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
148 DEFINE_GUID(DXVA_ModeH263_E,                        0x1b81be07, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
149 DEFINE_GUID(DXVA_ModeH263_F,                        0x1b81be08, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
150
151 DEFINE_GUID(DXVA_ModeVP8_VLD,                       0x90b899ea, 0x3a62, 0x4705, 0x88, 0xb3, 0x8d, 0xf0, 0x4b, 0x27, 0x44, 0xe7);
152 DEFINE_GUID(DXVA_ModeVP9_VLD_Profile0,              0x463707f8, 0xa1d0, 0x4585, 0x87, 0x6d, 0x83, 0xaa, 0x6d, 0x60, 0xb8, 0x9e);
153
154 typedef struct {
155     const char   *name;
156     const GUID   *guid;
157     int          codec;
158     const int    *p_profiles; // NULL or ends with 0
159 } directx_va_mode_t;
160
161 /* XXX Prefered modes must come first */
162 static const directx_va_mode_t DXVA_MODES[] = {
163     /* MPEG-1/2 */
164     { "MPEG-1 decoder, restricted profile A",                                         &DXVA_ModeMPEG1_A,                      0, NULL },
165     { "MPEG-2 decoder, restricted profile A",                                         &DXVA_ModeMPEG2_A,                      0, NULL },
166     { "MPEG-2 decoder, restricted profile B",                                         &DXVA_ModeMPEG2_B,                      0, NULL },
167     { "MPEG-2 decoder, restricted profile C",                                         &DXVA_ModeMPEG2_C,                      0, NULL },
168     { "MPEG-2 decoder, restricted profile D",                                         &DXVA_ModeMPEG2_D,                      0, NULL },
169
170     { "MPEG-2 variable-length decoder",                                               &DXVA2_ModeMPEG2_VLD,                   AV_CODEC_ID_MPEG2VIDEO, PROF_MPEG2_SIMPLE },
171     { "MPEG-2 & MPEG-1 variable-length decoder",                                      &DXVA2_ModeMPEG2and1_VLD,               AV_CODEC_ID_MPEG2VIDEO, PROF_MPEG2_MAIN },
172     { "MPEG-2 & MPEG-1 variable-length decoder",                                      &DXVA2_ModeMPEG2and1_VLD,               AV_CODEC_ID_MPEG1VIDEO, NULL },
173     { "MPEG-2 motion compensation",                                                   &DXVA2_ModeMPEG2_MoComp,                0, NULL },
174     { "MPEG-2 inverse discrete cosine transform",                                     &DXVA2_ModeMPEG2_IDCT,                  0, NULL },
175
176     /* MPEG-1 http://download.microsoft.com/download/B/1/7/B172A3C8-56F2-4210-80F1-A97BEA9182ED/DXVA_MPEG1_VLD.pdf */
177     { "MPEG-1 variable-length decoder, no D pictures",                                &DXVA2_ModeMPEG1_VLD,                   0, NULL },
178
179     /* H.264 http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=3d1c290b-310b-4ea2-bf76-714063a6d7a6 */
180     { "H.264 variable-length decoder, film grain technology",                         &DXVA2_ModeH264_F,                      AV_CODEC_ID_H264, PROF_H264_HIGH },
181     { "H.264 variable-length decoder, no film grain technology (Intel ClearVideo)",   &DXVA_Intel_H264_NoFGT_ClearVideo,      AV_CODEC_ID_H264, PROF_H264_HIGH },
182     { "H.264 variable-length decoder, no film grain technology",                      &DXVA2_ModeH264_E,                      AV_CODEC_ID_H264, PROF_H264_HIGH },
183     { "H.264 variable-length decoder, no film grain technology, FMO/ASO",             &DXVA_ModeH264_VLD_WithFMOASO_NoFGT,    AV_CODEC_ID_H264, PROF_H264_HIGH },
184     { "H.264 variable-length decoder, no film grain technology, Flash",               &DXVA_ModeH264_VLD_NoFGT_Flash,         AV_CODEC_ID_H264, PROF_H264_HIGH },
185
186     { "H.264 inverse discrete cosine transform, film grain technology",               &DXVA2_ModeH264_D,                      0, NULL },
187     { "H.264 inverse discrete cosine transform, no film grain technology",            &DXVA2_ModeH264_C,                      0, NULL },
188     { "H.264 inverse discrete cosine transform, no film grain technology (Intel)",    &DXVADDI_Intel_ModeH264_C,              0, NULL },
189
190     { "H.264 motion compensation, film grain technology",                             &DXVA2_ModeH264_B,                      0, NULL },
191     { "H.264 motion compensation, no film grain technology",                          &DXVA2_ModeH264_A,                      0, NULL },
192     { "H.264 motion compensation, no film grain technology (Intel)",                  &DXVADDI_Intel_ModeH264_A,              0, NULL },
193
194     /* http://download.microsoft.com/download/2/D/0/2D02E72E-7890-430F-BA91-4A363F72F8C8/DXVA_H264_MVC.pdf */
195     { "H.264 stereo high profile, mbs flag set",                                      &DXVA_ModeH264_VLD_Stereo_Progressive_NoFGT, 0, NULL },
196     { "H.264 stereo high profile",                                                    &DXVA_ModeH264_VLD_Stereo_NoFGT,             0, NULL },
197     { "H.264 multiview high profile",                                                 &DXVA_ModeH264_VLD_Multiview_NoFGT,          0, NULL },
198
199     /* SVC http://download.microsoft.com/download/C/8/A/C8AD9F1B-57D1-4C10-85A0-09E3EAC50322/DXVA_SVC_2012_06.pdf */
200     { "H.264 scalable video coding, Scalable Baseline Profile",                       &DXVA_ModeH264_VLD_SVC_Scalable_Baseline,            0, NULL },
201     { "H.264 scalable video coding, Scalable Constrained Baseline Profile",           &DXVA_ModeH264_VLD_SVC_Restricted_Scalable_Baseline, 0, NULL },
202     { "H.264 scalable video coding, Scalable High Profile",                           &DXVA_ModeH264_VLD_SVC_Scalable_High,                0, NULL },
203     { "H.264 scalable video coding, Scalable Constrained High Profile",               &DXVA_ModeH264_VLD_SVC_Restricted_Scalable_High_Progressive, 0, NULL },
204
205     /* WMV */
206     { "Windows Media Video 8 motion compensation",                                    &DXVA2_ModeWMV8_B,                      0, NULL },
207     { "Windows Media Video 8 post processing",                                        &DXVA2_ModeWMV8_A,                      0, NULL },
208
209     { "Windows Media Video 9 IDCT",                                                   &DXVA2_ModeWMV9_C,                      0, NULL },
210     { "Windows Media Video 9 motion compensation",                                    &DXVA2_ModeWMV9_B,                      0, NULL },
211     { "Windows Media Video 9 post processing",                                        &DXVA2_ModeWMV9_A,                      0, NULL },
212
213     /* VC-1 */
214     { "VC-1 variable-length decoder",                                                 &DXVA2_ModeVC1_D,                       AV_CODEC_ID_VC1, NULL },
215     { "VC-1 variable-length decoder",                                                 &DXVA2_ModeVC1_D,                       AV_CODEC_ID_WMV3, NULL },
216     { "VC-1 variable-length decoder",                                                 &DXVA2_ModeVC1_D2010,                   AV_CODEC_ID_VC1, NULL },
217     { "VC-1 variable-length decoder",                                                 &DXVA2_ModeVC1_D2010,                   AV_CODEC_ID_WMV3, NULL },
218     { "VC-1 variable-length decoder 2 (Intel)",                                       &DXVA_Intel_VC1_ClearVideo_2,           0, NULL },
219     { "VC-1 variable-length decoder (Intel)",                                         &DXVA_Intel_VC1_ClearVideo,             0, NULL },
220
221     { "VC-1 inverse discrete cosine transform",                                       &DXVA2_ModeVC1_C,                       0, NULL },
222     { "VC-1 motion compensation",                                                     &DXVA2_ModeVC1_B,                       0, NULL },
223     { "VC-1 post processing",                                                         &DXVA2_ModeVC1_A,                       0, NULL },
224
225     /* Xvid/Divx: TODO */
226     { "MPEG-4 Part 2 nVidia bitstream decoder",                                       &DXVA_nVidia_MPEG4_ASP,                 0, NULL },
227     { "MPEG-4 Part 2 variable-length decoder, Simple Profile",                        &DXVA_ModeMPEG4pt2_VLD_Simple,          0, NULL },
228     { "MPEG-4 Part 2 variable-length decoder, Simple&Advanced Profile, no GMC",       &DXVA_ModeMPEG4pt2_VLD_AdvSimple_NoGMC, 0, NULL },
229     { "MPEG-4 Part 2 variable-length decoder, Simple&Advanced Profile, GMC",          &DXVA_ModeMPEG4pt2_VLD_AdvSimple_GMC,   0, NULL },
230     { "MPEG-4 Part 2 variable-length decoder, Simple&Advanced Profile, Avivo",        &DXVA_ModeMPEG4pt2_VLD_AdvSimple_Avivo, 0, NULL },
231
232     /* HEVC */
233     { "HEVC Main profile",                                                            &DXVA_ModeHEVC_VLD_Main,                AV_CODEC_ID_HEVC, PROF_HEVC_MAIN },
234     { "HEVC Main 10 profile",                                                         &DXVA_ModeHEVC_VLD_Main10,              AV_CODEC_ID_HEVC, PROF_HEVC_MAIN10 },
235
236     /* H.261 */
237     { "H.261 decoder, restricted profile A",                                          &DXVA_ModeH261_A,                       0, NULL },
238     { "H.261 decoder, restricted profile B",                                          &DXVA_ModeH261_B,                       0, NULL },
239
240     /* H.263 */
241     { "H.263 decoder, restricted profile A",                                          &DXVA_ModeH263_A,                       0, NULL },
242     { "H.263 decoder, restricted profile B",                                          &DXVA_ModeH263_B,                       0, NULL },
243     { "H.263 decoder, restricted profile C",                                          &DXVA_ModeH263_C,                       0, NULL },
244     { "H.263 decoder, restricted profile D",                                          &DXVA_ModeH263_D,                       0, NULL },
245     { "H.263 decoder, restricted profile E",                                          &DXVA_ModeH263_E,                       0, NULL },
246     { "H.263 decoder, restricted profile F",                                          &DXVA_ModeH263_F,                       0, NULL },
247
248     /* VPx */
249     { "VP8",                                                                          &DXVA_ModeVP8_VLD,                      0, NULL },
250 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( 57, 17, 100 ) && LIBAVCODEC_VERSION_MICRO >= 100
251     { "VP9 profile 0",                                                                &DXVA_ModeVP9_VLD_Profile0,             AV_CODEC_ID_VP9, NULL },
252 #else
253     { "VP9 profile 0",                                                                &DXVA_ModeVP9_VLD_Profile0,             0, NULL },
254 #endif
255
256     { NULL, NULL, 0, NULL }
257 };
258
259 static int FindVideoServiceConversion(vlc_va_t *, directx_sys_t *, const es_format_t *fmt);
260 static void DestroyVideoDecoder(vlc_va_t *, directx_sys_t *);
261 static void DestroyVideoService(vlc_va_t *, directx_sys_t *);
262 static void DestroyDeviceManager(vlc_va_t *, directx_sys_t *);
263 static void DestroyDevice(vlc_va_t *, directx_sys_t *);
264
265 char *directx_va_GetDecoderName(const GUID *guid)
266 {
267     for (unsigned i = 0; DXVA_MODES[i].name; i++) {
268         if (IsEqualGUID(DXVA_MODES[i].guid, guid))
269             return strdup(DXVA_MODES[i].name);
270     }
271
272     char *psz_name = malloc(36);
273     if (likely(psz_name))
274         asprintf(&psz_name, "Unknown decoder " GUID_FMT, GUID_PRINT(*guid));
275     return psz_name;
276 }
277
278 /* */
279 int directx_va_Setup(vlc_va_t *va, directx_sys_t *dx_sys, AVCodecContext *avctx)
280 {
281     int surface_alignment = 16;
282     int surface_count = 2;
283
284     if (dx_sys->width == avctx->coded_width && dx_sys->height == avctx->coded_height
285      && dx_sys->decoder != NULL)
286         goto ok;
287
288     /* */
289     DestroyVideoDecoder(va, dx_sys);
290
291     avctx->hwaccel_context = NULL;
292     if (avctx->coded_width <= 0 || avctx->coded_height <= 0)
293         return VLC_EGENERIC;
294
295     /* */
296     msg_Dbg(va, "directx_va_Setup id %d %dx%d", dx_sys->codec_id, avctx->coded_width, avctx->coded_height);
297
298     switch ( dx_sys->codec_id )
299     {
300     case AV_CODEC_ID_MPEG2VIDEO:
301         /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
302            but it causes issues for H.264 on certain AMD GPUs..... */
303         surface_alignment = 32;
304         surface_count += 2;
305         break;
306     case AV_CODEC_ID_HEVC:
307         /* the HEVC DXVA2 spec asks for 128 pixel aligned surfaces to ensure
308            all coding features have enough room to work with */
309         surface_alignment = 128;
310         surface_count += 16;
311         break;
312     case AV_CODEC_ID_H264:
313         surface_count += 16;
314         break;
315     default:
316         surface_count += 2;
317     }
318
319     if ( avctx->active_thread_type & FF_THREAD_FRAME )
320         surface_count += dx_sys->thread_count;
321
322     if (surface_count > MAX_SURFACE_COUNT)
323         return VLC_EGENERIC;
324
325     dx_sys->surface_count = surface_count;
326
327 #define ALIGN(x, y) (((x) + ((y) - 1)) & ~((y) - 1))
328     dx_sys->width  = avctx->coded_width;
329     dx_sys->height = avctx->coded_height;
330     dx_sys->surface_width  = ALIGN(dx_sys->width, surface_alignment);
331     dx_sys->surface_height = ALIGN(dx_sys->height, surface_alignment);
332
333     /* FIXME transmit a video_format_t by VaSetup directly */
334     video_format_t fmt;
335     memset(&fmt, 0, sizeof(fmt));
336     fmt.i_width = dx_sys->width;
337     fmt.i_height = dx_sys->height;
338     fmt.i_frame_rate = avctx->framerate.num;
339     fmt.i_frame_rate_base = avctx->framerate.den;
340
341     if (dx_sys->pf_create_decoder_surfaces(va, dx_sys->codec_id, &fmt))
342         return VLC_EGENERIC;
343
344     if (avctx->coded_width != dx_sys->surface_width ||
345         avctx->coded_height != dx_sys->surface_height)
346         msg_Warn( va, "surface dimensions (%dx%d) differ from avcodec dimensions (%dx%d)",
347                   dx_sys->surface_width, dx_sys->surface_height,
348                   avctx->coded_width, avctx->coded_height);
349
350     dx_sys->pf_setup_avcodec_ctx(va);
351
352     for (int i = 0; i < dx_sys->surface_count; i++) {
353         vlc_va_surface_t *surface = &dx_sys->surface[i];
354         surface->refcount = 0;
355         surface->order = 0;
356         surface->p_lock = &dx_sys->surface_lock;
357         surface->p_pic = dx_sys->pf_alloc_surface_pic(va, &fmt, i);
358         if (unlikely(surface->p_pic == NULL))
359             return VLC_EGENERIC;
360     }
361
362 ok:
363     return VLC_SUCCESS;
364 }
365
366 void DestroyVideoDecoder(vlc_va_t *va, directx_sys_t *dx_sys)
367 {
368     dx_sys->pf_destroy_surfaces(va);
369
370     for (int i = 0; i < dx_sys->surface_count; i++)
371         IUnknown_Release( dx_sys->hw_surface[i] );
372
373     for (int i = 0; i < dx_sys->surface_count; i++)
374         if (dx_sys->surface[i].p_pic)
375             picture_Release(dx_sys->surface[i].p_pic);
376
377     if (dx_sys->decoder)
378         IUnknown_Release( dx_sys->decoder );
379
380     dx_sys->decoder = NULL;
381     dx_sys->surface_count = 0;
382 }
383
384 /* FIXME it is nearly common with VAAPI */
385 int directx_va_Get(vlc_va_t *va, directx_sys_t *dx_sys, picture_t *pic, uint8_t **data)
386 {
387     /* Check the device */
388     if (dx_sys->pf_check_device(va)!=VLC_SUCCESS)
389         return VLC_EGENERIC;
390
391     vlc_mutex_lock( &dx_sys->surface_lock );
392
393     /* Grab the oldest unused surface, in case none are, use the oldest used one
394      * XXX using the used one is a workaround in case a problem happens with libavcodec */
395     int i, old = -1, old_used = -1;
396
397     for (i = 0; i < dx_sys->surface_count; i++) {
398         vlc_va_surface_t *surface = &dx_sys->surface[i];
399         if (((old == -1 || surface->order < dx_sys->surface[old].order)) && !surface->refcount)
400             old = i;
401         if (old_used == -1 || surface->order < dx_sys->surface[old_used].order)
402             old_used = i;
403     }
404     if (old >= 0)
405         i = old;
406     else if (old_used >= 0)
407     {
408         msg_Warn(va, "couldn't find a free decoding buffer, using index %d", old_used);
409         i = old_used;
410     }
411
412     vlc_va_surface_t *surface = &dx_sys->surface[i];
413
414     surface->refcount = 1;
415     surface->order = ++dx_sys->surface_order;
416     *data = (void *)dx_sys->hw_surface[i];
417     pic->context = surface;
418
419     vlc_mutex_unlock( &dx_sys->surface_lock );
420
421     return VLC_SUCCESS;
422 }
423
424 void directx_va_Release(void *opaque, uint8_t *data)
425 {
426     picture_t *pic = opaque;
427     vlc_va_surface_t *surface = pic->context;
428     vlc_mutex_lock( surface->p_lock );
429
430     surface->refcount--;
431     pic->context = NULL;
432     picture_Release(pic);
433
434     vlc_mutex_unlock( surface->p_lock );
435 }
436
437 void directx_va_Close(vlc_va_t *va, directx_sys_t *dx_sys)
438 {
439     DestroyVideoDecoder(va, dx_sys);
440     DestroyVideoService(va, dx_sys);
441     DestroyDeviceManager(va, dx_sys);
442     DestroyDevice(va, dx_sys);
443
444     if (dx_sys->hdecoder_dll)
445         FreeLibrary(dx_sys->hdecoder_dll);
446
447     vlc_mutex_destroy( &dx_sys->surface_lock );
448 }
449
450 int directx_va_Open(vlc_va_t *va, directx_sys_t *dx_sys,
451                     AVCodecContext *ctx, const es_format_t *fmt, bool b_dll)
452 {
453     // TODO va->sys = sys;
454     dx_sys->codec_id = ctx->codec_id;
455
456     vlc_mutex_init( &dx_sys->surface_lock );
457
458     if (b_dll) {
459         /* Load dll*/
460         dx_sys->hdecoder_dll = LoadLibrary(dx_sys->psz_decoder_dll);
461         if (!dx_sys->hdecoder_dll) {
462             msg_Warn(va, "cannot load DirectX decoder DLL");
463             goto error;
464         }
465         msg_Dbg(va, "DLLs loaded");
466     }
467
468     /* */
469     if (dx_sys->pf_create_device(va)) {
470         msg_Err(va, "Failed to create DirectX device");
471         goto error;
472     }
473     msg_Dbg(va, "CreateDevice succeed");
474
475     if (dx_sys->pf_create_device_manager(va)) {
476         msg_Err(va, "D3dCreateDeviceManager failed");
477         goto error;
478     }
479
480     if (dx_sys->pf_create_video_service(va)) {
481         msg_Err(va, "DxCreateVideoService failed");
482         goto error;
483     }
484
485     /* */
486     if (FindVideoServiceConversion(va, dx_sys, fmt)) {
487         msg_Err(va, "FindVideoServiceConversion failed");
488         goto error;
489     }
490
491     dx_sys->thread_count = ctx->thread_count;
492
493     return VLC_SUCCESS;
494
495 error:
496     return VLC_EGENERIC;
497 }
498
499 static bool profile_supported(const directx_va_mode_t *mode, const es_format_t *fmt)
500 {
501     bool is_supported = mode->p_profiles == NULL || !mode->p_profiles[0];
502     if (!is_supported)
503     {
504         int profile = fmt->i_profile;
505         if (mode->codec == AV_CODEC_ID_H264)
506         {
507             uint8_t h264_profile;
508             if ( h264_get_profile_level(fmt, &h264_profile, NULL, NULL) )
509                 profile = h264_profile;
510         }
511         if (mode->codec == AV_CODEC_ID_HEVC)
512         {
513             uint8_t hevc_profile;
514             if (hevc_get_profile_level(fmt, &hevc_profile, NULL, NULL) )
515                 profile = hevc_profile;
516         }
517
518         if (profile <= 0)
519             is_supported = true;
520         else for (const int *p_profile = &mode->p_profiles[0]; *p_profile; ++p_profile)
521         {
522             if (*p_profile == profile)
523             {
524                 is_supported = true;
525                 break;
526             }
527         }
528     }
529     return is_supported;
530 }
531
532 void DestroyVideoService(vlc_va_t *va, directx_sys_t *dx_sys)
533 {
534     dx_sys->pf_destroy_video_service(va);
535     if (dx_sys->d3ddec)
536         IUnknown_Release(dx_sys->d3ddec);
537 }
538
539 /**
540  * Find the best suited decoder mode GUID and render format.
541  */
542 static int FindVideoServiceConversion(vlc_va_t *va, directx_sys_t *dx_sys, const es_format_t *fmt)
543 {
544     input_list_t p_list = { 0 };
545     int err = dx_sys->pf_get_input_list(va, &p_list);
546     if (err != VLC_SUCCESS)
547         return err;
548     if (p_list.count == 0) {
549         msg_Warn( va, "No input format found for HWAccel" );
550         return VLC_EGENERIC;
551     }
552
553     err = VLC_EGENERIC;
554     /* Retreive supported modes from the decoder service */
555     for (unsigned i = 0; i < p_list.count; i++) {
556         const GUID *g = &p_list.list[i];
557         char *psz_decoder_name = directx_va_GetDecoderName(g);
558         msg_Dbg(va, "- '%s' is supported", psz_decoder_name);
559         free(psz_decoder_name);
560     }
561
562     /* Try all supported mode by our priority */
563     const directx_va_mode_t *mode = DXVA_MODES;
564     for (; mode->name; ++mode) {
565         if (!mode->codec || mode->codec != dx_sys->codec_id)
566             continue;
567
568         /* */
569         bool is_supported = false;
570         for (const GUID *g = &p_list.list[0]; !is_supported && g < &p_list.list[p_list.count]; g++) {
571             is_supported = IsEqualGUID(mode->guid, g);
572         }
573         if ( is_supported )
574         {
575             is_supported = profile_supported( mode, fmt );
576             if (!is_supported)
577                 msg_Warn( va, "Unsupported profile %d for %s ",
578                           fmt->i_profile, directx_va_GetDecoderName(mode->guid) );
579         }
580         if (!is_supported)
581             continue;
582
583         /* */
584         msg_Dbg(va, "Trying to use '%s' as input", mode->name);
585         if (dx_sys->pf_setup_output(va, mode->guid, &fmt->video)==VLC_SUCCESS)
586         {
587             dx_sys->input = *mode->guid;
588             err = VLC_SUCCESS;
589             break;
590         }
591     }
592
593     p_list.pf_release(&p_list);
594     return err;
595 }
596
597 void DestroyDeviceManager(vlc_va_t *va, directx_sys_t *dx_sys)
598 {
599     dx_sys->pf_destroy_device_manager(va);
600 }
601
602 void DestroyDevice(vlc_va_t *va, directx_sys_t *dx_sys)
603 {
604     dx_sys->pf_destroy_device(va);
605     if (dx_sys->d3ddev)
606         IUnknown_Release( dx_sys->d3ddev );
607 }