asrc_anullsrc.c 4.9 KB

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