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.

244 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/avassert.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/channel_layout.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;
  54. char *exprs;
  55. int nb_samples; ///< number of samples per requested frame
  56. int64_t duration;
  57. uint64_t n;
  58. double var_values[VAR_VARS_NB];
  59. } EvalContext;
  60. #define OFFSET(x) offsetof(EvalContext, x)
  61. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  62. static const AVOption aevalsrc_options[]= {
  63. { "exprs", "set the '|'-separated list of channels expressions", OFFSET(exprs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
  64. { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
  65. { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
  66. { "sample_rate", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX, FLAGS },
  67. { "s", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX, FLAGS },
  68. { "duration", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
  69. { "d", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
  70. { "channel_layout", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  71. { "c", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  72. { NULL }
  73. };
  74. AVFILTER_DEFINE_CLASS(aevalsrc);
  75. static av_cold int init(AVFilterContext *ctx)
  76. {
  77. EvalContext *eval = ctx->priv;
  78. char *args1 = av_strdup(eval->exprs);
  79. char *expr, *buf;
  80. int ret;
  81. if (!args1) {
  82. av_log(ctx, AV_LOG_ERROR, "Channels expressions list is empty\n");
  83. ret = eval->exprs ? AVERROR(ENOMEM) : AVERROR(EINVAL);
  84. goto end;
  85. }
  86. /* parse expressions */
  87. buf = args1;
  88. while (expr = av_strtok(buf, "|", &buf)) {
  89. if (!av_dynarray2_add((void **)&eval->expr, &eval->nb_channels, sizeof(*eval->expr), NULL)) {
  90. ret = AVERROR(ENOMEM);
  91. goto end;
  92. }
  93. eval->expr[eval->nb_channels-1] = NULL;
  94. ret = av_expr_parse(&eval->expr[eval->nb_channels - 1], expr, var_names,
  95. NULL, NULL, NULL, NULL, 0, ctx);
  96. if (ret < 0)
  97. goto end;
  98. }
  99. if (eval->chlayout_str) {
  100. int n;
  101. ret = ff_parse_channel_layout(&eval->chlayout, NULL, eval->chlayout_str, ctx);
  102. if (ret < 0)
  103. goto end;
  104. n = av_get_channel_layout_nb_channels(eval->chlayout);
  105. if (n != eval->nb_channels) {
  106. av_log(ctx, AV_LOG_ERROR,
  107. "Mismatch between the specified number of channels '%d' "
  108. "and the number of channels '%d' in the specified channel layout '%s'\n",
  109. eval->nb_channels, n, eval->chlayout_str);
  110. ret = AVERROR(EINVAL);
  111. goto end;
  112. }
  113. } else {
  114. /* guess channel layout from nb expressions/channels */
  115. eval->chlayout = av_get_default_channel_layout(eval->nb_channels);
  116. if (!eval->chlayout && eval->nb_channels <= 0) {
  117. av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
  118. eval->nb_channels);
  119. ret = AVERROR(EINVAL);
  120. goto end;
  121. }
  122. }
  123. if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
  124. goto end;
  125. eval->n = 0;
  126. end:
  127. av_free(args1);
  128. return ret;
  129. }
  130. static av_cold void uninit(AVFilterContext *ctx)
  131. {
  132. EvalContext *eval = ctx->priv;
  133. int i;
  134. for (i = 0; i < eval->nb_channels; i++) {
  135. av_expr_free(eval->expr[i]);
  136. eval->expr[i] = NULL;
  137. }
  138. av_freep(&eval->expr);
  139. }
  140. static int config_props(AVFilterLink *outlink)
  141. {
  142. EvalContext *eval = outlink->src->priv;
  143. char buf[128];
  144. outlink->time_base = (AVRational){1, eval->sample_rate};
  145. outlink->sample_rate = eval->sample_rate;
  146. eval->var_values[VAR_S] = eval->sample_rate;
  147. av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
  148. av_log(outlink->src, AV_LOG_VERBOSE,
  149. "sample_rate:%d chlayout:%s duration:%"PRId64"\n",
  150. eval->sample_rate, buf, eval->duration);
  151. return 0;
  152. }
  153. static int query_formats(AVFilterContext *ctx)
  154. {
  155. EvalContext *eval = ctx->priv;
  156. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE };
  157. int64_t chlayouts[] = { eval->chlayout ? eval->chlayout : FF_COUNT2LAYOUT(eval->nb_channels) , -1 };
  158. int sample_rates[] = { eval->sample_rate, -1 };
  159. ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
  160. ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
  161. ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
  162. return 0;
  163. }
  164. static int request_frame(AVFilterLink *outlink)
  165. {
  166. EvalContext *eval = outlink->src->priv;
  167. AVFrame *samplesref;
  168. int i, j;
  169. int64_t t = av_rescale(eval->n, AV_TIME_BASE, eval->sample_rate);
  170. if (eval->duration >= 0 && t >= eval->duration)
  171. return AVERROR_EOF;
  172. samplesref = ff_get_audio_buffer(outlink, eval->nb_samples);
  173. if (!samplesref)
  174. return AVERROR(ENOMEM);
  175. /* evaluate expression for each single sample and for each channel */
  176. for (i = 0; i < eval->nb_samples; i++, eval->n++) {
  177. eval->var_values[VAR_N] = eval->n;
  178. eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
  179. for (j = 0; j < eval->nb_channels; j++) {
  180. *((double *) samplesref->extended_data[j] + i) =
  181. av_expr_eval(eval->expr[j], eval->var_values, NULL);
  182. }
  183. }
  184. samplesref->pts = eval->pts;
  185. samplesref->sample_rate = eval->sample_rate;
  186. eval->pts += eval->nb_samples;
  187. return ff_filter_frame(outlink, samplesref);
  188. }
  189. static const AVFilterPad aevalsrc_outputs[] = {
  190. {
  191. .name = "default",
  192. .type = AVMEDIA_TYPE_AUDIO,
  193. .config_props = config_props,
  194. .request_frame = request_frame,
  195. },
  196. { NULL }
  197. };
  198. AVFilter ff_asrc_aevalsrc = {
  199. .name = "aevalsrc",
  200. .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
  201. .query_formats = query_formats,
  202. .init = init,
  203. .uninit = uninit,
  204. .priv_size = sizeof(EvalContext),
  205. .inputs = NULL,
  206. .outputs = aevalsrc_outputs,
  207. .priv_class = &aevalsrc_class,
  208. };