]> git.sesse.net Git - movit/blob - test_util.cpp
Fix compiling without 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 = 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, 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, 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(out_data, out_data2, out_data3, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
119 }
120
121 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
122 {
123         internal_run<unsigned char>(out_data, NULL, NULL, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
124 }
125
126 void EffectChainTester::run(unsigned char *out_data, unsigned char *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
127 {
128         internal_run<unsigned char>(out_data, out_data2, 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, unsigned char *out_data3, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
132 {
133         internal_run(out_data, out_data2, out_data3, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
134 }
135
136 void EffectChainTester::run_10_10_10_2(uint32_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
137 {
138         internal_run<uint32_t>(out_data, NULL, NULL, GL_UNSIGNED_INT_2_10_10_10_REV, format, color_space, gamma_curve, alpha_format);
139 }
140
141 template<class T>
142 void EffectChainTester::internal_run(T *out_data, T *out_data2, T *out_data3, GLenum internal_format, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
143 {
144         if (!finalized) {
145                 finalize_chain(color_space, gamma_curve, alpha_format);
146         }
147
148         GLuint type;
149         if (framebuffer_format == GL_RGBA8) {
150                 type = GL_UNSIGNED_BYTE;
151         } else if (framebuffer_format == GL_RGBA16F || framebuffer_format == GL_RGBA32F) {
152                 type = GL_FLOAT;
153         } else if (framebuffer_format == GL_RGB10_A2) {
154                 type = GL_UNSIGNED_INT_2_10_10_10_REV;
155         } else {
156                 // Add more here as needed.
157                 assert(false);
158         }
159
160         unsigned num_outputs;
161         if (out_data3 != NULL) {
162                 num_outputs = 3;
163         } else if (out_data2 != NULL) {
164                 num_outputs = 2;
165         } else {
166                 num_outputs = 1;
167         }
168
169         GLuint fbo, texnum[3];
170
171         glGenTextures(num_outputs, texnum);
172         check_error();
173         for (unsigned i = 0; i < num_outputs; ++i) {
174                 glBindTexture(GL_TEXTURE_2D, texnum[i]);
175                 check_error();
176                 glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, type, NULL);
177                 check_error();
178         }
179
180         glGenFramebuffers(1, &fbo);
181         check_error();
182         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
183         check_error();
184         for (unsigned i = 0; i < num_outputs; ++i) {
185                 glFramebufferTexture2D(
186                         GL_FRAMEBUFFER,
187                         GL_COLOR_ATTACHMENT0 + i,
188                         GL_TEXTURE_2D,
189                         texnum[i],
190                         0);
191                 check_error();
192         }
193
194         GLenum bufs[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
195         glDrawBuffers(num_outputs, bufs);
196
197         chain.render_to_fbo(fbo, width, height);
198
199         T *data[3] = { out_data, out_data2, out_data3 };
200
201         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
202         check_error();
203         for (unsigned i = 0; i < num_outputs; ++i) {
204                 T *ptr = data[i];
205                 glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
206                 if (!epoxy_is_desktop_gl() && (format == GL_RED || format == GL_BLUE || format == GL_ALPHA)) {
207                         // GLES will only read GL_RGBA.
208                         T *temp = new T[width * height * 4];
209                         glReadPixels(0, 0, width, height, GL_RGBA, internal_format, temp);
210                         check_error();
211                         if (format == GL_ALPHA) {
212                                 for (unsigned i = 0; i < width * height; ++i) {
213                                         ptr[i] = temp[i * 4 + 3];
214                                 }
215                         } else if (format == GL_BLUE) {
216                                 for (unsigned i = 0; i < width * height; ++i) {
217                                         ptr[i] = temp[i * 4 + 2];
218                                 }
219                         } else {
220                                 for (unsigned i = 0; i < width * height; ++i) {
221                                         ptr[i] = temp[i * 4];
222                                 }
223                         }
224                         delete[] temp;
225                 } else {
226                         glReadPixels(0, 0, width, height, format, internal_format, ptr);
227                         check_error();
228                 }
229
230                 if (format == GL_RGBA && (type == GL_UNSIGNED_BYTE || type == GL_FLOAT)) {
231                         vertical_flip(ptr, width * 4, height);
232                 } else {
233                         vertical_flip(ptr, width, height);
234                 }
235         }
236
237         glDeleteFramebuffers(1, &fbo);
238         check_error();
239         glDeleteTextures(num_outputs, texnum);
240         check_error();
241 }
242
243 void EffectChainTester::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
244 {
245         chain.add_output(format, alpha_format);
246         output_added = true;
247 }
248
249 void EffectChainTester::add_ycbcr_output(const ImageFormat &format, OutputAlphaFormat alpha_format, const YCbCrFormat &ycbcr_format, YCbCrOutputSplitting output_splitting)
250 {
251         chain.add_ycbcr_output(format, alpha_format, ycbcr_format, output_splitting);
252         output_added = true;
253 }
254
255 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
256 {
257         assert(!finalized);
258         if (!output_added) {
259                 ImageFormat image_format;
260                 image_format.color_space = color_space;
261                 image_format.gamma_curve = gamma_curve;
262                 chain.add_output(image_format, alpha_format);
263                 output_added = true;
264         }
265         chain.finalize();
266         finalized = true;
267 }
268
269 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
270 {
271         float largest_difference = -1.0f;
272         float squared_difference = 0.0f;
273         int largest_diff_x = -1, largest_diff_y = -1;
274
275         for (unsigned y = 0; y < height; ++y) {
276                 for (unsigned x = 0; x < width; ++x) {
277                         float diff = ref[y * width + x] - result[y * width + x];
278                         if (fabs(diff) > largest_difference) {
279                                 largest_difference = fabs(diff);
280                                 largest_diff_x = x;
281                                 largest_diff_y = y;
282                         }
283                         squared_difference += diff * diff;
284                 }
285         }
286
287         EXPECT_LT(largest_difference, largest_difference_limit)
288                 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
289                 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
290                 << "Result:    " << result[largest_diff_y * width + largest_diff_x];
291
292         float rms = sqrt(squared_difference) / (width * height);
293         EXPECT_LT(rms, rms_limit);
294
295         if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
296                 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
297
298                 fprintf(stderr, "Reference:\n");
299                 for (unsigned y = 0; y < height; ++y) {
300                         for (unsigned x = 0; x < width; ++x) {
301                                 fprintf(stderr, "%7.4f ", ref[y * width + x]);
302                         }
303                         fprintf(stderr, "\n");
304                 }
305
306                 fprintf(stderr, "\nResult:\n");
307                 for (unsigned y = 0; y < height; ++y) {
308                         for (unsigned x = 0; x < width; ++x) {
309                                 fprintf(stderr, "%7.4f ", result[y * width + x]);
310                         }
311                         fprintf(stderr, "\n");
312                 }
313         }
314 }
315
316 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
317 {
318         assert(width > 0);
319         assert(height > 0);
320
321         float *ref_float = new float[width * height];
322         float *result_float = new float[width * height];
323
324         for (unsigned y = 0; y < height; ++y) {
325                 for (unsigned x = 0; x < width; ++x) {
326                         ref_float[y * width + x] = ref[y * width + x];
327                         result_float[y * width + x] = result[y * width + x];
328                 }
329         }
330
331         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
332
333         delete[] ref_float;
334         delete[] result_float;
335 }
336
337 void expect_equal(const int *ref, const int *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
338 {
339         assert(width > 0);
340         assert(height > 0);
341
342         float *ref_float = new float[width * height];
343         float *result_float = new float[width * height];
344
345         for (unsigned y = 0; y < height; ++y) {
346                 for (unsigned x = 0; x < width; ++x) {
347                         ref_float[y * width + x] = ref[y * width + x];
348                         result_float[y * width + x] = result[y * width + x];
349                 }
350         }
351
352         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
353
354         delete[] ref_float;
355         delete[] result_float;
356 }
357
358 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)
359 {
360         double squared_difference = 0.0;
361         for (unsigned i = 0; i < num_values; ++i) {
362                 double absolute_error = fabs(expected[i] - result[i]);
363                 squared_difference += absolute_error * absolute_error;
364                 EXPECT_LT(absolute_error, absolute_error_limit);
365
366                 if (expected[i] > 0.0) {
367                         double relative_error = fabs(absolute_error / expected[i]);
368
369                         EXPECT_LT(relative_error, relative_error_limit);
370                 }
371                 if (i < num_values - 1) {
372                         double delta = expected[i + 1] - expected[i];
373                         double local_relative_error = fabs(absolute_error / delta);
374                         EXPECT_LT(local_relative_error, local_relative_error_limit);
375                 }
376         }
377         double rms = sqrt(squared_difference) / num_values;
378         EXPECT_LT(rms, rms_limit);
379 }
380
381 double srgb_to_linear(double x)
382 {
383         // From the Wikipedia article on sRGB.
384         if (x < 0.04045) {
385                 return x / 12.92;
386         } else {
387                 return pow((x + 0.055) / 1.055, 2.4);
388         }
389 }
390
391 double linear_to_srgb(double x)
392 {
393         // From the Wikipedia article on sRGB.
394         if (x < 0.0031308) {
395                 return 12.92 * x;
396         } else {
397                 return 1.055 * pow(x, 1.0 / 2.4) - 0.055;
398         }
399 }
400
401 }  // namespace movit