ffmpeg_dxva2.c 20 KB

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