Signed-off-by: Paul B Mahol <onemda@gmail.com>
Paul B Mahol authored on 2015/12/09 21:40:03... | ... |
@@ -279,6 +279,7 @@ External library support: |
279 | 279 |
--disable-lzma disable lzma [autodetect] |
280 | 280 |
--enable-decklink enable Blackmagic DeckLink I/O support [no] |
281 | 281 |
--enable-mmal enable decoding via MMAL [no] |
282 |
+ --enable-netcdf enable NetCDF, needed for sofalizer filter [no] |
|
282 | 283 |
--enable-nvenc enable NVIDIA NVENC support [no] |
283 | 284 |
--enable-openal enable OpenAL 1.1 capture support [no] |
284 | 285 |
--enable-opencl enable OpenCL code |
... | ... |
@@ -1503,6 +1504,7 @@ EXTERNAL_LIBRARY_LIST=" |
1503 | 1503 |
libzvbi |
1504 | 1504 |
lzma |
1505 | 1505 |
mmal |
1506 |
+ netcdf |
|
1506 | 1507 |
nvenc |
1507 | 1508 |
openal |
1508 | 1509 |
opencl |
... | ... |
@@ -2890,6 +2892,7 @@ showfreqs_filter_deps="avcodec" |
2890 | 2890 |
showfreqs_filter_select="fft" |
2891 | 2891 |
showspectrum_filter_deps="avcodec" |
2892 | 2892 |
showspectrum_filter_select="rdft" |
2893 |
+sofalizer_filter_deps="netcdf" |
|
2893 | 2894 |
spp_filter_deps="gpl avcodec" |
2894 | 2895 |
spp_filter_select="fft idctdsp fdctdsp me_cmp pixblockdsp" |
2895 | 2896 |
stereo3d_filter_deps="gpl" |
... | ... |
@@ -5494,6 +5497,7 @@ enabled mmal && { check_lib interface/mmal/mmal.h mmal_port_connect |
5494 | 5494 |
check_lib interface/mmal/mmal.h mmal_port_connect ; } |
5495 | 5495 |
check_lib interface/mmal/mmal.h mmal_port_connect ; } || |
5496 | 5496 |
die "ERROR: mmal not found"; } |
5497 |
+enabled netcdf && require_pkg_config netcdf netcdf.h nc_inq_libvers |
|
5497 | 5498 |
enabled nvenc && { check_header nvEncodeAPI.h || die "ERROR: nvEncodeAPI.h not found."; } && |
5498 | 5499 |
{ check_cpp_condition nvEncodeAPI.h "NVENCAPI_MAJOR_VERSION >= 5" || |
5499 | 5500 |
die "ERROR: NVENC API version 4 or older is not supported"; } && |
... | ... |
@@ -2889,6 +2889,35 @@ silenceremove=1:5:0.02 |
2889 | 2889 |
@end example |
2890 | 2890 |
@end itemize |
2891 | 2891 |
|
2892 |
+@section sofalizer |
|
2893 |
+ |
|
2894 |
+SOFAlizer uses head-related transfer functions (HRTFs) to create virtual |
|
2895 |
+loudspeakers around the user for binaural listening via headphones (audio |
|
2896 |
+formats up to 9 channels supported). |
|
2897 |
+The HRTFs are stored in SOFA files (see www.sofacoustics.org for a database). |
|
2898 |
+SOFAlizer is developed at the Acoustics Research Institute (ARI) of the |
|
2899 |
+Austrian Academy of Sciences. |
|
2900 |
+ |
|
2901 |
+The filter accepts the following options: |
|
2902 |
+ |
|
2903 |
+@table @option |
|
2904 |
+@item sofa |
|
2905 |
+Set the SOFA file used for rendering. |
|
2906 |
+ |
|
2907 |
+@item gain |
|
2908 |
+Set gain applied to audio. Value is in dB. Default is 0. |
|
2909 |
+ |
|
2910 |
+@item rotation |
|
2911 |
+Set rotation of virtual loudspeakers in deg. Default is 0. |
|
2912 |
+ |
|
2913 |
+@item elevation |
|
2914 |
+Set elevation of virtual speakers in deg. Default is 0. |
|
2915 |
+ |
|
2916 |
+@item radius |
|
2917 |
+Set distance in meters between loudspeakers and the listener with near-field |
|
2918 |
+HRTFs. Default is 1. |
|
2919 |
+@end table |
|
2920 |
+ |
|
2892 | 2921 |
@section stereotools |
2893 | 2922 |
|
2894 | 2923 |
This filter has some handy utilities to manage stereo signals, for converting |
... | ... |
@@ -87,6 +87,7 @@ OBJS-$(CONFIG_SIDECHAINCOMPRESS_FILTER) += af_sidechaincompress.o |
87 | 87 |
OBJS-$(CONFIG_SIDECHAINGATE_FILTER) += af_agate.o |
88 | 88 |
OBJS-$(CONFIG_SILENCEDETECT_FILTER) += af_silencedetect.o |
89 | 89 |
OBJS-$(CONFIG_SILENCEREMOVE_FILTER) += af_silenceremove.o |
90 |
+OBJS-$(CONFIG_SOFALIZER_FILTER) += af_sofalizer.o |
|
90 | 91 |
OBJS-$(CONFIG_STEREOTOOLS_FILTER) += af_stereotools.o |
91 | 92 |
OBJS-$(CONFIG_STEREOWIDEN_FILTER) += af_stereowiden.o |
92 | 93 |
OBJS-$(CONFIG_TREBLE_FILTER) += af_biquads.o |
93 | 94 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,1018 @@ |
0 |
+/***************************************************************************** |
|
1 |
+ * sofalizer.c : SOFAlizer filter for virtual binaural acoustics |
|
2 |
+ ***************************************************************************** |
|
3 |
+ * Copyright (C) 2013-2015 Andreas Fuchs, Wolfgang Hrauda, |
|
4 |
+ * Acoustics Research Institute (ARI), Vienna, Austria |
|
5 |
+ * |
|
6 |
+ * Authors: Andreas Fuchs <andi.fuchs.mail@gmail.com> |
|
7 |
+ * Wolfgang Hrauda <wolfgang.hrauda@gmx.at> |
|
8 |
+ * |
|
9 |
+ * SOFAlizer project coordinator at ARI, main developer of SOFA: |
|
10 |
+ * Piotr Majdak <piotr@majdak.at> |
|
11 |
+ * |
|
12 |
+ * This program is free software; you can redistribute it and/or modify it |
|
13 |
+ * under the terms of the GNU Lesser General Public License as published by |
|
14 |
+ * the Free Software Foundation; either version 2.1 of the License, or |
|
15 |
+ * (at your option) any later version. |
|
16 |
+ * |
|
17 |
+ * This program is distributed in the hope that it will be useful, |
|
18 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 |
+ * GNU Lesser General Public License for more details. |
|
21 |
+ * |
|
22 |
+ * You should have received a copy of the GNU Lesser General Public License |
|
23 |
+ * along with this program; if not, write to the Free Software Foundation, |
|
24 |
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
|
25 |
+ *****************************************************************************/ |
|
26 |
+ |
|
27 |
+#include <math.h> |
|
28 |
+#include <netcdf.h> |
|
29 |
+ |
|
30 |
+#include "libavutil/float_dsp.h" |
|
31 |
+#include "libavutil/opt.h" |
|
32 |
+#include "avfilter.h" |
|
33 |
+#include "internal.h" |
|
34 |
+#include "audio.h" |
|
35 |
+ |
|
36 |
+typedef struct NCSofa { /* contains data of one SOFA file */ |
|
37 |
+ int ncid; /* netCDF ID of the opened SOFA file */ |
|
38 |
+ int n_samples; /* length of one impulse response (IR) */ |
|
39 |
+ int m_dim; /* number of measurement positions */ |
|
40 |
+ int *data_delay; /* broadband delay of each IR */ |
|
41 |
+ /* all measurement positions for each receiver (i.e. ear): */ |
|
42 |
+ float *sp_a; /* azimuth angles */ |
|
43 |
+ float *sp_e; /* elevation angles */ |
|
44 |
+ float *sp_r; /* radii */ |
|
45 |
+ /* data at each measurement position for each receiver: */ |
|
46 |
+ float *data_ir; /* IRs (time-domain) */ |
|
47 |
+} NCSofa; |
|
48 |
+ |
|
49 |
+typedef struct SOFAlizerContext { |
|
50 |
+ const AVClass *class; |
|
51 |
+ |
|
52 |
+ char *filename; /* name of SOFA file */ |
|
53 |
+ NCSofa sofa; /* contains data of the SOFA file */ |
|
54 |
+ |
|
55 |
+ const int8_t *reorder; /* reorder in SOFA channel order */ |
|
56 |
+ int sample_rate; /* sample rate from SOFA file */ |
|
57 |
+ float *speaker_pos; /* positions of the virtual loudspekaers */ |
|
58 |
+ float gain_lfe; /* gain applied to LFE channel */ |
|
59 |
+ |
|
60 |
+ int n_conv; /* number of channels to convolute */ |
|
61 |
+ |
|
62 |
+ /* buffer variables (for convolution) */ |
|
63 |
+ float *ringbuffer[2]; /* buffers input samples, length of one buffer: */ |
|
64 |
+ /* no. input ch. (incl. LFE) x buffer_length */ |
|
65 |
+ int write[2]; /* current write position to ringbuffer */ |
|
66 |
+ int buffer_length; /* is: longest IR plus max. delay in all SOFA files */ |
|
67 |
+ /* then choose next power of 2 */ |
|
68 |
+ |
|
69 |
+ /* netCDF variables */ |
|
70 |
+ int *delay[2]; /* broadband delay for each channel/IR to be convolved */ |
|
71 |
+ |
|
72 |
+ float *data_ir[2]; /* IRs for all channels to be convolved */ |
|
73 |
+ /* (this excludes the LFE) */ |
|
74 |
+ float *temp_src[2]; |
|
75 |
+ |
|
76 |
+ /* control variables */ |
|
77 |
+ float gain; /* filter gain (in dB) */ |
|
78 |
+ float rotation; /* rotation of virtual loudspeakers (in degrees) */ |
|
79 |
+ float elevation; /* elevation of virtual loudspeakers (in deg.) */ |
|
80 |
+ float radius; /* distance virtual loudspeakers to listener (in metres) */ |
|
81 |
+ |
|
82 |
+ int lfe; /* whether or not the LFE channel is used */ |
|
83 |
+ |
|
84 |
+ AVFloatDSPContext *fdsp; |
|
85 |
+} SOFAlizerContext; |
|
86 |
+ |
|
87 |
+static int close_sofa(struct NCSofa *sofa) |
|
88 |
+{ |
|
89 |
+ av_freep(&sofa->data_delay); |
|
90 |
+ av_freep(&sofa->sp_a); |
|
91 |
+ av_freep(&sofa->sp_e); |
|
92 |
+ av_freep(&sofa->sp_r); |
|
93 |
+ av_freep(&sofa->data_ir); |
|
94 |
+ nc_close(sofa->ncid); |
|
95 |
+ sofa->ncid = 0; |
|
96 |
+ |
|
97 |
+ return 0; |
|
98 |
+} |
|
99 |
+ |
|
100 |
+static int load_sofa(AVFilterContext *ctx, char *filename, int *samplingrate) |
|
101 |
+{ |
|
102 |
+ struct SOFAlizerContext *s = ctx->priv; |
|
103 |
+ /* variables associated with content of SOFA file: */ |
|
104 |
+ int ncid, n_dims, n_vars, n_gatts, n_unlim_dim_id, status; |
|
105 |
+ char data_delay_dim_name[NC_MAX_NAME]; |
|
106 |
+ float *sp_a, *sp_e, *sp_r, *data_ir; |
|
107 |
+ char *sofa_conventions; |
|
108 |
+ char dim_name[NC_MAX_NAME]; /* names of netCDF dimensions */ |
|
109 |
+ size_t *dim_length; /* lengths of netCDF dimensions */ |
|
110 |
+ char *psz_conventions; |
|
111 |
+ unsigned int sample_rate; |
|
112 |
+ int data_delay_dim_id[2]; |
|
113 |
+ int samplingrate_id; |
|
114 |
+ int data_delay_id; |
|
115 |
+ int n_samples; |
|
116 |
+ int m_dim_id = -1; |
|
117 |
+ int n_dim_id = -1; |
|
118 |
+ int data_ir_id; |
|
119 |
+ size_t att_len; |
|
120 |
+ int m_dim; |
|
121 |
+ int *data_delay; |
|
122 |
+ int sp_id; |
|
123 |
+ int i, ret; |
|
124 |
+ |
|
125 |
+ s->sofa.ncid = 0; |
|
126 |
+ status = nc_open(filename, NC_NOWRITE, &ncid); /* open SOFA file read-only */ |
|
127 |
+ if (status != NC_NOERR) { |
|
128 |
+ av_log(ctx, AV_LOG_ERROR, "Can't find SOFA-file '%s'\n", filename); |
|
129 |
+ return AVERROR(EINVAL); |
|
130 |
+ } |
|
131 |
+ |
|
132 |
+ /* get number of dimensions, vars, global attributes and Id of unlimited dimensions: */ |
|
133 |
+ nc_inq(ncid, &n_dims, &n_vars, &n_gatts, &n_unlim_dim_id); |
|
134 |
+ |
|
135 |
+ /* -- get number of measurements ("M") and length of one IR ("N") -- */ |
|
136 |
+ dim_length = av_malloc_array(n_dims, sizeof(*dim_length)); |
|
137 |
+ if (!dim_length) { |
|
138 |
+ nc_close(ncid); |
|
139 |
+ return AVERROR(ENOMEM); |
|
140 |
+ } |
|
141 |
+ |
|
142 |
+ for (i = 0; i < n_dims; i++) { /* go through all dimensions of file */ |
|
143 |
+ nc_inq_dim(ncid, i, (char *)&dim_name, &dim_length[i]); /* get dimensions */ |
|
144 |
+ if (!strncmp("M", (const char *)&dim_name, 1)) /* get ID of dimension "M" */ |
|
145 |
+ m_dim_id = i; |
|
146 |
+ if (!strncmp("N", (const char *)&dim_name, 1)) /* get ID of dimension "N" */ |
|
147 |
+ n_dim_id = i; |
|
148 |
+ } |
|
149 |
+ |
|
150 |
+ if ((m_dim_id == -1) || (n_dim_id == -1)) { /* dimension "M" or "N" couldn't be found */ |
|
151 |
+ av_log(ctx, AV_LOG_ERROR, "Can't find required dimensions in SOFA file.\n"); |
|
152 |
+ av_freep(&dim_length); |
|
153 |
+ nc_close(ncid); |
|
154 |
+ return AVERROR(EINVAL); |
|
155 |
+ } |
|
156 |
+ |
|
157 |
+ n_samples = dim_length[n_dim_id]; /* get number of measurements */ |
|
158 |
+ m_dim = dim_length[m_dim_id]; /* get length of one IR */ |
|
159 |
+ |
|
160 |
+ av_freep(&dim_length); |
|
161 |
+ |
|
162 |
+ /* -- check file type -- */ |
|
163 |
+ /* get length of attritube "Conventions" */ |
|
164 |
+ status = nc_inq_attlen(ncid, NC_GLOBAL, "Conventions", &att_len); |
|
165 |
+ if (status != NC_NOERR) { |
|
166 |
+ av_log(ctx, AV_LOG_ERROR, "Can't get length of attribute \"Conventions\".\n"); |
|
167 |
+ nc_close(ncid); |
|
168 |
+ return AVERROR_INVALIDDATA; |
|
169 |
+ } |
|
170 |
+ |
|
171 |
+ /* check whether file is SOFA file */ |
|
172 |
+ psz_conventions = av_malloc(att_len + 1); |
|
173 |
+ if (!psz_conventions) { |
|
174 |
+ nc_close(ncid); |
|
175 |
+ return AVERROR(ENOMEM); |
|
176 |
+ } |
|
177 |
+ |
|
178 |
+ nc_get_att_text(ncid, NC_GLOBAL, "Conventions", psz_conventions); |
|
179 |
+ *(psz_conventions + att_len) = 0; |
|
180 |
+ if (strncmp("SOFA", psz_conventions, 4)) { |
|
181 |
+ av_log(ctx, AV_LOG_ERROR, "Not a SOFA file!\n"); |
|
182 |
+ av_freep(&psz_conventions); |
|
183 |
+ nc_close(ncid); |
|
184 |
+ return AVERROR(EINVAL); |
|
185 |
+ } |
|
186 |
+ av_freep(&psz_conventions); |
|
187 |
+ |
|
188 |
+ status = nc_inq_attlen(ncid, NC_GLOBAL, "SOFAConventions", &att_len); |
|
189 |
+ if (status != NC_NOERR) { |
|
190 |
+ av_log(ctx, AV_LOG_ERROR, "Can't get length of attribute \"SOFAConventions\".\n"); |
|
191 |
+ nc_close(ncid); |
|
192 |
+ return AVERROR_INVALIDDATA; |
|
193 |
+ } |
|
194 |
+ |
|
195 |
+ sofa_conventions = av_malloc(att_len + 1); |
|
196 |
+ if (!sofa_conventions) { |
|
197 |
+ nc_close(ncid); |
|
198 |
+ return AVERROR(ENOMEM); |
|
199 |
+ } |
|
200 |
+ |
|
201 |
+ nc_get_att_text(ncid, NC_GLOBAL, "SOFAConventions", sofa_conventions); |
|
202 |
+ *(sofa_conventions + att_len) = 0; |
|
203 |
+ if (strncmp("SimpleFreeFieldHRIR", sofa_conventions, att_len)) { |
|
204 |
+ av_log(ctx, AV_LOG_ERROR, "Not a SimpleFreeFieldHRIR file!\n"); |
|
205 |
+ av_freep(&sofa_conventions); |
|
206 |
+ nc_close(ncid); |
|
207 |
+ return AVERROR(EINVAL); |
|
208 |
+ } |
|
209 |
+ av_freep(&sofa_conventions); |
|
210 |
+ |
|
211 |
+ /* -- get sampling rate of HRTFs -- */ |
|
212 |
+ /* read ID, then value */ |
|
213 |
+ status = nc_inq_varid(ncid, "Data.SamplingRate", &samplingrate_id); |
|
214 |
+ status += nc_get_var_uint(ncid, samplingrate_id, &sample_rate); |
|
215 |
+ if (status != NC_NOERR) { |
|
216 |
+ av_log(ctx, AV_LOG_ERROR, "Couldn't read Data.SamplingRate.\n"); |
|
217 |
+ nc_close(ncid); |
|
218 |
+ return AVERROR(EINVAL); |
|
219 |
+ } |
|
220 |
+ *samplingrate = sample_rate; /* remember sampling rate */ |
|
221 |
+ |
|
222 |
+ /* -- allocate memory for one value for each measurement position: -- */ |
|
223 |
+ sp_a = s->sofa.sp_a = av_malloc_array(m_dim, sizeof(float)); |
|
224 |
+ sp_e = s->sofa.sp_e = av_malloc_array(m_dim, sizeof(float)); |
|
225 |
+ sp_r = s->sofa.sp_r = av_malloc_array(m_dim, sizeof(float)); |
|
226 |
+ /* delay and IR values required for each ear and measurement position: */ |
|
227 |
+ data_delay = s->sofa.data_delay = av_calloc(m_dim, 2 * sizeof(int)); |
|
228 |
+ data_ir = s->sofa.data_ir = av_malloc_array(m_dim * n_samples, sizeof(float) * 2); |
|
229 |
+ s->temp_src[0] = av_calloc(FFALIGN(n_samples, 16), sizeof(float)); |
|
230 |
+ s->temp_src[1] = av_calloc(FFALIGN(n_samples, 16), sizeof(float)); |
|
231 |
+ |
|
232 |
+ if (!data_delay || !sp_a || !sp_e || !sp_r || !data_ir || |
|
233 |
+ !s->temp_src[0] || !s->temp_src[1]) { |
|
234 |
+ /* if memory could not be allocated */ |
|
235 |
+ close_sofa(&s->sofa); |
|
236 |
+ return AVERROR(ENOMEM); |
|
237 |
+ } |
|
238 |
+ |
|
239 |
+ /* get impulse responses (HRTFs): */ |
|
240 |
+ /* get corresponding ID */ |
|
241 |
+ status = nc_inq_varid(ncid, "Data.IR", &data_ir_id); |
|
242 |
+ status += nc_get_var_float(ncid, data_ir_id, data_ir); /* read and store IRs */ |
|
243 |
+ if (status != NC_NOERR) { |
|
244 |
+ av_log(ctx, AV_LOG_ERROR, "Couldn't read Data.IR!\n"); |
|
245 |
+ ret = AVERROR(EINVAL); |
|
246 |
+ goto error; |
|
247 |
+ } |
|
248 |
+ |
|
249 |
+ /* get source positions of the HRTFs in the SOFA file: */ |
|
250 |
+ status = nc_inq_varid(ncid, "SourcePosition", &sp_id); /* get corresponding ID */ |
|
251 |
+ status += nc_get_vara_float(ncid, sp_id, (size_t[2]){ 0, 0 } , |
|
252 |
+ (size_t[2]){ m_dim, 1}, sp_a); /* read & store azimuth angles */ |
|
253 |
+ status += nc_get_vara_float(ncid, sp_id, (size_t[2]){ 0, 1 } , |
|
254 |
+ (size_t[2]){ m_dim, 1}, sp_e); /* read & store elevation angles */ |
|
255 |
+ status += nc_get_vara_float(ncid, sp_id, (size_t[2]){ 0, 2 } , |
|
256 |
+ (size_t[2]){ m_dim, 1}, sp_r); /* read & store radii */ |
|
257 |
+ if (status != NC_NOERR) { /* if any source position variable coudn't be read */ |
|
258 |
+ av_log(ctx, AV_LOG_ERROR, "Couldn't read SourcePosition.\n"); |
|
259 |
+ ret = AVERROR(EINVAL); |
|
260 |
+ goto error; |
|
261 |
+ } |
|
262 |
+ |
|
263 |
+ /* read Data.Delay, check for errors and fit it to data_delay */ |
|
264 |
+ status = nc_inq_varid(ncid, "Data.Delay", &data_delay_id); |
|
265 |
+ status += nc_inq_vardimid(ncid, data_delay_id, &data_delay_dim_id[0]); |
|
266 |
+ status += nc_inq_dimname(ncid, data_delay_dim_id[0], data_delay_dim_name); |
|
267 |
+ if (status != NC_NOERR) { |
|
268 |
+ av_log(ctx, AV_LOG_ERROR, "Couldn't read Data.Delay.\n"); |
|
269 |
+ ret = AVERROR(EINVAL); |
|
270 |
+ goto error; |
|
271 |
+ } |
|
272 |
+ |
|
273 |
+ /* Data.Delay dimension check */ |
|
274 |
+ /* dimension of Data.Delay is [I R]: */ |
|
275 |
+ if (!strncmp(data_delay_dim_name, "I", 2)) { |
|
276 |
+ /* check 2 characters to assure string is 0-terminated after "I" */ |
|
277 |
+ int delay[2]; /* delays get from SOFA file: */ |
|
278 |
+ |
|
279 |
+ av_log(ctx, AV_LOG_DEBUG, "Data.Delay has dimension [I R]\n"); |
|
280 |
+ status = nc_get_var_int(ncid, data_delay_id, &delay[0]); |
|
281 |
+ if (status != NC_NOERR) { |
|
282 |
+ av_log(ctx, AV_LOG_ERROR, "Couldn't read Data.Delay\n"); |
|
283 |
+ ret = AVERROR(EINVAL); |
|
284 |
+ goto error; |
|
285 |
+ } |
|
286 |
+ int *data_delay_r = data_delay + m_dim; |
|
287 |
+ for (i = 0; i < m_dim; i++) { /* extend given dimension [I R] to [M R] */ |
|
288 |
+ /* assign constant delay value for all measurements to data_delay fields */ |
|
289 |
+ data_delay[i] = delay[0]; |
|
290 |
+ data_delay_r[i] = delay[1]; |
|
291 |
+ } |
|
292 |
+ /* dimension of Data.Delay is [M R] */ |
|
293 |
+ } else if (!strncmp(data_delay_dim_name, "M", 2)) { |
|
294 |
+ av_log(ctx, AV_LOG_ERROR, "Data.Delay in dimension [M R]\n"); |
|
295 |
+ /* get delays from SOFA file: */ |
|
296 |
+ status = nc_get_var_int(ncid, data_delay_id, data_delay); |
|
297 |
+ if (status != NC_NOERR) { |
|
298 |
+ av_log(ctx, AV_LOG_ERROR, "Couldn't read Data.Delay\n"); |
|
299 |
+ ret = AVERROR(EINVAL); |
|
300 |
+ goto error; |
|
301 |
+ } |
|
302 |
+ } else { /* dimension of Data.Delay is neither [I R] nor [M R] */ |
|
303 |
+ av_log(ctx, AV_LOG_ERROR, "Data.Delay does not have the required dimensions [I R] or [M R].\n"); |
|
304 |
+ ret = AVERROR(EINVAL); |
|
305 |
+ goto error; |
|
306 |
+ } |
|
307 |
+ |
|
308 |
+ /* save information in SOFA struct: */ |
|
309 |
+ s->sofa.m_dim = m_dim; /* no. measurement positions */ |
|
310 |
+ s->sofa.n_samples = n_samples; /* length on one IR */ |
|
311 |
+ s->sofa.ncid = ncid; /* netCDF ID of SOFA file */ |
|
312 |
+ nc_close(ncid); /* close SOFA file */ |
|
313 |
+ |
|
314 |
+ return 0; |
|
315 |
+ |
|
316 |
+error: |
|
317 |
+ close_sofa(&s->sofa); |
|
318 |
+ return ret; |
|
319 |
+} |
|
320 |
+ |
|
321 |
+static const int8_t reorder[18][9] = { |
|
322 |
+ { 0, -1, -1, -1, -1, -1, -1, -1, -1 }, |
|
323 |
+ { 0, 1, -1, -1, -1, -1, -1, -1, -1 }, |
|
324 |
+ { 0, 1, 2, -1, -1, -1, -1, -1, -1 }, |
|
325 |
+ { 0, 1, 2, -1, -1, -1, -1, -1, -1 }, |
|
326 |
+ { 0, 1, 2, 3, -1, -1, -1, -1, -1 }, |
|
327 |
+ { 0, 1, 2, 3, -1, -1, -1, -1, -1 }, |
|
328 |
+ { 0, 1, 2, 3, -1, -1, -1, -1, -1 }, |
|
329 |
+ { 0, 1, 3, 4, 2, -1, -1, -1, -1 }, |
|
330 |
+ { 0, 1, 3, 4, 2, -1, -1, -1, -1 }, |
|
331 |
+ { 0, 1, 4, 5, 2, 3, -1, -1, -1 }, |
|
332 |
+ { 0, 1, 4, 5, 2, 3, -1, -1, -1 }, |
|
333 |
+ { 0, 1, 5, 6, 4, 2, 3, -1, -1 }, |
|
334 |
+ { 0, 1, 5, 6, 3, 4, 2, -1, -1 }, |
|
335 |
+ { 0, 1, 6, 7, 4, 5, 2, 3, -1 }, |
|
336 |
+ { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, |
|
337 |
+ { 0, 1, 2, 3, 4, 5, 6, 7, -1 }, |
|
338 |
+ { 0, 1, 3, 4, 2, 5, -1, -1, -1 }, |
|
339 |
+ { 0, 1, 4, 5, 2, 6, 3, -1, -1 }, |
|
340 |
+}; |
|
341 |
+ |
|
342 |
+static int get_speaker_pos(AVFilterContext *ctx, float *speaker_pos) |
|
343 |
+{ |
|
344 |
+ struct SOFAlizerContext *s = ctx->priv; |
|
345 |
+ uint64_t channels_layout = ctx->inputs[0]->channel_layout; |
|
346 |
+ float pos_temp[9]; |
|
347 |
+ int nb_input_channels = ctx->inputs[0]->channels; /* get no. input channels */ |
|
348 |
+ int n_conv = nb_input_channels; |
|
349 |
+ |
|
350 |
+ if (channels_layout & AV_CH_LOW_FREQUENCY) { /* if LFE is used */ |
|
351 |
+ /* decrease number of channels to be convolved: */ |
|
352 |
+ n_conv = nb_input_channels - 1; |
|
353 |
+ } |
|
354 |
+ |
|
355 |
+ /* set speaker positions according to input channel configuration: */ |
|
356 |
+ switch (channels_layout) { |
|
357 |
+ case AV_CH_LAYOUT_MONO: |
|
358 |
+ pos_temp[0] = 0; |
|
359 |
+ break; |
|
360 |
+ case AV_CH_LAYOUT_STEREO: |
|
361 |
+ case AV_CH_LAYOUT_2POINT1: |
|
362 |
+ pos_temp[0] = 30; |
|
363 |
+ pos_temp[1] = 330; |
|
364 |
+ break; |
|
365 |
+ case AV_CH_LAYOUT_SURROUND: |
|
366 |
+ case AV_CH_LAYOUT_3POINT1: |
|
367 |
+ pos_temp[0] = 30; |
|
368 |
+ pos_temp[1] = 330; |
|
369 |
+ pos_temp[2] = 0; |
|
370 |
+ break; |
|
371 |
+ case AV_CH_LAYOUT_2_1: |
|
372 |
+ pos_temp[0] = 30; |
|
373 |
+ pos_temp[1] = 330; |
|
374 |
+ pos_temp[2] = 180; |
|
375 |
+ break; |
|
376 |
+ case AV_CH_LAYOUT_2_2: |
|
377 |
+ pos_temp[0] = 30; |
|
378 |
+ pos_temp[1] = 330; |
|
379 |
+ pos_temp[2] = 90; |
|
380 |
+ pos_temp[3] = 270; |
|
381 |
+ break; |
|
382 |
+ case AV_CH_LAYOUT_QUAD: |
|
383 |
+ pos_temp[0] = 30; |
|
384 |
+ pos_temp[1] = 330; |
|
385 |
+ pos_temp[2] = 120; |
|
386 |
+ pos_temp[3] = 240; |
|
387 |
+ break; |
|
388 |
+ case AV_CH_LAYOUT_4POINT0: |
|
389 |
+ case AV_CH_LAYOUT_4POINT1: |
|
390 |
+ pos_temp[0] = 30; |
|
391 |
+ pos_temp[1] = 330; |
|
392 |
+ pos_temp[2] = 0; |
|
393 |
+ pos_temp[3] = 180; |
|
394 |
+ break; |
|
395 |
+ case AV_CH_LAYOUT_5POINT0: |
|
396 |
+ case AV_CH_LAYOUT_5POINT1: |
|
397 |
+ pos_temp[0] = 30; |
|
398 |
+ pos_temp[1] = 330; |
|
399 |
+ pos_temp[2] = 90; |
|
400 |
+ pos_temp[3] = 270; |
|
401 |
+ pos_temp[4] = 0; |
|
402 |
+ break; |
|
403 |
+ case AV_CH_LAYOUT_5POINT0_BACK: |
|
404 |
+ case AV_CH_LAYOUT_5POINT1_BACK: |
|
405 |
+ pos_temp[0] = 30; |
|
406 |
+ pos_temp[1] = 330; |
|
407 |
+ pos_temp[2] = 120; |
|
408 |
+ pos_temp[3] = 240; |
|
409 |
+ pos_temp[4] = 0; |
|
410 |
+ break; |
|
411 |
+ case AV_CH_LAYOUT_6POINT0: |
|
412 |
+ case AV_CH_LAYOUT_6POINT1: |
|
413 |
+ pos_temp[0] = 30; |
|
414 |
+ pos_temp[1] = 330; |
|
415 |
+ pos_temp[2] = 90; |
|
416 |
+ pos_temp[3] = 270; |
|
417 |
+ pos_temp[4] = 0; |
|
418 |
+ pos_temp[5] = 180; |
|
419 |
+ break; |
|
420 |
+ case AV_CH_LAYOUT_6POINT1_BACK: |
|
421 |
+ pos_temp[0] = 30; |
|
422 |
+ pos_temp[1] = 330; |
|
423 |
+ pos_temp[2] = 120; |
|
424 |
+ pos_temp[3] = 240; |
|
425 |
+ pos_temp[4] = 0; |
|
426 |
+ pos_temp[4] = 180; |
|
427 |
+ break; |
|
428 |
+ case AV_CH_LAYOUT_HEXAGONAL: |
|
429 |
+ pos_temp[0] = 30; |
|
430 |
+ pos_temp[1] = 330; |
|
431 |
+ pos_temp[2] = 120; |
|
432 |
+ pos_temp[3] = 240; |
|
433 |
+ pos_temp[4] = 0; |
|
434 |
+ pos_temp[5] = 180; |
|
435 |
+ break; |
|
436 |
+ case AV_CH_LAYOUT_7POINT0: |
|
437 |
+ case AV_CH_LAYOUT_7POINT1: |
|
438 |
+ pos_temp[0] = 30; |
|
439 |
+ pos_temp[1] = 330; |
|
440 |
+ pos_temp[2] = 90; |
|
441 |
+ pos_temp[3] = 270; |
|
442 |
+ pos_temp[4] = 150; |
|
443 |
+ pos_temp[5] = 210; |
|
444 |
+ pos_temp[6] = 0; |
|
445 |
+ break; |
|
446 |
+ case AV_CH_LAYOUT_OCTAGONAL: |
|
447 |
+ pos_temp[0] = 30; |
|
448 |
+ pos_temp[1] = 330; |
|
449 |
+ pos_temp[2] = 0; |
|
450 |
+ pos_temp[3] = 150; |
|
451 |
+ pos_temp[4] = 210; |
|
452 |
+ pos_temp[5] = 180; |
|
453 |
+ pos_temp[6] = 90; |
|
454 |
+ pos_temp[7] = 270; |
|
455 |
+ break; |
|
456 |
+ default: |
|
457 |
+ return -1; |
|
458 |
+ } |
|
459 |
+ |
|
460 |
+ switch (channels_layout) { |
|
461 |
+ case AV_CH_LAYOUT_MONO: |
|
462 |
+ s->reorder = reorder[0]; |
|
463 |
+ break; |
|
464 |
+ case AV_CH_LAYOUT_STEREO: |
|
465 |
+ s->reorder = reorder[1]; |
|
466 |
+ break; |
|
467 |
+ case AV_CH_LAYOUT_2_1: |
|
468 |
+ case AV_CH_LAYOUT_2POINT1: |
|
469 |
+ s->reorder = reorder[2]; |
|
470 |
+ break; |
|
471 |
+ case AV_CH_LAYOUT_SURROUND: |
|
472 |
+ s->reorder = reorder[3]; |
|
473 |
+ break; |
|
474 |
+ case AV_CH_LAYOUT_3POINT1: |
|
475 |
+ case AV_CH_LAYOUT_2_2: |
|
476 |
+ s->reorder = reorder[4]; |
|
477 |
+ break; |
|
478 |
+ case AV_CH_LAYOUT_QUAD: |
|
479 |
+ s->reorder = reorder[5]; |
|
480 |
+ break; |
|
481 |
+ case AV_CH_LAYOUT_4POINT0: |
|
482 |
+ s->reorder = reorder[6]; |
|
483 |
+ break; |
|
484 |
+ case AV_CH_LAYOUT_4POINT1: |
|
485 |
+ s->reorder = reorder[7]; |
|
486 |
+ break; |
|
487 |
+ case AV_CH_LAYOUT_5POINT0: |
|
488 |
+ case AV_CH_LAYOUT_5POINT0_BACK: |
|
489 |
+ s->reorder = reorder[8]; |
|
490 |
+ break; |
|
491 |
+ case AV_CH_LAYOUT_5POINT1: |
|
492 |
+ case AV_CH_LAYOUT_5POINT1_BACK: |
|
493 |
+ s->reorder = reorder[9]; |
|
494 |
+ break; |
|
495 |
+ case AV_CH_LAYOUT_6POINT0: |
|
496 |
+ s->reorder = reorder[10]; |
|
497 |
+ break; |
|
498 |
+ case AV_CH_LAYOUT_HEXAGONAL: |
|
499 |
+ s->reorder = reorder[16]; |
|
500 |
+ break; |
|
501 |
+ case AV_CH_LAYOUT_6POINT1: |
|
502 |
+ s->reorder = reorder[11]; |
|
503 |
+ break; |
|
504 |
+ case AV_CH_LAYOUT_6POINT1_BACK: |
|
505 |
+ s->reorder = reorder[17]; |
|
506 |
+ break; |
|
507 |
+ case AV_CH_LAYOUT_7POINT0: |
|
508 |
+ s->reorder = reorder[12]; |
|
509 |
+ break; |
|
510 |
+ case AV_CH_LAYOUT_7POINT1: |
|
511 |
+ s->reorder = reorder[13]; |
|
512 |
+ break; |
|
513 |
+ case AV_CH_LAYOUT_OCTAGONAL: |
|
514 |
+ s->reorder = reorder[15]; |
|
515 |
+ break; |
|
516 |
+ default: |
|
517 |
+ return -1; |
|
518 |
+ } |
|
519 |
+ |
|
520 |
+ memcpy(speaker_pos, pos_temp, n_conv * sizeof(float)); |
|
521 |
+ |
|
522 |
+ return 0; |
|
523 |
+ |
|
524 |
+} |
|
525 |
+ |
|
526 |
+static int max_delay(struct NCSofa *sofa) |
|
527 |
+{ |
|
528 |
+ int i, max = 0; |
|
529 |
+ |
|
530 |
+ for (i = 0; i < sofa->m_dim * 2; i++) { |
|
531 |
+ /* search maximum delay in given SOFA file */ |
|
532 |
+ max = FFMAX(max, sofa->data_delay[i]); |
|
533 |
+ } |
|
534 |
+ |
|
535 |
+ return max; |
|
536 |
+} |
|
537 |
+ |
|
538 |
+static int find_m(SOFAlizerContext *s, int azim, int elev, float radius) |
|
539 |
+{ |
|
540 |
+ /* get source positions and M of currently selected SOFA file */ |
|
541 |
+ float *sp_a = s->sofa.sp_a; /* azimuth angle */ |
|
542 |
+ float *sp_e = s->sofa.sp_e; /* elevation angle */ |
|
543 |
+ float *sp_r = s->sofa.sp_r; /* radius */ |
|
544 |
+ int m_dim = s->sofa.m_dim; /* no. measurements */ |
|
545 |
+ int best_id = 0; /* index m currently closest to desired source pos. */ |
|
546 |
+ float delta = 1000; /* offset between desired and currently best pos. */ |
|
547 |
+ float current; |
|
548 |
+ int i; |
|
549 |
+ |
|
550 |
+ for (i = 0; i < m_dim; i++) { |
|
551 |
+ /* search through all measurements in currently selected SOFA file */ |
|
552 |
+ /* distance of current to desired source position: */ |
|
553 |
+ current = fabs(sp_a[i] - azim) + |
|
554 |
+ fabs(sp_e[i] - elev) + |
|
555 |
+ fabs(sp_r[i] - radius); |
|
556 |
+ if (current <= delta) { |
|
557 |
+ /* if current distance is smaller than smallest distance so far */ |
|
558 |
+ delta = current; |
|
559 |
+ best_id = i; /* remember index */ |
|
560 |
+ } |
|
561 |
+ } |
|
562 |
+ |
|
563 |
+ return best_id; |
|
564 |
+} |
|
565 |
+ |
|
566 |
+static int compensate_volume(AVFilterContext *ctx) |
|
567 |
+{ |
|
568 |
+ struct SOFAlizerContext *s = ctx->priv; |
|
569 |
+ float compensate; |
|
570 |
+ float energy = 0; |
|
571 |
+ float *ir; |
|
572 |
+ int m, j; |
|
573 |
+ |
|
574 |
+ if (s->sofa.ncid) { |
|
575 |
+ /* find IR at front center position in the SOFA file (IR closest to 0°,0°,1m) */ |
|
576 |
+ struct NCSofa *sofa = &s->sofa; |
|
577 |
+ m = find_m(s, 0, 0, 1); |
|
578 |
+ /* get energy of that IR and compensate volume */ |
|
579 |
+ ir = sofa->data_ir + 2 * m * sofa->n_samples; |
|
580 |
+ for (j = 0; j < sofa->n_samples; j++) { |
|
581 |
+ energy += *(ir + j) * *(ir + j); |
|
582 |
+ } |
|
583 |
+ compensate = 256 / (sofa->n_samples * sqrt(energy)); |
|
584 |
+ av_log(ctx, AV_LOG_DEBUG, "Compensate-factor: %f\n", compensate); |
|
585 |
+ ir = sofa->data_ir; |
|
586 |
+ for (j = 0; j < sofa->n_samples * sofa->m_dim * 2; j++) { |
|
587 |
+ ir[j] *= compensate; /* apply volume compensation to IRs */ |
|
588 |
+ } |
|
589 |
+ } |
|
590 |
+ |
|
591 |
+ return 0; |
|
592 |
+} |
|
593 |
+ |
|
594 |
+typedef struct ThreadData { |
|
595 |
+ AVFrame *in, *out; |
|
596 |
+ int *write; |
|
597 |
+ int **delay; |
|
598 |
+ float **ir; |
|
599 |
+ int *n_clippings; |
|
600 |
+ float **ringbuffer; |
|
601 |
+ float **temp_src; |
|
602 |
+} ThreadData; |
|
603 |
+ |
|
604 |
+static int sofalizer_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
|
605 |
+{ |
|
606 |
+ SOFAlizerContext *s = ctx->priv; |
|
607 |
+ ThreadData *td = arg; |
|
608 |
+ AVFrame *in = td->in, *out = td->out; |
|
609 |
+ int offset = jobnr; |
|
610 |
+ int *write = &td->write[jobnr]; |
|
611 |
+ const int *const delay = td->delay[jobnr]; |
|
612 |
+ const float *const ir = td->ir[jobnr]; |
|
613 |
+ int *n_clippings = &td->n_clippings[jobnr]; |
|
614 |
+ float *ringbuffer = td->ringbuffer[jobnr]; |
|
615 |
+ float *temp_src = td->temp_src[jobnr]; |
|
616 |
+ const int n_samples = s->sofa.n_samples; /* length of one IR */ |
|
617 |
+ const float *src = (const float *)in->data[0]; /* get pointer to audio input buffer */ |
|
618 |
+ float *dst = (float *)out->data[0]; /* get pointer to audio output buffer */ |
|
619 |
+ int in_channels = in->channels; /* number of input channels */ |
|
620 |
+ /* ring buffer length is: longest IR plus max. delay -> next power of 2 */ |
|
621 |
+ int buffer_length = s->buffer_length; |
|
622 |
+ /* -1 for AND instead of MODULO (applied to powers of 2): */ |
|
623 |
+ uint32_t modulo = (uint32_t)buffer_length - 1; |
|
624 |
+ float *buffer[10]; /* holds ringbuffer for each input channel */ |
|
625 |
+ int wr = *write; |
|
626 |
+ int read; |
|
627 |
+ int i, j, l; |
|
628 |
+ |
|
629 |
+ dst += offset; |
|
630 |
+ for (l = 0; l < in_channels; l++) { |
|
631 |
+ /* get starting address of ringbuffer for each input channel */ |
|
632 |
+ buffer[l] = ringbuffer + l * buffer_length; |
|
633 |
+ } |
|
634 |
+ |
|
635 |
+ for (i = 0; i < in->nb_samples; i++) { |
|
636 |
+ const float *temp_ir = ir; /* using same set of IRs for each sample */ |
|
637 |
+ |
|
638 |
+ *dst = 0; |
|
639 |
+ for (l = 0; l < in_channels; l++) { |
|
640 |
+ /* write current input sample to ringbuffer (for each channel) */ |
|
641 |
+ *(buffer[l] + wr) = src[s->reorder[l]]; |
|
642 |
+ } |
|
643 |
+ |
|
644 |
+ /* loop goes through all channels to be convolved (excl. LFE): */ |
|
645 |
+ for (l = 0; l < s->n_conv; l++) { |
|
646 |
+ const float *const bptr = buffer[l]; |
|
647 |
+ |
|
648 |
+ /* current read position in ringbuffer: input sample write position |
|
649 |
+ * - delay for l-th ch. + diff. betw. IR length and buffer length |
|
650 |
+ * (mod buffer length) */ |
|
651 |
+ read = (wr - *(delay + l) - (n_samples - 1) + buffer_length) & modulo; |
|
652 |
+ |
|
653 |
+ for (j = 0; j < n_samples; j++) |
|
654 |
+ temp_src[j] = bptr[(read + j) & modulo]; |
|
655 |
+ |
|
656 |
+ /* multiply signal and IR, and add up the results */ |
|
657 |
+ dst[0] += s->fdsp->scalarproduct_float(temp_ir, temp_src, n_samples); |
|
658 |
+ temp_ir += n_samples; |
|
659 |
+ } |
|
660 |
+ |
|
661 |
+ if (s->lfe) { /* LFE */ |
|
662 |
+ /* apply gain to LFE signal and add to output buffer */ |
|
663 |
+ *dst += *(buffer[s->n_conv] + wr) * s->gain_lfe; |
|
664 |
+ } |
|
665 |
+ |
|
666 |
+ /* clippings counter */ |
|
667 |
+ if (fabs(*dst) > 1) |
|
668 |
+ *n_clippings += 1; |
|
669 |
+ |
|
670 |
+ /* move output buffer pointer by +2 to get to next sample of processed channel: */ |
|
671 |
+ dst += 2; |
|
672 |
+ src += in_channels; |
|
673 |
+ wr = (wr + 1) & modulo; /* update ringbuffer write position */ |
|
674 |
+ } |
|
675 |
+ |
|
676 |
+ *write = wr; /* remember write position in ringbuffer for next call */ |
|
677 |
+ |
|
678 |
+ return 0; |
|
679 |
+} |
|
680 |
+ |
|
681 |
+static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
|
682 |
+{ |
|
683 |
+ AVFilterContext *ctx = inlink->dst; |
|
684 |
+ SOFAlizerContext *s = ctx->priv; |
|
685 |
+ AVFilterLink *outlink = ctx->outputs[0]; |
|
686 |
+ int n_clippings[2] = { 0 }; |
|
687 |
+ ThreadData td; |
|
688 |
+ AVFrame *out; |
|
689 |
+ |
|
690 |
+ out = ff_get_audio_buffer(outlink, in->nb_samples); |
|
691 |
+ if (!out) { |
|
692 |
+ av_frame_free(&in); |
|
693 |
+ return AVERROR(ENOMEM); |
|
694 |
+ } |
|
695 |
+ av_frame_copy_props(out, in); |
|
696 |
+ |
|
697 |
+ td.in = in; td.out = out; td.write = s->write; |
|
698 |
+ td.delay = s->delay; td.ir = s->data_ir; td.n_clippings = n_clippings; |
|
699 |
+ td.ringbuffer = s->ringbuffer; td.temp_src = s->temp_src; |
|
700 |
+ |
|
701 |
+ ctx->internal->execute(ctx, sofalizer_convolute, &td, NULL, 2); |
|
702 |
+ emms_c(); |
|
703 |
+ |
|
704 |
+ /* display error message if clipping occured */ |
|
705 |
+ if (n_clippings[0] + n_clippings[1] > 0) { |
|
706 |
+ av_log(ctx, AV_LOG_WARNING, "%d of %d samples clipped. Please reduce gain.\n", |
|
707 |
+ n_clippings[0] + n_clippings[1], out->nb_samples * 2); |
|
708 |
+ } |
|
709 |
+ |
|
710 |
+ av_frame_free(&in); |
|
711 |
+ return ff_filter_frame(outlink, out); |
|
712 |
+} |
|
713 |
+ |
|
714 |
+static int query_formats(AVFilterContext *ctx) |
|
715 |
+{ |
|
716 |
+ struct SOFAlizerContext *s = ctx->priv; |
|
717 |
+ AVFilterFormats *formats = NULL; |
|
718 |
+ AVFilterChannelLayouts *layouts = NULL; |
|
719 |
+ int ret, sample_rates[] = { 48000, -1 }; |
|
720 |
+ static const uint64_t channel_layouts[] = { AV_CH_LAYOUT_MONO, |
|
721 |
+ AV_CH_LAYOUT_STEREO, |
|
722 |
+ AV_CH_LAYOUT_2POINT1, |
|
723 |
+ AV_CH_LAYOUT_SURROUND, |
|
724 |
+ AV_CH_LAYOUT_2_1, |
|
725 |
+ AV_CH_LAYOUT_4POINT0, |
|
726 |
+ AV_CH_LAYOUT_QUAD, |
|
727 |
+ AV_CH_LAYOUT_2_2, |
|
728 |
+ AV_CH_LAYOUT_3POINT1, |
|
729 |
+ AV_CH_LAYOUT_5POINT0_BACK, |
|
730 |
+ AV_CH_LAYOUT_5POINT0, |
|
731 |
+ AV_CH_LAYOUT_4POINT1, |
|
732 |
+ AV_CH_LAYOUT_5POINT1_BACK, |
|
733 |
+ AV_CH_LAYOUT_5POINT1, |
|
734 |
+ AV_CH_LAYOUT_6POINT0, |
|
735 |
+ AV_CH_LAYOUT_HEXAGONAL, |
|
736 |
+ AV_CH_LAYOUT_6POINT1, |
|
737 |
+ AV_CH_LAYOUT_6POINT1_BACK, |
|
738 |
+ AV_CH_LAYOUT_7POINT0, |
|
739 |
+ AV_CH_LAYOUT_7POINT1, |
|
740 |
+ AV_CH_LAYOUT_OCTAGONAL, |
|
741 |
+ 0, }; |
|
742 |
+ |
|
743 |
+ ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLT); |
|
744 |
+ if (ret) |
|
745 |
+ return ret; |
|
746 |
+ ret = ff_set_common_formats(ctx, formats); |
|
747 |
+ if (ret) |
|
748 |
+ return ret; |
|
749 |
+ |
|
750 |
+ layouts = ff_make_formatu64_list(channel_layouts); |
|
751 |
+ if (!layouts) |
|
752 |
+ return AVERROR(ENOMEM); |
|
753 |
+ |
|
754 |
+ ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts); |
|
755 |
+ if (ret) |
|
756 |
+ return ret; |
|
757 |
+ |
|
758 |
+ layouts = NULL; |
|
759 |
+ ret = ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO); |
|
760 |
+ if (ret) |
|
761 |
+ return ret; |
|
762 |
+ |
|
763 |
+ ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts); |
|
764 |
+ if (ret) |
|
765 |
+ return ret; |
|
766 |
+ |
|
767 |
+ sample_rates[0] = s->sample_rate; |
|
768 |
+ formats = ff_make_format_list(sample_rates); |
|
769 |
+ if (!formats) |
|
770 |
+ return AVERROR(ENOMEM); |
|
771 |
+ return ff_set_common_samplerates(ctx, formats); |
|
772 |
+} |
|
773 |
+ |
|
774 |
+static int load_data(AVFilterContext *ctx, int azim, int elev, float radius) |
|
775 |
+{ |
|
776 |
+ struct SOFAlizerContext *s = ctx->priv; |
|
777 |
+ const int n_samples = s->sofa.n_samples; |
|
778 |
+ int n_conv = s->n_conv; /* no. channels to convolve (excl. LFE) */ |
|
779 |
+ int delay_l[10]; /* broadband delay for each IR */ |
|
780 |
+ int delay_r[10]; |
|
781 |
+ int nb_input_channels = ctx->inputs[0]->channels; /* no. input channels */ |
|
782 |
+ float gain_lin = expf((s->gain - 3 * nb_input_channels) / 20 * M_LN10); /* gain - 3dB/channel */ |
|
783 |
+ float *data_ir_l = NULL; |
|
784 |
+ float *data_ir_r = NULL; |
|
785 |
+ int offset = 0; /* used for faster pointer arithmetics in for-loop */ |
|
786 |
+ int m[s->n_conv]; /* measurement index m of IR closest to required source positions */ |
|
787 |
+ int i, j, azim_orig = azim; |
|
788 |
+ |
|
789 |
+ if (!s->sofa.ncid) { /* if an invalid SOFA file has been selected */ |
|
790 |
+ av_log(ctx, AV_LOG_ERROR, "Selected SOFA file is invalid. Please select valid SOFA file.\n"); |
|
791 |
+ return AVERROR_INVALIDDATA; |
|
792 |
+ } |
|
793 |
+ |
|
794 |
+ /* get temporary IR for L and R channel */ |
|
795 |
+ data_ir_l = av_malloc_array(n_conv * n_samples, sizeof(*data_ir_l)); |
|
796 |
+ data_ir_r = av_malloc_array(n_conv * n_samples, sizeof(*data_ir_r)); |
|
797 |
+ if (!data_ir_r || !data_ir_l) { |
|
798 |
+ av_free(data_ir_l); |
|
799 |
+ av_free(data_ir_r); |
|
800 |
+ return AVERROR(ENOMEM); |
|
801 |
+ } |
|
802 |
+ |
|
803 |
+ for (i = 0; i < s->n_conv; i++) { |
|
804 |
+ /* load and store IRs and corresponding delays */ |
|
805 |
+ azim = (int)(s->speaker_pos[i] + azim_orig) % 360; |
|
806 |
+ /* get id of IR closest to desired position */ |
|
807 |
+ m[i] = find_m(s, azim, elev, radius); |
|
808 |
+ |
|
809 |
+ /* load the delays associated with the current IRs */ |
|
810 |
+ delay_l[i] = *(s->sofa.data_delay + 2 * m[i]); |
|
811 |
+ delay_r[i] = *(s->sofa.data_delay + 2 * m[i] + 1); |
|
812 |
+ |
|
813 |
+ offset = i * n_samples; /* no. samples already written */ |
|
814 |
+ for (j = 0; j < n_samples; j++) { |
|
815 |
+ /* load reversed IRs of the specified source position |
|
816 |
+ * sample-by-sample for left and right ear; and apply gain */ |
|
817 |
+ *(data_ir_l + offset + j) = /* left channel */ |
|
818 |
+ *(s->sofa.data_ir + 2 * m[i] * n_samples + n_samples - 1 - j) * gain_lin; |
|
819 |
+ *(data_ir_r + offset + j) = /* right channel */ |
|
820 |
+ *(s->sofa.data_ir + 2 * m[i] * n_samples + n_samples - 1 - j + n_samples) * gain_lin; |
|
821 |
+ } |
|
822 |
+ |
|
823 |
+ av_log(ctx, AV_LOG_DEBUG, "Index: %d, Azimuth: %f, Elevation: %f, Radius: %f of SOFA file.\n", |
|
824 |
+ m[i], *(s->sofa.sp_a + m[i]), *(s->sofa.sp_e + m[i]), *(s->sofa.sp_r + m[i])); |
|
825 |
+ } |
|
826 |
+ |
|
827 |
+ /* copy IRs and delays to allocated memory in the SOFAlizerContext struct: */ |
|
828 |
+ memcpy(s->data_ir[0], data_ir_l, sizeof(float) * n_conv * n_samples); |
|
829 |
+ memcpy(s->data_ir[1], data_ir_r, sizeof(float) * n_conv * n_samples); |
|
830 |
+ |
|
831 |
+ av_free(data_ir_l); /* free temporary IR memory */ |
|
832 |
+ av_free(data_ir_r); |
|
833 |
+ |
|
834 |
+ memcpy(s->delay[0], &delay_l[0], sizeof(int) * s->n_conv); |
|
835 |
+ memcpy(s->delay[1], &delay_r[0], sizeof(int) * s->n_conv); |
|
836 |
+ |
|
837 |
+ return 0; |
|
838 |
+} |
|
839 |
+ |
|
840 |
+static av_cold int init(AVFilterContext *ctx) |
|
841 |
+{ |
|
842 |
+ SOFAlizerContext *s = ctx->priv; |
|
843 |
+ int ret; |
|
844 |
+ |
|
845 |
+ /* load SOFA file, */ |
|
846 |
+ /* initialize file IDs to 0 before attempting to load SOFA files, |
|
847 |
+ * this assures that in case of error, only the memory of already |
|
848 |
+ * loaded files is free'd */ |
|
849 |
+ s->sofa.ncid = 0; |
|
850 |
+ ret = load_sofa(ctx, s->filename, &s->sample_rate); |
|
851 |
+ if (ret) { |
|
852 |
+ /* file loading error */ |
|
853 |
+ av_log(ctx, AV_LOG_ERROR, "Error while loading SOFA file: '%s'\n", s->filename); |
|
854 |
+ } else { /* no file loading error, resampling not required */ |
|
855 |
+ av_log(ctx, AV_LOG_DEBUG, "File '%s' loaded.\n", s->filename); |
|
856 |
+ } |
|
857 |
+ |
|
858 |
+ if (ret) { |
|
859 |
+ av_log(ctx, AV_LOG_ERROR, "No valid SOFA file could be loaded. Please specify valid SOFA file.\n"); |
|
860 |
+ return ret; |
|
861 |
+ } |
|
862 |
+ |
|
863 |
+ s->fdsp = avpriv_float_dsp_alloc(0); |
|
864 |
+ if (!s->fdsp) |
|
865 |
+ return AVERROR(ENOMEM); |
|
866 |
+ |
|
867 |
+ return 0; |
|
868 |
+} |
|
869 |
+ |
|
870 |
+static inline unsigned clz(unsigned x) |
|
871 |
+{ |
|
872 |
+ unsigned i = sizeof(x) * 8; |
|
873 |
+ |
|
874 |
+ while (x) { |
|
875 |
+ x >>= 1; |
|
876 |
+ i--; |
|
877 |
+ } |
|
878 |
+ |
|
879 |
+ return i; |
|
880 |
+} |
|
881 |
+ |
|
882 |
+static int config_input(AVFilterLink *inlink) |
|
883 |
+{ |
|
884 |
+ AVFilterContext *ctx = inlink->dst; |
|
885 |
+ SOFAlizerContext *s = ctx->priv; |
|
886 |
+ int nb_input_channels = inlink->channels; /* no. input channels */ |
|
887 |
+ int n_max_ir = 0; |
|
888 |
+ int n_current; |
|
889 |
+ int n_max = 0; |
|
890 |
+ int ret; |
|
891 |
+ |
|
892 |
+ /* gain -3 dB per channel, -6 dB to get LFE on a similar level */ |
|
893 |
+ s->gain_lfe = expf((s->gain - 3 * inlink->channels - 6) / 20 * M_LN10); |
|
894 |
+ |
|
895 |
+ s->lfe = !!(inlink->channel_layout & AV_CH_LOW_FREQUENCY); |
|
896 |
+ /* LFE is an input channel but requires no convolution */ |
|
897 |
+ s->n_conv = nb_input_channels - s->lfe; |
|
898 |
+ |
|
899 |
+ /* get size of ringbuffer (longest IR plus max. delay) */ |
|
900 |
+ /* then choose next power of 2 for performance optimization */ |
|
901 |
+ n_current = s->sofa.n_samples + max_delay(&s->sofa); |
|
902 |
+ if (n_current > n_max) { |
|
903 |
+ /* length of longest IR plus max. delay (in all SOFA files) */ |
|
904 |
+ n_max = n_current; |
|
905 |
+ /* length of longest IR (without delay, in all SOFA files) */ |
|
906 |
+ n_max_ir = s->sofa.n_samples; |
|
907 |
+ } |
|
908 |
+ /* buffer length is longest IR plus max. delay -> next power of 2 |
|
909 |
+ (32 - count leading zeros gives required exponent) */ |
|
910 |
+ s->buffer_length = exp2(32 - clz((uint32_t)n_max)); |
|
911 |
+ |
|
912 |
+ /* Allocate memory for the impulse responses, delays and the ringbuffers */ |
|
913 |
+ /* size: (longest IR) * (number of channels to convolute), without LFE */ |
|
914 |
+ s->data_ir[0] = av_malloc_array(n_max_ir, sizeof(float) * s->n_conv); |
|
915 |
+ s->data_ir[1] = av_malloc_array(n_max_ir, sizeof(float) * s->n_conv); |
|
916 |
+ /* length: number of channels to convolute */ |
|
917 |
+ s->delay[0] = av_malloc_array(s->n_conv, sizeof(float)); |
|
918 |
+ s->delay[1] = av_malloc_array(s->n_conv, sizeof(float)); |
|
919 |
+ /* length: (buffer length) * (number of input channels), |
|
920 |
+ * OR: buffer length (if frequency domain processing) |
|
921 |
+ * calloc zero-initializes the buffer */ |
|
922 |
+ s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels); |
|
923 |
+ s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels); |
|
924 |
+ /* length: number of channels to convolute */ |
|
925 |
+ s->speaker_pos = av_malloc_array(s->n_conv, sizeof(*s->speaker_pos)); |
|
926 |
+ |
|
927 |
+ /* memory allocation failed: */ |
|
928 |
+ if (!s->data_ir[0] || !s->data_ir[1] || !s->delay[1] || |
|
929 |
+ !s->delay[0] || !s->ringbuffer[0] || !s->ringbuffer[1] || |
|
930 |
+ !s->speaker_pos) |
|
931 |
+ return AVERROR(ENOMEM); |
|
932 |
+ |
|
933 |
+ compensate_volume(ctx); |
|
934 |
+ |
|
935 |
+ /* get speaker positions */ |
|
936 |
+ if ((ret = get_speaker_pos(ctx, s->speaker_pos)) < 0) { |
|
937 |
+ av_log(ctx, AV_LOG_ERROR, "Couldn't get speaker positions. Input channel configuration not supported.\n"); |
|
938 |
+ return ret; |
|
939 |
+ } |
|
940 |
+ /* load IRs to data_ir[0] and data_ir[1] for required directions */ |
|
941 |
+ /* only load IRs if time-domain convolution is used. */ |
|
942 |
+ if ((ret = load_data(ctx, s->rotation, s->elevation, s->radius)) < 0) |
|
943 |
+ return ret; |
|
944 |
+ |
|
945 |
+ av_log(ctx, AV_LOG_DEBUG, "Samplerate: %d Channels to convolute: %d, Length of ringbuffer: %d x %d\n", |
|
946 |
+ inlink->sample_rate, s->n_conv, nb_input_channels, s->buffer_length); |
|
947 |
+ |
|
948 |
+ return 0; |
|
949 |
+} |
|
950 |
+ |
|
951 |
+static av_cold void uninit(AVFilterContext *ctx) |
|
952 |
+{ |
|
953 |
+ SOFAlizerContext *s = ctx->priv; |
|
954 |
+ |
|
955 |
+ if (s->sofa.ncid) { |
|
956 |
+ av_freep(&s->sofa.sp_a); |
|
957 |
+ av_freep(&s->sofa.sp_e); |
|
958 |
+ av_freep(&s->sofa.sp_r); |
|
959 |
+ av_freep(&s->sofa.data_delay); |
|
960 |
+ av_freep(&s->sofa.data_ir); |
|
961 |
+ } |
|
962 |
+ av_freep(&s->delay[0]); |
|
963 |
+ av_freep(&s->delay[1]); |
|
964 |
+ av_freep(&s->data_ir[0]); |
|
965 |
+ av_freep(&s->data_ir[1]); |
|
966 |
+ av_freep(&s->ringbuffer[0]); |
|
967 |
+ av_freep(&s->ringbuffer[1]); |
|
968 |
+ av_freep(&s->speaker_pos); |
|
969 |
+ av_freep(&s->temp_src[0]); |
|
970 |
+ av_freep(&s->temp_src[1]); |
|
971 |
+ av_freep(&s->fdsp); |
|
972 |
+} |
|
973 |
+ |
|
974 |
+#define OFFSET(x) offsetof(SOFAlizerContext, x) |
|
975 |
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
|
976 |
+ |
|
977 |
+static const AVOption sofalizer_options[] = { |
|
978 |
+ { "sofa", "sofa filename", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS }, |
|
979 |
+ { "gain", "set gain in dB", OFFSET(gain), AV_OPT_TYPE_FLOAT, {.dbl=0}, -20, 40, .flags = FLAGS }, |
|
980 |
+ { "rotation", "set rotation" , OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl=0}, -360, 360, .flags = FLAGS }, |
|
981 |
+ { "elevation", "set elevation", OFFSET(elevation), AV_OPT_TYPE_FLOAT, {.dbl=0}, -90, 90, .flags = FLAGS }, |
|
982 |
+ { "radius", "set radius", OFFSET(radius), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 3, .flags = FLAGS }, |
|
983 |
+ { NULL } |
|
984 |
+}; |
|
985 |
+ |
|
986 |
+AVFILTER_DEFINE_CLASS(sofalizer); |
|
987 |
+ |
|
988 |
+static const AVFilterPad inputs[] = { |
|
989 |
+ { |
|
990 |
+ .name = "default", |
|
991 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
992 |
+ .config_props = config_input, |
|
993 |
+ .filter_frame = filter_frame, |
|
994 |
+ }, |
|
995 |
+ { NULL } |
|
996 |
+}; |
|
997 |
+ |
|
998 |
+static const AVFilterPad outputs[] = { |
|
999 |
+ { |
|
1000 |
+ .name = "default", |
|
1001 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
1002 |
+ }, |
|
1003 |
+ { NULL } |
|
1004 |
+}; |
|
1005 |
+ |
|
1006 |
+AVFilter ff_af_sofalizer = { |
|
1007 |
+ .name = "sofalizer", |
|
1008 |
+ .description = NULL_IF_CONFIG_SMALL("SOFAlizer (Spatially Oriented Format for Acoustics)."), |
|
1009 |
+ .priv_size = sizeof(SOFAlizerContext), |
|
1010 |
+ .priv_class = &sofalizer_class, |
|
1011 |
+ .init = init, |
|
1012 |
+ .uninit = uninit, |
|
1013 |
+ .query_formats = query_formats, |
|
1014 |
+ .inputs = inputs, |
|
1015 |
+ .outputs = outputs, |
|
1016 |
+ .flags = AVFILTER_FLAG_SLICE_THREADS, |
|
1017 |
+}; |
... | ... |
@@ -109,6 +109,7 @@ void avfilter_register_all(void) |
109 | 109 |
REGISTER_FILTER(SIDECHAINGATE, sidechaingate, af); |
110 | 110 |
REGISTER_FILTER(SILENCEDETECT, silencedetect, af); |
111 | 111 |
REGISTER_FILTER(SILENCEREMOVE, silenceremove, af); |
112 |
+ REGISTER_FILTER(SOFALIZER, sofalizer, af); |
|
112 | 113 |
REGISTER_FILTER(STEREOTOOLS, stereotools, af); |
113 | 114 |
REGISTER_FILTER(STEREOWIDEN, stereowiden, af); |
114 | 115 |
REGISTER_FILTER(TREBLE, treble, af); |
... | ... |
@@ -289,6 +289,17 @@ AVFilterFormats *ff_make_format_list(const int *fmts) |
289 | 289 |
return formats; |
290 | 290 |
} |
291 | 291 |
|
292 |
+AVFilterChannelLayouts *ff_make_formatu64_list(const uint64_t *fmts) |
|
293 |
+{ |
|
294 |
+ MAKE_FORMAT_LIST(AVFilterChannelLayouts, |
|
295 |
+ channel_layouts, nb_channel_layouts); |
|
296 |
+ if (count) |
|
297 |
+ memcpy(formats->channel_layouts, fmts, |
|
298 |
+ sizeof(*formats->channel_layouts) * count); |
|
299 |
+ |
|
300 |
+ return formats; |
|
301 |
+} |
|
302 |
+ |
|
292 | 303 |
AVFilterChannelLayouts *avfilter_make_format64_list(const int64_t *fmts) |
293 | 304 |
{ |
294 | 305 |
MAKE_FORMAT_LIST(AVFilterChannelLayouts, |
... | ... |
@@ -141,6 +141,9 @@ AVFilterChannelLayouts *ff_all_channel_counts(void); |
141 | 141 |
av_warn_unused_result |
142 | 142 |
AVFilterChannelLayouts *avfilter_make_format64_list(const int64_t *fmts); |
143 | 143 |
|
144 |
+av_warn_unused_result |
|
145 |
+AVFilterChannelLayouts *ff_make_formatu64_list(const uint64_t *fmts); |
|
146 |
+ |
|
144 | 147 |
|
145 | 148 |
/** |
146 | 149 |
* A helper for query_formats() which sets all links to the same list of channel |
... | ... |
@@ -30,7 +30,7 @@ |
30 | 30 |
#include "libavutil/version.h" |
31 | 31 |
|
32 | 32 |
#define LIBAVFILTER_VERSION_MAJOR 6 |
33 |
-#define LIBAVFILTER_VERSION_MINOR 20 |
|
33 |
+#define LIBAVFILTER_VERSION_MINOR 21 |
|
34 | 34 |
#define LIBAVFILTER_VERSION_MICRO 100 |
35 | 35 |
|
36 | 36 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |