FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
af_stereotools.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen
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 
22 #include "libavutil/opt.h"
23 #include "avfilter.h"
24 #include "audio.h"
25 #include "formats.h"
26 
27 typedef struct StereoToolsContext {
28  const AVClass *class;
29 
30  int softclip;
31  int mute_l;
32  int mute_r;
33  int phase_l;
34  int phase_r;
35  int mode;
36  double slev;
37  double sbal;
38  double mlev;
39  double mpan;
40  double phase;
41  double base;
42  double delay;
43  double balance_in;
44  double balance_out;
47  double sc_level;
49  double level_in;
50  double level_out;
51 
52  double *buffer;
53  int length;
54  int pos;
56 
57 #define OFFSET(x) offsetof(StereoToolsContext, x)
58 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
59 
60 static const AVOption stereotools_options[] = {
61  { "level_in", "set level in", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
62  { "level_out", "set level out", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
63  { "balance_in", "set balance in", OFFSET(balance_in), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
64  { "balance_out", "set balance out", OFFSET(balance_out), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
65  { "softclip", "enable softclip", OFFSET(softclip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
66  { "mutel", "mute L", OFFSET(mute_l), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
67  { "muter", "mute R", OFFSET(mute_r), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
68  { "phasel", "phase L", OFFSET(phase_l), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
69  { "phaser", "phase R", OFFSET(phase_r), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
70  { "mode", "set stereo mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 6, A, "mode" },
71  { "lr>lr", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, "mode" },
72  { "lr>ms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, "mode" },
73  { "ms>lr", 0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, A, "mode" },
74  { "lr>ll", 0, 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, A, "mode" },
75  { "lr>rr", 0, 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, A, "mode" },
76  { "lr>l+r", 0, 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, A, "mode" },
77  { "lr>rl", 0, 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, A, "mode" },
78  { "slev", "set side level", OFFSET(slev), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
79  { "sbal", "set side balance", OFFSET(sbal), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
80  { "mlev", "set middle level", OFFSET(mlev), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
81  { "mpan", "set middle pan", OFFSET(mpan), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
82  { "base", "set stereo base", OFFSET(base), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
83  { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -20, 20, A },
84  { "sclevel", "set S/C level", OFFSET(sc_level), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 100, A },
85  { "phase", "set stereo phase", OFFSET(phase), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 360, A },
86  { NULL }
87 };
88 
89 AVFILTER_DEFINE_CLASS(stereotools);
90 
92 {
95  int ret;
96 
97  if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_DBL )) < 0 ||
98  (ret = ff_set_common_formats (ctx , formats )) < 0 ||
99  (ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO)) < 0 ||
100  (ret = ff_set_common_channel_layouts (ctx , layout )) < 0)
101  return ret;
102 
103  formats = ff_all_samplerates();
104  return ff_set_common_samplerates(ctx, formats);
105 }
106 
107 static int config_input(AVFilterLink *inlink)
108 {
109  AVFilterContext *ctx = inlink->dst;
110  StereoToolsContext *s = ctx->priv;
111 
112  s->length = 2 * inlink->sample_rate * 0.05;
113  if (s->length <= 1 || s->length & 1) {
114  av_log(ctx, AV_LOG_ERROR, "sample rate is too small\n");
115  return AVERROR(EINVAL);
116  }
117  s->buffer = av_calloc(s->length, sizeof(*s->buffer));
118  if (!s->buffer)
119  return AVERROR(ENOMEM);
120 
121  s->inv_atan_shape = 1.0 / atan(s->sc_level);
122  s->phase_cos_coef = cos(s->phase / 180 * M_PI);
123  s->phase_sin_coef = sin(s->phase / 180 * M_PI);
124 
125  return 0;
126 }
127 
128 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
129 {
130  AVFilterContext *ctx = inlink->dst;
131  AVFilterLink *outlink = ctx->outputs[0];
132  StereoToolsContext *s = ctx->priv;
133  const double *src = (const double *)in->data[0];
134  const double sb = s->base < 0 ? s->base * 0.5 : s->base;
135  const double sbal = 1 + s->sbal;
136  const double mpan = 1 + s->mpan;
137  const double slev = s->slev;
138  const double mlev = s->mlev;
139  const double balance_in = s->balance_in;
140  const double balance_out = s->balance_out;
141  const double level_in = s->level_in;
142  const double level_out = s->level_out;
143  const double sc_level = s->sc_level;
144  const double delay = s->delay;
145  const int length = s->length;
146  const int mute_l = s->mute_l;
147  const int mute_r = s->mute_r;
148  const int phase_l = s->phase_l;
149  const int phase_r = s->phase_r;
150  double *buffer = s->buffer;
151  AVFrame *out;
152  double *dst;
153  int nbuf = inlink->sample_rate * (fabs(delay) / 1000.);
154  int n;
155 
156  nbuf -= nbuf % 2;
157  if (av_frame_is_writable(in)) {
158  out = in;
159  } else {
160  out = ff_get_audio_buffer(inlink, in->nb_samples);
161  if (!out) {
162  av_frame_free(&in);
163  return AVERROR(ENOMEM);
164  }
166  }
167  dst = (double *)out->data[0];
168 
169  for (n = 0; n < in->nb_samples; n++, src += 2, dst += 2) {
170  double L = src[0], R = src[1], l, r, m, S;
171 
172  L *= level_in;
173  R *= level_in;
174 
175  L *= 1. - FFMAX(0., balance_in);
176  R *= 1. + FFMIN(0., balance_in);
177 
178  if (s->softclip) {
179  R = s->inv_atan_shape * atan(R * sc_level);
180  L = s->inv_atan_shape * atan(L * sc_level);
181  }
182 
183  switch (s->mode) {
184  case 0:
185  m = (L + R) * 0.5;
186  S = (L - R) * 0.5;
187  l = m * mlev * FFMIN(1., 2. - mpan) + S * slev * FFMIN(1., 2. - sbal);
188  r = m * mlev * FFMIN(1., mpan) - S * slev * FFMIN(1., sbal);
189  L = l;
190  R = r;
191  break;
192  case 1:
193  l = L * FFMIN(1., 2. - sbal);
194  r = R * FFMIN(1., sbal);
195  L = 0.5 * (l + r) * mlev;
196  R = 0.5 * (l - r) * slev;
197  break;
198  case 2:
199  l = L * mlev * FFMIN(1., 2. - mpan) + R * slev * FFMIN(1., 2. - sbal);
200  r = L * mlev * FFMIN(1., mpan) - R * slev * FFMIN(1., sbal);
201  L = l;
202  R = r;
203  break;
204  case 3:
205  R = L;
206  break;
207  case 4:
208  L = R;
209  break;
210  case 5:
211  L = (L + R) / 2;
212  R = L;
213  break;
214  case 6:
215  l = L;
216  L = R;
217  R = l;
218  m = (L + R) * 0.5;
219  S = (L - R) * 0.5;
220  l = m * mlev * FFMIN(1., 2. - mpan) + S * slev * FFMIN(1., 2. - sbal);
221  r = m * mlev * FFMIN(1., mpan) - S * slev * FFMIN(1., sbal);
222  L = l;
223  R = r;
224  break;
225  }
226 
227  L *= 1. - mute_l;
228  R *= 1. - mute_r;
229 
230  L *= (2. * (1. - phase_l)) - 1.;
231  R *= (2. * (1. - phase_r)) - 1.;
232 
233  buffer[s->pos ] = L;
234  buffer[s->pos+1] = R;
235 
236  if (delay > 0.) {
237  R = buffer[(s->pos - (int)nbuf + 1 + length) % length];
238  } else if (delay < 0.) {
239  L = buffer[(s->pos - (int)nbuf + length) % length];
240  }
241 
242  l = L + sb * L - sb * R;
243  r = R + sb * R - sb * L;
244 
245  L = l;
246  R = r;
247 
248  l = L * s->phase_cos_coef - R * s->phase_sin_coef;
249  r = L * s->phase_sin_coef + R * s->phase_cos_coef;
250 
251  L = l;
252  R = r;
253 
254  s->pos = (s->pos + 2) % s->length;
255 
256  L *= 1. - FFMAX(0., balance_out);
257  R *= 1. + FFMIN(0., balance_out);
258 
259  L *= level_out;
260  R *= level_out;
261 
262  dst[0] = L;
263  dst[1] = R;
264  }
265 
266  if (out != in)
267  av_frame_free(&in);
268  return ff_filter_frame(outlink, out);
269 }
270 
272 {
273  StereoToolsContext *s = ctx->priv;
274 
275  av_freep(&s->buffer);
276 }
277 
278 static const AVFilterPad inputs[] = {
279  {
280  .name = "default",
281  .type = AVMEDIA_TYPE_AUDIO,
282  .filter_frame = filter_frame,
283  .config_props = config_input,
284  },
285  { NULL }
286 };
287 
288 static const AVFilterPad outputs[] = {
289  {
290  .name = "default",
291  .type = AVMEDIA_TYPE_AUDIO,
292  },
293  { NULL }
294 };
295 
297  .name = "stereotools",
298  .description = NULL_IF_CONFIG_SMALL("Apply various stereo tools."),
299  .query_formats = query_formats,
300  .priv_size = sizeof(StereoToolsContext),
301  .priv_class = &stereotools_class,
302  .uninit = uninit,
303  .inputs = inputs,
304  .outputs = outputs,
305 };
static const AVOption stereotools_options[]
#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 * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
AVOption.
Definition: opt.h:245
AVFormatContext * ctx
Definition: movenc-test.c:48
Main libavfilter public API header.
static enum AVSampleFormat formats[]
#define AV_CH_LAYOUT_STEREO
const char * name
Pad name.
Definition: internal.h:59
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1163
#define av_cold
Definition: attributes.h:82
mode
Definition: f_perms.c:27
AVOptions.
#define OFFSET(x)
#define av_log(a,...)
#define A
unsigned m
Definition: audioconvert.c:187
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
A filter pad used for either input or output.
Definition: internal.h:53
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:65
#define S(s, c, i)
#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:154
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
const char * r
Definition: vf_curves.c:107
void * priv
private data for use by the filter
Definition: avfilter.h:319
GLsizei GLsizei * length
Definition: opengl_enc.c:115
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:337
#define FFMAX(a, b)
Definition: common.h:94
AVFilter ff_af_stereotools
audio channel layout utility functions
static av_cold void uninit(AVFilterContext *ctx)
#define FFMIN(a, b)
Definition: common.h:96
AVFILTER_DEFINE_CLASS(stereotools)
static int query_formats(AVFilterContext *ctx)
int n
Definition: avisynth_c.h:547
#define L(x)
Definition: vp56_arith.h:36
#define src
Definition: vp9dsp.c:530
static const AVFilterPad outputs[]
static int config_input(AVFilterLink *inlink)
A list of supported channel layouts.
Definition: formats.h:85
FILE * out
Definition: movenc-test.c:54
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:505
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:141
const char * name
Filter name.
Definition: avfilter.h:145
Definition: vf_geq.c:46
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:316
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
void * av_calloc(size_t nmemb, size_t size)
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
Definition: mem.c:260
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
static const AVFilterPad inputs[]
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:304
#define av_freep(p)
#define M_PI
Definition: mathematics.h:46
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:235
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:565
GLuint buffer
Definition: opengl_enc.c:102