af_asetnsamples.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2012 Andrey Utkin
  3. * Copyright (c) 2012 Stefano Sabatini
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Filter that changes number of samples on single output operation
  24. */
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/opt.h"
  28. #include "avfilter.h"
  29. #include "audio.h"
  30. #include "filters.h"
  31. #include "internal.h"
  32. #include "formats.h"
  33. typedef struct ASNSContext {
  34. const AVClass *class;
  35. int nb_out_samples; ///< how many samples to output
  36. int pad;
  37. } ASNSContext;
  38. #define OFFSET(x) offsetof(ASNSContext, x)
  39. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  40. static const AVOption asetnsamples_options[] = {
  41. { "nb_out_samples", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
  42. { "n", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
  43. { "pad", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  44. { "p", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  45. { NULL }
  46. };
  47. AVFILTER_DEFINE_CLASS(asetnsamples);
  48. static int activate(AVFilterContext *ctx)
  49. {
  50. AVFilterLink *inlink = ctx->inputs[0];
  51. AVFilterLink *outlink = ctx->outputs[0];
  52. ASNSContext *s = ctx->priv;
  53. AVFrame *frame = NULL, *pad_frame;
  54. int ret;
  55. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  56. ret = ff_inlink_consume_samples(inlink, s->nb_out_samples, s->nb_out_samples, &frame);
  57. if (ret < 0)
  58. return ret;
  59. if (ret > 0) {
  60. if (!s->pad || frame->nb_samples == s->nb_out_samples) {
  61. ret = ff_filter_frame(outlink, frame);
  62. if (ff_inlink_queued_samples(inlink) >= s->nb_out_samples)
  63. ff_filter_set_ready(ctx, 100);
  64. return ret;
  65. }
  66. pad_frame = ff_get_audio_buffer(outlink, s->nb_out_samples);
  67. if (!pad_frame) {
  68. av_frame_free(&frame);
  69. return AVERROR(ENOMEM);
  70. }
  71. ret = av_frame_copy_props(pad_frame, frame);
  72. if (ret < 0) {
  73. av_frame_free(&pad_frame);
  74. av_frame_free(&frame);
  75. return ret;
  76. }
  77. av_samples_copy(pad_frame->extended_data, frame->extended_data,
  78. 0, 0, frame->nb_samples, frame->channels, frame->format);
  79. av_samples_set_silence(pad_frame->extended_data, frame->nb_samples,
  80. s->nb_out_samples - frame->nb_samples, frame->channels,
  81. frame->format);
  82. av_frame_free(&frame);
  83. return ff_filter_frame(outlink, pad_frame);
  84. }
  85. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  86. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  87. return FFERROR_NOT_READY;
  88. }
  89. static const AVFilterPad asetnsamples_inputs[] = {
  90. {
  91. .name = "default",
  92. .type = AVMEDIA_TYPE_AUDIO,
  93. },
  94. { NULL }
  95. };
  96. static const AVFilterPad asetnsamples_outputs[] = {
  97. {
  98. .name = "default",
  99. .type = AVMEDIA_TYPE_AUDIO,
  100. },
  101. { NULL }
  102. };
  103. AVFilter ff_af_asetnsamples = {
  104. .name = "asetnsamples",
  105. .description = NULL_IF_CONFIG_SMALL("Set the number of samples for each output audio frames."),
  106. .priv_size = sizeof(ASNSContext),
  107. .priv_class = &asetnsamples_class,
  108. .inputs = asetnsamples_inputs,
  109. .outputs = asetnsamples_outputs,
  110. .activate = activate,
  111. };