forked from google/lyra
-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter_banks.cc
144 lines (130 loc) · 4.85 KB
/
filter_banks.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "filter_banks.h"
#include <cstdint>
#include <memory>
#include <utility>
#include <vector>
#include "absl/memory/memory.h"
#include "absl/types/span.h"
#include "filter_banks_interface.h"
#include "quadrature_mirror_filter.h"
namespace chromemedia {
namespace codec {
namespace {
bool IsPowerOfTwo(int x) { return x && !(x & (x - 1)); }
int IntLogTwo(int x) {
int log_two = 0;
while (x >>= 1) {
++log_two;
}
return log_two;
}
} // namespace
std::unique_ptr<SplitFilter> SplitFilter::Create(int num_bands) {
if (!IsPowerOfTwo(num_bands)) {
std::cerr << "Number of bands has to be a power of 2, but was "
<< num_bands << ".";
return nullptr;
}
return absl::WrapUnique(new SplitFilter(num_bands));
}
SplitFilter::SplitFilter(int num_bands) : num_bands_(num_bands) {
const int num_levels = IntLogTwo(num_bands);
filters_per_level_.reserve(num_levels);
for (int level = 0; level < num_levels; ++level) {
// Number of filters per level goes up as power of 2, starting at 1.
filters_per_level_.push_back(
std::vector<SplitQuadratureMirrorFilter<int16_t>>(1 << level));
}
}
std::vector<std::vector<int16_t>> SplitFilter::Split(
absl::Span<const int16_t> signal) {
if (signal.size() % num_bands_ != 0) {
std::cerr << "The number of samples has to be divisible by " << num_bands_
<< ", but was " << signal.size() << ".";
exit(EXIT_FAILURE);
}
std::vector<std::vector<int16_t>> old_bands;
old_bands.push_back(std::vector<int16_t>(signal.begin(), signal.end()));
for (std::vector<SplitQuadratureMirrorFilter<int16_t>>& filters :
filters_per_level_) {
std::vector<std::vector<int16_t>> new_bands;
for (int filter = 0; filter < filters.size(); ++filter) {
Bands<int16_t> bands = filters.at(filter).Split(old_bands.at(filter));
// Because of the mirroring characteristic of aliasing, odd bands are
// reversed.
if (filter % 2 == 0) {
new_bands.push_back(std::move(bands.low_band));
new_bands.push_back(std::move(bands.high_band));
} else {
new_bands.push_back(std::move(bands.high_band));
new_bands.push_back(std::move(bands.low_band));
}
}
old_bands = new_bands;
}
return old_bands;
}
std::unique_ptr<MergeFilter> MergeFilter::Create(int num_bands) {
if (!IsPowerOfTwo(num_bands)) {
std::cerr << "Number of bands has to be a power of 2, but was "
<< num_bands << ".";
return nullptr;
}
return absl::WrapUnique(new MergeFilter(num_bands));
}
MergeFilter::MergeFilter(int num_bands) : MergeFilterInterface(num_bands) {
const int num_levels = IntLogTwo(num_bands);
filters_per_level_.reserve(num_levels);
for (int level = 0; level < num_levels; ++level) {
// Number of filters per level goes down as power of 2, ending at 1.
filters_per_level_.push_back(
std::vector<MergeQuadratureMirrorFilter<int16_t>>(num_bands >>
(level + 1)));
}
}
std::vector<int16_t> MergeFilter::Merge(
const std::vector<std::vector<int16_t>>& bands) {
if (bands.size() != num_bands_) {
std::cerr << "The number of bands has to be " << num_bands_ << ", but was "
<< bands.size() << ".";
exit(EXIT_FAILURE);
}
const int band_size = bands.at(0).size();
for (int band = 0; band < bands.size(); ++band) {
if (bands.at(band).size() != band_size) {
std::cerr << "The number of samples of all bands has to be the same, but was "
<< band_size << " for the first band and " << bands.at(band).size()
<< " for the band number " << band + 1 << ".";
exit(EXIT_FAILURE);
}
}
std::vector<std::vector<int16_t>> old_bands(bands);
for (std::vector<MergeQuadratureMirrorFilter<int16_t>>& filters :
filters_per_level_) {
std::vector<std::vector<int16_t>> new_bands;
for (int filter = 0; filter < filters.size(); ++filter) {
// Because of the mirroring characteristic of aliasing, odd bands are
// reversed.
const Bands<int16_t> bands(old_bands.at(2 * filter + filter % 2),
old_bands.at(2 * filter + 1 - filter % 2));
new_bands.push_back(filters.at(filter).Merge(bands));
}
old_bands = new_bands;
}
return old_bands.at(0);
}
} // namespace codec
} // namespace chromemedia