1 /*
2 * Copyright © 2018 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "igt_color_encoding.h"
25 #include "igt_matrix.h"
26 #include "igt_core.h"
27 #include "igt_fb.h"
28 #include "drmtest.h"
29
30 struct color_encoding {
31 float kr, kb;
32 };
33
34 static const struct color_encoding color_encodings[IGT_NUM_COLOR_ENCODINGS] = {
35 [IGT_COLOR_YCBCR_BT601] = { .kr = .299f, .kb = .114f, },
36 [IGT_COLOR_YCBCR_BT709] = { .kr = .2126f, .kb = .0722f, },
37 [IGT_COLOR_YCBCR_BT2020] = { .kr = .2627f, .kb = .0593f, },
38 };
39
rgb_to_ycbcr_matrix(const struct color_encoding * e)40 static struct igt_mat4 rgb_to_ycbcr_matrix(const struct color_encoding *e)
41 {
42 float kr = e->kr;
43 float kb = e->kb;
44 float kg = 1.0f - kr - kb;
45
46 struct igt_mat4 ret = {
47 .d[m(0, 0)] = kr,
48 .d[m(0, 1)] = kg,
49 .d[m(0, 2)] = kb,
50
51 .d[m(1, 0)] = -kr / (1.0f - kb),
52 .d[m(1, 1)] = -kg / (1.0f - kb),
53 .d[m(1, 2)] = 1.0f,
54
55 .d[m(2, 0)] = 1.0f,
56 .d[m(2, 1)] = -kg / (1.0f - kr),
57 .d[m(2, 2)] = -kb / (1.0f - kr),
58
59 .d[m(3, 3)] = 1.0f,
60 };
61
62 return ret;
63 }
64
ycbcr_to_rgb_matrix(const struct color_encoding * e)65 static struct igt_mat4 ycbcr_to_rgb_matrix(const struct color_encoding *e)
66 {
67 float kr = e->kr;
68 float kb = e->kb;
69 float kg = 1.0f - kr - kb;
70
71 struct igt_mat4 ret = {
72 .d[m(0, 0)] = 1.0f,
73 .d[m(0, 1)] = 0.0f,
74 .d[m(0, 2)] = 1.0 - kr,
75
76 .d[m(1, 0)] = 1.0f,
77 .d[m(1, 1)] = -(1.0 - kb) * kb / kg,
78 .d[m(1, 2)] = -(1.0 - kr) * kr / kg,
79
80 .d[m(2, 0)] = 1.0f,
81 .d[m(2, 1)] = 1.0 - kb,
82 .d[m(2, 2)] = 0.0f,
83
84 .d[m(3, 3)] = 1.0f,
85 };
86
87 return ret;
88 }
89
ycbcr_input_convert_matrix(enum igt_color_range color_range,float scale,float ofs_y,float max_y,float ofs_cbcr,float mid_cbcr,float max_cbcr,float max_val)90 static struct igt_mat4 ycbcr_input_convert_matrix(enum igt_color_range color_range, float scale,
91 float ofs_y, float max_y,
92 float ofs_cbcr, float mid_cbcr, float max_cbcr,
93 float max_val)
94 {
95 struct igt_mat4 t, s;
96
97 if (color_range == IGT_COLOR_YCBCR_FULL_RANGE) {
98 t = igt_matrix_translate(0.f, -mid_cbcr, -mid_cbcr);
99 s = igt_matrix_scale(scale, 2.0f * scale, 2.0f * scale);
100 } else {
101 t = igt_matrix_translate(-ofs_y, -mid_cbcr, -mid_cbcr);
102 s = igt_matrix_scale(scale * max_val / (max_y - ofs_y),
103 scale * max_val / (max_cbcr - mid_cbcr),
104 scale * max_val / (max_cbcr - mid_cbcr));
105 }
106
107 return igt_matrix_multiply(&s, &t);
108 }
109
ycbcr_output_convert_matrix(enum igt_color_range color_range,float scale,float ofs_y,float max_y,float ofs_cbcr,float mid_cbcr,float max_cbcr,float max_val)110 static struct igt_mat4 ycbcr_output_convert_matrix(enum igt_color_range color_range, float scale,
111 float ofs_y, float max_y,
112 float ofs_cbcr, float mid_cbcr, float max_cbcr,
113 float max_val)
114 {
115 struct igt_mat4 s, t;
116
117 if (color_range == IGT_COLOR_YCBCR_FULL_RANGE) {
118 s = igt_matrix_scale(scale, 0.5f * scale, 0.5f * scale);
119 t = igt_matrix_translate(0.f, mid_cbcr, mid_cbcr);
120 } else {
121 s = igt_matrix_scale(scale * (max_y - ofs_y) / max_val,
122 scale * (max_cbcr - mid_cbcr) / max_val,
123 scale * (max_cbcr - mid_cbcr) / max_val);
124 t = igt_matrix_translate(ofs_y, mid_cbcr, mid_cbcr);
125 }
126
127 return igt_matrix_multiply(&t, &s);
128 }
129
130 static const struct color_encoding_format {
131 uint32_t fourcc;
132
133 float max_val;
134
135 float ofs_y, max_y, ofs_cbcr, mid_cbcr, max_cbcr;
136 } formats[] = {
137 { DRM_FORMAT_XRGB8888, 255.f, },
138 { IGT_FORMAT_FLOAT, 1.f, },
139 { DRM_FORMAT_NV12, 255.f, 16.f, 235.f, 16.f, 128.f, 240.f },
140 { DRM_FORMAT_NV16, 255.f, 16.f, 235.f, 16.f, 128.f, 240.f },
141 { DRM_FORMAT_NV21, 255.f, 16.f, 235.f, 16.f, 128.f, 240.f },
142 { DRM_FORMAT_NV61, 255.f, 16.f, 235.f, 16.f, 128.f, 240.f },
143 { DRM_FORMAT_YUV420, 255.f, 16.f, 235.f, 16.f, 128.f, 240.f },
144 { DRM_FORMAT_YUV422, 255.f, 16.f, 235.f, 16.f, 128.f, 240.f },
145 { DRM_FORMAT_YVU420, 255.f, 16.f, 235.f, 16.f, 128.f, 240.f },
146 { DRM_FORMAT_YVU422, 255.f, 16.f, 235.f, 16.f, 128.f, 240.f },
147 { DRM_FORMAT_YUYV, 255.f, 16.f, 235.f, 16.f, 128.f, 240.f },
148 { DRM_FORMAT_YVYU, 255.f, 16.f, 235.f, 16.f, 128.f, 240.f },
149 { DRM_FORMAT_UYVY, 255.f, 16.f, 235.f, 16.f, 128.f, 240.f },
150 { DRM_FORMAT_VYUY, 255.f, 16.f, 235.f, 16.f, 128.f, 240.f },
151 { DRM_FORMAT_P010, 65472.f, 4096.f, 60160.f, 4096.f, 32768.f, 61440.f },
152 { DRM_FORMAT_P012, 65520.f, 4096.f, 60160.f, 4096.f, 32768.f, 61440.f },
153 { DRM_FORMAT_P016, 65535.f, 4096.f, 60160.f, 4096.f, 32768.f, 61440.f },
154 { DRM_FORMAT_Y210, 65472.f, 4096.f, 60160.f, 4096.f, 32768.f, 61440.f },
155 { DRM_FORMAT_Y212, 65520.f, 4096.f, 60160.f, 4096.f, 32768.f, 61440.f },
156 { DRM_FORMAT_Y216, 65535.f, 4096.f, 60160.f, 4096.f, 32768.f, 61440.f },
157 { DRM_FORMAT_Y410, 1023.f, 64.f, 940.f, 64.f, 512.f, 960.f },
158 { DRM_FORMAT_Y412, 65520.f, 4096.f, 60160.f, 4096.f, 32768.f, 61440.f },
159 { DRM_FORMAT_Y416, 65535.f, 4096.f, 60160.f, 4096.f, 32768.f, 61440.f },
160 { DRM_FORMAT_XVYU2101010, 1023.f, 64.f, 940.f, 64.f, 512.f, 960.f },
161 { DRM_FORMAT_XVYU12_16161616, 65520.f, 4096.f, 60160.f, 4096.f, 32768.f, 61440.f },
162 { DRM_FORMAT_XVYU16161616, 65535.f, 4096.f, 60160.f, 4096.f, 32768.f, 61440.f },
163 };
164
lookup_fourcc(uint32_t fourcc)165 static const struct color_encoding_format *lookup_fourcc(uint32_t fourcc)
166 {
167 int i;
168
169 for (i = 0; i < ARRAY_SIZE(formats); i++)
170 if (fourcc == formats[i].fourcc)
171 return &formats[i];
172
173 igt_assert_f(0, "Could not look up fourcc %.4s\n", (char *)&fourcc);
174 }
175
igt_ycbcr_to_rgb_matrix(uint32_t from_fourcc,uint32_t to_fourcc,enum igt_color_encoding color_encoding,enum igt_color_range color_range)176 struct igt_mat4 igt_ycbcr_to_rgb_matrix(uint32_t from_fourcc,
177 uint32_t to_fourcc,
178 enum igt_color_encoding color_encoding,
179 enum igt_color_range color_range)
180 {
181 const struct color_encoding *e = &color_encodings[color_encoding];
182 struct igt_mat4 r, c;
183 const struct color_encoding_format *fycbcr = lookup_fourcc(from_fourcc);
184 const struct color_encoding_format *frgb = lookup_fourcc(to_fourcc);
185 float scale = frgb->max_val / fycbcr->max_val;
186
187 igt_assert(fycbcr->ofs_y && !frgb->ofs_y);
188
189 r = ycbcr_input_convert_matrix(color_range, scale, fycbcr->ofs_y, fycbcr->max_y, fycbcr->ofs_cbcr, fycbcr->mid_cbcr, fycbcr->max_cbcr, fycbcr->max_val);
190 c = ycbcr_to_rgb_matrix(e);
191
192 return igt_matrix_multiply(&c, &r);
193 }
194
igt_rgb_to_ycbcr_matrix(uint32_t from_fourcc,uint32_t to_fourcc,enum igt_color_encoding color_encoding,enum igt_color_range color_range)195 struct igt_mat4 igt_rgb_to_ycbcr_matrix(uint32_t from_fourcc,
196 uint32_t to_fourcc,
197 enum igt_color_encoding color_encoding,
198 enum igt_color_range color_range)
199 {
200 const struct color_encoding *e = &color_encodings[color_encoding];
201 const struct color_encoding_format *frgb = lookup_fourcc(from_fourcc);
202 const struct color_encoding_format *fycbcr = lookup_fourcc(to_fourcc);
203 struct igt_mat4 c, r;
204 float scale = fycbcr->max_val / frgb->max_val;
205
206 igt_assert(fycbcr->ofs_y && !frgb->ofs_y);
207
208 c = rgb_to_ycbcr_matrix(e);
209 r = ycbcr_output_convert_matrix(color_range, scale, fycbcr->ofs_y, fycbcr->max_y, fycbcr->ofs_cbcr, fycbcr->mid_cbcr, fycbcr->max_cbcr, fycbcr->max_val);
210
211 return igt_matrix_multiply(&r, &c);
212 }
213
igt_color_encoding_to_str(enum igt_color_encoding encoding)214 const char *igt_color_encoding_to_str(enum igt_color_encoding encoding)
215 {
216 switch (encoding) {
217 case IGT_COLOR_YCBCR_BT601: return "ITU-R BT.601 YCbCr";
218 case IGT_COLOR_YCBCR_BT709: return "ITU-R BT.709 YCbCr";
219 case IGT_COLOR_YCBCR_BT2020: return "ITU-R BT.2020 YCbCr";
220 default: igt_assert(0); return NULL;
221 }
222 }
223
igt_color_range_to_str(enum igt_color_range range)224 const char *igt_color_range_to_str(enum igt_color_range range)
225 {
226 switch (range) {
227 case IGT_COLOR_YCBCR_LIMITED_RANGE: return "YCbCr limited range";
228 case IGT_COLOR_YCBCR_FULL_RANGE: return "YCbCr full range";
229 default: igt_assert(0); return NULL;
230 }
231 }
232