Use pw_buffer::requested in newer PipeWire versions
[openal-soft.git] / core / bformatdec.cpp
blob606093c07f04febad803d9f37d6fae90d9e3402f
2 #include "config.h"
4 #include "bformatdec.h"
6 #include <algorithm>
7 #include <array>
8 #include <cmath>
9 #include <utility>
11 #include "almalloc.h"
12 #include "alnumbers.h"
13 #include "filters/splitter.h"
14 #include "front_stablizer.h"
15 #include "mixer.h"
16 #include "opthelpers.h"
19 BFormatDec::BFormatDec(const size_t inchans, const al::span<const ChannelDec> coeffs,
20 const al::span<const ChannelDec> coeffslf, const float xover_f0norm,
21 std::unique_ptr<FrontStablizer> stablizer)
22 : mStablizer{std::move(stablizer)}, mDualBand{!coeffslf.empty()}, mChannelDec{inchans}
24 if(!mDualBand)
26 for(size_t j{0};j < mChannelDec.size();++j)
28 float *outcoeffs{mChannelDec[j].mGains.Single};
29 for(const ChannelDec &incoeffs : coeffs)
30 *(outcoeffs++) = incoeffs[j];
33 else
35 mChannelDec[0].mXOver.init(xover_f0norm);
36 for(size_t j{1};j < mChannelDec.size();++j)
37 mChannelDec[j].mXOver = mChannelDec[0].mXOver;
39 for(size_t j{0};j < mChannelDec.size();++j)
41 float *outcoeffs{mChannelDec[j].mGains.Dual[sHFBand]};
42 for(const ChannelDec &incoeffs : coeffs)
43 *(outcoeffs++) = incoeffs[j];
45 outcoeffs = mChannelDec[j].mGains.Dual[sLFBand];
46 for(const ChannelDec &incoeffs : coeffslf)
47 *(outcoeffs++) = incoeffs[j];
53 void BFormatDec::process(const al::span<FloatBufferLine> OutBuffer,
54 const FloatBufferLine *InSamples, const size_t SamplesToDo)
56 ASSUME(SamplesToDo > 0);
58 if(mDualBand)
60 const al::span<float> hfSamples{mSamples[sHFBand].data(), SamplesToDo};
61 const al::span<float> lfSamples{mSamples[sLFBand].data(), SamplesToDo};
62 for(auto &chandec : mChannelDec)
64 chandec.mXOver.process({InSamples->data(), SamplesToDo}, hfSamples.data(),
65 lfSamples.data());
66 MixSamples(hfSamples, OutBuffer, chandec.mGains.Dual[sHFBand],
67 chandec.mGains.Dual[sHFBand], 0, 0);
68 MixSamples(lfSamples, OutBuffer, chandec.mGains.Dual[sLFBand],
69 chandec.mGains.Dual[sLFBand], 0, 0);
70 ++InSamples;
73 else
75 for(auto &chandec : mChannelDec)
77 MixSamples({InSamples->data(), SamplesToDo}, OutBuffer, chandec.mGains.Single,
78 chandec.mGains.Single, 0, 0);
79 ++InSamples;
84 void BFormatDec::processStablize(const al::span<FloatBufferLine> OutBuffer,
85 const FloatBufferLine *InSamples, const size_t lidx, const size_t ridx, const size_t cidx,
86 const size_t SamplesToDo)
88 ASSUME(SamplesToDo > 0);
90 /* Move the existing direct L/R signal out so it doesn't get processed by
91 * the stablizer. Add a delay to it so it stays aligned with the stablizer
92 * delay.
94 float *RESTRICT mid{al::assume_aligned<16>(mStablizer->MidDirect.data())};
95 float *RESTRICT side{al::assume_aligned<16>(mStablizer->Side.data())};
96 for(size_t i{0};i < SamplesToDo;++i)
98 mid[FrontStablizer::DelayLength+i] = OutBuffer[lidx][i] + OutBuffer[ridx][i];
99 side[FrontStablizer::DelayLength+i] = OutBuffer[lidx][i] - OutBuffer[ridx][i];
101 std::fill_n(OutBuffer[lidx].begin(), SamplesToDo, 0.0f);
102 std::fill_n(OutBuffer[ridx].begin(), SamplesToDo, 0.0f);
104 /* Decode the B-Format input to OutBuffer. */
105 process(OutBuffer, InSamples, SamplesToDo);
107 /* Apply a delay to all channels, except the front-left and front-right, so
108 * they maintain correct timing.
110 const size_t NumChannels{OutBuffer.size()};
111 for(size_t i{0u};i < NumChannels;i++)
113 if(i == lidx || i == ridx)
114 continue;
116 auto &DelayBuf = mStablizer->DelayBuf[i];
117 auto buffer_end = OutBuffer[i].begin() + SamplesToDo;
118 if LIKELY(SamplesToDo >= FrontStablizer::DelayLength)
120 auto delay_end = std::rotate(OutBuffer[i].begin(),
121 buffer_end - FrontStablizer::DelayLength, buffer_end);
122 std::swap_ranges(OutBuffer[i].begin(), delay_end, DelayBuf.begin());
124 else
126 auto delay_start = std::swap_ranges(OutBuffer[i].begin(), buffer_end,
127 DelayBuf.begin());
128 std::rotate(DelayBuf.begin(), delay_start, DelayBuf.end());
132 /* Include the side signal for what was just decoded. */
133 for(size_t i{0};i < SamplesToDo;++i)
134 side[FrontStablizer::DelayLength+i] += OutBuffer[lidx][i] - OutBuffer[ridx][i];
136 /* Combine the delayed mid signal with the decoded mid signal. */
137 float *tmpbuf{mStablizer->TempBuf.data()};
138 auto tmpiter = std::copy(mStablizer->MidDelay.cbegin(), mStablizer->MidDelay.cend(), tmpbuf);
139 for(size_t i{0};i < SamplesToDo;++i,++tmpiter)
140 *tmpiter = OutBuffer[lidx][i] + OutBuffer[ridx][i];
141 /* Save the newest samples for next time. */
142 std::copy_n(tmpbuf+SamplesToDo, mStablizer->MidDelay.size(), mStablizer->MidDelay.begin());
144 /* Apply an all-pass on the signal in reverse. The future samples are
145 * included with the all-pass to reduce the error in the output samples
146 * (the smaller the delay, the more error is introduced).
148 mStablizer->MidFilter.applyAllpassRev({tmpbuf, SamplesToDo+FrontStablizer::DelayLength});
150 /* Now apply the band-splitter, combining its phase shift with the reversed
151 * phase shift, restoring the original phase on the split signal.
153 mStablizer->MidFilter.process({tmpbuf, SamplesToDo}, mStablizer->MidHF.data(),
154 mStablizer->MidLF.data());
156 /* This pans the separate low- and high-frequency signals between being on
157 * the center channel and the left+right channels. The low-frequency signal
158 * is panned 1/3rd toward center and the high-frequency signal is panned
159 * 1/4th toward center. These values can be tweaked.
161 const float cos_lf{std::cos(1.0f/3.0f * (al::numbers::pi_v<float>*0.5f))};
162 const float cos_hf{std::cos(1.0f/4.0f * (al::numbers::pi_v<float>*0.5f))};
163 const float sin_lf{std::sin(1.0f/3.0f * (al::numbers::pi_v<float>*0.5f))};
164 const float sin_hf{std::sin(1.0f/4.0f * (al::numbers::pi_v<float>*0.5f))};
165 for(size_t i{0};i < SamplesToDo;i++)
167 const float m{mStablizer->MidLF[i]*cos_lf + mStablizer->MidHF[i]*cos_hf + mid[i]};
168 const float c{mStablizer->MidLF[i]*sin_lf + mStablizer->MidHF[i]*sin_hf};
169 const float s{side[i]};
171 /* The generated center channel signal adds to the existing signal,
172 * while the modified left and right channels replace.
174 OutBuffer[lidx][i] = (m + s) * 0.5f;
175 OutBuffer[ridx][i] = (m - s) * 0.5f;
176 OutBuffer[cidx][i] += c * 0.5f;
178 /* Move the delayed mid/side samples to the front for next time. */
179 auto mid_end = mStablizer->MidDirect.cbegin() + SamplesToDo;
180 std::copy(mid_end, mid_end+FrontStablizer::DelayLength, mStablizer->MidDirect.begin());
181 auto side_end = mStablizer->Side.cbegin() + SamplesToDo;
182 std::copy(side_end, side_end+FrontStablizer::DelayLength, mStablizer->Side.begin());
186 std::unique_ptr<BFormatDec> BFormatDec::Create(const size_t inchans,
187 const al::span<const ChannelDec> coeffs, const al::span<const ChannelDec> coeffslf,
188 const float xover_f0norm, std::unique_ptr<FrontStablizer> stablizer)
190 return std::make_unique<BFormatDec>(inchans, coeffs, coeffslf, xover_f0norm,
191 std::move(stablizer));