]> git.sesse.net Git - movit/blob - ycbcr_input_test.cpp
Add a unit test for Rec. 709 YCbCr.
[movit] / ycbcr_input_test.cpp
1 // Unit tests for YCbCrInput.
2 // FIXME: This class really ought to support mipmaps.
3
4 #include "test_util.h"
5 #include "gtest/gtest.h"
6 #include "ycbcr_input.h"
7
8 TEST(YCbCrInput, Simple444) {
9         const int width = 1;
10         const int height = 5;
11
12         // Pure-color test inputs, calculated with the formulas in Rec. 601
13         // section 2.5.4.
14         unsigned char y[width * height] = {
15                 16, 235, 81, 145, 41,
16         };
17         unsigned char cb[width * height] = {
18                 128, 128, 90, 54, 240,
19         };
20         unsigned char cr[width * height] = {
21                 128, 128, 240, 34, 110,
22         };
23         float expected_data[4 * width * height] = {
24                 0.0, 0.0, 0.0, 1.0,
25                 1.0, 1.0, 1.0, 1.0,
26                 1.0, 0.0, 0.0, 1.0,
27                 0.0, 1.0, 0.0, 1.0,
28                 0.0, 0.0, 1.0, 1.0,
29         };
30         float out_data[4 * width * height];
31
32         EffectChainTester tester(NULL, width, height);
33
34         ImageFormat format;
35         format.color_space = COLORSPACE_sRGB;
36         format.gamma_curve = GAMMA_sRGB;
37
38         YCbCrFormat ycbcr_format;
39         ycbcr_format.luma_coefficients = YCBCR_REC_601;
40         ycbcr_format.full_range = false;
41         ycbcr_format.chroma_subsampling_x = 1;
42         ycbcr_format.chroma_subsampling_y = 1;
43         ycbcr_format.chroma_x_position = 0.5f;
44         ycbcr_format.chroma_y_position = 0.5f;
45
46         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
47         input->set_pixel_data(0, y);
48         input->set_pixel_data(1, cb);
49         input->set_pixel_data(2, cr);
50         tester.get_chain()->add_input(input);
51
52         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
53
54         // Y'CbCr isn't 100% accurate (the input values are rounded),
55         // so we need some leeway.
56         expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
57 }
58
59 TEST(YCbCrInput, FullRangeRec601) {
60         const int width = 1;
61         const int height = 5;
62
63         // Pure-color test inputs, calculated with the formulas in Rec. 601
64         // section 2.5.4 but without the scaling factors applied
65         // (so both R, G, B, Y, Cb and R vary from 0 to 255).
66         unsigned char y[width * height] = {
67                 0, 255, 76, 150, 29,
68         };
69         unsigned char cb[width * height] = {
70                 128, 128, 85, 44, 255,
71         };
72         unsigned char cr[width * height] = {
73                 128, 128, 255, 21, 107,
74         };
75         float expected_data[4 * width * height] = {
76                 0.0, 0.0, 0.0, 1.0,
77                 1.0, 1.0, 1.0, 1.0,
78                 1.0, 0.0, 0.0, 1.0,
79                 0.0, 1.0, 0.0, 1.0,
80                 0.0, 0.0, 1.0, 1.0,
81         };
82         float out_data[4 * width * height];
83
84         EffectChainTester tester(NULL, width, height);
85
86         ImageFormat format;
87         format.color_space = COLORSPACE_sRGB;
88         format.gamma_curve = GAMMA_sRGB;
89
90         YCbCrFormat ycbcr_format;
91         ycbcr_format.luma_coefficients = YCBCR_REC_601;
92         ycbcr_format.full_range = true;
93         ycbcr_format.chroma_subsampling_x = 1;
94         ycbcr_format.chroma_subsampling_y = 1;
95         ycbcr_format.chroma_x_position = 0.5f;
96         ycbcr_format.chroma_y_position = 0.5f;
97
98         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
99         input->set_pixel_data(0, y);
100         input->set_pixel_data(1, cb);
101         input->set_pixel_data(2, cr);
102         tester.get_chain()->add_input(input);
103
104         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
105
106         // Y'CbCr isn't 100% accurate (the input values are rounded),
107         // so we need some leeway.
108         expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
109 }
110
111 TEST(YCbCrInput, Rec709) {
112         const int width = 1;
113         const int height = 5;
114
115         // Pure-color test inputs, calculated with the formulas in Rec. 709
116         // page 19, items 3.4 and 3.5.
117         unsigned char y[width * height] = {
118                 16, 235, 63, 173, 32, 
119         };
120         unsigned char cb[width * height] = {
121                 128, 128, 102, 42, 240,
122         };
123         unsigned char cr[width * height] = {
124                 128, 128, 240, 26, 118,
125         };
126         float expected_data[4 * width * height] = {
127                 0.0, 0.0, 0.0, 1.0,
128                 1.0, 1.0, 1.0, 1.0,
129                 1.0, 0.0, 0.0, 1.0,
130                 0.0, 1.0, 0.0, 1.0,
131                 0.0, 0.0, 1.0, 1.0,
132         };
133         float out_data[4 * width * height];
134
135         EffectChainTester tester(NULL, width, height);
136
137         ImageFormat format;
138         format.color_space = COLORSPACE_sRGB;
139         format.gamma_curve = GAMMA_sRGB;
140
141         YCbCrFormat ycbcr_format;
142         ycbcr_format.luma_coefficients = YCBCR_REC_709;
143         ycbcr_format.full_range = false;
144         ycbcr_format.chroma_subsampling_x = 1;
145         ycbcr_format.chroma_subsampling_y = 1;
146         ycbcr_format.chroma_x_position = 0.5f;
147         ycbcr_format.chroma_y_position = 0.5f;
148
149         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
150         input->set_pixel_data(0, y);
151         input->set_pixel_data(1, cb);
152         input->set_pixel_data(2, cr);
153         tester.get_chain()->add_input(input);
154
155         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
156
157         // Y'CbCr isn't 100% accurate (the input values are rounded),
158         // so we need some leeway.
159         expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
160 }
161
162 TEST(YCbCrInput, Subsampling420) {
163         const int width = 4;
164         const int height = 4;
165
166         unsigned char y[width * height] = {
167                 126, 126, 126, 126,
168                 126, 126, 126, 126,
169                 126, 126, 126, 126,
170                 126, 126, 126, 126,
171         };
172         unsigned char cb[(width/2) * (height/2)] = {
173                 64, 128,
174                 128, 192,
175         };
176         unsigned char cr[(width/2) * (height/2)] = {
177                 128, 128,
178                 128, 128,
179         };
180
181         // Note: This is only the blue channel. The chroma samples (with associated
182         // values for blue) are marked off in comments.
183         float expected_data[width * height] = {
184                 0.000, 0.125, 0.375, 0.500, 
185                  /* 0.0 */      /* 0.5 */
186                 0.125, 0.250, 0.500, 0.625,
187
188                 0.375, 0.500, 0.750, 0.875,
189                  /* 0.5 */      /* 1.0 */
190                 0.500, 0.625, 0.875, 1.000,
191         };
192         float out_data[width * height];
193
194         EffectChainTester tester(NULL, width, height);
195
196         ImageFormat format;
197         format.color_space = COLORSPACE_sRGB;
198         format.gamma_curve = GAMMA_sRGB;
199
200         YCbCrFormat ycbcr_format;
201         ycbcr_format.luma_coefficients = YCBCR_REC_601;
202         ycbcr_format.full_range = false;
203         ycbcr_format.chroma_subsampling_x = 2;
204         ycbcr_format.chroma_subsampling_y = 2;
205         ycbcr_format.chroma_x_position = 0.5f;
206         ycbcr_format.chroma_y_position = 0.5f;
207
208         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
209         input->set_pixel_data(0, y);
210         input->set_pixel_data(1, cb);
211         input->set_pixel_data(2, cr);
212         tester.get_chain()->add_input(input);
213
214         tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
215
216         // Y'CbCr isn't 100% accurate (the input values are rounded),
217         // so we need some leeway.
218         expect_equal(expected_data, out_data, width, height, 0.01, 0.001);
219 }
220
221 TEST(YCbCrInput, Subsampling420WithNonCenteredSamples) {
222         const int width = 4;
223         const int height = 4;
224
225         unsigned char y[width * height] = {
226                 126, 126, 126, 126,
227                 126, 126, 126, 126,
228                 126, 126, 126, 126,
229                 126, 126, 126, 126,
230         };
231         unsigned char cb[(width/2) * (height/2)] = {
232                 64, 128,
233                 128, 192,
234         };
235         unsigned char cr[(width/2) * (height/2)] = {
236                 128, 128,
237                 128, 128,
238         };
239
240         // Note: This is only the blue channel. The chroma samples (with associated
241         // values for blue) are marked off in comments.
242         float expected_data[width * height] = {
243                    0.000, 0.250, 0.500, 0.500, 
244                 /* 0.0 */     /* 0.5 */
245                    0.125, 0.375, 0.625, 0.625,
246
247                    0.375, 0.625, 0.875, 0.875,
248                 /* 0.5 */     /* 1.0 */
249                    0.500, 0.750, 1.000, 1.000,
250         };
251         float out_data[width * height];
252
253         EffectChainTester tester(NULL, width, height);
254
255         ImageFormat format;
256         format.color_space = COLORSPACE_sRGB;
257         format.gamma_curve = GAMMA_sRGB;
258
259         YCbCrFormat ycbcr_format;
260         ycbcr_format.luma_coefficients = YCBCR_REC_601;
261         ycbcr_format.full_range = false;
262         ycbcr_format.chroma_subsampling_x = 2;
263         ycbcr_format.chroma_subsampling_y = 2;
264         ycbcr_format.chroma_x_position = 0.0f;
265         ycbcr_format.chroma_y_position = 0.5f;
266
267         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
268         input->set_pixel_data(0, y);
269         input->set_pixel_data(1, cb);
270         input->set_pixel_data(2, cr);
271         tester.get_chain()->add_input(input);
272
273         tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
274
275         // Y'CbCr isn't 100% accurate (the input values are rounded),
276         // so we need some leeway.
277         expect_equal(expected_data, out_data, width, height, 0.01, 0.001);
278 }