blob: dc44369be6495ff8b4a8a267424e9fe6ef91b784 [file] [log] [blame]
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef COMMON_AUDIO_CHANNEL_BUFFER_H_
12#define COMMON_AUDIO_CHANNEL_BUFFER_H_
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000013
aluebs@webrtc.org87893762014-11-27 23:40:25 +000014#include <string.h>
15
kwibergc2b785d2016-02-24 05:22:32 -080016#include <memory>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "common_audio/include/audio_util.h"
19#include "rtc_base/checks.h"
20#include "rtc_base/gtest_prod_util.h"
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000021
22namespace webrtc {
23
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000024// 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.org87893762014-11-27 23:40:25 +000040template <typename T>
41class ChannelBuffer {
42 public:
Yves Gerey665174f2018-06-19 15:03:05 +020043 ChannelBuffer(size_t num_frames, size_t num_channels, size_t num_bands = 1)
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +000044 : data_(new T[num_frames * num_channels]()),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000045 channels_(new T*[num_channels * num_bands]),
46 bands_(new T*[num_channels * num_bands]),
47 num_frames_(num_frames),
48 num_frames_per_band_(num_frames / num_bands),
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070049 num_allocated_channels_(num_channels),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000050 num_channels_(num_channels),
51 num_bands_(num_bands) {
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070052 for (size_t i = 0; i < num_allocated_channels_; ++i) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070053 for (size_t j = 0; j < num_bands_; ++j) {
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070054 channels_[j * num_allocated_channels_ + i] =
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000055 &data_[i * num_frames_ + j * num_frames_per_band_];
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070056 bands_[i * num_bands_ + j] = channels_[j * num_allocated_channels_ + i];
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000057 }
58 }
aluebs@webrtc.org87893762014-11-27 23:40:25 +000059 }
60
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000061 // Returns a pointer array to the full-band channels (or lower band channels).
62 // Usage:
63 // channels()[channel][sample].
64 // Where:
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070065 // 0 <= channel < |num_allocated_channels_|
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000066 // 0 <= sample < |num_frames_|
67 T* const* channels() { return channels(0); }
68 const T* const* channels() const { return channels(0); }
69
70 // Returns a pointer array to the channels for a specific band.
71 // Usage:
72 // channels(band)[channel][sample].
73 // Where:
74 // 0 <= band < |num_bands_|
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070075 // 0 <= channel < |num_allocated_channels_|
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000076 // 0 <= sample < |num_frames_per_band_|
Peter Kastingdce40cf2015-08-24 14:52:23 -070077 const T* const* channels(size_t band) const {
henrikg91d6ede2015-09-17 00:24:34 -070078 RTC_DCHECK_LT(band, num_bands_);
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070079 return &channels_[band * num_allocated_channels_];
aluebs@webrtc.org87893762014-11-27 23:40:25 +000080 }
Peter Kastingdce40cf2015-08-24 14:52:23 -070081 T* const* channels(size_t band) {
aluebs@webrtc.org87893762014-11-27 23:40:25 +000082 const ChannelBuffer<T>* t = this;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000083 return const_cast<T* const*>(t->channels(band));
aluebs@webrtc.org87893762014-11-27 23:40:25 +000084 }
85
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000086 // Returns a pointer array to the bands for a specific channel.
87 // Usage:
88 // bands(channel)[band][sample].
89 // Where:
90 // 0 <= channel < |num_channels_|
91 // 0 <= band < |num_bands_|
92 // 0 <= sample < |num_frames_per_band_|
Peter Kasting69558702016-01-12 16:26:35 -080093 const T* const* bands(size_t channel) const {
henrikg91d6ede2015-09-17 00:24:34 -070094 RTC_DCHECK_LT(channel, num_channels_);
kwibergaf476c72016-11-28 15:21:39 -080095 RTC_DCHECK_GE(channel, 0);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000096 return &bands_[channel * num_bands_];
97 }
Peter Kasting69558702016-01-12 16:26:35 -080098 T* const* bands(size_t channel) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000099 const ChannelBuffer<T>* t = this;
100 return const_cast<T* const*>(t->bands(channel));
101 }
aluebs@webrtc.org87893762014-11-27 23:40:25 +0000102
andrew@webrtc.org041035b2015-01-26 21:23:53 +0000103 // Sets the |slice| pointers to the |start_frame| position for each channel.
104 // Returns |slice| for convenience.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700105 const T* const* Slice(T** slice, size_t start_frame) const {
henrikg91d6ede2015-09-17 00:24:34 -0700106 RTC_DCHECK_LT(start_frame, num_frames_);
Peter Kasting69558702016-01-12 16:26:35 -0800107 for (size_t i = 0; i < num_channels_; ++i)
andrew@webrtc.org041035b2015-01-26 21:23:53 +0000108 slice[i] = &channels_[i][start_frame];
109 return slice;
110 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700111 T** Slice(T** slice, size_t start_frame) {
andrew@webrtc.org041035b2015-01-26 21:23:53 +0000112 const ChannelBuffer<T>* t = this;
113 return const_cast<T**>(t->Slice(slice, start_frame));
114 }
115
Peter Kastingdce40cf2015-08-24 14:52:23 -0700116 size_t num_frames() const { return num_frames_; }
117 size_t num_frames_per_band() const { return num_frames_per_band_; }
Peter Kasting69558702016-01-12 16:26:35 -0800118 size_t num_channels() const { return num_channels_; }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700119 size_t num_bands() const { return num_bands_; }
Yves Gerey665174f2018-06-19 15:03:05 +0200120 size_t size() const { return num_frames_ * num_allocated_channels_; }
Alejandro Luebsa181c9a2016-06-30 15:33:37 -0700121
122 void set_num_channels(size_t num_channels) {
123 RTC_DCHECK_LE(num_channels, num_allocated_channels_);
124 num_channels_ = num_channels;
125 }
aluebs@webrtc.org87893762014-11-27 23:40:25 +0000126
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000127 void SetDataForTesting(const T* data, size_t size) {
henrikg91d6ede2015-09-17 00:24:34 -0700128 RTC_CHECK_EQ(size, this->size());
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000129 memcpy(data_.get(), data, size * sizeof(*data));
aluebs@webrtc.org87893762014-11-27 23:40:25 +0000130 }
131
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000132 private:
kwibergc2b785d2016-02-24 05:22:32 -0800133 std::unique_ptr<T[]> data_;
Jonas Olssona4d87372019-07-05 19:08:33 +0200134 std::unique_ptr<T*[]> channels_;
135 std::unique_ptr<T*[]> bands_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700136 const size_t num_frames_;
137 const size_t num_frames_per_band_;
Alejandro Luebsa181c9a2016-06-30 15:33:37 -0700138 // Number of channels the internal buffer holds.
139 const size_t num_allocated_channels_;
140 // Number of channels the user sees.
141 size_t num_channels_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700142 const size_t num_bands_;
aluebs@webrtc.org87893762014-11-27 23:40:25 +0000143};
144
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000145// One int16_t and one float ChannelBuffer that are kept in sync. The sync is
146// broken when someone requests write access to either ChannelBuffer, and
147// reestablished when someone requests the outdated ChannelBuffer. It is
148// therefore safe to use the return value of ibuf_const() and fbuf_const()
149// until the next call to ibuf() or fbuf(), and the return value of ibuf() and
150// fbuf() until the next call to any of the other functions.
151class IFChannelBuffer {
152 public:
Peter Kasting69558702016-01-12 16:26:35 -0800153 IFChannelBuffer(size_t num_frames, size_t num_channels, size_t num_bands = 1);
kwiberg942c8512016-08-29 13:10:29 -0700154 ~IFChannelBuffer();
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000155
156 ChannelBuffer<int16_t>* ibuf();
157 ChannelBuffer<float>* fbuf();
158 const ChannelBuffer<int16_t>* ibuf_const() const;
159 const ChannelBuffer<float>* fbuf_const() const;
160
Peter Kastingdce40cf2015-08-24 14:52:23 -0700161 size_t num_frames() const { return ibuf_.num_frames(); }
162 size_t num_frames_per_band() const { return ibuf_.num_frames_per_band(); }
Alejandro Luebsa181c9a2016-06-30 15:33:37 -0700163 size_t num_channels() const {
164 return ivalid_ ? ibuf_.num_channels() : fbuf_.num_channels();
165 }
166 void set_num_channels(size_t num_channels) {
167 ibuf_.set_num_channels(num_channels);
168 fbuf_.set_num_channels(num_channels);
169 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700170 size_t num_bands() const { return ibuf_.num_bands(); }
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000171
172 private:
173 void RefreshF() const;
174 void RefreshI() const;
175
176 mutable bool ivalid_;
177 mutable ChannelBuffer<int16_t> ibuf_;
178 mutable bool fvalid_;
179 mutable ChannelBuffer<float> fbuf_;
180};
181
182} // namespace webrtc
183
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200184#endif // COMMON_AUDIO_CHANNEL_BUFFER_H_