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