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/formatting.h" |
| 7 #include "services/media/framework/lpcm_util.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 break; |
| 45 case LpcmStreamType::SampleFormat::kSigned16: |
| 46 result = new LpcmUtilImpl<int16_t>(stream_type); |
| 47 break; |
| 48 case LpcmStreamType::SampleFormat::kSigned24In32: |
| 49 result = new LpcmUtilImpl<int32_t>(stream_type); |
| 50 break; |
| 51 case LpcmStreamType::SampleFormat::kFloat: |
| 52 result = new LpcmUtilImpl<float>(stream_type); |
| 53 break; |
| 54 default: |
| 55 NOTREACHED() |
| 56 << "unsupported sample format " << stream_type.sample_format(); |
| 57 result = nullptr; |
| 58 break; |
| 59 } |
| 60 |
| 61 return std::unique_ptr<LpcmUtil>(result); |
| 62 } |
| 63 |
| 64 template<typename T> |
| 65 LpcmUtilImpl<T>::LpcmUtilImpl(const LpcmStreamType& stream_type) : |
| 66 stream_type_(stream_type) {} |
| 67 |
| 68 template<typename T> |
| 69 LpcmUtilImpl<T>::~LpcmUtilImpl() {} |
| 70 |
| 71 template<typename T> |
| 72 void LpcmUtilImpl<T>::Silence(void* buffer, uint64_t frame_count) const { |
| 73 T* sample = reinterpret_cast<T*>(buffer); |
| 74 for ( |
| 75 uint64_t sample_countdown = frame_count * stream_type_.channels(); |
| 76 sample_countdown != 0; |
| 77 --sample_countdown) { |
| 78 *sample = 0; |
| 79 sample++; |
| 80 } |
| 81 } |
| 82 |
| 83 template<> |
| 84 void LpcmUtilImpl<uint8_t>::Silence(void* buffer, uint64_t frame_count) const { |
| 85 std::memset(buffer, 0x80, frame_count * stream_type_.bytes_per_frame()); |
| 86 } |
| 87 |
| 88 template<> |
| 89 void LpcmUtilImpl<int16_t>::Silence(void* buffer, uint64_t frame_count) const { |
| 90 std::memset(buffer, 0, frame_count * stream_type_.bytes_per_frame()); |
| 91 } |
| 92 |
| 93 template<> |
| 94 void LpcmUtilImpl<int32_t>::Silence(void* buffer, uint64_t frame_count) const { |
| 95 std::memset(buffer, 0, frame_count * stream_type_.bytes_per_frame()); |
| 96 } |
| 97 |
| 98 template<typename T> |
| 99 void LpcmUtilImpl<T>::Copy(const void* in, void* out, uint64_t frame_count) |
| 100 const { |
| 101 std::memcpy(out, in, stream_type_.min_buffer_size(frame_count)); |
| 102 } |
| 103 |
| 104 template<typename T> |
| 105 void LpcmUtilImpl<T>::Mix(const void* in, void* out, uint64_t frame_count) |
| 106 const { |
| 107 const T* in_sample = reinterpret_cast<const T*>(in); |
| 108 T* out_sample = reinterpret_cast<T*>(out); |
| 109 for ( |
| 110 uint64_t sample_countdown = frame_count * stream_type_.channels(); |
| 111 sample_countdown != 0; |
| 112 --sample_countdown) { |
| 113 *out_sample += *in_sample; // TODO(dalesat): Limit. |
| 114 out_sample++; |
| 115 in_sample++; |
| 116 } |
| 117 } |
| 118 |
| 119 template<> |
| 120 void LpcmUtilImpl<uint8_t>::Mix(const void* in, void* out, uint64_t frame_count) |
| 121 const { |
| 122 const uint8_t* in_sample = reinterpret_cast<const uint8_t*>(in); |
| 123 uint8_t* out_sample = reinterpret_cast<uint8_t*>(out); |
| 124 for ( |
| 125 uint64_t sample_countdown = frame_count * stream_type_.channels(); |
| 126 sample_countdown != 0; |
| 127 --sample_countdown) { |
| 128 *out_sample = uint8_t(uint16_t(*out_sample) + uint16_t(*in_sample) - 0x80); |
| 129 // TODO(dalesat): Limit. |
| 130 out_sample++; |
| 131 in_sample++; |
| 132 } |
| 133 } |
| 134 |
| 135 template<typename T> |
| 136 void LpcmUtilImpl<T>::Interleave( |
| 137 const void* in, |
| 138 uint64_t in_byte_count, |
| 139 void* out, |
| 140 uint64_t frame_count) const { |
| 141 DCHECK(in); |
| 142 DCHECK(in_byte_count); |
| 143 DCHECK(out); |
| 144 DCHECK(frame_count); |
| 145 |
| 146 uint32_t channels = stream_type_.channels(); |
| 147 DCHECK(channels); |
| 148 DCHECK(in_byte_count % stream_type_.bytes_per_frame() == 0); |
| 149 DCHECK(in_byte_count >= frame_count * stream_type_.bytes_per_frame()); |
| 150 uint64_t in_channel_stride = in_byte_count / stream_type_.bytes_per_frame(); |
| 151 |
| 152 const T* in_channel = reinterpret_cast<const T*>(in); |
| 153 T* out_channel = reinterpret_cast<T*>(out); |
| 154 |
| 155 for (uint32_t channel = channels; channel != 0; --channel) { |
| 156 const T* in_sample = in_channel; |
| 157 T* out_sample = out_channel; |
| 158 for (uint64_t frame = frame_count; frame != 0; --frame) { |
| 159 *out_sample = *in_sample; |
| 160 ++in_sample; |
| 161 out_sample += channels; |
| 162 } |
| 163 in_channel += in_channel_stride; |
| 164 ++out_channel; |
| 165 } |
| 166 } |
| 167 |
| 168 } // namespace media |
| 169 } // namespace mojo |
OLD | NEW |