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.

191 lines
5.4KB

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