| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2011 Stefano Sabatini | ||
| 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 | /** | ||
| 22 | * @file | ||
| 23 | * eval audio source | ||
| 24 | */ | ||
| 25 | |||
| 26 | #include "config_components.h" | ||
| 27 | |||
| 28 | #include "libavutil/avstring.h" | ||
| 29 | #include "libavutil/channel_layout.h" | ||
| 30 | #include "libavutil/eval.h" | ||
| 31 | #include "libavutil/mem.h" | ||
| 32 | #include "libavutil/opt.h" | ||
| 33 | #include "avfilter.h" | ||
| 34 | #include "audio.h" | ||
| 35 | #include "filters.h" | ||
| 36 | #include "formats.h" | ||
| 37 | |||
| 38 | static const char * const var_names[] = { | ||
| 39 | "ch", ///< the value of the current channel | ||
| 40 | "n", ///< number of frame | ||
| 41 | "nb_in_channels", | ||
| 42 | "nb_out_channels", | ||
| 43 | "t", ///< timestamp expressed in seconds | ||
| 44 | "s", ///< sample rate | ||
| 45 | NULL | ||
| 46 | }; | ||
| 47 | |||
| 48 | enum var_name { | ||
| 49 | VAR_CH, | ||
| 50 | VAR_N, | ||
| 51 | VAR_NB_IN_CHANNELS, | ||
| 52 | VAR_NB_OUT_CHANNELS, | ||
| 53 | VAR_T, | ||
| 54 | VAR_S, | ||
| 55 | VAR_VARS_NB | ||
| 56 | }; | ||
| 57 | |||
| 58 | typedef struct EvalContext { | ||
| 59 | const AVClass *class; | ||
| 60 | char *sample_rate_str; | ||
| 61 | int sample_rate; | ||
| 62 | AVChannelLayout chlayout; | ||
| 63 | char *chlayout_str; | ||
| 64 | int nb_channels; ///< number of output channels | ||
| 65 | int nb_in_channels; ///< number of input channels | ||
| 66 | int same_chlayout; ///< set output as input channel layout | ||
| 67 | int64_t pts; | ||
| 68 | AVExpr **expr; | ||
| 69 | char *exprs; | ||
| 70 | int nb_samples; ///< number of samples per requested frame | ||
| 71 | int64_t duration; | ||
| 72 | uint64_t n; | ||
| 73 | double var_values[VAR_VARS_NB]; | ||
| 74 | double *channel_values; | ||
| 75 | } EvalContext; | ||
| 76 | |||
| 77 | 264600 | static double val(void *priv, double ch) | |
| 78 | { | ||
| 79 | 264600 | EvalContext *eval = priv; | |
| 80 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 264600 times.
|
264600 | return eval->channel_values[FFMIN((int)ch, eval->nb_in_channels-1)]; |
| 81 | } | ||
| 82 | |||
| 83 | static double (* const aeval_func1[])(void *, double) = { val, NULL }; | ||
| 84 | static const char * const aeval_func1_names[] = { "val", NULL }; | ||
| 85 | |||
| 86 | #define OFFSET(x) offsetof(EvalContext, x) | ||
| 87 | #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM | ||
| 88 | |||
| 89 | static const AVOption aevalsrc_options[]= { | ||
| 90 | { "exprs", "set the '|'-separated list of channels expressions", OFFSET(exprs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS }, | ||
| 91 | { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS }, | ||
| 92 | { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS }, | ||
| 93 | { "sample_rate", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS }, | ||
| 94 | { "s", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS }, | ||
| 95 | { "duration", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS }, | ||
| 96 | { "d", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS }, | ||
| 97 | { "channel_layout", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, | ||
| 98 | { "c", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, | ||
| 99 | { NULL } | ||
| 100 | }; | ||
| 101 | |||
| 102 | AVFILTER_DEFINE_CLASS(aevalsrc); | ||
| 103 | |||
| 104 | 18 | static int parse_channel_expressions(AVFilterContext *ctx, | |
| 105 | int expected_nb_channels) | ||
| 106 | { | ||
| 107 | 18 | EvalContext *eval = ctx->priv; | |
| 108 | 18 | char *args1 = av_strdup(eval->exprs); | |
| 109 | 18 | char *expr, *last_expr = NULL, *buf; | |
| 110 | 18 | double (* const *func1)(void *, double) = NULL; | |
| 111 | 18 | const char * const *func1_names = NULL; | |
| 112 | 18 | int i, ret = 0; | |
| 113 | |||
| 114 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if (!args1) |
| 115 | ✗ | return AVERROR(ENOMEM); | |
| 116 | |||
| 117 |
2/4✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
|
18 | if (!eval->exprs || !*eval->exprs) { |
| 118 | ✗ | av_log(ctx, AV_LOG_ERROR, "Channels expressions list is empty\n"); | |
| 119 | ✗ | ret = AVERROR(EINVAL); | |
| 120 | ✗ | goto end; | |
| 121 | } | ||
| 122 | |||
| 123 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
|
18 | if (!strcmp(ctx->filter->name, "aeval")) { |
| 124 | 2 | func1 = aeval_func1; | |
| 125 | 2 | func1_names = aeval_func1_names; | |
| 126 | } | ||
| 127 | |||
| 128 | #define ADD_EXPRESSION(expr_) do { \ | ||
| 129 | ret = av_dynarray_add_nofree(&eval->expr, \ | ||
| 130 | &eval->nb_channels, NULL); \ | ||
| 131 | if (ret < 0) \ | ||
| 132 | goto end; \ | ||
| 133 | eval->expr[eval->nb_channels-1] = NULL; \ | ||
| 134 | ret = av_expr_parse(&eval->expr[eval->nb_channels - 1], expr_, \ | ||
| 135 | var_names, func1_names, func1, \ | ||
| 136 | NULL, NULL, 0, ctx); \ | ||
| 137 | if (ret < 0) \ | ||
| 138 | goto end; \ | ||
| 139 | } while (0) | ||
| 140 | |||
| 141 | /* reset expressions */ | ||
| 142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | for (i = 0; i < eval->nb_channels; i++) { |
| 143 | ✗ | av_expr_free(eval->expr[i]); | |
| 144 | ✗ | eval->expr[i] = NULL; | |
| 145 | } | ||
| 146 | 18 | av_freep(&eval->expr); | |
| 147 | 18 | eval->nb_channels = 0; | |
| 148 | |||
| 149 | 18 | buf = args1; | |
| 150 |
2/2✓ Branch 1 taken 18 times.
✓ Branch 2 taken 18 times.
|
36 | while (expr = av_strtok(buf, "|", &buf)) { |
| 151 |
2/4✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 18 times.
|
18 | ADD_EXPRESSION(expr); |
| 152 | 18 | last_expr = expr; | |
| 153 | } | ||
| 154 | |||
| 155 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if (expected_nb_channels > eval->nb_channels) |
| 156 | ✗ | for (i = eval->nb_channels; i < expected_nb_channels; i++) | |
| 157 | ✗ | ADD_EXPRESSION(last_expr); | |
| 158 | |||
| 159 |
1/4✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
18 | if (expected_nb_channels > 0 && eval->nb_channels != expected_nb_channels) { |
| 160 | ✗ | av_log(ctx, AV_LOG_ERROR, | |
| 161 | "Mismatch between the specified number of channel expressions '%d' " | ||
| 162 | "and the number of expected output channels '%d' for the specified channel layout\n", | ||
| 163 | eval->nb_channels, expected_nb_channels); | ||
| 164 | ✗ | ret = AVERROR(EINVAL); | |
| 165 | ✗ | goto end; | |
| 166 | } | ||
| 167 | |||
| 168 | 18 | end: | |
| 169 | 18 | av_free(args1); | |
| 170 | 18 | return ret; | |
| 171 | } | ||
| 172 | |||
| 173 | 18 | static av_cold int init(AVFilterContext *ctx) | |
| 174 | { | ||
| 175 | 18 | EvalContext *eval = ctx->priv; | |
| 176 | 18 | int ret = 0; | |
| 177 | |||
| 178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if (eval->chlayout_str) { |
| 179 | ✗ | if (!strcmp(eval->chlayout_str, "same") && !strcmp(ctx->filter->name, "aeval")) { | |
| 180 | ✗ | eval->same_chlayout = 1; | |
| 181 | } else { | ||
| 182 | ✗ | ret = ff_parse_channel_layout(&eval->chlayout, NULL, eval->chlayout_str, ctx); | |
| 183 | ✗ | if (ret < 0) | |
| 184 | ✗ | return ret; | |
| 185 | |||
| 186 | ✗ | ret = parse_channel_expressions(ctx, eval->chlayout.nb_channels); | |
| 187 | ✗ | if (ret < 0) | |
| 188 | ✗ | return ret; | |
| 189 | } | ||
| 190 | } else { | ||
| 191 | /* guess channel layout from nb expressions/channels */ | ||
| 192 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
|
18 | if ((ret = parse_channel_expressions(ctx, -1)) < 0) |
| 193 | ✗ | return ret; | |
| 194 | |||
| 195 | 18 | av_channel_layout_default(&eval->chlayout, eval->nb_channels); | |
| 196 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if (eval->nb_channels <= 0) { |
| 197 | ✗ | av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n", | |
| 198 | eval->nb_channels); | ||
| 199 | ✗ | return AVERROR(EINVAL); | |
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
|
18 | if (eval->sample_rate_str) |
| 204 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
|
16 | if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx))) |
| 205 | ✗ | return ret; | |
| 206 | 18 | eval->n = 0; | |
| 207 | |||
| 208 | 18 | return ret; | |
| 209 | } | ||
| 210 | |||
| 211 | 18 | static av_cold void uninit(AVFilterContext *ctx) | |
| 212 | { | ||
| 213 | 18 | EvalContext *eval = ctx->priv; | |
| 214 | int i; | ||
| 215 | |||
| 216 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
|
36 | for (i = 0; i < eval->nb_channels; i++) { |
| 217 | 18 | av_expr_free(eval->expr[i]); | |
| 218 | 18 | eval->expr[i] = NULL; | |
| 219 | } | ||
| 220 | 18 | av_freep(&eval->expr); | |
| 221 | 18 | av_freep(&eval->channel_values); | |
| 222 | 18 | av_channel_layout_uninit(&eval->chlayout); | |
| 223 | 18 | } | |
| 224 | |||
| 225 | 15 | static int config_props(AVFilterLink *outlink) | |
| 226 | { | ||
| 227 | 15 | EvalContext *eval = outlink->src->priv; | |
| 228 | char buf[128]; | ||
| 229 | |||
| 230 | 15 | outlink->time_base = (AVRational){1, eval->sample_rate}; | |
| 231 | 15 | outlink->sample_rate = eval->sample_rate; | |
| 232 | |||
| 233 | 15 | eval->var_values[VAR_S] = eval->sample_rate; | |
| 234 | 15 | eval->var_values[VAR_NB_IN_CHANNELS] = NAN; | |
| 235 | 15 | eval->var_values[VAR_NB_OUT_CHANNELS] = outlink->ch_layout.nb_channels; | |
| 236 | |||
| 237 | 15 | av_channel_layout_describe(&eval->chlayout, buf, sizeof(buf)); | |
| 238 | |||
| 239 | 15 | av_log(outlink->src, AV_LOG_VERBOSE, | |
| 240 | "sample_rate:%d chlayout:%s duration:%"PRId64"\n", | ||
| 241 | eval->sample_rate, buf, eval->duration); | ||
| 242 | |||
| 243 | 15 | return 0; | |
| 244 | } | ||
| 245 | |||
| 246 | 15 | static int query_formats(const AVFilterContext *ctx, | |
| 247 | AVFilterFormatsConfig **cfg_in, | ||
| 248 | AVFilterFormatsConfig **cfg_out) | ||
| 249 | { | ||
| 250 | 15 | const EvalContext *eval = ctx->priv; | |
| 251 | static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE }; | ||
| 252 |
1/2✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
|
15 | AVChannelLayout chlayouts[] = { eval->chlayout.nb_channels ? eval->chlayout : FF_COUNT2LAYOUT(eval->nb_channels), { 0 } }; |
| 253 | 15 | int sample_rates[] = { eval->sample_rate, -1 }; | |
| 254 | int ret; | ||
| 255 | |||
| 256 | 15 | ret = ff_set_sample_formats_from_list2(ctx, cfg_in, cfg_out, sample_fmts); | |
| 257 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (ret < 0) |
| 258 | ✗ | return ret; | |
| 259 | |||
| 260 | 15 | ret = ff_set_common_channel_layouts_from_list2(ctx, cfg_in, cfg_out, chlayouts); | |
| 261 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (ret < 0) |
| 262 | ✗ | return ret; | |
| 263 | |||
| 264 | 15 | return ff_set_common_samplerates_from_list2(ctx, cfg_in, cfg_out, sample_rates); | |
| 265 | } | ||
| 266 | |||
| 267 | 9390 | static int activate(AVFilterContext *ctx) | |
| 268 | { | ||
| 269 | 9390 | AVFilterLink *outlink = ctx->outputs[0]; | |
| 270 | 9390 | EvalContext *eval = outlink->src->priv; | |
| 271 | AVFrame *samplesref; | ||
| 272 | int i, j; | ||
| 273 | 9390 | int64_t t = av_rescale(eval->n, AV_TIME_BASE, eval->sample_rate); | |
| 274 | int nb_samples; | ||
| 275 | |||
| 276 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9390 times.
|
9390 | if (!ff_outlink_frame_wanted(outlink)) |
| 277 | ✗ | return FFERROR_NOT_READY; | |
| 278 | |||
| 279 |
3/4✓ Branch 0 taken 9390 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 9375 times.
|
9390 | if (eval->duration >= 0 && t >= eval->duration) { |
| 280 | 15 | ff_outlink_set_status(outlink, AVERROR_EOF, eval->pts); | |
| 281 | 15 | return 0; | |
| 282 | } | ||
| 283 | |||
| 284 |
1/2✓ Branch 0 taken 9375 times.
✗ Branch 1 not taken.
|
9375 | if (eval->duration >= 0) { |
| 285 | 9375 | nb_samples = FFMIN(eval->nb_samples, av_rescale(eval->duration, eval->sample_rate, AV_TIME_BASE) - eval->pts); | |
| 286 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9375 times.
|
9375 | if (!nb_samples) { |
| 287 | ✗ | ff_outlink_set_status(outlink, AVERROR_EOF, eval->pts); | |
| 288 | ✗ | return 0; | |
| 289 | } | ||
| 290 | } else { | ||
| 291 | ✗ | nb_samples = eval->nb_samples; | |
| 292 | } | ||
| 293 | 9375 | samplesref = ff_get_audio_buffer(outlink, nb_samples); | |
| 294 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9375 times.
|
9375 | if (!samplesref) |
| 295 | ✗ | return AVERROR(ENOMEM); | |
| 296 | |||
| 297 | /* evaluate expression for each single sample and for each channel */ | ||
| 298 |
2/2✓ Branch 0 taken 9976523 times.
✓ Branch 1 taken 9375 times.
|
9985898 | for (i = 0; i < nb_samples; i++, eval->n++) { |
| 299 | 9976523 | eval->var_values[VAR_N] = eval->n; | |
| 300 | 9976523 | eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate; | |
| 301 | |||
| 302 |
2/2✓ Branch 0 taken 9976523 times.
✓ Branch 1 taken 9976523 times.
|
19953046 | for (j = 0; j < eval->nb_channels; j++) { |
| 303 | 9976523 | *((double *) samplesref->extended_data[j] + i) = | |
| 304 | 9976523 | av_expr_eval(eval->expr[j], eval->var_values, NULL); | |
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | 9375 | samplesref->pts = eval->pts; | |
| 309 | 9375 | samplesref->sample_rate = eval->sample_rate; | |
| 310 | 9375 | eval->pts += nb_samples; | |
| 311 | |||
| 312 | 9375 | return ff_filter_frame(outlink, samplesref); | |
| 313 | } | ||
| 314 | |||
| 315 | #if CONFIG_AEVALSRC_FILTER | ||
| 316 | static const AVFilterPad aevalsrc_outputs[] = { | ||
| 317 | { | ||
| 318 | .name = "default", | ||
| 319 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 320 | .config_props = config_props, | ||
| 321 | }, | ||
| 322 | }; | ||
| 323 | |||
| 324 | const FFFilter ff_asrc_aevalsrc = { | ||
| 325 | .p.name = "aevalsrc", | ||
| 326 | .p.description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."), | ||
| 327 | .p.priv_class = &aevalsrc_class, | ||
| 328 | .init = init, | ||
| 329 | .uninit = uninit, | ||
| 330 | .activate = activate, | ||
| 331 | .priv_size = sizeof(EvalContext), | ||
| 332 | FILTER_OUTPUTS(aevalsrc_outputs), | ||
| 333 | FILTER_QUERY_FUNC2(query_formats), | ||
| 334 | }; | ||
| 335 | |||
| 336 | #endif /* CONFIG_AEVALSRC_FILTER */ | ||
| 337 | |||
| 338 | #define OFFSET(x) offsetof(EvalContext, x) | ||
| 339 | #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM | ||
| 340 | |||
| 341 | static const AVOption aeval_options[]= { | ||
| 342 | { "exprs", "set the '|'-separated list of channels expressions", OFFSET(exprs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS }, | ||
| 343 | { "channel_layout", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, | ||
| 344 | { "c", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, | ||
| 345 | { NULL } | ||
| 346 | }; | ||
| 347 | |||
| 348 | AVFILTER_DEFINE_CLASS(aeval); | ||
| 349 | |||
| 350 | 1 | static int aeval_query_formats(const AVFilterContext *ctx, | |
| 351 | AVFilterFormatsConfig **cfg_in, | ||
| 352 | AVFilterFormatsConfig **cfg_out) | ||
| 353 | { | ||
| 354 | AVFilterChannelLayouts *layouts; | ||
| 355 | 1 | const EvalContext *eval = ctx->priv; | |
| 356 | static const enum AVSampleFormat sample_fmts[] = { | ||
| 357 | AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE | ||
| 358 | }; | ||
| 359 | int ret; | ||
| 360 | |||
| 361 | // inlink supports any channel layout | ||
| 362 | 1 | layouts = ff_all_channel_counts(); | |
| 363 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = ff_channel_layouts_ref(layouts, &cfg_in[0]->channel_layouts)) < 0) |
| 364 | ✗ | return ret; | |
| 365 | |||
| 366 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!eval->same_chlayout) { |
| 367 | // outlink supports only requested output channel layout | ||
| 368 | 1 | layouts = NULL; | |
| 369 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = ff_add_channel_layout(&layouts, &FF_COUNT2LAYOUT(eval->nb_channels))) < 0) |
| 370 | ✗ | return ret; | |
| 371 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = ff_channel_layouts_ref(layouts, &cfg_out[0]->channel_layouts)) < 0) |
| 372 | ✗ | return ret; | |
| 373 | } | ||
| 374 | |||
| 375 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = ff_set_sample_formats_from_list2(ctx, cfg_in, cfg_out, sample_fmts)) < 0) |
| 376 | ✗ | return ret; | |
| 377 | |||
| 378 | 1 | return 0; | |
| 379 | } | ||
| 380 | |||
| 381 | 1 | static int aeval_config_output(AVFilterLink *outlink) | |
| 382 | { | ||
| 383 | 1 | AVFilterContext *ctx = outlink->src; | |
| 384 | 1 | EvalContext *eval = ctx->priv; | |
| 385 | 1 | AVFilterLink *inlink = ctx->inputs[0]; | |
| 386 | int ret; | ||
| 387 | |||
| 388 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (eval->same_chlayout) { |
| 389 | ✗ | if ((ret = av_channel_layout_copy(&eval->chlayout, &inlink->ch_layout)) < 0) | |
| 390 | ✗ | return ret; | |
| 391 | |||
| 392 | ✗ | if ((ret = parse_channel_expressions(ctx, inlink->ch_layout.nb_channels)) < 0) | |
| 393 | ✗ | return ret; | |
| 394 | } | ||
| 395 | |||
| 396 | 1 | eval->n = 0; | |
| 397 | 1 | eval->nb_in_channels = eval->var_values[VAR_NB_IN_CHANNELS] = inlink->ch_layout.nb_channels; | |
| 398 | 1 | eval->var_values[VAR_NB_OUT_CHANNELS] = outlink->ch_layout.nb_channels; | |
| 399 | 1 | eval->var_values[VAR_S] = inlink->sample_rate; | |
| 400 | 1 | eval->var_values[VAR_T] = NAN; | |
| 401 | |||
| 402 | 2 | eval->channel_values = av_realloc_f(eval->channel_values, | |
| 403 | 1 | inlink->ch_layout.nb_channels, sizeof(*eval->channel_values)); | |
| 404 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!eval->channel_values) |
| 405 | ✗ | return AVERROR(ENOMEM); | |
| 406 | |||
| 407 | 1 | return 0; | |
| 408 | } | ||
| 409 | |||
| 410 | 65 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) | |
| 411 | { | ||
| 412 | 65 | EvalContext *eval = inlink->dst->priv; | |
| 413 | 65 | AVFilterLink *outlink = inlink->dst->outputs[0]; | |
| 414 | 65 | int nb_samples = in->nb_samples; | |
| 415 | AVFrame *out; | ||
| 416 | double t0; | ||
| 417 | int i, j; | ||
| 418 | |||
| 419 | 65 | out = ff_get_audio_buffer(outlink, nb_samples); | |
| 420 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (!out) { |
| 421 | ✗ | av_frame_free(&in); | |
| 422 | ✗ | return AVERROR(ENOMEM); | |
| 423 | } | ||
| 424 | 65 | av_frame_copy_props(out, in); | |
| 425 | |||
| 426 |
1/2✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
|
65 | t0 = TS2T(in->pts, inlink->time_base); |
| 427 | |||
| 428 | /* evaluate expression for each single sample and for each channel */ | ||
| 429 |
2/2✓ Branch 0 taken 264600 times.
✓ Branch 1 taken 65 times.
|
264665 | for (i = 0; i < nb_samples; i++, eval->n++) { |
| 430 | 264600 | eval->var_values[VAR_N] = eval->n; | |
| 431 | 264600 | eval->var_values[VAR_T] = t0 + i * (double)1/inlink->sample_rate; | |
| 432 | |||
| 433 |
2/2✓ Branch 0 taken 264600 times.
✓ Branch 1 taken 264600 times.
|
529200 | for (j = 0; j < inlink->ch_layout.nb_channels; j++) |
| 434 | 264600 | eval->channel_values[j] = *((double *) in->extended_data[j] + i); | |
| 435 | |||
| 436 |
2/2✓ Branch 0 taken 264600 times.
✓ Branch 1 taken 264600 times.
|
529200 | for (j = 0; j < outlink->ch_layout.nb_channels; j++) { |
| 437 | 264600 | eval->var_values[VAR_CH] = j; | |
| 438 | 264600 | *((double *) out->extended_data[j] + i) = | |
| 439 | 264600 | av_expr_eval(eval->expr[j], eval->var_values, eval); | |
| 440 | } | ||
| 441 | } | ||
| 442 | |||
| 443 | 65 | av_frame_free(&in); | |
| 444 | 65 | return ff_filter_frame(outlink, out); | |
| 445 | } | ||
| 446 | |||
| 447 | #if CONFIG_AEVAL_FILTER | ||
| 448 | |||
| 449 | static const AVFilterPad aeval_inputs[] = { | ||
| 450 | { | ||
| 451 | .name = "default", | ||
| 452 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 453 | .filter_frame = filter_frame, | ||
| 454 | }, | ||
| 455 | }; | ||
| 456 | |||
| 457 | static const AVFilterPad aeval_outputs[] = { | ||
| 458 | { | ||
| 459 | .name = "default", | ||
| 460 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 461 | .config_props = aeval_config_output, | ||
| 462 | }, | ||
| 463 | }; | ||
| 464 | |||
| 465 | const FFFilter ff_af_aeval = { | ||
| 466 | .p.name = "aeval", | ||
| 467 | .p.description = NULL_IF_CONFIG_SMALL("Filter audio signal according to a specified expression."), | ||
| 468 | .p.priv_class = &aeval_class, | ||
| 469 | .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, | ||
| 470 | .init = init, | ||
| 471 | .uninit = uninit, | ||
| 472 | .priv_size = sizeof(EvalContext), | ||
| 473 | FILTER_INPUTS(aeval_inputs), | ||
| 474 | FILTER_OUTPUTS(aeval_outputs), | ||
| 475 | FILTER_QUERY_FUNC2(aeval_query_formats), | ||
| 476 | }; | ||
| 477 | |||
| 478 | #endif /* CONFIG_AEVAL_FILTER */ | ||
| 479 |