]> git.sesse.net Git - movit/blob - test_util.cpp
Help the compiler out a tiny bit.
[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()), width(width), height(height), output_added(false), finalized(false)
51 {
52         CHECK(init_movit(".", MOVIT_DEBUG_OFF));
53
54         if (data != NULL) {
55                 add_input(data, pixel_format, color_space, gamma_curve);
56         }
57
58         GLuint type;
59         if (framebuffer_format == GL_RGBA8) {
60                 type = GL_UNSIGNED_BYTE;
61         } else if (framebuffer_format == GL_RGBA16F || framebuffer_format == GL_RGBA32F) {
62                 type = GL_FLOAT;
63         } else {
64                 // Add more here as needed.
65                 assert(false);
66         }
67
68         glGenTextures(1, &texnum);
69         check_error();
70         glBindTexture(GL_TEXTURE_2D, texnum);
71         check_error();
72         glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, type, NULL);
73         check_error();
74
75         glGenFramebuffers(1, &fbo);
76         check_error();
77         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
78         check_error();
79         glFramebufferTexture2D(
80                 GL_FRAMEBUFFER,
81                 GL_COLOR_ATTACHMENT0,
82                 GL_TEXTURE_2D,
83                 texnum,
84                 0);
85         check_error();
86         glBindFramebuffer(GL_FRAMEBUFFER, 0);
87         check_error();
88 }
89
90 EffectChainTester::~EffectChainTester()
91 {
92         glDeleteFramebuffers(1, &fbo);
93         check_error();
94         glDeleteTextures(1, &texnum);
95         check_error();
96 }
97
98 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
99 {
100         ImageFormat format;
101         format.color_space = color_space;
102         format.gamma_curve = gamma_curve;
103
104         if (input_width == -1) {
105                 input_width = width;
106         }
107         if (input_height == -1) {
108                 input_height = height;
109         }
110
111         FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, input_width, input_height);
112         input->set_pixel_data(data);
113         chain.add_input(input);
114         return input;
115 }
116
117 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
118 {
119         ImageFormat format;
120         format.color_space = color_space;
121         format.gamma_curve = gamma_curve;
122
123         if (input_width == -1) {
124                 input_width = width;
125         }
126         if (input_height == -1) {
127                 input_height = height;
128         }
129
130         FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, input_width, input_height);
131         input->set_pixel_data(data);
132         chain.add_input(input);
133         return input;
134 }
135
136 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
137 {
138         if (!finalized) {
139                 finalize_chain(color_space, gamma_curve, alpha_format);
140         }
141
142         chain.render_to_fbo(fbo, width, height);
143
144         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
145         check_error();
146         if (!epoxy_is_desktop_gl() && (format == GL_RED || format == GL_BLUE || format == GL_ALPHA)) {
147                 // GLES will only read GL_RGBA.
148                 float *temp = new float[width * height * 4];
149                 glReadPixels(0, 0, width, height, GL_RGBA, GL_FLOAT, temp);
150                 check_error();
151                 if (format == GL_ALPHA) {
152                         for (unsigned i = 0; i < width * height; ++i) {
153                                 out_data[i] = temp[i * 4 + 3];
154                         }
155                 } else if (format == GL_BLUE) {
156                         for (unsigned i = 0; i < width * height; ++i) {
157                                 out_data[i] = temp[i * 4 + 2];
158                         }
159                 } else {
160                         for (unsigned i = 0; i < width * height; ++i) {
161                                 out_data[i] = temp[i * 4];
162                         }
163                 }
164                 delete[] temp;
165         } else {
166                 glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
167                 check_error();
168         }
169
170         if (format == GL_RGBA) {
171                 width *= 4;
172         }
173
174         vertical_flip(out_data, width, height);
175 }
176
177 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
178 {
179         if (!finalized) {
180                 finalize_chain(color_space, gamma_curve, alpha_format);
181         }
182
183         chain.render_to_fbo(fbo, width, height);
184
185         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
186         check_error();
187         if (!epoxy_is_desktop_gl() && (format == GL_RED || format == GL_BLUE || format == GL_ALPHA)) {
188                 // GLES will only read GL_RGBA.
189                 unsigned char *temp = new unsigned char[width * height * 4];
190                 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, temp);
191                 check_error();
192                 if (format == GL_ALPHA) {
193                         for (unsigned i = 0; i < width * height; ++i) {
194                                 out_data[i] = temp[i * 4 + 3];
195                         }
196                 } else if (format == GL_BLUE) {
197                         for (unsigned i = 0; i < width * height; ++i) {
198                                 out_data[i] = temp[i * 4 + 2];
199                         }
200                 } else {
201                         for (unsigned i = 0; i < width * height; ++i) {
202                                 out_data[i] = temp[i * 4];
203                         }
204                 }
205                 delete[] temp;
206         } else {
207                 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, out_data);
208                 check_error();
209         }
210
211         if (format == GL_RGBA) {
212                 width *= 4;
213         }
214
215         vertical_flip(out_data, width, height);
216 }
217
218 void EffectChainTester::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
219 {
220         chain.add_output(format, alpha_format);
221         output_added = true;
222 }
223
224 void EffectChainTester::add_ycbcr_output(const ImageFormat &format, OutputAlphaFormat alpha_format, const YCbCrFormat &ycbcr_format)
225 {
226         chain.add_ycbcr_output(format, alpha_format, ycbcr_format);
227         output_added = true;
228 }
229
230 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
231 {
232         assert(!finalized);
233         if (!output_added) {
234                 ImageFormat image_format;
235                 image_format.color_space = color_space;
236                 image_format.gamma_curve = gamma_curve;
237                 chain.add_output(image_format, alpha_format);
238                 output_added = true;
239         }
240         chain.finalize();
241         finalized = true;
242 }
243
244 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
245 {
246         float largest_difference = -1.0f;
247         float squared_difference = 0.0f;
248         int largest_diff_x = -1, largest_diff_y = -1;
249
250         for (unsigned y = 0; y < height; ++y) {
251                 for (unsigned x = 0; x < width; ++x) {
252                         float diff = ref[y * width + x] - result[y * width + x];
253                         if (fabs(diff) > largest_difference) {
254                                 largest_difference = fabs(diff);
255                                 largest_diff_x = x;
256                                 largest_diff_y = y;
257                         }
258                         squared_difference += diff * diff;
259                 }
260         }
261
262         EXPECT_LT(largest_difference, largest_difference_limit)
263                 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
264                 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
265                 << "Result:    " << result[largest_diff_y * width + largest_diff_x];
266
267         float rms = sqrt(squared_difference) / (width * height);
268         EXPECT_LT(rms, rms_limit);
269
270         if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
271                 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
272
273                 fprintf(stderr, "Reference:\n");
274                 for (unsigned y = 0; y < height; ++y) {
275                         for (unsigned x = 0; x < width; ++x) {
276                                 fprintf(stderr, "%7.4f ", ref[y * width + x]);
277                         }
278                         fprintf(stderr, "\n");
279                 }
280
281                 fprintf(stderr, "\nResult:\n");
282                 for (unsigned y = 0; y < height; ++y) {
283                         for (unsigned x = 0; x < width; ++x) {
284                                 fprintf(stderr, "%7.4f ", result[y * width + x]);
285                         }
286                         fprintf(stderr, "\n");
287                 }
288         }
289 }
290
291 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
292 {
293         assert(width > 0);
294         assert(height > 0);
295
296         float *ref_float = new float[width * height];
297         float *result_float = new float[width * height];
298
299         for (unsigned y = 0; y < height; ++y) {
300                 for (unsigned x = 0; x < width; ++x) {
301                         ref_float[y * width + x] = ref[y * width + x];
302                         result_float[y * width + x] = result[y * width + x];
303                 }
304         }
305
306         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
307
308         delete[] ref_float;
309         delete[] result_float;
310 }
311
312 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)
313 {
314         double squared_difference = 0.0;
315         for (unsigned i = 0; i < num_values; ++i) {
316                 double absolute_error = fabs(expected[i] - result[i]);
317                 squared_difference += absolute_error * absolute_error;
318                 EXPECT_LT(absolute_error, absolute_error_limit);
319
320                 if (expected[i] > 0.0) {
321                         double relative_error = fabs(absolute_error / expected[i]);
322
323                         EXPECT_LT(relative_error, relative_error_limit);
324                 }
325                 if (i < num_values - 1) {
326                         double delta = expected[i + 1] - expected[i];
327                         double local_relative_error = fabs(absolute_error / delta);
328                         EXPECT_LT(local_relative_error, local_relative_error_limit);
329                 }
330         }
331         double rms = sqrt(squared_difference) / num_values;
332         EXPECT_LT(rms, rms_limit);
333 }
334
335 }  // namespace movit