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