lavc/aacsbr: fix make checkheaders warning
[ffmpeg.git] / libavcodec / dxva2.c
index efb5328..3241611 100644 (file)
@@ -29,6 +29,7 @@
 #include "libavutil/time.h"
 
 #include "avcodec.h"
+#include "decode.h"
 #include "dxva2_internal.h"
 
 /* define all the GUIDs used directly here,
@@ -43,6 +44,7 @@ DEFINE_GUID(ff_DXVA2_ModeVC1_D2010,      0x1b81beA4, 0xa0c7,0x11d3,0xb9,0x84,0x0
 DEFINE_GUID(ff_DXVA2_ModeHEVC_VLD_Main,  0x5b11d51b, 0x2f4c,0x4452,0xbc,0xc3,0x09,0xf2,0xa1,0x16,0x0c,0xc0);
 DEFINE_GUID(ff_DXVA2_ModeHEVC_VLD_Main10,0x107af0e0, 0xef1a,0x4d19,0xab,0xa8,0x67,0xa1,0x63,0x07,0x3d,0x13);
 DEFINE_GUID(ff_DXVA2_ModeVP9_VLD_Profile0,0x463707f8,0xa1d0,0x4585,0x87,0x6d,0x83,0xaa,0x6d,0x60,0xb8,0x9e);
+DEFINE_GUID(ff_DXVA2_ModeVP9_VLD_10bit_Profile2,0xa4c749ef,0x6ecf,0x48aa,0x84,0x48,0x50,0xa7,0xa1,0x16,0x5f,0xf7);
 DEFINE_GUID(ff_DXVA2_NoEncrypt,          0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
 DEFINE_GUID(ff_GUID_NULL,                0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
 DEFINE_GUID(ff_IID_IDirectXVideoDecoderService, 0xfc51a551,0xd5e7,0x11d9,0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
@@ -50,18 +52,37 @@ 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 int prof_vp9_profile0[] = {FF_PROFILE_VP9_0,
+                                        FF_PROFILE_UNKNOWN};
+static const int prof_vp9_profile2[] = {FF_PROFILE_VP9_2,
+                                        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 },
@@ -70,11 +91,12 @@ 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 },
 
     /* VP8/9 */
-    { &ff_DXVA2_ModeVP9_VLD_Profile0,AV_CODEC_ID_VP9 },
+    { &ff_DXVA2_ModeVP9_VLD_Profile0,       AV_CODEC_ID_VP9, prof_vp9_profile0 },
+    { &ff_DXVA2_ModeVP9_VLD_10bit_Profile2, AV_CODEC_ID_VP9, prof_vp9_profile2 },
 
     { NULL,                          0 },
 };
@@ -164,17 +186,84 @@ static int dxva2_validate_output(void *decoder_service, GUID guid, const void *s
 }
 #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++) {
@@ -327,7 +416,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;
@@ -358,19 +446,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;
@@ -486,14 +583,20 @@ static void ff_dxva2_unlock(AVCodecContext *avctx)
 #endif
 }
 
-// This must work before the decoder is created.
-// This somehow needs to be exported to the user.
-static void dxva_adjust_hwframes(AVCodecContext *avctx, AVHWFramesContext *frames_ctx)
+int ff_dxva2_common_frame_params(AVCodecContext *avctx,
+                                 AVBufferRef *hw_frames_ctx)
 {
-    FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
+    AVHWFramesContext *frames_ctx = (AVHWFramesContext *)hw_frames_ctx->data;
+    AVHWDeviceContext *device_ctx = frames_ctx->device_ctx;
     int surface_alignment, num_surfaces;
 
-    frames_ctx->format = sctx->pix_fmt;
+    if (device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
+        frames_ctx->format = AV_PIX_FMT_DXVA2_VLD;
+    } else if (device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) {
+        frames_ctx->format = AV_PIX_FMT_D3D11;
+    } else {
+        return AVERROR(EINVAL);
+    }
 
     /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
     but it causes issues for H.264 on certain AMD GPUs..... */
@@ -506,8 +609,8 @@ static void dxva_adjust_hwframes(AVCodecContext *avctx, AVHWFramesContext *frame
     else
         surface_alignment = 16;
 
-    /* 4 base work surfaces */
-    num_surfaces = 4;
+    /* 1 base work surface */
+    num_surfaces = 1;
 
     /* add surfaces based on number of possible refs */
     if (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_HEVC)
@@ -517,10 +620,6 @@ static void dxva_adjust_hwframes(AVCodecContext *avctx, AVHWFramesContext *frame
     else
         num_surfaces += 2;
 
-    /* add extra surfaces for frame threading */
-    if (avctx->active_thread_type & FF_THREAD_FRAME)
-        num_surfaces += avctx->thread_count;
-
     frames_ctx->sw_format = avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
                             AV_PIX_FMT_P010 : AV_PIX_FMT_NV12;
     frames_ctx->width = FFALIGN(avctx->coded_width, surface_alignment);
@@ -543,12 +642,16 @@ static void dxva_adjust_hwframes(AVCodecContext *avctx, AVHWFramesContext *frame
         frames_hwctx->BindFlags |= D3D11_BIND_DECODER;
     }
 #endif
+
+    return 0;
 }
 
 int ff_dxva2_decode_init(AVCodecContext *avctx)
 {
     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
-    AVHWFramesContext *frames_ctx = NULL;
+    AVHWFramesContext *frames_ctx;
+    enum AVHWDeviceType dev_type = avctx->hwaccel->pix_fmt == AV_PIX_FMT_DXVA2_VLD
+                            ? AV_HWDEVICE_TYPE_DXVA2 : AV_HWDEVICE_TYPE_D3D11VA;
     int ret = 0;
 
     // Old API.
@@ -558,44 +661,14 @@ 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);
-    }
-
-    if (avctx->hw_frames_ctx) {
-        frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
-    } else {
-        avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
-        if (!avctx->hw_frames_ctx)
-            return AVERROR(ENOMEM);
-
-        frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
-
-        dxva_adjust_hwframes(avctx, frames_ctx);
-
-        ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
-        if (ret < 0)
-            goto fail;
-    }
+    ret = ff_decode_get_hw_frames_ctx(avctx, dev_type);
+    if (ret < 0)
+        return ret;
 
+    frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
     sctx->device_ctx = frames_ctx->device_ctx;
 
-    if (frames_ctx->format != sctx->pix_fmt ||
-        !((sctx->pix_fmt == AV_PIX_FMT_D3D11 && CONFIG_D3D11VA) ||
-          (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD && CONFIG_DXVA2))) {
+    if (frames_ctx->format != sctx->pix_fmt) {
         av_log(avctx, AV_LOG_ERROR, "Invalid pixfmt for hwaccel!\n");
         ret = AVERROR(EINVAL);
         goto fail;
@@ -605,7 +678,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);
@@ -672,7 +744,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) {
@@ -680,7 +752,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];
@@ -741,7 +813,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) {
@@ -783,7 +855,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;
@@ -853,7 +925,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;
     }
@@ -948,7 +1020,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;
     }
 
@@ -963,7 +1035,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;
     }