]> git.sesse.net Git - ffmpeg/blob - avconv_dxva2.c
mxf: Support AAC
[ffmpeg] / avconv_dxva2.c
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; 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 "avconv.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_NoEncrypt,          0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
56 DEFINE_GUID(GUID_NULL,                0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
57
58 typedef IDirect3D9* WINAPI pDirect3DCreate9(UINT);
59 typedef HRESULT WINAPI pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **);
60
61 typedef struct dxva2_mode {
62   const GUID     *guid;
63   enum AVCodecID codec;
64 } dxva2_mode;
65
66 static const dxva2_mode dxva2_modes[] = {
67     /* MPEG-2 */
68     { &DXVA2_ModeMPEG2_VLD,      AV_CODEC_ID_MPEG2VIDEO },
69     { &DXVA2_ModeMPEG2and1_VLD,  AV_CODEC_ID_MPEG2VIDEO },
70
71     /* H.264 */
72     { &DXVA2_ModeH264_F,         AV_CODEC_ID_H264 },
73     { &DXVA2_ModeH264_E,         AV_CODEC_ID_H264 },
74     /* Intel specific H.264 mode */
75     { &DXVADDI_Intel_ModeH264_E, AV_CODEC_ID_H264 },
76
77     /* VC-1 / WMV3 */
78     { &DXVA2_ModeVC1_D2010,      AV_CODEC_ID_VC1  },
79     { &DXVA2_ModeVC1_D2010,      AV_CODEC_ID_WMV3 },
80     { &DXVA2_ModeVC1_D,          AV_CODEC_ID_VC1  },
81     { &DXVA2_ModeVC1_D,          AV_CODEC_ID_WMV3 },
82
83     { NULL,                      0 },
84 };
85
86 typedef struct surface_info {
87     int used;
88     uint64_t age;
89 } surface_info;
90
91 typedef struct DXVA2Context {
92     HMODULE d3dlib;
93     HMODULE dxva2lib;
94
95     HANDLE  deviceHandle;
96
97     IDirect3D9                  *d3d9;
98     IDirect3DDevice9            *d3d9device;
99     IDirect3DDeviceManager9     *d3d9devmgr;
100     IDirectXVideoDecoderService *decoder_service;
101     IDirectXVideoDecoder        *decoder;
102
103     GUID                        decoder_guid;
104     DXVA2_ConfigPictureDecode   decoder_config;
105
106     LPDIRECT3DSURFACE9          *surfaces;
107     surface_info                *surface_infos;
108     uint32_t                    num_surfaces;
109     uint64_t                    surface_age;
110
111     AVFrame                     *tmp_frame;
112 } DXVA2Context;
113
114 typedef struct DXVA2SurfaceWrapper {
115     DXVA2Context         *ctx;
116     LPDIRECT3DSURFACE9   surface;
117     IDirectXVideoDecoder *decoder;
118 } DXVA2SurfaceWrapper;
119
120 static void dxva2_destroy_decoder(AVCodecContext *s)
121 {
122     InputStream  *ist = s->opaque;
123     DXVA2Context *ctx = ist->hwaccel_ctx;
124
125     if (ctx->surfaces) {
126         for (int i = 0; i < ctx->num_surfaces; i++) {
127             if (ctx->surfaces[i])
128                 IDirect3DSurface9_Release(ctx->surfaces[i]);
129         }
130     }
131     av_freep(&ctx->surfaces);
132     av_freep(&ctx->surface_infos);
133     ctx->num_surfaces = 0;
134     ctx->surface_age  = 0;
135
136     if (ctx->decoder) {
137         IDirectXVideoDecoder_Release(ctx->decoder);
138         ctx->decoder = NULL;
139     }
140 }
141
142 static void dxva2_uninit(AVCodecContext *s)
143 {
144     InputStream  *ist = s->opaque;
145     DXVA2Context *ctx = ist->hwaccel_ctx;
146
147     ist->hwaccel_uninit        = NULL;
148     ist->hwaccel_get_buffer    = NULL;
149     ist->hwaccel_retrieve_data = NULL;
150
151     if (ctx->decoder)
152         dxva2_destroy_decoder(s);
153
154     if (ctx->decoder_service)
155         IDirectXVideoDecoderService_Release(ctx->decoder_service);
156
157     if (ctx->d3d9devmgr && ctx->deviceHandle != INVALID_HANDLE_VALUE)
158         IDirect3DDeviceManager9_CloseDeviceHandle(ctx->d3d9devmgr, ctx->deviceHandle);
159
160     if (ctx->d3d9devmgr)
161         IDirect3DDeviceManager9_Release(ctx->d3d9devmgr);
162
163     if (ctx->d3d9device)
164         IDirect3DDevice9_Release(ctx->d3d9device);
165
166     if (ctx->d3d9)
167         IDirect3D9_Release(ctx->d3d9);
168
169     if (ctx->d3dlib)
170         FreeLibrary(ctx->d3dlib);
171
172     if (ctx->dxva2lib)
173         FreeLibrary(ctx->dxva2lib);
174
175     av_frame_free(&ctx->tmp_frame);
176
177     av_freep(&ist->hwaccel_ctx);
178     av_freep(&s->hwaccel_context);
179 }
180
181 static void dxva2_release_buffer(void *opaque, uint8_t *data)
182 {
183     DXVA2SurfaceWrapper *w   = opaque;
184     DXVA2Context        *ctx = w->ctx;
185     int i;
186
187     for (i = 0; i < ctx->num_surfaces; i++) {
188         if (ctx->surfaces[i] == w->surface) {
189             ctx->surface_infos[i].used = 0;
190             break;
191         }
192     }
193     IDirect3DSurface9_Release(w->surface);
194     IDirectXVideoDecoder_Release(w->decoder);
195     av_free(w);
196 }
197
198 static int dxva2_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
199 {
200     InputStream  *ist = s->opaque;
201     DXVA2Context *ctx = ist->hwaccel_ctx;
202     int i, old_unused = -1;
203     LPDIRECT3DSURFACE9 surface;
204     DXVA2SurfaceWrapper *w = NULL;
205
206     av_assert0(frame->format == AV_PIX_FMT_DXVA2_VLD);
207
208     for (i = 0; i < ctx->num_surfaces; i++) {
209         surface_info *info = &ctx->surface_infos[i];
210         if (!info->used && (old_unused == -1 || info->age < ctx->surface_infos[old_unused].age))
211             old_unused = i;
212     }
213     if (old_unused == -1) {
214         av_log(NULL, AV_LOG_ERROR, "No free DXVA2 surface!\n");
215         return AVERROR(ENOMEM);
216     }
217     i = old_unused;
218
219     surface = ctx->surfaces[i];
220
221     w = av_mallocz(sizeof(*w));
222     if (!w)
223         return AVERROR(ENOMEM);
224
225     frame->buf[0] = av_buffer_create((uint8_t*)surface, 0,
226                                      dxva2_release_buffer, w,
227                                      AV_BUFFER_FLAG_READONLY);
228     if (!frame->buf[0]) {
229         av_free(w);
230         return AVERROR(ENOMEM);
231     }
232
233     w->ctx     = ctx;
234     w->surface = surface;
235     IDirect3DSurface9_AddRef(w->surface);
236     w->decoder = ctx->decoder;
237     IDirectXVideoDecoder_AddRef(w->decoder);
238
239     ctx->surface_infos[i].used = 1;
240     ctx->surface_infos[i].age  = ctx->surface_age++;
241
242     frame->data[3] = (uint8_t *)surface;
243
244     return 0;
245 }
246
247 static int dxva2_retrieve_data(AVCodecContext *s, AVFrame *frame)
248 {
249     LPDIRECT3DSURFACE9 surface =  (LPDIRECT3DSURFACE9)frame->data[3];
250     InputStream        *ist = s->opaque;
251     DXVA2Context       *ctx = ist->hwaccel_ctx;
252     D3DSURFACE_DESC    surfaceDesc;
253     D3DLOCKED_RECT     LockedRect;
254     HRESULT            hr;
255     int                ret;
256
257     IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
258
259     ctx->tmp_frame->width  = frame->width;
260     ctx->tmp_frame->height = frame->height;
261     ctx->tmp_frame->format = AV_PIX_FMT_NV12;
262
263     ret = av_frame_get_buffer(ctx->tmp_frame, 32);
264     if (ret < 0)
265         return ret;
266
267     hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL, D3DLOCK_READONLY);
268     if (FAILED(hr)) {
269         av_log(NULL, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
270         return AVERROR_UNKNOWN;
271     }
272
273     av_image_copy_plane(ctx->tmp_frame->data[0], ctx->tmp_frame->linesize[0],
274                         (uint8_t*)LockedRect.pBits,
275                         LockedRect.Pitch, frame->width, frame->height);
276
277     av_image_copy_plane(ctx->tmp_frame->data[1], ctx->tmp_frame->linesize[1],
278                         (uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
279                         LockedRect.Pitch, frame->width, frame->height / 2);
280
281     IDirect3DSurface9_UnlockRect(surface);
282
283     ret = av_frame_copy_props(ctx->tmp_frame, frame);
284     if (ret < 0)
285         goto fail;
286
287     av_frame_unref(frame);
288     av_frame_move_ref(frame, ctx->tmp_frame);
289
290     return 0;
291 fail:
292     av_frame_unref(ctx->tmp_frame);
293     return ret;
294 }
295
296 static int dxva2_alloc(AVCodecContext *s)
297 {
298     InputStream  *ist = s->opaque;
299     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
300     DXVA2Context *ctx;
301     pDirect3DCreate9      *createD3D = NULL;
302     pCreateDeviceManager9 *createDeviceManager = NULL;
303     HRESULT hr;
304     D3DPRESENT_PARAMETERS d3dpp = {0};
305     D3DDISPLAYMODE        d3ddm;
306     unsigned resetToken = 0;
307     UINT adapter = D3DADAPTER_DEFAULT;
308
309     ctx = av_mallocz(sizeof(*ctx));
310     if (!ctx)
311         return AVERROR(ENOMEM);
312
313     ctx->deviceHandle = INVALID_HANDLE_VALUE;
314
315     ist->hwaccel_ctx           = ctx;
316     ist->hwaccel_uninit        = dxva2_uninit;
317     ist->hwaccel_get_buffer    = dxva2_get_buffer;
318     ist->hwaccel_retrieve_data = dxva2_retrieve_data;
319
320     ctx->d3dlib = LoadLibrary("d3d9.dll");
321     if (!ctx->d3dlib) {
322         av_log(NULL, loglevel, "Failed to load D3D9 library\n");
323         goto fail;
324     }
325     ctx->dxva2lib = LoadLibrary("dxva2.dll");
326     if (!ctx->dxva2lib) {
327         av_log(NULL, loglevel, "Failed to load DXVA2 library\n");
328         goto fail;
329     }
330
331     createD3D = (pDirect3DCreate9 *)GetProcAddress(ctx->d3dlib, "Direct3DCreate9");
332     if (!createD3D) {
333         av_log(NULL, loglevel, "Failed to locate Direct3DCreate9\n");
334         goto fail;
335     }
336     createDeviceManager = (pCreateDeviceManager9 *)GetProcAddress(ctx->dxva2lib, "DXVA2CreateDirect3DDeviceManager9");
337     if (!createDeviceManager) {
338         av_log(NULL, loglevel, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
339         goto fail;
340     }
341
342     ctx->d3d9 = createD3D(D3D_SDK_VERSION);
343     if (!ctx->d3d9) {
344         av_log(NULL, loglevel, "Failed to create IDirect3D object\n");
345         goto fail;
346     }
347
348     if (ist->hwaccel_device) {
349         adapter = atoi(ist->hwaccel_device);
350         av_log(NULL, AV_LOG_INFO, "Using HWAccel device %d\n", adapter);
351     }
352
353     IDirect3D9_GetAdapterDisplayMode(ctx->d3d9, adapter, &d3ddm);
354     d3dpp.Windowed         = TRUE;
355     d3dpp.BackBufferWidth  = 640;
356     d3dpp.BackBufferHeight = 480;
357     d3dpp.BackBufferCount  = 0;
358     d3dpp.BackBufferFormat = d3ddm.Format;
359     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
360     d3dpp.Flags            = D3DPRESENTFLAG_VIDEO;
361
362     hr = IDirect3D9_CreateDevice(ctx->d3d9, adapter, D3DDEVTYPE_HAL, GetShellWindow(),
363                                  D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
364                                  &d3dpp, &ctx->d3d9device);
365     if (FAILED(hr)) {
366         av_log(NULL, loglevel, "Failed to create Direct3D device\n");
367         goto fail;
368     }
369
370     hr = createDeviceManager(&resetToken, &ctx->d3d9devmgr);
371     if (FAILED(hr)) {
372         av_log(NULL, loglevel, "Failed to create Direct3D device manager\n");
373         goto fail;
374     }
375
376     hr = IDirect3DDeviceManager9_ResetDevice(ctx->d3d9devmgr, ctx->d3d9device, resetToken);
377     if (FAILED(hr)) {
378         av_log(NULL, loglevel, "Failed to bind Direct3D device to device manager\n");
379         goto fail;
380     }
381
382     hr = IDirect3DDeviceManager9_OpenDeviceHandle(ctx->d3d9devmgr, &ctx->deviceHandle);
383     if (FAILED(hr)) {
384         av_log(NULL, loglevel, "Failed to open device handle\n");
385         goto fail;
386     }
387
388     hr = IDirect3DDeviceManager9_GetVideoService(ctx->d3d9devmgr, ctx->deviceHandle, &IID_IDirectXVideoDecoderService, (void **)&ctx->decoder_service);
389     if (FAILED(hr)) {
390         av_log(NULL, loglevel, "Failed to create IDirectXVideoDecoderService\n");
391         goto fail;
392     }
393
394     ctx->tmp_frame = av_frame_alloc();
395     if (!ctx->tmp_frame)
396         goto fail;
397
398     s->hwaccel_context = av_mallocz(sizeof(struct dxva_context));
399     if (!s->hwaccel_context)
400         goto fail;
401
402     return 0;
403 fail:
404     dxva2_uninit(s);
405     return AVERROR(EINVAL);
406 }
407
408 static int dxva2_get_decoder_configuration(AVCodecContext *s, const GUID *device_guid,
409                                            const DXVA2_VideoDesc *desc,
410                                            DXVA2_ConfigPictureDecode *config)
411 {
412     InputStream  *ist = s->opaque;
413     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
414     DXVA2Context *ctx = ist->hwaccel_ctx;
415     unsigned cfg_count = 0, best_score = 0;
416     DXVA2_ConfigPictureDecode *cfg_list = NULL;
417     DXVA2_ConfigPictureDecode best_cfg = {{0}};
418     HRESULT hr;
419     int i;
420
421     hr = IDirectXVideoDecoderService_GetDecoderConfigurations(ctx->decoder_service, device_guid, desc, NULL, &cfg_count, &cfg_list);
422     if (FAILED(hr)) {
423         av_log(NULL, loglevel, "Unable to retrieve decoder configurations\n");
424         return AVERROR(EINVAL);
425     }
426
427     for (i = 0; i < cfg_count; i++) {
428         DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
429
430         unsigned score;
431         if (cfg->ConfigBitstreamRaw == 1)
432             score = 1;
433         else if (s->codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
434             score = 2;
435         else
436             continue;
437         if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
438             score += 16;
439         if (score > best_score) {
440             best_score = score;
441             best_cfg   = *cfg;
442         }
443     }
444     CoTaskMemFree(cfg_list);
445
446     if (!best_score) {
447         av_log(NULL, loglevel, "No valid decoder configuration available\n");
448         return AVERROR(EINVAL);
449     }
450
451     *config = best_cfg;
452     return 0;
453 }
454
455 static int dxva2_create_decoder(AVCodecContext *s)
456 {
457     InputStream  *ist = s->opaque;
458     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
459     DXVA2Context *ctx = ist->hwaccel_ctx;
460     struct dxva_context *dxva_ctx = s->hwaccel_context;
461     GUID *guid_list = NULL;
462     unsigned guid_count = 0, i, j;
463     GUID device_guid = GUID_NULL;
464     D3DFORMAT target_format = 0;
465     DXVA2_VideoDesc desc = { 0 };
466     DXVA2_ConfigPictureDecode config;
467     HRESULT hr;
468     int surface_alignment;
469     int ret;
470
471     hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(ctx->decoder_service, &guid_count, &guid_list);
472     if (FAILED(hr)) {
473         av_log(NULL, loglevel, "Failed to retrieve decoder device GUIDs\n");
474         goto fail;
475     }
476
477     for (i = 0; dxva2_modes[i].guid; i++) {
478         D3DFORMAT *target_list = NULL;
479         unsigned target_count = 0;
480         const dxva2_mode *mode = &dxva2_modes[i];
481         if (mode->codec != s->codec_id)
482             continue;
483
484         for (j = 0; j < guid_count; j++) {
485             if (IsEqualGUID(mode->guid, &guid_list[j]))
486                 break;
487         }
488         if (j == guid_count)
489             continue;
490
491         hr = IDirectXVideoDecoderService_GetDecoderRenderTargets(ctx->decoder_service, mode->guid, &target_count, &target_list);
492         if (FAILED(hr)) {
493             continue;
494         }
495         for (j = 0; j < target_count; j++) {
496             const D3DFORMAT format = target_list[j];
497             if (format == MKTAG('N','V','1','2')) {
498                 target_format = format;
499                 break;
500             }
501         }
502         CoTaskMemFree(target_list);
503         if (target_format) {
504             device_guid = *mode->guid;
505             break;
506         }
507     }
508     CoTaskMemFree(guid_list);
509
510     if (IsEqualGUID(&device_guid, &GUID_NULL)) {
511         av_log(NULL, loglevel, "No decoder device for codec found\n");
512         goto fail;
513     }
514
515     desc.SampleWidth  = s->coded_width;
516     desc.SampleHeight = s->coded_height;
517     desc.Format       = target_format;
518
519     ret = dxva2_get_decoder_configuration(s, &device_guid, &desc, &config);
520     if (ret < 0) {
521         goto fail;
522     }
523
524     /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
525        but it causes issues for H.264 on certain AMD GPUs..... */
526     if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO)
527         surface_alignment = 32;
528     else
529         surface_alignment = 16;
530
531     /* 4 base work surfaces */
532     ctx->num_surfaces = 4;
533
534     /* add surfaces based on number of possible refs */
535     if (s->codec_id == AV_CODEC_ID_H264)
536         ctx->num_surfaces += 16;
537     else
538         ctx->num_surfaces += 2;
539
540     /* add extra surfaces for frame threading */
541     if (s->active_thread_type & FF_THREAD_FRAME)
542         ctx->num_surfaces += s->thread_count;
543
544     ctx->surfaces      = av_mallocz(ctx->num_surfaces * sizeof(*ctx->surfaces));
545     ctx->surface_infos = av_mallocz(ctx->num_surfaces * sizeof(*ctx->surface_infos));
546
547     if (!ctx->surfaces || !ctx->surface_infos) {
548         av_log(NULL, loglevel, "Unable to allocate surface arrays\n");
549         goto fail;
550     }
551
552     hr = IDirectXVideoDecoderService_CreateSurface(ctx->decoder_service,
553                                                    FFALIGN(s->coded_width, surface_alignment),
554                                                    FFALIGN(s->coded_height, surface_alignment),
555                                                    ctx->num_surfaces - 1,
556                                                    target_format, D3DPOOL_DEFAULT, 0,
557                                                    DXVA2_VideoDecoderRenderTarget,
558                                                    ctx->surfaces, NULL);
559     if (FAILED(hr)) {
560         av_log(NULL, loglevel, "Failed to create %d video surfaces\n", ctx->num_surfaces);
561         goto fail;
562     }
563
564     hr = IDirectXVideoDecoderService_CreateVideoDecoder(ctx->decoder_service, &device_guid,
565                                                         &desc, &config, ctx->surfaces,
566                                                         ctx->num_surfaces, &ctx->decoder);
567     if (FAILED(hr)) {
568         av_log(NULL, loglevel, "Failed to create DXVA2 video decoder\n");
569         goto fail;
570     }
571
572     ctx->decoder_guid   = device_guid;
573     ctx->decoder_config = config;
574
575     dxva_ctx->cfg           = &ctx->decoder_config;
576     dxva_ctx->decoder       = ctx->decoder;
577     dxva_ctx->surface       = ctx->surfaces;
578     dxva_ctx->surface_count = ctx->num_surfaces;
579
580     if (IsEqualGUID(&ctx->decoder_guid, &DXVADDI_Intel_ModeH264_E))
581         dxva_ctx->workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
582
583     return 0;
584 fail:
585     dxva2_destroy_decoder(s);
586     return AVERROR(EINVAL);
587 }
588
589 int dxva2_init(AVCodecContext *s)
590 {
591     InputStream *ist = s->opaque;
592     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
593     DXVA2Context *ctx;
594     int ret;
595
596     if (!ist->hwaccel_ctx) {
597         ret = dxva2_alloc(s);
598         if (ret < 0)
599             return ret;
600     }
601     ctx = ist->hwaccel_ctx;
602
603     if (s->codec_id == AV_CODEC_ID_H264 &&
604         (s->profile & ~FF_PROFILE_H264_CONSTRAINED) > FF_PROFILE_H264_HIGH) {
605         av_log(NULL, loglevel, "Unsupported H.264 profile for DXVA2 HWAccel: %d\n", s->profile);
606         return AVERROR(EINVAL);
607     }
608
609     if (ctx->decoder)
610         dxva2_destroy_decoder(s);
611
612     ret = dxva2_create_decoder(s);
613     if (ret < 0) {
614         av_log(NULL, loglevel, "Error creating the DXVA2 decoder\n");
615         return ret;
616     }
617
618     return 0;
619 }