]> git.sesse.net Git - movit/blob - test_util.cpp
Use nullptr everywhere, now that we have C++11.
[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 = nullptr;
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 != nullptr) {
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, nullptr, 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, nullptr, 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, nullptr, 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, nullptr, 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, nullptr, 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, nullptr, 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(uint16_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
147 {
148         internal_run<uint16_t>(out_data, nullptr, NULL, NULL, GL_UNSIGNED_SHORT, format, color_space, gamma_curve, alpha_format);
149 }
150
151 void EffectChainTester::run_10_10_10_2(uint32_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
152 {
153         internal_run<uint32_t>(out_data, nullptr, NULL, NULL, GL_UNSIGNED_INT_2_10_10_10_REV, format, color_space, gamma_curve, alpha_format);
154 }
155
156 #ifdef HAVE_BENCHMARK
157
158 void EffectChainTester::benchmark(benchmark::State &state, float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
159 {
160         internal_run<float>(out_data, nullptr, NULL, NULL, GL_FLOAT, format, color_space, gamma_curve, alpha_format, &state);
161 }
162
163 void EffectChainTester::benchmark(benchmark::State &state, float *out_data, float *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
164 {
165         internal_run<float>(out_data, out_data2, nullptr, NULL, GL_FLOAT, format, color_space, gamma_curve, alpha_format, &state);
166 }
167
168 void EffectChainTester::benchmark(benchmark::State &state, float *out_data, float *out_data2, float *out_data3, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
169 {
170         internal_run<float>(out_data, out_data2, out_data3, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format, &state);
171 }
172
173 void EffectChainTester::benchmark(benchmark::State &state, float *out_data, float *out_data2, float *out_data3, float *out_data4, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
174 {
175         internal_run(out_data, out_data2, out_data3, out_data4, GL_FLOAT, format, color_space, gamma_curve, alpha_format, &state);
176 }
177
178 void EffectChainTester::benchmark(benchmark::State &state, unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
179 {
180         internal_run<unsigned char>(out_data, nullptr, NULL, NULL, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format, &state);
181 }
182
183 void EffectChainTester::benchmark(benchmark::State &state, unsigned char *out_data, unsigned char *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
184 {
185         internal_run<unsigned char>(out_data, out_data2, nullptr, NULL, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format, &state);
186 }
187
188 void EffectChainTester::benchmark(benchmark::State &state, unsigned char *out_data, unsigned char *out_data2, unsigned char *out_data3, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
189 {
190         internal_run<unsigned char>(out_data, out_data2, out_data3, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format, &state);
191 }
192
193 void EffectChainTester::benchmark(benchmark::State &state, 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)
194 {
195         internal_run(out_data, out_data2, out_data3, out_data4, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format, &state);
196 }
197
198 void EffectChainTester::benchmark(benchmark::State &state, uint16_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
199 {
200         internal_run<uint16_t>(out_data, nullptr, NULL, NULL, GL_UNSIGNED_SHORT, format, color_space, gamma_curve, alpha_format, &state);
201 }
202
203 void EffectChainTester::benchmark_10_10_10_2(benchmark::State &state, uint32_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
204 {
205         internal_run<uint32_t>(out_data, nullptr, NULL, NULL, GL_UNSIGNED_INT_2_10_10_10_REV, format, color_space, gamma_curve, alpha_format, &state);
206 }
207
208 #endif
209
210 template<class T>
211 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
212 #ifdef HAVE_BENCHMARK
213 , benchmark::State *benchmark_state
214 #endif
215 )
216 {
217         if (!finalized) {
218                 finalize_chain(color_space, gamma_curve, alpha_format);
219         }
220
221         GLuint type;
222         if (framebuffer_format == GL_RGBA8) {
223                 type = GL_UNSIGNED_BYTE;
224         } else if (framebuffer_format == GL_RGBA16) {
225                 type = GL_UNSIGNED_SHORT;
226         } else if (framebuffer_format == GL_RGBA16F || framebuffer_format == GL_RGBA32F) {
227                 type = GL_FLOAT;
228         } else if (framebuffer_format == GL_RGB10_A2) {
229                 type = GL_UNSIGNED_INT_2_10_10_10_REV;
230         } else {
231                 // Add more here as needed.
232                 assert(false);
233         }
234
235         unsigned num_outputs;
236         if (out_data4 != nullptr) {
237                 num_outputs = 4;
238         } else if (out_data3 != nullptr) {
239                 num_outputs = 3;
240         } else if (out_data2 != nullptr) {
241                 num_outputs = 2;
242         } else {
243                 num_outputs = 1;
244         }
245
246         GLuint fbo, texnum[4];
247
248         glGenTextures(num_outputs, texnum);
249         check_error();
250         for (unsigned i = 0; i < num_outputs; ++i) {
251                 glBindTexture(GL_TEXTURE_2D, texnum[i]);
252                 check_error();
253                 glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, type, nullptr);
254                 check_error();
255         }
256
257         glGenFramebuffers(1, &fbo);
258         check_error();
259         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
260         check_error();
261         for (unsigned i = 0; i < num_outputs; ++i) {
262                 glFramebufferTexture2D(
263                         GL_FRAMEBUFFER,
264                         GL_COLOR_ATTACHMENT0 + i,
265                         GL_TEXTURE_2D,
266                         texnum[i],
267                         0);
268                 check_error();
269         }
270
271         GLenum bufs[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
272         glDrawBuffers(num_outputs, bufs);
273
274         chain.render_to_fbo(fbo, width, height);
275
276 #ifdef HAVE_BENCHMARK
277         // If running benchmarks: Now we've warmed up everything, so let's run the
278         // actual benchmark loop.
279         if (benchmark_state != nullptr) {
280                 glFinish();
281                 size_t iters = benchmark_state->max_iterations;
282                 for (auto _ : *benchmark_state) {
283                         chain.render_to_fbo(fbo, width, height);
284                         if (--iters == 0) {
285                                 glFinish();
286                         }
287                 }
288                 benchmark_state->SetItemsProcessed(benchmark_state->iterations() * width * height);
289         }
290 #endif
291
292         T *data[4] = { out_data, out_data2, out_data3, out_data4 };
293
294         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
295         check_error();
296         for (unsigned i = 0; i < num_outputs; ++i) {
297                 T *ptr = data[i];
298                 glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
299                 if (!epoxy_is_desktop_gl() && (format == GL_RED || format == GL_BLUE || format == GL_ALPHA)) {
300                         // GLES will only read GL_RGBA.
301                         T *temp = new T[width * height * 4];
302                         glReadPixels(0, 0, width, height, GL_RGBA, internal_format, temp);
303                         check_error();
304                         if (format == GL_ALPHA) {
305                                 for (unsigned i = 0; i < width * height; ++i) {
306                                         ptr[i] = temp[i * 4 + 3];
307                                 }
308                         } else if (format == GL_BLUE) {
309                                 for (unsigned i = 0; i < width * height; ++i) {
310                                         ptr[i] = temp[i * 4 + 2];
311                                 }
312                         } else {
313                                 for (unsigned i = 0; i < width * height; ++i) {
314                                         ptr[i] = temp[i * 4];
315                                 }
316                         }
317                         delete[] temp;
318                 } else {
319                         glReadPixels(0, 0, width, height, format, internal_format, ptr);
320                         check_error();
321                 }
322
323                 if (format == GL_RGBA && (type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT || type == GL_FLOAT)) {
324                         vertical_flip(ptr, width * 4, height);
325                 } else {
326                         vertical_flip(ptr, width, height);
327                 }
328         }
329
330         glDeleteFramebuffers(1, &fbo);
331         check_error();
332         glDeleteTextures(num_outputs, texnum);
333         check_error();
334 }
335
336 void EffectChainTester::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
337 {
338         chain.add_output(format, alpha_format);
339         output_added = true;
340 }
341
342 void EffectChainTester::add_ycbcr_output(const ImageFormat &format, OutputAlphaFormat alpha_format, const YCbCrFormat &ycbcr_format, YCbCrOutputSplitting output_splitting, GLenum type)
343 {
344         chain.add_ycbcr_output(format, alpha_format, ycbcr_format, output_splitting, type);
345         output_added = true;
346 }
347
348 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
349 {
350         assert(!finalized);
351         if (!output_added) {
352                 ImageFormat image_format;
353                 image_format.color_space = color_space;
354                 image_format.gamma_curve = gamma_curve;
355                 chain.add_output(image_format, alpha_format);
356                 output_added = true;
357         }
358         chain.finalize();
359         finalized = true;
360 }
361
362 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
363 {
364         float largest_difference = -1.0f;
365         float squared_difference = 0.0f;
366         int largest_diff_x = -1, largest_diff_y = -1;
367
368         for (unsigned y = 0; y < height; ++y) {
369                 for (unsigned x = 0; x < width; ++x) {
370                         float diff = ref[y * width + x] - result[y * width + x];
371                         if (fabs(diff) > largest_difference) {
372                                 largest_difference = fabs(diff);
373                                 largest_diff_x = x;
374                                 largest_diff_y = y;
375                         }
376                         squared_difference += diff * diff;
377                 }
378         }
379
380         EXPECT_LT(largest_difference, largest_difference_limit)
381                 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
382                 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
383                 << "Result:    " << result[largest_diff_y * width + largest_diff_x];
384
385         float rms = sqrt(squared_difference) / (width * height);
386         EXPECT_LT(rms, rms_limit);
387
388         if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
389                 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
390
391                 fprintf(stderr, "Reference:\n");
392                 for (unsigned y = 0; y < height; ++y) {
393                         for (unsigned x = 0; x < width; ++x) {
394                                 fprintf(stderr, "%7.4f ", ref[y * width + x]);
395                         }
396                         fprintf(stderr, "\n");
397                 }
398
399                 fprintf(stderr, "\nResult:\n");
400                 for (unsigned y = 0; y < height; ++y) {
401                         for (unsigned x = 0; x < width; ++x) {
402                                 fprintf(stderr, "%7.4f ", result[y * width + x]);
403                         }
404                         fprintf(stderr, "\n");
405                 }
406         }
407 }
408
409 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
410 {
411         assert(width > 0);
412         assert(height > 0);
413
414         float *ref_float = new float[width * height];
415         float *result_float = new float[width * height];
416
417         for (unsigned y = 0; y < height; ++y) {
418                 for (unsigned x = 0; x < width; ++x) {
419                         ref_float[y * width + x] = ref[y * width + x];
420                         result_float[y * width + x] = result[y * width + x];
421                 }
422         }
423
424         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
425
426         delete[] ref_float;
427         delete[] result_float;
428 }
429
430 void expect_equal(const uint16_t *ref, const uint16_t *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
431 {
432         assert(width > 0);
433         assert(height > 0);
434
435         float *ref_float = new float[width * height];
436         float *result_float = new float[width * height];
437
438         for (unsigned y = 0; y < height; ++y) {
439                 for (unsigned x = 0; x < width; ++x) {
440                         ref_float[y * width + x] = ref[y * width + x];
441                         result_float[y * width + x] = result[y * width + x];
442                 }
443         }
444
445         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
446
447         delete[] ref_float;
448         delete[] result_float;
449 }
450
451 void expect_equal(const int *ref, const int *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
452 {
453         assert(width > 0);
454         assert(height > 0);
455
456         float *ref_float = new float[width * height];
457         float *result_float = new float[width * height];
458
459         for (unsigned y = 0; y < height; ++y) {
460                 for (unsigned x = 0; x < width; ++x) {
461                         ref_float[y * width + x] = ref[y * width + x];
462                         result_float[y * width + x] = result[y * width + x];
463                 }
464         }
465
466         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
467
468         delete[] ref_float;
469         delete[] result_float;
470 }
471
472 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)
473 {
474         double squared_difference = 0.0;
475         for (unsigned i = 0; i < num_values; ++i) {
476                 double absolute_error = fabs(expected[i] - result[i]);
477                 squared_difference += absolute_error * absolute_error;
478                 EXPECT_LT(absolute_error, absolute_error_limit);
479
480                 if (expected[i] > 0.0) {
481                         double relative_error = fabs(absolute_error / expected[i]);
482
483                         EXPECT_LT(relative_error, relative_error_limit);
484                 }
485                 if (i < num_values - 1) {
486                         double delta = expected[i + 1] - expected[i];
487                         double local_relative_error = fabs(absolute_error / delta);
488                         EXPECT_LT(local_relative_error, local_relative_error_limit);
489                 }
490         }
491         double rms = sqrt(squared_difference) / num_values;
492         EXPECT_LT(rms, rms_limit);
493 }
494
495 double srgb_to_linear(double x)
496 {
497         // From the Wikipedia article on sRGB.
498         if (x < 0.04045) {
499                 return x / 12.92;
500         } else {
501                 return pow((x + 0.055) / 1.055, 2.4);
502         }
503 }
504
505 double linear_to_srgb(double x)
506 {
507         // From the Wikipedia article on sRGB.
508         if (x < 0.0031308) {
509                 return 12.92 * x;
510         } else {
511                 return 1.055 * pow(x, 1.0 / 2.4) - 0.055;
512         }
513 }
514
515 }  // namespace movit