FFmpeg
af_deesser.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Chris Johnson
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
24 #include "libavutil/opt.h"
25 #include "avfilter.h"
26 #include "audio.h"
27 #include "formats.h"
28 
29 typedef struct DeesserChannel {
30  double s1, s2, s3;
31  double m1, m2;
32  double ratioA, ratioB;
34  int flip;
36 
37 typedef struct DeesserContext {
38  const AVClass *class;
39 
40  double intensity;
41  double max;
42  double frequency;
43  int mode;
44 
47 
48 enum OutModes {
53 };
54 
55 #define OFFSET(x) offsetof(DeesserContext, x)
56 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
57 
58 static const AVOption deesser_options[] = {
59  { "i", "set intensity", OFFSET(intensity), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, 0.0, 1.0, A },
60  { "m", "set max deessing", OFFSET(max), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0.0, 1.0, A },
61  { "f", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0.0, 1.0, A },
62  { "s", "set output mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_MODES-1, A, "mode" },
63  { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, A, "mode" },
64  { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, A, "mode" },
65  { "e", "ess", 0, AV_OPT_TYPE_CONST, {.i64=ESS_MODE}, 0, 0, A, "mode" },
66  { NULL }
67 };
68 
69 AVFILTER_DEFINE_CLASS(deesser);
70 
72 {
73  AVFilterContext *ctx = inlink->dst;
74  DeesserContext *s = ctx->priv;
75 
76  s->chan = av_calloc(inlink->channels, sizeof(*s->chan));
77  if (!s->chan)
78  return AVERROR(ENOMEM);
79 
80  for (int i = 0; i < inlink->channels; i++) {
81  DeesserChannel *chan = &s->chan[i];
82 
83  chan->ratioA = chan->ratioB = 1.0;
84  }
85 
86  return 0;
87 }
88 
90 {
91  AVFilterContext *ctx = inlink->dst;
92  AVFilterLink *outlink = ctx->outputs[0];
93  DeesserContext *s = ctx->priv;
94  AVFrame *out;
95 
96  if (av_frame_is_writable(in)) {
97  out = in;
98  } else {
99  out = ff_get_audio_buffer(outlink, in->nb_samples);
100  if (!out) {
101  av_frame_free(&in);
102  return AVERROR(ENOMEM);
103  }
105  }
106 
107  for (int ch = 0; ch < inlink->channels; ch++) {
108  DeesserChannel *dec = &s->chan[ch];
109  double *src = (double *)in->extended_data[ch];
110  double *dst = (double *)out->extended_data[ch];
111  double overallscale = inlink->sample_rate < 44100 ? 44100.0 / inlink->sample_rate : inlink->sample_rate / 44100.0;
112  double intensity = pow(s->intensity, 5) * (8192 / overallscale);
113  double maxdess = 1.0 / pow(10.0, ((s->max - 1.0) * 48.0) / 20);
114  double iirAmount = pow(s->frequency, 2) / overallscale;
115  double offset;
116  double sense;
117  double recovery;
118  double attackspeed;
119 
120  for (int i = 0; i < in->nb_samples; i++) {
121  double sample = src[i];
122 
123  dec->s3 = dec->s2;
124  dec->s2 = dec->s1;
125  dec->s1 = sample;
126  dec->m1 = (dec->s1 - dec->s2) * ((dec->s1 - dec->s2) / 1.3);
127  dec->m2 = (dec->s2 - dec->s3) * ((dec->s1 - dec->s2) / 1.3);
128  sense = (dec->m1 - dec->m2) * ((dec->m1 - dec->m2) / 1.3);
129  attackspeed = 7.0 + sense * 1024;
130 
131  sense = 1.0 + intensity * intensity * sense;
132  sense = FFMIN(sense, intensity);
133  recovery = 1.0 + (0.01 / sense);
134 
135  offset = 1.0 - fabs(sample);
136 
137  if (dec->flip) {
138  dec->iirSampleA = (dec->iirSampleA * (1.0 - (offset * iirAmount))) +
139  (sample * (offset * iirAmount));
140  if (dec->ratioA < sense) {
141  dec->ratioA = ((dec->ratioA * attackspeed) + sense) / (attackspeed + 1.0);
142  } else {
143  dec->ratioA = 1.0 + ((dec->ratioA - 1.0) / recovery);
144  }
145 
146  dec->ratioA = FFMIN(dec->ratioA, maxdess);
147  sample = dec->iirSampleA + ((sample - dec->iirSampleA) / dec->ratioA);
148  } else {
149  dec->iirSampleB = (dec->iirSampleB * (1.0 - (offset * iirAmount))) +
150  (sample * (offset * iirAmount));
151  if (dec->ratioB < sense) {
152  dec->ratioB = ((dec->ratioB * attackspeed) + sense) / (attackspeed + 1.0);
153  } else {
154  dec->ratioB = 1.0 + ((dec->ratioB - 1.0) / recovery);
155  }
156 
157  dec->ratioB = FFMIN(dec->ratioB, maxdess);
158  sample = dec->iirSampleB + ((sample - dec->iirSampleB) / dec->ratioB);
159  }
160 
161  dec->flip = !dec->flip;
162 
163  if (ctx->is_disabled)
164  sample = src[i];
165 
166  switch (s->mode) {
167  case IN_MODE: dst[i] = src[i]; break;
168  case OUT_MODE: dst[i] = sample; break;
169  case ESS_MODE: dst[i] = src[i] - sample; break;
170  }
171  }
172  }
173 
174  if (out != in)
175  av_frame_free(&in);
176 
177  return ff_filter_frame(outlink, out);
178 }
179 
181 {
182  DeesserContext *s = ctx->priv;
183 
184  av_freep(&s->chan);
185 }
186 
187 static const AVFilterPad inputs[] = {
188  {
189  .name = "default",
190  .type = AVMEDIA_TYPE_AUDIO,
191  .filter_frame = filter_frame,
192  .config_props = config_input,
193  },
194 };
195 
196 static const AVFilterPad outputs[] = {
197  {
198  .name = "default",
199  .type = AVMEDIA_TYPE_AUDIO,
200  },
201 };
202 
204  .name = "deesser",
205  .description = NULL_IF_CONFIG_SMALL("Apply de-essing to the audio."),
206  .priv_size = sizeof(DeesserContext),
207  .priv_class = &deesser_class,
208  .uninit = uninit,
213 };
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
DeesserChannel::s1
double s1
Definition: af_deesser.c:30
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:112
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_deesser.c:71
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
AVOption
AVOption.
Definition: opt.h:247
max
#define max(a, b)
Definition: cuda_runtime.h:33
DeesserChannel::ratioB
double ratioB
Definition: af_deesser.c:32
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_deesser.c:180
NB_MODES
@ NB_MODES
Definition: af_deesser.c:52
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:153
DeesserContext::max
double max
Definition: af_deesser.c:41
DeesserChannel::m1
double m1
Definition: af_deesser.c:31
formats.h
IN_MODE
@ IN_MODE
Definition: af_deesser.c:49
DeesserContext::frequency
double frequency
Definition: af_deesser.c:42
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_deesser.c:89
DeesserChannel::s2
double s2
Definition: af_deesser.c:30
DeesserChannel::m2
double m2
Definition: af_deesser.c:31
DeesserContext::mode
int mode
Definition: af_deesser.c:43
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
av_cold
#define av_cold
Definition: attributes.h:90
inputs
static const AVFilterPad inputs[]
Definition: af_deesser.c:187
DeesserChannel::iirSampleB
double iirSampleB
Definition: af_deesser.c:33
DeesserChannel::ratioA
double ratioA
Definition: af_deesser.c:32
s
#define s(width, name)
Definition: cbs_vp9.c:257
outputs
static const AVFilterPad outputs[]
Definition: af_deesser.c:196
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:226
A
#define A
Definition: af_deesser.c:56
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
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
deesser_options
static const AVOption deesser_options[]
Definition: af_deesser.c:58
DeesserChannel::flip
int flip
Definition: af_deesser.c:34
src
#define src
Definition: vp8dsp.c:255
DeesserContext::chan
DeesserChannel * chan
Definition: af_deesser.c:45
OutModes
OutModes
Definition: af_afftdn.c:37
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
sample
#define sample
Definition: flacdsp_template.c:44
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:473
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
DeesserChannel::iirSampleA
double iirSampleA
Definition: af_deesser.c:33
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:369
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:350
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:271
OFFSET
#define OFFSET(x)
Definition: af_deesser.c:55
AVFilter
Filter definition.
Definition: avfilter.h:149
DeesserChannel::s3
double s3
Definition: af_deesser.c:30
ff_af_deesser
const AVFilter ff_af_deesser
Definition: af_deesser.c:203
channel_layout.h
OUT_MODE
@ OUT_MODE
Definition: af_deesser.c:50
mode
mode
Definition: ebur128.h:83
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
avfilter.h
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:70
AVFilterContext
An instance of a filter.
Definition: avfilter.h:386
audio.h
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(deesser)
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
ESS_MODE
@ ESS_MODE
Definition: af_deesser.c:51
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:138
DeesserContext::intensity
double intensity
Definition: af_deesser.c:40
DeesserContext
Definition: af_deesser.c:37
DeesserChannel
Definition: af_deesser.c:29
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233