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, uint64_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, uint64_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, uint64_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( |
25 const void* in, | 25 const void* in, |
26 uint64_t in_byte_count, | 26 size_t in_byte_count, |
27 void* out, | 27 void* out, |
28 uint64_t frame_count) const override; | 28 size_t frame_count) const override; |
29 | 29 |
30 private: | 30 private: |
31 LpcmUtilImpl(const LpcmStreamType& stream_type); | 31 LpcmUtilImpl(const LpcmStreamType& stream_type); |
32 | 32 |
33 LpcmStreamType stream_type_; | 33 LpcmStreamType stream_type_; |
34 | 34 |
35 friend class LpcmUtil; | 35 friend class LpcmUtil; |
36 }; | 36 }; |
37 | 37 |
38 std::unique_ptr<LpcmUtil> LpcmUtil::Create(const LpcmStreamType& stream_type) { | 38 std::unique_ptr<LpcmUtil> LpcmUtil::Create(const LpcmStreamType& stream_type) { |
(...skipping 23 matching lines...) Expand all Loading... |
62 } | 62 } |
63 | 63 |
64 template<typename T> | 64 template<typename T> |
65 LpcmUtilImpl<T>::LpcmUtilImpl(const LpcmStreamType& stream_type) : | 65 LpcmUtilImpl<T>::LpcmUtilImpl(const LpcmStreamType& stream_type) : |
66 stream_type_(stream_type) {} | 66 stream_type_(stream_type) {} |
67 | 67 |
68 template<typename T> | 68 template<typename T> |
69 LpcmUtilImpl<T>::~LpcmUtilImpl() {} | 69 LpcmUtilImpl<T>::~LpcmUtilImpl() {} |
70 | 70 |
71 template<typename T> | 71 template<typename T> |
72 void LpcmUtilImpl<T>::Silence(void* buffer, uint64_t frame_count) const { | 72 void LpcmUtilImpl<T>::Silence(void* buffer, size_t frame_count) const { |
73 T* sample = reinterpret_cast<T*>(buffer); | 73 T* sample = reinterpret_cast<T*>(buffer); |
74 for ( | 74 for ( |
75 uint64_t sample_countdown = frame_count * stream_type_.channels(); | 75 size_t sample_countdown = frame_count * stream_type_.channels(); |
76 sample_countdown != 0; | 76 sample_countdown != 0; |
77 --sample_countdown) { | 77 --sample_countdown) { |
78 *sample = 0; | 78 *sample = 0; |
79 sample++; | 79 sample++; |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 template<> | 83 template<> |
84 void LpcmUtilImpl<uint8_t>::Silence(void* buffer, uint64_t frame_count) const { | 84 void LpcmUtilImpl<uint8_t>::Silence(void* buffer, size_t frame_count) const { |
85 std::memset(buffer, 0x80, frame_count * stream_type_.bytes_per_frame()); | 85 std::memset(buffer, 0x80, frame_count * stream_type_.bytes_per_frame()); |
86 } | 86 } |
87 | 87 |
88 template<> | 88 template<> |
89 void LpcmUtilImpl<int16_t>::Silence(void* buffer, uint64_t frame_count) const { | 89 void LpcmUtilImpl<int16_t>::Silence(void* buffer, size_t frame_count) const { |
90 std::memset(buffer, 0, frame_count * stream_type_.bytes_per_frame()); | 90 std::memset(buffer, 0, frame_count * stream_type_.bytes_per_frame()); |
91 } | 91 } |
92 | 92 |
93 template<> | 93 template<> |
94 void LpcmUtilImpl<int32_t>::Silence(void* buffer, uint64_t frame_count) const { | 94 void LpcmUtilImpl<int32_t>::Silence(void* buffer, size_t frame_count) const { |
95 std::memset(buffer, 0, frame_count * stream_type_.bytes_per_frame()); | 95 std::memset(buffer, 0, frame_count * stream_type_.bytes_per_frame()); |
96 } | 96 } |
97 | 97 |
98 template<typename T> | 98 template<typename T> |
99 void LpcmUtilImpl<T>::Copy(const void* in, void* out, uint64_t frame_count) | 99 void LpcmUtilImpl<T>::Copy(const void* in, void* out, size_t frame_count) |
100 const { | 100 const { |
101 std::memcpy(out, in, stream_type_.min_buffer_size(frame_count)); | 101 std::memcpy(out, in, stream_type_.min_buffer_size(frame_count)); |
102 } | 102 } |
103 | 103 |
104 template<typename T> | 104 template<typename T> |
105 void LpcmUtilImpl<T>::Mix(const void* in, void* out, uint64_t frame_count) | 105 void LpcmUtilImpl<T>::Mix(const void* in, void* out, size_t frame_count) |
106 const { | 106 const { |
107 const T* in_sample = reinterpret_cast<const T*>(in); | 107 const T* in_sample = reinterpret_cast<const T*>(in); |
108 T* out_sample = reinterpret_cast<T*>(out); | 108 T* out_sample = reinterpret_cast<T*>(out); |
109 for ( | 109 for ( |
110 uint64_t sample_countdown = frame_count * stream_type_.channels(); | 110 size_t sample_countdown = frame_count * stream_type_.channels(); |
111 sample_countdown != 0; | 111 sample_countdown != 0; |
112 --sample_countdown) { | 112 --sample_countdown) { |
113 *out_sample += *in_sample; // TODO(dalesat): Limit. | 113 *out_sample += *in_sample; // TODO(dalesat): Limit. |
114 out_sample++; | 114 out_sample++; |
115 in_sample++; | 115 in_sample++; |
116 } | 116 } |
117 } | 117 } |
118 | 118 |
119 template<> | 119 template<> |
120 void LpcmUtilImpl<uint8_t>::Mix(const void* in, void* out, uint64_t frame_count) | 120 void LpcmUtilImpl<uint8_t>::Mix(const void* in, void* out, size_t frame_count) |
121 const { | 121 const { |
122 const uint8_t* in_sample = reinterpret_cast<const uint8_t*>(in); | 122 const uint8_t* in_sample = reinterpret_cast<const uint8_t*>(in); |
123 uint8_t* out_sample = reinterpret_cast<uint8_t*>(out); | 123 uint8_t* out_sample = reinterpret_cast<uint8_t*>(out); |
124 for ( | 124 for ( |
125 uint64_t sample_countdown = frame_count * stream_type_.channels(); | 125 size_t sample_countdown = frame_count * stream_type_.channels(); |
126 sample_countdown != 0; | 126 sample_countdown != 0; |
127 --sample_countdown) { | 127 --sample_countdown) { |
128 *out_sample = uint8_t(uint16_t(*out_sample) + uint16_t(*in_sample) - 0x80); | 128 *out_sample = uint8_t(uint16_t(*out_sample) + uint16_t(*in_sample) - 0x80); |
129 // TODO(dalesat): Limit. | 129 // TODO(dalesat): Limit. |
130 out_sample++; | 130 out_sample++; |
131 in_sample++; | 131 in_sample++; |
132 } | 132 } |
133 } | 133 } |
134 | 134 |
135 template<typename T> | 135 template<typename T> |
136 void LpcmUtilImpl<T>::Interleave( | 136 void LpcmUtilImpl<T>::Interleave( |
137 const void* in, | 137 const void* in, |
138 uint64_t in_byte_count, | 138 size_t in_byte_count, |
139 void* out, | 139 void* out, |
140 uint64_t frame_count) const { | 140 size_t frame_count) const { |
141 DCHECK(in); | 141 DCHECK(in); |
142 DCHECK(in_byte_count); | 142 DCHECK(in_byte_count); |
143 DCHECK(out); | 143 DCHECK(out); |
144 DCHECK(frame_count); | 144 DCHECK(frame_count); |
145 | 145 |
146 uint32_t channels = stream_type_.channels(); | 146 uint32_t channels = stream_type_.channels(); |
147 DCHECK(channels); | 147 DCHECK(channels); |
148 DCHECK(in_byte_count % stream_type_.bytes_per_frame() == 0); | 148 DCHECK(in_byte_count % stream_type_.bytes_per_frame() == 0); |
149 DCHECK(in_byte_count >= frame_count * stream_type_.bytes_per_frame()); | 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(); | 150 uint64_t in_channel_stride = in_byte_count / stream_type_.bytes_per_frame(); |
151 | 151 |
152 const T* in_channel = reinterpret_cast<const T*>(in); | 152 const T* in_channel = reinterpret_cast<const T*>(in); |
153 T* out_channel = reinterpret_cast<T*>(out); | 153 T* out_channel = reinterpret_cast<T*>(out); |
154 | 154 |
155 for (uint32_t channel = channels; channel != 0; --channel) { | 155 for (uint32_t channel = channels; channel != 0; --channel) { |
156 const T* in_sample = in_channel; | 156 const T* in_sample = in_channel; |
157 T* out_sample = out_channel; | 157 T* out_sample = out_channel; |
158 for (uint64_t frame = frame_count; frame != 0; --frame) { | 158 for (uint64_t frame = frame_count; frame != 0; --frame) { |
159 *out_sample = *in_sample; | 159 *out_sample = *in_sample; |
160 ++in_sample; | 160 ++in_sample; |
161 out_sample += channels; | 161 out_sample += channels; |
162 } | 162 } |
163 in_channel += in_channel_stride; | 163 in_channel += in_channel_stride; |
164 ++out_channel; | 164 ++out_channel; |
165 } | 165 } |
166 } | 166 } |
167 | 167 |
168 } // namespace media | 168 } // namespace media |
169 } // namespace mojo | 169 } // namespace mojo |
OLD | NEW |