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
15 #include "modules/audio_processing/aec3/aec3_common.h"
16 #include "rtc_base/checks.h"
17
18 namespace webrtc {
19
BlockFramer(size_t num_bands,size_t num_channels)20 BlockFramer::BlockFramer(size_t num_bands, size_t num_channels)
21 : num_bands_(num_bands),
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 }
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.
InsertBlock(const std::vector<std::vector<std::vector<float>>> & block)37 void BlockFramer::InsertBlock(
38 const std::vector<std::vector<std::vector<float>>>& block) {
39 RTC_DCHECK_EQ(num_bands_, block.size());
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 }
50 }
51 }
52
InsertBlockAndExtractSubFrame(const std::vector<std::vector<std::vector<float>>> & block,std::vector<std::vector<rtc::ArrayView<float>>> * sub_frame)53 void BlockFramer::InsertBlockAndExtractSubFrame(
54 const std::vector<std::vector<std::vector<float>>>& block,
55 std::vector<std::vector<rtc::ArrayView<float>>>* sub_frame) {
56 RTC_DCHECK(sub_frame);
57 RTC_DCHECK_EQ(num_bands_, block.size());
58 RTC_DCHECK_EQ(num_bands_, sub_frame->size());
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 }
83 }
84 }
85
86 } // namespace webrtc
87