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.

175 lines
5.5KB

  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  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. * buffer sink
  23. */
  24. #include "libavutil/audio_fifo.h"
  25. #include "libavutil/audioconvert.h"
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/mathematics.h"
  28. #include "audio.h"
  29. #include "avfilter.h"
  30. #include "buffersink.h"
  31. #include "internal.h"
  32. typedef struct {
  33. AVFilterBufferRef *cur_buf; ///< last buffer delivered on the sink
  34. AVAudioFifo *audio_fifo; ///< FIFO for audio samples
  35. int64_t next_pts; ///< interpolating audio pts
  36. } BufferSinkContext;
  37. static av_cold void uninit(AVFilterContext *ctx)
  38. {
  39. BufferSinkContext *sink = ctx->priv;
  40. if (sink->audio_fifo)
  41. av_audio_fifo_free(sink->audio_fifo);
  42. }
  43. static void start_frame(AVFilterLink *link, AVFilterBufferRef *buf)
  44. {
  45. BufferSinkContext *s = link->dst->priv;
  46. // av_assert0(!s->cur_buf);
  47. s->cur_buf = buf;
  48. link->cur_buf = NULL;
  49. };
  50. static int filter_samples(AVFilterLink *link, AVFilterBufferRef *buf)
  51. {
  52. start_frame(link, buf);
  53. return 0;
  54. }
  55. int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
  56. {
  57. BufferSinkContext *s = ctx->priv;
  58. AVFilterLink *link = ctx->inputs[0];
  59. int ret;
  60. if (!buf)
  61. return ff_poll_frame(ctx->inputs[0]);
  62. if ((ret = ff_request_frame(link)) < 0)
  63. return ret;
  64. if (!s->cur_buf)
  65. return AVERROR(EINVAL);
  66. *buf = s->cur_buf;
  67. s->cur_buf = NULL;
  68. return 0;
  69. }
  70. static int read_from_fifo(AVFilterContext *ctx, AVFilterBufferRef **pbuf,
  71. int nb_samples)
  72. {
  73. BufferSinkContext *s = ctx->priv;
  74. AVFilterLink *link = ctx->inputs[0];
  75. AVFilterBufferRef *buf;
  76. if (!(buf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples)))
  77. return AVERROR(ENOMEM);
  78. av_audio_fifo_read(s->audio_fifo, (void**)buf->extended_data, nb_samples);
  79. buf->pts = s->next_pts;
  80. s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
  81. link->time_base);
  82. *pbuf = buf;
  83. return 0;
  84. }
  85. int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **pbuf,
  86. int nb_samples)
  87. {
  88. BufferSinkContext *s = ctx->priv;
  89. AVFilterLink *link = ctx->inputs[0];
  90. int ret = 0;
  91. if (!s->audio_fifo) {
  92. int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  93. if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples)))
  94. return AVERROR(ENOMEM);
  95. }
  96. while (ret >= 0) {
  97. AVFilterBufferRef *buf;
  98. if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
  99. return read_from_fifo(ctx, pbuf, nb_samples);
  100. ret = av_buffersink_read(ctx, &buf);
  101. if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo))
  102. return read_from_fifo(ctx, pbuf, av_audio_fifo_size(s->audio_fifo));
  103. else if (ret < 0)
  104. return ret;
  105. if (buf->pts != AV_NOPTS_VALUE) {
  106. s->next_pts = buf->pts -
  107. av_rescale_q(av_audio_fifo_size(s->audio_fifo),
  108. (AVRational){ 1, link->sample_rate },
  109. link->time_base);
  110. }
  111. ret = av_audio_fifo_write(s->audio_fifo, (void**)buf->extended_data,
  112. buf->audio->nb_samples);
  113. avfilter_unref_buffer(buf);
  114. }
  115. return ret;
  116. }
  117. AVFilter avfilter_vsink_buffer = {
  118. .name = "buffersink_old",
  119. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  120. .priv_size = sizeof(BufferSinkContext),
  121. .uninit = uninit,
  122. .inputs = (AVFilterPad[]) {{ .name = "default",
  123. .type = AVMEDIA_TYPE_VIDEO,
  124. .start_frame = start_frame,
  125. .min_perms = AV_PERM_READ,
  126. .needs_fifo = 1 },
  127. { .name = NULL }},
  128. .outputs = (AVFilterPad[]) {{ .name = NULL }},
  129. };
  130. AVFilter avfilter_asink_abuffer = {
  131. .name = "abuffersink_old",
  132. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  133. .priv_size = sizeof(BufferSinkContext),
  134. .uninit = uninit,
  135. .inputs = (AVFilterPad[]) {{ .name = "default",
  136. .type = AVMEDIA_TYPE_AUDIO,
  137. .filter_samples = filter_samples,
  138. .min_perms = AV_PERM_READ,
  139. .needs_fifo = 1 },
  140. { .name = NULL }},
  141. .outputs = (AVFilterPad[]) {{ .name = NULL }},
  142. };