🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.5 KiB

  1. #include "config.h"
  2. #include <cmath>
  3. #include <array>
  4. #include <vector>
  5. #include <numeric>
  6. #include <algorithm>
  7. #include <functional>
  8. #include "bformatdec.h"
  9. #include "ambdec.h"
  10. #include "filters/splitter.h"
  11. #include "alu.h"
  12. #include "threads.h"
  13. #include "almalloc.h"
  14. namespace {
  15. using namespace std::placeholders;
  16. constexpr ALfloat Ambi3DDecoderHFScale[MAX_AMBI_ORDER+1] = {
  17. 1.00000000e+00f, 1.00000000e+00f
  18. };
  19. constexpr ALfloat Ambi3DDecoderHFScale2O[MAX_AMBI_ORDER+1] = {
  20. 7.45355990e-01f, 1.00000000e+00f
  21. };
  22. constexpr ALfloat Ambi3DDecoderHFScale3O[MAX_AMBI_ORDER+1] = {
  23. 5.89792205e-01f, 8.79693856e-01f
  24. };
  25. inline auto GetDecoderHFScales(ALsizei order) noexcept -> const ALfloat(&)[MAX_AMBI_ORDER+1]
  26. {
  27. if(order >= 3) return Ambi3DDecoderHFScale3O;
  28. if(order == 2) return Ambi3DDecoderHFScale2O;
  29. return Ambi3DDecoderHFScale;
  30. }
  31. inline auto GetAmbiScales(AmbDecScale scaletype) noexcept -> const std::array<float,MAX_AMBI_CHANNELS>&
  32. {
  33. if(scaletype == AmbDecScale::FuMa) return AmbiScale::FromFuMa;
  34. if(scaletype == AmbDecScale::SN3D) return AmbiScale::FromSN3D;
  35. return AmbiScale::FromN3D;
  36. }
  37. } // namespace
  38. BFormatDec::BFormatDec(const AmbDecConf *conf, const bool allow_2band, const ALsizei inchans,
  39. const ALuint srate, const ALsizei (&chanmap)[MAX_OUTPUT_CHANNELS])
  40. {
  41. mDualBand = allow_2band && (conf->FreqBands == 2);
  42. if(!mDualBand)
  43. mSamples.resize(2);
  44. else
  45. {
  46. ASSUME(inchans > 0);
  47. mSamples.resize(inchans * 2);
  48. mSamplesHF = mSamples.data();
  49. mSamplesLF = mSamplesHF + inchans;
  50. }
  51. mNumChannels = inchans;
  52. mEnabled = std::accumulate(std::begin(chanmap), std::begin(chanmap)+conf->Speakers.size(), 0u,
  53. [](ALuint mask, const ALsizei &chan) noexcept -> ALuint
  54. { return mask | (1 << chan); }
  55. );
  56. const ALfloat xover_norm{conf->XOverFreq / static_cast<float>(srate)};
  57. const bool periphonic{(conf->ChanMask&AMBI_PERIPHONIC_MASK) != 0};
  58. const std::array<float,MAX_AMBI_CHANNELS> &coeff_scale = GetAmbiScales(conf->CoeffScale);
  59. const size_t coeff_count{periphonic ? MAX_AMBI_CHANNELS : MAX_AMBI2D_CHANNELS};
  60. if(!mDualBand)
  61. {
  62. for(size_t i{0u};i < conf->Speakers.size();i++)
  63. {
  64. ALfloat (&mtx)[MAX_AMBI_CHANNELS] = mMatrix.Single[chanmap[i]];
  65. for(size_t j{0},k{0};j < coeff_count;j++)
  66. {
  67. const size_t l{periphonic ? j : AmbiIndex::From2D[j]};
  68. if(!(conf->ChanMask&(1u<<l))) continue;
  69. mtx[j] = conf->HFMatrix[i][k] / coeff_scale[l] *
  70. ((l>=9) ? conf->HFOrderGain[3] :
  71. (l>=4) ? conf->HFOrderGain[2] :
  72. (l>=1) ? conf->HFOrderGain[1] : conf->HFOrderGain[0]);
  73. ++k;
  74. }
  75. }
  76. }
  77. else
  78. {
  79. mXOver[0].init(xover_norm);
  80. std::fill(std::begin(mXOver)+1, std::end(mXOver), mXOver[0]);
  81. const float ratio{std::pow(10.0f, conf->XOverRatio / 40.0f)};
  82. for(size_t i{0u};i < conf->Speakers.size();i++)
  83. {
  84. ALfloat (&mtx)[sNumBands][MAX_AMBI_CHANNELS] = mMatrix.Dual[chanmap[i]];
  85. for(size_t j{0},k{0};j < coeff_count;j++)
  86. {
  87. const size_t l{periphonic ? j : AmbiIndex::From2D[j]};
  88. if(!(conf->ChanMask&(1u<<l))) continue;
  89. mtx[sHFBand][j] = conf->HFMatrix[i][k] / coeff_scale[l] *
  90. ((l>=9) ? conf->HFOrderGain[3] :
  91. (l>=4) ? conf->HFOrderGain[2] :
  92. (l>=1) ? conf->HFOrderGain[1] : conf->HFOrderGain[0]) * ratio;
  93. mtx[sLFBand][j] = conf->LFMatrix[i][k] / coeff_scale[l] *
  94. ((l>=9) ? conf->LFOrderGain[3] :
  95. (l>=4) ? conf->LFOrderGain[2] :
  96. (l>=1) ? conf->LFOrderGain[1] : conf->LFOrderGain[0]) / ratio;
  97. ++k;
  98. }
  99. }
  100. }
  101. }
  102. BFormatDec::BFormatDec(const ALsizei inchans, const ALsizei chancount,
  103. const ChannelDec (&chancoeffs)[MAX_OUTPUT_CHANNELS],
  104. const ALsizei (&chanmap)[MAX_OUTPUT_CHANNELS])
  105. {
  106. mSamples.resize(2);
  107. mNumChannels = inchans;
  108. ASSUME(chancount > 0);
  109. mEnabled = std::accumulate(std::begin(chanmap), std::begin(chanmap)+chancount, 0u,
  110. [](ALuint mask, const ALsizei &chan) noexcept -> ALuint
  111. { return mask | (1 << chan); }
  112. );
  113. const ChannelDec *incoeffs{chancoeffs};
  114. auto set_coeffs = [this,inchans,&incoeffs](const ALsizei chanidx) noexcept -> void
  115. {
  116. ASSUME(chanidx >= 0);
  117. ALfloat (&mtx)[MAX_AMBI_CHANNELS] = mMatrix.Single[chanidx];
  118. const ALfloat (&coeffs)[MAX_AMBI_CHANNELS] = *(incoeffs++);
  119. ASSUME(inchans > 0);
  120. std::copy_n(std::begin(coeffs), inchans, std::begin(mtx));
  121. };
  122. std::for_each(chanmap, chanmap+chancount, set_coeffs);
  123. }
  124. void BFormatDec::process(ALfloat (*OutBuffer)[BUFFERSIZE], const ALsizei OutChannels, const ALfloat (*InSamples)[BUFFERSIZE], const ALsizei SamplesToDo)
  125. {
  126. ASSUME(OutChannels > 0);
  127. ASSUME(mNumChannels > 0);
  128. if(mDualBand)
  129. {
  130. for(ALsizei i{0};i < mNumChannels;i++)
  131. mXOver[i].process(mSamplesHF[i].data(), mSamplesLF[i].data(), InSamples[i],
  132. SamplesToDo);
  133. for(ALsizei chan{0};chan < OutChannels;chan++)
  134. {
  135. if(UNLIKELY(!(mEnabled&(1<<chan))))
  136. continue;
  137. MixRowSamples(OutBuffer[chan], mMatrix.Dual[chan][sHFBand],
  138. &reinterpret_cast<ALfloat(&)[BUFFERSIZE]>(mSamplesHF[0]),
  139. mNumChannels, 0, SamplesToDo);
  140. MixRowSamples(OutBuffer[chan], mMatrix.Dual[chan][sLFBand],
  141. &reinterpret_cast<ALfloat(&)[BUFFERSIZE]>(mSamplesLF[0]),
  142. mNumChannels, 0, SamplesToDo);
  143. }
  144. }
  145. else
  146. {
  147. for(ALsizei chan{0};chan < OutChannels;chan++)
  148. {
  149. if(UNLIKELY(!(mEnabled&(1<<chan))))
  150. continue;
  151. MixRowSamples(OutBuffer[chan], mMatrix.Single[chan], InSamples,
  152. mNumChannels, 0, SamplesToDo);
  153. }
  154. }
  155. }
  156. std::array<ALfloat,MAX_AMBI_ORDER+1> BFormatDec::GetHFOrderScales(const ALsizei in_order, const ALsizei out_order) noexcept
  157. {
  158. std::array<ALfloat,MAX_AMBI_ORDER+1> ret{};
  159. assert(out_order >= in_order);
  160. ASSUME(out_order >= in_order);
  161. const ALfloat (&target)[MAX_AMBI_ORDER+1] = GetDecoderHFScales(out_order);
  162. const ALfloat (&input)[MAX_AMBI_ORDER+1] = GetDecoderHFScales(in_order);
  163. for(ALsizei i{0};i < in_order+1;++i)
  164. ret[i] = input[i] / target[i];
  165. return ret;
  166. }