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