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.

255 lines
8.1KB

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