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

          Line data    Source code
       1             : /*
       2             :  *  Copyright (c) 2013 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_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_
      12             : #define WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_
      13             : 
      14             : #include <limits>
      15             : #include <cstring>
      16             : 
      17             : #include "webrtc/base/checks.h"
      18             : #include "webrtc/typedefs.h"
      19             : 
      20             : namespace webrtc {
      21             : 
      22             : typedef std::numeric_limits<int16_t> limits_int16;
      23             : 
      24             : // The conversion functions use the following naming convention:
      25             : // S16:      int16_t [-32768, 32767]
      26             : // Float:    float   [-1.0, 1.0]
      27             : // FloatS16: float   [-32768.0, 32767.0]
      28           0 : static inline int16_t FloatToS16(float v) {
      29           0 :   if (v > 0)
      30           0 :     return v >= 1 ? limits_int16::max()
      31           0 :                   : static_cast<int16_t>(v * limits_int16::max() + 0.5f);
      32           0 :   return v <= -1 ? limits_int16::min()
      33           0 :                  : static_cast<int16_t>(-v * limits_int16::min() - 0.5f);
      34             : }
      35             : 
      36           0 : static inline float S16ToFloat(int16_t v) {
      37             :   static const float kMaxInt16Inverse = 1.f / limits_int16::max();
      38             :   static const float kMinInt16Inverse = 1.f / limits_int16::min();
      39           0 :   return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse);
      40             : }
      41             : 
      42           0 : static inline int16_t FloatS16ToS16(float v) {
      43             :   static const float kMaxRound = limits_int16::max() - 0.5f;
      44             :   static const float kMinRound = limits_int16::min() + 0.5f;
      45           0 :   if (v > 0)
      46           0 :     return v >= kMaxRound ? limits_int16::max()
      47           0 :                           : static_cast<int16_t>(v + 0.5f);
      48           0 :   return v <= kMinRound ? limits_int16::min() : static_cast<int16_t>(v - 0.5f);
      49             : }
      50             : 
      51           0 : static inline float FloatToFloatS16(float v) {
      52           0 :   return v * (v > 0 ? limits_int16::max() : -limits_int16::min());
      53             : }
      54             : 
      55           0 : static inline float FloatS16ToFloat(float v) {
      56             :   static const float kMaxInt16Inverse = 1.f / limits_int16::max();
      57             :   static const float kMinInt16Inverse = 1.f / limits_int16::min();
      58           0 :   return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse);
      59             : }
      60             : 
      61             : void FloatToS16(const float* src, size_t size, int16_t* dest);
      62             : void S16ToFloat(const int16_t* src, size_t size, float* dest);
      63             : void FloatS16ToS16(const float* src, size_t size, int16_t* dest);
      64             : void FloatToFloatS16(const float* src, size_t size, float* dest);
      65             : void FloatS16ToFloat(const float* src, size_t size, float* dest);
      66             : 
      67             : // Copy audio from |src| channels to |dest| channels unless |src| and |dest|
      68             : // point to the same address. |src| and |dest| must have the same number of
      69             : // channels, and there must be sufficient space allocated in |dest|.
      70             : template <typename T>
      71           0 : void CopyAudioIfNeeded(const T* const* src,
      72             :                        int num_frames,
      73             :                        int num_channels,
      74             :                        T* const* dest) {
      75           0 :   for (int i = 0; i < num_channels; ++i) {
      76           0 :     if (src[i] != dest[i]) {
      77           0 :       std::copy(src[i], src[i] + num_frames, dest[i]);
      78             :     }
      79             :   }
      80           0 : }
      81             : 
      82             : // Deinterleave audio from |interleaved| to the channel buffers pointed to
      83             : // by |deinterleaved|. There must be sufficient space allocated in the
      84             : // |deinterleaved| buffers (|num_channel| buffers with |samples_per_channel|
      85             : // per buffer).
      86             : template <typename T>
      87           0 : void Deinterleave(const T* interleaved,
      88             :                   size_t samples_per_channel,
      89             :                   size_t num_channels,
      90             :                   T* const* deinterleaved) {
      91           0 :   for (size_t i = 0; i < num_channels; ++i) {
      92           0 :     T* channel = deinterleaved[i];
      93           0 :     size_t interleaved_idx = i;
      94           0 :     for (size_t j = 0; j < samples_per_channel; ++j) {
      95           0 :       channel[j] = interleaved[interleaved_idx];
      96           0 :       interleaved_idx += num_channels;
      97             :     }
      98             :   }
      99           0 : }
     100             : 
     101             : // Interleave audio from the channel buffers pointed to by |deinterleaved| to
     102             : // |interleaved|. There must be sufficient space allocated in |interleaved|
     103             : // (|samples_per_channel| * |num_channels|).
     104             : template <typename T>
     105           0 : void Interleave(const T* const* deinterleaved,
     106             :                 size_t samples_per_channel,
     107             :                 size_t num_channels,
     108             :                 T* interleaved) {
     109           0 :   for (size_t i = 0; i < num_channels; ++i) {
     110           0 :     const T* channel = deinterleaved[i];
     111           0 :     size_t interleaved_idx = i;
     112           0 :     for (size_t j = 0; j < samples_per_channel; ++j) {
     113           0 :       interleaved[interleaved_idx] = channel[j];
     114           0 :       interleaved_idx += num_channels;
     115             :     }
     116             :   }
     117           0 : }
     118             : 
     119             : // Copies audio from a single channel buffer pointed to by |mono| to each
     120             : // channel of |interleaved|. There must be sufficient space allocated in
     121             : // |interleaved| (|samples_per_channel| * |num_channels|).
     122             : template <typename T>
     123           0 : void UpmixMonoToInterleaved(const T* mono,
     124             :                             int num_frames,
     125             :                             int num_channels,
     126             :                             T* interleaved) {
     127           0 :   int interleaved_idx = 0;
     128           0 :   for (int i = 0; i < num_frames; ++i) {
     129           0 :     for (int j = 0; j < num_channels; ++j) {
     130           0 :       interleaved[interleaved_idx++] = mono[i];
     131             :     }
     132             :   }
     133           0 : }
     134             : 
     135             : template <typename T, typename Intermediate>
     136           0 : void DownmixToMono(const T* const* input_channels,
     137             :                    size_t num_frames,
     138             :                    int num_channels,
     139             :                    T* out) {
     140           0 :   for (size_t i = 0; i < num_frames; ++i) {
     141           0 :     Intermediate value = input_channels[0][i];
     142           0 :     for (int j = 1; j < num_channels; ++j) {
     143           0 :       value += input_channels[j][i];
     144             :     }
     145           0 :     out[i] = value / num_channels;
     146             :   }
     147           0 : }
     148             : 
     149             : // Downmixes an interleaved multichannel signal to a single channel by averaging
     150             : // all channels.
     151             : template <typename T, typename Intermediate>
     152           0 : void DownmixInterleavedToMonoImpl(const T* interleaved,
     153             :                                   size_t num_frames,
     154             :                                   int num_channels,
     155             :                                   T* deinterleaved) {
     156           0 :   RTC_DCHECK_GT(num_channels, 0);
     157           0 :   RTC_DCHECK_GT(num_frames, 0);
     158             : 
     159           0 :   const T* const end = interleaved + num_frames * num_channels;
     160             : 
     161           0 :   while (interleaved < end) {
     162           0 :     const T* const frame_end = interleaved + num_channels;
     163             : 
     164           0 :     Intermediate value = *interleaved++;
     165           0 :     while (interleaved < frame_end) {
     166           0 :       value += *interleaved++;
     167             :     }
     168             : 
     169           0 :     *deinterleaved++ = value / num_channels;
     170             :   }
     171           0 : }
     172             : 
     173             : template <typename T>
     174             : void DownmixInterleavedToMono(const T* interleaved,
     175             :                               size_t num_frames,
     176             :                               int num_channels,
     177             :                               T* deinterleaved);
     178             : 
     179             : template <>
     180             : void DownmixInterleavedToMono<int16_t>(const int16_t* interleaved,
     181             :                                        size_t num_frames,
     182             :                                        int num_channels,
     183             :                                        int16_t* deinterleaved);
     184             : 
     185             : }  // namespace webrtc
     186             : 
     187             : #endif  // WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_

Generated by: LCOV version 1.13