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 | |
| 16 | #include "webrtc/base/checks.h" |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame^] | 17 | #include "webrtc/base/scoped_ptr.h" |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 18 | #include "webrtc/common_audio/include/audio_util.h" |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 19 | #include "webrtc/test/testsupport/gtest_prod_util.h" |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 23 | // Helper to encapsulate a contiguous data buffer, full or split into frequency |
| 24 | // bands, with access to a pointer arrays of the deinterleaved channels and |
| 25 | // bands. The buffer is zero initialized at creation. |
| 26 | // |
| 27 | // The buffer structure is showed below for a 2 channel and 2 bands case: |
| 28 | // |
| 29 | // |data_|: |
| 30 | // { [ --- b1ch1 --- ] [ --- b2ch1 --- ] [ --- b1ch2 --- ] [ --- b2ch2 --- ] } |
| 31 | // |
| 32 | // The pointer arrays for the same example are as follows: |
| 33 | // |
| 34 | // |channels_|: |
| 35 | // { [ b1ch1* ] [ b1ch2* ] [ b2ch1* ] [ b2ch2* ] } |
| 36 | // |
| 37 | // |bands_|: |
| 38 | // { [ b1ch1* ] [ b2ch1* ] [ b1ch2* ] [ b2ch2* ] } |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 39 | template <typename T> |
| 40 | class ChannelBuffer { |
| 41 | public: |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 42 | ChannelBuffer(int num_frames, |
| 43 | int num_channels, |
| 44 | int num_bands = 1) |
| 45 | : data_(new T[num_frames * num_channels]), |
| 46 | channels_(new T*[num_channels * num_bands]), |
| 47 | bands_(new T*[num_channels * num_bands]), |
| 48 | num_frames_(num_frames), |
| 49 | num_frames_per_band_(num_frames / num_bands), |
| 50 | num_channels_(num_channels), |
| 51 | num_bands_(num_bands) { |
| 52 | memset(data_.get(), 0, size() * sizeof(T)); |
| 53 | for (int i = 0; i < num_channels_; ++i) { |
| 54 | for (int j = 0; j < num_bands_; ++j) { |
| 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_| |
| 78 | const T* const* channels(int band) const { |
| 79 | DCHECK_LT(band, num_bands_); |
| 80 | DCHECK_GE(band, 0); |
| 81 | return &channels_[band * num_channels_]; |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 82 | } |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 83 | T* const* channels(int band) { |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 84 | const ChannelBuffer<T>* t = this; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 85 | return const_cast<T* const*>(t->channels(band)); |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 86 | } |
| 87 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 88 | // Returns a pointer array to the bands for a specific channel. |
| 89 | // Usage: |
| 90 | // bands(channel)[band][sample]. |
| 91 | // Where: |
| 92 | // 0 <= channel < |num_channels_| |
| 93 | // 0 <= band < |num_bands_| |
| 94 | // 0 <= sample < |num_frames_per_band_| |
| 95 | const T* const* bands(int channel) const { |
| 96 | DCHECK_LT(channel, num_channels_); |
| 97 | DCHECK_GE(channel, 0); |
| 98 | return &bands_[channel * num_bands_]; |
| 99 | } |
| 100 | T* const* bands(int channel) { |
| 101 | const ChannelBuffer<T>* t = this; |
| 102 | return const_cast<T* const*>(t->bands(channel)); |
| 103 | } |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 104 | |
andrew@webrtc.org | 041035b | 2015-01-26 21:23:53 +0000 | [diff] [blame] | 105 | // Sets the |slice| pointers to the |start_frame| position for each channel. |
| 106 | // Returns |slice| for convenience. |
| 107 | const T* const* Slice(T** slice, int start_frame) const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 108 | DCHECK_LT(start_frame, num_frames_); |
andrew@webrtc.org | 041035b | 2015-01-26 21:23:53 +0000 | [diff] [blame] | 109 | for (int i = 0; i < num_channels_; ++i) |
| 110 | slice[i] = &channels_[i][start_frame]; |
| 111 | return slice; |
| 112 | } |
| 113 | T** Slice(T** slice, int start_frame) { |
| 114 | const ChannelBuffer<T>* t = this; |
| 115 | return const_cast<T**>(t->Slice(slice, start_frame)); |
| 116 | } |
| 117 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 118 | int num_frames() const { return num_frames_; } |
| 119 | int num_frames_per_band() const { return num_frames_per_band_; } |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 120 | int num_channels() const { return num_channels_; } |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 121 | int num_bands() const { return num_bands_; } |
| 122 | size_t size() const {return num_frames_ * num_channels_; } |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 123 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 124 | void SetDataForTesting(const T* data, size_t size) { |
| 125 | CHECK_EQ(size, this->size()); |
| 126 | memcpy(data_.get(), data, size * sizeof(*data)); |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 127 | } |
| 128 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 129 | private: |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame^] | 130 | rtc::scoped_ptr<T[]> data_; |
| 131 | rtc::scoped_ptr<T* []> channels_; |
| 132 | rtc::scoped_ptr<T* []> bands_; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 133 | const int num_frames_; |
| 134 | const int num_frames_per_band_; |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 135 | const int num_channels_; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 136 | const int num_bands_; |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 139 | // One int16_t and one float ChannelBuffer that are kept in sync. The sync is |
| 140 | // broken when someone requests write access to either ChannelBuffer, and |
| 141 | // reestablished when someone requests the outdated ChannelBuffer. It is |
| 142 | // therefore safe to use the return value of ibuf_const() and fbuf_const() |
| 143 | // until the next call to ibuf() or fbuf(), and the return value of ibuf() and |
| 144 | // fbuf() until the next call to any of the other functions. |
| 145 | class IFChannelBuffer { |
| 146 | public: |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 147 | IFChannelBuffer(int num_frames, int num_channels, int num_bands = 1); |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 148 | |
| 149 | ChannelBuffer<int16_t>* ibuf(); |
| 150 | ChannelBuffer<float>* fbuf(); |
| 151 | const ChannelBuffer<int16_t>* ibuf_const() const; |
| 152 | const ChannelBuffer<float>* fbuf_const() const; |
| 153 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 154 | int num_frames() const { return ibuf_.num_frames(); } |
| 155 | int num_frames_per_band() const { return ibuf_.num_frames_per_band(); } |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 156 | int num_channels() const { return ibuf_.num_channels(); } |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 157 | int num_bands() const { return ibuf_.num_bands(); } |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 158 | |
| 159 | private: |
| 160 | void RefreshF() const; |
| 161 | void RefreshI() const; |
| 162 | |
| 163 | mutable bool ivalid_; |
| 164 | mutable ChannelBuffer<int16_t> ibuf_; |
| 165 | mutable bool fvalid_; |
| 166 | mutable ChannelBuffer<float> fbuf_; |
| 167 | }; |
| 168 | |
| 169 | } // namespace webrtc |
| 170 | |
| 171 | #endif // WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_ |