FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/opt.h"
23 #include "libavutil/samplefmt.h"
24 #include "avfilter.h"
25 #include "audio.h"
26 #include "filters.h"
27 #include "internal.h"
28 
29 typedef struct ChanDelay {
30  int delay;
31  unsigned delay_index;
32  unsigned index;
34 } ChanDelay;
35 
36 typedef struct AudioDelayContext {
37  const AVClass *class;
38  char *delays;
40  int nb_delays;
42  int64_t padding;
43  int64_t max_delay;
44  int64_t next_pts;
45  int eof;
46 
47  void (*delay_channel)(ChanDelay *d, int nb_samples,
48  const uint8_t *src, uint8_t *dst);
50 
51 #define OFFSET(x) offsetof(AudioDelayContext, x)
52 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
53 
54 static const AVOption adelay_options[] = {
55  { "delays", "set list of delays for each channel", OFFSET(delays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
56  { NULL }
57 };
58 
59 AVFILTER_DEFINE_CLASS(adelay);
60 
62 {
65  static const enum AVSampleFormat sample_fmts[] = {
69  };
70  int ret;
71 
72  layouts = ff_all_channel_counts();
73  if (!layouts)
74  return AVERROR(ENOMEM);
75  ret = ff_set_common_channel_layouts(ctx, layouts);
76  if (ret < 0)
77  return ret;
78 
79  formats = ff_make_format_list(sample_fmts);
80  if (!formats)
81  return AVERROR(ENOMEM);
82  ret = ff_set_common_formats(ctx, formats);
83  if (ret < 0)
84  return ret;
85 
86  formats = ff_all_samplerates();
87  if (!formats)
88  return AVERROR(ENOMEM);
89  return ff_set_common_samplerates(ctx, formats);
90 }
91 
92 #define DELAY(name, type, fill) \
93 static void delay_channel_## name ##p(ChanDelay *d, int nb_samples, \
94  const uint8_t *ssrc, uint8_t *ddst) \
95 { \
96  const type *src = (type *)ssrc; \
97  type *dst = (type *)ddst; \
98  type *samples = (type *)d->samples; \
99  \
100  while (nb_samples) { \
101  if (d->delay_index < d->delay) { \
102  const int len = FFMIN(nb_samples, d->delay - d->delay_index); \
103  \
104  memcpy(&samples[d->delay_index], src, len * sizeof(type)); \
105  memset(dst, fill, len * sizeof(type)); \
106  d->delay_index += len; \
107  src += len; \
108  dst += len; \
109  nb_samples -= len; \
110  } else { \
111  *dst = samples[d->index]; \
112  samples[d->index] = *src; \
113  nb_samples--; \
114  d->index++; \
115  src++, dst++; \
116  d->index = d->index >= d->delay ? 0 : d->index; \
117  } \
118  } \
119 }
120 
121 DELAY(u8, uint8_t, 0x80)
122 DELAY(s16, int16_t, 0)
123 DELAY(s32, int32_t, 0)
124 DELAY(flt, float, 0)
125 DELAY(dbl, double, 0)
126 
127 static int config_input(AVFilterLink *inlink)
128 {
129  AVFilterContext *ctx = inlink->dst;
130  AudioDelayContext *s = ctx->priv;
131  char *p, *arg, *saveptr = NULL;
132  int i;
133 
134  s->chandelay = av_calloc(inlink->channels, sizeof(*s->chandelay));
135  if (!s->chandelay)
136  return AVERROR(ENOMEM);
137  s->nb_delays = inlink->channels;
138  s->block_align = av_get_bytes_per_sample(inlink->format);
139 
140  p = s->delays;
141  for (i = 0; i < s->nb_delays; i++) {
142  ChanDelay *d = &s->chandelay[i];
143  float delay;
144  char type = 0;
145  int ret;
146 
147  if (!(arg = av_strtok(p, "|", &saveptr)))
148  break;
149 
150  p = NULL;
151 
152  ret = sscanf(arg, "%d%c", &d->delay, &type);
153  if (ret != 2 || type != 'S') {
154  sscanf(arg, "%f", &delay);
155  d->delay = delay * inlink->sample_rate / 1000.0;
156  }
157 
158  if (d->delay < 0) {
159  av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n");
160  return AVERROR(EINVAL);
161  }
162  }
163 
164  s->padding = s->chandelay[0].delay;
165  for (i = 1; i < s->nb_delays; i++) {
166  ChanDelay *d = &s->chandelay[i];
167 
168  s->padding = FFMIN(s->padding, d->delay);
169  }
170 
171  if (s->padding) {
172  for (i = 0; i < s->nb_delays; i++) {
173  ChanDelay *d = &s->chandelay[i];
174 
175  d->delay -= s->padding;
176  }
177  }
178 
179  for (i = 0; i < s->nb_delays; i++) {
180  ChanDelay *d = &s->chandelay[i];
181 
182  if (!d->delay)
183  continue;
184 
186  if (!d->samples)
187  return AVERROR(ENOMEM);
188 
189  s->max_delay = FFMAX(s->max_delay, d->delay);
190  }
191 
192  switch (inlink->format) {
193  case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ; break;
194  case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p; break;
195  case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p; break;
196  case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp; break;
197  case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp; break;
198  }
199 
200  return 0;
201 }
202 
203 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
204 {
205  AVFilterContext *ctx = inlink->dst;
206  AudioDelayContext *s = ctx->priv;
207  AVFrame *out_frame;
208  int i;
209 
210  if (ctx->is_disabled || !s->delays)
211  return ff_filter_frame(ctx->outputs[0], frame);
212 
213  out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
214  if (!out_frame) {
215  av_frame_free(&frame);
216  return AVERROR(ENOMEM);
217  }
218  av_frame_copy_props(out_frame, frame);
219 
220  for (i = 0; i < s->nb_delays; i++) {
221  ChanDelay *d = &s->chandelay[i];
222  const uint8_t *src = frame->extended_data[i];
223  uint8_t *dst = out_frame->extended_data[i];
224 
225  if (!d->delay)
226  memcpy(dst, src, frame->nb_samples * s->block_align);
227  else
228  s->delay_channel(d, frame->nb_samples, src, dst);
229  }
230 
231  out_frame->pts = s->next_pts;
232  s->next_pts += av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
234  return ff_filter_frame(ctx->outputs[0], out_frame);
235 }
236 
238 {
239  AVFilterLink *inlink = ctx->inputs[0];
240  AVFilterLink *outlink = ctx->outputs[0];
241  AudioDelayContext *s = ctx->priv;
242  AVFrame *frame = NULL;
243  int ret, status;
244  int64_t pts;
245 
246  FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
247 
248  if (s->padding) {
249  int nb_samples = FFMIN(s->padding, 2048);
250 
251  frame = ff_get_audio_buffer(outlink, nb_samples);
252  if (!frame)
253  return AVERROR(ENOMEM);
254  s->padding -= nb_samples;
255 
257  frame->nb_samples,
258  outlink->channels,
259  frame->format);
260 
261  frame->pts = s->next_pts;
262  if (s->next_pts != AV_NOPTS_VALUE)
263  s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
264 
265  return ff_filter_frame(outlink, frame);
266  }
267 
268  ret = ff_inlink_consume_frame(inlink, &frame);
269  if (ret < 0)
270  return ret;
271 
272  if (ret > 0)
273  return filter_frame(inlink, frame);
274 
275  if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
276  if (status == AVERROR_EOF)
277  s->eof = 1;
278  }
279 
280  if (s->eof && s->max_delay) {
281  int nb_samples = FFMIN(s->max_delay, 2048);
282 
283  frame = ff_get_audio_buffer(outlink, nb_samples);
284  if (!frame)
285  return AVERROR(ENOMEM);
286  s->max_delay -= nb_samples;
287 
289  frame->nb_samples,
290  outlink->channels,
291  frame->format);
292 
293  frame->pts = s->next_pts;
294  return filter_frame(inlink, frame);
295  }
296 
297  if (s->eof && s->max_delay == 0) {
299  return 0;
300  }
301 
302  if (!s->eof)
303  FF_FILTER_FORWARD_WANTED(outlink, inlink);
304 
305  return FFERROR_NOT_READY;
306 }
307 
309 {
310  AudioDelayContext *s = ctx->priv;
311 
312  if (s->chandelay) {
313  for (int i = 0; i < s->nb_delays; i++)
314  av_freep(&s->chandelay[i].samples);
315  }
316  av_freep(&s->chandelay);
317 }
318 
319 static const AVFilterPad adelay_inputs[] = {
320  {
321  .name = "default",
322  .type = AVMEDIA_TYPE_AUDIO,
323  .config_props = config_input,
324  },
325  { NULL }
326 };
327 
328 static const AVFilterPad adelay_outputs[] = {
329  {
330  .name = "default",
331  .type = AVMEDIA_TYPE_AUDIO,
332  },
333  { NULL }
334 };
335 
337  .name = "adelay",
338  .description = NULL_IF_CONFIG_SMALL("Delay one or more audio channels."),
339  .query_formats = query_formats,
340  .priv_size = sizeof(AudioDelayContext),
341  .priv_class = &adelay_class,
342  .activate = activate,
343  .uninit = uninit,
344  .inputs = adelay_inputs,
345  .outputs = adelay_outputs,
347 };
float, planar
Definition: samplefmt.h:69
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: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:226
static int activate(AVFilterContext *ctx)
Definition: af_adelay.c:237
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_adelay.c:203
AVOption.
Definition: opt.h:246
#define OFFSET(x)
Definition: af_adelay.c:51
Main libavfilter public API header.
double, planar
Definition: samplefmt.h:70
#define FFERROR_NOT_READY
Filters implementation helper functions.
Definition: filters.h:34
#define src
Definition: vp8dsp.c:254
int64_t next_pts
Definition: af_adelay.c:44
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:33
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
void(* delay_channel)(ChanDelay *d, int nb_samples, const uint8_t *src, uint8_t *dst)
Definition: af_adelay.c:47
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:319
static AVFrame * frame
int64_t max_delay
Definition: af_adelay.c:43
#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:54
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:308
#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
#define AVERROR(e)
Definition: error.h:43
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:52
#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 delay
Definition: af_adelay.c:30
AVFILTER_DEFINE_CLASS(adelay)
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition: filters.h:254
#define FFMIN(a, b)
Definition: common.h:96
static int query_formats(AVFilterContext *ctx)
Definition: af_adelay.c:61
AVFilter ff_af_adelay
Definition: af_adelay.c:336
signed 32 bits, planar
Definition: samplefmt.h:68
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
unsigned 8 bits, planar
Definition: samplefmt.h:66
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
ChanDelay * chandelay
Definition: af_adelay.c:39
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:299
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
unsigned delay_index
Definition: af_adelay.c:31
int64_t padding
Definition: af_adelay.c:42
GLint GLenum type
Definition: opengl_enc.c:105
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
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:596
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
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:319
static int config_input(AVFilterLink *inlink)
Definition: af_adelay.c:127
unsigned index
Definition: af_adelay.c:32
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:328
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define DELAY(name, type, fill)
Definition: af_adelay.c:92
#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
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:273
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:292
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