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

Side by Side Diff: services/media/framework/parts/lpcm_reformatter.cc

Issue 1577953002: Motown in-proc streaming framework used to implement media services. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: removed build/util/LASTCHANGE Created 4 years, 11 months 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 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/parts/lpcm_reformatter.h"
7
8 namespace mojo {
9 namespace media {
10
11 LpcmReformatter* LpcmReformatter::NewImpl(
12 const LpcmStreamType& in_type,
13 const LpcmStreamTypeSet& out_type) {
14 switch (in_type.sample_format()) {
15 case LpcmStreamType::SampleFormat::kUnsigned8:
16 switch (out_type.sample_format()) {
17 case LpcmStreamType::SampleFormat::kUnsigned8:
18 case LpcmStreamType::SampleFormat::kAny:
19 return new LpcmReformatterImpl<uint8_t, uint8_t>(
20 in_type, out_type);
21 case LpcmStreamType::SampleFormat::kSigned16:
22 return new LpcmReformatterImpl<uint8_t, int16_t>(
23 in_type, out_type);
24 case LpcmStreamType::SampleFormat::kSigned24In32:
25 return new LpcmReformatterImpl<uint8_t, int32_t>(
26 in_type, out_type);
27 case LpcmStreamType::SampleFormat::kFloat:
28 return new LpcmReformatterImpl<uint8_t, float>(
29 in_type, out_type);
30 default:
31 NOTREACHED() << "unsupported sample format";
32 return nullptr;
33 }
34 case LpcmStreamType::SampleFormat::kSigned16:
35 switch (out_type.sample_format()) {
36 case LpcmStreamType::SampleFormat::kUnsigned8:
37 return new LpcmReformatterImpl<int16_t, uint8_t>(
38 in_type, out_type);
39 case LpcmStreamType::SampleFormat::kSigned16:
40 case LpcmStreamType::SampleFormat::kAny:
41 return new LpcmReformatterImpl<int16_t, int16_t>(
42 in_type, out_type);
43 case LpcmStreamType::SampleFormat::kSigned24In32:
44 return new LpcmReformatterImpl<int16_t, int32_t>(
45 in_type, out_type);
46 case LpcmStreamType::SampleFormat::kFloat:
47 return new LpcmReformatterImpl<int16_t, float>(
48 in_type, out_type);
49 default:
50 NOTREACHED() << "unsupported sample format";
51 return nullptr;
52 }
53 case LpcmStreamType::SampleFormat::kSigned24In32:
54 switch (out_type.sample_format()) {
55 case LpcmStreamType::SampleFormat::kUnsigned8:
56 return new LpcmReformatterImpl<int32_t, uint8_t>(
57 in_type, out_type);
58 case LpcmStreamType::SampleFormat::kSigned16:
59 return new LpcmReformatterImpl<int32_t, int16_t>(
60 in_type, out_type);
61 case LpcmStreamType::SampleFormat::kSigned24In32:
62 case LpcmStreamType::SampleFormat::kAny:
63 return new LpcmReformatterImpl<int32_t, int32_t>(
64 in_type, out_type);
65 case LpcmStreamType::SampleFormat::kFloat:
66 return new LpcmReformatterImpl<int32_t, float>(
67 in_type, out_type);
68 default:
69 NOTREACHED() << "unsupported sample format";
70 return nullptr;
71 }
72 case LpcmStreamType::SampleFormat::kFloat:
73 switch (out_type.sample_format()) {
74 case LpcmStreamType::SampleFormat::kUnsigned8:
75 return new LpcmReformatterImpl<float, uint8_t>(
76 in_type, out_type);
77 case LpcmStreamType::SampleFormat::kSigned16:
78 return new LpcmReformatterImpl<float, int16_t>(
79 in_type, out_type);
80 case LpcmStreamType::SampleFormat::kSigned24In32:
81 return new LpcmReformatterImpl<float, int32_t>(
82 in_type, out_type);
83 case LpcmStreamType::SampleFormat::kFloat:
84 case LpcmStreamType::SampleFormat::kAny:
85 return new LpcmReformatterImpl<float, float>(in_type, out_type);
86 default:
87 NOTREACHED() << "unsupported sample format";
88 return nullptr;
89 }
90 default:
91 NOTREACHED() << "unsupported sample format";
92 return nullptr;
93 }
94 }
95
96 template<typename TIn, typename TOut>
97 LpcmReformatterImpl<TIn, TOut>::LpcmReformatterImpl(
98 const LpcmStreamType& in_type,
99 const LpcmStreamTypeSet& out_type) :
100 in_type_(in_type),
101 out_type_(
102 out_type.sample_format() == LpcmStreamType::SampleFormat::kAny ?
103 in_type.sample_format() :
104 out_type.sample_format(),
105 in_type.channels(),
106 in_type.frames_per_second()) {}
107
108 template<typename TIn, typename TOut>
109 LpcmReformatterImpl<TIn, TOut>::~LpcmReformatterImpl() {}
110
111 namespace {
112
113 template<typename TIn, typename TOut>
114 inline void CopySample(TOut* dest, TIn* source) {
115 *dest = static_cast<TOut>(*source);
116 }
117
118 inline void CopySample(uint8_t* dest, int16_t* source) {
119 *dest = static_cast<uint8_t>((*source >> 8) ^ 0x80);
120 }
121
122 inline void CopySample(uint8_t* dest, int32_t* source) {
123 *dest = static_cast<uint8_t>((*source >> 16) ^ 0x80); // TODO(dalesat): Limit?
124 }
125
126 inline void CopySample(uint8_t* dest, float* source) {
127 *dest = static_cast<uint8_t>((*source * 0x7f) + 128); // TODO(dalesat): Limit?
128 }
129
130 inline void CopySample(int16_t* dest, uint8_t* source) {
131 *dest = static_cast<int16_t>(*source ^ 0x80) << 8;
132 }
133
134 inline void CopySample(int16_t* dest, int32_t* source) {
135 *dest = static_cast<int16_t>(*source >> 8); // TODO(dalesat): Limit?
136 }
137
138 inline void CopySample(int16_t* dest, float* source) {
139 *dest = static_cast<int16_t>(*source * 0x7fff); // TODO(dalesat): Limit?
140 }
141
142 inline void CopySample(int32_t* dest, uint8_t* source) {
143 *dest = static_cast<int32_t>(*source ^ 0x80) << 16;
144 }
145
146 inline void CopySample(int32_t* dest, int16_t* source) {
147 *dest = static_cast<int32_t>(*source << 8);
148 }
149
150 inline void CopySample(int32_t* dest, float* source) {
151 *dest = static_cast<int32_t>(*source * 0x7fffff); // TODO(dalesat): Limit?
152 }
153
154 inline void CopySample(float* dest, uint8_t* source) {
155 *dest = static_cast<float>(*source ^ 0x80) / 0x80;
156 }
157
158 inline void CopySample(float* dest, int16_t* source) {
159 *dest = static_cast<float>(*source) / 0x8000;
160 }
161
162 inline void CopySample(float* dest, int32_t* source) {
163 *dest = static_cast<float>(*source) / 0x800000;
164 }
165
166 } // namespace
167
168 template<typename TIn, typename TOut>
169 LpcmStreamType& LpcmReformatterImpl<TIn, TOut>::input_stream_type() {
170 return in_type_;
171 }
172
173 template<typename TIn, typename TOut>
174 LpcmStreamType& LpcmReformatterImpl<TIn, TOut>::output_stream_type() {
175 return out_type_;
176 }
177
178 template<typename TIn, typename TOut>
179 void LpcmReformatterImpl<TIn, TOut>::TransformFrames(
180 LpcmFrames& source,
181 LpcmFrames& dest,
182 bool mix) {
183 DCHECK(source.buffer_);
184 DCHECK(source.frame_count_);
185 DCHECK(dest.buffer_);
186 DCHECK(dest.frame_count_);
187
188 uint64_t frame_count = std::min(source.frame_count_, dest.frame_count_);
189
190 uint8_t* in_channel = reinterpret_cast<uint8_t*>(source.buffer_);
191 uint8_t* out_channel = reinterpret_cast<uint8_t*>(dest.buffer_);
192
193 for (uint32_t channel = 0; channel < in_type_.channels(); channel++) {
194 TIn* in_sample = reinterpret_cast<TIn*>(in_channel);
195 TOut* out_sample = reinterpret_cast<TOut*>(out_channel);
196 for (uint64_t sample = 0; sample < frame_count; sample++) {
197 CopySample(out_sample, in_sample);
198 in_sample += in_type_.channels();
199 out_sample += out_type_.channels();
200 }
201 in_channel += in_type_.sample_size();
202 out_channel += out_type_.sample_size();
203 }
204
205 source.advance(frame_count);
206 dest.advance(frame_count);
207 }
208
209 } // namespace media
210 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698