FFmpeg
af_vibrato.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Kyle Swanson <k@ylo.ph>.
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/opt.h"
22 #include "avfilter.h"
23 #include "internal.h"
24 #include "audio.h"
25 #include "generate_wave_table.h"
26 
27 typedef struct VibratoContext {
28  const AVClass *class;
29  double freq;
30  double depth;
31  int channels;
32 
33  double **buf;
34  int buf_index;
35  int buf_size;
36 
37  double *wave_table;
41 
42 #define OFFSET(x) offsetof(VibratoContext, x)
43 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
44 
45 static const AVOption vibrato_options[] = {
46  { "f", "set frequency in hertz", OFFSET(freq), AV_OPT_TYPE_DOUBLE, {.dbl = 5.0}, 0.1, 20000.0, FLAGS },
47  { "d", "set depth as percentage", OFFSET(depth), AV_OPT_TYPE_DOUBLE, {.dbl = 0.5}, 0.00, 1.0, FLAGS },
48  { NULL }
49 };
50 
51 AVFILTER_DEFINE_CLASS(vibrato);
52 
54 {
55  AVFilterContext *ctx = inlink->dst;
56  VibratoContext *s = ctx->priv;
57  AVFilterLink *outlink = ctx->outputs[0];
58  AVFrame *out;
59  int n, c;
60  const double *src;
61  double *dst;
62 
63  if (av_frame_is_writable(in)) {
64  out = in;
65  } else {
66  out = ff_get_audio_buffer(outlink, in->nb_samples);
67  if (!out) {
68  av_frame_free(&in);
69  return AVERROR(ENOMEM);
70  }
72  }
73 
74 
75  for (n = 0; n < in->nb_samples; n++) {
76  double integer, decimal;
77  decimal = modf(s->depth * s->wave_table[s->wave_table_index], &integer);
78 
79  s->wave_table_index++;
80  if (s->wave_table_index >= s->wave_table_size)
81  s->wave_table_index -= s->wave_table_size;
82 
83  for (c = 0; c < inlink->channels; c++) {
84  int samp1_index, samp2_index;
85  double *buf;
86  double this_samp;
87 
88  src = (const double *)in->extended_data[c];
89  dst = (double *)out->extended_data[c];
90  buf = s->buf[c];
91 
92  samp1_index = s->buf_index + integer;
93  if (samp1_index >= s->buf_size)
94  samp1_index -= s->buf_size;
95  samp2_index = samp1_index + 1;
96  if (samp2_index >= s->buf_size)
97  samp2_index -= s->buf_size;
98 
99  this_samp = src[n];
100  dst[n] = buf[samp1_index] + (decimal * (buf[samp2_index] - buf[samp1_index]));
101  buf[s->buf_index] = this_samp;
102  }
103  s->buf_index++;
104  if (s->buf_index >= s->buf_size)
105  s->buf_index -= s->buf_size;
106  }
107 
108  if (in != out)
109  av_frame_free(&in);
110 
111  return ff_filter_frame(outlink, out);
112 }
113 
115 {
118  static const enum AVSampleFormat sample_fmts[] = {
121  };
122  int ret;
123 
125  if (!layouts)
126  return AVERROR(ENOMEM);
128  if (ret < 0)
129  return ret;
130 
132  if (!formats)
133  return AVERROR(ENOMEM);
135  if (ret < 0)
136  return ret;
137 
139  if (!formats)
140  return AVERROR(ENOMEM);
142 }
143 
145 {
146  VibratoContext *s = ctx->priv;
147  int c;
148 
149  av_freep(&s->wave_table);
150  for (c = 0; c < s->channels; c++)
151  av_freep(&s->buf[c]);
152  av_freep(&s->buf);
153 }
154 
156 {
157  int c;
158  AVFilterContext *ctx = inlink->dst;
159  VibratoContext *s = ctx->priv;
160  s->channels = inlink->channels;
161 
162  s->buf = av_calloc(inlink->channels, sizeof(*s->buf));
163  if (!s->buf)
164  return AVERROR(ENOMEM);
165  s->buf_size = lrint(inlink->sample_rate * 0.005 + 0.5);
166  for (c = 0; c < s->channels; c++) {
167  s->buf[c] = av_malloc_array(s->buf_size, sizeof(*s->buf[c]));
168  if (!s->buf[c])
169  return AVERROR(ENOMEM);
170  }
171  s->buf_index = 0;
172 
173  s->wave_table_size = lrint(inlink->sample_rate / s->freq + 0.5);
174  s->wave_table = av_malloc_array(s->wave_table_size, sizeof(*s->wave_table));
175  if (!s->wave_table)
176  return AVERROR(ENOMEM);
177  ff_generate_wave_table(WAVE_SIN, AV_SAMPLE_FMT_DBL, s->wave_table, s->wave_table_size, 0.0, s->buf_size - 1, 3.0 * M_PI_2);
178  s->wave_table_index = 0;
179 
180  return 0;
181 }
182 
184  {
185  .name = "default",
186  .type = AVMEDIA_TYPE_AUDIO,
187  .config_props = config_input,
188  .filter_frame = filter_frame,
189  },
190  { NULL }
191 };
192 
194  {
195  .name = "default",
196  .type = AVMEDIA_TYPE_AUDIO,
197  },
198  { NULL }
199 };
200 
202  .name = "vibrato",
203  .description = NULL_IF_CONFIG_SMALL("Apply vibrato effect."),
204  .priv_size = sizeof(VibratoContext),
205  .priv_class = &vibrato_class,
206  .uninit = uninit,
210 };
formats
formats
Definition: signature.h:48
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:86
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
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
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:978
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
layouts
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
VibratoContext::wave_table
double * wave_table
Definition: af_vibrato.c:37
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:112
ff_all_channel_counts
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition.
Definition: formats.c:429
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
M_PI_2
#define M_PI_2
Definition: mathematics.h:55
VibratoContext::freq
double freq
Definition: af_vibrato.c:29
AVOption
AVOption.
Definition: opt.h:247
integer
int integer
Definition: swresample_internal.h:37
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:149
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
VibratoContext
Definition: af_vibrato.c:27
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_vibrato.c:53
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_vibrato.c:144
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
lrint
#define lrint
Definition: tablegen.h:53
av_cold
#define av_cold
Definition: attributes.h:90
inputs
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
Definition: filter_design.txt:243
ff_set_common_formats
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:580
avfilter_af_vibrato_inputs
static const AVFilterPad avfilter_af_vibrato_inputs[]
Definition: af_vibrato.c:183
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:226
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
ctx
AVFormatContext * ctx
Definition: movenc.c:48
WAVE_SIN
@ WAVE_SIN
Definition: generate_wave_table.h:25
ff_af_vibrato
const AVFilter ff_af_vibrato
Definition: af_vibrato.c:201
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
VibratoContext::buf
double ** buf
Definition: af_vibrato.c:33
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:537
VibratoContext::wave_table_size
int wave_table_size
Definition: af_vibrato.c:39
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_vibrato.c:155
src
#define src
Definition: vp8dsp.c:255
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(vibrato)
FLAGS
#define FLAGS
Definition: af_vibrato.c:43
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
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
VibratoContext::buf_index
int buf_index
Definition: af_vibrato.c:34
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:116
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:473
vibrato_options
static const AVOption vibrato_options[]
Definition: af_vibrato.c:45
avfilter_af_vibrato_outputs
static const AVFilterPad avfilter_af_vibrato_outputs[]
Definition: af_vibrato.c:193
VibratoContext::depth
double depth
Definition: af_vibrato.c:30
internal.h
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:369
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:350
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AVFilter
Filter definition.
Definition: avfilter.h:145
ret
ret
Definition: filter_design.txt:187
VibratoContext::buf_size
int buf_size
Definition: af_vibrato.c:35
ff_all_samplerates
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:414
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:251
generate_wave_table.h
avfilter.h
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:70
AVFilterContext
An instance of a filter.
Definition: avfilter.h:333
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_vibrato.c:114
audio.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
VibratoContext::wave_table_index
int wave_table_index
Definition: af_vibrato.c:38
VibratoContext::channels
int channels
Definition: af_vibrato.c:31
ff_set_common_samplerates
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:568
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:64
OFFSET
#define OFFSET(x)
Definition: af_vibrato.c:42
ff_set_common_channel_layouts
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates.
Definition: formats.c:561