asrc_aevalsrc.c 7.6 KB

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