FFmpeg
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 #include "generate_wave_table.h"
32 
33 typedef struct AudioPhaserContext {
34  const AVClass *class;
35  double in_gain, out_gain;
36  double delay;
37  double decay;
38  double speed;
39 
40  int type;
41 
43  double *delay_buffer;
44 
47 
49 
50  void (*phaser)(struct AudioPhaserContext *s,
51  uint8_t * const *src, uint8_t **dst,
52  int nb_samples, int channels);
54 
55 #define OFFSET(x) offsetof(AudioPhaserContext, x)
56 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
57 
58 static const AVOption aphaser_options[] = {
59  { "in_gain", "set input gain", OFFSET(in_gain), AV_OPT_TYPE_DOUBLE, {.dbl=.4}, 0, 1, FLAGS },
60  { "out_gain", "set output gain", OFFSET(out_gain), AV_OPT_TYPE_DOUBLE, {.dbl=.74}, 0, 1e9, FLAGS },
61  { "delay", "set delay in milliseconds", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=3.}, 0, 5, FLAGS },
62  { "decay", "set decay", OFFSET(decay), AV_OPT_TYPE_DOUBLE, {.dbl=.4}, 0, .99, FLAGS },
63  { "speed", "set modulation speed", OFFSET(speed), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, .1, 2, FLAGS },
64  { "type", "set modulation type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=WAVE_TRI}, 0, WAVE_NB-1, FLAGS, .unit = "type" },
65  { "triangular", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, FLAGS, .unit = "type" },
66  { "t", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, FLAGS, .unit = "type" },
67  { "sinusoidal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, FLAGS, .unit = "type" },
68  { "s", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, FLAGS, .unit = "type" },
69  { NULL }
70 };
71 
72 AVFILTER_DEFINE_CLASS(aphaser);
73 
75 {
76  AudioPhaserContext *s = ctx->priv;
77 
78  if (s->in_gain > (1 - s->decay * s->decay))
79  av_log(ctx, AV_LOG_WARNING, "in_gain may cause clipping\n");
80  if (s->in_gain / (1 - s->decay) > 1 / s->out_gain)
81  av_log(ctx, AV_LOG_WARNING, "out_gain may cause clipping\n");
82 
83  return 0;
84 }
85 
86 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
87 
88 #define PHASER_PLANAR(name, type) \
89 static void phaser_## name ##p(AudioPhaserContext *s, \
90  uint8_t * const *ssrc, uint8_t **ddst, \
91  int nb_samples, int channels) \
92 { \
93  int i, c, delay_pos, modulation_pos; \
94  \
95  av_assert0(channels > 0); \
96  for (c = 0; c < channels; c++) { \
97  type *src = (type *)ssrc[c]; \
98  type *dst = (type *)ddst[c]; \
99  double *buffer = s->delay_buffer + \
100  c * s->delay_buffer_length; \
101  \
102  delay_pos = s->delay_pos; \
103  modulation_pos = s->modulation_pos; \
104  \
105  for (i = 0; i < nb_samples; i++, src++, dst++) { \
106  double v = *src * s->in_gain + buffer[ \
107  MOD(delay_pos + s->modulation_buffer[ \
108  modulation_pos], \
109  s->delay_buffer_length)] * s->decay; \
110  \
111  modulation_pos = MOD(modulation_pos + 1, \
112  s->modulation_buffer_length); \
113  delay_pos = MOD(delay_pos + 1, s->delay_buffer_length); \
114  buffer[delay_pos] = v; \
115  \
116  *dst = v * s->out_gain; \
117  } \
118  } \
119  \
120  s->delay_pos = delay_pos; \
121  s->modulation_pos = modulation_pos; \
122 }
123 
124 #define PHASER(name, type) \
125 static void phaser_## name (AudioPhaserContext *s, \
126  uint8_t * const *ssrc, uint8_t **ddst, \
127  int nb_samples, int channels) \
128 { \
129  int i, c, delay_pos, modulation_pos; \
130  type *src = (type *)ssrc[0]; \
131  type *dst = (type *)ddst[0]; \
132  double *buffer = s->delay_buffer; \
133  \
134  delay_pos = s->delay_pos; \
135  modulation_pos = s->modulation_pos; \
136  \
137  for (i = 0; i < nb_samples; i++) { \
138  int pos = MOD(delay_pos + s->modulation_buffer[modulation_pos], \
139  s->delay_buffer_length) * channels; \
140  int npos; \
141  \
142  delay_pos = MOD(delay_pos + 1, s->delay_buffer_length); \
143  npos = delay_pos * channels; \
144  for (c = 0; c < channels; c++, src++, dst++) { \
145  double v = *src * s->in_gain + buffer[pos + c] * s->decay; \
146  \
147  buffer[npos + c] = v; \
148  \
149  *dst = v * s->out_gain; \
150  } \
151  \
152  modulation_pos = MOD(modulation_pos + 1, \
153  s->modulation_buffer_length); \
154  } \
155  \
156  s->delay_pos = delay_pos; \
157  s->modulation_pos = modulation_pos; \
158 }
159 
160 PHASER_PLANAR(dbl, double)
161 PHASER_PLANAR(flt, float)
162 PHASER_PLANAR(s16, int16_t)
164 
165 PHASER(dbl, double)
166 PHASER(flt, float)
167 PHASER(s16, int16_t)
168 PHASER(s32, int32_t)
169 
170 static int config_output(AVFilterLink *outlink)
171 {
172  AudioPhaserContext *s = outlink->src->priv;
173  AVFilterLink *inlink = outlink->src->inputs[0];
174 
175  s->delay_buffer_length = s->delay * 0.001 * inlink->sample_rate + 0.5;
176  if (s->delay_buffer_length <= 0) {
177  av_log(outlink->src, AV_LOG_ERROR, "delay is too small\n");
178  return AVERROR(EINVAL);
179  }
180  s->delay_buffer = av_calloc(s->delay_buffer_length, sizeof(*s->delay_buffer) * inlink->ch_layout.nb_channels);
181  s->modulation_buffer_length = inlink->sample_rate / s->speed + 0.5;
182  s->modulation_buffer = av_malloc_array(s->modulation_buffer_length, sizeof(*s->modulation_buffer));
183 
184  if (!s->modulation_buffer || !s->delay_buffer)
185  return AVERROR(ENOMEM);
186 
188  s->modulation_buffer, s->modulation_buffer_length,
189  1., s->delay_buffer_length, M_PI / 2.0);
190 
191  s->delay_pos = s->modulation_pos = 0;
192 
193  switch (inlink->format) {
194  case AV_SAMPLE_FMT_DBL: s->phaser = phaser_dbl; break;
195  case AV_SAMPLE_FMT_DBLP: s->phaser = phaser_dblp; break;
196  case AV_SAMPLE_FMT_FLT: s->phaser = phaser_flt; break;
197  case AV_SAMPLE_FMT_FLTP: s->phaser = phaser_fltp; break;
198  case AV_SAMPLE_FMT_S16: s->phaser = phaser_s16; break;
199  case AV_SAMPLE_FMT_S16P: s->phaser = phaser_s16p; break;
200  case AV_SAMPLE_FMT_S32: s->phaser = phaser_s32; break;
201  case AV_SAMPLE_FMT_S32P: s->phaser = phaser_s32p; break;
202  default: av_assert0(0);
203  }
204 
205  return 0;
206 }
207 
209 {
210  AudioPhaserContext *s = inlink->dst->priv;
211  AVFilterLink *outlink = inlink->dst->outputs[0];
212  AVFrame *outbuf;
213 
214  if (av_frame_is_writable(inbuf)) {
215  outbuf = inbuf;
216  } else {
217  outbuf = ff_get_audio_buffer(outlink, inbuf->nb_samples);
218  if (!outbuf) {
219  av_frame_free(&inbuf);
220  return AVERROR(ENOMEM);
221  }
222  av_frame_copy_props(outbuf, inbuf);
223  }
224 
225  s->phaser(s, inbuf->extended_data, outbuf->extended_data,
226  outbuf->nb_samples, outbuf->ch_layout.nb_channels);
227 
228  if (inbuf != outbuf)
229  av_frame_free(&inbuf);
230 
231  return ff_filter_frame(outlink, outbuf);
232 }
233 
235 {
236  AudioPhaserContext *s = ctx->priv;
237 
238  av_freep(&s->delay_buffer);
239  av_freep(&s->modulation_buffer);
240 }
241 
242 static const AVFilterPad aphaser_inputs[] = {
243  {
244  .name = "default",
245  .type = AVMEDIA_TYPE_AUDIO,
246  .filter_frame = filter_frame,
247  },
248 };
249 
250 static const AVFilterPad aphaser_outputs[] = {
251  {
252  .name = "default",
253  .type = AVMEDIA_TYPE_AUDIO,
254  .config_props = config_output,
255  },
256 };
257 
259  .name = "aphaser",
260  .description = NULL_IF_CONFIG_SMALL("Add a phasing effect to the audio."),
261  .priv_size = sizeof(AudioPhaserContext),
262  .init = init,
263  .uninit = uninit,
270  .priv_class = &aphaser_class,
271 };
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:97
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
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
AudioPhaserContext::phaser
void(* phaser)(struct AudioPhaserContext *s, uint8_t *const *src, uint8_t **dst, int nb_samples, int channels)
Definition: af_aphaser.c:50
AudioPhaserContext::decay
double decay
Definition: af_aphaser.c:37
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
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:130
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
AVOption
AVOption.
Definition: opt.h:346
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:65
aphaser_options
static const AVOption aphaser_options[]
Definition: af_aphaser.c:58
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
AudioPhaserContext
Definition: af_aphaser.c:33
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
Definition: af_aphaser.c:208
WAVE_TRI
@ WAVE_TRI
Definition: generate_wave_table.h:26
AudioPhaserContext::delay_buffer_length
int delay_buffer_length
Definition: af_aphaser.c:42
AVFrame::ch_layout
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition: frame.h:745
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
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:237
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
ctx
AVFormatContext * ctx
Definition: movenc.c:48
channels
channels
Definition: aptx.h:31
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
WAVE_SIN
@ WAVE_SIN
Definition: generate_wave_table.h:25
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:679
WAVE_NB
@ WAVE_NB
Definition: generate_wave_table.h:27
PHASER_PLANAR
#define PHASER_PLANAR(name, type)
Definition: af_aphaser.c:88
AudioPhaserContext::delay
double delay
Definition: af_aphaser.c:36
ff_generate_wave_table
void ff_generate_wave_table(enum WaveType wave_type, enum AVSampleFormat sample_fmt, void *table, int table_size, double min, double max, double phase)
Definition: generate_wave_table.c:24
AudioPhaserContext::delay_pos
int delay_pos
Definition: af_aphaser.c:48
FLAGS
#define FLAGS
Definition: af_aphaser.c:56
AudioPhaserContext::modulation_pos
int modulation_pos
Definition: af_aphaser.c:48
aphaser_outputs
static const AVFilterPad aphaser_outputs[]
Definition: af_aphaser.c:250
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:106
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_aphaser.c:234
ff_af_aphaser
const AVFilter ff_af_aphaser
Definition: af_aphaser.c:258
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:615
AudioPhaserContext::type
int type
Definition: af_aphaser.c:40
AudioPhaserContext::out_gain
double out_gain
Definition: af_aphaser.c:35
M_PI
#define M_PI
Definition: mathematics.h:67
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
internal.h
PHASER
#define PHASER(name, type)
Definition: af_aphaser.c:124
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:424
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:405
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
AudioPhaserContext::delay_buffer
double * delay_buffer
Definition: af_aphaser.c:43
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(aphaser)
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
AVFilter
Filter definition.
Definition: avfilter.h:166
AudioPhaserContext::modulation_buffer
int32_t * modulation_buffer
Definition: af_aphaser.c:46
aphaser_inputs
static const AVFilterPad aphaser_inputs[]
Definition: af_aphaser.c:242
generate_wave_table.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:235
avfilter.h
AudioPhaserContext::speed
double speed
Definition: af_aphaser.c:38
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
AudioPhaserContext::modulation_buffer_length
int modulation_buffer_length
Definition: af_aphaser.c:45
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
audio.h
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_aphaser.c:74
int32_t
int32_t
Definition: audioconvert.c:56
AudioPhaserContext::in_gain
double in_gain
Definition: af_aphaser.c:35
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:61
OFFSET
#define OFFSET(x)
Definition: af_aphaser.c:55
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:59
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:244
config_output
static int config_output(AVFilterLink *outlink)
Definition: af_aphaser.c:170
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60
FILTER_SAMPLEFMTS
#define FILTER_SAMPLEFMTS(...)
Definition: internal.h:170