| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2019 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 | |||
| 21 | #include "libavutil/channel_layout.h" | ||
| 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 "filters.h" | ||
| 30 | #include "formats.h" | ||
| 31 | |||
| 32 | enum OutModes { | ||
| 33 | IN_MODE, | ||
| 34 | DESIRED_MODE, | ||
| 35 | OUT_MODE, | ||
| 36 | NOISE_MODE, | ||
| 37 | ERROR_MODE, | ||
| 38 | NB_OMODES | ||
| 39 | }; | ||
| 40 | |||
| 41 | typedef struct AudioNLMSContext { | ||
| 42 | const AVClass *class; | ||
| 43 | |||
| 44 | int order; | ||
| 45 | float mu; | ||
| 46 | float eps; | ||
| 47 | float leakage; | ||
| 48 | int output_mode; | ||
| 49 | int precision; | ||
| 50 | |||
| 51 | int kernel_size; | ||
| 52 | AVFrame *offset; | ||
| 53 | AVFrame *delay; | ||
| 54 | AVFrame *coeffs; | ||
| 55 | AVFrame *tmp; | ||
| 56 | |||
| 57 | AVFrame *frame[2]; | ||
| 58 | |||
| 59 | int anlmf; | ||
| 60 | |||
| 61 | int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); | ||
| 62 | |||
| 63 | AVFloatDSPContext *fdsp; | ||
| 64 | } AudioNLMSContext; | ||
| 65 | |||
| 66 | #define OFFSET(x) offsetof(AudioNLMSContext, x) | ||
| 67 | #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM | ||
| 68 | #define AT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM | ||
| 69 | |||
| 70 | static const AVOption anlms_options[] = { | ||
| 71 | { "order", "set the filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=256}, 1, INT16_MAX, A }, | ||
| 72 | { "mu", "set the filter mu", OFFSET(mu), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 2, AT }, | ||
| 73 | { "eps", "set the filter eps", OFFSET(eps), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AT }, | ||
| 74 | { "leakage", "set the filter leakage", OFFSET(leakage), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, AT }, | ||
| 75 | { "out_mode", "set output mode", OFFSET(output_mode), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_OMODES-1, AT, .unit = "mode" }, | ||
| 76 | { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AT, .unit = "mode" }, | ||
| 77 | { "d", "desired", 0, AV_OPT_TYPE_CONST, {.i64=DESIRED_MODE}, 0, 0, AT, .unit = "mode" }, | ||
| 78 | { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AT, .unit = "mode" }, | ||
| 79 | { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE}, 0, 0, AT, .unit = "mode" }, | ||
| 80 | { "e", "error", 0, AV_OPT_TYPE_CONST, {.i64=ERROR_MODE}, 0, 0, AT, .unit = "mode" }, | ||
| 81 | { "precision", "set processing precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, A, .unit = "precision" }, | ||
| 82 | { "auto", "set auto processing precision", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, .unit = "precision" }, | ||
| 83 | { "float", "set single-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, .unit = "precision" }, | ||
| 84 | { "double","set double-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, A, .unit = "precision" }, | ||
| 85 | { NULL } | ||
| 86 | }; | ||
| 87 | |||
| 88 | AVFILTER_DEFINE_CLASS_EXT(anlms, "anlm(f|s)", anlms_options); | ||
| 89 | |||
| 90 | ✗ | static int query_formats(const AVFilterContext *ctx, | |
| 91 | AVFilterFormatsConfig **cfg_in, | ||
| 92 | AVFilterFormatsConfig **cfg_out) | ||
| 93 | { | ||
| 94 | ✗ | const AudioNLMSContext *s = ctx->priv; | |
| 95 | static const enum AVSampleFormat sample_fmts[3][3] = { | ||
| 96 | { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE }, | ||
| 97 | { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE }, | ||
| 98 | { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE }, | ||
| 99 | }; | ||
| 100 | int ret; | ||
| 101 | |||
| 102 | ✗ | if ((ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, | |
| 103 | ✗ | sample_fmts[s->precision])) < 0) | |
| 104 | ✗ | return ret; | |
| 105 | |||
| 106 | ✗ | return 0; | |
| 107 | } | ||
| 108 | |||
| 109 | ✗ | static int activate(AVFilterContext *ctx) | |
| 110 | { | ||
| 111 | ✗ | AudioNLMSContext *s = ctx->priv; | |
| 112 | int i, ret, status; | ||
| 113 | int nb_samples; | ||
| 114 | int64_t pts; | ||
| 115 | |||
| 116 | ✗ | FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx); | |
| 117 | |||
| 118 | ✗ | nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]), | |
| 119 | ff_inlink_queued_samples(ctx->inputs[1])); | ||
| 120 | ✗ | for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) { | |
| 121 | ✗ | if (s->frame[i]) | |
| 122 | ✗ | continue; | |
| 123 | |||
| 124 | ✗ | if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) { | |
| 125 | ✗ | ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frame[i]); | |
| 126 | ✗ | if (ret < 0) | |
| 127 | ✗ | return ret; | |
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | ✗ | if (s->frame[0] && s->frame[1]) { | |
| 132 | AVFrame *out; | ||
| 133 | |||
| 134 | ✗ | out = ff_get_audio_buffer(ctx->outputs[0], s->frame[0]->nb_samples); | |
| 135 | ✗ | if (!out) { | |
| 136 | ✗ | av_frame_free(&s->frame[0]); | |
| 137 | ✗ | av_frame_free(&s->frame[1]); | |
| 138 | ✗ | return AVERROR(ENOMEM); | |
| 139 | } | ||
| 140 | |||
| 141 | ✗ | ff_filter_execute(ctx, s->filter_channels, out, NULL, | |
| 142 | ✗ | FFMIN(ctx->outputs[0]->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx))); | |
| 143 | |||
| 144 | ✗ | out->pts = s->frame[0]->pts; | |
| 145 | ✗ | out->duration = s->frame[0]->duration; | |
| 146 | |||
| 147 | ✗ | av_frame_free(&s->frame[0]); | |
| 148 | ✗ | av_frame_free(&s->frame[1]); | |
| 149 | |||
| 150 | ✗ | ret = ff_filter_frame(ctx->outputs[0], out); | |
| 151 | ✗ | if (ret < 0) | |
| 152 | ✗ | return ret; | |
| 153 | } | ||
| 154 | |||
| 155 | ✗ | if (!nb_samples) { | |
| 156 | ✗ | for (i = 0; i < 2; i++) { | |
| 157 | ✗ | if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) { | |
| 158 | ✗ | ff_outlink_set_status(ctx->outputs[0], status, pts); | |
| 159 | ✗ | return 0; | |
| 160 | } | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | ✗ | if (ff_outlink_frame_wanted(ctx->outputs[0])) { | |
| 165 | ✗ | for (i = 0; i < 2; i++) { | |
| 166 | ✗ | if (s->frame[i] || ff_inlink_queued_samples(ctx->inputs[i]) > 0) | |
| 167 | ✗ | continue; | |
| 168 | ✗ | ff_inlink_request_frame(ctx->inputs[i]); | |
| 169 | ✗ | return 0; | |
| 170 | } | ||
| 171 | } | ||
| 172 | ✗ | return 0; | |
| 173 | } | ||
| 174 | |||
| 175 | #define DEPTH 32 | ||
| 176 | #include "anlms_template.c" | ||
| 177 | |||
| 178 | #undef DEPTH | ||
| 179 | #define DEPTH 64 | ||
| 180 | #include "anlms_template.c" | ||
| 181 | |||
| 182 | ✗ | static int config_output(AVFilterLink *outlink) | |
| 183 | { | ||
| 184 | ✗ | AVFilterContext *ctx = outlink->src; | |
| 185 | ✗ | AudioNLMSContext *s = ctx->priv; | |
| 186 | |||
| 187 | ✗ | s->anlmf = !strcmp(ctx->filter->name, "anlmf"); | |
| 188 | ✗ | s->kernel_size = FFALIGN(s->order, 16); | |
| 189 | |||
| 190 | ✗ | if (!s->offset) | |
| 191 | ✗ | s->offset = ff_get_audio_buffer(outlink, 1); | |
| 192 | ✗ | if (!s->delay) | |
| 193 | ✗ | s->delay = ff_get_audio_buffer(outlink, 2 * s->kernel_size); | |
| 194 | ✗ | if (!s->coeffs) | |
| 195 | ✗ | s->coeffs = ff_get_audio_buffer(outlink, 2 * s->kernel_size); | |
| 196 | ✗ | if (!s->tmp) | |
| 197 | ✗ | s->tmp = ff_get_audio_buffer(outlink, s->kernel_size); | |
| 198 | ✗ | if (!s->delay || !s->coeffs || !s->offset || !s->tmp) | |
| 199 | ✗ | return AVERROR(ENOMEM); | |
| 200 | |||
| 201 | ✗ | switch (outlink->format) { | |
| 202 | ✗ | case AV_SAMPLE_FMT_DBLP: | |
| 203 | ✗ | s->filter_channels = filter_channels_double; | |
| 204 | ✗ | break; | |
| 205 | ✗ | case AV_SAMPLE_FMT_FLTP: | |
| 206 | ✗ | s->filter_channels = filter_channels_float; | |
| 207 | ✗ | break; | |
| 208 | } | ||
| 209 | |||
| 210 | ✗ | return 0; | |
| 211 | } | ||
| 212 | |||
| 213 | ✗ | static av_cold int init(AVFilterContext *ctx) | |
| 214 | { | ||
| 215 | ✗ | AudioNLMSContext *s = ctx->priv; | |
| 216 | |||
| 217 | ✗ | s->fdsp = avpriv_float_dsp_alloc(0); | |
| 218 | ✗ | if (!s->fdsp) | |
| 219 | ✗ | return AVERROR(ENOMEM); | |
| 220 | |||
| 221 | ✗ | return 0; | |
| 222 | } | ||
| 223 | |||
| 224 | ✗ | static av_cold void uninit(AVFilterContext *ctx) | |
| 225 | { | ||
| 226 | ✗ | AudioNLMSContext *s = ctx->priv; | |
| 227 | |||
| 228 | ✗ | av_freep(&s->fdsp); | |
| 229 | ✗ | av_frame_free(&s->delay); | |
| 230 | ✗ | av_frame_free(&s->coeffs); | |
| 231 | ✗ | av_frame_free(&s->offset); | |
| 232 | ✗ | av_frame_free(&s->tmp); | |
| 233 | ✗ | } | |
| 234 | |||
| 235 | static const AVFilterPad inputs[] = { | ||
| 236 | { | ||
| 237 | .name = "input", | ||
| 238 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 239 | }, | ||
| 240 | { | ||
| 241 | .name = "desired", | ||
| 242 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 243 | }, | ||
| 244 | }; | ||
| 245 | |||
| 246 | static const AVFilterPad outputs[] = { | ||
| 247 | { | ||
| 248 | .name = "default", | ||
| 249 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 250 | .config_props = config_output, | ||
| 251 | }, | ||
| 252 | }; | ||
| 253 | |||
| 254 | const FFFilter ff_af_anlms = { | ||
| 255 | .p.name = "anlms", | ||
| 256 | .p.description = NULL_IF_CONFIG_SMALL("Apply Normalized Least-Mean-Squares algorithm to first audio stream."), | ||
| 257 | .p.priv_class = &anlms_class, | ||
| 258 | .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | | ||
| 259 | AVFILTER_FLAG_SLICE_THREADS, | ||
| 260 | .priv_size = sizeof(AudioNLMSContext), | ||
| 261 | .init = init, | ||
| 262 | .uninit = uninit, | ||
| 263 | .activate = activate, | ||
| 264 | FILTER_INPUTS(inputs), | ||
| 265 | FILTER_OUTPUTS(outputs), | ||
| 266 | FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_FLTP), | ||
| 267 | .process_command = ff_filter_process_command, | ||
| 268 | }; | ||
| 269 | |||
| 270 | const FFFilter ff_af_anlmf = { | ||
| 271 | .p.name = "anlmf", | ||
| 272 | .p.description = NULL_IF_CONFIG_SMALL("Apply Normalized Least-Mean-Fourth algorithm to first audio stream."), | ||
| 273 | .p.priv_class = &anlms_class, | ||
| 274 | .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | | ||
| 275 | AVFILTER_FLAG_SLICE_THREADS, | ||
| 276 | .priv_size = sizeof(AudioNLMSContext), | ||
| 277 | .init = init, | ||
| 278 | .uninit = uninit, | ||
| 279 | .activate = activate, | ||
| 280 | FILTER_INPUTS(inputs), | ||
| 281 | FILTER_OUTPUTS(outputs), | ||
| 282 | FILTER_QUERY_FUNC2(query_formats), | ||
| 283 | .process_command = ff_filter_process_command, | ||
| 284 | }; | ||
| 285 |