af_apad.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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, pad_len_left;
  39. int64_t whole_len, whole_len_left;
  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", "set number of samples of silence to add", OFFSET(pad_len), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, A },
  46. { "whole_len", "set minimum target number of samples in the audio stream", OFFSET(whole_len), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, 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 >= 0 && apad->pad_len >= 0) {
  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. apad->pad_len_left = apad->pad_len;
  59. apad->whole_len_left = apad->whole_len;
  60. return 0;
  61. }
  62. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  63. {
  64. AVFilterContext *ctx = inlink->dst;
  65. APadContext *apad = ctx->priv;
  66. if (apad->whole_len >= 0) {
  67. apad->whole_len_left = FFMAX(apad->whole_len_left - frame->nb_samples, 0);
  68. av_log(ctx, AV_LOG_DEBUG,
  69. "n_out:%d whole_len_left:%"PRId64"\n", frame->nb_samples, apad->whole_len_left);
  70. }
  71. apad->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
  72. return ff_filter_frame(ctx->outputs[0], frame);
  73. }
  74. static int request_frame(AVFilterLink *outlink)
  75. {
  76. AVFilterContext *ctx = outlink->src;
  77. APadContext *apad = ctx->priv;
  78. int ret;
  79. ret = ff_request_frame(ctx->inputs[0]);
  80. if (ret == AVERROR_EOF && !ctx->is_disabled) {
  81. int n_out = apad->packet_size;
  82. AVFrame *outsamplesref;
  83. if (apad->whole_len >= 0 && apad->pad_len < 0) {
  84. apad->pad_len = apad->pad_len_left = apad->whole_len_left;
  85. }
  86. if (apad->pad_len >=0 || apad->whole_len >= 0) {
  87. n_out = FFMIN(n_out, apad->pad_len_left);
  88. apad->pad_len_left -= n_out;
  89. av_log(ctx, AV_LOG_DEBUG,
  90. "padding n_out:%d pad_len_left:%"PRId64"\n", n_out, apad->pad_len_left);
  91. }
  92. if (!n_out)
  93. return AVERROR_EOF;
  94. outsamplesref = ff_get_audio_buffer(outlink, n_out);
  95. if (!outsamplesref)
  96. return AVERROR(ENOMEM);
  97. av_assert0(outsamplesref->sample_rate == outlink->sample_rate);
  98. av_assert0(outsamplesref->nb_samples == n_out);
  99. av_samples_set_silence(outsamplesref->extended_data, 0,
  100. n_out,
  101. av_frame_get_channels(outsamplesref),
  102. outsamplesref->format);
  103. outsamplesref->pts = apad->next_pts;
  104. if (apad->next_pts != AV_NOPTS_VALUE)
  105. apad->next_pts += av_rescale_q(n_out, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  106. return ff_filter_frame(outlink, outsamplesref);
  107. }
  108. return ret;
  109. }
  110. static const AVFilterPad apad_inputs[] = {
  111. {
  112. .name = "default",
  113. .type = AVMEDIA_TYPE_AUDIO,
  114. .filter_frame = filter_frame,
  115. },
  116. { NULL }
  117. };
  118. static const AVFilterPad apad_outputs[] = {
  119. {
  120. .name = "default",
  121. .request_frame = request_frame,
  122. .type = AVMEDIA_TYPE_AUDIO,
  123. },
  124. { NULL }
  125. };
  126. AVFilter ff_af_apad = {
  127. .name = "apad",
  128. .description = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
  129. .init = init,
  130. .priv_size = sizeof(APadContext),
  131. .inputs = apad_inputs,
  132. .outputs = apad_outputs,
  133. .priv_class = &apad_class,
  134. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  135. };