blob: 8241ce64f2ccc03f48b74d9a596f789bf508ac6c [file] [log] [blame]
peahd0263542017-01-03 04:20:34 -08001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/aec3/block_framer.h"
peahd0263542017-01-03 04:20:34 -080012
13#include <algorithm>
14
Yves Gerey988cc082018-10-23 12:03:01 +020015#include "modules/audio_processing/aec3/aec3_common.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
peahd0263542017-01-03 04:20:34 -080017
18namespace webrtc {
19
Per Åhgrence202a02019-09-02 17:01:19 +020020BlockFramer::BlockFramer(size_t num_bands, size_t num_channels)
peahd0263542017-01-03 04:20:34 -080021 : num_bands_(num_bands),
Per Åhgrence202a02019-09-02 17:01:19 +020022 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}
peahd0263542017-01-03 04:20:34 -080030
31BlockFramer::~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 Åhgrence202a02019-09-02 17:01:19 +020037void BlockFramer::InsertBlock(
38 const std::vector<std::vector<std::vector<float>>>& block) {
peahd0263542017-01-03 04:20:34 -080039 RTC_DCHECK_EQ(num_bands_, block.size());
Per Åhgrence202a02019-09-02 17:01:19 +020040 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 }
peahd0263542017-01-03 04:20:34 -080050 }
51}
52
53void BlockFramer::InsertBlockAndExtractSubFrame(
Per Åhgrence202a02019-09-02 17:01:19 +020054 const std::vector<std::vector<std::vector<float>>>& block,
55 std::vector<std::vector<rtc::ArrayView<float>>>* sub_frame) {
peahd0263542017-01-03 04:20:34 -080056 RTC_DCHECK(sub_frame);
57 RTC_DCHECK_EQ(num_bands_, block.size());
58 RTC_DCHECK_EQ(num_bands_, sub_frame->size());
Per Åhgrence202a02019-09-02 17:01:19 +020059 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 }
peahd0263542017-01-03 04:20:34 -080083 }
84}
85
86} // namespace webrtc