36 #define FREQUENCY_DOMAIN 1 93 int len,
i, channel_id = 0;
96 if (sscanf(*arg,
"%7[A-Z]%n", buf, &len)) {
98 for (i = 32; i > 0; i >>= 1) {
99 if (layout >= 1LL << i) {
104 if (channel_id >= 64 || layout0 != 1LL << channel_id)
106 *rchannel = channel_id;
117 uint64_t used_channels = 0;
127 for (i = 0; i < 64; i++) {
131 while ((arg =
av_strtok(p,
"|", &tokenizer))) {
140 if (used_channels & (1ULL << out_ch_id)) {
144 used_channels |= 1ULL << out_ch_id;
179 const float *
const ir = td->
ir[jobnr];
185 const float *
src = (
const float *)in->
data[0];
186 float *dst = (
float *)
out->data[0];
187 const int in_channels = in->
channels;
189 const uint32_t modulo = (uint32_t)buffer_length - 1;
196 for (l = 0; l < in_channels; l++) {
201 const float *temp_ir = ir;
204 for (l = 0; l < in_channels; l++) {
205 *(buffer[l] + wr) = src[l];
208 for (l = 0; l < in_channels; l++) {
209 const float *
const bptr = buffer[l];
217 read = (wr - *(delay + l) - (ir_len - 1) +
buffer_length) & modulo;
219 if (read + ir_len < buffer_length) {
220 memcpy(temp_src, bptr + read, ir_len *
sizeof(*temp_src));
222 int len =
FFMIN(air_len - (read % ir_len), buffer_length - read);
224 memcpy(temp_src, bptr + read, len *
sizeof(*temp_src));
225 memcpy(temp_src + len, bptr, (air_len - len) *
sizeof(*temp_src));
232 if (fabsf(dst[0]) > 1)
237 wr = (wr + 1) & modulo;
256 const float *
src = (
const float *)in->
data[0];
257 float *dst = (
float *)
out->data[0];
258 const int in_channels = in->
channels;
260 const uint32_t modulo = (uint32_t)buffer_length - 1;
266 const float fft_scale = 1.0f / s->
n_fft;
275 for (j = 0; j < n_read; j++) {
276 dst[2 * j] = ringbuffer[wr];
277 ringbuffer[wr] = 0.0;
278 wr = (wr + 1) & modulo;
285 memset(fft_acc, 0,
sizeof(
FFTComplex) * n_fft);
287 for (i = 0; i < in_channels; i++) {
290 dst[2 * j] += src[i + j * in_channels] * s->
gain_lfe;
296 hrtf_offset = hrtf +
offset;
298 memset(fft_in, 0,
sizeof(
FFTComplex) * n_fft);
301 fft_in[j].
re = src[j * in_channels +
i];
306 for (j = 0; j <
n_fft; j++) {
308 const float re = fft_in[j].
re;
309 const float im = fft_in[j].
im;
311 fft_acc[j].
re += re * hcomplex->
re - im * hcomplex->
im;
312 fft_acc[j].
im += re * hcomplex->
im + im * hcomplex->
re;
320 dst[2 * j] += fft_acc[j].
re * fft_scale;
323 for (j = 0; j < ir_len - 1; j++) {
324 int write_pos = (wr + j) & modulo;
326 *(ringbuffer + write_pos) += fft_acc[in->
nb_samples + j].
re * fft_scale;
329 for (i = 0; i <
out->nb_samples; i++) {
330 if (fabsf(dst[0]) > 1) {
350 if (ir_len > max_ir_len) {
363 int n_clippings[2] = { 0 };
387 if (n_clippings[0] + n_clippings[1] > 0) {
389 n_clippings[0] + n_clippings[1], out->
nb_samples * 2);
402 float gain_lin =
expf((s->
gain - 3 * nb_input_channels) / 20 *
M_LN10);
407 float *data_ir_l =
NULL;
408 float *data_ir_r =
NULL;
421 fft_in_l =
av_calloc(n_fft,
sizeof(*fft_in_l));
422 fft_in_r =
av_calloc(n_fft,
sizeof(*fft_in_r));
423 if (!fft_in_l || !fft_in_r) {
483 data_hrtf_l =
av_calloc(n_fft,
sizeof(*data_hrtf_l) * nb_irs);
484 data_hrtf_r =
av_calloc(n_fft,
sizeof(*data_hrtf_r) * nb_irs);
485 if (!data_hrtf_r || !data_hrtf_l) {
505 for (j = 0; j < inlink->
channels; j++) {
520 for (j = 0; j <
len; j++) {
521 data_ir_l[offset + j] = ptr[len * 2 - j * 2 - 2] * gain_lin;
522 data_ir_r[offset + j] = ptr[len * 2 - j * 2 - 1] * gain_lin;
525 memset(fft_in_l, 0, n_fft *
sizeof(*fft_in_l));
526 memset(fft_in_r, 0, n_fft *
sizeof(*fft_in_r));
528 offset = idx *
n_fft;
529 for (j = 0; j <
len; j++) {
530 fft_in_l[delay_l + j].
re = ptr[j * 2 ] * gain_lin;
531 fft_in_r[delay_r + j].
re = ptr[j * 2 + 1] * gain_lin;
536 memcpy(data_hrtf_l + offset, fft_in_l, n_fft *
sizeof(*fft_in_l));
539 memcpy(data_hrtf_r + offset, fft_in_r, n_fft *
sizeof(*fft_in_r));
544 for (k = 0; k < N / 2; k++) {
547 for (j = 0; j < inlink->
channels; j++) {
563 for (j = 0; j <
len; j++) {
564 data_ir_l[offset + j] = ptr[len * N - j * N - N + I ] * gain_lin;
565 data_ir_r[offset + j] = ptr[len * N - j * N - N + I + 1] * gain_lin;
568 memset(fft_in_l, 0, n_fft *
sizeof(*fft_in_l));
569 memset(fft_in_r, 0, n_fft *
sizeof(*fft_in_r));
571 offset = idx *
n_fft;
572 for (j = 0; j <
len; j++) {
573 fft_in_l[delay_l + j].
re = ptr[j * N + I ] * gain_lin;
574 fft_in_r[delay_r + j].
re = ptr[j * N + I + 1] * gain_lin;
579 memcpy(data_hrtf_l + offset, fft_in_l, n_fft *
sizeof(*fft_in_l));
582 memcpy(data_hrtf_r + offset, fft_in_r, n_fft *
sizeof(*fft_in_r));
591 memcpy(s->
data_ir[0], data_ir_l,
sizeof(
float) * nb_irs * s->
air_len);
592 memcpy(s->
data_ir[1], data_ir_r,
sizeof(
float) * nb_irs * s->
air_len);
646 "HRIR stream %d.\n", i - 1);
853 #define OFFSET(x) offsetof(HeadphoneContext, x) 854 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM 883 .description =
NULL_IF_CONFIG_SMALL(
"Apply headphone binaural spatialization with HRTFs in additional streams."),
885 .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)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
static int parse_channel_name(char **arg, int *rchannel, char *buf)
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.
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Main libavfilter public API header.
#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
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
static int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
static int activate(AVFilterContext *ctx)
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.
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.
A link between two filters.
AVFilterPad * input_pads
array of input pads
#define i(width, name, range_min, range_max)
#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.
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[]
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)
AVFILTER_DEFINE_CLASS(headphone)
char * av_asprintf(const char *fmt,...)
static int headphone_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
FFTComplex * data_hrtf[2]
int channels
number of audio channels, only used for audio.
audio channel layout utility functions
unsigned nb_inputs
number of input pads
int ff_inlink_queued_samples(AVFilterLink *link)
static int query_formats(AVFilterContext *ctx)
AVFilterContext * src
source filter
static const AVFilterPad inputs[]
static const AVOption headphone_options[]
A list of supported channel layouts.
static int headphone_frame(HeadphoneContext *s, AVFrame *in, AVFilterLink *outlink)
char * av_strdup(const char *s)
Duplicate a string.
Used for passing data between threads.
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.
Describe the class of an AVClass context structure.
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
const char * name
Filter name.
FFTComplex * temp_afft[2]
AVFilterLink ** outputs
array of pointers to output links
enum MovChannelLayoutTag * layouts
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
static av_cold int init(AVFilterContext *ctx)
#define flags(name, subs,...)
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
struct HeadphoneContext::headphone_inputs * in
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)
static int check_ir(AVFilterLink *inlink, int input_number)
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
static int ff_insert_inpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new input pad for the filter.