af_aphaser.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  * @file
23  * phaser audio filter
24  */
25 
26 #include "libavutil/avassert.h"
27 #include "libavutil/opt.h"
28 #include "audio.h"
29 #include "avfilter.h"
30 #include "internal.h"
31 
32 enum WaveType {
36 };
37 
38 typedef struct AudioPhaserContext {
39  const AVClass *class;
40  double in_gain, out_gain;
41  double delay;
42  double decay;
43  double speed;
44 
45  enum WaveType type;
46 
48  double *delay_buffer;
49 
52 
54 
56  uint8_t * const *src, uint8_t **dst,
57  int nb_samples, int channels);
59 
60 #define OFFSET(x) offsetof(AudioPhaserContext, x)
61 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
62 
63 static const AVOption aphaser_options[] = {
64  { "in_gain", "set input gain", OFFSET(in_gain), AV_OPT_TYPE_DOUBLE, {.dbl=.4}, 0, 1, FLAGS },
65  { "out_gain", "set output gain", OFFSET(out_gain), AV_OPT_TYPE_DOUBLE, {.dbl=.74}, 0, 1e9, FLAGS },
66  { "delay", "set delay in milliseconds", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=3.}, 0, 5, FLAGS },
67  { "decay", "set decay", OFFSET(decay), AV_OPT_TYPE_DOUBLE, {.dbl=.4}, 0, .99, FLAGS },
68  { "speed", "set modulation speed", OFFSET(speed), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, .1, 2, FLAGS },
69  { "type", "set modulation type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=WAVE_TRI}, 0, WAVE_NB-1, FLAGS, "type" },
70  { "triangular", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, FLAGS, "type" },
71  { "t", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, FLAGS, "type" },
72  { "sinusoidal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, FLAGS, "type" },
73  { "s", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, FLAGS, "type" },
74  { NULL },
75 };
76 
77 AVFILTER_DEFINE_CLASS(aphaser);
78 
79 static av_cold int init(AVFilterContext *ctx)
80 {
81  AudioPhaserContext *p = ctx->priv;
82 
83  if (p->in_gain > (1 - p->decay * p->decay))
84  av_log(ctx, AV_LOG_WARNING, "in_gain may cause clipping\n");
85  if (p->in_gain / (1 - p->decay) > 1 / p->out_gain)
86  av_log(ctx, AV_LOG_WARNING, "out_gain may cause clipping\n");
87 
88  return 0;
89 }
90 
92 {
95  static const enum AVSampleFormat sample_fmts[] = {
101  };
102 
103  layouts = ff_all_channel_layouts();
104  if (!layouts)
105  return AVERROR(ENOMEM);
106  ff_set_common_channel_layouts(ctx, layouts);
107 
108  formats = ff_make_format_list(sample_fmts);
109  if (!formats)
110  return AVERROR(ENOMEM);
111  ff_set_common_formats(ctx, formats);
112 
113  formats = ff_all_samplerates();
114  if (!formats)
115  return AVERROR(ENOMEM);
116  ff_set_common_samplerates(ctx, formats);
117 
118  return 0;
119 }
120 
121 static void generate_wave_table(enum WaveType wave_type, enum AVSampleFormat sample_fmt,
122  void *table, int table_size,
123  double min, double max, double phase)
124 {
125  uint32_t i, phase_offset = phase / M_PI / 2 * table_size + 0.5;
126 
127  for (i = 0; i < table_size; i++) {
128  uint32_t point = (i + phase_offset) % table_size;
129  double d;
130 
131  switch (wave_type) {
132  case WAVE_SIN:
133  d = (sin((double)point / table_size * 2 * M_PI) + 1) / 2;
134  break;
135  case WAVE_TRI:
136  d = (double)point * 2 / table_size;
137  switch (4 * point / table_size) {
138  case 0: d = d + 0.5; break;
139  case 1:
140  case 2: d = 1.5 - d; break;
141  case 3: d = d - 1.5; break;
142  }
143  break;
144  default:
145  av_assert0(0);
146  }
147 
148  d = d * (max - min) + min;
149  switch (sample_fmt) {
150  case AV_SAMPLE_FMT_FLT: {
151  float *fp = (float *)table;
152  *fp++ = (float)d;
153  table = fp;
154  continue; }
155  case AV_SAMPLE_FMT_DBL: {
156  double *dp = (double *)table;
157  *dp++ = d;
158  table = dp;
159  continue; }
160  }
161 
162  d += d < 0 ? -0.5 : 0.5;
163  switch (sample_fmt) {
164  case AV_SAMPLE_FMT_S16: {
165  int16_t *sp = table;
166  *sp++ = (int16_t)d;
167  table = sp;
168  continue; }
169  case AV_SAMPLE_FMT_S32: {
170  int32_t *ip = table;
171  *ip++ = (int32_t)d;
172  table = ip;
173  continue; }
174  default:
175  av_assert0(0);
176  }
177  }
178 }
179 
180 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
181 
182 #define PHASER_PLANAR(name, type) \
183 static void phaser_## name ##p(AudioPhaserContext *p, \
184  uint8_t * const *src, uint8_t **dst, \
185  int nb_samples, int channels) \
186 { \
187  int i, c, delay_pos, modulation_pos; \
188  \
189  av_assert0(channels > 0); \
190  for (c = 0; c < channels; c++) { \
191  type *s = (type *)src[c]; \
192  type *d = (type *)dst[c]; \
193  double *buffer = p->delay_buffer + \
194  c * p->delay_buffer_length; \
195  \
196  delay_pos = p->delay_pos; \
197  modulation_pos = p->modulation_pos; \
198  \
199  for (i = 0; i < nb_samples; i++, s++, d++) { \
200  double v = *s * p->in_gain + buffer[ \
201  MOD(delay_pos + p->modulation_buffer[ \
202  modulation_pos], \
203  p->delay_buffer_length)] * p->decay; \
204  \
205  modulation_pos = MOD(modulation_pos + 1, \
206  p->modulation_buffer_length); \
207  delay_pos = MOD(delay_pos + 1, p->delay_buffer_length); \
208  buffer[delay_pos] = v; \
209  \
210  *d = v * p->out_gain; \
211  } \
212  } \
213  \
214  p->delay_pos = delay_pos; \
215  p->modulation_pos = modulation_pos; \
216 }
217 
218 #define PHASER(name, type) \
219 static void phaser_## name (AudioPhaserContext *p, \
220  uint8_t * const *src, uint8_t **dst, \
221  int nb_samples, int channels) \
222 { \
223  int i, c, delay_pos, modulation_pos; \
224  type *s = (type *)src[0]; \
225  type *d = (type *)dst[0]; \
226  double *buffer = p->delay_buffer; \
227  \
228  delay_pos = p->delay_pos; \
229  modulation_pos = p->modulation_pos; \
230  \
231  for (i = 0; i < nb_samples; i++) { \
232  int pos = MOD(delay_pos + p->modulation_buffer[modulation_pos], \
233  p->delay_buffer_length) * channels; \
234  int npos; \
235  \
236  delay_pos = MOD(delay_pos + 1, p->delay_buffer_length); \
237  npos = delay_pos * channels; \
238  for (c = 0; c < channels; c++, s++, d++) { \
239  double v = *s * p->in_gain + buffer[pos + c] * p->decay; \
240  \
241  buffer[npos + c] = v; \
242  \
243  *d = v * p->out_gain; \
244  } \
245  \
246  modulation_pos = MOD(modulation_pos + 1, \
247  p->modulation_buffer_length); \
248  } \
249  \
250  p->delay_pos = delay_pos; \
251  p->modulation_pos = modulation_pos; \
252 }
253 
254 PHASER_PLANAR(dbl, double)
255 PHASER_PLANAR(flt, float)
256 PHASER_PLANAR(s16, int16_t)
258 
259 PHASER(dbl, double)
260 PHASER(flt, float)
261 PHASER(s16, int16_t)
262 PHASER(s32, int32_t)
263 
264 static int config_output(AVFilterLink *outlink)
265 {
266  AudioPhaserContext *p = outlink->src->priv;
267  AVFilterLink *inlink = outlink->src->inputs[0];
268 
269  p->delay_buffer_length = p->delay * 0.001 * inlink->sample_rate + 0.5;
270  p->delay_buffer = av_calloc(p->delay_buffer_length, sizeof(*p->delay_buffer) * inlink->channels);
271  p->modulation_buffer_length = inlink->sample_rate / p->speed + 0.5;
273 
274  if (!p->modulation_buffer || !p->delay_buffer)
275  return AVERROR(ENOMEM);
276 
279  1., p->delay_buffer_length, M_PI / 2.0);
280 
281  p->delay_pos = p->modulation_pos = 0;
282 
283  switch (inlink->format) {
284  case AV_SAMPLE_FMT_DBL: p->phaser = phaser_dbl; break;
285  case AV_SAMPLE_FMT_DBLP: p->phaser = phaser_dblp; break;
286  case AV_SAMPLE_FMT_FLT: p->phaser = phaser_flt; break;
287  case AV_SAMPLE_FMT_FLTP: p->phaser = phaser_fltp; break;
288  case AV_SAMPLE_FMT_S16: p->phaser = phaser_s16; break;
289  case AV_SAMPLE_FMT_S16P: p->phaser = phaser_s16p; break;
290  case AV_SAMPLE_FMT_S32: p->phaser = phaser_s32; break;
291  case AV_SAMPLE_FMT_S32P: p->phaser = phaser_s32p; break;
292  default: av_assert0(0);
293  }
294 
295  return 0;
296 }
297 
298 static int filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
299 {
300  AudioPhaserContext *p = inlink->dst->priv;
301  AVFilterLink *outlink = inlink->dst->outputs[0];
302  AVFrame *outbuf;
303 
304  if (av_frame_is_writable(inbuf)) {
305  outbuf = inbuf;
306  } else {
307  outbuf = ff_get_audio_buffer(inlink, inbuf->nb_samples);
308  if (!outbuf)
309  return AVERROR(ENOMEM);
310  av_frame_copy_props(outbuf, inbuf);
311  }
312 
313  p->phaser(p, inbuf->extended_data, outbuf->extended_data,
314  outbuf->nb_samples, av_frame_get_channels(outbuf));
315 
316  if (inbuf != outbuf)
317  av_frame_free(&inbuf);
318 
319  return ff_filter_frame(outlink, outbuf);
320 }
321 
322 static av_cold void uninit(AVFilterContext *ctx)
323 {
324  AudioPhaserContext *p = ctx->priv;
325 
326  av_freep(&p->delay_buffer);
328 }
329 
330 static const AVFilterPad aphaser_inputs[] = {
331  {
332  .name = "default",
333  .type = AVMEDIA_TYPE_AUDIO,
334  .filter_frame = filter_frame,
335  },
336  { NULL }
337 };
338 
339 static const AVFilterPad aphaser_outputs[] = {
340  {
341  .name = "default",
342  .type = AVMEDIA_TYPE_AUDIO,
343  .config_props = config_output,
344  },
345  { NULL }
346 };
347 
349  .name = "aphaser",
350  .description = NULL_IF_CONFIG_SMALL("Add a phasing effect to the audio."),
351  .query_formats = query_formats,
352  .priv_size = sizeof(AudioPhaserContext),
353  .init = init,
354  .uninit = uninit,
355  .inputs = aphaser_inputs,
356  .outputs = aphaser_outputs,
357  .priv_class = &aphaser_class,
358 };
static const AVOption aphaser_options[]
Definition: af_aphaser.c:63
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:424
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
if max(w)>1 w=0.9 *w/max(w)
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
enum WaveType type
Definition: af_aphaser.c:45
signed 16 bits
Definition: samplefmt.h:52
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:308
set threshold d
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:532
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void(* phaser)(struct AudioPhaserContext *p, uint8_t *const *src, uint8_t **dst, int nb_samples, int channels)
Definition: af_aphaser.c:55
uint8_t
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define av_cold
Definition: attributes.h:78
AVOptions.
struct AudioPhaserContext AudioPhaserContext
static const AVFilterPad aphaser_outputs[]
Definition: af_aphaser.c:339
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_aphaser.c:322
#define sp
Definition: regdef.h:63
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:545
signed 32 bits, planar
Definition: samplefmt.h:59
float, planar
Definition: samplefmt.h:60
A filter pad used for either input or output.
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:84
#define PHASER(name, type)
Definition: af_aphaser.c:218
static const struct endianess table[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
#define FLAGS
Definition: af_aphaser.c:61
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:361
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
static int config_output(AVFilterLink *outlink)
Definition: af_aphaser.c:264
signed 32 bits
Definition: samplefmt.h:53
static int filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
Definition: af_aphaser.c:298
int av_frame_get_channels(const AVFrame *frame)
AVFilter avfilter_af_aphaser
Definition: af_aphaser.c:348
int32_t
static av_cold int init(AVFilterContext *ctx)
Definition: af_aphaser.c:79
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (...
Definition: formats.c:402
static void generate_wave_table(enum WaveType wave_type, enum AVSampleFormat sample_fmt, void *table, int table_size, double min, double max, double phase)
Definition: af_aphaser.c:121
A list of supported channel layouts.
Definition: formats.h:85
NULL
Definition: eval.c:55
AVS_Value src
Definition: avisynth_c.h:523
typedef void(RENAME(mix_any_func_type))
AVFILTER_DEFINE_CLASS(aphaser)
#define fp
Definition: regdef.h:44
double * delay_buffer
Definition: af_aphaser.c:48
#define OFFSET(x)
Definition: af_aphaser.c:60
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:436
synthesis window for stochastic i
const char * name
filter name
Definition: avfilter.h:437
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:396
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:213
void ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:533
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common formats
Definition: swscale.txt:33
int modulation_buffer_length
Definition: af_aphaser.c:50
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
static int query_formats(AVFilterContext *ctx)
Definition: af_aphaser.c:91
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
WaveType
Definition: af_aphaser.c:32
A list of supported formats for one end of a filter link.
Definition: formats.h:64
int32_t * modulation_buffer
Definition: af_aphaser.c:51
An instance of a filter.
Definition: avfilter.h:524
signed 16 bits, planar
Definition: samplefmt.h:58
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
static const AVFilterPad aphaser_inputs[]
Definition: af_aphaser.c:330
#define M_PI
Definition: mathematics.h:46
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:526
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
float min
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
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
double, planar
Definition: samplefmt.h:61
#define PHASER_PLANAR(name, type)
Definition: af_aphaser.c:182