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.

249 lines
7.6KB

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