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.

252 lines
7.2KB

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