FFmpeg
af_axcorrelate.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/avassert.h"
22 #include "libavutil/audio_fifo.h"
24 #include "libavutil/common.h"
25 #include "libavutil/opt.h"
26 
27 #include "audio.h"
28 #include "avfilter.h"
29 #include "formats.h"
30 #include "filters.h"
31 #include "internal.h"
32 
33 typedef struct AudioXCorrelateContext {
34  const AVClass *class;
35 
36  int size;
37  int algo;
38  int64_t pts;
39 
45  int used;
46 
49 
51 {
54  static const enum AVSampleFormat sample_fmts[] = {
57  };
58  int ret;
59 
60  layouts = ff_all_channel_counts();
61  if (!layouts)
62  return AVERROR(ENOMEM);
63  ret = ff_set_common_channel_layouts(ctx, layouts);
64  if (ret < 0)
65  return ret;
66 
67  formats = ff_make_format_list(sample_fmts);
68  if (!formats)
69  return AVERROR(ENOMEM);
70  ret = ff_set_common_formats(ctx, formats);
71  if (ret < 0)
72  return ret;
73 
74  formats = ff_all_samplerates();
75  if (!formats)
76  return AVERROR(ENOMEM);
77  return ff_set_common_samplerates(ctx, formats);
78 }
79 
80 static float mean_sum(const float *in, int size)
81 {
82  float mean_sum = 0.f;
83 
84  for (int i = 0; i < size; i++)
85  mean_sum += in[i];
86 
87  return mean_sum;
88 }
89 
90 static float square_sum(const float *x, const float *y, int size)
91 {
92  float square_sum = 0.f;
93 
94  for (int i = 0; i < size; i++)
95  square_sum += x[i] * y[i];
96 
97  return square_sum;
98 }
99 
100 static float xcorrelate(const float *x, const float *y, float sumx, float sumy, int size)
101 {
102  const float xm = sumx / size, ym = sumy / size;
103  float num = 0.f, den, den0 = 0.f, den1 = 0.f;
104 
105  for (int i = 0; i < size; i++) {
106  float xd = x[i] - xm;
107  float yd = y[i] - ym;
108 
109  num += xd * yd;
110  den0 += xd * xd;
111  den1 += yd * yd;
112  }
113 
114  num /= size;
115  den = sqrtf((den0 * den1) / (size * size));
116 
117  return den <= 1e-6f ? 0.f : num / den;
118 }
119 
121 {
122  AudioXCorrelateContext *s = ctx->priv;
123  const int size = s->size;
124  int used;
125 
126  for (int ch = 0; ch < out->channels; ch++) {
127  const float *x = (const float *)s->cache[0]->extended_data[ch];
128  const float *y = (const float *)s->cache[1]->extended_data[ch];
129  float *sumx = (float *)s->mean_sum[0]->extended_data[ch];
130  float *sumy = (float *)s->mean_sum[1]->extended_data[ch];
131  float *dst = (float *)out->extended_data[ch];
132 
133  used = s->used;
134  if (!used) {
135  sumx[0] = mean_sum(x, size);
136  sumy[0] = mean_sum(y, size);
137  used = 1;
138  }
139 
140  for (int n = 0; n < out->nb_samples; n++) {
141  dst[n] = xcorrelate(x + n, y + n, sumx[0], sumy[0], size);
142 
143  sumx[0] -= x[n];
144  sumx[0] += x[n + size];
145  sumy[0] -= y[n];
146  sumy[0] += y[n + size];
147  }
148  }
149 
150  return used;
151 }
152 
154 {
155  AudioXCorrelateContext *s = ctx->priv;
156  const int size = s->size;
157  int used;
158 
159  for (int ch = 0; ch < out->channels; ch++) {
160  const float *x = (const float *)s->cache[0]->extended_data[ch];
161  const float *y = (const float *)s->cache[1]->extended_data[ch];
162  float *num_sum = (float *)s->num_sum->extended_data[ch];
163  float *den_sumx = (float *)s->den_sum[0]->extended_data[ch];
164  float *den_sumy = (float *)s->den_sum[1]->extended_data[ch];
165  float *dst = (float *)out->extended_data[ch];
166 
167  used = s->used;
168  if (!used) {
169  num_sum[0] = square_sum(x, y, size);
170  den_sumx[0] = square_sum(x, x, size);
171  den_sumy[0] = square_sum(y, y, size);
172  used = 1;
173  }
174 
175  for (int n = 0; n < out->nb_samples; n++) {
176  float num, den;
177 
178  num = num_sum[0] / size;
179  den = sqrtf((den_sumx[0] * den_sumy[0]) / (size * size));
180 
181  dst[n] = den <= 1e-6f ? 0.f : num / den;
182 
183  num_sum[0] -= x[n] * y[n];
184  num_sum[0] += x[n + size] * y[n + size];
185  den_sumx[0] -= x[n] * x[n];
186  den_sumx[0] = FFMAX(den_sumx[0], 0.f);
187  den_sumx[0] += x[n + size] * x[n + size];
188  den_sumy[0] -= y[n] * y[n];
189  den_sumy[0] = FFMAX(den_sumy[0], 0.f);
190  den_sumy[0] += y[n + size] * y[n + size];
191  }
192  }
193 
194  return used;
195 }
196 
198 {
199  AudioXCorrelateContext *s = ctx->priv;
200  AVFrame *frame = NULL;
201  int ret, status;
202  int available;
203  int64_t pts;
204 
206 
207  for (int i = 0; i < 2; i++) {
208  ret = ff_inlink_consume_frame(ctx->inputs[i], &frame);
209  if (ret > 0) {
210  if (s->pts == AV_NOPTS_VALUE)
211  s->pts = frame->pts;
212  ret = av_audio_fifo_write(s->fifo[i], (void **)frame->extended_data,
213  frame->nb_samples);
214  av_frame_free(&frame);
215  if (ret < 0)
216  return ret;
217  }
218  }
219 
220  available = FFMIN(av_audio_fifo_size(s->fifo[0]), av_audio_fifo_size(s->fifo[1]));
221  if (available > s->size) {
222  const int out_samples = available - s->size;
223  AVFrame *out;
224 
225  if (!s->cache[0] || s->cache[0]->nb_samples < available) {
226  av_frame_free(&s->cache[0]);
227  s->cache[0] = ff_get_audio_buffer(ctx->outputs[0], available);
228  if (!s->cache[0])
229  return AVERROR(ENOMEM);
230  }
231 
232  if (!s->cache[1] || s->cache[1]->nb_samples < available) {
233  av_frame_free(&s->cache[1]);
234  s->cache[1] = ff_get_audio_buffer(ctx->outputs[0], available);
235  if (!s->cache[1])
236  return AVERROR(ENOMEM);
237  }
238 
239  ret = av_audio_fifo_peek(s->fifo[0], (void **)s->cache[0]->extended_data, available);
240  if (ret < 0)
241  return ret;
242 
243  ret = av_audio_fifo_peek(s->fifo[1], (void **)s->cache[1]->extended_data, available);
244  if (ret < 0)
245  return ret;
246 
247  out = ff_get_audio_buffer(ctx->outputs[0], out_samples);
248  if (!out)
249  return AVERROR(ENOMEM);
250 
251  s->used = s->xcorrelate(ctx, out);
252 
253  out->pts = s->pts;
254  s->pts += out_samples;
255 
256  av_audio_fifo_drain(s->fifo[0], out_samples);
257  av_audio_fifo_drain(s->fifo[1], out_samples);
258 
259  return ff_filter_frame(ctx->outputs[0], out);
260  }
261 
262  if (av_audio_fifo_size(s->fifo[0]) > s->size &&
263  av_audio_fifo_size(s->fifo[1]) > s->size) {
264  ff_filter_set_ready(ctx, 10);
265  return 0;
266  }
267 
268  for (int i = 0; i < 2; i++) {
269  if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
270  ff_outlink_set_status(ctx->outputs[0], status, pts);
271  return 0;
272  }
273  }
274 
275  if (ff_outlink_frame_wanted(ctx->outputs[0])) {
276  for (int i = 0; i < 2; i++) {
277  if (av_audio_fifo_size(s->fifo[i]) > s->size)
278  continue;
280  return 0;
281  }
282  }
283 
284  return FFERROR_NOT_READY;
285 }
286 
287 static int config_output(AVFilterLink *outlink)
288 {
289  AVFilterContext *ctx = outlink->src;
290  AVFilterLink *inlink = ctx->inputs[0];
291  AudioXCorrelateContext *s = ctx->priv;
292 
293  s->pts = AV_NOPTS_VALUE;
294 
295  outlink->format = inlink->format;
296  outlink->channels = inlink->channels;
297  s->fifo[0] = av_audio_fifo_alloc(outlink->format, outlink->channels, s->size);
298  s->fifo[1] = av_audio_fifo_alloc(outlink->format, outlink->channels, s->size);
299  if (!s->fifo[0] || !s->fifo[1])
300  return AVERROR(ENOMEM);
301 
302  s->mean_sum[0] = ff_get_audio_buffer(outlink, 1);
303  s->mean_sum[1] = ff_get_audio_buffer(outlink, 1);
304  s->num_sum = ff_get_audio_buffer(outlink, 1);
305  s->den_sum[0] = ff_get_audio_buffer(outlink, 1);
306  s->den_sum[1] = ff_get_audio_buffer(outlink, 1);
307  if (!s->mean_sum[0] || !s->mean_sum[1] || !s->num_sum ||
308  !s->den_sum[0] || !s->den_sum[1])
309  return AVERROR(ENOMEM);
310 
311  switch (s->algo) {
312  case 0: s->xcorrelate = xcorrelate_slow; break;
313  case 1: s->xcorrelate = xcorrelate_fast; break;
314  }
315 
316  return 0;
317 }
318 
320 {
321  AudioXCorrelateContext *s = ctx->priv;
322 
323  av_audio_fifo_free(s->fifo[0]);
324  av_audio_fifo_free(s->fifo[1]);
325  av_frame_free(&s->cache[0]);
326  av_frame_free(&s->cache[1]);
327  av_frame_free(&s->mean_sum[0]);
328  av_frame_free(&s->mean_sum[1]);
329  av_frame_free(&s->num_sum);
330  av_frame_free(&s->den_sum[0]);
331  av_frame_free(&s->den_sum[1]);
332 }
333 
334 static const AVFilterPad inputs[] = {
335  {
336  .name = "axcorrelate0",
337  .type = AVMEDIA_TYPE_AUDIO,
338  },
339  {
340  .name = "axcorrelate1",
341  .type = AVMEDIA_TYPE_AUDIO,
342  },
343  { NULL }
344 };
345 
346 static const AVFilterPad outputs[] = {
347  {
348  .name = "default",
349  .type = AVMEDIA_TYPE_AUDIO,
350  .config_props = config_output,
351  },
352  { NULL }
353 };
354 
355 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
356 #define OFFSET(x) offsetof(AudioXCorrelateContext, x)
357 
358 static const AVOption axcorrelate_options[] = {
359  { "size", "set segment size", OFFSET(size), AV_OPT_TYPE_INT, {.i64=256}, 2, 131072, AF },
360  { "algo", "set alghorithm", OFFSET(algo), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AF, "algo" },
361  { "slow", "slow algorithm", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "algo" },
362  { "fast", "fast algorithm", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "algo" },
363  { NULL }
364 };
365 
366 AVFILTER_DEFINE_CLASS(axcorrelate);
367 
369  .name = "axcorrelate",
370  .description = NULL_IF_CONFIG_SMALL("Cross-correlate two audio streams."),
371  .priv_size = sizeof(AudioXCorrelateContext),
372  .priv_class = &axcorrelate_class,
374  .activate = activate,
375  .uninit = uninit,
376  .inputs = inputs,
377  .outputs = outputs,
378 };
float, planar
Definition: samplefmt.h:69
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link&#39;s FIFO and update the link&#39;s stats.
Definition: avfilter.c:1476
#define NULL
Definition: coverity.c:32
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:550
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:59
static int xcorrelate_slow(AVFilterContext *ctx, AVFrame *out)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
AVOption.
Definition: opt.h:246
static const AVFilterPad inputs[]
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition: filters.h:212
Main libavfilter public API header.
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
AVAudioFifo * fifo[2]
return FFERROR_NOT_READY
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:189
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1602
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1075
#define av_cold
Definition: attributes.h:82
AVOptions.
#define f(width, name)
Definition: cbs_vp9.c:255
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:388
#define OFFSET(x)
static av_cold void uninit(AVFilterContext *ctx)
ptrdiff_t size
Definition: opengl_enc.c:100
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
A filter pad used for either input or output.
Definition: internal.h:54
static int query_formats(AVFilterContext *ctx)
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition: avfilter.c:1431
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:569
if no frame is available
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:86
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
simple assert() macros that are a bit more flexible than ISO C assert().
#define FFMAX(a, b)
Definition: common.h:94
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
AVFilter ff_af_axcorrelate
Definition: dct.c:52
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:228
int channels
number of audio channels, only used for audio.
Definition: frame.h:601
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:96
#define AF
AVFormatContext * ctx
Definition: movenc.c:48
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 the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define s(width, name)
Definition: cbs_vp9.c:257
int n
Definition: avisynth_c.h:760
int(* xcorrelate)(AVFilterContext *ctx, AVFrame *out)
A list of supported channel layouts.
Definition: formats.h:85
if(ret)
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
static const AVFilterPad outputs[]
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
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
static int config_output(AVFilterLink *outlink)
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(const int64_t *) pi *(1.0f/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(const int64_t *) pi *(1.0/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(UINT64_C(1)<< 63)))#define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};static void cpy1(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, len);}static void cpy2(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 2 *len);}static void cpy4(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 4 *len);}static void cpy8(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;}void swri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len){int ch;int off=0;const int os=(out->planar?1:out->ch_count)*out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){int planes=in->planar?in->ch_count:1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){int planes=out->planar?out->ch_count:1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){int planes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:394
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:112
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
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:201
int
common internal and external API header
static int activate(AVFilterContext *ctx)
static float square_sum(const float *x, const float *y, int size)
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition: avfilter.c:193
static const AVOption axcorrelate_options[]
Audio FIFO Buffer.
A list of supported formats for one end of a filter link.
Definition: formats.h:64
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:138
An instance of a filter.
Definition: avfilter.h:338
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
FILE * out
Definition: movenc.c:54
formats
Definition: signature.h:48
AVFILTER_DEFINE_CLASS(axcorrelate)
internal API functions
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
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition...
Definition: formats.c:409
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:342
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:361
static int xcorrelate_fast(AVFilterContext *ctx, AVFrame *out)
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:557
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248