Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: services/media/audio/platform/generic/mixers/point_sampler.cc

Issue 1424933002: Add an initial revision of an audio server. (Closed) Base URL: https://github.com/domokit/mojo.git@change4
Patch Set: fix issues discovered with initial preflight Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "base/logging.h"
6 #include "services/media/audio/audio_track_impl.h"
7 #include "services/media/audio/platform/generic/mixers/mixer_utils.h"
8 #include "services/media/audio/platform/generic/mixers/point_sampler.h"
9
10 namespace mojo {
11 namespace media {
12 namespace audio {
13 namespace mixers {
14
15 // Point Sample Mixer implementation.
16 template <typename DType,
17 size_t DChCount,
18 typename SType,
19 size_t SChCount>
20 class PointSamplerImpl : public PointSampler {
21 public:
22 PointSamplerImpl() {}
23
24 bool Mix(void* dst,
25 uint32_t dst_frames,
26 uint32_t* dst_offset,
27 const void* src,
28 uint32_t frac_src_frames,
29 uint32_t* frac_src_offset,
30 uint32_t frac_step_size,
31 bool accumulate) override;
32
33 private:
34 template <bool DoAccumulate>
35 static inline bool Mix(void* dst,
36 uint32_t dst_frames,
37 uint32_t* dst_offset,
38 const void* src,
39 uint32_t frac_src_frames,
40 uint32_t* frac_src_offset,
41 uint32_t frac_step_size);
42 };
43
44 template <typename DType,
45 size_t DChCount,
46 typename SType,
47 size_t SChCount>
48 template <bool DoAccumulate>
49 inline bool PointSamplerImpl<DType, DChCount, SType, SChCount>::Mix(
50 void* dst_void,
51 uint32_t dst_frames,
52 uint32_t* dst_offset,
53 const void* src_void,
54 uint32_t frac_src_frames,
55 uint32_t* frac_src_offset,
56 uint32_t frac_step_size) {
57 using SR = utils::SrcReader<SType, SChCount, DChCount>;
58 using DM = utils::DstMixer<DType, DoAccumulate>;
59
60 const SType* src = static_cast<const SType*>(src_void);
61 DType* dst = static_cast<DType*>(dst_void);
62 uint32_t doff = *dst_offset;
63 uint32_t soff = *frac_src_offset;
64
65 DCHECK_LT(doff, dst_frames);
66 DCHECK_LT(soff, frac_src_frames);
67
68 while ((doff < dst_frames) && (soff < frac_src_frames)) {
69 uint32_t src_iter;
70 DType* out;
71
72 src_iter = (soff >> AudioTrackImpl::PTS_FRACTIONAL_BITS) * SChCount;
73 out = dst + (doff * DChCount);
74
75 for (size_t dst_iter = 0; dst_iter < DChCount; ++dst_iter) {
76 int32_t sample = SR::Read(src + src_iter + (dst_iter / SR::DstPerSrc));
77 out[dst_iter] = DM::Mix(out + dst_iter, 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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698