OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 if (frame->num_channels_ != 2) { | 107 if (frame->num_channels_ != 2) { |
108 return -1; | 108 return -1; |
109 } | 109 } |
110 | 110 |
111 StereoToMono(frame->data_, frame->samples_per_channel_, frame->data_); | 111 StereoToMono(frame->data_, frame->samples_per_channel_, frame->data_); |
112 frame->num_channels_ = 1; | 112 frame->num_channels_ = 1; |
113 | 113 |
114 return 0; | 114 return 0; |
115 } | 115 } |
116 | 116 |
117 void AudioFrameOperations::QuadToMono(const int16_t* src_audio, | |
hlundin-webrtc
2017/02/24 09:46:35
The order of the methods in the .cc file and the .
jens.nielsen
2017/02/24 14:55:48
Done.
| |
118 size_t samples_per_channel, | |
119 int16_t* dst_audio) { | |
120 for (size_t i = 0; i < samples_per_channel; i++) { | |
121 dst_audio[i] = (src_audio[4 * i] + src_audio[4 * i + 1] + | |
122 src_audio[4 * i + 2] + src_audio[4 * i + 3]) >> 2; | |
aleloi
2017/02/24 10:39:31
I think this can overflow. If so, can the sum be s
jens.nielsen
2017/02/24 11:11:18
I was too, and was about to fix that here as well
aleloi
2017/02/24 13:32:52
I tried to look it up:
http://en.cppreference.com/
jens.nielsen
2017/02/24 14:55:48
Done.
| |
123 } | |
124 } | |
125 | |
126 int AudioFrameOperations::QuadToMono(AudioFrame* frame) { | |
127 if (frame->num_channels_ != 4) { | |
128 return -1; | |
129 } | |
130 | |
aleloi
2017/02/24 10:39:31
I think this is a good place to RTC_DCHECK_LE that
jens.nielsen
2017/02/24 14:55:48
Done. But the damage is already done here if the b
| |
131 QuadToMono(frame->data_, frame->samples_per_channel_, frame->data_); | |
132 frame->num_channels_ = 1; | |
133 | |
134 return 0; | |
135 } | |
136 | |
137 void AudioFrameOperations::QuadToStereo(const int16_t* src_audio, | |
138 size_t samples_per_channel, | |
139 int16_t* dst_audio) { | |
140 for (size_t i = 0; i < samples_per_channel; i++) { | |
141 dst_audio[i * 2] = (src_audio[4 * i] + src_audio[4 * i + 1]) >> 1; | |
142 dst_audio[i * 2 + 1] = (src_audio[4 * i + 2] + src_audio[4 * i + 3]) >> 1; | |
aleloi
2017/02/24 10:39:31
I have concerns for overflows here as well.
jens.nielsen
2017/02/24 14:55:48
Done.
| |
143 } | |
144 } | |
145 | |
146 int AudioFrameOperations::QuadToStereo(AudioFrame* frame) { | |
147 if (frame->num_channels_ != 4) { | |
148 return -1; | |
149 } | |
150 | |
aleloi
2017/02/24 10:39:31
Please add
RTC_DCHECK_LE(samples_per_channel_ * 4
jens.nielsen
2017/02/24 14:55:48
Done.
| |
151 QuadToStereo(frame->data_, frame->samples_per_channel_, frame->data_); | |
152 frame->num_channels_ = 2; | |
153 | |
154 return 0; | |
155 } | |
156 | |
157 void AudioFrameOperations::DownmixChannels(const int16_t* src_audio, | |
158 size_t src_channels, | |
159 size_t samples_per_channel, | |
160 int16_t* dst_audio, | |
161 size_t dst_channels) { | |
162 if (src_channels == 2 && dst_channels == 1) { | |
163 StereoToMono(src_audio, samples_per_channel, dst_audio); | |
164 } else if (src_channels == 4 && dst_channels == 2) { | |
165 QuadToStereo(src_audio, samples_per_channel, dst_audio); | |
166 } else if (src_channels == 4 && dst_channels == 1) { | |
167 QuadToMono(src_audio, samples_per_channel, dst_audio); | |
168 } | |
hlundin-webrtc
2017/02/24 09:46:36
I'd like to see some kind of DCHECK that the numbe
jens.nielsen
2017/02/24 14:55:48
Done.
| |
169 } | |
170 | |
171 int AudioFrameOperations::DownmixChannels(AudioFrame* frame, | |
172 size_t dst_channels) { | |
173 if (frame->num_channels_ >= dst_channels) { | |
hlundin-webrtc
2017/02/24 09:46:35
This is not needed. You are explicitly testing all
jens.nielsen
2017/02/24 14:55:48
Done.
| |
174 return -1; | |
175 } | |
176 | |
177 if (frame->num_channels_ == 2 && dst_channels == 1) { | |
178 return StereoToMono(frame); | |
179 } else if (frame->num_channels_ == 4 && dst_channels == 2) { | |
180 return QuadToStereo(frame); | |
181 } else if (frame->num_channels_ == 4 && dst_channels == 1) { | |
182 return QuadToMono(frame); | |
183 } else { | |
hlundin-webrtc
2017/02/24 09:46:36
Delete the else statement and simply return -1 unc
jens.nielsen
2017/02/24 14:55:48
Done.
| |
184 return -1; | |
185 } | |
186 | |
187 return 0; | |
188 } | |
189 | |
117 void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) { | 190 void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) { |
118 RTC_DCHECK(frame); | 191 RTC_DCHECK(frame); |
119 if (frame->num_channels_ != 2) { | 192 if (frame->num_channels_ != 2) { |
120 return; | 193 return; |
121 } | 194 } |
122 | 195 |
123 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) { | 196 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) { |
124 int16_t temp_data = frame->data_[i]; | 197 int16_t temp_data = frame->data_[i]; |
125 frame->data_[i] = frame->data_[i + 1]; | 198 frame->data_[i] = frame->data_[i + 1]; |
126 frame->data_[i + 1] = temp_data; | 199 frame->data_[i + 1] = temp_data; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 frame.data_[i] = -32768; | 290 frame.data_[i] = -32768; |
218 } else if (temp_data > 32767) { | 291 } else if (temp_data > 32767) { |
219 frame.data_[i] = 32767; | 292 frame.data_[i] = 32767; |
220 } else { | 293 } else { |
221 frame.data_[i] = static_cast<int16_t>(temp_data); | 294 frame.data_[i] = static_cast<int16_t>(temp_data); |
222 } | 295 } |
223 } | 296 } |
224 return 0; | 297 return 0; |
225 } | 298 } |
226 } // namespace webrtc | 299 } // namespace webrtc |
OLD | NEW |