aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 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 WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_ |
| 13 | |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 14 | #include <string.h> |
| 15 | |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame^] | 16 | #include <memory> |
| 17 | |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 18 | #include "webrtc/base/checks.h" |
kjellander | 988d31e | 2016-02-05 00:23:50 -0800 | [diff] [blame] | 19 | #include "webrtc/base/gtest_prod_util.h" |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 20 | #include "webrtc/common_audio/include/audio_util.h" |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 24 | // Helper to encapsulate a contiguous data buffer, full or split into frequency |
| 25 | // bands, with access to a pointer arrays of the deinterleaved channels and |
| 26 | // bands. The buffer is zero initialized at creation. |
| 27 | // |
| 28 | // The buffer structure is showed below for a 2 channel and 2 bands case: |
| 29 | // |
| 30 | // |data_|: |
| 31 | // { [ --- b1ch1 --- ] [ --- b2ch1 --- ] [ --- b1ch2 --- ] [ --- b2ch2 --- ] } |
| 32 | // |
| 33 | // The pointer arrays for the same example are as follows: |
| 34 | // |
| 35 | // |channels_|: |
| 36 | // { [ b1ch1* ] [ b1ch2* ] [ b2ch1* ] [ b2ch2* ] } |
| 37 | // |
| 38 | // |bands_|: |
| 39 | // { [ b1ch1* ] [ b2ch1* ] [ b1ch2* ] [ b2ch2* ] } |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 40 | template <typename T> |
| 41 | class ChannelBuffer { |
| 42 | public: |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 43 | ChannelBuffer(size_t num_frames, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 44 | size_t num_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 45 | size_t num_bands = 1) |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 46 | : data_(new T[num_frames * num_channels]()), |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 47 | channels_(new T*[num_channels * num_bands]), |
| 48 | bands_(new T*[num_channels * num_bands]), |
| 49 | num_frames_(num_frames), |
| 50 | num_frames_per_band_(num_frames / num_bands), |
| 51 | num_channels_(num_channels), |
| 52 | num_bands_(num_bands) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 53 | for (size_t i = 0; i < num_channels_; ++i) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 54 | for (size_t j = 0; j < num_bands_; ++j) { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 55 | channels_[j * num_channels_ + i] = |
| 56 | &data_[i * num_frames_ + j * num_frames_per_band_]; |
| 57 | bands_[i * num_bands_ + j] = channels_[j * num_channels_ + i]; |
| 58 | } |
| 59 | } |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 60 | } |
| 61 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 62 | // Returns a pointer array to the full-band channels (or lower band channels). |
| 63 | // Usage: |
| 64 | // channels()[channel][sample]. |
| 65 | // Where: |
| 66 | // 0 <= channel < |num_channels_| |
| 67 | // 0 <= sample < |num_frames_| |
| 68 | T* const* channels() { return channels(0); } |
| 69 | const T* const* channels() const { return channels(0); } |
| 70 | |
| 71 | // Returns a pointer array to the channels for a specific band. |
| 72 | // Usage: |
| 73 | // channels(band)[channel][sample]. |
| 74 | // Where: |
| 75 | // 0 <= band < |num_bands_| |
| 76 | // 0 <= channel < |num_channels_| |
| 77 | // 0 <= sample < |num_frames_per_band_| |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 78 | const T* const* channels(size_t band) const { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 79 | RTC_DCHECK_LT(band, num_bands_); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 80 | return &channels_[band * num_channels_]; |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 81 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 82 | T* const* channels(size_t band) { |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 83 | const ChannelBuffer<T>* t = this; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 84 | return const_cast<T* const*>(t->channels(band)); |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 85 | } |
| 86 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 87 | // Returns a pointer array to the bands for a specific channel. |
| 88 | // Usage: |
| 89 | // bands(channel)[band][sample]. |
| 90 | // Where: |
| 91 | // 0 <= channel < |num_channels_| |
| 92 | // 0 <= band < |num_bands_| |
| 93 | // 0 <= sample < |num_frames_per_band_| |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 94 | const T* const* bands(size_t channel) const { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 95 | RTC_DCHECK_LT(channel, num_channels_); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 96 | RTC_DCHECK_GE(channel, 0u); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 97 | return &bands_[channel * num_bands_]; |
| 98 | } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 99 | T* const* bands(size_t channel) { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 100 | const ChannelBuffer<T>* t = this; |
| 101 | return const_cast<T* const*>(t->bands(channel)); |
| 102 | } |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 103 | |
andrew@webrtc.org | 041035b | 2015-01-26 21:23:53 +0000 | [diff] [blame] | 104 | // Sets the |slice| pointers to the |start_frame| position for each channel. |
| 105 | // Returns |slice| for convenience. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 106 | const T* const* Slice(T** slice, size_t start_frame) const { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 107 | RTC_DCHECK_LT(start_frame, num_frames_); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 108 | for (size_t i = 0; i < num_channels_; ++i) |
andrew@webrtc.org | 041035b | 2015-01-26 21:23:53 +0000 | [diff] [blame] | 109 | slice[i] = &channels_[i][start_frame]; |
| 110 | return slice; |
| 111 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 112 | T** Slice(T** slice, size_t start_frame) { |
andrew@webrtc.org | 041035b | 2015-01-26 21:23:53 +0000 | [diff] [blame] | 113 | const ChannelBuffer<T>* t = this; |
| 114 | return const_cast<T**>(t->Slice(slice, start_frame)); |
| 115 | } |
| 116 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 117 | size_t num_frames() const { return num_frames_; } |
| 118 | size_t num_frames_per_band() const { return num_frames_per_band_; } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 119 | size_t num_channels() const { return num_channels_; } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 120 | size_t num_bands() const { return num_bands_; } |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 121 | size_t size() const {return num_frames_ * num_channels_; } |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 122 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 123 | void SetDataForTesting(const T* data, size_t size) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 124 | RTC_CHECK_EQ(size, this->size()); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 125 | memcpy(data_.get(), data, size * sizeof(*data)); |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 126 | } |
| 127 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 128 | private: |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame^] | 129 | std::unique_ptr<T[]> data_; |
| 130 | std::unique_ptr<T* []> channels_; |
| 131 | std::unique_ptr<T* []> bands_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 132 | const size_t num_frames_; |
| 133 | const size_t num_frames_per_band_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 134 | const size_t num_channels_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 135 | const size_t num_bands_; |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 138 | // 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 |
| 140 | // reestablished when someone requests the outdated ChannelBuffer. It is |
| 141 | // 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 |
| 143 | // fbuf() until the next call to any of the other functions. |
| 144 | class IFChannelBuffer { |
| 145 | public: |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 146 | IFChannelBuffer(size_t num_frames, size_t num_channels, size_t num_bands = 1); |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 147 | |
| 148 | ChannelBuffer<int16_t>* ibuf(); |
| 149 | ChannelBuffer<float>* fbuf(); |
| 150 | const ChannelBuffer<int16_t>* ibuf_const() const; |
| 151 | const ChannelBuffer<float>* fbuf_const() const; |
| 152 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 153 | size_t num_frames() const { return ibuf_.num_frames(); } |
| 154 | size_t num_frames_per_band() const { return ibuf_.num_frames_per_band(); } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 155 | size_t num_channels() const { return ibuf_.num_channels(); } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 156 | size_t num_bands() const { return ibuf_.num_bands(); } |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 157 | |
| 158 | private: |
| 159 | void RefreshF() const; |
| 160 | void RefreshI() const; |
| 161 | |
| 162 | mutable bool ivalid_; |
| 163 | mutable ChannelBuffer<int16_t> ibuf_; |
| 164 | mutable bool fvalid_; |
| 165 | mutable ChannelBuffer<float> fbuf_; |
| 166 | }; |
| 167 | |
| 168 | } // namespace webrtc |
| 169 | |
| 170 | #endif // WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_ |