]> git.sesse.net Git - movit/blob - ycbcr_input_test.cpp
Use abort() on check_error() failure.
[movit] / ycbcr_input_test.cpp
1 // Unit tests for YCbCrInput.
2 // FIXME: This class really ought to support mipmaps.
3
4 #include <GL/glew.h>
5 #include <stddef.h>
6
7 #include "effect_chain.h"
8 #include "gtest/gtest.h"
9 #include "test_util.h"
10 #include "util.h"
11 #include "ycbcr_input.h"
12
13 namespace movit {
14
15 TEST(YCbCrInput, Simple444) {
16         const int width = 1;
17         const int height = 5;
18
19         // Pure-color test inputs, calculated with the formulas in Rec. 601
20         // section 2.5.4.
21         unsigned char y[width * height] = {
22                 16, 235, 81, 145, 41,
23         };
24         unsigned char cb[width * height] = {
25                 128, 128, 90, 54, 240,
26         };
27         unsigned char cr[width * height] = {
28                 128, 128, 240, 34, 110,
29         };
30         float expected_data[4 * width * height] = {
31                 0.0, 0.0, 0.0, 1.0,
32                 1.0, 1.0, 1.0, 1.0,
33                 1.0, 0.0, 0.0, 1.0,
34                 0.0, 1.0, 0.0, 1.0,
35                 0.0, 0.0, 1.0, 1.0,
36         };
37         float out_data[4 * width * height];
38
39         EffectChainTester tester(NULL, width, height);
40
41         ImageFormat format;
42         format.color_space = COLORSPACE_sRGB;
43         format.gamma_curve = GAMMA_sRGB;
44
45         YCbCrFormat ycbcr_format;
46         ycbcr_format.luma_coefficients = YCBCR_REC_601;
47         ycbcr_format.full_range = false;
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;
54
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);
60
61         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
62
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);
66 }
67
68 TEST(YCbCrInput, FullRangeRec601) {
69         const int width = 1;
70         const int height = 5;
71
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] = {
76                 0, 255, 76, 150, 29,
77         };
78         unsigned char cb[width * height] = {
79                 128, 128, 85, 44, 255,
80         };
81         unsigned char cr[width * height] = {
82                 128, 128, 255, 21, 107,
83         };
84         float expected_data[4 * width * height] = {
85                 0.0, 0.0, 0.0, 1.0,
86                 1.0, 1.0, 1.0, 1.0,
87                 1.0, 0.0, 0.0, 1.0,
88                 0.0, 1.0, 0.0, 1.0,
89                 0.0, 0.0, 1.0, 1.0,
90         };
91         float out_data[4 * width * height];
92
93         EffectChainTester tester(NULL, width, height);
94
95         ImageFormat format;
96         format.color_space = COLORSPACE_sRGB;
97         format.gamma_curve = GAMMA_sRGB;
98
99         YCbCrFormat ycbcr_format;
100         ycbcr_format.luma_coefficients = YCBCR_REC_601;
101         ycbcr_format.full_range = true;
102         ycbcr_format.chroma_subsampling_x = 1;
103         ycbcr_format.chroma_subsampling_y = 1;
104         ycbcr_format.cb_x_position = 0.5f;
105         ycbcr_format.cb_y_position = 0.5f;
106         ycbcr_format.cr_x_position = 0.5f;
107         ycbcr_format.cr_y_position = 0.5f;
108
109         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
110         input->set_pixel_data(0, y);
111         input->set_pixel_data(1, cb);
112         input->set_pixel_data(2, cr);
113         tester.get_chain()->add_input(input);
114
115         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
116
117         // Y'CbCr isn't 100% accurate (the input values are rounded),
118         // so we need some leeway.
119         expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
120 }
121
122 TEST(YCbCrInput, Rec709) {
123         const int width = 1;
124         const int height = 5;
125
126         // Pure-color test inputs, calculated with the formulas in Rec. 709
127         // page 19, items 3.4 and 3.5.
128         unsigned char y[width * height] = {
129                 16, 235, 63, 173, 32, 
130         };
131         unsigned char cb[width * height] = {
132                 128, 128, 102, 42, 240,
133         };
134         unsigned char cr[width * height] = {
135                 128, 128, 240, 26, 118,
136         };
137         float expected_data[4 * width * height] = {
138                 0.0, 0.0, 0.0, 1.0,
139                 1.0, 1.0, 1.0, 1.0,
140                 1.0, 0.0, 0.0, 1.0,
141                 0.0, 1.0, 0.0, 1.0,
142                 0.0, 0.0, 1.0, 1.0,
143         };
144         float out_data[4 * width * height];
145
146         EffectChainTester tester(NULL, width, height);
147
148         ImageFormat format;
149         format.color_space = COLORSPACE_sRGB;
150         format.gamma_curve = GAMMA_sRGB;
151
152         YCbCrFormat ycbcr_format;
153         ycbcr_format.luma_coefficients = YCBCR_REC_709;
154         ycbcr_format.full_range = false;
155         ycbcr_format.chroma_subsampling_x = 1;
156         ycbcr_format.chroma_subsampling_y = 1;
157         ycbcr_format.cb_x_position = 0.5f;
158         ycbcr_format.cb_y_position = 0.5f;
159         ycbcr_format.cr_x_position = 0.5f;
160         ycbcr_format.cr_y_position = 0.5f;
161
162         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
163         input->set_pixel_data(0, y);
164         input->set_pixel_data(1, cb);
165         input->set_pixel_data(2, cr);
166         tester.get_chain()->add_input(input);
167
168         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
169
170         // Y'CbCr isn't 100% accurate (the input values are rounded),
171         // so we need some leeway.
172         expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
173 }
174
175 TEST(YCbCrInput, Rec2020) {
176         const int width = 1;
177         const int height = 5;
178
179         // Pure-color test inputs, calculated with the formulas in Rec. 2020
180         // page 4, tables 4 and 5 (for conventional non-constant luminance).
181         // Note that we still use 8-bit inputs, even though Rec. 2020 is only
182         // defined for 10- and 12-bit.
183         unsigned char y[width * height] = {
184                 16, 235, 74, 164, 29,
185         };
186         unsigned char cb[width * height] = {
187                 128, 128, 97, 47, 240,
188         };
189         unsigned char cr[width * height] = {
190                 128, 128, 240, 25, 119,
191         };
192         float expected_data[4 * width * height] = {
193                 0.0, 0.0, 0.0, 1.0,
194                 1.0, 1.0, 1.0, 1.0,
195                 1.0, 0.0, 0.0, 1.0,
196                 0.0, 1.0, 0.0, 1.0,
197                 0.0, 0.0, 1.0, 1.0,
198         };
199         float out_data[4 * width * height];
200
201         EffectChainTester tester(NULL, width, height);
202
203         ImageFormat format;
204         format.color_space = COLORSPACE_sRGB;
205         format.gamma_curve = GAMMA_sRGB;
206
207         YCbCrFormat ycbcr_format;
208         ycbcr_format.luma_coefficients = YCBCR_REC_2020;
209         ycbcr_format.full_range = false;
210         ycbcr_format.chroma_subsampling_x = 1;
211         ycbcr_format.chroma_subsampling_y = 1;
212         ycbcr_format.cb_x_position = 0.5f;
213         ycbcr_format.cb_y_position = 0.5f;
214         ycbcr_format.cr_x_position = 0.5f;
215         ycbcr_format.cr_y_position = 0.5f;
216
217         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
218         input->set_pixel_data(0, y);
219         input->set_pixel_data(1, cb);
220         input->set_pixel_data(2, cr);
221         tester.get_chain()->add_input(input);
222
223         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
224
225         // Y'CbCr isn't 100% accurate (the input values are rounded),
226         // so we need some leeway.
227         expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
228 }
229
230 TEST(YCbCrInput, Subsampling420) {
231         const int width = 4;
232         const int height = 4;
233
234         unsigned char y[width * height] = {
235                 126, 126, 126, 126,
236                 126, 126, 126, 126,
237                 126, 126, 126, 126,
238                 126, 126, 126, 126,
239         };
240         unsigned char cb[(width/2) * (height/2)] = {
241                 64, 128,
242                 128, 192,
243         };
244         unsigned char cr[(width/2) * (height/2)] = {
245                 128, 128,
246                 128, 128,
247         };
248
249         // Note: This is only the blue channel. The chroma samples (with associated
250         // values for blue) are marked off in comments.
251         float expected_data[width * height] = {
252                 0.000, 0.125, 0.375, 0.500, 
253                  /* 0.0 */      /* 0.5 */
254                 0.125, 0.250, 0.500, 0.625,
255
256                 0.375, 0.500, 0.750, 0.875,
257                  /* 0.5 */      /* 1.0 */
258                 0.500, 0.625, 0.875, 1.000,
259         };
260         float out_data[width * height];
261
262         EffectChainTester tester(NULL, width, height);
263
264         ImageFormat format;
265         format.color_space = COLORSPACE_sRGB;
266         format.gamma_curve = GAMMA_sRGB;
267
268         YCbCrFormat ycbcr_format;
269         ycbcr_format.luma_coefficients = YCBCR_REC_601;
270         ycbcr_format.full_range = false;
271         ycbcr_format.chroma_subsampling_x = 2;
272         ycbcr_format.chroma_subsampling_y = 2;
273         ycbcr_format.cb_x_position = 0.5f;
274         ycbcr_format.cb_y_position = 0.5f;
275         ycbcr_format.cr_x_position = 0.5f;
276         ycbcr_format.cr_y_position = 0.5f;
277
278         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
279         input->set_pixel_data(0, y);
280         input->set_pixel_data(1, cb);
281         input->set_pixel_data(2, cr);
282         tester.get_chain()->add_input(input);
283
284         tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
285
286         // Y'CbCr isn't 100% accurate (the input values are rounded),
287         // so we need some leeway.
288         expect_equal(expected_data, out_data, width, height, 0.01, 0.001);
289 }
290
291 TEST(YCbCrInput, Subsampling420WithNonCenteredSamples) {
292         const int width = 4;
293         const int height = 4;
294
295         unsigned char y[width * height] = {
296                 126, 126, 126, 126,
297                 126, 126, 126, 126,
298                 126, 126, 126, 126,
299                 126, 126, 126, 126,
300         };
301         unsigned char cb[(width/2) * (height/2)] = {
302                 64, 128,
303                 128, 192,
304         };
305         unsigned char cr[(width/2) * (height/2)] = {
306                 128, 128,
307                 128, 128,
308         };
309
310         // Note: This is only the blue channel. The chroma samples (with associated
311         // values for blue) are marked off in comments.
312         float expected_data[width * height] = {
313                    0.000, 0.250, 0.500, 0.500, 
314                 /* 0.0 */     /* 0.5 */
315                    0.125, 0.375, 0.625, 0.625,
316
317                    0.375, 0.625, 0.875, 0.875,
318                 /* 0.5 */     /* 1.0 */
319                    0.500, 0.750, 1.000, 1.000,
320         };
321         float out_data[width * height];
322
323         EffectChainTester tester(NULL, width, height);
324
325         ImageFormat format;
326         format.color_space = COLORSPACE_sRGB;
327         format.gamma_curve = GAMMA_sRGB;
328
329         YCbCrFormat ycbcr_format;
330         ycbcr_format.luma_coefficients = YCBCR_REC_601;
331         ycbcr_format.full_range = false;
332         ycbcr_format.chroma_subsampling_x = 2;
333         ycbcr_format.chroma_subsampling_y = 2;
334         ycbcr_format.cb_x_position = 0.0f;
335         ycbcr_format.cb_y_position = 0.5f;
336         ycbcr_format.cr_x_position = 0.0f;
337         ycbcr_format.cr_y_position = 0.5f;
338
339         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
340         input->set_pixel_data(0, y);
341         input->set_pixel_data(1, cb);
342         input->set_pixel_data(2, cr);
343         tester.get_chain()->add_input(input);
344
345         tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
346
347         // Y'CbCr isn't 100% accurate (the input values are rounded),
348         // so we need some leeway.
349         expect_equal(expected_data, out_data, width, height, 0.01, 0.001);
350 }
351
352 // Yes, some 4:2:2 formats actually have this craziness.
353 TEST(YCbCrInput, DifferentCbAndCrPositioning) {
354         const int width = 4;
355         const int height = 4;
356
357         unsigned char y[width * height] = {
358                 126, 126, 126, 126,
359                 126, 126, 126, 126,
360                 126, 126, 126, 126,
361                 126, 126, 126, 126,
362         };
363         unsigned char cb[(width/2) * height] = {
364                 64, 128,
365                 128, 192,
366                 128, 128,
367                 128, 128,
368         };
369         unsigned char cr[(width/2) * height] = {
370                 48, 128,
371                 128, 208,
372                 128, 128,
373                 128, 128,
374         };
375
376         // Chroma samples in this csae are always co-sited with a luma sample;
377         // their associated color values and position are marked off in comments.
378         float expected_data_blue[width * height] = {
379                    0.000 /* 0.0 */, 0.250,           0.500 /* 0.5 */, 0.500, 
380                    0.500 /* 0.5 */, 0.750,           1.000 /* 1.0 */, 1.000, 
381                    0.500 /* 0.5 */, 0.500,           0.500 /* 0.5 */, 0.500, 
382                    0.500 /* 0.5 */, 0.500,           0.500 /* 0.5 */, 0.500, 
383         };
384         float expected_data_red[width * height] = {
385                    0.000,           0.000 /* 0.0 */, 0.250,           0.500 /* 0.5 */, 
386                    0.500,           0.500 /* 0.5 */, 0.750,           1.000 /* 1.0 */, 
387                    0.500,           0.500 /* 0.5 */, 0.500,           0.500 /* 0.5 */, 
388                    0.500,           0.500 /* 0.5 */, 0.500,           0.500 /* 0.5 */, 
389         };
390         float out_data[width * height];
391
392         EffectChainTester tester(NULL, width, height);
393
394         ImageFormat format;
395         format.color_space = COLORSPACE_sRGB;
396         format.gamma_curve = GAMMA_sRGB;
397
398         YCbCrFormat ycbcr_format;
399         ycbcr_format.luma_coefficients = YCBCR_REC_601;
400         ycbcr_format.full_range = false;
401         ycbcr_format.chroma_subsampling_x = 2;
402         ycbcr_format.chroma_subsampling_y = 1;
403         ycbcr_format.cb_x_position = 0.0f;
404         ycbcr_format.cb_y_position = 0.5f;
405         ycbcr_format.cr_x_position = 1.0f;
406         ycbcr_format.cr_y_position = 0.5f;
407
408         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
409         input->set_pixel_data(0, y);
410         input->set_pixel_data(1, cb);
411         input->set_pixel_data(2, cr);
412         tester.get_chain()->add_input(input);
413
414         // Y'CbCr isn't 100% accurate (the input values are rounded),
415         // so we need some leeway.
416         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
417         expect_equal(expected_data_red, out_data, width, height, 0.02, 0.002);
418
419         tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
420         expect_equal(expected_data_blue, out_data, width, height, 0.01, 0.001);
421 }
422
423 TEST(YCbCrInput, PBO) {
424         const int width = 1;
425         const int height = 5;
426
427         // Pure-color test inputs, calculated with the formulas in Rec. 601
428         // section 2.5.4.
429         unsigned char data[width * height * 3] = {
430                 16, 235, 81, 145, 41,
431                 128, 128, 90, 54, 240,
432                 128, 128, 240, 34, 110,
433         };
434         float expected_data[4 * width * height] = {
435                 0.0, 0.0, 0.0, 1.0,
436                 1.0, 1.0, 1.0, 1.0,
437                 1.0, 0.0, 0.0, 1.0,
438                 0.0, 1.0, 0.0, 1.0,
439                 0.0, 0.0, 1.0, 1.0,
440         };
441         float out_data[4 * width * height];
442
443         GLuint pbo;
444         glGenBuffers(1, &pbo);
445         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
446         glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * 3, data, GL_STREAM_DRAW);
447         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
448
449         EffectChainTester tester(NULL, width, height);
450
451         ImageFormat format;
452         format.color_space = COLORSPACE_sRGB;
453         format.gamma_curve = GAMMA_sRGB;
454
455         YCbCrFormat ycbcr_format;
456         ycbcr_format.luma_coefficients = YCBCR_REC_601;
457         ycbcr_format.full_range = false;
458         ycbcr_format.chroma_subsampling_x = 1;
459         ycbcr_format.chroma_subsampling_y = 1;
460         ycbcr_format.cb_x_position = 0.5f;
461         ycbcr_format.cb_y_position = 0.5f;
462         ycbcr_format.cr_x_position = 0.5f;
463         ycbcr_format.cr_y_position = 0.5f;
464
465         YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
466         input->set_pixel_data(0, (unsigned char *)BUFFER_OFFSET(0), pbo);
467         input->set_pixel_data(1, (unsigned char *)BUFFER_OFFSET(width * height), pbo);
468         input->set_pixel_data(2, (unsigned char *)BUFFER_OFFSET(width * height * 2), pbo);
469         tester.get_chain()->add_input(input);
470
471         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
472
473         // Y'CbCr isn't 100% accurate (the input values are rounded),
474         // so we need some leeway.
475         expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
476
477         glDeleteBuffers(1, &pbo);
478 }
479
480 }  // namespace movit