asrc_anullsrc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. ff_set_common_formats (ctx, ff_all_formats(AVMEDIA_TYPE_AUDIO));
  72. ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
  73. ff_set_common_samplerates (ctx, ff_make_format_list(sample_rates));
  74. return 0;
  75. }
  76. static int config_props(AVFilterLink *outlink)
  77. {
  78. ANullContext *null = outlink->src->priv;
  79. char buf[128];
  80. av_get_channel_layout_string(buf, sizeof(buf), 0, null->channel_layout);
  81. av_log(outlink->src, AV_LOG_VERBOSE,
  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. int ret;
  89. ANullContext *null = outlink->src->priv;
  90. AVFrame *samplesref;
  91. samplesref = ff_get_audio_buffer(outlink, null->nb_samples);
  92. if (!samplesref)
  93. return AVERROR(ENOMEM);
  94. samplesref->pts = null->pts;
  95. samplesref->channel_layout = null->channel_layout;
  96. samplesref->sample_rate = outlink->sample_rate;
  97. ret = ff_filter_frame(outlink, av_frame_clone(samplesref));
  98. av_frame_free(&samplesref);
  99. if (ret < 0)
  100. return ret;
  101. null->pts += null->nb_samples;
  102. return ret;
  103. }
  104. static const AVFilterPad avfilter_asrc_anullsrc_outputs[] = {
  105. {
  106. .name = "default",
  107. .type = AVMEDIA_TYPE_AUDIO,
  108. .config_props = config_props,
  109. .request_frame = request_frame,
  110. },
  111. { NULL }
  112. };
  113. AVFilter ff_asrc_anullsrc = {
  114. .name = "anullsrc",
  115. .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
  116. .init = init,
  117. .query_formats = query_formats,
  118. .priv_size = sizeof(ANullContext),
  119. .inputs = NULL,
  120. .outputs = avfilter_asrc_anullsrc_outputs,
  121. .priv_class = &anullsrc_class,
  122. };