ffmpeg_dxva2.c 21 KB

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