asrc_aevalsrc.c 8.3 KB

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