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.

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