asrc_aevalsrc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. if (!args1) {
  87. av_log(ctx, AV_LOG_ERROR, "Argument is empty\n");
  88. ret = args ? AVERROR(ENOMEM) : AVERROR(EINVAL);
  89. goto end;
  90. }
  91. /* parse expressions */
  92. buf = args1;
  93. i = 0;
  94. while (expr = av_strtok(buf, ":", &bufptr)) {
  95. if (i >= 8) {
  96. av_log(ctx, AV_LOG_ERROR,
  97. "More than 8 expressions provided, unsupported.\n");
  98. ret = AVERROR(EINVAL);
  99. return ret;
  100. }
  101. ret = av_expr_parse(&eval->expr[i], expr, var_names,
  102. NULL, NULL, NULL, NULL, 0, ctx);
  103. if (ret < 0)
  104. goto end;
  105. i++;
  106. if (bufptr && *bufptr == ':') { /* found last expression */
  107. bufptr++;
  108. break;
  109. }
  110. buf = NULL;
  111. }
  112. /* guess channel layout from nb expressions/channels */
  113. eval->nb_channels = i;
  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. if (bufptr && (ret = av_set_options_string(eval, bufptr, "=", ":")) < 0)
  122. goto end;
  123. if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
  124. goto end;
  125. eval->duration = -1;
  126. if (eval->duration_str) {
  127. int64_t us = -1;
  128. if ((ret = av_parse_time(&us, eval->duration_str, 1)) < 0) {
  129. av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", eval->duration_str);
  130. goto end;
  131. }
  132. eval->duration = (double)us / 1000000;
  133. }
  134. eval->n = 0;
  135. end:
  136. av_free(args1);
  137. return ret;
  138. }
  139. static void uninit(AVFilterContext *ctx)
  140. {
  141. EvalContext *eval = ctx->priv;
  142. int i;
  143. for (i = 0; i < 8; i++) {
  144. av_expr_free(eval->expr[i]);
  145. eval->expr[i] = NULL;
  146. }
  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_INFO,
  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. enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE };
  167. int64_t chlayouts[] = { eval->chlayout, -1 };
  168. int packing_fmts[] = { AVFILTER_PLANAR, -1 };
  169. avfilter_set_common_sample_formats (ctx, avfilter_make_format_list(sample_fmts));
  170. avfilter_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
  171. avfilter_set_common_packing_formats(ctx, avfilter_make_format_list(packing_fmts));
  172. return 0;
  173. }
  174. static int request_frame(AVFilterLink *outlink)
  175. {
  176. EvalContext *eval = outlink->src->priv;
  177. AVFilterBufferRef *samplesref;
  178. int i, j;
  179. double t = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
  180. if (eval->duration >= 0 && t > eval->duration)
  181. return AVERROR_EOF;
  182. samplesref = avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, eval->nb_samples);
  183. /* evaluate expression for each single sample and for each channel */
  184. for (i = 0; i < eval->nb_samples; i++, eval->n++) {
  185. eval->var_values[VAR_N] = eval->n;
  186. eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
  187. for (j = 0; j < eval->nb_channels; j++) {
  188. *((double *) samplesref->data[j] + i) =
  189. av_expr_eval(eval->expr[j], eval->var_values, NULL);
  190. }
  191. }
  192. samplesref->pts = eval->pts;
  193. samplesref->pos = -1;
  194. samplesref->audio->sample_rate = eval->sample_rate;
  195. eval->pts += eval->nb_samples;
  196. avfilter_filter_samples(outlink, samplesref);
  197. return 0;
  198. }
  199. AVFilter avfilter_asrc_aevalsrc = {
  200. .name = "aevalsrc",
  201. .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
  202. .query_formats = query_formats,
  203. .init = init,
  204. .uninit = uninit,
  205. .priv_size = sizeof(EvalContext),
  206. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  207. .outputs = (const AVFilterPad[]) {{ .name = "default",
  208. .type = AVMEDIA_TYPE_AUDIO,
  209. .config_props = config_props,
  210. .request_frame = request_frame, },
  211. { .name = NULL}},
  212. };