FFmpeg  4.3.9
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, int start, int end);
47 
48 #define OFFSET(x) offsetof(ASoftClipContext, x)
49 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_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 
77  formats = ff_make_format_list(sample_fmts);
78  if (!formats)
79  return AVERROR(ENOMEM);
80  ret = ff_set_common_formats(ctx, formats);
81  if (ret < 0)
82  return ret;
83 
84  layouts = ff_all_channel_counts();
85  if (!layouts)
86  return AVERROR(ENOMEM);
87 
88  ret = ff_set_common_channel_layouts(ctx, layouts);
89  if (ret < 0)
90  return ret;
91 
92  formats = ff_all_samplerates();
93  return ff_set_common_samplerates(ctx, formats);
94 }
95 
96 #define SQR(x) ((x) * (x))
97 
99  void **dptr, const void **sptr,
100  int nb_samples, int channels,
101  int start, int end)
102 {
103  float param = s->param;
104 
105  for (int c = start; c < end; c++) {
106  const float *src = sptr[c];
107  float *dst = dptr[c];
108 
109  switch (s->type) {
110  case ASC_TANH:
111  for (int n = 0; n < nb_samples; n++) {
112  dst[n] = tanhf(src[n] * param);
113  }
114  break;
115  case ASC_ATAN:
116  for (int n = 0; n < nb_samples; n++)
117  dst[n] = 2.f / M_PI * atanf(src[n] * param);
118  break;
119  case ASC_CUBIC:
120  for (int n = 0; n < nb_samples; n++) {
121  if (FFABS(src[n]) >= 1.5f)
122  dst[n] = FFSIGN(src[n]);
123  else
124  dst[n] = src[n] - 0.1481f * powf(src[n], 3.f);
125  }
126  break;
127  case ASC_EXP:
128  for (int n = 0; n < nb_samples; n++)
129  dst[n] = 2.f / (1.f + expf(-2.f * src[n])) - 1.;
130  break;
131  case ASC_ALG:
132  for (int n = 0; n < nb_samples; n++)
133  dst[n] = src[n] / (sqrtf(param + src[n] * src[n]));
134  break;
135  case ASC_QUINTIC:
136  for (int n = 0; n < nb_samples; n++) {
137  if (FFABS(src[n]) >= 1.25)
138  dst[n] = FFSIGN(src[n]);
139  else
140  dst[n] = src[n] - 0.08192f * powf(src[n], 5.f);
141  }
142  break;
143  case ASC_SIN:
144  for (int n = 0; n < nb_samples; n++) {
145  if (FFABS(src[n]) >= M_PI_2)
146  dst[n] = FFSIGN(src[n]);
147  else
148  dst[n] = sinf(src[n]);
149  }
150  break;
151  }
152  }
153 }
154 
156  void **dptr, const void **sptr,
157  int nb_samples, int channels,
158  int start, int end)
159 {
160  double param = s->param;
161 
162  for (int c = start; c < end; c++) {
163  const double *src = sptr[c];
164  double *dst = dptr[c];
165 
166  switch (s->type) {
167  case ASC_TANH:
168  for (int n = 0; n < nb_samples; n++) {
169  dst[n] = tanh(src[n] * param);
170  }
171  break;
172  case ASC_ATAN:
173  for (int n = 0; n < nb_samples; n++)
174  dst[n] = 2. / M_PI * atan(src[n] * param);
175  break;
176  case ASC_CUBIC:
177  for (int n = 0; n < nb_samples; n++) {
178  if (FFABS(src[n]) >= 1.5)
179  dst[n] = FFSIGN(src[n]);
180  else
181  dst[n] = src[n] - 0.1481 * pow(src[n], 3.);
182  }
183  break;
184  case ASC_EXP:
185  for (int n = 0; n < nb_samples; n++)
186  dst[n] = 2. / (1. + exp(-2. * src[n])) - 1.;
187  break;
188  case ASC_ALG:
189  for (int n = 0; n < nb_samples; n++)
190  dst[n] = src[n] / (sqrt(param + src[n] * src[n]));
191  break;
192  case ASC_QUINTIC:
193  for (int n = 0; n < nb_samples; n++) {
194  if (FFABS(src[n]) >= 1.25)
195  dst[n] = FFSIGN(src[n]);
196  else
197  dst[n] = src[n] - 0.08192 * pow(src[n], 5.);
198  }
199  break;
200  case ASC_SIN:
201  for (int n = 0; n < nb_samples; n++) {
202  if (FFABS(src[n]) >= M_PI_2)
203  dst[n] = FFSIGN(src[n]);
204  else
205  dst[n] = sin(src[n]);
206  }
207  break;
208  }
209  }
210 }
211 
212 static int config_input(AVFilterLink *inlink)
213 {
214  AVFilterContext *ctx = inlink->dst;
215  ASoftClipContext *s = ctx->priv;
216 
217  switch (inlink->format) {
218  case AV_SAMPLE_FMT_FLT:
219  case AV_SAMPLE_FMT_FLTP: s->filter = filter_flt; break;
220  case AV_SAMPLE_FMT_DBL:
221  case AV_SAMPLE_FMT_DBLP: s->filter = filter_dbl; break;
222  }
223 
224  return 0;
225 }
226 
227 typedef struct ThreadData {
228  AVFrame *in, *out;
230  int channels;
231 } ThreadData;
232 
233 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
234 {
235  ASoftClipContext *s = ctx->priv;
236  ThreadData *td = arg;
237  AVFrame *out = td->out;
238  AVFrame *in = td->in;
239  const int channels = td->channels;
240  const int nb_samples = td->nb_samples;
241  const int start = (channels * jobnr) / nb_jobs;
242  const int end = (channels * (jobnr+1)) / nb_jobs;
243 
244  s->filter(s, (void **)out->extended_data, (const void **)in->extended_data,
245  nb_samples, channels, start, end);
246 
247  return 0;
248 }
249 
250 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
251 {
252  AVFilterContext *ctx = inlink->dst;
253  AVFilterLink *outlink = ctx->outputs[0];
254  int nb_samples, channels;
255  ThreadData td;
256  AVFrame *out;
257 
258  if (av_frame_is_writable(in)) {
259  out = in;
260  } else {
261  out = ff_get_audio_buffer(outlink, in->nb_samples);
262  if (!out) {
263  av_frame_free(&in);
264  return AVERROR(ENOMEM);
265  }
266  av_frame_copy_props(out, in);
267  }
268 
269  if (av_sample_fmt_is_planar(in->format)) {
270  nb_samples = in->nb_samples;
271  channels = in->channels;
272  } else {
273  nb_samples = in->channels * in->nb_samples;
274  channels = 1;
275  }
276 
277  td.in = in;
278  td.out = out;
279  td.nb_samples = nb_samples;
280  td.channels = channels;
281  ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(channels,
283 
284  if (out != in)
285  av_frame_free(&in);
286 
287  return ff_filter_frame(outlink, out);
288 }
289 
290 static const AVFilterPad inputs[] = {
291  {
292  .name = "default",
293  .type = AVMEDIA_TYPE_AUDIO,
294  .filter_frame = filter_frame,
295  .config_props = config_input,
296  },
297  { NULL }
298 };
299 
300 static const AVFilterPad outputs[] = {
301  {
302  .name = "default",
303  .type = AVMEDIA_TYPE_AUDIO,
304  },
305  { NULL }
306 };
307 
309  .name = "asoftclip",
310  .description = NULL_IF_CONFIG_SMALL("Audio Soft Clipper."),
311  .query_formats = query_formats,
312  .priv_size = sizeof(ASoftClipContext),
313  .priv_class = &asoftclip_class,
314  .inputs = inputs,
315  .outputs = outputs,
319 };
float, planar
Definition: samplefmt.h:69
#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:586
AVFrame * out
Definition: af_adeclick.c:494
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
AVOption.
Definition: opt.h:246
static int config_input(AVFilterLink *inlink)
Definition: af_asoftclip.c:212
Main libavfilter public API header.
static const AVOption asoftclip_options[]
Definition: af_asoftclip.c:51
double, planar
Definition: samplefmt.h:70
static const AVFilterPad outputs[]
Definition: af_asoftclip.c:300
static int query_formats(AVFilterContext *ctx)
Definition: af_asoftclip.c:66
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:300
#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
const char * name
Pad name.
Definition: internal.h:60
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1075
#define OFFSET(x)
Definition: af_asoftclip.c:48
AVOptions.
#define f(width, name)
Definition: cbs_vp9.c:255
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
#define atanf(x)
Definition: libm.h:40
channels
Definition: aptx.h:33
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:112
A filter pad used for either input or output.
Definition: internal.h:54
#define expf(x)
Definition: libm.h:283
#define src
Definition: vp8dsp.c:254
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:605
#define td
Definition: regdef.h:70
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:203
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options...
Definition: avfilter.c:869
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:116
static const AVFilterPad inputs[]
Definition: af_asoftclip.c:290
const char * arg
Definition: jacosubdec.c:66
int8_t exp
Definition: eval.c:72
static void filter_dbl(ASoftClipContext *s, void **dptr, const void **sptr, int nb_samples, int channels, int start, int end)
Definition: af_asoftclip.c:155
#define A
Definition: af_asoftclip.c:49
#define powf(x, y)
Definition: libm.h:50
int channels
number of audio channels, only used for audio.
Definition: frame.h:606
audio channel layout utility functions
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:784
#define FFMIN(a, b)
Definition: common.h:96
#define FFSIGN(a)
Definition: common.h:73
#define M_PI_2
Definition: mathematics.h:55
AVFormatContext * ctx
Definition: movenc.c:48
AVFILTER_DEFINE_CLASS(asoftclip)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
void(* filter)(struct ASoftClipContext *s, void **dst, const void **src, int nb_samples, int channels, int start, int end)
Definition: af_asoftclip.c:44
A list of supported channel layouts.
Definition: formats.h:85
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:373
#define sinf(x)
Definition: libm.h:419
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
typedef void(RENAME(mix_any_func_type))
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:595
Used for passing data between threads.
Definition: dsddec.c:67
AVFilter ff_af_asoftclip
Definition: af_asoftclip.c:308
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)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_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
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:439
#define flags(name, subs,...)
Definition: cbs_av1.c:576
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:378
ASoftClipTypes
Definition: af_asoftclip.c:27
static double c[64]
avfilter_execute_func * execute
Definition: internal.h:144
static void filter_flt(ASoftClipContext *s, void **dptr, const void **sptr, int nb_samples, int channels, int start, int end)
Definition: af_asoftclip.c:98
A list of supported formats for one end of a filter link.
Definition: formats.h:64
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_asoftclip.c:250
static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_asoftclip.c:233
An instance of a filter.
Definition: avfilter.h:338
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:731
FILE * out
Definition: movenc.c:54
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_afftdn.c:1374
#define M_PI
Definition: mathematics.h:52
AVFrame * in
Definition: af_afftdn.c:1083
formats
Definition: signature.h:48
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition...
Definition: formats.c:454
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:347
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:366
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:593
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:659