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

Side by Side Diff: webrtc/common_audio/audio_converter.cc

Issue 1316523002: Convert channel counts to size_t. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Fix compile 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
« no previous file with comments | « webrtc/common_audio/audio_converter.h ('k') | webrtc/common_audio/audio_converter_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/common_audio/audio_converter.h" 11 #include "webrtc/common_audio/audio_converter.h"
12 12
13 #include <cstring> 13 #include <cstring>
14 #include <utility> 14 #include <utility>
15 15
16 #include "webrtc/base/checks.h" 16 #include "webrtc/base/checks.h"
17 #include "webrtc/base/safe_conversions.h" 17 #include "webrtc/base/safe_conversions.h"
18 #include "webrtc/common_audio/channel_buffer.h" 18 #include "webrtc/common_audio/channel_buffer.h"
19 #include "webrtc/common_audio/resampler/push_sinc_resampler.h" 19 #include "webrtc/common_audio/resampler/push_sinc_resampler.h"
20 #include "webrtc/system_wrappers/include/scoped_vector.h" 20 #include "webrtc/system_wrappers/include/scoped_vector.h"
21 21
22 using rtc::checked_cast; 22 using rtc::checked_cast;
23 23
24 namespace webrtc { 24 namespace webrtc {
25 25
26 class CopyConverter : public AudioConverter { 26 class CopyConverter : public AudioConverter {
27 public: 27 public:
28 CopyConverter(int src_channels, size_t src_frames, int dst_channels, 28 CopyConverter(size_t src_channels, size_t src_frames, size_t dst_channels,
29 size_t dst_frames) 29 size_t dst_frames)
30 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {} 30 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {}
31 ~CopyConverter() override {}; 31 ~CopyConverter() override {};
32 32
33 void Convert(const float* const* src, size_t src_size, float* const* dst, 33 void Convert(const float* const* src, size_t src_size, float* const* dst,
34 size_t dst_capacity) override { 34 size_t dst_capacity) override {
35 CheckSizes(src_size, dst_capacity); 35 CheckSizes(src_size, dst_capacity);
36 if (src != dst) { 36 if (src != dst) {
37 for (int i = 0; i < src_channels(); ++i) 37 for (size_t i = 0; i < src_channels(); ++i)
38 std::memcpy(dst[i], src[i], dst_frames() * sizeof(*dst[i])); 38 std::memcpy(dst[i], src[i], dst_frames() * sizeof(*dst[i]));
39 } 39 }
40 } 40 }
41 }; 41 };
42 42
43 class UpmixConverter : public AudioConverter { 43 class UpmixConverter : public AudioConverter {
44 public: 44 public:
45 UpmixConverter(int src_channels, size_t src_frames, int dst_channels, 45 UpmixConverter(size_t src_channels, size_t src_frames, size_t dst_channels,
46 size_t dst_frames) 46 size_t dst_frames)
47 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {} 47 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {}
48 ~UpmixConverter() override {}; 48 ~UpmixConverter() override {};
49 49
50 void Convert(const float* const* src, size_t src_size, float* const* dst, 50 void Convert(const float* const* src, size_t src_size, float* const* dst,
51 size_t dst_capacity) override { 51 size_t dst_capacity) override {
52 CheckSizes(src_size, dst_capacity); 52 CheckSizes(src_size, dst_capacity);
53 for (size_t i = 0; i < dst_frames(); ++i) { 53 for (size_t i = 0; i < dst_frames(); ++i) {
54 const float value = src[0][i]; 54 const float value = src[0][i];
55 for (int j = 0; j < dst_channels(); ++j) 55 for (size_t j = 0; j < dst_channels(); ++j)
56 dst[j][i] = value; 56 dst[j][i] = value;
57 } 57 }
58 } 58 }
59 }; 59 };
60 60
61 class DownmixConverter : public AudioConverter { 61 class DownmixConverter : public AudioConverter {
62 public: 62 public:
63 DownmixConverter(int src_channels, size_t src_frames, int dst_channels, 63 DownmixConverter(size_t src_channels, size_t src_frames, size_t dst_channels,
64 size_t dst_frames) 64 size_t dst_frames)
65 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) { 65 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {
66 } 66 }
67 ~DownmixConverter() override {}; 67 ~DownmixConverter() override {};
68 68
69 void Convert(const float* const* src, size_t src_size, float* const* dst, 69 void Convert(const float* const* src, size_t src_size, float* const* dst,
70 size_t dst_capacity) override { 70 size_t dst_capacity) override {
71 CheckSizes(src_size, dst_capacity); 71 CheckSizes(src_size, dst_capacity);
72 float* dst_mono = dst[0]; 72 float* dst_mono = dst[0];
73 for (size_t i = 0; i < src_frames(); ++i) { 73 for (size_t i = 0; i < src_frames(); ++i) {
74 float sum = 0; 74 float sum = 0;
75 for (int j = 0; j < src_channels(); ++j) 75 for (size_t j = 0; j < src_channels(); ++j)
76 sum += src[j][i]; 76 sum += src[j][i];
77 dst_mono[i] = sum / src_channels(); 77 dst_mono[i] = sum / src_channels();
78 } 78 }
79 } 79 }
80 }; 80 };
81 81
82 class ResampleConverter : public AudioConverter { 82 class ResampleConverter : public AudioConverter {
83 public: 83 public:
84 ResampleConverter(int src_channels, size_t src_frames, int dst_channels, 84 ResampleConverter(size_t src_channels, size_t src_frames, size_t dst_channels,
85 size_t dst_frames) 85 size_t dst_frames)
86 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) { 86 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {
87 resamplers_.reserve(src_channels); 87 resamplers_.reserve(src_channels);
88 for (int i = 0; i < src_channels; ++i) 88 for (size_t i = 0; i < src_channels; ++i)
89 resamplers_.push_back(new PushSincResampler(src_frames, dst_frames)); 89 resamplers_.push_back(new PushSincResampler(src_frames, dst_frames));
90 } 90 }
91 ~ResampleConverter() override {}; 91 ~ResampleConverter() override {};
92 92
93 void Convert(const float* const* src, size_t src_size, float* const* dst, 93 void Convert(const float* const* src, size_t src_size, float* const* dst,
94 size_t dst_capacity) override { 94 size_t dst_capacity) override {
95 CheckSizes(src_size, dst_capacity); 95 CheckSizes(src_size, dst_capacity);
96 for (size_t i = 0; i < resamplers_.size(); ++i) 96 for (size_t i = 0; i < resamplers_.size(); ++i)
97 resamplers_[i]->Resample(src[i], src_frames(), dst[i], dst_frames()); 97 resamplers_[i]->Resample(src[i], src_frames(), dst[i], dst_frames());
98 } 98 }
(...skipping 30 matching lines...) Expand all
129 } 129 }
130 converters_.back()->Convert(buffers_.back()->channels(), 130 converters_.back()->Convert(buffers_.back()->channels(),
131 buffers_.back()->size(), dst, dst_capacity); 131 buffers_.back()->size(), dst, dst_capacity);
132 } 132 }
133 133
134 private: 134 private:
135 ScopedVector<AudioConverter> converters_; 135 ScopedVector<AudioConverter> converters_;
136 ScopedVector<ChannelBuffer<float>> buffers_; 136 ScopedVector<ChannelBuffer<float>> buffers_;
137 }; 137 };
138 138
139 rtc::scoped_ptr<AudioConverter> AudioConverter::Create(int src_channels, 139 rtc::scoped_ptr<AudioConverter> AudioConverter::Create(size_t src_channels,
140 size_t src_frames, 140 size_t src_frames,
141 int dst_channels, 141 size_t dst_channels,
142 size_t dst_frames) { 142 size_t dst_frames) {
143 rtc::scoped_ptr<AudioConverter> sp; 143 rtc::scoped_ptr<AudioConverter> sp;
144 if (src_channels > dst_channels) { 144 if (src_channels > dst_channels) {
145 if (src_frames != dst_frames) { 145 if (src_frames != dst_frames) {
146 ScopedVector<AudioConverter> converters; 146 ScopedVector<AudioConverter> converters;
147 converters.push_back(new DownmixConverter(src_channels, src_frames, 147 converters.push_back(new DownmixConverter(src_channels, src_frames,
148 dst_channels, src_frames)); 148 dst_channels, src_frames));
149 converters.push_back(new ResampleConverter(dst_channels, src_frames, 149 converters.push_back(new ResampleConverter(dst_channels, src_frames,
150 dst_channels, dst_frames)); 150 dst_channels, dst_frames));
151 sp.reset(new CompositionConverter(std::move(converters))); 151 sp.reset(new CompositionConverter(std::move(converters)));
(...skipping 24 matching lines...) Expand all
176 return sp; 176 return sp;
177 } 177 }
178 178
179 // For CompositionConverter. 179 // For CompositionConverter.
180 AudioConverter::AudioConverter() 180 AudioConverter::AudioConverter()
181 : src_channels_(0), 181 : src_channels_(0),
182 src_frames_(0), 182 src_frames_(0),
183 dst_channels_(0), 183 dst_channels_(0),
184 dst_frames_(0) {} 184 dst_frames_(0) {}
185 185
186 AudioConverter::AudioConverter(int src_channels, size_t src_frames, 186 AudioConverter::AudioConverter(size_t src_channels, size_t src_frames,
187 int dst_channels, size_t dst_frames) 187 size_t dst_channels, size_t dst_frames)
188 : src_channels_(src_channels), 188 : src_channels_(src_channels),
189 src_frames_(src_frames), 189 src_frames_(src_frames),
190 dst_channels_(dst_channels), 190 dst_channels_(dst_channels),
191 dst_frames_(dst_frames) { 191 dst_frames_(dst_frames) {
192 RTC_CHECK(dst_channels == src_channels || dst_channels == 1 || 192 RTC_CHECK(dst_channels == src_channels || dst_channels == 1 ||
193 src_channels == 1); 193 src_channels == 1);
194 } 194 }
195 195
196 void AudioConverter::CheckSizes(size_t src_size, size_t dst_capacity) const { 196 void AudioConverter::CheckSizes(size_t src_size, size_t dst_capacity) const {
197 RTC_CHECK_EQ(src_size, src_channels() * src_frames()); 197 RTC_CHECK_EQ(src_size, src_channels() * src_frames());
198 RTC_CHECK_GE(dst_capacity, dst_channels() * dst_frames()); 198 RTC_CHECK_GE(dst_capacity, dst_channels() * dst_frames());
199 } 199 }
200 200
201 } // namespace webrtc 201 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/common_audio/audio_converter.h ('k') | webrtc/common_audio/audio_converter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698