aboutsummaryrefslogtreecommitdiff
path: root/mesalib/src/gallium/auxiliary/util/u_format_yuv.h
blob: 4cb22df7a3c85d2fb2ed796546c76268b9fa6c7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**************************************************************************
 *
 * Copyright 2010 VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 **************************************************************************/


/**
 * @file
 * YUV colorspace conversion.
 *
 * @author Brian Paul <brianp@vmware.com>
 * @author Michal Krol <michal@vmware.com>
 * @author Jose Fonseca <jfonseca@vmware.com>
 *
 * See also:
 * - http://www.fourcc.org/fccyvrgb.php
 * - http://msdn.microsoft.com/en-us/library/ms893078
 * - http://en.wikipedia.org/wiki/YUV
 */


#ifndef U_FORMAT_YUV_H_
#define U_FORMAT_YUV_H_


#include "pipe/p_compiler.h"
#include "u_math.h"


/*
 * TODO: Ensure we use consistent and right floating formulas, with enough
 * precision in the coefficients.
 */

static INLINE void
util_format_rgb_float_to_yuv(float r, float g, float b,
                             uint8_t *y, uint8_t *u, uint8_t *v)
{
   const float _r = CLAMP(r, 0.0f, 1.0f);
   const float _g = CLAMP(g, 0.0f, 1.0f);
   const float _b = CLAMP(b, 0.0f, 1.0f);

   const float scale = 255.0f;

   const int _y = scale * ( (0.257f * _r) + (0.504f * _g) + (0.098f * _b));
   const int _u = scale * (-(0.148f * _r) - (0.291f * _g) + (0.439f * _b));
   const int _v = scale * ( (0.439f * _r) - (0.368f * _g) - (0.071f * _b));

   *y = _y + 16; 
   *u = _u + 128; 
   *v = _v + 128; 
}


static INLINE void
util_format_yuv_to_rgb_float(uint8_t y, uint8_t u, uint8_t v,
                             float *r, float *g, float *b)
{
   const int _y = y - 16;
   const int _u = u - 128;
   const int _v = v - 128;

   const float y_factor = 255.0f / 219.0f;

   const float scale = 1.0f / 255.0f;

   *r = scale * (y_factor * _y               + 1.596f * _v);
   *g = scale * (y_factor * _y - 0.391f * _u - 0.813f * _v);
   *b = scale * (y_factor * _y + 2.018f * _u              );
}


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)
{
   *y = ((  66 * r + 129 * g +  25 * b + 128) >> 8) +  16;
   *u = (( -38 * r -  74 * g + 112 * b + 128) >> 8) + 128;
   *v = (( 112 * r -  94 * g -  18 * b + 128) >> 8) + 128;
}


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)
{
   const int _y = y - 16;
   const int _u = u - 128;
   const int _v = v - 128;

   const int _r = (298 * _y            + 409 * _v + 128) >> 8;
   const int _g = (298 * _y - 100 * _u - 208 * _v + 128) >> 8;
   const int _b = (298 * _y + 516 * _u            + 128) >> 8;

   *r = CLAMP(_r, 0, 255);
   *g = CLAMP(_g, 0, 255);
   *b = CLAMP(_b, 0, 255);
}



void
util_format_uyvy_unpack_rgba_float(float *dst_row, unsigned dst_stride,
                              const uint8_t *src_row, unsigned src_stride,
                              unsigned width, unsigned height);

void
util_format_uyvy_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                               const uint8_t *src_row, unsigned src_stride,
                               unsigned width, unsigned height);

void
util_format_uyvy_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
                            const float *src_row, unsigned src_stride,
                            unsigned width, unsigned height);

void
util_format_uyvy_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);

void
util_format_uyvy_fetch_rgba_float(float *dst, const uint8_t *src,
                             unsigned i, unsigned j);

void
util_format_yuyv_unpack_rgba_float(float *dst_row, unsigned dst_stride,
                              const uint8_t *src_row, unsigned src_stride,
                              unsigned width, unsigned height);

void
util_format_yuyv_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                               const uint8_t *src_row, unsigned src_stride,
                               unsigned width, unsigned height);

void
util_format_yuyv_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
                            const float *src_row, unsigned src_stride,
                            unsigned width, unsigned height);

void
util_format_yuyv_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);

void
util_format_yuyv_fetch_rgba_float(float *dst, const uint8_t *src,
                             unsigned i, unsigned j);

/* XXX: Stubbed for now */
void
util_format_yv12_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_yv12_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_yv12_unpack_rgba_float(float *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_yv12_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
                             const float *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_yv12_fetch_rgba_float(float *dst, const uint8_t *src,
                             unsigned i, unsigned j);
void
util_format_yv16_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_yv16_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_yv16_unpack_rgba_float(float *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_yv16_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
                             const float *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_yv16_fetch_rgba_float(float *dst, const uint8_t *src,
                             unsigned i, unsigned j);
void
util_format_iyuv_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_iyuv_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_iyuv_unpack_rgba_float(float *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_iyuv_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
                             const float *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_iyuv_fetch_rgba_float(float *dst, const uint8_t *src,
                             unsigned i, unsigned j);
void
util_format_nv12_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_nv12_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_nv12_unpack_rgba_float(float *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_nv12_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
                             const float *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_nv12_fetch_rgba_float(float *dst, const uint8_t *src,
                             unsigned i, unsigned j);
void
util_format_nv21_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_nv21_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_nv21_unpack_rgba_float(float *dst_row, unsigned dst_stride,
                             const uint8_t *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_nv21_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
                             const float *src_row, unsigned src_stride,
                             unsigned width, unsigned height);
void
util_format_nv21_fetch_rgba_float(float *dst, const uint8_t *src,
                             unsigned i, unsigned j);
void
util_format_r8g8_b8g8_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride,
                                         const uint8_t *src_row, unsigned src_stride,
                                         unsigned width, unsigned height);

void
util_format_r8g8_b8g8_unorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                                          const uint8_t *src_row, unsigned src_stride,
                                          unsigned width, unsigned height);

void
util_format_r8g8_b8g8_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
                                       const float *src_row, unsigned src_stride,
                                       unsigned width, unsigned height);

void
util_format_r8g8_b8g8_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                                        const uint8_t *src_row, unsigned src_stride,
                                        unsigned width, unsigned height);

void
util_format_r8g8_b8g8_unorm_fetch_rgba_float(float *dst, const uint8_t *src,
                                        unsigned i, unsigned j);

void
util_format_g8r8_g8b8_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride,
                                         const uint8_t *src_row, unsigned src_stride,
                                         unsigned width, unsigned height);

void
util_format_g8r8_g8b8_unorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                                          const uint8_t *src_row, unsigned src_stride,
                                          unsigned width, unsigned height);

void
util_format_g8r8_g8b8_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
                                       const float *src_row, unsigned src_stride,
                                       unsigned width, unsigned height);

void
util_format_g8r8_g8b8_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
                                        const uint8_t *src_row, unsigned src_stride,
                                        unsigned width, unsigned height);

void
util_format_g8r8_g8b8_unorm_fetch_rgba_float(float *dst, const uint8_t *src,
                                        unsigned i, unsigned j);



#endif /* U_FORMAT_YUV_H_ */