36 #define FREQUENCY_DOMAIN 1 93 int len, i, channel_id = 0;
96 if (sscanf(*arg,
"%7[A-Z]%n", buf, &len)) {
100 for (i = 32; i > 0; i >>= 1) {
101 if (layout >= 1LL << i) {
106 if (channel_id >= 64 || layout0 != 1LL << channel_id)
108 *rchannel = channel_id;
128 for (i = 0; i < 64; i++) {
132 while ((arg =
av_strtok(p,
"|", &tokenizer))) {
172 const float *
const ir = td->
ir[jobnr];
177 const float *
src = (
const float *)in->
data[0];
178 float *dst = (
float *)
out->data[0];
179 const int in_channels = in->
channels;
181 const uint32_t modulo = (uint32_t)buffer_length - 1;
188 for (l = 0; l < in_channels; l++) {
193 const float *temp_ir = ir;
196 for (l = 0; l < in_channels; l++) {
197 *(buffer[l] + wr) = src[l];
200 for (l = 0; l < in_channels; l++) {
201 const float *
const bptr = buffer[l];
205 temp_ir +=
FFALIGN(ir_len, 16);
209 read = (wr - *(delay + l) - (ir_len - 1) +
buffer_length) & modulo;
211 if (read + ir_len < buffer_length) {
212 memcpy(temp_src, bptr + read, ir_len *
sizeof(*temp_src));
214 int len =
FFMIN(ir_len - (read % ir_len), buffer_length - read);
216 memcpy(temp_src, bptr + read, len *
sizeof(*temp_src));
217 memcpy(temp_src + len, bptr, (ir_len - len) *
sizeof(*temp_src));
221 temp_ir +=
FFALIGN(ir_len, 16);
229 wr = (wr + 1) & modulo;
248 const float *
src = (
const float *)in->
data[0];
249 float *dst = (
float *)
out->data[0];
250 const int in_channels = in->
channels;
252 const uint32_t modulo = (uint32_t)buffer_length - 1;
257 const float fft_scale = 1.0f / s->
n_fft;
266 for (j = 0; j < n_read; j++) {
267 dst[2 * j] = ringbuffer[wr];
268 ringbuffer[wr] = 0.0;
269 wr = (wr + 1) & modulo;
276 for (i = 0; i < in_channels; i++) {
279 dst[2 * j] += src[i + j * in_channels] * s->
gain_lfe;
285 hrtf_offset = hrtf +
offset;
287 memset(fft_in, 0,
sizeof(
FFTComplex) * n_fft);
290 fft_in[j].
re = src[j * in_channels + i];
295 for (j = 0; j <
n_fft; j++) {
297 const float re = fft_in[j].
re;
298 const float im = fft_in[j].
im;
300 fft_in[j].
re = re * hcomplex->
re - im * hcomplex->
im;
301 fft_in[j].
im = re * hcomplex->
im + im * hcomplex->
re;
308 dst[2 * j] += fft_in[j].
re * fft_scale;
311 for (j = 0; j < ir_len - 1; j++) {
312 int write_pos = (wr + j) & modulo;
314 *(ringbuffer + write_pos) += fft_in[in->
nb_samples + j].
re * fft_scale;
318 for (i = 0; i <
out->nb_samples; i++) {
319 if (fabs(*dst) > 1) {
335 int ir_len, max_ir_len, input_number, ret;
337 for (input_number = 0; input_number < s->
nb_inputs; input_number++)
338 if (inlink == ctx->
inputs[input_number])
350 if (ir_len > max_ir_len) {
364 int n_clippings[2] = { 0 };
389 if (n_clippings[0] + n_clippings[1] > 0) {
391 n_clippings[0] + n_clippings[1], out->
nb_samples * 2);
404 float gain_lin =
expf((s->
gain - 3 * nb_input_channels) / 20 *
M_LN10);
409 float *data_ir_l =
NULL;
410 float *data_ir_r =
NULL;
419 fft_in_l =
av_calloc(n_fft,
sizeof(*fft_in_l));
420 fft_in_r =
av_calloc(n_fft,
sizeof(*fft_in_r));
421 if (!fft_in_l || !fft_in_r) {
491 data_hrtf_l =
av_calloc(n_fft,
sizeof(*data_hrtf_l) * nb_irs);
492 data_hrtf_r =
av_calloc(n_fft,
sizeof(*data_hrtf_r) * nb_irs);
493 if (!data_hrtf_r || !data_hrtf_l) {
511 for (j = 0; j < inlink->
channels; j++) {
525 offset = idx *
FFALIGN(len, 16);
526 for (j = 0; j <
len; j++) {
527 data_ir_l[offset + j] = ptr[len * 2 - j * 2 - 2] * gain_lin;
528 data_ir_r[offset + j] = ptr[len * 2 - j * 2 - 1] * gain_lin;
531 memset(fft_in_l, 0, n_fft *
sizeof(*fft_in_l));
532 memset(fft_in_r, 0, n_fft *
sizeof(*fft_in_r));
534 offset = idx *
n_fft;
535 for (j = 0; j <
len; j++) {
536 fft_in_l[delay_l + j].
re = ptr[j * 2 ] * gain_lin;
537 fft_in_r[delay_r + j].
re = ptr[j * 2 + 1] * gain_lin;
542 memcpy(data_hrtf_l + offset, fft_in_l, n_fft *
sizeof(*fft_in_l));
545 memcpy(data_hrtf_r + offset, fft_in_r, n_fft *
sizeof(*fft_in_r));
550 for (k = 0; k < N / 2; k++) {
553 for (j = 0; j < inlink->
channels; j++) {
568 offset = idx *
FFALIGN(len, 16);
569 for (j = 0; j <
len; j++) {
570 data_ir_l[offset + j] = ptr[len * N - j * N - N + I ] * gain_lin;
571 data_ir_r[offset + j] = ptr[len * N - j * N - N + I + 1] * gain_lin;
574 memset(fft_in_l, 0, n_fft *
sizeof(*fft_in_l));
575 memset(fft_in_r, 0, n_fft *
sizeof(*fft_in_r));
577 offset = idx *
n_fft;
578 for (j = 0; j <
len; j++) {
579 fft_in_l[delay_l + j].
re = ptr[j * N + I ] * gain_lin;
580 fft_in_r[delay_r + j].
re = ptr[j * N + I + 1] * gain_lin;
585 memcpy(data_hrtf_l + offset, fft_in_l, n_fft *
sizeof(*fft_in_l));
588 memcpy(data_hrtf_r + offset, fft_in_r, n_fft *
sizeof(*fft_in_r));
595 memcpy(s->
data_ir[0], data_ir_l,
sizeof(
float) * nb_irs *
FFALIGN(ir_len, 16));
596 memcpy(s->
data_ir[1], data_ir_r,
sizeof(
float) * nb_irs *
FFALIGN(ir_len, 16));
879 #define OFFSET(x) offsetof(HeadphoneContext, x) 880 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM 910 .description =
NULL_IF_CONFIG_SMALL(
"Apply headphone binaural spatialization with HRTFs in additional streams."),
912 .priv_class = &headphone_class,
float(* scalarproduct_float)(const float *v1, const float *v2, int len)
Calculate the scalar product of two vectors of floats.
static int config_output(AVFilterLink *outlink)
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
This structure describes decoded (raw) audio or video data.
av_cold void av_fft_end(FFTContext *s)
static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink)
#define AV_LOG_WARNING
Something somehow does not look correct.
Main libavfilter public API header.
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
void av_fft_permute(FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling ff_fft_calc().
#define AV_CH_LAYOUT_STEREO
struct AVFilterChannelLayouts * in_channel_layouts
static int read_ir(AVFilterLink *inlink, AVFrame *frame)
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
const char * name
Pad name.
uint64_t av_get_channel_layout(const char *name)
Return a channel layout id that matches name, or 0 if no match is found.
AVFilterLink ** inputs
array of pointers to input links
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static int parse_channel_name(HeadphoneContext *s, int x, char **arg, int *rchannel, char *buf)
static int request_frame(AVFilterLink *outlink)
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
#define AV_CH_LOW_FREQUENCY
#define AVERROR_EOF
End of file.
static int config_input(AVFilterLink *inlink)
A filter pad used for either input or output.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
A link between two filters.
AVFilterPad * input_pads
array of input pads
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
static void parse_map(AVFilterContext *ctx)
static const AVFilterPad outputs[]
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link...
struct AVFilterChannelLayouts * out_channel_layouts
FFTContext * av_fft_init(int nbits, int inverse)
Set up a complex FFT.
static const uint8_t offset[127][2]
static av_cold void uninit(AVFilterContext *ctx)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Context for an Audio FIFO Buffer.
AVFILTER_DEFINE_CLASS(headphone)
char * av_asprintf(const char *fmt,...)
static int headphone_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
FFTComplex * data_hrtf[2]
int channels
number of audio channels, only used for audio.
audio channel layout utility functions
const char AVS_Value args
static int query_formats(AVFilterContext *ctx)
AVFilterContext * src
source filter
static const AVFilterPad inputs[]
int format
agreed upon media format
static const AVOption headphone_options[]
A list of supported channel layouts.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
char * av_strdup(const char *s)
Duplicate a string.
Describe the class of an AVClass context structure.
Rational number (pair of numerator and denominator).
const char * name
Filter name.
static int headphone_frame(HeadphoneContext *s, AVFilterLink *outlink, int max_nb_samples)
AVFilterLink ** outputs
array of pointers to output links
enum MovChannelLayoutTag * layouts
static av_cold int init(AVFilterContext *ctx)
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
struct HeadphoneContext::headphone_inputs * in
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
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()...
uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
Get the channel with the given index in channel_layout.
uint64_t channel_layout
channel layout of current buffer (see libavutil/channel_layout.h)
int channels
Number of channels.
avfilter_execute_func * execute
AVFilterContext * dst
dest filter
static int headphone_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
uint8_t ** extended_data
pointers to the data planes/channels.
void av_fft_calc(FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in av_fft_init().
int nb_samples
number of audio samples (per channel) described by this frame
#define AV_NOPTS_VALUE
Undefined timestamp value.
static int ff_insert_inpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new input pad for the filter.