xbmc
AEUtil.h
1 /*
2  * Copyright (C) 2010-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "AEAudioFormat.h"
12 #include "PlatformDefs.h"
13 #include <math.h>
14 
15 extern "C" {
16 #include <libavutil/channel_layout.h>
17 #include <libavutil/samplefmt.h>
18 }
19 
20 // AV sync options
21 enum AVSync
22 {
23  SYNC_DISCON = 0,
24  SYNC_RESAMPLE
25 };
26 
28 {
29  void SetDelay(double d);
30  double GetDelay() const;
31 
32  double delay = 0.0; // delay in sink currently
33  double maxcorrection = 0.0; // time correction must not be greater than sink delay
34  int64_t tick = 0; // timestamp when delay was calculated
35 };
36 
55 {
56 public:
57  void enter() { m_enter++; }
58  void leave() { m_leave = m_enter; }
59 
60 protected:
61  friend class CAESpinLock;
62  volatile unsigned int m_enter = 0;
63  volatile unsigned int m_leave = 0;
64 };
65 
67 {
68 public:
69  explicit CAESpinLock(CAESpinSection& section)
70  : m_section(section)
71  , m_begin(section.m_enter)
72  {}
73 
74  bool retry()
75  {
76  if(m_section.m_enter != m_begin
77  || m_section.m_enter != m_section.m_leave)
78  {
79  m_begin = m_section.m_enter;
80  return true;
81  }
82  else
83  return false;
84  }
85 
86 private:
87  CAESpinSection& m_section;
88  unsigned int m_begin;
89 };
90 
91 class CAEUtil
92 {
93 private:
94 
95  static float SoftClamp(const float x);
96 
97 public:
98  static CAEChannelInfo GuessChLayout (const unsigned int channels);
99  static const char* GetStdChLayoutName(const enum AEStdChLayout layout);
100  static unsigned int DataFormatToBits (const enum AEDataFormat dataFormat);
101  static unsigned int DataFormatToUsedBits (const enum AEDataFormat dataFormat);
102  static unsigned int DataFormatToDitherBits(const enum AEDataFormat dataFormat);
103  static const char* DataFormatToStr (const enum AEDataFormat dataFormat);
104  static const char* StreamTypeToStr(const enum CAEStreamInfo::DataType dataType);
105 
113  static inline float PercentToGain(const float value)
114  {
115  static const float db_range = 60.0f;
116  return (value - 1)*db_range;
117  }
118 
126  static inline float GainToPercent(const float gain)
127  {
128  static const float db_range = 60.0f;
129  return 1+(gain/db_range);
130  }
131 
138  static inline float GainToScale(const float dB)
139  {
140  float val = 0.0f;
141  // we need to make sure that our lowest db returns plain zero
142  if (dB > -60.0f)
143  val = pow(10.0f, dB/20);
144 
145  // in order to not introduce computing overhead for nearly zero
146  // values of dB e.g. -0.01 or -0.001 we clamp to top
147  if (val >= 0.99f)
148  val = 1.0f;
149 
150  return val;
151  }
152 
159  static inline float ScaleToGain(const float scale)
160  {
161  return 20*log10(scale);
162  }
163 
164  #if defined(HAVE_SSE) && defined(__SSE__)
165  static void SSEMulArray (float *data, const float mul, uint32_t count);
166  static void SSEMulAddArray (float *data, float *add, const float mul, uint32_t count);
167  #endif
168  static void ClampArray(float *data, uint32_t count);
169 
170  static bool S16NeedsByteSwap(AEDataFormat in, AEDataFormat out);
171 
172  static uint64_t GetAVChannelLayout(const CAEChannelInfo &info);
173  static CAEChannelInfo GetAEChannelLayout(uint64_t layout);
174  static AVSampleFormat GetAVSampleFormat(AEDataFormat format);
175  static uint64_t GetAVChannelMask(enum AEChannel aechannel);
176  static enum AVChannel GetAVChannel(enum AEChannel aechannel);
177  static int GetAVChannelIndex(enum AEChannel aechannel, uint64_t layout);
178 };
Definition: AEUtil.h:66
Definition: AEUtil.h:91
static float ScaleToGain(const float scale)
convert a scale factor to dB gain for audio manipulation Inverts GainToScale result ...
Definition: AEUtil.h:159
static float PercentToGain(const float value)
convert a volume percentage (as a proportion) to a dB gain We assume a dB range of 60dB...
Definition: AEUtil.h:113
Definition: AEUtil.h:27
lockless consistency guaranteeer
Definition: AEUtil.h:54
static float GainToPercent(const float gain)
convert a dB gain to volume percentage (as a proportion) We assume a dB range of 60dB, i.e. assume that 0% volume corresponds to a reduction of 60dB.
Definition: AEUtil.h:126
Definition: AEChannelInfo.h:19
static float GainToScale(const float dB)
convert a dB gain to a scale factor for audio manipulation Inverts gain = 20 log_10(scale) ...
Definition: AEUtil.h:138