af_apad.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 APadContext {
  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. int64_t pad_dur;
  41. int64_t whole_dur;
  42. } APadContext;
  43. #define OFFSET(x) offsetof(APadContext, x)
  44. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  45. static const AVOption apad_options[] = {
  46. { "packet_size", "set silence packet size", OFFSET(packet_size), AV_OPT_TYPE_INT, { .i64 = 4096 }, 0, INT_MAX, A },
  47. { "pad_len", "set number of samples of silence to add", OFFSET(pad_len), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, A },
  48. { "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 },
  49. { "pad_dur", "set duration of silence to add", OFFSET(pad_dur), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, A },
  50. { "whole_dur", "set minimum target duration in the audio stream", OFFSET(whole_dur), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, A },
  51. { NULL }
  52. };
  53. AVFILTER_DEFINE_CLASS(apad);
  54. static av_cold int init(AVFilterContext *ctx)
  55. {
  56. APadContext *s = ctx->priv;
  57. s->next_pts = AV_NOPTS_VALUE;
  58. if (s->whole_len >= 0 && s->pad_len >= 0) {
  59. av_log(ctx, AV_LOG_ERROR, "Both whole and pad length are set, this is not possible\n");
  60. return AVERROR(EINVAL);
  61. }
  62. return 0;
  63. }
  64. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  65. {
  66. AVFilterContext *ctx = inlink->dst;
  67. APadContext *s = ctx->priv;
  68. if (s->whole_len >= 0) {
  69. s->whole_len_left = FFMAX(s->whole_len_left - frame->nb_samples, 0);
  70. av_log(ctx, AV_LOG_DEBUG,
  71. "n_out:%d whole_len_left:%"PRId64"\n", frame->nb_samples, s->whole_len_left);
  72. }
  73. s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
  74. return ff_filter_frame(ctx->outputs[0], frame);
  75. }
  76. static int request_frame(AVFilterLink *outlink)
  77. {
  78. AVFilterContext *ctx = outlink->src;
  79. APadContext *s = ctx->priv;
  80. int ret;
  81. ret = ff_request_frame(ctx->inputs[0]);
  82. if (ret == AVERROR_EOF && !ctx->is_disabled) {
  83. int n_out = s->packet_size;
  84. AVFrame *outsamplesref;
  85. if (s->whole_len >= 0 && s->pad_len < 0) {
  86. s->pad_len = s->pad_len_left = s->whole_len_left;
  87. }
  88. if (s->pad_len >=0 || s->whole_len >= 0) {
  89. n_out = FFMIN(n_out, s->pad_len_left);
  90. s->pad_len_left -= n_out;
  91. av_log(ctx, AV_LOG_DEBUG,
  92. "padding n_out:%d pad_len_left:%"PRId64"\n", n_out, s->pad_len_left);
  93. }
  94. if (!n_out)
  95. return AVERROR_EOF;
  96. outsamplesref = ff_get_audio_buffer(outlink, n_out);
  97. if (!outsamplesref)
  98. return AVERROR(ENOMEM);
  99. av_assert0(outsamplesref->sample_rate == outlink->sample_rate);
  100. av_assert0(outsamplesref->nb_samples == n_out);
  101. av_samples_set_silence(outsamplesref->extended_data, 0,
  102. n_out,
  103. outsamplesref->channels,
  104. outsamplesref->format);
  105. outsamplesref->pts = s->next_pts;
  106. if (s->next_pts != AV_NOPTS_VALUE)
  107. s->next_pts += av_rescale_q(n_out, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  108. return ff_filter_frame(outlink, outsamplesref);
  109. }
  110. return ret;
  111. }
  112. static int config_output(AVFilterLink *outlink)
  113. {
  114. AVFilterContext *ctx = outlink->src;
  115. APadContext *s = ctx->priv;
  116. if (s->pad_dur)
  117. s->pad_len = av_rescale(s->pad_dur, outlink->sample_rate, AV_TIME_BASE);
  118. if (s->whole_dur)
  119. s->whole_len = av_rescale(s->whole_dur, outlink->sample_rate, AV_TIME_BASE);
  120. s->pad_len_left = s->pad_len;
  121. s->whole_len_left = s->whole_len;
  122. return 0;
  123. }
  124. static const AVFilterPad apad_inputs[] = {
  125. {
  126. .name = "default",
  127. .type = AVMEDIA_TYPE_AUDIO,
  128. .filter_frame = filter_frame,
  129. },
  130. { NULL }
  131. };
  132. static const AVFilterPad apad_outputs[] = {
  133. {
  134. .name = "default",
  135. .request_frame = request_frame,
  136. .config_props = config_output,
  137. .type = AVMEDIA_TYPE_AUDIO,
  138. },
  139. { NULL }
  140. };
  141. AVFilter ff_af_apad = {
  142. .name = "apad",
  143. .description = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
  144. .init = init,
  145. .priv_size = sizeof(APadContext),
  146. .inputs = apad_inputs,
  147. .outputs = apad_outputs,
  148. .priv_class = &apad_class,
  149. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  150. };