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