asrc_sine.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Nicolas George
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 License
8  * 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
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <float.h>
22 
23 #include "libavutil/avassert.h"
25 #include "libavutil/opt.h"
26 #include "audio.h"
27 #include "avfilter.h"
28 #include "internal.h"
29 
30 typedef struct {
31  const AVClass *class;
32  double frequency;
33  double beep_factor;
36  int64_t duration;
37  int16_t *sin;
38  int64_t pts;
39  uint32_t phi; ///< current phase of the sine (2pi = 1<<32)
40  uint32_t dphi; ///< phase increment between two samples
41  unsigned beep_period;
42  unsigned beep_index;
43  unsigned beep_length;
44  uint32_t phi_beep; ///< current phase of the beep
45  uint32_t dphi_beep; ///< phase increment of the beep
46 } SineContext;
47 
48 #define CONTEXT SineContext
49 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50 
51 #define OPT_GENERIC(name, field, def, min, max, descr, type, deffield, ...) \
52  { name, descr, offsetof(CONTEXT, field), AV_OPT_TYPE_ ## type, \
53  { .deffield = def }, min, max, FLAGS, __VA_ARGS__ }
54 
55 #define OPT_INT(name, field, def, min, max, descr, ...) \
56  OPT_GENERIC(name, field, def, min, max, descr, INT, i64, __VA_ARGS__)
57 
58 #define OPT_DBL(name, field, def, min, max, descr, ...) \
59  OPT_GENERIC(name, field, def, min, max, descr, DOUBLE, dbl, __VA_ARGS__)
60 
61 #define OPT_DUR(name, field, def, min, max, descr, ...) \
62  OPT_GENERIC(name, field, def, min, max, descr, DURATION, str, __VA_ARGS__)
63 
64 static const AVOption sine_options[] = {
65  OPT_DBL("frequency", frequency, 440, 0, DBL_MAX, "set the sine frequency"),
66  OPT_DBL("f", frequency, 440, 0, DBL_MAX, "set the sine frequency"),
67  OPT_DBL("beep_factor", beep_factor, 0, 0, DBL_MAX, "set the beep fequency factor"),
68  OPT_DBL("b", beep_factor, 0, 0, DBL_MAX, "set the beep fequency factor"),
69  OPT_INT("sample_rate", sample_rate, 44100, 1, INT_MAX, "set the sample rate"),
70  OPT_INT("r", sample_rate, 44100, 1, INT_MAX, "set the sample rate"),
71  OPT_DUR("duration", duration, 0, 0, INT64_MAX, "set the audio duration"),
72  OPT_DUR("d", duration, 0, 0, INT64_MAX, "set the audio duration"),
73  OPT_INT("samples_per_frame", samples_per_frame, 1024, 0, INT_MAX, "set the number of samples per frame"),
74  {NULL},
75 };
76 
78 
79 #define LOG_PERIOD 15
80 #define AMPLITUDE 4095
81 #define AMPLITUDE_SHIFT 3
82 
83 static void make_sin_table(int16_t *sin)
84 {
85  unsigned half_pi = 1 << (LOG_PERIOD - 2);
86  unsigned ampls = AMPLITUDE << AMPLITUDE_SHIFT;
87  uint64_t unit2 = (uint64_t)(ampls * ampls) << 32;
88  unsigned step, i, c, s, k, new_k, n2;
89 
90  /* Principle: if u = exp(i*a1) and v = exp(i*a2), then
91  exp(i*(a1+a2)/2) = (u+v) / length(u+v) */
92  sin[0] = 0;
93  sin[half_pi] = ampls;
94  for (step = half_pi; step > 1; step /= 2) {
95  /* k = (1 << 16) * amplitude / length(u+v)
96  In exact values, k is constant at a given step */
97  k = 0x10000;
98  for (i = 0; i < half_pi / 2; i += step) {
99  s = sin[i] + sin[i + step];
100  c = sin[half_pi - i] + sin[half_pi - i - step];
101  n2 = s * s + c * c;
102  /* Newton's method to solve n² * k² = unit² */
103  while (1) {
104  new_k = (k + unit2 / ((uint64_t)k * n2) + 1) >> 1;
105  if (k == new_k)
106  break;
107  k = new_k;
108  }
109  sin[i + step / 2] = (k * s + 0x7FFF) >> 16;
110  sin[half_pi - i - step / 2] = (k * c + 0x8000) >> 16;
111  }
112  }
113  /* Unshift amplitude */
114  for (i = 0; i <= half_pi; i++)
115  sin[i] = (sin[i] + (1 << (AMPLITUDE_SHIFT - 1))) >> AMPLITUDE_SHIFT;
116  /* Use symmetries to fill the other three quarters */
117  for (i = 0; i < half_pi; i++)
118  sin[half_pi * 2 - i] = sin[i];
119  for (i = 0; i < 2 * half_pi; i++)
120  sin[i + 2 * half_pi] = -sin[i];
121 }
122 
123 static av_cold int init(AVFilterContext *ctx)
124 {
125  SineContext *sine = ctx->priv;
126 
127  if (!(sine->sin = av_malloc(sizeof(*sine->sin) << LOG_PERIOD)))
128  return AVERROR(ENOMEM);
129  sine->dphi = ldexp(sine->frequency, 32) / sine->sample_rate + 0.5;
130  make_sin_table(sine->sin);
131 
132  if (sine->beep_factor) {
133  sine->beep_period = sine->sample_rate;
134  sine->beep_length = sine->beep_period / 25;
135  sine->dphi_beep = ldexp(sine->beep_factor * sine->frequency, 32) /
136  sine->sample_rate + 0.5;
137  }
138 
139  return 0;
140 }
141 
142 static av_cold void uninit(AVFilterContext *ctx)
143 {
144  SineContext *sine = ctx->priv;
145 
146  av_freep(&sine->sin);
147 }
148 
150 {
151  SineContext *sine = ctx->priv;
152  static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 };
153  int sample_rates[] = { sine->sample_rate, -1 };
154  static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16,
156 
157  ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
160  return 0;
161 }
162 
163 static av_cold int config_props(AVFilterLink *outlink)
164 {
165  SineContext *sine = outlink->src->priv;
166  sine->duration = av_rescale(sine->duration, sine->sample_rate, AV_TIME_BASE);
167  return 0;
168 }
169 
170 static int request_frame(AVFilterLink *outlink)
171 {
172  SineContext *sine = outlink->src->priv;
173  AVFrame *frame;
174  int i, nb_samples = sine->samples_per_frame;
175  int16_t *samples;
176 
177  if (sine->duration) {
178  nb_samples = FFMIN(nb_samples, sine->duration - sine->pts);
179  av_assert1(nb_samples >= 0);
180  if (!nb_samples)
181  return AVERROR_EOF;
182  }
183  if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
184  return AVERROR(ENOMEM);
185  samples = (int16_t *)frame->data[0];
186 
187  for (i = 0; i < nb_samples; i++) {
188  samples[i] = sine->sin[sine->phi >> (32 - LOG_PERIOD)];
189  sine->phi += sine->dphi;
190  if (sine->beep_index < sine->beep_length) {
191  samples[i] += sine->sin[sine->phi_beep >> (32 - LOG_PERIOD)] << 1;
192  sine->phi_beep += sine->dphi_beep;
193  }
194  if (++sine->beep_index == sine->beep_period)
195  sine->beep_index = 0;
196  }
197 
198  frame->pts = sine->pts;
199  sine->pts += nb_samples;
200  return ff_filter_frame(outlink, frame);
201 }
202 
203 static const AVFilterPad sine_outputs[] = {
204  {
205  .name = "default",
206  .type = AVMEDIA_TYPE_AUDIO,
207  .request_frame = request_frame,
208  .config_props = config_props,
209  },
210  { NULL }
211 };
212 
214  .name = "sine",
215  .description = NULL_IF_CONFIG_SMALL("Generate sine wave audio signal."),
216  .query_formats = query_formats,
217  .init = init,
218  .uninit = uninit,
219  .priv_size = sizeof(SineContext),
220  .inputs = NULL,
221  .outputs = sine_outputs,
222  .priv_class = &sine_class,
223 };
const char * s
Definition: avisynth_c.h:668
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
#define OPT_INT(name, field, def, min, max, descr,...)
Definition: asrc_sine.c:55
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
uint32_t dphi_beep
phase increment of the beep
Definition: asrc_sine.c:45
static const AVOption sine_options[]
Definition: asrc_sine.c:64
#define AMPLITUDE
Definition: asrc_sine.c:80
uint32_t phi_beep
current phase of the beep
Definition: asrc_sine.c:44
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
const char * name
Pad name.
int samples_per_frame
Definition: asrc_sine.c:34
AVFILTER_DEFINE_CLASS(sine)
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.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int init(AVFilterContext *ctx)
Definition: asrc_sine.c:123
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
static int64_t duration
Definition: ffplay.c:294
#define LOG_PERIOD
Definition: asrc_sine.c:79
frame
Definition: stft.m:14
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
int sample_rate
Definition: asrc_sine.c:35
#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
double frequency
Definition: asrc_sine.c:32
simple assert() macros that are a bit more flexible than ISO C assert().
unsigned beep_length
Definition: asrc_sine.c:43
audio channel layout utility functions
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:118
int64_t pts
Definition: asrc_sine.c:38
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:196
#define FFMIN(a, b)
Definition: common.h:58
double beep_factor
Definition: asrc_sine.c:33
for k
NULL
Definition: eval.c:55
sample_rate
static int request_frame(AVFilterLink *outlink)
Definition: asrc_sine.c:170
static const AVFilterPad sine_outputs[]
Definition: asrc_sine.c:203
AVFilterChannelLayouts * avfilter_make_format64_list(const int64_t *fmts)
Definition: formats.c:317
#define OPT_DBL(name, field, def, min, max, descr,...)
Definition: asrc_sine.c:58
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
static void make_sin_table(int16_t *sin)
Definition: asrc_sine.c:83
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
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
void ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:533
static av_cold void uninit(AVFilterContext *ctx)
Definition: asrc_sine.c:142
unsigned beep_period
Definition: asrc_sine.c:41
int64_t duration
Definition: asrc_sine.c:36
static double c[64]
AVFilter avfilter_asrc_sine
Definition: asrc_sine.c:213
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
#define AMPLITUDE_SHIFT
Definition: asrc_sine.c:81
static av_cold int config_props(AVFilterLink *outlink)
Definition: asrc_sine.c:163
uint32_t phi
current phase of the sine (2pi = 1<<32)
Definition: asrc_sine.c:39
uint32_t dphi
phase increment between two samples
Definition: asrc_sine.c:40
static av_cold int query_formats(AVFilterContext *ctx)
Definition: asrc_sine.c:149
int16_t * sin
Definition: asrc_sine.c:37
An instance of a filter.
Definition: avfilter.h:524
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
unsigned beep_index
Definition: asrc_sine.c:42
Filter the word “frame” indicates either a video frame or a group of audio samples
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
#define AV_CH_LAYOUT_MONO
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
for(j=16;j >0;--j)
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
#define OPT_DUR(name, field, def, min, max, descr,...)
Definition: asrc_sine.c:61