You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.6KB

  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)
  52. {
  53. APadContext *apad = ctx->priv;
  54. apad->next_pts = AV_NOPTS_VALUE;
  55. if (apad->whole_len && apad->pad_len) {
  56. av_log(ctx, AV_LOG_ERROR, "Both whole and pad length are set, this is not possible\n");
  57. return AVERROR(EINVAL);
  58. }
  59. return 0;
  60. }
  61. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  62. {
  63. AVFilterContext *ctx = inlink->dst;
  64. APadContext *apad = ctx->priv;
  65. if (apad->whole_len)
  66. apad->whole_len -= frame->nb_samples;
  67. apad->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
  68. return ff_filter_frame(ctx->outputs[0], frame);
  69. }
  70. static int request_frame(AVFilterLink *outlink)
  71. {
  72. AVFilterContext *ctx = outlink->src;
  73. APadContext *apad = ctx->priv;
  74. int ret;
  75. ret = ff_request_frame(ctx->inputs[0]);
  76. if (ret == AVERROR_EOF && !ctx->is_disabled) {
  77. int n_out = apad->packet_size;
  78. AVFrame *outsamplesref;
  79. if (apad->whole_len > 0) {
  80. apad->pad_len = apad->whole_len;
  81. apad->whole_len = 0;
  82. }
  83. if (apad->pad_len > 0) {
  84. n_out = FFMIN(n_out, apad->pad_len);
  85. apad->pad_len -= n_out;
  86. }
  87. if(!n_out)
  88. return AVERROR_EOF;
  89. outsamplesref = ff_get_audio_buffer(outlink, n_out);
  90. if (!outsamplesref)
  91. return AVERROR(ENOMEM);
  92. av_assert0(outsamplesref->sample_rate == outlink->sample_rate);
  93. av_assert0(outsamplesref->nb_samples == n_out);
  94. av_samples_set_silence(outsamplesref->extended_data, 0,
  95. n_out,
  96. av_frame_get_channels(outsamplesref),
  97. outsamplesref->format);
  98. outsamplesref->pts = apad->next_pts;
  99. if (apad->next_pts != AV_NOPTS_VALUE)
  100. apad->next_pts += av_rescale_q(n_out, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  101. return ff_filter_frame(outlink, outsamplesref);
  102. }
  103. return ret;
  104. }
  105. static const AVFilterPad apad_inputs[] = {
  106. {
  107. .name = "default",
  108. .type = AVMEDIA_TYPE_AUDIO,
  109. .filter_frame = filter_frame,
  110. },
  111. { NULL },
  112. };
  113. static const AVFilterPad apad_outputs[] = {
  114. {
  115. .name = "default",
  116. .request_frame = request_frame,
  117. .type = AVMEDIA_TYPE_AUDIO,
  118. },
  119. { NULL },
  120. };
  121. AVFilter avfilter_af_apad = {
  122. .name = "apad",
  123. .description = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
  124. .init = init,
  125. .priv_size = sizeof(APadContext),
  126. .inputs = apad_inputs,
  127. .outputs = apad_outputs,
  128. .priv_class = &apad_class,
  129. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  130. };