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