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.

117 lines
3.3KB

  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. int64_t next_pts;
  37. } APadContext;
  38. static av_cold int init(AVFilterContext *ctx, const char *args)
  39. {
  40. APadContext *apad = ctx->priv;
  41. apad->next_pts = AV_NOPTS_VALUE;
  42. return 0;
  43. }
  44. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
  45. {
  46. AVFilterContext *ctx = inlink->dst;
  47. APadContext *apad = ctx->priv;
  48. apad->next_pts = frame->pts + av_rescale_q(frame->audio->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
  49. return ff_filter_frame(ctx->outputs[0], frame);
  50. }
  51. static int request_frame(AVFilterLink *outlink)
  52. {
  53. AVFilterContext *ctx = outlink->src;
  54. APadContext *apad = ctx->priv;
  55. int ret;
  56. ret = ff_request_frame(ctx->inputs[0]);
  57. if (ret == AVERROR_EOF) {
  58. int n_out = 4096;
  59. AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
  60. if (!outsamplesref)
  61. return AVERROR(ENOMEM);
  62. av_assert0(outsamplesref->audio->sample_rate == outlink->sample_rate);
  63. av_assert0(outsamplesref->audio->nb_samples == n_out);
  64. av_samples_set_silence(outsamplesref->extended_data, 0,
  65. n_out,
  66. outsamplesref->audio->channels,
  67. outsamplesref->format);
  68. outsamplesref->pts = apad->next_pts;
  69. if (apad->next_pts != AV_NOPTS_VALUE)
  70. apad->next_pts += av_rescale_q(n_out, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  71. return ff_filter_frame(outlink, outsamplesref);
  72. }
  73. return ret;
  74. }
  75. static const AVFilterPad apad_inputs[] = {
  76. {
  77. .name = "default",
  78. .type = AVMEDIA_TYPE_AUDIO,
  79. .filter_frame = filter_frame,
  80. .min_perms = AV_PERM_READ,
  81. },
  82. { NULL },
  83. };
  84. static const AVFilterPad apad_outputs[] = {
  85. {
  86. .name = "default",
  87. .request_frame = request_frame,
  88. .type = AVMEDIA_TYPE_AUDIO,
  89. },
  90. { NULL },
  91. };
  92. AVFilter avfilter_af_apad = {
  93. .name = "apad",
  94. .description = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
  95. .init = init,
  96. .priv_size = sizeof(APadContext),
  97. .inputs = apad_inputs,
  98. .outputs = apad_outputs,
  99. };