xref: /aosp_15_r20/external/mesa3d/src/util/format/u_format_yuv.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20  * USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  **************************************************************************/
27 
28 
29 /**
30  * @file
31  * YUV colorspace conversion.
32  *
33  * @author Brian Paul <[email protected]>
34  * @author Michal Krol <[email protected]>
35  * @author Jose Fonseca <[email protected]>
36  *
37  * See also:
38  * - http://www.fourcc.org/fccyvrgb.php
39  * - http://msdn.microsoft.com/en-us/library/ms893078
40  * - http://en.wikipedia.org/wiki/YUV
41  */
42 
43 
44 #ifndef U_FORMAT_YUV_H_
45 #define U_FORMAT_YUV_H_
46 
47 
48 #include "util/compiler.h"
49 #include "util/u_math.h"
50 
51 #include "c99_compat.h"
52 
53 /*
54  * TODO: Ensure we use consistent and right floating formulas, with enough
55  * precision in the coefficients.
56  */
57 
58 static inline void
util_format_rgb_float_to_yuv(float r,float g,float b,uint8_t * y,uint8_t * u,uint8_t * v)59 util_format_rgb_float_to_yuv(float r, float g, float b,
60                              uint8_t *y, uint8_t *u, uint8_t *v)
61 {
62    const float _r = SATURATE(r);
63    const float _g = SATURATE(g);
64    const float _b = SATURATE(b);
65 
66    const float scale = 255.0f;
67 
68    const int _y = scale * ( (0.257f * _r) + (0.504f * _g) + (0.098f * _b));
69    const int _u = scale * (-(0.148f * _r) - (0.291f * _g) + (0.439f * _b));
70    const int _v = scale * ( (0.439f * _r) - (0.368f * _g) - (0.071f * _b));
71 
72    *y = _y + 16;
73    *u = _u + 128;
74    *v = _v + 128;
75 }
76 
77 
78 static inline void
util_format_yuv_to_rgb_float(uint8_t y,uint8_t u,uint8_t v,float * r,float * g,float * b)79 util_format_yuv_to_rgb_float(uint8_t y, uint8_t u, uint8_t v,
80                              float *r, float *g, float *b)
81 {
82    const int _y = y - 16;
83    const int _u = u - 128;
84    const int _v = v - 128;
85 
86    const float y_factor = 255.0f / 219.0f;
87 
88    const float scale = 1.0f / 255.0f;
89 
90    *r = scale * (y_factor * _y               + 1.596f * _v);
91    *g = scale * (y_factor * _y - 0.391f * _u - 0.813f * _v);
92    *b = scale * (y_factor * _y + 2.018f * _u              );
93 }
94 
95 
96 static inline void
util_format_rgb_8unorm_to_yuv(uint8_t r,uint8_t g,uint8_t b,uint8_t * y,uint8_t * u,uint8_t * v)97 util_format_rgb_8unorm_to_yuv(uint8_t r, uint8_t g, uint8_t b,
98                 	      uint8_t *y, uint8_t *u, uint8_t *v)
99 {
100    *y = ((  66 * r + 129 * g +  25 * b + 128) >> 8) +  16;
101    *u = (( -38 * r -  74 * g + 112 * b + 128) >> 8) + 128;
102    *v = (( 112 * r -  94 * g -  18 * b + 128) >> 8) + 128;
103 }
104 
105 
106 static inline void
util_format_yuv_to_rgb_8unorm(uint8_t y,uint8_t u,uint8_t v,uint8_t * r,uint8_t * g,uint8_t * b)107 util_format_yuv_to_rgb_8unorm(uint8_t y, uint8_t u, uint8_t v,
108                               uint8_t *r, uint8_t *g, uint8_t *b)
109 {
110    const int _y = y - 16;
111    const int _u = u - 128;
112    const int _v = v - 128;
113 
114    const int _r = (298 * _y            + 409 * _v + 128) >> 8;
115    const int _g = (298 * _y - 100 * _u - 208 * _v + 128) >> 8;
116    const int _b = (298 * _y + 516 * _u            + 128) >> 8;
117 
118    *r = CLAMP(_r, 0, 255);
119    *g = CLAMP(_g, 0, 255);
120    *b = CLAMP(_b, 0, 255);
121 }
122 
123 
124 
125 void
126 util_format_uyvy_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,
127                               const uint8_t *restrict src_row, unsigned src_stride,
128                               unsigned width, unsigned height);
129 
130 void
131 util_format_uyvy_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
132                                const uint8_t *restrict src_row, unsigned src_stride,
133                                unsigned width, unsigned height);
134 
135 void
136 util_format_uyvy_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,
137                             const float *restrict src_row, unsigned src_stride,
138                             unsigned width, unsigned height);
139 
140 void
141 util_format_uyvy_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
142                              const uint8_t *restrict src_row, unsigned src_stride,
143                              unsigned width, unsigned height);
144 
145 void
146 util_format_uyvy_fetch_rgba(void *restrict dst, const uint8_t *restrict src,
147                              unsigned i, unsigned j);
148 
149 void
150 util_format_vyuy_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,
151                               const uint8_t *restrict src_row, unsigned src_stride,
152                               unsigned width, unsigned height);
153 
154 void
155 util_format_vyuy_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
156                                const uint8_t *restrict src_row, unsigned src_stride,
157                                unsigned width, unsigned height);
158 
159 void
160 util_format_vyuy_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,
161                             const float *restrict src_row, unsigned src_stride,
162                             unsigned width, unsigned height);
163 
164 void
165 util_format_vyuy_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
166                              const uint8_t *restrict src_row, unsigned src_stride,
167                              unsigned width, unsigned height);
168 
169 void
170 util_format_vyuy_fetch_rgba(void *restrict dst, const uint8_t *restrict src,
171                              unsigned i, unsigned j);
172 
173 void
174 util_format_yuyv_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,
175                               const uint8_t *restrict src_row, unsigned src_stride,
176                               unsigned width, unsigned height);
177 
178 void
179 util_format_yuyv_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
180                                const uint8_t *restrict src_row, unsigned src_stride,
181                                unsigned width, unsigned height);
182 
183 void
184 util_format_yuyv_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,
185                             const float *restrict src_row, unsigned src_stride,
186                             unsigned width, unsigned height);
187 
188 void
189 util_format_yuyv_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
190                              const uint8_t *restrict src_row, unsigned src_stride,
191                              unsigned width, unsigned height);
192 
193 void
194 util_format_yuyv_fetch_rgba(void *restrict dst, const uint8_t *restrict src,
195                              unsigned i, unsigned j);
196 
197 void
198 util_format_yvyu_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,
199                               const uint8_t *restrict src_row, unsigned src_stride,
200                               unsigned width, unsigned height);
201 
202 void
203 util_format_yvyu_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
204                                const uint8_t *restrict src_row, unsigned src_stride,
205                                unsigned width, unsigned height);
206 
207 void
208 util_format_yvyu_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,
209                             const float *restrict src_row, unsigned src_stride,
210                             unsigned width, unsigned height);
211 
212 void
213 util_format_yvyu_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
214                              const uint8_t *restrict src_row, unsigned src_stride,
215                              unsigned width, unsigned height);
216 
217 void
218 util_format_yvyu_fetch_rgba(void *restrict dst, const uint8_t *restrict src,
219                              unsigned i, unsigned j);
220 
221 void
222 util_format_r8g8_b8g8_unorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,
223                                          const uint8_t *restrict src_row, unsigned src_stride,
224                                          unsigned width, unsigned height);
225 
226 void
227 util_format_r8g8_b8g8_unorm_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
228                                           const uint8_t *restrict src_row, unsigned src_stride,
229                                           unsigned width, unsigned height);
230 
231 void
232 util_format_r8g8_b8g8_unorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,
233                                        const float *restrict src_row, unsigned src_stride,
234                                        unsigned width, unsigned height);
235 
236 void
237 util_format_r8g8_b8g8_unorm_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
238                                         const uint8_t *restrict src_row, unsigned src_stride,
239                                         unsigned width, unsigned height);
240 
241 void
242 util_format_r8g8_b8g8_unorm_fetch_rgba(void *restrict dst, const uint8_t *restrict src,
243                                         unsigned i, unsigned j);
244 
245 void
246 util_format_g8r8_g8b8_unorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,
247                                          const uint8_t *restrict src_row, unsigned src_stride,
248                                          unsigned width, unsigned height);
249 
250 void
251 util_format_g8r8_g8b8_unorm_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
252                                           const uint8_t *restrict src_row, unsigned src_stride,
253                                           unsigned width, unsigned height);
254 
255 void
256 util_format_g8r8_g8b8_unorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,
257                                        const float *restrict src_row, unsigned src_stride,
258                                        unsigned width, unsigned height);
259 
260 void
261 util_format_g8r8_g8b8_unorm_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,
262                                         const uint8_t *restrict src_row, unsigned src_stride,
263                                         unsigned width, unsigned height);
264 
265 void
266 util_format_g8r8_g8b8_unorm_fetch_rgba(void *restrict dst, const uint8_t *restrict src,
267                                         unsigned i, unsigned j);
268 
269 #endif /* U_FORMAT_YUV_H_ */
270