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.

140 lines
4.6KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * null audio source
  21. */
  22. #include "libavutil/audioconvert.h"
  23. #include "libavutil/opt.h"
  24. #include "audio.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. typedef struct {
  28. const AVClass *class;
  29. char *channel_layout_str;
  30. uint64_t channel_layout;
  31. char *sample_rate_str;
  32. int sample_rate;
  33. int nb_samples; ///< number of samples per requested frame
  34. int64_t pts;
  35. } ANullContext;
  36. #define OFFSET(x) offsetof(ANullContext, x)
  37. static const AVOption anullsrc_options[]= {
  38. { "channel_layout", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0 },
  39. { "cl", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0 },
  40. { "sample_rate", "set sample rate", OFFSET(sample_rate_str) , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0 },
  41. { "r", "set sample rate", OFFSET(sample_rate_str) , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0 },
  42. { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.dbl = 1024}, 0, INT_MAX },
  43. { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.dbl = 1024}, 0, INT_MAX },
  44. { NULL },
  45. };
  46. static const char *anullsrc_get_name(void *ctx)
  47. {
  48. return "anullsrc";
  49. }
  50. static const AVClass anullsrc_class = {
  51. "ANullSrcContext",
  52. anullsrc_get_name,
  53. anullsrc_options
  54. };
  55. static int init(AVFilterContext *ctx, const char *args, void *opaque)
  56. {
  57. ANullContext *null = ctx->priv;
  58. int ret;
  59. null->class = &anullsrc_class;
  60. av_opt_set_defaults(null);
  61. if ((ret = (av_set_options_string(null, args, "=", ":"))) < 0) {
  62. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  63. return ret;
  64. }
  65. if ((ret = ff_parse_sample_rate(&null->sample_rate,
  66. null->sample_rate_str, ctx)) < 0)
  67. return ret;
  68. if ((ret = ff_parse_channel_layout(&null->channel_layout,
  69. null->channel_layout_str, ctx)) < 0)
  70. return ret;
  71. return 0;
  72. }
  73. static int config_props(AVFilterLink *outlink)
  74. {
  75. ANullContext *null = outlink->src->priv;
  76. char buf[128];
  77. int chans_nb;
  78. outlink->sample_rate = null->sample_rate;
  79. outlink->channel_layout = null->channel_layout;
  80. chans_nb = av_get_channel_layout_nb_channels(null->channel_layout);
  81. av_get_channel_layout_string(buf, sizeof(buf), chans_nb, null->channel_layout);
  82. av_log(outlink->src, AV_LOG_INFO,
  83. "sample_rate:%d channel_layout:'%s' nb_samples:%d\n",
  84. null->sample_rate, buf, null->nb_samples);
  85. return 0;
  86. }
  87. static int request_frame(AVFilterLink *outlink)
  88. {
  89. ANullContext *null = outlink->src->priv;
  90. AVFilterBufferRef *samplesref;
  91. samplesref =
  92. ff_get_audio_buffer(outlink, AV_PERM_WRITE, null->nb_samples);
  93. samplesref->pts = null->pts;
  94. samplesref->pos = -1;
  95. samplesref->audio->channel_layout = null->channel_layout;
  96. samplesref->audio->sample_rate = outlink->sample_rate;
  97. ff_filter_samples(outlink, avfilter_ref_buffer(samplesref, ~0));
  98. avfilter_unref_buffer(samplesref);
  99. null->pts += null->nb_samples;
  100. return 0;
  101. }
  102. AVFilter avfilter_asrc_anullsrc = {
  103. .name = "anullsrc",
  104. .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
  105. .init = init,
  106. .priv_size = sizeof(ANullContext),
  107. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  108. .outputs = (const AVFilterPad[]) {{ .name = "default",
  109. .type = AVMEDIA_TYPE_AUDIO,
  110. .config_props = config_props,
  111. .request_frame = request_frame, },
  112. { .name = NULL}},
  113. };