36 #define FREQUENCY_DOMAIN 1
99 char *
arg, *tokenizer, *p;
100 uint64_t used_channels = 0;
104 uint64_t out_channel;
111 if (used_channels & out_channel) {
115 used_channels |= out_channel;
116 s->mapping[
s->nb_irs] = out_channel;
121 s->nb_hrir_inputs = 1;
123 s->nb_hrir_inputs =
s->nb_irs;
143 int *write = &
td->write[jobnr];
144 const float *
const ir =
td->ir[jobnr];
145 int *n_clippings = &
td->n_clippings[jobnr];
146 float *ringbuffer =
td->ringbuffer[jobnr];
147 float *temp_src =
td->temp_src[jobnr];
148 const int ir_len =
s->ir_len;
149 const int air_len =
s->air_len;
150 const float *
src = (
const float *)
in->data[0];
151 float *dst = (
float *)
out->data[0];
153 const int buffer_length =
s->buffer_length;
154 const uint32_t modulo = (uint32_t)buffer_length - 1;
161 for (l = 0; l < in_channels; l++) {
162 buffer[l] = ringbuffer + l * buffer_length;
165 for (
i = 0;
i <
in->nb_samples;
i++) {
166 const float *cur_ir = ir;
169 for (l = 0; l < in_channels; l++) {
173 for (l = 0; l < in_channels; cur_ir += air_len, l++) {
174 const float *
const bptr =
buffer[l];
176 if (l ==
s->lfe_channel) {
177 *dst += *(
buffer[
s->lfe_channel] + wr) *
s->gain_lfe;
181 read = (wr - (ir_len - 1)) & modulo;
183 if (read + ir_len < buffer_length) {
184 memcpy(temp_src, bptr + read, ir_len *
sizeof(*temp_src));
186 int len =
FFMIN(air_len - (read % ir_len), buffer_length - read);
188 memcpy(temp_src, bptr + read,
len *
sizeof(*temp_src));
189 memcpy(temp_src +
len, bptr, (air_len -
len) *
sizeof(*temp_src));
192 dst[0] +=
s->scalarproduct_float(cur_ir, temp_src,
FFALIGN(ir_len, 32));
195 if (
fabsf(dst[0]) > 1)
200 wr = (wr + 1) & modulo;
214 int *write = &
td->write[jobnr];
216 int *n_clippings = &
td->n_clippings[jobnr];
217 float *ringbuffer =
td->ringbuffer[jobnr];
218 const int ir_len =
s->ir_len;
219 const float *
src = (
const float *)
in->data[0];
220 float *dst = (
float *)
out->data[0];
222 const int buffer_length =
s->buffer_length;
223 const uint32_t modulo = (uint32_t)buffer_length - 1;
228 const int n_fft =
s->n_fft;
229 const float fft_scale = 1.0f /
s->n_fft;
237 n_read =
FFMIN(ir_len,
in->nb_samples);
238 for (j = 0; j < n_read; j++) {
239 dst[2 * j] = ringbuffer[wr];
240 ringbuffer[wr] = 0.0;
241 wr = (wr + 1) & modulo;
244 for (j = n_read; j <
in->nb_samples; j++) {
248 memset(fft_acc, 0,
sizeof(
FFTComplex) * n_fft);
250 for (
i = 0;
i < in_channels;
i++) {
251 if (
i ==
s->lfe_channel) {
252 for (j = 0; j <
in->nb_samples; j++) {
253 dst[2 * j] +=
src[
i + j * in_channels] *
s->gain_lfe;
259 hrtf_offset = hrtf +
offset;
261 memset(fft_in, 0,
sizeof(
FFTComplex) * n_fft);
263 for (j = 0; j <
in->nb_samples; j++) {
264 fft_in[j].
re =
src[j * in_channels +
i];
269 for (j = 0; j < n_fft; j++) {
271 const float re = fft_in[j].
re;
272 const float im = fft_in[j].
im;
274 fft_acc[j].
re +=
re * hcomplex->
re -
im * hcomplex->
im;
275 fft_acc[j].
im +=
re * hcomplex->
im +
im * hcomplex->
re;
282 for (j = 0; j <
in->nb_samples; j++) {
283 dst[2 * j] += fft_acc[j].
re * fft_scale;
284 if (
fabsf(dst[2 * j]) > 1)
288 for (j = 0; j < ir_len - 1; j++) {
289 int write_pos = (wr + j) & modulo;
291 *(ringbuffer + write_pos) += fft_acc[
in->nb_samples + j].
re * fft_scale;
303 int ir_len, max_ir_len;
307 if (ir_len > max_ir_len) {
311 s->hrir_in[input_number].ir_len = ir_len;
312 s->ir_len =
FFMAX(ir_len,
s->ir_len);
320 int n_clippings[2] = { 0 };
332 td.ir =
s->data_ir;
td.n_clippings = n_clippings;
333 td.ringbuffer =
s->ringbuffer;
td.temp_src =
s->temp_src;
334 td.temp_fft =
s->temp_fft;
335 td.temp_afft =
s->temp_afft;
344 if (n_clippings[0] + n_clippings[1] > 0) {
346 n_clippings[0] + n_clippings[1],
out->nb_samples * 2);
357 int nb_input_channels =
ctx->inputs[0]->channels;
358 float gain_lin =
expf((
s->gain - 3 * nb_input_channels) / 20 *
M_LN10);
368 s->buffer_length = 1 << (32 -
ff_clz(
s->air_len));
377 if (!
s->fft[0] || !
s->fft[1] || !
s->ifft[0] || !
s->ifft[1]) {
385 s->ringbuffer[0] =
av_calloc(
s->buffer_length,
sizeof(
float) * nb_input_channels);
386 s->ringbuffer[1] =
av_calloc(
s->buffer_length,
sizeof(
float) * nb_input_channels);
388 s->ringbuffer[0] =
av_calloc(
s->buffer_length,
sizeof(
float));
389 s->ringbuffer[1] =
av_calloc(
s->buffer_length,
sizeof(
float));
394 if (!
s->temp_fft[0] || !
s->temp_fft[1] ||
395 !
s->temp_afft[0] || !
s->temp_afft[1]) {
401 if (!
s->ringbuffer[0] || !
s->ringbuffer[1]) {
407 s->temp_src[0] =
av_calloc(
s->air_len,
sizeof(
float));
408 s->temp_src[1] =
av_calloc(
s->air_len,
sizeof(
float));
410 s->data_ir[0] =
av_calloc(nb_input_channels *
s->air_len,
sizeof(*
s->data_ir[0]));
411 s->data_ir[1] =
av_calloc(nb_input_channels *
s->air_len,
sizeof(*
s->data_ir[1]));
412 if (!
s->data_ir[0] || !
s->data_ir[1] || !
s->temp_src[0] || !
s->temp_src[1]) {
417 s->data_hrtf[0] =
av_calloc(
n_fft,
sizeof(*
s->data_hrtf[0]) * nb_input_channels);
418 s->data_hrtf[1] =
av_calloc(
n_fft,
sizeof(*
s->data_hrtf[1]) * nb_input_channels);
419 if (!
s->data_hrtf[0] || !
s->data_hrtf[1]) {
426 int len =
s->hrir_in[
i].ir_len;
440 float *data_ir_l =
s->data_ir[0] + idx *
s->air_len;
441 float *data_ir_r =
s->data_ir[1] + idx *
s->air_len;
443 for (j = 0; j <
len; j++) {
444 data_ir_l[j] = ptr[
len * 2 - j * 2 - 2] * gain_lin;
445 data_ir_r[j] = ptr[
len * 2 - j * 2 - 1] * gain_lin;
451 for (j = 0; j <
len; j++) {
452 fft_in_l[j].
re = ptr[j * 2 ] * gain_lin;
453 fft_in_r[j].
re = ptr[j * 2 + 1] * gain_lin;
462 int I,
N =
ctx->inputs[1]->channels;
464 for (k = 0; k <
N / 2; k++) {
472 float *data_ir_l =
s->data_ir[0] + idx *
s->air_len;
473 float *data_ir_r =
s->data_ir[1] + idx *
s->air_len;
475 for (j = 0; j <
len; j++) {
476 data_ir_l[j] = ptr[
len *
N - j *
N -
N + I ] * gain_lin;
477 data_ir_r[j] = ptr[
len *
N - j *
N -
N + I + 1] * gain_lin;
483 for (j = 0; j <
len; j++) {
484 fft_in_l[j].
re = ptr[j *
N + I ] * gain_lin;
485 fft_in_r[j].
re = ptr[j *
N + I + 1] * gain_lin;
514 for (
i = 0;
i <
s->nb_hrir_inputs;
i++) {
517 if (
s->hrir_in[
i].eof)
526 "HRIR stream %d.\n",
i);
529 s->hrir_in[
i].eof = 1;
543 }
else if (!
s->have_hrirs)
601 for (
i = 1;
i <=
s->nb_hrir_inputs;
i++) {
649 for (
i = 0;
i <
s->nb_hrir_inputs;
i++) {
715 for (
unsigned i = 1;
i <
ctx->nb_inputs;
i++)
719 #define OFFSET(x) offsetof(HeadphoneContext, x)
720 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
749 .description =
NULL_IF_CONFIG_SMALL(
"Apply headphone binaural spatialization with HRTFs in additional streams."),
751 .priv_class = &headphone_class,
static const AVFilterPad inputs[]
static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink)
static int headphone_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static int headphone_frame(HeadphoneContext *s, AVFrame *in, AVFilterLink *outlink)
static int query_formats(AVFilterContext *ctx)
static int config_input(AVFilterLink *inlink)
static int parse_channel_name(const char *arg, uint64_t *rchannel)
static const AVFilterPad outputs[]
static const AVOption headphone_options[]
static int check_ir(AVFilterLink *inlink, int input_number)
static int activate(AVFilterContext *ctx)
static av_cold int init(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static void parse_map(AVFilterContext *ctx)
static int config_output(AVFilterLink *outlink)
AVFILTER_DEFINE_CLASS(headphone)
static int headphone_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
int ff_inlink_queued_samples(AVFilterLink *link)
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Main libavfilter public API header.
char * av_asprintf(const char *fmt,...)
#define flags(name, subs,...)
audio channel layout utility functions
static __device__ float fabsf(float a)
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
static int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
int av_get_channel_layout_channel_index(uint64_t channel_layout, uint64_t channel)
Get the index of a channel in channel_layout.
uint64_t av_get_channel_layout(const char *name)
Return a channel layout id that matches name, or 0 if no match is found.
#define AV_CH_LAYOUT_STEREO
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
#define AV_CH_LOW_FREQUENCY
void av_fft_permute(FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling ff_fft_calc().
void av_fft_calc(FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in av_fft_init().
FFTContext * av_fft_init(int nbits, int inverse)
Set up a complex FFT.
av_cold void av_fft_end(FFTContext *s)
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AVERROR_EOF
End of file.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
const VDPAUPixFmtMap * map
static int ff_insert_inpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new input pad for the filter.
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
enum MovChannelLayoutTag * layouts
Describe the class of an AVClass context structure.
A list of supported channel layouts.
A link between two filters.
int channels
Number of channels.
AVFilterContext * src
source filter
uint64_t channel_layout
channel layout of current buffer (see libavutil/channel_layout.h)
AVFilterContext * dst
dest filter
A filter pad used for either input or output.
const char * name
Pad name.
const char * name
Filter name.
float(* scalarproduct_float)(const float *v1, const float *v2, int len)
Calculate the scalar product of two vectors of floats.
AVFormatInternal * internal
An opaque field for libavformat internal usage.
This structure describes decoded (raw) audio or video data.
uint8_t ** extended_data
pointers to the data planes/channels.
FFTComplex * data_hrtf[2]
struct HeadphoneContext::hrir_inputs hrir_in[64]
FFTComplex * temp_afft[2]
float(* scalarproduct_float)(const float *v1, const float *v2, int len)
Used for passing data between threads.
static const uint8_t offset[127][2]