peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 "modules/audio_processing/aec3/block_framer.h" |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 15 | #include "modules/audio_processing/aec3/aec3_common.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 20 | BlockFramer::BlockFramer(size_t num_bands, size_t num_channels) |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 21 | : num_bands_(num_bands), |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 22 | num_channels_(num_channels), |
| 23 | buffer_(num_bands_, |
| 24 | std::vector<std::vector<float>>( |
| 25 | num_channels, |
| 26 | std::vector<float>(kBlockSize, 0.f))) { |
| 27 | RTC_DCHECK_LT(0, num_bands); |
| 28 | RTC_DCHECK_LT(0, num_channels); |
| 29 | } |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 30 | |
| 31 | BlockFramer::~BlockFramer() = default; |
| 32 | |
| 33 | // All the constants are chosen so that the buffer is either empty or has enough |
| 34 | // samples for InsertBlockAndExtractSubFrame to produce a frame. In order to |
| 35 | // achieve this, the InsertBlockAndExtractSubFrame and InsertBlock methods need |
| 36 | // to be called in the correct order. |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 37 | void BlockFramer::InsertBlock( |
| 38 | const std::vector<std::vector<std::vector<float>>>& block) { |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 39 | RTC_DCHECK_EQ(num_bands_, block.size()); |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 40 | for (size_t band = 0; band < num_bands_; ++band) { |
| 41 | RTC_DCHECK_EQ(num_channels_, block[band].size()); |
| 42 | for (size_t channel = 0; channel < num_channels_; ++channel) { |
| 43 | RTC_DCHECK_EQ(kBlockSize, block[band][channel].size()); |
| 44 | RTC_DCHECK_EQ(0, buffer_[band][channel].size()); |
| 45 | |
| 46 | buffer_[band][channel].insert(buffer_[band][channel].begin(), |
| 47 | block[band][channel].begin(), |
| 48 | block[band][channel].end()); |
| 49 | } |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | void BlockFramer::InsertBlockAndExtractSubFrame( |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 54 | const std::vector<std::vector<std::vector<float>>>& block, |
| 55 | std::vector<std::vector<rtc::ArrayView<float>>>* sub_frame) { |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 56 | RTC_DCHECK(sub_frame); |
| 57 | RTC_DCHECK_EQ(num_bands_, block.size()); |
| 58 | RTC_DCHECK_EQ(num_bands_, sub_frame->size()); |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 59 | for (size_t band = 0; band < num_bands_; ++band) { |
| 60 | RTC_DCHECK_EQ(num_channels_, block[band].size()); |
| 61 | RTC_DCHECK_EQ(num_channels_, (*sub_frame)[0].size()); |
| 62 | for (size_t channel = 0; channel < num_channels_; ++channel) { |
| 63 | RTC_DCHECK_LE(kSubFrameLength, |
| 64 | buffer_[band][channel].size() + kBlockSize); |
| 65 | RTC_DCHECK_EQ(kBlockSize, block[band][channel].size()); |
| 66 | RTC_DCHECK_GE(kBlockSize, buffer_[band][channel].size()); |
| 67 | RTC_DCHECK_EQ(kSubFrameLength, (*sub_frame)[band][channel].size()); |
| 68 | |
| 69 | const int samples_to_frame = |
| 70 | kSubFrameLength - buffer_[band][channel].size(); |
| 71 | std::copy(buffer_[band][channel].begin(), buffer_[band][channel].end(), |
| 72 | (*sub_frame)[band][channel].begin()); |
| 73 | std::copy( |
| 74 | block[band][channel].begin(), |
| 75 | block[band][channel].begin() + samples_to_frame, |
| 76 | (*sub_frame)[band][channel].begin() + buffer_[band][channel].size()); |
| 77 | buffer_[band][channel].clear(); |
| 78 | buffer_[band][channel].insert( |
| 79 | buffer_[band][channel].begin(), |
| 80 | block[band][channel].begin() + samples_to_frame, |
| 81 | block[band][channel].end()); |
| 82 | } |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | } // namespace webrtc |