FFmpeg
Loading...
Searching...
No Matches
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
33
34static int config_input(AVFilterLink *inlink)
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
46static int filter_frame(AVFilterLink *inlink, AVFrame *in)
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
113AVFILTER_DEFINE_CLASS(adynamicsmooth);
114
115static 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 .p.name = "adynamicsmooth",
126 .p.description = NULL_IF_CONFIG_SMALL("Apply Dynamic Smoothing of input audio."),
127 .p.priv_class = &adynamicsmooth_class,
129 .priv_size = sizeof(AudioDynamicSmoothContext),
130 .uninit = uninit,
134 .process_command = ff_filter_process_command,
135};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static const AVFilterPad inputs[]
Definition af_aap.c:299
static int config_input(AVFilterLink *inlink)
static const AVOption adynamicsmooth_options[]
static int config_input(AVFilterLink *inlink)
const FFFilter ff_af_adynamicsmooth
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
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
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
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:906
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
static __device__ float fabs(float a)
double fmin(double, double)
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
internal math functions header
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#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:204
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition samplefmt.h:67
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition filters.h:257
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
AVOptions.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
int sample_rate
Sample rate of the audio data.
Definition frame.h:635
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:533
AVOption.
Definition opt.h:428
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
const char * g
Definition vf_curves.c:128