1 // Unit tests for YCbCrInput.
6 #include "effect_chain.h"
7 #include "gtest/gtest.h"
10 #include "ycbcr_input.h"
14 TEST(YCbCrInput, 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.chroma_subsampling_x = 1;
48 ycbcr_format.chroma_subsampling_y = 1;
49 ycbcr_format.cb_x_position = 0.5f;
50 ycbcr_format.cb_y_position = 0.5f;
51 ycbcr_format.cr_x_position = 0.5f;
52 ycbcr_format.cr_y_position = 0.5f;
54 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
55 input->set_pixel_data(0, y);
56 input->set_pixel_data(1, cb);
57 input->set_pixel_data(2, cr);
58 tester.get_chain()->add_input(input);
60 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
62 // Y'CbCr isn't 100% accurate (the input values are rounded),
63 // so we need some leeway.
64 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
67 TEST(YCbCrInput, FullRangeRec601) {
71 // Pure-color test inputs, calculated with the formulas in Rec. 601
72 // section 2.5.4 but without the scaling factors applied
73 // (so both R, G, B, Y, Cb and R vary from 0 to 255).
74 unsigned char y[width * height] = {
77 unsigned char cb[width * height] = {
78 128, 128, 85, 44, 255,
80 unsigned char cr[width * height] = {
81 128, 128, 255, 21, 107,
83 float expected_data[4 * width * height] = {
90 float out_data[4 * width * height];
92 EffectChainTester tester(NULL, width, height);
95 format.color_space = COLORSPACE_sRGB;
96 format.gamma_curve = GAMMA_sRGB;
98 YCbCrFormat ycbcr_format;
99 ycbcr_format.luma_coefficients = YCBCR_REC_601;
100 ycbcr_format.full_range = true;
101 ycbcr_format.chroma_subsampling_x = 1;
102 ycbcr_format.chroma_subsampling_y = 1;
103 ycbcr_format.cb_x_position = 0.5f;
104 ycbcr_format.cb_y_position = 0.5f;
105 ycbcr_format.cr_x_position = 0.5f;
106 ycbcr_format.cr_y_position = 0.5f;
108 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
109 input->set_pixel_data(0, y);
110 input->set_pixel_data(1, cb);
111 input->set_pixel_data(2, cr);
112 tester.get_chain()->add_input(input);
114 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
116 // Y'CbCr isn't 100% accurate (the input values are rounded),
117 // so we need some leeway.
118 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
121 TEST(YCbCrInput, Rec709) {
123 const int height = 5;
125 // Pure-color test inputs, calculated with the formulas in Rec. 709
126 // page 19, items 3.4 and 3.5.
127 unsigned char y[width * height] = {
128 16, 235, 63, 173, 32,
130 unsigned char cb[width * height] = {
131 128, 128, 102, 42, 240,
133 unsigned char cr[width * height] = {
134 128, 128, 240, 26, 118,
136 float expected_data[4 * width * height] = {
143 float out_data[4 * width * height];
145 EffectChainTester tester(NULL, width, height);
148 format.color_space = COLORSPACE_sRGB;
149 format.gamma_curve = GAMMA_sRGB;
151 YCbCrFormat ycbcr_format;
152 ycbcr_format.luma_coefficients = YCBCR_REC_709;
153 ycbcr_format.full_range = false;
154 ycbcr_format.chroma_subsampling_x = 1;
155 ycbcr_format.chroma_subsampling_y = 1;
156 ycbcr_format.cb_x_position = 0.5f;
157 ycbcr_format.cb_y_position = 0.5f;
158 ycbcr_format.cr_x_position = 0.5f;
159 ycbcr_format.cr_y_position = 0.5f;
161 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
162 input->set_pixel_data(0, y);
163 input->set_pixel_data(1, cb);
164 input->set_pixel_data(2, cr);
165 tester.get_chain()->add_input(input);
167 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
169 // Y'CbCr isn't 100% accurate (the input values are rounded),
170 // so we need some leeway.
171 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
174 TEST(YCbCrInput, Rec2020) {
176 const int height = 5;
178 // Pure-color test inputs, calculated with the formulas in Rec. 2020
179 // page 4, tables 4 and 5 (for conventional non-constant luminance).
180 // Note that we still use 8-bit inputs, even though Rec. 2020 is only
181 // defined for 10- and 12-bit.
182 unsigned char y[width * height] = {
183 16, 235, 74, 164, 29,
185 unsigned char cb[width * height] = {
186 128, 128, 97, 47, 240,
188 unsigned char cr[width * height] = {
189 128, 128, 240, 25, 119,
191 float expected_data[4 * width * height] = {
198 float out_data[4 * width * height];
200 EffectChainTester tester(NULL, width, height);
203 format.color_space = COLORSPACE_sRGB;
204 format.gamma_curve = GAMMA_sRGB;
206 YCbCrFormat ycbcr_format;
207 ycbcr_format.luma_coefficients = YCBCR_REC_2020;
208 ycbcr_format.full_range = false;
209 ycbcr_format.chroma_subsampling_x = 1;
210 ycbcr_format.chroma_subsampling_y = 1;
211 ycbcr_format.cb_x_position = 0.5f;
212 ycbcr_format.cb_y_position = 0.5f;
213 ycbcr_format.cr_x_position = 0.5f;
214 ycbcr_format.cr_y_position = 0.5f;
216 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
217 input->set_pixel_data(0, y);
218 input->set_pixel_data(1, cb);
219 input->set_pixel_data(2, cr);
220 tester.get_chain()->add_input(input);
222 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
224 // Y'CbCr isn't 100% accurate (the input values are rounded),
225 // so we need some leeway.
226 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
229 TEST(YCbCrInput, Subsampling420) {
231 const int height = 4;
233 unsigned char y[width * height] = {
239 unsigned char cb[(width/2) * (height/2)] = {
243 unsigned char cr[(width/2) * (height/2)] = {
248 // Note: This is only the blue channel. The chroma samples (with associated
249 // values for blue) are marked off in comments.
250 float expected_data[width * height] = {
251 0.000, 0.125, 0.375, 0.500,
253 0.125, 0.250, 0.500, 0.625,
255 0.375, 0.500, 0.750, 0.875,
257 0.500, 0.625, 0.875, 1.000,
259 float out_data[width * height];
261 EffectChainTester tester(NULL, width, height);
264 format.color_space = COLORSPACE_sRGB;
265 format.gamma_curve = GAMMA_sRGB;
267 YCbCrFormat ycbcr_format;
268 ycbcr_format.luma_coefficients = YCBCR_REC_601;
269 ycbcr_format.full_range = false;
270 ycbcr_format.chroma_subsampling_x = 2;
271 ycbcr_format.chroma_subsampling_y = 2;
272 ycbcr_format.cb_x_position = 0.5f;
273 ycbcr_format.cb_y_position = 0.5f;
274 ycbcr_format.cr_x_position = 0.5f;
275 ycbcr_format.cr_y_position = 0.5f;
277 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
278 input->set_pixel_data(0, y);
279 input->set_pixel_data(1, cb);
280 input->set_pixel_data(2, cr);
281 tester.get_chain()->add_input(input);
283 tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
285 // Y'CbCr isn't 100% accurate (the input values are rounded),
286 // so we need some leeway.
287 expect_equal(expected_data, out_data, width, height, 0.01, 0.001);
290 TEST(YCbCrInput, Subsampling420WithNonCenteredSamples) {
292 const int height = 4;
294 unsigned char y[width * height] = {
300 unsigned char cb[(width/2) * (height/2)] = {
304 unsigned char cr[(width/2) * (height/2)] = {
309 // Note: This is only the blue channel. The chroma samples (with associated
310 // values for blue) are marked off in comments.
311 float expected_data[width * height] = {
312 0.000, 0.250, 0.500, 0.500,
314 0.125, 0.375, 0.625, 0.625,
316 0.375, 0.625, 0.875, 0.875,
318 0.500, 0.750, 1.000, 1.000,
320 float out_data[width * height];
322 EffectChainTester tester(NULL, width, height);
325 format.color_space = COLORSPACE_sRGB;
326 format.gamma_curve = GAMMA_sRGB;
328 YCbCrFormat ycbcr_format;
329 ycbcr_format.luma_coefficients = YCBCR_REC_601;
330 ycbcr_format.full_range = false;
331 ycbcr_format.chroma_subsampling_x = 2;
332 ycbcr_format.chroma_subsampling_y = 2;
333 ycbcr_format.cb_x_position = 0.0f;
334 ycbcr_format.cb_y_position = 0.5f;
335 ycbcr_format.cr_x_position = 0.0f;
336 ycbcr_format.cr_y_position = 0.5f;
338 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
339 input->set_pixel_data(0, y);
340 input->set_pixel_data(1, cb);
341 input->set_pixel_data(2, cr);
342 tester.get_chain()->add_input(input);
344 tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
346 // Y'CbCr isn't 100% accurate (the input values are rounded),
347 // so we need some leeway.
348 expect_equal(expected_data, out_data, width, height, 0.01, 0.001);
351 // Yes, some 4:2:2 formats actually have this craziness.
352 TEST(YCbCrInput, DifferentCbAndCrPositioning) {
354 const int height = 4;
356 unsigned char y[width * height] = {
362 unsigned char cb[(width/2) * height] = {
368 unsigned char cr[(width/2) * height] = {
375 // Chroma samples in this csae are always co-sited with a luma sample;
376 // their associated color values and position are marked off in comments.
377 float expected_data_blue[width * height] = {
378 0.000 /* 0.0 */, 0.250, 0.500 /* 0.5 */, 0.500,
379 0.500 /* 0.5 */, 0.750, 1.000 /* 1.0 */, 1.000,
380 0.500 /* 0.5 */, 0.500, 0.500 /* 0.5 */, 0.500,
381 0.500 /* 0.5 */, 0.500, 0.500 /* 0.5 */, 0.500,
383 float expected_data_red[width * height] = {
384 0.000, 0.000 /* 0.0 */, 0.250, 0.500 /* 0.5 */,
385 0.500, 0.500 /* 0.5 */, 0.750, 1.000 /* 1.0 */,
386 0.500, 0.500 /* 0.5 */, 0.500, 0.500 /* 0.5 */,
387 0.500, 0.500 /* 0.5 */, 0.500, 0.500 /* 0.5 */,
389 float out_data[width * height];
391 EffectChainTester tester(NULL, width, height);
394 format.color_space = COLORSPACE_sRGB;
395 format.gamma_curve = GAMMA_sRGB;
397 YCbCrFormat ycbcr_format;
398 ycbcr_format.luma_coefficients = YCBCR_REC_601;
399 ycbcr_format.full_range = false;
400 ycbcr_format.chroma_subsampling_x = 2;
401 ycbcr_format.chroma_subsampling_y = 1;
402 ycbcr_format.cb_x_position = 0.0f;
403 ycbcr_format.cb_y_position = 0.5f;
404 ycbcr_format.cr_x_position = 1.0f;
405 ycbcr_format.cr_y_position = 0.5f;
407 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
408 input->set_pixel_data(0, y);
409 input->set_pixel_data(1, cb);
410 input->set_pixel_data(2, cr);
411 tester.get_chain()->add_input(input);
413 // Y'CbCr isn't 100% accurate (the input values are rounded),
414 // so we need some leeway.
415 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
416 expect_equal(expected_data_red, out_data, width, height, 0.02, 0.002);
418 tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
419 expect_equal(expected_data_blue, out_data, width, height, 0.01, 0.001);
422 TEST(YCbCrInput, PBO) {
424 const int height = 5;
426 // Pure-color test inputs, calculated with the formulas in Rec. 601
428 unsigned char data[width * height * 3] = {
429 16, 235, 81, 145, 41,
430 128, 128, 90, 54, 240,
431 128, 128, 240, 34, 110,
433 float expected_data[4 * width * height] = {
440 float out_data[4 * width * height];
443 glGenBuffers(1, &pbo);
444 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
445 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * 3, data, GL_STREAM_DRAW);
446 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
448 EffectChainTester tester(NULL, width, height);
451 format.color_space = COLORSPACE_sRGB;
452 format.gamma_curve = GAMMA_sRGB;
454 YCbCrFormat ycbcr_format;
455 ycbcr_format.luma_coefficients = YCBCR_REC_601;
456 ycbcr_format.full_range = false;
457 ycbcr_format.chroma_subsampling_x = 1;
458 ycbcr_format.chroma_subsampling_y = 1;
459 ycbcr_format.cb_x_position = 0.5f;
460 ycbcr_format.cb_y_position = 0.5f;
461 ycbcr_format.cr_x_position = 0.5f;
462 ycbcr_format.cr_y_position = 0.5f;
464 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
465 input->set_pixel_data(0, (unsigned char *)BUFFER_OFFSET(0), pbo);
466 input->set_pixel_data(1, (unsigned char *)BUFFER_OFFSET(width * height), pbo);
467 input->set_pixel_data(2, (unsigned char *)BUFFER_OFFSET(width * height * 2), pbo);
468 tester.get_chain()->add_input(input);
470 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
472 // Y'CbCr isn't 100% accurate (the input values are rounded),
473 // so we need some leeway.
474 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
476 glDeleteBuffers(1, &pbo);