blob: a5dcc6c2641011f683def5c6ca1a0fa0983d9a4b [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
11#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_
13
aluebs@webrtc.org87893762014-11-27 23:40:25 +000014#include <string.h>
15
16#include "webrtc/base/checks.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000017#include "webrtc/base/scoped_ptr.h"
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000018#include "webrtc/common_audio/include/audio_util.h"
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000019#include "webrtc/test/testsupport/gtest_prod_util.h"
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000020
21namespace webrtc {
22
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000023// 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.org87893762014-11-27 23:40:25 +000039template <typename T>
40class ChannelBuffer {
41 public:
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000042 ChannelBuffer(int num_frames,
43 int num_channels,
44 int num_bands = 1)
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +000045 : data_(new T[num_frames * num_channels]()),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000046 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) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000052 for (int i = 0; i < num_channels_; ++i) {
53 for (int j = 0; j < num_bands_; ++j) {
54 channels_[j * num_channels_ + i] =
55 &data_[i * num_frames_ + j * num_frames_per_band_];
56 bands_[i * num_bands_ + j] = channels_[j * num_channels_ + i];
57 }
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:
65 // 0 <= channel < |num_channels_|
66 // 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_|
75 // 0 <= channel < |num_channels_|
76 // 0 <= sample < |num_frames_per_band_|
77 const T* const* channels(int band) const {
78 DCHECK_LT(band, num_bands_);
79 DCHECK_GE(band, 0);
80 return &channels_[band * num_channels_];
aluebs@webrtc.org87893762014-11-27 23:40:25 +000081 }
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000082 T* const* channels(int band) {
aluebs@webrtc.org87893762014-11-27 23:40:25 +000083 const ChannelBuffer<T>* t = this;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000084 return const_cast<T* const*>(t->channels(band));
aluebs@webrtc.org87893762014-11-27 23:40:25 +000085 }
86
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000087 // 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_|
94 const T* const* bands(int channel) const {
95 DCHECK_LT(channel, num_channels_);
96 DCHECK_GE(channel, 0);
97 return &bands_[channel * num_bands_];
98 }
99 T* const* bands(int channel) {
100 const ChannelBuffer<T>* t = this;
101 return const_cast<T* const*>(t->bands(channel));
102 }
aluebs@webrtc.org87893762014-11-27 23:40:25 +0000103
andrew@webrtc.org041035b2015-01-26 21:23:53 +0000104 // Sets the |slice| pointers to the |start_frame| position for each channel.
105 // Returns |slice| for convenience.
106 const T* const* Slice(T** slice, int start_frame) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000107 DCHECK_LT(start_frame, num_frames_);
andrew@webrtc.org041035b2015-01-26 21:23:53 +0000108 for (int i = 0; i < num_channels_; ++i)
109 slice[i] = &channels_[i][start_frame];
110 return slice;
111 }
112 T** Slice(T** slice, int start_frame) {
113 const ChannelBuffer<T>* t = this;
114 return const_cast<T**>(t->Slice(slice, start_frame));
115 }
116
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000117 int num_frames() const { return num_frames_; }
118 int num_frames_per_band() const { return num_frames_per_band_; }
aluebs@webrtc.org87893762014-11-27 23:40:25 +0000119 int num_channels() const { return num_channels_; }
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000120 int num_bands() const { return num_bands_; }
121 size_t size() const {return num_frames_ * num_channels_; }
aluebs@webrtc.org87893762014-11-27 23:40:25 +0000122
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000123 void SetDataForTesting(const T* data, size_t size) {
124 CHECK_EQ(size, this->size());
125 memcpy(data_.get(), data, size * sizeof(*data));
aluebs@webrtc.org87893762014-11-27 23:40:25 +0000126 }
127
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000128 private:
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000129 rtc::scoped_ptr<T[]> data_;
130 rtc::scoped_ptr<T* []> channels_;
131 rtc::scoped_ptr<T* []> bands_;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000132 const int num_frames_;
133 const int num_frames_per_band_;
aluebs@webrtc.org87893762014-11-27 23:40:25 +0000134 const int num_channels_;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000135 const int num_bands_;
aluebs@webrtc.org87893762014-11-27 23:40:25 +0000136};
137
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000138// 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.
144class IFChannelBuffer {
145 public:
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000146 IFChannelBuffer(int num_frames, int num_channels, int num_bands = 1);
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000147
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
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000153 int num_frames() const { return ibuf_.num_frames(); }
154 int num_frames_per_band() const { return ibuf_.num_frames_per_band(); }
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000155 int num_channels() const { return ibuf_.num_channels(); }
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000156 int num_bands() const { return ibuf_.num_bands(); }
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000157
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_