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 class HalfFloatMaker_xor : public HalfFloatMaker { |
| 23 public: |
| 24 explicit HalfFloatMaker_xor(int bits_per_channel) |
| 25 : bits_per_channel_(bits_per_channel) {} |
| 26 float Offset() const override { return 0.5; } |
| 27 float Multiplier() const override { |
| 28 int max_input_value = (1 << bits_per_channel_) - 1; |
| 29 // 2 << 11 = 2048 would be 1.0 with our exponent. |
| 30 return 2048.0 / max_input_value; |
| 31 } |
| 32 void MakeHalfFloats(const uint16_t* src, size_t num, uint16_t* dst) override { |
| 33 // Micro-benchmarking indicates that the compiler does |
| 34 // a good enough job of optimizing this loop that trying |
| 35 // to manually operate on one uint64 at a time is not |
| 36 // actually helpful. |
| 37 // Note to future optimizers: Benchmark your optimizations! |
| 38 for (size_t i = 0; i < num; i++) |
| 39 dst[i] = src[i] | 0x3800; |
| 40 } |
| 41 |
| 42 private: |
| 43 int bits_per_channel_; |
| 44 }; |
| 45 |
| 46 // Convert plane of 16 bit shorts to half floats using libyuv. |
| 47 class HalfFloatMaker_libyuv : public HalfFloatMaker { |
| 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 |