Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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 "third_party/libyuv/include/libyuv.h" | |
| 7 | |
| 8 namespace media { | |
| 9 | |
| 10 // By OR-ing with 0x3800, 10-bit numbers become half-floats in the | |
| 11 // range [0.5..1) and 9-bit numbers get the range [0.5..0.75). | |
| 12 // | |
| 13 // Half-floats are evaluated as: | |
| 14 // float value = pow(2.0, exponent - 25) * (0x400 + fraction); | |
| 15 // | |
| 16 // In our case the exponent is 14 (since we or with 0x3800) and | |
| 17 // pow(2.0, 14-25) * 0x400 evaluates to 0.5 (our offset) and | |
| 18 // pow(2.0, 14-25) * fraction is [0..0.49951171875] for 10-bit and | |
| 19 // [0..0.24951171875] for 9-bit. | |
| 20 // | |
| 21 // https://en.wikipedia.org/wiki/Half-precision_floating-point_format | |
| 22 | |
|
hubbe
2017/03/23 17:46:11
extra newline
Uzair
2017/03/24 05:39:08
removed extra line.
| |
| 23 class HalfFloatMaker_xor : public HalfFloatMaker { | |
| 24 public: | |
| 25 explicit HalfFloatMaker_xor(int bits_per_channel) | |
| 26 : bits_per_channel_(bits_per_channel) {} | |
| 27 float Offset() const override { return 0.5; } | |
| 28 float Multiplier() const override { | |
| 29 int max_input_value = (1 << bits_per_channel_) - 1; | |
| 30 // 2 << 11 = 2048 would be 1.0 with our exponent. | |
| 31 return 2048.0 / max_input_value; | |
| 32 } | |
| 33 void MakeHalfFloats(const uint16_t* src, size_t num, uint16_t* dst) override { | |
| 34 // Micro-benchmarking indicates that the compiler does | |
| 35 // a good enough job of optimizing this loop that trying | |
| 36 // to manually operate on one uint64 at a time is not | |
| 37 // actually helpful. | |
| 38 // Note to future optimizers: Benchmark your optimizations! | |
| 39 for (size_t i = 0; i < num; i++) | |
| 40 dst[i] = src[i] | 0x3800; | |
| 41 } | |
| 42 | |
| 43 private: | |
| 44 int bits_per_channel_; | |
| 45 }; | |
| 46 | |
| 47 class HalfFloatMaker_libyuv : public HalfFloatMaker { | |
|
hubbe
2017/03/23 17:46:11
Add class comment.
Uzair
2017/03/24 05:39:08
Done.
| |
| 48 public: | |
| 49 explicit HalfFloatMaker_libyuv(int bits_per_channel) { | |
| 50 int max_value = (1 << bits_per_channel) - 1; | |
| 51 // For less than 15 bits, we can give libyuv a multiplier of | |
| 52 // 1.0, which is faster on some platforms. If bits is 16 or larger, | |
| 53 // a multiplier of 1.0 would cause overflows. However, a multiplier | |
| 54 // of 1/max_value would cause subnormal floats, which perform | |
| 55 // very poorly on some platforms. | |
| 56 if (bits_per_channel <= 15) { | |
| 57 libyuv_multiplier_ = 1.0f; | |
| 58 } else { | |
| 59 // This multiplier makes sure that we avoid subnormal values. | |
| 60 libyuv_multiplier_ = 1.0f / 4096.0f; | |
| 61 } | |
| 62 resource_multiplier_ = 1.0f / libyuv_multiplier_ / max_value; | |
| 63 } | |
| 64 float Offset() const override { return 0.0f; } | |
| 65 float Multiplier() const override { return resource_multiplier_; } | |
| 66 void MakeHalfFloats(const uint16_t* src, size_t num, uint16_t* dst) override { | |
| 67 // Source and dest stride can be zero since we're only copying | |
| 68 // one row at a time. | |
| 69 int stride = 0; | |
| 70 int rows = 1; | |
| 71 libyuv::HalfFloatPlane(src, stride, dst, stride, libyuv_multiplier_, num, | |
| 72 rows); | |
| 73 } | |
| 74 | |
| 75 private: | |
| 76 float libyuv_multiplier_; | |
| 77 float resource_multiplier_; | |
| 78 }; | |
| 79 | |
| 80 std::unique_ptr<HalfFloatMaker> HalfFloatMaker::NewHalfFloatMaker( | |
| 81 int bits_per_channel) { | |
| 82 if (bits_per_channel < 11) { | |
| 83 return std::unique_ptr<HalfFloatMaker>( | |
| 84 new HalfFloatMaker_xor(bits_per_channel)); | |
| 85 } else { | |
| 86 return std::unique_ptr<HalfFloatMaker>( | |
| 87 new HalfFloatMaker_libyuv(bits_per_channel)); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 } // namespace media | |
| OLD | NEW |