buffersink.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 int 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. return 0;
  50. };
  51. int av_buffersink_read(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 av_buffersink_read_samples(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. AVFilter avfilter_vsink_buffer = {
  114. .name = "buffersink_old",
  115. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  116. .priv_size = sizeof(BufferSinkContext),
  117. .uninit = uninit,
  118. .inputs = (const AVFilterPad[]) {{ .name = "default",
  119. .type = AVMEDIA_TYPE_VIDEO,
  120. .start_frame = start_frame,
  121. .min_perms = AV_PERM_READ,
  122. .needs_fifo = 1 },
  123. { .name = NULL }},
  124. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  125. };
  126. AVFilter avfilter_asink_abuffer = {
  127. .name = "abuffersink_old",
  128. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  129. .priv_size = sizeof(BufferSinkContext),
  130. .uninit = uninit,
  131. .inputs = (const AVFilterPad[]) {{ .name = "default",
  132. .type = AVMEDIA_TYPE_AUDIO,
  133. .filter_samples = start_frame,
  134. .min_perms = AV_PERM_READ,
  135. .needs_fifo = 1 },
  136. { .name = NULL }},
  137. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  138. };