1 /*
2 * Copyright (c) 2013 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/transient/wpd_tree.h"
12
13 #include <string.h>
14
15 #include "modules/audio_processing/transient/wpd_node.h"
16 #include "rtc_base/checks.h"
17
18 namespace webrtc {
19
WPDTree(size_t data_length,const float * high_pass_coefficients,const float * low_pass_coefficients,size_t coefficients_length,int levels)20 WPDTree::WPDTree(size_t data_length,
21 const float* high_pass_coefficients,
22 const float* low_pass_coefficients,
23 size_t coefficients_length,
24 int levels)
25 : data_length_(data_length),
26 levels_(levels),
27 num_nodes_((1 << (levels + 1)) - 1) {
28 RTC_DCHECK_GT(data_length, (static_cast<size_t>(1) << levels));
29 RTC_DCHECK(high_pass_coefficients);
30 RTC_DCHECK(low_pass_coefficients);
31 RTC_DCHECK_GT(levels, 0);
32 // Size is 1 more, so we can use the array as 1-based. nodes_[0] is never
33 // allocated.
34 nodes_.reset(new std::unique_ptr<WPDNode>[num_nodes_ + 1]);
35
36 // Create the first node
37 const float kRootCoefficient = 1.f; // Identity Coefficient.
38 nodes_[1].reset(new WPDNode(data_length, &kRootCoefficient, 1));
39 // Variables used to create the rest of the nodes.
40 size_t index = 1;
41 size_t index_left_child = 0;
42 size_t index_right_child = 0;
43
44 int num_nodes_at_curr_level = 0;
45
46 // Branching each node in each level to create its children. The last level is
47 // not branched (all the nodes of that level are leaves).
48 for (int current_level = 0; current_level < levels; ++current_level) {
49 num_nodes_at_curr_level = 1 << current_level;
50 for (int i = 0; i < num_nodes_at_curr_level; ++i) {
51 index = (1 << current_level) + i;
52 // Obtain the index of the current node children.
53 index_left_child = index * 2;
54 index_right_child = index_left_child + 1;
55 nodes_[index_left_child].reset(new WPDNode(nodes_[index]->length() / 2,
56 low_pass_coefficients,
57 coefficients_length));
58 nodes_[index_right_child].reset(new WPDNode(nodes_[index]->length() / 2,
59 high_pass_coefficients,
60 coefficients_length));
61 }
62 }
63 }
64
~WPDTree()65 WPDTree::~WPDTree() {}
66
NodeAt(int level,int index)67 WPDNode* WPDTree::NodeAt(int level, int index) {
68 if (level < 0 || level > levels_ || index < 0 || index >= 1 << level) {
69 return NULL;
70 }
71
72 return nodes_[(1 << level) + index].get();
73 }
74
Update(const float * data,size_t data_length)75 int WPDTree::Update(const float* data, size_t data_length) {
76 if (!data || data_length != data_length_) {
77 return -1;
78 }
79
80 // Update the root node.
81 int update_result = nodes_[1]->set_data(data, data_length);
82 if (update_result != 0) {
83 return -1;
84 }
85
86 // Variables used to update the rest of the nodes.
87 size_t index = 1;
88 size_t index_left_child = 0;
89 size_t index_right_child = 0;
90
91 int num_nodes_at_curr_level = 0;
92
93 for (int current_level = 0; current_level < levels_; ++current_level) {
94 num_nodes_at_curr_level = 1 << current_level;
95 for (int i = 0; i < num_nodes_at_curr_level; ++i) {
96 index = (1 << current_level) + i;
97 // Obtain the index of the current node children.
98 index_left_child = index * 2;
99 index_right_child = index_left_child + 1;
100
101 update_result = nodes_[index_left_child]->Update(nodes_[index]->data(),
102 nodes_[index]->length());
103 if (update_result != 0) {
104 return -1;
105 }
106
107 update_result = nodes_[index_right_child]->Update(
108 nodes_[index]->data(), nodes_[index]->length());
109 if (update_result != 0) {
110 return -1;
111 }
112 }
113 }
114
115 return 0;
116 }
117
118 } // namespace webrtc
119