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