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.

256 lines
8.2KB

  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. "n", ///< number of frame
  35. "t", ///< timestamp expressed in seconds
  36. "s", ///< sample rate
  37. NULL
  38. };
  39. enum var_name {
  40. VAR_N,
  41. VAR_T,
  42. VAR_S,
  43. VAR_VARS_NB
  44. };
  45. typedef struct {
  46. const AVClass *class;
  47. char *sample_rate_str;
  48. int sample_rate;
  49. int64_t chlayout;
  50. char *chlayout_str;
  51. int nb_channels;
  52. int64_t pts;
  53. AVExpr *expr[8];
  54. char *exprs;
  55. int nb_samples; ///< number of samples per requested frame
  56. char *duration_str; ///< total duration of the generated audio
  57. double duration;
  58. uint64_t n;
  59. double var_values[VAR_VARS_NB];
  60. } EvalContext;
  61. #define OFFSET(x) offsetof(EvalContext, x)
  62. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  63. static const AVOption aevalsrc_options[]= {
  64. { "exprs", "set the '|'-separated list of channels expressions", OFFSET(exprs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
  65. { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
  66. { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
  67. { "sample_rate", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX, FLAGS },
  68. { "s", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX, FLAGS },
  69. { "duration", "set audio duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  70. { "d", "set audio duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  71. { "channel_layout", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  72. { "c", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  73. {NULL},
  74. };
  75. AVFILTER_DEFINE_CLASS(aevalsrc);
  76. static int init(AVFilterContext *ctx)
  77. {
  78. EvalContext *eval = ctx->priv;
  79. char *args1 = av_strdup(eval->exprs);
  80. char *expr, *buf;
  81. int ret, i;
  82. if (!args1) {
  83. av_log(ctx, AV_LOG_ERROR, "Channels expressions list is empty\n");
  84. ret = eval->exprs ? AVERROR(ENOMEM) : AVERROR(EINVAL);
  85. goto end;
  86. }
  87. /* parse expressions */
  88. buf = args1;
  89. i = 0;
  90. while (i < FF_ARRAY_ELEMS(eval->expr) && (expr = av_strtok(buf, "|", &buf))) {
  91. ret = av_expr_parse(&eval->expr[i], expr, var_names,
  92. NULL, NULL, NULL, NULL, 0, ctx);
  93. if (ret < 0)
  94. goto end;
  95. i++;
  96. }
  97. eval->nb_channels = i;
  98. if (eval->chlayout_str) {
  99. int n;
  100. ret = ff_parse_channel_layout(&eval->chlayout, eval->chlayout_str, ctx);
  101. if (ret < 0)
  102. goto end;
  103. n = av_get_channel_layout_nb_channels(eval->chlayout);
  104. if (n != eval->nb_channels) {
  105. av_log(ctx, AV_LOG_ERROR,
  106. "Mismatch between the specified number of channels '%d' "
  107. "and the number of channels '%d' in the specified channel layout '%s'\n",
  108. eval->nb_channels, n, eval->chlayout_str);
  109. ret = AVERROR(EINVAL);
  110. goto end;
  111. }
  112. } else {
  113. /* guess channel layout from nb expressions/channels */
  114. eval->chlayout = av_get_default_channel_layout(eval->nb_channels);
  115. if (!eval->chlayout) {
  116. av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
  117. eval->nb_channels);
  118. ret = AVERROR(EINVAL);
  119. goto end;
  120. }
  121. }
  122. if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
  123. goto end;
  124. eval->duration = -1;
  125. if (eval->duration_str) {
  126. int64_t us = -1;
  127. if ((ret = av_parse_time(&us, eval->duration_str, 1)) < 0) {
  128. av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", eval->duration_str);
  129. goto end;
  130. }
  131. eval->duration = (double)us / 1000000;
  132. }
  133. eval->n = 0;
  134. end:
  135. av_free(args1);
  136. return ret;
  137. }
  138. static void uninit(AVFilterContext *ctx)
  139. {
  140. EvalContext *eval = ctx->priv;
  141. int i;
  142. for (i = 0; i < 8; i++) {
  143. av_expr_free(eval->expr[i]);
  144. eval->expr[i] = NULL;
  145. }
  146. av_freep(&eval->chlayout_str);
  147. av_freep(&eval->duration_str);
  148. av_freep(&eval->sample_rate_str);
  149. }
  150. static int config_props(AVFilterLink *outlink)
  151. {
  152. EvalContext *eval = outlink->src->priv;
  153. char buf[128];
  154. outlink->time_base = (AVRational){1, eval->sample_rate};
  155. outlink->sample_rate = eval->sample_rate;
  156. eval->var_values[VAR_S] = eval->sample_rate;
  157. av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
  158. av_log(outlink->src, AV_LOG_VERBOSE,
  159. "sample_rate:%d chlayout:%s duration:%f\n",
  160. eval->sample_rate, buf, eval->duration);
  161. return 0;
  162. }
  163. static int query_formats(AVFilterContext *ctx)
  164. {
  165. EvalContext *eval = ctx->priv;
  166. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE };
  167. int64_t chlayouts[] = { eval->chlayout, -1 };
  168. int sample_rates[] = { eval->sample_rate, -1 };
  169. ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
  170. ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
  171. ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
  172. return 0;
  173. }
  174. static int request_frame(AVFilterLink *outlink)
  175. {
  176. EvalContext *eval = outlink->src->priv;
  177. AVFrame *samplesref;
  178. int i, j;
  179. double t = eval->n * (double)1/eval->sample_rate;
  180. if (eval->duration >= 0 && t >= eval->duration)
  181. return AVERROR_EOF;
  182. samplesref = ff_get_audio_buffer(outlink, eval->nb_samples);
  183. if (!samplesref)
  184. return AVERROR(ENOMEM);
  185. /* evaluate expression for each single sample and for each channel */
  186. for (i = 0; i < eval->nb_samples; i++, eval->n++) {
  187. eval->var_values[VAR_N] = eval->n;
  188. eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
  189. for (j = 0; j < eval->nb_channels; j++) {
  190. *((double *) samplesref->extended_data[j] + i) =
  191. av_expr_eval(eval->expr[j], eval->var_values, NULL);
  192. }
  193. }
  194. samplesref->pts = eval->pts;
  195. samplesref->sample_rate = eval->sample_rate;
  196. eval->pts += eval->nb_samples;
  197. return ff_filter_frame(outlink, samplesref);
  198. }
  199. static const AVFilterPad aevalsrc_outputs[] = {
  200. {
  201. .name = "default",
  202. .type = AVMEDIA_TYPE_AUDIO,
  203. .config_props = config_props,
  204. .request_frame = request_frame,
  205. },
  206. { NULL }
  207. };
  208. AVFilter avfilter_asrc_aevalsrc = {
  209. .name = "aevalsrc",
  210. .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
  211. .query_formats = query_formats,
  212. .init = init,
  213. .uninit = uninit,
  214. .priv_size = sizeof(EvalContext),
  215. .inputs = NULL,
  216. .outputs = aevalsrc_outputs,
  217. .priv_class = &aevalsrc_class,
  218. };