8529e824f3d546c60eb4d1a6b8c05094e96d680b
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst-libs / gst / d3d11 / gstd3d11-private.h
1 /* GStreamer
2  * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #pragma once
21
22 #include <d3d11_4.h>
23 #include <dxgi1_6.h>
24
25 #include <gst/gst.h>
26 #include <gst/video/video.h>
27 #include <gst/d3d11/gstd3d11format.h>
28 #include <gst/d3d11/gstd3d11device.h>
29
30 G_BEGIN_DECLS
31
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"
37
38 #define GST_D3D11_EXTRA_IN_FORMATS \
39     "Y410, YUY2"
40
41 #define GST_D3D11_SINK_FORMATS \
42     "{ " GST_D3D11_COMMON_FORMATS " ," GST_D3D11_EXTRA_IN_FORMATS " }"
43
44 #define GST_D3D11_SRC_FORMATS \
45     "{ " GST_D3D11_COMMON_FORMATS " }"
46
47 #define GST_D3D11_ALL_FORMATS \
48     "{ " GST_D3D11_COMMON_FORMATS " ," GST_D3D11_EXTRA_IN_FORMATS " }"
49
50 #define GST_TYPE_D3D11_FORMAT_SUPPORT (gst_d3d11_format_support_get_type())
51 GType gst_d3d11_format_support_get_type (void);
52
53 void  gst_d3d11_device_d3d11_debug (GstD3D11Device * device,
54                                     const gchar * file,
55                                     const gchar * function,
56                                     gint line);
57
58 void  gst_d3d11_device_dxgi_debug  (GstD3D11Device * device,
59                                     const gchar * file,
60                                     const gchar * function,
61                                     gint line);
62
63 #define GST_D3D11_CLEAR_COM(obj) G_STMT_START { \
64     if (obj) { \
65       (obj)->Release (); \
66       (obj) = NULL; \
67     } \
68   } G_STMT_END
69
70
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) }
76
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) }
82
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) }
88
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),
115   /* GRAY */
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
119    * those formats */
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),
126 };
127
128 #undef MAKE_FORMAT_MAP_YUV
129 #undef MAKE_FORMAT_MAP_YUV_FULL
130 #undef MAKE_FORMAT_MAP_RGB
131
132 #define GST_D3D11_N_FORMATS G_N_ELEMENTS(_gst_d3d11_default_format_map)
133
134 typedef struct _GstD3D11ColorMatrix
135 {
136   gdouble matrix[3][3];
137   gdouble offset[3];
138   gdouble min[3];
139   gdouble max[3];
140 } GstD3D11ColorMatrix;
141
142 GST_D3D11_API
143 gchar *         gst_d3d11_dump_color_matrix (GstD3D11ColorMatrix * matrix);
144
145 GST_D3D11_API
146 gboolean        gst_d3d11_color_range_adjust_matrix_unorm (const GstVideoInfo * in_info,
147                                                            const GstVideoInfo * out_info,
148                                                            GstD3D11ColorMatrix * matrix);
149
150 GST_D3D11_API
151 gboolean        gst_d3d11_yuv_to_rgb_matrix_unorm (const GstVideoInfo * in_yuv_info,
152                                                    const GstVideoInfo * out_rgb_info,
153                                                    GstD3D11ColorMatrix * matrix);
154
155 GST_D3D11_API
156 gboolean        gst_d3d11_rgb_to_yuv_matrix_unorm (const GstVideoInfo * in_rgb_info,
157                                                    const GstVideoInfo * out_yuv_info,
158                                                    GstD3D11ColorMatrix * matrix);
159
160 GST_D3D11_API
161 gboolean        gst_d3d11_color_primaries_matrix_unorm (const GstVideoColorPrimariesInfo * in_info,
162                                                         const GstVideoColorPrimariesInfo * out_info,
163                                                         GstD3D11ColorMatrix * matrix);
164
165 G_END_DECLS
166
167 #ifdef __cplusplus
168 #include <mutex>
169
170 class GstD3D11DeviceLockGuard
171 {
172 public:
173   explicit GstD3D11DeviceLockGuard(GstD3D11Device * device) : device_ (device)
174   {
175     gst_d3d11_device_lock (device_);
176   }
177
178   ~GstD3D11DeviceLockGuard()
179   {
180     gst_d3d11_device_unlock (device_);
181   }
182
183   GstD3D11DeviceLockGuard(const GstD3D11DeviceLockGuard&) = delete;
184   GstD3D11DeviceLockGuard& operator=(const GstD3D11DeviceLockGuard&) = delete;
185
186 private:
187   GstD3D11Device *device_;
188 };
189
190 class GstD3D11CSLockGuard
191 {
192 public:
193   explicit GstD3D11CSLockGuard(CRITICAL_SECTION * cs) : cs_ (cs)
194   {
195     EnterCriticalSection (cs_);
196   }
197
198   ~GstD3D11CSLockGuard()
199   {
200     LeaveCriticalSection (cs_);
201   }
202
203   GstD3D11CSLockGuard(const GstD3D11CSLockGuard&) = delete;
204   GstD3D11CSLockGuard& operator=(const GstD3D11CSLockGuard&) = delete;
205
206 private:
207   CRITICAL_SECTION *cs_;
208 };
209
210 class GstD3D11SRWLockGuard
211 {
212 public:
213   explicit GstD3D11SRWLockGuard(SRWLOCK * lock) : lock_ (lock)
214   {
215     AcquireSRWLockExclusive (lock_);
216   }
217
218   ~GstD3D11SRWLockGuard()
219   {
220     ReleaseSRWLockExclusive (lock_);
221   }
222
223   GstD3D11SRWLockGuard(const GstD3D11SRWLockGuard&) = delete;
224   GstD3D11SRWLockGuard& operator=(const GstD3D11SRWLockGuard&) = delete;
225
226 private:
227   SRWLOCK *lock_;
228 };
229
230 #define GST_D3D11_CALL_ONCE_BEGIN \
231     static std::once_flag __once_flag; \
232     std::call_once (__once_flag, [&]()
233
234 #define GST_D3D11_CALL_ONCE_END )
235
236 #endif /* __cplusplus */