FFmpeg
af_asoftclip.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 The FFmpeg Project
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 
36 };
37 
38 typedef struct ASoftClipContext {
39  const AVClass *class;
40 
41  int type;
42  double param;
43 
44  void (*filter)(struct ASoftClipContext *s, void **dst, const void **src,
45  int nb_samples, int channels);
47 
48 #define OFFSET(x) offsetof(ASoftClipContext, x)
49 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50 
51 static const AVOption asoftclip_options[] = {
52  { "type", "set softclip type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_TYPES-1, A, "types" },
53  { "tanh", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_TANH}, 0, 0, A, "types" },
54  { "atan", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ATAN}, 0, 0, A, "types" },
55  { "cubic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_CUBIC}, 0, 0, A, "types" },
56  { "exp", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_EXP}, 0, 0, A, "types" },
57  { "alg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ALG}, 0, 0, A, "types" },
58  { "quintic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_QUINTIC},0, 0, A, "types" },
59  { "sin", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_SIN}, 0, 0, A, "types" },
60  { "param", "set softclip parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 3, A },
61  { NULL }
62 };
63 
64 AVFILTER_DEFINE_CLASS(asoftclip);
65 
67 {
70  static const enum AVSampleFormat sample_fmts[] = {
74  };
75  int ret;
76 
78  if (!formats)
79  return AVERROR(ENOMEM);
81  if (ret < 0)
82  return ret;
83 
85  if (!layouts)
86  return AVERROR(ENOMEM);
87 
89  if (ret < 0)
90  return ret;
91 
94 }
95 
96 #define SQR(x) ((x) * (x))
97 
99  void **dptr, const void **sptr,
100  int nb_samples, int channels)
101 {
102  float param = s->param;
103 
104  for (int c = 0; c < channels; c++) {
105  const float *src = sptr[c];
106  float *dst = dptr[c];
107 
108  switch (s->type) {
109  case ASC_TANH:
110  for (int n = 0; n < nb_samples; n++) {
111  dst[n] = tanhf(src[n] * param);
112  }
113  break;
114  case ASC_ATAN:
115  for (int n = 0; n < nb_samples; n++)
116  dst[n] = 2.f / M_PI * atanf(src[n] * param);
117  break;
118  case ASC_CUBIC:
119  for (int n = 0; n < nb_samples; n++) {
120  if (FFABS(src[n]) >= 1.5f)
121  dst[n] = FFSIGN(src[n]);
122  else
123  dst[n] = src[n] - 0.1481f * powf(src[n], 3.f);
124  }
125  break;
126  case ASC_EXP:
127  for (int n = 0; n < nb_samples; n++)
128  dst[n] = 2.f / (1.f + expf(-2.f * src[n])) - 1.;
129  break;
130  case ASC_ALG:
131  for (int n = 0; n < nb_samples; n++)
132  dst[n] = src[n] / (sqrtf(param + src[n] * src[n]));
133  break;
134  case ASC_QUINTIC:
135  for (int n = 0; n < nb_samples; n++) {
136  if (FFABS(src[n]) >= 1.25)
137  dst[n] = FFSIGN(src[n]);
138  else
139  dst[n] = src[n] - 0.08192f * powf(src[n], 5.f);
140  }
141  break;
142  case ASC_SIN:
143  for (int n = 0; n < nb_samples; n++) {
144  if (FFABS(src[n]) >= M_PI_2)
145  dst[n] = FFSIGN(src[n]);
146  else
147  dst[n] = sinf(src[n]);
148  }
149  break;
150  }
151  }
152 }
153 
155  void **dptr, const void **sptr,
156  int nb_samples, int channels)
157 {
158  double param = s->param;
159 
160  for (int c = 0; c < channels; c++) {
161  const double *src = sptr[c];
162  double *dst = dptr[c];
163 
164  switch (s->type) {
165  case ASC_TANH:
166  for (int n = 0; n < nb_samples; n++) {
167  dst[n] = tanh(src[n] * param);
168  }
169  break;
170  case ASC_ATAN:
171  for (int n = 0; n < nb_samples; n++)
172  dst[n] = 2. / M_PI * atan(src[n] * param);
173  break;
174  case ASC_CUBIC:
175  for (int n = 0; n < nb_samples; n++) {
176  if (FFABS(src[n]) >= 1.5)
177  dst[n] = FFSIGN(src[n]);
178  else
179  dst[n] = src[n] - 0.1481 * pow(src[n], 3.);
180  }
181  break;
182  case ASC_EXP:
183  for (int n = 0; n < nb_samples; n++)
184  dst[n] = 2. / (1. + exp(-2. * src[n])) - 1.;
185  break;
186  case ASC_ALG:
187  for (int n = 0; n < nb_samples; n++)
188  dst[n] = src[n] / (sqrt(param + src[n] * src[n]));
189  break;
190  case ASC_QUINTIC:
191  for (int n = 0; n < nb_samples; n++) {
192  if (FFABS(src[n]) >= 1.25)
193  dst[n] = FFSIGN(src[n]);
194  else
195  dst[n] = src[n] - 0.08192 * pow(src[n], 5.);
196  }
197  break;
198  case ASC_SIN:
199  for (int n = 0; n < nb_samples; n++) {
200  if (FFABS(src[n]) >= M_PI_2)
201  dst[n] = FFSIGN(src[n]);
202  else
203  dst[n] = sin(src[n]);
204  }
205  break;
206  }
207  }
208 }
209 
211 {
212  AVFilterContext *ctx = inlink->dst;
213  ASoftClipContext *s = ctx->priv;
214 
215  switch (inlink->format) {
216  case AV_SAMPLE_FMT_FLT:
217  case AV_SAMPLE_FMT_FLTP: s->filter = filter_flt; break;
218  case AV_SAMPLE_FMT_DBL:
219  case AV_SAMPLE_FMT_DBLP: s->filter = filter_dbl; break;
220  }
221 
222  return 0;
223 }
224 
226 {
227  AVFilterContext *ctx = inlink->dst;
228  AVFilterLink *outlink = ctx->outputs[0];
229  ASoftClipContext *s = ctx->priv;
230  int nb_samples, channels;
231  AVFrame *out;
232 
233  if (av_frame_is_writable(in)) {
234  out = in;
235  } else {
236  out = ff_get_audio_buffer(outlink, in->nb_samples);
237  if (!out) {
238  av_frame_free(&in);
239  return AVERROR(ENOMEM);
240  }
242  }
243 
244  if (av_sample_fmt_is_planar(in->format)) {
245  nb_samples = in->nb_samples;
246  channels = in->channels;
247  } else {
248  nb_samples = in->channels * in->nb_samples;
249  channels = 1;
250  }
251 
252  s->filter(s, (void **)out->extended_data, (const void **)in->extended_data,
253  nb_samples, channels);
254 
255  if (out != in)
256  av_frame_free(&in);
257 
258  return ff_filter_frame(outlink, out);
259 }
260 
261 static const AVFilterPad inputs[] = {
262  {
263  .name = "default",
264  .type = AVMEDIA_TYPE_AUDIO,
265  .filter_frame = filter_frame,
266  .config_props = config_input,
267  },
268  { NULL }
269 };
270 
271 static const AVFilterPad outputs[] = {
272  {
273  .name = "default",
274  .type = AVMEDIA_TYPE_AUDIO,
275  },
276  { NULL }
277 };
278 
280  .name = "asoftclip",
281  .description = NULL_IF_CONFIG_SMALL("Audio Soft Clipper."),
282  .query_formats = query_formats,
283  .priv_size = sizeof(ASoftClipContext),
284  .priv_class = &asoftclip_class,
285  .inputs = inputs,
286  .outputs = outputs,
288 };
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
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
ASoftClipContext::filter
void(* filter)(struct ASoftClipContext *s, void **dst, const void **src, int nb_samples, int channels)
Definition: af_asoftclip.c:44
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
NB_TYPES
@ NB_TYPES
Definition: af_asoftclip.c:35
opt.h
ASC_EXP
@ ASC_EXP
Definition: af_asoftclip.c:31
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
out
FILE * out
Definition: movenc.c:54
n
int n
Definition: avisynth_c.h:760
ff_set_common_channel_layouts
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
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:686
layouts
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
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
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_asoftclip.c:210
outputs
static const AVFilterPad outputs[]
Definition: af_asoftclip.c:271
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
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:410
AudioConvert::channels
int channels
Definition: audio_convert.c:54
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
M_PI_2
#define M_PI_2
Definition: mathematics.h:55
AVOption
AVOption.
Definition: opt.h:246
expf
#define expf(x)
Definition: libm.h:283
asoftclip_options
static const AVOption asoftclip_options[]
Definition: af_asoftclip.c:51
channels
channels
Definition: aptx.c:30
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:148
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_asoftclip.c:66
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
ASC_ALG
@ ASC_ALG
Definition: af_asoftclip.c:32
formats.h
FFSIGN
#define FFSIGN(a)
Definition: common.h:73
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
ASC_TANH
@ ASC_TANH
Definition: af_asoftclip.c:28
OFFSET
#define OFFSET(x)
Definition: af_asoftclip.c:48
src
#define src
Definition: vp8dsp.c:254
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
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:568
ASC_ATAN
@ ASC_ATAN
Definition: af_asoftclip.c:29
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:225
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_sample_fmt_is_planar
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:112
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ASoftClipContext
Definition: af_asoftclip.c:38
f
#define f(width, name)
Definition: cbs_vp9.c:255
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
inputs
static const AVFilterPad inputs[]
Definition: af_asoftclip.c:261
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
ASC_CUBIC
@ ASC_CUBIC
Definition: af_asoftclip.c:30
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:654
ASoftClipContext::type
int type
Definition: af_asoftclip.c:41
A
#define A
Definition: af_asoftclip.c:49
ASC_QUINTIC
@ ASC_QUINTIC
Definition: af_asoftclip.c:33
sinf
#define sinf(x)
Definition: libm.h:419
exp
int8_t exp
Definition: eval.c:72
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
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(asoftclip)
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:188
powf
#define powf(x, y)
Definition: libm.h:50
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:594
ff_af_asoftclip
AVFilter ff_af_asoftclip
Definition: af_asoftclip.c:279
ASC_SIN
@ ASC_SIN
Definition: af_asoftclip.c:34
M_PI
#define M_PI
Definition: mathematics.h:52
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:125
in
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;} return ac;} 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;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->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);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Definition: audio_convert.c:326
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
atanf
#define atanf(x)
Definition: libm.h:40
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AVFilter
Filter definition.
Definition: avfilter.h:144
ret
ret
Definition: filter_design.txt:187
ASoftClipTypes
ASoftClipTypes
Definition: af_asoftclip.c:27
ff_all_samplerates
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
channel_layout.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
avfilter.h
filter_dbl
static void filter_dbl(ASoftClipContext *s, void **dptr, const void **sptr, int nb_samples, int channels)
Definition: af_asoftclip.c:154
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:70
AVFilterContext
An instance of a filter.
Definition: avfilter.h:338
audio.h
ASoftClipContext::param
double param
Definition: af_asoftclip.c:42
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
ff_set_common_samplerates
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556
filter_flt
static void filter_flt(ASoftClipContext *s, void **dptr, const void **sptr, int nb_samples, int channels)
Definition: af_asoftclip.c:98
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:64
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_asoftclip.c:225
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:232
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:63