]> git.sesse.net Git - movit/commitdiff
Add accuracy unit tests for GammaExpansionEffect.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 9 Jan 2014 19:55:02 +0000 (20:55 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 9 Jan 2014 19:55:02 +0000 (20:55 +0100)
After all the worry about accuracy in the polynomial expansion,
I figured it was time to actually add tests verifying the error
bounds here in a more reasonable way. The limits are set sort-of
randomly, more-or-less to what the new code already handles,
rounded up a bit. (The old texture-based code was _way_ worse than this,
it seems, probably due to texel center issues.)

This also shows that you can probably do 10-bit processing with
Movit without losing way too much accuracy, but that 12-bit is
too much for fp16 to handle.

gamma_expansion_effect.cpp
gamma_expansion_effect_test.cpp
test_util.cpp
test_util.h

index 5f9423f7c749081f965d57bbb8ba06901381ca08..bfccaa04b925a3aacd6d359a8b9680a2d1899373 100644 (file)
@@ -74,6 +74,9 @@ void GammaExpansionEffect::set_gl_state(GLuint glsl_program_num, const std::stri
                // maxerror      = 0.000094
                // error at beta = 0.000012
                // error at 1.0  = 0.000012
                // maxerror      = 0.000094
                // error at beta = 0.000012
                // error at 1.0  = 0.000012
+               //
+               // Note that the worst _relative_ error by far is just at the beginning
+               // of the exponential curve, ie., just around β.
                set_uniform_float(glsl_program_num, prefix, "linear_scale", 1.0 / 12.92);
                set_uniform_float(glsl_program_num, prefix, "c0", 0.001324469581);
                set_uniform_float(glsl_program_num, prefix, "c1", 0.02227416690);
                set_uniform_float(glsl_program_num, prefix, "linear_scale", 1.0 / 12.92);
                set_uniform_float(glsl_program_num, prefix, "c0", 0.001324469581);
                set_uniform_float(glsl_program_num, prefix, "c1", 0.02227416690);
index dc50f911f9dca122e1e6e67a1eee6131b1305dbd..452b46c8ffbd34dd9b1780b6a15d4587bc75fd6e 100644 (file)
@@ -1,5 +1,6 @@
 // Unit tests for GammaExpansionEffect.
 
 // Unit tests for GammaExpansionEffect.
 
+#include <math.h>
 #include <GL/glew.h>
 
 #include "gamma_expansion_effect.h"
 #include <GL/glew.h>
 
 #include "gamma_expansion_effect.h"
@@ -51,6 +52,37 @@ TEST(GammaExpansionEffectTest, sRGB_AlphaIsUnchanged) {
        expect_equal(data, out_data, 5, 1);
 }
 
        expect_equal(data, out_data, 5, 1);
 }
 
+TEST(GammaExpansionEffectTest, sRGB_Accuracy) {
+       float data[256], expected_data[256], out_data[256];
+
+       for (int i = 0; i < 256; ++i) {
+               double x = i / 255.0;
+
+               data[i] = x;
+
+               // From the Wikipedia article on sRGB.
+               if (x < 0.04045) {
+                       expected_data[i] = x / 12.92;
+               } else {
+                       expected_data[i] = pow((x + 0.055) / 1.055, 2.4);
+               }
+       }
+
+       EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB, GL_RGBA32F);
+       tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+       // Accuracy limits; for comparison, limits for a straightforward ALU solution
+       // (using a branch and pow()) in parenthesis, used as a “high anchor” to
+       // indicate limitations of float arithmetic etc.:
+       //
+       //   Maximum absolute error: 0.1% of max energy (0.051%)
+       //   Maximum relative error: 2.5% of correct answer (0.093%)
+       //                           25% of difference to next pixel level (6.18%)
+       //   Allowed RMS error:      0.0001 (0.000010)
+       //
+       test_accuracy(expected_data, out_data, 256, 1e-3, 0.025, 0.25, 1e-4);
+}
+
 TEST(GammaExpansionEffectTest, Rec709_KeyValues) {
        float data[] = {
                0.0f, 1.0f,
 TEST(GammaExpansionEffectTest, Rec709_KeyValues) {
        float data[] = {
                0.0f, 1.0f,
@@ -96,6 +128,72 @@ TEST(GammaExpansionEffectTest, Rec709_AlphaIsUnchanged) {
        expect_equal(data, out_data, 5, 1);
 }
 
        expect_equal(data, out_data, 5, 1);
 }
 
+TEST(GammaExpansionEffectTest, Rec709_Accuracy) {
+       float data[256], expected_data[256], out_data[256];
+
+       for (int i = 0; i < 256; ++i) {
+               double x = i / 255.0;
+
+               data[i] = x;
+
+               // Rec. 2020, page 3.
+               if (x < 0.018 * 4.5) {
+                       expected_data[i] = x / 4.5;
+               } else {
+                       expected_data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
+               }
+       }
+
+       EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709, GL_RGBA32F);
+       tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+       // Accuracy limits; for comparison, limits for a straightforward ALU solution
+       // (using a branch and pow()) in parenthesis, used as a “high anchor” to
+       // indicate limitations of float arithmetic etc.:
+       //
+       //   Maximum absolute error: 0.1% of max energy (0.046%)
+       //   Maximum relative error: 1.0% of correct answer (0.080%)
+       //                           10% of difference to next pixel level (6.19%)
+       //   Allowed RMS error:      0.0001 (0.000010)
+       //
+       test_accuracy(expected_data, out_data, 256, 1e-3, 0.01, 0.1, 1e-4);
+}
+
+// This test tests the same gamma ramp as Rec709_Accuracy, but with 10-bit
+// input range and somewhat looser error bounds. (One could claim that this is
+// already on the limit of what we can reasonably do with fp16 input, if you
+// look at the local relative error.)
+TEST(GammaExpansionEffectTest, Rec2020_10Bit_Accuracy) {
+       float data[1024], expected_data[1024], out_data[1024];
+
+       for (int i = 0; i < 1024; ++i) {
+               double x = i / 1023.0;
+
+               data[i] = x;
+
+               // Rec. 2020, page 3.
+               if (x < 0.018 * 4.5) {
+                       expected_data[i] = x / 4.5;
+               } else {
+                       expected_data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
+               }
+       }
+
+       EffectChainTester tester(data, 1024, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_10_BIT, GL_RGBA32F);
+       tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+       // Accuracy limits; for comparison, limits for a straightforward ALU solution
+       // (using a branch and pow()) in parenthesis, used as a “high anchor” to
+       // indicate limitations of float arithmetic etc.:
+       //
+       //   Maximum absolute error: 0.1% of max energy (0.036%)
+       //   Maximum relative error: 1.0% of correct answer (0.064%)
+       //                           30% of difference to next pixel level (24.9%)
+       //   Allowed RMS error:      0.0001 (0.000005)
+       //
+       test_accuracy(expected_data, out_data, 1024, 1e-3, 0.01, 0.30, 1e-4);
+}
+
 TEST(GammaExpansionEffectTest, Rec2020_12BitIsVeryCloseToRec709) {
        float data[256];
        for (unsigned i = 0; i < 256; ++i) {
 TEST(GammaExpansionEffectTest, Rec2020_12BitIsVeryCloseToRec709) {
        float data[256];
        for (unsigned i = 0; i < 256; ++i) {
@@ -116,3 +214,38 @@ TEST(GammaExpansionEffectTest, Rec2020_12BitIsVeryCloseToRec709) {
        }
        EXPECT_GT(sqdiff, 1e-6);
 }
        }
        EXPECT_GT(sqdiff, 1e-6);
 }
+
+// The fp16 _input_ provided by FlatInput is not enough to distinguish between
+// all of the possible 12-bit input values (every other level translates to the
+// same value). Thus, this test has extremely loose bounds; if we ever decide
+// to start supporting fp32, we should re-run this and tighten them a lot.
+TEST(GammaExpansionEffectTest, Rec2020_12Bit_Inaccuracy) {
+       float data[4096], expected_data[4096], out_data[4096];
+
+       for (int i = 0; i < 4096; ++i) {
+               double x = i / 4095.0;
+
+               data[i] = x;
+
+               // Rec. 2020, page 3.
+               if (x < 0.0181 * 4.5) {
+                       expected_data[i] = x / 4.5;
+               } else {
+                       expected_data[i] = pow((x + 0.0993) / 1.0993, 1.0/0.45);
+               }
+       }
+
+       EffectChainTester tester(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT, GL_RGBA32F);
+       tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+       // Accuracy limits; for comparison, limits for a straightforward ALU solution
+       // (using a branch and pow()) in parenthesis, used as a “high anchor” to
+       // indicate limitations of float arithmetic etc.:
+       //
+       //   Maximum absolute error: 0.1% of max energy (0.050%)
+       //   Maximum relative error: 1.0% of correct answer (0.050%)
+       //                           250% of difference to next pixel level (100.00%)
+       //   Allowed RMS error:      0.0001 (0.000003)
+       //
+       test_accuracy(expected_data, out_data, 4096, 1e-3, 0.01, 2.50, 1e-4);
+}
index 190af9559bb8fa302e0cf7bd6251f8a277ab0488..a35bbea594a02ce9882d4a449805a28a00d8c63c 100644 (file)
@@ -207,3 +207,26 @@ void expect_equal(const unsigned char *ref, const unsigned char *result, unsigne
        delete[] ref_float;
        delete[] result_float;
 }
        delete[] ref_float;
        delete[] result_float;
 }
+
+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)
+{
+       double squared_difference = 0.0;
+       for (unsigned i = 0; i < num_values; ++i) {
+               double absolute_error = fabs(expected[i] - result[i]);
+               squared_difference += absolute_error * absolute_error;
+               EXPECT_LT(absolute_error, absolute_error_limit);
+
+               if (expected[i] > 0.0) {
+                       double relative_error = fabs(absolute_error / expected[i]);
+
+                       EXPECT_LT(relative_error, relative_error_limit);
+               }
+               if (i < num_values - 1) {
+                       double delta = expected[i + 1] - expected[i];
+                       double local_relative_error = fabs(absolute_error / delta);
+                       EXPECT_LT(local_relative_error, local_relative_error_limit);
+               }
+       }
+       double rms = sqrt(squared_difference) / num_values;
+       EXPECT_LT(rms, rms_limit);
+}
index 10ecf9ff122a9461dd6e4e520db4f5c971d7f673..992ca46bab9e68a736282ea715b8fb9a0cc2a29b 100644 (file)
@@ -33,5 +33,6 @@ private:
 
 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit = 1.5 / 255.0, float rms_limit = 0.2 / 255.0);
 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit = 1, float rms_limit = 0.2);
 
 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit = 1.5 / 255.0, float rms_limit = 0.2 / 255.0);
 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit = 1, float rms_limit = 0.2);
+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);
 
 #endif  // !defined(_MOVIT_TEST_UTIL_H)
 
 #endif  // !defined(_MOVIT_TEST_UTIL_H)