FFmpeg
Loading...
Searching...
No Matches
af_aap.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 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
8 * License 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
22#include "libavutil/common.h"
23#include "libavutil/float_dsp.h"
24#include "libavutil/mem.h"
25#include "libavutil/opt.h"
26
27#include "audio.h"
28#include "avfilter.h"
29#include "formats.h"
30#include "filters.h"
31
40
73
74#define OFFSET(x) offsetof(AudioAPContext, x)
75#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
76#define AT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
77
78static const AVOption aap_options[] = {
79 { "order", "set the filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=16}, 1, INT16_MAX, A },
80 { "projection", "set the filter projection", OFFSET(projection), AV_OPT_TYPE_INT, {.i64=2}, 1, 256, A },
81 { "mu", "set the filter mu", OFFSET(mu), AV_OPT_TYPE_FLOAT, {.dbl=0.0001},0,1, AT },
82 { "delta", "set the filter delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0.001},0, 1, AT },
83 { "out_mode", "set output mode", OFFSET(output_mode), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_OMODES-1, AT, .unit = "mode" },
84 { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AT, .unit = "mode" },
85 { "d", "desired", 0, AV_OPT_TYPE_CONST, {.i64=DESIRED_MODE}, 0, 0, AT, .unit = "mode" },
86 { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AT, .unit = "mode" },
87 { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE}, 0, 0, AT, .unit = "mode" },
88 { "e", "error", 0, AV_OPT_TYPE_CONST, {.i64=ERROR_MODE}, 0, 0, AT, .unit = "mode" },
89 { "precision", "set processing precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, A, .unit = "precision" },
90 { "auto", "set auto processing precision", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, .unit = "precision" },
91 { "float", "set single-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, .unit = "precision" },
92 { "double","set double-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, A, .unit = "precision" },
93 { NULL }
94};
95
97
99 AVFilterFormatsConfig **cfg_in,
100 AVFilterFormatsConfig **cfg_out)
101{
102 const AudioAPContext *s = ctx->priv;
103 static const enum AVSampleFormat sample_fmts[3][3] = {
107 };
108 int ret;
109
110 if ((ret = ff_set_sample_formats_from_list2(ctx, cfg_in, cfg_out,
111 sample_fmts[s->precision])) < 0)
112 return ret;
113
114 return 0;
115}
116
118{
119 AudioAPContext *s = ctx->priv;
120 int i, ret, status;
121 int nb_samples;
122 int64_t pts;
123
125
126 nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]),
127 ff_inlink_queued_samples(ctx->inputs[1]));
128 for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) {
129 if (s->frame[i])
130 continue;
131
132 if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
133 ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frame[i]);
134 if (ret < 0)
135 return ret;
136 }
137 }
138
139 if (s->frame[0] && s->frame[1]) {
140 AVFrame *out;
141
142 out = ff_get_audio_buffer(ctx->outputs[0], s->frame[0]->nb_samples);
143 if (!out) {
144 av_frame_free(&s->frame[0]);
145 av_frame_free(&s->frame[1]);
146 return AVERROR(ENOMEM);
147 }
148
149 ff_filter_execute(ctx, s->filter_channels, out, NULL,
150 FFMIN(ctx->outputs[0]->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
151
152 out->pts = s->frame[0]->pts;
153 out->duration = s->frame[0]->duration;
154
155 av_frame_free(&s->frame[0]);
156 av_frame_free(&s->frame[1]);
157
158 ret = ff_filter_frame(ctx->outputs[0], out);
159 if (ret < 0)
160 return ret;
161 }
162
163 if (!nb_samples) {
164 for (i = 0; i < 2; i++) {
165 if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
166 ff_outlink_set_status(ctx->outputs[0], status, pts);
167 return 0;
168 }
169 }
170 }
171
172 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
173 for (i = 0; i < 2; i++) {
174 if (s->frame[i] || ff_inlink_queued_samples(ctx->inputs[i]) > 0)
175 continue;
176 ff_inlink_request_frame(ctx->inputs[i]);
177 return 0;
178 }
179 }
180 return 0;
181}
182
183#define DEPTH 32
184#include "aap_template.c"
185
186#undef DEPTH
187#define DEPTH 64
188#include "aap_template.c"
189
190static int config_output(AVFilterLink *outlink)
191{
192 const int channels = outlink->ch_layout.nb_channels;
193 AVFilterContext *ctx = outlink->src;
194 AudioAPContext *s = ctx->priv;
195
196 s->kernel_size = FFALIGN(s->order, 16);
197
198 if (!s->offset)
199 s->offset = ff_get_audio_buffer(outlink, 3);
200 if (!s->delay)
201 s->delay = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
202 if (!s->dcoeffs)
203 s->dcoeffs = ff_get_audio_buffer(outlink, s->kernel_size);
204 if (!s->coeffs)
205 s->coeffs = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
206 if (!s->e)
207 s->e = ff_get_audio_buffer(outlink, 2 * s->projection);
208 if (!s->p)
209 s->p = ff_get_audio_buffer(outlink, s->projection + 1);
210 if (!s->x)
211 s->x = ff_get_audio_buffer(outlink, 2 * (s->projection + s->order));
212 if (!s->w)
213 s->w = ff_get_audio_buffer(outlink, s->projection);
214 if (!s->tmp)
215 s->tmp = ff_get_audio_buffer(outlink, s->kernel_size);
216 if (!s->tmpm)
217 s->tmpm = ff_get_audio_buffer(outlink, s->projection * s->projection);
218 if (!s->itmpm)
219 s->itmpm = ff_get_audio_buffer(outlink, s->projection * s->projection);
220
221 if (!s->tmpmp)
222 s->tmpmp = av_calloc(s->projection * channels, sizeof(*s->tmpmp));
223 if (!s->itmpmp)
224 s->itmpmp = av_calloc(s->projection * channels, sizeof(*s->itmpmp));
225
226 if (!s->offset || !s->delay || !s->dcoeffs || !s->coeffs || !s->tmpmp || !s->itmpmp ||
227 !s->e || !s->p || !s->x || !s->w || !s->tmp || !s->tmpm || !s->itmpm)
228 return AVERROR(ENOMEM);
229
230 switch (outlink->format) {
232 for (int ch = 0; ch < channels; ch++) {
233 double *itmpm = (double *)s->itmpm->extended_data[ch];
234 double *tmpm = (double *)s->tmpm->extended_data[ch];
235 double **itmpmp = (double **)&s->itmpmp[s->projection * ch];
236 double **tmpmp = (double **)&s->tmpmp[s->projection * ch];
237
238 for (int i = 0; i < s->projection; i++) {
239 itmpmp[i] = &itmpm[i * s->projection];
240 tmpmp[i] = &tmpm[i * s->projection];
241 }
242 }
243
244 s->filter_channels = filter_channels_double;
245 break;
247 for (int ch = 0; ch < channels; ch++) {
248 float *itmpm = (float *)s->itmpm->extended_data[ch];
249 float *tmpm = (float *)s->tmpm->extended_data[ch];
250 float **itmpmp = (float **)&s->itmpmp[s->projection * ch];
251 float **tmpmp = (float **)&s->tmpmp[s->projection * ch];
252
253 for (int i = 0; i < s->projection; i++) {
254 itmpmp[i] = &itmpm[i * s->projection];
255 tmpmp[i] = &tmpm[i * s->projection];
256 }
257 }
258
259 s->filter_channels = filter_channels_float;
260 break;
261 }
262
263 return 0;
264}
265
267{
268 AudioAPContext *s = ctx->priv;
269
270 s->fdsp = avpriv_float_dsp_alloc(0);
271 if (!s->fdsp)
272 return AVERROR(ENOMEM);
273
274 return 0;
275}
276
278{
279 AudioAPContext *s = ctx->priv;
280
281 av_freep(&s->fdsp);
282
283 av_frame_free(&s->offset);
284 av_frame_free(&s->delay);
285 av_frame_free(&s->dcoeffs);
286 av_frame_free(&s->coeffs);
287 av_frame_free(&s->e);
288 av_frame_free(&s->p);
289 av_frame_free(&s->w);
290 av_frame_free(&s->x);
291 av_frame_free(&s->tmp);
292 av_frame_free(&s->tmpm);
293 av_frame_free(&s->itmpm);
294
295 av_freep(&s->tmpmp);
296 av_freep(&s->itmpmp);
297}
298
299static const AVFilterPad inputs[] = {
300 {
301 .name = "input",
302 .type = AVMEDIA_TYPE_AUDIO,
303 },
304 {
305 .name = "desired",
306 .type = AVMEDIA_TYPE_AUDIO,
307 },
308};
309
310static const AVFilterPad outputs[] = {
311 {
312 .name = "default",
313 .type = AVMEDIA_TYPE_AUDIO,
314 .config_props = config_output,
315 },
316};
317
319 .p.name = "aap",
320 .p.description = NULL_IF_CONFIG_SMALL("Apply Affine Projection algorithm to first audio stream."),
321 .p.priv_class = &aap_class,
324 .priv_size = sizeof(AudioAPContext),
325 .init = init,
326 .uninit = uninit,
331 .process_command = ff_filter_process_command,
332};
static enum AVSampleFormat sample_fmts[]
Definition adpcmenc.c:933
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
const FFFilter ff_af_aap
Definition af_aap.c:318
OutModes
Definition af_aap.c:32
@ NOISE_MODE
Definition af_aap.c:36
@ ERROR_MODE
Definition af_aap.c:37
@ IN_MODE
Definition af_aap.c:33
@ DESIRED_MODE
Definition af_aap.c:34
@ NB_OMODES
Definition af_aap.c:38
@ OUT_MODE
Definition af_aap.c:35
static const AVFilterPad inputs[]
Definition af_aap.c:299
static const AVFilterPad outputs[]
Definition af_aap.c:310
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition af_aap.c:98
static int activate(AVFilterContext *ctx)
Definition af_aap.c:117
static av_cold void uninit(AVFilterContext *ctx)
Definition af_aap.c:277
#define OFFSET(x)
Definition af_aap.c:74
static int config_output(AVFilterLink *outlink)
Definition af_aap.c:190
#define AT
Definition af_aap.c:76
static const AVOption aap_options[]
Definition af_aap.c:78
channels
Definition aptx.h:31
#define A(x)
Definition vpx_arith.h:28
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_inlink_check_available_samples(AVFilterLink *link, unsigned min)
Test if enough samples are available on the link.
Definition avfilter.c:1501
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_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
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
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
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 i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
common internal and external API header
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_set_sample_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const enum AVSampleFormat *fmts)
Definition formats.c:1154
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#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
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
@ 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)
static int config_output(AVBitStreamFilterLink *outlink)
const char * arg
Definition jacosubdec.c:65
#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_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition filters.h:652
#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
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition float_dsp.c:135
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
#define FFALIGN(x, a)
Definition macros.h:78
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
AVOptions.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
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
AVOption.
Definition opt.h:428
void ** tmpmp
Definition af_aap.c:64
AVFrame * frame[2]
Definition af_aap.c:67
AVFloatDSPContext * fdsp
Definition af_aap.c:71
int(* filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition af_aap.c:69
AVFrame * x
Definition af_aap.c:57
AVFrame * w
Definition af_aap.c:58
int output_mode
Definition af_aap.c:48
int kernel_size
Definition af_aap.c:51
void ** itmpmp
Definition af_aap.c:65
int projection
Definition af_aap.c:45
AVFrame * coeffs
Definition af_aap.c:54
AVFrame * tmp
Definition af_aap.c:60
AVFrame * p
Definition af_aap.c:56
AVFrame * tmpm
Definition af_aap.c:61
AVFrame * itmpm
Definition af_aap.c:62
AVFrame * dcoeffs
Definition af_aap.c:59
int precision
Definition af_aap.c:49
AVFrame * offset
Definition af_aap.c:52
float delta
Definition af_aap.c:47
AVFrame * delay
Definition af_aap.c:53
AVFrame * e
Definition af_aap.c:55
#define av_freep(p)
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static int64_t pts
float delta