gitlab-ci: auto-cancel CI runs when a newer commit is pushed to the same branch
[mesa.git] / src / gallium / auxiliary / util / u_format_yuv.h
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 <brianp@vmware.com>
34 * @author Michal Krol <michal@vmware.com>
35 * @author Jose Fonseca <jfonseca@vmware.com>
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 "pipe/p_compiler.h"
49 #include "util/u_math.h"
50
51
52 /*
53 * TODO: Ensure we use consistent and right floating formulas, with enough
54 * precision in the coefficients.
55 */
56
57 static inline void
58 util_format_rgb_float_to_yuv(float r, float g, float b,
59 uint8_t *y, uint8_t *u, uint8_t *v)
60 {
61 const float _r = CLAMP(r, 0.0f, 1.0f);
62 const float _g = CLAMP(g, 0.0f, 1.0f);
63 const float _b = CLAMP(b, 0.0f, 1.0f);
64
65 const float scale = 255.0f;
66
67 const int _y = scale * ( (0.257f * _r) + (0.504f * _g) + (0.098f * _b));
68 const int _u = scale * (-(0.148f * _r) - (0.291f * _g) + (0.439f * _b));
69 const int _v = scale * ( (0.439f * _r) - (0.368f * _g) - (0.071f * _b));
70
71 *y = _y + 16;
72 *u = _u + 128;
73 *v = _v + 128;
74 }
75
76
77 static inline void
78 util_format_yuv_to_rgb_float(uint8_t y, uint8_t u, uint8_t v,
79 float *r, float *g, float *b)
80 {
81 const int _y = y - 16;
82 const int _u = u - 128;
83 const int _v = v - 128;
84
85 const float y_factor = 255.0f / 219.0f;
86
87 const float scale = 1.0f / 255.0f;
88
89 *r = scale * (y_factor * _y + 1.596f * _v);
90 *g = scale * (y_factor * _y - 0.391f * _u - 0.813f * _v);
91 *b = scale * (y_factor * _y + 2.018f * _u );
92 }
93
94
95 static inline void
96 util_format_rgb_8unorm_to_yuv(uint8_t r, uint8_t g, uint8_t b,
97 uint8_t *y, uint8_t *u, uint8_t *v)
98 {
99 *y = (( 66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
100 *u = (( -38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
101 *v = (( 112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
102 }
103
104
105 static inline void
106 util_format_yuv_to_rgb_8unorm(uint8_t y, uint8_t u, uint8_t v,
107 uint8_t *r, uint8_t *g, uint8_t *b)
108 {
109 const int _y = y - 16;
110 const int _u = u - 128;
111 const int _v = v - 128;
112
113 const int _r = (298 * _y + 409 * _v + 128) >> 8;
114 const int _g = (298 * _y - 100 * _u - 208 * _v + 128) >> 8;
115 const int _b = (298 * _y + 516 * _u + 128) >> 8;
116
117 *r = CLAMP(_r, 0, 255);
118 *g = CLAMP(_g, 0, 255);
119 *b = CLAMP(_b, 0, 255);
120 }
121
122
123
124 void
125 util_format_uyvy_unpack_rgba_float(float *dst_row, unsigned dst_stride,
126 const uint8_t *src_row, unsigned src_stride,
127 unsigned width, unsigned height);
128
129 void
130 util_format_uyvy_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
131 const uint8_t *src_row, unsigned src_stride,
132 unsigned width, unsigned height);
133
134 void
135 util_format_uyvy_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
136 const float *src_row, unsigned src_stride,
137 unsigned width, unsigned height);
138
139 void
140 util_format_uyvy_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
141 const uint8_t *src_row, unsigned src_stride,
142 unsigned width, unsigned height);
143
144 void
145 util_format_uyvy_fetch_rgba_float(float *dst, const uint8_t *src,
146 unsigned i, unsigned j);
147
148 void
149 util_format_yuyv_unpack_rgba_float(float *dst_row, unsigned dst_stride,
150 const uint8_t *src_row, unsigned src_stride,
151 unsigned width, unsigned height);
152
153 void
154 util_format_yuyv_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
155 const uint8_t *src_row, unsigned src_stride,
156 unsigned width, unsigned height);
157
158 void
159 util_format_yuyv_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
160 const float *src_row, unsigned src_stride,
161 unsigned width, unsigned height);
162
163 void
164 util_format_yuyv_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
165 const uint8_t *src_row, unsigned src_stride,
166 unsigned width, unsigned height);
167
168 void
169 util_format_yuyv_fetch_rgba_float(float *dst, const uint8_t *src,
170 unsigned i, unsigned j);
171
172 /* XXX: Stubbed for now */
173 void
174 util_format_yv12_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
175 const uint8_t *src_row, unsigned src_stride,
176 unsigned width, unsigned height);
177 void
178 util_format_yv12_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
179 const uint8_t *src_row, unsigned src_stride,
180 unsigned width, unsigned height);
181 void
182 util_format_yv12_unpack_rgba_float(float *dst_row, unsigned dst_stride,
183 const uint8_t *src_row, unsigned src_stride,
184 unsigned width, unsigned height);
185 void
186 util_format_yv12_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
187 const float *src_row, unsigned src_stride,
188 unsigned width, unsigned height);
189 void
190 util_format_yv12_fetch_rgba_float(float *dst, const uint8_t *src,
191 unsigned i, unsigned j);
192 void
193 util_format_yv16_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
194 const uint8_t *src_row, unsigned src_stride,
195 unsigned width, unsigned height);
196 void
197 util_format_yv16_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
198 const uint8_t *src_row, unsigned src_stride,
199 unsigned width, unsigned height);
200 void
201 util_format_yv16_unpack_rgba_float(float *dst_row, unsigned dst_stride,
202 const uint8_t *src_row, unsigned src_stride,
203 unsigned width, unsigned height);
204 void
205 util_format_yv16_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
206 const float *src_row, unsigned src_stride,
207 unsigned width, unsigned height);
208 void
209 util_format_yv16_fetch_rgba_float(float *dst, const uint8_t *src,
210 unsigned i, unsigned j);
211 void
212 util_format_iyuv_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
213 const uint8_t *src_row, unsigned src_stride,
214 unsigned width, unsigned height);
215 void
216 util_format_iyuv_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
217 const uint8_t *src_row, unsigned src_stride,
218 unsigned width, unsigned height);
219 void
220 util_format_iyuv_unpack_rgba_float(float *dst_row, unsigned dst_stride,
221 const uint8_t *src_row, unsigned src_stride,
222 unsigned width, unsigned height);
223 void
224 util_format_iyuv_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
225 const float *src_row, unsigned src_stride,
226 unsigned width, unsigned height);
227 void
228 util_format_iyuv_fetch_rgba_float(float *dst, const uint8_t *src,
229 unsigned i, unsigned j);
230 void
231 util_format_nv12_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
232 const uint8_t *src_row, unsigned src_stride,
233 unsigned width, unsigned height);
234 void
235 util_format_nv12_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
236 const uint8_t *src_row, unsigned src_stride,
237 unsigned width, unsigned height);
238 void
239 util_format_nv12_unpack_rgba_float(float *dst_row, unsigned dst_stride,
240 const uint8_t *src_row, unsigned src_stride,
241 unsigned width, unsigned height);
242 void
243 util_format_nv12_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
244 const float *src_row, unsigned src_stride,
245 unsigned width, unsigned height);
246 void
247 util_format_nv12_fetch_rgba_float(float *dst, const uint8_t *src,
248 unsigned i, unsigned j);
249 void
250 util_format_nv21_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
251 const uint8_t *src_row, unsigned src_stride,
252 unsigned width, unsigned height);
253 void
254 util_format_nv21_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
255 const uint8_t *src_row, unsigned src_stride,
256 unsigned width, unsigned height);
257 void
258 util_format_nv21_unpack_rgba_float(float *dst_row, unsigned dst_stride,
259 const uint8_t *src_row, unsigned src_stride,
260 unsigned width, unsigned height);
261 void
262 util_format_nv21_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
263 const float *src_row, unsigned src_stride,
264 unsigned width, unsigned height);
265 void
266 util_format_nv21_fetch_rgba_float(float *dst, const uint8_t *src,
267 unsigned i, unsigned j);
268 void
269 util_format_p016_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
270 const uint8_t *src_row, unsigned src_stride,
271 unsigned width, unsigned height);
272 void
273 util_format_p016_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
274 const uint8_t *src_row, unsigned src_stride,
275 unsigned width, unsigned height);
276 void
277 util_format_p016_unpack_rgba_float(float *dst_row, unsigned dst_stride,
278 const uint8_t *src_row, unsigned src_stride,
279 unsigned width, unsigned height);
280 void
281 util_format_p016_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
282 const float *src_row, unsigned src_stride,
283 unsigned width, unsigned height);
284 void
285 util_format_p016_fetch_rgba_float(float *dst, const uint8_t *src,
286 unsigned i, unsigned j);
287
288 void
289 util_format_xyuv_unpack_rgba_float(float *dst_row, unsigned dst_stride,
290 const uint8_t *src_row, unsigned src_stride,
291 unsigned width, unsigned height);
292
293 void
294 util_format_xyuv_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
295 const uint8_t *src_row, unsigned src_stride,
296 unsigned width, unsigned height);
297
298 void
299 util_format_xyuv_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
300 const float *src_row, unsigned src_stride,
301 unsigned width, unsigned height);
302
303 void
304 util_format_xyuv_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
305 const uint8_t *src_row, unsigned src_stride,
306 unsigned width, unsigned height);
307
308 void
309 util_format_xyuv_fetch_rgba_float(float *dst, const uint8_t *src,
310 unsigned i, unsigned j);
311 void
312 util_format_ayuv_unpack_rgba_float(float *dst_row, unsigned dst_stride,
313 const uint8_t *src_row, unsigned src_stride,
314 unsigned width, unsigned height);
315
316 void
317 util_format_ayuv_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
318 const uint8_t *src_row, unsigned src_stride,
319 unsigned width, unsigned height);
320
321 void
322 util_format_ayuv_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
323 const float *src_row, unsigned src_stride,
324 unsigned width, unsigned height);
325
326 void
327 util_format_ayuv_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
328 const uint8_t *src_row, unsigned src_stride,
329 unsigned width, unsigned height);
330
331 void
332 util_format_ayuv_fetch_rgba_float(float *dst, const uint8_t *src,
333 unsigned i, unsigned j);
334 void
335 util_format_r8g8_b8g8_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride,
336 const uint8_t *src_row, unsigned src_stride,
337 unsigned width, unsigned height);
338
339 void
340 util_format_r8g8_b8g8_unorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
341 const uint8_t *src_row, unsigned src_stride,
342 unsigned width, unsigned height);
343
344 void
345 util_format_r8g8_b8g8_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
346 const float *src_row, unsigned src_stride,
347 unsigned width, unsigned height);
348
349 void
350 util_format_r8g8_b8g8_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
351 const uint8_t *src_row, unsigned src_stride,
352 unsigned width, unsigned height);
353
354 void
355 util_format_r8g8_b8g8_unorm_fetch_rgba_float(float *dst, const uint8_t *src,
356 unsigned i, unsigned j);
357
358 void
359 util_format_g8r8_g8b8_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride,
360 const uint8_t *src_row, unsigned src_stride,
361 unsigned width, unsigned height);
362
363 void
364 util_format_g8r8_g8b8_unorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
365 const uint8_t *src_row, unsigned src_stride,
366 unsigned width, unsigned height);
367
368 void
369 util_format_g8r8_g8b8_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
370 const float *src_row, unsigned src_stride,
371 unsigned width, unsigned height);
372
373 void
374 util_format_g8r8_g8b8_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
375 const uint8_t *src_row, unsigned src_stride,
376 unsigned width, unsigned height);
377
378 void
379 util_format_g8r8_g8b8_unorm_fetch_rgba_float(float *dst, const uint8_t *src,
380 unsigned i, unsigned j);
381
382 void
383 util_format_r8g8_r8b8_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride,
384 const uint8_t *src_row, unsigned src_stride,
385 unsigned width, unsigned height);
386
387 void
388 util_format_r8g8_r8b8_unorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
389 const uint8_t *src_row, unsigned src_stride,
390 unsigned width, unsigned height);
391
392 void
393 util_format_r8g8_r8b8_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
394 const float *src_row, unsigned src_stride,
395 unsigned width, unsigned height);
396
397 void
398 util_format_r8g8_r8b8_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
399 const uint8_t *src_row, unsigned src_stride,
400 unsigned width, unsigned height);
401
402 void
403 util_format_r8g8_r8b8_unorm_fetch_rgba_float(float *dst, const uint8_t *src,
404 unsigned i, unsigned j);
405
406 void
407 util_format_g8r8_b8r8_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride,
408 const uint8_t *src_row, unsigned src_stride,
409 unsigned width, unsigned height);
410
411 void
412 util_format_g8r8_b8r8_unorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
413 const uint8_t *src_row, unsigned src_stride,
414 unsigned width, unsigned height);
415
416 void
417 util_format_g8r8_b8r8_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
418 const float *src_row, unsigned src_stride,
419 unsigned width, unsigned height);
420
421 void
422 util_format_g8r8_b8r8_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
423 const uint8_t *src_row, unsigned src_stride,
424 unsigned width, unsigned height);
425
426 void
427 util_format_g8r8_b8r8_unorm_fetch_rgba_float(float *dst, const uint8_t *src,
428 unsigned i, unsigned j);
429
430 #endif /* U_FORMAT_YUV_H_ */