]> git.sesse.net Git - movit/blob - ycbcr_input_test.cpp
Small whitespace fix.
[movit] / ycbcr_input_test.cpp
1 // Unit tests for YCbCrInput.
2
3 #include <epoxy/gl.h>
4 #include <stddef.h>
5
6 #include "effect_chain.h"
7 #include "gtest/gtest.h"
8 #include "test_util.h"
9 #include "util.h"
10 #include "ycbcr_input.h"
11
12 namespace movit {
13
14 TEST(YCbCrInputTest, Simple444) {
15         const int width = 1;
16         const int height = 5;
17
18         // Pure-color test inputs, calculated with the formulas in Rec. 601
19         // section 2.5.4.
20         unsigned char y[width * height] = {
21                 16, 235, 81, 145, 41,
22         };
23         unsigned char cb[width * height] = {
24                 128, 128, 90, 54, 240,
25         };
26         unsigned char cr[width * height] = {
27                 128, 128, 240, 34, 110,
28         };
29         float expected_data[4 * width * height] = {
30                 0.0, 0.0, 0.0, 1.0,
31                 1.0, 1.0, 1.0, 1.0,
32                 1.0, 0.0, 0.0, 1.0,
33                 0.0, 1.0, 0.0, 1.0,
34                 0.0, 0.0, 1.0, 1.0,
35         };
36         float out_data[4 * width * height];
37
38         EffectChainTester tester(NULL, width, height);
39
40         ImageFormat format;
41         format.color_space = COLORSPACE_sRGB;
42         format.gamma_curve = GAMMA_sRGB;
43
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;
53
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);
59
60         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
61
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);
65 }
66
67 TEST(YCbCrInputTest, FullRangeRec601) {
68         const int width = 1;
69         const int height = 5;
70
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] = {
75                 0, 255, 76, 150, 29,
76         };
77         unsigned char cb[width * height] = {
78                 128, 128, 85, 44, 255,
79         };
80         unsigned char cr[width * height] = {
81                 128, 128, 255, 21, 107,
82         };
83         float expected_data[4 * width * height] = {
84                 0.0, 0.0, 0.0, 1.0,
85                 1.0, 1.0, 1.0, 1.0,
86                 1.0, 0.0, 0.0, 1.0,
87                 0.0, 1.0, 0.0, 1.0,
88                 0.0, 0.0, 1.0, 1.0,
89         };
90         float out_data[4 * width * height];
91
92         EffectChainTester tester(NULL, width, height);
93
94         ImageFormat format;
95         format.color_space = COLORSPACE_sRGB;
96         format.gamma_curve = GAMMA_sRGB;
97
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;
107
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);
113
114         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
115
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);
119 }
120
121 TEST(YCbCrInputTest, Rec709) {
122         const int width = 1;
123         const int height = 5;
124
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, 
129         };
130         unsigned char cb[width * height] = {
131                 128, 128, 102, 42, 240,
132         };
133         unsigned char cr[width * height] = {
134                 128, 128, 240, 26, 118,
135         };
136         float expected_data[4 * width * height] = {
137                 0.0, 0.0, 0.0, 1.0,
138                 1.0, 1.0, 1.0, 1.0,
139                 1.0, 0.0, 0.0, 1.0,
140                 0.0, 1.0, 0.0, 1.0,
141                 0.0, 0.0, 1.0, 1.0,
142         };
143         float out_data[4 * width * height];
144
145         EffectChainTester tester(NULL, width, height);
146
147         ImageFormat format;
148         format.color_space = COLORSPACE_sRGB;
149         format.gamma_curve = GAMMA_sRGB;
150
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;
160
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);
166
167         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
168
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);
172 }
173
174 TEST(YCbCrInputTest, Rec2020) {
175         const int width = 1;
176         const int height = 5;
177
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,
184         };
185         unsigned char cb[width * height] = {
186                 128, 128, 97, 47, 240,
187         };
188         unsigned char cr[width * height] = {
189                 128, 128, 240, 25, 119,
190         };
191         float expected_data[4 * width * height] = {
192                 0.0, 0.0, 0.0, 1.0,
193                 1.0, 1.0, 1.0, 1.0,
194                 1.0, 0.0, 0.0, 1.0,
195                 0.0, 1.0, 0.0, 1.0,
196                 0.0, 0.0, 1.0, 1.0,
197         };
198         float out_data[4 * width * height];
199
200         EffectChainTester tester(NULL, width, height);
201
202         ImageFormat format;
203         format.color_space = COLORSPACE_sRGB;
204         format.gamma_curve = GAMMA_sRGB;
205
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;
215
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);
221
222         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
223
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);
227 }
228
229 TEST(YCbCrInputTest, Subsampling420) {
230         const int width = 4;
231         const int height = 4;
232
233         unsigned char y[width * height] = {
234                 126, 126, 126, 126,
235                 126, 126, 126, 126,
236                 126, 126, 126, 126,
237                 126, 126, 126, 126,
238         };
239         unsigned char cb[(width/2) * (height/2)] = {
240                 64, 128,
241                 128, 192,
242         };
243         unsigned char cr[(width/2) * (height/2)] = {
244                 128, 128,
245                 128, 128,
246         };
247
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, 
252                  /* 0.0 */      /* 0.5 */
253                 0.125, 0.250, 0.500, 0.625,
254
255                 0.375, 0.500, 0.750, 0.875,
256                  /* 0.5 */      /* 1.0 */
257                 0.500, 0.625, 0.875, 1.000,
258         };
259         float out_data[width * height];
260
261         EffectChainTester tester(NULL, width, height);
262
263         ImageFormat format;
264         format.color_space = COLORSPACE_sRGB;
265         format.gamma_curve = GAMMA_sRGB;
266
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;
276
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);
282
283         tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
284
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);
288 }
289
290 TEST(YCbCrInputTest, Subsampling420WithNonCenteredSamples) {
291         const int width = 4;
292         const int height = 4;
293
294         unsigned char y[width * height] = {
295                 126, 126, 126, 126,
296                 126, 126, 126, 126,
297                 126, 126, 126, 126,
298                 126, 126, 126, 126,
299         };
300         unsigned char cb[(width/2) * (height/2)] = {
301                 64, 128,
302                 128, 192,
303         };
304         unsigned char cr[(width/2) * (height/2)] = {
305                 128, 128,
306                 128, 128,
307         };
308
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, 
313                 /* 0.0 */     /* 0.5 */
314                    0.125, 0.375, 0.625, 0.625,
315
316                    0.375, 0.625, 0.875, 0.875,
317                 /* 0.5 */     /* 1.0 */
318                    0.500, 0.750, 1.000, 1.000,
319         };
320         float out_data[width * height];
321
322         EffectChainTester tester(NULL, width, height);
323
324         ImageFormat format;
325         format.color_space = COLORSPACE_sRGB;
326         format.gamma_curve = GAMMA_sRGB;
327
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;
337
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);
343
344         tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
345
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);
349 }
350
351 // Yes, some 4:2:2 formats actually have this craziness.
352 TEST(YCbCrInputTest, DifferentCbAndCrPositioning) {
353         const int width = 4;
354         const int height = 4;
355
356         unsigned char y[width * height] = {
357                 126, 126, 126, 126,
358                 126, 126, 126, 126,
359                 126, 126, 126, 126,
360                 126, 126, 126, 126,
361         };
362         unsigned char cb[(width/2) * height] = {
363                 64, 128,
364                 128, 192,
365                 128, 128,
366                 128, 128,
367         };
368         unsigned char cr[(width/2) * height] = {
369                 48, 128,
370                 128, 208,
371                 128, 128,
372                 128, 128,
373         };
374
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, 
382         };
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 */, 
388         };
389         float out_data[width * height];
390
391         EffectChainTester tester(NULL, width, height);
392
393         ImageFormat format;
394         format.color_space = COLORSPACE_sRGB;
395         format.gamma_curve = GAMMA_sRGB;
396
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;
406
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);
412
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);
417
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);
420 }
421
422 TEST(YCbCrInputTest, PBO) {
423         const int width = 1;
424         const int height = 5;
425
426         // Pure-color test inputs, calculated with the formulas in Rec. 601
427         // section 2.5.4.
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,
432         };
433         float expected_data[4 * width * height] = {
434                 0.0, 0.0, 0.0, 1.0,
435                 1.0, 1.0, 1.0, 1.0,
436                 1.0, 0.0, 0.0, 1.0,
437                 0.0, 1.0, 0.0, 1.0,
438                 0.0, 0.0, 1.0, 1.0,
439         };
440         float out_data[4 * width * height];
441
442         GLuint pbo;
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);
447
448         EffectChainTester tester(NULL, width, height);
449
450         ImageFormat format;
451         format.color_space = COLORSPACE_sRGB;
452         format.gamma_curve = GAMMA_sRGB;
453
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;
463
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);
469
470         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
471
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);
475
476         glDeleteBuffers(1, &pbo);
477 }
478
479 }  // namespace movit