channel_buffer.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #ifndef COMMON_AUDIO_CHANNEL_BUFFER_H_
  11. #define COMMON_AUDIO_CHANNEL_BUFFER_H_
  12. #include <string.h>
  13. #include <memory>
  14. #include <vector>
  15. #include "api/array_view.h"
  16. #include "common_audio/include/audio_util.h"
  17. #include "rtc_base/checks.h"
  18. #include "rtc_base/gtest_prod_util.h"
  19. namespace webrtc {
  20. // Helper to encapsulate a contiguous data buffer, full or split into frequency
  21. // bands, with access to a pointer arrays of the deinterleaved channels and
  22. // bands. The buffer is zero initialized at creation.
  23. //
  24. // The buffer structure is showed below for a 2 channel and 2 bands case:
  25. //
  26. // |data_|:
  27. // { [ --- b1ch1 --- ] [ --- b2ch1 --- ] [ --- b1ch2 --- ] [ --- b2ch2 --- ] }
  28. //
  29. // The pointer arrays for the same example are as follows:
  30. //
  31. // |channels_|:
  32. // { [ b1ch1* ] [ b1ch2* ] [ b2ch1* ] [ b2ch2* ] }
  33. //
  34. // |bands_|:
  35. // { [ b1ch1* ] [ b2ch1* ] [ b1ch2* ] [ b2ch2* ] }
  36. template <typename T>
  37. class ChannelBuffer {
  38. public:
  39. ChannelBuffer(size_t num_frames, size_t num_channels, size_t num_bands = 1)
  40. : data_(new T[num_frames * num_channels]()),
  41. channels_(new T*[num_channels * num_bands]),
  42. bands_(new T*[num_channels * num_bands]),
  43. num_frames_(num_frames),
  44. num_frames_per_band_(num_frames / num_bands),
  45. num_allocated_channels_(num_channels),
  46. num_channels_(num_channels),
  47. num_bands_(num_bands),
  48. bands_view_(num_allocated_channels_,
  49. std::vector<rtc::ArrayView<T>>(num_bands_)),
  50. channels_view_(
  51. num_bands_,
  52. std::vector<rtc::ArrayView<T>>(num_allocated_channels_)) {
  53. // Temporarily cast away const_ness to allow populating the array views.
  54. auto* bands_view =
  55. const_cast<std::vector<std::vector<rtc::ArrayView<T>>>*>(&bands_view_);
  56. auto* channels_view =
  57. const_cast<std::vector<std::vector<rtc::ArrayView<T>>>*>(
  58. &channels_view_);
  59. for (size_t ch = 0; ch < num_allocated_channels_; ++ch) {
  60. for (size_t band = 0; band < num_bands_; ++band) {
  61. (*channels_view)[band][ch] = rtc::ArrayView<T>(
  62. &data_[ch * num_frames_ + band * num_frames_per_band_],
  63. num_frames_per_band_);
  64. (*bands_view)[ch][band] = channels_view_[band][ch];
  65. channels_[band * num_allocated_channels_ + ch] =
  66. channels_view_[band][ch].data();
  67. bands_[ch * num_bands_ + band] =
  68. channels_[band * num_allocated_channels_ + ch];
  69. }
  70. }
  71. }
  72. // Returns a pointer array to the channels.
  73. // If band is explicitly specificed, the channels for a specific band are
  74. // returned and the usage becomes: channels(band)[channel][sample].
  75. // Where:
  76. // 0 <= band < |num_bands_|
  77. // 0 <= channel < |num_allocated_channels_|
  78. // 0 <= sample < |num_frames_per_band_|
  79. // If band is not explicitly specified, the full-band channels (or lower band
  80. // channels) are returned and the usage becomes: channels()[channel][sample].
  81. // Where:
  82. // 0 <= channel < |num_allocated_channels_|
  83. // 0 <= sample < |num_frames_|
  84. const T* const* channels(size_t band = 0) const {
  85. RTC_DCHECK_LT(band, num_bands_);
  86. return &channels_[band * num_allocated_channels_];
  87. }
  88. T* const* channels(size_t band = 0) {
  89. const ChannelBuffer<T>* t = this;
  90. return const_cast<T* const*>(t->channels(band));
  91. }
  92. rtc::ArrayView<const rtc::ArrayView<T>> channels_view(size_t band = 0) {
  93. return channels_view_[band];
  94. }
  95. rtc::ArrayView<const rtc::ArrayView<T>> channels_view(size_t band = 0) const {
  96. return channels_view_[band];
  97. }
  98. // Returns a pointer array to the bands for a specific channel.
  99. // Usage:
  100. // bands(channel)[band][sample].
  101. // Where:
  102. // 0 <= channel < |num_channels_|
  103. // 0 <= band < |num_bands_|
  104. // 0 <= sample < |num_frames_per_band_|
  105. const T* const* bands(size_t channel) const {
  106. RTC_DCHECK_LT(channel, num_channels_);
  107. RTC_DCHECK_GE(channel, 0);
  108. return &bands_[channel * num_bands_];
  109. }
  110. T* const* bands(size_t channel) {
  111. const ChannelBuffer<T>* t = this;
  112. return const_cast<T* const*>(t->bands(channel));
  113. }
  114. rtc::ArrayView<const rtc::ArrayView<T>> bands_view(size_t channel) {
  115. return bands_view_[channel];
  116. }
  117. rtc::ArrayView<const rtc::ArrayView<T>> bands_view(size_t channel) const {
  118. return bands_view_[channel];
  119. }
  120. // Sets the |slice| pointers to the |start_frame| position for each channel.
  121. // Returns |slice| for convenience.
  122. const T* const* Slice(T** slice, size_t start_frame) const {
  123. RTC_DCHECK_LT(start_frame, num_frames_);
  124. for (size_t i = 0; i < num_channels_; ++i)
  125. slice[i] = &channels_[i][start_frame];
  126. return slice;
  127. }
  128. T** Slice(T** slice, size_t start_frame) {
  129. const ChannelBuffer<T>* t = this;
  130. return const_cast<T**>(t->Slice(slice, start_frame));
  131. }
  132. size_t num_frames() const { return num_frames_; }
  133. size_t num_frames_per_band() const { return num_frames_per_band_; }
  134. size_t num_channels() const { return num_channels_; }
  135. size_t num_bands() const { return num_bands_; }
  136. size_t size() const { return num_frames_ * num_allocated_channels_; }
  137. void set_num_channels(size_t num_channels) {
  138. RTC_DCHECK_LE(num_channels, num_allocated_channels_);
  139. num_channels_ = num_channels;
  140. }
  141. void SetDataForTesting(const T* data, size_t size) {
  142. RTC_CHECK_EQ(size, this->size());
  143. memcpy(data_.get(), data, size * sizeof(*data));
  144. }
  145. private:
  146. std::unique_ptr<T[]> data_;
  147. std::unique_ptr<T*[]> channels_;
  148. std::unique_ptr<T*[]> bands_;
  149. const size_t num_frames_;
  150. const size_t num_frames_per_band_;
  151. // Number of channels the internal buffer holds.
  152. const size_t num_allocated_channels_;
  153. // Number of channels the user sees.
  154. size_t num_channels_;
  155. const size_t num_bands_;
  156. const std::vector<std::vector<rtc::ArrayView<T>>> bands_view_;
  157. const std::vector<std::vector<rtc::ArrayView<T>>> channels_view_;
  158. };
  159. // One int16_t and one float ChannelBuffer that are kept in sync. The sync is
  160. // broken when someone requests write access to either ChannelBuffer, and
  161. // reestablished when someone requests the outdated ChannelBuffer. It is
  162. // therefore safe to use the return value of ibuf_const() and fbuf_const()
  163. // until the next call to ibuf() or fbuf(), and the return value of ibuf() and
  164. // fbuf() until the next call to any of the other functions.
  165. class IFChannelBuffer {
  166. public:
  167. IFChannelBuffer(size_t num_frames, size_t num_channels, size_t num_bands = 1);
  168. ~IFChannelBuffer();
  169. ChannelBuffer<int16_t>* ibuf();
  170. ChannelBuffer<float>* fbuf();
  171. const ChannelBuffer<int16_t>* ibuf_const() const;
  172. const ChannelBuffer<float>* fbuf_const() const;
  173. size_t num_frames() const { return ibuf_.num_frames(); }
  174. size_t num_frames_per_band() const { return ibuf_.num_frames_per_band(); }
  175. size_t num_channels() const {
  176. return ivalid_ ? ibuf_.num_channels() : fbuf_.num_channels();
  177. }
  178. void set_num_channels(size_t num_channels) {
  179. ibuf_.set_num_channels(num_channels);
  180. fbuf_.set_num_channels(num_channels);
  181. }
  182. size_t num_bands() const { return ibuf_.num_bands(); }
  183. private:
  184. void RefreshF() const;
  185. void RefreshI() const;
  186. mutable bool ivalid_;
  187. mutable ChannelBuffer<int16_t> ibuf_;
  188. mutable bool fvalid_;
  189. mutable ChannelBuffer<float> fbuf_;
  190. };
  191. } // namespace webrtc
  192. #endif // COMMON_AUDIO_CHANNEL_BUFFER_H_