]> git.sesse.net Git - ffmpeg/blob - libavcodec/dxva2.c
h264/aarch64: optimize neon loop filter
[ffmpeg] / 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     frames_ctx->sw_format = avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
612                             AV_PIX_FMT_P010 : AV_PIX_FMT_NV12;
613     frames_ctx->width = FFALIGN(avctx->coded_width, surface_alignment);
614     frames_ctx->height = FFALIGN(avctx->coded_height, surface_alignment);
615     frames_ctx->initial_pool_size = num_surfaces;
616
617
618 #if CONFIG_DXVA2
619     if (frames_ctx->format == AV_PIX_FMT_DXVA2_VLD) {
620         AVDXVA2FramesContext *frames_hwctx = frames_ctx->hwctx;
621
622         frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
623     }
624 #endif
625
626 #if CONFIG_D3D11VA
627     if (frames_ctx->format == AV_PIX_FMT_D3D11) {
628         AVD3D11VAFramesContext *frames_hwctx = frames_ctx->hwctx;
629
630         frames_hwctx->BindFlags |= D3D11_BIND_DECODER;
631     }
632 #endif
633
634     return 0;
635 }
636
637 int ff_dxva2_decode_init(AVCodecContext *avctx)
638 {
639     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
640     AVHWFramesContext *frames_ctx;
641     enum AVHWDeviceType dev_type = avctx->hwaccel->pix_fmt == AV_PIX_FMT_DXVA2_VLD
642                             ? AV_HWDEVICE_TYPE_DXVA2 : AV_HWDEVICE_TYPE_D3D11VA;
643     int ret = 0;
644
645     // Old API.
646     if (avctx->hwaccel_context)
647         return 0;
648
649     // (avctx->pix_fmt is not updated yet at this point)
650     sctx->pix_fmt = avctx->hwaccel->pix_fmt;
651
652     ret = ff_decode_get_hw_frames_ctx(avctx, dev_type);
653     if (ret < 0)
654         return ret;
655
656     frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
657     sctx->device_ctx = frames_ctx->device_ctx;
658
659     if (frames_ctx->format != sctx->pix_fmt) {
660         av_log(avctx, AV_LOG_ERROR, "Invalid pixfmt for hwaccel!\n");
661         ret = AVERROR(EINVAL);
662         goto fail;
663     }
664
665 #if CONFIG_D3D11VA
666     if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
667         AVD3D11VADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
668         AVD3D11VAContext *d3d11_ctx = &sctx->ctx.d3d11va;
669
670         ff_dxva2_lock(avctx);
671         ret = d3d11va_create_decoder(avctx);
672         ff_dxva2_unlock(avctx);
673         if (ret < 0)
674             goto fail;
675
676         d3d11_ctx->decoder       = sctx->d3d11_decoder;
677         d3d11_ctx->video_context = device_hwctx->video_context;
678         d3d11_ctx->cfg           = &sctx->d3d11_config;
679         d3d11_ctx->surface_count = sctx->nb_d3d11_views;
680         d3d11_ctx->surface       = sctx->d3d11_views;
681         d3d11_ctx->workaround    = sctx->workaround;
682         d3d11_ctx->context_mutex = INVALID_HANDLE_VALUE;
683     }
684 #endif
685
686 #if CONFIG_DXVA2
687     if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
688         AVDXVA2FramesContext *frames_hwctx = frames_ctx->hwctx;
689         struct dxva_context *dxva_ctx = &sctx->ctx.dxva2;
690
691         ff_dxva2_lock(avctx);
692         ret = dxva2_create_decoder(avctx);
693         ff_dxva2_unlock(avctx);
694         if (ret < 0)
695             goto fail;
696
697         dxva_ctx->decoder       = sctx->dxva2_decoder;
698         dxva_ctx->cfg           = &sctx->dxva2_config;
699         dxva_ctx->surface       = frames_hwctx->surfaces;
700         dxva_ctx->surface_count = frames_hwctx->nb_surfaces;
701         dxva_ctx->workaround    = sctx->workaround;
702     }
703 #endif
704
705     return 0;
706
707 fail:
708     ff_dxva2_decode_uninit(avctx);
709     return ret;
710 }
711
712 int ff_dxva2_decode_uninit(AVCodecContext *avctx)
713 {
714     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
715     int i;
716
717     av_buffer_unref(&sctx->decoder_ref);
718
719 #if CONFIG_D3D11VA
720     for (i = 0; i < sctx->nb_d3d11_views; i++) {
721         if (sctx->d3d11_views[i])
722             ID3D11VideoDecoderOutputView_Release(sctx->d3d11_views[i]);
723     }
724     av_freep(&sctx->d3d11_views);
725 #endif
726
727 #if CONFIG_DXVA2
728     if (sctx->dxva2_service)
729         IDirectXVideoDecoderService_Release(sctx->dxva2_service);
730 #endif
731
732     return 0;
733 }
734
735 static void *get_surface(const AVCodecContext *avctx, const AVFrame *frame)
736 {
737 #if CONFIG_D3D11VA
738     if (frame->format == AV_PIX_FMT_D3D11) {
739         FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
740         intptr_t index = (intptr_t)frame->data[1];
741         if (index < 0 || index >= sctx->nb_d3d11_views ||
742             sctx->d3d11_texture != (ID3D11Texture2D *)frame->data[0]) {
743             av_log((void *)avctx, AV_LOG_ERROR, "get_buffer frame is invalid!\n");
744             return NULL;
745         }
746         return sctx->d3d11_views[index];
747     }
748 #endif
749     return frame->data[3];
750 }
751
752 unsigned ff_dxva2_get_surface_index(const AVCodecContext *avctx,
753                                     const AVDXVAContext *ctx,
754                                     const AVFrame *frame)
755 {
756     void *surface = get_surface(avctx, frame);
757     unsigned i;
758
759 #if CONFIG_D3D11VA
760     if (avctx->pix_fmt == AV_PIX_FMT_D3D11)
761         return (intptr_t)frame->data[1];
762     if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
763         D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
764         ID3D11VideoDecoderOutputView_GetDesc((ID3D11VideoDecoderOutputView*) surface, &viewDesc);
765         return viewDesc.Texture2D.ArraySlice;
766     }
767 #endif
768 #if CONFIG_DXVA2
769     for (i = 0; i < DXVA_CONTEXT_COUNT(avctx, ctx); i++) {
770         if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD && ctx->dxva2.surface[i] == surface)
771             return i;
772     }
773 #endif
774
775     assert(0);
776     return 0;
777 }
778
779 int ff_dxva2_commit_buffer(AVCodecContext *avctx,
780                            AVDXVAContext *ctx,
781                            DECODER_BUFFER_DESC *dsc,
782                            unsigned type, const void *data, unsigned size,
783                            unsigned mb_count)
784 {
785     void     *dxva_data;
786     unsigned dxva_size;
787     int      result;
788     HRESULT hr;
789
790 #if CONFIG_D3D11VA
791     if (ff_dxva2_is_d3d11(avctx))
792         hr = ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
793                                                  D3D11VA_CONTEXT(ctx)->decoder,
794                                                  type,
795                                                  &dxva_size, &dxva_data);
796 #endif
797 #if CONFIG_DXVA2
798     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
799         hr = IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder, type,
800                                             &dxva_data, &dxva_size);
801 #endif
802     if (FAILED(hr)) {
803         av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %u: 0x%x\n",
804                type, (unsigned)hr);
805         return -1;
806     }
807     if (size <= dxva_size) {
808         memcpy(dxva_data, data, size);
809
810 #if CONFIG_D3D11VA
811         if (ff_dxva2_is_d3d11(avctx)) {
812             D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = dsc;
813             memset(dsc11, 0, sizeof(*dsc11));
814             dsc11->BufferType           = type;
815             dsc11->DataSize             = size;
816             dsc11->NumMBsInBuffer       = mb_count;
817         }
818 #endif
819 #if CONFIG_DXVA2
820         if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
821             DXVA2_DecodeBufferDesc *dsc2 = dsc;
822             memset(dsc2, 0, sizeof(*dsc2));
823             dsc2->CompressedBufferType = type;
824             dsc2->DataSize             = size;
825             dsc2->NumMBsInBuffer       = mb_count;
826         }
827 #endif
828
829         result = 0;
830     } else {
831         av_log(avctx, AV_LOG_ERROR, "Buffer for type %u was too small\n", type);
832         result = -1;
833     }
834
835 #if CONFIG_D3D11VA
836     if (ff_dxva2_is_d3d11(avctx))
837         hr = ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type);
838 #endif
839 #if CONFIG_DXVA2
840     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
841         hr = IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type);
842 #endif
843     if (FAILED(hr)) {
844         av_log(avctx, AV_LOG_ERROR,
845                "Failed to release buffer type %u: 0x%x\n",
846                type, (unsigned)hr);
847         result = -1;
848     }
849     return result;
850 }
851
852 static int frame_add_buf(AVFrame *frame, AVBufferRef *ref)
853 {
854     int i;
855
856     for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
857         if (!frame->buf[i]) {
858             frame->buf[i] = av_buffer_ref(ref);
859             return frame->buf[i] ? 0 : AVERROR(ENOMEM);
860         }
861     }
862
863     // For now we expect that the caller does not use more than
864     // AV_NUM_DATA_POINTERS-1 buffers if the user uses a custom pool.
865     return AVERROR(EINVAL);
866 }
867
868 int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
869                               const void *pp, unsigned pp_size,
870                               const void *qm, unsigned qm_size,
871                               int (*commit_bs_si)(AVCodecContext *,
872                                                   DECODER_BUFFER_DESC *bs,
873                                                   DECODER_BUFFER_DESC *slice))
874 {
875     AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
876     unsigned               buffer_count = 0;
877 #if CONFIG_D3D11VA
878     D3D11_VIDEO_DECODER_BUFFER_DESC buffer11[4];
879 #endif
880 #if CONFIG_DXVA2
881     DXVA2_DecodeBufferDesc          buffer2[4];
882 #endif
883     DECODER_BUFFER_DESC             *buffer,*buffer_slice;
884     int result, runs = 0;
885     HRESULT hr;
886     unsigned type;
887     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
888
889     if (sctx->decoder_ref) {
890         result = frame_add_buf(frame, sctx->decoder_ref);
891         if (result < 0)
892             return result;
893     }
894
895     do {
896         ff_dxva2_lock(avctx);
897 #if CONFIG_D3D11VA
898         if (ff_dxva2_is_d3d11(avctx))
899             hr = ID3D11VideoContext_DecoderBeginFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder,
900                                                       get_surface(avctx, frame),
901                                                       0, NULL);
902 #endif
903 #if CONFIG_DXVA2
904         if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
905             hr = IDirectXVideoDecoder_BeginFrame(DXVA2_CONTEXT(ctx)->decoder,
906                                                  get_surface(avctx, frame),
907                                                  NULL);
908 #endif
909         if (hr != E_PENDING || ++runs > 50)
910             break;
911         ff_dxva2_unlock(avctx);
912         av_usleep(2000);
913     } while(1);
914
915     if (FAILED(hr)) {
916         av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%x\n", (unsigned)hr);
917         ff_dxva2_unlock(avctx);
918         return -1;
919     }
920
921 #if CONFIG_D3D11VA
922     if (ff_dxva2_is_d3d11(avctx)) {
923         buffer = &buffer11[buffer_count];
924         type = D3D11_VIDEO_DECODER_BUFFER_PICTURE_PARAMETERS;
925     }
926 #endif
927 #if CONFIG_DXVA2
928     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
929         buffer = &buffer2[buffer_count];
930         type = DXVA2_PictureParametersBufferType;
931     }
932 #endif
933     result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
934                                     type,
935                                     pp, pp_size, 0);
936     if (result) {
937         av_log(avctx, AV_LOG_ERROR,
938                "Failed to add picture parameter buffer\n");
939         goto end;
940     }
941     buffer_count++;
942
943     if (qm_size > 0) {
944 #if CONFIG_D3D11VA
945         if (ff_dxva2_is_d3d11(avctx)) {
946             buffer = &buffer11[buffer_count];
947             type = D3D11_VIDEO_DECODER_BUFFER_INVERSE_QUANTIZATION_MATRIX;
948         }
949 #endif
950 #if CONFIG_DXVA2
951         if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
952             buffer = &buffer2[buffer_count];
953             type = DXVA2_InverseQuantizationMatrixBufferType;
954         }
955 #endif
956         result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
957                                         type,
958                                         qm, qm_size, 0);
959         if (result) {
960             av_log(avctx, AV_LOG_ERROR,
961                    "Failed to add inverse quantization matrix buffer\n");
962             goto end;
963         }
964         buffer_count++;
965     }
966
967 #if CONFIG_D3D11VA
968     if (ff_dxva2_is_d3d11(avctx)) {
969         buffer       = &buffer11[buffer_count + 0];
970         buffer_slice = &buffer11[buffer_count + 1];
971     }
972 #endif
973 #if CONFIG_DXVA2
974     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
975         buffer       = &buffer2[buffer_count + 0];
976         buffer_slice = &buffer2[buffer_count + 1];
977     }
978 #endif
979
980     result = commit_bs_si(avctx,
981                           buffer,
982                           buffer_slice);
983     if (result) {
984         av_log(avctx, AV_LOG_ERROR,
985                "Failed to add bitstream or slice control buffer\n");
986         goto end;
987     }
988     buffer_count += 2;
989
990     /* TODO Film Grain when possible */
991
992     assert(buffer_count == 1 + (qm_size > 0) + 2);
993
994 #if CONFIG_D3D11VA
995     if (ff_dxva2_is_d3d11(avctx))
996         hr = ID3D11VideoContext_SubmitDecoderBuffers(D3D11VA_CONTEXT(ctx)->video_context,
997                                                      D3D11VA_CONTEXT(ctx)->decoder,
998                                                      buffer_count, buffer11);
999 #endif
1000 #if CONFIG_DXVA2
1001     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
1002         DXVA2_DecodeExecuteParams exec = {
1003             .NumCompBuffers     = buffer_count,
1004             .pCompressedBuffers = buffer2,
1005             .pExtensionData     = NULL,
1006         };
1007         hr = IDirectXVideoDecoder_Execute(DXVA2_CONTEXT(ctx)->decoder, &exec);
1008     }
1009 #endif
1010     if (FAILED(hr)) {
1011         av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%x\n", (unsigned)hr);
1012         result = -1;
1013     }
1014
1015 end:
1016 #if CONFIG_D3D11VA
1017     if (ff_dxva2_is_d3d11(avctx))
1018         hr = ID3D11VideoContext_DecoderEndFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder);
1019 #endif
1020 #if CONFIG_DXVA2
1021     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
1022         hr = IDirectXVideoDecoder_EndFrame(DXVA2_CONTEXT(ctx)->decoder, NULL);
1023 #endif
1024     ff_dxva2_unlock(avctx);
1025     if (FAILED(hr)) {
1026         av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%x\n", (unsigned)hr);
1027         result = -1;
1028     }
1029
1030     return result;
1031 }
1032
1033 int ff_dxva2_is_d3d11(const AVCodecContext *avctx)
1034 {
1035     if (CONFIG_D3D11VA)
1036         return avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD ||
1037                avctx->pix_fmt == AV_PIX_FMT_D3D11;
1038     else
1039         return 0;
1040 }