blob: 323c5e9b5fb68b5eeb761018003171ffe831cbd9 [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)
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.org87893762014-11-27 23:40:25 +000060 }
61
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000062 // 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.org87893762014-11-27 23:40:25 +000082 }
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000083 T* const* channels(int band) {
aluebs@webrtc.org87893762014-11-27 23:40:25 +000084 const ChannelBuffer<T>* t = this;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000085 return const_cast<T* const*>(t->channels(band));
aluebs@webrtc.org87893762014-11-27 23:40:25 +000086 }
87
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000088 // 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.org87893762014-11-27 23:40:25 +0000104
andrew@webrtc.org041035b2015-01-26 21:23:53 +0000105 // 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.orgd35a5c32015-02-10 22:52:15 +0000108 DCHECK_LT(start_frame, num_frames_);
andrew@webrtc.org041035b2015-01-26 21:23:53 +0000109 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.orgd35a5c32015-02-10 22:52:15 +0000118 int num_frames() const { return num_frames_; }
119 int num_frames_per_band() const { return num_frames_per_band_; }
aluebs@webrtc.org87893762014-11-27 23:40:25 +0000120 int num_channels() const { return num_channels_; }
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000121 int num_bands() const { return num_bands_; }
122 size_t size() const {return num_frames_ * num_channels_; }
aluebs@webrtc.org87893762014-11-27 23:40:25 +0000123
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000124 void SetDataForTesting(const T* data, size_t size) {
125 CHECK_EQ(size, this->size());
126 memcpy(data_.get(), data, size * sizeof(*data));
aluebs@webrtc.org87893762014-11-27 23:40:25 +0000127 }
128
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000129 private:
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000130 rtc::scoped_ptr<T[]> data_;
131 rtc::scoped_ptr<T* []> channels_;
132 rtc::scoped_ptr<T* []> bands_;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000133 const int num_frames_;
134 const int num_frames_per_band_;
aluebs@webrtc.org87893762014-11-27 23:40:25 +0000135 const int num_channels_;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000136 const int num_bands_;
aluebs@webrtc.org87893762014-11-27 23:40:25 +0000137};
138
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000139// 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.
145class IFChannelBuffer {
146 public:
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000147 IFChannelBuffer(int num_frames, int num_channels, int num_bands = 1);
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000148
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.orgd35a5c32015-02-10 22:52:15 +0000154 int num_frames() const { return ibuf_.num_frames(); }
155 int num_frames_per_band() const { return ibuf_.num_frames_per_band(); }
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000156 int num_channels() const { return ibuf_.num_channels(); }
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000157 int num_bands() const { return ibuf_.num_bands(); }
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000158
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_