asrc_anullsrc.c 4.7 KB

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