2 * Copyright (C) 2010-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
11 #include "AEAudioFormat.h"
16 #include "PlatformDefs.h"
19 #include <libavutil/channel_layout.h>
20 #include <libavutil/samplefmt.h>
32 void SetDelay(double d
);
33 double GetDelay() const;
35 double delay
= 0.0; // delay in sink currently
36 double maxcorrection
= 0.0; // time correction must not be greater than sink delay
37 int64_t tick
= 0; // timestamp when delay was calculated
41 * @brief lockless consistency guaranteeer
43 * Requires write to be a higher priority thread
51 * CAESpinLock lock(m_locker);
54 * } while(lock.retry());
60 void enter() { m_enter
++; }
61 void leave() { m_leave
.store(m_enter
); }
64 friend class CAESpinLock
;
65 std::atomic_uint32_t m_enter
= 0;
66 std::atomic_uint32_t m_leave
= 0;
72 explicit CAESpinLock(CAESpinSection
& section
)
74 , m_begin(section
.m_enter
)
79 if(m_section
.m_enter
!= m_begin
80 || m_section
.m_enter
!= m_section
.m_leave
)
82 m_begin
= m_section
.m_enter
;
90 CAESpinSection
& m_section
;
98 static float SoftClamp(const float x
);
101 static CAEChannelInfo
GuessChLayout (const unsigned int channels
);
102 static const char* GetStdChLayoutName(const enum AEStdChLayout layout
);
103 static unsigned int DataFormatToBits (const enum AEDataFormat dataFormat
);
104 static unsigned int DataFormatToUsedBits (const enum AEDataFormat dataFormat
);
105 static unsigned int DataFormatToDitherBits(const enum AEDataFormat dataFormat
);
106 static const char* DataFormatToStr (const enum AEDataFormat dataFormat
);
107 static const char* StreamTypeToStr(const enum CAEStreamInfo::DataType dataType
);
109 /*! \brief convert a volume percentage (as a proportion) to a dB gain
110 We assume a dB range of 60dB, i.e. assume that 0% volume corresponds
111 to a reduction of 60dB.
112 \param value the volume from 0..1
113 \return the corresponding gain in dB from -60dB .. 0dB.
116 static inline float PercentToGain(const float value
)
118 static const float db_range
= 60.0f
;
119 return (value
- 1)*db_range
;
122 /*! \brief convert a dB gain to volume percentage (as a proportion)
123 We assume a dB range of 60dB, i.e. assume that 0% volume corresponds
124 to a reduction of 60dB.
125 \param the corresponding gain in dB from -60dB .. 0dB.
126 \return value the volume from 0..1
129 static inline float GainToPercent(const float gain
)
131 static const float db_range
= 60.0f
;
132 return 1+(gain
/db_range
);
135 /*! \brief convert a dB gain to a scale factor for audio manipulation
136 Inverts gain = 20 log_10(scale)
137 \param dB the gain in decibels.
138 \return the scale factor (equivalent to a voltage multiplier).
141 static inline float GainToScale(const float dB
)
144 // we need to make sure that our lowest db returns plain zero
146 val
= pow(10.0f
, dB
/20);
148 // in order to not introduce computing overhead for nearly zero
149 // values of dB e.g. -0.01 or -0.001 we clamp to top
156 /*! \brief convert a scale factor to dB gain for audio manipulation
157 Inverts GainToScale result
158 \param the scale factor (equivalent to a voltage multiplier).
159 \return dB the gain in decibels.
162 static inline float ScaleToGain(const float scale
)
164 return 20*log10(scale
);
167 #if defined(HAVE_SSE) && defined(__SSE__)
168 static void SSEMulArray (float *data
, const float mul
, uint32_t count
);
169 static void SSEMulAddArray (float *data
, float *add
, const float mul
, uint32_t count
);
171 static void ClampArray(float *data
, uint32_t count
);
173 static bool S16NeedsByteSwap(AEDataFormat in
, AEDataFormat out
);
175 static uint64_t GetAVChannelLayout(const CAEChannelInfo
&info
);
176 static CAEChannelInfo
GetAEChannelLayout(uint64_t layout
);
177 static AVSampleFormat
GetAVSampleFormat(AEDataFormat format
);
178 static uint64_t GetAVChannelMask(enum AEChannel aechannel
);
179 static enum AVChannel
GetAVChannel(enum AEChannel aechannel
);
180 static int GetAVChannelIndex(enum AEChannel aechannel
, uint64_t layout
);