FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ffmpeg_dxva2.c
Go to the documentation of this file.
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 
19 #include <windows.h>
20 
21 #ifdef _WIN32_WINNT
22 #undef _WIN32_WINNT
23 #endif
24 #define _WIN32_WINNT 0x0600
25 #define DXVA2API_USE_BITFIELDS
26 #define COBJMACROS
27 
28 #include <stdint.h>
29 
30 #include <d3d9.h>
31 #include <dxva2api.h>
32 
33 #include "ffmpeg.h"
34 
35 #include "libavcodec/dxva2.h"
36 
37 #include "libavutil/avassert.h"
38 #include "libavutil/buffer.h"
39 #include "libavutil/frame.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/pixfmt.h"
42 
43 /* define all the GUIDs used directly here,
44  to avoid problems with inconsistent dxva2api.h versions in mingw-w64 and different MSVC version */
45 #include <initguid.h>
46 DEFINE_GUID(IID_IDirectXVideoDecoderService, 0xfc51a551,0xd5e7,0x11d9,0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
47 
48 DEFINE_GUID(DXVA2_ModeMPEG2_VLD, 0xee27417f, 0x5e28,0x4e65,0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9);
49 DEFINE_GUID(DXVA2_ModeMPEG2and1_VLD, 0x86695f12, 0x340e,0x4f04,0x9f,0xd3,0x92,0x53,0xdd,0x32,0x74,0x60);
50 DEFINE_GUID(DXVA2_ModeH264_E, 0x1b81be68, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
51 DEFINE_GUID(DXVA2_ModeH264_F, 0x1b81be69, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
52 DEFINE_GUID(DXVADDI_Intel_ModeH264_E, 0x604F8E68, 0x4951,0x4C54,0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6);
53 DEFINE_GUID(DXVA2_ModeVC1_D, 0x1b81beA3, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
54 DEFINE_GUID(DXVA2_ModeVC1_D2010, 0x1b81beA4, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
55 DEFINE_GUID(DXVA2_ModeHEVC_VLD_Main, 0x5b11d51b, 0x2f4c,0x4452,0xbc,0xc3,0x09,0xf2,0xa1,0x16,0x0c,0xc0);
56 DEFINE_GUID(DXVA2_ModeVP9_VLD_Profile0, 0x463707f8, 0xa1d0,0x4585,0x87,0x6d,0x83,0xaa,0x6d,0x60,0xb8,0x9e);
57 DEFINE_GUID(DXVA2_NoEncrypt, 0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
58 DEFINE_GUID(GUID_NULL, 0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
59 
60 typedef IDirect3D9* WINAPI pDirect3DCreate9(UINT);
61 typedef HRESULT WINAPI pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **);
62 
63 typedef struct dxva2_mode {
64  const GUID *guid;
66 } dxva2_mode;
67 
68 static const dxva2_mode dxva2_modes[] = {
69  /* MPEG-2 */
70  { &DXVA2_ModeMPEG2_VLD, AV_CODEC_ID_MPEG2VIDEO },
71  { &DXVA2_ModeMPEG2and1_VLD, AV_CODEC_ID_MPEG2VIDEO },
72 
73  /* H.264 */
74  { &DXVA2_ModeH264_F, AV_CODEC_ID_H264 },
75  { &DXVA2_ModeH264_E, AV_CODEC_ID_H264 },
76  /* Intel specific H.264 mode */
77  { &DXVADDI_Intel_ModeH264_E, AV_CODEC_ID_H264 },
78 
79  /* VC-1 / WMV3 */
80  { &DXVA2_ModeVC1_D2010, AV_CODEC_ID_VC1 },
81  { &DXVA2_ModeVC1_D2010, AV_CODEC_ID_WMV3 },
82  { &DXVA2_ModeVC1_D, AV_CODEC_ID_VC1 },
83  { &DXVA2_ModeVC1_D, AV_CODEC_ID_WMV3 },
84 
85  /* HEVC/H.265 */
86  { &DXVA2_ModeHEVC_VLD_Main, AV_CODEC_ID_HEVC },
87 
88  /* VP8/9 */
89  { &DXVA2_ModeVP9_VLD_Profile0, AV_CODEC_ID_VP9 },
90 
91  { NULL, 0 },
92 };
93 
94 typedef struct surface_info {
95  int used;
96  uint64_t age;
97 } surface_info;
98 
99 typedef struct DXVA2Context {
102 
104 
105  IDirect3D9 *d3d9;
106  IDirect3DDevice9 *d3d9device;
107  IDirect3DDeviceManager9 *d3d9devmgr;
108  IDirectXVideoDecoderService *decoder_service;
109  IDirectXVideoDecoder *decoder;
110 
112  DXVA2_ConfigPictureDecode decoder_config;
113 
114  LPDIRECT3DSURFACE9 *surfaces;
116  uint32_t num_surfaces;
117  uint64_t surface_age;
118 
120 } DXVA2Context;
121 
122 typedef struct DXVA2SurfaceWrapper {
124  LPDIRECT3DSURFACE9 surface;
125  IDirectXVideoDecoder *decoder;
127 
129 {
130  InputStream *ist = s->opaque;
131  DXVA2Context *ctx = ist->hwaccel_ctx;
132  int i;
133 
134  if (ctx->surfaces) {
135  for (i = 0; i < ctx->num_surfaces; i++) {
136  if (ctx->surfaces[i])
137  IDirect3DSurface9_Release(ctx->surfaces[i]);
138  }
139  }
140  av_freep(&ctx->surfaces);
141  av_freep(&ctx->surface_infos);
142  ctx->num_surfaces = 0;
143  ctx->surface_age = 0;
144 
145  if (ctx->decoder) {
146  IDirectXVideoDecoder_Release(ctx->decoder);
147  ctx->decoder = NULL;
148  }
149 }
150 
152 {
153  InputStream *ist = s->opaque;
154  DXVA2Context *ctx = ist->hwaccel_ctx;
155 
156  ist->hwaccel_uninit = NULL;
157  ist->hwaccel_get_buffer = NULL;
159 
160  if (ctx->decoder)
162 
163  if (ctx->decoder_service)
164  IDirectXVideoDecoderService_Release(ctx->decoder_service);
165 
166  if (ctx->d3d9devmgr && ctx->deviceHandle != INVALID_HANDLE_VALUE)
167  IDirect3DDeviceManager9_CloseDeviceHandle(ctx->d3d9devmgr, ctx->deviceHandle);
168 
169  if (ctx->d3d9devmgr)
170  IDirect3DDeviceManager9_Release(ctx->d3d9devmgr);
171 
172  if (ctx->d3d9device)
173  IDirect3DDevice9_Release(ctx->d3d9device);
174 
175  if (ctx->d3d9)
176  IDirect3D9_Release(ctx->d3d9);
177 
178  if (ctx->d3dlib)
179  FreeLibrary(ctx->d3dlib);
180 
181  if (ctx->dxva2lib)
182  FreeLibrary(ctx->dxva2lib);
183 
184  av_frame_free(&ctx->tmp_frame);
185 
186  av_freep(&ist->hwaccel_ctx);
188 }
189 
190 static void dxva2_release_buffer(void *opaque, uint8_t *data)
191 {
192  DXVA2SurfaceWrapper *w = opaque;
193  DXVA2Context *ctx = w->ctx;
194  int i;
195 
196  for (i = 0; i < ctx->num_surfaces; i++) {
197  if (ctx->surfaces[i] == w->surface) {
198  ctx->surface_infos[i].used = 0;
199  break;
200  }
201  }
202  IDirect3DSurface9_Release(w->surface);
203  IDirectXVideoDecoder_Release(w->decoder);
204  av_free(w);
205 }
206 
208 {
209  InputStream *ist = s->opaque;
210  DXVA2Context *ctx = ist->hwaccel_ctx;
211  int i, old_unused = -1;
212  LPDIRECT3DSURFACE9 surface;
214 
216 
217  for (i = 0; i < ctx->num_surfaces; i++) {
218  surface_info *info = &ctx->surface_infos[i];
219  if (!info->used && (old_unused == -1 || info->age < ctx->surface_infos[old_unused].age))
220  old_unused = i;
221  }
222  if (old_unused == -1) {
223  av_log(NULL, AV_LOG_ERROR, "No free DXVA2 surface!\n");
224  return AVERROR(ENOMEM);
225  }
226  i = old_unused;
227 
228  surface = ctx->surfaces[i];
229 
230  w = av_mallocz(sizeof(*w));
231  if (!w)
232  return AVERROR(ENOMEM);
233 
234  frame->buf[0] = av_buffer_create((uint8_t*)surface, 0,
237  if (!frame->buf[0]) {
238  av_free(w);
239  return AVERROR(ENOMEM);
240  }
241 
242  w->ctx = ctx;
243  w->surface = surface;
244  IDirect3DSurface9_AddRef(w->surface);
245  w->decoder = ctx->decoder;
246  IDirectXVideoDecoder_AddRef(w->decoder);
247 
248  ctx->surface_infos[i].used = 1;
249  ctx->surface_infos[i].age = ctx->surface_age++;
250 
251  frame->data[3] = (uint8_t *)surface;
252 
253  return 0;
254 }
255 
257 {
258  LPDIRECT3DSURFACE9 surface = (LPDIRECT3DSURFACE9)frame->data[3];
259  InputStream *ist = s->opaque;
260  DXVA2Context *ctx = ist->hwaccel_ctx;
261  D3DSURFACE_DESC surfaceDesc;
262  D3DLOCKED_RECT LockedRect;
263  HRESULT hr;
264  int ret;
265 
266  IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
267 
268  ctx->tmp_frame->width = frame->width;
269  ctx->tmp_frame->height = frame->height;
270  ctx->tmp_frame->format = AV_PIX_FMT_NV12;
271 
272  ret = av_frame_get_buffer(ctx->tmp_frame, 32);
273  if (ret < 0)
274  return ret;
275 
276  hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL, D3DLOCK_READONLY);
277  if (FAILED(hr)) {
278  av_log(NULL, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
279  return AVERROR_UNKNOWN;
280  }
281 
282  av_image_copy_plane(ctx->tmp_frame->data[0], ctx->tmp_frame->linesize[0],
283  (uint8_t*)LockedRect.pBits,
284  LockedRect.Pitch, frame->width, frame->height);
285 
286  av_image_copy_plane(ctx->tmp_frame->data[1], ctx->tmp_frame->linesize[1],
287  (uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
288  LockedRect.Pitch, frame->width, frame->height / 2);
289 
290  IDirect3DSurface9_UnlockRect(surface);
291 
292  ret = av_frame_copy_props(ctx->tmp_frame, frame);
293  if (ret < 0)
294  goto fail;
295 
296  av_frame_unref(frame);
297  av_frame_move_ref(frame, ctx->tmp_frame);
298 
299  return 0;
300 fail:
301  av_frame_unref(ctx->tmp_frame);
302  return ret;
303 }
304 
306 {
307  InputStream *ist = s->opaque;
308  int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
309  DXVA2Context *ctx;
310  pDirect3DCreate9 *createD3D = NULL;
311  pCreateDeviceManager9 *createDeviceManager = NULL;
312  HRESULT hr;
313  D3DPRESENT_PARAMETERS d3dpp = {0};
314  D3DDISPLAYMODE d3ddm;
315  unsigned resetToken = 0;
316  UINT adapter = D3DADAPTER_DEFAULT;
317 
318  ctx = av_mallocz(sizeof(*ctx));
319  if (!ctx)
320  return AVERROR(ENOMEM);
321 
323 
324  ist->hwaccel_ctx = ctx;
328 
329  ctx->d3dlib = LoadLibrary("d3d9.dll");
330  if (!ctx->d3dlib) {
331  av_log(NULL, loglevel, "Failed to load D3D9 library\n");
332  goto fail;
333  }
334  ctx->dxva2lib = LoadLibrary("dxva2.dll");
335  if (!ctx->dxva2lib) {
336  av_log(NULL, loglevel, "Failed to load DXVA2 library\n");
337  goto fail;
338  }
339 
340  createD3D = (pDirect3DCreate9 *)GetProcAddress(ctx->d3dlib, "Direct3DCreate9");
341  if (!createD3D) {
342  av_log(NULL, loglevel, "Failed to locate Direct3DCreate9\n");
343  goto fail;
344  }
345  createDeviceManager = (pCreateDeviceManager9 *)GetProcAddress(ctx->dxva2lib, "DXVA2CreateDirect3DDeviceManager9");
346  if (!createDeviceManager) {
347  av_log(NULL, loglevel, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
348  goto fail;
349  }
350 
351  ctx->d3d9 = createD3D(D3D_SDK_VERSION);
352  if (!ctx->d3d9) {
353  av_log(NULL, loglevel, "Failed to create IDirect3D object\n");
354  goto fail;
355  }
356 
357  if (ist->hwaccel_device) {
358  adapter = atoi(ist->hwaccel_device);
359  av_log(NULL, AV_LOG_INFO, "Using HWAccel device %d\n", adapter);
360  }
361 
362  IDirect3D9_GetAdapterDisplayMode(ctx->d3d9, adapter, &d3ddm);
363  d3dpp.Windowed = TRUE;
364  d3dpp.BackBufferWidth = 640;
365  d3dpp.BackBufferHeight = 480;
366  d3dpp.BackBufferCount = 0;
367  d3dpp.BackBufferFormat = d3ddm.Format;
368  d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
369  d3dpp.Flags = D3DPRESENTFLAG_VIDEO;
370 
371  hr = IDirect3D9_CreateDevice(ctx->d3d9, adapter, D3DDEVTYPE_HAL, GetDesktopWindow(),
372  D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
373  &d3dpp, &ctx->d3d9device);
374  if (FAILED(hr)) {
375  av_log(NULL, loglevel, "Failed to create Direct3D device\n");
376  goto fail;
377  }
378 
379  hr = createDeviceManager(&resetToken, &ctx->d3d9devmgr);
380  if (FAILED(hr)) {
381  av_log(NULL, loglevel, "Failed to create Direct3D device manager\n");
382  goto fail;
383  }
384 
385  hr = IDirect3DDeviceManager9_ResetDevice(ctx->d3d9devmgr, ctx->d3d9device, resetToken);
386  if (FAILED(hr)) {
387  av_log(NULL, loglevel, "Failed to bind Direct3D device to device manager\n");
388  goto fail;
389  }
390 
391  hr = IDirect3DDeviceManager9_OpenDeviceHandle(ctx->d3d9devmgr, &ctx->deviceHandle);
392  if (FAILED(hr)) {
393  av_log(NULL, loglevel, "Failed to open device handle\n");
394  goto fail;
395  }
396 
397  hr = IDirect3DDeviceManager9_GetVideoService(ctx->d3d9devmgr, ctx->deviceHandle, &IID_IDirectXVideoDecoderService, (void **)&ctx->decoder_service);
398  if (FAILED(hr)) {
399  av_log(NULL, loglevel, "Failed to create IDirectXVideoDecoderService\n");
400  goto fail;
401  }
402 
403  ctx->tmp_frame = av_frame_alloc();
404  if (!ctx->tmp_frame)
405  goto fail;
406 
407  s->hwaccel_context = av_mallocz(sizeof(struct dxva_context));
408  if (!s->hwaccel_context)
409  goto fail;
410 
411  return 0;
412 fail:
413  dxva2_uninit(s);
414  return AVERROR(EINVAL);
415 }
416 
417 static int dxva2_get_decoder_configuration(AVCodecContext *s, const GUID *device_guid,
418  const DXVA2_VideoDesc *desc,
419  DXVA2_ConfigPictureDecode *config)
420 {
421  InputStream *ist = s->opaque;
422  int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
423  DXVA2Context *ctx = ist->hwaccel_ctx;
424  unsigned cfg_count = 0, best_score = 0;
425  DXVA2_ConfigPictureDecode *cfg_list = NULL;
426  DXVA2_ConfigPictureDecode best_cfg = {{0}};
427  HRESULT hr;
428  int i;
429 
430  hr = IDirectXVideoDecoderService_GetDecoderConfigurations(ctx->decoder_service, device_guid, desc, NULL, &cfg_count, &cfg_list);
431  if (FAILED(hr)) {
432  av_log(NULL, loglevel, "Unable to retrieve decoder configurations\n");
433  return AVERROR(EINVAL);
434  }
435 
436  for (i = 0; i < cfg_count; i++) {
437  DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
438 
439  unsigned score;
440  if (cfg->ConfigBitstreamRaw == 1)
441  score = 1;
442  else if (s->codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
443  score = 2;
444  else
445  continue;
446  if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
447  score += 16;
448  if (score > best_score) {
449  best_score = score;
450  best_cfg = *cfg;
451  }
452  }
453  CoTaskMemFree(cfg_list);
454 
455  if (!best_score) {
456  av_log(NULL, loglevel, "No valid decoder configuration available\n");
457  return AVERROR(EINVAL);
458  }
459 
460  *config = best_cfg;
461  return 0;
462 }
463 
465 {
466  InputStream *ist = s->opaque;
467  int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
468  DXVA2Context *ctx = ist->hwaccel_ctx;
469  struct dxva_context *dxva_ctx = s->hwaccel_context;
470  GUID *guid_list = NULL;
471  unsigned guid_count = 0, i, j;
472  GUID device_guid = GUID_NULL;
473  D3DFORMAT target_format = 0;
474  DXVA2_VideoDesc desc = { 0 };
475  DXVA2_ConfigPictureDecode config;
476  HRESULT hr;
477  int surface_alignment;
478  int ret;
479 
480  hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(ctx->decoder_service, &guid_count, &guid_list);
481  if (FAILED(hr)) {
482  av_log(NULL, loglevel, "Failed to retrieve decoder device GUIDs\n");
483  goto fail;
484  }
485 
486  for (i = 0; dxva2_modes[i].guid; i++) {
487  D3DFORMAT *target_list = NULL;
488  unsigned target_count = 0;
489  const dxva2_mode *mode = &dxva2_modes[i];
490  if (mode->codec != s->codec_id)
491  continue;
492 
493  for (j = 0; j < guid_count; j++) {
494  if (IsEqualGUID(mode->guid, &guid_list[j]))
495  break;
496  }
497  if (j == guid_count)
498  continue;
499 
500  hr = IDirectXVideoDecoderService_GetDecoderRenderTargets(ctx->decoder_service, mode->guid, &target_count, &target_list);
501  if (FAILED(hr)) {
502  continue;
503  }
504  for (j = 0; j < target_count; j++) {
505  const D3DFORMAT format = target_list[j];
506  if (format == MKTAG('N','V','1','2')) {
507  target_format = format;
508  break;
509  }
510  }
511  CoTaskMemFree(target_list);
512  if (target_format) {
513  device_guid = *mode->guid;
514  break;
515  }
516  }
517  CoTaskMemFree(guid_list);
518 
519  if (IsEqualGUID(&device_guid, &GUID_NULL)) {
520  av_log(NULL, loglevel, "No decoder device for codec found\n");
521  goto fail;
522  }
523 
524  desc.SampleWidth = s->coded_width;
525  desc.SampleHeight = s->coded_height;
526  desc.Format = target_format;
527 
528  ret = dxva2_get_decoder_configuration(s, &device_guid, &desc, &config);
529  if (ret < 0) {
530  goto fail;
531  }
532 
533  /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
534  but it causes issues for H.264 on certain AMD GPUs..... */
536  surface_alignment = 32;
537  /* the HEVC DXVA2 spec asks for 128 pixel aligned surfaces to ensure
538  all coding features have enough room to work with */
539  else if (s->codec_id == AV_CODEC_ID_HEVC)
540  surface_alignment = 128;
541  else
542  surface_alignment = 16;
543 
544  /* 4 base work surfaces */
545  ctx->num_surfaces = 4;
546 
547  /* add surfaces based on number of possible refs */
549  ctx->num_surfaces += 16;
550  else if (s->codec_id == AV_CODEC_ID_VP9)
551  ctx->num_surfaces += 8;
552  else
553  ctx->num_surfaces += 2;
554 
555  /* add extra surfaces for frame threading */
557  ctx->num_surfaces += s->thread_count;
558 
559  ctx->surfaces = av_mallocz(ctx->num_surfaces * sizeof(*ctx->surfaces));
560  ctx->surface_infos = av_mallocz(ctx->num_surfaces * sizeof(*ctx->surface_infos));
561 
562  if (!ctx->surfaces || !ctx->surface_infos) {
563  av_log(NULL, loglevel, "Unable to allocate surface arrays\n");
564  goto fail;
565  }
566 
567  hr = IDirectXVideoDecoderService_CreateSurface(ctx->decoder_service,
568  FFALIGN(s->coded_width, surface_alignment),
569  FFALIGN(s->coded_height, surface_alignment),
570  ctx->num_surfaces - 1,
571  target_format, D3DPOOL_DEFAULT, 0,
572  DXVA2_VideoDecoderRenderTarget,
573  ctx->surfaces, NULL);
574  if (FAILED(hr)) {
575  av_log(NULL, loglevel, "Failed to create %d video surfaces\n", ctx->num_surfaces);
576  goto fail;
577  }
578 
579  hr = IDirectXVideoDecoderService_CreateVideoDecoder(ctx->decoder_service, &device_guid,
580  &desc, &config, ctx->surfaces,
581  ctx->num_surfaces, &ctx->decoder);
582  if (FAILED(hr)) {
583  av_log(NULL, loglevel, "Failed to create DXVA2 video decoder\n");
584  goto fail;
585  }
586 
587  ctx->decoder_guid = device_guid;
588  ctx->decoder_config = config;
589 
590  dxva_ctx->cfg = &ctx->decoder_config;
591  dxva_ctx->decoder = ctx->decoder;
592  dxva_ctx->surface = ctx->surfaces;
593  dxva_ctx->surface_count = ctx->num_surfaces;
594 
595  if (IsEqualGUID(&ctx->decoder_guid, &DXVADDI_Intel_ModeH264_E))
597 
598  return 0;
599 fail:
601  return AVERROR(EINVAL);
602 }
603 
605 {
606  InputStream *ist = s->opaque;
607  int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
608  DXVA2Context *ctx;
609  int ret;
610 
611  if (!ist->hwaccel_ctx) {
612  ret = dxva2_alloc(s);
613  if (ret < 0)
614  return ret;
615  }
616  ctx = ist->hwaccel_ctx;
617 
618  if (s->codec_id == AV_CODEC_ID_H264 &&
620  av_log(NULL, loglevel, "Unsupported H.264 profile for DXVA2 HWAccel: %d\n", s->profile);
621  return AVERROR(EINVAL);
622  }
623 
624  if (ctx->decoder)
626 
627  ret = dxva2_create_decoder(s);
628  if (ret < 0) {
629  av_log(NULL, loglevel, "Error creating the DXVA2 decoder\n");
630  return ret;
631  }
632 
633  return 0;
634 }
#define NULL
Definition: coverity.c:32
const GUID * guid
Definition: ffmpeg_dxva2.c:64
const char * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1726
AVFormatContext * ctx
Definition: movenc-test.c:48
misc image utilities
static int dxva2_create_decoder(AVCodecContext *s)
Definition: ffmpeg_dxva2.c:464
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:357
static FFServerConfig config
Definition: ffserver.c:202
static void dxva2_uninit(AVCodecContext *s)
Definition: ffmpeg_dxva2.c:151
HRESULT WINAPI pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **)
Definition: ffmpeg_dxva2.c:61
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:496
LPDIRECT3DSURFACE9 * surface
The array of Direct3D surfaces used to create the decoder.
Definition: dxva2.h:76
DXVA2Context * ctx
Definition: ffmpeg_dxva2.c:123
int(* hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags)
Definition: ffmpeg.h:333
int profile
profile
Definition: avcodec.h:3028
const DXVA2_ConfigPictureDecode * cfg
DXVA2 configuration used to create the decoder.
Definition: dxva2.h:66
IDirect3DDeviceManager9 * d3d9devmgr
Definition: ffmpeg_dxva2.c:107
unsigned surface_count
The number of surface in the surface array.
Definition: dxva2.h:71
int dxva2_init(AVCodecContext *s)
Definition: ffmpeg_dxva2.c:604
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
HMODULE d3dlib
Definition: ffmpeg_dxva2.c:100
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:141
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2855
mode
Definition: f_perms.c:27
uint64_t age
Definition: ffmpeg_dxva2.c:96
static int dxva2_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
Definition: ffmpeg_dxva2.c:207
static AVFrame * frame
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
Definition: buffer.h:113
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
static const dxva2_mode dxva2_modes[]
Definition: ffmpeg_dxva2.c:68
uint64_t workaround
A bit field configuring the workarounds needed for using the decoder.
Definition: dxva2.h:81
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:101
int width
width and height of the video frame
Definition: frame.h:230
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void(* hwaccel_uninit)(AVCodecContext *s)
Definition: ffmpeg.h:332
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:154
void * HMODULE
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2973
#define FF_PROFILE_H264_HIGH
Definition: avcodec.h:3064
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:91
simple assert() macros that are a bit more flexible than ISO C assert().
static void dxva2_release_buffer(void *opaque, uint8_t *data)
Definition: ffmpeg_dxva2.c:190
static void dxva2_destroy_decoder(AVCodecContext *s)
Definition: ffmpeg_dxva2.c:128
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
IDirect3D9 * d3d9
Definition: ffmpeg_dxva2.c:105
#define fail()
Definition: checkasm.h:80
reference-counted frame API
DXVA2_ConfigPictureDecode decoder_config
Definition: ffmpeg_dxva2.c:112
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:2965
IDirect3D9 *WINAPI pDirect3DCreate9(UINT)
Definition: ffmpeg_dxva2.c:60
PVOID HANDLE
#define FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO
Work around for Direct3D11 and old Intel GPUs with ClearVideo interface.
Definition: d3d11va.h:49
#define TRUE
Definition: windows2linux.h:33
int(* hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame)
Definition: ffmpeg.h:334
IDirectXVideoDecoder * decoder
Definition: ffmpeg_dxva2.c:125
AVFrame * tmp_frame
Definition: ffmpeg_dxva2.c:119
#define LoadLibrary(x)
Definition: avisynth.c:43
enum AVCodecID codec
Definition: ffmpeg_dxva2.c:65
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:106
static int dxva2_get_decoder_configuration(AVCodecContext *s, const GUID *device_guid, const DXVA2_VideoDesc *desc, DXVA2_ConfigPictureDecode *config)
Definition: ffmpeg_dxva2.c:417
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2954
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:242
LPDIRECT3DSURFACE9 * surfaces
Definition: ffmpeg_dxva2.c:114
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
Public libavcodec DXVA2 header.
HANDLE deviceHandle
Definition: ffmpeg_dxva2.c:103
enum AVCodecID codec_id
Definition: avcodec.h:1549
main external API structure.
Definition: avcodec.h:1532
static const char * format
Definition: movenc-test.c:47
uint64_t surface_age
Definition: ffmpeg_dxva2.c:117
int coded_height
Definition: avcodec.h:1726
refcounted data buffer API
IDirectXVideoDecoder * decoder
Definition: ffmpeg_dxva2.c:109
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer. ...
Definition: pixfmt.h:149
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:271
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:474
DWORD HRESULT
static int flags
Definition: cpu.c:47
#define FAILED(hr)
Definition: windows2linux.h:48
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
surface_info * surface_infos
Definition: ffmpeg_dxva2.c:115
static int dxva2_alloc(AVCodecContext *s)
Definition: ffmpeg_dxva2.c:305
#define GetProcAddress
Definition: avisynth.c:44
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
DEFINE_GUID(IID_IDirectXVideoDecoderService, 0xfc51a551, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02)
HMODULE dxva2lib
Definition: ffmpeg_dxva2.c:101
static int dxva2_retrieve_data(AVCodecContext *s, AVFrame *frame)
Definition: ffmpeg_dxva2.c:256
IDirectXVideoDecoderService * decoder_service
Definition: ffmpeg_dxva2.c:108
pixel format definitions
#define av_free(p)
void * hwaccel_ctx
Definition: ffmpeg.h:331
LPDIRECT3DSURFACE9 surface
Definition: ffmpeg_dxva2.c:124
char * hwaccel_device
Definition: ffmpeg.h:327
int height
Definition: frame.h:230
uint32_t num_surfaces
Definition: ffmpeg_dxva2.c:116
#define av_freep(p)
unsigned int UINT
IDirectXVideoDecoder * decoder
DXVA2 decoder object.
Definition: dxva2.h:61
#define INVALID_HANDLE_VALUE
Definition: windows2linux.h:47
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:287
#define FF_PROFILE_H264_CONSTRAINED
Definition: avcodec.h:3057
enum HWAccelID hwaccel_id
Definition: ffmpeg.h:326
#define FreeLibrary
Definition: avisynth.c:45
#define MKTAG(a, b, c, d)
Definition: common.h:342
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:1589
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:565
This structure is used to provides the necessary configurations and data to the DXVA2 FFmpeg HWAccel ...
Definition: dxva2.h:57
IDirect3DDevice9 * d3d9device
Definition: ffmpeg_dxva2.c:106