aeval.c 16 KB

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