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 "internal.h"
27 
28 typedef struct ChanDelay {
29  int delay;
30  unsigned delay_index;
31  unsigned index;
33 } ChanDelay;
34 
35 typedef struct AudioDelayContext {
36  const AVClass *class;
37  char *delays;
39  int nb_delays;
41  unsigned max_delay;
42  int64_t next_pts;
43 
44  void (*delay_channel)(ChanDelay *d, int nb_samples,
45  const uint8_t *src, uint8_t *dst);
47 
48 #define OFFSET(x) offsetof(AudioDelayContext, x)
49 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50 
51 static const AVOption adelay_options[] = {
52  { "delays", "set list of delays for each channel", OFFSET(delays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
53  { NULL }
54 };
55 
56 AVFILTER_DEFINE_CLASS(adelay);
57 
59 {
62  static const enum AVSampleFormat sample_fmts[] = {
66  };
67  int ret;
68 
69  layouts = ff_all_channel_counts();
70  if (!layouts)
71  return AVERROR(ENOMEM);
72  ret = ff_set_common_channel_layouts(ctx, layouts);
73  if (ret < 0)
74  return ret;
75 
76  formats = ff_make_format_list(sample_fmts);
77  if (!formats)
78  return AVERROR(ENOMEM);
79  ret = ff_set_common_formats(ctx, formats);
80  if (ret < 0)
81  return ret;
82 
83  formats = ff_all_samplerates();
84  if (!formats)
85  return AVERROR(ENOMEM);
86  return ff_set_common_samplerates(ctx, formats);
87 }
88 
89 #define DELAY(name, type, fill) \
90 static void delay_channel_## name ##p(ChanDelay *d, int nb_samples, \
91  const uint8_t *ssrc, uint8_t *ddst) \
92 { \
93  const type *src = (type *)ssrc; \
94  type *dst = (type *)ddst; \
95  type *samples = (type *)d->samples; \
96  \
97  while (nb_samples) { \
98  if (d->delay_index < d->delay) { \
99  const int len = FFMIN(nb_samples, d->delay - d->delay_index); \
100  \
101  memcpy(&samples[d->delay_index], src, len * sizeof(type)); \
102  memset(dst, fill, len * sizeof(type)); \
103  d->delay_index += len; \
104  src += len; \
105  dst += len; \
106  nb_samples -= len; \
107  } else { \
108  *dst = samples[d->index]; \
109  samples[d->index] = *src; \
110  nb_samples--; \
111  d->index++; \
112  src++, dst++; \
113  d->index = d->index >= d->delay ? 0 : d->index; \
114  } \
115  } \
116 }
117 
118 DELAY(u8, uint8_t, 0x80)
119 DELAY(s16, int16_t, 0)
120 DELAY(s32, int32_t, 0)
121 DELAY(flt, float, 0)
122 DELAY(dbl, double, 0)
123 
124 static int config_input(AVFilterLink *inlink)
125 {
126  AVFilterContext *ctx = inlink->dst;
127  AudioDelayContext *s = ctx->priv;
128  char *p, *arg, *saveptr = NULL;
129  int i;
130 
131  s->chandelay = av_calloc(inlink->channels, sizeof(*s->chandelay));
132  if (!s->chandelay)
133  return AVERROR(ENOMEM);
134  s->nb_delays = inlink->channels;
135  s->block_align = av_get_bytes_per_sample(inlink->format);
136 
137  p = s->delays;
138  for (i = 0; i < s->nb_delays; i++) {
139  ChanDelay *d = &s->chandelay[i];
140  float delay;
141 
142  if (!(arg = av_strtok(p, "|", &saveptr)))
143  break;
144 
145  p = NULL;
146  sscanf(arg, "%f", &delay);
147 
148  d->delay = delay * inlink->sample_rate / 1000.0;
149  if (d->delay < 0) {
150  av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n");
151  return AVERROR(EINVAL);
152  }
153  }
154 
155  for (i = 0; i < s->nb_delays; i++) {
156  ChanDelay *d = &s->chandelay[i];
157 
158  if (!d->delay)
159  continue;
160 
162  if (!d->samples)
163  return AVERROR(ENOMEM);
164 
165  s->max_delay = FFMAX(s->max_delay, d->delay);
166  }
167 
168  if (!s->max_delay) {
169  av_log(ctx, AV_LOG_ERROR, "At least one delay >0 must be specified.\n");
170  return AVERROR(EINVAL);
171  }
172 
173  switch (inlink->format) {
174  case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ; break;
175  case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p; break;
176  case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p; break;
177  case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp; break;
178  case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp; break;
179  }
180 
181  return 0;
182 }
183 
184 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
185 {
186  AVFilterContext *ctx = inlink->dst;
187  AudioDelayContext *s = ctx->priv;
188  AVFrame *out_frame;
189  int i;
190 
191  if (ctx->is_disabled || !s->delays)
192  return ff_filter_frame(ctx->outputs[0], frame);
193 
194  out_frame = ff_get_audio_buffer(inlink, frame->nb_samples);
195  if (!out_frame) {
196  av_frame_free(&frame);
197  return AVERROR(ENOMEM);
198  }
199  av_frame_copy_props(out_frame, frame);
200 
201  for (i = 0; i < s->nb_delays; i++) {
202  ChanDelay *d = &s->chandelay[i];
203  const uint8_t *src = frame->extended_data[i];
204  uint8_t *dst = out_frame->extended_data[i];
205 
206  if (!d->delay)
207  memcpy(dst, src, frame->nb_samples * s->block_align);
208  else
209  s->delay_channel(d, frame->nb_samples, src, dst);
210  }
211 
212  s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
214  return ff_filter_frame(ctx->outputs[0], out_frame);
215 }
216 
217 static int request_frame(AVFilterLink *outlink)
218 {
219  AVFilterContext *ctx = outlink->src;
220  AudioDelayContext *s = ctx->priv;
221  int ret;
222 
223  ret = ff_request_frame(ctx->inputs[0]);
224  if (ret == AVERROR_EOF && !ctx->is_disabled && s->max_delay) {
225  int nb_samples = FFMIN(s->max_delay, 2048);
226  AVFrame *frame;
227 
228  frame = ff_get_audio_buffer(outlink, nb_samples);
229  if (!frame)
230  return AVERROR(ENOMEM);
231  s->max_delay -= nb_samples;
232 
234  frame->nb_samples,
235  outlink->channels,
236  frame->format);
237 
238  frame->pts = s->next_pts;
239  if (s->next_pts != AV_NOPTS_VALUE)
240  s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
241 
242  ret = filter_frame(ctx->inputs[0], frame);
243  }
244 
245  return ret;
246 }
247 
249 {
250  AudioDelayContext *s = ctx->priv;
251  int i;
252 
253  for (i = 0; i < s->nb_delays; i++)
254  av_freep(&s->chandelay[i].samples);
255  av_freep(&s->chandelay);
256 }
257 
258 static const AVFilterPad adelay_inputs[] = {
259  {
260  .name = "default",
261  .type = AVMEDIA_TYPE_AUDIO,
262  .config_props = config_input,
263  .filter_frame = filter_frame,
264  },
265  { NULL }
266 };
267 
268 static const AVFilterPad adelay_outputs[] = {
269  {
270  .name = "default",
271  .request_frame = request_frame,
272  .type = AVMEDIA_TYPE_AUDIO,
273  },
274  { NULL }
275 };
276 
278  .name = "adelay",
279  .description = NULL_IF_CONFIG_SMALL("Delay one or more audio channels."),
280  .query_formats = query_formats,
281  .priv_size = sizeof(AudioDelayContext),
282  .priv_class = &adelay_class,
283  .uninit = uninit,
284  .inputs = adelay_inputs,
285  .outputs = adelay_outputs,
287 };
float, planar
Definition: samplefmt.h:70
#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
const char * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_adelay.c:184
AVOption.
Definition: opt.h:245
#define OFFSET(x)
Definition: af_adelay.c:48
AVFormatContext * ctx
Definition: movenc-test.c:48
Main libavfilter public API header.
double, planar
Definition: samplefmt.h:71
static enum AVSampleFormat formats[]
int64_t next_pts
Definition: af_adelay.c:42
int is_disabled
the enabled state from the last expression evaluation
Definition: avfilter.h:351
uint8_t * samples
Definition: af_adelay.c:32
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:59
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:312
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1163
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:44
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:262
static AVFrame * frame
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define av_log(a,...)
static const AVOption adelay_options[]
Definition: af_adelay.c:51
A filter pad used for either input or output.
Definition: internal.h:53
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
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_adelay.c:248
#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:235
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:65
#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:154
#define A
Definition: af_adelay.c:49
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
void * priv
private data for use by the filter
Definition: avfilter.h:319
const char * arg
Definition: jacosubdec.c:66
#define FFMAX(a, b)
Definition: common.h:94
int delay
Definition: af_adelay.c:29
AVFILTER_DEFINE_CLASS(adelay)
#define FFMIN(a, b)
Definition: common.h:96
static int query_formats(AVFilterContext *ctx)
Definition: af_adelay.c:58
AVFilter ff_af_adelay
Definition: af_adelay.c:277
signed 32 bits, planar
Definition: samplefmt.h:69
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
int32_t
unsigned 8 bits, planar
Definition: samplefmt.h:67
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:385
#define src
Definition: vp9dsp.c:530
ChanDelay * chandelay
Definition: af_adelay.c:38
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:242
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:375
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
unsigned delay_index
Definition: af_adelay.c:30
unsigned max_delay
Definition: af_adelay.c:41
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:141
rational number numerator/denominator
Definition: rational.h:43
const char * name
Filter name.
Definition: avfilter.h:145
#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:130
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:316
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
void * av_calloc(size_t nmemb, size_t size)
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
Definition: mem.c:260
static int flags
Definition: cpu.c:47
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:104
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:258
static int config_input(AVFilterLink *inlink)
Definition: af_adelay.c:124
unsigned index
Definition: af_adelay.c:31
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:304
static const AVFilterPad adelay_outputs[]
Definition: af_adelay.c:268
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
static int request_frame(AVFilterLink *outlink)
Definition: af_adelay.c:217
#define DELAY(name, type, fill)
Definition: af_adelay.c:89
#define av_freep(p)
signed 16 bits, planar
Definition: samplefmt.h:68
#define av_malloc_array(a, b)
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:356
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:225
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:235
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:565
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240