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.

259 lines
8.2KB

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