OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 21 matching lines...) Expand all Loading... |
32 // The pointer arrays for the same example are as follows: | 32 // The pointer arrays for the same example are as follows: |
33 // | 33 // |
34 // |channels_|: | 34 // |channels_|: |
35 // { [ b1ch1* ] [ b1ch2* ] [ b2ch1* ] [ b2ch2* ] } | 35 // { [ b1ch1* ] [ b1ch2* ] [ b2ch1* ] [ b2ch2* ] } |
36 // | 36 // |
37 // |bands_|: | 37 // |bands_|: |
38 // { [ b1ch1* ] [ b2ch1* ] [ b1ch2* ] [ b2ch2* ] } | 38 // { [ b1ch1* ] [ b2ch1* ] [ b1ch2* ] [ b2ch2* ] } |
39 template <typename T> | 39 template <typename T> |
40 class ChannelBuffer { | 40 class ChannelBuffer { |
41 public: | 41 public: |
42 ChannelBuffer(int num_frames, | 42 ChannelBuffer(size_t num_frames, |
43 int num_channels, | 43 int num_channels, |
44 int num_bands = 1) | 44 size_t num_bands = 1) |
45 : data_(new T[num_frames * num_channels]()), | 45 : data_(new T[num_frames * num_channels]()), |
46 channels_(new T*[num_channels * num_bands]), | 46 channels_(new T*[num_channels * num_bands]), |
47 bands_(new T*[num_channels * num_bands]), | 47 bands_(new T*[num_channels * num_bands]), |
48 num_frames_(num_frames), | 48 num_frames_(num_frames), |
49 num_frames_per_band_(num_frames / num_bands), | 49 num_frames_per_band_(num_frames / num_bands), |
50 num_channels_(num_channels), | 50 num_channels_(num_channels), |
51 num_bands_(num_bands) { | 51 num_bands_(num_bands) { |
52 for (int i = 0; i < num_channels_; ++i) { | 52 for (int i = 0; i < num_channels_; ++i) { |
53 for (int j = 0; j < num_bands_; ++j) { | 53 for (size_t j = 0; j < num_bands_; ++j) { |
54 channels_[j * num_channels_ + i] = | 54 channels_[j * num_channels_ + i] = |
55 &data_[i * num_frames_ + j * num_frames_per_band_]; | 55 &data_[i * num_frames_ + j * num_frames_per_band_]; |
56 bands_[i * num_bands_ + j] = channels_[j * num_channels_ + i]; | 56 bands_[i * num_bands_ + j] = channels_[j * num_channels_ + i]; |
57 } | 57 } |
58 } | 58 } |
59 } | 59 } |
60 | 60 |
61 // Returns a pointer array to the full-band channels (or lower band channels). | 61 // Returns a pointer array to the full-band channels (or lower band channels). |
62 // Usage: | 62 // Usage: |
63 // channels()[channel][sample]. | 63 // channels()[channel][sample]. |
64 // Where: | 64 // Where: |
65 // 0 <= channel < |num_channels_| | 65 // 0 <= channel < |num_channels_| |
66 // 0 <= sample < |num_frames_| | 66 // 0 <= sample < |num_frames_| |
67 T* const* channels() { return channels(0); } | 67 T* const* channels() { return channels(0); } |
68 const T* const* channels() const { return channels(0); } | 68 const T* const* channels() const { return channels(0); } |
69 | 69 |
70 // Returns a pointer array to the channels for a specific band. | 70 // Returns a pointer array to the channels for a specific band. |
71 // Usage: | 71 // Usage: |
72 // channels(band)[channel][sample]. | 72 // channels(band)[channel][sample]. |
73 // Where: | 73 // Where: |
74 // 0 <= band < |num_bands_| | 74 // 0 <= band < |num_bands_| |
75 // 0 <= channel < |num_channels_| | 75 // 0 <= channel < |num_channels_| |
76 // 0 <= sample < |num_frames_per_band_| | 76 // 0 <= sample < |num_frames_per_band_| |
77 const T* const* channels(int band) const { | 77 const T* const* channels(size_t band) const { |
78 DCHECK_LT(band, num_bands_); | 78 DCHECK_LT(band, num_bands_); |
79 DCHECK_GE(band, 0); | |
80 return &channels_[band * num_channels_]; | 79 return &channels_[band * num_channels_]; |
81 } | 80 } |
82 T* const* channels(int band) { | 81 T* const* channels(size_t band) { |
83 const ChannelBuffer<T>* t = this; | 82 const ChannelBuffer<T>* t = this; |
84 return const_cast<T* const*>(t->channels(band)); | 83 return const_cast<T* const*>(t->channels(band)); |
85 } | 84 } |
86 | 85 |
87 // Returns a pointer array to the bands for a specific channel. | 86 // Returns a pointer array to the bands for a specific channel. |
88 // Usage: | 87 // Usage: |
89 // bands(channel)[band][sample]. | 88 // bands(channel)[band][sample]. |
90 // Where: | 89 // Where: |
91 // 0 <= channel < |num_channels_| | 90 // 0 <= channel < |num_channels_| |
92 // 0 <= band < |num_bands_| | 91 // 0 <= band < |num_bands_| |
93 // 0 <= sample < |num_frames_per_band_| | 92 // 0 <= sample < |num_frames_per_band_| |
94 const T* const* bands(int channel) const { | 93 const T* const* bands(int channel) const { |
95 DCHECK_LT(channel, num_channels_); | 94 DCHECK_LT(channel, num_channels_); |
96 DCHECK_GE(channel, 0); | 95 DCHECK_GE(channel, 0); |
97 return &bands_[channel * num_bands_]; | 96 return &bands_[channel * num_bands_]; |
98 } | 97 } |
99 T* const* bands(int channel) { | 98 T* const* bands(int channel) { |
100 const ChannelBuffer<T>* t = this; | 99 const ChannelBuffer<T>* t = this; |
101 return const_cast<T* const*>(t->bands(channel)); | 100 return const_cast<T* const*>(t->bands(channel)); |
102 } | 101 } |
103 | 102 |
104 // Sets the |slice| pointers to the |start_frame| position for each channel. | 103 // Sets the |slice| pointers to the |start_frame| position for each channel. |
105 // Returns |slice| for convenience. | 104 // Returns |slice| for convenience. |
106 const T* const* Slice(T** slice, int start_frame) const { | 105 const T* const* Slice(T** slice, size_t start_frame) const { |
107 DCHECK_LT(start_frame, num_frames_); | 106 DCHECK_LT(start_frame, num_frames_); |
108 for (int i = 0; i < num_channels_; ++i) | 107 for (int i = 0; i < num_channels_; ++i) |
109 slice[i] = &channels_[i][start_frame]; | 108 slice[i] = &channels_[i][start_frame]; |
110 return slice; | 109 return slice; |
111 } | 110 } |
112 T** Slice(T** slice, int start_frame) { | 111 T** Slice(T** slice, size_t start_frame) { |
113 const ChannelBuffer<T>* t = this; | 112 const ChannelBuffer<T>* t = this; |
114 return const_cast<T**>(t->Slice(slice, start_frame)); | 113 return const_cast<T**>(t->Slice(slice, start_frame)); |
115 } | 114 } |
116 | 115 |
117 int num_frames() const { return num_frames_; } | 116 size_t num_frames() const { return num_frames_; } |
118 int num_frames_per_band() const { return num_frames_per_band_; } | 117 size_t num_frames_per_band() const { return num_frames_per_band_; } |
119 int num_channels() const { return num_channels_; } | 118 int num_channels() const { return num_channels_; } |
120 int num_bands() const { return num_bands_; } | 119 size_t num_bands() const { return num_bands_; } |
121 size_t size() const {return num_frames_ * num_channels_; } | 120 size_t size() const {return num_frames_ * num_channels_; } |
122 | 121 |
123 void SetDataForTesting(const T* data, size_t size) { | 122 void SetDataForTesting(const T* data, size_t size) { |
124 CHECK_EQ(size, this->size()); | 123 CHECK_EQ(size, this->size()); |
125 memcpy(data_.get(), data, size * sizeof(*data)); | 124 memcpy(data_.get(), data, size * sizeof(*data)); |
126 } | 125 } |
127 | 126 |
128 private: | 127 private: |
129 rtc::scoped_ptr<T[]> data_; | 128 rtc::scoped_ptr<T[]> data_; |
130 rtc::scoped_ptr<T* []> channels_; | 129 rtc::scoped_ptr<T* []> channels_; |
131 rtc::scoped_ptr<T* []> bands_; | 130 rtc::scoped_ptr<T* []> bands_; |
132 const int num_frames_; | 131 const size_t num_frames_; |
133 const int num_frames_per_band_; | 132 const size_t num_frames_per_band_; |
134 const int num_channels_; | 133 const int num_channels_; |
135 const int num_bands_; | 134 const size_t num_bands_; |
136 }; | 135 }; |
137 | 136 |
138 // One int16_t and one float ChannelBuffer that are kept in sync. The sync is | 137 // One int16_t and one float ChannelBuffer that are kept in sync. The sync is |
139 // broken when someone requests write access to either ChannelBuffer, and | 138 // broken when someone requests write access to either ChannelBuffer, and |
140 // reestablished when someone requests the outdated ChannelBuffer. It is | 139 // reestablished when someone requests the outdated ChannelBuffer. It is |
141 // therefore safe to use the return value of ibuf_const() and fbuf_const() | 140 // therefore safe to use the return value of ibuf_const() and fbuf_const() |
142 // until the next call to ibuf() or fbuf(), and the return value of ibuf() and | 141 // until the next call to ibuf() or fbuf(), and the return value of ibuf() and |
143 // fbuf() until the next call to any of the other functions. | 142 // fbuf() until the next call to any of the other functions. |
144 class IFChannelBuffer { | 143 class IFChannelBuffer { |
145 public: | 144 public: |
146 IFChannelBuffer(int num_frames, int num_channels, int num_bands = 1); | 145 IFChannelBuffer(size_t num_frames, int num_channels, size_t num_bands = 1); |
147 | 146 |
148 ChannelBuffer<int16_t>* ibuf(); | 147 ChannelBuffer<int16_t>* ibuf(); |
149 ChannelBuffer<float>* fbuf(); | 148 ChannelBuffer<float>* fbuf(); |
150 const ChannelBuffer<int16_t>* ibuf_const() const; | 149 const ChannelBuffer<int16_t>* ibuf_const() const; |
151 const ChannelBuffer<float>* fbuf_const() const; | 150 const ChannelBuffer<float>* fbuf_const() const; |
152 | 151 |
153 int num_frames() const { return ibuf_.num_frames(); } | 152 size_t num_frames() const { return ibuf_.num_frames(); } |
154 int num_frames_per_band() const { return ibuf_.num_frames_per_band(); } | 153 size_t num_frames_per_band() const { return ibuf_.num_frames_per_band(); } |
155 int num_channels() const { return ibuf_.num_channels(); } | 154 int num_channels() const { return ibuf_.num_channels(); } |
156 int num_bands() const { return ibuf_.num_bands(); } | 155 size_t num_bands() const { return ibuf_.num_bands(); } |
157 | 156 |
158 private: | 157 private: |
159 void RefreshF() const; | 158 void RefreshF() const; |
160 void RefreshI() const; | 159 void RefreshI() const; |
161 | 160 |
162 mutable bool ivalid_; | 161 mutable bool ivalid_; |
163 mutable ChannelBuffer<int16_t> ibuf_; | 162 mutable ChannelBuffer<int16_t> ibuf_; |
164 mutable bool fvalid_; | 163 mutable bool fvalid_; |
165 mutable ChannelBuffer<float> fbuf_; | 164 mutable ChannelBuffer<float> fbuf_; |
166 }; | 165 }; |
167 | 166 |
168 } // namespace webrtc | 167 } // namespace webrtc |
169 | 168 |
170 #endif // WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_ | 169 #endif // WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_ |
OLD | NEW |