You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

484 lines
16KB

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