aeval.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 {
  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, *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. }
  193. static int config_props(AVFilterLink *outlink)
  194. {
  195. EvalContext *eval = outlink->src->priv;
  196. char buf[128];
  197. outlink->time_base = (AVRational){1, eval->sample_rate};
  198. outlink->sample_rate = eval->sample_rate;
  199. eval->var_values[VAR_S] = eval->sample_rate;
  200. eval->var_values[VAR_NB_IN_CHANNELS] = NAN;
  201. eval->var_values[VAR_NB_OUT_CHANNELS] = outlink->channels;
  202. av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
  203. av_log(outlink->src, AV_LOG_VERBOSE,
  204. "sample_rate:%d chlayout:%s duration:%"PRId64"\n",
  205. eval->sample_rate, buf, eval->duration);
  206. return 0;
  207. }
  208. static int query_formats(AVFilterContext *ctx)
  209. {
  210. EvalContext *eval = ctx->priv;
  211. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE };
  212. int64_t chlayouts[] = { eval->chlayout ? eval->chlayout : FF_COUNT2LAYOUT(eval->nb_channels) , -1 };
  213. int sample_rates[] = { eval->sample_rate, -1 };
  214. ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
  215. ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
  216. ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
  217. return 0;
  218. }
  219. static int request_frame(AVFilterLink *outlink)
  220. {
  221. EvalContext *eval = outlink->src->priv;
  222. AVFrame *samplesref;
  223. int i, j;
  224. int64_t t = av_rescale(eval->n, AV_TIME_BASE, eval->sample_rate);
  225. if (eval->duration >= 0 && t >= eval->duration)
  226. return AVERROR_EOF;
  227. samplesref = ff_get_audio_buffer(outlink, eval->nb_samples);
  228. if (!samplesref)
  229. return AVERROR(ENOMEM);
  230. /* evaluate expression for each single sample and for each channel */
  231. for (i = 0; i < eval->nb_samples; i++, eval->n++) {
  232. eval->var_values[VAR_N] = eval->n;
  233. eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
  234. for (j = 0; j < eval->nb_channels; j++) {
  235. *((double *) samplesref->extended_data[j] + i) =
  236. av_expr_eval(eval->expr[j], eval->var_values, NULL);
  237. }
  238. }
  239. samplesref->pts = eval->pts;
  240. samplesref->sample_rate = eval->sample_rate;
  241. eval->pts += eval->nb_samples;
  242. return ff_filter_frame(outlink, samplesref);
  243. }
  244. #if CONFIG_AEVALSRC_FILTER
  245. static const AVFilterPad aevalsrc_outputs[] = {
  246. {
  247. .name = "default",
  248. .type = AVMEDIA_TYPE_AUDIO,
  249. .config_props = config_props,
  250. .request_frame = request_frame,
  251. },
  252. { NULL }
  253. };
  254. AVFilter ff_asrc_aevalsrc = {
  255. .name = "aevalsrc",
  256. .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
  257. .query_formats = query_formats,
  258. .init = init,
  259. .uninit = uninit,
  260. .priv_size = sizeof(EvalContext),
  261. .inputs = NULL,
  262. .outputs = aevalsrc_outputs,
  263. .priv_class = &aevalsrc_class,
  264. };
  265. #endif /* CONFIG_AEVALSRC_FILTER */
  266. #define OFFSET(x) offsetof(EvalContext, x)
  267. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  268. static const AVOption aeval_options[]= {
  269. { "exprs", "set the '|'-separated list of channels expressions", OFFSET(exprs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
  270. { "channel_layout", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  271. { "c", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  272. { NULL }
  273. };
  274. AVFILTER_DEFINE_CLASS(aeval);
  275. static int aeval_query_formats(AVFilterContext *ctx)
  276. {
  277. AVFilterFormats *formats = NULL;
  278. AVFilterChannelLayouts *layouts;
  279. AVFilterLink *inlink = ctx->inputs[0];
  280. AVFilterLink *outlink = ctx->outputs[0];
  281. EvalContext *eval = ctx->priv;
  282. static const enum AVSampleFormat sample_fmts[] = {
  283. AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE
  284. };
  285. // inlink supports any channel layout
  286. layouts = ff_all_channel_counts();
  287. ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
  288. if (eval->same_chlayout) {
  289. layouts = ff_all_channel_counts();
  290. if (!layouts)
  291. return AVERROR(ENOMEM);
  292. ff_set_common_channel_layouts(ctx, layouts);
  293. } else {
  294. // outlink supports only requested output channel layout
  295. layouts = NULL;
  296. ff_add_channel_layout(&layouts,
  297. eval->out_channel_layout ? eval->out_channel_layout :
  298. FF_COUNT2LAYOUT(eval->nb_channels));
  299. ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
  300. }
  301. formats = ff_make_format_list(sample_fmts);
  302. if (!formats)
  303. return AVERROR(ENOMEM);
  304. ff_set_common_formats(ctx, formats);
  305. formats = ff_all_samplerates();
  306. if (!formats)
  307. return AVERROR(ENOMEM);
  308. ff_set_common_samplerates(ctx, formats);
  309. return 0;
  310. }
  311. static int aeval_config_output(AVFilterLink *outlink)
  312. {
  313. AVFilterContext *ctx = outlink->src;
  314. EvalContext *eval = ctx->priv;
  315. AVFilterLink *inlink = ctx->inputs[0];
  316. int ret;
  317. if (eval->same_chlayout) {
  318. eval->chlayout = inlink->channel_layout;
  319. if ((ret = parse_channel_expressions(ctx, inlink->channels)) < 0)
  320. return ret;
  321. }
  322. eval->n = 0;
  323. eval->nb_in_channels = eval->var_values[VAR_NB_IN_CHANNELS] = inlink->channels;
  324. eval->var_values[VAR_NB_OUT_CHANNELS] = outlink->channels;
  325. eval->var_values[VAR_S] = inlink->sample_rate;
  326. eval->var_values[VAR_T] = NAN;
  327. eval->channel_values = av_realloc_f(eval->channel_values,
  328. inlink->channels, sizeof(*eval->channel_values));
  329. if (!eval->channel_values)
  330. return AVERROR(ENOMEM);
  331. return 0;
  332. }
  333. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
  334. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  335. {
  336. EvalContext *eval = inlink->dst->priv;
  337. AVFilterLink *outlink = inlink->dst->outputs[0];
  338. int nb_samples = in->nb_samples;
  339. AVFrame *out;
  340. double t0;
  341. int i, j;
  342. /* do volume scaling in-place if input buffer is writable */
  343. out = ff_get_audio_buffer(outlink, nb_samples);
  344. if (!out)
  345. return AVERROR(ENOMEM);
  346. av_frame_copy_props(out, in);
  347. t0 = TS2T(in->pts, inlink->time_base);
  348. /* evaluate expression for each single sample and for each channel */
  349. for (i = 0; i < nb_samples; i++, eval->n++) {
  350. eval->var_values[VAR_N] = eval->n;
  351. eval->var_values[VAR_T] = t0 + i * (double)1/inlink->sample_rate;
  352. for (j = 0; j < inlink->channels; j++)
  353. eval->channel_values[j] = *((double *) in->extended_data[j] + i);
  354. for (j = 0; j < outlink->channels; j++) {
  355. eval->var_values[VAR_CH] = j;
  356. *((double *) out->extended_data[j] + i) =
  357. av_expr_eval(eval->expr[j], eval->var_values, eval);
  358. }
  359. }
  360. av_frame_free(&in);
  361. return ff_filter_frame(outlink, out);
  362. }
  363. #if CONFIG_AEVAL_FILTER
  364. static const AVFilterPad aeval_inputs[] = {
  365. {
  366. .name = "default",
  367. .type = AVMEDIA_TYPE_AUDIO,
  368. .filter_frame = filter_frame,
  369. },
  370. { NULL }
  371. };
  372. static const AVFilterPad aeval_outputs[] = {
  373. {
  374. .name = "default",
  375. .type = AVMEDIA_TYPE_AUDIO,
  376. .config_props = aeval_config_output,
  377. },
  378. { NULL }
  379. };
  380. AVFilter ff_af_aeval = {
  381. .name = "aeval",
  382. .description = NULL_IF_CONFIG_SMALL("Filter audio signal according to a specified expression."),
  383. .query_formats = aeval_query_formats,
  384. .init = init,
  385. .uninit = uninit,
  386. .priv_size = sizeof(EvalContext),
  387. .inputs = aeval_inputs,
  388. .outputs = aeval_outputs,
  389. .priv_class = &aeval_class,
  390. };
  391. #endif /* CONFIG_AEVAL_FILTER */