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.

151 lines
5.2KB

  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}, 1, UINT16_MAX, FLAGS },
  52. { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, UINT16_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 av_cold int config_props(AVFilterLink *outlink)
  82. {
  83. ANullContext *null = outlink->src->priv;
  84. if (null->duration >= 0)
  85. null->duration = av_rescale(null->duration, null->sample_rate, AV_TIME_BASE);
  86. return 0;
  87. }
  88. static int activate(AVFilterContext *ctx)
  89. {
  90. ANullContext *null = ctx->priv;
  91. AVFilterLink *outlink = ctx->outputs[0];
  92. if (null->duration >= 0 && null->pts >= null->duration) {
  93. ff_outlink_set_status(outlink, AVERROR_EOF, null->pts);
  94. return 0;
  95. }
  96. if (ff_outlink_frame_wanted(outlink)) {
  97. AVFrame *samplesref = ff_get_audio_buffer(outlink, null->duration >= 0 ? FFMIN(null->nb_samples, null->duration - null->pts) : null->nb_samples);
  98. if (!samplesref)
  99. return AVERROR(ENOMEM);
  100. samplesref->pts = null->pts;
  101. null->pts += samplesref->nb_samples;
  102. return ff_filter_frame(outlink, samplesref);
  103. }
  104. return FFERROR_NOT_READY;
  105. }
  106. static const AVFilterPad avfilter_asrc_anullsrc_outputs[] = {
  107. {
  108. .name = "default",
  109. .type = AVMEDIA_TYPE_AUDIO,
  110. .config_props = config_props,
  111. },
  112. { NULL }
  113. };
  114. AVFilter ff_asrc_anullsrc = {
  115. .name = "anullsrc",
  116. .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
  117. .init = init,
  118. .query_formats = query_formats,
  119. .priv_size = sizeof(ANullContext),
  120. .inputs = NULL,
  121. .outputs = avfilter_asrc_anullsrc_outputs,
  122. .activate = activate,
  123. .priv_class = &anullsrc_class,
  124. };