You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
6.2 KiB

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