channel_buffer.h (9059B)
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 #ifndef COMMON_AUDIO_CHANNEL_BUFFER_H_ 12 #define COMMON_AUDIO_CHANNEL_BUFFER_H_ 13 14 #include <cstdint> 15 #include <cstring> 16 #include <memory> 17 #include <vector> 18 19 #include "api/array_view.h" 20 #include "api/audio/audio_view.h" 21 #include "rtc_base/checks.h" 22 23 namespace webrtc { 24 25 // TODO: b/335805780 - Remove this method. Instead, use Deinterleave() from 26 // audio_util.h which requires size checked buffer views. 27 template <typename T> 28 void Deinterleave(const T* interleaved, 29 size_t samples_per_channel, 30 size_t num_channels, 31 T* const* deinterleaved) { 32 for (size_t i = 0; i < num_channels; ++i) { 33 T* channel = deinterleaved[i]; 34 size_t interleaved_idx = i; 35 for (size_t j = 0; j < samples_per_channel; ++j) { 36 channel[j] = interleaved[interleaved_idx]; 37 interleaved_idx += num_channels; 38 } 39 } 40 } 41 42 // `Interleave()` variant for cases where the deinterleaved channels aren't 43 // represented by a `DeinterleavedView`. 44 // TODO: b/335805780 - Remove this method. Instead, use Deinterleave() from 45 // audio_util.h which requires size checked buffer views. 46 template <typename T> 47 void Interleave(const T* const* deinterleaved, 48 size_t samples_per_channel, 49 size_t num_channels, 50 InterleavedView<T>& interleaved) { 51 RTC_DCHECK_EQ(NumChannels(interleaved), num_channels); 52 RTC_DCHECK_EQ(SamplesPerChannel(interleaved), samples_per_channel); 53 for (size_t i = 0; i < num_channels; ++i) { 54 const T* channel = deinterleaved[i]; 55 size_t interleaved_idx = i; 56 for (size_t j = 0; j < samples_per_channel; ++j) { 57 interleaved[interleaved_idx] = channel[j]; 58 interleaved_idx += num_channels; 59 } 60 } 61 } 62 63 // Helper to encapsulate a contiguous data buffer, full or split into frequency 64 // bands, with access to a pointer arrays of the deinterleaved channels and 65 // bands. The buffer is zero initialized at creation. 66 // 67 // The buffer structure is showed below for a 2 channel and 2 bands case: 68 // 69 // `data_`: 70 // { [ --- b1ch1 --- ] [ --- b2ch1 --- ] [ --- b1ch2 --- ] [ --- b2ch2 --- ] } 71 // 72 // The pointer arrays for the same example are as follows: 73 // 74 // `channels_`: 75 // { [ b1ch1* ] [ b1ch2* ] [ b2ch1* ] [ b2ch2* ] } 76 // 77 // `bands_`: 78 // { [ b1ch1* ] [ b2ch1* ] [ b1ch2* ] [ b2ch2* ] } 79 template <typename T> 80 class ChannelBuffer { 81 public: 82 ChannelBuffer(size_t num_frames, size_t num_channels, size_t num_bands = 1) 83 : data_(new T[num_frames * num_channels]()), 84 channels_(new T*[num_channels * num_bands]), 85 bands_(new T*[num_channels * num_bands]), 86 num_frames_(num_frames), 87 num_frames_per_band_(num_frames / num_bands), 88 num_allocated_channels_(num_channels), 89 num_channels_(num_channels), 90 num_bands_(num_bands), 91 bands_view_(num_allocated_channels_, 92 std::vector<ArrayView<T>>(num_bands_)), 93 channels_view_(num_bands_, 94 std::vector<ArrayView<T>>(num_allocated_channels_)) { 95 // Temporarily cast away const_ness to allow populating the array views. 96 auto* bands_view = 97 const_cast<std::vector<std::vector<ArrayView<T>>>*>(&bands_view_); 98 auto* channels_view = 99 const_cast<std::vector<std::vector<ArrayView<T>>>*>(&channels_view_); 100 101 for (size_t ch = 0; ch < num_allocated_channels_; ++ch) { 102 for (size_t band = 0; band < num_bands_; ++band) { 103 (*channels_view)[band][ch] = 104 ArrayView<T>(&data_[ch * num_frames_ + band * num_frames_per_band_], 105 num_frames_per_band_); 106 (*bands_view)[ch][band] = channels_view_[band][ch]; 107 channels_[band * num_allocated_channels_ + ch] = 108 channels_view_[band][ch].data(); 109 bands_[ch * num_bands_ + band] = 110 channels_[band * num_allocated_channels_ + ch]; 111 } 112 } 113 } 114 115 // Returns a pointer array to the channels. 116 // If band is explicitly specificed, the channels for a specific band are 117 // returned and the usage becomes: channels(band)[channel][sample]. 118 // Where: 119 // 0 <= band < `num_bands_` 120 // 0 <= channel < `num_allocated_channels_` 121 // 0 <= sample < `num_frames_per_band_` 122 123 // If band is not explicitly specified, the full-band channels (or lower band 124 // channels) are returned and the usage becomes: channels()[channel][sample]. 125 // Where: 126 // 0 <= channel < `num_allocated_channels_` 127 // 0 <= sample < `num_frames_` 128 const T* const* channels(size_t band = 0) const { 129 RTC_DCHECK_LT(band, num_bands_); 130 return &channels_[band * num_allocated_channels_]; 131 } 132 T* const* channels(size_t band = 0) { 133 const ChannelBuffer<T>* t = this; 134 return const_cast<T* const*>(t->channels(band)); 135 } 136 ArrayView<const ArrayView<T>> channels_view(size_t band = 0) { 137 return channels_view_[band]; 138 } 139 ArrayView<const ArrayView<T>> channels_view(size_t band = 0) const { 140 return channels_view_[band]; 141 } 142 143 // Returns a pointer array to the bands for a specific channel. 144 // Usage: 145 // bands(channel)[band][sample]. 146 // Where: 147 // 0 <= channel < `num_channels_` 148 // 0 <= band < `num_bands_` 149 // 0 <= sample < `num_frames_per_band_` 150 const T* const* bands(size_t channel) const { 151 RTC_DCHECK_LT(channel, num_channels_); 152 RTC_DCHECK_GE(channel, 0); 153 return &bands_[channel * num_bands_]; 154 } 155 T* const* bands(size_t channel) { 156 const ChannelBuffer<T>* t = this; 157 return const_cast<T* const*>(t->bands(channel)); 158 } 159 160 ArrayView<const ArrayView<T>> bands_view(size_t channel) { 161 return bands_view_[channel]; 162 } 163 ArrayView<const ArrayView<T>> bands_view(size_t channel) const { 164 return bands_view_[channel]; 165 } 166 167 // Sets the `slice` pointers to the `start_frame` position for each channel. 168 // Returns `slice` for convenience. 169 const T* const* Slice(T** slice, size_t start_frame) const { 170 RTC_DCHECK_LT(start_frame, num_frames_); 171 for (size_t i = 0; i < num_channels_; ++i) 172 slice[i] = &channels_[i][start_frame]; 173 return slice; 174 } 175 T** Slice(T** slice, size_t start_frame) { 176 const ChannelBuffer<T>* t = this; 177 return const_cast<T**>(t->Slice(slice, start_frame)); 178 } 179 180 size_t num_frames() const { return num_frames_; } 181 size_t num_frames_per_band() const { return num_frames_per_band_; } 182 size_t num_channels() const { return num_channels_; } 183 size_t num_bands() const { return num_bands_; } 184 size_t size() const { return num_frames_ * num_allocated_channels_; } 185 186 void set_num_channels(size_t num_channels) { 187 RTC_DCHECK_LE(num_channels, num_allocated_channels_); 188 num_channels_ = num_channels; 189 } 190 191 void SetDataForTesting(const T* data, size_t size) { 192 RTC_CHECK_EQ(size, this->size()); 193 memcpy(data_.get(), data, size * sizeof(*data)); 194 } 195 196 private: 197 std::unique_ptr<T[]> data_; 198 std::unique_ptr<T*[]> channels_; 199 std::unique_ptr<T*[]> bands_; 200 const size_t num_frames_; 201 const size_t num_frames_per_band_; 202 // Number of channels the internal buffer holds. 203 const size_t num_allocated_channels_; 204 // Number of channels the user sees. 205 size_t num_channels_; 206 const size_t num_bands_; 207 const std::vector<std::vector<ArrayView<T>>> bands_view_; 208 const std::vector<std::vector<ArrayView<T>>> channels_view_; 209 }; 210 211 // One int16_t and one float ChannelBuffer that are kept in sync. The sync is 212 // broken when someone requests write access to either ChannelBuffer, and 213 // reestablished when someone requests the outdated ChannelBuffer. It is 214 // therefore safe to use the return value of ibuf_const() and fbuf_const() 215 // until the next call to ibuf() or fbuf(), and the return value of ibuf() and 216 // fbuf() until the next call to any of the other functions. 217 class IFChannelBuffer { 218 public: 219 IFChannelBuffer(size_t num_frames, size_t num_channels, size_t num_bands = 1); 220 ~IFChannelBuffer(); 221 222 ChannelBuffer<int16_t>* ibuf(); 223 ChannelBuffer<float>* fbuf(); 224 const ChannelBuffer<int16_t>* ibuf_const() const; 225 const ChannelBuffer<float>* fbuf_const() const; 226 227 size_t num_frames() const { return ibuf_.num_frames(); } 228 size_t num_frames_per_band() const { return ibuf_.num_frames_per_band(); } 229 size_t num_channels() const { 230 return ivalid_ ? ibuf_.num_channels() : fbuf_.num_channels(); 231 } 232 void set_num_channels(size_t num_channels) { 233 ibuf_.set_num_channels(num_channels); 234 fbuf_.set_num_channels(num_channels); 235 } 236 size_t num_bands() const { return ibuf_.num_bands(); } 237 238 private: 239 void RefreshF() const; 240 void RefreshI() const; 241 242 mutable bool ivalid_; 243 mutable ChannelBuffer<int16_t> ibuf_; 244 mutable bool fvalid_; 245 mutable ChannelBuffer<float> fbuf_; 246 }; 247 248 } // namespace webrtc 249 250 #endif // COMMON_AUDIO_CHANNEL_BUFFER_H_