4 #include "bformatdec.h"
11 typedef struct BandSplitter
{
18 static void bandsplit_init(BandSplitter
*splitter
, ALfloat freq_mult
)
20 ALfloat w
= freq_mult
* F_TAU
;
23 splitter
->coeff
= (sinf(w
) - 1.0f
) / cw
;
25 splitter
->coeff
= cw
* -0.5f
;
27 splitter
->lp_z1
= 0.0f
;
28 splitter
->lp_z2
= 0.0f
;
29 splitter
->hp_z1
= 0.0f
;
32 static void bandsplit_process(BandSplitter
*splitter
, ALfloat
*restrict hpout
, ALfloat
*restrict lpout
,
33 const ALfloat
*input
, ALuint count
)
38 coeff
= splitter
->coeff
*0.5f
+ 0.5f
;
39 for(i
= 0;i
< count
;i
++)
43 d
= (x
- splitter
->lp_z1
) * coeff
;
44 x
= splitter
->lp_z1
+ d
;
45 splitter
->lp_z1
= x
+ d
;
47 d
= (x
- splitter
->lp_z2
) * coeff
;
48 x
= splitter
->lp_z2
+ d
;
49 splitter
->lp_z2
= x
+ d
;
54 coeff
= splitter
->coeff
;
55 for(i
= 0;i
< count
;i
++)
59 d
= x
- coeff
*splitter
->hp_z1
;
60 x
= splitter
->hp_z1
+ coeff
*d
;
63 hpout
[i
] = x
- lpout
[i
];
68 static const ALfloat UnitScale
[MAX_AMBI_COEFFS
] = {
69 1.0f
, 1.0f
, 1.0f
, 1.0f
, 1.0f
, 1.0f
, 1.0f
, 1.0f
,
70 1.0f
, 1.0f
, 1.0f
, 1.0f
, 1.0f
, 1.0f
, 1.0f
, 1.0f
72 static const ALfloat SN3D2N3DScale
[MAX_AMBI_COEFFS
] = {
73 1.000000000f
, /* ACN 0 (W), sqrt(1) */
74 1.732050808f
, /* ACN 1 (Y), sqrt(3) */
75 1.732050808f
, /* ACN 2 (Z), sqrt(3) */
76 1.732050808f
, /* ACN 3 (X), sqrt(3) */
77 2.236067978f
, /* ACN 4 (V), sqrt(5) */
78 2.236067978f
, /* ACN 5 (T), sqrt(5) */
79 2.236067978f
, /* ACN 6 (R), sqrt(5) */
80 2.236067978f
, /* ACN 7 (S), sqrt(5) */
81 2.236067978f
, /* ACN 8 (U), sqrt(5) */
82 2.645751311f
, /* ACN 9 (Q), sqrt(7) */
83 2.645751311f
, /* ACN 10 (O), sqrt(7) */
84 2.645751311f
, /* ACN 11 (M), sqrt(7) */
85 2.645751311f
, /* ACN 12 (K), sqrt(7) */
86 2.645751311f
, /* ACN 13 (L), sqrt(7) */
87 2.645751311f
, /* ACN 14 (N), sqrt(7) */
88 2.645751311f
, /* ACN 15 (P), sqrt(7) */
90 static const ALfloat FuMa2N3DScale
[MAX_AMBI_COEFFS
] = {
91 1.414213562f
, /* ACN 0 (W), sqrt(2) */
92 1.732050808f
, /* ACN 1 (Y), sqrt(3) */
93 1.732050808f
, /* ACN 2 (Z), sqrt(3) */
94 1.732050808f
, /* ACN 3 (X), sqrt(3) */
95 1.936491673f
, /* ACN 4 (V), sqrt(15)/2 */
96 1.936491673f
, /* ACN 5 (T), sqrt(15)/2 */
97 2.236067978f
, /* ACN 6 (R), sqrt(5) */
98 1.936491673f
, /* ACN 7 (S), sqrt(15)/2 */
99 1.936491673f
, /* ACN 8 (U), sqrt(15)/2 */
100 2.091650066f
, /* ACN 9 (Q), sqrt(35/8) */
101 1.972026594f
, /* ACN 10 (O), sqrt(35)/3 */
102 2.231093404f
, /* ACN 11 (M), sqrt(224/45) */
103 2.645751311f
, /* ACN 12 (K), sqrt(7) */
104 2.231093404f
, /* ACN 13 (L), sqrt(224/45) */
105 1.972026594f
, /* ACN 14 (N), sqrt(35)/3 */
106 2.091650066f
, /* ACN 15 (P), sqrt(35/8) */
110 static const ALfloat SquareMatrixHF
[4][MAX_AMBI_COEFFS
] = {
111 { 0.353553f
, 0.204094f
, 0.0f
, 0.204094f
},
112 { 0.353553f
, -0.204094f
, 0.0f
, 0.204094f
},
113 { 0.353553f
, 0.204094f
, 0.0f
, -0.204094f
},
114 { 0.353553f
, -0.204094f
, 0.0f
, -0.204094f
},
116 static ALfloat SquareEncoder
[4][MAX_AMBI_COEFFS
];
118 static const ALfloat CubeMatrixHF
[8][MAX_AMBI_COEFFS
] = {
119 { 0.25f
, 0.14425f
, 0.14425f
, 0.14425f
},
120 { 0.25f
, -0.14425f
, 0.14425f
, 0.14425f
},
121 { 0.25f
, 0.14425f
, 0.14425f
, -0.14425f
},
122 { 0.25f
, -0.14425f
, 0.14425f
, -0.14425f
},
123 { 0.25f
, 0.14425f
, -0.14425f
, 0.14425f
},
124 { 0.25f
, -0.14425f
, -0.14425f
, 0.14425f
},
125 { 0.25f
, 0.14425f
, -0.14425f
, -0.14425f
},
126 { 0.25f
, -0.14425f
, -0.14425f
, -0.14425f
},
128 static ALfloat CubeEncoder
[8][MAX_AMBI_COEFFS
];
130 static alonce_flag encoder_inited
= AL_ONCE_FLAG_INIT
;
132 static void init_encoder(void)
134 CalcXYZCoeffs(-0.577350269f
, 0.577350269f
, -0.577350269f
, CubeEncoder
[0]);
135 CalcXYZCoeffs( 0.577350269f
, 0.577350269f
, -0.577350269f
, CubeEncoder
[1]);
136 CalcXYZCoeffs(-0.577350269f
, 0.577350269f
, 0.577350269f
, CubeEncoder
[2]);
137 CalcXYZCoeffs( 0.577350269f
, 0.577350269f
, 0.577350269f
, CubeEncoder
[3]);
138 CalcXYZCoeffs(-0.577350269f
, -0.577350269f
, -0.577350269f
, CubeEncoder
[4]);
139 CalcXYZCoeffs( 0.577350269f
, -0.577350269f
, -0.577350269f
, CubeEncoder
[5]);
140 CalcXYZCoeffs(-0.577350269f
, -0.577350269f
, 0.577350269f
, CubeEncoder
[6]);
141 CalcXYZCoeffs( 0.577350269f
, -0.577350269f
, 0.577350269f
, CubeEncoder
[7]);
143 CalcXYZCoeffs(-0.707106781f
, 0.0f
, -0.707106781f
, SquareEncoder
[0]);
144 CalcXYZCoeffs( 0.707106781f
, 0.0f
, -0.707106781f
, SquareEncoder
[1]);
145 CalcXYZCoeffs(-0.707106781f
, 0.0f
, 0.707106781f
, SquareEncoder
[2]);
146 CalcXYZCoeffs( 0.707106781f
, 0.0f
, 0.707106781f
, SquareEncoder
[3]);
150 /* NOTE: Low-frequency (LF) fields and BandSplitter filters are unused with
151 * single-band decoding
153 typedef struct BFormatDec
{
154 alignas(16) ALfloat MatrixHF
[MAX_OUTPUT_CHANNELS
][MAX_AMBI_COEFFS
];
155 alignas(16) ALfloat MatrixLF
[MAX_OUTPUT_CHANNELS
][MAX_AMBI_COEFFS
];
157 BandSplitter XOver
[MAX_AMBI_COEFFS
];
159 ALfloat (*Samples
)[BUFFERSIZE
];
160 /* These two alias into Samples */
161 ALfloat (*SamplesHF
)[BUFFERSIZE
];
162 ALfloat (*SamplesLF
)[BUFFERSIZE
];
165 const ALfloat (*restrict MatrixHF
)[MAX_AMBI_COEFFS
];
166 const ALfloat (*restrict Encoder
)[MAX_AMBI_COEFFS
];
174 BFormatDec
*bformatdec_alloc()
176 alcall_once(&encoder_inited
, init_encoder
);
177 return al_calloc(16, sizeof(BFormatDec
));
180 void bformatdec_free(BFormatDec
*dec
)
184 al_free(dec
->Samples
);
186 dec
->SamplesHF
= NULL
;
187 dec
->SamplesLF
= NULL
;
189 memset(dec
, 0, sizeof(*dec
));
194 int bformatdec_getOrder(const struct BFormatDec
*dec
)
196 if(dec
->NumChannels
> 9) return 3;
197 if(dec
->NumChannels
> 4) return 2;
198 if(dec
->NumChannels
> 1) return 1;
202 void bformatdec_reset(BFormatDec
*dec
, const AmbDecConf
*conf
, ALuint chancount
, ALuint srate
, const ALuint chanmap
[MAX_OUTPUT_CHANNELS
])
204 const ALfloat
*coeff_scale
= UnitScale
;
208 al_free(dec
->Samples
);
210 dec
->SamplesHF
= NULL
;
211 dec
->SamplesLF
= NULL
;
213 dec
->NumChannels
= chancount
;
214 dec
->Samples
= al_calloc(16, dec
->NumChannels
* conf
->FreqBands
*
215 sizeof(dec
->Samples
[0]));
216 dec
->SamplesHF
= dec
->Samples
;
217 dec
->SamplesLF
= dec
->SamplesHF
+ dec
->NumChannels
;
219 if(conf
->CoeffScale
== ADS_SN3D
)
220 coeff_scale
= SN3D2N3DScale
;
221 else if(conf
->CoeffScale
== ADS_FuMa
)
222 coeff_scale
= FuMa2N3DScale
;
224 if((conf
->ChanMask
& ~0x831b))
226 dec
->UpSampler
.MatrixHF
= CubeMatrixHF
;
227 dec
->UpSampler
.Encoder
= (const ALfloat(*)[MAX_AMBI_COEFFS
])CubeEncoder
;
228 dec
->UpSampler
.NumChannels
= 8;
232 dec
->UpSampler
.MatrixHF
= SquareMatrixHF
;
233 dec
->UpSampler
.Encoder
= (const ALfloat(*)[MAX_AMBI_COEFFS
])SquareEncoder
;
234 dec
->UpSampler
.NumChannels
= 4;
237 if(conf
->FreqBands
== 1)
239 dec
->DualBand
= AL_FALSE
;
244 dec
->DualBand
= AL_TRUE
;
246 ratio
= conf
->XOverFreq
/ (ALfloat
)srate
;
247 for(i
= 0;i
< MAX_AMBI_COEFFS
;i
++)
248 bandsplit_init(&dec
->XOver
[i
], ratio
);
250 ratio
= powf(10.0f
, conf
->XOverRatio
/ 40.0f
);
251 memset(dec
->MatrixLF
, 0, sizeof(dec
->MatrixLF
));
252 for(i
= 0;i
< conf
->NumSpeakers
;i
++)
254 ALuint chan
= chanmap
[i
];
258 for(j
= 0;j
< MAX_AMBI_COEFFS
;j
++)
260 if(j
== 0) gain
= conf
->LFOrderGain
[0] / ratio
;
261 else if(j
== 1) gain
= conf
->LFOrderGain
[1] / ratio
;
262 else if(j
== 4) gain
= conf
->LFOrderGain
[2] / ratio
;
263 else if(j
== 9) gain
= conf
->LFOrderGain
[3] / ratio
;
264 if((conf
->ChanMask
&(1<<j
)))
265 dec
->MatrixLF
[chan
][j
] = conf
->LFMatrix
[i
][k
++] / coeff_scale
[j
] * gain
;
270 memset(dec
->MatrixHF
, 0, sizeof(dec
->MatrixHF
));
271 for(i
= 0;i
< conf
->NumSpeakers
;i
++)
273 ALuint chan
= chanmap
[i
];
277 for(j
= 0;j
< MAX_AMBI_COEFFS
;j
++)
279 if(j
== 0) gain
= conf
->HFOrderGain
[0] * ratio
;
280 else if(j
== 1) gain
= conf
->HFOrderGain
[1] * ratio
;
281 else if(j
== 4) gain
= conf
->HFOrderGain
[2] * ratio
;
282 else if(j
== 9) gain
= conf
->HFOrderGain
[3] * ratio
;
283 if((conf
->ChanMask
&(1<<j
)))
284 dec
->MatrixHF
[chan
][j
] = conf
->HFMatrix
[i
][k
++] / coeff_scale
[j
] * gain
;
290 static void apply_row(ALfloat
*out
, const ALfloat
*mtx
, ALfloat (*restrict in
)[BUFFERSIZE
], ALuint inchans
, ALuint todo
)
294 for(c
= 0;c
< inchans
;c
++)
296 ALfloat gain
= mtx
[c
];
297 if(!(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
))
299 for(i
= 0;i
< todo
;i
++)
300 out
[i
] += in
[c
][i
] * gain
;
304 void bformatdec_process(struct BFormatDec
*dec
, ALfloat (*restrict OutBuffer
)[BUFFERSIZE
], ALuint OutChannels
, ALfloat (*restrict InSamples
)[BUFFERSIZE
], ALuint SamplesToDo
)
310 for(i
= 0;i
< dec
->NumChannels
;i
++)
311 bandsplit_process(&dec
->XOver
[i
], dec
->SamplesHF
[i
], dec
->SamplesLF
[i
],
312 InSamples
[i
], SamplesToDo
);
314 for(chan
= 0;chan
< OutChannels
;chan
++)
316 apply_row(OutBuffer
[chan
], dec
->MatrixHF
[chan
], dec
->SamplesHF
,
317 dec
->NumChannels
, SamplesToDo
);
318 apply_row(OutBuffer
[chan
], dec
->MatrixLF
[chan
], dec
->SamplesLF
,
319 dec
->NumChannels
, SamplesToDo
);
324 for(chan
= 0;chan
< OutChannels
;chan
++)
325 apply_row(OutBuffer
[chan
], dec
->MatrixHF
[chan
], InSamples
,
326 dec
->NumChannels
, SamplesToDo
);
331 void bformatdec_upSample(struct BFormatDec
*dec
, ALfloat (*restrict OutBuffer
)[BUFFERSIZE
], ALfloat (*restrict InSamples
)[BUFFERSIZE
], ALuint InChannels
, ALuint SamplesToDo
)
335 /* This up-sampler is very simplistic. It essentially decodes the first-
336 * order content to a square channel array (or cube if height is desired),
337 * then encodes those points onto the higher order soundfield.
339 for(k
= 0;k
< dec
->UpSampler
.NumChannels
;k
++)
341 memset(dec
->Samples
[0], 0, SamplesToDo
*sizeof(ALfloat
));
342 apply_row(dec
->Samples
[0], dec
->UpSampler
.MatrixHF
[k
], InSamples
,
343 InChannels
, SamplesToDo
);
345 for(j
= 0;j
< dec
->NumChannels
;j
++)
347 ALfloat gain
= dec
->UpSampler
.Encoder
[k
][j
];
348 if(!(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
))
350 for(i
= 0;i
< SamplesToDo
;i
++)
351 OutBuffer
[j
][i
] += dec
->Samples
[0][i
] * gain
;