buffersink.c 5.6 KB

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