Go to the documentation of this file.
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) {
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++) {
140 if (used_channels & (1ULL << out_ch_id)) {
144 used_channels |= 1ULL << out_ch_id;
146 s->lfe_channel =
s->nb_irs;
147 s->mapping[
s->nb_irs] = out_ch_id;
154 s->nb_inputs =
s->nb_irs + 1;
177 int *write = &
td->write[jobnr];
178 const int *
const delay =
td->delay[jobnr];
179 const float *
const ir =
td->ir[jobnr];
180 int *n_clippings = &
td->n_clippings[jobnr];
181 float *ringbuffer =
td->ringbuffer[jobnr];
182 float *temp_src =
td->temp_src[jobnr];
183 const int ir_len =
s->ir_len;
184 const int air_len =
s->air_len;
185 const float *
src = (
const float *)
in->data[0];
186 float *dst = (
float *)
out->data[0];
188 const int buffer_length =
s->buffer_length;
189 const uint32_t modulo = (uint32_t)buffer_length - 1;
196 for (l = 0; l < in_channels; l++) {
197 buffer[l] = ringbuffer + l * buffer_length;
200 for (
i = 0;
i <
in->nb_samples;
i++) {
201 const float *temp_ir = ir;
204 for (l = 0; l < in_channels; l++) {
208 for (l = 0; l < in_channels; l++) {
209 const float *
const bptr =
buffer[l];
211 if (l ==
s->lfe_channel) {
212 *dst += *(
buffer[
s->lfe_channel] + wr) *
s->gain_lfe;
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));
228 dst[0] +=
s->fdsp->scalarproduct_float(temp_ir, temp_src,
FFALIGN(ir_len, 32));
232 if (fabsf(dst[0]) > 1)
237 wr = (wr + 1) & modulo;
251 int *write = &
td->write[jobnr];
253 int *n_clippings = &
td->n_clippings[jobnr];
254 float *ringbuffer =
td->ringbuffer[jobnr];
255 const int ir_len =
s->ir_len;
256 const float *
src = (
const float *)
in->data[0];
257 float *dst = (
float *)
out->data[0];
259 const int buffer_length =
s->buffer_length;
260 const uint32_t modulo = (uint32_t)buffer_length - 1;
265 const int n_fft =
s->n_fft;
266 const float fft_scale = 1.0f /
s->n_fft;
274 n_read =
FFMIN(ir_len,
in->nb_samples);
275 for (j = 0; j < n_read; j++) {
276 dst[2 * j] = ringbuffer[wr];
277 ringbuffer[wr] = 0.0;
278 wr = (wr + 1) & modulo;
281 for (j = n_read; j <
in->nb_samples; j++) {
285 memset(fft_acc, 0,
sizeof(
FFTComplex) * n_fft);
287 for (
i = 0;
i < in_channels;
i++) {
288 if (
i ==
s->lfe_channel) {
289 for (j = 0; j <
in->nb_samples; j++) {
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);
300 for (j = 0; j <
in->nb_samples; j++) {
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;
319 for (j = 0; j <
in->nb_samples; j++) {
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) {
346 int ir_len, max_ir_len;
350 if (ir_len > max_ir_len) {
354 s->in[input_number].ir_len = ir_len;
355 s->ir_len =
FFMAX(ir_len,
s->ir_len);
363 int n_clippings[2] = { 0 };
375 td.delay =
s->delay;
td.ir =
s->data_ir;
td.n_clippings = n_clippings;
376 td.ringbuffer =
s->ringbuffer;
td.temp_src =
s->temp_src;
377 td.temp_fft =
s->temp_fft;
378 td.temp_afft =
s->temp_afft;
387 if (n_clippings[0] + n_clippings[1] > 0) {
389 n_clippings[0] + n_clippings[1],
out->nb_samples * 2);
401 int nb_input_channels =
ctx->inputs[0]->channels;
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;
417 s->buffer_length = 1 << (32 -
ff_clz(
s->air_len));
423 if (!fft_in_l || !fft_in_r) {
437 if (!
s->fft[0] || !
s->fft[1] || !
s->ifft[0] || !
s->ifft[1]) {
444 s->data_ir[0] =
av_calloc(
s->air_len,
sizeof(
float) *
s->nb_irs);
445 s->data_ir[1] =
av_calloc(
s->air_len,
sizeof(
float) *
s->nb_irs);
450 s->ringbuffer[0] =
av_calloc(
s->buffer_length,
sizeof(
float) * nb_input_channels);
451 s->ringbuffer[1] =
av_calloc(
s->buffer_length,
sizeof(
float) * nb_input_channels);
453 s->ringbuffer[0] =
av_calloc(
s->buffer_length,
sizeof(
float));
454 s->ringbuffer[1] =
av_calloc(
s->buffer_length,
sizeof(
float));
459 if (!
s->temp_fft[0] || !
s->temp_fft[1] ||
460 !
s->temp_afft[0] || !
s->temp_afft[1]) {
466 if (!
s->data_ir[0] || !
s->data_ir[1] ||
467 !
s->ringbuffer[0] || !
s->ringbuffer[1]) {
473 s->temp_src[0] =
av_calloc(
s->air_len,
sizeof(
float));
474 s->temp_src[1] =
av_calloc(
s->air_len,
sizeof(
float));
478 if (!data_ir_r || !data_ir_l || !
s->temp_src[0] || !
s->temp_src[1]) {
485 if (!data_hrtf_r || !data_hrtf_l) {
491 for (
i = 0;
i <
s->nb_inputs - 1;
i++) {
492 int len =
s->in[
i + 1].ir_len;
493 int delay_l =
s->in[
i + 1].delay_l;
494 int delay_r =
s->in[
i + 1].delay_r;
500 ptr = (
float *)
s->in[
i + 1].frame->extended_data[0];
505 for (j = 0; j <
inlink->channels; j++) {
506 if (
s->mapping[
i] < 0) {
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));
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));
542 int I,
N =
ctx->inputs[1]->channels;
544 for (k = 0; k <
N / 2; k++) {
547 for (j = 0; j <
inlink->channels; j++) {
548 if (
s->mapping[k] < 0) {
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));
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);
596 if (!
s->data_hrtf[0] || !
s->data_hrtf[1]) {
601 memcpy(
s->data_hrtf[0], data_hrtf_l,
603 memcpy(
s->data_hrtf[1], data_hrtf_r,
611 for (
i = 0;
i <
s->nb_inputs - 1;
i++)
636 for (
i = 1;
i <
s->nb_inputs;
i++) {
646 "HRIR stream %d.\n",
i - 1);
653 for (
i = 1;
i <
s->nb_inputs;
i++) {
658 if (
i !=
s->nb_inputs) {
660 for (
i = 1;
i <
s->nb_inputs;
i++) {
672 if (!
s->have_hrirs &&
s->eof_hrirs) {
733 for (
i = 1;
i <
s->nb_inputs;
i++) {
751 if (
s->nb_irs <
inlink->channels) {
783 for (
i = 1;
i <
s->nb_inputs;
i++) {
849 for (
unsigned i = 1;
i <
ctx->nb_inputs;
i++)
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,
static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink)
av_cold void av_fft_end(FFTContext *s)
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
A list of supported channel layouts.
#define AV_LOG_WARNING
Something somehow does not look correct.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
enum MovChannelLayoutTag * layouts
#define AVERROR_EOF
End of file.
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
char * av_asprintf(const char *fmt,...)
static int parse_channel_name(char **arg, int *rchannel, char *buf)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
This structure describes decoded (raw) audio or video data.
void av_fft_permute(FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling ff_fft_calc().
uint64_t av_get_channel_layout(const char *name)
Return a channel layout id that matches name, or 0 if no match is found.
const char * name
Filter name.
AVFormatInternal * internal
An opaque field for libavformat internal usage.
A link between two filters.
int channels
Number of channels.
static int ff_insert_inpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new input pad for the filter.
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
static int activate(AVFilterContext *ctx)
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
#define AV_CH_LAYOUT_STEREO
static int config_input(AVFilterLink *inlink)
A filter pad used for either input or output.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define AV_CH_LOW_FREQUENCY
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
FFTComplex * data_hrtf[2]
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().
static av_cold void uninit(AVFilterContext *ctx)
static int headphone_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Describe the class of an AVClass context structure.
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.
static void parse_map(AVFilterContext *ctx)
static const AVFilterPad outputs[]
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
static int query_formats(AVFilterContext *ctx)
AVFILTER_DEFINE_CLASS(headphone)
static int headphone_frame(HeadphoneContext *s, AVFrame *in, AVFilterLink *outlink)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
struct HeadphoneContext::headphone_inputs * in
AVFilterContext * src
source filter
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
static const AVOption headphone_options[]
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel layout
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
#define i(width, name, range_min, range_max)
uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
Get the channel with the given index in channel_layout.
static av_cold int init(AVFilterContext *ctx)
Used for passing data between threads.
const char * name
Pad name.
int ff_inlink_queued_samples(AVFilterLink *link)
FFTContext * av_fft_init(int nbits, int inverse)
Set up a complex FFT.
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
static int headphone_fast_convolute(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
char * av_strdup(const char *s)
Duplicate a string.
const VDPAUPixFmtMap * map
FF_FILTER_FORWARD_STATUS(inlink, outlink)
FFTComplex * temp_afft[2]
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
#define flags(name, subs,...)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
void av_fft_calc(FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in av_fft_init().
static int config_output(AVFilterLink *outlink)
static int check_ir(AVFilterLink *inlink, int input_number)