OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 <limits> | 5 #include <limits> |
6 #include <type_traits> | 6 #include <type_traits> |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "mojo/services/media/common/cpp/linear_transform.h" | 9 #include "mojo/services/media/common/cpp/linear_transform.h" |
10 #include "services/media/audio/platform/generic/output_formatter.h" | 10 #include "services/media/audio/platform/generic/output_formatter.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 static inline void Fill(void* dest, size_t samples) { | 61 static inline void Fill(void* dest, size_t samples) { |
62 ::memset(dest, 0x80, samples * sizeof(DType)); | 62 ::memset(dest, 0x80, samples * sizeof(DType)); |
63 } | 63 } |
64 }; | 64 }; |
65 | 65 |
66 // A templated class which implements the ProduceOutput and FillWithSilence | 66 // A templated class which implements the ProduceOutput and FillWithSilence |
67 // methods of OutputFormatter | 67 // methods of OutputFormatter |
68 template <typename DType, uint32_t DChCount> | 68 template <typename DType, uint32_t DChCount> |
69 class OutputFormatterImpl : public OutputFormatter { | 69 class OutputFormatterImpl : public OutputFormatter { |
70 public: | 70 public: |
71 explicit OutputFormatterImpl(const LpcmMediaTypeDetailsPtr& format) | 71 explicit OutputFormatterImpl(const AudioMediaTypeDetailsPtr& format) |
72 : OutputFormatter(format, sizeof(DType), DChCount) {} | 72 : OutputFormatter(format, sizeof(DType), DChCount) {} |
73 | 73 |
74 void ProduceOutput(const int32_t* source, | 74 void ProduceOutput(const int32_t* source, |
75 void* dest_void, | 75 void* dest_void, |
76 uint32_t frames) const override { | 76 uint32_t frames) const override { |
77 using DC = DstConverter<DType>; | 77 using DC = DstConverter<DType>; |
78 DType* dest = static_cast<DType*>(dest_void); | 78 DType* dest = static_cast<DType*>(dest_void); |
79 | 79 |
80 for (size_t i = 0; i < (static_cast<size_t>(frames) * DChCount); ++i) { | 80 for (size_t i = 0; i < (static_cast<size_t>(frames) * DChCount); ++i) { |
81 register int32_t val = source[i]; | 81 register int32_t val = source[i]; |
82 if (val > std::numeric_limits<int16_t>::max()) { | 82 if (val > std::numeric_limits<int16_t>::max()) { |
83 dest[i] = DC::Convert(std::numeric_limits<int16_t>::max()); | 83 dest[i] = DC::Convert(std::numeric_limits<int16_t>::max()); |
84 } else if (val < std::numeric_limits<int16_t>::min()) { | 84 } else if (val < std::numeric_limits<int16_t>::min()) { |
85 dest[i] = DC::Convert(std::numeric_limits<int16_t>::min()); | 85 dest[i] = DC::Convert(std::numeric_limits<int16_t>::min()); |
86 } else { | 86 } else { |
87 dest[i] = DC::Convert(val); | 87 dest[i] = DC::Convert(val); |
88 } | 88 } |
89 } | 89 } |
90 } | 90 } |
91 | 91 |
92 void FillWithSilence(void* dest, uint32_t frames) const override { | 92 void FillWithSilence(void* dest, uint32_t frames) const override { |
93 SilenceMaker<DType>::Fill(dest, frames * DChCount); | 93 SilenceMaker<DType>::Fill(dest, frames * DChCount); |
94 } | 94 } |
95 }; | 95 }; |
96 | 96 |
97 // Constructor/destructor for the common OutputFormatter base class. | 97 // Constructor/destructor for the common OutputFormatter base class. |
98 OutputFormatter::OutputFormatter(const LpcmMediaTypeDetailsPtr& format, | 98 OutputFormatter::OutputFormatter(const AudioMediaTypeDetailsPtr& format, |
99 uint32_t bytes_per_sample, | 99 uint32_t bytes_per_sample, |
100 uint32_t channels) | 100 uint32_t channels) |
101 : format_(format.Clone()), | 101 : format_(format.Clone()), |
102 channels_(channels), | 102 channels_(channels), |
103 bytes_per_sample_(bytes_per_sample), | 103 bytes_per_sample_(bytes_per_sample), |
104 bytes_per_frame_(bytes_per_sample * channels) {} | 104 bytes_per_frame_(bytes_per_sample * channels) {} |
105 | 105 |
106 OutputFormatter::~OutputFormatter() {} | 106 OutputFormatter::~OutputFormatter() {} |
107 | 107 |
108 // Selection routines which will instantiate a particular templatized version of | 108 // Selection routines which will instantiate a particular templatized version of |
109 // the output formatter. | 109 // the output formatter. |
110 template <typename DType> | 110 template <typename DType> |
111 static inline OutputFormatterPtr SelectOF( | 111 static inline OutputFormatterPtr SelectOF( |
112 const LpcmMediaTypeDetailsPtr& format) { | 112 const AudioMediaTypeDetailsPtr& format) { |
113 switch (format->channels) { | 113 switch (format->channels) { |
114 case 1: | 114 case 1: |
115 return OutputFormatterPtr(new OutputFormatterImpl<DType, 1>(format)); | 115 return OutputFormatterPtr(new OutputFormatterImpl<DType, 1>(format)); |
116 case 2: | 116 case 2: |
117 return OutputFormatterPtr(new OutputFormatterImpl<DType, 2>(format)); | 117 return OutputFormatterPtr(new OutputFormatterImpl<DType, 2>(format)); |
118 default: | 118 default: |
119 LOG(ERROR) << "Unsupported output channels " | 119 LOG(ERROR) << "Unsupported output channels " |
120 << format->channels; | 120 << format->channels; |
121 return nullptr; | 121 return nullptr; |
122 } | 122 } |
123 } | 123 } |
124 | 124 |
125 OutputFormatterPtr OutputFormatter::Select( | 125 OutputFormatterPtr OutputFormatter::Select( |
126 const LpcmMediaTypeDetailsPtr& format) { | 126 const AudioMediaTypeDetailsPtr& format) { |
127 DCHECK(format); | 127 DCHECK(format); |
128 | 128 |
129 switch (format->sample_format) { | 129 switch (format->sample_format) { |
130 case LpcmSampleFormat::UNSIGNED_8: | 130 case AudioSampleFormat::UNSIGNED_8: |
131 return SelectOF<uint8_t>(format); | 131 return SelectOF<uint8_t>(format); |
132 case LpcmSampleFormat::SIGNED_16: | 132 case AudioSampleFormat::SIGNED_16: |
133 return SelectOF<int16_t>(format); | 133 return SelectOF<int16_t>(format); |
134 default: | 134 default: |
135 LOG(ERROR) << "Unsupported output sample format " | 135 LOG(ERROR) << "Unsupported output sample format " |
136 << format->sample_format; | 136 << format->sample_format; |
137 return nullptr; | 137 return nullptr; |
138 } | 138 } |
139 } | 139 } |
140 | 140 |
141 } // namespace audio | 141 } // namespace audio |
142 } // namespace media | 142 } // namespace media |
143 } // namespace mojo | 143 } // namespace mojo |
OLD | NEW |