ffmpeg_dxva2.c 20 KB

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