Support multiple Y'CbCr outputs.
[movit] / test_util.cpp
1 #include <assert.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <algorithm>
5 #include <epoxy/gl.h>
6 #include <gtest/gtest.h>
7 #include <gtest/gtest-message.h>
8
9 #include "flat_input.h"
10 #include "init.h"
11 #include "resource_pool.h"
12 #include "test_util.h"
13 #include "util.h"
14
15 using namespace std;
16
17 namespace movit {
18
19 class Input;
20
21 namespace {
22
23 // Not thread-safe, but this isn't a big problem for testing.
24 ResourcePool *get_static_pool()
25 {
26         static ResourcePool *resource_pool = NULL;
27         if (!resource_pool) {
28                 resource_pool = new ResourcePool();
29         }
30         return resource_pool;
31 }
32
33 // Flip upside-down to compensate for different origin.
34 template<class T>
35 void vertical_flip(T *data, unsigned width, unsigned height)
36 {
37         for (unsigned y = 0; y < height / 2; ++y) {
38                 unsigned flip_y = height - y - 1;
39                 for (unsigned x = 0; x < width; ++x) {
40                         swap(data[y * width + x], data[flip_y * width + x]);
41                 }
42         }
43 }
44
45 }  // namespace
46
47 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
48                                      MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve,
49                                      GLenum framebuffer_format)
50         : chain(width, height, get_static_pool()),
51           width(width),
52           height(height),
53           framebuffer_format(framebuffer_format),
54           output_added(false),
55           finalized(false)
56 {
57         CHECK(init_movit(".", MOVIT_DEBUG_OFF));
58
59         if (data != NULL) {
60                 add_input(data, pixel_format, color_space, gamma_curve);
61         }
62 }
63
64 EffectChainTester::~EffectChainTester()
65 {
66 }
67
68 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
69 {
70         ImageFormat format;
71         format.color_space = color_space;
72         format.gamma_curve = gamma_curve;
73
74         if (input_width == -1) {
75                 input_width = width;
76         }
77         if (input_height == -1) {
78                 input_height = height;
79         }
80
81         FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, input_width, input_height);
82         input->set_pixel_data(data);
83         chain.add_input(input);
84         return input;
85 }
86
87 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
88 {
89         ImageFormat format;
90         format.color_space = color_space;
91         format.gamma_curve = gamma_curve;
92
93         if (input_width == -1) {
94                 input_width = width;
95         }
96         if (input_height == -1) {
97                 input_height = height;
98         }
99
100         FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, input_width, input_height);
101         input->set_pixel_data(data);
102         chain.add_input(input);
103         return input;
104 }
105
106 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
107 {
108         internal_run<float>(out_data, NULL, NULL, NULL, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
109 }
110
111 void EffectChainTester::run(float *out_data, float *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
112 {
113         internal_run<float>(out_data, out_data2, NULL, NULL, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
114 }
115
116 void EffectChainTester::run(float *out_data, float *out_data2, float *out_data3, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
117 {
118         internal_run<float>(out_data, out_data2, out_data3, NULL, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
119 }
120
121 void EffectChainTester::run(float *out_data, float *out_data2, float *out_data3, float *out_data4, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
122 {
123         internal_run(out_data, out_data2, out_data3, out_data4, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
124 }
125
126 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
127 {
128         internal_run<unsigned char>(out_data, NULL, NULL, NULL, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
129 }
130
131 void EffectChainTester::run(unsigned char *out_data, unsigned char *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
132 {
133         internal_run<unsigned char>(out_data, out_data2, NULL, NULL, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
134 }
135
136 void EffectChainTester::run(unsigned char *out_data, unsigned char *out_data2, unsigned char *out_data3, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
137 {
138         internal_run<unsigned char>(out_data, out_data2, out_data3, NULL, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
139 }
140
141 void EffectChainTester::run(unsigned char *out_data, unsigned char *out_data2, unsigned char *out_data3, unsigned char *out_data4, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
142 {
143         internal_run(out_data, out_data2, out_data3, out_data4, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
144 }
145
146 void EffectChainTester::run_10_10_10_2(uint32_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
147 {
148         internal_run<uint32_t>(out_data, NULL, NULL, NULL, GL_UNSIGNED_INT_2_10_10_10_REV, format, color_space, gamma_curve, alpha_format);
149 }
150
151 template<class T>
152 void EffectChainTester::internal_run(T *out_data, T *out_data2, T *out_data3, T *out_data4, GLenum internal_format, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
153 {
154         if (!finalized) {
155                 finalize_chain(color_space, gamma_curve, alpha_format);
156         }
157
158         GLuint type;
159         if (framebuffer_format == GL_RGBA8) {
160                 type = GL_UNSIGNED_BYTE;
161         } else if (framebuffer_format == GL_RGBA16F || framebuffer_format == GL_RGBA32F) {
162                 type = GL_FLOAT;
163         } else if (framebuffer_format == GL_RGB10_A2) {
164                 type = GL_UNSIGNED_INT_2_10_10_10_REV;
165         } else {
166                 // Add more here as needed.
167                 assert(false);
168         }
169
170         unsigned num_outputs;
171         if (out_data4 != NULL) {
172                 num_outputs = 4;
173         } else if (out_data3 != NULL) {
174                 num_outputs = 3;
175         } else if (out_data2 != NULL) {
176                 num_outputs = 2;
177         } else {
178                 num_outputs = 1;
179         }
180
181         GLuint fbo, texnum[4];
182
183         glGenTextures(num_outputs, texnum);
184         check_error();
185         for (unsigned i = 0; i < num_outputs; ++i) {
186                 glBindTexture(GL_TEXTURE_2D, texnum[i]);
187                 check_error();
188                 glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, type, NULL);
189                 check_error();
190         }
191
192         glGenFramebuffers(1, &fbo);
193         check_error();
194         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
195         check_error();
196         for (unsigned i = 0; i < num_outputs; ++i) {
197                 glFramebufferTexture2D(
198                         GL_FRAMEBUFFER,
199                         GL_COLOR_ATTACHMENT0 + i,
200                         GL_TEXTURE_2D,
201                         texnum[i],
202                         0);
203                 check_error();
204         }
205
206         GLenum bufs[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
207         glDrawBuffers(num_outputs, bufs);
208
209         chain.render_to_fbo(fbo, width, height);
210
211         T *data[4] = { out_data, out_data2, out_data3, out_data4 };
212
213         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
214         check_error();
215         for (unsigned i = 0; i < num_outputs; ++i) {
216                 T *ptr = data[i];
217                 glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
218                 if (!epoxy_is_desktop_gl() && (format == GL_RED || format == GL_BLUE || format == GL_ALPHA)) {
219                         // GLES will only read GL_RGBA.
220                         T *temp = new T[width * height * 4];
221                         glReadPixels(0, 0, width, height, GL_RGBA, internal_format, temp);
222                         check_error();
223                         if (format == GL_ALPHA) {
224                                 for (unsigned i = 0; i < width * height; ++i) {
225                                         ptr[i] = temp[i * 4 + 3];
226                                 }
227                         } else if (format == GL_BLUE) {
228                                 for (unsigned i = 0; i < width * height; ++i) {
229                                         ptr[i] = temp[i * 4 + 2];
230                                 }
231                         } else {
232                                 for (unsigned i = 0; i < width * height; ++i) {
233                                         ptr[i] = temp[i * 4];
234                                 }
235                         }
236                         delete[] temp;
237                 } else {
238                         glReadPixels(0, 0, width, height, format, internal_format, ptr);
239                         check_error();
240                 }
241
242                 if (format == GL_RGBA && (type == GL_UNSIGNED_BYTE || type == GL_FLOAT)) {
243                         vertical_flip(ptr, width * 4, height);
244                 } else {
245                         vertical_flip(ptr, width, height);
246                 }
247         }
248
249         glDeleteFramebuffers(1, &fbo);
250         check_error();
251         glDeleteTextures(num_outputs, texnum);
252         check_error();
253 }
254
255 void EffectChainTester::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
256 {
257         chain.add_output(format, alpha_format);
258         output_added = true;
259 }
260
261 void EffectChainTester::add_ycbcr_output(const ImageFormat &format, OutputAlphaFormat alpha_format, const YCbCrFormat &ycbcr_format, YCbCrOutputSplitting output_splitting)
262 {
263         chain.add_ycbcr_output(format, alpha_format, ycbcr_format, output_splitting);
264         output_added = true;
265 }
266
267 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
268 {
269         assert(!finalized);
270         if (!output_added) {
271                 ImageFormat image_format;
272                 image_format.color_space = color_space;
273                 image_format.gamma_curve = gamma_curve;
274                 chain.add_output(image_format, alpha_format);
275                 output_added = true;
276         }
277         chain.finalize();
278         finalized = true;
279 }
280
281 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
282 {
283         float largest_difference = -1.0f;
284         float squared_difference = 0.0f;
285         int largest_diff_x = -1, largest_diff_y = -1;
286
287         for (unsigned y = 0; y < height; ++y) {
288                 for (unsigned x = 0; x < width; ++x) {
289                         float diff = ref[y * width + x] - result[y * width + x];
290                         if (fabs(diff) > largest_difference) {
291                                 largest_difference = fabs(diff);
292                                 largest_diff_x = x;
293                                 largest_diff_y = y;
294                         }
295                         squared_difference += diff * diff;
296                 }
297         }
298
299         EXPECT_LT(largest_difference, largest_difference_limit)
300                 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
301                 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
302                 << "Result:    " << result[largest_diff_y * width + largest_diff_x];
303
304         float rms = sqrt(squared_difference) / (width * height);
305         EXPECT_LT(rms, rms_limit);
306
307         if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
308                 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
309
310                 fprintf(stderr, "Reference:\n");
311                 for (unsigned y = 0; y < height; ++y) {
312                         for (unsigned x = 0; x < width; ++x) {
313                                 fprintf(stderr, "%7.4f ", ref[y * width + x]);
314                         }
315                         fprintf(stderr, "\n");
316                 }
317
318                 fprintf(stderr, "\nResult:\n");
319                 for (unsigned y = 0; y < height; ++y) {
320                         for (unsigned x = 0; x < width; ++x) {
321                                 fprintf(stderr, "%7.4f ", result[y * width + x]);
322                         }
323                         fprintf(stderr, "\n");
324                 }
325         }
326 }
327
328 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
329 {
330         assert(width > 0);
331         assert(height > 0);
332
333         float *ref_float = new float[width * height];
334         float *result_float = new float[width * height];
335
336         for (unsigned y = 0; y < height; ++y) {
337                 for (unsigned x = 0; x < width; ++x) {
338                         ref_float[y * width + x] = ref[y * width + x];
339                         result_float[y * width + x] = result[y * width + x];
340                 }
341         }
342
343         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
344
345         delete[] ref_float;
346         delete[] result_float;
347 }
348
349 void expect_equal(const int *ref, const int *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
350 {
351         assert(width > 0);
352         assert(height > 0);
353
354         float *ref_float = new float[width * height];
355         float *result_float = new float[width * height];
356
357         for (unsigned y = 0; y < height; ++y) {
358                 for (unsigned x = 0; x < width; ++x) {
359                         ref_float[y * width + x] = ref[y * width + x];
360                         result_float[y * width + x] = result[y * width + x];
361                 }
362         }
363
364         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
365
366         delete[] ref_float;
367         delete[] result_float;
368 }
369
370 void test_accuracy(const float *expected, const float *result, unsigned num_values, double absolute_error_limit, double relative_error_limit, double local_relative_error_limit, double rms_limit)
371 {
372         double squared_difference = 0.0;
373         for (unsigned i = 0; i < num_values; ++i) {
374                 double absolute_error = fabs(expected[i] - result[i]);
375                 squared_difference += absolute_error * absolute_error;
376                 EXPECT_LT(absolute_error, absolute_error_limit);
377
378                 if (expected[i] > 0.0) {
379                         double relative_error = fabs(absolute_error / expected[i]);
380
381                         EXPECT_LT(relative_error, relative_error_limit);
382                 }
383                 if (i < num_values - 1) {
384                         double delta = expected[i + 1] - expected[i];
385                         double local_relative_error = fabs(absolute_error / delta);
386                         EXPECT_LT(local_relative_error, local_relative_error_limit);
387                 }
388         }
389         double rms = sqrt(squared_difference) / num_values;
390         EXPECT_LT(rms, rms_limit);
391 }
392
393 double srgb_to_linear(double x)
394 {
395         // From the Wikipedia article on sRGB.
396         if (x < 0.04045) {
397                 return x / 12.92;
398         } else {
399                 return pow((x + 0.055) / 1.055, 2.4);
400         }
401 }
402
403 double linear_to_srgb(double x)
404 {
405         // From the Wikipedia article on sRGB.
406         if (x < 0.0031308) {
407                 return 12.92 * x;
408         } else {
409                 return 1.055 * pow(x, 1.0 / 2.4) - 0.055;
410         }
411 }
412
413 }  // namespace movit