Skip to content

Commit aa922cc

Browse files
committed
fix bug w/ duplicate sources w/ same filter
1 parent 470ba9b commit aa922cc

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

rematrix-filter.c

+42-41
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ struct rematrix_data {
4040
uint8_t *tmpbuffer[MAX_AV_PLANES];
4141
//ensure we can treat it as a dynamic array
4242
size_t size;
43+
44+
//initialize once, optimize for fast use
45+
volatile long long _ch_count[MAX_AV_PLANES];
46+
volatile double _gain[MAX_AV_PLANES];
47+
volatile float _mix[MAX_AV_PLANES][MAX_AV_PLANES];
48+
volatile double _true_gain[MAX_AV_PLANES];
49+
//static volatile uint32_t _options[MAX_AV_PLANES];
4350
};
4451

4552
/*****************************************************************************/
@@ -96,7 +103,7 @@ static void rematrix_update(void *data, obs_data_t *settings) {
96103
for (long long j = 0; j < MAX_AV_PLANES; j++) {
97104
sprintf(mix_name, mix_name_format, i, j);
98105
mix[i][j] = (float)obs_data_get_double(settings, mix_name) / SCALE;
99-
106+
100107
if (rematrix->mix[i][j] != mix[i][j]) {
101108
rematrix->mix[i][j] = mix[i][j];
102109
mix_changed = true;
@@ -148,13 +155,6 @@ static void *rematrix_create(obs_data_t *settings, obs_source_t *filter) {
148155
static struct obs_audio_data *rematrix_filter_audio(void *data,
149156
struct obs_audio_data *audio) {
150157

151-
//initialize once, optimize for fast use
152-
static volatile long long ch_count[MAX_AV_PLANES];
153-
static volatile double gain[MAX_AV_PLANES];
154-
static volatile float mix[MAX_AV_PLANES][MAX_AV_PLANES];
155-
static volatile double true_gain[MAX_AV_PLANES];
156-
static volatile uint32_t options[MAX_AV_PLANES];
157-
158158
struct rematrix_data *rematrix = data;
159159
const size_t channels = rematrix->channels;
160160
float **fmatrixed_data = (float**)rematrix->tmpbuffer;
@@ -163,21 +163,21 @@ static struct obs_audio_data *rematrix_filter_audio(void *data,
163163

164164
//prevent race condition
165165
for (size_t c = 0; c < channels; c++) {
166-
ch_count[c] = 0;
167-
gain[c] = rematrix->gain[c];
166+
rematrix->_ch_count[c] = 0;
167+
rematrix->_gain[c] = rematrix->gain[c];
168168
for (size_t c2 = 0; c2 < channels; c2++) {
169-
mix[c][c2] = rematrix->mix[c][c2];
169+
rematrix->_mix[c][c2] = rematrix->mix[c][c2];
170170
//use ch_count to "count" how many chs are in use
171171
//for normalization
172-
if (mix[c][c2] > 0)
173-
ch_count[c]++;
172+
if (rematrix->_mix[c][c2] > 0)
173+
rematrix->_ch_count[c]++;
174174
}
175175
//when ch_count == 0.0 float returns +inf, making this division by 0.0 actually safe
176-
if (ch_count[c] == 0.0) {
177-
true_gain[c] = 0.0;
176+
if (rematrix->_ch_count[c] == 0.0) {
177+
rematrix->_true_gain[c] = 0.0;
178178
}
179179
else {
180-
true_gain[c] = gain[c] / ch_count[c];
180+
rematrix->_true_gain[c] = rematrix->_gain[c] / rematrix->_ch_count[c];
181181
}
182182
}
183183

@@ -207,80 +207,81 @@ static struct obs_audio_data *rematrix_filter_audio(void *data,
207207
//reset to 0
208208
size_t c2 = 0;
209209
for (; c2 < 1; c2++) {
210-
if (fdata[c] && mix[c][c2]) {
210+
if (fdata[c] && rematrix->_mix[c][c2]) {
211211
size_t s = 0;
212212
size_t end;
213213
/*
214214
end = unprocessed_samples - 8;
215215
for(; s < end; s+=8) {
216-
s256 = _mm256_load_ps(&fdata[c2][chunk + s]);
217-
m256 = _mm256_set1_ps(mix[c][c2]);//_mm256_load_ps(&mix[c][c2]);
218-
_mm256_store_ps(&fmatrixed_data[c][s], _mm256_mul_ps(s256, m256));
216+
s256 = _mm256_load_ps(&fdata[c2][chunk + s]);
217+
m256 = _mm256_set1_ps(mix[c][c2]);//_mm256_load_ps(&mix[c][c2]);
218+
_mm256_store_ps(&fmatrixed_data[c][s], _mm256_mul_ps(s256, m256));
219219
}
220220
*/
221221
end = unprocessed_samples - 4;
222-
for(; s < end; s+=4) {
222+
for (; s < end; s += 4) {
223223
s128 = _mm_load_ps(&fdata[c2][chunk + s]);
224-
m128 = _mm_set1_ps(mix[c][c2]);//_mm256_load_ps(&mix[c][c2]);
224+
m128 = _mm_set1_ps(rematrix->_mix[c][c2]);//_mm256_load_ps(&mix[c][c2]);
225225
_mm_store_ps(&fmatrixed_data[c][s], _mm_mul_ps(s128, m128));
226226
}
227227
for (; s < unprocessed_samples; s++) {
228-
fmatrixed_data[c][s] = fdata[c2][chunk + s] * mix[c][c2];
228+
fmatrixed_data[c][s] = fdata[c2][chunk + s] * rematrix->_mix[c][c2];
229229
}
230-
} else {
230+
}
231+
else {
231232
memset(fmatrixed_data[c], 0, copy_size);
232233
}
233234
}
234235
//memset(fmatrixed_data[c], 0, copy_size);
235236
//add contributions
236237
for (; c2 < channels; c2++) {
237-
if (fdata[c] && mix[c][c2]) {
238+
if (fdata[c] && rematrix->_mix[c][c2]) {
238239
size_t s = 0;
239240
size_t end;
240241
/*
241242
end = unprocessed_samples - 8;
242243
for(; s < end; s+=8) {
243-
s256 = _mm256_load_ps(&fdata[c2][chunk + s]);
244-
t256 = _mm256_load_ps(&fmatrixed_data[c][s]);
245-
m256 = _mm256_set1_ps(mix[c][c2]);//_mm256_load_ps(&mix[c][c2]);
246-
_mm256_store_ps(&fmatrixed_data[c][s], _mm256_add_ps(t256, _mm256_mul_ps(s256, m256)));
244+
s256 = _mm256_load_ps(&fdata[c2][chunk + s]);
245+
t256 = _mm256_load_ps(&fmatrixed_data[c][s]);
246+
m256 = _mm256_set1_ps(mix[c][c2]);//_mm256_load_ps(&mix[c][c2]);
247+
_mm256_store_ps(&fmatrixed_data[c][s], _mm256_add_ps(t256, _mm256_mul_ps(s256, m256)));
247248
}
248249
*/
249250
end = unprocessed_samples - 4;
250-
for(; s < end; s+=4) {
251+
for (; s < end; s += 4) {
251252
s128 = _mm_load_ps(&fdata[c2][chunk + s]);
252253
t128 = _mm_load_ps(&fmatrixed_data[c][s]);
253-
m128 = _mm_set1_ps(mix[c][c2]);//_mm256_load_ps(&mix[c][c2]);
254+
m128 = _mm_set1_ps(rematrix->_mix[c][c2]);//_mm256_load_ps(&mix[c][c2]);
254255
_mm_store_ps(&fmatrixed_data[c][s], _mm_add_ps(t128, _mm_mul_ps(s128, m128)));
255256
}
256257
for (; s < unprocessed_samples; s++) {
257-
fmatrixed_data[c][s] += fdata[c2][chunk + s] * mix[c][c2];
258+
fmatrixed_data[c][s] += fdata[c2][chunk + s] * rematrix->_mix[c][c2];
258259
}
259260
}
260261
}
261262
}
262263
//move data into place and process gain
263264
for (size_t c = 0; c < channels; c++) {
264-
if(!fdata[c])
265+
if (!fdata[c])
265266
continue;
266267
size_t s = 0;
267268
size_t end;
268269
/*
269270
end = unprocessed_samples - 8;
270271
for(; s < end; s+=8){
271-
s256 = _mm256_load_ps(&(fmatrixed_data[c][s]));
272-
m256 = _mm256_set1_ps(true_gain[c]);
273-
_mm256_store_ps(&fdata[c][chunk + s], _mm256_mul_ps(s256, m256));
272+
s256 = _mm256_load_ps(&(fmatrixed_data[c][s]));
273+
m256 = _mm256_set1_ps(true_gain[c]);
274+
_mm256_store_ps(&fdata[c][chunk + s], _mm256_mul_ps(s256, m256));
274275
}
275276
*/
276277
end = unprocessed_samples - 4;
277-
for(; s < end; s+=4){
278+
for (; s < end; s += 4) {
278279
s128 = _mm_load_ps(&(fmatrixed_data[c][s]));
279-
m128 = _mm_set1_ps(true_gain[c]);
280+
m128 = _mm_set1_ps(rematrix->_true_gain[c]);
280281
_mm_store_ps(&fdata[c][chunk + s], _mm_mul_ps(s128, m128));
281282
}
282283
for (; s < unprocessed_samples; s++) {
283-
fdata[c][chunk + s] = fmatrixed_data[c][s] * true_gain[c];
284+
fdata[c][chunk + s] = fmatrixed_data[c][s] * rematrix->_true_gain[c];
284285
}
285286
}
286287
//move to next chunk of unprocessed data
@@ -370,11 +371,11 @@ static bool update_visible(obs_properties_t *props, obs_property_t *prop,
370371

371372
for (i = 0; i < channels; i++) {
372373
sprintf(gain_name, gain_name_format, i);
373-
374+
374375
visible = i == selected_ch;
375376

376377
gain = obs_properties_get(props, gain_name);
377-
obs_property_set_visible(gain,visible);
378+
obs_property_set_visible(gain, visible);
378379

379380
for (j = 0; j < channels; j++) {
380381
sprintf(mix_name, mix_name_format, i, j);

0 commit comments

Comments
 (0)