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 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_.channels()); | |
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_.channels()); | |
johngro
2016/01/27 22:35:22
memset operates in units of bytes, so this needs t
dalesat
2016/01/28 18:49:17
stream_type_ has bytes_per_frame(). Using that. Th
| |
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_.channels()); | |
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 % channels == 0); | |
johngro
2016/01/27 22:35:22
sorry; I made a mistake in my last comment. This
dalesat
2016/01/28 18:49:17
Hmmm. I guess this is true because the samples are
| |
149 uint64_t in_channel_stride = in_byte_count / channels; | |
150 | |
151 const uint8_t* in_channel = reinterpret_cast<const uint8_t*>(in); | |
152 uint8_t* out_channel = reinterpret_cast<uint8_t*>(out); | |
153 | |
154 for (uint32_t channel = channels; channel != 0; --channel) { | |
155 const T* in_sample = reinterpret_cast<const T*>(in_channel); | |
156 T* out_sample = reinterpret_cast<T*>(out_channel); | |
157 for (uint64_t frame = frame_count; frame != 0; --frame) { | |
158 *out_sample = *in_sample; | |
159 ++in_sample; | |
160 out_sample += channels; | |
161 } | |
162 in_channel += in_channel_stride; | |
163 out_channel += stream_type_.sample_size(); | |
164 } | |
165 } | |
166 | |
167 } // namespace media | |
168 } // namespace mojo | |
OLD | NEW |