asrc_aevalsrc.c 8.4 KB

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