vsink_buffer.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "avfilter.h"
  25. #include "vsink_buffer.h"
  26. typedef struct {
  27. AVFilterBufferRef *picref; ///< cached picref
  28. enum PixelFormat *pix_fmts; ///< accepted pixel formats, must be terminated with -1
  29. } BufferSinkContext;
  30. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  31. {
  32. BufferSinkContext *buf = ctx->priv;
  33. if (!opaque) {
  34. av_log(ctx, AV_LOG_ERROR, "No opaque field provided, which is required.\n");
  35. return AVERROR(EINVAL);
  36. }
  37. buf->pix_fmts = opaque;
  38. return 0;
  39. }
  40. static av_cold void uninit(AVFilterContext *ctx)
  41. {
  42. BufferSinkContext *buf = ctx->priv;
  43. if (buf->picref)
  44. avfilter_unref_buffer(buf->picref);
  45. buf->picref = NULL;
  46. }
  47. static void end_frame(AVFilterLink *inlink)
  48. {
  49. BufferSinkContext *buf = inlink->dst->priv;
  50. if (buf->picref) /* drop the last cached frame */
  51. avfilter_unref_buffer(buf->picref);
  52. buf->picref = inlink->cur_buf;
  53. }
  54. static int query_formats(AVFilterContext *ctx)
  55. {
  56. BufferSinkContext *buf = ctx->priv;
  57. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pix_fmts));
  58. return 0;
  59. }
  60. int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
  61. AVFilterBufferRef **picref, int flags)
  62. {
  63. BufferSinkContext *buf = ctx->priv;
  64. AVFilterLink *inlink = ctx->inputs[0];
  65. int ret;
  66. *picref = NULL;
  67. /* no picref available, fetch it from the filterchain */
  68. if (!buf->picref) {
  69. if ((ret = avfilter_request_frame(inlink)) < 0)
  70. return ret;
  71. }
  72. if (!buf->picref)
  73. return AVERROR(EINVAL);
  74. *picref = buf->picref;
  75. if (!(flags & AV_VSINK_BUF_FLAG_PEEK))
  76. buf->picref = NULL;
  77. return 0;
  78. }
  79. AVFilter avfilter_vsink_buffersink = {
  80. .name = "buffersink",
  81. .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
  82. .priv_size = sizeof(BufferSinkContext),
  83. .init = init,
  84. .uninit = uninit,
  85. .query_formats = query_formats,
  86. .inputs = (AVFilterPad[]) {{ .name = "default",
  87. .type = AVMEDIA_TYPE_VIDEO,
  88. .end_frame = end_frame,
  89. .min_perms = AV_PERM_READ, },
  90. { .name = NULL }},
  91. .outputs = (AVFilterPad[]) {{ .name = NULL }},
  92. };