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  int64_t delay;
32  size_t delay_index;
33  size_t index;
34  uint8_t *samples;
35 } ChanDelay;
36 
37 typedef struct AudioDelayContext {
38  const AVClass *class;
39  int all;
40  char *delays;
42  int nb_delays;
44  int64_t padding;
45  int64_t max_delay;
46  int64_t next_pts;
47  int eof;
48 
49  void (*delay_channel)(ChanDelay *d, int nb_samples,
50  const uint8_t *src, uint8_t *dst);
52 
53 #define OFFSET(x) offsetof(AudioDelayContext, x)
54 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
55 
56 static const AVOption adelay_options[] = {
57  { "delays", "set list of delays for each channel", OFFSET(delays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
58  { "all", "use last available delay for remained channels", OFFSET(all), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
59  { NULL }
60 };
61 
62 AVFILTER_DEFINE_CLASS(adelay);
63 
65 {
66  static const enum AVSampleFormat sample_fmts[] = {
70  };
72  if (ret < 0)
73  return ret;
74 
76  if (ret < 0)
77  return ret;
78 
80 }
81 
82 #define DELAY(name, type, fill) \
83 static void delay_channel_## name ##p(ChanDelay *d, int nb_samples, \
84  const uint8_t *ssrc, uint8_t *ddst) \
85 { \
86  const type *src = (type *)ssrc; \
87  type *dst = (type *)ddst; \
88  type *samples = (type *)d->samples; \
89  \
90  while (nb_samples) { \
91  if (d->delay_index < d->delay) { \
92  const int len = FFMIN(nb_samples, d->delay - d->delay_index); \
93  \
94  memcpy(&samples[d->delay_index], src, len * sizeof(type)); \
95  memset(dst, fill, len * sizeof(type)); \
96  d->delay_index += len; \
97  src += len; \
98  dst += len; \
99  nb_samples -= len; \
100  } else { \
101  *dst = samples[d->index]; \
102  samples[d->index] = *src; \
103  nb_samples--; \
104  d->index++; \
105  src++, dst++; \
106  d->index = d->index >= d->delay ? 0 : d->index; \
107  } \
108  } \
109 }
110 
111 DELAY(u8, uint8_t, 0x80)
112 DELAY(s16, int16_t, 0)
113 DELAY(s32, int32_t, 0)
114 DELAY(flt, float, 0)
115 DELAY(dbl, double, 0)
116 
118 {
119  AVFilterContext *ctx = inlink->dst;
120  AudioDelayContext *s = ctx->priv;
121  char *p, *arg, *saveptr = NULL;
122  int i;
123 
124  s->chandelay = av_calloc(inlink->channels, sizeof(*s->chandelay));
125  if (!s->chandelay)
126  return AVERROR(ENOMEM);
127  s->nb_delays = inlink->channels;
128  s->block_align = av_get_bytes_per_sample(inlink->format);
129 
130  p = s->delays;
131  for (i = 0; i < s->nb_delays; i++) {
132  ChanDelay *d = &s->chandelay[i];
133  float delay, div;
134  char type = 0;
135  int ret;
136 
137  if (!(arg = av_strtok(p, "|", &saveptr)))
138  break;
139 
140  p = NULL;
141 
142  ret = av_sscanf(arg, "%"SCNd64"%c", &d->delay, &type);
143  if (ret != 2 || type != 'S') {
144  div = type == 's' ? 1.0 : 1000.0;
145  if (av_sscanf(arg, "%f", &delay) != 1) {
146  av_log(ctx, AV_LOG_ERROR, "Invalid syntax for delay.\n");
147  return AVERROR(EINVAL);
148  }
149  d->delay = delay * inlink->sample_rate / div;
150  }
151 
152  if (d->delay < 0) {
153  av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n");
154  return AVERROR(EINVAL);
155  }
156  }
157 
158  if (s->all && i) {
159  for (int j = i; j < s->nb_delays; j++)
160  s->chandelay[j].delay = s->chandelay[i-1].delay;
161  }
162 
163  s->padding = s->chandelay[0].delay;
164  for (i = 1; i < s->nb_delays; i++) {
165  ChanDelay *d = &s->chandelay[i];
166 
167  s->padding = FFMIN(s->padding, d->delay);
168  }
169 
170  if (s->padding) {
171  for (i = 0; i < s->nb_delays; i++) {
172  ChanDelay *d = &s->chandelay[i];
173 
174  d->delay -= s->padding;
175  }
176  }
177 
178  for (i = 0; i < s->nb_delays; i++) {
179  ChanDelay *d = &s->chandelay[i];
180 
181  if (!d->delay)
182  continue;
183 
184  if (d->delay > SIZE_MAX) {
185  av_log(ctx, AV_LOG_ERROR, "Requested delay is too big.\n");
186  return AVERROR(EINVAL);
187  }
188 
189  d->samples = av_malloc_array(d->delay, s->block_align);
190  if (!d->samples)
191  return AVERROR(ENOMEM);
192 
193  s->max_delay = FFMAX(s->max_delay, d->delay);
194  }
195 
196  switch (inlink->format) {
197  case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ; break;
198  case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p; break;
199  case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p; break;
200  case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp; break;
201  case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp; break;
202  }
203 
204  return 0;
205 }
206 
208 {
209  AVFilterContext *ctx = inlink->dst;
210  AudioDelayContext *s = ctx->priv;
211  AVFrame *out_frame;
212  int i;
213 
214  if (ctx->is_disabled || !s->delays)
215  return ff_filter_frame(ctx->outputs[0], frame);
216 
217  out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
218  if (!out_frame) {
220  return AVERROR(ENOMEM);
221  }
222  av_frame_copy_props(out_frame, frame);
223 
224  for (i = 0; i < s->nb_delays; i++) {
225  ChanDelay *d = &s->chandelay[i];
226  const uint8_t *src = frame->extended_data[i];
227  uint8_t *dst = out_frame->extended_data[i];
228 
229  if (!d->delay)
230  memcpy(dst, src, frame->nb_samples * s->block_align);
231  else
232  s->delay_channel(d, frame->nb_samples, src, dst);
233  }
234 
235  out_frame->pts = s->next_pts;
236  s->next_pts += av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
238  return ff_filter_frame(ctx->outputs[0], out_frame);
239 }
240 
242 {
243  AVFilterLink *inlink = ctx->inputs[0];
244  AVFilterLink *outlink = ctx->outputs[0];
245  AudioDelayContext *s = ctx->priv;
246  AVFrame *frame = NULL;
247  int ret, status;
248  int64_t pts;
249 
251 
252  if (s->padding) {
253  int nb_samples = FFMIN(s->padding, 2048);
254 
255  frame = ff_get_audio_buffer(outlink, nb_samples);
256  if (!frame)
257  return AVERROR(ENOMEM);
258  s->padding -= nb_samples;
259 
260  av_samples_set_silence(frame->extended_data, 0,
261  frame->nb_samples,
262  outlink->channels,
263  frame->format);
264 
265  frame->pts = s->next_pts;
266  if (s->next_pts != AV_NOPTS_VALUE)
267  s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
268 
269  return ff_filter_frame(outlink, frame);
270  }
271 
273  if (ret < 0)
274  return ret;
275 
276  if (ret > 0)
277  return filter_frame(inlink, frame);
278 
280  if (status == AVERROR_EOF)
281  s->eof = 1;
282  }
283 
284  if (s->eof && s->max_delay) {
285  int nb_samples = FFMIN(s->max_delay, 2048);
286 
287  frame = ff_get_audio_buffer(outlink, nb_samples);
288  if (!frame)
289  return AVERROR(ENOMEM);
290  s->max_delay -= nb_samples;
291 
292  av_samples_set_silence(frame->extended_data, 0,
293  frame->nb_samples,
294  outlink->channels,
295  frame->format);
296 
297  frame->pts = s->next_pts;
298  return filter_frame(inlink, frame);
299  }
300 
301  if (s->eof && s->max_delay == 0) {
302  ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
303  return 0;
304  }
305 
306  if (!s->eof)
308 
309  return FFERROR_NOT_READY;
310 }
311 
313 {
314  AudioDelayContext *s = ctx->priv;
315 
316  if (s->chandelay) {
317  for (int i = 0; i < s->nb_delays; i++)
318  av_freep(&s->chandelay[i].samples);
319  }
320  av_freep(&s->chandelay);
321 }
322 
323 static const AVFilterPad adelay_inputs[] = {
324  {
325  .name = "default",
326  .type = AVMEDIA_TYPE_AUDIO,
327  .config_props = config_input,
328  },
329 };
330 
331 static const AVFilterPad adelay_outputs[] = {
332  {
333  .name = "default",
334  .type = AVMEDIA_TYPE_AUDIO,
335  },
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,
349 };
AudioDelayContext::next_pts
int64_t next_pts
Definition: af_adelay.c:46
ff_get_audio_buffer
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:88
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
status
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
AVERROR
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
opt.h
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1019
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
inlink
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
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:112
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:396
AVOption
AVOption.
Definition: opt.h:247
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(adelay)
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:68
ChanDelay
Definition: af_adelay.c:30
ff_set_common_all_samplerates
int ff_set_common_all_samplerates(AVFilterContext *ctx)
Equivalent to ff_set_common_samplerates(ctx, ff_all_samplerates())
Definition: formats.c:682
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:153
ChanDelay::delay
int64_t delay
Definition: af_adelay.c:31
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
ff_af_adelay
const AVFilter ff_af_adelay
Definition: af_adelay.c:338
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_adelay.c:64
OFFSET
#define OFFSET(x)
Definition: af_adelay.c:53
ff_inlink_consume_frame
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1418
AudioDelayContext
Definition: af_adelay.c:37
samplefmt.h
type
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
Definition: writing_filters.txt:86
pts
static int64_t pts
Definition: transcode_aac.c:653
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
activate
static int activate(AVFilterContext *ctx)
Definition: af_adelay.c:241
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_adelay.c:117
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AudioDelayContext::delay_channel
void(* delay_channel)(ChanDelay *d, int nb_samples, const uint8_t *src, uint8_t *dst)
Definition: af_adelay.c:49
av_cold
#define av_cold
Definition: attributes.h:90
ff_outlink_set_status
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
s
#define s(width, name)
Definition: cbs_vp9.c:257
AudioDelayContext::delays
char * delays
Definition: af_adelay.c:40
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_strtok
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:186
ff_set_common_formats_from_list
int ff_set_common_formats_from_list(AVFilterContext *ctx, const int *fmts)
Equivalent to ff_set_common_formats(ctx, ff_make_format_list(fmts))
Definition: formats.c:698
filters.h
adelay_options
static const AVOption adelay_options[]
Definition: af_adelay.c:56
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AudioDelayContext::nb_delays
int nb_delays
Definition: af_adelay.c:42
AudioDelayContext::padding
int64_t padding
Definition: af_adelay.c:44
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:141
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:152
arg
const char * arg
Definition: jacosubdec.c:67
av_sscanf
int av_sscanf(const char *string, const char *format,...)
See libc sscanf manual for more information.
Definition: avsscanf.c:960
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:537
adelay_outputs
static const AVFilterPad adelay_outputs[]
Definition: af_adelay.c:331
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
A
#define A
Definition: af_adelay.c:54
src
#define src
Definition: vp8dsp.c:255
ff_set_common_all_channel_counts
int ff_set_common_all_channel_counts(AVFilterContext *ctx)
Equivalent to ff_set_common_channel_layouts(ctx, ff_all_channel_counts())
Definition: formats.c:664
ff_inlink_acknowledge_status
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:1372
eval.h
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AV_SAMPLE_FMT_U8P
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition: samplefmt.h:66
DELAY
#define DELAY(name, type, fill)
Definition: af_adelay.c:82
AudioDelayContext::block_align
int block_align
Definition: af_adelay.c:43
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_adelay.c:207
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:67
internal.h
ChanDelay::index
size_t index
Definition: af_adelay.c:33
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
AudioDelayContext::chandelay
ChanDelay * chandelay
Definition: af_adelay.c:41
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:350
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
ChanDelay::samples
uint8_t * samples
Definition: af_adelay.c:34
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:271
av_samples_set_silence
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:244
AVFilter
Filter definition.
Definition: avfilter.h:149
ret
ret
Definition: filter_design.txt:187
frame
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
Definition: filter_design.txt:264
adelay_inputs
static const AVFilterPad adelay_inputs[]
Definition: af_adelay.c:323
avfilter.h
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:70
AudioDelayContext::eof
int eof
Definition: af_adelay.c:47
AVFilterContext
An instance of a filter.
Definition: avfilter.h:346
audio.h
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:241
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:153
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AudioDelayContext::all
int all
Definition: af_adelay.c:39
d
d
Definition: ffmpeg_filter.c:156
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_adelay.c:312
int32_t
int32_t
Definition: audioconvert.c:56
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
#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:138
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ChanDelay::delay_index
size_t delay_index
Definition: af_adelay.c:32
avstring.h
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:228
AudioDelayContext::max_delay
int64_t max_delay
Definition: af_adelay.c:45