af_apad.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2012 Michael Niedermayer
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * audio pad filter.
  23. *
  24. * Based on af_aresample.c
  25. */
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/channel_layout.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/samplefmt.h"
  30. #include "libavutil/avassert.h"
  31. #include "avfilter.h"
  32. #include "audio.h"
  33. #include "internal.h"
  34. typedef struct {
  35. const AVClass *class;
  36. int64_t next_pts;
  37. int packet_size;
  38. int64_t pad_len;
  39. int64_t whole_len;
  40. } APadContext;
  41. #define OFFSET(x) offsetof(APadContext, x)
  42. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  43. static const AVOption apad_options[] = {
  44. { "packet_size", "set silence packet size", OFFSET(packet_size), AV_OPT_TYPE_INT, { .i64 = 4096 }, 0, INT_MAX, A },
  45. { "pad_len", "number of samples of silence to add", OFFSET(pad_len), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, A },
  46. { "whole_len", "target number of samples in the audio stream", OFFSET(whole_len), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, A },
  47. { NULL }
  48. };
  49. AVFILTER_DEFINE_CLASS(apad);
  50. static av_cold int init(AVFilterContext *ctx)
  51. {
  52. APadContext *apad = ctx->priv;
  53. apad->next_pts = AV_NOPTS_VALUE;
  54. if (apad->whole_len && apad->pad_len) {
  55. av_log(ctx, AV_LOG_ERROR, "Both whole and pad length are set, this is not possible\n");
  56. return AVERROR(EINVAL);
  57. }
  58. return 0;
  59. }
  60. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  61. {
  62. AVFilterContext *ctx = inlink->dst;
  63. APadContext *apad = ctx->priv;
  64. if (apad->whole_len)
  65. apad->whole_len -= frame->nb_samples;
  66. apad->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
  67. return ff_filter_frame(ctx->outputs[0], frame);
  68. }
  69. static int request_frame(AVFilterLink *outlink)
  70. {
  71. AVFilterContext *ctx = outlink->src;
  72. APadContext *apad = ctx->priv;
  73. int ret;
  74. ret = ff_request_frame(ctx->inputs[0]);
  75. if (ret == AVERROR_EOF && !ctx->is_disabled) {
  76. int n_out = apad->packet_size;
  77. AVFrame *outsamplesref;
  78. if (apad->whole_len > 0) {
  79. apad->pad_len = apad->whole_len;
  80. apad->whole_len = 0;
  81. }
  82. if (apad->pad_len > 0) {
  83. n_out = FFMIN(n_out, apad->pad_len);
  84. apad->pad_len -= n_out;
  85. }
  86. if(!n_out)
  87. return AVERROR_EOF;
  88. outsamplesref = ff_get_audio_buffer(outlink, n_out);
  89. if (!outsamplesref)
  90. return AVERROR(ENOMEM);
  91. av_assert0(outsamplesref->sample_rate == outlink->sample_rate);
  92. av_assert0(outsamplesref->nb_samples == n_out);
  93. av_samples_set_silence(outsamplesref->extended_data, 0,
  94. n_out,
  95. av_frame_get_channels(outsamplesref),
  96. outsamplesref->format);
  97. outsamplesref->pts = apad->next_pts;
  98. if (apad->next_pts != AV_NOPTS_VALUE)
  99. apad->next_pts += av_rescale_q(n_out, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  100. return ff_filter_frame(outlink, outsamplesref);
  101. }
  102. return ret;
  103. }
  104. static const AVFilterPad apad_inputs[] = {
  105. {
  106. .name = "default",
  107. .type = AVMEDIA_TYPE_AUDIO,
  108. .filter_frame = filter_frame,
  109. },
  110. { NULL }
  111. };
  112. static const AVFilterPad apad_outputs[] = {
  113. {
  114. .name = "default",
  115. .request_frame = request_frame,
  116. .type = AVMEDIA_TYPE_AUDIO,
  117. },
  118. { NULL }
  119. };
  120. AVFilter ff_af_apad = {
  121. .name = "apad",
  122. .description = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
  123. .init = init,
  124. .priv_size = sizeof(APadContext),
  125. .inputs = apad_inputs,
  126. .outputs = apad_outputs,
  127. .priv_class = &apad_class,
  128. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  129. };