36 #define FREQUENCY_DOMAIN 1
91 int len, i, channel_id = 0;
94 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;
126 for (i = 0; i < 64; i++) {
130 while ((arg =
av_strtok(p,
"|", &tokenizer))) {
168 int *write = &td->
write[jobnr];
169 const int *
const delay = td->
delay[jobnr];
170 const float *
const ir = td->
ir[jobnr];
173 float *temp_src = td->
temp_src[jobnr];
174 const int ir_len = s->
ir_len;
175 const float *
src = (
const float *)in->
data[0];
176 float *dst = (
float *)
out->data[0];
177 const int in_channels = in->
channels;
179 const uint32_t modulo = (uint32_t)buffer_length - 1;
186 for (l = 0; l < in_channels; l++) {
187 buffer[l] = ringbuffer + l * buffer_length;
191 const float *temp_ir = ir;
194 for (l = 0; l < in_channels; l++) {
195 *(buffer[l] + wr) = src[l];
198 for (l = 0; l < in_channels; l++) {
199 const float *
const bptr = buffer[l];
203 temp_ir +=
FFALIGN(ir_len, 16);
207 read = (wr - *(delay + l) - (ir_len - 1) + buffer_length) & modulo;
209 if (read + ir_len < buffer_length) {
210 memcpy(temp_src, bptr + read, ir_len *
sizeof(*temp_src));
212 int len =
FFMIN(ir_len - (read % ir_len), buffer_length - read);
214 memcpy(temp_src, bptr + read, len *
sizeof(*temp_src));
215 memcpy(temp_src + len, bptr, (ir_len - len) *
sizeof(*temp_src));
219 temp_ir +=
FFALIGN(ir_len, 16);
227 wr = (wr + 1) & modulo;
241 int *write = &td->
write[jobnr];
245 const int ir_len = s->
ir_len;
246 const float *
src = (
const float *)in->
data[0];
247 float *dst = (
float *)
out->data[0];
248 const int in_channels = in->
channels;
250 const uint32_t modulo = (uint32_t)buffer_length - 1;
254 const int n_fft = s->
n_fft;
255 const float fft_scale = 1.0f / s->
n_fft;
264 for (j = 0; j < n_read; j++) {
265 dst[2 * j] = ringbuffer[wr];
266 ringbuffer[wr] = 0.0;
267 wr = (wr + 1) & modulo;
274 for (i = 0; i < in_channels; i++) {
277 dst[2 * j] += src[i + j * in_channels] * s->
gain_lfe;
283 hrtf_offset = hrtf +
offset;
285 memset(fft_in, 0,
sizeof(
FFTComplex) * n_fft);
288 fft_in[j].
re = src[j * in_channels + i];
293 for (j = 0; j < n_fft; j++) {
295 const float re = fft_in[j].
re;
296 const float im = fft_in[j].
im;
298 fft_in[j].
re = re * hcomplex->
re - im * hcomplex->
im;
299 fft_in[j].
im = re * hcomplex->
im + im * hcomplex->
re;
306 dst[2 * j] += fft_in[j].
re * fft_scale;
309 for (j = 0; j < ir_len - 1; j++) {
310 int write_pos = (wr + j) & modulo;
312 *(ringbuffer + write_pos) += fft_in[in->
nb_samples + j].
re * fft_scale;
316 for (i = 0; i <
out->nb_samples; i++) {
317 if (fabs(*dst) > 1) {
333 int ir_len, max_ir_len;
337 if (ir_len > max_ir_len) {
341 s->
in[input_number].
ir_len = ir_len;
350 int n_clippings[2] = { 0 };
373 if (n_clippings[0] + n_clippings[1] > 0) {
375 n_clippings[0] + n_clippings[1], out->
nb_samples * 2);
388 float gain_lin =
expf((s->
gain - 3 * nb_input_channels) / 20 *
M_LN10);
393 float *data_ir_l =
NULL;
394 float *data_ir_r =
NULL;
403 fft_in_l =
av_calloc(n_fft,
sizeof(*fft_in_l));
404 fft_in_r =
av_calloc(n_fft,
sizeof(*fft_in_r));
405 if (!fft_in_l || !fft_in_r) {
462 data_hrtf_l =
av_calloc(n_fft,
sizeof(*data_hrtf_l) * nb_irs);
463 data_hrtf_r =
av_calloc(n_fft,
sizeof(*data_hrtf_r) * nb_irs);
464 if (!data_hrtf_r || !data_hrtf_l) {
484 for (j = 0; j < inlink->
channels; j++) {
498 offset = idx *
FFALIGN(len, 16);
499 for (j = 0; j <
len; j++) {
500 data_ir_l[offset + j] = ptr[len * 2 - j * 2 - 2] * gain_lin;
501 data_ir_r[offset + j] = ptr[len * 2 - j * 2 - 1] * gain_lin;
504 memset(fft_in_l, 0, n_fft *
sizeof(*fft_in_l));
505 memset(fft_in_r, 0, n_fft *
sizeof(*fft_in_r));
507 offset = idx *
n_fft;
508 for (j = 0; j <
len; j++) {
509 fft_in_l[delay_l + j].
re = ptr[j * 2 ] * gain_lin;
510 fft_in_r[delay_r + j].
re = ptr[j * 2 + 1] * gain_lin;
515 memcpy(data_hrtf_l + offset, fft_in_l, n_fft *
sizeof(*fft_in_l));
518 memcpy(data_hrtf_r + offset, fft_in_r, n_fft *
sizeof(*fft_in_r));
523 for (k = 0; k < N / 2; k++) {
526 for (j = 0; j < inlink->
channels; j++) {
541 offset = idx *
FFALIGN(len, 16);
542 for (j = 0; j <
len; j++) {
543 data_ir_l[offset + j] = ptr[len * N - j * N - N + I ] * gain_lin;
544 data_ir_r[offset + j] = ptr[len * N - j * N - N + I + 1] * gain_lin;
547 memset(fft_in_l, 0, n_fft *
sizeof(*fft_in_l));
548 memset(fft_in_r, 0, n_fft *
sizeof(*fft_in_r));
550 offset = idx *
n_fft;
551 for (j = 0; j <
len; j++) {
552 fft_in_l[delay_l + j].
re = ptr[j * N + I ] * gain_lin;
553 fft_in_r[delay_r + j].
re = ptr[j * N + I + 1] * gain_lin;
558 memcpy(data_hrtf_l + offset, fft_in_l, n_fft *
sizeof(*fft_in_l));
561 memcpy(data_hrtf_r + offset, fft_in_r, n_fft *
sizeof(*fft_in_r));
570 memcpy(s->
data_ir[0], data_ir_l,
sizeof(
float) * nb_irs *
FFALIGN(ir_len, 16));
571 memcpy(s->
data_ir[1], data_ir_r,
sizeof(
float) * nb_irs *
FFALIGN(ir_len, 16));
827 #define OFFSET(x) offsetof(HeadphoneContext, x)
828 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
857 .description =
NULL_IF_CONFIG_SMALL(
"Apply headphone binaural spatialization with HRTFs in additional streams."),
859 .priv_class = &headphone_class,
static int config_output(AVFilterLink *outlink)
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.
#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.
float(* scalarproduct_float)(const float *v1, const float *v2, int len)
Calculate the scalar product of two vectors of floats.
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.
static int parse_channel_name(HeadphoneContext *s, int x, char **arg, int *rchannel, char *buf)
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 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
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.
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
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.
const VDPAUPixFmtMap * map
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.