[FFmpeg-devel] [PATCH v9 1/9] libavutil: add hwcontext_d3d12va and AV_PIX_FMT_D3D12

Jean-Baptiste Kempf jb at videolan.org
Wed Nov 8 13:03:54 EET 2023


Hello,

On Wed, 8 Nov 2023, at 02:05, Tong Wu wrote:
> +/**
> + * @brief This struct is used to sync d3d12 execution
> + *
> + */
> +typedef struct AVD3D12VASyncContext {
> +    /**
> +     * D3D12 fence object
> +     */
> +    ID3D12Fence *fence;
> +
> +    /**
> +     * A handle to the event object
> +     */
> +    HANDLE event;

Sorry, but which event object? I would guess it's the ID3D12Fence::Signal, but I'm not even sure.

I think this needs a bit more doc and explanations.

> +
> +    /**
> +     * The fence value used for sync
> +     */
> +    uint64_t fence_value;
> +} AVD3D12VASyncContext;
> +
> +/**
> + * @brief D3D12VA frame descriptor for pool allocation.
> + *
> + */
> +typedef struct AVD3D12VAFrame {
> +    /**
> +     * The texture in which the frame is located. The reference count 
> is
> +     * managed by the AVBufferRef, and destroying the reference will 
> release
> +     * the interface.
> +     */
> +    ID3D12Resource *texture;
> +
> +    /**
> +     * The index into the array texture element representing the frame
> +     */
> +    intptr_t index;
> +
> +    /**
> +     * The sync context for the texture
> +     *
> +     * Use av_d3d12va_wait_idle(sync_ctx) to ensure the decoding or 
> encoding have been finised
> +     * @see: 
> https://learn.microsoft.com/en-us/windows/win32/medfound/direct3d-12-video-overview#directx-12-fences
> +     */
> +    AVD3D12VASyncContext *sync_ctx;
> +} AVD3D12VAFrame;
> +
> +/**
> + * @brief This struct is allocated as AVHWFramesContext.hwctx
> + *
> + */
> +typedef struct AVD3D12VAFramesContext {
> +    /**
> +     * This field is not able to be user-allocated at the present.
> +     */
> +    AVD3D12VAFrame *texture_infos;
> +} AVD3D12VAFramesContext;
> +
> +/**
> + * @brief Map sw pixel format to d3d12 format
> + *
> + * @return d3d12 specified format
> + */
> +DXGI_FORMAT av_d3d12va_map_sw_to_hw_format(enum AVPixelFormat pix_fmt);
> +
> +/**
> + * @brief Allocate an AVD3D12VASyncContext
> + *
> + * @return Error code (ret < 0 if failed)
> + */
> +int av_d3d12va_sync_context_alloc(AVD3D12VADeviceContext *ctx, 
> AVD3D12VASyncContext **sync_ctx);
> +
> +/**
> + * @brief Free an AVD3D12VASyncContext
> + */
> +void av_d3d12va_sync_context_free(AVD3D12VASyncContext **sync_ctx);
> +
> +#endif /* AVUTIL_HWCONTEXT_D3D12VA_H */
> diff --git a/libavutil/hwcontext_d3d12va_internal.h 
> b/libavutil/hwcontext_d3d12va_internal.h
> new file mode 100644
> index 0000000000..bfd89b3545
> --- /dev/null
> +++ b/libavutil/hwcontext_d3d12va_internal.h
> @@ -0,0 +1,59 @@
> +/*
> + * Direct3D 12 HW acceleration.
> + *
> + * copyright (c) 2022-2023 Wu Jianhua <toqsxw at outlook.com>
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
> 02110-1301 USA
> + */
> +
> +#ifndef AVUTIL_HWCONTEXT_D3D12VA_INTERNAL_H
> +#define AVUTIL_HWCONTEXT_D3D12VA_INTERNAL_H
> +
> +/**
> + * @def COBJMACROS
> + *
> + * @brief Enable C style interface for D3D12
> + */
> +#ifndef COBJMACROS
> +#define COBJMACROS
> +#endif
> +
> +/**
> + * @def DX_CHECK
> + *
> + * @brief A check macro used by D3D12 functions highly frequently
> + */
> +#define DX_CHECK(hr)                              \
> +    do {                                          \
> +        if (FAILED(hr))                           \
> +            goto fail;                            \
> +    } while (0)
> +
> +/**
> + * @def D3D12_OBJECT_RELEASE
> + *
> + * @brief A release macro used by D3D12 objects highly frequently
> + */
> +#define D3D12_OBJECT_RELEASE(pInterface)              \
> +    do {                                              \
> +        if (pInterface) {                             \
> +            IUnknown_Release((IUnknown *)pInterface); \
> +            pInterface = NULL;                        \
> +        }                                             \
> +    } while (0)
> +
> +#endif /* AVUTIL_HWCONTEXT_D3D12VA_INTERNAL_H */
> \ No newline at end of file
> diff --git a/libavutil/hwcontext_internal.h 
> b/libavutil/hwcontext_internal.h
> index e6266494ac..4df516ee6a 100644
> --- a/libavutil/hwcontext_internal.h
> +++ b/libavutil/hwcontext_internal.h
> @@ -165,6 +165,7 @@ int ff_hwframe_map_replace(AVFrame *dst, const 
> AVFrame *src);
> 
>  extern const HWContextType ff_hwcontext_type_cuda;
>  extern const HWContextType ff_hwcontext_type_d3d11va;
> +extern const HWContextType ff_hwcontext_type_d3d12va;
>  extern const HWContextType ff_hwcontext_type_drm;
>  extern const HWContextType ff_hwcontext_type_dxva2;
>  extern const HWContextType ff_hwcontext_type_opencl;
> diff --git a/libavutil/pixdesc.c b/libavutil/pixdesc.c
> index 4e4a63e287..0db4167934 100644
> --- a/libavutil/pixdesc.c
> +++ b/libavutil/pixdesc.c
> @@ -2311,6 +2311,10 @@ static const AVPixFmtDescriptor 
> av_pix_fmt_descriptors[AV_PIX_FMT_NB] = {
>          .name = "d3d11",
>          .flags = AV_PIX_FMT_FLAG_HWACCEL,
>      },
> +    [AV_PIX_FMT_D3D12] = {
> +        .name = "d3d12",
> +        .flags = AV_PIX_FMT_FLAG_HWACCEL,
> +    },
>      [AV_PIX_FMT_GBRPF32BE] = {
>          .name = "gbrpf32be",
>          .nb_components = 3,
> diff --git a/libavutil/pixfmt.h b/libavutil/pixfmt.h
> index a26c72d56b..58f9ad28bd 100644
> --- a/libavutil/pixfmt.h
> +++ b/libavutil/pixfmt.h
> @@ -429,6 +429,13 @@ enum AVPixelFormat {
>      AV_PIX_FMT_GBRAP14BE,  ///< planar GBR 4:4:4:4 56bpp, big-endian
>      AV_PIX_FMT_GBRAP14LE,  ///< planar GBR 4:4:4:4 56bpp, little-endian
> 
> +    /**
> +     * Hardware surfaces for Direct3D 12.
> +     *
> +     * data[0] points to an AVD3D12VAFrame
> +     */
> +    AV_PIX_FMT_D3D12,
> +
>      AV_PIX_FMT_NB         ///< number of pixel formats, DO NOT USE 
> THIS if you want to link with shared libav* because the number of 
> formats might differ between versions
>  };
> 
> diff --git a/libavutil/tests/hwdevice.c b/libavutil/tests/hwdevice.c
> index c57586613a..9d7964f9ee 100644
> --- a/libavutil/tests/hwdevice.c
> +++ b/libavutil/tests/hwdevice.c
> @@ -137,6 +137,8 @@ static const struct {
>        { "0", "1", "2" } },
>      { AV_HWDEVICE_TYPE_D3D11VA,
>        { "0", "1", "2" } },
> +    { AV_HWDEVICE_TYPE_D3D12VA,
> +      { "0", "1", "2" } },
>      { AV_HWDEVICE_TYPE_OPENCL,
>        { "0.0", "0.1", "1.0", "1.1" } },
>      { AV_HWDEVICE_TYPE_VAAPI,
> diff --git a/libavutil/version.h b/libavutil/version.h
> index 589a42b0fa..c5fa7c3692 100644
> --- a/libavutil/version.h
> +++ b/libavutil/version.h
> @@ -79,7 +79,7 @@
>   */
> 
>  #define LIBAVUTIL_VERSION_MAJOR  58
> -#define LIBAVUTIL_VERSION_MINOR  31
> +#define LIBAVUTIL_VERSION_MINOR  32
>  #define LIBAVUTIL_VERSION_MICRO 100
> 
>  #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
> -- 
> 2.41.0.windows.1
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe".

-- 
Jean-Baptiste Kempf -  President
+33 672 704 734


More information about the ffmpeg-devel mailing list