OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 <limits> | |
6 #include <type_traits> | |
7 | |
8 #include "base/logging.h" | |
9 #include "services/media/audio/audio_track_impl.h" | |
10 #include "services/media/audio/platform/generic/mixers/mixer_utils.h" | |
11 #include "services/media/audio/platform/generic/mixers/point_sampler.h" | |
12 | |
13 namespace mojo { | |
14 namespace media { | |
15 namespace audio { | |
16 namespace mixers { | |
17 | |
18 // Point Sample Mixer implementation. | |
19 template <typename DType, | |
20 size_t DChCount, | |
21 typename SType, | |
22 size_t SChCount> | |
23 class PointSamplerImpl : public PointSampler { | |
24 public: | |
25 PointSamplerImpl() {} | |
26 | |
27 bool Mix(void* dst, | |
28 uint32_t dst_frames, | |
29 uint32_t* dst_offset, | |
30 const void* src, | |
31 uint32_t frac_src_frames, | |
32 uint32_t* frac_src_offset, | |
33 uint32_t frac_step_size, | |
34 bool accumulate) override; | |
35 | |
36 private: | |
37 template <bool DoAccumulate> | |
38 static inline bool Mix(void* dst, | |
39 uint32_t dst_frames, | |
40 uint32_t* dst_offset, | |
41 const void* src, | |
42 uint32_t frac_src_frames, | |
43 uint32_t* frac_src_offset, | |
44 uint32_t frac_step_size); | |
45 }; | |
46 | |
47 template <typename DType, | |
48 size_t DChCount, | |
49 typename SType, | |
50 size_t SChCount> | |
51 template <bool DoAccumulate> | |
52 inline bool PointSamplerImpl<DType, DChCount, SType, SChCount>::Mix( | |
53 void* dst_void, | |
54 uint32_t dst_frames, | |
55 uint32_t* dst_offset, | |
56 const void* src_void, | |
57 uint32_t frac_src_frames, | |
58 uint32_t* frac_src_offset, | |
59 uint32_t frac_step_size) { | |
60 using SR = utils::SrcReader<SType, SChCount, DChCount>; | |
61 using DM = utils::DstMixer<DType, DoAccumulate>; | |
62 | |
63 const SType* src = static_cast<const SType*>(src_void); | |
64 DType* dst = static_cast<DType*>(dst_void); | |
65 uint32_t doff = *dst_offset; | |
66 uint32_t soff = *frac_src_offset; | |
67 | |
68 DCHECK_LT(doff, dst_frames); | |
69 DCHECK_LT(soff, frac_src_frames); | |
70 | |
71 while ((doff < dst_frames) && (soff < frac_src_frames)) { | |
72 uint32_t S = (soff >> AudioTrackImpl::PTS_FRACTIONAL_BITS) * SChCount; | |
jeffbrown
2015/11/04 23:43:34
variables shouldn't be uppercase
johngro
2015/11/06 02:20:27
Done.
| |
73 DType* out = dst + (doff * DChCount); | |
74 | |
75 for (size_t D = 0; D < DChCount; ++D) { | |
jeffbrown
2015/11/04 23:43:34
We could handle all of the specialization for accu
johngro
2015/11/06 02:20:27
I disagree. I don't want to write two versions of
| |
76 int32_t sample = SR::Read(src + S + (D / SR::DstPerSrc)); | |
77 out[D] = DM::Mix(out + D, sample); | |
78 } | |
79 | |
80 doff += 1; | |
81 soff += frac_step_size; | |
82 } | |
83 | |
84 *dst_offset = doff; | |
85 *frac_src_offset = soff; | |
86 | |
87 return (soff >= frac_src_frames); | |
88 } | |
89 | |
90 template <typename DType, | |
91 size_t DChCount, | |
92 typename SType, | |
93 size_t SChCount> | |
94 bool PointSamplerImpl<DType, DChCount, SType, SChCount>::Mix( | |
95 void* dst, | |
96 uint32_t dst_frames, | |
97 uint32_t* dst_offset, | |
98 const void* src, | |
99 uint32_t frac_src_frames, | |
100 uint32_t* frac_src_offset, | |
101 uint32_t frac_step_size, | |
102 bool accumulate) { | |
103 return accumulate ? Mix<true>(dst, dst_frames, dst_offset, | |
104 src, frac_src_frames, frac_src_offset, | |
105 frac_step_size) | |
106 : Mix<false>(dst, dst_frames, dst_offset, | |
107 src, frac_src_frames, frac_src_offset, | |
108 frac_step_size); | |
109 } | |
110 | |
111 // Templates used to expand all of the different combinations of the possible | |
112 // Point Sampler Mixer configurations. | |
113 template <typename DType, | |
114 size_t DChCount, | |
115 typename SType, | |
116 size_t SChCount> | |
117 static inline MixerPtr SelectPSM(const LpcmMediaTypeDetailsPtr& src_format, | |
118 const LpcmMediaTypeDetailsPtr& dst_format) { | |
119 return MixerPtr(new PointSamplerImpl<DType, DChCount, SType, SChCount>()); | |
120 } | |
121 | |
122 template <typename DType, | |
123 size_t DChCount, | |
124 typename SType> | |
125 static inline MixerPtr SelectPSM(const LpcmMediaTypeDetailsPtr& src_format, | |
126 const LpcmMediaTypeDetailsPtr& dst_format) { | |
127 switch (src_format->samples_per_frame) { | |
128 case 1: | |
129 return SelectPSM<DType, DChCount, SType, 1>(src_format, dst_format); | |
130 case 2: | |
131 return SelectPSM<DType, DChCount, SType, 2>(src_format, dst_format); | |
132 default: | |
133 return nullptr; | |
134 } | |
135 } | |
136 | |
137 template <typename DType, | |
138 size_t DChCount> | |
139 static inline MixerPtr SelectPSM(const LpcmMediaTypeDetailsPtr& src_format, | |
140 const LpcmMediaTypeDetailsPtr& dst_format) { | |
141 switch (src_format->sample_format) { | |
142 case LpcmSampleFormat::UNSIGNED_8: | |
143 return SelectPSM<DType, DChCount, uint8_t>(src_format, dst_format); | |
144 case LpcmSampleFormat::SIGNED_16: | |
145 return SelectPSM<DType, DChCount, int16_t>(src_format, dst_format); | |
146 default: | |
147 return nullptr; | |
148 } | |
149 } | |
150 | |
151 template <typename DType> | |
152 static inline MixerPtr SelectPSM(const LpcmMediaTypeDetailsPtr& src_format, | |
153 const LpcmMediaTypeDetailsPtr& dst_format) { | |
154 switch (dst_format->samples_per_frame) { | |
155 case 1: | |
156 return SelectPSM<DType, 1>(src_format, dst_format); | |
157 case 2: | |
158 return SelectPSM<DType, 2>(src_format, dst_format); | |
159 default: | |
160 return nullptr; | |
161 } | |
162 } | |
163 | |
164 MixerPtr PointSampler::Select(const LpcmMediaTypeDetailsPtr& src_format, | |
165 const LpcmMediaTypeDetailsPtr& dst_format) { | |
166 switch (dst_format->sample_format) { | |
167 case LpcmSampleFormat::UNSIGNED_8: | |
168 return SelectPSM<uint8_t>(src_format, dst_format); | |
169 case LpcmSampleFormat::SIGNED_16: | |
170 return SelectPSM<int16_t>(src_format, dst_format); | |
171 default: | |
172 return nullptr; | |
173 } | |
174 } | |
175 | |
176 } // namespace mixers | |
177 } // namespace audio | |
178 } // namespace media | |
179 } // namespace mojo | |
OLD | NEW |