FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dxva2.c
Go to the documentation of this file.
1 /*
2  * DXVA2 HW acceleration.
3  *
4  * copyright (c) 2010 Laurent Aimar
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <assert.h>
24 #include <string.h>
25 #include <initguid.h>
26 
27 #include "libavutil/common.h"
28 #include "libavutil/log.h"
29 #include "libavutil/time.h"
30 
31 #include "avcodec.h"
32 #include "decode.h"
33 #include "dxva2_internal.h"
34 
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 DEFINE_GUID(ff_DXVA2_ModeMPEG2_VLD, 0xee27417f, 0x5e28,0x4e65,0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9);
38 DEFINE_GUID(ff_DXVA2_ModeMPEG2and1_VLD, 0x86695f12, 0x340e,0x4f04,0x9f,0xd3,0x92,0x53,0xdd,0x32,0x74,0x60);
39 DEFINE_GUID(ff_DXVA2_ModeH264_E, 0x1b81be68, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
40 DEFINE_GUID(ff_DXVA2_ModeH264_F, 0x1b81be69, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
41 DEFINE_GUID(ff_DXVADDI_Intel_ModeH264_E, 0x604F8E68, 0x4951,0x4C54,0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6);
42 DEFINE_GUID(ff_DXVA2_ModeVC1_D, 0x1b81beA3, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
43 DEFINE_GUID(ff_DXVA2_ModeVC1_D2010, 0x1b81beA4, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
44 DEFINE_GUID(ff_DXVA2_ModeHEVC_VLD_Main, 0x5b11d51b, 0x2f4c,0x4452,0xbc,0xc3,0x09,0xf2,0xa1,0x16,0x0c,0xc0);
45 DEFINE_GUID(ff_DXVA2_ModeHEVC_VLD_Main10,0x107af0e0, 0xef1a,0x4d19,0xab,0xa8,0x67,0xa1,0x63,0x07,0x3d,0x13);
46 DEFINE_GUID(ff_DXVA2_ModeVP9_VLD_Profile0,0x463707f8,0xa1d0,0x4585,0x87,0x6d,0x83,0xaa,0x6d,0x60,0xb8,0x9e);
47 DEFINE_GUID(ff_DXVA2_NoEncrypt, 0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
48 DEFINE_GUID(ff_GUID_NULL, 0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
49 DEFINE_GUID(ff_IID_IDirectXVideoDecoderService, 0xfc51a551,0xd5e7,0x11d9,0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
50 
51 typedef struct dxva_mode {
52  const GUID *guid;
54  // List of supported profiles, terminated by a FF_PROFILE_UNKNOWN entry.
55  // If NULL, don't check profile.
56  const int *profiles;
57 } dxva_mode;
58 
66 static const int prof_hevc_main[] = {FF_PROFILE_HEVC_MAIN,
70 
71 static const dxva_mode dxva_modes[] = {
72  /* MPEG-2 */
73  { &ff_DXVA2_ModeMPEG2_VLD, AV_CODEC_ID_MPEG2VIDEO, prof_mpeg2_main },
74  { &ff_DXVA2_ModeMPEG2and1_VLD, AV_CODEC_ID_MPEG2VIDEO, prof_mpeg2_main },
75 
76  /* H.264 */
77  { &ff_DXVA2_ModeH264_F, AV_CODEC_ID_H264, prof_h264_high },
78  { &ff_DXVA2_ModeH264_E, AV_CODEC_ID_H264, prof_h264_high },
79  /* Intel specific H.264 mode */
80  { &ff_DXVADDI_Intel_ModeH264_E, AV_CODEC_ID_H264, prof_h264_high },
81 
82  /* VC-1 / WMV3 */
83  { &ff_DXVA2_ModeVC1_D2010, AV_CODEC_ID_VC1 },
84  { &ff_DXVA2_ModeVC1_D2010, AV_CODEC_ID_WMV3 },
85  { &ff_DXVA2_ModeVC1_D, AV_CODEC_ID_VC1 },
86  { &ff_DXVA2_ModeVC1_D, AV_CODEC_ID_WMV3 },
87 
88  /* HEVC/H.265 */
89  { &ff_DXVA2_ModeHEVC_VLD_Main10, AV_CODEC_ID_HEVC, prof_hevc_main10 },
90  { &ff_DXVA2_ModeHEVC_VLD_Main, AV_CODEC_ID_HEVC, prof_hevc_main },
91 
92  /* VP8/9 */
93  { &ff_DXVA2_ModeVP9_VLD_Profile0,AV_CODEC_ID_VP9 },
94 
95  { NULL, 0 },
96 };
97 
99  const void *cfg_list,
100  unsigned cfg_count)
101 {
103  unsigned i, best_score = 0;
104  int best_cfg = -1;
105 
106  for (i = 0; i < cfg_count; i++) {
107  unsigned score;
108  UINT ConfigBitstreamRaw;
109  GUID guidConfigBitstreamEncryption;
110 
111 #if CONFIG_D3D11VA
112  if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
113  D3D11_VIDEO_DECODER_CONFIG *cfg = &((D3D11_VIDEO_DECODER_CONFIG *)cfg_list)[i];
114  ConfigBitstreamRaw = cfg->ConfigBitstreamRaw;
115  guidConfigBitstreamEncryption = cfg->guidConfigBitstreamEncryption;
116  }
117 #endif
118 #if CONFIG_DXVA2
119  if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
120  DXVA2_ConfigPictureDecode *cfg = &((DXVA2_ConfigPictureDecode *)cfg_list)[i];
121  ConfigBitstreamRaw = cfg->ConfigBitstreamRaw;
122  guidConfigBitstreamEncryption = cfg->guidConfigBitstreamEncryption;
123  }
124 #endif
125 
126  if (ConfigBitstreamRaw == 1)
127  score = 1;
128  else if (avctx->codec_id == AV_CODEC_ID_H264 && ConfigBitstreamRaw == 2)
129  score = 2;
130  else
131  continue;
132  if (IsEqualGUID(&guidConfigBitstreamEncryption, &ff_DXVA2_NoEncrypt))
133  score += 16;
134  if (score > best_score) {
135  best_score = score;
136  best_cfg = i;
137  }
138  }
139 
140  if (!best_score) {
141  av_log(avctx, AV_LOG_VERBOSE, "No valid decoder configuration available\n");
142  return AVERROR(EINVAL);
143  }
144 
145  return best_cfg;
146 }
147 
148 #if CONFIG_D3D11VA
149 static int d3d11va_validate_output(void *service, GUID guid, const void *surface_format)
150 {
151  HRESULT hr;
153  hr = ID3D11VideoDevice_CheckVideoDecoderFormat((ID3D11VideoDevice *)service,
154  &guid,
155  *(DXGI_FORMAT *)surface_format,
156  &is_supported);
157  return SUCCEEDED(hr) && is_supported;
158 }
159 #endif
160 
161 #if CONFIG_DXVA2
162 static int dxva2_validate_output(void *decoder_service, GUID guid, const void *surface_format)
163 {
164  HRESULT hr;
165  int ret = 0;
166  unsigned j, target_count;
167  D3DFORMAT *target_list;
168  hr = IDirectXVideoDecoderService_GetDecoderRenderTargets((IDirectXVideoDecoderService *)decoder_service, &guid, &target_count, &target_list);
169  if (SUCCEEDED(hr)) {
170  for (j = 0; j < target_count; j++) {
171  const D3DFORMAT format = target_list[j];
172  if (format == *(D3DFORMAT *)surface_format) {
173  ret = 1;
174  break;
175  }
176  }
177  CoTaskMemFree(target_list);
178  }
179  return ret;
180 }
181 #endif
182 
184 {
185  if (mode->codec != avctx->codec_id)
186  return 0;
187 
189  int i, found = 0;
190  for (i = 0; mode->profiles[i] != FF_PROFILE_UNKNOWN; i++) {
191  if (avctx->profile == mode->profiles[i]) {
192  found = 1;
193  break;
194  }
195  }
196  if (!found)
197  return 0;
198  }
199 
200  return 1;
201 }
202 
203 static void dxva_list_guids_debug(AVCodecContext *avctx, void *service,
204  unsigned guid_count, const GUID *guid_list)
205 {
207  int i;
208 
209  av_log(avctx, AV_LOG_VERBOSE, "Decoder GUIDs reported as supported:\n");
210 
211  for (i = 0; i < guid_count; i++) {
212  const GUID *guid = &guid_list[i];
213 
214  av_log(avctx, AV_LOG_VERBOSE,
215  "{%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x}",
216  (unsigned) guid->Data1, guid->Data2, guid->Data3,
217  guid->Data4[0], guid->Data4[1],
218  guid->Data4[2], guid->Data4[3],
219  guid->Data4[4], guid->Data4[5],
220  guid->Data4[6], guid->Data4[7]);
221 
222 #if CONFIG_D3D11VA
223  if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
224  DXGI_FORMAT format;
225  // We don't know the maximum valid DXGI_FORMAT, so use 200 as
226  // arbitrary upper bound (that could become outdated).
227  for (format = 0; format < 200; format++) {
228  if (d3d11va_validate_output(service, *guid, &format))
229  av_log(avctx, AV_LOG_VERBOSE, " %d", (int)format);
230  }
231  }
232 #endif
233 #if CONFIG_DXVA2
234  if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
235  const D3DFORMAT formats[] = {MKTAG('N', 'V', '1', '2'),
236  MKTAG('P', '0', '1', '0')};
237  int i;
238  for (i = 0; i < FF_ARRAY_ELEMS(formats); i++) {
239  if (dxva2_validate_output(service, *guid, &formats[i]))
240  av_log(avctx, AV_LOG_VERBOSE, " %d", i);
241  }
242  }
243 #endif
244  av_log(avctx, AV_LOG_VERBOSE, "\n");
245  }
246 }
247 
248 static int dxva_get_decoder_guid(AVCodecContext *avctx, void *service, void *surface_format,
249  unsigned guid_count, const GUID *guid_list, GUID *decoder_guid)
250 {
252  unsigned i, j;
253 
254  dxva_list_guids_debug(avctx, service, guid_count, guid_list);
255 
256  *decoder_guid = ff_GUID_NULL;
257  for (i = 0; dxva_modes[i].guid; i++) {
258  const dxva_mode *mode = &dxva_modes[i];
259  int validate;
260  if (!dxva_check_codec_compatibility(avctx, mode))
261  continue;
262 
263  for (j = 0; j < guid_count; j++) {
264  if (IsEqualGUID(mode->guid, &guid_list[j]))
265  break;
266  }
267  if (j == guid_count)
268  continue;
269 
270 #if CONFIG_D3D11VA
271  if (sctx->pix_fmt == AV_PIX_FMT_D3D11)
272  validate = d3d11va_validate_output(service, *mode->guid, surface_format);
273 #endif
274 #if CONFIG_DXVA2
275  if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
276  validate = dxva2_validate_output(service, *mode->guid, surface_format);
277 #endif
278  if (validate) {
279  *decoder_guid = *mode->guid;
280  break;
281  }
282  }
283 
284  if (IsEqualGUID(decoder_guid, &ff_GUID_NULL)) {
285  av_log(avctx, AV_LOG_VERBOSE, "No decoder device for codec found\n");
286  return AVERROR(EINVAL);
287  }
288 
289  if (IsEqualGUID(decoder_guid, &ff_DXVADDI_Intel_ModeH264_E))
291 
292  return 0;
293 }
294 
295 static void bufref_free_interface(void *opaque, uint8_t *data)
296 {
297  IUnknown_Release((IUnknown *)opaque);
298 }
299 
300 static AVBufferRef *bufref_wrap_interface(IUnknown *iface)
301 {
302  return av_buffer_create((uint8_t*)iface, 1, bufref_free_interface, iface, 0);
303 }
304 
305 #if CONFIG_DXVA2
306 
307 static int dxva2_get_decoder_configuration(AVCodecContext *avctx, const GUID *device_guid,
308  const DXVA2_VideoDesc *desc,
309  DXVA2_ConfigPictureDecode *config)
310 {
312  unsigned cfg_count;
313  DXVA2_ConfigPictureDecode *cfg_list;
314  HRESULT hr;
315  int ret;
316 
317  hr = IDirectXVideoDecoderService_GetDecoderConfigurations(sctx->dxva2_service, device_guid, desc, NULL, &cfg_count, &cfg_list);
318  if (FAILED(hr)) {
319  av_log(avctx, AV_LOG_ERROR, "Unable to retrieve decoder configurations\n");
320  return AVERROR(EINVAL);
321  }
322 
323  ret = dxva_get_decoder_configuration(avctx, cfg_list, cfg_count);
324  if (ret >= 0)
325  *config = cfg_list[ret];
326  CoTaskMemFree(cfg_list);
327  return ret;
328 }
329 
330 static int dxva2_create_decoder(AVCodecContext *avctx)
331 {
333  GUID *guid_list;
334  unsigned guid_count;
335  GUID device_guid;
336  D3DFORMAT surface_format = avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
337  MKTAG('P', '0', '1', '0') : MKTAG('N', 'V', '1', '2');
338  DXVA2_VideoDesc desc = { 0 };
339  DXVA2_ConfigPictureDecode config;
340  HRESULT hr;
341  int ret;
342  HANDLE device_handle;
343  AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
344  AVDXVA2FramesContext *frames_hwctx = frames_ctx->hwctx;
345  AVDXVA2DeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
346 
347  hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr,
348  &device_handle);
349  if (FAILED(hr)) {
350  av_log(avctx, AV_LOG_ERROR, "Failed to open a device handle\n");
351  goto fail;
352  }
353 
354  hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr, device_handle,
355  &ff_IID_IDirectXVideoDecoderService,
356  (void **)&sctx->dxva2_service);
357  IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, device_handle);
358  if (FAILED(hr)) {
359  av_log(avctx, AV_LOG_ERROR, "Failed to create IDirectXVideoDecoderService\n");
360  goto fail;
361  }
362 
363  hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(sctx->dxva2_service, &guid_count, &guid_list);
364  if (FAILED(hr)) {
365  av_log(avctx, AV_LOG_ERROR, "Failed to retrieve decoder device GUIDs\n");
366  goto fail;
367  }
368 
369  ret = dxva_get_decoder_guid(avctx, sctx->dxva2_service, &surface_format,
370  guid_count, guid_list, &device_guid);
371  CoTaskMemFree(guid_list);
372  if (ret < 0) {
373  goto fail;
374  }
375 
376  desc.SampleWidth = avctx->coded_width;
377  desc.SampleHeight = avctx->coded_height;
378  desc.Format = surface_format;
379 
380  ret = dxva2_get_decoder_configuration(avctx, &device_guid, &desc, &config);
381  if (ret < 0) {
382  goto fail;
383  }
384 
385  hr = IDirectXVideoDecoderService_CreateVideoDecoder(sctx->dxva2_service, &device_guid,
386  &desc, &config, frames_hwctx->surfaces,
387  frames_hwctx->nb_surfaces, &sctx->dxva2_decoder);
388  if (FAILED(hr)) {
389  av_log(avctx, AV_LOG_ERROR, "Failed to create DXVA2 video decoder\n");
390  goto fail;
391  }
392 
393  sctx->dxva2_config = config;
394 
395  sctx->decoder_ref = bufref_wrap_interface((IUnknown *)sctx->dxva2_decoder);
396  if (!sctx->decoder_ref)
397  return AVERROR(ENOMEM);
398 
399  return 0;
400 fail:
401  return AVERROR(EINVAL);
402 }
403 
404 #endif
405 
406 #if CONFIG_D3D11VA
407 
408 static int d3d11va_get_decoder_configuration(AVCodecContext *avctx,
409  ID3D11VideoDevice *video_device,
410  const D3D11_VIDEO_DECODER_DESC *desc,
411  D3D11_VIDEO_DECODER_CONFIG *config)
412 {
413  unsigned cfg_count = 0;
414  D3D11_VIDEO_DECODER_CONFIG *cfg_list = NULL;
415  HRESULT hr;
416  int i, ret;
417 
418  hr = ID3D11VideoDevice_GetVideoDecoderConfigCount(video_device, desc, &cfg_count);
419  if (FAILED(hr)) {
420  av_log(avctx, AV_LOG_ERROR, "Unable to retrieve decoder configurations\n");
421  return AVERROR(EINVAL);
422  }
423 
424  cfg_list = av_malloc_array(cfg_count, sizeof(D3D11_VIDEO_DECODER_CONFIG));
425  if (cfg_list == NULL)
426  return AVERROR(ENOMEM);
427  for (i = 0; i < cfg_count; i++) {
428  hr = ID3D11VideoDevice_GetVideoDecoderConfig(video_device, desc, i, &cfg_list[i]);
429  if (FAILED(hr)) {
430  av_log(avctx, AV_LOG_ERROR, "Unable to retrieve decoder configurations. (hr=0x%lX)\n", hr);
431  av_free(cfg_list);
432  return AVERROR(EINVAL);
433  }
434  }
435 
436  ret = dxva_get_decoder_configuration(avctx, cfg_list, cfg_count);
437  if (ret >= 0)
438  *config = cfg_list[ret];
439  av_free(cfg_list);
440  return ret;
441 }
442 
443 static DXGI_FORMAT d3d11va_map_sw_to_hw_format(enum AVPixelFormat pix_fmt)
444 {
445  switch (pix_fmt) {
446  case AV_PIX_FMT_NV12: return DXGI_FORMAT_NV12;
447  case AV_PIX_FMT_P010: return DXGI_FORMAT_P010;
448  case AV_PIX_FMT_YUV420P: return DXGI_FORMAT_420_OPAQUE;
449  default: return DXGI_FORMAT_UNKNOWN;
450  }
451 }
452 
453 static int d3d11va_create_decoder(AVCodecContext *avctx)
454 {
456  GUID *guid_list;
457  unsigned guid_count, i;
458  GUID decoder_guid;
459  D3D11_VIDEO_DECODER_DESC desc = { 0 };
460  D3D11_VIDEO_DECODER_CONFIG config;
461  AVHWFramesContext *frames_ctx = (AVHWFramesContext *)avctx->hw_frames_ctx->data;
462  AVD3D11VADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
463  AVD3D11VAFramesContext *frames_hwctx = frames_ctx->hwctx;
464  DXGI_FORMAT surface_format = d3d11va_map_sw_to_hw_format(frames_ctx->sw_format);
465  D3D11_TEXTURE2D_DESC texdesc;
466  HRESULT hr;
467  int ret;
468 
469  if (!frames_hwctx->texture) {
470  av_log(avctx, AV_LOG_ERROR, "AVD3D11VAFramesContext.texture not set.\n");
471  return AVERROR(EINVAL);
472  }
473  ID3D11Texture2D_GetDesc(frames_hwctx->texture, &texdesc);
474 
475  guid_count = ID3D11VideoDevice_GetVideoDecoderProfileCount(device_hwctx->video_device);
476  guid_list = av_malloc_array(guid_count, sizeof(*guid_list));
477  if (guid_list == NULL || guid_count == 0) {
478  av_log(avctx, AV_LOG_ERROR, "Failed to get the decoder GUIDs\n");
479  av_free(guid_list);
480  return AVERROR(EINVAL);
481  }
482  for (i = 0; i < guid_count; i++) {
483  hr = ID3D11VideoDevice_GetVideoDecoderProfile(device_hwctx->video_device, i, &guid_list[i]);
484  if (FAILED(hr)) {
485  av_log(avctx, AV_LOG_ERROR, "Failed to retrieve decoder GUID %d\n", i);
486  av_free(guid_list);
487  return AVERROR(EINVAL);
488  }
489  }
490 
491  ret = dxva_get_decoder_guid(avctx, device_hwctx->video_device, &surface_format,
492  guid_count, guid_list, &decoder_guid);
493  av_free(guid_list);
494  if (ret < 0)
495  return AVERROR(EINVAL);
496 
497  desc.SampleWidth = avctx->coded_width;
498  desc.SampleHeight = avctx->coded_height;
499  desc.OutputFormat = surface_format;
500  desc.Guid = decoder_guid;
501 
502  ret = d3d11va_get_decoder_configuration(avctx, device_hwctx->video_device, &desc, &config);
503  if (ret < 0)
504  return AVERROR(EINVAL);
505 
506  sctx->d3d11_views = av_mallocz_array(texdesc.ArraySize, sizeof(sctx->d3d11_views[0]));
507  if (!sctx->d3d11_views)
508  return AVERROR(ENOMEM);
509  sctx->nb_d3d11_views = texdesc.ArraySize;
510 
511  for (i = 0; i < sctx->nb_d3d11_views; i++) {
512  D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc = {
513  .DecodeProfile = decoder_guid,
514  .ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D,
515  .Texture2D = {
516  .ArraySlice = i,
517  }
518  };
519  hr = ID3D11VideoDevice_CreateVideoDecoderOutputView(device_hwctx->video_device,
520  (ID3D11Resource*) frames_hwctx->texture,
521  &viewDesc,
522  (ID3D11VideoDecoderOutputView**) &sctx->d3d11_views[i]);
523  if (FAILED(hr)) {
524  av_log(avctx, AV_LOG_ERROR, "Could not create the decoder output view %d\n", i);
525  return AVERROR_UNKNOWN;
526  }
527  }
528 
529  hr = ID3D11VideoDevice_CreateVideoDecoder(device_hwctx->video_device, &desc,
530  &config, &sctx->d3d11_decoder);
531  if (FAILED(hr)) {
532  av_log(avctx, AV_LOG_ERROR, "Failed to create D3D11VA video decoder\n");
533  return AVERROR(EINVAL);
534  }
535 
536  sctx->d3d11_config = config;
537  sctx->d3d11_texture = frames_hwctx->texture;
538 
539  sctx->decoder_ref = bufref_wrap_interface((IUnknown *)sctx->d3d11_decoder);
540  if (!sctx->decoder_ref)
541  return AVERROR(ENOMEM);
542 
543  return 0;
544 }
545 
546 #endif
547 
548 static void ff_dxva2_lock(AVCodecContext *avctx)
549 {
550 #if CONFIG_D3D11VA
551  if (ff_dxva2_is_d3d11(avctx)) {
553  AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
554  if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
555  WaitForSingleObjectEx(D3D11VA_CONTEXT(ctx)->context_mutex, INFINITE, FALSE);
556  if (sctx->device_ctx) {
557  AVD3D11VADeviceContext *hwctx = sctx->device_ctx->hwctx;
558  hwctx->lock(hwctx->lock_ctx);
559  }
560  }
561 #endif
562 }
563 
564 static void ff_dxva2_unlock(AVCodecContext *avctx)
565 {
566 #if CONFIG_D3D11VA
567  if (ff_dxva2_is_d3d11(avctx)) {
569  AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
570  if (D3D11VA_CONTEXT(ctx)->context_mutex != INVALID_HANDLE_VALUE)
571  ReleaseMutex(D3D11VA_CONTEXT(ctx)->context_mutex);
572  if (sctx->device_ctx) {
573  AVD3D11VADeviceContext *hwctx = sctx->device_ctx->hwctx;
574  hwctx->unlock(hwctx->lock_ctx);
575  }
576  }
577 #endif
578 }
579 
581  AVBufferRef *hw_frames_ctx)
582 {
583  AVHWFramesContext *frames_ctx = (AVHWFramesContext *)hw_frames_ctx->data;
584  AVHWDeviceContext *device_ctx = frames_ctx->device_ctx;
585  int surface_alignment, num_surfaces;
586 
587  if (device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
588  frames_ctx->format = AV_PIX_FMT_DXVA2_VLD;
589  } else if (device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) {
590  frames_ctx->format = AV_PIX_FMT_D3D11;
591  } else {
592  return AVERROR(EINVAL);
593  }
594 
595  /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
596  but it causes issues for H.264 on certain AMD GPUs..... */
597  if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO)
598  surface_alignment = 32;
599  /* the HEVC DXVA2 spec asks for 128 pixel aligned surfaces to ensure
600  all coding features have enough room to work with */
601  else if (avctx->codec_id == AV_CODEC_ID_HEVC)
602  surface_alignment = 128;
603  else
604  surface_alignment = 16;
605 
606  /* 1 base work surface */
607  num_surfaces = 1;
608 
609  /* add surfaces based on number of possible refs */
610  if (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_HEVC)
611  num_surfaces += 16;
612  else if (avctx->codec_id == AV_CODEC_ID_VP9)
613  num_surfaces += 8;
614  else
615  num_surfaces += 2;
616 
617  /* add extra surfaces for frame threading */
618  if (avctx->active_thread_type & FF_THREAD_FRAME)
619  num_surfaces += avctx->thread_count;
620 
621  frames_ctx->sw_format = avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
623  frames_ctx->width = FFALIGN(avctx->coded_width, surface_alignment);
624  frames_ctx->height = FFALIGN(avctx->coded_height, surface_alignment);
625  frames_ctx->initial_pool_size = num_surfaces;
626 
627 
628 #if CONFIG_DXVA2
629  if (frames_ctx->format == AV_PIX_FMT_DXVA2_VLD) {
630  AVDXVA2FramesContext *frames_hwctx = frames_ctx->hwctx;
631 
632  frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
633  }
634 #endif
635 
636 #if CONFIG_D3D11VA
637  if (frames_ctx->format == AV_PIX_FMT_D3D11) {
638  AVD3D11VAFramesContext *frames_hwctx = frames_ctx->hwctx;
639 
640  frames_hwctx->BindFlags |= D3D11_BIND_DECODER;
641  }
642 #endif
643 
644  return 0;
645 }
646 
648 {
650  AVHWFramesContext *frames_ctx;
651  enum AVHWDeviceType dev_type = avctx->hwaccel->pix_fmt == AV_PIX_FMT_DXVA2_VLD
653  int ret = 0;
654 
655  // Old API.
656  if (avctx->hwaccel_context)
657  return 0;
658 
659  // (avctx->pix_fmt is not updated yet at this point)
660  sctx->pix_fmt = avctx->hwaccel->pix_fmt;
661 
662  ret = ff_decode_get_hw_frames_ctx(avctx, dev_type);
663  if (ret < 0)
664  return ret;
665 
666  frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
667  sctx->device_ctx = frames_ctx->device_ctx;
668 
669  if (frames_ctx->format != sctx->pix_fmt) {
670  av_log(avctx, AV_LOG_ERROR, "Invalid pixfmt for hwaccel!\n");
671  ret = AVERROR(EINVAL);
672  goto fail;
673  }
674 
675 #if CONFIG_D3D11VA
676  if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
677  AVD3D11VADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
678  AVD3D11VAContext *d3d11_ctx = &sctx->ctx.d3d11va;
679 
680  ff_dxva2_lock(avctx);
681  ret = d3d11va_create_decoder(avctx);
682  ff_dxva2_unlock(avctx);
683  if (ret < 0)
684  goto fail;
685 
686  d3d11_ctx->decoder = sctx->d3d11_decoder;
687  d3d11_ctx->video_context = device_hwctx->video_context;
688  d3d11_ctx->cfg = &sctx->d3d11_config;
689  d3d11_ctx->surface_count = sctx->nb_d3d11_views;
690  d3d11_ctx->surface = sctx->d3d11_views;
691  d3d11_ctx->workaround = sctx->workaround;
692  d3d11_ctx->context_mutex = INVALID_HANDLE_VALUE;
693  }
694 #endif
695 
696 #if CONFIG_DXVA2
697  if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
698  AVDXVA2FramesContext *frames_hwctx = frames_ctx->hwctx;
699  struct dxva_context *dxva_ctx = &sctx->ctx.dxva2;
700 
701  ff_dxva2_lock(avctx);
702  ret = dxva2_create_decoder(avctx);
703  ff_dxva2_unlock(avctx);
704  if (ret < 0)
705  goto fail;
706 
707  dxva_ctx->decoder = sctx->dxva2_decoder;
708  dxva_ctx->cfg = &sctx->dxva2_config;
709  dxva_ctx->surface = frames_hwctx->surfaces;
710  dxva_ctx->surface_count = frames_hwctx->nb_surfaces;
711  dxva_ctx->workaround = sctx->workaround;
712  }
713 #endif
714 
715  return 0;
716 
717 fail:
718  ff_dxva2_decode_uninit(avctx);
719  return ret;
720 }
721 
723 {
725  int i;
726 
728 
729 #if CONFIG_D3D11VA
730  for (i = 0; i < sctx->nb_d3d11_views; i++) {
731  if (sctx->d3d11_views[i])
732  ID3D11VideoDecoderOutputView_Release(sctx->d3d11_views[i]);
733  }
734  av_freep(&sctx->d3d11_views);
735 #endif
736 
737 #if CONFIG_DXVA2
738  if (sctx->dxva2_service)
739  IDirectXVideoDecoderService_Release(sctx->dxva2_service);
740 #endif
741 
742  return 0;
743 }
744 
745 static void *get_surface(const AVCodecContext *avctx, const AVFrame *frame)
746 {
747 #if CONFIG_D3D11VA
748  if (frame->format == AV_PIX_FMT_D3D11) {
750  intptr_t index = (intptr_t)frame->data[1];
751  if (index < 0 || index >= sctx->nb_d3d11_views ||
752  sctx->d3d11_texture != (ID3D11Texture2D *)frame->data[0]) {
753  av_log((void *)avctx, AV_LOG_ERROR, "get_buffer frame is invalid!\n");
754  return NULL;
755  }
756  return sctx->d3d11_views[index];
757  }
758 #endif
759  return frame->data[3];
760 }
761 
763  const AVDXVAContext *ctx,
764  const AVFrame *frame)
765 {
766  void *surface = get_surface(avctx, frame);
767  unsigned i;
768 
769 #if CONFIG_D3D11VA
770  if (avctx->pix_fmt == AV_PIX_FMT_D3D11)
771  return (intptr_t)frame->data[1];
772  if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
773  D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
774  ID3D11VideoDecoderOutputView_GetDesc((ID3D11VideoDecoderOutputView*) surface, &viewDesc);
775  return viewDesc.Texture2D.ArraySlice;
776  }
777 #endif
778 #if CONFIG_DXVA2
779  for (i = 0; i < DXVA_CONTEXT_COUNT(avctx, ctx); i++) {
780  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD && ctx->dxva2.surface[i] == surface)
781  return i;
782  }
783 #endif
784 
785  assert(0);
786  return 0;
787 }
788 
791  DECODER_BUFFER_DESC *dsc,
792  unsigned type, const void *data, unsigned size,
793  unsigned mb_count)
794 {
795  void *dxva_data;
796  unsigned dxva_size;
797  int result;
798  HRESULT hr = 0;
799 
800 #if CONFIG_D3D11VA
801  if (ff_dxva2_is_d3d11(avctx))
802  hr = ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
803  D3D11VA_CONTEXT(ctx)->decoder,
804  type,
805  &dxva_size, &dxva_data);
806 #endif
807 #if CONFIG_DXVA2
808  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
809  hr = IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder, type,
810  &dxva_data, &dxva_size);
811 #endif
812  if (FAILED(hr)) {
813  av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %u: 0x%x\n",
814  type, (unsigned)hr);
815  return -1;
816  }
817  if (size <= dxva_size) {
818  memcpy(dxva_data, data, size);
819 
820 #if CONFIG_D3D11VA
821  if (ff_dxva2_is_d3d11(avctx)) {
822  D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = dsc;
823  memset(dsc11, 0, sizeof(*dsc11));
824  dsc11->BufferType = type;
825  dsc11->DataSize = size;
826  dsc11->NumMBsInBuffer = mb_count;
827  }
828 #endif
829 #if CONFIG_DXVA2
830  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
831  DXVA2_DecodeBufferDesc *dsc2 = dsc;
832  memset(dsc2, 0, sizeof(*dsc2));
833  dsc2->CompressedBufferType = type;
834  dsc2->DataSize = size;
835  dsc2->NumMBsInBuffer = mb_count;
836  }
837 #endif
838 
839  result = 0;
840  } else {
841  av_log(avctx, AV_LOG_ERROR, "Buffer for type %u was too small\n", type);
842  result = -1;
843  }
844 
845 #if CONFIG_D3D11VA
846  if (ff_dxva2_is_d3d11(avctx))
847  hr = ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type);
848 #endif
849 #if CONFIG_DXVA2
850  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
851  hr = IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type);
852 #endif
853  if (FAILED(hr)) {
854  av_log(avctx, AV_LOG_ERROR,
855  "Failed to release buffer type %u: 0x%x\n",
856  type, (unsigned)hr);
857  result = -1;
858  }
859  return result;
860 }
861 
863 {
864  int i;
865 
866  for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
867  if (!frame->buf[i]) {
868  frame->buf[i] = av_buffer_ref(ref);
869  return frame->buf[i] ? 0 : AVERROR(ENOMEM);
870  }
871  }
872 
873  // For now we expect that the caller does not use more than
874  // AV_NUM_DATA_POINTERS-1 buffers if the user uses a custom pool.
875  return AVERROR(EINVAL);
876 }
877 
879  const void *pp, unsigned pp_size,
880  const void *qm, unsigned qm_size,
881  int (*commit_bs_si)(AVCodecContext *,
883  DECODER_BUFFER_DESC *slice))
884 {
885  AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
886  unsigned buffer_count = 0;
887 #if CONFIG_D3D11VA
888  D3D11_VIDEO_DECODER_BUFFER_DESC buffer11[4];
889 #endif
890 #if CONFIG_DXVA2
891  DXVA2_DecodeBufferDesc buffer2[4];
892 #endif
893  DECODER_BUFFER_DESC *buffer = NULL, *buffer_slice = NULL;
894  int result, runs = 0;
895  HRESULT hr;
896  unsigned type;
898 
899  if (sctx->decoder_ref) {
900  result = frame_add_buf(frame, sctx->decoder_ref);
901  if (result < 0)
902  return result;
903  }
904 
905  do {
906  ff_dxva2_lock(avctx);
907 #if CONFIG_D3D11VA
908  if (ff_dxva2_is_d3d11(avctx))
909  hr = ID3D11VideoContext_DecoderBeginFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder,
910  get_surface(avctx, frame),
911  0, NULL);
912 #endif
913 #if CONFIG_DXVA2
914  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
915  hr = IDirectXVideoDecoder_BeginFrame(DXVA2_CONTEXT(ctx)->decoder,
916  get_surface(avctx, frame),
917  NULL);
918 #endif
919  if (hr != E_PENDING || ++runs > 50)
920  break;
921  ff_dxva2_unlock(avctx);
922  av_usleep(2000);
923  } while(1);
924 
925  if (FAILED(hr)) {
926  av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%x\n", (unsigned)hr);
927  ff_dxva2_unlock(avctx);
928  return -1;
929  }
930 
931 #if CONFIG_D3D11VA
932  if (ff_dxva2_is_d3d11(avctx)) {
933  buffer = &buffer11[buffer_count];
934  type = D3D11_VIDEO_DECODER_BUFFER_PICTURE_PARAMETERS;
935  }
936 #endif
937 #if CONFIG_DXVA2
938  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
939  buffer = &buffer2[buffer_count];
940  type = DXVA2_PictureParametersBufferType;
941  }
942 #endif
943  result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
944  type,
945  pp, pp_size, 0);
946  if (result) {
947  av_log(avctx, AV_LOG_ERROR,
948  "Failed to add picture parameter buffer\n");
949  goto end;
950  }
951  buffer_count++;
952 
953  if (qm_size > 0) {
954 #if CONFIG_D3D11VA
955  if (ff_dxva2_is_d3d11(avctx)) {
956  buffer = &buffer11[buffer_count];
957  type = D3D11_VIDEO_DECODER_BUFFER_INVERSE_QUANTIZATION_MATRIX;
958  }
959 #endif
960 #if CONFIG_DXVA2
961  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
962  buffer = &buffer2[buffer_count];
963  type = DXVA2_InverseQuantizationMatrixBufferType;
964  }
965 #endif
966  result = ff_dxva2_commit_buffer(avctx, ctx, buffer,
967  type,
968  qm, qm_size, 0);
969  if (result) {
970  av_log(avctx, AV_LOG_ERROR,
971  "Failed to add inverse quantization matrix buffer\n");
972  goto end;
973  }
974  buffer_count++;
975  }
976 
977 #if CONFIG_D3D11VA
978  if (ff_dxva2_is_d3d11(avctx)) {
979  buffer = &buffer11[buffer_count + 0];
980  buffer_slice = &buffer11[buffer_count + 1];
981  }
982 #endif
983 #if CONFIG_DXVA2
984  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
985  buffer = &buffer2[buffer_count + 0];
986  buffer_slice = &buffer2[buffer_count + 1];
987  }
988 #endif
989 
990  result = commit_bs_si(avctx,
991  buffer,
992  buffer_slice);
993  if (result) {
994  av_log(avctx, AV_LOG_ERROR,
995  "Failed to add bitstream or slice control buffer\n");
996  goto end;
997  }
998  buffer_count += 2;
999 
1000  /* TODO Film Grain when possible */
1001 
1002  assert(buffer_count == 1 + (qm_size > 0) + 2);
1003 
1004 #if CONFIG_D3D11VA
1005  if (ff_dxva2_is_d3d11(avctx))
1006  hr = ID3D11VideoContext_SubmitDecoderBuffers(D3D11VA_CONTEXT(ctx)->video_context,
1007  D3D11VA_CONTEXT(ctx)->decoder,
1008  buffer_count, buffer11);
1009 #endif
1010 #if CONFIG_DXVA2
1011  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
1012  DXVA2_DecodeExecuteParams exec = {
1013  .NumCompBuffers = buffer_count,
1014  .pCompressedBuffers = buffer2,
1015  .pExtensionData = NULL,
1016  };
1017  hr = IDirectXVideoDecoder_Execute(DXVA2_CONTEXT(ctx)->decoder, &exec);
1018  }
1019 #endif
1020  if (FAILED(hr)) {
1021  av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%x\n", (unsigned)hr);
1022  result = -1;
1023  }
1024 
1025 end:
1026 #if CONFIG_D3D11VA
1027  if (ff_dxva2_is_d3d11(avctx))
1028  hr = ID3D11VideoContext_DecoderEndFrame(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder);
1029 #endif
1030 #if CONFIG_DXVA2
1031  if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
1032  hr = IDirectXVideoDecoder_EndFrame(DXVA2_CONTEXT(ctx)->decoder, NULL);
1033 #endif
1034  ff_dxva2_unlock(avctx);
1035  if (FAILED(hr)) {
1036  av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%x\n", (unsigned)hr);
1037  result = -1;
1038  }
1039 
1040  return result;
1041 }
1042 
1044 {
1045  if (CONFIG_D3D11VA)
1046  return avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD ||
1047  avctx->pix_fmt == AV_PIX_FMT_D3D11;
1048  else
1049  return 0;
1050 }
#define FF_PROFILE_H264_MAIN
Definition: avcodec.h:2868
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:60
uint32_t BOOL
#define NULL
Definition: coverity.c:32
This structure is used to provides the necessary configurations and data to the Direct3D11 FFmpeg HWA...
Definition: d3d11va.h:59
static enum AVPixelFormat pix_fmt
#define AV_NUM_DATA_POINTERS
Definition: frame.h:202
#define FF_PROFILE_MPEG2_MAIN
Definition: avcodec.h:2860
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
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:1689
DEFINE_GUID(ff_DXVA2_ModeMPEG2_VLD, 0xee27417f, 0x5e28, 0x4e65, 0xbe, 0xea, 0x1d, 0x26, 0xb5, 0x08, 0xad, 0xc9)
static int dxva_get_decoder_guid(AVCodecContext *avctx, void *service, void *surface_format, unsigned guid_count, const GUID *guid_list, GUID *decoder_guid)
Definition: dxva2.c:248
This struct is allocated as AVHWFramesContext.hwctx.
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:393
const char * desc
Definition: nvenc.c:63
static FFServerConfig config
Definition: ffserver.c:193
enum AVPixelFormat pix_fmt
Supported pixel format.
Definition: avcodec.h:3557
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:228
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1711
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:208
static void * get_surface(const AVCodecContext *avctx, const AVFrame *frame)
Definition: dxva2.c:745
LPDIRECT3DSURFACE9 * surface
The array of Direct3D surfaces used to create the decoder.
Definition: dxva2.h:76
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2658
int profile
profile
Definition: avcodec.h:2827
#define D3D11VA_CONTEXT(ctx)
const DXVA2_ConfigPictureDecode * cfg
DXVA2 configuration used to create the decoder.
Definition: dxva2.h:66
UINT BindFlags
D3D11_TEXTURE2D_DESC.BindFlags used for texture creation.
unsigned surface_count
The number of surface in the surface array.
Definition: dxva2.h:71
const int * profiles
Definition: dxva2.c:56
#define AV_PIX_FMT_P010
Definition: pixfmt.h:413
int ff_dxva2_common_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
Definition: dxva2.c:580
int ff_dxva2_decode_uninit(AVCodecContext *avctx)
Definition: dxva2.c:722
uint8_t
static const dxva_mode dxva_modes[]
Definition: dxva2.c:71
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2670
HANDLE context_mutex
Mutex to access video_context.
Definition: d3d11va.h:98
ID3D11VideoDecoder * decoder
D3D11 decoder object.
Definition: d3d11va.h:63
DWORD surface_type
The surface type (e.g.
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame, const void *pp, unsigned pp_size, const void *qm, unsigned qm_size, int(*commit_bs_si)(AVCodecContext *, DECODER_BUFFER_DESC *bs, DECODER_BUFFER_DESC *slice))
Definition: dxva2.c:878
#define FF_PROFILE_HEVC_MAIN
Definition: avcodec.h:2915
#define DXVA_SHARED_CONTEXT(avctx)
static void ff_dxva2_lock(AVCodecContext *avctx)
Definition: dxva2.c:548
static AVFrame * frame
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:91
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
ptrdiff_t size
Definition: opengl_enc.c:101
#define FF_PROFILE_HEVC_MAIN_10
Definition: avcodec.h:2916
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
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:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
D3D11_VIDEO_DECODER_CONFIG * cfg
D3D11 configuration used to create the decoder.
Definition: d3d11va.h:73
#define FALSE
Definition: windows2linux.h:37
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2772
static AVBufferRef * bufref_wrap_interface(IUnknown *iface)
Definition: dxva2.c:300
int ff_dxva2_is_d3d11(const AVCodecContext *avctx)
Definition: dxva2.c:1043
#define FF_PROFILE_H264_HIGH
Definition: avcodec.h:2870
#define DXVA2_CONTEXT(ctx)
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:85
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
static const int prof_hevc_main10[]
Definition: dxva2.c:68
#define fail()
Definition: checkasm.h:113
ID3D11VideoContext * video_context
If unset, this will be set from the device_context field on init.
int initial_pool_size
Initial size of the frame pool.
Definition: hwcontext.h:198
static void ff_dxva2_unlock(AVCodecContext *avctx)
Definition: dxva2.c:564
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:2764
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:148
static const chunk_decoder decoder[8]
Definition: dfa.c:330
PVOID HANDLE
#define FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO
Work around for Direct3D11 and old Intel GPUs with ClearVideo interface.
Definition: d3d11va.h:49
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames...
Definition: avcodec.h:3168
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2828
static void bufref_free_interface(void *opaque, uint8_t *data)
Definition: dxva2.c:295
unsigned surface_count
The number of surface in the surface array.
Definition: d3d11va.h:78
ID3D11VideoContext * video_context
D3D11 VideoContext.
Definition: d3d11va.h:68
AVFormatContext * ctx
Definition: movenc.c:48
static int dxva_get_decoder_configuration(AVCodecContext *avctx, const void *cfg_list, unsigned cfg_count)
Definition: dxva2.c:98
int ff_dxva2_decode_init(AVCodecContext *avctx)
Definition: dxva2.c:647
AVHWDeviceContext * device_ctx
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:220
enum AVPixelFormat pix_fmt
#define FF_ARRAY_ELEMS(a)
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2753
static int dxva_check_codec_compatibility(AVCodecContext *avctx, const dxva_mode *mode)
Definition: dxva2.c:183
static const int prof_h264_high[]
Definition: dxva2.c:62
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:274
void(* unlock)(void *lock_ctx)
Libavcodec external API header.
enum AVCodecID codec_id
Definition: avcodec.h:1512
unsigned ff_dxva2_get_surface_index(const AVCodecContext *avctx, const AVDXVAContext *ctx, const AVFrame *frame)
Definition: dxva2.c:762
static int is_supported(enum AVCodecID id)
Definition: rtpenc.c:49
main external API structure.
Definition: avcodec.h:1502
uint8_t * data
The data buffer.
Definition: buffer.h:89
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition: hwcontext.h:161
int ff_dxva2_commit_buffer(AVCodecContext *avctx, AVDXVAContext *ctx, DECODER_BUFFER_DESC *dsc, unsigned type, const void *data, unsigned size, unsigned mb_count)
Definition: dxva2.c:789
GLint GLenum type
Definition: opengl_enc.c:105
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:368
int coded_height
Definition: avcodec.h:1689
static const char * format
Definition: movenc.c:47
static const int prof_mpeg2_main[]
Definition: dxva2.c:59
int index
Definition: gxfenc.c:89
uint64_t workaround
A bit field configuring the workarounds needed for using the decoder.
Definition: d3d11va.h:88
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:123
const GUID * guid
Definition: dxva2.c:52
void DECODER_BUFFER_DESC
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer. ...
Definition: pixfmt.h:133
This struct is allocated as AVHWFramesContext.hwctx.
AVBufferRef * decoder_ref
DWORD HRESULT
int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx, enum AVHWDeviceType dev_type)
Make sure avctx.hw_frames_ctx is set.
Definition: decode.c:1149
#define FAILED(hr)
Definition: windows2linux.h:48
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
Hardware surfaces for Direct3D11.
Definition: pixfmt.h:309
#define SUCCEEDED(hr)
Definition: windows2linux.h:49
A reference to a data buffer.
Definition: buffer.h:81
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
AVDXVAContext ctx
common internal and external API header
if(ret< 0)
Definition: vf_mcdeint.c:279
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
ID3D11VideoDecoderOutputView ** surface
The array of Direct3D surfaces used to create the decoder.
Definition: d3d11va.h:83
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
IDirect3DSurface9 ** surfaces
The surface pool.
static int frame_add_buf(AVFrame *frame, AVBufferRef *ref)
Definition: dxva2.c:862
AVHWDeviceType
Definition: hwcontext.h:27
This struct is allocated as AVHWDeviceContext.hwctx.
#define av_free(p)
static void dxva_list_guids_debug(AVCodecContext *avctx, void *service, unsigned guid_count, const GUID *guid_list)
Definition: dxva2.c:203
#define FF_PROFILE_MPEG2_SIMPLE
Definition: avcodec.h:2861
#define DXVA_CONTEXT(avctx)
static const int prof_hevc_main[]
Definition: dxva2.c:66
This struct is allocated as AVHWDeviceContext.hwctx.
void(* lock)(void *lock_ctx)
Callbacks for locking.
#define av_freep(p)
unsigned int UINT
#define AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH
Hardware acceleration should still be attempted for decoding when the codec profile does not match th...
Definition: avcodec.h:3726
int hwaccel_flags
Bit set of AV_HWACCEL_FLAG_* flags, which affect hardware accelerated decoding (if active)...
Definition: avcodec.h:3229
HW decoding through Direct3D11 via old API, Picture.data[3] contains a ID3D11VideoDecoderOutputView p...
Definition: pixfmt.h:225
IDirectXVideoDecoder * decoder
DXVA2 decoder object.
Definition: dxva2.h:61
#define av_malloc_array(a, b)
#define INVALID_HANDLE_VALUE
Definition: windows2linux.h:47
formats
Definition: signature.h:48
#define FF_PROFILE_H264_CONSTRAINED_BASELINE
Definition: avcodec.h:2867
#define MKTAG(a, b, c, d)
Definition: common.h:366
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:221
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
enum AVCodecID codec
Definition: dxva2.c:53
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:3019
GLuint buffer
Definition: opengl_enc.c:102
This structure is used to provides the necessary configurations and data to the DXVA2 FFmpeg HWAccel ...
Definition: dxva2.h:57
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:191