d3d11: Update build-time dependency
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst-libs / gst / d3d11 / gstd3d11format.cpp
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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "gstd3d11format.h"
25 #include "gstd3d11utils.h"
26 #include "gstd3d11device.h"
27 #include "gstd3d11memory.h"
28 #include "gstd3d11_private.h"
29
30 #include <string.h>
31
32 #ifndef GST_DISABLE_GST_DEBUG
33 #define GST_CAT_DEFAULT ensure_debug_category()
34 static GstDebugCategory *
35 ensure_debug_category (void)
36 {
37   static gsize cat_gonce = 0;
38
39   if (g_once_init_enter (&cat_gonce)) {
40     gsize cat_done;
41
42     cat_done = (gsize) _gst_debug_category_new ("d3d11format", 0,
43         "d3d11 specific formats");
44
45     g_once_init_leave (&cat_gonce, cat_done);
46   }
47
48   return (GstDebugCategory *) cat_gonce;
49 }
50 #else
51 #define ensure_debug_category() /* NOOP */
52 #endif /* GST_DISABLE_GST_DEBUG */
53
54 /**
55  * gst_d3d11_dxgi_format_n_planes:
56  * @format: a DXGI_FORMAT
57  *
58  * Returns: the number of planes for @format
59  *
60  * Since: 1.20
61  */
62 guint
63 gst_d3d11_dxgi_format_n_planes (DXGI_FORMAT format)
64 {
65   switch (format) {
66     case DXGI_FORMAT_B8G8R8A8_UNORM:
67     case DXGI_FORMAT_R8G8B8A8_UNORM:
68     case DXGI_FORMAT_R10G10B10A2_UNORM:
69     case DXGI_FORMAT_AYUV:
70     case DXGI_FORMAT_YUY2:
71     case DXGI_FORMAT_R8_UNORM:
72     case DXGI_FORMAT_R8G8_UNORM:
73     case DXGI_FORMAT_R16_UNORM:
74     case DXGI_FORMAT_R16G16_UNORM:
75     case DXGI_FORMAT_G8R8_G8B8_UNORM:
76     case DXGI_FORMAT_R8G8_B8G8_UNORM:
77     case DXGI_FORMAT_Y210:
78     case DXGI_FORMAT_Y410:
79     case DXGI_FORMAT_R16G16B16A16_UNORM:
80       return 1;
81     case DXGI_FORMAT_NV12:
82     case DXGI_FORMAT_P010:
83     case DXGI_FORMAT_P016:
84       return 2;
85     default:
86       break;
87   }
88
89   return 0;
90 }
91
92 /**
93  * gst_d3d11_dxgi_format_get_size:
94  * @format: a DXGI_FORMAT
95  * @width: a texture width
96  * @height: a texture height
97  * @pitch: a pitch of texture
98  * @offset: offset for each plane
99  * @stride: stride for each plane
100  * @size: (out): required memory size for given format
101  *
102  * Calculate required memory size and per plane stride with
103  * based on information
104  *
105  * Returns: %TRUE if @size can be calculated with given information
106  *
107  * Since: 1.20
108  */
109 gboolean
110 gst_d3d11_dxgi_format_get_size (DXGI_FORMAT format, guint width, guint height,
111     guint pitch, gsize offset[GST_VIDEO_MAX_PLANES],
112     gint stride[GST_VIDEO_MAX_PLANES], gsize * size)
113 {
114   g_return_val_if_fail (format != DXGI_FORMAT_UNKNOWN, FALSE);
115
116   switch (format) {
117     case DXGI_FORMAT_B8G8R8A8_UNORM:
118     case DXGI_FORMAT_R8G8B8A8_UNORM:
119     case DXGI_FORMAT_R10G10B10A2_UNORM:
120     case DXGI_FORMAT_AYUV:
121     case DXGI_FORMAT_YUY2:
122     case DXGI_FORMAT_R8_UNORM:
123     case DXGI_FORMAT_R8G8_UNORM:
124     case DXGI_FORMAT_R16_UNORM:
125     case DXGI_FORMAT_R16G16_UNORM:
126     case DXGI_FORMAT_G8R8_G8B8_UNORM:
127     case DXGI_FORMAT_R8G8_B8G8_UNORM:
128     case DXGI_FORMAT_Y210:
129     case DXGI_FORMAT_Y410:
130     case DXGI_FORMAT_R16G16B16A16_UNORM:
131       offset[0] = 0;
132       stride[0] = pitch;
133       *size = pitch * height;
134       break;
135     case DXGI_FORMAT_NV12:
136     case DXGI_FORMAT_P010:
137     case DXGI_FORMAT_P016:
138       offset[0] = 0;
139       stride[0] = pitch;
140       offset[1] = offset[0] + stride[0] * height;
141       stride[1] = pitch;
142       *size = offset[1] + stride[1] * GST_ROUND_UP_2 (height / 2);
143       break;
144     default:
145       return FALSE;
146   }
147
148   GST_LOG ("Calculated buffer size: %" G_GSIZE_FORMAT
149       " (dxgi format:%d, %dx%d, Pitch %d)",
150       *size, format, width, height, pitch);
151
152   return TRUE;
153 }
154
155 /**
156  * gst_d3d11_dxgi_format_to_gst:
157  * @format: a DXGI_FORMAT
158  *
159  * Converts the @format to its #GstVideoFormat representation.
160  *
161  * Returns: a #GstVideoFormat equivalent to @format
162  *
163  * Since: 1.20
164  */
165 GstVideoFormat
166 gst_d3d11_dxgi_format_to_gst (DXGI_FORMAT format)
167 {
168   switch (format) {
169     case DXGI_FORMAT_B8G8R8A8_UNORM:
170       return GST_VIDEO_FORMAT_BGRA;
171     case DXGI_FORMAT_R8G8B8A8_UNORM:
172       return GST_VIDEO_FORMAT_RGBA;
173     case DXGI_FORMAT_R10G10B10A2_UNORM:
174       return GST_VIDEO_FORMAT_RGB10A2_LE;
175     case DXGI_FORMAT_AYUV:
176       return GST_VIDEO_FORMAT_VUYA;
177     case DXGI_FORMAT_YUY2:
178       return GST_VIDEO_FORMAT_YUY2;
179     case DXGI_FORMAT_Y210:
180       return GST_VIDEO_FORMAT_Y210;
181     case DXGI_FORMAT_Y410:
182       return GST_VIDEO_FORMAT_Y410;
183     case DXGI_FORMAT_NV12:
184       return GST_VIDEO_FORMAT_NV12;
185     case DXGI_FORMAT_P010:
186       return GST_VIDEO_FORMAT_P010_10LE;
187     case DXGI_FORMAT_P016:
188       return GST_VIDEO_FORMAT_P016_LE;
189     default:
190       break;
191   }
192
193   return GST_VIDEO_FORMAT_UNKNOWN;
194 }