1 // Unit tests for YCbCrInput.
6 #include "effect_chain.h"
7 #include "gtest/gtest.h"
10 #include "ycbcr_input.h"
14 TEST(YCbCrInputTest, Simple444) {
18 // Pure-color test inputs, calculated with the formulas in Rec. 601
20 unsigned char y[width * height] = {
23 unsigned char cb[width * height] = {
24 128, 128, 90, 54, 240,
26 unsigned char cr[width * height] = {
27 128, 128, 240, 34, 110,
29 float expected_data[4 * width * height] = {
36 float out_data[4 * width * height];
38 EffectChainTester tester(NULL, width, height);
41 format.color_space = COLORSPACE_sRGB;
42 format.gamma_curve = GAMMA_sRGB;
44 YCbCrFormat ycbcr_format;
45 ycbcr_format.luma_coefficients = YCBCR_REC_601;
46 ycbcr_format.full_range = false;
47 ycbcr_format.num_levels = 256;
48 ycbcr_format.chroma_subsampling_x = 1;
49 ycbcr_format.chroma_subsampling_y = 1;
50 ycbcr_format.cb_x_position = 0.5f;
51 ycbcr_format.cb_y_position = 0.5f;
52 ycbcr_format.cr_x_position = 0.5f;
53 ycbcr_format.cr_y_position = 0.5f;
55 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
56 input->set_pixel_data(0, y);
57 input->set_pixel_data(1, cb);
58 input->set_pixel_data(2, cr);
59 tester.get_chain()->add_input(input);
61 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
63 // Y'CbCr isn't 100% accurate (the input values are rounded),
64 // so we need some leeway.
65 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
68 TEST(YCbCrInputTest, FullRangeRec601) {
72 // Pure-color test inputs, calculated with the formulas in Rec. 601
73 // section 2.5.4 but without the scaling factors applied
74 // (so both R, G, B, Y, Cb and R vary from 0 to 255).
75 unsigned char y[width * height] = {
78 unsigned char cb[width * height] = {
79 128, 128, 85, 44, 255,
81 unsigned char cr[width * height] = {
82 128, 128, 255, 21, 107,
84 float expected_data[4 * width * height] = {
91 float out_data[4 * width * height];
93 EffectChainTester tester(NULL, width, height);
96 format.color_space = COLORSPACE_sRGB;
97 format.gamma_curve = GAMMA_sRGB;
99 YCbCrFormat ycbcr_format;
100 ycbcr_format.luma_coefficients = YCBCR_REC_601;
101 ycbcr_format.full_range = true;
102 ycbcr_format.num_levels = 256;
103 ycbcr_format.chroma_subsampling_x = 1;
104 ycbcr_format.chroma_subsampling_y = 1;
105 ycbcr_format.cb_x_position = 0.5f;
106 ycbcr_format.cb_y_position = 0.5f;
107 ycbcr_format.cr_x_position = 0.5f;
108 ycbcr_format.cr_y_position = 0.5f;
110 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
111 input->set_pixel_data(0, y);
112 input->set_pixel_data(1, cb);
113 input->set_pixel_data(2, cr);
114 tester.get_chain()->add_input(input);
116 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
118 // Y'CbCr isn't 100% accurate (the input values are rounded),
119 // so we need some leeway.
120 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
123 TEST(YCbCrInputTest, Rec709) {
125 const int height = 5;
127 // Pure-color test inputs, calculated with the formulas in Rec. 709
128 // page 19, items 3.4 and 3.5.
129 unsigned char y[width * height] = {
130 16, 235, 63, 173, 32,
132 unsigned char cb[width * height] = {
133 128, 128, 102, 42, 240,
135 unsigned char cr[width * height] = {
136 128, 128, 240, 26, 118,
138 float expected_data[4 * width * height] = {
145 float out_data[4 * width * height];
147 EffectChainTester tester(NULL, width, height);
150 format.color_space = COLORSPACE_sRGB;
151 format.gamma_curve = GAMMA_sRGB;
153 YCbCrFormat ycbcr_format;
154 ycbcr_format.luma_coefficients = YCBCR_REC_709;
155 ycbcr_format.full_range = false;
156 ycbcr_format.num_levels = 256;
157 ycbcr_format.chroma_subsampling_x = 1;
158 ycbcr_format.chroma_subsampling_y = 1;
159 ycbcr_format.cb_x_position = 0.5f;
160 ycbcr_format.cb_y_position = 0.5f;
161 ycbcr_format.cr_x_position = 0.5f;
162 ycbcr_format.cr_y_position = 0.5f;
164 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
165 input->set_pixel_data(0, y);
166 input->set_pixel_data(1, cb);
167 input->set_pixel_data(2, cr);
168 tester.get_chain()->add_input(input);
170 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
172 // Y'CbCr isn't 100% accurate (the input values are rounded),
173 // so we need some leeway.
174 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
177 TEST(YCbCrInputTest, Rec2020) {
179 const int height = 5;
181 // Pure-color test inputs, calculated with the formulas in Rec. 2020
182 // page 4, tables 4 and 5 (for conventional non-constant luminance).
183 // Note that we still use 8-bit inputs, even though Rec. 2020 is only
184 // defined for 10- and 12-bit.
185 unsigned char y[width * height] = {
186 16, 235, 74, 164, 29,
188 unsigned char cb[width * height] = {
189 128, 128, 97, 47, 240,
191 unsigned char cr[width * height] = {
192 128, 128, 240, 25, 119,
194 float expected_data[4 * width * height] = {
201 float out_data[4 * width * height];
203 EffectChainTester tester(NULL, width, height);
206 format.color_space = COLORSPACE_sRGB;
207 format.gamma_curve = GAMMA_sRGB;
209 YCbCrFormat ycbcr_format;
210 ycbcr_format.luma_coefficients = YCBCR_REC_2020;
211 ycbcr_format.full_range = false;
212 ycbcr_format.num_levels = 256;
213 ycbcr_format.chroma_subsampling_x = 1;
214 ycbcr_format.chroma_subsampling_y = 1;
215 ycbcr_format.cb_x_position = 0.5f;
216 ycbcr_format.cb_y_position = 0.5f;
217 ycbcr_format.cr_x_position = 0.5f;
218 ycbcr_format.cr_y_position = 0.5f;
220 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
221 input->set_pixel_data(0, y);
222 input->set_pixel_data(1, cb);
223 input->set_pixel_data(2, cr);
224 tester.get_chain()->add_input(input);
226 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
228 // Y'CbCr isn't 100% accurate (the input values are rounded),
229 // so we need some leeway.
230 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
233 TEST(YCbCrInputTest, Subsampling420) {
235 const int height = 4;
237 unsigned char y[width * height] = {
243 unsigned char cb[(width/2) * (height/2)] = {
247 unsigned char cr[(width/2) * (height/2)] = {
252 // Note: This is only the blue channel. The chroma samples (with associated
253 // values for blue) are marked off in comments.
254 float expected_data[width * height] = {
255 0.000, 0.125, 0.375, 0.500,
257 0.125, 0.250, 0.500, 0.625,
259 0.375, 0.500, 0.750, 0.875,
261 0.500, 0.625, 0.875, 1.000,
263 float out_data[width * height];
265 EffectChainTester tester(NULL, width, height);
268 format.color_space = COLORSPACE_sRGB;
269 format.gamma_curve = GAMMA_sRGB;
271 YCbCrFormat ycbcr_format;
272 ycbcr_format.luma_coefficients = YCBCR_REC_601;
273 ycbcr_format.full_range = false;
274 ycbcr_format.num_levels = 256;
275 ycbcr_format.chroma_subsampling_x = 2;
276 ycbcr_format.chroma_subsampling_y = 2;
277 ycbcr_format.cb_x_position = 0.5f;
278 ycbcr_format.cb_y_position = 0.5f;
279 ycbcr_format.cr_x_position = 0.5f;
280 ycbcr_format.cr_y_position = 0.5f;
282 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
283 input->set_pixel_data(0, y);
284 input->set_pixel_data(1, cb);
285 input->set_pixel_data(2, cr);
286 tester.get_chain()->add_input(input);
288 tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
290 // Y'CbCr isn't 100% accurate (the input values are rounded),
291 // so we need some leeway.
292 expect_equal(expected_data, out_data, width, height, 0.01, 0.001);
295 TEST(YCbCrInputTest, Subsampling420WithNonCenteredSamples) {
297 const int height = 4;
299 unsigned char y[width * height] = {
305 unsigned char cb[(width/2) * (height/2)] = {
309 unsigned char cr[(width/2) * (height/2)] = {
314 // Note: This is only the blue channel. The chroma samples (with associated
315 // values for blue) are marked off in comments.
316 float expected_data[width * height] = {
317 0.000, 0.250, 0.500, 0.500,
319 0.125, 0.375, 0.625, 0.625,
321 0.375, 0.625, 0.875, 0.875,
323 0.500, 0.750, 1.000, 1.000,
325 float out_data[width * height];
327 EffectChainTester tester(NULL, width, height);
330 format.color_space = COLORSPACE_sRGB;
331 format.gamma_curve = GAMMA_sRGB;
333 YCbCrFormat ycbcr_format;
334 ycbcr_format.luma_coefficients = YCBCR_REC_601;
335 ycbcr_format.full_range = false;
336 ycbcr_format.num_levels = 256;
337 ycbcr_format.chroma_subsampling_x = 2;
338 ycbcr_format.chroma_subsampling_y = 2;
339 ycbcr_format.cb_x_position = 0.0f;
340 ycbcr_format.cb_y_position = 0.5f;
341 ycbcr_format.cr_x_position = 0.0f;
342 ycbcr_format.cr_y_position = 0.5f;
344 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
345 input->set_pixel_data(0, y);
346 input->set_pixel_data(1, cb);
347 input->set_pixel_data(2, cr);
348 tester.get_chain()->add_input(input);
350 tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
352 // Y'CbCr isn't 100% accurate (the input values are rounded),
353 // so we need some leeway.
354 expect_equal(expected_data, out_data, width, height, 0.01, 0.001);
357 // Yes, some 4:2:2 formats actually have this craziness.
358 TEST(YCbCrInputTest, DifferentCbAndCrPositioning) {
360 const int height = 4;
362 unsigned char y[width * height] = {
368 unsigned char cb[(width/2) * height] = {
374 unsigned char cr[(width/2) * height] = {
381 // Chroma samples in this csae are always co-sited with a luma sample;
382 // their associated color values and position are marked off in comments.
383 float expected_data_blue[width * height] = {
384 0.000 /* 0.0 */, 0.250, 0.500 /* 0.5 */, 0.500,
385 0.500 /* 0.5 */, 0.750, 1.000 /* 1.0 */, 1.000,
386 0.500 /* 0.5 */, 0.500, 0.500 /* 0.5 */, 0.500,
387 0.500 /* 0.5 */, 0.500, 0.500 /* 0.5 */, 0.500,
389 float expected_data_red[width * height] = {
390 0.000, 0.000 /* 0.0 */, 0.250, 0.500 /* 0.5 */,
391 0.500, 0.500 /* 0.5 */, 0.750, 1.000 /* 1.0 */,
392 0.500, 0.500 /* 0.5 */, 0.500, 0.500 /* 0.5 */,
393 0.500, 0.500 /* 0.5 */, 0.500, 0.500 /* 0.5 */,
395 float out_data[width * height];
397 EffectChainTester tester(NULL, width, height);
400 format.color_space = COLORSPACE_sRGB;
401 format.gamma_curve = GAMMA_sRGB;
403 YCbCrFormat ycbcr_format;
404 ycbcr_format.luma_coefficients = YCBCR_REC_601;
405 ycbcr_format.full_range = false;
406 ycbcr_format.num_levels = 256;
407 ycbcr_format.chroma_subsampling_x = 2;
408 ycbcr_format.chroma_subsampling_y = 1;
409 ycbcr_format.cb_x_position = 0.0f;
410 ycbcr_format.cb_y_position = 0.5f;
411 ycbcr_format.cr_x_position = 1.0f;
412 ycbcr_format.cr_y_position = 0.5f;
414 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
415 input->set_pixel_data(0, y);
416 input->set_pixel_data(1, cb);
417 input->set_pixel_data(2, cr);
418 tester.get_chain()->add_input(input);
420 // Y'CbCr isn't 100% accurate (the input values are rounded),
421 // so we need some leeway.
422 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
423 expect_equal(expected_data_red, out_data, width, height, 0.02, 0.002);
425 tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
426 expect_equal(expected_data_blue, out_data, width, height, 0.01, 0.001);
429 TEST(YCbCrInputTest, PBO) {
431 const int height = 5;
433 // Pure-color test inputs, calculated with the formulas in Rec. 601
435 unsigned char data[width * height * 3] = {
436 16, 235, 81, 145, 41,
437 128, 128, 90, 54, 240,
438 128, 128, 240, 34, 110,
440 float expected_data[4 * width * height] = {
447 float out_data[4 * width * height];
450 glGenBuffers(1, &pbo);
451 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
452 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * 3, data, GL_STREAM_DRAW);
453 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
455 EffectChainTester tester(NULL, width, height);
458 format.color_space = COLORSPACE_sRGB;
459 format.gamma_curve = GAMMA_sRGB;
461 YCbCrFormat ycbcr_format;
462 ycbcr_format.luma_coefficients = YCBCR_REC_601;
463 ycbcr_format.full_range = false;
464 ycbcr_format.num_levels = 256;
465 ycbcr_format.chroma_subsampling_x = 1;
466 ycbcr_format.chroma_subsampling_y = 1;
467 ycbcr_format.cb_x_position = 0.5f;
468 ycbcr_format.cb_y_position = 0.5f;
469 ycbcr_format.cr_x_position = 0.5f;
470 ycbcr_format.cr_y_position = 0.5f;
472 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
473 input->set_pixel_data(0, (unsigned char *)BUFFER_OFFSET(0), pbo);
474 input->set_pixel_data(1, (unsigned char *)BUFFER_OFFSET(width * height), pbo);
475 input->set_pixel_data(2, (unsigned char *)BUFFER_OFFSET(width * height * 2), pbo);
476 tester.get_chain()->add_input(input);
478 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
480 // Y'CbCr isn't 100% accurate (the input values are rounded),
481 // so we need some leeway.
482 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
484 glDeleteBuffers(1, &pbo);
487 TEST(YCbCrInputTest, CombinedCbAndCr) {
489 const int height = 5;
491 // Pure-color test inputs, calculated with the formulas in Rec. 601
493 unsigned char y[width * height] = {
494 16, 235, 81, 145, 41,
496 unsigned char cb_cr[width * height * 2] = {
503 float expected_data[4 * width * height] = {
510 float out_data[4 * width * height];
512 EffectChainTester tester(NULL, width, height);
515 format.color_space = COLORSPACE_sRGB;
516 format.gamma_curve = GAMMA_sRGB;
518 YCbCrFormat ycbcr_format;
519 ycbcr_format.luma_coefficients = YCBCR_REC_601;
520 ycbcr_format.full_range = false;
521 ycbcr_format.num_levels = 256;
522 ycbcr_format.chroma_subsampling_x = 1;
523 ycbcr_format.chroma_subsampling_y = 1;
524 ycbcr_format.cb_x_position = 0.5f;
525 ycbcr_format.cb_y_position = 0.5f;
526 ycbcr_format.cr_x_position = 0.5f;
527 ycbcr_format.cr_y_position = 0.5f;
529 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
530 input->set_pixel_data(0, y);
531 input->set_pixel_data(1, cb_cr);
532 tester.get_chain()->add_input(input);
534 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
536 // Y'CbCr isn't 100% accurate (the input values are rounded),
537 // so we need some leeway.
538 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);