FFmpeg
af_apulsator.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
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/avassert.h"
23 #include "libavutil/opt.h"
24 #include "avfilter.h"
25 #include "filters.h"
26 #include "formats.h"
27 #include "audio.h"
28 
31 
32 typedef struct SimpleLFO {
33  double phase;
34  double freq;
35  double offset;
36  double amount;
37  double pwidth;
38  int mode;
39  int srate;
40 } SimpleLFO;
41 
42 typedef struct AudioPulsatorContext {
43  const AVClass *class;
44  int mode;
45  double level_in;
46  double level_out;
47  double amount;
48  double offset_l;
49  double offset_r;
50  double pwidth;
51  double bpm;
52  double hertz;
53  int ms;
54  int timing;
55 
58 
59 #define OFFSET(x) offsetof(AudioPulsatorContext, x)
60 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
61 
62 static const AVOption apulsator_options[] = {
63  { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, FLAGS, },
64  { "level_out", "set output gain", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, FLAGS, },
65  { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=SINE}, SINE, NB_MODES-1, FLAGS, .unit = "mode" },
66  { "sine", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SINE}, 0, 0, FLAGS, .unit = "mode" },
67  { "triangle", NULL, 0, AV_OPT_TYPE_CONST, {.i64=TRIANGLE},0, 0, FLAGS, .unit = "mode" },
68  { "square", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SQUARE}, 0, 0, FLAGS, .unit = "mode" },
69  { "sawup", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SAWUP}, 0, 0, FLAGS, .unit = "mode" },
70  { "sawdown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SAWDOWN}, 0, 0, FLAGS, .unit = "mode" },
71  { "amount", "set modulation", OFFSET(amount), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
72  { "offset_l", "set offset L", OFFSET(offset_l), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
73  { "offset_r", "set offset R", OFFSET(offset_r), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, 0, 1, FLAGS },
74  { "width", "set pulse width", OFFSET(pwidth), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 2, FLAGS },
75  { "timing", "set timing", OFFSET(timing), AV_OPT_TYPE_INT, {.i64=2}, 0, NB_TIMINGS-1, FLAGS, .unit = "timing" },
76  { "bpm", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_BPM}, 0, 0, FLAGS, .unit = "timing" },
77  { "ms", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_MS}, 0, 0, FLAGS, .unit = "timing" },
78  { "hz", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_HZ}, 0, 0, FLAGS, .unit = "timing" },
79  { "bpm", "set BPM", OFFSET(bpm), AV_OPT_TYPE_DOUBLE, {.dbl=120}, 30, 300, FLAGS },
80  { "ms", "set ms", OFFSET(ms), AV_OPT_TYPE_INT, {.i64=500}, 10, 2000, FLAGS },
81  { "hz", "set frequency", OFFSET(hertz), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0.01, 100, FLAGS },
82  { NULL }
83 };
84 
85 AVFILTER_DEFINE_CLASS(apulsator);
86 
87 static void lfo_advance(SimpleLFO *lfo, unsigned count)
88 {
89  lfo->phase = fabs(lfo->phase + count * lfo->freq / lfo->srate);
90  if (lfo->phase >= 1)
91  lfo->phase = fmod(lfo->phase, 1);
92 }
93 
94 static double lfo_get_value(SimpleLFO *lfo)
95 {
96  double phs = FFMIN(100, lfo->phase / FFMIN(1.99, FFMAX(0.01, lfo->pwidth)) + lfo->offset);
97  double val;
98 
99  if (phs > 1)
100  phs = fmod(phs, 1.);
101 
102  switch (lfo->mode) {
103  case SINE:
104  val = sin(phs * 2 * M_PI);
105  break;
106  case TRIANGLE:
107  if (phs > 0.75)
108  val = (phs - 0.75) * 4 - 1;
109  else if (phs > 0.25)
110  val = -4 * phs + 2;
111  else
112  val = phs * 4;
113  break;
114  case SQUARE:
115  val = phs < 0.5 ? -1 : +1;
116  break;
117  case SAWUP:
118  val = phs * 2 - 1;
119  break;
120  case SAWDOWN:
121  val = 1 - phs * 2;
122  break;
123  default: av_assert0(0);
124  }
125 
126  return val * lfo->amount;
127 }
128 
130 {
131  AVFilterContext *ctx = inlink->dst;
132  AVFilterLink *outlink = ctx->outputs[0];
133  AudioPulsatorContext *s = ctx->priv;
134  const double *src = (const double *)in->data[0];
135  const int nb_samples = in->nb_samples;
136  const double level_out = s->level_out;
137  const double level_in = s->level_in;
138  const double amount = s->amount;
139  AVFrame *out;
140  double *dst;
141  int n;
142 
143  if (av_frame_is_writable(in)) {
144  out = in;
145  } else {
147  if (!out) {
148  av_frame_free(&in);
149  return AVERROR(ENOMEM);
150  }
152  }
153  dst = (double *)out->data[0];
154 
155  for (n = 0; n < nb_samples; n++) {
156  double outL;
157  double outR;
158  double inL = src[0] * level_in;
159  double inR = src[1] * level_in;
160  double procL = inL;
161  double procR = inR;
162 
163  procL *= lfo_get_value(&s->lfoL) * 0.5 + amount / 2;
164  procR *= lfo_get_value(&s->lfoR) * 0.5 + amount / 2;
165 
166  outL = procL + inL * (1 - amount);
167  outR = procR + inR * (1 - amount);
168 
169  outL *= level_out;
170  outR *= level_out;
171 
172  dst[0] = outL;
173  dst[1] = outR;
174 
175  lfo_advance(&s->lfoL, 1);
176  lfo_advance(&s->lfoR, 1);
177 
178  dst += 2;
179  src += 2;
180  }
181 
182  if (in != out)
183  av_frame_free(&in);
184 
185  return ff_filter_frame(outlink, out);
186 }
187 
189  AVFilterFormatsConfig **cfg_in,
190  AVFilterFormatsConfig **cfg_out)
191 {
192  static const enum AVSampleFormat formats[] = {
195  };
196  static const AVChannelLayout layouts[] = {
198  { .nb_channels = 0 },
199  };
200 
201  int ret;
202 
203  ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, formats);
204  if (ret < 0)
205  return ret;
206 
208  if (ret < 0)
209  return ret;
210 
211  return 0;
212 }
213 
215 {
216  AVFilterContext *ctx = inlink->dst;
217  AudioPulsatorContext *s = ctx->priv;
218  double freq;
219 
220  switch (s->timing) {
221  case UNIT_BPM: freq = s->bpm / 60; break;
222  case UNIT_MS: freq = 1 / (s->ms / 1000.); break;
223  case UNIT_HZ: freq = s->hertz; break;
224  default: av_assert0(0);
225  }
226 
227  s->lfoL.freq = freq;
228  s->lfoR.freq = freq;
229  s->lfoL.mode = s->mode;
230  s->lfoR.mode = s->mode;
231  s->lfoL.offset = s->offset_l;
232  s->lfoR.offset = s->offset_r;
233  s->lfoL.srate = inlink->sample_rate;
234  s->lfoR.srate = inlink->sample_rate;
235  s->lfoL.amount = s->amount;
236  s->lfoR.amount = s->amount;
237  s->lfoL.pwidth = s->pwidth;
238  s->lfoR.pwidth = s->pwidth;
239 
240  return 0;
241 }
242 
243 static const AVFilterPad inputs[] = {
244  {
245  .name = "default",
246  .type = AVMEDIA_TYPE_AUDIO,
247  .config_props = config_input,
248  .filter_frame = filter_frame,
249  },
250 };
251 
253  .name = "apulsator",
254  .description = NULL_IF_CONFIG_SMALL("Audio pulsator."),
255  .priv_size = sizeof(AudioPulsatorContext),
256  .priv_class = &apulsator_class,
260 };
formats
formats
Definition: signature.h:47
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:98
SQUARE
@ SQUARE
Definition: af_apulsator.c:29
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
out
FILE * out
Definition: movenc.c:55
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:387
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1062
layouts
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:335
AudioPulsatorContext
Definition: af_apulsator.c:42
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:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVOption
AVOption.
Definition: opt.h:429
ff_set_common_channel_layouts_from_list2
int ff_set_common_channel_layouts_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const AVChannelLayout *fmts)
Definition: formats.c:920
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AudioPulsatorContext::timing
int timing
Definition: af_apulsator.c:54
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
SimpleLFO::phase
double phase
Definition: af_apulsator.c:33
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
UNIT_BPM
@ UNIT_BPM
Definition: af_apulsator.c:30
formats.h
lfo_advance
static void lfo_advance(SimpleLFO *lfo, unsigned count)
Definition: af_apulsator.c:87
apulsator_options
static const AVOption apulsator_options[]
Definition: af_apulsator.c:62
val
static double val(void *priv, double ch)
Definition: aeval.c:77
SimpleLFO::srate
int srate
Definition: af_apulsator.c:39
AudioPulsatorContext::bpm
double bpm
Definition: af_apulsator.c:51
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
avassert.h
UNIT_MS
@ UNIT_MS
Definition: af_apulsator.c:30
SimpleLFO::pwidth
double pwidth
Definition: af_apulsator.c:37
s
#define s(width, name)
Definition: cbs_vp9.c:198
NB_TIMINGS
@ NB_TIMINGS
Definition: af_apulsator.c:30
AudioPulsatorContext::level_out
double level_out
Definition: af_apulsator.c:46
PulsatorTimings
PulsatorTimings
Definition: af_apulsator.c:30
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition: opt.h:267
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_apulsator.c:129
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
SimpleLFO
Definition: af_apulsator.c:32
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
AudioPulsatorContext::lfoL
SimpleLFO lfoL
Definition: af_apulsator.c:56
if
if(ret)
Definition: filter_design.txt:179
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
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:713
AudioPulsatorContext::amount
double amount
Definition: af_apulsator.c:47
ff_audio_default_filterpad
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition: audio.c:34
TRIANGLE
@ TRIANGLE
Definition: af_apulsator.c:29
PulsatorModes
PulsatorModes
Definition: af_apulsator.c:29
AVFilterFormatsConfig
Lists of formats / etc.
Definition: avfilter.h:111
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(apulsator)
AudioPulsatorContext::hertz
double hertz
Definition: af_apulsator.c:52
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:94
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:311
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
SAWUP
@ SAWUP
Definition: af_apulsator.c:29
AudioPulsatorContext::lfoR
SimpleLFO lfoR
Definition: af_apulsator.c:56
UNIT_HZ
@ UNIT_HZ
Definition: af_apulsator.c:30
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:649
FLAGS
#define FLAGS
Definition: af_apulsator.c:60
M_PI
#define M_PI
Definition: mathematics.h:67
AudioPulsatorContext::level_in
double level_in
Definition: af_apulsator.c:45
SimpleLFO::freq
double freq
Definition: af_apulsator.c:34
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:469
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
lfo_get_value
static double lfo_get_value(SimpleLFO *lfo)
Definition: af_apulsator.c:94
FILTER_QUERY_FUNC2
#define FILTER_QUERY_FUNC2(func)
Definition: filters.h:239
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
query_formats
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition: af_apulsator.c:188
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
SimpleLFO::mode
int mode
Definition: af_apulsator.c:38
AVFilter
Filter definition.
Definition: avfilter.h:201
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_apulsator.c:214
AudioPulsatorContext::pwidth
double pwidth
Definition: af_apulsator.c:50
ret
ret
Definition: filter_design.txt:187
NB_MODES
@ NB_MODES
Definition: af_apulsator.c:29
SINE
@ SINE
Definition: af_apulsator.c:29
AudioPulsatorContext::ms
int ms
Definition: af_apulsator.c:53
SimpleLFO::offset
double offset
Definition: af_apulsator.c:35
ff_set_common_formats_from_list2
int ff_set_common_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const int *fmts)
Definition: formats.c:1016
channel_layout.h
SAWDOWN
@ SAWDOWN
Definition: af_apulsator.c:29
ff_af_apulsator
const AVFilter ff_af_apulsator
Definition: af_apulsator.c:252
mode
mode
Definition: ebur128.h:83
OFFSET
#define OFFSET(x)
Definition: af_apulsator.c:59
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
AudioPulsatorContext::mode
int mode
Definition: af_apulsator.c:44
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
audio.h
AudioPulsatorContext::offset_r
double offset_r
Definition: af_apulsator.c:49
inputs
static const AVFilterPad inputs[]
Definition: af_apulsator.c:243
SimpleLFO::amount
double amount
Definition: af_apulsator.c:36
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:61
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
AudioPulsatorContext::offset_l
double offset_l
Definition: af_apulsator.c:48
src
#define src
Definition: vp8dsp.c:248