dxva: DXVA2_ModeHEVC_VLD_Main10 does not support Main
[ffmpeg.git] / libavcodec / dxva2.c
index e0c5406..9ceb623 100644 (file)
@@ -49,18 +49,33 @@ DEFINE_GUID(ff_IID_IDirectXVideoDecoderService, 0xfc51a551,0xd5e7,0x11d9,0xaf,0x
 typedef struct dxva_mode {
     const GUID     *guid;
     enum AVCodecID codec;
+    // List of supported profiles, terminated by a FF_PROFILE_UNKNOWN entry.
+    // If NULL, don't check profile.
+    const int      *profiles;
 } dxva_mode;
 
+static const int prof_mpeg2_main[]   = {FF_PROFILE_MPEG2_SIMPLE,
+                                        FF_PROFILE_MPEG2_MAIN,
+                                        FF_PROFILE_UNKNOWN};
+static const int prof_h264_high[]    = {FF_PROFILE_H264_CONSTRAINED_BASELINE,
+                                        FF_PROFILE_H264_MAIN,
+                                        FF_PROFILE_H264_HIGH,
+                                        FF_PROFILE_UNKNOWN};
+static const int prof_hevc_main[]    = {FF_PROFILE_HEVC_MAIN,
+                                        FF_PROFILE_UNKNOWN};
+static const int prof_hevc_main10[]  = {FF_PROFILE_HEVC_MAIN_10,
+                                        FF_PROFILE_UNKNOWN};
+
 static const dxva_mode dxva_modes[] = {
     /* MPEG-2 */
-    { &ff_DXVA2_ModeMPEG2_VLD,       AV_CODEC_ID_MPEG2VIDEO },
-    { &ff_DXVA2_ModeMPEG2and1_VLD,   AV_CODEC_ID_MPEG2VIDEO },
+    { &ff_DXVA2_ModeMPEG2_VLD,       AV_CODEC_ID_MPEG2VIDEO, prof_mpeg2_main },
+    { &ff_DXVA2_ModeMPEG2and1_VLD,   AV_CODEC_ID_MPEG2VIDEO, prof_mpeg2_main },
 
     /* H.264 */
-    { &ff_DXVA2_ModeH264_F,          AV_CODEC_ID_H264 },
-    { &ff_DXVA2_ModeH264_E,          AV_CODEC_ID_H264 },
+    { &ff_DXVA2_ModeH264_F,          AV_CODEC_ID_H264, prof_h264_high },
+    { &ff_DXVA2_ModeH264_E,          AV_CODEC_ID_H264, prof_h264_high },
     /* Intel specific H.264 mode */
-    { &ff_DXVADDI_Intel_ModeH264_E,  AV_CODEC_ID_H264 },
+    { &ff_DXVADDI_Intel_ModeH264_E,  AV_CODEC_ID_H264, prof_h264_high },
 
     /* VC-1 / WMV3 */
     { &ff_DXVA2_ModeVC1_D2010,       AV_CODEC_ID_VC1 },
@@ -69,8 +84,8 @@ static const dxva_mode dxva_modes[] = {
     { &ff_DXVA2_ModeVC1_D,           AV_CODEC_ID_WMV3 },
 
     /* HEVC/H.265 */
-    { &ff_DXVA2_ModeHEVC_VLD_Main,   AV_CODEC_ID_HEVC },
-    { &ff_DXVA2_ModeHEVC_VLD_Main10, AV_CODEC_ID_HEVC },
+    { &ff_DXVA2_ModeHEVC_VLD_Main10, AV_CODEC_ID_HEVC, prof_hevc_main10 },
+    { &ff_DXVA2_ModeHEVC_VLD_Main,   AV_CODEC_ID_HEVC, prof_hevc_main },
 
     { NULL,                          0 },
 };
@@ -160,17 +175,84 @@ static int dxva2_validate_output(void *decoder_service, GUID guid, void *surface
 }
 #endif
 
+static int dxva_check_codec_compatibility(AVCodecContext *avctx, const dxva_mode *mode)
+{
+    if (mode->codec != avctx->codec_id)
+            return 0;
+
+    if (mode->profiles && !(avctx->hwaccel_flags & AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH)) {
+        int i, found = 0;
+        for (i = 0; mode->profiles[i] != FF_PROFILE_UNKNOWN; i++) {
+            if (avctx->profile == mode->profiles[i]) {
+                found = 1;
+                break;
+            }
+        }
+        if (!found)
+            return 0;
+    }
+
+    return 1;
+}
+
+static void dxva_list_guids_debug(AVCodecContext *avctx, void *service,
+                                 unsigned guid_count, const GUID *guid_list)
+{
+    FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
+    int i;
+
+    av_log(avctx, AV_LOG_VERBOSE, "Decoder GUIDs reported as supported:\n");
+
+    for (i = 0; i < guid_count; i++) {
+        const GUID *guid = &guid_list[i];
+
+        av_log(avctx, AV_LOG_VERBOSE,
+             "{%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x}",
+             (unsigned) guid->Data1, guid->Data2, guid->Data3,
+             guid->Data4[0], guid->Data4[1],
+             guid->Data4[2], guid->Data4[3],
+             guid->Data4[4], guid->Data4[5],
+             guid->Data4[6], guid->Data4[7]);
+
+#if CONFIG_D3D11VA
+        if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
+            DXGI_FORMAT format;
+            // We don't know the maximum valid DXGI_FORMAT, so use 200 as
+            // arbitrary upper bound (that could become outdated).
+            for (format = 0; format < 200; format++) {
+                if (d3d11va_validate_output(service, *guid, &format))
+                    av_log(avctx, AV_LOG_VERBOSE, " %d", (int)format);
+            }
+        }
+#endif
+#if CONFIG_DXVA2
+        if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
+            const D3DFORMAT formats[] = {MKTAG('N', 'V', '1', '2'),
+                                         MKTAG('P', '0', '1', '0')};
+            int i;
+            for (i = 0; i < FF_ARRAY_ELEMS(formats); i++) {
+                if (dxva2_validate_output(service, *guid, &formats[i]))
+                    av_log(avctx, AV_LOG_VERBOSE, " %d", i);
+            }
+        }
+#endif
+        av_log(avctx, AV_LOG_VERBOSE, "\n");
+    }
+}
+
 static int dxva_get_decoder_guid(AVCodecContext *avctx, void *service, void *surface_format,
                                  unsigned guid_count, const GUID *guid_list, GUID *decoder_guid)
 {
     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
     unsigned i, j;
 
+    dxva_list_guids_debug(avctx, service, guid_count, guid_list);
+
     *decoder_guid = ff_GUID_NULL;
     for (i = 0; dxva_modes[i].guid; i++) {
         const dxva_mode *mode = &dxva_modes[i];
         int validate;
-        if (mode->codec != avctx->codec_id)
+        if (!dxva_check_codec_compatibility(avctx, mode))
             continue;
 
         for (j = 0; j < guid_count; j++) {
@@ -323,7 +405,6 @@ static int d3d11va_get_decoder_configuration(AVCodecContext *avctx,
                                              const D3D11_VIDEO_DECODER_DESC *desc,
                                              D3D11_VIDEO_DECODER_CONFIG *config)
 {
-    FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
     unsigned cfg_count = 0;
     D3D11_VIDEO_DECODER_CONFIG *cfg_list = NULL;
     HRESULT hr;
@@ -354,19 +435,28 @@ static int d3d11va_get_decoder_configuration(AVCodecContext *avctx,
     return ret;
 }
 
+static DXGI_FORMAT d3d11va_map_sw_to_hw_format(enum AVPixelFormat pix_fmt)
+{
+    switch (pix_fmt) {
+    case AV_PIX_FMT_NV12:       return DXGI_FORMAT_NV12;
+    case AV_PIX_FMT_P010:       return DXGI_FORMAT_P010;
+    case AV_PIX_FMT_YUV420P:    return DXGI_FORMAT_420_OPAQUE;
+    default:                    return DXGI_FORMAT_UNKNOWN;
+    }
+}
+
 static int d3d11va_create_decoder(AVCodecContext *avctx)
 {
     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
     GUID *guid_list;
     unsigned guid_count, i;
     GUID decoder_guid;
-    DXGI_FORMAT surface_format = avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
-                                 DXGI_FORMAT_P010 : DXGI_FORMAT_NV12;
     D3D11_VIDEO_DECODER_DESC desc = { 0 };
     D3D11_VIDEO_DECODER_CONFIG config;
     AVHWFramesContext *frames_ctx = (AVHWFramesContext *)avctx->hw_frames_ctx->data;
     AVD3D11VADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
     AVD3D11VAFramesContext *frames_hwctx = frames_ctx->hwctx;
+    DXGI_FORMAT surface_format = d3d11va_map_sw_to_hw_format(frames_ctx->sw_format);
     D3D11_TEXTURE2D_DESC texdesc;
     HRESULT hr;
     int ret;
@@ -552,18 +642,6 @@ int ff_dxva2_decode_init(AVCodecContext *avctx)
     // (avctx->pix_fmt is not updated yet at this point)
     sctx->pix_fmt = avctx->hwaccel->pix_fmt;
 
-    if (avctx->codec_id == AV_CODEC_ID_H264 &&
-        (avctx->profile & ~FF_PROFILE_H264_CONSTRAINED) > FF_PROFILE_H264_HIGH) {
-        av_log(avctx, AV_LOG_VERBOSE, "Unsupported H.264 profile for DXVA HWAccel: %d\n",avctx->profile);
-        return AVERROR(ENOTSUP);
-    }
-
-    if (avctx->codec_id == AV_CODEC_ID_HEVC &&
-        avctx->profile != FF_PROFILE_HEVC_MAIN && avctx->profile != FF_PROFILE_HEVC_MAIN_10) {
-        av_log(avctx, AV_LOG_VERBOSE, "Unsupported HEVC profile for DXVA HWAccel: %d\n", avctx->profile);
-        return AVERROR(ENOTSUP);
-    }
-
     if (!avctx->hw_frames_ctx && !avctx->hw_device_ctx) {
         av_log(avctx, AV_LOG_ERROR, "Either a hw_frames_ctx or a hw_device_ctx needs to be set for hardware decoding.\n");
         return AVERROR(EINVAL);
@@ -599,7 +677,6 @@ int ff_dxva2_decode_init(AVCodecContext *avctx)
     if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
         AVD3D11VADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
         AVD3D11VAContext *d3d11_ctx = &sctx->ctx.d3d11va;
-        HRESULT hr;
 
         ff_dxva2_lock(avctx);
         ret = d3d11va_create_decoder(avctx);
@@ -666,7 +743,7 @@ int ff_dxva2_decode_uninit(AVCodecContext *avctx)
     return 0;
 }
 
-static void *get_surface(AVCodecContext *avctx, const AVFrame *frame)
+static void *get_surface(const AVCodecContext *avctx, const AVFrame *frame)
 {
 #if CONFIG_D3D11VA
     if (frame->format == AV_PIX_FMT_D3D11) {
@@ -674,7 +751,7 @@ static void *get_surface(AVCodecContext *avctx, const AVFrame *frame)
         intptr_t index = (intptr_t)frame->data[1];
         if (index < 0 || index >= sctx->nb_d3d11_views ||
             sctx->d3d11_texture != (ID3D11Texture2D *)frame->data[0]) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer frame is invalid!\n");
+            av_log((void *)avctx, AV_LOG_ERROR, "get_buffer frame is invalid!\n");
             return NULL;
         }
         return sctx->d3d11_views[index];
@@ -735,7 +812,7 @@ int ff_dxva2_commit_buffer(AVCodecContext *avctx,
 #endif
     if (FAILED(hr)) {
         av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %u: 0x%x\n",
-               type, hr);
+               type, (unsigned)hr);
         return -1;
     }
     if (size <= dxva_size) {
@@ -777,7 +854,7 @@ int ff_dxva2_commit_buffer(AVCodecContext *avctx,
     if (FAILED(hr)) {
         av_log(avctx, AV_LOG_ERROR,
                "Failed to release buffer type %u: 0x%x\n",
-               type, hr);
+               type, (unsigned)hr);
         result = -1;
     }
     return result;
@@ -847,7 +924,7 @@ int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
     } while(1);
 
     if (FAILED(hr)) {
-        av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%x\n", hr);
+        av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%x\n", (unsigned)hr);
         ff_dxva2_unlock(avctx);
         return -1;
     }
@@ -942,7 +1019,7 @@ int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
     }
 #endif
     if (FAILED(hr)) {
-        av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%x\n", hr);
+        av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%x\n", (unsigned)hr);
         result = -1;
     }
 
@@ -957,7 +1034,7 @@ end:
 #endif
     ff_dxva2_unlock(avctx);
     if (FAILED(hr)) {
-        av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%x\n", hr);
+        av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%x\n", (unsigned)hr);
         result = -1;
     }