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