2 * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
26 #include <gst/video/video.h>
27 #include <gst/d3d11/gstd3d11format.h>
28 #include <gst/d3d11/gstd3d11device.h>
32 #define GST_D3D11_COMMON_FORMATS \
33 "RGBA64_LE, RGB10A2_LE, BGRA, RGBA, BGRx, RGBx, VUYA, NV12, NV21, " \
34 "P010_10LE, P012_LE, P016_LE, I420, YV12, I420_10LE, I420_12LE, " \
35 "Y42B, I422_10LE, I422_12LE, Y444, Y444_10LE, Y444_12LE, Y444_16LE, " \
36 "GRAY8, GRAY16_LE, AYUV, AYUV64"
38 #define GST_D3D11_EXTRA_IN_FORMATS \
41 #define GST_D3D11_SINK_FORMATS \
42 "{ " GST_D3D11_COMMON_FORMATS " ," GST_D3D11_EXTRA_IN_FORMATS " }"
44 #define GST_D3D11_SRC_FORMATS \
45 "{ " GST_D3D11_COMMON_FORMATS " }"
47 #define GST_D3D11_ALL_FORMATS \
48 "{ " GST_D3D11_COMMON_FORMATS " ," GST_D3D11_EXTRA_IN_FORMATS " }"
50 #define GST_TYPE_D3D11_FORMAT_SUPPORT (gst_d3d11_format_support_get_type())
51 GType gst_d3d11_format_support_get_type (void);
53 void gst_d3d11_device_d3d11_debug (GstD3D11Device * device,
55 const gchar * function,
58 void gst_d3d11_device_dxgi_debug (GstD3D11Device * device,
60 const gchar * function,
63 #define GST_D3D11_CLEAR_COM(obj) G_STMT_START { \
71 #define MAKE_FORMAT_MAP_YUV(g,d,r0,r1,r2,r3) \
72 { GST_VIDEO_FORMAT_ ##g, DXGI_FORMAT_ ##d, \
73 { DXGI_FORMAT_ ##r0, DXGI_FORMAT_ ##r1, DXGI_FORMAT_ ##r2, DXGI_FORMAT_ ##r3 }, \
74 { DXGI_FORMAT_ ##r0, DXGI_FORMAT_ ##r1, DXGI_FORMAT_ ##r2, DXGI_FORMAT_ ##r3 }, \
75 (D3D11_FORMAT_SUPPORT) (D3D11_FORMAT_SUPPORT_RENDER_TARGET | D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) }
77 #define MAKE_FORMAT_MAP_YUV_FULL(g,d,r0,r1,r2,r3,f) \
78 { GST_VIDEO_FORMAT_ ##g, DXGI_FORMAT_ ##d, \
79 { DXGI_FORMAT_ ##r0, DXGI_FORMAT_ ##r1, DXGI_FORMAT_ ##r2, DXGI_FORMAT_ ##r3 }, \
80 { DXGI_FORMAT_ ##r0, DXGI_FORMAT_ ##r1, DXGI_FORMAT_ ##r2, DXGI_FORMAT_ ##r3 }, \
81 (D3D11_FORMAT_SUPPORT) (f) }
83 #define MAKE_FORMAT_MAP_RGB(g,d) \
84 { GST_VIDEO_FORMAT_ ##g, DXGI_FORMAT_ ##d, \
85 { DXGI_FORMAT_ ##d, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN }, \
86 { DXGI_FORMAT_ ##d, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN }, \
87 (D3D11_FORMAT_SUPPORT) (D3D11_FORMAT_SUPPORT_RENDER_TARGET | D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) }
89 static const GstD3D11Format _gst_d3d11_default_format_map[] = {
90 MAKE_FORMAT_MAP_RGB (BGRA, B8G8R8A8_UNORM),
91 MAKE_FORMAT_MAP_RGB (RGBA, R8G8B8A8_UNORM),
92 MAKE_FORMAT_MAP_RGB (BGRx, B8G8R8A8_UNORM),
93 MAKE_FORMAT_MAP_RGB (RGBx, R8G8B8A8_UNORM),
94 MAKE_FORMAT_MAP_RGB (RGB10A2_LE, R10G10B10A2_UNORM),
95 MAKE_FORMAT_MAP_RGB (RGBA64_LE, R16G16B16A16_UNORM),
96 MAKE_FORMAT_MAP_YUV (AYUV, UNKNOWN, R8G8B8A8_UNORM, UNKNOWN, UNKNOWN, UNKNOWN),
97 MAKE_FORMAT_MAP_YUV (AYUV64, UNKNOWN, R16G16B16A16_UNORM, UNKNOWN, UNKNOWN, UNKNOWN),
98 MAKE_FORMAT_MAP_YUV (VUYA, AYUV, R8G8B8A8_UNORM, UNKNOWN, UNKNOWN, UNKNOWN),
99 MAKE_FORMAT_MAP_YUV (NV12, NV12, R8_UNORM, R8G8_UNORM, UNKNOWN, UNKNOWN),
100 MAKE_FORMAT_MAP_YUV (NV21, UNKNOWN, R8_UNORM, R8G8_UNORM, UNKNOWN, UNKNOWN),
101 MAKE_FORMAT_MAP_YUV (P010_10LE, P010, R16_UNORM, R16G16_UNORM, UNKNOWN, UNKNOWN),
102 MAKE_FORMAT_MAP_YUV (P012_LE, P016, R16_UNORM, R16G16_UNORM, UNKNOWN, UNKNOWN),
103 MAKE_FORMAT_MAP_YUV (P016_LE, P016, R16_UNORM, R16G16_UNORM, UNKNOWN, UNKNOWN),
104 MAKE_FORMAT_MAP_YUV (I420, UNKNOWN, R8_UNORM, R8_UNORM, R8_UNORM, UNKNOWN),
105 MAKE_FORMAT_MAP_YUV (YV12, UNKNOWN, R8_UNORM, R8_UNORM, R8_UNORM, UNKNOWN),
106 MAKE_FORMAT_MAP_YUV (I420_10LE, UNKNOWN, R16_UNORM, R16_UNORM, R16_UNORM, UNKNOWN),
107 MAKE_FORMAT_MAP_YUV (I420_12LE, UNKNOWN, R16_UNORM, R16_UNORM, R16_UNORM, UNKNOWN),
108 MAKE_FORMAT_MAP_YUV (Y42B, UNKNOWN, R8_UNORM, R8_UNORM, R8_UNORM, UNKNOWN),
109 MAKE_FORMAT_MAP_YUV (I422_10LE, UNKNOWN, R16_UNORM, R16_UNORM, R16_UNORM, UNKNOWN),
110 MAKE_FORMAT_MAP_YUV (I422_12LE, UNKNOWN, R16_UNORM, R16_UNORM, R16_UNORM, UNKNOWN),
111 MAKE_FORMAT_MAP_YUV (Y444, UNKNOWN, R8_UNORM, R8_UNORM, R8_UNORM, UNKNOWN),
112 MAKE_FORMAT_MAP_YUV (Y444_10LE, UNKNOWN, R16_UNORM, R16_UNORM, R16_UNORM, UNKNOWN),
113 MAKE_FORMAT_MAP_YUV (Y444_12LE, UNKNOWN, R16_UNORM, R16_UNORM, R16_UNORM, UNKNOWN),
114 MAKE_FORMAT_MAP_YUV (Y444_16LE, UNKNOWN, R16_UNORM, R16_UNORM, R16_UNORM, UNKNOWN),
116 /* NOTE: To support conversion by using video processor,
117 * mark DXGI_FORMAT_{R8,R16}_UNORM formats as known dxgi_format.
118 * Otherwise, d3d11 elements will not try to use video processor for
120 MAKE_FORMAT_MAP_RGB (GRAY8, R8_UNORM),
121 MAKE_FORMAT_MAP_RGB (GRAY16_LE, R16_UNORM),
122 MAKE_FORMAT_MAP_YUV_FULL (Y410, Y410, R10G10B10A2_UNORM, UNKNOWN, UNKNOWN, UNKNOWN,
123 D3D11_FORMAT_SUPPORT_SHADER_SAMPLE),
124 MAKE_FORMAT_MAP_YUV_FULL (YUY2, YUY2, R8G8B8A8_UNORM, UNKNOWN, UNKNOWN, UNKNOWN,
125 D3D11_FORMAT_SUPPORT_SHADER_SAMPLE),
128 #undef MAKE_FORMAT_MAP_YUV
129 #undef MAKE_FORMAT_MAP_YUV_FULL
130 #undef MAKE_FORMAT_MAP_RGB
132 #define GST_D3D11_N_FORMATS G_N_ELEMENTS(_gst_d3d11_default_format_map)
134 typedef struct _GstD3D11ColorMatrix
136 gdouble matrix[3][3];
140 } GstD3D11ColorMatrix;
143 gchar * gst_d3d11_dump_color_matrix (GstD3D11ColorMatrix * matrix);
146 gboolean gst_d3d11_color_range_adjust_matrix_unorm (const GstVideoInfo * in_info,
147 const GstVideoInfo * out_info,
148 GstD3D11ColorMatrix * matrix);
151 gboolean gst_d3d11_yuv_to_rgb_matrix_unorm (const GstVideoInfo * in_yuv_info,
152 const GstVideoInfo * out_rgb_info,
153 GstD3D11ColorMatrix * matrix);
156 gboolean gst_d3d11_rgb_to_yuv_matrix_unorm (const GstVideoInfo * in_rgb_info,
157 const GstVideoInfo * out_yuv_info,
158 GstD3D11ColorMatrix * matrix);
161 gboolean gst_d3d11_color_primaries_matrix_unorm (const GstVideoColorPrimariesInfo * in_info,
162 const GstVideoColorPrimariesInfo * out_info,
163 GstD3D11ColorMatrix * matrix);
170 class GstD3D11DeviceLockGuard
173 explicit GstD3D11DeviceLockGuard(GstD3D11Device * device) : device_ (device)
175 gst_d3d11_device_lock (device_);
178 ~GstD3D11DeviceLockGuard()
180 gst_d3d11_device_unlock (device_);
183 GstD3D11DeviceLockGuard(const GstD3D11DeviceLockGuard&) = delete;
184 GstD3D11DeviceLockGuard& operator=(const GstD3D11DeviceLockGuard&) = delete;
187 GstD3D11Device *device_;
190 class GstD3D11CSLockGuard
193 explicit GstD3D11CSLockGuard(CRITICAL_SECTION * cs) : cs_ (cs)
195 EnterCriticalSection (cs_);
198 ~GstD3D11CSLockGuard()
200 LeaveCriticalSection (cs_);
203 GstD3D11CSLockGuard(const GstD3D11CSLockGuard&) = delete;
204 GstD3D11CSLockGuard& operator=(const GstD3D11CSLockGuard&) = delete;
207 CRITICAL_SECTION *cs_;
210 class GstD3D11SRWLockGuard
213 explicit GstD3D11SRWLockGuard(SRWLOCK * lock) : lock_ (lock)
215 AcquireSRWLockExclusive (lock_);
218 ~GstD3D11SRWLockGuard()
220 ReleaseSRWLockExclusive (lock_);
223 GstD3D11SRWLockGuard(const GstD3D11SRWLockGuard&) = delete;
224 GstD3D11SRWLockGuard& operator=(const GstD3D11SRWLockGuard&) = delete;
230 #define GST_D3D11_CALL_ONCE_BEGIN \
231 static std::once_flag __once_flag; \
232 std::call_once (__once_flag, [&]()
234 #define GST_D3D11_CALL_ONCE_END )
236 #endif /* __cplusplus */