OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/logging.h" |
| 6 #include "services/media/framework/lpcm_util.h" |
| 7 #include "services/media/framework/ostream.h" |
| 8 |
| 9 namespace mojo { |
| 10 namespace media { |
| 11 |
| 12 // LpcmUtil implementation that processes samples of type T. |
| 13 template<typename T> |
| 14 class LpcmUtilImpl : public LpcmUtil { |
| 15 public: |
| 16 ~LpcmUtilImpl(); |
| 17 |
| 18 void Silence(void* buffer, uint64_t frame_count) const override; |
| 19 |
| 20 void Copy(const void* in, void* out, uint64_t frame_count) const override; |
| 21 |
| 22 void Mix(const void* in, void* out, uint64_t frame_count) const override; |
| 23 |
| 24 void Interleave( |
| 25 const void* in, |
| 26 uint64_t in_byte_count, |
| 27 void* out, |
| 28 uint64_t frame_count) const override; |
| 29 |
| 30 private: |
| 31 LpcmUtilImpl(const LpcmStreamType& stream_type); |
| 32 |
| 33 LpcmStreamType stream_type_; |
| 34 |
| 35 friend class LpcmUtil; |
| 36 }; |
| 37 |
| 38 std::unique_ptr<LpcmUtil> LpcmUtil::Create(const LpcmStreamType& stream_type) { |
| 39 LpcmUtil* result; |
| 40 switch (stream_type.sample_format()) { |
| 41 case LpcmStreamType::SampleFormat::kUnsigned8: |
| 42 case LpcmStreamType::SampleFormat::kAny: |
| 43 result = new LpcmUtilImpl<uint8_t>(stream_type); |
| 44 case LpcmStreamType::SampleFormat::kSigned16: |
| 45 result = new LpcmUtilImpl<int16_t>(stream_type); |
| 46 case LpcmStreamType::SampleFormat::kSigned24In32: |
| 47 result = new LpcmUtilImpl<int32_t>(stream_type); |
| 48 case LpcmStreamType::SampleFormat::kFloat: |
| 49 result = new LpcmUtilImpl<float>(stream_type); |
| 50 default: |
| 51 NOTREACHED() |
| 52 << "unsupported sample format " << stream_type.sample_format(); |
| 53 result = nullptr; |
| 54 } |
| 55 |
| 56 return std::unique_ptr<LpcmUtil>(result); |
| 57 } |
| 58 |
| 59 template<typename T> |
| 60 LpcmUtilImpl<T>::LpcmUtilImpl(const LpcmStreamType& stream_type) : |
| 61 stream_type_(stream_type) {} |
| 62 |
| 63 template<typename T> |
| 64 LpcmUtilImpl<T>::~LpcmUtilImpl() {} |
| 65 |
| 66 template<typename T> |
| 67 void LpcmUtilImpl<T>::Silence(void* buffer, uint64_t frame_count) const { |
| 68 T* sample = reinterpret_cast<T*>(buffer); |
| 69 for ( |
| 70 uint64_t sample_countdown = frame_count * stream_type_.channels(); |
| 71 sample_countdown != 0; |
| 72 --sample_countdown) { |
| 73 *sample = 0; |
| 74 sample++; |
| 75 } |
| 76 } |
| 77 |
| 78 template<> |
| 79 void LpcmUtilImpl<uint8_t>::Silence(void* buffer, uint64_t frame_count) const { |
| 80 std::memset(buffer, 0x80, frame_count * stream_type_.channels()); |
| 81 } |
| 82 |
| 83 template<> |
| 84 void LpcmUtilImpl<int16_t>::Silence(void* buffer, uint64_t frame_count) const { |
| 85 std::memset(buffer, 0, frame_count * stream_type_.channels()); |
| 86 } |
| 87 |
| 88 template<> |
| 89 void LpcmUtilImpl<int32_t>::Silence(void* buffer, uint64_t frame_count) const { |
| 90 std::memset(buffer, 0, frame_count * stream_type_.channels()); |
| 91 } |
| 92 |
| 93 template<typename T> |
| 94 void LpcmUtilImpl<T>::Copy(const void* in, void* out, uint64_t frame_count) |
| 95 const { |
| 96 std::memcpy(out, in, stream_type_.min_buffer_size(frame_count)); |
| 97 } |
| 98 |
| 99 template<typename T> |
| 100 void LpcmUtilImpl<T>::Mix(const void* in, void* out, uint64_t frame_count) |
| 101 const { |
| 102 const T* in_sample = reinterpret_cast<const T*>(in); |
| 103 T* out_sample = reinterpret_cast<T*>(out); |
| 104 for ( |
| 105 uint64_t sample_countdown = frame_count * stream_type_.channels(); |
| 106 sample_countdown != 0; |
| 107 --sample_countdown) { |
| 108 *out_sample += *in_sample; // TODO(dalesat): Limit. |
| 109 out_sample++; |
| 110 in_sample++; |
| 111 } |
| 112 } |
| 113 |
| 114 template<> |
| 115 void LpcmUtilImpl<uint8_t>::Mix(const void* in, void* out, uint64_t frame_count) |
| 116 const { |
| 117 const uint8_t* in_sample = reinterpret_cast<const uint8_t*>(in); |
| 118 uint8_t* out_sample = reinterpret_cast<uint8_t*>(out); |
| 119 for ( |
| 120 uint64_t sample_countdown = frame_count * stream_type_.channels(); |
| 121 sample_countdown != 0; |
| 122 --sample_countdown) { |
| 123 *out_sample = uint8_t(uint16_t(*out_sample) + uint16_t(*in_sample) - 0x80); |
| 124 // TODO(dalesat): Limit. |
| 125 out_sample++; |
| 126 in_sample++; |
| 127 } |
| 128 } |
| 129 |
| 130 template<typename T> |
| 131 void LpcmUtilImpl<T>::Interleave( |
| 132 const void* in, |
| 133 uint64_t in_byte_count, |
| 134 void* out, |
| 135 uint64_t frame_count) const { |
| 136 DCHECK(in); |
| 137 DCHECK(in_byte_count); |
| 138 DCHECK(out); |
| 139 DCHECK(frame_count); |
| 140 |
| 141 uint32_t channels = stream_type_.channels(); |
| 142 DCHECK(channels); |
| 143 DCHECK(in_byte_count % channels == 0); |
| 144 uint64_t in_channel_stride = in_byte_count / channels; |
| 145 |
| 146 const uint8_t* in_channel = reinterpret_cast<const uint8_t*>(in); |
| 147 uint8_t* out_channel = reinterpret_cast<uint8_t*>(out); |
| 148 |
| 149 for (uint32_t channel = channels; channel != 0; --channel) { |
| 150 const T* in_sample = reinterpret_cast<const T*>(in_channel); |
| 151 T* out_sample = reinterpret_cast<T*>(out_channel); |
| 152 for (uint64_t frame = frame_count; frame != 0; --frame) { |
| 153 *out_sample = *in_sample; |
| 154 ++in_sample; |
| 155 out_sample += channels; |
| 156 } |
| 157 in_channel += in_channel_stride; |
| 158 out_channel += stream_type_.sample_size(); |
| 159 } |
| 160 } |
| 161 |
| 162 } // namespace media |
| 163 } // namespace mojo |
OLD | NEW |