You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

619 lines
19KB

  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. #include <windows.h>
  19. #ifdef _WIN32_WINNT
  20. #undef _WIN32_WINNT
  21. #endif
  22. #define _WIN32_WINNT 0x0600
  23. #define DXVA2API_USE_BITFIELDS
  24. #define COBJMACROS
  25. #include <stdint.h>
  26. /* initguid.h needs to be above d3d/dxva to ensure
  27. the GUIDs are initialized properly */
  28. #include <initguid.h>
  29. #include <d3d9.h>
  30. #include <dxva2api.h>
  31. #include "avconv.h"
  32. #include "libavcodec/dxva2.h"
  33. #include "libavutil/avassert.h"
  34. #include "libavutil/buffer.h"
  35. #include "libavutil/frame.h"
  36. #include "libavutil/imgutils.h"
  37. #include "libavutil/pixfmt.h"
  38. typedef IDirect3D9* WINAPI pDirect3DCreate9(UINT);
  39. typedef HRESULT WINAPI pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **);
  40. /* GUIDs not defined in the common dxva2api.h in mingw-w64 */
  41. #ifndef _MSC_VER
  42. DEFINE_GUID(DXVA2_ModeMPEG2and1_VLD, 0x86695f12, 0x340e,0x4f04,0x9f,0xd3,0x92,0x53,0xdd,0x32,0x74,0x60);
  43. DEFINE_GUID(DXVA2_ModeVC1_D2010, 0x1b81beA4, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
  44. DEFINE_GUID(DXVA2_NoEncrypt, 0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
  45. #endif
  46. DEFINE_GUID(DXVADDI_Intel_ModeH264_E, 0x604F8E68, 0x4951,0x4C54,0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6);
  47. DEFINE_GUID(GUID_NULL, 0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
  48. typedef struct dxva2_mode {
  49. const GUID *guid;
  50. enum AVCodecID codec;
  51. } dxva2_mode;
  52. static const dxva2_mode dxva2_modes[] = {
  53. /* MPEG-2 */
  54. { &DXVA2_ModeMPEG2_VLD, AV_CODEC_ID_MPEG2VIDEO },
  55. { &DXVA2_ModeMPEG2and1_VLD, AV_CODEC_ID_MPEG2VIDEO },
  56. /* H.264 */
  57. { &DXVA2_ModeH264_F, AV_CODEC_ID_H264 },
  58. { &DXVA2_ModeH264_E, AV_CODEC_ID_H264 },
  59. /* Intel specific H.264 mode */
  60. { &DXVADDI_Intel_ModeH264_E, AV_CODEC_ID_H264 },
  61. /* VC-1 / WMV3 */
  62. { &DXVA2_ModeVC1_D2010, AV_CODEC_ID_VC1 },
  63. { &DXVA2_ModeVC1_D2010, AV_CODEC_ID_WMV3 },
  64. { &DXVA2_ModeVC1_D, AV_CODEC_ID_VC1 },
  65. { &DXVA2_ModeVC1_D, AV_CODEC_ID_WMV3 },
  66. { NULL, 0 },
  67. };
  68. typedef struct surface_info {
  69. int used;
  70. uint64_t age;
  71. } surface_info;
  72. typedef struct DXVA2Context {
  73. HMODULE d3dlib;
  74. HMODULE dxva2lib;
  75. HANDLE deviceHandle;
  76. IDirect3D9 *d3d9;
  77. IDirect3DDevice9 *d3d9device;
  78. IDirect3DDeviceManager9 *d3d9devmgr;
  79. IDirectXVideoDecoderService *decoder_service;
  80. IDirectXVideoDecoder *decoder;
  81. GUID decoder_guid;
  82. DXVA2_ConfigPictureDecode decoder_config;
  83. LPDIRECT3DSURFACE9 *surfaces;
  84. surface_info *surface_infos;
  85. uint32_t num_surfaces;
  86. uint64_t surface_age;
  87. AVFrame *tmp_frame;
  88. } DXVA2Context;
  89. typedef struct DXVA2SurfaceWrapper {
  90. DXVA2Context *ctx;
  91. LPDIRECT3DSURFACE9 surface;
  92. IDirectXVideoDecoder *decoder;
  93. } DXVA2SurfaceWrapper;
  94. static void dxva2_destroy_decoder(AVCodecContext *s)
  95. {
  96. InputStream *ist = s->opaque;
  97. DXVA2Context *ctx = ist->hwaccel_ctx;
  98. if (ctx->surfaces) {
  99. for (int i = 0; i < ctx->num_surfaces; i++) {
  100. if (ctx->surfaces[i])
  101. IDirect3DSurface9_Release(ctx->surfaces[i]);
  102. }
  103. }
  104. av_freep(&ctx->surfaces);
  105. av_freep(&ctx->surface_infos);
  106. ctx->num_surfaces = 0;
  107. ctx->surface_age = 0;
  108. if (ctx->decoder) {
  109. IDirectXVideoDecoder_Release(ctx->decoder);
  110. ctx->decoder = NULL;
  111. }
  112. }
  113. static void dxva2_uninit(AVCodecContext *s)
  114. {
  115. InputStream *ist = s->opaque;
  116. DXVA2Context *ctx = ist->hwaccel_ctx;
  117. ist->hwaccel_uninit = NULL;
  118. ist->hwaccel_get_buffer = NULL;
  119. ist->hwaccel_retrieve_data = NULL;
  120. if (ctx->decoder)
  121. dxva2_destroy_decoder(s);
  122. if (ctx->decoder_service)
  123. IDirectXVideoDecoderService_Release(ctx->decoder_service);
  124. if (ctx->d3d9devmgr && ctx->deviceHandle != INVALID_HANDLE_VALUE)
  125. IDirect3DDeviceManager9_CloseDeviceHandle(ctx->d3d9devmgr, ctx->deviceHandle);
  126. if (ctx->d3d9devmgr)
  127. IDirect3DDeviceManager9_Release(ctx->d3d9devmgr);
  128. if (ctx->d3d9device)
  129. IDirect3DDevice9_Release(ctx->d3d9device);
  130. if (ctx->d3d9)
  131. IDirect3D9_Release(ctx->d3d9);
  132. if (ctx->d3dlib)
  133. FreeLibrary(ctx->d3dlib);
  134. if (ctx->dxva2lib)
  135. FreeLibrary(ctx->dxva2lib);
  136. av_frame_free(&ctx->tmp_frame);
  137. av_freep(&ist->hwaccel_ctx);
  138. av_freep(&s->hwaccel_context);
  139. }
  140. static void dxva2_release_buffer(void *opaque, uint8_t *data)
  141. {
  142. DXVA2SurfaceWrapper *w = opaque;
  143. DXVA2Context *ctx = w->ctx;
  144. int i;
  145. for (i = 0; i < ctx->num_surfaces; i++) {
  146. if (ctx->surfaces[i] == w->surface) {
  147. ctx->surface_infos[i].used = 0;
  148. break;
  149. }
  150. }
  151. IDirect3DSurface9_Release(w->surface);
  152. IDirectXVideoDecoder_Release(w->decoder);
  153. av_free(w);
  154. }
  155. static int dxva2_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
  156. {
  157. InputStream *ist = s->opaque;
  158. DXVA2Context *ctx = ist->hwaccel_ctx;
  159. int i, old_unused = -1;
  160. LPDIRECT3DSURFACE9 surface;
  161. DXVA2SurfaceWrapper *w = NULL;
  162. av_assert0(frame->format == AV_PIX_FMT_DXVA2_VLD);
  163. for (i = 0; i < ctx->num_surfaces; i++) {
  164. surface_info *info = &ctx->surface_infos[i];
  165. if (!info->used && (old_unused == -1 || info->age < ctx->surface_infos[old_unused].age))
  166. old_unused = i;
  167. }
  168. if (old_unused == -1) {
  169. av_log(NULL, AV_LOG_ERROR, "No free DXVA2 surface!\n");
  170. return AVERROR(ENOMEM);
  171. }
  172. i = old_unused;
  173. surface = ctx->surfaces[i];
  174. w = av_mallocz(sizeof(*w));
  175. if (!w)
  176. return AVERROR(ENOMEM);
  177. frame->buf[0] = av_buffer_create((uint8_t*)surface, 0,
  178. dxva2_release_buffer, w,
  179. AV_BUFFER_FLAG_READONLY);
  180. if (!frame->buf[0]) {
  181. av_free(w);
  182. return AVERROR(ENOMEM);
  183. }
  184. w->ctx = ctx;
  185. w->surface = surface;
  186. IDirect3DSurface9_AddRef(w->surface);
  187. w->decoder = ctx->decoder;
  188. IDirectXVideoDecoder_AddRef(w->decoder);
  189. ctx->surface_infos[i].used = 1;
  190. ctx->surface_infos[i].age = ctx->surface_age++;
  191. frame->data[3] = (uint8_t *)surface;
  192. return 0;
  193. }
  194. static int dxva2_retrieve_data(AVCodecContext *s, AVFrame *frame)
  195. {
  196. LPDIRECT3DSURFACE9 surface = (LPDIRECT3DSURFACE9)frame->data[3];
  197. InputStream *ist = s->opaque;
  198. DXVA2Context *ctx = ist->hwaccel_ctx;
  199. D3DSURFACE_DESC surfaceDesc;
  200. D3DLOCKED_RECT LockedRect;
  201. HRESULT hr;
  202. int ret;
  203. IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
  204. ctx->tmp_frame->width = frame->width;
  205. ctx->tmp_frame->height = frame->height;
  206. ctx->tmp_frame->format = AV_PIX_FMT_NV12;
  207. ret = av_frame_get_buffer(ctx->tmp_frame, 32);
  208. if (ret < 0)
  209. return ret;
  210. hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL, D3DLOCK_READONLY);
  211. if (FAILED(hr)) {
  212. av_log(NULL, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
  213. return AVERROR_UNKNOWN;
  214. }
  215. av_image_copy_plane(ctx->tmp_frame->data[0], ctx->tmp_frame->linesize[0],
  216. (uint8_t*)LockedRect.pBits,
  217. LockedRect.Pitch, frame->width, frame->height);
  218. av_image_copy_plane(ctx->tmp_frame->data[1], ctx->tmp_frame->linesize[1],
  219. (uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
  220. LockedRect.Pitch, frame->width, frame->height / 2);
  221. IDirect3DSurface9_UnlockRect(surface);
  222. ret = av_frame_copy_props(ctx->tmp_frame, frame);
  223. if (ret < 0)
  224. goto fail;
  225. av_frame_unref(frame);
  226. av_frame_move_ref(frame, ctx->tmp_frame);
  227. return 0;
  228. fail:
  229. av_frame_unref(ctx->tmp_frame);
  230. return ret;
  231. }
  232. static int dxva2_alloc(AVCodecContext *s)
  233. {
  234. InputStream *ist = s->opaque;
  235. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  236. DXVA2Context *ctx;
  237. pDirect3DCreate9 *createD3D = NULL;
  238. pCreateDeviceManager9 *createDeviceManager = NULL;
  239. HRESULT hr;
  240. D3DPRESENT_PARAMETERS d3dpp = {0};
  241. D3DDISPLAYMODE d3ddm;
  242. unsigned resetToken = 0;
  243. UINT adapter = D3DADAPTER_DEFAULT;
  244. ctx = av_mallocz(sizeof(*ctx));
  245. if (!ctx)
  246. return AVERROR(ENOMEM);
  247. ctx->deviceHandle = INVALID_HANDLE_VALUE;
  248. ist->hwaccel_ctx = ctx;
  249. ist->hwaccel_uninit = dxva2_uninit;
  250. ist->hwaccel_get_buffer = dxva2_get_buffer;
  251. ist->hwaccel_retrieve_data = dxva2_retrieve_data;
  252. ctx->d3dlib = LoadLibrary("d3d9.dll");
  253. if (!ctx->d3dlib) {
  254. av_log(NULL, loglevel, "Failed to load D3D9 library\n");
  255. goto fail;
  256. }
  257. ctx->dxva2lib = LoadLibrary("dxva2.dll");
  258. if (!ctx->dxva2lib) {
  259. av_log(NULL, loglevel, "Failed to load DXVA2 library\n");
  260. goto fail;
  261. }
  262. createD3D = (pDirect3DCreate9 *)GetProcAddress(ctx->d3dlib, "Direct3DCreate9");
  263. if (!createD3D) {
  264. av_log(NULL, loglevel, "Failed to locate Direct3DCreate9\n");
  265. goto fail;
  266. }
  267. createDeviceManager = (pCreateDeviceManager9 *)GetProcAddress(ctx->dxva2lib, "DXVA2CreateDirect3DDeviceManager9");
  268. if (!createDeviceManager) {
  269. av_log(NULL, loglevel, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
  270. goto fail;
  271. }
  272. ctx->d3d9 = createD3D(D3D_SDK_VERSION);
  273. if (!ctx->d3d9) {
  274. av_log(NULL, loglevel, "Failed to create IDirect3D object\n");
  275. goto fail;
  276. }
  277. if (ist->hwaccel_device) {
  278. adapter = atoi(ist->hwaccel_device);
  279. av_log(NULL, AV_LOG_INFO, "Using HWAccel device %d\n", adapter);
  280. }
  281. IDirect3D9_GetAdapterDisplayMode(ctx->d3d9, adapter, &d3ddm);
  282. d3dpp.Windowed = TRUE;
  283. d3dpp.BackBufferWidth = 640;
  284. d3dpp.BackBufferHeight = 480;
  285. d3dpp.BackBufferCount = 0;
  286. d3dpp.BackBufferFormat = d3ddm.Format;
  287. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  288. d3dpp.Flags = D3DPRESENTFLAG_VIDEO;
  289. hr = IDirect3D9_CreateDevice(ctx->d3d9, adapter, D3DDEVTYPE_HAL, GetShellWindow(),
  290. D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
  291. &d3dpp, &ctx->d3d9device);
  292. if (FAILED(hr)) {
  293. av_log(NULL, loglevel, "Failed to create Direct3D device\n");
  294. goto fail;
  295. }
  296. hr = createDeviceManager(&resetToken, &ctx->d3d9devmgr);
  297. if (FAILED(hr)) {
  298. av_log(NULL, loglevel, "Failed to create Direct3D device manager\n");
  299. goto fail;
  300. }
  301. hr = IDirect3DDeviceManager9_ResetDevice(ctx->d3d9devmgr, ctx->d3d9device, resetToken);
  302. if (FAILED(hr)) {
  303. av_log(NULL, loglevel, "Failed to bind Direct3D device to device manager\n");
  304. goto fail;
  305. }
  306. hr = IDirect3DDeviceManager9_OpenDeviceHandle(ctx->d3d9devmgr, &ctx->deviceHandle);
  307. if (FAILED(hr)) {
  308. av_log(NULL, loglevel, "Failed to open device handle\n");
  309. goto fail;
  310. }
  311. hr = IDirect3DDeviceManager9_GetVideoService(ctx->d3d9devmgr, ctx->deviceHandle, &IID_IDirectXVideoDecoderService, (void **)&ctx->decoder_service);
  312. if (FAILED(hr)) {
  313. av_log(NULL, loglevel, "Failed to create IDirectXVideoDecoderService\n");
  314. goto fail;
  315. }
  316. ctx->tmp_frame = av_frame_alloc();
  317. if (!ctx->tmp_frame)
  318. goto fail;
  319. s->hwaccel_context = av_mallocz(sizeof(struct dxva_context));
  320. if (!s->hwaccel_context)
  321. goto fail;
  322. return 0;
  323. fail:
  324. dxva2_uninit(s);
  325. return AVERROR(EINVAL);
  326. }
  327. static int dxva2_get_decoder_configuration(AVCodecContext *s, const GUID *device_guid,
  328. const DXVA2_VideoDesc *desc,
  329. DXVA2_ConfigPictureDecode *config)
  330. {
  331. InputStream *ist = s->opaque;
  332. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  333. DXVA2Context *ctx = ist->hwaccel_ctx;
  334. unsigned cfg_count = 0, best_score = 0;
  335. DXVA2_ConfigPictureDecode *cfg_list = NULL;
  336. DXVA2_ConfigPictureDecode best_cfg = {{0}};
  337. HRESULT hr;
  338. int i;
  339. hr = IDirectXVideoDecoderService_GetDecoderConfigurations(ctx->decoder_service, device_guid, desc, NULL, &cfg_count, &cfg_list);
  340. if (FAILED(hr)) {
  341. av_log(NULL, loglevel, "Unable to retrieve decoder configurations\n");
  342. return AVERROR(EINVAL);
  343. }
  344. for (i = 0; i < cfg_count; i++) {
  345. DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
  346. unsigned score;
  347. if (cfg->ConfigBitstreamRaw == 1)
  348. score = 1;
  349. else if (s->codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
  350. score = 2;
  351. else
  352. continue;
  353. if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
  354. score += 16;
  355. if (score > best_score) {
  356. best_score = score;
  357. best_cfg = *cfg;
  358. }
  359. }
  360. CoTaskMemFree(cfg_list);
  361. if (!best_score) {
  362. av_log(NULL, loglevel, "No valid decoder configuration available\n");
  363. return AVERROR(EINVAL);
  364. }
  365. *config = best_cfg;
  366. return 0;
  367. }
  368. static int dxva2_create_decoder(AVCodecContext *s)
  369. {
  370. InputStream *ist = s->opaque;
  371. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  372. DXVA2Context *ctx = ist->hwaccel_ctx;
  373. struct dxva_context *dxva_ctx = s->hwaccel_context;
  374. GUID *guid_list = NULL;
  375. unsigned guid_count = 0, i, j;
  376. GUID device_guid = GUID_NULL;
  377. D3DFORMAT target_format = 0;
  378. DXVA2_VideoDesc desc = { 0 };
  379. DXVA2_ConfigPictureDecode config;
  380. HRESULT hr;
  381. int surface_alignment;
  382. int ret;
  383. hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(ctx->decoder_service, &guid_count, &guid_list);
  384. if (FAILED(hr)) {
  385. av_log(NULL, loglevel, "Failed to retrieve decoder device GUIDs\n");
  386. goto fail;
  387. }
  388. for (i = 0; dxva2_modes[i].guid; i++) {
  389. D3DFORMAT *target_list = NULL;
  390. unsigned target_count = 0;
  391. const dxva2_mode *mode = &dxva2_modes[i];
  392. if (mode->codec != s->codec_id)
  393. continue;
  394. for (j = 0; j < guid_count; j++) {
  395. if (IsEqualGUID(mode->guid, &guid_list[j]))
  396. break;
  397. }
  398. if (j == guid_count)
  399. continue;
  400. hr = IDirectXVideoDecoderService_GetDecoderRenderTargets(ctx->decoder_service, mode->guid, &target_count, &target_list);
  401. if (FAILED(hr)) {
  402. continue;
  403. }
  404. for (j = 0; j < target_count; j++) {
  405. const D3DFORMAT format = target_list[j];
  406. if (format == MKTAG('N','V','1','2')) {
  407. target_format = format;
  408. break;
  409. }
  410. }
  411. CoTaskMemFree(target_list);
  412. if (target_format) {
  413. device_guid = *mode->guid;
  414. break;
  415. }
  416. }
  417. CoTaskMemFree(guid_list);
  418. if (IsEqualGUID(&device_guid, &GUID_NULL)) {
  419. av_log(NULL, loglevel, "No decoder device for codec found\n");
  420. goto fail;
  421. }
  422. desc.SampleWidth = s->coded_width;
  423. desc.SampleHeight = s->coded_height;
  424. desc.Format = target_format;
  425. ret = dxva2_get_decoder_configuration(s, &device_guid, &desc, &config);
  426. if (ret < 0) {
  427. goto fail;
  428. }
  429. /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
  430. but it causes issues for H.264 on certain AMD GPUs..... */
  431. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO)
  432. surface_alignment = 32;
  433. else
  434. surface_alignment = 16;
  435. /* 4 base work surfaces */
  436. ctx->num_surfaces = 4;
  437. /* add surfaces based on number of possible refs */
  438. if (s->codec_id == AV_CODEC_ID_H264)
  439. ctx->num_surfaces += 16;
  440. else
  441. ctx->num_surfaces += 2;
  442. /* add extra surfaces for frame threading */
  443. if (s->active_thread_type & FF_THREAD_FRAME)
  444. ctx->num_surfaces += s->thread_count;
  445. ctx->surfaces = av_mallocz(ctx->num_surfaces * sizeof(*ctx->surfaces));
  446. ctx->surface_infos = av_mallocz(ctx->num_surfaces * sizeof(*ctx->surface_infos));
  447. if (!ctx->surfaces || !ctx->surface_infos) {
  448. av_log(NULL, loglevel, "Unable to allocate surface arrays\n");
  449. goto fail;
  450. }
  451. hr = IDirectXVideoDecoderService_CreateSurface(ctx->decoder_service,
  452. FFALIGN(s->coded_width, surface_alignment),
  453. FFALIGN(s->coded_height, surface_alignment),
  454. ctx->num_surfaces - 1,
  455. target_format, D3DPOOL_DEFAULT, 0,
  456. DXVA2_VideoDecoderRenderTarget,
  457. ctx->surfaces, NULL);
  458. if (FAILED(hr)) {
  459. av_log(NULL, loglevel, "Failed to create %d video surfaces\n", ctx->num_surfaces);
  460. goto fail;
  461. }
  462. hr = IDirectXVideoDecoderService_CreateVideoDecoder(ctx->decoder_service, &device_guid,
  463. &desc, &config, ctx->surfaces,
  464. ctx->num_surfaces, &ctx->decoder);
  465. if (FAILED(hr)) {
  466. av_log(NULL, loglevel, "Failed to create DXVA2 video decoder\n");
  467. goto fail;
  468. }
  469. ctx->decoder_guid = device_guid;
  470. ctx->decoder_config = config;
  471. dxva_ctx->cfg = &ctx->decoder_config;
  472. dxva_ctx->decoder = ctx->decoder;
  473. dxva_ctx->surface = ctx->surfaces;
  474. dxva_ctx->surface_count = ctx->num_surfaces;
  475. if (IsEqualGUID(&ctx->decoder_guid, &DXVADDI_Intel_ModeH264_E))
  476. dxva_ctx->workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
  477. return 0;
  478. fail:
  479. dxva2_destroy_decoder(s);
  480. return AVERROR(EINVAL);
  481. }
  482. int dxva2_init(AVCodecContext *s)
  483. {
  484. InputStream *ist = s->opaque;
  485. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  486. DXVA2Context *ctx;
  487. int ret;
  488. if (!ist->hwaccel_ctx) {
  489. ret = dxva2_alloc(s);
  490. if (ret < 0)
  491. return ret;
  492. }
  493. ctx = ist->hwaccel_ctx;
  494. if (s->codec_id == AV_CODEC_ID_H264 &&
  495. (s->profile & ~FF_PROFILE_H264_CONSTRAINED) > FF_PROFILE_H264_HIGH) {
  496. av_log(NULL, loglevel, "Unsupported H.264 profile for DXVA2 HWAccel: %d\n", s->profile);
  497. return AVERROR(EINVAL);
  498. }
  499. if (ctx->decoder)
  500. dxva2_destroy_decoder(s);
  501. ret = dxva2_create_decoder(s);
  502. if (ret < 0) {
  503. av_log(NULL, loglevel, "Error creating the DXVA2 decoder\n");
  504. return ret;
  505. }
  506. return 0;
  507. }