amfenc: Add DXVA2 hardware frame input support
[ffmpeg.git] / libavcodec / dxva2.c
1 /*
2  * DXVA2 HW acceleration.
3  *
4  * copyright (c) 2010 Laurent Aimar
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include <assert.h>
24 #include <string.h>
25 #include <initguid.h>
26
27 #include "libavutil/common.h"
28 #include "libavutil/log.h"
29 #include "libavutil/time.h"
30
31 #include "avcodec.h"
32 #include "decode.h"
33 #include "dxva2_internal.h"
34
35 /* define all the GUIDs used directly here,
36  to avoid problems with inconsistent dxva2api.h versions in mingw-w64 and different MSVC version */
37 DEFINE_GUID(ff_DXVA2_ModeMPEG2_VLD,      0xee27417f, 0x5e28,0x4e65,0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9);
38 DEFINE_GUID(ff_DXVA2_ModeMPEG2and1_VLD,  0x86695f12, 0x340e,0x4f04,0x9f,0xd3,0x92,0x53,0xdd,0x32,0x74,0x60);
39 DEFINE_GUID(ff_DXVA2_ModeH264_E,         0x1b81be68, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
40 DEFINE_GUID(ff_DXVA2_ModeH264_F,         0x1b81be69, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
41 DEFINE_GUID(ff_DXVADDI_Intel_ModeH264_E, 0x604F8E68, 0x4951,0x4C54,0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6);
42 DEFINE_GUID(ff_DXVA2_ModeVC1_D,          0x1b81beA3, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
43 DEFINE_GUID(ff_DXVA2_ModeVC1_D2010,      0x1b81beA4, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
44 DEFINE_GUID(ff_DXVA2_ModeHEVC_VLD_Main,  0x5b11d51b, 0x2f4c,0x4452,0xbc,0xc3,0x09,0xf2,0xa1,0x16,0x0c,0xc0);
45 DEFINE_GUID(ff_DXVA2_ModeHEVC_VLD_Main10,0x107af0e0, 0xef1a,0x4d19,0xab,0xa8,0x67,0xa1,0x63,0x07,0x3d,0x13);
46 DEFINE_GUID(ff_DXVA2_ModeVP9_VLD_Profile0,0x463707f8,0xa1d0,0x4585,0x87,0x6d,0x83,0xaa,0x6d,0x60,0xb8,0x9e);
47 DEFINE_GUID(ff_DXVA2_ModeVP9_VLD_10bit_Profile2,0xa4c749ef,0x6ecf,0x48aa,0x84,0x48,0x50,0xa7,0xa1,0x16,0x5f,0xf7);
48 DEFINE_GUID(ff_DXVA2_NoEncrypt,          0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
49 DEFINE_GUID(ff_GUID_NULL,                0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
50 DEFINE_GUID(ff_IID_IDirectXVideoDecoderService, 0xfc51a551,0xd5e7,0x11d9,0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
51
52 typedef struct dxva_mode {
53     const GUID     *guid;
54     enum AVCodecID codec;
55     // List of supported profiles, terminated by a FF_PROFILE_UNKNOWN entry.
56     // If NULL, don't check profile.
57     const int      *profiles;
58 } dxva_mode;
59
60 static const int prof_mpeg2_main[]   = {FF_PROFILE_MPEG2_SIMPLE,
61                                         FF_PROFILE_MPEG2_MAIN,
62                                         FF_PROFILE_UNKNOWN};
63 static const int prof_h264_high[]    = {FF_PROFILE_H264_CONSTRAINED_BASELINE,
64                                         FF_PROFILE_H264_MAIN,
65                                         FF_PROFILE_H264_HIGH,
66                                         FF_PROFILE_UNKNOWN};
67 static const int prof_hevc_main[]    = {FF_PROFILE_HEVC_MAIN,
68                                         FF_PROFILE_UNKNOWN};
69 static const int prof_hevc_main10[]  = {FF_PROFILE_HEVC_MAIN_10,
70                                         FF_PROFILE_UNKNOWN};
71 static const int prof_vp9_profile0[] = {FF_PROFILE_VP9_0,
72                                         FF_PROFILE_UNKNOWN};
73 static const int prof_vp9_profile2[] = {FF_PROFILE_VP9_2,
74                                         FF_PROFILE_UNKNOWN};
75
76 static const dxva_mode dxva_modes[] = {
77     /* MPEG-2 */
78     { &ff_DXVA2_ModeMPEG2_VLD,       AV_CODEC_ID_MPEG2VIDEO, prof_mpeg2_main },
79     { &ff_DXVA2_ModeMPEG2and1_VLD,   AV_CODEC_ID_MPEG2VIDEO, prof_mpeg2_main },
80
81     /* H.264 */
82     { &ff_DXVA2_ModeH264_F,          AV_CODEC_ID_H264, prof_h264_high },
83     { &ff_DXVA2_ModeH264_E,          AV_CODEC_ID_H264, prof_h264_high },
84     /* Intel specific H.264 mode */
85     { &ff_DXVADDI_Intel_ModeH264_E,  AV_CODEC_ID_H264, prof_h264_high },
86
87     /* VC-1 / WMV3 */
88     { &ff_DXVA2_ModeVC1_D2010,       AV_CODEC_ID_VC1 },
89     { &ff_DXVA2_ModeVC1_D2010,       AV_CODEC_ID_WMV3 },
90     { &ff_DXVA2_ModeVC1_D,           AV_CODEC_ID_VC1 },
91     { &ff_DXVA2_ModeVC1_D,           AV_CODEC_ID_WMV3 },
92
93     /* HEVC/H.265 */
94     { &ff_DXVA2_ModeHEVC_VLD_Main10, AV_CODEC_ID_HEVC, prof_hevc_main10 },
95     { &ff_DXVA2_ModeHEVC_VLD_Main,   AV_CODEC_ID_HEVC, prof_hevc_main },
96
97     /* VP8/9 */
98     { &ff_DXVA2_ModeVP9_VLD_Profile0,       AV_CODEC_ID_VP9, prof_vp9_profile0 },
99     { &ff_DXVA2_ModeVP9_VLD_10bit_Profile2, AV_CODEC_ID_VP9, prof_vp9_profile2 },
100
101     { NULL,                          0 },
102 };
103
104 static int dxva_get_decoder_configuration(AVCodecContext *avctx,
105                                           const void *cfg_list,
106                                           unsigned cfg_count)
107 {
108     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
109     unsigned i, best_score = 0;
110     int best_cfg = -1;
111
112     for (i = 0; i < cfg_count; i++) {
113         unsigned score;
114         UINT ConfigBitstreamRaw;
115         GUID guidConfigBitstreamEncryption;
116
117 #if CONFIG_D3D11VA
118         if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
119             D3D11_VIDEO_DECODER_CONFIG *cfg = &((D3D11_VIDEO_DECODER_CONFIG *)cfg_list)[i];
120             ConfigBitstreamRaw = cfg->ConfigBitstreamRaw;
121             guidConfigBitstreamEncryption = cfg->guidConfigBitstreamEncryption;
122         }
123 #endif
124 #if CONFIG_DXVA2
125         if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
126             DXVA2_ConfigPictureDecode *cfg = &((DXVA2_ConfigPictureDecode *)cfg_list)[i];
127             ConfigBitstreamRaw = cfg->ConfigBitstreamRaw;
128             guidConfigBitstreamEncryption = cfg->guidConfigBitstreamEncryption;
129         }
130 #endif
131
132         if (ConfigBitstreamRaw == 1)
133             score = 1;
134         else if (avctx->codec_id == AV_CODEC_ID_H264 && ConfigBitstreamRaw == 2)
135             score = 2;
136         else
137             continue;
138         if (IsEqualGUID(&guidConfigBitstreamEncryption, &ff_DXVA2_NoEncrypt))
139             score += 16;
140         if (score > best_score) {
141             best_score = score;
142             best_cfg = i;
143         }
144     }
145
146     if (!best_score) {
147         av_log(avctx, AV_LOG_VERBOSE, "No valid decoder configuration available\n");
148         return AVERROR(EINVAL);
149     }
150
151     return best_cfg;
152 }
153
154 #if CONFIG_D3D11VA
155 static int d3d11va_validate_output(void *service, GUID guid, const void *surface_format)
156 {
157     HRESULT hr;
158     BOOL is_supported = FALSE;
159     hr = ID3D11VideoDevice_CheckVideoDecoderFormat((ID3D11VideoDevice *)service,
160                                                    &guid,
161                                                    *(DXGI_FORMAT *)surface_format,
162                                                    &is_supported);
163     return SUCCEEDED(hr) && is_supported;
164 }
165 #endif
166
167 #if CONFIG_DXVA2
168 static int dxva2_validate_output(void *decoder_service, GUID guid, const void *surface_format)
169 {
170     HRESULT hr;
171     int ret = 0;
172     unsigned j, target_count;
173     D3DFORMAT *target_list;
174     hr = IDirectXVideoDecoderService_GetDecoderRenderTargets((IDirectXVideoDecoderService *)decoder_service, &guid, &target_count, &target_list);
175     if (SUCCEEDED(hr)) {
176         for (j = 0; j < target_count; j++) {
177             const D3DFORMAT format = target_list[j];
178             if (format == *(D3DFORMAT *)surface_format) {
179                 ret = 1;
180                 break;
181             }
182         }
183         CoTaskMemFree(target_list);
184     }
185     return ret;
186 }
187 #endif
188
189 static int dxva_check_codec_compatibility(AVCodecContext *avctx, const dxva_mode *mode)
190 {
191     if (mode->codec != avctx->codec_id)
192             return 0;
193
194     if (mode->profiles && !(avctx->hwaccel_flags & AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH)) {
195         int i, found = 0;
196         for (i = 0; mode->profiles[i] != FF_PROFILE_UNKNOWN; i++) {
197             if (avctx->profile == mode->profiles[i]) {
198                 found = 1;
199                 break;
200             }
201         }
202         if (!found)
203             return 0;
204     }
205
206     return 1;
207 }
208
209 static void dxva_list_guids_debug(AVCodecContext *avctx, void *service,
210                                  unsigned guid_count, const GUID *guid_list)
211 {
212     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
213     int i;
214
215     av_log(avctx, AV_LOG_VERBOSE, "Decoder GUIDs reported as supported:\n");
216
217     for (i = 0; i < guid_count; i++) {
218         const GUID *guid = &guid_list[i];
219
220         av_log(avctx, AV_LOG_VERBOSE,
221              "{%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x}",
222              (unsigned) guid->Data1, guid->Data2, guid->Data3,
223              guid->Data4[0], guid->Data4[1],
224              guid->Data4[2], guid->Data4[3],
225              guid->Data4[4], guid->Data4[5],
226              guid->Data4[6], guid->Data4[7]);
227
228 #if CONFIG_D3D11VA
229         if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
230             DXGI_FORMAT format;
231             // We don't know the maximum valid DXGI_FORMAT, so use 200 as
232             // arbitrary upper bound (that could become outdated).
233             for (format = 0; format < 200; format++) {
234                 if (d3d11va_validate_output(service, *guid, &format))
235                     av_log(avctx, AV_LOG_VERBOSE, " %d", (int)format);
236             }
237         }
238 #endif
239 #if CONFIG_DXVA2
240         if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
241             const D3DFORMAT formats[] = {MKTAG('N', 'V', '1', '2'),
242                                          MKTAG('P', '0', '1', '0')};
243             int i;
244             for (i = 0; i < FF_ARRAY_ELEMS(formats); i++) {
245                 if (dxva2_validate_output(service, *guid, &formats[i]))
246                     av_log(avctx, AV_LOG_VERBOSE, " %d", i);
247             }
248         }
249 #endif
250         av_log(avctx, AV_LOG_VERBOSE, "\n");
251     }
252 }
253
254 static int dxva_get_decoder_guid(AVCodecContext *avctx, void *service, void *surface_format,
255                                  unsigned guid_count, const GUID *guid_list, GUID *decoder_guid)
256 {
257     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
258     unsigned i, j;
259
260     dxva_list_guids_debug(avctx, service, guid_count, guid_list);
261
262     *decoder_guid = ff_GUID_NULL;
263     for (i = 0; dxva_modes[i].guid; i++) {
264         const dxva_mode *mode = &dxva_modes[i];
265         int validate;
266         if (!dxva_check_codec_compatibility(avctx, mode))
267             continue;
268
269         for (j = 0; j < guid_count; j++) {
270             if (IsEqualGUID(mode->guid, &guid_list[j]))
271                 break;
272         }
273         if (j == guid_count)
274             continue;
275
276 #if CONFIG_D3D11VA
277         if (sctx->pix_fmt == AV_PIX_FMT_D3D11)
278             validate = d3d11va_validate_output(service, *mode->guid, surface_format);
279 #endif
280 #if CONFIG_DXVA2
281         if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
282             validate = dxva2_validate_output(service, *mode->guid, surface_format);
283 #endif
284         if (validate) {
285             *decoder_guid = *mode->guid;
286             break;
287         }
288     }
289
290     if (IsEqualGUID(decoder_guid, &ff_GUID_NULL)) {
291         av_log(avctx, AV_LOG_VERBOSE, "No decoder device for codec found\n");
292         return AVERROR(EINVAL);
293     }
294
295     if (IsEqualGUID(decoder_guid, &ff_DXVADDI_Intel_ModeH264_E))
296         sctx->workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
297
298     return 0;
299 }
300
301 static void bufref_free_interface(void *opaque, uint8_t *data)
302 {
303     IUnknown_Release((IUnknown *)opaque);
304 }
305
306 static AVBufferRef *bufref_wrap_interface(IUnknown *iface)
307 {
308     return av_buffer_create((uint8_t*)iface, 1, bufref_free_interface, iface, 0);
309 }
310
311 #if CONFIG_DXVA2
312
313 static int dxva2_get_decoder_configuration(AVCodecContext *avctx, const GUID *device_guid,
314                                            const DXVA2_VideoDesc *desc,
315                                            DXVA2_ConfigPictureDecode *config)
316 {
317     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
318     unsigned cfg_count;
319     DXVA2_ConfigPictureDecode *cfg_list;
320     HRESULT hr;
321     int ret;
322
323     hr = IDirectXVideoDecoderService_GetDecoderConfigurations(sctx->dxva2_service, device_guid, desc, NULL, &cfg_count, &cfg_list);
324     if (FAILED(hr)) {
325         av_log(avctx, AV_LOG_ERROR, "Unable to retrieve decoder configurations\n");
326         return AVERROR(EINVAL);
327     }
328
329     ret = dxva_get_decoder_configuration(avctx, cfg_list, cfg_count);
330     if (ret >= 0)
331         *config = cfg_list[ret];
332     CoTaskMemFree(cfg_list);
333     return ret;
334 }
335
336 static int dxva2_create_decoder(AVCodecContext *avctx)
337 {
338     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
339     GUID *guid_list;
340     unsigned guid_count;
341     GUID device_guid;
342     D3DFORMAT surface_format = avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
343                                MKTAG('P', '0', '1', '0') : MKTAG('N', 'V', '1', '2');
344     DXVA2_VideoDesc desc = { 0 };
345     DXVA2_ConfigPictureDecode config;
346     HRESULT hr;
347     int ret;
348     HANDLE device_handle;
349     AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
350     AVDXVA2FramesContext *frames_hwctx = frames_ctx->hwctx;
351     AVDXVA2DeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
352
353     hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr,
354                                                   &device_handle);
355     if (FAILED(hr)) {
356         av_log(avctx, AV_LOG_ERROR, "Failed to open a device handle\n");
357         goto fail;
358     }
359
360     hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr, device_handle,
361                                                  &ff_IID_IDirectXVideoDecoderService,
362                                                  (void **)&sctx->dxva2_service);
363     IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, device_handle);
364     if (FAILED(hr)) {
365         av_log(avctx, AV_LOG_ERROR, "Failed to create IDirectXVideoDecoderService\n");
366         goto fail;
367     }
368
369     hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(sctx->dxva2_service, &guid_count, &guid_list);
370     if (FAILED(hr)) {
371         av_log(avctx, AV_LOG_ERROR, "Failed to retrieve decoder device GUIDs\n");
372         goto fail;
373     }
374
375     ret = dxva_get_decoder_guid(avctx, sctx->dxva2_service, &surface_format,
376                                 guid_count, guid_list, &device_guid);
377     CoTaskMemFree(guid_list);
378     if (ret < 0) {
379         goto fail;
380     }
381
382     desc.SampleWidth  = avctx->coded_width;
383     desc.SampleHeight = avctx->coded_height;
384     desc.Format       = surface_format;
385
386     ret = dxva2_get_decoder_configuration(avctx, &device_guid, &desc, &config);
387     if (ret < 0) {
388         goto fail;
389     }
390
391     hr = IDirectXVideoDecoderService_CreateVideoDecoder(sctx->dxva2_service, &device_guid,
392                                                         &desc, &config, frames_hwctx->surfaces,
393                                                         frames_hwctx->nb_surfaces, &sctx->dxva2_decoder);
394     if (FAILED(hr)) {
395         av_log(avctx, AV_LOG_ERROR, "Failed to create DXVA2 video decoder\n");
396         goto fail;
397     }
398
399     sctx->dxva2_config = config;
400
401     sctx->decoder_ref = bufref_wrap_interface((IUnknown *)sctx->dxva2_decoder);
402     if (!sctx->decoder_ref)
403         return AVERROR(ENOMEM);
404
405     return 0;
406 fail:
407     return AVERROR(EINVAL);
408 }
409
410 #endif
411
412 #if CONFIG_D3D11VA
413
414 static int d3d11va_get_decoder_configuration(AVCodecContext *avctx,
415                                              ID3D11VideoDevice *video_device,
416                                              const D3D11_VIDEO_DECODER_DESC *desc,
417                                              D3D11_VIDEO_DECODER_CONFIG *config)
418 {
419     unsigned cfg_count = 0;
420     D3D11_VIDEO_DECODER_CONFIG *cfg_list = NULL;
421     HRESULT hr;
422     int i, ret;
423
424     hr = ID3D11VideoDevice_GetVideoDecoderConfigCount(video_device, desc, &cfg_count);
425     if (FAILED(hr)) {
426         av_log(avctx, AV_LOG_ERROR, "Unable to retrieve decoder configurations\n");
427         return AVERROR(EINVAL);
428     }
429
430     cfg_list = av_malloc_array(cfg_count, sizeof(D3D11_VIDEO_DECODER_CONFIG));
431     if (cfg_list == NULL)
432         return AVERROR(ENOMEM);
433     for (i = 0; i < cfg_count; i++) {
434         hr = ID3D11VideoDevice_GetVideoDecoderConfig(video_device, desc, i, &cfg_list[i]);
435         if (FAILED(hr)) {
436             av_log(avctx, AV_LOG_ERROR, "Unable to retrieve decoder configurations. (hr=0x%lX)\n", hr);
437             av_free(cfg_list);
438             return AVERROR(EINVAL);
439         }
440     }
441
442     ret = dxva_get_decoder_configuration(avctx, cfg_list, cfg_count);
443     if (ret >= 0)
444         *config = cfg_list[ret];
445     av_free(cfg_list);
446     return ret;
447 }
448
449 static DXGI_FORMAT d3d11va_map_sw_to_hw_format(enum AVPixelFormat pix_fmt)
450 {
451     switch (pix_fmt) {
452     case AV_PIX_FMT_NV12:       return DXGI_FORMAT_NV12;
453     case AV_PIX_FMT_P010:       return DXGI_FORMAT_P010;
454     case AV_PIX_FMT_YUV420P:    return DXGI_FORMAT_420_OPAQUE;
455     default:                    return DXGI_FORMAT_UNKNOWN;
456     }
457 }
458
459 static int d3d11va_create_decoder(AVCodecContext *avctx)
460 {
461     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
462     GUID *guid_list;
463     unsigned guid_count, i;
464     GUID decoder_guid;
465     D3D11_VIDEO_DECODER_DESC desc = { 0 };
466     D3D11_VIDEO_DECODER_CONFIG config;
467     AVHWFramesContext *frames_ctx = (AVHWFramesContext *)avctx->hw_frames_ctx->data;
468     AVD3D11VADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
469     AVD3D11VAFramesContext *frames_hwctx = frames_ctx->hwctx;
470     DXGI_FORMAT surface_format = d3d11va_map_sw_to_hw_format(frames_ctx->sw_format);
471     D3D11_TEXTURE2D_DESC texdesc;
472     HRESULT hr;
473     int ret;
474
475     if (!frames_hwctx->texture) {
476         av_log(avctx, AV_LOG_ERROR, "AVD3D11VAFramesContext.texture not set.\n");
477         return AVERROR(EINVAL);
478     }
479     ID3D11Texture2D_GetDesc(frames_hwctx->texture, &texdesc);
480
481     guid_count = ID3D11VideoDevice_GetVideoDecoderProfileCount(device_hwctx->video_device);
482     guid_list = av_malloc_array(guid_count, sizeof(*guid_list));
483     if (guid_list == NULL || guid_count == 0) {
484         av_log(avctx, AV_LOG_ERROR, "Failed to get the decoder GUIDs\n");
485         av_free(guid_list);
486         return AVERROR(EINVAL);
487     }
488     for (i = 0; i < guid_count; i++) {
489         hr = ID3D11VideoDevice_GetVideoDecoderProfile(device_hwctx->video_device, i, &guid_list[i]);
490         if (FAILED(hr)) {
491             av_log(avctx, AV_LOG_ERROR, "Failed to retrieve decoder GUID %d\n", i);
492             av_free(guid_list);
493             return AVERROR(EINVAL);
494         }
495     }
496
497     ret = dxva_get_decoder_guid(avctx, device_hwctx->video_device, &surface_format,
498                                 guid_count, guid_list, &decoder_guid);
499     av_free(guid_list);
500     if (ret < 0)
501         return AVERROR(EINVAL);
502
503     desc.SampleWidth  = avctx->coded_width;
504     desc.SampleHeight = avctx->coded_height;
505     desc.OutputFormat = surface_format;
506     desc.Guid         = decoder_guid;
507
508     ret = d3d11va_get_decoder_configuration(avctx, device_hwctx->video_device, &desc, &config);
509     if (ret < 0)
510         return AVERROR(EINVAL);
511
512     sctx->d3d11_views = av_mallocz_array(texdesc.ArraySize, sizeof(sctx->d3d11_views[0]));
513     if (!sctx->d3d11_views)
514         return AVERROR(ENOMEM);
515     sctx->nb_d3d11_views = texdesc.ArraySize;
516
517     for (i = 0; i < sctx->nb_d3d11_views; i++) {
518         D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc = {
519             .DecodeProfile = decoder_guid,
520             .ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D,
521             .Texture2D = {
522                 .ArraySlice = i,
523             }
524         };
525         hr = ID3D11VideoDevice_CreateVideoDecoderOutputView(device_hwctx->video_device,
526                                                             (ID3D11Resource*) frames_hwctx->texture,
527                                                             &viewDesc,
528                                                             (ID3D11VideoDecoderOutputView**) &sctx->d3d11_views[i]);
529         if (FAILED(hr)) {
530             av_log(avctx, AV_LOG_ERROR, "Could not create the decoder output view %d\n", i);
531             return AVERROR_UNKNOWN;
532         }
533     }
534
535     hr = ID3D11VideoDevice_CreateVideoDecoder(device_hwctx->video_device, &desc,
536                                               &config, &sctx->d3d11_decoder);
537     if (FAILED(hr)) {
538         av_log(avctx, AV_LOG_ERROR, "Failed to create D3D11VA video decoder\n");
539         return AVERROR(EINVAL);
540     }
541
542     sctx->d3d11_config = config;
543     sctx->d3d11_texture = frames_hwctx->texture;
544
545     sctx->decoder_ref = bufref_wrap_interface((IUnknown *)sctx->d3d11_decoder);
546     if (!sctx->decoder_ref)
547         return AVERROR(ENOMEM);
548
549     return 0;
550 }
551
552 #endif
553
554 static void ff_dxva2_lock(AVCodecContext *avctx)
555 {
556 #if CONFIG_D3D11VA
557     if (ff_dxva2_is_d3d11(avctx)) {
558         FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
559         AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
560         if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
561             WaitForSingleObjectEx(D3D11VA_CONTEXT(ctx)->context_mutex, INFINITE, FALSE);
562         if (sctx->device_ctx) {
563             AVD3D11VADeviceContext *hwctx = sctx->device_ctx->hwctx;
564             hwctx->lock(hwctx->lock_ctx);
565         }
566     }
567 #endif
568 }
569
570 static void ff_dxva2_unlock(AVCodecContext *avctx)
571 {
572 #if CONFIG_D3D11VA
573     if (ff_dxva2_is_d3d11(avctx)) {
574         FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
575         AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
576         if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
577             ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
578         if (sctx->device_ctx) {
579             AVD3D11VADeviceContext *hwctx = sctx->device_ctx->hwctx;
580             hwctx->unlock(hwctx->lock_ctx);
581         }
582     }
583 #endif
584 }
585
586 int ff_dxva2_common_frame_params(AVCodecContext *avctx,
587                                  AVBufferRef *hw_frames_ctx)
588 {
589     AVHWFramesContext *frames_ctx = (AVHWFramesContext *)hw_frames_ctx->data;
590     AVHWDeviceContext *device_ctx = frames_ctx->device_ctx;
591     int surface_alignment, num_surfaces;
592
593     if (device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
594         frames_ctx->format = AV_PIX_FMT_DXVA2_VLD;
595     } else if (device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) {
596         frames_ctx->format = AV_PIX_FMT_D3D11;
597     } else {
598         return AVERROR(EINVAL);
599     }
600
601     /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
602     but it causes issues for H.264 on certain AMD GPUs..... */
603     if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO)
604         surface_alignment = 32;
605     /* the HEVC DXVA2 spec asks for 128 pixel aligned surfaces to ensure
606     all coding features have enough room to work with */
607     else if (avctx->codec_id == AV_CODEC_ID_HEVC)
608         surface_alignment = 128;
609     else
610         surface_alignment = 16;
611
612     /* 1 base work surface */
613     num_surfaces = 1;
614
615     /* add surfaces based on number of possible refs */
616     if (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_HEVC)
617         num_surfaces += 16;
618     else if (avctx->codec_id == AV_CODEC_ID_VP9)
619         num_surfaces += 8;
620     else
621         num_surfaces += 2;
622
623     frames_ctx->sw_format = avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
624                             AV_PIX_FMT_P010 : AV_PIX_FMT_NV12;
625     frames_ctx->width = FFALIGN(avctx->coded_width, surface_alignment);
626     frames_ctx->height = FFALIGN(avctx->coded_height, surface_alignment);
627     frames_ctx->initial_pool_size = num_surfaces;
628
629
630 #if CONFIG_DXVA2
631     if (frames_ctx->format == AV_PIX_FMT_DXVA2_VLD) {
632         AVDXVA2FramesContext *frames_hwctx = frames_ctx->hwctx;
633
634         frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
635     }
636 #endif
637
638 #if CONFIG_D3D11VA
639     if (frames_ctx->format == AV_PIX_FMT_D3D11) {
640         AVD3D11VAFramesContext *frames_hwctx = frames_ctx->hwctx;
641
642         frames_hwctx->BindFlags |= D3D11_BIND_DECODER;
643     }
644 #endif
645
646     return 0;
647 }
648
649 int ff_dxva2_decode_init(AVCodecContext *avctx)
650 {
651     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
652     AVHWFramesContext *frames_ctx;
653     enum AVHWDeviceType dev_type = avctx->hwaccel->pix_fmt == AV_PIX_FMT_DXVA2_VLD
654                             ? AV_HWDEVICE_TYPE_DXVA2 : AV_HWDEVICE_TYPE_D3D11VA;
655     int ret = 0;
656
657     // Old API.
658     if (avctx->hwaccel_context)
659         return 0;
660
661     // (avctx->pix_fmt is not updated yet at this point)
662     sctx->pix_fmt = avctx->hwaccel->pix_fmt;
663
664     ret = ff_decode_get_hw_frames_ctx(avctx, dev_type);
665     if (ret < 0)
666         return ret;
667
668     frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
669     sctx->device_ctx = frames_ctx->device_ctx;
670
671     if (frames_ctx->format != sctx->pix_fmt) {
672         av_log(avctx, AV_LOG_ERROR, "Invalid pixfmt for hwaccel!\n");
673         ret = AVERROR(EINVAL);
674         goto fail;
675     }
676
677 #if CONFIG_D3D11VA
678     if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
679         AVD3D11VADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
680         AVD3D11VAContext *d3d11_ctx = &sctx->ctx.d3d11va;
681
682         ff_dxva2_lock(avctx);
683         ret = d3d11va_create_decoder(avctx);
684         ff_dxva2_unlock(avctx);
685         if (ret < 0)
686             goto fail;
687
688         d3d11_ctx->decoder       = sctx->d3d11_decoder;
689         d3d11_ctx->video_context = device_hwctx->video_context;
690         d3d11_ctx->cfg           = &sctx->d3d11_config;
691         d3d11_ctx->surface_count = sctx->nb_d3d11_views;
692         d3d11_ctx->surface       = sctx->d3d11_views;
693         d3d11_ctx->workaround    = sctx->workaround;
694         d3d11_ctx->context_mutex = INVALID_HANDLE_VALUE;
695     }
696 #endif
697
698 #if CONFIG_DXVA2
699     if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
700         AVDXVA2FramesContext *frames_hwctx = frames_ctx->hwctx;
701         struct dxva_context *dxva_ctx = &sctx->ctx.dxva2;
702
703         ff_dxva2_lock(avctx);
704         ret = dxva2_create_decoder(avctx);
705         ff_dxva2_unlock(avctx);
706         if (ret < 0)
707             goto fail;
708
709         dxva_ctx->decoder       = sctx->dxva2_decoder;
710         dxva_ctx->cfg           = &sctx->dxva2_config;
711         dxva_ctx->surface       = frames_hwctx->surfaces;
712         dxva_ctx->surface_count = frames_hwctx->nb_surfaces;
713         dxva_ctx->workaround    = sctx->workaround;
714     }
715 #endif
716
717     return 0;
718
719 fail:
720     ff_dxva2_decode_uninit(avctx);
721     return ret;
722 }
723
724 int ff_dxva2_decode_uninit(AVCodecContext *avctx)
725 {
726     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
727     int i;
728
729     av_buffer_unref(&sctx->decoder_ref);
730
731 #if CONFIG_D3D11VA
732     for (i = 0; i < sctx->nb_d3d11_views; i++) {
733         if (sctx->d3d11_views[i])
734             ID3D11VideoDecoderOutputView_Release(sctx->d3d11_views[i]);
735     }
736     av_freep(&sctx->d3d11_views);
737 #endif
738
739 #if CONFIG_DXVA2
740     if (sctx->dxva2_service)
741         IDirectXVideoDecoderService_Release(sctx->dxva2_service);
742 #endif
743
744     return 0;
745 }
746
747 static void *get_surface(const AVCodecContext *avctx, const AVFrame *frame)
748 {
749 #if CONFIG_D3D11VA
750     if (frame->format == AV_PIX_FMT_D3D11) {
751         FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
752         intptr_t index = (intptr_t)frame->data[1];
753         if (index < 0 || index >= sctx->nb_d3d11_views ||
754             sctx->d3d11_texture != (ID3D11Texture2D *)frame->data[0]) {
755             av_log((void *)avctx, AV_LOG_ERROR, "get_buffer frame is invalid!\n");
756             return NULL;
757         }
758         return sctx->d3d11_views[index];
759     }
760 #endif
761     return frame->data[3];
762 }
763
764 unsigned ff_dxva2_get_surface_index(const AVCodecContext *avctx,
765                                     const AVDXVAContext *ctx,
766                                     const AVFrame *frame)
767 {
768     void *surface = get_surface(avctx, frame);
769     unsigned i;
770
771 #if CONFIG_D3D11VA
772     if (avctx->pix_fmt == AV_PIX_FMT_D3D11)
773         return (intptr_t)frame->data[1];
774     if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
775         D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
776         ID3D11VideoDecoderOutputView_GetDesc((ID3D11VideoDecoderOutputView*) surface, &viewDesc);
777         return viewDesc.Texture2D.ArraySlice;
778     }
779 #endif
780 #if CONFIG_DXVA2
781     for (i = 0; i < DXVA_CONTEXT_COUNT(avctx, ctx); i++) {
782         if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD && ctx->dxva2.surface[i] == surface)
783             return i;
784     }
785 #endif
786
787     assert(0);
788     return 0;
789 }
790
791 int ff_dxva2_commit_buffer(AVCodecContext *avctx,
792                            AVDXVAContext *ctx,
793                            DECODER_BUFFER_DESC *dsc,
794                            unsigned type, const void *data, unsigned size,
795                            unsigned mb_count)
796 {
797     void     *dxva_data;
798     unsigned dxva_size;
799     int      result;
800     HRESULT hr = 0;
801
802 #if CONFIG_D3D11VA
803     if (ff_dxva2_is_d3d11(avctx))
804         hr = ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
805                                                  D3D11VA_CONTEXT(ctx)->decoder,
806                                                  type,
807                                                  &dxva_size, &dxva_data);
808 #endif
809 #if CONFIG_DXVA2
810     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
811         hr = IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder, type,
812                                             &dxva_data, &dxva_size);
813 #endif
814     if (FAILED(hr)) {
815         av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %u: 0x%x\n",
816                type, (unsigned)hr);
817         return -1;
818     }
819     if (size <= dxva_size) {
820         memcpy(dxva_data, data, size);
821
822 #if CONFIG_D3D11VA
823         if (ff_dxva2_is_d3d11(avctx)) {
824             D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = dsc;
825             memset(dsc11, 0, sizeof(*dsc11));
826             dsc11->BufferType           = type;
827             dsc11->DataSize             = size;
828             dsc11->NumMBsInBuffer       = mb_count;
829         }
830 #endif
831 #if CONFIG_DXVA2
832         if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
833             DXVA2_DecodeBufferDesc *dsc2 = dsc;
834             memset(dsc2, 0, sizeof(*dsc2));
835             dsc2->CompressedBufferType = type;
836             dsc2->DataSize             = size;
837             dsc2->NumMBsInBuffer       = mb_count;
838         }
839 #endif
840
841         result = 0;
842     } else {
843         av_log(avctx, AV_LOG_ERROR, "Buffer for type %u was too small\n", type);
844         result = -1;
845     }
846
847 #if CONFIG_D3D11VA
848     if (ff_dxva2_is_d3d11(avctx))
849         hr = ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type);
850 #endif
851 #if CONFIG_DXVA2
852     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
853         hr = IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type);
854 #endif
855     if (FAILED(hr)) {
856         av_log(avctx, AV_LOG_ERROR,
857                "Failed to release buffer type %u: 0x%x\n",
858                type, (unsigned)hr);
859         result = -1;
860     }
861     return result;
862 }
863
864 static int frame_add_buf(AVFrame *frame, AVBufferRef *ref)
865 {
866     int i;
867
868     for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
869         if (!frame->buf[i]) {
870             frame->buf[i] = av_buffer_ref(ref);
871             return frame->buf[i] ? 0 : AVERROR(ENOMEM);
872         }
873     }
874
875     // For now we expect that the caller does not use more than
876     // AV_NUM_DATA_POINTERS-1 buffers if the user uses a custom pool.
877     return AVERROR(EINVAL);
878 }
879
880 int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
881                               const void *pp, unsigned pp_size,
882                               const void *qm, unsigned qm_size,
883                               int (*commit_bs_si)(AVCodecContext *,
884                                                   DECODER_BUFFER_DESC *bs,
885                                                   DECODER_BUFFER_DESC *slice))
886 {
887     AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
888     unsigned               buffer_count = 0;
889 #if CONFIG_D3D11VA
890     D3D11_VIDEO_DECODER_BUFFER_DESC buffer11[4];
891 #endif
892 #if CONFIG_DXVA2
893     DXVA2_DecodeBufferDesc          buffer2[4];
894 #endif
895     DECODER_BUFFER_DESC             *buffer = NULL, *buffer_slice = NULL;
896     int result, runs = 0;
897     HRESULT hr;
898     unsigned type;
899     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
900
901     if (sctx->decoder_ref) {
902         result = frame_add_buf(frame, sctx->decoder_ref);
903         if (result < 0)
904             return result;
905     }
906
907     do {
908         ff_dxva2_lock(avctx);
909 #if CONFIG_D3D11VA
910         if (ff_dxva2_is_d3d11(avctx))
911             hr = ID3D11VideoContext_DecoderBeginFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder,
912                                                       get_surface(avctx, frame),
913                                                       0, NULL);
914 #endif
915 #if CONFIG_DXVA2
916         if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
917             hr = IDirectXVideoDecoder_BeginFrame(DXVA2_CONTEXT(ctx)->decoder,
918                                                  get_surface(avctx, frame),
919                                                  NULL);
920 #endif
921         if (hr != E_PENDING || ++runs > 50)
922             break;
923         ff_dxva2_unlock(avctx);
924         av_usleep(2000);
925     } while(1);
926
927     if (FAILED(hr)) {
928         av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%x\n", (unsigned)hr);
929         ff_dxva2_unlock(avctx);
930         return -1;
931     }
932
933 #if CONFIG_D3D11VA
934     if (ff_dxva2_is_d3d11(avctx)) {
935         buffer = &buffer11[buffer_count];
936         type = D3D11_VIDEO_DECODER_BUFFER_PICTURE_PARAMETERS;
937     }
938 #endif
939 #if CONFIG_DXVA2
940     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
941         buffer = &buffer2[buffer_count];
942         type = DXVA2_PictureParametersBufferType;
943     }
944 #endif
945     result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
946                                     type,
947                                     pp, pp_size, 0);
948     if (result) {
949         av_log(avctx, AV_LOG_ERROR,
950                "Failed to add picture parameter buffer\n");
951         goto end;
952     }
953     buffer_count++;
954
955     if (qm_size > 0) {
956 #if CONFIG_D3D11VA
957         if (ff_dxva2_is_d3d11(avctx)) {
958             buffer = &buffer11[buffer_count];
959             type = D3D11_VIDEO_DECODER_BUFFER_INVERSE_QUANTIZATION_MATRIX;
960         }
961 #endif
962 #if CONFIG_DXVA2
963         if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
964             buffer = &buffer2[buffer_count];
965             type = DXVA2_InverseQuantizationMatrixBufferType;
966         }
967 #endif
968         result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
969                                         type,
970                                         qm, qm_size, 0);
971         if (result) {
972             av_log(avctx, AV_LOG_ERROR,
973                    "Failed to add inverse quantization matrix buffer\n");
974             goto end;
975         }
976         buffer_count++;
977     }
978
979 #if CONFIG_D3D11VA
980     if (ff_dxva2_is_d3d11(avctx)) {
981         buffer       = &buffer11[buffer_count + 0];
982         buffer_slice = &buffer11[buffer_count + 1];
983     }
984 #endif
985 #if CONFIG_DXVA2
986     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
987         buffer       = &buffer2[buffer_count + 0];
988         buffer_slice = &buffer2[buffer_count + 1];
989     }
990 #endif
991
992     result = commit_bs_si(avctx,
993                           buffer,
994                           buffer_slice);
995     if (result) {
996         av_log(avctx, AV_LOG_ERROR,
997                "Failed to add bitstream or slice control buffer\n");
998         goto end;
999     }
1000     buffer_count += 2;
1001
1002     /* TODO Film Grain when possible */
1003
1004     assert(buffer_count == 1 + (qm_size > 0) + 2);
1005
1006 #if CONFIG_D3D11VA
1007     if (ff_dxva2_is_d3d11(avctx))
1008         hr = ID3D11VideoContext_SubmitDecoderBuffers(D3D11VA_CONTEXT(ctx)->video_context,
1009                                                      D3D11VA_CONTEXT(ctx)->decoder,
1010                                                      buffer_count, buffer11);
1011 #endif
1012 #if CONFIG_DXVA2
1013     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
1014         DXVA2_DecodeExecuteParams exec = {
1015             .NumCompBuffers     = buffer_count,
1016             .pCompressedBuffers = buffer2,
1017             .pExtensionData     = NULL,
1018         };
1019         hr = IDirectXVideoDecoder_Execute(DXVA2_CONTEXT(ctx)->decoder, &exec);
1020     }
1021 #endif
1022     if (FAILED(hr)) {
1023         av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%x\n", (unsigned)hr);
1024         result = -1;
1025     }
1026
1027 end:
1028 #if CONFIG_D3D11VA
1029     if (ff_dxva2_is_d3d11(avctx))
1030         hr = ID3D11VideoContext_DecoderEndFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder);
1031 #endif
1032 #if CONFIG_DXVA2
1033     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
1034         hr = IDirectXVideoDecoder_EndFrame(DXVA2_CONTEXT(ctx)->decoder, NULL);
1035 #endif
1036     ff_dxva2_unlock(avctx);
1037     if (FAILED(hr)) {
1038         av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%x\n", (unsigned)hr);
1039         result = -1;
1040     }
1041
1042     return result;
1043 }
1044
1045 int ff_dxva2_is_d3d11(const AVCodecContext *avctx)
1046 {
1047     if (CONFIG_D3D11VA)
1048         return avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD ||
1049                avctx->pix_fmt == AV_PIX_FMT_D3D11;
1050     else
1051         return 0;
1052 }