vf_fifo.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2007 Bobby Bingham
  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. * FIFO buffering video filter
  23. */
  24. #include "avfilter.h"
  25. #include "video.h"
  26. typedef struct BufPic {
  27. AVFilterBufferRef *picref;
  28. struct BufPic *next;
  29. } BufPic;
  30. typedef struct {
  31. BufPic root;
  32. BufPic *last; ///< last buffered picture
  33. } FifoContext;
  34. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  35. {
  36. FifoContext *fifo = ctx->priv;
  37. fifo->last = &fifo->root;
  38. av_log(ctx, AV_LOG_INFO, "\n");
  39. return 0;
  40. }
  41. static av_cold void uninit(AVFilterContext *ctx)
  42. {
  43. FifoContext *fifo = ctx->priv;
  44. BufPic *pic, *tmp;
  45. for (pic = fifo->root.next; pic; pic = tmp) {
  46. tmp = pic->next;
  47. avfilter_unref_buffer(pic->picref);
  48. av_free(pic);
  49. }
  50. }
  51. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  52. {
  53. FifoContext *fifo = inlink->dst->priv;
  54. fifo->last->next = av_mallocz(sizeof(BufPic));
  55. fifo->last = fifo->last->next;
  56. fifo->last->picref = picref;
  57. }
  58. static void end_frame(AVFilterLink *inlink) { }
  59. static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
  60. static int request_frame(AVFilterLink *outlink)
  61. {
  62. FifoContext *fifo = outlink->src->priv;
  63. BufPic *tmp;
  64. int ret;
  65. if (!fifo->root.next) {
  66. if ((ret = avfilter_request_frame(outlink->src->inputs[0])) < 0)
  67. return ret;
  68. }
  69. /* by doing this, we give ownership of the reference to the next filter,
  70. * so we don't have to worry about dereferencing it ourselves. */
  71. avfilter_start_frame(outlink, fifo->root.next->picref);
  72. avfilter_draw_slice (outlink, 0, outlink->h, 1);
  73. avfilter_end_frame (outlink);
  74. if (fifo->last == fifo->root.next)
  75. fifo->last = &fifo->root;
  76. tmp = fifo->root.next->next;
  77. av_free(fifo->root.next);
  78. fifo->root.next = tmp;
  79. return 0;
  80. }
  81. AVFilter avfilter_vf_fifo = {
  82. .name = "fifo",
  83. .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
  84. .init = init,
  85. .uninit = uninit,
  86. .priv_size = sizeof(FifoContext),
  87. .inputs = (const AVFilterPad[]) {{ .name = "default",
  88. .type = AVMEDIA_TYPE_VIDEO,
  89. .get_video_buffer= ff_null_get_video_buffer,
  90. .start_frame = start_frame,
  91. .draw_slice = draw_slice,
  92. .end_frame = end_frame,
  93. .rej_perms = AV_PERM_REUSE2, },
  94. { .name = NULL}},
  95. .outputs = (const AVFilterPad[]) {{ .name = "default",
  96. .type = AVMEDIA_TYPE_VIDEO,
  97. .request_frame = request_frame, },
  98. { .name = NULL}},
  99. };