asrc_anullsrc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "avfilter.h"
  25. #include "internal.h"
  26. typedef struct {
  27. const AVClass *class;
  28. char *channel_layout_str;
  29. uint64_t channel_layout;
  30. char *sample_rate_str;
  31. int sample_rate;
  32. int nb_samples; ///< number of samples per requested frame
  33. int64_t pts;
  34. } ANullContext;
  35. #define OFFSET(x) offsetof(ANullContext, x)
  36. static const AVOption anullsrc_options[]= {
  37. { "channel_layout", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0 },
  38. { "cl", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0 },
  39. { "sample_rate", "set sample rate", OFFSET(sample_rate_str) , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0 },
  40. { "r", "set sample rate", OFFSET(sample_rate_str) , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0 },
  41. { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.dbl = 1024}, 0, INT_MAX },
  42. { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.dbl = 1024}, 0, INT_MAX },
  43. { NULL },
  44. };
  45. static const char *anullsrc_get_name(void *ctx)
  46. {
  47. return "anullsrc";
  48. }
  49. static const AVClass anullsrc_class = {
  50. "ANullSrcContext",
  51. anullsrc_get_name,
  52. anullsrc_options
  53. };
  54. static int init(AVFilterContext *ctx, const char *args, void *opaque)
  55. {
  56. ANullContext *null = ctx->priv;
  57. int ret;
  58. null->class = &anullsrc_class;
  59. av_opt_set_defaults(null);
  60. if ((ret = (av_set_options_string(null, args, "=", ":"))) < 0) {
  61. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  62. return ret;
  63. }
  64. if ((ret = ff_parse_sample_rate(&null->sample_rate,
  65. null->sample_rate_str, ctx)) < 0)
  66. return ret;
  67. if ((ret = ff_parse_channel_layout(&null->channel_layout,
  68. null->channel_layout_str, ctx)) < 0)
  69. return ret;
  70. return 0;
  71. }
  72. static int config_props(AVFilterLink *outlink)
  73. {
  74. ANullContext *null = outlink->src->priv;
  75. char buf[128];
  76. int chans_nb;
  77. outlink->sample_rate = null->sample_rate;
  78. outlink->channel_layout = null->channel_layout;
  79. chans_nb = av_get_channel_layout_nb_channels(null->channel_layout);
  80. av_get_channel_layout_string(buf, sizeof(buf), chans_nb, null->channel_layout);
  81. av_log(outlink->src, AV_LOG_INFO,
  82. "sample_rate:%d channel_layout:'%s' nb_samples:%d\n",
  83. null->sample_rate, buf, null->nb_samples);
  84. return 0;
  85. }
  86. static int request_frame(AVFilterLink *outlink)
  87. {
  88. ANullContext *null = outlink->src->priv;
  89. AVFilterBufferRef *samplesref;
  90. samplesref =
  91. avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, null->nb_samples);
  92. samplesref->pts = null->pts;
  93. samplesref->pos = -1;
  94. samplesref->audio->channel_layout = null->channel_layout;
  95. samplesref->audio->sample_rate = outlink->sample_rate;
  96. avfilter_filter_samples(outlink, avfilter_ref_buffer(samplesref, ~0));
  97. avfilter_unref_buffer(samplesref);
  98. null->pts += null->nb_samples;
  99. return 0;
  100. }
  101. AVFilter avfilter_asrc_anullsrc = {
  102. .name = "anullsrc",
  103. .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
  104. .init = init,
  105. .priv_size = sizeof(ANullContext),
  106. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  107. .outputs = (const AVFilterPad[]) {{ .name = "default",
  108. .type = AVMEDIA_TYPE_AUDIO,
  109. .config_props = config_props,
  110. .request_frame = request_frame, },
  111. { .name = NULL}},
  112. };