]> git.sesse.net Git - ffmpeg/blob - ffmpeg_dxva2.c
avcodec/nvenc: Bring encoder names in line with other encoders
[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 #include "libavutil/hwcontext.h"
44 #include "libavutil/hwcontext_dxva2.h"
45
46 /* define all the GUIDs used directly here,
47    to avoid problems with inconsistent dxva2api.h versions in mingw-w64 and different MSVC version */
48 #include <initguid.h>
49 DEFINE_GUID(IID_IDirectXVideoDecoderService, 0xfc51a551,0xd5e7,0x11d9,0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
50
51 DEFINE_GUID(DXVA2_ModeMPEG2_VLD,      0xee27417f, 0x5e28,0x4e65,0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9);
52 DEFINE_GUID(DXVA2_ModeMPEG2and1_VLD,  0x86695f12, 0x340e,0x4f04,0x9f,0xd3,0x92,0x53,0xdd,0x32,0x74,0x60);
53 DEFINE_GUID(DXVA2_ModeH264_E,         0x1b81be68, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
54 DEFINE_GUID(DXVA2_ModeH264_F,         0x1b81be69, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
55 DEFINE_GUID(DXVADDI_Intel_ModeH264_E, 0x604F8E68, 0x4951,0x4C54,0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6);
56 DEFINE_GUID(DXVA2_ModeVC1_D,          0x1b81beA3, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
57 DEFINE_GUID(DXVA2_ModeVC1_D2010,      0x1b81beA4, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
58 DEFINE_GUID(DXVA2_ModeHEVC_VLD_Main,  0x5b11d51b, 0x2f4c,0x4452,0xbc,0xc3,0x09,0xf2,0xa1,0x16,0x0c,0xc0);
59 DEFINE_GUID(DXVA2_ModeHEVC_VLD_Main10,0x107af0e0, 0xef1a,0x4d19,0xab,0xa8,0x67,0xa1,0x63,0x07,0x3d,0x13);
60 DEFINE_GUID(DXVA2_ModeVP9_VLD_Profile0, 0x463707f8, 0xa1d0,0x4585,0x87,0x6d,0x83,0xaa,0x6d,0x60,0xb8,0x9e);
61 DEFINE_GUID(DXVA2_NoEncrypt,          0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
62 DEFINE_GUID(GUID_NULL,                0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
63
64 typedef IDirect3D9* WINAPI pDirect3DCreate9(UINT);
65 typedef HRESULT WINAPI pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **);
66
67 typedef struct dxva2_mode {
68   const GUID     *guid;
69   enum AVCodecID codec;
70 } dxva2_mode;
71
72 static const dxva2_mode dxva2_modes[] = {
73     /* MPEG-2 */
74     { &DXVA2_ModeMPEG2_VLD,      AV_CODEC_ID_MPEG2VIDEO },
75     { &DXVA2_ModeMPEG2and1_VLD,  AV_CODEC_ID_MPEG2VIDEO },
76
77     /* H.264 */
78     { &DXVA2_ModeH264_F,         AV_CODEC_ID_H264 },
79     { &DXVA2_ModeH264_E,         AV_CODEC_ID_H264 },
80     /* Intel specific H.264 mode */
81     { &DXVADDI_Intel_ModeH264_E, AV_CODEC_ID_H264 },
82
83     /* VC-1 / WMV3 */
84     { &DXVA2_ModeVC1_D2010,      AV_CODEC_ID_VC1  },
85     { &DXVA2_ModeVC1_D2010,      AV_CODEC_ID_WMV3 },
86     { &DXVA2_ModeVC1_D,          AV_CODEC_ID_VC1  },
87     { &DXVA2_ModeVC1_D,          AV_CODEC_ID_WMV3 },
88
89     /* HEVC/H.265 */
90     { &DXVA2_ModeHEVC_VLD_Main,  AV_CODEC_ID_HEVC },
91     { &DXVA2_ModeHEVC_VLD_Main10,AV_CODEC_ID_HEVC },
92
93     /* VP8/9 */
94     { &DXVA2_ModeVP9_VLD_Profile0, AV_CODEC_ID_VP9 },
95
96     { NULL,                      0 },
97 };
98
99 typedef struct DXVA2DevicePriv {
100     HMODULE d3dlib;
101     HMODULE dxva2lib;
102
103     HANDLE  deviceHandle;
104
105     IDirect3D9                  *d3d9;
106     IDirect3DDevice9            *d3d9device;
107 } DXVA2DevicePriv;
108
109 typedef struct DXVA2Context {
110     IDirectXVideoDecoder        *decoder;
111
112     GUID                        decoder_guid;
113     DXVA2_ConfigPictureDecode   decoder_config;
114     IDirectXVideoDecoderService *decoder_service;
115
116     AVFrame                     *tmp_frame;
117
118     AVBufferRef                 *hw_device_ctx;
119     AVBufferRef                 *hw_frames_ctx;
120 } DXVA2Context;
121
122 static void dxva2_device_uninit(AVHWDeviceContext *ctx)
123 {
124     AVDXVA2DeviceContext *hwctx = ctx->hwctx;
125     DXVA2DevicePriv       *priv = ctx->user_opaque;
126
127     if (hwctx->devmgr && priv->deviceHandle != INVALID_HANDLE_VALUE)
128         IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->devmgr, priv->deviceHandle);
129
130     if (hwctx->devmgr)
131         IDirect3DDeviceManager9_Release(hwctx->devmgr);
132
133     if (priv->d3d9device)
134         IDirect3DDevice9_Release(priv->d3d9device);
135
136     if (priv->d3d9)
137         IDirect3D9_Release(priv->d3d9);
138
139     if (priv->d3dlib)
140         FreeLibrary(priv->d3dlib);
141
142     if (priv->dxva2lib)
143         FreeLibrary(priv->dxva2lib);
144
145     av_freep(&ctx->user_opaque);
146 }
147
148 static void dxva2_uninit(AVCodecContext *s)
149 {
150     InputStream  *ist = s->opaque;
151     DXVA2Context *ctx = ist->hwaccel_ctx;
152
153     ist->hwaccel_uninit        = NULL;
154     ist->hwaccel_get_buffer    = NULL;
155     ist->hwaccel_retrieve_data = NULL;
156
157     if (ctx->decoder_service)
158         IDirectXVideoDecoderService_Release(ctx->decoder_service);
159
160     av_buffer_unref(&ctx->hw_frames_ctx);
161     av_buffer_unref(&ctx->hw_device_ctx);
162
163     av_frame_free(&ctx->tmp_frame);
164
165     av_freep(&ist->hwaccel_ctx);
166     av_freep(&s->hwaccel_context);
167 }
168
169 static int dxva2_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
170 {
171     InputStream  *ist = s->opaque;
172     DXVA2Context *ctx = ist->hwaccel_ctx;
173
174     return av_hwframe_get_buffer(ctx->hw_frames_ctx, frame, 0);
175 }
176
177 static int dxva2_retrieve_data(AVCodecContext *s, AVFrame *frame)
178 {
179     InputStream        *ist = s->opaque;
180     DXVA2Context       *ctx = ist->hwaccel_ctx;
181     int                ret;
182
183     ret = av_hwframe_transfer_data(ctx->tmp_frame, frame, 0);
184     if (ret < 0)
185         return ret;
186
187     ret = av_frame_copy_props(ctx->tmp_frame, frame);
188     if (ret < 0) {
189         av_frame_unref(ctx->tmp_frame);
190         return ret;
191     }
192
193     av_frame_unref(frame);
194     av_frame_move_ref(frame, ctx->tmp_frame);
195
196     return 0;
197 }
198
199 static int dxva2_alloc(AVCodecContext *s)
200 {
201     InputStream  *ist = s->opaque;
202     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
203     DXVA2Context *ctx;
204     pDirect3DCreate9      *createD3D = NULL;
205     pCreateDeviceManager9 *createDeviceManager = NULL;
206     HRESULT hr;
207     D3DPRESENT_PARAMETERS d3dpp = {0};
208     D3DDISPLAYMODE        d3ddm;
209     unsigned resetToken = 0;
210     UINT adapter = D3DADAPTER_DEFAULT;
211
212     AVHWDeviceContext    *device_ctx;
213     AVDXVA2DeviceContext *device_hwctx;
214     DXVA2DevicePriv      *device_priv;
215     int ret;
216
217     ctx = av_mallocz(sizeof(*ctx));
218     if (!ctx)
219         return AVERROR(ENOMEM);
220
221     ist->hwaccel_ctx           = ctx;
222     ist->hwaccel_uninit        = dxva2_uninit;
223     ist->hwaccel_get_buffer    = dxva2_get_buffer;
224     ist->hwaccel_retrieve_data = dxva2_retrieve_data;
225
226     ctx->hw_device_ctx = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_DXVA2);
227     if (!ctx->hw_device_ctx)
228         goto fail;
229
230     device_ctx   = (AVHWDeviceContext*)ctx->hw_device_ctx->data;
231     device_hwctx = device_ctx->hwctx;
232
233     device_priv = av_mallocz(sizeof(*device_priv));
234     if (!device_priv)
235         goto fail;
236
237     device_ctx->user_opaque = device_priv;
238     device_ctx->free        = dxva2_device_uninit;
239
240     device_priv->deviceHandle = INVALID_HANDLE_VALUE;
241
242     device_priv->d3dlib = LoadLibrary("d3d9.dll");
243     if (!device_priv->d3dlib) {
244         av_log(NULL, loglevel, "Failed to load D3D9 library\n");
245         goto fail;
246     }
247     device_priv->dxva2lib = LoadLibrary("dxva2.dll");
248     if (!device_priv->dxva2lib) {
249         av_log(NULL, loglevel, "Failed to load DXVA2 library\n");
250         goto fail;
251     }
252
253     createD3D = (pDirect3DCreate9 *)GetProcAddress(device_priv->d3dlib, "Direct3DCreate9");
254     if (!createD3D) {
255         av_log(NULL, loglevel, "Failed to locate Direct3DCreate9\n");
256         goto fail;
257     }
258     createDeviceManager = (pCreateDeviceManager9 *)GetProcAddress(device_priv->dxva2lib, "DXVA2CreateDirect3DDeviceManager9");
259     if (!createDeviceManager) {
260         av_log(NULL, loglevel, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
261         goto fail;
262     }
263
264     device_priv->d3d9 = createD3D(D3D_SDK_VERSION);
265     if (!device_priv->d3d9) {
266         av_log(NULL, loglevel, "Failed to create IDirect3D object\n");
267         goto fail;
268     }
269
270     if (ist->hwaccel_device) {
271         adapter = atoi(ist->hwaccel_device);
272         av_log(NULL, AV_LOG_INFO, "Using HWAccel device %d\n", adapter);
273     }
274
275     IDirect3D9_GetAdapterDisplayMode(device_priv->d3d9, adapter, &d3ddm);
276     d3dpp.Windowed         = TRUE;
277     d3dpp.BackBufferWidth  = 640;
278     d3dpp.BackBufferHeight = 480;
279     d3dpp.BackBufferCount  = 0;
280     d3dpp.BackBufferFormat = d3ddm.Format;
281     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
282     d3dpp.Flags            = D3DPRESENTFLAG_VIDEO;
283
284     hr = IDirect3D9_CreateDevice(device_priv->d3d9, adapter, D3DDEVTYPE_HAL, GetDesktopWindow(),
285                                  D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
286                                  &d3dpp, &device_priv->d3d9device);
287     if (FAILED(hr)) {
288         av_log(NULL, loglevel, "Failed to create Direct3D device\n");
289         goto fail;
290     }
291
292     hr = createDeviceManager(&resetToken, &device_hwctx->devmgr);
293     if (FAILED(hr)) {
294         av_log(NULL, loglevel, "Failed to create Direct3D device manager\n");
295         goto fail;
296     }
297
298     hr = IDirect3DDeviceManager9_ResetDevice(device_hwctx->devmgr, device_priv->d3d9device, resetToken);
299     if (FAILED(hr)) {
300         av_log(NULL, loglevel, "Failed to bind Direct3D device to device manager\n");
301         goto fail;
302     }
303
304     hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr, &device_priv->deviceHandle);
305     if (FAILED(hr)) {
306         av_log(NULL, loglevel, "Failed to open device handle\n");
307         goto fail;
308     }
309
310     hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr, device_priv->deviceHandle, &IID_IDirectXVideoDecoderService, (void **)&ctx->decoder_service);
311     if (FAILED(hr)) {
312         av_log(NULL, loglevel, "Failed to create IDirectXVideoDecoderService\n");
313         goto fail;
314     }
315
316     ret = av_hwdevice_ctx_init(ctx->hw_device_ctx);
317     if (ret < 0) {
318         av_log(NULL, loglevel, "Failed to initialize the HW device context\n");
319         goto fail;
320     }
321
322     ctx->tmp_frame = av_frame_alloc();
323     if (!ctx->tmp_frame)
324         goto fail;
325
326     s->hwaccel_context = av_mallocz(sizeof(struct dxva_context));
327     if (!s->hwaccel_context)
328         goto fail;
329
330     return 0;
331 fail:
332     dxva2_uninit(s);
333     return AVERROR(EINVAL);
334 }
335
336 static int dxva2_get_decoder_configuration(AVCodecContext *s, const GUID *device_guid,
337                                            const DXVA2_VideoDesc *desc,
338                                            DXVA2_ConfigPictureDecode *config)
339 {
340     InputStream  *ist = s->opaque;
341     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
342     DXVA2Context *ctx = ist->hwaccel_ctx;
343     unsigned cfg_count = 0, best_score = 0;
344     DXVA2_ConfigPictureDecode *cfg_list = NULL;
345     DXVA2_ConfigPictureDecode best_cfg = {{0}};
346     HRESULT hr;
347     int i;
348
349     hr = IDirectXVideoDecoderService_GetDecoderConfigurations(ctx->decoder_service, device_guid, desc, NULL, &cfg_count, &cfg_list);
350     if (FAILED(hr)) {
351         av_log(NULL, loglevel, "Unable to retrieve decoder configurations\n");
352         return AVERROR(EINVAL);
353     }
354
355     for (i = 0; i < cfg_count; i++) {
356         DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
357
358         unsigned score;
359         if (cfg->ConfigBitstreamRaw == 1)
360             score = 1;
361         else if (s->codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
362             score = 2;
363         else
364             continue;
365         if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
366             score += 16;
367         if (score > best_score) {
368             best_score = score;
369             best_cfg   = *cfg;
370         }
371     }
372     CoTaskMemFree(cfg_list);
373
374     if (!best_score) {
375         av_log(NULL, loglevel, "No valid decoder configuration available\n");
376         return AVERROR(EINVAL);
377     }
378
379     *config = best_cfg;
380     return 0;
381 }
382
383 static int dxva2_create_decoder(AVCodecContext *s)
384 {
385     InputStream  *ist = s->opaque;
386     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
387     DXVA2Context *ctx = ist->hwaccel_ctx;
388     struct dxva_context *dxva_ctx = s->hwaccel_context;
389     GUID *guid_list = NULL;
390     unsigned guid_count = 0, i, j;
391     GUID device_guid = GUID_NULL;
392     const D3DFORMAT surface_format = (s->sw_pix_fmt == AV_PIX_FMT_YUV420P10) ? MKTAG('P','0','1','0') : MKTAG('N','V','1','2');
393     D3DFORMAT target_format = 0;
394     DXVA2_VideoDesc desc = { 0 };
395     DXVA2_ConfigPictureDecode config;
396     HRESULT hr;
397     int surface_alignment, num_surfaces;
398     int ret;
399
400     AVDXVA2FramesContext *frames_hwctx;
401     AVHWFramesContext *frames_ctx;
402
403     hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(ctx->decoder_service, &guid_count, &guid_list);
404     if (FAILED(hr)) {
405         av_log(NULL, loglevel, "Failed to retrieve decoder device GUIDs\n");
406         goto fail;
407     }
408
409     for (i = 0; dxva2_modes[i].guid; i++) {
410         D3DFORMAT *target_list = NULL;
411         unsigned target_count = 0;
412         const dxva2_mode *mode = &dxva2_modes[i];
413         if (mode->codec != s->codec_id)
414             continue;
415
416         for (j = 0; j < guid_count; j++) {
417             if (IsEqualGUID(mode->guid, &guid_list[j]))
418                 break;
419         }
420         if (j == guid_count)
421             continue;
422
423         hr = IDirectXVideoDecoderService_GetDecoderRenderTargets(ctx->decoder_service, mode->guid, &target_count, &target_list);
424         if (FAILED(hr)) {
425             continue;
426         }
427         for (j = 0; j < target_count; j++) {
428             const D3DFORMAT format = target_list[j];
429             if (format == surface_format) {
430                 target_format = format;
431                 break;
432             }
433         }
434         CoTaskMemFree(target_list);
435         if (target_format) {
436             device_guid = *mode->guid;
437             break;
438         }
439     }
440     CoTaskMemFree(guid_list);
441
442     if (IsEqualGUID(&device_guid, &GUID_NULL)) {
443         av_log(NULL, loglevel, "No decoder device for codec found\n");
444         goto fail;
445     }
446
447     desc.SampleWidth  = s->coded_width;
448     desc.SampleHeight = s->coded_height;
449     desc.Format       = target_format;
450
451     ret = dxva2_get_decoder_configuration(s, &device_guid, &desc, &config);
452     if (ret < 0) {
453         goto fail;
454     }
455
456     /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
457        but it causes issues for H.264 on certain AMD GPUs..... */
458     if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO)
459         surface_alignment = 32;
460     /* the HEVC DXVA2 spec asks for 128 pixel aligned surfaces to ensure
461        all coding features have enough room to work with */
462     else if  (s->codec_id == AV_CODEC_ID_HEVC)
463         surface_alignment = 128;
464     else
465         surface_alignment = 16;
466
467     /* 4 base work surfaces */
468     num_surfaces = 4;
469
470     /* add surfaces based on number of possible refs */
471     if (s->codec_id == AV_CODEC_ID_H264 || s->codec_id == AV_CODEC_ID_HEVC)
472         num_surfaces += 16;
473     else if (s->codec_id == AV_CODEC_ID_VP9)
474         num_surfaces += 8;
475     else
476         num_surfaces += 2;
477
478     /* add extra surfaces for frame threading */
479     if (s->active_thread_type & FF_THREAD_FRAME)
480         num_surfaces += s->thread_count;
481
482     ctx->hw_frames_ctx = av_hwframe_ctx_alloc(ctx->hw_device_ctx);
483     if (!ctx->hw_frames_ctx)
484         goto fail;
485     frames_ctx   = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
486     frames_hwctx = frames_ctx->hwctx;
487
488     frames_ctx->format            = AV_PIX_FMT_DXVA2_VLD;
489     frames_ctx->sw_format         = (target_format == MKTAG('P','0','1','0') ? AV_PIX_FMT_P010 : AV_PIX_FMT_NV12);
490     frames_ctx->width             = FFALIGN(s->coded_width, surface_alignment);
491     frames_ctx->height            = FFALIGN(s->coded_height, surface_alignment);
492     frames_ctx->initial_pool_size = num_surfaces;
493
494     frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
495
496     ret = av_hwframe_ctx_init(ctx->hw_frames_ctx);
497     if (ret < 0) {
498         av_log(NULL, loglevel, "Failed to initialize the HW frames context\n");
499         goto fail;
500     }
501
502     hr = IDirectXVideoDecoderService_CreateVideoDecoder(ctx->decoder_service, &device_guid,
503                                                         &desc, &config, frames_hwctx->surfaces,
504                                                         frames_hwctx->nb_surfaces, &frames_hwctx->decoder_to_release);
505     if (FAILED(hr)) {
506         av_log(NULL, loglevel, "Failed to create DXVA2 video decoder\n");
507         goto fail;
508     }
509
510     ctx->decoder_guid   = device_guid;
511     ctx->decoder_config = config;
512
513     dxva_ctx->cfg           = &ctx->decoder_config;
514     dxva_ctx->decoder       = frames_hwctx->decoder_to_release;
515     dxva_ctx->surface       = frames_hwctx->surfaces;
516     dxva_ctx->surface_count = frames_hwctx->nb_surfaces;
517
518     if (IsEqualGUID(&ctx->decoder_guid, &DXVADDI_Intel_ModeH264_E))
519         dxva_ctx->workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
520
521     return 0;
522 fail:
523     av_buffer_unref(&ctx->hw_frames_ctx);
524     return AVERROR(EINVAL);
525 }
526
527 int dxva2_init(AVCodecContext *s)
528 {
529     InputStream *ist = s->opaque;
530     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
531     DXVA2Context *ctx;
532     int ret;
533
534     if (!ist->hwaccel_ctx) {
535         ret = dxva2_alloc(s);
536         if (ret < 0)
537             return ret;
538     }
539     ctx = ist->hwaccel_ctx;
540
541     if (s->codec_id == AV_CODEC_ID_H264 &&
542         (s->profile & ~FF_PROFILE_H264_CONSTRAINED) > FF_PROFILE_H264_HIGH) {
543         av_log(NULL, loglevel, "Unsupported H.264 profile for DXVA2 HWAccel: %d\n", s->profile);
544         return AVERROR(EINVAL);
545     }
546
547     if (s->codec_id == AV_CODEC_ID_HEVC &&
548         s->profile != FF_PROFILE_HEVC_MAIN && s->profile != FF_PROFILE_HEVC_MAIN_10) {
549         av_log(NULL, loglevel, "Unsupported HEVC profile for DXVA2 HWAccel: %d\n", s->profile);
550         return AVERROR(EINVAL);
551     }
552
553     av_buffer_unref(&ctx->hw_frames_ctx);
554
555     ret = dxva2_create_decoder(s);
556     if (ret < 0) {
557         av_log(NULL, loglevel, "Error creating the DXVA2 decoder\n");
558         return ret;
559     }
560
561     return 0;
562 }