]> git.sesse.net Git - ffmpeg/blob - ffmpeg_dxva2.c
avcodec/ac3dec: Reset SPX when switching from EAC3 to AC3
[ffmpeg] / ffmpeg_dxva2.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <windows.h>
20
21 #ifdef _WIN32_WINNT
22 #undef _WIN32_WINNT
23 #endif
24 #define _WIN32_WINNT 0x0600
25 #define DXVA2API_USE_BITFIELDS
26 #define COBJMACROS
27
28 #include <stdint.h>
29
30 #include <d3d9.h>
31 #include <dxva2api.h>
32
33 #include "ffmpeg.h"
34
35 #include "libavcodec/dxva2.h"
36
37 #include "libavutil/avassert.h"
38 #include "libavutil/buffer.h"
39 #include "libavutil/frame.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/pixfmt.h"
42
43 /* define all the GUIDs used directly here,
44    to avoid problems with inconsistent dxva2api.h versions in mingw-w64 and different MSVC version */
45 #include <initguid.h>
46 DEFINE_GUID(IID_IDirectXVideoDecoderService, 0xfc51a551,0xd5e7,0x11d9,0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
47
48 DEFINE_GUID(DXVA2_ModeMPEG2_VLD,      0xee27417f, 0x5e28,0x4e65,0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9);
49 DEFINE_GUID(DXVA2_ModeMPEG2and1_VLD,  0x86695f12, 0x340e,0x4f04,0x9f,0xd3,0x92,0x53,0xdd,0x32,0x74,0x60);
50 DEFINE_GUID(DXVA2_ModeH264_E,         0x1b81be68, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
51 DEFINE_GUID(DXVA2_ModeH264_F,         0x1b81be69, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
52 DEFINE_GUID(DXVADDI_Intel_ModeH264_E, 0x604F8E68, 0x4951,0x4C54,0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6);
53 DEFINE_GUID(DXVA2_ModeVC1_D,          0x1b81beA3, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
54 DEFINE_GUID(DXVA2_ModeVC1_D2010,      0x1b81beA4, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
55 DEFINE_GUID(DXVA2_ModeHEVC_VLD_Main,  0x5b11d51b, 0x2f4c,0x4452,0xbc,0xc3,0x09,0xf2,0xa1,0x16,0x0c,0xc0);
56 DEFINE_GUID(DXVA2_ModeHEVC_VLD_Main10,0x107af0e0, 0xef1a,0x4d19,0xab,0xa8,0x67,0xa1,0x63,0x07,0x3d,0x13);
57 DEFINE_GUID(DXVA2_ModeVP9_VLD_Profile0, 0x463707f8, 0xa1d0,0x4585,0x87,0x6d,0x83,0xaa,0x6d,0x60,0xb8,0x9e);
58 DEFINE_GUID(DXVA2_NoEncrypt,          0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
59 DEFINE_GUID(GUID_NULL,                0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
60
61 typedef IDirect3D9* WINAPI pDirect3DCreate9(UINT);
62 typedef HRESULT WINAPI pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **);
63
64 typedef struct dxva2_mode {
65   const GUID     *guid;
66   enum AVCodecID codec;
67 } dxva2_mode;
68
69 static const dxva2_mode dxva2_modes[] = {
70     /* MPEG-2 */
71     { &DXVA2_ModeMPEG2_VLD,      AV_CODEC_ID_MPEG2VIDEO },
72     { &DXVA2_ModeMPEG2and1_VLD,  AV_CODEC_ID_MPEG2VIDEO },
73
74     /* H.264 */
75     { &DXVA2_ModeH264_F,         AV_CODEC_ID_H264 },
76     { &DXVA2_ModeH264_E,         AV_CODEC_ID_H264 },
77     /* Intel specific H.264 mode */
78     { &DXVADDI_Intel_ModeH264_E, AV_CODEC_ID_H264 },
79
80     /* VC-1 / WMV3 */
81     { &DXVA2_ModeVC1_D2010,      AV_CODEC_ID_VC1  },
82     { &DXVA2_ModeVC1_D2010,      AV_CODEC_ID_WMV3 },
83     { &DXVA2_ModeVC1_D,          AV_CODEC_ID_VC1  },
84     { &DXVA2_ModeVC1_D,          AV_CODEC_ID_WMV3 },
85
86     /* HEVC/H.265 */
87     { &DXVA2_ModeHEVC_VLD_Main,  AV_CODEC_ID_HEVC },
88     { &DXVA2_ModeHEVC_VLD_Main10,AV_CODEC_ID_HEVC },
89
90     /* VP8/9 */
91     { &DXVA2_ModeVP9_VLD_Profile0, AV_CODEC_ID_VP9 },
92
93     { NULL,                      0 },
94 };
95
96 typedef struct surface_info {
97     int used;
98     uint64_t age;
99 } surface_info;
100
101 typedef struct DXVA2Context {
102     HMODULE d3dlib;
103     HMODULE dxva2lib;
104
105     HANDLE  deviceHandle;
106
107     IDirect3D9                  *d3d9;
108     IDirect3DDevice9            *d3d9device;
109     IDirect3DDeviceManager9     *d3d9devmgr;
110     IDirectXVideoDecoderService *decoder_service;
111     IDirectXVideoDecoder        *decoder;
112
113     GUID                        decoder_guid;
114     DXVA2_ConfigPictureDecode   decoder_config;
115
116     LPDIRECT3DSURFACE9          *surfaces;
117     surface_info                *surface_infos;
118     uint32_t                    num_surfaces;
119     uint64_t                    surface_age;
120     D3DFORMAT                   surface_format;
121
122     AVFrame                     *tmp_frame;
123 } DXVA2Context;
124
125 typedef struct DXVA2SurfaceWrapper {
126     DXVA2Context         *ctx;
127     LPDIRECT3DSURFACE9   surface;
128     IDirectXVideoDecoder *decoder;
129 } DXVA2SurfaceWrapper;
130
131 static void dxva2_destroy_decoder(AVCodecContext *s)
132 {
133     InputStream  *ist = s->opaque;
134     DXVA2Context *ctx = ist->hwaccel_ctx;
135     int i;
136
137     if (ctx->surfaces) {
138         for (i = 0; i < ctx->num_surfaces; i++) {
139             if (ctx->surfaces[i])
140                 IDirect3DSurface9_Release(ctx->surfaces[i]);
141         }
142     }
143     av_freep(&ctx->surfaces);
144     av_freep(&ctx->surface_infos);
145     ctx->num_surfaces = 0;
146     ctx->surface_age  = 0;
147
148     if (ctx->decoder) {
149         IDirectXVideoDecoder_Release(ctx->decoder);
150         ctx->decoder = NULL;
151     }
152 }
153
154 static void dxva2_uninit(AVCodecContext *s)
155 {
156     InputStream  *ist = s->opaque;
157     DXVA2Context *ctx = ist->hwaccel_ctx;
158
159     ist->hwaccel_uninit        = NULL;
160     ist->hwaccel_get_buffer    = NULL;
161     ist->hwaccel_retrieve_data = NULL;
162
163     if (ctx->decoder)
164         dxva2_destroy_decoder(s);
165
166     if (ctx->decoder_service)
167         IDirectXVideoDecoderService_Release(ctx->decoder_service);
168
169     if (ctx->d3d9devmgr && ctx->deviceHandle != INVALID_HANDLE_VALUE)
170         IDirect3DDeviceManager9_CloseDeviceHandle(ctx->d3d9devmgr, ctx->deviceHandle);
171
172     if (ctx->d3d9devmgr)
173         IDirect3DDeviceManager9_Release(ctx->d3d9devmgr);
174
175     if (ctx->d3d9device)
176         IDirect3DDevice9_Release(ctx->d3d9device);
177
178     if (ctx->d3d9)
179         IDirect3D9_Release(ctx->d3d9);
180
181     if (ctx->d3dlib)
182         FreeLibrary(ctx->d3dlib);
183
184     if (ctx->dxva2lib)
185         FreeLibrary(ctx->dxva2lib);
186
187     av_frame_free(&ctx->tmp_frame);
188
189     av_freep(&ist->hwaccel_ctx);
190     av_freep(&s->hwaccel_context);
191 }
192
193 static void dxva2_release_buffer(void *opaque, uint8_t *data)
194 {
195     DXVA2SurfaceWrapper *w   = opaque;
196     DXVA2Context        *ctx = w->ctx;
197     int i;
198
199     for (i = 0; i < ctx->num_surfaces; i++) {
200         if (ctx->surfaces[i] == w->surface) {
201             ctx->surface_infos[i].used = 0;
202             break;
203         }
204     }
205     IDirect3DSurface9_Release(w->surface);
206     IDirectXVideoDecoder_Release(w->decoder);
207     av_free(w);
208 }
209
210 static int dxva2_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
211 {
212     InputStream  *ist = s->opaque;
213     DXVA2Context *ctx = ist->hwaccel_ctx;
214     int i, old_unused = -1;
215     LPDIRECT3DSURFACE9 surface;
216     DXVA2SurfaceWrapper *w = NULL;
217
218     av_assert0(frame->format == AV_PIX_FMT_DXVA2_VLD);
219
220     for (i = 0; i < ctx->num_surfaces; i++) {
221         surface_info *info = &ctx->surface_infos[i];
222         if (!info->used && (old_unused == -1 || info->age < ctx->surface_infos[old_unused].age))
223             old_unused = i;
224     }
225     if (old_unused == -1) {
226         av_log(NULL, AV_LOG_ERROR, "No free DXVA2 surface!\n");
227         return AVERROR(ENOMEM);
228     }
229     i = old_unused;
230
231     surface = ctx->surfaces[i];
232
233     w = av_mallocz(sizeof(*w));
234     if (!w)
235         return AVERROR(ENOMEM);
236
237     frame->buf[0] = av_buffer_create((uint8_t*)surface, 0,
238                                      dxva2_release_buffer, w,
239                                      AV_BUFFER_FLAG_READONLY);
240     if (!frame->buf[0]) {
241         av_free(w);
242         return AVERROR(ENOMEM);
243     }
244
245     w->ctx     = ctx;
246     w->surface = surface;
247     IDirect3DSurface9_AddRef(w->surface);
248     w->decoder = ctx->decoder;
249     IDirectXVideoDecoder_AddRef(w->decoder);
250
251     ctx->surface_infos[i].used = 1;
252     ctx->surface_infos[i].age  = ctx->surface_age++;
253
254     frame->data[3] = (uint8_t *)surface;
255
256     return 0;
257 }
258
259 static int dxva2_retrieve_data(AVCodecContext *s, AVFrame *frame)
260 {
261     LPDIRECT3DSURFACE9 surface =  (LPDIRECT3DSURFACE9)frame->data[3];
262     InputStream        *ist = s->opaque;
263     DXVA2Context       *ctx = ist->hwaccel_ctx;
264     D3DSURFACE_DESC    surfaceDesc;
265     D3DLOCKED_RECT     LockedRect;
266     HRESULT            hr;
267     int                ret, nbytes;
268
269     IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
270
271     ctx->tmp_frame->width  = frame->width;
272     ctx->tmp_frame->height = frame->height;
273     switch (ctx->surface_format){
274     case MKTAG('N','V','1','2'):
275         ctx->tmp_frame->format = AV_PIX_FMT_NV12;
276         nbytes = 1;
277         break;
278     case MKTAG('P','0','1','0'):
279         ctx->tmp_frame->format = AV_PIX_FMT_P010;
280         nbytes = 2;
281         break;
282     default:
283         av_assert0(0);
284     }
285
286     ret = av_frame_get_buffer(ctx->tmp_frame, 32);
287     if (ret < 0)
288         return ret;
289
290     hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL, D3DLOCK_READONLY);
291     if (FAILED(hr)) {
292         av_log(NULL, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
293         return AVERROR_UNKNOWN;
294     }
295
296     av_image_copy_plane(ctx->tmp_frame->data[0], ctx->tmp_frame->linesize[0],
297                         (uint8_t*)LockedRect.pBits,
298                         LockedRect.Pitch, frame->width * nbytes, frame->height);
299
300     av_image_copy_plane(ctx->tmp_frame->data[1], ctx->tmp_frame->linesize[1],
301                         (uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
302                         LockedRect.Pitch, frame->width * nbytes, frame->height / 2);
303
304     IDirect3DSurface9_UnlockRect(surface);
305
306     ret = av_frame_copy_props(ctx->tmp_frame, frame);
307     if (ret < 0)
308         goto fail;
309
310     av_frame_unref(frame);
311     av_frame_move_ref(frame, ctx->tmp_frame);
312
313     return 0;
314 fail:
315     av_frame_unref(ctx->tmp_frame);
316     return ret;
317 }
318
319 static int dxva2_alloc(AVCodecContext *s)
320 {
321     InputStream  *ist = s->opaque;
322     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
323     DXVA2Context *ctx;
324     pDirect3DCreate9      *createD3D = NULL;
325     pCreateDeviceManager9 *createDeviceManager = NULL;
326     HRESULT hr;
327     D3DPRESENT_PARAMETERS d3dpp = {0};
328     D3DDISPLAYMODE        d3ddm;
329     unsigned resetToken = 0;
330     UINT adapter = D3DADAPTER_DEFAULT;
331
332     ctx = av_mallocz(sizeof(*ctx));
333     if (!ctx)
334         return AVERROR(ENOMEM);
335
336     ctx->deviceHandle = INVALID_HANDLE_VALUE;
337
338     ist->hwaccel_ctx           = ctx;
339     ist->hwaccel_uninit        = dxva2_uninit;
340     ist->hwaccel_get_buffer    = dxva2_get_buffer;
341     ist->hwaccel_retrieve_data = dxva2_retrieve_data;
342
343     ctx->d3dlib = LoadLibrary("d3d9.dll");
344     if (!ctx->d3dlib) {
345         av_log(NULL, loglevel, "Failed to load D3D9 library\n");
346         goto fail;
347     }
348     ctx->dxva2lib = LoadLibrary("dxva2.dll");
349     if (!ctx->dxva2lib) {
350         av_log(NULL, loglevel, "Failed to load DXVA2 library\n");
351         goto fail;
352     }
353
354     createD3D = (pDirect3DCreate9 *)GetProcAddress(ctx->d3dlib, "Direct3DCreate9");
355     if (!createD3D) {
356         av_log(NULL, loglevel, "Failed to locate Direct3DCreate9\n");
357         goto fail;
358     }
359     createDeviceManager = (pCreateDeviceManager9 *)GetProcAddress(ctx->dxva2lib, "DXVA2CreateDirect3DDeviceManager9");
360     if (!createDeviceManager) {
361         av_log(NULL, loglevel, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
362         goto fail;
363     }
364
365     ctx->d3d9 = createD3D(D3D_SDK_VERSION);
366     if (!ctx->d3d9) {
367         av_log(NULL, loglevel, "Failed to create IDirect3D object\n");
368         goto fail;
369     }
370
371     if (ist->hwaccel_device) {
372         adapter = atoi(ist->hwaccel_device);
373         av_log(NULL, AV_LOG_INFO, "Using HWAccel device %d\n", adapter);
374     }
375
376     IDirect3D9_GetAdapterDisplayMode(ctx->d3d9, adapter, &d3ddm);
377     d3dpp.Windowed         = TRUE;
378     d3dpp.BackBufferWidth  = 640;
379     d3dpp.BackBufferHeight = 480;
380     d3dpp.BackBufferCount  = 0;
381     d3dpp.BackBufferFormat = d3ddm.Format;
382     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
383     d3dpp.Flags            = D3DPRESENTFLAG_VIDEO;
384
385     hr = IDirect3D9_CreateDevice(ctx->d3d9, adapter, D3DDEVTYPE_HAL, GetDesktopWindow(),
386                                  D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
387                                  &d3dpp, &ctx->d3d9device);
388     if (FAILED(hr)) {
389         av_log(NULL, loglevel, "Failed to create Direct3D device\n");
390         goto fail;
391     }
392
393     hr = createDeviceManager(&resetToken, &ctx->d3d9devmgr);
394     if (FAILED(hr)) {
395         av_log(NULL, loglevel, "Failed to create Direct3D device manager\n");
396         goto fail;
397     }
398
399     hr = IDirect3DDeviceManager9_ResetDevice(ctx->d3d9devmgr, ctx->d3d9device, resetToken);
400     if (FAILED(hr)) {
401         av_log(NULL, loglevel, "Failed to bind Direct3D device to device manager\n");
402         goto fail;
403     }
404
405     hr = IDirect3DDeviceManager9_OpenDeviceHandle(ctx->d3d9devmgr, &ctx->deviceHandle);
406     if (FAILED(hr)) {
407         av_log(NULL, loglevel, "Failed to open device handle\n");
408         goto fail;
409     }
410
411     hr = IDirect3DDeviceManager9_GetVideoService(ctx->d3d9devmgr, ctx->deviceHandle, &IID_IDirectXVideoDecoderService, (void **)&ctx->decoder_service);
412     if (FAILED(hr)) {
413         av_log(NULL, loglevel, "Failed to create IDirectXVideoDecoderService\n");
414         goto fail;
415     }
416
417     ctx->tmp_frame = av_frame_alloc();
418     if (!ctx->tmp_frame)
419         goto fail;
420
421     s->hwaccel_context = av_mallocz(sizeof(struct dxva_context));
422     if (!s->hwaccel_context)
423         goto fail;
424
425     return 0;
426 fail:
427     dxva2_uninit(s);
428     return AVERROR(EINVAL);
429 }
430
431 static int dxva2_get_decoder_configuration(AVCodecContext *s, const GUID *device_guid,
432                                            const DXVA2_VideoDesc *desc,
433                                            DXVA2_ConfigPictureDecode *config)
434 {
435     InputStream  *ist = s->opaque;
436     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
437     DXVA2Context *ctx = ist->hwaccel_ctx;
438     unsigned cfg_count = 0, best_score = 0;
439     DXVA2_ConfigPictureDecode *cfg_list = NULL;
440     DXVA2_ConfigPictureDecode best_cfg = {{0}};
441     HRESULT hr;
442     int i;
443
444     hr = IDirectXVideoDecoderService_GetDecoderConfigurations(ctx->decoder_service, device_guid, desc, NULL, &cfg_count, &cfg_list);
445     if (FAILED(hr)) {
446         av_log(NULL, loglevel, "Unable to retrieve decoder configurations\n");
447         return AVERROR(EINVAL);
448     }
449
450     for (i = 0; i < cfg_count; i++) {
451         DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
452
453         unsigned score;
454         if (cfg->ConfigBitstreamRaw == 1)
455             score = 1;
456         else if (s->codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
457             score = 2;
458         else
459             continue;
460         if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
461             score += 16;
462         if (score > best_score) {
463             best_score = score;
464             best_cfg   = *cfg;
465         }
466     }
467     CoTaskMemFree(cfg_list);
468
469     if (!best_score) {
470         av_log(NULL, loglevel, "No valid decoder configuration available\n");
471         return AVERROR(EINVAL);
472     }
473
474     *config = best_cfg;
475     return 0;
476 }
477
478 static int dxva2_create_decoder(AVCodecContext *s)
479 {
480     InputStream  *ist = s->opaque;
481     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
482     DXVA2Context *ctx = ist->hwaccel_ctx;
483     struct dxva_context *dxva_ctx = s->hwaccel_context;
484     GUID *guid_list = NULL;
485     unsigned guid_count = 0, i, j;
486     GUID device_guid = GUID_NULL;
487     const D3DFORMAT surface_format = (s->sw_pix_fmt == AV_PIX_FMT_YUV420P10) ? MKTAG('P','0','1','0') : MKTAG('N','V','1','2');
488     D3DFORMAT target_format = 0;
489     DXVA2_VideoDesc desc = { 0 };
490     DXVA2_ConfigPictureDecode config;
491     HRESULT hr;
492     int surface_alignment;
493     int ret;
494
495     hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(ctx->decoder_service, &guid_count, &guid_list);
496     if (FAILED(hr)) {
497         av_log(NULL, loglevel, "Failed to retrieve decoder device GUIDs\n");
498         goto fail;
499     }
500
501     for (i = 0; dxva2_modes[i].guid; i++) {
502         D3DFORMAT *target_list = NULL;
503         unsigned target_count = 0;
504         const dxva2_mode *mode = &dxva2_modes[i];
505         if (mode->codec != s->codec_id)
506             continue;
507
508         for (j = 0; j < guid_count; j++) {
509             if (IsEqualGUID(mode->guid, &guid_list[j]))
510                 break;
511         }
512         if (j == guid_count)
513             continue;
514
515         hr = IDirectXVideoDecoderService_GetDecoderRenderTargets(ctx->decoder_service, mode->guid, &target_count, &target_list);
516         if (FAILED(hr)) {
517             continue;
518         }
519         for (j = 0; j < target_count; j++) {
520             const D3DFORMAT format = target_list[j];
521             if (format == surface_format) {
522                 target_format = format;
523                 break;
524             }
525         }
526         CoTaskMemFree(target_list);
527         if (target_format) {
528             device_guid = *mode->guid;
529             break;
530         }
531     }
532     CoTaskMemFree(guid_list);
533
534     if (IsEqualGUID(&device_guid, &GUID_NULL)) {
535         av_log(NULL, loglevel, "No decoder device for codec found\n");
536         goto fail;
537     }
538
539     desc.SampleWidth  = s->coded_width;
540     desc.SampleHeight = s->coded_height;
541     desc.Format       = target_format;
542
543     ret = dxva2_get_decoder_configuration(s, &device_guid, &desc, &config);
544     if (ret < 0) {
545         goto fail;
546     }
547
548     /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
549        but it causes issues for H.264 on certain AMD GPUs..... */
550     if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO)
551         surface_alignment = 32;
552     /* the HEVC DXVA2 spec asks for 128 pixel aligned surfaces to ensure
553        all coding features have enough room to work with */
554     else if  (s->codec_id == AV_CODEC_ID_HEVC)
555         surface_alignment = 128;
556     else
557         surface_alignment = 16;
558
559     /* 4 base work surfaces */
560     ctx->num_surfaces = 4;
561
562     /* add surfaces based on number of possible refs */
563     if (s->codec_id == AV_CODEC_ID_H264 || s->codec_id == AV_CODEC_ID_HEVC)
564         ctx->num_surfaces += 16;
565     else if (s->codec_id == AV_CODEC_ID_VP9)
566         ctx->num_surfaces += 8;
567     else
568         ctx->num_surfaces += 2;
569
570     /* add extra surfaces for frame threading */
571     if (s->active_thread_type & FF_THREAD_FRAME)
572         ctx->num_surfaces += s->thread_count;
573
574     ctx->surfaces      = av_mallocz(ctx->num_surfaces * sizeof(*ctx->surfaces));
575     ctx->surface_infos = av_mallocz(ctx->num_surfaces * sizeof(*ctx->surface_infos));
576     ctx->surface_format = target_format;
577
578     if (!ctx->surfaces || !ctx->surface_infos) {
579         av_log(NULL, loglevel, "Unable to allocate surface arrays\n");
580         goto fail;
581     }
582
583     hr = IDirectXVideoDecoderService_CreateSurface(ctx->decoder_service,
584                                                    FFALIGN(s->coded_width, surface_alignment),
585                                                    FFALIGN(s->coded_height, surface_alignment),
586                                                    ctx->num_surfaces - 1,
587                                                    target_format, D3DPOOL_DEFAULT, 0,
588                                                    DXVA2_VideoDecoderRenderTarget,
589                                                    ctx->surfaces, NULL);
590     if (FAILED(hr)) {
591         av_log(NULL, loglevel, "Failed to create %d video surfaces\n", ctx->num_surfaces);
592         goto fail;
593     }
594
595     hr = IDirectXVideoDecoderService_CreateVideoDecoder(ctx->decoder_service, &device_guid,
596                                                         &desc, &config, ctx->surfaces,
597                                                         ctx->num_surfaces, &ctx->decoder);
598     if (FAILED(hr)) {
599         av_log(NULL, loglevel, "Failed to create DXVA2 video decoder\n");
600         goto fail;
601     }
602
603     ctx->decoder_guid   = device_guid;
604     ctx->decoder_config = config;
605
606     dxva_ctx->cfg           = &ctx->decoder_config;
607     dxva_ctx->decoder       = ctx->decoder;
608     dxva_ctx->surface       = ctx->surfaces;
609     dxva_ctx->surface_count = ctx->num_surfaces;
610
611     if (IsEqualGUID(&ctx->decoder_guid, &DXVADDI_Intel_ModeH264_E))
612         dxva_ctx->workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
613
614     return 0;
615 fail:
616     dxva2_destroy_decoder(s);
617     return AVERROR(EINVAL);
618 }
619
620 int dxva2_init(AVCodecContext *s)
621 {
622     InputStream *ist = s->opaque;
623     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
624     DXVA2Context *ctx;
625     int ret;
626
627     if (!ist->hwaccel_ctx) {
628         ret = dxva2_alloc(s);
629         if (ret < 0)
630             return ret;
631     }
632     ctx = ist->hwaccel_ctx;
633
634     if (s->codec_id == AV_CODEC_ID_H264 &&
635         (s->profile & ~FF_PROFILE_H264_CONSTRAINED) > FF_PROFILE_H264_HIGH) {
636         av_log(NULL, loglevel, "Unsupported H.264 profile for DXVA2 HWAccel: %d\n", s->profile);
637         return AVERROR(EINVAL);
638     }
639
640     if (s->codec_id == AV_CODEC_ID_HEVC &&
641         s->profile != FF_PROFILE_HEVC_MAIN && s->profile != FF_PROFILE_HEVC_MAIN_10) {
642         av_log(NULL, loglevel, "Unsupported HEVC profile for DXVA2 HWAccel: %d\n", s->profile);
643         return AVERROR(EINVAL);
644     }
645
646     if (ctx->decoder)
647         dxva2_destroy_decoder(s);
648
649     ret = dxva2_create_decoder(s);
650     if (ret < 0) {
651         av_log(NULL, loglevel, "Error creating the DXVA2 decoder\n");
652         return ret;
653     }
654
655     return 0;
656 }