FFmpeg
Loading...
Searching...
No Matches
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
30#include <float.h>
31
59
60#define OFFSET(x) offsetof(AudioDialogueEnhanceContext, x)
61#define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
62
64 { "original", "set original center factor", OFFSET(original), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
65 { "enhance", "set dialogue enhance factor",OFFSET(enhance), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 3, FLAGS },
66 { "voice", "set voice detection factor", OFFSET(voice), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 2,32, FLAGS },
67 {NULL}
68};
69
70AVFILTER_DEFINE_CLASS(dialoguenhance);
71
73 AVFilterFormatsConfig **cfg_in,
74 AVFilterFormatsConfig **cfg_out)
75{
76 static const enum AVSampleFormat formats[] = {
80 };
81
82 AVFilterChannelLayouts *in_layout = NULL, *out_layout = NULL;
83 int ret;
84
85 ret = ff_set_sample_formats_from_list2(ctx, cfg_in, cfg_out, formats);
86 if (ret < 0)
87 return ret;
88
89 if ((ret = ff_add_channel_layout (&in_layout , &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) < 0 ||
90 (ret = ff_channel_layouts_ref(in_layout, &cfg_in[0]->channel_layouts)) < 0 ||
92 (ret = ff_channel_layouts_ref(out_layout, &cfg_out[0]->channel_layouts)) < 0)
93 return ret;
94
95 return 0;
96}
97
98#define DEPTH 32
100
101#undef DEPTH
102#define DEPTH 64
104
105static int config_input(AVFilterLink *inlink)
106{
107 AVFilterContext *ctx = inlink->dst;
109 int ret;
110
111 s->fft_size = inlink->sample_rate > 100000 ? 8192 : inlink->sample_rate > 50000 ? 4096 : 2048;
112 s->overlap = s->fft_size / 4;
113
114 s->in_frame = ff_get_audio_buffer(inlink, (s->fft_size + 2) * 2);
115 s->center_frame = ff_get_audio_buffer(inlink, (s->fft_size + 2) * 2);
116 s->out_dist_frame = ff_get_audio_buffer(inlink, (s->fft_size + 2) * 2);
117 s->windowed_frame = ff_get_audio_buffer(inlink, (s->fft_size + 2) * 2);
118 s->windowed_out = ff_get_audio_buffer(inlink, (s->fft_size + 2) * 2);
119 s->windowed_prev = ff_get_audio_buffer(inlink, (s->fft_size + 2) * 2);
120 if (!s->in_frame || !s->windowed_out || !s->windowed_prev ||
121 !s->out_dist_frame || !s->windowed_frame || !s->center_frame)
122 return AVERROR(ENOMEM);
123
124 switch (inlink->format) {
126 s->de_stereo = de_stereo_float;
127 ret = de_tx_init_float(ctx);
128 break;
130 s->de_stereo = de_stereo_double;
131 ret = de_tx_init_double(ctx);
132 break;
133 }
134
135 return ret;
136}
137
138static int filter_frame(AVFilterLink *inlink, AVFrame *in)
139{
140 AVFilterContext *ctx = inlink->dst;
141 AVFilterLink *outlink = ctx->outputs[0];
143 AVFrame *out;
144 int ret;
145
146 out = ff_get_audio_buffer(outlink, s->overlap);
147 if (!out) {
148 ret = AVERROR(ENOMEM);
149 goto fail;
150 }
151
152 s->in = in;
153 s->de_stereo(ctx, out);
154
156 out->nb_samples = in->nb_samples;
157 ret = ff_filter_frame(outlink, out);
158fail:
159 av_frame_free(&in);
160 s->in = NULL;
161 return ret < 0 ? ret : 0;
162}
163
165{
166 AVFilterLink *inlink = ctx->inputs[0];
167 AVFilterLink *outlink = ctx->outputs[0];
169 AVFrame *in = NULL;
170 int ret = 0, status;
171 int64_t pts;
172
173 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
174
175 ret = ff_inlink_consume_samples(inlink, s->overlap, s->overlap, &in);
176 if (ret < 0)
177 return ret;
178
179 if (ret > 0) {
180 return filter_frame(inlink, in);
181 } else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
182 ff_outlink_set_status(outlink, status, pts);
183 return 0;
184 } else {
185 if (ff_inlink_queued_samples(inlink) >= s->overlap) {
187 } else if (ff_outlink_frame_wanted(outlink)) {
189 }
190 return 0;
191 }
192}
193
195{
197
198 av_freep(&s->window);
199
200 av_frame_free(&s->in_frame);
201 av_frame_free(&s->center_frame);
202 av_frame_free(&s->out_dist_frame);
203 av_frame_free(&s->windowed_frame);
204 av_frame_free(&s->windowed_out);
205 av_frame_free(&s->windowed_prev);
206
207 av_tx_uninit(&s->tx_ctx[0]);
208 av_tx_uninit(&s->tx_ctx[1]);
209 av_tx_uninit(&s->itx_ctx);
210}
211
212static const AVFilterPad inputs[] = {
213 {
214 .name = "default",
215 .type = AVMEDIA_TYPE_AUDIO,
216 .config_props = config_input,
217 },
218};
219
221 .p.name = "dialoguenhance",
222 .p.description = NULL_IF_CONFIG_SMALL("Audio Dialogue Enhancement."),
223 .p.priv_class = &dialoguenhance_class,
225 .priv_size = sizeof(AudioDialogueEnhanceContext),
226 .uninit = uninit,
230 .activate = activate,
231 .process_command = ff_filter_process_command,
232};
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static const AVFilterPad inputs[]
Definition af_aap.c:299
static int config_input(AVFilterLink *inlink)
static int config_input(AVFilterLink *inlink)
static const AVOption dialoguenhance_options[]
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
const FFFilter ff_af_dialoguenhance
static FILE * out
static AVFormatContext * ctx
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_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition avfilter.c:1467
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition avfilter.c:1690
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
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:1540
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition avfilter.c:229
int ff_inlink_queued_samples(AVFilterLink *link)
Definition avfilter.c:1495
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition avfilter.c:1623
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static const uint16_t channel_layouts[7]
Definition dca_lbr.c:112
int ff_add_channel_layout(AVFilterChannelLayouts **l, const AVChannelLayout *channel_layout)
Definition formats.c:588
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition formats.c:751
int ff_set_sample_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const enum AVSampleFormat *fmts)
Definition formats.c:1154
#define fail
Definition test.h:479
@ 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 AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_SURROUND
#define AVERROR(e)
Definition error.h:45
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
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition samplefmt.h:67
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
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:629
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition filters.h:639
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#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
Memory handling functions.
AVOptions.
formats
Definition signature.h:47
An AVChannelLayout holds information about the channel layout of audio data.
Describe the class of an AVClass context structure.
Definition log.h:76
A list of supported channel layouts.
Definition formats.h:85
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
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
AVOption.
Definition opt.h:428
int(* de_stereo)(AVFilterContext *ctx, AVFrame *out)
#define av_freep(p)
static int64_t pts
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
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