vf_fifo.c 3.6 KB

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