Line data Source code
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 :
11 : #include "webrtc/modules/audio_processing/splitting_filter.h"
12 :
13 : #include "webrtc/base/checks.h"
14 : #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
15 : #include "webrtc/common_audio/channel_buffer.h"
16 :
17 : namespace webrtc {
18 :
19 0 : SplittingFilter::SplittingFilter(size_t num_channels,
20 : size_t num_bands,
21 0 : size_t num_frames)
22 0 : : num_bands_(num_bands) {
23 0 : RTC_CHECK(num_bands_ == 2 || num_bands_ == 3);
24 0 : if (num_bands_ == 2) {
25 0 : two_bands_states_.resize(num_channels);
26 0 : } else if (num_bands_ == 3) {
27 0 : for (size_t i = 0; i < num_channels; ++i) {
28 0 : three_band_filter_banks_.push_back(std::unique_ptr<ThreeBandFilterBank>(
29 0 : new ThreeBandFilterBank(num_frames)));
30 : }
31 : }
32 0 : }
33 :
34 : SplittingFilter::~SplittingFilter() = default;
35 :
36 0 : void SplittingFilter::Analysis(const IFChannelBuffer* data,
37 : IFChannelBuffer* bands) {
38 0 : RTC_DCHECK_EQ(num_bands_, bands->num_bands());
39 0 : RTC_DCHECK_EQ(data->num_channels(), bands->num_channels());
40 0 : RTC_DCHECK_EQ(data->num_frames(),
41 0 : bands->num_frames_per_band() * bands->num_bands());
42 0 : if (bands->num_bands() == 2) {
43 0 : TwoBandsAnalysis(data, bands);
44 0 : } else if (bands->num_bands() == 3) {
45 0 : ThreeBandsAnalysis(data, bands);
46 : }
47 0 : }
48 :
49 0 : void SplittingFilter::Synthesis(const IFChannelBuffer* bands,
50 : IFChannelBuffer* data) {
51 0 : RTC_DCHECK_EQ(num_bands_, bands->num_bands());
52 0 : RTC_DCHECK_EQ(data->num_channels(), bands->num_channels());
53 0 : RTC_DCHECK_EQ(data->num_frames(),
54 0 : bands->num_frames_per_band() * bands->num_bands());
55 0 : if (bands->num_bands() == 2) {
56 0 : TwoBandsSynthesis(bands, data);
57 0 : } else if (bands->num_bands() == 3) {
58 0 : ThreeBandsSynthesis(bands, data);
59 : }
60 0 : }
61 :
62 0 : void SplittingFilter::TwoBandsAnalysis(const IFChannelBuffer* data,
63 : IFChannelBuffer* bands) {
64 0 : RTC_DCHECK_EQ(two_bands_states_.size(), data->num_channels());
65 0 : for (size_t i = 0; i < two_bands_states_.size(); ++i) {
66 0 : WebRtcSpl_AnalysisQMF(data->ibuf_const()->channels()[i],
67 : data->num_frames(),
68 0 : bands->ibuf()->channels(0)[i],
69 0 : bands->ibuf()->channels(1)[i],
70 0 : two_bands_states_[i].analysis_state1,
71 0 : two_bands_states_[i].analysis_state2);
72 : }
73 0 : }
74 :
75 0 : void SplittingFilter::TwoBandsSynthesis(const IFChannelBuffer* bands,
76 : IFChannelBuffer* data) {
77 0 : RTC_DCHECK_LE(data->num_channels(), two_bands_states_.size());
78 0 : for (size_t i = 0; i < data->num_channels(); ++i) {
79 0 : WebRtcSpl_SynthesisQMF(bands->ibuf_const()->channels(0)[i],
80 0 : bands->ibuf_const()->channels(1)[i],
81 : bands->num_frames_per_band(),
82 0 : data->ibuf()->channels()[i],
83 0 : two_bands_states_[i].synthesis_state1,
84 0 : two_bands_states_[i].synthesis_state2);
85 : }
86 0 : }
87 :
88 0 : void SplittingFilter::ThreeBandsAnalysis(const IFChannelBuffer* data,
89 : IFChannelBuffer* bands) {
90 0 : RTC_DCHECK_EQ(three_band_filter_banks_.size(), data->num_channels());
91 0 : for (size_t i = 0; i < three_band_filter_banks_.size(); ++i) {
92 0 : three_band_filter_banks_[i]->Analysis(data->fbuf_const()->channels()[i],
93 : data->num_frames(),
94 0 : bands->fbuf()->bands(i));
95 : }
96 0 : }
97 :
98 0 : void SplittingFilter::ThreeBandsSynthesis(const IFChannelBuffer* bands,
99 : IFChannelBuffer* data) {
100 0 : RTC_DCHECK_LE(data->num_channels(), three_band_filter_banks_.size());
101 0 : for (size_t i = 0; i < data->num_channels(); ++i) {
102 0 : three_band_filter_banks_[i]->Synthesis(bands->fbuf_const()->bands(i),
103 : bands->num_frames_per_band(),
104 0 : data->fbuf()->channels()[i]);
105 : }
106 0 : }
107 :
108 : } // namespace webrtc
|