1 | /* |
2 | * Copyright 2019 Google Inc. |
3 | * |
4 | * Use of this source code is governed by a BSD-style license that can be |
5 | * found in the LICENSE file. |
6 | */ |
7 | |
8 | #include "include/core/SkM44.h" |
9 | #include "src/core/SkYUVMath.h" |
10 | |
11 | // in SkColorMatrix order (row-major) |
12 | // Created by running SkColorMatrix_DumpYUVMatrixTables() |
13 | |
14 | const float Rec709_rgb_to_yuv[] = { |
15 | 0.182586f, 0.614231f, 0.062007f, 0.000000f, 0.062745f, |
16 | -0.100644f, -0.338572f, 0.439216f, 0.000000f, 0.501961f, |
17 | 0.439216f, -0.398942f, -0.040274f, 0.000000f, 0.501961f, |
18 | 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f, |
19 | }; |
20 | const float Rec709_yuv_to_rgb[] = { |
21 | 1.164384f, 0.000000f, 1.792741f, 0.000000f, -0.972945f, |
22 | 1.164384f, -0.213249f, -0.532909f, 0.000000f, 0.301483f, |
23 | 1.164384f, 2.112402f, 0.000000f, 0.000000f, -1.133402f, |
24 | 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f, |
25 | }; |
26 | const float Rec601_rgb_to_yuv[] = { |
27 | 0.256788f, 0.504129f, 0.097906f, 0.000000f, 0.062745f, |
28 | -0.148223f, -0.290993f, 0.439216f, 0.000000f, 0.501961f, |
29 | 0.439216f, -0.367788f, -0.071427f, 0.000000f, 0.501961f, |
30 | 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f, |
31 | }; |
32 | const float Rec601_yuv_to_rgb[] = { |
33 | 1.164384f, 0.000000f, 1.596027f, 0.000000f, -0.874202f, |
34 | 1.164384f, -0.391762f, -0.812968f, 0.000000f, 0.531668f, |
35 | 1.164384f, 2.017232f, 0.000000f, 0.000000f, -1.085631f, |
36 | 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f, |
37 | }; |
38 | const float JPEG_rgb_to_yuv[] = { |
39 | 0.299000f, 0.587000f, 0.114000f, 0.000000f, 0.000000f, |
40 | -0.168736f, -0.331264f, 0.500000f, 0.000000f, 0.501961f, |
41 | 0.500000f, -0.418688f, -0.081312f, 0.000000f, 0.501961f, |
42 | 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f, |
43 | }; |
44 | const float JPEG_yuv_to_rgb[] = { |
45 | 1.000000f, 0.000000f, 1.402000f, 0.000000f, -0.703749f, |
46 | 1.000000f, -0.344136f, -0.714136f, 0.000000f, 0.531211f, |
47 | 1.000000f, 1.772000f, 0.000000f, 0.000000f, -0.889475f, |
48 | 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f, |
49 | }; |
50 | const float BT2020_rgb_to_yuv[] = { |
51 | 0.225613f, 0.582282f, 0.050928f, 0.000000f, 0.062745f, |
52 | -0.122655f, -0.316560f, 0.439216f, 0.000000f, 0.501961f, |
53 | 0.439216f, -0.403890f, -0.035326f, 0.000000f, 0.501961f, |
54 | 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f, |
55 | }; |
56 | const float BT2020_yuv_to_rgb[] = { |
57 | 1.164384f, 0.000000f, 1.678674f, 0.000000f, -0.915688f, |
58 | 1.164384f, -0.187326f, -0.650424f, 0.000000f, 0.347458f, |
59 | 1.164384f, 2.141772f, 0.000000f, 0.000000f, -1.148145f, |
60 | 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f, |
61 | }; |
62 | |
63 | static_assert(kJPEG_SkYUVColorSpace == 0, "" ); |
64 | static_assert(kRec601_SkYUVColorSpace == 1, "" ); |
65 | static_assert(kRec709_SkYUVColorSpace == 2, "" ); |
66 | static_assert(kBT2020_SkYUVColorSpace == 3, "" ); |
67 | |
68 | const float* yuv_to_rgb_array[] = { |
69 | JPEG_yuv_to_rgb, |
70 | Rec601_yuv_to_rgb, |
71 | Rec709_yuv_to_rgb, |
72 | BT2020_yuv_to_rgb, |
73 | }; |
74 | |
75 | const float* rgb_to_yuv_array[] = { |
76 | JPEG_rgb_to_yuv, |
77 | Rec601_rgb_to_yuv, |
78 | Rec709_rgb_to_yuv, |
79 | BT2020_rgb_to_yuv, |
80 | }; |
81 | |
82 | constexpr size_t kSizeOfColorMatrix = 20 * sizeof(float); |
83 | |
84 | void SkColorMatrix_RGB2YUV(SkYUVColorSpace cs, float m[20]) { |
85 | if ((unsigned)cs < (unsigned)kIdentity_SkYUVColorSpace) { |
86 | memcpy(m, rgb_to_yuv_array[(unsigned)cs], kSizeOfColorMatrix); |
87 | } else { |
88 | memset(m, 0, kSizeOfColorMatrix); |
89 | m[0] = m[6] = m[12] = m[18] = 1; |
90 | } |
91 | } |
92 | |
93 | void SkColorMatrix_YUV2RGB(SkYUVColorSpace cs, float m[20]) { |
94 | if ((unsigned)cs < (unsigned)kIdentity_SkYUVColorSpace) { |
95 | memcpy(m, yuv_to_rgb_array[(unsigned)cs], kSizeOfColorMatrix); |
96 | } else { |
97 | memset(m, 0, kSizeOfColorMatrix); |
98 | m[0] = m[6] = m[12] = m[18] = 1; |
99 | } |
100 | } |
101 | |
102 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
103 | |
104 | // we just drop the alpha rol/col from the colormatrix |
105 | // output is | tr | |
106 | // | 3x3 tg | |
107 | // | tb | |
108 | // | 0 0 0 1 | |
109 | static void colormatrix_to_matrix44(const float src[20], SkM44* dst) { |
110 | *dst = SkM44(src[ 0], src[ 1], src[ 2], src[ 4], |
111 | src[ 5], src[ 6], src[ 7], src[ 9], |
112 | src[10], src[11], src[12], src[14], |
113 | 0, 0, 0, 1); |
114 | } |
115 | |
116 | // input: ignore the bottom row |
117 | // output: inject identity row/column for alpha |
118 | static void matrix44_to_colormatrix(const SkM44& src, float dst[20]) { |
119 | dst[0] = src.rc(0,0); |
120 | dst[1] = src.rc(0,1); |
121 | dst[2] = src.rc(0,2); |
122 | dst[3] = 0; |
123 | dst[4] = src.rc(0,3); // tx |
124 | |
125 | dst[5] = src.rc(1,0); |
126 | dst[6] = src.rc(1,1); |
127 | dst[7] = src.rc(1,2); |
128 | dst[8] = 0; |
129 | dst[9] = src.rc(1,3); // ty |
130 | |
131 | dst[10] = src.rc(2,0); |
132 | dst[11] = src.rc(2,1); |
133 | dst[12] = src.rc(2,2); |
134 | dst[13] = 0; |
135 | dst[14] = src.rc(2,3); // tz |
136 | |
137 | dst[15] = dst[16] = dst[17] = dst[19] = 0; |
138 | dst[18] = 1; |
139 | } |
140 | |
141 | static void scale3(float m[], float s) { |
142 | for (int i = 0; i < 3; ++i) { |
143 | m[i] *= s; |
144 | } |
145 | } |
146 | |
147 | namespace { |
148 | struct YUVCoeff { |
149 | float Kr, Kb; |
150 | float scaleY, addY; |
151 | float scaleUV; |
152 | }; |
153 | } // namespace |
154 | |
155 | const YUVCoeff gCoeff[] = { |
156 | // kJPEG_SkYUVColorSpace |
157 | { 0.299f, 0.114f, 1, 0, 1, }, |
158 | |
159 | // kRec601_SkYUVColorSpace |
160 | { 0.299f, 0.114f, 219/255.f, 16/255.f, 224/255.f, }, |
161 | |
162 | // kRec709_SkYUVColorSpace |
163 | { 0.2126f, 0.0722f, 219/255.f, 16/255.f, 224/255.f, }, |
164 | |
165 | // kBT2020_SkYUVColorSpace |
166 | { 0.2627f, 0.0593f, 219/255.f, 16/255.f, 224/255.f, }, |
167 | }; |
168 | |
169 | static void make_rgb_to_yuv_matrix(float mx[20], const YUVCoeff& c) { |
170 | const float Kr = c.Kr; |
171 | const float Kb = c.Kb; |
172 | const float Kg = 1.0f - Kr - Kb; |
173 | const float Cr = 0.5f / (1.0f - Kb); |
174 | const float Cb = 0.5f / (1.0f - Kr); |
175 | |
176 | float m[20] = { |
177 | Kr, Kg, Kb, 0, c.addY, |
178 | -Kr, -Kg, 1-Kb, 0, 128/255.f, |
179 | 1-Kr, -Kg, -Kb, 0, 128/255.f, |
180 | 0, 0, 0, 1, 0, |
181 | }; |
182 | memcpy(mx, m, sizeof(m)); |
183 | scale3(mx + 0, c.scaleY); |
184 | scale3(mx + 5, Cr * c.scaleUV); |
185 | scale3(mx + 10, Cb * c.scaleUV); |
186 | } |
187 | |
188 | static void dump(const float m[20], SkYUVColorSpace cs, bool rgb2yuv) { |
189 | const char* names[] = { |
190 | "JPEG" , "Rec601" , "Rec709" , "BT2020" , |
191 | }; |
192 | const char* dirnames[] = { |
193 | "yuv_to_rgb" , "rgb_to_yuv" , |
194 | }; |
195 | SkDebugf("const float %s_%s[] = {\n" , names[cs], dirnames[rgb2yuv]); |
196 | for (int i = 0; i < 4; ++i) { |
197 | SkDebugf(" " ); |
198 | for (int j = 0; j < 5; ++j) { |
199 | SkDebugf(" %9.6ff," , m[i * 5 + j]); |
200 | } |
201 | SkDebugf("\n" ); |
202 | } |
203 | SkDebugf("};\n" ); |
204 | } |
205 | |
206 | // Used to create the prebuilt tables for each colorspace. |
207 | // Don't remove this function, in case we want to recompute those tables in the future. |
208 | void SkColorMatrix_DumpYUVMatrixTables() { |
209 | for (auto cs : {kRec709_SkYUVColorSpace, kRec601_SkYUVColorSpace, kJPEG_SkYUVColorSpace, |
210 | kBT2020_SkYUVColorSpace}) { |
211 | float m[20]; |
212 | make_rgb_to_yuv_matrix(m, gCoeff[(unsigned)cs]); |
213 | dump(m, cs, true); |
214 | SkM44 m44, im44; |
215 | colormatrix_to_matrix44(m, &m44); |
216 | float im[20]; |
217 | #ifdef SK_DEBUG |
218 | // be sure our coversion between matrix44 and colormatrix is perfect |
219 | matrix44_to_colormatrix(m44, im); |
220 | SkASSERT(memcmp(m, im, sizeof(im)) == 0); |
221 | #endif |
222 | SkAssertResult(m44.invert(&im44)); |
223 | matrix44_to_colormatrix(im44, im); |
224 | dump(im, cs, false); |
225 | } |
226 | } |
227 | |