FFmpeg
af_dialoguenhance.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
22 #include "libavutil/mem.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/tx.h"
25 #include "audio.h"
26 #include "avfilter.h"
27 #include "filters.h"
28 #include "formats.h"
29 #include "internal.h"
30 
31 #include <float.h>
32 
34  const AVClass *class;
35 
37 
38  int fft_size;
39  int overlap;
40 
41  void *window;
42  float *window_float;
43  double *window_double;
46 
54 
56 
59 } AudioDialogueEnhanceContext;
60 
61 #define OFFSET(x) offsetof(AudioDialogueEnhanceContext, x)
62 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
63 
64 static const AVOption dialoguenhance_options[] = {
65  { "original", "set original center factor", OFFSET(original), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
66  { "enhance", "set dialogue enhance factor",OFFSET(enhance), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 3, FLAGS },
67  { "voice", "set voice detection factor", OFFSET(voice), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 2,32, FLAGS },
68  {NULL}
69 };
70 
71 AVFILTER_DEFINE_CLASS(dialoguenhance);
72 
74 {
76  AVFilterChannelLayouts *in_layout = NULL, *out_layout = NULL;
77  int ret;
78 
79  if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_FLTP )) < 0 ||
81  (ret = ff_set_common_formats (ctx , formats )) < 0 ||
83  (ret = ff_channel_layouts_ref(in_layout, &ctx->inputs[0]->outcfg.channel_layouts)) < 0 ||
85  (ret = ff_channel_layouts_ref(out_layout, &ctx->outputs[0]->incfg.channel_layouts)) < 0)
86  return ret;
87 
89 }
90 
91 #define DEPTH 32
93 
94 #undef DEPTH
95 #define DEPTH 64
97 
99 {
100  AVFilterContext *ctx = inlink->dst;
101  AudioDialogueEnhanceContext *s = ctx->priv;
102  int ret;
103 
104  s->fft_size = inlink->sample_rate > 100000 ? 8192 : inlink->sample_rate > 50000 ? 4096 : 2048;
105  s->overlap = s->fft_size / 4;
106 
107  s->in_frame = ff_get_audio_buffer(inlink, (s->fft_size + 2) * 2);
108  s->center_frame = ff_get_audio_buffer(inlink, (s->fft_size + 2) * 2);
109  s->out_dist_frame = ff_get_audio_buffer(inlink, (s->fft_size + 2) * 2);
110  s->windowed_frame = ff_get_audio_buffer(inlink, (s->fft_size + 2) * 2);
111  s->windowed_out = ff_get_audio_buffer(inlink, (s->fft_size + 2) * 2);
112  s->windowed_prev = ff_get_audio_buffer(inlink, (s->fft_size + 2) * 2);
113  if (!s->in_frame || !s->windowed_out || !s->windowed_prev ||
114  !s->out_dist_frame || !s->windowed_frame || !s->center_frame)
115  return AVERROR(ENOMEM);
116 
117  switch (inlink->format) {
118  case AV_SAMPLE_FMT_FLTP:
119  s->de_stereo = de_stereo_float;
120  ret = de_tx_init_float(ctx);
121  break;
122  case AV_SAMPLE_FMT_DBLP:
123  s->de_stereo = de_stereo_double;
124  ret = de_tx_init_double(ctx);
125  break;
126  }
127 
128  return ret;
129 }
130 
132 {
133  AVFilterContext *ctx = inlink->dst;
134  AVFilterLink *outlink = ctx->outputs[0];
135  AudioDialogueEnhanceContext *s = ctx->priv;
136  AVFrame *out;
137  int ret;
138 
139  out = ff_get_audio_buffer(outlink, s->overlap);
140  if (!out) {
141  ret = AVERROR(ENOMEM);
142  goto fail;
143  }
144 
145  s->in = in;
146  s->de_stereo(ctx, out);
147 
149  out->nb_samples = in->nb_samples;
150  ret = ff_filter_frame(outlink, out);
151 fail:
152  av_frame_free(&in);
153  s->in = NULL;
154  return ret < 0 ? ret : 0;
155 }
156 
158 {
159  AVFilterLink *inlink = ctx->inputs[0];
160  AVFilterLink *outlink = ctx->outputs[0];
161  AudioDialogueEnhanceContext *s = ctx->priv;
162  AVFrame *in = NULL;
163  int ret = 0, status;
164  int64_t pts;
165 
167 
168  ret = ff_inlink_consume_samples(inlink, s->overlap, s->overlap, &in);
169  if (ret < 0)
170  return ret;
171 
172  if (ret > 0) {
173  return filter_frame(inlink, in);
174  } else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
175  ff_outlink_set_status(outlink, status, pts);
176  return 0;
177  } else {
178  if (ff_inlink_queued_samples(inlink) >= s->overlap) {
180  } else if (ff_outlink_frame_wanted(outlink)) {
182  }
183  return 0;
184  }
185 }
186 
188 {
189  AudioDialogueEnhanceContext *s = ctx->priv;
190 
191  av_freep(&s->window);
192 
193  av_frame_free(&s->in_frame);
194  av_frame_free(&s->center_frame);
195  av_frame_free(&s->out_dist_frame);
196  av_frame_free(&s->windowed_frame);
197  av_frame_free(&s->windowed_out);
198  av_frame_free(&s->windowed_prev);
199 
200  av_tx_uninit(&s->tx_ctx[0]);
201  av_tx_uninit(&s->tx_ctx[1]);
202  av_tx_uninit(&s->itx_ctx);
203 }
204 
205 static const AVFilterPad inputs[] = {
206  {
207  .name = "default",
208  .type = AVMEDIA_TYPE_AUDIO,
209  .config_props = config_input,
210  },
211 };
212 
214  .name = "dialoguenhance",
215  .description = NULL_IF_CONFIG_SMALL("Audio Dialogue Enhancement."),
216  .priv_size = sizeof(AudioDialogueEnhanceContext),
217  .priv_class = &dialoguenhance_class,
218  .uninit = uninit,
223  .activate = activate,
224  .process_command = ff_filter_process_command,
225 };
formats
formats
Definition: signature.h:48
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:97
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
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
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:379
inputs
static const AVFilterPad inputs[]
Definition: af_dialoguenhance.c:205
AudioDialogueEnhancementContext::in_frame
AVFrame * in_frame
Definition: af_dialoguenhance.c:48
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1015
ff_channel_layouts_ref
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:674
AVTXContext
Definition: tx_priv.h:235
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:160
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:375
AudioDialogueEnhancementContext::de_stereo
int(* de_stereo)(AVFilterContext *ctx, AVFrame *out)
Definition: af_dialoguenhance.c:55
AVOption
AVOption.
Definition: opt.h:346
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:159
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_dialoguenhance.c:131
ff_set_common_all_samplerates
int ff_set_common_all_samplerates(AVFilterContext *ctx)
Equivalent to ff_set_common_samplerates(ctx, ff_all_samplerates())
Definition: formats.c:822
float.h
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
AudioDialogueEnhancementContext::overlap
int overlap
Definition: af_dialoguenhance.c:39
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_dialoguenhance.c:187
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
AudioDialogueEnhancementContext::windowed_prev
AVFrame * windowed_prev
Definition: af_dialoguenhance.c:52
AudioDialogueEnhancementContext::voice
double voice
Definition: af_dialoguenhance.c:36
fail
#define fail()
Definition: checkasm.h:179
AudioDialogueEnhancementContext::prev_vad_float
float prev_vad_float
Definition: af_dialoguenhance.c:44
ff_af_dialoguenhance
const AVFilter ff_af_dialoguenhance
Definition: af_dialoguenhance.c:213
AudioDialogueEnhancementContext::prev_vad_double
double prev_vad_double
Definition: af_dialoguenhance.c:45
pts
static int64_t pts
Definition: transcode_aac.c:644
AudioDialogueEnhancementContext::windowed_out
AVFrame * windowed_out
Definition: af_dialoguenhance.c:51
AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_SURROUND
Definition: channel_layout.h:382
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
AudioDialogueEnhancementContext::tx_ctx
AVTXContext * tx_ctx[2]
Definition: af_dialoguenhance.c:57
AudioDialogueEnhancementContext::windowed_frame
AVFrame * windowed_frame
Definition: af_dialoguenhance.c:50
AudioDialogueEnhancementContext::itx_fn
av_tx_fn itx_fn
Definition: af_dialoguenhance.c:58
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:868
av_tx_fn
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition: tx.h:151
ff_outlink_set_status
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:189
ff_inlink_request_frame
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1568
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:237
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
filters.h
AudioDialogueEnhancementContext::out_dist_frame
AVFrame * out_dist_frame
Definition: af_dialoguenhance.c:49
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FLAGS
#define FLAGS
Definition: af_dialoguenhance.c:62
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
AudioDialogueEnhancementContext::original
double original
Definition: af_dialoguenhance.c:36
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
ff_inlink_consume_samples
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
Definition: avfilter.c:1462
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:709
OFFSET
#define OFFSET(x)
Definition: af_dialoguenhance.c:61
ff_add_format
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:505
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:33
ff_add_channel_layout
int ff_add_channel_layout(AVFilterChannelLayouts **l, const AVChannelLayout *channel_layout)
Definition: formats.c:522
ff_inlink_acknowledge_status
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition: avfilter.c:1389
AudioDialogueEnhancementContext::window
void * window
Definition: af_dialoguenhance.c:41
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
AudioDialogueEnhancementContext::fft_size
int fft_size
Definition: af_dialoguenhance.c:38
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
dialoguenhance_template.c
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:887
AudioDialogueEnhancementContext::window_float
float * window_float
Definition: af_dialoguenhance.c:42
av_tx_uninit
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition: tx.c:295
AudioDialogueEnhancementContext
Definition: af_dialoguenhance.c:33
internal.h
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:455
dialoguenhance_options
static const AVOption dialoguenhance_options[]
Definition: af_dialoguenhance.c:64
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
ff_inlink_queued_samples
int ff_inlink_queued_samples(AVFilterLink *link)
Definition: avfilter.c:1417
AudioDialogueEnhancementContext::in
AVFrame * in
Definition: af_dialoguenhance.c:47
AudioDialogueEnhancementContext::tx_fn
av_tx_fn tx_fn
Definition: af_dialoguenhance.c:58
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
status
ov_status_e status
Definition: dnn_backend_openvino.c:121
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_dialoguenhance.c:98
channel_layout.h
avfilter.h
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_dialoguenhance.c:73
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
activate
static int activate(AVFilterContext *ctx)
Definition: af_dialoguenhance.c:157
AudioDialogueEnhancementContext::window_double
double * window_double
Definition: af_dialoguenhance.c:43
mem.h
audio.h
AudioDialogueEnhancementContext::itx_ctx
AVTXContext * itx_ctx
Definition: af_dialoguenhance.c:57
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h: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:155
ff_outlink_frame_wanted
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
AudioDialogueEnhancementContext::center_frame
AVFrame * center_frame
Definition: af_dialoguenhance.c:53
int
int
Definition: ffmpeg_filter.c:424
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(dialoguenhance)
AudioDialogueEnhancementContext::enhance
double enhance
Definition: af_dialoguenhance.c:36
ff_filter_set_ready
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition: avfilter.c:235
tx.h