1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
#include "config.h"
#include "bformatdec.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <utility>
#include "almalloc.h"
#include "ambdec.h"
#include "filters/splitter.h"
#include "front_stablizer.h"
#include "math_defs.h"
#include "mixer.h"
#include "opthelpers.h"
namespace {
inline auto& GetAmbiScales(AmbDecScale scaletype) noexcept
{
if(scaletype == AmbDecScale::FuMa) return AmbiScale::FromFuMa();
if(scaletype == AmbDecScale::SN3D) return AmbiScale::FromSN3D();
return AmbiScale::FromN3D();
}
} // namespace
BFormatDec::BFormatDec(const AmbDecConf *conf, const bool allow_2band, const size_t inchans,
const uint srate, const uint (&chanmap)[MAX_OUTPUT_CHANNELS],
std::unique_ptr<FrontStablizer> stablizer)
: mStablizer{std::move(stablizer)}, mDualBand{allow_2band && (conf->FreqBands == 2)}
, mChannelDec{inchans}
{
const bool periphonic{(conf->ChanMask&AmbiPeriphonicMask) != 0};
auto&& coeff_scale = GetAmbiScales(conf->CoeffScale);
if(!mDualBand)
{
for(size_t j{0},k{0};j < mChannelDec.size();++j)
{
const size_t acn{periphonic ? j : AmbiIndex::FromACN2D()[j]};
if(!(conf->ChanMask&(1u<<acn))) continue;
const size_t order{AmbiIndex::OrderFromChannel()[acn]};
const float gain{conf->HFOrderGain[order] / coeff_scale[acn]};
for(size_t i{0u};i < conf->NumSpeakers;++i)
{
const size_t chanidx{chanmap[i]};
mChannelDec[j].mGains.Single[chanidx] = conf->Matrix[i][k] * gain;
}
++k;
}
}
else
{
mChannelDec[0].mXOver.init(conf->XOverFreq / static_cast<float>(srate));
for(size_t j{1};j < mChannelDec.size();++j)
mChannelDec[j].mXOver = mChannelDec[0].mXOver;
const float ratio{std::pow(10.0f, conf->XOverRatio / 40.0f)};
for(size_t j{0},k{0};j < mChannelDec.size();++j)
{
const size_t acn{periphonic ? j : AmbiIndex::FromACN2D()[j]};
if(!(conf->ChanMask&(1u<<acn))) continue;
const size_t order{AmbiIndex::OrderFromChannel()[acn]};
const float hfGain{conf->HFOrderGain[order] * ratio / coeff_scale[acn]};
const float lfGain{conf->LFOrderGain[order] / ratio / coeff_scale[acn]};
for(size_t i{0u};i < conf->NumSpeakers;++i)
{
const size_t chanidx{chanmap[i]};
mChannelDec[j].mGains.Dual[sHFBand][chanidx] = conf->HFMatrix[i][k] * hfGain;
mChannelDec[j].mGains.Dual[sLFBand][chanidx] = conf->LFMatrix[i][k] * lfGain;
}
++k;
}
}
}
BFormatDec::BFormatDec(const size_t inchans, const al::span<const ChannelDec> coeffs,
const al::span<const ChannelDec> coeffslf, std::unique_ptr<FrontStablizer> stablizer)
: mStablizer{std::move(stablizer)}, mDualBand{!coeffslf.empty()}, mChannelDec{inchans}
{
if(!mDualBand)
{
for(size_t j{0};j < mChannelDec.size();++j)
{
float *outcoeffs{mChannelDec[j].mGains.Single};
for(const ChannelDec &incoeffs : coeffs)
*(outcoeffs++) = incoeffs[j];
}
}
else
{
for(size_t j{0};j < mChannelDec.size();++j)
{
float *outcoeffs{mChannelDec[j].mGains.Dual[sHFBand]};
for(const ChannelDec &incoeffs : coeffs)
*(outcoeffs++) = incoeffs[j];
outcoeffs = mChannelDec[j].mGains.Dual[sLFBand];
for(const ChannelDec &incoeffs : coeffslf)
*(outcoeffs++) = incoeffs[j];
}
}
}
void BFormatDec::process(const al::span<FloatBufferLine> OutBuffer,
const FloatBufferLine *InSamples, const size_t SamplesToDo)
{
ASSUME(SamplesToDo > 0);
if(mDualBand)
{
const al::span<float> hfSamples{mSamples[sHFBand].data(), SamplesToDo};
const al::span<float> lfSamples{mSamples[sLFBand].data(), SamplesToDo};
for(auto &chandec : mChannelDec)
{
chandec.mXOver.process({InSamples->data(), SamplesToDo}, hfSamples.data(),
lfSamples.data());
MixSamples(hfSamples, OutBuffer, chandec.mGains.Dual[sHFBand],
chandec.mGains.Dual[sHFBand], 0, 0);
MixSamples(lfSamples, OutBuffer, chandec.mGains.Dual[sLFBand],
chandec.mGains.Dual[sLFBand], 0, 0);
++InSamples;
}
}
else
{
for(auto &chandec : mChannelDec)
{
MixSamples({InSamples->data(), SamplesToDo}, OutBuffer, chandec.mGains.Single,
chandec.mGains.Single, 0, 0);
++InSamples;
}
}
}
void BFormatDec::processStablize(const al::span<FloatBufferLine> OutBuffer,
const FloatBufferLine *InSamples, const size_t lidx, const size_t ridx, const size_t cidx,
const size_t SamplesToDo)
{
ASSUME(SamplesToDo > 0);
/* Move the existing direct L/R signal out so it doesn't get processed by
* the stablizer. Add a delay to it so it stays aligned with the stablizer
* delay.
*/
float *RESTRICT mid{al::assume_aligned<16>(mStablizer->MidDirect.data())};
float *RESTRICT side{al::assume_aligned<16>(mStablizer->Side.data())};
for(size_t i{0};i < SamplesToDo;++i)
{
mid[FrontStablizer::DelayLength+i] = OutBuffer[lidx][i] + OutBuffer[ridx][i];
side[FrontStablizer::DelayLength+i] = OutBuffer[lidx][i] - OutBuffer[ridx][i];
}
std::fill_n(OutBuffer[lidx].begin(), SamplesToDo, 0.0f);
std::fill_n(OutBuffer[ridx].begin(), SamplesToDo, 0.0f);
/* Decode the B-Format input to OutBuffer. */
process(OutBuffer, InSamples, SamplesToDo);
/* Apply a delay to all channels, except the front-left and front-right, so
* they maintain correct timing.
*/
const size_t NumChannels{OutBuffer.size()};
for(size_t i{0u};i < NumChannels;i++)
{
if(i == lidx || i == ridx)
continue;
auto &DelayBuf = mStablizer->DelayBuf[i];
auto buffer_end = OutBuffer[i].begin() + SamplesToDo;
if LIKELY(SamplesToDo >= FrontStablizer::DelayLength)
{
auto delay_end = std::rotate(OutBuffer[i].begin(),
buffer_end - FrontStablizer::DelayLength, buffer_end);
std::swap_ranges(OutBuffer[i].begin(), delay_end, DelayBuf.begin());
}
else
{
auto delay_start = std::swap_ranges(OutBuffer[i].begin(), buffer_end,
DelayBuf.begin());
std::rotate(DelayBuf.begin(), delay_start, DelayBuf.end());
}
}
/* Include the side signal for what was just decoded. */
for(size_t i{0};i < SamplesToDo;++i)
side[FrontStablizer::DelayLength+i] += OutBuffer[lidx][i] - OutBuffer[ridx][i];
/* Combine the delayed mid signal with the decoded mid signal. Note that
* the samples are stored and combined in reverse, so the newest samples
* are at the front and the oldest at the back.
*/
al::span<float> tmpbuf{mStablizer->TempBuf.data(), SamplesToDo+FrontStablizer::DelayLength};
auto tmpiter = tmpbuf.begin() + SamplesToDo;
std::copy(mStablizer->MidDelay.cbegin(), mStablizer->MidDelay.cend(), tmpiter);
for(size_t i{0};i < SamplesToDo;++i)
*--tmpiter = OutBuffer[lidx][i] + OutBuffer[ridx][i];
/* Save the newest samples for next time. */
std::copy_n(tmpbuf.cbegin(), mStablizer->MidDelay.size(), mStablizer->MidDelay.begin());
/* Apply an all-pass on the reversed signal, then reverse the samples to
* get the forward signal with a reversed phase shift. The future samples
* are included with the all-pass to reduce the error in the output
* samples (the smaller the delay, the more error is introduced).
*/
mStablizer->MidFilter.applyAllpass(tmpbuf);
tmpbuf = tmpbuf.subspan<FrontStablizer::DelayLength>();
std::reverse(tmpbuf.begin(), tmpbuf.end());
/* Now apply the band-splitter, combining its phase shift with the reversed
* phase shift, restoring the original phase on the split signal.
*/
mStablizer->MidFilter.process(tmpbuf, mStablizer->MidHF.data(), mStablizer->MidLF.data());
/* This pans the separate low- and high-frequency signals between being on
* the center channel and the left+right channels. The low-frequency signal
* is panned 1/3rd toward center and the high-frequency signal is panned
* 1/4th toward center. These values can be tweaked.
*/
const float cos_lf{std::cos(1.0f/3.0f * (al::MathDefs<float>::Pi()*0.5f))};
const float cos_hf{std::cos(1.0f/4.0f * (al::MathDefs<float>::Pi()*0.5f))};
const float sin_lf{std::sin(1.0f/3.0f * (al::MathDefs<float>::Pi()*0.5f))};
const float sin_hf{std::sin(1.0f/4.0f * (al::MathDefs<float>::Pi()*0.5f))};
for(size_t i{0};i < SamplesToDo;i++)
{
const float m{mStablizer->MidLF[i]*cos_lf + mStablizer->MidHF[i]*cos_hf + mid[i]};
const float c{mStablizer->MidLF[i]*sin_lf + mStablizer->MidHF[i]*sin_hf};
const float s{side[i]};
/* The generated center channel signal adds to the existing signal,
* while the modified left and right channels replace.
*/
OutBuffer[lidx][i] = (m + s) * 0.5f;
OutBuffer[ridx][i] = (m - s) * 0.5f;
OutBuffer[cidx][i] += c * 0.5f;
}
/* Move the delayed mid/side samples to the front for next time. */
auto mid_end = mStablizer->MidDirect.cbegin() + SamplesToDo;
std::copy(mid_end, mid_end+FrontStablizer::DelayLength, mStablizer->MidDirect.begin());
auto side_end = mStablizer->Side.cbegin() + SamplesToDo;
std::copy(side_end, side_end+FrontStablizer::DelayLength, mStablizer->Side.begin());
}
std::unique_ptr<BFormatDec> BFormatDec::Create(const AmbDecConf *conf, const bool allow_2band,
const size_t inchans, const uint srate, const uint (&chanmap)[MAX_OUTPUT_CHANNELS],
std::unique_ptr<FrontStablizer> stablizer)
{
return std::unique_ptr<BFormatDec>{new(FamCount(inchans))
BFormatDec{conf, allow_2band, inchans, srate, chanmap, std::move(stablizer)}};
}
std::unique_ptr<BFormatDec> BFormatDec::Create(const size_t inchans,
const al::span<const ChannelDec> coeffs, const al::span<const ChannelDec> coeffslf,
std::unique_ptr<FrontStablizer> stablizer)
{
return std::unique_ptr<BFormatDec>{new(FamCount(inchans))
BFormatDec{inchans, coeffs, coeffslf, std::move(stablizer)}};
}
|