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.

620 lines
19KB

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