LCOV - code coverage report
Current view: top level - media/webrtc/trunk/webrtc/common_audio - channel_buffer.h (source / functions) Hit Total Coverage
Test: output.info Lines: 0 47 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 32 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      11             : #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_
      12             : #define WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_
      13             : 
      14             : #include <string.h>
      15             : 
      16             : #include <memory>
      17             : 
      18             : #include "webrtc/base/checks.h"
      19             : #include "webrtc/base/gtest_prod_util.h"
      20             : #include "webrtc/common_audio/include/audio_util.h"
      21             : 
      22             : namespace webrtc {
      23             : 
      24             : // 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* ] }
      40             : template <typename T>
      41           0 : class ChannelBuffer {
      42             :  public:
      43           0 :   ChannelBuffer(size_t num_frames,
      44             :                 size_t num_channels,
      45             :                 size_t num_bands = 1)
      46           0 :       : data_(new T[num_frames * num_channels]()),
      47           0 :         channels_(new T*[num_channels * num_bands]),
      48           0 :         bands_(new T*[num_channels * num_bands]),
      49             :         num_frames_(num_frames),
      50           0 :         num_frames_per_band_(num_frames / num_bands),
      51             :         num_allocated_channels_(num_channels),
      52             :         num_channels_(num_channels),
      53           0 :         num_bands_(num_bands) {
      54           0 :     for (size_t i = 0; i < num_allocated_channels_; ++i) {
      55           0 :       for (size_t j = 0; j < num_bands_; ++j) {
      56           0 :         channels_[j * num_allocated_channels_ + i] =
      57           0 :             &data_[i * num_frames_ + j * num_frames_per_band_];
      58           0 :         bands_[i * num_bands_ + j] = channels_[j * num_allocated_channels_ + i];
      59             :       }
      60             :     }
      61           0 :   }
      62             : 
      63             :   // Returns a pointer array to the full-band channels (or lower band channels).
      64             :   // Usage:
      65             :   // channels()[channel][sample].
      66             :   // Where:
      67             :   // 0 <= channel < |num_allocated_channels_|
      68             :   // 0 <= sample < |num_frames_|
      69           0 :   T* const* channels() { return channels(0); }
      70           0 :   const T* const* channels() const { return channels(0); }
      71             : 
      72             :   // Returns a pointer array to the channels for a specific band.
      73             :   // Usage:
      74             :   // 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           0 :   const T* const* channels(size_t band) const {
      80           0 :     RTC_DCHECK_LT(band, num_bands_);
      81           0 :     return &channels_[band * num_allocated_channels_];
      82             :   }
      83           0 :   T* const* channels(size_t band) {
      84           0 :     const ChannelBuffer<T>* t = this;
      85           0 :     return const_cast<T* const*>(t->channels(band));
      86             :   }
      87             : 
      88             :   // 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           0 :   const T* const* bands(size_t channel) const {
      96           0 :     RTC_DCHECK_LT(channel, num_channels_);
      97           0 :     RTC_DCHECK_GE(channel, 0);
      98           0 :     return &bands_[channel * num_bands_];
      99             :   }
     100           0 :   T* const* bands(size_t channel) {
     101           0 :     const ChannelBuffer<T>* t = this;
     102           0 :     return const_cast<T* const*>(t->bands(channel));
     103             :   }
     104             : 
     105             :   // Sets the |slice| pointers to the |start_frame| position for each channel.
     106             :   // Returns |slice| for convenience.
     107             :   const T* const* Slice(T** slice, size_t start_frame) const {
     108             :     RTC_DCHECK_LT(start_frame, num_frames_);
     109             :     for (size_t i = 0; i < num_channels_; ++i)
     110             :       slice[i] = &channels_[i][start_frame];
     111             :     return slice;
     112             :   }
     113             :   T** Slice(T** slice, size_t start_frame) {
     114             :     const ChannelBuffer<T>* t = this;
     115             :     return const_cast<T**>(t->Slice(slice, start_frame));
     116             :   }
     117             : 
     118           0 :   size_t num_frames() const { return num_frames_; }
     119           0 :   size_t num_frames_per_band() const { return num_frames_per_band_; }
     120           0 :   size_t num_channels() const { return num_channels_; }
     121           0 :   size_t num_bands() const { return num_bands_; }
     122           0 :   size_t size() const {return num_frames_ * num_allocated_channels_; }
     123             : 
     124           0 :   void set_num_channels(size_t num_channels) {
     125           0 :     RTC_DCHECK_LE(num_channels, num_allocated_channels_);
     126           0 :     num_channels_ = num_channels;
     127           0 :   }
     128             : 
     129             :   void SetDataForTesting(const T* data, size_t size) {
     130             :     RTC_CHECK_EQ(size, this->size());
     131             :     memcpy(data_.get(), data, size * sizeof(*data));
     132             :   }
     133             : 
     134             :  private:
     135             :   std::unique_ptr<T[]> data_;
     136             :   std::unique_ptr<T* []> channels_;
     137             :   std::unique_ptr<T* []> bands_;
     138             :   const size_t num_frames_;
     139             :   const size_t num_frames_per_band_;
     140             :   // Number of channels the internal buffer holds.
     141             :   const size_t num_allocated_channels_;
     142             :   // Number of channels the user sees.
     143             :   size_t num_channels_;
     144             :   const size_t num_bands_;
     145             : };
     146             : 
     147             : // One int16_t and one float ChannelBuffer that are kept in sync. The sync is
     148             : // broken when someone requests write access to either ChannelBuffer, and
     149             : // reestablished when someone requests the outdated ChannelBuffer. It is
     150             : // therefore safe to use the return value of ibuf_const() and fbuf_const()
     151             : // until the next call to ibuf() or fbuf(), and the return value of ibuf() and
     152             : // fbuf() until the next call to any of the other functions.
     153           0 : class IFChannelBuffer {
     154             :  public:
     155             :   IFChannelBuffer(size_t num_frames, size_t num_channels, size_t num_bands = 1);
     156             :   ~IFChannelBuffer();
     157             : 
     158             :   ChannelBuffer<int16_t>* ibuf();
     159             :   ChannelBuffer<float>* fbuf();
     160             :   const ChannelBuffer<int16_t>* ibuf_const() const;
     161             :   const ChannelBuffer<float>* fbuf_const() const;
     162             : 
     163           0 :   size_t num_frames() const { return ibuf_.num_frames(); }
     164           0 :   size_t num_frames_per_band() const { return ibuf_.num_frames_per_band(); }
     165           0 :   size_t num_channels() const {
     166           0 :     return ivalid_ ? ibuf_.num_channels() : fbuf_.num_channels();
     167             :   }
     168           0 :   void set_num_channels(size_t num_channels) {
     169           0 :     ibuf_.set_num_channels(num_channels);
     170           0 :     fbuf_.set_num_channels(num_channels);
     171           0 :   }
     172           0 :   size_t num_bands() const { return ibuf_.num_bands(); }
     173             : 
     174             :  private:
     175             :   void RefreshF() const;
     176             :   void RefreshI() const;
     177             : 
     178             :   mutable bool ivalid_;
     179             :   mutable ChannelBuffer<int16_t> ibuf_;
     180             :   mutable bool fvalid_;
     181             :   mutable ChannelBuffer<float> fbuf_;
     182             : };
     183             : 
     184             : }  // namespace webrtc
     185             : 
     186             : #endif  // WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_

Generated by: LCOV version 1.13