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.

160 lines
5.4KB

  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 <inttypes.h>
  26. #include <stdio.h>
  27. #include "libavutil/channel_layout.h"
  28. #include "libavutil/internal.h"
  29. #include "libavutil/opt.h"
  30. #include "audio.h"
  31. #include "avfilter.h"
  32. #include "filters.h"
  33. #include "internal.h"
  34. typedef struct ANullContext {
  35. const AVClass *class;
  36. char *channel_layout_str;
  37. uint64_t channel_layout;
  38. char *sample_rate_str;
  39. int sample_rate;
  40. int64_t duration;
  41. int nb_samples; ///< number of samples per requested frame
  42. int64_t pts;
  43. } ANullContext;
  44. #define OFFSET(x) offsetof(ANullContext, x)
  45. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  46. static const AVOption anullsrc_options[]= {
  47. { "channel_layout", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, FLAGS },
  48. { "cl", "set channel_layout", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, FLAGS },
  49. { "sample_rate", "set sample rate", OFFSET(sample_rate_str) , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
  50. { "r", "set sample rate", OFFSET(sample_rate_str) , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
  51. { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
  52. { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
  53. { "duration", "set the audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
  54. { "d", "set the audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
  55. { NULL }
  56. };
  57. AVFILTER_DEFINE_CLASS(anullsrc);
  58. static av_cold int init(AVFilterContext *ctx)
  59. {
  60. ANullContext *null = ctx->priv;
  61. int ret;
  62. if ((ret = ff_parse_sample_rate(&null->sample_rate,
  63. null->sample_rate_str, ctx)) < 0)
  64. return ret;
  65. if ((ret = ff_parse_channel_layout(&null->channel_layout, NULL,
  66. null->channel_layout_str, ctx)) < 0)
  67. return ret;
  68. return 0;
  69. }
  70. static int query_formats(AVFilterContext *ctx)
  71. {
  72. ANullContext *null = ctx->priv;
  73. int64_t chlayouts[] = { null->channel_layout, -1 };
  74. int sample_rates[] = { null->sample_rate, -1 };
  75. int ret;
  76. if ((ret = ff_set_common_formats (ctx, ff_all_formats (AVMEDIA_TYPE_AUDIO))) < 0 ||
  77. (ret = ff_set_common_samplerates (ctx, ff_make_format_list (sample_rates ))) < 0)
  78. return ret;
  79. return ff_set_common_channel_layouts(ctx, ff_make_format64_list(chlayouts));
  80. }
  81. static int config_props(AVFilterLink *outlink)
  82. {
  83. ANullContext *null = outlink->src->priv;
  84. char buf[128];
  85. av_get_channel_layout_string(buf, sizeof(buf), 0, null->channel_layout);
  86. av_log(outlink->src, AV_LOG_VERBOSE,
  87. "sample_rate:%d channel_layout:'%s' nb_samples:%d\n",
  88. null->sample_rate, buf, null->nb_samples);
  89. return 0;
  90. }
  91. static int activate(AVFilterContext *ctx)
  92. {
  93. ANullContext *null = ctx->priv;
  94. AVFilterLink *outlink = ctx->outputs[0];
  95. if (null->duration >= 0 &&
  96. av_rescale_q(null->pts, outlink->time_base, AV_TIME_BASE_Q) >= null->duration) {
  97. ff_outlink_set_status(outlink, AVERROR_EOF, null->pts);
  98. return 0;
  99. }
  100. if (ff_outlink_frame_wanted(outlink)) {
  101. AVFrame *samplesref = ff_get_audio_buffer(outlink, null->nb_samples);
  102. int ret;
  103. if (!samplesref)
  104. return AVERROR(ENOMEM);
  105. samplesref->pts = null->pts;
  106. ret = ff_filter_frame(outlink, samplesref);
  107. if (ret < 0)
  108. return ret;
  109. null->pts += null->nb_samples;
  110. return 0;
  111. }
  112. return FFERROR_NOT_READY;
  113. }
  114. static const AVFilterPad avfilter_asrc_anullsrc_outputs[] = {
  115. {
  116. .name = "default",
  117. .type = AVMEDIA_TYPE_AUDIO,
  118. .config_props = config_props,
  119. },
  120. { NULL }
  121. };
  122. AVFilter ff_asrc_anullsrc = {
  123. .name = "anullsrc",
  124. .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
  125. .init = init,
  126. .query_formats = query_formats,
  127. .priv_size = sizeof(ANullContext),
  128. .inputs = NULL,
  129. .outputs = avfilter_asrc_anullsrc_outputs,
  130. .activate = activate,
  131. .priv_class = &anullsrc_class,
  132. };