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