andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
peah | 31b44f2 | 2016-04-05 21:57:48 | [diff] [blame] | 11 | #include "webrtc/common_audio/blocker.h" |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 12 | |
| 13 | #include <string.h> |
| 14 | |
Edward Lemur | 76de83e | 2017-07-06 17:44:34 | [diff] [blame] | 15 | #include "webrtc/rtc_base/checks.h" |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 16 | |
| 17 | namespace { |
| 18 | |
| 19 | // Adds |a| and |b| frame by frame into |result| (basically matrix addition). |
| 20 | void AddFrames(const float* const* a, |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 21 | size_t a_start_index, |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 22 | const float* const* b, |
| 23 | int b_start_index, |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 24 | size_t num_frames, |
Peter Kasting | 80590d9 | 2016-01-13 00:26:35 | [diff] [blame] | 25 | size_t num_channels, |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 26 | float* const* result, |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 27 | size_t result_start_index) { |
Peter Kasting | 80590d9 | 2016-01-13 00:26:35 | [diff] [blame] | 28 | for (size_t i = 0; i < num_channels; ++i) { |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 29 | for (size_t j = 0; j < num_frames; ++j) { |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 30 | result[i][j + result_start_index] = |
| 31 | a[i][j + a_start_index] + b[i][j + b_start_index]; |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // Copies |src| into |dst| channel by channel. |
| 37 | void CopyFrames(const float* const* src, |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 38 | size_t src_start_index, |
| 39 | size_t num_frames, |
Peter Kasting | 80590d9 | 2016-01-13 00:26:35 | [diff] [blame] | 40 | size_t num_channels, |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 41 | float* const* dst, |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 42 | size_t dst_start_index) { |
Peter Kasting | 80590d9 | 2016-01-13 00:26:35 | [diff] [blame] | 43 | for (size_t i = 0; i < num_channels; ++i) { |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 44 | memcpy(&dst[i][dst_start_index], |
| 45 | &src[i][src_start_index], |
mgraczyk@chromium.org | 8dfdcb9 | 2015-03-12 23:23:38 | [diff] [blame] | 46 | num_frames * sizeof(dst[i][dst_start_index])); |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
aluebs@webrtc.org | 7d87f7c | 2014-12-17 17:28:31 | [diff] [blame] | 50 | // Moves |src| into |dst| channel by channel. |
| 51 | void MoveFrames(const float* const* src, |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 52 | size_t src_start_index, |
| 53 | size_t num_frames, |
Peter Kasting | 80590d9 | 2016-01-13 00:26:35 | [diff] [blame] | 54 | size_t num_channels, |
aluebs@webrtc.org | 7d87f7c | 2014-12-17 17:28:31 | [diff] [blame] | 55 | float* const* dst, |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 56 | size_t dst_start_index) { |
Peter Kasting | 80590d9 | 2016-01-13 00:26:35 | [diff] [blame] | 57 | for (size_t i = 0; i < num_channels; ++i) { |
aluebs@webrtc.org | 7d87f7c | 2014-12-17 17:28:31 | [diff] [blame] | 58 | memmove(&dst[i][dst_start_index], |
| 59 | &src[i][src_start_index], |
mgraczyk@chromium.org | 8dfdcb9 | 2015-03-12 23:23:38 | [diff] [blame] | 60 | num_frames * sizeof(dst[i][dst_start_index])); |
aluebs@webrtc.org | 7d87f7c | 2014-12-17 17:28:31 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 64 | void ZeroOut(float* const* buffer, |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 65 | size_t starting_idx, |
| 66 | size_t num_frames, |
Peter Kasting | 80590d9 | 2016-01-13 00:26:35 | [diff] [blame] | 67 | size_t num_channels) { |
| 68 | for (size_t i = 0; i < num_channels; ++i) { |
mgraczyk@chromium.org | 8dfdcb9 | 2015-03-12 23:23:38 | [diff] [blame] | 69 | memset(&buffer[i][starting_idx], 0, |
| 70 | num_frames * sizeof(buffer[i][starting_idx])); |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | // Pointwise multiplies each channel of |frames| with |window|. Results are |
| 75 | // stored in |frames|. |
| 76 | void ApplyWindow(const float* window, |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 77 | size_t num_frames, |
Peter Kasting | 80590d9 | 2016-01-13 00:26:35 | [diff] [blame] | 78 | size_t num_channels, |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 79 | float* const* frames) { |
Peter Kasting | 80590d9 | 2016-01-13 00:26:35 | [diff] [blame] | 80 | for (size_t i = 0; i < num_channels; ++i) { |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 81 | for (size_t j = 0; j < num_frames; ++j) { |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 82 | frames[i][j] = frames[i][j] * window[j]; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 87 | size_t gcd(size_t a, size_t b) { |
| 88 | size_t tmp; |
aluebs@webrtc.org | cbe631e | 2015-01-13 22:28:35 | [diff] [blame] | 89 | while (b) { |
| 90 | tmp = a; |
| 91 | a = b; |
| 92 | b = tmp % b; |
| 93 | } |
| 94 | return a; |
| 95 | } |
| 96 | |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 97 | } // namespace |
| 98 | |
| 99 | namespace webrtc { |
| 100 | |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 101 | Blocker::Blocker(size_t chunk_size, |
| 102 | size_t block_size, |
Peter Kasting | 80590d9 | 2016-01-13 00:26:35 | [diff] [blame] | 103 | size_t num_input_channels, |
| 104 | size_t num_output_channels, |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 105 | const float* window, |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 106 | size_t shift_amount, |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 107 | BlockerCallback* callback) |
| 108 | : chunk_size_(chunk_size), |
| 109 | block_size_(block_size), |
| 110 | num_input_channels_(num_input_channels), |
| 111 | num_output_channels_(num_output_channels), |
aluebs@webrtc.org | cbe631e | 2015-01-13 22:28:35 | [diff] [blame] | 112 | initial_delay_(block_size_ - gcd(chunk_size, shift_amount)), |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 113 | frame_offset_(0), |
andrew@webrtc.org | a03359c | 2015-01-26 21:23:53 | [diff] [blame] | 114 | input_buffer_(num_input_channels_, chunk_size_ + initial_delay_), |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 115 | output_buffer_(chunk_size_ + initial_delay_, num_output_channels_), |
| 116 | input_block_(block_size_, num_input_channels_), |
| 117 | output_block_(block_size_, num_output_channels_), |
| 118 | window_(new float[block_size_]), |
| 119 | shift_amount_(shift_amount), |
| 120 | callback_(callback) { |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 121 | RTC_CHECK_LE(num_output_channels_, num_input_channels_); |
| 122 | RTC_CHECK_LE(shift_amount_, block_size_); |
mgraczyk@chromium.org | 8dfdcb9 | 2015-03-12 23:23:38 | [diff] [blame] | 123 | |
| 124 | memcpy(window_.get(), window, block_size_ * sizeof(*window_.get())); |
andrew | 04fa582 | 2015-07-28 07:52:59 | [diff] [blame] | 125 | input_buffer_.MoveReadPositionBackward(initial_delay_); |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 126 | } |
| 127 | |
kwiberg | 6967316 | 2016-08-29 20:10:29 | [diff] [blame] | 128 | Blocker::~Blocker() = default; |
| 129 | |
aluebs@webrtc.org | 7d87f7c | 2014-12-17 17:28:31 | [diff] [blame] | 130 | // When block_size < chunk_size the input and output buffers look like this: |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 131 | // |
| 132 | // delay* chunk_size chunk_size + delay* |
| 133 | // buffer: <-------------|---------------------|---------------|> |
| 134 | // _a_ _b_ _c_ |
| 135 | // |
| 136 | // On each call to ProcessChunk(): |
| 137 | // 1. New input gets read into sections _b_ and _c_ of the input buffer. |
| 138 | // 2. We block starting from frame_offset. |
| 139 | // 3. We block until we reach a block |bl| that doesn't contain any frames |
| 140 | // from sections _a_ or _b_ of the input buffer. |
| 141 | // 4. We window the current block, fire the callback for processing, window |
| 142 | // again, and overlap/add to the output buffer. |
| 143 | // 5. We copy sections _a_ and _b_ of the output buffer into output. |
aluebs@webrtc.org | 7d87f7c | 2014-12-17 17:28:31 | [diff] [blame] | 144 | // 6. For both the input and the output buffers, we copy section _c_ into |
| 145 | // section _a_. |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 146 | // 7. We set the new frame_offset to be the difference between the first frame |
| 147 | // of |bl| and the border between sections _b_ and _c_. |
| 148 | // |
aluebs@webrtc.org | 7d87f7c | 2014-12-17 17:28:31 | [diff] [blame] | 149 | // When block_size > chunk_size the input and output buffers look like this: |
| 150 | // |
| 151 | // chunk_size delay* chunk_size + delay* |
| 152 | // buffer: <-------------|---------------------|---------------|> |
| 153 | // _a_ _b_ _c_ |
| 154 | // |
| 155 | // On each call to ProcessChunk(): |
| 156 | // The procedure is the same as above, except for: |
| 157 | // 1. New input gets read into section _c_ of the input buffer. |
| 158 | // 3. We block until we reach a block |bl| that doesn't contain any frames |
| 159 | // from section _a_ of the input buffer. |
| 160 | // 5. We copy section _a_ of the output buffer into output. |
| 161 | // 6. For both the input and the output buffers, we copy sections _b_ and _c_ |
| 162 | // into section _a_ and _b_. |
| 163 | // 7. We set the new frame_offset to be the difference between the first frame |
| 164 | // of |bl| and the border between sections _a_ and _b_. |
| 165 | // |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 166 | // * delay here refers to inintial_delay_ |
| 167 | // |
| 168 | // TODO(claguna): Look at using ring buffers to eliminate some copies. |
| 169 | void Blocker::ProcessChunk(const float* const* input, |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 170 | size_t chunk_size, |
Peter Kasting | 80590d9 | 2016-01-13 00:26:35 | [diff] [blame] | 171 | size_t num_input_channels, |
| 172 | size_t num_output_channels, |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 173 | float* const* output) { |
henrikg | 5c075c8 | 2015-09-17 07:24:34 | [diff] [blame] | 174 | RTC_CHECK_EQ(chunk_size, chunk_size_); |
| 175 | RTC_CHECK_EQ(num_input_channels, num_input_channels_); |
| 176 | RTC_CHECK_EQ(num_output_channels, num_output_channels_); |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 177 | |
andrew@webrtc.org | a03359c | 2015-01-26 21:23:53 | [diff] [blame] | 178 | input_buffer_.Write(input, num_input_channels, chunk_size_); |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 179 | size_t first_frame_in_block = frame_offset_; |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 180 | |
| 181 | // Loop through blocks. |
| 182 | while (first_frame_in_block < chunk_size_) { |
andrew@webrtc.org | a03359c | 2015-01-26 21:23:53 | [diff] [blame] | 183 | input_buffer_.Read(input_block_.channels(), num_input_channels, |
| 184 | block_size_); |
andrew | 04fa582 | 2015-07-28 07:52:59 | [diff] [blame] | 185 | input_buffer_.MoveReadPositionBackward(block_size_ - shift_amount_); |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 186 | |
| 187 | ApplyWindow(window_.get(), |
| 188 | block_size_, |
| 189 | num_input_channels_, |
| 190 | input_block_.channels()); |
| 191 | callback_->ProcessBlock(input_block_.channels(), |
| 192 | block_size_, |
| 193 | num_input_channels_, |
| 194 | num_output_channels_, |
| 195 | output_block_.channels()); |
| 196 | ApplyWindow(window_.get(), |
| 197 | block_size_, |
| 198 | num_output_channels_, |
| 199 | output_block_.channels()); |
| 200 | |
| 201 | AddFrames(output_buffer_.channels(), |
| 202 | first_frame_in_block, |
| 203 | output_block_.channels(), |
| 204 | 0, |
| 205 | block_size_, |
| 206 | num_output_channels_, |
| 207 | output_buffer_.channels(), |
| 208 | first_frame_in_block); |
| 209 | |
| 210 | first_frame_in_block += shift_amount_; |
| 211 | } |
| 212 | |
| 213 | // Copy output buffer to output |
| 214 | CopyFrames(output_buffer_.channels(), |
| 215 | 0, |
| 216 | chunk_size_, |
| 217 | num_output_channels_, |
| 218 | output, |
| 219 | 0); |
| 220 | |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 221 | // Copy output buffer [chunk_size_, chunk_size_ + initial_delay] |
| 222 | // to output buffer [0, initial_delay], zero the rest. |
aluebs@webrtc.org | 7d87f7c | 2014-12-17 17:28:31 | [diff] [blame] | 223 | MoveFrames(output_buffer_.channels(), |
andrew@webrtc.org | 1bbdf57 | 2014-10-01 17:42:18 | [diff] [blame] | 224 | chunk_size, |
| 225 | initial_delay_, |
| 226 | num_output_channels_, |
| 227 | output_buffer_.channels(), |
| 228 | 0); |
| 229 | ZeroOut(output_buffer_.channels(), |
| 230 | initial_delay_, |
| 231 | chunk_size_, |
| 232 | num_output_channels_); |
| 233 | |
| 234 | // Calculate new starting frames. |
| 235 | frame_offset_ = first_frame_in_block - chunk_size_; |
| 236 | } |
| 237 | |
| 238 | } // namespace webrtc |