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 
24 typedef struct AudioDynamicSmoothContext {
25  const AVClass *class;
26 
27  double sensitivity;
28  double basefreq;
29 
32 
34 {
35  AVFilterContext *ctx = inlink->dst;
37 
38  s->coeffs = ff_get_audio_buffer(inlink, 3);
39  if (!s->coeffs)
40  return AVERROR(ENOMEM);
41 
42  return 0;
43 }
44 
46 {
47  AVFilterContext *ctx = inlink->dst;
48  AVFilterLink *outlink = ctx->outputs[0];
50  const double sensitivity = s->sensitivity;
51  const double wc = s->basefreq / in->sample_rate;
52  AVFrame *out;
53 
54  if (av_frame_is_writable(in)) {
55  out = in;
56  } else {
57  out = ff_get_audio_buffer(outlink, in->nb_samples);
58  if (!out) {
59  av_frame_free(&in);
60  return AVERROR(ENOMEM);
61  }
63  }
64 
65  for (int ch = 0; ch < out->ch_layout.nb_channels; ch++) {
66  const double *src = (const double *)in->extended_data[ch];
67  double *dst = (double *)out->extended_data[ch];
68  double *coeffs = (double *)s->coeffs->extended_data[ch];
69  double low1 = coeffs[0];
70  double low2 = coeffs[1];
71  double inz = coeffs[2];
72 
73  for (int n = 0; n < out->nb_samples; n++) {
74  double low1z = low1;
75  double low2z = low2;
76  double bandz = low2z - low1z;
77  double wd = wc + sensitivity * fabs(bandz);
78  double g = fmin(1., wd * (5.9948827 + wd * (-11.969296 + wd * 15.959062)));
79 
80  low1 = low1z + g * (0.5 * (src[n] + inz) - low1z);
81  low2 = low2z + g * (0.5 * (low1 + low1z) - low2z);
82  inz = src[n];
83  dst[n] = ctx->is_disabled ? src[n] : low2;
84  }
85 
86  coeffs[0] = low1;
87  coeffs[1] = low2;
88  coeffs[2] = inz;
89  }
90 
91  if (out != in)
92  av_frame_free(&in);
93  return ff_filter_frame(outlink, out);
94 }
95 
97 {
99 
100  av_frame_free(&s->coeffs);
101 }
102 
103 #define OFFSET(x) offsetof(AudioDynamicSmoothContext, x)
104 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
105 
107  { "sensitivity", "set smooth sensitivity", OFFSET(sensitivity), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, 1000000, FLAGS },
108  { "basefreq", "set base frequency", OFFSET(basefreq), AV_OPT_TYPE_DOUBLE, {.dbl=22050}, 2, 1000000, FLAGS },
109  { NULL }
110 };
111 
112 AVFILTER_DEFINE_CLASS(adynamicsmooth);
113 
114 static const AVFilterPad inputs[] = {
115  {
116  .name = "default",
117  .type = AVMEDIA_TYPE_AUDIO,
118  .filter_frame = filter_frame,
119  .config_props = config_input,
120  },
121 };
122 
124  .name = "adynamicsmooth",
125  .description = NULL_IF_CONFIG_SMALL("Apply Dynamic Smoothing of input audio."),
126  .priv_size = sizeof(AudioDynamicSmoothContext),
127  .priv_class = &adynamicsmooth_class,
128  .uninit = uninit,
133  .process_command = ff_filter_process_command,
134 };
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:107
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:978
FILTER_SINGLE_SAMPLEFMT
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition: internal.h:185
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:100
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVOption
AVOption.
Definition: opt.h:251
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_adynamicsmooth.c:45
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_adynamicsmooth.c:96
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:47
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:198
g
const char * g
Definition: vf_curves.c:127
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:227
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
adynamicsmooth_options
static const AVOption adynamicsmooth_options[]
Definition: af_adynamicsmooth.c:106
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:192
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:736
ff_audio_default_filterpad
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition: audio.c:32
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:106
AVFrame::sample_rate
int sample_rate
Sample rate of the audio data.
Definition: frame.h:567
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
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:666
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:851
OFFSET
#define OFFSET(x)
Definition: af_adynamicsmooth.c:103
FLAGS
#define FLAGS
Definition: af_adynamicsmooth.c:104
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
inputs
static const AVFilterPad inputs[]
Definition: af_adynamicsmooth.c:114
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:401
ff_af_adynamicsmooth
const AVFilter ff_af_adynamicsmooth
Definition: af_adynamicsmooth.c:123
AudioDynamicSmoothContext::sensitivity
double sensitivity
Definition: af_adynamicsmooth.c:27
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:53
AudioDynamicSmoothContext::coeffs
AVFrame * coeffs
Definition: af_adynamicsmooth.c:30
AVFilter
Filter definition.
Definition: avfilter.h:166
avfilter.h
AudioDynamicSmoothContext::basefreq
double basefreq
Definition: af_adynamicsmooth.c:28
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
ffmath.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:397
audio.h
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:193
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_adynamicsmooth.c:33
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:155
AudioDynamicSmoothContext
Definition: af_adynamicsmooth.c:24
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(adynamicsmooth)