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.

140 lines
4.2KB

  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 video sink
  23. */
  24. #include "libavutil/fifo.h"
  25. #include "avfilter.h"
  26. #include "vsink_buffer.h"
  27. typedef struct {
  28. AVFifoBuffer *fifo; ///< FIFO buffer of video frame references
  29. enum PixelFormat *pix_fmts; ///< accepted pixel formats, must be terminated with -1
  30. } BufferSinkContext;
  31. #define FIFO_INIT_SIZE 8
  32. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  33. {
  34. BufferSinkContext *buf = ctx->priv;
  35. if (!opaque) {
  36. av_log(ctx, AV_LOG_ERROR, "No opaque field provided, which is required.\n");
  37. return AVERROR(EINVAL);
  38. }
  39. buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
  40. if (!buf->fifo) {
  41. av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
  42. return AVERROR(ENOMEM);
  43. }
  44. buf->pix_fmts = opaque;
  45. return 0;
  46. }
  47. static av_cold void uninit(AVFilterContext *ctx)
  48. {
  49. BufferSinkContext *buf = ctx->priv;
  50. AVFilterBufferRef *picref;
  51. if (buf->fifo) {
  52. while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
  53. av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
  54. avfilter_unref_buffer(picref);
  55. }
  56. av_fifo_free(buf->fifo);
  57. buf->fifo = NULL;
  58. }
  59. }
  60. static void end_frame(AVFilterLink *inlink)
  61. {
  62. AVFilterContext *ctx = inlink->dst;
  63. BufferSinkContext *buf = inlink->dst->priv;
  64. if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
  65. /* realloc fifo size */
  66. if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
  67. av_log(ctx, AV_LOG_ERROR,
  68. "Cannot buffer more frames. Consume some available frames "
  69. "before adding new ones.\n");
  70. return;
  71. }
  72. }
  73. /* cache frame */
  74. av_fifo_generic_write(buf->fifo,
  75. &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
  76. }
  77. static int query_formats(AVFilterContext *ctx)
  78. {
  79. BufferSinkContext *buf = ctx->priv;
  80. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pix_fmts));
  81. return 0;
  82. }
  83. int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
  84. AVFilterBufferRef **picref, int flags)
  85. {
  86. BufferSinkContext *buf = ctx->priv;
  87. AVFilterLink *inlink = ctx->inputs[0];
  88. int ret;
  89. *picref = NULL;
  90. /* no picref available, fetch it from the filterchain */
  91. if (!av_fifo_size(buf->fifo)) {
  92. if ((ret = avfilter_request_frame(inlink)) < 0)
  93. return ret;
  94. }
  95. if (!av_fifo_size(buf->fifo))
  96. return AVERROR(EINVAL);
  97. if (flags & AV_VSINK_BUF_FLAG_PEEK)
  98. *picref = (AVFilterBufferRef *)av_fifo_peek2(buf->fifo, 0);
  99. else
  100. av_fifo_generic_read(buf->fifo, picref, sizeof(*picref), NULL);
  101. return 0;
  102. }
  103. AVFilter avfilter_vsink_buffersink = {
  104. .name = "buffersink",
  105. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  106. .priv_size = sizeof(BufferSinkContext),
  107. .init = init,
  108. .uninit = uninit,
  109. .query_formats = query_formats,
  110. .inputs = (AVFilterPad[]) {{ .name = "default",
  111. .type = AVMEDIA_TYPE_VIDEO,
  112. .end_frame = end_frame,
  113. .min_perms = AV_PERM_READ, },
  114. { .name = NULL }},
  115. .outputs = (AVFilterPad[]) {{ .name = NULL }},
  116. };