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 "filters.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->ch_layout.nb_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 
125  .name = "adynamicsmooth",
126  .description = NULL_IF_CONFIG_SMALL("Apply Dynamic Smoothing of input audio."),
127  .priv_size = sizeof(AudioDynamicSmoothContext),
128  .priv_class = &adynamicsmooth_class,
129  .uninit = uninit,
134  .process_command = ff_filter_process_command,
135 };
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:98
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:55
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1062
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:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVOption
AVOption.
Definition: opt.h:429
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
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: filters.h:38
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:128
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition: opt.h:267
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
filters.h
adynamicsmooth_options
static const AVOption adynamicsmooth_options[]
Definition: af_adynamicsmooth.c:107
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
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:713
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:34
FILTER_SINGLE_SAMPLEFMT
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition: filters.h:255
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:94
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
AVFrame::sample_rate
int sample_rate
Sample rate of the audio data.
Definition: frame.h:588
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:649
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:901
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:469
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:450
ff_af_adynamicsmooth
const AVFilter ff_af_adynamicsmooth
Definition: af_adynamicsmooth.c:124
AudioDynamicSmoothContext::sensitivity
double sensitivity
Definition: af_adynamicsmooth.c:28
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
AudioDynamicSmoothContext::coeffs
AVFrame * coeffs
Definition: af_adynamicsmooth.c:31
AVFilter
Filter definition.
Definition: avfilter.h:201
avfilter.h
AudioDynamicSmoothContext::basefreq
double basefreq
Definition: af_adynamicsmooth.c:29
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:457
audio.h
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:190
src
#define src
Definition: vp8dsp.c:248
AudioDynamicSmoothContext
Definition: af_adynamicsmooth.c:25
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(adynamicsmooth)