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.

271 lines
7.7KB

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