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