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
20KB

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