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