FFmpeg  2.1.1
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 
22 #include "libavutil/avstring.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/samplefmt.h"
25 #include "avfilter.h"
26 #include "audio.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  unsigned max_delay;
43  int64_t next_pts;
44 
45  void (*delay_channel)(ChanDelay *d, int nb_samples,
46  const uint8_t *src, uint8_t *dst);
48 
49 #define OFFSET(x) offsetof(AudioDelayContext, x)
50 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51 
52 static const AVOption adelay_options[] = {
53  { "delays", "set list of delays for each channel", OFFSET(delays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
54  { NULL }
55 };
56 
57 AVFILTER_DEFINE_CLASS(adelay);
58 
60 {
63  static const enum AVSampleFormat sample_fmts[] = {
67  };
68 
69  layouts = ff_all_channel_layouts();
70  if (!layouts)
71  return AVERROR(ENOMEM);
72  ff_set_common_channel_layouts(ctx, layouts);
73 
74  formats = ff_make_format_list(sample_fmts);
75  if (!formats)
76  return AVERROR(ENOMEM);
77  ff_set_common_formats(ctx, formats);
78 
79  formats = ff_all_samplerates();
80  if (!formats)
81  return AVERROR(ENOMEM);
82  ff_set_common_samplerates(ctx, formats);
83 
84  return 0;
85 }
86 
87 #define DELAY(name, type, fill) \
88 static void delay_channel_## name ##p(ChanDelay *d, int nb_samples, \
89  const uint8_t *ssrc, uint8_t *ddst) \
90 { \
91  const type *src = (type *)ssrc; \
92  type *dst = (type *)ddst; \
93  type *samples = (type *)d->samples; \
94  \
95  while (nb_samples) { \
96  if (d->delay_index < d->delay) { \
97  const int len = FFMIN(nb_samples, d->delay - d->delay_index); \
98  \
99  memcpy(&samples[d->delay_index], src, len * sizeof(type)); \
100  memset(dst, fill, len * sizeof(type)); \
101  d->delay_index += len; \
102  src += len; \
103  dst += len; \
104  nb_samples -= len; \
105  } else { \
106  *dst = samples[d->index]; \
107  samples[d->index] = *src; \
108  nb_samples--; \
109  d->index++; \
110  src++, dst++; \
111  d->index = d->index >= d->delay ? 0 : d->index; \
112  } \
113  } \
114 }
115 
116 DELAY(u8, uint8_t, 0x80)
117 DELAY(s16, int16_t, 0)
118 DELAY(s32, int32_t, 0)
119 DELAY(flt, float, 0)
120 DELAY(dbl, double, 0)
121 
122 static int config_input(AVFilterLink *inlink)
123 {
124  AVFilterContext *ctx = inlink->dst;
125  AudioDelayContext *s = ctx->priv;
126  char *p, *arg, *saveptr = NULL;
127  int i;
128 
129  s->chandelay = av_calloc(inlink->channels, sizeof(*s->chandelay));
130  if (!s->chandelay)
131  return AVERROR(ENOMEM);
132  s->nb_delays = inlink->channels;
133  s->block_align = av_get_bytes_per_sample(inlink->format);
134 
135  p = s->delays;
136  for (i = 0; i < s->nb_delays; i++) {
137  ChanDelay *d = &s->chandelay[i];
138  float delay;
139 
140  if (!(arg = av_strtok(p, "|", &saveptr)))
141  break;
142 
143  p = NULL;
144  sscanf(arg, "%f", &delay);
145 
146  d->delay = delay * inlink->sample_rate / 1000.0;
147  if (d->delay < 0) {
148  av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n");
149  return AVERROR(EINVAL);
150  }
151  }
152 
153  for (i = 0; i < s->nb_delays; i++) {
154  ChanDelay *d = &s->chandelay[i];
155 
156  if (!d->delay)
157  continue;
158 
160  if (!d->samples)
161  return AVERROR(ENOMEM);
162 
163  s->max_delay = FFMAX(s->max_delay, d->delay);
164  }
165 
166  if (!s->max_delay) {
167  av_log(ctx, AV_LOG_ERROR, "At least one delay >0 must be specified.\n");
168  return AVERROR(EINVAL);
169  }
170 
171  switch (inlink->format) {
172  case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ; break;
173  case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p; break;
174  case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p; break;
175  case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp; break;
176  case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp; break;
177  }
178 
179  return 0;
180 }
181 
182 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
183 {
184  AVFilterContext *ctx = inlink->dst;
185  AudioDelayContext *s = ctx->priv;
186  AVFrame *out_frame;
187  int i;
188 
189  if (ctx->is_disabled || !s->delays)
190  return ff_filter_frame(ctx->outputs[0], frame);
191 
192  out_frame = ff_get_audio_buffer(inlink, frame->nb_samples);
193  if (!out_frame)
194  return AVERROR(ENOMEM);
195  av_frame_copy_props(out_frame, frame);
196 
197  for (i = 0; i < s->nb_delays; i++) {
198  ChanDelay *d = &s->chandelay[i];
199  const uint8_t *src = frame->extended_data[i];
200  uint8_t *dst = out_frame->extended_data[i];
201 
202  if (!d->delay)
203  memcpy(dst, src, frame->nb_samples * s->block_align);
204  else
205  s->delay_channel(d, frame->nb_samples, src, dst);
206  }
207 
208  s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
210  return ff_filter_frame(ctx->outputs[0], out_frame);
211 }
212 
213 static int request_frame(AVFilterLink *outlink)
214 {
215  AVFilterContext *ctx = outlink->src;
216  AudioDelayContext *s = ctx->priv;
217  int ret;
218 
219  ret = ff_request_frame(ctx->inputs[0]);
220  if (ret == AVERROR_EOF && !ctx->is_disabled && s->max_delay) {
221  int nb_samples = FFMIN(s->max_delay, 2048);
222  AVFrame *frame;
223 
224  frame = ff_get_audio_buffer(outlink, nb_samples);
225  if (!frame)
226  return AVERROR(ENOMEM);
227  s->max_delay -= nb_samples;
228 
230  frame->nb_samples,
231  outlink->channels,
232  frame->format);
233 
234  frame->pts = s->next_pts;
235  if (s->next_pts != AV_NOPTS_VALUE)
236  s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
237 
238  ret = filter_frame(ctx->inputs[0], frame);
239  }
240 
241  return ret;
242 }
243 
244 static av_cold void uninit(AVFilterContext *ctx)
245 {
246  AudioDelayContext *s = ctx->priv;
247  int i;
248 
249  for (i = 0; i < s->nb_delays; i++)
250  av_free(s->chandelay[i].samples);
251  av_freep(&s->chandelay);
252 }
253 
254 static const AVFilterPad adelay_inputs[] = {
255  {
256  .name = "default",
257  .type = AVMEDIA_TYPE_AUDIO,
258  .config_props = config_input,
259  .filter_frame = filter_frame,
260  },
261  { NULL }
262 };
263 
264 static const AVFilterPad adelay_outputs[] = {
265  {
266  .name = "default",
267  .request_frame = request_frame,
268  .type = AVMEDIA_TYPE_AUDIO,
269  },
270  { NULL }
271 };
272 
274  .name = "adelay",
275  .description = NULL_IF_CONFIG_SMALL("Delay one or more audio channels."),
276  .query_formats = query_formats,
277  .priv_size = sizeof(AudioDelayContext),
278  .priv_class = &adelay_class,
279  .uninit = uninit,
280  .inputs = adelay_inputs,
281  .outputs = adelay_outputs,
283 };
AVFilter avfilter_af_adelay
Definition: af_adelay.c:273
const char * s
Definition: avisynth_c.h:668
void * av_calloc(size_t nmemb, size_t size) av_malloc_attrib
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
Definition: mem.c:249
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_adelay.c:182
AVOption.
Definition: opt.h:253
#define OFFSET(x)
Definition: af_adelay.c:49
const char * name
Filter name.
Definition: avfilter.h:468
void * priv
private data for use by the filter
Definition: avfilter.h:648
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:111
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:140
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
static enum AVSampleFormat formats[]
int64_t next_pts
Definition: af_adelay.c:43
#define av_cold
Definition: avcodec.h:653
int is_disabled
the enabled state from the last expression evaluation
Definition: avfilter.h:680
uint8_t * samples
Definition: af_adelay.c:33
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:294
const char * name
Pad name.
Definition: internal.h:66
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1118
uint8_t
void(* delay_channel)(ChanDelay *d, int nb_samples, const uint8_t *src, uint8_t *dst)
Definition: af_adelay.c:45
signed 32 bits, planar
Definition: samplefmt.h:59
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only &quot;metadata&quot; fields from src to dst.
Definition: frame.c:446
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:182
void 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:531
#define A(x)
Definition: vp56_arith.h:28
static const AVOption adelay_options[]
Definition: af_adelay.c:52
static AVFrame * frame
Definition: demuxing.c:51
A filter pad used for either input or output.
Definition: internal.h:60
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:130
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_adelay.c:244
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:219
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:70
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
const char * arg
Definition: jacosubdec.c:69
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:123
unsigned 8 bits, planar
Definition: samplefmt.h:57
int delay
Definition: af_adelay.c:30
static int query_formats(AVFilterContext *ctx)
Definition: af_adelay.c:59
ret
Definition: avfilter.c:961
int32_t
#define FFMIN(a, b)
Definition: avcodec.h:925
ChanDelay * chandelay
Definition: af_adelay.c:39
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (w...
Definition: formats.c:388
A list of supported channel layouts.
Definition: formats.h:85
Main libavfilter public API header.
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:642
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:157
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
AVS_Value src
Definition: avisynth_c.h:523
#define FFMAX(a, b)
Definition: avcodec.h:923
typedef void(RENAME(mix_any_func_type))
unsigned delay_index
Definition: af_adelay.c:31
unsigned max_delay
Definition: af_adelay.c:42
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:464
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:102
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:635
rational number numerator/denominator
Definition: rational.h:43
#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:453
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:382
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:249
static int flags
Definition: cpu.c:45
#define AVERROR_EOF
static void * av_malloc_array(size_t nmemb, size_t size)
Definition: mem.h:93
void ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:519
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:183
static const AVFilterPad adelay_inputs[]
Definition: af_adelay.c:254
static int config_input(AVFilterLink *inlink)
Definition: af_adelay.c:122
unsigned index
Definition: af_adelay.c:32
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:301
A list of supported formats for one end of a filter link.
Definition: formats.h:64
#define AVERROR(e)
float, planar
Definition: samplefmt.h:60
An instance of a filter.
Definition: avfilter.h:627
static const AVFilterPad adelay_outputs[]
Definition: af_adelay.c:264
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
static int request_frame(AVFilterLink *outlink)
Definition: af_adelay.c:213
#define DELAY(name, type, fill)
Definition: af_adelay.c:87
signed 16 bits, planar
Definition: samplefmt.h:58
void 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:512
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:335
internal API functions
double, planar
Definition: samplefmt.h:61
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:104
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:150
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avcodec.h:2278