FFmpeg
af_adelay.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 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/avstring.h"
22 #include "libavutil/eval.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/samplefmt.h"
25 #include "avfilter.h"
26 #include "audio.h"
27 #include "filters.h"
28 #include "internal.h"
29 
30 typedef struct ChanDelay {
31  int delay;
32  unsigned delay_index;
33  unsigned index;
35 } ChanDelay;
36 
37 typedef struct AudioDelayContext {
38  const AVClass *class;
39  char *delays;
41  int nb_delays;
43  int64_t padding;
44  int64_t max_delay;
45  int64_t next_pts;
46  int eof;
47 
48  void (*delay_channel)(ChanDelay *d, int nb_samples,
49  const uint8_t *src, uint8_t *dst);
51 
52 #define OFFSET(x) offsetof(AudioDelayContext, x)
53 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
54 
55 static const AVOption adelay_options[] = {
56  { "delays", "set list of delays for each channel", OFFSET(delays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
57  { NULL }
58 };
59 
60 AVFILTER_DEFINE_CLASS(adelay);
61 
63 {
66  static const enum AVSampleFormat sample_fmts[] = {
70  };
71  int ret;
72 
73  layouts = ff_all_channel_counts();
74  if (!layouts)
75  return AVERROR(ENOMEM);
76  ret = ff_set_common_channel_layouts(ctx, layouts);
77  if (ret < 0)
78  return ret;
79 
80  formats = ff_make_format_list(sample_fmts);
81  if (!formats)
82  return AVERROR(ENOMEM);
83  ret = ff_set_common_formats(ctx, formats);
84  if (ret < 0)
85  return ret;
86 
87  formats = ff_all_samplerates();
88  if (!formats)
89  return AVERROR(ENOMEM);
90  return ff_set_common_samplerates(ctx, formats);
91 }
92 
93 #define DELAY(name, type, fill) \
94 static void delay_channel_## name ##p(ChanDelay *d, int nb_samples, \
95  const uint8_t *ssrc, uint8_t *ddst) \
96 { \
97  const type *src = (type *)ssrc; \
98  type *dst = (type *)ddst; \
99  type *samples = (type *)d->samples; \
100  \
101  while (nb_samples) { \
102  if (d->delay_index < d->delay) { \
103  const int len = FFMIN(nb_samples, d->delay - d->delay_index); \
104  \
105  memcpy(&samples[d->delay_index], src, len * sizeof(type)); \
106  memset(dst, fill, len * sizeof(type)); \
107  d->delay_index += len; \
108  src += len; \
109  dst += len; \
110  nb_samples -= len; \
111  } else { \
112  *dst = samples[d->index]; \
113  samples[d->index] = *src; \
114  nb_samples--; \
115  d->index++; \
116  src++, dst++; \
117  d->index = d->index >= d->delay ? 0 : d->index; \
118  } \
119  } \
120 }
121 
122 DELAY(u8, uint8_t, 0x80)
123 DELAY(s16, int16_t, 0)
124 DELAY(s32, int32_t, 0)
125 DELAY(flt, float, 0)
126 DELAY(dbl, double, 0)
127 
129 {
130  AVFilterContext *ctx = inlink->dst;
131  AudioDelayContext *s = ctx->priv;
132  char *p, *arg, *saveptr = NULL;
133  int i;
134 
135  s->chandelay = av_calloc(inlink->channels, sizeof(*s->chandelay));
136  if (!s->chandelay)
137  return AVERROR(ENOMEM);
138  s->nb_delays = inlink->channels;
139  s->block_align = av_get_bytes_per_sample(inlink->format);
140 
141  p = s->delays;
142  for (i = 0; i < s->nb_delays; i++) {
143  ChanDelay *d = &s->chandelay[i];
144  float delay, div;
145  char type = 0;
146  int ret;
147 
148  if (!(arg = av_strtok(p, "|", &saveptr)))
149  break;
150 
151  p = NULL;
152 
153  ret = av_sscanf(arg, "%d%c", &d->delay, &type);
154  if (ret != 2 || type != 'S') {
155  div = type == 's' ? 1.0 : 1000.0;
156  av_sscanf(arg, "%f", &delay);
157  d->delay = delay * inlink->sample_rate / div;
158  }
159 
160  if (d->delay < 0) {
161  av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n");
162  return AVERROR(EINVAL);
163  }
164  }
165 
166  s->padding = s->chandelay[0].delay;
167  for (i = 1; i < s->nb_delays; i++) {
168  ChanDelay *d = &s->chandelay[i];
169 
170  s->padding = FFMIN(s->padding, d->delay);
171  }
172 
173  if (s->padding) {
174  for (i = 0; i < s->nb_delays; i++) {
175  ChanDelay *d = &s->chandelay[i];
176 
177  d->delay -= s->padding;
178  }
179  }
180 
181  for (i = 0; i < s->nb_delays; i++) {
182  ChanDelay *d = &s->chandelay[i];
183 
184  if (!d->delay)
185  continue;
186 
188  if (!d->samples)
189  return AVERROR(ENOMEM);
190 
191  s->max_delay = FFMAX(s->max_delay, d->delay);
192  }
193 
194  switch (inlink->format) {
195  case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ; break;
196  case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p; break;
197  case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p; break;
198  case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp; break;
199  case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp; break;
200  }
201 
202  return 0;
203 }
204 
206 {
207  AVFilterContext *ctx = inlink->dst;
208  AudioDelayContext *s = ctx->priv;
209  AVFrame *out_frame;
210  int i;
211 
212  if (ctx->is_disabled || !s->delays)
213  return ff_filter_frame(ctx->outputs[0], frame);
214 
215  out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
216  if (!out_frame) {
217  av_frame_free(&frame);
218  return AVERROR(ENOMEM);
219  }
220  av_frame_copy_props(out_frame, frame);
221 
222  for (i = 0; i < s->nb_delays; i++) {
223  ChanDelay *d = &s->chandelay[i];
224  const uint8_t *src = frame->extended_data[i];
225  uint8_t *dst = out_frame->extended_data[i];
226 
227  if (!d->delay)
228  memcpy(dst, src, frame->nb_samples * s->block_align);
229  else
230  s->delay_channel(d, frame->nb_samples, src, dst);
231  }
232 
233  out_frame->pts = s->next_pts;
234  s->next_pts += av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
235  av_frame_free(&frame);
236  return ff_filter_frame(ctx->outputs[0], out_frame);
237 }
238 
240 {
241  AVFilterLink *inlink = ctx->inputs[0];
242  AVFilterLink *outlink = ctx->outputs[0];
243  AudioDelayContext *s = ctx->priv;
244  AVFrame *frame = NULL;
245  int ret, status;
246  int64_t pts;
247 
248  FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
249 
250  if (s->padding) {
251  int nb_samples = FFMIN(s->padding, 2048);
252 
253  frame = ff_get_audio_buffer(outlink, nb_samples);
254  if (!frame)
255  return AVERROR(ENOMEM);
256  s->padding -= nb_samples;
257 
259  frame->nb_samples,
260  outlink->channels,
261  frame->format);
262 
263  frame->pts = s->next_pts;
264  if (s->next_pts != AV_NOPTS_VALUE)
265  s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
266 
267  return ff_filter_frame(outlink, frame);
268  }
269 
270  ret = ff_inlink_consume_frame(inlink, &frame);
271  if (ret < 0)
272  return ret;
273 
274  if (ret > 0)
275  return filter_frame(inlink, frame);
276 
277  if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
278  if (status == AVERROR_EOF)
279  s->eof = 1;
280  }
281 
282  if (s->eof && s->max_delay) {
283  int nb_samples = FFMIN(s->max_delay, 2048);
284 
285  frame = ff_get_audio_buffer(outlink, nb_samples);
286  if (!frame)
287  return AVERROR(ENOMEM);
288  s->max_delay -= nb_samples;
289 
291  frame->nb_samples,
292  outlink->channels,
293  frame->format);
294 
295  frame->pts = s->next_pts;
296  return filter_frame(inlink, frame);
297  }
298 
299  if (s->eof && s->max_delay == 0) {
301  return 0;
302  }
303 
304  if (!s->eof)
305  FF_FILTER_FORWARD_WANTED(outlink, inlink);
306 
307  return FFERROR_NOT_READY;
308 }
309 
311 {
312  AudioDelayContext *s = ctx->priv;
313 
314  if (s->chandelay) {
315  for (int i = 0; i < s->nb_delays; i++)
316  av_freep(&s->chandelay[i].samples);
317  }
318  av_freep(&s->chandelay);
319 }
320 
321 static const AVFilterPad adelay_inputs[] = {
322  {
323  .name = "default",
324  .type = AVMEDIA_TYPE_AUDIO,
325  .config_props = config_input,
326  },
327  { NULL }
328 };
329 
330 static const AVFilterPad adelay_outputs[] = {
331  {
332  .name = "default",
333  .type = AVMEDIA_TYPE_AUDIO,
334  },
335  { NULL }
336 };
337 
339  .name = "adelay",
340  .description = NULL_IF_CONFIG_SMALL("Delay one or more audio channels."),
341  .query_formats = query_formats,
342  .priv_size = sizeof(AudioDelayContext),
343  .priv_class = &adelay_class,
344  .activate = activate,
345  .uninit = uninit,
346  .inputs = adelay_inputs,
347  .outputs = adelay_outputs,
349 };
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:1481
#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:549
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
static int activate(AVFilterContext *ctx)
Definition: af_adelay.c:239
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_adelay.c:205
AVOption.
Definition: opt.h:246
#define OFFSET(x)
Definition: af_adelay.c:52
Main libavfilter public API header.
double, planar
Definition: samplefmt.h:70
GLint GLenum type
Definition: opengl_enc.c:104
return FFERROR_NOT_READY
#define src
Definition: vp8dsp.c:254
int64_t next_pts
Definition: af_adelay.c:45
int is_disabled
the enabled state from the last expression evaluation
Definition: avfilter.h:385
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
uint8_t * samples
Definition: af_adelay.c:34
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:244
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:1080
uint8_t
#define av_cold
Definition: attributes.h:82
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:388
int64_t max_delay
Definition: af_adelay.c:44
void(* delay_channel)(ChanDelay *d, int nb_samples, const uint8_t *src, uint8_t *dst)
Definition: af_adelay.c:48
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define av_log(a,...)
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
static const AVOption adelay_options[]
Definition: af_adelay.c:55
A filter pad used for either input or output.
Definition: internal.h:54
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
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:1436
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_adelay.c:310
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:568
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.
Definition: samplefmt.c:237
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:202
#define A
Definition: af_adelay.c:53
#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
const char * arg
Definition: jacosubdec.c:66
#define FFMAX(a, b)
Definition: common.h:94
int av_sscanf(const char *string, const char *format,...)
See libc sscanf manual for more information.
Definition: avsscanf.c:962
int delay
Definition: af_adelay.c:31
AVFILTER_DEFINE_CLASS(adelay)
#define FFMIN(a, b)
Definition: common.h:96
static int query_formats(AVFilterContext *ctx)
Definition: af_adelay.c:62
AVFilter ff_af_adelay
Definition: af_adelay.c:338
signed 32 bits, planar
Definition: samplefmt.h:68
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
int32_t
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
unsigned 8 bits, planar
Definition: samplefmt.h:66
ChanDelay * chandelay
Definition: af_adelay.c:40
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
A list of supported channel layouts.
Definition: formats.h:85
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:368
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
unsigned delay_index
Definition: af_adelay.c:32
int64_t padding
Definition: af_adelay.c:43
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
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
Rational number (pair of numerator and denominator).
Definition: rational.h:58
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
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition: avfilter.h:133
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
static int64_t pts
#define flags(name, subs,...)
Definition: cbs_av1.c:561
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
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_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
Definition: avstring.c:184
static const AVFilterPad adelay_inputs[]
Definition: af_adelay.c:321
FF_FILTER_FORWARD_WANTED(outlink, inlink)
static int config_input(AVFilterLink *inlink)
Definition: af_adelay.c:128
unsigned index
Definition: af_adelay.c:33
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
static const AVFilterPad adelay_outputs[]
Definition: af_adelay.c:330
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define DELAY(name, type, fill)
Definition: af_adelay.c:93
#define av_freep(p)
signed 16 bits, planar
Definition: samplefmt.h:67
#define av_malloc_array(a, b)
formats
Definition: signature.h:48
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:410
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
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:654
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
simple arithmetic expression evaluator