FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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"
22 #include "libavutil/opt.h"
23 #include "avfilter.h"
24 #include "internal.h"
25 #include "audio.h"
26 
29 
30 typedef struct SimpleLFO {
31  double phase;
32  double freq;
33  double offset;
34  double amount;
35  double pwidth;
36  int mode;
37  int srate;
38 } SimpleLFO;
39 
40 typedef struct AudioPulsatorContext {
41  const AVClass *class;
42  int mode;
43  double level_in;
44  double level_out;
45  double amount;
46  double offset_l;
47  double offset_r;
48  double pwidth;
49  double bpm;
50  double hertz;
51  int ms;
52  int timing;
53 
56 
57 #define OFFSET(x) offsetof(AudioPulsatorContext, x)
58 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
59 
60 static const AVOption apulsator_options[] = {
61  { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, FLAGS, },
62  { "level_out", "set output gain", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, FLAGS, },
63  { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=SINE}, SINE, NB_MODES-1, FLAGS, "mode" },
64  { "sine", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SINE}, 0, 0, FLAGS, "mode" },
65  { "triangle", NULL, 0, AV_OPT_TYPE_CONST, {.i64=TRIANGLE},0, 0, FLAGS, "mode" },
66  { "square", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SQUARE}, 0, 0, FLAGS, "mode" },
67  { "sawup", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SAWUP}, 0, 0, FLAGS, "mode" },
68  { "sawdown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SAWDOWN}, 0, 0, FLAGS, "mode" },
69  { "amount", "set modulation", OFFSET(amount), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
70  { "offset_l", "set offset L", OFFSET(offset_l), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
71  { "offset_r", "set offset R", OFFSET(offset_r), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, 0, 1, FLAGS },
72  { "width", "set pulse width", OFFSET(pwidth), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 2, FLAGS },
73  { "timing", "set timing", OFFSET(timing), AV_OPT_TYPE_INT, {.i64=2}, 0, NB_TIMINGS-1, FLAGS, "timing" },
74  { "bpm", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_BPM}, 0, 0, FLAGS, "timing" },
75  { "ms", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_MS}, 0, 0, FLAGS, "timing" },
76  { "hz", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_HZ}, 0, 0, FLAGS, "timing" },
77  { "bpm", "set BPM", OFFSET(bpm), AV_OPT_TYPE_DOUBLE, {.dbl=120}, 30, 300, FLAGS },
78  { "ms", "set ms", OFFSET(ms), AV_OPT_TYPE_INT, {.i64=500}, 10, 2000, FLAGS },
79  { "hz", "set frequency", OFFSET(hertz), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0.01, 100, FLAGS },
80  { NULL }
81 };
82 
83 AVFILTER_DEFINE_CLASS(apulsator);
84 
85 static void lfo_advance(SimpleLFO *lfo, unsigned count)
86 {
87  lfo->phase = fabs(lfo->phase + count * lfo->freq / lfo->srate);
88  if (lfo->phase >= 1)
89  lfo->phase = fmod(lfo->phase, 1);
90 }
91 
92 static double lfo_get_value(SimpleLFO *lfo)
93 {
94  double phs = FFMIN(100, lfo->phase / FFMIN(1.99, FFMAX(0.01, lfo->pwidth)) + lfo->offset);
95  double val;
96 
97  if (phs > 1)
98  phs = fmod(phs, 1.);
99 
100  switch (lfo->mode) {
101  case SINE:
102  val = sin(phs * 2 * M_PI);
103  break;
104  case TRIANGLE:
105  if (phs > 0.75)
106  val = (phs - 0.75) * 4 - 1;
107  else if (phs > 0.25)
108  val = -4 * phs + 2;
109  else
110  val = phs * 4;
111  break;
112  case SQUARE:
113  val = phs < 0.5 ? -1 : +1;
114  break;
115  case SAWUP:
116  val = phs * 2 - 1;
117  break;
118  case SAWDOWN:
119  val = 1 - phs * 2;
120  break;
121  default: av_assert0(0);
122  }
123 
124  return val * lfo->amount;
125 }
126 
127 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
128 {
129  AVFilterContext *ctx = inlink->dst;
130  AVFilterLink *outlink = ctx->outputs[0];
131  AudioPulsatorContext *s = ctx->priv;
132  const double *src = (const double *)in->data[0];
133  const int nb_samples = in->nb_samples;
134  const double level_out = s->level_out;
135  const double level_in = s->level_in;
136  const double amount = s->amount;
137  AVFrame *out;
138  double *dst;
139  int n;
140 
141  if (av_frame_is_writable(in)) {
142  out = in;
143  } else {
144  out = ff_get_audio_buffer(inlink, in->nb_samples);
145  if (!out) {
146  av_frame_free(&in);
147  return AVERROR(ENOMEM);
148  }
150  }
151  dst = (double *)out->data[0];
152 
153  for (n = 0; n < nb_samples; n++) {
154  double outL;
155  double outR;
156  double inL = src[0] * level_in;
157  double inR = src[1] * level_in;
158  double procL = inL;
159  double procR = inR;
160 
161  procL *= lfo_get_value(&s->lfoL) * 0.5 + amount / 2;
162  procR *= lfo_get_value(&s->lfoR) * 0.5 + amount / 2;
163 
164  outL = procL + inL * (1 - amount);
165  outR = procR + inR * (1 - amount);
166 
167  outL *= level_out;
168  outR *= level_out;
169 
170  dst[0] = outL;
171  dst[1] = outR;
172 
173  lfo_advance(&s->lfoL, 1);
174  lfo_advance(&s->lfoR, 1);
175 
176  dst += 2;
177  src += 2;
178  }
179 
180  if (in != out)
181  av_frame_free(&in);
182 
183  return ff_filter_frame(outlink, out);
184 }
185 
187 {
190  int ret;
191 
192  if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_DBL )) < 0 ||
193  (ret = ff_set_common_formats (ctx , formats )) < 0 ||
194  (ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO)) < 0 ||
195  (ret = ff_set_common_channel_layouts (ctx , layout )) < 0)
196  return ret;
197 
198  formats = ff_all_samplerates();
199  return ff_set_common_samplerates(ctx, formats);
200 }
201 
202 static int config_input(AVFilterLink *inlink)
203 {
204  AVFilterContext *ctx = inlink->dst;
205  AudioPulsatorContext *s = ctx->priv;
206  double freq;
207 
208  switch (s->timing) {
209  case UNIT_BPM: freq = s->bpm / 60; break;
210  case UNIT_MS: freq = 1 / (s->ms / 1000.); break;
211  case UNIT_HZ: freq = s->hertz; break;
212  default: av_assert0(0);
213  }
214 
215  s->lfoL.freq = freq;
216  s->lfoR.freq = freq;
217  s->lfoL.mode = s->mode;
218  s->lfoR.mode = s->mode;
219  s->lfoL.offset = s->offset_l;
220  s->lfoR.offset = s->offset_r;
221  s->lfoL.srate = inlink->sample_rate;
222  s->lfoR.srate = inlink->sample_rate;
223  s->lfoL.amount = s->amount;
224  s->lfoR.amount = s->amount;
225  s->lfoL.pwidth = s->pwidth;
226  s->lfoR.pwidth = s->pwidth;
227 
228  return 0;
229 }
230 
231 static const AVFilterPad inputs[] = {
232  {
233  .name = "default",
234  .type = AVMEDIA_TYPE_AUDIO,
235  .config_props = config_input,
236  .filter_frame = filter_frame,
237  },
238  { NULL }
239 };
240 
241 static const AVFilterPad outputs[] = {
242  {
243  .name = "default",
244  .type = AVMEDIA_TYPE_AUDIO,
245  },
246  { NULL }
247 };
248 
250  .name = "apulsator",
251  .description = NULL_IF_CONFIG_SMALL("Audio pulsator."),
252  .priv_size = sizeof(AudioPulsatorContext),
253  .priv_class = &apulsator_class,
255  .inputs = inputs,
256  .outputs = outputs,
257 };
#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 const char void * val
Definition: avisynth_c.h:771
const char * s
Definition: avisynth_c.h:768
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
AVOption.
Definition: opt.h:246
Main libavfilter public API header.
#define AV_CH_LAYOUT_STEREO
#define src
Definition: vp8dsp.c:254
static const AVOption apulsator_options[]
Definition: af_apulsator.c:60
PulsatorTimings
Definition: af_apulsator.c:28
const char * name
Pad name.
Definition: internal.h:60
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1125
static int query_formats(AVFilterContext *ctx)
Definition: af_apulsator.c:186
AVOptions.
double offset
Definition: af_apulsator.c:33
static void lfo_advance(SimpleLFO *lfo, unsigned count)
Definition: af_apulsator.c:85
A filter pad used for either input or output.
Definition: internal.h:54
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 ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:343
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:86
#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:163
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
void * priv
private data for use by the filter
Definition: avfilter.h:338
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_apulsator.c:127
simple assert() macros that are a bit more flexible than ISO C assert().
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:337
GLsizei count
Definition: opengl_enc.c:109
#define FFMAX(a, b)
Definition: common.h:94
PulsatorModes
Definition: af_apulsator.c:27
AVFILTER_DEFINE_CLASS(apulsator)
double freq
Definition: af_apulsator.c:32
#define FFMIN(a, b)
Definition: common.h:96
AVFormatContext * ctx
Definition: movenc.c:48
double amount
Definition: af_apulsator.c:34
int n
Definition: avisynth_c.h:684
#define FLAGS
Definition: af_apulsator.c:58
A list of supported channel layouts.
Definition: formats.h:85
static int config_input(AVFilterLink *inlink)
Definition: af_apulsator.c:202
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:536
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
AVFilter ff_af_apulsator
Definition: af_apulsator.c:249
double phase
Definition: af_apulsator.c:31
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:335
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:201
#define OFFSET(x)
Definition: af_apulsator.c:57
static const AVFilterPad outputs[]
Definition: af_apulsator.c:241
if(ret< 0)
Definition: vf_mcdeint.c:282
static double lfo_get_value(SimpleLFO *lfo)
Definition: af_apulsator.c:92
double pwidth
Definition: af_apulsator.c:35
A list of supported formats for one end of a filter link.
Definition: formats.h:64
uint64_t layout
An instance of a filter.
Definition: avfilter.h:323
FILE * out
Definition: movenc.c:54
#define M_PI
Definition: mathematics.h:52
formats
Definition: signature.h:48
internal API functions
static const AVFilterPad inputs[]
Definition: af_apulsator.c:231
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:244
for(j=16;j >0;--j)
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:596