asrc_aevalsrc.c 6.7 KB

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