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.

249 lines
7.0KB

  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/mathematics.h"
  29. #include "audio.h"
  30. #include "avfilter.h"
  31. #include "buffersink.h"
  32. #include "internal.h"
  33. typedef struct {
  34. AVFrame *cur_frame; ///< last frame 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, AVFrame *frame)
  45. {
  46. BufferSinkContext *s = link->dst->priv;
  47. av_assert0(!s->cur_frame);
  48. s->cur_frame = frame;
  49. return 0;
  50. }
  51. int av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
  52. {
  53. BufferSinkContext *s = ctx->priv;
  54. AVFilterLink *link = ctx->inputs[0];
  55. int ret;
  56. if ((ret = ff_request_frame(link)) < 0)
  57. return ret;
  58. if (!s->cur_frame)
  59. return AVERROR(EINVAL);
  60. av_frame_move_ref(frame, s->cur_frame);
  61. av_frame_free(&s->cur_frame);
  62. return 0;
  63. }
  64. static int read_from_fifo(AVFilterContext *ctx, AVFrame *frame,
  65. int nb_samples)
  66. {
  67. BufferSinkContext *s = ctx->priv;
  68. AVFilterLink *link = ctx->inputs[0];
  69. AVFrame *tmp;
  70. if (!(tmp = ff_get_audio_buffer(link, nb_samples)))
  71. return AVERROR(ENOMEM);
  72. av_audio_fifo_read(s->audio_fifo, (void**)tmp->extended_data, nb_samples);
  73. tmp->pts = s->next_pts;
  74. s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
  75. link->time_base);
  76. av_frame_move_ref(frame, tmp);
  77. av_frame_free(&tmp);
  78. return 0;
  79. }
  80. int av_buffersink_get_samples(AVFilterContext *ctx, AVFrame *frame, int nb_samples)
  81. {
  82. BufferSinkContext *s = ctx->priv;
  83. AVFilterLink *link = ctx->inputs[0];
  84. int ret = 0;
  85. if (!s->audio_fifo) {
  86. int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  87. if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples)))
  88. return AVERROR(ENOMEM);
  89. }
  90. while (ret >= 0) {
  91. if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
  92. return read_from_fifo(ctx, frame, nb_samples);
  93. ret = ff_request_frame(link);
  94. if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo))
  95. return read_from_fifo(ctx, frame, av_audio_fifo_size(s->audio_fifo));
  96. else if (ret < 0)
  97. return ret;
  98. if (s->cur_frame->pts != AV_NOPTS_VALUE) {
  99. s->next_pts = s->cur_frame->pts -
  100. av_rescale_q(av_audio_fifo_size(s->audio_fifo),
  101. (AVRational){ 1, link->sample_rate },
  102. link->time_base);
  103. }
  104. ret = av_audio_fifo_write(s->audio_fifo, (void**)s->cur_frame->extended_data,
  105. s->cur_frame->nb_samples);
  106. av_frame_free(&s->cur_frame);
  107. }
  108. return ret;
  109. }
  110. #if FF_API_AVFILTERBUFFER
  111. static void compat_free_buffer(AVFilterBuffer *buf)
  112. {
  113. AVFrame *frame = buf->priv;
  114. av_frame_free(&frame);
  115. av_free(buf);
  116. }
  117. static int compat_read(AVFilterContext *ctx, AVFilterBufferRef **pbuf, int nb_samples)
  118. {
  119. AVFilterBufferRef *buf;
  120. AVFrame *frame;
  121. int ret;
  122. if (!pbuf)
  123. return ff_poll_frame(ctx->inputs[0]);
  124. frame = av_frame_alloc();
  125. if (!frame)
  126. return AVERROR(ENOMEM);
  127. if (!nb_samples)
  128. ret = av_buffersink_get_frame(ctx, frame);
  129. else
  130. ret = av_buffersink_get_samples(ctx, frame, nb_samples);
  131. if (ret < 0)
  132. goto fail;
  133. if (ctx->inputs[0]->type == AVMEDIA_TYPE_VIDEO) {
  134. buf = avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize,
  135. AV_PERM_READ,
  136. frame->width, frame->height,
  137. frame->format);
  138. } else {
  139. buf = avfilter_get_audio_buffer_ref_from_arrays(frame->extended_data,
  140. frame->linesize[0], AV_PERM_READ,
  141. frame->nb_samples,
  142. frame->format,
  143. frame->channel_layout);
  144. }
  145. if (!buf) {
  146. ret = AVERROR(ENOMEM);
  147. goto fail;
  148. }
  149. avfilter_copy_frame_props(buf, frame);
  150. buf->buf->priv = frame;
  151. buf->buf->free = compat_free_buffer;
  152. *pbuf = buf;
  153. return 0;
  154. fail:
  155. av_frame_free(&frame);
  156. return ret;
  157. }
  158. int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
  159. {
  160. return compat_read(ctx, buf, 0);
  161. }
  162. int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
  163. int nb_samples)
  164. {
  165. return compat_read(ctx, buf, nb_samples);
  166. }
  167. #endif
  168. static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
  169. {
  170. .name = "default",
  171. .type = AVMEDIA_TYPE_VIDEO,
  172. .filter_frame = filter_frame,
  173. .needs_fifo = 1
  174. },
  175. { NULL }
  176. };
  177. AVFilter avfilter_vsink_buffer = {
  178. .name = "buffersink",
  179. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  180. .priv_size = sizeof(BufferSinkContext),
  181. .uninit = uninit,
  182. .inputs = avfilter_vsink_buffer_inputs,
  183. .outputs = NULL,
  184. };
  185. static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
  186. {
  187. .name = "default",
  188. .type = AVMEDIA_TYPE_AUDIO,
  189. .filter_frame = filter_frame,
  190. .needs_fifo = 1
  191. },
  192. { NULL }
  193. };
  194. AVFilter avfilter_asink_abuffer = {
  195. .name = "abuffersink",
  196. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  197. .priv_size = sizeof(BufferSinkContext),
  198. .uninit = uninit,
  199. .inputs = avfilter_asink_abuffer_inputs,
  200. .outputs = NULL,
  201. };