FFmpeg
af_adynamicsmooth.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "libavutil/ffmath.h"
20 #include "libavutil/opt.h"
21 #include "avfilter.h"
22 #include "audio.h"
23 #include "formats.h"
24 
25 typedef struct AudioDynamicSmoothContext {
26  const AVClass *class;
27 
28  double sensitivity;
29  double basefreq;
30 
33 
35 {
36  AVFilterContext *ctx = inlink->dst;
38 
39  s->coeffs = ff_get_audio_buffer(inlink, 3);
40  if (!s->coeffs)
41  return AVERROR(ENOMEM);
42 
43  return 0;
44 }
45 
47 {
48  AVFilterContext *ctx = inlink->dst;
49  AVFilterLink *outlink = ctx->outputs[0];
51  const double sensitivity = s->sensitivity;
52  const double wc = s->basefreq / in->sample_rate;
53  AVFrame *out;
54 
55  if (av_frame_is_writable(in)) {
56  out = in;
57  } else {
58  out = ff_get_audio_buffer(outlink, in->nb_samples);
59  if (!out) {
60  av_frame_free(&in);
61  return AVERROR(ENOMEM);
62  }
64  }
65 
66  for (int ch = 0; ch < out->channels; ch++) {
67  const double *src = (const double *)in->extended_data[ch];
68  double *dst = (double *)out->extended_data[ch];
69  double *coeffs = (double *)s->coeffs->extended_data[ch];
70  double low1 = coeffs[0];
71  double low2 = coeffs[1];
72  double inz = coeffs[2];
73 
74  for (int n = 0; n < out->nb_samples; n++) {
75  double low1z = low1;
76  double low2z = low2;
77  double bandz = low2z - low1z;
78  double wd = wc + sensitivity * fabs(bandz);
79  double g = fmin(1., wd * (5.9948827 + wd * (-11.969296 + wd * 15.959062)));
80 
81  low1 = low1z + g * (0.5 * (src[n] + inz) - low1z);
82  low2 = low2z + g * (0.5 * (low1 + low1z) - low2z);
83  inz = src[n];
84  dst[n] = ctx->is_disabled ? src[n] : low2;
85  }
86 
87  coeffs[0] = low1;
88  coeffs[1] = low2;
89  coeffs[2] = inz;
90  }
91 
92  if (out != in)
93  av_frame_free(&in);
94  return ff_filter_frame(outlink, out);
95 }
96 
98 {
100 
101  av_frame_free(&s->coeffs);
102 }
103 
104 #define OFFSET(x) offsetof(AudioDynamicSmoothContext, x)
105 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
106 
108  { "sensitivity", "set smooth sensitivity", OFFSET(sensitivity), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, 1000000, FLAGS },
109  { "basefreq", "set base frequency", OFFSET(basefreq), AV_OPT_TYPE_DOUBLE, {.dbl=22050}, 2, 1000000, FLAGS },
110  { NULL }
111 };
112 
113 AVFILTER_DEFINE_CLASS(adynamicsmooth);
114 
115 static const AVFilterPad inputs[] = {
116  {
117  .name = "default",
118  .type = AVMEDIA_TYPE_AUDIO,
119  .filter_frame = filter_frame,
120  .config_props = config_input,
121  },
122 };
123 
124 static const AVFilterPad outputs[] = {
125  {
126  .name = "default",
127  .type = AVMEDIA_TYPE_AUDIO,
128  },
129 };
130 
132  .name = "adynamicsmooth",
133  .description = NULL_IF_CONFIG_SMALL("Apply Dynamic Smoothing of input audio."),
134  .priv_size = sizeof(AudioDynamicSmoothContext),
135  .priv_class = &adynamicsmooth_class,
136  .uninit = uninit,
141  .process_command = ff_filter_process_command,
142 };
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:88
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
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:1018
FILTER_SINGLE_SAMPLEFMT
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition: internal.h:184
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:109
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
AVOption
AVOption.
Definition: opt.h:247
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
formats.h
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_adynamicsmooth.c:46
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_adynamicsmooth.c:97
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
av_cold
#define av_cold
Definition: attributes.h:90
outputs
static const AVFilterPad outputs[]
Definition: af_adynamicsmooth.c:124
s
#define s(width, name)
Definition: cbs_vp9.c:257
g
const char * g
Definition: vf_curves.c:117
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:226
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
adynamicsmooth_options
static const AVOption adynamicsmooth_options[]
Definition: af_adynamicsmooth.c:107
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
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
src
#define src
Definition: vp8dsp.c:255
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
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:117
AVFrame::sample_rate
int sample_rate
Sample rate of the audio data.
Definition: frame.h:494
fmin
double fmin(double, double)
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:473
ff_filter_process_command
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:882
OFFSET
#define OFFSET(x)
Definition: af_adynamicsmooth.c:104
FLAGS
#define FLAGS
Definition: af_adynamicsmooth.c:105
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:397
inputs
static const AVFilterPad inputs[]
Definition: af_adynamicsmooth.c:115
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:378
ff_af_adynamicsmooth
const AVFilter ff_af_adynamicsmooth
Definition: af_adynamicsmooth.c:131
AudioDynamicSmoothContext::sensitivity
double sensitivity
Definition: af_adynamicsmooth.c:28
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
AudioDynamicSmoothContext::coeffs
AVFrame * coeffs
Definition: af_adynamicsmooth.c:31
AVFilter
Filter definition.
Definition: avfilter.h:165
avfilter.h
AudioDynamicSmoothContext::basefreq
double basefreq
Definition: af_adynamicsmooth.c:29
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:70
ffmath.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
audio.h
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_adynamicsmooth.c:34
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition: avfilter.h:154
AudioDynamicSmoothContext
Definition: af_adynamicsmooth.c:25
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(adynamicsmooth)