]> git.sesse.net Git - movit/blob - ycbcr_input_test.cpp
Add deinterlace_effect_test from .gitignore. Reported by Christophe Thommeret.
[movit] / ycbcr_input_test.cpp
1 // Unit tests for YCbCrInput. Also tests the matrix functions in ycbcr.cpp directly.
2
3 #include <epoxy/gl.h>
4 #include <stddef.h>
5
6 #include <Eigen/Core>
7 #include <Eigen/LU>
8
9 #include "effect_chain.h"
10 #include "gtest/gtest.h"
11 #include "test_util.h"
12 #include "util.h"
13 #include "resource_pool.h"
14 #include "ycbcr_input.h"
15
16 namespace movit {
17
18 TEST(YCbCrInputTest, Simple444) {
19         const int width = 1;
20         const int height = 5;
21
22         // Pure-color test inputs, calculated with the formulas in Rec. 601
23         // section 2.5.4.
24         unsigned char y[width * height] = {
25                 16, 235, 81, 145, 41,
26         };
27         unsigned char cb[width * height] = {
28                 128, 128, 90, 54, 240,
29         };
30         unsigned char cr[width * height] = {
31                 128, 128, 240, 34, 110,
32         };
33         float expected_data[4 * width * height] = {
34                 0.0, 0.0, 0.0, 1.0,
35                 1.0, 1.0, 1.0, 1.0,
36                 1.0, 0.0, 0.0, 1.0,
37                 0.0, 1.0, 0.0, 1.0,
38                 0.0, 0.0, 1.0, 1.0,
39         };
40         float out_data[4 * width * height];
41
42         EffectChainTester tester(NULL, width, height);
43
44         ImageFormat format;
45         format.color_space = COLORSPACE_sRGB;
46         format.gamma_curve = GAMMA_sRGB;
47
48         YCbCrFormat ycbcr_format;
49         ycbcr_format.luma_coefficients = YCBCR_REC_601;
50         ycbcr_format.full_range = false;
51         ycbcr_format.num_levels = 256;
52         ycbcr_format.chroma_subsampling_x = 1;
53         ycbcr_format.chroma_subsampling_y = 1;
54         ycbcr_format.cb_x_position = 0.5f;
55         ycbcr_format.cb_y_position = 0.5f;
56         ycbcr_format.cr_x_position = 0.5f;
57         ycbcr_format.cr_y_position = 0.5f;
58
59         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
60         input->set_pixel_data(0, y);
61         input->set_pixel_data(1, cb);
62         input->set_pixel_data(2, cr);
63         tester.get_chain()->add_input(input);
64
65         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
66
67         // Y'CbCr isn't 100% accurate (the input values are rounded),
68         // so we need some leeway.
69         expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
70 }
71
72 TEST(YCbCrInputTest, FullRangeRec601) {
73         const int width = 1;
74         const int height = 5;
75
76         // Pure-color test inputs, calculated with the formulas in Rec. 601
77         // section 2.5.4 but without the scaling factors applied
78         // (so both R, G, B, Y, Cb and R vary from 0 to 255).
79         unsigned char y[width * height] = {
80                 0, 255, 76, 150, 29,
81         };
82         unsigned char cb[width * height] = {
83                 128, 128, 85, 44, 255,
84         };
85         unsigned char cr[width * height] = {
86                 128, 128, 255, 21, 107,
87         };
88         float expected_data[4 * width * height] = {
89                 0.0, 0.0, 0.0, 1.0,
90                 1.0, 1.0, 1.0, 1.0,
91                 1.0, 0.0, 0.0, 1.0,
92                 0.0, 1.0, 0.0, 1.0,
93                 0.0, 0.0, 1.0, 1.0,
94         };
95         float out_data[4 * width * height];
96
97         EffectChainTester tester(NULL, width, height);
98
99         ImageFormat format;
100         format.color_space = COLORSPACE_sRGB;
101         format.gamma_curve = GAMMA_sRGB;
102
103         YCbCrFormat ycbcr_format;
104         ycbcr_format.luma_coefficients = YCBCR_REC_601;
105         ycbcr_format.full_range = true;
106         ycbcr_format.num_levels = 256;
107         ycbcr_format.chroma_subsampling_x = 1;
108         ycbcr_format.chroma_subsampling_y = 1;
109         ycbcr_format.cb_x_position = 0.5f;
110         ycbcr_format.cb_y_position = 0.5f;
111         ycbcr_format.cr_x_position = 0.5f;
112         ycbcr_format.cr_y_position = 0.5f;
113
114         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
115         input->set_pixel_data(0, y);
116         input->set_pixel_data(1, cb);
117         input->set_pixel_data(2, cr);
118         tester.get_chain()->add_input(input);
119
120         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
121
122         // Y'CbCr isn't 100% accurate (the input values are rounded),
123         // so we need some leeway.
124         expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
125 }
126
127 TEST(YCbCrInputTest, Rec709) {
128         const int width = 1;
129         const int height = 5;
130
131         // Pure-color test inputs, calculated with the formulas in Rec. 709
132         // page 19, items 3.4 and 3.5.
133         unsigned char y[width * height] = {
134                 16, 235, 63, 173, 32, 
135         };
136         unsigned char cb[width * height] = {
137                 128, 128, 102, 42, 240,
138         };
139         unsigned char cr[width * height] = {
140                 128, 128, 240, 26, 118,
141         };
142         float expected_data[4 * width * height] = {
143                 0.0, 0.0, 0.0, 1.0,
144                 1.0, 1.0, 1.0, 1.0,
145                 1.0, 0.0, 0.0, 1.0,
146                 0.0, 1.0, 0.0, 1.0,
147                 0.0, 0.0, 1.0, 1.0,
148         };
149         float out_data[4 * width * height];
150
151         EffectChainTester tester(NULL, width, height);
152
153         ImageFormat format;
154         format.color_space = COLORSPACE_sRGB;
155         format.gamma_curve = GAMMA_sRGB;
156
157         YCbCrFormat ycbcr_format;
158         ycbcr_format.luma_coefficients = YCBCR_REC_709;
159         ycbcr_format.full_range = false;
160         ycbcr_format.num_levels = 256;
161         ycbcr_format.chroma_subsampling_x = 1;
162         ycbcr_format.chroma_subsampling_y = 1;
163         ycbcr_format.cb_x_position = 0.5f;
164         ycbcr_format.cb_y_position = 0.5f;
165         ycbcr_format.cr_x_position = 0.5f;
166         ycbcr_format.cr_y_position = 0.5f;
167
168         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
169         input->set_pixel_data(0, y);
170         input->set_pixel_data(1, cb);
171         input->set_pixel_data(2, cr);
172         tester.get_chain()->add_input(input);
173
174         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
175
176         // Y'CbCr isn't 100% accurate (the input values are rounded),
177         // so we need some leeway.
178         expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
179 }
180
181 TEST(YCbCrInputTest, Rec2020) {
182         const int width = 1;
183         const int height = 5;
184
185         // Pure-color test inputs, calculated with the formulas in Rec. 2020
186         // page 4, tables 4 and 5 (for conventional non-constant luminance).
187         // Note that we still use 8-bit inputs, even though Rec. 2020 is only
188         // defined for 10- and 12-bit.
189         unsigned char y[width * height] = {
190                 16, 235, 74, 164, 29,
191         };
192         unsigned char cb[width * height] = {
193                 128, 128, 97, 47, 240,
194         };
195         unsigned char cr[width * height] = {
196                 128, 128, 240, 25, 119,
197         };
198         float expected_data[4 * width * height] = {
199                 0.0, 0.0, 0.0, 1.0,
200                 1.0, 1.0, 1.0, 1.0,
201                 1.0, 0.0, 0.0, 1.0,
202                 0.0, 1.0, 0.0, 1.0,
203                 0.0, 0.0, 1.0, 1.0,
204         };
205         float out_data[4 * width * height];
206
207         EffectChainTester tester(NULL, width, height);
208
209         ImageFormat format;
210         format.color_space = COLORSPACE_sRGB;
211         format.gamma_curve = GAMMA_sRGB;
212
213         YCbCrFormat ycbcr_format;
214         ycbcr_format.luma_coefficients = YCBCR_REC_2020;
215         ycbcr_format.full_range = false;
216         ycbcr_format.num_levels = 256;
217         ycbcr_format.chroma_subsampling_x = 1;
218         ycbcr_format.chroma_subsampling_y = 1;
219         ycbcr_format.cb_x_position = 0.5f;
220         ycbcr_format.cb_y_position = 0.5f;
221         ycbcr_format.cr_x_position = 0.5f;
222         ycbcr_format.cr_y_position = 0.5f;
223
224         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
225         input->set_pixel_data(0, y);
226         input->set_pixel_data(1, cb);
227         input->set_pixel_data(2, cr);
228         tester.get_chain()->add_input(input);
229
230         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
231
232         // Y'CbCr isn't 100% accurate (the input values are rounded),
233         // so we need some leeway.
234         expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
235 }
236
237 TEST(YCbCrInputTest, Subsampling420) {
238         const int width = 4;
239         const int height = 4;
240
241         unsigned char y[width * height] = {
242                 126, 126, 126, 126,
243                 126, 126, 126, 126,
244                 126, 126, 126, 126,
245                 126, 126, 126, 126,
246         };
247         unsigned char cb[(width/2) * (height/2)] = {
248                 64, 128,
249                 128, 192,
250         };
251         unsigned char cr[(width/2) * (height/2)] = {
252                 128, 128,
253                 128, 128,
254         };
255
256         // Note: This is only the blue channel. The chroma samples (with associated
257         // values for blue) are marked off in comments.
258         float expected_data[width * height] = {
259                 0.000, 0.125, 0.375, 0.500, 
260                  /* 0.0 */      /* 0.5 */
261                 0.125, 0.250, 0.500, 0.625,
262
263                 0.375, 0.500, 0.750, 0.875,
264                  /* 0.5 */      /* 1.0 */
265                 0.500, 0.625, 0.875, 1.000,
266         };
267         float out_data[width * height];
268
269         EffectChainTester tester(NULL, width, height);
270
271         ImageFormat format;
272         format.color_space = COLORSPACE_sRGB;
273         format.gamma_curve = GAMMA_sRGB;
274
275         YCbCrFormat ycbcr_format;
276         ycbcr_format.luma_coefficients = YCBCR_REC_601;
277         ycbcr_format.full_range = false;
278         ycbcr_format.num_levels = 256;
279         ycbcr_format.chroma_subsampling_x = 2;
280         ycbcr_format.chroma_subsampling_y = 2;
281         ycbcr_format.cb_x_position = 0.5f;
282         ycbcr_format.cb_y_position = 0.5f;
283         ycbcr_format.cr_x_position = 0.5f;
284         ycbcr_format.cr_y_position = 0.5f;
285
286         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
287         input->set_pixel_data(0, y);
288         input->set_pixel_data(1, cb);
289         input->set_pixel_data(2, cr);
290         tester.get_chain()->add_input(input);
291
292         tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
293
294         // Y'CbCr isn't 100% accurate (the input values are rounded),
295         // so we need some leeway.
296         expect_equal(expected_data, out_data, width, height, 0.01, 0.001);
297 }
298
299 TEST(YCbCrInputTest, Subsampling420WithNonCenteredSamples) {
300         const int width = 4;
301         const int height = 4;
302
303         unsigned char y[width * height] = {
304                 126, 126, 126, 126,
305                 126, 126, 126, 126,
306                 126, 126, 126, 126,
307                 126, 126, 126, 126,
308         };
309         unsigned char cb[(width/2) * (height/2)] = {
310                 64, 128,
311                 128, 192,
312         };
313         unsigned char cr[(width/2) * (height/2)] = {
314                 128, 128,
315                 128, 128,
316         };
317
318         // Note: This is only the blue channel. The chroma samples (with associated
319         // values for blue) are marked off in comments.
320         float expected_data[width * height] = {
321                    0.000, 0.250, 0.500, 0.500, 
322                 /* 0.0 */     /* 0.5 */
323                    0.125, 0.375, 0.625, 0.625,
324
325                    0.375, 0.625, 0.875, 0.875,
326                 /* 0.5 */     /* 1.0 */
327                    0.500, 0.750, 1.000, 1.000,
328         };
329         float out_data[width * height];
330
331         EffectChainTester tester(NULL, width, height);
332
333         ImageFormat format;
334         format.color_space = COLORSPACE_sRGB;
335         format.gamma_curve = GAMMA_sRGB;
336
337         YCbCrFormat ycbcr_format;
338         ycbcr_format.luma_coefficients = YCBCR_REC_601;
339         ycbcr_format.full_range = false;
340         ycbcr_format.num_levels = 256;
341         ycbcr_format.chroma_subsampling_x = 2;
342         ycbcr_format.chroma_subsampling_y = 2;
343         ycbcr_format.cb_x_position = 0.0f;
344         ycbcr_format.cb_y_position = 0.5f;
345         ycbcr_format.cr_x_position = 0.0f;
346         ycbcr_format.cr_y_position = 0.5f;
347
348         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
349         input->set_pixel_data(0, y);
350         input->set_pixel_data(1, cb);
351         input->set_pixel_data(2, cr);
352         tester.get_chain()->add_input(input);
353
354         tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
355
356         // Y'CbCr isn't 100% accurate (the input values are rounded),
357         // so we need some leeway.
358         expect_equal(expected_data, out_data, width, height, 0.01, 0.0012);
359 }
360
361 // Yes, some 4:2:2 formats actually have this craziness.
362 TEST(YCbCrInputTest, DifferentCbAndCrPositioning) {
363         const int width = 4;
364         const int height = 4;
365
366         unsigned char y[width * height] = {
367                 126, 126, 126, 126,
368                 126, 126, 126, 126,
369                 126, 126, 126, 126,
370                 126, 126, 126, 126,
371         };
372         unsigned char cb[(width/2) * height] = {
373                 64, 128,
374                 128, 192,
375                 128, 128,
376                 128, 128,
377         };
378         unsigned char cr[(width/2) * height] = {
379                 48, 128,
380                 128, 208,
381                 128, 128,
382                 128, 128,
383         };
384
385         // Chroma samples in this csae are always co-sited with a luma sample;
386         // their associated color values and position are marked off in comments.
387         float expected_data_blue[width * height] = {
388                    0.000 /* 0.0 */, 0.250,           0.500 /* 0.5 */, 0.500, 
389                    0.500 /* 0.5 */, 0.750,           1.000 /* 1.0 */, 1.000, 
390                    0.500 /* 0.5 */, 0.500,           0.500 /* 0.5 */, 0.500, 
391                    0.500 /* 0.5 */, 0.500,           0.500 /* 0.5 */, 0.500, 
392         };
393         float expected_data_red[width * height] = {
394                    0.000,           0.000 /* 0.0 */, 0.250,           0.500 /* 0.5 */, 
395                    0.500,           0.500 /* 0.5 */, 0.750,           1.000 /* 1.0 */, 
396                    0.500,           0.500 /* 0.5 */, 0.500,           0.500 /* 0.5 */, 
397                    0.500,           0.500 /* 0.5 */, 0.500,           0.500 /* 0.5 */, 
398         };
399         float out_data[width * height];
400
401         EffectChainTester tester(NULL, width, height);
402
403         ImageFormat format;
404         format.color_space = COLORSPACE_sRGB;
405         format.gamma_curve = GAMMA_sRGB;
406
407         YCbCrFormat ycbcr_format;
408         ycbcr_format.luma_coefficients = YCBCR_REC_601;
409         ycbcr_format.full_range = false;
410         ycbcr_format.num_levels = 256;
411         ycbcr_format.chroma_subsampling_x = 2;
412         ycbcr_format.chroma_subsampling_y = 1;
413         ycbcr_format.cb_x_position = 0.0f;
414         ycbcr_format.cb_y_position = 0.5f;
415         ycbcr_format.cr_x_position = 1.0f;
416         ycbcr_format.cr_y_position = 0.5f;
417
418         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
419         input->set_pixel_data(0, y);
420         input->set_pixel_data(1, cb);
421         input->set_pixel_data(2, cr);
422         tester.get_chain()->add_input(input);
423
424         // Y'CbCr isn't 100% accurate (the input values are rounded),
425         // so we need some leeway.
426         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
427         expect_equal(expected_data_red, out_data, width, height, 0.02, 0.002);
428
429         tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
430         expect_equal(expected_data_blue, out_data, width, height, 0.01, 0.001);
431 }
432
433 TEST(YCbCrInputTest, PBO) {
434         const int width = 1;
435         const int height = 5;
436
437         // Pure-color test inputs, calculated with the formulas in Rec. 601
438         // section 2.5.4.
439         unsigned char data[width * height * 3] = {
440                 16, 235, 81, 145, 41,
441                 128, 128, 90, 54, 240,
442                 128, 128, 240, 34, 110,
443         };
444         float expected_data[4 * width * height] = {
445                 0.0, 0.0, 0.0, 1.0,
446                 1.0, 1.0, 1.0, 1.0,
447                 1.0, 0.0, 0.0, 1.0,
448                 0.0, 1.0, 0.0, 1.0,
449                 0.0, 0.0, 1.0, 1.0,
450         };
451         float out_data[4 * width * height];
452
453         GLuint pbo;
454         glGenBuffers(1, &pbo);
455         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
456         glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * 3, data, GL_STREAM_DRAW);
457         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
458
459         EffectChainTester tester(NULL, width, height);
460
461         ImageFormat format;
462         format.color_space = COLORSPACE_sRGB;
463         format.gamma_curve = GAMMA_sRGB;
464
465         YCbCrFormat ycbcr_format;
466         ycbcr_format.luma_coefficients = YCBCR_REC_601;
467         ycbcr_format.full_range = false;
468         ycbcr_format.num_levels = 256;
469         ycbcr_format.chroma_subsampling_x = 1;
470         ycbcr_format.chroma_subsampling_y = 1;
471         ycbcr_format.cb_x_position = 0.5f;
472         ycbcr_format.cb_y_position = 0.5f;
473         ycbcr_format.cr_x_position = 0.5f;
474         ycbcr_format.cr_y_position = 0.5f;
475
476         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
477         input->set_pixel_data(0, (unsigned char *)BUFFER_OFFSET(0), pbo);
478         input->set_pixel_data(1, (unsigned char *)BUFFER_OFFSET(width * height), pbo);
479         input->set_pixel_data(2, (unsigned char *)BUFFER_OFFSET(width * height * 2), pbo);
480         tester.get_chain()->add_input(input);
481
482         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
483
484         // Y'CbCr isn't 100% accurate (the input values are rounded),
485         // so we need some leeway.
486         expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
487
488         glDeleteBuffers(1, &pbo);
489 }
490
491 TEST(YCbCrInputTest, CombinedCbAndCr) {
492         const int width = 1;
493         const int height = 5;
494
495         // Pure-color test inputs, calculated with the formulas in Rec. 601
496         // section 2.5.4.
497         unsigned char y[width * height] = {
498                 16, 235, 81, 145, 41,
499         };
500         unsigned char cb_cr[width * height * 2] = {
501                 128, 128,
502                 128, 128,
503                  90, 240,
504                  54,  34,
505                 240, 110,
506         };
507         float expected_data[4 * width * height] = {
508                 0.0, 0.0, 0.0, 1.0,
509                 1.0, 1.0, 1.0, 1.0,
510                 1.0, 0.0, 0.0, 1.0,
511                 0.0, 1.0, 0.0, 1.0,
512                 0.0, 0.0, 1.0, 1.0,
513         };
514         float out_data[4 * width * height];
515
516         EffectChainTester tester(NULL, width, height);
517
518         ImageFormat format;
519         format.color_space = COLORSPACE_sRGB;
520         format.gamma_curve = GAMMA_sRGB;
521
522         YCbCrFormat ycbcr_format;
523         ycbcr_format.luma_coefficients = YCBCR_REC_601;
524         ycbcr_format.full_range = false;
525         ycbcr_format.num_levels = 256;
526         ycbcr_format.chroma_subsampling_x = 1;
527         ycbcr_format.chroma_subsampling_y = 1;
528         ycbcr_format.cb_x_position = 0.5f;
529         ycbcr_format.cb_y_position = 0.5f;
530         ycbcr_format.cr_x_position = 0.5f;
531         ycbcr_format.cr_y_position = 0.5f;
532
533         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
534         input->set_pixel_data(0, y);
535         input->set_pixel_data(1, cb_cr);
536         tester.get_chain()->add_input(input);
537
538         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
539
540         // Y'CbCr isn't 100% accurate (the input values are rounded),
541         // so we need some leeway.
542         expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
543 }
544
545 TEST(YCbCrInputTest, ExternalTexture) {
546         const int width = 1;
547         const int height = 5;
548
549         // Pure-color test inputs, calculated with the formulas in Rec. 601
550         // section 2.5.4.
551         unsigned char y[width * height] = {
552                 16, 235, 81, 145, 41,
553         };
554         unsigned char cb[width * height] = {
555                 128, 128, 90, 54, 240,
556         };
557         unsigned char cr[width * height] = {
558                 128, 128, 240, 34, 110,
559         };
560         float expected_data[4 * width * height] = {
561                 0.0, 0.0, 0.0, 1.0,
562                 1.0, 1.0, 1.0, 1.0,
563                 1.0, 0.0, 0.0, 1.0,
564                 0.0, 1.0, 0.0, 1.0,
565                 0.0, 0.0, 1.0, 1.0,
566         };
567         float out_data[4 * width * height];
568
569         EffectChainTester tester(NULL, width, height);
570
571         ImageFormat format;
572         format.color_space = COLORSPACE_sRGB;
573         format.gamma_curve = GAMMA_sRGB;
574
575         YCbCrFormat ycbcr_format;
576         ycbcr_format.luma_coefficients = YCBCR_REC_601;
577         ycbcr_format.full_range = false;
578         ycbcr_format.num_levels = 256;
579         ycbcr_format.chroma_subsampling_x = 1;
580         ycbcr_format.chroma_subsampling_y = 1;
581         ycbcr_format.cb_x_position = 0.5f;
582         ycbcr_format.cb_y_position = 0.5f;
583         ycbcr_format.cr_x_position = 0.5f;
584         ycbcr_format.cr_y_position = 0.5f;
585
586         // Make a texture for the Cb data; keep the others as regular uploads.
587         ResourcePool pool;
588         GLuint cb_tex = pool.create_2d_texture(GL_R8, width, height);
589         check_error();
590         glBindTexture(GL_TEXTURE_2D, cb_tex);
591         check_error();
592         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
593         check_error();
594         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
595         check_error();
596         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, cb);
597         check_error();
598         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
599         check_error();
600         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
601         check_error();
602
603         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
604         input->set_pixel_data(0, y);
605         input->set_texture_num(1, cb_tex);
606         input->set_pixel_data(2, cr);
607         tester.get_chain()->add_input(input);
608
609         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
610
611         pool.release_2d_texture(cb_tex);
612
613         // Y'CbCr isn't 100% accurate (the input values are rounded),
614         // so we need some leeway.
615         expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
616 }
617
618 TEST(YCbCrTest, WikipediaRec601ForwardMatrix) {
619         YCbCrFormat ycbcr_format;
620         ycbcr_format.luma_coefficients = YCBCR_REC_601;
621         ycbcr_format.full_range = false;
622         ycbcr_format.num_levels = 256;
623
624         float offset[3];
625         Eigen::Matrix3d ycbcr_to_rgb;
626         compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
627
628         Eigen::Matrix3d rgb_to_ycbcr = ycbcr_to_rgb.inverse() * 255.0;
629
630         // Values from https://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.601_conversion.
631         EXPECT_NEAR(  65.481, rgb_to_ycbcr(0,0), 1e-3);
632         EXPECT_NEAR( 128.553, rgb_to_ycbcr(0,1), 1e-3);
633         EXPECT_NEAR(  24.966, rgb_to_ycbcr(0,2), 1e-3);
634
635         EXPECT_NEAR( -37.797, rgb_to_ycbcr(1,0), 1e-3);
636         EXPECT_NEAR( -74.203, rgb_to_ycbcr(1,1), 1e-3);
637         EXPECT_NEAR( 112.000, rgb_to_ycbcr(1,2), 1e-3);
638
639         EXPECT_NEAR( 112.000, rgb_to_ycbcr(2,0), 1e-3);
640         EXPECT_NEAR( -93.786, rgb_to_ycbcr(2,1), 1e-3);
641         EXPECT_NEAR( -18.214, rgb_to_ycbcr(2,2), 1e-3);
642
643         EXPECT_NEAR( 16.0, offset[0] * 255.0, 1e-3);
644         EXPECT_NEAR(128.0, offset[1] * 255.0, 1e-3);
645         EXPECT_NEAR(128.0, offset[2] * 255.0, 1e-3);
646 }
647
648 TEST(YCbCrTest, WikipediaJPEGMatrices) {
649         YCbCrFormat ycbcr_format;
650         ycbcr_format.luma_coefficients = YCBCR_REC_601;
651         ycbcr_format.full_range = true;
652         ycbcr_format.num_levels = 256;
653
654         float offset[3];
655         Eigen::Matrix3d ycbcr_to_rgb;
656         compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
657
658         // Values from https://en.wikipedia.org/wiki/YCbCr#JPEG_conversion.
659         EXPECT_NEAR( 1.00000, ycbcr_to_rgb(0,0), 1e-5);
660         EXPECT_NEAR( 0.00000, ycbcr_to_rgb(0,1), 1e-5);
661         EXPECT_NEAR( 1.40200, ycbcr_to_rgb(0,2), 1e-5);
662
663         EXPECT_NEAR( 1.00000, ycbcr_to_rgb(1,0), 1e-5);
664         EXPECT_NEAR(-0.34414, ycbcr_to_rgb(1,1), 1e-5);
665         EXPECT_NEAR(-0.71414, ycbcr_to_rgb(1,2), 1e-5);
666
667         EXPECT_NEAR( 1.00000, ycbcr_to_rgb(2,0), 1e-5);
668         EXPECT_NEAR( 1.77200, ycbcr_to_rgb(2,1), 1e-5);
669         EXPECT_NEAR( 0.00000, ycbcr_to_rgb(2,2), 1e-5);
670
671         Eigen::Matrix3d rgb_to_ycbcr = ycbcr_to_rgb.inverse();
672
673         // Same.
674         EXPECT_NEAR( 0.299000, rgb_to_ycbcr(0,0), 1e-6);
675         EXPECT_NEAR( 0.587000, rgb_to_ycbcr(0,1), 1e-6);
676         EXPECT_NEAR( 0.114000, rgb_to_ycbcr(0,2), 1e-6);
677
678         EXPECT_NEAR(-0.168736, rgb_to_ycbcr(1,0), 1e-6);
679         EXPECT_NEAR(-0.331264, rgb_to_ycbcr(1,1), 1e-6);
680         EXPECT_NEAR( 0.500000, rgb_to_ycbcr(1,2), 1e-6);
681
682         EXPECT_NEAR( 0.500000, rgb_to_ycbcr(2,0), 1e-6);
683         EXPECT_NEAR(-0.418688, rgb_to_ycbcr(2,1), 1e-6);
684         EXPECT_NEAR(-0.081312, rgb_to_ycbcr(2,2), 1e-6);
685
686         EXPECT_NEAR(  0.0, offset[0] * 255.0, 1e-3);
687         EXPECT_NEAR(128.0, offset[1] * 255.0, 1e-3);
688         EXPECT_NEAR(128.0, offset[2] * 255.0, 1e-3);
689 }
690
691 }  // namespace movit