buffersink.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/fifo.h"
  25. #include "avfilter.h"
  26. #include "buffersink.h"
  27. typedef struct {
  28. AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
  29. } BufferSinkContext;
  30. #define FIFO_INIT_SIZE 8
  31. static av_cold void uninit(AVFilterContext *ctx)
  32. {
  33. BufferSinkContext *sink = ctx->priv;
  34. while (sink->fifo && av_fifo_size(sink->fifo)) {
  35. AVFilterBufferRef *buf;
  36. av_fifo_generic_read(sink->fifo, &buf, sizeof(buf), NULL);
  37. avfilter_unref_buffer(buf);
  38. }
  39. av_fifo_free(sink->fifo);
  40. }
  41. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  42. {
  43. BufferSinkContext *sink = ctx->priv;
  44. if (!(sink->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef*)))) {
  45. av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
  46. return AVERROR(ENOMEM);
  47. }
  48. return 0;
  49. }
  50. static void end_frame(AVFilterLink *link)
  51. {
  52. AVFilterContext *ctx = link->dst;
  53. BufferSinkContext *sink = ctx->priv;
  54. if (av_fifo_space(sink->fifo) < sizeof(AVFilterBufferRef *) &&
  55. (av_fifo_realloc2(sink->fifo, av_fifo_size(sink->fifo) * 2) < 0)) {
  56. av_log(ctx, AV_LOG_ERROR, "Error reallocating the FIFO.\n");
  57. return;
  58. }
  59. av_fifo_generic_write(sink->fifo, &link->cur_buf, sizeof(link->cur_buf), NULL);
  60. link->cur_buf = NULL;
  61. }
  62. int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
  63. {
  64. BufferSinkContext *sink = ctx->priv;
  65. AVFilterLink *link = ctx->inputs[0];
  66. int ret;
  67. if (!buf) {
  68. if (av_fifo_size(sink->fifo))
  69. return av_fifo_size(sink->fifo)/sizeof(*buf);
  70. else
  71. return avfilter_poll_frame(ctx->inputs[0]);
  72. }
  73. if (!av_fifo_size(sink->fifo) &&
  74. (ret = avfilter_request_frame(link)) < 0)
  75. return ret;
  76. if (!av_fifo_size(sink->fifo))
  77. return AVERROR(EINVAL);
  78. av_fifo_generic_read(sink->fifo, buf, sizeof(*buf), NULL);
  79. return 0;
  80. }
  81. AVFilter avfilter_vsink_buffer = {
  82. .name = "buffersink",
  83. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  84. .priv_size = sizeof(BufferSinkContext),
  85. .init = init,
  86. .uninit = uninit,
  87. .inputs = (AVFilterPad[]) {{ .name = "default",
  88. .type = AVMEDIA_TYPE_VIDEO,
  89. .end_frame = end_frame,
  90. .min_perms = AV_PERM_READ, },
  91. { .name = NULL }},
  92. .outputs = (AVFilterPad[]) {{ .name = NULL }},
  93. };