OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <math.h> |
| 6 |
| 7 #include "media/video/half_float_maker.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace media { |
| 11 |
| 12 class HalfFloatMakerTest : public testing::Test {}; |
| 13 |
| 14 // Convert an IEEE 754 half-float to a double value |
| 15 // that we can do math on. |
| 16 double FromHalfFloat(uint16_t half_float) { |
| 17 if (!half_float) |
| 18 return 0.0; |
| 19 int sign = (half_float & 0x8000) ? -1 : 1; |
| 20 int exponent = (half_float >> 10) & 0x1F; |
| 21 int fraction = half_float & 0x3FF; |
| 22 if (exponent == 0) { |
| 23 return pow(2.0, -24.0) * fraction; |
| 24 } else if (exponent == 0x1F) { |
| 25 return sign * 1000000000000.0; |
| 26 } else { |
| 27 return pow(2.0, exponent - 25) * (0x400 + fraction); |
| 28 } |
| 29 } |
| 30 |
| 31 TEST_F(HalfFloatMakerTest, MakeHalfFloatTest) { |
| 32 unsigned short integers[1 << 16]; |
| 33 unsigned short half_floats[1 << 16]; |
| 34 for (int bits = 9; bits <= 16; bits++) { |
| 35 std::unique_ptr<media::HalfFloatMaker> half_float_maker; |
| 36 half_float_maker = media::HalfFloatMaker::NewHalfFloatMaker(bits); |
| 37 int num_values = 1 << bits; |
| 38 for (int i = 0; i < num_values; i++) |
| 39 integers[i] = i; |
| 40 |
| 41 half_float_maker->MakeHalfFloats(integers, num_values, half_floats); |
| 42 // Multiplier to converting integers to 0.0..1.0 range. |
| 43 double multiplier = 1.0 / (num_values - 1); |
| 44 |
| 45 for (int i = 0; i < num_values; i++) { |
| 46 // This value is in range 0..1 |
| 47 float value = integers[i] * multiplier; |
| 48 // Reverse the effect of offset and multiplier to get the expected |
| 49 // output value from the half-float converter. |
| 50 float expected_value = |
| 51 value / half_float_maker->Multiplier() + half_float_maker->Offset(); |
| 52 EXPECT_EQ(integers[i], i); |
| 53 |
| 54 // We expect the result to be within +/- one least-significant bit. |
| 55 // Within the range we care about, half-floats values and |
| 56 // their representation both sort in the same order, so we |
| 57 // can just add one to get the next bigger half-float. |
| 58 float expected_precision = |
| 59 FromHalfFloat(half_floats[i] + 1) - FromHalfFloat(half_floats[i]); |
| 60 EXPECT_NEAR(FromHalfFloat(half_floats[i]), expected_value, |
| 61 expected_precision) |
| 62 << "i = " << i << " bits = " << bits; |
| 63 } |
| 64 } |
| 65 } |
| 66 |
| 67 } // namespace media |
OLD | NEW |