[videodb] remove unused seasons table from episode_view
[xbmc.git] / xbmc / cores / AudioEngine / Utils / AEUtil.h
blob65a846e04402da312463652ad4da9bb3587360cf
1 /*
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.
7 */
9 #pragma once
11 #include "AEAudioFormat.h"
13 #include <atomic>
14 #include <math.h>
16 #include "PlatformDefs.h"
18 extern "C" {
19 #include <libavutil/channel_layout.h>
20 #include <libavutil/samplefmt.h>
23 // AV sync options
24 enum AVSync
26 SYNC_DISCON = 0,
27 SYNC_RESAMPLE
30 struct AEDelayStatus
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
40 /**
41 * @brief lockless consistency guaranteeer
43 * Requires write to be a higher priority thread
45 * use in writer:
46 * m_locker.enter();
47 * update_stuff();
48 * m_locker.leave();
50 * use in reader:
51 * CAESpinLock lock(m_locker);
52 * do {
53 * get_stuff();
54 * } while(lock.retry());
57 class CAESpinSection
59 public:
60 void enter() { m_enter++; }
61 void leave() { m_leave.store(m_enter); }
63 protected:
64 friend class CAESpinLock;
65 std::atomic_uint32_t m_enter = 0;
66 std::atomic_uint32_t m_leave = 0;
69 class CAESpinLock
71 public:
72 explicit CAESpinLock(CAESpinSection& section)
73 : m_section(section)
74 , m_begin(section.m_enter)
77 bool retry()
79 if(m_section.m_enter != m_begin
80 || m_section.m_enter != m_section.m_leave)
82 m_begin = m_section.m_enter;
83 return true;
85 else
86 return false;
89 private:
90 CAESpinSection& m_section;
91 unsigned int m_begin;
94 class CAEUtil
96 private:
98 static float SoftClamp(const float x);
100 public:
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.
114 \sa GainToScale
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
127 \sa ScaleToGain
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).
139 \sa PercentToGain
141 static inline float GainToScale(const float dB)
143 float val = 0.0f;
144 // we need to make sure that our lowest db returns plain zero
145 if (dB > -60.0f)
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
150 if (val >= 0.99f)
151 val = 1.0f;
153 return val;
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.
160 \sa GainToScale
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);
170 #endif
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);