af_apad.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /**
  22. * @file
  23. * audio pad filter.
  24. *
  25. * Based on af_aresample.c
  26. */
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/channel_layout.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/samplefmt.h"
  31. #include "libavutil/avassert.h"
  32. #include "avfilter.h"
  33. #include "audio.h"
  34. #include "internal.h"
  35. typedef struct {
  36. const AVClass *class;
  37. int64_t next_pts;
  38. int packet_size;
  39. int64_t pad_len;
  40. int64_t whole_len;
  41. } APadContext;
  42. #define OFFSET(x) offsetof(APadContext, x)
  43. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  44. static const AVOption apad_options[] = {
  45. { "packet_size", "set silence packet size", OFFSET(packet_size), AV_OPT_TYPE_INT, { .i64 = 4096 }, 0, INT_MAX, A },
  46. { "pad_len", "number of samples of silence to add", OFFSET(pad_len), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, A },
  47. { "whole_len", "target number of samples in the audio stream", OFFSET(whole_len), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, A },
  48. { NULL },
  49. };
  50. AVFILTER_DEFINE_CLASS(apad);
  51. static av_cold int init(AVFilterContext *ctx, const char *args)
  52. {
  53. int ret;
  54. APadContext *apad = ctx->priv;
  55. apad->class = &apad_class;
  56. apad->next_pts = AV_NOPTS_VALUE;
  57. av_opt_set_defaults(apad);
  58. if ((ret = av_opt_set_from_string(apad, args, NULL, "=", ":")) < 0)
  59. return ret;
  60. if (apad->whole_len && apad->pad_len) {
  61. av_log(ctx, AV_LOG_ERROR, "Both whole and pad length are set, this is not possible\n");
  62. return AVERROR(EINVAL);
  63. }
  64. return 0;
  65. }
  66. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
  67. {
  68. AVFilterContext *ctx = inlink->dst;
  69. APadContext *apad = ctx->priv;
  70. if (apad->whole_len)
  71. apad->whole_len -= frame->audio->nb_samples;
  72. apad->next_pts = frame->pts + av_rescale_q(frame->audio->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
  73. return ff_filter_frame(ctx->outputs[0], frame);
  74. }
  75. static int request_frame(AVFilterLink *outlink)
  76. {
  77. AVFilterContext *ctx = outlink->src;
  78. APadContext *apad = ctx->priv;
  79. int ret;
  80. ret = ff_request_frame(ctx->inputs[0]);
  81. if (ret == AVERROR_EOF) {
  82. int n_out = apad->packet_size;
  83. AVFilterBufferRef *outsamplesref;
  84. if (apad->whole_len > 0) {
  85. apad->pad_len = apad->whole_len;
  86. apad->whole_len = 0;
  87. }
  88. if (apad->pad_len > 0) {
  89. n_out = FFMIN(n_out, apad->pad_len);
  90. apad->pad_len -= n_out;
  91. }
  92. if(!n_out)
  93. return AVERROR_EOF;
  94. outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
  95. if (!outsamplesref)
  96. return AVERROR(ENOMEM);
  97. av_assert0(outsamplesref->audio->sample_rate == outlink->sample_rate);
  98. av_assert0(outsamplesref->audio->nb_samples == n_out);
  99. av_samples_set_silence(outsamplesref->extended_data, 0,
  100. n_out,
  101. outsamplesref->audio->channels,
  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. .min_perms = AV_PERM_READ,
  116. },
  117. { NULL },
  118. };
  119. static const AVFilterPad apad_outputs[] = {
  120. {
  121. .name = "default",
  122. .request_frame = request_frame,
  123. .type = AVMEDIA_TYPE_AUDIO,
  124. },
  125. { NULL },
  126. };
  127. AVFilter avfilter_af_apad = {
  128. .name = "apad",
  129. .description = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
  130. .init = init,
  131. .priv_size = sizeof(APadContext),
  132. .inputs = apad_inputs,
  133. .outputs = apad_outputs,
  134. .priv_class = &apad_class,
  135. };