buffersink.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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/internal.h"
  29. #include "libavutil/mathematics.h"
  30. #include "audio.h"
  31. #include "avfilter.h"
  32. #include "buffersink.h"
  33. #include "internal.h"
  34. typedef struct BufferSinkContext {
  35. AVFrame *cur_frame; ///< last frame delivered on the sink
  36. AVAudioFifo *audio_fifo; ///< FIFO for audio samples
  37. int64_t next_pts; ///< interpolating audio pts
  38. } BufferSinkContext;
  39. static av_cold void uninit(AVFilterContext *ctx)
  40. {
  41. BufferSinkContext *sink = ctx->priv;
  42. if (sink->audio_fifo)
  43. av_audio_fifo_free(sink->audio_fifo);
  44. }
  45. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  46. {
  47. BufferSinkContext *s = link->dst->priv;
  48. av_assert0(!s->cur_frame);
  49. s->cur_frame = frame;
  50. return 0;
  51. }
  52. int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx,
  53. AVFrame *frame)
  54. {
  55. BufferSinkContext *s = ctx->priv;
  56. AVFilterLink *link = ctx->inputs[0];
  57. int ret;
  58. if ((ret = ff_request_frame(link)) < 0)
  59. return ret;
  60. if (!s->cur_frame)
  61. return AVERROR(EINVAL);
  62. av_frame_move_ref(frame, s->cur_frame);
  63. av_frame_free(&s->cur_frame);
  64. return 0;
  65. }
  66. static int read_from_fifo(AVFilterContext *ctx, AVFrame *frame,
  67. int nb_samples)
  68. {
  69. BufferSinkContext *s = ctx->priv;
  70. AVFilterLink *link = ctx->inputs[0];
  71. AVFrame *tmp;
  72. if (!(tmp = ff_get_audio_buffer(link, nb_samples)))
  73. return AVERROR(ENOMEM);
  74. av_audio_fifo_read(s->audio_fifo, (void**)tmp->extended_data, nb_samples);
  75. tmp->pts = s->next_pts;
  76. s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
  77. link->time_base);
  78. av_frame_move_ref(frame, tmp);
  79. av_frame_free(&tmp);
  80. return 0;
  81. }
  82. int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx,
  83. AVFrame *frame, 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. if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
  95. return read_from_fifo(ctx, frame, nb_samples);
  96. ret = ff_request_frame(link);
  97. if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo))
  98. return read_from_fifo(ctx, frame, av_audio_fifo_size(s->audio_fifo));
  99. else if (ret < 0)
  100. return ret;
  101. if (s->cur_frame->pts != AV_NOPTS_VALUE) {
  102. s->next_pts = s->cur_frame->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**)s->cur_frame->extended_data,
  108. s->cur_frame->nb_samples);
  109. av_frame_free(&s->cur_frame);
  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. .needs_fifo = 1
  119. },
  120. { NULL }
  121. };
  122. AVFilter ff_vsink_buffer = {
  123. .name = "buffersink",
  124. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  125. .priv_size = sizeof(BufferSinkContext),
  126. .uninit = uninit,
  127. .inputs = avfilter_vsink_buffer_inputs,
  128. .outputs = NULL,
  129. };
  130. static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
  131. {
  132. .name = "default",
  133. .type = AVMEDIA_TYPE_AUDIO,
  134. .filter_frame = filter_frame,
  135. .needs_fifo = 1
  136. },
  137. { NULL }
  138. };
  139. AVFilter ff_asink_abuffer = {
  140. .name = "abuffersink",
  141. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  142. .priv_size = sizeof(BufferSinkContext),
  143. .uninit = uninit,
  144. .inputs = avfilter_asink_abuffer_inputs,
  145. .outputs = NULL,
  146. };