24 #define _WIN32_WINNT 0x0600
25 #define DXVA2API_USE_BITFIELDS
46 DEFINE_GUID(IID_IDirectXVideoDecoderService, 0xfc51a551,0xd5e7,0x11d9,0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
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);
137 IDirect3DSurface9_Release(ctx->
surfaces[i]);
146 IDirectXVideoDecoder_Release(ctx->
decoder);
170 IDirect3DDeviceManager9_Release(ctx->
d3d9devmgr);
176 IDirect3D9_Release(ctx->
d3d9);
202 IDirect3DSurface9_Release(w->
surface);
203 IDirectXVideoDecoder_Release(w->
decoder);
211 int i, old_unused = -1;
212 LPDIRECT3DSURFACE9 surface;
222 if (old_unused == -1) {
237 if (!frame->
buf[0]) {
244 IDirect3DSurface9_AddRef(w->
surface);
246 IDirectXVideoDecoder_AddRef(w->
decoder);
258 LPDIRECT3DSURFACE9 surface = (LPDIRECT3DSURFACE9)frame->
data[3];
261 D3DSURFACE_DESC surfaceDesc;
262 D3DLOCKED_RECT LockedRect;
266 IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
268 ctx->tmp_frame->width = frame->
width;
276 hr = IDirect3DSurface9_LockRect(surface, &LockedRect,
NULL, D3DLOCK_READONLY);
287 (
uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
288 LockedRect.Pitch, frame->
width, frame->
height / 2);
290 IDirect3DSurface9_UnlockRect(surface);
313 D3DPRESENT_PARAMETERS d3dpp = {0};
314 D3DDISPLAYMODE d3ddm;
315 unsigned resetToken = 0;
316 UINT adapter = D3DADAPTER_DEFAULT;
331 av_log(
NULL, loglevel,
"Failed to load D3D9 library\n");
336 av_log(
NULL, loglevel,
"Failed to load DXVA2 library\n");
342 av_log(
NULL, loglevel,
"Failed to locate Direct3DCreate9\n");
346 if (!createDeviceManager) {
347 av_log(
NULL, loglevel,
"Failed to locate DXVA2CreateDirect3DDeviceManager9\n");
351 ctx->
d3d9 = createD3D(D3D_SDK_VERSION);
353 av_log(
NULL, loglevel,
"Failed to create IDirect3D object\n");
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;
371 hr = IDirect3D9_CreateDevice(ctx->
d3d9, adapter, D3DDEVTYPE_HAL, GetDesktopWindow(),
372 D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
375 av_log(
NULL, loglevel,
"Failed to create Direct3D device\n");
379 hr = createDeviceManager(&resetToken, &ctx->
d3d9devmgr);
381 av_log(
NULL, loglevel,
"Failed to create Direct3D device manager\n");
387 av_log(
NULL, loglevel,
"Failed to bind Direct3D device to device manager\n");
393 av_log(
NULL, loglevel,
"Failed to open device handle\n");
399 av_log(
NULL, loglevel,
"Failed to create IDirectXVideoDecoderService\n");
418 const DXVA2_VideoDesc *desc,
419 DXVA2_ConfigPictureDecode *
config)
424 unsigned cfg_count = 0, best_score = 0;
425 DXVA2_ConfigPictureDecode *cfg_list =
NULL;
426 DXVA2_ConfigPictureDecode best_cfg = {{0}};
430 hr = IDirectXVideoDecoderService_GetDecoderConfigurations(ctx->
decoder_service, device_guid, desc,
NULL, &cfg_count, &cfg_list);
432 av_log(
NULL, loglevel,
"Unable to retrieve decoder configurations\n");
436 for (i = 0; i < cfg_count; i++) {
437 DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
440 if (cfg->ConfigBitstreamRaw == 1)
446 if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
448 if (score > best_score) {
453 CoTaskMemFree(cfg_list);
456 av_log(
NULL, loglevel,
"No valid decoder configuration available\n");
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;
477 int surface_alignment;
480 hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(ctx->
decoder_service, &guid_count, &guid_list);
482 av_log(
NULL, loglevel,
"Failed to retrieve decoder device GUIDs\n");
486 for (i = 0; dxva2_modes[i].
guid; i++) {
487 D3DFORMAT *target_list =
NULL;
488 unsigned target_count = 0;
493 for (j = 0; j < guid_count; j++) {
494 if (IsEqualGUID(mode->
guid, &guid_list[j]))
500 hr = IDirectXVideoDecoderService_GetDecoderRenderTargets(ctx->
decoder_service, mode->
guid, &target_count, &target_list);
504 for (j = 0; j < target_count; j++) {
505 const D3DFORMAT
format = target_list[j];
506 if (format ==
MKTAG(
'N',
'V',
'1',
'2')) {
511 CoTaskMemFree(target_list);
513 device_guid = *mode->
guid;
517 CoTaskMemFree(guid_list);
519 if (IsEqualGUID(&device_guid, &GUID_NULL)) {
520 av_log(
NULL, loglevel,
"No decoder device for codec found\n");
526 desc.Format = target_format;
536 surface_alignment = 32;
540 surface_alignment = 128;
542 surface_alignment = 16;
563 av_log(
NULL, loglevel,
"Unable to allocate surface arrays\n");
571 target_format, D3DPOOL_DEFAULT, 0,
572 DXVA2_VideoDecoderRenderTarget,
579 hr = IDirectXVideoDecoderService_CreateVideoDecoder(ctx->
decoder_service, &device_guid,
583 av_log(
NULL, loglevel,
"Failed to create DXVA2 video decoder\n");
595 if (IsEqualGUID(&ctx->
decoder_guid, &DXVADDI_Intel_ModeH264_E))
620 av_log(
NULL, loglevel,
"Unsupported H.264 profile for DXVA2 HWAccel: %d\n", s->
profile);
629 av_log(
NULL, loglevel,
"Error creating the DXVA2 decoder\n");
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
int coded_width
Bitstream width / height, may be different from width/height e.g.
static int dxva2_create_decoder(AVCodecContext *s)
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
static FFServerConfig config
static void dxva2_uninit(AVCodecContext *s)
HRESULT WINAPI pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **)
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
LPDIRECT3DSURFACE9 * surface
The array of Direct3D surfaces used to create the decoder.
const DXVA2_ConfigPictureDecode * cfg
DXVA2 configuration used to create the decoder.
IDirect3DDeviceManager9 * d3d9devmgr
unsigned surface_count
The number of surface in the surface array.
int dxva2_init(AVCodecContext *s)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
void * hwaccel_context
Hardware accelerator context.
static int dxva2_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
#define AV_LOG_VERBOSE
Detailed information.
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
static const dxva2_mode dxva2_modes[]
uint64_t workaround
A bit field configuring the workarounds needed for using the decoder.
AVCodecID
Identify the syntax and semantics of the bitstream.
int width
width and height of the video frame
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int active_thread_type
Which multithreading methods are in use by the codec.
#define FF_PROFILE_H264_HIGH
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
simple assert() macros that are a bit more flexible than ISO C assert().
static void dxva2_release_buffer(void *opaque, uint8_t *data)
static void dxva2_destroy_decoder(AVCodecContext *s)
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.
reference-counted frame API
DXVA2_ConfigPictureDecode decoder_config
#define FF_THREAD_FRAME
Decode more than one frame at once.
IDirect3D9 *WINAPI pDirect3DCreate9(UINT)
#define FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO
Work around for Direct3D11 and old Intel GPUs with ClearVideo interface.
IDirectXVideoDecoder * decoder
preferred ID for MPEG-1/2 video decoding
static int dxva2_get_decoder_configuration(AVCodecContext *s, const GUID *device_guid, const DXVA2_VideoDesc *desc, DXVA2_ConfigPictureDecode *config)
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
LPDIRECT3DSURFACE9 * surfaces
#define AV_LOG_INFO
Standard information.
Public libavcodec DXVA2 header.
main external API structure.
static const char * format
refcounted data buffer API
IDirectXVideoDecoder * decoder
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer. ...
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
surface_info * surface_infos
static int dxva2_alloc(AVCodecContext *s)
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
DEFINE_GUID(IID_IDirectXVideoDecoderService, 0xfc51a551, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02)
static int dxva2_retrieve_data(AVCodecContext *s, AVFrame *frame)
IDirectXVideoDecoderService * decoder_service
LPDIRECT3DSURFACE9 surface
IDirectXVideoDecoder * decoder
DXVA2 decoder object.
#define INVALID_HANDLE_VALUE
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.
#define FF_PROFILE_H264_CONSTRAINED
#define MKTAG(a, b, c, d)
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
void * opaque
Private data of the user, can be used to carry app specific stuff.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
This structure is used to provides the necessary configurations and data to the DXVA2 FFmpeg HWAccel ...
IDirect3DDevice9 * d3d9device