| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "services/media/framework/parts/lpcm_reformatter.h" | 6 #include "services/media/framework/parts/lpcm_reformatter.h" |
| 7 | 7 |
| 8 namespace mojo { | 8 namespace mojo { |
| 9 namespace media { | 9 namespace media { |
| 10 | 10 |
| 11 // LpcmReformatter implementation that accepts samples of type TIn and | 11 // LpcmReformatter implementation that accepts samples of type TIn and |
| 12 // produces samples of type TOut. | 12 // produces samples of type TOut. |
| 13 template <typename TIn, typename TOut> | 13 template <typename TIn, typename TOut> |
| 14 class LpcmReformatterImpl : public LpcmReformatter { | 14 class LpcmReformatterImpl : public LpcmReformatter { |
| 15 public: | 15 public: |
| 16 LpcmReformatterImpl(const LpcmStreamType& in_type, | 16 LpcmReformatterImpl(const AudioStreamType& in_type, |
| 17 const LpcmStreamTypeSet& out_type); | 17 const AudioStreamTypeSet& out_type); |
| 18 | 18 |
| 19 ~LpcmReformatterImpl() override; | 19 ~LpcmReformatterImpl() override; |
| 20 | 20 |
| 21 // Transform implementation. | 21 // Transform implementation. |
| 22 bool TransformPacket(const PacketPtr& input, | 22 bool TransformPacket(const PacketPtr& input, |
| 23 bool new_input, | 23 bool new_input, |
| 24 PayloadAllocator* allocator, | 24 PayloadAllocator* allocator, |
| 25 PacketPtr* output) override; | 25 PacketPtr* output) override; |
| 26 | 26 |
| 27 private: | 27 private: |
| 28 LpcmStreamType in_type_; | 28 AudioStreamType in_type_; |
| 29 LpcmStreamType out_type_; | 29 AudioStreamType out_type_; |
| 30 }; | 30 }; |
| 31 | 31 |
| 32 std::shared_ptr<LpcmReformatter> LpcmReformatter::Create( | 32 std::shared_ptr<LpcmReformatter> LpcmReformatter::Create( |
| 33 const LpcmStreamType& in_type, | 33 const AudioStreamType& in_type, |
| 34 const LpcmStreamTypeSet& out_type) { | 34 const AudioStreamTypeSet& out_type) { |
| 35 LpcmReformatter* result = nullptr; | 35 LpcmReformatter* result = nullptr; |
| 36 | 36 |
| 37 switch (in_type.sample_format()) { | 37 switch (in_type.sample_format()) { |
| 38 case LpcmStreamType::SampleFormat::kUnsigned8: | 38 case AudioStreamType::SampleFormat::kUnsigned8: |
| 39 switch (out_type.sample_format()) { | 39 switch (out_type.sample_format()) { |
| 40 case LpcmStreamType::SampleFormat::kUnsigned8: | 40 case AudioStreamType::SampleFormat::kUnsigned8: |
| 41 case LpcmStreamType::SampleFormat::kAny: | 41 case AudioStreamType::SampleFormat::kAny: |
| 42 result = new LpcmReformatterImpl<uint8_t, uint8_t>(in_type, out_type); | 42 result = new LpcmReformatterImpl<uint8_t, uint8_t>(in_type, out_type); |
| 43 break; | 43 break; |
| 44 case LpcmStreamType::SampleFormat::kSigned16: | 44 case AudioStreamType::SampleFormat::kSigned16: |
| 45 result = new LpcmReformatterImpl<uint8_t, int16_t>(in_type, out_type); | 45 result = new LpcmReformatterImpl<uint8_t, int16_t>(in_type, out_type); |
| 46 break; | 46 break; |
| 47 case LpcmStreamType::SampleFormat::kSigned24In32: | 47 case AudioStreamType::SampleFormat::kSigned24In32: |
| 48 result = new LpcmReformatterImpl<uint8_t, int32_t>(in_type, out_type); | 48 result = new LpcmReformatterImpl<uint8_t, int32_t>(in_type, out_type); |
| 49 break; | 49 break; |
| 50 case LpcmStreamType::SampleFormat::kFloat: | 50 case AudioStreamType::SampleFormat::kFloat: |
| 51 result = new LpcmReformatterImpl<uint8_t, float>(in_type, out_type); | 51 result = new LpcmReformatterImpl<uint8_t, float>(in_type, out_type); |
| 52 break; | 52 break; |
| 53 default: | 53 default: |
| 54 NOTREACHED() << "unsupported sample format"; | 54 NOTREACHED() << "unsupported sample format"; |
| 55 result = nullptr; | 55 result = nullptr; |
| 56 break; | 56 break; |
| 57 } | 57 } |
| 58 break; | 58 break; |
| 59 case LpcmStreamType::SampleFormat::kSigned16: | 59 case AudioStreamType::SampleFormat::kSigned16: |
| 60 switch (out_type.sample_format()) { | 60 switch (out_type.sample_format()) { |
| 61 case LpcmStreamType::SampleFormat::kUnsigned8: | 61 case AudioStreamType::SampleFormat::kUnsigned8: |
| 62 result = new LpcmReformatterImpl<int16_t, uint8_t>(in_type, out_type); | 62 result = new LpcmReformatterImpl<int16_t, uint8_t>(in_type, out_type); |
| 63 break; | 63 break; |
| 64 case LpcmStreamType::SampleFormat::kSigned16: | 64 case AudioStreamType::SampleFormat::kSigned16: |
| 65 case LpcmStreamType::SampleFormat::kAny: | 65 case AudioStreamType::SampleFormat::kAny: |
| 66 result = new LpcmReformatterImpl<int16_t, int16_t>(in_type, out_type); | 66 result = new LpcmReformatterImpl<int16_t, int16_t>(in_type, out_type); |
| 67 break; | 67 break; |
| 68 case LpcmStreamType::SampleFormat::kSigned24In32: | 68 case AudioStreamType::SampleFormat::kSigned24In32: |
| 69 result = new LpcmReformatterImpl<int16_t, int32_t>(in_type, out_type); | 69 result = new LpcmReformatterImpl<int16_t, int32_t>(in_type, out_type); |
| 70 break; | 70 break; |
| 71 case LpcmStreamType::SampleFormat::kFloat: | 71 case AudioStreamType::SampleFormat::kFloat: |
| 72 result = new LpcmReformatterImpl<int16_t, float>(in_type, out_type); | 72 result = new LpcmReformatterImpl<int16_t, float>(in_type, out_type); |
| 73 break; | 73 break; |
| 74 default: | 74 default: |
| 75 NOTREACHED() << "unsupported sample format"; | 75 NOTREACHED() << "unsupported sample format"; |
| 76 result = nullptr; | 76 result = nullptr; |
| 77 break; | 77 break; |
| 78 } | 78 } |
| 79 break; | 79 break; |
| 80 case LpcmStreamType::SampleFormat::kSigned24In32: | 80 case AudioStreamType::SampleFormat::kSigned24In32: |
| 81 switch (out_type.sample_format()) { | 81 switch (out_type.sample_format()) { |
| 82 case LpcmStreamType::SampleFormat::kUnsigned8: | 82 case AudioStreamType::SampleFormat::kUnsigned8: |
| 83 result = new LpcmReformatterImpl<int32_t, uint8_t>(in_type, out_type); | 83 result = new LpcmReformatterImpl<int32_t, uint8_t>(in_type, out_type); |
| 84 break; | 84 break; |
| 85 case LpcmStreamType::SampleFormat::kSigned16: | 85 case AudioStreamType::SampleFormat::kSigned16: |
| 86 result = new LpcmReformatterImpl<int32_t, int16_t>(in_type, out_type); | 86 result = new LpcmReformatterImpl<int32_t, int16_t>(in_type, out_type); |
| 87 break; | 87 break; |
| 88 case LpcmStreamType::SampleFormat::kSigned24In32: | 88 case AudioStreamType::SampleFormat::kSigned24In32: |
| 89 case LpcmStreamType::SampleFormat::kAny: | 89 case AudioStreamType::SampleFormat::kAny: |
| 90 result = new LpcmReformatterImpl<int32_t, int32_t>(in_type, out_type); | 90 result = new LpcmReformatterImpl<int32_t, int32_t>(in_type, out_type); |
| 91 break; | 91 break; |
| 92 case LpcmStreamType::SampleFormat::kFloat: | 92 case AudioStreamType::SampleFormat::kFloat: |
| 93 result = new LpcmReformatterImpl<int32_t, float>(in_type, out_type); | 93 result = new LpcmReformatterImpl<int32_t, float>(in_type, out_type); |
| 94 break; | 94 break; |
| 95 default: | 95 default: |
| 96 NOTREACHED() << "unsupported sample format"; | 96 NOTREACHED() << "unsupported sample format"; |
| 97 result = nullptr; | 97 result = nullptr; |
| 98 break; | 98 break; |
| 99 } | 99 } |
| 100 break; | 100 break; |
| 101 case LpcmStreamType::SampleFormat::kFloat: | 101 case AudioStreamType::SampleFormat::kFloat: |
| 102 switch (out_type.sample_format()) { | 102 switch (out_type.sample_format()) { |
| 103 case LpcmStreamType::SampleFormat::kUnsigned8: | 103 case AudioStreamType::SampleFormat::kUnsigned8: |
| 104 result = new LpcmReformatterImpl<float, uint8_t>(in_type, out_type); | 104 result = new LpcmReformatterImpl<float, uint8_t>(in_type, out_type); |
| 105 break; | 105 break; |
| 106 case LpcmStreamType::SampleFormat::kSigned16: | 106 case AudioStreamType::SampleFormat::kSigned16: |
| 107 result = new LpcmReformatterImpl<float, int16_t>(in_type, out_type); | 107 result = new LpcmReformatterImpl<float, int16_t>(in_type, out_type); |
| 108 break; | 108 break; |
| 109 case LpcmStreamType::SampleFormat::kSigned24In32: | 109 case AudioStreamType::SampleFormat::kSigned24In32: |
| 110 result = new LpcmReformatterImpl<float, int32_t>(in_type, out_type); | 110 result = new LpcmReformatterImpl<float, int32_t>(in_type, out_type); |
| 111 break; | 111 break; |
| 112 case LpcmStreamType::SampleFormat::kFloat: | 112 case AudioStreamType::SampleFormat::kFloat: |
| 113 case LpcmStreamType::SampleFormat::kAny: | 113 case AudioStreamType::SampleFormat::kAny: |
| 114 result = new LpcmReformatterImpl<float, float>(in_type, out_type); | 114 result = new LpcmReformatterImpl<float, float>(in_type, out_type); |
| 115 break; | 115 break; |
| 116 default: | 116 default: |
| 117 NOTREACHED() << "unsupported sample format"; | 117 NOTREACHED() << "unsupported sample format"; |
| 118 result = nullptr; | 118 result = nullptr; |
| 119 break; | 119 break; |
| 120 } | 120 } |
| 121 break; | 121 break; |
| 122 default: | 122 default: |
| 123 NOTREACHED() << "unsupported sample format"; | 123 NOTREACHED() << "unsupported sample format"; |
| 124 result = nullptr; | 124 result = nullptr; |
| 125 break; | 125 break; |
| 126 } | 126 } |
| 127 | 127 |
| 128 return std::shared_ptr<LpcmReformatter>(result); | 128 return std::shared_ptr<LpcmReformatter>(result); |
| 129 } | 129 } |
| 130 | 130 |
| 131 template <typename TIn, typename TOut> | 131 template <typename TIn, typename TOut> |
| 132 LpcmReformatterImpl<TIn, TOut>::LpcmReformatterImpl( | 132 LpcmReformatterImpl<TIn, TOut>::LpcmReformatterImpl( |
| 133 const LpcmStreamType& in_type, | 133 const AudioStreamType& in_type, |
| 134 const LpcmStreamTypeSet& out_type) | 134 const AudioStreamTypeSet& out_type) |
| 135 : in_type_(in_type), | 135 : in_type_(in_type), |
| 136 out_type_(out_type.sample_format() == LpcmStreamType::SampleFormat::kAny | 136 out_type_( |
| 137 ? in_type.sample_format() | 137 in_type.encoding(), |
| 138 : out_type.sample_format(), | 138 nullptr, |
| 139 in_type.channels(), | 139 out_type.sample_format() == AudioStreamType::SampleFormat::kAny |
| 140 in_type.frames_per_second()) {} | 140 ? in_type.sample_format() |
| 141 : out_type.sample_format(), |
| 142 in_type.channels(), |
| 143 in_type.frames_per_second()) { |
| 144 DCHECK(in_type.encoding() == StreamType::kAudioEncodingLpcm); |
| 145 DCHECK(in_type.encoding_parameters() == nullptr); |
| 146 } |
| 141 | 147 |
| 142 template <typename TIn, typename TOut> | 148 template <typename TIn, typename TOut> |
| 143 LpcmReformatterImpl<TIn, TOut>::~LpcmReformatterImpl() {} | 149 LpcmReformatterImpl<TIn, TOut>::~LpcmReformatterImpl() {} |
| 144 | 150 |
| 145 namespace { | 151 namespace { |
| 146 | 152 |
| 147 template <typename T> | 153 template <typename T> |
| 148 inline constexpr T Clamp(T val, T min, T max) { | 154 inline constexpr T Clamp(T val, T min, T max) { |
| 149 return (val > max) ? max : ((val < min) ? min : val); | 155 return (val > max) ? max : ((val < min) ? min : val); |
| 150 } | 156 } |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 } | 268 } |
| 263 | 269 |
| 264 *output = Packet::Create(input->pts(), input->end_of_stream(), out_size, | 270 *output = Packet::Create(input->pts(), input->end_of_stream(), out_size, |
| 265 buffer, allocator); | 271 buffer, allocator); |
| 266 | 272 |
| 267 return true; | 273 return true; |
| 268 } | 274 } |
| 269 | 275 |
| 270 } // namespace media | 276 } // namespace media |
| 271 } // namespace mojo | 277 } // namespace mojo |
| OLD | NEW |