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.

253 lines
7.3KB

  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 {
  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. #if FF_API_AVFILTERBUFFER
  114. FF_DISABLE_DEPRECATION_WARNINGS
  115. static void compat_free_buffer(AVFilterBuffer *buf)
  116. {
  117. AVFrame *frame = buf->priv;
  118. av_frame_free(&frame);
  119. av_free(buf);
  120. }
  121. static int compat_read(AVFilterContext *ctx,
  122. AVFilterBufferRef **pbuf, int nb_samples)
  123. {
  124. AVFilterBufferRef *buf;
  125. AVFrame *frame;
  126. int ret;
  127. if (!pbuf)
  128. return ff_poll_frame(ctx->inputs[0]);
  129. frame = av_frame_alloc();
  130. if (!frame)
  131. return AVERROR(ENOMEM);
  132. if (!nb_samples)
  133. ret = av_buffersink_get_frame(ctx, frame);
  134. else
  135. ret = av_buffersink_get_samples(ctx, frame, nb_samples);
  136. if (ret < 0)
  137. goto fail;
  138. if (ctx->inputs[0]->type == AVMEDIA_TYPE_VIDEO) {
  139. buf = avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize,
  140. AV_PERM_READ,
  141. frame->width, frame->height,
  142. frame->format);
  143. } else {
  144. buf = avfilter_get_audio_buffer_ref_from_arrays(frame->extended_data,
  145. frame->linesize[0], AV_PERM_READ,
  146. frame->nb_samples,
  147. frame->format,
  148. frame->channel_layout);
  149. }
  150. if (!buf) {
  151. ret = AVERROR(ENOMEM);
  152. goto fail;
  153. }
  154. avfilter_copy_frame_props(buf, frame);
  155. buf->buf->priv = frame;
  156. buf->buf->free = compat_free_buffer;
  157. *pbuf = buf;
  158. return 0;
  159. fail:
  160. av_frame_free(&frame);
  161. return ret;
  162. }
  163. int attribute_align_arg av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
  164. {
  165. return compat_read(ctx, buf, 0);
  166. }
  167. int attribute_align_arg av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
  168. int nb_samples)
  169. {
  170. return compat_read(ctx, buf, nb_samples);
  171. }
  172. FF_ENABLE_DEPRECATION_WARNINGS
  173. #endif
  174. static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
  175. {
  176. .name = "default",
  177. .type = AVMEDIA_TYPE_VIDEO,
  178. .filter_frame = filter_frame,
  179. .needs_fifo = 1
  180. },
  181. { NULL }
  182. };
  183. AVFilter ff_vsink_buffer = {
  184. .name = "buffersink",
  185. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  186. .priv_size = sizeof(BufferSinkContext),
  187. .uninit = uninit,
  188. .inputs = avfilter_vsink_buffer_inputs,
  189. .outputs = NULL,
  190. };
  191. static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
  192. {
  193. .name = "default",
  194. .type = AVMEDIA_TYPE_AUDIO,
  195. .filter_frame = filter_frame,
  196. .needs_fifo = 1
  197. },
  198. { NULL }
  199. };
  200. AVFilter ff_asink_abuffer = {
  201. .name = "abuffersink",
  202. .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
  203. .priv_size = sizeof(BufferSinkContext),
  204. .uninit = uninit,
  205. .inputs = avfilter_asink_abuffer_inputs,
  206. .outputs = NULL,
  207. };